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         int                     devigone = DEVI(devi)->devi_gone;
8555 
8556         mutex_enter(&sd_detach_mutex);
8557 
8558         /*
8559          * Fail the detach for any of the following:
8560          * - Unable to get the sd_lun struct for the instance
8561          * - Another thread is already detaching this instance
8562          * - Another thread is currently performing an open
8563          *
8564          * Additionaly, if "device gone" flag is not set:
8565          * - There are outstanding commands in driver
8566          * - There are outstanding commands in transport
8567          */
8568         devp = ddi_get_driver_private(devi);
8569         if (devp == NULL || (un = (struct sd_lun *)devp->sd_private) == NULL ||
8570             un->un_detach_count != 0 || un->un_opens_in_progress != 0 ||
8571             (!devigone && (un->un_ncmds_in_driver != 0 ||
8572             un->un_ncmds_in_transport != 0 ||
8573             un->un_state == SD_STATE_RWAIT))) {
8574                 mutex_exit(&sd_detach_mutex);
8575                 return (DDI_FAILURE);
8576         }
8577 
8578         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "%s: entry 0x%p\n", __func__, un);
8579 
8580         /*
8581          * Mark this instance as currently in a detach, to inhibit any
8582          * opens from a layered driver.
8583          */
8584         un->un_detach_count++;
8585         mutex_exit(&sd_detach_mutex);
8586 
8587         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8588             SCSI_ADDR_PROP_TARGET, -1);
8589 
8590         dev = sd_make_device(SD_DEVINFO(un));
8591 
8592         mutex_enter(SD_MUTEX(un));
8593 
8594         /*
8595          * Fail the detach if there are any outstanding layered
8596          * opens on this device.
8597          */
8598         for (i = 0; i < NDKMAP; i++) {
8599                 if (un->un_ocmap.lyropen[i] != 0) {
8600                         goto err_notclosed;
8601                 }
8602         }
8603 
8604         /*
8605          * If we have the device reserved, release the reservation.
8606          */
8607         if (!devigone &&
8608             (un->un_resvd_status & SD_RESERVE) &&
8609             !(un->un_resvd_status & SD_LOST_RESERVE)) {
8610                 mutex_exit(SD_MUTEX(un));
8611                 /*
8612                  * Note: sd_reserve_release sends a command to the device
8613                  * via the sd_ioctlcmd() path, and can sleep.
8614                  */
8615                 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8616                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8617                             "%s: cannot release reservation\n", __func__);
8618                 }
8619         } else {
8620                 mutex_exit(SD_MUTEX(un));
8621         }
8622 
8623         /*
8624          * Untimeout any reserve recover, throttle reset, restart unit
8625          * and delayed broadcast timeout threads. Protect the timeout pointer
8626          * from getting nulled by their callback functions.
8627          */
8628         mutex_enter(SD_MUTEX(un));
8629         if (un->un_resvd_timeid != NULL) {
8630                 timeout_id_t temp_id = un->un_resvd_timeid;
8631                 un->un_resvd_timeid = NULL;
8632                 mutex_exit(SD_MUTEX(un));
8633                 (void) untimeout(temp_id);
8634                 mutex_enter(SD_MUTEX(un));
8635         }
8636 
8637         if (un->un_reset_throttle_timeid != NULL) {
8638                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8639                 un->un_reset_throttle_timeid = NULL;
8640                 mutex_exit(SD_MUTEX(un));
8641                 (void) untimeout(temp_id);
8642                 mutex_enter(SD_MUTEX(un));
8643         }
8644 
8645         if (un->un_startstop_timeid != NULL) {
8646                 timeout_id_t temp_id = un->un_startstop_timeid;
8647                 un->un_startstop_timeid = NULL;
8648                 mutex_exit(SD_MUTEX(un));
8649                 (void) untimeout(temp_id);
8650                 mutex_enter(SD_MUTEX(un));
8651         }
8652 
8653         if (un->un_rmw_msg_timeid != NULL) {
8654                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8655                 un->un_rmw_msg_timeid = NULL;
8656                 mutex_exit(SD_MUTEX(un));
8657                 (void) untimeout(temp_id);
8658                 mutex_enter(SD_MUTEX(un));
8659         }
8660 
8661         if (un->un_dcvb_timeid != NULL) {
8662                 timeout_id_t temp_id = un->un_dcvb_timeid;
8663                 un->un_dcvb_timeid = NULL;
8664                 mutex_exit(SD_MUTEX(un));
8665                 (void) untimeout(temp_id);
8666         } else {
8667                 mutex_exit(SD_MUTEX(un));
8668         }
8669 
8670         /* Remove any pending reservation reclaim requests for this device */
8671         sd_rmv_resv_reclaim_req(dev);
8672 
8673         mutex_enter(SD_MUTEX(un));
8674         if (un->un_retry_timeid != NULL) {
8675                 timeout_id_t temp_id = un->un_retry_timeid;
8676                 un->un_retry_timeid = NULL;
8677                 mutex_exit(SD_MUTEX(un));
8678                 (void) untimeout(temp_id);
8679                 mutex_enter(SD_MUTEX(un));
8680         }
8681 
8682         /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8683         if (un->un_direct_priority_timeid != NULL) {
8684                 timeout_id_t temp_id = un->un_direct_priority_timeid;
8685                 un->un_direct_priority_timeid = NULL;
8686                 mutex_exit(SD_MUTEX(un));
8687                 (void) untimeout(temp_id);
8688                 mutex_enter(SD_MUTEX(un));
8689         }
8690 
8691         /* Cancel any active multi-host disk watch thread requests */
8692         if (un->un_mhd_token != NULL) {
8693                 mutex_exit(SD_MUTEX(un));
8694                  _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8695                 if (scsi_watch_request_terminate(un->un_mhd_token,
8696                     SCSI_WATCH_TERMINATE_NOWAIT)) {
8697                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8698                             "%s: cannot cancel mhd watch request\n", __func__);
8699                         /*
8700                          * Note: We are returning here after having removed
8701                          * some driver timeouts above. This is consistent with
8702                          * the legacy implementation but perhaps the watch
8703                          * terminate call should be made with the wait flag set.
8704                          */
8705                         goto err_stillbusy;
8706                 }
8707                 mutex_enter(SD_MUTEX(un));
8708                 un->un_mhd_token = NULL;
8709         }
8710 
8711         if (un->un_swr_token != NULL) {
8712                 mutex_exit(SD_MUTEX(un));
8713                 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8714                 if (scsi_watch_request_terminate(un->un_swr_token,
8715                     SCSI_WATCH_TERMINATE_NOWAIT)) {
8716                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8717                             "%s: cannot cancel swr watch request\n", __func__);
8718                         /*
8719                          * Note: We are returning here after having removed
8720                          * some driver timeouts above. This is consistent with
8721                          * the legacy implementation but perhaps the watch
8722                          * terminate call should be made with the wait flag set.
8723                          */
8724                         goto err_stillbusy;
8725                 }
8726                 mutex_enter(SD_MUTEX(un));
8727                 un->un_swr_token = NULL;
8728         }
8729 
8730         /*
8731          * Clear any scsi_reset_notifies. We clear the reset notifies
8732          * if we have not registered one.
8733          * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8734          */
8735         mutex_exit(SD_MUTEX(un));
8736         (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8737             sd_mhd_reset_notify_cb, (caddr_t)un);
8738 
8739         /*
8740          * protect the timeout pointers from getting nulled by
8741          * their callback functions during the cancellation process.
8742          * In such a scenario untimeout can be invoked with a null value.
8743          */
8744         _NOTE(NO_COMPETING_THREADS_NOW);
8745 
8746         mutex_enter(&un->un_pm_mutex);
8747         if (un->un_pm_idle_timeid != NULL) {
8748                 timeout_id_t temp_id = un->un_pm_idle_timeid;
8749                 un->un_pm_idle_timeid = NULL;
8750                 mutex_exit(&un->un_pm_mutex);
8751 
8752                 /*
8753                  * Timeout is active; cancel it.
8754                  * Note that it'll never be active on a device
8755                  * that does not support PM therefore we don't
8756                  * have to check before calling pm_idle_component.
8757                  */
8758                 (void) untimeout(temp_id);
8759                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8760                 mutex_enter(&un->un_pm_mutex);
8761         }
8762 
8763         /*
8764          * Check whether there is already a timeout scheduled for power
8765          * management. If yes then don't lower the power here, that's.
8766          * the timeout handler's job.
8767          */
8768         if (un->un_pm_timeid != NULL) {
8769                 timeout_id_t temp_id = un->un_pm_timeid;
8770                 un->un_pm_timeid = NULL;
8771                 mutex_exit(&un->un_pm_mutex);
8772                 /*
8773                  * Timeout is active; cancel it.
8774                  * Note that it'll never be active on a device
8775                  * that does not support PM therefore we don't
8776                  * have to check before calling pm_idle_component.
8777                  */
8778                 (void) untimeout(temp_id);
8779                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8780 
8781         } else {
8782                 mutex_exit(&un->un_pm_mutex);
8783                 if ((un->un_f_pm_is_enabled == TRUE) &&
8784                     (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8785                     != DDI_SUCCESS)) {
8786                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8787                             "%s: lower power request failed, ignoring\n",
8788                             __func__);
8789                         /*
8790                          * The above test now includes a check to see if PM is
8791                          * supported by this device before call
8792                          * pm_lower_power().
8793                          * Note, the following is not dead code. The call to
8794                          * pm_lower_power above will generate a call back into
8795                          * our sdpower routine which might result in a timeout
8796                          * handler getting activated. Therefore the following
8797                          * code is valid and necessary.
8798                          */
8799                         mutex_enter(&un->un_pm_mutex);
8800                         if (un->un_pm_timeid != NULL) {
8801                                 timeout_id_t temp_id = un->un_pm_timeid;
8802                                 un->un_pm_timeid = NULL;
8803                                 mutex_exit(&un->un_pm_mutex);
8804                                 (void) untimeout(temp_id);
8805                                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8806                         } else {
8807                                 mutex_exit(&un->un_pm_mutex);
8808                         }
8809                 }
8810         }
8811 
8812         /*
8813          * Cleanup from the scsi_ifsetcap() calls (437868)
8814          * Relocated here from above to be after the call to
8815          * pm_lower_power, which was getting errors.
8816          */
8817         (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8818         (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8819 
8820         /*
8821          * Currently, tagged queuing is supported per target based by HBA.
8822          * Setting this per lun instance actually sets the capability of this
8823          * target in HBA, which affects those luns already attached on the
8824          * same target. So during detach, we can only disable this capability
8825          * only when this is the only lun left on this target. By doing
8826          * this, we assume a target has the same tagged queuing capability
8827          * for every lun. The condition can be removed when HBA is changed to
8828          * support per lun based tagged queuing capability.
8829          */
8830         if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8831                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8832         }
8833 
8834         if (un->un_f_is_fibre == FALSE) {
8835                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8836         }
8837 
8838         /*
8839          * Remove any event callbacks, fibre only
8840          */
8841         if (un->un_f_is_fibre == TRUE) {
8842                 if ((un->un_insert_event != NULL) &&
8843                     (ddi_remove_event_handler(un->un_insert_cb_id) !=
8844                     DDI_SUCCESS)) {
8845                         /*
8846                          * Note: We are returning here after having done
8847                          * substantial cleanup above. This is consistent
8848                          * with the legacy implementation but this may not
8849                          * be the right thing to do.
8850                          */
8851                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8852                             "%s: cannot cancel insert event\n", __func__);
8853                         goto err_remove_event;
8854                 }
8855                 un->un_insert_event = NULL;
8856 
8857                 if ((un->un_remove_event != NULL) &&
8858                     (ddi_remove_event_handler(un->un_remove_cb_id) !=
8859                     DDI_SUCCESS)) {
8860                         /*
8861                          * Note: We are returning here after having done
8862                          * substantial cleanup above. This is consistent
8863                          * with the legacy implementation but this may not
8864                          * be the right thing to do.
8865                          */
8866                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8867                             "%s: cannot cancel remove event\n", __func__);
8868                         goto err_remove_event;
8869                 }
8870                 un->un_remove_event = NULL;
8871         }
8872 
8873         /* Do not free the softstate if the callback routine is active */
8874         sd_sync_with_callback(un);
8875 
8876         cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8877         cmlb_free_handle(&un->un_cmlbhandle);
8878 
8879         /*
8880          * Hold the detach mutex here, to make sure that no other threads ever
8881          * can access a (partially) freed soft state structure.
8882          */
8883         mutex_enter(&sd_detach_mutex);
8884 
8885         /*
8886          * Clean up the soft state struct.
8887          * Cleanup is done in reverse order of allocs/inits.
8888          * At this point there should be no competing threads anymore.
8889          */
8890 
8891         scsi_fm_fini(devp);
8892 
8893         /*
8894          * Deallocate memory for SCSI FMA.
8895          */
8896         kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8897 
8898         /*
8899          * Unregister and free device id if it was not registered
8900          * by the transport.
8901          */
8902         if (un->un_f_devid_transport_defined == FALSE)
8903                 ddi_devid_unregister(devi);
8904 
8905         /*
8906          * free the devid structure if allocated before (by ddi_devid_init()
8907          * or ddi_devid_get()).
8908          */
8909         if (un->un_devid) {
8910                 ddi_devid_free(un->un_devid);
8911                 un->un_devid = NULL;
8912         }
8913 
8914         /*
8915          * Destroy wmap cache if it exists.
8916          */
8917         if (un->un_wm_cache != NULL) {
8918                 kmem_cache_destroy(un->un_wm_cache);
8919                 un->un_wm_cache = NULL;
8920         }
8921 
8922         /*
8923          * kstat cleanup is done in detach for all device types (4363169).
8924          * We do not want to fail detach if the device kstats are not deleted
8925          * since there is a confusion about the devo_refcnt for the device.
8926          * We just delete the kstats and let detach complete successfully.
8927          */
8928         if (un->un_stats != NULL) {
8929                 kstat_delete(un->un_stats);
8930                 un->un_stats = NULL;
8931         }
8932         if (un->un_errstats != NULL) {
8933                 kstat_delete(un->un_errstats);
8934                 un->un_errstats = NULL;
8935         }
8936 
8937         /* Remove partition stats */
8938         if (un->un_f_pkstats_enabled) {
8939                 for (i = 0; i < NSDMAP; i++) {
8940                         if (un->un_pstats[i] != NULL) {
8941                                 kstat_delete(un->un_pstats[i]);
8942                                 un->un_pstats[i] = NULL;
8943                         }
8944                 }
8945         }
8946 
8947         /* Remove xbuf registration */
8948         ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8949         ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8950 
8951         /* Remove driver properties */
8952         ddi_prop_remove_all(devi);
8953 
8954         mutex_destroy(&un->un_pm_mutex);
8955         cv_destroy(&un->un_pm_busy_cv);
8956 
8957         cv_destroy(&un->un_wcc_cv);
8958 
8959         /* Open/close semaphore */
8960         sema_destroy(&un->un_semoclose);
8961 
8962         /* Removable media condvar. */
8963         cv_destroy(&un->un_state_cv);
8964 
8965         /* Suspend/resume condvar. */
8966         cv_destroy(&un->un_suspend_cv);
8967         cv_destroy(&un->un_disk_busy_cv);
8968 
8969         sd_free_rqs(un);
8970 
8971         /* Free up soft state */
8972         devp->sd_private = NULL;
8973 
8974         bzero(un, sizeof (struct sd_lun));
8975 
8976         ddi_soft_state_free(sd_state, instance);
8977 
8978         mutex_exit(&sd_detach_mutex);
8979 
8980         /* This frees up the INQUIRY data associated with the device. */
8981         scsi_unprobe(devp);
8982 
8983         /*
8984          * After successfully detaching an instance, we update the information
8985          * of how many luns have been attached in the relative target and
8986          * controller for parallel SCSI. This information is used when sd tries
8987          * to set the tagged queuing capability in HBA.
8988          * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8989          * check if the device is parallel SCSI. However, we don't need to
8990          * check here because we've already checked during attach. No device
8991          * that is not parallel SCSI is in the chain.
8992          */
8993         if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8994                 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
8995         }
8996 
8997         return (DDI_SUCCESS);
8998 
8999 err_notclosed:
9000         mutex_exit(SD_MUTEX(un));
9001 
9002 err_stillbusy:
9003         _NOTE(NO_COMPETING_THREADS_NOW);
9004 
9005 err_remove_event:
9006         mutex_enter(&sd_detach_mutex);
9007         un->un_detach_count--;
9008         mutex_exit(&sd_detach_mutex);
9009 
9010         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "%s: exit failure\n", __func__);
9011         return (DDI_FAILURE);
9012 }
9013 
9014 
9015 /*
9016  *    Function: sd_create_errstats
9017  *
9018  * Description: This routine instantiates the device error stats.
9019  *
9020  *              Note: During attach the stats are instantiated first so they are
9021  *              available for attach-time routines that utilize the driver
9022  *              iopath to send commands to the device. The stats are initialized
9023  *              separately so data obtained during some attach-time routines is
9024  *              available. (4362483)
9025  *
9026  *   Arguments: un - driver soft state (unit) structure
9027  *              instance - driver instance
9028  *
9029  *     Context: Kernel thread context
9030  */
9031 
9032 static void
9033 sd_create_errstats(struct sd_lun *un, int instance)
9034 {
9035         struct  sd_errstats     *stp;
9036         char    kstatmodule_err[KSTAT_STRLEN];
9037         char    kstatname[KSTAT_STRLEN];
9038         int     ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9039 
9040         ASSERT(un != NULL);
9041 
9042         if (un->un_errstats != NULL) {
9043                 return;
9044         }
9045 
9046         (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9047             "%serr", sd_label);
9048         (void) snprintf(kstatname, sizeof (kstatname),
9049             "%s%d,err", sd_label, instance);
9050 
9051         un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9052             "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9053 
9054         if (un->un_errstats == NULL) {
9055                 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9056                     "sd_create_errstats: Failed kstat_create\n");
9057                 return;
9058         }
9059 
9060         stp = (struct sd_errstats *)un->un_errstats->ks_data;
9061         kstat_named_init(&stp->sd_softerrs,      "Soft Errors",
9062             KSTAT_DATA_UINT32);
9063         kstat_named_init(&stp->sd_harderrs,      "Hard Errors",
9064             KSTAT_DATA_UINT32);
9065         kstat_named_init(&stp->sd_transerrs,     "Transport Errors",
9066             KSTAT_DATA_UINT32);
9067         kstat_named_init(&stp->sd_vid,           "Vendor",
9068             KSTAT_DATA_CHAR);
9069         kstat_named_init(&stp->sd_pid,           "Product",
9070             KSTAT_DATA_CHAR);
9071         kstat_named_init(&stp->sd_revision,      "Revision",
9072             KSTAT_DATA_CHAR);
9073         kstat_named_init(&stp->sd_serial,        "Serial No",
9074             KSTAT_DATA_CHAR);
9075         kstat_named_init(&stp->sd_capacity,      "Size",
9076             KSTAT_DATA_ULONGLONG);
9077         kstat_named_init(&stp->sd_rq_media_err,  "Media Error",
9078             KSTAT_DATA_UINT32);
9079         kstat_named_init(&stp->sd_rq_ntrdy_err,  "Device Not Ready",
9080             KSTAT_DATA_UINT32);
9081         kstat_named_init(&stp->sd_rq_nodev_err,  "No Device",
9082             KSTAT_DATA_UINT32);
9083         kstat_named_init(&stp->sd_rq_recov_err,  "Recoverable",
9084             KSTAT_DATA_UINT32);
9085         kstat_named_init(&stp->sd_rq_illrq_err,  "Illegal Request",
9086             KSTAT_DATA_UINT32);
9087         kstat_named_init(&stp->sd_rq_pfa_err,    "Predictive Failure Analysis",
9088             KSTAT_DATA_UINT32);
9089 
9090         un->un_errstats->ks_private = un;
9091         un->un_errstats->ks_update  = nulldev;
9092 
9093         kstat_install(un->un_errstats);
9094 }
9095 
9096 
9097 /*
9098  *    Function: sd_set_errstats
9099  *
9100  * Description: This routine sets the value of the vendor id, product id,
9101  *              revision, serial number, and capacity device error stats.
9102  *
9103  *              Note: During attach the stats are instantiated first so they are
9104  *              available for attach-time routines that utilize the driver
9105  *              iopath to send commands to the device. The stats are initialized
9106  *              separately so data obtained during some attach-time routines is
9107  *              available. (4362483)
9108  *
9109  *   Arguments: un - driver soft state (unit) structure
9110  *
9111  *     Context: Kernel thread context
9112  */
9113 
9114 static void
9115 sd_set_errstats(struct sd_lun *un)
9116 {
9117         struct  sd_errstats     *stp;
9118         char                    *sn;
9119 
9120         ASSERT(un != NULL);
9121         ASSERT(un->un_errstats != NULL);
9122         stp = (struct sd_errstats *)un->un_errstats->ks_data;
9123         ASSERT(stp != NULL);
9124         (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9125         (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9126         (void) strncpy(stp->sd_revision.value.c,
9127             un->un_sd->sd_inq->inq_revision, 4);
9128 
9129         /*
9130          * All the errstats are persistent across detach/attach,
9131          * so reset all the errstats here in case of the hot
9132          * replacement of disk drives, except for not changed
9133          * Sun qualified drives.
9134          */
9135         if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9136             (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9137             sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9138                 stp->sd_softerrs.value.ui32 = 0;
9139                 stp->sd_harderrs.value.ui32 = 0;
9140                 stp->sd_transerrs.value.ui32 = 0;
9141                 stp->sd_rq_media_err.value.ui32 = 0;
9142                 stp->sd_rq_ntrdy_err.value.ui32 = 0;
9143                 stp->sd_rq_nodev_err.value.ui32 = 0;
9144                 stp->sd_rq_recov_err.value.ui32 = 0;
9145                 stp->sd_rq_illrq_err.value.ui32 = 0;
9146                 stp->sd_rq_pfa_err.value.ui32 = 0;
9147         }
9148 
9149         /*
9150          * Set the "Serial No" kstat for Sun qualified drives (indicated by
9151          * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9152          * (4376302))
9153          */
9154         if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9155                 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9156                     sizeof (SD_INQUIRY(un)->inq_serial));
9157         } else {
9158                 /*
9159                  * Set the "Serial No" kstat for non-Sun qualified drives
9160                  */
9161                 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9162                     DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9163                     INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9164                         (void) strlcpy(stp->sd_serial.value.c, sn,
9165                             sizeof (stp->sd_serial.value.c));
9166                         ddi_prop_free(sn);
9167                 }
9168         }
9169 
9170         if (un->un_f_blockcount_is_valid != TRUE) {
9171                 /*
9172                  * Set capacity error stat to 0 for no media. This ensures
9173                  * a valid capacity is displayed in response to 'iostat -E'
9174                  * when no media is present in the device.
9175                  */
9176                 stp->sd_capacity.value.ui64 = 0;
9177         } else {
9178                 /*
9179                  * Multiply un_blockcount by un->un_sys_blocksize to get
9180                  * capacity.
9181                  *
9182                  * Note: for non-512 blocksize devices "un_blockcount" has been
9183                  * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9184                  * (un_tgt_blocksize / un->un_sys_blocksize).
9185                  */
9186                 stp->sd_capacity.value.ui64 = (uint64_t)
9187                     ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9188         }
9189 }
9190 
9191 
9192 /*
9193  *    Function: sd_set_pstats
9194  *
9195  * Description: This routine instantiates and initializes the partition
9196  *              stats for each partition with more than zero blocks.
9197  *              (4363169)
9198  *
9199  *   Arguments: un - driver soft state (unit) structure
9200  *
9201  *     Context: Kernel thread context
9202  */
9203 
9204 static void
9205 sd_set_pstats(struct sd_lun *un)
9206 {
9207         char    kstatname[KSTAT_STRLEN];
9208         int     instance;
9209         int     i;
9210         diskaddr_t      nblks = 0;
9211         char    *partname = NULL;
9212 
9213         ASSERT(un != NULL);
9214 
9215         instance = ddi_get_instance(SD_DEVINFO(un));
9216 
9217         /* Note:x86: is this a VTOC8/VTOC16 difference? */
9218         for (i = 0; i < NSDMAP; i++) {
9219 
9220                 if (cmlb_partinfo(un->un_cmlbhandle, i,
9221                     &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9222                         continue;
9223                 mutex_enter(SD_MUTEX(un));
9224 
9225                 if ((un->un_pstats[i] == NULL) &&
9226                     (nblks != 0)) {
9227 
9228                         (void) snprintf(kstatname, sizeof (kstatname),
9229                             "%s%d,%s", sd_label, instance,
9230                             partname);
9231 
9232                         un->un_pstats[i] = kstat_create(sd_label,
9233                             instance, kstatname, "partition", KSTAT_TYPE_IO,
9234                             1, KSTAT_FLAG_PERSISTENT);
9235                         if (un->un_pstats[i] != NULL) {
9236                                 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9237                                 kstat_install(un->un_pstats[i]);
9238                         }
9239                 }
9240                 mutex_exit(SD_MUTEX(un));
9241         }
9242 }
9243 
9244 
9245 #if (defined(__fibre))
9246 /*
9247  *    Function: sd_init_event_callbacks
9248  *
9249  * Description: This routine initializes the insertion and removal event
9250  *              callbacks. (fibre only)
9251  *
9252  *   Arguments: un - driver soft state (unit) structure
9253  *
9254  *     Context: Kernel thread context
9255  */
9256 
9257 static void
9258 sd_init_event_callbacks(struct sd_lun *un)
9259 {
9260         ASSERT(un != NULL);
9261 
9262         if ((un->un_insert_event == NULL) &&
9263             (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9264             &un->un_insert_event) == DDI_SUCCESS)) {
9265                 /*
9266                  * Add the callback for an insertion event
9267                  */
9268                 (void) ddi_add_event_handler(SD_DEVINFO(un),
9269                     un->un_insert_event, sd_event_callback, (void *)un,
9270                     &(un->un_insert_cb_id));
9271         }
9272 
9273         if ((un->un_remove_event == NULL) &&
9274             (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9275             &un->un_remove_event) == DDI_SUCCESS)) {
9276                 /*
9277                  * Add the callback for a removal event
9278                  */
9279                 (void) ddi_add_event_handler(SD_DEVINFO(un),
9280                     un->un_remove_event, sd_event_callback, (void *)un,
9281                     &(un->un_remove_cb_id));
9282         }
9283 }
9284 
9285 
9286 /*
9287  *    Function: sd_event_callback
9288  *
9289  * Description: This routine handles insert/remove events (photon). The
9290  *              state is changed to OFFLINE which can be used to supress
9291  *              error msgs. (fibre only)
9292  *
9293  *   Arguments: un - driver soft state (unit) structure
9294  *
9295  *     Context: Callout thread context
9296  */
9297 /* ARGSUSED */
9298 static void
9299 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9300     void *bus_impldata)
9301 {
9302         struct sd_lun *un = (struct sd_lun *)arg;
9303 
9304         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9305         if (event == un->un_insert_event) {
9306                 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9307                 mutex_enter(SD_MUTEX(un));
9308                 if (un->un_state == SD_STATE_OFFLINE) {
9309                         if (un->un_last_state != SD_STATE_SUSPENDED) {
9310                                 un->un_state = un->un_last_state;
9311                         } else {
9312                                 /*
9313                                  * We have gone through SUSPEND/RESUME while
9314                                  * we were offline. Restore the last state
9315                                  */
9316                                 un->un_state = un->un_save_state;
9317                         }
9318                 }
9319                 mutex_exit(SD_MUTEX(un));
9320 
9321         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9322         } else if (event == un->un_remove_event) {
9323                 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9324                 mutex_enter(SD_MUTEX(un));
9325                 /*
9326                  * We need to handle an event callback that occurs during
9327                  * the suspend operation, since we don't prevent it.
9328                  */
9329                 if (un->un_state != SD_STATE_OFFLINE) {
9330                         if (un->un_state != SD_STATE_SUSPENDED) {
9331                                 New_state(un, SD_STATE_OFFLINE);
9332                         } else {
9333                                 un->un_last_state = SD_STATE_OFFLINE;
9334                         }
9335                 }
9336                 mutex_exit(SD_MUTEX(un));
9337         } else {
9338                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9339                     "!Unknown event\n");
9340         }
9341 
9342 }
9343 #endif
9344 
9345 /*
9346  * Values related to caching mode page depending on whether the unit is ATAPI.
9347  */
9348 #define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9349         CDB_GROUP1 : CDB_GROUP0)
9350 #define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9351         MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9352 /*
9353  * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9354  * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9355  */
9356 #define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9357         sizeof (struct mode_cache_scsi3))
9358 
9359 static int
9360 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9361     int *bdlen)
9362 {
9363         struct sd_lun   *un = ssc->ssc_un;
9364         struct mode_caching *mode_caching_page;
9365         size_t          buflen = SDC_BUFLEN(un);
9366         int             hdrlen = SDC_HDRLEN(un);
9367         int             rval;
9368 
9369         /*
9370          * Do a test unit ready, otherwise a mode sense may not work if this
9371          * is the first command sent to the device after boot.
9372          */
9373         if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
9374                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9375 
9376         /*
9377          * Allocate memory for the retrieved mode page and its headers.  Set
9378          * a pointer to the page itself.
9379          */
9380         *header = kmem_zalloc(buflen, KM_SLEEP);
9381 
9382         /* Get the information from the device */
9383         rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
9384             page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
9385         if (rval != 0) {
9386                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
9387                     __func__);
9388                 goto mode_sense_failed;
9389         }
9390 
9391         /*
9392          * Determine size of Block Descriptors in order to locate
9393          * the mode page data. ATAPI devices return 0, SCSI devices
9394          * should return MODE_BLK_DESC_LENGTH.
9395          */
9396         if (un->un_f_cfg_is_atapi == TRUE) {
9397                 struct mode_header_grp2 *mhp =
9398                     (struct mode_header_grp2 *)(*header);
9399                 *bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9400         } else {
9401                 *bdlen = ((struct mode_header *)(*header))->bdesc_length;
9402         }
9403 
9404         if (*bdlen > MODE_BLK_DESC_LENGTH) {
9405                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9406                     "%s: Mode Sense returned invalid block descriptor length\n",
9407                     __func__);
9408                 rval = EIO;
9409                 goto mode_sense_failed;
9410         }
9411 
9412         mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
9413         if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9414                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9415                     "%s: Mode Sense caching page code mismatch %d\n",
9416                     __func__, mode_caching_page->mode_page.code);
9417                 rval = EIO;
9418         }
9419 
9420 mode_sense_failed:
9421         if (rval != 0) {
9422                 kmem_free(*header, buflen);
9423                 *header = NULL;
9424                 *bdlen = 0;
9425         }
9426         return (rval);
9427 }
9428 
9429 /*
9430  *    Function: sd_cache_control()
9431  *
9432  * Description: This routine is the driver entry point for setting
9433  *              read and write caching by modifying the WCE (write cache
9434  *              enable) and RCD (read cache disable) bits of mode
9435  *              page 8 (MODEPAGE_CACHING).
9436  *
9437  *   Arguments: ssc             - ssc contains pointer to driver soft state
9438  *                                (unit) structure for this target.
9439  *              rcd_flag        - flag for controlling the read cache
9440  *              wce_flag        - flag for controlling the write cache
9441  *
9442  * Return Code: EIO
9443  *              code returned by sd_send_scsi_MODE_SENSE and
9444  *              sd_send_scsi_MODE_SELECT
9445  *
9446  *     Context: Kernel Thread
9447  */
9448 
9449 static int
9450 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9451 {
9452         struct sd_lun   *un = ssc->ssc_un;
9453         struct mode_caching *mode_caching_page;
9454         uchar_t         *header;
9455         size_t          buflen = SDC_BUFLEN(un);
9456         int             hdrlen = SDC_HDRLEN(un);
9457         int             bdlen;
9458         int             rval;
9459 
9460         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9461         switch (rval) {
9462         case 0:
9463                 /* Check the relevant bits on successful mode sense */
9464                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9465                     bdlen);
9466                 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9467                     (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9468                     (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9469                     (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9470                         size_t sbuflen;
9471                         uchar_t save_pg;
9472 
9473                         /*
9474                          * Construct select buffer length based on the
9475                          * length of the sense data returned.
9476                          */
9477                         sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
9478                             (int)mode_caching_page->mode_page.length;
9479 
9480                         /* Set the caching bits as requested */
9481                         if (rcd_flag == SD_CACHE_ENABLE)
9482                                 mode_caching_page->rcd = 0;
9483                         else if (rcd_flag == SD_CACHE_DISABLE)
9484                                 mode_caching_page->rcd = 1;
9485 
9486                         if (wce_flag == SD_CACHE_ENABLE)
9487                                 mode_caching_page->wce = 1;
9488                         else if (wce_flag == SD_CACHE_DISABLE)
9489                                 mode_caching_page->wce = 0;
9490 
9491                         /*
9492                          * Save the page if the mode sense says the
9493                          * drive supports it.
9494                          */
9495                         save_pg = mode_caching_page->mode_page.ps ?
9496                             SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9497 
9498                         /* Clear reserved bits before mode select */
9499                         mode_caching_page->mode_page.ps = 0;
9500 
9501                         /*
9502                          * Clear out mode header for mode select.
9503                          * The rest of the retrieved page will be reused.
9504                          */
9505                         bzero(header, hdrlen);
9506 
9507                         if (un->un_f_cfg_is_atapi == TRUE) {
9508                                 struct mode_header_grp2 *mhp =
9509                                     (struct mode_header_grp2 *)header;
9510                                 mhp->bdesc_length_hi = bdlen >> 8;
9511                                 mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
9512                         } else {
9513                                 ((struct mode_header *)header)->bdesc_length =
9514                                     bdlen;
9515                         }
9516 
9517                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9518 
9519                         /* Issue mode select to change the cache settings */
9520                         rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
9521                             header, sbuflen, save_pg, SD_PATH_DIRECT);
9522                 }
9523                 kmem_free(header, buflen);
9524                 break;
9525         case EIO:
9526                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9527                 break;
9528         default:
9529                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9530                 break;
9531         }
9532 
9533         return (rval);
9534 }
9535 
9536 
9537 /*
9538  *    Function: sd_get_write_cache_enabled()
9539  *
9540  * Description: This routine is the driver entry point for determining if write
9541  *              caching is enabled.  It examines the WCE (write cache enable)
9542  *              bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9543  *              bits set to MODEPAGE_CURRENT.
9544  *
9545  *   Arguments: ssc             - ssc contains pointer to driver soft state
9546  *                                (unit) structure for this target.
9547  *              is_enabled      - pointer to int where write cache enabled state
9548  *                                is returned (non-zero -> write cache enabled)
9549  *
9550  * Return Code: EIO
9551  *              code returned by sd_send_scsi_MODE_SENSE
9552  *
9553  *     Context: Kernel Thread
9554  *
9555  * NOTE: If ioctl is added to disable write cache, this sequence should
9556  * be followed so that no locking is required for accesses to
9557  * un->un_f_write_cache_enabled:
9558  *      do mode select to clear wce
9559  *      do synchronize cache to flush cache
9560  *      set un->un_f_write_cache_enabled = FALSE
9561  *
9562  * Conversely, an ioctl to enable the write cache should be done
9563  * in this order:
9564  *      set un->un_f_write_cache_enabled = TRUE
9565  *      do mode select to set wce
9566  */
9567 
9568 static int
9569 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9570 {
9571         struct sd_lun   *un = ssc->ssc_un;
9572         struct mode_caching *mode_caching_page;
9573         uchar_t         *header;
9574         size_t          buflen = SDC_BUFLEN(un);
9575         int             hdrlen = SDC_HDRLEN(un);
9576         int             bdlen;
9577         int             rval;
9578 
9579         /* In case of error, flag as enabled */
9580         *is_enabled = TRUE;
9581 
9582         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9583         switch (rval) {
9584         case 0:
9585                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9586                     bdlen);
9587                 *is_enabled = mode_caching_page->wce;
9588                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9589                 kmem_free(header, buflen);
9590                 break;
9591         case EIO: {
9592                 /*
9593                  * Some disks do not support Mode Sense(6), we
9594                  * should ignore this kind of error (sense key is
9595                  * 0x5 - illegal request).
9596                  */
9597                 uint8_t *sensep;
9598                 int senlen;
9599 
9600                 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9601                 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9602                     ssc->ssc_uscsi_cmd->uscsi_rqresid);
9603 
9604                 if (senlen > 0 &&
9605                     scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9606                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9607                 } else {
9608                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9609                 }
9610                 break;
9611         }
9612         default:
9613                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9614                 break;
9615         }
9616 
9617         return (rval);
9618 }
9619 
9620 /*
9621  *    Function: sd_get_write_cache_changeable()
9622  *
9623  * Description: This routine is the driver entry point for determining if write
9624  *              caching is changeable.  It examines the WCE (write cache enable)
9625  *              bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9626  *              bits set to MODEPAGE_CHANGEABLE.
9627  *
9628  *   Arguments: ssc             - ssc contains pointer to driver soft state
9629  *                                (unit) structure for this target.
9630  *              is_changeable   - pointer to int where write cache changeable
9631  *                                state is returned (non-zero -> write cache
9632  *                                changeable)
9633  *
9634  *     Context: Kernel Thread
9635  */
9636 
9637 static void
9638 sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
9639 {
9640         struct sd_lun   *un = ssc->ssc_un;
9641         struct mode_caching *mode_caching_page;
9642         uchar_t         *header;
9643         size_t          buflen = SDC_BUFLEN(un);
9644         int             hdrlen = SDC_HDRLEN(un);
9645         int             bdlen;
9646         int             rval;
9647 
9648         /* In case of error, flag as enabled */
9649         *is_changeable = TRUE;
9650 
9651         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
9652             &bdlen);
9653         switch (rval) {
9654         case 0:
9655                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9656                     bdlen);
9657                 *is_changeable = mode_caching_page->wce;
9658                 kmem_free(header, buflen);
9659                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9660                 break;
9661         case EIO:
9662                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9663                 break;
9664         default:
9665                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9666                 break;
9667         }
9668 }
9669 
9670 /*
9671  *    Function: sd_get_nv_sup()
9672  *
9673  * Description: This routine is the driver entry point for
9674  * determining whether non-volatile cache is supported. This
9675  * determination process works as follows:
9676  *
9677  * 1. sd first queries sd.conf on whether
9678  * suppress_cache_flush bit is set for this device.
9679  *
9680  * 2. if not there, then queries the internal disk table.
9681  *
9682  * 3. if either sd.conf or internal disk table specifies
9683  * cache flush be suppressed, we don't bother checking
9684  * NV_SUP bit.
9685  *
9686  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9687  * the optional INQUIRY VPD page 0x86. If the device
9688  * supports VPD page 0x86, sd examines the NV_SUP
9689  * (non-volatile cache support) bit in the INQUIRY VPD page
9690  * 0x86:
9691  *   o If NV_SUP bit is set, sd assumes the device has a
9692  *   non-volatile cache and set the
9693  *   un_f_sync_nv_supported to TRUE.
9694  *   o Otherwise cache is not non-volatile,
9695  *   un_f_sync_nv_supported is set to FALSE.
9696  *
9697  * Arguments: un - driver soft state (unit) structure
9698  *
9699  * Return Code:
9700  *
9701  *     Context: Kernel Thread
9702  */
9703 
9704 static void
9705 sd_get_nv_sup(sd_ssc_t *ssc)
9706 {
9707         int             rval            = 0;
9708         uchar_t         *inq86          = NULL;
9709         size_t          inq86_len       = MAX_INQUIRY_SIZE;
9710         size_t          inq86_resid     = 0;
9711         struct          dk_callback *dkc;
9712         struct sd_lun   *un;
9713 
9714         ASSERT(ssc != NULL);
9715         un = ssc->ssc_un;
9716         ASSERT(un != NULL);
9717 
9718         mutex_enter(SD_MUTEX(un));
9719 
9720         /*
9721          * Be conservative on the device's support of
9722          * SYNC_NV bit: un_f_sync_nv_supported is
9723          * initialized to be false.
9724          */
9725         un->un_f_sync_nv_supported = FALSE;
9726 
9727         /*
9728          * If either sd.conf or internal disk table
9729          * specifies cache flush be suppressed, then
9730          * we don't bother checking NV_SUP bit.
9731          */
9732         if (un->un_f_suppress_cache_flush == TRUE) {
9733                 mutex_exit(SD_MUTEX(un));
9734                 return;
9735         }
9736 
9737         if (sd_check_vpd_page_support(ssc) == 0 &&
9738             un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9739                 mutex_exit(SD_MUTEX(un));
9740                 /* collect page 86 data if available */
9741                 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9742 
9743                 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9744                     0x01, 0x86, &inq86_resid);
9745 
9746                 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9747                         SD_TRACE(SD_LOG_COMMON, un,
9748                             "sd_get_nv_sup: \
9749                             successfully get VPD page: %x \
9750                             PAGE LENGTH: %x BYTE 6: %x\n",
9751                             inq86[1], inq86[3], inq86[6]);
9752 
9753                         mutex_enter(SD_MUTEX(un));
9754                         /*
9755                          * check the value of NV_SUP bit: only if the device
9756                          * reports NV_SUP bit to be 1, the
9757                          * un_f_sync_nv_supported bit will be set to true.
9758                          */
9759                         if (inq86[6] & SD_VPD_NV_SUP) {
9760                                 un->un_f_sync_nv_supported = TRUE;
9761                         }
9762                         mutex_exit(SD_MUTEX(un));
9763                 } else if (rval != 0) {
9764                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9765                 }
9766 
9767                 kmem_free(inq86, inq86_len);
9768         } else {
9769                 mutex_exit(SD_MUTEX(un));
9770         }
9771 
9772         /*
9773          * Send a SYNC CACHE command to check whether
9774          * SYNC_NV bit is supported. This command should have
9775          * un_f_sync_nv_supported set to correct value.
9776          */
9777         mutex_enter(SD_MUTEX(un));
9778         if (un->un_f_sync_nv_supported) {
9779                 mutex_exit(SD_MUTEX(un));
9780                 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9781                 dkc->dkc_flag = FLUSH_VOLATILE;
9782                 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9783 
9784                 /*
9785                  * Send a TEST UNIT READY command to the device. This should
9786                  * clear any outstanding UNIT ATTENTION that may be present.
9787                  */
9788                 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9789                 if (rval != 0)
9790                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9791 
9792                 kmem_free(dkc, sizeof (struct dk_callback));
9793         } else {
9794                 mutex_exit(SD_MUTEX(un));
9795         }
9796 
9797         SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9798             un_f_suppress_cache_flush is set to %d\n",
9799             un->un_f_suppress_cache_flush);
9800 }
9801 
9802 /*
9803  *    Function: sd_make_device
9804  *
9805  * Description: Utility routine to return the Solaris device number from
9806  *              the data in the device's dev_info structure.
9807  *
9808  * Return Code: The Solaris device number
9809  *
9810  *     Context: Any
9811  */
9812 
9813 static dev_t
9814 sd_make_device(dev_info_t *devi)
9815 {
9816         return (makedevice(ddi_driver_major(devi),
9817             ddi_get_instance(devi) << SDUNIT_SHIFT));
9818 }
9819 
9820 
9821 /*
9822  *    Function: sd_pm_entry
9823  *
9824  * Description: Called at the start of a new command to manage power
9825  *              and busy status of a device. This includes determining whether
9826  *              the current power state of the device is sufficient for
9827  *              performing the command or whether it must be changed.
9828  *              The PM framework is notified appropriately.
9829  *              Only with a return status of DDI_SUCCESS will the
9830  *              component be busy to the framework.
9831  *
9832  *              All callers of sd_pm_entry must check the return status
9833  *              and only call sd_pm_exit it it was DDI_SUCCESS. A status
9834  *              of DDI_FAILURE indicates the device failed to power up.
9835  *              In this case un_pm_count has been adjusted so the result
9836  *              on exit is still powered down, ie. count is less than 0.
9837  *              Calling sd_pm_exit with this count value hits an ASSERT.
9838  *
9839  * Return Code: DDI_SUCCESS or DDI_FAILURE
9840  *
9841  *     Context: Kernel thread context.
9842  */
9843 
9844 static int
9845 sd_pm_entry(struct sd_lun *un)
9846 {
9847         int return_status = DDI_SUCCESS;
9848 
9849         ASSERT(!mutex_owned(SD_MUTEX(un)));
9850         ASSERT(!mutex_owned(&un->un_pm_mutex));
9851 
9852         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9853 
9854         if (un->un_f_pm_is_enabled == FALSE) {
9855                 SD_TRACE(SD_LOG_IO_PM, un,
9856                     "sd_pm_entry: exiting, PM not enabled\n");
9857                 return (return_status);
9858         }
9859 
9860         /*
9861          * Just increment a counter if PM is enabled. On the transition from
9862          * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9863          * the count with each IO and mark the device as idle when the count
9864          * hits 0.
9865          *
9866          * If the count is less than 0 the device is powered down. If a powered
9867          * down device is successfully powered up then the count must be
9868          * incremented to reflect the power up. Note that it'll get incremented
9869          * a second time to become busy.
9870          *
9871          * Because the following has the potential to change the device state
9872          * and must release the un_pm_mutex to do so, only one thread can be
9873          * allowed through at a time.
9874          */
9875 
9876         mutex_enter(&un->un_pm_mutex);
9877         while (un->un_pm_busy == TRUE) {
9878                 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9879         }
9880         un->un_pm_busy = TRUE;
9881 
9882         if (un->un_pm_count < 1) {
9883 
9884                 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9885 
9886                 /*
9887                  * Indicate we are now busy so the framework won't attempt to
9888                  * power down the device. This call will only fail if either
9889                  * we passed a bad component number or the device has no
9890                  * components. Neither of these should ever happen.
9891                  */
9892                 mutex_exit(&un->un_pm_mutex);
9893                 return_status = pm_busy_component(SD_DEVINFO(un), 0);
9894                 ASSERT(return_status == DDI_SUCCESS);
9895 
9896                 mutex_enter(&un->un_pm_mutex);
9897 
9898                 if (un->un_pm_count < 0) {
9899                         mutex_exit(&un->un_pm_mutex);
9900 
9901                         SD_TRACE(SD_LOG_IO_PM, un,
9902                             "sd_pm_entry: power up component\n");
9903 
9904                         /*
9905                          * pm_raise_power will cause sdpower to be called
9906                          * which brings the device power level to the
9907                          * desired state, If successful, un_pm_count and
9908                          * un_power_level will be updated appropriately.
9909                          */
9910                         return_status = pm_raise_power(SD_DEVINFO(un), 0,
9911                             SD_PM_STATE_ACTIVE(un));
9912 
9913                         mutex_enter(&un->un_pm_mutex);
9914 
9915                         if (return_status != DDI_SUCCESS) {
9916                                 /*
9917                                  * Power up failed.
9918                                  * Idle the device and adjust the count
9919                                  * so the result on exit is that we're
9920                                  * still powered down, ie. count is less than 0.
9921                                  */
9922                                 SD_TRACE(SD_LOG_IO_PM, un,
9923                                     "sd_pm_entry: power up failed,"
9924                                     " idle the component\n");
9925 
9926                                 (void) pm_idle_component(SD_DEVINFO(un), 0);
9927                                 un->un_pm_count--;
9928                         } else {
9929                                 /*
9930                                  * Device is powered up, verify the
9931                                  * count is non-negative.
9932                                  * This is debug only.
9933                                  */
9934                                 ASSERT(un->un_pm_count == 0);
9935                         }
9936                 }
9937 
9938                 if (return_status == DDI_SUCCESS) {
9939                         /*
9940                          * For performance, now that the device has been tagged
9941                          * as busy, and it's known to be powered up, update the
9942                          * chain types to use jump tables that do not include
9943                          * pm. This significantly lowers the overhead and
9944                          * therefore improves performance.
9945                          */
9946 
9947                         mutex_exit(&un->un_pm_mutex);
9948                         mutex_enter(SD_MUTEX(un));
9949                         SD_TRACE(SD_LOG_IO_PM, un,
9950                             "sd_pm_entry: changing uscsi_chain_type from %d\n",
9951                             un->un_uscsi_chain_type);
9952 
9953                         if (un->un_f_non_devbsize_supported) {
9954                                 un->un_buf_chain_type =
9955                                     SD_CHAIN_INFO_RMMEDIA_NO_PM;
9956                         } else {
9957                                 un->un_buf_chain_type =
9958                                     SD_CHAIN_INFO_DISK_NO_PM;
9959                         }
9960                         un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9961 
9962                         SD_TRACE(SD_LOG_IO_PM, un,
9963                             "             changed  uscsi_chain_type to   %d\n",
9964                             un->un_uscsi_chain_type);
9965                         mutex_exit(SD_MUTEX(un));
9966                         mutex_enter(&un->un_pm_mutex);
9967 
9968                         if (un->un_pm_idle_timeid == NULL) {
9969                                 /* 300 ms. */
9970                                 un->un_pm_idle_timeid =
9971                                     timeout(sd_pm_idletimeout_handler, un,
9972                                     (drv_usectohz((clock_t)300000)));
9973                                 /*
9974                                  * Include an extra call to busy which keeps the
9975                                  * device busy with-respect-to the PM layer
9976                                  * until the timer fires, at which time it'll
9977                                  * get the extra idle call.
9978                                  */
9979                                 (void) pm_busy_component(SD_DEVINFO(un), 0);
9980                         }
9981                 }
9982         }
9983         un->un_pm_busy = FALSE;
9984         /* Next... */
9985         cv_signal(&un->un_pm_busy_cv);
9986 
9987         un->un_pm_count++;
9988 
9989         SD_TRACE(SD_LOG_IO_PM, un,
9990             "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9991 
9992         mutex_exit(&un->un_pm_mutex);
9993 
9994         return (return_status);
9995 }
9996 
9997 
9998 /*
9999  *    Function: sd_pm_exit
10000  *
10001  * Description: Called at the completion of a command to manage busy
10002  *              status for the device. If the device becomes idle the
10003  *              PM framework is notified.
10004  *
10005  *     Context: Kernel thread context
10006  */
10007 
10008 static void
10009 sd_pm_exit(struct sd_lun *un)
10010 {
10011         ASSERT(!mutex_owned(SD_MUTEX(un)));
10012         ASSERT(!mutex_owned(&un->un_pm_mutex));
10013 
10014         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10015 
10016         /*
10017          * After attach the following flag is only read, so don't
10018          * take the penalty of acquiring a mutex for it.
10019          */
10020         if (un->un_f_pm_is_enabled == TRUE) {
10021 
10022                 mutex_enter(&un->un_pm_mutex);
10023                 un->un_pm_count--;
10024 
10025                 SD_TRACE(SD_LOG_IO_PM, un,
10026                     "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10027 
10028                 ASSERT(un->un_pm_count >= 0);
10029                 if (un->un_pm_count == 0) {
10030                         mutex_exit(&un->un_pm_mutex);
10031 
10032                         SD_TRACE(SD_LOG_IO_PM, un,
10033                             "sd_pm_exit: idle component\n");
10034 
10035                         (void) pm_idle_component(SD_DEVINFO(un), 0);
10036 
10037                 } else {
10038                         mutex_exit(&un->un_pm_mutex);
10039                 }
10040         }
10041 
10042         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10043 }
10044 
10045 
10046 /*
10047  *    Function: sdopen
10048  *
10049  * Description: Driver's open(9e) entry point function.
10050  *
10051  *   Arguments: dev_i   - pointer to device number
10052  *              flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10053  *              otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10054  *              cred_p  - user credential pointer
10055  *
10056  * Return Code: EINVAL
10057  *              ENXIO
10058  *              EIO
10059  *              EROFS
10060  *              EBUSY
10061  *
10062  *     Context: Kernel thread context
10063  */
10064 /* ARGSUSED */
10065 static int
10066 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10067 {
10068         struct sd_lun   *un;
10069         int             nodelay;
10070         int             part;
10071         uint64_t        partmask;
10072         int             instance;
10073         dev_t           dev;
10074         int             rval = EIO;
10075         diskaddr_t      nblks = 0;
10076         diskaddr_t      label_cap;
10077 
10078         /* Validate the open type */
10079         if (otyp >= OTYPCNT) {
10080                 return (EINVAL);
10081         }
10082 
10083         dev = *dev_p;
10084         instance = SDUNIT(dev);
10085         mutex_enter(&sd_detach_mutex);
10086 
10087         /*
10088          * Fail the open if there is no softstate for the instance, or
10089          * if another thread somewhere is trying to detach the instance.
10090          */
10091         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10092             (un->un_detach_count != 0)) {
10093                 mutex_exit(&sd_detach_mutex);
10094                 /*
10095                  * The probe cache only needs to be cleared when open (9e) fails
10096                  * with ENXIO (4238046).
10097                  */
10098                 /*
10099                  * un-conditionally clearing probe cache is ok with
10100                  * separate sd/ssd binaries
10101                  * x86 platform can be an issue with both parallel
10102                  * and fibre in 1 binary
10103                  */
10104                 sd_scsi_clear_probe_cache();
10105                 return (ENXIO);
10106         }
10107 
10108         /*
10109          * The un_layer_count is to prevent another thread in specfs from
10110          * trying to detach the instance, which can happen when we are
10111          * called from a higher-layer driver instead of thru specfs.
10112          * This will not be needed when DDI provides a layered driver
10113          * interface that allows specfs to know that an instance is in
10114          * use by a layered driver & should not be detached.
10115          *
10116          * Note: the semantics for layered driver opens are exactly one
10117          * close for every open.
10118          */
10119         if (otyp == OTYP_LYR) {
10120                 un->un_layer_count++;
10121         }
10122 
10123         /*
10124          * Keep a count of the current # of opens in progress. This is because
10125          * some layered drivers try to call us as a regular open. This can
10126          * cause problems that we cannot prevent, however by keeping this count
10127          * we can at least keep our open and detach routines from racing against
10128          * each other under such conditions.
10129          */
10130         un->un_opens_in_progress++;
10131         mutex_exit(&sd_detach_mutex);
10132 
10133         nodelay  = (flag & (FNDELAY | FNONBLOCK));
10134         part     = SDPART(dev);
10135         partmask = 1 << part;
10136 
10137         /*
10138          * We use a semaphore here in order to serialize
10139          * open and close requests on the device.
10140          */
10141         sema_p(&un->un_semoclose);
10142 
10143         mutex_enter(SD_MUTEX(un));
10144 
10145         /*
10146          * All device accesses go thru sdstrategy() where we check
10147          * on suspend status but there could be a scsi_poll command,
10148          * which bypasses sdstrategy(), so we need to check pm
10149          * status.
10150          */
10151 
10152         if (!nodelay) {
10153                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10154                     (un->un_state == SD_STATE_PM_CHANGING)) {
10155                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10156                 }
10157 
10158                 mutex_exit(SD_MUTEX(un));
10159                 if (sd_pm_entry(un) != DDI_SUCCESS) {
10160                         rval = EIO;
10161                         SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10162                             "sdopen: sd_pm_entry failed\n");
10163                         goto open_failed_with_pm;
10164                 }
10165                 mutex_enter(SD_MUTEX(un));
10166         }
10167 
10168         /* check for previous exclusive open */
10169         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10170         SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10171             "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10172             un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10173 
10174         if (un->un_exclopen & (partmask)) {
10175                 goto excl_open_fail;
10176         }
10177 
10178         if (flag & FEXCL) {
10179                 int i;
10180                 if (un->un_ocmap.lyropen[part]) {
10181                         goto excl_open_fail;
10182                 }
10183                 for (i = 0; i < (OTYPCNT - 1); i++) {
10184                         if (un->un_ocmap.regopen[i] & (partmask)) {
10185                                 goto excl_open_fail;
10186                         }
10187                 }
10188         }
10189 
10190         /*
10191          * Check the write permission if this is a removable media device,
10192          * NDELAY has not been set, and writable permission is requested.
10193          *
10194          * Note: If NDELAY was set and this is write-protected media the WRITE
10195          * attempt will fail with EIO as part of the I/O processing. This is a
10196          * more permissive implementation that allows the open to succeed and
10197          * WRITE attempts to fail when appropriate.
10198          */
10199         if (un->un_f_chk_wp_open) {
10200                 if ((flag & FWRITE) && (!nodelay)) {
10201                         mutex_exit(SD_MUTEX(un));
10202                         /*
10203                          * Defer the check for write permission on writable
10204                          * DVD drive till sdstrategy and will not fail open even
10205                          * if FWRITE is set as the device can be writable
10206                          * depending upon the media and the media can change
10207                          * after the call to open().
10208                          */
10209                         if (un->un_f_dvdram_writable_device == FALSE) {
10210                                 if (ISCD(un) || sr_check_wp(dev)) {
10211                                 rval = EROFS;
10212                                 mutex_enter(SD_MUTEX(un));
10213                                 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10214                                     "write to cd or write protected media\n");
10215                                 goto open_fail;
10216                                 }
10217                         }
10218                         mutex_enter(SD_MUTEX(un));
10219                 }
10220         }
10221 
10222         /*
10223          * If opening in NDELAY/NONBLOCK mode, just return.
10224          * Check if disk is ready and has a valid geometry later.
10225          */
10226         if (!nodelay) {
10227                 sd_ssc_t        *ssc;
10228 
10229                 mutex_exit(SD_MUTEX(un));
10230                 ssc = sd_ssc_init(un);
10231                 rval = sd_ready_and_valid(ssc, part);
10232                 sd_ssc_fini(ssc);
10233                 mutex_enter(SD_MUTEX(un));
10234                 /*
10235                  * Fail if device is not ready or if the number of disk
10236                  * blocks is zero or negative for non CD devices.
10237                  */
10238 
10239                 nblks = 0;
10240 
10241                 if (rval == SD_READY_VALID && (!ISCD(un))) {
10242                         /* if cmlb_partinfo fails, nblks remains 0 */
10243                         mutex_exit(SD_MUTEX(un));
10244                         (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10245                             NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10246                         mutex_enter(SD_MUTEX(un));
10247                 }
10248 
10249                 if ((rval != SD_READY_VALID) ||
10250                     (!ISCD(un) && nblks <= 0)) {
10251                         rval = un->un_f_has_removable_media ? ENXIO : EIO;
10252                         SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10253                             "device not ready or invalid disk block value\n");
10254                         goto open_fail;
10255                 }
10256 #if defined(__i386) || defined(__amd64)
10257         } else {
10258                 uchar_t *cp;
10259                 /*
10260                  * x86 requires special nodelay handling, so that p0 is
10261                  * always defined and accessible.
10262                  * Invalidate geometry only if device is not already open.
10263                  */
10264                 cp = &un->un_ocmap.chkd[0];
10265                 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10266                         if (*cp != (uchar_t)0) {
10267                                 break;
10268                         }
10269                         cp++;
10270                 }
10271                 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10272                         mutex_exit(SD_MUTEX(un));
10273                         cmlb_invalidate(un->un_cmlbhandle,
10274                             (void *)SD_PATH_DIRECT);
10275                         mutex_enter(SD_MUTEX(un));
10276                 }
10277 
10278 #endif
10279         }
10280 
10281         if (otyp == OTYP_LYR) {
10282                 un->un_ocmap.lyropen[part]++;
10283         } else {
10284                 un->un_ocmap.regopen[otyp] |= partmask;
10285         }
10286 
10287         /* Set up open and exclusive open flags */
10288         if (flag & FEXCL) {
10289                 un->un_exclopen |= (partmask);
10290         }
10291 
10292         /*
10293          * If the lun is EFI labeled and lun capacity is greater than the
10294          * capacity contained in the label, log a sys-event to notify the
10295          * interested module.
10296          * To avoid an infinite loop of logging sys-event, we only log the
10297          * event when the lun is not opened in NDELAY mode. The event handler
10298          * should open the lun in NDELAY mode.
10299          */
10300         if (!nodelay) {
10301                 mutex_exit(SD_MUTEX(un));
10302                 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10303                     (void*)SD_PATH_DIRECT) == 0) {
10304                         mutex_enter(SD_MUTEX(un));
10305                         if (un->un_f_blockcount_is_valid &&
10306                             un->un_blockcount > label_cap &&
10307                             un->un_f_expnevent == B_FALSE) {
10308                                 un->un_f_expnevent = B_TRUE;
10309                                 mutex_exit(SD_MUTEX(un));
10310                                 sd_log_lun_expansion_event(un,
10311                                     (nodelay ? KM_NOSLEEP : KM_SLEEP));
10312                                 mutex_enter(SD_MUTEX(un));
10313                         }
10314                 } else {
10315                         mutex_enter(SD_MUTEX(un));
10316                 }
10317         }
10318 
10319         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10320             "open of part %d type %d\n", part, otyp);
10321 
10322         mutex_exit(SD_MUTEX(un));
10323         if (!nodelay) {
10324                 sd_pm_exit(un);
10325         }
10326 
10327         sema_v(&un->un_semoclose);
10328 
10329         mutex_enter(&sd_detach_mutex);
10330         un->un_opens_in_progress--;
10331         mutex_exit(&sd_detach_mutex);
10332 
10333         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10334         return (DDI_SUCCESS);
10335 
10336 excl_open_fail:
10337         SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10338         rval = EBUSY;
10339 
10340 open_fail:
10341         mutex_exit(SD_MUTEX(un));
10342 
10343         /*
10344          * On a failed open we must exit the pm management.
10345          */
10346         if (!nodelay) {
10347                 sd_pm_exit(un);
10348         }
10349 open_failed_with_pm:
10350         sema_v(&un->un_semoclose);
10351 
10352         mutex_enter(&sd_detach_mutex);
10353         un->un_opens_in_progress--;
10354         if (otyp == OTYP_LYR) {
10355                 un->un_layer_count--;
10356         }
10357         mutex_exit(&sd_detach_mutex);
10358 
10359         return (rval);
10360 }
10361 
10362 
10363 /*
10364  *    Function: sdclose
10365  *
10366  * Description: Driver's close(9e) entry point function.
10367  *
10368  *   Arguments: dev    - device number
10369  *              flag   - file status flag, informational only
10370  *              otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10371  *              cred_p - user credential pointer
10372  *
10373  * Return Code: ENXIO
10374  *
10375  *     Context: Kernel thread context
10376  */
10377 /* ARGSUSED */
10378 static int
10379 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10380 {
10381         struct sd_lun   *un;
10382         uchar_t         *cp;
10383         int             part;
10384         int             nodelay;
10385         int             rval = 0;
10386 
10387         /* Validate the open type */
10388         if (otyp >= OTYPCNT) {
10389                 return (ENXIO);
10390         }
10391 
10392         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10393                 return (ENXIO);
10394         }
10395 
10396         part = SDPART(dev);
10397         nodelay = flag & (FNDELAY | FNONBLOCK);
10398 
10399         SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10400             "sdclose: close of part %d type %d\n", part, otyp);
10401 
10402         /*
10403          * We use a semaphore here in order to serialize
10404          * open and close requests on the device.
10405          */
10406         sema_p(&un->un_semoclose);
10407 
10408         mutex_enter(SD_MUTEX(un));
10409 
10410         /* Don't proceed if power is being changed. */
10411         while (un->un_state == SD_STATE_PM_CHANGING) {
10412                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10413         }
10414 
10415         if (un->un_exclopen & (1 << part)) {
10416                 un->un_exclopen &= ~(1 << part);
10417         }
10418 
10419         /* Update the open partition map */
10420         if (otyp == OTYP_LYR) {
10421                 un->un_ocmap.lyropen[part] -= 1;
10422         } else {
10423                 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10424         }
10425 
10426         cp = &un->un_ocmap.chkd[0];
10427         while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10428                 if (*cp != NULL) {
10429                         break;
10430                 }
10431                 cp++;
10432         }
10433 
10434         if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10435                 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10436 
10437                 /*
10438                  * We avoid persistance upon the last close, and set
10439                  * the throttle back to the maximum.
10440                  */
10441                 un->un_throttle = un->un_saved_throttle;
10442 
10443                 if (un->un_state == SD_STATE_OFFLINE) {
10444                         if (un->un_f_is_fibre == FALSE) {
10445                                 scsi_log(SD_DEVINFO(un), sd_label,
10446                                     CE_WARN, "offline\n");
10447                         }
10448                         mutex_exit(SD_MUTEX(un));
10449                         cmlb_invalidate(un->un_cmlbhandle,
10450                             (void *)SD_PATH_DIRECT);
10451                         mutex_enter(SD_MUTEX(un));
10452 
10453                 } else {
10454                         /*
10455                          * Flush any outstanding writes in NVRAM cache.
10456                          * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10457                          * cmd, it may not work for non-Pluto devices.
10458                          * SYNCHRONIZE CACHE is not required for removables,
10459                          * except DVD-RAM drives.
10460                          *
10461                          * Also note: because SYNCHRONIZE CACHE is currently
10462                          * the only command issued here that requires the
10463                          * drive be powered up, only do the power up before
10464                          * sending the Sync Cache command. If additional
10465                          * commands are added which require a powered up
10466                          * drive, the following sequence may have to change.
10467                          *
10468                          * And finally, note that parallel SCSI on SPARC
10469                          * only issues a Sync Cache to DVD-RAM, a newly
10470                          * supported device.
10471                          */
10472 #if defined(__i386) || defined(__amd64)
10473                         if ((un->un_f_sync_cache_supported &&
10474                             un->un_f_sync_cache_required) ||
10475                             un->un_f_dvdram_writable_device == TRUE) {
10476 #else
10477                         if (un->un_f_dvdram_writable_device == TRUE) {
10478 #endif
10479                                 mutex_exit(SD_MUTEX(un));
10480                                 if (sd_pm_entry(un) == DDI_SUCCESS) {
10481                                         rval =
10482                                             sd_send_scsi_SYNCHRONIZE_CACHE(un,
10483                                             NULL);
10484                                         /* ignore error if not supported */
10485                                         if (rval == ENOTSUP) {
10486                                                 rval = 0;
10487                                         } else if (rval != 0) {
10488                                                 rval = EIO;
10489                                         }
10490                                         sd_pm_exit(un);
10491                                 } else {
10492                                         rval = EIO;
10493                                 }
10494                                 mutex_enter(SD_MUTEX(un));
10495                         }
10496 
10497                         /*
10498                          * For devices which supports DOOR_LOCK, send an ALLOW
10499                          * MEDIA REMOVAL command, but don't get upset if it
10500                          * fails. We need to raise the power of the drive before
10501                          * we can call sd_send_scsi_DOORLOCK()
10502                          */
10503                         if (un->un_f_doorlock_supported) {
10504                                 mutex_exit(SD_MUTEX(un));
10505                                 if (sd_pm_entry(un) == DDI_SUCCESS) {
10506                                         sd_ssc_t        *ssc;
10507 
10508                                         ssc = sd_ssc_init(un);
10509                                         rval = sd_send_scsi_DOORLOCK(ssc,
10510                                             SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10511                                         if (rval != 0)
10512                                                 sd_ssc_assessment(ssc,
10513                                                     SD_FMT_IGNORE);
10514                                         sd_ssc_fini(ssc);
10515 
10516                                         sd_pm_exit(un);
10517                                         if (ISCD(un) && (rval != 0) &&
10518                                             (nodelay != 0)) {
10519                                                 rval = ENXIO;
10520                                         }
10521                                 } else {
10522                                         rval = EIO;
10523                                 }
10524                                 mutex_enter(SD_MUTEX(un));
10525                         }
10526 
10527                         /*
10528                          * If a device has removable media, invalidate all
10529                          * parameters related to media, such as geometry,
10530                          * blocksize, and blockcount.
10531                          */
10532                         if (un->un_f_has_removable_media) {
10533                                 sr_ejected(un);
10534                         }
10535 
10536                         /*
10537                          * Destroy the cache (if it exists) which was
10538                          * allocated for the write maps since this is
10539                          * the last close for this media.
10540                          */
10541                         if (un->un_wm_cache) {
10542                                 /*
10543                                  * Check if there are pending commands.
10544                                  * and if there are give a warning and
10545                                  * do not destroy the cache.
10546                                  */
10547                                 if (un->un_ncmds_in_driver > 0) {
10548                                         scsi_log(SD_DEVINFO(un),
10549                                             sd_label, CE_WARN,
10550                                             "Unable to clean up memory "
10551                                             "because of pending I/O\n");
10552                                 } else {
10553                                         kmem_cache_destroy(
10554                                             un->un_wm_cache);
10555                                         un->un_wm_cache = NULL;
10556                                 }
10557                         }
10558                 }
10559         }
10560 
10561         mutex_exit(SD_MUTEX(un));
10562         sema_v(&un->un_semoclose);
10563 
10564         if (otyp == OTYP_LYR) {
10565                 mutex_enter(&sd_detach_mutex);
10566                 /*
10567                  * The detach routine may run when the layer count
10568                  * drops to zero.
10569                  */
10570                 un->un_layer_count--;
10571                 mutex_exit(&sd_detach_mutex);
10572         }
10573 
10574         return (rval);
10575 }
10576 
10577 
10578 /*
10579  *    Function: sd_ready_and_valid
10580  *
10581  * Description: Test if device is ready and has a valid geometry.
10582  *
10583  *   Arguments: ssc - sd_ssc_t will contain un
10584  *              un  - driver soft state (unit) structure
10585  *
10586  * Return Code: SD_READY_VALID          ready and valid label
10587  *              SD_NOT_READY_VALID      not ready, no label
10588  *              SD_RESERVED_BY_OTHERS   reservation conflict
10589  *
10590  *     Context: Never called at interrupt context.
10591  */
10592 
10593 static int
10594 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10595 {
10596         struct sd_errstats      *stp;
10597         uint64_t                capacity;
10598         uint_t                  lbasize;
10599         int                     rval = SD_READY_VALID;
10600         char                    name_str[48];
10601         boolean_t               is_valid;
10602         struct sd_lun           *un;
10603         int                     status;
10604 
10605         ASSERT(ssc != NULL);
10606         un = ssc->ssc_un;
10607         ASSERT(un != NULL);
10608         ASSERT(!mutex_owned(SD_MUTEX(un)));
10609 
10610         mutex_enter(SD_MUTEX(un));
10611         /*
10612          * If a device has removable media, we must check if media is
10613          * ready when checking if this device is ready and valid.
10614          */
10615         if (un->un_f_has_removable_media) {
10616                 mutex_exit(SD_MUTEX(un));
10617                 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10618 
10619                 if (status != 0) {
10620                         rval = SD_NOT_READY_VALID;
10621                         mutex_enter(SD_MUTEX(un));
10622 
10623                         /* Ignore all failed status for removalbe media */
10624                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10625 
10626                         goto done;
10627                 }
10628 
10629                 is_valid = SD_IS_VALID_LABEL(un);
10630                 mutex_enter(SD_MUTEX(un));
10631                 if (!is_valid ||
10632                     (un->un_f_blockcount_is_valid == FALSE) ||
10633                     (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10634 
10635                         /* capacity has to be read every open. */
10636                         mutex_exit(SD_MUTEX(un));
10637                         status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10638                             &lbasize, SD_PATH_DIRECT);
10639 
10640                         if (status != 0) {
10641                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10642 
10643                                 cmlb_invalidate(un->un_cmlbhandle,
10644                                     (void *)SD_PATH_DIRECT);
10645                                 mutex_enter(SD_MUTEX(un));
10646                                 rval = SD_NOT_READY_VALID;
10647 
10648                                 goto done;
10649                         } else {
10650                                 mutex_enter(SD_MUTEX(un));
10651                                 sd_update_block_info(un, lbasize, capacity);
10652                         }
10653                 }
10654 
10655                 /*
10656                  * Check if the media in the device is writable or not.
10657                  */
10658                 if (!is_valid && ISCD(un)) {
10659                         sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10660                 }
10661 
10662         } else {
10663                 /*
10664                  * Do a test unit ready to clear any unit attention from non-cd
10665                  * devices.
10666                  */
10667                 mutex_exit(SD_MUTEX(un));
10668 
10669                 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10670                 if (status != 0) {
10671                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10672                 }
10673 
10674                 mutex_enter(SD_MUTEX(un));
10675         }
10676 
10677 
10678         /*
10679          * If this is a non 512 block device, allocate space for
10680          * the wmap cache. This is being done here since every time
10681          * a media is changed this routine will be called and the
10682          * block size is a function of media rather than device.
10683          */
10684         if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10685             un->un_f_non_devbsize_supported) &&
10686             un->un_tgt_blocksize != DEV_BSIZE) ||
10687             un->un_f_enable_rmw) {
10688                 if (!(un->un_wm_cache)) {
10689                         (void) snprintf(name_str, sizeof (name_str),
10690                             "%s%d_cache",
10691                             ddi_driver_name(SD_DEVINFO(un)),
10692                             ddi_get_instance(SD_DEVINFO(un)));
10693                         un->un_wm_cache = kmem_cache_create(
10694                             name_str, sizeof (struct sd_w_map),
10695                             8, sd_wm_cache_constructor,
10696                             sd_wm_cache_destructor, NULL,
10697                             (void *)un, NULL, 0);
10698                         if (!(un->un_wm_cache)) {
10699                                 rval = ENOMEM;
10700                                 goto done;
10701                         }
10702                 }
10703         }
10704 
10705         if (un->un_state == SD_STATE_NORMAL) {
10706                 /*
10707                  * If the target is not yet ready here (defined by a TUR
10708                  * failure), invalidate the geometry and print an 'offline'
10709                  * message. This is a legacy message, as the state of the
10710                  * target is not actually changed to SD_STATE_OFFLINE.
10711                  *
10712                  * If the TUR fails for EACCES (Reservation Conflict),
10713                  * SD_RESERVED_BY_OTHERS will be returned to indicate
10714                  * reservation conflict. If the TUR fails for other
10715                  * reasons, SD_NOT_READY_VALID will be returned.
10716                  */
10717                 int err;
10718 
10719                 mutex_exit(SD_MUTEX(un));
10720                 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10721                 mutex_enter(SD_MUTEX(un));
10722 
10723                 if (err != 0) {
10724                         mutex_exit(SD_MUTEX(un));
10725                         cmlb_invalidate(un->un_cmlbhandle,
10726                             (void *)SD_PATH_DIRECT);
10727                         mutex_enter(SD_MUTEX(un));
10728                         if (err == EACCES) {
10729                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10730                                     "reservation conflict\n");
10731                                 rval = SD_RESERVED_BY_OTHERS;
10732                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10733                         } else {
10734                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10735                                     "drive offline\n");
10736                                 rval = SD_NOT_READY_VALID;
10737                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10738                         }
10739                         goto done;
10740                 }
10741         }
10742 
10743         if (un->un_f_format_in_progress == FALSE) {
10744                 mutex_exit(SD_MUTEX(un));
10745 
10746                 (void) cmlb_validate(un->un_cmlbhandle, 0,
10747                     (void *)SD_PATH_DIRECT);
10748                 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10749                     NULL, (void *) SD_PATH_DIRECT) != 0) {
10750                         rval = SD_NOT_READY_VALID;
10751                         mutex_enter(SD_MUTEX(un));
10752 
10753                         goto done;
10754                 }
10755                 if (un->un_f_pkstats_enabled) {
10756                         sd_set_pstats(un);
10757                         SD_TRACE(SD_LOG_IO_PARTITION, un,
10758                             "sd_ready_and_valid: un:0x%p pstats created and "
10759                             "set\n", un);
10760                 }
10761                 mutex_enter(SD_MUTEX(un));
10762         }
10763 
10764         /*
10765          * If this device supports DOOR_LOCK command, try and send
10766          * this command to PREVENT MEDIA REMOVAL, but don't get upset
10767          * if it fails. For a CD, however, it is an error
10768          */
10769         if (un->un_f_doorlock_supported) {
10770                 mutex_exit(SD_MUTEX(un));
10771                 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10772                     SD_PATH_DIRECT);
10773 
10774                 if ((status != 0) && ISCD(un)) {
10775                         rval = SD_NOT_READY_VALID;
10776                         mutex_enter(SD_MUTEX(un));
10777 
10778                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10779 
10780                         goto done;
10781                 } else if (status != 0)
10782                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10783                 mutex_enter(SD_MUTEX(un));
10784         }
10785 
10786         /* The state has changed, inform the media watch routines */
10787         un->un_mediastate = DKIO_INSERTED;
10788         cv_broadcast(&un->un_state_cv);
10789         rval = SD_READY_VALID;
10790 
10791 done:
10792 
10793         /*
10794          * Initialize the capacity kstat value, if no media previously
10795          * (capacity kstat is 0) and a media has been inserted
10796          * (un_blockcount > 0).
10797          */
10798         if (un->un_errstats != NULL) {
10799                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
10800                 if ((stp->sd_capacity.value.ui64 == 0) &&
10801                     (un->un_f_blockcount_is_valid == TRUE)) {
10802                         stp->sd_capacity.value.ui64 =
10803                             (uint64_t)((uint64_t)un->un_blockcount *
10804                             un->un_sys_blocksize);
10805                 }
10806         }
10807 
10808         mutex_exit(SD_MUTEX(un));
10809         return (rval);
10810 }
10811 
10812 
10813 /*
10814  *    Function: sdmin
10815  *
10816  * Description: Routine to limit the size of a data transfer. Used in
10817  *              conjunction with physio(9F).
10818  *
10819  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10820  *
10821  *     Context: Kernel thread context.
10822  */
10823 
10824 static void
10825 sdmin(struct buf *bp)
10826 {
10827         struct sd_lun   *un;
10828         int             instance;
10829 
10830         instance = SDUNIT(bp->b_edev);
10831 
10832         un = ddi_get_soft_state(sd_state, instance);
10833         ASSERT(un != NULL);
10834 
10835         /*
10836          * We depend on buf breakup to restrict
10837          * IO size if it is enabled.
10838          */
10839         if (un->un_buf_breakup_supported) {
10840                 return;
10841         }
10842 
10843         if (bp->b_bcount > un->un_max_xfer_size) {
10844                 bp->b_bcount = un->un_max_xfer_size;
10845         }
10846 }
10847 
10848 
10849 /*
10850  *    Function: sdread
10851  *
10852  * Description: Driver's read(9e) entry point function.
10853  *
10854  *   Arguments: dev   - device number
10855  *              uio   - structure pointer describing where data is to be stored
10856  *                      in user's space
10857  *              cred_p  - user credential pointer
10858  *
10859  * Return Code: ENXIO
10860  *              EIO
10861  *              EINVAL
10862  *              value returned by physio
10863  *
10864  *     Context: Kernel thread context.
10865  */
10866 /* ARGSUSED */
10867 static int
10868 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10869 {
10870         struct sd_lun   *un = NULL;
10871         int             secmask;
10872         int             err = 0;
10873         sd_ssc_t        *ssc;
10874 
10875         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10876                 return (ENXIO);
10877         }
10878 
10879         ASSERT(!mutex_owned(SD_MUTEX(un)));
10880 
10881 
10882         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10883                 mutex_enter(SD_MUTEX(un));
10884                 /*
10885                  * Because the call to sd_ready_and_valid will issue I/O we
10886                  * must wait here if either the device is suspended or
10887                  * if it's power level is changing.
10888                  */
10889                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10890                     (un->un_state == SD_STATE_PM_CHANGING)) {
10891                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10892                 }
10893                 un->un_ncmds_in_driver++;
10894                 mutex_exit(SD_MUTEX(un));
10895 
10896                 /* Initialize sd_ssc_t for internal uscsi commands */
10897                 ssc = sd_ssc_init(un);
10898                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10899                         err = EIO;
10900                 } else {
10901                         err = 0;
10902                 }
10903                 sd_ssc_fini(ssc);
10904 
10905                 mutex_enter(SD_MUTEX(un));
10906                 un->un_ncmds_in_driver--;
10907                 ASSERT(un->un_ncmds_in_driver >= 0);
10908                 mutex_exit(SD_MUTEX(un));
10909                 if (err != 0)
10910                         return (err);
10911         }
10912 
10913         /*
10914          * Read requests are restricted to multiples of the system block size.
10915          */
10916         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10917             !un->un_f_enable_rmw)
10918                 secmask = un->un_tgt_blocksize - 1;
10919         else
10920                 secmask = DEV_BSIZE - 1;
10921 
10922         if (uio->uio_loffset & ((offset_t)(secmask))) {
10923                 SD_ERROR(SD_LOG_READ_WRITE, un,
10924                     "sdread: file offset not modulo %d\n",
10925                     secmask + 1);
10926                 err = EINVAL;
10927         } else if (uio->uio_iov->iov_len & (secmask)) {
10928                 SD_ERROR(SD_LOG_READ_WRITE, un,
10929                     "sdread: transfer length not modulo %d\n",
10930                     secmask + 1);
10931                 err = EINVAL;
10932         } else {
10933                 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10934         }
10935 
10936         return (err);
10937 }
10938 
10939 
10940 /*
10941  *    Function: sdwrite
10942  *
10943  * Description: Driver's write(9e) entry point function.
10944  *
10945  *   Arguments: dev   - device number
10946  *              uio   - structure pointer describing where data is stored in
10947  *                      user's space
10948  *              cred_p  - user credential pointer
10949  *
10950  * Return Code: ENXIO
10951  *              EIO
10952  *              EINVAL
10953  *              value returned by physio
10954  *
10955  *     Context: Kernel thread context.
10956  */
10957 /* ARGSUSED */
10958 static int
10959 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10960 {
10961         struct sd_lun   *un = NULL;
10962         int             secmask;
10963         int             err = 0;
10964         sd_ssc_t        *ssc;
10965 
10966         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10967                 return (ENXIO);
10968         }
10969 
10970         ASSERT(!mutex_owned(SD_MUTEX(un)));
10971 
10972         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10973                 mutex_enter(SD_MUTEX(un));
10974                 /*
10975                  * Because the call to sd_ready_and_valid will issue I/O we
10976                  * must wait here if either the device is suspended or
10977                  * if it's power level is changing.
10978                  */
10979                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10980                     (un->un_state == SD_STATE_PM_CHANGING)) {
10981                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10982                 }
10983                 un->un_ncmds_in_driver++;
10984                 mutex_exit(SD_MUTEX(un));
10985 
10986                 /* Initialize sd_ssc_t for internal uscsi commands */
10987                 ssc = sd_ssc_init(un);
10988                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10989                         err = EIO;
10990                 } else {
10991                         err = 0;
10992                 }
10993                 sd_ssc_fini(ssc);
10994 
10995                 mutex_enter(SD_MUTEX(un));
10996                 un->un_ncmds_in_driver--;
10997                 ASSERT(un->un_ncmds_in_driver >= 0);
10998                 mutex_exit(SD_MUTEX(un));
10999                 if (err != 0)
11000                         return (err);
11001         }
11002 
11003         /*
11004          * Write requests are restricted to multiples of the system block size.
11005          */
11006         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11007             !un->un_f_enable_rmw)
11008                 secmask = un->un_tgt_blocksize - 1;
11009         else
11010                 secmask = DEV_BSIZE - 1;
11011 
11012         if (uio->uio_loffset & ((offset_t)(secmask))) {
11013                 SD_ERROR(SD_LOG_READ_WRITE, un,
11014                     "sdwrite: file offset not modulo %d\n",
11015                     secmask + 1);
11016                 err = EINVAL;
11017         } else if (uio->uio_iov->iov_len & (secmask)) {
11018                 SD_ERROR(SD_LOG_READ_WRITE, un,
11019                     "sdwrite: transfer length not modulo %d\n",
11020                     secmask + 1);
11021                 err = EINVAL;
11022         } else {
11023                 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11024         }
11025 
11026         return (err);
11027 }
11028 
11029 
11030 /*
11031  *    Function: sdaread
11032  *
11033  * Description: Driver's aread(9e) entry point function.
11034  *
11035  *   Arguments: dev   - device number
11036  *              aio   - structure pointer describing where data is to be stored
11037  *              cred_p  - user credential pointer
11038  *
11039  * Return Code: ENXIO
11040  *              EIO
11041  *              EINVAL
11042  *              value returned by aphysio
11043  *
11044  *     Context: Kernel thread context.
11045  */
11046 /* ARGSUSED */
11047 static int
11048 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11049 {
11050         struct sd_lun   *un = NULL;
11051         struct uio      *uio = aio->aio_uio;
11052         int             secmask;
11053         int             err = 0;
11054         sd_ssc_t        *ssc;
11055 
11056         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11057                 return (ENXIO);
11058         }
11059 
11060         ASSERT(!mutex_owned(SD_MUTEX(un)));
11061 
11062         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11063                 mutex_enter(SD_MUTEX(un));
11064                 /*
11065                  * Because the call to sd_ready_and_valid will issue I/O we
11066                  * must wait here if either the device is suspended or
11067                  * if it's power level is changing.
11068                  */
11069                 while ((un->un_state == SD_STATE_SUSPENDED) ||
11070                     (un->un_state == SD_STATE_PM_CHANGING)) {
11071                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11072                 }
11073                 un->un_ncmds_in_driver++;
11074                 mutex_exit(SD_MUTEX(un));
11075 
11076                 /* Initialize sd_ssc_t for internal uscsi commands */
11077                 ssc = sd_ssc_init(un);
11078                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11079                         err = EIO;
11080                 } else {
11081                         err = 0;
11082                 }
11083                 sd_ssc_fini(ssc);
11084 
11085                 mutex_enter(SD_MUTEX(un));
11086                 un->un_ncmds_in_driver--;
11087                 ASSERT(un->un_ncmds_in_driver >= 0);
11088                 mutex_exit(SD_MUTEX(un));
11089                 if (err != 0)
11090                         return (err);
11091         }
11092 
11093         /*
11094          * Read requests are restricted to multiples of the system block size.
11095          */
11096         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11097             !un->un_f_enable_rmw)
11098                 secmask = un->un_tgt_blocksize - 1;
11099         else
11100                 secmask = DEV_BSIZE - 1;
11101 
11102         if (uio->uio_loffset & ((offset_t)(secmask))) {
11103                 SD_ERROR(SD_LOG_READ_WRITE, un,
11104                     "sdaread: file offset not modulo %d\n",
11105                     secmask + 1);
11106                 err = EINVAL;
11107         } else if (uio->uio_iov->iov_len & (secmask)) {
11108                 SD_ERROR(SD_LOG_READ_WRITE, un,
11109                     "sdaread: transfer length not modulo %d\n",
11110                     secmask + 1);
11111                 err = EINVAL;
11112         } else {
11113                 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11114         }
11115 
11116         return (err);
11117 }
11118 
11119 
11120 /*
11121  *    Function: sdawrite
11122  *
11123  * Description: Driver's awrite(9e) entry point function.
11124  *
11125  *   Arguments: dev   - device number
11126  *              aio   - structure pointer describing where data is stored
11127  *              cred_p  - user credential pointer
11128  *
11129  * Return Code: ENXIO
11130  *              EIO
11131  *              EINVAL
11132  *              value returned by aphysio
11133  *
11134  *     Context: Kernel thread context.
11135  */
11136 /* ARGSUSED */
11137 static int
11138 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11139 {
11140         struct sd_lun   *un = NULL;
11141         struct uio      *uio = aio->aio_uio;
11142         int             secmask;
11143         int             err = 0;
11144         sd_ssc_t        *ssc;
11145 
11146         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11147                 return (ENXIO);
11148         }
11149 
11150         ASSERT(!mutex_owned(SD_MUTEX(un)));
11151 
11152         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11153                 mutex_enter(SD_MUTEX(un));
11154                 /*
11155                  * Because the call to sd_ready_and_valid will issue I/O we
11156                  * must wait here if either the device is suspended or
11157                  * if it's power level is changing.
11158                  */
11159                 while ((un->un_state == SD_STATE_SUSPENDED) ||
11160                     (un->un_state == SD_STATE_PM_CHANGING)) {
11161                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11162                 }
11163                 un->un_ncmds_in_driver++;
11164                 mutex_exit(SD_MUTEX(un));
11165 
11166                 /* Initialize sd_ssc_t for internal uscsi commands */
11167                 ssc = sd_ssc_init(un);
11168                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11169                         err = EIO;
11170                 } else {
11171                         err = 0;
11172                 }
11173                 sd_ssc_fini(ssc);
11174 
11175                 mutex_enter(SD_MUTEX(un));
11176                 un->un_ncmds_in_driver--;
11177                 ASSERT(un->un_ncmds_in_driver >= 0);
11178                 mutex_exit(SD_MUTEX(un));
11179                 if (err != 0)
11180                         return (err);
11181         }
11182 
11183         /*
11184          * Write requests are restricted to multiples of the system block size.
11185          */
11186         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11187             !un->un_f_enable_rmw)
11188                 secmask = un->un_tgt_blocksize - 1;
11189         else
11190                 secmask = DEV_BSIZE - 1;
11191 
11192         if (uio->uio_loffset & ((offset_t)(secmask))) {
11193                 SD_ERROR(SD_LOG_READ_WRITE, un,
11194                     "sdawrite: file offset not modulo %d\n",
11195                     secmask + 1);
11196                 err = EINVAL;
11197         } else if (uio->uio_iov->iov_len & (secmask)) {
11198                 SD_ERROR(SD_LOG_READ_WRITE, un,
11199                     "sdawrite: transfer length not modulo %d\n",
11200                     secmask + 1);
11201                 err = EINVAL;
11202         } else {
11203                 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11204         }
11205 
11206         return (err);
11207 }
11208 
11209 
11210 
11211 
11212 
11213 /*
11214  * Driver IO processing follows the following sequence:
11215  *
11216  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11217  *         |                |                     ^
11218  *         v                v                     |
11219  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11220  *         |                |                     |                   |
11221  *         v                |                     |                   |
11222  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11223  *         |                |                     ^                   ^
11224  *         v                v                     |                   |
11225  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11226  *         |                |                     |                   |
11227  *     +---+                |                     +------------+      +-------+
11228  *     |                    |                                  |              |
11229  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11230  *     |                    v                                  |              |
11231  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11232  *     |                    |                                  ^              |
11233  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11234  *     |                    v                                  |              |
11235  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11236  *     |                    |                                  ^              |
11237  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11238  *     |                    v                                  |              |
11239  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11240  *     |                    |                                  ^              |
11241  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11242  *     |                    v                                  |              |
11243  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11244  *     |                    |                                  ^              |
11245  *     |                    |                                  |              |
11246  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11247  *                          |                           ^
11248  *                          v                           |
11249  *                   sd_core_iostart()                  |
11250  *                          |                           |
11251  *                          |                           +------>(*destroypkt)()
11252  *                          +-> sd_start_cmds() <-+     |           |
11253  *                          |                     |     |           v
11254  *                          |                     |     |  scsi_destroy_pkt(9F)
11255  *                          |                     |     |
11256  *                          +->(*initpkt)()       +- sdintr()
11257  *                          |  |                        |  |
11258  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11259  *                          |  +-> scsi_setup_cdb(9F)   |
11260  *                          |                           |
11261  *                          +--> scsi_transport(9F)     |
11262  *                                     |                |
11263  *                                     +----> SCSA ---->+
11264  *
11265  *
11266  * This code is based upon the following presumptions:
11267  *
11268  *   - iostart and iodone functions operate on buf(9S) structures. These
11269  *     functions perform the necessary operations on the buf(9S) and pass
11270  *     them along to the next function in the chain by using the macros
11271  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11272  *     (for iodone side functions).
11273  *
11274  *   - The iostart side functions may sleep. The iodone side functions
11275  *     are called under interrupt context and may NOT sleep. Therefore
11276  *     iodone side functions also may not call iostart side functions.
11277  *     (NOTE: iostart side functions should NOT sleep for memory, as
11278  *     this could result in deadlock.)
11279  *
11280  *   - An iostart side function may call its corresponding iodone side
11281  *     function directly (if necessary).
11282  *
11283  *   - In the event of an error, an iostart side function can return a buf(9S)
11284  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11285  *     b_error in the usual way of course).
11286  *
11287  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11288  *     requests to the iostart side functions.  The iostart side functions in
11289  *     this case would be called under the context of a taskq thread, so it's
11290  *     OK for them to block/sleep/spin in this case.
11291  *
11292  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11293  *     pass them along to the next function in the chain.  The corresponding
11294  *     iodone side functions must coalesce the "shadow" bufs and return
11295  *     the "original" buf to the next higher layer.
11296  *
11297  *   - The b_private field of the buf(9S) struct holds a pointer to
11298  *     an sd_xbuf struct, which contains information needed to
11299  *     construct the scsi_pkt for the command.
11300  *
11301  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11302  *     layer must acquire & release the SD_MUTEX(un) as needed.
11303  */
11304 
11305 
11306 /*
11307  * Create taskq for all targets in the system. This is created at
11308  * _init(9E) and destroyed at _fini(9E).
11309  *
11310  * Note: here we set the minalloc to a reasonably high number to ensure that
11311  * we will have an adequate supply of task entries available at interrupt time.
11312  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11313  * sd_create_taskq().  Since we do not want to sleep for allocations at
11314  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11315  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11316  * requests any one instant in time.
11317  */
11318 #define SD_TASKQ_NUMTHREADS     8
11319 #define SD_TASKQ_MINALLOC       256
11320 #define SD_TASKQ_MAXALLOC       256
11321 
11322 static taskq_t  *sd_tq = NULL;
11323 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11324 
11325 static int      sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11326 static int      sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11327 
11328 /*
11329  * The following task queue is being created for the write part of
11330  * read-modify-write of non-512 block size devices.
11331  * Limit the number of threads to 1 for now. This number has been chosen
11332  * considering the fact that it applies only to dvd ram drives/MO drives
11333  * currently. Performance for which is not main criteria at this stage.
11334  * Note: It needs to be explored if we can use a single taskq in future
11335  */
11336 #define SD_WMR_TASKQ_NUMTHREADS 1
11337 static taskq_t  *sd_wmr_tq = NULL;
11338 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11339 
11340 /*
11341  *    Function: sd_taskq_create
11342  *
11343  * Description: Create taskq thread(s) and preallocate task entries
11344  *
11345  * Return Code: Returns a pointer to the allocated taskq_t.
11346  *
11347  *     Context: Can sleep. Requires blockable context.
11348  *
11349  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11350  *                (definitely NOT recommeded for 3rd-party drivers!) :-)
11351  *              - taskq_create() will block for memory, also it will panic
11352  *                if it cannot create the requested number of threads.
11353  *              - Currently taskq_create() creates threads that cannot be
11354  *                swapped.
11355  *              - We use TASKQ_PREPOPULATE to ensure we have an adequate
11356  *                supply of taskq entries at interrupt time (ie, so that we
11357  *                do not have to sleep for memory)
11358  */
11359 
11360 static void
11361 sd_taskq_create(void)
11362 {
11363         char    taskq_name[TASKQ_NAMELEN];
11364 
11365         ASSERT(sd_tq == NULL);
11366         ASSERT(sd_wmr_tq == NULL);
11367 
11368         (void) snprintf(taskq_name, sizeof (taskq_name),
11369             "%s_drv_taskq", sd_label);
11370         sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11371             (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11372             TASKQ_PREPOPULATE));
11373 
11374         (void) snprintf(taskq_name, sizeof (taskq_name),
11375             "%s_rmw_taskq", sd_label);
11376         sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11377             (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11378             TASKQ_PREPOPULATE));
11379 }
11380 
11381 
11382 /*
11383  *    Function: sd_taskq_delete
11384  *
11385  * Description: Complementary cleanup routine for sd_taskq_create().
11386  *
11387  *     Context: Kernel thread context.
11388  */
11389 
11390 static void
11391 sd_taskq_delete(void)
11392 {
11393         ASSERT(sd_tq != NULL);
11394         ASSERT(sd_wmr_tq != NULL);
11395         taskq_destroy(sd_tq);
11396         taskq_destroy(sd_wmr_tq);
11397         sd_tq = NULL;
11398         sd_wmr_tq = NULL;
11399 }
11400 
11401 
11402 /*
11403  *    Function: sdstrategy
11404  *
11405  * Description: Driver's strategy (9E) entry point function.
11406  *
11407  *   Arguments: bp - pointer to buf(9S)
11408  *
11409  * Return Code: Always returns zero
11410  *
11411  *     Context: Kernel thread context.
11412  */
11413 
11414 static int
11415 sdstrategy(struct buf *bp)
11416 {
11417         struct sd_lun *un;
11418 
11419         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11420         if (un == NULL) {
11421                 bioerror(bp, EIO);
11422                 bp->b_resid = bp->b_bcount;
11423                 biodone(bp);
11424                 return (0);
11425         }
11426 
11427         /* As was done in the past, fail new cmds. if state is dumping. */
11428         if (un->un_state == SD_STATE_DUMPING) {
11429                 bioerror(bp, ENXIO);
11430                 bp->b_resid = bp->b_bcount;
11431                 biodone(bp);
11432                 return (0);
11433         }
11434 
11435         ASSERT(!mutex_owned(SD_MUTEX(un)));
11436 
11437         /*
11438          * Commands may sneak in while we released the mutex in
11439          * DDI_SUSPEND, we should block new commands. However, old
11440          * commands that are still in the driver at this point should
11441          * still be allowed to drain.
11442          */
11443         mutex_enter(SD_MUTEX(un));
11444         /*
11445          * Must wait here if either the device is suspended or
11446          * if it's power level is changing.
11447          */
11448         while ((un->un_state == SD_STATE_SUSPENDED) ||
11449             (un->un_state == SD_STATE_PM_CHANGING)) {
11450                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11451         }
11452 
11453         un->un_ncmds_in_driver++;
11454 
11455         /*
11456          * atapi: Since we are running the CD for now in PIO mode we need to
11457          * call bp_mapin here to avoid bp_mapin called interrupt context under
11458          * the HBA's init_pkt routine.
11459          */
11460         if (un->un_f_cfg_is_atapi == TRUE) {
11461                 mutex_exit(SD_MUTEX(un));
11462                 bp_mapin(bp);
11463                 mutex_enter(SD_MUTEX(un));
11464         }
11465         SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11466             un->un_ncmds_in_driver);
11467 
11468         if (bp->b_flags & B_WRITE)
11469                 un->un_f_sync_cache_required = TRUE;
11470 
11471         mutex_exit(SD_MUTEX(un));
11472 
11473         /*
11474          * This will (eventually) allocate the sd_xbuf area and
11475          * call sd_xbuf_strategy().  We just want to return the
11476          * result of ddi_xbuf_qstrategy so that we have an opt-
11477          * imized tail call which saves us a stack frame.
11478          */
11479         return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11480 }
11481 
11482 
11483 /*
11484  *    Function: sd_xbuf_strategy
11485  *
11486  * Description: Function for initiating IO operations via the
11487  *              ddi_xbuf_qstrategy() mechanism.
11488  *
11489  *     Context: Kernel thread context.
11490  */
11491 
11492 static void
11493 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11494 {
11495         struct sd_lun *un = arg;
11496 
11497         ASSERT(bp != NULL);
11498         ASSERT(xp != NULL);
11499         ASSERT(un != NULL);
11500         ASSERT(!mutex_owned(SD_MUTEX(un)));
11501 
11502         /*
11503          * Initialize the fields in the xbuf and save a pointer to the
11504          * xbuf in bp->b_private.
11505          */
11506         sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11507 
11508         /* Send the buf down the iostart chain */
11509         SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11510 }
11511 
11512 
11513 /*
11514  *    Function: sd_xbuf_init
11515  *
11516  * Description: Prepare the given sd_xbuf struct for use.
11517  *
11518  *   Arguments: un - ptr to softstate
11519  *              bp - ptr to associated buf(9S)
11520  *              xp - ptr to associated sd_xbuf
11521  *              chain_type - IO chain type to use:
11522  *                      SD_CHAIN_NULL
11523  *                      SD_CHAIN_BUFIO
11524  *                      SD_CHAIN_USCSI
11525  *                      SD_CHAIN_DIRECT
11526  *                      SD_CHAIN_DIRECT_PRIORITY
11527  *              pktinfop - ptr to private data struct for scsi_pkt(9S)
11528  *                      initialization; may be NULL if none.
11529  *
11530  *     Context: Kernel thread context
11531  */
11532 
11533 static void
11534 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11535     uchar_t chain_type, void *pktinfop)
11536 {
11537         int index;
11538 
11539         ASSERT(un != NULL);
11540         ASSERT(bp != NULL);
11541         ASSERT(xp != NULL);
11542 
11543         SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11544             bp, chain_type);
11545 
11546         xp->xb_un    = un;
11547         xp->xb_pktp  = NULL;
11548         xp->xb_pktinfo       = pktinfop;
11549         xp->xb_private       = bp->b_private;
11550         xp->xb_blkno = (daddr_t)bp->b_blkno;
11551 
11552         /*
11553          * Set up the iostart and iodone chain indexes in the xbuf, based
11554          * upon the specified chain type to use.
11555          */
11556         switch (chain_type) {
11557         case SD_CHAIN_NULL:
11558                 /*
11559                  * Fall thru to just use the values for the buf type, even
11560                  * tho for the NULL chain these values will never be used.
11561                  */
11562                 /* FALLTHRU */
11563         case SD_CHAIN_BUFIO:
11564                 index = un->un_buf_chain_type;
11565                 if ((!un->un_f_has_removable_media) &&
11566                     (un->un_tgt_blocksize != 0) &&
11567                     (un->un_tgt_blocksize != DEV_BSIZE ||
11568                     un->un_f_enable_rmw)) {
11569                         int secmask = 0, blknomask = 0;
11570                         if (un->un_f_enable_rmw) {
11571                                 blknomask =
11572                                     (un->un_phy_blocksize / DEV_BSIZE) - 1;
11573                                 secmask = un->un_phy_blocksize - 1;
11574                         } else {
11575                                 blknomask =
11576                                     (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11577                                 secmask = un->un_tgt_blocksize - 1;
11578                         }
11579 
11580                         if ((bp->b_lblkno & (blknomask)) ||
11581                             (bp->b_bcount & (secmask))) {
11582                                 if ((un->un_f_rmw_type !=
11583                                     SD_RMW_TYPE_RETURN_ERROR) ||
11584                                     un->un_f_enable_rmw) {
11585                                         if (un->un_f_pm_is_enabled == FALSE)
11586                                                 index =
11587                                                     SD_CHAIN_INFO_MSS_DSK_NO_PM;
11588                                         else
11589                                                 index =
11590                                                     SD_CHAIN_INFO_MSS_DISK;
11591                                 }
11592                         }
11593                 }
11594                 break;
11595         case SD_CHAIN_USCSI:
11596                 index = un->un_uscsi_chain_type;
11597                 break;
11598         case SD_CHAIN_DIRECT:
11599                 index = un->un_direct_chain_type;
11600                 break;
11601         case SD_CHAIN_DIRECT_PRIORITY:
11602                 index = un->un_priority_chain_type;
11603                 break;
11604         default:
11605                 /* We're really broken if we ever get here... */
11606                 panic("sd_xbuf_init: illegal chain type!");
11607                 /*NOTREACHED*/
11608         }
11609 
11610         xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11611         xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11612 
11613         /*
11614          * It might be a bit easier to simply bzero the entire xbuf above,
11615          * but it turns out that since we init a fair number of members anyway,
11616          * we save a fair number cycles by doing explicit assignment of zero.
11617          */
11618         xp->xb_pkt_flags     = 0;
11619         xp->xb_dma_resid     = 0;
11620         xp->xb_retry_count   = 0;
11621         xp->xb_victim_retry_count = 0;
11622         xp->xb_ua_retry_count        = 0;
11623         xp->xb_nr_retry_count        = 0;
11624         xp->xb_sense_bp              = NULL;
11625         xp->xb_sense_status  = 0;
11626         xp->xb_sense_state   = 0;
11627         xp->xb_sense_resid   = 0;
11628         xp->xb_ena           = 0;
11629 
11630         bp->b_private        = xp;
11631         bp->b_flags  &= ~(B_DONE | B_ERROR);
11632         bp->b_resid  = 0;
11633         bp->av_forw  = NULL;
11634         bp->av_back  = NULL;
11635         bioerror(bp, 0);
11636 
11637         SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11638 }
11639 
11640 
11641 /*
11642  *    Function: sd_uscsi_strategy
11643  *
11644  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11645  *
11646  *   Arguments: bp - buf struct ptr
11647  *
11648  * Return Code: Always returns 0
11649  *
11650  *     Context: Kernel thread context
11651  */
11652 
11653 static int
11654 sd_uscsi_strategy(struct buf *bp)
11655 {
11656         struct sd_lun           *un;
11657         struct sd_uscsi_info    *uip;
11658         struct sd_xbuf          *xp;
11659         uchar_t                 chain_type;
11660         uchar_t                 cmd;
11661 
11662         ASSERT(bp != NULL);
11663 
11664         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11665         if (un == NULL) {
11666                 bioerror(bp, EIO);
11667                 bp->b_resid = bp->b_bcount;
11668                 biodone(bp);
11669                 return (0);
11670         }
11671 
11672         ASSERT(!mutex_owned(SD_MUTEX(un)));
11673 
11674         SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11675 
11676         /*
11677          * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11678          */
11679         ASSERT(bp->b_private != NULL);
11680         uip = (struct sd_uscsi_info *)bp->b_private;
11681         cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11682 
11683         mutex_enter(SD_MUTEX(un));
11684         /*
11685          * atapi: Since we are running the CD for now in PIO mode we need to
11686          * call bp_mapin here to avoid bp_mapin called interrupt context under
11687          * the HBA's init_pkt routine.
11688          */
11689         if (un->un_f_cfg_is_atapi == TRUE) {
11690                 mutex_exit(SD_MUTEX(un));
11691                 bp_mapin(bp);
11692                 mutex_enter(SD_MUTEX(un));
11693         }
11694         un->un_ncmds_in_driver++;
11695         SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11696             un->un_ncmds_in_driver);
11697 
11698         if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11699             (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11700                 un->un_f_sync_cache_required = TRUE;
11701 
11702         mutex_exit(SD_MUTEX(un));
11703 
11704         switch (uip->ui_flags) {
11705         case SD_PATH_DIRECT:
11706                 chain_type = SD_CHAIN_DIRECT;
11707                 break;
11708         case SD_PATH_DIRECT_PRIORITY:
11709                 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11710                 break;
11711         default:
11712                 chain_type = SD_CHAIN_USCSI;
11713                 break;
11714         }
11715 
11716         /*
11717          * We may allocate extra buf for external USCSI commands. If the
11718          * application asks for bigger than 20-byte sense data via USCSI,
11719          * SCSA layer will allocate 252 bytes sense buf for that command.
11720          */
11721         if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11722             SENSE_LENGTH) {
11723                 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11724                     MAX_SENSE_LENGTH, KM_SLEEP);
11725         } else {
11726                 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11727         }
11728 
11729         sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11730 
11731         /* Use the index obtained within xbuf_init */
11732         SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11733 
11734         SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11735 
11736         return (0);
11737 }
11738 
11739 /*
11740  *    Function: sd_send_scsi_cmd
11741  *
11742  * Description: Runs a USCSI command for user (when called thru sdioctl),
11743  *              or for the driver
11744  *
11745  *   Arguments: dev - the dev_t for the device
11746  *              incmd - ptr to a valid uscsi_cmd struct
11747  *              flag - bit flag, indicating open settings, 32/64 bit type
11748  *              dataspace - UIO_USERSPACE or UIO_SYSSPACE
11749  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11750  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11751  *                      to use the USCSI "direct" chain and bypass the normal
11752  *                      command waitq.
11753  *
11754  * Return Code: 0 -  successful completion of the given command
11755  *              EIO - scsi_uscsi_handle_command() failed
11756  *              ENXIO  - soft state not found for specified dev
11757  *              EINVAL
11758  *              EFAULT - copyin/copyout error
11759  *              return code of scsi_uscsi_handle_command():
11760  *                      EIO
11761  *                      ENXIO
11762  *                      EACCES
11763  *
11764  *     Context: Waits for command to complete. Can sleep.
11765  */
11766 
11767 static int
11768 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11769     enum uio_seg dataspace, int path_flag)
11770 {
11771         struct sd_lun   *un;
11772         sd_ssc_t        *ssc;
11773         int             rval;
11774 
11775         un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11776         if (un == NULL) {
11777                 return (ENXIO);
11778         }
11779 
11780         /*
11781          * Using sd_ssc_send to handle uscsi cmd
11782          */
11783         ssc = sd_ssc_init(un);
11784         rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11785         sd_ssc_fini(ssc);
11786 
11787         return (rval);
11788 }
11789 
11790 /*
11791  *    Function: sd_ssc_init
11792  *
11793  * Description: Uscsi end-user call this function to initialize necessary
11794  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11795  *
11796  *              The return value of sd_send_scsi_cmd will be treated as a
11797  *              fault in various conditions. Even it is not Zero, some
11798  *              callers may ignore the return value. That is to say, we can
11799  *              not make an accurate assessment in sdintr, since if a
11800  *              command is failed in sdintr it does not mean the caller of
11801  *              sd_send_scsi_cmd will treat it as a real failure.
11802  *
11803  *              To avoid printing too many error logs for a failed uscsi
11804  *              packet that the caller may not treat it as a failure, the
11805  *              sd will keep silent for handling all uscsi commands.
11806  *
11807  *              During detach->attach and attach-open, for some types of
11808  *              problems, the driver should be providing information about
11809  *              the problem encountered. Device use USCSI_SILENT, which
11810  *              suppresses all driver information. The result is that no
11811  *              information about the problem is available. Being
11812  *              completely silent during this time is inappropriate. The
11813  *              driver needs a more selective filter than USCSI_SILENT, so
11814  *              that information related to faults is provided.
11815  *
11816  *              To make the accurate accessment, the caller  of
11817  *              sd_send_scsi_USCSI_CMD should take the ownership and
11818  *              get necessary information to print error messages.
11819  *
11820  *              If we want to print necessary info of uscsi command, we need to
11821  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11822  *              assessment. We use sd_ssc_init to alloc necessary
11823  *              structs for sending an uscsi command and we are also
11824  *              responsible for free the memory by calling
11825  *              sd_ssc_fini.
11826  *
11827  *              The calling secquences will look like:
11828  *              sd_ssc_init->
11829  *
11830  *                  ...
11831  *
11832  *                  sd_send_scsi_USCSI_CMD->
11833  *                      sd_ssc_send-> - - - sdintr
11834  *                  ...
11835  *
11836  *                  if we think the return value should be treated as a
11837  *                  failure, we make the accessment here and print out
11838  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11839  *
11840  *                  ...
11841  *
11842  *              sd_ssc_fini
11843  *
11844  *
11845  *   Arguments: un - pointer to driver soft state (unit) structure for this
11846  *                   target.
11847  *
11848  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11849  *                         uscsi_cmd and sd_uscsi_info.
11850  *                  NULL - if can not alloc memory for sd_ssc_t struct
11851  *
11852  *     Context: Kernel Thread.
11853  */
11854 static sd_ssc_t *
11855 sd_ssc_init(struct sd_lun *un)
11856 {
11857         sd_ssc_t                *ssc;
11858         struct uscsi_cmd        *ucmdp;
11859         struct sd_uscsi_info    *uip;
11860 
11861         ASSERT(un != NULL);
11862         ASSERT(!mutex_owned(SD_MUTEX(un)));
11863 
11864         /*
11865          * Allocate sd_ssc_t structure
11866          */
11867         ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11868 
11869         /*
11870          * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11871          */
11872         ucmdp = scsi_uscsi_alloc();
11873 
11874         /*
11875          * Allocate sd_uscsi_info structure
11876          */
11877         uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11878 
11879         ssc->ssc_uscsi_cmd = ucmdp;
11880         ssc->ssc_uscsi_info = uip;
11881         ssc->ssc_un = un;
11882 
11883         return (ssc);
11884 }
11885 
11886 /*
11887  * Function: sd_ssc_fini
11888  *
11889  * Description: To free sd_ssc_t and it's hanging off
11890  *
11891  * Arguments: ssc - struct pointer of sd_ssc_t.
11892  */
11893 static void
11894 sd_ssc_fini(sd_ssc_t *ssc)
11895 {
11896         scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11897 
11898         if (ssc->ssc_uscsi_info != NULL) {
11899                 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11900                 ssc->ssc_uscsi_info = NULL;
11901         }
11902 
11903         kmem_free(ssc, sizeof (sd_ssc_t));
11904         ssc = NULL;
11905 }
11906 
11907 /*
11908  * Function: sd_ssc_send
11909  *
11910  * Description: Runs a USCSI command for user when called through sdioctl,
11911  *              or for the driver.
11912  *
11913  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11914  *                    sd_uscsi_info in.
11915  *              incmd - ptr to a valid uscsi_cmd struct
11916  *              flag - bit flag, indicating open settings, 32/64 bit type
11917  *              dataspace - UIO_USERSPACE or UIO_SYSSPACE
11918  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11919  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11920  *                      to use the USCSI "direct" chain and bypass the normal
11921  *                      command waitq.
11922  *
11923  * Return Code: 0 -  successful completion of the given command
11924  *              EIO - scsi_uscsi_handle_command() failed
11925  *              ENXIO  - soft state not found for specified dev
11926  *              ECANCELED - command cancelled due to low power
11927  *              EINVAL
11928  *              EFAULT - copyin/copyout error
11929  *              return code of scsi_uscsi_handle_command():
11930  *                      EIO
11931  *                      ENXIO
11932  *                      EACCES
11933  *
11934  *     Context: Kernel Thread;
11935  *              Waits for command to complete. Can sleep.
11936  */
11937 static int
11938 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11939     enum uio_seg dataspace, int path_flag)
11940 {
11941         struct sd_uscsi_info    *uip;
11942         struct uscsi_cmd        *uscmd;
11943         struct sd_lun           *un;
11944         dev_t                   dev;
11945 
11946         int     format = 0;
11947         int     rval;
11948 
11949         ASSERT(ssc != NULL);
11950         un = ssc->ssc_un;
11951         ASSERT(un != NULL);
11952         uscmd = ssc->ssc_uscsi_cmd;
11953         ASSERT(uscmd != NULL);
11954         ASSERT(!mutex_owned(SD_MUTEX(un)));
11955         if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11956                 /*
11957                  * If enter here, it indicates that the previous uscsi
11958                  * command has not been processed by sd_ssc_assessment.
11959                  * This is violating our rules of FMA telemetry processing.
11960                  * We should print out this message and the last undisposed
11961                  * uscsi command.
11962                  */
11963                 if (uscmd->uscsi_cdb != NULL) {
11964                         SD_INFO(SD_LOG_SDTEST, un,
11965                             "sd_ssc_send is missing the alternative "
11966                             "sd_ssc_assessment when running command 0x%x.\n",
11967                             uscmd->uscsi_cdb[0]);
11968                 }
11969                 /*
11970                  * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11971                  * the initial status.
11972                  */
11973                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11974         }
11975 
11976         /*
11977          * We need to make sure sd_ssc_send will have sd_ssc_assessment
11978          * followed to avoid missing FMA telemetries.
11979          */
11980         ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11981 
11982         /*
11983          * if USCSI_PMFAILFAST is set and un is in low power, fail the
11984          * command immediately.
11985          */
11986         mutex_enter(SD_MUTEX(un));
11987         mutex_enter(&un->un_pm_mutex);
11988         if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11989             SD_DEVICE_IS_IN_LOW_POWER(un)) {
11990                 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11991                     "un:0x%p is in low power\n", un);
11992                 mutex_exit(&un->un_pm_mutex);
11993                 mutex_exit(SD_MUTEX(un));
11994                 return (ECANCELED);
11995         }
11996         mutex_exit(&un->un_pm_mutex);
11997         mutex_exit(SD_MUTEX(un));
11998 
11999 #ifdef SDDEBUG
12000         switch (dataspace) {
12001         case UIO_USERSPACE:
12002                 SD_TRACE(SD_LOG_IO, un,
12003                     "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12004                 break;
12005         case UIO_SYSSPACE:
12006                 SD_TRACE(SD_LOG_IO, un,
12007                     "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12008                 break;
12009         default:
12010                 SD_TRACE(SD_LOG_IO, un,
12011                     "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12012                 break;
12013         }
12014 #endif
12015 
12016         rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12017             SD_ADDRESS(un), &uscmd);
12018         if (rval != 0) {
12019                 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12020                     "scsi_uscsi_alloc_and_copyin failed\n", un);
12021                 return (rval);
12022         }
12023 
12024         if ((uscmd->uscsi_cdb != NULL) &&
12025             (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12026                 mutex_enter(SD_MUTEX(un));
12027                 un->un_f_format_in_progress = TRUE;
12028                 mutex_exit(SD_MUTEX(un));
12029                 format = 1;
12030         }
12031 
12032         /*
12033          * Allocate an sd_uscsi_info struct and fill it with the info
12034          * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12035          * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12036          * since we allocate the buf here in this function, we do not
12037          * need to preserve the prior contents of b_private.
12038          * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12039          */
12040         uip = ssc->ssc_uscsi_info;
12041         uip->ui_flags = path_flag;
12042         uip->ui_cmdp = uscmd;
12043 
12044         /*
12045          * Commands sent with priority are intended for error recovery
12046          * situations, and do not have retries performed.
12047          */
12048         if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12049                 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12050         }
12051         uscmd->uscsi_flags &= ~USCSI_NOINTR;
12052 
12053         dev = SD_GET_DEV(un);
12054         rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12055             sd_uscsi_strategy, NULL, uip);
12056 
12057         /*
12058          * mark ssc_flags right after handle_cmd to make sure
12059          * the uscsi has been sent
12060          */
12061         ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12062 
12063 #ifdef SDDEBUG
12064         SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12065             "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12066             uscmd->uscsi_status, uscmd->uscsi_resid);
12067         if (uscmd->uscsi_bufaddr != NULL) {
12068                 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12069                     "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12070                     uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12071                 if (dataspace == UIO_SYSSPACE) {
12072                         SD_DUMP_MEMORY(un, SD_LOG_IO,
12073                             "data", (uchar_t *)uscmd->uscsi_bufaddr,
12074                             uscmd->uscsi_buflen, SD_LOG_HEX);
12075                 }
12076         }
12077 #endif
12078 
12079         if (format == 1) {
12080                 mutex_enter(SD_MUTEX(un));
12081                 un->un_f_format_in_progress = FALSE;
12082                 mutex_exit(SD_MUTEX(un));
12083         }
12084 
12085         (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12086 
12087         return (rval);
12088 }
12089 
12090 /*
12091  *     Function: sd_ssc_print
12092  *
12093  * Description: Print information available to the console.
12094  *
12095  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12096  *                    sd_uscsi_info in.
12097  *            sd_severity - log level.
12098  *     Context: Kernel thread or interrupt context.
12099  */
12100 static void
12101 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12102 {
12103         struct uscsi_cmd        *ucmdp;
12104         struct scsi_device      *devp;
12105         dev_info_t              *devinfo;
12106         uchar_t                 *sensep;
12107         int                     senlen;
12108         union scsi_cdb          *cdbp;
12109         uchar_t                 com;
12110         extern struct scsi_key_strings scsi_cmds[];
12111 
12112         ASSERT(ssc != NULL);
12113         ASSERT(ssc->ssc_un != NULL);
12114 
12115         if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12116                 return;
12117         ucmdp = ssc->ssc_uscsi_cmd;
12118         devp = SD_SCSI_DEVP(ssc->ssc_un);
12119         devinfo = SD_DEVINFO(ssc->ssc_un);
12120         ASSERT(ucmdp != NULL);
12121         ASSERT(devp != NULL);
12122         ASSERT(devinfo != NULL);
12123         sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12124         senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12125         cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12126 
12127         /* In certain case (like DOORLOCK), the cdb could be NULL. */
12128         if (cdbp == NULL)
12129                 return;
12130         /* We don't print log if no sense data available. */
12131         if (senlen == 0)
12132                 sensep = NULL;
12133         com = cdbp->scc_cmd;
12134         scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12135             scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12136 }
12137 
12138 /*
12139  *     Function: sd_ssc_assessment
12140  *
12141  * Description: We use this function to make an assessment at the point
12142  *              where SD driver may encounter a potential error.
12143  *
12144  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12145  *                  sd_uscsi_info in.
12146  *            tp_assess - a hint of strategy for ereport posting.
12147  *            Possible values of tp_assess include:
12148  *                SD_FMT_IGNORE - we don't post any ereport because we're
12149  *                sure that it is ok to ignore the underlying problems.
12150  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12151  *                but it might be not correct to ignore the underlying hardware
12152  *                error.
12153  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12154  *                payload driver-assessment of value "fail" or
12155  *                "fatal"(depending on what information we have here). This
12156  *                assessment value is usually set when SD driver think there
12157  *                is a potential error occurred(Typically, when return value
12158  *                of the SCSI command is EIO).
12159  *                SD_FMT_STANDARD - we will post an ereport with the payload
12160  *                driver-assessment of value "info". This assessment value is
12161  *                set when the SCSI command returned successfully and with
12162  *                sense data sent back.
12163  *
12164  *     Context: Kernel thread.
12165  */
12166 static void
12167 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12168 {
12169         int senlen = 0;
12170         struct uscsi_cmd *ucmdp = NULL;
12171         struct sd_lun *un;
12172 
12173         ASSERT(ssc != NULL);
12174         un = ssc->ssc_un;
12175         ASSERT(un != NULL);
12176         ucmdp = ssc->ssc_uscsi_cmd;
12177         ASSERT(ucmdp != NULL);
12178 
12179         if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12180                 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12181         } else {
12182                 /*
12183                  * If enter here, it indicates that we have a wrong
12184                  * calling sequence of sd_ssc_send and sd_ssc_assessment,
12185                  * both of which should be called in a pair in case of
12186                  * loss of FMA telemetries.
12187                  */
12188                 if (ucmdp->uscsi_cdb != NULL) {
12189                         SD_INFO(SD_LOG_SDTEST, un,
12190                             "sd_ssc_assessment is missing the "
12191                             "alternative sd_ssc_send when running 0x%x, "
12192                             "or there are superfluous sd_ssc_assessment for "
12193                             "the same sd_ssc_send.\n",
12194                             ucmdp->uscsi_cdb[0]);
12195                 }
12196                 /*
12197                  * Set the ssc_flags to the initial value to avoid passing
12198                  * down dirty flags to the following sd_ssc_send function.
12199                  */
12200                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12201                 return;
12202         }
12203 
12204         /*
12205          * Only handle an issued command which is waiting for assessment.
12206          * A command which is not issued will not have
12207          * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12208          */
12209         if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12210                 sd_ssc_print(ssc, SCSI_ERR_INFO);
12211                 return;
12212         } else {
12213                 /*
12214                  * For an issued command, we should clear this flag in
12215                  * order to make the sd_ssc_t structure be used off
12216                  * multiple uscsi commands.
12217                  */
12218                 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12219         }
12220 
12221         /*
12222          * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12223          * commands here. And we should clear the ssc_flags before return.
12224          */
12225         if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12226                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12227                 return;
12228         }
12229 
12230         switch (tp_assess) {
12231         case SD_FMT_IGNORE:
12232         case SD_FMT_IGNORE_COMPROMISE:
12233                 break;
12234         case SD_FMT_STATUS_CHECK:
12235                 /*
12236                  * For a failed command(including the succeeded command
12237                  * with invalid data sent back).
12238                  */
12239                 sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12240                 break;
12241         case SD_FMT_STANDARD:
12242                 /*
12243                  * Always for the succeeded commands probably with sense
12244                  * data sent back.
12245                  * Limitation:
12246                  *      We can only handle a succeeded command with sense
12247                  *      data sent back when auto-request-sense is enabled.
12248                  */
12249                 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12250                     ssc->ssc_uscsi_cmd->uscsi_rqresid;
12251                 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12252                     (un->un_f_arq_enabled == TRUE) &&
12253                     senlen > 0 &&
12254                     ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12255                         sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12256                 }
12257                 break;
12258         default:
12259                 /*
12260                  * Should not have other type of assessment.
12261                  */
12262                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12263                     "sd_ssc_assessment got wrong "
12264                     "sd_type_assessment %d.\n", tp_assess);
12265                 break;
12266         }
12267         /*
12268          * Clear up the ssc_flags before return.
12269          */
12270         ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12271 }
12272 
12273 /*
12274  *    Function: sd_ssc_post
12275  *
12276  * Description: 1. read the driver property to get fm-scsi-log flag.
12277  *              2. print log if fm_log_capable is non-zero.
12278  *              3. call sd_ssc_ereport_post to post ereport if possible.
12279  *
12280  *    Context: May be called from kernel thread or interrupt context.
12281  */
12282 static void
12283 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12284 {
12285         struct sd_lun   *un;
12286         int             sd_severity;
12287 
12288         ASSERT(ssc != NULL);
12289         un = ssc->ssc_un;
12290         ASSERT(un != NULL);
12291 
12292         /*
12293          * We may enter here from sd_ssc_assessment(for USCSI command) or
12294          * by directly called from sdintr context.
12295          * We don't handle a non-disk drive(CD-ROM, removable media).
12296          * Clear the ssc_flags before return in case we've set
12297          * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12298          * driver.
12299          */
12300         if (ISCD(un) || un->un_f_has_removable_media) {
12301                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12302                 return;
12303         }
12304 
12305         switch (sd_assess) {
12306                 case SD_FM_DRV_FATAL:
12307                         sd_severity = SCSI_ERR_FATAL;
12308                         break;
12309                 case SD_FM_DRV_RECOVERY:
12310                         sd_severity = SCSI_ERR_RECOVERED;
12311                         break;
12312                 case SD_FM_DRV_RETRY:
12313                         sd_severity = SCSI_ERR_RETRYABLE;
12314                         break;
12315                 case SD_FM_DRV_NOTICE:
12316                         sd_severity = SCSI_ERR_INFO;
12317                         break;
12318                 default:
12319                         sd_severity = SCSI_ERR_UNKNOWN;
12320         }
12321         /* print log */
12322         sd_ssc_print(ssc, sd_severity);
12323 
12324         /* always post ereport */
12325         sd_ssc_ereport_post(ssc, sd_assess);
12326 }
12327 
12328 /*
12329  *    Function: sd_ssc_set_info
12330  *
12331  * Description: Mark ssc_flags and set ssc_info which would be the
12332  *              payload of uderr ereport. This function will cause
12333  *              sd_ssc_ereport_post to post uderr ereport only.
12334  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12335  *              the function will also call SD_ERROR or scsi_log for a
12336  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12337  *
12338  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12339  *                  sd_uscsi_info in.
12340  *            ssc_flags - indicate the sub-category of a uderr.
12341  *            comp - this argument is meaningful only when
12342  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12343  *                   values include:
12344  *                   > 0, SD_ERROR is used with comp as the driver logging
12345  *                   component;
12346  *                   = 0, scsi-log is used to log error telemetries;
12347  *                   < 0, no log available for this telemetry.
12348  *
12349  *    Context: Kernel thread or interrupt context
12350  */
12351 static void
12352 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12353 {
12354         va_list ap;
12355 
12356         ASSERT(ssc != NULL);
12357         ASSERT(ssc->ssc_un != NULL);
12358 
12359         ssc->ssc_flags |= ssc_flags;
12360         va_start(ap, fmt);
12361         (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12362         va_end(ap);
12363 
12364         /*
12365          * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12366          * with invalid data sent back. For non-uscsi command, the
12367          * following code will be bypassed.
12368          */
12369         if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12370                 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12371                         /*
12372                          * If the error belong to certain component and we
12373                          * do not want it to show up on the console, we
12374                          * will use SD_ERROR, otherwise scsi_log is
12375                          * preferred.
12376                          */
12377                         if (comp > 0) {
12378                                 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12379                         } else if (comp == 0) {
12380                                 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12381                                     CE_WARN, ssc->ssc_info);
12382                         }
12383                 }
12384         }
12385 }
12386 
12387 /*
12388  *    Function: sd_buf_iodone
12389  *
12390  * Description: Frees the sd_xbuf & returns the buf to its originator.
12391  *
12392  *     Context: May be called from interrupt context.
12393  */
12394 /* ARGSUSED */
12395 static void
12396 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12397 {
12398         struct sd_xbuf *xp;
12399 
12400         ASSERT(un != NULL);
12401         ASSERT(bp != NULL);
12402         ASSERT(!mutex_owned(SD_MUTEX(un)));
12403 
12404         SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12405 
12406         xp = SD_GET_XBUF(bp);
12407         ASSERT(xp != NULL);
12408 
12409         /* xbuf is gone after this */
12410         if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12411                 mutex_enter(SD_MUTEX(un));
12412 
12413                 /*
12414                  * Grab time when the cmd completed.
12415                  * This is used for determining if the system has been
12416                  * idle long enough to make it idle to the PM framework.
12417                  * This is for lowering the overhead, and therefore improving
12418                  * performance per I/O operation.
12419                  */
12420                 un->un_pm_idle_time = gethrtime();
12421 
12422                 un->un_ncmds_in_driver--;
12423                 ASSERT(un->un_ncmds_in_driver >= 0);
12424                 SD_INFO(SD_LOG_IO, un,
12425                     "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12426                     un->un_ncmds_in_driver);
12427 
12428                 mutex_exit(SD_MUTEX(un));
12429         }
12430 
12431         biodone(bp);                            /* bp is gone after this */
12432 
12433         SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12434 }
12435 
12436 
12437 /*
12438  *    Function: sd_uscsi_iodone
12439  *
12440  * Description: Frees the sd_xbuf & returns the buf to its originator.
12441  *
12442  *     Context: May be called from interrupt context.
12443  */
12444 /* ARGSUSED */
12445 static void
12446 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12447 {
12448         struct sd_xbuf *xp;
12449 
12450         ASSERT(un != NULL);
12451         ASSERT(bp != NULL);
12452 
12453         xp = SD_GET_XBUF(bp);
12454         ASSERT(xp != NULL);
12455         ASSERT(!mutex_owned(SD_MUTEX(un)));
12456 
12457         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12458 
12459         bp->b_private = xp->xb_private;
12460 
12461         mutex_enter(SD_MUTEX(un));
12462 
12463         /*
12464          * Grab time when the cmd completed.
12465          * This is used for determining if the system has been
12466          * idle long enough to make it idle to the PM framework.
12467          * This is for lowering the overhead, and therefore improving
12468          * performance per I/O operation.
12469          */
12470         un->un_pm_idle_time = gethrtime();
12471 
12472         un->un_ncmds_in_driver--;
12473         ASSERT(un->un_ncmds_in_driver >= 0);
12474         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12475             un->un_ncmds_in_driver);
12476 
12477         mutex_exit(SD_MUTEX(un));
12478 
12479         if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12480             SENSE_LENGTH) {
12481                 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12482                     MAX_SENSE_LENGTH);
12483         } else {
12484                 kmem_free(xp, sizeof (struct sd_xbuf));
12485         }
12486 
12487         biodone(bp);
12488 
12489         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12490 }
12491 
12492 
12493 /*
12494  *    Function: sd_mapblockaddr_iostart
12495  *
12496  * Description: Verify request lies within the partition limits for
12497  *              the indicated minor device.  Issue "overrun" buf if
12498  *              request would exceed partition range.  Converts
12499  *              partition-relative block address to absolute.
12500  *
12501  *              Upon exit of this function:
12502  *              1.I/O is aligned
12503  *                 xp->xb_blkno represents the absolute sector address
12504  *              2.I/O is misaligned
12505  *                 xp->xb_blkno represents the absolute logical block address
12506  *                 based on DEV_BSIZE. The logical block address will be
12507  *                 converted to physical sector address in sd_mapblocksize_\
12508  *                 iostart.
12509  *              3.I/O is misaligned but is aligned in "overrun" buf
12510  *                 xp->xb_blkno represents the absolute logical block address
12511  *                 based on DEV_BSIZE. The logical block address will be
12512  *                 converted to physical sector address in sd_mapblocksize_\
12513  *                 iostart. But no RMW will be issued in this case.
12514  *
12515  *     Context: Can sleep
12516  *
12517  *      Issues: This follows what the old code did, in terms of accessing
12518  *              some of the partition info in the unit struct without holding
12519  *              the mutext.  This is a general issue, if the partition info
12520  *              can be altered while IO is in progress... as soon as we send
12521  *              a buf, its partitioning can be invalid before it gets to the
12522  *              device.  Probably the right fix is to move partitioning out
12523  *              of the driver entirely.
12524  */
12525 
12526 static void
12527 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12528 {
12529         diskaddr_t      nblocks;        /* #blocks in the given partition */
12530         daddr_t blocknum;       /* Block number specified by the buf */
12531         size_t  requested_nblocks;
12532         size_t  available_nblocks;
12533         int     partition;
12534         diskaddr_t      partition_offset;
12535         struct sd_xbuf *xp;
12536         int secmask = 0, blknomask = 0;
12537         ushort_t is_aligned = TRUE;
12538 
12539         ASSERT(un != NULL);
12540         ASSERT(bp != NULL);
12541         ASSERT(!mutex_owned(SD_MUTEX(un)));
12542 
12543         SD_TRACE(SD_LOG_IO_PARTITION, un,
12544             "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12545 
12546         xp = SD_GET_XBUF(bp);
12547         ASSERT(xp != NULL);
12548 
12549         /*
12550          * If the geometry is not indicated as valid, attempt to access
12551          * the unit & verify the geometry/label. This can be the case for
12552          * removable-media devices, of if the device was opened in
12553          * NDELAY/NONBLOCK mode.
12554          */
12555         partition = SDPART(bp->b_edev);
12556 
12557         if (!SD_IS_VALID_LABEL(un)) {
12558                 sd_ssc_t *ssc;
12559                 /*
12560                  * Initialize sd_ssc_t for internal uscsi commands
12561                  * In case of potential porformance issue, we need
12562                  * to alloc memory only if there is invalid label
12563                  */
12564                 ssc = sd_ssc_init(un);
12565 
12566                 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12567                         /*
12568                          * For removable devices it is possible to start an
12569                          * I/O without a media by opening the device in nodelay
12570                          * mode. Also for writable CDs there can be many
12571                          * scenarios where there is no geometry yet but volume
12572                          * manager is trying to issue a read() just because
12573                          * it can see TOC on the CD. So do not print a message
12574                          * for removables.
12575                          */
12576                         if (!un->un_f_has_removable_media) {
12577                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12578                                     "i/o to invalid geometry\n");
12579                         }
12580                         bioerror(bp, EIO);
12581                         bp->b_resid = bp->b_bcount;
12582                         SD_BEGIN_IODONE(index, un, bp);
12583 
12584                         sd_ssc_fini(ssc);
12585                         return;
12586                 }
12587                 sd_ssc_fini(ssc);
12588         }
12589 
12590         nblocks = 0;
12591         (void) cmlb_partinfo(un->un_cmlbhandle, partition,
12592             &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12593 
12594         if (un->un_f_enable_rmw) {
12595                 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12596                 secmask = un->un_phy_blocksize - 1;
12597         } else {
12598                 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12599                 secmask = un->un_tgt_blocksize - 1;
12600         }
12601 
12602         if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12603                 is_aligned = FALSE;
12604         }
12605 
12606         if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12607                 /*
12608                  * If I/O is aligned, no need to involve RMW(Read Modify Write)
12609                  * Convert the logical block number to target's physical sector
12610                  * number.
12611                  */
12612                 if (is_aligned) {
12613                         xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12614                 } else {
12615                         /*
12616                          * There is no RMW if we're just reading, so don't
12617                          * warn or error out because of it.
12618                          */
12619                         if (bp->b_flags & B_READ) {
12620                                 /*EMPTY*/
12621                         } else if (!un->un_f_enable_rmw &&
12622                             un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12623                                 bp->b_flags |= B_ERROR;
12624                                 goto error_exit;
12625                         } else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12626                                 mutex_enter(SD_MUTEX(un));
12627                                 if (!un->un_f_enable_rmw &&
12628                                     un->un_rmw_msg_timeid == NULL) {
12629                                         scsi_log(SD_DEVINFO(un), sd_label,
12630                                             CE_WARN, "I/O request is not "
12631                                             "aligned with %d disk sector size. "
12632                                             "It is handled through Read Modify "
12633                                             "Write but the performance is "
12634                                             "very low.\n",
12635                                             un->un_tgt_blocksize);
12636                                         un->un_rmw_msg_timeid =
12637                                             timeout(sd_rmw_msg_print_handler,
12638                                             un, SD_RMW_MSG_PRINT_TIMEOUT);
12639                                 } else {
12640                                         un->un_rmw_incre_count ++;
12641                                 }
12642                                 mutex_exit(SD_MUTEX(un));
12643                         }
12644 
12645                         nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12646                         partition_offset = SD_TGT2SYSBLOCK(un,
12647                             partition_offset);
12648                 }
12649         }
12650 
12651         /*
12652          * blocknum is the starting block number of the request. At this
12653          * point it is still relative to the start of the minor device.
12654          */
12655         blocknum = xp->xb_blkno;
12656 
12657         /*
12658          * Legacy: If the starting block number is one past the last block
12659          * in the partition, do not set B_ERROR in the buf.
12660          */
12661         if (blocknum == nblocks)  {
12662                 goto error_exit;
12663         }
12664 
12665         /*
12666          * Confirm that the first block of the request lies within the
12667          * partition limits. Also the requested number of bytes must be
12668          * a multiple of the system block size.
12669          */
12670         if ((blocknum < 0) || (blocknum >= nblocks) ||
12671             ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12672                 bp->b_flags |= B_ERROR;
12673                 goto error_exit;
12674         }
12675 
12676         /*
12677          * If the requsted # blocks exceeds the available # blocks, that
12678          * is an overrun of the partition.
12679          */
12680         if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12681                 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12682         } else {
12683                 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12684         }
12685 
12686         available_nblocks = (size_t)(nblocks - blocknum);
12687         ASSERT(nblocks >= blocknum);
12688 
12689         if (requested_nblocks > available_nblocks) {
12690                 size_t resid;
12691 
12692                 /*
12693                  * Allocate an "overrun" buf to allow the request to proceed
12694                  * for the amount of space available in the partition. The
12695                  * amount not transferred will be added into the b_resid
12696                  * when the operation is complete. The overrun buf
12697                  * replaces the original buf here, and the original buf
12698                  * is saved inside the overrun buf, for later use.
12699                  */
12700                 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12701                         resid = SD_TGTBLOCKS2BYTES(un,
12702                             (offset_t)(requested_nblocks - available_nblocks));
12703                 } else {
12704                         resid = SD_SYSBLOCKS2BYTES(
12705                             (offset_t)(requested_nblocks - available_nblocks));
12706                 }
12707 
12708                 size_t count = bp->b_bcount - resid;
12709                 /*
12710                  * Note: count is an unsigned entity thus it'll NEVER
12711                  * be less than 0 so ASSERT the original values are
12712                  * correct.
12713                  */
12714                 ASSERT(bp->b_bcount >= resid);
12715 
12716                 bp = sd_bioclone_alloc(bp, count, blocknum,
12717                     (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12718                 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12719                 ASSERT(xp != NULL);
12720         }
12721 
12722         /* At this point there should be no residual for this buf. */
12723         ASSERT(bp->b_resid == 0);
12724 
12725         /* Convert the block number to an absolute address. */
12726         xp->xb_blkno += partition_offset;
12727 
12728         SD_NEXT_IOSTART(index, un, bp);
12729 
12730         SD_TRACE(SD_LOG_IO_PARTITION, un,
12731             "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12732 
12733         return;
12734 
12735 error_exit:
12736         bp->b_resid = bp->b_bcount;
12737         SD_BEGIN_IODONE(index, un, bp);
12738         SD_TRACE(SD_LOG_IO_PARTITION, un,
12739             "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12740 }
12741 
12742 
12743 /*
12744  *    Function: sd_mapblockaddr_iodone
12745  *
12746  * Description: Completion-side processing for partition management.
12747  *
12748  *     Context: May be called under interrupt context
12749  */
12750 
12751 static void
12752 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12753 {
12754         /* int  partition; */   /* Not used, see below. */
12755         ASSERT(un != NULL);
12756         ASSERT(bp != NULL);
12757         ASSERT(!mutex_owned(SD_MUTEX(un)));
12758 
12759         SD_TRACE(SD_LOG_IO_PARTITION, un,
12760             "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12761 
12762         if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12763                 /*
12764                  * We have an "overrun" buf to deal with...
12765                  */
12766                 struct sd_xbuf  *xp;
12767                 struct buf      *obp;   /* ptr to the original buf */
12768 
12769                 xp = SD_GET_XBUF(bp);
12770                 ASSERT(xp != NULL);
12771 
12772                 /* Retrieve the pointer to the original buf */
12773                 obp = (struct buf *)xp->xb_private;
12774                 ASSERT(obp != NULL);
12775 
12776                 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12777                 bioerror(obp, bp->b_error);
12778 
12779                 sd_bioclone_free(bp);
12780 
12781                 /*
12782                  * Get back the original buf.
12783                  * Note that since the restoration of xb_blkno below
12784                  * was removed, the sd_xbuf is not needed.
12785                  */
12786                 bp = obp;
12787                 /*
12788                  * xp = SD_GET_XBUF(bp);
12789                  * ASSERT(xp != NULL);
12790                  */
12791         }
12792 
12793         /*
12794          * Convert sd->xb_blkno back to a minor-device relative value.
12795          * Note: this has been commented out, as it is not needed in the
12796          * current implementation of the driver (ie, since this function
12797          * is at the top of the layering chains, so the info will be
12798          * discarded) and it is in the "hot" IO path.
12799          *
12800          * partition = getminor(bp->b_edev) & SDPART_MASK;
12801          * xp->xb_blkno -= un->un_offset[partition];
12802          */
12803 
12804         SD_NEXT_IODONE(index, un, bp);
12805 
12806         SD_TRACE(SD_LOG_IO_PARTITION, un,
12807             "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12808 }
12809 
12810 
12811 /*
12812  *    Function: sd_mapblocksize_iostart
12813  *
12814  * Description: Convert between system block size (un->un_sys_blocksize)
12815  *              and target block size (un->un_tgt_blocksize).
12816  *
12817  *     Context: Can sleep to allocate resources.
12818  *
12819  * Assumptions: A higher layer has already performed any partition validation,
12820  *              and converted the xp->xb_blkno to an absolute value relative
12821  *              to the start of the device.
12822  *
12823  *              It is also assumed that the higher layer has implemented
12824  *              an "overrun" mechanism for the case where the request would
12825  *              read/write beyond the end of a partition.  In this case we
12826  *              assume (and ASSERT) that bp->b_resid == 0.
12827  *
12828  *              Note: The implementation for this routine assumes the target
12829  *              block size remains constant between allocation and transport.
12830  */
12831 
12832 static void
12833 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12834 {
12835         struct sd_mapblocksize_info     *bsp;
12836         struct sd_xbuf                  *xp;
12837         offset_t first_byte;
12838         daddr_t start_block, end_block;
12839         daddr_t request_bytes;
12840         ushort_t is_aligned = FALSE;
12841 
12842         ASSERT(un != NULL);
12843         ASSERT(bp != NULL);
12844         ASSERT(!mutex_owned(SD_MUTEX(un)));
12845         ASSERT(bp->b_resid == 0);
12846 
12847         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12848             "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12849 
12850         /*
12851          * For a non-writable CD, a write request is an error
12852          */
12853         if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12854             (un->un_f_mmc_writable_media == FALSE)) {
12855                 bioerror(bp, EIO);
12856                 bp->b_resid = bp->b_bcount;
12857                 SD_BEGIN_IODONE(index, un, bp);
12858                 return;
12859         }
12860 
12861         /*
12862          * We do not need a shadow buf if the device is using
12863          * un->un_sys_blocksize as its block size or if bcount == 0.
12864          * In this case there is no layer-private data block allocated.
12865          */
12866         if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12867             (bp->b_bcount == 0)) {
12868                 goto done;
12869         }
12870 
12871 #if defined(__i386) || defined(__amd64)
12872         /* We do not support non-block-aligned transfers for ROD devices */
12873         ASSERT(!ISROD(un));
12874 #endif
12875 
12876         xp = SD_GET_XBUF(bp);
12877         ASSERT(xp != NULL);
12878 
12879         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12880             "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12881             un->un_tgt_blocksize, DEV_BSIZE);
12882         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12883             "request start block:0x%x\n", xp->xb_blkno);
12884         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12885             "request len:0x%x\n", bp->b_bcount);
12886 
12887         /*
12888          * Allocate the layer-private data area for the mapblocksize layer.
12889          * Layers are allowed to use the xp_private member of the sd_xbuf
12890          * struct to store the pointer to their layer-private data block, but
12891          * each layer also has the responsibility of restoring the prior
12892          * contents of xb_private before returning the buf/xbuf to the
12893          * higher layer that sent it.
12894          *
12895          * Here we save the prior contents of xp->xb_private into the
12896          * bsp->mbs_oprivate field of our layer-private data area. This value
12897          * is restored by sd_mapblocksize_iodone() just prior to freeing up
12898          * the layer-private area and returning the buf/xbuf to the layer
12899          * that sent it.
12900          *
12901          * Note that here we use kmem_zalloc for the allocation as there are
12902          * parts of the mapblocksize code that expect certain fields to be
12903          * zero unless explicitly set to a required value.
12904          */
12905         bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12906         bsp->mbs_oprivate = xp->xb_private;
12907         xp->xb_private = bsp;
12908 
12909         /*
12910          * This treats the data on the disk (target) as an array of bytes.
12911          * first_byte is the byte offset, from the beginning of the device,
12912          * to the location of the request. This is converted from a
12913          * un->un_sys_blocksize block address to a byte offset, and then back
12914          * to a block address based upon a un->un_tgt_blocksize block size.
12915          *
12916          * xp->xb_blkno should be absolute upon entry into this function,
12917          * but, but it is based upon partitions that use the "system"
12918          * block size. It must be adjusted to reflect the block size of
12919          * the target.
12920          *
12921          * Note that end_block is actually the block that follows the last
12922          * block of the request, but that's what is needed for the computation.
12923          */
12924         first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12925         if (un->un_f_enable_rmw) {
12926                 start_block = xp->xb_blkno =
12927                     (first_byte / un->un_phy_blocksize) *
12928                     (un->un_phy_blocksize / DEV_BSIZE);
12929                 end_block   = ((first_byte + bp->b_bcount +
12930                     un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12931                     (un->un_phy_blocksize / DEV_BSIZE);
12932         } else {
12933                 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12934                 end_block   = (first_byte + bp->b_bcount +
12935                     un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12936         }
12937 
12938         /* request_bytes is rounded up to a multiple of the target block size */
12939         request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12940 
12941         /*
12942          * See if the starting address of the request and the request
12943          * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12944          * then we do not need to allocate a shadow buf to handle the request.
12945          */
12946         if (un->un_f_enable_rmw) {
12947                 if (((first_byte % un->un_phy_blocksize) == 0) &&
12948                     ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12949                         is_aligned = TRUE;
12950                 }
12951         } else {
12952                 if (((first_byte % un->un_tgt_blocksize) == 0) &&
12953                     ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12954                         is_aligned = TRUE;
12955                 }
12956         }
12957 
12958         if ((bp->b_flags & B_READ) == 0) {
12959                 /*
12960                  * Lock the range for a write operation. An aligned request is
12961                  * considered a simple write; otherwise the request must be a
12962                  * read-modify-write.
12963                  */
12964                 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12965                     (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12966         }
12967 
12968         /*
12969          * Alloc a shadow buf if the request is not aligned. Also, this is
12970          * where the READ command is generated for a read-modify-write. (The
12971          * write phase is deferred until after the read completes.)
12972          */
12973         if (is_aligned == FALSE) {
12974 
12975                 struct sd_mapblocksize_info     *shadow_bsp;
12976                 struct sd_xbuf  *shadow_xp;
12977                 struct buf      *shadow_bp;
12978 
12979                 /*
12980                  * Allocate the shadow buf and it associated xbuf. Note that
12981                  * after this call the xb_blkno value in both the original
12982                  * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12983                  * same: absolute relative to the start of the device, and
12984                  * adjusted for the target block size. The b_blkno in the
12985                  * shadow buf will also be set to this value. We should never
12986                  * change b_blkno in the original bp however.
12987                  *
12988                  * Note also that the shadow buf will always need to be a
12989                  * READ command, regardless of whether the incoming command
12990                  * is a READ or a WRITE.
12991                  */
12992                 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12993                     xp->xb_blkno,
12994                     (int (*)(struct buf *)) sd_mapblocksize_iodone);
12995 
12996                 shadow_xp = SD_GET_XBUF(shadow_bp);
12997 
12998                 /*
12999                  * Allocate the layer-private data for the shadow buf.
13000                  * (No need to preserve xb_private in the shadow xbuf.)
13001                  */
13002                 shadow_xp->xb_private = shadow_bsp =
13003                     kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13004 
13005                 /*
13006                  * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13007                  * to figure out where the start of the user data is (based upon
13008                  * the system block size) in the data returned by the READ
13009                  * command (which will be based upon the target blocksize). Note
13010                  * that this is only really used if the request is unaligned.
13011                  */
13012                 if (un->un_f_enable_rmw) {
13013                         bsp->mbs_copy_offset = (ssize_t)(first_byte -
13014                             ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13015                         ASSERT((bsp->mbs_copy_offset >= 0) &&
13016                             (bsp->mbs_copy_offset < un->un_phy_blocksize));
13017                 } else {
13018                         bsp->mbs_copy_offset = (ssize_t)(first_byte -
13019                             ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13020                         ASSERT((bsp->mbs_copy_offset >= 0) &&
13021                             (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13022                 }
13023 
13024                 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13025 
13026                 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13027 
13028                 /* Transfer the wmap (if any) to the shadow buf */
13029                 shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13030                 bsp->mbs_wmp = NULL;
13031 
13032                 /*
13033                  * The shadow buf goes on from here in place of the
13034                  * original buf.
13035                  */
13036                 shadow_bsp->mbs_orig_bp = bp;
13037                 bp = shadow_bp;
13038         }
13039 
13040         SD_INFO(SD_LOG_IO_RMMEDIA, un,
13041             "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13042         SD_INFO(SD_LOG_IO_RMMEDIA, un,
13043             "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13044             request_bytes);
13045         SD_INFO(SD_LOG_IO_RMMEDIA, un,
13046             "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13047 
13048 done:
13049         SD_NEXT_IOSTART(index, un, bp);
13050 
13051         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13052             "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13053 }
13054 
13055 
13056 /*
13057  *    Function: sd_mapblocksize_iodone
13058  *
13059  * Description: Completion side processing for block-size mapping.
13060  *
13061  *     Context: May be called under interrupt context
13062  */
13063 
13064 static void
13065 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13066 {
13067         struct sd_mapblocksize_info     *bsp;
13068         struct sd_xbuf  *xp;
13069         struct sd_xbuf  *orig_xp;       /* sd_xbuf for the original buf */
13070         struct buf      *orig_bp;       /* ptr to the original buf */
13071         offset_t        shadow_end;
13072         offset_t        request_end;
13073         offset_t        shadow_start;
13074         ssize_t         copy_offset;
13075         size_t          copy_length;
13076         size_t          shortfall;
13077         uint_t          is_write;       /* TRUE if this bp is a WRITE */
13078         uint_t          has_wmap;       /* TRUE is this bp has a wmap */
13079 
13080         ASSERT(un != NULL);
13081         ASSERT(bp != NULL);
13082 
13083         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13084             "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13085 
13086         /*
13087          * There is no shadow buf or layer-private data if the target is
13088          * using un->un_sys_blocksize as its block size or if bcount == 0.
13089          */
13090         if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13091             (bp->b_bcount == 0)) {
13092                 goto exit;
13093         }
13094 
13095         xp = SD_GET_XBUF(bp);
13096         ASSERT(xp != NULL);
13097 
13098         /* Retrieve the pointer to the layer-private data area from the xbuf. */
13099         bsp = xp->xb_private;
13100 
13101         is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13102         has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13103 
13104         if (is_write) {
13105                 /*
13106                  * For a WRITE request we must free up the block range that
13107                  * we have locked up.  This holds regardless of whether this is
13108                  * an aligned write request or a read-modify-write request.
13109                  */
13110                 sd_range_unlock(un, bsp->mbs_wmp);
13111                 bsp->mbs_wmp = NULL;
13112         }
13113 
13114         if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13115                 /*
13116                  * An aligned read or write command will have no shadow buf;
13117                  * there is not much else to do with it.
13118                  */
13119                 goto done;
13120         }
13121 
13122         orig_bp = bsp->mbs_orig_bp;
13123         ASSERT(orig_bp != NULL);
13124         orig_xp = SD_GET_XBUF(orig_bp);
13125         ASSERT(orig_xp != NULL);
13126         ASSERT(!mutex_owned(SD_MUTEX(un)));
13127 
13128         if (!is_write && has_wmap) {
13129                 /*
13130                  * A READ with a wmap means this is the READ phase of a
13131                  * read-modify-write. If an error occurred on the READ then
13132                  * we do not proceed with the WRITE phase or copy any data.
13133                  * Just release the write maps and return with an error.
13134                  */
13135                 if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13136                         orig_bp->b_resid = orig_bp->b_bcount;
13137                         bioerror(orig_bp, bp->b_error);
13138                         sd_range_unlock(un, bsp->mbs_wmp);
13139                         goto freebuf_done;
13140                 }
13141         }
13142 
13143         /*
13144          * Here is where we set up to copy the data from the shadow buf
13145          * into the space associated with the original buf.
13146          *
13147          * To deal with the conversion between block sizes, these
13148          * computations treat the data as an array of bytes, with the
13149          * first byte (byte 0) corresponding to the first byte in the
13150          * first block on the disk.
13151          */
13152 
13153         /*
13154          * shadow_start and shadow_len indicate the location and size of
13155          * the data returned with the shadow IO request.
13156          */
13157         if (un->un_f_enable_rmw) {
13158                 shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13159         } else {
13160                 shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13161         }
13162         shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13163 
13164         /*
13165          * copy_offset gives the offset (in bytes) from the start of the first
13166          * block of the READ request to the beginning of the data.  We retrieve
13167          * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13168          * there by sd_mapblockize_iostart(). copy_length gives the amount of
13169          * data to be copied (in bytes).
13170          */
13171         copy_offset  = bsp->mbs_copy_offset;
13172         if (un->un_f_enable_rmw) {
13173                 ASSERT((copy_offset >= 0) &&
13174                     (copy_offset < un->un_phy_blocksize));
13175         } else {
13176                 ASSERT((copy_offset >= 0) &&
13177                     (copy_offset < un->un_tgt_blocksize));
13178         }
13179 
13180         copy_length  = orig_bp->b_bcount;
13181         request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13182 
13183         /*
13184          * Set up the resid and error fields of orig_bp as appropriate.
13185          */
13186         if (shadow_end >= request_end) {
13187                 /* We got all the requested data; set resid to zero */
13188                 orig_bp->b_resid = 0;
13189         } else {
13190                 /*
13191                  * We failed to get enough data to fully satisfy the original
13192                  * request. Just copy back whatever data we got and set
13193                  * up the residual and error code as required.
13194                  *
13195                  * 'shortfall' is the amount by which the data received with the
13196                  * shadow buf has "fallen short" of the requested amount.
13197                  */
13198                 shortfall = (size_t)(request_end - shadow_end);
13199 
13200                 if (shortfall > orig_bp->b_bcount) {
13201                         /*
13202                          * We did not get enough data to even partially
13203                          * fulfill the original request.  The residual is
13204                          * equal to the amount requested.
13205                          */
13206                         orig_bp->b_resid = orig_bp->b_bcount;
13207                 } else {
13208                         /*
13209                          * We did not get all the data that we requested
13210                          * from the device, but we will try to return what
13211                          * portion we did get.
13212                          */
13213                         orig_bp->b_resid = shortfall;
13214                 }
13215                 ASSERT(copy_length >= orig_bp->b_resid);
13216                 copy_length  -= orig_bp->b_resid;
13217         }
13218 
13219         /* Propagate the error code from the shadow buf to the original buf */
13220         bioerror(orig_bp, bp->b_error);
13221 
13222         if (is_write) {
13223                 goto freebuf_done;      /* No data copying for a WRITE */
13224         }
13225 
13226         if (has_wmap) {
13227                 /*
13228                  * This is a READ command from the READ phase of a
13229                  * read-modify-write request. We have to copy the data given
13230                  * by the user OVER the data returned by the READ command,
13231                  * then convert the command from a READ to a WRITE and send
13232                  * it back to the target.
13233                  */
13234                 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13235                     copy_length);
13236 
13237                 bp->b_flags &= ~((int)B_READ);   /* Convert to a WRITE */
13238 
13239                 /*
13240                  * Dispatch the WRITE command to the taskq thread, which
13241                  * will in turn send the command to the target. When the
13242                  * WRITE command completes, we (sd_mapblocksize_iodone())
13243                  * will get called again as part of the iodone chain
13244                  * processing for it. Note that we will still be dealing
13245                  * with the shadow buf at that point.
13246                  */
13247                 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13248                     KM_NOSLEEP) != 0) {
13249                         /*
13250                          * Dispatch was successful so we are done. Return
13251                          * without going any higher up the iodone chain. Do
13252                          * not free up any layer-private data until after the
13253                          * WRITE completes.
13254                          */
13255                         return;
13256                 }
13257 
13258                 /*
13259                  * Dispatch of the WRITE command failed; set up the error
13260                  * condition and send this IO back up the iodone chain.
13261                  */
13262                 bioerror(orig_bp, EIO);
13263                 orig_bp->b_resid = orig_bp->b_bcount;
13264 
13265         } else {
13266                 /*
13267                  * This is a regular READ request (ie, not a RMW). Copy the
13268                  * data from the shadow buf into the original buf. The
13269                  * copy_offset compensates for any "misalignment" between the
13270                  * shadow buf (with its un->un_tgt_blocksize blocks) and the
13271                  * original buf (with its un->un_sys_blocksize blocks).
13272                  */
13273                 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13274                     copy_length);
13275         }
13276 
13277 freebuf_done:
13278 
13279         /*
13280          * At this point we still have both the shadow buf AND the original
13281          * buf to deal with, as well as the layer-private data area in each.
13282          * Local variables are as follows:
13283          *
13284          * bp -- points to shadow buf
13285          * xp -- points to xbuf of shadow buf
13286          * bsp -- points to layer-private data area of shadow buf
13287          * orig_bp -- points to original buf
13288          *
13289          * First free the shadow buf and its associated xbuf, then free the
13290          * layer-private data area from the shadow buf. There is no need to
13291          * restore xb_private in the shadow xbuf.
13292          */
13293         sd_shadow_buf_free(bp);
13294         kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13295 
13296         /*
13297          * Now update the local variables to point to the original buf, xbuf,
13298          * and layer-private area.
13299          */
13300         bp = orig_bp;
13301         xp = SD_GET_XBUF(bp);
13302         ASSERT(xp != NULL);
13303         ASSERT(xp == orig_xp);
13304         bsp = xp->xb_private;
13305         ASSERT(bsp != NULL);
13306 
13307 done:
13308         /*
13309          * Restore xb_private to whatever it was set to by the next higher
13310          * layer in the chain, then free the layer-private data area.
13311          */
13312         xp->xb_private = bsp->mbs_oprivate;
13313         kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13314 
13315 exit:
13316         SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13317             "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13318 
13319         SD_NEXT_IODONE(index, un, bp);
13320 }
13321 
13322 
13323 /*
13324  *    Function: sd_checksum_iostart
13325  *
13326  * Description: A stub function for a layer that's currently not used.
13327  *              For now just a placeholder.
13328  *
13329  *     Context: Kernel thread context
13330  */
13331 
13332 static void
13333 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13334 {
13335         ASSERT(un != NULL);
13336         ASSERT(bp != NULL);
13337         ASSERT(!mutex_owned(SD_MUTEX(un)));
13338         SD_NEXT_IOSTART(index, un, bp);
13339 }
13340 
13341 
13342 /*
13343  *    Function: sd_checksum_iodone
13344  *
13345  * Description: A stub function for a layer that's currently not used.
13346  *              For now just a placeholder.
13347  *
13348  *     Context: May be called under interrupt context
13349  */
13350 
13351 static void
13352 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13353 {
13354         ASSERT(un != NULL);
13355         ASSERT(bp != NULL);
13356         ASSERT(!mutex_owned(SD_MUTEX(un)));
13357         SD_NEXT_IODONE(index, un, bp);
13358 }
13359 
13360 
13361 /*
13362  *    Function: sd_checksum_uscsi_iostart
13363  *
13364  * Description: A stub function for a layer that's currently not used.
13365  *              For now just a placeholder.
13366  *
13367  *     Context: Kernel thread context
13368  */
13369 
13370 static void
13371 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13372 {
13373         ASSERT(un != NULL);
13374         ASSERT(bp != NULL);
13375         ASSERT(!mutex_owned(SD_MUTEX(un)));
13376         SD_NEXT_IOSTART(index, un, bp);
13377 }
13378 
13379 
13380 /*
13381  *    Function: sd_checksum_uscsi_iodone
13382  *
13383  * Description: A stub function for a layer that's currently not used.
13384  *              For now just a placeholder.
13385  *
13386  *     Context: May be called under interrupt context
13387  */
13388 
13389 static void
13390 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13391 {
13392         ASSERT(un != NULL);
13393         ASSERT(bp != NULL);
13394         ASSERT(!mutex_owned(SD_MUTEX(un)));
13395         SD_NEXT_IODONE(index, un, bp);
13396 }
13397 
13398 
13399 /*
13400  *    Function: sd_pm_iostart
13401  *
13402  * Description: iostart-side routine for Power mangement.
13403  *
13404  *     Context: Kernel thread context
13405  */
13406 
13407 static void
13408 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13409 {
13410         ASSERT(un != NULL);
13411         ASSERT(bp != NULL);
13412         ASSERT(!mutex_owned(SD_MUTEX(un)));
13413         ASSERT(!mutex_owned(&un->un_pm_mutex));
13414 
13415         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13416 
13417         if (sd_pm_entry(un) != DDI_SUCCESS) {
13418                 /*
13419                  * Set up to return the failed buf back up the 'iodone'
13420                  * side of the calling chain.
13421                  */
13422                 bioerror(bp, EIO);
13423                 bp->b_resid = bp->b_bcount;
13424 
13425                 SD_BEGIN_IODONE(index, un, bp);
13426 
13427                 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13428                 return;
13429         }
13430 
13431         SD_NEXT_IOSTART(index, un, bp);
13432 
13433         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13434 }
13435 
13436 
13437 /*
13438  *    Function: sd_pm_iodone
13439  *
13440  * Description: iodone-side routine for power mangement.
13441  *
13442  *     Context: may be called from interrupt context
13443  */
13444 
13445 static void
13446 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13447 {
13448         ASSERT(un != NULL);
13449         ASSERT(bp != NULL);
13450         ASSERT(!mutex_owned(&un->un_pm_mutex));
13451 
13452         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13453 
13454         /*
13455          * After attach the following flag is only read, so don't
13456          * take the penalty of acquiring a mutex for it.
13457          */
13458         if (un->un_f_pm_is_enabled == TRUE) {
13459                 sd_pm_exit(un);
13460         }
13461 
13462         SD_NEXT_IODONE(index, un, bp);
13463 
13464         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13465 }
13466 
13467 
13468 /*
13469  *    Function: sd_core_iostart
13470  *
13471  * Description: Primary driver function for enqueuing buf(9S) structs from
13472  *              the system and initiating IO to the target device
13473  *
13474  *     Context: Kernel thread context. Can sleep.
13475  *
13476  * Assumptions:  - The given xp->xb_blkno is absolute
13477  *                 (ie, relative to the start of the device).
13478  *               - The IO is to be done using the native blocksize of
13479  *                 the device, as specified in un->un_tgt_blocksize.
13480  */
13481 /* ARGSUSED */
13482 static void
13483 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13484 {
13485         struct sd_xbuf *xp;
13486 
13487         ASSERT(un != NULL);
13488         ASSERT(bp != NULL);
13489         ASSERT(!mutex_owned(SD_MUTEX(un)));
13490         ASSERT(bp->b_resid == 0);
13491 
13492         SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13493 
13494         xp = SD_GET_XBUF(bp);
13495         ASSERT(xp != NULL);
13496 
13497         mutex_enter(SD_MUTEX(un));
13498 
13499         /*
13500          * If we are currently in the failfast state, fail any new IO
13501          * that has B_FAILFAST set, then return.
13502          */
13503         if ((bp->b_flags & B_FAILFAST) &&
13504             (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13505                 mutex_exit(SD_MUTEX(un));
13506                 bioerror(bp, EIO);
13507                 bp->b_resid = bp->b_bcount;
13508                 SD_BEGIN_IODONE(index, un, bp);
13509                 return;
13510         }
13511 
13512         if (SD_IS_DIRECT_PRIORITY(xp)) {
13513                 /*
13514                  * Priority command -- transport it immediately.
13515                  *
13516                  * Note: We may want to assert that USCSI_DIAGNOSE is set,
13517                  * because all direct priority commands should be associated
13518                  * with error recovery actions which we don't want to retry.
13519                  */
13520                 sd_start_cmds(un, bp);
13521         } else {
13522                 /*
13523                  * Normal command -- add it to the wait queue, then start
13524                  * transporting commands from the wait queue.
13525                  */
13526                 sd_add_buf_to_waitq(un, bp);
13527                 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13528                 sd_start_cmds(un, NULL);
13529         }
13530 
13531         mutex_exit(SD_MUTEX(un));
13532 
13533         SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13534 }
13535 
13536 
13537 /*
13538  *    Function: sd_init_cdb_limits
13539  *
13540  * Description: This is to handle scsi_pkt initialization differences
13541  *              between the driver platforms.
13542  *
13543  *              Legacy behaviors:
13544  *
13545  *              If the block number or the sector count exceeds the
13546  *              capabilities of a Group 0 command, shift over to a
13547  *              Group 1 command. We don't blindly use Group 1
13548  *              commands because a) some drives (CDC Wren IVs) get a
13549  *              bit confused, and b) there is probably a fair amount
13550  *              of speed difference for a target to receive and decode
13551  *              a 10 byte command instead of a 6 byte command.
13552  *
13553  *              The xfer time difference of 6 vs 10 byte CDBs is
13554  *              still significant so this code is still worthwhile.
13555  *              10 byte CDBs are very inefficient with the fas HBA driver
13556  *              and older disks. Each CDB byte took 1 usec with some
13557  *              popular disks.
13558  *
13559  *     Context: Must be called at attach time
13560  */
13561 
13562 static void
13563 sd_init_cdb_limits(struct sd_lun *un)
13564 {
13565         int hba_cdb_limit;
13566 
13567         /*
13568          * Use CDB_GROUP1 commands for most devices except for
13569          * parallel SCSI fixed drives in which case we get better
13570          * performance using CDB_GROUP0 commands (where applicable).
13571          */
13572         un->un_mincdb = SD_CDB_GROUP1;
13573 #if !defined(__fibre)
13574         if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13575             !un->un_f_has_removable_media) {
13576                 un->un_mincdb = SD_CDB_GROUP0;
13577         }
13578 #endif
13579 
13580         /*
13581          * Try to read the max-cdb-length supported by HBA.
13582          */
13583         un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13584         if (0 >= un->un_max_hba_cdb) {
13585                 un->un_max_hba_cdb = CDB_GROUP4;
13586                 hba_cdb_limit = SD_CDB_GROUP4;
13587         } else if (0 < un->un_max_hba_cdb &&
13588             un->un_max_hba_cdb < CDB_GROUP1) {
13589                 hba_cdb_limit = SD_CDB_GROUP0;
13590         } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13591             un->un_max_hba_cdb < CDB_GROUP5) {
13592                 hba_cdb_limit = SD_CDB_GROUP1;
13593         } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13594             un->un_max_hba_cdb < CDB_GROUP4) {
13595                 hba_cdb_limit = SD_CDB_GROUP5;
13596         } else {
13597                 hba_cdb_limit = SD_CDB_GROUP4;
13598         }
13599 
13600         /*
13601          * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13602          * commands for fixed disks unless we are building for a 32 bit
13603          * kernel.
13604          */
13605 #ifdef _LP64
13606         un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13607             min(hba_cdb_limit, SD_CDB_GROUP4);
13608 #else
13609         un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13610             min(hba_cdb_limit, SD_CDB_GROUP1);
13611 #endif
13612 
13613         un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13614             ? sizeof (struct scsi_arq_status) : 1);
13615         if (!ISCD(un))
13616                 un->un_cmd_timeout = (ushort_t)sd_io_time;
13617         un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13618 }
13619 
13620 
13621 /*
13622  *    Function: sd_initpkt_for_buf
13623  *
13624  * Description: Allocate and initialize for transport a scsi_pkt struct,
13625  *              based upon the info specified in the given buf struct.
13626  *
13627  *              Assumes the xb_blkno in the request is absolute (ie,
13628  *              relative to the start of the device (NOT partition!).
13629  *              Also assumes that the request is using the native block
13630  *              size of the device (as returned by the READ CAPACITY
13631  *              command).
13632  *
13633  * Return Code: SD_PKT_ALLOC_SUCCESS
13634  *              SD_PKT_ALLOC_FAILURE
13635  *              SD_PKT_ALLOC_FAILURE_NO_DMA
13636  *              SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13637  *
13638  *     Context: Kernel thread and may be called from software interrupt context
13639  *              as part of a sdrunout callback. This function may not block or
13640  *              call routines that block
13641  */
13642 
13643 static int
13644 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13645 {
13646         struct sd_xbuf  *xp;
13647         struct scsi_pkt *pktp = NULL;
13648         struct sd_lun   *un;
13649         size_t          blockcount;
13650         daddr_t         startblock;
13651         int             rval;
13652         int             cmd_flags;
13653 
13654         ASSERT(bp != NULL);
13655         ASSERT(pktpp != NULL);
13656         xp = SD_GET_XBUF(bp);
13657         ASSERT(xp != NULL);
13658         un = SD_GET_UN(bp);
13659         ASSERT(un != NULL);
13660         ASSERT(mutex_owned(SD_MUTEX(un)));
13661         ASSERT(bp->b_resid == 0);
13662 
13663         SD_TRACE(SD_LOG_IO_CORE, un,
13664             "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13665 
13666         mutex_exit(SD_MUTEX(un));
13667 
13668 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13669         if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13670                 /*
13671                  * Already have a scsi_pkt -- just need DMA resources.
13672                  * We must recompute the CDB in case the mapping returns
13673                  * a nonzero pkt_resid.
13674                  * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13675                  * that is being retried, the unmap/remap of the DMA resouces
13676                  * will result in the entire transfer starting over again
13677                  * from the very first block.
13678                  */
13679                 ASSERT(xp->xb_pktp != NULL);
13680                 pktp = xp->xb_pktp;
13681         } else {
13682                 pktp = NULL;
13683         }
13684 #endif /* __i386 || __amd64 */
13685 
13686         startblock = xp->xb_blkno;   /* Absolute block num. */
13687         blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13688 
13689         cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13690 
13691         /*
13692          * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13693          * call scsi_init_pkt, and build the CDB.
13694          */
13695         rval = sd_setup_rw_pkt(un, &pktp, bp,
13696             cmd_flags, sdrunout, (caddr_t)un,
13697             startblock, blockcount);
13698 
13699         if (rval == 0) {
13700                 /*
13701                  * Success.
13702                  *
13703                  * If partial DMA is being used and required for this transfer.
13704                  * set it up here.
13705                  */
13706                 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13707                     (pktp->pkt_resid != 0)) {
13708 
13709                         /*
13710                          * Save the CDB length and pkt_resid for the
13711                          * next xfer
13712                          */
13713                         xp->xb_dma_resid = pktp->pkt_resid;
13714 
13715                         /* rezero resid */
13716                         pktp->pkt_resid = 0;
13717 
13718                 } else {
13719                         xp->xb_dma_resid = 0;
13720                 }
13721 
13722                 pktp->pkt_flags = un->un_tagflags;
13723                 pktp->pkt_time  = un->un_cmd_timeout;
13724                 pktp->pkt_comp  = sdintr;
13725 
13726                 pktp->pkt_private = bp;
13727                 *pktpp = pktp;
13728 
13729                 SD_TRACE(SD_LOG_IO_CORE, un,
13730                     "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13731 
13732 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13733                 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13734 #endif
13735 
13736                 mutex_enter(SD_MUTEX(un));
13737                 return (SD_PKT_ALLOC_SUCCESS);
13738 
13739         }
13740 
13741         /*
13742          * SD_PKT_ALLOC_FAILURE is the only expected failure code
13743          * from sd_setup_rw_pkt.
13744          */
13745         ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13746 
13747         if (rval == SD_PKT_ALLOC_FAILURE) {
13748                 *pktpp = NULL;
13749                 /*
13750                  * Set the driver state to RWAIT to indicate the driver
13751                  * is waiting on resource allocations. The driver will not
13752                  * suspend, pm_suspend, or detatch while the state is RWAIT.
13753                  */
13754                 mutex_enter(SD_MUTEX(un));
13755                 New_state(un, SD_STATE_RWAIT);
13756 
13757                 SD_ERROR(SD_LOG_IO_CORE, un,
13758                     "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13759 
13760                 if ((bp->b_flags & B_ERROR) != 0) {
13761                         return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13762                 }
13763                 return (SD_PKT_ALLOC_FAILURE);
13764         } else {
13765                 /*
13766                  * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13767                  *
13768                  * This should never happen.  Maybe someone messed with the
13769                  * kernel's minphys?
13770                  */
13771                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13772                     "Request rejected: too large for CDB: "
13773                     "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13774                 SD_ERROR(SD_LOG_IO_CORE, un,
13775                     "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13776                 mutex_enter(SD_MUTEX(un));
13777                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13778 
13779         }
13780 }
13781 
13782 
13783 /*
13784  *    Function: sd_destroypkt_for_buf
13785  *
13786  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13787  *
13788  *     Context: Kernel thread or interrupt context
13789  */
13790 
13791 static void
13792 sd_destroypkt_for_buf(struct buf *bp)
13793 {
13794         ASSERT(bp != NULL);
13795         ASSERT(SD_GET_UN(bp) != NULL);
13796 
13797         SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13798             "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13799 
13800         ASSERT(SD_GET_PKTP(bp) != NULL);
13801         scsi_destroy_pkt(SD_GET_PKTP(bp));
13802 
13803         SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13804             "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13805 }
13806 
13807 /*
13808  *    Function: sd_setup_rw_pkt
13809  *
13810  * Description: Determines appropriate CDB group for the requested LBA
13811  *              and transfer length, calls scsi_init_pkt, and builds
13812  *              the CDB.  Do not use for partial DMA transfers except
13813  *              for the initial transfer since the CDB size must
13814  *              remain constant.
13815  *
13816  *     Context: Kernel thread and may be called from software interrupt
13817  *              context as part of a sdrunout callback. This function may not
13818  *              block or call routines that block
13819  */
13820 
13821 
13822 int
13823 sd_setup_rw_pkt(struct sd_lun *un,
13824     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13825     int (*callback)(caddr_t), caddr_t callback_arg,
13826     diskaddr_t lba, uint32_t blockcount)
13827 {
13828         struct scsi_pkt *return_pktp;
13829         union scsi_cdb *cdbp;
13830         struct sd_cdbinfo *cp = NULL;
13831         int i;
13832 
13833         /*
13834          * See which size CDB to use, based upon the request.
13835          */
13836         for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13837 
13838                 /*
13839                  * Check lba and block count against sd_cdbtab limits.
13840                  * In the partial DMA case, we have to use the same size
13841                  * CDB for all the transfers.  Check lba + blockcount
13842                  * against the max LBA so we know that segment of the
13843                  * transfer can use the CDB we select.
13844                  */
13845                 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13846                     (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13847 
13848                         /*
13849                          * The command will fit into the CDB type
13850                          * specified by sd_cdbtab[i].
13851                          */
13852                         cp = sd_cdbtab + i;
13853 
13854                         /*
13855                          * Call scsi_init_pkt so we can fill in the
13856                          * CDB.
13857                          */
13858                         return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13859                             bp, cp->sc_grpcode, un->un_status_len, 0,
13860                             flags, callback, callback_arg);
13861 
13862                         if (return_pktp != NULL) {
13863 
13864                                 /*
13865                                  * Return new value of pkt
13866                                  */
13867                                 *pktpp = return_pktp;
13868 
13869                                 /*
13870                                  * To be safe, zero the CDB insuring there is
13871                                  * no leftover data from a previous command.
13872                                  */
13873                                 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13874 
13875                                 /*
13876                                  * Handle partial DMA mapping
13877                                  */
13878                                 if (return_pktp->pkt_resid != 0) {
13879 
13880                                         /*
13881                                          * Not going to xfer as many blocks as
13882                                          * originally expected
13883                                          */
13884                                         blockcount -=
13885                                             SD_BYTES2TGTBLOCKS(un,
13886                                             return_pktp->pkt_resid);
13887                                 }
13888 
13889                                 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13890 
13891                                 /*
13892                                  * Set command byte based on the CDB
13893                                  * type we matched.
13894                                  */
13895                                 cdbp->scc_cmd = cp->sc_grpmask |
13896                                     ((bp->b_flags & B_READ) ?
13897                                     SCMD_READ : SCMD_WRITE);
13898 
13899                                 SD_FILL_SCSI1_LUN(un, return_pktp);
13900 
13901                                 /*
13902                                  * Fill in LBA and length
13903                                  */
13904                                 ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13905                                     (cp->sc_grpcode == CDB_GROUP4) ||
13906                                     (cp->sc_grpcode == CDB_GROUP0) ||
13907                                     (cp->sc_grpcode == CDB_GROUP5));
13908 
13909                                 if (cp->sc_grpcode == CDB_GROUP1) {
13910                                         FORMG1ADDR(cdbp, lba);
13911                                         FORMG1COUNT(cdbp, blockcount);
13912                                         return (0);
13913                                 } else if (cp->sc_grpcode == CDB_GROUP4) {
13914                                         FORMG4LONGADDR(cdbp, lba);
13915                                         FORMG4COUNT(cdbp, blockcount);
13916                                         return (0);
13917                                 } else if (cp->sc_grpcode == CDB_GROUP0) {
13918                                         FORMG0ADDR(cdbp, lba);
13919                                         FORMG0COUNT(cdbp, blockcount);
13920                                         return (0);
13921                                 } else if (cp->sc_grpcode == CDB_GROUP5) {
13922                                         FORMG5ADDR(cdbp, lba);
13923                                         FORMG5COUNT(cdbp, blockcount);
13924                                         return (0);
13925                                 }
13926 
13927                                 /*
13928                                  * It should be impossible to not match one
13929                                  * of the CDB types above, so we should never
13930                                  * reach this point.  Set the CDB command byte
13931                                  * to test-unit-ready to avoid writing
13932                                  * to somewhere we don't intend.
13933                                  */
13934                                 cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13935                                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13936                         } else {
13937                                 /*
13938                                  * Couldn't get scsi_pkt
13939                                  */
13940                                 return (SD_PKT_ALLOC_FAILURE);
13941                         }
13942                 }
13943         }
13944 
13945         /*
13946          * None of the available CDB types were suitable.  This really
13947          * should never happen:  on a 64 bit system we support
13948          * READ16/WRITE16 which will hold an entire 64 bit disk address
13949          * and on a 32 bit system we will refuse to bind to a device
13950          * larger than 2TB so addresses will never be larger than 32 bits.
13951          */
13952         return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13953 }
13954 
13955 /*
13956  *    Function: sd_setup_next_rw_pkt
13957  *
13958  * Description: Setup packet for partial DMA transfers, except for the
13959  *              initial transfer.  sd_setup_rw_pkt should be used for
13960  *              the initial transfer.
13961  *
13962  *     Context: Kernel thread and may be called from interrupt context.
13963  */
13964 
13965 int
13966 sd_setup_next_rw_pkt(struct sd_lun *un,
13967     struct scsi_pkt *pktp, struct buf *bp,
13968     diskaddr_t lba, uint32_t blockcount)
13969 {
13970         uchar_t com;
13971         union scsi_cdb *cdbp;
13972         uchar_t cdb_group_id;
13973 
13974         ASSERT(pktp != NULL);
13975         ASSERT(pktp->pkt_cdbp != NULL);
13976 
13977         cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13978         com = cdbp->scc_cmd;
13979         cdb_group_id = CDB_GROUPID(com);
13980 
13981         ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13982             (cdb_group_id == CDB_GROUPID_1) ||
13983             (cdb_group_id == CDB_GROUPID_4) ||
13984             (cdb_group_id == CDB_GROUPID_5));
13985 
13986         /*
13987          * Move pkt to the next portion of the xfer.
13988          * func is NULL_FUNC so we do not have to release
13989          * the disk mutex here.
13990          */
13991         if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13992             NULL_FUNC, NULL) == pktp) {
13993                 /* Success.  Handle partial DMA */
13994                 if (pktp->pkt_resid != 0) {
13995                         blockcount -=
13996                             SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13997                 }
13998 
13999                 cdbp->scc_cmd = com;
14000                 SD_FILL_SCSI1_LUN(un, pktp);
14001                 if (cdb_group_id == CDB_GROUPID_1) {
14002                         FORMG1ADDR(cdbp, lba);
14003                         FORMG1COUNT(cdbp, blockcount);
14004                         return (0);
14005                 } else if (cdb_group_id == CDB_GROUPID_4) {
14006                         FORMG4LONGADDR(cdbp, lba);
14007                         FORMG4COUNT(cdbp, blockcount);
14008                         return (0);
14009                 } else if (cdb_group_id == CDB_GROUPID_0) {
14010                         FORMG0ADDR(cdbp, lba);
14011                         FORMG0COUNT(cdbp, blockcount);
14012                         return (0);
14013                 } else if (cdb_group_id == CDB_GROUPID_5) {
14014                         FORMG5ADDR(cdbp, lba);
14015                         FORMG5COUNT(cdbp, blockcount);
14016                         return (0);
14017                 }
14018 
14019                 /* Unreachable */
14020                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14021         }
14022 
14023         /*
14024          * Error setting up next portion of cmd transfer.
14025          * Something is definitely very wrong and this
14026          * should not happen.
14027          */
14028         return (SD_PKT_ALLOC_FAILURE);
14029 }
14030 
14031 /*
14032  *    Function: sd_initpkt_for_uscsi
14033  *
14034  * Description: Allocate and initialize for transport a scsi_pkt struct,
14035  *              based upon the info specified in the given uscsi_cmd struct.
14036  *
14037  * Return Code: SD_PKT_ALLOC_SUCCESS
14038  *              SD_PKT_ALLOC_FAILURE
14039  *              SD_PKT_ALLOC_FAILURE_NO_DMA
14040  *              SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14041  *
14042  *     Context: Kernel thread and may be called from software interrupt context
14043  *              as part of a sdrunout callback. This function may not block or
14044  *              call routines that block
14045  */
14046 
14047 static int
14048 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14049 {
14050         struct uscsi_cmd *uscmd;
14051         struct sd_xbuf  *xp;
14052         struct scsi_pkt *pktp;
14053         struct sd_lun   *un;
14054         uint32_t        flags = 0;
14055 
14056         ASSERT(bp != NULL);
14057         ASSERT(pktpp != NULL);
14058         xp = SD_GET_XBUF(bp);
14059         ASSERT(xp != NULL);
14060         un = SD_GET_UN(bp);
14061         ASSERT(un != NULL);
14062         ASSERT(mutex_owned(SD_MUTEX(un)));
14063 
14064         /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14065         uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14066         ASSERT(uscmd != NULL);
14067 
14068         SD_TRACE(SD_LOG_IO_CORE, un,
14069             "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14070 
14071         /*
14072          * Allocate the scsi_pkt for the command.
14073          * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14074          *       during scsi_init_pkt time and will continue to use the
14075          *       same path as long as the same scsi_pkt is used without
14076          *       intervening scsi_dma_free(). Since uscsi command does
14077          *       not call scsi_dmafree() before retry failed command, it
14078          *       is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14079          *       set such that scsi_vhci can use other available path for
14080          *       retry. Besides, ucsci command does not allow DMA breakup,
14081          *       so there is no need to set PKT_DMA_PARTIAL flag.
14082          */
14083         if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14084                 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14085                     ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14086                     ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14087                     - sizeof (struct scsi_extended_sense)), 0,
14088                     (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14089                     sdrunout, (caddr_t)un);
14090         } else {
14091                 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14092                     ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14093                     sizeof (struct scsi_arq_status), 0,
14094                     (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14095                     sdrunout, (caddr_t)un);
14096         }
14097 
14098         if (pktp == NULL) {
14099                 *pktpp = NULL;
14100                 /*
14101                  * Set the driver state to RWAIT to indicate the driver
14102                  * is waiting on resource allocations. The driver will not
14103                  * suspend, pm_suspend, or detatch while the state is RWAIT.
14104                  */
14105                 New_state(un, SD_STATE_RWAIT);
14106 
14107                 SD_ERROR(SD_LOG_IO_CORE, un,
14108                     "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14109 
14110                 if ((bp->b_flags & B_ERROR) != 0) {
14111                         return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14112                 }
14113                 return (SD_PKT_ALLOC_FAILURE);
14114         }
14115 
14116         /*
14117          * We do not do DMA breakup for USCSI commands, so return failure
14118          * here if all the needed DMA resources were not allocated.
14119          */
14120         if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14121             (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14122                 scsi_destroy_pkt(pktp);
14123                 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14124                     "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14125                 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14126         }
14127 
14128         /* Init the cdb from the given uscsi struct */
14129         (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14130             uscmd->uscsi_cdb[0], 0, 0, 0);
14131 
14132         SD_FILL_SCSI1_LUN(un, pktp);
14133 
14134         /*
14135          * Set up the optional USCSI flags. See the uscsi (7I) man page
14136          * for listing of the supported flags.
14137          */
14138 
14139         if (uscmd->uscsi_flags & USCSI_SILENT) {
14140                 flags |= FLAG_SILENT;
14141         }
14142 
14143         if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14144                 flags |= FLAG_DIAGNOSE;
14145         }
14146 
14147         if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14148                 flags |= FLAG_ISOLATE;
14149         }
14150 
14151         if (un->un_f_is_fibre == FALSE) {
14152                 if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14153                         flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14154                 }
14155         }
14156 
14157         /*
14158          * Set the pkt flags here so we save time later.
14159          * Note: These flags are NOT in the uscsi man page!!!
14160          */
14161         if (uscmd->uscsi_flags & USCSI_HEAD) {
14162                 flags |= FLAG_HEAD;
14163         }
14164 
14165         if (uscmd->uscsi_flags & USCSI_NOINTR) {
14166                 flags |= FLAG_NOINTR;
14167         }
14168 
14169         /*
14170          * For tagged queueing, things get a bit complicated.
14171          * Check first for head of queue and last for ordered queue.
14172          * If neither head nor order, use the default driver tag flags.
14173          */
14174         if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14175                 if (uscmd->uscsi_flags & USCSI_HTAG) {
14176                         flags |= FLAG_HTAG;
14177                 } else if (uscmd->uscsi_flags & USCSI_OTAG) {
14178                         flags |= FLAG_OTAG;
14179                 } else {
14180                         flags |= un->un_tagflags & FLAG_TAGMASK;
14181                 }
14182         }
14183 
14184         if (uscmd->uscsi_flags & USCSI_NODISCON) {
14185                 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14186         }
14187 
14188         pktp->pkt_flags = flags;
14189 
14190         /* Transfer uscsi information to scsi_pkt */
14191         (void) scsi_uscsi_pktinit(uscmd, pktp);
14192 
14193         /* Copy the caller's CDB into the pkt... */
14194         bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14195 
14196         if (uscmd->uscsi_timeout == 0) {
14197                 pktp->pkt_time = un->un_uscsi_timeout;
14198         } else {
14199                 pktp->pkt_time = uscmd->uscsi_timeout;
14200         }
14201 
14202         /* need it later to identify USCSI request in sdintr */
14203         xp->xb_pkt_flags |= SD_XB_USCSICMD;
14204 
14205         xp->xb_sense_resid = uscmd->uscsi_rqresid;
14206 
14207         pktp->pkt_private = bp;
14208         pktp->pkt_comp = sdintr;
14209         *pktpp = pktp;
14210 
14211         SD_TRACE(SD_LOG_IO_CORE, un,
14212             "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14213 
14214         return (SD_PKT_ALLOC_SUCCESS);
14215 }
14216 
14217 
14218 /*
14219  *    Function: sd_destroypkt_for_uscsi
14220  *
14221  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14222  *              IOs.. Also saves relevant info into the associated uscsi_cmd
14223  *              struct.
14224  *
14225  *     Context: May be called under interrupt context
14226  */
14227 
14228 static void
14229 sd_destroypkt_for_uscsi(struct buf *bp)
14230 {
14231         struct uscsi_cmd *uscmd;
14232         struct sd_xbuf  *xp;
14233         struct scsi_pkt *pktp;
14234         struct sd_lun   *un;
14235         struct sd_uscsi_info *suip;
14236 
14237         ASSERT(bp != NULL);
14238         xp = SD_GET_XBUF(bp);
14239         ASSERT(xp != NULL);
14240         un = SD_GET_UN(bp);
14241         ASSERT(un != NULL);
14242         ASSERT(!mutex_owned(SD_MUTEX(un)));
14243         pktp = SD_GET_PKTP(bp);
14244         ASSERT(pktp != NULL);
14245 
14246         SD_TRACE(SD_LOG_IO_CORE, un,
14247             "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14248 
14249         /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14250         uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14251         ASSERT(uscmd != NULL);
14252 
14253         /* Save the status and the residual into the uscsi_cmd struct */
14254         uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14255         uscmd->uscsi_resid  = bp->b_resid;
14256 
14257         /* Transfer scsi_pkt information to uscsi */
14258         (void) scsi_uscsi_pktfini(pktp, uscmd);
14259 
14260         /*
14261          * If enabled, copy any saved sense data into the area specified
14262          * by the uscsi command.
14263          */
14264         if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14265             (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14266                 /*
14267                  * Note: uscmd->uscsi_rqbuf should always point to a buffer
14268                  * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14269                  */
14270                 uscmd->uscsi_rqstatus = xp->xb_sense_status;
14271                 uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14272                 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14273                         bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14274                             MAX_SENSE_LENGTH);
14275                 } else {
14276                         bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14277                             SENSE_LENGTH);
14278                 }
14279         }
14280         /*
14281          * The following assignments are for SCSI FMA.
14282          */
14283         ASSERT(xp->xb_private != NULL);
14284         suip = (struct sd_uscsi_info *)xp->xb_private;
14285         suip->ui_pkt_reason = pktp->pkt_reason;
14286         suip->ui_pkt_state = pktp->pkt_state;
14287         suip->ui_pkt_statistics = pktp->pkt_statistics;
14288         suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14289 
14290         /* We are done with the scsi_pkt; free it now */
14291         ASSERT(SD_GET_PKTP(bp) != NULL);
14292         scsi_destroy_pkt(SD_GET_PKTP(bp));
14293 
14294         SD_TRACE(SD_LOG_IO_CORE, un,
14295             "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14296 }
14297 
14298 
14299 /*
14300  *    Function: sd_bioclone_alloc
14301  *
14302  * Description: Allocate a buf(9S) and init it as per the given buf
14303  *              and the various arguments.  The associated sd_xbuf
14304  *              struct is (nearly) duplicated.  The struct buf *bp
14305  *              argument is saved in new_xp->xb_private.
14306  *
14307  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14308  *              datalen - size of data area for the shadow bp
14309  *              blkno - starting LBA
14310  *              func - function pointer for b_iodone in the shadow buf. (May
14311  *                      be NULL if none.)
14312  *
14313  * Return Code: Pointer to allocates buf(9S) struct
14314  *
14315  *     Context: Can sleep.
14316  */
14317 
14318 static struct buf *
14319 sd_bioclone_alloc(struct buf *bp, size_t datalen, daddr_t blkno,
14320     int (*func)(struct buf *))
14321 {
14322         struct  sd_lun  *un;
14323         struct  sd_xbuf *xp;
14324         struct  sd_xbuf *new_xp;
14325         struct  buf     *new_bp;
14326 
14327         ASSERT(bp != NULL);
14328         xp = SD_GET_XBUF(bp);
14329         ASSERT(xp != NULL);
14330         un = SD_GET_UN(bp);
14331         ASSERT(un != NULL);
14332         ASSERT(!mutex_owned(SD_MUTEX(un)));
14333 
14334         new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14335             NULL, KM_SLEEP);
14336 
14337         new_bp->b_lblkno     = blkno;
14338 
14339         /*
14340          * Allocate an xbuf for the shadow bp and copy the contents of the
14341          * original xbuf into it.
14342          */
14343         new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14344         bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14345 
14346         /*
14347          * The given bp is automatically saved in the xb_private member
14348          * of the new xbuf.  Callers are allowed to depend on this.
14349          */
14350         new_xp->xb_private = bp;
14351 
14352         new_bp->b_private  = new_xp;
14353 
14354         return (new_bp);
14355 }
14356 
14357 /*
14358  *    Function: sd_shadow_buf_alloc
14359  *
14360  * Description: Allocate a buf(9S) and init it as per the given buf
14361  *              and the various arguments.  The associated sd_xbuf
14362  *              struct is (nearly) duplicated.  The struct buf *bp
14363  *              argument is saved in new_xp->xb_private.
14364  *
14365  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14366  *              datalen - size of data area for the shadow bp
14367  *              bflags - B_READ or B_WRITE (pseudo flag)
14368  *              blkno - starting LBA
14369  *              func - function pointer for b_iodone in the shadow buf. (May
14370  *                      be NULL if none.)
14371  *
14372  * Return Code: Pointer to allocates buf(9S) struct
14373  *
14374  *     Context: Can sleep.
14375  */
14376 
14377 static struct buf *
14378 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14379     daddr_t blkno, int (*func)(struct buf *))
14380 {
14381         struct  sd_lun  *un;
14382         struct  sd_xbuf *xp;
14383         struct  sd_xbuf *new_xp;
14384         struct  buf     *new_bp;
14385 
14386         ASSERT(bp != NULL);
14387         xp = SD_GET_XBUF(bp);
14388         ASSERT(xp != NULL);
14389         un = SD_GET_UN(bp);
14390         ASSERT(un != NULL);
14391         ASSERT(!mutex_owned(SD_MUTEX(un)));
14392 
14393         if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14394                 bp_mapin(bp);
14395         }
14396 
14397         bflags &= (B_READ | B_WRITE);
14398 #if defined(__i386) || defined(__amd64)
14399         new_bp = getrbuf(KM_SLEEP);
14400         new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14401         new_bp->b_bcount = datalen;
14402         new_bp->b_flags = bflags |
14403             (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14404 #else
14405         new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14406             datalen, bflags, SLEEP_FUNC, NULL);
14407 #endif
14408         new_bp->av_forw      = NULL;
14409         new_bp->av_back      = NULL;
14410         new_bp->b_dev        = bp->b_dev;
14411         new_bp->b_blkno      = blkno;
14412         new_bp->b_iodone = func;
14413         new_bp->b_edev       = bp->b_edev;
14414         new_bp->b_resid      = 0;
14415 
14416         /* We need to preserve the B_FAILFAST flag */
14417         if (bp->b_flags & B_FAILFAST) {
14418                 new_bp->b_flags |= B_FAILFAST;
14419         }
14420 
14421         /*
14422          * Allocate an xbuf for the shadow bp and copy the contents of the
14423          * original xbuf into it.
14424          */
14425         new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14426         bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14427 
14428         /* Need later to copy data between the shadow buf & original buf! */
14429         new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14430 
14431         /*
14432          * The given bp is automatically saved in the xb_private member
14433          * of the new xbuf.  Callers are allowed to depend on this.
14434          */
14435         new_xp->xb_private = bp;
14436 
14437         new_bp->b_private  = new_xp;
14438 
14439         return (new_bp);
14440 }
14441 
14442 /*
14443  *    Function: sd_bioclone_free
14444  *
14445  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14446  *              in the larger than partition operation.
14447  *
14448  *     Context: May be called under interrupt context
14449  */
14450 
14451 static void
14452 sd_bioclone_free(struct buf *bp)
14453 {
14454         struct sd_xbuf  *xp;
14455 
14456         ASSERT(bp != NULL);
14457         xp = SD_GET_XBUF(bp);
14458         ASSERT(xp != NULL);
14459 
14460         /*
14461          * Call bp_mapout() before freeing the buf,  in case a lower
14462          * layer or HBA  had done a bp_mapin().  we must do this here
14463          * as we are the "originator" of the shadow buf.
14464          */
14465         bp_mapout(bp);
14466 
14467         /*
14468          * Null out b_iodone before freeing the bp, to ensure that the driver
14469          * never gets confused by a stale value in this field. (Just a little
14470          * extra defensiveness here.)
14471          */
14472         bp->b_iodone = NULL;
14473 
14474         freerbuf(bp);
14475 
14476         kmem_free(xp, sizeof (struct sd_xbuf));
14477 }
14478 
14479 /*
14480  *    Function: sd_shadow_buf_free
14481  *
14482  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14483  *
14484  *     Context: May be called under interrupt context
14485  */
14486 
14487 static void
14488 sd_shadow_buf_free(struct buf *bp)
14489 {
14490         struct sd_xbuf  *xp;
14491 
14492         ASSERT(bp != NULL);
14493         xp = SD_GET_XBUF(bp);
14494         ASSERT(xp != NULL);
14495 
14496 #if defined(__sparc)
14497         /*
14498          * Call bp_mapout() before freeing the buf,  in case a lower
14499          * layer or HBA  had done a bp_mapin().  we must do this here
14500          * as we are the "originator" of the shadow buf.
14501          */
14502         bp_mapout(bp);
14503 #endif
14504 
14505         /*
14506          * Null out b_iodone before freeing the bp, to ensure that the driver
14507          * never gets confused by a stale value in this field. (Just a little
14508          * extra defensiveness here.)
14509          */
14510         bp->b_iodone = NULL;
14511 
14512 #if defined(__i386) || defined(__amd64)
14513         kmem_free(bp->b_un.b_addr, bp->b_bcount);
14514         freerbuf(bp);
14515 #else
14516         scsi_free_consistent_buf(bp);
14517 #endif
14518 
14519         kmem_free(xp, sizeof (struct sd_xbuf));
14520 }
14521 
14522 
14523 /*
14524  *    Function: sd_print_transport_rejected_message
14525  *
14526  * Description: This implements the ludicrously complex rules for printing
14527  *              a "transport rejected" message.  This is to address the
14528  *              specific problem of having a flood of this error message
14529  *              produced when a failover occurs.
14530  *
14531  *     Context: Any.
14532  */
14533 
14534 static void
14535 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14536     int code)
14537 {
14538         ASSERT(un != NULL);
14539         ASSERT(mutex_owned(SD_MUTEX(un)));
14540         ASSERT(xp != NULL);
14541 
14542         /*
14543          * Print the "transport rejected" message under the following
14544          * conditions:
14545          *
14546          * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14547          * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14548          * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14549          *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14550          *   scsi_transport(9F) (which indicates that the target might have
14551          *   gone off-line).  This uses the un->un_tran_fatal_count
14552          *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14553          *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14554          *   from scsi_transport().
14555          *
14556          * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14557          * the preceeding cases in order for the message to be printed.
14558          */
14559         if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14560             (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14561                 if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14562                     (code != TRAN_FATAL_ERROR) ||
14563                     (un->un_tran_fatal_count == 1)) {
14564                         switch (code) {
14565                         case TRAN_BADPKT:
14566                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14567                                     "transport rejected bad packet\n");
14568                                 break;
14569                         case TRAN_FATAL_ERROR:
14570                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14571                                     "transport rejected fatal error\n");
14572                                 break;
14573                         default:
14574                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14575                                     "transport rejected (%d)\n", code);
14576                                 break;
14577                         }
14578                 }
14579         }
14580 }
14581 
14582 
14583 /*
14584  *    Function: sd_add_buf_to_waitq
14585  *
14586  * Description: Add the given buf(9S) struct to the wait queue for the
14587  *              instance.  If sorting is enabled, then the buf is added
14588  *              to the queue via an elevator sort algorithm (a la
14589  *              disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14590  *              If sorting is not enabled, then the buf is just added
14591  *              to the end of the wait queue.
14592  *
14593  * Return Code: void
14594  *
14595  *     Context: Does not sleep/block, therefore technically can be called
14596  *              from any context.  However if sorting is enabled then the
14597  *              execution time is indeterminate, and may take long if
14598  *              the wait queue grows large.
14599  */
14600 
14601 static void
14602 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14603 {
14604         struct buf *ap;
14605 
14606         ASSERT(bp != NULL);
14607         ASSERT(un != NULL);
14608         ASSERT(mutex_owned(SD_MUTEX(un)));
14609 
14610         /* If the queue is empty, add the buf as the only entry & return. */
14611         if (un->un_waitq_headp == NULL) {
14612                 ASSERT(un->un_waitq_tailp == NULL);
14613                 un->un_waitq_headp = un->un_waitq_tailp = bp;
14614                 bp->av_forw = NULL;
14615                 return;
14616         }
14617 
14618         ASSERT(un->un_waitq_tailp != NULL);
14619 
14620         /*
14621          * If sorting is disabled, just add the buf to the tail end of
14622          * the wait queue and return.
14623          */
14624         if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14625                 un->un_waitq_tailp->av_forw = bp;
14626                 un->un_waitq_tailp = bp;
14627                 bp->av_forw = NULL;
14628                 return;
14629         }
14630 
14631         /*
14632          * Sort thru the list of requests currently on the wait queue
14633          * and add the new buf request at the appropriate position.
14634          *
14635          * The un->un_waitq_headp is an activity chain pointer on which
14636          * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14637          * first queue holds those requests which are positioned after
14638          * the current SD_GET_BLKNO() (in the first request); the second holds
14639          * requests which came in after their SD_GET_BLKNO() number was passed.
14640          * Thus we implement a one way scan, retracting after reaching
14641          * the end of the drive to the first request on the second
14642          * queue, at which time it becomes the first queue.
14643          * A one-way scan is natural because of the way UNIX read-ahead
14644          * blocks are allocated.
14645          *
14646          * If we lie after the first request, then we must locate the
14647          * second request list and add ourselves to it.
14648          */
14649         ap = un->un_waitq_headp;
14650         if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14651                 while (ap->av_forw != NULL) {
14652                         /*
14653                          * Look for an "inversion" in the (normally
14654                          * ascending) block numbers. This indicates
14655                          * the start of the second request list.
14656                          */
14657                         if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14658                                 /*
14659                                  * Search the second request list for the
14660                                  * first request at a larger block number.
14661                                  * We go before that; however if there is
14662                                  * no such request, we go at the end.
14663                                  */
14664                                 do {
14665                                         if (SD_GET_BLKNO(bp) <
14666                                             SD_GET_BLKNO(ap->av_forw)) {
14667                                                 goto insert;
14668                                         }
14669                                         ap = ap->av_forw;
14670                                 } while (ap->av_forw != NULL);
14671                                 goto insert;            /* after last */
14672                         }
14673                         ap = ap->av_forw;
14674                 }
14675 
14676                 /*
14677                  * No inversions... we will go after the last, and
14678                  * be the first request in the second request list.
14679                  */
14680                 goto insert;
14681         }
14682 
14683         /*
14684          * Request is at/after the current request...
14685          * sort in the first request list.
14686          */
14687         while (ap->av_forw != NULL) {
14688                 /*
14689                  * We want to go after the current request (1) if
14690                  * there is an inversion after it (i.e. it is the end
14691                  * of the first request list), or (2) if the next
14692                  * request is a larger block no. than our request.
14693                  */
14694                 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14695                     (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14696                         goto insert;
14697                 }
14698                 ap = ap->av_forw;
14699         }
14700 
14701         /*
14702          * Neither a second list nor a larger request, therefore
14703          * we go at the end of the first list (which is the same
14704          * as the end of the whole schebang).
14705          */
14706 insert:
14707         bp->av_forw = ap->av_forw;
14708         ap->av_forw = bp;
14709 
14710         /*
14711          * If we inserted onto the tail end of the waitq, make sure the
14712          * tail pointer is updated.
14713          */
14714         if (ap == un->un_waitq_tailp) {
14715                 un->un_waitq_tailp = bp;
14716         }
14717 }
14718 
14719 
14720 /*
14721  *    Function: sd_start_cmds
14722  *
14723  * Description: Remove and transport cmds from the driver queues.
14724  *
14725  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14726  *
14727  *              immed_bp - ptr to a buf to be transported immediately. Only
14728  *              the immed_bp is transported; bufs on the waitq are not
14729  *              processed and the un_retry_bp is not checked.  If immed_bp is
14730  *              NULL, then normal queue processing is performed.
14731  *
14732  *     Context: May be called from kernel thread context, interrupt context,
14733  *              or runout callback context. This function may not block or
14734  *              call routines that block.
14735  */
14736 
14737 static void
14738 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14739 {
14740         struct  sd_xbuf *xp;
14741         struct  buf     *bp;
14742         void    (*statp)(kstat_io_t *);
14743 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14744         void    (*saved_statp)(kstat_io_t *);
14745 #endif
14746         int     rval;
14747         struct sd_fm_internal *sfip = NULL;
14748 
14749         ASSERT(un != NULL);
14750         ASSERT(mutex_owned(SD_MUTEX(un)));
14751         ASSERT(un->un_ncmds_in_transport >= 0);
14752         ASSERT(un->un_throttle >= 0);
14753 
14754         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14755 
14756         do {
14757 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14758                 saved_statp = NULL;
14759 #endif
14760 
14761                 /*
14762                  * If we are syncing or dumping, fail the command to
14763                  * avoid recursively calling back into scsi_transport().
14764                  * The dump I/O itself uses a separate code path so this
14765                  * only prevents non-dump I/O from being sent while dumping.
14766                  * File system sync takes place before dumping begins.
14767                  * During panic, filesystem I/O is allowed provided
14768                  * un_in_callback is <= 1.  This is to prevent recursion
14769                  * such as sd_start_cmds -> scsi_transport -> sdintr ->
14770                  * sd_start_cmds and so on.  See panic.c for more information
14771                  * about the states the system can be in during panic.
14772                  */
14773                 if ((un->un_state == SD_STATE_DUMPING) ||
14774                     (ddi_in_panic() && (un->un_in_callback > 1))) {
14775                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14776                             "sd_start_cmds: panicking\n");
14777                         goto exit;
14778                 }
14779 
14780                 if ((bp = immed_bp) != NULL) {
14781                         /*
14782                          * We have a bp that must be transported immediately.
14783                          * It's OK to transport the immed_bp here without doing
14784                          * the throttle limit check because the immed_bp is
14785                          * always used in a retry/recovery case. This means
14786                          * that we know we are not at the throttle limit by
14787                          * virtue of the fact that to get here we must have
14788                          * already gotten a command back via sdintr(). This also
14789                          * relies on (1) the command on un_retry_bp preventing
14790                          * further commands from the waitq from being issued;
14791                          * and (2) the code in sd_retry_command checking the
14792                          * throttle limit before issuing a delayed or immediate
14793                          * retry. This holds even if the throttle limit is
14794                          * currently ratcheted down from its maximum value.
14795                          */
14796                         statp = kstat_runq_enter;
14797                         if (bp == un->un_retry_bp) {
14798                                 ASSERT((un->un_retry_statp == NULL) ||
14799                                     (un->un_retry_statp == kstat_waitq_enter) ||
14800                                     (un->un_retry_statp ==
14801                                     kstat_runq_back_to_waitq));
14802                                 /*
14803                                  * If the waitq kstat was incremented when
14804                                  * sd_set_retry_bp() queued this bp for a retry,
14805                                  * then we must set up statp so that the waitq
14806                                  * count will get decremented correctly below.
14807                                  * Also we must clear un->un_retry_statp to
14808                                  * ensure that we do not act on a stale value
14809                                  * in this field.
14810                                  */
14811                                 if ((un->un_retry_statp == kstat_waitq_enter) ||
14812                                     (un->un_retry_statp ==
14813                                     kstat_runq_back_to_waitq)) {
14814                                         statp = kstat_waitq_to_runq;
14815                                 }
14816 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14817                                 saved_statp = un->un_retry_statp;
14818 #endif
14819                                 un->un_retry_statp = NULL;
14820 
14821                                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14822                                     "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14823                                     "un_throttle:%d un_ncmds_in_transport:%d\n",
14824                                     un, un->un_retry_bp, un->un_throttle,
14825                                     un->un_ncmds_in_transport);
14826                         } else {
14827                                 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14828                                     "processing priority bp:0x%p\n", bp);
14829                         }
14830 
14831                 } else if ((bp = un->un_waitq_headp) != NULL) {
14832                         /*
14833                          * A command on the waitq is ready to go, but do not
14834                          * send it if:
14835                          *
14836                          * (1) the throttle limit has been reached, or
14837                          * (2) a retry is pending, or
14838                          * (3) a START_STOP_UNIT callback pending, or
14839                          * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14840                          *      command is pending.
14841                          *
14842                          * For all of these conditions, IO processing will
14843                          * restart after the condition is cleared.
14844                          */
14845                         if (un->un_ncmds_in_transport >= un->un_throttle) {
14846                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14847                                     "sd_start_cmds: exiting, "
14848                                     "throttle limit reached!\n");
14849                                 goto exit;
14850                         }
14851                         if (un->un_retry_bp != NULL) {
14852                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14853                                     "sd_start_cmds: exiting, retry pending!\n");
14854                                 goto exit;
14855                         }
14856                         if (un->un_startstop_timeid != NULL) {
14857                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14858                                     "sd_start_cmds: exiting, "
14859                                     "START_STOP pending!\n");
14860                                 goto exit;
14861                         }
14862                         if (un->un_direct_priority_timeid != NULL) {
14863                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14864                                     "sd_start_cmds: exiting, "
14865                                     "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14866                                 goto exit;
14867                         }
14868 
14869                         /* Dequeue the command */
14870                         un->un_waitq_headp = bp->av_forw;
14871                         if (un->un_waitq_headp == NULL) {
14872                                 un->un_waitq_tailp = NULL;
14873                         }
14874                         bp->av_forw = NULL;
14875                         statp = kstat_waitq_to_runq;
14876                         SD_TRACE(SD_LOG_IO_CORE, un,
14877                             "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14878 
14879                 } else {
14880                         /* No work to do so bail out now */
14881                         SD_TRACE(SD_LOG_IO_CORE, un,
14882                             "sd_start_cmds: no more work, exiting!\n");
14883                         goto exit;
14884                 }
14885 
14886                 /*
14887                  * Reset the state to normal. This is the mechanism by which
14888                  * the state transitions from either SD_STATE_RWAIT or
14889                  * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14890                  * If state is SD_STATE_PM_CHANGING then this command is
14891                  * part of the device power control and the state must
14892                  * not be put back to normal. Doing so would would
14893                  * allow new commands to proceed when they shouldn't,
14894                  * the device may be going off.
14895                  */
14896                 if ((un->un_state != SD_STATE_SUSPENDED) &&
14897                     (un->un_state != SD_STATE_PM_CHANGING)) {
14898                         New_state(un, SD_STATE_NORMAL);
14899                 }
14900 
14901                 xp = SD_GET_XBUF(bp);
14902                 ASSERT(xp != NULL);
14903 
14904 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14905                 /*
14906                  * Allocate the scsi_pkt if we need one, or attach DMA
14907                  * resources if we have a scsi_pkt that needs them. The
14908                  * latter should only occur for commands that are being
14909                  * retried.
14910                  */
14911                 if ((xp->xb_pktp == NULL) ||
14912                     ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14913 #else
14914                 if (xp->xb_pktp == NULL) {
14915 #endif
14916                         /*
14917                          * There is no scsi_pkt allocated for this buf. Call
14918                          * the initpkt function to allocate & init one.
14919                          *
14920                          * The scsi_init_pkt runout callback functionality is
14921                          * implemented as follows:
14922                          *
14923                          * 1) The initpkt function always calls
14924                          *    scsi_init_pkt(9F) with sdrunout specified as the
14925                          *    callback routine.
14926                          * 2) A successful packet allocation is initialized and
14927                          *    the I/O is transported.
14928                          * 3) The I/O associated with an allocation resource
14929                          *    failure is left on its queue to be retried via
14930                          *    runout or the next I/O.
14931                          * 4) The I/O associated with a DMA error is removed
14932                          *    from the queue and failed with EIO. Processing of
14933                          *    the transport queues is also halted to be
14934                          *    restarted via runout or the next I/O.
14935                          * 5) The I/O associated with a CDB size or packet
14936                          *    size error is removed from the queue and failed
14937                          *    with EIO. Processing of the transport queues is
14938                          *    continued.
14939                          *
14940                          * Note: there is no interface for canceling a runout
14941                          * callback. To prevent the driver from detaching or
14942                          * suspending while a runout is pending the driver
14943                          * state is set to SD_STATE_RWAIT
14944                          *
14945                          * Note: using the scsi_init_pkt callback facility can
14946                          * result in an I/O request persisting at the head of
14947                          * the list which cannot be satisfied even after
14948                          * multiple retries. In the future the driver may
14949                          * implement some kind of maximum runout count before
14950                          * failing an I/O.
14951                          *
14952                          * Note: the use of funcp below may seem superfluous,
14953                          * but it helps warlock figure out the correct
14954                          * initpkt function calls (see [s]sd.wlcmd).
14955                          */
14956                         struct scsi_pkt *pktp;
14957                         int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14958 
14959                         ASSERT(bp != un->un_rqs_bp);
14960 
14961                         funcp = sd_initpkt_map[xp->xb_chain_iostart];
14962                         switch ((*funcp)(bp, &pktp)) {
14963                         case  SD_PKT_ALLOC_SUCCESS:
14964                                 xp->xb_pktp = pktp;
14965                                 SD_TRACE(SD_LOG_IO_CORE, un,
14966                                     "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14967                                     pktp);
14968                                 goto got_pkt;
14969 
14970                         case SD_PKT_ALLOC_FAILURE:
14971                                 /*
14972                                  * Temporary (hopefully) resource depletion.
14973                                  * Since retries and RQS commands always have a
14974                                  * scsi_pkt allocated, these cases should never
14975                                  * get here. So the only cases this needs to
14976                                  * handle is a bp from the waitq (which we put
14977                                  * back onto the waitq for sdrunout), or a bp
14978                                  * sent as an immed_bp (which we just fail).
14979                                  */
14980                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14981                                     "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14982 
14983 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14984 
14985                                 if (bp == immed_bp) {
14986                                         /*
14987                                          * If SD_XB_DMA_FREED is clear, then
14988                                          * this is a failure to allocate a
14989                                          * scsi_pkt, and we must fail the
14990                                          * command.
14991                                          */
14992                                         if ((xp->xb_pkt_flags &
14993                                             SD_XB_DMA_FREED) == 0) {
14994                                                 break;
14995                                         }
14996 
14997                                         /*
14998                                          * If this immediate command is NOT our
14999                                          * un_retry_bp, then we must fail it.
15000                                          */
15001                                         if (bp != un->un_retry_bp) {
15002                                                 break;
15003                                         }
15004 
15005                                         /*
15006                                          * We get here if this cmd is our
15007                                          * un_retry_bp that was DMAFREED, but
15008                                          * scsi_init_pkt() failed to reallocate
15009                                          * DMA resources when we attempted to
15010                                          * retry it. This can happen when an
15011                                          * mpxio failover is in progress, but
15012                                          * we don't want to just fail the
15013                                          * command in this case.
15014                                          *
15015                                          * Use timeout(9F) to restart it after
15016                                          * a 100ms delay.  We don't want to
15017                                          * let sdrunout() restart it, because
15018                                          * sdrunout() is just supposed to start
15019                                          * commands that are sitting on the
15020                                          * wait queue.  The un_retry_bp stays
15021                                          * set until the command completes, but
15022                                          * sdrunout can be called many times
15023                                          * before that happens.  Since sdrunout
15024                                          * cannot tell if the un_retry_bp is
15025                                          * already in the transport, it could
15026                                          * end up calling scsi_transport() for
15027                                          * the un_retry_bp multiple times.
15028                                          *
15029                                          * Also: don't schedule the callback
15030                                          * if some other callback is already
15031                                          * pending.
15032                                          */
15033                                         if (un->un_retry_statp == NULL) {
15034                                                 /*
15035                                                  * restore the kstat pointer to
15036                                                  * keep kstat counts coherent
15037                                                  * when we do retry the command.
15038                                                  */
15039                                                 un->un_retry_statp =
15040                                                     saved_statp;
15041                                         }
15042 
15043                                         if ((un->un_startstop_timeid == NULL) &&
15044                                             (un->un_retry_timeid == NULL) &&
15045                                             (un->un_direct_priority_timeid ==
15046                                             NULL)) {
15047 
15048                                                 un->un_retry_timeid =
15049                                                     timeout(
15050                                                     sd_start_retry_command,
15051                                                     un, SD_RESTART_TIMEOUT);
15052                                         }
15053                                         goto exit;
15054                                 }
15055 
15056 #else
15057                                 if (bp == immed_bp) {
15058                                         break;  /* Just fail the command */
15059                                 }
15060 #endif
15061 
15062                                 /* Add the buf back to the head of the waitq */
15063                                 bp->av_forw = un->un_waitq_headp;
15064                                 un->un_waitq_headp = bp;
15065                                 if (un->un_waitq_tailp == NULL) {
15066                                         un->un_waitq_tailp = bp;
15067                                 }
15068                                 goto exit;
15069 
15070                         case SD_PKT_ALLOC_FAILURE_NO_DMA:
15071                                 /*
15072                                  * HBA DMA resource failure. Fail the command
15073                                  * and continue processing of the queues.
15074                                  */
15075                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15076                                     "sd_start_cmds: "
15077                                     "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15078                                 break;
15079 
15080                         case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15081                                 /*
15082                                  * Note:x86: Partial DMA mapping not supported
15083                                  * for USCSI commands, and all the needed DMA
15084                                  * resources were not allocated.
15085                                  */
15086                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15087                                     "sd_start_cmds: "
15088                                     "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15089                                 break;
15090 
15091                         case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15092                                 /*
15093                                  * Note:x86: Request cannot fit into CDB based
15094                                  * on lba and len.
15095                                  */
15096                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15097                                     "sd_start_cmds: "
15098                                     "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15099                                 break;
15100 
15101                         default:
15102                                 /* Should NEVER get here! */
15103                                 panic("scsi_initpkt error");
15104                                 /*NOTREACHED*/
15105                         }
15106 
15107                         /*
15108                          * Fatal error in allocating a scsi_pkt for this buf.
15109                          * Update kstats & return the buf with an error code.
15110                          * We must use sd_return_failed_command_no_restart() to
15111                          * avoid a recursive call back into sd_start_cmds().
15112                          * However this also means that we must keep processing
15113                          * the waitq here in order to avoid stalling.
15114                          */
15115                         if (statp == kstat_waitq_to_runq) {
15116                                 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15117                         }
15118                         sd_return_failed_command_no_restart(un, bp, EIO);
15119                         if (bp == immed_bp) {
15120                                 /* immed_bp is gone by now, so clear this */
15121                                 immed_bp = NULL;
15122                         }
15123                         continue;
15124                 }
15125 got_pkt:
15126                 if (bp == immed_bp) {
15127                         /* goto the head of the class.... */
15128                         xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15129                 }
15130 
15131                 un->un_ncmds_in_transport++;
15132                 SD_UPDATE_KSTATS(un, statp, bp);
15133 
15134                 /*
15135                  * Call scsi_transport() to send the command to the target.
15136                  * According to SCSA architecture, we must drop the mutex here
15137                  * before calling scsi_transport() in order to avoid deadlock.
15138                  * Note that the scsi_pkt's completion routine can be executed
15139                  * (from interrupt context) even before the call to
15140                  * scsi_transport() returns.
15141                  */
15142                 SD_TRACE(SD_LOG_IO_CORE, un,
15143                     "sd_start_cmds: calling scsi_transport()\n");
15144                 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15145 
15146                 mutex_exit(SD_MUTEX(un));
15147                 rval = scsi_transport(xp->xb_pktp);
15148                 mutex_enter(SD_MUTEX(un));
15149 
15150                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15151                     "sd_start_cmds: scsi_transport() returned %d\n", rval);
15152 
15153                 switch (rval) {
15154                 case TRAN_ACCEPT:
15155                         /* Clear this with every pkt accepted by the HBA */
15156                         un->un_tran_fatal_count = 0;
15157                         break;  /* Success; try the next cmd (if any) */
15158 
15159                 case TRAN_BUSY:
15160                         un->un_ncmds_in_transport--;
15161                         ASSERT(un->un_ncmds_in_transport >= 0);
15162 
15163                         /*
15164                          * Don't retry request sense, the sense data
15165                          * is lost when another request is sent.
15166                          * Free up the rqs buf and retry
15167                          * the original failed cmd.  Update kstat.
15168                          */
15169                         if (bp == un->un_rqs_bp) {
15170                                 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15171                                 bp = sd_mark_rqs_idle(un, xp);
15172                                 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15173                                     NULL, NULL, EIO, un->un_busy_timeout / 500,
15174                                     kstat_waitq_enter);
15175                                 goto exit;
15176                         }
15177 
15178 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15179                         /*
15180                          * Free the DMA resources for the  scsi_pkt. This will
15181                          * allow mpxio to select another path the next time
15182                          * we call scsi_transport() with this scsi_pkt.
15183                          * See sdintr() for the rationalization behind this.
15184                          */
15185                         if ((un->un_f_is_fibre == TRUE) &&
15186                             ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15187                             ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15188                                 scsi_dmafree(xp->xb_pktp);
15189                                 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15190                         }
15191 #endif
15192 
15193                         if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15194                                 /*
15195                                  * Commands that are SD_PATH_DIRECT_PRIORITY
15196                                  * are for error recovery situations. These do
15197                                  * not use the normal command waitq, so if they
15198                                  * get a TRAN_BUSY we cannot put them back onto
15199                                  * the waitq for later retry. One possible
15200                                  * problem is that there could already be some
15201                                  * other command on un_retry_bp that is waiting
15202                                  * for this one to complete, so we would be
15203                                  * deadlocked if we put this command back onto
15204                                  * the waitq for later retry (since un_retry_bp
15205                                  * must complete before the driver gets back to
15206                                  * commands on the waitq).
15207                                  *
15208                                  * To avoid deadlock we must schedule a callback
15209                                  * that will restart this command after a set
15210                                  * interval.  This should keep retrying for as
15211                                  * long as the underlying transport keeps
15212                                  * returning TRAN_BUSY (just like for other
15213                                  * commands).  Use the same timeout interval as
15214                                  * for the ordinary TRAN_BUSY retry.
15215                                  */
15216                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15217                                     "sd_start_cmds: scsi_transport() returned "
15218                                     "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15219 
15220                                 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15221                                 un->un_direct_priority_timeid =
15222                                     timeout(sd_start_direct_priority_command,
15223                                     bp, un->un_busy_timeout / 500);
15224 
15225                                 goto exit;
15226                         }
15227 
15228                         /*
15229                          * For TRAN_BUSY, we want to reduce the throttle value,
15230                          * unless we are retrying a command.
15231                          */
15232                         if (bp != un->un_retry_bp) {
15233                                 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15234                         }
15235 
15236                         /*
15237                          * Set up the bp to be tried again 10 ms later.
15238                          * Note:x86: Is there a timeout value in the sd_lun
15239                          * for this condition?
15240                          */
15241                         sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15242                             kstat_runq_back_to_waitq);
15243                         goto exit;
15244 
15245                 case TRAN_FATAL_ERROR:
15246                         un->un_tran_fatal_count++;
15247                         /* FALLTHRU */
15248 
15249                 case TRAN_BADPKT:
15250                 default:
15251                         un->un_ncmds_in_transport--;
15252                         ASSERT(un->un_ncmds_in_transport >= 0);
15253 
15254                         /*
15255                          * If this is our REQUEST SENSE command with a
15256                          * transport error, we must get back the pointers
15257                          * to the original buf, and mark the REQUEST
15258                          * SENSE command as "available".
15259                          */
15260                         if (bp == un->un_rqs_bp) {
15261                                 bp = sd_mark_rqs_idle(un, xp);
15262                                 xp = SD_GET_XBUF(bp);
15263                         } else {
15264                                 /*
15265                                  * Legacy behavior: do not update transport
15266                                  * error count for request sense commands.
15267                                  */
15268                                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
15269                         }
15270 
15271                         SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15272                         sd_print_transport_rejected_message(un, xp, rval);
15273 
15274                         /*
15275                          * This command will be terminated by SD driver due
15276                          * to a fatal transport error. We should post
15277                          * ereport.io.scsi.cmd.disk.tran with driver-assessment
15278                          * of "fail" for any command to indicate this
15279                          * situation.
15280                          */
15281                         if (xp->xb_ena > 0) {
15282                                 ASSERT(un->un_fm_private != NULL);
15283                                 sfip = un->un_fm_private;
15284                                 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15285                                 sd_ssc_extract_info(&sfip->fm_ssc, un,
15286                                     xp->xb_pktp, bp, xp);
15287                                 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15288                         }
15289 
15290                         /*
15291                          * We must use sd_return_failed_command_no_restart() to
15292                          * avoid a recursive call back into sd_start_cmds().
15293                          * However this also means that we must keep processing
15294                          * the waitq here in order to avoid stalling.
15295                          */
15296                         sd_return_failed_command_no_restart(un, bp, EIO);
15297 
15298                         /*
15299                          * Notify any threads waiting in sd_ddi_suspend() that
15300                          * a command completion has occurred.
15301                          */
15302                         if (un->un_state == SD_STATE_SUSPENDED) {
15303                                 cv_broadcast(&un->un_disk_busy_cv);
15304                         }
15305 
15306                         if (bp == immed_bp) {
15307                                 /* immed_bp is gone by now, so clear this */
15308                                 immed_bp = NULL;
15309                         }
15310                         break;
15311                 }
15312 
15313         } while (immed_bp == NULL);
15314 
15315 exit:
15316         ASSERT(mutex_owned(SD_MUTEX(un)));
15317         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15318 }
15319 
15320 
15321 /*
15322  *    Function: sd_return_command
15323  *
15324  * Description: Returns a command to its originator (with or without an
15325  *              error).  Also starts commands waiting to be transported
15326  *              to the target.
15327  *
15328  *     Context: May be called from interrupt, kernel, or timeout context
15329  */
15330 
15331 static void
15332 sd_return_command(struct sd_lun *un, struct buf *bp)
15333 {
15334         struct sd_xbuf *xp;
15335         struct scsi_pkt *pktp;
15336         struct sd_fm_internal *sfip;
15337 
15338         ASSERT(bp != NULL);
15339         ASSERT(un != NULL);
15340         ASSERT(mutex_owned(SD_MUTEX(un)));
15341         ASSERT(bp != un->un_rqs_bp);
15342         xp = SD_GET_XBUF(bp);
15343         ASSERT(xp != NULL);
15344 
15345         pktp = SD_GET_PKTP(bp);
15346         sfip = (struct sd_fm_internal *)un->un_fm_private;
15347         ASSERT(sfip != NULL);
15348 
15349         SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15350 
15351         /*
15352          * Note: check for the "sdrestart failed" case.
15353          */
15354         if ((un->un_partial_dma_supported == 1) &&
15355             ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15356             (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15357             (xp->xb_pktp->pkt_resid == 0)) {
15358 
15359                 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15360                         /*
15361                          * Successfully set up next portion of cmd
15362                          * transfer, try sending it
15363                          */
15364                         sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15365                             NULL, NULL, 0, (clock_t)0, NULL);
15366                         sd_start_cmds(un, NULL);
15367                         return; /* Note:x86: need a return here? */
15368                 }
15369         }
15370 
15371         /*
15372          * If this is the failfast bp, clear it from un_failfast_bp. This
15373          * can happen if upon being re-tried the failfast bp either
15374          * succeeded or encountered another error (possibly even a different
15375          * error than the one that precipitated the failfast state, but in
15376          * that case it would have had to exhaust retries as well). Regardless,
15377          * this should not occur whenever the instance is in the active
15378          * failfast state.
15379          */
15380         if (bp == un->un_failfast_bp) {
15381                 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15382                 un->un_failfast_bp = NULL;
15383         }
15384 
15385         /*
15386          * Clear the failfast state upon successful completion of ANY cmd.
15387          */
15388         if (bp->b_error == 0) {
15389                 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15390                 /*
15391                  * If this is a successful command, but used to be retried,
15392                  * we will take it as a recovered command and post an
15393                  * ereport with driver-assessment of "recovered".
15394                  */
15395                 if (xp->xb_ena > 0) {
15396                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15397                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15398                 }
15399         } else {
15400                 /*
15401                  * If this is a failed non-USCSI command we will post an
15402                  * ereport with driver-assessment set accordingly("fail" or
15403                  * "fatal").
15404                  */
15405                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15406                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15407                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15408                 }
15409         }
15410 
15411         /*
15412          * This is used if the command was retried one or more times. Show that
15413          * we are done with it, and allow processing of the waitq to resume.
15414          */
15415         if (bp == un->un_retry_bp) {
15416                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15417                     "sd_return_command: un:0x%p: "
15418                     "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15419                 un->un_retry_bp = NULL;
15420                 un->un_retry_statp = NULL;
15421         }
15422 
15423         SD_UPDATE_RDWR_STATS(un, bp);
15424         SD_UPDATE_PARTITION_STATS(un, bp);
15425 
15426         switch (un->un_state) {
15427         case SD_STATE_SUSPENDED:
15428                 /*
15429                  * Notify any threads waiting in sd_ddi_suspend() that
15430                  * a command completion has occurred.
15431                  */
15432                 cv_broadcast(&un->un_disk_busy_cv);
15433                 break;
15434         default:
15435                 sd_start_cmds(un, NULL);
15436                 break;
15437         }
15438 
15439         /* Return this command up the iodone chain to its originator. */
15440         mutex_exit(SD_MUTEX(un));
15441 
15442         (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15443         xp->xb_pktp = NULL;
15444 
15445         SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15446 
15447         ASSERT(!mutex_owned(SD_MUTEX(un)));
15448         mutex_enter(SD_MUTEX(un));
15449 
15450         SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15451 }
15452 
15453 
15454 /*
15455  *    Function: sd_return_failed_command
15456  *
15457  * Description: Command completion when an error occurred.
15458  *
15459  *     Context: May be called from interrupt context
15460  */
15461 
15462 static void
15463 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15464 {
15465         ASSERT(bp != NULL);
15466         ASSERT(un != NULL);
15467         ASSERT(mutex_owned(SD_MUTEX(un)));
15468 
15469         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15470             "sd_return_failed_command: entry\n");
15471 
15472         /*
15473          * b_resid could already be nonzero due to a partial data
15474          * transfer, so do not change it here.
15475          */
15476         SD_BIOERROR(bp, errcode);
15477 
15478         sd_return_command(un, bp);
15479         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15480             "sd_return_failed_command: exit\n");
15481 }
15482 
15483 
15484 /*
15485  *    Function: sd_return_failed_command_no_restart
15486  *
15487  * Description: Same as sd_return_failed_command, but ensures that no
15488  *              call back into sd_start_cmds will be issued.
15489  *
15490  *     Context: May be called from interrupt context
15491  */
15492 
15493 static void
15494 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15495     int errcode)
15496 {
15497         struct sd_xbuf *xp;
15498 
15499         ASSERT(bp != NULL);
15500         ASSERT(un != NULL);
15501         ASSERT(mutex_owned(SD_MUTEX(un)));
15502         xp = SD_GET_XBUF(bp);
15503         ASSERT(xp != NULL);
15504         ASSERT(errcode != 0);
15505 
15506         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15507             "sd_return_failed_command_no_restart: entry\n");
15508 
15509         /*
15510          * b_resid could already be nonzero due to a partial data
15511          * transfer, so do not change it here.
15512          */
15513         SD_BIOERROR(bp, errcode);
15514 
15515         /*
15516          * If this is the failfast bp, clear it. This can happen if the
15517          * failfast bp encounterd a fatal error when we attempted to
15518          * re-try it (such as a scsi_transport(9F) failure).  However
15519          * we should NOT be in an active failfast state if the failfast
15520          * bp is not NULL.
15521          */
15522         if (bp == un->un_failfast_bp) {
15523                 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15524                 un->un_failfast_bp = NULL;
15525         }
15526 
15527         if (bp == un->un_retry_bp) {
15528                 /*
15529                  * This command was retried one or more times. Show that we are
15530                  * done with it, and allow processing of the waitq to resume.
15531                  */
15532                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15533                     "sd_return_failed_command_no_restart: "
15534                     " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15535                 un->un_retry_bp = NULL;
15536                 un->un_retry_statp = NULL;
15537         }
15538 
15539         SD_UPDATE_RDWR_STATS(un, bp);
15540         SD_UPDATE_PARTITION_STATS(un, bp);
15541 
15542         mutex_exit(SD_MUTEX(un));
15543 
15544         if (xp->xb_pktp != NULL) {
15545                 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15546                 xp->xb_pktp = NULL;
15547         }
15548 
15549         SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15550 
15551         mutex_enter(SD_MUTEX(un));
15552 
15553         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15554             "sd_return_failed_command_no_restart: exit\n");
15555 }
15556 
15557 
15558 /*
15559  *    Function: sd_retry_command
15560  *
15561  * Description: queue up a command for retry, or (optionally) fail it
15562  *              if retry counts are exhausted.
15563  *
15564  *   Arguments: un - Pointer to the sd_lun struct for the target.
15565  *
15566  *              bp - Pointer to the buf for the command to be retried.
15567  *
15568  *              retry_check_flag - Flag to see which (if any) of the retry
15569  *                 counts should be decremented/checked. If the indicated
15570  *                 retry count is exhausted, then the command will not be
15571  *                 retried; it will be failed instead. This should use a
15572  *                 value equal to one of the following:
15573  *
15574  *                      SD_RETRIES_NOCHECK
15575  *                      SD_RESD_RETRIES_STANDARD
15576  *                      SD_RETRIES_VICTIM
15577  *
15578  *                 Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15579  *                 if the check should be made to see of FLAG_ISOLATE is set
15580  *                 in the pkt. If FLAG_ISOLATE is set, then the command is
15581  *                 not retried, it is simply failed.
15582  *
15583  *              user_funcp - Ptr to function to call before dispatching the
15584  *                 command. May be NULL if no action needs to be performed.
15585  *                 (Primarily intended for printing messages.)
15586  *
15587  *              user_arg - Optional argument to be passed along to
15588  *                 the user_funcp call.
15589  *
15590  *              failure_code - errno return code to set in the bp if the
15591  *                 command is going to be failed.
15592  *
15593  *              retry_delay - Retry delay interval in (clock_t) units. May
15594  *                 be zero which indicates that the retry should be retried
15595  *                 immediately (ie, without an intervening delay).
15596  *
15597  *              statp - Ptr to kstat function to be updated if the command
15598  *                 is queued for a delayed retry. May be NULL if no kstat
15599  *                 update is desired.
15600  *
15601  *     Context: May be called from interrupt context.
15602  */
15603 
15604 static void
15605 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15606     void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int code),
15607     void *user_arg, int failure_code, clock_t retry_delay,
15608     void (*statp)(kstat_io_t *))
15609 {
15610         struct sd_xbuf  *xp;
15611         struct scsi_pkt *pktp;
15612         struct sd_fm_internal *sfip;
15613 
15614         ASSERT(un != NULL);
15615         ASSERT(mutex_owned(SD_MUTEX(un)));
15616         ASSERT(bp != NULL);
15617         xp = SD_GET_XBUF(bp);
15618         ASSERT(xp != NULL);
15619         pktp = SD_GET_PKTP(bp);
15620         ASSERT(pktp != NULL);
15621 
15622         sfip = (struct sd_fm_internal *)un->un_fm_private;
15623         ASSERT(sfip != NULL);
15624 
15625         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15626             "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15627 
15628         /*
15629          * If we are syncing or dumping, fail the command to avoid
15630          * recursively calling back into scsi_transport().
15631          */
15632         if (ddi_in_panic()) {
15633                 goto fail_command_no_log;
15634         }
15635 
15636         /*
15637          * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15638          * log an error and fail the command.
15639          */
15640         if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15641                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15642                     "ERROR, retrying FLAG_DIAGNOSE command.\n");
15643                 sd_dump_memory(un, SD_LOG_IO, "CDB",
15644                     (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15645                 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15646                     (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15647                 goto fail_command;
15648         }
15649 
15650         /*
15651          * If we are suspended, then put the command onto head of the
15652          * wait queue since we don't want to start more commands, and
15653          * clear the un_retry_bp. Next time when we are resumed, will
15654          * handle the command in the wait queue.
15655          */
15656         switch (un->un_state) {
15657         case SD_STATE_SUSPENDED:
15658         case SD_STATE_DUMPING:
15659                 bp->av_forw = un->un_waitq_headp;
15660                 un->un_waitq_headp = bp;
15661                 if (un->un_waitq_tailp == NULL) {
15662                         un->un_waitq_tailp = bp;
15663                 }
15664                 if (bp == un->un_retry_bp) {
15665                         un->un_retry_bp = NULL;
15666                         un->un_retry_statp = NULL;
15667                 }
15668                 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15669                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15670                     "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15671                 return;
15672         default:
15673                 break;
15674         }
15675 
15676         /*
15677          * If the caller wants us to check FLAG_ISOLATE, then see if that
15678          * is set; if it is then we do not want to retry the command.
15679          * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15680          */
15681         if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15682                 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15683                         goto fail_command;
15684                 }
15685         }
15686 
15687 
15688         /*
15689          * If SD_RETRIES_FAILFAST is set, it indicates that either a
15690          * command timeout or a selection timeout has occurred. This means
15691          * that we were unable to establish an kind of communication with
15692          * the target, and subsequent retries and/or commands are likely
15693          * to encounter similar results and take a long time to complete.
15694          *
15695          * If this is a failfast error condition, we need to update the
15696          * failfast state, even if this bp does not have B_FAILFAST set.
15697          */
15698         if (retry_check_flag & SD_RETRIES_FAILFAST) {
15699                 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15700                         ASSERT(un->un_failfast_bp == NULL);
15701                         /*
15702                          * If we are already in the active failfast state, and
15703                          * another failfast error condition has been detected,
15704                          * then fail this command if it has B_FAILFAST set.
15705                          * If B_FAILFAST is clear, then maintain the legacy
15706                          * behavior of retrying heroically, even tho this will
15707                          * take a lot more time to fail the command.
15708                          */
15709                         if (bp->b_flags & B_FAILFAST) {
15710                                 goto fail_command;
15711                         }
15712                 } else {
15713                         /*
15714                          * We're not in the active failfast state, but we
15715                          * have a failfast error condition, so we must begin
15716                          * transition to the next state. We do this regardless
15717                          * of whether or not this bp has B_FAILFAST set.
15718                          */
15719                         if (un->un_failfast_bp == NULL) {
15720                                 /*
15721                                  * This is the first bp to meet a failfast
15722                                  * condition so save it on un_failfast_bp &
15723                                  * do normal retry processing. Do not enter
15724                                  * active failfast state yet. This marks
15725                                  * entry into the "failfast pending" state.
15726                                  */
15727                                 un->un_failfast_bp = bp;
15728 
15729                         } else if (un->un_failfast_bp == bp) {
15730                                 /*
15731                                  * This is the second time *this* bp has
15732                                  * encountered a failfast error condition,
15733                                  * so enter active failfast state & flush
15734                                  * queues as appropriate.
15735                                  */
15736                                 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15737                                 un->un_failfast_bp = NULL;
15738                                 sd_failfast_flushq(un);
15739 
15740                                 /*
15741                                  * Fail this bp now if B_FAILFAST set;
15742                                  * otherwise continue with retries. (It would
15743                                  * be pretty ironic if this bp succeeded on a
15744                                  * subsequent retry after we just flushed all
15745                                  * the queues).
15746                                  */
15747                                 if (bp->b_flags & B_FAILFAST) {
15748                                         goto fail_command;
15749                                 }
15750 
15751 #if !defined(lint) && !defined(__lint)
15752                         } else {
15753                                 /*
15754                                  * If neither of the preceeding conditionals
15755                                  * was true, it means that there is some
15756                                  * *other* bp that has met an inital failfast
15757                                  * condition and is currently either being
15758                                  * retried or is waiting to be retried. In
15759                                  * that case we should perform normal retry
15760                                  * processing on *this* bp, since there is a
15761                                  * chance that the current failfast condition
15762                                  * is transient and recoverable. If that does
15763                                  * not turn out to be the case, then retries
15764                                  * will be cleared when the wait queue is
15765                                  * flushed anyway.
15766                                  */
15767 #endif
15768                         }
15769                 }
15770         } else {
15771                 /*
15772                  * SD_RETRIES_FAILFAST is clear, which indicates that we
15773                  * likely were able to at least establish some level of
15774                  * communication with the target and subsequent commands
15775                  * and/or retries are likely to get through to the target,
15776                  * In this case we want to be aggressive about clearing
15777                  * the failfast state. Note that this does not affect
15778                  * the "failfast pending" condition.
15779                  */
15780                 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15781         }
15782 
15783 
15784         /*
15785          * Check the specified retry count to see if we can still do
15786          * any retries with this pkt before we should fail it.
15787          */
15788         switch (retry_check_flag & SD_RETRIES_MASK) {
15789         case SD_RETRIES_VICTIM:
15790                 /*
15791                  * Check the victim retry count. If exhausted, then fall
15792                  * thru & check against the standard retry count.
15793                  */
15794                 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15795                         /* Increment count & proceed with the retry */
15796                         xp->xb_victim_retry_count++;
15797                         break;
15798                 }
15799                 /* Victim retries exhausted, fall back to std. retries... */
15800                 /* FALLTHRU */
15801 
15802         case SD_RETRIES_STANDARD:
15803                 if (xp->xb_retry_count >= un->un_retry_count) {
15804                         /* Retries exhausted, fail the command */
15805                         SD_TRACE(SD_LOG_IO_CORE, un,
15806                             "sd_retry_command: retries exhausted!\n");
15807                         /*
15808                          * update b_resid for failed SCMD_READ & SCMD_WRITE
15809                          * commands with nonzero pkt_resid.
15810                          */
15811                         if ((pktp->pkt_reason == CMD_CMPLT) &&
15812                             (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15813                             (pktp->pkt_resid != 0)) {
15814                                 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15815                                 if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15816                                         SD_UPDATE_B_RESID(bp, pktp);
15817                                 }
15818                         }
15819                         goto fail_command;
15820                 }
15821                 xp->xb_retry_count++;
15822                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15823                     "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15824                 break;
15825 
15826         case SD_RETRIES_UA:
15827                 if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15828                         /* Retries exhausted, fail the command */
15829                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15830                             "Unit Attention retries exhausted. "
15831                             "Check the target.\n");
15832                         goto fail_command;
15833                 }
15834                 xp->xb_ua_retry_count++;
15835                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15836                     "sd_retry_command: retry count:%d\n",
15837                     xp->xb_ua_retry_count);
15838                 break;
15839 
15840         case SD_RETRIES_BUSY:
15841                 if (xp->xb_retry_count >= un->un_busy_retry_count) {
15842                         /* Retries exhausted, fail the command */
15843                         SD_TRACE(SD_LOG_IO_CORE, un,
15844                             "sd_retry_command: retries exhausted!\n");
15845                         goto fail_command;
15846                 }
15847                 xp->xb_retry_count++;
15848                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15849                     "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15850                 break;
15851 
15852         case SD_RETRIES_NOCHECK:
15853         default:
15854                 /* No retry count to check. Just proceed with the retry */
15855                 break;
15856         }
15857 
15858         xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15859 
15860         /*
15861          * If this is a non-USCSI command being retried
15862          * during execution last time, we should post an ereport with
15863          * driver-assessment of the value "retry".
15864          * For partial DMA, request sense and STATUS_QFULL, there are no
15865          * hardware errors, we bypass ereport posting.
15866          */
15867         if (failure_code != 0) {
15868                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15869                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15870                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15871                 }
15872         }
15873 
15874         /*
15875          * If we were given a zero timeout, we must attempt to retry the
15876          * command immediately (ie, without a delay).
15877          */
15878         if (retry_delay == 0) {
15879                 /*
15880                  * Check some limiting conditions to see if we can actually
15881                  * do the immediate retry.  If we cannot, then we must
15882                  * fall back to queueing up a delayed retry.
15883                  */
15884                 if (un->un_ncmds_in_transport >= un->un_throttle) {
15885                         /*
15886                          * We are at the throttle limit for the target,
15887                          * fall back to delayed retry.
15888                          */
15889                         retry_delay = un->un_busy_timeout;
15890                         statp = kstat_waitq_enter;
15891                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15892                             "sd_retry_command: immed. retry hit "
15893                             "throttle!\n");
15894                 } else {
15895                         /*
15896                          * We're clear to proceed with the immediate retry.
15897                          * First call the user-provided function (if any)
15898                          */
15899                         if (user_funcp != NULL) {
15900                                 (*user_funcp)(un, bp, user_arg,
15901                                     SD_IMMEDIATE_RETRY_ISSUED);
15902 #ifdef __lock_lint
15903                                 sd_print_incomplete_msg(un, bp, user_arg,
15904                                     SD_IMMEDIATE_RETRY_ISSUED);
15905                                 sd_print_cmd_incomplete_msg(un, bp, user_arg,
15906                                     SD_IMMEDIATE_RETRY_ISSUED);
15907                                 sd_print_sense_failed_msg(un, bp, user_arg,
15908                                     SD_IMMEDIATE_RETRY_ISSUED);
15909 #endif
15910                         }
15911 
15912                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15913                             "sd_retry_command: issuing immediate retry\n");
15914 
15915                         /*
15916                          * Call sd_start_cmds() to transport the command to
15917                          * the target.
15918                          */
15919                         sd_start_cmds(un, bp);
15920 
15921                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15922                             "sd_retry_command exit\n");
15923                         return;
15924                 }
15925         }
15926 
15927         /*
15928          * Set up to retry the command after a delay.
15929          * First call the user-provided function (if any)
15930          */
15931         if (user_funcp != NULL) {
15932                 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15933         }
15934 
15935         sd_set_retry_bp(un, bp, retry_delay, statp);
15936 
15937         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15938         return;
15939 
15940 fail_command:
15941 
15942         if (user_funcp != NULL) {
15943                 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15944         }
15945 
15946 fail_command_no_log:
15947 
15948         SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15949             "sd_retry_command: returning failed command\n");
15950 
15951         sd_return_failed_command(un, bp, failure_code);
15952 
15953         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15954 }
15955 
15956 
15957 /*
15958  *    Function: sd_set_retry_bp
15959  *
15960  * Description: Set up the given bp for retry.
15961  *
15962  *   Arguments: un - ptr to associated softstate
15963  *              bp - ptr to buf(9S) for the command
15964  *              retry_delay - time interval before issuing retry (may be 0)
15965  *              statp - optional pointer to kstat function
15966  *
15967  *     Context: May be called under interrupt context
15968  */
15969 
15970 static void
15971 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15972     void (*statp)(kstat_io_t *))
15973 {
15974         ASSERT(un != NULL);
15975         ASSERT(mutex_owned(SD_MUTEX(un)));
15976         ASSERT(bp != NULL);
15977 
15978         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15979             "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15980 
15981         /*
15982          * Indicate that the command is being retried. This will not allow any
15983          * other commands on the wait queue to be transported to the target
15984          * until this command has been completed (success or failure). The
15985          * "retry command" is not transported to the target until the given
15986          * time delay expires, unless the user specified a 0 retry_delay.
15987          *
15988          * Note: the timeout(9F) callback routine is what actually calls
15989          * sd_start_cmds() to transport the command, with the exception of a
15990          * zero retry_delay. The only current implementor of a zero retry delay
15991          * is the case where a START_STOP_UNIT is sent to spin-up a device.
15992          */
15993         if (un->un_retry_bp == NULL) {
15994                 ASSERT(un->un_retry_statp == NULL);
15995                 un->un_retry_bp = bp;
15996 
15997                 /*
15998                  * If the user has not specified a delay the command should
15999                  * be queued and no timeout should be scheduled.
16000                  */
16001                 if (retry_delay == 0) {
16002                         /*
16003                          * Save the kstat pointer that will be used in the
16004                          * call to SD_UPDATE_KSTATS() below, so that
16005                          * sd_start_cmds() can correctly decrement the waitq
16006                          * count when it is time to transport this command.
16007                          */
16008                         un->un_retry_statp = statp;
16009                         goto done;
16010                 }
16011         }
16012 
16013         if (un->un_retry_bp == bp) {
16014                 /*
16015                  * Save the kstat pointer that will be used in the call to
16016                  * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16017                  * correctly decrement the waitq count when it is time to
16018                  * transport this command.
16019                  */
16020                 un->un_retry_statp = statp;
16021 
16022                 /*
16023                  * Schedule a timeout if:
16024                  *   1) The user has specified a delay.
16025                  *   2) There is not a START_STOP_UNIT callback pending.
16026                  *
16027                  * If no delay has been specified, then it is up to the caller
16028                  * to ensure that IO processing continues without stalling.
16029                  * Effectively, this means that the caller will issue the
16030                  * required call to sd_start_cmds(). The START_STOP_UNIT
16031                  * callback does this after the START STOP UNIT command has
16032                  * completed. In either of these cases we should not schedule
16033                  * a timeout callback here.  Also don't schedule the timeout if
16034                  * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16035                  */
16036                 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16037                     (un->un_direct_priority_timeid == NULL)) {
16038                         un->un_retry_timeid =
16039                             timeout(sd_start_retry_command, un, retry_delay);
16040                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16041                             "sd_set_retry_bp: setting timeout: un: 0x%p"
16042                             " bp:0x%p un_retry_timeid:0x%p\n",
16043                             un, bp, un->un_retry_timeid);
16044                 }
16045         } else {
16046                 /*
16047                  * We only get in here if there is already another command
16048                  * waiting to be retried.  In this case, we just put the
16049                  * given command onto the wait queue, so it can be transported
16050                  * after the current retry command has completed.
16051                  *
16052                  * Also we have to make sure that if the command at the head
16053                  * of the wait queue is the un_failfast_bp, that we do not
16054                  * put ahead of it any other commands that are to be retried.
16055                  */
16056                 if ((un->un_failfast_bp != NULL) &&
16057                     (un->un_failfast_bp == un->un_waitq_headp)) {
16058                         /*
16059                          * Enqueue this command AFTER the first command on
16060                          * the wait queue (which is also un_failfast_bp).
16061                          */
16062                         bp->av_forw = un->un_waitq_headp->av_forw;
16063                         un->un_waitq_headp->av_forw = bp;
16064                         if (un->un_waitq_headp == un->un_waitq_tailp) {
16065                                 un->un_waitq_tailp = bp;
16066                         }
16067                 } else {
16068                         /* Enqueue this command at the head of the waitq. */
16069                         bp->av_forw = un->un_waitq_headp;
16070                         un->un_waitq_headp = bp;
16071                         if (un->un_waitq_tailp == NULL) {
16072                                 un->un_waitq_tailp = bp;
16073                         }
16074                 }
16075 
16076                 if (statp == NULL) {
16077                         statp = kstat_waitq_enter;
16078                 }
16079                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16080                     "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16081         }
16082 
16083 done:
16084         if (statp != NULL) {
16085                 SD_UPDATE_KSTATS(un, statp, bp);
16086         }
16087 
16088         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16089             "sd_set_retry_bp: exit un:0x%p\n", un);
16090 }
16091 
16092 
16093 /*
16094  *    Function: sd_start_retry_command
16095  *
16096  * Description: Start the command that has been waiting on the target's
16097  *              retry queue.  Called from timeout(9F) context after the
16098  *              retry delay interval has expired.
16099  *
16100  *   Arguments: arg - pointer to associated softstate for the device.
16101  *
16102  *     Context: timeout(9F) thread context.  May not sleep.
16103  */
16104 
16105 static void
16106 sd_start_retry_command(void *arg)
16107 {
16108         struct sd_lun *un = arg;
16109 
16110         ASSERT(un != NULL);
16111         ASSERT(!mutex_owned(SD_MUTEX(un)));
16112 
16113         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16114             "sd_start_retry_command: entry\n");
16115 
16116         mutex_enter(SD_MUTEX(un));
16117 
16118         un->un_retry_timeid = NULL;
16119 
16120         if (un->un_retry_bp != NULL) {
16121                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16122                     "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16123                     un, un->un_retry_bp);
16124                 sd_start_cmds(un, un->un_retry_bp);
16125         }
16126 
16127         mutex_exit(SD_MUTEX(un));
16128 
16129         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16130             "sd_start_retry_command: exit\n");
16131 }
16132 
16133 /*
16134  *    Function: sd_rmw_msg_print_handler
16135  *
16136  * Description: If RMW mode is enabled and warning message is triggered
16137  *              print I/O count during a fixed interval.
16138  *
16139  *   Arguments: arg - pointer to associated softstate for the device.
16140  *
16141  *     Context: timeout(9F) thread context. May not sleep.
16142  */
16143 static void
16144 sd_rmw_msg_print_handler(void *arg)
16145 {
16146         struct sd_lun *un = arg;
16147 
16148         ASSERT(un != NULL);
16149         ASSERT(!mutex_owned(SD_MUTEX(un)));
16150 
16151         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16152             "sd_rmw_msg_print_handler: entry\n");
16153 
16154         mutex_enter(SD_MUTEX(un));
16155 
16156         if (un->un_rmw_incre_count > 0) {
16157                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16158                     "%"PRIu64" I/O requests are not aligned with %d disk "
16159                     "sector size in %ld seconds. They are handled through "
16160                     "Read Modify Write but the performance is very low!\n",
16161                     un->un_rmw_incre_count, un->un_tgt_blocksize,
16162                     drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16163                 un->un_rmw_incre_count = 0;
16164                 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16165                     un, SD_RMW_MSG_PRINT_TIMEOUT);
16166         } else {
16167                 un->un_rmw_msg_timeid = NULL;
16168         }
16169 
16170         mutex_exit(SD_MUTEX(un));
16171 
16172         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16173             "sd_rmw_msg_print_handler: exit\n");
16174 }
16175 
16176 /*
16177  *    Function: sd_start_direct_priority_command
16178  *
16179  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16180  *              received TRAN_BUSY when we called scsi_transport() to send it
16181  *              to the underlying HBA. This function is called from timeout(9F)
16182  *              context after the delay interval has expired.
16183  *
16184  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16185  *
16186  *     Context: timeout(9F) thread context.  May not sleep.
16187  */
16188 
16189 static void
16190 sd_start_direct_priority_command(void *arg)
16191 {
16192         struct buf      *priority_bp = arg;
16193         struct sd_lun   *un;
16194 
16195         ASSERT(priority_bp != NULL);
16196         un = SD_GET_UN(priority_bp);
16197         ASSERT(un != NULL);
16198         ASSERT(!mutex_owned(SD_MUTEX(un)));
16199 
16200         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16201             "sd_start_direct_priority_command: entry\n");
16202 
16203         mutex_enter(SD_MUTEX(un));
16204         un->un_direct_priority_timeid = NULL;
16205         sd_start_cmds(un, priority_bp);
16206         mutex_exit(SD_MUTEX(un));
16207 
16208         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16209             "sd_start_direct_priority_command: exit\n");
16210 }
16211 
16212 
16213 /*
16214  *    Function: sd_send_request_sense_command
16215  *
16216  * Description: Sends a REQUEST SENSE command to the target
16217  *
16218  *     Context: May be called from interrupt context.
16219  */
16220 
16221 static void
16222 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16223     struct scsi_pkt *pktp)
16224 {
16225         ASSERT(bp != NULL);
16226         ASSERT(un != NULL);
16227         ASSERT(mutex_owned(SD_MUTEX(un)));
16228 
16229         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16230             "entry: buf:0x%p\n", bp);
16231 
16232         /*
16233          * If we are syncing or dumping, then fail the command to avoid a
16234          * recursive callback into scsi_transport(). Also fail the command
16235          * if we are suspended (legacy behavior).
16236          */
16237         if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16238             (un->un_state == SD_STATE_DUMPING)) {
16239                 sd_return_failed_command(un, bp, EIO);
16240                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16241                     "sd_send_request_sense_command: syncing/dumping, exit\n");
16242                 return;
16243         }
16244 
16245         /*
16246          * Retry the failed command and don't issue the request sense if:
16247          *    1) the sense buf is busy
16248          *    2) we have 1 or more outstanding commands on the target
16249          *    (the sense data will be cleared or invalidated any way)
16250          *
16251          * Note: There could be an issue with not checking a retry limit here,
16252          * the problem is determining which retry limit to check.
16253          */
16254         if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16255                 /* Don't retry if the command is flagged as non-retryable */
16256                 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16257                         sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16258                             NULL, NULL, 0, un->un_busy_timeout,
16259                             kstat_waitq_enter);
16260                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16261                             "sd_send_request_sense_command: "
16262                             "at full throttle, retrying exit\n");
16263                 } else {
16264                         sd_return_failed_command(un, bp, EIO);
16265                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16266                             "sd_send_request_sense_command: "
16267                             "at full throttle, non-retryable exit\n");
16268                 }
16269                 return;
16270         }
16271 
16272         sd_mark_rqs_busy(un, bp);
16273         sd_start_cmds(un, un->un_rqs_bp);
16274 
16275         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16276             "sd_send_request_sense_command: exit\n");
16277 }
16278 
16279 
16280 /*
16281  *    Function: sd_mark_rqs_busy
16282  *
16283  * Description: Indicate that the request sense bp for this instance is
16284  *              in use.
16285  *
16286  *     Context: May be called under interrupt context
16287  */
16288 
16289 static void
16290 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16291 {
16292         struct sd_xbuf  *sense_xp;
16293 
16294         ASSERT(un != NULL);
16295         ASSERT(bp != NULL);
16296         ASSERT(mutex_owned(SD_MUTEX(un)));
16297         ASSERT(un->un_sense_isbusy == 0);
16298 
16299         SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16300             "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16301 
16302         sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16303         ASSERT(sense_xp != NULL);
16304 
16305         SD_INFO(SD_LOG_IO, un,
16306             "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16307 
16308         ASSERT(sense_xp->xb_pktp != NULL);
16309         ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16310             == (FLAG_SENSING | FLAG_HEAD));
16311 
16312         un->un_sense_isbusy = 1;
16313         un->un_rqs_bp->b_resid = 0;
16314         sense_xp->xb_pktp->pkt_resid  = 0;
16315         sense_xp->xb_pktp->pkt_reason = 0;
16316 
16317         /* So we can get back the bp at interrupt time! */
16318         sense_xp->xb_sense_bp = bp;
16319 
16320         bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16321 
16322         /*
16323          * Mark this buf as awaiting sense data. (This is already set in
16324          * the pkt_flags for the RQS packet.)
16325          */
16326         ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16327 
16328         /* Request sense down same path */
16329         if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16330             ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16331                 sense_xp->xb_pktp->pkt_path_instance =
16332                     ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16333 
16334         sense_xp->xb_retry_count     = 0;
16335         sense_xp->xb_victim_retry_count = 0;
16336         sense_xp->xb_ua_retry_count  = 0;
16337         sense_xp->xb_nr_retry_count  = 0;
16338         sense_xp->xb_dma_resid  = 0;
16339 
16340         /* Clean up the fields for auto-request sense */
16341         sense_xp->xb_sense_status = 0;
16342         sense_xp->xb_sense_state  = 0;
16343         sense_xp->xb_sense_resid  = 0;
16344         bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16345 
16346         SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16347 }
16348 
16349 
16350 /*
16351  *    Function: sd_mark_rqs_idle
16352  *
16353  * Description: SD_MUTEX must be held continuously through this routine
16354  *              to prevent reuse of the rqs struct before the caller can
16355  *              complete it's processing.
16356  *
16357  * Return Code: Pointer to the RQS buf
16358  *
16359  *     Context: May be called under interrupt context
16360  */
16361 
16362 static struct buf *
16363 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16364 {
16365         struct buf *bp;
16366         ASSERT(un != NULL);
16367         ASSERT(sense_xp != NULL);
16368         ASSERT(mutex_owned(SD_MUTEX(un)));
16369         ASSERT(un->un_sense_isbusy != 0);
16370 
16371         un->un_sense_isbusy = 0;
16372         bp = sense_xp->xb_sense_bp;
16373         sense_xp->xb_sense_bp = NULL;
16374 
16375         /* This pkt is no longer interested in getting sense data */
16376         ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16377 
16378         return (bp);
16379 }
16380 
16381 
16382 
16383 /*
16384  *    Function: sd_alloc_rqs
16385  *
16386  * Description: Set up the unit to receive auto request sense data
16387  *
16388  * Return Code: DDI_SUCCESS or DDI_FAILURE
16389  *
16390  *     Context: Called under attach(9E) context
16391  */
16392 
16393 static int
16394 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16395 {
16396         struct sd_xbuf *xp;
16397 
16398         ASSERT(un != NULL);
16399         ASSERT(!mutex_owned(SD_MUTEX(un)));
16400         ASSERT(un->un_rqs_bp == NULL);
16401         ASSERT(un->un_rqs_pktp == NULL);
16402 
16403         /*
16404          * First allocate the required buf and scsi_pkt structs, then set up
16405          * the CDB in the scsi_pkt for a REQUEST SENSE command.
16406          */
16407         un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16408             MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16409         if (un->un_rqs_bp == NULL) {
16410                 return (DDI_FAILURE);
16411         }
16412 
16413         un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16414             CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16415 
16416         if (un->un_rqs_pktp == NULL) {
16417                 sd_free_rqs(un);
16418                 return (DDI_FAILURE);
16419         }
16420 
16421         /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16422         (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16423             SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16424 
16425         SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16426 
16427         /* Set up the other needed members in the ARQ scsi_pkt. */
16428         un->un_rqs_pktp->pkt_comp   = sdintr;
16429         un->un_rqs_pktp->pkt_time   = sd_io_time;
16430         un->un_rqs_pktp->pkt_flags |=
16431             (FLAG_SENSING | FLAG_HEAD); /* (1222170) */
16432 
16433         /*
16434          * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16435          * provide any intpkt, destroypkt routines as we take care of
16436          * scsi_pkt allocation/freeing here and in sd_free_rqs().
16437          */
16438         xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16439         sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16440         xp->xb_pktp = un->un_rqs_pktp;
16441         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16442             "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16443             un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16444 
16445         /*
16446          * Save the pointer to the request sense private bp so it can
16447          * be retrieved in sdintr.
16448          */
16449         un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16450         ASSERT(un->un_rqs_bp->b_private == xp);
16451 
16452         /*
16453          * See if the HBA supports auto-request sense for the specified
16454          * target/lun. If it does, then try to enable it (if not already
16455          * enabled).
16456          *
16457          * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16458          * failure, while for other HBAs (pln) scsi_ifsetcap will always
16459          * return success.  However, in both of these cases ARQ is always
16460          * enabled and scsi_ifgetcap will always return true. The best approach
16461          * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16462          *
16463          * The 3rd case is the HBA (adp) always return enabled on
16464          * scsi_ifgetgetcap even when it's not enable, the best approach
16465          * is issue a scsi_ifsetcap then a scsi_ifgetcap
16466          * Note: this case is to circumvent the Adaptec bug. (x86 only)
16467          */
16468 
16469         if (un->un_f_is_fibre == TRUE) {
16470                 un->un_f_arq_enabled = TRUE;
16471         } else {
16472 #if defined(__i386) || defined(__amd64)
16473                 /*
16474                  * Circumvent the Adaptec bug, remove this code when
16475                  * the bug is fixed
16476                  */
16477                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16478 #endif
16479                 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16480                 case 0:
16481                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16482                             "sd_alloc_rqs: HBA supports ARQ\n");
16483                         /*
16484                          * ARQ is supported by this HBA but currently is not
16485                          * enabled. Attempt to enable it and if successful then
16486                          * mark this instance as ARQ enabled.
16487                          */
16488                         if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16489                             == 1) {
16490                                 /* Successfully enabled ARQ in the HBA */
16491                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16492                                     "sd_alloc_rqs: ARQ enabled\n");
16493                                 un->un_f_arq_enabled = TRUE;
16494                         } else {
16495                                 /* Could not enable ARQ in the HBA */
16496                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16497                                     "sd_alloc_rqs: failed ARQ enable\n");
16498                                 un->un_f_arq_enabled = FALSE;
16499                         }
16500                         break;
16501                 case 1:
16502                         /*
16503                          * ARQ is supported by this HBA and is already enabled.
16504                          * Just mark ARQ as enabled for this instance.
16505                          */
16506                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16507                             "sd_alloc_rqs: ARQ already enabled\n");
16508                         un->un_f_arq_enabled = TRUE;
16509                         break;
16510                 default:
16511                         /*
16512                          * ARQ is not supported by this HBA; disable it for this
16513                          * instance.
16514                          */
16515                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16516                             "sd_alloc_rqs: HBA does not support ARQ\n");
16517                         un->un_f_arq_enabled = FALSE;
16518                         break;
16519                 }
16520         }
16521 
16522         return (DDI_SUCCESS);
16523 }
16524 
16525 
16526 /*
16527  *    Function: sd_free_rqs
16528  *
16529  * Description: Cleanup for the pre-instance RQS command.
16530  *
16531  *     Context: Kernel thread context
16532  */
16533 
16534 static void
16535 sd_free_rqs(struct sd_lun *un)
16536 {
16537         ASSERT(un != NULL);
16538 
16539         SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16540 
16541         /*
16542          * If consistent memory is bound to a scsi_pkt, the pkt
16543          * has to be destroyed *before* freeing the consistent memory.
16544          * Don't change the sequence of this operations.
16545          * scsi_destroy_pkt() might access memory, which isn't allowed,
16546          * after it was freed in scsi_free_consistent_buf().
16547          */
16548         if (un->un_rqs_pktp != NULL) {
16549                 scsi_destroy_pkt(un->un_rqs_pktp);
16550                 un->un_rqs_pktp = NULL;
16551         }
16552 
16553         if (un->un_rqs_bp != NULL) {
16554                 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16555                 if (xp != NULL) {
16556                         kmem_free(xp, sizeof (struct sd_xbuf));
16557                 }
16558                 scsi_free_consistent_buf(un->un_rqs_bp);
16559                 un->un_rqs_bp = NULL;
16560         }
16561         SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16562 }
16563 
16564 
16565 
16566 /*
16567  *    Function: sd_reduce_throttle
16568  *
16569  * Description: Reduces the maximum # of outstanding commands on a
16570  *              target to the current number of outstanding commands.
16571  *              Queues a tiemout(9F) callback to restore the limit
16572  *              after a specified interval has elapsed.
16573  *              Typically used when we get a TRAN_BUSY return code
16574  *              back from scsi_transport().
16575  *
16576  *   Arguments: un - ptr to the sd_lun softstate struct
16577  *              throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16578  *
16579  *     Context: May be called from interrupt context
16580  */
16581 
16582 static void
16583 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16584 {
16585         ASSERT(un != NULL);
16586         ASSERT(mutex_owned(SD_MUTEX(un)));
16587         ASSERT(un->un_ncmds_in_transport >= 0);
16588 
16589         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16590             "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16591             un, un->un_throttle, un->un_ncmds_in_transport);
16592 
16593         if (un->un_throttle > 1) {
16594                 if (un->un_f_use_adaptive_throttle == TRUE) {
16595                         switch (throttle_type) {
16596                         case SD_THROTTLE_TRAN_BUSY:
16597                                 if (un->un_busy_throttle == 0) {
16598                                         un->un_busy_throttle = un->un_throttle;
16599                                 }
16600                                 break;
16601                         case SD_THROTTLE_QFULL:
16602                                 un->un_busy_throttle = 0;
16603                                 break;
16604                         default:
16605                                 ASSERT(FALSE);
16606                         }
16607 
16608                         if (un->un_ncmds_in_transport > 0) {
16609                                 un->un_throttle = un->un_ncmds_in_transport;
16610                         }
16611 
16612                 } else {
16613                         if (un->un_ncmds_in_transport == 0) {
16614                                 un->un_throttle = 1;
16615                         } else {
16616                                 un->un_throttle = un->un_ncmds_in_transport;
16617                         }
16618                 }
16619         }
16620 
16621         /* Reschedule the timeout if none is currently active */
16622         if (un->un_reset_throttle_timeid == NULL) {
16623                 un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16624                     un, SD_THROTTLE_RESET_INTERVAL);
16625                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16626                     "sd_reduce_throttle: timeout scheduled!\n");
16627         }
16628 
16629         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16630             "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16631 }
16632 
16633 
16634 
16635 /*
16636  *    Function: sd_restore_throttle
16637  *
16638  * Description: Callback function for timeout(9F).  Resets the current
16639  *              value of un->un_throttle to its default.
16640  *
16641  *   Arguments: arg - pointer to associated softstate for the device.
16642  *
16643  *     Context: May be called from interrupt context
16644  */
16645 
16646 static void
16647 sd_restore_throttle(void *arg)
16648 {
16649         struct sd_lun   *un = arg;
16650 
16651         ASSERT(un != NULL);
16652         ASSERT(!mutex_owned(SD_MUTEX(un)));
16653 
16654         mutex_enter(SD_MUTEX(un));
16655 
16656         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16657             "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16658 
16659         un->un_reset_throttle_timeid = NULL;
16660 
16661         if (un->un_f_use_adaptive_throttle == TRUE) {
16662                 /*
16663                  * If un_busy_throttle is nonzero, then it contains the
16664                  * value that un_throttle was when we got a TRAN_BUSY back
16665                  * from scsi_transport(). We want to revert back to this
16666                  * value.
16667                  *
16668                  * In the QFULL case, the throttle limit will incrementally
16669                  * increase until it reaches max throttle.
16670                  */
16671                 if (un->un_busy_throttle > 0) {
16672                         un->un_throttle = un->un_busy_throttle;
16673                         un->un_busy_throttle = 0;
16674                 } else {
16675                         /*
16676                          * increase throttle by 10% open gate slowly, schedule
16677                          * another restore if saved throttle has not been
16678                          * reached
16679                          */
16680                         short throttle;
16681                         if (sd_qfull_throttle_enable) {
16682                                 throttle = un->un_throttle +
16683                                     max((un->un_throttle / 10), 1);
16684                                 un->un_throttle =
16685                                     (throttle < un->un_saved_throttle) ?
16686                                     throttle : un->un_saved_throttle;
16687                                 if (un->un_throttle < un->un_saved_throttle) {
16688                                         un->un_reset_throttle_timeid =
16689                                             timeout(sd_restore_throttle,
16690                                             un,
16691                                             SD_QFULL_THROTTLE_RESET_INTERVAL);
16692                                 }
16693                         }
16694                 }
16695 
16696                 /*
16697                  * If un_throttle has fallen below the low-water mark, we
16698                  * restore the maximum value here (and allow it to ratchet
16699                  * down again if necessary).
16700                  */
16701                 if (un->un_throttle < un->un_min_throttle) {
16702                         un->un_throttle = un->un_saved_throttle;
16703                 }
16704         } else {
16705                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16706                     "restoring limit from 0x%x to 0x%x\n",
16707                     un->un_throttle, un->un_saved_throttle);
16708                 un->un_throttle = un->un_saved_throttle;
16709         }
16710 
16711         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16712             "sd_restore_throttle: calling sd_start_cmds!\n");
16713 
16714         sd_start_cmds(un, NULL);
16715 
16716         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16717             "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16718             un, un->un_throttle);
16719 
16720         mutex_exit(SD_MUTEX(un));
16721 
16722         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16723 }
16724 
16725 /*
16726  *    Function: sdrunout
16727  *
16728  * Description: Callback routine for scsi_init_pkt when a resource allocation
16729  *              fails.
16730  *
16731  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16732  *              soft state instance.
16733  *
16734  * Return Code: The scsi_init_pkt routine allows for the callback function to
16735  *              return a 0 indicating the callback should be rescheduled or a 1
16736  *              indicating not to reschedule. This routine always returns 1
16737  *              because the driver always provides a callback function to
16738  *              scsi_init_pkt. This results in a callback always being scheduled
16739  *              (via the scsi_init_pkt callback implementation) if a resource
16740  *              failure occurs.
16741  *
16742  *     Context: This callback function may not block or call routines that block
16743  *
16744  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16745  *              request persisting at the head of the list which cannot be
16746  *              satisfied even after multiple retries. In the future the driver
16747  *              may implement some time of maximum runout count before failing
16748  *              an I/O.
16749  */
16750 
16751 static int
16752 sdrunout(caddr_t arg)
16753 {
16754         struct sd_lun   *un = (struct sd_lun *)arg;
16755 
16756         ASSERT(un != NULL);
16757         ASSERT(!mutex_owned(SD_MUTEX(un)));
16758 
16759         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16760 
16761         mutex_enter(SD_MUTEX(un));
16762         sd_start_cmds(un, NULL);
16763         mutex_exit(SD_MUTEX(un));
16764         /*
16765          * This callback routine always returns 1 (i.e. do not reschedule)
16766          * because we always specify sdrunout as the callback handler for
16767          * scsi_init_pkt inside the call to sd_start_cmds.
16768          */
16769         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16770         return (1);
16771 }
16772 
16773 
16774 /*
16775  *    Function: sdintr
16776  *
16777  * Description: Completion callback routine for scsi_pkt(9S) structs
16778  *              sent to the HBA driver via scsi_transport(9F).
16779  *
16780  *     Context: Interrupt context
16781  */
16782 
16783 static void
16784 sdintr(struct scsi_pkt *pktp)
16785 {
16786         struct buf      *bp;
16787         struct sd_xbuf  *xp;
16788         struct sd_lun   *un;
16789         size_t          actual_len;
16790         sd_ssc_t        *sscp;
16791 
16792         ASSERT(pktp != NULL);
16793         bp = (struct buf *)pktp->pkt_private;
16794         ASSERT(bp != NULL);
16795         xp = SD_GET_XBUF(bp);
16796         ASSERT(xp != NULL);
16797         ASSERT(xp->xb_pktp != NULL);
16798         un = SD_GET_UN(bp);
16799         ASSERT(un != NULL);
16800         ASSERT(!mutex_owned(SD_MUTEX(un)));
16801 
16802 #ifdef SD_FAULT_INJECTION
16803 
16804         SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16805         /* SD FaultInjection */
16806         sd_faultinjection(pktp);
16807 
16808 #endif /* SD_FAULT_INJECTION */
16809 
16810         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16811             " xp:0x%p, un:0x%p\n", bp, xp, un);
16812 
16813         mutex_enter(SD_MUTEX(un));
16814 
16815         ASSERT(un->un_fm_private != NULL);
16816         sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16817         ASSERT(sscp != NULL);
16818 
16819         /* Reduce the count of the #commands currently in transport */
16820         un->un_ncmds_in_transport--;
16821         ASSERT(un->un_ncmds_in_transport >= 0);
16822 
16823         /* Increment counter to indicate that the callback routine is active */
16824         un->un_in_callback++;
16825 
16826         SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16827 
16828 #ifdef  SDDEBUG
16829         if (bp == un->un_retry_bp) {
16830                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16831                     "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16832                     un, un->un_retry_bp, un->un_ncmds_in_transport);
16833         }
16834 #endif
16835 
16836         /*
16837          * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16838          * state if needed.
16839          */
16840         if (pktp->pkt_reason == CMD_DEV_GONE) {
16841                 /* Prevent multiple console messages for the same failure. */
16842                 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16843                         un->un_last_pkt_reason = CMD_DEV_GONE;
16844                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16845                             "Command failed to complete...Device is gone\n");
16846                 }
16847                 if (un->un_mediastate != DKIO_DEV_GONE) {
16848                         un->un_mediastate = DKIO_DEV_GONE;
16849                         cv_broadcast(&un->un_state_cv);
16850                 }
16851                 /*
16852                  * If the command happens to be the REQUEST SENSE command,
16853                  * free up the rqs buf and fail the original command.
16854                  */
16855                 if (bp == un->un_rqs_bp) {
16856                         bp = sd_mark_rqs_idle(un, xp);
16857                 }
16858                 sd_return_failed_command(un, bp, EIO);
16859                 goto exit;
16860         }
16861 
16862         if (pktp->pkt_state & STATE_XARQ_DONE) {
16863                 SD_TRACE(SD_LOG_COMMON, un,
16864                     "sdintr: extra sense data received. pkt=%p\n", pktp);
16865         }
16866 
16867         /*
16868          * First see if the pkt has auto-request sense data with it....
16869          * Look at the packet state first so we don't take a performance
16870          * hit looking at the arq enabled flag unless absolutely necessary.
16871          */
16872         if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16873             (un->un_f_arq_enabled == TRUE)) {
16874                 /*
16875                  * The HBA did an auto request sense for this command so check
16876                  * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16877                  * driver command that should not be retried.
16878                  */
16879                 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16880                         /*
16881                          * Save the relevant sense info into the xp for the
16882                          * original cmd.
16883                          */
16884                         struct scsi_arq_status *asp;
16885                         asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16886                         xp->xb_sense_status =
16887                             *((uchar_t *)(&(asp->sts_rqpkt_status)));
16888                         xp->xb_sense_state  = asp->sts_rqpkt_state;
16889                         xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16890                         if (pktp->pkt_state & STATE_XARQ_DONE) {
16891                                 actual_len = MAX_SENSE_LENGTH -
16892                                     xp->xb_sense_resid;
16893                                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16894                                     MAX_SENSE_LENGTH);
16895                         } else {
16896                                 if (xp->xb_sense_resid > SENSE_LENGTH) {
16897                                         actual_len = MAX_SENSE_LENGTH -
16898                                             xp->xb_sense_resid;
16899                                 } else {
16900                                         actual_len = SENSE_LENGTH -
16901                                             xp->xb_sense_resid;
16902                                 }
16903                                 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16904                                         if ((((struct uscsi_cmd *)
16905                                             (xp->xb_pktinfo))->uscsi_rqlen) >
16906                                             actual_len) {
16907                                                 xp->xb_sense_resid =
16908                                                     (((struct uscsi_cmd *)
16909                                                     (xp->xb_pktinfo))->
16910                                                     uscsi_rqlen) - actual_len;
16911                                         } else {
16912                                                 xp->xb_sense_resid = 0;
16913                                         }
16914                                 }
16915                                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16916                                     SENSE_LENGTH);
16917                         }
16918 
16919                         /* fail the command */
16920                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16921                             "sdintr: arq done and FLAG_DIAGNOSE set\n");
16922                         sd_return_failed_command(un, bp, EIO);
16923                         goto exit;
16924                 }
16925 
16926 #if (defined(__i386) || defined(__amd64))       /* DMAFREE for x86 only */
16927                 /*
16928                  * We want to either retry or fail this command, so free
16929                  * the DMA resources here.  If we retry the command then
16930                  * the DMA resources will be reallocated in sd_start_cmds().
16931                  * Note that when PKT_DMA_PARTIAL is used, this reallocation
16932                  * causes the *entire* transfer to start over again from the
16933                  * beginning of the request, even for PARTIAL chunks that
16934                  * have already transferred successfully.
16935                  */
16936                 if ((un->un_f_is_fibre == TRUE) &&
16937                     ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16938                     ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16939                         scsi_dmafree(pktp);
16940                         xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16941                 }
16942 #endif
16943 
16944                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16945                     "sdintr: arq done, sd_handle_auto_request_sense\n");
16946 
16947                 sd_handle_auto_request_sense(un, bp, xp, pktp);
16948                 goto exit;
16949         }
16950 
16951         /* Next see if this is the REQUEST SENSE pkt for the instance */
16952         if (pktp->pkt_flags & FLAG_SENSING)  {
16953                 /* This pktp is from the unit's REQUEST_SENSE command */
16954                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16955                     "sdintr: sd_handle_request_sense\n");
16956                 sd_handle_request_sense(un, bp, xp, pktp);
16957                 goto exit;
16958         }
16959 
16960         /*
16961          * Check to see if the command successfully completed as requested;
16962          * this is the most common case (and also the hot performance path).
16963          *
16964          * Requirements for successful completion are:
16965          * pkt_reason is CMD_CMPLT and packet status is status good.
16966          * In addition:
16967          * - A residual of zero indicates successful completion no matter what
16968          *   the command is.
16969          * - If the residual is not zero and the command is not a read or
16970          *   write, then it's still defined as successful completion. In other
16971          *   words, if the command is a read or write the residual must be
16972          *   zero for successful completion.
16973          * - If the residual is not zero and the command is a read or
16974          *   write, and it's a USCSICMD, then it's still defined as
16975          *   successful completion.
16976          */
16977         if ((pktp->pkt_reason == CMD_CMPLT) &&
16978             (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16979 
16980                 /*
16981                  * Since this command is returned with a good status, we
16982                  * can reset the count for Sonoma failover.
16983                  */
16984                 un->un_sonoma_failure_count = 0;
16985 
16986                 /*
16987                  * Return all USCSI commands on good status
16988                  */
16989                 if (pktp->pkt_resid == 0) {
16990                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16991                             "sdintr: returning command for resid == 0\n");
16992                 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16993                     ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16994                         SD_UPDATE_B_RESID(bp, pktp);
16995                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16996                             "sdintr: returning command for resid != 0\n");
16997                 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16998                         SD_UPDATE_B_RESID(bp, pktp);
16999                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17000                             "sdintr: returning uscsi command\n");
17001                 } else {
17002                         goto not_successful;
17003                 }
17004                 sd_return_command(un, bp);
17005 
17006                 /*
17007                  * Decrement counter to indicate that the callback routine
17008                  * is done.
17009                  */
17010                 un->un_in_callback--;
17011                 ASSERT(un->un_in_callback >= 0);
17012                 mutex_exit(SD_MUTEX(un));
17013 
17014                 return;
17015         }
17016 
17017 not_successful:
17018 
17019 #if (defined(__i386) || defined(__amd64))       /* DMAFREE for x86 only */
17020         /*
17021          * The following is based upon knowledge of the underlying transport
17022          * and its use of DMA resources.  This code should be removed when
17023          * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17024          * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17025          * and sd_start_cmds().
17026          *
17027          * Free any DMA resources associated with this command if there
17028          * is a chance it could be retried or enqueued for later retry.
17029          * If we keep the DMA binding then mpxio cannot reissue the
17030          * command on another path whenever a path failure occurs.
17031          *
17032          * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17033          * causes the *entire* transfer to start over again from the
17034          * beginning of the request, even for PARTIAL chunks that
17035          * have already transferred successfully.
17036          *
17037          * This is only done for non-uscsi commands (and also skipped for the
17038          * driver's internal RQS command). Also just do this for Fibre Channel
17039          * devices as these are the only ones that support mpxio.
17040          */
17041         if ((un->un_f_is_fibre == TRUE) &&
17042             ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17043             ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17044                 scsi_dmafree(pktp);
17045                 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17046         }
17047 #endif
17048 
17049         /*
17050          * The command did not successfully complete as requested so check
17051          * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17052          * driver command that should not be retried so just return. If
17053          * FLAG_DIAGNOSE is not set the error will be processed below.
17054          */
17055         if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17056                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17057                     "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17058                 /*
17059                  * Issue a request sense if a check condition caused the error
17060                  * (we handle the auto request sense case above), otherwise
17061                  * just fail the command.
17062                  */
17063                 if ((pktp->pkt_reason == CMD_CMPLT) &&
17064                     (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17065                         sd_send_request_sense_command(un, bp, pktp);
17066                 } else {
17067                         sd_return_failed_command(un, bp, EIO);
17068                 }
17069                 goto exit;
17070         }
17071 
17072         /*
17073          * The command did not successfully complete as requested so process
17074          * the error, retry, and/or attempt recovery.
17075          */
17076         switch (pktp->pkt_reason) {
17077         case CMD_CMPLT:
17078                 switch (SD_GET_PKT_STATUS(pktp)) {
17079                 case STATUS_GOOD:
17080                         /*
17081                          * The command completed successfully with a non-zero
17082                          * residual
17083                          */
17084                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17085                             "sdintr: STATUS_GOOD \n");
17086                         sd_pkt_status_good(un, bp, xp, pktp);
17087                         break;
17088 
17089                 case STATUS_CHECK:
17090                 case STATUS_TERMINATED:
17091                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17092                             "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17093                         sd_pkt_status_check_condition(un, bp, xp, pktp);
17094                         break;
17095 
17096                 case STATUS_BUSY:
17097                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17098                             "sdintr: STATUS_BUSY\n");
17099                         sd_pkt_status_busy(un, bp, xp, pktp);
17100                         break;
17101 
17102                 case STATUS_RESERVATION_CONFLICT:
17103                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17104                             "sdintr: STATUS_RESERVATION_CONFLICT\n");
17105                         sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17106                         break;
17107 
17108                 case STATUS_QFULL:
17109                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17110                             "sdintr: STATUS_QFULL\n");
17111                         sd_pkt_status_qfull(un, bp, xp, pktp);
17112                         break;
17113 
17114                 case STATUS_MET:
17115                 case STATUS_INTERMEDIATE:
17116                 case STATUS_SCSI2:
17117                 case STATUS_INTERMEDIATE_MET:
17118                 case STATUS_ACA_ACTIVE:
17119                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17120                             "Unexpected SCSI status received: 0x%x\n",
17121                             SD_GET_PKT_STATUS(pktp));
17122                         /*
17123                          * Mark the ssc_flags when detected invalid status
17124                          * code for non-USCSI command.
17125                          */
17126                         if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17127                                 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17128                                     0, "stat-code");
17129                         }
17130                         sd_return_failed_command(un, bp, EIO);
17131                         break;
17132 
17133                 default:
17134                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17135                             "Invalid SCSI status received: 0x%x\n",
17136                             SD_GET_PKT_STATUS(pktp));
17137                         if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17138                                 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17139                                     0, "stat-code");
17140                         }
17141                         sd_return_failed_command(un, bp, EIO);
17142                         break;
17143 
17144                 }
17145                 break;
17146 
17147         case CMD_INCOMPLETE:
17148                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17149                     "sdintr:  CMD_INCOMPLETE\n");
17150                 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17151                 break;
17152         case CMD_TRAN_ERR:
17153                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17154                     "sdintr: CMD_TRAN_ERR\n");
17155                 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17156                 break;
17157         case CMD_RESET:
17158                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17159                     "sdintr: CMD_RESET \n");
17160                 sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17161                 break;
17162         case CMD_ABORTED:
17163                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17164                     "sdintr: CMD_ABORTED \n");
17165                 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17166                 break;
17167         case CMD_TIMEOUT:
17168                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17169                     "sdintr: CMD_TIMEOUT\n");
17170                 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17171                 break;
17172         case CMD_UNX_BUS_FREE:
17173                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17174                     "sdintr: CMD_UNX_BUS_FREE \n");
17175                 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17176                 break;
17177         case CMD_TAG_REJECT:
17178                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17179                     "sdintr: CMD_TAG_REJECT\n");
17180                 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17181                 break;
17182         default:
17183                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17184                     "sdintr: default\n");
17185                 /*
17186                  * Mark the ssc_flags for detecting invliad pkt_reason.
17187                  */
17188                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17189                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17190                             0, "pkt-reason");
17191                 }
17192                 sd_pkt_reason_default(un, bp, xp, pktp);
17193                 break;
17194         }
17195 
17196 exit:
17197         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17198 
17199         /* Decrement counter to indicate that the callback routine is done. */
17200         un->un_in_callback--;
17201         ASSERT(un->un_in_callback >= 0);
17202 
17203         /*
17204          * At this point, the pkt has been dispatched, ie, it is either
17205          * being re-tried or has been returned to its caller and should
17206          * not be referenced.
17207          */
17208 
17209         mutex_exit(SD_MUTEX(un));
17210 }
17211 
17212 
17213 /*
17214  *    Function: sd_print_incomplete_msg
17215  *
17216  * Description: Prints the error message for a CMD_INCOMPLETE error.
17217  *
17218  *   Arguments: un - ptr to associated softstate for the device.
17219  *              bp - ptr to the buf(9S) for the command.
17220  *              arg - message string ptr
17221  *              code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17222  *                      or SD_NO_RETRY_ISSUED.
17223  *
17224  *     Context: May be called under interrupt context
17225  */
17226 
17227 static void
17228 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17229 {
17230         struct scsi_pkt *pktp;
17231         char    *msgp;
17232         char    *cmdp = arg;
17233 
17234         ASSERT(un != NULL);
17235         ASSERT(mutex_owned(SD_MUTEX(un)));
17236         ASSERT(bp != NULL);
17237         ASSERT(arg != NULL);
17238         pktp = SD_GET_PKTP(bp);
17239         ASSERT(pktp != NULL);
17240 
17241         switch (code) {
17242         case SD_DELAYED_RETRY_ISSUED:
17243         case SD_IMMEDIATE_RETRY_ISSUED:
17244                 msgp = "retrying";
17245                 break;
17246         case SD_NO_RETRY_ISSUED:
17247         default:
17248                 msgp = "giving up";
17249                 break;
17250         }
17251 
17252         if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17253                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17254                     "incomplete %s- %s\n", cmdp, msgp);
17255         }
17256 }
17257 
17258 
17259 
17260 /*
17261  *    Function: sd_pkt_status_good
17262  *
17263  * Description: Processing for a STATUS_GOOD code in pkt_status.
17264  *
17265  *     Context: May be called under interrupt context
17266  */
17267 
17268 static void
17269 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17270     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17271 {
17272         char    *cmdp;
17273 
17274         ASSERT(un != NULL);
17275         ASSERT(mutex_owned(SD_MUTEX(un)));
17276         ASSERT(bp != NULL);
17277         ASSERT(xp != NULL);
17278         ASSERT(pktp != NULL);
17279         ASSERT(pktp->pkt_reason == CMD_CMPLT);
17280         ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17281         ASSERT(pktp->pkt_resid != 0);
17282 
17283         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17284 
17285         SD_UPDATE_ERRSTATS(un, sd_harderrs);
17286         switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17287         case SCMD_READ:
17288                 cmdp = "read";
17289                 break;
17290         case SCMD_WRITE:
17291                 cmdp = "write";
17292                 break;
17293         default:
17294                 SD_UPDATE_B_RESID(bp, pktp);
17295                 sd_return_command(un, bp);
17296                 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17297                 return;
17298         }
17299 
17300         /*
17301          * See if we can retry the read/write, preferrably immediately.
17302          * If retries are exhaused, then sd_retry_command() will update
17303          * the b_resid count.
17304          */
17305         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17306             cmdp, EIO, (clock_t)0, NULL);
17307 
17308         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17309 }
17310 
17311 
17312 
17313 
17314 
17315 /*
17316  *    Function: sd_handle_request_sense
17317  *
17318  * Description: Processing for non-auto Request Sense command.
17319  *
17320  *   Arguments: un - ptr to associated softstate
17321  *              sense_bp - ptr to buf(9S) for the RQS command
17322  *              sense_xp - ptr to the sd_xbuf for the RQS command
17323  *              sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17324  *
17325  *     Context: May be called under interrupt context
17326  */
17327 
17328 static void
17329 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17330     struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17331 {
17332         struct buf      *cmd_bp;        /* buf for the original command */
17333         struct sd_xbuf  *cmd_xp;        /* sd_xbuf for the original command */
17334         struct scsi_pkt *cmd_pktp;      /* pkt for the original command */
17335         size_t          actual_len;     /* actual sense data length */
17336 
17337         ASSERT(un != NULL);
17338         ASSERT(mutex_owned(SD_MUTEX(un)));
17339         ASSERT(sense_bp != NULL);
17340         ASSERT(sense_xp != NULL);
17341         ASSERT(sense_pktp != NULL);
17342 
17343         /*
17344          * Note the sense_bp, sense_xp, and sense_pktp here are for the
17345          * RQS command and not the original command.
17346          */
17347         ASSERT(sense_pktp == un->un_rqs_pktp);
17348         ASSERT(sense_bp   == un->un_rqs_bp);
17349         ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17350             (FLAG_SENSING | FLAG_HEAD));
17351         ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17352             FLAG_SENSING) == FLAG_SENSING);
17353 
17354         /* These are the bp, xp, and pktp for the original command */
17355         cmd_bp = sense_xp->xb_sense_bp;
17356         cmd_xp = SD_GET_XBUF(cmd_bp);
17357         cmd_pktp = SD_GET_PKTP(cmd_bp);
17358 
17359         if (sense_pktp->pkt_reason != CMD_CMPLT) {
17360                 /*
17361                  * The REQUEST SENSE command failed.  Release the REQUEST
17362                  * SENSE command for re-use, get back the bp for the original
17363                  * command, and attempt to re-try the original command if
17364                  * FLAG_DIAGNOSE is not set in the original packet.
17365                  */
17366                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17367                 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17368                         cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17369                         sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17370                             NULL, NULL, EIO, (clock_t)0, NULL);
17371                         return;
17372                 }
17373         }
17374 
17375         /*
17376          * Save the relevant sense info into the xp for the original cmd.
17377          *
17378          * Note: if the request sense failed the state info will be zero
17379          * as set in sd_mark_rqs_busy()
17380          */
17381         cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17382         cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17383         actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17384         if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17385             (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17386             SENSE_LENGTH)) {
17387                 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17388                     MAX_SENSE_LENGTH);
17389                 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17390         } else {
17391                 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17392                     SENSE_LENGTH);
17393                 if (actual_len < SENSE_LENGTH) {
17394                         cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17395                 } else {
17396                         cmd_xp->xb_sense_resid = 0;
17397                 }
17398         }
17399 
17400         /*
17401          *  Free up the RQS command....
17402          *  NOTE:
17403          *      Must do this BEFORE calling sd_validate_sense_data!
17404          *      sd_validate_sense_data may return the original command in
17405          *      which case the pkt will be freed and the flags can no
17406          *      longer be touched.
17407          *      SD_MUTEX is held through this process until the command
17408          *      is dispatched based upon the sense data, so there are
17409          *      no race conditions.
17410          */
17411         (void) sd_mark_rqs_idle(un, sense_xp);
17412 
17413         /*
17414          * For a retryable command see if we have valid sense data, if so then
17415          * turn it over to sd_decode_sense() to figure out the right course of
17416          * action. Just fail a non-retryable command.
17417          */
17418         if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17419                 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17420                     SD_SENSE_DATA_IS_VALID) {
17421                         sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17422                 }
17423         } else {
17424                 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17425                     (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17426                 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17427                     (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17428                 sd_return_failed_command(un, cmd_bp, EIO);
17429         }
17430 }
17431 
17432 
17433 
17434 
17435 /*
17436  *    Function: sd_handle_auto_request_sense
17437  *
17438  * Description: Processing for auto-request sense information.
17439  *
17440  *   Arguments: un - ptr to associated softstate
17441  *              bp - ptr to buf(9S) for the command
17442  *              xp - ptr to the sd_xbuf for the command
17443  *              pktp - ptr to the scsi_pkt(9S) for the command
17444  *
17445  *     Context: May be called under interrupt context
17446  */
17447 
17448 static void
17449 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17450     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17451 {
17452         struct scsi_arq_status *asp;
17453         size_t actual_len;
17454 
17455         ASSERT(un != NULL);
17456         ASSERT(mutex_owned(SD_MUTEX(un)));
17457         ASSERT(bp != NULL);
17458         ASSERT(xp != NULL);
17459         ASSERT(pktp != NULL);
17460         ASSERT(pktp != un->un_rqs_pktp);
17461         ASSERT(bp   != un->un_rqs_bp);
17462 
17463         /*
17464          * For auto-request sense, we get a scsi_arq_status back from
17465          * the HBA, with the sense data in the sts_sensedata member.
17466          * The pkt_scbp of the packet points to this scsi_arq_status.
17467          */
17468         asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17469 
17470         if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17471                 /*
17472                  * The auto REQUEST SENSE failed; see if we can re-try
17473                  * the original command.
17474                  */
17475                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17476                     "auto request sense failed (reason=%s)\n",
17477                     scsi_rname(asp->sts_rqpkt_reason));
17478 
17479                 sd_reset_target(un, pktp);
17480 
17481                 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17482                     NULL, NULL, EIO, (clock_t)0, NULL);
17483                 return;
17484         }
17485 
17486         /* Save the relevant sense info into the xp for the original cmd. */
17487         xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17488         xp->xb_sense_state  = asp->sts_rqpkt_state;
17489         xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17490         if (xp->xb_sense_state & STATE_XARQ_DONE) {
17491                 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17492                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17493                     MAX_SENSE_LENGTH);
17494         } else {
17495                 if (xp->xb_sense_resid > SENSE_LENGTH) {
17496                         actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17497                 } else {
17498                         actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17499                 }
17500                 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17501                         if ((((struct uscsi_cmd *)
17502                             (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17503                                 xp->xb_sense_resid = (((struct uscsi_cmd *)
17504                                     (xp->xb_pktinfo))->uscsi_rqlen) -
17505                                     actual_len;
17506                         } else {
17507                                 xp->xb_sense_resid = 0;
17508                         }
17509                 }
17510                 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17511         }
17512 
17513         /*
17514          * See if we have valid sense data, if so then turn it over to
17515          * sd_decode_sense() to figure out the right course of action.
17516          */
17517         if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17518             SD_SENSE_DATA_IS_VALID) {
17519                 sd_decode_sense(un, bp, xp, pktp);
17520         }
17521 }
17522 
17523 
17524 /*
17525  *    Function: sd_print_sense_failed_msg
17526  *
17527  * Description: Print log message when RQS has failed.
17528  *
17529  *   Arguments: un - ptr to associated softstate
17530  *              bp - ptr to buf(9S) for the command
17531  *              arg - generic message string ptr
17532  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17533  *                      or SD_NO_RETRY_ISSUED
17534  *
17535  *     Context: May be called from interrupt context
17536  */
17537 
17538 static void
17539 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17540     int code)
17541 {
17542         char    *msgp = arg;
17543 
17544         ASSERT(un != NULL);
17545         ASSERT(mutex_owned(SD_MUTEX(un)));
17546         ASSERT(bp != NULL);
17547 
17548         if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17549                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17550         }
17551 }
17552 
17553 
17554 /*
17555  *    Function: sd_validate_sense_data
17556  *
17557  * Description: Check the given sense data for validity.
17558  *              If the sense data is not valid, the command will
17559  *              be either failed or retried!
17560  *
17561  * Return Code: SD_SENSE_DATA_IS_INVALID
17562  *              SD_SENSE_DATA_IS_VALID
17563  *
17564  *     Context: May be called from interrupt context
17565  */
17566 
17567 static int
17568 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17569     size_t actual_len)
17570 {
17571         struct scsi_extended_sense *esp;
17572         struct  scsi_pkt *pktp;
17573         char    *msgp = NULL;
17574         sd_ssc_t *sscp;
17575 
17576         ASSERT(un != NULL);
17577         ASSERT(mutex_owned(SD_MUTEX(un)));
17578         ASSERT(bp != NULL);
17579         ASSERT(bp != un->un_rqs_bp);
17580         ASSERT(xp != NULL);
17581         ASSERT(un->un_fm_private != NULL);
17582 
17583         pktp = SD_GET_PKTP(bp);
17584         ASSERT(pktp != NULL);
17585 
17586         sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17587         ASSERT(sscp != NULL);
17588 
17589         /*
17590          * Check the status of the RQS command (auto or manual).
17591          */
17592         switch (xp->xb_sense_status & STATUS_MASK) {
17593         case STATUS_GOOD:
17594                 break;
17595 
17596         case STATUS_RESERVATION_CONFLICT:
17597                 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17598                 return (SD_SENSE_DATA_IS_INVALID);
17599 
17600         case STATUS_BUSY:
17601                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17602                     "Busy Status on REQUEST SENSE\n");
17603                 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17604                     NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17605                 return (SD_SENSE_DATA_IS_INVALID);
17606 
17607         case STATUS_QFULL:
17608                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17609                     "QFULL Status on REQUEST SENSE\n");
17610                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17611                     NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17612                 return (SD_SENSE_DATA_IS_INVALID);
17613 
17614         case STATUS_CHECK:
17615         case STATUS_TERMINATED:
17616                 msgp = "Check Condition on REQUEST SENSE\n";
17617                 goto sense_failed;
17618 
17619         default:
17620                 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17621                 goto sense_failed;
17622         }
17623 
17624         /*
17625          * See if we got the minimum required amount of sense data.
17626          * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17627          * or less.
17628          */
17629         if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17630             (actual_len == 0)) {
17631                 msgp = "Request Sense couldn't get sense data\n";
17632                 goto sense_failed;
17633         }
17634 
17635         if (actual_len < SUN_MIN_SENSE_LENGTH) {
17636                 msgp = "Not enough sense information\n";
17637                 /* Mark the ssc_flags for detecting invalid sense data */
17638                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17639                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17640                             "sense-data");
17641                 }
17642                 goto sense_failed;
17643         }
17644 
17645         /*
17646          * We require the extended sense data
17647          */
17648         esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17649         if (esp->es_class != CLASS_EXTENDED_SENSE) {
17650                 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17651                         static char tmp[8];
17652                         static char buf[148];
17653                         char *p = (char *)(xp->xb_sense_data);
17654                         int i;
17655 
17656                         mutex_enter(&sd_sense_mutex);
17657                         (void) strcpy(buf, "undecodable sense information:");
17658                         for (i = 0; i < actual_len; i++) {
17659                                 (void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17660                                 (void) strcpy(&buf[strlen(buf)], tmp);
17661                         }
17662                         i = strlen(buf);
17663                         (void) strcpy(&buf[i], "-(assumed fatal)\n");
17664 
17665                         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17666                                 scsi_log(SD_DEVINFO(un), sd_label,
17667                                     CE_WARN, buf);
17668                         }
17669                         mutex_exit(&sd_sense_mutex);
17670                 }
17671 
17672                 /* Mark the ssc_flags for detecting invalid sense data */
17673                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17674                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17675                             "sense-data");
17676                 }
17677 
17678                 /* Note: Legacy behavior, fail the command with no retry */
17679                 sd_return_failed_command(un, bp, EIO);
17680                 return (SD_SENSE_DATA_IS_INVALID);
17681         }
17682 
17683         /*
17684          * Check that es_code is valid (es_class concatenated with es_code
17685          * make up the "response code" field.  es_class will always be 7, so
17686          * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17687          * format.
17688          */
17689         if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17690             (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17691             (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17692             (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17693             (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17694                 /* Mark the ssc_flags for detecting invalid sense data */
17695                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17696                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17697                             "sense-data");
17698                 }
17699                 goto sense_failed;
17700         }
17701 
17702         return (SD_SENSE_DATA_IS_VALID);
17703 
17704 sense_failed:
17705         /*
17706          * If the request sense failed (for whatever reason), attempt
17707          * to retry the original command.
17708          */
17709 #if defined(__i386) || defined(__amd64)
17710         /*
17711          * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17712          * sddef.h for Sparc platform, and x86 uses 1 binary
17713          * for both SCSI/FC.
17714          * The SD_RETRY_DELAY value need to be adjusted here
17715          * when SD_RETRY_DELAY change in sddef.h
17716          */
17717         sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17718             sd_print_sense_failed_msg, msgp, EIO,
17719             un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17720 #else
17721         sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17722             sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17723 #endif
17724 
17725         return (SD_SENSE_DATA_IS_INVALID);
17726 }
17727 
17728 /*
17729  *    Function: sd_decode_sense
17730  *
17731  * Description: Take recovery action(s) when SCSI Sense Data is received.
17732  *
17733  *     Context: Interrupt context.
17734  */
17735 
17736 static void
17737 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17738     struct scsi_pkt *pktp)
17739 {
17740         uint8_t sense_key;
17741 
17742         ASSERT(un != NULL);
17743         ASSERT(mutex_owned(SD_MUTEX(un)));
17744         ASSERT(bp != NULL);
17745         ASSERT(bp != un->un_rqs_bp);
17746         ASSERT(xp != NULL);
17747         ASSERT(pktp != NULL);
17748 
17749         sense_key = scsi_sense_key(xp->xb_sense_data);
17750 
17751         switch (sense_key) {
17752         case KEY_NO_SENSE:
17753                 sd_sense_key_no_sense(un, bp, xp, pktp);
17754                 break;
17755         case KEY_RECOVERABLE_ERROR:
17756                 sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17757                     bp, xp, pktp);
17758                 break;
17759         case KEY_NOT_READY:
17760                 sd_sense_key_not_ready(un, xp->xb_sense_data,
17761                     bp, xp, pktp);
17762                 break;
17763         case KEY_MEDIUM_ERROR:
17764         case KEY_HARDWARE_ERROR:
17765                 sd_sense_key_medium_or_hardware_error(un,
17766                     xp->xb_sense_data, bp, xp, pktp);
17767                 break;
17768         case KEY_ILLEGAL_REQUEST:
17769                 sd_sense_key_illegal_request(un, bp, xp, pktp);
17770                 break;
17771         case KEY_UNIT_ATTENTION:
17772                 sd_sense_key_unit_attention(un, xp->xb_sense_data,
17773                     bp, xp, pktp);
17774                 break;
17775         case KEY_WRITE_PROTECT:
17776         case KEY_VOLUME_OVERFLOW:
17777         case KEY_MISCOMPARE:
17778                 sd_sense_key_fail_command(un, bp, xp, pktp);
17779                 break;
17780         case KEY_BLANK_CHECK:
17781                 sd_sense_key_blank_check(un, bp, xp, pktp);
17782                 break;
17783         case KEY_ABORTED_COMMAND:
17784                 sd_sense_key_aborted_command(un, bp, xp, pktp);
17785                 break;
17786         case KEY_VENDOR_UNIQUE:
17787         case KEY_COPY_ABORTED:
17788         case KEY_EQUAL:
17789         case KEY_RESERVED:
17790         default:
17791                 sd_sense_key_default(un, xp->xb_sense_data,
17792                     bp, xp, pktp);
17793                 break;
17794         }
17795 }
17796 
17797 
17798 /*
17799  *    Function: sd_dump_memory
17800  *
17801  * Description: Debug logging routine to print the contents of a user provided
17802  *              buffer. The output of the buffer is broken up into 256 byte
17803  *              segments due to a size constraint of the scsi_log.
17804  *              implementation.
17805  *
17806  *   Arguments: un - ptr to softstate
17807  *              comp - component mask
17808  *              title - "title" string to preceed data when printed
17809  *              data - ptr to data block to be printed
17810  *              len - size of data block to be printed
17811  *              fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17812  *
17813  *     Context: May be called from interrupt context
17814  */
17815 
17816 #define SD_DUMP_MEMORY_BUF_SIZE 256
17817 
17818 static char *sd_dump_format_string[] = {
17819                 " 0x%02x",
17820                 " %c"
17821 };
17822 
17823 static void
17824 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17825     int len, int fmt)
17826 {
17827         int     i, j;
17828         int     avail_count;
17829         int     start_offset;
17830         int     end_offset;
17831         size_t  entry_len;
17832         char    *bufp;
17833         char    *local_buf;
17834         char    *format_string;
17835 
17836         ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17837 
17838         /*
17839          * In the debug version of the driver, this function is called from a
17840          * number of places which are NOPs in the release driver.
17841          * The debug driver therefore has additional methods of filtering
17842          * debug output.
17843          */
17844 #ifdef SDDEBUG
17845         /*
17846          * In the debug version of the driver we can reduce the amount of debug
17847          * messages by setting sd_error_level to something other than
17848          * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17849          * sd_component_mask.
17850          */
17851         if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17852             (sd_error_level != SCSI_ERR_ALL)) {
17853                 return;
17854         }
17855         if (((sd_component_mask & comp) == 0) ||
17856             (sd_error_level != SCSI_ERR_ALL)) {
17857                 return;
17858         }
17859 #else
17860         if (sd_error_level != SCSI_ERR_ALL) {
17861                 return;
17862         }
17863 #endif
17864 
17865         local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17866         bufp = local_buf;
17867         /*
17868          * Available length is the length of local_buf[], minus the
17869          * length of the title string, minus one for the ":", minus
17870          * one for the newline, minus one for the NULL terminator.
17871          * This gives the #bytes available for holding the printed
17872          * values from the given data buffer.
17873          */
17874         if (fmt == SD_LOG_HEX) {
17875                 format_string = sd_dump_format_string[0];
17876         } else /* SD_LOG_CHAR */ {
17877                 format_string = sd_dump_format_string[1];
17878         }
17879         /*
17880          * Available count is the number of elements from the given
17881          * data buffer that we can fit into the available length.
17882          * This is based upon the size of the format string used.
17883          * Make one entry and find it's size.
17884          */
17885         (void) sprintf(bufp, format_string, data[0]);
17886         entry_len = strlen(bufp);
17887         avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17888 
17889         j = 0;
17890         while (j < len) {
17891                 bufp = local_buf;
17892                 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17893                 start_offset = j;
17894 
17895                 end_offset = start_offset + avail_count;
17896 
17897                 (void) sprintf(bufp, "%s:", title);
17898                 bufp += strlen(bufp);
17899                 for (i = start_offset; ((i < end_offset) && (j < len));
17900                     i++, j++) {
17901                         (void) sprintf(bufp, format_string, data[i]);
17902                         bufp += entry_len;
17903                 }
17904                 (void) sprintf(bufp, "\n");
17905 
17906                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17907         }
17908         kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17909 }
17910 
17911 /*
17912  *    Function: sd_print_sense_msg
17913  *
17914  * Description: Log a message based upon the given sense data.
17915  *
17916  *   Arguments: un - ptr to associated softstate
17917  *              bp - ptr to buf(9S) for the command
17918  *              arg - ptr to associate sd_sense_info struct
17919  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17920  *                      or SD_NO_RETRY_ISSUED
17921  *
17922  *     Context: May be called from interrupt context
17923  */
17924 
17925 static void
17926 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17927 {
17928         struct sd_xbuf  *xp;
17929         struct scsi_pkt *pktp;
17930         uint8_t *sensep;
17931         daddr_t request_blkno;
17932         diskaddr_t err_blkno;
17933         int severity;
17934         int pfa_flag;
17935         extern struct scsi_key_strings scsi_cmds[];
17936 
17937         ASSERT(un != NULL);
17938         ASSERT(mutex_owned(SD_MUTEX(un)));
17939         ASSERT(bp != NULL);
17940         xp = SD_GET_XBUF(bp);
17941         ASSERT(xp != NULL);
17942         pktp = SD_GET_PKTP(bp);
17943         ASSERT(pktp != NULL);
17944         ASSERT(arg != NULL);
17945 
17946         severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17947         pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17948 
17949         if ((code == SD_DELAYED_RETRY_ISSUED) ||
17950             (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17951                 severity = SCSI_ERR_RETRYABLE;
17952         }
17953 
17954         /* Use absolute block number for the request block number */
17955         request_blkno = xp->xb_blkno;
17956 
17957         /*
17958          * Now try to get the error block number from the sense data
17959          */
17960         sensep = xp->xb_sense_data;
17961 
17962         if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17963             (uint64_t *)&err_blkno)) {
17964                 /*
17965                  * We retrieved the error block number from the information
17966                  * portion of the sense data.
17967                  *
17968                  * For USCSI commands we are better off using the error
17969                  * block no. as the requested block no. (This is the best
17970                  * we can estimate.)
17971                  */
17972                 if ((SD_IS_BUFIO(xp) == FALSE) &&
17973                     ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17974                         request_blkno = err_blkno;
17975                 }
17976         } else {
17977                 /*
17978                  * Without the es_valid bit set (for fixed format) or an
17979                  * information descriptor (for descriptor format) we cannot
17980                  * be certain of the error blkno, so just use the
17981                  * request_blkno.
17982                  */
17983                 err_blkno = (diskaddr_t)request_blkno;
17984         }
17985 
17986         /*
17987          * The following will log the buffer contents for the release driver
17988          * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17989          * level is set to verbose.
17990          */
17991         sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17992             (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17993         sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17994             (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17995 
17996         if (pfa_flag == FALSE) {
17997                 /* This is normally only set for USCSI */
17998                 if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17999                         return;
18000                 }
18001 
18002                 if ((SD_IS_BUFIO(xp) == TRUE) &&
18003                     (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18004                     (severity < sd_error_level))) {
18005                         return;
18006                 }
18007         }
18008         /*
18009          * Check for Sonoma Failover and keep a count of how many failed I/O's
18010          */
18011         if ((SD_IS_LSI(un)) &&
18012             (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18013             (scsi_sense_asc(sensep) == 0x94) &&
18014             (scsi_sense_ascq(sensep) == 0x01)) {
18015                 un->un_sonoma_failure_count++;
18016                 if (un->un_sonoma_failure_count > 1) {
18017                         return;
18018                 }
18019         }
18020 
18021         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18022             ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18023             (pktp->pkt_resid == 0))) {
18024                 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18025                     request_blkno, err_blkno, scsi_cmds,
18026                     (struct scsi_extended_sense *)sensep,
18027                     un->un_additional_codes, NULL);
18028         }
18029 }
18030 
18031 /*
18032  *    Function: sd_sense_key_no_sense
18033  *
18034  * Description: Recovery action when sense data was not received.
18035  *
18036  *     Context: May be called from interrupt context
18037  */
18038 
18039 static void
18040 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18041     struct scsi_pkt *pktp)
18042 {
18043         struct sd_sense_info    si;
18044 
18045         ASSERT(un != NULL);
18046         ASSERT(mutex_owned(SD_MUTEX(un)));
18047         ASSERT(bp != NULL);
18048         ASSERT(xp != NULL);
18049         ASSERT(pktp != NULL);
18050 
18051         si.ssi_severity = SCSI_ERR_FATAL;
18052         si.ssi_pfa_flag = FALSE;
18053 
18054         SD_UPDATE_ERRSTATS(un, sd_softerrs);
18055 
18056         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18057             &si, EIO, (clock_t)0, NULL);
18058 }
18059 
18060 
18061 /*
18062  *    Function: sd_sense_key_recoverable_error
18063  *
18064  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18065  *
18066  *     Context: May be called from interrupt context
18067  */
18068 
18069 static void
18070 sd_sense_key_recoverable_error(struct sd_lun *un, uint8_t *sense_datap,
18071     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18072 {
18073         struct sd_sense_info    si;
18074         uint8_t asc = scsi_sense_asc(sense_datap);
18075         uint8_t ascq = scsi_sense_ascq(sense_datap);
18076 
18077         ASSERT(un != NULL);
18078         ASSERT(mutex_owned(SD_MUTEX(un)));
18079         ASSERT(bp != NULL);
18080         ASSERT(xp != NULL);
18081         ASSERT(pktp != NULL);
18082 
18083         /*
18084          * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
18085          */
18086         if (asc == 0x00 && ascq == 0x1D) {
18087                 sd_return_command(un, bp);
18088                 return;
18089         }
18090 
18091         /*
18092          * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18093          */
18094         if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18095                 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18096                 si.ssi_severity = SCSI_ERR_INFO;
18097                 si.ssi_pfa_flag = TRUE;
18098         } else {
18099                 SD_UPDATE_ERRSTATS(un, sd_softerrs);
18100                 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18101                 si.ssi_severity = SCSI_ERR_RECOVERED;
18102                 si.ssi_pfa_flag = FALSE;
18103         }
18104 
18105         if (pktp->pkt_resid == 0) {
18106                 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18107                 sd_return_command(un, bp);
18108                 return;
18109         }
18110 
18111         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18112             &si, EIO, (clock_t)0, NULL);
18113 }
18114 
18115 
18116 
18117 
18118 /*
18119  *    Function: sd_sense_key_not_ready
18120  *
18121  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18122  *
18123  *     Context: May be called from interrupt context
18124  */
18125 
18126 static void
18127 sd_sense_key_not_ready(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18128     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18129 {
18130         struct sd_sense_info    si;
18131         uint8_t asc = scsi_sense_asc(sense_datap);
18132         uint8_t ascq = scsi_sense_ascq(sense_datap);
18133 
18134         ASSERT(un != NULL);
18135         ASSERT(mutex_owned(SD_MUTEX(un)));
18136         ASSERT(bp != NULL);
18137         ASSERT(xp != NULL);
18138         ASSERT(pktp != NULL);
18139 
18140         si.ssi_severity = SCSI_ERR_FATAL;
18141         si.ssi_pfa_flag = FALSE;
18142 
18143         /*
18144          * Update error stats after first NOT READY error. Disks may have
18145          * been powered down and may need to be restarted.  For CDROMs,
18146          * report NOT READY errors only if media is present.
18147          */
18148         if ((ISCD(un) && (asc == 0x3A)) ||
18149             (xp->xb_nr_retry_count > 0)) {
18150                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18151                 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18152         }
18153 
18154         /*
18155          * Just fail if the "not ready" retry limit has been reached.
18156          */
18157         if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18158                 /* Special check for error message printing for removables. */
18159                 if (un->un_f_has_removable_media && (asc == 0x04) &&
18160                     (ascq >= 0x04)) {
18161                         si.ssi_severity = SCSI_ERR_ALL;
18162                 }
18163                 goto fail_command;
18164         }
18165 
18166         /*
18167          * Check the ASC and ASCQ in the sense data as needed, to determine
18168          * what to do.
18169          */
18170         switch (asc) {
18171         case 0x04:      /* LOGICAL UNIT NOT READY */
18172                 /*
18173                  * disk drives that don't spin up result in a very long delay
18174                  * in format without warning messages. We will log a message
18175                  * if the error level is set to verbose.
18176                  */
18177                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18178                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18179                             "logical unit not ready, resetting disk\n");
18180                 }
18181 
18182                 /*
18183                  * There are different requirements for CDROMs and disks for
18184                  * the number of retries.  If a CD-ROM is giving this, it is
18185                  * probably reading TOC and is in the process of getting
18186                  * ready, so we should keep on trying for a long time to make
18187                  * sure that all types of media are taken in account (for
18188                  * some media the drive takes a long time to read TOC).  For
18189                  * disks we do not want to retry this too many times as this
18190                  * can cause a long hang in format when the drive refuses to
18191                  * spin up (a very common failure).
18192                  */
18193                 switch (ascq) {
18194                 case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18195                         /*
18196                          * Disk drives frequently refuse to spin up which
18197                          * results in a very long hang in format without
18198                          * warning messages.
18199                          *
18200                          * Note: This code preserves the legacy behavior of
18201                          * comparing xb_nr_retry_count against zero for fibre
18202                          * channel targets instead of comparing against the
18203                          * un_reset_retry_count value.  The reason for this
18204                          * discrepancy has been so utterly lost beneath the
18205                          * Sands of Time that even Indiana Jones could not
18206                          * find it.
18207                          */
18208                         if (un->un_f_is_fibre == TRUE) {
18209                                 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18210                                     (xp->xb_nr_retry_count > 0)) &&
18211                                     (un->un_startstop_timeid == NULL)) {
18212                                         scsi_log(SD_DEVINFO(un), sd_label,
18213                                             CE_WARN, "logical unit not ready, "
18214                                             "resetting disk\n");
18215                                         sd_reset_target(un, pktp);
18216                                 }
18217                         } else {
18218                                 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18219                                     (xp->xb_nr_retry_count >
18220                                     un->un_reset_retry_count)) &&
18221                                     (un->un_startstop_timeid == NULL)) {
18222                                         scsi_log(SD_DEVINFO(un), sd_label,
18223                                             CE_WARN, "logical unit not ready, "
18224                                             "resetting disk\n");
18225                                         sd_reset_target(un, pktp);
18226                                 }
18227                         }
18228                         break;
18229 
18230                 case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18231                         /*
18232                          * If the target is in the process of becoming
18233                          * ready, just proceed with the retry. This can
18234                          * happen with CD-ROMs that take a long time to
18235                          * read TOC after a power cycle or reset.
18236                          */
18237                         goto do_retry;
18238 
18239                 case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18240                         break;
18241 
18242                 case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18243                         /*
18244                          * Retries cannot help here so just fail right away.
18245                          */
18246                         goto fail_command;
18247 
18248                 case 0x88:
18249                         /*
18250                          * Vendor-unique code for T3/T4: it indicates a
18251                          * path problem in a mutipathed config, but as far as
18252                          * the target driver is concerned it equates to a fatal
18253                          * error, so we should just fail the command right away
18254                          * (without printing anything to the console). If this
18255                          * is not a T3/T4, fall thru to the default recovery
18256                          * action.
18257                          * T3/T4 is FC only, don't need to check is_fibre
18258                          */
18259                         if (SD_IS_T3(un) || SD_IS_T4(un)) {
18260                                 sd_return_failed_command(un, bp, EIO);
18261                                 return;
18262                         }
18263                         /* FALLTHRU */
18264 
18265                 case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18266                 case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18267                 case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18268                 case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18269                 case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18270                 default:    /* Possible future codes in SCSI spec? */
18271                         /*
18272                          * For removable-media devices, do not retry if
18273                          * ASCQ > 2 as these result mostly from USCSI commands
18274                          * on MMC devices issued to check status of an
18275                          * operation initiated in immediate mode.  Also for
18276                          * ASCQ >= 4 do not print console messages as these
18277                          * mainly represent a user-initiated operation
18278                          * instead of a system failure.
18279                          */
18280                         if (un->un_f_has_removable_media) {
18281                                 si.ssi_severity = SCSI_ERR_ALL;
18282                                 goto fail_command;
18283                         }
18284                         break;
18285                 }
18286 
18287                 /*
18288                  * As part of our recovery attempt for the NOT READY
18289                  * condition, we issue a START STOP UNIT command. However
18290                  * we want to wait for a short delay before attempting this
18291                  * as there may still be more commands coming back from the
18292                  * target with the check condition. To do this we use
18293                  * timeout(9F) to call sd_start_stop_unit_callback() after
18294                  * the delay interval expires. (sd_start_stop_unit_callback()
18295                  * dispatches sd_start_stop_unit_task(), which will issue
18296                  * the actual START STOP UNIT command. The delay interval
18297                  * is one-half of the delay that we will use to retry the
18298                  * command that generated the NOT READY condition.
18299                  *
18300                  * Note that we could just dispatch sd_start_stop_unit_task()
18301                  * from here and allow it to sleep for the delay interval,
18302                  * but then we would be tying up the taskq thread
18303                  * uncesessarily for the duration of the delay.
18304                  *
18305                  * Do not issue the START STOP UNIT if the current command
18306                  * is already a START STOP UNIT.
18307                  */
18308                 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18309                         break;
18310                 }
18311 
18312                 /*
18313                  * Do not schedule the timeout if one is already pending.
18314                  */
18315                 if (un->un_startstop_timeid != NULL) {
18316                         SD_INFO(SD_LOG_ERROR, un,
18317                             "sd_sense_key_not_ready: restart already issued to"
18318                             " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18319                             ddi_get_instance(SD_DEVINFO(un)));
18320                         break;
18321                 }
18322 
18323                 /*
18324                  * Schedule the START STOP UNIT command, then queue the command
18325                  * for a retry.
18326                  *
18327                  * Note: A timeout is not scheduled for this retry because we
18328                  * want the retry to be serial with the START_STOP_UNIT. The
18329                  * retry will be started when the START_STOP_UNIT is completed
18330                  * in sd_start_stop_unit_task.
18331                  */
18332                 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18333                     un, un->un_busy_timeout / 2);
18334                 xp->xb_nr_retry_count++;
18335                 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18336                 return;
18337 
18338         case 0x05:      /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18339                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18340                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18341                             "unit does not respond to selection\n");
18342                 }
18343                 break;
18344 
18345         case 0x3A:      /* MEDIUM NOT PRESENT */
18346                 if (sd_error_level >= SCSI_ERR_FATAL) {
18347                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18348                             "Caddy not inserted in drive\n");
18349                 }
18350 
18351                 sr_ejected(un);
18352                 un->un_mediastate = DKIO_EJECTED;
18353                 /* The state has changed, inform the media watch routines */
18354                 cv_broadcast(&un->un_state_cv);
18355                 /* Just fail if no media is present in the drive. */
18356                 goto fail_command;
18357 
18358         default:
18359                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18360                         scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18361                             "Unit not Ready. Additional sense code 0x%x\n",
18362                             asc);
18363                 }
18364                 break;
18365         }
18366 
18367 do_retry:
18368 
18369         /*
18370          * Retry the command, as some targets may report NOT READY for
18371          * several seconds after being reset.
18372          */
18373         xp->xb_nr_retry_count++;
18374         si.ssi_severity = SCSI_ERR_RETRYABLE;
18375         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18376             &si, EIO, un->un_busy_timeout, NULL);
18377 
18378         return;
18379 
18380 fail_command:
18381         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18382         sd_return_failed_command(un, bp, EIO);
18383 }
18384 
18385 
18386 
18387 /*
18388  *    Function: sd_sense_key_medium_or_hardware_error
18389  *
18390  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18391  *              sense key.
18392  *
18393  *     Context: May be called from interrupt context
18394  */
18395 
18396 static void
18397 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, uint8_t *sense_datap,
18398     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18399 {
18400         struct sd_sense_info    si;
18401         uint8_t sense_key = scsi_sense_key(sense_datap);
18402         uint8_t asc = scsi_sense_asc(sense_datap);
18403 
18404         ASSERT(un != NULL);
18405         ASSERT(mutex_owned(SD_MUTEX(un)));
18406         ASSERT(bp != NULL);
18407         ASSERT(xp != NULL);
18408         ASSERT(pktp != NULL);
18409 
18410         si.ssi_severity = SCSI_ERR_FATAL;
18411         si.ssi_pfa_flag = FALSE;
18412 
18413         if (sense_key == KEY_MEDIUM_ERROR) {
18414                 SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18415         }
18416 
18417         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18418 
18419         if ((un->un_reset_retry_count != 0) &&
18420             (xp->xb_retry_count == un->un_reset_retry_count)) {
18421                 mutex_exit(SD_MUTEX(un));
18422                 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18423                 if (un->un_f_allow_bus_device_reset == TRUE) {
18424 
18425                         boolean_t try_resetting_target = B_TRUE;
18426 
18427                         /*
18428                          * We need to be able to handle specific ASC when we are
18429                          * handling a KEY_HARDWARE_ERROR. In particular
18430                          * taking the default action of resetting the target may
18431                          * not be the appropriate way to attempt recovery.
18432                          * Resetting a target because of a single LUN failure
18433                          * victimizes all LUNs on that target.
18434                          *
18435                          * This is true for the LSI arrays, if an LSI
18436                          * array controller returns an ASC of 0x84 (LUN Dead) we
18437                          * should trust it.
18438                          */
18439 
18440                         if (sense_key == KEY_HARDWARE_ERROR) {
18441                                 switch (asc) {
18442                                 case 0x84:
18443                                         if (SD_IS_LSI(un)) {
18444                                                 try_resetting_target = B_FALSE;
18445                                         }
18446                                         break;
18447                                 default:
18448                                         break;
18449                                 }
18450                         }
18451 
18452                         if (try_resetting_target == B_TRUE) {
18453                                 int reset_retval = 0;
18454                                 if (un->un_f_lun_reset_enabled == TRUE) {
18455                                         SD_TRACE(SD_LOG_IO_CORE, un,
18456                                             "sd_sense_key_medium_or_hardware_"
18457                                             "error: issuing RESET_LUN\n");
18458                                         reset_retval =
18459                                             scsi_reset(SD_ADDRESS(un),
18460                                             RESET_LUN);
18461                                 }
18462                                 if (reset_retval == 0) {
18463                                         SD_TRACE(SD_LOG_IO_CORE, un,
18464                                             "sd_sense_key_medium_or_hardware_"
18465                                             "error: issuing RESET_TARGET\n");
18466                                         (void) scsi_reset(SD_ADDRESS(un),
18467                                             RESET_TARGET);
18468                                 }
18469                         }
18470                 }
18471                 mutex_enter(SD_MUTEX(un));
18472         }
18473 
18474         /*
18475          * This really ought to be a fatal error, but we will retry anyway
18476          * as some drives report this as a spurious error.
18477          */
18478         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18479             &si, EIO, (clock_t)0, NULL);
18480 }
18481 
18482 
18483 
18484 /*
18485  *    Function: sd_sense_key_illegal_request
18486  *
18487  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18488  *
18489  *     Context: May be called from interrupt context
18490  */
18491 
18492 static void
18493 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18494     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18495 {
18496         struct sd_sense_info    si;
18497 
18498         ASSERT(un != NULL);
18499         ASSERT(mutex_owned(SD_MUTEX(un)));
18500         ASSERT(bp != NULL);
18501         ASSERT(xp != NULL);
18502         ASSERT(pktp != NULL);
18503 
18504         SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18505 
18506         si.ssi_severity = SCSI_ERR_INFO;
18507         si.ssi_pfa_flag = FALSE;
18508 
18509         /* Pointless to retry if the target thinks it's an illegal request */
18510         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18511         sd_return_failed_command(un, bp, EIO);
18512 }
18513 
18514 
18515 
18516 
18517 /*
18518  *    Function: sd_sense_key_unit_attention
18519  *
18520  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18521  *
18522  *     Context: May be called from interrupt context
18523  */
18524 
18525 static void
18526 sd_sense_key_unit_attention(struct sd_lun *un,  uint8_t *sense_datap,
18527     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18528 {
18529         /*
18530          * For UNIT ATTENTION we allow retries for one minute. Devices
18531          * like Sonoma can return UNIT ATTENTION close to a minute
18532          * under certain conditions.
18533          */
18534         int     retry_check_flag = SD_RETRIES_UA;
18535         boolean_t       kstat_updated = B_FALSE;
18536         struct  sd_sense_info           si;
18537         uint8_t asc = scsi_sense_asc(sense_datap);
18538         uint8_t ascq = scsi_sense_ascq(sense_datap);
18539 
18540         ASSERT(un != NULL);
18541         ASSERT(mutex_owned(SD_MUTEX(un)));
18542         ASSERT(bp != NULL);
18543         ASSERT(xp != NULL);
18544         ASSERT(pktp != NULL);
18545 
18546         si.ssi_severity = SCSI_ERR_INFO;
18547         si.ssi_pfa_flag = FALSE;
18548 
18549 
18550         switch (asc) {
18551         case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18552                 if (sd_report_pfa != 0) {
18553                         SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18554                         si.ssi_pfa_flag = TRUE;
18555                         retry_check_flag = SD_RETRIES_STANDARD;
18556                         goto do_retry;
18557                 }
18558 
18559                 break;
18560 
18561         case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18562                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18563                         un->un_resvd_status |=
18564                             (SD_LOST_RESERVE | SD_WANT_RESERVE);
18565                 }
18566 #ifdef _LP64
18567                 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18568                         if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18569                             un, KM_NOSLEEP) == 0) {
18570                                 /*
18571                                  * If we can't dispatch the task we'll just
18572                                  * live without descriptor sense.  We can
18573                                  * try again on the next "unit attention"
18574                                  */
18575                                 SD_ERROR(SD_LOG_ERROR, un,
18576                                     "sd_sense_key_unit_attention: "
18577                                     "Could not dispatch "
18578                                     "sd_reenable_dsense_task\n");
18579                         }
18580                 }
18581 #endif /* _LP64 */
18582                 /* FALLTHRU */
18583 
18584         case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18585                 if (!un->un_f_has_removable_media) {
18586                         break;
18587                 }
18588 
18589                 /*
18590                  * When we get a unit attention from a removable-media device,
18591                  * it may be in a state that will take a long time to recover
18592                  * (e.g., from a reset).  Since we are executing in interrupt
18593                  * context here, we cannot wait around for the device to come
18594                  * back. So hand this command off to sd_media_change_task()
18595                  * for deferred processing under taskq thread context. (Note
18596                  * that the command still may be failed if a problem is
18597                  * encountered at a later time.)
18598                  */
18599                 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18600                     KM_NOSLEEP) == 0) {
18601                         /*
18602                          * Cannot dispatch the request so fail the command.
18603                          */
18604                         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18605                         SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18606                         si.ssi_severity = SCSI_ERR_FATAL;
18607                         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18608                         sd_return_failed_command(un, bp, EIO);
18609                 }
18610 
18611                 /*
18612                  * If failed to dispatch sd_media_change_task(), we already
18613                  * updated kstat. If succeed to dispatch sd_media_change_task(),
18614                  * we should update kstat later if it encounters an error. So,
18615                  * we update kstat_updated flag here.
18616                  */
18617                 kstat_updated = B_TRUE;
18618 
18619                 /*
18620                  * Either the command has been successfully dispatched to a
18621                  * task Q for retrying, or the dispatch failed. In either case
18622                  * do NOT retry again by calling sd_retry_command. This sets up
18623                  * two retries of the same command and when one completes and
18624                  * frees the resources the other will access freed memory,
18625                  * a bad thing.
18626                  */
18627                 return;
18628 
18629         default:
18630                 break;
18631         }
18632 
18633         /*
18634          * ASC  ASCQ
18635          *  2A   09     Capacity data has changed
18636          *  2A   01     Mode parameters changed
18637          *  3F   0E     Reported luns data has changed
18638          * Arrays that support logical unit expansion should report
18639          * capacity changes(2Ah/09). Mode parameters changed and
18640          * reported luns data has changed are the approximation.
18641          */
18642         if (((asc == 0x2a) && (ascq == 0x09)) ||
18643             ((asc == 0x2a) && (ascq == 0x01)) ||
18644             ((asc == 0x3f) && (ascq == 0x0e))) {
18645                 if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18646                     KM_NOSLEEP) == 0) {
18647                         SD_ERROR(SD_LOG_ERROR, un,
18648                             "sd_sense_key_unit_attention: "
18649                             "Could not dispatch sd_target_change_task\n");
18650                 }
18651         }
18652 
18653         /*
18654          * Update kstat if we haven't done that.
18655          */
18656         if (!kstat_updated) {
18657                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18658                 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18659         }
18660 
18661 do_retry:
18662         sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18663             EIO, SD_UA_RETRY_DELAY, NULL);
18664 }
18665 
18666 
18667 
18668 /*
18669  *    Function: sd_sense_key_fail_command
18670  *
18671  * Description: Use to fail a command when we don't like the sense key that
18672  *              was returned.
18673  *
18674  *     Context: May be called from interrupt context
18675  */
18676 
18677 static void
18678 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18679     struct scsi_pkt *pktp)
18680 {
18681         struct sd_sense_info    si;
18682 
18683         ASSERT(un != NULL);
18684         ASSERT(mutex_owned(SD_MUTEX(un)));
18685         ASSERT(bp != NULL);
18686         ASSERT(xp != NULL);
18687         ASSERT(pktp != NULL);
18688 
18689         si.ssi_severity = SCSI_ERR_FATAL;
18690         si.ssi_pfa_flag = FALSE;
18691 
18692         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18693         sd_return_failed_command(un, bp, EIO);
18694 }
18695 
18696 
18697 
18698 /*
18699  *    Function: sd_sense_key_blank_check
18700  *
18701  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18702  *              Has no monetary connotation.
18703  *
18704  *     Context: May be called from interrupt context
18705  */
18706 
18707 static void
18708 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18709     struct scsi_pkt *pktp)
18710 {
18711         struct sd_sense_info    si;
18712 
18713         ASSERT(un != NULL);
18714         ASSERT(mutex_owned(SD_MUTEX(un)));
18715         ASSERT(bp != NULL);
18716         ASSERT(xp != NULL);
18717         ASSERT(pktp != NULL);
18718 
18719         /*
18720          * Blank check is not fatal for removable devices, therefore
18721          * it does not require a console message.
18722          */
18723         si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18724             SCSI_ERR_FATAL;
18725         si.ssi_pfa_flag = FALSE;
18726 
18727         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18728         sd_return_failed_command(un, bp, EIO);
18729 }
18730 
18731 
18732 
18733 
18734 /*
18735  *    Function: sd_sense_key_aborted_command
18736  *
18737  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18738  *
18739  *     Context: May be called from interrupt context
18740  */
18741 
18742 static void
18743 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18744     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18745 {
18746         struct sd_sense_info    si;
18747 
18748         ASSERT(un != NULL);
18749         ASSERT(mutex_owned(SD_MUTEX(un)));
18750         ASSERT(bp != NULL);
18751         ASSERT(xp != NULL);
18752         ASSERT(pktp != NULL);
18753 
18754         si.ssi_severity = SCSI_ERR_FATAL;
18755         si.ssi_pfa_flag = FALSE;
18756 
18757         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18758 
18759         /*
18760          * This really ought to be a fatal error, but we will retry anyway
18761          * as some drives report this as a spurious error.
18762          */
18763         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18764             &si, EIO, drv_usectohz(100000), NULL);
18765 }
18766 
18767 
18768 
18769 /*
18770  *    Function: sd_sense_key_default
18771  *
18772  * Description: Default recovery action for several SCSI sense keys (basically
18773  *              attempts a retry).
18774  *
18775  *     Context: May be called from interrupt context
18776  */
18777 
18778 static void
18779 sd_sense_key_default(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18780     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18781 {
18782         struct sd_sense_info    si;
18783         uint8_t sense_key = scsi_sense_key(sense_datap);
18784 
18785         ASSERT(un != NULL);
18786         ASSERT(mutex_owned(SD_MUTEX(un)));
18787         ASSERT(bp != NULL);
18788         ASSERT(xp != NULL);
18789         ASSERT(pktp != NULL);
18790 
18791         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18792 
18793         /*
18794          * Undecoded sense key. Attempt retries and hope that will fix
18795          * the problem.  Otherwise, we're dead.
18796          */
18797         if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18798                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18799                     "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18800         }
18801 
18802         si.ssi_severity = SCSI_ERR_FATAL;
18803         si.ssi_pfa_flag = FALSE;
18804 
18805         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18806             &si, EIO, (clock_t)0, NULL);
18807 }
18808 
18809 
18810 
18811 /*
18812  *    Function: sd_print_retry_msg
18813  *
18814  * Description: Print a message indicating the retry action being taken.
18815  *
18816  *   Arguments: un - ptr to associated softstate
18817  *              bp - ptr to buf(9S) for the command
18818  *              arg - not used.
18819  *              flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18820  *                      or SD_NO_RETRY_ISSUED
18821  *
18822  *     Context: May be called from interrupt context
18823  */
18824 /* ARGSUSED */
18825 static void
18826 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18827 {
18828         struct sd_xbuf  *xp;
18829         struct scsi_pkt *pktp;
18830         char *reasonp;
18831         char *msgp;
18832 
18833         ASSERT(un != NULL);
18834         ASSERT(mutex_owned(SD_MUTEX(un)));
18835         ASSERT(bp != NULL);
18836         pktp = SD_GET_PKTP(bp);
18837         ASSERT(pktp != NULL);
18838         xp = SD_GET_XBUF(bp);
18839         ASSERT(xp != NULL);
18840 
18841         ASSERT(!mutex_owned(&un->un_pm_mutex));
18842         mutex_enter(&un->un_pm_mutex);
18843         if ((un->un_state == SD_STATE_SUSPENDED) ||
18844             (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18845             (pktp->pkt_flags & FLAG_SILENT)) {
18846                 mutex_exit(&un->un_pm_mutex);
18847                 goto update_pkt_reason;
18848         }
18849         mutex_exit(&un->un_pm_mutex);
18850 
18851         /*
18852          * Suppress messages if they are all the same pkt_reason; with
18853          * TQ, many (up to 256) are returned with the same pkt_reason.
18854          * If we are in panic, then suppress the retry messages.
18855          */
18856         switch (flag) {
18857         case SD_NO_RETRY_ISSUED:
18858                 msgp = "giving up";
18859                 break;
18860         case SD_IMMEDIATE_RETRY_ISSUED:
18861         case SD_DELAYED_RETRY_ISSUED:
18862                 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18863                     ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18864                     (sd_error_level != SCSI_ERR_ALL))) {
18865                         return;
18866                 }
18867                 msgp = "retrying command";
18868                 break;
18869         default:
18870                 goto update_pkt_reason;
18871         }
18872 
18873         reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18874             scsi_rname(pktp->pkt_reason));
18875 
18876         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18877                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18878                     "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18879         }
18880 
18881 update_pkt_reason:
18882         /*
18883          * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18884          * This is to prevent multiple console messages for the same failure
18885          * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18886          * when the command is retried successfully because there still may be
18887          * more commands coming back with the same value of pktp->pkt_reason.
18888          */
18889         if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18890                 un->un_last_pkt_reason = pktp->pkt_reason;
18891         }
18892 }
18893 
18894 
18895 /*
18896  *    Function: sd_print_cmd_incomplete_msg
18897  *
18898  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18899  *
18900  *   Arguments: un - ptr to associated softstate
18901  *              bp - ptr to buf(9S) for the command
18902  *              arg - passed to sd_print_retry_msg()
18903  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18904  *                      or SD_NO_RETRY_ISSUED
18905  *
18906  *     Context: May be called from interrupt context
18907  */
18908 
18909 static void
18910 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18911     int code)
18912 {
18913         dev_info_t      *dip;
18914 
18915         ASSERT(un != NULL);
18916         ASSERT(mutex_owned(SD_MUTEX(un)));
18917         ASSERT(bp != NULL);
18918 
18919         switch (code) {
18920         case SD_NO_RETRY_ISSUED:
18921                 /* Command was failed. Someone turned off this target? */
18922                 if (un->un_state != SD_STATE_OFFLINE) {
18923                         /*
18924                          * Suppress message if we are detaching and
18925                          * device has been disconnected
18926                          * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18927                          * private interface and not part of the DDI
18928                          */
18929                         dip = un->un_sd->sd_dev;
18930                         if (!(DEVI_IS_DETACHING(dip) &&
18931                             DEVI_IS_DEVICE_REMOVED(dip))) {
18932                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18933                                 "disk not responding to selection\n");
18934                         }
18935                         New_state(un, SD_STATE_OFFLINE);
18936                 }
18937                 break;
18938 
18939         case SD_DELAYED_RETRY_ISSUED:
18940         case SD_IMMEDIATE_RETRY_ISSUED:
18941         default:
18942                 /* Command was successfully queued for retry */
18943                 sd_print_retry_msg(un, bp, arg, code);
18944                 break;
18945         }
18946 }
18947 
18948 
18949 /*
18950  *    Function: sd_pkt_reason_cmd_incomplete
18951  *
18952  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18953  *
18954  *     Context: May be called from interrupt context
18955  */
18956 
18957 static void
18958 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18959     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18960 {
18961         int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18962 
18963         ASSERT(un != NULL);
18964         ASSERT(mutex_owned(SD_MUTEX(un)));
18965         ASSERT(bp != NULL);
18966         ASSERT(xp != NULL);
18967         ASSERT(pktp != NULL);
18968 
18969         /* Do not do a reset if selection did not complete */
18970         /* Note: Should this not just check the bit? */
18971         if (pktp->pkt_state != STATE_GOT_BUS) {
18972                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18973                 sd_reset_target(un, pktp);
18974         }
18975 
18976         /*
18977          * If the target was not successfully selected, then set
18978          * SD_RETRIES_FAILFAST to indicate that we lost communication
18979          * with the target, and further retries and/or commands are
18980          * likely to take a long time.
18981          */
18982         if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18983                 flag |= SD_RETRIES_FAILFAST;
18984         }
18985 
18986         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18987 
18988         sd_retry_command(un, bp, flag,
18989             sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18990 }
18991 
18992 
18993 
18994 /*
18995  *    Function: sd_pkt_reason_cmd_tran_err
18996  *
18997  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18998  *
18999  *     Context: May be called from interrupt context
19000  */
19001 
19002 static void
19003 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19004     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19005 {
19006         ASSERT(un != NULL);
19007         ASSERT(mutex_owned(SD_MUTEX(un)));
19008         ASSERT(bp != NULL);
19009         ASSERT(xp != NULL);
19010         ASSERT(pktp != NULL);
19011 
19012         /*
19013          * Do not reset if we got a parity error, or if
19014          * selection did not complete.
19015          */
19016         SD_UPDATE_ERRSTATS(un, sd_harderrs);
19017         /* Note: Should this not just check the bit for pkt_state? */
19018         if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19019             (pktp->pkt_state != STATE_GOT_BUS)) {
19020                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19021                 sd_reset_target(un, pktp);
19022         }
19023 
19024         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19025 
19026         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19027             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19028 }
19029 
19030 
19031 
19032 /*
19033  *    Function: sd_pkt_reason_cmd_reset
19034  *
19035  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19036  *
19037  *     Context: May be called from interrupt context
19038  */
19039 
19040 static void
19041 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19042     struct scsi_pkt *pktp)
19043 {
19044         ASSERT(un != NULL);
19045         ASSERT(mutex_owned(SD_MUTEX(un)));
19046         ASSERT(bp != NULL);
19047         ASSERT(xp != NULL);
19048         ASSERT(pktp != NULL);
19049 
19050         /* The target may still be running the command, so try to reset. */
19051         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19052         sd_reset_target(un, pktp);
19053 
19054         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19055 
19056         /*
19057          * If pkt_reason is CMD_RESET chances are that this pkt got
19058          * reset because another target on this bus caused it. The target
19059          * that caused it should get CMD_TIMEOUT with pkt_statistics
19060          * of STAT_TIMEOUT/STAT_DEV_RESET.
19061          */
19062 
19063         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19064             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19065 }
19066 
19067 
19068 
19069 
19070 /*
19071  *    Function: sd_pkt_reason_cmd_aborted
19072  *
19073  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19074  *
19075  *     Context: May be called from interrupt context
19076  */
19077 
19078 static void
19079 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19080     struct scsi_pkt *pktp)
19081 {
19082         ASSERT(un != NULL);
19083         ASSERT(mutex_owned(SD_MUTEX(un)));
19084         ASSERT(bp != NULL);
19085         ASSERT(xp != NULL);
19086         ASSERT(pktp != NULL);
19087 
19088         /* The target may still be running the command, so try to reset. */
19089         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19090         sd_reset_target(un, pktp);
19091 
19092         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19093 
19094         /*
19095          * If pkt_reason is CMD_ABORTED chances are that this pkt got
19096          * aborted because another target on this bus caused it. The target
19097          * that caused it should get CMD_TIMEOUT with pkt_statistics
19098          * of STAT_TIMEOUT/STAT_DEV_RESET.
19099          */
19100 
19101         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19102             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19103 }
19104 
19105 
19106 
19107 /*
19108  *    Function: sd_pkt_reason_cmd_timeout
19109  *
19110  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19111  *
19112  *     Context: May be called from interrupt context
19113  */
19114 
19115 static void
19116 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19117     struct scsi_pkt *pktp)
19118 {
19119         ASSERT(un != NULL);
19120         ASSERT(mutex_owned(SD_MUTEX(un)));
19121         ASSERT(bp != NULL);
19122         ASSERT(xp != NULL);
19123         ASSERT(pktp != NULL);
19124 
19125 
19126         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19127         sd_reset_target(un, pktp);
19128 
19129         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19130 
19131         /*
19132          * A command timeout indicates that we could not establish
19133          * communication with the target, so set SD_RETRIES_FAILFAST
19134          * as further retries/commands are likely to take a long time.
19135          */
19136         sd_retry_command(un, bp,
19137             (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19138             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19139 }
19140 
19141 
19142 
19143 /*
19144  *    Function: sd_pkt_reason_cmd_unx_bus_free
19145  *
19146  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19147  *
19148  *     Context: May be called from interrupt context
19149  */
19150 
19151 static void
19152 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19153     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19154 {
19155         void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19156 
19157         ASSERT(un != NULL);
19158         ASSERT(mutex_owned(SD_MUTEX(un)));
19159         ASSERT(bp != NULL);
19160         ASSERT(xp != NULL);
19161         ASSERT(pktp != NULL);
19162 
19163         SD_UPDATE_ERRSTATS(un, sd_harderrs);
19164         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19165 
19166         funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19167             sd_print_retry_msg : NULL;
19168 
19169         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19170             funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19171 }
19172 
19173 
19174 /*
19175  *    Function: sd_pkt_reason_cmd_tag_reject
19176  *
19177  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19178  *
19179  *     Context: May be called from interrupt context
19180  */
19181 
19182 static void
19183 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19184     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19185 {
19186         ASSERT(un != NULL);
19187         ASSERT(mutex_owned(SD_MUTEX(un)));
19188         ASSERT(bp != NULL);
19189         ASSERT(xp != NULL);
19190         ASSERT(pktp != NULL);
19191 
19192         SD_UPDATE_ERRSTATS(un, sd_harderrs);
19193         pktp->pkt_flags = 0;
19194         un->un_tagflags = 0;
19195         if (un->un_f_opt_queueing == TRUE) {
19196                 un->un_throttle = min(un->un_throttle, 3);
19197         } else {
19198                 un->un_throttle = 1;
19199         }
19200         mutex_exit(SD_MUTEX(un));
19201         (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19202         mutex_enter(SD_MUTEX(un));
19203 
19204         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19205 
19206         /* Legacy behavior not to check retry counts here. */
19207         sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19208             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19209 }
19210 
19211 
19212 /*
19213  *    Function: sd_pkt_reason_default
19214  *
19215  * Description: Default recovery actions for SCSA pkt_reason values that
19216  *              do not have more explicit recovery actions.
19217  *
19218  *     Context: May be called from interrupt context
19219  */
19220 
19221 static void
19222 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19223     struct scsi_pkt *pktp)
19224 {
19225         ASSERT(un != NULL);
19226         ASSERT(mutex_owned(SD_MUTEX(un)));
19227         ASSERT(bp != NULL);
19228         ASSERT(xp != NULL);
19229         ASSERT(pktp != NULL);
19230 
19231         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19232         sd_reset_target(un, pktp);
19233 
19234         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19235 
19236         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19237             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19238 }
19239 
19240 
19241 
19242 /*
19243  *    Function: sd_pkt_status_check_condition
19244  *
19245  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19246  *
19247  *     Context: May be called from interrupt context
19248  */
19249 
19250 static void
19251 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19252     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19253 {
19254         ASSERT(un != NULL);
19255         ASSERT(mutex_owned(SD_MUTEX(un)));
19256         ASSERT(bp != NULL);
19257         ASSERT(xp != NULL);
19258         ASSERT(pktp != NULL);
19259 
19260         SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19261             "entry: buf:0x%p xp:0x%p\n", bp, xp);
19262 
19263         /*
19264          * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19265          * command will be retried after the request sense). Otherwise, retry
19266          * the command. Note: we are issuing the request sense even though the
19267          * retry limit may have been reached for the failed command.
19268          */
19269         if (un->un_f_arq_enabled == FALSE) {
19270                 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19271                     "no ARQ, sending request sense command\n");
19272                 sd_send_request_sense_command(un, bp, pktp);
19273         } else {
19274                 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19275                     "ARQ,retrying request sense command\n");
19276 #if defined(__i386) || defined(__amd64)
19277                 /*
19278                  * The SD_RETRY_DELAY value need to be adjusted here
19279                  * when SD_RETRY_DELAY change in sddef.h
19280                  */
19281                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19282                     un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19283                     NULL);
19284 #else
19285                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19286                     EIO, SD_RETRY_DELAY, NULL);
19287 #endif
19288         }
19289 
19290         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19291 }
19292 
19293 
19294 /*
19295  *    Function: sd_pkt_status_busy
19296  *
19297  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19298  *
19299  *     Context: May be called from interrupt context
19300  */
19301 
19302 static void
19303 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19304     struct scsi_pkt *pktp)
19305 {
19306         ASSERT(un != NULL);
19307         ASSERT(mutex_owned(SD_MUTEX(un)));
19308         ASSERT(bp != NULL);
19309         ASSERT(xp != NULL);
19310         ASSERT(pktp != NULL);
19311 
19312         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19313             "sd_pkt_status_busy: entry\n");
19314 
19315         /* If retries are exhausted, just fail the command. */
19316         if (xp->xb_retry_count >= un->un_busy_retry_count) {
19317                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19318                     "device busy too long\n");
19319                 sd_return_failed_command(un, bp, EIO);
19320                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19321                     "sd_pkt_status_busy: exit\n");
19322                 return;
19323         }
19324         xp->xb_retry_count++;
19325 
19326         /*
19327          * Try to reset the target. However, we do not want to perform
19328          * more than one reset if the device continues to fail. The reset
19329          * will be performed when the retry count reaches the reset
19330          * threshold.  This threshold should be set such that at least
19331          * one retry is issued before the reset is performed.
19332          */
19333         if (xp->xb_retry_count ==
19334             ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19335                 int rval = 0;
19336                 mutex_exit(SD_MUTEX(un));
19337                 if (un->un_f_allow_bus_device_reset == TRUE) {
19338                         /*
19339                          * First try to reset the LUN; if we cannot then
19340                          * try to reset the target.
19341                          */
19342                         if (un->un_f_lun_reset_enabled == TRUE) {
19343                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19344                                     "sd_pkt_status_busy: RESET_LUN\n");
19345                                 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19346                         }
19347                         if (rval == 0) {
19348                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19349                                     "sd_pkt_status_busy: RESET_TARGET\n");
19350                                 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19351                         }
19352                 }
19353                 if (rval == 0) {
19354                         /*
19355                          * If the RESET_LUN and/or RESET_TARGET failed,
19356                          * try RESET_ALL
19357                          */
19358                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19359                             "sd_pkt_status_busy: RESET_ALL\n");
19360                         rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19361                 }
19362                 mutex_enter(SD_MUTEX(un));
19363                 if (rval == 0) {
19364                         /*
19365                          * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19366                          * At this point we give up & fail the command.
19367                          */
19368                         sd_return_failed_command(un, bp, EIO);
19369                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19370                             "sd_pkt_status_busy: exit (failed cmd)\n");
19371                         return;
19372                 }
19373         }
19374 
19375         /*
19376          * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19377          * we have already checked the retry counts above.
19378          */
19379         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19380             EIO, un->un_busy_timeout, NULL);
19381 
19382         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19383             "sd_pkt_status_busy: exit\n");
19384 }
19385 
19386 
19387 /*
19388  *    Function: sd_pkt_status_reservation_conflict
19389  *
19390  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19391  *              command status.
19392  *
19393  *     Context: May be called from interrupt context
19394  */
19395 
19396 static void
19397 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19398     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19399 {
19400         ASSERT(un != NULL);
19401         ASSERT(mutex_owned(SD_MUTEX(un)));
19402         ASSERT(bp != NULL);
19403         ASSERT(xp != NULL);
19404         ASSERT(pktp != NULL);
19405 
19406         /*
19407          * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19408          * conflict could be due to various reasons like incorrect keys, not
19409          * registered or not reserved etc. So, we return EACCES to the caller.
19410          */
19411         if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19412                 int cmd = SD_GET_PKT_OPCODE(pktp);
19413                 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19414                     (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19415                         sd_return_failed_command(un, bp, EACCES);
19416                         return;
19417                 }
19418         }
19419 
19420         un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19421 
19422         if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19423                 if (sd_failfast_enable != 0) {
19424                         /* By definition, we must panic here.... */
19425                         sd_panic_for_res_conflict(un);
19426                         /*NOTREACHED*/
19427                 }
19428                 SD_ERROR(SD_LOG_IO, un,
19429                     "sd_handle_resv_conflict: Disk Reserved\n");
19430                 sd_return_failed_command(un, bp, EACCES);
19431                 return;
19432         }
19433 
19434         /*
19435          * 1147670: retry only if sd_retry_on_reservation_conflict
19436          * property is set (default is 1). Retries will not succeed
19437          * on a disk reserved by another initiator. HA systems
19438          * may reset this via sd.conf to avoid these retries.
19439          *
19440          * Note: The legacy return code for this failure is EIO, however EACCES
19441          * seems more appropriate for a reservation conflict.
19442          */
19443         if (sd_retry_on_reservation_conflict == 0) {
19444                 SD_ERROR(SD_LOG_IO, un,
19445                     "sd_handle_resv_conflict: Device Reserved\n");
19446                 sd_return_failed_command(un, bp, EIO);
19447                 return;
19448         }
19449 
19450         /*
19451          * Retry the command if we can.
19452          *
19453          * Note: The legacy return code for this failure is EIO, however EACCES
19454          * seems more appropriate for a reservation conflict.
19455          */
19456         sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19457             (clock_t)2, NULL);
19458 }
19459 
19460 
19461 
19462 /*
19463  *    Function: sd_pkt_status_qfull
19464  *
19465  * Description: Handle a QUEUE FULL condition from the target.  This can
19466  *              occur if the HBA does not handle the queue full condition.
19467  *              (Basically this means third-party HBAs as Sun HBAs will
19468  *              handle the queue full condition.)  Note that if there are
19469  *              some commands already in the transport, then the queue full
19470  *              has occurred because the queue for this nexus is actually
19471  *              full. If there are no commands in the transport, then the
19472  *              queue full is resulting from some other initiator or lun
19473  *              consuming all the resources at the target.
19474  *
19475  *     Context: May be called from interrupt context
19476  */
19477 
19478 static void
19479 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19480     struct scsi_pkt *pktp)
19481 {
19482         ASSERT(un != NULL);
19483         ASSERT(mutex_owned(SD_MUTEX(un)));
19484         ASSERT(bp != NULL);
19485         ASSERT(xp != NULL);
19486         ASSERT(pktp != NULL);
19487 
19488         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19489             "sd_pkt_status_qfull: entry\n");
19490 
19491         /*
19492          * Just lower the QFULL throttle and retry the command.  Note that
19493          * we do not limit the number of retries here.
19494          */
19495         sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19496         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19497             SD_RESTART_TIMEOUT, NULL);
19498 
19499         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19500             "sd_pkt_status_qfull: exit\n");
19501 }
19502 
19503 
19504 /*
19505  *    Function: sd_reset_target
19506  *
19507  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19508  *              RESET_TARGET, or RESET_ALL.
19509  *
19510  *     Context: May be called under interrupt context.
19511  */
19512 
19513 static void
19514 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19515 {
19516         int rval = 0;
19517 
19518         ASSERT(un != NULL);
19519         ASSERT(mutex_owned(SD_MUTEX(un)));
19520         ASSERT(pktp != NULL);
19521 
19522         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19523 
19524         /*
19525          * No need to reset if the transport layer has already done so.
19526          */
19527         if ((pktp->pkt_statistics &
19528             (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19529                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19530                     "sd_reset_target: no reset\n");
19531                 return;
19532         }
19533 
19534         mutex_exit(SD_MUTEX(un));
19535 
19536         if (un->un_f_allow_bus_device_reset == TRUE) {
19537                 if (un->un_f_lun_reset_enabled == TRUE) {
19538                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19539                             "sd_reset_target: RESET_LUN\n");
19540                         rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19541                 }
19542                 if (rval == 0) {
19543                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19544                             "sd_reset_target: RESET_TARGET\n");
19545                         rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19546                 }
19547         }
19548 
19549         if (rval == 0) {
19550                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19551                     "sd_reset_target: RESET_ALL\n");
19552                 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19553         }
19554 
19555         mutex_enter(SD_MUTEX(un));
19556 
19557         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19558 }
19559 
19560 /*
19561  *    Function: sd_target_change_task
19562  *
19563  * Description: Handle dynamic target change
19564  *
19565  *     Context: Executes in a taskq() thread context
19566  */
19567 static void
19568 sd_target_change_task(void *arg)
19569 {
19570         struct sd_lun           *un = arg;
19571         uint64_t                capacity;
19572         diskaddr_t              label_cap;
19573         uint_t                  lbasize;
19574         sd_ssc_t                *ssc;
19575 
19576         ASSERT(un != NULL);
19577         ASSERT(!mutex_owned(SD_MUTEX(un)));
19578 
19579         if ((un->un_f_blockcount_is_valid == FALSE) ||
19580             (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19581                 return;
19582         }
19583 
19584         ssc = sd_ssc_init(un);
19585 
19586         if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19587             &lbasize, SD_PATH_DIRECT) != 0) {
19588                 SD_ERROR(SD_LOG_ERROR, un,
19589                     "sd_target_change_task: fail to read capacity\n");
19590                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19591                 goto task_exit;
19592         }
19593 
19594         mutex_enter(SD_MUTEX(un));
19595         if (capacity <= un->un_blockcount) {
19596                 mutex_exit(SD_MUTEX(un));
19597                 goto task_exit;
19598         }
19599 
19600         sd_update_block_info(un, lbasize, capacity);
19601         mutex_exit(SD_MUTEX(un));
19602 
19603         /*
19604          * If lun is EFI labeled and lun capacity is greater than the
19605          * capacity contained in the label, log a sys event.
19606          */
19607         if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19608             (void*)SD_PATH_DIRECT) == 0) {
19609                 mutex_enter(SD_MUTEX(un));
19610                 if (un->un_f_blockcount_is_valid &&
19611                     un->un_blockcount > label_cap) {
19612                         mutex_exit(SD_MUTEX(un));
19613                         sd_log_lun_expansion_event(un, KM_SLEEP);
19614                 } else {
19615                         mutex_exit(SD_MUTEX(un));
19616                 }
19617         }
19618 
19619 task_exit:
19620         sd_ssc_fini(ssc);
19621 }
19622 
19623 
19624 /*
19625  *    Function: sd_log_dev_status_event
19626  *
19627  * Description: Log EC_dev_status sysevent
19628  *
19629  *     Context: Never called from interrupt context
19630  */
19631 static void
19632 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19633 {
19634         int err;
19635         char                    *path;
19636         nvlist_t                *attr_list;
19637 
19638         /* Allocate and build sysevent attribute list */
19639         err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19640         if (err != 0) {
19641                 SD_ERROR(SD_LOG_ERROR, un,
19642                     "sd_log_dev_status_event: fail to allocate space\n");
19643                 return;
19644         }
19645 
19646         path = kmem_alloc(MAXPATHLEN, km_flag);
19647         if (path == NULL) {
19648                 nvlist_free(attr_list);
19649                 SD_ERROR(SD_LOG_ERROR, un,
19650                     "sd_log_dev_status_event: fail to allocate space\n");
19651                 return;
19652         }
19653         /*
19654          * Add path attribute to identify the lun.
19655          * We are using minor node 'a' as the sysevent attribute.
19656          */
19657         (void) snprintf(path, MAXPATHLEN, "/devices");
19658         (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19659         (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19660             ":a");
19661 
19662         err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19663         if (err != 0) {
19664                 nvlist_free(attr_list);
19665                 kmem_free(path, MAXPATHLEN);
19666                 SD_ERROR(SD_LOG_ERROR, un,
19667                     "sd_log_dev_status_event: fail to add attribute\n");
19668                 return;
19669         }
19670 
19671         /* Log dynamic lun expansion sysevent */
19672         err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19673             esc, attr_list, NULL, km_flag);
19674         if (err != DDI_SUCCESS) {
19675                 SD_ERROR(SD_LOG_ERROR, un,
19676                     "sd_log_dev_status_event: fail to log sysevent\n");
19677         }
19678 
19679         nvlist_free(attr_list);
19680         kmem_free(path, MAXPATHLEN);
19681 }
19682 
19683 
19684 /*
19685  *    Function: sd_log_lun_expansion_event
19686  *
19687  * Description: Log lun expansion sys event
19688  *
19689  *     Context: Never called from interrupt context
19690  */
19691 static void
19692 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19693 {
19694         sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19695 }
19696 
19697 
19698 /*
19699  *    Function: sd_log_eject_request_event
19700  *
19701  * Description: Log eject request sysevent
19702  *
19703  *     Context: Never called from interrupt context
19704  */
19705 static void
19706 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19707 {
19708         sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19709 }
19710 
19711 
19712 /*
19713  *    Function: sd_media_change_task
19714  *
19715  * Description: Recovery action for CDROM to become available.
19716  *
19717  *     Context: Executes in a taskq() thread context
19718  */
19719 
19720 static void
19721 sd_media_change_task(void *arg)
19722 {
19723         struct  scsi_pkt        *pktp = arg;
19724         struct  sd_lun          *un;
19725         struct  buf             *bp;
19726         struct  sd_xbuf         *xp;
19727         int     err             = 0;
19728         int     retry_count     = 0;
19729         int     retry_limit     = SD_UNIT_ATTENTION_RETRY/10;
19730         struct  sd_sense_info   si;
19731 
19732         ASSERT(pktp != NULL);
19733         bp = (struct buf *)pktp->pkt_private;
19734         ASSERT(bp != NULL);
19735         xp = SD_GET_XBUF(bp);
19736         ASSERT(xp != NULL);
19737         un = SD_GET_UN(bp);
19738         ASSERT(un != NULL);
19739         ASSERT(!mutex_owned(SD_MUTEX(un)));
19740         ASSERT(un->un_f_monitor_media_state);
19741 
19742         si.ssi_severity = SCSI_ERR_INFO;
19743         si.ssi_pfa_flag = FALSE;
19744 
19745         /*
19746          * When a reset is issued on a CDROM, it takes a long time to
19747          * recover. First few attempts to read capacity and other things
19748          * related to handling unit attention fail (with a ASC 0x4 and
19749          * ASCQ 0x1). In that case we want to do enough retries and we want
19750          * to limit the retries in other cases of genuine failures like
19751          * no media in drive.
19752          */
19753         while (retry_count++ < retry_limit) {
19754                 if ((err = sd_handle_mchange(un)) == 0) {
19755                         break;
19756                 }
19757                 if (err == EAGAIN) {
19758                         retry_limit = SD_UNIT_ATTENTION_RETRY;
19759                 }
19760                 /* Sleep for 0.5 sec. & try again */
19761                 delay(drv_usectohz(500000));
19762         }
19763 
19764         /*
19765          * Dispatch (retry or fail) the original command here,
19766          * along with appropriate console messages....
19767          *
19768          * Must grab the mutex before calling sd_retry_command,
19769          * sd_print_sense_msg and sd_return_failed_command.
19770          */
19771         mutex_enter(SD_MUTEX(un));
19772         if (err != SD_CMD_SUCCESS) {
19773                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19774                 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19775                 si.ssi_severity = SCSI_ERR_FATAL;
19776                 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19777                 sd_return_failed_command(un, bp, EIO);
19778         } else {
19779                 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19780                     &si, EIO, (clock_t)0, NULL);
19781         }
19782         mutex_exit(SD_MUTEX(un));
19783 }
19784 
19785 
19786 
19787 /*
19788  *    Function: sd_handle_mchange
19789  *
19790  * Description: Perform geometry validation & other recovery when CDROM
19791  *              has been removed from drive.
19792  *
19793  * Return Code: 0 for success
19794  *              errno-type return code of either sd_send_scsi_DOORLOCK() or
19795  *              sd_send_scsi_READ_CAPACITY()
19796  *
19797  *     Context: Executes in a taskq() thread context
19798  */
19799 
19800 static int
19801 sd_handle_mchange(struct sd_lun *un)
19802 {
19803         uint64_t        capacity;
19804         uint32_t        lbasize;
19805         int             rval;
19806         sd_ssc_t        *ssc;
19807 
19808         ASSERT(!mutex_owned(SD_MUTEX(un)));
19809         ASSERT(un->un_f_monitor_media_state);
19810 
19811         ssc = sd_ssc_init(un);
19812         rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19813             SD_PATH_DIRECT_PRIORITY);
19814 
19815         if (rval != 0)
19816                 goto failed;
19817 
19818         mutex_enter(SD_MUTEX(un));
19819         sd_update_block_info(un, lbasize, capacity);
19820 
19821         if (un->un_errstats != NULL) {
19822                 struct  sd_errstats *stp =
19823                     (struct sd_errstats *)un->un_errstats->ks_data;
19824                 stp->sd_capacity.value.ui64 = (uint64_t)
19825                     ((uint64_t)un->un_blockcount *
19826                     (uint64_t)un->un_tgt_blocksize);
19827         }
19828 
19829         /*
19830          * Check if the media in the device is writable or not
19831          */
19832         if (ISCD(un)) {
19833                 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19834         }
19835 
19836         /*
19837          * Note: Maybe let the strategy/partitioning chain worry about getting
19838          * valid geometry.
19839          */
19840         mutex_exit(SD_MUTEX(un));
19841         cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19842 
19843 
19844         if (cmlb_validate(un->un_cmlbhandle, 0,
19845             (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19846                 sd_ssc_fini(ssc);
19847                 return (EIO);
19848         } else {
19849                 if (un->un_f_pkstats_enabled) {
19850                         sd_set_pstats(un);
19851                         SD_TRACE(SD_LOG_IO_PARTITION, un,
19852                             "sd_handle_mchange: un:0x%p pstats created and "
19853                             "set\n", un);
19854                 }
19855         }
19856 
19857         /*
19858          * Try to lock the door
19859          */
19860         rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19861             SD_PATH_DIRECT_PRIORITY);
19862 failed:
19863         if (rval != 0)
19864                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19865         sd_ssc_fini(ssc);
19866         return (rval);
19867 }
19868 
19869 
19870 /*
19871  *    Function: sd_send_scsi_DOORLOCK
19872  *
19873  * Description: Issue the scsi DOOR LOCK command
19874  *
19875  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19876  *                      structure for this target.
19877  *              flag  - SD_REMOVAL_ALLOW
19878  *                      SD_REMOVAL_PREVENT
19879  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19880  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19881  *                      to use the USCSI "direct" chain and bypass the normal
19882  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19883  *                      command is issued as part of an error recovery action.
19884  *
19885  * Return Code: 0   - Success
19886  *              errno return code from sd_ssc_send()
19887  *
19888  *     Context: Can sleep.
19889  */
19890 
19891 static int
19892 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19893 {
19894         struct scsi_extended_sense      sense_buf;
19895         union scsi_cdb          cdb;
19896         struct uscsi_cmd        ucmd_buf;
19897         int                     status;
19898         struct sd_lun           *un;
19899 
19900         ASSERT(ssc != NULL);
19901         un = ssc->ssc_un;
19902         ASSERT(un != NULL);
19903         ASSERT(!mutex_owned(SD_MUTEX(un)));
19904 
19905         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19906 
19907         /* already determined doorlock is not supported, fake success */
19908         if (un->un_f_doorlock_supported == FALSE) {
19909                 return (0);
19910         }
19911 
19912         /*
19913          * If we are ejecting and see an SD_REMOVAL_PREVENT
19914          * ignore the command so we can complete the eject
19915          * operation.
19916          */
19917         if (flag == SD_REMOVAL_PREVENT) {
19918                 mutex_enter(SD_MUTEX(un));
19919                 if (un->un_f_ejecting == TRUE) {
19920                         mutex_exit(SD_MUTEX(un));
19921                         return (EAGAIN);
19922                 }
19923                 mutex_exit(SD_MUTEX(un));
19924         }
19925 
19926         bzero(&cdb, sizeof (cdb));
19927         bzero(&ucmd_buf, sizeof (ucmd_buf));
19928 
19929         cdb.scc_cmd = SCMD_DOORLOCK;
19930         cdb.cdb_opaque[4] = (uchar_t)flag;
19931 
19932         ucmd_buf.uscsi_cdb      = (char *)&cdb;
19933         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
19934         ucmd_buf.uscsi_bufaddr  = NULL;
19935         ucmd_buf.uscsi_buflen   = 0;
19936         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
19937         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
19938         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
19939         ucmd_buf.uscsi_timeout  = 15;
19940 
19941         SD_TRACE(SD_LOG_IO, un,
19942             "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19943 
19944         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19945             UIO_SYSSPACE, path_flag);
19946 
19947         if (status == 0)
19948                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19949 
19950         if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19951             (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19952             (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19953                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19954 
19955                 /* fake success and skip subsequent doorlock commands */
19956                 un->un_f_doorlock_supported = FALSE;
19957                 return (0);
19958         }
19959 
19960         return (status);
19961 }
19962 
19963 /*
19964  *    Function: sd_send_scsi_READ_CAPACITY
19965  *
19966  * Description: This routine uses the scsi READ CAPACITY command to determine
19967  *              the device capacity in number of blocks and the device native
19968  *              block size. If this function returns a failure, then the
19969  *              values in *capp and *lbap are undefined.  If the capacity
19970  *              returned is 0xffffffff then the lun is too large for a
19971  *              normal READ CAPACITY command and the results of a
19972  *              READ CAPACITY 16 will be used instead.
19973  *
19974  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19975  *              capp - ptr to unsigned 64-bit variable to receive the
19976  *                      capacity value from the command.
19977  *              lbap - ptr to unsigned 32-bit varaible to receive the
19978  *                      block size value from the command
19979  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19980  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19981  *                      to use the USCSI "direct" chain and bypass the normal
19982  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19983  *                      command is issued as part of an error recovery action.
19984  *
19985  * Return Code: 0   - Success
19986  *              EIO - IO error
19987  *              EACCES - Reservation conflict detected
19988  *              EAGAIN - Device is becoming ready
19989  *              errno return code from sd_ssc_send()
19990  *
19991  *     Context: Can sleep.  Blocks until command completes.
19992  */
19993 
19994 #define SD_CAPACITY_SIZE        sizeof (struct scsi_capacity)
19995 
19996 static int
19997 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
19998     int path_flag)
19999 {
20000         struct  scsi_extended_sense     sense_buf;
20001         struct  uscsi_cmd       ucmd_buf;
20002         union   scsi_cdb        cdb;
20003         uint32_t                *capacity_buf;
20004         uint64_t                capacity;
20005         uint32_t                lbasize;
20006         uint32_t                pbsize;
20007         int                     status;
20008         struct sd_lun           *un;
20009 
20010         ASSERT(ssc != NULL);
20011 
20012         un = ssc->ssc_un;
20013         ASSERT(un != NULL);
20014         ASSERT(!mutex_owned(SD_MUTEX(un)));
20015         ASSERT(capp != NULL);
20016         ASSERT(lbap != NULL);
20017 
20018         SD_TRACE(SD_LOG_IO, un,
20019             "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20020 
20021         /*
20022          * First send a READ_CAPACITY command to the target.
20023          * (This command is mandatory under SCSI-2.)
20024          *
20025          * Set up the CDB for the READ_CAPACITY command.  The Partial
20026          * Medium Indicator bit is cleared.  The address field must be
20027          * zero if the PMI bit is zero.
20028          */
20029         bzero(&cdb, sizeof (cdb));
20030         bzero(&ucmd_buf, sizeof (ucmd_buf));
20031 
20032         capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20033 
20034         cdb.scc_cmd = SCMD_READ_CAPACITY;
20035 
20036         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20037         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
20038         ucmd_buf.uscsi_bufaddr  = (caddr_t)capacity_buf;
20039         ucmd_buf.uscsi_buflen   = SD_CAPACITY_SIZE;
20040         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20041         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
20042         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20043         ucmd_buf.uscsi_timeout  = 60;
20044 
20045         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20046             UIO_SYSSPACE, path_flag);
20047 
20048         switch (status) {
20049         case 0:
20050                 /* Return failure if we did not get valid capacity data. */
20051                 if (ucmd_buf.uscsi_resid != 0) {
20052                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20053                             "sd_send_scsi_READ_CAPACITY received invalid "
20054                             "capacity data");
20055                         kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20056                         return (EIO);
20057                 }
20058                 /*
20059                  * Read capacity and block size from the READ CAPACITY 10 data.
20060                  * This data may be adjusted later due to device specific
20061                  * issues.
20062                  *
20063                  * According to the SCSI spec, the READ CAPACITY 10
20064                  * command returns the following:
20065                  *
20066                  *  bytes 0-3: Maximum logical block address available.
20067                  *              (MSB in byte:0 & LSB in byte:3)
20068                  *
20069                  *  bytes 4-7: Block length in bytes
20070                  *              (MSB in byte:4 & LSB in byte:7)
20071                  *
20072                  */
20073                 capacity = BE_32(capacity_buf[0]);
20074                 lbasize = BE_32(capacity_buf[1]);
20075 
20076                 /*
20077                  * Done with capacity_buf
20078                  */
20079                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20080 
20081                 /*
20082                  * if the reported capacity is set to all 0xf's, then
20083                  * this disk is too large and requires SBC-2 commands.
20084                  * Reissue the request using READ CAPACITY 16.
20085                  */
20086                 if (capacity == 0xffffffff) {
20087                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20088                         status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20089                             &lbasize, &pbsize, path_flag);
20090                         if (status != 0) {
20091                                 return (status);
20092                         } else {
20093                                 goto rc16_done;
20094                         }
20095                 }
20096                 break;  /* Success! */
20097         case EIO:
20098                 switch (ucmd_buf.uscsi_status) {
20099                 case STATUS_RESERVATION_CONFLICT:
20100                         status = EACCES;
20101                         break;
20102                 case STATUS_CHECK:
20103                         /*
20104                          * Check condition; look for ASC/ASCQ of 0x04/0x01
20105                          * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20106                          */
20107                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20108                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20109                             (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20110                                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20111                                 return (EAGAIN);
20112                         }
20113                         break;
20114                 default:
20115                         break;
20116                 }
20117                 /* FALLTHRU */
20118         default:
20119                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20120                 return (status);
20121         }
20122 
20123         /*
20124          * Some ATAPI CD-ROM drives report inaccurate LBA size values
20125          * (2352 and 0 are common) so for these devices always force the value
20126          * to 2048 as required by the ATAPI specs.
20127          */
20128         if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20129                 lbasize = 2048;
20130         }
20131 
20132         /*
20133          * Get the maximum LBA value from the READ CAPACITY data.
20134          * Here we assume that the Partial Medium Indicator (PMI) bit
20135          * was cleared when issuing the command. This means that the LBA
20136          * returned from the device is the LBA of the last logical block
20137          * on the logical unit.  The actual logical block count will be
20138          * this value plus one.
20139          */
20140         capacity += 1;
20141 
20142         /*
20143          * Currently, for removable media, the capacity is saved in terms
20144          * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20145          */
20146         if (un->un_f_has_removable_media)
20147                 capacity *= (lbasize / un->un_sys_blocksize);
20148 
20149 rc16_done:
20150 
20151         /*
20152          * Copy the values from the READ CAPACITY command into the space
20153          * provided by the caller.
20154          */
20155         *capp = capacity;
20156         *lbap = lbasize;
20157 
20158         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20159             "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20160 
20161         /*
20162          * Both the lbasize and capacity from the device must be nonzero,
20163          * otherwise we assume that the values are not valid and return
20164          * failure to the caller. (4203735)
20165          */
20166         if ((capacity == 0) || (lbasize == 0)) {
20167                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20168                     "sd_send_scsi_READ_CAPACITY received invalid value "
20169                     "capacity %llu lbasize %d", capacity, lbasize);
20170                 return (EIO);
20171         }
20172         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20173         return (0);
20174 }
20175 
20176 /*
20177  *    Function: sd_send_scsi_READ_CAPACITY_16
20178  *
20179  * Description: This routine uses the scsi READ CAPACITY 16 command to
20180  *              determine the device capacity in number of blocks and the
20181  *              device native block size.  If this function returns a failure,
20182  *              then the values in *capp and *lbap are undefined.
20183  *              This routine should be called by sd_send_scsi_READ_CAPACITY
20184  *              which will apply any device specific adjustments to capacity
20185  *              and lbasize. One exception is it is also called by
20186  *              sd_get_media_info_ext. In that function, there is no need to
20187  *              adjust the capacity and lbasize.
20188  *
20189  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20190  *              capp - ptr to unsigned 64-bit variable to receive the
20191  *                      capacity value from the command.
20192  *              lbap - ptr to unsigned 32-bit varaible to receive the
20193  *                      block size value from the command
20194  *              psp  - ptr to unsigned 32-bit variable to receive the
20195  *                      physical block size value from the command
20196  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20197  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20198  *                      to use the USCSI "direct" chain and bypass the normal
20199  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when
20200  *                      this command is issued as part of an error recovery
20201  *                      action.
20202  *
20203  * Return Code: 0   - Success
20204  *              EIO - IO error
20205  *              EACCES - Reservation conflict detected
20206  *              EAGAIN - Device is becoming ready
20207  *              errno return code from sd_ssc_send()
20208  *
20209  *     Context: Can sleep.  Blocks until command completes.
20210  */
20211 
20212 #define SD_CAPACITY_16_SIZE     sizeof (struct scsi_capacity_16)
20213 
20214 static int
20215 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20216     uint32_t *psp, int path_flag)
20217 {
20218         struct  scsi_extended_sense     sense_buf;
20219         struct  uscsi_cmd       ucmd_buf;
20220         union   scsi_cdb        cdb;
20221         uint64_t                *capacity16_buf;
20222         uint64_t                capacity;
20223         uint32_t                lbasize;
20224         uint32_t                pbsize;
20225         uint32_t                lbpb_exp;
20226         int                     status;
20227         struct sd_lun           *un;
20228 
20229         ASSERT(ssc != NULL);
20230 
20231         un = ssc->ssc_un;
20232         ASSERT(un != NULL);
20233         ASSERT(!mutex_owned(SD_MUTEX(un)));
20234         ASSERT(capp != NULL);
20235         ASSERT(lbap != NULL);
20236 
20237         SD_TRACE(SD_LOG_IO, un,
20238             "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20239 
20240         /*
20241          * First send a READ_CAPACITY_16 command to the target.
20242          *
20243          * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20244          * Medium Indicator bit is cleared.  The address field must be
20245          * zero if the PMI bit is zero.
20246          */
20247         bzero(&cdb, sizeof (cdb));
20248         bzero(&ucmd_buf, sizeof (ucmd_buf));
20249 
20250         capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20251 
20252         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20253         ucmd_buf.uscsi_cdblen   = CDB_GROUP4;
20254         ucmd_buf.uscsi_bufaddr  = (caddr_t)capacity16_buf;
20255         ucmd_buf.uscsi_buflen   = SD_CAPACITY_16_SIZE;
20256         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20257         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
20258         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20259         ucmd_buf.uscsi_timeout  = 60;
20260 
20261         /*
20262          * Read Capacity (16) is a Service Action In command.  One
20263          * command byte (0x9E) is overloaded for multiple operations,
20264          * with the second CDB byte specifying the desired operation
20265          */
20266         cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20267         cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20268 
20269         /*
20270          * Fill in allocation length field
20271          */
20272         FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20273 
20274         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20275             UIO_SYSSPACE, path_flag);
20276 
20277         switch (status) {
20278         case 0:
20279                 /* Return failure if we did not get valid capacity data. */
20280                 if (ucmd_buf.uscsi_resid > 20) {
20281                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20282                             "sd_send_scsi_READ_CAPACITY_16 received invalid "
20283                             "capacity data");
20284                         kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20285                         return (EIO);
20286                 }
20287 
20288                 /*
20289                  * Read capacity and block size from the READ CAPACITY 16 data.
20290                  * This data may be adjusted later due to device specific
20291                  * issues.
20292                  *
20293                  * According to the SCSI spec, the READ CAPACITY 16
20294                  * command returns the following:
20295                  *
20296                  *  bytes 0-7: Maximum logical block address available.
20297                  *              (MSB in byte:0 & LSB in byte:7)
20298                  *
20299                  *  bytes 8-11: Block length in bytes
20300                  *              (MSB in byte:8 & LSB in byte:11)
20301                  *
20302                  *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20303                  */
20304                 capacity = BE_64(capacity16_buf[0]);
20305                 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20306                 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20307 
20308                 pbsize = lbasize << lbpb_exp;
20309 
20310                 /*
20311                  * Done with capacity16_buf
20312                  */
20313                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20314 
20315                 /*
20316                  * if the reported capacity is set to all 0xf's, then
20317                  * this disk is too large.  This could only happen with
20318                  * a device that supports LBAs larger than 64 bits which
20319                  * are not defined by any current T10 standards.
20320                  */
20321                 if (capacity == 0xffffffffffffffff) {
20322                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20323                             "disk is too large");
20324                         return (EIO);
20325                 }
20326                 break;  /* Success! */
20327         case EIO:
20328                 switch (ucmd_buf.uscsi_status) {
20329                 case STATUS_RESERVATION_CONFLICT:
20330                         status = EACCES;
20331                         break;
20332                 case STATUS_CHECK:
20333                         /*
20334                          * Check condition; look for ASC/ASCQ of 0x04/0x01
20335                          * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20336                          */
20337                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20338                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20339                             (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20340                                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20341                                 return (EAGAIN);
20342                         }
20343                         break;
20344                 default:
20345                         break;
20346                 }
20347                 /* FALLTHRU */
20348         default:
20349                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20350                 return (status);
20351         }
20352 
20353         /*
20354          * Some ATAPI CD-ROM drives report inaccurate LBA size values
20355          * (2352 and 0 are common) so for these devices always force the value
20356          * to 2048 as required by the ATAPI specs.
20357          */
20358         if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20359                 lbasize = 2048;
20360         }
20361 
20362         /*
20363          * Get the maximum LBA value from the READ CAPACITY 16 data.
20364          * Here we assume that the Partial Medium Indicator (PMI) bit
20365          * was cleared when issuing the command. This means that the LBA
20366          * returned from the device is the LBA of the last logical block
20367          * on the logical unit.  The actual logical block count will be
20368          * this value plus one.
20369          */
20370         capacity += 1;
20371 
20372         /*
20373          * Currently, for removable media, the capacity is saved in terms
20374          * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20375          */
20376         if (un->un_f_has_removable_media)
20377                 capacity *= (lbasize / un->un_sys_blocksize);
20378 
20379         *capp = capacity;
20380         *lbap = lbasize;
20381         *psp = pbsize;
20382 
20383         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20384             "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20385             capacity, lbasize, pbsize);
20386 
20387         if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20388                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20389                     "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20390                     "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20391                 return (EIO);
20392         }
20393 
20394         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20395         return (0);
20396 }
20397 
20398 
20399 /*
20400  *    Function: sd_send_scsi_START_STOP_UNIT
20401  *
20402  * Description: Issue a scsi START STOP UNIT command to the target.
20403  *
20404  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20405  *                       structure for this target.
20406  *      pc_flag - SD_POWER_CONDITION
20407  *                SD_START_STOP
20408  *              flag  - SD_TARGET_START
20409  *                      SD_TARGET_STOP
20410  *                      SD_TARGET_EJECT
20411  *                      SD_TARGET_CLOSE
20412  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20413  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20414  *                      to use the USCSI "direct" chain and bypass the normal
20415  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20416  *                      command is issued as part of an error recovery action.
20417  *
20418  * Return Code: 0   - Success
20419  *              EIO - IO error
20420  *              EACCES - Reservation conflict detected
20421  *              ENXIO  - Not Ready, medium not present
20422  *              errno return code from sd_ssc_send()
20423  *
20424  *     Context: Can sleep.
20425  */
20426 
20427 static int
20428 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20429     int path_flag)
20430 {
20431         struct  scsi_extended_sense     sense_buf;
20432         union scsi_cdb          cdb;
20433         struct uscsi_cmd        ucmd_buf;
20434         int                     status;
20435         struct sd_lun           *un;
20436 
20437         ASSERT(ssc != NULL);
20438         un = ssc->ssc_un;
20439         ASSERT(un != NULL);
20440         ASSERT(!mutex_owned(SD_MUTEX(un)));
20441 
20442         SD_TRACE(SD_LOG_IO, un,
20443             "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20444 
20445         if (un->un_f_check_start_stop &&
20446             (pc_flag == SD_START_STOP) &&
20447             ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20448             (un->un_f_start_stop_supported != TRUE)) {
20449                 return (0);
20450         }
20451 
20452         /*
20453          * If we are performing an eject operation and
20454          * we receive any command other than SD_TARGET_EJECT
20455          * we should immediately return.
20456          */
20457         if (flag != SD_TARGET_EJECT) {
20458                 mutex_enter(SD_MUTEX(un));
20459                 if (un->un_f_ejecting == TRUE) {
20460                         mutex_exit(SD_MUTEX(un));
20461                         return (EAGAIN);
20462                 }
20463                 mutex_exit(SD_MUTEX(un));
20464         }
20465 
20466         bzero(&cdb, sizeof (cdb));
20467         bzero(&ucmd_buf, sizeof (ucmd_buf));
20468         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20469 
20470         cdb.scc_cmd = SCMD_START_STOP;
20471         cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20472             (uchar_t)(flag << 4) : (uchar_t)flag;
20473 
20474         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20475         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20476         ucmd_buf.uscsi_bufaddr  = NULL;
20477         ucmd_buf.uscsi_buflen   = 0;
20478         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20479         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20480         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
20481         ucmd_buf.uscsi_timeout  = 200;
20482 
20483         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20484             UIO_SYSSPACE, path_flag);
20485 
20486         switch (status) {
20487         case 0:
20488                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20489                 break;  /* Success! */
20490         case EIO:
20491                 switch (ucmd_buf.uscsi_status) {
20492                 case STATUS_RESERVATION_CONFLICT:
20493                         status = EACCES;
20494                         break;
20495                 case STATUS_CHECK:
20496                         if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20497                                 switch (scsi_sense_key(
20498                                     (uint8_t *)&sense_buf)) {
20499                                 case KEY_ILLEGAL_REQUEST:
20500                                         status = ENOTSUP;
20501                                         break;
20502                                 case KEY_NOT_READY:
20503                                         if (scsi_sense_asc(
20504                                             (uint8_t *)&sense_buf)
20505                                             == 0x3A) {
20506                                                 status = ENXIO;
20507                                         }
20508                                         break;
20509                                 default:
20510                                         break;
20511                                 }
20512                         }
20513                         break;
20514                 default:
20515                         break;
20516                 }
20517                 break;
20518         default:
20519                 break;
20520         }
20521 
20522         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20523 
20524         return (status);
20525 }
20526 
20527 
20528 /*
20529  *    Function: sd_start_stop_unit_callback
20530  *
20531  * Description: timeout(9F) callback to begin recovery process for a
20532  *              device that has spun down.
20533  *
20534  *   Arguments: arg - pointer to associated softstate struct.
20535  *
20536  *     Context: Executes in a timeout(9F) thread context
20537  */
20538 
20539 static void
20540 sd_start_stop_unit_callback(void *arg)
20541 {
20542         struct sd_lun   *un = arg;
20543         ASSERT(un != NULL);
20544         ASSERT(!mutex_owned(SD_MUTEX(un)));
20545 
20546         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20547 
20548         (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20549 }
20550 
20551 
20552 /*
20553  *    Function: sd_start_stop_unit_task
20554  *
20555  * Description: Recovery procedure when a drive is spun down.
20556  *
20557  *   Arguments: arg - pointer to associated softstate struct.
20558  *
20559  *     Context: Executes in a taskq() thread context
20560  */
20561 
20562 static void
20563 sd_start_stop_unit_task(void *arg)
20564 {
20565         struct sd_lun   *un = arg;
20566         sd_ssc_t        *ssc;
20567         int             power_level;
20568         int             rval;
20569 
20570         ASSERT(un != NULL);
20571         ASSERT(!mutex_owned(SD_MUTEX(un)));
20572 
20573         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20574 
20575         /*
20576          * Some unformatted drives report not ready error, no need to
20577          * restart if format has been initiated.
20578          */
20579         mutex_enter(SD_MUTEX(un));
20580         if (un->un_f_format_in_progress == TRUE) {
20581                 mutex_exit(SD_MUTEX(un));
20582                 return;
20583         }
20584         mutex_exit(SD_MUTEX(un));
20585 
20586         ssc = sd_ssc_init(un);
20587         /*
20588          * When a START STOP command is issued from here, it is part of a
20589          * failure recovery operation and must be issued before any other
20590          * commands, including any pending retries. Thus it must be sent
20591          * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20592          * succeeds or not, we will start I/O after the attempt.
20593          * If power condition is supported and the current power level
20594          * is capable of performing I/O, we should set the power condition
20595          * to that level. Otherwise, set the power condition to ACTIVE.
20596          */
20597         if (un->un_f_power_condition_supported) {
20598                 mutex_enter(SD_MUTEX(un));
20599                 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20600                 power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20601                     > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20602                 mutex_exit(SD_MUTEX(un));
20603                 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20604                     sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20605         } else {
20606                 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20607                     SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20608         }
20609 
20610         if (rval != 0)
20611                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20612         sd_ssc_fini(ssc);
20613         /*
20614          * The above call blocks until the START_STOP_UNIT command completes.
20615          * Now that it has completed, we must re-try the original IO that
20616          * received the NOT READY condition in the first place. There are
20617          * three possible conditions here:
20618          *
20619          *  (1) The original IO is on un_retry_bp.
20620          *  (2) The original IO is on the regular wait queue, and un_retry_bp
20621          *      is NULL.
20622          *  (3) The original IO is on the regular wait queue, and un_retry_bp
20623          *      points to some other, unrelated bp.
20624          *
20625          * For each case, we must call sd_start_cmds() with un_retry_bp
20626          * as the argument. If un_retry_bp is NULL, this will initiate
20627          * processing of the regular wait queue.  If un_retry_bp is not NULL,
20628          * then this will process the bp on un_retry_bp. That may or may not
20629          * be the original IO, but that does not matter: the important thing
20630          * is to keep the IO processing going at this point.
20631          *
20632          * Note: This is a very specific error recovery sequence associated
20633          * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20634          * serialize the I/O with completion of the spin-up.
20635          */
20636         mutex_enter(SD_MUTEX(un));
20637         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20638             "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20639             un, un->un_retry_bp);
20640         un->un_startstop_timeid = NULL;      /* Timeout is no longer pending */
20641         sd_start_cmds(un, un->un_retry_bp);
20642         mutex_exit(SD_MUTEX(un));
20643 
20644         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20645 }
20646 
20647 
20648 /*
20649  *    Function: sd_send_scsi_INQUIRY
20650  *
20651  * Description: Issue the scsi INQUIRY command.
20652  *
20653  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20654  *                      structure for this target.
20655  *              bufaddr
20656  *              buflen
20657  *              evpd
20658  *              page_code
20659  *              page_length
20660  *
20661  * Return Code: 0   - Success
20662  *              errno return code from sd_ssc_send()
20663  *
20664  *     Context: Can sleep. Does not return until command is completed.
20665  */
20666 
20667 static int
20668 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20669     uchar_t evpd, uchar_t page_code, size_t *residp)
20670 {
20671         union scsi_cdb          cdb;
20672         struct uscsi_cmd        ucmd_buf;
20673         int                     status;
20674         struct sd_lun           *un;
20675 
20676         ASSERT(ssc != NULL);
20677         un = ssc->ssc_un;
20678         ASSERT(un != NULL);
20679         ASSERT(!mutex_owned(SD_MUTEX(un)));
20680         ASSERT(bufaddr != NULL);
20681 
20682         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20683 
20684         bzero(&cdb, sizeof (cdb));
20685         bzero(&ucmd_buf, sizeof (ucmd_buf));
20686         bzero(bufaddr, buflen);
20687 
20688         cdb.scc_cmd = SCMD_INQUIRY;
20689         cdb.cdb_opaque[1] = evpd;
20690         cdb.cdb_opaque[2] = page_code;
20691         FORMG0COUNT(&cdb, buflen);
20692 
20693         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20694         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20695         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
20696         ucmd_buf.uscsi_buflen   = buflen;
20697         ucmd_buf.uscsi_rqbuf    = NULL;
20698         ucmd_buf.uscsi_rqlen    = 0;
20699         ucmd_buf.uscsi_flags    = USCSI_READ | USCSI_SILENT;
20700         ucmd_buf.uscsi_timeout  = 200;  /* Excessive legacy value */
20701 
20702         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20703             UIO_SYSSPACE, SD_PATH_DIRECT);
20704 
20705         /*
20706          * Only handle status == 0, the upper-level caller
20707          * will put different assessment based on the context.
20708          */
20709         if (status == 0)
20710                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20711 
20712         if ((status == 0) && (residp != NULL)) {
20713                 *residp = ucmd_buf.uscsi_resid;
20714         }
20715 
20716         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20717 
20718         return (status);
20719 }
20720 
20721 
20722 /*
20723  *    Function: sd_send_scsi_TEST_UNIT_READY
20724  *
20725  * Description: Issue the scsi TEST UNIT READY command.
20726  *              This routine can be told to set the flag USCSI_DIAGNOSE to
20727  *              prevent retrying failed commands. Use this when the intent
20728  *              is either to check for device readiness, to clear a Unit
20729  *              Attention, or to clear any outstanding sense data.
20730  *              However under specific conditions the expected behavior
20731  *              is for retries to bring a device ready, so use the flag
20732  *              with caution.
20733  *
20734  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20735  *                      structure for this target.
20736  *              flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20737  *                      SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20738  *                      0: dont check for media present, do retries on cmd.
20739  *
20740  * Return Code: 0   - Success
20741  *              EIO - IO error
20742  *              EACCES - Reservation conflict detected
20743  *              ENXIO  - Not Ready, medium not present
20744  *              errno return code from sd_ssc_send()
20745  *
20746  *     Context: Can sleep. Does not return until command is completed.
20747  */
20748 
20749 static int
20750 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20751 {
20752         struct  scsi_extended_sense     sense_buf;
20753         union scsi_cdb          cdb;
20754         struct uscsi_cmd        ucmd_buf;
20755         int                     status;
20756         struct sd_lun           *un;
20757 
20758         ASSERT(ssc != NULL);
20759         un = ssc->ssc_un;
20760         ASSERT(un != NULL);
20761         ASSERT(!mutex_owned(SD_MUTEX(un)));
20762 
20763         SD_TRACE(SD_LOG_IO, un,
20764             "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20765 
20766         /*
20767          * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20768          * timeouts when they receive a TUR and the queue is not empty. Check
20769          * the configuration flag set during attach (indicating the drive has
20770          * this firmware bug) and un_ncmds_in_transport before issuing the
20771          * TUR. If there are
20772          * pending commands return success, this is a bit arbitrary but is ok
20773          * for non-removables (i.e. the eliteI disks) and non-clustering
20774          * configurations.
20775          */
20776         if (un->un_f_cfg_tur_check == TRUE) {
20777                 mutex_enter(SD_MUTEX(un));
20778                 if (un->un_ncmds_in_transport != 0) {
20779                         mutex_exit(SD_MUTEX(un));
20780                         return (0);
20781                 }
20782                 mutex_exit(SD_MUTEX(un));
20783         }
20784 
20785         bzero(&cdb, sizeof (cdb));
20786         bzero(&ucmd_buf, sizeof (ucmd_buf));
20787         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20788 
20789         cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20790 
20791         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20792         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20793         ucmd_buf.uscsi_bufaddr  = NULL;
20794         ucmd_buf.uscsi_buflen   = 0;
20795         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20796         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20797         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
20798 
20799         /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20800         if ((flag & SD_DONT_RETRY_TUR) != 0) {
20801                 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20802         }
20803         ucmd_buf.uscsi_timeout  = 60;
20804 
20805         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20806             UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20807             SD_PATH_STANDARD));
20808 
20809         switch (status) {
20810         case 0:
20811                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20812                 break;  /* Success! */
20813         case EIO:
20814                 switch (ucmd_buf.uscsi_status) {
20815                 case STATUS_RESERVATION_CONFLICT:
20816                         status = EACCES;
20817                         break;
20818                 case STATUS_CHECK:
20819                         if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20820                                 break;
20821                         }
20822                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20823                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20824                             KEY_NOT_READY) &&
20825                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20826                                 status = ENXIO;
20827                         }
20828                         break;
20829                 default:
20830                         break;
20831                 }
20832                 break;
20833         default:
20834                 break;
20835         }
20836 
20837         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20838 
20839         return (status);
20840 }
20841 
20842 /*
20843  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20844  *
20845  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20846  *
20847  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20848  *                      structure for this target.
20849  *
20850  * Return Code: 0   - Success
20851  *              EACCES
20852  *              ENOTSUP
20853  *              errno return code from sd_ssc_send()
20854  *
20855  *     Context: Can sleep. Does not return until command is completed.
20856  */
20857 
20858 static int
20859 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
20860     uint16_t data_len, uchar_t *data_bufp)
20861 {
20862         struct scsi_extended_sense      sense_buf;
20863         union scsi_cdb          cdb;
20864         struct uscsi_cmd        ucmd_buf;
20865         int                     status;
20866         int                     no_caller_buf = FALSE;
20867         struct sd_lun           *un;
20868 
20869         ASSERT(ssc != NULL);
20870         un = ssc->ssc_un;
20871         ASSERT(un != NULL);
20872         ASSERT(!mutex_owned(SD_MUTEX(un)));
20873         ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20874 
20875         SD_TRACE(SD_LOG_IO, un,
20876             "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20877 
20878         bzero(&cdb, sizeof (cdb));
20879         bzero(&ucmd_buf, sizeof (ucmd_buf));
20880         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20881         if (data_bufp == NULL) {
20882                 /* Allocate a default buf if the caller did not give one */
20883                 ASSERT(data_len == 0);
20884                 data_len  = MHIOC_RESV_KEY_SIZE;
20885                 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20886                 no_caller_buf = TRUE;
20887         }
20888 
20889         cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20890         cdb.cdb_opaque[1] = usr_cmd;
20891         FORMG1COUNT(&cdb, data_len);
20892 
20893         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20894         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
20895         ucmd_buf.uscsi_bufaddr  = (caddr_t)data_bufp;
20896         ucmd_buf.uscsi_buflen   = data_len;
20897         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20898         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20899         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20900         ucmd_buf.uscsi_timeout  = 60;
20901 
20902         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20903             UIO_SYSSPACE, SD_PATH_STANDARD);
20904 
20905         switch (status) {
20906         case 0:
20907                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20908 
20909                 break;  /* Success! */
20910         case EIO:
20911                 switch (ucmd_buf.uscsi_status) {
20912                 case STATUS_RESERVATION_CONFLICT:
20913                         status = EACCES;
20914                         break;
20915                 case STATUS_CHECK:
20916                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20917                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20918                             KEY_ILLEGAL_REQUEST)) {
20919                                 status = ENOTSUP;
20920                         }
20921                         break;
20922                 default:
20923                         break;
20924                 }
20925                 break;
20926         default:
20927                 break;
20928         }
20929 
20930         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20931 
20932         if (no_caller_buf == TRUE) {
20933                 kmem_free(data_bufp, data_len);
20934         }
20935 
20936         return (status);
20937 }
20938 
20939 
20940 /*
20941  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20942  *
20943  * Description: This routine is the driver entry point for handling CD-ROM
20944  *              multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20945  *              MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20946  *              device.
20947  *
20948  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
20949  *                      for the target.
20950  *              usr_cmd SCSI-3 reservation facility command (one of
20951  *                      SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20952  *                      SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
20953  *              usr_bufp - user provided pointer register, reserve descriptor or
20954  *                      preempt and abort structure (mhioc_register_t,
20955  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20956  *
20957  * Return Code: 0   - Success
20958  *              EACCES
20959  *              ENOTSUP
20960  *              errno return code from sd_ssc_send()
20961  *
20962  *     Context: Can sleep. Does not return until command is completed.
20963  */
20964 
20965 static int
20966 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20967     uchar_t *usr_bufp)
20968 {
20969         struct scsi_extended_sense      sense_buf;
20970         union scsi_cdb          cdb;
20971         struct uscsi_cmd        ucmd_buf;
20972         int                     status;
20973         uchar_t                 data_len = sizeof (sd_prout_t);
20974         sd_prout_t              *prp;
20975         struct sd_lun           *un;
20976 
20977         ASSERT(ssc != NULL);
20978         un = ssc->ssc_un;
20979         ASSERT(un != NULL);
20980         ASSERT(!mutex_owned(SD_MUTEX(un)));
20981         ASSERT(data_len == 24); /* required by scsi spec */
20982 
20983         SD_TRACE(SD_LOG_IO, un,
20984             "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20985 
20986         if (usr_bufp == NULL) {
20987                 return (EINVAL);
20988         }
20989 
20990         bzero(&cdb, sizeof (cdb));
20991         bzero(&ucmd_buf, sizeof (ucmd_buf));
20992         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20993         prp = kmem_zalloc(data_len, KM_SLEEP);
20994 
20995         cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20996         cdb.cdb_opaque[1] = usr_cmd;
20997         FORMG1COUNT(&cdb, data_len);
20998 
20999         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21000         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
21001         ucmd_buf.uscsi_bufaddr  = (caddr_t)prp;
21002         ucmd_buf.uscsi_buflen   = data_len;
21003         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21004         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21005         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21006         ucmd_buf.uscsi_timeout  = 60;
21007 
21008         switch (usr_cmd) {
21009         case SD_SCSI3_REGISTER: {
21010                 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21011 
21012                 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21013                 bcopy(ptr->newkey.key, prp->service_key,
21014                     MHIOC_RESV_KEY_SIZE);
21015                 prp->aptpl = ptr->aptpl;
21016                 break;
21017         }
21018         case SD_SCSI3_CLEAR: {
21019                 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21020 
21021                 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21022                 break;
21023         }
21024         case SD_SCSI3_RESERVE:
21025         case SD_SCSI3_RELEASE: {
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                 prp->scope_address = BE_32(ptr->scope_specific_addr);
21030                 cdb.cdb_opaque[2] = ptr->type;
21031                 break;
21032         }
21033         case SD_SCSI3_PREEMPTANDABORT: {
21034                 mhioc_preemptandabort_t *ptr =
21035                     (mhioc_preemptandabort_t *)usr_bufp;
21036 
21037                 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21038                 bcopy(ptr->victim_key.key, prp->service_key,
21039                     MHIOC_RESV_KEY_SIZE);
21040                 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21041                 cdb.cdb_opaque[2] = ptr->resvdesc.type;
21042                 ucmd_buf.uscsi_flags |= USCSI_HEAD;
21043                 break;
21044         }
21045         case SD_SCSI3_REGISTERANDIGNOREKEY:
21046         {
21047                 mhioc_registerandignorekey_t *ptr;
21048                 ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21049                 bcopy(ptr->newkey.key,
21050                     prp->service_key, MHIOC_RESV_KEY_SIZE);
21051                 prp->aptpl = ptr->aptpl;
21052                 break;
21053         }
21054         default:
21055                 ASSERT(FALSE);
21056                 break;
21057         }
21058 
21059         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21060             UIO_SYSSPACE, SD_PATH_STANDARD);
21061 
21062         switch (status) {
21063         case 0:
21064                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21065                 break;  /* Success! */
21066         case EIO:
21067                 switch (ucmd_buf.uscsi_status) {
21068                 case STATUS_RESERVATION_CONFLICT:
21069                         status = EACCES;
21070                         break;
21071                 case STATUS_CHECK:
21072                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21073                             (scsi_sense_key((uint8_t *)&sense_buf) ==
21074                             KEY_ILLEGAL_REQUEST)) {
21075                                 status = ENOTSUP;
21076                         }
21077                         break;
21078                 default:
21079                         break;
21080                 }
21081                 break;
21082         default:
21083                 break;
21084         }
21085 
21086         kmem_free(prp, data_len);
21087         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21088         return (status);
21089 }
21090 
21091 
21092 /*
21093  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
21094  *
21095  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21096  *
21097  *   Arguments: un - pointer to the target's soft state struct
21098  *              dkc - pointer to the callback structure
21099  *
21100  * Return Code: 0 - success
21101  *              errno-type error code
21102  *
21103  *     Context: kernel thread context only.
21104  *
21105  *  _______________________________________________________________
21106  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
21107  * |FLUSH_VOLATILE|              | operation                       |
21108  * |______________|______________|_________________________________|
21109  * | 0            | NULL         | Synchronous flush on both       |
21110  * |              |              | volatile and non-volatile cache |
21111  * |______________|______________|_________________________________|
21112  * | 1            | NULL         | Synchronous flush on volatile   |
21113  * |              |              | cache; disk drivers may suppress|
21114  * |              |              | flush if disk table indicates   |
21115  * |              |              | non-volatile cache              |
21116  * |______________|______________|_________________________________|
21117  * | 0            | !NULL        | Asynchronous flush on both      |
21118  * |              |              | volatile and non-volatile cache;|
21119  * |______________|______________|_________________________________|
21120  * | 1            | !NULL        | Asynchronous flush on volatile  |
21121  * |              |              | cache; disk drivers may suppress|
21122  * |              |              | flush if disk table indicates   |
21123  * |              |              | non-volatile cache              |
21124  * |______________|______________|_________________________________|
21125  *
21126  */
21127 
21128 static int
21129 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21130 {
21131         struct sd_uscsi_info    *uip;
21132         struct uscsi_cmd        *uscmd;
21133         union scsi_cdb          *cdb;
21134         struct buf              *bp;
21135         int                     rval = 0;
21136         int                     is_async;
21137 
21138         SD_TRACE(SD_LOG_IO, un,
21139             "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21140 
21141         ASSERT(un != NULL);
21142         ASSERT(!mutex_owned(SD_MUTEX(un)));
21143 
21144         if (dkc == NULL || dkc->dkc_callback == NULL) {
21145                 is_async = FALSE;
21146         } else {
21147                 is_async = TRUE;
21148         }
21149 
21150         mutex_enter(SD_MUTEX(un));
21151         /* check whether cache flush should be suppressed */
21152         if (un->un_f_suppress_cache_flush == TRUE) {
21153                 mutex_exit(SD_MUTEX(un));
21154                 /*
21155                  * suppress the cache flush if the device is told to do
21156                  * so by sd.conf or disk table
21157                  */
21158                 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21159                     skip the cache flush since suppress_cache_flush is %d!\n",
21160                     un->un_f_suppress_cache_flush);
21161 
21162                 if (is_async == TRUE) {
21163                         /* invoke callback for asynchronous flush */
21164                         (*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21165                 }
21166                 return (rval);
21167         }
21168         mutex_exit(SD_MUTEX(un));
21169 
21170         /*
21171          * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21172          * set properly
21173          */
21174         cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21175         cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21176 
21177         mutex_enter(SD_MUTEX(un));
21178         if (dkc != NULL && un->un_f_sync_nv_supported &&
21179             (dkc->dkc_flag & FLUSH_VOLATILE)) {
21180                 /*
21181                  * if the device supports SYNC_NV bit, turn on
21182                  * the SYNC_NV bit to only flush volatile cache
21183                  */
21184                 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21185         }
21186         mutex_exit(SD_MUTEX(un));
21187 
21188         /*
21189          * First get some memory for the uscsi_cmd struct and cdb
21190          * and initialize for SYNCHRONIZE_CACHE cmd.
21191          */
21192         uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21193         uscmd->uscsi_cdblen = CDB_GROUP1;
21194         uscmd->uscsi_cdb = (caddr_t)cdb;
21195         uscmd->uscsi_bufaddr = NULL;
21196         uscmd->uscsi_buflen = 0;
21197         uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21198         uscmd->uscsi_rqlen = SENSE_LENGTH;
21199         uscmd->uscsi_rqresid = SENSE_LENGTH;
21200         uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21201         uscmd->uscsi_timeout = sd_io_time;
21202 
21203         /*
21204          * Allocate an sd_uscsi_info struct and fill it with the info
21205          * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21206          * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21207          * since we allocate the buf here in this function, we do not
21208          * need to preserve the prior contents of b_private.
21209          * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21210          */
21211         uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21212         uip->ui_flags = SD_PATH_DIRECT;
21213         uip->ui_cmdp  = uscmd;
21214 
21215         bp = getrbuf(KM_SLEEP);
21216         bp->b_private = uip;
21217 
21218         /*
21219          * Setup buffer to carry uscsi request.
21220          */
21221         bp->b_flags  = B_BUSY;
21222         bp->b_bcount = 0;
21223         bp->b_blkno  = 0;
21224 
21225         if (is_async == TRUE) {
21226                 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21227                 uip->ui_dkc = *dkc;
21228         }
21229 
21230         bp->b_edev = SD_GET_DEV(un);
21231         bp->b_dev = cmpdev(bp->b_edev);   /* maybe unnecessary? */
21232 
21233         /*
21234          * Unset un_f_sync_cache_required flag
21235          */
21236         mutex_enter(SD_MUTEX(un));
21237         un->un_f_sync_cache_required = FALSE;
21238         mutex_exit(SD_MUTEX(un));
21239 
21240         (void) sd_uscsi_strategy(bp);
21241 
21242         /*
21243          * If synchronous request, wait for completion
21244          * If async just return and let b_iodone callback
21245          * cleanup.
21246          * NOTE: On return, u_ncmds_in_driver will be decremented,
21247          * but it was also incremented in sd_uscsi_strategy(), so
21248          * we should be ok.
21249          */
21250         if (is_async == FALSE) {
21251                 (void) biowait(bp);
21252                 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21253         }
21254 
21255         return (rval);
21256 }
21257 
21258 
21259 static int
21260 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21261 {
21262         struct sd_uscsi_info *uip;
21263         struct uscsi_cmd *uscmd;
21264         uint8_t *sense_buf;
21265         struct sd_lun *un;
21266         int status;
21267         union scsi_cdb *cdb;
21268 
21269         uip = (struct sd_uscsi_info *)(bp->b_private);
21270         ASSERT(uip != NULL);
21271 
21272         uscmd = uip->ui_cmdp;
21273         ASSERT(uscmd != NULL);
21274 
21275         sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21276         ASSERT(sense_buf != NULL);
21277 
21278         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21279         ASSERT(un != NULL);
21280 
21281         cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21282 
21283         status = geterror(bp);
21284         switch (status) {
21285         case 0:
21286                 break;  /* Success! */
21287         case EIO:
21288                 switch (uscmd->uscsi_status) {
21289                 case STATUS_RESERVATION_CONFLICT:
21290                         /* Ignore reservation conflict */
21291                         status = 0;
21292                         goto done;
21293 
21294                 case STATUS_CHECK:
21295                         if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21296                             (scsi_sense_key(sense_buf) ==
21297                             KEY_ILLEGAL_REQUEST)) {
21298                                 /* Ignore Illegal Request error */
21299                                 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21300                                         mutex_enter(SD_MUTEX(un));
21301                                         un->un_f_sync_nv_supported = FALSE;
21302                                         mutex_exit(SD_MUTEX(un));
21303                                         status = 0;
21304                                         SD_TRACE(SD_LOG_IO, un,
21305                                             "un_f_sync_nv_supported \
21306                                             is set to false.\n");
21307                                         goto done;
21308                                 }
21309 
21310                                 mutex_enter(SD_MUTEX(un));
21311                                 un->un_f_sync_cache_supported = FALSE;
21312                                 mutex_exit(SD_MUTEX(un));
21313                                 SD_TRACE(SD_LOG_IO, un,
21314                                     "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21315                                     un_f_sync_cache_supported set to false \
21316                                     with asc = %x, ascq = %x\n",
21317                                     scsi_sense_asc(sense_buf),
21318                                     scsi_sense_ascq(sense_buf));
21319                                 status = ENOTSUP;
21320                                 goto done;
21321                         }
21322                         break;
21323                 default:
21324                         break;
21325                 }
21326                 /* FALLTHRU */
21327         default:
21328                 /*
21329                  * Turn on the un_f_sync_cache_required flag
21330                  * since the SYNC CACHE command failed
21331                  */
21332                 mutex_enter(SD_MUTEX(un));
21333                 un->un_f_sync_cache_required = TRUE;
21334                 mutex_exit(SD_MUTEX(un));
21335 
21336                 /*
21337                  * Don't log an error message if this device
21338                  * has removable media.
21339                  */
21340                 if (!un->un_f_has_removable_media) {
21341                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21342                             "SYNCHRONIZE CACHE command failed (%d)\n", status);
21343                 }
21344                 break;
21345         }
21346 
21347 done:
21348         if (uip->ui_dkc.dkc_callback != NULL) {
21349                 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21350         }
21351 
21352         ASSERT((bp->b_flags & B_REMAPPED) == 0);
21353         freerbuf(bp);
21354         kmem_free(uip, sizeof (struct sd_uscsi_info));
21355         kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21356         kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21357         kmem_free(uscmd, sizeof (struct uscsi_cmd));
21358 
21359         return (status);
21360 }
21361 
21362 
21363 /*
21364  *    Function: sd_send_scsi_GET_CONFIGURATION
21365  *
21366  * Description: Issues the get configuration command to the device.
21367  *              Called from sd_check_for_writable_cd & sd_get_media_info
21368  *              caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21369  *   Arguments: ssc
21370  *              ucmdbuf
21371  *              rqbuf
21372  *              rqbuflen
21373  *              bufaddr
21374  *              buflen
21375  *              path_flag
21376  *
21377  * Return Code: 0   - Success
21378  *              errno return code from sd_ssc_send()
21379  *
21380  *     Context: Can sleep. Does not return until command is completed.
21381  *
21382  */
21383 
21384 static int
21385 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21386     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21387     int path_flag)
21388 {
21389         char    cdb[CDB_GROUP1];
21390         int     status;
21391         struct sd_lun   *un;
21392 
21393         ASSERT(ssc != NULL);
21394         un = ssc->ssc_un;
21395         ASSERT(un != NULL);
21396         ASSERT(!mutex_owned(SD_MUTEX(un)));
21397         ASSERT(bufaddr != NULL);
21398         ASSERT(ucmdbuf != NULL);
21399         ASSERT(rqbuf != NULL);
21400 
21401         SD_TRACE(SD_LOG_IO, un,
21402             "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21403 
21404         bzero(cdb, sizeof (cdb));
21405         bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21406         bzero(rqbuf, rqbuflen);
21407         bzero(bufaddr, buflen);
21408 
21409         /*
21410          * Set up cdb field for the get configuration command.
21411          */
21412         cdb[0] = SCMD_GET_CONFIGURATION;
21413         cdb[1] = 0x02;  /* Requested Type */
21414         cdb[8] = SD_PROFILE_HEADER_LEN;
21415         ucmdbuf->uscsi_cdb = cdb;
21416         ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21417         ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21418         ucmdbuf->uscsi_buflen = buflen;
21419         ucmdbuf->uscsi_timeout = sd_io_time;
21420         ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21421         ucmdbuf->uscsi_rqlen = rqbuflen;
21422         ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21423 
21424         status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21425             UIO_SYSSPACE, path_flag);
21426 
21427         switch (status) {
21428         case 0:
21429                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21430                 break;  /* Success! */
21431         case EIO:
21432                 switch (ucmdbuf->uscsi_status) {
21433                 case STATUS_RESERVATION_CONFLICT:
21434                         status = EACCES;
21435                         break;
21436                 default:
21437                         break;
21438                 }
21439                 break;
21440         default:
21441                 break;
21442         }
21443 
21444         if (status == 0) {
21445                 SD_DUMP_MEMORY(un, SD_LOG_IO,
21446                     "sd_send_scsi_GET_CONFIGURATION: data",
21447                     (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21448         }
21449 
21450         SD_TRACE(SD_LOG_IO, un,
21451             "sd_send_scsi_GET_CONFIGURATION: exit\n");
21452 
21453         return (status);
21454 }
21455 
21456 /*
21457  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21458  *
21459  * Description: Issues the get configuration command to the device to
21460  *              retrieve a specific feature. Called from
21461  *              sd_check_for_writable_cd & sd_set_mmc_caps.
21462  *   Arguments: ssc
21463  *              ucmdbuf
21464  *              rqbuf
21465  *              rqbuflen
21466  *              bufaddr
21467  *              buflen
21468  *              feature
21469  *
21470  * Return Code: 0   - Success
21471  *              errno return code from sd_ssc_send()
21472  *
21473  *     Context: Can sleep. Does not return until command is completed.
21474  *
21475  */
21476 static int
21477 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21478     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21479     char feature, int path_flag)
21480 {
21481         char    cdb[CDB_GROUP1];
21482         int     status;
21483         struct sd_lun   *un;
21484 
21485         ASSERT(ssc != NULL);
21486         un = ssc->ssc_un;
21487         ASSERT(un != NULL);
21488         ASSERT(!mutex_owned(SD_MUTEX(un)));
21489         ASSERT(bufaddr != NULL);
21490         ASSERT(ucmdbuf != NULL);
21491         ASSERT(rqbuf != NULL);
21492 
21493         SD_TRACE(SD_LOG_IO, un,
21494             "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21495 
21496         bzero(cdb, sizeof (cdb));
21497         bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21498         bzero(rqbuf, rqbuflen);
21499         bzero(bufaddr, buflen);
21500 
21501         /*
21502          * Set up cdb field for the get configuration command.
21503          */
21504         cdb[0] = SCMD_GET_CONFIGURATION;
21505         cdb[1] = 0x02;  /* Requested Type */
21506         cdb[3] = feature;
21507         cdb[8] = buflen;
21508         ucmdbuf->uscsi_cdb = cdb;
21509         ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21510         ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21511         ucmdbuf->uscsi_buflen = buflen;
21512         ucmdbuf->uscsi_timeout = sd_io_time;
21513         ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21514         ucmdbuf->uscsi_rqlen = rqbuflen;
21515         ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21516 
21517         status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21518             UIO_SYSSPACE, path_flag);
21519 
21520         switch (status) {
21521         case 0:
21522 
21523                 break;  /* Success! */
21524         case EIO:
21525                 switch (ucmdbuf->uscsi_status) {
21526                 case STATUS_RESERVATION_CONFLICT:
21527                         status = EACCES;
21528                         break;
21529                 default:
21530                         break;
21531                 }
21532                 break;
21533         default:
21534                 break;
21535         }
21536 
21537         if (status == 0) {
21538                 SD_DUMP_MEMORY(un, SD_LOG_IO,
21539                     "sd_send_scsi_feature_GET_CONFIGURATION: data",
21540                     (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21541         }
21542 
21543         SD_TRACE(SD_LOG_IO, un,
21544             "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21545 
21546         return (status);
21547 }
21548 
21549 
21550 /*
21551  *    Function: sd_send_scsi_MODE_SENSE
21552  *
21553  * Description: Utility function for issuing a scsi MODE SENSE command.
21554  *              Note: This routine uses a consistent implementation for Group0,
21555  *              Group1, and Group2 commands across all platforms. ATAPI devices
21556  *              use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21557  *
21558  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21559  *                      structure for this target.
21560  *              cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21561  *                        CDB_GROUP[1|2] (10 byte).
21562  *              bufaddr - buffer for page data retrieved from the target.
21563  *              buflen - size of page to be retrieved.
21564  *              page_code - page code of data to be retrieved from the target.
21565  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21566  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21567  *                      to use the USCSI "direct" chain and bypass the normal
21568  *                      command waitq.
21569  *
21570  * Return Code: 0   - Success
21571  *              errno return code from sd_ssc_send()
21572  *
21573  *     Context: Can sleep. Does not return until command is completed.
21574  */
21575 
21576 static int
21577 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21578     size_t buflen,  uchar_t page_code, int path_flag)
21579 {
21580         struct  scsi_extended_sense     sense_buf;
21581         union scsi_cdb          cdb;
21582         struct uscsi_cmd        ucmd_buf;
21583         int                     status;
21584         int                     headlen;
21585         struct sd_lun           *un;
21586 
21587         ASSERT(ssc != NULL);
21588         un = ssc->ssc_un;
21589         ASSERT(un != NULL);
21590         ASSERT(!mutex_owned(SD_MUTEX(un)));
21591         ASSERT(bufaddr != NULL);
21592         ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21593             (cdbsize == CDB_GROUP2));
21594 
21595         SD_TRACE(SD_LOG_IO, un,
21596             "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21597 
21598         bzero(&cdb, sizeof (cdb));
21599         bzero(&ucmd_buf, sizeof (ucmd_buf));
21600         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21601         bzero(bufaddr, buflen);
21602 
21603         if (cdbsize == CDB_GROUP0) {
21604                 cdb.scc_cmd = SCMD_MODE_SENSE;
21605                 cdb.cdb_opaque[2] = page_code;
21606                 FORMG0COUNT(&cdb, buflen);
21607                 headlen = MODE_HEADER_LENGTH;
21608         } else {
21609                 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21610                 cdb.cdb_opaque[2] = page_code;
21611                 FORMG1COUNT(&cdb, buflen);
21612                 headlen = MODE_HEADER_LENGTH_GRP2;
21613         }
21614 
21615         ASSERT(headlen <= buflen);
21616         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21617 
21618         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21619         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21620         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21621         ucmd_buf.uscsi_buflen   = buflen;
21622         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21623         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21624         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21625         ucmd_buf.uscsi_timeout  = 60;
21626 
21627         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21628             UIO_SYSSPACE, path_flag);
21629 
21630         switch (status) {
21631         case 0:
21632                 /*
21633                  * sr_check_wp() uses 0x3f page code and check the header of
21634                  * mode page to determine if target device is write-protected.
21635                  * But some USB devices return 0 bytes for 0x3f page code. For
21636                  * this case, make sure that mode page header is returned at
21637                  * least.
21638                  */
21639                 if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21640                         status = EIO;
21641                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21642                             "mode page header is not returned");
21643                 }
21644                 break;  /* Success! */
21645         case EIO:
21646                 switch (ucmd_buf.uscsi_status) {
21647                 case STATUS_RESERVATION_CONFLICT:
21648                         status = EACCES;
21649                         break;
21650                 default:
21651                         break;
21652                 }
21653                 break;
21654         default:
21655                 break;
21656         }
21657 
21658         if (status == 0) {
21659                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21660                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21661         }
21662         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21663 
21664         return (status);
21665 }
21666 
21667 
21668 /*
21669  *    Function: sd_send_scsi_MODE_SELECT
21670  *
21671  * Description: Utility function for issuing a scsi MODE SELECT command.
21672  *              Note: This routine uses a consistent implementation for Group0,
21673  *              Group1, and Group2 commands across all platforms. ATAPI devices
21674  *              use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21675  *
21676  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21677  *                      structure for this target.
21678  *              cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21679  *                        CDB_GROUP[1|2] (10 byte).
21680  *              bufaddr - buffer for page data retrieved from the target.
21681  *              buflen - size of page to be retrieved.
21682  *              save_page - boolean to determin if SP bit should be set.
21683  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21684  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21685  *                      to use the USCSI "direct" chain and bypass the normal
21686  *                      command waitq.
21687  *
21688  * Return Code: 0   - Success
21689  *              errno return code from sd_ssc_send()
21690  *
21691  *     Context: Can sleep. Does not return until command is completed.
21692  */
21693 
21694 static int
21695 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21696     size_t buflen,  uchar_t save_page, int path_flag)
21697 {
21698         struct  scsi_extended_sense     sense_buf;
21699         union scsi_cdb          cdb;
21700         struct uscsi_cmd        ucmd_buf;
21701         int                     status;
21702         struct sd_lun           *un;
21703 
21704         ASSERT(ssc != NULL);
21705         un = ssc->ssc_un;
21706         ASSERT(un != NULL);
21707         ASSERT(!mutex_owned(SD_MUTEX(un)));
21708         ASSERT(bufaddr != NULL);
21709         ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21710             (cdbsize == CDB_GROUP2));
21711 
21712         SD_TRACE(SD_LOG_IO, un,
21713             "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21714 
21715         bzero(&cdb, sizeof (cdb));
21716         bzero(&ucmd_buf, sizeof (ucmd_buf));
21717         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21718 
21719         /* Set the PF bit for many third party drives */
21720         cdb.cdb_opaque[1] = 0x10;
21721 
21722         /* Set the savepage(SP) bit if given */
21723         if (save_page == SD_SAVE_PAGE) {
21724                 cdb.cdb_opaque[1] |= 0x01;
21725         }
21726 
21727         if (cdbsize == CDB_GROUP0) {
21728                 cdb.scc_cmd = SCMD_MODE_SELECT;
21729                 FORMG0COUNT(&cdb, buflen);
21730         } else {
21731                 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21732                 FORMG1COUNT(&cdb, buflen);
21733         }
21734 
21735         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21736 
21737         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21738         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21739         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21740         ucmd_buf.uscsi_buflen   = buflen;
21741         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21742         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21743         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21744         ucmd_buf.uscsi_timeout  = 60;
21745 
21746         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21747             UIO_SYSSPACE, path_flag);
21748 
21749         switch (status) {
21750         case 0:
21751                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21752                 break;  /* Success! */
21753         case EIO:
21754                 switch (ucmd_buf.uscsi_status) {
21755                 case STATUS_RESERVATION_CONFLICT:
21756                         status = EACCES;
21757                         break;
21758                 default:
21759                         break;
21760                 }
21761                 break;
21762         default:
21763                 break;
21764         }
21765 
21766         if (status == 0) {
21767                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21768                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21769         }
21770         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21771 
21772         return (status);
21773 }
21774 
21775 
21776 /*
21777  *    Function: sd_send_scsi_RDWR
21778  *
21779  * Description: Issue a scsi READ or WRITE command with the given parameters.
21780  *
21781  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21782  *                      structure for this target.
21783  *              cmd:     SCMD_READ or SCMD_WRITE
21784  *              bufaddr: Address of caller's buffer to receive the RDWR data
21785  *              buflen:  Length of caller's buffer receive the RDWR data.
21786  *              start_block: Block number for the start of the RDWR operation.
21787  *                       (Assumes target-native block size.)
21788  *              residp:  Pointer to variable to receive the redisual of the
21789  *                       RDWR operation (may be NULL of no residual requested).
21790  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21791  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21792  *                      to use the USCSI "direct" chain and bypass the normal
21793  *                      command waitq.
21794  *
21795  * Return Code: 0   - Success
21796  *              errno return code from sd_ssc_send()
21797  *
21798  *     Context: Can sleep. Does not return until command is completed.
21799  */
21800 
21801 static int
21802 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21803     size_t buflen, daddr_t start_block, int path_flag)
21804 {
21805         struct  scsi_extended_sense     sense_buf;
21806         union scsi_cdb          cdb;
21807         struct uscsi_cmd        ucmd_buf;
21808         uint32_t                block_count;
21809         int                     status;
21810         int                     cdbsize;
21811         uchar_t                 flag;
21812         struct sd_lun           *un;
21813 
21814         ASSERT(ssc != NULL);
21815         un = ssc->ssc_un;
21816         ASSERT(un != NULL);
21817         ASSERT(!mutex_owned(SD_MUTEX(un)));
21818         ASSERT(bufaddr != NULL);
21819         ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21820 
21821         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21822 
21823         if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21824                 return (EINVAL);
21825         }
21826 
21827         mutex_enter(SD_MUTEX(un));
21828         block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21829         mutex_exit(SD_MUTEX(un));
21830 
21831         flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21832 
21833         SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21834             "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21835             bufaddr, buflen, start_block, block_count);
21836 
21837         bzero(&cdb, sizeof (cdb));
21838         bzero(&ucmd_buf, sizeof (ucmd_buf));
21839         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21840 
21841         /* Compute CDB size to use */
21842         if (start_block > 0xffffffff)
21843                 cdbsize = CDB_GROUP4;
21844         else if ((start_block & 0xFFE00000) ||
21845             (un->un_f_cfg_is_atapi == TRUE))
21846                 cdbsize = CDB_GROUP1;
21847         else
21848                 cdbsize = CDB_GROUP0;
21849 
21850         switch (cdbsize) {
21851         case CDB_GROUP0:        /* 6-byte CDBs */
21852                 cdb.scc_cmd = cmd;
21853                 FORMG0ADDR(&cdb, start_block);
21854                 FORMG0COUNT(&cdb, block_count);
21855                 break;
21856         case CDB_GROUP1:        /* 10-byte CDBs */
21857                 cdb.scc_cmd = cmd | SCMD_GROUP1;
21858                 FORMG1ADDR(&cdb, start_block);
21859                 FORMG1COUNT(&cdb, block_count);
21860                 break;
21861         case CDB_GROUP4:        /* 16-byte CDBs */
21862                 cdb.scc_cmd = cmd | SCMD_GROUP4;
21863                 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21864                 FORMG4COUNT(&cdb, block_count);
21865                 break;
21866         case CDB_GROUP5:        /* 12-byte CDBs (currently unsupported) */
21867         default:
21868                 /* All others reserved */
21869                 return (EINVAL);
21870         }
21871 
21872         /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21873         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21874 
21875         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21876         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21877         ucmd_buf.uscsi_bufaddr  = bufaddr;
21878         ucmd_buf.uscsi_buflen   = buflen;
21879         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21880         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21881         ucmd_buf.uscsi_flags    = flag | USCSI_RQENABLE | USCSI_SILENT;
21882         ucmd_buf.uscsi_timeout  = 60;
21883         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21884             UIO_SYSSPACE, path_flag);
21885 
21886         switch (status) {
21887         case 0:
21888                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21889                 break;  /* Success! */
21890         case EIO:
21891                 switch (ucmd_buf.uscsi_status) {
21892                 case STATUS_RESERVATION_CONFLICT:
21893                         status = EACCES;
21894                         break;
21895                 default:
21896                         break;
21897                 }
21898                 break;
21899         default:
21900                 break;
21901         }
21902 
21903         if (status == 0) {
21904                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21905                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21906         }
21907 
21908         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21909 
21910         return (status);
21911 }
21912 
21913 
21914 /*
21915  *    Function: sd_send_scsi_LOG_SENSE
21916  *
21917  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21918  *
21919  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21920  *                      structure for this target.
21921  *
21922  * Return Code: 0   - Success
21923  *              errno return code from sd_ssc_send()
21924  *
21925  *     Context: Can sleep. Does not return until command is completed.
21926  */
21927 
21928 static int
21929 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21930     uchar_t page_code, uchar_t page_control, uint16_t param_ptr, int path_flag)
21931 {
21932         struct scsi_extended_sense      sense_buf;
21933         union scsi_cdb          cdb;
21934         struct uscsi_cmd        ucmd_buf;
21935         int                     status;
21936         struct sd_lun           *un;
21937 
21938         ASSERT(ssc != NULL);
21939         un = ssc->ssc_un;
21940         ASSERT(un != NULL);
21941         ASSERT(!mutex_owned(SD_MUTEX(un)));
21942 
21943         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21944 
21945         bzero(&cdb, sizeof (cdb));
21946         bzero(&ucmd_buf, sizeof (ucmd_buf));
21947         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21948 
21949         cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21950         cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21951         cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21952         cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21953         FORMG1COUNT(&cdb, buflen);
21954 
21955         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21956         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
21957         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21958         ucmd_buf.uscsi_buflen   = buflen;
21959         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21960         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21961         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21962         ucmd_buf.uscsi_timeout  = 60;
21963 
21964         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21965             UIO_SYSSPACE, path_flag);
21966 
21967         switch (status) {
21968         case 0:
21969                 break;
21970         case EIO:
21971                 switch (ucmd_buf.uscsi_status) {
21972                 case STATUS_RESERVATION_CONFLICT:
21973                         status = EACCES;
21974                         break;
21975                 case STATUS_CHECK:
21976                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21977                             (scsi_sense_key((uint8_t *)&sense_buf) ==
21978                             KEY_ILLEGAL_REQUEST) &&
21979                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21980                                 /*
21981                                  * ASC 0x24: INVALID FIELD IN CDB
21982                                  */
21983                                 switch (page_code) {
21984                                 case START_STOP_CYCLE_PAGE:
21985                                         /*
21986                                          * The start stop cycle counter is
21987                                          * implemented as page 0x31 in earlier
21988                                          * generation disks. In new generation
21989                                          * disks the start stop cycle counter is
21990                                          * implemented as page 0xE. To properly
21991                                          * handle this case if an attempt for
21992                                          * log page 0xE is made and fails we
21993                                          * will try again using page 0x31.
21994                                          *
21995                                          * Network storage BU committed to
21996                                          * maintain the page 0x31 for this
21997                                          * purpose and will not have any other
21998                                          * page implemented with page code 0x31
21999                                          * until all disks transition to the
22000                                          * standard page.
22001                                          */
22002                                         mutex_enter(SD_MUTEX(un));
22003                                         un->un_start_stop_cycle_page =
22004                                             START_STOP_CYCLE_VU_PAGE;
22005                                         cdb.cdb_opaque[2] =
22006                                             (char)(page_control << 6) |
22007                                             un->un_start_stop_cycle_page;
22008                                         mutex_exit(SD_MUTEX(un));
22009                                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22010                                         status = sd_ssc_send(
22011                                             ssc, &ucmd_buf, FKIOCTL,
22012                                             UIO_SYSSPACE, path_flag);
22013 
22014                                         break;
22015                                 case TEMPERATURE_PAGE:
22016                                         status = ENOTTY;
22017                                         break;
22018                                 default:
22019                                         break;
22020                                 }
22021                         }
22022                         break;
22023                 default:
22024                         break;
22025                 }
22026                 break;
22027         default:
22028                 break;
22029         }
22030 
22031         if (status == 0) {
22032                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22033                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22034                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22035         }
22036 
22037         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22038 
22039         return (status);
22040 }
22041 
22042 
22043 /*
22044  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22045  *
22046  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22047  *
22048  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22049  *                      structure for this target.
22050  *              bufaddr
22051  *              buflen
22052  *              class_req
22053  *
22054  * Return Code: 0   - Success
22055  *              errno return code from sd_ssc_send()
22056  *
22057  *     Context: Can sleep. Does not return until command is completed.
22058  */
22059 
22060 static int
22061 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22062     size_t buflen, uchar_t class_req)
22063 {
22064         union scsi_cdb          cdb;
22065         struct uscsi_cmd        ucmd_buf;
22066         int                     status;
22067         struct sd_lun           *un;
22068 
22069         ASSERT(ssc != NULL);
22070         un = ssc->ssc_un;
22071         ASSERT(un != NULL);
22072         ASSERT(!mutex_owned(SD_MUTEX(un)));
22073         ASSERT(bufaddr != NULL);
22074 
22075         SD_TRACE(SD_LOG_IO, un,
22076             "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22077 
22078         bzero(&cdb, sizeof (cdb));
22079         bzero(&ucmd_buf, sizeof (ucmd_buf));
22080         bzero(bufaddr, buflen);
22081 
22082         cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22083         cdb.cdb_opaque[1] = 1; /* polled */
22084         cdb.cdb_opaque[4] = class_req;
22085         FORMG1COUNT(&cdb, buflen);
22086 
22087         ucmd_buf.uscsi_cdb      = (char *)&cdb;
22088         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
22089         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
22090         ucmd_buf.uscsi_buflen   = buflen;
22091         ucmd_buf.uscsi_rqbuf    = NULL;
22092         ucmd_buf.uscsi_rqlen    = 0;
22093         ucmd_buf.uscsi_flags    = USCSI_READ | USCSI_SILENT;
22094         ucmd_buf.uscsi_timeout  = 60;
22095 
22096         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22097             UIO_SYSSPACE, SD_PATH_DIRECT);
22098 
22099         /*
22100          * Only handle status == 0, the upper-level caller
22101          * will put different assessment based on the context.
22102          */
22103         if (status == 0) {
22104                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22105 
22106                 if (ucmd_buf.uscsi_resid != 0) {
22107                         status = EIO;
22108                 }
22109         }
22110 
22111         SD_TRACE(SD_LOG_IO, un,
22112             "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22113 
22114         return (status);
22115 }
22116 
22117 
22118 static boolean_t
22119 sd_gesn_media_data_valid(uchar_t *data)
22120 {
22121         uint16_t                        len;
22122 
22123         len = (data[1] << 8) | data[0];
22124         return ((len >= 6) &&
22125             ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22126             ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22127             ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22128 }
22129 
22130 
22131 /*
22132  *    Function: sdioctl
22133  *
22134  * Description: Driver's ioctl(9e) entry point function.
22135  *
22136  *   Arguments: dev     - device number
22137  *              cmd     - ioctl operation to be performed
22138  *              arg     - user argument, contains data to be set or reference
22139  *                        parameter for get
22140  *              flag    - bit flag, indicating open settings, 32/64 bit type
22141  *              cred_p  - user credential pointer
22142  *              rval_p  - calling process return value (OPT)
22143  *
22144  * Return Code: EINVAL
22145  *              ENOTTY
22146  *              ENXIO
22147  *              EIO
22148  *              EFAULT
22149  *              ENOTSUP
22150  *              EPERM
22151  *
22152  *     Context: Called from the device switch at normal priority.
22153  */
22154 
22155 static int
22156 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22157 {
22158         struct sd_lun   *un = NULL;
22159         int             err = 0;
22160         int             i = 0;
22161         cred_t          *cr;
22162         int             tmprval = EINVAL;
22163         boolean_t       is_valid;
22164         sd_ssc_t        *ssc;
22165 
22166         /*
22167          * All device accesses go thru sdstrategy where we check on suspend
22168          * status
22169          */
22170         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22171                 return (ENXIO);
22172         }
22173 
22174         ASSERT(!mutex_owned(SD_MUTEX(un)));
22175 
22176         /* Initialize sd_ssc_t for internal uscsi commands */
22177         ssc = sd_ssc_init(un);
22178 
22179         is_valid = SD_IS_VALID_LABEL(un);
22180 
22181         /*
22182          * Moved this wait from sd_uscsi_strategy to here for
22183          * reasons of deadlock prevention. Internal driver commands,
22184          * specifically those to change a devices power level, result
22185          * in a call to sd_uscsi_strategy.
22186          */
22187         mutex_enter(SD_MUTEX(un));
22188         while ((un->un_state == SD_STATE_SUSPENDED) ||
22189             (un->un_state == SD_STATE_PM_CHANGING)) {
22190                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22191         }
22192         /*
22193          * Twiddling the counter here protects commands from now
22194          * through to the top of sd_uscsi_strategy. Without the
22195          * counter inc. a power down, for example, could get in
22196          * after the above check for state is made and before
22197          * execution gets to the top of sd_uscsi_strategy.
22198          * That would cause problems.
22199          */
22200         un->un_ncmds_in_driver++;
22201 
22202         if (!is_valid &&
22203             (flag & (FNDELAY | FNONBLOCK))) {
22204                 switch (cmd) {
22205                 case DKIOCGGEOM:        /* SD_PATH_DIRECT */
22206                 case DKIOCGVTOC:
22207                 case DKIOCGEXTVTOC:
22208                 case DKIOCGAPART:
22209                 case DKIOCPARTINFO:
22210                 case DKIOCEXTPARTINFO:
22211                 case DKIOCSGEOM:
22212                 case DKIOCSAPART:
22213                 case DKIOCGETEFI:
22214                 case DKIOCPARTITION:
22215                 case DKIOCSVTOC:
22216                 case DKIOCSEXTVTOC:
22217                 case DKIOCSETEFI:
22218                 case DKIOCGMBOOT:
22219                 case DKIOCSMBOOT:
22220                 case DKIOCG_PHYGEOM:
22221                 case DKIOCG_VIRTGEOM:
22222 #if defined(__i386) || defined(__amd64)
22223                 case DKIOCSETEXTPART:
22224 #endif
22225                         /* let cmlb handle it */
22226                         goto skip_ready_valid;
22227 
22228                 case CDROMPAUSE:
22229                 case CDROMRESUME:
22230                 case CDROMPLAYMSF:
22231                 case CDROMPLAYTRKIND:
22232                 case CDROMREADTOCHDR:
22233                 case CDROMREADTOCENTRY:
22234                 case CDROMSTOP:
22235                 case CDROMSTART:
22236                 case CDROMVOLCTRL:
22237                 case CDROMSUBCHNL:
22238                 case CDROMREADMODE2:
22239                 case CDROMREADMODE1:
22240                 case CDROMREADOFFSET:
22241                 case CDROMSBLKMODE:
22242                 case CDROMGBLKMODE:
22243                 case CDROMGDRVSPEED:
22244                 case CDROMSDRVSPEED:
22245                 case CDROMCDDA:
22246                 case CDROMCDXA:
22247                 case CDROMSUBCODE:
22248                         if (!ISCD(un)) {
22249                                 un->un_ncmds_in_driver--;
22250                                 ASSERT(un->un_ncmds_in_driver >= 0);
22251                                 mutex_exit(SD_MUTEX(un));
22252                                 err = ENOTTY;
22253                                 goto done_without_assess;
22254                         }
22255                         break;
22256                 case FDEJECT:
22257                 case DKIOCEJECT:
22258                 case CDROMEJECT:
22259                         if (!un->un_f_eject_media_supported) {
22260                                 un->un_ncmds_in_driver--;
22261                                 ASSERT(un->un_ncmds_in_driver >= 0);
22262                                 mutex_exit(SD_MUTEX(un));
22263                                 err = ENOTTY;
22264                                 goto done_without_assess;
22265                         }
22266                         break;
22267                 case DKIOCFLUSHWRITECACHE:
22268                         mutex_exit(SD_MUTEX(un));
22269                         err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22270                         if (err != 0) {
22271                                 mutex_enter(SD_MUTEX(un));
22272                                 un->un_ncmds_in_driver--;
22273                                 ASSERT(un->un_ncmds_in_driver >= 0);
22274                                 mutex_exit(SD_MUTEX(un));
22275                                 err = EIO;
22276                                 goto done_quick_assess;
22277                         }
22278                         mutex_enter(SD_MUTEX(un));
22279                         /* FALLTHROUGH */
22280                 case DKIOCREMOVABLE:
22281                 case DKIOCHOTPLUGGABLE:
22282                 case DKIOCINFO:
22283                 case DKIOCGMEDIAINFO:
22284                 case DKIOCGMEDIAINFOEXT:
22285                 case DKIOCSOLIDSTATE:
22286                 case MHIOCENFAILFAST:
22287                 case MHIOCSTATUS:
22288                 case MHIOCTKOWN:
22289                 case MHIOCRELEASE:
22290                 case MHIOCGRP_INKEYS:
22291                 case MHIOCGRP_INRESV:
22292                 case MHIOCGRP_REGISTER:
22293                 case MHIOCGRP_CLEAR:
22294                 case MHIOCGRP_RESERVE:
22295                 case MHIOCGRP_PREEMPTANDABORT:
22296                 case MHIOCGRP_REGISTERANDIGNOREKEY:
22297                 case CDROMCLOSETRAY:
22298                 case USCSICMD:
22299                         goto skip_ready_valid;
22300                 default:
22301                         break;
22302                 }
22303 
22304                 mutex_exit(SD_MUTEX(un));
22305                 err = sd_ready_and_valid(ssc, SDPART(dev));
22306                 mutex_enter(SD_MUTEX(un));
22307 
22308                 if (err != SD_READY_VALID) {
22309                         switch (cmd) {
22310                         case DKIOCSTATE:
22311                         case CDROMGDRVSPEED:
22312                         case CDROMSDRVSPEED:
22313                         case FDEJECT:   /* for eject command */
22314                         case DKIOCEJECT:
22315                         case CDROMEJECT:
22316                         case DKIOCREMOVABLE:
22317                         case DKIOCHOTPLUGGABLE:
22318                                 break;
22319                         default:
22320                                 if (un->un_f_has_removable_media) {
22321                                         err = ENXIO;
22322                                 } else {
22323                                 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22324                                         if (err == SD_RESERVED_BY_OTHERS) {
22325                                                 err = EACCES;
22326                                         } else {
22327                                                 err = EIO;
22328                                         }
22329                                 }
22330                                 un->un_ncmds_in_driver--;
22331                                 ASSERT(un->un_ncmds_in_driver >= 0);
22332                                 mutex_exit(SD_MUTEX(un));
22333 
22334                                 goto done_without_assess;
22335                         }
22336                 }
22337         }
22338 
22339 skip_ready_valid:
22340         mutex_exit(SD_MUTEX(un));
22341 
22342         switch (cmd) {
22343         case DKIOCINFO:
22344                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22345                 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22346                 break;
22347 
22348         case DKIOCGMEDIAINFO:
22349                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22350                 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22351                 break;
22352 
22353         case DKIOCGMEDIAINFOEXT:
22354                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22355                 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22356                 break;
22357 
22358         case DKIOCGGEOM:
22359         case DKIOCGVTOC:
22360         case DKIOCGEXTVTOC:
22361         case DKIOCGAPART:
22362         case DKIOCPARTINFO:
22363         case DKIOCEXTPARTINFO:
22364         case DKIOCSGEOM:
22365         case DKIOCSAPART:
22366         case DKIOCGETEFI:
22367         case DKIOCPARTITION:
22368         case DKIOCSVTOC:
22369         case DKIOCSEXTVTOC:
22370         case DKIOCSETEFI:
22371         case DKIOCGMBOOT:
22372         case DKIOCSMBOOT:
22373         case DKIOCG_PHYGEOM:
22374         case DKIOCG_VIRTGEOM:
22375 #if defined(__i386) || defined(__amd64)
22376         case DKIOCSETEXTPART:
22377 #endif
22378                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22379 
22380                 /* TUR should spin up */
22381 
22382                 if (un->un_f_has_removable_media)
22383                         err = sd_send_scsi_TEST_UNIT_READY(ssc,
22384                             SD_CHECK_FOR_MEDIA);
22385 
22386                 else
22387                         err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22388 
22389                 if (err != 0)
22390                         goto done_with_assess;
22391 
22392                 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22393                     cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22394 
22395                 if ((err == 0) &&
22396                     ((cmd == DKIOCSETEFI) ||
22397                     ((un->un_f_pkstats_enabled) &&
22398                     (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22399                     cmd == DKIOCSEXTVTOC)))) {
22400 
22401                         tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22402                             (void *)SD_PATH_DIRECT);
22403                         if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22404                                 sd_set_pstats(un);
22405                                 SD_TRACE(SD_LOG_IO_PARTITION, un,
22406                                     "sd_ioctl: un:0x%p pstats created and "
22407                                     "set\n", un);
22408                         }
22409                 }
22410 
22411                 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22412                     ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22413 
22414                         mutex_enter(SD_MUTEX(un));
22415                         if (un->un_f_devid_supported &&
22416                             (un->un_f_opt_fab_devid == TRUE)) {
22417                                 if (un->un_devid == NULL) {
22418                                         sd_register_devid(ssc, SD_DEVINFO(un),
22419                                             SD_TARGET_IS_UNRESERVED);
22420                                 } else {
22421                                         /*
22422                                          * The device id for this disk
22423                                          * has been fabricated. The
22424                                          * device id must be preserved
22425                                          * by writing it back out to
22426                                          * disk.
22427                                          */
22428                                         if (sd_write_deviceid(ssc) != 0) {
22429                                                 ddi_devid_free(un->un_devid);
22430                                                 un->un_devid = NULL;
22431                                         }
22432                                 }
22433                         }
22434                         mutex_exit(SD_MUTEX(un));
22435                 }
22436 
22437                 break;
22438 
22439         case DKIOCLOCK:
22440                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22441                 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22442                     SD_PATH_STANDARD);
22443                 goto done_with_assess;
22444 
22445         case DKIOCUNLOCK:
22446                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22447                 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22448                     SD_PATH_STANDARD);
22449                 goto done_with_assess;
22450 
22451         case DKIOCSTATE: {
22452                 enum dkio_state         state;
22453                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22454 
22455                 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22456                         err = EFAULT;
22457                 } else {
22458                         err = sd_check_media(dev, state);
22459                         if (err == 0) {
22460                                 if (ddi_copyout(&un->un_mediastate, (void *)arg,
22461                                     sizeof (int), flag) != 0)
22462                                         err = EFAULT;
22463                         }
22464                 }
22465                 break;
22466         }
22467 
22468         case DKIOCREMOVABLE:
22469                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22470                 i = un->un_f_has_removable_media ? 1 : 0;
22471                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22472                         err = EFAULT;
22473                 } else {
22474                         err = 0;
22475                 }
22476                 break;
22477 
22478         case DKIOCSOLIDSTATE:
22479                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22480                 i = un->un_f_is_solid_state ? 1 : 0;
22481                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22482                         err = EFAULT;
22483                 } else {
22484                         err = 0;
22485                 }
22486                 break;
22487 
22488         case DKIOCHOTPLUGGABLE:
22489                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22490                 i = un->un_f_is_hotpluggable ? 1 : 0;
22491                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22492                         err = EFAULT;
22493                 } else {
22494                         err = 0;
22495                 }
22496                 break;
22497 
22498         case DKIOCREADONLY:
22499                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22500                 i = 0;
22501                 if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22502                     (sr_check_wp(dev) != 0)) {
22503                         i = 1;
22504                 }
22505                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22506                         err = EFAULT;
22507                 } else {
22508                         err = 0;
22509                 }
22510                 break;
22511 
22512         case DKIOCGTEMPERATURE:
22513                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22514                 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22515                 break;
22516 
22517         case MHIOCENFAILFAST:
22518                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22519                 if ((err = drv_priv(cred_p)) == 0) {
22520                         err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22521                 }
22522                 break;
22523 
22524         case MHIOCTKOWN:
22525                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22526                 if ((err = drv_priv(cred_p)) == 0) {
22527                         err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22528                 }
22529                 break;
22530 
22531         case MHIOCRELEASE:
22532                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22533                 if ((err = drv_priv(cred_p)) == 0) {
22534                         err = sd_mhdioc_release(dev);
22535                 }
22536                 break;
22537 
22538         case MHIOCSTATUS:
22539                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22540                 if ((err = drv_priv(cred_p)) == 0) {
22541                         switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22542                         case 0:
22543                                 err = 0;
22544                                 break;
22545                         case EACCES:
22546                                 *rval_p = 1;
22547                                 err = 0;
22548                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22549                                 break;
22550                         default:
22551                                 err = EIO;
22552                                 goto done_with_assess;
22553                         }
22554                 }
22555                 break;
22556 
22557         case MHIOCQRESERVE:
22558                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22559                 if ((err = drv_priv(cred_p)) == 0) {
22560                         err = sd_reserve_release(dev, SD_RESERVE);
22561                 }
22562                 break;
22563 
22564         case MHIOCREREGISTERDEVID:
22565                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22566                 if (drv_priv(cred_p) == EPERM) {
22567                         err = EPERM;
22568                 } else if (!un->un_f_devid_supported) {
22569                         err = ENOTTY;
22570                 } else {
22571                         err = sd_mhdioc_register_devid(dev);
22572                 }
22573                 break;
22574 
22575         case MHIOCGRP_INKEYS:
22576                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22577                 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22578                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22579                                 err = ENOTSUP;
22580                         } else {
22581                                 err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22582                                     flag);
22583                         }
22584                 }
22585                 break;
22586 
22587         case MHIOCGRP_INRESV:
22588                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22589                 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22590                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22591                                 err = ENOTSUP;
22592                         } else {
22593                                 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22594                         }
22595                 }
22596                 break;
22597 
22598         case MHIOCGRP_REGISTER:
22599                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22600                 if ((err = drv_priv(cred_p)) != EPERM) {
22601                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22602                                 err = ENOTSUP;
22603                         } else if (arg != NULL) {
22604                                 mhioc_register_t reg;
22605                                 if (ddi_copyin((void *)arg, &reg,
22606                                     sizeof (mhioc_register_t), flag) != 0) {
22607                                         err = EFAULT;
22608                                 } else {
22609                                         err =
22610                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22611                                             ssc, SD_SCSI3_REGISTER,
22612                                             (uchar_t *)&reg);
22613                                         if (err != 0)
22614                                                 goto done_with_assess;
22615                                 }
22616                         }
22617                 }
22618                 break;
22619 
22620         case MHIOCGRP_CLEAR:
22621                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22622                 if ((err = drv_priv(cred_p)) != EPERM) {
22623                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22624                                 err = ENOTSUP;
22625                         } else if (arg != NULL) {
22626                                 mhioc_register_t reg;
22627                                 if (ddi_copyin((void *)arg, &reg,
22628                                     sizeof (mhioc_register_t), flag) != 0) {
22629                                         err = EFAULT;
22630                                 } else {
22631                                         err =
22632                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22633                                             ssc, SD_SCSI3_CLEAR,
22634                                             (uchar_t *)&reg);
22635                                         if (err != 0)
22636                                                 goto done_with_assess;
22637                                 }
22638                         }
22639                 }
22640                 break;
22641 
22642         case MHIOCGRP_RESERVE:
22643                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22644                 if ((err = drv_priv(cred_p)) != EPERM) {
22645                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22646                                 err = ENOTSUP;
22647                         } else if (arg != NULL) {
22648                                 mhioc_resv_desc_t resv_desc;
22649                                 if (ddi_copyin((void *)arg, &resv_desc,
22650                                     sizeof (mhioc_resv_desc_t), flag) != 0) {
22651                                         err = EFAULT;
22652                                 } else {
22653                                         err =
22654                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22655                                             ssc, SD_SCSI3_RESERVE,
22656                                             (uchar_t *)&resv_desc);
22657                                         if (err != 0)
22658                                                 goto done_with_assess;
22659                                 }
22660                         }
22661                 }
22662                 break;
22663 
22664         case MHIOCGRP_PREEMPTANDABORT:
22665                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22666                 if ((err = drv_priv(cred_p)) != EPERM) {
22667                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22668                                 err = ENOTSUP;
22669                         } else if (arg != NULL) {
22670                                 mhioc_preemptandabort_t preempt_abort;
22671                                 if (ddi_copyin((void *)arg, &preempt_abort,
22672                                     sizeof (mhioc_preemptandabort_t),
22673                                     flag) != 0) {
22674                                         err = EFAULT;
22675                                 } else {
22676                                         err =
22677                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22678                                             ssc, SD_SCSI3_PREEMPTANDABORT,
22679                                             (uchar_t *)&preempt_abort);
22680                                         if (err != 0)
22681                                                 goto done_with_assess;
22682                                 }
22683                         }
22684                 }
22685                 break;
22686 
22687         case MHIOCGRP_REGISTERANDIGNOREKEY:
22688                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22689                 if ((err = drv_priv(cred_p)) != EPERM) {
22690                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22691                                 err = ENOTSUP;
22692                         } else if (arg != NULL) {
22693                                 mhioc_registerandignorekey_t r_and_i;
22694                                 if (ddi_copyin((void *)arg, (void *)&r_and_i,
22695                                     sizeof (mhioc_registerandignorekey_t),
22696                                     flag) != 0) {
22697                                         err = EFAULT;
22698                                 } else {
22699                                         err =
22700                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22701                                             ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22702                                             (uchar_t *)&r_and_i);
22703                                         if (err != 0)
22704                                                 goto done_with_assess;
22705                                 }
22706                         }
22707                 }
22708                 break;
22709 
22710         case USCSICMD:
22711                 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22712                 cr = ddi_get_cred();
22713                 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22714                         err = EPERM;
22715                 } else {
22716                         enum uio_seg    uioseg;
22717 
22718                         uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22719                             UIO_USERSPACE;
22720                         if (un->un_f_format_in_progress == TRUE) {
22721                                 err = EAGAIN;
22722                                 break;
22723                         }
22724 
22725                         err = sd_ssc_send(ssc,
22726                             (struct uscsi_cmd *)arg,
22727                             flag, uioseg, SD_PATH_STANDARD);
22728                         if (err != 0)
22729                                 goto done_with_assess;
22730                         else
22731                                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22732                 }
22733                 break;
22734 
22735         case CDROMPAUSE:
22736         case CDROMRESUME:
22737                 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22738                 if (!ISCD(un)) {
22739                         err = ENOTTY;
22740                 } else {
22741                         err = sr_pause_resume(dev, cmd);
22742                 }
22743                 break;
22744 
22745         case CDROMPLAYMSF:
22746                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22747                 if (!ISCD(un)) {
22748                         err = ENOTTY;
22749                 } else {
22750                         err = sr_play_msf(dev, (caddr_t)arg, flag);
22751                 }
22752                 break;
22753 
22754         case CDROMPLAYTRKIND:
22755                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22756 #if defined(__i386) || defined(__amd64)
22757                 /*
22758                  * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22759                  */
22760                 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22761 #else
22762                 if (!ISCD(un)) {
22763 #endif
22764                         err = ENOTTY;
22765                 } else {
22766                         err = sr_play_trkind(dev, (caddr_t)arg, flag);
22767                 }
22768                 break;
22769 
22770         case CDROMREADTOCHDR:
22771                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22772                 if (!ISCD(un)) {
22773                         err = ENOTTY;
22774                 } else {
22775                         err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22776                 }
22777                 break;
22778 
22779         case CDROMREADTOCENTRY:
22780                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22781                 if (!ISCD(un)) {
22782                         err = ENOTTY;
22783                 } else {
22784                         err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22785                 }
22786                 break;
22787 
22788         case CDROMSTOP:
22789                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22790                 if (!ISCD(un)) {
22791                         err = ENOTTY;
22792                 } else {
22793                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22794                             SD_TARGET_STOP, SD_PATH_STANDARD);
22795                         goto done_with_assess;
22796                 }
22797                 break;
22798 
22799         case CDROMSTART:
22800                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22801                 if (!ISCD(un)) {
22802                         err = ENOTTY;
22803                 } else {
22804                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22805                             SD_TARGET_START, SD_PATH_STANDARD);
22806                         goto done_with_assess;
22807                 }
22808                 break;
22809 
22810         case CDROMCLOSETRAY:
22811                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22812                 if (!ISCD(un)) {
22813                         err = ENOTTY;
22814                 } else {
22815                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22816                             SD_TARGET_CLOSE, SD_PATH_STANDARD);
22817                         goto done_with_assess;
22818                 }
22819                 break;
22820 
22821         case FDEJECT:   /* for eject command */
22822         case DKIOCEJECT:
22823         case CDROMEJECT:
22824                 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22825                 if (!un->un_f_eject_media_supported) {
22826                         err = ENOTTY;
22827                 } else {
22828                         err = sr_eject(dev);
22829                 }
22830                 break;
22831 
22832         case CDROMVOLCTRL:
22833                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22834                 if (!ISCD(un)) {
22835                         err = ENOTTY;
22836                 } else {
22837                         err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22838                 }
22839                 break;
22840 
22841         case CDROMSUBCHNL:
22842                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22843                 if (!ISCD(un)) {
22844                         err = ENOTTY;
22845                 } else {
22846                         err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22847                 }
22848                 break;
22849 
22850         case CDROMREADMODE2:
22851                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22852                 if (!ISCD(un)) {
22853                         err = ENOTTY;
22854                 } else if (un->un_f_cfg_is_atapi == TRUE) {
22855                         /*
22856                          * If the drive supports READ CD, use that instead of
22857                          * switching the LBA size via a MODE SELECT
22858                          * Block Descriptor
22859                          */
22860                         err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22861                 } else {
22862                         err = sr_read_mode2(dev, (caddr_t)arg, flag);
22863                 }
22864                 break;
22865 
22866         case CDROMREADMODE1:
22867                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22868                 if (!ISCD(un)) {
22869                         err = ENOTTY;
22870                 } else {
22871                         err = sr_read_mode1(dev, (caddr_t)arg, flag);
22872                 }
22873                 break;
22874 
22875         case CDROMREADOFFSET:
22876                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22877                 if (!ISCD(un)) {
22878                         err = ENOTTY;
22879                 } else {
22880                         err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22881                             flag);
22882                 }
22883                 break;
22884 
22885         case CDROMSBLKMODE:
22886                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22887                 /*
22888                  * There is no means of changing block size in case of atapi
22889                  * drives, thus return ENOTTY if drive type is atapi
22890                  */
22891                 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22892                         err = ENOTTY;
22893                 } else if (un->un_f_mmc_cap == TRUE) {
22894 
22895                         /*
22896                          * MMC Devices do not support changing the
22897                          * logical block size
22898                          *
22899                          * Note: EINVAL is being returned instead of ENOTTY to
22900                          * maintain consistancy with the original mmc
22901                          * driver update.
22902                          */
22903                         err = EINVAL;
22904                 } else {
22905                         mutex_enter(SD_MUTEX(un));
22906                         if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22907                             (un->un_ncmds_in_transport > 0)) {
22908                                 mutex_exit(SD_MUTEX(un));
22909                                 err = EINVAL;
22910                         } else {
22911                                 mutex_exit(SD_MUTEX(un));
22912                                 err = sr_change_blkmode(dev, cmd, arg, flag);
22913                         }
22914                 }
22915                 break;
22916 
22917         case CDROMGBLKMODE:
22918                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22919                 if (!ISCD(un)) {
22920                         err = ENOTTY;
22921                 } else if ((un->un_f_cfg_is_atapi != FALSE) &&
22922                     (un->un_f_blockcount_is_valid != FALSE)) {
22923                         /*
22924                          * Drive is an ATAPI drive so return target block
22925                          * size for ATAPI drives since we cannot change the
22926                          * blocksize on ATAPI drives. Used primarily to detect
22927                          * if an ATAPI cdrom is present.
22928                          */
22929                         if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22930                             sizeof (int), flag) != 0) {
22931                                 err = EFAULT;
22932                         } else {
22933                                 err = 0;
22934                         }
22935 
22936                 } else {
22937                         /*
22938                          * Drive supports changing block sizes via a Mode
22939                          * Select.
22940                          */
22941                         err = sr_change_blkmode(dev, cmd, arg, flag);
22942                 }
22943                 break;
22944 
22945         case CDROMGDRVSPEED:
22946         case CDROMSDRVSPEED:
22947                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22948                 if (!ISCD(un)) {
22949                         err = ENOTTY;
22950                 } else if (un->un_f_mmc_cap == TRUE) {
22951                         /*
22952                          * Note: In the future the driver implementation
22953                          * for getting and
22954                          * setting cd speed should entail:
22955                          * 1) If non-mmc try the Toshiba mode page
22956                          *    (sr_change_speed)
22957                          * 2) If mmc but no support for Real Time Streaming try
22958                          *    the SET CD SPEED (0xBB) command
22959                          *   (sr_atapi_change_speed)
22960                          * 3) If mmc and support for Real Time Streaming
22961                          *    try the GET PERFORMANCE and SET STREAMING
22962                          *    commands (not yet implemented, 4380808)
22963                          */
22964                         /*
22965                          * As per recent MMC spec, CD-ROM speed is variable
22966                          * and changes with LBA. Since there is no such
22967                          * things as drive speed now, fail this ioctl.
22968                          *
22969                          * Note: EINVAL is returned for consistancy of original
22970                          * implementation which included support for getting
22971                          * the drive speed of mmc devices but not setting
22972                          * the drive speed. Thus EINVAL would be returned
22973                          * if a set request was made for an mmc device.
22974                          * We no longer support get or set speed for
22975                          * mmc but need to remain consistent with regard
22976                          * to the error code returned.
22977                          */
22978                         err = EINVAL;
22979                 } else if (un->un_f_cfg_is_atapi == TRUE) {
22980                         err = sr_atapi_change_speed(dev, cmd, arg, flag);
22981                 } else {
22982                         err = sr_change_speed(dev, cmd, arg, flag);
22983                 }
22984                 break;
22985 
22986         case CDROMCDDA:
22987                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22988                 if (!ISCD(un)) {
22989                         err = ENOTTY;
22990                 } else {
22991                         err = sr_read_cdda(dev, (void *)arg, flag);
22992                 }
22993                 break;
22994 
22995         case CDROMCDXA:
22996                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
22997                 if (!ISCD(un)) {
22998                         err = ENOTTY;
22999                 } else {
23000                         err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23001                 }
23002                 break;
23003 
23004         case CDROMSUBCODE:
23005                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23006                 if (!ISCD(un)) {
23007                         err = ENOTTY;
23008                 } else {
23009                         err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23010                 }
23011                 break;
23012 
23013 
23014 #ifdef SDDEBUG
23015 /* RESET/ABORTS testing ioctls */
23016         case DKIOCRESET: {
23017                 int     reset_level;
23018 
23019                 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23020                         err = EFAULT;
23021                 } else {
23022                         SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23023                             "reset_level = 0x%lx\n", reset_level);
23024                         if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23025                                 err = 0;
23026                         } else {
23027                                 err = EIO;
23028                         }
23029                 }
23030                 break;
23031         }
23032 
23033         case DKIOCABORT:
23034                 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23035                 if (scsi_abort(SD_ADDRESS(un), NULL)) {
23036                         err = 0;
23037                 } else {
23038                         err = EIO;
23039                 }
23040                 break;
23041 #endif
23042 
23043 #ifdef SD_FAULT_INJECTION
23044 /* SDIOC FaultInjection testing ioctls */
23045         case SDIOCSTART:
23046         case SDIOCSTOP:
23047         case SDIOCINSERTPKT:
23048         case SDIOCINSERTXB:
23049         case SDIOCINSERTUN:
23050         case SDIOCINSERTARQ:
23051         case SDIOCPUSH:
23052         case SDIOCRETRIEVE:
23053         case SDIOCRUN:
23054                 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23055                     "SDIOC detected cmd:0x%X:\n", cmd);
23056                 /* call error generator */
23057                 sd_faultinjection_ioctl(cmd, arg, un);
23058                 err = 0;
23059                 break;
23060 
23061 #endif /* SD_FAULT_INJECTION */
23062 
23063         case DKIOCFLUSHWRITECACHE:
23064                 {
23065                         struct dk_callback *dkc = (struct dk_callback *)arg;
23066 
23067                         mutex_enter(SD_MUTEX(un));
23068                         if (!un->un_f_sync_cache_supported ||
23069                             !un->un_f_write_cache_enabled) {
23070                                 err = un->un_f_sync_cache_supported ?
23071                                     0 : ENOTSUP;
23072                                 mutex_exit(SD_MUTEX(un));
23073                                 if ((flag & FKIOCTL) && dkc != NULL &&
23074                                     dkc->dkc_callback != NULL) {
23075                                         (*dkc->dkc_callback)(dkc->dkc_cookie,
23076                                             err);
23077                                         /*
23078                                          * Did callback and reported error.
23079                                          * Since we did a callback, ioctl
23080                                          * should return 0.
23081                                          */
23082                                         err = 0;
23083                                 }
23084                                 break;
23085                         }
23086                         mutex_exit(SD_MUTEX(un));
23087 
23088                         if ((flag & FKIOCTL) && dkc != NULL &&
23089                             dkc->dkc_callback != NULL) {
23090                                 /* async SYNC CACHE request */
23091                                 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23092                         } else {
23093                                 /* synchronous SYNC CACHE request */
23094                                 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23095                         }
23096                 }
23097                 break;
23098 
23099         case DKIOCGETWCE: {
23100 
23101                 int wce;
23102 
23103                 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23104                         break;
23105                 }
23106 
23107                 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23108                         err = EFAULT;
23109                 }
23110                 break;
23111         }
23112 
23113         case DKIOCSETWCE: {
23114 
23115                 int wce, sync_supported;
23116                 int cur_wce = 0;
23117 
23118                 if (!un->un_f_cache_mode_changeable) {
23119                         err = EINVAL;
23120                         break;
23121                 }
23122 
23123                 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23124                         err = EFAULT;
23125                         break;
23126                 }
23127 
23128                 /*
23129                  * Synchronize multiple threads trying to enable
23130                  * or disable the cache via the un_f_wcc_cv
23131                  * condition variable.
23132                  */
23133                 mutex_enter(SD_MUTEX(un));
23134 
23135                 /*
23136                  * Don't allow the cache to be enabled if the
23137                  * config file has it disabled.
23138                  */
23139                 if (un->un_f_opt_disable_cache && wce) {
23140                         mutex_exit(SD_MUTEX(un));
23141                         err = EINVAL;
23142                         break;
23143                 }
23144 
23145                 /*
23146                  * Wait for write cache change in progress
23147                  * bit to be clear before proceeding.
23148                  */
23149                 while (un->un_f_wcc_inprog)
23150                         cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23151 
23152                 un->un_f_wcc_inprog = 1;
23153 
23154                 mutex_exit(SD_MUTEX(un));
23155 
23156                 /*
23157                  * Get the current write cache state
23158                  */
23159                 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23160                         mutex_enter(SD_MUTEX(un));
23161                         un->un_f_wcc_inprog = 0;
23162                         cv_broadcast(&un->un_wcc_cv);
23163                         mutex_exit(SD_MUTEX(un));
23164                         break;
23165                 }
23166 
23167                 mutex_enter(SD_MUTEX(un));
23168                 un->un_f_write_cache_enabled = (cur_wce != 0);
23169 
23170                 if (un->un_f_write_cache_enabled && wce == 0) {
23171                         /*
23172                          * Disable the write cache.  Don't clear
23173                          * un_f_write_cache_enabled until after
23174                          * the mode select and flush are complete.
23175                          */
23176                         sync_supported = un->un_f_sync_cache_supported;
23177 
23178                         /*
23179                          * If cache flush is suppressed, we assume that the
23180                          * controller firmware will take care of managing the
23181                          * write cache for us: no need to explicitly
23182                          * disable it.
23183                          */
23184                         if (!un->un_f_suppress_cache_flush) {
23185                                 mutex_exit(SD_MUTEX(un));
23186                                 if ((err = sd_cache_control(ssc,
23187                                     SD_CACHE_NOCHANGE,
23188                                     SD_CACHE_DISABLE)) == 0 &&
23189                                     sync_supported) {
23190                                         err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23191                                             NULL);
23192                                 }
23193                         } else {
23194                                 mutex_exit(SD_MUTEX(un));
23195                         }
23196 
23197                         mutex_enter(SD_MUTEX(un));
23198                         if (err == 0) {
23199                                 un->un_f_write_cache_enabled = 0;
23200                         }
23201 
23202                 } else if (!un->un_f_write_cache_enabled && wce != 0) {
23203                         /*
23204                          * Set un_f_write_cache_enabled first, so there is
23205                          * no window where the cache is enabled, but the
23206                          * bit says it isn't.
23207                          */
23208                         un->un_f_write_cache_enabled = 1;
23209 
23210                         /*
23211                          * If cache flush is suppressed, we assume that the
23212                          * controller firmware will take care of managing the
23213                          * write cache for us: no need to explicitly
23214                          * enable it.
23215                          */
23216                         if (!un->un_f_suppress_cache_flush) {
23217                                 mutex_exit(SD_MUTEX(un));
23218                                 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23219                                     SD_CACHE_ENABLE);
23220                         } else {
23221                                 mutex_exit(SD_MUTEX(un));
23222                         }
23223 
23224                         mutex_enter(SD_MUTEX(un));
23225 
23226                         if (err) {
23227                                 un->un_f_write_cache_enabled = 0;
23228                         }
23229                 }
23230 
23231                 un->un_f_wcc_inprog = 0;
23232                 cv_broadcast(&un->un_wcc_cv);
23233                 mutex_exit(SD_MUTEX(un));
23234                 break;
23235         }
23236 
23237         default:
23238                 err = ENOTTY;
23239                 break;
23240         }
23241         mutex_enter(SD_MUTEX(un));
23242         un->un_ncmds_in_driver--;
23243         ASSERT(un->un_ncmds_in_driver >= 0);
23244         mutex_exit(SD_MUTEX(un));
23245 
23246 
23247 done_without_assess:
23248         sd_ssc_fini(ssc);
23249 
23250         SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23251         return (err);
23252 
23253 done_with_assess:
23254         mutex_enter(SD_MUTEX(un));
23255         un->un_ncmds_in_driver--;
23256         ASSERT(un->un_ncmds_in_driver >= 0);
23257         mutex_exit(SD_MUTEX(un));
23258 
23259 done_quick_assess:
23260         if (err != 0)
23261                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23262         /* Uninitialize sd_ssc_t pointer */
23263         sd_ssc_fini(ssc);
23264 
23265         SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23266         return (err);
23267 }
23268 
23269 
23270 /*
23271  *    Function: sd_dkio_ctrl_info
23272  *
23273  * Description: This routine is the driver entry point for handling controller
23274  *              information ioctl requests (DKIOCINFO).
23275  *
23276  *   Arguments: dev  - the device number
23277  *              arg  - pointer to user provided dk_cinfo structure
23278  *                     specifying the controller type and attributes.
23279  *              flag - this argument is a pass through to ddi_copyxxx()
23280  *                     directly from the mode argument of ioctl().
23281  *
23282  * Return Code: 0
23283  *              EFAULT
23284  *              ENXIO
23285  */
23286 
23287 static int
23288 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23289 {
23290         struct sd_lun   *un = NULL;
23291         struct dk_cinfo *info;
23292         dev_info_t      *pdip;
23293         int             lun, tgt;
23294 
23295         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23296                 return (ENXIO);
23297         }
23298 
23299         info = (struct dk_cinfo *)
23300             kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23301 
23302         switch (un->un_ctype) {
23303         case CTYPE_CDROM:
23304                 info->dki_ctype = DKC_CDROM;
23305                 break;
23306         default:
23307                 info->dki_ctype = DKC_SCSI_CCS;
23308                 break;
23309         }
23310         pdip = ddi_get_parent(SD_DEVINFO(un));
23311         info->dki_cnum = ddi_get_instance(pdip);
23312         if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23313                 (void) strcpy(info->dki_cname, ddi_get_name(pdip));
23314         } else {
23315                 (void) strncpy(info->dki_cname, ddi_node_name(pdip),
23316                     DK_DEVLEN - 1);
23317         }
23318 
23319         lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23320             DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23321         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23322             DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23323 
23324         /* Unit Information */
23325         info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23326         info->dki_slave = ((tgt << 3) | lun);
23327         (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23328             DK_DEVLEN - 1);
23329         info->dki_flags = DKI_FMTVOL;
23330         info->dki_partition = SDPART(dev);
23331 
23332         /* Max Transfer size of this device in blocks */
23333         info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23334         info->dki_addr = 0;
23335         info->dki_space = 0;
23336         info->dki_prio = 0;
23337         info->dki_vec = 0;
23338 
23339         if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23340                 kmem_free(info, sizeof (struct dk_cinfo));
23341                 return (EFAULT);
23342         } else {
23343                 kmem_free(info, sizeof (struct dk_cinfo));
23344                 return (0);
23345         }
23346 }
23347 
23348 /*
23349  *    Function: sd_get_media_info_com
23350  *
23351  * Description: This routine returns the information required to populate
23352  *              the fields for the dk_minfo/dk_minfo_ext structures.
23353  *
23354  *   Arguments: dev             - the device number
23355  *              dki_media_type  - media_type
23356  *              dki_lbsize      - logical block size
23357  *              dki_capacity    - capacity in blocks
23358  *              dki_pbsize      - physical block size (if requested)
23359  *
23360  * Return Code: 0
23361  *              EACCESS
23362  *              EFAULT
23363  *              ENXIO
23364  *              EIO
23365  */
23366 static int
23367 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23368     diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23369 {
23370         struct sd_lun           *un = NULL;
23371         struct uscsi_cmd        com;
23372         struct scsi_inquiry     *sinq;
23373         u_longlong_t            media_capacity;
23374         uint64_t                capacity;
23375         uint_t                  lbasize;
23376         uint_t                  pbsize;
23377         uchar_t                 *out_data;
23378         uchar_t                 *rqbuf;
23379         int                     rval = 0;
23380         int                     rtn;
23381         sd_ssc_t                *ssc;
23382 
23383         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23384             (un->un_state == SD_STATE_OFFLINE)) {
23385                 return (ENXIO);
23386         }
23387 
23388         SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23389 
23390         out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23391         rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23392         ssc = sd_ssc_init(un);
23393 
23394         /* Issue a TUR to determine if the drive is ready with media present */
23395         rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23396         if (rval == ENXIO) {
23397                 goto done;
23398         } else if (rval != 0) {
23399                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23400         }
23401 
23402         /* Now get configuration data */
23403         if (ISCD(un)) {
23404                 *dki_media_type = DK_CDROM;
23405 
23406                 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23407                 if (un->un_f_mmc_cap == TRUE) {
23408                         rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23409                             SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23410                             SD_PATH_STANDARD);
23411 
23412                         if (rtn) {
23413                                 /*
23414                                  * We ignore all failures for CD and need to
23415                                  * put the assessment before processing code
23416                                  * to avoid missing assessment for FMA.
23417                                  */
23418                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23419                                 /*
23420                                  * Failed for other than an illegal request
23421                                  * or command not supported
23422                                  */
23423                                 if ((com.uscsi_status == STATUS_CHECK) &&
23424                                     (com.uscsi_rqstatus == STATUS_GOOD)) {
23425                                         if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23426                                             (rqbuf[12] != 0x20)) {
23427                                                 rval = EIO;
23428                                                 goto no_assessment;
23429                                         }
23430                                 }
23431                         } else {
23432                                 /*
23433                                  * The GET CONFIGURATION command succeeded
23434                                  * so set the media type according to the
23435                                  * returned data
23436                                  */
23437                                 *dki_media_type = out_data[6];
23438                                 *dki_media_type <<= 8;
23439                                 *dki_media_type |= out_data[7];
23440                         }
23441                 }
23442         } else {
23443                 /*
23444                  * The profile list is not available, so we attempt to identify
23445                  * the media type based on the inquiry data
23446                  */
23447                 sinq = un->un_sd->sd_inq;
23448                 if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23449                     (sinq->inq_dtype == DTYPE_OPTICAL)) {
23450                         /* This is a direct access device  or optical disk */
23451                         *dki_media_type = DK_FIXED_DISK;
23452 
23453                         if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23454                             (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23455                                 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23456                                         *dki_media_type = DK_ZIP;
23457                                 } else if (
23458                                     (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23459                                         *dki_media_type = DK_JAZ;
23460                                 }
23461                         }
23462                 } else {
23463                         /*
23464                          * Not a CD, direct access or optical disk so return
23465                          * unknown media
23466                          */
23467                         *dki_media_type = DK_UNKNOWN;
23468                 }
23469         }
23470 
23471         /*
23472          * Now read the capacity so we can provide the lbasize,
23473          * pbsize and capacity.
23474          */
23475         if (dki_pbsize && un->un_f_descr_format_supported) {
23476                 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23477                     &pbsize, SD_PATH_DIRECT);
23478 
23479                 /*
23480                  * Override the physical blocksize if the instance already
23481                  * has a larger value.
23482                  */
23483                 pbsize = MAX(pbsize, un->un_phy_blocksize);
23484         }
23485 
23486         if (dki_pbsize == NULL || rval != 0 ||
23487             !un->un_f_descr_format_supported) {
23488                 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23489                     SD_PATH_DIRECT);
23490 
23491                 switch (rval) {
23492                 case 0:
23493                         if (un->un_f_enable_rmw &&
23494                             un->un_phy_blocksize != 0) {
23495                                 pbsize = un->un_phy_blocksize;
23496                         } else {
23497                                 pbsize = lbasize;
23498                         }
23499                         media_capacity = capacity;
23500 
23501                         /*
23502                          * sd_send_scsi_READ_CAPACITY() reports capacity in
23503                          * un->un_sys_blocksize chunks. So we need to convert
23504                          * it into cap.lbsize chunks.
23505                          */
23506                         if (un->un_f_has_removable_media) {
23507                                 media_capacity *= un->un_sys_blocksize;
23508                                 media_capacity /= lbasize;
23509                         }
23510                         break;
23511                 case EACCES:
23512                         rval = EACCES;
23513                         goto done;
23514                 default:
23515                         rval = EIO;
23516                         goto done;
23517                 }
23518         } else {
23519                 if (un->un_f_enable_rmw &&
23520                     !ISP2(pbsize % DEV_BSIZE)) {
23521                         pbsize = SSD_SECSIZE;
23522                 } else if (!ISP2(lbasize % DEV_BSIZE) ||
23523                     !ISP2(pbsize % DEV_BSIZE)) {
23524                         pbsize = lbasize = DEV_BSIZE;
23525                 }
23526                 media_capacity = capacity;
23527         }
23528 
23529         /*
23530          * If lun is expanded dynamically, update the un structure.
23531          */
23532         mutex_enter(SD_MUTEX(un));
23533         if ((un->un_f_blockcount_is_valid == TRUE) &&
23534             (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23535             (capacity > un->un_blockcount)) {
23536                 un->un_f_expnevent = B_FALSE;
23537                 sd_update_block_info(un, lbasize, capacity);
23538         }
23539         mutex_exit(SD_MUTEX(un));
23540 
23541         *dki_lbsize = lbasize;
23542         *dki_capacity = media_capacity;
23543         if (dki_pbsize)
23544                 *dki_pbsize = pbsize;
23545 
23546 done:
23547         if (rval != 0) {
23548                 if (rval == EIO)
23549                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23550                 else
23551                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23552         }
23553 no_assessment:
23554         sd_ssc_fini(ssc);
23555         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23556         kmem_free(rqbuf, SENSE_LENGTH);
23557         return (rval);
23558 }
23559 
23560 /*
23561  *    Function: sd_get_media_info
23562  *
23563  * Description: This routine is the driver entry point for handling ioctl
23564  *              requests for the media type or command set profile used by the
23565  *              drive to operate on the media (DKIOCGMEDIAINFO).
23566  *
23567  *   Arguments: dev     - the device number
23568  *              arg     - pointer to user provided dk_minfo structure
23569  *                        specifying the media type, logical block size and
23570  *                        drive capacity.
23571  *              flag    - this argument is a pass through to ddi_copyxxx()
23572  *                        directly from the mode argument of ioctl().
23573  *
23574  * Return Code: returns the value from sd_get_media_info_com
23575  */
23576 static int
23577 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23578 {
23579         struct dk_minfo         mi;
23580         int                     rval;
23581 
23582         rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23583             &mi.dki_lbsize, &mi.dki_capacity, NULL);
23584 
23585         if (rval)
23586                 return (rval);
23587         if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23588                 rval = EFAULT;
23589         return (rval);
23590 }
23591 
23592 /*
23593  *    Function: sd_get_media_info_ext
23594  *
23595  * Description: This routine is the driver entry point for handling ioctl
23596  *              requests for the media type or command set profile used by the
23597  *