1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 /* 27 * SCSI disk target driver. 28 */ 29 #include <sys/scsi/scsi.h> 30 #include <sys/dkbad.h> 31 #include <sys/dklabel.h> 32 #include <sys/dkio.h> 33 #include <sys/fdio.h> 34 #include <sys/cdio.h> 35 #include <sys/mhd.h> 36 #include <sys/vtoc.h> 37 #include <sys/dktp/fdisk.h> 38 #include <sys/kstat.h> 39 #include <sys/vtrace.h> 40 #include <sys/note.h> 41 #include <sys/thread.h> 42 #include <sys/proc.h> 43 #include <sys/efi_partition.h> 44 #include <sys/var.h> 45 #include <sys/aio_req.h> 46 47 #ifdef __lock_lint 48 #define _LP64 49 #define __amd64 50 #endif 51 52 #if (defined(__fibre)) 53 /* Note: is there a leadville version of the following? */ 54 #include <sys/fc4/fcal_linkapp.h> 55 #endif 56 #include <sys/taskq.h> 57 #include <sys/uuid.h> 58 #include <sys/byteorder.h> 59 #include <sys/sdt.h> 60 61 #include "sd_xbuf.h" 62 63 #include <sys/scsi/targets/sddef.h> 64 #include <sys/cmlb.h> 65 #include <sys/sysevent/eventdefs.h> 66 #include <sys/sysevent/dev.h> 67 68 #include <sys/fm/protocol.h> 69 70 /* 71 * Loadable module info. 72 */ 73 #if (defined(__fibre)) 74 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver" 75 char _depends_on[] = "misc/scsi misc/cmlb drv/fcp"; 76 #else /* !__fibre */ 77 #define SD_MODULE_NAME "SCSI Disk Driver" 78 char _depends_on[] = "misc/scsi misc/cmlb"; 79 #endif /* !__fibre */ 80 81 /* 82 * Define the interconnect type, to allow the driver to distinguish 83 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 84 * 85 * This is really for backward compatibility. In the future, the driver 86 * should actually check the "interconnect-type" property as reported by 87 * the HBA; however at present this property is not defined by all HBAs, 88 * so we will use this #define (1) to permit the driver to run in 89 * backward-compatibility mode; and (2) to print a notification message 90 * if an FC HBA does not support the "interconnect-type" property. The 91 * behavior of the driver will be to assume parallel SCSI behaviors unless 92 * the "interconnect-type" property is defined by the HBA **AND** has a 93 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 94 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 95 * Channel behaviors (as per the old ssd). (Note that the 96 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 97 * will result in the driver assuming parallel SCSI behaviors.) 98 * 99 * (see common/sys/scsi/impl/services.h) 100 * 101 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 102 * since some FC HBAs may already support that, and there is some code in 103 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 104 * default would confuse that code, and besides things should work fine 105 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 106 * "interconnect_type" property. 107 * 108 */ 109 #if (defined(__fibre)) 110 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 111 #else 112 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 113 #endif 114 115 /* 116 * The name of the driver, established from the module name in _init. 117 */ 118 static char *sd_label = NULL; 119 120 /* 121 * Driver name is unfortunately prefixed on some driver.conf properties. 122 */ 123 #if (defined(__fibre)) 124 #define sd_max_xfer_size ssd_max_xfer_size 125 #define sd_config_list ssd_config_list 126 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 127 static char *sd_config_list = "ssd-config-list"; 128 #else 129 static char *sd_max_xfer_size = "sd_max_xfer_size"; 130 static char *sd_config_list = "sd-config-list"; 131 #endif 132 133 /* 134 * Driver global variables 135 */ 136 137 #if (defined(__fibre)) 138 /* 139 * These #defines are to avoid namespace collisions that occur because this 140 * code is currently used to compile two separate driver modules: sd and ssd. 141 * All global variables need to be treated this way (even if declared static) 142 * in order to allow the debugger to resolve the names properly. 143 * It is anticipated that in the near future the ssd module will be obsoleted, 144 * at which time this namespace issue should go away. 145 */ 146 #define sd_state ssd_state 147 #define sd_io_time ssd_io_time 148 #define sd_failfast_enable ssd_failfast_enable 149 #define sd_ua_retry_count ssd_ua_retry_count 150 #define sd_report_pfa ssd_report_pfa 151 #define sd_max_throttle ssd_max_throttle 152 #define sd_min_throttle ssd_min_throttle 153 #define sd_rot_delay ssd_rot_delay 154 155 #define sd_retry_on_reservation_conflict \ 156 ssd_retry_on_reservation_conflict 157 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 158 #define sd_resv_conflict_name ssd_resv_conflict_name 159 160 #define sd_component_mask ssd_component_mask 161 #define sd_level_mask ssd_level_mask 162 #define sd_debug_un ssd_debug_un 163 #define sd_error_level ssd_error_level 164 165 #define sd_xbuf_active_limit ssd_xbuf_active_limit 166 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 167 168 #define sd_tr ssd_tr 169 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 170 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 171 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 172 #define sd_check_media_time ssd_check_media_time 173 #define sd_wait_cmds_complete ssd_wait_cmds_complete 174 #define sd_label_mutex ssd_label_mutex 175 #define sd_detach_mutex ssd_detach_mutex 176 #define sd_log_buf ssd_log_buf 177 #define sd_log_mutex ssd_log_mutex 178 179 #define sd_disk_table ssd_disk_table 180 #define sd_disk_table_size ssd_disk_table_size 181 #define sd_sense_mutex ssd_sense_mutex 182 #define sd_cdbtab ssd_cdbtab 183 184 #define sd_cb_ops ssd_cb_ops 185 #define sd_ops ssd_ops 186 #define sd_additional_codes ssd_additional_codes 187 #define sd_tgops ssd_tgops 188 189 #define sd_minor_data ssd_minor_data 190 #define sd_minor_data_efi ssd_minor_data_efi 191 192 #define sd_tq ssd_tq 193 #define sd_wmr_tq ssd_wmr_tq 194 #define sd_taskq_name ssd_taskq_name 195 #define sd_wmr_taskq_name ssd_wmr_taskq_name 196 #define sd_taskq_minalloc ssd_taskq_minalloc 197 #define sd_taskq_maxalloc ssd_taskq_maxalloc 198 199 #define sd_dump_format_string ssd_dump_format_string 200 201 #define sd_iostart_chain ssd_iostart_chain 202 #define sd_iodone_chain ssd_iodone_chain 203 204 #define sd_pm_idletime ssd_pm_idletime 205 206 #define sd_force_pm_supported ssd_force_pm_supported 207 208 #define sd_dtype_optical_bind ssd_dtype_optical_bind 209 210 #define sd_ssc_init ssd_ssc_init 211 #define sd_ssc_send ssd_ssc_send 212 #define sd_ssc_fini ssd_ssc_fini 213 #define sd_ssc_assessment ssd_ssc_assessment 214 #define sd_ssc_post ssd_ssc_post 215 #define sd_ssc_print ssd_ssc_print 216 #define sd_ssc_ereport_post ssd_ssc_ereport_post 217 #define sd_ssc_set_info ssd_ssc_set_info 218 #define sd_ssc_extract_info ssd_ssc_extract_info 219 220 #endif 221 222 #ifdef SDDEBUG 223 int sd_force_pm_supported = 0; 224 #endif /* SDDEBUG */ 225 226 void *sd_state = NULL; 227 int sd_io_time = SD_IO_TIME; 228 int sd_failfast_enable = 1; 229 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 230 int sd_report_pfa = 1; 231 int sd_max_throttle = SD_MAX_THROTTLE; 232 int sd_min_throttle = SD_MIN_THROTTLE; 233 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 234 int sd_qfull_throttle_enable = TRUE; 235 236 int sd_retry_on_reservation_conflict = 1; 237 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 238 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 239 240 static int sd_dtype_optical_bind = -1; 241 242 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 243 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 244 245 /* 246 * Global data for debug logging. To enable debug printing, sd_component_mask 247 * and sd_level_mask should be set to the desired bit patterns as outlined in 248 * sddef.h. 249 */ 250 uint_t sd_component_mask = 0x0; 251 uint_t sd_level_mask = 0x0; 252 struct sd_lun *sd_debug_un = NULL; 253 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 254 255 /* Note: these may go away in the future... */ 256 static uint32_t sd_xbuf_active_limit = 512; 257 static uint32_t sd_xbuf_reserve_limit = 16; 258 259 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 260 261 /* 262 * Timer value used to reset the throttle after it has been reduced 263 * (typically in response to TRAN_BUSY or STATUS_QFULL) 264 */ 265 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 266 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 267 268 /* 269 * Interval value associated with the media change scsi watch. 270 */ 271 static int sd_check_media_time = 3000000; 272 273 /* 274 * Wait value used for in progress operations during a DDI_SUSPEND 275 */ 276 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 277 278 /* 279 * sd_label_mutex protects a static buffer used in the disk label 280 * component of the driver 281 */ 282 static kmutex_t sd_label_mutex; 283 284 /* 285 * sd_detach_mutex protects un_layer_count, un_detach_count, and 286 * un_opens_in_progress in the sd_lun structure. 287 */ 288 static kmutex_t sd_detach_mutex; 289 290 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 291 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 292 293 /* 294 * Global buffer and mutex for debug logging 295 */ 296 static char sd_log_buf[1024]; 297 static kmutex_t sd_log_mutex; 298 299 /* 300 * Structs and globals for recording attached lun information. 301 * This maintains a chain. Each node in the chain represents a SCSI controller. 302 * The structure records the number of luns attached to each target connected 303 * with the controller. 304 * For parallel scsi device only. 305 */ 306 struct sd_scsi_hba_tgt_lun { 307 struct sd_scsi_hba_tgt_lun *next; 308 dev_info_t *pdip; 309 int nlun[NTARGETS_WIDE]; 310 }; 311 312 /* 313 * Flag to indicate the lun is attached or detached 314 */ 315 #define SD_SCSI_LUN_ATTACH 0 316 #define SD_SCSI_LUN_DETACH 1 317 318 static kmutex_t sd_scsi_target_lun_mutex; 319 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 320 321 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 322 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 323 324 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 325 sd_scsi_target_lun_head)) 326 327 /* 328 * "Smart" Probe Caching structs, globals, #defines, etc. 329 * For parallel scsi and non-self-identify device only. 330 */ 331 332 /* 333 * The following resources and routines are implemented to support 334 * "smart" probing, which caches the scsi_probe() results in an array, 335 * in order to help avoid long probe times. 336 */ 337 struct sd_scsi_probe_cache { 338 struct sd_scsi_probe_cache *next; 339 dev_info_t *pdip; 340 int cache[NTARGETS_WIDE]; 341 }; 342 343 static kmutex_t sd_scsi_probe_cache_mutex; 344 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 345 346 /* 347 * Really we only need protection on the head of the linked list, but 348 * better safe than sorry. 349 */ 350 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 351 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 352 353 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 354 sd_scsi_probe_cache_head)) 355 356 /* 357 * Power attribute table 358 */ 359 static sd_power_attr_ss sd_pwr_ss = { 360 { "NAME=spindle-motor", "0=off", "1=on", NULL }, 361 {0, 100}, 362 {30, 0}, 363 {20000, 0} 364 }; 365 366 static sd_power_attr_pc sd_pwr_pc = { 367 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle", 368 "3=active", NULL }, 369 {0, 0, 0, 100}, 370 {90, 90, 20, 0}, 371 {15000, 15000, 1000, 0} 372 }; 373 374 /* 375 * Power level to power condition 376 */ 377 static int sd_pl2pc[] = { 378 SD_TARGET_START_VALID, 379 SD_TARGET_STANDBY, 380 SD_TARGET_IDLE, 381 SD_TARGET_ACTIVE 382 }; 383 384 /* 385 * Vendor specific data name property declarations 386 */ 387 388 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 389 390 static sd_tunables seagate_properties = { 391 SEAGATE_THROTTLE_VALUE, 392 0, 393 0, 394 0, 395 0, 396 0, 397 0, 398 0, 399 0 400 }; 401 402 403 static sd_tunables fujitsu_properties = { 404 FUJITSU_THROTTLE_VALUE, 405 0, 406 0, 407 0, 408 0, 409 0, 410 0, 411 0, 412 0 413 }; 414 415 static sd_tunables ibm_properties = { 416 IBM_THROTTLE_VALUE, 417 0, 418 0, 419 0, 420 0, 421 0, 422 0, 423 0, 424 0 425 }; 426 427 static sd_tunables purple_properties = { 428 PURPLE_THROTTLE_VALUE, 429 0, 430 0, 431 PURPLE_BUSY_RETRIES, 432 PURPLE_RESET_RETRY_COUNT, 433 PURPLE_RESERVE_RELEASE_TIME, 434 0, 435 0, 436 0 437 }; 438 439 static sd_tunables sve_properties = { 440 SVE_THROTTLE_VALUE, 441 0, 442 0, 443 SVE_BUSY_RETRIES, 444 SVE_RESET_RETRY_COUNT, 445 SVE_RESERVE_RELEASE_TIME, 446 SVE_MIN_THROTTLE_VALUE, 447 SVE_DISKSORT_DISABLED_FLAG, 448 0 449 }; 450 451 static sd_tunables maserati_properties = { 452 0, 453 0, 454 0, 455 0, 456 0, 457 0, 458 0, 459 MASERATI_DISKSORT_DISABLED_FLAG, 460 MASERATI_LUN_RESET_ENABLED_FLAG 461 }; 462 463 static sd_tunables pirus_properties = { 464 PIRUS_THROTTLE_VALUE, 465 0, 466 PIRUS_NRR_COUNT, 467 PIRUS_BUSY_RETRIES, 468 PIRUS_RESET_RETRY_COUNT, 469 0, 470 PIRUS_MIN_THROTTLE_VALUE, 471 PIRUS_DISKSORT_DISABLED_FLAG, 472 PIRUS_LUN_RESET_ENABLED_FLAG 473 }; 474 475 #endif 476 477 #if (defined(__sparc) && !defined(__fibre)) || \ 478 (defined(__i386) || defined(__amd64)) 479 480 481 static sd_tunables elite_properties = { 482 ELITE_THROTTLE_VALUE, 483 0, 484 0, 485 0, 486 0, 487 0, 488 0, 489 0, 490 0 491 }; 492 493 static sd_tunables st31200n_properties = { 494 ST31200N_THROTTLE_VALUE, 495 0, 496 0, 497 0, 498 0, 499 0, 500 0, 501 0, 502 0 503 }; 504 505 #endif /* Fibre or not */ 506 507 static sd_tunables lsi_properties_scsi = { 508 LSI_THROTTLE_VALUE, 509 0, 510 LSI_NOTREADY_RETRIES, 511 0, 512 0, 513 0, 514 0, 515 0, 516 0 517 }; 518 519 static sd_tunables symbios_properties = { 520 SYMBIOS_THROTTLE_VALUE, 521 0, 522 SYMBIOS_NOTREADY_RETRIES, 523 0, 524 0, 525 0, 526 0, 527 0, 528 0 529 }; 530 531 static sd_tunables lsi_properties = { 532 0, 533 0, 534 LSI_NOTREADY_RETRIES, 535 0, 536 0, 537 0, 538 0, 539 0, 540 0 541 }; 542 543 static sd_tunables lsi_oem_properties = { 544 0, 545 0, 546 LSI_OEM_NOTREADY_RETRIES, 547 0, 548 0, 549 0, 550 0, 551 0, 552 0, 553 1 554 }; 555 556 557 558 #if (defined(SD_PROP_TST)) 559 560 #define SD_TST_CTYPE_VAL CTYPE_CDROM 561 #define SD_TST_THROTTLE_VAL 16 562 #define SD_TST_NOTREADY_VAL 12 563 #define SD_TST_BUSY_VAL 60 564 #define SD_TST_RST_RETRY_VAL 36 565 #define SD_TST_RSV_REL_TIME 60 566 567 static sd_tunables tst_properties = { 568 SD_TST_THROTTLE_VAL, 569 SD_TST_CTYPE_VAL, 570 SD_TST_NOTREADY_VAL, 571 SD_TST_BUSY_VAL, 572 SD_TST_RST_RETRY_VAL, 573 SD_TST_RSV_REL_TIME, 574 0, 575 0, 576 0 577 }; 578 #endif 579 580 /* This is similar to the ANSI toupper implementation */ 581 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 582 583 /* 584 * Static Driver Configuration Table 585 * 586 * This is the table of disks which need throttle adjustment (or, perhaps 587 * something else as defined by the flags at a future time.) device_id 588 * is a string consisting of concatenated vid (vendor), pid (product/model) 589 * and revision strings as defined in the scsi_inquiry structure. Offsets of 590 * the parts of the string are as defined by the sizes in the scsi_inquiry 591 * structure. Device type is searched as far as the device_id string is 592 * defined. Flags defines which values are to be set in the driver from the 593 * properties list. 594 * 595 * Entries below which begin and end with a "*" are a special case. 596 * These do not have a specific vendor, and the string which follows 597 * can appear anywhere in the 16 byte PID portion of the inquiry data. 598 * 599 * Entries below which begin and end with a " " (blank) are a special 600 * case. The comparison function will treat multiple consecutive blanks 601 * as equivalent to a single blank. For example, this causes a 602 * sd_disk_table entry of " NEC CDROM " to match a device's id string 603 * of "NEC CDROM". 604 * 605 * Note: The MD21 controller type has been obsoleted. 606 * ST318202F is a Legacy device 607 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 608 * made with an FC connection. The entries here are a legacy. 609 */ 610 static sd_disk_config_t sd_disk_table[] = { 611 #if defined(__fibre) || defined(__i386) || defined(__amd64) 612 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 613 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 614 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 615 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 616 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 617 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 618 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 619 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 620 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 621 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 622 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 623 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 624 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 625 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 626 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 627 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 628 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 629 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 630 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 631 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 632 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 633 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 634 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 635 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 636 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 637 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 638 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 639 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 640 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 641 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 642 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 643 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 644 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 645 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 648 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 649 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 650 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 652 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 653 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 654 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 655 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 656 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 657 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 658 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 659 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 660 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 661 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT | 662 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 663 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT | 664 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 665 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 666 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 667 { "SUN T3", SD_CONF_BSET_THROTTLE | 668 SD_CONF_BSET_BSY_RETRY_COUNT| 669 SD_CONF_BSET_RST_RETRIES| 670 SD_CONF_BSET_RSV_REL_TIME, 671 &purple_properties }, 672 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 673 SD_CONF_BSET_BSY_RETRY_COUNT| 674 SD_CONF_BSET_RST_RETRIES| 675 SD_CONF_BSET_RSV_REL_TIME| 676 SD_CONF_BSET_MIN_THROTTLE| 677 SD_CONF_BSET_DISKSORT_DISABLED, 678 &sve_properties }, 679 { "SUN T4", 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 &purple_properties }, 684 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 685 SD_CONF_BSET_LUN_RESET_ENABLED, 686 &maserati_properties }, 687 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 688 SD_CONF_BSET_NRR_COUNT| 689 SD_CONF_BSET_BSY_RETRY_COUNT| 690 SD_CONF_BSET_RST_RETRIES| 691 SD_CONF_BSET_MIN_THROTTLE| 692 SD_CONF_BSET_DISKSORT_DISABLED| 693 SD_CONF_BSET_LUN_RESET_ENABLED, 694 &pirus_properties }, 695 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 696 SD_CONF_BSET_NRR_COUNT| 697 SD_CONF_BSET_BSY_RETRY_COUNT| 698 SD_CONF_BSET_RST_RETRIES| 699 SD_CONF_BSET_MIN_THROTTLE| 700 SD_CONF_BSET_DISKSORT_DISABLED| 701 SD_CONF_BSET_LUN_RESET_ENABLED, 702 &pirus_properties }, 703 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 704 SD_CONF_BSET_NRR_COUNT| 705 SD_CONF_BSET_BSY_RETRY_COUNT| 706 SD_CONF_BSET_RST_RETRIES| 707 SD_CONF_BSET_MIN_THROTTLE| 708 SD_CONF_BSET_DISKSORT_DISABLED| 709 SD_CONF_BSET_LUN_RESET_ENABLED, 710 &pirus_properties }, 711 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 712 SD_CONF_BSET_NRR_COUNT| 713 SD_CONF_BSET_BSY_RETRY_COUNT| 714 SD_CONF_BSET_RST_RETRIES| 715 SD_CONF_BSET_MIN_THROTTLE| 716 SD_CONF_BSET_DISKSORT_DISABLED| 717 SD_CONF_BSET_LUN_RESET_ENABLED, 718 &pirus_properties }, 719 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 720 SD_CONF_BSET_NRR_COUNT| 721 SD_CONF_BSET_BSY_RETRY_COUNT| 722 SD_CONF_BSET_RST_RETRIES| 723 SD_CONF_BSET_MIN_THROTTLE| 724 SD_CONF_BSET_DISKSORT_DISABLED| 725 SD_CONF_BSET_LUN_RESET_ENABLED, 726 &pirus_properties }, 727 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 728 SD_CONF_BSET_NRR_COUNT| 729 SD_CONF_BSET_BSY_RETRY_COUNT| 730 SD_CONF_BSET_RST_RETRIES| 731 SD_CONF_BSET_MIN_THROTTLE| 732 SD_CONF_BSET_DISKSORT_DISABLED| 733 SD_CONF_BSET_LUN_RESET_ENABLED, 734 &pirus_properties }, 735 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 736 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 737 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 738 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 739 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 740 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 741 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 742 #endif /* fibre or NON-sparc platforms */ 743 #if ((defined(__sparc) && !defined(__fibre)) ||\ 744 (defined(__i386) || defined(__amd64))) 745 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 746 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 747 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 748 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 749 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 750 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 751 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 752 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 753 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 754 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 755 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 756 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 757 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 758 &symbios_properties }, 759 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 760 &lsi_properties_scsi }, 761 #if defined(__i386) || defined(__amd64) 762 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 763 | SD_CONF_BSET_READSUB_BCD 764 | SD_CONF_BSET_READ_TOC_ADDR_BCD 765 | SD_CONF_BSET_NO_READ_HEADER 766 | SD_CONF_BSET_READ_CD_XD4), NULL }, 767 768 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 769 | SD_CONF_BSET_READSUB_BCD 770 | SD_CONF_BSET_READ_TOC_ADDR_BCD 771 | SD_CONF_BSET_NO_READ_HEADER 772 | SD_CONF_BSET_READ_CD_XD4), NULL }, 773 #endif /* __i386 || __amd64 */ 774 #endif /* sparc NON-fibre or NON-sparc platforms */ 775 776 #if (defined(SD_PROP_TST)) 777 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 778 | SD_CONF_BSET_CTYPE 779 | SD_CONF_BSET_NRR_COUNT 780 | SD_CONF_BSET_FAB_DEVID 781 | SD_CONF_BSET_NOCACHE 782 | SD_CONF_BSET_BSY_RETRY_COUNT 783 | SD_CONF_BSET_PLAYMSF_BCD 784 | SD_CONF_BSET_READSUB_BCD 785 | SD_CONF_BSET_READ_TOC_TRK_BCD 786 | SD_CONF_BSET_READ_TOC_ADDR_BCD 787 | SD_CONF_BSET_NO_READ_HEADER 788 | SD_CONF_BSET_READ_CD_XD4 789 | SD_CONF_BSET_RST_RETRIES 790 | SD_CONF_BSET_RSV_REL_TIME 791 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 792 #endif 793 }; 794 795 static const int sd_disk_table_size = 796 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 797 798 /* 799 * Emulation mode disk drive VID/PID table 800 */ 801 static char sd_flash_dev_table[][25] = { 802 "ATA MARVELL SD88SA02", 803 "MARVELL SD88SA02", 804 "TOSHIBA THNSNV05", 805 }; 806 807 static const int sd_flash_dev_table_size = 808 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]); 809 810 #define SD_INTERCONNECT_PARALLEL 0 811 #define SD_INTERCONNECT_FABRIC 1 812 #define SD_INTERCONNECT_FIBRE 2 813 #define SD_INTERCONNECT_SSA 3 814 #define SD_INTERCONNECT_SATA 4 815 #define SD_INTERCONNECT_SAS 5 816 817 #define SD_IS_PARALLEL_SCSI(un) \ 818 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 819 #define SD_IS_SERIAL(un) \ 820 (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\ 821 ((un)->un_interconnect_type == SD_INTERCONNECT_SAS)) 822 823 /* 824 * Definitions used by device id registration routines 825 */ 826 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 827 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 828 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 829 830 static kmutex_t sd_sense_mutex = {0}; 831 832 /* 833 * Macros for updates of the driver state 834 */ 835 #define New_state(un, s) \ 836 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 837 #define Restore_state(un) \ 838 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 839 840 static struct sd_cdbinfo sd_cdbtab[] = { 841 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 842 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 843 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 844 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 845 }; 846 847 /* 848 * Specifies the number of seconds that must have elapsed since the last 849 * cmd. has completed for a device to be declared idle to the PM framework. 850 */ 851 static int sd_pm_idletime = 1; 852 853 /* 854 * Internal function prototypes 855 */ 856 857 #if (defined(__fibre)) 858 /* 859 * These #defines are to avoid namespace collisions that occur because this 860 * code is currently used to compile two separate driver modules: sd and ssd. 861 * All function names need to be treated this way (even if declared static) 862 * in order to allow the debugger to resolve the names properly. 863 * It is anticipated that in the near future the ssd module will be obsoleted, 864 * at which time this ugliness should go away. 865 */ 866 #define sd_log_trace ssd_log_trace 867 #define sd_log_info ssd_log_info 868 #define sd_log_err ssd_log_err 869 #define sdprobe ssdprobe 870 #define sdinfo ssdinfo 871 #define sd_prop_op ssd_prop_op 872 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 873 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 874 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 875 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 876 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 877 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 878 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 879 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 880 #define sd_spin_up_unit ssd_spin_up_unit 881 #define sd_enable_descr_sense ssd_enable_descr_sense 882 #define sd_reenable_dsense_task ssd_reenable_dsense_task 883 #define sd_set_mmc_caps ssd_set_mmc_caps 884 #define sd_read_unit_properties ssd_read_unit_properties 885 #define sd_process_sdconf_file ssd_process_sdconf_file 886 #define sd_process_sdconf_table ssd_process_sdconf_table 887 #define sd_sdconf_id_match ssd_sdconf_id_match 888 #define sd_blank_cmp ssd_blank_cmp 889 #define sd_chk_vers1_data ssd_chk_vers1_data 890 #define sd_set_vers1_properties ssd_set_vers1_properties 891 #define sd_check_solid_state ssd_check_solid_state 892 #define sd_check_emulation_mode ssd_check_emulation_mode 893 894 #define sd_get_physical_geometry ssd_get_physical_geometry 895 #define sd_get_virtual_geometry ssd_get_virtual_geometry 896 #define sd_update_block_info ssd_update_block_info 897 #define sd_register_devid ssd_register_devid 898 #define sd_get_devid ssd_get_devid 899 #define sd_create_devid ssd_create_devid 900 #define sd_write_deviceid ssd_write_deviceid 901 #define sd_check_vpd_page_support ssd_check_vpd_page_support 902 #define sd_setup_pm ssd_setup_pm 903 #define sd_create_pm_components ssd_create_pm_components 904 #define sd_ddi_suspend ssd_ddi_suspend 905 #define sd_ddi_resume ssd_ddi_resume 906 #define sd_pm_state_change ssd_pm_state_change 907 #define sdpower ssdpower 908 #define sdattach ssdattach 909 #define sddetach ssddetach 910 #define sd_unit_attach ssd_unit_attach 911 #define sd_unit_detach ssd_unit_detach 912 #define sd_set_unit_attributes ssd_set_unit_attributes 913 #define sd_create_errstats ssd_create_errstats 914 #define sd_set_errstats ssd_set_errstats 915 #define sd_set_pstats ssd_set_pstats 916 #define sddump ssddump 917 #define sd_scsi_poll ssd_scsi_poll 918 #define sd_send_polled_RQS ssd_send_polled_RQS 919 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 920 #define sd_init_event_callbacks ssd_init_event_callbacks 921 #define sd_event_callback ssd_event_callback 922 #define sd_cache_control ssd_cache_control 923 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 924 #define sd_get_nv_sup ssd_get_nv_sup 925 #define sd_make_device ssd_make_device 926 #define sdopen ssdopen 927 #define sdclose ssdclose 928 #define sd_ready_and_valid ssd_ready_and_valid 929 #define sdmin ssdmin 930 #define sdread ssdread 931 #define sdwrite ssdwrite 932 #define sdaread ssdaread 933 #define sdawrite ssdawrite 934 #define sdstrategy ssdstrategy 935 #define sdioctl ssdioctl 936 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 937 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 938 #define sd_checksum_iostart ssd_checksum_iostart 939 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 940 #define sd_pm_iostart ssd_pm_iostart 941 #define sd_core_iostart ssd_core_iostart 942 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 943 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 944 #define sd_checksum_iodone ssd_checksum_iodone 945 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 946 #define sd_pm_iodone ssd_pm_iodone 947 #define sd_initpkt_for_buf ssd_initpkt_for_buf 948 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 949 #define sd_setup_rw_pkt ssd_setup_rw_pkt 950 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 951 #define sd_buf_iodone ssd_buf_iodone 952 #define sd_uscsi_strategy ssd_uscsi_strategy 953 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 954 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 955 #define sd_uscsi_iodone ssd_uscsi_iodone 956 #define sd_xbuf_strategy ssd_xbuf_strategy 957 #define sd_xbuf_init ssd_xbuf_init 958 #define sd_pm_entry ssd_pm_entry 959 #define sd_pm_exit ssd_pm_exit 960 961 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 962 #define sd_pm_timeout_handler ssd_pm_timeout_handler 963 964 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 965 #define sdintr ssdintr 966 #define sd_start_cmds ssd_start_cmds 967 #define sd_send_scsi_cmd ssd_send_scsi_cmd 968 #define sd_bioclone_alloc ssd_bioclone_alloc 969 #define sd_bioclone_free ssd_bioclone_free 970 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 971 #define sd_shadow_buf_free ssd_shadow_buf_free 972 #define sd_print_transport_rejected_message \ 973 ssd_print_transport_rejected_message 974 #define sd_retry_command ssd_retry_command 975 #define sd_set_retry_bp ssd_set_retry_bp 976 #define sd_send_request_sense_command ssd_send_request_sense_command 977 #define sd_start_retry_command ssd_start_retry_command 978 #define sd_start_direct_priority_command \ 979 ssd_start_direct_priority_command 980 #define sd_return_failed_command ssd_return_failed_command 981 #define sd_return_failed_command_no_restart \ 982 ssd_return_failed_command_no_restart 983 #define sd_return_command ssd_return_command 984 #define sd_sync_with_callback ssd_sync_with_callback 985 #define sdrunout ssdrunout 986 #define sd_mark_rqs_busy ssd_mark_rqs_busy 987 #define sd_mark_rqs_idle ssd_mark_rqs_idle 988 #define sd_reduce_throttle ssd_reduce_throttle 989 #define sd_restore_throttle ssd_restore_throttle 990 #define sd_print_incomplete_msg ssd_print_incomplete_msg 991 #define sd_init_cdb_limits ssd_init_cdb_limits 992 #define sd_pkt_status_good ssd_pkt_status_good 993 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 994 #define sd_pkt_status_busy ssd_pkt_status_busy 995 #define sd_pkt_status_reservation_conflict \ 996 ssd_pkt_status_reservation_conflict 997 #define sd_pkt_status_qfull ssd_pkt_status_qfull 998 #define sd_handle_request_sense ssd_handle_request_sense 999 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 1000 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 1001 #define sd_validate_sense_data ssd_validate_sense_data 1002 #define sd_decode_sense ssd_decode_sense 1003 #define sd_print_sense_msg ssd_print_sense_msg 1004 #define sd_sense_key_no_sense ssd_sense_key_no_sense 1005 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 1006 #define sd_sense_key_not_ready ssd_sense_key_not_ready 1007 #define sd_sense_key_medium_or_hardware_error \ 1008 ssd_sense_key_medium_or_hardware_error 1009 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 1010 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 1011 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1012 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1013 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1014 #define sd_sense_key_default ssd_sense_key_default 1015 #define sd_print_retry_msg ssd_print_retry_msg 1016 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1017 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1018 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1019 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1020 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1021 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1022 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1023 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1024 #define sd_pkt_reason_default ssd_pkt_reason_default 1025 #define sd_reset_target ssd_reset_target 1026 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1027 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1028 #define sd_taskq_create ssd_taskq_create 1029 #define sd_taskq_delete ssd_taskq_delete 1030 #define sd_target_change_task ssd_target_change_task 1031 #define sd_log_dev_status_event ssd_log_dev_status_event 1032 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event 1033 #define sd_log_eject_request_event ssd_log_eject_request_event 1034 #define sd_media_change_task ssd_media_change_task 1035 #define sd_handle_mchange ssd_handle_mchange 1036 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1037 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1038 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1039 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1040 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1041 sd_send_scsi_feature_GET_CONFIGURATION 1042 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1043 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1044 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1045 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1046 ssd_send_scsi_PERSISTENT_RESERVE_IN 1047 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1048 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1049 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1050 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1051 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1052 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1053 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1054 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1055 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1056 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION \ 1057 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 1058 #define sd_gesn_media_data_valid ssd_gesn_media_data_valid 1059 #define sd_alloc_rqs ssd_alloc_rqs 1060 #define sd_free_rqs ssd_free_rqs 1061 #define sd_dump_memory ssd_dump_memory 1062 #define sd_get_media_info ssd_get_media_info 1063 #define sd_get_media_info_ext ssd_get_media_info_ext 1064 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1065 #define sd_nvpair_str_decode ssd_nvpair_str_decode 1066 #define sd_strtok_r ssd_strtok_r 1067 #define sd_set_properties ssd_set_properties 1068 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1069 #define sd_setup_next_xfer ssd_setup_next_xfer 1070 #define sd_dkio_get_temp ssd_dkio_get_temp 1071 #define sd_check_mhd ssd_check_mhd 1072 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1073 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1074 #define sd_sname ssd_sname 1075 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1076 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1077 #define sd_take_ownership ssd_take_ownership 1078 #define sd_reserve_release ssd_reserve_release 1079 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1080 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1081 #define sd_persistent_reservation_in_read_keys \ 1082 ssd_persistent_reservation_in_read_keys 1083 #define sd_persistent_reservation_in_read_resv \ 1084 ssd_persistent_reservation_in_read_resv 1085 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1086 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1087 #define sd_mhdioc_release ssd_mhdioc_release 1088 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1089 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1090 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1091 #define sr_change_blkmode ssr_change_blkmode 1092 #define sr_change_speed ssr_change_speed 1093 #define sr_atapi_change_speed ssr_atapi_change_speed 1094 #define sr_pause_resume ssr_pause_resume 1095 #define sr_play_msf ssr_play_msf 1096 #define sr_play_trkind ssr_play_trkind 1097 #define sr_read_all_subcodes ssr_read_all_subcodes 1098 #define sr_read_subchannel ssr_read_subchannel 1099 #define sr_read_tocentry ssr_read_tocentry 1100 #define sr_read_tochdr ssr_read_tochdr 1101 #define sr_read_cdda ssr_read_cdda 1102 #define sr_read_cdxa ssr_read_cdxa 1103 #define sr_read_mode1 ssr_read_mode1 1104 #define sr_read_mode2 ssr_read_mode2 1105 #define sr_read_cd_mode2 ssr_read_cd_mode2 1106 #define sr_sector_mode ssr_sector_mode 1107 #define sr_eject ssr_eject 1108 #define sr_ejected ssr_ejected 1109 #define sr_check_wp ssr_check_wp 1110 #define sd_watch_request_submit ssd_watch_request_submit 1111 #define sd_check_media ssd_check_media 1112 #define sd_media_watch_cb ssd_media_watch_cb 1113 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1114 #define sr_volume_ctrl ssr_volume_ctrl 1115 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1116 #define sd_log_page_supported ssd_log_page_supported 1117 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1118 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1119 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1120 #define sd_range_lock ssd_range_lock 1121 #define sd_get_range ssd_get_range 1122 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1123 #define sd_range_unlock ssd_range_unlock 1124 #define sd_read_modify_write_task ssd_read_modify_write_task 1125 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1126 1127 #define sd_iostart_chain ssd_iostart_chain 1128 #define sd_iodone_chain ssd_iodone_chain 1129 #define sd_initpkt_map ssd_initpkt_map 1130 #define sd_destroypkt_map ssd_destroypkt_map 1131 #define sd_chain_type_map ssd_chain_type_map 1132 #define sd_chain_index_map ssd_chain_index_map 1133 1134 #define sd_failfast_flushctl ssd_failfast_flushctl 1135 #define sd_failfast_flushq ssd_failfast_flushq 1136 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1137 1138 #define sd_is_lsi ssd_is_lsi 1139 #define sd_tg_rdwr ssd_tg_rdwr 1140 #define sd_tg_getinfo ssd_tg_getinfo 1141 #define sd_rmw_msg_print_handler ssd_rmw_msg_print_handler 1142 1143 #endif /* #if (defined(__fibre)) */ 1144 1145 1146 int _init(void); 1147 int _fini(void); 1148 int _info(struct modinfo *modinfop); 1149 1150 /*PRINTFLIKE3*/ 1151 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1152 /*PRINTFLIKE3*/ 1153 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1154 /*PRINTFLIKE3*/ 1155 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1156 1157 static int sdprobe(dev_info_t *devi); 1158 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1159 void **result); 1160 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1161 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1162 1163 /* 1164 * Smart probe for parallel scsi 1165 */ 1166 static void sd_scsi_probe_cache_init(void); 1167 static void sd_scsi_probe_cache_fini(void); 1168 static void sd_scsi_clear_probe_cache(void); 1169 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1170 1171 /* 1172 * Attached luns on target for parallel scsi 1173 */ 1174 static void sd_scsi_target_lun_init(void); 1175 static void sd_scsi_target_lun_fini(void); 1176 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1177 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1178 1179 static int sd_spin_up_unit(sd_ssc_t *ssc); 1180 1181 /* 1182 * Using sd_ssc_init to establish sd_ssc_t struct 1183 * Using sd_ssc_send to send uscsi internal command 1184 * Using sd_ssc_fini to free sd_ssc_t struct 1185 */ 1186 static sd_ssc_t *sd_ssc_init(struct sd_lun *un); 1187 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, 1188 int flag, enum uio_seg dataspace, int path_flag); 1189 static void sd_ssc_fini(sd_ssc_t *ssc); 1190 1191 /* 1192 * Using sd_ssc_assessment to set correct type-of-assessment 1193 * Using sd_ssc_post to post ereport & system log 1194 * sd_ssc_post will call sd_ssc_print to print system log 1195 * sd_ssc_post will call sd_ssd_ereport_post to post ereport 1196 */ 1197 static void sd_ssc_assessment(sd_ssc_t *ssc, 1198 enum sd_type_assessment tp_assess); 1199 1200 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess); 1201 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity); 1202 static void sd_ssc_ereport_post(sd_ssc_t *ssc, 1203 enum sd_driver_assessment drv_assess); 1204 1205 /* 1206 * Using sd_ssc_set_info to mark an un-decodable-data error. 1207 * Using sd_ssc_extract_info to transfer information from internal 1208 * data structures to sd_ssc_t. 1209 */ 1210 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, 1211 const char *fmt, ...); 1212 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, 1213 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp); 1214 1215 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1216 enum uio_seg dataspace, int path_flag); 1217 1218 #ifdef _LP64 1219 static void sd_enable_descr_sense(sd_ssc_t *ssc); 1220 static void sd_reenable_dsense_task(void *arg); 1221 #endif /* _LP64 */ 1222 1223 static void sd_set_mmc_caps(sd_ssc_t *ssc); 1224 1225 static void sd_read_unit_properties(struct sd_lun *un); 1226 static int sd_process_sdconf_file(struct sd_lun *un); 1227 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str); 1228 static char *sd_strtok_r(char *string, const char *sepset, char **lasts); 1229 static void sd_set_properties(struct sd_lun *un, char *name, char *value); 1230 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1231 int *data_list, sd_tunables *values); 1232 static void sd_process_sdconf_table(struct sd_lun *un); 1233 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1234 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1235 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1236 int list_len, char *dataname_ptr); 1237 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1238 sd_tunables *prop_list); 1239 1240 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, 1241 int reservation_flag); 1242 static int sd_get_devid(sd_ssc_t *ssc); 1243 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc); 1244 static int sd_write_deviceid(sd_ssc_t *ssc); 1245 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1246 static int sd_check_vpd_page_support(sd_ssc_t *ssc); 1247 1248 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi); 1249 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1250 1251 static int sd_ddi_suspend(dev_info_t *devi); 1252 static int sd_ddi_resume(dev_info_t *devi); 1253 static int sd_pm_state_change(struct sd_lun *un, int level, int flag); 1254 static int sdpower(dev_info_t *devi, int component, int level); 1255 1256 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1257 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1258 static int sd_unit_attach(dev_info_t *devi); 1259 static int sd_unit_detach(dev_info_t *devi); 1260 1261 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1262 static void sd_create_errstats(struct sd_lun *un, int instance); 1263 static void sd_set_errstats(struct sd_lun *un); 1264 static void sd_set_pstats(struct sd_lun *un); 1265 1266 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1267 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1268 static int sd_send_polled_RQS(struct sd_lun *un); 1269 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1270 1271 #if (defined(__fibre)) 1272 /* 1273 * Event callbacks (photon) 1274 */ 1275 static void sd_init_event_callbacks(struct sd_lun *un); 1276 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1277 #endif 1278 1279 /* 1280 * Defines for sd_cache_control 1281 */ 1282 1283 #define SD_CACHE_ENABLE 1 1284 #define SD_CACHE_DISABLE 0 1285 #define SD_CACHE_NOCHANGE -1 1286 1287 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag); 1288 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled); 1289 static void sd_get_nv_sup(sd_ssc_t *ssc); 1290 static dev_t sd_make_device(dev_info_t *devi); 1291 static void sd_check_solid_state(sd_ssc_t *ssc); 1292 static void sd_check_emulation_mode(sd_ssc_t *ssc); 1293 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1294 uint64_t capacity); 1295 1296 /* 1297 * Driver entry point functions. 1298 */ 1299 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1300 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1301 static int sd_ready_and_valid(sd_ssc_t *ssc, int part); 1302 1303 static void sdmin(struct buf *bp); 1304 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1305 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1306 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1307 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1308 1309 static int sdstrategy(struct buf *bp); 1310 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1311 1312 /* 1313 * Function prototypes for layering functions in the iostart chain. 1314 */ 1315 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1316 struct buf *bp); 1317 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1318 struct buf *bp); 1319 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1320 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1321 struct buf *bp); 1322 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1323 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1324 1325 /* 1326 * Function prototypes for layering functions in the iodone chain. 1327 */ 1328 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1329 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1330 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1331 struct buf *bp); 1332 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1333 struct buf *bp); 1334 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1335 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1336 struct buf *bp); 1337 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1338 1339 /* 1340 * Prototypes for functions to support buf(9S) based IO. 1341 */ 1342 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1343 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1344 static void sd_destroypkt_for_buf(struct buf *); 1345 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1346 struct buf *bp, int flags, 1347 int (*callback)(caddr_t), caddr_t callback_arg, 1348 diskaddr_t lba, uint32_t blockcount); 1349 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1350 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1351 1352 /* 1353 * Prototypes for functions to support USCSI IO. 1354 */ 1355 static int sd_uscsi_strategy(struct buf *bp); 1356 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1357 static void sd_destroypkt_for_uscsi(struct buf *); 1358 1359 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1360 uchar_t chain_type, void *pktinfop); 1361 1362 static int sd_pm_entry(struct sd_lun *un); 1363 static void sd_pm_exit(struct sd_lun *un); 1364 1365 static void sd_pm_idletimeout_handler(void *arg); 1366 1367 /* 1368 * sd_core internal functions (used at the sd_core_io layer). 1369 */ 1370 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1371 static void sdintr(struct scsi_pkt *pktp); 1372 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1373 1374 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1375 enum uio_seg dataspace, int path_flag); 1376 1377 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1378 daddr_t blkno, int (*func)(struct buf *)); 1379 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1380 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1381 static void sd_bioclone_free(struct buf *bp); 1382 static void sd_shadow_buf_free(struct buf *bp); 1383 1384 static void sd_print_transport_rejected_message(struct sd_lun *un, 1385 struct sd_xbuf *xp, int code); 1386 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1387 void *arg, int code); 1388 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1389 void *arg, int code); 1390 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1391 void *arg, int code); 1392 1393 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1394 int retry_check_flag, 1395 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1396 int c), 1397 void *user_arg, int failure_code, clock_t retry_delay, 1398 void (*statp)(kstat_io_t *)); 1399 1400 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1401 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1402 1403 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1404 struct scsi_pkt *pktp); 1405 static void sd_start_retry_command(void *arg); 1406 static void sd_start_direct_priority_command(void *arg); 1407 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1408 int errcode); 1409 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1410 struct buf *bp, int errcode); 1411 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1412 static void sd_sync_with_callback(struct sd_lun *un); 1413 static int sdrunout(caddr_t arg); 1414 1415 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1416 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1417 1418 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1419 static void sd_restore_throttle(void *arg); 1420 1421 static void sd_init_cdb_limits(struct sd_lun *un); 1422 1423 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1424 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1425 1426 /* 1427 * Error handling functions 1428 */ 1429 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1430 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1431 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1432 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1433 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1434 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1435 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1436 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1437 1438 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1439 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1440 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1441 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1442 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1443 struct sd_xbuf *xp, size_t actual_len); 1444 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1445 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1446 1447 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1448 void *arg, int code); 1449 1450 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1451 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1452 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1453 uint8_t *sense_datap, 1454 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1455 static void sd_sense_key_not_ready(struct sd_lun *un, 1456 uint8_t *sense_datap, 1457 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1458 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1459 uint8_t *sense_datap, 1460 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1461 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1462 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1463 static void sd_sense_key_unit_attention(struct sd_lun *un, 1464 uint8_t *sense_datap, 1465 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1466 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1467 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1468 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1469 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1470 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1471 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1472 static void sd_sense_key_default(struct sd_lun *un, 1473 uint8_t *sense_datap, 1474 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1475 1476 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1477 void *arg, int flag); 1478 1479 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1480 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1481 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1482 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1483 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1484 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1485 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1486 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1487 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1488 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1489 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1490 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1491 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1492 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1493 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1494 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1495 1496 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1497 1498 static void sd_start_stop_unit_callback(void *arg); 1499 static void sd_start_stop_unit_task(void *arg); 1500 1501 static void sd_taskq_create(void); 1502 static void sd_taskq_delete(void); 1503 static void sd_target_change_task(void *arg); 1504 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag); 1505 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag); 1506 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag); 1507 static void sd_media_change_task(void *arg); 1508 1509 static int sd_handle_mchange(struct sd_lun *un); 1510 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag); 1511 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, 1512 uint32_t *lbap, int path_flag); 1513 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 1514 uint32_t *lbap, uint32_t *psp, int path_flag); 1515 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, 1516 int flag, int path_flag); 1517 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, 1518 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1519 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag); 1520 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, 1521 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1522 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, 1523 uchar_t usr_cmd, uchar_t *usr_bufp); 1524 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1525 struct dk_callback *dkc); 1526 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1527 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, 1528 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1529 uchar_t *bufaddr, uint_t buflen, int path_flag); 1530 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 1531 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1532 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1533 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, 1534 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1535 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, 1536 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1537 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 1538 size_t buflen, daddr_t start_block, int path_flag); 1539 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \ 1540 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \ 1541 path_flag) 1542 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\ 1543 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\ 1544 path_flag) 1545 1546 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, 1547 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1548 uint16_t param_ptr, int path_flag); 1549 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, 1550 uchar_t *bufaddr, size_t buflen, uchar_t class_req); 1551 static boolean_t sd_gesn_media_data_valid(uchar_t *data); 1552 1553 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1554 static void sd_free_rqs(struct sd_lun *un); 1555 1556 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1557 uchar_t *data, int len, int fmt); 1558 static void sd_panic_for_res_conflict(struct sd_lun *un); 1559 1560 /* 1561 * Disk Ioctl Function Prototypes 1562 */ 1563 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1564 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag); 1565 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1566 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1567 1568 /* 1569 * Multi-host Ioctl Prototypes 1570 */ 1571 static int sd_check_mhd(dev_t dev, int interval); 1572 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1573 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1574 static char *sd_sname(uchar_t status); 1575 static void sd_mhd_resvd_recover(void *arg); 1576 static void sd_resv_reclaim_thread(); 1577 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1578 static int sd_reserve_release(dev_t dev, int cmd); 1579 static void sd_rmv_resv_reclaim_req(dev_t dev); 1580 static void sd_mhd_reset_notify_cb(caddr_t arg); 1581 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1582 mhioc_inkeys_t *usrp, int flag); 1583 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1584 mhioc_inresvs_t *usrp, int flag); 1585 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1586 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1587 static int sd_mhdioc_release(dev_t dev); 1588 static int sd_mhdioc_register_devid(dev_t dev); 1589 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1590 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1591 1592 /* 1593 * SCSI removable prototypes 1594 */ 1595 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1596 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1597 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1598 static int sr_pause_resume(dev_t dev, int mode); 1599 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1600 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1601 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1602 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1603 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1604 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1605 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1606 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1607 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1609 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1610 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1611 static int sr_eject(dev_t dev); 1612 static void sr_ejected(register struct sd_lun *un); 1613 static int sr_check_wp(dev_t dev); 1614 static opaque_t sd_watch_request_submit(struct sd_lun *un); 1615 static int sd_check_media(dev_t dev, enum dkio_state state); 1616 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1617 static void sd_delayed_cv_broadcast(void *arg); 1618 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1619 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1620 1621 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page); 1622 1623 /* 1624 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1625 */ 1626 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag); 1627 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1628 static void sd_wm_cache_destructor(void *wm, void *un); 1629 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1630 daddr_t endb, ushort_t typ); 1631 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1632 daddr_t endb); 1633 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1634 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1635 static void sd_read_modify_write_task(void * arg); 1636 static int 1637 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1638 struct buf **bpp); 1639 1640 1641 /* 1642 * Function prototypes for failfast support. 1643 */ 1644 static void sd_failfast_flushq(struct sd_lun *un); 1645 static int sd_failfast_flushq_callback(struct buf *bp); 1646 1647 /* 1648 * Function prototypes to check for lsi devices 1649 */ 1650 static void sd_is_lsi(struct sd_lun *un); 1651 1652 /* 1653 * Function prototypes for partial DMA support 1654 */ 1655 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1656 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1657 1658 1659 /* Function prototypes for cmlb */ 1660 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1661 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1662 1663 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1664 1665 /* 1666 * For printing RMW warning message timely 1667 */ 1668 static void sd_rmw_msg_print_handler(void *arg); 1669 1670 /* 1671 * Constants for failfast support: 1672 * 1673 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1674 * failfast processing being performed. 1675 * 1676 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1677 * failfast processing on all bufs with B_FAILFAST set. 1678 */ 1679 1680 #define SD_FAILFAST_INACTIVE 0 1681 #define SD_FAILFAST_ACTIVE 1 1682 1683 /* 1684 * Bitmask to control behavior of buf(9S) flushes when a transition to 1685 * the failfast state occurs. Optional bits include: 1686 * 1687 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1688 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1689 * be flushed. 1690 * 1691 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1692 * driver, in addition to the regular wait queue. This includes the xbuf 1693 * queues. When clear, only the driver's wait queue will be flushed. 1694 */ 1695 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1696 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1697 1698 /* 1699 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1700 * to flush all queues within the driver. 1701 */ 1702 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1703 1704 1705 /* 1706 * SD Testing Fault Injection 1707 */ 1708 #ifdef SD_FAULT_INJECTION 1709 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1710 static void sd_faultinjection(struct scsi_pkt *pktp); 1711 static void sd_injection_log(char *buf, struct sd_lun *un); 1712 #endif 1713 1714 /* 1715 * Device driver ops vector 1716 */ 1717 static struct cb_ops sd_cb_ops = { 1718 sdopen, /* open */ 1719 sdclose, /* close */ 1720 sdstrategy, /* strategy */ 1721 nodev, /* print */ 1722 sddump, /* dump */ 1723 sdread, /* read */ 1724 sdwrite, /* write */ 1725 sdioctl, /* ioctl */ 1726 nodev, /* devmap */ 1727 nodev, /* mmap */ 1728 nodev, /* segmap */ 1729 nochpoll, /* poll */ 1730 sd_prop_op, /* cb_prop_op */ 1731 0, /* streamtab */ 1732 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1733 CB_REV, /* cb_rev */ 1734 sdaread, /* async I/O read entry point */ 1735 sdawrite /* async I/O write entry point */ 1736 }; 1737 1738 struct dev_ops sd_ops = { 1739 DEVO_REV, /* devo_rev, */ 1740 0, /* refcnt */ 1741 sdinfo, /* info */ 1742 nulldev, /* identify */ 1743 sdprobe, /* probe */ 1744 sdattach, /* attach */ 1745 sddetach, /* detach */ 1746 nodev, /* reset */ 1747 &sd_cb_ops, /* driver operations */ 1748 NULL, /* bus operations */ 1749 sdpower, /* power */ 1750 ddi_quiesce_not_needed, /* quiesce */ 1751 }; 1752 1753 /* 1754 * This is the loadable module wrapper. 1755 */ 1756 #include <sys/modctl.h> 1757 1758 #ifndef XPV_HVM_DRIVER 1759 static struct modldrv modldrv = { 1760 &mod_driverops, /* Type of module. This one is a driver */ 1761 SD_MODULE_NAME, /* Module name. */ 1762 &sd_ops /* driver ops */ 1763 }; 1764 1765 static struct modlinkage modlinkage = { 1766 MODREV_1, &modldrv, NULL 1767 }; 1768 1769 #else /* XPV_HVM_DRIVER */ 1770 static struct modlmisc modlmisc = { 1771 &mod_miscops, /* Type of module. This one is a misc */ 1772 "HVM " SD_MODULE_NAME, /* Module name. */ 1773 }; 1774 1775 static struct modlinkage modlinkage = { 1776 MODREV_1, &modlmisc, NULL 1777 }; 1778 1779 #endif /* XPV_HVM_DRIVER */ 1780 1781 static cmlb_tg_ops_t sd_tgops = { 1782 TG_DK_OPS_VERSION_1, 1783 sd_tg_rdwr, 1784 sd_tg_getinfo 1785 }; 1786 1787 static struct scsi_asq_key_strings sd_additional_codes[] = { 1788 0x81, 0, "Logical Unit is Reserved", 1789 0x85, 0, "Audio Address Not Valid", 1790 0xb6, 0, "Media Load Mechanism Failed", 1791 0xB9, 0, "Audio Play Operation Aborted", 1792 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1793 0x53, 2, "Medium removal prevented", 1794 0x6f, 0, "Authentication failed during key exchange", 1795 0x6f, 1, "Key not present", 1796 0x6f, 2, "Key not established", 1797 0x6f, 3, "Read without proper authentication", 1798 0x6f, 4, "Mismatched region to this logical unit", 1799 0x6f, 5, "Region reset count error", 1800 0xffff, 0x0, NULL 1801 }; 1802 1803 1804 /* 1805 * Struct for passing printing information for sense data messages 1806 */ 1807 struct sd_sense_info { 1808 int ssi_severity; 1809 int ssi_pfa_flag; 1810 }; 1811 1812 /* 1813 * Table of function pointers for iostart-side routines. Separate "chains" 1814 * of layered function calls are formed by placing the function pointers 1815 * sequentially in the desired order. Functions are called according to an 1816 * incrementing table index ordering. The last function in each chain must 1817 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1818 * in the sd_iodone_chain[] array. 1819 * 1820 * Note: It may seem more natural to organize both the iostart and iodone 1821 * functions together, into an array of structures (or some similar 1822 * organization) with a common index, rather than two separate arrays which 1823 * must be maintained in synchronization. The purpose of this division is 1824 * to achieve improved performance: individual arrays allows for more 1825 * effective cache line utilization on certain platforms. 1826 */ 1827 1828 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1829 1830 1831 static sd_chain_t sd_iostart_chain[] = { 1832 1833 /* Chain for buf IO for disk drive targets (PM enabled) */ 1834 sd_mapblockaddr_iostart, /* Index: 0 */ 1835 sd_pm_iostart, /* Index: 1 */ 1836 sd_core_iostart, /* Index: 2 */ 1837 1838 /* Chain for buf IO for disk drive targets (PM disabled) */ 1839 sd_mapblockaddr_iostart, /* Index: 3 */ 1840 sd_core_iostart, /* Index: 4 */ 1841 1842 /* 1843 * Chain for buf IO for removable-media or large sector size 1844 * disk drive targets with RMW needed (PM enabled) 1845 */ 1846 sd_mapblockaddr_iostart, /* Index: 5 */ 1847 sd_mapblocksize_iostart, /* Index: 6 */ 1848 sd_pm_iostart, /* Index: 7 */ 1849 sd_core_iostart, /* Index: 8 */ 1850 1851 /* 1852 * Chain for buf IO for removable-media or large sector size 1853 * disk drive targets with RMW needed (PM disabled) 1854 */ 1855 sd_mapblockaddr_iostart, /* Index: 9 */ 1856 sd_mapblocksize_iostart, /* Index: 10 */ 1857 sd_core_iostart, /* Index: 11 */ 1858 1859 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1860 sd_mapblockaddr_iostart, /* Index: 12 */ 1861 sd_checksum_iostart, /* Index: 13 */ 1862 sd_pm_iostart, /* Index: 14 */ 1863 sd_core_iostart, /* Index: 15 */ 1864 1865 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1866 sd_mapblockaddr_iostart, /* Index: 16 */ 1867 sd_checksum_iostart, /* Index: 17 */ 1868 sd_core_iostart, /* Index: 18 */ 1869 1870 /* Chain for USCSI commands (all targets) */ 1871 sd_pm_iostart, /* Index: 19 */ 1872 sd_core_iostart, /* Index: 20 */ 1873 1874 /* Chain for checksumming USCSI commands (all targets) */ 1875 sd_checksum_uscsi_iostart, /* Index: 21 */ 1876 sd_pm_iostart, /* Index: 22 */ 1877 sd_core_iostart, /* Index: 23 */ 1878 1879 /* Chain for "direct" USCSI commands (all targets) */ 1880 sd_core_iostart, /* Index: 24 */ 1881 1882 /* Chain for "direct priority" USCSI commands (all targets) */ 1883 sd_core_iostart, /* Index: 25 */ 1884 1885 /* 1886 * Chain for buf IO for large sector size disk drive targets 1887 * with RMW needed with checksumming (PM enabled) 1888 */ 1889 sd_mapblockaddr_iostart, /* Index: 26 */ 1890 sd_mapblocksize_iostart, /* Index: 27 */ 1891 sd_checksum_iostart, /* Index: 28 */ 1892 sd_pm_iostart, /* Index: 29 */ 1893 sd_core_iostart, /* Index: 30 */ 1894 1895 /* 1896 * Chain for buf IO for large sector size disk drive targets 1897 * with RMW needed with checksumming (PM disabled) 1898 */ 1899 sd_mapblockaddr_iostart, /* Index: 31 */ 1900 sd_mapblocksize_iostart, /* Index: 32 */ 1901 sd_checksum_iostart, /* Index: 33 */ 1902 sd_core_iostart, /* Index: 34 */ 1903 1904 }; 1905 1906 /* 1907 * Macros to locate the first function of each iostart chain in the 1908 * sd_iostart_chain[] array. These are located by the index in the array. 1909 */ 1910 #define SD_CHAIN_DISK_IOSTART 0 1911 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1912 #define SD_CHAIN_MSS_DISK_IOSTART 5 1913 #define SD_CHAIN_RMMEDIA_IOSTART 5 1914 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9 1915 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1916 #define SD_CHAIN_CHKSUM_IOSTART 12 1917 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1918 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1919 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1920 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1921 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1922 #define SD_CHAIN_MSS_CHKSUM_IOSTART 26 1923 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31 1924 1925 1926 /* 1927 * Table of function pointers for the iodone-side routines for the driver- 1928 * internal layering mechanism. The calling sequence for iodone routines 1929 * uses a decrementing table index, so the last routine called in a chain 1930 * must be at the lowest array index location for that chain. The last 1931 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1932 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1933 * of the functions in an iodone side chain must correspond to the ordering 1934 * of the iostart routines for that chain. Note that there is no iodone 1935 * side routine that corresponds to sd_core_iostart(), so there is no 1936 * entry in the table for this. 1937 */ 1938 1939 static sd_chain_t sd_iodone_chain[] = { 1940 1941 /* Chain for buf IO for disk drive targets (PM enabled) */ 1942 sd_buf_iodone, /* Index: 0 */ 1943 sd_mapblockaddr_iodone, /* Index: 1 */ 1944 sd_pm_iodone, /* Index: 2 */ 1945 1946 /* Chain for buf IO for disk drive targets (PM disabled) */ 1947 sd_buf_iodone, /* Index: 3 */ 1948 sd_mapblockaddr_iodone, /* Index: 4 */ 1949 1950 /* 1951 * Chain for buf IO for removable-media or large sector size 1952 * disk drive targets with RMW needed (PM enabled) 1953 */ 1954 sd_buf_iodone, /* Index: 5 */ 1955 sd_mapblockaddr_iodone, /* Index: 6 */ 1956 sd_mapblocksize_iodone, /* Index: 7 */ 1957 sd_pm_iodone, /* Index: 8 */ 1958 1959 /* 1960 * Chain for buf IO for removable-media or large sector size 1961 * disk drive targets with RMW needed (PM disabled) 1962 */ 1963 sd_buf_iodone, /* Index: 9 */ 1964 sd_mapblockaddr_iodone, /* Index: 10 */ 1965 sd_mapblocksize_iodone, /* Index: 11 */ 1966 1967 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1968 sd_buf_iodone, /* Index: 12 */ 1969 sd_mapblockaddr_iodone, /* Index: 13 */ 1970 sd_checksum_iodone, /* Index: 14 */ 1971 sd_pm_iodone, /* Index: 15 */ 1972 1973 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1974 sd_buf_iodone, /* Index: 16 */ 1975 sd_mapblockaddr_iodone, /* Index: 17 */ 1976 sd_checksum_iodone, /* Index: 18 */ 1977 1978 /* Chain for USCSI commands (non-checksum targets) */ 1979 sd_uscsi_iodone, /* Index: 19 */ 1980 sd_pm_iodone, /* Index: 20 */ 1981 1982 /* Chain for USCSI commands (checksum targets) */ 1983 sd_uscsi_iodone, /* Index: 21 */ 1984 sd_checksum_uscsi_iodone, /* Index: 22 */ 1985 sd_pm_iodone, /* Index: 22 */ 1986 1987 /* Chain for "direct" USCSI commands (all targets) */ 1988 sd_uscsi_iodone, /* Index: 24 */ 1989 1990 /* Chain for "direct priority" USCSI commands (all targets) */ 1991 sd_uscsi_iodone, /* Index: 25 */ 1992 1993 /* 1994 * Chain for buf IO for large sector size disk drive targets 1995 * with checksumming (PM enabled) 1996 */ 1997 sd_buf_iodone, /* Index: 26 */ 1998 sd_mapblockaddr_iodone, /* Index: 27 */ 1999 sd_mapblocksize_iodone, /* Index: 28 */ 2000 sd_checksum_iodone, /* Index: 29 */ 2001 sd_pm_iodone, /* Index: 30 */ 2002 2003 /* 2004 * Chain for buf IO for large sector size disk drive targets 2005 * with checksumming (PM disabled) 2006 */ 2007 sd_buf_iodone, /* Index: 31 */ 2008 sd_mapblockaddr_iodone, /* Index: 32 */ 2009 sd_mapblocksize_iodone, /* Index: 33 */ 2010 sd_checksum_iodone, /* Index: 34 */ 2011 }; 2012 2013 2014 /* 2015 * Macros to locate the "first" function in the sd_iodone_chain[] array for 2016 * each iodone-side chain. These are located by the array index, but as the 2017 * iodone side functions are called in a decrementing-index order, the 2018 * highest index number in each chain must be specified (as these correspond 2019 * to the first function in the iodone chain that will be called by the core 2020 * at IO completion time). 2021 */ 2022 2023 #define SD_CHAIN_DISK_IODONE 2 2024 #define SD_CHAIN_DISK_IODONE_NO_PM 4 2025 #define SD_CHAIN_RMMEDIA_IODONE 8 2026 #define SD_CHAIN_MSS_DISK_IODONE 8 2027 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 2028 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11 2029 #define SD_CHAIN_CHKSUM_IODONE 15 2030 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 2031 #define SD_CHAIN_USCSI_CMD_IODONE 20 2032 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 2033 #define SD_CHAIN_DIRECT_CMD_IODONE 24 2034 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 2035 #define SD_CHAIN_MSS_CHKSUM_IODONE 30 2036 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34 2037 2038 2039 2040 /* 2041 * Array to map a layering chain index to the appropriate initpkt routine. 2042 * The redundant entries are present so that the index used for accessing 2043 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2044 * with this table as well. 2045 */ 2046 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 2047 2048 static sd_initpkt_t sd_initpkt_map[] = { 2049 2050 /* Chain for buf IO for disk drive targets (PM enabled) */ 2051 sd_initpkt_for_buf, /* Index: 0 */ 2052 sd_initpkt_for_buf, /* Index: 1 */ 2053 sd_initpkt_for_buf, /* Index: 2 */ 2054 2055 /* Chain for buf IO for disk drive targets (PM disabled) */ 2056 sd_initpkt_for_buf, /* Index: 3 */ 2057 sd_initpkt_for_buf, /* Index: 4 */ 2058 2059 /* 2060 * Chain for buf IO for removable-media or large sector size 2061 * disk drive targets (PM enabled) 2062 */ 2063 sd_initpkt_for_buf, /* Index: 5 */ 2064 sd_initpkt_for_buf, /* Index: 6 */ 2065 sd_initpkt_for_buf, /* Index: 7 */ 2066 sd_initpkt_for_buf, /* Index: 8 */ 2067 2068 /* 2069 * Chain for buf IO for removable-media or large sector size 2070 * disk drive targets (PM disabled) 2071 */ 2072 sd_initpkt_for_buf, /* Index: 9 */ 2073 sd_initpkt_for_buf, /* Index: 10 */ 2074 sd_initpkt_for_buf, /* Index: 11 */ 2075 2076 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2077 sd_initpkt_for_buf, /* Index: 12 */ 2078 sd_initpkt_for_buf, /* Index: 13 */ 2079 sd_initpkt_for_buf, /* Index: 14 */ 2080 sd_initpkt_for_buf, /* Index: 15 */ 2081 2082 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2083 sd_initpkt_for_buf, /* Index: 16 */ 2084 sd_initpkt_for_buf, /* Index: 17 */ 2085 sd_initpkt_for_buf, /* Index: 18 */ 2086 2087 /* Chain for USCSI commands (non-checksum targets) */ 2088 sd_initpkt_for_uscsi, /* Index: 19 */ 2089 sd_initpkt_for_uscsi, /* Index: 20 */ 2090 2091 /* Chain for USCSI commands (checksum targets) */ 2092 sd_initpkt_for_uscsi, /* Index: 21 */ 2093 sd_initpkt_for_uscsi, /* Index: 22 */ 2094 sd_initpkt_for_uscsi, /* Index: 22 */ 2095 2096 /* Chain for "direct" USCSI commands (all targets) */ 2097 sd_initpkt_for_uscsi, /* Index: 24 */ 2098 2099 /* Chain for "direct priority" USCSI commands (all targets) */ 2100 sd_initpkt_for_uscsi, /* Index: 25 */ 2101 2102 /* 2103 * Chain for buf IO for large sector size disk drive targets 2104 * with checksumming (PM enabled) 2105 */ 2106 sd_initpkt_for_buf, /* Index: 26 */ 2107 sd_initpkt_for_buf, /* Index: 27 */ 2108 sd_initpkt_for_buf, /* Index: 28 */ 2109 sd_initpkt_for_buf, /* Index: 29 */ 2110 sd_initpkt_for_buf, /* Index: 30 */ 2111 2112 /* 2113 * Chain for buf IO for large sector size disk drive targets 2114 * with checksumming (PM disabled) 2115 */ 2116 sd_initpkt_for_buf, /* Index: 31 */ 2117 sd_initpkt_for_buf, /* Index: 32 */ 2118 sd_initpkt_for_buf, /* Index: 33 */ 2119 sd_initpkt_for_buf, /* Index: 34 */ 2120 }; 2121 2122 2123 /* 2124 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2125 * The redundant entries are present so that the index used for accessing 2126 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2127 * with this table as well. 2128 */ 2129 typedef void (*sd_destroypkt_t)(struct buf *); 2130 2131 static sd_destroypkt_t sd_destroypkt_map[] = { 2132 2133 /* Chain for buf IO for disk drive targets (PM enabled) */ 2134 sd_destroypkt_for_buf, /* Index: 0 */ 2135 sd_destroypkt_for_buf, /* Index: 1 */ 2136 sd_destroypkt_for_buf, /* Index: 2 */ 2137 2138 /* Chain for buf IO for disk drive targets (PM disabled) */ 2139 sd_destroypkt_for_buf, /* Index: 3 */ 2140 sd_destroypkt_for_buf, /* Index: 4 */ 2141 2142 /* 2143 * Chain for buf IO for removable-media or large sector size 2144 * disk drive targets (PM enabled) 2145 */ 2146 sd_destroypkt_for_buf, /* Index: 5 */ 2147 sd_destroypkt_for_buf, /* Index: 6 */ 2148 sd_destroypkt_for_buf, /* Index: 7 */ 2149 sd_destroypkt_for_buf, /* Index: 8 */ 2150 2151 /* 2152 * Chain for buf IO for removable-media or large sector size 2153 * disk drive targets (PM disabled) 2154 */ 2155 sd_destroypkt_for_buf, /* Index: 9 */ 2156 sd_destroypkt_for_buf, /* Index: 10 */ 2157 sd_destroypkt_for_buf, /* Index: 11 */ 2158 2159 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2160 sd_destroypkt_for_buf, /* Index: 12 */ 2161 sd_destroypkt_for_buf, /* Index: 13 */ 2162 sd_destroypkt_for_buf, /* Index: 14 */ 2163 sd_destroypkt_for_buf, /* Index: 15 */ 2164 2165 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2166 sd_destroypkt_for_buf, /* Index: 16 */ 2167 sd_destroypkt_for_buf, /* Index: 17 */ 2168 sd_destroypkt_for_buf, /* Index: 18 */ 2169 2170 /* Chain for USCSI commands (non-checksum targets) */ 2171 sd_destroypkt_for_uscsi, /* Index: 19 */ 2172 sd_destroypkt_for_uscsi, /* Index: 20 */ 2173 2174 /* Chain for USCSI commands (checksum targets) */ 2175 sd_destroypkt_for_uscsi, /* Index: 21 */ 2176 sd_destroypkt_for_uscsi, /* Index: 22 */ 2177 sd_destroypkt_for_uscsi, /* Index: 22 */ 2178 2179 /* Chain for "direct" USCSI commands (all targets) */ 2180 sd_destroypkt_for_uscsi, /* Index: 24 */ 2181 2182 /* Chain for "direct priority" USCSI commands (all targets) */ 2183 sd_destroypkt_for_uscsi, /* Index: 25 */ 2184 2185 /* 2186 * Chain for buf IO for large sector size disk drive targets 2187 * with checksumming (PM disabled) 2188 */ 2189 sd_destroypkt_for_buf, /* Index: 26 */ 2190 sd_destroypkt_for_buf, /* Index: 27 */ 2191 sd_destroypkt_for_buf, /* Index: 28 */ 2192 sd_destroypkt_for_buf, /* Index: 29 */ 2193 sd_destroypkt_for_buf, /* Index: 30 */ 2194 2195 /* 2196 * Chain for buf IO for large sector size disk drive targets 2197 * with checksumming (PM enabled) 2198 */ 2199 sd_destroypkt_for_buf, /* Index: 31 */ 2200 sd_destroypkt_for_buf, /* Index: 32 */ 2201 sd_destroypkt_for_buf, /* Index: 33 */ 2202 sd_destroypkt_for_buf, /* Index: 34 */ 2203 }; 2204 2205 2206 2207 /* 2208 * Array to map a layering chain index to the appropriate chain "type". 2209 * The chain type indicates a specific property/usage of the chain. 2210 * The redundant entries are present so that the index used for accessing 2211 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2212 * with this table as well. 2213 */ 2214 2215 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2216 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2217 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2218 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2219 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2220 /* (for error recovery) */ 2221 2222 static int sd_chain_type_map[] = { 2223 2224 /* Chain for buf IO for disk drive targets (PM enabled) */ 2225 SD_CHAIN_BUFIO, /* Index: 0 */ 2226 SD_CHAIN_BUFIO, /* Index: 1 */ 2227 SD_CHAIN_BUFIO, /* Index: 2 */ 2228 2229 /* Chain for buf IO for disk drive targets (PM disabled) */ 2230 SD_CHAIN_BUFIO, /* Index: 3 */ 2231 SD_CHAIN_BUFIO, /* Index: 4 */ 2232 2233 /* 2234 * Chain for buf IO for removable-media or large sector size 2235 * disk drive targets (PM enabled) 2236 */ 2237 SD_CHAIN_BUFIO, /* Index: 5 */ 2238 SD_CHAIN_BUFIO, /* Index: 6 */ 2239 SD_CHAIN_BUFIO, /* Index: 7 */ 2240 SD_CHAIN_BUFIO, /* Index: 8 */ 2241 2242 /* 2243 * Chain for buf IO for removable-media or large sector size 2244 * disk drive targets (PM disabled) 2245 */ 2246 SD_CHAIN_BUFIO, /* Index: 9 */ 2247 SD_CHAIN_BUFIO, /* Index: 10 */ 2248 SD_CHAIN_BUFIO, /* Index: 11 */ 2249 2250 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2251 SD_CHAIN_BUFIO, /* Index: 12 */ 2252 SD_CHAIN_BUFIO, /* Index: 13 */ 2253 SD_CHAIN_BUFIO, /* Index: 14 */ 2254 SD_CHAIN_BUFIO, /* Index: 15 */ 2255 2256 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2257 SD_CHAIN_BUFIO, /* Index: 16 */ 2258 SD_CHAIN_BUFIO, /* Index: 17 */ 2259 SD_CHAIN_BUFIO, /* Index: 18 */ 2260 2261 /* Chain for USCSI commands (non-checksum targets) */ 2262 SD_CHAIN_USCSI, /* Index: 19 */ 2263 SD_CHAIN_USCSI, /* Index: 20 */ 2264 2265 /* Chain for USCSI commands (checksum targets) */ 2266 SD_CHAIN_USCSI, /* Index: 21 */ 2267 SD_CHAIN_USCSI, /* Index: 22 */ 2268 SD_CHAIN_USCSI, /* Index: 23 */ 2269 2270 /* Chain for "direct" USCSI commands (all targets) */ 2271 SD_CHAIN_DIRECT, /* Index: 24 */ 2272 2273 /* Chain for "direct priority" USCSI commands (all targets) */ 2274 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2275 2276 /* 2277 * Chain for buf IO for large sector size disk drive targets 2278 * with checksumming (PM enabled) 2279 */ 2280 SD_CHAIN_BUFIO, /* Index: 26 */ 2281 SD_CHAIN_BUFIO, /* Index: 27 */ 2282 SD_CHAIN_BUFIO, /* Index: 28 */ 2283 SD_CHAIN_BUFIO, /* Index: 29 */ 2284 SD_CHAIN_BUFIO, /* Index: 30 */ 2285 2286 /* 2287 * Chain for buf IO for large sector size disk drive targets 2288 * with checksumming (PM disabled) 2289 */ 2290 SD_CHAIN_BUFIO, /* Index: 31 */ 2291 SD_CHAIN_BUFIO, /* Index: 32 */ 2292 SD_CHAIN_BUFIO, /* Index: 33 */ 2293 SD_CHAIN_BUFIO, /* Index: 34 */ 2294 }; 2295 2296 2297 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2298 #define SD_IS_BUFIO(xp) \ 2299 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2300 2301 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2302 #define SD_IS_DIRECT_PRIORITY(xp) \ 2303 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2304 2305 2306 2307 /* 2308 * Struct, array, and macros to map a specific chain to the appropriate 2309 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2310 * 2311 * The sd_chain_index_map[] array is used at attach time to set the various 2312 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2313 * chain to be used with the instance. This allows different instances to use 2314 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2315 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2316 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2317 * dynamically & without the use of locking; and (2) a layer may update the 2318 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2319 * to allow for deferred processing of an IO within the same chain from a 2320 * different execution context. 2321 */ 2322 2323 struct sd_chain_index { 2324 int sci_iostart_index; 2325 int sci_iodone_index; 2326 }; 2327 2328 static struct sd_chain_index sd_chain_index_map[] = { 2329 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2330 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2331 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2332 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2333 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2334 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2335 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2336 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2337 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2338 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2339 { SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE }, 2340 { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM }, 2341 2342 }; 2343 2344 2345 /* 2346 * The following are indexes into the sd_chain_index_map[] array. 2347 */ 2348 2349 /* un->un_buf_chain_type must be set to one of these */ 2350 #define SD_CHAIN_INFO_DISK 0 2351 #define SD_CHAIN_INFO_DISK_NO_PM 1 2352 #define SD_CHAIN_INFO_RMMEDIA 2 2353 #define SD_CHAIN_INFO_MSS_DISK 2 2354 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2355 #define SD_CHAIN_INFO_MSS_DSK_NO_PM 3 2356 #define SD_CHAIN_INFO_CHKSUM 4 2357 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2358 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10 2359 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11 2360 2361 /* un->un_uscsi_chain_type must be set to one of these */ 2362 #define SD_CHAIN_INFO_USCSI_CMD 6 2363 /* USCSI with PM disabled is the same as DIRECT */ 2364 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2365 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2366 2367 /* un->un_direct_chain_type must be set to one of these */ 2368 #define SD_CHAIN_INFO_DIRECT_CMD 8 2369 2370 /* un->un_priority_chain_type must be set to one of these */ 2371 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2372 2373 /* size for devid inquiries */ 2374 #define MAX_INQUIRY_SIZE 0xF0 2375 2376 /* 2377 * Macros used by functions to pass a given buf(9S) struct along to the 2378 * next function in the layering chain for further processing. 2379 * 2380 * In the following macros, passing more than three arguments to the called 2381 * routines causes the optimizer for the SPARC compiler to stop doing tail 2382 * call elimination which results in significant performance degradation. 2383 */ 2384 #define SD_BEGIN_IOSTART(index, un, bp) \ 2385 ((*(sd_iostart_chain[index]))(index, un, bp)) 2386 2387 #define SD_BEGIN_IODONE(index, un, bp) \ 2388 ((*(sd_iodone_chain[index]))(index, un, bp)) 2389 2390 #define SD_NEXT_IOSTART(index, un, bp) \ 2391 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2392 2393 #define SD_NEXT_IODONE(index, un, bp) \ 2394 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2395 2396 /* 2397 * Function: _init 2398 * 2399 * Description: This is the driver _init(9E) entry point. 2400 * 2401 * Return Code: Returns the value from mod_install(9F) or 2402 * ddi_soft_state_init(9F) as appropriate. 2403 * 2404 * Context: Called when driver module loaded. 2405 */ 2406 2407 int 2408 _init(void) 2409 { 2410 int err; 2411 2412 /* establish driver name from module name */ 2413 sd_label = (char *)mod_modname(&modlinkage); 2414 2415 #ifndef XPV_HVM_DRIVER 2416 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2417 SD_MAXUNIT); 2418 if (err != 0) { 2419 return (err); 2420 } 2421 2422 #else /* XPV_HVM_DRIVER */ 2423 /* Remove the leading "hvm_" from the module name */ 2424 ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0); 2425 sd_label += strlen("hvm_"); 2426 2427 #endif /* XPV_HVM_DRIVER */ 2428 2429 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2430 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2431 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2432 2433 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2434 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2435 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2436 2437 /* 2438 * it's ok to init here even for fibre device 2439 */ 2440 sd_scsi_probe_cache_init(); 2441 2442 sd_scsi_target_lun_init(); 2443 2444 /* 2445 * Creating taskq before mod_install ensures that all callers (threads) 2446 * that enter the module after a successful mod_install encounter 2447 * a valid taskq. 2448 */ 2449 sd_taskq_create(); 2450 2451 err = mod_install(&modlinkage); 2452 if (err != 0) { 2453 /* delete taskq if install fails */ 2454 sd_taskq_delete(); 2455 2456 mutex_destroy(&sd_detach_mutex); 2457 mutex_destroy(&sd_log_mutex); 2458 mutex_destroy(&sd_label_mutex); 2459 2460 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2461 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2462 cv_destroy(&sd_tr.srq_inprocess_cv); 2463 2464 sd_scsi_probe_cache_fini(); 2465 2466 sd_scsi_target_lun_fini(); 2467 2468 #ifndef XPV_HVM_DRIVER 2469 ddi_soft_state_fini(&sd_state); 2470 #endif /* !XPV_HVM_DRIVER */ 2471 return (err); 2472 } 2473 2474 return (err); 2475 } 2476 2477 2478 /* 2479 * Function: _fini 2480 * 2481 * Description: This is the driver _fini(9E) entry point. 2482 * 2483 * Return Code: Returns the value from mod_remove(9F) 2484 * 2485 * Context: Called when driver module is unloaded. 2486 */ 2487 2488 int 2489 _fini(void) 2490 { 2491 int err; 2492 2493 if ((err = mod_remove(&modlinkage)) != 0) { 2494 return (err); 2495 } 2496 2497 sd_taskq_delete(); 2498 2499 mutex_destroy(&sd_detach_mutex); 2500 mutex_destroy(&sd_log_mutex); 2501 mutex_destroy(&sd_label_mutex); 2502 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2503 2504 sd_scsi_probe_cache_fini(); 2505 2506 sd_scsi_target_lun_fini(); 2507 2508 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2509 cv_destroy(&sd_tr.srq_inprocess_cv); 2510 2511 #ifndef XPV_HVM_DRIVER 2512 ddi_soft_state_fini(&sd_state); 2513 #endif /* !XPV_HVM_DRIVER */ 2514 2515 return (err); 2516 } 2517 2518 2519 /* 2520 * Function: _info 2521 * 2522 * Description: This is the driver _info(9E) entry point. 2523 * 2524 * Arguments: modinfop - pointer to the driver modinfo structure 2525 * 2526 * Return Code: Returns the value from mod_info(9F). 2527 * 2528 * Context: Kernel thread context 2529 */ 2530 2531 int 2532 _info(struct modinfo *modinfop) 2533 { 2534 return (mod_info(&modlinkage, modinfop)); 2535 } 2536 2537 2538 /* 2539 * The following routines implement the driver message logging facility. 2540 * They provide component- and level- based debug output filtering. 2541 * Output may also be restricted to messages for a single instance by 2542 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2543 * to NULL, then messages for all instances are printed. 2544 * 2545 * These routines have been cloned from each other due to the language 2546 * constraints of macros and variable argument list processing. 2547 */ 2548 2549 2550 /* 2551 * Function: sd_log_err 2552 * 2553 * Description: This routine is called by the SD_ERROR macro for debug 2554 * logging of error conditions. 2555 * 2556 * Arguments: comp - driver component being logged 2557 * dev - pointer to driver info structure 2558 * fmt - error string and format to be logged 2559 */ 2560 2561 static void 2562 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2563 { 2564 va_list ap; 2565 dev_info_t *dev; 2566 2567 ASSERT(un != NULL); 2568 dev = SD_DEVINFO(un); 2569 ASSERT(dev != NULL); 2570 2571 /* 2572 * Filter messages based on the global component and level masks. 2573 * Also print if un matches the value of sd_debug_un, or if 2574 * sd_debug_un is set to NULL. 2575 */ 2576 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2577 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2578 mutex_enter(&sd_log_mutex); 2579 va_start(ap, fmt); 2580 (void) vsprintf(sd_log_buf, fmt, ap); 2581 va_end(ap); 2582 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2583 mutex_exit(&sd_log_mutex); 2584 } 2585 #ifdef SD_FAULT_INJECTION 2586 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2587 if (un->sd_injection_mask & comp) { 2588 mutex_enter(&sd_log_mutex); 2589 va_start(ap, fmt); 2590 (void) vsprintf(sd_log_buf, fmt, ap); 2591 va_end(ap); 2592 sd_injection_log(sd_log_buf, un); 2593 mutex_exit(&sd_log_mutex); 2594 } 2595 #endif 2596 } 2597 2598 2599 /* 2600 * Function: sd_log_info 2601 * 2602 * Description: This routine is called by the SD_INFO macro for debug 2603 * logging of general purpose informational conditions. 2604 * 2605 * Arguments: comp - driver component being logged 2606 * dev - pointer to driver info structure 2607 * fmt - info string and format to be logged 2608 */ 2609 2610 static void 2611 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2612 { 2613 va_list ap; 2614 dev_info_t *dev; 2615 2616 ASSERT(un != NULL); 2617 dev = SD_DEVINFO(un); 2618 ASSERT(dev != NULL); 2619 2620 /* 2621 * Filter messages based on the global component and level masks. 2622 * Also print if un matches the value of sd_debug_un, or if 2623 * sd_debug_un is set to NULL. 2624 */ 2625 if ((sd_component_mask & component) && 2626 (sd_level_mask & SD_LOGMASK_INFO) && 2627 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2628 mutex_enter(&sd_log_mutex); 2629 va_start(ap, fmt); 2630 (void) vsprintf(sd_log_buf, fmt, ap); 2631 va_end(ap); 2632 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2633 mutex_exit(&sd_log_mutex); 2634 } 2635 #ifdef SD_FAULT_INJECTION 2636 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2637 if (un->sd_injection_mask & component) { 2638 mutex_enter(&sd_log_mutex); 2639 va_start(ap, fmt); 2640 (void) vsprintf(sd_log_buf, fmt, ap); 2641 va_end(ap); 2642 sd_injection_log(sd_log_buf, un); 2643 mutex_exit(&sd_log_mutex); 2644 } 2645 #endif 2646 } 2647 2648 2649 /* 2650 * Function: sd_log_trace 2651 * 2652 * Description: This routine is called by the SD_TRACE macro for debug 2653 * logging of trace conditions (i.e. function entry/exit). 2654 * 2655 * Arguments: comp - driver component being logged 2656 * dev - pointer to driver info structure 2657 * fmt - trace string and format to be logged 2658 */ 2659 2660 static void 2661 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2662 { 2663 va_list ap; 2664 dev_info_t *dev; 2665 2666 ASSERT(un != NULL); 2667 dev = SD_DEVINFO(un); 2668 ASSERT(dev != NULL); 2669 2670 /* 2671 * Filter messages based on the global component and level masks. 2672 * Also print if un matches the value of sd_debug_un, or if 2673 * sd_debug_un is set to NULL. 2674 */ 2675 if ((sd_component_mask & component) && 2676 (sd_level_mask & SD_LOGMASK_TRACE) && 2677 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2678 mutex_enter(&sd_log_mutex); 2679 va_start(ap, fmt); 2680 (void) vsprintf(sd_log_buf, fmt, ap); 2681 va_end(ap); 2682 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2683 mutex_exit(&sd_log_mutex); 2684 } 2685 #ifdef SD_FAULT_INJECTION 2686 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2687 if (un->sd_injection_mask & component) { 2688 mutex_enter(&sd_log_mutex); 2689 va_start(ap, fmt); 2690 (void) vsprintf(sd_log_buf, fmt, ap); 2691 va_end(ap); 2692 sd_injection_log(sd_log_buf, un); 2693 mutex_exit(&sd_log_mutex); 2694 } 2695 #endif 2696 } 2697 2698 2699 /* 2700 * Function: sdprobe 2701 * 2702 * Description: This is the driver probe(9e) entry point function. 2703 * 2704 * Arguments: devi - opaque device info handle 2705 * 2706 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2707 * DDI_PROBE_FAILURE: If the probe failed. 2708 * DDI_PROBE_PARTIAL: If the instance is not present now, 2709 * but may be present in the future. 2710 */ 2711 2712 static int 2713 sdprobe(dev_info_t *devi) 2714 { 2715 struct scsi_device *devp; 2716 int rval; 2717 #ifndef XPV_HVM_DRIVER 2718 int instance = ddi_get_instance(devi); 2719 #endif /* !XPV_HVM_DRIVER */ 2720 2721 /* 2722 * if it wasn't for pln, sdprobe could actually be nulldev 2723 * in the "__fibre" case. 2724 */ 2725 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2726 return (DDI_PROBE_DONTCARE); 2727 } 2728 2729 devp = ddi_get_driver_private(devi); 2730 2731 if (devp == NULL) { 2732 /* Ooops... nexus driver is mis-configured... */ 2733 return (DDI_PROBE_FAILURE); 2734 } 2735 2736 #ifndef XPV_HVM_DRIVER 2737 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2738 return (DDI_PROBE_PARTIAL); 2739 } 2740 #endif /* !XPV_HVM_DRIVER */ 2741 2742 /* 2743 * Call the SCSA utility probe routine to see if we actually 2744 * have a target at this SCSI nexus. 2745 */ 2746 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2747 case SCSIPROBE_EXISTS: 2748 switch (devp->sd_inq->inq_dtype) { 2749 case DTYPE_DIRECT: 2750 rval = DDI_PROBE_SUCCESS; 2751 break; 2752 case DTYPE_RODIRECT: 2753 /* CDs etc. Can be removable media */ 2754 rval = DDI_PROBE_SUCCESS; 2755 break; 2756 case DTYPE_OPTICAL: 2757 /* 2758 * Rewritable optical driver HP115AA 2759 * Can also be removable media 2760 */ 2761 2762 /* 2763 * Do not attempt to bind to DTYPE_OPTICAL if 2764 * pre solaris 9 sparc sd behavior is required 2765 * 2766 * If first time through and sd_dtype_optical_bind 2767 * has not been set in /etc/system check properties 2768 */ 2769 2770 if (sd_dtype_optical_bind < 0) { 2771 sd_dtype_optical_bind = ddi_prop_get_int 2772 (DDI_DEV_T_ANY, devi, 0, 2773 "optical-device-bind", 1); 2774 } 2775 2776 if (sd_dtype_optical_bind == 0) { 2777 rval = DDI_PROBE_FAILURE; 2778 } else { 2779 rval = DDI_PROBE_SUCCESS; 2780 } 2781 break; 2782 2783 case DTYPE_NOTPRESENT: 2784 default: 2785 rval = DDI_PROBE_FAILURE; 2786 break; 2787 } 2788 break; 2789 default: 2790 rval = DDI_PROBE_PARTIAL; 2791 break; 2792 } 2793 2794 /* 2795 * This routine checks for resource allocation prior to freeing, 2796 * so it will take care of the "smart probing" case where a 2797 * scsi_probe() may or may not have been issued and will *not* 2798 * free previously-freed resources. 2799 */ 2800 scsi_unprobe(devp); 2801 return (rval); 2802 } 2803 2804 2805 /* 2806 * Function: sdinfo 2807 * 2808 * Description: This is the driver getinfo(9e) entry point function. 2809 * Given the device number, return the devinfo pointer from 2810 * the scsi_device structure or the instance number 2811 * associated with the dev_t. 2812 * 2813 * Arguments: dip - pointer to device info structure 2814 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2815 * DDI_INFO_DEVT2INSTANCE) 2816 * arg - driver dev_t 2817 * resultp - user buffer for request response 2818 * 2819 * Return Code: DDI_SUCCESS 2820 * DDI_FAILURE 2821 */ 2822 /* ARGSUSED */ 2823 static int 2824 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2825 { 2826 struct sd_lun *un; 2827 dev_t dev; 2828 int instance; 2829 int error; 2830 2831 switch (infocmd) { 2832 case DDI_INFO_DEVT2DEVINFO: 2833 dev = (dev_t)arg; 2834 instance = SDUNIT(dev); 2835 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2836 return (DDI_FAILURE); 2837 } 2838 *result = (void *) SD_DEVINFO(un); 2839 error = DDI_SUCCESS; 2840 break; 2841 case DDI_INFO_DEVT2INSTANCE: 2842 dev = (dev_t)arg; 2843 instance = SDUNIT(dev); 2844 *result = (void *)(uintptr_t)instance; 2845 error = DDI_SUCCESS; 2846 break; 2847 default: 2848 error = DDI_FAILURE; 2849 } 2850 return (error); 2851 } 2852 2853 /* 2854 * Function: sd_prop_op 2855 * 2856 * Description: This is the driver prop_op(9e) entry point function. 2857 * Return the number of blocks for the partition in question 2858 * or forward the request to the property facilities. 2859 * 2860 * Arguments: dev - device number 2861 * dip - pointer to device info structure 2862 * prop_op - property operator 2863 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2864 * name - pointer to property name 2865 * valuep - pointer or address of the user buffer 2866 * lengthp - property length 2867 * 2868 * Return Code: DDI_PROP_SUCCESS 2869 * DDI_PROP_NOT_FOUND 2870 * DDI_PROP_UNDEFINED 2871 * DDI_PROP_NO_MEMORY 2872 * DDI_PROP_BUF_TOO_SMALL 2873 */ 2874 2875 static int 2876 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2877 char *name, caddr_t valuep, int *lengthp) 2878 { 2879 struct sd_lun *un; 2880 2881 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL) 2882 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2883 name, valuep, lengthp)); 2884 2885 return (cmlb_prop_op(un->un_cmlbhandle, 2886 dev, dip, prop_op, mod_flags, name, valuep, lengthp, 2887 SDPART(dev), (void *)SD_PATH_DIRECT)); 2888 } 2889 2890 /* 2891 * The following functions are for smart probing: 2892 * sd_scsi_probe_cache_init() 2893 * sd_scsi_probe_cache_fini() 2894 * sd_scsi_clear_probe_cache() 2895 * sd_scsi_probe_with_cache() 2896 */ 2897 2898 /* 2899 * Function: sd_scsi_probe_cache_init 2900 * 2901 * Description: Initializes the probe response cache mutex and head pointer. 2902 * 2903 * Context: Kernel thread context 2904 */ 2905 2906 static void 2907 sd_scsi_probe_cache_init(void) 2908 { 2909 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2910 sd_scsi_probe_cache_head = NULL; 2911 } 2912 2913 2914 /* 2915 * Function: sd_scsi_probe_cache_fini 2916 * 2917 * Description: Frees all resources associated with the probe response cache. 2918 * 2919 * Context: Kernel thread context 2920 */ 2921 2922 static void 2923 sd_scsi_probe_cache_fini(void) 2924 { 2925 struct sd_scsi_probe_cache *cp; 2926 struct sd_scsi_probe_cache *ncp; 2927 2928 /* Clean up our smart probing linked list */ 2929 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2930 ncp = cp->next; 2931 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2932 } 2933 sd_scsi_probe_cache_head = NULL; 2934 mutex_destroy(&sd_scsi_probe_cache_mutex); 2935 } 2936 2937 2938 /* 2939 * Function: sd_scsi_clear_probe_cache 2940 * 2941 * Description: This routine clears the probe response cache. This is 2942 * done when open() returns ENXIO so that when deferred 2943 * attach is attempted (possibly after a device has been 2944 * turned on) we will retry the probe. Since we don't know 2945 * which target we failed to open, we just clear the 2946 * entire cache. 2947 * 2948 * Context: Kernel thread context 2949 */ 2950 2951 static void 2952 sd_scsi_clear_probe_cache(void) 2953 { 2954 struct sd_scsi_probe_cache *cp; 2955 int i; 2956 2957 mutex_enter(&sd_scsi_probe_cache_mutex); 2958 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2959 /* 2960 * Reset all entries to SCSIPROBE_EXISTS. This will 2961 * force probing to be performed the next time 2962 * sd_scsi_probe_with_cache is called. 2963 */ 2964 for (i = 0; i < NTARGETS_WIDE; i++) { 2965 cp->cache[i] = SCSIPROBE_EXISTS; 2966 } 2967 } 2968 mutex_exit(&sd_scsi_probe_cache_mutex); 2969 } 2970 2971 2972 /* 2973 * Function: sd_scsi_probe_with_cache 2974 * 2975 * Description: This routine implements support for a scsi device probe 2976 * with cache. The driver maintains a cache of the target 2977 * responses to scsi probes. If we get no response from a 2978 * target during a probe inquiry, we remember that, and we 2979 * avoid additional calls to scsi_probe on non-zero LUNs 2980 * on the same target until the cache is cleared. By doing 2981 * so we avoid the 1/4 sec selection timeout for nonzero 2982 * LUNs. lun0 of a target is always probed. 2983 * 2984 * Arguments: devp - Pointer to a scsi_device(9S) structure 2985 * waitfunc - indicates what the allocator routines should 2986 * do when resources are not available. This value 2987 * is passed on to scsi_probe() when that routine 2988 * is called. 2989 * 2990 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2991 * otherwise the value returned by scsi_probe(9F). 2992 * 2993 * Context: Kernel thread context 2994 */ 2995 2996 static int 2997 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2998 { 2999 struct sd_scsi_probe_cache *cp; 3000 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 3001 int lun, tgt; 3002 3003 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 3004 SCSI_ADDR_PROP_LUN, 0); 3005 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 3006 SCSI_ADDR_PROP_TARGET, -1); 3007 3008 /* Make sure caching enabled and target in range */ 3009 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 3010 /* do it the old way (no cache) */ 3011 return (scsi_probe(devp, waitfn)); 3012 } 3013 3014 mutex_enter(&sd_scsi_probe_cache_mutex); 3015 3016 /* Find the cache for this scsi bus instance */ 3017 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 3018 if (cp->pdip == pdip) { 3019 break; 3020 } 3021 } 3022 3023 /* If we can't find a cache for this pdip, create one */ 3024 if (cp == NULL) { 3025 int i; 3026 3027 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 3028 KM_SLEEP); 3029 cp->pdip = pdip; 3030 cp->next = sd_scsi_probe_cache_head; 3031 sd_scsi_probe_cache_head = cp; 3032 for (i = 0; i < NTARGETS_WIDE; i++) { 3033 cp->cache[i] = SCSIPROBE_EXISTS; 3034 } 3035 } 3036 3037 mutex_exit(&sd_scsi_probe_cache_mutex); 3038 3039 /* Recompute the cache for this target if LUN zero */ 3040 if (lun == 0) { 3041 cp->cache[tgt] = SCSIPROBE_EXISTS; 3042 } 3043 3044 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 3045 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 3046 return (SCSIPROBE_NORESP); 3047 } 3048 3049 /* Do the actual probe; save & return the result */ 3050 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 3051 } 3052 3053 3054 /* 3055 * Function: sd_scsi_target_lun_init 3056 * 3057 * Description: Initializes the attached lun chain mutex and head pointer. 3058 * 3059 * Context: Kernel thread context 3060 */ 3061 3062 static void 3063 sd_scsi_target_lun_init(void) 3064 { 3065 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 3066 sd_scsi_target_lun_head = NULL; 3067 } 3068 3069 3070 /* 3071 * Function: sd_scsi_target_lun_fini 3072 * 3073 * Description: Frees all resources associated with the attached lun 3074 * chain 3075 * 3076 * Context: Kernel thread context 3077 */ 3078 3079 static void 3080 sd_scsi_target_lun_fini(void) 3081 { 3082 struct sd_scsi_hba_tgt_lun *cp; 3083 struct sd_scsi_hba_tgt_lun *ncp; 3084 3085 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 3086 ncp = cp->next; 3087 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 3088 } 3089 sd_scsi_target_lun_head = NULL; 3090 mutex_destroy(&sd_scsi_target_lun_mutex); 3091 } 3092 3093 3094 /* 3095 * Function: sd_scsi_get_target_lun_count 3096 * 3097 * Description: This routine will check in the attached lun chain to see 3098 * how many luns are attached on the required SCSI controller 3099 * and target. Currently, some capabilities like tagged queue 3100 * are supported per target based by HBA. So all luns in a 3101 * target have the same capabilities. Based on this assumption, 3102 * sd should only set these capabilities once per target. This 3103 * function is called when sd needs to decide how many luns 3104 * already attached on a target. 3105 * 3106 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 3107 * controller device. 3108 * target - The target ID on the controller's SCSI bus. 3109 * 3110 * Return Code: The number of luns attached on the required target and 3111 * controller. 3112 * -1 if target ID is not in parallel SCSI scope or the given 3113 * dip is not in the chain. 3114 * 3115 * Context: Kernel thread context 3116 */ 3117 3118 static int 3119 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 3120 { 3121 struct sd_scsi_hba_tgt_lun *cp; 3122 3123 if ((target < 0) || (target >= NTARGETS_WIDE)) { 3124 return (-1); 3125 } 3126 3127 mutex_enter(&sd_scsi_target_lun_mutex); 3128 3129 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3130 if (cp->pdip == dip) { 3131 break; 3132 } 3133 } 3134 3135 mutex_exit(&sd_scsi_target_lun_mutex); 3136 3137 if (cp == NULL) { 3138 return (-1); 3139 } 3140 3141 return (cp->nlun[target]); 3142 } 3143 3144 3145 /* 3146 * Function: sd_scsi_update_lun_on_target 3147 * 3148 * Description: This routine is used to update the attached lun chain when a 3149 * lun is attached or detached on a target. 3150 * 3151 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 3152 * controller device. 3153 * target - The target ID on the controller's SCSI bus. 3154 * flag - Indicate the lun is attached or detached. 3155 * 3156 * Context: Kernel thread context 3157 */ 3158 3159 static void 3160 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 3161 { 3162 struct sd_scsi_hba_tgt_lun *cp; 3163 3164 mutex_enter(&sd_scsi_target_lun_mutex); 3165 3166 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3167 if (cp->pdip == dip) { 3168 break; 3169 } 3170 } 3171 3172 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3173 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3174 KM_SLEEP); 3175 cp->pdip = dip; 3176 cp->next = sd_scsi_target_lun_head; 3177 sd_scsi_target_lun_head = cp; 3178 } 3179 3180 mutex_exit(&sd_scsi_target_lun_mutex); 3181 3182 if (cp != NULL) { 3183 if (flag == SD_SCSI_LUN_ATTACH) { 3184 cp->nlun[target] ++; 3185 } else { 3186 cp->nlun[target] --; 3187 } 3188 } 3189 } 3190 3191 3192 /* 3193 * Function: sd_spin_up_unit 3194 * 3195 * Description: Issues the following commands to spin-up the device: 3196 * START STOP UNIT, and INQUIRY. 3197 * 3198 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3199 * structure for this target. 3200 * 3201 * Return Code: 0 - success 3202 * EIO - failure 3203 * EACCES - reservation conflict 3204 * 3205 * Context: Kernel thread context 3206 */ 3207 3208 static int 3209 sd_spin_up_unit(sd_ssc_t *ssc) 3210 { 3211 size_t resid = 0; 3212 int has_conflict = FALSE; 3213 uchar_t *bufaddr; 3214 int status; 3215 struct sd_lun *un; 3216 3217 ASSERT(ssc != NULL); 3218 un = ssc->ssc_un; 3219 ASSERT(un != NULL); 3220 3221 /* 3222 * Send a throwaway START UNIT command. 3223 * 3224 * If we fail on this, we don't care presently what precisely 3225 * is wrong. EMC's arrays will also fail this with a check 3226 * condition (0x2/0x4/0x3) if the device is "inactive," but 3227 * we don't want to fail the attach because it may become 3228 * "active" later. 3229 * We don't know if power condition is supported or not at 3230 * this stage, use START STOP bit. 3231 */ 3232 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 3233 SD_TARGET_START, SD_PATH_DIRECT); 3234 3235 if (status != 0) { 3236 if (status == EACCES) 3237 has_conflict = TRUE; 3238 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3239 } 3240 3241 /* 3242 * Send another INQUIRY command to the target. This is necessary for 3243 * non-removable media direct access devices because their INQUIRY data 3244 * may not be fully qualified until they are spun up (perhaps via the 3245 * START command above). Note: This seems to be needed for some 3246 * legacy devices only.) The INQUIRY command should succeed even if a 3247 * Reservation Conflict is present. 3248 */ 3249 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3250 3251 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid) 3252 != 0) { 3253 kmem_free(bufaddr, SUN_INQSIZE); 3254 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 3255 return (EIO); 3256 } 3257 3258 /* 3259 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3260 * Note that this routine does not return a failure here even if the 3261 * INQUIRY command did not return any data. This is a legacy behavior. 3262 */ 3263 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3264 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3265 } 3266 3267 kmem_free(bufaddr, SUN_INQSIZE); 3268 3269 /* If we hit a reservation conflict above, tell the caller. */ 3270 if (has_conflict == TRUE) { 3271 return (EACCES); 3272 } 3273 3274 return (0); 3275 } 3276 3277 #ifdef _LP64 3278 /* 3279 * Function: sd_enable_descr_sense 3280 * 3281 * Description: This routine attempts to select descriptor sense format 3282 * using the Control mode page. Devices that support 64 bit 3283 * LBAs (for >2TB luns) should also implement descriptor 3284 * sense data so we will call this function whenever we see 3285 * a lun larger than 2TB. If for some reason the device 3286 * supports 64 bit LBAs but doesn't support descriptor sense 3287 * presumably the mode select will fail. Everything will 3288 * continue to work normally except that we will not get 3289 * complete sense data for commands that fail with an LBA 3290 * larger than 32 bits. 3291 * 3292 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3293 * structure for this target. 3294 * 3295 * Context: Kernel thread context only 3296 */ 3297 3298 static void 3299 sd_enable_descr_sense(sd_ssc_t *ssc) 3300 { 3301 uchar_t *header; 3302 struct mode_control_scsi3 *ctrl_bufp; 3303 size_t buflen; 3304 size_t bd_len; 3305 int status; 3306 struct sd_lun *un; 3307 3308 ASSERT(ssc != NULL); 3309 un = ssc->ssc_un; 3310 ASSERT(un != NULL); 3311 3312 /* 3313 * Read MODE SENSE page 0xA, Control Mode Page 3314 */ 3315 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3316 sizeof (struct mode_control_scsi3); 3317 header = kmem_zalloc(buflen, KM_SLEEP); 3318 3319 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 3320 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT); 3321 3322 if (status != 0) { 3323 SD_ERROR(SD_LOG_COMMON, un, 3324 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3325 goto eds_exit; 3326 } 3327 3328 /* 3329 * Determine size of Block Descriptors in order to locate 3330 * the mode page data. ATAPI devices return 0, SCSI devices 3331 * should return MODE_BLK_DESC_LENGTH. 3332 */ 3333 bd_len = ((struct mode_header *)header)->bdesc_length; 3334 3335 /* Clear the mode data length field for MODE SELECT */ 3336 ((struct mode_header *)header)->length = 0; 3337 3338 ctrl_bufp = (struct mode_control_scsi3 *) 3339 (header + MODE_HEADER_LENGTH + bd_len); 3340 3341 /* 3342 * If the page length is smaller than the expected value, 3343 * the target device doesn't support D_SENSE. Bail out here. 3344 */ 3345 if (ctrl_bufp->mode_page.length < 3346 sizeof (struct mode_control_scsi3) - 2) { 3347 SD_ERROR(SD_LOG_COMMON, un, 3348 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3349 goto eds_exit; 3350 } 3351 3352 /* 3353 * Clear PS bit for MODE SELECT 3354 */ 3355 ctrl_bufp->mode_page.ps = 0; 3356 3357 /* 3358 * Set D_SENSE to enable descriptor sense format. 3359 */ 3360 ctrl_bufp->d_sense = 1; 3361 3362 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3363 3364 /* 3365 * Use MODE SELECT to commit the change to the D_SENSE bit 3366 */ 3367 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 3368 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT); 3369 3370 if (status != 0) { 3371 SD_INFO(SD_LOG_COMMON, un, 3372 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3373 } else { 3374 kmem_free(header, buflen); 3375 return; 3376 } 3377 3378 eds_exit: 3379 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3380 kmem_free(header, buflen); 3381 } 3382 3383 /* 3384 * Function: sd_reenable_dsense_task 3385 * 3386 * Description: Re-enable descriptor sense after device or bus reset 3387 * 3388 * Context: Executes in a taskq() thread context 3389 */ 3390 static void 3391 sd_reenable_dsense_task(void *arg) 3392 { 3393 struct sd_lun *un = arg; 3394 sd_ssc_t *ssc; 3395 3396 ASSERT(un != NULL); 3397 3398 ssc = sd_ssc_init(un); 3399 sd_enable_descr_sense(ssc); 3400 sd_ssc_fini(ssc); 3401 } 3402 #endif /* _LP64 */ 3403 3404 /* 3405 * Function: sd_set_mmc_caps 3406 * 3407 * Description: This routine determines if the device is MMC compliant and if 3408 * the device supports CDDA via a mode sense of the CDVD 3409 * capabilities mode page. Also checks if the device is a 3410 * dvdram writable device. 3411 * 3412 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3413 * structure for this target. 3414 * 3415 * Context: Kernel thread context only 3416 */ 3417 3418 static void 3419 sd_set_mmc_caps(sd_ssc_t *ssc) 3420 { 3421 struct mode_header_grp2 *sense_mhp; 3422 uchar_t *sense_page; 3423 caddr_t buf; 3424 int bd_len; 3425 int status; 3426 struct uscsi_cmd com; 3427 int rtn; 3428 uchar_t *out_data_rw, *out_data_hd; 3429 uchar_t *rqbuf_rw, *rqbuf_hd; 3430 uchar_t *out_data_gesn; 3431 int gesn_len; 3432 struct sd_lun *un; 3433 3434 ASSERT(ssc != NULL); 3435 un = ssc->ssc_un; 3436 ASSERT(un != NULL); 3437 3438 /* 3439 * The flags which will be set in this function are - mmc compliant, 3440 * dvdram writable device, cdda support. Initialize them to FALSE 3441 * and if a capability is detected - it will be set to TRUE. 3442 */ 3443 un->un_f_mmc_cap = FALSE; 3444 un->un_f_dvdram_writable_device = FALSE; 3445 un->un_f_cfg_cdda = FALSE; 3446 3447 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3448 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3449 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3450 3451 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3452 3453 if (status != 0) { 3454 /* command failed; just return */ 3455 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3456 return; 3457 } 3458 /* 3459 * If the mode sense request for the CDROM CAPABILITIES 3460 * page (0x2A) succeeds the device is assumed to be MMC. 3461 */ 3462 un->un_f_mmc_cap = TRUE; 3463 3464 /* See if GET STATUS EVENT NOTIFICATION is supported */ 3465 if (un->un_f_mmc_gesn_polling) { 3466 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN; 3467 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP); 3468 3469 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc, 3470 out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS); 3471 3472 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3473 3474 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) { 3475 un->un_f_mmc_gesn_polling = FALSE; 3476 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3477 "sd_set_mmc_caps: gesn not supported " 3478 "%d %x %x %x %x\n", rtn, 3479 out_data_gesn[0], out_data_gesn[1], 3480 out_data_gesn[2], out_data_gesn[3]); 3481 } 3482 3483 kmem_free(out_data_gesn, gesn_len); 3484 } 3485 3486 /* Get to the page data */ 3487 sense_mhp = (struct mode_header_grp2 *)buf; 3488 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3489 sense_mhp->bdesc_length_lo; 3490 if (bd_len > MODE_BLK_DESC_LENGTH) { 3491 /* 3492 * We did not get back the expected block descriptor 3493 * length so we cannot determine if the device supports 3494 * CDDA. However, we still indicate the device is MMC 3495 * according to the successful response to the page 3496 * 0x2A mode sense request. 3497 */ 3498 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3499 "sd_set_mmc_caps: Mode Sense returned " 3500 "invalid block descriptor length\n"); 3501 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3502 return; 3503 } 3504 3505 /* See if read CDDA is supported */ 3506 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3507 bd_len); 3508 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3509 3510 /* See if writing DVD RAM is supported. */ 3511 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3512 if (un->un_f_dvdram_writable_device == TRUE) { 3513 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3514 return; 3515 } 3516 3517 /* 3518 * If the device presents DVD or CD capabilities in the mode 3519 * page, we can return here since a RRD will not have 3520 * these capabilities. 3521 */ 3522 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3523 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3524 return; 3525 } 3526 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3527 3528 /* 3529 * If un->un_f_dvdram_writable_device is still FALSE, 3530 * check for a Removable Rigid Disk (RRD). A RRD 3531 * device is identified by the features RANDOM_WRITABLE and 3532 * HARDWARE_DEFECT_MANAGEMENT. 3533 */ 3534 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3535 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3536 3537 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3538 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3539 RANDOM_WRITABLE, SD_PATH_STANDARD); 3540 3541 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3542 3543 if (rtn != 0) { 3544 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3545 kmem_free(rqbuf_rw, SENSE_LENGTH); 3546 return; 3547 } 3548 3549 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3550 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3551 3552 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3553 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3554 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3555 3556 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3557 3558 if (rtn == 0) { 3559 /* 3560 * We have good information, check for random writable 3561 * and hardware defect features. 3562 */ 3563 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3564 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3565 un->un_f_dvdram_writable_device = TRUE; 3566 } 3567 } 3568 3569 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3570 kmem_free(rqbuf_rw, SENSE_LENGTH); 3571 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3572 kmem_free(rqbuf_hd, SENSE_LENGTH); 3573 } 3574 3575 /* 3576 * Function: sd_check_for_writable_cd 3577 * 3578 * Description: This routine determines if the media in the device is 3579 * writable or not. It uses the get configuration command (0x46) 3580 * to determine if the media is writable 3581 * 3582 * Arguments: un - driver soft state (unit) structure 3583 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3584 * chain and the normal command waitq, or 3585 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3586 * "direct" chain and bypass the normal command 3587 * waitq. 3588 * 3589 * Context: Never called at interrupt context. 3590 */ 3591 3592 static void 3593 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag) 3594 { 3595 struct uscsi_cmd com; 3596 uchar_t *out_data; 3597 uchar_t *rqbuf; 3598 int rtn; 3599 uchar_t *out_data_rw, *out_data_hd; 3600 uchar_t *rqbuf_rw, *rqbuf_hd; 3601 struct mode_header_grp2 *sense_mhp; 3602 uchar_t *sense_page; 3603 caddr_t buf; 3604 int bd_len; 3605 int status; 3606 struct sd_lun *un; 3607 3608 ASSERT(ssc != NULL); 3609 un = ssc->ssc_un; 3610 ASSERT(un != NULL); 3611 ASSERT(mutex_owned(SD_MUTEX(un))); 3612 3613 /* 3614 * Initialize the writable media to false, if configuration info. 3615 * tells us otherwise then only we will set it. 3616 */ 3617 un->un_f_mmc_writable_media = FALSE; 3618 mutex_exit(SD_MUTEX(un)); 3619 3620 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3621 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3622 3623 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH, 3624 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3625 3626 if (rtn != 0) 3627 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3628 3629 mutex_enter(SD_MUTEX(un)); 3630 if (rtn == 0) { 3631 /* 3632 * We have good information, check for writable DVD. 3633 */ 3634 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3635 un->un_f_mmc_writable_media = TRUE; 3636 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3637 kmem_free(rqbuf, SENSE_LENGTH); 3638 return; 3639 } 3640 } 3641 3642 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3643 kmem_free(rqbuf, SENSE_LENGTH); 3644 3645 /* 3646 * Determine if this is a RRD type device. 3647 */ 3648 mutex_exit(SD_MUTEX(un)); 3649 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3650 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3651 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3652 3653 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3654 3655 mutex_enter(SD_MUTEX(un)); 3656 if (status != 0) { 3657 /* command failed; just return */ 3658 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3659 return; 3660 } 3661 3662 /* Get to the page data */ 3663 sense_mhp = (struct mode_header_grp2 *)buf; 3664 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3665 if (bd_len > MODE_BLK_DESC_LENGTH) { 3666 /* 3667 * We did not get back the expected block descriptor length so 3668 * we cannot check the mode page. 3669 */ 3670 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3671 "sd_check_for_writable_cd: Mode Sense returned " 3672 "invalid block descriptor length\n"); 3673 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3674 return; 3675 } 3676 3677 /* 3678 * If the device presents DVD or CD capabilities in the mode 3679 * page, we can return here since a RRD device will not have 3680 * these capabilities. 3681 */ 3682 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3683 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3684 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3685 return; 3686 } 3687 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3688 3689 /* 3690 * If un->un_f_mmc_writable_media is still FALSE, 3691 * check for RRD type media. A RRD device is identified 3692 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3693 */ 3694 mutex_exit(SD_MUTEX(un)); 3695 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3696 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3697 3698 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3699 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3700 RANDOM_WRITABLE, path_flag); 3701 3702 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3703 if (rtn != 0) { 3704 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3705 kmem_free(rqbuf_rw, SENSE_LENGTH); 3706 mutex_enter(SD_MUTEX(un)); 3707 return; 3708 } 3709 3710 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3711 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3712 3713 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3714 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3715 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3716 3717 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3718 mutex_enter(SD_MUTEX(un)); 3719 if (rtn == 0) { 3720 /* 3721 * We have good information, check for random writable 3722 * and hardware defect features as current. 3723 */ 3724 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3725 (out_data_rw[10] & 0x1) && 3726 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3727 (out_data_hd[10] & 0x1)) { 3728 un->un_f_mmc_writable_media = TRUE; 3729 } 3730 } 3731 3732 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3733 kmem_free(rqbuf_rw, SENSE_LENGTH); 3734 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3735 kmem_free(rqbuf_hd, SENSE_LENGTH); 3736 } 3737 3738 /* 3739 * Function: sd_read_unit_properties 3740 * 3741 * Description: The following implements a property lookup mechanism. 3742 * Properties for particular disks (keyed on vendor, model 3743 * and rev numbers) are sought in the sd.conf file via 3744 * sd_process_sdconf_file(), and if not found there, are 3745 * looked for in a list hardcoded in this driver via 3746 * sd_process_sdconf_table() Once located the properties 3747 * are used to update the driver unit structure. 3748 * 3749 * Arguments: un - driver soft state (unit) structure 3750 */ 3751 3752 static void 3753 sd_read_unit_properties(struct sd_lun *un) 3754 { 3755 /* 3756 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3757 * the "sd-config-list" property (from the sd.conf file) or if 3758 * there was not a match for the inquiry vid/pid. If this event 3759 * occurs the static driver configuration table is searched for 3760 * a match. 3761 */ 3762 ASSERT(un != NULL); 3763 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3764 sd_process_sdconf_table(un); 3765 } 3766 3767 /* check for LSI device */ 3768 sd_is_lsi(un); 3769 3770 3771 } 3772 3773 3774 /* 3775 * Function: sd_process_sdconf_file 3776 * 3777 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the 3778 * driver's config file (ie, sd.conf) and update the driver 3779 * soft state structure accordingly. 3780 * 3781 * Arguments: un - driver soft state (unit) structure 3782 * 3783 * Return Code: SD_SUCCESS - The properties were successfully set according 3784 * to the driver configuration file. 3785 * SD_FAILURE - The driver config list was not obtained or 3786 * there was no vid/pid match. This indicates that 3787 * the static config table should be used. 3788 * 3789 * The config file has a property, "sd-config-list". Currently we support 3790 * two kinds of formats. For both formats, the value of this property 3791 * is a list of duplets: 3792 * 3793 * sd-config-list= 3794 * <duplet>, 3795 * [,<duplet>]*; 3796 * 3797 * For the improved format, where 3798 * 3799 * <duplet>:= "<vid+pid>","<tunable-list>" 3800 * 3801 * and 3802 * 3803 * <tunable-list>:= <tunable> [, <tunable> ]*; 3804 * <tunable> = <name> : <value> 3805 * 3806 * The <vid+pid> is the string that is returned by the target device on a 3807 * SCSI inquiry command, the <tunable-list> contains one or more tunables 3808 * to apply to all target devices with the specified <vid+pid>. 3809 * 3810 * Each <tunable> is a "<name> : <value>" pair. 3811 * 3812 * For the old format, the structure of each duplet is as follows: 3813 * 3814 * <duplet>:= "<vid+pid>","<data-property-name_list>" 3815 * 3816 * The first entry of the duplet is the device ID string (the concatenated 3817 * vid & pid; not to be confused with a device_id). This is defined in 3818 * the same way as in the sd_disk_table. 3819 * 3820 * The second part of the duplet is a string that identifies a 3821 * data-property-name-list. The data-property-name-list is defined as 3822 * follows: 3823 * 3824 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3825 * 3826 * The syntax of <data-property-name> depends on the <version> field. 3827 * 3828 * If version = SD_CONF_VERSION_1 we have the following syntax: 3829 * 3830 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3831 * 3832 * where the prop0 value will be used to set prop0 if bit0 set in the 3833 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3834 * 3835 */ 3836 3837 static int 3838 sd_process_sdconf_file(struct sd_lun *un) 3839 { 3840 char **config_list = NULL; 3841 uint_t nelements; 3842 char *vidptr; 3843 int vidlen; 3844 char *dnlist_ptr; 3845 char *dataname_ptr; 3846 char *dataname_lasts; 3847 int *data_list = NULL; 3848 uint_t data_list_len; 3849 int rval = SD_FAILURE; 3850 int i; 3851 3852 ASSERT(un != NULL); 3853 3854 /* Obtain the configuration list associated with the .conf file */ 3855 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un), 3856 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list, 3857 &config_list, &nelements) != DDI_PROP_SUCCESS) { 3858 return (SD_FAILURE); 3859 } 3860 3861 /* 3862 * Compare vids in each duplet to the inquiry vid - if a match is 3863 * made, get the data value and update the soft state structure 3864 * accordingly. 3865 * 3866 * Each duplet should show as a pair of strings, return SD_FAILURE 3867 * otherwise. 3868 */ 3869 if (nelements & 1) { 3870 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3871 "sd-config-list should show as pairs of strings.\n"); 3872 if (config_list) 3873 ddi_prop_free(config_list); 3874 return (SD_FAILURE); 3875 } 3876 3877 for (i = 0; i < nelements; i += 2) { 3878 /* 3879 * Note: The assumption here is that each vid entry is on 3880 * a unique line from its associated duplet. 3881 */ 3882 vidptr = config_list[i]; 3883 vidlen = (int)strlen(vidptr); 3884 if ((vidlen == 0) || 3885 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3886 continue; 3887 } 3888 3889 /* 3890 * dnlist contains 1 or more blank separated 3891 * data-property-name entries 3892 */ 3893 dnlist_ptr = config_list[i + 1]; 3894 3895 if (strchr(dnlist_ptr, ':') != NULL) { 3896 /* 3897 * Decode the improved format sd-config-list. 3898 */ 3899 sd_nvpair_str_decode(un, dnlist_ptr); 3900 } else { 3901 /* 3902 * The old format sd-config-list, loop through all 3903 * data-property-name entries in the 3904 * data-property-name-list 3905 * setting the properties for each. 3906 */ 3907 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t", 3908 &dataname_lasts); dataname_ptr != NULL; 3909 dataname_ptr = sd_strtok_r(NULL, " \t", 3910 &dataname_lasts)) { 3911 int version; 3912 3913 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3914 "sd_process_sdconf_file: disk:%s, " 3915 "data:%s\n", vidptr, dataname_ptr); 3916 3917 /* Get the data list */ 3918 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, 3919 SD_DEVINFO(un), 0, dataname_ptr, &data_list, 3920 &data_list_len) != DDI_PROP_SUCCESS) { 3921 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3922 "sd_process_sdconf_file: data " 3923 "property (%s) has no value\n", 3924 dataname_ptr); 3925 continue; 3926 } 3927 3928 version = data_list[0]; 3929 3930 if (version == SD_CONF_VERSION_1) { 3931 sd_tunables values; 3932 3933 /* Set the properties */ 3934 if (sd_chk_vers1_data(un, data_list[1], 3935 &data_list[2], data_list_len, 3936 dataname_ptr) == SD_SUCCESS) { 3937 sd_get_tunables_from_conf(un, 3938 data_list[1], &data_list[2], 3939 &values); 3940 sd_set_vers1_properties(un, 3941 data_list[1], &values); 3942 rval = SD_SUCCESS; 3943 } else { 3944 rval = SD_FAILURE; 3945 } 3946 } else { 3947 scsi_log(SD_DEVINFO(un), sd_label, 3948 CE_WARN, "data property %s version " 3949 "0x%x is invalid.", 3950 dataname_ptr, version); 3951 rval = SD_FAILURE; 3952 } 3953 if (data_list) 3954 ddi_prop_free(data_list); 3955 } 3956 } 3957 } 3958 3959 /* free up the memory allocated by ddi_prop_lookup_string_array(). */ 3960 if (config_list) { 3961 ddi_prop_free(config_list); 3962 } 3963 3964 return (rval); 3965 } 3966 3967 /* 3968 * Function: sd_nvpair_str_decode() 3969 * 3970 * Description: Parse the improved format sd-config-list to get 3971 * each entry of tunable, which includes a name-value pair. 3972 * Then call sd_set_properties() to set the property. 3973 * 3974 * Arguments: un - driver soft state (unit) structure 3975 * nvpair_str - the tunable list 3976 */ 3977 static void 3978 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) 3979 { 3980 char *nv, *name, *value, *token; 3981 char *nv_lasts, *v_lasts, *x_lasts; 3982 3983 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; 3984 nv = sd_strtok_r(NULL, ",", &nv_lasts)) { 3985 token = sd_strtok_r(nv, ":", &v_lasts); 3986 name = sd_strtok_r(token, " \t", &x_lasts); 3987 token = sd_strtok_r(NULL, ":", &v_lasts); 3988 value = sd_strtok_r(token, " \t", &x_lasts); 3989 if (name == NULL || value == NULL) { 3990 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3991 "sd_nvpair_str_decode: " 3992 "name or value is not valid!\n"); 3993 } else { 3994 sd_set_properties(un, name, value); 3995 } 3996 } 3997 } 3998 3999 /* 4000 * Function: sd_strtok_r() 4001 * 4002 * Description: This function uses strpbrk and strspn to break 4003 * string into tokens on sequentially subsequent calls. Return 4004 * NULL when no non-separator characters remain. The first 4005 * argument is NULL for subsequent calls. 4006 */ 4007 static char * 4008 sd_strtok_r(char *string, const char *sepset, char **lasts) 4009 { 4010 char *q, *r; 4011 4012 /* First or subsequent call */ 4013 if (string == NULL) 4014 string = *lasts; 4015 4016 if (string == NULL) 4017 return (NULL); 4018 4019 /* Skip leading separators */ 4020 q = string + strspn(string, sepset); 4021 4022 if (*q == '\0') 4023 return (NULL); 4024 4025 if ((r = strpbrk(q, sepset)) == NULL) 4026 *lasts = NULL; 4027 else { 4028 *r = '\0'; 4029 *lasts = r + 1; 4030 } 4031 return (q); 4032 } 4033 4034 /* 4035 * Function: sd_set_properties() 4036 * 4037 * Description: Set device properties based on the improved 4038 * format sd-config-list. 4039 * 4040 * Arguments: un - driver soft state (unit) structure 4041 * name - supported tunable name 4042 * value - tunable value 4043 */ 4044 static void 4045 sd_set_properties(struct sd_lun *un, char *name, char *value) 4046 { 4047 char *endptr = NULL; 4048 long val = 0; 4049 4050 if (strcasecmp(name, "cache-nonvolatile") == 0) { 4051 if (strcasecmp(value, "true") == 0) { 4052 un->un_f_suppress_cache_flush = TRUE; 4053 } else if (strcasecmp(value, "false") == 0) { 4054 un->un_f_suppress_cache_flush = FALSE; 4055 } else { 4056 goto value_invalid; 4057 } 4058 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4059 "suppress_cache_flush flag set to %d\n", 4060 un->un_f_suppress_cache_flush); 4061 return; 4062 } 4063 4064 if (strcasecmp(name, "controller-type") == 0) { 4065 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4066 un->un_ctype = val; 4067 } else { 4068 goto value_invalid; 4069 } 4070 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4071 "ctype set to %d\n", un->un_ctype); 4072 return; 4073 } 4074 4075 if (strcasecmp(name, "delay-busy") == 0) { 4076 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4077 un->un_busy_timeout = drv_usectohz(val / 1000); 4078 } else { 4079 goto value_invalid; 4080 } 4081 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4082 "busy_timeout set to %d\n", un->un_busy_timeout); 4083 return; 4084 } 4085 4086 if (strcasecmp(name, "disksort") == 0) { 4087 if (strcasecmp(value, "true") == 0) { 4088 un->un_f_disksort_disabled = FALSE; 4089 } else if (strcasecmp(value, "false") == 0) { 4090 un->un_f_disksort_disabled = TRUE; 4091 } else { 4092 goto value_invalid; 4093 } 4094 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4095 "disksort disabled flag set to %d\n", 4096 un->un_f_disksort_disabled); 4097 return; 4098 } 4099 4100 if (strcasecmp(name, "power-condition") == 0) { 4101 if (strcasecmp(value, "true") == 0) { 4102 un->un_f_power_condition_disabled = FALSE; 4103 } else if (strcasecmp(value, "false") == 0) { 4104 un->un_f_power_condition_disabled = TRUE; 4105 } else { 4106 goto value_invalid; 4107 } 4108 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4109 "power condition disabled flag set to %d\n", 4110 un->un_f_power_condition_disabled); 4111 return; 4112 } 4113 4114 if (strcasecmp(name, "timeout-releasereservation") == 0) { 4115 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4116 un->un_reserve_release_time = val; 4117 } else { 4118 goto value_invalid; 4119 } 4120 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4121 "reservation release timeout set to %d\n", 4122 un->un_reserve_release_time); 4123 return; 4124 } 4125 4126 if (strcasecmp(name, "reset-lun") == 0) { 4127 if (strcasecmp(value, "true") == 0) { 4128 un->un_f_lun_reset_enabled = TRUE; 4129 } else if (strcasecmp(value, "false") == 0) { 4130 un->un_f_lun_reset_enabled = FALSE; 4131 } else { 4132 goto value_invalid; 4133 } 4134 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4135 "lun reset enabled flag set to %d\n", 4136 un->un_f_lun_reset_enabled); 4137 return; 4138 } 4139 4140 if (strcasecmp(name, "retries-busy") == 0) { 4141 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4142 un->un_busy_retry_count = val; 4143 } else { 4144 goto value_invalid; 4145 } 4146 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4147 "busy retry count set to %d\n", un->un_busy_retry_count); 4148 return; 4149 } 4150 4151 if (strcasecmp(name, "retries-timeout") == 0) { 4152 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4153 un->un_retry_count = val; 4154 } else { 4155 goto value_invalid; 4156 } 4157 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4158 "timeout retry count set to %d\n", un->un_retry_count); 4159 return; 4160 } 4161 4162 if (strcasecmp(name, "retries-notready") == 0) { 4163 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4164 un->un_notready_retry_count = val; 4165 } else { 4166 goto value_invalid; 4167 } 4168 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4169 "notready retry count set to %d\n", 4170 un->un_notready_retry_count); 4171 return; 4172 } 4173 4174 if (strcasecmp(name, "retries-reset") == 0) { 4175 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4176 un->un_reset_retry_count = val; 4177 } else { 4178 goto value_invalid; 4179 } 4180 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4181 "reset retry count set to %d\n", 4182 un->un_reset_retry_count); 4183 return; 4184 } 4185 4186 if (strcasecmp(name, "throttle-max") == 0) { 4187 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4188 un->un_saved_throttle = un->un_throttle = val; 4189 } else { 4190 goto value_invalid; 4191 } 4192 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4193 "throttle set to %d\n", un->un_throttle); 4194 } 4195 4196 if (strcasecmp(name, "throttle-min") == 0) { 4197 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4198 un->un_min_throttle = val; 4199 } else { 4200 goto value_invalid; 4201 } 4202 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4203 "min throttle set to %d\n", un->un_min_throttle); 4204 } 4205 4206 if (strcasecmp(name, "rmw-type") == 0) { 4207 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4208 un->un_f_rmw_type = val; 4209 } else { 4210 goto value_invalid; 4211 } 4212 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4213 "RMW type set to %d\n", un->un_f_rmw_type); 4214 } 4215 4216 /* 4217 * Validate the throttle values. 4218 * If any of the numbers are invalid, set everything to defaults. 4219 */ 4220 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4221 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4222 (un->un_min_throttle > un->un_throttle)) { 4223 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4224 un->un_min_throttle = sd_min_throttle; 4225 } 4226 4227 if (strcasecmp(name, "mmc-gesn-polling") == 0) { 4228 if (strcasecmp(value, "true") == 0) { 4229 un->un_f_mmc_gesn_polling = TRUE; 4230 } else if (strcasecmp(value, "false") == 0) { 4231 un->un_f_mmc_gesn_polling = FALSE; 4232 } else { 4233 goto value_invalid; 4234 } 4235 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4236 "mmc-gesn-polling set to %d\n", 4237 un->un_f_mmc_gesn_polling); 4238 } 4239 4240 return; 4241 4242 value_invalid: 4243 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4244 "value of prop %s is invalid\n", name); 4245 } 4246 4247 /* 4248 * Function: sd_get_tunables_from_conf() 4249 * 4250 * 4251 * This function reads the data list from the sd.conf file and pulls 4252 * the values that can have numeric values as arguments and places 4253 * the values in the appropriate sd_tunables member. 4254 * Since the order of the data list members varies across platforms 4255 * This function reads them from the data list in a platform specific 4256 * order and places them into the correct sd_tunable member that is 4257 * consistent across all platforms. 4258 */ 4259 static void 4260 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 4261 sd_tunables *values) 4262 { 4263 int i; 4264 int mask; 4265 4266 bzero(values, sizeof (sd_tunables)); 4267 4268 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4269 4270 mask = 1 << i; 4271 if (mask > flags) { 4272 break; 4273 } 4274 4275 switch (mask & flags) { 4276 case 0: /* This mask bit not set in flags */ 4277 continue; 4278 case SD_CONF_BSET_THROTTLE: 4279 values->sdt_throttle = data_list[i]; 4280 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4281 "sd_get_tunables_from_conf: throttle = %d\n", 4282 values->sdt_throttle); 4283 break; 4284 case SD_CONF_BSET_CTYPE: 4285 values->sdt_ctype = data_list[i]; 4286 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4287 "sd_get_tunables_from_conf: ctype = %d\n", 4288 values->sdt_ctype); 4289 break; 4290 case SD_CONF_BSET_NRR_COUNT: 4291 values->sdt_not_rdy_retries = data_list[i]; 4292 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4293 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 4294 values->sdt_not_rdy_retries); 4295 break; 4296 case SD_CONF_BSET_BSY_RETRY_COUNT: 4297 values->sdt_busy_retries = data_list[i]; 4298 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4299 "sd_get_tunables_from_conf: busy_retries = %d\n", 4300 values->sdt_busy_retries); 4301 break; 4302 case SD_CONF_BSET_RST_RETRIES: 4303 values->sdt_reset_retries = data_list[i]; 4304 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4305 "sd_get_tunables_from_conf: reset_retries = %d\n", 4306 values->sdt_reset_retries); 4307 break; 4308 case SD_CONF_BSET_RSV_REL_TIME: 4309 values->sdt_reserv_rel_time = data_list[i]; 4310 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4311 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 4312 values->sdt_reserv_rel_time); 4313 break; 4314 case SD_CONF_BSET_MIN_THROTTLE: 4315 values->sdt_min_throttle = data_list[i]; 4316 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4317 "sd_get_tunables_from_conf: min_throttle = %d\n", 4318 values->sdt_min_throttle); 4319 break; 4320 case SD_CONF_BSET_DISKSORT_DISABLED: 4321 values->sdt_disk_sort_dis = data_list[i]; 4322 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4323 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 4324 values->sdt_disk_sort_dis); 4325 break; 4326 case SD_CONF_BSET_LUN_RESET_ENABLED: 4327 values->sdt_lun_reset_enable = data_list[i]; 4328 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4329 "sd_get_tunables_from_conf: lun_reset_enable = %d" 4330 "\n", values->sdt_lun_reset_enable); 4331 break; 4332 case SD_CONF_BSET_CACHE_IS_NV: 4333 values->sdt_suppress_cache_flush = data_list[i]; 4334 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4335 "sd_get_tunables_from_conf: \ 4336 suppress_cache_flush = %d" 4337 "\n", values->sdt_suppress_cache_flush); 4338 break; 4339 case SD_CONF_BSET_PC_DISABLED: 4340 values->sdt_disk_sort_dis = data_list[i]; 4341 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4342 "sd_get_tunables_from_conf: power_condition_dis = " 4343 "%d\n", values->sdt_power_condition_dis); 4344 break; 4345 } 4346 } 4347 } 4348 4349 /* 4350 * Function: sd_process_sdconf_table 4351 * 4352 * Description: Search the static configuration table for a match on the 4353 * inquiry vid/pid and update the driver soft state structure 4354 * according to the table property values for the device. 4355 * 4356 * The form of a configuration table entry is: 4357 * <vid+pid>,<flags>,<property-data> 4358 * "SEAGATE ST42400N",1,0x40000, 4359 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 4360 * 4361 * Arguments: un - driver soft state (unit) structure 4362 */ 4363 4364 static void 4365 sd_process_sdconf_table(struct sd_lun *un) 4366 { 4367 char *id = NULL; 4368 int table_index; 4369 int idlen; 4370 4371 ASSERT(un != NULL); 4372 for (table_index = 0; table_index < sd_disk_table_size; 4373 table_index++) { 4374 id = sd_disk_table[table_index].device_id; 4375 idlen = strlen(id); 4376 if (idlen == 0) { 4377 continue; 4378 } 4379 4380 /* 4381 * The static configuration table currently does not 4382 * implement version 10 properties. Additionally, 4383 * multiple data-property-name entries are not 4384 * implemented in the static configuration table. 4385 */ 4386 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4387 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4388 "sd_process_sdconf_table: disk %s\n", id); 4389 sd_set_vers1_properties(un, 4390 sd_disk_table[table_index].flags, 4391 sd_disk_table[table_index].properties); 4392 break; 4393 } 4394 } 4395 } 4396 4397 4398 /* 4399 * Function: sd_sdconf_id_match 4400 * 4401 * Description: This local function implements a case sensitive vid/pid 4402 * comparison as well as the boundary cases of wild card and 4403 * multiple blanks. 4404 * 4405 * Note: An implicit assumption made here is that the scsi 4406 * inquiry structure will always keep the vid, pid and 4407 * revision strings in consecutive sequence, so they can be 4408 * read as a single string. If this assumption is not the 4409 * case, a separate string, to be used for the check, needs 4410 * to be built with these strings concatenated. 4411 * 4412 * Arguments: un - driver soft state (unit) structure 4413 * id - table or config file vid/pid 4414 * idlen - length of the vid/pid (bytes) 4415 * 4416 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4417 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4418 */ 4419 4420 static int 4421 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 4422 { 4423 struct scsi_inquiry *sd_inq; 4424 int rval = SD_SUCCESS; 4425 4426 ASSERT(un != NULL); 4427 sd_inq = un->un_sd->sd_inq; 4428 ASSERT(id != NULL); 4429 4430 /* 4431 * We use the inq_vid as a pointer to a buffer containing the 4432 * vid and pid and use the entire vid/pid length of the table 4433 * entry for the comparison. This works because the inq_pid 4434 * data member follows inq_vid in the scsi_inquiry structure. 4435 */ 4436 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 4437 /* 4438 * The user id string is compared to the inquiry vid/pid 4439 * using a case insensitive comparison and ignoring 4440 * multiple spaces. 4441 */ 4442 rval = sd_blank_cmp(un, id, idlen); 4443 if (rval != SD_SUCCESS) { 4444 /* 4445 * User id strings that start and end with a "*" 4446 * are a special case. These do not have a 4447 * specific vendor, and the product string can 4448 * appear anywhere in the 16 byte PID portion of 4449 * the inquiry data. This is a simple strstr() 4450 * type search for the user id in the inquiry data. 4451 */ 4452 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 4453 char *pidptr = &id[1]; 4454 int i; 4455 int j; 4456 int pidstrlen = idlen - 2; 4457 j = sizeof (SD_INQUIRY(un)->inq_pid) - 4458 pidstrlen; 4459 4460 if (j < 0) { 4461 return (SD_FAILURE); 4462 } 4463 for (i = 0; i < j; i++) { 4464 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 4465 pidptr, pidstrlen) == 0) { 4466 rval = SD_SUCCESS; 4467 break; 4468 } 4469 } 4470 } 4471 } 4472 } 4473 return (rval); 4474 } 4475 4476 4477 /* 4478 * Function: sd_blank_cmp 4479 * 4480 * Description: If the id string starts and ends with a space, treat 4481 * multiple consecutive spaces as equivalent to a single 4482 * space. For example, this causes a sd_disk_table entry 4483 * of " NEC CDROM " to match a device's id string of 4484 * "NEC CDROM". 4485 * 4486 * Note: The success exit condition for this routine is if 4487 * the pointer to the table entry is '\0' and the cnt of 4488 * the inquiry length is zero. This will happen if the inquiry 4489 * string returned by the device is padded with spaces to be 4490 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 4491 * SCSI spec states that the inquiry string is to be padded with 4492 * spaces. 4493 * 4494 * Arguments: un - driver soft state (unit) structure 4495 * id - table or config file vid/pid 4496 * idlen - length of the vid/pid (bytes) 4497 * 4498 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4499 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4500 */ 4501 4502 static int 4503 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 4504 { 4505 char *p1; 4506 char *p2; 4507 int cnt; 4508 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 4509 sizeof (SD_INQUIRY(un)->inq_pid); 4510 4511 ASSERT(un != NULL); 4512 p2 = un->un_sd->sd_inq->inq_vid; 4513 ASSERT(id != NULL); 4514 p1 = id; 4515 4516 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 4517 /* 4518 * Note: string p1 is terminated by a NUL but string p2 4519 * isn't. The end of p2 is determined by cnt. 4520 */ 4521 for (;;) { 4522 /* skip over any extra blanks in both strings */ 4523 while ((*p1 != '\0') && (*p1 == ' ')) { 4524 p1++; 4525 } 4526 while ((cnt != 0) && (*p2 == ' ')) { 4527 p2++; 4528 cnt--; 4529 } 4530 4531 /* compare the two strings */ 4532 if ((cnt == 0) || 4533 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 4534 break; 4535 } 4536 while ((cnt > 0) && 4537 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 4538 p1++; 4539 p2++; 4540 cnt--; 4541 } 4542 } 4543 } 4544 4545 /* return SD_SUCCESS if both strings match */ 4546 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 4547 } 4548 4549 4550 /* 4551 * Function: sd_chk_vers1_data 4552 * 4553 * Description: Verify the version 1 device properties provided by the 4554 * user via the configuration file 4555 * 4556 * Arguments: un - driver soft state (unit) structure 4557 * flags - integer mask indicating properties to be set 4558 * prop_list - integer list of property values 4559 * list_len - number of the elements 4560 * 4561 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4562 * SD_FAILURE - Indicates the user provided data is invalid 4563 */ 4564 4565 static int 4566 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4567 int list_len, char *dataname_ptr) 4568 { 4569 int i; 4570 int mask = 1; 4571 int index = 0; 4572 4573 ASSERT(un != NULL); 4574 4575 /* Check for a NULL property name and list */ 4576 if (dataname_ptr == NULL) { 4577 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4578 "sd_chk_vers1_data: NULL data property name."); 4579 return (SD_FAILURE); 4580 } 4581 if (prop_list == NULL) { 4582 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4583 "sd_chk_vers1_data: %s NULL data property list.", 4584 dataname_ptr); 4585 return (SD_FAILURE); 4586 } 4587 4588 /* Display a warning if undefined bits are set in the flags */ 4589 if (flags & ~SD_CONF_BIT_MASK) { 4590 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4591 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4592 "Properties not set.", 4593 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4594 return (SD_FAILURE); 4595 } 4596 4597 /* 4598 * Verify the length of the list by identifying the highest bit set 4599 * in the flags and validating that the property list has a length 4600 * up to the index of this bit. 4601 */ 4602 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4603 if (flags & mask) { 4604 index++; 4605 } 4606 mask = 1 << i; 4607 } 4608 if (list_len < (index + 2)) { 4609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4610 "sd_chk_vers1_data: " 4611 "Data property list %s size is incorrect. " 4612 "Properties not set.", dataname_ptr); 4613 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4614 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4615 return (SD_FAILURE); 4616 } 4617 return (SD_SUCCESS); 4618 } 4619 4620 4621 /* 4622 * Function: sd_set_vers1_properties 4623 * 4624 * Description: Set version 1 device properties based on a property list 4625 * retrieved from the driver configuration file or static 4626 * configuration table. Version 1 properties have the format: 4627 * 4628 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4629 * 4630 * where the prop0 value will be used to set prop0 if bit0 4631 * is set in the flags 4632 * 4633 * Arguments: un - driver soft state (unit) structure 4634 * flags - integer mask indicating properties to be set 4635 * prop_list - integer list of property values 4636 */ 4637 4638 static void 4639 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4640 { 4641 ASSERT(un != NULL); 4642 4643 /* 4644 * Set the flag to indicate cache is to be disabled. An attempt 4645 * to disable the cache via sd_cache_control() will be made 4646 * later during attach once the basic initialization is complete. 4647 */ 4648 if (flags & SD_CONF_BSET_NOCACHE) { 4649 un->un_f_opt_disable_cache = TRUE; 4650 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4651 "sd_set_vers1_properties: caching disabled flag set\n"); 4652 } 4653 4654 /* CD-specific configuration parameters */ 4655 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4656 un->un_f_cfg_playmsf_bcd = TRUE; 4657 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4658 "sd_set_vers1_properties: playmsf_bcd set\n"); 4659 } 4660 if (flags & SD_CONF_BSET_READSUB_BCD) { 4661 un->un_f_cfg_readsub_bcd = TRUE; 4662 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4663 "sd_set_vers1_properties: readsub_bcd set\n"); 4664 } 4665 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4666 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4667 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4668 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4669 } 4670 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4671 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4672 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4673 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4674 } 4675 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4676 un->un_f_cfg_no_read_header = TRUE; 4677 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4678 "sd_set_vers1_properties: no_read_header set\n"); 4679 } 4680 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4681 un->un_f_cfg_read_cd_xd4 = TRUE; 4682 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4683 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4684 } 4685 4686 /* Support for devices which do not have valid/unique serial numbers */ 4687 if (flags & SD_CONF_BSET_FAB_DEVID) { 4688 un->un_f_opt_fab_devid = TRUE; 4689 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4690 "sd_set_vers1_properties: fab_devid bit set\n"); 4691 } 4692 4693 /* Support for user throttle configuration */ 4694 if (flags & SD_CONF_BSET_THROTTLE) { 4695 ASSERT(prop_list != NULL); 4696 un->un_saved_throttle = un->un_throttle = 4697 prop_list->sdt_throttle; 4698 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4699 "sd_set_vers1_properties: throttle set to %d\n", 4700 prop_list->sdt_throttle); 4701 } 4702 4703 /* Set the per disk retry count according to the conf file or table. */ 4704 if (flags & SD_CONF_BSET_NRR_COUNT) { 4705 ASSERT(prop_list != NULL); 4706 if (prop_list->sdt_not_rdy_retries) { 4707 un->un_notready_retry_count = 4708 prop_list->sdt_not_rdy_retries; 4709 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4710 "sd_set_vers1_properties: not ready retry count" 4711 " set to %d\n", un->un_notready_retry_count); 4712 } 4713 } 4714 4715 /* The controller type is reported for generic disk driver ioctls */ 4716 if (flags & SD_CONF_BSET_CTYPE) { 4717 ASSERT(prop_list != NULL); 4718 switch (prop_list->sdt_ctype) { 4719 case CTYPE_CDROM: 4720 un->un_ctype = prop_list->sdt_ctype; 4721 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4722 "sd_set_vers1_properties: ctype set to " 4723 "CTYPE_CDROM\n"); 4724 break; 4725 case CTYPE_CCS: 4726 un->un_ctype = prop_list->sdt_ctype; 4727 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4728 "sd_set_vers1_properties: ctype set to " 4729 "CTYPE_CCS\n"); 4730 break; 4731 case CTYPE_ROD: /* RW optical */ 4732 un->un_ctype = prop_list->sdt_ctype; 4733 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4734 "sd_set_vers1_properties: ctype set to " 4735 "CTYPE_ROD\n"); 4736 break; 4737 default: 4738 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4739 "sd_set_vers1_properties: Could not set " 4740 "invalid ctype value (%d)", 4741 prop_list->sdt_ctype); 4742 } 4743 } 4744 4745 /* Purple failover timeout */ 4746 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4747 ASSERT(prop_list != NULL); 4748 un->un_busy_retry_count = 4749 prop_list->sdt_busy_retries; 4750 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4751 "sd_set_vers1_properties: " 4752 "busy retry count set to %d\n", 4753 un->un_busy_retry_count); 4754 } 4755 4756 /* Purple reset retry count */ 4757 if (flags & SD_CONF_BSET_RST_RETRIES) { 4758 ASSERT(prop_list != NULL); 4759 un->un_reset_retry_count = 4760 prop_list->sdt_reset_retries; 4761 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4762 "sd_set_vers1_properties: " 4763 "reset retry count set to %d\n", 4764 un->un_reset_retry_count); 4765 } 4766 4767 /* Purple reservation release timeout */ 4768 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4769 ASSERT(prop_list != NULL); 4770 un->un_reserve_release_time = 4771 prop_list->sdt_reserv_rel_time; 4772 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4773 "sd_set_vers1_properties: " 4774 "reservation release timeout set to %d\n", 4775 un->un_reserve_release_time); 4776 } 4777 4778 /* 4779 * Driver flag telling the driver to verify that no commands are pending 4780 * for a device before issuing a Test Unit Ready. This is a workaround 4781 * for a firmware bug in some Seagate eliteI drives. 4782 */ 4783 if (flags & SD_CONF_BSET_TUR_CHECK) { 4784 un->un_f_cfg_tur_check = TRUE; 4785 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4786 "sd_set_vers1_properties: tur queue check set\n"); 4787 } 4788 4789 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4790 un->un_min_throttle = prop_list->sdt_min_throttle; 4791 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4792 "sd_set_vers1_properties: min throttle set to %d\n", 4793 un->un_min_throttle); 4794 } 4795 4796 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4797 un->un_f_disksort_disabled = 4798 (prop_list->sdt_disk_sort_dis != 0) ? 4799 TRUE : FALSE; 4800 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4801 "sd_set_vers1_properties: disksort disabled " 4802 "flag set to %d\n", 4803 prop_list->sdt_disk_sort_dis); 4804 } 4805 4806 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4807 un->un_f_lun_reset_enabled = 4808 (prop_list->sdt_lun_reset_enable != 0) ? 4809 TRUE : FALSE; 4810 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4811 "sd_set_vers1_properties: lun reset enabled " 4812 "flag set to %d\n", 4813 prop_list->sdt_lun_reset_enable); 4814 } 4815 4816 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4817 un->un_f_suppress_cache_flush = 4818 (prop_list->sdt_suppress_cache_flush != 0) ? 4819 TRUE : FALSE; 4820 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4821 "sd_set_vers1_properties: suppress_cache_flush " 4822 "flag set to %d\n", 4823 prop_list->sdt_suppress_cache_flush); 4824 } 4825 4826 if (flags & SD_CONF_BSET_PC_DISABLED) { 4827 un->un_f_power_condition_disabled = 4828 (prop_list->sdt_power_condition_dis != 0) ? 4829 TRUE : FALSE; 4830 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4831 "sd_set_vers1_properties: power_condition_disabled " 4832 "flag set to %d\n", 4833 prop_list->sdt_power_condition_dis); 4834 } 4835 4836 /* 4837 * Validate the throttle values. 4838 * If any of the numbers are invalid, set everything to defaults. 4839 */ 4840 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4841 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4842 (un->un_min_throttle > un->un_throttle)) { 4843 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4844 un->un_min_throttle = sd_min_throttle; 4845 } 4846 } 4847 4848 /* 4849 * Function: sd_is_lsi() 4850 * 4851 * Description: Check for lsi devices, step through the static device 4852 * table to match vid/pid. 4853 * 4854 * Args: un - ptr to sd_lun 4855 * 4856 * Notes: When creating new LSI property, need to add the new LSI property 4857 * to this function. 4858 */ 4859 static void 4860 sd_is_lsi(struct sd_lun *un) 4861 { 4862 char *id = NULL; 4863 int table_index; 4864 int idlen; 4865 void *prop; 4866 4867 ASSERT(un != NULL); 4868 for (table_index = 0; table_index < sd_disk_table_size; 4869 table_index++) { 4870 id = sd_disk_table[table_index].device_id; 4871 idlen = strlen(id); 4872 if (idlen == 0) { 4873 continue; 4874 } 4875 4876 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4877 prop = sd_disk_table[table_index].properties; 4878 if (prop == &lsi_properties || 4879 prop == &lsi_oem_properties || 4880 prop == &lsi_properties_scsi || 4881 prop == &symbios_properties) { 4882 un->un_f_cfg_is_lsi = TRUE; 4883 } 4884 break; 4885 } 4886 } 4887 } 4888 4889 /* 4890 * Function: sd_get_physical_geometry 4891 * 4892 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4893 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4894 * target, and use this information to initialize the physical 4895 * geometry cache specified by pgeom_p. 4896 * 4897 * MODE SENSE is an optional command, so failure in this case 4898 * does not necessarily denote an error. We want to use the 4899 * MODE SENSE commands to derive the physical geometry of the 4900 * device, but if either command fails, the logical geometry is 4901 * used as the fallback for disk label geometry in cmlb. 4902 * 4903 * This requires that un->un_blockcount and un->un_tgt_blocksize 4904 * have already been initialized for the current target and 4905 * that the current values be passed as args so that we don't 4906 * end up ever trying to use -1 as a valid value. This could 4907 * happen if either value is reset while we're not holding 4908 * the mutex. 4909 * 4910 * Arguments: un - driver soft state (unit) structure 4911 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4912 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4913 * to use the USCSI "direct" chain and bypass the normal 4914 * command waitq. 4915 * 4916 * Context: Kernel thread only (can sleep). 4917 */ 4918 4919 static int 4920 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4921 diskaddr_t capacity, int lbasize, int path_flag) 4922 { 4923 struct mode_format *page3p; 4924 struct mode_geometry *page4p; 4925 struct mode_header *headerp; 4926 int sector_size; 4927 int nsect; 4928 int nhead; 4929 int ncyl; 4930 int intrlv; 4931 int spc; 4932 diskaddr_t modesense_capacity; 4933 int rpm; 4934 int bd_len; 4935 int mode_header_length; 4936 uchar_t *p3bufp; 4937 uchar_t *p4bufp; 4938 int cdbsize; 4939 int ret = EIO; 4940 sd_ssc_t *ssc; 4941 int status; 4942 4943 ASSERT(un != NULL); 4944 4945 if (lbasize == 0) { 4946 if (ISCD(un)) { 4947 lbasize = 2048; 4948 } else { 4949 lbasize = un->un_sys_blocksize; 4950 } 4951 } 4952 pgeom_p->g_secsize = (unsigned short)lbasize; 4953 4954 /* 4955 * If the unit is a cd/dvd drive MODE SENSE page three 4956 * and MODE SENSE page four are reserved (see SBC spec 4957 * and MMC spec). To prevent soft errors just return 4958 * using the default LBA size. 4959 */ 4960 if (ISCD(un)) 4961 return (ret); 4962 4963 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4964 4965 /* 4966 * Retrieve MODE SENSE page 3 - Format Device Page 4967 */ 4968 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4969 ssc = sd_ssc_init(un); 4970 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp, 4971 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag); 4972 if (status != 0) { 4973 SD_ERROR(SD_LOG_COMMON, un, 4974 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4975 goto page3_exit; 4976 } 4977 4978 /* 4979 * Determine size of Block Descriptors in order to locate the mode 4980 * page data. ATAPI devices return 0, SCSI devices should return 4981 * MODE_BLK_DESC_LENGTH. 4982 */ 4983 headerp = (struct mode_header *)p3bufp; 4984 if (un->un_f_cfg_is_atapi == TRUE) { 4985 struct mode_header_grp2 *mhp = 4986 (struct mode_header_grp2 *)headerp; 4987 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4988 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4989 } else { 4990 mode_header_length = MODE_HEADER_LENGTH; 4991 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4992 } 4993 4994 if (bd_len > MODE_BLK_DESC_LENGTH) { 4995 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 4996 "sd_get_physical_geometry: received unexpected bd_len " 4997 "of %d, page3\n", bd_len); 4998 status = EIO; 4999 goto page3_exit; 5000 } 5001 5002 page3p = (struct mode_format *) 5003 ((caddr_t)headerp + mode_header_length + bd_len); 5004 5005 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5006 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5007 "sd_get_physical_geometry: mode sense pg3 code mismatch " 5008 "%d\n", page3p->mode_page.code); 5009 status = EIO; 5010 goto page3_exit; 5011 } 5012 5013 /* 5014 * Use this physical geometry data only if BOTH MODE SENSE commands 5015 * complete successfully; otherwise, revert to the logical geometry. 5016 * So, we need to save everything in temporary variables. 5017 */ 5018 sector_size = BE_16(page3p->data_bytes_sect); 5019 5020 /* 5021 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5022 */ 5023 if (sector_size == 0) { 5024 sector_size = un->un_sys_blocksize; 5025 } else { 5026 sector_size &= ~(un->un_sys_blocksize - 1); 5027 } 5028 5029 nsect = BE_16(page3p->sect_track); 5030 intrlv = BE_16(page3p->interleave); 5031 5032 SD_INFO(SD_LOG_COMMON, un, 5033 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5034 SD_INFO(SD_LOG_COMMON, un, 5035 " mode page: %d; nsect: %d; sector size: %d;\n", 5036 page3p->mode_page.code, nsect, sector_size); 5037 SD_INFO(SD_LOG_COMMON, un, 5038 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5039 BE_16(page3p->track_skew), 5040 BE_16(page3p->cylinder_skew)); 5041 5042 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5043 5044 /* 5045 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5046 */ 5047 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5048 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp, 5049 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag); 5050 if (status != 0) { 5051 SD_ERROR(SD_LOG_COMMON, un, 5052 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5053 goto page4_exit; 5054 } 5055 5056 /* 5057 * Determine size of Block Descriptors in order to locate the mode 5058 * page data. ATAPI devices return 0, SCSI devices should return 5059 * MODE_BLK_DESC_LENGTH. 5060 */ 5061 headerp = (struct mode_header *)p4bufp; 5062 if (un->un_f_cfg_is_atapi == TRUE) { 5063 struct mode_header_grp2 *mhp = 5064 (struct mode_header_grp2 *)headerp; 5065 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5066 } else { 5067 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5068 } 5069 5070 if (bd_len > MODE_BLK_DESC_LENGTH) { 5071 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5072 "sd_get_physical_geometry: received unexpected bd_len of " 5073 "%d, page4\n", bd_len); 5074 status = EIO; 5075 goto page4_exit; 5076 } 5077 5078 page4p = (struct mode_geometry *) 5079 ((caddr_t)headerp + mode_header_length + bd_len); 5080 5081 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5082 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5083 "sd_get_physical_geometry: mode sense pg4 code mismatch " 5084 "%d\n", page4p->mode_page.code); 5085 status = EIO; 5086 goto page4_exit; 5087 } 5088 5089 /* 5090 * Stash the data now, after we know that both commands completed. 5091 */ 5092 5093 5094 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5095 spc = nhead * nsect; 5096 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5097 rpm = BE_16(page4p->rpm); 5098 5099 modesense_capacity = spc * ncyl; 5100 5101 SD_INFO(SD_LOG_COMMON, un, 5102 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5103 SD_INFO(SD_LOG_COMMON, un, 5104 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5105 SD_INFO(SD_LOG_COMMON, un, 5106 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5107 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5108 (void *)pgeom_p, capacity); 5109 5110 /* 5111 * Compensate if the drive's geometry is not rectangular, i.e., 5112 * the product of C * H * S returned by MODE SENSE >= that returned 5113 * by read capacity. This is an idiosyncrasy of the original x86 5114 * disk subsystem. 5115 */ 5116 if (modesense_capacity >= capacity) { 5117 SD_INFO(SD_LOG_COMMON, un, 5118 "sd_get_physical_geometry: adjusting acyl; " 5119 "old: %d; new: %d\n", pgeom_p->g_acyl, 5120 (modesense_capacity - capacity + spc - 1) / spc); 5121 if (sector_size != 0) { 5122 /* 1243403: NEC D38x7 drives don't support sec size */ 5123 pgeom_p->g_secsize = (unsigned short)sector_size; 5124 } 5125 pgeom_p->g_nsect = (unsigned short)nsect; 5126 pgeom_p->g_nhead = (unsigned short)nhead; 5127 pgeom_p->g_capacity = capacity; 5128 pgeom_p->g_acyl = 5129 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5130 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5131 } 5132 5133 pgeom_p->g_rpm = (unsigned short)rpm; 5134 pgeom_p->g_intrlv = (unsigned short)intrlv; 5135 ret = 0; 5136 5137 SD_INFO(SD_LOG_COMMON, un, 5138 "sd_get_physical_geometry: mode sense geometry:\n"); 5139 SD_INFO(SD_LOG_COMMON, un, 5140 " nsect: %d; sector size: %d; interlv: %d\n", 5141 nsect, sector_size, intrlv); 5142 SD_INFO(SD_LOG_COMMON, un, 5143 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5144 nhead, ncyl, rpm, modesense_capacity); 5145 SD_INFO(SD_LOG_COMMON, un, 5146 "sd_get_physical_geometry: (cached)\n"); 5147 SD_INFO(SD_LOG_COMMON, un, 5148 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5149 pgeom_p->g_ncyl, pgeom_p->g_acyl, 5150 pgeom_p->g_nhead, pgeom_p->g_nsect); 5151 SD_INFO(SD_LOG_COMMON, un, 5152 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5153 pgeom_p->g_secsize, pgeom_p->g_capacity, 5154 pgeom_p->g_intrlv, pgeom_p->g_rpm); 5155 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5156 5157 page4_exit: 5158 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5159 5160 page3_exit: 5161 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5162 5163 if (status != 0) { 5164 if (status == EIO) { 5165 /* 5166 * Some disks do not support mode sense(6), we 5167 * should ignore this kind of error(sense key is 5168 * 0x5 - illegal request). 5169 */ 5170 uint8_t *sensep; 5171 int senlen; 5172 5173 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 5174 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 5175 ssc->ssc_uscsi_cmd->uscsi_rqresid); 5176 5177 if (senlen > 0 && 5178 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 5179 sd_ssc_assessment(ssc, 5180 SD_FMT_IGNORE_COMPROMISE); 5181 } else { 5182 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 5183 } 5184 } else { 5185 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5186 } 5187 } 5188 sd_ssc_fini(ssc); 5189 return (ret); 5190 } 5191 5192 /* 5193 * Function: sd_get_virtual_geometry 5194 * 5195 * Description: Ask the controller to tell us about the target device. 5196 * 5197 * Arguments: un - pointer to softstate 5198 * capacity - disk capacity in #blocks 5199 * lbasize - disk block size in bytes 5200 * 5201 * Context: Kernel thread only 5202 */ 5203 5204 static int 5205 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 5206 diskaddr_t capacity, int lbasize) 5207 { 5208 uint_t geombuf; 5209 int spc; 5210 5211 ASSERT(un != NULL); 5212 5213 /* Set sector size, and total number of sectors */ 5214 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5215 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5216 5217 /* Let the HBA tell us its geometry */ 5218 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5219 5220 /* A value of -1 indicates an undefined "geometry" property */ 5221 if (geombuf == (-1)) { 5222 return (EINVAL); 5223 } 5224 5225 /* Initialize the logical geometry cache. */ 5226 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5227 lgeom_p->g_nsect = geombuf & 0xffff; 5228 lgeom_p->g_secsize = un->un_sys_blocksize; 5229 5230 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5231 5232 /* 5233 * Note: The driver originally converted the capacity value from 5234 * target blocks to system blocks. However, the capacity value passed 5235 * to this routine is already in terms of system blocks (this scaling 5236 * is done when the READ CAPACITY command is issued and processed). 5237 * This 'error' may have gone undetected because the usage of g_ncyl 5238 * (which is based upon g_capacity) is very limited within the driver 5239 */ 5240 lgeom_p->g_capacity = capacity; 5241 5242 /* 5243 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5244 * hba may return zero values if the device has been removed. 5245 */ 5246 if (spc == 0) { 5247 lgeom_p->g_ncyl = 0; 5248 } else { 5249 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5250 } 5251 lgeom_p->g_acyl = 0; 5252 5253 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5254 return (0); 5255 5256 } 5257 /* 5258 * Function: sd_update_block_info 5259 * 5260 * Description: Calculate a byte count to sector count bitshift value 5261 * from sector size. 5262 * 5263 * Arguments: un: unit struct. 5264 * lbasize: new target sector size 5265 * capacity: new target capacity, ie. block count 5266 * 5267 * Context: Kernel thread context 5268 */ 5269 5270 static void 5271 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5272 { 5273 if (lbasize != 0) { 5274 un->un_tgt_blocksize = lbasize; 5275 un->un_f_tgt_blocksize_is_valid = TRUE; 5276 if (!un->un_f_has_removable_media) { 5277 un->un_sys_blocksize = lbasize; 5278 } 5279 } 5280 5281 if (capacity != 0) { 5282 un->un_blockcount = capacity; 5283 un->un_f_blockcount_is_valid = TRUE; 5284 } 5285 } 5286 5287 5288 /* 5289 * Function: sd_register_devid 5290 * 5291 * Description: This routine will obtain the device id information from the 5292 * target, obtain the serial number, and register the device 5293 * id with the ddi framework. 5294 * 5295 * Arguments: devi - the system's dev_info_t for the device. 5296 * un - driver soft state (unit) structure 5297 * reservation_flag - indicates if a reservation conflict 5298 * occurred during attach 5299 * 5300 * Context: Kernel Thread 5301 */ 5302 static void 5303 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag) 5304 { 5305 int rval = 0; 5306 uchar_t *inq80 = NULL; 5307 size_t inq80_len = MAX_INQUIRY_SIZE; 5308 size_t inq80_resid = 0; 5309 uchar_t *inq83 = NULL; 5310 size_t inq83_len = MAX_INQUIRY_SIZE; 5311 size_t inq83_resid = 0; 5312 int dlen, len; 5313 char *sn; 5314 struct sd_lun *un; 5315 5316 ASSERT(ssc != NULL); 5317 un = ssc->ssc_un; 5318 ASSERT(un != NULL); 5319 ASSERT(mutex_owned(SD_MUTEX(un))); 5320 ASSERT((SD_DEVINFO(un)) == devi); 5321 5322 5323 /* 5324 * We check the availability of the World Wide Name (0x83) and Unit 5325 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5326 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5327 * 0x83 is available, that is the best choice. Our next choice is 5328 * 0x80. If neither are available, we munge the devid from the device 5329 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5330 * to fabricate a devid for non-Sun qualified disks. 5331 */ 5332 if (sd_check_vpd_page_support(ssc) == 0) { 5333 /* collect page 80 data if available */ 5334 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5335 5336 mutex_exit(SD_MUTEX(un)); 5337 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5338 5339 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len, 5340 0x01, 0x80, &inq80_resid); 5341 5342 if (rval != 0) { 5343 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5344 kmem_free(inq80, inq80_len); 5345 inq80 = NULL; 5346 inq80_len = 0; 5347 } else if (ddi_prop_exists( 5348 DDI_DEV_T_NONE, SD_DEVINFO(un), 5349 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 5350 INQUIRY_SERIAL_NO) == 0) { 5351 /* 5352 * If we don't already have a serial number 5353 * property, do quick verify of data returned 5354 * and define property. 5355 */ 5356 dlen = inq80_len - inq80_resid; 5357 len = (size_t)inq80[3]; 5358 if ((dlen >= 4) && ((len + 4) <= dlen)) { 5359 /* 5360 * Ensure sn termination, skip leading 5361 * blanks, and create property 5362 * 'inquiry-serial-no'. 5363 */ 5364 sn = (char *)&inq80[4]; 5365 sn[len] = 0; 5366 while (*sn && (*sn == ' ')) 5367 sn++; 5368 if (*sn) { 5369 (void) ddi_prop_update_string( 5370 DDI_DEV_T_NONE, 5371 SD_DEVINFO(un), 5372 INQUIRY_SERIAL_NO, sn); 5373 } 5374 } 5375 } 5376 mutex_enter(SD_MUTEX(un)); 5377 } 5378 5379 /* collect page 83 data if available */ 5380 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5381 mutex_exit(SD_MUTEX(un)); 5382 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5383 5384 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len, 5385 0x01, 0x83, &inq83_resid); 5386 5387 if (rval != 0) { 5388 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5389 kmem_free(inq83, inq83_len); 5390 inq83 = NULL; 5391 inq83_len = 0; 5392 } 5393 mutex_enter(SD_MUTEX(un)); 5394 } 5395 } 5396 5397 /* 5398 * If transport has already registered a devid for this target 5399 * then that takes precedence over the driver's determination 5400 * of the devid. 5401 * 5402 * NOTE: The reason this check is done here instead of at the beginning 5403 * of the function is to allow the code above to create the 5404 * 'inquiry-serial-no' property. 5405 */ 5406 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 5407 ASSERT(un->un_devid); 5408 un->un_f_devid_transport_defined = TRUE; 5409 goto cleanup; /* use devid registered by the transport */ 5410 } 5411 5412 /* 5413 * This is the case of antiquated Sun disk drives that have the 5414 * FAB_DEVID property set in the disk_table. These drives 5415 * manage the devid's by storing them in last 2 available sectors 5416 * on the drive and have them fabricated by the ddi layer by calling 5417 * ddi_devid_init and passing the DEVID_FAB flag. 5418 */ 5419 if (un->un_f_opt_fab_devid == TRUE) { 5420 /* 5421 * Depending on EINVAL isn't reliable, since a reserved disk 5422 * may result in invalid geometry, so check to make sure a 5423 * reservation conflict did not occur during attach. 5424 */ 5425 if ((sd_get_devid(ssc) == EINVAL) && 5426 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5427 /* 5428 * The devid is invalid AND there is no reservation 5429 * conflict. Fabricate a new devid. 5430 */ 5431 (void) sd_create_devid(ssc); 5432 } 5433 5434 /* Register the devid if it exists */ 5435 if (un->un_devid != NULL) { 5436 (void) ddi_devid_register(SD_DEVINFO(un), 5437 un->un_devid); 5438 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5439 "sd_register_devid: Devid Fabricated\n"); 5440 } 5441 goto cleanup; 5442 } 5443 5444 /* encode best devid possible based on data available */ 5445 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 5446 (char *)ddi_driver_name(SD_DEVINFO(un)), 5447 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 5448 inq80, inq80_len - inq80_resid, inq83, inq83_len - 5449 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 5450 5451 /* devid successfully encoded, register devid */ 5452 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 5453 5454 } else { 5455 /* 5456 * Unable to encode a devid based on data available. 5457 * This is not a Sun qualified disk. Older Sun disk 5458 * drives that have the SD_FAB_DEVID property 5459 * set in the disk_table and non Sun qualified 5460 * disks are treated in the same manner. These 5461 * drives manage the devid's by storing them in 5462 * last 2 available sectors on the drive and 5463 * have them fabricated by the ddi layer by 5464 * calling ddi_devid_init and passing the 5465 * DEVID_FAB flag. 5466 * Create a fabricate devid only if there's no 5467 * fabricate devid existed. 5468 */ 5469 if (sd_get_devid(ssc) == EINVAL) { 5470 (void) sd_create_devid(ssc); 5471 } 5472 un->un_f_opt_fab_devid = TRUE; 5473 5474 /* Register the devid if it exists */ 5475 if (un->un_devid != NULL) { 5476 (void) ddi_devid_register(SD_DEVINFO(un), 5477 un->un_devid); 5478 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5479 "sd_register_devid: devid fabricated using " 5480 "ddi framework\n"); 5481 } 5482 } 5483 5484 cleanup: 5485 /* clean up resources */ 5486 if (inq80 != NULL) { 5487 kmem_free(inq80, inq80_len); 5488 } 5489 if (inq83 != NULL) { 5490 kmem_free(inq83, inq83_len); 5491 } 5492 } 5493 5494 5495 5496 /* 5497 * Function: sd_get_devid 5498 * 5499 * Description: This routine will return 0 if a valid device id has been 5500 * obtained from the target and stored in the soft state. If a 5501 * valid device id has not been previously read and stored, a 5502 * read attempt will be made. 5503 * 5504 * Arguments: un - driver soft state (unit) structure 5505 * 5506 * Return Code: 0 if we successfully get the device id 5507 * 5508 * Context: Kernel Thread 5509 */ 5510 5511 static int 5512 sd_get_devid(sd_ssc_t *ssc) 5513 { 5514 struct dk_devid *dkdevid; 5515 ddi_devid_t tmpid; 5516 uint_t *ip; 5517 size_t sz; 5518 diskaddr_t blk; 5519 int status; 5520 int chksum; 5521 int i; 5522 size_t buffer_size; 5523 struct sd_lun *un; 5524 5525 ASSERT(ssc != NULL); 5526 un = ssc->ssc_un; 5527 ASSERT(un != NULL); 5528 ASSERT(mutex_owned(SD_MUTEX(un))); 5529 5530 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 5531 un); 5532 5533 if (un->un_devid != NULL) { 5534 return (0); 5535 } 5536 5537 mutex_exit(SD_MUTEX(un)); 5538 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5539 (void *)SD_PATH_DIRECT) != 0) { 5540 mutex_enter(SD_MUTEX(un)); 5541 return (EINVAL); 5542 } 5543 5544 /* 5545 * Read and verify device id, stored in the reserved cylinders at the 5546 * end of the disk. Backup label is on the odd sectors of the last 5547 * track of the last cylinder. Device id will be on track of the next 5548 * to last cylinder. 5549 */ 5550 mutex_enter(SD_MUTEX(un)); 5551 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 5552 mutex_exit(SD_MUTEX(un)); 5553 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 5554 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk, 5555 SD_PATH_DIRECT); 5556 5557 if (status != 0) { 5558 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5559 goto error; 5560 } 5561 5562 /* Validate the revision */ 5563 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 5564 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 5565 status = EINVAL; 5566 goto error; 5567 } 5568 5569 /* Calculate the checksum */ 5570 chksum = 0; 5571 ip = (uint_t *)dkdevid; 5572 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5573 i++) { 5574 chksum ^= ip[i]; 5575 } 5576 5577 /* Compare the checksums */ 5578 if (DKD_GETCHKSUM(dkdevid) != chksum) { 5579 status = EINVAL; 5580 goto error; 5581 } 5582 5583 /* Validate the device id */ 5584 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 5585 status = EINVAL; 5586 goto error; 5587 } 5588 5589 /* 5590 * Store the device id in the driver soft state 5591 */ 5592 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 5593 tmpid = kmem_alloc(sz, KM_SLEEP); 5594 5595 mutex_enter(SD_MUTEX(un)); 5596 5597 un->un_devid = tmpid; 5598 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 5599 5600 kmem_free(dkdevid, buffer_size); 5601 5602 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 5603 5604 return (status); 5605 error: 5606 mutex_enter(SD_MUTEX(un)); 5607 kmem_free(dkdevid, buffer_size); 5608 return (status); 5609 } 5610 5611 5612 /* 5613 * Function: sd_create_devid 5614 * 5615 * Description: This routine will fabricate the device id and write it 5616 * to the disk. 5617 * 5618 * Arguments: un - driver soft state (unit) structure 5619 * 5620 * Return Code: value of the fabricated device id 5621 * 5622 * Context: Kernel Thread 5623 */ 5624 5625 static ddi_devid_t 5626 sd_create_devid(sd_ssc_t *ssc) 5627 { 5628 struct sd_lun *un; 5629 5630 ASSERT(ssc != NULL); 5631 un = ssc->ssc_un; 5632 ASSERT(un != NULL); 5633 5634 /* Fabricate the devid */ 5635 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 5636 == DDI_FAILURE) { 5637 return (NULL); 5638 } 5639 5640 /* Write the devid to disk */ 5641 if (sd_write_deviceid(ssc) != 0) { 5642 ddi_devid_free(un->un_devid); 5643 un->un_devid = NULL; 5644 } 5645 5646 return (un->un_devid); 5647 } 5648 5649 5650 /* 5651 * Function: sd_write_deviceid 5652 * 5653 * Description: This routine will write the device id to the disk 5654 * reserved sector. 5655 * 5656 * Arguments: un - driver soft state (unit) structure 5657 * 5658 * Return Code: EINVAL 5659 * value returned by sd_send_scsi_cmd 5660 * 5661 * Context: Kernel Thread 5662 */ 5663 5664 static int 5665 sd_write_deviceid(sd_ssc_t *ssc) 5666 { 5667 struct dk_devid *dkdevid; 5668 uchar_t *buf; 5669 diskaddr_t blk; 5670 uint_t *ip, chksum; 5671 int status; 5672 int i; 5673 struct sd_lun *un; 5674 5675 ASSERT(ssc != NULL); 5676 un = ssc->ssc_un; 5677 ASSERT(un != NULL); 5678 ASSERT(mutex_owned(SD_MUTEX(un))); 5679 5680 mutex_exit(SD_MUTEX(un)); 5681 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5682 (void *)SD_PATH_DIRECT) != 0) { 5683 mutex_enter(SD_MUTEX(un)); 5684 return (-1); 5685 } 5686 5687 5688 /* Allocate the buffer */ 5689 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 5690 dkdevid = (struct dk_devid *)buf; 5691 5692 /* Fill in the revision */ 5693 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 5694 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 5695 5696 /* Copy in the device id */ 5697 mutex_enter(SD_MUTEX(un)); 5698 bcopy(un->un_devid, &dkdevid->dkd_devid, 5699 ddi_devid_sizeof(un->un_devid)); 5700 mutex_exit(SD_MUTEX(un)); 5701 5702 /* Calculate the checksum */ 5703 chksum = 0; 5704 ip = (uint_t *)dkdevid; 5705 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5706 i++) { 5707 chksum ^= ip[i]; 5708 } 5709 5710 /* Fill-in checksum */ 5711 DKD_FORMCHKSUM(chksum, dkdevid); 5712 5713 /* Write the reserved sector */ 5714 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk, 5715 SD_PATH_DIRECT); 5716 if (status != 0) 5717 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5718 5719 kmem_free(buf, un->un_sys_blocksize); 5720 5721 mutex_enter(SD_MUTEX(un)); 5722 return (status); 5723 } 5724 5725 5726 /* 5727 * Function: sd_check_vpd_page_support 5728 * 5729 * Description: This routine sends an inquiry command with the EVPD bit set and 5730 * a page code of 0x00 to the device. It is used to determine which 5731 * vital product pages are available to find the devid. We are 5732 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1, 5733 * the device does not support that command. 5734 * 5735 * Arguments: un - driver soft state (unit) structure 5736 * 5737 * Return Code: 0 - success 5738 * 1 - check condition 5739 * 5740 * Context: This routine can sleep. 5741 */ 5742 5743 static int 5744 sd_check_vpd_page_support(sd_ssc_t *ssc) 5745 { 5746 uchar_t *page_list = NULL; 5747 uchar_t page_length = 0xff; /* Use max possible length */ 5748 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5749 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5750 int rval = 0; 5751 int counter; 5752 struct sd_lun *un; 5753 5754 ASSERT(ssc != NULL); 5755 un = ssc->ssc_un; 5756 ASSERT(un != NULL); 5757 ASSERT(mutex_owned(SD_MUTEX(un))); 5758 5759 mutex_exit(SD_MUTEX(un)); 5760 5761 /* 5762 * We'll set the page length to the maximum to save figuring it out 5763 * with an additional call. 5764 */ 5765 page_list = kmem_zalloc(page_length, KM_SLEEP); 5766 5767 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd, 5768 page_code, NULL); 5769 5770 if (rval != 0) 5771 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5772 5773 mutex_enter(SD_MUTEX(un)); 5774 5775 /* 5776 * Now we must validate that the device accepted the command, as some 5777 * drives do not support it. If the drive does support it, we will 5778 * return 0, and the supported pages will be in un_vpd_page_mask. If 5779 * not, we return -1. 5780 */ 5781 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5782 /* Loop to find one of the 2 pages we need */ 5783 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5784 5785 /* 5786 * Pages are returned in ascending order, and 0x83 is what we 5787 * are hoping for. 5788 */ 5789 while ((page_list[counter] <= 0xB1) && 5790 (counter <= (page_list[VPD_PAGE_LENGTH] + 5791 VPD_HEAD_OFFSET))) { 5792 /* 5793 * Add 3 because page_list[3] is the number of 5794 * pages minus 3 5795 */ 5796 5797 switch (page_list[counter]) { 5798 case 0x00: 5799 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5800 break; 5801 case 0x80: 5802 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5803 break; 5804 case 0x81: 5805 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5806 break; 5807 case 0x82: 5808 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5809 break; 5810 case 0x83: 5811 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5812 break; 5813 case 0x86: 5814 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5815 break; 5816 case 0xB1: 5817 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG; 5818 break; 5819 } 5820 counter++; 5821 } 5822 5823 } else { 5824 rval = -1; 5825 5826 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5827 "sd_check_vpd_page_support: This drive does not implement " 5828 "VPD pages.\n"); 5829 } 5830 5831 kmem_free(page_list, page_length); 5832 5833 return (rval); 5834 } 5835 5836 5837 /* 5838 * Function: sd_setup_pm 5839 * 5840 * Description: Initialize Power Management on the device 5841 * 5842 * Context: Kernel Thread 5843 */ 5844 5845 static void 5846 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi) 5847 { 5848 uint_t log_page_size; 5849 uchar_t *log_page_data; 5850 int rval = 0; 5851 struct sd_lun *un; 5852 5853 ASSERT(ssc != NULL); 5854 un = ssc->ssc_un; 5855 ASSERT(un != NULL); 5856 5857 /* 5858 * Since we are called from attach, holding a mutex for 5859 * un is unnecessary. Because some of the routines called 5860 * from here require SD_MUTEX to not be held, assert this 5861 * right up front. 5862 */ 5863 ASSERT(!mutex_owned(SD_MUTEX(un))); 5864 /* 5865 * Since the sd device does not have the 'reg' property, 5866 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5867 * The following code is to tell cpr that this device 5868 * DOES need to be suspended and resumed. 5869 */ 5870 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5871 "pm-hardware-state", "needs-suspend-resume"); 5872 5873 /* 5874 * This complies with the new power management framework 5875 * for certain desktop machines. Create the pm_components 5876 * property as a string array property. 5877 * If un_f_pm_supported is TRUE, that means the disk 5878 * attached HBA has set the "pm-capable" property and 5879 * the value of this property is bigger than 0. 5880 */ 5881 if (un->un_f_pm_supported) { 5882 /* 5883 * not all devices have a motor, try it first. 5884 * some devices may return ILLEGAL REQUEST, some 5885 * will hang 5886 * The following START_STOP_UNIT is used to check if target 5887 * device has a motor. 5888 */ 5889 un->un_f_start_stop_supported = TRUE; 5890 5891 if (un->un_f_power_condition_supported) { 5892 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5893 SD_POWER_CONDITION, SD_TARGET_ACTIVE, 5894 SD_PATH_DIRECT); 5895 if (rval != 0) { 5896 un->un_f_power_condition_supported = FALSE; 5897 } 5898 } 5899 if (!un->un_f_power_condition_supported) { 5900 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5901 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT); 5902 } 5903 if (rval != 0) { 5904 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5905 un->un_f_start_stop_supported = FALSE; 5906 } 5907 5908 /* 5909 * create pm properties anyways otherwise the parent can't 5910 * go to sleep 5911 */ 5912 un->un_f_pm_is_enabled = TRUE; 5913 (void) sd_create_pm_components(devi, un); 5914 5915 /* 5916 * If it claims that log sense is supported, check it out. 5917 */ 5918 if (un->un_f_log_sense_supported) { 5919 rval = sd_log_page_supported(ssc, 5920 START_STOP_CYCLE_PAGE); 5921 if (rval == 1) { 5922 /* Page found, use it. */ 5923 un->un_start_stop_cycle_page = 5924 START_STOP_CYCLE_PAGE; 5925 } else { 5926 /* 5927 * Page not found or log sense is not 5928 * supported. 5929 * Notice we do not check the old style 5930 * START_STOP_CYCLE_VU_PAGE because this 5931 * code path does not apply to old disks. 5932 */ 5933 un->un_f_log_sense_supported = FALSE; 5934 un->un_f_pm_log_sense_smart = FALSE; 5935 } 5936 } 5937 5938 return; 5939 } 5940 5941 /* 5942 * For the disk whose attached HBA has not set the "pm-capable" 5943 * property, check if it supports the power management. 5944 */ 5945 if (!un->un_f_log_sense_supported) { 5946 un->un_power_level = SD_SPINDLE_ON; 5947 un->un_f_pm_is_enabled = FALSE; 5948 return; 5949 } 5950 5951 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE); 5952 5953 #ifdef SDDEBUG 5954 if (sd_force_pm_supported) { 5955 /* Force a successful result */ 5956 rval = 1; 5957 } 5958 #endif 5959 5960 /* 5961 * If the start-stop cycle counter log page is not supported 5962 * or if the pm-capable property is set to be false (0), 5963 * then we should not create the pm_components property. 5964 */ 5965 if (rval == -1) { 5966 /* 5967 * Error. 5968 * Reading log sense failed, most likely this is 5969 * an older drive that does not support log sense. 5970 * If this fails auto-pm is not supported. 5971 */ 5972 un->un_power_level = SD_SPINDLE_ON; 5973 un->un_f_pm_is_enabled = FALSE; 5974 5975 } else if (rval == 0) { 5976 /* 5977 * Page not found. 5978 * The start stop cycle counter is implemented as page 5979 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5980 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 5981 */ 5982 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) { 5983 /* 5984 * Page found, use this one. 5985 */ 5986 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 5987 un->un_f_pm_is_enabled = TRUE; 5988 } else { 5989 /* 5990 * Error or page not found. 5991 * auto-pm is not supported for this device. 5992 */ 5993 un->un_power_level = SD_SPINDLE_ON; 5994 un->un_f_pm_is_enabled = FALSE; 5995 } 5996 } else { 5997 /* 5998 * Page found, use it. 5999 */ 6000 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6001 un->un_f_pm_is_enabled = TRUE; 6002 } 6003 6004 6005 if (un->un_f_pm_is_enabled == TRUE) { 6006 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6007 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6008 6009 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6010 log_page_size, un->un_start_stop_cycle_page, 6011 0x01, 0, SD_PATH_DIRECT); 6012 6013 if (rval != 0) { 6014 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6015 } 6016 6017 #ifdef SDDEBUG 6018 if (sd_force_pm_supported) { 6019 /* Force a successful result */ 6020 rval = 0; 6021 } 6022 #endif 6023 6024 /* 6025 * If the Log sense for Page( Start/stop cycle counter page) 6026 * succeeds, then power management is supported and we can 6027 * enable auto-pm. 6028 */ 6029 if (rval == 0) { 6030 (void) sd_create_pm_components(devi, un); 6031 } else { 6032 un->un_power_level = SD_SPINDLE_ON; 6033 un->un_f_pm_is_enabled = FALSE; 6034 } 6035 6036 kmem_free(log_page_data, log_page_size); 6037 } 6038 } 6039 6040 6041 /* 6042 * Function: sd_create_pm_components 6043 * 6044 * Description: Initialize PM property. 6045 * 6046 * Context: Kernel thread context 6047 */ 6048 6049 static void 6050 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6051 { 6052 ASSERT(!mutex_owned(SD_MUTEX(un))); 6053 6054 if (un->un_f_power_condition_supported) { 6055 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6056 "pm-components", sd_pwr_pc.pm_comp, 5) 6057 != DDI_PROP_SUCCESS) { 6058 un->un_power_level = SD_SPINDLE_ACTIVE; 6059 un->un_f_pm_is_enabled = FALSE; 6060 return; 6061 } 6062 } else { 6063 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6064 "pm-components", sd_pwr_ss.pm_comp, 3) 6065 != DDI_PROP_SUCCESS) { 6066 un->un_power_level = SD_SPINDLE_ON; 6067 un->un_f_pm_is_enabled = FALSE; 6068 return; 6069 } 6070 } 6071 /* 6072 * When components are initially created they are idle, 6073 * power up any non-removables. 6074 * Note: the return value of pm_raise_power can't be used 6075 * for determining if PM should be enabled for this device. 6076 * Even if you check the return values and remove this 6077 * property created above, the PM framework will not honor the 6078 * change after the first call to pm_raise_power. Hence, 6079 * removal of that property does not help if pm_raise_power 6080 * fails. In the case of removable media, the start/stop 6081 * will fail if the media is not present. 6082 */ 6083 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6084 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) { 6085 mutex_enter(SD_MUTEX(un)); 6086 un->un_power_level = SD_PM_STATE_ACTIVE(un); 6087 mutex_enter(&un->un_pm_mutex); 6088 /* Set to on and not busy. */ 6089 un->un_pm_count = 0; 6090 } else { 6091 mutex_enter(SD_MUTEX(un)); 6092 un->un_power_level = SD_PM_STATE_STOPPED(un); 6093 mutex_enter(&un->un_pm_mutex); 6094 /* Set to off. */ 6095 un->un_pm_count = -1; 6096 } 6097 mutex_exit(&un->un_pm_mutex); 6098 mutex_exit(SD_MUTEX(un)); 6099 } 6100 6101 6102 /* 6103 * Function: sd_ddi_suspend 6104 * 6105 * Description: Performs system power-down operations. This includes 6106 * setting the drive state to indicate its suspended so 6107 * that no new commands will be accepted. Also, wait for 6108 * all commands that are in transport or queued to a timer 6109 * for retry to complete. All timeout threads are cancelled. 6110 * 6111 * Return Code: DDI_FAILURE or DDI_SUCCESS 6112 * 6113 * Context: Kernel thread context 6114 */ 6115 6116 static int 6117 sd_ddi_suspend(dev_info_t *devi) 6118 { 6119 struct sd_lun *un; 6120 clock_t wait_cmds_complete; 6121 6122 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6123 if (un == NULL) { 6124 return (DDI_FAILURE); 6125 } 6126 6127 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6128 6129 mutex_enter(SD_MUTEX(un)); 6130 6131 /* Return success if the device is already suspended. */ 6132 if (un->un_state == SD_STATE_SUSPENDED) { 6133 mutex_exit(SD_MUTEX(un)); 6134 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6135 "device already suspended, exiting\n"); 6136 return (DDI_SUCCESS); 6137 } 6138 6139 /* Return failure if the device is being used by HA */ 6140 if (un->un_resvd_status & 6141 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6142 mutex_exit(SD_MUTEX(un)); 6143 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6144 "device in use by HA, exiting\n"); 6145 return (DDI_FAILURE); 6146 } 6147 6148 /* 6149 * Return failure if the device is in a resource wait 6150 * or power changing state. 6151 */ 6152 if ((un->un_state == SD_STATE_RWAIT) || 6153 (un->un_state == SD_STATE_PM_CHANGING)) { 6154 mutex_exit(SD_MUTEX(un)); 6155 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6156 "device in resource wait state, exiting\n"); 6157 return (DDI_FAILURE); 6158 } 6159 6160 6161 un->un_save_state = un->un_last_state; 6162 New_state(un, SD_STATE_SUSPENDED); 6163 6164 /* 6165 * Wait for all commands that are in transport or queued to a timer 6166 * for retry to complete. 6167 * 6168 * While waiting, no new commands will be accepted or sent because of 6169 * the new state we set above. 6170 * 6171 * Wait till current operation has completed. If we are in the resource 6172 * wait state (with an intr outstanding) then we need to wait till the 6173 * intr completes and starts the next cmd. We want to wait for 6174 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6175 */ 6176 wait_cmds_complete = ddi_get_lbolt() + 6177 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6178 6179 while (un->un_ncmds_in_transport != 0) { 6180 /* 6181 * Fail if commands do not finish in the specified time. 6182 */ 6183 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6184 wait_cmds_complete) == -1) { 6185 /* 6186 * Undo the state changes made above. Everything 6187 * must go back to it's original value. 6188 */ 6189 Restore_state(un); 6190 un->un_last_state = un->un_save_state; 6191 /* Wake up any threads that might be waiting. */ 6192 cv_broadcast(&un->un_suspend_cv); 6193 mutex_exit(SD_MUTEX(un)); 6194 SD_ERROR(SD_LOG_IO_PM, un, 6195 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6196 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6197 return (DDI_FAILURE); 6198 } 6199 } 6200 6201 /* 6202 * Cancel SCSI watch thread and timeouts, if any are active 6203 */ 6204 6205 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6206 opaque_t temp_token = un->un_swr_token; 6207 mutex_exit(SD_MUTEX(un)); 6208 scsi_watch_suspend(temp_token); 6209 mutex_enter(SD_MUTEX(un)); 6210 } 6211 6212 if (un->un_reset_throttle_timeid != NULL) { 6213 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6214 un->un_reset_throttle_timeid = NULL; 6215 mutex_exit(SD_MUTEX(un)); 6216 (void) untimeout(temp_id); 6217 mutex_enter(SD_MUTEX(un)); 6218 } 6219 6220 if (un->un_dcvb_timeid != NULL) { 6221 timeout_id_t temp_id = un->un_dcvb_timeid; 6222 un->un_dcvb_timeid = NULL; 6223 mutex_exit(SD_MUTEX(un)); 6224 (void) untimeout(temp_id); 6225 mutex_enter(SD_MUTEX(un)); 6226 } 6227 6228 mutex_enter(&un->un_pm_mutex); 6229 if (un->un_pm_timeid != NULL) { 6230 timeout_id_t temp_id = un->un_pm_timeid; 6231 un->un_pm_timeid = NULL; 6232 mutex_exit(&un->un_pm_mutex); 6233 mutex_exit(SD_MUTEX(un)); 6234 (void) untimeout(temp_id); 6235 mutex_enter(SD_MUTEX(un)); 6236 } else { 6237 mutex_exit(&un->un_pm_mutex); 6238 } 6239 6240 if (un->un_rmw_msg_timeid != NULL) { 6241 timeout_id_t temp_id = un->un_rmw_msg_timeid; 6242 un->un_rmw_msg_timeid = NULL; 6243 mutex_exit(SD_MUTEX(un)); 6244 (void) untimeout(temp_id); 6245 mutex_enter(SD_MUTEX(un)); 6246 } 6247 6248 if (un->un_retry_timeid != NULL) { 6249 timeout_id_t temp_id = un->un_retry_timeid; 6250 un->un_retry_timeid = NULL; 6251 mutex_exit(SD_MUTEX(un)); 6252 (void) untimeout(temp_id); 6253 mutex_enter(SD_MUTEX(un)); 6254 6255 if (un->un_retry_bp != NULL) { 6256 un->un_retry_bp->av_forw = un->un_waitq_headp; 6257 un->un_waitq_headp = un->un_retry_bp; 6258 if (un->un_waitq_tailp == NULL) { 6259 un->un_waitq_tailp = un->un_retry_bp; 6260 } 6261 un->un_retry_bp = NULL; 6262 un->un_retry_statp = NULL; 6263 } 6264 } 6265 6266 if (un->un_direct_priority_timeid != NULL) { 6267 timeout_id_t temp_id = un->un_direct_priority_timeid; 6268 un->un_direct_priority_timeid = NULL; 6269 mutex_exit(SD_MUTEX(un)); 6270 (void) untimeout(temp_id); 6271 mutex_enter(SD_MUTEX(un)); 6272 } 6273 6274 if (un->un_f_is_fibre == TRUE) { 6275 /* 6276 * Remove callbacks for insert and remove events 6277 */ 6278 if (un->un_insert_event != NULL) { 6279 mutex_exit(SD_MUTEX(un)); 6280 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6281 mutex_enter(SD_MUTEX(un)); 6282 un->un_insert_event = NULL; 6283 } 6284 6285 if (un->un_remove_event != NULL) { 6286 mutex_exit(SD_MUTEX(un)); 6287 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6288 mutex_enter(SD_MUTEX(un)); 6289 un->un_remove_event = NULL; 6290 } 6291 } 6292 6293 mutex_exit(SD_MUTEX(un)); 6294 6295 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6296 6297 return (DDI_SUCCESS); 6298 } 6299 6300 6301 /* 6302 * Function: sd_ddi_resume 6303 * 6304 * Description: Performs system power-up operations.. 6305 * 6306 * Return Code: DDI_SUCCESS 6307 * DDI_FAILURE 6308 * 6309 * Context: Kernel thread context 6310 */ 6311 6312 static int 6313 sd_ddi_resume(dev_info_t *devi) 6314 { 6315 struct sd_lun *un; 6316 6317 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6318 if (un == NULL) { 6319 return (DDI_FAILURE); 6320 } 6321 6322 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6323 6324 mutex_enter(SD_MUTEX(un)); 6325 Restore_state(un); 6326 6327 /* 6328 * Restore the state which was saved to give the 6329 * the right state in un_last_state 6330 */ 6331 un->un_last_state = un->un_save_state; 6332 /* 6333 * Note: throttle comes back at full. 6334 * Also note: this MUST be done before calling pm_raise_power 6335 * otherwise the system can get hung in biowait. The scenario where 6336 * this'll happen is under cpr suspend. Writing of the system 6337 * state goes through sddump, which writes 0 to un_throttle. If 6338 * writing the system state then fails, example if the partition is 6339 * too small, then cpr attempts a resume. If throttle isn't restored 6340 * from the saved value until after calling pm_raise_power then 6341 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6342 * in biowait. 6343 */ 6344 un->un_throttle = un->un_saved_throttle; 6345 6346 /* 6347 * The chance of failure is very rare as the only command done in power 6348 * entry point is START command when you transition from 0->1 or 6349 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6350 * which suspend was done. Ignore the return value as the resume should 6351 * not be failed. In the case of removable media the media need not be 6352 * inserted and hence there is a chance that raise power will fail with 6353 * media not present. 6354 */ 6355 if (un->un_f_attach_spinup) { 6356 mutex_exit(SD_MUTEX(un)); 6357 (void) pm_raise_power(SD_DEVINFO(un), 0, 6358 SD_PM_STATE_ACTIVE(un)); 6359 mutex_enter(SD_MUTEX(un)); 6360 } 6361 6362 /* 6363 * Don't broadcast to the suspend cv and therefore possibly 6364 * start I/O until after power has been restored. 6365 */ 6366 cv_broadcast(&un->un_suspend_cv); 6367 cv_broadcast(&un->un_state_cv); 6368 6369 /* restart thread */ 6370 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6371 scsi_watch_resume(un->un_swr_token); 6372 } 6373 6374 #if (defined(__fibre)) 6375 if (un->un_f_is_fibre == TRUE) { 6376 /* 6377 * Add callbacks for insert and remove events 6378 */ 6379 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6380 sd_init_event_callbacks(un); 6381 } 6382 } 6383 #endif 6384 6385 /* 6386 * Transport any pending commands to the target. 6387 * 6388 * If this is a low-activity device commands in queue will have to wait 6389 * until new commands come in, which may take awhile. Also, we 6390 * specifically don't check un_ncmds_in_transport because we know that 6391 * there really are no commands in progress after the unit was 6392 * suspended and we could have reached the throttle level, been 6393 * suspended, and have no new commands coming in for awhile. Highly 6394 * unlikely, but so is the low-activity disk scenario. 6395 */ 6396 ddi_xbuf_dispatch(un->un_xbuf_attr); 6397 6398 sd_start_cmds(un, NULL); 6399 mutex_exit(SD_MUTEX(un)); 6400 6401 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6402 6403 return (DDI_SUCCESS); 6404 } 6405 6406 6407 /* 6408 * Function: sd_pm_state_change 6409 * 6410 * Description: Change the driver power state. 6411 * Someone else is required to actually change the driver 6412 * power level. 6413 * 6414 * Arguments: un - driver soft state (unit) structure 6415 * level - the power level that is changed to 6416 * flag - to decide how to change the power state 6417 * 6418 * Return Code: DDI_SUCCESS 6419 * 6420 * Context: Kernel thread context 6421 */ 6422 static int 6423 sd_pm_state_change(struct sd_lun *un, int level, int flag) 6424 { 6425 ASSERT(un != NULL); 6426 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n"); 6427 6428 ASSERT(!mutex_owned(SD_MUTEX(un))); 6429 mutex_enter(SD_MUTEX(un)); 6430 6431 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) { 6432 un->un_power_level = level; 6433 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6434 mutex_enter(&un->un_pm_mutex); 6435 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6436 un->un_pm_count++; 6437 ASSERT(un->un_pm_count == 0); 6438 } 6439 mutex_exit(&un->un_pm_mutex); 6440 } else { 6441 /* 6442 * Exit if power management is not enabled for this device, 6443 * or if the device is being used by HA. 6444 */ 6445 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6446 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6447 mutex_exit(SD_MUTEX(un)); 6448 SD_TRACE(SD_LOG_POWER, un, 6449 "sd_pm_state_change: exiting\n"); 6450 return (DDI_FAILURE); 6451 } 6452 6453 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: " 6454 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver); 6455 6456 /* 6457 * See if the device is not busy, ie.: 6458 * - we have no commands in the driver for this device 6459 * - not waiting for resources 6460 */ 6461 if ((un->un_ncmds_in_driver == 0) && 6462 (un->un_state != SD_STATE_RWAIT)) { 6463 /* 6464 * The device is not busy, so it is OK to go to low 6465 * power state. Indicate low power, but rely on someone 6466 * else to actually change it. 6467 */ 6468 mutex_enter(&un->un_pm_mutex); 6469 un->un_pm_count = -1; 6470 mutex_exit(&un->un_pm_mutex); 6471 un->un_power_level = level; 6472 } 6473 } 6474 6475 mutex_exit(SD_MUTEX(un)); 6476 6477 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n"); 6478 6479 return (DDI_SUCCESS); 6480 } 6481 6482 6483 /* 6484 * Function: sd_pm_idletimeout_handler 6485 * 6486 * Description: A timer routine that's active only while a device is busy. 6487 * The purpose is to extend slightly the pm framework's busy 6488 * view of the device to prevent busy/idle thrashing for 6489 * back-to-back commands. Do this by comparing the current time 6490 * to the time at which the last command completed and when the 6491 * difference is greater than sd_pm_idletime, call 6492 * pm_idle_component. In addition to indicating idle to the pm 6493 * framework, update the chain type to again use the internal pm 6494 * layers of the driver. 6495 * 6496 * Arguments: arg - driver soft state (unit) structure 6497 * 6498 * Context: Executes in a timeout(9F) thread context 6499 */ 6500 6501 static void 6502 sd_pm_idletimeout_handler(void *arg) 6503 { 6504 struct sd_lun *un = arg; 6505 6506 time_t now; 6507 6508 mutex_enter(&sd_detach_mutex); 6509 if (un->un_detach_count != 0) { 6510 /* Abort if the instance is detaching */ 6511 mutex_exit(&sd_detach_mutex); 6512 return; 6513 } 6514 mutex_exit(&sd_detach_mutex); 6515 6516 now = ddi_get_time(); 6517 /* 6518 * Grab both mutexes, in the proper order, since we're accessing 6519 * both PM and softstate variables. 6520 */ 6521 mutex_enter(SD_MUTEX(un)); 6522 mutex_enter(&un->un_pm_mutex); 6523 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 6524 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 6525 /* 6526 * Update the chain types. 6527 * This takes affect on the next new command received. 6528 */ 6529 if (un->un_f_non_devbsize_supported) { 6530 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6531 } else { 6532 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6533 } 6534 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6535 6536 SD_TRACE(SD_LOG_IO_PM, un, 6537 "sd_pm_idletimeout_handler: idling device\n"); 6538 (void) pm_idle_component(SD_DEVINFO(un), 0); 6539 un->un_pm_idle_timeid = NULL; 6540 } else { 6541 un->un_pm_idle_timeid = 6542 timeout(sd_pm_idletimeout_handler, un, 6543 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 6544 } 6545 mutex_exit(&un->un_pm_mutex); 6546 mutex_exit(SD_MUTEX(un)); 6547 } 6548 6549 6550 /* 6551 * Function: sd_pm_timeout_handler 6552 * 6553 * Description: Callback to tell framework we are idle. 6554 * 6555 * Context: timeout(9f) thread context. 6556 */ 6557 6558 static void 6559 sd_pm_timeout_handler(void *arg) 6560 { 6561 struct sd_lun *un = arg; 6562 6563 (void) pm_idle_component(SD_DEVINFO(un), 0); 6564 mutex_enter(&un->un_pm_mutex); 6565 un->un_pm_timeid = NULL; 6566 mutex_exit(&un->un_pm_mutex); 6567 } 6568 6569 6570 /* 6571 * Function: sdpower 6572 * 6573 * Description: PM entry point. 6574 * 6575 * Return Code: DDI_SUCCESS 6576 * DDI_FAILURE 6577 * 6578 * Context: Kernel thread context 6579 */ 6580 6581 static int 6582 sdpower(dev_info_t *devi, int component, int level) 6583 { 6584 struct sd_lun *un; 6585 int instance; 6586 int rval = DDI_SUCCESS; 6587 uint_t i, log_page_size, maxcycles, ncycles; 6588 uchar_t *log_page_data; 6589 int log_sense_page; 6590 int medium_present; 6591 time_t intvlp; 6592 struct pm_trans_data sd_pm_tran_data; 6593 uchar_t save_state; 6594 int sval; 6595 uchar_t state_before_pm; 6596 int got_semaphore_here; 6597 sd_ssc_t *ssc; 6598 int last_power_level; 6599 6600 instance = ddi_get_instance(devi); 6601 6602 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 6603 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) { 6604 return (DDI_FAILURE); 6605 } 6606 6607 ssc = sd_ssc_init(un); 6608 6609 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 6610 6611 /* 6612 * Must synchronize power down with close. 6613 * Attempt to decrement/acquire the open/close semaphore, 6614 * but do NOT wait on it. If it's not greater than zero, 6615 * ie. it can't be decremented without waiting, then 6616 * someone else, either open or close, already has it 6617 * and the try returns 0. Use that knowledge here to determine 6618 * if it's OK to change the device power level. 6619 * Also, only increment it on exit if it was decremented, ie. gotten, 6620 * here. 6621 */ 6622 got_semaphore_here = sema_tryp(&un->un_semoclose); 6623 6624 mutex_enter(SD_MUTEX(un)); 6625 6626 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 6627 un->un_ncmds_in_driver); 6628 6629 /* 6630 * If un_ncmds_in_driver is non-zero it indicates commands are 6631 * already being processed in the driver, or if the semaphore was 6632 * not gotten here it indicates an open or close is being processed. 6633 * At the same time somebody is requesting to go to a lower power 6634 * that can't perform I/O, which can't happen, therefore we need to 6635 * return failure. 6636 */ 6637 if ((!SD_PM_IS_IO_CAPABLE(un, level)) && 6638 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 6639 mutex_exit(SD_MUTEX(un)); 6640 6641 if (got_semaphore_here != 0) { 6642 sema_v(&un->un_semoclose); 6643 } 6644 SD_TRACE(SD_LOG_IO_PM, un, 6645 "sdpower: exit, device has queued cmds.\n"); 6646 6647 goto sdpower_failed; 6648 } 6649 6650 /* 6651 * if it is OFFLINE that means the disk is completely dead 6652 * in our case we have to put the disk in on or off by sending commands 6653 * Of course that will fail anyway so return back here. 6654 * 6655 * Power changes to a device that's OFFLINE or SUSPENDED 6656 * are not allowed. 6657 */ 6658 if ((un->un_state == SD_STATE_OFFLINE) || 6659 (un->un_state == SD_STATE_SUSPENDED)) { 6660 mutex_exit(SD_MUTEX(un)); 6661 6662 if (got_semaphore_here != 0) { 6663 sema_v(&un->un_semoclose); 6664 } 6665 SD_TRACE(SD_LOG_IO_PM, un, 6666 "sdpower: exit, device is off-line.\n"); 6667 6668 goto sdpower_failed; 6669 } 6670 6671 /* 6672 * Change the device's state to indicate it's power level 6673 * is being changed. Do this to prevent a power off in the 6674 * middle of commands, which is especially bad on devices 6675 * that are really powered off instead of just spun down. 6676 */ 6677 state_before_pm = un->un_state; 6678 un->un_state = SD_STATE_PM_CHANGING; 6679 6680 mutex_exit(SD_MUTEX(un)); 6681 6682 /* 6683 * If log sense command is not supported, bypass the 6684 * following checking, otherwise, check the log sense 6685 * information for this device. 6686 */ 6687 if (SD_PM_STOP_MOTOR_NEEDED(un, level) && 6688 un->un_f_log_sense_supported) { 6689 /* 6690 * Get the log sense information to understand whether the 6691 * the powercycle counts have gone beyond the threshhold. 6692 */ 6693 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6694 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6695 6696 mutex_enter(SD_MUTEX(un)); 6697 log_sense_page = un->un_start_stop_cycle_page; 6698 mutex_exit(SD_MUTEX(un)); 6699 6700 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6701 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 6702 6703 if (rval != 0) { 6704 if (rval == EIO) 6705 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6706 else 6707 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6708 } 6709 6710 #ifdef SDDEBUG 6711 if (sd_force_pm_supported) { 6712 /* Force a successful result */ 6713 rval = 0; 6714 } 6715 #endif 6716 if (rval != 0) { 6717 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 6718 "Log Sense Failed\n"); 6719 6720 kmem_free(log_page_data, log_page_size); 6721 /* Cannot support power management on those drives */ 6722 6723 if (got_semaphore_here != 0) { 6724 sema_v(&un->un_semoclose); 6725 } 6726 /* 6727 * On exit put the state back to it's original value 6728 * and broadcast to anyone waiting for the power 6729 * change completion. 6730 */ 6731 mutex_enter(SD_MUTEX(un)); 6732 un->un_state = state_before_pm; 6733 cv_broadcast(&un->un_suspend_cv); 6734 mutex_exit(SD_MUTEX(un)); 6735 SD_TRACE(SD_LOG_IO_PM, un, 6736 "sdpower: exit, Log Sense Failed.\n"); 6737 6738 goto sdpower_failed; 6739 } 6740 6741 /* 6742 * From the page data - Convert the essential information to 6743 * pm_trans_data 6744 */ 6745 maxcycles = 6746 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 6747 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 6748 6749 ncycles = 6750 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 6751 (log_page_data[0x26] << 8) | log_page_data[0x27]; 6752 6753 if (un->un_f_pm_log_sense_smart) { 6754 sd_pm_tran_data.un.smart_count.allowed = maxcycles; 6755 sd_pm_tran_data.un.smart_count.consumed = ncycles; 6756 sd_pm_tran_data.un.smart_count.flag = 0; 6757 sd_pm_tran_data.format = DC_SMART_FORMAT; 6758 } else { 6759 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 6760 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 6761 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 6762 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 6763 log_page_data[8+i]; 6764 } 6765 sd_pm_tran_data.un.scsi_cycles.flag = 0; 6766 sd_pm_tran_data.format = DC_SCSI_FORMAT; 6767 } 6768 6769 kmem_free(log_page_data, log_page_size); 6770 6771 /* 6772 * Call pm_trans_check routine to get the Ok from 6773 * the global policy 6774 */ 6775 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 6776 #ifdef SDDEBUG 6777 if (sd_force_pm_supported) { 6778 /* Force a successful result */ 6779 rval = 1; 6780 } 6781 #endif 6782 switch (rval) { 6783 case 0: 6784 /* 6785 * Not Ok to Power cycle or error in parameters passed 6786 * Would have given the advised time to consider power 6787 * cycle. Based on the new intvlp parameter we are 6788 * supposed to pretend we are busy so that pm framework 6789 * will never call our power entry point. Because of 6790 * that install a timeout handler and wait for the 6791 * recommended time to elapse so that power management 6792 * can be effective again. 6793 * 6794 * To effect this behavior, call pm_busy_component to 6795 * indicate to the framework this device is busy. 6796 * By not adjusting un_pm_count the rest of PM in 6797 * the driver will function normally, and independent 6798 * of this but because the framework is told the device 6799 * is busy it won't attempt powering down until it gets 6800 * a matching idle. The timeout handler sends this. 6801 * Note: sd_pm_entry can't be called here to do this 6802 * because sdpower may have been called as a result 6803 * of a call to pm_raise_power from within sd_pm_entry. 6804 * 6805 * If a timeout handler is already active then 6806 * don't install another. 6807 */ 6808 mutex_enter(&un->un_pm_mutex); 6809 if (un->un_pm_timeid == NULL) { 6810 un->un_pm_timeid = 6811 timeout(sd_pm_timeout_handler, 6812 un, intvlp * drv_usectohz(1000000)); 6813 mutex_exit(&un->un_pm_mutex); 6814 (void) pm_busy_component(SD_DEVINFO(un), 0); 6815 } else { 6816 mutex_exit(&un->un_pm_mutex); 6817 } 6818 if (got_semaphore_here != 0) { 6819 sema_v(&un->un_semoclose); 6820 } 6821 /* 6822 * On exit put the state back to it's original value 6823 * and broadcast to anyone waiting for the power 6824 * change completion. 6825 */ 6826 mutex_enter(SD_MUTEX(un)); 6827 un->un_state = state_before_pm; 6828 cv_broadcast(&un->un_suspend_cv); 6829 mutex_exit(SD_MUTEX(un)); 6830 6831 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6832 "trans check Failed, not ok to power cycle.\n"); 6833 6834 goto sdpower_failed; 6835 case -1: 6836 if (got_semaphore_here != 0) { 6837 sema_v(&un->un_semoclose); 6838 } 6839 /* 6840 * On exit put the state back to it's original value 6841 * and broadcast to anyone waiting for the power 6842 * change completion. 6843 */ 6844 mutex_enter(SD_MUTEX(un)); 6845 un->un_state = state_before_pm; 6846 cv_broadcast(&un->un_suspend_cv); 6847 mutex_exit(SD_MUTEX(un)); 6848 SD_TRACE(SD_LOG_IO_PM, un, 6849 "sdpower: exit, trans check command Failed.\n"); 6850 6851 goto sdpower_failed; 6852 } 6853 } 6854 6855 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6856 /* 6857 * Save the last state... if the STOP FAILS we need it 6858 * for restoring 6859 */ 6860 mutex_enter(SD_MUTEX(un)); 6861 save_state = un->un_last_state; 6862 last_power_level = un->un_power_level; 6863 /* 6864 * There must not be any cmds. getting processed 6865 * in the driver when we get here. Power to the 6866 * device is potentially going off. 6867 */ 6868 ASSERT(un->un_ncmds_in_driver == 0); 6869 mutex_exit(SD_MUTEX(un)); 6870 6871 /* 6872 * For now PM suspend the device completely before spindle is 6873 * turned off 6874 */ 6875 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE)) 6876 == DDI_FAILURE) { 6877 if (got_semaphore_here != 0) { 6878 sema_v(&un->un_semoclose); 6879 } 6880 /* 6881 * On exit put the state back to it's original value 6882 * and broadcast to anyone waiting for the power 6883 * change completion. 6884 */ 6885 mutex_enter(SD_MUTEX(un)); 6886 un->un_state = state_before_pm; 6887 un->un_power_level = last_power_level; 6888 cv_broadcast(&un->un_suspend_cv); 6889 mutex_exit(SD_MUTEX(un)); 6890 SD_TRACE(SD_LOG_IO_PM, un, 6891 "sdpower: exit, PM suspend Failed.\n"); 6892 6893 goto sdpower_failed; 6894 } 6895 } 6896 6897 /* 6898 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6899 * close, or strategy. Dump no long uses this routine, it uses it's 6900 * own code so it can be done in polled mode. 6901 */ 6902 6903 medium_present = TRUE; 6904 6905 /* 6906 * When powering up, issue a TUR in case the device is at unit 6907 * attention. Don't do retries. Bypass the PM layer, otherwise 6908 * a deadlock on un_pm_busy_cv will occur. 6909 */ 6910 if (SD_PM_IS_IO_CAPABLE(un, level)) { 6911 sval = sd_send_scsi_TEST_UNIT_READY(ssc, 6912 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6913 if (sval != 0) 6914 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6915 } 6916 6917 if (un->un_f_power_condition_supported) { 6918 char *pm_condition_name[] = {"STOPPED", "STANDBY", 6919 "IDLE", "ACTIVE"}; 6920 SD_TRACE(SD_LOG_IO_PM, un, 6921 "sdpower: sending \'%s\' power condition", 6922 pm_condition_name[level]); 6923 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 6924 sd_pl2pc[level], SD_PATH_DIRECT); 6925 } else { 6926 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6927 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6928 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 6929 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : 6930 SD_TARGET_STOP), SD_PATH_DIRECT); 6931 } 6932 if (sval != 0) { 6933 if (sval == EIO) 6934 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6935 else 6936 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6937 } 6938 6939 /* Command failed, check for media present. */ 6940 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6941 medium_present = FALSE; 6942 } 6943 6944 /* 6945 * The conditions of interest here are: 6946 * if a spindle off with media present fails, 6947 * then restore the state and return an error. 6948 * else if a spindle on fails, 6949 * then return an error (there's no state to restore). 6950 * In all other cases we setup for the new state 6951 * and return success. 6952 */ 6953 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6954 if ((medium_present == TRUE) && (sval != 0)) { 6955 /* The stop command from above failed */ 6956 rval = DDI_FAILURE; 6957 /* 6958 * The stop command failed, and we have media 6959 * present. Put the level back by calling the 6960 * sd_pm_resume() and set the state back to 6961 * it's previous value. 6962 */ 6963 (void) sd_pm_state_change(un, last_power_level, 6964 SD_PM_STATE_ROLLBACK); 6965 mutex_enter(SD_MUTEX(un)); 6966 un->un_last_state = save_state; 6967 mutex_exit(SD_MUTEX(un)); 6968 } else if (un->un_f_monitor_media_state) { 6969 /* 6970 * The stop command from above succeeded. 6971 * Terminate watch thread in case of removable media 6972 * devices going into low power state. This is as per 6973 * the requirements of pm framework, otherwise commands 6974 * will be generated for the device (through watch 6975 * thread), even when the device is in low power state. 6976 */ 6977 mutex_enter(SD_MUTEX(un)); 6978 un->un_f_watcht_stopped = FALSE; 6979 if (un->un_swr_token != NULL) { 6980 opaque_t temp_token = un->un_swr_token; 6981 un->un_f_watcht_stopped = TRUE; 6982 un->un_swr_token = NULL; 6983 mutex_exit(SD_MUTEX(un)); 6984 (void) scsi_watch_request_terminate(temp_token, 6985 SCSI_WATCH_TERMINATE_ALL_WAIT); 6986 } else { 6987 mutex_exit(SD_MUTEX(un)); 6988 } 6989 } 6990 } else { 6991 /* 6992 * The level requested is I/O capable. 6993 * Legacy behavior: return success on a failed spinup 6994 * if there is no media in the drive. 6995 * Do this by looking at medium_present here. 6996 */ 6997 if ((sval != 0) && medium_present) { 6998 /* The start command from above failed */ 6999 rval = DDI_FAILURE; 7000 } else { 7001 /* 7002 * The start command from above succeeded 7003 * PM resume the devices now that we have 7004 * started the disks 7005 */ 7006 (void) sd_pm_state_change(un, level, 7007 SD_PM_STATE_CHANGE); 7008 7009 /* 7010 * Resume the watch thread since it was suspended 7011 * when the device went into low power mode. 7012 */ 7013 if (un->un_f_monitor_media_state) { 7014 mutex_enter(SD_MUTEX(un)); 7015 if (un->un_f_watcht_stopped == TRUE) { 7016 opaque_t temp_token; 7017 7018 un->un_f_watcht_stopped = FALSE; 7019 mutex_exit(SD_MUTEX(un)); 7020 temp_token = 7021 sd_watch_request_submit(un); 7022 mutex_enter(SD_MUTEX(un)); 7023 un->un_swr_token = temp_token; 7024 } 7025 mutex_exit(SD_MUTEX(un)); 7026 } 7027 } 7028 } 7029 7030 if (got_semaphore_here != 0) { 7031 sema_v(&un->un_semoclose); 7032 } 7033 /* 7034 * On exit put the state back to it's original value 7035 * and broadcast to anyone waiting for the power 7036 * change completion. 7037 */ 7038 mutex_enter(SD_MUTEX(un)); 7039 un->un_state = state_before_pm; 7040 cv_broadcast(&un->un_suspend_cv); 7041 mutex_exit(SD_MUTEX(un)); 7042 7043 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7044 7045 sd_ssc_fini(ssc); 7046 return (rval); 7047 7048 sdpower_failed: 7049 7050 sd_ssc_fini(ssc); 7051 return (DDI_FAILURE); 7052 } 7053 7054 7055 7056 /* 7057 * Function: sdattach 7058 * 7059 * Description: Driver's attach(9e) entry point function. 7060 * 7061 * Arguments: devi - opaque device info handle 7062 * cmd - attach type 7063 * 7064 * Return Code: DDI_SUCCESS 7065 * DDI_FAILURE 7066 * 7067 * Context: Kernel thread context 7068 */ 7069 7070 static int 7071 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7072 { 7073 switch (cmd) { 7074 case DDI_ATTACH: 7075 return (sd_unit_attach(devi)); 7076 case DDI_RESUME: 7077 return (sd_ddi_resume(devi)); 7078 default: 7079 break; 7080 } 7081 return (DDI_FAILURE); 7082 } 7083 7084 7085 /* 7086 * Function: sddetach 7087 * 7088 * Description: Driver's detach(9E) entry point function. 7089 * 7090 * Arguments: devi - opaque device info handle 7091 * cmd - detach type 7092 * 7093 * Return Code: DDI_SUCCESS 7094 * DDI_FAILURE 7095 * 7096 * Context: Kernel thread context 7097 */ 7098 7099 static int 7100 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7101 { 7102 switch (cmd) { 7103 case DDI_DETACH: 7104 return (sd_unit_detach(devi)); 7105 case DDI_SUSPEND: 7106 return (sd_ddi_suspend(devi)); 7107 default: 7108 break; 7109 } 7110 return (DDI_FAILURE); 7111 } 7112 7113 7114 /* 7115 * Function: sd_sync_with_callback 7116 * 7117 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7118 * state while the callback routine is active. 7119 * 7120 * Arguments: un: softstate structure for the instance 7121 * 7122 * Context: Kernel thread context 7123 */ 7124 7125 static void 7126 sd_sync_with_callback(struct sd_lun *un) 7127 { 7128 ASSERT(un != NULL); 7129 7130 mutex_enter(SD_MUTEX(un)); 7131 7132 ASSERT(un->un_in_callback >= 0); 7133 7134 while (un->un_in_callback > 0) { 7135 mutex_exit(SD_MUTEX(un)); 7136 delay(2); 7137 mutex_enter(SD_MUTEX(un)); 7138 } 7139 7140 mutex_exit(SD_MUTEX(un)); 7141 } 7142 7143 /* 7144 * Function: sd_unit_attach 7145 * 7146 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7147 * the soft state structure for the device and performs 7148 * all necessary structure and device initializations. 7149 * 7150 * Arguments: devi: the system's dev_info_t for the device. 7151 * 7152 * Return Code: DDI_SUCCESS if attach is successful. 7153 * DDI_FAILURE if any part of the attach fails. 7154 * 7155 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7156 * Kernel thread context only. Can sleep. 7157 */ 7158 7159 static int 7160 sd_unit_attach(dev_info_t *devi) 7161 { 7162 struct scsi_device *devp; 7163 struct sd_lun *un; 7164 char *variantp; 7165 char name_str[48]; 7166 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7167 int instance; 7168 int rval; 7169 int wc_enabled; 7170 int tgt; 7171 uint64_t capacity; 7172 uint_t lbasize = 0; 7173 dev_info_t *pdip = ddi_get_parent(devi); 7174 int offbyone = 0; 7175 int geom_label_valid = 0; 7176 sd_ssc_t *ssc; 7177 int status; 7178 struct sd_fm_internal *sfip = NULL; 7179 int max_xfer_size; 7180 7181 /* 7182 * Retrieve the target driver's private data area. This was set 7183 * up by the HBA. 7184 */ 7185 devp = ddi_get_driver_private(devi); 7186 7187 /* 7188 * Retrieve the target ID of the device. 7189 */ 7190 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7191 SCSI_ADDR_PROP_TARGET, -1); 7192 7193 /* 7194 * Since we have no idea what state things were left in by the last 7195 * user of the device, set up some 'default' settings, ie. turn 'em 7196 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7197 * Do this before the scsi_probe, which sends an inquiry. 7198 * This is a fix for bug (4430280). 7199 * Of special importance is wide-xfer. The drive could have been left 7200 * in wide transfer mode by the last driver to communicate with it, 7201 * this includes us. If that's the case, and if the following is not 7202 * setup properly or we don't re-negotiate with the drive prior to 7203 * transferring data to/from the drive, it causes bus parity errors, 7204 * data overruns, and unexpected interrupts. This first occurred when 7205 * the fix for bug (4378686) was made. 7206 */ 7207 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7208 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7209 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7210 7211 /* 7212 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 7213 * on a target. Setting it per lun instance actually sets the 7214 * capability of this target, which affects those luns already 7215 * attached on the same target. So during attach, we can only disable 7216 * this capability only when no other lun has been attached on this 7217 * target. By doing this, we assume a target has the same tagged-qing 7218 * capability for every lun. The condition can be removed when HBA 7219 * is changed to support per lun based tagged-qing capability. 7220 */ 7221 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7222 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7223 } 7224 7225 /* 7226 * Use scsi_probe() to issue an INQUIRY command to the device. 7227 * This call will allocate and fill in the scsi_inquiry structure 7228 * and point the sd_inq member of the scsi_device structure to it. 7229 * If the attach succeeds, then this memory will not be de-allocated 7230 * (via scsi_unprobe()) until the instance is detached. 7231 */ 7232 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7233 goto probe_failed; 7234 } 7235 7236 /* 7237 * Check the device type as specified in the inquiry data and 7238 * claim it if it is of a type that we support. 7239 */ 7240 switch (devp->sd_inq->inq_dtype) { 7241 case DTYPE_DIRECT: 7242 break; 7243 case DTYPE_RODIRECT: 7244 break; 7245 case DTYPE_OPTICAL: 7246 break; 7247 case DTYPE_NOTPRESENT: 7248 default: 7249 /* Unsupported device type; fail the attach. */ 7250 goto probe_failed; 7251 } 7252 7253 /* 7254 * Allocate the soft state structure for this unit. 7255 * 7256 * We rely upon this memory being set to all zeroes by 7257 * ddi_soft_state_zalloc(). We assume that any member of the 7258 * soft state structure that is not explicitly initialized by 7259 * this routine will have a value of zero. 7260 */ 7261 instance = ddi_get_instance(devp->sd_dev); 7262 #ifndef XPV_HVM_DRIVER 7263 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7264 goto probe_failed; 7265 } 7266 #endif /* !XPV_HVM_DRIVER */ 7267 7268 /* 7269 * Retrieve a pointer to the newly-allocated soft state. 7270 * 7271 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7272 * was successful, unless something has gone horribly wrong and the 7273 * ddi's soft state internals are corrupt (in which case it is 7274 * probably better to halt here than just fail the attach....) 7275 */ 7276 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7277 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7278 instance); 7279 /*NOTREACHED*/ 7280 } 7281 7282 /* 7283 * Link the back ptr of the driver soft state to the scsi_device 7284 * struct for this lun. 7285 * Save a pointer to the softstate in the driver-private area of 7286 * the scsi_device struct. 7287 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7288 * we first set un->un_sd below. 7289 */ 7290 un->un_sd = devp; 7291 devp->sd_private = (opaque_t)un; 7292 7293 /* 7294 * The following must be after devp is stored in the soft state struct. 7295 */ 7296 #ifdef SDDEBUG 7297 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7298 "%s_unit_attach: un:0x%p instance:%d\n", 7299 ddi_driver_name(devi), un, instance); 7300 #endif 7301 7302 /* 7303 * Set up the device type and node type (for the minor nodes). 7304 * By default we assume that the device can at least support the 7305 * Common Command Set. Call it a CD-ROM if it reports itself 7306 * as a RODIRECT device. 7307 */ 7308 switch (devp->sd_inq->inq_dtype) { 7309 case DTYPE_RODIRECT: 7310 un->un_node_type = DDI_NT_CD_CHAN; 7311 un->un_ctype = CTYPE_CDROM; 7312 break; 7313 case DTYPE_OPTICAL: 7314 un->un_node_type = DDI_NT_BLOCK_CHAN; 7315 un->un_ctype = CTYPE_ROD; 7316 break; 7317 default: 7318 un->un_node_type = DDI_NT_BLOCK_CHAN; 7319 un->un_ctype = CTYPE_CCS; 7320 break; 7321 } 7322 7323 /* 7324 * Try to read the interconnect type from the HBA. 7325 * 7326 * Note: This driver is currently compiled as two binaries, a parallel 7327 * scsi version (sd) and a fibre channel version (ssd). All functional 7328 * differences are determined at compile time. In the future a single 7329 * binary will be provided and the interconnect type will be used to 7330 * differentiate between fibre and parallel scsi behaviors. At that time 7331 * it will be necessary for all fibre channel HBAs to support this 7332 * property. 7333 * 7334 * set un_f_is_fiber to TRUE ( default fiber ) 7335 */ 7336 un->un_f_is_fibre = TRUE; 7337 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7338 case INTERCONNECT_SSA: 7339 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7340 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7341 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7342 break; 7343 case INTERCONNECT_PARALLEL: 7344 un->un_f_is_fibre = FALSE; 7345 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7346 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7347 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7348 break; 7349 case INTERCONNECT_SAS: 7350 un->un_f_is_fibre = FALSE; 7351 un->un_interconnect_type = SD_INTERCONNECT_SAS; 7352 un->un_node_type = DDI_NT_BLOCK_SAS; 7353 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7354 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un); 7355 break; 7356 case INTERCONNECT_SATA: 7357 un->un_f_is_fibre = FALSE; 7358 un->un_interconnect_type = SD_INTERCONNECT_SATA; 7359 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7360 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 7361 break; 7362 case INTERCONNECT_FIBRE: 7363 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7364 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7365 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7366 break; 7367 case INTERCONNECT_FABRIC: 7368 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7369 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7370 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7371 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7372 break; 7373 default: 7374 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7375 /* 7376 * The HBA does not support the "interconnect-type" property 7377 * (or did not provide a recognized type). 7378 * 7379 * Note: This will be obsoleted when a single fibre channel 7380 * and parallel scsi driver is delivered. In the meantime the 7381 * interconnect type will be set to the platform default.If that 7382 * type is not parallel SCSI, it means that we should be 7383 * assuming "ssd" semantics. However, here this also means that 7384 * the FC HBA is not supporting the "interconnect-type" property 7385 * like we expect it to, so log this occurrence. 7386 */ 7387 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7388 if (!SD_IS_PARALLEL_SCSI(un)) { 7389 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7390 "sd_unit_attach: un:0x%p Assuming " 7391 "INTERCONNECT_FIBRE\n", un); 7392 } else { 7393 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7394 "sd_unit_attach: un:0x%p Assuming " 7395 "INTERCONNECT_PARALLEL\n", un); 7396 un->un_f_is_fibre = FALSE; 7397 } 7398 #else 7399 /* 7400 * Note: This source will be implemented when a single fibre 7401 * channel and parallel scsi driver is delivered. The default 7402 * will be to assume that if a device does not support the 7403 * "interconnect-type" property it is a parallel SCSI HBA and 7404 * we will set the interconnect type for parallel scsi. 7405 */ 7406 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7407 un->un_f_is_fibre = FALSE; 7408 #endif 7409 break; 7410 } 7411 7412 if (un->un_f_is_fibre == TRUE) { 7413 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7414 SCSI_VERSION_3) { 7415 switch (un->un_interconnect_type) { 7416 case SD_INTERCONNECT_FIBRE: 7417 case SD_INTERCONNECT_SSA: 7418 un->un_node_type = DDI_NT_BLOCK_WWN; 7419 break; 7420 default: 7421 break; 7422 } 7423 } 7424 } 7425 7426 /* 7427 * Initialize the Request Sense command for the target 7428 */ 7429 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7430 goto alloc_rqs_failed; 7431 } 7432 7433 /* 7434 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7435 * with separate binary for sd and ssd. 7436 * 7437 * x86 has 1 binary, un_retry_count is set base on connection type. 7438 * The hardcoded values will go away when Sparc uses 1 binary 7439 * for sd and ssd. This hardcoded values need to match 7440 * SD_RETRY_COUNT in sddef.h 7441 * The value used is base on interconnect type. 7442 * fibre = 3, parallel = 5 7443 */ 7444 #if defined(__i386) || defined(__amd64) 7445 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7446 #else 7447 un->un_retry_count = SD_RETRY_COUNT; 7448 #endif 7449 7450 /* 7451 * Set the per disk retry count to the default number of retries 7452 * for disks and CDROMs. This value can be overridden by the 7453 * disk property list or an entry in sd.conf. 7454 */ 7455 un->un_notready_retry_count = 7456 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7457 : DISK_NOT_READY_RETRY_COUNT(un); 7458 7459 /* 7460 * Set the busy retry count to the default value of un_retry_count. 7461 * This can be overridden by entries in sd.conf or the device 7462 * config table. 7463 */ 7464 un->un_busy_retry_count = un->un_retry_count; 7465 7466 /* 7467 * Init the reset threshold for retries. This number determines 7468 * how many retries must be performed before a reset can be issued 7469 * (for certain error conditions). This can be overridden by entries 7470 * in sd.conf or the device config table. 7471 */ 7472 un->un_reset_retry_count = (un->un_retry_count / 2); 7473 7474 /* 7475 * Set the victim_retry_count to the default un_retry_count 7476 */ 7477 un->un_victim_retry_count = (2 * un->un_retry_count); 7478 7479 /* 7480 * Set the reservation release timeout to the default value of 7481 * 5 seconds. This can be overridden by entries in ssd.conf or the 7482 * device config table. 7483 */ 7484 un->un_reserve_release_time = 5; 7485 7486 /* 7487 * Set up the default maximum transfer size. Note that this may 7488 * get updated later in the attach, when setting up default wide 7489 * operations for disks. 7490 */ 7491 #if defined(__i386) || defined(__amd64) 7492 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7493 un->un_partial_dma_supported = 1; 7494 #else 7495 un->un_max_xfer_size = (uint_t)maxphys; 7496 #endif 7497 7498 /* 7499 * Get "allow bus device reset" property (defaults to "enabled" if 7500 * the property was not defined). This is to disable bus resets for 7501 * certain kinds of error recovery. Note: In the future when a run-time 7502 * fibre check is available the soft state flag should default to 7503 * enabled. 7504 */ 7505 if (un->un_f_is_fibre == TRUE) { 7506 un->un_f_allow_bus_device_reset = TRUE; 7507 } else { 7508 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7509 "allow-bus-device-reset", 1) != 0) { 7510 un->un_f_allow_bus_device_reset = TRUE; 7511 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7512 "sd_unit_attach: un:0x%p Bus device reset " 7513 "enabled\n", un); 7514 } else { 7515 un->un_f_allow_bus_device_reset = FALSE; 7516 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7517 "sd_unit_attach: un:0x%p Bus device reset " 7518 "disabled\n", un); 7519 } 7520 } 7521 7522 /* 7523 * Check if this is an ATAPI device. ATAPI devices use Group 1 7524 * Read/Write commands and Group 2 Mode Sense/Select commands. 7525 * 7526 * Note: The "obsolete" way of doing this is to check for the "atapi" 7527 * property. The new "variant" property with a value of "atapi" has been 7528 * introduced so that future 'variants' of standard SCSI behavior (like 7529 * atapi) could be specified by the underlying HBA drivers by supplying 7530 * a new value for the "variant" property, instead of having to define a 7531 * new property. 7532 */ 7533 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7534 un->un_f_cfg_is_atapi = TRUE; 7535 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7536 "sd_unit_attach: un:0x%p Atapi device\n", un); 7537 } 7538 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7539 &variantp) == DDI_PROP_SUCCESS) { 7540 if (strcmp(variantp, "atapi") == 0) { 7541 un->un_f_cfg_is_atapi = TRUE; 7542 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7543 "sd_unit_attach: un:0x%p Atapi device\n", un); 7544 } 7545 ddi_prop_free(variantp); 7546 } 7547 7548 un->un_cmd_timeout = SD_IO_TIME; 7549 7550 un->un_busy_timeout = SD_BSY_TIMEOUT; 7551 7552 /* Info on current states, statuses, etc. (Updated frequently) */ 7553 un->un_state = SD_STATE_NORMAL; 7554 un->un_last_state = SD_STATE_NORMAL; 7555 7556 /* Control & status info for command throttling */ 7557 un->un_throttle = sd_max_throttle; 7558 un->un_saved_throttle = sd_max_throttle; 7559 un->un_min_throttle = sd_min_throttle; 7560 7561 if (un->un_f_is_fibre == TRUE) { 7562 un->un_f_use_adaptive_throttle = TRUE; 7563 } else { 7564 un->un_f_use_adaptive_throttle = FALSE; 7565 } 7566 7567 /* Removable media support. */ 7568 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7569 un->un_mediastate = DKIO_NONE; 7570 un->un_specified_mediastate = DKIO_NONE; 7571 7572 /* CVs for suspend/resume (PM or DR) */ 7573 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7574 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7575 7576 /* Power management support. */ 7577 un->un_power_level = SD_SPINDLE_UNINIT; 7578 7579 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 7580 un->un_f_wcc_inprog = 0; 7581 7582 /* 7583 * The open/close semaphore is used to serialize threads executing 7584 * in the driver's open & close entry point routines for a given 7585 * instance. 7586 */ 7587 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 7588 7589 /* 7590 * The conf file entry and softstate variable is a forceful override, 7591 * meaning a non-zero value must be entered to change the default. 7592 */ 7593 un->un_f_disksort_disabled = FALSE; 7594 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT; 7595 un->un_f_enable_rmw = FALSE; 7596 7597 /* 7598 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but 7599 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property. 7600 */ 7601 un->un_f_mmc_gesn_polling = TRUE; 7602 7603 /* 7604 * Retrieve the properties from the static driver table or the driver 7605 * configuration file (.conf) for this unit and update the soft state 7606 * for the device as needed for the indicated properties. 7607 * Note: the property configuration needs to occur here as some of the 7608 * following routines may have dependencies on soft state flags set 7609 * as part of the driver property configuration. 7610 */ 7611 sd_read_unit_properties(un); 7612 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7613 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 7614 7615 /* 7616 * Only if a device has "hotpluggable" property, it is 7617 * treated as hotpluggable device. Otherwise, it is 7618 * regarded as non-hotpluggable one. 7619 */ 7620 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 7621 -1) != -1) { 7622 un->un_f_is_hotpluggable = TRUE; 7623 } 7624 7625 /* 7626 * set unit's attributes(flags) according to "hotpluggable" and 7627 * RMB bit in INQUIRY data. 7628 */ 7629 sd_set_unit_attributes(un, devi); 7630 7631 /* 7632 * By default, we mark the capacity, lbasize, and geometry 7633 * as invalid. Only if we successfully read a valid capacity 7634 * will we update the un_blockcount and un_tgt_blocksize with the 7635 * valid values (the geometry will be validated later). 7636 */ 7637 un->un_f_blockcount_is_valid = FALSE; 7638 un->un_f_tgt_blocksize_is_valid = FALSE; 7639 7640 /* 7641 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 7642 * otherwise. 7643 */ 7644 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 7645 un->un_blockcount = 0; 7646 7647 /* 7648 * physical sector size default to DEV_BSIZE currently. 7649 */ 7650 un->un_phy_blocksize = DEV_BSIZE; 7651 7652 /* 7653 * Set up the per-instance info needed to determine the correct 7654 * CDBs and other info for issuing commands to the target. 7655 */ 7656 sd_init_cdb_limits(un); 7657 7658 /* 7659 * Set up the IO chains to use, based upon the target type. 7660 */ 7661 if (un->un_f_non_devbsize_supported) { 7662 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7663 } else { 7664 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7665 } 7666 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7667 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 7668 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 7669 7670 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 7671 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 7672 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 7673 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 7674 7675 7676 if (ISCD(un)) { 7677 un->un_additional_codes = sd_additional_codes; 7678 } else { 7679 un->un_additional_codes = NULL; 7680 } 7681 7682 /* 7683 * Create the kstats here so they can be available for attach-time 7684 * routines that send commands to the unit (either polled or via 7685 * sd_send_scsi_cmd). 7686 * 7687 * Note: This is a critical sequence that needs to be maintained: 7688 * 1) Instantiate the kstats here, before any routines using the 7689 * iopath (i.e. sd_send_scsi_cmd). 7690 * 2) Instantiate and initialize the partition stats 7691 * (sd_set_pstats). 7692 * 3) Initialize the error stats (sd_set_errstats), following 7693 * sd_validate_geometry(),sd_register_devid(), 7694 * and sd_cache_control(). 7695 */ 7696 7697 un->un_stats = kstat_create(sd_label, instance, 7698 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 7699 if (un->un_stats != NULL) { 7700 un->un_stats->ks_lock = SD_MUTEX(un); 7701 kstat_install(un->un_stats); 7702 } 7703 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7704 "sd_unit_attach: un:0x%p un_stats created\n", un); 7705 7706 sd_create_errstats(un, instance); 7707 if (un->un_errstats == NULL) { 7708 goto create_errstats_failed; 7709 } 7710 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7711 "sd_unit_attach: un:0x%p errstats created\n", un); 7712 7713 /* 7714 * The following if/else code was relocated here from below as part 7715 * of the fix for bug (4430280). However with the default setup added 7716 * on entry to this routine, it's no longer absolutely necessary for 7717 * this to be before the call to sd_spin_up_unit. 7718 */ 7719 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 7720 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) || 7721 (devp->sd_inq->inq_ansi == 5)) && 7722 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque; 7723 7724 /* 7725 * If tagged queueing is supported by the target 7726 * and by the host adapter then we will enable it 7727 */ 7728 un->un_tagflags = 0; 7729 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag && 7730 (un->un_f_arq_enabled == TRUE)) { 7731 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 7732 1, 1) == 1) { 7733 un->un_tagflags = FLAG_STAG; 7734 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7735 "sd_unit_attach: un:0x%p tag queueing " 7736 "enabled\n", un); 7737 } else if (scsi_ifgetcap(SD_ADDRESS(un), 7738 "untagged-qing", 0) == 1) { 7739 un->un_f_opt_queueing = TRUE; 7740 un->un_saved_throttle = un->un_throttle = 7741 min(un->un_throttle, 3); 7742 } else { 7743 un->un_f_opt_queueing = FALSE; 7744 un->un_saved_throttle = un->un_throttle = 1; 7745 } 7746 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 7747 == 1) && (un->un_f_arq_enabled == TRUE)) { 7748 /* The Host Adapter supports internal queueing. */ 7749 un->un_f_opt_queueing = TRUE; 7750 un->un_saved_throttle = un->un_throttle = 7751 min(un->un_throttle, 3); 7752 } else { 7753 un->un_f_opt_queueing = FALSE; 7754 un->un_saved_throttle = un->un_throttle = 1; 7755 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7756 "sd_unit_attach: un:0x%p no tag queueing\n", un); 7757 } 7758 7759 /* 7760 * Enable large transfers for SATA/SAS drives 7761 */ 7762 if (SD_IS_SERIAL(un)) { 7763 un->un_max_xfer_size = 7764 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7765 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7766 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7767 "sd_unit_attach: un:0x%p max transfer " 7768 "size=0x%x\n", un, un->un_max_xfer_size); 7769 7770 } 7771 7772 /* Setup or tear down default wide operations for disks */ 7773 7774 /* 7775 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 7776 * and "ssd_max_xfer_size" to exist simultaneously on the same 7777 * system and be set to different values. In the future this 7778 * code may need to be updated when the ssd module is 7779 * obsoleted and removed from the system. (4299588) 7780 */ 7781 if (SD_IS_PARALLEL_SCSI(un) && 7782 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 7783 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 7784 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7785 1, 1) == 1) { 7786 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7787 "sd_unit_attach: un:0x%p Wide Transfer " 7788 "enabled\n", un); 7789 } 7790 7791 /* 7792 * If tagged queuing has also been enabled, then 7793 * enable large xfers 7794 */ 7795 if (un->un_saved_throttle == sd_max_throttle) { 7796 un->un_max_xfer_size = 7797 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7798 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7799 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7800 "sd_unit_attach: un:0x%p max transfer " 7801 "size=0x%x\n", un, un->un_max_xfer_size); 7802 } 7803 } else { 7804 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7805 0, 1) == 1) { 7806 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7807 "sd_unit_attach: un:0x%p " 7808 "Wide Transfer disabled\n", un); 7809 } 7810 } 7811 } else { 7812 un->un_tagflags = FLAG_STAG; 7813 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 7814 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 7815 } 7816 7817 /* 7818 * If this target supports LUN reset, try to enable it. 7819 */ 7820 if (un->un_f_lun_reset_enabled) { 7821 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 7822 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7823 "un:0x%p lun_reset capability set\n", un); 7824 } else { 7825 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7826 "un:0x%p lun-reset capability not set\n", un); 7827 } 7828 } 7829 7830 /* 7831 * Adjust the maximum transfer size. This is to fix 7832 * the problem of partial DMA support on SPARC. Some 7833 * HBA driver, like aac, has very small dma_attr_maxxfer 7834 * size, which requires partial DMA support on SPARC. 7835 * In the future the SPARC pci nexus driver may solve 7836 * the problem instead of this fix. 7837 */ 7838 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 7839 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 7840 /* We need DMA partial even on sparc to ensure sddump() works */ 7841 un->un_max_xfer_size = max_xfer_size; 7842 if (un->un_partial_dma_supported == 0) 7843 un->un_partial_dma_supported = 1; 7844 } 7845 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7846 DDI_PROP_DONTPASS, "buf_break", 0) == 1) { 7847 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr, 7848 un->un_max_xfer_size) == 1) { 7849 un->un_buf_breakup_supported = 1; 7850 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7851 "un:0x%p Buf breakup enabled\n", un); 7852 } 7853 } 7854 7855 /* 7856 * Set PKT_DMA_PARTIAL flag. 7857 */ 7858 if (un->un_partial_dma_supported == 1) { 7859 un->un_pkt_flags = PKT_DMA_PARTIAL; 7860 } else { 7861 un->un_pkt_flags = 0; 7862 } 7863 7864 /* Initialize sd_ssc_t for internal uscsi commands */ 7865 ssc = sd_ssc_init(un); 7866 scsi_fm_init(devp); 7867 7868 /* 7869 * Allocate memory for SCSI FMA stuffs. 7870 */ 7871 un->un_fm_private = 7872 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP); 7873 sfip = (struct sd_fm_internal *)un->un_fm_private; 7874 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd; 7875 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo; 7876 sfip->fm_ssc.ssc_un = un; 7877 7878 if (ISCD(un) || 7879 un->un_f_has_removable_media || 7880 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) { 7881 /* 7882 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device. 7883 * Their log are unchanged. 7884 */ 7885 sfip->fm_log_level = SD_FM_LOG_NSUP; 7886 } else { 7887 /* 7888 * If enter here, it should be non-CDROM and FM-capable 7889 * device, and it will not keep the old scsi_log as before 7890 * in /var/adm/messages. However, the property 7891 * "fm-scsi-log" will control whether the FM telemetry will 7892 * be logged in /var/adm/messages. 7893 */ 7894 int fm_scsi_log; 7895 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7896 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0); 7897 7898 if (fm_scsi_log) 7899 sfip->fm_log_level = SD_FM_LOG_EREPORT; 7900 else 7901 sfip->fm_log_level = SD_FM_LOG_SILENT; 7902 } 7903 7904 /* 7905 * At this point in the attach, we have enough info in the 7906 * soft state to be able to issue commands to the target. 7907 * 7908 * All command paths used below MUST issue their commands as 7909 * SD_PATH_DIRECT. This is important as intermediate layers 7910 * are not all initialized yet (such as PM). 7911 */ 7912 7913 /* 7914 * Send a TEST UNIT READY command to the device. This should clear 7915 * any outstanding UNIT ATTENTION that may be present. 7916 * 7917 * Note: Don't check for success, just track if there is a reservation, 7918 * this is a throw away command to clear any unit attentions. 7919 * 7920 * Note: This MUST be the first command issued to the target during 7921 * attach to ensure power on UNIT ATTENTIONS are cleared. 7922 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7923 * with attempts at spinning up a device with no media. 7924 */ 7925 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 7926 if (status != 0) { 7927 if (status == EACCES) 7928 reservation_flag = SD_TARGET_IS_RESERVED; 7929 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7930 } 7931 7932 /* 7933 * If the device is NOT a removable media device, attempt to spin 7934 * it up (using the START_STOP_UNIT command) and read its capacity 7935 * (using the READ CAPACITY command). Note, however, that either 7936 * of these could fail and in some cases we would continue with 7937 * the attach despite the failure (see below). 7938 */ 7939 if (un->un_f_descr_format_supported) { 7940 7941 switch (sd_spin_up_unit(ssc)) { 7942 case 0: 7943 /* 7944 * Spin-up was successful; now try to read the 7945 * capacity. If successful then save the results 7946 * and mark the capacity & lbasize as valid. 7947 */ 7948 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7949 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7950 7951 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 7952 &lbasize, SD_PATH_DIRECT); 7953 7954 switch (status) { 7955 case 0: { 7956 if (capacity > DK_MAX_BLOCKS) { 7957 #ifdef _LP64 7958 if ((capacity + 1) > 7959 SD_GROUP1_MAX_ADDRESS) { 7960 /* 7961 * Enable descriptor format 7962 * sense data so that we can 7963 * get 64 bit sense data 7964 * fields. 7965 */ 7966 sd_enable_descr_sense(ssc); 7967 } 7968 #else 7969 /* 32-bit kernels can't handle this */ 7970 scsi_log(SD_DEVINFO(un), 7971 sd_label, CE_WARN, 7972 "disk has %llu blocks, which " 7973 "is too large for a 32-bit " 7974 "kernel", capacity); 7975 7976 #if defined(__i386) || defined(__amd64) 7977 /* 7978 * 1TB disk was treated as (1T - 512)B 7979 * in the past, so that it might have 7980 * valid VTOC and solaris partitions, 7981 * we have to allow it to continue to 7982 * work. 7983 */ 7984 if (capacity -1 > DK_MAX_BLOCKS) 7985 #endif 7986 goto spinup_failed; 7987 #endif 7988 } 7989 7990 /* 7991 * Here it's not necessary to check the case: 7992 * the capacity of the device is bigger than 7993 * what the max hba cdb can support. Because 7994 * sd_send_scsi_READ_CAPACITY will retrieve 7995 * the capacity by sending USCSI command, which 7996 * is constrained by the max hba cdb. Actually, 7997 * sd_send_scsi_READ_CAPACITY will return 7998 * EINVAL when using bigger cdb than required 7999 * cdb length. Will handle this case in 8000 * "case EINVAL". 8001 */ 8002 8003 /* 8004 * The following relies on 8005 * sd_send_scsi_READ_CAPACITY never 8006 * returning 0 for capacity and/or lbasize. 8007 */ 8008 sd_update_block_info(un, lbasize, capacity); 8009 8010 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8011 "sd_unit_attach: un:0x%p capacity = %ld " 8012 "blocks; lbasize= %ld.\n", un, 8013 un->un_blockcount, un->un_tgt_blocksize); 8014 8015 break; 8016 } 8017 case EINVAL: 8018 /* 8019 * In the case where the max-cdb-length property 8020 * is smaller than the required CDB length for 8021 * a SCSI device, a target driver can fail to 8022 * attach to that device. 8023 */ 8024 scsi_log(SD_DEVINFO(un), 8025 sd_label, CE_WARN, 8026 "disk capacity is too large " 8027 "for current cdb length"); 8028 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8029 8030 goto spinup_failed; 8031 case EACCES: 8032 /* 8033 * Should never get here if the spin-up 8034 * succeeded, but code it in anyway. 8035 * From here, just continue with the attach... 8036 */ 8037 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8038 "sd_unit_attach: un:0x%p " 8039 "sd_send_scsi_READ_CAPACITY " 8040 "returned reservation conflict\n", un); 8041 reservation_flag = SD_TARGET_IS_RESERVED; 8042 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8043 break; 8044 default: 8045 /* 8046 * Likewise, should never get here if the 8047 * spin-up succeeded. Just continue with 8048 * the attach... 8049 */ 8050 if (status == EIO) 8051 sd_ssc_assessment(ssc, 8052 SD_FMT_STATUS_CHECK); 8053 else 8054 sd_ssc_assessment(ssc, 8055 SD_FMT_IGNORE); 8056 break; 8057 } 8058 break; 8059 case EACCES: 8060 /* 8061 * Device is reserved by another host. In this case 8062 * we could not spin it up or read the capacity, but 8063 * we continue with the attach anyway. 8064 */ 8065 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8066 "sd_unit_attach: un:0x%p spin-up reservation " 8067 "conflict.\n", un); 8068 reservation_flag = SD_TARGET_IS_RESERVED; 8069 break; 8070 default: 8071 /* Fail the attach if the spin-up failed. */ 8072 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8073 "sd_unit_attach: un:0x%p spin-up failed.", un); 8074 goto spinup_failed; 8075 } 8076 8077 } 8078 8079 /* 8080 * Check to see if this is a MMC drive 8081 */ 8082 if (ISCD(un)) { 8083 sd_set_mmc_caps(ssc); 8084 } 8085 8086 /* 8087 * Add a zero-length attribute to tell the world we support 8088 * kernel ioctls (for layered drivers) 8089 */ 8090 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8091 DDI_KERNEL_IOCTL, NULL, 0); 8092 8093 /* 8094 * Add a boolean property to tell the world we support 8095 * the B_FAILFAST flag (for layered drivers) 8096 */ 8097 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8098 "ddi-failfast-supported", NULL, 0); 8099 8100 /* 8101 * Initialize power management 8102 */ 8103 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8104 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8105 sd_setup_pm(ssc, devi); 8106 if (un->un_f_pm_is_enabled == FALSE) { 8107 /* 8108 * For performance, point to a jump table that does 8109 * not include pm. 8110 * The direct and priority chains don't change with PM. 8111 * 8112 * Note: this is currently done based on individual device 8113 * capabilities. When an interface for determining system 8114 * power enabled state becomes available, or when additional 8115 * layers are added to the command chain, these values will 8116 * have to be re-evaluated for correctness. 8117 */ 8118 if (un->un_f_non_devbsize_supported) { 8119 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8120 } else { 8121 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8122 } 8123 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8124 } 8125 8126 /* 8127 * This property is set to 0 by HA software to avoid retries 8128 * on a reserved disk. (The preferred property name is 8129 * "retry-on-reservation-conflict") (1189689) 8130 * 8131 * Note: The use of a global here can have unintended consequences. A 8132 * per instance variable is preferable to match the capabilities of 8133 * different underlying hba's (4402600) 8134 */ 8135 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8136 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8137 sd_retry_on_reservation_conflict); 8138 if (sd_retry_on_reservation_conflict != 0) { 8139 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8140 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8141 sd_retry_on_reservation_conflict); 8142 } 8143 8144 /* Set up options for QFULL handling. */ 8145 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8146 "qfull-retries", -1)) != -1) { 8147 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8148 rval, 1); 8149 } 8150 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8151 "qfull-retry-interval", -1)) != -1) { 8152 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8153 rval, 1); 8154 } 8155 8156 /* 8157 * This just prints a message that announces the existence of the 8158 * device. The message is always printed in the system logfile, but 8159 * only appears on the console if the system is booted with the 8160 * -v (verbose) argument. 8161 */ 8162 ddi_report_dev(devi); 8163 8164 un->un_mediastate = DKIO_NONE; 8165 8166 /* 8167 * Check if this is a SSD(Solid State Drive). 8168 */ 8169 sd_check_solid_state(ssc); 8170 8171 /* 8172 * Check whether the drive is in emulation mode. 8173 */ 8174 sd_check_emulation_mode(ssc); 8175 8176 cmlb_alloc_handle(&un->un_cmlbhandle); 8177 8178 #if defined(__i386) || defined(__amd64) 8179 /* 8180 * On x86, compensate for off-by-1 legacy error 8181 */ 8182 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 8183 (lbasize == un->un_sys_blocksize)) 8184 offbyone = CMLB_OFF_BY_ONE; 8185 #endif 8186 8187 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 8188 VOID2BOOLEAN(un->un_f_has_removable_media != 0), 8189 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0), 8190 un->un_node_type, offbyone, un->un_cmlbhandle, 8191 (void *)SD_PATH_DIRECT) != 0) { 8192 goto cmlb_attach_failed; 8193 } 8194 8195 8196 /* 8197 * Read and validate the device's geometry (ie, disk label) 8198 * A new unformatted drive will not have a valid geometry, but 8199 * the driver needs to successfully attach to this device so 8200 * the drive can be formatted via ioctls. 8201 */ 8202 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 8203 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 8204 8205 mutex_enter(SD_MUTEX(un)); 8206 8207 /* 8208 * Read and initialize the devid for the unit. 8209 */ 8210 if (un->un_f_devid_supported) { 8211 sd_register_devid(ssc, devi, reservation_flag); 8212 } 8213 mutex_exit(SD_MUTEX(un)); 8214 8215 #if (defined(__fibre)) 8216 /* 8217 * Register callbacks for fibre only. You can't do this solely 8218 * on the basis of the devid_type because this is hba specific. 8219 * We need to query our hba capabilities to find out whether to 8220 * register or not. 8221 */ 8222 if (un->un_f_is_fibre) { 8223 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8224 sd_init_event_callbacks(un); 8225 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8226 "sd_unit_attach: un:0x%p event callbacks inserted", 8227 un); 8228 } 8229 } 8230 #endif 8231 8232 if (un->un_f_opt_disable_cache == TRUE) { 8233 /* 8234 * Disable both read cache and write cache. This is 8235 * the historic behavior of the keywords in the config file. 8236 */ 8237 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8238 0) { 8239 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8240 "sd_unit_attach: un:0x%p Could not disable " 8241 "caching", un); 8242 goto devid_failed; 8243 } 8244 } 8245 8246 /* 8247 * Check the value of the WCE bit now and 8248 * set un_f_write_cache_enabled accordingly. 8249 */ 8250 (void) sd_get_write_cache_enabled(ssc, &wc_enabled); 8251 mutex_enter(SD_MUTEX(un)); 8252 un->un_f_write_cache_enabled = (wc_enabled != 0); 8253 mutex_exit(SD_MUTEX(un)); 8254 8255 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR && 8256 un->un_tgt_blocksize != DEV_BSIZE) || 8257 un->un_f_enable_rmw) { 8258 if (!(un->un_wm_cache)) { 8259 (void) snprintf(name_str, sizeof (name_str), 8260 "%s%d_cache", 8261 ddi_driver_name(SD_DEVINFO(un)), 8262 ddi_get_instance(SD_DEVINFO(un))); 8263 un->un_wm_cache = kmem_cache_create( 8264 name_str, sizeof (struct sd_w_map), 8265 8, sd_wm_cache_constructor, 8266 sd_wm_cache_destructor, NULL, 8267 (void *)un, NULL, 0); 8268 if (!(un->un_wm_cache)) { 8269 goto wm_cache_failed; 8270 } 8271 } 8272 } 8273 8274 /* 8275 * Check the value of the NV_SUP bit and set 8276 * un_f_suppress_cache_flush accordingly. 8277 */ 8278 sd_get_nv_sup(ssc); 8279 8280 /* 8281 * Find out what type of reservation this disk supports. 8282 */ 8283 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL); 8284 8285 switch (status) { 8286 case 0: 8287 /* 8288 * SCSI-3 reservations are supported. 8289 */ 8290 un->un_reservation_type = SD_SCSI3_RESERVATION; 8291 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8292 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8293 break; 8294 case ENOTSUP: 8295 /* 8296 * The PERSISTENT RESERVE IN command would not be recognized by 8297 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8298 */ 8299 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8300 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8301 un->un_reservation_type = SD_SCSI2_RESERVATION; 8302 8303 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8304 break; 8305 default: 8306 /* 8307 * default to SCSI-3 reservations 8308 */ 8309 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8310 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8311 un->un_reservation_type = SD_SCSI3_RESERVATION; 8312 8313 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8314 break; 8315 } 8316 8317 /* 8318 * Set the pstat and error stat values here, so data obtained during the 8319 * previous attach-time routines is available. 8320 * 8321 * Note: This is a critical sequence that needs to be maintained: 8322 * 1) Instantiate the kstats before any routines using the iopath 8323 * (i.e. sd_send_scsi_cmd). 8324 * 2) Initialize the error stats (sd_set_errstats) and partition 8325 * stats (sd_set_pstats)here, following 8326 * cmlb_validate_geometry(), sd_register_devid(), and 8327 * sd_cache_control(). 8328 */ 8329 8330 if (un->un_f_pkstats_enabled && geom_label_valid) { 8331 sd_set_pstats(un); 8332 SD_TRACE(SD_LOG_IO_PARTITION, un, 8333 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8334 } 8335 8336 sd_set_errstats(un); 8337 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8338 "sd_unit_attach: un:0x%p errstats set\n", un); 8339 8340 8341 /* 8342 * After successfully attaching an instance, we record the information 8343 * of how many luns have been attached on the relative target and 8344 * controller for parallel SCSI. This information is used when sd tries 8345 * to set the tagged queuing capability in HBA. 8346 */ 8347 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8348 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 8349 } 8350 8351 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8352 "sd_unit_attach: un:0x%p exit success\n", un); 8353 8354 /* Uninitialize sd_ssc_t pointer */ 8355 sd_ssc_fini(ssc); 8356 8357 return (DDI_SUCCESS); 8358 8359 /* 8360 * An error occurred during the attach; clean up & return failure. 8361 */ 8362 wm_cache_failed: 8363 devid_failed: 8364 8365 setup_pm_failed: 8366 ddi_remove_minor_node(devi, NULL); 8367 8368 cmlb_attach_failed: 8369 /* 8370 * Cleanup from the scsi_ifsetcap() calls (437868) 8371 */ 8372 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8373 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8374 8375 /* 8376 * Refer to the comments of setting tagged-qing in the beginning of 8377 * sd_unit_attach. We can only disable tagged queuing when there is 8378 * no lun attached on the target. 8379 */ 8380 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8381 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8382 } 8383 8384 if (un->un_f_is_fibre == FALSE) { 8385 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8386 } 8387 8388 spinup_failed: 8389 8390 /* Uninitialize sd_ssc_t pointer */ 8391 sd_ssc_fini(ssc); 8392 8393 mutex_enter(SD_MUTEX(un)); 8394 8395 /* Deallocate SCSI FMA memory spaces */ 8396 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8397 8398 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8399 if (un->un_direct_priority_timeid != NULL) { 8400 timeout_id_t temp_id = un->un_direct_priority_timeid; 8401 un->un_direct_priority_timeid = NULL; 8402 mutex_exit(SD_MUTEX(un)); 8403 (void) untimeout(temp_id); 8404 mutex_enter(SD_MUTEX(un)); 8405 } 8406 8407 /* Cancel any pending start/stop timeouts */ 8408 if (un->un_startstop_timeid != NULL) { 8409 timeout_id_t temp_id = un->un_startstop_timeid; 8410 un->un_startstop_timeid = NULL; 8411 mutex_exit(SD_MUTEX(un)); 8412 (void) untimeout(temp_id); 8413 mutex_enter(SD_MUTEX(un)); 8414 } 8415 8416 /* Cancel any pending reset-throttle timeouts */ 8417 if (un->un_reset_throttle_timeid != NULL) { 8418 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8419 un->un_reset_throttle_timeid = NULL; 8420 mutex_exit(SD_MUTEX(un)); 8421 (void) untimeout(temp_id); 8422 mutex_enter(SD_MUTEX(un)); 8423 } 8424 8425 /* Cancel rmw warning message timeouts */ 8426 if (un->un_rmw_msg_timeid != NULL) { 8427 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8428 un->un_rmw_msg_timeid = NULL; 8429 mutex_exit(SD_MUTEX(un)); 8430 (void) untimeout(temp_id); 8431 mutex_enter(SD_MUTEX(un)); 8432 } 8433 8434 /* Cancel any pending retry timeouts */ 8435 if (un->un_retry_timeid != NULL) { 8436 timeout_id_t temp_id = un->un_retry_timeid; 8437 un->un_retry_timeid = NULL; 8438 mutex_exit(SD_MUTEX(un)); 8439 (void) untimeout(temp_id); 8440 mutex_enter(SD_MUTEX(un)); 8441 } 8442 8443 /* Cancel any pending delayed cv broadcast timeouts */ 8444 if (un->un_dcvb_timeid != NULL) { 8445 timeout_id_t temp_id = un->un_dcvb_timeid; 8446 un->un_dcvb_timeid = NULL; 8447 mutex_exit(SD_MUTEX(un)); 8448 (void) untimeout(temp_id); 8449 mutex_enter(SD_MUTEX(un)); 8450 } 8451 8452 mutex_exit(SD_MUTEX(un)); 8453 8454 /* There should not be any in-progress I/O so ASSERT this check */ 8455 ASSERT(un->un_ncmds_in_transport == 0); 8456 ASSERT(un->un_ncmds_in_driver == 0); 8457 8458 /* Do not free the softstate if the callback routine is active */ 8459 sd_sync_with_callback(un); 8460 8461 /* 8462 * Partition stats apparently are not used with removables. These would 8463 * not have been created during attach, so no need to clean them up... 8464 */ 8465 if (un->un_errstats != NULL) { 8466 kstat_delete(un->un_errstats); 8467 un->un_errstats = NULL; 8468 } 8469 8470 create_errstats_failed: 8471 8472 if (un->un_stats != NULL) { 8473 kstat_delete(un->un_stats); 8474 un->un_stats = NULL; 8475 } 8476 8477 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8478 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8479 8480 ddi_prop_remove_all(devi); 8481 sema_destroy(&un->un_semoclose); 8482 cv_destroy(&un->un_state_cv); 8483 8484 getrbuf_failed: 8485 8486 sd_free_rqs(un); 8487 8488 alloc_rqs_failed: 8489 8490 devp->sd_private = NULL; 8491 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8492 8493 get_softstate_failed: 8494 /* 8495 * Note: the man pages are unclear as to whether or not doing a 8496 * ddi_soft_state_free(sd_state, instance) is the right way to 8497 * clean up after the ddi_soft_state_zalloc() if the subsequent 8498 * ddi_get_soft_state() fails. The implication seems to be 8499 * that the get_soft_state cannot fail if the zalloc succeeds. 8500 */ 8501 #ifndef XPV_HVM_DRIVER 8502 ddi_soft_state_free(sd_state, instance); 8503 #endif /* !XPV_HVM_DRIVER */ 8504 8505 probe_failed: 8506 scsi_unprobe(devp); 8507 8508 return (DDI_FAILURE); 8509 } 8510 8511 8512 /* 8513 * Function: sd_unit_detach 8514 * 8515 * Description: Performs DDI_DETACH processing for sddetach(). 8516 * 8517 * Return Code: DDI_SUCCESS 8518 * DDI_FAILURE 8519 * 8520 * Context: Kernel thread context 8521 */ 8522 8523 static int 8524 sd_unit_detach(dev_info_t *devi) 8525 { 8526 struct scsi_device *devp; 8527 struct sd_lun *un; 8528 int i; 8529 int tgt; 8530 dev_t dev; 8531 dev_info_t *pdip = ddi_get_parent(devi); 8532 #ifndef XPV_HVM_DRIVER 8533 int instance = ddi_get_instance(devi); 8534 #endif /* !XPV_HVM_DRIVER */ 8535 8536 mutex_enter(&sd_detach_mutex); 8537 8538 /* 8539 * Fail the detach for any of the following: 8540 * - Unable to get the sd_lun struct for the instance 8541 * - A layered driver has an outstanding open on the instance 8542 * - Another thread is already detaching this instance 8543 * - Another thread is currently performing an open 8544 */ 8545 devp = ddi_get_driver_private(devi); 8546 if ((devp == NULL) || 8547 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8548 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8549 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8550 mutex_exit(&sd_detach_mutex); 8551 return (DDI_FAILURE); 8552 } 8553 8554 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8555 8556 /* 8557 * Mark this instance as currently in a detach, to inhibit any 8558 * opens from a layered driver. 8559 */ 8560 un->un_detach_count++; 8561 mutex_exit(&sd_detach_mutex); 8562 8563 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8564 SCSI_ADDR_PROP_TARGET, -1); 8565 8566 dev = sd_make_device(SD_DEVINFO(un)); 8567 8568 #ifndef lint 8569 _NOTE(COMPETING_THREADS_NOW); 8570 #endif 8571 8572 mutex_enter(SD_MUTEX(un)); 8573 8574 /* 8575 * Fail the detach if there are any outstanding layered 8576 * opens on this device. 8577 */ 8578 for (i = 0; i < NDKMAP; i++) { 8579 if (un->un_ocmap.lyropen[i] != 0) { 8580 goto err_notclosed; 8581 } 8582 } 8583 8584 /* 8585 * Verify there are NO outstanding commands issued to this device. 8586 * ie, un_ncmds_in_transport == 0. 8587 * It's possible to have outstanding commands through the physio 8588 * code path, even though everything's closed. 8589 */ 8590 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8591 (un->un_direct_priority_timeid != NULL) || 8592 (un->un_state == SD_STATE_RWAIT)) { 8593 mutex_exit(SD_MUTEX(un)); 8594 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8595 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8596 goto err_stillbusy; 8597 } 8598 8599 /* 8600 * If we have the device reserved, release the reservation. 8601 */ 8602 if ((un->un_resvd_status & SD_RESERVE) && 8603 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8604 mutex_exit(SD_MUTEX(un)); 8605 /* 8606 * Note: sd_reserve_release sends a command to the device 8607 * via the sd_ioctlcmd() path, and can sleep. 8608 */ 8609 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8610 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8611 "sd_dr_detach: Cannot release reservation \n"); 8612 } 8613 } else { 8614 mutex_exit(SD_MUTEX(un)); 8615 } 8616 8617 /* 8618 * Untimeout any reserve recover, throttle reset, restart unit 8619 * and delayed broadcast timeout threads. Protect the timeout pointer 8620 * from getting nulled by their callback functions. 8621 */ 8622 mutex_enter(SD_MUTEX(un)); 8623 if (un->un_resvd_timeid != NULL) { 8624 timeout_id_t temp_id = un->un_resvd_timeid; 8625 un->un_resvd_timeid = NULL; 8626 mutex_exit(SD_MUTEX(un)); 8627 (void) untimeout(temp_id); 8628 mutex_enter(SD_MUTEX(un)); 8629 } 8630 8631 if (un->un_reset_throttle_timeid != NULL) { 8632 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8633 un->un_reset_throttle_timeid = NULL; 8634 mutex_exit(SD_MUTEX(un)); 8635 (void) untimeout(temp_id); 8636 mutex_enter(SD_MUTEX(un)); 8637 } 8638 8639 if (un->un_startstop_timeid != NULL) { 8640 timeout_id_t temp_id = un->un_startstop_timeid; 8641 un->un_startstop_timeid = NULL; 8642 mutex_exit(SD_MUTEX(un)); 8643 (void) untimeout(temp_id); 8644 mutex_enter(SD_MUTEX(un)); 8645 } 8646 8647 if (un->un_rmw_msg_timeid != NULL) { 8648 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8649 un->un_rmw_msg_timeid = NULL; 8650 mutex_exit(SD_MUTEX(un)); 8651 (void) untimeout(temp_id); 8652 mutex_enter(SD_MUTEX(un)); 8653 } 8654 8655 if (un->un_dcvb_timeid != NULL) { 8656 timeout_id_t temp_id = un->un_dcvb_timeid; 8657 un->un_dcvb_timeid = NULL; 8658 mutex_exit(SD_MUTEX(un)); 8659 (void) untimeout(temp_id); 8660 } else { 8661 mutex_exit(SD_MUTEX(un)); 8662 } 8663 8664 /* Remove any pending reservation reclaim requests for this device */ 8665 sd_rmv_resv_reclaim_req(dev); 8666 8667 mutex_enter(SD_MUTEX(un)); 8668 8669 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8670 if (un->un_direct_priority_timeid != NULL) { 8671 timeout_id_t temp_id = un->un_direct_priority_timeid; 8672 un->un_direct_priority_timeid = NULL; 8673 mutex_exit(SD_MUTEX(un)); 8674 (void) untimeout(temp_id); 8675 mutex_enter(SD_MUTEX(un)); 8676 } 8677 8678 /* Cancel any active multi-host disk watch thread requests */ 8679 if (un->un_mhd_token != NULL) { 8680 mutex_exit(SD_MUTEX(un)); 8681 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8682 if (scsi_watch_request_terminate(un->un_mhd_token, 8683 SCSI_WATCH_TERMINATE_NOWAIT)) { 8684 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8685 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8686 /* 8687 * Note: We are returning here after having removed 8688 * some driver timeouts above. This is consistent with 8689 * the legacy implementation but perhaps the watch 8690 * terminate call should be made with the wait flag set. 8691 */ 8692 goto err_stillbusy; 8693 } 8694 mutex_enter(SD_MUTEX(un)); 8695 un->un_mhd_token = NULL; 8696 } 8697 8698 if (un->un_swr_token != NULL) { 8699 mutex_exit(SD_MUTEX(un)); 8700 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8701 if (scsi_watch_request_terminate(un->un_swr_token, 8702 SCSI_WATCH_TERMINATE_NOWAIT)) { 8703 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8704 "sd_dr_detach: Cannot cancel swr watch request\n"); 8705 /* 8706 * Note: We are returning here after having removed 8707 * some driver timeouts above. This is consistent with 8708 * the legacy implementation but perhaps the watch 8709 * terminate call should be made with the wait flag set. 8710 */ 8711 goto err_stillbusy; 8712 } 8713 mutex_enter(SD_MUTEX(un)); 8714 un->un_swr_token = NULL; 8715 } 8716 8717 mutex_exit(SD_MUTEX(un)); 8718 8719 /* 8720 * Clear any scsi_reset_notifies. We clear the reset notifies 8721 * if we have not registered one. 8722 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8723 */ 8724 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8725 sd_mhd_reset_notify_cb, (caddr_t)un); 8726 8727 /* 8728 * protect the timeout pointers from getting nulled by 8729 * their callback functions during the cancellation process. 8730 * In such a scenario untimeout can be invoked with a null value. 8731 */ 8732 _NOTE(NO_COMPETING_THREADS_NOW); 8733 8734 mutex_enter(&un->un_pm_mutex); 8735 if (un->un_pm_idle_timeid != NULL) { 8736 timeout_id_t temp_id = un->un_pm_idle_timeid; 8737 un->un_pm_idle_timeid = NULL; 8738 mutex_exit(&un->un_pm_mutex); 8739 8740 /* 8741 * Timeout is active; cancel it. 8742 * Note that it'll never be active on a device 8743 * that does not support PM therefore we don't 8744 * have to check before calling pm_idle_component. 8745 */ 8746 (void) untimeout(temp_id); 8747 (void) pm_idle_component(SD_DEVINFO(un), 0); 8748 mutex_enter(&un->un_pm_mutex); 8749 } 8750 8751 /* 8752 * Check whether there is already a timeout scheduled for power 8753 * management. If yes then don't lower the power here, that's. 8754 * the timeout handler's job. 8755 */ 8756 if (un->un_pm_timeid != NULL) { 8757 timeout_id_t temp_id = un->un_pm_timeid; 8758 un->un_pm_timeid = NULL; 8759 mutex_exit(&un->un_pm_mutex); 8760 /* 8761 * Timeout is active; cancel it. 8762 * Note that it'll never be active on a device 8763 * that does not support PM therefore we don't 8764 * have to check before calling pm_idle_component. 8765 */ 8766 (void) untimeout(temp_id); 8767 (void) pm_idle_component(SD_DEVINFO(un), 0); 8768 8769 } else { 8770 mutex_exit(&un->un_pm_mutex); 8771 if ((un->un_f_pm_is_enabled == TRUE) && 8772 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un)) 8773 != DDI_SUCCESS)) { 8774 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8775 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8776 /* 8777 * Fix for bug: 4297749, item # 13 8778 * The above test now includes a check to see if PM is 8779 * supported by this device before call 8780 * pm_lower_power(). 8781 * Note, the following is not dead code. The call to 8782 * pm_lower_power above will generate a call back into 8783 * our sdpower routine which might result in a timeout 8784 * handler getting activated. Therefore the following 8785 * code is valid and necessary. 8786 */ 8787 mutex_enter(&un->un_pm_mutex); 8788 if (un->un_pm_timeid != NULL) { 8789 timeout_id_t temp_id = un->un_pm_timeid; 8790 un->un_pm_timeid = NULL; 8791 mutex_exit(&un->un_pm_mutex); 8792 (void) untimeout(temp_id); 8793 (void) pm_idle_component(SD_DEVINFO(un), 0); 8794 } else { 8795 mutex_exit(&un->un_pm_mutex); 8796 } 8797 } 8798 } 8799 8800 /* 8801 * Cleanup from the scsi_ifsetcap() calls (437868) 8802 * Relocated here from above to be after the call to 8803 * pm_lower_power, which was getting errors. 8804 */ 8805 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8806 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8807 8808 /* 8809 * Currently, tagged queuing is supported per target based by HBA. 8810 * Setting this per lun instance actually sets the capability of this 8811 * target in HBA, which affects those luns already attached on the 8812 * same target. So during detach, we can only disable this capability 8813 * only when this is the only lun left on this target. By doing 8814 * this, we assume a target has the same tagged queuing capability 8815 * for every lun. The condition can be removed when HBA is changed to 8816 * support per lun based tagged queuing capability. 8817 */ 8818 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 8819 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8820 } 8821 8822 if (un->un_f_is_fibre == FALSE) { 8823 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8824 } 8825 8826 /* 8827 * Remove any event callbacks, fibre only 8828 */ 8829 if (un->un_f_is_fibre == TRUE) { 8830 if ((un->un_insert_event != NULL) && 8831 (ddi_remove_event_handler(un->un_insert_cb_id) != 8832 DDI_SUCCESS)) { 8833 /* 8834 * Note: We are returning here after having done 8835 * substantial cleanup above. This is consistent 8836 * with the legacy implementation but this may not 8837 * be the right thing to do. 8838 */ 8839 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8840 "sd_dr_detach: Cannot cancel insert event\n"); 8841 goto err_remove_event; 8842 } 8843 un->un_insert_event = NULL; 8844 8845 if ((un->un_remove_event != NULL) && 8846 (ddi_remove_event_handler(un->un_remove_cb_id) != 8847 DDI_SUCCESS)) { 8848 /* 8849 * Note: We are returning here after having done 8850 * substantial cleanup above. This is consistent 8851 * with the legacy implementation but this may not 8852 * be the right thing to do. 8853 */ 8854 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8855 "sd_dr_detach: Cannot cancel remove event\n"); 8856 goto err_remove_event; 8857 } 8858 un->un_remove_event = NULL; 8859 } 8860 8861 /* Do not free the softstate if the callback routine is active */ 8862 sd_sync_with_callback(un); 8863 8864 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 8865 cmlb_free_handle(&un->un_cmlbhandle); 8866 8867 /* 8868 * Hold the detach mutex here, to make sure that no other threads ever 8869 * can access a (partially) freed soft state structure. 8870 */ 8871 mutex_enter(&sd_detach_mutex); 8872 8873 /* 8874 * Clean up the soft state struct. 8875 * Cleanup is done in reverse order of allocs/inits. 8876 * At this point there should be no competing threads anymore. 8877 */ 8878 8879 scsi_fm_fini(devp); 8880 8881 /* 8882 * Deallocate memory for SCSI FMA. 8883 */ 8884 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8885 8886 /* 8887 * Unregister and free device id if it was not registered 8888 * by the transport. 8889 */ 8890 if (un->un_f_devid_transport_defined == FALSE) 8891 ddi_devid_unregister(devi); 8892 8893 /* 8894 * free the devid structure if allocated before (by ddi_devid_init() 8895 * or ddi_devid_get()). 8896 */ 8897 if (un->un_devid) { 8898 ddi_devid_free(un->un_devid); 8899 un->un_devid = NULL; 8900 } 8901 8902 /* 8903 * Destroy wmap cache if it exists. 8904 */ 8905 if (un->un_wm_cache != NULL) { 8906 kmem_cache_destroy(un->un_wm_cache); 8907 un->un_wm_cache = NULL; 8908 } 8909 8910 /* 8911 * kstat cleanup is done in detach for all device types (4363169). 8912 * We do not want to fail detach if the device kstats are not deleted 8913 * since there is a confusion about the devo_refcnt for the device. 8914 * We just delete the kstats and let detach complete successfully. 8915 */ 8916 if (un->un_stats != NULL) { 8917 kstat_delete(un->un_stats); 8918 un->un_stats = NULL; 8919 } 8920 if (un->un_errstats != NULL) { 8921 kstat_delete(un->un_errstats); 8922 un->un_errstats = NULL; 8923 } 8924 8925 /* Remove partition stats */ 8926 if (un->un_f_pkstats_enabled) { 8927 for (i = 0; i < NSDMAP; i++) { 8928 if (un->un_pstats[i] != NULL) { 8929 kstat_delete(un->un_pstats[i]); 8930 un->un_pstats[i] = NULL; 8931 } 8932 } 8933 } 8934 8935 /* Remove xbuf registration */ 8936 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8937 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8938 8939 /* Remove driver properties */ 8940 ddi_prop_remove_all(devi); 8941 8942 mutex_destroy(&un->un_pm_mutex); 8943 cv_destroy(&un->un_pm_busy_cv); 8944 8945 cv_destroy(&un->un_wcc_cv); 8946 8947 /* Open/close semaphore */ 8948 sema_destroy(&un->un_semoclose); 8949 8950 /* Removable media condvar. */ 8951 cv_destroy(&un->un_state_cv); 8952 8953 /* Suspend/resume condvar. */ 8954 cv_destroy(&un->un_suspend_cv); 8955 cv_destroy(&un->un_disk_busy_cv); 8956 8957 sd_free_rqs(un); 8958 8959 /* Free up soft state */ 8960 devp->sd_private = NULL; 8961 8962 bzero(un, sizeof (struct sd_lun)); 8963 #ifndef XPV_HVM_DRIVER 8964 ddi_soft_state_free(sd_state, instance); 8965 #endif /* !XPV_HVM_DRIVER */ 8966 8967 mutex_exit(&sd_detach_mutex); 8968 8969 /* This frees up the INQUIRY data associated with the device. */ 8970 scsi_unprobe(devp); 8971 8972 /* 8973 * After successfully detaching an instance, we update the information 8974 * of how many luns have been attached in the relative target and 8975 * controller for parallel SCSI. This information is used when sd tries 8976 * to set the tagged queuing capability in HBA. 8977 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 8978 * check if the device is parallel SCSI. However, we don't need to 8979 * check here because we've already checked during attach. No device 8980 * that is not parallel SCSI is in the chain. 8981 */ 8982 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8983 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 8984 } 8985 8986 return (DDI_SUCCESS); 8987 8988 err_notclosed: 8989 mutex_exit(SD_MUTEX(un)); 8990 8991 err_stillbusy: 8992 _NOTE(NO_COMPETING_THREADS_NOW); 8993 8994 err_remove_event: 8995 mutex_enter(&sd_detach_mutex); 8996 un->un_detach_count--; 8997 mutex_exit(&sd_detach_mutex); 8998 8999 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9000 return (DDI_FAILURE); 9001 } 9002 9003 9004 /* 9005 * Function: sd_create_errstats 9006 * 9007 * Description: This routine instantiates the device error stats. 9008 * 9009 * Note: During attach the stats are instantiated first so they are 9010 * available for attach-time routines that utilize the driver 9011 * iopath to send commands to the device. The stats are initialized 9012 * separately so data obtained during some attach-time routines is 9013 * available. (4362483) 9014 * 9015 * Arguments: un - driver soft state (unit) structure 9016 * instance - driver instance 9017 * 9018 * Context: Kernel thread context 9019 */ 9020 9021 static void 9022 sd_create_errstats(struct sd_lun *un, int instance) 9023 { 9024 struct sd_errstats *stp; 9025 char kstatmodule_err[KSTAT_STRLEN]; 9026 char kstatname[KSTAT_STRLEN]; 9027 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9028 9029 ASSERT(un != NULL); 9030 9031 if (un->un_errstats != NULL) { 9032 return; 9033 } 9034 9035 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9036 "%serr", sd_label); 9037 (void) snprintf(kstatname, sizeof (kstatname), 9038 "%s%d,err", sd_label, instance); 9039 9040 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9041 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9042 9043 if (un->un_errstats == NULL) { 9044 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9045 "sd_create_errstats: Failed kstat_create\n"); 9046 return; 9047 } 9048 9049 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9050 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9051 KSTAT_DATA_UINT32); 9052 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9053 KSTAT_DATA_UINT32); 9054 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9055 KSTAT_DATA_UINT32); 9056 kstat_named_init(&stp->sd_vid, "Vendor", 9057 KSTAT_DATA_CHAR); 9058 kstat_named_init(&stp->sd_pid, "Product", 9059 KSTAT_DATA_CHAR); 9060 kstat_named_init(&stp->sd_revision, "Revision", 9061 KSTAT_DATA_CHAR); 9062 kstat_named_init(&stp->sd_serial, "Serial No", 9063 KSTAT_DATA_CHAR); 9064 kstat_named_init(&stp->sd_capacity, "Size", 9065 KSTAT_DATA_ULONGLONG); 9066 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9067 KSTAT_DATA_UINT32); 9068 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9069 KSTAT_DATA_UINT32); 9070 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9071 KSTAT_DATA_UINT32); 9072 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9073 KSTAT_DATA_UINT32); 9074 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9075 KSTAT_DATA_UINT32); 9076 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9077 KSTAT_DATA_UINT32); 9078 9079 un->un_errstats->ks_private = un; 9080 un->un_errstats->ks_update = nulldev; 9081 9082 kstat_install(un->un_errstats); 9083 } 9084 9085 9086 /* 9087 * Function: sd_set_errstats 9088 * 9089 * Description: This routine sets the value of the vendor id, product id, 9090 * revision, serial number, and capacity device error stats. 9091 * 9092 * Note: During attach the stats are instantiated first so they are 9093 * available for attach-time routines that utilize the driver 9094 * iopath to send commands to the device. The stats are initialized 9095 * separately so data obtained during some attach-time routines is 9096 * available. (4362483) 9097 * 9098 * Arguments: un - driver soft state (unit) structure 9099 * 9100 * Context: Kernel thread context 9101 */ 9102 9103 static void 9104 sd_set_errstats(struct sd_lun *un) 9105 { 9106 struct sd_errstats *stp; 9107 9108 ASSERT(un != NULL); 9109 ASSERT(un->un_errstats != NULL); 9110 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9111 ASSERT(stp != NULL); 9112 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9113 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9114 (void) strncpy(stp->sd_revision.value.c, 9115 un->un_sd->sd_inq->inq_revision, 4); 9116 9117 /* 9118 * All the errstats are persistent across detach/attach, 9119 * so reset all the errstats here in case of the hot 9120 * replacement of disk drives, except for not changed 9121 * Sun qualified drives. 9122 */ 9123 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9124 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9125 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9126 stp->sd_softerrs.value.ui32 = 0; 9127 stp->sd_harderrs.value.ui32 = 0; 9128 stp->sd_transerrs.value.ui32 = 0; 9129 stp->sd_rq_media_err.value.ui32 = 0; 9130 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9131 stp->sd_rq_nodev_err.value.ui32 = 0; 9132 stp->sd_rq_recov_err.value.ui32 = 0; 9133 stp->sd_rq_illrq_err.value.ui32 = 0; 9134 stp->sd_rq_pfa_err.value.ui32 = 0; 9135 } 9136 9137 /* 9138 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9139 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9140 * (4376302)) 9141 */ 9142 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9143 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9144 sizeof (SD_INQUIRY(un)->inq_serial)); 9145 } 9146 9147 if (un->un_f_blockcount_is_valid != TRUE) { 9148 /* 9149 * Set capacity error stat to 0 for no media. This ensures 9150 * a valid capacity is displayed in response to 'iostat -E' 9151 * when no media is present in the device. 9152 */ 9153 stp->sd_capacity.value.ui64 = 0; 9154 } else { 9155 /* 9156 * Multiply un_blockcount by un->un_sys_blocksize to get 9157 * capacity. 9158 * 9159 * Note: for non-512 blocksize devices "un_blockcount" has been 9160 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9161 * (un_tgt_blocksize / un->un_sys_blocksize). 9162 */ 9163 stp->sd_capacity.value.ui64 = (uint64_t) 9164 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9165 } 9166 } 9167 9168 9169 /* 9170 * Function: sd_set_pstats 9171 * 9172 * Description: This routine instantiates and initializes the partition 9173 * stats for each partition with more than zero blocks. 9174 * (4363169) 9175 * 9176 * Arguments: un - driver soft state (unit) structure 9177 * 9178 * Context: Kernel thread context 9179 */ 9180 9181 static void 9182 sd_set_pstats(struct sd_lun *un) 9183 { 9184 char kstatname[KSTAT_STRLEN]; 9185 int instance; 9186 int i; 9187 diskaddr_t nblks = 0; 9188 char *partname = NULL; 9189 9190 ASSERT(un != NULL); 9191 9192 instance = ddi_get_instance(SD_DEVINFO(un)); 9193 9194 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9195 for (i = 0; i < NSDMAP; i++) { 9196 9197 if (cmlb_partinfo(un->un_cmlbhandle, i, 9198 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 9199 continue; 9200 mutex_enter(SD_MUTEX(un)); 9201 9202 if ((un->un_pstats[i] == NULL) && 9203 (nblks != 0)) { 9204 9205 (void) snprintf(kstatname, sizeof (kstatname), 9206 "%s%d,%s", sd_label, instance, 9207 partname); 9208 9209 un->un_pstats[i] = kstat_create(sd_label, 9210 instance, kstatname, "partition", KSTAT_TYPE_IO, 9211 1, KSTAT_FLAG_PERSISTENT); 9212 if (un->un_pstats[i] != NULL) { 9213 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9214 kstat_install(un->un_pstats[i]); 9215 } 9216 } 9217 mutex_exit(SD_MUTEX(un)); 9218 } 9219 } 9220 9221 9222 #if (defined(__fibre)) 9223 /* 9224 * Function: sd_init_event_callbacks 9225 * 9226 * Description: This routine initializes the insertion and removal event 9227 * callbacks. (fibre only) 9228 * 9229 * Arguments: un - driver soft state (unit) structure 9230 * 9231 * Context: Kernel thread context 9232 */ 9233 9234 static void 9235 sd_init_event_callbacks(struct sd_lun *un) 9236 { 9237 ASSERT(un != NULL); 9238 9239 if ((un->un_insert_event == NULL) && 9240 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9241 &un->un_insert_event) == DDI_SUCCESS)) { 9242 /* 9243 * Add the callback for an insertion event 9244 */ 9245 (void) ddi_add_event_handler(SD_DEVINFO(un), 9246 un->un_insert_event, sd_event_callback, (void *)un, 9247 &(un->un_insert_cb_id)); 9248 } 9249 9250 if ((un->un_remove_event == NULL) && 9251 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9252 &un->un_remove_event) == DDI_SUCCESS)) { 9253 /* 9254 * Add the callback for a removal event 9255 */ 9256 (void) ddi_add_event_handler(SD_DEVINFO(un), 9257 un->un_remove_event, sd_event_callback, (void *)un, 9258 &(un->un_remove_cb_id)); 9259 } 9260 } 9261 9262 9263 /* 9264 * Function: sd_event_callback 9265 * 9266 * Description: This routine handles insert/remove events (photon). The 9267 * state is changed to OFFLINE which can be used to supress 9268 * error msgs. (fibre only) 9269 * 9270 * Arguments: un - driver soft state (unit) structure 9271 * 9272 * Context: Callout thread context 9273 */ 9274 /* ARGSUSED */ 9275 static void 9276 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9277 void *bus_impldata) 9278 { 9279 struct sd_lun *un = (struct sd_lun *)arg; 9280 9281 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9282 if (event == un->un_insert_event) { 9283 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9284 mutex_enter(SD_MUTEX(un)); 9285 if (un->un_state == SD_STATE_OFFLINE) { 9286 if (un->un_last_state != SD_STATE_SUSPENDED) { 9287 un->un_state = un->un_last_state; 9288 } else { 9289 /* 9290 * We have gone through SUSPEND/RESUME while 9291 * we were offline. Restore the last state 9292 */ 9293 un->un_state = un->un_save_state; 9294 } 9295 } 9296 mutex_exit(SD_MUTEX(un)); 9297 9298 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9299 } else if (event == un->un_remove_event) { 9300 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9301 mutex_enter(SD_MUTEX(un)); 9302 /* 9303 * We need to handle an event callback that occurs during 9304 * the suspend operation, since we don't prevent it. 9305 */ 9306 if (un->un_state != SD_STATE_OFFLINE) { 9307 if (un->un_state != SD_STATE_SUSPENDED) { 9308 New_state(un, SD_STATE_OFFLINE); 9309 } else { 9310 un->un_last_state = SD_STATE_OFFLINE; 9311 } 9312 } 9313 mutex_exit(SD_MUTEX(un)); 9314 } else { 9315 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9316 "!Unknown event\n"); 9317 } 9318 9319 } 9320 #endif 9321 9322 /* 9323 * Function: sd_cache_control() 9324 * 9325 * Description: This routine is the driver entry point for setting 9326 * read and write caching by modifying the WCE (write cache 9327 * enable) and RCD (read cache disable) bits of mode 9328 * page 8 (MODEPAGE_CACHING). 9329 * 9330 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9331 * structure for this target. 9332 * rcd_flag - flag for controlling the read cache 9333 * wce_flag - flag for controlling the write cache 9334 * 9335 * Return Code: EIO 9336 * code returned by sd_send_scsi_MODE_SENSE and 9337 * sd_send_scsi_MODE_SELECT 9338 * 9339 * Context: Kernel Thread 9340 */ 9341 9342 static int 9343 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag) 9344 { 9345 struct mode_caching *mode_caching_page; 9346 uchar_t *header; 9347 size_t buflen; 9348 int hdrlen; 9349 int bd_len; 9350 int rval = 0; 9351 struct mode_header_grp2 *mhp; 9352 struct sd_lun *un; 9353 int status; 9354 9355 ASSERT(ssc != NULL); 9356 un = ssc->ssc_un; 9357 ASSERT(un != NULL); 9358 9359 /* 9360 * Do a test unit ready, otherwise a mode sense may not work if this 9361 * is the first command sent to the device after boot. 9362 */ 9363 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9364 if (status != 0) 9365 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9366 9367 if (un->un_f_cfg_is_atapi == TRUE) { 9368 hdrlen = MODE_HEADER_LENGTH_GRP2; 9369 } else { 9370 hdrlen = MODE_HEADER_LENGTH; 9371 } 9372 9373 /* 9374 * Allocate memory for the retrieved mode page and its headers. Set 9375 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9376 * we get all of the mode sense data otherwise, the mode select 9377 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9378 */ 9379 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9380 sizeof (struct mode_cache_scsi3); 9381 9382 header = kmem_zalloc(buflen, KM_SLEEP); 9383 9384 /* Get the information from the device. */ 9385 if (un->un_f_cfg_is_atapi == TRUE) { 9386 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9387 MODEPAGE_CACHING, SD_PATH_DIRECT); 9388 } else { 9389 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9390 MODEPAGE_CACHING, SD_PATH_DIRECT); 9391 } 9392 9393 if (rval != 0) { 9394 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9395 "sd_cache_control: Mode Sense Failed\n"); 9396 goto mode_sense_failed; 9397 } 9398 9399 /* 9400 * Determine size of Block Descriptors in order to locate 9401 * the mode page data. ATAPI devices return 0, SCSI devices 9402 * should return MODE_BLK_DESC_LENGTH. 9403 */ 9404 if (un->un_f_cfg_is_atapi == TRUE) { 9405 mhp = (struct mode_header_grp2 *)header; 9406 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9407 } else { 9408 bd_len = ((struct mode_header *)header)->bdesc_length; 9409 } 9410 9411 if (bd_len > MODE_BLK_DESC_LENGTH) { 9412 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9413 "sd_cache_control: Mode Sense returned invalid block " 9414 "descriptor length\n"); 9415 rval = EIO; 9416 goto mode_sense_failed; 9417 } 9418 9419 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9420 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9421 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9422 "sd_cache_control: Mode Sense caching page code mismatch " 9423 "%d\n", mode_caching_page->mode_page.code); 9424 rval = EIO; 9425 goto mode_sense_failed; 9426 } 9427 9428 /* Check the relevant bits on successful mode sense. */ 9429 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9430 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9431 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9432 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9433 9434 size_t sbuflen; 9435 uchar_t save_pg; 9436 9437 /* 9438 * Construct select buffer length based on the 9439 * length of the sense data returned. 9440 */ 9441 sbuflen = hdrlen + bd_len + 9442 sizeof (struct mode_page) + 9443 (int)mode_caching_page->mode_page.length; 9444 9445 /* 9446 * Set the caching bits as requested. 9447 */ 9448 if (rcd_flag == SD_CACHE_ENABLE) 9449 mode_caching_page->rcd = 0; 9450 else if (rcd_flag == SD_CACHE_DISABLE) 9451 mode_caching_page->rcd = 1; 9452 9453 if (wce_flag == SD_CACHE_ENABLE) 9454 mode_caching_page->wce = 1; 9455 else if (wce_flag == SD_CACHE_DISABLE) 9456 mode_caching_page->wce = 0; 9457 9458 /* 9459 * Save the page if the mode sense says the 9460 * drive supports it. 9461 */ 9462 save_pg = mode_caching_page->mode_page.ps ? 9463 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9464 9465 /* Clear reserved bits before mode select. */ 9466 mode_caching_page->mode_page.ps = 0; 9467 9468 /* 9469 * Clear out mode header for mode select. 9470 * The rest of the retrieved page will be reused. 9471 */ 9472 bzero(header, hdrlen); 9473 9474 if (un->un_f_cfg_is_atapi == TRUE) { 9475 mhp = (struct mode_header_grp2 *)header; 9476 mhp->bdesc_length_hi = bd_len >> 8; 9477 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9478 } else { 9479 ((struct mode_header *)header)->bdesc_length = bd_len; 9480 } 9481 9482 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9483 9484 /* Issue mode select to change the cache settings */ 9485 if (un->un_f_cfg_is_atapi == TRUE) { 9486 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header, 9487 sbuflen, save_pg, SD_PATH_DIRECT); 9488 } else { 9489 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 9490 sbuflen, save_pg, SD_PATH_DIRECT); 9491 } 9492 9493 } 9494 9495 9496 mode_sense_failed: 9497 9498 kmem_free(header, buflen); 9499 9500 if (rval != 0) { 9501 if (rval == EIO) 9502 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9503 else 9504 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9505 } 9506 return (rval); 9507 } 9508 9509 9510 /* 9511 * Function: sd_get_write_cache_enabled() 9512 * 9513 * Description: This routine is the driver entry point for determining if 9514 * write caching is enabled. It examines the WCE (write cache 9515 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9516 * 9517 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9518 * structure for this target. 9519 * is_enabled - pointer to int where write cache enabled state 9520 * is returned (non-zero -> write cache enabled) 9521 * 9522 * 9523 * Return Code: EIO 9524 * code returned by sd_send_scsi_MODE_SENSE 9525 * 9526 * Context: Kernel Thread 9527 * 9528 * NOTE: If ioctl is added to disable write cache, this sequence should 9529 * be followed so that no locking is required for accesses to 9530 * un->un_f_write_cache_enabled: 9531 * do mode select to clear wce 9532 * do synchronize cache to flush cache 9533 * set un->un_f_write_cache_enabled = FALSE 9534 * 9535 * Conversely, an ioctl to enable the write cache should be done 9536 * in this order: 9537 * set un->un_f_write_cache_enabled = TRUE 9538 * do mode select to set wce 9539 */ 9540 9541 static int 9542 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled) 9543 { 9544 struct mode_caching *mode_caching_page; 9545 uchar_t *header; 9546 size_t buflen; 9547 int hdrlen; 9548 int bd_len; 9549 int rval = 0; 9550 struct sd_lun *un; 9551 int status; 9552 9553 ASSERT(ssc != NULL); 9554 un = ssc->ssc_un; 9555 ASSERT(un != NULL); 9556 ASSERT(is_enabled != NULL); 9557 9558 /* in case of error, flag as enabled */ 9559 *is_enabled = TRUE; 9560 9561 /* 9562 * Do a test unit ready, otherwise a mode sense may not work if this 9563 * is the first command sent to the device after boot. 9564 */ 9565 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9566 9567 if (status != 0) 9568 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9569 9570 if (un->un_f_cfg_is_atapi == TRUE) { 9571 hdrlen = MODE_HEADER_LENGTH_GRP2; 9572 } else { 9573 hdrlen = MODE_HEADER_LENGTH; 9574 } 9575 9576 /* 9577 * Allocate memory for the retrieved mode page and its headers. Set 9578 * a pointer to the page itself. 9579 */ 9580 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9581 header = kmem_zalloc(buflen, KM_SLEEP); 9582 9583 /* Get the information from the device. */ 9584 if (un->un_f_cfg_is_atapi == TRUE) { 9585 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9586 MODEPAGE_CACHING, SD_PATH_DIRECT); 9587 } else { 9588 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9589 MODEPAGE_CACHING, SD_PATH_DIRECT); 9590 } 9591 9592 if (rval != 0) { 9593 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9594 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9595 goto mode_sense_failed; 9596 } 9597 9598 /* 9599 * Determine size of Block Descriptors in order to locate 9600 * the mode page data. ATAPI devices return 0, SCSI devices 9601 * should return MODE_BLK_DESC_LENGTH. 9602 */ 9603 if (un->un_f_cfg_is_atapi == TRUE) { 9604 struct mode_header_grp2 *mhp; 9605 mhp = (struct mode_header_grp2 *)header; 9606 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9607 } else { 9608 bd_len = ((struct mode_header *)header)->bdesc_length; 9609 } 9610 9611 if (bd_len > MODE_BLK_DESC_LENGTH) { 9612 /* FMA should make upset complain here */ 9613 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9614 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9615 "block descriptor length\n"); 9616 rval = EIO; 9617 goto mode_sense_failed; 9618 } 9619 9620 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9621 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9622 /* FMA could make upset complain here */ 9623 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9624 "sd_get_write_cache_enabled: Mode Sense caching page " 9625 "code mismatch %d\n", mode_caching_page->mode_page.code); 9626 rval = EIO; 9627 goto mode_sense_failed; 9628 } 9629 *is_enabled = mode_caching_page->wce; 9630 9631 mode_sense_failed: 9632 if (rval == 0) { 9633 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 9634 } else if (rval == EIO) { 9635 /* 9636 * Some disks do not support mode sense(6), we 9637 * should ignore this kind of error(sense key is 9638 * 0x5 - illegal request). 9639 */ 9640 uint8_t *sensep; 9641 int senlen; 9642 9643 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 9644 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 9645 ssc->ssc_uscsi_cmd->uscsi_rqresid); 9646 9647 if (senlen > 0 && 9648 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 9649 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 9650 } else { 9651 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9652 } 9653 } else { 9654 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9655 } 9656 kmem_free(header, buflen); 9657 return (rval); 9658 } 9659 9660 /* 9661 * Function: sd_get_nv_sup() 9662 * 9663 * Description: This routine is the driver entry point for 9664 * determining whether non-volatile cache is supported. This 9665 * determination process works as follows: 9666 * 9667 * 1. sd first queries sd.conf on whether 9668 * suppress_cache_flush bit is set for this device. 9669 * 9670 * 2. if not there, then queries the internal disk table. 9671 * 9672 * 3. if either sd.conf or internal disk table specifies 9673 * cache flush be suppressed, we don't bother checking 9674 * NV_SUP bit. 9675 * 9676 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 9677 * the optional INQUIRY VPD page 0x86. If the device 9678 * supports VPD page 0x86, sd examines the NV_SUP 9679 * (non-volatile cache support) bit in the INQUIRY VPD page 9680 * 0x86: 9681 * o If NV_SUP bit is set, sd assumes the device has a 9682 * non-volatile cache and set the 9683 * un_f_sync_nv_supported to TRUE. 9684 * o Otherwise cache is not non-volatile, 9685 * un_f_sync_nv_supported is set to FALSE. 9686 * 9687 * Arguments: un - driver soft state (unit) structure 9688 * 9689 * Return Code: 9690 * 9691 * Context: Kernel Thread 9692 */ 9693 9694 static void 9695 sd_get_nv_sup(sd_ssc_t *ssc) 9696 { 9697 int rval = 0; 9698 uchar_t *inq86 = NULL; 9699 size_t inq86_len = MAX_INQUIRY_SIZE; 9700 size_t inq86_resid = 0; 9701 struct dk_callback *dkc; 9702 struct sd_lun *un; 9703 9704 ASSERT(ssc != NULL); 9705 un = ssc->ssc_un; 9706 ASSERT(un != NULL); 9707 9708 mutex_enter(SD_MUTEX(un)); 9709 9710 /* 9711 * Be conservative on the device's support of 9712 * SYNC_NV bit: un_f_sync_nv_supported is 9713 * initialized to be false. 9714 */ 9715 un->un_f_sync_nv_supported = FALSE; 9716 9717 /* 9718 * If either sd.conf or internal disk table 9719 * specifies cache flush be suppressed, then 9720 * we don't bother checking NV_SUP bit. 9721 */ 9722 if (un->un_f_suppress_cache_flush == TRUE) { 9723 mutex_exit(SD_MUTEX(un)); 9724 return; 9725 } 9726 9727 if (sd_check_vpd_page_support(ssc) == 0 && 9728 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 9729 mutex_exit(SD_MUTEX(un)); 9730 /* collect page 86 data if available */ 9731 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 9732 9733 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len, 9734 0x01, 0x86, &inq86_resid); 9735 9736 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 9737 SD_TRACE(SD_LOG_COMMON, un, 9738 "sd_get_nv_sup: \ 9739 successfully get VPD page: %x \ 9740 PAGE LENGTH: %x BYTE 6: %x\n", 9741 inq86[1], inq86[3], inq86[6]); 9742 9743 mutex_enter(SD_MUTEX(un)); 9744 /* 9745 * check the value of NV_SUP bit: only if the device 9746 * reports NV_SUP bit to be 1, the 9747 * un_f_sync_nv_supported bit will be set to true. 9748 */ 9749 if (inq86[6] & SD_VPD_NV_SUP) { 9750 un->un_f_sync_nv_supported = TRUE; 9751 } 9752 mutex_exit(SD_MUTEX(un)); 9753 } else if (rval != 0) { 9754 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9755 } 9756 9757 kmem_free(inq86, inq86_len); 9758 } else { 9759 mutex_exit(SD_MUTEX(un)); 9760 } 9761 9762 /* 9763 * Send a SYNC CACHE command to check whether 9764 * SYNC_NV bit is supported. This command should have 9765 * un_f_sync_nv_supported set to correct value. 9766 */ 9767 mutex_enter(SD_MUTEX(un)); 9768 if (un->un_f_sync_nv_supported) { 9769 mutex_exit(SD_MUTEX(un)); 9770 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 9771 dkc->dkc_flag = FLUSH_VOLATILE; 9772 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 9773 9774 /* 9775 * Send a TEST UNIT READY command to the device. This should 9776 * clear any outstanding UNIT ATTENTION that may be present. 9777 */ 9778 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 9779 if (rval != 0) 9780 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9781 9782 kmem_free(dkc, sizeof (struct dk_callback)); 9783 } else { 9784 mutex_exit(SD_MUTEX(un)); 9785 } 9786 9787 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 9788 un_f_suppress_cache_flush is set to %d\n", 9789 un->un_f_suppress_cache_flush); 9790 } 9791 9792 /* 9793 * Function: sd_make_device 9794 * 9795 * Description: Utility routine to return the Solaris device number from 9796 * the data in the device's dev_info structure. 9797 * 9798 * Return Code: The Solaris device number 9799 * 9800 * Context: Any 9801 */ 9802 9803 static dev_t 9804 sd_make_device(dev_info_t *devi) 9805 { 9806 return (makedevice(ddi_driver_major(devi), 9807 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9808 } 9809 9810 9811 /* 9812 * Function: sd_pm_entry 9813 * 9814 * Description: Called at the start of a new command to manage power 9815 * and busy status of a device. This includes determining whether 9816 * the current power state of the device is sufficient for 9817 * performing the command or whether it must be changed. 9818 * The PM framework is notified appropriately. 9819 * Only with a return status of DDI_SUCCESS will the 9820 * component be busy to the framework. 9821 * 9822 * All callers of sd_pm_entry must check the return status 9823 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9824 * of DDI_FAILURE indicates the device failed to power up. 9825 * In this case un_pm_count has been adjusted so the result 9826 * on exit is still powered down, ie. count is less than 0. 9827 * Calling sd_pm_exit with this count value hits an ASSERT. 9828 * 9829 * Return Code: DDI_SUCCESS or DDI_FAILURE 9830 * 9831 * Context: Kernel thread context. 9832 */ 9833 9834 static int 9835 sd_pm_entry(struct sd_lun *un) 9836 { 9837 int return_status = DDI_SUCCESS; 9838 9839 ASSERT(!mutex_owned(SD_MUTEX(un))); 9840 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9841 9842 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9843 9844 if (un->un_f_pm_is_enabled == FALSE) { 9845 SD_TRACE(SD_LOG_IO_PM, un, 9846 "sd_pm_entry: exiting, PM not enabled\n"); 9847 return (return_status); 9848 } 9849 9850 /* 9851 * Just increment a counter if PM is enabled. On the transition from 9852 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9853 * the count with each IO and mark the device as idle when the count 9854 * hits 0. 9855 * 9856 * If the count is less than 0 the device is powered down. If a powered 9857 * down device is successfully powered up then the count must be 9858 * incremented to reflect the power up. Note that it'll get incremented 9859 * a second time to become busy. 9860 * 9861 * Because the following has the potential to change the device state 9862 * and must release the un_pm_mutex to do so, only one thread can be 9863 * allowed through at a time. 9864 */ 9865 9866 mutex_enter(&un->un_pm_mutex); 9867 while (un->un_pm_busy == TRUE) { 9868 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9869 } 9870 un->un_pm_busy = TRUE; 9871 9872 if (un->un_pm_count < 1) { 9873 9874 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9875 9876 /* 9877 * Indicate we are now busy so the framework won't attempt to 9878 * power down the device. This call will only fail if either 9879 * we passed a bad component number or the device has no 9880 * components. Neither of these should ever happen. 9881 */ 9882 mutex_exit(&un->un_pm_mutex); 9883 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9884 ASSERT(return_status == DDI_SUCCESS); 9885 9886 mutex_enter(&un->un_pm_mutex); 9887 9888 if (un->un_pm_count < 0) { 9889 mutex_exit(&un->un_pm_mutex); 9890 9891 SD_TRACE(SD_LOG_IO_PM, un, 9892 "sd_pm_entry: power up component\n"); 9893 9894 /* 9895 * pm_raise_power will cause sdpower to be called 9896 * which brings the device power level to the 9897 * desired state, If successful, un_pm_count and 9898 * un_power_level will be updated appropriately. 9899 */ 9900 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9901 SD_PM_STATE_ACTIVE(un)); 9902 9903 mutex_enter(&un->un_pm_mutex); 9904 9905 if (return_status != DDI_SUCCESS) { 9906 /* 9907 * Power up failed. 9908 * Idle the device and adjust the count 9909 * so the result on exit is that we're 9910 * still powered down, ie. count is less than 0. 9911 */ 9912 SD_TRACE(SD_LOG_IO_PM, un, 9913 "sd_pm_entry: power up failed," 9914 " idle the component\n"); 9915 9916 (void) pm_idle_component(SD_DEVINFO(un), 0); 9917 un->un_pm_count--; 9918 } else { 9919 /* 9920 * Device is powered up, verify the 9921 * count is non-negative. 9922 * This is debug only. 9923 */ 9924 ASSERT(un->un_pm_count == 0); 9925 } 9926 } 9927 9928 if (return_status == DDI_SUCCESS) { 9929 /* 9930 * For performance, now that the device has been tagged 9931 * as busy, and it's known to be powered up, update the 9932 * chain types to use jump tables that do not include 9933 * pm. This significantly lowers the overhead and 9934 * therefore improves performance. 9935 */ 9936 9937 mutex_exit(&un->un_pm_mutex); 9938 mutex_enter(SD_MUTEX(un)); 9939 SD_TRACE(SD_LOG_IO_PM, un, 9940 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9941 un->un_uscsi_chain_type); 9942 9943 if (un->un_f_non_devbsize_supported) { 9944 un->un_buf_chain_type = 9945 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9946 } else { 9947 un->un_buf_chain_type = 9948 SD_CHAIN_INFO_DISK_NO_PM; 9949 } 9950 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9951 9952 SD_TRACE(SD_LOG_IO_PM, un, 9953 " changed uscsi_chain_type to %d\n", 9954 un->un_uscsi_chain_type); 9955 mutex_exit(SD_MUTEX(un)); 9956 mutex_enter(&un->un_pm_mutex); 9957 9958 if (un->un_pm_idle_timeid == NULL) { 9959 /* 300 ms. */ 9960 un->un_pm_idle_timeid = 9961 timeout(sd_pm_idletimeout_handler, un, 9962 (drv_usectohz((clock_t)300000))); 9963 /* 9964 * Include an extra call to busy which keeps the 9965 * device busy with-respect-to the PM layer 9966 * until the timer fires, at which time it'll 9967 * get the extra idle call. 9968 */ 9969 (void) pm_busy_component(SD_DEVINFO(un), 0); 9970 } 9971 } 9972 } 9973 un->un_pm_busy = FALSE; 9974 /* Next... */ 9975 cv_signal(&un->un_pm_busy_cv); 9976 9977 un->un_pm_count++; 9978 9979 SD_TRACE(SD_LOG_IO_PM, un, 9980 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 9981 9982 mutex_exit(&un->un_pm_mutex); 9983 9984 return (return_status); 9985 } 9986 9987 9988 /* 9989 * Function: sd_pm_exit 9990 * 9991 * Description: Called at the completion of a command to manage busy 9992 * status for the device. If the device becomes idle the 9993 * PM framework is notified. 9994 * 9995 * Context: Kernel thread context 9996 */ 9997 9998 static void 9999 sd_pm_exit(struct sd_lun *un) 10000 { 10001 ASSERT(!mutex_owned(SD_MUTEX(un))); 10002 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10003 10004 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10005 10006 /* 10007 * After attach the following flag is only read, so don't 10008 * take the penalty of acquiring a mutex for it. 10009 */ 10010 if (un->un_f_pm_is_enabled == TRUE) { 10011 10012 mutex_enter(&un->un_pm_mutex); 10013 un->un_pm_count--; 10014 10015 SD_TRACE(SD_LOG_IO_PM, un, 10016 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10017 10018 ASSERT(un->un_pm_count >= 0); 10019 if (un->un_pm_count == 0) { 10020 mutex_exit(&un->un_pm_mutex); 10021 10022 SD_TRACE(SD_LOG_IO_PM, un, 10023 "sd_pm_exit: idle component\n"); 10024 10025 (void) pm_idle_component(SD_DEVINFO(un), 0); 10026 10027 } else { 10028 mutex_exit(&un->un_pm_mutex); 10029 } 10030 } 10031 10032 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10033 } 10034 10035 10036 /* 10037 * Function: sdopen 10038 * 10039 * Description: Driver's open(9e) entry point function. 10040 * 10041 * Arguments: dev_i - pointer to device number 10042 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10043 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10044 * cred_p - user credential pointer 10045 * 10046 * Return Code: EINVAL 10047 * ENXIO 10048 * EIO 10049 * EROFS 10050 * EBUSY 10051 * 10052 * Context: Kernel thread context 10053 */ 10054 /* ARGSUSED */ 10055 static int 10056 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10057 { 10058 struct sd_lun *un; 10059 int nodelay; 10060 int part; 10061 uint64_t partmask; 10062 int instance; 10063 dev_t dev; 10064 int rval = EIO; 10065 diskaddr_t nblks = 0; 10066 diskaddr_t label_cap; 10067 10068 /* Validate the open type */ 10069 if (otyp >= OTYPCNT) { 10070 return (EINVAL); 10071 } 10072 10073 dev = *dev_p; 10074 instance = SDUNIT(dev); 10075 mutex_enter(&sd_detach_mutex); 10076 10077 /* 10078 * Fail the open if there is no softstate for the instance, or 10079 * if another thread somewhere is trying to detach the instance. 10080 */ 10081 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10082 (un->un_detach_count != 0)) { 10083 mutex_exit(&sd_detach_mutex); 10084 /* 10085 * The probe cache only needs to be cleared when open (9e) fails 10086 * with ENXIO (4238046). 10087 */ 10088 /* 10089 * un-conditionally clearing probe cache is ok with 10090 * separate sd/ssd binaries 10091 * x86 platform can be an issue with both parallel 10092 * and fibre in 1 binary 10093 */ 10094 sd_scsi_clear_probe_cache(); 10095 return (ENXIO); 10096 } 10097 10098 /* 10099 * The un_layer_count is to prevent another thread in specfs from 10100 * trying to detach the instance, which can happen when we are 10101 * called from a higher-layer driver instead of thru specfs. 10102 * This will not be needed when DDI provides a layered driver 10103 * interface that allows specfs to know that an instance is in 10104 * use by a layered driver & should not be detached. 10105 * 10106 * Note: the semantics for layered driver opens are exactly one 10107 * close for every open. 10108 */ 10109 if (otyp == OTYP_LYR) { 10110 un->un_layer_count++; 10111 } 10112 10113 /* 10114 * Keep a count of the current # of opens in progress. This is because 10115 * some layered drivers try to call us as a regular open. This can 10116 * cause problems that we cannot prevent, however by keeping this count 10117 * we can at least keep our open and detach routines from racing against 10118 * each other under such conditions. 10119 */ 10120 un->un_opens_in_progress++; 10121 mutex_exit(&sd_detach_mutex); 10122 10123 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10124 part = SDPART(dev); 10125 partmask = 1 << part; 10126 10127 /* 10128 * We use a semaphore here in order to serialize 10129 * open and close requests on the device. 10130 */ 10131 sema_p(&un->un_semoclose); 10132 10133 mutex_enter(SD_MUTEX(un)); 10134 10135 /* 10136 * All device accesses go thru sdstrategy() where we check 10137 * on suspend status but there could be a scsi_poll command, 10138 * which bypasses sdstrategy(), so we need to check pm 10139 * status. 10140 */ 10141 10142 if (!nodelay) { 10143 while ((un->un_state == SD_STATE_SUSPENDED) || 10144 (un->un_state == SD_STATE_PM_CHANGING)) { 10145 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10146 } 10147 10148 mutex_exit(SD_MUTEX(un)); 10149 if (sd_pm_entry(un) != DDI_SUCCESS) { 10150 rval = EIO; 10151 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10152 "sdopen: sd_pm_entry failed\n"); 10153 goto open_failed_with_pm; 10154 } 10155 mutex_enter(SD_MUTEX(un)); 10156 } 10157 10158 /* check for previous exclusive open */ 10159 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10160 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10161 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10162 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10163 10164 if (un->un_exclopen & (partmask)) { 10165 goto excl_open_fail; 10166 } 10167 10168 if (flag & FEXCL) { 10169 int i; 10170 if (un->un_ocmap.lyropen[part]) { 10171 goto excl_open_fail; 10172 } 10173 for (i = 0; i < (OTYPCNT - 1); i++) { 10174 if (un->un_ocmap.regopen[i] & (partmask)) { 10175 goto excl_open_fail; 10176 } 10177 } 10178 } 10179 10180 /* 10181 * Check the write permission if this is a removable media device, 10182 * NDELAY has not been set, and writable permission is requested. 10183 * 10184 * Note: If NDELAY was set and this is write-protected media the WRITE 10185 * attempt will fail with EIO as part of the I/O processing. This is a 10186 * more permissive implementation that allows the open to succeed and 10187 * WRITE attempts to fail when appropriate. 10188 */ 10189 if (un->un_f_chk_wp_open) { 10190 if ((flag & FWRITE) && (!nodelay)) { 10191 mutex_exit(SD_MUTEX(un)); 10192 /* 10193 * Defer the check for write permission on writable 10194 * DVD drive till sdstrategy and will not fail open even 10195 * if FWRITE is set as the device can be writable 10196 * depending upon the media and the media can change 10197 * after the call to open(). 10198 */ 10199 if (un->un_f_dvdram_writable_device == FALSE) { 10200 if (ISCD(un) || sr_check_wp(dev)) { 10201 rval = EROFS; 10202 mutex_enter(SD_MUTEX(un)); 10203 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10204 "write to cd or write protected media\n"); 10205 goto open_fail; 10206 } 10207 } 10208 mutex_enter(SD_MUTEX(un)); 10209 } 10210 } 10211 10212 /* 10213 * If opening in NDELAY/NONBLOCK mode, just return. 10214 * Check if disk is ready and has a valid geometry later. 10215 */ 10216 if (!nodelay) { 10217 sd_ssc_t *ssc; 10218 10219 mutex_exit(SD_MUTEX(un)); 10220 ssc = sd_ssc_init(un); 10221 rval = sd_ready_and_valid(ssc, part); 10222 sd_ssc_fini(ssc); 10223 mutex_enter(SD_MUTEX(un)); 10224 /* 10225 * Fail if device is not ready or if the number of disk 10226 * blocks is zero or negative for non CD devices. 10227 */ 10228 10229 nblks = 0; 10230 10231 if (rval == SD_READY_VALID && (!ISCD(un))) { 10232 /* if cmlb_partinfo fails, nblks remains 0 */ 10233 mutex_exit(SD_MUTEX(un)); 10234 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 10235 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 10236 mutex_enter(SD_MUTEX(un)); 10237 } 10238 10239 if ((rval != SD_READY_VALID) || 10240 (!ISCD(un) && nblks <= 0)) { 10241 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10242 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10243 "device not ready or invalid disk block value\n"); 10244 goto open_fail; 10245 } 10246 #if defined(__i386) || defined(__amd64) 10247 } else { 10248 uchar_t *cp; 10249 /* 10250 * x86 requires special nodelay handling, so that p0 is 10251 * always defined and accessible. 10252 * Invalidate geometry only if device is not already open. 10253 */ 10254 cp = &un->un_ocmap.chkd[0]; 10255 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10256 if (*cp != (uchar_t)0) { 10257 break; 10258 } 10259 cp++; 10260 } 10261 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10262 mutex_exit(SD_MUTEX(un)); 10263 cmlb_invalidate(un->un_cmlbhandle, 10264 (void *)SD_PATH_DIRECT); 10265 mutex_enter(SD_MUTEX(un)); 10266 } 10267 10268 #endif 10269 } 10270 10271 if (otyp == OTYP_LYR) { 10272 un->un_ocmap.lyropen[part]++; 10273 } else { 10274 un->un_ocmap.regopen[otyp] |= partmask; 10275 } 10276 10277 /* Set up open and exclusive open flags */ 10278 if (flag & FEXCL) { 10279 un->un_exclopen |= (partmask); 10280 } 10281 10282 /* 10283 * If the lun is EFI labeled and lun capacity is greater than the 10284 * capacity contained in the label, log a sys-event to notify the 10285 * interested module. 10286 * To avoid an infinite loop of logging sys-event, we only log the 10287 * event when the lun is not opened in NDELAY mode. The event handler 10288 * should open the lun in NDELAY mode. 10289 */ 10290 if (!(flag & FNDELAY)) { 10291 mutex_exit(SD_MUTEX(un)); 10292 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 10293 (void*)SD_PATH_DIRECT) == 0) { 10294 mutex_enter(SD_MUTEX(un)); 10295 if (un->un_f_blockcount_is_valid && 10296 un->un_blockcount > label_cap) { 10297 mutex_exit(SD_MUTEX(un)); 10298 sd_log_lun_expansion_event(un, 10299 (nodelay ? KM_NOSLEEP : KM_SLEEP)); 10300 mutex_enter(SD_MUTEX(un)); 10301 } 10302 } else { 10303 mutex_enter(SD_MUTEX(un)); 10304 } 10305 } 10306 10307 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10308 "open of part %d type %d\n", part, otyp); 10309 10310 mutex_exit(SD_MUTEX(un)); 10311 if (!nodelay) { 10312 sd_pm_exit(un); 10313 } 10314 10315 sema_v(&un->un_semoclose); 10316 10317 mutex_enter(&sd_detach_mutex); 10318 un->un_opens_in_progress--; 10319 mutex_exit(&sd_detach_mutex); 10320 10321 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10322 return (DDI_SUCCESS); 10323 10324 excl_open_fail: 10325 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10326 rval = EBUSY; 10327 10328 open_fail: 10329 mutex_exit(SD_MUTEX(un)); 10330 10331 /* 10332 * On a failed open we must exit the pm management. 10333 */ 10334 if (!nodelay) { 10335 sd_pm_exit(un); 10336 } 10337 open_failed_with_pm: 10338 sema_v(&un->un_semoclose); 10339 10340 mutex_enter(&sd_detach_mutex); 10341 un->un_opens_in_progress--; 10342 if (otyp == OTYP_LYR) { 10343 un->un_layer_count--; 10344 } 10345 mutex_exit(&sd_detach_mutex); 10346 10347 return (rval); 10348 } 10349 10350 10351 /* 10352 * Function: sdclose 10353 * 10354 * Description: Driver's close(9e) entry point function. 10355 * 10356 * Arguments: dev - device number 10357 * flag - file status flag, informational only 10358 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10359 * cred_p - user credential pointer 10360 * 10361 * Return Code: ENXIO 10362 * 10363 * Context: Kernel thread context 10364 */ 10365 /* ARGSUSED */ 10366 static int 10367 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10368 { 10369 struct sd_lun *un; 10370 uchar_t *cp; 10371 int part; 10372 int nodelay; 10373 int rval = 0; 10374 10375 /* Validate the open type */ 10376 if (otyp >= OTYPCNT) { 10377 return (ENXIO); 10378 } 10379 10380 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10381 return (ENXIO); 10382 } 10383 10384 part = SDPART(dev); 10385 nodelay = flag & (FNDELAY | FNONBLOCK); 10386 10387 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10388 "sdclose: close of part %d type %d\n", part, otyp); 10389 10390 /* 10391 * We use a semaphore here in order to serialize 10392 * open and close requests on the device. 10393 */ 10394 sema_p(&un->un_semoclose); 10395 10396 mutex_enter(SD_MUTEX(un)); 10397 10398 /* Don't proceed if power is being changed. */ 10399 while (un->un_state == SD_STATE_PM_CHANGING) { 10400 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10401 } 10402 10403 if (un->un_exclopen & (1 << part)) { 10404 un->un_exclopen &= ~(1 << part); 10405 } 10406 10407 /* Update the open partition map */ 10408 if (otyp == OTYP_LYR) { 10409 un->un_ocmap.lyropen[part] -= 1; 10410 } else { 10411 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10412 } 10413 10414 cp = &un->un_ocmap.chkd[0]; 10415 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10416 if (*cp != NULL) { 10417 break; 10418 } 10419 cp++; 10420 } 10421 10422 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10423 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10424 10425 /* 10426 * We avoid persistance upon the last close, and set 10427 * the throttle back to the maximum. 10428 */ 10429 un->un_throttle = un->un_saved_throttle; 10430 10431 if (un->un_state == SD_STATE_OFFLINE) { 10432 if (un->un_f_is_fibre == FALSE) { 10433 scsi_log(SD_DEVINFO(un), sd_label, 10434 CE_WARN, "offline\n"); 10435 } 10436 mutex_exit(SD_MUTEX(un)); 10437 cmlb_invalidate(un->un_cmlbhandle, 10438 (void *)SD_PATH_DIRECT); 10439 mutex_enter(SD_MUTEX(un)); 10440 10441 } else { 10442 /* 10443 * Flush any outstanding writes in NVRAM cache. 10444 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10445 * cmd, it may not work for non-Pluto devices. 10446 * SYNCHRONIZE CACHE is not required for removables, 10447 * except DVD-RAM drives. 10448 * 10449 * Also note: because SYNCHRONIZE CACHE is currently 10450 * the only command issued here that requires the 10451 * drive be powered up, only do the power up before 10452 * sending the Sync Cache command. If additional 10453 * commands are added which require a powered up 10454 * drive, the following sequence may have to change. 10455 * 10456 * And finally, note that parallel SCSI on SPARC 10457 * only issues a Sync Cache to DVD-RAM, a newly 10458 * supported device. 10459 */ 10460 #if defined(__i386) || defined(__amd64) 10461 if ((un->un_f_sync_cache_supported && 10462 un->un_f_sync_cache_required) || 10463 un->un_f_dvdram_writable_device == TRUE) { 10464 #else 10465 if (un->un_f_dvdram_writable_device == TRUE) { 10466 #endif 10467 mutex_exit(SD_MUTEX(un)); 10468 if (sd_pm_entry(un) == DDI_SUCCESS) { 10469 rval = 10470 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10471 NULL); 10472 /* ignore error if not supported */ 10473 if (rval == ENOTSUP) { 10474 rval = 0; 10475 } else if (rval != 0) { 10476 rval = EIO; 10477 } 10478 sd_pm_exit(un); 10479 } else { 10480 rval = EIO; 10481 } 10482 mutex_enter(SD_MUTEX(un)); 10483 } 10484 10485 /* 10486 * For devices which supports DOOR_LOCK, send an ALLOW 10487 * MEDIA REMOVAL command, but don't get upset if it 10488 * fails. We need to raise the power of the drive before 10489 * we can call sd_send_scsi_DOORLOCK() 10490 */ 10491 if (un->un_f_doorlock_supported) { 10492 mutex_exit(SD_MUTEX(un)); 10493 if (sd_pm_entry(un) == DDI_SUCCESS) { 10494 sd_ssc_t *ssc; 10495 10496 ssc = sd_ssc_init(un); 10497 rval = sd_send_scsi_DOORLOCK(ssc, 10498 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10499 if (rval != 0) 10500 sd_ssc_assessment(ssc, 10501 SD_FMT_IGNORE); 10502 sd_ssc_fini(ssc); 10503 10504 sd_pm_exit(un); 10505 if (ISCD(un) && (rval != 0) && 10506 (nodelay != 0)) { 10507 rval = ENXIO; 10508 } 10509 } else { 10510 rval = EIO; 10511 } 10512 mutex_enter(SD_MUTEX(un)); 10513 } 10514 10515 /* 10516 * If a device has removable media, invalidate all 10517 * parameters related to media, such as geometry, 10518 * blocksize, and blockcount. 10519 */ 10520 if (un->un_f_has_removable_media) { 10521 sr_ejected(un); 10522 } 10523 10524 /* 10525 * Destroy the cache (if it exists) which was 10526 * allocated for the write maps since this is 10527 * the last close for this media. 10528 */ 10529 if (un->un_wm_cache) { 10530 /* 10531 * Check if there are pending commands. 10532 * and if there are give a warning and 10533 * do not destroy the cache. 10534 */ 10535 if (un->un_ncmds_in_driver > 0) { 10536 scsi_log(SD_DEVINFO(un), 10537 sd_label, CE_WARN, 10538 "Unable to clean up memory " 10539 "because of pending I/O\n"); 10540 } else { 10541 kmem_cache_destroy( 10542 un->un_wm_cache); 10543 un->un_wm_cache = NULL; 10544 } 10545 } 10546 } 10547 } 10548 10549 mutex_exit(SD_MUTEX(un)); 10550 sema_v(&un->un_semoclose); 10551 10552 if (otyp == OTYP_LYR) { 10553 mutex_enter(&sd_detach_mutex); 10554 /* 10555 * The detach routine may run when the layer count 10556 * drops to zero. 10557 */ 10558 un->un_layer_count--; 10559 mutex_exit(&sd_detach_mutex); 10560 } 10561 10562 return (rval); 10563 } 10564 10565 10566 /* 10567 * Function: sd_ready_and_valid 10568 * 10569 * Description: Test if device is ready and has a valid geometry. 10570 * 10571 * Arguments: ssc - sd_ssc_t will contain un 10572 * un - driver soft state (unit) structure 10573 * 10574 * Return Code: SD_READY_VALID ready and valid label 10575 * SD_NOT_READY_VALID not ready, no label 10576 * SD_RESERVED_BY_OTHERS reservation conflict 10577 * 10578 * Context: Never called at interrupt context. 10579 */ 10580 10581 static int 10582 sd_ready_and_valid(sd_ssc_t *ssc, int part) 10583 { 10584 struct sd_errstats *stp; 10585 uint64_t capacity; 10586 uint_t lbasize; 10587 int rval = SD_READY_VALID; 10588 char name_str[48]; 10589 boolean_t is_valid; 10590 struct sd_lun *un; 10591 int status; 10592 10593 ASSERT(ssc != NULL); 10594 un = ssc->ssc_un; 10595 ASSERT(un != NULL); 10596 ASSERT(!mutex_owned(SD_MUTEX(un))); 10597 10598 mutex_enter(SD_MUTEX(un)); 10599 /* 10600 * If a device has removable media, we must check if media is 10601 * ready when checking if this device is ready and valid. 10602 */ 10603 if (un->un_f_has_removable_media) { 10604 mutex_exit(SD_MUTEX(un)); 10605 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10606 10607 if (status != 0) { 10608 rval = SD_NOT_READY_VALID; 10609 mutex_enter(SD_MUTEX(un)); 10610 10611 /* Ignore all failed status for removalbe media */ 10612 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10613 10614 goto done; 10615 } 10616 10617 is_valid = SD_IS_VALID_LABEL(un); 10618 mutex_enter(SD_MUTEX(un)); 10619 if (!is_valid || 10620 (un->un_f_blockcount_is_valid == FALSE) || 10621 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10622 10623 /* capacity has to be read every open. */ 10624 mutex_exit(SD_MUTEX(un)); 10625 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 10626 &lbasize, SD_PATH_DIRECT); 10627 10628 if (status != 0) { 10629 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10630 10631 cmlb_invalidate(un->un_cmlbhandle, 10632 (void *)SD_PATH_DIRECT); 10633 mutex_enter(SD_MUTEX(un)); 10634 rval = SD_NOT_READY_VALID; 10635 10636 goto done; 10637 } else { 10638 mutex_enter(SD_MUTEX(un)); 10639 sd_update_block_info(un, lbasize, capacity); 10640 } 10641 } 10642 10643 /* 10644 * Check if the media in the device is writable or not. 10645 */ 10646 if (!is_valid && ISCD(un)) { 10647 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 10648 } 10649 10650 } else { 10651 /* 10652 * Do a test unit ready to clear any unit attention from non-cd 10653 * devices. 10654 */ 10655 mutex_exit(SD_MUTEX(un)); 10656 10657 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10658 if (status != 0) { 10659 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10660 } 10661 10662 mutex_enter(SD_MUTEX(un)); 10663 } 10664 10665 10666 /* 10667 * If this is a non 512 block device, allocate space for 10668 * the wmap cache. This is being done here since every time 10669 * a media is changed this routine will be called and the 10670 * block size is a function of media rather than device. 10671 */ 10672 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR || 10673 un->un_f_non_devbsize_supported) && 10674 un->un_tgt_blocksize != DEV_BSIZE) || 10675 un->un_f_enable_rmw) { 10676 if (!(un->un_wm_cache)) { 10677 (void) snprintf(name_str, sizeof (name_str), 10678 "%s%d_cache", 10679 ddi_driver_name(SD_DEVINFO(un)), 10680 ddi_get_instance(SD_DEVINFO(un))); 10681 un->un_wm_cache = kmem_cache_create( 10682 name_str, sizeof (struct sd_w_map), 10683 8, sd_wm_cache_constructor, 10684 sd_wm_cache_destructor, NULL, 10685 (void *)un, NULL, 0); 10686 if (!(un->un_wm_cache)) { 10687 rval = ENOMEM; 10688 goto done; 10689 } 10690 } 10691 } 10692 10693 if (un->un_state == SD_STATE_NORMAL) { 10694 /* 10695 * If the target is not yet ready here (defined by a TUR 10696 * failure), invalidate the geometry and print an 'offline' 10697 * message. This is a legacy message, as the state of the 10698 * target is not actually changed to SD_STATE_OFFLINE. 10699 * 10700 * If the TUR fails for EACCES (Reservation Conflict), 10701 * SD_RESERVED_BY_OTHERS will be returned to indicate 10702 * reservation conflict. If the TUR fails for other 10703 * reasons, SD_NOT_READY_VALID will be returned. 10704 */ 10705 int err; 10706 10707 mutex_exit(SD_MUTEX(un)); 10708 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10709 mutex_enter(SD_MUTEX(un)); 10710 10711 if (err != 0) { 10712 mutex_exit(SD_MUTEX(un)); 10713 cmlb_invalidate(un->un_cmlbhandle, 10714 (void *)SD_PATH_DIRECT); 10715 mutex_enter(SD_MUTEX(un)); 10716 if (err == EACCES) { 10717 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10718 "reservation conflict\n"); 10719 rval = SD_RESERVED_BY_OTHERS; 10720 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10721 } else { 10722 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10723 "drive offline\n"); 10724 rval = SD_NOT_READY_VALID; 10725 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 10726 } 10727 goto done; 10728 } 10729 } 10730 10731 if (un->un_f_format_in_progress == FALSE) { 10732 mutex_exit(SD_MUTEX(un)); 10733 10734 (void) cmlb_validate(un->un_cmlbhandle, 0, 10735 (void *)SD_PATH_DIRECT); 10736 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL, 10737 NULL, (void *) SD_PATH_DIRECT) != 0) { 10738 rval = SD_NOT_READY_VALID; 10739 mutex_enter(SD_MUTEX(un)); 10740 10741 goto done; 10742 } 10743 if (un->un_f_pkstats_enabled) { 10744 sd_set_pstats(un); 10745 SD_TRACE(SD_LOG_IO_PARTITION, un, 10746 "sd_ready_and_valid: un:0x%p pstats created and " 10747 "set\n", un); 10748 } 10749 mutex_enter(SD_MUTEX(un)); 10750 } 10751 10752 /* 10753 * If this device supports DOOR_LOCK command, try and send 10754 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10755 * if it fails. For a CD, however, it is an error 10756 */ 10757 if (un->un_f_doorlock_supported) { 10758 mutex_exit(SD_MUTEX(un)); 10759 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 10760 SD_PATH_DIRECT); 10761 10762 if ((status != 0) && ISCD(un)) { 10763 rval = SD_NOT_READY_VALID; 10764 mutex_enter(SD_MUTEX(un)); 10765 10766 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10767 10768 goto done; 10769 } else if (status != 0) 10770 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10771 mutex_enter(SD_MUTEX(un)); 10772 } 10773 10774 /* The state has changed, inform the media watch routines */ 10775 un->un_mediastate = DKIO_INSERTED; 10776 cv_broadcast(&un->un_state_cv); 10777 rval = SD_READY_VALID; 10778 10779 done: 10780 10781 /* 10782 * Initialize the capacity kstat value, if no media previously 10783 * (capacity kstat is 0) and a media has been inserted 10784 * (un_blockcount > 0). 10785 */ 10786 if (un->un_errstats != NULL) { 10787 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10788 if ((stp->sd_capacity.value.ui64 == 0) && 10789 (un->un_f_blockcount_is_valid == TRUE)) { 10790 stp->sd_capacity.value.ui64 = 10791 (uint64_t)((uint64_t)un->un_blockcount * 10792 un->un_sys_blocksize); 10793 } 10794 } 10795 10796 mutex_exit(SD_MUTEX(un)); 10797 return (rval); 10798 } 10799 10800 10801 /* 10802 * Function: sdmin 10803 * 10804 * Description: Routine to limit the size of a data transfer. Used in 10805 * conjunction with physio(9F). 10806 * 10807 * Arguments: bp - pointer to the indicated buf(9S) struct. 10808 * 10809 * Context: Kernel thread context. 10810 */ 10811 10812 static void 10813 sdmin(struct buf *bp) 10814 { 10815 struct sd_lun *un; 10816 int instance; 10817 10818 instance = SDUNIT(bp->b_edev); 10819 10820 un = ddi_get_soft_state(sd_state, instance); 10821 ASSERT(un != NULL); 10822 10823 /* 10824 * We depend on buf breakup to restrict 10825 * IO size if it is enabled. 10826 */ 10827 if (un->un_buf_breakup_supported) { 10828 return; 10829 } 10830 10831 if (bp->b_bcount > un->un_max_xfer_size) { 10832 bp->b_bcount = un->un_max_xfer_size; 10833 } 10834 } 10835 10836 10837 /* 10838 * Function: sdread 10839 * 10840 * Description: Driver's read(9e) entry point function. 10841 * 10842 * Arguments: dev - device number 10843 * uio - structure pointer describing where data is to be stored 10844 * in user's space 10845 * cred_p - user credential pointer 10846 * 10847 * Return Code: ENXIO 10848 * EIO 10849 * EINVAL 10850 * value returned by physio 10851 * 10852 * Context: Kernel thread context. 10853 */ 10854 /* ARGSUSED */ 10855 static int 10856 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10857 { 10858 struct sd_lun *un = NULL; 10859 int secmask; 10860 int err = 0; 10861 sd_ssc_t *ssc; 10862 10863 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10864 return (ENXIO); 10865 } 10866 10867 ASSERT(!mutex_owned(SD_MUTEX(un))); 10868 10869 10870 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10871 mutex_enter(SD_MUTEX(un)); 10872 /* 10873 * Because the call to sd_ready_and_valid will issue I/O we 10874 * must wait here if either the device is suspended or 10875 * if it's power level is changing. 10876 */ 10877 while ((un->un_state == SD_STATE_SUSPENDED) || 10878 (un->un_state == SD_STATE_PM_CHANGING)) { 10879 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10880 } 10881 un->un_ncmds_in_driver++; 10882 mutex_exit(SD_MUTEX(un)); 10883 10884 /* Initialize sd_ssc_t for internal uscsi commands */ 10885 ssc = sd_ssc_init(un); 10886 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10887 err = EIO; 10888 } else { 10889 err = 0; 10890 } 10891 sd_ssc_fini(ssc); 10892 10893 mutex_enter(SD_MUTEX(un)); 10894 un->un_ncmds_in_driver--; 10895 ASSERT(un->un_ncmds_in_driver >= 0); 10896 mutex_exit(SD_MUTEX(un)); 10897 if (err != 0) 10898 return (err); 10899 } 10900 10901 /* 10902 * Read requests are restricted to multiples of the system block size. 10903 */ 10904 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 10905 !un->un_f_enable_rmw) 10906 secmask = un->un_tgt_blocksize - 1; 10907 else 10908 secmask = DEV_BSIZE - 1; 10909 10910 if (uio->uio_loffset & ((offset_t)(secmask))) { 10911 SD_ERROR(SD_LOG_READ_WRITE, un, 10912 "sdread: file offset not modulo %d\n", 10913 secmask + 1); 10914 err = EINVAL; 10915 } else if (uio->uio_iov->iov_len & (secmask)) { 10916 SD_ERROR(SD_LOG_READ_WRITE, un, 10917 "sdread: transfer length not modulo %d\n", 10918 secmask + 1); 10919 err = EINVAL; 10920 } else { 10921 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10922 } 10923 10924 return (err); 10925 } 10926 10927 10928 /* 10929 * Function: sdwrite 10930 * 10931 * Description: Driver's write(9e) entry point function. 10932 * 10933 * Arguments: dev - device number 10934 * uio - structure pointer describing where data is stored in 10935 * user's space 10936 * cred_p - user credential pointer 10937 * 10938 * Return Code: ENXIO 10939 * EIO 10940 * EINVAL 10941 * value returned by physio 10942 * 10943 * Context: Kernel thread context. 10944 */ 10945 /* ARGSUSED */ 10946 static int 10947 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10948 { 10949 struct sd_lun *un = NULL; 10950 int secmask; 10951 int err = 0; 10952 sd_ssc_t *ssc; 10953 10954 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10955 return (ENXIO); 10956 } 10957 10958 ASSERT(!mutex_owned(SD_MUTEX(un))); 10959 10960 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10961 mutex_enter(SD_MUTEX(un)); 10962 /* 10963 * Because the call to sd_ready_and_valid will issue I/O we 10964 * must wait here if either the device is suspended or 10965 * if it's power level is changing. 10966 */ 10967 while ((un->un_state == SD_STATE_SUSPENDED) || 10968 (un->un_state == SD_STATE_PM_CHANGING)) { 10969 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10970 } 10971 un->un_ncmds_in_driver++; 10972 mutex_exit(SD_MUTEX(un)); 10973 10974 /* Initialize sd_ssc_t for internal uscsi commands */ 10975 ssc = sd_ssc_init(un); 10976 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10977 err = EIO; 10978 } else { 10979 err = 0; 10980 } 10981 sd_ssc_fini(ssc); 10982 10983 mutex_enter(SD_MUTEX(un)); 10984 un->un_ncmds_in_driver--; 10985 ASSERT(un->un_ncmds_in_driver >= 0); 10986 mutex_exit(SD_MUTEX(un)); 10987 if (err != 0) 10988 return (err); 10989 } 10990 10991 /* 10992 * Write requests are restricted to multiples of the system block size. 10993 */ 10994 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 10995 !un->un_f_enable_rmw) 10996 secmask = un->un_tgt_blocksize - 1; 10997 else 10998 secmask = DEV_BSIZE - 1; 10999 11000 if (uio->uio_loffset & ((offset_t)(secmask))) { 11001 SD_ERROR(SD_LOG_READ_WRITE, un, 11002 "sdwrite: file offset not modulo %d\n", 11003 secmask + 1); 11004 err = EINVAL; 11005 } else if (uio->uio_iov->iov_len & (secmask)) { 11006 SD_ERROR(SD_LOG_READ_WRITE, un, 11007 "sdwrite: transfer length not modulo %d\n", 11008 secmask + 1); 11009 err = EINVAL; 11010 } else { 11011 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11012 } 11013 11014 return (err); 11015 } 11016 11017 11018 /* 11019 * Function: sdaread 11020 * 11021 * Description: Driver's aread(9e) entry point function. 11022 * 11023 * Arguments: dev - device number 11024 * aio - structure pointer describing where data is to be stored 11025 * cred_p - user credential pointer 11026 * 11027 * Return Code: ENXIO 11028 * EIO 11029 * EINVAL 11030 * value returned by aphysio 11031 * 11032 * Context: Kernel thread context. 11033 */ 11034 /* ARGSUSED */ 11035 static int 11036 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11037 { 11038 struct sd_lun *un = NULL; 11039 struct uio *uio = aio->aio_uio; 11040 int secmask; 11041 int err = 0; 11042 sd_ssc_t *ssc; 11043 11044 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11045 return (ENXIO); 11046 } 11047 11048 ASSERT(!mutex_owned(SD_MUTEX(un))); 11049 11050 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11051 mutex_enter(SD_MUTEX(un)); 11052 /* 11053 * Because the call to sd_ready_and_valid will issue I/O we 11054 * must wait here if either the device is suspended or 11055 * if it's power level is changing. 11056 */ 11057 while ((un->un_state == SD_STATE_SUSPENDED) || 11058 (un->un_state == SD_STATE_PM_CHANGING)) { 11059 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11060 } 11061 un->un_ncmds_in_driver++; 11062 mutex_exit(SD_MUTEX(un)); 11063 11064 /* Initialize sd_ssc_t for internal uscsi commands */ 11065 ssc = sd_ssc_init(un); 11066 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11067 err = EIO; 11068 } else { 11069 err = 0; 11070 } 11071 sd_ssc_fini(ssc); 11072 11073 mutex_enter(SD_MUTEX(un)); 11074 un->un_ncmds_in_driver--; 11075 ASSERT(un->un_ncmds_in_driver >= 0); 11076 mutex_exit(SD_MUTEX(un)); 11077 if (err != 0) 11078 return (err); 11079 } 11080 11081 /* 11082 * Read requests are restricted to multiples of the system block size. 11083 */ 11084 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11085 !un->un_f_enable_rmw) 11086 secmask = un->un_tgt_blocksize - 1; 11087 else 11088 secmask = DEV_BSIZE - 1; 11089 11090 if (uio->uio_loffset & ((offset_t)(secmask))) { 11091 SD_ERROR(SD_LOG_READ_WRITE, un, 11092 "sdaread: file offset not modulo %d\n", 11093 secmask + 1); 11094 err = EINVAL; 11095 } else if (uio->uio_iov->iov_len & (secmask)) { 11096 SD_ERROR(SD_LOG_READ_WRITE, un, 11097 "sdaread: transfer length not modulo %d\n", 11098 secmask + 1); 11099 err = EINVAL; 11100 } else { 11101 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11102 } 11103 11104 return (err); 11105 } 11106 11107 11108 /* 11109 * Function: sdawrite 11110 * 11111 * Description: Driver's awrite(9e) entry point function. 11112 * 11113 * Arguments: dev - device number 11114 * aio - structure pointer describing where data is stored 11115 * cred_p - user credential pointer 11116 * 11117 * Return Code: ENXIO 11118 * EIO 11119 * EINVAL 11120 * value returned by aphysio 11121 * 11122 * Context: Kernel thread context. 11123 */ 11124 /* ARGSUSED */ 11125 static int 11126 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11127 { 11128 struct sd_lun *un = NULL; 11129 struct uio *uio = aio->aio_uio; 11130 int secmask; 11131 int err = 0; 11132 sd_ssc_t *ssc; 11133 11134 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11135 return (ENXIO); 11136 } 11137 11138 ASSERT(!mutex_owned(SD_MUTEX(un))); 11139 11140 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11141 mutex_enter(SD_MUTEX(un)); 11142 /* 11143 * Because the call to sd_ready_and_valid will issue I/O we 11144 * must wait here if either the device is suspended or 11145 * if it's power level is changing. 11146 */ 11147 while ((un->un_state == SD_STATE_SUSPENDED) || 11148 (un->un_state == SD_STATE_PM_CHANGING)) { 11149 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11150 } 11151 un->un_ncmds_in_driver++; 11152 mutex_exit(SD_MUTEX(un)); 11153 11154 /* Initialize sd_ssc_t for internal uscsi commands */ 11155 ssc = sd_ssc_init(un); 11156 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11157 err = EIO; 11158 } else { 11159 err = 0; 11160 } 11161 sd_ssc_fini(ssc); 11162 11163 mutex_enter(SD_MUTEX(un)); 11164 un->un_ncmds_in_driver--; 11165 ASSERT(un->un_ncmds_in_driver >= 0); 11166 mutex_exit(SD_MUTEX(un)); 11167 if (err != 0) 11168 return (err); 11169 } 11170 11171 /* 11172 * Write requests are restricted to multiples of the system block size. 11173 */ 11174 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11175 !un->un_f_enable_rmw) 11176 secmask = un->un_tgt_blocksize - 1; 11177 else 11178 secmask = DEV_BSIZE - 1; 11179 11180 if (uio->uio_loffset & ((offset_t)(secmask))) { 11181 SD_ERROR(SD_LOG_READ_WRITE, un, 11182 "sdawrite: file offset not modulo %d\n", 11183 secmask + 1); 11184 err = EINVAL; 11185 } else if (uio->uio_iov->iov_len & (secmask)) { 11186 SD_ERROR(SD_LOG_READ_WRITE, un, 11187 "sdawrite: transfer length not modulo %d\n", 11188 secmask + 1); 11189 err = EINVAL; 11190 } else { 11191 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11192 } 11193 11194 return (err); 11195 } 11196 11197 11198 11199 11200 11201 /* 11202 * Driver IO processing follows the following sequence: 11203 * 11204 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11205 * | | ^ 11206 * v v | 11207 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11208 * | | | | 11209 * v | | | 11210 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11211 * | | ^ ^ 11212 * v v | | 11213 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11214 * | | | | 11215 * +---+ | +------------+ +-------+ 11216 * | | | | 11217 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11218 * | v | | 11219 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11220 * | | ^ | 11221 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11222 * | v | | 11223 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11224 * | | ^ | 11225 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11226 * | v | | 11227 * | sd_checksum_iostart() sd_checksum_iodone() | 11228 * | | ^ | 11229 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11230 * | v | | 11231 * | sd_pm_iostart() sd_pm_iodone() | 11232 * | | ^ | 11233 * | | | | 11234 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11235 * | ^ 11236 * v | 11237 * sd_core_iostart() | 11238 * | | 11239 * | +------>(*destroypkt)() 11240 * +-> sd_start_cmds() <-+ | | 11241 * | | | v 11242 * | | | scsi_destroy_pkt(9F) 11243 * | | | 11244 * +->(*initpkt)() +- sdintr() 11245 * | | | | 11246 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11247 * | +-> scsi_setup_cdb(9F) | 11248 * | | 11249 * +--> scsi_transport(9F) | 11250 * | | 11251 * +----> SCSA ---->+ 11252 * 11253 * 11254 * This code is based upon the following presumptions: 11255 * 11256 * - iostart and iodone functions operate on buf(9S) structures. These 11257 * functions perform the necessary operations on the buf(9S) and pass 11258 * them along to the next function in the chain by using the macros 11259 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11260 * (for iodone side functions). 11261 * 11262 * - The iostart side functions may sleep. The iodone side functions 11263 * are called under interrupt context and may NOT sleep. Therefore 11264 * iodone side functions also may not call iostart side functions. 11265 * (NOTE: iostart side functions should NOT sleep for memory, as 11266 * this could result in deadlock.) 11267 * 11268 * - An iostart side function may call its corresponding iodone side 11269 * function directly (if necessary). 11270 * 11271 * - In the event of an error, an iostart side function can return a buf(9S) 11272 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11273 * b_error in the usual way of course). 11274 * 11275 * - The taskq mechanism may be used by the iodone side functions to dispatch 11276 * requests to the iostart side functions. The iostart side functions in 11277 * this case would be called under the context of a taskq thread, so it's 11278 * OK for them to block/sleep/spin in this case. 11279 * 11280 * - iostart side functions may allocate "shadow" buf(9S) structs and 11281 * pass them along to the next function in the chain. The corresponding 11282 * iodone side functions must coalesce the "shadow" bufs and return 11283 * the "original" buf to the next higher layer. 11284 * 11285 * - The b_private field of the buf(9S) struct holds a pointer to 11286 * an sd_xbuf struct, which contains information needed to 11287 * construct the scsi_pkt for the command. 11288 * 11289 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11290 * layer must acquire & release the SD_MUTEX(un) as needed. 11291 */ 11292 11293 11294 /* 11295 * Create taskq for all targets in the system. This is created at 11296 * _init(9E) and destroyed at _fini(9E). 11297 * 11298 * Note: here we set the minalloc to a reasonably high number to ensure that 11299 * we will have an adequate supply of task entries available at interrupt time. 11300 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11301 * sd_create_taskq(). Since we do not want to sleep for allocations at 11302 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11303 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11304 * requests any one instant in time. 11305 */ 11306 #define SD_TASKQ_NUMTHREADS 8 11307 #define SD_TASKQ_MINALLOC 256 11308 #define SD_TASKQ_MAXALLOC 256 11309 11310 static taskq_t *sd_tq = NULL; 11311 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11312 11313 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11314 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11315 11316 /* 11317 * The following task queue is being created for the write part of 11318 * read-modify-write of non-512 block size devices. 11319 * Limit the number of threads to 1 for now. This number has been chosen 11320 * considering the fact that it applies only to dvd ram drives/MO drives 11321 * currently. Performance for which is not main criteria at this stage. 11322 * Note: It needs to be explored if we can use a single taskq in future 11323 */ 11324 #define SD_WMR_TASKQ_NUMTHREADS 1 11325 static taskq_t *sd_wmr_tq = NULL; 11326 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11327 11328 /* 11329 * Function: sd_taskq_create 11330 * 11331 * Description: Create taskq thread(s) and preallocate task entries 11332 * 11333 * Return Code: Returns a pointer to the allocated taskq_t. 11334 * 11335 * Context: Can sleep. Requires blockable context. 11336 * 11337 * Notes: - The taskq() facility currently is NOT part of the DDI. 11338 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11339 * - taskq_create() will block for memory, also it will panic 11340 * if it cannot create the requested number of threads. 11341 * - Currently taskq_create() creates threads that cannot be 11342 * swapped. 11343 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11344 * supply of taskq entries at interrupt time (ie, so that we 11345 * do not have to sleep for memory) 11346 */ 11347 11348 static void 11349 sd_taskq_create(void) 11350 { 11351 char taskq_name[TASKQ_NAMELEN]; 11352 11353 ASSERT(sd_tq == NULL); 11354 ASSERT(sd_wmr_tq == NULL); 11355 11356 (void) snprintf(taskq_name, sizeof (taskq_name), 11357 "%s_drv_taskq", sd_label); 11358 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11359 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11360 TASKQ_PREPOPULATE)); 11361 11362 (void) snprintf(taskq_name, sizeof (taskq_name), 11363 "%s_rmw_taskq", sd_label); 11364 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11365 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11366 TASKQ_PREPOPULATE)); 11367 } 11368 11369 11370 /* 11371 * Function: sd_taskq_delete 11372 * 11373 * Description: Complementary cleanup routine for sd_taskq_create(). 11374 * 11375 * Context: Kernel thread context. 11376 */ 11377 11378 static void 11379 sd_taskq_delete(void) 11380 { 11381 ASSERT(sd_tq != NULL); 11382 ASSERT(sd_wmr_tq != NULL); 11383 taskq_destroy(sd_tq); 11384 taskq_destroy(sd_wmr_tq); 11385 sd_tq = NULL; 11386 sd_wmr_tq = NULL; 11387 } 11388 11389 11390 /* 11391 * Function: sdstrategy 11392 * 11393 * Description: Driver's strategy (9E) entry point function. 11394 * 11395 * Arguments: bp - pointer to buf(9S) 11396 * 11397 * Return Code: Always returns zero 11398 * 11399 * Context: Kernel thread context. 11400 */ 11401 11402 static int 11403 sdstrategy(struct buf *bp) 11404 { 11405 struct sd_lun *un; 11406 11407 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11408 if (un == NULL) { 11409 bioerror(bp, EIO); 11410 bp->b_resid = bp->b_bcount; 11411 biodone(bp); 11412 return (0); 11413 } 11414 11415 /* As was done in the past, fail new cmds. if state is dumping. */ 11416 if (un->un_state == SD_STATE_DUMPING) { 11417 bioerror(bp, ENXIO); 11418 bp->b_resid = bp->b_bcount; 11419 biodone(bp); 11420 return (0); 11421 } 11422 11423 ASSERT(!mutex_owned(SD_MUTEX(un))); 11424 11425 /* 11426 * Commands may sneak in while we released the mutex in 11427 * DDI_SUSPEND, we should block new commands. However, old 11428 * commands that are still in the driver at this point should 11429 * still be allowed to drain. 11430 */ 11431 mutex_enter(SD_MUTEX(un)); 11432 /* 11433 * Must wait here if either the device is suspended or 11434 * if it's power level is changing. 11435 */ 11436 while ((un->un_state == SD_STATE_SUSPENDED) || 11437 (un->un_state == SD_STATE_PM_CHANGING)) { 11438 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11439 } 11440 11441 un->un_ncmds_in_driver++; 11442 11443 /* 11444 * atapi: Since we are running the CD for now in PIO mode we need to 11445 * call bp_mapin here to avoid bp_mapin called interrupt context under 11446 * the HBA's init_pkt routine. 11447 */ 11448 if (un->un_f_cfg_is_atapi == TRUE) { 11449 mutex_exit(SD_MUTEX(un)); 11450 bp_mapin(bp); 11451 mutex_enter(SD_MUTEX(un)); 11452 } 11453 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11454 un->un_ncmds_in_driver); 11455 11456 if (bp->b_flags & B_WRITE) 11457 un->un_f_sync_cache_required = TRUE; 11458 11459 mutex_exit(SD_MUTEX(un)); 11460 11461 /* 11462 * This will (eventually) allocate the sd_xbuf area and 11463 * call sd_xbuf_strategy(). We just want to return the 11464 * result of ddi_xbuf_qstrategy so that we have an opt- 11465 * imized tail call which saves us a stack frame. 11466 */ 11467 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11468 } 11469 11470 11471 /* 11472 * Function: sd_xbuf_strategy 11473 * 11474 * Description: Function for initiating IO operations via the 11475 * ddi_xbuf_qstrategy() mechanism. 11476 * 11477 * Context: Kernel thread context. 11478 */ 11479 11480 static void 11481 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11482 { 11483 struct sd_lun *un = arg; 11484 11485 ASSERT(bp != NULL); 11486 ASSERT(xp != NULL); 11487 ASSERT(un != NULL); 11488 ASSERT(!mutex_owned(SD_MUTEX(un))); 11489 11490 /* 11491 * Initialize the fields in the xbuf and save a pointer to the 11492 * xbuf in bp->b_private. 11493 */ 11494 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11495 11496 /* Send the buf down the iostart chain */ 11497 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11498 } 11499 11500 11501 /* 11502 * Function: sd_xbuf_init 11503 * 11504 * Description: Prepare the given sd_xbuf struct for use. 11505 * 11506 * Arguments: un - ptr to softstate 11507 * bp - ptr to associated buf(9S) 11508 * xp - ptr to associated sd_xbuf 11509 * chain_type - IO chain type to use: 11510 * SD_CHAIN_NULL 11511 * SD_CHAIN_BUFIO 11512 * SD_CHAIN_USCSI 11513 * SD_CHAIN_DIRECT 11514 * SD_CHAIN_DIRECT_PRIORITY 11515 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11516 * initialization; may be NULL if none. 11517 * 11518 * Context: Kernel thread context 11519 */ 11520 11521 static void 11522 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11523 uchar_t chain_type, void *pktinfop) 11524 { 11525 int index; 11526 11527 ASSERT(un != NULL); 11528 ASSERT(bp != NULL); 11529 ASSERT(xp != NULL); 11530 11531 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11532 bp, chain_type); 11533 11534 xp->xb_un = un; 11535 xp->xb_pktp = NULL; 11536 xp->xb_pktinfo = pktinfop; 11537 xp->xb_private = bp->b_private; 11538 xp->xb_blkno = (daddr_t)bp->b_blkno; 11539 11540 /* 11541 * Set up the iostart and iodone chain indexes in the xbuf, based 11542 * upon the specified chain type to use. 11543 */ 11544 switch (chain_type) { 11545 case SD_CHAIN_NULL: 11546 /* 11547 * Fall thru to just use the values for the buf type, even 11548 * tho for the NULL chain these values will never be used. 11549 */ 11550 /* FALLTHRU */ 11551 case SD_CHAIN_BUFIO: 11552 index = un->un_buf_chain_type; 11553 if ((!un->un_f_has_removable_media) && 11554 (un->un_tgt_blocksize != 0) && 11555 (un->un_tgt_blocksize != DEV_BSIZE || 11556 un->un_f_enable_rmw)) { 11557 int secmask = 0, blknomask = 0; 11558 if (un->un_f_enable_rmw) { 11559 blknomask = 11560 (un->un_phy_blocksize / DEV_BSIZE) - 1; 11561 secmask = un->un_phy_blocksize - 1; 11562 } else { 11563 blknomask = 11564 (un->un_tgt_blocksize / DEV_BSIZE) - 1; 11565 secmask = un->un_tgt_blocksize - 1; 11566 } 11567 11568 if ((bp->b_lblkno & (blknomask)) || 11569 (bp->b_bcount & (secmask))) { 11570 if ((un->un_f_rmw_type != 11571 SD_RMW_TYPE_RETURN_ERROR) || 11572 un->un_f_enable_rmw) { 11573 if (un->un_f_pm_is_enabled == FALSE) 11574 index = 11575 SD_CHAIN_INFO_MSS_DSK_NO_PM; 11576 else 11577 index = 11578 SD_CHAIN_INFO_MSS_DISK; 11579 } 11580 } 11581 } 11582 break; 11583 case SD_CHAIN_USCSI: 11584 index = un->un_uscsi_chain_type; 11585 break; 11586 case SD_CHAIN_DIRECT: 11587 index = un->un_direct_chain_type; 11588 break; 11589 case SD_CHAIN_DIRECT_PRIORITY: 11590 index = un->un_priority_chain_type; 11591 break; 11592 default: 11593 /* We're really broken if we ever get here... */ 11594 panic("sd_xbuf_init: illegal chain type!"); 11595 /*NOTREACHED*/ 11596 } 11597 11598 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11599 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11600 11601 /* 11602 * It might be a bit easier to simply bzero the entire xbuf above, 11603 * but it turns out that since we init a fair number of members anyway, 11604 * we save a fair number cycles by doing explicit assignment of zero. 11605 */ 11606 xp->xb_pkt_flags = 0; 11607 xp->xb_dma_resid = 0; 11608 xp->xb_retry_count = 0; 11609 xp->xb_victim_retry_count = 0; 11610 xp->xb_ua_retry_count = 0; 11611 xp->xb_nr_retry_count = 0; 11612 xp->xb_sense_bp = NULL; 11613 xp->xb_sense_status = 0; 11614 xp->xb_sense_state = 0; 11615 xp->xb_sense_resid = 0; 11616 xp->xb_ena = 0; 11617 11618 bp->b_private = xp; 11619 bp->b_flags &= ~(B_DONE | B_ERROR); 11620 bp->b_resid = 0; 11621 bp->av_forw = NULL; 11622 bp->av_back = NULL; 11623 bioerror(bp, 0); 11624 11625 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11626 } 11627 11628 11629 /* 11630 * Function: sd_uscsi_strategy 11631 * 11632 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11633 * 11634 * Arguments: bp - buf struct ptr 11635 * 11636 * Return Code: Always returns 0 11637 * 11638 * Context: Kernel thread context 11639 */ 11640 11641 static int 11642 sd_uscsi_strategy(struct buf *bp) 11643 { 11644 struct sd_lun *un; 11645 struct sd_uscsi_info *uip; 11646 struct sd_xbuf *xp; 11647 uchar_t chain_type; 11648 uchar_t cmd; 11649 11650 ASSERT(bp != NULL); 11651 11652 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11653 if (un == NULL) { 11654 bioerror(bp, EIO); 11655 bp->b_resid = bp->b_bcount; 11656 biodone(bp); 11657 return (0); 11658 } 11659 11660 ASSERT(!mutex_owned(SD_MUTEX(un))); 11661 11662 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11663 11664 /* 11665 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11666 */ 11667 ASSERT(bp->b_private != NULL); 11668 uip = (struct sd_uscsi_info *)bp->b_private; 11669 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0]; 11670 11671 mutex_enter(SD_MUTEX(un)); 11672 /* 11673 * atapi: Since we are running the CD for now in PIO mode we need to 11674 * call bp_mapin here to avoid bp_mapin called interrupt context under 11675 * the HBA's init_pkt routine. 11676 */ 11677 if (un->un_f_cfg_is_atapi == TRUE) { 11678 mutex_exit(SD_MUTEX(un)); 11679 bp_mapin(bp); 11680 mutex_enter(SD_MUTEX(un)); 11681 } 11682 un->un_ncmds_in_driver++; 11683 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11684 un->un_ncmds_in_driver); 11685 11686 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) && 11687 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1)) 11688 un->un_f_sync_cache_required = TRUE; 11689 11690 mutex_exit(SD_MUTEX(un)); 11691 11692 switch (uip->ui_flags) { 11693 case SD_PATH_DIRECT: 11694 chain_type = SD_CHAIN_DIRECT; 11695 break; 11696 case SD_PATH_DIRECT_PRIORITY: 11697 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11698 break; 11699 default: 11700 chain_type = SD_CHAIN_USCSI; 11701 break; 11702 } 11703 11704 /* 11705 * We may allocate extra buf for external USCSI commands. If the 11706 * application asks for bigger than 20-byte sense data via USCSI, 11707 * SCSA layer will allocate 252 bytes sense buf for that command. 11708 */ 11709 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 11710 SENSE_LENGTH) { 11711 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 11712 MAX_SENSE_LENGTH, KM_SLEEP); 11713 } else { 11714 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 11715 } 11716 11717 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11718 11719 /* Use the index obtained within xbuf_init */ 11720 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11721 11722 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11723 11724 return (0); 11725 } 11726 11727 /* 11728 * Function: sd_send_scsi_cmd 11729 * 11730 * Description: Runs a USCSI command for user (when called thru sdioctl), 11731 * or for the driver 11732 * 11733 * Arguments: dev - the dev_t for the device 11734 * incmd - ptr to a valid uscsi_cmd struct 11735 * flag - bit flag, indicating open settings, 32/64 bit type 11736 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11737 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11738 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11739 * to use the USCSI "direct" chain and bypass the normal 11740 * command waitq. 11741 * 11742 * Return Code: 0 - successful completion of the given command 11743 * EIO - scsi_uscsi_handle_command() failed 11744 * ENXIO - soft state not found for specified dev 11745 * EINVAL 11746 * EFAULT - copyin/copyout error 11747 * return code of scsi_uscsi_handle_command(): 11748 * EIO 11749 * ENXIO 11750 * EACCES 11751 * 11752 * Context: Waits for command to complete. Can sleep. 11753 */ 11754 11755 static int 11756 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 11757 enum uio_seg dataspace, int path_flag) 11758 { 11759 struct sd_lun *un; 11760 sd_ssc_t *ssc; 11761 int rval; 11762 11763 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11764 if (un == NULL) { 11765 return (ENXIO); 11766 } 11767 11768 /* 11769 * Using sd_ssc_send to handle uscsi cmd 11770 */ 11771 ssc = sd_ssc_init(un); 11772 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag); 11773 sd_ssc_fini(ssc); 11774 11775 return (rval); 11776 } 11777 11778 /* 11779 * Function: sd_ssc_init 11780 * 11781 * Description: Uscsi end-user call this function to initialize necessary 11782 * fields, such as uscsi_cmd and sd_uscsi_info struct. 11783 * 11784 * The return value of sd_send_scsi_cmd will be treated as a 11785 * fault in various conditions. Even it is not Zero, some 11786 * callers may ignore the return value. That is to say, we can 11787 * not make an accurate assessment in sdintr, since if a 11788 * command is failed in sdintr it does not mean the caller of 11789 * sd_send_scsi_cmd will treat it as a real failure. 11790 * 11791 * To avoid printing too many error logs for a failed uscsi 11792 * packet that the caller may not treat it as a failure, the 11793 * sd will keep silent for handling all uscsi commands. 11794 * 11795 * During detach->attach and attach-open, for some types of 11796 * problems, the driver should be providing information about 11797 * the problem encountered. Device use USCSI_SILENT, which 11798 * suppresses all driver information. The result is that no 11799 * information about the problem is available. Being 11800 * completely silent during this time is inappropriate. The 11801 * driver needs a more selective filter than USCSI_SILENT, so 11802 * that information related to faults is provided. 11803 * 11804 * To make the accurate accessment, the caller of 11805 * sd_send_scsi_USCSI_CMD should take the ownership and 11806 * get necessary information to print error messages. 11807 * 11808 * If we want to print necessary info of uscsi command, we need to 11809 * keep the uscsi_cmd and sd_uscsi_info till we can make the 11810 * assessment. We use sd_ssc_init to alloc necessary 11811 * structs for sending an uscsi command and we are also 11812 * responsible for free the memory by calling 11813 * sd_ssc_fini. 11814 * 11815 * The calling secquences will look like: 11816 * sd_ssc_init-> 11817 * 11818 * ... 11819 * 11820 * sd_send_scsi_USCSI_CMD-> 11821 * sd_ssc_send-> - - - sdintr 11822 * ... 11823 * 11824 * if we think the return value should be treated as a 11825 * failure, we make the accessment here and print out 11826 * necessary by retrieving uscsi_cmd and sd_uscsi_info' 11827 * 11828 * ... 11829 * 11830 * sd_ssc_fini 11831 * 11832 * 11833 * Arguments: un - pointer to driver soft state (unit) structure for this 11834 * target. 11835 * 11836 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains 11837 * uscsi_cmd and sd_uscsi_info. 11838 * NULL - if can not alloc memory for sd_ssc_t struct 11839 * 11840 * Context: Kernel Thread. 11841 */ 11842 static sd_ssc_t * 11843 sd_ssc_init(struct sd_lun *un) 11844 { 11845 sd_ssc_t *ssc; 11846 struct uscsi_cmd *ucmdp; 11847 struct sd_uscsi_info *uip; 11848 11849 ASSERT(un != NULL); 11850 ASSERT(!mutex_owned(SD_MUTEX(un))); 11851 11852 /* 11853 * Allocate sd_ssc_t structure 11854 */ 11855 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP); 11856 11857 /* 11858 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine 11859 */ 11860 ucmdp = scsi_uscsi_alloc(); 11861 11862 /* 11863 * Allocate sd_uscsi_info structure 11864 */ 11865 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11866 11867 ssc->ssc_uscsi_cmd = ucmdp; 11868 ssc->ssc_uscsi_info = uip; 11869 ssc->ssc_un = un; 11870 11871 return (ssc); 11872 } 11873 11874 /* 11875 * Function: sd_ssc_fini 11876 * 11877 * Description: To free sd_ssc_t and it's hanging off 11878 * 11879 * Arguments: ssc - struct pointer of sd_ssc_t. 11880 */ 11881 static void 11882 sd_ssc_fini(sd_ssc_t *ssc) 11883 { 11884 scsi_uscsi_free(ssc->ssc_uscsi_cmd); 11885 11886 if (ssc->ssc_uscsi_info != NULL) { 11887 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info)); 11888 ssc->ssc_uscsi_info = NULL; 11889 } 11890 11891 kmem_free(ssc, sizeof (sd_ssc_t)); 11892 ssc = NULL; 11893 } 11894 11895 /* 11896 * Function: sd_ssc_send 11897 * 11898 * Description: Runs a USCSI command for user when called through sdioctl, 11899 * or for the driver. 11900 * 11901 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11902 * sd_uscsi_info in. 11903 * incmd - ptr to a valid uscsi_cmd struct 11904 * flag - bit flag, indicating open settings, 32/64 bit type 11905 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11906 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11907 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11908 * to use the USCSI "direct" chain and bypass the normal 11909 * command waitq. 11910 * 11911 * Return Code: 0 - successful completion of the given command 11912 * EIO - scsi_uscsi_handle_command() failed 11913 * ENXIO - soft state not found for specified dev 11914 * ECANCELED - command cancelled due to low power 11915 * EINVAL 11916 * EFAULT - copyin/copyout error 11917 * return code of scsi_uscsi_handle_command(): 11918 * EIO 11919 * ENXIO 11920 * EACCES 11921 * 11922 * Context: Kernel Thread; 11923 * Waits for command to complete. Can sleep. 11924 */ 11925 static int 11926 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag, 11927 enum uio_seg dataspace, int path_flag) 11928 { 11929 struct sd_uscsi_info *uip; 11930 struct uscsi_cmd *uscmd; 11931 struct sd_lun *un; 11932 dev_t dev; 11933 11934 int format = 0; 11935 int rval; 11936 11937 ASSERT(ssc != NULL); 11938 un = ssc->ssc_un; 11939 ASSERT(un != NULL); 11940 uscmd = ssc->ssc_uscsi_cmd; 11941 ASSERT(uscmd != NULL); 11942 ASSERT(!mutex_owned(SD_MUTEX(un))); 11943 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 11944 /* 11945 * If enter here, it indicates that the previous uscsi 11946 * command has not been processed by sd_ssc_assessment. 11947 * This is violating our rules of FMA telemetry processing. 11948 * We should print out this message and the last undisposed 11949 * uscsi command. 11950 */ 11951 if (uscmd->uscsi_cdb != NULL) { 11952 SD_INFO(SD_LOG_SDTEST, un, 11953 "sd_ssc_send is missing the alternative " 11954 "sd_ssc_assessment when running command 0x%x.\n", 11955 uscmd->uscsi_cdb[0]); 11956 } 11957 /* 11958 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be 11959 * the initial status. 11960 */ 11961 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11962 } 11963 11964 /* 11965 * We need to make sure sd_ssc_send will have sd_ssc_assessment 11966 * followed to avoid missing FMA telemetries. 11967 */ 11968 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT; 11969 11970 /* 11971 * if USCSI_PMFAILFAST is set and un is in low power, fail the 11972 * command immediately. 11973 */ 11974 mutex_enter(SD_MUTEX(un)); 11975 mutex_enter(&un->un_pm_mutex); 11976 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) && 11977 SD_DEVICE_IS_IN_LOW_POWER(un)) { 11978 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:" 11979 "un:0x%p is in low power\n", un); 11980 mutex_exit(&un->un_pm_mutex); 11981 mutex_exit(SD_MUTEX(un)); 11982 return (ECANCELED); 11983 } 11984 mutex_exit(&un->un_pm_mutex); 11985 mutex_exit(SD_MUTEX(un)); 11986 11987 #ifdef SDDEBUG 11988 switch (dataspace) { 11989 case UIO_USERSPACE: 11990 SD_TRACE(SD_LOG_IO, un, 11991 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un); 11992 break; 11993 case UIO_SYSSPACE: 11994 SD_TRACE(SD_LOG_IO, un, 11995 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un); 11996 break; 11997 default: 11998 SD_TRACE(SD_LOG_IO, un, 11999 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un); 12000 break; 12001 } 12002 #endif 12003 12004 rval = scsi_uscsi_copyin((intptr_t)incmd, flag, 12005 SD_ADDRESS(un), &uscmd); 12006 if (rval != 0) { 12007 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 12008 "scsi_uscsi_alloc_and_copyin failed\n", un); 12009 return (rval); 12010 } 12011 12012 if ((uscmd->uscsi_cdb != NULL) && 12013 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 12014 mutex_enter(SD_MUTEX(un)); 12015 un->un_f_format_in_progress = TRUE; 12016 mutex_exit(SD_MUTEX(un)); 12017 format = 1; 12018 } 12019 12020 /* 12021 * Allocate an sd_uscsi_info struct and fill it with the info 12022 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12023 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12024 * since we allocate the buf here in this function, we do not 12025 * need to preserve the prior contents of b_private. 12026 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12027 */ 12028 uip = ssc->ssc_uscsi_info; 12029 uip->ui_flags = path_flag; 12030 uip->ui_cmdp = uscmd; 12031 12032 /* 12033 * Commands sent with priority are intended for error recovery 12034 * situations, and do not have retries performed. 12035 */ 12036 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12037 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12038 } 12039 uscmd->uscsi_flags &= ~USCSI_NOINTR; 12040 12041 dev = SD_GET_DEV(un); 12042 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 12043 sd_uscsi_strategy, NULL, uip); 12044 12045 /* 12046 * mark ssc_flags right after handle_cmd to make sure 12047 * the uscsi has been sent 12048 */ 12049 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED; 12050 12051 #ifdef SDDEBUG 12052 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12053 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12054 uscmd->uscsi_status, uscmd->uscsi_resid); 12055 if (uscmd->uscsi_bufaddr != NULL) { 12056 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12057 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12058 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12059 if (dataspace == UIO_SYSSPACE) { 12060 SD_DUMP_MEMORY(un, SD_LOG_IO, 12061 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12062 uscmd->uscsi_buflen, SD_LOG_HEX); 12063 } 12064 } 12065 #endif 12066 12067 if (format == 1) { 12068 mutex_enter(SD_MUTEX(un)); 12069 un->un_f_format_in_progress = FALSE; 12070 mutex_exit(SD_MUTEX(un)); 12071 } 12072 12073 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd); 12074 12075 return (rval); 12076 } 12077 12078 /* 12079 * Function: sd_ssc_print 12080 * 12081 * Description: Print information available to the console. 12082 * 12083 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12084 * sd_uscsi_info in. 12085 * sd_severity - log level. 12086 * Context: Kernel thread or interrupt context. 12087 */ 12088 static void 12089 sd_ssc_print(sd_ssc_t *ssc, int sd_severity) 12090 { 12091 struct uscsi_cmd *ucmdp; 12092 struct scsi_device *devp; 12093 dev_info_t *devinfo; 12094 uchar_t *sensep; 12095 int senlen; 12096 union scsi_cdb *cdbp; 12097 uchar_t com; 12098 extern struct scsi_key_strings scsi_cmds[]; 12099 12100 ASSERT(ssc != NULL); 12101 ASSERT(ssc->ssc_un != NULL); 12102 12103 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT) 12104 return; 12105 ucmdp = ssc->ssc_uscsi_cmd; 12106 devp = SD_SCSI_DEVP(ssc->ssc_un); 12107 devinfo = SD_DEVINFO(ssc->ssc_un); 12108 ASSERT(ucmdp != NULL); 12109 ASSERT(devp != NULL); 12110 ASSERT(devinfo != NULL); 12111 sensep = (uint8_t *)ucmdp->uscsi_rqbuf; 12112 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid; 12113 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb; 12114 12115 /* In certain case (like DOORLOCK), the cdb could be NULL. */ 12116 if (cdbp == NULL) 12117 return; 12118 /* We don't print log if no sense data available. */ 12119 if (senlen == 0) 12120 sensep = NULL; 12121 com = cdbp->scc_cmd; 12122 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com, 12123 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL); 12124 } 12125 12126 /* 12127 * Function: sd_ssc_assessment 12128 * 12129 * Description: We use this function to make an assessment at the point 12130 * where SD driver may encounter a potential error. 12131 * 12132 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12133 * sd_uscsi_info in. 12134 * tp_assess - a hint of strategy for ereport posting. 12135 * Possible values of tp_assess include: 12136 * SD_FMT_IGNORE - we don't post any ereport because we're 12137 * sure that it is ok to ignore the underlying problems. 12138 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now 12139 * but it might be not correct to ignore the underlying hardware 12140 * error. 12141 * SD_FMT_STATUS_CHECK - we will post an ereport with the 12142 * payload driver-assessment of value "fail" or 12143 * "fatal"(depending on what information we have here). This 12144 * assessment value is usually set when SD driver think there 12145 * is a potential error occurred(Typically, when return value 12146 * of the SCSI command is EIO). 12147 * SD_FMT_STANDARD - we will post an ereport with the payload 12148 * driver-assessment of value "info". This assessment value is 12149 * set when the SCSI command returned successfully and with 12150 * sense data sent back. 12151 * 12152 * Context: Kernel thread. 12153 */ 12154 static void 12155 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess) 12156 { 12157 int senlen = 0; 12158 struct uscsi_cmd *ucmdp = NULL; 12159 struct sd_lun *un; 12160 12161 ASSERT(ssc != NULL); 12162 un = ssc->ssc_un; 12163 ASSERT(un != NULL); 12164 ucmdp = ssc->ssc_uscsi_cmd; 12165 ASSERT(ucmdp != NULL); 12166 12167 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 12168 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT; 12169 } else { 12170 /* 12171 * If enter here, it indicates that we have a wrong 12172 * calling sequence of sd_ssc_send and sd_ssc_assessment, 12173 * both of which should be called in a pair in case of 12174 * loss of FMA telemetries. 12175 */ 12176 if (ucmdp->uscsi_cdb != NULL) { 12177 SD_INFO(SD_LOG_SDTEST, un, 12178 "sd_ssc_assessment is missing the " 12179 "alternative sd_ssc_send when running 0x%x, " 12180 "or there are superfluous sd_ssc_assessment for " 12181 "the same sd_ssc_send.\n", 12182 ucmdp->uscsi_cdb[0]); 12183 } 12184 /* 12185 * Set the ssc_flags to the initial value to avoid passing 12186 * down dirty flags to the following sd_ssc_send function. 12187 */ 12188 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12189 return; 12190 } 12191 12192 /* 12193 * Only handle an issued command which is waiting for assessment. 12194 * A command which is not issued will not have 12195 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here. 12196 */ 12197 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) { 12198 sd_ssc_print(ssc, SCSI_ERR_INFO); 12199 return; 12200 } else { 12201 /* 12202 * For an issued command, we should clear this flag in 12203 * order to make the sd_ssc_t structure be used off 12204 * multiple uscsi commands. 12205 */ 12206 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED; 12207 } 12208 12209 /* 12210 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set) 12211 * commands here. And we should clear the ssc_flags before return. 12212 */ 12213 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) { 12214 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12215 return; 12216 } 12217 12218 switch (tp_assess) { 12219 case SD_FMT_IGNORE: 12220 case SD_FMT_IGNORE_COMPROMISE: 12221 break; 12222 case SD_FMT_STATUS_CHECK: 12223 /* 12224 * For a failed command(including the succeeded command 12225 * with invalid data sent back). 12226 */ 12227 sd_ssc_post(ssc, SD_FM_DRV_FATAL); 12228 break; 12229 case SD_FMT_STANDARD: 12230 /* 12231 * Always for the succeeded commands probably with sense 12232 * data sent back. 12233 * Limitation: 12234 * We can only handle a succeeded command with sense 12235 * data sent back when auto-request-sense is enabled. 12236 */ 12237 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen - 12238 ssc->ssc_uscsi_cmd->uscsi_rqresid; 12239 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) && 12240 (un->un_f_arq_enabled == TRUE) && 12241 senlen > 0 && 12242 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) { 12243 sd_ssc_post(ssc, SD_FM_DRV_NOTICE); 12244 } 12245 break; 12246 default: 12247 /* 12248 * Should not have other type of assessment. 12249 */ 12250 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 12251 "sd_ssc_assessment got wrong " 12252 "sd_type_assessment %d.\n", tp_assess); 12253 break; 12254 } 12255 /* 12256 * Clear up the ssc_flags before return. 12257 */ 12258 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12259 } 12260 12261 /* 12262 * Function: sd_ssc_post 12263 * 12264 * Description: 1. read the driver property to get fm-scsi-log flag. 12265 * 2. print log if fm_log_capable is non-zero. 12266 * 3. call sd_ssc_ereport_post to post ereport if possible. 12267 * 12268 * Context: May be called from kernel thread or interrupt context. 12269 */ 12270 static void 12271 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess) 12272 { 12273 struct sd_lun *un; 12274 int sd_severity; 12275 12276 ASSERT(ssc != NULL); 12277 un = ssc->ssc_un; 12278 ASSERT(un != NULL); 12279 12280 /* 12281 * We may enter here from sd_ssc_assessment(for USCSI command) or 12282 * by directly called from sdintr context. 12283 * We don't handle a non-disk drive(CD-ROM, removable media). 12284 * Clear the ssc_flags before return in case we've set 12285 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk 12286 * driver. 12287 */ 12288 if (ISCD(un) || un->un_f_has_removable_media) { 12289 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12290 return; 12291 } 12292 12293 switch (sd_assess) { 12294 case SD_FM_DRV_FATAL: 12295 sd_severity = SCSI_ERR_FATAL; 12296 break; 12297 case SD_FM_DRV_RECOVERY: 12298 sd_severity = SCSI_ERR_RECOVERED; 12299 break; 12300 case SD_FM_DRV_RETRY: 12301 sd_severity = SCSI_ERR_RETRYABLE; 12302 break; 12303 case SD_FM_DRV_NOTICE: 12304 sd_severity = SCSI_ERR_INFO; 12305 break; 12306 default: 12307 sd_severity = SCSI_ERR_UNKNOWN; 12308 } 12309 /* print log */ 12310 sd_ssc_print(ssc, sd_severity); 12311 12312 /* always post ereport */ 12313 sd_ssc_ereport_post(ssc, sd_assess); 12314 } 12315 12316 /* 12317 * Function: sd_ssc_set_info 12318 * 12319 * Description: Mark ssc_flags and set ssc_info which would be the 12320 * payload of uderr ereport. This function will cause 12321 * sd_ssc_ereport_post to post uderr ereport only. 12322 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI), 12323 * the function will also call SD_ERROR or scsi_log for a 12324 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device. 12325 * 12326 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12327 * sd_uscsi_info in. 12328 * ssc_flags - indicate the sub-category of a uderr. 12329 * comp - this argument is meaningful only when 12330 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible 12331 * values include: 12332 * > 0, SD_ERROR is used with comp as the driver logging 12333 * component; 12334 * = 0, scsi-log is used to log error telemetries; 12335 * < 0, no log available for this telemetry. 12336 * 12337 * Context: Kernel thread or interrupt context 12338 */ 12339 static void 12340 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...) 12341 { 12342 va_list ap; 12343 12344 ASSERT(ssc != NULL); 12345 ASSERT(ssc->ssc_un != NULL); 12346 12347 ssc->ssc_flags |= ssc_flags; 12348 va_start(ap, fmt); 12349 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap); 12350 va_end(ap); 12351 12352 /* 12353 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command 12354 * with invalid data sent back. For non-uscsi command, the 12355 * following code will be bypassed. 12356 */ 12357 if (ssc_flags & SSC_FLAGS_INVALID_DATA) { 12358 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) { 12359 /* 12360 * If the error belong to certain component and we 12361 * do not want it to show up on the console, we 12362 * will use SD_ERROR, otherwise scsi_log is 12363 * preferred. 12364 */ 12365 if (comp > 0) { 12366 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info); 12367 } else if (comp == 0) { 12368 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label, 12369 CE_WARN, ssc->ssc_info); 12370 } 12371 } 12372 } 12373 } 12374 12375 /* 12376 * Function: sd_buf_iodone 12377 * 12378 * Description: Frees the sd_xbuf & returns the buf to its originator. 12379 * 12380 * Context: May be called from interrupt context. 12381 */ 12382 /* ARGSUSED */ 12383 static void 12384 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12385 { 12386 struct sd_xbuf *xp; 12387 12388 ASSERT(un != NULL); 12389 ASSERT(bp != NULL); 12390 ASSERT(!mutex_owned(SD_MUTEX(un))); 12391 12392 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12393 12394 xp = SD_GET_XBUF(bp); 12395 ASSERT(xp != NULL); 12396 12397 /* xbuf is gone after this */ 12398 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) { 12399 mutex_enter(SD_MUTEX(un)); 12400 12401 /* 12402 * Grab time when the cmd completed. 12403 * This is used for determining if the system has been 12404 * idle long enough to make it idle to the PM framework. 12405 * This is for lowering the overhead, and therefore improving 12406 * performance per I/O operation. 12407 */ 12408 un->un_pm_idle_time = ddi_get_time(); 12409 12410 un->un_ncmds_in_driver--; 12411 ASSERT(un->un_ncmds_in_driver >= 0); 12412 SD_INFO(SD_LOG_IO, un, 12413 "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12414 un->un_ncmds_in_driver); 12415 12416 mutex_exit(SD_MUTEX(un)); 12417 } 12418 12419 biodone(bp); /* bp is gone after this */ 12420 12421 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12422 } 12423 12424 12425 /* 12426 * Function: sd_uscsi_iodone 12427 * 12428 * Description: Frees the sd_xbuf & returns the buf to its originator. 12429 * 12430 * Context: May be called from interrupt context. 12431 */ 12432 /* ARGSUSED */ 12433 static void 12434 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12435 { 12436 struct sd_xbuf *xp; 12437 12438 ASSERT(un != NULL); 12439 ASSERT(bp != NULL); 12440 12441 xp = SD_GET_XBUF(bp); 12442 ASSERT(xp != NULL); 12443 ASSERT(!mutex_owned(SD_MUTEX(un))); 12444 12445 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12446 12447 bp->b_private = xp->xb_private; 12448 12449 mutex_enter(SD_MUTEX(un)); 12450 12451 /* 12452 * Grab time when the cmd completed. 12453 * This is used for determining if the system has been 12454 * idle long enough to make it idle to the PM framework. 12455 * This is for lowering the overhead, and therefore improving 12456 * performance per I/O operation. 12457 */ 12458 un->un_pm_idle_time = ddi_get_time(); 12459 12460 un->un_ncmds_in_driver--; 12461 ASSERT(un->un_ncmds_in_driver >= 0); 12462 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12463 un->un_ncmds_in_driver); 12464 12465 mutex_exit(SD_MUTEX(un)); 12466 12467 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 12468 SENSE_LENGTH) { 12469 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 12470 MAX_SENSE_LENGTH); 12471 } else { 12472 kmem_free(xp, sizeof (struct sd_xbuf)); 12473 } 12474 12475 biodone(bp); 12476 12477 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12478 } 12479 12480 12481 /* 12482 * Function: sd_mapblockaddr_iostart 12483 * 12484 * Description: Verify request lies within the partition limits for 12485 * the indicated minor device. Issue "overrun" buf if 12486 * request would exceed partition range. Converts 12487 * partition-relative block address to absolute. 12488 * 12489 * Upon exit of this function: 12490 * 1.I/O is aligned 12491 * xp->xb_blkno represents the absolute sector address 12492 * 2.I/O is misaligned 12493 * xp->xb_blkno represents the absolute logical block address 12494 * based on DEV_BSIZE. The logical block address will be 12495 * converted to physical sector address in sd_mapblocksize_\ 12496 * iostart. 12497 * 3.I/O is misaligned but is aligned in "overrun" buf 12498 * xp->xb_blkno represents the absolute logical block address 12499 * based on DEV_BSIZE. The logical block address will be 12500 * converted to physical sector address in sd_mapblocksize_\ 12501 * iostart. But no RMW will be issued in this case. 12502 * 12503 * Context: Can sleep 12504 * 12505 * Issues: This follows what the old code did, in terms of accessing 12506 * some of the partition info in the unit struct without holding 12507 * the mutext. This is a general issue, if the partition info 12508 * can be altered while IO is in progress... as soon as we send 12509 * a buf, its partitioning can be invalid before it gets to the 12510 * device. Probably the right fix is to move partitioning out 12511 * of the driver entirely. 12512 */ 12513 12514 static void 12515 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12516 { 12517 diskaddr_t nblocks; /* #blocks in the given partition */ 12518 daddr_t blocknum; /* Block number specified by the buf */ 12519 size_t requested_nblocks; 12520 size_t available_nblocks; 12521 int partition; 12522 diskaddr_t partition_offset; 12523 struct sd_xbuf *xp; 12524 int secmask = 0, blknomask = 0; 12525 ushort_t is_aligned = TRUE; 12526 12527 ASSERT(un != NULL); 12528 ASSERT(bp != NULL); 12529 ASSERT(!mutex_owned(SD_MUTEX(un))); 12530 12531 SD_TRACE(SD_LOG_IO_PARTITION, un, 12532 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12533 12534 xp = SD_GET_XBUF(bp); 12535 ASSERT(xp != NULL); 12536 12537 /* 12538 * If the geometry is not indicated as valid, attempt to access 12539 * the unit & verify the geometry/label. This can be the case for 12540 * removable-media devices, of if the device was opened in 12541 * NDELAY/NONBLOCK mode. 12542 */ 12543 partition = SDPART(bp->b_edev); 12544 12545 if (!SD_IS_VALID_LABEL(un)) { 12546 sd_ssc_t *ssc; 12547 /* 12548 * Initialize sd_ssc_t for internal uscsi commands 12549 * In case of potential porformance issue, we need 12550 * to alloc memory only if there is invalid label 12551 */ 12552 ssc = sd_ssc_init(un); 12553 12554 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) { 12555 /* 12556 * For removable devices it is possible to start an 12557 * I/O without a media by opening the device in nodelay 12558 * mode. Also for writable CDs there can be many 12559 * scenarios where there is no geometry yet but volume 12560 * manager is trying to issue a read() just because 12561 * it can see TOC on the CD. So do not print a message 12562 * for removables. 12563 */ 12564 if (!un->un_f_has_removable_media) { 12565 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12566 "i/o to invalid geometry\n"); 12567 } 12568 bioerror(bp, EIO); 12569 bp->b_resid = bp->b_bcount; 12570 SD_BEGIN_IODONE(index, un, bp); 12571 12572 sd_ssc_fini(ssc); 12573 return; 12574 } 12575 sd_ssc_fini(ssc); 12576 } 12577 12578 nblocks = 0; 12579 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 12580 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 12581 12582 if (un->un_f_enable_rmw) { 12583 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1; 12584 secmask = un->un_phy_blocksize - 1; 12585 } else { 12586 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 12587 secmask = un->un_tgt_blocksize - 1; 12588 } 12589 12590 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) { 12591 is_aligned = FALSE; 12592 } 12593 12594 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) { 12595 /* 12596 * If I/O is aligned, no need to involve RMW(Read Modify Write) 12597 * Convert the logical block number to target's physical sector 12598 * number. 12599 */ 12600 if (is_aligned) { 12601 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno); 12602 } else { 12603 switch (un->un_f_rmw_type) { 12604 case SD_RMW_TYPE_RETURN_ERROR: 12605 if (un->un_f_enable_rmw) 12606 break; 12607 else { 12608 bp->b_flags |= B_ERROR; 12609 goto error_exit; 12610 } 12611 12612 case SD_RMW_TYPE_DEFAULT: 12613 mutex_enter(SD_MUTEX(un)); 12614 if (!un->un_f_enable_rmw && 12615 un->un_rmw_msg_timeid == NULL) { 12616 scsi_log(SD_DEVINFO(un), sd_label, 12617 CE_WARN, "I/O request is not " 12618 "aligned with %d disk sector size. " 12619 "It is handled through Read Modify " 12620 "Write but the performance is " 12621 "very low.\n", 12622 un->un_tgt_blocksize); 12623 un->un_rmw_msg_timeid = 12624 timeout(sd_rmw_msg_print_handler, 12625 un, SD_RMW_MSG_PRINT_TIMEOUT); 12626 } else { 12627 un->un_rmw_incre_count ++; 12628 } 12629 mutex_exit(SD_MUTEX(un)); 12630 break; 12631 12632 case SD_RMW_TYPE_NO_WARNING: 12633 default: 12634 break; 12635 } 12636 12637 nblocks = SD_TGT2SYSBLOCK(un, nblocks); 12638 partition_offset = SD_TGT2SYSBLOCK(un, 12639 partition_offset); 12640 } 12641 } 12642 12643 /* 12644 * blocknum is the starting block number of the request. At this 12645 * point it is still relative to the start of the minor device. 12646 */ 12647 blocknum = xp->xb_blkno; 12648 12649 /* 12650 * Legacy: If the starting block number is one past the last block 12651 * in the partition, do not set B_ERROR in the buf. 12652 */ 12653 if (blocknum == nblocks) { 12654 goto error_exit; 12655 } 12656 12657 /* 12658 * Confirm that the first block of the request lies within the 12659 * partition limits. Also the requested number of bytes must be 12660 * a multiple of the system block size. 12661 */ 12662 if ((blocknum < 0) || (blocknum >= nblocks) || 12663 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) { 12664 bp->b_flags |= B_ERROR; 12665 goto error_exit; 12666 } 12667 12668 /* 12669 * If the requsted # blocks exceeds the available # blocks, that 12670 * is an overrun of the partition. 12671 */ 12672 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12673 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 12674 } else { 12675 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount); 12676 } 12677 12678 available_nblocks = (size_t)(nblocks - blocknum); 12679 ASSERT(nblocks >= blocknum); 12680 12681 if (requested_nblocks > available_nblocks) { 12682 size_t resid; 12683 12684 /* 12685 * Allocate an "overrun" buf to allow the request to proceed 12686 * for the amount of space available in the partition. The 12687 * amount not transferred will be added into the b_resid 12688 * when the operation is complete. The overrun buf 12689 * replaces the original buf here, and the original buf 12690 * is saved inside the overrun buf, for later use. 12691 */ 12692 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12693 resid = SD_TGTBLOCKS2BYTES(un, 12694 (offset_t)(requested_nblocks - available_nblocks)); 12695 } else { 12696 resid = SD_SYSBLOCKS2BYTES( 12697 (offset_t)(requested_nblocks - available_nblocks)); 12698 } 12699 12700 size_t count = bp->b_bcount - resid; 12701 /* 12702 * Note: count is an unsigned entity thus it'll NEVER 12703 * be less than 0 so ASSERT the original values are 12704 * correct. 12705 */ 12706 ASSERT(bp->b_bcount >= resid); 12707 12708 bp = sd_bioclone_alloc(bp, count, blocknum, 12709 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12710 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12711 ASSERT(xp != NULL); 12712 } 12713 12714 /* At this point there should be no residual for this buf. */ 12715 ASSERT(bp->b_resid == 0); 12716 12717 /* Convert the block number to an absolute address. */ 12718 xp->xb_blkno += partition_offset; 12719 12720 SD_NEXT_IOSTART(index, un, bp); 12721 12722 SD_TRACE(SD_LOG_IO_PARTITION, un, 12723 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12724 12725 return; 12726 12727 error_exit: 12728 bp->b_resid = bp->b_bcount; 12729 SD_BEGIN_IODONE(index, un, bp); 12730 SD_TRACE(SD_LOG_IO_PARTITION, un, 12731 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12732 } 12733 12734 12735 /* 12736 * Function: sd_mapblockaddr_iodone 12737 * 12738 * Description: Completion-side processing for partition management. 12739 * 12740 * Context: May be called under interrupt context 12741 */ 12742 12743 static void 12744 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12745 { 12746 /* int partition; */ /* Not used, see below. */ 12747 ASSERT(un != NULL); 12748 ASSERT(bp != NULL); 12749 ASSERT(!mutex_owned(SD_MUTEX(un))); 12750 12751 SD_TRACE(SD_LOG_IO_PARTITION, un, 12752 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12753 12754 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12755 /* 12756 * We have an "overrun" buf to deal with... 12757 */ 12758 struct sd_xbuf *xp; 12759 struct buf *obp; /* ptr to the original buf */ 12760 12761 xp = SD_GET_XBUF(bp); 12762 ASSERT(xp != NULL); 12763 12764 /* Retrieve the pointer to the original buf */ 12765 obp = (struct buf *)xp->xb_private; 12766 ASSERT(obp != NULL); 12767 12768 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12769 bioerror(obp, bp->b_error); 12770 12771 sd_bioclone_free(bp); 12772 12773 /* 12774 * Get back the original buf. 12775 * Note that since the restoration of xb_blkno below 12776 * was removed, the sd_xbuf is not needed. 12777 */ 12778 bp = obp; 12779 /* 12780 * xp = SD_GET_XBUF(bp); 12781 * ASSERT(xp != NULL); 12782 */ 12783 } 12784 12785 /* 12786 * Convert sd->xb_blkno back to a minor-device relative value. 12787 * Note: this has been commented out, as it is not needed in the 12788 * current implementation of the driver (ie, since this function 12789 * is at the top of the layering chains, so the info will be 12790 * discarded) and it is in the "hot" IO path. 12791 * 12792 * partition = getminor(bp->b_edev) & SDPART_MASK; 12793 * xp->xb_blkno -= un->un_offset[partition]; 12794 */ 12795 12796 SD_NEXT_IODONE(index, un, bp); 12797 12798 SD_TRACE(SD_LOG_IO_PARTITION, un, 12799 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12800 } 12801 12802 12803 /* 12804 * Function: sd_mapblocksize_iostart 12805 * 12806 * Description: Convert between system block size (un->un_sys_blocksize) 12807 * and target block size (un->un_tgt_blocksize). 12808 * 12809 * Context: Can sleep to allocate resources. 12810 * 12811 * Assumptions: A higher layer has already performed any partition validation, 12812 * and converted the xp->xb_blkno to an absolute value relative 12813 * to the start of the device. 12814 * 12815 * It is also assumed that the higher layer has implemented 12816 * an "overrun" mechanism for the case where the request would 12817 * read/write beyond the end of a partition. In this case we 12818 * assume (and ASSERT) that bp->b_resid == 0. 12819 * 12820 * Note: The implementation for this routine assumes the target 12821 * block size remains constant between allocation and transport. 12822 */ 12823 12824 static void 12825 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12826 { 12827 struct sd_mapblocksize_info *bsp; 12828 struct sd_xbuf *xp; 12829 offset_t first_byte; 12830 daddr_t start_block, end_block; 12831 daddr_t request_bytes; 12832 ushort_t is_aligned = FALSE; 12833 12834 ASSERT(un != NULL); 12835 ASSERT(bp != NULL); 12836 ASSERT(!mutex_owned(SD_MUTEX(un))); 12837 ASSERT(bp->b_resid == 0); 12838 12839 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12840 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12841 12842 /* 12843 * For a non-writable CD, a write request is an error 12844 */ 12845 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12846 (un->un_f_mmc_writable_media == FALSE)) { 12847 bioerror(bp, EIO); 12848 bp->b_resid = bp->b_bcount; 12849 SD_BEGIN_IODONE(index, un, bp); 12850 return; 12851 } 12852 12853 /* 12854 * We do not need a shadow buf if the device is using 12855 * un->un_sys_blocksize as its block size or if bcount == 0. 12856 * In this case there is no layer-private data block allocated. 12857 */ 12858 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 12859 (bp->b_bcount == 0)) { 12860 goto done; 12861 } 12862 12863 #if defined(__i386) || defined(__amd64) 12864 /* We do not support non-block-aligned transfers for ROD devices */ 12865 ASSERT(!ISROD(un)); 12866 #endif 12867 12868 xp = SD_GET_XBUF(bp); 12869 ASSERT(xp != NULL); 12870 12871 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12872 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12873 un->un_tgt_blocksize, DEV_BSIZE); 12874 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12875 "request start block:0x%x\n", xp->xb_blkno); 12876 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12877 "request len:0x%x\n", bp->b_bcount); 12878 12879 /* 12880 * Allocate the layer-private data area for the mapblocksize layer. 12881 * Layers are allowed to use the xp_private member of the sd_xbuf 12882 * struct to store the pointer to their layer-private data block, but 12883 * each layer also has the responsibility of restoring the prior 12884 * contents of xb_private before returning the buf/xbuf to the 12885 * higher layer that sent it. 12886 * 12887 * Here we save the prior contents of xp->xb_private into the 12888 * bsp->mbs_oprivate field of our layer-private data area. This value 12889 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12890 * the layer-private area and returning the buf/xbuf to the layer 12891 * that sent it. 12892 * 12893 * Note that here we use kmem_zalloc for the allocation as there are 12894 * parts of the mapblocksize code that expect certain fields to be 12895 * zero unless explicitly set to a required value. 12896 */ 12897 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12898 bsp->mbs_oprivate = xp->xb_private; 12899 xp->xb_private = bsp; 12900 12901 /* 12902 * This treats the data on the disk (target) as an array of bytes. 12903 * first_byte is the byte offset, from the beginning of the device, 12904 * to the location of the request. This is converted from a 12905 * un->un_sys_blocksize block address to a byte offset, and then back 12906 * to a block address based upon a un->un_tgt_blocksize block size. 12907 * 12908 * xp->xb_blkno should be absolute upon entry into this function, 12909 * but, but it is based upon partitions that use the "system" 12910 * block size. It must be adjusted to reflect the block size of 12911 * the target. 12912 * 12913 * Note that end_block is actually the block that follows the last 12914 * block of the request, but that's what is needed for the computation. 12915 */ 12916 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 12917 if (un->un_f_enable_rmw) { 12918 start_block = xp->xb_blkno = 12919 (first_byte / un->un_phy_blocksize) * 12920 (un->un_phy_blocksize / DEV_BSIZE); 12921 end_block = ((first_byte + bp->b_bcount + 12922 un->un_phy_blocksize - 1) / un->un_phy_blocksize) * 12923 (un->un_phy_blocksize / DEV_BSIZE); 12924 } else { 12925 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12926 end_block = (first_byte + bp->b_bcount + 12927 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 12928 } 12929 12930 /* request_bytes is rounded up to a multiple of the target block size */ 12931 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12932 12933 /* 12934 * See if the starting address of the request and the request 12935 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12936 * then we do not need to allocate a shadow buf to handle the request. 12937 */ 12938 if (un->un_f_enable_rmw) { 12939 if (((first_byte % un->un_phy_blocksize) == 0) && 12940 ((bp->b_bcount % un->un_phy_blocksize) == 0)) { 12941 is_aligned = TRUE; 12942 } 12943 } else { 12944 if (((first_byte % un->un_tgt_blocksize) == 0) && 12945 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12946 is_aligned = TRUE; 12947 } 12948 } 12949 12950 if ((bp->b_flags & B_READ) == 0) { 12951 /* 12952 * Lock the range for a write operation. An aligned request is 12953 * considered a simple write; otherwise the request must be a 12954 * read-modify-write. 12955 */ 12956 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12957 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12958 } 12959 12960 /* 12961 * Alloc a shadow buf if the request is not aligned. Also, this is 12962 * where the READ command is generated for a read-modify-write. (The 12963 * write phase is deferred until after the read completes.) 12964 */ 12965 if (is_aligned == FALSE) { 12966 12967 struct sd_mapblocksize_info *shadow_bsp; 12968 struct sd_xbuf *shadow_xp; 12969 struct buf *shadow_bp; 12970 12971 /* 12972 * Allocate the shadow buf and it associated xbuf. Note that 12973 * after this call the xb_blkno value in both the original 12974 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12975 * same: absolute relative to the start of the device, and 12976 * adjusted for the target block size. The b_blkno in the 12977 * shadow buf will also be set to this value. We should never 12978 * change b_blkno in the original bp however. 12979 * 12980 * Note also that the shadow buf will always need to be a 12981 * READ command, regardless of whether the incoming command 12982 * is a READ or a WRITE. 12983 */ 12984 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12985 xp->xb_blkno, 12986 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12987 12988 shadow_xp = SD_GET_XBUF(shadow_bp); 12989 12990 /* 12991 * Allocate the layer-private data for the shadow buf. 12992 * (No need to preserve xb_private in the shadow xbuf.) 12993 */ 12994 shadow_xp->xb_private = shadow_bsp = 12995 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12996 12997 /* 12998 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12999 * to figure out where the start of the user data is (based upon 13000 * the system block size) in the data returned by the READ 13001 * command (which will be based upon the target blocksize). Note 13002 * that this is only really used if the request is unaligned. 13003 */ 13004 if (un->un_f_enable_rmw) { 13005 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13006 ((offset_t)xp->xb_blkno * un->un_sys_blocksize)); 13007 ASSERT((bsp->mbs_copy_offset >= 0) && 13008 (bsp->mbs_copy_offset < un->un_phy_blocksize)); 13009 } else { 13010 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13011 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13012 ASSERT((bsp->mbs_copy_offset >= 0) && 13013 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13014 } 13015 13016 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13017 13018 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13019 13020 /* Transfer the wmap (if any) to the shadow buf */ 13021 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13022 bsp->mbs_wmp = NULL; 13023 13024 /* 13025 * The shadow buf goes on from here in place of the 13026 * original buf. 13027 */ 13028 shadow_bsp->mbs_orig_bp = bp; 13029 bp = shadow_bp; 13030 } 13031 13032 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13033 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13034 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13035 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13036 request_bytes); 13037 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13038 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13039 13040 done: 13041 SD_NEXT_IOSTART(index, un, bp); 13042 13043 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13044 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13045 } 13046 13047 13048 /* 13049 * Function: sd_mapblocksize_iodone 13050 * 13051 * Description: Completion side processing for block-size mapping. 13052 * 13053 * Context: May be called under interrupt context 13054 */ 13055 13056 static void 13057 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13058 { 13059 struct sd_mapblocksize_info *bsp; 13060 struct sd_xbuf *xp; 13061 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13062 struct buf *orig_bp; /* ptr to the original buf */ 13063 offset_t shadow_end; 13064 offset_t request_end; 13065 offset_t shadow_start; 13066 ssize_t copy_offset; 13067 size_t copy_length; 13068 size_t shortfall; 13069 uint_t is_write; /* TRUE if this bp is a WRITE */ 13070 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13071 13072 ASSERT(un != NULL); 13073 ASSERT(bp != NULL); 13074 13075 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13076 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13077 13078 /* 13079 * There is no shadow buf or layer-private data if the target is 13080 * using un->un_sys_blocksize as its block size or if bcount == 0. 13081 */ 13082 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 13083 (bp->b_bcount == 0)) { 13084 goto exit; 13085 } 13086 13087 xp = SD_GET_XBUF(bp); 13088 ASSERT(xp != NULL); 13089 13090 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13091 bsp = xp->xb_private; 13092 13093 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13094 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13095 13096 if (is_write) { 13097 /* 13098 * For a WRITE request we must free up the block range that 13099 * we have locked up. This holds regardless of whether this is 13100 * an aligned write request or a read-modify-write request. 13101 */ 13102 sd_range_unlock(un, bsp->mbs_wmp); 13103 bsp->mbs_wmp = NULL; 13104 } 13105 13106 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13107 /* 13108 * An aligned read or write command will have no shadow buf; 13109 * there is not much else to do with it. 13110 */ 13111 goto done; 13112 } 13113 13114 orig_bp = bsp->mbs_orig_bp; 13115 ASSERT(orig_bp != NULL); 13116 orig_xp = SD_GET_XBUF(orig_bp); 13117 ASSERT(orig_xp != NULL); 13118 ASSERT(!mutex_owned(SD_MUTEX(un))); 13119 13120 if (!is_write && has_wmap) { 13121 /* 13122 * A READ with a wmap means this is the READ phase of a 13123 * read-modify-write. If an error occurred on the READ then 13124 * we do not proceed with the WRITE phase or copy any data. 13125 * Just release the write maps and return with an error. 13126 */ 13127 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13128 orig_bp->b_resid = orig_bp->b_bcount; 13129 bioerror(orig_bp, bp->b_error); 13130 sd_range_unlock(un, bsp->mbs_wmp); 13131 goto freebuf_done; 13132 } 13133 } 13134 13135 /* 13136 * Here is where we set up to copy the data from the shadow buf 13137 * into the space associated with the original buf. 13138 * 13139 * To deal with the conversion between block sizes, these 13140 * computations treat the data as an array of bytes, with the 13141 * first byte (byte 0) corresponding to the first byte in the 13142 * first block on the disk. 13143 */ 13144 13145 /* 13146 * shadow_start and shadow_len indicate the location and size of 13147 * the data returned with the shadow IO request. 13148 */ 13149 if (un->un_f_enable_rmw) { 13150 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 13151 } else { 13152 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13153 } 13154 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13155 13156 /* 13157 * copy_offset gives the offset (in bytes) from the start of the first 13158 * block of the READ request to the beginning of the data. We retrieve 13159 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13160 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13161 * data to be copied (in bytes). 13162 */ 13163 copy_offset = bsp->mbs_copy_offset; 13164 if (un->un_f_enable_rmw) { 13165 ASSERT((copy_offset >= 0) && 13166 (copy_offset < un->un_phy_blocksize)); 13167 } else { 13168 ASSERT((copy_offset >= 0) && 13169 (copy_offset < un->un_tgt_blocksize)); 13170 } 13171 13172 copy_length = orig_bp->b_bcount; 13173 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13174 13175 /* 13176 * Set up the resid and error fields of orig_bp as appropriate. 13177 */ 13178 if (shadow_end >= request_end) { 13179 /* We got all the requested data; set resid to zero */ 13180 orig_bp->b_resid = 0; 13181 } else { 13182 /* 13183 * We failed to get enough data to fully satisfy the original 13184 * request. Just copy back whatever data we got and set 13185 * up the residual and error code as required. 13186 * 13187 * 'shortfall' is the amount by which the data received with the 13188 * shadow buf has "fallen short" of the requested amount. 13189 */ 13190 shortfall = (size_t)(request_end - shadow_end); 13191 13192 if (shortfall > orig_bp->b_bcount) { 13193 /* 13194 * We did not get enough data to even partially 13195 * fulfill the original request. The residual is 13196 * equal to the amount requested. 13197 */ 13198 orig_bp->b_resid = orig_bp->b_bcount; 13199 } else { 13200 /* 13201 * We did not get all the data that we requested 13202 * from the device, but we will try to return what 13203 * portion we did get. 13204 */ 13205 orig_bp->b_resid = shortfall; 13206 } 13207 ASSERT(copy_length >= orig_bp->b_resid); 13208 copy_length -= orig_bp->b_resid; 13209 } 13210 13211 /* Propagate the error code from the shadow buf to the original buf */ 13212 bioerror(orig_bp, bp->b_error); 13213 13214 if (is_write) { 13215 goto freebuf_done; /* No data copying for a WRITE */ 13216 } 13217 13218 if (has_wmap) { 13219 /* 13220 * This is a READ command from the READ phase of a 13221 * read-modify-write request. We have to copy the data given 13222 * by the user OVER the data returned by the READ command, 13223 * then convert the command from a READ to a WRITE and send 13224 * it back to the target. 13225 */ 13226 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13227 copy_length); 13228 13229 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13230 13231 /* 13232 * Dispatch the WRITE command to the taskq thread, which 13233 * will in turn send the command to the target. When the 13234 * WRITE command completes, we (sd_mapblocksize_iodone()) 13235 * will get called again as part of the iodone chain 13236 * processing for it. Note that we will still be dealing 13237 * with the shadow buf at that point. 13238 */ 13239 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13240 KM_NOSLEEP) != 0) { 13241 /* 13242 * Dispatch was successful so we are done. Return 13243 * without going any higher up the iodone chain. Do 13244 * not free up any layer-private data until after the 13245 * WRITE completes. 13246 */ 13247 return; 13248 } 13249 13250 /* 13251 * Dispatch of the WRITE command failed; set up the error 13252 * condition and send this IO back up the iodone chain. 13253 */ 13254 bioerror(orig_bp, EIO); 13255 orig_bp->b_resid = orig_bp->b_bcount; 13256 13257 } else { 13258 /* 13259 * This is a regular READ request (ie, not a RMW). Copy the 13260 * data from the shadow buf into the original buf. The 13261 * copy_offset compensates for any "misalignment" between the 13262 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13263 * original buf (with its un->un_sys_blocksize blocks). 13264 */ 13265 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13266 copy_length); 13267 } 13268 13269 freebuf_done: 13270 13271 /* 13272 * At this point we still have both the shadow buf AND the original 13273 * buf to deal with, as well as the layer-private data area in each. 13274 * Local variables are as follows: 13275 * 13276 * bp -- points to shadow buf 13277 * xp -- points to xbuf of shadow buf 13278 * bsp -- points to layer-private data area of shadow buf 13279 * orig_bp -- points to original buf 13280 * 13281 * First free the shadow buf and its associated xbuf, then free the 13282 * layer-private data area from the shadow buf. There is no need to 13283 * restore xb_private in the shadow xbuf. 13284 */ 13285 sd_shadow_buf_free(bp); 13286 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13287 13288 /* 13289 * Now update the local variables to point to the original buf, xbuf, 13290 * and layer-private area. 13291 */ 13292 bp = orig_bp; 13293 xp = SD_GET_XBUF(bp); 13294 ASSERT(xp != NULL); 13295 ASSERT(xp == orig_xp); 13296 bsp = xp->xb_private; 13297 ASSERT(bsp != NULL); 13298 13299 done: 13300 /* 13301 * Restore xb_private to whatever it was set to by the next higher 13302 * layer in the chain, then free the layer-private data area. 13303 */ 13304 xp->xb_private = bsp->mbs_oprivate; 13305 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13306 13307 exit: 13308 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13309 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13310 13311 SD_NEXT_IODONE(index, un, bp); 13312 } 13313 13314 13315 /* 13316 * Function: sd_checksum_iostart 13317 * 13318 * Description: A stub function for a layer that's currently not used. 13319 * For now just a placeholder. 13320 * 13321 * Context: Kernel thread context 13322 */ 13323 13324 static void 13325 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13326 { 13327 ASSERT(un != NULL); 13328 ASSERT(bp != NULL); 13329 ASSERT(!mutex_owned(SD_MUTEX(un))); 13330 SD_NEXT_IOSTART(index, un, bp); 13331 } 13332 13333 13334 /* 13335 * Function: sd_checksum_iodone 13336 * 13337 * Description: A stub function for a layer that's currently not used. 13338 * For now just a placeholder. 13339 * 13340 * Context: May be called under interrupt context 13341 */ 13342 13343 static void 13344 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13345 { 13346 ASSERT(un != NULL); 13347 ASSERT(bp != NULL); 13348 ASSERT(!mutex_owned(SD_MUTEX(un))); 13349 SD_NEXT_IODONE(index, un, bp); 13350 } 13351 13352 13353 /* 13354 * Function: sd_checksum_uscsi_iostart 13355 * 13356 * Description: A stub function for a layer that's currently not used. 13357 * For now just a placeholder. 13358 * 13359 * Context: Kernel thread context 13360 */ 13361 13362 static void 13363 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13364 { 13365 ASSERT(un != NULL); 13366 ASSERT(bp != NULL); 13367 ASSERT(!mutex_owned(SD_MUTEX(un))); 13368 SD_NEXT_IOSTART(index, un, bp); 13369 } 13370 13371 13372 /* 13373 * Function: sd_checksum_uscsi_iodone 13374 * 13375 * Description: A stub function for a layer that's currently not used. 13376 * For now just a placeholder. 13377 * 13378 * Context: May be called under interrupt context 13379 */ 13380 13381 static void 13382 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13383 { 13384 ASSERT(un != NULL); 13385 ASSERT(bp != NULL); 13386 ASSERT(!mutex_owned(SD_MUTEX(un))); 13387 SD_NEXT_IODONE(index, un, bp); 13388 } 13389 13390 13391 /* 13392 * Function: sd_pm_iostart 13393 * 13394 * Description: iostart-side routine for Power mangement. 13395 * 13396 * Context: Kernel thread context 13397 */ 13398 13399 static void 13400 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13401 { 13402 ASSERT(un != NULL); 13403 ASSERT(bp != NULL); 13404 ASSERT(!mutex_owned(SD_MUTEX(un))); 13405 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13406 13407 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13408 13409 if (sd_pm_entry(un) != DDI_SUCCESS) { 13410 /* 13411 * Set up to return the failed buf back up the 'iodone' 13412 * side of the calling chain. 13413 */ 13414 bioerror(bp, EIO); 13415 bp->b_resid = bp->b_bcount; 13416 13417 SD_BEGIN_IODONE(index, un, bp); 13418 13419 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13420 return; 13421 } 13422 13423 SD_NEXT_IOSTART(index, un, bp); 13424 13425 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13426 } 13427 13428 13429 /* 13430 * Function: sd_pm_iodone 13431 * 13432 * Description: iodone-side routine for power mangement. 13433 * 13434 * Context: may be called from interrupt context 13435 */ 13436 13437 static void 13438 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13439 { 13440 ASSERT(un != NULL); 13441 ASSERT(bp != NULL); 13442 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13443 13444 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13445 13446 /* 13447 * After attach the following flag is only read, so don't 13448 * take the penalty of acquiring a mutex for it. 13449 */ 13450 if (un->un_f_pm_is_enabled == TRUE) { 13451 sd_pm_exit(un); 13452 } 13453 13454 SD_NEXT_IODONE(index, un, bp); 13455 13456 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13457 } 13458 13459 13460 /* 13461 * Function: sd_core_iostart 13462 * 13463 * Description: Primary driver function for enqueuing buf(9S) structs from 13464 * the system and initiating IO to the target device 13465 * 13466 * Context: Kernel thread context. Can sleep. 13467 * 13468 * Assumptions: - The given xp->xb_blkno is absolute 13469 * (ie, relative to the start of the device). 13470 * - The IO is to be done using the native blocksize of 13471 * the device, as specified in un->un_tgt_blocksize. 13472 */ 13473 /* ARGSUSED */ 13474 static void 13475 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13476 { 13477 struct sd_xbuf *xp; 13478 13479 ASSERT(un != NULL); 13480 ASSERT(bp != NULL); 13481 ASSERT(!mutex_owned(SD_MUTEX(un))); 13482 ASSERT(bp->b_resid == 0); 13483 13484 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13485 13486 xp = SD_GET_XBUF(bp); 13487 ASSERT(xp != NULL); 13488 13489 mutex_enter(SD_MUTEX(un)); 13490 13491 /* 13492 * If we are currently in the failfast state, fail any new IO 13493 * that has B_FAILFAST set, then return. 13494 */ 13495 if ((bp->b_flags & B_FAILFAST) && 13496 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13497 mutex_exit(SD_MUTEX(un)); 13498 bioerror(bp, EIO); 13499 bp->b_resid = bp->b_bcount; 13500 SD_BEGIN_IODONE(index, un, bp); 13501 return; 13502 } 13503 13504 if (SD_IS_DIRECT_PRIORITY(xp)) { 13505 /* 13506 * Priority command -- transport it immediately. 13507 * 13508 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13509 * because all direct priority commands should be associated 13510 * with error recovery actions which we don't want to retry. 13511 */ 13512 sd_start_cmds(un, bp); 13513 } else { 13514 /* 13515 * Normal command -- add it to the wait queue, then start 13516 * transporting commands from the wait queue. 13517 */ 13518 sd_add_buf_to_waitq(un, bp); 13519 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13520 sd_start_cmds(un, NULL); 13521 } 13522 13523 mutex_exit(SD_MUTEX(un)); 13524 13525 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13526 } 13527 13528 13529 /* 13530 * Function: sd_init_cdb_limits 13531 * 13532 * Description: This is to handle scsi_pkt initialization differences 13533 * between the driver platforms. 13534 * 13535 * Legacy behaviors: 13536 * 13537 * If the block number or the sector count exceeds the 13538 * capabilities of a Group 0 command, shift over to a 13539 * Group 1 command. We don't blindly use Group 1 13540 * commands because a) some drives (CDC Wren IVs) get a 13541 * bit confused, and b) there is probably a fair amount 13542 * of speed difference for a target to receive and decode 13543 * a 10 byte command instead of a 6 byte command. 13544 * 13545 * The xfer time difference of 6 vs 10 byte CDBs is 13546 * still significant so this code is still worthwhile. 13547 * 10 byte CDBs are very inefficient with the fas HBA driver 13548 * and older disks. Each CDB byte took 1 usec with some 13549 * popular disks. 13550 * 13551 * Context: Must be called at attach time 13552 */ 13553 13554 static void 13555 sd_init_cdb_limits(struct sd_lun *un) 13556 { 13557 int hba_cdb_limit; 13558 13559 /* 13560 * Use CDB_GROUP1 commands for most devices except for 13561 * parallel SCSI fixed drives in which case we get better 13562 * performance using CDB_GROUP0 commands (where applicable). 13563 */ 13564 un->un_mincdb = SD_CDB_GROUP1; 13565 #if !defined(__fibre) 13566 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13567 !un->un_f_has_removable_media) { 13568 un->un_mincdb = SD_CDB_GROUP0; 13569 } 13570 #endif 13571 13572 /* 13573 * Try to read the max-cdb-length supported by HBA. 13574 */ 13575 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13576 if (0 >= un->un_max_hba_cdb) { 13577 un->un_max_hba_cdb = CDB_GROUP4; 13578 hba_cdb_limit = SD_CDB_GROUP4; 13579 } else if (0 < un->un_max_hba_cdb && 13580 un->un_max_hba_cdb < CDB_GROUP1) { 13581 hba_cdb_limit = SD_CDB_GROUP0; 13582 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13583 un->un_max_hba_cdb < CDB_GROUP5) { 13584 hba_cdb_limit = SD_CDB_GROUP1; 13585 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13586 un->un_max_hba_cdb < CDB_GROUP4) { 13587 hba_cdb_limit = SD_CDB_GROUP5; 13588 } else { 13589 hba_cdb_limit = SD_CDB_GROUP4; 13590 } 13591 13592 /* 13593 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13594 * commands for fixed disks unless we are building for a 32 bit 13595 * kernel. 13596 */ 13597 #ifdef _LP64 13598 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13599 min(hba_cdb_limit, SD_CDB_GROUP4); 13600 #else 13601 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13602 min(hba_cdb_limit, SD_CDB_GROUP1); 13603 #endif 13604 13605 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13606 ? sizeof (struct scsi_arq_status) : 1); 13607 un->un_cmd_timeout = (ushort_t)sd_io_time; 13608 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13609 } 13610 13611 13612 /* 13613 * Function: sd_initpkt_for_buf 13614 * 13615 * Description: Allocate and initialize for transport a scsi_pkt struct, 13616 * based upon the info specified in the given buf struct. 13617 * 13618 * Assumes the xb_blkno in the request is absolute (ie, 13619 * relative to the start of the device (NOT partition!). 13620 * Also assumes that the request is using the native block 13621 * size of the device (as returned by the READ CAPACITY 13622 * command). 13623 * 13624 * Return Code: SD_PKT_ALLOC_SUCCESS 13625 * SD_PKT_ALLOC_FAILURE 13626 * SD_PKT_ALLOC_FAILURE_NO_DMA 13627 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13628 * 13629 * Context: Kernel thread and may be called from software interrupt context 13630 * as part of a sdrunout callback. This function may not block or 13631 * call routines that block 13632 */ 13633 13634 static int 13635 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13636 { 13637 struct sd_xbuf *xp; 13638 struct scsi_pkt *pktp = NULL; 13639 struct sd_lun *un; 13640 size_t blockcount; 13641 daddr_t startblock; 13642 int rval; 13643 int cmd_flags; 13644 13645 ASSERT(bp != NULL); 13646 ASSERT(pktpp != NULL); 13647 xp = SD_GET_XBUF(bp); 13648 ASSERT(xp != NULL); 13649 un = SD_GET_UN(bp); 13650 ASSERT(un != NULL); 13651 ASSERT(mutex_owned(SD_MUTEX(un))); 13652 ASSERT(bp->b_resid == 0); 13653 13654 SD_TRACE(SD_LOG_IO_CORE, un, 13655 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13656 13657 mutex_exit(SD_MUTEX(un)); 13658 13659 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13660 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13661 /* 13662 * Already have a scsi_pkt -- just need DMA resources. 13663 * We must recompute the CDB in case the mapping returns 13664 * a nonzero pkt_resid. 13665 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13666 * that is being retried, the unmap/remap of the DMA resouces 13667 * will result in the entire transfer starting over again 13668 * from the very first block. 13669 */ 13670 ASSERT(xp->xb_pktp != NULL); 13671 pktp = xp->xb_pktp; 13672 } else { 13673 pktp = NULL; 13674 } 13675 #endif /* __i386 || __amd64 */ 13676 13677 startblock = xp->xb_blkno; /* Absolute block num. */ 13678 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13679 13680 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13681 13682 /* 13683 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13684 * call scsi_init_pkt, and build the CDB. 13685 */ 13686 rval = sd_setup_rw_pkt(un, &pktp, bp, 13687 cmd_flags, sdrunout, (caddr_t)un, 13688 startblock, blockcount); 13689 13690 if (rval == 0) { 13691 /* 13692 * Success. 13693 * 13694 * If partial DMA is being used and required for this transfer. 13695 * set it up here. 13696 */ 13697 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13698 (pktp->pkt_resid != 0)) { 13699 13700 /* 13701 * Save the CDB length and pkt_resid for the 13702 * next xfer 13703 */ 13704 xp->xb_dma_resid = pktp->pkt_resid; 13705 13706 /* rezero resid */ 13707 pktp->pkt_resid = 0; 13708 13709 } else { 13710 xp->xb_dma_resid = 0; 13711 } 13712 13713 pktp->pkt_flags = un->un_tagflags; 13714 pktp->pkt_time = un->un_cmd_timeout; 13715 pktp->pkt_comp = sdintr; 13716 13717 pktp->pkt_private = bp; 13718 *pktpp = pktp; 13719 13720 SD_TRACE(SD_LOG_IO_CORE, un, 13721 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13722 13723 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13724 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13725 #endif 13726 13727 mutex_enter(SD_MUTEX(un)); 13728 return (SD_PKT_ALLOC_SUCCESS); 13729 13730 } 13731 13732 /* 13733 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13734 * from sd_setup_rw_pkt. 13735 */ 13736 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13737 13738 if (rval == SD_PKT_ALLOC_FAILURE) { 13739 *pktpp = NULL; 13740 /* 13741 * Set the driver state to RWAIT to indicate the driver 13742 * is waiting on resource allocations. The driver will not 13743 * suspend, pm_suspend, or detatch while the state is RWAIT. 13744 */ 13745 mutex_enter(SD_MUTEX(un)); 13746 New_state(un, SD_STATE_RWAIT); 13747 13748 SD_ERROR(SD_LOG_IO_CORE, un, 13749 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13750 13751 if ((bp->b_flags & B_ERROR) != 0) { 13752 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13753 } 13754 return (SD_PKT_ALLOC_FAILURE); 13755 } else { 13756 /* 13757 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13758 * 13759 * This should never happen. Maybe someone messed with the 13760 * kernel's minphys? 13761 */ 13762 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13763 "Request rejected: too large for CDB: " 13764 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13765 SD_ERROR(SD_LOG_IO_CORE, un, 13766 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13767 mutex_enter(SD_MUTEX(un)); 13768 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13769 13770 } 13771 } 13772 13773 13774 /* 13775 * Function: sd_destroypkt_for_buf 13776 * 13777 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13778 * 13779 * Context: Kernel thread or interrupt context 13780 */ 13781 13782 static void 13783 sd_destroypkt_for_buf(struct buf *bp) 13784 { 13785 ASSERT(bp != NULL); 13786 ASSERT(SD_GET_UN(bp) != NULL); 13787 13788 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13789 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13790 13791 ASSERT(SD_GET_PKTP(bp) != NULL); 13792 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13793 13794 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13795 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13796 } 13797 13798 /* 13799 * Function: sd_setup_rw_pkt 13800 * 13801 * Description: Determines appropriate CDB group for the requested LBA 13802 * and transfer length, calls scsi_init_pkt, and builds 13803 * the CDB. Do not use for partial DMA transfers except 13804 * for the initial transfer since the CDB size must 13805 * remain constant. 13806 * 13807 * Context: Kernel thread and may be called from software interrupt 13808 * context as part of a sdrunout callback. This function may not 13809 * block or call routines that block 13810 */ 13811 13812 13813 int 13814 sd_setup_rw_pkt(struct sd_lun *un, 13815 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13816 int (*callback)(caddr_t), caddr_t callback_arg, 13817 diskaddr_t lba, uint32_t blockcount) 13818 { 13819 struct scsi_pkt *return_pktp; 13820 union scsi_cdb *cdbp; 13821 struct sd_cdbinfo *cp = NULL; 13822 int i; 13823 13824 /* 13825 * See which size CDB to use, based upon the request. 13826 */ 13827 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13828 13829 /* 13830 * Check lba and block count against sd_cdbtab limits. 13831 * In the partial DMA case, we have to use the same size 13832 * CDB for all the transfers. Check lba + blockcount 13833 * against the max LBA so we know that segment of the 13834 * transfer can use the CDB we select. 13835 */ 13836 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13837 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13838 13839 /* 13840 * The command will fit into the CDB type 13841 * specified by sd_cdbtab[i]. 13842 */ 13843 cp = sd_cdbtab + i; 13844 13845 /* 13846 * Call scsi_init_pkt so we can fill in the 13847 * CDB. 13848 */ 13849 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13850 bp, cp->sc_grpcode, un->un_status_len, 0, 13851 flags, callback, callback_arg); 13852 13853 if (return_pktp != NULL) { 13854 13855 /* 13856 * Return new value of pkt 13857 */ 13858 *pktpp = return_pktp; 13859 13860 /* 13861 * To be safe, zero the CDB insuring there is 13862 * no leftover data from a previous command. 13863 */ 13864 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13865 13866 /* 13867 * Handle partial DMA mapping 13868 */ 13869 if (return_pktp->pkt_resid != 0) { 13870 13871 /* 13872 * Not going to xfer as many blocks as 13873 * originally expected 13874 */ 13875 blockcount -= 13876 SD_BYTES2TGTBLOCKS(un, 13877 return_pktp->pkt_resid); 13878 } 13879 13880 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13881 13882 /* 13883 * Set command byte based on the CDB 13884 * type we matched. 13885 */ 13886 cdbp->scc_cmd = cp->sc_grpmask | 13887 ((bp->b_flags & B_READ) ? 13888 SCMD_READ : SCMD_WRITE); 13889 13890 SD_FILL_SCSI1_LUN(un, return_pktp); 13891 13892 /* 13893 * Fill in LBA and length 13894 */ 13895 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13896 (cp->sc_grpcode == CDB_GROUP4) || 13897 (cp->sc_grpcode == CDB_GROUP0) || 13898 (cp->sc_grpcode == CDB_GROUP5)); 13899 13900 if (cp->sc_grpcode == CDB_GROUP1) { 13901 FORMG1ADDR(cdbp, lba); 13902 FORMG1COUNT(cdbp, blockcount); 13903 return (0); 13904 } else if (cp->sc_grpcode == CDB_GROUP4) { 13905 FORMG4LONGADDR(cdbp, lba); 13906 FORMG4COUNT(cdbp, blockcount); 13907 return (0); 13908 } else if (cp->sc_grpcode == CDB_GROUP0) { 13909 FORMG0ADDR(cdbp, lba); 13910 FORMG0COUNT(cdbp, blockcount); 13911 return (0); 13912 } else if (cp->sc_grpcode == CDB_GROUP5) { 13913 FORMG5ADDR(cdbp, lba); 13914 FORMG5COUNT(cdbp, blockcount); 13915 return (0); 13916 } 13917 13918 /* 13919 * It should be impossible to not match one 13920 * of the CDB types above, so we should never 13921 * reach this point. Set the CDB command byte 13922 * to test-unit-ready to avoid writing 13923 * to somewhere we don't intend. 13924 */ 13925 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13926 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13927 } else { 13928 /* 13929 * Couldn't get scsi_pkt 13930 */ 13931 return (SD_PKT_ALLOC_FAILURE); 13932 } 13933 } 13934 } 13935 13936 /* 13937 * None of the available CDB types were suitable. This really 13938 * should never happen: on a 64 bit system we support 13939 * READ16/WRITE16 which will hold an entire 64 bit disk address 13940 * and on a 32 bit system we will refuse to bind to a device 13941 * larger than 2TB so addresses will never be larger than 32 bits. 13942 */ 13943 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13944 } 13945 13946 /* 13947 * Function: sd_setup_next_rw_pkt 13948 * 13949 * Description: Setup packet for partial DMA transfers, except for the 13950 * initial transfer. sd_setup_rw_pkt should be used for 13951 * the initial transfer. 13952 * 13953 * Context: Kernel thread and may be called from interrupt context. 13954 */ 13955 13956 int 13957 sd_setup_next_rw_pkt(struct sd_lun *un, 13958 struct scsi_pkt *pktp, struct buf *bp, 13959 diskaddr_t lba, uint32_t blockcount) 13960 { 13961 uchar_t com; 13962 union scsi_cdb *cdbp; 13963 uchar_t cdb_group_id; 13964 13965 ASSERT(pktp != NULL); 13966 ASSERT(pktp->pkt_cdbp != NULL); 13967 13968 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13969 com = cdbp->scc_cmd; 13970 cdb_group_id = CDB_GROUPID(com); 13971 13972 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13973 (cdb_group_id == CDB_GROUPID_1) || 13974 (cdb_group_id == CDB_GROUPID_4) || 13975 (cdb_group_id == CDB_GROUPID_5)); 13976 13977 /* 13978 * Move pkt to the next portion of the xfer. 13979 * func is NULL_FUNC so we do not have to release 13980 * the disk mutex here. 13981 */ 13982 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13983 NULL_FUNC, NULL) == pktp) { 13984 /* Success. Handle partial DMA */ 13985 if (pktp->pkt_resid != 0) { 13986 blockcount -= 13987 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13988 } 13989 13990 cdbp->scc_cmd = com; 13991 SD_FILL_SCSI1_LUN(un, pktp); 13992 if (cdb_group_id == CDB_GROUPID_1) { 13993 FORMG1ADDR(cdbp, lba); 13994 FORMG1COUNT(cdbp, blockcount); 13995 return (0); 13996 } else if (cdb_group_id == CDB_GROUPID_4) { 13997 FORMG4LONGADDR(cdbp, lba); 13998 FORMG4COUNT(cdbp, blockcount); 13999 return (0); 14000 } else if (cdb_group_id == CDB_GROUPID_0) { 14001 FORMG0ADDR(cdbp, lba); 14002 FORMG0COUNT(cdbp, blockcount); 14003 return (0); 14004 } else if (cdb_group_id == CDB_GROUPID_5) { 14005 FORMG5ADDR(cdbp, lba); 14006 FORMG5COUNT(cdbp, blockcount); 14007 return (0); 14008 } 14009 14010 /* Unreachable */ 14011 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14012 } 14013 14014 /* 14015 * Error setting up next portion of cmd transfer. 14016 * Something is definitely very wrong and this 14017 * should not happen. 14018 */ 14019 return (SD_PKT_ALLOC_FAILURE); 14020 } 14021 14022 /* 14023 * Function: sd_initpkt_for_uscsi 14024 * 14025 * Description: Allocate and initialize for transport a scsi_pkt struct, 14026 * based upon the info specified in the given uscsi_cmd struct. 14027 * 14028 * Return Code: SD_PKT_ALLOC_SUCCESS 14029 * SD_PKT_ALLOC_FAILURE 14030 * SD_PKT_ALLOC_FAILURE_NO_DMA 14031 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14032 * 14033 * Context: Kernel thread and may be called from software interrupt context 14034 * as part of a sdrunout callback. This function may not block or 14035 * call routines that block 14036 */ 14037 14038 static int 14039 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14040 { 14041 struct uscsi_cmd *uscmd; 14042 struct sd_xbuf *xp; 14043 struct scsi_pkt *pktp; 14044 struct sd_lun *un; 14045 uint32_t flags = 0; 14046 14047 ASSERT(bp != NULL); 14048 ASSERT(pktpp != NULL); 14049 xp = SD_GET_XBUF(bp); 14050 ASSERT(xp != NULL); 14051 un = SD_GET_UN(bp); 14052 ASSERT(un != NULL); 14053 ASSERT(mutex_owned(SD_MUTEX(un))); 14054 14055 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14056 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14057 ASSERT(uscmd != NULL); 14058 14059 SD_TRACE(SD_LOG_IO_CORE, un, 14060 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14061 14062 /* 14063 * Allocate the scsi_pkt for the command. 14064 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14065 * during scsi_init_pkt time and will continue to use the 14066 * same path as long as the same scsi_pkt is used without 14067 * intervening scsi_dma_free(). Since uscsi command does 14068 * not call scsi_dmafree() before retry failed command, it 14069 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14070 * set such that scsi_vhci can use other available path for 14071 * retry. Besides, ucsci command does not allow DMA breakup, 14072 * so there is no need to set PKT_DMA_PARTIAL flag. 14073 */ 14074 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14075 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14076 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14077 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 14078 - sizeof (struct scsi_extended_sense)), 0, 14079 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 14080 sdrunout, (caddr_t)un); 14081 } else { 14082 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14083 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14084 sizeof (struct scsi_arq_status), 0, 14085 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14086 sdrunout, (caddr_t)un); 14087 } 14088 14089 if (pktp == NULL) { 14090 *pktpp = NULL; 14091 /* 14092 * Set the driver state to RWAIT to indicate the driver 14093 * is waiting on resource allocations. The driver will not 14094 * suspend, pm_suspend, or detatch while the state is RWAIT. 14095 */ 14096 New_state(un, SD_STATE_RWAIT); 14097 14098 SD_ERROR(SD_LOG_IO_CORE, un, 14099 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14100 14101 if ((bp->b_flags & B_ERROR) != 0) { 14102 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14103 } 14104 return (SD_PKT_ALLOC_FAILURE); 14105 } 14106 14107 /* 14108 * We do not do DMA breakup for USCSI commands, so return failure 14109 * here if all the needed DMA resources were not allocated. 14110 */ 14111 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14112 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14113 scsi_destroy_pkt(pktp); 14114 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14115 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14116 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14117 } 14118 14119 /* Init the cdb from the given uscsi struct */ 14120 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14121 uscmd->uscsi_cdb[0], 0, 0, 0); 14122 14123 SD_FILL_SCSI1_LUN(un, pktp); 14124 14125 /* 14126 * Set up the optional USCSI flags. See the uscsi (7I) man page 14127 * for listing of the supported flags. 14128 */ 14129 14130 if (uscmd->uscsi_flags & USCSI_SILENT) { 14131 flags |= FLAG_SILENT; 14132 } 14133 14134 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14135 flags |= FLAG_DIAGNOSE; 14136 } 14137 14138 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14139 flags |= FLAG_ISOLATE; 14140 } 14141 14142 if (un->un_f_is_fibre == FALSE) { 14143 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14144 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14145 } 14146 } 14147 14148 /* 14149 * Set the pkt flags here so we save time later. 14150 * Note: These flags are NOT in the uscsi man page!!! 14151 */ 14152 if (uscmd->uscsi_flags & USCSI_HEAD) { 14153 flags |= FLAG_HEAD; 14154 } 14155 14156 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14157 flags |= FLAG_NOINTR; 14158 } 14159 14160 /* 14161 * For tagged queueing, things get a bit complicated. 14162 * Check first for head of queue and last for ordered queue. 14163 * If neither head nor order, use the default driver tag flags. 14164 */ 14165 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14166 if (uscmd->uscsi_flags & USCSI_HTAG) { 14167 flags |= FLAG_HTAG; 14168 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14169 flags |= FLAG_OTAG; 14170 } else { 14171 flags |= un->un_tagflags & FLAG_TAGMASK; 14172 } 14173 } 14174 14175 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14176 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14177 } 14178 14179 pktp->pkt_flags = flags; 14180 14181 /* Transfer uscsi information to scsi_pkt */ 14182 (void) scsi_uscsi_pktinit(uscmd, pktp); 14183 14184 /* Copy the caller's CDB into the pkt... */ 14185 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14186 14187 if (uscmd->uscsi_timeout == 0) { 14188 pktp->pkt_time = un->un_uscsi_timeout; 14189 } else { 14190 pktp->pkt_time = uscmd->uscsi_timeout; 14191 } 14192 14193 /* need it later to identify USCSI request in sdintr */ 14194 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14195 14196 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14197 14198 pktp->pkt_private = bp; 14199 pktp->pkt_comp = sdintr; 14200 *pktpp = pktp; 14201 14202 SD_TRACE(SD_LOG_IO_CORE, un, 14203 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14204 14205 return (SD_PKT_ALLOC_SUCCESS); 14206 } 14207 14208 14209 /* 14210 * Function: sd_destroypkt_for_uscsi 14211 * 14212 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14213 * IOs.. Also saves relevant info into the associated uscsi_cmd 14214 * struct. 14215 * 14216 * Context: May be called under interrupt context 14217 */ 14218 14219 static void 14220 sd_destroypkt_for_uscsi(struct buf *bp) 14221 { 14222 struct uscsi_cmd *uscmd; 14223 struct sd_xbuf *xp; 14224 struct scsi_pkt *pktp; 14225 struct sd_lun *un; 14226 struct sd_uscsi_info *suip; 14227 14228 ASSERT(bp != NULL); 14229 xp = SD_GET_XBUF(bp); 14230 ASSERT(xp != NULL); 14231 un = SD_GET_UN(bp); 14232 ASSERT(un != NULL); 14233 ASSERT(!mutex_owned(SD_MUTEX(un))); 14234 pktp = SD_GET_PKTP(bp); 14235 ASSERT(pktp != NULL); 14236 14237 SD_TRACE(SD_LOG_IO_CORE, un, 14238 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14239 14240 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14241 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14242 ASSERT(uscmd != NULL); 14243 14244 /* Save the status and the residual into the uscsi_cmd struct */ 14245 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14246 uscmd->uscsi_resid = bp->b_resid; 14247 14248 /* Transfer scsi_pkt information to uscsi */ 14249 (void) scsi_uscsi_pktfini(pktp, uscmd); 14250 14251 /* 14252 * If enabled, copy any saved sense data into the area specified 14253 * by the uscsi command. 14254 */ 14255 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14256 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14257 /* 14258 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14259 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14260 */ 14261 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14262 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14263 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14264 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14265 MAX_SENSE_LENGTH); 14266 } else { 14267 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14268 SENSE_LENGTH); 14269 } 14270 } 14271 /* 14272 * The following assignments are for SCSI FMA. 14273 */ 14274 ASSERT(xp->xb_private != NULL); 14275 suip = (struct sd_uscsi_info *)xp->xb_private; 14276 suip->ui_pkt_reason = pktp->pkt_reason; 14277 suip->ui_pkt_state = pktp->pkt_state; 14278 suip->ui_pkt_statistics = pktp->pkt_statistics; 14279 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 14280 14281 /* We are done with the scsi_pkt; free it now */ 14282 ASSERT(SD_GET_PKTP(bp) != NULL); 14283 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14284 14285 SD_TRACE(SD_LOG_IO_CORE, un, 14286 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14287 } 14288 14289 14290 /* 14291 * Function: sd_bioclone_alloc 14292 * 14293 * Description: Allocate a buf(9S) and init it as per the given buf 14294 * and the various arguments. The associated sd_xbuf 14295 * struct is (nearly) duplicated. The struct buf *bp 14296 * argument is saved in new_xp->xb_private. 14297 * 14298 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14299 * datalen - size of data area for the shadow bp 14300 * blkno - starting LBA 14301 * func - function pointer for b_iodone in the shadow buf. (May 14302 * be NULL if none.) 14303 * 14304 * Return Code: Pointer to allocates buf(9S) struct 14305 * 14306 * Context: Can sleep. 14307 */ 14308 14309 static struct buf * 14310 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14311 daddr_t blkno, int (*func)(struct buf *)) 14312 { 14313 struct sd_lun *un; 14314 struct sd_xbuf *xp; 14315 struct sd_xbuf *new_xp; 14316 struct buf *new_bp; 14317 14318 ASSERT(bp != NULL); 14319 xp = SD_GET_XBUF(bp); 14320 ASSERT(xp != NULL); 14321 un = SD_GET_UN(bp); 14322 ASSERT(un != NULL); 14323 ASSERT(!mutex_owned(SD_MUTEX(un))); 14324 14325 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14326 NULL, KM_SLEEP); 14327 14328 new_bp->b_lblkno = blkno; 14329 14330 /* 14331 * Allocate an xbuf for the shadow bp and copy the contents of the 14332 * original xbuf into it. 14333 */ 14334 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14335 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14336 14337 /* 14338 * The given bp is automatically saved in the xb_private member 14339 * of the new xbuf. Callers are allowed to depend on this. 14340 */ 14341 new_xp->xb_private = bp; 14342 14343 new_bp->b_private = new_xp; 14344 14345 return (new_bp); 14346 } 14347 14348 /* 14349 * Function: sd_shadow_buf_alloc 14350 * 14351 * Description: Allocate a buf(9S) and init it as per the given buf 14352 * and the various arguments. The associated sd_xbuf 14353 * struct is (nearly) duplicated. The struct buf *bp 14354 * argument is saved in new_xp->xb_private. 14355 * 14356 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14357 * datalen - size of data area for the shadow bp 14358 * bflags - B_READ or B_WRITE (pseudo flag) 14359 * blkno - starting LBA 14360 * func - function pointer for b_iodone in the shadow buf. (May 14361 * be NULL if none.) 14362 * 14363 * Return Code: Pointer to allocates buf(9S) struct 14364 * 14365 * Context: Can sleep. 14366 */ 14367 14368 static struct buf * 14369 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14370 daddr_t blkno, int (*func)(struct buf *)) 14371 { 14372 struct sd_lun *un; 14373 struct sd_xbuf *xp; 14374 struct sd_xbuf *new_xp; 14375 struct buf *new_bp; 14376 14377 ASSERT(bp != NULL); 14378 xp = SD_GET_XBUF(bp); 14379 ASSERT(xp != NULL); 14380 un = SD_GET_UN(bp); 14381 ASSERT(un != NULL); 14382 ASSERT(!mutex_owned(SD_MUTEX(un))); 14383 14384 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14385 bp_mapin(bp); 14386 } 14387 14388 bflags &= (B_READ | B_WRITE); 14389 #if defined(__i386) || defined(__amd64) 14390 new_bp = getrbuf(KM_SLEEP); 14391 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14392 new_bp->b_bcount = datalen; 14393 new_bp->b_flags = bflags | 14394 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14395 #else 14396 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14397 datalen, bflags, SLEEP_FUNC, NULL); 14398 #endif 14399 new_bp->av_forw = NULL; 14400 new_bp->av_back = NULL; 14401 new_bp->b_dev = bp->b_dev; 14402 new_bp->b_blkno = blkno; 14403 new_bp->b_iodone = func; 14404 new_bp->b_edev = bp->b_edev; 14405 new_bp->b_resid = 0; 14406 14407 /* We need to preserve the B_FAILFAST flag */ 14408 if (bp->b_flags & B_FAILFAST) { 14409 new_bp->b_flags |= B_FAILFAST; 14410 } 14411 14412 /* 14413 * Allocate an xbuf for the shadow bp and copy the contents of the 14414 * original xbuf into it. 14415 */ 14416 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14417 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14418 14419 /* Need later to copy data between the shadow buf & original buf! */ 14420 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14421 14422 /* 14423 * The given bp is automatically saved in the xb_private member 14424 * of the new xbuf. Callers are allowed to depend on this. 14425 */ 14426 new_xp->xb_private = bp; 14427 14428 new_bp->b_private = new_xp; 14429 14430 return (new_bp); 14431 } 14432 14433 /* 14434 * Function: sd_bioclone_free 14435 * 14436 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14437 * in the larger than partition operation. 14438 * 14439 * Context: May be called under interrupt context 14440 */ 14441 14442 static void 14443 sd_bioclone_free(struct buf *bp) 14444 { 14445 struct sd_xbuf *xp; 14446 14447 ASSERT(bp != NULL); 14448 xp = SD_GET_XBUF(bp); 14449 ASSERT(xp != NULL); 14450 14451 /* 14452 * Call bp_mapout() before freeing the buf, in case a lower 14453 * layer or HBA had done a bp_mapin(). we must do this here 14454 * as we are the "originator" of the shadow buf. 14455 */ 14456 bp_mapout(bp); 14457 14458 /* 14459 * Null out b_iodone before freeing the bp, to ensure that the driver 14460 * never gets confused by a stale value in this field. (Just a little 14461 * extra defensiveness here.) 14462 */ 14463 bp->b_iodone = NULL; 14464 14465 freerbuf(bp); 14466 14467 kmem_free(xp, sizeof (struct sd_xbuf)); 14468 } 14469 14470 /* 14471 * Function: sd_shadow_buf_free 14472 * 14473 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14474 * 14475 * Context: May be called under interrupt context 14476 */ 14477 14478 static void 14479 sd_shadow_buf_free(struct buf *bp) 14480 { 14481 struct sd_xbuf *xp; 14482 14483 ASSERT(bp != NULL); 14484 xp = SD_GET_XBUF(bp); 14485 ASSERT(xp != NULL); 14486 14487 #if defined(__sparc) 14488 /* 14489 * Call bp_mapout() before freeing the buf, in case a lower 14490 * layer or HBA had done a bp_mapin(). we must do this here 14491 * as we are the "originator" of the shadow buf. 14492 */ 14493 bp_mapout(bp); 14494 #endif 14495 14496 /* 14497 * Null out b_iodone before freeing the bp, to ensure that the driver 14498 * never gets confused by a stale value in this field. (Just a little 14499 * extra defensiveness here.) 14500 */ 14501 bp->b_iodone = NULL; 14502 14503 #if defined(__i386) || defined(__amd64) 14504 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14505 freerbuf(bp); 14506 #else 14507 scsi_free_consistent_buf(bp); 14508 #endif 14509 14510 kmem_free(xp, sizeof (struct sd_xbuf)); 14511 } 14512 14513 14514 /* 14515 * Function: sd_print_transport_rejected_message 14516 * 14517 * Description: This implements the ludicrously complex rules for printing 14518 * a "transport rejected" message. This is to address the 14519 * specific problem of having a flood of this error message 14520 * produced when a failover occurs. 14521 * 14522 * Context: Any. 14523 */ 14524 14525 static void 14526 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14527 int code) 14528 { 14529 ASSERT(un != NULL); 14530 ASSERT(mutex_owned(SD_MUTEX(un))); 14531 ASSERT(xp != NULL); 14532 14533 /* 14534 * Print the "transport rejected" message under the following 14535 * conditions: 14536 * 14537 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14538 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14539 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14540 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14541 * scsi_transport(9F) (which indicates that the target might have 14542 * gone off-line). This uses the un->un_tran_fatal_count 14543 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14544 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14545 * from scsi_transport(). 14546 * 14547 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14548 * the preceeding cases in order for the message to be printed. 14549 */ 14550 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) && 14551 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) { 14552 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14553 (code != TRAN_FATAL_ERROR) || 14554 (un->un_tran_fatal_count == 1)) { 14555 switch (code) { 14556 case TRAN_BADPKT: 14557 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14558 "transport rejected bad packet\n"); 14559 break; 14560 case TRAN_FATAL_ERROR: 14561 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14562 "transport rejected fatal error\n"); 14563 break; 14564 default: 14565 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14566 "transport rejected (%d)\n", code); 14567 break; 14568 } 14569 } 14570 } 14571 } 14572 14573 14574 /* 14575 * Function: sd_add_buf_to_waitq 14576 * 14577 * Description: Add the given buf(9S) struct to the wait queue for the 14578 * instance. If sorting is enabled, then the buf is added 14579 * to the queue via an elevator sort algorithm (a la 14580 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14581 * If sorting is not enabled, then the buf is just added 14582 * to the end of the wait queue. 14583 * 14584 * Return Code: void 14585 * 14586 * Context: Does not sleep/block, therefore technically can be called 14587 * from any context. However if sorting is enabled then the 14588 * execution time is indeterminate, and may take long if 14589 * the wait queue grows large. 14590 */ 14591 14592 static void 14593 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14594 { 14595 struct buf *ap; 14596 14597 ASSERT(bp != NULL); 14598 ASSERT(un != NULL); 14599 ASSERT(mutex_owned(SD_MUTEX(un))); 14600 14601 /* If the queue is empty, add the buf as the only entry & return. */ 14602 if (un->un_waitq_headp == NULL) { 14603 ASSERT(un->un_waitq_tailp == NULL); 14604 un->un_waitq_headp = un->un_waitq_tailp = bp; 14605 bp->av_forw = NULL; 14606 return; 14607 } 14608 14609 ASSERT(un->un_waitq_tailp != NULL); 14610 14611 /* 14612 * If sorting is disabled, just add the buf to the tail end of 14613 * the wait queue and return. 14614 */ 14615 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) { 14616 un->un_waitq_tailp->av_forw = bp; 14617 un->un_waitq_tailp = bp; 14618 bp->av_forw = NULL; 14619 return; 14620 } 14621 14622 /* 14623 * Sort thru the list of requests currently on the wait queue 14624 * and add the new buf request at the appropriate position. 14625 * 14626 * The un->un_waitq_headp is an activity chain pointer on which 14627 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14628 * first queue holds those requests which are positioned after 14629 * the current SD_GET_BLKNO() (in the first request); the second holds 14630 * requests which came in after their SD_GET_BLKNO() number was passed. 14631 * Thus we implement a one way scan, retracting after reaching 14632 * the end of the drive to the first request on the second 14633 * queue, at which time it becomes the first queue. 14634 * A one-way scan is natural because of the way UNIX read-ahead 14635 * blocks are allocated. 14636 * 14637 * If we lie after the first request, then we must locate the 14638 * second request list and add ourselves to it. 14639 */ 14640 ap = un->un_waitq_headp; 14641 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14642 while (ap->av_forw != NULL) { 14643 /* 14644 * Look for an "inversion" in the (normally 14645 * ascending) block numbers. This indicates 14646 * the start of the second request list. 14647 */ 14648 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14649 /* 14650 * Search the second request list for the 14651 * first request at a larger block number. 14652 * We go before that; however if there is 14653 * no such request, we go at the end. 14654 */ 14655 do { 14656 if (SD_GET_BLKNO(bp) < 14657 SD_GET_BLKNO(ap->av_forw)) { 14658 goto insert; 14659 } 14660 ap = ap->av_forw; 14661 } while (ap->av_forw != NULL); 14662 goto insert; /* after last */ 14663 } 14664 ap = ap->av_forw; 14665 } 14666 14667 /* 14668 * No inversions... we will go after the last, and 14669 * be the first request in the second request list. 14670 */ 14671 goto insert; 14672 } 14673 14674 /* 14675 * Request is at/after the current request... 14676 * sort in the first request list. 14677 */ 14678 while (ap->av_forw != NULL) { 14679 /* 14680 * We want to go after the current request (1) if 14681 * there is an inversion after it (i.e. it is the end 14682 * of the first request list), or (2) if the next 14683 * request is a larger block no. than our request. 14684 */ 14685 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14686 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14687 goto insert; 14688 } 14689 ap = ap->av_forw; 14690 } 14691 14692 /* 14693 * Neither a second list nor a larger request, therefore 14694 * we go at the end of the first list (which is the same 14695 * as the end of the whole schebang). 14696 */ 14697 insert: 14698 bp->av_forw = ap->av_forw; 14699 ap->av_forw = bp; 14700 14701 /* 14702 * If we inserted onto the tail end of the waitq, make sure the 14703 * tail pointer is updated. 14704 */ 14705 if (ap == un->un_waitq_tailp) { 14706 un->un_waitq_tailp = bp; 14707 } 14708 } 14709 14710 14711 /* 14712 * Function: sd_start_cmds 14713 * 14714 * Description: Remove and transport cmds from the driver queues. 14715 * 14716 * Arguments: un - pointer to the unit (soft state) struct for the target. 14717 * 14718 * immed_bp - ptr to a buf to be transported immediately. Only 14719 * the immed_bp is transported; bufs on the waitq are not 14720 * processed and the un_retry_bp is not checked. If immed_bp is 14721 * NULL, then normal queue processing is performed. 14722 * 14723 * Context: May be called from kernel thread context, interrupt context, 14724 * or runout callback context. This function may not block or 14725 * call routines that block. 14726 */ 14727 14728 static void 14729 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14730 { 14731 struct sd_xbuf *xp; 14732 struct buf *bp; 14733 void (*statp)(kstat_io_t *); 14734 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14735 void (*saved_statp)(kstat_io_t *); 14736 #endif 14737 int rval; 14738 struct sd_fm_internal *sfip = NULL; 14739 14740 ASSERT(un != NULL); 14741 ASSERT(mutex_owned(SD_MUTEX(un))); 14742 ASSERT(un->un_ncmds_in_transport >= 0); 14743 ASSERT(un->un_throttle >= 0); 14744 14745 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14746 14747 do { 14748 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14749 saved_statp = NULL; 14750 #endif 14751 14752 /* 14753 * If we are syncing or dumping, fail the command to 14754 * avoid recursively calling back into scsi_transport(). 14755 * The dump I/O itself uses a separate code path so this 14756 * only prevents non-dump I/O from being sent while dumping. 14757 * File system sync takes place before dumping begins. 14758 * During panic, filesystem I/O is allowed provided 14759 * un_in_callback is <= 1. This is to prevent recursion 14760 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14761 * sd_start_cmds and so on. See panic.c for more information 14762 * about the states the system can be in during panic. 14763 */ 14764 if ((un->un_state == SD_STATE_DUMPING) || 14765 (ddi_in_panic() && (un->un_in_callback > 1))) { 14766 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14767 "sd_start_cmds: panicking\n"); 14768 goto exit; 14769 } 14770 14771 if ((bp = immed_bp) != NULL) { 14772 /* 14773 * We have a bp that must be transported immediately. 14774 * It's OK to transport the immed_bp here without doing 14775 * the throttle limit check because the immed_bp is 14776 * always used in a retry/recovery case. This means 14777 * that we know we are not at the throttle limit by 14778 * virtue of the fact that to get here we must have 14779 * already gotten a command back via sdintr(). This also 14780 * relies on (1) the command on un_retry_bp preventing 14781 * further commands from the waitq from being issued; 14782 * and (2) the code in sd_retry_command checking the 14783 * throttle limit before issuing a delayed or immediate 14784 * retry. This holds even if the throttle limit is 14785 * currently ratcheted down from its maximum value. 14786 */ 14787 statp = kstat_runq_enter; 14788 if (bp == un->un_retry_bp) { 14789 ASSERT((un->un_retry_statp == NULL) || 14790 (un->un_retry_statp == kstat_waitq_enter) || 14791 (un->un_retry_statp == 14792 kstat_runq_back_to_waitq)); 14793 /* 14794 * If the waitq kstat was incremented when 14795 * sd_set_retry_bp() queued this bp for a retry, 14796 * then we must set up statp so that the waitq 14797 * count will get decremented correctly below. 14798 * Also we must clear un->un_retry_statp to 14799 * ensure that we do not act on a stale value 14800 * in this field. 14801 */ 14802 if ((un->un_retry_statp == kstat_waitq_enter) || 14803 (un->un_retry_statp == 14804 kstat_runq_back_to_waitq)) { 14805 statp = kstat_waitq_to_runq; 14806 } 14807 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14808 saved_statp = un->un_retry_statp; 14809 #endif 14810 un->un_retry_statp = NULL; 14811 14812 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14813 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14814 "un_throttle:%d un_ncmds_in_transport:%d\n", 14815 un, un->un_retry_bp, un->un_throttle, 14816 un->un_ncmds_in_transport); 14817 } else { 14818 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14819 "processing priority bp:0x%p\n", bp); 14820 } 14821 14822 } else if ((bp = un->un_waitq_headp) != NULL) { 14823 /* 14824 * A command on the waitq is ready to go, but do not 14825 * send it if: 14826 * 14827 * (1) the throttle limit has been reached, or 14828 * (2) a retry is pending, or 14829 * (3) a START_STOP_UNIT callback pending, or 14830 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14831 * command is pending. 14832 * 14833 * For all of these conditions, IO processing will 14834 * restart after the condition is cleared. 14835 */ 14836 if (un->un_ncmds_in_transport >= un->un_throttle) { 14837 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14838 "sd_start_cmds: exiting, " 14839 "throttle limit reached!\n"); 14840 goto exit; 14841 } 14842 if (un->un_retry_bp != NULL) { 14843 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14844 "sd_start_cmds: exiting, retry pending!\n"); 14845 goto exit; 14846 } 14847 if (un->un_startstop_timeid != NULL) { 14848 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14849 "sd_start_cmds: exiting, " 14850 "START_STOP pending!\n"); 14851 goto exit; 14852 } 14853 if (un->un_direct_priority_timeid != NULL) { 14854 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14855 "sd_start_cmds: exiting, " 14856 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14857 goto exit; 14858 } 14859 14860 /* Dequeue the command */ 14861 un->un_waitq_headp = bp->av_forw; 14862 if (un->un_waitq_headp == NULL) { 14863 un->un_waitq_tailp = NULL; 14864 } 14865 bp->av_forw = NULL; 14866 statp = kstat_waitq_to_runq; 14867 SD_TRACE(SD_LOG_IO_CORE, un, 14868 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14869 14870 } else { 14871 /* No work to do so bail out now */ 14872 SD_TRACE(SD_LOG_IO_CORE, un, 14873 "sd_start_cmds: no more work, exiting!\n"); 14874 goto exit; 14875 } 14876 14877 /* 14878 * Reset the state to normal. This is the mechanism by which 14879 * the state transitions from either SD_STATE_RWAIT or 14880 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14881 * If state is SD_STATE_PM_CHANGING then this command is 14882 * part of the device power control and the state must 14883 * not be put back to normal. Doing so would would 14884 * allow new commands to proceed when they shouldn't, 14885 * the device may be going off. 14886 */ 14887 if ((un->un_state != SD_STATE_SUSPENDED) && 14888 (un->un_state != SD_STATE_PM_CHANGING)) { 14889 New_state(un, SD_STATE_NORMAL); 14890 } 14891 14892 xp = SD_GET_XBUF(bp); 14893 ASSERT(xp != NULL); 14894 14895 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14896 /* 14897 * Allocate the scsi_pkt if we need one, or attach DMA 14898 * resources if we have a scsi_pkt that needs them. The 14899 * latter should only occur for commands that are being 14900 * retried. 14901 */ 14902 if ((xp->xb_pktp == NULL) || 14903 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14904 #else 14905 if (xp->xb_pktp == NULL) { 14906 #endif 14907 /* 14908 * There is no scsi_pkt allocated for this buf. Call 14909 * the initpkt function to allocate & init one. 14910 * 14911 * The scsi_init_pkt runout callback functionality is 14912 * implemented as follows: 14913 * 14914 * 1) The initpkt function always calls 14915 * scsi_init_pkt(9F) with sdrunout specified as the 14916 * callback routine. 14917 * 2) A successful packet allocation is initialized and 14918 * the I/O is transported. 14919 * 3) The I/O associated with an allocation resource 14920 * failure is left on its queue to be retried via 14921 * runout or the next I/O. 14922 * 4) The I/O associated with a DMA error is removed 14923 * from the queue and failed with EIO. Processing of 14924 * the transport queues is also halted to be 14925 * restarted via runout or the next I/O. 14926 * 5) The I/O associated with a CDB size or packet 14927 * size error is removed from the queue and failed 14928 * with EIO. Processing of the transport queues is 14929 * continued. 14930 * 14931 * Note: there is no interface for canceling a runout 14932 * callback. To prevent the driver from detaching or 14933 * suspending while a runout is pending the driver 14934 * state is set to SD_STATE_RWAIT 14935 * 14936 * Note: using the scsi_init_pkt callback facility can 14937 * result in an I/O request persisting at the head of 14938 * the list which cannot be satisfied even after 14939 * multiple retries. In the future the driver may 14940 * implement some kind of maximum runout count before 14941 * failing an I/O. 14942 * 14943 * Note: the use of funcp below may seem superfluous, 14944 * but it helps warlock figure out the correct 14945 * initpkt function calls (see [s]sd.wlcmd). 14946 */ 14947 struct scsi_pkt *pktp; 14948 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14949 14950 ASSERT(bp != un->un_rqs_bp); 14951 14952 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14953 switch ((*funcp)(bp, &pktp)) { 14954 case SD_PKT_ALLOC_SUCCESS: 14955 xp->xb_pktp = pktp; 14956 SD_TRACE(SD_LOG_IO_CORE, un, 14957 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14958 pktp); 14959 goto got_pkt; 14960 14961 case SD_PKT_ALLOC_FAILURE: 14962 /* 14963 * Temporary (hopefully) resource depletion. 14964 * Since retries and RQS commands always have a 14965 * scsi_pkt allocated, these cases should never 14966 * get here. So the only cases this needs to 14967 * handle is a bp from the waitq (which we put 14968 * back onto the waitq for sdrunout), or a bp 14969 * sent as an immed_bp (which we just fail). 14970 */ 14971 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14972 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14973 14974 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14975 14976 if (bp == immed_bp) { 14977 /* 14978 * If SD_XB_DMA_FREED is clear, then 14979 * this is a failure to allocate a 14980 * scsi_pkt, and we must fail the 14981 * command. 14982 */ 14983 if ((xp->xb_pkt_flags & 14984 SD_XB_DMA_FREED) == 0) { 14985 break; 14986 } 14987 14988 /* 14989 * If this immediate command is NOT our 14990 * un_retry_bp, then we must fail it. 14991 */ 14992 if (bp != un->un_retry_bp) { 14993 break; 14994 } 14995 14996 /* 14997 * We get here if this cmd is our 14998 * un_retry_bp that was DMAFREED, but 14999 * scsi_init_pkt() failed to reallocate 15000 * DMA resources when we attempted to 15001 * retry it. This can happen when an 15002 * mpxio failover is in progress, but 15003 * we don't want to just fail the 15004 * command in this case. 15005 * 15006 * Use timeout(9F) to restart it after 15007 * a 100ms delay. We don't want to 15008 * let sdrunout() restart it, because 15009 * sdrunout() is just supposed to start 15010 * commands that are sitting on the 15011 * wait queue. The un_retry_bp stays 15012 * set until the command completes, but 15013 * sdrunout can be called many times 15014 * before that happens. Since sdrunout 15015 * cannot tell if the un_retry_bp is 15016 * already in the transport, it could 15017 * end up calling scsi_transport() for 15018 * the un_retry_bp multiple times. 15019 * 15020 * Also: don't schedule the callback 15021 * if some other callback is already 15022 * pending. 15023 */ 15024 if (un->un_retry_statp == NULL) { 15025 /* 15026 * restore the kstat pointer to 15027 * keep kstat counts coherent 15028 * when we do retry the command. 15029 */ 15030 un->un_retry_statp = 15031 saved_statp; 15032 } 15033 15034 if ((un->un_startstop_timeid == NULL) && 15035 (un->un_retry_timeid == NULL) && 15036 (un->un_direct_priority_timeid == 15037 NULL)) { 15038 15039 un->un_retry_timeid = 15040 timeout( 15041 sd_start_retry_command, 15042 un, SD_RESTART_TIMEOUT); 15043 } 15044 goto exit; 15045 } 15046 15047 #else 15048 if (bp == immed_bp) { 15049 break; /* Just fail the command */ 15050 } 15051 #endif 15052 15053 /* Add the buf back to the head of the waitq */ 15054 bp->av_forw = un->un_waitq_headp; 15055 un->un_waitq_headp = bp; 15056 if (un->un_waitq_tailp == NULL) { 15057 un->un_waitq_tailp = bp; 15058 } 15059 goto exit; 15060 15061 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15062 /* 15063 * HBA DMA resource failure. Fail the command 15064 * and continue processing of the queues. 15065 */ 15066 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15067 "sd_start_cmds: " 15068 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15069 break; 15070 15071 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15072 /* 15073 * Note:x86: Partial DMA mapping not supported 15074 * for USCSI commands, and all the needed DMA 15075 * resources were not allocated. 15076 */ 15077 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15078 "sd_start_cmds: " 15079 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15080 break; 15081 15082 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15083 /* 15084 * Note:x86: Request cannot fit into CDB based 15085 * on lba and len. 15086 */ 15087 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15088 "sd_start_cmds: " 15089 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15090 break; 15091 15092 default: 15093 /* Should NEVER get here! */ 15094 panic("scsi_initpkt error"); 15095 /*NOTREACHED*/ 15096 } 15097 15098 /* 15099 * Fatal error in allocating a scsi_pkt for this buf. 15100 * Update kstats & return the buf with an error code. 15101 * We must use sd_return_failed_command_no_restart() to 15102 * avoid a recursive call back into sd_start_cmds(). 15103 * However this also means that we must keep processing 15104 * the waitq here in order to avoid stalling. 15105 */ 15106 if (statp == kstat_waitq_to_runq) { 15107 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15108 } 15109 sd_return_failed_command_no_restart(un, bp, EIO); 15110 if (bp == immed_bp) { 15111 /* immed_bp is gone by now, so clear this */ 15112 immed_bp = NULL; 15113 } 15114 continue; 15115 } 15116 got_pkt: 15117 if (bp == immed_bp) { 15118 /* goto the head of the class.... */ 15119 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15120 } 15121 15122 un->un_ncmds_in_transport++; 15123 SD_UPDATE_KSTATS(un, statp, bp); 15124 15125 /* 15126 * Call scsi_transport() to send the command to the target. 15127 * According to SCSA architecture, we must drop the mutex here 15128 * before calling scsi_transport() in order to avoid deadlock. 15129 * Note that the scsi_pkt's completion routine can be executed 15130 * (from interrupt context) even before the call to 15131 * scsi_transport() returns. 15132 */ 15133 SD_TRACE(SD_LOG_IO_CORE, un, 15134 "sd_start_cmds: calling scsi_transport()\n"); 15135 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15136 15137 mutex_exit(SD_MUTEX(un)); 15138 rval = scsi_transport(xp->xb_pktp); 15139 mutex_enter(SD_MUTEX(un)); 15140 15141 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15142 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15143 15144 switch (rval) { 15145 case TRAN_ACCEPT: 15146 /* Clear this with every pkt accepted by the HBA */ 15147 un->un_tran_fatal_count = 0; 15148 break; /* Success; try the next cmd (if any) */ 15149 15150 case TRAN_BUSY: 15151 un->un_ncmds_in_transport--; 15152 ASSERT(un->un_ncmds_in_transport >= 0); 15153 15154 /* 15155 * Don't retry request sense, the sense data 15156 * is lost when another request is sent. 15157 * Free up the rqs buf and retry 15158 * the original failed cmd. Update kstat. 15159 */ 15160 if (bp == un->un_rqs_bp) { 15161 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15162 bp = sd_mark_rqs_idle(un, xp); 15163 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15164 NULL, NULL, EIO, un->un_busy_timeout / 500, 15165 kstat_waitq_enter); 15166 goto exit; 15167 } 15168 15169 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15170 /* 15171 * Free the DMA resources for the scsi_pkt. This will 15172 * allow mpxio to select another path the next time 15173 * we call scsi_transport() with this scsi_pkt. 15174 * See sdintr() for the rationalization behind this. 15175 */ 15176 if ((un->un_f_is_fibre == TRUE) && 15177 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15178 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15179 scsi_dmafree(xp->xb_pktp); 15180 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15181 } 15182 #endif 15183 15184 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15185 /* 15186 * Commands that are SD_PATH_DIRECT_PRIORITY 15187 * are for error recovery situations. These do 15188 * not use the normal command waitq, so if they 15189 * get a TRAN_BUSY we cannot put them back onto 15190 * the waitq for later retry. One possible 15191 * problem is that there could already be some 15192 * other command on un_retry_bp that is waiting 15193 * for this one to complete, so we would be 15194 * deadlocked if we put this command back onto 15195 * the waitq for later retry (since un_retry_bp 15196 * must complete before the driver gets back to 15197 * commands on the waitq). 15198 * 15199 * To avoid deadlock we must schedule a callback 15200 * that will restart this command after a set 15201 * interval. This should keep retrying for as 15202 * long as the underlying transport keeps 15203 * returning TRAN_BUSY (just like for other 15204 * commands). Use the same timeout interval as 15205 * for the ordinary TRAN_BUSY retry. 15206 */ 15207 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15208 "sd_start_cmds: scsi_transport() returned " 15209 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15210 15211 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15212 un->un_direct_priority_timeid = 15213 timeout(sd_start_direct_priority_command, 15214 bp, un->un_busy_timeout / 500); 15215 15216 goto exit; 15217 } 15218 15219 /* 15220 * For TRAN_BUSY, we want to reduce the throttle value, 15221 * unless we are retrying a command. 15222 */ 15223 if (bp != un->un_retry_bp) { 15224 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15225 } 15226 15227 /* 15228 * Set up the bp to be tried again 10 ms later. 15229 * Note:x86: Is there a timeout value in the sd_lun 15230 * for this condition? 15231 */ 15232 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500, 15233 kstat_runq_back_to_waitq); 15234 goto exit; 15235 15236 case TRAN_FATAL_ERROR: 15237 un->un_tran_fatal_count++; 15238 /* FALLTHRU */ 15239 15240 case TRAN_BADPKT: 15241 default: 15242 un->un_ncmds_in_transport--; 15243 ASSERT(un->un_ncmds_in_transport >= 0); 15244 15245 /* 15246 * If this is our REQUEST SENSE command with a 15247 * transport error, we must get back the pointers 15248 * to the original buf, and mark the REQUEST 15249 * SENSE command as "available". 15250 */ 15251 if (bp == un->un_rqs_bp) { 15252 bp = sd_mark_rqs_idle(un, xp); 15253 xp = SD_GET_XBUF(bp); 15254 } else { 15255 /* 15256 * Legacy behavior: do not update transport 15257 * error count for request sense commands. 15258 */ 15259 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15260 } 15261 15262 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15263 sd_print_transport_rejected_message(un, xp, rval); 15264 15265 /* 15266 * This command will be terminated by SD driver due 15267 * to a fatal transport error. We should post 15268 * ereport.io.scsi.cmd.disk.tran with driver-assessment 15269 * of "fail" for any command to indicate this 15270 * situation. 15271 */ 15272 if (xp->xb_ena > 0) { 15273 ASSERT(un->un_fm_private != NULL); 15274 sfip = un->un_fm_private; 15275 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT; 15276 sd_ssc_extract_info(&sfip->fm_ssc, un, 15277 xp->xb_pktp, bp, xp); 15278 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15279 } 15280 15281 /* 15282 * We must use sd_return_failed_command_no_restart() to 15283 * avoid a recursive call back into sd_start_cmds(). 15284 * However this also means that we must keep processing 15285 * the waitq here in order to avoid stalling. 15286 */ 15287 sd_return_failed_command_no_restart(un, bp, EIO); 15288 15289 /* 15290 * Notify any threads waiting in sd_ddi_suspend() that 15291 * a command completion has occurred. 15292 */ 15293 if (un->un_state == SD_STATE_SUSPENDED) { 15294 cv_broadcast(&un->un_disk_busy_cv); 15295 } 15296 15297 if (bp == immed_bp) { 15298 /* immed_bp is gone by now, so clear this */ 15299 immed_bp = NULL; 15300 } 15301 break; 15302 } 15303 15304 } while (immed_bp == NULL); 15305 15306 exit: 15307 ASSERT(mutex_owned(SD_MUTEX(un))); 15308 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15309 } 15310 15311 15312 /* 15313 * Function: sd_return_command 15314 * 15315 * Description: Returns a command to its originator (with or without an 15316 * error). Also starts commands waiting to be transported 15317 * to the target. 15318 * 15319 * Context: May be called from interrupt, kernel, or timeout context 15320 */ 15321 15322 static void 15323 sd_return_command(struct sd_lun *un, struct buf *bp) 15324 { 15325 struct sd_xbuf *xp; 15326 struct scsi_pkt *pktp; 15327 struct sd_fm_internal *sfip; 15328 15329 ASSERT(bp != NULL); 15330 ASSERT(un != NULL); 15331 ASSERT(mutex_owned(SD_MUTEX(un))); 15332 ASSERT(bp != un->un_rqs_bp); 15333 xp = SD_GET_XBUF(bp); 15334 ASSERT(xp != NULL); 15335 15336 pktp = SD_GET_PKTP(bp); 15337 sfip = (struct sd_fm_internal *)un->un_fm_private; 15338 ASSERT(sfip != NULL); 15339 15340 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15341 15342 /* 15343 * Note: check for the "sdrestart failed" case. 15344 */ 15345 if ((un->un_partial_dma_supported == 1) && 15346 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15347 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15348 (xp->xb_pktp->pkt_resid == 0)) { 15349 15350 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15351 /* 15352 * Successfully set up next portion of cmd 15353 * transfer, try sending it 15354 */ 15355 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15356 NULL, NULL, 0, (clock_t)0, NULL); 15357 sd_start_cmds(un, NULL); 15358 return; /* Note:x86: need a return here? */ 15359 } 15360 } 15361 15362 /* 15363 * If this is the failfast bp, clear it from un_failfast_bp. This 15364 * can happen if upon being re-tried the failfast bp either 15365 * succeeded or encountered another error (possibly even a different 15366 * error than the one that precipitated the failfast state, but in 15367 * that case it would have had to exhaust retries as well). Regardless, 15368 * this should not occur whenever the instance is in the active 15369 * failfast state. 15370 */ 15371 if (bp == un->un_failfast_bp) { 15372 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15373 un->un_failfast_bp = NULL; 15374 } 15375 15376 /* 15377 * Clear the failfast state upon successful completion of ANY cmd. 15378 */ 15379 if (bp->b_error == 0) { 15380 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15381 /* 15382 * If this is a successful command, but used to be retried, 15383 * we will take it as a recovered command and post an 15384 * ereport with driver-assessment of "recovered". 15385 */ 15386 if (xp->xb_ena > 0) { 15387 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15388 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY); 15389 } 15390 } else { 15391 /* 15392 * If this is a failed non-USCSI command we will post an 15393 * ereport with driver-assessment set accordingly("fail" or 15394 * "fatal"). 15395 */ 15396 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15397 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15398 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15399 } 15400 } 15401 15402 /* 15403 * This is used if the command was retried one or more times. Show that 15404 * we are done with it, and allow processing of the waitq to resume. 15405 */ 15406 if (bp == un->un_retry_bp) { 15407 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15408 "sd_return_command: un:0x%p: " 15409 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15410 un->un_retry_bp = NULL; 15411 un->un_retry_statp = NULL; 15412 } 15413 15414 SD_UPDATE_RDWR_STATS(un, bp); 15415 SD_UPDATE_PARTITION_STATS(un, bp); 15416 15417 switch (un->un_state) { 15418 case SD_STATE_SUSPENDED: 15419 /* 15420 * Notify any threads waiting in sd_ddi_suspend() that 15421 * a command completion has occurred. 15422 */ 15423 cv_broadcast(&un->un_disk_busy_cv); 15424 break; 15425 default: 15426 sd_start_cmds(un, NULL); 15427 break; 15428 } 15429 15430 /* Return this command up the iodone chain to its originator. */ 15431 mutex_exit(SD_MUTEX(un)); 15432 15433 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15434 xp->xb_pktp = NULL; 15435 15436 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15437 15438 ASSERT(!mutex_owned(SD_MUTEX(un))); 15439 mutex_enter(SD_MUTEX(un)); 15440 15441 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15442 } 15443 15444 15445 /* 15446 * Function: sd_return_failed_command 15447 * 15448 * Description: Command completion when an error occurred. 15449 * 15450 * Context: May be called from interrupt context 15451 */ 15452 15453 static void 15454 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15455 { 15456 ASSERT(bp != NULL); 15457 ASSERT(un != NULL); 15458 ASSERT(mutex_owned(SD_MUTEX(un))); 15459 15460 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15461 "sd_return_failed_command: entry\n"); 15462 15463 /* 15464 * b_resid could already be nonzero due to a partial data 15465 * transfer, so do not change it here. 15466 */ 15467 SD_BIOERROR(bp, errcode); 15468 15469 sd_return_command(un, bp); 15470 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15471 "sd_return_failed_command: exit\n"); 15472 } 15473 15474 15475 /* 15476 * Function: sd_return_failed_command_no_restart 15477 * 15478 * Description: Same as sd_return_failed_command, but ensures that no 15479 * call back into sd_start_cmds will be issued. 15480 * 15481 * Context: May be called from interrupt context 15482 */ 15483 15484 static void 15485 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15486 int errcode) 15487 { 15488 struct sd_xbuf *xp; 15489 15490 ASSERT(bp != NULL); 15491 ASSERT(un != NULL); 15492 ASSERT(mutex_owned(SD_MUTEX(un))); 15493 xp = SD_GET_XBUF(bp); 15494 ASSERT(xp != NULL); 15495 ASSERT(errcode != 0); 15496 15497 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15498 "sd_return_failed_command_no_restart: entry\n"); 15499 15500 /* 15501 * b_resid could already be nonzero due to a partial data 15502 * transfer, so do not change it here. 15503 */ 15504 SD_BIOERROR(bp, errcode); 15505 15506 /* 15507 * If this is the failfast bp, clear it. This can happen if the 15508 * failfast bp encounterd a fatal error when we attempted to 15509 * re-try it (such as a scsi_transport(9F) failure). However 15510 * we should NOT be in an active failfast state if the failfast 15511 * bp is not NULL. 15512 */ 15513 if (bp == un->un_failfast_bp) { 15514 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15515 un->un_failfast_bp = NULL; 15516 } 15517 15518 if (bp == un->un_retry_bp) { 15519 /* 15520 * This command was retried one or more times. Show that we are 15521 * done with it, and allow processing of the waitq to resume. 15522 */ 15523 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15524 "sd_return_failed_command_no_restart: " 15525 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15526 un->un_retry_bp = NULL; 15527 un->un_retry_statp = NULL; 15528 } 15529 15530 SD_UPDATE_RDWR_STATS(un, bp); 15531 SD_UPDATE_PARTITION_STATS(un, bp); 15532 15533 mutex_exit(SD_MUTEX(un)); 15534 15535 if (xp->xb_pktp != NULL) { 15536 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15537 xp->xb_pktp = NULL; 15538 } 15539 15540 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15541 15542 mutex_enter(SD_MUTEX(un)); 15543 15544 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15545 "sd_return_failed_command_no_restart: exit\n"); 15546 } 15547 15548 15549 /* 15550 * Function: sd_retry_command 15551 * 15552 * Description: queue up a command for retry, or (optionally) fail it 15553 * if retry counts are exhausted. 15554 * 15555 * Arguments: un - Pointer to the sd_lun struct for the target. 15556 * 15557 * bp - Pointer to the buf for the command to be retried. 15558 * 15559 * retry_check_flag - Flag to see which (if any) of the retry 15560 * counts should be decremented/checked. If the indicated 15561 * retry count is exhausted, then the command will not be 15562 * retried; it will be failed instead. This should use a 15563 * value equal to one of the following: 15564 * 15565 * SD_RETRIES_NOCHECK 15566 * SD_RESD_RETRIES_STANDARD 15567 * SD_RETRIES_VICTIM 15568 * 15569 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15570 * if the check should be made to see of FLAG_ISOLATE is set 15571 * in the pkt. If FLAG_ISOLATE is set, then the command is 15572 * not retried, it is simply failed. 15573 * 15574 * user_funcp - Ptr to function to call before dispatching the 15575 * command. May be NULL if no action needs to be performed. 15576 * (Primarily intended for printing messages.) 15577 * 15578 * user_arg - Optional argument to be passed along to 15579 * the user_funcp call. 15580 * 15581 * failure_code - errno return code to set in the bp if the 15582 * command is going to be failed. 15583 * 15584 * retry_delay - Retry delay interval in (clock_t) units. May 15585 * be zero which indicates that the retry should be retried 15586 * immediately (ie, without an intervening delay). 15587 * 15588 * statp - Ptr to kstat function to be updated if the command 15589 * is queued for a delayed retry. May be NULL if no kstat 15590 * update is desired. 15591 * 15592 * Context: May be called from interrupt context. 15593 */ 15594 15595 static void 15596 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15597 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15598 code), void *user_arg, int failure_code, clock_t retry_delay, 15599 void (*statp)(kstat_io_t *)) 15600 { 15601 struct sd_xbuf *xp; 15602 struct scsi_pkt *pktp; 15603 struct sd_fm_internal *sfip; 15604 15605 ASSERT(un != NULL); 15606 ASSERT(mutex_owned(SD_MUTEX(un))); 15607 ASSERT(bp != NULL); 15608 xp = SD_GET_XBUF(bp); 15609 ASSERT(xp != NULL); 15610 pktp = SD_GET_PKTP(bp); 15611 ASSERT(pktp != NULL); 15612 15613 sfip = (struct sd_fm_internal *)un->un_fm_private; 15614 ASSERT(sfip != NULL); 15615 15616 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15617 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15618 15619 /* 15620 * If we are syncing or dumping, fail the command to avoid 15621 * recursively calling back into scsi_transport(). 15622 */ 15623 if (ddi_in_panic()) { 15624 goto fail_command_no_log; 15625 } 15626 15627 /* 15628 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15629 * log an error and fail the command. 15630 */ 15631 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15632 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15633 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15634 sd_dump_memory(un, SD_LOG_IO, "CDB", 15635 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15636 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15637 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15638 goto fail_command; 15639 } 15640 15641 /* 15642 * If we are suspended, then put the command onto head of the 15643 * wait queue since we don't want to start more commands, and 15644 * clear the un_retry_bp. Next time when we are resumed, will 15645 * handle the command in the wait queue. 15646 */ 15647 switch (un->un_state) { 15648 case SD_STATE_SUSPENDED: 15649 case SD_STATE_DUMPING: 15650 bp->av_forw = un->un_waitq_headp; 15651 un->un_waitq_headp = bp; 15652 if (un->un_waitq_tailp == NULL) { 15653 un->un_waitq_tailp = bp; 15654 } 15655 if (bp == un->un_retry_bp) { 15656 un->un_retry_bp = NULL; 15657 un->un_retry_statp = NULL; 15658 } 15659 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15660 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15661 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15662 return; 15663 default: 15664 break; 15665 } 15666 15667 /* 15668 * If the caller wants us to check FLAG_ISOLATE, then see if that 15669 * is set; if it is then we do not want to retry the command. 15670 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15671 */ 15672 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15673 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15674 goto fail_command; 15675 } 15676 } 15677 15678 15679 /* 15680 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15681 * command timeout or a selection timeout has occurred. This means 15682 * that we were unable to establish an kind of communication with 15683 * the target, and subsequent retries and/or commands are likely 15684 * to encounter similar results and take a long time to complete. 15685 * 15686 * If this is a failfast error condition, we need to update the 15687 * failfast state, even if this bp does not have B_FAILFAST set. 15688 */ 15689 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15690 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15691 ASSERT(un->un_failfast_bp == NULL); 15692 /* 15693 * If we are already in the active failfast state, and 15694 * another failfast error condition has been detected, 15695 * then fail this command if it has B_FAILFAST set. 15696 * If B_FAILFAST is clear, then maintain the legacy 15697 * behavior of retrying heroically, even tho this will 15698 * take a lot more time to fail the command. 15699 */ 15700 if (bp->b_flags & B_FAILFAST) { 15701 goto fail_command; 15702 } 15703 } else { 15704 /* 15705 * We're not in the active failfast state, but we 15706 * have a failfast error condition, so we must begin 15707 * transition to the next state. We do this regardless 15708 * of whether or not this bp has B_FAILFAST set. 15709 */ 15710 if (un->un_failfast_bp == NULL) { 15711 /* 15712 * This is the first bp to meet a failfast 15713 * condition so save it on un_failfast_bp & 15714 * do normal retry processing. Do not enter 15715 * active failfast state yet. This marks 15716 * entry into the "failfast pending" state. 15717 */ 15718 un->un_failfast_bp = bp; 15719 15720 } else if (un->un_failfast_bp == bp) { 15721 /* 15722 * This is the second time *this* bp has 15723 * encountered a failfast error condition, 15724 * so enter active failfast state & flush 15725 * queues as appropriate. 15726 */ 15727 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15728 un->un_failfast_bp = NULL; 15729 sd_failfast_flushq(un); 15730 15731 /* 15732 * Fail this bp now if B_FAILFAST set; 15733 * otherwise continue with retries. (It would 15734 * be pretty ironic if this bp succeeded on a 15735 * subsequent retry after we just flushed all 15736 * the queues). 15737 */ 15738 if (bp->b_flags & B_FAILFAST) { 15739 goto fail_command; 15740 } 15741 15742 #if !defined(lint) && !defined(__lint) 15743 } else { 15744 /* 15745 * If neither of the preceeding conditionals 15746 * was true, it means that there is some 15747 * *other* bp that has met an inital failfast 15748 * condition and is currently either being 15749 * retried or is waiting to be retried. In 15750 * that case we should perform normal retry 15751 * processing on *this* bp, since there is a 15752 * chance that the current failfast condition 15753 * is transient and recoverable. If that does 15754 * not turn out to be the case, then retries 15755 * will be cleared when the wait queue is 15756 * flushed anyway. 15757 */ 15758 #endif 15759 } 15760 } 15761 } else { 15762 /* 15763 * SD_RETRIES_FAILFAST is clear, which indicates that we 15764 * likely were able to at least establish some level of 15765 * communication with the target and subsequent commands 15766 * and/or retries are likely to get through to the target, 15767 * In this case we want to be aggressive about clearing 15768 * the failfast state. Note that this does not affect 15769 * the "failfast pending" condition. 15770 */ 15771 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15772 } 15773 15774 15775 /* 15776 * Check the specified retry count to see if we can still do 15777 * any retries with this pkt before we should fail it. 15778 */ 15779 switch (retry_check_flag & SD_RETRIES_MASK) { 15780 case SD_RETRIES_VICTIM: 15781 /* 15782 * Check the victim retry count. If exhausted, then fall 15783 * thru & check against the standard retry count. 15784 */ 15785 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15786 /* Increment count & proceed with the retry */ 15787 xp->xb_victim_retry_count++; 15788 break; 15789 } 15790 /* Victim retries exhausted, fall back to std. retries... */ 15791 /* FALLTHRU */ 15792 15793 case SD_RETRIES_STANDARD: 15794 if (xp->xb_retry_count >= un->un_retry_count) { 15795 /* Retries exhausted, fail the command */ 15796 SD_TRACE(SD_LOG_IO_CORE, un, 15797 "sd_retry_command: retries exhausted!\n"); 15798 /* 15799 * update b_resid for failed SCMD_READ & SCMD_WRITE 15800 * commands with nonzero pkt_resid. 15801 */ 15802 if ((pktp->pkt_reason == CMD_CMPLT) && 15803 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15804 (pktp->pkt_resid != 0)) { 15805 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15806 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15807 SD_UPDATE_B_RESID(bp, pktp); 15808 } 15809 } 15810 goto fail_command; 15811 } 15812 xp->xb_retry_count++; 15813 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15814 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15815 break; 15816 15817 case SD_RETRIES_UA: 15818 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15819 /* Retries exhausted, fail the command */ 15820 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15821 "Unit Attention retries exhausted. " 15822 "Check the target.\n"); 15823 goto fail_command; 15824 } 15825 xp->xb_ua_retry_count++; 15826 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15827 "sd_retry_command: retry count:%d\n", 15828 xp->xb_ua_retry_count); 15829 break; 15830 15831 case SD_RETRIES_BUSY: 15832 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15833 /* Retries exhausted, fail the command */ 15834 SD_TRACE(SD_LOG_IO_CORE, un, 15835 "sd_retry_command: retries exhausted!\n"); 15836 goto fail_command; 15837 } 15838 xp->xb_retry_count++; 15839 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15840 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15841 break; 15842 15843 case SD_RETRIES_NOCHECK: 15844 default: 15845 /* No retry count to check. Just proceed with the retry */ 15846 break; 15847 } 15848 15849 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15850 15851 /* 15852 * If this is a non-USCSI command being retried 15853 * during execution last time, we should post an ereport with 15854 * driver-assessment of the value "retry". 15855 * For partial DMA, request sense and STATUS_QFULL, there are no 15856 * hardware errors, we bypass ereport posting. 15857 */ 15858 if (failure_code != 0) { 15859 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15860 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15861 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY); 15862 } 15863 } 15864 15865 /* 15866 * If we were given a zero timeout, we must attempt to retry the 15867 * command immediately (ie, without a delay). 15868 */ 15869 if (retry_delay == 0) { 15870 /* 15871 * Check some limiting conditions to see if we can actually 15872 * do the immediate retry. If we cannot, then we must 15873 * fall back to queueing up a delayed retry. 15874 */ 15875 if (un->un_ncmds_in_transport >= un->un_throttle) { 15876 /* 15877 * We are at the throttle limit for the target, 15878 * fall back to delayed retry. 15879 */ 15880 retry_delay = un->un_busy_timeout; 15881 statp = kstat_waitq_enter; 15882 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15883 "sd_retry_command: immed. retry hit " 15884 "throttle!\n"); 15885 } else { 15886 /* 15887 * We're clear to proceed with the immediate retry. 15888 * First call the user-provided function (if any) 15889 */ 15890 if (user_funcp != NULL) { 15891 (*user_funcp)(un, bp, user_arg, 15892 SD_IMMEDIATE_RETRY_ISSUED); 15893 #ifdef __lock_lint 15894 sd_print_incomplete_msg(un, bp, user_arg, 15895 SD_IMMEDIATE_RETRY_ISSUED); 15896 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15897 SD_IMMEDIATE_RETRY_ISSUED); 15898 sd_print_sense_failed_msg(un, bp, user_arg, 15899 SD_IMMEDIATE_RETRY_ISSUED); 15900 #endif 15901 } 15902 15903 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15904 "sd_retry_command: issuing immediate retry\n"); 15905 15906 /* 15907 * Call sd_start_cmds() to transport the command to 15908 * the target. 15909 */ 15910 sd_start_cmds(un, bp); 15911 15912 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15913 "sd_retry_command exit\n"); 15914 return; 15915 } 15916 } 15917 15918 /* 15919 * Set up to retry the command after a delay. 15920 * First call the user-provided function (if any) 15921 */ 15922 if (user_funcp != NULL) { 15923 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15924 } 15925 15926 sd_set_retry_bp(un, bp, retry_delay, statp); 15927 15928 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15929 return; 15930 15931 fail_command: 15932 15933 if (user_funcp != NULL) { 15934 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15935 } 15936 15937 fail_command_no_log: 15938 15939 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15940 "sd_retry_command: returning failed command\n"); 15941 15942 sd_return_failed_command(un, bp, failure_code); 15943 15944 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15945 } 15946 15947 15948 /* 15949 * Function: sd_set_retry_bp 15950 * 15951 * Description: Set up the given bp for retry. 15952 * 15953 * Arguments: un - ptr to associated softstate 15954 * bp - ptr to buf(9S) for the command 15955 * retry_delay - time interval before issuing retry (may be 0) 15956 * statp - optional pointer to kstat function 15957 * 15958 * Context: May be called under interrupt context 15959 */ 15960 15961 static void 15962 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15963 void (*statp)(kstat_io_t *)) 15964 { 15965 ASSERT(un != NULL); 15966 ASSERT(mutex_owned(SD_MUTEX(un))); 15967 ASSERT(bp != NULL); 15968 15969 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15970 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15971 15972 /* 15973 * Indicate that the command is being retried. This will not allow any 15974 * other commands on the wait queue to be transported to the target 15975 * until this command has been completed (success or failure). The 15976 * "retry command" is not transported to the target until the given 15977 * time delay expires, unless the user specified a 0 retry_delay. 15978 * 15979 * Note: the timeout(9F) callback routine is what actually calls 15980 * sd_start_cmds() to transport the command, with the exception of a 15981 * zero retry_delay. The only current implementor of a zero retry delay 15982 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15983 */ 15984 if (un->un_retry_bp == NULL) { 15985 ASSERT(un->un_retry_statp == NULL); 15986 un->un_retry_bp = bp; 15987 15988 /* 15989 * If the user has not specified a delay the command should 15990 * be queued and no timeout should be scheduled. 15991 */ 15992 if (retry_delay == 0) { 15993 /* 15994 * Save the kstat pointer that will be used in the 15995 * call to SD_UPDATE_KSTATS() below, so that 15996 * sd_start_cmds() can correctly decrement the waitq 15997 * count when it is time to transport this command. 15998 */ 15999 un->un_retry_statp = statp; 16000 goto done; 16001 } 16002 } 16003 16004 if (un->un_retry_bp == bp) { 16005 /* 16006 * Save the kstat pointer that will be used in the call to 16007 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 16008 * correctly decrement the waitq count when it is time to 16009 * transport this command. 16010 */ 16011 un->un_retry_statp = statp; 16012 16013 /* 16014 * Schedule a timeout if: 16015 * 1) The user has specified a delay. 16016 * 2) There is not a START_STOP_UNIT callback pending. 16017 * 16018 * If no delay has been specified, then it is up to the caller 16019 * to ensure that IO processing continues without stalling. 16020 * Effectively, this means that the caller will issue the 16021 * required call to sd_start_cmds(). The START_STOP_UNIT 16022 * callback does this after the START STOP UNIT command has 16023 * completed. In either of these cases we should not schedule 16024 * a timeout callback here. Also don't schedule the timeout if 16025 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 16026 */ 16027 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 16028 (un->un_direct_priority_timeid == NULL)) { 16029 un->un_retry_timeid = 16030 timeout(sd_start_retry_command, un, retry_delay); 16031 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16032 "sd_set_retry_bp: setting timeout: un: 0x%p" 16033 " bp:0x%p un_retry_timeid:0x%p\n", 16034 un, bp, un->un_retry_timeid); 16035 } 16036 } else { 16037 /* 16038 * We only get in here if there is already another command 16039 * waiting to be retried. In this case, we just put the 16040 * given command onto the wait queue, so it can be transported 16041 * after the current retry command has completed. 16042 * 16043 * Also we have to make sure that if the command at the head 16044 * of the wait queue is the un_failfast_bp, that we do not 16045 * put ahead of it any other commands that are to be retried. 16046 */ 16047 if ((un->un_failfast_bp != NULL) && 16048 (un->un_failfast_bp == un->un_waitq_headp)) { 16049 /* 16050 * Enqueue this command AFTER the first command on 16051 * the wait queue (which is also un_failfast_bp). 16052 */ 16053 bp->av_forw = un->un_waitq_headp->av_forw; 16054 un->un_waitq_headp->av_forw = bp; 16055 if (un->un_waitq_headp == un->un_waitq_tailp) { 16056 un->un_waitq_tailp = bp; 16057 } 16058 } else { 16059 /* Enqueue this command at the head of the waitq. */ 16060 bp->av_forw = un->un_waitq_headp; 16061 un->un_waitq_headp = bp; 16062 if (un->un_waitq_tailp == NULL) { 16063 un->un_waitq_tailp = bp; 16064 } 16065 } 16066 16067 if (statp == NULL) { 16068 statp = kstat_waitq_enter; 16069 } 16070 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16071 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 16072 } 16073 16074 done: 16075 if (statp != NULL) { 16076 SD_UPDATE_KSTATS(un, statp, bp); 16077 } 16078 16079 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16080 "sd_set_retry_bp: exit un:0x%p\n", un); 16081 } 16082 16083 16084 /* 16085 * Function: sd_start_retry_command 16086 * 16087 * Description: Start the command that has been waiting on the target's 16088 * retry queue. Called from timeout(9F) context after the 16089 * retry delay interval has expired. 16090 * 16091 * Arguments: arg - pointer to associated softstate for the device. 16092 * 16093 * Context: timeout(9F) thread context. May not sleep. 16094 */ 16095 16096 static void 16097 sd_start_retry_command(void *arg) 16098 { 16099 struct sd_lun *un = arg; 16100 16101 ASSERT(un != NULL); 16102 ASSERT(!mutex_owned(SD_MUTEX(un))); 16103 16104 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16105 "sd_start_retry_command: entry\n"); 16106 16107 mutex_enter(SD_MUTEX(un)); 16108 16109 un->un_retry_timeid = NULL; 16110 16111 if (un->un_retry_bp != NULL) { 16112 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16113 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16114 un, un->un_retry_bp); 16115 sd_start_cmds(un, un->un_retry_bp); 16116 } 16117 16118 mutex_exit(SD_MUTEX(un)); 16119 16120 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16121 "sd_start_retry_command: exit\n"); 16122 } 16123 16124 /* 16125 * Function: sd_rmw_msg_print_handler 16126 * 16127 * Description: If RMW mode is enabled and warning message is triggered 16128 * print I/O count during a fixed interval. 16129 * 16130 * Arguments: arg - pointer to associated softstate for the device. 16131 * 16132 * Context: timeout(9F) thread context. May not sleep. 16133 */ 16134 static void 16135 sd_rmw_msg_print_handler(void *arg) 16136 { 16137 struct sd_lun *un = arg; 16138 16139 ASSERT(un != NULL); 16140 ASSERT(!mutex_owned(SD_MUTEX(un))); 16141 16142 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16143 "sd_rmw_msg_print_handler: entry\n"); 16144 16145 mutex_enter(SD_MUTEX(un)); 16146 16147 if (un->un_rmw_incre_count > 0) { 16148 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16149 "%"PRIu64" I/O requests are not aligned with %d disk " 16150 "sector size in %ld seconds. They are handled through " 16151 "Read Modify Write but the performance is very low!\n", 16152 un->un_rmw_incre_count, un->un_tgt_blocksize, 16153 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000); 16154 un->un_rmw_incre_count = 0; 16155 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler, 16156 un, SD_RMW_MSG_PRINT_TIMEOUT); 16157 } else { 16158 un->un_rmw_msg_timeid = NULL; 16159 } 16160 16161 mutex_exit(SD_MUTEX(un)); 16162 16163 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16164 "sd_rmw_msg_print_handler: exit\n"); 16165 } 16166 16167 /* 16168 * Function: sd_start_direct_priority_command 16169 * 16170 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16171 * received TRAN_BUSY when we called scsi_transport() to send it 16172 * to the underlying HBA. This function is called from timeout(9F) 16173 * context after the delay interval has expired. 16174 * 16175 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16176 * 16177 * Context: timeout(9F) thread context. May not sleep. 16178 */ 16179 16180 static void 16181 sd_start_direct_priority_command(void *arg) 16182 { 16183 struct buf *priority_bp = arg; 16184 struct sd_lun *un; 16185 16186 ASSERT(priority_bp != NULL); 16187 un = SD_GET_UN(priority_bp); 16188 ASSERT(un != NULL); 16189 ASSERT(!mutex_owned(SD_MUTEX(un))); 16190 16191 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16192 "sd_start_direct_priority_command: entry\n"); 16193 16194 mutex_enter(SD_MUTEX(un)); 16195 un->un_direct_priority_timeid = NULL; 16196 sd_start_cmds(un, priority_bp); 16197 mutex_exit(SD_MUTEX(un)); 16198 16199 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16200 "sd_start_direct_priority_command: exit\n"); 16201 } 16202 16203 16204 /* 16205 * Function: sd_send_request_sense_command 16206 * 16207 * Description: Sends a REQUEST SENSE command to the target 16208 * 16209 * Context: May be called from interrupt context. 16210 */ 16211 16212 static void 16213 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16214 struct scsi_pkt *pktp) 16215 { 16216 ASSERT(bp != NULL); 16217 ASSERT(un != NULL); 16218 ASSERT(mutex_owned(SD_MUTEX(un))); 16219 16220 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16221 "entry: buf:0x%p\n", bp); 16222 16223 /* 16224 * If we are syncing or dumping, then fail the command to avoid a 16225 * recursive callback into scsi_transport(). Also fail the command 16226 * if we are suspended (legacy behavior). 16227 */ 16228 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16229 (un->un_state == SD_STATE_DUMPING)) { 16230 sd_return_failed_command(un, bp, EIO); 16231 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16232 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16233 return; 16234 } 16235 16236 /* 16237 * Retry the failed command and don't issue the request sense if: 16238 * 1) the sense buf is busy 16239 * 2) we have 1 or more outstanding commands on the target 16240 * (the sense data will be cleared or invalidated any way) 16241 * 16242 * Note: There could be an issue with not checking a retry limit here, 16243 * the problem is determining which retry limit to check. 16244 */ 16245 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16246 /* Don't retry if the command is flagged as non-retryable */ 16247 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16248 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16249 NULL, NULL, 0, un->un_busy_timeout, 16250 kstat_waitq_enter); 16251 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16252 "sd_send_request_sense_command: " 16253 "at full throttle, retrying exit\n"); 16254 } else { 16255 sd_return_failed_command(un, bp, EIO); 16256 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16257 "sd_send_request_sense_command: " 16258 "at full throttle, non-retryable exit\n"); 16259 } 16260 return; 16261 } 16262 16263 sd_mark_rqs_busy(un, bp); 16264 sd_start_cmds(un, un->un_rqs_bp); 16265 16266 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16267 "sd_send_request_sense_command: exit\n"); 16268 } 16269 16270 16271 /* 16272 * Function: sd_mark_rqs_busy 16273 * 16274 * Description: Indicate that the request sense bp for this instance is 16275 * in use. 16276 * 16277 * Context: May be called under interrupt context 16278 */ 16279 16280 static void 16281 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16282 { 16283 struct sd_xbuf *sense_xp; 16284 16285 ASSERT(un != NULL); 16286 ASSERT(bp != NULL); 16287 ASSERT(mutex_owned(SD_MUTEX(un))); 16288 ASSERT(un->un_sense_isbusy == 0); 16289 16290 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16291 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16292 16293 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16294 ASSERT(sense_xp != NULL); 16295 16296 SD_INFO(SD_LOG_IO, un, 16297 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16298 16299 ASSERT(sense_xp->xb_pktp != NULL); 16300 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16301 == (FLAG_SENSING | FLAG_HEAD)); 16302 16303 un->un_sense_isbusy = 1; 16304 un->un_rqs_bp->b_resid = 0; 16305 sense_xp->xb_pktp->pkt_resid = 0; 16306 sense_xp->xb_pktp->pkt_reason = 0; 16307 16308 /* So we can get back the bp at interrupt time! */ 16309 sense_xp->xb_sense_bp = bp; 16310 16311 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16312 16313 /* 16314 * Mark this buf as awaiting sense data. (This is already set in 16315 * the pkt_flags for the RQS packet.) 16316 */ 16317 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16318 16319 /* Request sense down same path */ 16320 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) && 16321 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance) 16322 sense_xp->xb_pktp->pkt_path_instance = 16323 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance; 16324 16325 sense_xp->xb_retry_count = 0; 16326 sense_xp->xb_victim_retry_count = 0; 16327 sense_xp->xb_ua_retry_count = 0; 16328 sense_xp->xb_nr_retry_count = 0; 16329 sense_xp->xb_dma_resid = 0; 16330 16331 /* Clean up the fields for auto-request sense */ 16332 sense_xp->xb_sense_status = 0; 16333 sense_xp->xb_sense_state = 0; 16334 sense_xp->xb_sense_resid = 0; 16335 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16336 16337 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16338 } 16339 16340 16341 /* 16342 * Function: sd_mark_rqs_idle 16343 * 16344 * Description: SD_MUTEX must be held continuously through this routine 16345 * to prevent reuse of the rqs struct before the caller can 16346 * complete it's processing. 16347 * 16348 * Return Code: Pointer to the RQS buf 16349 * 16350 * Context: May be called under interrupt context 16351 */ 16352 16353 static struct buf * 16354 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16355 { 16356 struct buf *bp; 16357 ASSERT(un != NULL); 16358 ASSERT(sense_xp != NULL); 16359 ASSERT(mutex_owned(SD_MUTEX(un))); 16360 ASSERT(un->un_sense_isbusy != 0); 16361 16362 un->un_sense_isbusy = 0; 16363 bp = sense_xp->xb_sense_bp; 16364 sense_xp->xb_sense_bp = NULL; 16365 16366 /* This pkt is no longer interested in getting sense data */ 16367 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16368 16369 return (bp); 16370 } 16371 16372 16373 16374 /* 16375 * Function: sd_alloc_rqs 16376 * 16377 * Description: Set up the unit to receive auto request sense data 16378 * 16379 * Return Code: DDI_SUCCESS or DDI_FAILURE 16380 * 16381 * Context: Called under attach(9E) context 16382 */ 16383 16384 static int 16385 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16386 { 16387 struct sd_xbuf *xp; 16388 16389 ASSERT(un != NULL); 16390 ASSERT(!mutex_owned(SD_MUTEX(un))); 16391 ASSERT(un->un_rqs_bp == NULL); 16392 ASSERT(un->un_rqs_pktp == NULL); 16393 16394 /* 16395 * First allocate the required buf and scsi_pkt structs, then set up 16396 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16397 */ 16398 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16399 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16400 if (un->un_rqs_bp == NULL) { 16401 return (DDI_FAILURE); 16402 } 16403 16404 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16405 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16406 16407 if (un->un_rqs_pktp == NULL) { 16408 sd_free_rqs(un); 16409 return (DDI_FAILURE); 16410 } 16411 16412 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16413 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16414 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 16415 16416 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16417 16418 /* Set up the other needed members in the ARQ scsi_pkt. */ 16419 un->un_rqs_pktp->pkt_comp = sdintr; 16420 un->un_rqs_pktp->pkt_time = sd_io_time; 16421 un->un_rqs_pktp->pkt_flags |= 16422 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16423 16424 /* 16425 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16426 * provide any intpkt, destroypkt routines as we take care of 16427 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16428 */ 16429 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16430 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16431 xp->xb_pktp = un->un_rqs_pktp; 16432 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16433 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16434 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16435 16436 /* 16437 * Save the pointer to the request sense private bp so it can 16438 * be retrieved in sdintr. 16439 */ 16440 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16441 ASSERT(un->un_rqs_bp->b_private == xp); 16442 16443 /* 16444 * See if the HBA supports auto-request sense for the specified 16445 * target/lun. If it does, then try to enable it (if not already 16446 * enabled). 16447 * 16448 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16449 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16450 * return success. However, in both of these cases ARQ is always 16451 * enabled and scsi_ifgetcap will always return true. The best approach 16452 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16453 * 16454 * The 3rd case is the HBA (adp) always return enabled on 16455 * scsi_ifgetgetcap even when it's not enable, the best approach 16456 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16457 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16458 */ 16459 16460 if (un->un_f_is_fibre == TRUE) { 16461 un->un_f_arq_enabled = TRUE; 16462 } else { 16463 #if defined(__i386) || defined(__amd64) 16464 /* 16465 * Circumvent the Adaptec bug, remove this code when 16466 * the bug is fixed 16467 */ 16468 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16469 #endif 16470 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16471 case 0: 16472 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16473 "sd_alloc_rqs: HBA supports ARQ\n"); 16474 /* 16475 * ARQ is supported by this HBA but currently is not 16476 * enabled. Attempt to enable it and if successful then 16477 * mark this instance as ARQ enabled. 16478 */ 16479 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16480 == 1) { 16481 /* Successfully enabled ARQ in the HBA */ 16482 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16483 "sd_alloc_rqs: ARQ enabled\n"); 16484 un->un_f_arq_enabled = TRUE; 16485 } else { 16486 /* Could not enable ARQ in the HBA */ 16487 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16488 "sd_alloc_rqs: failed ARQ enable\n"); 16489 un->un_f_arq_enabled = FALSE; 16490 } 16491 break; 16492 case 1: 16493 /* 16494 * ARQ is supported by this HBA and is already enabled. 16495 * Just mark ARQ as enabled for this instance. 16496 */ 16497 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16498 "sd_alloc_rqs: ARQ already enabled\n"); 16499 un->un_f_arq_enabled = TRUE; 16500 break; 16501 default: 16502 /* 16503 * ARQ is not supported by this HBA; disable it for this 16504 * instance. 16505 */ 16506 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16507 "sd_alloc_rqs: HBA does not support ARQ\n"); 16508 un->un_f_arq_enabled = FALSE; 16509 break; 16510 } 16511 } 16512 16513 return (DDI_SUCCESS); 16514 } 16515 16516 16517 /* 16518 * Function: sd_free_rqs 16519 * 16520 * Description: Cleanup for the pre-instance RQS command. 16521 * 16522 * Context: Kernel thread context 16523 */ 16524 16525 static void 16526 sd_free_rqs(struct sd_lun *un) 16527 { 16528 ASSERT(un != NULL); 16529 16530 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16531 16532 /* 16533 * If consistent memory is bound to a scsi_pkt, the pkt 16534 * has to be destroyed *before* freeing the consistent memory. 16535 * Don't change the sequence of this operations. 16536 * scsi_destroy_pkt() might access memory, which isn't allowed, 16537 * after it was freed in scsi_free_consistent_buf(). 16538 */ 16539 if (un->un_rqs_pktp != NULL) { 16540 scsi_destroy_pkt(un->un_rqs_pktp); 16541 un->un_rqs_pktp = NULL; 16542 } 16543 16544 if (un->un_rqs_bp != NULL) { 16545 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 16546 if (xp != NULL) { 16547 kmem_free(xp, sizeof (struct sd_xbuf)); 16548 } 16549 scsi_free_consistent_buf(un->un_rqs_bp); 16550 un->un_rqs_bp = NULL; 16551 } 16552 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16553 } 16554 16555 16556 16557 /* 16558 * Function: sd_reduce_throttle 16559 * 16560 * Description: Reduces the maximum # of outstanding commands on a 16561 * target to the current number of outstanding commands. 16562 * Queues a tiemout(9F) callback to restore the limit 16563 * after a specified interval has elapsed. 16564 * Typically used when we get a TRAN_BUSY return code 16565 * back from scsi_transport(). 16566 * 16567 * Arguments: un - ptr to the sd_lun softstate struct 16568 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16569 * 16570 * Context: May be called from interrupt context 16571 */ 16572 16573 static void 16574 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16575 { 16576 ASSERT(un != NULL); 16577 ASSERT(mutex_owned(SD_MUTEX(un))); 16578 ASSERT(un->un_ncmds_in_transport >= 0); 16579 16580 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16581 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16582 un, un->un_throttle, un->un_ncmds_in_transport); 16583 16584 if (un->un_throttle > 1) { 16585 if (un->un_f_use_adaptive_throttle == TRUE) { 16586 switch (throttle_type) { 16587 case SD_THROTTLE_TRAN_BUSY: 16588 if (un->un_busy_throttle == 0) { 16589 un->un_busy_throttle = un->un_throttle; 16590 } 16591 break; 16592 case SD_THROTTLE_QFULL: 16593 un->un_busy_throttle = 0; 16594 break; 16595 default: 16596 ASSERT(FALSE); 16597 } 16598 16599 if (un->un_ncmds_in_transport > 0) { 16600 un->un_throttle = un->un_ncmds_in_transport; 16601 } 16602 16603 } else { 16604 if (un->un_ncmds_in_transport == 0) { 16605 un->un_throttle = 1; 16606 } else { 16607 un->un_throttle = un->un_ncmds_in_transport; 16608 } 16609 } 16610 } 16611 16612 /* Reschedule the timeout if none is currently active */ 16613 if (un->un_reset_throttle_timeid == NULL) { 16614 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16615 un, SD_THROTTLE_RESET_INTERVAL); 16616 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16617 "sd_reduce_throttle: timeout scheduled!\n"); 16618 } 16619 16620 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16621 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16622 } 16623 16624 16625 16626 /* 16627 * Function: sd_restore_throttle 16628 * 16629 * Description: Callback function for timeout(9F). Resets the current 16630 * value of un->un_throttle to its default. 16631 * 16632 * Arguments: arg - pointer to associated softstate for the device. 16633 * 16634 * Context: May be called from interrupt context 16635 */ 16636 16637 static void 16638 sd_restore_throttle(void *arg) 16639 { 16640 struct sd_lun *un = arg; 16641 16642 ASSERT(un != NULL); 16643 ASSERT(!mutex_owned(SD_MUTEX(un))); 16644 16645 mutex_enter(SD_MUTEX(un)); 16646 16647 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16648 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16649 16650 un->un_reset_throttle_timeid = NULL; 16651 16652 if (un->un_f_use_adaptive_throttle == TRUE) { 16653 /* 16654 * If un_busy_throttle is nonzero, then it contains the 16655 * value that un_throttle was when we got a TRAN_BUSY back 16656 * from scsi_transport(). We want to revert back to this 16657 * value. 16658 * 16659 * In the QFULL case, the throttle limit will incrementally 16660 * increase until it reaches max throttle. 16661 */ 16662 if (un->un_busy_throttle > 0) { 16663 un->un_throttle = un->un_busy_throttle; 16664 un->un_busy_throttle = 0; 16665 } else { 16666 /* 16667 * increase throttle by 10% open gate slowly, schedule 16668 * another restore if saved throttle has not been 16669 * reached 16670 */ 16671 short throttle; 16672 if (sd_qfull_throttle_enable) { 16673 throttle = un->un_throttle + 16674 max((un->un_throttle / 10), 1); 16675 un->un_throttle = 16676 (throttle < un->un_saved_throttle) ? 16677 throttle : un->un_saved_throttle; 16678 if (un->un_throttle < un->un_saved_throttle) { 16679 un->un_reset_throttle_timeid = 16680 timeout(sd_restore_throttle, 16681 un, 16682 SD_QFULL_THROTTLE_RESET_INTERVAL); 16683 } 16684 } 16685 } 16686 16687 /* 16688 * If un_throttle has fallen below the low-water mark, we 16689 * restore the maximum value here (and allow it to ratchet 16690 * down again if necessary). 16691 */ 16692 if (un->un_throttle < un->un_min_throttle) { 16693 un->un_throttle = un->un_saved_throttle; 16694 } 16695 } else { 16696 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16697 "restoring limit from 0x%x to 0x%x\n", 16698 un->un_throttle, un->un_saved_throttle); 16699 un->un_throttle = un->un_saved_throttle; 16700 } 16701 16702 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16703 "sd_restore_throttle: calling sd_start_cmds!\n"); 16704 16705 sd_start_cmds(un, NULL); 16706 16707 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16708 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16709 un, un->un_throttle); 16710 16711 mutex_exit(SD_MUTEX(un)); 16712 16713 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16714 } 16715 16716 /* 16717 * Function: sdrunout 16718 * 16719 * Description: Callback routine for scsi_init_pkt when a resource allocation 16720 * fails. 16721 * 16722 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16723 * soft state instance. 16724 * 16725 * Return Code: The scsi_init_pkt routine allows for the callback function to 16726 * return a 0 indicating the callback should be rescheduled or a 1 16727 * indicating not to reschedule. This routine always returns 1 16728 * because the driver always provides a callback function to 16729 * scsi_init_pkt. This results in a callback always being scheduled 16730 * (via the scsi_init_pkt callback implementation) if a resource 16731 * failure occurs. 16732 * 16733 * Context: This callback function may not block or call routines that block 16734 * 16735 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16736 * request persisting at the head of the list which cannot be 16737 * satisfied even after multiple retries. In the future the driver 16738 * may implement some time of maximum runout count before failing 16739 * an I/O. 16740 */ 16741 16742 static int 16743 sdrunout(caddr_t arg) 16744 { 16745 struct sd_lun *un = (struct sd_lun *)arg; 16746 16747 ASSERT(un != NULL); 16748 ASSERT(!mutex_owned(SD_MUTEX(un))); 16749 16750 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16751 16752 mutex_enter(SD_MUTEX(un)); 16753 sd_start_cmds(un, NULL); 16754 mutex_exit(SD_MUTEX(un)); 16755 /* 16756 * This callback routine always returns 1 (i.e. do not reschedule) 16757 * because we always specify sdrunout as the callback handler for 16758 * scsi_init_pkt inside the call to sd_start_cmds. 16759 */ 16760 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16761 return (1); 16762 } 16763 16764 16765 /* 16766 * Function: sdintr 16767 * 16768 * Description: Completion callback routine for scsi_pkt(9S) structs 16769 * sent to the HBA driver via scsi_transport(9F). 16770 * 16771 * Context: Interrupt context 16772 */ 16773 16774 static void 16775 sdintr(struct scsi_pkt *pktp) 16776 { 16777 struct buf *bp; 16778 struct sd_xbuf *xp; 16779 struct sd_lun *un; 16780 size_t actual_len; 16781 sd_ssc_t *sscp; 16782 16783 ASSERT(pktp != NULL); 16784 bp = (struct buf *)pktp->pkt_private; 16785 ASSERT(bp != NULL); 16786 xp = SD_GET_XBUF(bp); 16787 ASSERT(xp != NULL); 16788 ASSERT(xp->xb_pktp != NULL); 16789 un = SD_GET_UN(bp); 16790 ASSERT(un != NULL); 16791 ASSERT(!mutex_owned(SD_MUTEX(un))); 16792 16793 #ifdef SD_FAULT_INJECTION 16794 16795 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16796 /* SD FaultInjection */ 16797 sd_faultinjection(pktp); 16798 16799 #endif /* SD_FAULT_INJECTION */ 16800 16801 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16802 " xp:0x%p, un:0x%p\n", bp, xp, un); 16803 16804 mutex_enter(SD_MUTEX(un)); 16805 16806 ASSERT(un->un_fm_private != NULL); 16807 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 16808 ASSERT(sscp != NULL); 16809 16810 /* Reduce the count of the #commands currently in transport */ 16811 un->un_ncmds_in_transport--; 16812 ASSERT(un->un_ncmds_in_transport >= 0); 16813 16814 /* Increment counter to indicate that the callback routine is active */ 16815 un->un_in_callback++; 16816 16817 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16818 16819 #ifdef SDDEBUG 16820 if (bp == un->un_retry_bp) { 16821 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16822 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16823 un, un->un_retry_bp, un->un_ncmds_in_transport); 16824 } 16825 #endif 16826 16827 /* 16828 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 16829 * state if needed. 16830 */ 16831 if (pktp->pkt_reason == CMD_DEV_GONE) { 16832 /* Prevent multiple console messages for the same failure. */ 16833 if (un->un_last_pkt_reason != CMD_DEV_GONE) { 16834 un->un_last_pkt_reason = CMD_DEV_GONE; 16835 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16836 "Command failed to complete...Device is gone\n"); 16837 } 16838 if (un->un_mediastate != DKIO_DEV_GONE) { 16839 un->un_mediastate = DKIO_DEV_GONE; 16840 cv_broadcast(&un->un_state_cv); 16841 } 16842 /* 16843 * If the command happens to be the REQUEST SENSE command, 16844 * free up the rqs buf and fail the original command. 16845 */ 16846 if (bp == un->un_rqs_bp) { 16847 bp = sd_mark_rqs_idle(un, xp); 16848 } 16849 sd_return_failed_command(un, bp, EIO); 16850 goto exit; 16851 } 16852 16853 if (pktp->pkt_state & STATE_XARQ_DONE) { 16854 SD_TRACE(SD_LOG_COMMON, un, 16855 "sdintr: extra sense data received. pkt=%p\n", pktp); 16856 } 16857 16858 /* 16859 * First see if the pkt has auto-request sense data with it.... 16860 * Look at the packet state first so we don't take a performance 16861 * hit looking at the arq enabled flag unless absolutely necessary. 16862 */ 16863 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16864 (un->un_f_arq_enabled == TRUE)) { 16865 /* 16866 * The HBA did an auto request sense for this command so check 16867 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16868 * driver command that should not be retried. 16869 */ 16870 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16871 /* 16872 * Save the relevant sense info into the xp for the 16873 * original cmd. 16874 */ 16875 struct scsi_arq_status *asp; 16876 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16877 xp->xb_sense_status = 16878 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16879 xp->xb_sense_state = asp->sts_rqpkt_state; 16880 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16881 if (pktp->pkt_state & STATE_XARQ_DONE) { 16882 actual_len = MAX_SENSE_LENGTH - 16883 xp->xb_sense_resid; 16884 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16885 MAX_SENSE_LENGTH); 16886 } else { 16887 if (xp->xb_sense_resid > SENSE_LENGTH) { 16888 actual_len = MAX_SENSE_LENGTH - 16889 xp->xb_sense_resid; 16890 } else { 16891 actual_len = SENSE_LENGTH - 16892 xp->xb_sense_resid; 16893 } 16894 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16895 if ((((struct uscsi_cmd *) 16896 (xp->xb_pktinfo))->uscsi_rqlen) > 16897 actual_len) { 16898 xp->xb_sense_resid = 16899 (((struct uscsi_cmd *) 16900 (xp->xb_pktinfo))-> 16901 uscsi_rqlen) - actual_len; 16902 } else { 16903 xp->xb_sense_resid = 0; 16904 } 16905 } 16906 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16907 SENSE_LENGTH); 16908 } 16909 16910 /* fail the command */ 16911 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16912 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16913 sd_return_failed_command(un, bp, EIO); 16914 goto exit; 16915 } 16916 16917 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16918 /* 16919 * We want to either retry or fail this command, so free 16920 * the DMA resources here. If we retry the command then 16921 * the DMA resources will be reallocated in sd_start_cmds(). 16922 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16923 * causes the *entire* transfer to start over again from the 16924 * beginning of the request, even for PARTIAL chunks that 16925 * have already transferred successfully. 16926 */ 16927 if ((un->un_f_is_fibre == TRUE) && 16928 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16929 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16930 scsi_dmafree(pktp); 16931 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16932 } 16933 #endif 16934 16935 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16936 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16937 16938 sd_handle_auto_request_sense(un, bp, xp, pktp); 16939 goto exit; 16940 } 16941 16942 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16943 if (pktp->pkt_flags & FLAG_SENSING) { 16944 /* This pktp is from the unit's REQUEST_SENSE command */ 16945 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16946 "sdintr: sd_handle_request_sense\n"); 16947 sd_handle_request_sense(un, bp, xp, pktp); 16948 goto exit; 16949 } 16950 16951 /* 16952 * Check to see if the command successfully completed as requested; 16953 * this is the most common case (and also the hot performance path). 16954 * 16955 * Requirements for successful completion are: 16956 * pkt_reason is CMD_CMPLT and packet status is status good. 16957 * In addition: 16958 * - A residual of zero indicates successful completion no matter what 16959 * the command is. 16960 * - If the residual is not zero and the command is not a read or 16961 * write, then it's still defined as successful completion. In other 16962 * words, if the command is a read or write the residual must be 16963 * zero for successful completion. 16964 * - If the residual is not zero and the command is a read or 16965 * write, and it's a USCSICMD, then it's still defined as 16966 * successful completion. 16967 */ 16968 if ((pktp->pkt_reason == CMD_CMPLT) && 16969 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16970 16971 /* 16972 * Since this command is returned with a good status, we 16973 * can reset the count for Sonoma failover. 16974 */ 16975 un->un_sonoma_failure_count = 0; 16976 16977 /* 16978 * Return all USCSI commands on good status 16979 */ 16980 if (pktp->pkt_resid == 0) { 16981 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16982 "sdintr: returning command for resid == 0\n"); 16983 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16984 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16985 SD_UPDATE_B_RESID(bp, pktp); 16986 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16987 "sdintr: returning command for resid != 0\n"); 16988 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16989 SD_UPDATE_B_RESID(bp, pktp); 16990 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16991 "sdintr: returning uscsi command\n"); 16992 } else { 16993 goto not_successful; 16994 } 16995 sd_return_command(un, bp); 16996 16997 /* 16998 * Decrement counter to indicate that the callback routine 16999 * is done. 17000 */ 17001 un->un_in_callback--; 17002 ASSERT(un->un_in_callback >= 0); 17003 mutex_exit(SD_MUTEX(un)); 17004 17005 return; 17006 } 17007 17008 not_successful: 17009 17010 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 17011 /* 17012 * The following is based upon knowledge of the underlying transport 17013 * and its use of DMA resources. This code should be removed when 17014 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 17015 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 17016 * and sd_start_cmds(). 17017 * 17018 * Free any DMA resources associated with this command if there 17019 * is a chance it could be retried or enqueued for later retry. 17020 * If we keep the DMA binding then mpxio cannot reissue the 17021 * command on another path whenever a path failure occurs. 17022 * 17023 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 17024 * causes the *entire* transfer to start over again from the 17025 * beginning of the request, even for PARTIAL chunks that 17026 * have already transferred successfully. 17027 * 17028 * This is only done for non-uscsi commands (and also skipped for the 17029 * driver's internal RQS command). Also just do this for Fibre Channel 17030 * devices as these are the only ones that support mpxio. 17031 */ 17032 if ((un->un_f_is_fibre == TRUE) && 17033 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 17034 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 17035 scsi_dmafree(pktp); 17036 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 17037 } 17038 #endif 17039 17040 /* 17041 * The command did not successfully complete as requested so check 17042 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 17043 * driver command that should not be retried so just return. If 17044 * FLAG_DIAGNOSE is not set the error will be processed below. 17045 */ 17046 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 17047 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17048 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 17049 /* 17050 * Issue a request sense if a check condition caused the error 17051 * (we handle the auto request sense case above), otherwise 17052 * just fail the command. 17053 */ 17054 if ((pktp->pkt_reason == CMD_CMPLT) && 17055 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 17056 sd_send_request_sense_command(un, bp, pktp); 17057 } else { 17058 sd_return_failed_command(un, bp, EIO); 17059 } 17060 goto exit; 17061 } 17062 17063 /* 17064 * The command did not successfully complete as requested so process 17065 * the error, retry, and/or attempt recovery. 17066 */ 17067 switch (pktp->pkt_reason) { 17068 case CMD_CMPLT: 17069 switch (SD_GET_PKT_STATUS(pktp)) { 17070 case STATUS_GOOD: 17071 /* 17072 * The command completed successfully with a non-zero 17073 * residual 17074 */ 17075 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17076 "sdintr: STATUS_GOOD \n"); 17077 sd_pkt_status_good(un, bp, xp, pktp); 17078 break; 17079 17080 case STATUS_CHECK: 17081 case STATUS_TERMINATED: 17082 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17083 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 17084 sd_pkt_status_check_condition(un, bp, xp, pktp); 17085 break; 17086 17087 case STATUS_BUSY: 17088 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17089 "sdintr: STATUS_BUSY\n"); 17090 sd_pkt_status_busy(un, bp, xp, pktp); 17091 break; 17092 17093 case STATUS_RESERVATION_CONFLICT: 17094 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17095 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 17096 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17097 break; 17098 17099 case STATUS_QFULL: 17100 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17101 "sdintr: STATUS_QFULL\n"); 17102 sd_pkt_status_qfull(un, bp, xp, pktp); 17103 break; 17104 17105 case STATUS_MET: 17106 case STATUS_INTERMEDIATE: 17107 case STATUS_SCSI2: 17108 case STATUS_INTERMEDIATE_MET: 17109 case STATUS_ACA_ACTIVE: 17110 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17111 "Unexpected SCSI status received: 0x%x\n", 17112 SD_GET_PKT_STATUS(pktp)); 17113 /* 17114 * Mark the ssc_flags when detected invalid status 17115 * code for non-USCSI command. 17116 */ 17117 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17118 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17119 0, "stat-code"); 17120 } 17121 sd_return_failed_command(un, bp, EIO); 17122 break; 17123 17124 default: 17125 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17126 "Invalid SCSI status received: 0x%x\n", 17127 SD_GET_PKT_STATUS(pktp)); 17128 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17129 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17130 0, "stat-code"); 17131 } 17132 sd_return_failed_command(un, bp, EIO); 17133 break; 17134 17135 } 17136 break; 17137 17138 case CMD_INCOMPLETE: 17139 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17140 "sdintr: CMD_INCOMPLETE\n"); 17141 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 17142 break; 17143 case CMD_TRAN_ERR: 17144 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17145 "sdintr: CMD_TRAN_ERR\n"); 17146 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 17147 break; 17148 case CMD_RESET: 17149 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17150 "sdintr: CMD_RESET \n"); 17151 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 17152 break; 17153 case CMD_ABORTED: 17154 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17155 "sdintr: CMD_ABORTED \n"); 17156 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 17157 break; 17158 case CMD_TIMEOUT: 17159 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17160 "sdintr: CMD_TIMEOUT\n"); 17161 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 17162 break; 17163 case CMD_UNX_BUS_FREE: 17164 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17165 "sdintr: CMD_UNX_BUS_FREE \n"); 17166 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 17167 break; 17168 case CMD_TAG_REJECT: 17169 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17170 "sdintr: CMD_TAG_REJECT\n"); 17171 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 17172 break; 17173 default: 17174 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17175 "sdintr: default\n"); 17176 /* 17177 * Mark the ssc_flags for detecting invliad pkt_reason. 17178 */ 17179 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17180 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON, 17181 0, "pkt-reason"); 17182 } 17183 sd_pkt_reason_default(un, bp, xp, pktp); 17184 break; 17185 } 17186 17187 exit: 17188 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 17189 17190 /* Decrement counter to indicate that the callback routine is done. */ 17191 un->un_in_callback--; 17192 ASSERT(un->un_in_callback >= 0); 17193 17194 /* 17195 * At this point, the pkt has been dispatched, ie, it is either 17196 * being re-tried or has been returned to its caller and should 17197 * not be referenced. 17198 */ 17199 17200 mutex_exit(SD_MUTEX(un)); 17201 } 17202 17203 17204 /* 17205 * Function: sd_print_incomplete_msg 17206 * 17207 * Description: Prints the error message for a CMD_INCOMPLETE error. 17208 * 17209 * Arguments: un - ptr to associated softstate for the device. 17210 * bp - ptr to the buf(9S) for the command. 17211 * arg - message string ptr 17212 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17213 * or SD_NO_RETRY_ISSUED. 17214 * 17215 * Context: May be called under interrupt context 17216 */ 17217 17218 static void 17219 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17220 { 17221 struct scsi_pkt *pktp; 17222 char *msgp; 17223 char *cmdp = arg; 17224 17225 ASSERT(un != NULL); 17226 ASSERT(mutex_owned(SD_MUTEX(un))); 17227 ASSERT(bp != NULL); 17228 ASSERT(arg != NULL); 17229 pktp = SD_GET_PKTP(bp); 17230 ASSERT(pktp != NULL); 17231 17232 switch (code) { 17233 case SD_DELAYED_RETRY_ISSUED: 17234 case SD_IMMEDIATE_RETRY_ISSUED: 17235 msgp = "retrying"; 17236 break; 17237 case SD_NO_RETRY_ISSUED: 17238 default: 17239 msgp = "giving up"; 17240 break; 17241 } 17242 17243 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17244 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17245 "incomplete %s- %s\n", cmdp, msgp); 17246 } 17247 } 17248 17249 17250 17251 /* 17252 * Function: sd_pkt_status_good 17253 * 17254 * Description: Processing for a STATUS_GOOD code in pkt_status. 17255 * 17256 * Context: May be called under interrupt context 17257 */ 17258 17259 static void 17260 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17261 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17262 { 17263 char *cmdp; 17264 17265 ASSERT(un != NULL); 17266 ASSERT(mutex_owned(SD_MUTEX(un))); 17267 ASSERT(bp != NULL); 17268 ASSERT(xp != NULL); 17269 ASSERT(pktp != NULL); 17270 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17271 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17272 ASSERT(pktp->pkt_resid != 0); 17273 17274 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17275 17276 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17277 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17278 case SCMD_READ: 17279 cmdp = "read"; 17280 break; 17281 case SCMD_WRITE: 17282 cmdp = "write"; 17283 break; 17284 default: 17285 SD_UPDATE_B_RESID(bp, pktp); 17286 sd_return_command(un, bp); 17287 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17288 return; 17289 } 17290 17291 /* 17292 * See if we can retry the read/write, preferrably immediately. 17293 * If retries are exhaused, then sd_retry_command() will update 17294 * the b_resid count. 17295 */ 17296 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17297 cmdp, EIO, (clock_t)0, NULL); 17298 17299 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17300 } 17301 17302 17303 17304 17305 17306 /* 17307 * Function: sd_handle_request_sense 17308 * 17309 * Description: Processing for non-auto Request Sense command. 17310 * 17311 * Arguments: un - ptr to associated softstate 17312 * sense_bp - ptr to buf(9S) for the RQS command 17313 * sense_xp - ptr to the sd_xbuf for the RQS command 17314 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17315 * 17316 * Context: May be called under interrupt context 17317 */ 17318 17319 static void 17320 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17321 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17322 { 17323 struct buf *cmd_bp; /* buf for the original command */ 17324 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17325 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17326 size_t actual_len; /* actual sense data length */ 17327 17328 ASSERT(un != NULL); 17329 ASSERT(mutex_owned(SD_MUTEX(un))); 17330 ASSERT(sense_bp != NULL); 17331 ASSERT(sense_xp != NULL); 17332 ASSERT(sense_pktp != NULL); 17333 17334 /* 17335 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17336 * RQS command and not the original command. 17337 */ 17338 ASSERT(sense_pktp == un->un_rqs_pktp); 17339 ASSERT(sense_bp == un->un_rqs_bp); 17340 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17341 (FLAG_SENSING | FLAG_HEAD)); 17342 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17343 FLAG_SENSING) == FLAG_SENSING); 17344 17345 /* These are the bp, xp, and pktp for the original command */ 17346 cmd_bp = sense_xp->xb_sense_bp; 17347 cmd_xp = SD_GET_XBUF(cmd_bp); 17348 cmd_pktp = SD_GET_PKTP(cmd_bp); 17349 17350 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17351 /* 17352 * The REQUEST SENSE command failed. Release the REQUEST 17353 * SENSE command for re-use, get back the bp for the original 17354 * command, and attempt to re-try the original command if 17355 * FLAG_DIAGNOSE is not set in the original packet. 17356 */ 17357 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17358 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17359 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17360 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17361 NULL, NULL, EIO, (clock_t)0, NULL); 17362 return; 17363 } 17364 } 17365 17366 /* 17367 * Save the relevant sense info into the xp for the original cmd. 17368 * 17369 * Note: if the request sense failed the state info will be zero 17370 * as set in sd_mark_rqs_busy() 17371 */ 17372 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17373 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17374 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 17375 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 17376 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 17377 SENSE_LENGTH)) { 17378 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17379 MAX_SENSE_LENGTH); 17380 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17381 } else { 17382 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17383 SENSE_LENGTH); 17384 if (actual_len < SENSE_LENGTH) { 17385 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 17386 } else { 17387 cmd_xp->xb_sense_resid = 0; 17388 } 17389 } 17390 17391 /* 17392 * Free up the RQS command.... 17393 * NOTE: 17394 * Must do this BEFORE calling sd_validate_sense_data! 17395 * sd_validate_sense_data may return the original command in 17396 * which case the pkt will be freed and the flags can no 17397 * longer be touched. 17398 * SD_MUTEX is held through this process until the command 17399 * is dispatched based upon the sense data, so there are 17400 * no race conditions. 17401 */ 17402 (void) sd_mark_rqs_idle(un, sense_xp); 17403 17404 /* 17405 * For a retryable command see if we have valid sense data, if so then 17406 * turn it over to sd_decode_sense() to figure out the right course of 17407 * action. Just fail a non-retryable command. 17408 */ 17409 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17410 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 17411 SD_SENSE_DATA_IS_VALID) { 17412 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17413 } 17414 } else { 17415 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17416 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17417 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17418 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17419 sd_return_failed_command(un, cmd_bp, EIO); 17420 } 17421 } 17422 17423 17424 17425 17426 /* 17427 * Function: sd_handle_auto_request_sense 17428 * 17429 * Description: Processing for auto-request sense information. 17430 * 17431 * Arguments: un - ptr to associated softstate 17432 * bp - ptr to buf(9S) for the command 17433 * xp - ptr to the sd_xbuf for the command 17434 * pktp - ptr to the scsi_pkt(9S) for the command 17435 * 17436 * Context: May be called under interrupt context 17437 */ 17438 17439 static void 17440 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17441 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17442 { 17443 struct scsi_arq_status *asp; 17444 size_t actual_len; 17445 17446 ASSERT(un != NULL); 17447 ASSERT(mutex_owned(SD_MUTEX(un))); 17448 ASSERT(bp != NULL); 17449 ASSERT(xp != NULL); 17450 ASSERT(pktp != NULL); 17451 ASSERT(pktp != un->un_rqs_pktp); 17452 ASSERT(bp != un->un_rqs_bp); 17453 17454 /* 17455 * For auto-request sense, we get a scsi_arq_status back from 17456 * the HBA, with the sense data in the sts_sensedata member. 17457 * The pkt_scbp of the packet points to this scsi_arq_status. 17458 */ 17459 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17460 17461 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17462 /* 17463 * The auto REQUEST SENSE failed; see if we can re-try 17464 * the original command. 17465 */ 17466 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17467 "auto request sense failed (reason=%s)\n", 17468 scsi_rname(asp->sts_rqpkt_reason)); 17469 17470 sd_reset_target(un, pktp); 17471 17472 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17473 NULL, NULL, EIO, (clock_t)0, NULL); 17474 return; 17475 } 17476 17477 /* Save the relevant sense info into the xp for the original cmd. */ 17478 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17479 xp->xb_sense_state = asp->sts_rqpkt_state; 17480 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17481 if (xp->xb_sense_state & STATE_XARQ_DONE) { 17482 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17483 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17484 MAX_SENSE_LENGTH); 17485 } else { 17486 if (xp->xb_sense_resid > SENSE_LENGTH) { 17487 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17488 } else { 17489 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 17490 } 17491 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 17492 if ((((struct uscsi_cmd *) 17493 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) { 17494 xp->xb_sense_resid = (((struct uscsi_cmd *) 17495 (xp->xb_pktinfo))->uscsi_rqlen) - 17496 actual_len; 17497 } else { 17498 xp->xb_sense_resid = 0; 17499 } 17500 } 17501 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 17502 } 17503 17504 /* 17505 * See if we have valid sense data, if so then turn it over to 17506 * sd_decode_sense() to figure out the right course of action. 17507 */ 17508 if (sd_validate_sense_data(un, bp, xp, actual_len) == 17509 SD_SENSE_DATA_IS_VALID) { 17510 sd_decode_sense(un, bp, xp, pktp); 17511 } 17512 } 17513 17514 17515 /* 17516 * Function: sd_print_sense_failed_msg 17517 * 17518 * Description: Print log message when RQS has failed. 17519 * 17520 * Arguments: un - ptr to associated softstate 17521 * bp - ptr to buf(9S) for the command 17522 * arg - generic message string ptr 17523 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17524 * or SD_NO_RETRY_ISSUED 17525 * 17526 * Context: May be called from interrupt context 17527 */ 17528 17529 static void 17530 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17531 int code) 17532 { 17533 char *msgp = arg; 17534 17535 ASSERT(un != NULL); 17536 ASSERT(mutex_owned(SD_MUTEX(un))); 17537 ASSERT(bp != NULL); 17538 17539 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17540 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17541 } 17542 } 17543 17544 17545 /* 17546 * Function: sd_validate_sense_data 17547 * 17548 * Description: Check the given sense data for validity. 17549 * If the sense data is not valid, the command will 17550 * be either failed or retried! 17551 * 17552 * Return Code: SD_SENSE_DATA_IS_INVALID 17553 * SD_SENSE_DATA_IS_VALID 17554 * 17555 * Context: May be called from interrupt context 17556 */ 17557 17558 static int 17559 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17560 size_t actual_len) 17561 { 17562 struct scsi_extended_sense *esp; 17563 struct scsi_pkt *pktp; 17564 char *msgp = NULL; 17565 sd_ssc_t *sscp; 17566 17567 ASSERT(un != NULL); 17568 ASSERT(mutex_owned(SD_MUTEX(un))); 17569 ASSERT(bp != NULL); 17570 ASSERT(bp != un->un_rqs_bp); 17571 ASSERT(xp != NULL); 17572 ASSERT(un->un_fm_private != NULL); 17573 17574 pktp = SD_GET_PKTP(bp); 17575 ASSERT(pktp != NULL); 17576 17577 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 17578 ASSERT(sscp != NULL); 17579 17580 /* 17581 * Check the status of the RQS command (auto or manual). 17582 */ 17583 switch (xp->xb_sense_status & STATUS_MASK) { 17584 case STATUS_GOOD: 17585 break; 17586 17587 case STATUS_RESERVATION_CONFLICT: 17588 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17589 return (SD_SENSE_DATA_IS_INVALID); 17590 17591 case STATUS_BUSY: 17592 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17593 "Busy Status on REQUEST SENSE\n"); 17594 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17595 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17596 return (SD_SENSE_DATA_IS_INVALID); 17597 17598 case STATUS_QFULL: 17599 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17600 "QFULL Status on REQUEST SENSE\n"); 17601 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17602 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17603 return (SD_SENSE_DATA_IS_INVALID); 17604 17605 case STATUS_CHECK: 17606 case STATUS_TERMINATED: 17607 msgp = "Check Condition on REQUEST SENSE\n"; 17608 goto sense_failed; 17609 17610 default: 17611 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17612 goto sense_failed; 17613 } 17614 17615 /* 17616 * See if we got the minimum required amount of sense data. 17617 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17618 * or less. 17619 */ 17620 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17621 (actual_len == 0)) { 17622 msgp = "Request Sense couldn't get sense data\n"; 17623 goto sense_failed; 17624 } 17625 17626 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17627 msgp = "Not enough sense information\n"; 17628 /* Mark the ssc_flags for detecting invalid sense data */ 17629 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17630 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17631 "sense-data"); 17632 } 17633 goto sense_failed; 17634 } 17635 17636 /* 17637 * We require the extended sense data 17638 */ 17639 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17640 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17641 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17642 static char tmp[8]; 17643 static char buf[148]; 17644 char *p = (char *)(xp->xb_sense_data); 17645 int i; 17646 17647 mutex_enter(&sd_sense_mutex); 17648 (void) strcpy(buf, "undecodable sense information:"); 17649 for (i = 0; i < actual_len; i++) { 17650 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17651 (void) strcpy(&buf[strlen(buf)], tmp); 17652 } 17653 i = strlen(buf); 17654 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17655 17656 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 17657 scsi_log(SD_DEVINFO(un), sd_label, 17658 CE_WARN, buf); 17659 } 17660 mutex_exit(&sd_sense_mutex); 17661 } 17662 17663 /* Mark the ssc_flags for detecting invalid sense data */ 17664 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17665 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17666 "sense-data"); 17667 } 17668 17669 /* Note: Legacy behavior, fail the command with no retry */ 17670 sd_return_failed_command(un, bp, EIO); 17671 return (SD_SENSE_DATA_IS_INVALID); 17672 } 17673 17674 /* 17675 * Check that es_code is valid (es_class concatenated with es_code 17676 * make up the "response code" field. es_class will always be 7, so 17677 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17678 * format. 17679 */ 17680 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17681 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17682 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17683 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17684 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17685 /* Mark the ssc_flags for detecting invalid sense data */ 17686 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17687 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17688 "sense-data"); 17689 } 17690 goto sense_failed; 17691 } 17692 17693 return (SD_SENSE_DATA_IS_VALID); 17694 17695 sense_failed: 17696 /* 17697 * If the request sense failed (for whatever reason), attempt 17698 * to retry the original command. 17699 */ 17700 #if defined(__i386) || defined(__amd64) 17701 /* 17702 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17703 * sddef.h for Sparc platform, and x86 uses 1 binary 17704 * for both SCSI/FC. 17705 * The SD_RETRY_DELAY value need to be adjusted here 17706 * when SD_RETRY_DELAY change in sddef.h 17707 */ 17708 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17709 sd_print_sense_failed_msg, msgp, EIO, 17710 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17711 #else 17712 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17713 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17714 #endif 17715 17716 return (SD_SENSE_DATA_IS_INVALID); 17717 } 17718 17719 /* 17720 * Function: sd_decode_sense 17721 * 17722 * Description: Take recovery action(s) when SCSI Sense Data is received. 17723 * 17724 * Context: Interrupt context. 17725 */ 17726 17727 static void 17728 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17729 struct scsi_pkt *pktp) 17730 { 17731 uint8_t sense_key; 17732 17733 ASSERT(un != NULL); 17734 ASSERT(mutex_owned(SD_MUTEX(un))); 17735 ASSERT(bp != NULL); 17736 ASSERT(bp != un->un_rqs_bp); 17737 ASSERT(xp != NULL); 17738 ASSERT(pktp != NULL); 17739 17740 sense_key = scsi_sense_key(xp->xb_sense_data); 17741 17742 switch (sense_key) { 17743 case KEY_NO_SENSE: 17744 sd_sense_key_no_sense(un, bp, xp, pktp); 17745 break; 17746 case KEY_RECOVERABLE_ERROR: 17747 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17748 bp, xp, pktp); 17749 break; 17750 case KEY_NOT_READY: 17751 sd_sense_key_not_ready(un, xp->xb_sense_data, 17752 bp, xp, pktp); 17753 break; 17754 case KEY_MEDIUM_ERROR: 17755 case KEY_HARDWARE_ERROR: 17756 sd_sense_key_medium_or_hardware_error(un, 17757 xp->xb_sense_data, bp, xp, pktp); 17758 break; 17759 case KEY_ILLEGAL_REQUEST: 17760 sd_sense_key_illegal_request(un, bp, xp, pktp); 17761 break; 17762 case KEY_UNIT_ATTENTION: 17763 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17764 bp, xp, pktp); 17765 break; 17766 case KEY_WRITE_PROTECT: 17767 case KEY_VOLUME_OVERFLOW: 17768 case KEY_MISCOMPARE: 17769 sd_sense_key_fail_command(un, bp, xp, pktp); 17770 break; 17771 case KEY_BLANK_CHECK: 17772 sd_sense_key_blank_check(un, bp, xp, pktp); 17773 break; 17774 case KEY_ABORTED_COMMAND: 17775 sd_sense_key_aborted_command(un, bp, xp, pktp); 17776 break; 17777 case KEY_VENDOR_UNIQUE: 17778 case KEY_COPY_ABORTED: 17779 case KEY_EQUAL: 17780 case KEY_RESERVED: 17781 default: 17782 sd_sense_key_default(un, xp->xb_sense_data, 17783 bp, xp, pktp); 17784 break; 17785 } 17786 } 17787 17788 17789 /* 17790 * Function: sd_dump_memory 17791 * 17792 * Description: Debug logging routine to print the contents of a user provided 17793 * buffer. The output of the buffer is broken up into 256 byte 17794 * segments due to a size constraint of the scsi_log. 17795 * implementation. 17796 * 17797 * Arguments: un - ptr to softstate 17798 * comp - component mask 17799 * title - "title" string to preceed data when printed 17800 * data - ptr to data block to be printed 17801 * len - size of data block to be printed 17802 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17803 * 17804 * Context: May be called from interrupt context 17805 */ 17806 17807 #define SD_DUMP_MEMORY_BUF_SIZE 256 17808 17809 static char *sd_dump_format_string[] = { 17810 " 0x%02x", 17811 " %c" 17812 }; 17813 17814 static void 17815 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17816 int len, int fmt) 17817 { 17818 int i, j; 17819 int avail_count; 17820 int start_offset; 17821 int end_offset; 17822 size_t entry_len; 17823 char *bufp; 17824 char *local_buf; 17825 char *format_string; 17826 17827 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17828 17829 /* 17830 * In the debug version of the driver, this function is called from a 17831 * number of places which are NOPs in the release driver. 17832 * The debug driver therefore has additional methods of filtering 17833 * debug output. 17834 */ 17835 #ifdef SDDEBUG 17836 /* 17837 * In the debug version of the driver we can reduce the amount of debug 17838 * messages by setting sd_error_level to something other than 17839 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17840 * sd_component_mask. 17841 */ 17842 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17843 (sd_error_level != SCSI_ERR_ALL)) { 17844 return; 17845 } 17846 if (((sd_component_mask & comp) == 0) || 17847 (sd_error_level != SCSI_ERR_ALL)) { 17848 return; 17849 } 17850 #else 17851 if (sd_error_level != SCSI_ERR_ALL) { 17852 return; 17853 } 17854 #endif 17855 17856 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17857 bufp = local_buf; 17858 /* 17859 * Available length is the length of local_buf[], minus the 17860 * length of the title string, minus one for the ":", minus 17861 * one for the newline, minus one for the NULL terminator. 17862 * This gives the #bytes available for holding the printed 17863 * values from the given data buffer. 17864 */ 17865 if (fmt == SD_LOG_HEX) { 17866 format_string = sd_dump_format_string[0]; 17867 } else /* SD_LOG_CHAR */ { 17868 format_string = sd_dump_format_string[1]; 17869 } 17870 /* 17871 * Available count is the number of elements from the given 17872 * data buffer that we can fit into the available length. 17873 * This is based upon the size of the format string used. 17874 * Make one entry and find it's size. 17875 */ 17876 (void) sprintf(bufp, format_string, data[0]); 17877 entry_len = strlen(bufp); 17878 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17879 17880 j = 0; 17881 while (j < len) { 17882 bufp = local_buf; 17883 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17884 start_offset = j; 17885 17886 end_offset = start_offset + avail_count; 17887 17888 (void) sprintf(bufp, "%s:", title); 17889 bufp += strlen(bufp); 17890 for (i = start_offset; ((i < end_offset) && (j < len)); 17891 i++, j++) { 17892 (void) sprintf(bufp, format_string, data[i]); 17893 bufp += entry_len; 17894 } 17895 (void) sprintf(bufp, "\n"); 17896 17897 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17898 } 17899 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17900 } 17901 17902 /* 17903 * Function: sd_print_sense_msg 17904 * 17905 * Description: Log a message based upon the given sense data. 17906 * 17907 * Arguments: un - ptr to associated softstate 17908 * bp - ptr to buf(9S) for the command 17909 * arg - ptr to associate sd_sense_info struct 17910 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17911 * or SD_NO_RETRY_ISSUED 17912 * 17913 * Context: May be called from interrupt context 17914 */ 17915 17916 static void 17917 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17918 { 17919 struct sd_xbuf *xp; 17920 struct scsi_pkt *pktp; 17921 uint8_t *sensep; 17922 daddr_t request_blkno; 17923 diskaddr_t err_blkno; 17924 int severity; 17925 int pfa_flag; 17926 extern struct scsi_key_strings scsi_cmds[]; 17927 17928 ASSERT(un != NULL); 17929 ASSERT(mutex_owned(SD_MUTEX(un))); 17930 ASSERT(bp != NULL); 17931 xp = SD_GET_XBUF(bp); 17932 ASSERT(xp != NULL); 17933 pktp = SD_GET_PKTP(bp); 17934 ASSERT(pktp != NULL); 17935 ASSERT(arg != NULL); 17936 17937 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17938 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17939 17940 if ((code == SD_DELAYED_RETRY_ISSUED) || 17941 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17942 severity = SCSI_ERR_RETRYABLE; 17943 } 17944 17945 /* Use absolute block number for the request block number */ 17946 request_blkno = xp->xb_blkno; 17947 17948 /* 17949 * Now try to get the error block number from the sense data 17950 */ 17951 sensep = xp->xb_sense_data; 17952 17953 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17954 (uint64_t *)&err_blkno)) { 17955 /* 17956 * We retrieved the error block number from the information 17957 * portion of the sense data. 17958 * 17959 * For USCSI commands we are better off using the error 17960 * block no. as the requested block no. (This is the best 17961 * we can estimate.) 17962 */ 17963 if ((SD_IS_BUFIO(xp) == FALSE) && 17964 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17965 request_blkno = err_blkno; 17966 } 17967 } else { 17968 /* 17969 * Without the es_valid bit set (for fixed format) or an 17970 * information descriptor (for descriptor format) we cannot 17971 * be certain of the error blkno, so just use the 17972 * request_blkno. 17973 */ 17974 err_blkno = (diskaddr_t)request_blkno; 17975 } 17976 17977 /* 17978 * The following will log the buffer contents for the release driver 17979 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17980 * level is set to verbose. 17981 */ 17982 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17983 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17984 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17985 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17986 17987 if (pfa_flag == FALSE) { 17988 /* This is normally only set for USCSI */ 17989 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17990 return; 17991 } 17992 17993 if ((SD_IS_BUFIO(xp) == TRUE) && 17994 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17995 (severity < sd_error_level))) { 17996 return; 17997 } 17998 } 17999 /* 18000 * Check for Sonoma Failover and keep a count of how many failed I/O's 18001 */ 18002 if ((SD_IS_LSI(un)) && 18003 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 18004 (scsi_sense_asc(sensep) == 0x94) && 18005 (scsi_sense_ascq(sensep) == 0x01)) { 18006 un->un_sonoma_failure_count++; 18007 if (un->un_sonoma_failure_count > 1) { 18008 return; 18009 } 18010 } 18011 18012 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP || 18013 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) && 18014 (pktp->pkt_resid == 0))) { 18015 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 18016 request_blkno, err_blkno, scsi_cmds, 18017 (struct scsi_extended_sense *)sensep, 18018 un->un_additional_codes, NULL); 18019 } 18020 } 18021 18022 /* 18023 * Function: sd_sense_key_no_sense 18024 * 18025 * Description: Recovery action when sense data was not received. 18026 * 18027 * Context: May be called from interrupt context 18028 */ 18029 18030 static void 18031 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 18032 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18033 { 18034 struct sd_sense_info si; 18035 18036 ASSERT(un != NULL); 18037 ASSERT(mutex_owned(SD_MUTEX(un))); 18038 ASSERT(bp != NULL); 18039 ASSERT(xp != NULL); 18040 ASSERT(pktp != NULL); 18041 18042 si.ssi_severity = SCSI_ERR_FATAL; 18043 si.ssi_pfa_flag = FALSE; 18044 18045 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18046 18047 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18048 &si, EIO, (clock_t)0, NULL); 18049 } 18050 18051 18052 /* 18053 * Function: sd_sense_key_recoverable_error 18054 * 18055 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 18056 * 18057 * Context: May be called from interrupt context 18058 */ 18059 18060 static void 18061 sd_sense_key_recoverable_error(struct sd_lun *un, 18062 uint8_t *sense_datap, 18063 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18064 { 18065 struct sd_sense_info si; 18066 uint8_t asc = scsi_sense_asc(sense_datap); 18067 18068 ASSERT(un != NULL); 18069 ASSERT(mutex_owned(SD_MUTEX(un))); 18070 ASSERT(bp != NULL); 18071 ASSERT(xp != NULL); 18072 ASSERT(pktp != NULL); 18073 18074 /* 18075 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 18076 */ 18077 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 18078 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18079 si.ssi_severity = SCSI_ERR_INFO; 18080 si.ssi_pfa_flag = TRUE; 18081 } else { 18082 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18083 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 18084 si.ssi_severity = SCSI_ERR_RECOVERED; 18085 si.ssi_pfa_flag = FALSE; 18086 } 18087 18088 if (pktp->pkt_resid == 0) { 18089 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18090 sd_return_command(un, bp); 18091 return; 18092 } 18093 18094 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18095 &si, EIO, (clock_t)0, NULL); 18096 } 18097 18098 18099 18100 18101 /* 18102 * Function: sd_sense_key_not_ready 18103 * 18104 * Description: Recovery actions for a SCSI "Not Ready" sense key. 18105 * 18106 * Context: May be called from interrupt context 18107 */ 18108 18109 static void 18110 sd_sense_key_not_ready(struct sd_lun *un, 18111 uint8_t *sense_datap, 18112 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18113 { 18114 struct sd_sense_info si; 18115 uint8_t asc = scsi_sense_asc(sense_datap); 18116 uint8_t ascq = scsi_sense_ascq(sense_datap); 18117 18118 ASSERT(un != NULL); 18119 ASSERT(mutex_owned(SD_MUTEX(un))); 18120 ASSERT(bp != NULL); 18121 ASSERT(xp != NULL); 18122 ASSERT(pktp != NULL); 18123 18124 si.ssi_severity = SCSI_ERR_FATAL; 18125 si.ssi_pfa_flag = FALSE; 18126 18127 /* 18128 * Update error stats after first NOT READY error. Disks may have 18129 * been powered down and may need to be restarted. For CDROMs, 18130 * report NOT READY errors only if media is present. 18131 */ 18132 if ((ISCD(un) && (asc == 0x3A)) || 18133 (xp->xb_nr_retry_count > 0)) { 18134 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18135 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 18136 } 18137 18138 /* 18139 * Just fail if the "not ready" retry limit has been reached. 18140 */ 18141 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 18142 /* Special check for error message printing for removables. */ 18143 if (un->un_f_has_removable_media && (asc == 0x04) && 18144 (ascq >= 0x04)) { 18145 si.ssi_severity = SCSI_ERR_ALL; 18146 } 18147 goto fail_command; 18148 } 18149 18150 /* 18151 * Check the ASC and ASCQ in the sense data as needed, to determine 18152 * what to do. 18153 */ 18154 switch (asc) { 18155 case 0x04: /* LOGICAL UNIT NOT READY */ 18156 /* 18157 * disk drives that don't spin up result in a very long delay 18158 * in format without warning messages. We will log a message 18159 * if the error level is set to verbose. 18160 */ 18161 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18162 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18163 "logical unit not ready, resetting disk\n"); 18164 } 18165 18166 /* 18167 * There are different requirements for CDROMs and disks for 18168 * the number of retries. If a CD-ROM is giving this, it is 18169 * probably reading TOC and is in the process of getting 18170 * ready, so we should keep on trying for a long time to make 18171 * sure that all types of media are taken in account (for 18172 * some media the drive takes a long time to read TOC). For 18173 * disks we do not want to retry this too many times as this 18174 * can cause a long hang in format when the drive refuses to 18175 * spin up (a very common failure). 18176 */ 18177 switch (ascq) { 18178 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 18179 /* 18180 * Disk drives frequently refuse to spin up which 18181 * results in a very long hang in format without 18182 * warning messages. 18183 * 18184 * Note: This code preserves the legacy behavior of 18185 * comparing xb_nr_retry_count against zero for fibre 18186 * channel targets instead of comparing against the 18187 * un_reset_retry_count value. The reason for this 18188 * discrepancy has been so utterly lost beneath the 18189 * Sands of Time that even Indiana Jones could not 18190 * find it. 18191 */ 18192 if (un->un_f_is_fibre == TRUE) { 18193 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18194 (xp->xb_nr_retry_count > 0)) && 18195 (un->un_startstop_timeid == NULL)) { 18196 scsi_log(SD_DEVINFO(un), sd_label, 18197 CE_WARN, "logical unit not ready, " 18198 "resetting disk\n"); 18199 sd_reset_target(un, pktp); 18200 } 18201 } else { 18202 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18203 (xp->xb_nr_retry_count > 18204 un->un_reset_retry_count)) && 18205 (un->un_startstop_timeid == NULL)) { 18206 scsi_log(SD_DEVINFO(un), sd_label, 18207 CE_WARN, "logical unit not ready, " 18208 "resetting disk\n"); 18209 sd_reset_target(un, pktp); 18210 } 18211 } 18212 break; 18213 18214 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 18215 /* 18216 * If the target is in the process of becoming 18217 * ready, just proceed with the retry. This can 18218 * happen with CD-ROMs that take a long time to 18219 * read TOC after a power cycle or reset. 18220 */ 18221 goto do_retry; 18222 18223 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 18224 break; 18225 18226 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 18227 /* 18228 * Retries cannot help here so just fail right away. 18229 */ 18230 goto fail_command; 18231 18232 case 0x88: 18233 /* 18234 * Vendor-unique code for T3/T4: it indicates a 18235 * path problem in a mutipathed config, but as far as 18236 * the target driver is concerned it equates to a fatal 18237 * error, so we should just fail the command right away 18238 * (without printing anything to the console). If this 18239 * is not a T3/T4, fall thru to the default recovery 18240 * action. 18241 * T3/T4 is FC only, don't need to check is_fibre 18242 */ 18243 if (SD_IS_T3(un) || SD_IS_T4(un)) { 18244 sd_return_failed_command(un, bp, EIO); 18245 return; 18246 } 18247 /* FALLTHRU */ 18248 18249 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 18250 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 18251 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 18252 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 18253 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 18254 default: /* Possible future codes in SCSI spec? */ 18255 /* 18256 * For removable-media devices, do not retry if 18257 * ASCQ > 2 as these result mostly from USCSI commands 18258 * on MMC devices issued to check status of an 18259 * operation initiated in immediate mode. Also for 18260 * ASCQ >= 4 do not print console messages as these 18261 * mainly represent a user-initiated operation 18262 * instead of a system failure. 18263 */ 18264 if (un->un_f_has_removable_media) { 18265 si.ssi_severity = SCSI_ERR_ALL; 18266 goto fail_command; 18267 } 18268 break; 18269 } 18270 18271 /* 18272 * As part of our recovery attempt for the NOT READY 18273 * condition, we issue a START STOP UNIT command. However 18274 * we want to wait for a short delay before attempting this 18275 * as there may still be more commands coming back from the 18276 * target with the check condition. To do this we use 18277 * timeout(9F) to call sd_start_stop_unit_callback() after 18278 * the delay interval expires. (sd_start_stop_unit_callback() 18279 * dispatches sd_start_stop_unit_task(), which will issue 18280 * the actual START STOP UNIT command. The delay interval 18281 * is one-half of the delay that we will use to retry the 18282 * command that generated the NOT READY condition. 18283 * 18284 * Note that we could just dispatch sd_start_stop_unit_task() 18285 * from here and allow it to sleep for the delay interval, 18286 * but then we would be tying up the taskq thread 18287 * uncesessarily for the duration of the delay. 18288 * 18289 * Do not issue the START STOP UNIT if the current command 18290 * is already a START STOP UNIT. 18291 */ 18292 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18293 break; 18294 } 18295 18296 /* 18297 * Do not schedule the timeout if one is already pending. 18298 */ 18299 if (un->un_startstop_timeid != NULL) { 18300 SD_INFO(SD_LOG_ERROR, un, 18301 "sd_sense_key_not_ready: restart already issued to" 18302 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18303 ddi_get_instance(SD_DEVINFO(un))); 18304 break; 18305 } 18306 18307 /* 18308 * Schedule the START STOP UNIT command, then queue the command 18309 * for a retry. 18310 * 18311 * Note: A timeout is not scheduled for this retry because we 18312 * want the retry to be serial with the START_STOP_UNIT. The 18313 * retry will be started when the START_STOP_UNIT is completed 18314 * in sd_start_stop_unit_task. 18315 */ 18316 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18317 un, un->un_busy_timeout / 2); 18318 xp->xb_nr_retry_count++; 18319 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18320 return; 18321 18322 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18323 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18324 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18325 "unit does not respond to selection\n"); 18326 } 18327 break; 18328 18329 case 0x3A: /* MEDIUM NOT PRESENT */ 18330 if (sd_error_level >= SCSI_ERR_FATAL) { 18331 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18332 "Caddy not inserted in drive\n"); 18333 } 18334 18335 sr_ejected(un); 18336 un->un_mediastate = DKIO_EJECTED; 18337 /* The state has changed, inform the media watch routines */ 18338 cv_broadcast(&un->un_state_cv); 18339 /* Just fail if no media is present in the drive. */ 18340 goto fail_command; 18341 18342 default: 18343 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18344 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18345 "Unit not Ready. Additional sense code 0x%x\n", 18346 asc); 18347 } 18348 break; 18349 } 18350 18351 do_retry: 18352 18353 /* 18354 * Retry the command, as some targets may report NOT READY for 18355 * several seconds after being reset. 18356 */ 18357 xp->xb_nr_retry_count++; 18358 si.ssi_severity = SCSI_ERR_RETRYABLE; 18359 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18360 &si, EIO, un->un_busy_timeout, NULL); 18361 18362 return; 18363 18364 fail_command: 18365 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18366 sd_return_failed_command(un, bp, EIO); 18367 } 18368 18369 18370 18371 /* 18372 * Function: sd_sense_key_medium_or_hardware_error 18373 * 18374 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18375 * sense key. 18376 * 18377 * Context: May be called from interrupt context 18378 */ 18379 18380 static void 18381 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18382 uint8_t *sense_datap, 18383 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18384 { 18385 struct sd_sense_info si; 18386 uint8_t sense_key = scsi_sense_key(sense_datap); 18387 uint8_t asc = scsi_sense_asc(sense_datap); 18388 18389 ASSERT(un != NULL); 18390 ASSERT(mutex_owned(SD_MUTEX(un))); 18391 ASSERT(bp != NULL); 18392 ASSERT(xp != NULL); 18393 ASSERT(pktp != NULL); 18394 18395 si.ssi_severity = SCSI_ERR_FATAL; 18396 si.ssi_pfa_flag = FALSE; 18397 18398 if (sense_key == KEY_MEDIUM_ERROR) { 18399 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18400 } 18401 18402 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18403 18404 if ((un->un_reset_retry_count != 0) && 18405 (xp->xb_retry_count == un->un_reset_retry_count)) { 18406 mutex_exit(SD_MUTEX(un)); 18407 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18408 if (un->un_f_allow_bus_device_reset == TRUE) { 18409 18410 boolean_t try_resetting_target = B_TRUE; 18411 18412 /* 18413 * We need to be able to handle specific ASC when we are 18414 * handling a KEY_HARDWARE_ERROR. In particular 18415 * taking the default action of resetting the target may 18416 * not be the appropriate way to attempt recovery. 18417 * Resetting a target because of a single LUN failure 18418 * victimizes all LUNs on that target. 18419 * 18420 * This is true for the LSI arrays, if an LSI 18421 * array controller returns an ASC of 0x84 (LUN Dead) we 18422 * should trust it. 18423 */ 18424 18425 if (sense_key == KEY_HARDWARE_ERROR) { 18426 switch (asc) { 18427 case 0x84: 18428 if (SD_IS_LSI(un)) { 18429 try_resetting_target = B_FALSE; 18430 } 18431 break; 18432 default: 18433 break; 18434 } 18435 } 18436 18437 if (try_resetting_target == B_TRUE) { 18438 int reset_retval = 0; 18439 if (un->un_f_lun_reset_enabled == TRUE) { 18440 SD_TRACE(SD_LOG_IO_CORE, un, 18441 "sd_sense_key_medium_or_hardware_" 18442 "error: issuing RESET_LUN\n"); 18443 reset_retval = 18444 scsi_reset(SD_ADDRESS(un), 18445 RESET_LUN); 18446 } 18447 if (reset_retval == 0) { 18448 SD_TRACE(SD_LOG_IO_CORE, un, 18449 "sd_sense_key_medium_or_hardware_" 18450 "error: issuing RESET_TARGET\n"); 18451 (void) scsi_reset(SD_ADDRESS(un), 18452 RESET_TARGET); 18453 } 18454 } 18455 } 18456 mutex_enter(SD_MUTEX(un)); 18457 } 18458 18459 /* 18460 * This really ought to be a fatal error, but we will retry anyway 18461 * as some drives report this as a spurious error. 18462 */ 18463 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18464 &si, EIO, (clock_t)0, NULL); 18465 } 18466 18467 18468 18469 /* 18470 * Function: sd_sense_key_illegal_request 18471 * 18472 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18473 * 18474 * Context: May be called from interrupt context 18475 */ 18476 18477 static void 18478 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18479 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18480 { 18481 struct sd_sense_info si; 18482 18483 ASSERT(un != NULL); 18484 ASSERT(mutex_owned(SD_MUTEX(un))); 18485 ASSERT(bp != NULL); 18486 ASSERT(xp != NULL); 18487 ASSERT(pktp != NULL); 18488 18489 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18490 18491 si.ssi_severity = SCSI_ERR_INFO; 18492 si.ssi_pfa_flag = FALSE; 18493 18494 /* Pointless to retry if the target thinks it's an illegal request */ 18495 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18496 sd_return_failed_command(un, bp, EIO); 18497 } 18498 18499 18500 18501 18502 /* 18503 * Function: sd_sense_key_unit_attention 18504 * 18505 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18506 * 18507 * Context: May be called from interrupt context 18508 */ 18509 18510 static void 18511 sd_sense_key_unit_attention(struct sd_lun *un, 18512 uint8_t *sense_datap, 18513 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18514 { 18515 /* 18516 * For UNIT ATTENTION we allow retries for one minute. Devices 18517 * like Sonoma can return UNIT ATTENTION close to a minute 18518 * under certain conditions. 18519 */ 18520 int retry_check_flag = SD_RETRIES_UA; 18521 boolean_t kstat_updated = B_FALSE; 18522 struct sd_sense_info si; 18523 uint8_t asc = scsi_sense_asc(sense_datap); 18524 uint8_t ascq = scsi_sense_ascq(sense_datap); 18525 18526 ASSERT(un != NULL); 18527 ASSERT(mutex_owned(SD_MUTEX(un))); 18528 ASSERT(bp != NULL); 18529 ASSERT(xp != NULL); 18530 ASSERT(pktp != NULL); 18531 18532 si.ssi_severity = SCSI_ERR_INFO; 18533 si.ssi_pfa_flag = FALSE; 18534 18535 18536 switch (asc) { 18537 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18538 if (sd_report_pfa != 0) { 18539 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18540 si.ssi_pfa_flag = TRUE; 18541 retry_check_flag = SD_RETRIES_STANDARD; 18542 goto do_retry; 18543 } 18544 18545 break; 18546 18547 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18548 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18549 un->un_resvd_status |= 18550 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18551 } 18552 #ifdef _LP64 18553 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18554 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18555 un, KM_NOSLEEP) == 0) { 18556 /* 18557 * If we can't dispatch the task we'll just 18558 * live without descriptor sense. We can 18559 * try again on the next "unit attention" 18560 */ 18561 SD_ERROR(SD_LOG_ERROR, un, 18562 "sd_sense_key_unit_attention: " 18563 "Could not dispatch " 18564 "sd_reenable_dsense_task\n"); 18565 } 18566 } 18567 #endif /* _LP64 */ 18568 /* FALLTHRU */ 18569 18570 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18571 if (!un->un_f_has_removable_media) { 18572 break; 18573 } 18574 18575 /* 18576 * When we get a unit attention from a removable-media device, 18577 * it may be in a state that will take a long time to recover 18578 * (e.g., from a reset). Since we are executing in interrupt 18579 * context here, we cannot wait around for the device to come 18580 * back. So hand this command off to sd_media_change_task() 18581 * for deferred processing under taskq thread context. (Note 18582 * that the command still may be failed if a problem is 18583 * encountered at a later time.) 18584 */ 18585 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18586 KM_NOSLEEP) == 0) { 18587 /* 18588 * Cannot dispatch the request so fail the command. 18589 */ 18590 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18591 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18592 si.ssi_severity = SCSI_ERR_FATAL; 18593 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18594 sd_return_failed_command(un, bp, EIO); 18595 } 18596 18597 /* 18598 * If failed to dispatch sd_media_change_task(), we already 18599 * updated kstat. If succeed to dispatch sd_media_change_task(), 18600 * we should update kstat later if it encounters an error. So, 18601 * we update kstat_updated flag here. 18602 */ 18603 kstat_updated = B_TRUE; 18604 18605 /* 18606 * Either the command has been successfully dispatched to a 18607 * task Q for retrying, or the dispatch failed. In either case 18608 * do NOT retry again by calling sd_retry_command. This sets up 18609 * two retries of the same command and when one completes and 18610 * frees the resources the other will access freed memory, 18611 * a bad thing. 18612 */ 18613 return; 18614 18615 default: 18616 break; 18617 } 18618 18619 /* 18620 * ASC ASCQ 18621 * 2A 09 Capacity data has changed 18622 * 2A 01 Mode parameters changed 18623 * 3F 0E Reported luns data has changed 18624 * Arrays that support logical unit expansion should report 18625 * capacity changes(2Ah/09). Mode parameters changed and 18626 * reported luns data has changed are the approximation. 18627 */ 18628 if (((asc == 0x2a) && (ascq == 0x09)) || 18629 ((asc == 0x2a) && (ascq == 0x01)) || 18630 ((asc == 0x3f) && (ascq == 0x0e))) { 18631 if (taskq_dispatch(sd_tq, sd_target_change_task, un, 18632 KM_NOSLEEP) == 0) { 18633 SD_ERROR(SD_LOG_ERROR, un, 18634 "sd_sense_key_unit_attention: " 18635 "Could not dispatch sd_target_change_task\n"); 18636 } 18637 } 18638 18639 /* 18640 * Update kstat if we haven't done that. 18641 */ 18642 if (!kstat_updated) { 18643 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18644 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18645 } 18646 18647 do_retry: 18648 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18649 EIO, SD_UA_RETRY_DELAY, NULL); 18650 } 18651 18652 18653 18654 /* 18655 * Function: sd_sense_key_fail_command 18656 * 18657 * Description: Use to fail a command when we don't like the sense key that 18658 * was returned. 18659 * 18660 * Context: May be called from interrupt context 18661 */ 18662 18663 static void 18664 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18665 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18666 { 18667 struct sd_sense_info si; 18668 18669 ASSERT(un != NULL); 18670 ASSERT(mutex_owned(SD_MUTEX(un))); 18671 ASSERT(bp != NULL); 18672 ASSERT(xp != NULL); 18673 ASSERT(pktp != NULL); 18674 18675 si.ssi_severity = SCSI_ERR_FATAL; 18676 si.ssi_pfa_flag = FALSE; 18677 18678 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18679 sd_return_failed_command(un, bp, EIO); 18680 } 18681 18682 18683 18684 /* 18685 * Function: sd_sense_key_blank_check 18686 * 18687 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18688 * Has no monetary connotation. 18689 * 18690 * Context: May be called from interrupt context 18691 */ 18692 18693 static void 18694 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18695 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18696 { 18697 struct sd_sense_info si; 18698 18699 ASSERT(un != NULL); 18700 ASSERT(mutex_owned(SD_MUTEX(un))); 18701 ASSERT(bp != NULL); 18702 ASSERT(xp != NULL); 18703 ASSERT(pktp != NULL); 18704 18705 /* 18706 * Blank check is not fatal for removable devices, therefore 18707 * it does not require a console message. 18708 */ 18709 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18710 SCSI_ERR_FATAL; 18711 si.ssi_pfa_flag = FALSE; 18712 18713 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18714 sd_return_failed_command(un, bp, EIO); 18715 } 18716 18717 18718 18719 18720 /* 18721 * Function: sd_sense_key_aborted_command 18722 * 18723 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18724 * 18725 * Context: May be called from interrupt context 18726 */ 18727 18728 static void 18729 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18730 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18731 { 18732 struct sd_sense_info si; 18733 18734 ASSERT(un != NULL); 18735 ASSERT(mutex_owned(SD_MUTEX(un))); 18736 ASSERT(bp != NULL); 18737 ASSERT(xp != NULL); 18738 ASSERT(pktp != NULL); 18739 18740 si.ssi_severity = SCSI_ERR_FATAL; 18741 si.ssi_pfa_flag = FALSE; 18742 18743 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18744 18745 /* 18746 * This really ought to be a fatal error, but we will retry anyway 18747 * as some drives report this as a spurious error. 18748 */ 18749 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18750 &si, EIO, drv_usectohz(100000), NULL); 18751 } 18752 18753 18754 18755 /* 18756 * Function: sd_sense_key_default 18757 * 18758 * Description: Default recovery action for several SCSI sense keys (basically 18759 * attempts a retry). 18760 * 18761 * Context: May be called from interrupt context 18762 */ 18763 18764 static void 18765 sd_sense_key_default(struct sd_lun *un, 18766 uint8_t *sense_datap, 18767 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18768 { 18769 struct sd_sense_info si; 18770 uint8_t sense_key = scsi_sense_key(sense_datap); 18771 18772 ASSERT(un != NULL); 18773 ASSERT(mutex_owned(SD_MUTEX(un))); 18774 ASSERT(bp != NULL); 18775 ASSERT(xp != NULL); 18776 ASSERT(pktp != NULL); 18777 18778 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18779 18780 /* 18781 * Undecoded sense key. Attempt retries and hope that will fix 18782 * the problem. Otherwise, we're dead. 18783 */ 18784 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18785 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18786 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18787 } 18788 18789 si.ssi_severity = SCSI_ERR_FATAL; 18790 si.ssi_pfa_flag = FALSE; 18791 18792 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18793 &si, EIO, (clock_t)0, NULL); 18794 } 18795 18796 18797 18798 /* 18799 * Function: sd_print_retry_msg 18800 * 18801 * Description: Print a message indicating the retry action being taken. 18802 * 18803 * Arguments: un - ptr to associated softstate 18804 * bp - ptr to buf(9S) for the command 18805 * arg - not used. 18806 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18807 * or SD_NO_RETRY_ISSUED 18808 * 18809 * Context: May be called from interrupt context 18810 */ 18811 /* ARGSUSED */ 18812 static void 18813 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18814 { 18815 struct sd_xbuf *xp; 18816 struct scsi_pkt *pktp; 18817 char *reasonp; 18818 char *msgp; 18819 18820 ASSERT(un != NULL); 18821 ASSERT(mutex_owned(SD_MUTEX(un))); 18822 ASSERT(bp != NULL); 18823 pktp = SD_GET_PKTP(bp); 18824 ASSERT(pktp != NULL); 18825 xp = SD_GET_XBUF(bp); 18826 ASSERT(xp != NULL); 18827 18828 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18829 mutex_enter(&un->un_pm_mutex); 18830 if ((un->un_state == SD_STATE_SUSPENDED) || 18831 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18832 (pktp->pkt_flags & FLAG_SILENT)) { 18833 mutex_exit(&un->un_pm_mutex); 18834 goto update_pkt_reason; 18835 } 18836 mutex_exit(&un->un_pm_mutex); 18837 18838 /* 18839 * Suppress messages if they are all the same pkt_reason; with 18840 * TQ, many (up to 256) are returned with the same pkt_reason. 18841 * If we are in panic, then suppress the retry messages. 18842 */ 18843 switch (flag) { 18844 case SD_NO_RETRY_ISSUED: 18845 msgp = "giving up"; 18846 break; 18847 case SD_IMMEDIATE_RETRY_ISSUED: 18848 case SD_DELAYED_RETRY_ISSUED: 18849 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18850 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18851 (sd_error_level != SCSI_ERR_ALL))) { 18852 return; 18853 } 18854 msgp = "retrying command"; 18855 break; 18856 default: 18857 goto update_pkt_reason; 18858 } 18859 18860 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18861 scsi_rname(pktp->pkt_reason)); 18862 18863 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 18864 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18865 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18866 } 18867 18868 update_pkt_reason: 18869 /* 18870 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18871 * This is to prevent multiple console messages for the same failure 18872 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18873 * when the command is retried successfully because there still may be 18874 * more commands coming back with the same value of pktp->pkt_reason. 18875 */ 18876 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18877 un->un_last_pkt_reason = pktp->pkt_reason; 18878 } 18879 } 18880 18881 18882 /* 18883 * Function: sd_print_cmd_incomplete_msg 18884 * 18885 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18886 * 18887 * Arguments: un - ptr to associated softstate 18888 * bp - ptr to buf(9S) for the command 18889 * arg - passed to sd_print_retry_msg() 18890 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18891 * or SD_NO_RETRY_ISSUED 18892 * 18893 * Context: May be called from interrupt context 18894 */ 18895 18896 static void 18897 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18898 int code) 18899 { 18900 dev_info_t *dip; 18901 18902 ASSERT(un != NULL); 18903 ASSERT(mutex_owned(SD_MUTEX(un))); 18904 ASSERT(bp != NULL); 18905 18906 switch (code) { 18907 case SD_NO_RETRY_ISSUED: 18908 /* Command was failed. Someone turned off this target? */ 18909 if (un->un_state != SD_STATE_OFFLINE) { 18910 /* 18911 * Suppress message if we are detaching and 18912 * device has been disconnected 18913 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18914 * private interface and not part of the DDI 18915 */ 18916 dip = un->un_sd->sd_dev; 18917 if (!(DEVI_IS_DETACHING(dip) && 18918 DEVI_IS_DEVICE_REMOVED(dip))) { 18919 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18920 "disk not responding to selection\n"); 18921 } 18922 New_state(un, SD_STATE_OFFLINE); 18923 } 18924 break; 18925 18926 case SD_DELAYED_RETRY_ISSUED: 18927 case SD_IMMEDIATE_RETRY_ISSUED: 18928 default: 18929 /* Command was successfully queued for retry */ 18930 sd_print_retry_msg(un, bp, arg, code); 18931 break; 18932 } 18933 } 18934 18935 18936 /* 18937 * Function: sd_pkt_reason_cmd_incomplete 18938 * 18939 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18940 * 18941 * Context: May be called from interrupt context 18942 */ 18943 18944 static void 18945 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18946 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18947 { 18948 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18949 18950 ASSERT(un != NULL); 18951 ASSERT(mutex_owned(SD_MUTEX(un))); 18952 ASSERT(bp != NULL); 18953 ASSERT(xp != NULL); 18954 ASSERT(pktp != NULL); 18955 18956 /* Do not do a reset if selection did not complete */ 18957 /* Note: Should this not just check the bit? */ 18958 if (pktp->pkt_state != STATE_GOT_BUS) { 18959 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18960 sd_reset_target(un, pktp); 18961 } 18962 18963 /* 18964 * If the target was not successfully selected, then set 18965 * SD_RETRIES_FAILFAST to indicate that we lost communication 18966 * with the target, and further retries and/or commands are 18967 * likely to take a long time. 18968 */ 18969 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18970 flag |= SD_RETRIES_FAILFAST; 18971 } 18972 18973 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18974 18975 sd_retry_command(un, bp, flag, 18976 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18977 } 18978 18979 18980 18981 /* 18982 * Function: sd_pkt_reason_cmd_tran_err 18983 * 18984 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18985 * 18986 * Context: May be called from interrupt context 18987 */ 18988 18989 static void 18990 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18991 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18992 { 18993 ASSERT(un != NULL); 18994 ASSERT(mutex_owned(SD_MUTEX(un))); 18995 ASSERT(bp != NULL); 18996 ASSERT(xp != NULL); 18997 ASSERT(pktp != NULL); 18998 18999 /* 19000 * Do not reset if we got a parity error, or if 19001 * selection did not complete. 19002 */ 19003 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19004 /* Note: Should this not just check the bit for pkt_state? */ 19005 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 19006 (pktp->pkt_state != STATE_GOT_BUS)) { 19007 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19008 sd_reset_target(un, pktp); 19009 } 19010 19011 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19012 19013 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19014 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19015 } 19016 19017 19018 19019 /* 19020 * Function: sd_pkt_reason_cmd_reset 19021 * 19022 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 19023 * 19024 * Context: May be called from interrupt context 19025 */ 19026 19027 static void 19028 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 19029 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19030 { 19031 ASSERT(un != NULL); 19032 ASSERT(mutex_owned(SD_MUTEX(un))); 19033 ASSERT(bp != NULL); 19034 ASSERT(xp != NULL); 19035 ASSERT(pktp != NULL); 19036 19037 /* The target may still be running the command, so try to reset. */ 19038 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19039 sd_reset_target(un, pktp); 19040 19041 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19042 19043 /* 19044 * If pkt_reason is CMD_RESET chances are that this pkt got 19045 * reset because another target on this bus caused it. The target 19046 * that caused it should get CMD_TIMEOUT with pkt_statistics 19047 * of STAT_TIMEOUT/STAT_DEV_RESET. 19048 */ 19049 19050 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19051 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19052 } 19053 19054 19055 19056 19057 /* 19058 * Function: sd_pkt_reason_cmd_aborted 19059 * 19060 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 19061 * 19062 * Context: May be called from interrupt context 19063 */ 19064 19065 static void 19066 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 19067 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19068 { 19069 ASSERT(un != NULL); 19070 ASSERT(mutex_owned(SD_MUTEX(un))); 19071 ASSERT(bp != NULL); 19072 ASSERT(xp != NULL); 19073 ASSERT(pktp != NULL); 19074 19075 /* The target may still be running the command, so try to reset. */ 19076 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19077 sd_reset_target(un, pktp); 19078 19079 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19080 19081 /* 19082 * If pkt_reason is CMD_ABORTED chances are that this pkt got 19083 * aborted because another target on this bus caused it. The target 19084 * that caused it should get CMD_TIMEOUT with pkt_statistics 19085 * of STAT_TIMEOUT/STAT_DEV_RESET. 19086 */ 19087 19088 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19089 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19090 } 19091 19092 19093 19094 /* 19095 * Function: sd_pkt_reason_cmd_timeout 19096 * 19097 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 19098 * 19099 * Context: May be called from interrupt context 19100 */ 19101 19102 static void 19103 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 19104 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19105 { 19106 ASSERT(un != NULL); 19107 ASSERT(mutex_owned(SD_MUTEX(un))); 19108 ASSERT(bp != NULL); 19109 ASSERT(xp != NULL); 19110 ASSERT(pktp != NULL); 19111 19112 19113 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19114 sd_reset_target(un, pktp); 19115 19116 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19117 19118 /* 19119 * A command timeout indicates that we could not establish 19120 * communication with the target, so set SD_RETRIES_FAILFAST 19121 * as further retries/commands are likely to take a long time. 19122 */ 19123 sd_retry_command(un, bp, 19124 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 19125 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19126 } 19127 19128 19129 19130 /* 19131 * Function: sd_pkt_reason_cmd_unx_bus_free 19132 * 19133 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 19134 * 19135 * Context: May be called from interrupt context 19136 */ 19137 19138 static void 19139 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 19140 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19141 { 19142 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 19143 19144 ASSERT(un != NULL); 19145 ASSERT(mutex_owned(SD_MUTEX(un))); 19146 ASSERT(bp != NULL); 19147 ASSERT(xp != NULL); 19148 ASSERT(pktp != NULL); 19149 19150 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19151 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19152 19153 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 19154 sd_print_retry_msg : NULL; 19155 19156 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19157 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19158 } 19159 19160 19161 /* 19162 * Function: sd_pkt_reason_cmd_tag_reject 19163 * 19164 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 19165 * 19166 * Context: May be called from interrupt context 19167 */ 19168 19169 static void 19170 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 19171 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19172 { 19173 ASSERT(un != NULL); 19174 ASSERT(mutex_owned(SD_MUTEX(un))); 19175 ASSERT(bp != NULL); 19176 ASSERT(xp != NULL); 19177 ASSERT(pktp != NULL); 19178 19179 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19180 pktp->pkt_flags = 0; 19181 un->un_tagflags = 0; 19182 if (un->un_f_opt_queueing == TRUE) { 19183 un->un_throttle = min(un->un_throttle, 3); 19184 } else { 19185 un->un_throttle = 1; 19186 } 19187 mutex_exit(SD_MUTEX(un)); 19188 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 19189 mutex_enter(SD_MUTEX(un)); 19190 19191 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19192 19193 /* Legacy behavior not to check retry counts here. */ 19194 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 19195 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19196 } 19197 19198 19199 /* 19200 * Function: sd_pkt_reason_default 19201 * 19202 * Description: Default recovery actions for SCSA pkt_reason values that 19203 * do not have more explicit recovery actions. 19204 * 19205 * Context: May be called from interrupt context 19206 */ 19207 19208 static void 19209 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 19210 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19211 { 19212 ASSERT(un != NULL); 19213 ASSERT(mutex_owned(SD_MUTEX(un))); 19214 ASSERT(bp != NULL); 19215 ASSERT(xp != NULL); 19216 ASSERT(pktp != NULL); 19217 19218 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19219 sd_reset_target(un, pktp); 19220 19221 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19222 19223 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19224 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19225 } 19226 19227 19228 19229 /* 19230 * Function: sd_pkt_status_check_condition 19231 * 19232 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 19233 * 19234 * Context: May be called from interrupt context 19235 */ 19236 19237 static void 19238 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 19239 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19240 { 19241 ASSERT(un != NULL); 19242 ASSERT(mutex_owned(SD_MUTEX(un))); 19243 ASSERT(bp != NULL); 19244 ASSERT(xp != NULL); 19245 ASSERT(pktp != NULL); 19246 19247 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 19248 "entry: buf:0x%p xp:0x%p\n", bp, xp); 19249 19250 /* 19251 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 19252 * command will be retried after the request sense). Otherwise, retry 19253 * the command. Note: we are issuing the request sense even though the 19254 * retry limit may have been reached for the failed command. 19255 */ 19256 if (un->un_f_arq_enabled == FALSE) { 19257 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19258 "no ARQ, sending request sense command\n"); 19259 sd_send_request_sense_command(un, bp, pktp); 19260 } else { 19261 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19262 "ARQ,retrying request sense command\n"); 19263 #if defined(__i386) || defined(__amd64) 19264 /* 19265 * The SD_RETRY_DELAY value need to be adjusted here 19266 * when SD_RETRY_DELAY change in sddef.h 19267 */ 19268 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19269 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 19270 NULL); 19271 #else 19272 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 19273 EIO, SD_RETRY_DELAY, NULL); 19274 #endif 19275 } 19276 19277 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 19278 } 19279 19280 19281 /* 19282 * Function: sd_pkt_status_busy 19283 * 19284 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 19285 * 19286 * Context: May be called from interrupt context 19287 */ 19288 19289 static void 19290 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 19291 struct scsi_pkt *pktp) 19292 { 19293 ASSERT(un != NULL); 19294 ASSERT(mutex_owned(SD_MUTEX(un))); 19295 ASSERT(bp != NULL); 19296 ASSERT(xp != NULL); 19297 ASSERT(pktp != NULL); 19298 19299 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19300 "sd_pkt_status_busy: entry\n"); 19301 19302 /* If retries are exhausted, just fail the command. */ 19303 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19304 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19305 "device busy too long\n"); 19306 sd_return_failed_command(un, bp, EIO); 19307 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19308 "sd_pkt_status_busy: exit\n"); 19309 return; 19310 } 19311 xp->xb_retry_count++; 19312 19313 /* 19314 * Try to reset the target. However, we do not want to perform 19315 * more than one reset if the device continues to fail. The reset 19316 * will be performed when the retry count reaches the reset 19317 * threshold. This threshold should be set such that at least 19318 * one retry is issued before the reset is performed. 19319 */ 19320 if (xp->xb_retry_count == 19321 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19322 int rval = 0; 19323 mutex_exit(SD_MUTEX(un)); 19324 if (un->un_f_allow_bus_device_reset == TRUE) { 19325 /* 19326 * First try to reset the LUN; if we cannot then 19327 * try to reset the target. 19328 */ 19329 if (un->un_f_lun_reset_enabled == TRUE) { 19330 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19331 "sd_pkt_status_busy: RESET_LUN\n"); 19332 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19333 } 19334 if (rval == 0) { 19335 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19336 "sd_pkt_status_busy: RESET_TARGET\n"); 19337 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19338 } 19339 } 19340 if (rval == 0) { 19341 /* 19342 * If the RESET_LUN and/or RESET_TARGET failed, 19343 * try RESET_ALL 19344 */ 19345 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19346 "sd_pkt_status_busy: RESET_ALL\n"); 19347 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19348 } 19349 mutex_enter(SD_MUTEX(un)); 19350 if (rval == 0) { 19351 /* 19352 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19353 * At this point we give up & fail the command. 19354 */ 19355 sd_return_failed_command(un, bp, EIO); 19356 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19357 "sd_pkt_status_busy: exit (failed cmd)\n"); 19358 return; 19359 } 19360 } 19361 19362 /* 19363 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19364 * we have already checked the retry counts above. 19365 */ 19366 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19367 EIO, un->un_busy_timeout, NULL); 19368 19369 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19370 "sd_pkt_status_busy: exit\n"); 19371 } 19372 19373 19374 /* 19375 * Function: sd_pkt_status_reservation_conflict 19376 * 19377 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19378 * command status. 19379 * 19380 * Context: May be called from interrupt context 19381 */ 19382 19383 static void 19384 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19385 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19386 { 19387 ASSERT(un != NULL); 19388 ASSERT(mutex_owned(SD_MUTEX(un))); 19389 ASSERT(bp != NULL); 19390 ASSERT(xp != NULL); 19391 ASSERT(pktp != NULL); 19392 19393 /* 19394 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19395 * conflict could be due to various reasons like incorrect keys, not 19396 * registered or not reserved etc. So, we return EACCES to the caller. 19397 */ 19398 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19399 int cmd = SD_GET_PKT_OPCODE(pktp); 19400 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19401 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19402 sd_return_failed_command(un, bp, EACCES); 19403 return; 19404 } 19405 } 19406 19407 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19408 19409 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19410 if (sd_failfast_enable != 0) { 19411 /* By definition, we must panic here.... */ 19412 sd_panic_for_res_conflict(un); 19413 /*NOTREACHED*/ 19414 } 19415 SD_ERROR(SD_LOG_IO, un, 19416 "sd_handle_resv_conflict: Disk Reserved\n"); 19417 sd_return_failed_command(un, bp, EACCES); 19418 return; 19419 } 19420 19421 /* 19422 * 1147670: retry only if sd_retry_on_reservation_conflict 19423 * property is set (default is 1). Retries will not succeed 19424 * on a disk reserved by another initiator. HA systems 19425 * may reset this via sd.conf to avoid these retries. 19426 * 19427 * Note: The legacy return code for this failure is EIO, however EACCES 19428 * seems more appropriate for a reservation conflict. 19429 */ 19430 if (sd_retry_on_reservation_conflict == 0) { 19431 SD_ERROR(SD_LOG_IO, un, 19432 "sd_handle_resv_conflict: Device Reserved\n"); 19433 sd_return_failed_command(un, bp, EIO); 19434 return; 19435 } 19436 19437 /* 19438 * Retry the command if we can. 19439 * 19440 * Note: The legacy return code for this failure is EIO, however EACCES 19441 * seems more appropriate for a reservation conflict. 19442 */ 19443 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19444 (clock_t)2, NULL); 19445 } 19446 19447 19448 19449 /* 19450 * Function: sd_pkt_status_qfull 19451 * 19452 * Description: Handle a QUEUE FULL condition from the target. This can 19453 * occur if the HBA does not handle the queue full condition. 19454 * (Basically this means third-party HBAs as Sun HBAs will 19455 * handle the queue full condition.) Note that if there are 19456 * some commands already in the transport, then the queue full 19457 * has occurred because the queue for this nexus is actually 19458 * full. If there are no commands in the transport, then the 19459 * queue full is resulting from some other initiator or lun 19460 * consuming all the resources at the target. 19461 * 19462 * Context: May be called from interrupt context 19463 */ 19464 19465 static void 19466 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19467 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19468 { 19469 ASSERT(un != NULL); 19470 ASSERT(mutex_owned(SD_MUTEX(un))); 19471 ASSERT(bp != NULL); 19472 ASSERT(xp != NULL); 19473 ASSERT(pktp != NULL); 19474 19475 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19476 "sd_pkt_status_qfull: entry\n"); 19477 19478 /* 19479 * Just lower the QFULL throttle and retry the command. Note that 19480 * we do not limit the number of retries here. 19481 */ 19482 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19483 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19484 SD_RESTART_TIMEOUT, NULL); 19485 19486 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19487 "sd_pkt_status_qfull: exit\n"); 19488 } 19489 19490 19491 /* 19492 * Function: sd_reset_target 19493 * 19494 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19495 * RESET_TARGET, or RESET_ALL. 19496 * 19497 * Context: May be called under interrupt context. 19498 */ 19499 19500 static void 19501 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19502 { 19503 int rval = 0; 19504 19505 ASSERT(un != NULL); 19506 ASSERT(mutex_owned(SD_MUTEX(un))); 19507 ASSERT(pktp != NULL); 19508 19509 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19510 19511 /* 19512 * No need to reset if the transport layer has already done so. 19513 */ 19514 if ((pktp->pkt_statistics & 19515 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19516 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19517 "sd_reset_target: no reset\n"); 19518 return; 19519 } 19520 19521 mutex_exit(SD_MUTEX(un)); 19522 19523 if (un->un_f_allow_bus_device_reset == TRUE) { 19524 if (un->un_f_lun_reset_enabled == TRUE) { 19525 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19526 "sd_reset_target: RESET_LUN\n"); 19527 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19528 } 19529 if (rval == 0) { 19530 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19531 "sd_reset_target: RESET_TARGET\n"); 19532 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19533 } 19534 } 19535 19536 if (rval == 0) { 19537 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19538 "sd_reset_target: RESET_ALL\n"); 19539 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19540 } 19541 19542 mutex_enter(SD_MUTEX(un)); 19543 19544 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19545 } 19546 19547 /* 19548 * Function: sd_target_change_task 19549 * 19550 * Description: Handle dynamic target change 19551 * 19552 * Context: Executes in a taskq() thread context 19553 */ 19554 static void 19555 sd_target_change_task(void *arg) 19556 { 19557 struct sd_lun *un = arg; 19558 uint64_t capacity; 19559 diskaddr_t label_cap; 19560 uint_t lbasize; 19561 sd_ssc_t *ssc; 19562 19563 ASSERT(un != NULL); 19564 ASSERT(!mutex_owned(SD_MUTEX(un))); 19565 19566 if ((un->un_f_blockcount_is_valid == FALSE) || 19567 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 19568 return; 19569 } 19570 19571 ssc = sd_ssc_init(un); 19572 19573 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity, 19574 &lbasize, SD_PATH_DIRECT) != 0) { 19575 SD_ERROR(SD_LOG_ERROR, un, 19576 "sd_target_change_task: fail to read capacity\n"); 19577 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19578 goto task_exit; 19579 } 19580 19581 mutex_enter(SD_MUTEX(un)); 19582 if (capacity <= un->un_blockcount) { 19583 mutex_exit(SD_MUTEX(un)); 19584 goto task_exit; 19585 } 19586 19587 sd_update_block_info(un, lbasize, capacity); 19588 mutex_exit(SD_MUTEX(un)); 19589 19590 /* 19591 * If lun is EFI labeled and lun capacity is greater than the 19592 * capacity contained in the label, log a sys event. 19593 */ 19594 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 19595 (void*)SD_PATH_DIRECT) == 0) { 19596 mutex_enter(SD_MUTEX(un)); 19597 if (un->un_f_blockcount_is_valid && 19598 un->un_blockcount > label_cap) { 19599 mutex_exit(SD_MUTEX(un)); 19600 sd_log_lun_expansion_event(un, KM_SLEEP); 19601 } else { 19602 mutex_exit(SD_MUTEX(un)); 19603 } 19604 } 19605 19606 task_exit: 19607 sd_ssc_fini(ssc); 19608 } 19609 19610 19611 /* 19612 * Function: sd_log_dev_status_event 19613 * 19614 * Description: Log EC_dev_status sysevent 19615 * 19616 * Context: Never called from interrupt context 19617 */ 19618 static void 19619 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag) 19620 { 19621 int err; 19622 char *path; 19623 nvlist_t *attr_list; 19624 19625 /* Allocate and build sysevent attribute list */ 19626 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag); 19627 if (err != 0) { 19628 SD_ERROR(SD_LOG_ERROR, un, 19629 "sd_log_dev_status_event: fail to allocate space\n"); 19630 return; 19631 } 19632 19633 path = kmem_alloc(MAXPATHLEN, km_flag); 19634 if (path == NULL) { 19635 nvlist_free(attr_list); 19636 SD_ERROR(SD_LOG_ERROR, un, 19637 "sd_log_dev_status_event: fail to allocate space\n"); 19638 return; 19639 } 19640 /* 19641 * Add path attribute to identify the lun. 19642 * We are using minor node 'a' as the sysevent attribute. 19643 */ 19644 (void) snprintf(path, MAXPATHLEN, "/devices"); 19645 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path)); 19646 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path), 19647 ":a"); 19648 19649 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path); 19650 if (err != 0) { 19651 nvlist_free(attr_list); 19652 kmem_free(path, MAXPATHLEN); 19653 SD_ERROR(SD_LOG_ERROR, un, 19654 "sd_log_dev_status_event: fail to add attribute\n"); 19655 return; 19656 } 19657 19658 /* Log dynamic lun expansion sysevent */ 19659 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS, 19660 esc, attr_list, NULL, km_flag); 19661 if (err != DDI_SUCCESS) { 19662 SD_ERROR(SD_LOG_ERROR, un, 19663 "sd_log_dev_status_event: fail to log sysevent\n"); 19664 } 19665 19666 nvlist_free(attr_list); 19667 kmem_free(path, MAXPATHLEN); 19668 } 19669 19670 19671 /* 19672 * Function: sd_log_lun_expansion_event 19673 * 19674 * Description: Log lun expansion sys event 19675 * 19676 * Context: Never called from interrupt context 19677 */ 19678 static void 19679 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag) 19680 { 19681 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag); 19682 } 19683 19684 19685 /* 19686 * Function: sd_log_eject_request_event 19687 * 19688 * Description: Log eject request sysevent 19689 * 19690 * Context: Never called from interrupt context 19691 */ 19692 static void 19693 sd_log_eject_request_event(struct sd_lun *un, int km_flag) 19694 { 19695 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag); 19696 } 19697 19698 19699 /* 19700 * Function: sd_media_change_task 19701 * 19702 * Description: Recovery action for CDROM to become available. 19703 * 19704 * Context: Executes in a taskq() thread context 19705 */ 19706 19707 static void 19708 sd_media_change_task(void *arg) 19709 { 19710 struct scsi_pkt *pktp = arg; 19711 struct sd_lun *un; 19712 struct buf *bp; 19713 struct sd_xbuf *xp; 19714 int err = 0; 19715 int retry_count = 0; 19716 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19717 struct sd_sense_info si; 19718 19719 ASSERT(pktp != NULL); 19720 bp = (struct buf *)pktp->pkt_private; 19721 ASSERT(bp != NULL); 19722 xp = SD_GET_XBUF(bp); 19723 ASSERT(xp != NULL); 19724 un = SD_GET_UN(bp); 19725 ASSERT(un != NULL); 19726 ASSERT(!mutex_owned(SD_MUTEX(un))); 19727 ASSERT(un->un_f_monitor_media_state); 19728 19729 si.ssi_severity = SCSI_ERR_INFO; 19730 si.ssi_pfa_flag = FALSE; 19731 19732 /* 19733 * When a reset is issued on a CDROM, it takes a long time to 19734 * recover. First few attempts to read capacity and other things 19735 * related to handling unit attention fail (with a ASC 0x4 and 19736 * ASCQ 0x1). In that case we want to do enough retries and we want 19737 * to limit the retries in other cases of genuine failures like 19738 * no media in drive. 19739 */ 19740 while (retry_count++ < retry_limit) { 19741 if ((err = sd_handle_mchange(un)) == 0) { 19742 break; 19743 } 19744 if (err == EAGAIN) { 19745 retry_limit = SD_UNIT_ATTENTION_RETRY; 19746 } 19747 /* Sleep for 0.5 sec. & try again */ 19748 delay(drv_usectohz(500000)); 19749 } 19750 19751 /* 19752 * Dispatch (retry or fail) the original command here, 19753 * along with appropriate console messages.... 19754 * 19755 * Must grab the mutex before calling sd_retry_command, 19756 * sd_print_sense_msg and sd_return_failed_command. 19757 */ 19758 mutex_enter(SD_MUTEX(un)); 19759 if (err != SD_CMD_SUCCESS) { 19760 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19761 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19762 si.ssi_severity = SCSI_ERR_FATAL; 19763 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19764 sd_return_failed_command(un, bp, EIO); 19765 } else { 19766 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19767 &si, EIO, (clock_t)0, NULL); 19768 } 19769 mutex_exit(SD_MUTEX(un)); 19770 } 19771 19772 19773 19774 /* 19775 * Function: sd_handle_mchange 19776 * 19777 * Description: Perform geometry validation & other recovery when CDROM 19778 * has been removed from drive. 19779 * 19780 * Return Code: 0 for success 19781 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19782 * sd_send_scsi_READ_CAPACITY() 19783 * 19784 * Context: Executes in a taskq() thread context 19785 */ 19786 19787 static int 19788 sd_handle_mchange(struct sd_lun *un) 19789 { 19790 uint64_t capacity; 19791 uint32_t lbasize; 19792 int rval; 19793 sd_ssc_t *ssc; 19794 19795 ASSERT(!mutex_owned(SD_MUTEX(un))); 19796 ASSERT(un->un_f_monitor_media_state); 19797 19798 ssc = sd_ssc_init(un); 19799 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 19800 SD_PATH_DIRECT_PRIORITY); 19801 19802 if (rval != 0) 19803 goto failed; 19804 19805 mutex_enter(SD_MUTEX(un)); 19806 sd_update_block_info(un, lbasize, capacity); 19807 19808 if (un->un_errstats != NULL) { 19809 struct sd_errstats *stp = 19810 (struct sd_errstats *)un->un_errstats->ks_data; 19811 stp->sd_capacity.value.ui64 = (uint64_t) 19812 ((uint64_t)un->un_blockcount * 19813 (uint64_t)un->un_tgt_blocksize); 19814 } 19815 19816 /* 19817 * Check if the media in the device is writable or not 19818 */ 19819 if (ISCD(un)) { 19820 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY); 19821 } 19822 19823 /* 19824 * Note: Maybe let the strategy/partitioning chain worry about getting 19825 * valid geometry. 19826 */ 19827 mutex_exit(SD_MUTEX(un)); 19828 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 19829 19830 19831 if (cmlb_validate(un->un_cmlbhandle, 0, 19832 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 19833 sd_ssc_fini(ssc); 19834 return (EIO); 19835 } else { 19836 if (un->un_f_pkstats_enabled) { 19837 sd_set_pstats(un); 19838 SD_TRACE(SD_LOG_IO_PARTITION, un, 19839 "sd_handle_mchange: un:0x%p pstats created and " 19840 "set\n", un); 19841 } 19842 } 19843 19844 /* 19845 * Try to lock the door 19846 */ 19847 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 19848 SD_PATH_DIRECT_PRIORITY); 19849 failed: 19850 if (rval != 0) 19851 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19852 sd_ssc_fini(ssc); 19853 return (rval); 19854 } 19855 19856 19857 /* 19858 * Function: sd_send_scsi_DOORLOCK 19859 * 19860 * Description: Issue the scsi DOOR LOCK command 19861 * 19862 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19863 * structure for this target. 19864 * flag - SD_REMOVAL_ALLOW 19865 * SD_REMOVAL_PREVENT 19866 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19867 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19868 * to use the USCSI "direct" chain and bypass the normal 19869 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19870 * command is issued as part of an error recovery action. 19871 * 19872 * Return Code: 0 - Success 19873 * errno return code from sd_ssc_send() 19874 * 19875 * Context: Can sleep. 19876 */ 19877 19878 static int 19879 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag) 19880 { 19881 struct scsi_extended_sense sense_buf; 19882 union scsi_cdb cdb; 19883 struct uscsi_cmd ucmd_buf; 19884 int status; 19885 struct sd_lun *un; 19886 19887 ASSERT(ssc != NULL); 19888 un = ssc->ssc_un; 19889 ASSERT(un != NULL); 19890 ASSERT(!mutex_owned(SD_MUTEX(un))); 19891 19892 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19893 19894 /* already determined doorlock is not supported, fake success */ 19895 if (un->un_f_doorlock_supported == FALSE) { 19896 return (0); 19897 } 19898 19899 /* 19900 * If we are ejecting and see an SD_REMOVAL_PREVENT 19901 * ignore the command so we can complete the eject 19902 * operation. 19903 */ 19904 if (flag == SD_REMOVAL_PREVENT) { 19905 mutex_enter(SD_MUTEX(un)); 19906 if (un->un_f_ejecting == TRUE) { 19907 mutex_exit(SD_MUTEX(un)); 19908 return (EAGAIN); 19909 } 19910 mutex_exit(SD_MUTEX(un)); 19911 } 19912 19913 bzero(&cdb, sizeof (cdb)); 19914 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19915 19916 cdb.scc_cmd = SCMD_DOORLOCK; 19917 cdb.cdb_opaque[4] = (uchar_t)flag; 19918 19919 ucmd_buf.uscsi_cdb = (char *)&cdb; 19920 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19921 ucmd_buf.uscsi_bufaddr = NULL; 19922 ucmd_buf.uscsi_buflen = 0; 19923 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19924 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19925 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19926 ucmd_buf.uscsi_timeout = 15; 19927 19928 SD_TRACE(SD_LOG_IO, un, 19929 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n"); 19930 19931 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19932 UIO_SYSSPACE, path_flag); 19933 19934 if (status == 0) 19935 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19936 19937 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19938 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19939 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19940 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19941 19942 /* fake success and skip subsequent doorlock commands */ 19943 un->un_f_doorlock_supported = FALSE; 19944 return (0); 19945 } 19946 19947 return (status); 19948 } 19949 19950 /* 19951 * Function: sd_send_scsi_READ_CAPACITY 19952 * 19953 * Description: This routine uses the scsi READ CAPACITY command to determine 19954 * the device capacity in number of blocks and the device native 19955 * block size. If this function returns a failure, then the 19956 * values in *capp and *lbap are undefined. If the capacity 19957 * returned is 0xffffffff then the lun is too large for a 19958 * normal READ CAPACITY command and the results of a 19959 * READ CAPACITY 16 will be used instead. 19960 * 19961 * Arguments: ssc - ssc contains ptr to soft state struct for the target 19962 * capp - ptr to unsigned 64-bit variable to receive the 19963 * capacity value from the command. 19964 * lbap - ptr to unsigned 32-bit varaible to receive the 19965 * block size value from the command 19966 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19967 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19968 * to use the USCSI "direct" chain and bypass the normal 19969 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19970 * command is issued as part of an error recovery action. 19971 * 19972 * Return Code: 0 - Success 19973 * EIO - IO error 19974 * EACCES - Reservation conflict detected 19975 * EAGAIN - Device is becoming ready 19976 * errno return code from sd_ssc_send() 19977 * 19978 * Context: Can sleep. Blocks until command completes. 19979 */ 19980 19981 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19982 19983 static int 19984 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap, 19985 int path_flag) 19986 { 19987 struct scsi_extended_sense sense_buf; 19988 struct uscsi_cmd ucmd_buf; 19989 union scsi_cdb cdb; 19990 uint32_t *capacity_buf; 19991 uint64_t capacity; 19992 uint32_t lbasize; 19993 uint32_t pbsize; 19994 int status; 19995 struct sd_lun *un; 19996 19997 ASSERT(ssc != NULL); 19998 19999 un = ssc->ssc_un; 20000 ASSERT(un != NULL); 20001 ASSERT(!mutex_owned(SD_MUTEX(un))); 20002 ASSERT(capp != NULL); 20003 ASSERT(lbap != NULL); 20004 20005 SD_TRACE(SD_LOG_IO, un, 20006 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20007 20008 /* 20009 * First send a READ_CAPACITY command to the target. 20010 * (This command is mandatory under SCSI-2.) 20011 * 20012 * Set up the CDB for the READ_CAPACITY command. The Partial 20013 * Medium Indicator bit is cleared. The address field must be 20014 * zero if the PMI bit is zero. 20015 */ 20016 bzero(&cdb, sizeof (cdb)); 20017 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20018 20019 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 20020 20021 cdb.scc_cmd = SCMD_READ_CAPACITY; 20022 20023 ucmd_buf.uscsi_cdb = (char *)&cdb; 20024 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20025 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 20026 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 20027 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20028 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20029 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20030 ucmd_buf.uscsi_timeout = 60; 20031 20032 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20033 UIO_SYSSPACE, path_flag); 20034 20035 switch (status) { 20036 case 0: 20037 /* Return failure if we did not get valid capacity data. */ 20038 if (ucmd_buf.uscsi_resid != 0) { 20039 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20040 "sd_send_scsi_READ_CAPACITY received invalid " 20041 "capacity data"); 20042 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20043 return (EIO); 20044 } 20045 /* 20046 * Read capacity and block size from the READ CAPACITY 10 data. 20047 * This data may be adjusted later due to device specific 20048 * issues. 20049 * 20050 * According to the SCSI spec, the READ CAPACITY 10 20051 * command returns the following: 20052 * 20053 * bytes 0-3: Maximum logical block address available. 20054 * (MSB in byte:0 & LSB in byte:3) 20055 * 20056 * bytes 4-7: Block length in bytes 20057 * (MSB in byte:4 & LSB in byte:7) 20058 * 20059 */ 20060 capacity = BE_32(capacity_buf[0]); 20061 lbasize = BE_32(capacity_buf[1]); 20062 20063 /* 20064 * Done with capacity_buf 20065 */ 20066 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20067 20068 /* 20069 * if the reported capacity is set to all 0xf's, then 20070 * this disk is too large and requires SBC-2 commands. 20071 * Reissue the request using READ CAPACITY 16. 20072 */ 20073 if (capacity == 0xffffffff) { 20074 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20075 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, 20076 &lbasize, &pbsize, path_flag); 20077 if (status != 0) { 20078 return (status); 20079 } else { 20080 goto rc16_done; 20081 } 20082 } 20083 break; /* Success! */ 20084 case EIO: 20085 switch (ucmd_buf.uscsi_status) { 20086 case STATUS_RESERVATION_CONFLICT: 20087 status = EACCES; 20088 break; 20089 case STATUS_CHECK: 20090 /* 20091 * Check condition; look for ASC/ASCQ of 0x04/0x01 20092 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20093 */ 20094 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20095 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20096 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20097 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20098 return (EAGAIN); 20099 } 20100 break; 20101 default: 20102 break; 20103 } 20104 /* FALLTHRU */ 20105 default: 20106 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 20107 return (status); 20108 } 20109 20110 /* 20111 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20112 * (2352 and 0 are common) so for these devices always force the value 20113 * to 2048 as required by the ATAPI specs. 20114 */ 20115 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20116 lbasize = 2048; 20117 } 20118 20119 /* 20120 * Get the maximum LBA value from the READ CAPACITY data. 20121 * Here we assume that the Partial Medium Indicator (PMI) bit 20122 * was cleared when issuing the command. This means that the LBA 20123 * returned from the device is the LBA of the last logical block 20124 * on the logical unit. The actual logical block count will be 20125 * this value plus one. 20126 */ 20127 capacity += 1; 20128 20129 /* 20130 * Currently, for removable media, the capacity is saved in terms 20131 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 20132 */ 20133 if (un->un_f_has_removable_media) 20134 capacity *= (lbasize / un->un_sys_blocksize); 20135 20136 rc16_done: 20137 20138 /* 20139 * Copy the values from the READ CAPACITY command into the space 20140 * provided by the caller. 20141 */ 20142 *capp = capacity; 20143 *lbap = lbasize; 20144 20145 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 20146 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 20147 20148 /* 20149 * Both the lbasize and capacity from the device must be nonzero, 20150 * otherwise we assume that the values are not valid and return 20151 * failure to the caller. (4203735) 20152 */ 20153 if ((capacity == 0) || (lbasize == 0)) { 20154 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20155 "sd_send_scsi_READ_CAPACITY received invalid value " 20156 "capacity %llu lbasize %d", capacity, lbasize); 20157 return (EIO); 20158 } 20159 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20160 return (0); 20161 } 20162 20163 /* 20164 * Function: sd_send_scsi_READ_CAPACITY_16 20165 * 20166 * Description: This routine uses the scsi READ CAPACITY 16 command to 20167 * determine the device capacity in number of blocks and the 20168 * device native block size. If this function returns a failure, 20169 * then the values in *capp and *lbap are undefined. 20170 * This routine should be called by sd_send_scsi_READ_CAPACITY 20171 * which will apply any device specific adjustments to capacity 20172 * and lbasize. One exception is it is also called by 20173 * sd_get_media_info_ext. In that function, there is no need to 20174 * adjust the capacity and lbasize. 20175 * 20176 * Arguments: ssc - ssc contains ptr to soft state struct for the target 20177 * capp - ptr to unsigned 64-bit variable to receive the 20178 * capacity value from the command. 20179 * lbap - ptr to unsigned 32-bit varaible to receive the 20180 * block size value from the command 20181 * psp - ptr to unsigned 32-bit variable to receive the 20182 * physical block size value from the command 20183 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20184 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20185 * to use the USCSI "direct" chain and bypass the normal 20186 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 20187 * this command is issued as part of an error recovery 20188 * action. 20189 * 20190 * Return Code: 0 - Success 20191 * EIO - IO error 20192 * EACCES - Reservation conflict detected 20193 * EAGAIN - Device is becoming ready 20194 * errno return code from sd_ssc_send() 20195 * 20196 * Context: Can sleep. Blocks until command completes. 20197 */ 20198 20199 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 20200 20201 static int 20202 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 20203 uint32_t *lbap, uint32_t *psp, int path_flag) 20204 { 20205 struct scsi_extended_sense sense_buf; 20206 struct uscsi_cmd ucmd_buf; 20207 union scsi_cdb cdb; 20208 uint64_t *capacity16_buf; 20209 uint64_t capacity; 20210 uint32_t lbasize; 20211 uint32_t pbsize; 20212 uint32_t lbpb_exp; 20213 int status; 20214 struct sd_lun *un; 20215 20216 ASSERT(ssc != NULL); 20217 20218 un = ssc->ssc_un; 20219 ASSERT(un != NULL); 20220 ASSERT(!mutex_owned(SD_MUTEX(un))); 20221 ASSERT(capp != NULL); 20222 ASSERT(lbap != NULL); 20223 20224 SD_TRACE(SD_LOG_IO, un, 20225 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20226 20227 /* 20228 * First send a READ_CAPACITY_16 command to the target. 20229 * 20230 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 20231 * Medium Indicator bit is cleared. The address field must be 20232 * zero if the PMI bit is zero. 20233 */ 20234 bzero(&cdb, sizeof (cdb)); 20235 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20236 20237 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 20238 20239 ucmd_buf.uscsi_cdb = (char *)&cdb; 20240 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 20241 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 20242 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 20243 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20244 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20245 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20246 ucmd_buf.uscsi_timeout = 60; 20247 20248 /* 20249 * Read Capacity (16) is a Service Action In command. One 20250 * command byte (0x9E) is overloaded for multiple operations, 20251 * with the second CDB byte specifying the desired operation 20252 */ 20253 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 20254 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 20255 20256 /* 20257 * Fill in allocation length field 20258 */ 20259 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 20260 20261 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20262 UIO_SYSSPACE, path_flag); 20263 20264 switch (status) { 20265 case 0: 20266 /* Return failure if we did not get valid capacity data. */ 20267 if (ucmd_buf.uscsi_resid > 20) { 20268 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20269 "sd_send_scsi_READ_CAPACITY_16 received invalid " 20270 "capacity data"); 20271 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20272 return (EIO); 20273 } 20274 20275 /* 20276 * Read capacity and block size from the READ CAPACITY 16 data. 20277 * This data may be adjusted later due to device specific 20278 * issues. 20279 * 20280 * According to the SCSI spec, the READ CAPACITY 16 20281 * command returns the following: 20282 * 20283 * bytes 0-7: Maximum logical block address available. 20284 * (MSB in byte:0 & LSB in byte:7) 20285 * 20286 * bytes 8-11: Block length in bytes 20287 * (MSB in byte:8 & LSB in byte:11) 20288 * 20289 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT 20290 */ 20291 capacity = BE_64(capacity16_buf[0]); 20292 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 20293 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f; 20294 20295 pbsize = lbasize << lbpb_exp; 20296 20297 /* 20298 * Done with capacity16_buf 20299 */ 20300 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20301 20302 /* 20303 * if the reported capacity is set to all 0xf's, then 20304 * this disk is too large. This could only happen with 20305 * a device that supports LBAs larger than 64 bits which 20306 * are not defined by any current T10 standards. 20307 */ 20308 if (capacity == 0xffffffffffffffff) { 20309 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20310 "disk is too large"); 20311 return (EIO); 20312 } 20313 break; /* Success! */ 20314 case EIO: 20315 switch (ucmd_buf.uscsi_status) { 20316 case STATUS_RESERVATION_CONFLICT: 20317 status = EACCES; 20318 break; 20319 case STATUS_CHECK: 20320 /* 20321 * Check condition; look for ASC/ASCQ of 0x04/0x01 20322 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20323 */ 20324 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20325 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20326 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20327 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20328 return (EAGAIN); 20329 } 20330 break; 20331 default: 20332 break; 20333 } 20334 /* FALLTHRU */ 20335 default: 20336 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 20337 return (status); 20338 } 20339 20340 /* 20341 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20342 * (2352 and 0 are common) so for these devices always force the value 20343 * to 2048 as required by the ATAPI specs. 20344 */ 20345 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20346 lbasize = 2048; 20347 } 20348 20349 /* 20350 * Get the maximum LBA value from the READ CAPACITY 16 data. 20351 * Here we assume that the Partial Medium Indicator (PMI) bit 20352 * was cleared when issuing the command. This means that the LBA 20353 * returned from the device is the LBA of the last logical block 20354 * on the logical unit. The actual logical block count will be 20355 * this value plus one. 20356 */ 20357 capacity += 1; 20358 20359 /* 20360 * Currently, for removable media, the capacity is saved in terms 20361 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 20362 */ 20363 if (un->un_f_has_removable_media) 20364 capacity *= (lbasize / un->un_sys_blocksize); 20365 20366 *capp = capacity; 20367 *lbap = lbasize; 20368 *psp = pbsize; 20369 20370 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 20371 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n", 20372 capacity, lbasize, pbsize); 20373 20374 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) { 20375 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20376 "sd_send_scsi_READ_CAPACITY_16 received invalid value " 20377 "capacity %llu lbasize %d pbsize %d", capacity, lbasize); 20378 return (EIO); 20379 } 20380 20381 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20382 return (0); 20383 } 20384 20385 20386 /* 20387 * Function: sd_send_scsi_START_STOP_UNIT 20388 * 20389 * Description: Issue a scsi START STOP UNIT command to the target. 20390 * 20391 * Arguments: ssc - ssc contatins pointer to driver soft state (unit) 20392 * structure for this target. 20393 * pc_flag - SD_POWER_CONDITION 20394 * SD_START_STOP 20395 * flag - SD_TARGET_START 20396 * SD_TARGET_STOP 20397 * SD_TARGET_EJECT 20398 * SD_TARGET_CLOSE 20399 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20400 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20401 * to use the USCSI "direct" chain and bypass the normal 20402 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 20403 * command is issued as part of an error recovery action. 20404 * 20405 * Return Code: 0 - Success 20406 * EIO - IO error 20407 * EACCES - Reservation conflict detected 20408 * ENXIO - Not Ready, medium not present 20409 * errno return code from sd_ssc_send() 20410 * 20411 * Context: Can sleep. 20412 */ 20413 20414 static int 20415 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag, 20416 int path_flag) 20417 { 20418 struct scsi_extended_sense sense_buf; 20419 union scsi_cdb cdb; 20420 struct uscsi_cmd ucmd_buf; 20421 int status; 20422 struct sd_lun *un; 20423 20424 ASSERT(ssc != NULL); 20425 un = ssc->ssc_un; 20426 ASSERT(un != NULL); 20427 ASSERT(!mutex_owned(SD_MUTEX(un))); 20428 20429 SD_TRACE(SD_LOG_IO, un, 20430 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 20431 20432 if (un->un_f_check_start_stop && 20433 (pc_flag == SD_START_STOP) && 20434 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 20435 (un->un_f_start_stop_supported != TRUE)) { 20436 return (0); 20437 } 20438 20439 /* 20440 * If we are performing an eject operation and 20441 * we receive any command other than SD_TARGET_EJECT 20442 * we should immediately return. 20443 */ 20444 if (flag != SD_TARGET_EJECT) { 20445 mutex_enter(SD_MUTEX(un)); 20446 if (un->un_f_ejecting == TRUE) { 20447 mutex_exit(SD_MUTEX(un)); 20448 return (EAGAIN); 20449 } 20450 mutex_exit(SD_MUTEX(un)); 20451 } 20452 20453 bzero(&cdb, sizeof (cdb)); 20454 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20455 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20456 20457 cdb.scc_cmd = SCMD_START_STOP; 20458 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ? 20459 (uchar_t)(flag << 4) : (uchar_t)flag; 20460 20461 ucmd_buf.uscsi_cdb = (char *)&cdb; 20462 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20463 ucmd_buf.uscsi_bufaddr = NULL; 20464 ucmd_buf.uscsi_buflen = 0; 20465 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20466 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20467 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20468 ucmd_buf.uscsi_timeout = 200; 20469 20470 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20471 UIO_SYSSPACE, path_flag); 20472 20473 switch (status) { 20474 case 0: 20475 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20476 break; /* Success! */ 20477 case EIO: 20478 switch (ucmd_buf.uscsi_status) { 20479 case STATUS_RESERVATION_CONFLICT: 20480 status = EACCES; 20481 break; 20482 case STATUS_CHECK: 20483 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 20484 switch (scsi_sense_key( 20485 (uint8_t *)&sense_buf)) { 20486 case KEY_ILLEGAL_REQUEST: 20487 status = ENOTSUP; 20488 break; 20489 case KEY_NOT_READY: 20490 if (scsi_sense_asc( 20491 (uint8_t *)&sense_buf) 20492 == 0x3A) { 20493 status = ENXIO; 20494 } 20495 break; 20496 default: 20497 break; 20498 } 20499 } 20500 break; 20501 default: 20502 break; 20503 } 20504 break; 20505 default: 20506 break; 20507 } 20508 20509 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 20510 20511 return (status); 20512 } 20513 20514 20515 /* 20516 * Function: sd_start_stop_unit_callback 20517 * 20518 * Description: timeout(9F) callback to begin recovery process for a 20519 * device that has spun down. 20520 * 20521 * Arguments: arg - pointer to associated softstate struct. 20522 * 20523 * Context: Executes in a timeout(9F) thread context 20524 */ 20525 20526 static void 20527 sd_start_stop_unit_callback(void *arg) 20528 { 20529 struct sd_lun *un = arg; 20530 ASSERT(un != NULL); 20531 ASSERT(!mutex_owned(SD_MUTEX(un))); 20532 20533 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 20534 20535 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 20536 } 20537 20538 20539 /* 20540 * Function: sd_start_stop_unit_task 20541 * 20542 * Description: Recovery procedure when a drive is spun down. 20543 * 20544 * Arguments: arg - pointer to associated softstate struct. 20545 * 20546 * Context: Executes in a taskq() thread context 20547 */ 20548 20549 static void 20550 sd_start_stop_unit_task(void *arg) 20551 { 20552 struct sd_lun *un = arg; 20553 sd_ssc_t *ssc; 20554 int power_level; 20555 int rval; 20556 20557 ASSERT(un != NULL); 20558 ASSERT(!mutex_owned(SD_MUTEX(un))); 20559 20560 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 20561 20562 /* 20563 * Some unformatted drives report not ready error, no need to 20564 * restart if format has been initiated. 20565 */ 20566 mutex_enter(SD_MUTEX(un)); 20567 if (un->un_f_format_in_progress == TRUE) { 20568 mutex_exit(SD_MUTEX(un)); 20569 return; 20570 } 20571 mutex_exit(SD_MUTEX(un)); 20572 20573 ssc = sd_ssc_init(un); 20574 /* 20575 * When a START STOP command is issued from here, it is part of a 20576 * failure recovery operation and must be issued before any other 20577 * commands, including any pending retries. Thus it must be sent 20578 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 20579 * succeeds or not, we will start I/O after the attempt. 20580 * If power condition is supported and the current power level 20581 * is capable of performing I/O, we should set the power condition 20582 * to that level. Otherwise, set the power condition to ACTIVE. 20583 */ 20584 if (un->un_f_power_condition_supported) { 20585 mutex_enter(SD_MUTEX(un)); 20586 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level)); 20587 power_level = sd_pwr_pc.ran_perf[un->un_power_level] 20588 > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE; 20589 mutex_exit(SD_MUTEX(un)); 20590 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 20591 sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY); 20592 } else { 20593 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 20594 SD_TARGET_START, SD_PATH_DIRECT_PRIORITY); 20595 } 20596 20597 if (rval != 0) 20598 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20599 sd_ssc_fini(ssc); 20600 /* 20601 * The above call blocks until the START_STOP_UNIT command completes. 20602 * Now that it has completed, we must re-try the original IO that 20603 * received the NOT READY condition in the first place. There are 20604 * three possible conditions here: 20605 * 20606 * (1) The original IO is on un_retry_bp. 20607 * (2) The original IO is on the regular wait queue, and un_retry_bp 20608 * is NULL. 20609 * (3) The original IO is on the regular wait queue, and un_retry_bp 20610 * points to some other, unrelated bp. 20611 * 20612 * For each case, we must call sd_start_cmds() with un_retry_bp 20613 * as the argument. If un_retry_bp is NULL, this will initiate 20614 * processing of the regular wait queue. If un_retry_bp is not NULL, 20615 * then this will process the bp on un_retry_bp. That may or may not 20616 * be the original IO, but that does not matter: the important thing 20617 * is to keep the IO processing going at this point. 20618 * 20619 * Note: This is a very specific error recovery sequence associated 20620 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20621 * serialize the I/O with completion of the spin-up. 20622 */ 20623 mutex_enter(SD_MUTEX(un)); 20624 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20625 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20626 un, un->un_retry_bp); 20627 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20628 sd_start_cmds(un, un->un_retry_bp); 20629 mutex_exit(SD_MUTEX(un)); 20630 20631 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20632 } 20633 20634 20635 /* 20636 * Function: sd_send_scsi_INQUIRY 20637 * 20638 * Description: Issue the scsi INQUIRY command. 20639 * 20640 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20641 * structure for this target. 20642 * bufaddr 20643 * buflen 20644 * evpd 20645 * page_code 20646 * page_length 20647 * 20648 * Return Code: 0 - Success 20649 * errno return code from sd_ssc_send() 20650 * 20651 * Context: Can sleep. Does not return until command is completed. 20652 */ 20653 20654 static int 20655 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen, 20656 uchar_t evpd, uchar_t page_code, size_t *residp) 20657 { 20658 union scsi_cdb cdb; 20659 struct uscsi_cmd ucmd_buf; 20660 int status; 20661 struct sd_lun *un; 20662 20663 ASSERT(ssc != NULL); 20664 un = ssc->ssc_un; 20665 ASSERT(un != NULL); 20666 ASSERT(!mutex_owned(SD_MUTEX(un))); 20667 ASSERT(bufaddr != NULL); 20668 20669 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20670 20671 bzero(&cdb, sizeof (cdb)); 20672 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20673 bzero(bufaddr, buflen); 20674 20675 cdb.scc_cmd = SCMD_INQUIRY; 20676 cdb.cdb_opaque[1] = evpd; 20677 cdb.cdb_opaque[2] = page_code; 20678 FORMG0COUNT(&cdb, buflen); 20679 20680 ucmd_buf.uscsi_cdb = (char *)&cdb; 20681 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20682 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20683 ucmd_buf.uscsi_buflen = buflen; 20684 ucmd_buf.uscsi_rqbuf = NULL; 20685 ucmd_buf.uscsi_rqlen = 0; 20686 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20687 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20688 20689 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20690 UIO_SYSSPACE, SD_PATH_DIRECT); 20691 20692 /* 20693 * Only handle status == 0, the upper-level caller 20694 * will put different assessment based on the context. 20695 */ 20696 if (status == 0) 20697 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20698 20699 if ((status == 0) && (residp != NULL)) { 20700 *residp = ucmd_buf.uscsi_resid; 20701 } 20702 20703 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20704 20705 return (status); 20706 } 20707 20708 20709 /* 20710 * Function: sd_send_scsi_TEST_UNIT_READY 20711 * 20712 * Description: Issue the scsi TEST UNIT READY command. 20713 * This routine can be told to set the flag USCSI_DIAGNOSE to 20714 * prevent retrying failed commands. Use this when the intent 20715 * is either to check for device readiness, to clear a Unit 20716 * Attention, or to clear any outstanding sense data. 20717 * However under specific conditions the expected behavior 20718 * is for retries to bring a device ready, so use the flag 20719 * with caution. 20720 * 20721 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20722 * structure for this target. 20723 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20724 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20725 * 0: dont check for media present, do retries on cmd. 20726 * 20727 * Return Code: 0 - Success 20728 * EIO - IO error 20729 * EACCES - Reservation conflict detected 20730 * ENXIO - Not Ready, medium not present 20731 * errno return code from sd_ssc_send() 20732 * 20733 * Context: Can sleep. Does not return until command is completed. 20734 */ 20735 20736 static int 20737 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag) 20738 { 20739 struct scsi_extended_sense sense_buf; 20740 union scsi_cdb cdb; 20741 struct uscsi_cmd ucmd_buf; 20742 int status; 20743 struct sd_lun *un; 20744 20745 ASSERT(ssc != NULL); 20746 un = ssc->ssc_un; 20747 ASSERT(un != NULL); 20748 ASSERT(!mutex_owned(SD_MUTEX(un))); 20749 20750 SD_TRACE(SD_LOG_IO, un, 20751 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20752 20753 /* 20754 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20755 * timeouts when they receive a TUR and the queue is not empty. Check 20756 * the configuration flag set during attach (indicating the drive has 20757 * this firmware bug) and un_ncmds_in_transport before issuing the 20758 * TUR. If there are 20759 * pending commands return success, this is a bit arbitrary but is ok 20760 * for non-removables (i.e. the eliteI disks) and non-clustering 20761 * configurations. 20762 */ 20763 if (un->un_f_cfg_tur_check == TRUE) { 20764 mutex_enter(SD_MUTEX(un)); 20765 if (un->un_ncmds_in_transport != 0) { 20766 mutex_exit(SD_MUTEX(un)); 20767 return (0); 20768 } 20769 mutex_exit(SD_MUTEX(un)); 20770 } 20771 20772 bzero(&cdb, sizeof (cdb)); 20773 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20774 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20775 20776 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20777 20778 ucmd_buf.uscsi_cdb = (char *)&cdb; 20779 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20780 ucmd_buf.uscsi_bufaddr = NULL; 20781 ucmd_buf.uscsi_buflen = 0; 20782 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20783 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20784 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20785 20786 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20787 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20788 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20789 } 20790 ucmd_buf.uscsi_timeout = 60; 20791 20792 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20793 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 20794 SD_PATH_STANDARD)); 20795 20796 switch (status) { 20797 case 0: 20798 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20799 break; /* Success! */ 20800 case EIO: 20801 switch (ucmd_buf.uscsi_status) { 20802 case STATUS_RESERVATION_CONFLICT: 20803 status = EACCES; 20804 break; 20805 case STATUS_CHECK: 20806 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20807 break; 20808 } 20809 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20810 (scsi_sense_key((uint8_t *)&sense_buf) == 20811 KEY_NOT_READY) && 20812 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20813 status = ENXIO; 20814 } 20815 break; 20816 default: 20817 break; 20818 } 20819 break; 20820 default: 20821 break; 20822 } 20823 20824 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20825 20826 return (status); 20827 } 20828 20829 /* 20830 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20831 * 20832 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20833 * 20834 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20835 * structure for this target. 20836 * 20837 * Return Code: 0 - Success 20838 * EACCES 20839 * ENOTSUP 20840 * errno return code from sd_ssc_send() 20841 * 20842 * Context: Can sleep. Does not return until command is completed. 20843 */ 20844 20845 static int 20846 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd, 20847 uint16_t data_len, uchar_t *data_bufp) 20848 { 20849 struct scsi_extended_sense sense_buf; 20850 union scsi_cdb cdb; 20851 struct uscsi_cmd ucmd_buf; 20852 int status; 20853 int no_caller_buf = FALSE; 20854 struct sd_lun *un; 20855 20856 ASSERT(ssc != NULL); 20857 un = ssc->ssc_un; 20858 ASSERT(un != NULL); 20859 ASSERT(!mutex_owned(SD_MUTEX(un))); 20860 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20861 20862 SD_TRACE(SD_LOG_IO, un, 20863 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20864 20865 bzero(&cdb, sizeof (cdb)); 20866 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20867 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20868 if (data_bufp == NULL) { 20869 /* Allocate a default buf if the caller did not give one */ 20870 ASSERT(data_len == 0); 20871 data_len = MHIOC_RESV_KEY_SIZE; 20872 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20873 no_caller_buf = TRUE; 20874 } 20875 20876 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20877 cdb.cdb_opaque[1] = usr_cmd; 20878 FORMG1COUNT(&cdb, data_len); 20879 20880 ucmd_buf.uscsi_cdb = (char *)&cdb; 20881 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20882 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20883 ucmd_buf.uscsi_buflen = data_len; 20884 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20885 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20886 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20887 ucmd_buf.uscsi_timeout = 60; 20888 20889 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20890 UIO_SYSSPACE, SD_PATH_STANDARD); 20891 20892 switch (status) { 20893 case 0: 20894 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20895 20896 break; /* Success! */ 20897 case EIO: 20898 switch (ucmd_buf.uscsi_status) { 20899 case STATUS_RESERVATION_CONFLICT: 20900 status = EACCES; 20901 break; 20902 case STATUS_CHECK: 20903 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20904 (scsi_sense_key((uint8_t *)&sense_buf) == 20905 KEY_ILLEGAL_REQUEST)) { 20906 status = ENOTSUP; 20907 } 20908 break; 20909 default: 20910 break; 20911 } 20912 break; 20913 default: 20914 break; 20915 } 20916 20917 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20918 20919 if (no_caller_buf == TRUE) { 20920 kmem_free(data_bufp, data_len); 20921 } 20922 20923 return (status); 20924 } 20925 20926 20927 /* 20928 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20929 * 20930 * Description: This routine is the driver entry point for handling CD-ROM 20931 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20932 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20933 * device. 20934 * 20935 * Arguments: ssc - ssc contains un - pointer to soft state struct 20936 * for the target. 20937 * usr_cmd SCSI-3 reservation facility command (one of 20938 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20939 * SD_SCSI3_PREEMPTANDABORT) 20940 * usr_bufp - user provided pointer register, reserve descriptor or 20941 * preempt and abort structure (mhioc_register_t, 20942 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20943 * 20944 * Return Code: 0 - Success 20945 * EACCES 20946 * ENOTSUP 20947 * errno return code from sd_ssc_send() 20948 * 20949 * Context: Can sleep. Does not return until command is completed. 20950 */ 20951 20952 static int 20953 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd, 20954 uchar_t *usr_bufp) 20955 { 20956 struct scsi_extended_sense sense_buf; 20957 union scsi_cdb cdb; 20958 struct uscsi_cmd ucmd_buf; 20959 int status; 20960 uchar_t data_len = sizeof (sd_prout_t); 20961 sd_prout_t *prp; 20962 struct sd_lun *un; 20963 20964 ASSERT(ssc != NULL); 20965 un = ssc->ssc_un; 20966 ASSERT(un != NULL); 20967 ASSERT(!mutex_owned(SD_MUTEX(un))); 20968 ASSERT(data_len == 24); /* required by scsi spec */ 20969 20970 SD_TRACE(SD_LOG_IO, un, 20971 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20972 20973 if (usr_bufp == NULL) { 20974 return (EINVAL); 20975 } 20976 20977 bzero(&cdb, sizeof (cdb)); 20978 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20979 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20980 prp = kmem_zalloc(data_len, KM_SLEEP); 20981 20982 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20983 cdb.cdb_opaque[1] = usr_cmd; 20984 FORMG1COUNT(&cdb, data_len); 20985 20986 ucmd_buf.uscsi_cdb = (char *)&cdb; 20987 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20988 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20989 ucmd_buf.uscsi_buflen = data_len; 20990 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20991 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20992 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20993 ucmd_buf.uscsi_timeout = 60; 20994 20995 switch (usr_cmd) { 20996 case SD_SCSI3_REGISTER: { 20997 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20998 20999 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21000 bcopy(ptr->newkey.key, prp->service_key, 21001 MHIOC_RESV_KEY_SIZE); 21002 prp->aptpl = ptr->aptpl; 21003 break; 21004 } 21005 case SD_SCSI3_RESERVE: 21006 case SD_SCSI3_RELEASE: { 21007 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 21008 21009 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21010 prp->scope_address = BE_32(ptr->scope_specific_addr); 21011 cdb.cdb_opaque[2] = ptr->type; 21012 break; 21013 } 21014 case SD_SCSI3_PREEMPTANDABORT: { 21015 mhioc_preemptandabort_t *ptr = 21016 (mhioc_preemptandabort_t *)usr_bufp; 21017 21018 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 21019 bcopy(ptr->victim_key.key, prp->service_key, 21020 MHIOC_RESV_KEY_SIZE); 21021 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 21022 cdb.cdb_opaque[2] = ptr->resvdesc.type; 21023 ucmd_buf.uscsi_flags |= USCSI_HEAD; 21024 break; 21025 } 21026 case SD_SCSI3_REGISTERANDIGNOREKEY: 21027 { 21028 mhioc_registerandignorekey_t *ptr; 21029 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 21030 bcopy(ptr->newkey.key, 21031 prp->service_key, MHIOC_RESV_KEY_SIZE); 21032 prp->aptpl = ptr->aptpl; 21033 break; 21034 } 21035 default: 21036 ASSERT(FALSE); 21037 break; 21038 } 21039 21040 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21041 UIO_SYSSPACE, SD_PATH_STANDARD); 21042 21043 switch (status) { 21044 case 0: 21045 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21046 break; /* Success! */ 21047 case EIO: 21048 switch (ucmd_buf.uscsi_status) { 21049 case STATUS_RESERVATION_CONFLICT: 21050 status = EACCES; 21051 break; 21052 case STATUS_CHECK: 21053 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21054 (scsi_sense_key((uint8_t *)&sense_buf) == 21055 KEY_ILLEGAL_REQUEST)) { 21056 status = ENOTSUP; 21057 } 21058 break; 21059 default: 21060 break; 21061 } 21062 break; 21063 default: 21064 break; 21065 } 21066 21067 kmem_free(prp, data_len); 21068 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 21069 return (status); 21070 } 21071 21072 21073 /* 21074 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 21075 * 21076 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 21077 * 21078 * Arguments: un - pointer to the target's soft state struct 21079 * dkc - pointer to the callback structure 21080 * 21081 * Return Code: 0 - success 21082 * errno-type error code 21083 * 21084 * Context: kernel thread context only. 21085 * 21086 * _______________________________________________________________ 21087 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 21088 * |FLUSH_VOLATILE| | operation | 21089 * |______________|______________|_________________________________| 21090 * | 0 | NULL | Synchronous flush on both | 21091 * | | | volatile and non-volatile cache | 21092 * |______________|______________|_________________________________| 21093 * | 1 | NULL | Synchronous flush on volatile | 21094 * | | | cache; disk drivers may suppress| 21095 * | | | flush if disk table indicates | 21096 * | | | non-volatile cache | 21097 * |______________|______________|_________________________________| 21098 * | 0 | !NULL | Asynchronous flush on both | 21099 * | | | volatile and non-volatile cache;| 21100 * |______________|______________|_________________________________| 21101 * | 1 | !NULL | Asynchronous flush on volatile | 21102 * | | | cache; disk drivers may suppress| 21103 * | | | flush if disk table indicates | 21104 * | | | non-volatile cache | 21105 * |______________|______________|_________________________________| 21106 * 21107 */ 21108 21109 static int 21110 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 21111 { 21112 struct sd_uscsi_info *uip; 21113 struct uscsi_cmd *uscmd; 21114 union scsi_cdb *cdb; 21115 struct buf *bp; 21116 int rval = 0; 21117 int is_async; 21118 21119 SD_TRACE(SD_LOG_IO, un, 21120 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 21121 21122 ASSERT(un != NULL); 21123 ASSERT(!mutex_owned(SD_MUTEX(un))); 21124 21125 if (dkc == NULL || dkc->dkc_callback == NULL) { 21126 is_async = FALSE; 21127 } else { 21128 is_async = TRUE; 21129 } 21130 21131 mutex_enter(SD_MUTEX(un)); 21132 /* check whether cache flush should be suppressed */ 21133 if (un->un_f_suppress_cache_flush == TRUE) { 21134 mutex_exit(SD_MUTEX(un)); 21135 /* 21136 * suppress the cache flush if the device is told to do 21137 * so by sd.conf or disk table 21138 */ 21139 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 21140 skip the cache flush since suppress_cache_flush is %d!\n", 21141 un->un_f_suppress_cache_flush); 21142 21143 if (is_async == TRUE) { 21144 /* invoke callback for asynchronous flush */ 21145 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 21146 } 21147 return (rval); 21148 } 21149 mutex_exit(SD_MUTEX(un)); 21150 21151 /* 21152 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 21153 * set properly 21154 */ 21155 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 21156 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 21157 21158 mutex_enter(SD_MUTEX(un)); 21159 if (dkc != NULL && un->un_f_sync_nv_supported && 21160 (dkc->dkc_flag & FLUSH_VOLATILE)) { 21161 /* 21162 * if the device supports SYNC_NV bit, turn on 21163 * the SYNC_NV bit to only flush volatile cache 21164 */ 21165 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 21166 } 21167 mutex_exit(SD_MUTEX(un)); 21168 21169 /* 21170 * First get some memory for the uscsi_cmd struct and cdb 21171 * and initialize for SYNCHRONIZE_CACHE cmd. 21172 */ 21173 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21174 uscmd->uscsi_cdblen = CDB_GROUP1; 21175 uscmd->uscsi_cdb = (caddr_t)cdb; 21176 uscmd->uscsi_bufaddr = NULL; 21177 uscmd->uscsi_buflen = 0; 21178 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 21179 uscmd->uscsi_rqlen = SENSE_LENGTH; 21180 uscmd->uscsi_rqresid = SENSE_LENGTH; 21181 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 21182 uscmd->uscsi_timeout = sd_io_time; 21183 21184 /* 21185 * Allocate an sd_uscsi_info struct and fill it with the info 21186 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 21187 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 21188 * since we allocate the buf here in this function, we do not 21189 * need to preserve the prior contents of b_private. 21190 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 21191 */ 21192 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 21193 uip->ui_flags = SD_PATH_DIRECT; 21194 uip->ui_cmdp = uscmd; 21195 21196 bp = getrbuf(KM_SLEEP); 21197 bp->b_private = uip; 21198 21199 /* 21200 * Setup buffer to carry uscsi request. 21201 */ 21202 bp->b_flags = B_BUSY; 21203 bp->b_bcount = 0; 21204 bp->b_blkno = 0; 21205 21206 if (is_async == TRUE) { 21207 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 21208 uip->ui_dkc = *dkc; 21209 } 21210 21211 bp->b_edev = SD_GET_DEV(un); 21212 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 21213 21214 /* 21215 * Unset un_f_sync_cache_required flag 21216 */ 21217 mutex_enter(SD_MUTEX(un)); 21218 un->un_f_sync_cache_required = FALSE; 21219 mutex_exit(SD_MUTEX(un)); 21220 21221 (void) sd_uscsi_strategy(bp); 21222 21223 /* 21224 * If synchronous request, wait for completion 21225 * If async just return and let b_iodone callback 21226 * cleanup. 21227 * NOTE: On return, u_ncmds_in_driver will be decremented, 21228 * but it was also incremented in sd_uscsi_strategy(), so 21229 * we should be ok. 21230 */ 21231 if (is_async == FALSE) { 21232 (void) biowait(bp); 21233 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 21234 } 21235 21236 return (rval); 21237 } 21238 21239 21240 static int 21241 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 21242 { 21243 struct sd_uscsi_info *uip; 21244 struct uscsi_cmd *uscmd; 21245 uint8_t *sense_buf; 21246 struct sd_lun *un; 21247 int status; 21248 union scsi_cdb *cdb; 21249 21250 uip = (struct sd_uscsi_info *)(bp->b_private); 21251 ASSERT(uip != NULL); 21252 21253 uscmd = uip->ui_cmdp; 21254 ASSERT(uscmd != NULL); 21255 21256 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 21257 ASSERT(sense_buf != NULL); 21258 21259 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 21260 ASSERT(un != NULL); 21261 21262 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 21263 21264 status = geterror(bp); 21265 switch (status) { 21266 case 0: 21267 break; /* Success! */ 21268 case EIO: 21269 switch (uscmd->uscsi_status) { 21270 case STATUS_RESERVATION_CONFLICT: 21271 /* Ignore reservation conflict */ 21272 status = 0; 21273 goto done; 21274 21275 case STATUS_CHECK: 21276 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 21277 (scsi_sense_key(sense_buf) == 21278 KEY_ILLEGAL_REQUEST)) { 21279 /* Ignore Illegal Request error */ 21280 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) { 21281 mutex_enter(SD_MUTEX(un)); 21282 un->un_f_sync_nv_supported = FALSE; 21283 mutex_exit(SD_MUTEX(un)); 21284 status = 0; 21285 SD_TRACE(SD_LOG_IO, un, 21286 "un_f_sync_nv_supported \ 21287 is set to false.\n"); 21288 goto done; 21289 } 21290 21291 mutex_enter(SD_MUTEX(un)); 21292 un->un_f_sync_cache_supported = FALSE; 21293 mutex_exit(SD_MUTEX(un)); 21294 SD_TRACE(SD_LOG_IO, un, 21295 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 21296 un_f_sync_cache_supported set to false \ 21297 with asc = %x, ascq = %x\n", 21298 scsi_sense_asc(sense_buf), 21299 scsi_sense_ascq(sense_buf)); 21300 status = ENOTSUP; 21301 goto done; 21302 } 21303 break; 21304 default: 21305 break; 21306 } 21307 /* FALLTHRU */ 21308 default: 21309 /* 21310 * Turn on the un_f_sync_cache_required flag 21311 * since the SYNC CACHE command failed 21312 */ 21313 mutex_enter(SD_MUTEX(un)); 21314 un->un_f_sync_cache_required = TRUE; 21315 mutex_exit(SD_MUTEX(un)); 21316 21317 /* 21318 * Don't log an error message if this device 21319 * has removable media. 21320 */ 21321 if (!un->un_f_has_removable_media) { 21322 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 21323 "SYNCHRONIZE CACHE command failed (%d)\n", status); 21324 } 21325 break; 21326 } 21327 21328 done: 21329 if (uip->ui_dkc.dkc_callback != NULL) { 21330 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 21331 } 21332 21333 ASSERT((bp->b_flags & B_REMAPPED) == 0); 21334 freerbuf(bp); 21335 kmem_free(uip, sizeof (struct sd_uscsi_info)); 21336 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 21337 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 21338 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 21339 21340 return (status); 21341 } 21342 21343 21344 /* 21345 * Function: sd_send_scsi_GET_CONFIGURATION 21346 * 21347 * Description: Issues the get configuration command to the device. 21348 * Called from sd_check_for_writable_cd & sd_get_media_info 21349 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 21350 * Arguments: ssc 21351 * ucmdbuf 21352 * rqbuf 21353 * rqbuflen 21354 * bufaddr 21355 * buflen 21356 * path_flag 21357 * 21358 * Return Code: 0 - Success 21359 * errno return code from sd_ssc_send() 21360 * 21361 * Context: Can sleep. Does not return until command is completed. 21362 * 21363 */ 21364 21365 static int 21366 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf, 21367 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 21368 int path_flag) 21369 { 21370 char cdb[CDB_GROUP1]; 21371 int status; 21372 struct sd_lun *un; 21373 21374 ASSERT(ssc != NULL); 21375 un = ssc->ssc_un; 21376 ASSERT(un != NULL); 21377 ASSERT(!mutex_owned(SD_MUTEX(un))); 21378 ASSERT(bufaddr != NULL); 21379 ASSERT(ucmdbuf != NULL); 21380 ASSERT(rqbuf != NULL); 21381 21382 SD_TRACE(SD_LOG_IO, un, 21383 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 21384 21385 bzero(cdb, sizeof (cdb)); 21386 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21387 bzero(rqbuf, rqbuflen); 21388 bzero(bufaddr, buflen); 21389 21390 /* 21391 * Set up cdb field for the get configuration command. 21392 */ 21393 cdb[0] = SCMD_GET_CONFIGURATION; 21394 cdb[1] = 0x02; /* Requested Type */ 21395 cdb[8] = SD_PROFILE_HEADER_LEN; 21396 ucmdbuf->uscsi_cdb = cdb; 21397 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21398 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21399 ucmdbuf->uscsi_buflen = buflen; 21400 ucmdbuf->uscsi_timeout = sd_io_time; 21401 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21402 ucmdbuf->uscsi_rqlen = rqbuflen; 21403 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21404 21405 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21406 UIO_SYSSPACE, path_flag); 21407 21408 switch (status) { 21409 case 0: 21410 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21411 break; /* Success! */ 21412 case EIO: 21413 switch (ucmdbuf->uscsi_status) { 21414 case STATUS_RESERVATION_CONFLICT: 21415 status = EACCES; 21416 break; 21417 default: 21418 break; 21419 } 21420 break; 21421 default: 21422 break; 21423 } 21424 21425 if (status == 0) { 21426 SD_DUMP_MEMORY(un, SD_LOG_IO, 21427 "sd_send_scsi_GET_CONFIGURATION: data", 21428 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21429 } 21430 21431 SD_TRACE(SD_LOG_IO, un, 21432 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 21433 21434 return (status); 21435 } 21436 21437 /* 21438 * Function: sd_send_scsi_feature_GET_CONFIGURATION 21439 * 21440 * Description: Issues the get configuration command to the device to 21441 * retrieve a specific feature. Called from 21442 * sd_check_for_writable_cd & sd_set_mmc_caps. 21443 * Arguments: ssc 21444 * ucmdbuf 21445 * rqbuf 21446 * rqbuflen 21447 * bufaddr 21448 * buflen 21449 * feature 21450 * 21451 * Return Code: 0 - Success 21452 * errno return code from sd_ssc_send() 21453 * 21454 * Context: Can sleep. Does not return until command is completed. 21455 * 21456 */ 21457 static int 21458 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 21459 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 21460 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 21461 { 21462 char cdb[CDB_GROUP1]; 21463 int status; 21464 struct sd_lun *un; 21465 21466 ASSERT(ssc != NULL); 21467 un = ssc->ssc_un; 21468 ASSERT(un != NULL); 21469 ASSERT(!mutex_owned(SD_MUTEX(un))); 21470 ASSERT(bufaddr != NULL); 21471 ASSERT(ucmdbuf != NULL); 21472 ASSERT(rqbuf != NULL); 21473 21474 SD_TRACE(SD_LOG_IO, un, 21475 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 21476 21477 bzero(cdb, sizeof (cdb)); 21478 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21479 bzero(rqbuf, rqbuflen); 21480 bzero(bufaddr, buflen); 21481 21482 /* 21483 * Set up cdb field for the get configuration command. 21484 */ 21485 cdb[0] = SCMD_GET_CONFIGURATION; 21486 cdb[1] = 0x02; /* Requested Type */ 21487 cdb[3] = feature; 21488 cdb[8] = buflen; 21489 ucmdbuf->uscsi_cdb = cdb; 21490 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21491 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21492 ucmdbuf->uscsi_buflen = buflen; 21493 ucmdbuf->uscsi_timeout = sd_io_time; 21494 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21495 ucmdbuf->uscsi_rqlen = rqbuflen; 21496 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21497 21498 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21499 UIO_SYSSPACE, path_flag); 21500 21501 switch (status) { 21502 case 0: 21503 21504 break; /* Success! */ 21505 case EIO: 21506 switch (ucmdbuf->uscsi_status) { 21507 case STATUS_RESERVATION_CONFLICT: 21508 status = EACCES; 21509 break; 21510 default: 21511 break; 21512 } 21513 break; 21514 default: 21515 break; 21516 } 21517 21518 if (status == 0) { 21519 SD_DUMP_MEMORY(un, SD_LOG_IO, 21520 "sd_send_scsi_feature_GET_CONFIGURATION: data", 21521 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21522 } 21523 21524 SD_TRACE(SD_LOG_IO, un, 21525 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 21526 21527 return (status); 21528 } 21529 21530 21531 /* 21532 * Function: sd_send_scsi_MODE_SENSE 21533 * 21534 * Description: Utility function for issuing a scsi MODE SENSE command. 21535 * Note: This routine uses a consistent implementation for Group0, 21536 * Group1, and Group2 commands across all platforms. ATAPI devices 21537 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21538 * 21539 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21540 * structure for this target. 21541 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21542 * CDB_GROUP[1|2] (10 byte). 21543 * bufaddr - buffer for page data retrieved from the target. 21544 * buflen - size of page to be retrieved. 21545 * page_code - page code of data to be retrieved from the target. 21546 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21547 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21548 * to use the USCSI "direct" chain and bypass the normal 21549 * command waitq. 21550 * 21551 * Return Code: 0 - Success 21552 * errno return code from sd_ssc_send() 21553 * 21554 * Context: Can sleep. Does not return until command is completed. 21555 */ 21556 21557 static int 21558 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21559 size_t buflen, uchar_t page_code, int path_flag) 21560 { 21561 struct scsi_extended_sense sense_buf; 21562 union scsi_cdb cdb; 21563 struct uscsi_cmd ucmd_buf; 21564 int status; 21565 int headlen; 21566 struct sd_lun *un; 21567 21568 ASSERT(ssc != NULL); 21569 un = ssc->ssc_un; 21570 ASSERT(un != NULL); 21571 ASSERT(!mutex_owned(SD_MUTEX(un))); 21572 ASSERT(bufaddr != NULL); 21573 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21574 (cdbsize == CDB_GROUP2)); 21575 21576 SD_TRACE(SD_LOG_IO, un, 21577 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 21578 21579 bzero(&cdb, sizeof (cdb)); 21580 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21581 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21582 bzero(bufaddr, buflen); 21583 21584 if (cdbsize == CDB_GROUP0) { 21585 cdb.scc_cmd = SCMD_MODE_SENSE; 21586 cdb.cdb_opaque[2] = page_code; 21587 FORMG0COUNT(&cdb, buflen); 21588 headlen = MODE_HEADER_LENGTH; 21589 } else { 21590 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 21591 cdb.cdb_opaque[2] = page_code; 21592 FORMG1COUNT(&cdb, buflen); 21593 headlen = MODE_HEADER_LENGTH_GRP2; 21594 } 21595 21596 ASSERT(headlen <= buflen); 21597 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21598 21599 ucmd_buf.uscsi_cdb = (char *)&cdb; 21600 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21601 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21602 ucmd_buf.uscsi_buflen = buflen; 21603 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21604 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21605 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21606 ucmd_buf.uscsi_timeout = 60; 21607 21608 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21609 UIO_SYSSPACE, path_flag); 21610 21611 switch (status) { 21612 case 0: 21613 /* 21614 * sr_check_wp() uses 0x3f page code and check the header of 21615 * mode page to determine if target device is write-protected. 21616 * But some USB devices return 0 bytes for 0x3f page code. For 21617 * this case, make sure that mode page header is returned at 21618 * least. 21619 */ 21620 if (buflen - ucmd_buf.uscsi_resid < headlen) { 21621 status = EIO; 21622 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 21623 "mode page header is not returned"); 21624 } 21625 break; /* Success! */ 21626 case EIO: 21627 switch (ucmd_buf.uscsi_status) { 21628 case STATUS_RESERVATION_CONFLICT: 21629 status = EACCES; 21630 break; 21631 default: 21632 break; 21633 } 21634 break; 21635 default: 21636 break; 21637 } 21638 21639 if (status == 0) { 21640 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 21641 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21642 } 21643 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 21644 21645 return (status); 21646 } 21647 21648 21649 /* 21650 * Function: sd_send_scsi_MODE_SELECT 21651 * 21652 * Description: Utility function for issuing a scsi MODE SELECT command. 21653 * Note: This routine uses a consistent implementation for Group0, 21654 * Group1, and Group2 commands across all platforms. ATAPI devices 21655 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21656 * 21657 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21658 * structure for this target. 21659 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21660 * CDB_GROUP[1|2] (10 byte). 21661 * bufaddr - buffer for page data retrieved from the target. 21662 * buflen - size of page to be retrieved. 21663 * save_page - boolean to determin if SP bit should be set. 21664 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21665 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21666 * to use the USCSI "direct" chain and bypass the normal 21667 * command waitq. 21668 * 21669 * Return Code: 0 - Success 21670 * errno return code from sd_ssc_send() 21671 * 21672 * Context: Can sleep. Does not return until command is completed. 21673 */ 21674 21675 static int 21676 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21677 size_t buflen, uchar_t save_page, int path_flag) 21678 { 21679 struct scsi_extended_sense sense_buf; 21680 union scsi_cdb cdb; 21681 struct uscsi_cmd ucmd_buf; 21682 int status; 21683 struct sd_lun *un; 21684 21685 ASSERT(ssc != NULL); 21686 un = ssc->ssc_un; 21687 ASSERT(un != NULL); 21688 ASSERT(!mutex_owned(SD_MUTEX(un))); 21689 ASSERT(bufaddr != NULL); 21690 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21691 (cdbsize == CDB_GROUP2)); 21692 21693 SD_TRACE(SD_LOG_IO, un, 21694 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 21695 21696 bzero(&cdb, sizeof (cdb)); 21697 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21698 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21699 21700 /* Set the PF bit for many third party drives */ 21701 cdb.cdb_opaque[1] = 0x10; 21702 21703 /* Set the savepage(SP) bit if given */ 21704 if (save_page == SD_SAVE_PAGE) { 21705 cdb.cdb_opaque[1] |= 0x01; 21706 } 21707 21708 if (cdbsize == CDB_GROUP0) { 21709 cdb.scc_cmd = SCMD_MODE_SELECT; 21710 FORMG0COUNT(&cdb, buflen); 21711 } else { 21712 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 21713 FORMG1COUNT(&cdb, buflen); 21714 } 21715 21716 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21717 21718 ucmd_buf.uscsi_cdb = (char *)&cdb; 21719 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21720 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21721 ucmd_buf.uscsi_buflen = buflen; 21722 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21723 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21724 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 21725 ucmd_buf.uscsi_timeout = 60; 21726 21727 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21728 UIO_SYSSPACE, path_flag); 21729 21730 switch (status) { 21731 case 0: 21732 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21733 break; /* Success! */ 21734 case EIO: 21735 switch (ucmd_buf.uscsi_status) { 21736 case STATUS_RESERVATION_CONFLICT: 21737 status = EACCES; 21738 break; 21739 default: 21740 break; 21741 } 21742 break; 21743 default: 21744 break; 21745 } 21746 21747 if (status == 0) { 21748 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21749 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21750 } 21751 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21752 21753 return (status); 21754 } 21755 21756 21757 /* 21758 * Function: sd_send_scsi_RDWR 21759 * 21760 * Description: Issue a scsi READ or WRITE command with the given parameters. 21761 * 21762 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21763 * structure for this target. 21764 * cmd: SCMD_READ or SCMD_WRITE 21765 * bufaddr: Address of caller's buffer to receive the RDWR data 21766 * buflen: Length of caller's buffer receive the RDWR data. 21767 * start_block: Block number for the start of the RDWR operation. 21768 * (Assumes target-native block size.) 21769 * residp: Pointer to variable to receive the redisual of the 21770 * RDWR operation (may be NULL of no residual requested). 21771 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21772 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21773 * to use the USCSI "direct" chain and bypass the normal 21774 * command waitq. 21775 * 21776 * Return Code: 0 - Success 21777 * errno return code from sd_ssc_send() 21778 * 21779 * Context: Can sleep. Does not return until command is completed. 21780 */ 21781 21782 static int 21783 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 21784 size_t buflen, daddr_t start_block, int path_flag) 21785 { 21786 struct scsi_extended_sense sense_buf; 21787 union scsi_cdb cdb; 21788 struct uscsi_cmd ucmd_buf; 21789 uint32_t block_count; 21790 int status; 21791 int cdbsize; 21792 uchar_t flag; 21793 struct sd_lun *un; 21794 21795 ASSERT(ssc != NULL); 21796 un = ssc->ssc_un; 21797 ASSERT(un != NULL); 21798 ASSERT(!mutex_owned(SD_MUTEX(un))); 21799 ASSERT(bufaddr != NULL); 21800 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21801 21802 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21803 21804 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21805 return (EINVAL); 21806 } 21807 21808 mutex_enter(SD_MUTEX(un)); 21809 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21810 mutex_exit(SD_MUTEX(un)); 21811 21812 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21813 21814 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21815 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21816 bufaddr, buflen, start_block, block_count); 21817 21818 bzero(&cdb, sizeof (cdb)); 21819 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21820 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21821 21822 /* Compute CDB size to use */ 21823 if (start_block > 0xffffffff) 21824 cdbsize = CDB_GROUP4; 21825 else if ((start_block & 0xFFE00000) || 21826 (un->un_f_cfg_is_atapi == TRUE)) 21827 cdbsize = CDB_GROUP1; 21828 else 21829 cdbsize = CDB_GROUP0; 21830 21831 switch (cdbsize) { 21832 case CDB_GROUP0: /* 6-byte CDBs */ 21833 cdb.scc_cmd = cmd; 21834 FORMG0ADDR(&cdb, start_block); 21835 FORMG0COUNT(&cdb, block_count); 21836 break; 21837 case CDB_GROUP1: /* 10-byte CDBs */ 21838 cdb.scc_cmd = cmd | SCMD_GROUP1; 21839 FORMG1ADDR(&cdb, start_block); 21840 FORMG1COUNT(&cdb, block_count); 21841 break; 21842 case CDB_GROUP4: /* 16-byte CDBs */ 21843 cdb.scc_cmd = cmd | SCMD_GROUP4; 21844 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21845 FORMG4COUNT(&cdb, block_count); 21846 break; 21847 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21848 default: 21849 /* All others reserved */ 21850 return (EINVAL); 21851 } 21852 21853 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21854 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21855 21856 ucmd_buf.uscsi_cdb = (char *)&cdb; 21857 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21858 ucmd_buf.uscsi_bufaddr = bufaddr; 21859 ucmd_buf.uscsi_buflen = buflen; 21860 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21861 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21862 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21863 ucmd_buf.uscsi_timeout = 60; 21864 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21865 UIO_SYSSPACE, path_flag); 21866 21867 switch (status) { 21868 case 0: 21869 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21870 break; /* Success! */ 21871 case EIO: 21872 switch (ucmd_buf.uscsi_status) { 21873 case STATUS_RESERVATION_CONFLICT: 21874 status = EACCES; 21875 break; 21876 default: 21877 break; 21878 } 21879 break; 21880 default: 21881 break; 21882 } 21883 21884 if (status == 0) { 21885 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21886 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21887 } 21888 21889 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21890 21891 return (status); 21892 } 21893 21894 21895 /* 21896 * Function: sd_send_scsi_LOG_SENSE 21897 * 21898 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21899 * 21900 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21901 * structure for this target. 21902 * 21903 * Return Code: 0 - Success 21904 * errno return code from sd_ssc_send() 21905 * 21906 * Context: Can sleep. Does not return until command is completed. 21907 */ 21908 21909 static int 21910 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen, 21911 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21912 int path_flag) 21913 21914 { 21915 struct scsi_extended_sense sense_buf; 21916 union scsi_cdb cdb; 21917 struct uscsi_cmd ucmd_buf; 21918 int status; 21919 struct sd_lun *un; 21920 21921 ASSERT(ssc != NULL); 21922 un = ssc->ssc_un; 21923 ASSERT(un != NULL); 21924 ASSERT(!mutex_owned(SD_MUTEX(un))); 21925 21926 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21927 21928 bzero(&cdb, sizeof (cdb)); 21929 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21930 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21931 21932 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21933 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21934 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21935 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21936 FORMG1COUNT(&cdb, buflen); 21937 21938 ucmd_buf.uscsi_cdb = (char *)&cdb; 21939 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21940 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21941 ucmd_buf.uscsi_buflen = buflen; 21942 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21943 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21944 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21945 ucmd_buf.uscsi_timeout = 60; 21946 21947 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21948 UIO_SYSSPACE, path_flag); 21949 21950 switch (status) { 21951 case 0: 21952 break; 21953 case EIO: 21954 switch (ucmd_buf.uscsi_status) { 21955 case STATUS_RESERVATION_CONFLICT: 21956 status = EACCES; 21957 break; 21958 case STATUS_CHECK: 21959 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21960 (scsi_sense_key((uint8_t *)&sense_buf) == 21961 KEY_ILLEGAL_REQUEST) && 21962 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21963 /* 21964 * ASC 0x24: INVALID FIELD IN CDB 21965 */ 21966 switch (page_code) { 21967 case START_STOP_CYCLE_PAGE: 21968 /* 21969 * The start stop cycle counter is 21970 * implemented as page 0x31 in earlier 21971 * generation disks. In new generation 21972 * disks the start stop cycle counter is 21973 * implemented as page 0xE. To properly 21974 * handle this case if an attempt for 21975 * log page 0xE is made and fails we 21976 * will try again using page 0x31. 21977 * 21978 * Network storage BU committed to 21979 * maintain the page 0x31 for this 21980 * purpose and will not have any other 21981 * page implemented with page code 0x31 21982 * until all disks transition to the 21983 * standard page. 21984 */ 21985 mutex_enter(SD_MUTEX(un)); 21986 un->un_start_stop_cycle_page = 21987 START_STOP_CYCLE_VU_PAGE; 21988 cdb.cdb_opaque[2] = 21989 (char)(page_control << 6) | 21990 un->un_start_stop_cycle_page; 21991 mutex_exit(SD_MUTEX(un)); 21992 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 21993 status = sd_ssc_send( 21994 ssc, &ucmd_buf, FKIOCTL, 21995 UIO_SYSSPACE, path_flag); 21996 21997 break; 21998 case TEMPERATURE_PAGE: 21999 status = ENOTTY; 22000 break; 22001 default: 22002 break; 22003 } 22004 } 22005 break; 22006 default: 22007 break; 22008 } 22009 break; 22010 default: 22011 break; 22012 } 22013 22014 if (status == 0) { 22015 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22016 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 22017 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 22018 } 22019 22020 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 22021 22022 return (status); 22023 } 22024 22025 22026 /* 22027 * Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 22028 * 22029 * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command. 22030 * 22031 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 22032 * structure for this target. 22033 * bufaddr 22034 * buflen 22035 * class_req 22036 * 22037 * Return Code: 0 - Success 22038 * errno return code from sd_ssc_send() 22039 * 22040 * Context: Can sleep. Does not return until command is completed. 22041 */ 22042 22043 static int 22044 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr, 22045 size_t buflen, uchar_t class_req) 22046 { 22047 union scsi_cdb cdb; 22048 struct uscsi_cmd ucmd_buf; 22049 int status; 22050 struct sd_lun *un; 22051 22052 ASSERT(ssc != NULL); 22053 un = ssc->ssc_un; 22054 ASSERT(un != NULL); 22055 ASSERT(!mutex_owned(SD_MUTEX(un))); 22056 ASSERT(bufaddr != NULL); 22057 22058 SD_TRACE(SD_LOG_IO, un, 22059 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un); 22060 22061 bzero(&cdb, sizeof (cdb)); 22062 bzero(&ucmd_buf, sizeof (ucmd_buf)); 22063 bzero(bufaddr, buflen); 22064 22065 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION; 22066 cdb.cdb_opaque[1] = 1; /* polled */ 22067 cdb.cdb_opaque[4] = class_req; 22068 FORMG1COUNT(&cdb, buflen); 22069 22070 ucmd_buf.uscsi_cdb = (char *)&cdb; 22071 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 22072 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 22073 ucmd_buf.uscsi_buflen = buflen; 22074 ucmd_buf.uscsi_rqbuf = NULL; 22075 ucmd_buf.uscsi_rqlen = 0; 22076 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 22077 ucmd_buf.uscsi_timeout = 60; 22078 22079 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 22080 UIO_SYSSPACE, SD_PATH_DIRECT); 22081 22082 /* 22083 * Only handle status == 0, the upper-level caller 22084 * will put different assessment based on the context. 22085 */ 22086 if (status == 0) { 22087 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22088 22089 if (ucmd_buf.uscsi_resid != 0) { 22090 status = EIO; 22091 } 22092 } 22093 22094 SD_TRACE(SD_LOG_IO, un, 22095 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n"); 22096 22097 return (status); 22098 } 22099 22100 22101 static boolean_t 22102 sd_gesn_media_data_valid(uchar_t *data) 22103 { 22104 uint16_t len; 22105 22106 len = (data[1] << 8) | data[0]; 22107 return ((len >= 6) && 22108 ((data[2] & SD_GESN_HEADER_NEA) == 0) && 22109 ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) && 22110 ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0)); 22111 } 22112 22113 22114 /* 22115 * Function: sdioctl 22116 * 22117 * Description: Driver's ioctl(9e) entry point function. 22118 * 22119 * Arguments: dev - device number 22120 * cmd - ioctl operation to be performed 22121 * arg - user argument, contains data to be set or reference 22122 * parameter for get 22123 * flag - bit flag, indicating open settings, 32/64 bit type 22124 * cred_p - user credential pointer 22125 * rval_p - calling process return value (OPT) 22126 * 22127 * Return Code: EINVAL 22128 * ENOTTY 22129 * ENXIO 22130 * EIO 22131 * EFAULT 22132 * ENOTSUP 22133 * EPERM 22134 * 22135 * Context: Called from the device switch at normal priority. 22136 */ 22137 22138 static int 22139 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 22140 { 22141 struct sd_lun *un = NULL; 22142 int err = 0; 22143 int i = 0; 22144 cred_t *cr; 22145 int tmprval = EINVAL; 22146 boolean_t is_valid; 22147 sd_ssc_t *ssc; 22148 22149 /* 22150 * All device accesses go thru sdstrategy where we check on suspend 22151 * status 22152 */ 22153 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22154 return (ENXIO); 22155 } 22156 22157 ASSERT(!mutex_owned(SD_MUTEX(un))); 22158 22159 /* Initialize sd_ssc_t for internal uscsi commands */ 22160 ssc = sd_ssc_init(un); 22161 22162 is_valid = SD_IS_VALID_LABEL(un); 22163 22164 /* 22165 * Moved this wait from sd_uscsi_strategy to here for 22166 * reasons of deadlock prevention. Internal driver commands, 22167 * specifically those to change a devices power level, result 22168 * in a call to sd_uscsi_strategy. 22169 */ 22170 mutex_enter(SD_MUTEX(un)); 22171 while ((un->un_state == SD_STATE_SUSPENDED) || 22172 (un->un_state == SD_STATE_PM_CHANGING)) { 22173 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 22174 } 22175 /* 22176 * Twiddling the counter here protects commands from now 22177 * through to the top of sd_uscsi_strategy. Without the 22178 * counter inc. a power down, for example, could get in 22179 * after the above check for state is made and before 22180 * execution gets to the top of sd_uscsi_strategy. 22181 * That would cause problems. 22182 */ 22183 un->un_ncmds_in_driver++; 22184 22185 if (!is_valid && 22186 (flag & (FNDELAY | FNONBLOCK))) { 22187 switch (cmd) { 22188 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 22189 case DKIOCGVTOC: 22190 case DKIOCGEXTVTOC: 22191 case DKIOCGAPART: 22192 case DKIOCPARTINFO: 22193 case DKIOCEXTPARTINFO: 22194 case DKIOCSGEOM: 22195 case DKIOCSAPART: 22196 case DKIOCGETEFI: 22197 case DKIOCPARTITION: 22198 case DKIOCSVTOC: 22199 case DKIOCSEXTVTOC: 22200 case DKIOCSETEFI: 22201 case DKIOCGMBOOT: 22202 case DKIOCSMBOOT: 22203 case DKIOCG_PHYGEOM: 22204 case DKIOCG_VIRTGEOM: 22205 #if defined(__i386) || defined(__amd64) 22206 case DKIOCSETEXTPART: 22207 #endif 22208 /* let cmlb handle it */ 22209 goto skip_ready_valid; 22210 22211 case CDROMPAUSE: 22212 case CDROMRESUME: 22213 case CDROMPLAYMSF: 22214 case CDROMPLAYTRKIND: 22215 case CDROMREADTOCHDR: 22216 case CDROMREADTOCENTRY: 22217 case CDROMSTOP: 22218 case CDROMSTART: 22219 case CDROMVOLCTRL: 22220 case CDROMSUBCHNL: 22221 case CDROMREADMODE2: 22222 case CDROMREADMODE1: 22223 case CDROMREADOFFSET: 22224 case CDROMSBLKMODE: 22225 case CDROMGBLKMODE: 22226 case CDROMGDRVSPEED: 22227 case CDROMSDRVSPEED: 22228 case CDROMCDDA: 22229 case CDROMCDXA: 22230 case CDROMSUBCODE: 22231 if (!ISCD(un)) { 22232 un->un_ncmds_in_driver--; 22233 ASSERT(un->un_ncmds_in_driver >= 0); 22234 mutex_exit(SD_MUTEX(un)); 22235 err = ENOTTY; 22236 goto done_without_assess; 22237 } 22238 break; 22239 case FDEJECT: 22240 case DKIOCEJECT: 22241 case CDROMEJECT: 22242 if (!un->un_f_eject_media_supported) { 22243 un->un_ncmds_in_driver--; 22244 ASSERT(un->un_ncmds_in_driver >= 0); 22245 mutex_exit(SD_MUTEX(un)); 22246 err = ENOTTY; 22247 goto done_without_assess; 22248 } 22249 break; 22250 case DKIOCFLUSHWRITECACHE: 22251 mutex_exit(SD_MUTEX(un)); 22252 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22253 if (err != 0) { 22254 mutex_enter(SD_MUTEX(un)); 22255 un->un_ncmds_in_driver--; 22256 ASSERT(un->un_ncmds_in_driver >= 0); 22257 mutex_exit(SD_MUTEX(un)); 22258 err = EIO; 22259 goto done_quick_assess; 22260 } 22261 mutex_enter(SD_MUTEX(un)); 22262 /* FALLTHROUGH */ 22263 case DKIOCREMOVABLE: 22264 case DKIOCHOTPLUGGABLE: 22265 case DKIOCINFO: 22266 case DKIOCGMEDIAINFO: 22267 case DKIOCGMEDIAINFOEXT: 22268 case MHIOCENFAILFAST: 22269 case MHIOCSTATUS: 22270 case MHIOCTKOWN: 22271 case MHIOCRELEASE: 22272 case MHIOCGRP_INKEYS: 22273 case MHIOCGRP_INRESV: 22274 case MHIOCGRP_REGISTER: 22275 case MHIOCGRP_RESERVE: 22276 case MHIOCGRP_PREEMPTANDABORT: 22277 case MHIOCGRP_REGISTERANDIGNOREKEY: 22278 case CDROMCLOSETRAY: 22279 case USCSICMD: 22280 goto skip_ready_valid; 22281 default: 22282 break; 22283 } 22284 22285 mutex_exit(SD_MUTEX(un)); 22286 err = sd_ready_and_valid(ssc, SDPART(dev)); 22287 mutex_enter(SD_MUTEX(un)); 22288 22289 if (err != SD_READY_VALID) { 22290 switch (cmd) { 22291 case DKIOCSTATE: 22292 case CDROMGDRVSPEED: 22293 case CDROMSDRVSPEED: 22294 case FDEJECT: /* for eject command */ 22295 case DKIOCEJECT: 22296 case CDROMEJECT: 22297 case DKIOCREMOVABLE: 22298 case DKIOCHOTPLUGGABLE: 22299 break; 22300 default: 22301 if (un->un_f_has_removable_media) { 22302 err = ENXIO; 22303 } else { 22304 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 22305 if (err == SD_RESERVED_BY_OTHERS) { 22306 err = EACCES; 22307 } else { 22308 err = EIO; 22309 } 22310 } 22311 un->un_ncmds_in_driver--; 22312 ASSERT(un->un_ncmds_in_driver >= 0); 22313 mutex_exit(SD_MUTEX(un)); 22314 22315 goto done_without_assess; 22316 } 22317 } 22318 } 22319 22320 skip_ready_valid: 22321 mutex_exit(SD_MUTEX(un)); 22322 22323 switch (cmd) { 22324 case DKIOCINFO: 22325 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 22326 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 22327 break; 22328 22329 case DKIOCGMEDIAINFO: 22330 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 22331 err = sd_get_media_info(dev, (caddr_t)arg, flag); 22332 break; 22333 22334 case DKIOCGMEDIAINFOEXT: 22335 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n"); 22336 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag); 22337 break; 22338 22339 case DKIOCGGEOM: 22340 case DKIOCGVTOC: 22341 case DKIOCGEXTVTOC: 22342 case DKIOCGAPART: 22343 case DKIOCPARTINFO: 22344 case DKIOCEXTPARTINFO: 22345 case DKIOCSGEOM: 22346 case DKIOCSAPART: 22347 case DKIOCGETEFI: 22348 case DKIOCPARTITION: 22349 case DKIOCSVTOC: 22350 case DKIOCSEXTVTOC: 22351 case DKIOCSETEFI: 22352 case DKIOCGMBOOT: 22353 case DKIOCSMBOOT: 22354 case DKIOCG_PHYGEOM: 22355 case DKIOCG_VIRTGEOM: 22356 #if defined(__i386) || defined(__amd64) 22357 case DKIOCSETEXTPART: 22358 #endif 22359 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 22360 22361 /* TUR should spin up */ 22362 22363 if (un->un_f_has_removable_media) 22364 err = sd_send_scsi_TEST_UNIT_READY(ssc, 22365 SD_CHECK_FOR_MEDIA); 22366 22367 else 22368 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22369 22370 if (err != 0) 22371 goto done_with_assess; 22372 22373 err = cmlb_ioctl(un->un_cmlbhandle, dev, 22374 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 22375 22376 if ((err == 0) && 22377 ((cmd == DKIOCSETEFI) || 22378 (un->un_f_pkstats_enabled) && 22379 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC || 22380 cmd == DKIOCSEXTVTOC))) { 22381 22382 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 22383 (void *)SD_PATH_DIRECT); 22384 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 22385 sd_set_pstats(un); 22386 SD_TRACE(SD_LOG_IO_PARTITION, un, 22387 "sd_ioctl: un:0x%p pstats created and " 22388 "set\n", un); 22389 } 22390 } 22391 22392 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) || 22393 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 22394 22395 mutex_enter(SD_MUTEX(un)); 22396 if (un->un_f_devid_supported && 22397 (un->un_f_opt_fab_devid == TRUE)) { 22398 if (un->un_devid == NULL) { 22399 sd_register_devid(ssc, SD_DEVINFO(un), 22400 SD_TARGET_IS_UNRESERVED); 22401 } else { 22402 /* 22403 * The device id for this disk 22404 * has been fabricated. The 22405 * device id must be preserved 22406 * by writing it back out to 22407 * disk. 22408 */ 22409 if (sd_write_deviceid(ssc) != 0) { 22410 ddi_devid_free(un->un_devid); 22411 un->un_devid = NULL; 22412 } 22413 } 22414 } 22415 mutex_exit(SD_MUTEX(un)); 22416 } 22417 22418 break; 22419 22420 case DKIOCLOCK: 22421 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 22422 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 22423 SD_PATH_STANDARD); 22424 goto done_with_assess; 22425 22426 case DKIOCUNLOCK: 22427 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 22428 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 22429 SD_PATH_STANDARD); 22430 goto done_with_assess; 22431 22432 case DKIOCSTATE: { 22433 enum dkio_state state; 22434 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 22435 22436 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 22437 err = EFAULT; 22438 } else { 22439 err = sd_check_media(dev, state); 22440 if (err == 0) { 22441 if (ddi_copyout(&un->un_mediastate, (void *)arg, 22442 sizeof (int), flag) != 0) 22443 err = EFAULT; 22444 } 22445 } 22446 break; 22447 } 22448 22449 case DKIOCREMOVABLE: 22450 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 22451 i = un->un_f_has_removable_media ? 1 : 0; 22452 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22453 err = EFAULT; 22454 } else { 22455 err = 0; 22456 } 22457 break; 22458 22459 case DKIOCHOTPLUGGABLE: 22460 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 22461 i = un->un_f_is_hotpluggable ? 1 : 0; 22462 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22463 err = EFAULT; 22464 } else { 22465 err = 0; 22466 } 22467 break; 22468 22469 case DKIOCREADONLY: 22470 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n"); 22471 i = 0; 22472 if ((ISCD(un) && !un->un_f_mmc_writable_media) || 22473 (sr_check_wp(dev) != 0)) { 22474 i = 1; 22475 } 22476 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22477 err = EFAULT; 22478 } else { 22479 err = 0; 22480 } 22481 break; 22482 22483 case DKIOCGTEMPERATURE: 22484 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 22485 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 22486 break; 22487 22488 case MHIOCENFAILFAST: 22489 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 22490 if ((err = drv_priv(cred_p)) == 0) { 22491 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 22492 } 22493 break; 22494 22495 case MHIOCTKOWN: 22496 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 22497 if ((err = drv_priv(cred_p)) == 0) { 22498 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 22499 } 22500 break; 22501 22502 case MHIOCRELEASE: 22503 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 22504 if ((err = drv_priv(cred_p)) == 0) { 22505 err = sd_mhdioc_release(dev); 22506 } 22507 break; 22508 22509 case MHIOCSTATUS: 22510 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 22511 if ((err = drv_priv(cred_p)) == 0) { 22512 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) { 22513 case 0: 22514 err = 0; 22515 break; 22516 case EACCES: 22517 *rval_p = 1; 22518 err = 0; 22519 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22520 break; 22521 default: 22522 err = EIO; 22523 goto done_with_assess; 22524 } 22525 } 22526 break; 22527 22528 case MHIOCQRESERVE: 22529 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 22530 if ((err = drv_priv(cred_p)) == 0) { 22531 err = sd_reserve_release(dev, SD_RESERVE); 22532 } 22533 break; 22534 22535 case MHIOCREREGISTERDEVID: 22536 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 22537 if (drv_priv(cred_p) == EPERM) { 22538 err = EPERM; 22539 } else if (!un->un_f_devid_supported) { 22540 err = ENOTTY; 22541 } else { 22542 err = sd_mhdioc_register_devid(dev); 22543 } 22544 break; 22545 22546 case MHIOCGRP_INKEYS: 22547 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 22548 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22549 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22550 err = ENOTSUP; 22551 } else { 22552 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 22553 flag); 22554 } 22555 } 22556 break; 22557 22558 case MHIOCGRP_INRESV: 22559 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 22560 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22561 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22562 err = ENOTSUP; 22563 } else { 22564 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 22565 } 22566 } 22567 break; 22568 22569 case MHIOCGRP_REGISTER: 22570 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 22571 if ((err = drv_priv(cred_p)) != EPERM) { 22572 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22573 err = ENOTSUP; 22574 } else if (arg != NULL) { 22575 mhioc_register_t reg; 22576 if (ddi_copyin((void *)arg, ®, 22577 sizeof (mhioc_register_t), flag) != 0) { 22578 err = EFAULT; 22579 } else { 22580 err = 22581 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22582 ssc, SD_SCSI3_REGISTER, 22583 (uchar_t *)®); 22584 if (err != 0) 22585 goto done_with_assess; 22586 } 22587 } 22588 } 22589 break; 22590 22591 case MHIOCGRP_RESERVE: 22592 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 22593 if ((err = drv_priv(cred_p)) != EPERM) { 22594 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22595 err = ENOTSUP; 22596 } else if (arg != NULL) { 22597 mhioc_resv_desc_t resv_desc; 22598 if (ddi_copyin((void *)arg, &resv_desc, 22599 sizeof (mhioc_resv_desc_t), flag) != 0) { 22600 err = EFAULT; 22601 } else { 22602 err = 22603 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22604 ssc, SD_SCSI3_RESERVE, 22605 (uchar_t *)&resv_desc); 22606 if (err != 0) 22607 goto done_with_assess; 22608 } 22609 } 22610 } 22611 break; 22612 22613 case MHIOCGRP_PREEMPTANDABORT: 22614 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 22615 if ((err = drv_priv(cred_p)) != EPERM) { 22616 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22617 err = ENOTSUP; 22618 } else if (arg != NULL) { 22619 mhioc_preemptandabort_t preempt_abort; 22620 if (ddi_copyin((void *)arg, &preempt_abort, 22621 sizeof (mhioc_preemptandabort_t), 22622 flag) != 0) { 22623 err = EFAULT; 22624 } else { 22625 err = 22626 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22627 ssc, SD_SCSI3_PREEMPTANDABORT, 22628 (uchar_t *)&preempt_abort); 22629 if (err != 0) 22630 goto done_with_assess; 22631 } 22632 } 22633 } 22634 break; 22635 22636 case MHIOCGRP_REGISTERANDIGNOREKEY: 22637 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 22638 if ((err = drv_priv(cred_p)) != EPERM) { 22639 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22640 err = ENOTSUP; 22641 } else if (arg != NULL) { 22642 mhioc_registerandignorekey_t r_and_i; 22643 if (ddi_copyin((void *)arg, (void *)&r_and_i, 22644 sizeof (mhioc_registerandignorekey_t), 22645 flag) != 0) { 22646 err = EFAULT; 22647 } else { 22648 err = 22649 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22650 ssc, SD_SCSI3_REGISTERANDIGNOREKEY, 22651 (uchar_t *)&r_and_i); 22652 if (err != 0) 22653 goto done_with_assess; 22654 } 22655 } 22656 } 22657 break; 22658 22659 case USCSICMD: 22660 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 22661 cr = ddi_get_cred(); 22662 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 22663 err = EPERM; 22664 } else { 22665 enum uio_seg uioseg; 22666 22667 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 22668 UIO_USERSPACE; 22669 if (un->un_f_format_in_progress == TRUE) { 22670 err = EAGAIN; 22671 break; 22672 } 22673 22674 err = sd_ssc_send(ssc, 22675 (struct uscsi_cmd *)arg, 22676 flag, uioseg, SD_PATH_STANDARD); 22677 if (err != 0) 22678 goto done_with_assess; 22679 else 22680 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22681 } 22682 break; 22683 22684 case CDROMPAUSE: 22685 case CDROMRESUME: 22686 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 22687 if (!ISCD(un)) { 22688 err = ENOTTY; 22689 } else { 22690 err = sr_pause_resume(dev, cmd); 22691 } 22692 break; 22693 22694 case CDROMPLAYMSF: 22695 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 22696 if (!ISCD(un)) { 22697 err = ENOTTY; 22698 } else { 22699 err = sr_play_msf(dev, (caddr_t)arg, flag); 22700 } 22701 break; 22702 22703 case CDROMPLAYTRKIND: 22704 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 22705 #if defined(__i386) || defined(__amd64) 22706 /* 22707 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 22708 */ 22709 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22710 #else 22711 if (!ISCD(un)) { 22712 #endif 22713 err = ENOTTY; 22714 } else { 22715 err = sr_play_trkind(dev, (caddr_t)arg, flag); 22716 } 22717 break; 22718 22719 case CDROMREADTOCHDR: 22720 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 22721 if (!ISCD(un)) { 22722 err = ENOTTY; 22723 } else { 22724 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 22725 } 22726 break; 22727 22728 case CDROMREADTOCENTRY: 22729 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 22730 if (!ISCD(un)) { 22731 err = ENOTTY; 22732 } else { 22733 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 22734 } 22735 break; 22736 22737 case CDROMSTOP: 22738 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 22739 if (!ISCD(un)) { 22740 err = ENOTTY; 22741 } else { 22742 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22743 SD_TARGET_STOP, SD_PATH_STANDARD); 22744 goto done_with_assess; 22745 } 22746 break; 22747 22748 case CDROMSTART: 22749 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 22750 if (!ISCD(un)) { 22751 err = ENOTTY; 22752 } else { 22753 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22754 SD_TARGET_START, SD_PATH_STANDARD); 22755 goto done_with_assess; 22756 } 22757 break; 22758 22759 case CDROMCLOSETRAY: 22760 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 22761 if (!ISCD(un)) { 22762 err = ENOTTY; 22763 } else { 22764 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22765 SD_TARGET_CLOSE, SD_PATH_STANDARD); 22766 goto done_with_assess; 22767 } 22768 break; 22769 22770 case FDEJECT: /* for eject command */ 22771 case DKIOCEJECT: 22772 case CDROMEJECT: 22773 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 22774 if (!un->un_f_eject_media_supported) { 22775 err = ENOTTY; 22776 } else { 22777 err = sr_eject(dev); 22778 } 22779 break; 22780 22781 case CDROMVOLCTRL: 22782 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 22783 if (!ISCD(un)) { 22784 err = ENOTTY; 22785 } else { 22786 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 22787 } 22788 break; 22789 22790 case CDROMSUBCHNL: 22791 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 22792 if (!ISCD(un)) { 22793 err = ENOTTY; 22794 } else { 22795 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 22796 } 22797 break; 22798 22799 case CDROMREADMODE2: 22800 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 22801 if (!ISCD(un)) { 22802 err = ENOTTY; 22803 } else if (un->un_f_cfg_is_atapi == TRUE) { 22804 /* 22805 * If the drive supports READ CD, use that instead of 22806 * switching the LBA size via a MODE SELECT 22807 * Block Descriptor 22808 */ 22809 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 22810 } else { 22811 err = sr_read_mode2(dev, (caddr_t)arg, flag); 22812 } 22813 break; 22814 22815 case CDROMREADMODE1: 22816 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 22817 if (!ISCD(un)) { 22818 err = ENOTTY; 22819 } else { 22820 err = sr_read_mode1(dev, (caddr_t)arg, flag); 22821 } 22822 break; 22823 22824 case CDROMREADOFFSET: 22825 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 22826 if (!ISCD(un)) { 22827 err = ENOTTY; 22828 } else { 22829 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 22830 flag); 22831 } 22832 break; 22833 22834 case CDROMSBLKMODE: 22835 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 22836 /* 22837 * There is no means of changing block size in case of atapi 22838 * drives, thus return ENOTTY if drive type is atapi 22839 */ 22840 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22841 err = ENOTTY; 22842 } else if (un->un_f_mmc_cap == TRUE) { 22843 22844 /* 22845 * MMC Devices do not support changing the 22846 * logical block size 22847 * 22848 * Note: EINVAL is being returned instead of ENOTTY to 22849 * maintain consistancy with the original mmc 22850 * driver update. 22851 */ 22852 err = EINVAL; 22853 } else { 22854 mutex_enter(SD_MUTEX(un)); 22855 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 22856 (un->un_ncmds_in_transport > 0)) { 22857 mutex_exit(SD_MUTEX(un)); 22858 err = EINVAL; 22859 } else { 22860 mutex_exit(SD_MUTEX(un)); 22861 err = sr_change_blkmode(dev, cmd, arg, flag); 22862 } 22863 } 22864 break; 22865 22866 case CDROMGBLKMODE: 22867 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 22868 if (!ISCD(un)) { 22869 err = ENOTTY; 22870 } else if ((un->un_f_cfg_is_atapi != FALSE) && 22871 (un->un_f_blockcount_is_valid != FALSE)) { 22872 /* 22873 * Drive is an ATAPI drive so return target block 22874 * size for ATAPI drives since we cannot change the 22875 * blocksize on ATAPI drives. Used primarily to detect 22876 * if an ATAPI cdrom is present. 22877 */ 22878 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 22879 sizeof (int), flag) != 0) { 22880 err = EFAULT; 22881 } else { 22882 err = 0; 22883 } 22884 22885 } else { 22886 /* 22887 * Drive supports changing block sizes via a Mode 22888 * Select. 22889 */ 22890 err = sr_change_blkmode(dev, cmd, arg, flag); 22891 } 22892 break; 22893 22894 case CDROMGDRVSPEED: 22895 case CDROMSDRVSPEED: 22896 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 22897 if (!ISCD(un)) { 22898 err = ENOTTY; 22899 } else if (un->un_f_mmc_cap == TRUE) { 22900 /* 22901 * Note: In the future the driver implementation 22902 * for getting and 22903 * setting cd speed should entail: 22904 * 1) If non-mmc try the Toshiba mode page 22905 * (sr_change_speed) 22906 * 2) If mmc but no support for Real Time Streaming try 22907 * the SET CD SPEED (0xBB) command 22908 * (sr_atapi_change_speed) 22909 * 3) If mmc and support for Real Time Streaming 22910 * try the GET PERFORMANCE and SET STREAMING 22911 * commands (not yet implemented, 4380808) 22912 */ 22913 /* 22914 * As per recent MMC spec, CD-ROM speed is variable 22915 * and changes with LBA. Since there is no such 22916 * things as drive speed now, fail this ioctl. 22917 * 22918 * Note: EINVAL is returned for consistancy of original 22919 * implementation which included support for getting 22920 * the drive speed of mmc devices but not setting 22921 * the drive speed. Thus EINVAL would be returned 22922 * if a set request was made for an mmc device. 22923 * We no longer support get or set speed for 22924 * mmc but need to remain consistent with regard 22925 * to the error code returned. 22926 */ 22927 err = EINVAL; 22928 } else if (un->un_f_cfg_is_atapi == TRUE) { 22929 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22930 } else { 22931 err = sr_change_speed(dev, cmd, arg, flag); 22932 } 22933 break; 22934 22935 case CDROMCDDA: 22936 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22937 if (!ISCD(un)) { 22938 err = ENOTTY; 22939 } else { 22940 err = sr_read_cdda(dev, (void *)arg, flag); 22941 } 22942 break; 22943 22944 case CDROMCDXA: 22945 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22946 if (!ISCD(un)) { 22947 err = ENOTTY; 22948 } else { 22949 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22950 } 22951 break; 22952 22953 case CDROMSUBCODE: 22954 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22955 if (!ISCD(un)) { 22956 err = ENOTTY; 22957 } else { 22958 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22959 } 22960 break; 22961 22962 22963 #ifdef SDDEBUG 22964 /* RESET/ABORTS testing ioctls */ 22965 case DKIOCRESET: { 22966 int reset_level; 22967 22968 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22969 err = EFAULT; 22970 } else { 22971 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22972 "reset_level = 0x%lx\n", reset_level); 22973 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22974 err = 0; 22975 } else { 22976 err = EIO; 22977 } 22978 } 22979 break; 22980 } 22981 22982 case DKIOCABORT: 22983 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22984 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22985 err = 0; 22986 } else { 22987 err = EIO; 22988 } 22989 break; 22990 #endif 22991 22992 #ifdef SD_FAULT_INJECTION 22993 /* SDIOC FaultInjection testing ioctls */ 22994 case SDIOCSTART: 22995 case SDIOCSTOP: 22996 case SDIOCINSERTPKT: 22997 case SDIOCINSERTXB: 22998 case SDIOCINSERTUN: 22999 case SDIOCINSERTARQ: 23000 case SDIOCPUSH: 23001 case SDIOCRETRIEVE: 23002 case SDIOCRUN: 23003 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 23004 "SDIOC detected cmd:0x%X:\n", cmd); 23005 /* call error generator */ 23006 sd_faultinjection_ioctl(cmd, arg, un); 23007 err = 0; 23008 break; 23009 23010 #endif /* SD_FAULT_INJECTION */ 23011 23012 case DKIOCFLUSHWRITECACHE: 23013 { 23014 struct dk_callback *dkc = (struct dk_callback *)arg; 23015 23016 mutex_enter(SD_MUTEX(un)); 23017 if (!un->un_f_sync_cache_supported || 23018 !un->un_f_write_cache_enabled) { 23019 err = un->un_f_sync_cache_supported ? 23020 0 : ENOTSUP; 23021 mutex_exit(SD_MUTEX(un)); 23022 if ((flag & FKIOCTL) && dkc != NULL && 23023 dkc->dkc_callback != NULL) { 23024 (*dkc->dkc_callback)(dkc->dkc_cookie, 23025 err); 23026 /* 23027 * Did callback and reported error. 23028 * Since we did a callback, ioctl 23029 * should return 0. 23030 */ 23031 err = 0; 23032 } 23033 break; 23034 } 23035 mutex_exit(SD_MUTEX(un)); 23036 23037 if ((flag & FKIOCTL) && dkc != NULL && 23038 dkc->dkc_callback != NULL) { 23039 /* async SYNC CACHE request */ 23040 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 23041 } else { 23042 /* synchronous SYNC CACHE request */ 23043 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 23044 } 23045 } 23046 break; 23047 23048 case DKIOCGETWCE: { 23049 23050 int wce; 23051 23052 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) { 23053 break; 23054 } 23055 23056 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 23057 err = EFAULT; 23058 } 23059 break; 23060 } 23061 23062 case DKIOCSETWCE: { 23063 23064 int wce, sync_supported; 23065 int cur_wce = 0; 23066 23067 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 23068 err = EFAULT; 23069 break; 23070 } 23071 23072 /* 23073 * Synchronize multiple threads trying to enable 23074 * or disable the cache via the un_f_wcc_cv 23075 * condition variable. 23076 */ 23077 mutex_enter(SD_MUTEX(un)); 23078 23079 /* 23080 * Don't allow the cache to be enabled if the 23081 * config file has it disabled. 23082 */ 23083 if (un->un_f_opt_disable_cache && wce) { 23084 mutex_exit(SD_MUTEX(un)); 23085 err = EINVAL; 23086 break; 23087 } 23088 23089 /* 23090 * Wait for write cache change in progress 23091 * bit to be clear before proceeding. 23092 */ 23093 while (un->un_f_wcc_inprog) 23094 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 23095 23096 un->un_f_wcc_inprog = 1; 23097 23098 mutex_exit(SD_MUTEX(un)); 23099 23100 /* 23101 * Get the current write cache state 23102 */ 23103 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) { 23104 mutex_enter(SD_MUTEX(un)); 23105 un->un_f_wcc_inprog = 0; 23106 cv_broadcast(&un->un_wcc_cv); 23107 mutex_exit(SD_MUTEX(un)); 23108 break; 23109 } 23110 23111 mutex_enter(SD_MUTEX(un)); 23112 un->un_f_write_cache_enabled = (cur_wce != 0); 23113 23114 if (un->un_f_write_cache_enabled && wce == 0) { 23115 /* 23116 * Disable the write cache. Don't clear 23117 * un_f_write_cache_enabled until after 23118 * the mode select and flush are complete. 23119 */ 23120 sync_supported = un->un_f_sync_cache_supported; 23121 23122 /* 23123 * If cache flush is suppressed, we assume that the 23124 * controller firmware will take care of managing the 23125 * write cache for us: no need to explicitly 23126 * disable it. 23127 */ 23128 if (!un->un_f_suppress_cache_flush) { 23129 mutex_exit(SD_MUTEX(un)); 23130 if ((err = sd_cache_control(ssc, 23131 SD_CACHE_NOCHANGE, 23132 SD_CACHE_DISABLE)) == 0 && 23133 sync_supported) { 23134 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 23135 NULL); 23136 } 23137 } else { 23138 mutex_exit(SD_MUTEX(un)); 23139 } 23140 23141 mutex_enter(SD_MUTEX(un)); 23142 if (err == 0) { 23143 un->un_f_write_cache_enabled = 0; 23144 } 23145 23146 } else if (!un->un_f_write_cache_enabled && wce != 0) { 23147 /* 23148 * Set un_f_write_cache_enabled first, so there is 23149 * no window where the cache is enabled, but the 23150 * bit says it isn't. 23151 */ 23152 un->un_f_write_cache_enabled = 1; 23153 23154 /* 23155 * If cache flush is suppressed, we assume that the 23156 * controller firmware will take care of managing the 23157 * write cache for us: no need to explicitly 23158 * enable it. 23159 */ 23160 if (!un->un_f_suppress_cache_flush) { 23161 mutex_exit(SD_MUTEX(un)); 23162 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE, 23163 SD_CACHE_ENABLE); 23164 } else { 23165 mutex_exit(SD_MUTEX(un)); 23166 } 23167 23168 mutex_enter(SD_MUTEX(un)); 23169 23170 if (err) { 23171 un->un_f_write_cache_enabled = 0; 23172 } 23173 } 23174 23175 un->un_f_wcc_inprog = 0; 23176 cv_broadcast(&un->un_wcc_cv); 23177 mutex_exit(SD_MUTEX(un)); 23178 break; 23179 } 23180 23181 default: 23182 err = ENOTTY; 23183 break; 23184 } 23185 mutex_enter(SD_MUTEX(un)); 23186 un->un_ncmds_in_driver--; 23187 ASSERT(un->un_ncmds_in_driver >= 0); 23188 mutex_exit(SD_MUTEX(un)); 23189 23190 23191 done_without_assess: 23192 sd_ssc_fini(ssc); 23193 23194 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23195 return (err); 23196 23197 done_with_assess: 23198 mutex_enter(SD_MUTEX(un)); 23199 un->un_ncmds_in_driver--; 23200 ASSERT(un->un_ncmds_in_driver >= 0); 23201 mutex_exit(SD_MUTEX(un)); 23202 23203 done_quick_assess: 23204 if (err != 0) 23205 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23206 /* Uninitialize sd_ssc_t pointer */ 23207 sd_ssc_fini(ssc); 23208 23209 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23210 return (err); 23211 } 23212 23213 23214 /* 23215 * Function: sd_dkio_ctrl_info 23216 * 23217 * Description: This routine is the driver entry point for handling controller 23218 * information ioctl requests (DKIOCINFO). 23219 * 23220 * Arguments: dev - the device number 23221 * arg - pointer to user provided dk_cinfo structure 23222 * specifying the controller type and attributes. 23223 * flag - this argument is a pass through to ddi_copyxxx() 23224 * directly from the mode argument of ioctl(). 23225 * 23226 * Return Code: 0 23227 * EFAULT 23228 * ENXIO 23229 */ 23230 23231 static int 23232 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 23233 { 23234 struct sd_lun *un = NULL; 23235 struct dk_cinfo *info; 23236 dev_info_t *pdip; 23237 int lun, tgt; 23238 23239 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23240 return (ENXIO); 23241 } 23242 23243 info = (struct dk_cinfo *) 23244 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 23245 23246 switch (un->un_ctype) { 23247 case CTYPE_CDROM: 23248 info->dki_ctype = DKC_CDROM; 23249 break; 23250 default: 23251 info->dki_ctype = DKC_SCSI_CCS; 23252 break; 23253 } 23254 pdip = ddi_get_parent(SD_DEVINFO(un)); 23255 info->dki_cnum = ddi_get_instance(pdip); 23256 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 23257 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 23258 } else { 23259 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 23260 DK_DEVLEN - 1); 23261 } 23262 23263 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23264 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 23265 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23266 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 23267 23268 /* Unit Information */ 23269 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 23270 info->dki_slave = ((tgt << 3) | lun); 23271 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 23272 DK_DEVLEN - 1); 23273 info->dki_flags = DKI_FMTVOL; 23274 info->dki_partition = SDPART(dev); 23275 23276 /* Max Transfer size of this device in blocks */ 23277 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 23278 info->dki_addr = 0; 23279 info->dki_space = 0; 23280 info->dki_prio = 0; 23281 info->dki_vec = 0; 23282 23283 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 23284 kmem_free(info, sizeof (struct dk_cinfo)); 23285 return (EFAULT); 23286 } else { 23287 kmem_free(info, sizeof (struct dk_cinfo)); 23288 return (0); 23289 } 23290 } 23291 23292 23293 /* 23294 * Function: sd_get_media_info 23295 * 23296 * Description: This routine is the driver entry point for handling ioctl 23297 * requests for the media type or command set profile used by the 23298 * drive to operate on the media (DKIOCGMEDIAINFO). 23299 * 23300 * Arguments: dev - the device number 23301 * arg - pointer to user provided dk_minfo structure 23302 * specifying the media type, logical block size and 23303 * drive capacity. 23304 * flag - this argument is a pass through to ddi_copyxxx() 23305 * directly from the mode argument of ioctl(). 23306 * 23307 * Return Code: 0 23308 * EACCESS 23309 * EFAULT 23310 * ENXIO 23311 * EIO 23312 */ 23313 23314 static int 23315 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 23316 { 23317 struct sd_lun *un = NULL; 23318 struct uscsi_cmd com; 23319 struct scsi_inquiry *sinq; 23320 struct dk_minfo media_info; 23321 u_longlong_t media_capacity; 23322 uint64_t capacity; 23323 uint_t lbasize; 23324 uchar_t *out_data; 23325 uchar_t *rqbuf; 23326 int rval = 0; 23327 int rtn; 23328 sd_ssc_t *ssc; 23329 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 23330 (un->un_state == SD_STATE_OFFLINE)) { 23331 return (ENXIO); 23332 } 23333 23334 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 23335 23336 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 23337 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 23338 23339 /* Issue a TUR to determine if the drive is ready with media present */ 23340 ssc = sd_ssc_init(un); 23341 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA); 23342 if (rval == ENXIO) { 23343 goto done; 23344 } else if (rval != 0) { 23345 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23346 } 23347 23348 /* Now get configuration data */ 23349 if (ISCD(un)) { 23350 media_info.dki_media_type = DK_CDROM; 23351 23352 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 23353 if (un->un_f_mmc_cap == TRUE) { 23354 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, 23355 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 23356 SD_PATH_STANDARD); 23357 23358 if (rtn) { 23359 /* 23360 * We ignore all failures for CD and need to 23361 * put the assessment before processing code 23362 * to avoid missing assessment for FMA. 23363 */ 23364 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23365 /* 23366 * Failed for other than an illegal request 23367 * or command not supported 23368 */ 23369 if ((com.uscsi_status == STATUS_CHECK) && 23370 (com.uscsi_rqstatus == STATUS_GOOD)) { 23371 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 23372 (rqbuf[12] != 0x20)) { 23373 rval = EIO; 23374 goto no_assessment; 23375 } 23376 } 23377 } else { 23378 /* 23379 * The GET CONFIGURATION command succeeded 23380 * so set the media type according to the 23381 * returned data 23382 */ 23383 media_info.dki_media_type = out_data[6]; 23384 media_info.dki_media_type <<= 8; 23385 media_info.dki_media_type |= out_data[7]; 23386 } 23387 } 23388 } else { 23389 /* 23390 * The profile list is not available, so we attempt to identify 23391 * the media type based on the inquiry data 23392 */ 23393 sinq = un->un_sd->sd_inq; 23394 if ((sinq->inq_dtype == DTYPE_DIRECT) || 23395 (sinq->inq_dtype == DTYPE_OPTICAL)) { 23396 /* This is a direct access device or optical disk */ 23397 media_info.dki_media_type = DK_FIXED_DISK; 23398 23399 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 23400 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 23401 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 23402 media_info.dki_media_type = DK_ZIP; 23403 } else if ( 23404 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 23405 media_info.dki_media_type = DK_JAZ; 23406 } 23407 } 23408 } else { 23409 /* 23410 * Not a CD, direct access or optical disk so return 23411 * unknown media 23412 */ 23413 media_info.dki_media_type = DK_UNKNOWN; 23414 } 23415 } 23416 23417 /* Now read the capacity so we can provide the lbasize and capacity */ 23418 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 23419 SD_PATH_DIRECT); 23420 switch (rval) { 23421 case 0: 23422 break; 23423 case EACCES: 23424 rval = EACCES; 23425 goto done; 23426 default: 23427 rval = EIO; 23428 goto done; 23429 } 23430 23431 /* 23432 * If lun is expanded dynamically, update the un structure. 23433 */ 23434 mutex_enter(SD_MUTEX(un)); 23435 if ((un->un_f_blockcount_is_valid == TRUE) && 23436 (un->un_f_tgt_blocksize_is_valid == TRUE) && 23437 (capacity > un->un_blockcount)) { 23438 sd_update_block_info(un, lbasize, capacity); 23439 } 23440 mutex_exit(SD_MUTEX(un)); 23441 23442 media_info.dki_lbsize = lbasize; 23443 media_capacity = capacity; 23444 23445 /* 23446 * sd_send_scsi_READ_CAPACITY() reports capacity in 23447 * un->un_sys_blocksize chunks. So we need to convert it into 23448 * cap.lbasize chunks. 23449 */ 23450 media_capacity *= un->un_sys_blocksize; 23451 media_capacity /= lbasize; 23452 media_info.dki_capacity = media_capacity; 23453 23454 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 23455 rval = EFAULT; 23456 /* Put goto. Anybody might add some code below in future */ 23457 goto no_assessment; 23458 } 23459 done: 23460 if (rval != 0) { 23461 if (rval == EIO) 23462 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23463 else 23464 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23465 } 23466 no_assessment: 23467 sd_ssc_fini(ssc); 23468 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 23469 kmem_free(rqbuf, SENSE_LENGTH); 23470 return (rval); 23471 } 23472 23473 /* 23474 * Function: sd_get_media_info_ext 23475 * 23476 * Description: This routine is the driver entry point for handling ioctl 23477 * requests for the media type or command set profile used by the 23478 * drive to operate on the media (DKIOCGMEDIAINFOEXT). The 23479 * difference this ioctl and DKIOCGMEDIAINFO is the return value 23480 * of this ioctl contains both logical block size and physical 23481 * block size. 23482 * 23483 * 23484 * Arguments: dev - the device number 23485 * arg - pointer to user provided dk_minfo_ext structure 23486 * specifying the media type, logical block size, 23487 * physical block size and disk capacity. 23488 * flag - this argument is a pass through to ddi_copyxxx() 23489 * directly from the mode argument of ioctl(). 23490 * 23491 * Return Code: 0 23492 * EACCESS 23493 * EFAULT 23494 * ENXIO 23495 * EIO 23496 */ 23497 23498 static int 23499 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag) 23500 { 23501 struct sd_lun *un = NULL; 23502 struct uscsi_cmd com; 23503 struct scsi_inquiry *sinq; 23504 struct dk_minfo_ext media_info_ext; 23505 u_longlong_t media_capacity; 23506 uint64_t capacity; 23507 uint_t lbasize; 23508 uint_t pbsize; 23509 uchar_t *out_data; 23510 uchar_t *rqbuf; 23511 int rval = 0; 23512 int rtn; 23513 sd_ssc_t *ssc; 23514 23515 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 23516 (un->un_state == SD_STATE_OFFLINE)) { 23517 return (ENXIO); 23518 } 23519 23520 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_ext: entry\n"); 23521 23522 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 23523 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 23524 ssc = sd_ssc_init(un); 23525 23526 /* Issue a TUR to determine if the drive is ready with media present */ 23527 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA); 23528 if (rval == ENXIO) { 23529 goto done; 23530 } else if (rval != 0) { 23531 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23532 } 23533 23534 /* Now get configuration data */ 23535 if (ISCD(un)) { 23536 media_info_ext.dki_media_type = DK_CDROM; 23537 23538 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 23539 if (un->un_f_mmc_cap == TRUE) { 23540 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, 23541 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 23542 SD_PATH_STANDARD); 23543 23544 if (rtn) { 23545 /* 23546 * We ignore all failures for CD and need to 23547 * put the assessment before processing code 23548 * to avoid missing assessment for FMA. 23549 */ 23550 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23551 /* 23552 * Failed for other than an illegal request 23553 * or command not supported 23554 */ 23555 if ((com.uscsi_status == STATUS_CHECK) && 23556 (com.uscsi_rqstatus == STATUS_GOOD)) { 23557 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 23558 (rqbuf[12] != 0x20)) { 23559 rval = EIO; 23560 goto no_assessment; 23561 } 23562 } 23563 } else { 23564 /* 23565 * The GET CONFIGURATION command succeeded 23566 * so set the media type according to the 23567 * returned data 23568 */ 23569 media_info_ext.dki_media_type = out_data[6]; 23570 media_info_ext.dki_media_type <<= 8; 23571 media_info_ext.dki_media_type |= out_data[7]; 23572 } 23573 } 23574 } else { 23575 /* 23576 * The profile list is not available, so we attempt to identify 23577 * the media type based on the inquiry data 23578 */ 23579 sinq = un->un_sd->sd_inq; 23580 if ((sinq->inq_dtype == DTYPE_DIRECT) || 23581 (sinq->inq_dtype == DTYPE_OPTICAL)) { 23582 /* This is a direct access device or optical disk */ 23583 media_info_ext.dki_media_type = DK_FIXED_DISK; 23584 23585 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 23586 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 23587 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 23588 media_info_ext.dki_media_type = DK_ZIP; 23589 } else if ( 23590 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 23591 media_info_ext.dki_media_type = DK_JAZ; 23592 } 23593 } 23594 } else { 23595 /* 23596 * Not a CD, direct access or optical disk so return 23597 * unknown media 23598 */ 23599 media_info_ext.dki_media_type = DK_UNKNOWN; 23600 } 23601 } 23602 23603 /* 23604 * Now read the capacity so we can provide the lbasize, 23605 * pbsize and capacity. 23606 */ 23607 if (un->un_f_descr_format_supported) 23608 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 23609 &pbsize, SD_PATH_DIRECT); 23610 23611 if (rval != 0 || !un->un_f_descr_format_supported) { 23612 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 23613 SD_PATH_DIRECT); 23614 23615 switch (rval) { 23616 case 0: 23617 if (un->un_f_enable_rmw && 23618 un->un_phy_blocksize != 0) { 23619 pbsize = un->un_phy_blocksize; 23620 } else { 23621 pbsize = lbasize; 23622 } 23623 media_capacity = capacity; 23624 23625 /* 23626 * sd_send_scsi_READ_CAPACITY() reports capacity in 23627 * un->un_sys_blocksize chunks. So we need to convert 23628 * it into cap.lbsize chunks. 23629 */ 23630 if (un->un_f_has_removable_media) { 23631 media_capacity *= un->un_sys_blocksize; 23632 media_capacity /= lbasize; 23633 } 23634 break; 23635 case EACCES: 23636 rval = EACCES; 23637 goto done; 23638 default: 23639 rval = EIO; 23640 goto done; 23641 } 23642 } else { 23643 if (un->un_f_enable_rmw && 23644 !ISP2(pbsize % DEV_BSIZE)) { 23645 pbsize = SSD_SECSIZE; 23646 } else if (!ISP2(lbasize % DEV_BSIZE) || 23647 !ISP2(pbsize % DEV_BSIZE)) { 23648 pbsize = lbasize = DEV_BSIZE; 23649 } 23650 media_capacity = capacity; 23651 } 23652 23653 /* 23654 * If lun is expanded dynamically, update the un structure. 23655 */ 23656 mutex_enter(SD_MUTEX(un)); 23657 if ((un->un_f_blockcount_is_valid == TRUE) && 23658 (un->un_f_tgt_blocksize_is_valid == TRUE) && 23659 (capacity > un->un_blockcount)) { 23660 sd_update_block_info(un, lbasize, capacity); 23661 } 23662 mutex_exit(SD_MUTEX(un)); 23663 23664 media_info_ext.dki_lbsize = lbasize; 23665 media_info_ext.dki_pbsize = pbsize; 23666 media_info_ext.dki_capacity = media_capacity; 23667 23668 if (ddi_copyout(&media_info_ext, arg, sizeof (struct dk_minfo_ext), 23669 flag)) { 23670 rval = EFAULT; 23671 goto no_assessment; 23672 } 23673 done: 23674 if (rval != 0) { 23675 if (rval == EIO) 23676 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23677 else 23678 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23679 } 23680 no_assessment: 23681 sd_ssc_fini(ssc); 23682 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 23683 kmem_free(rqbuf, SENSE_LENGTH); 23684 return (rval); 23685 } 23686 23687 /* 23688 * Function: sd_watch_request_submit 23689 * 23690 * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit 23691 * depending on which is supported by device. 23692 */ 23693 static opaque_t 23694 sd_watch_request_submit(struct sd_lun *un) 23695 { 23696 dev_t dev; 23697 23698 /* All submissions are unified to use same device number */ 23699 dev = sd_make_device(SD_DEVINFO(un)); 23700 23701 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23702 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un), 23703 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23704 (caddr_t)dev)); 23705 } else { 23706 return (scsi_watch_request_submit(SD_SCSI_DEVP(un), 23707 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23708 (caddr_t)dev)); 23709 } 23710 } 23711 23712 23713 /* 23714 * Function: sd_check_media 23715 * 23716 * Description: This utility routine implements the functionality for the 23717 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23718 * driver state changes from that specified by the user 23719 * (inserted or ejected). For example, if the user specifies 23720 * DKIO_EJECTED and the current media state is inserted this 23721 * routine will immediately return DKIO_INSERTED. However, if the 23722 * current media state is not inserted the user thread will be 23723 * blocked until the drive state changes. If DKIO_NONE is specified 23724 * the user thread will block until a drive state change occurs. 23725 * 23726 * Arguments: dev - the device number 23727 * state - user pointer to a dkio_state, updated with the current 23728 * drive state at return. 23729 * 23730 * Return Code: ENXIO 23731 * EIO 23732 * EAGAIN 23733 * EINTR 23734 */ 23735 23736 static int 23737 sd_check_media(dev_t dev, enum dkio_state state) 23738 { 23739 struct sd_lun *un = NULL; 23740 enum dkio_state prev_state; 23741 opaque_t token = NULL; 23742 int rval = 0; 23743 sd_ssc_t *ssc; 23744 23745 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23746 return (ENXIO); 23747 } 23748 23749 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23750 23751 ssc = sd_ssc_init(un); 23752 23753 mutex_enter(SD_MUTEX(un)); 23754 23755 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23756 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23757 23758 prev_state = un->un_mediastate; 23759 23760 /* is there anything to do? */ 23761 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23762 /* 23763 * submit the request to the scsi_watch service; 23764 * scsi_media_watch_cb() does the real work 23765 */ 23766 mutex_exit(SD_MUTEX(un)); 23767 23768 /* 23769 * This change handles the case where a scsi watch request is 23770 * added to a device that is powered down. To accomplish this 23771 * we power up the device before adding the scsi watch request, 23772 * since the scsi watch sends a TUR directly to the device 23773 * which the device cannot handle if it is powered down. 23774 */ 23775 if (sd_pm_entry(un) != DDI_SUCCESS) { 23776 mutex_enter(SD_MUTEX(un)); 23777 goto done; 23778 } 23779 23780 token = sd_watch_request_submit(un); 23781 23782 sd_pm_exit(un); 23783 23784 mutex_enter(SD_MUTEX(un)); 23785 if (token == NULL) { 23786 rval = EAGAIN; 23787 goto done; 23788 } 23789 23790 /* 23791 * This is a special case IOCTL that doesn't return 23792 * until the media state changes. Routine sdpower 23793 * knows about and handles this so don't count it 23794 * as an active cmd in the driver, which would 23795 * keep the device busy to the pm framework. 23796 * If the count isn't decremented the device can't 23797 * be powered down. 23798 */ 23799 un->un_ncmds_in_driver--; 23800 ASSERT(un->un_ncmds_in_driver >= 0); 23801 23802 /* 23803 * if a prior request had been made, this will be the same 23804 * token, as scsi_watch was designed that way. 23805 */ 23806 un->un_swr_token = token; 23807 un->un_specified_mediastate = state; 23808 23809 /* 23810 * now wait for media change 23811 * we will not be signalled unless mediastate == state but it is 23812 * still better to test for this condition, since there is a 23813 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23814 */ 23815 SD_TRACE(SD_LOG_COMMON, un, 23816 "sd_check_media: waiting for media state change\n"); 23817 while (un->un_mediastate == state) { 23818 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23819 SD_TRACE(SD_LOG_COMMON, un, 23820 "sd_check_media: waiting for media state " 23821 "was interrupted\n"); 23822 un->un_ncmds_in_driver++; 23823 rval = EINTR; 23824 goto done; 23825 } 23826 SD_TRACE(SD_LOG_COMMON, un, 23827 "sd_check_media: received signal, state=%x\n", 23828 un->un_mediastate); 23829 } 23830 /* 23831 * Inc the counter to indicate the device once again 23832 * has an active outstanding cmd. 23833 */ 23834 un->un_ncmds_in_driver++; 23835 } 23836 23837 /* invalidate geometry */ 23838 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 23839 sr_ejected(un); 23840 } 23841 23842 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 23843 uint64_t capacity; 23844 uint_t lbasize; 23845 23846 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 23847 mutex_exit(SD_MUTEX(un)); 23848 /* 23849 * Since the following routines use SD_PATH_DIRECT, we must 23850 * call PM directly before the upcoming disk accesses. This 23851 * may cause the disk to be power/spin up. 23852 */ 23853 23854 if (sd_pm_entry(un) == DDI_SUCCESS) { 23855 rval = sd_send_scsi_READ_CAPACITY(ssc, 23856 &capacity, &lbasize, SD_PATH_DIRECT); 23857 if (rval != 0) { 23858 sd_pm_exit(un); 23859 if (rval == EIO) 23860 sd_ssc_assessment(ssc, 23861 SD_FMT_STATUS_CHECK); 23862 else 23863 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23864 mutex_enter(SD_MUTEX(un)); 23865 goto done; 23866 } 23867 } else { 23868 rval = EIO; 23869 mutex_enter(SD_MUTEX(un)); 23870 goto done; 23871 } 23872 mutex_enter(SD_MUTEX(un)); 23873 23874 sd_update_block_info(un, lbasize, capacity); 23875 23876 /* 23877 * Check if the media in the device is writable or not 23878 */ 23879 if (ISCD(un)) { 23880 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 23881 } 23882 23883 mutex_exit(SD_MUTEX(un)); 23884 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 23885 if ((cmlb_validate(un->un_cmlbhandle, 0, 23886 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 23887 sd_set_pstats(un); 23888 SD_TRACE(SD_LOG_IO_PARTITION, un, 23889 "sd_check_media: un:0x%p pstats created and " 23890 "set\n", un); 23891 } 23892 23893 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 23894 SD_PATH_DIRECT); 23895 23896 sd_pm_exit(un); 23897 23898 if (rval != 0) { 23899 if (rval == EIO) 23900 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23901 else 23902 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23903 } 23904 23905 mutex_enter(SD_MUTEX(un)); 23906 } 23907 done: 23908 sd_ssc_fini(ssc); 23909 un->un_f_watcht_stopped = FALSE; 23910 if (token != NULL && un->un_swr_token != NULL) { 23911 /* 23912 * Use of this local token and the mutex ensures that we avoid 23913 * some race conditions associated with terminating the 23914 * scsi watch. 23915 */ 23916 token = un->un_swr_token; 23917 mutex_exit(SD_MUTEX(un)); 23918 (void) scsi_watch_request_terminate(token, 23919 SCSI_WATCH_TERMINATE_WAIT); 23920 if (scsi_watch_get_ref_count(token) == 0) { 23921 mutex_enter(SD_MUTEX(un)); 23922 un->un_swr_token = (opaque_t)NULL; 23923 } else { 23924 mutex_enter(SD_MUTEX(un)); 23925 } 23926 } 23927 23928 /* 23929 * Update the capacity kstat value, if no media previously 23930 * (capacity kstat is 0) and a media has been inserted 23931 * (un_f_blockcount_is_valid == TRUE) 23932 */ 23933 if (un->un_errstats) { 23934 struct sd_errstats *stp = NULL; 23935 23936 stp = (struct sd_errstats *)un->un_errstats->ks_data; 23937 if ((stp->sd_capacity.value.ui64 == 0) && 23938 (un->un_f_blockcount_is_valid == TRUE)) { 23939 stp->sd_capacity.value.ui64 = 23940 (uint64_t)((uint64_t)un->un_blockcount * 23941 un->un_sys_blocksize); 23942 } 23943 } 23944 mutex_exit(SD_MUTEX(un)); 23945 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 23946 return (rval); 23947 } 23948 23949 23950 /* 23951 * Function: sd_delayed_cv_broadcast 23952 * 23953 * Description: Delayed cv_broadcast to allow for target to recover from media 23954 * insertion. 23955 * 23956 * Arguments: arg - driver soft state (unit) structure 23957 */ 23958 23959 static void 23960 sd_delayed_cv_broadcast(void *arg) 23961 { 23962 struct sd_lun *un = arg; 23963 23964 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 23965 23966 mutex_enter(SD_MUTEX(un)); 23967 un->un_dcvb_timeid = NULL; 23968 cv_broadcast(&un->un_state_cv); 23969 mutex_exit(SD_MUTEX(un)); 23970 } 23971 23972 23973 /* 23974 * Function: sd_media_watch_cb 23975 * 23976 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 23977 * routine processes the TUR sense data and updates the driver 23978 * state if a transition has occurred. The user thread 23979 * (sd_check_media) is then signalled. 23980 * 23981 * Arguments: arg - the device 'dev_t' is used for context to discriminate 23982 * among multiple watches that share this callback function 23983 * resultp - scsi watch facility result packet containing scsi 23984 * packet, status byte and sense data 23985 * 23986 * Return Code: 0 for success, -1 for failure 23987 */ 23988 23989 static int 23990 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 23991 { 23992 struct sd_lun *un; 23993 struct scsi_status *statusp = resultp->statusp; 23994 uint8_t *sensep = (uint8_t *)resultp->sensep; 23995 enum dkio_state state = DKIO_NONE; 23996 dev_t dev = (dev_t)arg; 23997 uchar_t actual_sense_length; 23998 uint8_t skey, asc, ascq; 23999 24000 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24001 return (-1); 24002 } 24003 actual_sense_length = resultp->actual_sense_length; 24004 24005 mutex_enter(SD_MUTEX(un)); 24006 SD_TRACE(SD_LOG_COMMON, un, 24007 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24008 *((char *)statusp), (void *)sensep, actual_sense_length); 24009 24010 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24011 un->un_mediastate = DKIO_DEV_GONE; 24012 cv_broadcast(&un->un_state_cv); 24013 mutex_exit(SD_MUTEX(un)); 24014 24015 return (0); 24016 } 24017 24018 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 24019 if (sd_gesn_media_data_valid(resultp->mmc_data)) { 24020 if ((resultp->mmc_data[5] & 24021 SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) { 24022 state = DKIO_INSERTED; 24023 } else { 24024 state = DKIO_EJECTED; 24025 } 24026 if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) == 24027 SD_GESN_MEDIA_EVENT_EJECTREQUEST) { 24028 sd_log_eject_request_event(un, KM_NOSLEEP); 24029 } 24030 } 24031 } else if (sensep != NULL) { 24032 /* 24033 * If there was a check condition then sensep points to valid 24034 * sense data. If status was not a check condition but a 24035 * reservation or busy status then the new state is DKIO_NONE. 24036 */ 24037 skey = scsi_sense_key(sensep); 24038 asc = scsi_sense_asc(sensep); 24039 ascq = scsi_sense_ascq(sensep); 24040 24041 SD_INFO(SD_LOG_COMMON, un, 24042 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24043 skey, asc, ascq); 24044 /* This routine only uses up to 13 bytes of sense data. */ 24045 if (actual_sense_length >= 13) { 24046 if (skey == KEY_UNIT_ATTENTION) { 24047 if (asc == 0x28) { 24048 state = DKIO_INSERTED; 24049 } 24050 } else if (skey == KEY_NOT_READY) { 24051 /* 24052 * Sense data of 02/06/00 means that the 24053 * drive could not read the media (No 24054 * reference position found). In this case 24055 * to prevent a hang on the DKIOCSTATE IOCTL 24056 * we set the media state to DKIO_INSERTED. 24057 */ 24058 if (asc == 0x06 && ascq == 0x00) 24059 state = DKIO_INSERTED; 24060 24061 /* 24062 * if 02/04/02 means that the host 24063 * should send start command. Explicitly 24064 * leave the media state as is 24065 * (inserted) as the media is inserted 24066 * and host has stopped device for PM 24067 * reasons. Upon next true read/write 24068 * to this media will bring the 24069 * device to the right state good for 24070 * media access. 24071 */ 24072 if (asc == 0x3a) { 24073 state = DKIO_EJECTED; 24074 } else { 24075 /* 24076 * If the drive is busy with an 24077 * operation or long write, keep the 24078 * media in an inserted state. 24079 */ 24080 24081 if ((asc == 0x04) && 24082 ((ascq == 0x02) || 24083 (ascq == 0x07) || 24084 (ascq == 0x08))) { 24085 state = DKIO_INSERTED; 24086 } 24087 } 24088 } else if (skey == KEY_NO_SENSE) { 24089 if ((asc == 0x00) && (ascq == 0x00)) { 24090 /* 24091 * Sense Data 00/00/00 does not provide 24092 * any information about the state of 24093 * the media. Ignore it. 24094 */ 24095 mutex_exit(SD_MUTEX(un)); 24096 return (0); 24097 } 24098 } 24099 } 24100 } else if ((*((char *)statusp) == STATUS_GOOD) && 24101 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24102 state = DKIO_INSERTED; 24103 } 24104 24105 SD_TRACE(SD_LOG_COMMON, un, 24106 "sd_media_watch_cb: state=%x, specified=%x\n", 24107 state, un->un_specified_mediastate); 24108 24109 /* 24110 * now signal the waiting thread if this is *not* the specified state; 24111 * delay the signal if the state is DKIO_INSERTED to allow the target 24112 * to recover 24113 */ 24114 if (state != un->un_specified_mediastate) { 24115 un->un_mediastate = state; 24116 if (state == DKIO_INSERTED) { 24117 /* 24118 * delay the signal to give the drive a chance 24119 * to do what it apparently needs to do 24120 */ 24121 SD_TRACE(SD_LOG_COMMON, un, 24122 "sd_media_watch_cb: delayed cv_broadcast\n"); 24123 if (un->un_dcvb_timeid == NULL) { 24124 un->un_dcvb_timeid = 24125 timeout(sd_delayed_cv_broadcast, un, 24126 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24127 } 24128 } else { 24129 SD_TRACE(SD_LOG_COMMON, un, 24130 "sd_media_watch_cb: immediate cv_broadcast\n"); 24131 cv_broadcast(&un->un_state_cv); 24132 } 24133 } 24134 mutex_exit(SD_MUTEX(un)); 24135 return (0); 24136 } 24137 24138 24139 /* 24140 * Function: sd_dkio_get_temp 24141 * 24142 * Description: This routine is the driver entry point for handling ioctl 24143 * requests to get the disk temperature. 24144 * 24145 * Arguments: dev - the device number 24146 * arg - pointer to user provided dk_temperature structure. 24147 * flag - this argument is a pass through to ddi_copyxxx() 24148 * directly from the mode argument of ioctl(). 24149 * 24150 * Return Code: 0 24151 * EFAULT 24152 * ENXIO 24153 * EAGAIN 24154 */ 24155 24156 static int 24157 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24158 { 24159 struct sd_lun *un = NULL; 24160 struct dk_temperature *dktemp = NULL; 24161 uchar_t *temperature_page; 24162 int rval = 0; 24163 int path_flag = SD_PATH_STANDARD; 24164 sd_ssc_t *ssc; 24165 24166 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24167 return (ENXIO); 24168 } 24169 24170 ssc = sd_ssc_init(un); 24171 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24172 24173 /* copyin the disk temp argument to get the user flags */ 24174 if (ddi_copyin((void *)arg, dktemp, 24175 sizeof (struct dk_temperature), flag) != 0) { 24176 rval = EFAULT; 24177 goto done; 24178 } 24179 24180 /* Initialize the temperature to invalid. */ 24181 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24182 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24183 24184 /* 24185 * Note: Investigate removing the "bypass pm" semantic. 24186 * Can we just bypass PM always? 24187 */ 24188 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24189 path_flag = SD_PATH_DIRECT; 24190 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24191 mutex_enter(&un->un_pm_mutex); 24192 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24193 /* 24194 * If DKT_BYPASS_PM is set, and the drive happens to be 24195 * in low power mode, we can not wake it up, Need to 24196 * return EAGAIN. 24197 */ 24198 mutex_exit(&un->un_pm_mutex); 24199 rval = EAGAIN; 24200 goto done; 24201 } else { 24202 /* 24203 * Indicate to PM the device is busy. This is required 24204 * to avoid a race - i.e. the ioctl is issuing a 24205 * command and the pm framework brings down the device 24206 * to low power mode (possible power cut-off on some 24207 * platforms). 24208 */ 24209 mutex_exit(&un->un_pm_mutex); 24210 if (sd_pm_entry(un) != DDI_SUCCESS) { 24211 rval = EAGAIN; 24212 goto done; 24213 } 24214 } 24215 } 24216 24217 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24218 24219 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page, 24220 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag); 24221 if (rval != 0) 24222 goto done2; 24223 24224 /* 24225 * For the current temperature verify that the parameter length is 0x02 24226 * and the parameter code is 0x00 24227 */ 24228 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24229 (temperature_page[5] == 0x00)) { 24230 if (temperature_page[9] == 0xFF) { 24231 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24232 } else { 24233 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24234 } 24235 } 24236 24237 /* 24238 * For the reference temperature verify that the parameter 24239 * length is 0x02 and the parameter code is 0x01 24240 */ 24241 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24242 (temperature_page[11] == 0x01)) { 24243 if (temperature_page[15] == 0xFF) { 24244 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24245 } else { 24246 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24247 } 24248 } 24249 24250 /* Do the copyout regardless of the temperature commands status. */ 24251 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24252 flag) != 0) { 24253 rval = EFAULT; 24254 goto done1; 24255 } 24256 24257 done2: 24258 if (rval != 0) { 24259 if (rval == EIO) 24260 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24261 else 24262 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24263 } 24264 done1: 24265 if (path_flag == SD_PATH_DIRECT) { 24266 sd_pm_exit(un); 24267 } 24268 24269 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24270 done: 24271 sd_ssc_fini(ssc); 24272 if (dktemp != NULL) { 24273 kmem_free(dktemp, sizeof (struct dk_temperature)); 24274 } 24275 24276 return (rval); 24277 } 24278 24279 24280 /* 24281 * Function: sd_log_page_supported 24282 * 24283 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24284 * supported log pages. 24285 * 24286 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 24287 * structure for this target. 24288 * log_page - 24289 * 24290 * Return Code: -1 - on error (log sense is optional and may not be supported). 24291 * 0 - log page not found. 24292 * 1 - log page found. 24293 */ 24294 24295 static int 24296 sd_log_page_supported(sd_ssc_t *ssc, int log_page) 24297 { 24298 uchar_t *log_page_data; 24299 int i; 24300 int match = 0; 24301 int log_size; 24302 int status = 0; 24303 struct sd_lun *un; 24304 24305 ASSERT(ssc != NULL); 24306 un = ssc->ssc_un; 24307 ASSERT(un != NULL); 24308 24309 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24310 24311 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0, 24312 SD_PATH_DIRECT); 24313 24314 if (status != 0) { 24315 if (status == EIO) { 24316 /* 24317 * Some disks do not support log sense, we 24318 * should ignore this kind of error(sense key is 24319 * 0x5 - illegal request). 24320 */ 24321 uint8_t *sensep; 24322 int senlen; 24323 24324 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 24325 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 24326 ssc->ssc_uscsi_cmd->uscsi_rqresid); 24327 24328 if (senlen > 0 && 24329 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 24330 sd_ssc_assessment(ssc, 24331 SD_FMT_IGNORE_COMPROMISE); 24332 } else { 24333 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24334 } 24335 } else { 24336 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24337 } 24338 24339 SD_ERROR(SD_LOG_COMMON, un, 24340 "sd_log_page_supported: failed log page retrieval\n"); 24341 kmem_free(log_page_data, 0xFF); 24342 return (-1); 24343 } 24344 24345 log_size = log_page_data[3]; 24346 24347 /* 24348 * The list of supported log pages start from the fourth byte. Check 24349 * until we run out of log pages or a match is found. 24350 */ 24351 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24352 if (log_page_data[i] == log_page) { 24353 match++; 24354 } 24355 } 24356 kmem_free(log_page_data, 0xFF); 24357 return (match); 24358 } 24359 24360 24361 /* 24362 * Function: sd_mhdioc_failfast 24363 * 24364 * Description: This routine is the driver entry point for handling ioctl 24365 * requests to enable/disable the multihost failfast option. 24366 * (MHIOCENFAILFAST) 24367 * 24368 * Arguments: dev - the device number 24369 * arg - user specified probing interval. 24370 * flag - this argument is a pass through to ddi_copyxxx() 24371 * directly from the mode argument of ioctl(). 24372 * 24373 * Return Code: 0 24374 * EFAULT 24375 * ENXIO 24376 */ 24377 24378 static int 24379 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24380 { 24381 struct sd_lun *un = NULL; 24382 int mh_time; 24383 int rval = 0; 24384 24385 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24386 return (ENXIO); 24387 } 24388 24389 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24390 return (EFAULT); 24391 24392 if (mh_time) { 24393 mutex_enter(SD_MUTEX(un)); 24394 un->un_resvd_status |= SD_FAILFAST; 24395 mutex_exit(SD_MUTEX(un)); 24396 /* 24397 * If mh_time is INT_MAX, then this ioctl is being used for 24398 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24399 */ 24400 if (mh_time != INT_MAX) { 24401 rval = sd_check_mhd(dev, mh_time); 24402 } 24403 } else { 24404 (void) sd_check_mhd(dev, 0); 24405 mutex_enter(SD_MUTEX(un)); 24406 un->un_resvd_status &= ~SD_FAILFAST; 24407 mutex_exit(SD_MUTEX(un)); 24408 } 24409 return (rval); 24410 } 24411 24412 24413 /* 24414 * Function: sd_mhdioc_takeown 24415 * 24416 * Description: This routine is the driver entry point for handling ioctl 24417 * requests to forcefully acquire exclusive access rights to the 24418 * multihost disk (MHIOCTKOWN). 24419 * 24420 * Arguments: dev - the device number 24421 * arg - user provided structure specifying the delay 24422 * parameters in milliseconds 24423 * flag - this argument is a pass through to ddi_copyxxx() 24424 * directly from the mode argument of ioctl(). 24425 * 24426 * Return Code: 0 24427 * EFAULT 24428 * ENXIO 24429 */ 24430 24431 static int 24432 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24433 { 24434 struct sd_lun *un = NULL; 24435 struct mhioctkown *tkown = NULL; 24436 int rval = 0; 24437 24438 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24439 return (ENXIO); 24440 } 24441 24442 if (arg != NULL) { 24443 tkown = (struct mhioctkown *) 24444 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24445 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24446 if (rval != 0) { 24447 rval = EFAULT; 24448 goto error; 24449 } 24450 } 24451 24452 rval = sd_take_ownership(dev, tkown); 24453 mutex_enter(SD_MUTEX(un)); 24454 if (rval == 0) { 24455 un->un_resvd_status |= SD_RESERVE; 24456 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24457 sd_reinstate_resv_delay = 24458 tkown->reinstate_resv_delay * 1000; 24459 } else { 24460 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24461 } 24462 /* 24463 * Give the scsi_watch routine interval set by 24464 * the MHIOCENFAILFAST ioctl precedence here. 24465 */ 24466 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24467 mutex_exit(SD_MUTEX(un)); 24468 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24469 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24470 "sd_mhdioc_takeown : %d\n", 24471 sd_reinstate_resv_delay); 24472 } else { 24473 mutex_exit(SD_MUTEX(un)); 24474 } 24475 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24476 sd_mhd_reset_notify_cb, (caddr_t)un); 24477 } else { 24478 un->un_resvd_status &= ~SD_RESERVE; 24479 mutex_exit(SD_MUTEX(un)); 24480 } 24481 24482 error: 24483 if (tkown != NULL) { 24484 kmem_free(tkown, sizeof (struct mhioctkown)); 24485 } 24486 return (rval); 24487 } 24488 24489 24490 /* 24491 * Function: sd_mhdioc_release 24492 * 24493 * Description: This routine is the driver entry point for handling ioctl 24494 * requests to release exclusive access rights to the multihost 24495 * disk (MHIOCRELEASE). 24496 * 24497 * Arguments: dev - the device number 24498 * 24499 * Return Code: 0 24500 * ENXIO 24501 */ 24502 24503 static int 24504 sd_mhdioc_release(dev_t dev) 24505 { 24506 struct sd_lun *un = NULL; 24507 timeout_id_t resvd_timeid_save; 24508 int resvd_status_save; 24509 int rval = 0; 24510 24511 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24512 return (ENXIO); 24513 } 24514 24515 mutex_enter(SD_MUTEX(un)); 24516 resvd_status_save = un->un_resvd_status; 24517 un->un_resvd_status &= 24518 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24519 if (un->un_resvd_timeid) { 24520 resvd_timeid_save = un->un_resvd_timeid; 24521 un->un_resvd_timeid = NULL; 24522 mutex_exit(SD_MUTEX(un)); 24523 (void) untimeout(resvd_timeid_save); 24524 } else { 24525 mutex_exit(SD_MUTEX(un)); 24526 } 24527 24528 /* 24529 * destroy any pending timeout thread that may be attempting to 24530 * reinstate reservation on this device. 24531 */ 24532 sd_rmv_resv_reclaim_req(dev); 24533 24534 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24535 mutex_enter(SD_MUTEX(un)); 24536 if ((un->un_mhd_token) && 24537 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24538 mutex_exit(SD_MUTEX(un)); 24539 (void) sd_check_mhd(dev, 0); 24540 } else { 24541 mutex_exit(SD_MUTEX(un)); 24542 } 24543 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24544 sd_mhd_reset_notify_cb, (caddr_t)un); 24545 } else { 24546 /* 24547 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24548 */ 24549 mutex_enter(SD_MUTEX(un)); 24550 un->un_resvd_status = resvd_status_save; 24551 mutex_exit(SD_MUTEX(un)); 24552 } 24553 return (rval); 24554 } 24555 24556 24557 /* 24558 * Function: sd_mhdioc_register_devid 24559 * 24560 * Description: This routine is the driver entry point for handling ioctl 24561 * requests to register the device id (MHIOCREREGISTERDEVID). 24562 * 24563 * Note: The implementation for this ioctl has been updated to 24564 * be consistent with the original PSARC case (1999/357) 24565 * (4375899, 4241671, 4220005) 24566 * 24567 * Arguments: dev - the device number 24568 * 24569 * Return Code: 0 24570 * ENXIO 24571 */ 24572 24573 static int 24574 sd_mhdioc_register_devid(dev_t dev) 24575 { 24576 struct sd_lun *un = NULL; 24577 int rval = 0; 24578 sd_ssc_t *ssc; 24579 24580 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24581 return (ENXIO); 24582 } 24583 24584 ASSERT(!mutex_owned(SD_MUTEX(un))); 24585 24586 mutex_enter(SD_MUTEX(un)); 24587 24588 /* If a devid already exists, de-register it */ 24589 if (un->un_devid != NULL) { 24590 ddi_devid_unregister(SD_DEVINFO(un)); 24591 /* 24592 * After unregister devid, needs to free devid memory 24593 */ 24594 ddi_devid_free(un->un_devid); 24595 un->un_devid = NULL; 24596 } 24597 24598 /* Check for reservation conflict */ 24599 mutex_exit(SD_MUTEX(un)); 24600 ssc = sd_ssc_init(un); 24601 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 24602 mutex_enter(SD_MUTEX(un)); 24603 24604 switch (rval) { 24605 case 0: 24606 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24607 break; 24608 case EACCES: 24609 break; 24610 default: 24611 rval = EIO; 24612 } 24613 24614 mutex_exit(SD_MUTEX(un)); 24615 if (rval != 0) { 24616 if (rval == EIO) 24617 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24618 else 24619 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24620 } 24621 sd_ssc_fini(ssc); 24622 return (rval); 24623 } 24624 24625 24626 /* 24627 * Function: sd_mhdioc_inkeys 24628 * 24629 * Description: This routine is the driver entry point for handling ioctl 24630 * requests to issue the SCSI-3 Persistent In Read Keys command 24631 * to the device (MHIOCGRP_INKEYS). 24632 * 24633 * Arguments: dev - the device number 24634 * arg - user provided in_keys structure 24635 * flag - this argument is a pass through to ddi_copyxxx() 24636 * directly from the mode argument of ioctl(). 24637 * 24638 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24639 * ENXIO 24640 * EFAULT 24641 */ 24642 24643 static int 24644 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24645 { 24646 struct sd_lun *un; 24647 mhioc_inkeys_t inkeys; 24648 int rval = 0; 24649 24650 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24651 return (ENXIO); 24652 } 24653 24654 #ifdef _MULTI_DATAMODEL 24655 switch (ddi_model_convert_from(flag & FMODELS)) { 24656 case DDI_MODEL_ILP32: { 24657 struct mhioc_inkeys32 inkeys32; 24658 24659 if (ddi_copyin(arg, &inkeys32, 24660 sizeof (struct mhioc_inkeys32), flag) != 0) { 24661 return (EFAULT); 24662 } 24663 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24664 if ((rval = sd_persistent_reservation_in_read_keys(un, 24665 &inkeys, flag)) != 0) { 24666 return (rval); 24667 } 24668 inkeys32.generation = inkeys.generation; 24669 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24670 flag) != 0) { 24671 return (EFAULT); 24672 } 24673 break; 24674 } 24675 case DDI_MODEL_NONE: 24676 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24677 flag) != 0) { 24678 return (EFAULT); 24679 } 24680 if ((rval = sd_persistent_reservation_in_read_keys(un, 24681 &inkeys, flag)) != 0) { 24682 return (rval); 24683 } 24684 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24685 flag) != 0) { 24686 return (EFAULT); 24687 } 24688 break; 24689 } 24690 24691 #else /* ! _MULTI_DATAMODEL */ 24692 24693 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24694 return (EFAULT); 24695 } 24696 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24697 if (rval != 0) { 24698 return (rval); 24699 } 24700 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24701 return (EFAULT); 24702 } 24703 24704 #endif /* _MULTI_DATAMODEL */ 24705 24706 return (rval); 24707 } 24708 24709 24710 /* 24711 * Function: sd_mhdioc_inresv 24712 * 24713 * Description: This routine is the driver entry point for handling ioctl 24714 * requests to issue the SCSI-3 Persistent In Read Reservations 24715 * command to the device (MHIOCGRP_INKEYS). 24716 * 24717 * Arguments: dev - the device number 24718 * arg - user provided in_resv structure 24719 * flag - this argument is a pass through to ddi_copyxxx() 24720 * directly from the mode argument of ioctl(). 24721 * 24722 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24723 * ENXIO 24724 * EFAULT 24725 */ 24726 24727 static int 24728 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24729 { 24730 struct sd_lun *un; 24731 mhioc_inresvs_t inresvs; 24732 int rval = 0; 24733 24734 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24735 return (ENXIO); 24736 } 24737 24738 #ifdef _MULTI_DATAMODEL 24739 24740 switch (ddi_model_convert_from(flag & FMODELS)) { 24741 case DDI_MODEL_ILP32: { 24742 struct mhioc_inresvs32 inresvs32; 24743 24744 if (ddi_copyin(arg, &inresvs32, 24745 sizeof (struct mhioc_inresvs32), flag) != 0) { 24746 return (EFAULT); 24747 } 24748 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24749 if ((rval = sd_persistent_reservation_in_read_resv(un, 24750 &inresvs, flag)) != 0) { 24751 return (rval); 24752 } 24753 inresvs32.generation = inresvs.generation; 24754 if (ddi_copyout(&inresvs32, arg, 24755 sizeof (struct mhioc_inresvs32), flag) != 0) { 24756 return (EFAULT); 24757 } 24758 break; 24759 } 24760 case DDI_MODEL_NONE: 24761 if (ddi_copyin(arg, &inresvs, 24762 sizeof (mhioc_inresvs_t), flag) != 0) { 24763 return (EFAULT); 24764 } 24765 if ((rval = sd_persistent_reservation_in_read_resv(un, 24766 &inresvs, flag)) != 0) { 24767 return (rval); 24768 } 24769 if (ddi_copyout(&inresvs, arg, 24770 sizeof (mhioc_inresvs_t), flag) != 0) { 24771 return (EFAULT); 24772 } 24773 break; 24774 } 24775 24776 #else /* ! _MULTI_DATAMODEL */ 24777 24778 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24779 return (EFAULT); 24780 } 24781 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24782 if (rval != 0) { 24783 return (rval); 24784 } 24785 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24786 return (EFAULT); 24787 } 24788 24789 #endif /* ! _MULTI_DATAMODEL */ 24790 24791 return (rval); 24792 } 24793 24794 24795 /* 24796 * The following routines support the clustering functionality described below 24797 * and implement lost reservation reclaim functionality. 24798 * 24799 * Clustering 24800 * ---------- 24801 * The clustering code uses two different, independent forms of SCSI 24802 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24803 * Persistent Group Reservations. For any particular disk, it will use either 24804 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24805 * 24806 * SCSI-2 24807 * The cluster software takes ownership of a multi-hosted disk by issuing the 24808 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24809 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 24810 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 24811 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 24812 * driver. The meaning of failfast is that if the driver (on this host) ever 24813 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 24814 * it should immediately panic the host. The motivation for this ioctl is that 24815 * if this host does encounter reservation conflict, the underlying cause is 24816 * that some other host of the cluster has decided that this host is no longer 24817 * in the cluster and has seized control of the disks for itself. Since this 24818 * host is no longer in the cluster, it ought to panic itself. The 24819 * MHIOCENFAILFAST ioctl does two things: 24820 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24821 * error to panic the host 24822 * (b) it sets up a periodic timer to test whether this host still has 24823 * "access" (in that no other host has reserved the device): if the 24824 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24825 * purpose of that periodic timer is to handle scenarios where the host is 24826 * otherwise temporarily quiescent, temporarily doing no real i/o. 24827 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24828 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24829 * the device itself. 24830 * 24831 * SCSI-3 PGR 24832 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24833 * facility is supported through the shared multihost disk ioctls 24834 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24835 * MHIOCGRP_PREEMPTANDABORT) 24836 * 24837 * Reservation Reclaim: 24838 * -------------------- 24839 * To support the lost reservation reclaim operations this driver creates a 24840 * single thread to handle reinstating reservations on all devices that have 24841 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24842 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24843 * and the reservation reclaim thread loops through the requests to regain the 24844 * lost reservations. 24845 */ 24846 24847 /* 24848 * Function: sd_check_mhd() 24849 * 24850 * Description: This function sets up and submits a scsi watch request or 24851 * terminates an existing watch request. This routine is used in 24852 * support of reservation reclaim. 24853 * 24854 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24855 * among multiple watches that share the callback function 24856 * interval - the number of microseconds specifying the watch 24857 * interval for issuing TEST UNIT READY commands. If 24858 * set to 0 the watch should be terminated. If the 24859 * interval is set to 0 and if the device is required 24860 * to hold reservation while disabling failfast, the 24861 * watch is restarted with an interval of 24862 * reinstate_resv_delay. 24863 * 24864 * Return Code: 0 - Successful submit/terminate of scsi watch request 24865 * ENXIO - Indicates an invalid device was specified 24866 * EAGAIN - Unable to submit the scsi watch request 24867 */ 24868 24869 static int 24870 sd_check_mhd(dev_t dev, int interval) 24871 { 24872 struct sd_lun *un; 24873 opaque_t token; 24874 24875 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24876 return (ENXIO); 24877 } 24878 24879 /* is this a watch termination request? */ 24880 if (interval == 0) { 24881 mutex_enter(SD_MUTEX(un)); 24882 /* if there is an existing watch task then terminate it */ 24883 if (un->un_mhd_token) { 24884 token = un->un_mhd_token; 24885 un->un_mhd_token = NULL; 24886 mutex_exit(SD_MUTEX(un)); 24887 (void) scsi_watch_request_terminate(token, 24888 SCSI_WATCH_TERMINATE_ALL_WAIT); 24889 mutex_enter(SD_MUTEX(un)); 24890 } else { 24891 mutex_exit(SD_MUTEX(un)); 24892 /* 24893 * Note: If we return here we don't check for the 24894 * failfast case. This is the original legacy 24895 * implementation but perhaps we should be checking 24896 * the failfast case. 24897 */ 24898 return (0); 24899 } 24900 /* 24901 * If the device is required to hold reservation while 24902 * disabling failfast, we need to restart the scsi_watch 24903 * routine with an interval of reinstate_resv_delay. 24904 */ 24905 if (un->un_resvd_status & SD_RESERVE) { 24906 interval = sd_reinstate_resv_delay/1000; 24907 } else { 24908 /* no failfast so bail */ 24909 mutex_exit(SD_MUTEX(un)); 24910 return (0); 24911 } 24912 mutex_exit(SD_MUTEX(un)); 24913 } 24914 24915 /* 24916 * adjust minimum time interval to 1 second, 24917 * and convert from msecs to usecs 24918 */ 24919 if (interval > 0 && interval < 1000) { 24920 interval = 1000; 24921 } 24922 interval *= 1000; 24923 24924 /* 24925 * submit the request to the scsi_watch service 24926 */ 24927 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24928 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24929 if (token == NULL) { 24930 return (EAGAIN); 24931 } 24932 24933 /* 24934 * save token for termination later on 24935 */ 24936 mutex_enter(SD_MUTEX(un)); 24937 un->un_mhd_token = token; 24938 mutex_exit(SD_MUTEX(un)); 24939 return (0); 24940 } 24941 24942 24943 /* 24944 * Function: sd_mhd_watch_cb() 24945 * 24946 * Description: This function is the call back function used by the scsi watch 24947 * facility. The scsi watch facility sends the "Test Unit Ready" 24948 * and processes the status. If applicable (i.e. a "Unit Attention" 24949 * status and automatic "Request Sense" not used) the scsi watch 24950 * facility will send a "Request Sense" and retrieve the sense data 24951 * to be passed to this callback function. In either case the 24952 * automatic "Request Sense" or the facility submitting one, this 24953 * callback is passed the status and sense data. 24954 * 24955 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24956 * among multiple watches that share this callback function 24957 * resultp - scsi watch facility result packet containing scsi 24958 * packet, status byte and sense data 24959 * 24960 * Return Code: 0 - continue the watch task 24961 * non-zero - terminate the watch task 24962 */ 24963 24964 static int 24965 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24966 { 24967 struct sd_lun *un; 24968 struct scsi_status *statusp; 24969 uint8_t *sensep; 24970 struct scsi_pkt *pkt; 24971 uchar_t actual_sense_length; 24972 dev_t dev = (dev_t)arg; 24973 24974 ASSERT(resultp != NULL); 24975 statusp = resultp->statusp; 24976 sensep = (uint8_t *)resultp->sensep; 24977 pkt = resultp->pkt; 24978 actual_sense_length = resultp->actual_sense_length; 24979 24980 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24981 return (ENXIO); 24982 } 24983 24984 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24985 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 24986 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 24987 24988 /* Begin processing of the status and/or sense data */ 24989 if (pkt->pkt_reason != CMD_CMPLT) { 24990 /* Handle the incomplete packet */ 24991 sd_mhd_watch_incomplete(un, pkt); 24992 return (0); 24993 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 24994 if (*((unsigned char *)statusp) 24995 == STATUS_RESERVATION_CONFLICT) { 24996 /* 24997 * Handle a reservation conflict by panicking if 24998 * configured for failfast or by logging the conflict 24999 * and updating the reservation status 25000 */ 25001 mutex_enter(SD_MUTEX(un)); 25002 if ((un->un_resvd_status & SD_FAILFAST) && 25003 (sd_failfast_enable)) { 25004 sd_panic_for_res_conflict(un); 25005 /*NOTREACHED*/ 25006 } 25007 SD_INFO(SD_LOG_IOCTL_MHD, un, 25008 "sd_mhd_watch_cb: Reservation Conflict\n"); 25009 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25010 mutex_exit(SD_MUTEX(un)); 25011 } 25012 } 25013 25014 if (sensep != NULL) { 25015 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25016 mutex_enter(SD_MUTEX(un)); 25017 if ((scsi_sense_asc(sensep) == 25018 SD_SCSI_RESET_SENSE_CODE) && 25019 (un->un_resvd_status & SD_RESERVE)) { 25020 /* 25021 * The additional sense code indicates a power 25022 * on or bus device reset has occurred; update 25023 * the reservation status. 25024 */ 25025 un->un_resvd_status |= 25026 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25027 SD_INFO(SD_LOG_IOCTL_MHD, un, 25028 "sd_mhd_watch_cb: Lost Reservation\n"); 25029 } 25030 } else { 25031 return (0); 25032 } 25033 } else { 25034 mutex_enter(SD_MUTEX(un)); 25035 } 25036 25037 if ((un->un_resvd_status & SD_RESERVE) && 25038 (un->un_resvd_status & SD_LOST_RESERVE)) { 25039 if (un->un_resvd_status & SD_WANT_RESERVE) { 25040 /* 25041 * A reset occurred in between the last probe and this 25042 * one so if a timeout is pending cancel it. 25043 */ 25044 if (un->un_resvd_timeid) { 25045 timeout_id_t temp_id = un->un_resvd_timeid; 25046 un->un_resvd_timeid = NULL; 25047 mutex_exit(SD_MUTEX(un)); 25048 (void) untimeout(temp_id); 25049 mutex_enter(SD_MUTEX(un)); 25050 } 25051 un->un_resvd_status &= ~SD_WANT_RESERVE; 25052 } 25053 if (un->un_resvd_timeid == 0) { 25054 /* Schedule a timeout to handle the lost reservation */ 25055 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25056 (void *)dev, 25057 drv_usectohz(sd_reinstate_resv_delay)); 25058 } 25059 } 25060 mutex_exit(SD_MUTEX(un)); 25061 return (0); 25062 } 25063 25064 25065 /* 25066 * Function: sd_mhd_watch_incomplete() 25067 * 25068 * Description: This function is used to find out why a scsi pkt sent by the 25069 * scsi watch facility was not completed. Under some scenarios this 25070 * routine will return. Otherwise it will send a bus reset to see 25071 * if the drive is still online. 25072 * 25073 * Arguments: un - driver soft state (unit) structure 25074 * pkt - incomplete scsi pkt 25075 */ 25076 25077 static void 25078 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25079 { 25080 int be_chatty; 25081 int perr; 25082 25083 ASSERT(pkt != NULL); 25084 ASSERT(un != NULL); 25085 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25086 perr = (pkt->pkt_statistics & STAT_PERR); 25087 25088 mutex_enter(SD_MUTEX(un)); 25089 if (un->un_state == SD_STATE_DUMPING) { 25090 mutex_exit(SD_MUTEX(un)); 25091 return; 25092 } 25093 25094 switch (pkt->pkt_reason) { 25095 case CMD_UNX_BUS_FREE: 25096 /* 25097 * If we had a parity error that caused the target to drop BSY*, 25098 * don't be chatty about it. 25099 */ 25100 if (perr && be_chatty) { 25101 be_chatty = 0; 25102 } 25103 break; 25104 case CMD_TAG_REJECT: 25105 /* 25106 * The SCSI-2 spec states that a tag reject will be sent by the 25107 * target if tagged queuing is not supported. A tag reject may 25108 * also be sent during certain initialization periods or to 25109 * control internal resources. For the latter case the target 25110 * may also return Queue Full. 25111 * 25112 * If this driver receives a tag reject from a target that is 25113 * going through an init period or controlling internal 25114 * resources tagged queuing will be disabled. This is a less 25115 * than optimal behavior but the driver is unable to determine 25116 * the target state and assumes tagged queueing is not supported 25117 */ 25118 pkt->pkt_flags = 0; 25119 un->un_tagflags = 0; 25120 25121 if (un->un_f_opt_queueing == TRUE) { 25122 un->un_throttle = min(un->un_throttle, 3); 25123 } else { 25124 un->un_throttle = 1; 25125 } 25126 mutex_exit(SD_MUTEX(un)); 25127 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25128 mutex_enter(SD_MUTEX(un)); 25129 break; 25130 case CMD_INCOMPLETE: 25131 /* 25132 * The transport stopped with an abnormal state, fallthrough and 25133 * reset the target and/or bus unless selection did not complete 25134 * (indicated by STATE_GOT_BUS) in which case we don't want to 25135 * go through a target/bus reset 25136 */ 25137 if (pkt->pkt_state == STATE_GOT_BUS) { 25138 break; 25139 } 25140 /*FALLTHROUGH*/ 25141 25142 case CMD_TIMEOUT: 25143 default: 25144 /* 25145 * The lun may still be running the command, so a lun reset 25146 * should be attempted. If the lun reset fails or cannot be 25147 * issued, than try a target reset. Lastly try a bus reset. 25148 */ 25149 if ((pkt->pkt_statistics & 25150 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25151 int reset_retval = 0; 25152 mutex_exit(SD_MUTEX(un)); 25153 if (un->un_f_allow_bus_device_reset == TRUE) { 25154 if (un->un_f_lun_reset_enabled == TRUE) { 25155 reset_retval = 25156 scsi_reset(SD_ADDRESS(un), 25157 RESET_LUN); 25158 } 25159 if (reset_retval == 0) { 25160 reset_retval = 25161 scsi_reset(SD_ADDRESS(un), 25162 RESET_TARGET); 25163 } 25164 } 25165 if (reset_retval == 0) { 25166 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25167 } 25168 mutex_enter(SD_MUTEX(un)); 25169 } 25170 break; 25171 } 25172 25173 /* A device/bus reset has occurred; update the reservation status. */ 25174 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25175 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25176 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25177 un->un_resvd_status |= 25178 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25179 SD_INFO(SD_LOG_IOCTL_MHD, un, 25180 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25181 } 25182 } 25183 25184 /* 25185 * The disk has been turned off; Update the device state. 25186 * 25187 * Note: Should we be offlining the disk here? 25188 */ 25189 if (pkt->pkt_state == STATE_GOT_BUS) { 25190 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25191 "Disk not responding to selection\n"); 25192 if (un->un_state != SD_STATE_OFFLINE) { 25193 New_state(un, SD_STATE_OFFLINE); 25194 } 25195 } else if (be_chatty) { 25196 /* 25197 * suppress messages if they are all the same pkt reason; 25198 * with TQ, many (up to 256) are returned with the same 25199 * pkt_reason 25200 */ 25201 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25202 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25203 "sd_mhd_watch_incomplete: " 25204 "SCSI transport failed: reason '%s'\n", 25205 scsi_rname(pkt->pkt_reason)); 25206 } 25207 } 25208 un->un_last_pkt_reason = pkt->pkt_reason; 25209 mutex_exit(SD_MUTEX(un)); 25210 } 25211 25212 25213 /* 25214 * Function: sd_sname() 25215 * 25216 * Description: This is a simple little routine to return a string containing 25217 * a printable description of command status byte for use in 25218 * logging. 25219 * 25220 * Arguments: status - pointer to a status byte 25221 * 25222 * Return Code: char * - string containing status description. 25223 */ 25224 25225 static char * 25226 sd_sname(uchar_t status) 25227 { 25228 switch (status & STATUS_MASK) { 25229 case STATUS_GOOD: 25230 return ("good status"); 25231 case STATUS_CHECK: 25232 return ("check condition"); 25233 case STATUS_MET: 25234 return ("condition met"); 25235 case STATUS_BUSY: 25236 return ("busy"); 25237 case STATUS_INTERMEDIATE: 25238 return ("intermediate"); 25239 case STATUS_INTERMEDIATE_MET: 25240 return ("intermediate - condition met"); 25241 case STATUS_RESERVATION_CONFLICT: 25242 return ("reservation_conflict"); 25243 case STATUS_TERMINATED: 25244 return ("command terminated"); 25245 case STATUS_QFULL: 25246 return ("queue full"); 25247 default: 25248 return ("<unknown status>"); 25249 } 25250 } 25251 25252 25253 /* 25254 * Function: sd_mhd_resvd_recover() 25255 * 25256 * Description: This function adds a reservation entry to the 25257 * sd_resv_reclaim_request list and signals the reservation 25258 * reclaim thread that there is work pending. If the reservation 25259 * reclaim thread has not been previously created this function 25260 * will kick it off. 25261 * 25262 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25263 * among multiple watches that share this callback function 25264 * 25265 * Context: This routine is called by timeout() and is run in interrupt 25266 * context. It must not sleep or call other functions which may 25267 * sleep. 25268 */ 25269 25270 static void 25271 sd_mhd_resvd_recover(void *arg) 25272 { 25273 dev_t dev = (dev_t)arg; 25274 struct sd_lun *un; 25275 struct sd_thr_request *sd_treq = NULL; 25276 struct sd_thr_request *sd_cur = NULL; 25277 struct sd_thr_request *sd_prev = NULL; 25278 int already_there = 0; 25279 25280 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25281 return; 25282 } 25283 25284 mutex_enter(SD_MUTEX(un)); 25285 un->un_resvd_timeid = NULL; 25286 if (un->un_resvd_status & SD_WANT_RESERVE) { 25287 /* 25288 * There was a reset so don't issue the reserve, allow the 25289 * sd_mhd_watch_cb callback function to notice this and 25290 * reschedule the timeout for reservation. 25291 */ 25292 mutex_exit(SD_MUTEX(un)); 25293 return; 25294 } 25295 mutex_exit(SD_MUTEX(un)); 25296 25297 /* 25298 * Add this device to the sd_resv_reclaim_request list and the 25299 * sd_resv_reclaim_thread should take care of the rest. 25300 * 25301 * Note: We can't sleep in this context so if the memory allocation 25302 * fails allow the sd_mhd_watch_cb callback function to notice this and 25303 * reschedule the timeout for reservation. (4378460) 25304 */ 25305 sd_treq = (struct sd_thr_request *) 25306 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25307 if (sd_treq == NULL) { 25308 return; 25309 } 25310 25311 sd_treq->sd_thr_req_next = NULL; 25312 sd_treq->dev = dev; 25313 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25314 if (sd_tr.srq_thr_req_head == NULL) { 25315 sd_tr.srq_thr_req_head = sd_treq; 25316 } else { 25317 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25318 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25319 if (sd_cur->dev == dev) { 25320 /* 25321 * already in Queue so don't log 25322 * another request for the device 25323 */ 25324 already_there = 1; 25325 break; 25326 } 25327 sd_prev = sd_cur; 25328 } 25329 if (!already_there) { 25330 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25331 "logging request for %lx\n", dev); 25332 sd_prev->sd_thr_req_next = sd_treq; 25333 } else { 25334 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25335 } 25336 } 25337 25338 /* 25339 * Create a kernel thread to do the reservation reclaim and free up this 25340 * thread. We cannot block this thread while we go away to do the 25341 * reservation reclaim 25342 */ 25343 if (sd_tr.srq_resv_reclaim_thread == NULL) 25344 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25345 sd_resv_reclaim_thread, NULL, 25346 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25347 25348 /* Tell the reservation reclaim thread that it has work to do */ 25349 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25350 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25351 } 25352 25353 /* 25354 * Function: sd_resv_reclaim_thread() 25355 * 25356 * Description: This function implements the reservation reclaim operations 25357 * 25358 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25359 * among multiple watches that share this callback function 25360 */ 25361 25362 static void 25363 sd_resv_reclaim_thread() 25364 { 25365 struct sd_lun *un; 25366 struct sd_thr_request *sd_mhreq; 25367 25368 /* Wait for work */ 25369 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25370 if (sd_tr.srq_thr_req_head == NULL) { 25371 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25372 &sd_tr.srq_resv_reclaim_mutex); 25373 } 25374 25375 /* Loop while we have work */ 25376 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25377 un = ddi_get_soft_state(sd_state, 25378 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25379 if (un == NULL) { 25380 /* 25381 * softstate structure is NULL so just 25382 * dequeue the request and continue 25383 */ 25384 sd_tr.srq_thr_req_head = 25385 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25386 kmem_free(sd_tr.srq_thr_cur_req, 25387 sizeof (struct sd_thr_request)); 25388 continue; 25389 } 25390 25391 /* dequeue the request */ 25392 sd_mhreq = sd_tr.srq_thr_cur_req; 25393 sd_tr.srq_thr_req_head = 25394 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25395 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25396 25397 /* 25398 * Reclaim reservation only if SD_RESERVE is still set. There 25399 * may have been a call to MHIOCRELEASE before we got here. 25400 */ 25401 mutex_enter(SD_MUTEX(un)); 25402 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25403 /* 25404 * Note: The SD_LOST_RESERVE flag is cleared before 25405 * reclaiming the reservation. If this is done after the 25406 * call to sd_reserve_release a reservation loss in the 25407 * window between pkt completion of reserve cmd and 25408 * mutex_enter below may not be recognized 25409 */ 25410 un->un_resvd_status &= ~SD_LOST_RESERVE; 25411 mutex_exit(SD_MUTEX(un)); 25412 25413 if (sd_reserve_release(sd_mhreq->dev, 25414 SD_RESERVE) == 0) { 25415 mutex_enter(SD_MUTEX(un)); 25416 un->un_resvd_status |= SD_RESERVE; 25417 mutex_exit(SD_MUTEX(un)); 25418 SD_INFO(SD_LOG_IOCTL_MHD, un, 25419 "sd_resv_reclaim_thread: " 25420 "Reservation Recovered\n"); 25421 } else { 25422 mutex_enter(SD_MUTEX(un)); 25423 un->un_resvd_status |= SD_LOST_RESERVE; 25424 mutex_exit(SD_MUTEX(un)); 25425 SD_INFO(SD_LOG_IOCTL_MHD, un, 25426 "sd_resv_reclaim_thread: Failed " 25427 "Reservation Recovery\n"); 25428 } 25429 } else { 25430 mutex_exit(SD_MUTEX(un)); 25431 } 25432 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25433 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25434 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25435 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25436 /* 25437 * wakeup the destroy thread if anyone is waiting on 25438 * us to complete. 25439 */ 25440 cv_signal(&sd_tr.srq_inprocess_cv); 25441 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25442 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25443 } 25444 25445 /* 25446 * cleanup the sd_tr structure now that this thread will not exist 25447 */ 25448 ASSERT(sd_tr.srq_thr_req_head == NULL); 25449 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25450 sd_tr.srq_resv_reclaim_thread = NULL; 25451 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25452 thread_exit(); 25453 } 25454 25455 25456 /* 25457 * Function: sd_rmv_resv_reclaim_req() 25458 * 25459 * Description: This function removes any pending reservation reclaim requests 25460 * for the specified device. 25461 * 25462 * Arguments: dev - the device 'dev_t' 25463 */ 25464 25465 static void 25466 sd_rmv_resv_reclaim_req(dev_t dev) 25467 { 25468 struct sd_thr_request *sd_mhreq; 25469 struct sd_thr_request *sd_prev; 25470 25471 /* Remove a reservation reclaim request from the list */ 25472 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25473 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25474 /* 25475 * We are attempting to reinstate reservation for 25476 * this device. We wait for sd_reserve_release() 25477 * to return before we return. 25478 */ 25479 cv_wait(&sd_tr.srq_inprocess_cv, 25480 &sd_tr.srq_resv_reclaim_mutex); 25481 } else { 25482 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25483 if (sd_mhreq && sd_mhreq->dev == dev) { 25484 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25485 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25486 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25487 return; 25488 } 25489 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25490 if (sd_mhreq && sd_mhreq->dev == dev) { 25491 break; 25492 } 25493 sd_prev = sd_mhreq; 25494 } 25495 if (sd_mhreq != NULL) { 25496 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25497 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25498 } 25499 } 25500 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25501 } 25502 25503 25504 /* 25505 * Function: sd_mhd_reset_notify_cb() 25506 * 25507 * Description: This is a call back function for scsi_reset_notify. This 25508 * function updates the softstate reserved status and logs the 25509 * reset. The driver scsi watch facility callback function 25510 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25511 * will reclaim the reservation. 25512 * 25513 * Arguments: arg - driver soft state (unit) structure 25514 */ 25515 25516 static void 25517 sd_mhd_reset_notify_cb(caddr_t arg) 25518 { 25519 struct sd_lun *un = (struct sd_lun *)arg; 25520 25521 mutex_enter(SD_MUTEX(un)); 25522 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25523 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25524 SD_INFO(SD_LOG_IOCTL_MHD, un, 25525 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25526 } 25527 mutex_exit(SD_MUTEX(un)); 25528 } 25529 25530 25531 /* 25532 * Function: sd_take_ownership() 25533 * 25534 * Description: This routine implements an algorithm to achieve a stable 25535 * reservation on disks which don't implement priority reserve, 25536 * and makes sure that other host lose re-reservation attempts. 25537 * This algorithm contains of a loop that keeps issuing the RESERVE 25538 * for some period of time (min_ownership_delay, default 6 seconds) 25539 * During that loop, it looks to see if there has been a bus device 25540 * reset or bus reset (both of which cause an existing reservation 25541 * to be lost). If the reservation is lost issue RESERVE until a 25542 * period of min_ownership_delay with no resets has gone by, or 25543 * until max_ownership_delay has expired. This loop ensures that 25544 * the host really did manage to reserve the device, in spite of 25545 * resets. The looping for min_ownership_delay (default six 25546 * seconds) is important to early generation clustering products, 25547 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25548 * MHIOCENFAILFAST periodic timer of two seconds. By having 25549 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25550 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25551 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25552 * have already noticed, via the MHIOCENFAILFAST polling, that it 25553 * no longer "owns" the disk and will have panicked itself. Thus, 25554 * the host issuing the MHIOCTKOWN is assured (with timing 25555 * dependencies) that by the time it actually starts to use the 25556 * disk for real work, the old owner is no longer accessing it. 25557 * 25558 * min_ownership_delay is the minimum amount of time for which the 25559 * disk must be reserved continuously devoid of resets before the 25560 * MHIOCTKOWN ioctl will return success. 25561 * 25562 * max_ownership_delay indicates the amount of time by which the 25563 * take ownership should succeed or timeout with an error. 25564 * 25565 * Arguments: dev - the device 'dev_t' 25566 * *p - struct containing timing info. 25567 * 25568 * Return Code: 0 for success or error code 25569 */ 25570 25571 static int 25572 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25573 { 25574 struct sd_lun *un; 25575 int rval; 25576 int err; 25577 int reservation_count = 0; 25578 int min_ownership_delay = 6000000; /* in usec */ 25579 int max_ownership_delay = 30000000; /* in usec */ 25580 clock_t start_time; /* starting time of this algorithm */ 25581 clock_t end_time; /* time limit for giving up */ 25582 clock_t ownership_time; /* time limit for stable ownership */ 25583 clock_t current_time; 25584 clock_t previous_current_time; 25585 25586 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25587 return (ENXIO); 25588 } 25589 25590 /* 25591 * Attempt a device reservation. A priority reservation is requested. 25592 */ 25593 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25594 != SD_SUCCESS) { 25595 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25596 "sd_take_ownership: return(1)=%d\n", rval); 25597 return (rval); 25598 } 25599 25600 /* Update the softstate reserved status to indicate the reservation */ 25601 mutex_enter(SD_MUTEX(un)); 25602 un->un_resvd_status |= SD_RESERVE; 25603 un->un_resvd_status &= 25604 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25605 mutex_exit(SD_MUTEX(un)); 25606 25607 if (p != NULL) { 25608 if (p->min_ownership_delay != 0) { 25609 min_ownership_delay = p->min_ownership_delay * 1000; 25610 } 25611 if (p->max_ownership_delay != 0) { 25612 max_ownership_delay = p->max_ownership_delay * 1000; 25613 } 25614 } 25615 SD_INFO(SD_LOG_IOCTL_MHD, un, 25616 "sd_take_ownership: min, max delays: %d, %d\n", 25617 min_ownership_delay, max_ownership_delay); 25618 25619 start_time = ddi_get_lbolt(); 25620 current_time = start_time; 25621 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25622 end_time = start_time + drv_usectohz(max_ownership_delay); 25623 25624 while (current_time - end_time < 0) { 25625 delay(drv_usectohz(500000)); 25626 25627 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25628 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25629 mutex_enter(SD_MUTEX(un)); 25630 rval = (un->un_resvd_status & 25631 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25632 mutex_exit(SD_MUTEX(un)); 25633 break; 25634 } 25635 } 25636 previous_current_time = current_time; 25637 current_time = ddi_get_lbolt(); 25638 mutex_enter(SD_MUTEX(un)); 25639 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25640 ownership_time = ddi_get_lbolt() + 25641 drv_usectohz(min_ownership_delay); 25642 reservation_count = 0; 25643 } else { 25644 reservation_count++; 25645 } 25646 un->un_resvd_status |= SD_RESERVE; 25647 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25648 mutex_exit(SD_MUTEX(un)); 25649 25650 SD_INFO(SD_LOG_IOCTL_MHD, un, 25651 "sd_take_ownership: ticks for loop iteration=%ld, " 25652 "reservation=%s\n", (current_time - previous_current_time), 25653 reservation_count ? "ok" : "reclaimed"); 25654 25655 if (current_time - ownership_time >= 0 && 25656 reservation_count >= 4) { 25657 rval = 0; /* Achieved a stable ownership */ 25658 break; 25659 } 25660 if (current_time - end_time >= 0) { 25661 rval = EACCES; /* No ownership in max possible time */ 25662 break; 25663 } 25664 } 25665 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25666 "sd_take_ownership: return(2)=%d\n", rval); 25667 return (rval); 25668 } 25669 25670 25671 /* 25672 * Function: sd_reserve_release() 25673 * 25674 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25675 * PRIORITY RESERVE commands based on a user specified command type 25676 * 25677 * Arguments: dev - the device 'dev_t' 25678 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25679 * SD_RESERVE, SD_RELEASE 25680 * 25681 * Return Code: 0 or Error Code 25682 */ 25683 25684 static int 25685 sd_reserve_release(dev_t dev, int cmd) 25686 { 25687 struct uscsi_cmd *com = NULL; 25688 struct sd_lun *un = NULL; 25689 char cdb[CDB_GROUP0]; 25690 int rval; 25691 25692 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25693 (cmd == SD_PRIORITY_RESERVE)); 25694 25695 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25696 return (ENXIO); 25697 } 25698 25699 /* instantiate and initialize the command and cdb */ 25700 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25701 bzero(cdb, CDB_GROUP0); 25702 com->uscsi_flags = USCSI_SILENT; 25703 com->uscsi_timeout = un->un_reserve_release_time; 25704 com->uscsi_cdblen = CDB_GROUP0; 25705 com->uscsi_cdb = cdb; 25706 if (cmd == SD_RELEASE) { 25707 cdb[0] = SCMD_RELEASE; 25708 } else { 25709 cdb[0] = SCMD_RESERVE; 25710 } 25711 25712 /* Send the command. */ 25713 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25714 SD_PATH_STANDARD); 25715 25716 /* 25717 * "break" a reservation that is held by another host, by issuing a 25718 * reset if priority reserve is desired, and we could not get the 25719 * device. 25720 */ 25721 if ((cmd == SD_PRIORITY_RESERVE) && 25722 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25723 /* 25724 * First try to reset the LUN. If we cannot, then try a target 25725 * reset, followed by a bus reset if the target reset fails. 25726 */ 25727 int reset_retval = 0; 25728 if (un->un_f_lun_reset_enabled == TRUE) { 25729 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25730 } 25731 if (reset_retval == 0) { 25732 /* The LUN reset either failed or was not issued */ 25733 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25734 } 25735 if ((reset_retval == 0) && 25736 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25737 rval = EIO; 25738 kmem_free(com, sizeof (*com)); 25739 return (rval); 25740 } 25741 25742 bzero(com, sizeof (struct uscsi_cmd)); 25743 com->uscsi_flags = USCSI_SILENT; 25744 com->uscsi_cdb = cdb; 25745 com->uscsi_cdblen = CDB_GROUP0; 25746 com->uscsi_timeout = 5; 25747 25748 /* 25749 * Reissue the last reserve command, this time without request 25750 * sense. Assume that it is just a regular reserve command. 25751 */ 25752 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25753 SD_PATH_STANDARD); 25754 } 25755 25756 /* Return an error if still getting a reservation conflict. */ 25757 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25758 rval = EACCES; 25759 } 25760 25761 kmem_free(com, sizeof (*com)); 25762 return (rval); 25763 } 25764 25765 25766 #define SD_NDUMP_RETRIES 12 25767 /* 25768 * System Crash Dump routine 25769 */ 25770 25771 static int 25772 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25773 { 25774 int instance; 25775 int partition; 25776 int i; 25777 int err; 25778 struct sd_lun *un; 25779 struct scsi_pkt *wr_pktp; 25780 struct buf *wr_bp; 25781 struct buf wr_buf; 25782 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25783 daddr_t tgt_blkno; /* rmw - blkno for target */ 25784 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25785 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25786 size_t io_start_offset; 25787 int doing_rmw = FALSE; 25788 int rval; 25789 ssize_t dma_resid; 25790 daddr_t oblkno; 25791 diskaddr_t nblks = 0; 25792 diskaddr_t start_block; 25793 25794 instance = SDUNIT(dev); 25795 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25796 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 25797 return (ENXIO); 25798 } 25799 25800 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25801 25802 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25803 25804 partition = SDPART(dev); 25805 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25806 25807 if (!(NOT_DEVBSIZE(un))) { 25808 int secmask = 0; 25809 int blknomask = 0; 25810 25811 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 25812 secmask = un->un_tgt_blocksize - 1; 25813 25814 if (blkno & blknomask) { 25815 SD_TRACE(SD_LOG_DUMP, un, 25816 "sddump: dump start block not modulo %d\n", 25817 un->un_tgt_blocksize); 25818 return (EINVAL); 25819 } 25820 25821 if ((nblk * DEV_BSIZE) & secmask) { 25822 SD_TRACE(SD_LOG_DUMP, un, 25823 "sddump: dump length not modulo %d\n", 25824 un->un_tgt_blocksize); 25825 return (EINVAL); 25826 } 25827 25828 } 25829 25830 /* Validate blocks to dump at against partition size. */ 25831 25832 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 25833 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 25834 25835 if (NOT_DEVBSIZE(un)) { 25836 if ((blkno + nblk) > nblks) { 25837 SD_TRACE(SD_LOG_DUMP, un, 25838 "sddump: dump range larger than partition: " 25839 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25840 blkno, nblk, nblks); 25841 return (EINVAL); 25842 } 25843 } else { 25844 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) + 25845 (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) { 25846 SD_TRACE(SD_LOG_DUMP, un, 25847 "sddump: dump range larger than partition: " 25848 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25849 blkno, nblk, nblks); 25850 return (EINVAL); 25851 } 25852 } 25853 25854 mutex_enter(&un->un_pm_mutex); 25855 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25856 struct scsi_pkt *start_pktp; 25857 25858 mutex_exit(&un->un_pm_mutex); 25859 25860 /* 25861 * use pm framework to power on HBA 1st 25862 */ 25863 (void) pm_raise_power(SD_DEVINFO(un), 0, 25864 SD_PM_STATE_ACTIVE(un)); 25865 25866 /* 25867 * Dump no long uses sdpower to power on a device, it's 25868 * in-line here so it can be done in polled mode. 25869 */ 25870 25871 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25872 25873 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25874 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25875 25876 if (start_pktp == NULL) { 25877 /* We were not given a SCSI packet, fail. */ 25878 return (EIO); 25879 } 25880 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25881 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25882 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25883 start_pktp->pkt_flags = FLAG_NOINTR; 25884 25885 mutex_enter(SD_MUTEX(un)); 25886 SD_FILL_SCSI1_LUN(un, start_pktp); 25887 mutex_exit(SD_MUTEX(un)); 25888 /* 25889 * Scsi_poll returns 0 (success) if the command completes and 25890 * the status block is STATUS_GOOD. 25891 */ 25892 if (sd_scsi_poll(un, start_pktp) != 0) { 25893 scsi_destroy_pkt(start_pktp); 25894 return (EIO); 25895 } 25896 scsi_destroy_pkt(start_pktp); 25897 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un), 25898 SD_PM_STATE_CHANGE); 25899 } else { 25900 mutex_exit(&un->un_pm_mutex); 25901 } 25902 25903 mutex_enter(SD_MUTEX(un)); 25904 un->un_throttle = 0; 25905 25906 /* 25907 * The first time through, reset the specific target device. 25908 * However, when cpr calls sddump we know that sd is in a 25909 * a good state so no bus reset is required. 25910 * Clear sense data via Request Sense cmd. 25911 * In sddump we don't care about allow_bus_device_reset anymore 25912 */ 25913 25914 if ((un->un_state != SD_STATE_SUSPENDED) && 25915 (un->un_state != SD_STATE_DUMPING)) { 25916 25917 New_state(un, SD_STATE_DUMPING); 25918 25919 if (un->un_f_is_fibre == FALSE) { 25920 mutex_exit(SD_MUTEX(un)); 25921 /* 25922 * Attempt a bus reset for parallel scsi. 25923 * 25924 * Note: A bus reset is required because on some host 25925 * systems (i.e. E420R) a bus device reset is 25926 * insufficient to reset the state of the target. 25927 * 25928 * Note: Don't issue the reset for fibre-channel, 25929 * because this tends to hang the bus (loop) for 25930 * too long while everyone is logging out and in 25931 * and the deadman timer for dumping will fire 25932 * before the dump is complete. 25933 */ 25934 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25935 mutex_enter(SD_MUTEX(un)); 25936 Restore_state(un); 25937 mutex_exit(SD_MUTEX(un)); 25938 return (EIO); 25939 } 25940 25941 /* Delay to give the device some recovery time. */ 25942 drv_usecwait(10000); 25943 25944 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25945 SD_INFO(SD_LOG_DUMP, un, 25946 "sddump: sd_send_polled_RQS failed\n"); 25947 } 25948 mutex_enter(SD_MUTEX(un)); 25949 } 25950 } 25951 25952 /* 25953 * Convert the partition-relative block number to a 25954 * disk physical block number. 25955 */ 25956 if (NOT_DEVBSIZE(un)) { 25957 blkno += start_block; 25958 } else { 25959 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE); 25960 blkno += start_block; 25961 } 25962 25963 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25964 25965 25966 /* 25967 * Check if the device has a non-512 block size. 25968 */ 25969 wr_bp = NULL; 25970 if (NOT_DEVBSIZE(un)) { 25971 tgt_byte_offset = blkno * un->un_sys_blocksize; 25972 tgt_byte_count = nblk * un->un_sys_blocksize; 25973 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25974 (tgt_byte_count % un->un_tgt_blocksize)) { 25975 doing_rmw = TRUE; 25976 /* 25977 * Calculate the block number and number of block 25978 * in terms of the media block size. 25979 */ 25980 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25981 tgt_nblk = 25982 ((tgt_byte_offset + tgt_byte_count + 25983 (un->un_tgt_blocksize - 1)) / 25984 un->un_tgt_blocksize) - tgt_blkno; 25985 25986 /* 25987 * Invoke the routine which is going to do read part 25988 * of read-modify-write. 25989 * Note that this routine returns a pointer to 25990 * a valid bp in wr_bp. 25991 */ 25992 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25993 &wr_bp); 25994 if (err) { 25995 mutex_exit(SD_MUTEX(un)); 25996 return (err); 25997 } 25998 /* 25999 * Offset is being calculated as - 26000 * (original block # * system block size) - 26001 * (new block # * target block size) 26002 */ 26003 io_start_offset = 26004 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26005 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26006 26007 ASSERT((io_start_offset >= 0) && 26008 (io_start_offset < un->un_tgt_blocksize)); 26009 /* 26010 * Do the modify portion of read modify write. 26011 */ 26012 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26013 (size_t)nblk * un->un_sys_blocksize); 26014 } else { 26015 doing_rmw = FALSE; 26016 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26017 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26018 } 26019 26020 /* Convert blkno and nblk to target blocks */ 26021 blkno = tgt_blkno; 26022 nblk = tgt_nblk; 26023 } else { 26024 wr_bp = &wr_buf; 26025 bzero(wr_bp, sizeof (struct buf)); 26026 wr_bp->b_flags = B_BUSY; 26027 wr_bp->b_un.b_addr = addr; 26028 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26029 wr_bp->b_resid = 0; 26030 } 26031 26032 mutex_exit(SD_MUTEX(un)); 26033 26034 /* 26035 * Obtain a SCSI packet for the write command. 26036 * It should be safe to call the allocator here without 26037 * worrying about being locked for DVMA mapping because 26038 * the address we're passed is already a DVMA mapping 26039 * 26040 * We are also not going to worry about semaphore ownership 26041 * in the dump buffer. Dumping is single threaded at present. 26042 */ 26043 26044 wr_pktp = NULL; 26045 26046 dma_resid = wr_bp->b_bcount; 26047 oblkno = blkno; 26048 26049 if (!(NOT_DEVBSIZE(un))) { 26050 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE); 26051 } 26052 26053 while (dma_resid != 0) { 26054 26055 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26056 wr_bp->b_flags &= ~B_ERROR; 26057 26058 if (un->un_partial_dma_supported == 1) { 26059 blkno = oblkno + 26060 ((wr_bp->b_bcount - dma_resid) / 26061 un->un_tgt_blocksize); 26062 nblk = dma_resid / un->un_tgt_blocksize; 26063 26064 if (wr_pktp) { 26065 /* 26066 * Partial DMA transfers after initial transfer 26067 */ 26068 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26069 blkno, nblk); 26070 } else { 26071 /* Initial transfer */ 26072 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26073 un->un_pkt_flags, NULL_FUNC, NULL, 26074 blkno, nblk); 26075 } 26076 } else { 26077 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26078 0, NULL_FUNC, NULL, blkno, nblk); 26079 } 26080 26081 if (rval == 0) { 26082 /* We were given a SCSI packet, continue. */ 26083 break; 26084 } 26085 26086 if (i == 0) { 26087 if (wr_bp->b_flags & B_ERROR) { 26088 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26089 "no resources for dumping; " 26090 "error code: 0x%x, retrying", 26091 geterror(wr_bp)); 26092 } else { 26093 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26094 "no resources for dumping; retrying"); 26095 } 26096 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26097 if (wr_bp->b_flags & B_ERROR) { 26098 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26099 "no resources for dumping; error code: " 26100 "0x%x, retrying\n", geterror(wr_bp)); 26101 } 26102 } else { 26103 if (wr_bp->b_flags & B_ERROR) { 26104 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26105 "no resources for dumping; " 26106 "error code: 0x%x, retries failed, " 26107 "giving up.\n", geterror(wr_bp)); 26108 } else { 26109 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26110 "no resources for dumping; " 26111 "retries failed, giving up.\n"); 26112 } 26113 mutex_enter(SD_MUTEX(un)); 26114 Restore_state(un); 26115 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26116 mutex_exit(SD_MUTEX(un)); 26117 scsi_free_consistent_buf(wr_bp); 26118 } else { 26119 mutex_exit(SD_MUTEX(un)); 26120 } 26121 return (EIO); 26122 } 26123 drv_usecwait(10000); 26124 } 26125 26126 if (un->un_partial_dma_supported == 1) { 26127 /* 26128 * save the resid from PARTIAL_DMA 26129 */ 26130 dma_resid = wr_pktp->pkt_resid; 26131 if (dma_resid != 0) 26132 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26133 wr_pktp->pkt_resid = 0; 26134 } else { 26135 dma_resid = 0; 26136 } 26137 26138 /* SunBug 1222170 */ 26139 wr_pktp->pkt_flags = FLAG_NOINTR; 26140 26141 err = EIO; 26142 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26143 26144 /* 26145 * Scsi_poll returns 0 (success) if the command completes and 26146 * the status block is STATUS_GOOD. We should only check 26147 * errors if this condition is not true. Even then we should 26148 * send our own request sense packet only if we have a check 26149 * condition and auto request sense has not been performed by 26150 * the hba. 26151 */ 26152 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26153 26154 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26155 (wr_pktp->pkt_resid == 0)) { 26156 err = SD_SUCCESS; 26157 break; 26158 } 26159 26160 /* 26161 * Check CMD_DEV_GONE 1st, give up if device is gone. 26162 */ 26163 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26164 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26165 "Error while dumping state...Device is gone\n"); 26166 break; 26167 } 26168 26169 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26170 SD_INFO(SD_LOG_DUMP, un, 26171 "sddump: write failed with CHECK, try # %d\n", i); 26172 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26173 (void) sd_send_polled_RQS(un); 26174 } 26175 26176 continue; 26177 } 26178 26179 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26180 int reset_retval = 0; 26181 26182 SD_INFO(SD_LOG_DUMP, un, 26183 "sddump: write failed with BUSY, try # %d\n", i); 26184 26185 if (un->un_f_lun_reset_enabled == TRUE) { 26186 reset_retval = scsi_reset(SD_ADDRESS(un), 26187 RESET_LUN); 26188 } 26189 if (reset_retval == 0) { 26190 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26191 } 26192 (void) sd_send_polled_RQS(un); 26193 26194 } else { 26195 SD_INFO(SD_LOG_DUMP, un, 26196 "sddump: write failed with 0x%x, try # %d\n", 26197 SD_GET_PKT_STATUS(wr_pktp), i); 26198 mutex_enter(SD_MUTEX(un)); 26199 sd_reset_target(un, wr_pktp); 26200 mutex_exit(SD_MUTEX(un)); 26201 } 26202 26203 /* 26204 * If we are not getting anywhere with lun/target resets, 26205 * let's reset the bus. 26206 */ 26207 if (i == SD_NDUMP_RETRIES/2) { 26208 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26209 (void) sd_send_polled_RQS(un); 26210 } 26211 } 26212 } 26213 26214 scsi_destroy_pkt(wr_pktp); 26215 mutex_enter(SD_MUTEX(un)); 26216 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26217 mutex_exit(SD_MUTEX(un)); 26218 scsi_free_consistent_buf(wr_bp); 26219 } else { 26220 mutex_exit(SD_MUTEX(un)); 26221 } 26222 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26223 return (err); 26224 } 26225 26226 /* 26227 * Function: sd_scsi_poll() 26228 * 26229 * Description: This is a wrapper for the scsi_poll call. 26230 * 26231 * Arguments: sd_lun - The unit structure 26232 * scsi_pkt - The scsi packet being sent to the device. 26233 * 26234 * Return Code: 0 - Command completed successfully with good status 26235 * -1 - Command failed. This could indicate a check condition 26236 * or other status value requiring recovery action. 26237 * 26238 * NOTE: This code is only called off sddump(). 26239 */ 26240 26241 static int 26242 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26243 { 26244 int status; 26245 26246 ASSERT(un != NULL); 26247 ASSERT(!mutex_owned(SD_MUTEX(un))); 26248 ASSERT(pktp != NULL); 26249 26250 status = SD_SUCCESS; 26251 26252 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26253 pktp->pkt_flags |= un->un_tagflags; 26254 pktp->pkt_flags &= ~FLAG_NODISCON; 26255 } 26256 26257 status = sd_ddi_scsi_poll(pktp); 26258 /* 26259 * Scsi_poll returns 0 (success) if the command completes and the 26260 * status block is STATUS_GOOD. We should only check errors if this 26261 * condition is not true. Even then we should send our own request 26262 * sense packet only if we have a check condition and auto 26263 * request sense has not been performed by the hba. 26264 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26265 */ 26266 if ((status != SD_SUCCESS) && 26267 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26268 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26269 (pktp->pkt_reason != CMD_DEV_GONE)) 26270 (void) sd_send_polled_RQS(un); 26271 26272 return (status); 26273 } 26274 26275 /* 26276 * Function: sd_send_polled_RQS() 26277 * 26278 * Description: This sends the request sense command to a device. 26279 * 26280 * Arguments: sd_lun - The unit structure 26281 * 26282 * Return Code: 0 - Command completed successfully with good status 26283 * -1 - Command failed. 26284 * 26285 */ 26286 26287 static int 26288 sd_send_polled_RQS(struct sd_lun *un) 26289 { 26290 int ret_val; 26291 struct scsi_pkt *rqs_pktp; 26292 struct buf *rqs_bp; 26293 26294 ASSERT(un != NULL); 26295 ASSERT(!mutex_owned(SD_MUTEX(un))); 26296 26297 ret_val = SD_SUCCESS; 26298 26299 rqs_pktp = un->un_rqs_pktp; 26300 rqs_bp = un->un_rqs_bp; 26301 26302 mutex_enter(SD_MUTEX(un)); 26303 26304 if (un->un_sense_isbusy) { 26305 ret_val = SD_FAILURE; 26306 mutex_exit(SD_MUTEX(un)); 26307 return (ret_val); 26308 } 26309 26310 /* 26311 * If the request sense buffer (and packet) is not in use, 26312 * let's set the un_sense_isbusy and send our packet 26313 */ 26314 un->un_sense_isbusy = 1; 26315 rqs_pktp->pkt_resid = 0; 26316 rqs_pktp->pkt_reason = 0; 26317 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26318 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26319 26320 mutex_exit(SD_MUTEX(un)); 26321 26322 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26323 " 0x%p\n", rqs_bp->b_un.b_addr); 26324 26325 /* 26326 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26327 * axle - it has a call into us! 26328 */ 26329 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26330 SD_INFO(SD_LOG_COMMON, un, 26331 "sd_send_polled_RQS: RQS failed\n"); 26332 } 26333 26334 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26335 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26336 26337 mutex_enter(SD_MUTEX(un)); 26338 un->un_sense_isbusy = 0; 26339 mutex_exit(SD_MUTEX(un)); 26340 26341 return (ret_val); 26342 } 26343 26344 /* 26345 * Defines needed for localized version of the scsi_poll routine. 26346 */ 26347 #define CSEC 10000 /* usecs */ 26348 #define SEC_TO_CSEC (1000000/CSEC) 26349 26350 /* 26351 * Function: sd_ddi_scsi_poll() 26352 * 26353 * Description: Localized version of the scsi_poll routine. The purpose is to 26354 * send a scsi_pkt to a device as a polled command. This version 26355 * is to ensure more robust handling of transport errors. 26356 * Specifically this routine cures not ready, coming ready 26357 * transition for power up and reset of sonoma's. This can take 26358 * up to 45 seconds for power-on and 20 seconds for reset of a 26359 * sonoma lun. 26360 * 26361 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26362 * 26363 * Return Code: 0 - Command completed successfully with good status 26364 * -1 - Command failed. 26365 * 26366 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can 26367 * be fixed (removing this code), we need to determine how to handle the 26368 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump(). 26369 * 26370 * NOTE: This code is only called off sddump(). 26371 */ 26372 static int 26373 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26374 { 26375 int rval = -1; 26376 int savef; 26377 long savet; 26378 void (*savec)(); 26379 int timeout; 26380 int busy_count; 26381 int poll_delay; 26382 int rc; 26383 uint8_t *sensep; 26384 struct scsi_arq_status *arqstat; 26385 extern int do_polled_io; 26386 26387 ASSERT(pkt->pkt_scbp); 26388 26389 /* 26390 * save old flags.. 26391 */ 26392 savef = pkt->pkt_flags; 26393 savec = pkt->pkt_comp; 26394 savet = pkt->pkt_time; 26395 26396 pkt->pkt_flags |= FLAG_NOINTR; 26397 26398 /* 26399 * XXX there is nothing in the SCSA spec that states that we should not 26400 * do a callback for polled cmds; however, removing this will break sd 26401 * and probably other target drivers 26402 */ 26403 pkt->pkt_comp = NULL; 26404 26405 /* 26406 * we don't like a polled command without timeout. 26407 * 60 seconds seems long enough. 26408 */ 26409 if (pkt->pkt_time == 0) 26410 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26411 26412 /* 26413 * Send polled cmd. 26414 * 26415 * We do some error recovery for various errors. Tran_busy, 26416 * queue full, and non-dispatched commands are retried every 10 msec. 26417 * as they are typically transient failures. Busy status and Not 26418 * Ready are retried every second as this status takes a while to 26419 * change. 26420 */ 26421 timeout = pkt->pkt_time * SEC_TO_CSEC; 26422 26423 for (busy_count = 0; busy_count < timeout; busy_count++) { 26424 /* 26425 * Initialize pkt status variables. 26426 */ 26427 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26428 26429 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26430 if (rc != TRAN_BUSY) { 26431 /* Transport failed - give up. */ 26432 break; 26433 } else { 26434 /* Transport busy - try again. */ 26435 poll_delay = 1 * CSEC; /* 10 msec. */ 26436 } 26437 } else { 26438 /* 26439 * Transport accepted - check pkt status. 26440 */ 26441 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26442 if ((pkt->pkt_reason == CMD_CMPLT) && 26443 (rc == STATUS_CHECK) && 26444 (pkt->pkt_state & STATE_ARQ_DONE)) { 26445 arqstat = 26446 (struct scsi_arq_status *)(pkt->pkt_scbp); 26447 sensep = (uint8_t *)&arqstat->sts_sensedata; 26448 } else { 26449 sensep = NULL; 26450 } 26451 26452 if ((pkt->pkt_reason == CMD_CMPLT) && 26453 (rc == STATUS_GOOD)) { 26454 /* No error - we're done */ 26455 rval = 0; 26456 break; 26457 26458 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26459 /* Lost connection - give up */ 26460 break; 26461 26462 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26463 (pkt->pkt_state == 0)) { 26464 /* Pkt not dispatched - try again. */ 26465 poll_delay = 1 * CSEC; /* 10 msec. */ 26466 26467 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26468 (rc == STATUS_QFULL)) { 26469 /* Queue full - try again. */ 26470 poll_delay = 1 * CSEC; /* 10 msec. */ 26471 26472 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26473 (rc == STATUS_BUSY)) { 26474 /* Busy - try again. */ 26475 poll_delay = 100 * CSEC; /* 1 sec. */ 26476 busy_count += (SEC_TO_CSEC - 1); 26477 26478 } else if ((sensep != NULL) && 26479 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) { 26480 /* 26481 * Unit Attention - try again. 26482 * Pretend it took 1 sec. 26483 * NOTE: 'continue' avoids poll_delay 26484 */ 26485 busy_count += (SEC_TO_CSEC - 1); 26486 continue; 26487 26488 } else if ((sensep != NULL) && 26489 (scsi_sense_key(sensep) == KEY_NOT_READY) && 26490 (scsi_sense_asc(sensep) == 0x04) && 26491 (scsi_sense_ascq(sensep) == 0x01)) { 26492 /* 26493 * Not ready -> ready - try again. 26494 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY 26495 * ...same as STATUS_BUSY 26496 */ 26497 poll_delay = 100 * CSEC; /* 1 sec. */ 26498 busy_count += (SEC_TO_CSEC - 1); 26499 26500 } else { 26501 /* BAD status - give up. */ 26502 break; 26503 } 26504 } 26505 26506 if (((curthread->t_flag & T_INTR_THREAD) == 0) && 26507 !do_polled_io) { 26508 delay(drv_usectohz(poll_delay)); 26509 } else { 26510 /* we busy wait during cpr_dump or interrupt threads */ 26511 drv_usecwait(poll_delay); 26512 } 26513 } 26514 26515 pkt->pkt_flags = savef; 26516 pkt->pkt_comp = savec; 26517 pkt->pkt_time = savet; 26518 26519 /* return on error */ 26520 if (rval) 26521 return (rval); 26522 26523 /* 26524 * This is not a performance critical code path. 26525 * 26526 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync() 26527 * issues associated with looking at DMA memory prior to 26528 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return. 26529 */ 26530 scsi_sync_pkt(pkt); 26531 return (0); 26532 } 26533 26534 26535 26536 /* 26537 * Function: sd_persistent_reservation_in_read_keys 26538 * 26539 * Description: This routine is the driver entry point for handling CD-ROM 26540 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26541 * by sending the SCSI-3 PRIN commands to the device. 26542 * Processes the read keys command response by copying the 26543 * reservation key information into the user provided buffer. 26544 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26545 * 26546 * Arguments: un - Pointer to soft state struct for the target. 26547 * usrp - user provided pointer to multihost Persistent In Read 26548 * Keys structure (mhioc_inkeys_t) 26549 * flag - this argument is a pass through to ddi_copyxxx() 26550 * directly from the mode argument of ioctl(). 26551 * 26552 * Return Code: 0 - Success 26553 * EACCES 26554 * ENOTSUP 26555 * errno return code from sd_send_scsi_cmd() 26556 * 26557 * Context: Can sleep. Does not return until command is completed. 26558 */ 26559 26560 static int 26561 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26562 mhioc_inkeys_t *usrp, int flag) 26563 { 26564 #ifdef _MULTI_DATAMODEL 26565 struct mhioc_key_list32 li32; 26566 #endif 26567 sd_prin_readkeys_t *in; 26568 mhioc_inkeys_t *ptr; 26569 mhioc_key_list_t li; 26570 uchar_t *data_bufp; 26571 int data_len; 26572 int rval = 0; 26573 size_t copysz; 26574 sd_ssc_t *ssc; 26575 26576 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26577 return (EINVAL); 26578 } 26579 bzero(&li, sizeof (mhioc_key_list_t)); 26580 26581 ssc = sd_ssc_init(un); 26582 26583 /* 26584 * Get the listsize from user 26585 */ 26586 #ifdef _MULTI_DATAMODEL 26587 26588 switch (ddi_model_convert_from(flag & FMODELS)) { 26589 case DDI_MODEL_ILP32: 26590 copysz = sizeof (struct mhioc_key_list32); 26591 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26592 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26593 "sd_persistent_reservation_in_read_keys: " 26594 "failed ddi_copyin: mhioc_key_list32_t\n"); 26595 rval = EFAULT; 26596 goto done; 26597 } 26598 li.listsize = li32.listsize; 26599 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26600 break; 26601 26602 case DDI_MODEL_NONE: 26603 copysz = sizeof (mhioc_key_list_t); 26604 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26605 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26606 "sd_persistent_reservation_in_read_keys: " 26607 "failed ddi_copyin: mhioc_key_list_t\n"); 26608 rval = EFAULT; 26609 goto done; 26610 } 26611 break; 26612 } 26613 26614 #else /* ! _MULTI_DATAMODEL */ 26615 copysz = sizeof (mhioc_key_list_t); 26616 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26617 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26618 "sd_persistent_reservation_in_read_keys: " 26619 "failed ddi_copyin: mhioc_key_list_t\n"); 26620 rval = EFAULT; 26621 goto done; 26622 } 26623 #endif 26624 26625 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26626 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26627 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26628 26629 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 26630 data_len, data_bufp); 26631 if (rval != 0) { 26632 if (rval == EIO) 26633 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26634 else 26635 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26636 goto done; 26637 } 26638 in = (sd_prin_readkeys_t *)data_bufp; 26639 ptr->generation = BE_32(in->generation); 26640 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26641 26642 /* 26643 * Return the min(listsize, listlen) keys 26644 */ 26645 #ifdef _MULTI_DATAMODEL 26646 26647 switch (ddi_model_convert_from(flag & FMODELS)) { 26648 case DDI_MODEL_ILP32: 26649 li32.listlen = li.listlen; 26650 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26651 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26652 "sd_persistent_reservation_in_read_keys: " 26653 "failed ddi_copyout: mhioc_key_list32_t\n"); 26654 rval = EFAULT; 26655 goto done; 26656 } 26657 break; 26658 26659 case DDI_MODEL_NONE: 26660 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26661 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26662 "sd_persistent_reservation_in_read_keys: " 26663 "failed ddi_copyout: mhioc_key_list_t\n"); 26664 rval = EFAULT; 26665 goto done; 26666 } 26667 break; 26668 } 26669 26670 #else /* ! _MULTI_DATAMODEL */ 26671 26672 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26673 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26674 "sd_persistent_reservation_in_read_keys: " 26675 "failed ddi_copyout: mhioc_key_list_t\n"); 26676 rval = EFAULT; 26677 goto done; 26678 } 26679 26680 #endif /* _MULTI_DATAMODEL */ 26681 26682 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26683 li.listsize * MHIOC_RESV_KEY_SIZE); 26684 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26685 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26686 "sd_persistent_reservation_in_read_keys: " 26687 "failed ddi_copyout: keylist\n"); 26688 rval = EFAULT; 26689 } 26690 done: 26691 sd_ssc_fini(ssc); 26692 kmem_free(data_bufp, data_len); 26693 return (rval); 26694 } 26695 26696 26697 /* 26698 * Function: sd_persistent_reservation_in_read_resv 26699 * 26700 * Description: This routine is the driver entry point for handling CD-ROM 26701 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26702 * by sending the SCSI-3 PRIN commands to the device. 26703 * Process the read persistent reservations command response by 26704 * copying the reservation information into the user provided 26705 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26706 * 26707 * Arguments: un - Pointer to soft state struct for the target. 26708 * usrp - user provided pointer to multihost Persistent In Read 26709 * Keys structure (mhioc_inkeys_t) 26710 * flag - this argument is a pass through to ddi_copyxxx() 26711 * directly from the mode argument of ioctl(). 26712 * 26713 * Return Code: 0 - Success 26714 * EACCES 26715 * ENOTSUP 26716 * errno return code from sd_send_scsi_cmd() 26717 * 26718 * Context: Can sleep. Does not return until command is completed. 26719 */ 26720 26721 static int 26722 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26723 mhioc_inresvs_t *usrp, int flag) 26724 { 26725 #ifdef _MULTI_DATAMODEL 26726 struct mhioc_resv_desc_list32 resvlist32; 26727 #endif 26728 sd_prin_readresv_t *in; 26729 mhioc_inresvs_t *ptr; 26730 sd_readresv_desc_t *readresv_ptr; 26731 mhioc_resv_desc_list_t resvlist; 26732 mhioc_resv_desc_t resvdesc; 26733 uchar_t *data_bufp = NULL; 26734 int data_len; 26735 int rval = 0; 26736 int i; 26737 size_t copysz; 26738 mhioc_resv_desc_t *bufp; 26739 sd_ssc_t *ssc; 26740 26741 if ((ptr = usrp) == NULL) { 26742 return (EINVAL); 26743 } 26744 26745 ssc = sd_ssc_init(un); 26746 26747 /* 26748 * Get the listsize from user 26749 */ 26750 #ifdef _MULTI_DATAMODEL 26751 switch (ddi_model_convert_from(flag & FMODELS)) { 26752 case DDI_MODEL_ILP32: 26753 copysz = sizeof (struct mhioc_resv_desc_list32); 26754 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26755 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26756 "sd_persistent_reservation_in_read_resv: " 26757 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26758 rval = EFAULT; 26759 goto done; 26760 } 26761 resvlist.listsize = resvlist32.listsize; 26762 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26763 break; 26764 26765 case DDI_MODEL_NONE: 26766 copysz = sizeof (mhioc_resv_desc_list_t); 26767 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26768 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26769 "sd_persistent_reservation_in_read_resv: " 26770 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26771 rval = EFAULT; 26772 goto done; 26773 } 26774 break; 26775 } 26776 #else /* ! _MULTI_DATAMODEL */ 26777 copysz = sizeof (mhioc_resv_desc_list_t); 26778 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26779 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26780 "sd_persistent_reservation_in_read_resv: " 26781 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26782 rval = EFAULT; 26783 goto done; 26784 } 26785 #endif /* ! _MULTI_DATAMODEL */ 26786 26787 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26788 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26789 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26790 26791 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV, 26792 data_len, data_bufp); 26793 if (rval != 0) { 26794 if (rval == EIO) 26795 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26796 else 26797 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26798 goto done; 26799 } 26800 in = (sd_prin_readresv_t *)data_bufp; 26801 ptr->generation = BE_32(in->generation); 26802 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26803 26804 /* 26805 * Return the min(listsize, listlen( keys 26806 */ 26807 #ifdef _MULTI_DATAMODEL 26808 26809 switch (ddi_model_convert_from(flag & FMODELS)) { 26810 case DDI_MODEL_ILP32: 26811 resvlist32.listlen = resvlist.listlen; 26812 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26813 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26814 "sd_persistent_reservation_in_read_resv: " 26815 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26816 rval = EFAULT; 26817 goto done; 26818 } 26819 break; 26820 26821 case DDI_MODEL_NONE: 26822 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26823 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26824 "sd_persistent_reservation_in_read_resv: " 26825 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26826 rval = EFAULT; 26827 goto done; 26828 } 26829 break; 26830 } 26831 26832 #else /* ! _MULTI_DATAMODEL */ 26833 26834 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26835 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26836 "sd_persistent_reservation_in_read_resv: " 26837 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26838 rval = EFAULT; 26839 goto done; 26840 } 26841 26842 #endif /* ! _MULTI_DATAMODEL */ 26843 26844 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26845 bufp = resvlist.list; 26846 copysz = sizeof (mhioc_resv_desc_t); 26847 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26848 i++, readresv_ptr++, bufp++) { 26849 26850 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26851 MHIOC_RESV_KEY_SIZE); 26852 resvdesc.type = readresv_ptr->type; 26853 resvdesc.scope = readresv_ptr->scope; 26854 resvdesc.scope_specific_addr = 26855 BE_32(readresv_ptr->scope_specific_addr); 26856 26857 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26858 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26859 "sd_persistent_reservation_in_read_resv: " 26860 "failed ddi_copyout: resvlist\n"); 26861 rval = EFAULT; 26862 goto done; 26863 } 26864 } 26865 done: 26866 sd_ssc_fini(ssc); 26867 /* only if data_bufp is allocated, we need to free it */ 26868 if (data_bufp) { 26869 kmem_free(data_bufp, data_len); 26870 } 26871 return (rval); 26872 } 26873 26874 26875 /* 26876 * Function: sr_change_blkmode() 26877 * 26878 * Description: This routine is the driver entry point for handling CD-ROM 26879 * block mode ioctl requests. Support for returning and changing 26880 * the current block size in use by the device is implemented. The 26881 * LBA size is changed via a MODE SELECT Block Descriptor. 26882 * 26883 * This routine issues a mode sense with an allocation length of 26884 * 12 bytes for the mode page header and a single block descriptor. 26885 * 26886 * Arguments: dev - the device 'dev_t' 26887 * cmd - the request type; one of CDROMGBLKMODE (get) or 26888 * CDROMSBLKMODE (set) 26889 * data - current block size or requested block size 26890 * flag - this argument is a pass through to ddi_copyxxx() directly 26891 * from the mode argument of ioctl(). 26892 * 26893 * Return Code: the code returned by sd_send_scsi_cmd() 26894 * EINVAL if invalid arguments are provided 26895 * EFAULT if ddi_copyxxx() fails 26896 * ENXIO if fail ddi_get_soft_state 26897 * EIO if invalid mode sense block descriptor length 26898 * 26899 */ 26900 26901 static int 26902 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26903 { 26904 struct sd_lun *un = NULL; 26905 struct mode_header *sense_mhp, *select_mhp; 26906 struct block_descriptor *sense_desc, *select_desc; 26907 int current_bsize; 26908 int rval = EINVAL; 26909 uchar_t *sense = NULL; 26910 uchar_t *select = NULL; 26911 sd_ssc_t *ssc; 26912 26913 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26914 26915 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26916 return (ENXIO); 26917 } 26918 26919 /* 26920 * The block length is changed via the Mode Select block descriptor, the 26921 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26922 * required as part of this routine. Therefore the mode sense allocation 26923 * length is specified to be the length of a mode page header and a 26924 * block descriptor. 26925 */ 26926 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26927 26928 ssc = sd_ssc_init(un); 26929 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 26930 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 26931 sd_ssc_fini(ssc); 26932 if (rval != 0) { 26933 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26934 "sr_change_blkmode: Mode Sense Failed\n"); 26935 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26936 return (rval); 26937 } 26938 26939 /* Check the block descriptor len to handle only 1 block descriptor */ 26940 sense_mhp = (struct mode_header *)sense; 26941 if ((sense_mhp->bdesc_length == 0) || 26942 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26943 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26944 "sr_change_blkmode: Mode Sense returned invalid block" 26945 " descriptor length\n"); 26946 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26947 return (EIO); 26948 } 26949 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26950 current_bsize = ((sense_desc->blksize_hi << 16) | 26951 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26952 26953 /* Process command */ 26954 switch (cmd) { 26955 case CDROMGBLKMODE: 26956 /* Return the block size obtained during the mode sense */ 26957 if (ddi_copyout(¤t_bsize, (void *)data, 26958 sizeof (int), flag) != 0) 26959 rval = EFAULT; 26960 break; 26961 case CDROMSBLKMODE: 26962 /* Validate the requested block size */ 26963 switch (data) { 26964 case CDROM_BLK_512: 26965 case CDROM_BLK_1024: 26966 case CDROM_BLK_2048: 26967 case CDROM_BLK_2056: 26968 case CDROM_BLK_2336: 26969 case CDROM_BLK_2340: 26970 case CDROM_BLK_2352: 26971 case CDROM_BLK_2368: 26972 case CDROM_BLK_2448: 26973 case CDROM_BLK_2646: 26974 case CDROM_BLK_2647: 26975 break; 26976 default: 26977 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26978 "sr_change_blkmode: " 26979 "Block Size '%ld' Not Supported\n", data); 26980 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26981 return (EINVAL); 26982 } 26983 26984 /* 26985 * The current block size matches the requested block size so 26986 * there is no need to send the mode select to change the size 26987 */ 26988 if (current_bsize == data) { 26989 break; 26990 } 26991 26992 /* Build the select data for the requested block size */ 26993 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26994 select_mhp = (struct mode_header *)select; 26995 select_desc = 26996 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26997 /* 26998 * The LBA size is changed via the block descriptor, so the 26999 * descriptor is built according to the user data 27000 */ 27001 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27002 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27003 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27004 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27005 27006 /* Send the mode select for the requested block size */ 27007 ssc = sd_ssc_init(un); 27008 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 27009 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27010 SD_PATH_STANDARD); 27011 sd_ssc_fini(ssc); 27012 if (rval != 0) { 27013 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27014 "sr_change_blkmode: Mode Select Failed\n"); 27015 /* 27016 * The mode select failed for the requested block size, 27017 * so reset the data for the original block size and 27018 * send it to the target. The error is indicated by the 27019 * return value for the failed mode select. 27020 */ 27021 select_desc->blksize_hi = sense_desc->blksize_hi; 27022 select_desc->blksize_mid = sense_desc->blksize_mid; 27023 select_desc->blksize_lo = sense_desc->blksize_lo; 27024 ssc = sd_ssc_init(un); 27025 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 27026 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27027 SD_PATH_STANDARD); 27028 sd_ssc_fini(ssc); 27029 } else { 27030 ASSERT(!mutex_owned(SD_MUTEX(un))); 27031 mutex_enter(SD_MUTEX(un)); 27032 sd_update_block_info(un, (uint32_t)data, 0); 27033 mutex_exit(SD_MUTEX(un)); 27034 } 27035 break; 27036 default: 27037 /* should not reach here, but check anyway */ 27038 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27039 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27040 rval = EINVAL; 27041 break; 27042 } 27043 27044 if (select) { 27045 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27046 } 27047 if (sense) { 27048 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27049 } 27050 return (rval); 27051 } 27052 27053 27054 /* 27055 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27056 * implement driver support for getting and setting the CD speed. The command 27057 * set used will be based on the device type. If the device has not been 27058 * identified as MMC the Toshiba vendor specific mode page will be used. If 27059 * the device is MMC but does not support the Real Time Streaming feature 27060 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27061 * be used to read the speed. 27062 */ 27063 27064 /* 27065 * Function: sr_change_speed() 27066 * 27067 * Description: This routine is the driver entry point for handling CD-ROM 27068 * drive speed ioctl requests for devices supporting the Toshiba 27069 * vendor specific drive speed mode page. Support for returning 27070 * and changing the current drive speed in use by the device is 27071 * implemented. 27072 * 27073 * Arguments: dev - the device 'dev_t' 27074 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27075 * CDROMSDRVSPEED (set) 27076 * data - current drive speed or requested drive speed 27077 * flag - this argument is a pass through to ddi_copyxxx() directly 27078 * from the mode argument of ioctl(). 27079 * 27080 * Return Code: the code returned by sd_send_scsi_cmd() 27081 * EINVAL if invalid arguments are provided 27082 * EFAULT if ddi_copyxxx() fails 27083 * ENXIO if fail ddi_get_soft_state 27084 * EIO if invalid mode sense block descriptor length 27085 */ 27086 27087 static int 27088 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27089 { 27090 struct sd_lun *un = NULL; 27091 struct mode_header *sense_mhp, *select_mhp; 27092 struct mode_speed *sense_page, *select_page; 27093 int current_speed; 27094 int rval = EINVAL; 27095 int bd_len; 27096 uchar_t *sense = NULL; 27097 uchar_t *select = NULL; 27098 sd_ssc_t *ssc; 27099 27100 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27101 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27102 return (ENXIO); 27103 } 27104 27105 /* 27106 * Note: The drive speed is being modified here according to a Toshiba 27107 * vendor specific mode page (0x31). 27108 */ 27109 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27110 27111 ssc = sd_ssc_init(un); 27112 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 27113 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27114 SD_PATH_STANDARD); 27115 sd_ssc_fini(ssc); 27116 if (rval != 0) { 27117 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27118 "sr_change_speed: Mode Sense Failed\n"); 27119 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27120 return (rval); 27121 } 27122 sense_mhp = (struct mode_header *)sense; 27123 27124 /* Check the block descriptor len to handle only 1 block descriptor */ 27125 bd_len = sense_mhp->bdesc_length; 27126 if (bd_len > MODE_BLK_DESC_LENGTH) { 27127 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27128 "sr_change_speed: Mode Sense returned invalid block " 27129 "descriptor length\n"); 27130 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27131 return (EIO); 27132 } 27133 27134 sense_page = (struct mode_speed *) 27135 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27136 current_speed = sense_page->speed; 27137 27138 /* Process command */ 27139 switch (cmd) { 27140 case CDROMGDRVSPEED: 27141 /* Return the drive speed obtained during the mode sense */ 27142 if (current_speed == 0x2) { 27143 current_speed = CDROM_TWELVE_SPEED; 27144 } 27145 if (ddi_copyout(¤t_speed, (void *)data, 27146 sizeof (int), flag) != 0) { 27147 rval = EFAULT; 27148 } 27149 break; 27150 case CDROMSDRVSPEED: 27151 /* Validate the requested drive speed */ 27152 switch ((uchar_t)data) { 27153 case CDROM_TWELVE_SPEED: 27154 data = 0x2; 27155 /*FALLTHROUGH*/ 27156 case CDROM_NORMAL_SPEED: 27157 case CDROM_DOUBLE_SPEED: 27158 case CDROM_QUAD_SPEED: 27159 case CDROM_MAXIMUM_SPEED: 27160 break; 27161 default: 27162 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27163 "sr_change_speed: " 27164 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27165 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27166 return (EINVAL); 27167 } 27168 27169 /* 27170 * The current drive speed matches the requested drive speed so 27171 * there is no need to send the mode select to change the speed 27172 */ 27173 if (current_speed == data) { 27174 break; 27175 } 27176 27177 /* Build the select data for the requested drive speed */ 27178 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27179 select_mhp = (struct mode_header *)select; 27180 select_mhp->bdesc_length = 0; 27181 select_page = 27182 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27183 select_page = 27184 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27185 select_page->mode_page.code = CDROM_MODE_SPEED; 27186 select_page->mode_page.length = 2; 27187 select_page->speed = (uchar_t)data; 27188 27189 /* Send the mode select for the requested block size */ 27190 ssc = sd_ssc_init(un); 27191 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27192 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27193 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27194 sd_ssc_fini(ssc); 27195 if (rval != 0) { 27196 /* 27197 * The mode select failed for the requested drive speed, 27198 * so reset the data for the original drive speed and 27199 * send it to the target. The error is indicated by the 27200 * return value for the failed mode select. 27201 */ 27202 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27203 "sr_drive_speed: Mode Select Failed\n"); 27204 select_page->speed = sense_page->speed; 27205 ssc = sd_ssc_init(un); 27206 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27207 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27208 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27209 sd_ssc_fini(ssc); 27210 } 27211 break; 27212 default: 27213 /* should not reach here, but check anyway */ 27214 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27215 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27216 rval = EINVAL; 27217 break; 27218 } 27219 27220 if (select) { 27221 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27222 } 27223 if (sense) { 27224 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27225 } 27226 27227 return (rval); 27228 } 27229 27230 27231 /* 27232 * Function: sr_atapi_change_speed() 27233 * 27234 * Description: This routine is the driver entry point for handling CD-ROM 27235 * drive speed ioctl requests for MMC devices that do not support 27236 * the Real Time Streaming feature (0x107). 27237 * 27238 * Note: This routine will use the SET SPEED command which may not 27239 * be supported by all devices. 27240 * 27241 * Arguments: dev- the device 'dev_t' 27242 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27243 * CDROMSDRVSPEED (set) 27244 * data- current drive speed or requested drive speed 27245 * flag- this argument is a pass through to ddi_copyxxx() directly 27246 * from the mode argument of ioctl(). 27247 * 27248 * Return Code: the code returned by sd_send_scsi_cmd() 27249 * EINVAL if invalid arguments are provided 27250 * EFAULT if ddi_copyxxx() fails 27251 * ENXIO if fail ddi_get_soft_state 27252 * EIO if invalid mode sense block descriptor length 27253 */ 27254 27255 static int 27256 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27257 { 27258 struct sd_lun *un; 27259 struct uscsi_cmd *com = NULL; 27260 struct mode_header_grp2 *sense_mhp; 27261 uchar_t *sense_page; 27262 uchar_t *sense = NULL; 27263 char cdb[CDB_GROUP5]; 27264 int bd_len; 27265 int current_speed = 0; 27266 int max_speed = 0; 27267 int rval; 27268 sd_ssc_t *ssc; 27269 27270 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27271 27272 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27273 return (ENXIO); 27274 } 27275 27276 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27277 27278 ssc = sd_ssc_init(un); 27279 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 27280 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27281 SD_PATH_STANDARD); 27282 sd_ssc_fini(ssc); 27283 if (rval != 0) { 27284 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27285 "sr_atapi_change_speed: Mode Sense Failed\n"); 27286 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27287 return (rval); 27288 } 27289 27290 /* Check the block descriptor len to handle only 1 block descriptor */ 27291 sense_mhp = (struct mode_header_grp2 *)sense; 27292 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27293 if (bd_len > MODE_BLK_DESC_LENGTH) { 27294 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27295 "sr_atapi_change_speed: Mode Sense returned invalid " 27296 "block descriptor length\n"); 27297 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27298 return (EIO); 27299 } 27300 27301 /* Calculate the current and maximum drive speeds */ 27302 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27303 current_speed = (sense_page[14] << 8) | sense_page[15]; 27304 max_speed = (sense_page[8] << 8) | sense_page[9]; 27305 27306 /* Process the command */ 27307 switch (cmd) { 27308 case CDROMGDRVSPEED: 27309 current_speed /= SD_SPEED_1X; 27310 if (ddi_copyout(¤t_speed, (void *)data, 27311 sizeof (int), flag) != 0) 27312 rval = EFAULT; 27313 break; 27314 case CDROMSDRVSPEED: 27315 /* Convert the speed code to KB/sec */ 27316 switch ((uchar_t)data) { 27317 case CDROM_NORMAL_SPEED: 27318 current_speed = SD_SPEED_1X; 27319 break; 27320 case CDROM_DOUBLE_SPEED: 27321 current_speed = 2 * SD_SPEED_1X; 27322 break; 27323 case CDROM_QUAD_SPEED: 27324 current_speed = 4 * SD_SPEED_1X; 27325 break; 27326 case CDROM_TWELVE_SPEED: 27327 current_speed = 12 * SD_SPEED_1X; 27328 break; 27329 case CDROM_MAXIMUM_SPEED: 27330 current_speed = 0xffff; 27331 break; 27332 default: 27333 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27334 "sr_atapi_change_speed: invalid drive speed %d\n", 27335 (uchar_t)data); 27336 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27337 return (EINVAL); 27338 } 27339 27340 /* Check the request against the drive's max speed. */ 27341 if (current_speed != 0xffff) { 27342 if (current_speed > max_speed) { 27343 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27344 return (EINVAL); 27345 } 27346 } 27347 27348 /* 27349 * Build and send the SET SPEED command 27350 * 27351 * Note: The SET SPEED (0xBB) command used in this routine is 27352 * obsolete per the SCSI MMC spec but still supported in the 27353 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27354 * therefore the command is still implemented in this routine. 27355 */ 27356 bzero(cdb, sizeof (cdb)); 27357 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27358 cdb[2] = (uchar_t)(current_speed >> 8); 27359 cdb[3] = (uchar_t)current_speed; 27360 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27361 com->uscsi_cdb = (caddr_t)cdb; 27362 com->uscsi_cdblen = CDB_GROUP5; 27363 com->uscsi_bufaddr = NULL; 27364 com->uscsi_buflen = 0; 27365 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27366 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 27367 break; 27368 default: 27369 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27370 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27371 rval = EINVAL; 27372 } 27373 27374 if (sense) { 27375 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27376 } 27377 if (com) { 27378 kmem_free(com, sizeof (*com)); 27379 } 27380 return (rval); 27381 } 27382 27383 27384 /* 27385 * Function: sr_pause_resume() 27386 * 27387 * Description: This routine is the driver entry point for handling CD-ROM 27388 * pause/resume ioctl requests. This only affects the audio play 27389 * operation. 27390 * 27391 * Arguments: dev - the device 'dev_t' 27392 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27393 * for setting the resume bit of the cdb. 27394 * 27395 * Return Code: the code returned by sd_send_scsi_cmd() 27396 * EINVAL if invalid mode specified 27397 * 27398 */ 27399 27400 static int 27401 sr_pause_resume(dev_t dev, int cmd) 27402 { 27403 struct sd_lun *un; 27404 struct uscsi_cmd *com; 27405 char cdb[CDB_GROUP1]; 27406 int rval; 27407 27408 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27409 return (ENXIO); 27410 } 27411 27412 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27413 bzero(cdb, CDB_GROUP1); 27414 cdb[0] = SCMD_PAUSE_RESUME; 27415 switch (cmd) { 27416 case CDROMRESUME: 27417 cdb[8] = 1; 27418 break; 27419 case CDROMPAUSE: 27420 cdb[8] = 0; 27421 break; 27422 default: 27423 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27424 " Command '%x' Not Supported\n", cmd); 27425 rval = EINVAL; 27426 goto done; 27427 } 27428 27429 com->uscsi_cdb = cdb; 27430 com->uscsi_cdblen = CDB_GROUP1; 27431 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27432 27433 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27434 SD_PATH_STANDARD); 27435 27436 done: 27437 kmem_free(com, sizeof (*com)); 27438 return (rval); 27439 } 27440 27441 27442 /* 27443 * Function: sr_play_msf() 27444 * 27445 * Description: This routine is the driver entry point for handling CD-ROM 27446 * ioctl requests to output the audio signals at the specified 27447 * starting address and continue the audio play until the specified 27448 * ending address (CDROMPLAYMSF) The address is in Minute Second 27449 * Frame (MSF) format. 27450 * 27451 * Arguments: dev - the device 'dev_t' 27452 * data - pointer to user provided audio msf structure, 27453 * specifying start/end addresses. 27454 * flag - this argument is a pass through to ddi_copyxxx() 27455 * directly from the mode argument of ioctl(). 27456 * 27457 * Return Code: the code returned by sd_send_scsi_cmd() 27458 * EFAULT if ddi_copyxxx() fails 27459 * ENXIO if fail ddi_get_soft_state 27460 * EINVAL if data pointer is NULL 27461 */ 27462 27463 static int 27464 sr_play_msf(dev_t dev, caddr_t data, int flag) 27465 { 27466 struct sd_lun *un; 27467 struct uscsi_cmd *com; 27468 struct cdrom_msf msf_struct; 27469 struct cdrom_msf *msf = &msf_struct; 27470 char cdb[CDB_GROUP1]; 27471 int rval; 27472 27473 if (data == NULL) { 27474 return (EINVAL); 27475 } 27476 27477 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27478 return (ENXIO); 27479 } 27480 27481 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27482 return (EFAULT); 27483 } 27484 27485 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27486 bzero(cdb, CDB_GROUP1); 27487 cdb[0] = SCMD_PLAYAUDIO_MSF; 27488 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27489 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27490 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27491 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27492 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27493 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27494 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27495 } else { 27496 cdb[3] = msf->cdmsf_min0; 27497 cdb[4] = msf->cdmsf_sec0; 27498 cdb[5] = msf->cdmsf_frame0; 27499 cdb[6] = msf->cdmsf_min1; 27500 cdb[7] = msf->cdmsf_sec1; 27501 cdb[8] = msf->cdmsf_frame1; 27502 } 27503 com->uscsi_cdb = cdb; 27504 com->uscsi_cdblen = CDB_GROUP1; 27505 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27506 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27507 SD_PATH_STANDARD); 27508 kmem_free(com, sizeof (*com)); 27509 return (rval); 27510 } 27511 27512 27513 /* 27514 * Function: sr_play_trkind() 27515 * 27516 * Description: This routine is the driver entry point for handling CD-ROM 27517 * ioctl requests to output the audio signals at the specified 27518 * starting address and continue the audio play until the specified 27519 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27520 * format. 27521 * 27522 * Arguments: dev - the device 'dev_t' 27523 * data - pointer to user provided audio track/index structure, 27524 * specifying start/end addresses. 27525 * flag - this argument is a pass through to ddi_copyxxx() 27526 * directly from the mode argument of ioctl(). 27527 * 27528 * Return Code: the code returned by sd_send_scsi_cmd() 27529 * EFAULT if ddi_copyxxx() fails 27530 * ENXIO if fail ddi_get_soft_state 27531 * EINVAL if data pointer is NULL 27532 */ 27533 27534 static int 27535 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27536 { 27537 struct cdrom_ti ti_struct; 27538 struct cdrom_ti *ti = &ti_struct; 27539 struct uscsi_cmd *com = NULL; 27540 char cdb[CDB_GROUP1]; 27541 int rval; 27542 27543 if (data == NULL) { 27544 return (EINVAL); 27545 } 27546 27547 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27548 return (EFAULT); 27549 } 27550 27551 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27552 bzero(cdb, CDB_GROUP1); 27553 cdb[0] = SCMD_PLAYAUDIO_TI; 27554 cdb[4] = ti->cdti_trk0; 27555 cdb[5] = ti->cdti_ind0; 27556 cdb[7] = ti->cdti_trk1; 27557 cdb[8] = ti->cdti_ind1; 27558 com->uscsi_cdb = cdb; 27559 com->uscsi_cdblen = CDB_GROUP1; 27560 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27561 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27562 SD_PATH_STANDARD); 27563 kmem_free(com, sizeof (*com)); 27564 return (rval); 27565 } 27566 27567 27568 /* 27569 * Function: sr_read_all_subcodes() 27570 * 27571 * Description: This routine is the driver entry point for handling CD-ROM 27572 * ioctl requests to return raw subcode data while the target is 27573 * playing audio (CDROMSUBCODE). 27574 * 27575 * Arguments: dev - the device 'dev_t' 27576 * data - pointer to user provided cdrom subcode structure, 27577 * specifying the transfer length and address. 27578 * flag - this argument is a pass through to ddi_copyxxx() 27579 * directly from the mode argument of ioctl(). 27580 * 27581 * Return Code: the code returned by sd_send_scsi_cmd() 27582 * EFAULT if ddi_copyxxx() fails 27583 * ENXIO if fail ddi_get_soft_state 27584 * EINVAL if data pointer is NULL 27585 */ 27586 27587 static int 27588 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27589 { 27590 struct sd_lun *un = NULL; 27591 struct uscsi_cmd *com = NULL; 27592 struct cdrom_subcode *subcode = NULL; 27593 int rval; 27594 size_t buflen; 27595 char cdb[CDB_GROUP5]; 27596 27597 #ifdef _MULTI_DATAMODEL 27598 /* To support ILP32 applications in an LP64 world */ 27599 struct cdrom_subcode32 cdrom_subcode32; 27600 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27601 #endif 27602 if (data == NULL) { 27603 return (EINVAL); 27604 } 27605 27606 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27607 return (ENXIO); 27608 } 27609 27610 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27611 27612 #ifdef _MULTI_DATAMODEL 27613 switch (ddi_model_convert_from(flag & FMODELS)) { 27614 case DDI_MODEL_ILP32: 27615 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27616 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27617 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27618 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27619 return (EFAULT); 27620 } 27621 /* Convert the ILP32 uscsi data from the application to LP64 */ 27622 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27623 break; 27624 case DDI_MODEL_NONE: 27625 if (ddi_copyin(data, subcode, 27626 sizeof (struct cdrom_subcode), flag)) { 27627 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27628 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27629 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27630 return (EFAULT); 27631 } 27632 break; 27633 } 27634 #else /* ! _MULTI_DATAMODEL */ 27635 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27636 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27637 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27638 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27639 return (EFAULT); 27640 } 27641 #endif /* _MULTI_DATAMODEL */ 27642 27643 /* 27644 * Since MMC-2 expects max 3 bytes for length, check if the 27645 * length input is greater than 3 bytes 27646 */ 27647 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27648 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27649 "sr_read_all_subcodes: " 27650 "cdrom transfer length too large: %d (limit %d)\n", 27651 subcode->cdsc_length, 0xFFFFFF); 27652 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27653 return (EINVAL); 27654 } 27655 27656 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27657 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27658 bzero(cdb, CDB_GROUP5); 27659 27660 if (un->un_f_mmc_cap == TRUE) { 27661 cdb[0] = (char)SCMD_READ_CD; 27662 cdb[2] = (char)0xff; 27663 cdb[3] = (char)0xff; 27664 cdb[4] = (char)0xff; 27665 cdb[5] = (char)0xff; 27666 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27667 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27668 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27669 cdb[10] = 1; 27670 } else { 27671 /* 27672 * Note: A vendor specific command (0xDF) is being used her to 27673 * request a read of all subcodes. 27674 */ 27675 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27676 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27677 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27678 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27679 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27680 } 27681 com->uscsi_cdb = cdb; 27682 com->uscsi_cdblen = CDB_GROUP5; 27683 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27684 com->uscsi_buflen = buflen; 27685 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27686 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27687 SD_PATH_STANDARD); 27688 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27689 kmem_free(com, sizeof (*com)); 27690 return (rval); 27691 } 27692 27693 27694 /* 27695 * Function: sr_read_subchannel() 27696 * 27697 * Description: This routine is the driver entry point for handling CD-ROM 27698 * ioctl requests to return the Q sub-channel data of the CD 27699 * current position block. (CDROMSUBCHNL) The data includes the 27700 * track number, index number, absolute CD-ROM address (LBA or MSF 27701 * format per the user) , track relative CD-ROM address (LBA or MSF 27702 * format per the user), control data and audio status. 27703 * 27704 * Arguments: dev - the device 'dev_t' 27705 * data - pointer to user provided cdrom sub-channel structure 27706 * flag - this argument is a pass through to ddi_copyxxx() 27707 * directly from the mode argument of ioctl(). 27708 * 27709 * Return Code: the code returned by sd_send_scsi_cmd() 27710 * EFAULT if ddi_copyxxx() fails 27711 * ENXIO if fail ddi_get_soft_state 27712 * EINVAL if data pointer is NULL 27713 */ 27714 27715 static int 27716 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27717 { 27718 struct sd_lun *un; 27719 struct uscsi_cmd *com; 27720 struct cdrom_subchnl subchanel; 27721 struct cdrom_subchnl *subchnl = &subchanel; 27722 char cdb[CDB_GROUP1]; 27723 caddr_t buffer; 27724 int rval; 27725 27726 if (data == NULL) { 27727 return (EINVAL); 27728 } 27729 27730 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27731 (un->un_state == SD_STATE_OFFLINE)) { 27732 return (ENXIO); 27733 } 27734 27735 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27736 return (EFAULT); 27737 } 27738 27739 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27740 bzero(cdb, CDB_GROUP1); 27741 cdb[0] = SCMD_READ_SUBCHANNEL; 27742 /* Set the MSF bit based on the user requested address format */ 27743 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27744 /* 27745 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27746 * returned 27747 */ 27748 cdb[2] = 0x40; 27749 /* 27750 * Set byte 3 to specify the return data format. A value of 0x01 27751 * indicates that the CD-ROM current position should be returned. 27752 */ 27753 cdb[3] = 0x01; 27754 cdb[8] = 0x10; 27755 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27756 com->uscsi_cdb = cdb; 27757 com->uscsi_cdblen = CDB_GROUP1; 27758 com->uscsi_bufaddr = buffer; 27759 com->uscsi_buflen = 16; 27760 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27761 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27762 SD_PATH_STANDARD); 27763 if (rval != 0) { 27764 kmem_free(buffer, 16); 27765 kmem_free(com, sizeof (*com)); 27766 return (rval); 27767 } 27768 27769 /* Process the returned Q sub-channel data */ 27770 subchnl->cdsc_audiostatus = buffer[1]; 27771 subchnl->cdsc_adr = (buffer[5] & 0xF0); 27772 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27773 subchnl->cdsc_trk = buffer[6]; 27774 subchnl->cdsc_ind = buffer[7]; 27775 if (subchnl->cdsc_format & CDROM_LBA) { 27776 subchnl->cdsc_absaddr.lba = 27777 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27778 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27779 subchnl->cdsc_reladdr.lba = 27780 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27781 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27782 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27783 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27784 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27785 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27786 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27787 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27788 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27789 } else { 27790 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27791 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27792 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27793 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27794 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27795 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27796 } 27797 kmem_free(buffer, 16); 27798 kmem_free(com, sizeof (*com)); 27799 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27800 != 0) { 27801 return (EFAULT); 27802 } 27803 return (rval); 27804 } 27805 27806 27807 /* 27808 * Function: sr_read_tocentry() 27809 * 27810 * Description: This routine is the driver entry point for handling CD-ROM 27811 * ioctl requests to read from the Table of Contents (TOC) 27812 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27813 * fields, the starting address (LBA or MSF format per the user) 27814 * and the data mode if the user specified track is a data track. 27815 * 27816 * Note: The READ HEADER (0x44) command used in this routine is 27817 * obsolete per the SCSI MMC spec but still supported in the 27818 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27819 * therefore the command is still implemented in this routine. 27820 * 27821 * Arguments: dev - the device 'dev_t' 27822 * data - pointer to user provided toc entry structure, 27823 * specifying the track # and the address format 27824 * (LBA or MSF). 27825 * flag - this argument is a pass through to ddi_copyxxx() 27826 * directly from the mode argument of ioctl(). 27827 * 27828 * Return Code: the code returned by sd_send_scsi_cmd() 27829 * EFAULT if ddi_copyxxx() fails 27830 * ENXIO if fail ddi_get_soft_state 27831 * EINVAL if data pointer is NULL 27832 */ 27833 27834 static int 27835 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27836 { 27837 struct sd_lun *un = NULL; 27838 struct uscsi_cmd *com; 27839 struct cdrom_tocentry toc_entry; 27840 struct cdrom_tocentry *entry = &toc_entry; 27841 caddr_t buffer; 27842 int rval; 27843 char cdb[CDB_GROUP1]; 27844 27845 if (data == NULL) { 27846 return (EINVAL); 27847 } 27848 27849 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27850 (un->un_state == SD_STATE_OFFLINE)) { 27851 return (ENXIO); 27852 } 27853 27854 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27855 return (EFAULT); 27856 } 27857 27858 /* Validate the requested track and address format */ 27859 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27860 return (EINVAL); 27861 } 27862 27863 if (entry->cdte_track == 0) { 27864 return (EINVAL); 27865 } 27866 27867 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27868 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27869 bzero(cdb, CDB_GROUP1); 27870 27871 cdb[0] = SCMD_READ_TOC; 27872 /* Set the MSF bit based on the user requested address format */ 27873 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27874 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27875 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27876 } else { 27877 cdb[6] = entry->cdte_track; 27878 } 27879 27880 /* 27881 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27882 * (4 byte TOC response header + 8 byte track descriptor) 27883 */ 27884 cdb[8] = 12; 27885 com->uscsi_cdb = cdb; 27886 com->uscsi_cdblen = CDB_GROUP1; 27887 com->uscsi_bufaddr = buffer; 27888 com->uscsi_buflen = 0x0C; 27889 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27890 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27891 SD_PATH_STANDARD); 27892 if (rval != 0) { 27893 kmem_free(buffer, 12); 27894 kmem_free(com, sizeof (*com)); 27895 return (rval); 27896 } 27897 27898 /* Process the toc entry */ 27899 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27900 entry->cdte_ctrl = (buffer[5] & 0x0F); 27901 if (entry->cdte_format & CDROM_LBA) { 27902 entry->cdte_addr.lba = 27903 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27904 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27905 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27906 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27907 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27908 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27909 /* 27910 * Send a READ TOC command using the LBA address format to get 27911 * the LBA for the track requested so it can be used in the 27912 * READ HEADER request 27913 * 27914 * Note: The MSF bit of the READ HEADER command specifies the 27915 * output format. The block address specified in that command 27916 * must be in LBA format. 27917 */ 27918 cdb[1] = 0; 27919 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27920 SD_PATH_STANDARD); 27921 if (rval != 0) { 27922 kmem_free(buffer, 12); 27923 kmem_free(com, sizeof (*com)); 27924 return (rval); 27925 } 27926 } else { 27927 entry->cdte_addr.msf.minute = buffer[9]; 27928 entry->cdte_addr.msf.second = buffer[10]; 27929 entry->cdte_addr.msf.frame = buffer[11]; 27930 /* 27931 * Send a READ TOC command using the LBA address format to get 27932 * the LBA for the track requested so it can be used in the 27933 * READ HEADER request 27934 * 27935 * Note: The MSF bit of the READ HEADER command specifies the 27936 * output format. The block address specified in that command 27937 * must be in LBA format. 27938 */ 27939 cdb[1] = 0; 27940 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27941 SD_PATH_STANDARD); 27942 if (rval != 0) { 27943 kmem_free(buffer, 12); 27944 kmem_free(com, sizeof (*com)); 27945 return (rval); 27946 } 27947 } 27948 27949 /* 27950 * Build and send the READ HEADER command to determine the data mode of 27951 * the user specified track. 27952 */ 27953 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27954 (entry->cdte_track != CDROM_LEADOUT)) { 27955 bzero(cdb, CDB_GROUP1); 27956 cdb[0] = SCMD_READ_HEADER; 27957 cdb[2] = buffer[8]; 27958 cdb[3] = buffer[9]; 27959 cdb[4] = buffer[10]; 27960 cdb[5] = buffer[11]; 27961 cdb[8] = 0x08; 27962 com->uscsi_buflen = 0x08; 27963 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27964 SD_PATH_STANDARD); 27965 if (rval == 0) { 27966 entry->cdte_datamode = buffer[0]; 27967 } else { 27968 /* 27969 * READ HEADER command failed, since this is 27970 * obsoleted in one spec, its better to return 27971 * -1 for an invlid track so that we can still 27972 * receive the rest of the TOC data. 27973 */ 27974 entry->cdte_datamode = (uchar_t)-1; 27975 } 27976 } else { 27977 entry->cdte_datamode = (uchar_t)-1; 27978 } 27979 27980 kmem_free(buffer, 12); 27981 kmem_free(com, sizeof (*com)); 27982 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27983 return (EFAULT); 27984 27985 return (rval); 27986 } 27987 27988 27989 /* 27990 * Function: sr_read_tochdr() 27991 * 27992 * Description: This routine is the driver entry point for handling CD-ROM 27993 * ioctl requests to read the Table of Contents (TOC) header 27994 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27995 * and ending track numbers 27996 * 27997 * Arguments: dev - the device 'dev_t' 27998 * data - pointer to user provided toc header structure, 27999 * specifying the starting and ending track numbers. 28000 * flag - this argument is a pass through to ddi_copyxxx() 28001 * directly from the mode argument of ioctl(). 28002 * 28003 * Return Code: the code returned by sd_send_scsi_cmd() 28004 * EFAULT if ddi_copyxxx() fails 28005 * ENXIO if fail ddi_get_soft_state 28006 * EINVAL if data pointer is NULL 28007 */ 28008 28009 static int 28010 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28011 { 28012 struct sd_lun *un; 28013 struct uscsi_cmd *com; 28014 struct cdrom_tochdr toc_header; 28015 struct cdrom_tochdr *hdr = &toc_header; 28016 char cdb[CDB_GROUP1]; 28017 int rval; 28018 caddr_t buffer; 28019 28020 if (data == NULL) { 28021 return (EINVAL); 28022 } 28023 28024 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28025 (un->un_state == SD_STATE_OFFLINE)) { 28026 return (ENXIO); 28027 } 28028 28029 buffer = kmem_zalloc(4, KM_SLEEP); 28030 bzero(cdb, CDB_GROUP1); 28031 cdb[0] = SCMD_READ_TOC; 28032 /* 28033 * Specifying a track number of 0x00 in the READ TOC command indicates 28034 * that the TOC header should be returned 28035 */ 28036 cdb[6] = 0x00; 28037 /* 28038 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28039 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28040 */ 28041 cdb[8] = 0x04; 28042 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28043 com->uscsi_cdb = cdb; 28044 com->uscsi_cdblen = CDB_GROUP1; 28045 com->uscsi_bufaddr = buffer; 28046 com->uscsi_buflen = 0x04; 28047 com->uscsi_timeout = 300; 28048 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28049 28050 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28051 SD_PATH_STANDARD); 28052 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28053 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28054 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28055 } else { 28056 hdr->cdth_trk0 = buffer[2]; 28057 hdr->cdth_trk1 = buffer[3]; 28058 } 28059 kmem_free(buffer, 4); 28060 kmem_free(com, sizeof (*com)); 28061 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28062 return (EFAULT); 28063 } 28064 return (rval); 28065 } 28066 28067 28068 /* 28069 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28070 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28071 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28072 * digital audio and extended architecture digital audio. These modes are 28073 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28074 * MMC specs. 28075 * 28076 * In addition to support for the various data formats these routines also 28077 * include support for devices that implement only the direct access READ 28078 * commands (0x08, 0x28), devices that implement the READ_CD commands 28079 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28080 * READ CDXA commands (0xD8, 0xDB) 28081 */ 28082 28083 /* 28084 * Function: sr_read_mode1() 28085 * 28086 * Description: This routine is the driver entry point for handling CD-ROM 28087 * ioctl read mode1 requests (CDROMREADMODE1). 28088 * 28089 * Arguments: dev - the device 'dev_t' 28090 * data - pointer to user provided cd read structure specifying 28091 * the lba buffer address and length. 28092 * flag - this argument is a pass through to ddi_copyxxx() 28093 * directly from the mode argument of ioctl(). 28094 * 28095 * Return Code: the code returned by sd_send_scsi_cmd() 28096 * EFAULT if ddi_copyxxx() fails 28097 * ENXIO if fail ddi_get_soft_state 28098 * EINVAL if data pointer is NULL 28099 */ 28100 28101 static int 28102 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28103 { 28104 struct sd_lun *un; 28105 struct cdrom_read mode1_struct; 28106 struct cdrom_read *mode1 = &mode1_struct; 28107 int rval; 28108 sd_ssc_t *ssc; 28109 28110 #ifdef _MULTI_DATAMODEL 28111 /* To support ILP32 applications in an LP64 world */ 28112 struct cdrom_read32 cdrom_read32; 28113 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28114 #endif /* _MULTI_DATAMODEL */ 28115 28116 if (data == NULL) { 28117 return (EINVAL); 28118 } 28119 28120 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28121 (un->un_state == SD_STATE_OFFLINE)) { 28122 return (ENXIO); 28123 } 28124 28125 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28126 "sd_read_mode1: entry: un:0x%p\n", un); 28127 28128 #ifdef _MULTI_DATAMODEL 28129 switch (ddi_model_convert_from(flag & FMODELS)) { 28130 case DDI_MODEL_ILP32: 28131 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28132 return (EFAULT); 28133 } 28134 /* Convert the ILP32 uscsi data from the application to LP64 */ 28135 cdrom_read32tocdrom_read(cdrd32, mode1); 28136 break; 28137 case DDI_MODEL_NONE: 28138 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28139 return (EFAULT); 28140 } 28141 } 28142 #else /* ! _MULTI_DATAMODEL */ 28143 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28144 return (EFAULT); 28145 } 28146 #endif /* _MULTI_DATAMODEL */ 28147 28148 ssc = sd_ssc_init(un); 28149 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr, 28150 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28151 sd_ssc_fini(ssc); 28152 28153 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28154 "sd_read_mode1: exit: un:0x%p\n", un); 28155 28156 return (rval); 28157 } 28158 28159 28160 /* 28161 * Function: sr_read_cd_mode2() 28162 * 28163 * Description: This routine is the driver entry point for handling CD-ROM 28164 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28165 * support the READ CD (0xBE) command or the 1st generation 28166 * READ CD (0xD4) command. 28167 * 28168 * Arguments: dev - the device 'dev_t' 28169 * data - pointer to user provided cd read structure specifying 28170 * the lba buffer address and length. 28171 * flag - this argument is a pass through to ddi_copyxxx() 28172 * directly from the mode argument of ioctl(). 28173 * 28174 * Return Code: the code returned by sd_send_scsi_cmd() 28175 * EFAULT if ddi_copyxxx() fails 28176 * ENXIO if fail ddi_get_soft_state 28177 * EINVAL if data pointer is NULL 28178 */ 28179 28180 static int 28181 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28182 { 28183 struct sd_lun *un; 28184 struct uscsi_cmd *com; 28185 struct cdrom_read mode2_struct; 28186 struct cdrom_read *mode2 = &mode2_struct; 28187 uchar_t cdb[CDB_GROUP5]; 28188 int nblocks; 28189 int rval; 28190 #ifdef _MULTI_DATAMODEL 28191 /* To support ILP32 applications in an LP64 world */ 28192 struct cdrom_read32 cdrom_read32; 28193 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28194 #endif /* _MULTI_DATAMODEL */ 28195 28196 if (data == NULL) { 28197 return (EINVAL); 28198 } 28199 28200 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28201 (un->un_state == SD_STATE_OFFLINE)) { 28202 return (ENXIO); 28203 } 28204 28205 #ifdef _MULTI_DATAMODEL 28206 switch (ddi_model_convert_from(flag & FMODELS)) { 28207 case DDI_MODEL_ILP32: 28208 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28209 return (EFAULT); 28210 } 28211 /* Convert the ILP32 uscsi data from the application to LP64 */ 28212 cdrom_read32tocdrom_read(cdrd32, mode2); 28213 break; 28214 case DDI_MODEL_NONE: 28215 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28216 return (EFAULT); 28217 } 28218 break; 28219 } 28220 28221 #else /* ! _MULTI_DATAMODEL */ 28222 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28223 return (EFAULT); 28224 } 28225 #endif /* _MULTI_DATAMODEL */ 28226 28227 bzero(cdb, sizeof (cdb)); 28228 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28229 /* Read command supported by 1st generation atapi drives */ 28230 cdb[0] = SCMD_READ_CDD4; 28231 } else { 28232 /* Universal CD Access Command */ 28233 cdb[0] = SCMD_READ_CD; 28234 } 28235 28236 /* 28237 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28238 */ 28239 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28240 28241 /* set the start address */ 28242 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28243 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28244 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28245 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28246 28247 /* set the transfer length */ 28248 nblocks = mode2->cdread_buflen / 2336; 28249 cdb[6] = (uchar_t)(nblocks >> 16); 28250 cdb[7] = (uchar_t)(nblocks >> 8); 28251 cdb[8] = (uchar_t)nblocks; 28252 28253 /* set the filter bits */ 28254 cdb[9] = CDROM_READ_CD_USERDATA; 28255 28256 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28257 com->uscsi_cdb = (caddr_t)cdb; 28258 com->uscsi_cdblen = sizeof (cdb); 28259 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28260 com->uscsi_buflen = mode2->cdread_buflen; 28261 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28262 28263 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28264 SD_PATH_STANDARD); 28265 kmem_free(com, sizeof (*com)); 28266 return (rval); 28267 } 28268 28269 28270 /* 28271 * Function: sr_read_mode2() 28272 * 28273 * Description: This routine is the driver entry point for handling CD-ROM 28274 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28275 * do not support the READ CD (0xBE) command. 28276 * 28277 * Arguments: dev - the device 'dev_t' 28278 * data - pointer to user provided cd read structure specifying 28279 * the lba buffer address and length. 28280 * flag - this argument is a pass through to ddi_copyxxx() 28281 * directly from the mode argument of ioctl(). 28282 * 28283 * Return Code: the code returned by sd_send_scsi_cmd() 28284 * EFAULT if ddi_copyxxx() fails 28285 * ENXIO if fail ddi_get_soft_state 28286 * EINVAL if data pointer is NULL 28287 * EIO if fail to reset block size 28288 * EAGAIN if commands are in progress in the driver 28289 */ 28290 28291 static int 28292 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28293 { 28294 struct sd_lun *un; 28295 struct cdrom_read mode2_struct; 28296 struct cdrom_read *mode2 = &mode2_struct; 28297 int rval; 28298 uint32_t restore_blksize; 28299 struct uscsi_cmd *com; 28300 uchar_t cdb[CDB_GROUP0]; 28301 int nblocks; 28302 28303 #ifdef _MULTI_DATAMODEL 28304 /* To support ILP32 applications in an LP64 world */ 28305 struct cdrom_read32 cdrom_read32; 28306 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28307 #endif /* _MULTI_DATAMODEL */ 28308 28309 if (data == NULL) { 28310 return (EINVAL); 28311 } 28312 28313 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28314 (un->un_state == SD_STATE_OFFLINE)) { 28315 return (ENXIO); 28316 } 28317 28318 /* 28319 * Because this routine will update the device and driver block size 28320 * being used we want to make sure there are no commands in progress. 28321 * If commands are in progress the user will have to try again. 28322 * 28323 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28324 * in sdioctl to protect commands from sdioctl through to the top of 28325 * sd_uscsi_strategy. See sdioctl for details. 28326 */ 28327 mutex_enter(SD_MUTEX(un)); 28328 if (un->un_ncmds_in_driver != 1) { 28329 mutex_exit(SD_MUTEX(un)); 28330 return (EAGAIN); 28331 } 28332 mutex_exit(SD_MUTEX(un)); 28333 28334 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28335 "sd_read_mode2: entry: un:0x%p\n", un); 28336 28337 #ifdef _MULTI_DATAMODEL 28338 switch (ddi_model_convert_from(flag & FMODELS)) { 28339 case DDI_MODEL_ILP32: 28340 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28341 return (EFAULT); 28342 } 28343 /* Convert the ILP32 uscsi data from the application to LP64 */ 28344 cdrom_read32tocdrom_read(cdrd32, mode2); 28345 break; 28346 case DDI_MODEL_NONE: 28347 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28348 return (EFAULT); 28349 } 28350 break; 28351 } 28352 #else /* ! _MULTI_DATAMODEL */ 28353 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28354 return (EFAULT); 28355 } 28356 #endif /* _MULTI_DATAMODEL */ 28357 28358 /* Store the current target block size for restoration later */ 28359 restore_blksize = un->un_tgt_blocksize; 28360 28361 /* Change the device and soft state target block size to 2336 */ 28362 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28363 rval = EIO; 28364 goto done; 28365 } 28366 28367 28368 bzero(cdb, sizeof (cdb)); 28369 28370 /* set READ operation */ 28371 cdb[0] = SCMD_READ; 28372 28373 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28374 mode2->cdread_lba >>= 2; 28375 28376 /* set the start address */ 28377 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28378 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28379 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28380 28381 /* set the transfer length */ 28382 nblocks = mode2->cdread_buflen / 2336; 28383 cdb[4] = (uchar_t)nblocks & 0xFF; 28384 28385 /* build command */ 28386 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28387 com->uscsi_cdb = (caddr_t)cdb; 28388 com->uscsi_cdblen = sizeof (cdb); 28389 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28390 com->uscsi_buflen = mode2->cdread_buflen; 28391 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28392 28393 /* 28394 * Issue SCSI command with user space address for read buffer. 28395 * 28396 * This sends the command through main channel in the driver. 28397 * 28398 * Since this is accessed via an IOCTL call, we go through the 28399 * standard path, so that if the device was powered down, then 28400 * it would be 'awakened' to handle the command. 28401 */ 28402 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28403 SD_PATH_STANDARD); 28404 28405 kmem_free(com, sizeof (*com)); 28406 28407 /* Restore the device and soft state target block size */ 28408 if (sr_sector_mode(dev, restore_blksize) != 0) { 28409 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28410 "can't do switch back to mode 1\n"); 28411 /* 28412 * If sd_send_scsi_READ succeeded we still need to report 28413 * an error because we failed to reset the block size 28414 */ 28415 if (rval == 0) { 28416 rval = EIO; 28417 } 28418 } 28419 28420 done: 28421 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28422 "sd_read_mode2: exit: un:0x%p\n", un); 28423 28424 return (rval); 28425 } 28426 28427 28428 /* 28429 * Function: sr_sector_mode() 28430 * 28431 * Description: This utility function is used by sr_read_mode2 to set the target 28432 * block size based on the user specified size. This is a legacy 28433 * implementation based upon a vendor specific mode page 28434 * 28435 * Arguments: dev - the device 'dev_t' 28436 * data - flag indicating if block size is being set to 2336 or 28437 * 512. 28438 * 28439 * Return Code: the code returned by sd_send_scsi_cmd() 28440 * EFAULT if ddi_copyxxx() fails 28441 * ENXIO if fail ddi_get_soft_state 28442 * EINVAL if data pointer is NULL 28443 */ 28444 28445 static int 28446 sr_sector_mode(dev_t dev, uint32_t blksize) 28447 { 28448 struct sd_lun *un; 28449 uchar_t *sense; 28450 uchar_t *select; 28451 int rval; 28452 sd_ssc_t *ssc; 28453 28454 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28455 (un->un_state == SD_STATE_OFFLINE)) { 28456 return (ENXIO); 28457 } 28458 28459 sense = kmem_zalloc(20, KM_SLEEP); 28460 28461 /* Note: This is a vendor specific mode page (0x81) */ 28462 ssc = sd_ssc_init(un); 28463 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81, 28464 SD_PATH_STANDARD); 28465 sd_ssc_fini(ssc); 28466 if (rval != 0) { 28467 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28468 "sr_sector_mode: Mode Sense failed\n"); 28469 kmem_free(sense, 20); 28470 return (rval); 28471 } 28472 select = kmem_zalloc(20, KM_SLEEP); 28473 select[3] = 0x08; 28474 select[10] = ((blksize >> 8) & 0xff); 28475 select[11] = (blksize & 0xff); 28476 select[12] = 0x01; 28477 select[13] = 0x06; 28478 select[14] = sense[14]; 28479 select[15] = sense[15]; 28480 if (blksize == SD_MODE2_BLKSIZE) { 28481 select[14] |= 0x01; 28482 } 28483 28484 ssc = sd_ssc_init(un); 28485 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20, 28486 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28487 sd_ssc_fini(ssc); 28488 if (rval != 0) { 28489 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28490 "sr_sector_mode: Mode Select failed\n"); 28491 } else { 28492 /* 28493 * Only update the softstate block size if we successfully 28494 * changed the device block mode. 28495 */ 28496 mutex_enter(SD_MUTEX(un)); 28497 sd_update_block_info(un, blksize, 0); 28498 mutex_exit(SD_MUTEX(un)); 28499 } 28500 kmem_free(sense, 20); 28501 kmem_free(select, 20); 28502 return (rval); 28503 } 28504 28505 28506 /* 28507 * Function: sr_read_cdda() 28508 * 28509 * Description: This routine is the driver entry point for handling CD-ROM 28510 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28511 * the target supports CDDA these requests are handled via a vendor 28512 * specific command (0xD8) If the target does not support CDDA 28513 * these requests are handled via the READ CD command (0xBE). 28514 * 28515 * Arguments: dev - the device 'dev_t' 28516 * data - pointer to user provided CD-DA structure specifying 28517 * the track starting address, transfer length, and 28518 * subcode options. 28519 * flag - this argument is a pass through to ddi_copyxxx() 28520 * directly from the mode argument of ioctl(). 28521 * 28522 * Return Code: the code returned by sd_send_scsi_cmd() 28523 * EFAULT if ddi_copyxxx() fails 28524 * ENXIO if fail ddi_get_soft_state 28525 * EINVAL if invalid arguments are provided 28526 * ENOTTY 28527 */ 28528 28529 static int 28530 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28531 { 28532 struct sd_lun *un; 28533 struct uscsi_cmd *com; 28534 struct cdrom_cdda *cdda; 28535 int rval; 28536 size_t buflen; 28537 char cdb[CDB_GROUP5]; 28538 28539 #ifdef _MULTI_DATAMODEL 28540 /* To support ILP32 applications in an LP64 world */ 28541 struct cdrom_cdda32 cdrom_cdda32; 28542 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28543 #endif /* _MULTI_DATAMODEL */ 28544 28545 if (data == NULL) { 28546 return (EINVAL); 28547 } 28548 28549 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28550 return (ENXIO); 28551 } 28552 28553 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28554 28555 #ifdef _MULTI_DATAMODEL 28556 switch (ddi_model_convert_from(flag & FMODELS)) { 28557 case DDI_MODEL_ILP32: 28558 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28559 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28560 "sr_read_cdda: ddi_copyin Failed\n"); 28561 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28562 return (EFAULT); 28563 } 28564 /* Convert the ILP32 uscsi data from the application to LP64 */ 28565 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28566 break; 28567 case DDI_MODEL_NONE: 28568 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28569 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28570 "sr_read_cdda: ddi_copyin Failed\n"); 28571 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28572 return (EFAULT); 28573 } 28574 break; 28575 } 28576 #else /* ! _MULTI_DATAMODEL */ 28577 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28578 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28579 "sr_read_cdda: ddi_copyin Failed\n"); 28580 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28581 return (EFAULT); 28582 } 28583 #endif /* _MULTI_DATAMODEL */ 28584 28585 /* 28586 * Since MMC-2 expects max 3 bytes for length, check if the 28587 * length input is greater than 3 bytes 28588 */ 28589 if ((cdda->cdda_length & 0xFF000000) != 0) { 28590 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28591 "cdrom transfer length too large: %d (limit %d)\n", 28592 cdda->cdda_length, 0xFFFFFF); 28593 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28594 return (EINVAL); 28595 } 28596 28597 switch (cdda->cdda_subcode) { 28598 case CDROM_DA_NO_SUBCODE: 28599 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28600 break; 28601 case CDROM_DA_SUBQ: 28602 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28603 break; 28604 case CDROM_DA_ALL_SUBCODE: 28605 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28606 break; 28607 case CDROM_DA_SUBCODE_ONLY: 28608 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28609 break; 28610 default: 28611 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28612 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28613 cdda->cdda_subcode); 28614 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28615 return (EINVAL); 28616 } 28617 28618 /* Build and send the command */ 28619 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28620 bzero(cdb, CDB_GROUP5); 28621 28622 if (un->un_f_cfg_cdda == TRUE) { 28623 cdb[0] = (char)SCMD_READ_CD; 28624 cdb[1] = 0x04; 28625 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28626 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28627 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28628 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28629 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28630 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28631 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28632 cdb[9] = 0x10; 28633 switch (cdda->cdda_subcode) { 28634 case CDROM_DA_NO_SUBCODE : 28635 cdb[10] = 0x0; 28636 break; 28637 case CDROM_DA_SUBQ : 28638 cdb[10] = 0x2; 28639 break; 28640 case CDROM_DA_ALL_SUBCODE : 28641 cdb[10] = 0x1; 28642 break; 28643 case CDROM_DA_SUBCODE_ONLY : 28644 /* FALLTHROUGH */ 28645 default : 28646 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28647 kmem_free(com, sizeof (*com)); 28648 return (ENOTTY); 28649 } 28650 } else { 28651 cdb[0] = (char)SCMD_READ_CDDA; 28652 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28653 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28654 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28655 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28656 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28657 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28658 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28659 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28660 cdb[10] = cdda->cdda_subcode; 28661 } 28662 28663 com->uscsi_cdb = cdb; 28664 com->uscsi_cdblen = CDB_GROUP5; 28665 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28666 com->uscsi_buflen = buflen; 28667 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28668 28669 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28670 SD_PATH_STANDARD); 28671 28672 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28673 kmem_free(com, sizeof (*com)); 28674 return (rval); 28675 } 28676 28677 28678 /* 28679 * Function: sr_read_cdxa() 28680 * 28681 * Description: This routine is the driver entry point for handling CD-ROM 28682 * ioctl requests to return CD-XA (Extended Architecture) data. 28683 * (CDROMCDXA). 28684 * 28685 * Arguments: dev - the device 'dev_t' 28686 * data - pointer to user provided CD-XA structure specifying 28687 * the data starting address, transfer length, and format 28688 * flag - this argument is a pass through to ddi_copyxxx() 28689 * directly from the mode argument of ioctl(). 28690 * 28691 * Return Code: the code returned by sd_send_scsi_cmd() 28692 * EFAULT if ddi_copyxxx() fails 28693 * ENXIO if fail ddi_get_soft_state 28694 * EINVAL if data pointer is NULL 28695 */ 28696 28697 static int 28698 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28699 { 28700 struct sd_lun *un; 28701 struct uscsi_cmd *com; 28702 struct cdrom_cdxa *cdxa; 28703 int rval; 28704 size_t buflen; 28705 char cdb[CDB_GROUP5]; 28706 uchar_t read_flags; 28707 28708 #ifdef _MULTI_DATAMODEL 28709 /* To support ILP32 applications in an LP64 world */ 28710 struct cdrom_cdxa32 cdrom_cdxa32; 28711 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28712 #endif /* _MULTI_DATAMODEL */ 28713 28714 if (data == NULL) { 28715 return (EINVAL); 28716 } 28717 28718 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28719 return (ENXIO); 28720 } 28721 28722 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28723 28724 #ifdef _MULTI_DATAMODEL 28725 switch (ddi_model_convert_from(flag & FMODELS)) { 28726 case DDI_MODEL_ILP32: 28727 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28728 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28729 return (EFAULT); 28730 } 28731 /* 28732 * Convert the ILP32 uscsi data from the 28733 * application to LP64 for internal use. 28734 */ 28735 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28736 break; 28737 case DDI_MODEL_NONE: 28738 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28739 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28740 return (EFAULT); 28741 } 28742 break; 28743 } 28744 #else /* ! _MULTI_DATAMODEL */ 28745 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28746 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28747 return (EFAULT); 28748 } 28749 #endif /* _MULTI_DATAMODEL */ 28750 28751 /* 28752 * Since MMC-2 expects max 3 bytes for length, check if the 28753 * length input is greater than 3 bytes 28754 */ 28755 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28756 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28757 "cdrom transfer length too large: %d (limit %d)\n", 28758 cdxa->cdxa_length, 0xFFFFFF); 28759 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28760 return (EINVAL); 28761 } 28762 28763 switch (cdxa->cdxa_format) { 28764 case CDROM_XA_DATA: 28765 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28766 read_flags = 0x10; 28767 break; 28768 case CDROM_XA_SECTOR_DATA: 28769 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28770 read_flags = 0xf8; 28771 break; 28772 case CDROM_XA_DATA_W_ERROR: 28773 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28774 read_flags = 0xfc; 28775 break; 28776 default: 28777 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28778 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28779 cdxa->cdxa_format); 28780 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28781 return (EINVAL); 28782 } 28783 28784 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28785 bzero(cdb, CDB_GROUP5); 28786 if (un->un_f_mmc_cap == TRUE) { 28787 cdb[0] = (char)SCMD_READ_CD; 28788 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28789 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28790 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28791 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28792 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28793 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28794 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28795 cdb[9] = (char)read_flags; 28796 } else { 28797 /* 28798 * Note: A vendor specific command (0xDB) is being used her to 28799 * request a read of all subcodes. 28800 */ 28801 cdb[0] = (char)SCMD_READ_CDXA; 28802 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28803 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28804 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28805 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28806 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28807 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28808 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28809 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28810 cdb[10] = cdxa->cdxa_format; 28811 } 28812 com->uscsi_cdb = cdb; 28813 com->uscsi_cdblen = CDB_GROUP5; 28814 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28815 com->uscsi_buflen = buflen; 28816 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28817 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28818 SD_PATH_STANDARD); 28819 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28820 kmem_free(com, sizeof (*com)); 28821 return (rval); 28822 } 28823 28824 28825 /* 28826 * Function: sr_eject() 28827 * 28828 * Description: This routine is the driver entry point for handling CD-ROM 28829 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28830 * 28831 * Arguments: dev - the device 'dev_t' 28832 * 28833 * Return Code: the code returned by sd_send_scsi_cmd() 28834 */ 28835 28836 static int 28837 sr_eject(dev_t dev) 28838 { 28839 struct sd_lun *un; 28840 int rval; 28841 sd_ssc_t *ssc; 28842 28843 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28844 (un->un_state == SD_STATE_OFFLINE)) { 28845 return (ENXIO); 28846 } 28847 28848 /* 28849 * To prevent race conditions with the eject 28850 * command, keep track of an eject command as 28851 * it progresses. If we are already handling 28852 * an eject command in the driver for the given 28853 * unit and another request to eject is received 28854 * immediately return EAGAIN so we don't lose 28855 * the command if the current eject command fails. 28856 */ 28857 mutex_enter(SD_MUTEX(un)); 28858 if (un->un_f_ejecting == TRUE) { 28859 mutex_exit(SD_MUTEX(un)); 28860 return (EAGAIN); 28861 } 28862 un->un_f_ejecting = TRUE; 28863 mutex_exit(SD_MUTEX(un)); 28864 28865 ssc = sd_ssc_init(un); 28866 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 28867 SD_PATH_STANDARD); 28868 sd_ssc_fini(ssc); 28869 28870 if (rval != 0) { 28871 mutex_enter(SD_MUTEX(un)); 28872 un->un_f_ejecting = FALSE; 28873 mutex_exit(SD_MUTEX(un)); 28874 return (rval); 28875 } 28876 28877 ssc = sd_ssc_init(un); 28878 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 28879 SD_TARGET_EJECT, SD_PATH_STANDARD); 28880 sd_ssc_fini(ssc); 28881 28882 if (rval == 0) { 28883 mutex_enter(SD_MUTEX(un)); 28884 sr_ejected(un); 28885 un->un_mediastate = DKIO_EJECTED; 28886 un->un_f_ejecting = FALSE; 28887 cv_broadcast(&un->un_state_cv); 28888 mutex_exit(SD_MUTEX(un)); 28889 } else { 28890 mutex_enter(SD_MUTEX(un)); 28891 un->un_f_ejecting = FALSE; 28892 mutex_exit(SD_MUTEX(un)); 28893 } 28894 return (rval); 28895 } 28896 28897 28898 /* 28899 * Function: sr_ejected() 28900 * 28901 * Description: This routine updates the soft state structure to invalidate the 28902 * geometry information after the media has been ejected or a 28903 * media eject has been detected. 28904 * 28905 * Arguments: un - driver soft state (unit) structure 28906 */ 28907 28908 static void 28909 sr_ejected(struct sd_lun *un) 28910 { 28911 struct sd_errstats *stp; 28912 28913 ASSERT(un != NULL); 28914 ASSERT(mutex_owned(SD_MUTEX(un))); 28915 28916 un->un_f_blockcount_is_valid = FALSE; 28917 un->un_f_tgt_blocksize_is_valid = FALSE; 28918 mutex_exit(SD_MUTEX(un)); 28919 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 28920 mutex_enter(SD_MUTEX(un)); 28921 28922 if (un->un_errstats != NULL) { 28923 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28924 stp->sd_capacity.value.ui64 = 0; 28925 } 28926 } 28927 28928 28929 /* 28930 * Function: sr_check_wp() 28931 * 28932 * Description: This routine checks the write protection of a removable 28933 * media disk and hotpluggable devices via the write protect bit of 28934 * the Mode Page Header device specific field. Some devices choke 28935 * on unsupported mode page. In order to workaround this issue, 28936 * this routine has been implemented to use 0x3f mode page(request 28937 * for all pages) for all device types. 28938 * 28939 * Arguments: dev - the device 'dev_t' 28940 * 28941 * Return Code: int indicating if the device is write protected (1) or not (0) 28942 * 28943 * Context: Kernel thread. 28944 * 28945 */ 28946 28947 static int 28948 sr_check_wp(dev_t dev) 28949 { 28950 struct sd_lun *un; 28951 uchar_t device_specific; 28952 uchar_t *sense; 28953 int hdrlen; 28954 int rval = FALSE; 28955 int status; 28956 sd_ssc_t *ssc; 28957 28958 /* 28959 * Note: The return codes for this routine should be reworked to 28960 * properly handle the case of a NULL softstate. 28961 */ 28962 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28963 return (FALSE); 28964 } 28965 28966 if (un->un_f_cfg_is_atapi == TRUE) { 28967 /* 28968 * The mode page contents are not required; set the allocation 28969 * length for the mode page header only 28970 */ 28971 hdrlen = MODE_HEADER_LENGTH_GRP2; 28972 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28973 ssc = sd_ssc_init(un); 28974 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen, 28975 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28976 sd_ssc_fini(ssc); 28977 if (status != 0) 28978 goto err_exit; 28979 device_specific = 28980 ((struct mode_header_grp2 *)sense)->device_specific; 28981 } else { 28982 hdrlen = MODE_HEADER_LENGTH; 28983 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28984 ssc = sd_ssc_init(un); 28985 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen, 28986 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28987 sd_ssc_fini(ssc); 28988 if (status != 0) 28989 goto err_exit; 28990 device_specific = 28991 ((struct mode_header *)sense)->device_specific; 28992 } 28993 28994 28995 /* 28996 * Write protect mode sense failed; not all disks 28997 * understand this query. Return FALSE assuming that 28998 * these devices are not writable. 28999 */ 29000 if (device_specific & WRITE_PROTECT) { 29001 rval = TRUE; 29002 } 29003 29004 err_exit: 29005 kmem_free(sense, hdrlen); 29006 return (rval); 29007 } 29008 29009 /* 29010 * Function: sr_volume_ctrl() 29011 * 29012 * Description: This routine is the driver entry point for handling CD-ROM 29013 * audio output volume ioctl requests. (CDROMVOLCTRL) 29014 * 29015 * Arguments: dev - the device 'dev_t' 29016 * data - pointer to user audio volume control structure 29017 * flag - this argument is a pass through to ddi_copyxxx() 29018 * directly from the mode argument of ioctl(). 29019 * 29020 * Return Code: the code returned by sd_send_scsi_cmd() 29021 * EFAULT if ddi_copyxxx() fails 29022 * ENXIO if fail ddi_get_soft_state 29023 * EINVAL if data pointer is NULL 29024 * 29025 */ 29026 29027 static int 29028 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29029 { 29030 struct sd_lun *un; 29031 struct cdrom_volctrl volume; 29032 struct cdrom_volctrl *vol = &volume; 29033 uchar_t *sense_page; 29034 uchar_t *select_page; 29035 uchar_t *sense; 29036 uchar_t *select; 29037 int sense_buflen; 29038 int select_buflen; 29039 int rval; 29040 sd_ssc_t *ssc; 29041 29042 if (data == NULL) { 29043 return (EINVAL); 29044 } 29045 29046 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29047 (un->un_state == SD_STATE_OFFLINE)) { 29048 return (ENXIO); 29049 } 29050 29051 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29052 return (EFAULT); 29053 } 29054 29055 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29056 struct mode_header_grp2 *sense_mhp; 29057 struct mode_header_grp2 *select_mhp; 29058 int bd_len; 29059 29060 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29061 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29062 MODEPAGE_AUDIO_CTRL_LEN; 29063 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29064 select = kmem_zalloc(select_buflen, KM_SLEEP); 29065 ssc = sd_ssc_init(un); 29066 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 29067 sense_buflen, MODEPAGE_AUDIO_CTRL, 29068 SD_PATH_STANDARD); 29069 sd_ssc_fini(ssc); 29070 29071 if (rval != 0) { 29072 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29073 "sr_volume_ctrl: Mode Sense Failed\n"); 29074 kmem_free(sense, sense_buflen); 29075 kmem_free(select, select_buflen); 29076 return (rval); 29077 } 29078 sense_mhp = (struct mode_header_grp2 *)sense; 29079 select_mhp = (struct mode_header_grp2 *)select; 29080 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29081 sense_mhp->bdesc_length_lo; 29082 if (bd_len > MODE_BLK_DESC_LENGTH) { 29083 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29084 "sr_volume_ctrl: Mode Sense returned invalid " 29085 "block descriptor length\n"); 29086 kmem_free(sense, sense_buflen); 29087 kmem_free(select, select_buflen); 29088 return (EIO); 29089 } 29090 sense_page = (uchar_t *) 29091 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29092 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29093 select_mhp->length_msb = 0; 29094 select_mhp->length_lsb = 0; 29095 select_mhp->bdesc_length_hi = 0; 29096 select_mhp->bdesc_length_lo = 0; 29097 } else { 29098 struct mode_header *sense_mhp, *select_mhp; 29099 29100 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29101 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29102 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29103 select = kmem_zalloc(select_buflen, KM_SLEEP); 29104 ssc = sd_ssc_init(un); 29105 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 29106 sense_buflen, MODEPAGE_AUDIO_CTRL, 29107 SD_PATH_STANDARD); 29108 sd_ssc_fini(ssc); 29109 29110 if (rval != 0) { 29111 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29112 "sr_volume_ctrl: Mode Sense Failed\n"); 29113 kmem_free(sense, sense_buflen); 29114 kmem_free(select, select_buflen); 29115 return (rval); 29116 } 29117 sense_mhp = (struct mode_header *)sense; 29118 select_mhp = (struct mode_header *)select; 29119 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29120 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29121 "sr_volume_ctrl: Mode Sense returned invalid " 29122 "block descriptor length\n"); 29123 kmem_free(sense, sense_buflen); 29124 kmem_free(select, select_buflen); 29125 return (EIO); 29126 } 29127 sense_page = (uchar_t *) 29128 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29129 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29130 select_mhp->length = 0; 29131 select_mhp->bdesc_length = 0; 29132 } 29133 /* 29134 * Note: An audio control data structure could be created and overlayed 29135 * on the following in place of the array indexing method implemented. 29136 */ 29137 29138 /* Build the select data for the user volume data */ 29139 select_page[0] = MODEPAGE_AUDIO_CTRL; 29140 select_page[1] = 0xE; 29141 /* Set the immediate bit */ 29142 select_page[2] = 0x04; 29143 /* Zero out reserved fields */ 29144 select_page[3] = 0x00; 29145 select_page[4] = 0x00; 29146 /* Return sense data for fields not to be modified */ 29147 select_page[5] = sense_page[5]; 29148 select_page[6] = sense_page[6]; 29149 select_page[7] = sense_page[7]; 29150 /* Set the user specified volume levels for channel 0 and 1 */ 29151 select_page[8] = 0x01; 29152 select_page[9] = vol->channel0; 29153 select_page[10] = 0x02; 29154 select_page[11] = vol->channel1; 29155 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29156 select_page[12] = sense_page[12]; 29157 select_page[13] = sense_page[13]; 29158 select_page[14] = sense_page[14]; 29159 select_page[15] = sense_page[15]; 29160 29161 ssc = sd_ssc_init(un); 29162 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29163 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select, 29164 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29165 } else { 29166 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 29167 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29168 } 29169 sd_ssc_fini(ssc); 29170 29171 kmem_free(sense, sense_buflen); 29172 kmem_free(select, select_buflen); 29173 return (rval); 29174 } 29175 29176 29177 /* 29178 * Function: sr_read_sony_session_offset() 29179 * 29180 * Description: This routine is the driver entry point for handling CD-ROM 29181 * ioctl requests for session offset information. (CDROMREADOFFSET) 29182 * The address of the first track in the last session of a 29183 * multi-session CD-ROM is returned 29184 * 29185 * Note: This routine uses a vendor specific key value in the 29186 * command control field without implementing any vendor check here 29187 * or in the ioctl routine. 29188 * 29189 * Arguments: dev - the device 'dev_t' 29190 * data - pointer to an int to hold the requested address 29191 * flag - this argument is a pass through to ddi_copyxxx() 29192 * directly from the mode argument of ioctl(). 29193 * 29194 * Return Code: the code returned by sd_send_scsi_cmd() 29195 * EFAULT if ddi_copyxxx() fails 29196 * ENXIO if fail ddi_get_soft_state 29197 * EINVAL if data pointer is NULL 29198 */ 29199 29200 static int 29201 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29202 { 29203 struct sd_lun *un; 29204 struct uscsi_cmd *com; 29205 caddr_t buffer; 29206 char cdb[CDB_GROUP1]; 29207 int session_offset = 0; 29208 int rval; 29209 29210 if (data == NULL) { 29211 return (EINVAL); 29212 } 29213 29214 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29215 (un->un_state == SD_STATE_OFFLINE)) { 29216 return (ENXIO); 29217 } 29218 29219 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29220 bzero(cdb, CDB_GROUP1); 29221 cdb[0] = SCMD_READ_TOC; 29222 /* 29223 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29224 * (4 byte TOC response header + 8 byte response data) 29225 */ 29226 cdb[8] = SONY_SESSION_OFFSET_LEN; 29227 /* Byte 9 is the control byte. A vendor specific value is used */ 29228 cdb[9] = SONY_SESSION_OFFSET_KEY; 29229 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29230 com->uscsi_cdb = cdb; 29231 com->uscsi_cdblen = CDB_GROUP1; 29232 com->uscsi_bufaddr = buffer; 29233 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29234 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29235 29236 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 29237 SD_PATH_STANDARD); 29238 if (rval != 0) { 29239 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29240 kmem_free(com, sizeof (*com)); 29241 return (rval); 29242 } 29243 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29244 session_offset = 29245 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29246 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29247 /* 29248 * Offset returned offset in current lbasize block's. Convert to 29249 * 2k block's to return to the user 29250 */ 29251 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29252 session_offset >>= 2; 29253 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29254 session_offset >>= 1; 29255 } 29256 } 29257 29258 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29259 rval = EFAULT; 29260 } 29261 29262 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29263 kmem_free(com, sizeof (*com)); 29264 return (rval); 29265 } 29266 29267 29268 /* 29269 * Function: sd_wm_cache_constructor() 29270 * 29271 * Description: Cache Constructor for the wmap cache for the read/modify/write 29272 * devices. 29273 * 29274 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29275 * un - sd_lun structure for the device. 29276 * flag - the km flags passed to constructor 29277 * 29278 * Return Code: 0 on success. 29279 * -1 on failure. 29280 */ 29281 29282 /*ARGSUSED*/ 29283 static int 29284 sd_wm_cache_constructor(void *wm, void *un, int flags) 29285 { 29286 bzero(wm, sizeof (struct sd_w_map)); 29287 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29288 return (0); 29289 } 29290 29291 29292 /* 29293 * Function: sd_wm_cache_destructor() 29294 * 29295 * Description: Cache destructor for the wmap cache for the read/modify/write 29296 * devices. 29297 * 29298 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29299 * un - sd_lun structure for the device. 29300 */ 29301 /*ARGSUSED*/ 29302 static void 29303 sd_wm_cache_destructor(void *wm, void *un) 29304 { 29305 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29306 } 29307 29308 29309 /* 29310 * Function: sd_range_lock() 29311 * 29312 * Description: Lock the range of blocks specified as parameter to ensure 29313 * that read, modify write is atomic and no other i/o writes 29314 * to the same location. The range is specified in terms 29315 * of start and end blocks. Block numbers are the actual 29316 * media block numbers and not system. 29317 * 29318 * Arguments: un - sd_lun structure for the device. 29319 * startb - The starting block number 29320 * endb - The end block number 29321 * typ - type of i/o - simple/read_modify_write 29322 * 29323 * Return Code: wm - pointer to the wmap structure. 29324 * 29325 * Context: This routine can sleep. 29326 */ 29327 29328 static struct sd_w_map * 29329 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29330 { 29331 struct sd_w_map *wmp = NULL; 29332 struct sd_w_map *sl_wmp = NULL; 29333 struct sd_w_map *tmp_wmp; 29334 wm_state state = SD_WM_CHK_LIST; 29335 29336 29337 ASSERT(un != NULL); 29338 ASSERT(!mutex_owned(SD_MUTEX(un))); 29339 29340 mutex_enter(SD_MUTEX(un)); 29341 29342 while (state != SD_WM_DONE) { 29343 29344 switch (state) { 29345 case SD_WM_CHK_LIST: 29346 /* 29347 * This is the starting state. Check the wmap list 29348 * to see if the range is currently available. 29349 */ 29350 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29351 /* 29352 * If this is a simple write and no rmw 29353 * i/o is pending then try to lock the 29354 * range as the range should be available. 29355 */ 29356 state = SD_WM_LOCK_RANGE; 29357 } else { 29358 tmp_wmp = sd_get_range(un, startb, endb); 29359 if (tmp_wmp != NULL) { 29360 if ((wmp != NULL) && ONLIST(un, wmp)) { 29361 /* 29362 * Should not keep onlist wmps 29363 * while waiting this macro 29364 * will also do wmp = NULL; 29365 */ 29366 FREE_ONLIST_WMAP(un, wmp); 29367 } 29368 /* 29369 * sl_wmp is the wmap on which wait 29370 * is done, since the tmp_wmp points 29371 * to the inuse wmap, set sl_wmp to 29372 * tmp_wmp and change the state to sleep 29373 */ 29374 sl_wmp = tmp_wmp; 29375 state = SD_WM_WAIT_MAP; 29376 } else { 29377 state = SD_WM_LOCK_RANGE; 29378 } 29379 29380 } 29381 break; 29382 29383 case SD_WM_LOCK_RANGE: 29384 ASSERT(un->un_wm_cache); 29385 /* 29386 * The range need to be locked, try to get a wmap. 29387 * First attempt it with NO_SLEEP, want to avoid a sleep 29388 * if possible as we will have to release the sd mutex 29389 * if we have to sleep. 29390 */ 29391 if (wmp == NULL) 29392 wmp = kmem_cache_alloc(un->un_wm_cache, 29393 KM_NOSLEEP); 29394 if (wmp == NULL) { 29395 mutex_exit(SD_MUTEX(un)); 29396 _NOTE(DATA_READABLE_WITHOUT_LOCK 29397 (sd_lun::un_wm_cache)) 29398 wmp = kmem_cache_alloc(un->un_wm_cache, 29399 KM_SLEEP); 29400 mutex_enter(SD_MUTEX(un)); 29401 /* 29402 * we released the mutex so recheck and go to 29403 * check list state. 29404 */ 29405 state = SD_WM_CHK_LIST; 29406 } else { 29407 /* 29408 * We exit out of state machine since we 29409 * have the wmap. Do the housekeeping first. 29410 * place the wmap on the wmap list if it is not 29411 * on it already and then set the state to done. 29412 */ 29413 wmp->wm_start = startb; 29414 wmp->wm_end = endb; 29415 wmp->wm_flags = typ | SD_WM_BUSY; 29416 if (typ & SD_WTYPE_RMW) { 29417 un->un_rmw_count++; 29418 } 29419 /* 29420 * If not already on the list then link 29421 */ 29422 if (!ONLIST(un, wmp)) { 29423 wmp->wm_next = un->un_wm; 29424 wmp->wm_prev = NULL; 29425 if (wmp->wm_next) 29426 wmp->wm_next->wm_prev = wmp; 29427 un->un_wm = wmp; 29428 } 29429 state = SD_WM_DONE; 29430 } 29431 break; 29432 29433 case SD_WM_WAIT_MAP: 29434 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29435 /* 29436 * Wait is done on sl_wmp, which is set in the 29437 * check_list state. 29438 */ 29439 sl_wmp->wm_wanted_count++; 29440 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29441 sl_wmp->wm_wanted_count--; 29442 /* 29443 * We can reuse the memory from the completed sl_wmp 29444 * lock range for our new lock, but only if noone is 29445 * waiting for it. 29446 */ 29447 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29448 if (sl_wmp->wm_wanted_count == 0) { 29449 if (wmp != NULL) 29450 CHK_N_FREEWMP(un, wmp); 29451 wmp = sl_wmp; 29452 } 29453 sl_wmp = NULL; 29454 /* 29455 * After waking up, need to recheck for availability of 29456 * range. 29457 */ 29458 state = SD_WM_CHK_LIST; 29459 break; 29460 29461 default: 29462 panic("sd_range_lock: " 29463 "Unknown state %d in sd_range_lock", state); 29464 /*NOTREACHED*/ 29465 } /* switch(state) */ 29466 29467 } /* while(state != SD_WM_DONE) */ 29468 29469 mutex_exit(SD_MUTEX(un)); 29470 29471 ASSERT(wmp != NULL); 29472 29473 return (wmp); 29474 } 29475 29476 29477 /* 29478 * Function: sd_get_range() 29479 * 29480 * Description: Find if there any overlapping I/O to this one 29481 * Returns the write-map of 1st such I/O, NULL otherwise. 29482 * 29483 * Arguments: un - sd_lun structure for the device. 29484 * startb - The starting block number 29485 * endb - The end block number 29486 * 29487 * Return Code: wm - pointer to the wmap structure. 29488 */ 29489 29490 static struct sd_w_map * 29491 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29492 { 29493 struct sd_w_map *wmp; 29494 29495 ASSERT(un != NULL); 29496 29497 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29498 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29499 continue; 29500 } 29501 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29502 break; 29503 } 29504 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29505 break; 29506 } 29507 } 29508 29509 return (wmp); 29510 } 29511 29512 29513 /* 29514 * Function: sd_free_inlist_wmap() 29515 * 29516 * Description: Unlink and free a write map struct. 29517 * 29518 * Arguments: un - sd_lun structure for the device. 29519 * wmp - sd_w_map which needs to be unlinked. 29520 */ 29521 29522 static void 29523 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29524 { 29525 ASSERT(un != NULL); 29526 29527 if (un->un_wm == wmp) { 29528 un->un_wm = wmp->wm_next; 29529 } else { 29530 wmp->wm_prev->wm_next = wmp->wm_next; 29531 } 29532 29533 if (wmp->wm_next) { 29534 wmp->wm_next->wm_prev = wmp->wm_prev; 29535 } 29536 29537 wmp->wm_next = wmp->wm_prev = NULL; 29538 29539 kmem_cache_free(un->un_wm_cache, wmp); 29540 } 29541 29542 29543 /* 29544 * Function: sd_range_unlock() 29545 * 29546 * Description: Unlock the range locked by wm. 29547 * Free write map if nobody else is waiting on it. 29548 * 29549 * Arguments: un - sd_lun structure for the device. 29550 * wmp - sd_w_map which needs to be unlinked. 29551 */ 29552 29553 static void 29554 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29555 { 29556 ASSERT(un != NULL); 29557 ASSERT(wm != NULL); 29558 ASSERT(!mutex_owned(SD_MUTEX(un))); 29559 29560 mutex_enter(SD_MUTEX(un)); 29561 29562 if (wm->wm_flags & SD_WTYPE_RMW) { 29563 un->un_rmw_count--; 29564 } 29565 29566 if (wm->wm_wanted_count) { 29567 wm->wm_flags = 0; 29568 /* 29569 * Broadcast that the wmap is available now. 29570 */ 29571 cv_broadcast(&wm->wm_avail); 29572 } else { 29573 /* 29574 * If no one is waiting on the map, it should be free'ed. 29575 */ 29576 sd_free_inlist_wmap(un, wm); 29577 } 29578 29579 mutex_exit(SD_MUTEX(un)); 29580 } 29581 29582 29583 /* 29584 * Function: sd_read_modify_write_task 29585 * 29586 * Description: Called from a taskq thread to initiate the write phase of 29587 * a read-modify-write request. This is used for targets where 29588 * un->un_sys_blocksize != un->un_tgt_blocksize. 29589 * 29590 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29591 * 29592 * Context: Called under taskq thread context. 29593 */ 29594 29595 static void 29596 sd_read_modify_write_task(void *arg) 29597 { 29598 struct sd_mapblocksize_info *bsp; 29599 struct buf *bp; 29600 struct sd_xbuf *xp; 29601 struct sd_lun *un; 29602 29603 bp = arg; /* The bp is given in arg */ 29604 ASSERT(bp != NULL); 29605 29606 /* Get the pointer to the layer-private data struct */ 29607 xp = SD_GET_XBUF(bp); 29608 ASSERT(xp != NULL); 29609 bsp = xp->xb_private; 29610 ASSERT(bsp != NULL); 29611 29612 un = SD_GET_UN(bp); 29613 ASSERT(un != NULL); 29614 ASSERT(!mutex_owned(SD_MUTEX(un))); 29615 29616 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29617 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29618 29619 /* 29620 * This is the write phase of a read-modify-write request, called 29621 * under the context of a taskq thread in response to the completion 29622 * of the read portion of the rmw request completing under interrupt 29623 * context. The write request must be sent from here down the iostart 29624 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29625 * we use the layer index saved in the layer-private data area. 29626 */ 29627 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29628 29629 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29630 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29631 } 29632 29633 29634 /* 29635 * Function: sddump_do_read_of_rmw() 29636 * 29637 * Description: This routine will be called from sddump, If sddump is called 29638 * with an I/O which not aligned on device blocksize boundary 29639 * then the write has to be converted to read-modify-write. 29640 * Do the read part here in order to keep sddump simple. 29641 * Note - That the sd_mutex is held across the call to this 29642 * routine. 29643 * 29644 * Arguments: un - sd_lun 29645 * blkno - block number in terms of media block size. 29646 * nblk - number of blocks. 29647 * bpp - pointer to pointer to the buf structure. On return 29648 * from this function, *bpp points to the valid buffer 29649 * to which the write has to be done. 29650 * 29651 * Return Code: 0 for success or errno-type return code 29652 */ 29653 29654 static int 29655 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29656 struct buf **bpp) 29657 { 29658 int err; 29659 int i; 29660 int rval; 29661 struct buf *bp; 29662 struct scsi_pkt *pkt = NULL; 29663 uint32_t target_blocksize; 29664 29665 ASSERT(un != NULL); 29666 ASSERT(mutex_owned(SD_MUTEX(un))); 29667 29668 target_blocksize = un->un_tgt_blocksize; 29669 29670 mutex_exit(SD_MUTEX(un)); 29671 29672 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29673 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29674 if (bp == NULL) { 29675 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29676 "no resources for dumping; giving up"); 29677 err = ENOMEM; 29678 goto done; 29679 } 29680 29681 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29682 blkno, nblk); 29683 if (rval != 0) { 29684 scsi_free_consistent_buf(bp); 29685 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29686 "no resources for dumping; giving up"); 29687 err = ENOMEM; 29688 goto done; 29689 } 29690 29691 pkt->pkt_flags |= FLAG_NOINTR; 29692 29693 err = EIO; 29694 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29695 29696 /* 29697 * Scsi_poll returns 0 (success) if the command completes and 29698 * the status block is STATUS_GOOD. We should only check 29699 * errors if this condition is not true. Even then we should 29700 * send our own request sense packet only if we have a check 29701 * condition and auto request sense has not been performed by 29702 * the hba. 29703 */ 29704 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29705 29706 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29707 err = 0; 29708 break; 29709 } 29710 29711 /* 29712 * Check CMD_DEV_GONE 1st, give up if device is gone, 29713 * no need to read RQS data. 29714 */ 29715 if (pkt->pkt_reason == CMD_DEV_GONE) { 29716 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29717 "Error while dumping state with rmw..." 29718 "Device is gone\n"); 29719 break; 29720 } 29721 29722 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29723 SD_INFO(SD_LOG_DUMP, un, 29724 "sddump: read failed with CHECK, try # %d\n", i); 29725 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29726 (void) sd_send_polled_RQS(un); 29727 } 29728 29729 continue; 29730 } 29731 29732 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29733 int reset_retval = 0; 29734 29735 SD_INFO(SD_LOG_DUMP, un, 29736 "sddump: read failed with BUSY, try # %d\n", i); 29737 29738 if (un->un_f_lun_reset_enabled == TRUE) { 29739 reset_retval = scsi_reset(SD_ADDRESS(un), 29740 RESET_LUN); 29741 } 29742 if (reset_retval == 0) { 29743 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29744 } 29745 (void) sd_send_polled_RQS(un); 29746 29747 } else { 29748 SD_INFO(SD_LOG_DUMP, un, 29749 "sddump: read failed with 0x%x, try # %d\n", 29750 SD_GET_PKT_STATUS(pkt), i); 29751 mutex_enter(SD_MUTEX(un)); 29752 sd_reset_target(un, pkt); 29753 mutex_exit(SD_MUTEX(un)); 29754 } 29755 29756 /* 29757 * If we are not getting anywhere with lun/target resets, 29758 * let's reset the bus. 29759 */ 29760 if (i > SD_NDUMP_RETRIES/2) { 29761 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29762 (void) sd_send_polled_RQS(un); 29763 } 29764 29765 } 29766 scsi_destroy_pkt(pkt); 29767 29768 if (err != 0) { 29769 scsi_free_consistent_buf(bp); 29770 *bpp = NULL; 29771 } else { 29772 *bpp = bp; 29773 } 29774 29775 done: 29776 mutex_enter(SD_MUTEX(un)); 29777 return (err); 29778 } 29779 29780 29781 /* 29782 * Function: sd_failfast_flushq 29783 * 29784 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29785 * in b_flags and move them onto the failfast queue, then kick 29786 * off a thread to return all bp's on the failfast queue to 29787 * their owners with an error set. 29788 * 29789 * Arguments: un - pointer to the soft state struct for the instance. 29790 * 29791 * Context: may execute in interrupt context. 29792 */ 29793 29794 static void 29795 sd_failfast_flushq(struct sd_lun *un) 29796 { 29797 struct buf *bp; 29798 struct buf *next_waitq_bp; 29799 struct buf *prev_waitq_bp = NULL; 29800 29801 ASSERT(un != NULL); 29802 ASSERT(mutex_owned(SD_MUTEX(un))); 29803 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29804 ASSERT(un->un_failfast_bp == NULL); 29805 29806 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29807 "sd_failfast_flushq: entry: un:0x%p\n", un); 29808 29809 /* 29810 * Check if we should flush all bufs when entering failfast state, or 29811 * just those with B_FAILFAST set. 29812 */ 29813 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29814 /* 29815 * Move *all* bp's on the wait queue to the failfast flush 29816 * queue, including those that do NOT have B_FAILFAST set. 29817 */ 29818 if (un->un_failfast_headp == NULL) { 29819 ASSERT(un->un_failfast_tailp == NULL); 29820 un->un_failfast_headp = un->un_waitq_headp; 29821 } else { 29822 ASSERT(un->un_failfast_tailp != NULL); 29823 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29824 } 29825 29826 un->un_failfast_tailp = un->un_waitq_tailp; 29827 29828 /* update kstat for each bp moved out of the waitq */ 29829 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29830 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29831 } 29832 29833 /* empty the waitq */ 29834 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29835 29836 } else { 29837 /* 29838 * Go thru the wait queue, pick off all entries with 29839 * B_FAILFAST set, and move these onto the failfast queue. 29840 */ 29841 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29842 /* 29843 * Save the pointer to the next bp on the wait queue, 29844 * so we get to it on the next iteration of this loop. 29845 */ 29846 next_waitq_bp = bp->av_forw; 29847 29848 /* 29849 * If this bp from the wait queue does NOT have 29850 * B_FAILFAST set, just move on to the next element 29851 * in the wait queue. Note, this is the only place 29852 * where it is correct to set prev_waitq_bp. 29853 */ 29854 if ((bp->b_flags & B_FAILFAST) == 0) { 29855 prev_waitq_bp = bp; 29856 continue; 29857 } 29858 29859 /* 29860 * Remove the bp from the wait queue. 29861 */ 29862 if (bp == un->un_waitq_headp) { 29863 /* The bp is the first element of the waitq. */ 29864 un->un_waitq_headp = next_waitq_bp; 29865 if (un->un_waitq_headp == NULL) { 29866 /* The wait queue is now empty */ 29867 un->un_waitq_tailp = NULL; 29868 } 29869 } else { 29870 /* 29871 * The bp is either somewhere in the middle 29872 * or at the end of the wait queue. 29873 */ 29874 ASSERT(un->un_waitq_headp != NULL); 29875 ASSERT(prev_waitq_bp != NULL); 29876 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29877 == 0); 29878 if (bp == un->un_waitq_tailp) { 29879 /* bp is the last entry on the waitq. */ 29880 ASSERT(next_waitq_bp == NULL); 29881 un->un_waitq_tailp = prev_waitq_bp; 29882 } 29883 prev_waitq_bp->av_forw = next_waitq_bp; 29884 } 29885 bp->av_forw = NULL; 29886 29887 /* 29888 * update kstat since the bp is moved out of 29889 * the waitq 29890 */ 29891 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29892 29893 /* 29894 * Now put the bp onto the failfast queue. 29895 */ 29896 if (un->un_failfast_headp == NULL) { 29897 /* failfast queue is currently empty */ 29898 ASSERT(un->un_failfast_tailp == NULL); 29899 un->un_failfast_headp = 29900 un->un_failfast_tailp = bp; 29901 } else { 29902 /* Add the bp to the end of the failfast q */ 29903 ASSERT(un->un_failfast_tailp != NULL); 29904 ASSERT(un->un_failfast_tailp->b_flags & 29905 B_FAILFAST); 29906 un->un_failfast_tailp->av_forw = bp; 29907 un->un_failfast_tailp = bp; 29908 } 29909 } 29910 } 29911 29912 /* 29913 * Now return all bp's on the failfast queue to their owners. 29914 */ 29915 while ((bp = un->un_failfast_headp) != NULL) { 29916 29917 un->un_failfast_headp = bp->av_forw; 29918 if (un->un_failfast_headp == NULL) { 29919 un->un_failfast_tailp = NULL; 29920 } 29921 29922 /* 29923 * We want to return the bp with a failure error code, but 29924 * we do not want a call to sd_start_cmds() to occur here, 29925 * so use sd_return_failed_command_no_restart() instead of 29926 * sd_return_failed_command(). 29927 */ 29928 sd_return_failed_command_no_restart(un, bp, EIO); 29929 } 29930 29931 /* Flush the xbuf queues if required. */ 29932 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29933 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29934 } 29935 29936 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29937 "sd_failfast_flushq: exit: un:0x%p\n", un); 29938 } 29939 29940 29941 /* 29942 * Function: sd_failfast_flushq_callback 29943 * 29944 * Description: Return TRUE if the given bp meets the criteria for failfast 29945 * flushing. Used with ddi_xbuf_flushq(9F). 29946 * 29947 * Arguments: bp - ptr to buf struct to be examined. 29948 * 29949 * Context: Any 29950 */ 29951 29952 static int 29953 sd_failfast_flushq_callback(struct buf *bp) 29954 { 29955 /* 29956 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29957 * state is entered; OR (2) the given bp has B_FAILFAST set. 29958 */ 29959 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29960 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29961 } 29962 29963 29964 29965 /* 29966 * Function: sd_setup_next_xfer 29967 * 29968 * Description: Prepare next I/O operation using DMA_PARTIAL 29969 * 29970 */ 29971 29972 static int 29973 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29974 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29975 { 29976 ssize_t num_blks_not_xfered; 29977 daddr_t strt_blk_num; 29978 ssize_t bytes_not_xfered; 29979 int rval; 29980 29981 ASSERT(pkt->pkt_resid == 0); 29982 29983 /* 29984 * Calculate next block number and amount to be transferred. 29985 * 29986 * How much data NOT transfered to the HBA yet. 29987 */ 29988 bytes_not_xfered = xp->xb_dma_resid; 29989 29990 /* 29991 * figure how many blocks NOT transfered to the HBA yet. 29992 */ 29993 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29994 29995 /* 29996 * set starting block number to the end of what WAS transfered. 29997 */ 29998 strt_blk_num = xp->xb_blkno + 29999 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30000 30001 /* 30002 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30003 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30004 * the disk mutex here. 30005 */ 30006 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30007 strt_blk_num, num_blks_not_xfered); 30008 30009 if (rval == 0) { 30010 30011 /* 30012 * Success. 30013 * 30014 * Adjust things if there are still more blocks to be 30015 * transfered. 30016 */ 30017 xp->xb_dma_resid = pkt->pkt_resid; 30018 pkt->pkt_resid = 0; 30019 30020 return (1); 30021 } 30022 30023 /* 30024 * There's really only one possible return value from 30025 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30026 * returns NULL. 30027 */ 30028 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30029 30030 bp->b_resid = bp->b_bcount; 30031 bp->b_flags |= B_ERROR; 30032 30033 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30034 "Error setting up next portion of DMA transfer\n"); 30035 30036 return (0); 30037 } 30038 30039 /* 30040 * Function: sd_panic_for_res_conflict 30041 * 30042 * Description: Call panic with a string formatted with "Reservation Conflict" 30043 * and a human readable identifier indicating the SD instance 30044 * that experienced the reservation conflict. 30045 * 30046 * Arguments: un - pointer to the soft state struct for the instance. 30047 * 30048 * Context: may execute in interrupt context. 30049 */ 30050 30051 #define SD_RESV_CONFLICT_FMT_LEN 40 30052 void 30053 sd_panic_for_res_conflict(struct sd_lun *un) 30054 { 30055 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30056 char path_str[MAXPATHLEN]; 30057 30058 (void) snprintf(panic_str, sizeof (panic_str), 30059 "Reservation Conflict\nDisk: %s", 30060 ddi_pathname(SD_DEVINFO(un), path_str)); 30061 30062 panic(panic_str); 30063 } 30064 30065 /* 30066 * Note: The following sd_faultinjection_ioctl( ) routines implement 30067 * driver support for handling fault injection for error analysis 30068 * causing faults in multiple layers of the driver. 30069 * 30070 */ 30071 30072 #ifdef SD_FAULT_INJECTION 30073 static uint_t sd_fault_injection_on = 0; 30074 30075 /* 30076 * Function: sd_faultinjection_ioctl() 30077 * 30078 * Description: This routine is the driver entry point for handling 30079 * faultinjection ioctls to inject errors into the 30080 * layer model 30081 * 30082 * Arguments: cmd - the ioctl cmd received 30083 * arg - the arguments from user and returns 30084 */ 30085 30086 static void 30087 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30088 30089 uint_t i = 0; 30090 uint_t rval; 30091 30092 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30093 30094 mutex_enter(SD_MUTEX(un)); 30095 30096 switch (cmd) { 30097 case SDIOCRUN: 30098 /* Allow pushed faults to be injected */ 30099 SD_INFO(SD_LOG_SDTEST, un, 30100 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30101 30102 sd_fault_injection_on = 1; 30103 30104 SD_INFO(SD_LOG_IOERR, un, 30105 "sd_faultinjection_ioctl: run finished\n"); 30106 break; 30107 30108 case SDIOCSTART: 30109 /* Start Injection Session */ 30110 SD_INFO(SD_LOG_SDTEST, un, 30111 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30112 30113 sd_fault_injection_on = 0; 30114 un->sd_injection_mask = 0xFFFFFFFF; 30115 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30116 un->sd_fi_fifo_pkt[i] = NULL; 30117 un->sd_fi_fifo_xb[i] = NULL; 30118 un->sd_fi_fifo_un[i] = NULL; 30119 un->sd_fi_fifo_arq[i] = NULL; 30120 } 30121 un->sd_fi_fifo_start = 0; 30122 un->sd_fi_fifo_end = 0; 30123 30124 mutex_enter(&(un->un_fi_mutex)); 30125 un->sd_fi_log[0] = '\0'; 30126 un->sd_fi_buf_len = 0; 30127 mutex_exit(&(un->un_fi_mutex)); 30128 30129 SD_INFO(SD_LOG_IOERR, un, 30130 "sd_faultinjection_ioctl: start finished\n"); 30131 break; 30132 30133 case SDIOCSTOP: 30134 /* Stop Injection Session */ 30135 SD_INFO(SD_LOG_SDTEST, un, 30136 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30137 sd_fault_injection_on = 0; 30138 un->sd_injection_mask = 0x0; 30139 30140 /* Empty stray or unuseds structs from fifo */ 30141 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30142 if (un->sd_fi_fifo_pkt[i] != NULL) { 30143 kmem_free(un->sd_fi_fifo_pkt[i], 30144 sizeof (struct sd_fi_pkt)); 30145 } 30146 if (un->sd_fi_fifo_xb[i] != NULL) { 30147 kmem_free(un->sd_fi_fifo_xb[i], 30148 sizeof (struct sd_fi_xb)); 30149 } 30150 if (un->sd_fi_fifo_un[i] != NULL) { 30151 kmem_free(un->sd_fi_fifo_un[i], 30152 sizeof (struct sd_fi_un)); 30153 } 30154 if (un->sd_fi_fifo_arq[i] != NULL) { 30155 kmem_free(un->sd_fi_fifo_arq[i], 30156 sizeof (struct sd_fi_arq)); 30157 } 30158 un->sd_fi_fifo_pkt[i] = NULL; 30159 un->sd_fi_fifo_un[i] = NULL; 30160 un->sd_fi_fifo_xb[i] = NULL; 30161 un->sd_fi_fifo_arq[i] = NULL; 30162 } 30163 un->sd_fi_fifo_start = 0; 30164 un->sd_fi_fifo_end = 0; 30165 30166 SD_INFO(SD_LOG_IOERR, un, 30167 "sd_faultinjection_ioctl: stop finished\n"); 30168 break; 30169 30170 case SDIOCINSERTPKT: 30171 /* Store a packet struct to be pushed onto fifo */ 30172 SD_INFO(SD_LOG_SDTEST, un, 30173 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30174 30175 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30176 30177 sd_fault_injection_on = 0; 30178 30179 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30180 if (un->sd_fi_fifo_pkt[i] != NULL) { 30181 kmem_free(un->sd_fi_fifo_pkt[i], 30182 sizeof (struct sd_fi_pkt)); 30183 } 30184 if (arg != NULL) { 30185 un->sd_fi_fifo_pkt[i] = 30186 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30187 if (un->sd_fi_fifo_pkt[i] == NULL) { 30188 /* Alloc failed don't store anything */ 30189 break; 30190 } 30191 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30192 sizeof (struct sd_fi_pkt), 0); 30193 if (rval == -1) { 30194 kmem_free(un->sd_fi_fifo_pkt[i], 30195 sizeof (struct sd_fi_pkt)); 30196 un->sd_fi_fifo_pkt[i] = NULL; 30197 } 30198 } else { 30199 SD_INFO(SD_LOG_IOERR, un, 30200 "sd_faultinjection_ioctl: pkt null\n"); 30201 } 30202 break; 30203 30204 case SDIOCINSERTXB: 30205 /* Store a xb struct to be pushed onto fifo */ 30206 SD_INFO(SD_LOG_SDTEST, un, 30207 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30208 30209 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30210 30211 sd_fault_injection_on = 0; 30212 30213 if (un->sd_fi_fifo_xb[i] != NULL) { 30214 kmem_free(un->sd_fi_fifo_xb[i], 30215 sizeof (struct sd_fi_xb)); 30216 un->sd_fi_fifo_xb[i] = NULL; 30217 } 30218 if (arg != NULL) { 30219 un->sd_fi_fifo_xb[i] = 30220 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30221 if (un->sd_fi_fifo_xb[i] == NULL) { 30222 /* Alloc failed don't store anything */ 30223 break; 30224 } 30225 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30226 sizeof (struct sd_fi_xb), 0); 30227 30228 if (rval == -1) { 30229 kmem_free(un->sd_fi_fifo_xb[i], 30230 sizeof (struct sd_fi_xb)); 30231 un->sd_fi_fifo_xb[i] = NULL; 30232 } 30233 } else { 30234 SD_INFO(SD_LOG_IOERR, un, 30235 "sd_faultinjection_ioctl: xb null\n"); 30236 } 30237 break; 30238 30239 case SDIOCINSERTUN: 30240 /* Store a un struct to be pushed onto fifo */ 30241 SD_INFO(SD_LOG_SDTEST, un, 30242 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30243 30244 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30245 30246 sd_fault_injection_on = 0; 30247 30248 if (un->sd_fi_fifo_un[i] != NULL) { 30249 kmem_free(un->sd_fi_fifo_un[i], 30250 sizeof (struct sd_fi_un)); 30251 un->sd_fi_fifo_un[i] = NULL; 30252 } 30253 if (arg != NULL) { 30254 un->sd_fi_fifo_un[i] = 30255 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30256 if (un->sd_fi_fifo_un[i] == NULL) { 30257 /* Alloc failed don't store anything */ 30258 break; 30259 } 30260 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30261 sizeof (struct sd_fi_un), 0); 30262 if (rval == -1) { 30263 kmem_free(un->sd_fi_fifo_un[i], 30264 sizeof (struct sd_fi_un)); 30265 un->sd_fi_fifo_un[i] = NULL; 30266 } 30267 30268 } else { 30269 SD_INFO(SD_LOG_IOERR, un, 30270 "sd_faultinjection_ioctl: un null\n"); 30271 } 30272 30273 break; 30274 30275 case SDIOCINSERTARQ: 30276 /* Store a arq struct to be pushed onto fifo */ 30277 SD_INFO(SD_LOG_SDTEST, un, 30278 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30279 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30280 30281 sd_fault_injection_on = 0; 30282 30283 if (un->sd_fi_fifo_arq[i] != NULL) { 30284 kmem_free(un->sd_fi_fifo_arq[i], 30285 sizeof (struct sd_fi_arq)); 30286 un->sd_fi_fifo_arq[i] = NULL; 30287 } 30288 if (arg != NULL) { 30289 un->sd_fi_fifo_arq[i] = 30290 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30291 if (un->sd_fi_fifo_arq[i] == NULL) { 30292 /* Alloc failed don't store anything */ 30293 break; 30294 } 30295 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30296 sizeof (struct sd_fi_arq), 0); 30297 if (rval == -1) { 30298 kmem_free(un->sd_fi_fifo_arq[i], 30299 sizeof (struct sd_fi_arq)); 30300 un->sd_fi_fifo_arq[i] = NULL; 30301 } 30302 30303 } else { 30304 SD_INFO(SD_LOG_IOERR, un, 30305 "sd_faultinjection_ioctl: arq null\n"); 30306 } 30307 30308 break; 30309 30310 case SDIOCPUSH: 30311 /* Push stored xb, pkt, un, and arq onto fifo */ 30312 sd_fault_injection_on = 0; 30313 30314 if (arg != NULL) { 30315 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30316 if (rval != -1 && 30317 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30318 un->sd_fi_fifo_end += i; 30319 } 30320 } else { 30321 SD_INFO(SD_LOG_IOERR, un, 30322 "sd_faultinjection_ioctl: push arg null\n"); 30323 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30324 un->sd_fi_fifo_end++; 30325 } 30326 } 30327 SD_INFO(SD_LOG_IOERR, un, 30328 "sd_faultinjection_ioctl: push to end=%d\n", 30329 un->sd_fi_fifo_end); 30330 break; 30331 30332 case SDIOCRETRIEVE: 30333 /* Return buffer of log from Injection session */ 30334 SD_INFO(SD_LOG_SDTEST, un, 30335 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30336 30337 sd_fault_injection_on = 0; 30338 30339 mutex_enter(&(un->un_fi_mutex)); 30340 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30341 un->sd_fi_buf_len+1, 0); 30342 mutex_exit(&(un->un_fi_mutex)); 30343 30344 if (rval == -1) { 30345 /* 30346 * arg is possibly invalid setting 30347 * it to NULL for return 30348 */ 30349 arg = NULL; 30350 } 30351 break; 30352 } 30353 30354 mutex_exit(SD_MUTEX(un)); 30355 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30356 " exit\n"); 30357 } 30358 30359 30360 /* 30361 * Function: sd_injection_log() 30362 * 30363 * Description: This routine adds buff to the already existing injection log 30364 * for retrieval via faultinjection_ioctl for use in fault 30365 * detection and recovery 30366 * 30367 * Arguments: buf - the string to add to the log 30368 */ 30369 30370 static void 30371 sd_injection_log(char *buf, struct sd_lun *un) 30372 { 30373 uint_t len; 30374 30375 ASSERT(un != NULL); 30376 ASSERT(buf != NULL); 30377 30378 mutex_enter(&(un->un_fi_mutex)); 30379 30380 len = min(strlen(buf), 255); 30381 /* Add logged value to Injection log to be returned later */ 30382 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30383 uint_t offset = strlen((char *)un->sd_fi_log); 30384 char *destp = (char *)un->sd_fi_log + offset; 30385 int i; 30386 for (i = 0; i < len; i++) { 30387 *destp++ = *buf++; 30388 } 30389 un->sd_fi_buf_len += len; 30390 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30391 } 30392 30393 mutex_exit(&(un->un_fi_mutex)); 30394 } 30395 30396 30397 /* 30398 * Function: sd_faultinjection() 30399 * 30400 * Description: This routine takes the pkt and changes its 30401 * content based on error injection scenerio. 30402 * 30403 * Arguments: pktp - packet to be changed 30404 */ 30405 30406 static void 30407 sd_faultinjection(struct scsi_pkt *pktp) 30408 { 30409 uint_t i; 30410 struct sd_fi_pkt *fi_pkt; 30411 struct sd_fi_xb *fi_xb; 30412 struct sd_fi_un *fi_un; 30413 struct sd_fi_arq *fi_arq; 30414 struct buf *bp; 30415 struct sd_xbuf *xb; 30416 struct sd_lun *un; 30417 30418 ASSERT(pktp != NULL); 30419 30420 /* pull bp xb and un from pktp */ 30421 bp = (struct buf *)pktp->pkt_private; 30422 xb = SD_GET_XBUF(bp); 30423 un = SD_GET_UN(bp); 30424 30425 ASSERT(un != NULL); 30426 30427 mutex_enter(SD_MUTEX(un)); 30428 30429 SD_TRACE(SD_LOG_SDTEST, un, 30430 "sd_faultinjection: entry Injection from sdintr\n"); 30431 30432 /* if injection is off return */ 30433 if (sd_fault_injection_on == 0 || 30434 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30435 mutex_exit(SD_MUTEX(un)); 30436 return; 30437 } 30438 30439 SD_INFO(SD_LOG_SDTEST, un, 30440 "sd_faultinjection: is working for copying\n"); 30441 30442 /* take next set off fifo */ 30443 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30444 30445 fi_pkt = un->sd_fi_fifo_pkt[i]; 30446 fi_xb = un->sd_fi_fifo_xb[i]; 30447 fi_un = un->sd_fi_fifo_un[i]; 30448 fi_arq = un->sd_fi_fifo_arq[i]; 30449 30450 30451 /* set variables accordingly */ 30452 /* set pkt if it was on fifo */ 30453 if (fi_pkt != NULL) { 30454 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30455 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30456 if (fi_pkt->pkt_cdbp != 0xff) 30457 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30458 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30459 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30460 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30461 30462 } 30463 /* set xb if it was on fifo */ 30464 if (fi_xb != NULL) { 30465 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30466 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30467 if (fi_xb->xb_retry_count != 0) 30468 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30469 SD_CONDSET(xb, xb, xb_victim_retry_count, 30470 "xb_victim_retry_count"); 30471 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30472 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30473 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30474 30475 /* copy in block data from sense */ 30476 /* 30477 * if (fi_xb->xb_sense_data[0] != -1) { 30478 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30479 * SENSE_LENGTH); 30480 * } 30481 */ 30482 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH); 30483 30484 /* copy in extended sense codes */ 30485 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30486 xb, es_code, "es_code"); 30487 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30488 xb, es_key, "es_key"); 30489 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30490 xb, es_add_code, "es_add_code"); 30491 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30492 xb, es_qual_code, "es_qual_code"); 30493 struct scsi_extended_sense *esp; 30494 esp = (struct scsi_extended_sense *)xb->xb_sense_data; 30495 esp->es_class = CLASS_EXTENDED_SENSE; 30496 } 30497 30498 /* set un if it was on fifo */ 30499 if (fi_un != NULL) { 30500 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 30501 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30502 SD_CONDSET(un, un, un_reset_retry_count, 30503 "un_reset_retry_count"); 30504 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30505 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30506 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30507 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30508 "un_f_allow_bus_device_reset"); 30509 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30510 30511 } 30512 30513 /* copy in auto request sense if it was on fifo */ 30514 if (fi_arq != NULL) { 30515 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30516 } 30517 30518 /* free structs */ 30519 if (un->sd_fi_fifo_pkt[i] != NULL) { 30520 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30521 } 30522 if (un->sd_fi_fifo_xb[i] != NULL) { 30523 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30524 } 30525 if (un->sd_fi_fifo_un[i] != NULL) { 30526 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30527 } 30528 if (un->sd_fi_fifo_arq[i] != NULL) { 30529 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30530 } 30531 30532 /* 30533 * kmem_free does not gurantee to set to NULL 30534 * since we uses these to determine if we set 30535 * values or not lets confirm they are always 30536 * NULL after free 30537 */ 30538 un->sd_fi_fifo_pkt[i] = NULL; 30539 un->sd_fi_fifo_un[i] = NULL; 30540 un->sd_fi_fifo_xb[i] = NULL; 30541 un->sd_fi_fifo_arq[i] = NULL; 30542 30543 un->sd_fi_fifo_start++; 30544 30545 mutex_exit(SD_MUTEX(un)); 30546 30547 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30548 } 30549 30550 #endif /* SD_FAULT_INJECTION */ 30551 30552 /* 30553 * This routine is invoked in sd_unit_attach(). Before calling it, the 30554 * properties in conf file should be processed already, and "hotpluggable" 30555 * property was processed also. 30556 * 30557 * The sd driver distinguishes 3 different type of devices: removable media, 30558 * non-removable media, and hotpluggable. Below the differences are defined: 30559 * 30560 * 1. Device ID 30561 * 30562 * The device ID of a device is used to identify this device. Refer to 30563 * ddi_devid_register(9F). 30564 * 30565 * For a non-removable media disk device which can provide 0x80 or 0x83 30566 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30567 * device ID is created to identify this device. For other non-removable 30568 * media devices, a default device ID is created only if this device has 30569 * at least 2 alter cylinders. Otherwise, this device has no devid. 30570 * 30571 * ------------------------------------------------------- 30572 * removable media hotpluggable | Can Have Device ID 30573 * ------------------------------------------------------- 30574 * false false | Yes 30575 * false true | Yes 30576 * true x | No 30577 * ------------------------------------------------------ 30578 * 30579 * 30580 * 2. SCSI group 4 commands 30581 * 30582 * In SCSI specs, only some commands in group 4 command set can use 30583 * 8-byte addresses that can be used to access >2TB storage spaces. 30584 * Other commands have no such capability. Without supporting group4, 30585 * it is impossible to make full use of storage spaces of a disk with 30586 * capacity larger than 2TB. 30587 * 30588 * ----------------------------------------------- 30589 * removable media hotpluggable LP64 | Group 30590 * ----------------------------------------------- 30591 * false false false | 1 30592 * false false true | 4 30593 * false true false | 1 30594 * false true true | 4 30595 * true x x | 5 30596 * ----------------------------------------------- 30597 * 30598 * 30599 * 3. Check for VTOC Label 30600 * 30601 * If a direct-access disk has no EFI label, sd will check if it has a 30602 * valid VTOC label. Now, sd also does that check for removable media 30603 * and hotpluggable devices. 30604 * 30605 * -------------------------------------------------------------- 30606 * Direct-Access removable media hotpluggable | Check Label 30607 * ------------------------------------------------------------- 30608 * false false false | No 30609 * false false true | No 30610 * false true false | Yes 30611 * false true true | Yes 30612 * true x x | Yes 30613 * -------------------------------------------------------------- 30614 * 30615 * 30616 * 4. Building default VTOC label 30617 * 30618 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30619 * If those devices have no valid VTOC label, sd(7d) will attempt to 30620 * create default VTOC for them. Currently sd creates default VTOC label 30621 * for all devices on x86 platform (VTOC_16), but only for removable 30622 * media devices on SPARC (VTOC_8). 30623 * 30624 * ----------------------------------------------------------- 30625 * removable media hotpluggable platform | Default Label 30626 * ----------------------------------------------------------- 30627 * false false sparc | No 30628 * false true x86 | Yes 30629 * false true sparc | Yes 30630 * true x x | Yes 30631 * ---------------------------------------------------------- 30632 * 30633 * 30634 * 5. Supported blocksizes of target devices 30635 * 30636 * Sd supports non-512-byte blocksize for removable media devices only. 30637 * For other devices, only 512-byte blocksize is supported. This may be 30638 * changed in near future because some RAID devices require non-512-byte 30639 * blocksize 30640 * 30641 * ----------------------------------------------------------- 30642 * removable media hotpluggable | non-512-byte blocksize 30643 * ----------------------------------------------------------- 30644 * false false | No 30645 * false true | No 30646 * true x | Yes 30647 * ----------------------------------------------------------- 30648 * 30649 * 30650 * 6. Automatic mount & unmount 30651 * 30652 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30653 * if a device is removable media device. It return 1 for removable media 30654 * devices, and 0 for others. 30655 * 30656 * The automatic mounting subsystem should distinguish between the types 30657 * of devices and apply automounting policies to each. 30658 * 30659 * 30660 * 7. fdisk partition management 30661 * 30662 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30663 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30664 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30665 * fdisk partitions on both x86 and SPARC platform. 30666 * 30667 * ----------------------------------------------------------- 30668 * platform removable media USB/1394 | fdisk supported 30669 * ----------------------------------------------------------- 30670 * x86 X X | true 30671 * ------------------------------------------------------------ 30672 * sparc X X | false 30673 * ------------------------------------------------------------ 30674 * 30675 * 30676 * 8. MBOOT/MBR 30677 * 30678 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30679 * read/write mboot for removable media devices on sparc platform. 30680 * 30681 * ----------------------------------------------------------- 30682 * platform removable media USB/1394 | mboot supported 30683 * ----------------------------------------------------------- 30684 * x86 X X | true 30685 * ------------------------------------------------------------ 30686 * sparc false false | false 30687 * sparc false true | true 30688 * sparc true false | true 30689 * sparc true true | true 30690 * ------------------------------------------------------------ 30691 * 30692 * 30693 * 9. error handling during opening device 30694 * 30695 * If failed to open a disk device, an errno is returned. For some kinds 30696 * of errors, different errno is returned depending on if this device is 30697 * a removable media device. This brings USB/1394 hard disks in line with 30698 * expected hard disk behavior. It is not expected that this breaks any 30699 * application. 30700 * 30701 * ------------------------------------------------------ 30702 * removable media hotpluggable | errno 30703 * ------------------------------------------------------ 30704 * false false | EIO 30705 * false true | EIO 30706 * true x | ENXIO 30707 * ------------------------------------------------------ 30708 * 30709 * 30710 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30711 * 30712 * These IOCTLs are applicable only to removable media devices. 30713 * 30714 * ----------------------------------------------------------- 30715 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30716 * ----------------------------------------------------------- 30717 * false false | No 30718 * false true | No 30719 * true x | Yes 30720 * ----------------------------------------------------------- 30721 * 30722 * 30723 * 12. Kstats for partitions 30724 * 30725 * sd creates partition kstat for non-removable media devices. USB and 30726 * Firewire hard disks now have partition kstats 30727 * 30728 * ------------------------------------------------------ 30729 * removable media hotpluggable | kstat 30730 * ------------------------------------------------------ 30731 * false false | Yes 30732 * false true | Yes 30733 * true x | No 30734 * ------------------------------------------------------ 30735 * 30736 * 30737 * 13. Removable media & hotpluggable properties 30738 * 30739 * Sd driver creates a "removable-media" property for removable media 30740 * devices. Parent nexus drivers create a "hotpluggable" property if 30741 * it supports hotplugging. 30742 * 30743 * --------------------------------------------------------------------- 30744 * removable media hotpluggable | "removable-media" " hotpluggable" 30745 * --------------------------------------------------------------------- 30746 * false false | No No 30747 * false true | No Yes 30748 * true false | Yes No 30749 * true true | Yes Yes 30750 * --------------------------------------------------------------------- 30751 * 30752 * 30753 * 14. Power Management 30754 * 30755 * sd only power manages removable media devices or devices that support 30756 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30757 * 30758 * A parent nexus that supports hotplugging can also set "pm-capable" 30759 * if the disk can be power managed. 30760 * 30761 * ------------------------------------------------------------ 30762 * removable media hotpluggable pm-capable | power manage 30763 * ------------------------------------------------------------ 30764 * false false false | No 30765 * false false true | Yes 30766 * false true false | No 30767 * false true true | Yes 30768 * true x x | Yes 30769 * ------------------------------------------------------------ 30770 * 30771 * USB and firewire hard disks can now be power managed independently 30772 * of the framebuffer 30773 * 30774 * 30775 * 15. Support for USB disks with capacity larger than 1TB 30776 * 30777 * Currently, sd doesn't permit a fixed disk device with capacity 30778 * larger than 1TB to be used in a 32-bit operating system environment. 30779 * However, sd doesn't do that for removable media devices. Instead, it 30780 * assumes that removable media devices cannot have a capacity larger 30781 * than 1TB. Therefore, using those devices on 32-bit system is partially 30782 * supported, which can cause some unexpected results. 30783 * 30784 * --------------------------------------------------------------------- 30785 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30786 * --------------------------------------------------------------------- 30787 * false false | true | no 30788 * false true | true | no 30789 * true false | true | Yes 30790 * true true | true | Yes 30791 * --------------------------------------------------------------------- 30792 * 30793 * 30794 * 16. Check write-protection at open time 30795 * 30796 * When a removable media device is being opened for writing without NDELAY 30797 * flag, sd will check if this device is writable. If attempting to open 30798 * without NDELAY flag a write-protected device, this operation will abort. 30799 * 30800 * ------------------------------------------------------------ 30801 * removable media USB/1394 | WP Check 30802 * ------------------------------------------------------------ 30803 * false false | No 30804 * false true | No 30805 * true false | Yes 30806 * true true | Yes 30807 * ------------------------------------------------------------ 30808 * 30809 * 30810 * 17. syslog when corrupted VTOC is encountered 30811 * 30812 * Currently, if an invalid VTOC is encountered, sd only print syslog 30813 * for fixed SCSI disks. 30814 * ------------------------------------------------------------ 30815 * removable media USB/1394 | print syslog 30816 * ------------------------------------------------------------ 30817 * false false | Yes 30818 * false true | No 30819 * true false | No 30820 * true true | No 30821 * ------------------------------------------------------------ 30822 */ 30823 static void 30824 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30825 { 30826 int pm_cap; 30827 30828 ASSERT(un->un_sd); 30829 ASSERT(un->un_sd->sd_inq); 30830 30831 /* 30832 * Enable SYNC CACHE support for all devices. 30833 */ 30834 un->un_f_sync_cache_supported = TRUE; 30835 30836 /* 30837 * Set the sync cache required flag to false. 30838 * This would ensure that there is no SYNC CACHE 30839 * sent when there are no writes 30840 */ 30841 un->un_f_sync_cache_required = FALSE; 30842 30843 if (un->un_sd->sd_inq->inq_rmb) { 30844 /* 30845 * The media of this device is removable. And for this kind 30846 * of devices, it is possible to change medium after opening 30847 * devices. Thus we should support this operation. 30848 */ 30849 un->un_f_has_removable_media = TRUE; 30850 30851 /* 30852 * support non-512-byte blocksize of removable media devices 30853 */ 30854 un->un_f_non_devbsize_supported = TRUE; 30855 30856 /* 30857 * Assume that all removable media devices support DOOR_LOCK 30858 */ 30859 un->un_f_doorlock_supported = TRUE; 30860 30861 /* 30862 * For a removable media device, it is possible to be opened 30863 * with NDELAY flag when there is no media in drive, in this 30864 * case we don't care if device is writable. But if without 30865 * NDELAY flag, we need to check if media is write-protected. 30866 */ 30867 un->un_f_chk_wp_open = TRUE; 30868 30869 /* 30870 * need to start a SCSI watch thread to monitor media state, 30871 * when media is being inserted or ejected, notify syseventd. 30872 */ 30873 un->un_f_monitor_media_state = TRUE; 30874 30875 /* 30876 * Some devices don't support START_STOP_UNIT command. 30877 * Therefore, we'd better check if a device supports it 30878 * before sending it. 30879 */ 30880 un->un_f_check_start_stop = TRUE; 30881 30882 /* 30883 * support eject media ioctl: 30884 * FDEJECT, DKIOCEJECT, CDROMEJECT 30885 */ 30886 un->un_f_eject_media_supported = TRUE; 30887 30888 /* 30889 * Because many removable-media devices don't support 30890 * LOG_SENSE, we couldn't use this command to check if 30891 * a removable media device support power-management. 30892 * We assume that they support power-management via 30893 * START_STOP_UNIT command and can be spun up and down 30894 * without limitations. 30895 */ 30896 un->un_f_pm_supported = TRUE; 30897 30898 /* 30899 * Need to create a zero length (Boolean) property 30900 * removable-media for the removable media devices. 30901 * Note that the return value of the property is not being 30902 * checked, since if unable to create the property 30903 * then do not want the attach to fail altogether. Consistent 30904 * with other property creation in attach. 30905 */ 30906 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 30907 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 30908 30909 } else { 30910 /* 30911 * create device ID for device 30912 */ 30913 un->un_f_devid_supported = TRUE; 30914 30915 /* 30916 * Spin up non-removable-media devices once it is attached 30917 */ 30918 un->un_f_attach_spinup = TRUE; 30919 30920 /* 30921 * According to SCSI specification, Sense data has two kinds of 30922 * format: fixed format, and descriptor format. At present, we 30923 * don't support descriptor format sense data for removable 30924 * media. 30925 */ 30926 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 30927 un->un_f_descr_format_supported = TRUE; 30928 } 30929 30930 /* 30931 * kstats are created only for non-removable media devices. 30932 * 30933 * Set this in sd.conf to 0 in order to disable kstats. The 30934 * default is 1, so they are enabled by default. 30935 */ 30936 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 30937 SD_DEVINFO(un), DDI_PROP_DONTPASS, 30938 "enable-partition-kstats", 1)); 30939 30940 /* 30941 * Check if HBA has set the "pm-capable" property. 30942 * If "pm-capable" exists and is non-zero then we can 30943 * power manage the device without checking the start/stop 30944 * cycle count log sense page. 30945 * 30946 * If "pm-capable" exists and is set to be false (0), 30947 * then we should not power manage the device. 30948 * 30949 * If "pm-capable" doesn't exist then pm_cap will 30950 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 30951 * sd will check the start/stop cycle count log sense page 30952 * and power manage the device if the cycle count limit has 30953 * not been exceeded. 30954 */ 30955 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 30956 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 30957 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) { 30958 un->un_f_log_sense_supported = TRUE; 30959 if (!un->un_f_power_condition_disabled && 30960 SD_INQUIRY(un)->inq_ansi == 6) { 30961 un->un_f_power_condition_supported = TRUE; 30962 } 30963 } else { 30964 /* 30965 * pm-capable property exists. 30966 * 30967 * Convert "TRUE" values for pm_cap to 30968 * SD_PM_CAPABLE_IS_TRUE to make it easier to check 30969 * later. "TRUE" values are any values defined in 30970 * inquiry.h. 30971 */ 30972 if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) { 30973 un->un_f_log_sense_supported = FALSE; 30974 } else { 30975 /* SD_PM_CAPABLE_IS_TRUE case */ 30976 un->un_f_pm_supported = TRUE; 30977 if (!un->un_f_power_condition_disabled && 30978 SD_PM_CAPABLE_IS_SPC_4(pm_cap)) { 30979 un->un_f_power_condition_supported = 30980 TRUE; 30981 } 30982 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) { 30983 un->un_f_log_sense_supported = TRUE; 30984 un->un_f_pm_log_sense_smart = 30985 SD_PM_CAP_SMART_LOG(pm_cap); 30986 } 30987 } 30988 30989 SD_INFO(SD_LOG_ATTACH_DETACH, un, 30990 "sd_unit_attach: un:0x%p pm-capable " 30991 "property set to %d.\n", un, un->un_f_pm_supported); 30992 } 30993 } 30994 30995 if (un->un_f_is_hotpluggable) { 30996 30997 /* 30998 * Have to watch hotpluggable devices as well, since 30999 * that's the only way for userland applications to 31000 * detect hot removal while device is busy/mounted. 31001 */ 31002 un->un_f_monitor_media_state = TRUE; 31003 31004 un->un_f_check_start_stop = TRUE; 31005 31006 } 31007 } 31008 31009 /* 31010 * sd_tg_rdwr: 31011 * Provides rdwr access for cmlb via sd_tgops. The start_block is 31012 * in sys block size, req_length in bytes. 31013 * 31014 */ 31015 static int 31016 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 31017 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 31018 { 31019 struct sd_lun *un; 31020 int path_flag = (int)(uintptr_t)tg_cookie; 31021 char *dkl = NULL; 31022 diskaddr_t real_addr = start_block; 31023 diskaddr_t first_byte, end_block; 31024 31025 size_t buffer_size = reqlength; 31026 int rval = 0; 31027 diskaddr_t cap; 31028 uint32_t lbasize; 31029 sd_ssc_t *ssc; 31030 31031 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 31032 if (un == NULL) 31033 return (ENXIO); 31034 31035 if (cmd != TG_READ && cmd != TG_WRITE) 31036 return (EINVAL); 31037 31038 ssc = sd_ssc_init(un); 31039 mutex_enter(SD_MUTEX(un)); 31040 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 31041 mutex_exit(SD_MUTEX(un)); 31042 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 31043 &lbasize, path_flag); 31044 if (rval != 0) 31045 goto done1; 31046 mutex_enter(SD_MUTEX(un)); 31047 sd_update_block_info(un, lbasize, cap); 31048 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 31049 mutex_exit(SD_MUTEX(un)); 31050 rval = EIO; 31051 goto done; 31052 } 31053 } 31054 31055 if (NOT_DEVBSIZE(un)) { 31056 /* 31057 * sys_blocksize != tgt_blocksize, need to re-adjust 31058 * blkno and save the index to beginning of dk_label 31059 */ 31060 first_byte = SD_SYSBLOCKS2BYTES(start_block); 31061 real_addr = first_byte / un->un_tgt_blocksize; 31062 31063 end_block = (first_byte + reqlength + 31064 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 31065 31066 /* round up buffer size to multiple of target block size */ 31067 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 31068 31069 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 31070 "label_addr: 0x%x allocation size: 0x%x\n", 31071 real_addr, buffer_size); 31072 31073 if (((first_byte % un->un_tgt_blocksize) != 0) || 31074 (reqlength % un->un_tgt_blocksize) != 0) 31075 /* the request is not aligned */ 31076 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 31077 } 31078 31079 /* 31080 * The MMC standard allows READ CAPACITY to be 31081 * inaccurate by a bounded amount (in the interest of 31082 * response latency). As a result, failed READs are 31083 * commonplace (due to the reading of metadata and not 31084 * data). Depending on the per-Vendor/drive Sense data, 31085 * the failed READ can cause many (unnecessary) retries. 31086 */ 31087 31088 if (ISCD(un) && (cmd == TG_READ) && 31089 (un->un_f_blockcount_is_valid == TRUE) && 31090 ((start_block == (un->un_blockcount - 1))|| 31091 (start_block == (un->un_blockcount - 2)))) { 31092 path_flag = SD_PATH_DIRECT_PRIORITY; 31093 } 31094 31095 mutex_exit(SD_MUTEX(un)); 31096 if (cmd == TG_READ) { 31097 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr, 31098 buffer_size, real_addr, path_flag); 31099 if (dkl != NULL) 31100 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 31101 real_addr), bufaddr, reqlength); 31102 } else { 31103 if (dkl) { 31104 rval = sd_send_scsi_READ(ssc, dkl, buffer_size, 31105 real_addr, path_flag); 31106 if (rval) { 31107 goto done1; 31108 } 31109 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 31110 real_addr), reqlength); 31111 } 31112 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr, 31113 buffer_size, real_addr, path_flag); 31114 } 31115 31116 done1: 31117 if (dkl != NULL) 31118 kmem_free(dkl, buffer_size); 31119 31120 if (rval != 0) { 31121 if (rval == EIO) 31122 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 31123 else 31124 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31125 } 31126 done: 31127 sd_ssc_fini(ssc); 31128 return (rval); 31129 } 31130 31131 31132 static int 31133 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 31134 { 31135 31136 struct sd_lun *un; 31137 diskaddr_t cap; 31138 uint32_t lbasize; 31139 int path_flag = (int)(uintptr_t)tg_cookie; 31140 int ret = 0; 31141 31142 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 31143 if (un == NULL) 31144 return (ENXIO); 31145 31146 switch (cmd) { 31147 case TG_GETPHYGEOM: 31148 case TG_GETVIRTGEOM: 31149 case TG_GETCAPACITY: 31150 case TG_GETBLOCKSIZE: 31151 mutex_enter(SD_MUTEX(un)); 31152 31153 if ((un->un_f_blockcount_is_valid == TRUE) && 31154 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 31155 cap = un->un_blockcount; 31156 lbasize = un->un_tgt_blocksize; 31157 mutex_exit(SD_MUTEX(un)); 31158 } else { 31159 sd_ssc_t *ssc; 31160 mutex_exit(SD_MUTEX(un)); 31161 ssc = sd_ssc_init(un); 31162 ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 31163 &lbasize, path_flag); 31164 if (ret != 0) { 31165 if (ret == EIO) 31166 sd_ssc_assessment(ssc, 31167 SD_FMT_STATUS_CHECK); 31168 else 31169 sd_ssc_assessment(ssc, 31170 SD_FMT_IGNORE); 31171 sd_ssc_fini(ssc); 31172 return (ret); 31173 } 31174 sd_ssc_fini(ssc); 31175 mutex_enter(SD_MUTEX(un)); 31176 sd_update_block_info(un, lbasize, cap); 31177 if ((un->un_f_blockcount_is_valid == FALSE) || 31178 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 31179 mutex_exit(SD_MUTEX(un)); 31180 return (EIO); 31181 } 31182 mutex_exit(SD_MUTEX(un)); 31183 } 31184 31185 if (cmd == TG_GETCAPACITY) { 31186 *(diskaddr_t *)arg = cap; 31187 return (0); 31188 } 31189 31190 if (cmd == TG_GETBLOCKSIZE) { 31191 *(uint32_t *)arg = lbasize; 31192 return (0); 31193 } 31194 31195 if (cmd == TG_GETPHYGEOM) 31196 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 31197 cap, lbasize, path_flag); 31198 else 31199 /* TG_GETVIRTGEOM */ 31200 ret = sd_get_virtual_geometry(un, 31201 (cmlb_geom_t *)arg, cap, lbasize); 31202 31203 return (ret); 31204 31205 case TG_GETATTR: 31206 mutex_enter(SD_MUTEX(un)); 31207 ((tg_attribute_t *)arg)->media_is_writable = 31208 un->un_f_mmc_writable_media; 31209 ((tg_attribute_t *)arg)->media_is_solid_state = 31210 un->un_f_is_solid_state; 31211 mutex_exit(SD_MUTEX(un)); 31212 return (0); 31213 default: 31214 return (ENOTTY); 31215 31216 } 31217 } 31218 31219 /* 31220 * Function: sd_ssc_ereport_post 31221 * 31222 * Description: Will be called when SD driver need to post an ereport. 31223 * 31224 * Context: Kernel thread or interrupt context. 31225 */ 31226 31227 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown" 31228 31229 static void 31230 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess) 31231 { 31232 int uscsi_path_instance = 0; 31233 uchar_t uscsi_pkt_reason; 31234 uint32_t uscsi_pkt_state; 31235 uint32_t uscsi_pkt_statistics; 31236 uint64_t uscsi_ena; 31237 uchar_t op_code; 31238 uint8_t *sensep; 31239 union scsi_cdb *cdbp; 31240 uint_t cdblen = 0; 31241 uint_t senlen = 0; 31242 struct sd_lun *un; 31243 dev_info_t *dip; 31244 char *devid; 31245 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON | 31246 SSC_FLAGS_INVALID_STATUS | 31247 SSC_FLAGS_INVALID_SENSE | 31248 SSC_FLAGS_INVALID_DATA; 31249 char assessment[16]; 31250 31251 ASSERT(ssc != NULL); 31252 ASSERT(ssc->ssc_uscsi_cmd != NULL); 31253 ASSERT(ssc->ssc_uscsi_info != NULL); 31254 31255 un = ssc->ssc_un; 31256 ASSERT(un != NULL); 31257 31258 dip = un->un_sd->sd_dev; 31259 31260 /* 31261 * Get the devid: 31262 * devid will only be passed to non-transport error reports. 31263 */ 31264 devid = DEVI(dip)->devi_devid_str; 31265 31266 /* 31267 * If we are syncing or dumping, the command will not be executed 31268 * so we bypass this situation. 31269 */ 31270 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 31271 (un->un_state == SD_STATE_DUMPING)) 31272 return; 31273 31274 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason; 31275 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance; 31276 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state; 31277 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics; 31278 uscsi_ena = ssc->ssc_uscsi_info->ui_ena; 31279 31280 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 31281 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb; 31282 31283 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */ 31284 if (cdbp == NULL) { 31285 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 31286 "sd_ssc_ereport_post meet empty cdb\n"); 31287 return; 31288 } 31289 31290 op_code = cdbp->scc_cmd; 31291 31292 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen; 31293 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 31294 ssc->ssc_uscsi_cmd->uscsi_rqresid); 31295 31296 if (senlen > 0) 31297 ASSERT(sensep != NULL); 31298 31299 /* 31300 * Initialize drv_assess to corresponding values. 31301 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending 31302 * on the sense-key returned back. 31303 */ 31304 switch (drv_assess) { 31305 case SD_FM_DRV_RECOVERY: 31306 (void) sprintf(assessment, "%s", "recovered"); 31307 break; 31308 case SD_FM_DRV_RETRY: 31309 (void) sprintf(assessment, "%s", "retry"); 31310 break; 31311 case SD_FM_DRV_NOTICE: 31312 (void) sprintf(assessment, "%s", "info"); 31313 break; 31314 case SD_FM_DRV_FATAL: 31315 default: 31316 (void) sprintf(assessment, "%s", "unknown"); 31317 } 31318 /* 31319 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered 31320 * command, we will post ereport.io.scsi.cmd.disk.recovered. 31321 * driver-assessment will always be "recovered" here. 31322 */ 31323 if (drv_assess == SD_FM_DRV_RECOVERY) { 31324 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31325 "cmd.disk.recovered", uscsi_ena, devid, NULL, 31326 DDI_NOSLEEP, NULL, 31327 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31328 DEVID_IF_KNOWN(devid), 31329 "driver-assessment", DATA_TYPE_STRING, assessment, 31330 "op-code", DATA_TYPE_UINT8, op_code, 31331 "cdb", DATA_TYPE_UINT8_ARRAY, 31332 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31333 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31334 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31335 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31336 NULL); 31337 return; 31338 } 31339 31340 /* 31341 * If there is un-expected/un-decodable data, we should post 31342 * ereport.io.scsi.cmd.disk.dev.uderr. 31343 * driver-assessment will be set based on parameter drv_assess. 31344 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back. 31345 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered. 31346 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered. 31347 * SSC_FLAGS_INVALID_DATA - invalid data sent back. 31348 */ 31349 if (ssc->ssc_flags & ssc_invalid_flags) { 31350 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) { 31351 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31352 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid, 31353 NULL, DDI_NOSLEEP, NULL, 31354 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31355 DEVID_IF_KNOWN(devid), 31356 "driver-assessment", DATA_TYPE_STRING, 31357 drv_assess == SD_FM_DRV_FATAL ? 31358 "fail" : assessment, 31359 "op-code", DATA_TYPE_UINT8, op_code, 31360 "cdb", DATA_TYPE_UINT8_ARRAY, 31361 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31362 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31363 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31364 "pkt-stats", DATA_TYPE_UINT32, 31365 uscsi_pkt_statistics, 31366 "stat-code", DATA_TYPE_UINT8, 31367 ssc->ssc_uscsi_cmd->uscsi_status, 31368 "un-decode-info", DATA_TYPE_STRING, 31369 ssc->ssc_info, 31370 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31371 senlen, sensep, 31372 NULL); 31373 } else { 31374 /* 31375 * For other type of invalid data, the 31376 * un-decode-value field would be empty because the 31377 * un-decodable content could be seen from upper 31378 * level payload or inside un-decode-info. 31379 */ 31380 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31381 NULL, 31382 "cmd.disk.dev.uderr", uscsi_ena, devid, 31383 NULL, DDI_NOSLEEP, NULL, 31384 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31385 DEVID_IF_KNOWN(devid), 31386 "driver-assessment", DATA_TYPE_STRING, 31387 drv_assess == SD_FM_DRV_FATAL ? 31388 "fail" : assessment, 31389 "op-code", DATA_TYPE_UINT8, op_code, 31390 "cdb", DATA_TYPE_UINT8_ARRAY, 31391 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31392 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31393 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31394 "pkt-stats", DATA_TYPE_UINT32, 31395 uscsi_pkt_statistics, 31396 "stat-code", DATA_TYPE_UINT8, 31397 ssc->ssc_uscsi_cmd->uscsi_status, 31398 "un-decode-info", DATA_TYPE_STRING, 31399 ssc->ssc_info, 31400 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31401 0, NULL, 31402 NULL); 31403 } 31404 ssc->ssc_flags &= ~ssc_invalid_flags; 31405 return; 31406 } 31407 31408 if (uscsi_pkt_reason != CMD_CMPLT || 31409 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) { 31410 /* 31411 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was 31412 * set inside sd_start_cmds due to errors(bad packet or 31413 * fatal transport error), we should take it as a 31414 * transport error, so we post ereport.io.scsi.cmd.disk.tran. 31415 * driver-assessment will be set based on drv_assess. 31416 * We will set devid to NULL because it is a transport 31417 * error. 31418 */ 31419 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT) 31420 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT; 31421 31422 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31423 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL, 31424 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31425 DEVID_IF_KNOWN(devid), 31426 "driver-assessment", DATA_TYPE_STRING, 31427 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31428 "op-code", DATA_TYPE_UINT8, op_code, 31429 "cdb", DATA_TYPE_UINT8_ARRAY, 31430 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31431 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31432 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state, 31433 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31434 NULL); 31435 } else { 31436 /* 31437 * If we got here, we have a completed command, and we need 31438 * to further investigate the sense data to see what kind 31439 * of ereport we should post. 31440 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr 31441 * if sense-key == 0x3. 31442 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise. 31443 * driver-assessment will be set based on the parameter 31444 * drv_assess. 31445 */ 31446 if (senlen > 0) { 31447 /* 31448 * Here we have sense data available. 31449 */ 31450 uint8_t sense_key; 31451 sense_key = scsi_sense_key(sensep); 31452 if (sense_key == 0x3) { 31453 /* 31454 * sense-key == 0x3(medium error), 31455 * driver-assessment should be "fatal" if 31456 * drv_assess is SD_FM_DRV_FATAL. 31457 */ 31458 scsi_fm_ereport_post(un->un_sd, 31459 uscsi_path_instance, NULL, 31460 "cmd.disk.dev.rqs.merr", 31461 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL, 31462 FM_VERSION, DATA_TYPE_UINT8, 31463 FM_EREPORT_VERS0, 31464 DEVID_IF_KNOWN(devid), 31465 "driver-assessment", 31466 DATA_TYPE_STRING, 31467 drv_assess == SD_FM_DRV_FATAL ? 31468 "fatal" : assessment, 31469 "op-code", 31470 DATA_TYPE_UINT8, op_code, 31471 "cdb", 31472 DATA_TYPE_UINT8_ARRAY, cdblen, 31473 ssc->ssc_uscsi_cmd->uscsi_cdb, 31474 "pkt-reason", 31475 DATA_TYPE_UINT8, uscsi_pkt_reason, 31476 "pkt-state", 31477 DATA_TYPE_UINT8, uscsi_pkt_state, 31478 "pkt-stats", 31479 DATA_TYPE_UINT32, 31480 uscsi_pkt_statistics, 31481 "stat-code", 31482 DATA_TYPE_UINT8, 31483 ssc->ssc_uscsi_cmd->uscsi_status, 31484 "key", 31485 DATA_TYPE_UINT8, 31486 scsi_sense_key(sensep), 31487 "asc", 31488 DATA_TYPE_UINT8, 31489 scsi_sense_asc(sensep), 31490 "ascq", 31491 DATA_TYPE_UINT8, 31492 scsi_sense_ascq(sensep), 31493 "sense-data", 31494 DATA_TYPE_UINT8_ARRAY, 31495 senlen, sensep, 31496 "lba", 31497 DATA_TYPE_UINT64, 31498 ssc->ssc_uscsi_info->ui_lba, 31499 NULL); 31500 } else { 31501 /* 31502 * if sense-key == 0x4(hardware 31503 * error), driver-assessment should 31504 * be "fatal" if drv_assess is 31505 * SD_FM_DRV_FATAL. 31506 */ 31507 scsi_fm_ereport_post(un->un_sd, 31508 uscsi_path_instance, NULL, 31509 "cmd.disk.dev.rqs.derr", 31510 uscsi_ena, devid, 31511 NULL, DDI_NOSLEEP, NULL, 31512 FM_VERSION, 31513 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31514 DEVID_IF_KNOWN(devid), 31515 "driver-assessment", 31516 DATA_TYPE_STRING, 31517 drv_assess == SD_FM_DRV_FATAL ? 31518 (sense_key == 0x4 ? 31519 "fatal" : "fail") : assessment, 31520 "op-code", 31521 DATA_TYPE_UINT8, op_code, 31522 "cdb", 31523 DATA_TYPE_UINT8_ARRAY, cdblen, 31524 ssc->ssc_uscsi_cmd->uscsi_cdb, 31525 "pkt-reason", 31526 DATA_TYPE_UINT8, uscsi_pkt_reason, 31527 "pkt-state", 31528 DATA_TYPE_UINT8, uscsi_pkt_state, 31529 "pkt-stats", 31530 DATA_TYPE_UINT32, 31531 uscsi_pkt_statistics, 31532 "stat-code", 31533 DATA_TYPE_UINT8, 31534 ssc->ssc_uscsi_cmd->uscsi_status, 31535 "key", 31536 DATA_TYPE_UINT8, 31537 scsi_sense_key(sensep), 31538 "asc", 31539 DATA_TYPE_UINT8, 31540 scsi_sense_asc(sensep), 31541 "ascq", 31542 DATA_TYPE_UINT8, 31543 scsi_sense_ascq(sensep), 31544 "sense-data", 31545 DATA_TYPE_UINT8_ARRAY, 31546 senlen, sensep, 31547 NULL); 31548 } 31549 } else { 31550 /* 31551 * For stat_code == STATUS_GOOD, this is not a 31552 * hardware error. 31553 */ 31554 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) 31555 return; 31556 31557 /* 31558 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the 31559 * stat-code but with sense data unavailable. 31560 * driver-assessment will be set based on parameter 31561 * drv_assess. 31562 */ 31563 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31564 NULL, 31565 "cmd.disk.dev.serr", uscsi_ena, 31566 devid, NULL, DDI_NOSLEEP, NULL, 31567 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31568 DEVID_IF_KNOWN(devid), 31569 "driver-assessment", DATA_TYPE_STRING, 31570 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31571 "op-code", DATA_TYPE_UINT8, op_code, 31572 "cdb", 31573 DATA_TYPE_UINT8_ARRAY, 31574 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31575 "pkt-reason", 31576 DATA_TYPE_UINT8, uscsi_pkt_reason, 31577 "pkt-state", 31578 DATA_TYPE_UINT8, uscsi_pkt_state, 31579 "pkt-stats", 31580 DATA_TYPE_UINT32, uscsi_pkt_statistics, 31581 "stat-code", 31582 DATA_TYPE_UINT8, 31583 ssc->ssc_uscsi_cmd->uscsi_status, 31584 NULL); 31585 } 31586 } 31587 } 31588 31589 /* 31590 * Function: sd_ssc_extract_info 31591 * 31592 * Description: Extract information available to help generate ereport. 31593 * 31594 * Context: Kernel thread or interrupt context. 31595 */ 31596 static void 31597 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp, 31598 struct buf *bp, struct sd_xbuf *xp) 31599 { 31600 size_t senlen = 0; 31601 union scsi_cdb *cdbp; 31602 int path_instance; 31603 /* 31604 * Need scsi_cdb_size array to determine the cdb length. 31605 */ 31606 extern uchar_t scsi_cdb_size[]; 31607 31608 ASSERT(un != NULL); 31609 ASSERT(pktp != NULL); 31610 ASSERT(bp != NULL); 31611 ASSERT(xp != NULL); 31612 ASSERT(ssc != NULL); 31613 ASSERT(mutex_owned(SD_MUTEX(un))); 31614 31615 /* 31616 * Transfer the cdb buffer pointer here. 31617 */ 31618 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 31619 31620 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 31621 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp; 31622 31623 /* 31624 * Transfer the sense data buffer pointer if sense data is available, 31625 * calculate the sense data length first. 31626 */ 31627 if ((xp->xb_sense_state & STATE_XARQ_DONE) || 31628 (xp->xb_sense_state & STATE_ARQ_DONE)) { 31629 /* 31630 * For arq case, we will enter here. 31631 */ 31632 if (xp->xb_sense_state & STATE_XARQ_DONE) { 31633 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid; 31634 } else { 31635 senlen = SENSE_LENGTH; 31636 } 31637 } else { 31638 /* 31639 * For non-arq case, we will enter this branch. 31640 */ 31641 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK && 31642 (xp->xb_sense_state & STATE_XFERRED_DATA)) { 31643 senlen = SENSE_LENGTH - xp->xb_sense_resid; 31644 } 31645 31646 } 31647 31648 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff); 31649 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0; 31650 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data; 31651 31652 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 31653 31654 /* 31655 * Only transfer path_instance when scsi_pkt was properly allocated. 31656 */ 31657 path_instance = pktp->pkt_path_instance; 31658 if (scsi_pkt_allocated_correctly(pktp) && path_instance) 31659 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance; 31660 else 31661 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0; 31662 31663 /* 31664 * Copy in the other fields we may need when posting ereport. 31665 */ 31666 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason; 31667 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state; 31668 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics; 31669 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 31670 31671 /* 31672 * For partially read/write command, we will not create ena 31673 * in case of a successful command be reconized as recovered. 31674 */ 31675 if ((pktp->pkt_reason == CMD_CMPLT) && 31676 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) && 31677 (senlen == 0)) { 31678 return; 31679 } 31680 31681 /* 31682 * To associate ereports of a single command execution flow, we 31683 * need a shared ena for a specific command. 31684 */ 31685 if (xp->xb_ena == 0) 31686 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1); 31687 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena; 31688 } 31689 31690 31691 /* 31692 * Function: sd_check_solid_state 31693 * 31694 * Description: Query the optional INQUIRY VPD page 0xb1. If the device 31695 * supports VPD page 0xb1, sd examines the MEDIUM ROTATION 31696 * RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the 31697 * device is a solid state drive. 31698 * 31699 * Context: Kernel thread or interrupt context. 31700 */ 31701 31702 static void 31703 sd_check_solid_state(sd_ssc_t *ssc) 31704 { 31705 int rval = 0; 31706 uchar_t *inqb1 = NULL; 31707 size_t inqb1_len = MAX_INQUIRY_SIZE; 31708 size_t inqb1_resid = 0; 31709 struct sd_lun *un; 31710 31711 ASSERT(ssc != NULL); 31712 un = ssc->ssc_un; 31713 ASSERT(un != NULL); 31714 ASSERT(!mutex_owned(SD_MUTEX(un))); 31715 31716 mutex_enter(SD_MUTEX(un)); 31717 un->un_f_is_solid_state = FALSE; 31718 31719 if (ISCD(un)) { 31720 mutex_exit(SD_MUTEX(un)); 31721 return; 31722 } 31723 31724 if (sd_check_vpd_page_support(ssc) == 0 && 31725 un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) { 31726 mutex_exit(SD_MUTEX(un)); 31727 /* collect page b1 data */ 31728 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP); 31729 31730 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len, 31731 0x01, 0xB1, &inqb1_resid); 31732 31733 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) { 31734 SD_TRACE(SD_LOG_COMMON, un, 31735 "sd_check_solid_state: \ 31736 successfully get VPD page: %x \ 31737 PAGE LENGTH: %x BYTE 4: %x \ 31738 BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4], 31739 inqb1[5]); 31740 31741 mutex_enter(SD_MUTEX(un)); 31742 /* 31743 * Check the MEDIUM ROTATION RATE. If it is set 31744 * to 1, the device is a solid state drive. 31745 */ 31746 if (inqb1[4] == 0 && inqb1[5] == 1) { 31747 un->un_f_is_solid_state = TRUE; 31748 } 31749 mutex_exit(SD_MUTEX(un)); 31750 } else if (rval != 0) { 31751 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31752 } 31753 31754 kmem_free(inqb1, inqb1_len); 31755 } else { 31756 mutex_exit(SD_MUTEX(un)); 31757 } 31758 } 31759 31760 /* 31761 * Function: sd_check_emulation_mode 31762 * 31763 * Description: Check whether the SSD is at emulation mode 31764 * by issuing READ_CAPACITY_16 to see whether 31765 * we can get physical block size of the drive. 31766 * 31767 * Context: Kernel thread or interrupt context. 31768 */ 31769 31770 static void 31771 sd_check_emulation_mode(sd_ssc_t *ssc) 31772 { 31773 int rval = 0; 31774 uint64_t capacity; 31775 uint_t lbasize; 31776 uint_t pbsize; 31777 int i; 31778 int devid_len; 31779 struct sd_lun *un; 31780 31781 ASSERT(ssc != NULL); 31782 un = ssc->ssc_un; 31783 ASSERT(un != NULL); 31784 ASSERT(!mutex_owned(SD_MUTEX(un))); 31785 31786 mutex_enter(SD_MUTEX(un)); 31787 if (ISCD(un)) { 31788 mutex_exit(SD_MUTEX(un)); 31789 return; 31790 } 31791 31792 if (un->un_f_descr_format_supported) { 31793 mutex_exit(SD_MUTEX(un)); 31794 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 31795 &pbsize, SD_PATH_DIRECT); 31796 mutex_enter(SD_MUTEX(un)); 31797 31798 if (rval != 0) { 31799 un->un_phy_blocksize = DEV_BSIZE; 31800 } else { 31801 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) { 31802 un->un_phy_blocksize = DEV_BSIZE; 31803 } else { 31804 un->un_phy_blocksize = pbsize; 31805 } 31806 } 31807 } 31808 31809 for (i = 0; i < sd_flash_dev_table_size; i++) { 31810 devid_len = (int)strlen(sd_flash_dev_table[i]); 31811 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len) 31812 == SD_SUCCESS) { 31813 un->un_phy_blocksize = SSD_SECSIZE; 31814 if (un->un_f_is_solid_state && 31815 un->un_phy_blocksize != un->un_tgt_blocksize) 31816 un->un_f_enable_rmw = TRUE; 31817 } 31818 } 31819 31820 mutex_exit(SD_MUTEX(un)); 31821 } 31822