1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright (c) 2011 Bayard G. Bell. All rights reserved. 27 * Copyright 2011 cyril.galibern@opensvc.com 28 * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 29 */ 30 31 /* 32 * SCSI disk target driver. 33 */ 34 #include <sys/scsi/scsi.h> 35 #include <sys/dkbad.h> 36 #include <sys/dklabel.h> 37 #include <sys/dkio.h> 38 #include <sys/fdio.h> 39 #include <sys/cdio.h> 40 #include <sys/mhd.h> 41 #include <sys/vtoc.h> 42 #include <sys/dktp/fdisk.h> 43 #include <sys/kstat.h> 44 #include <sys/vtrace.h> 45 #include <sys/note.h> 46 #include <sys/thread.h> 47 #include <sys/proc.h> 48 #include <sys/efi_partition.h> 49 #include <sys/var.h> 50 #include <sys/aio_req.h> 51 52 #ifdef __lock_lint 53 #define _LP64 54 #define __amd64 55 #endif 56 57 #if (defined(__fibre)) 58 /* Note: is there a leadville version of the following? */ 59 #include <sys/fc4/fcal_linkapp.h> 60 #endif 61 #include <sys/taskq.h> 62 #include <sys/uuid.h> 63 #include <sys/byteorder.h> 64 #include <sys/sdt.h> 65 66 #include "sd_xbuf.h" 67 68 #include <sys/scsi/targets/sddef.h> 69 #include <sys/cmlb.h> 70 #include <sys/sysevent/eventdefs.h> 71 #include <sys/sysevent/dev.h> 72 73 #include <sys/fm/protocol.h> 74 75 /* 76 * Loadable module info. 77 */ 78 #if (defined(__fibre)) 79 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver" 80 #else /* !__fibre */ 81 #define SD_MODULE_NAME "SCSI Disk Driver" 82 #endif /* !__fibre */ 83 84 /* 85 * Define the interconnect type, to allow the driver to distinguish 86 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 87 * 88 * This is really for backward compatibility. In the future, the driver 89 * should actually check the "interconnect-type" property as reported by 90 * the HBA; however at present this property is not defined by all HBAs, 91 * so we will use this #define (1) to permit the driver to run in 92 * backward-compatibility mode; and (2) to print a notification message 93 * if an FC HBA does not support the "interconnect-type" property. The 94 * behavior of the driver will be to assume parallel SCSI behaviors unless 95 * the "interconnect-type" property is defined by the HBA **AND** has a 96 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 97 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 98 * Channel behaviors (as per the old ssd). (Note that the 99 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 100 * will result in the driver assuming parallel SCSI behaviors.) 101 * 102 * (see common/sys/scsi/impl/services.h) 103 * 104 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 105 * since some FC HBAs may already support that, and there is some code in 106 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 107 * default would confuse that code, and besides things should work fine 108 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 109 * "interconnect_type" property. 110 * 111 */ 112 #if (defined(__fibre)) 113 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 114 #else 115 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 116 #endif 117 118 /* 119 * The name of the driver, established from the module name in _init. 120 */ 121 static char *sd_label = NULL; 122 123 /* 124 * Driver name is unfortunately prefixed on some driver.conf properties. 125 */ 126 #if (defined(__fibre)) 127 #define sd_max_xfer_size ssd_max_xfer_size 128 #define sd_config_list ssd_config_list 129 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 130 static char *sd_config_list = "ssd-config-list"; 131 #else 132 static char *sd_max_xfer_size = "sd_max_xfer_size"; 133 static char *sd_config_list = "sd-config-list"; 134 #endif 135 136 /* 137 * Driver global variables 138 */ 139 140 #if (defined(__fibre)) 141 /* 142 * These #defines are to avoid namespace collisions that occur because this 143 * code is currently used to compile two separate driver modules: sd and ssd. 144 * All global variables need to be treated this way (even if declared static) 145 * in order to allow the debugger to resolve the names properly. 146 * It is anticipated that in the near future the ssd module will be obsoleted, 147 * at which time this namespace issue should go away. 148 */ 149 #define sd_state ssd_state 150 #define sd_io_time ssd_io_time 151 #define sd_failfast_enable ssd_failfast_enable 152 #define sd_ua_retry_count ssd_ua_retry_count 153 #define sd_report_pfa ssd_report_pfa 154 #define sd_max_throttle ssd_max_throttle 155 #define sd_min_throttle ssd_min_throttle 156 #define sd_rot_delay ssd_rot_delay 157 158 #define sd_retry_on_reservation_conflict \ 159 ssd_retry_on_reservation_conflict 160 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 161 #define sd_resv_conflict_name ssd_resv_conflict_name 162 163 #define sd_component_mask ssd_component_mask 164 #define sd_level_mask ssd_level_mask 165 #define sd_debug_un ssd_debug_un 166 #define sd_error_level ssd_error_level 167 168 #define sd_xbuf_active_limit ssd_xbuf_active_limit 169 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 170 171 #define sd_tr ssd_tr 172 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 173 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 174 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 175 #define sd_check_media_time ssd_check_media_time 176 #define sd_wait_cmds_complete ssd_wait_cmds_complete 177 #define sd_label_mutex ssd_label_mutex 178 #define sd_detach_mutex ssd_detach_mutex 179 #define sd_log_buf ssd_log_buf 180 #define sd_log_mutex ssd_log_mutex 181 182 #define sd_disk_table ssd_disk_table 183 #define sd_disk_table_size ssd_disk_table_size 184 #define sd_sense_mutex ssd_sense_mutex 185 #define sd_cdbtab ssd_cdbtab 186 187 #define sd_cb_ops ssd_cb_ops 188 #define sd_ops ssd_ops 189 #define sd_additional_codes ssd_additional_codes 190 #define sd_tgops ssd_tgops 191 192 #define sd_minor_data ssd_minor_data 193 #define sd_minor_data_efi ssd_minor_data_efi 194 195 #define sd_tq ssd_tq 196 #define sd_wmr_tq ssd_wmr_tq 197 #define sd_taskq_name ssd_taskq_name 198 #define sd_wmr_taskq_name ssd_wmr_taskq_name 199 #define sd_taskq_minalloc ssd_taskq_minalloc 200 #define sd_taskq_maxalloc ssd_taskq_maxalloc 201 202 #define sd_dump_format_string ssd_dump_format_string 203 204 #define sd_iostart_chain ssd_iostart_chain 205 #define sd_iodone_chain ssd_iodone_chain 206 207 #define sd_pm_idletime ssd_pm_idletime 208 209 #define sd_force_pm_supported ssd_force_pm_supported 210 211 #define sd_dtype_optical_bind ssd_dtype_optical_bind 212 213 #define sd_ssc_init ssd_ssc_init 214 #define sd_ssc_send ssd_ssc_send 215 #define sd_ssc_fini ssd_ssc_fini 216 #define sd_ssc_assessment ssd_ssc_assessment 217 #define sd_ssc_post ssd_ssc_post 218 #define sd_ssc_print ssd_ssc_print 219 #define sd_ssc_ereport_post ssd_ssc_ereport_post 220 #define sd_ssc_set_info ssd_ssc_set_info 221 #define sd_ssc_extract_info ssd_ssc_extract_info 222 223 #endif 224 225 #ifdef SDDEBUG 226 int sd_force_pm_supported = 0; 227 #endif /* SDDEBUG */ 228 229 void *sd_state = NULL; 230 int sd_io_time = SD_IO_TIME; 231 int sd_failfast_enable = 1; 232 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 233 int sd_report_pfa = 1; 234 int sd_max_throttle = SD_MAX_THROTTLE; 235 int sd_min_throttle = SD_MIN_THROTTLE; 236 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 237 int sd_qfull_throttle_enable = TRUE; 238 239 int sd_retry_on_reservation_conflict = 1; 240 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 241 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 242 243 static int sd_dtype_optical_bind = -1; 244 245 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 246 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 247 248 /* 249 * Global data for debug logging. To enable debug printing, sd_component_mask 250 * and sd_level_mask should be set to the desired bit patterns as outlined in 251 * sddef.h. 252 */ 253 uint_t sd_component_mask = 0x0; 254 uint_t sd_level_mask = 0x0; 255 struct sd_lun *sd_debug_un = NULL; 256 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 257 258 /* Note: these may go away in the future... */ 259 static uint32_t sd_xbuf_active_limit = 512; 260 static uint32_t sd_xbuf_reserve_limit = 16; 261 262 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 263 264 /* 265 * Timer value used to reset the throttle after it has been reduced 266 * (typically in response to TRAN_BUSY or STATUS_QFULL) 267 */ 268 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 269 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 270 271 /* 272 * Interval value associated with the media change scsi watch. 273 */ 274 static int sd_check_media_time = 3000000; 275 276 /* 277 * Wait value used for in progress operations during a DDI_SUSPEND 278 */ 279 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 280 281 /* 282 * sd_label_mutex protects a static buffer used in the disk label 283 * component of the driver 284 */ 285 static kmutex_t sd_label_mutex; 286 287 /* 288 * sd_detach_mutex protects un_layer_count, un_detach_count, and 289 * un_opens_in_progress in the sd_lun structure. 290 */ 291 static kmutex_t sd_detach_mutex; 292 293 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 294 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 295 296 /* 297 * Global buffer and mutex for debug logging 298 */ 299 static char sd_log_buf[1024]; 300 static kmutex_t sd_log_mutex; 301 302 /* 303 * Structs and globals for recording attached lun information. 304 * This maintains a chain. Each node in the chain represents a SCSI controller. 305 * The structure records the number of luns attached to each target connected 306 * with the controller. 307 * For parallel scsi device only. 308 */ 309 struct sd_scsi_hba_tgt_lun { 310 struct sd_scsi_hba_tgt_lun *next; 311 dev_info_t *pdip; 312 int nlun[NTARGETS_WIDE]; 313 }; 314 315 /* 316 * Flag to indicate the lun is attached or detached 317 */ 318 #define SD_SCSI_LUN_ATTACH 0 319 #define SD_SCSI_LUN_DETACH 1 320 321 static kmutex_t sd_scsi_target_lun_mutex; 322 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 323 324 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 325 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 326 327 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 328 sd_scsi_target_lun_head)) 329 330 /* 331 * "Smart" Probe Caching structs, globals, #defines, etc. 332 * For parallel scsi and non-self-identify device only. 333 */ 334 335 /* 336 * The following resources and routines are implemented to support 337 * "smart" probing, which caches the scsi_probe() results in an array, 338 * in order to help avoid long probe times. 339 */ 340 struct sd_scsi_probe_cache { 341 struct sd_scsi_probe_cache *next; 342 dev_info_t *pdip; 343 int cache[NTARGETS_WIDE]; 344 }; 345 346 static kmutex_t sd_scsi_probe_cache_mutex; 347 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 348 349 /* 350 * Really we only need protection on the head of the linked list, but 351 * better safe than sorry. 352 */ 353 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 354 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 355 356 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 357 sd_scsi_probe_cache_head)) 358 359 /* 360 * Power attribute table 361 */ 362 static sd_power_attr_ss sd_pwr_ss = { 363 { "NAME=spindle-motor", "0=off", "1=on", NULL }, 364 {0, 100}, 365 {30, 0}, 366 {20000, 0} 367 }; 368 369 static sd_power_attr_pc sd_pwr_pc = { 370 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle", 371 "3=active", NULL }, 372 {0, 0, 0, 100}, 373 {90, 90, 20, 0}, 374 {15000, 15000, 1000, 0} 375 }; 376 377 /* 378 * Power level to power condition 379 */ 380 static int sd_pl2pc[] = { 381 SD_TARGET_START_VALID, 382 SD_TARGET_STANDBY, 383 SD_TARGET_IDLE, 384 SD_TARGET_ACTIVE 385 }; 386 387 /* 388 * Vendor specific data name property declarations 389 */ 390 391 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 392 393 static sd_tunables seagate_properties = { 394 SEAGATE_THROTTLE_VALUE, 395 0, 396 0, 397 0, 398 0, 399 0, 400 0, 401 0, 402 0 403 }; 404 405 406 static sd_tunables fujitsu_properties = { 407 FUJITSU_THROTTLE_VALUE, 408 0, 409 0, 410 0, 411 0, 412 0, 413 0, 414 0, 415 0 416 }; 417 418 static sd_tunables ibm_properties = { 419 IBM_THROTTLE_VALUE, 420 0, 421 0, 422 0, 423 0, 424 0, 425 0, 426 0, 427 0 428 }; 429 430 static sd_tunables purple_properties = { 431 PURPLE_THROTTLE_VALUE, 432 0, 433 0, 434 PURPLE_BUSY_RETRIES, 435 PURPLE_RESET_RETRY_COUNT, 436 PURPLE_RESERVE_RELEASE_TIME, 437 0, 438 0, 439 0 440 }; 441 442 static sd_tunables sve_properties = { 443 SVE_THROTTLE_VALUE, 444 0, 445 0, 446 SVE_BUSY_RETRIES, 447 SVE_RESET_RETRY_COUNT, 448 SVE_RESERVE_RELEASE_TIME, 449 SVE_MIN_THROTTLE_VALUE, 450 SVE_DISKSORT_DISABLED_FLAG, 451 0 452 }; 453 454 static sd_tunables maserati_properties = { 455 0, 456 0, 457 0, 458 0, 459 0, 460 0, 461 0, 462 MASERATI_DISKSORT_DISABLED_FLAG, 463 MASERATI_LUN_RESET_ENABLED_FLAG 464 }; 465 466 static sd_tunables pirus_properties = { 467 PIRUS_THROTTLE_VALUE, 468 0, 469 PIRUS_NRR_COUNT, 470 PIRUS_BUSY_RETRIES, 471 PIRUS_RESET_RETRY_COUNT, 472 0, 473 PIRUS_MIN_THROTTLE_VALUE, 474 PIRUS_DISKSORT_DISABLED_FLAG, 475 PIRUS_LUN_RESET_ENABLED_FLAG 476 }; 477 478 #endif 479 480 #if (defined(__sparc) && !defined(__fibre)) || \ 481 (defined(__i386) || defined(__amd64)) 482 483 484 static sd_tunables elite_properties = { 485 ELITE_THROTTLE_VALUE, 486 0, 487 0, 488 0, 489 0, 490 0, 491 0, 492 0, 493 0 494 }; 495 496 static sd_tunables st31200n_properties = { 497 ST31200N_THROTTLE_VALUE, 498 0, 499 0, 500 0, 501 0, 502 0, 503 0, 504 0, 505 0 506 }; 507 508 #endif /* Fibre or not */ 509 510 static sd_tunables lsi_properties_scsi = { 511 LSI_THROTTLE_VALUE, 512 0, 513 LSI_NOTREADY_RETRIES, 514 0, 515 0, 516 0, 517 0, 518 0, 519 0 520 }; 521 522 static sd_tunables symbios_properties = { 523 SYMBIOS_THROTTLE_VALUE, 524 0, 525 SYMBIOS_NOTREADY_RETRIES, 526 0, 527 0, 528 0, 529 0, 530 0, 531 0 532 }; 533 534 static sd_tunables lsi_properties = { 535 0, 536 0, 537 LSI_NOTREADY_RETRIES, 538 0, 539 0, 540 0, 541 0, 542 0, 543 0 544 }; 545 546 static sd_tunables lsi_oem_properties = { 547 0, 548 0, 549 LSI_OEM_NOTREADY_RETRIES, 550 0, 551 0, 552 0, 553 0, 554 0, 555 0, 556 1 557 }; 558 559 560 561 #if (defined(SD_PROP_TST)) 562 563 #define SD_TST_CTYPE_VAL CTYPE_CDROM 564 #define SD_TST_THROTTLE_VAL 16 565 #define SD_TST_NOTREADY_VAL 12 566 #define SD_TST_BUSY_VAL 60 567 #define SD_TST_RST_RETRY_VAL 36 568 #define SD_TST_RSV_REL_TIME 60 569 570 static sd_tunables tst_properties = { 571 SD_TST_THROTTLE_VAL, 572 SD_TST_CTYPE_VAL, 573 SD_TST_NOTREADY_VAL, 574 SD_TST_BUSY_VAL, 575 SD_TST_RST_RETRY_VAL, 576 SD_TST_RSV_REL_TIME, 577 0, 578 0, 579 0 580 }; 581 #endif 582 583 /* This is similar to the ANSI toupper implementation */ 584 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 585 586 /* 587 * Static Driver Configuration Table 588 * 589 * This is the table of disks which need throttle adjustment (or, perhaps 590 * something else as defined by the flags at a future time.) device_id 591 * is a string consisting of concatenated vid (vendor), pid (product/model) 592 * and revision strings as defined in the scsi_inquiry structure. Offsets of 593 * the parts of the string are as defined by the sizes in the scsi_inquiry 594 * structure. Device type is searched as far as the device_id string is 595 * defined. Flags defines which values are to be set in the driver from the 596 * properties list. 597 * 598 * Entries below which begin and end with a "*" are a special case. 599 * These do not have a specific vendor, and the string which follows 600 * can appear anywhere in the 16 byte PID portion of the inquiry data. 601 * 602 * Entries below which begin and end with a " " (blank) are a special 603 * case. The comparison function will treat multiple consecutive blanks 604 * as equivalent to a single blank. For example, this causes a 605 * sd_disk_table entry of " NEC CDROM " to match a device's id string 606 * of "NEC CDROM". 607 * 608 * Note: The MD21 controller type has been obsoleted. 609 * ST318202F is a Legacy device 610 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 611 * made with an FC connection. The entries here are a legacy. 612 */ 613 static sd_disk_config_t sd_disk_table[] = { 614 #if defined(__fibre) || defined(__i386) || defined(__amd64) 615 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 616 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 617 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 618 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 619 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 620 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 621 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 622 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 623 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 624 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 625 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 626 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 627 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 628 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 629 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 630 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 631 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 632 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 633 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 634 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 635 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 636 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 637 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 638 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 639 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 640 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 641 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 642 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 643 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 644 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 645 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 648 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 649 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 650 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 652 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 653 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 654 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 655 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 656 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 657 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 658 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 659 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 660 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 661 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 662 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 663 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 664 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT | 665 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 666 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT | 667 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 668 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 669 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 670 { "SUN T3", SD_CONF_BSET_THROTTLE | 671 SD_CONF_BSET_BSY_RETRY_COUNT| 672 SD_CONF_BSET_RST_RETRIES| 673 SD_CONF_BSET_RSV_REL_TIME, 674 &purple_properties }, 675 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 676 SD_CONF_BSET_BSY_RETRY_COUNT| 677 SD_CONF_BSET_RST_RETRIES| 678 SD_CONF_BSET_RSV_REL_TIME| 679 SD_CONF_BSET_MIN_THROTTLE| 680 SD_CONF_BSET_DISKSORT_DISABLED, 681 &sve_properties }, 682 { "SUN T4", SD_CONF_BSET_THROTTLE | 683 SD_CONF_BSET_BSY_RETRY_COUNT| 684 SD_CONF_BSET_RST_RETRIES| 685 SD_CONF_BSET_RSV_REL_TIME, 686 &purple_properties }, 687 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 688 SD_CONF_BSET_LUN_RESET_ENABLED, 689 &maserati_properties }, 690 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 691 SD_CONF_BSET_NRR_COUNT| 692 SD_CONF_BSET_BSY_RETRY_COUNT| 693 SD_CONF_BSET_RST_RETRIES| 694 SD_CONF_BSET_MIN_THROTTLE| 695 SD_CONF_BSET_DISKSORT_DISABLED| 696 SD_CONF_BSET_LUN_RESET_ENABLED, 697 &pirus_properties }, 698 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 699 SD_CONF_BSET_NRR_COUNT| 700 SD_CONF_BSET_BSY_RETRY_COUNT| 701 SD_CONF_BSET_RST_RETRIES| 702 SD_CONF_BSET_MIN_THROTTLE| 703 SD_CONF_BSET_DISKSORT_DISABLED| 704 SD_CONF_BSET_LUN_RESET_ENABLED, 705 &pirus_properties }, 706 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 707 SD_CONF_BSET_NRR_COUNT| 708 SD_CONF_BSET_BSY_RETRY_COUNT| 709 SD_CONF_BSET_RST_RETRIES| 710 SD_CONF_BSET_MIN_THROTTLE| 711 SD_CONF_BSET_DISKSORT_DISABLED| 712 SD_CONF_BSET_LUN_RESET_ENABLED, 713 &pirus_properties }, 714 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 715 SD_CONF_BSET_NRR_COUNT| 716 SD_CONF_BSET_BSY_RETRY_COUNT| 717 SD_CONF_BSET_RST_RETRIES| 718 SD_CONF_BSET_MIN_THROTTLE| 719 SD_CONF_BSET_DISKSORT_DISABLED| 720 SD_CONF_BSET_LUN_RESET_ENABLED, 721 &pirus_properties }, 722 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 723 SD_CONF_BSET_NRR_COUNT| 724 SD_CONF_BSET_BSY_RETRY_COUNT| 725 SD_CONF_BSET_RST_RETRIES| 726 SD_CONF_BSET_MIN_THROTTLE| 727 SD_CONF_BSET_DISKSORT_DISABLED| 728 SD_CONF_BSET_LUN_RESET_ENABLED, 729 &pirus_properties }, 730 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 731 SD_CONF_BSET_NRR_COUNT| 732 SD_CONF_BSET_BSY_RETRY_COUNT| 733 SD_CONF_BSET_RST_RETRIES| 734 SD_CONF_BSET_MIN_THROTTLE| 735 SD_CONF_BSET_DISKSORT_DISABLED| 736 SD_CONF_BSET_LUN_RESET_ENABLED, 737 &pirus_properties }, 738 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 739 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 740 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 741 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 742 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 743 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 744 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 745 #endif /* fibre or NON-sparc platforms */ 746 #if ((defined(__sparc) && !defined(__fibre)) ||\ 747 (defined(__i386) || defined(__amd64))) 748 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 749 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 750 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 751 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 752 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 753 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 754 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 755 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 756 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 757 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 758 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 759 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 760 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 761 &symbios_properties }, 762 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 763 &lsi_properties_scsi }, 764 #if defined(__i386) || defined(__amd64) 765 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 766 | SD_CONF_BSET_READSUB_BCD 767 | SD_CONF_BSET_READ_TOC_ADDR_BCD 768 | SD_CONF_BSET_NO_READ_HEADER 769 | SD_CONF_BSET_READ_CD_XD4), NULL }, 770 771 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 772 | SD_CONF_BSET_READSUB_BCD 773 | SD_CONF_BSET_READ_TOC_ADDR_BCD 774 | SD_CONF_BSET_NO_READ_HEADER 775 | SD_CONF_BSET_READ_CD_XD4), NULL }, 776 #endif /* __i386 || __amd64 */ 777 #endif /* sparc NON-fibre or NON-sparc platforms */ 778 779 #if (defined(SD_PROP_TST)) 780 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 781 | SD_CONF_BSET_CTYPE 782 | SD_CONF_BSET_NRR_COUNT 783 | SD_CONF_BSET_FAB_DEVID 784 | SD_CONF_BSET_NOCACHE 785 | SD_CONF_BSET_BSY_RETRY_COUNT 786 | SD_CONF_BSET_PLAYMSF_BCD 787 | SD_CONF_BSET_READSUB_BCD 788 | SD_CONF_BSET_READ_TOC_TRK_BCD 789 | SD_CONF_BSET_READ_TOC_ADDR_BCD 790 | SD_CONF_BSET_NO_READ_HEADER 791 | SD_CONF_BSET_READ_CD_XD4 792 | SD_CONF_BSET_RST_RETRIES 793 | SD_CONF_BSET_RSV_REL_TIME 794 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 795 #endif 796 }; 797 798 static const int sd_disk_table_size = 799 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 800 801 /* 802 * Emulation mode disk drive VID/PID table 803 */ 804 static char sd_flash_dev_table[][25] = { 805 "ATA MARVELL SD88SA02", 806 "MARVELL SD88SA02", 807 "TOSHIBA THNSNV05", 808 }; 809 810 static const int sd_flash_dev_table_size = 811 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]); 812 813 #define SD_INTERCONNECT_PARALLEL 0 814 #define SD_INTERCONNECT_FABRIC 1 815 #define SD_INTERCONNECT_FIBRE 2 816 #define SD_INTERCONNECT_SSA 3 817 #define SD_INTERCONNECT_SATA 4 818 #define SD_INTERCONNECT_SAS 5 819 820 #define SD_IS_PARALLEL_SCSI(un) \ 821 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 822 #define SD_IS_SERIAL(un) \ 823 (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\ 824 ((un)->un_interconnect_type == SD_INTERCONNECT_SAS)) 825 826 /* 827 * Definitions used by device id registration routines 828 */ 829 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 830 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 831 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 832 833 static kmutex_t sd_sense_mutex = {0}; 834 835 /* 836 * Macros for updates of the driver state 837 */ 838 #define New_state(un, s) \ 839 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 840 #define Restore_state(un) \ 841 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 842 843 static struct sd_cdbinfo sd_cdbtab[] = { 844 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 845 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 846 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 847 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 848 }; 849 850 /* 851 * Specifies the number of seconds that must have elapsed since the last 852 * cmd. has completed for a device to be declared idle to the PM framework. 853 */ 854 static int sd_pm_idletime = 1; 855 856 /* 857 * Internal function prototypes 858 */ 859 860 #if (defined(__fibre)) 861 /* 862 * These #defines are to avoid namespace collisions that occur because this 863 * code is currently used to compile two separate driver modules: sd and ssd. 864 * All function names need to be treated this way (even if declared static) 865 * in order to allow the debugger to resolve the names properly. 866 * It is anticipated that in the near future the ssd module will be obsoleted, 867 * at which time this ugliness should go away. 868 */ 869 #define sd_log_trace ssd_log_trace 870 #define sd_log_info ssd_log_info 871 #define sd_log_err ssd_log_err 872 #define sdprobe ssdprobe 873 #define sdinfo ssdinfo 874 #define sd_prop_op ssd_prop_op 875 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 876 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 877 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 878 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 879 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 880 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 881 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 882 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 883 #define sd_spin_up_unit ssd_spin_up_unit 884 #define sd_enable_descr_sense ssd_enable_descr_sense 885 #define sd_reenable_dsense_task ssd_reenable_dsense_task 886 #define sd_set_mmc_caps ssd_set_mmc_caps 887 #define sd_read_unit_properties ssd_read_unit_properties 888 #define sd_process_sdconf_file ssd_process_sdconf_file 889 #define sd_process_sdconf_table ssd_process_sdconf_table 890 #define sd_sdconf_id_match ssd_sdconf_id_match 891 #define sd_blank_cmp ssd_blank_cmp 892 #define sd_chk_vers1_data ssd_chk_vers1_data 893 #define sd_set_vers1_properties ssd_set_vers1_properties 894 #define sd_check_solid_state ssd_check_solid_state 895 #define sd_read_capacity ssd_read_capacity 896 897 #define sd_get_physical_geometry ssd_get_physical_geometry 898 #define sd_get_virtual_geometry ssd_get_virtual_geometry 899 #define sd_update_block_info ssd_update_block_info 900 #define sd_register_devid ssd_register_devid 901 #define sd_get_devid ssd_get_devid 902 #define sd_create_devid ssd_create_devid 903 #define sd_write_deviceid ssd_write_deviceid 904 #define sd_check_vpd_page_support ssd_check_vpd_page_support 905 #define sd_setup_pm ssd_setup_pm 906 #define sd_create_pm_components ssd_create_pm_components 907 #define sd_ddi_suspend ssd_ddi_suspend 908 #define sd_ddi_resume ssd_ddi_resume 909 #define sd_pm_state_change ssd_pm_state_change 910 #define sdpower ssdpower 911 #define sdattach ssdattach 912 #define sddetach ssddetach 913 #define sd_unit_attach ssd_unit_attach 914 #define sd_unit_detach ssd_unit_detach 915 #define sd_set_unit_attributes ssd_set_unit_attributes 916 #define sd_create_errstats ssd_create_errstats 917 #define sd_set_errstats ssd_set_errstats 918 #define sd_set_pstats ssd_set_pstats 919 #define sddump ssddump 920 #define sd_scsi_poll ssd_scsi_poll 921 #define sd_send_polled_RQS ssd_send_polled_RQS 922 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 923 #define sd_init_event_callbacks ssd_init_event_callbacks 924 #define sd_event_callback ssd_event_callback 925 #define sd_cache_control ssd_cache_control 926 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 927 #define sd_get_nv_sup ssd_get_nv_sup 928 #define sd_make_device ssd_make_device 929 #define sdopen ssdopen 930 #define sdclose ssdclose 931 #define sd_ready_and_valid ssd_ready_and_valid 932 #define sdmin ssdmin 933 #define sdread ssdread 934 #define sdwrite ssdwrite 935 #define sdaread ssdaread 936 #define sdawrite ssdawrite 937 #define sdstrategy ssdstrategy 938 #define sdioctl ssdioctl 939 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 940 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 941 #define sd_checksum_iostart ssd_checksum_iostart 942 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 943 #define sd_pm_iostart ssd_pm_iostart 944 #define sd_core_iostart ssd_core_iostart 945 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 946 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 947 #define sd_checksum_iodone ssd_checksum_iodone 948 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 949 #define sd_pm_iodone ssd_pm_iodone 950 #define sd_initpkt_for_buf ssd_initpkt_for_buf 951 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 952 #define sd_setup_rw_pkt ssd_setup_rw_pkt 953 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 954 #define sd_buf_iodone ssd_buf_iodone 955 #define sd_uscsi_strategy ssd_uscsi_strategy 956 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 957 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 958 #define sd_uscsi_iodone ssd_uscsi_iodone 959 #define sd_xbuf_strategy ssd_xbuf_strategy 960 #define sd_xbuf_init ssd_xbuf_init 961 #define sd_pm_entry ssd_pm_entry 962 #define sd_pm_exit ssd_pm_exit 963 964 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 965 #define sd_pm_timeout_handler ssd_pm_timeout_handler 966 967 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 968 #define sdintr ssdintr 969 #define sd_start_cmds ssd_start_cmds 970 #define sd_send_scsi_cmd ssd_send_scsi_cmd 971 #define sd_bioclone_alloc ssd_bioclone_alloc 972 #define sd_bioclone_free ssd_bioclone_free 973 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 974 #define sd_shadow_buf_free ssd_shadow_buf_free 975 #define sd_print_transport_rejected_message \ 976 ssd_print_transport_rejected_message 977 #define sd_retry_command ssd_retry_command 978 #define sd_set_retry_bp ssd_set_retry_bp 979 #define sd_send_request_sense_command ssd_send_request_sense_command 980 #define sd_start_retry_command ssd_start_retry_command 981 #define sd_start_direct_priority_command \ 982 ssd_start_direct_priority_command 983 #define sd_return_failed_command ssd_return_failed_command 984 #define sd_return_failed_command_no_restart \ 985 ssd_return_failed_command_no_restart 986 #define sd_return_command ssd_return_command 987 #define sd_sync_with_callback ssd_sync_with_callback 988 #define sdrunout ssdrunout 989 #define sd_mark_rqs_busy ssd_mark_rqs_busy 990 #define sd_mark_rqs_idle ssd_mark_rqs_idle 991 #define sd_reduce_throttle ssd_reduce_throttle 992 #define sd_restore_throttle ssd_restore_throttle 993 #define sd_print_incomplete_msg ssd_print_incomplete_msg 994 #define sd_init_cdb_limits ssd_init_cdb_limits 995 #define sd_pkt_status_good ssd_pkt_status_good 996 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 997 #define sd_pkt_status_busy ssd_pkt_status_busy 998 #define sd_pkt_status_reservation_conflict \ 999 ssd_pkt_status_reservation_conflict 1000 #define sd_pkt_status_qfull ssd_pkt_status_qfull 1001 #define sd_handle_request_sense ssd_handle_request_sense 1002 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 1003 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 1004 #define sd_validate_sense_data ssd_validate_sense_data 1005 #define sd_decode_sense ssd_decode_sense 1006 #define sd_print_sense_msg ssd_print_sense_msg 1007 #define sd_sense_key_no_sense ssd_sense_key_no_sense 1008 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 1009 #define sd_sense_key_not_ready ssd_sense_key_not_ready 1010 #define sd_sense_key_medium_or_hardware_error \ 1011 ssd_sense_key_medium_or_hardware_error 1012 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 1013 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 1014 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1015 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1016 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1017 #define sd_sense_key_default ssd_sense_key_default 1018 #define sd_print_retry_msg ssd_print_retry_msg 1019 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1020 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1021 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1022 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1023 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1024 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1025 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1026 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1027 #define sd_pkt_reason_default ssd_pkt_reason_default 1028 #define sd_reset_target ssd_reset_target 1029 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1030 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1031 #define sd_taskq_create ssd_taskq_create 1032 #define sd_taskq_delete ssd_taskq_delete 1033 #define sd_target_change_task ssd_target_change_task 1034 #define sd_log_dev_status_event ssd_log_dev_status_event 1035 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event 1036 #define sd_log_eject_request_event ssd_log_eject_request_event 1037 #define sd_media_change_task ssd_media_change_task 1038 #define sd_handle_mchange ssd_handle_mchange 1039 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1040 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1041 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1042 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1043 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1044 sd_send_scsi_feature_GET_CONFIGURATION 1045 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1046 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1047 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1048 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1049 ssd_send_scsi_PERSISTENT_RESERVE_IN 1050 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1051 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1052 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1053 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1054 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1055 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1056 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1057 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1058 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1059 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION \ 1060 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 1061 #define sd_gesn_media_data_valid ssd_gesn_media_data_valid 1062 #define sd_alloc_rqs ssd_alloc_rqs 1063 #define sd_free_rqs ssd_free_rqs 1064 #define sd_dump_memory ssd_dump_memory 1065 #define sd_get_media_info_com ssd_get_media_info_com 1066 #define sd_get_media_info ssd_get_media_info 1067 #define sd_get_media_info_ext ssd_get_media_info_ext 1068 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1069 #define sd_nvpair_str_decode ssd_nvpair_str_decode 1070 #define sd_strtok_r ssd_strtok_r 1071 #define sd_set_properties ssd_set_properties 1072 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1073 #define sd_setup_next_xfer ssd_setup_next_xfer 1074 #define sd_dkio_get_temp ssd_dkio_get_temp 1075 #define sd_check_mhd ssd_check_mhd 1076 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1077 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1078 #define sd_sname ssd_sname 1079 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1080 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1081 #define sd_take_ownership ssd_take_ownership 1082 #define sd_reserve_release ssd_reserve_release 1083 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1084 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1085 #define sd_persistent_reservation_in_read_keys \ 1086 ssd_persistent_reservation_in_read_keys 1087 #define sd_persistent_reservation_in_read_resv \ 1088 ssd_persistent_reservation_in_read_resv 1089 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1090 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1091 #define sd_mhdioc_release ssd_mhdioc_release 1092 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1093 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1094 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1095 #define sr_change_blkmode ssr_change_blkmode 1096 #define sr_change_speed ssr_change_speed 1097 #define sr_atapi_change_speed ssr_atapi_change_speed 1098 #define sr_pause_resume ssr_pause_resume 1099 #define sr_play_msf ssr_play_msf 1100 #define sr_play_trkind ssr_play_trkind 1101 #define sr_read_all_subcodes ssr_read_all_subcodes 1102 #define sr_read_subchannel ssr_read_subchannel 1103 #define sr_read_tocentry ssr_read_tocentry 1104 #define sr_read_tochdr ssr_read_tochdr 1105 #define sr_read_cdda ssr_read_cdda 1106 #define sr_read_cdxa ssr_read_cdxa 1107 #define sr_read_mode1 ssr_read_mode1 1108 #define sr_read_mode2 ssr_read_mode2 1109 #define sr_read_cd_mode2 ssr_read_cd_mode2 1110 #define sr_sector_mode ssr_sector_mode 1111 #define sr_eject ssr_eject 1112 #define sr_ejected ssr_ejected 1113 #define sr_check_wp ssr_check_wp 1114 #define sd_watch_request_submit ssd_watch_request_submit 1115 #define sd_check_media ssd_check_media 1116 #define sd_media_watch_cb ssd_media_watch_cb 1117 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1118 #define sr_volume_ctrl ssr_volume_ctrl 1119 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1120 #define sd_log_page_supported ssd_log_page_supported 1121 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1122 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1123 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1124 #define sd_range_lock ssd_range_lock 1125 #define sd_get_range ssd_get_range 1126 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1127 #define sd_range_unlock ssd_range_unlock 1128 #define sd_read_modify_write_task ssd_read_modify_write_task 1129 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1130 1131 #define sd_iostart_chain ssd_iostart_chain 1132 #define sd_iodone_chain ssd_iodone_chain 1133 #define sd_initpkt_map ssd_initpkt_map 1134 #define sd_destroypkt_map ssd_destroypkt_map 1135 #define sd_chain_type_map ssd_chain_type_map 1136 #define sd_chain_index_map ssd_chain_index_map 1137 1138 #define sd_failfast_flushctl ssd_failfast_flushctl 1139 #define sd_failfast_flushq ssd_failfast_flushq 1140 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1141 1142 #define sd_is_lsi ssd_is_lsi 1143 #define sd_tg_rdwr ssd_tg_rdwr 1144 #define sd_tg_getinfo ssd_tg_getinfo 1145 #define sd_rmw_msg_print_handler ssd_rmw_msg_print_handler 1146 1147 #endif /* #if (defined(__fibre)) */ 1148 1149 1150 int _init(void); 1151 int _fini(void); 1152 int _info(struct modinfo *modinfop); 1153 1154 /*PRINTFLIKE3*/ 1155 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1156 /*PRINTFLIKE3*/ 1157 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1158 /*PRINTFLIKE3*/ 1159 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1160 1161 static int sdprobe(dev_info_t *devi); 1162 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1163 void **result); 1164 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1165 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1166 1167 /* 1168 * Smart probe for parallel scsi 1169 */ 1170 static void sd_scsi_probe_cache_init(void); 1171 static void sd_scsi_probe_cache_fini(void); 1172 static void sd_scsi_clear_probe_cache(void); 1173 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1174 1175 /* 1176 * Attached luns on target for parallel scsi 1177 */ 1178 static void sd_scsi_target_lun_init(void); 1179 static void sd_scsi_target_lun_fini(void); 1180 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1181 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1182 1183 static int sd_spin_up_unit(sd_ssc_t *ssc); 1184 1185 /* 1186 * Using sd_ssc_init to establish sd_ssc_t struct 1187 * Using sd_ssc_send to send uscsi internal command 1188 * Using sd_ssc_fini to free sd_ssc_t struct 1189 */ 1190 static sd_ssc_t *sd_ssc_init(struct sd_lun *un); 1191 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, 1192 int flag, enum uio_seg dataspace, int path_flag); 1193 static void sd_ssc_fini(sd_ssc_t *ssc); 1194 1195 /* 1196 * Using sd_ssc_assessment to set correct type-of-assessment 1197 * Using sd_ssc_post to post ereport & system log 1198 * sd_ssc_post will call sd_ssc_print to print system log 1199 * sd_ssc_post will call sd_ssd_ereport_post to post ereport 1200 */ 1201 static void sd_ssc_assessment(sd_ssc_t *ssc, 1202 enum sd_type_assessment tp_assess); 1203 1204 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess); 1205 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity); 1206 static void sd_ssc_ereport_post(sd_ssc_t *ssc, 1207 enum sd_driver_assessment drv_assess); 1208 1209 /* 1210 * Using sd_ssc_set_info to mark an un-decodable-data error. 1211 * Using sd_ssc_extract_info to transfer information from internal 1212 * data structures to sd_ssc_t. 1213 */ 1214 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, 1215 const char *fmt, ...); 1216 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, 1217 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp); 1218 1219 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1220 enum uio_seg dataspace, int path_flag); 1221 1222 #ifdef _LP64 1223 static void sd_enable_descr_sense(sd_ssc_t *ssc); 1224 static void sd_reenable_dsense_task(void *arg); 1225 #endif /* _LP64 */ 1226 1227 static void sd_set_mmc_caps(sd_ssc_t *ssc); 1228 1229 static void sd_read_unit_properties(struct sd_lun *un); 1230 static int sd_process_sdconf_file(struct sd_lun *un); 1231 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str); 1232 static char *sd_strtok_r(char *string, const char *sepset, char **lasts); 1233 static void sd_set_properties(struct sd_lun *un, char *name, char *value); 1234 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1235 int *data_list, sd_tunables *values); 1236 static void sd_process_sdconf_table(struct sd_lun *un); 1237 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1238 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1239 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1240 int list_len, char *dataname_ptr); 1241 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1242 sd_tunables *prop_list); 1243 1244 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, 1245 int reservation_flag); 1246 static int sd_get_devid(sd_ssc_t *ssc); 1247 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc); 1248 static int sd_write_deviceid(sd_ssc_t *ssc); 1249 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1250 static int sd_check_vpd_page_support(sd_ssc_t *ssc); 1251 1252 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi); 1253 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1254 1255 static int sd_ddi_suspend(dev_info_t *devi); 1256 static int sd_ddi_resume(dev_info_t *devi); 1257 static int sd_pm_state_change(struct sd_lun *un, int level, int flag); 1258 static int sdpower(dev_info_t *devi, int component, int level); 1259 1260 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1261 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1262 static int sd_unit_attach(dev_info_t *devi); 1263 static int sd_unit_detach(dev_info_t *devi); 1264 1265 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1266 static void sd_create_errstats(struct sd_lun *un, int instance); 1267 static void sd_set_errstats(struct sd_lun *un); 1268 static void sd_set_pstats(struct sd_lun *un); 1269 1270 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1271 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1272 static int sd_send_polled_RQS(struct sd_lun *un); 1273 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1274 1275 #if (defined(__fibre)) 1276 /* 1277 * Event callbacks (photon) 1278 */ 1279 static void sd_init_event_callbacks(struct sd_lun *un); 1280 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1281 #endif 1282 1283 /* 1284 * Defines for sd_cache_control 1285 */ 1286 1287 #define SD_CACHE_ENABLE 1 1288 #define SD_CACHE_DISABLE 0 1289 #define SD_CACHE_NOCHANGE -1 1290 1291 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag); 1292 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled); 1293 static void sd_get_nv_sup(sd_ssc_t *ssc); 1294 static dev_t sd_make_device(dev_info_t *devi); 1295 static void sd_check_solid_state(sd_ssc_t *ssc); 1296 static int sd_read_capacity(sd_ssc_t *ssc, int path_flag); 1297 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1298 uint64_t capacity); 1299 1300 /* 1301 * Driver entry point functions. 1302 */ 1303 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1304 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1305 static int sd_ready_and_valid(sd_ssc_t *ssc, int part); 1306 1307 static void sdmin(struct buf *bp); 1308 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1309 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1310 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1311 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1312 1313 static int sdstrategy(struct buf *bp); 1314 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1315 1316 /* 1317 * Function prototypes for layering functions in the iostart chain. 1318 */ 1319 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1320 struct buf *bp); 1321 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1322 struct buf *bp); 1323 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1324 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1325 struct buf *bp); 1326 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1327 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1328 1329 /* 1330 * Function prototypes for layering functions in the iodone chain. 1331 */ 1332 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1333 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1334 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1335 struct buf *bp); 1336 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1337 struct buf *bp); 1338 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1339 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1340 struct buf *bp); 1341 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1342 1343 /* 1344 * Prototypes for functions to support buf(9S) based IO. 1345 */ 1346 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1347 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1348 static void sd_destroypkt_for_buf(struct buf *); 1349 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1350 struct buf *bp, int flags, 1351 int (*callback)(caddr_t), caddr_t callback_arg, 1352 diskaddr_t lba, uint32_t blockcount); 1353 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1354 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1355 1356 /* 1357 * Prototypes for functions to support USCSI IO. 1358 */ 1359 static int sd_uscsi_strategy(struct buf *bp); 1360 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1361 static void sd_destroypkt_for_uscsi(struct buf *); 1362 1363 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1364 uchar_t chain_type, void *pktinfop); 1365 1366 static int sd_pm_entry(struct sd_lun *un); 1367 static void sd_pm_exit(struct sd_lun *un); 1368 1369 static void sd_pm_idletimeout_handler(void *arg); 1370 1371 /* 1372 * sd_core internal functions (used at the sd_core_io layer). 1373 */ 1374 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1375 static void sdintr(struct scsi_pkt *pktp); 1376 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1377 1378 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1379 enum uio_seg dataspace, int path_flag); 1380 1381 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1382 daddr_t blkno, int (*func)(struct buf *)); 1383 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1384 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1385 static void sd_bioclone_free(struct buf *bp); 1386 static void sd_shadow_buf_free(struct buf *bp); 1387 1388 static void sd_print_transport_rejected_message(struct sd_lun *un, 1389 struct sd_xbuf *xp, int code); 1390 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1391 void *arg, int code); 1392 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1393 void *arg, int code); 1394 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1395 void *arg, int code); 1396 1397 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1398 int retry_check_flag, 1399 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1400 int c), 1401 void *user_arg, int failure_code, clock_t retry_delay, 1402 void (*statp)(kstat_io_t *)); 1403 1404 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1405 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1406 1407 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1408 struct scsi_pkt *pktp); 1409 static void sd_start_retry_command(void *arg); 1410 static void sd_start_direct_priority_command(void *arg); 1411 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1412 int errcode); 1413 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1414 struct buf *bp, int errcode); 1415 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1416 static void sd_sync_with_callback(struct sd_lun *un); 1417 static int sdrunout(caddr_t arg); 1418 1419 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1420 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1421 1422 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1423 static void sd_restore_throttle(void *arg); 1424 1425 static void sd_init_cdb_limits(struct sd_lun *un); 1426 1427 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1428 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1429 1430 /* 1431 * Error handling functions 1432 */ 1433 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1434 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1435 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1436 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1437 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1438 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1439 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1440 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1441 1442 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1443 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1444 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1445 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1446 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1447 struct sd_xbuf *xp, size_t actual_len); 1448 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1449 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1450 1451 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1452 void *arg, int code); 1453 1454 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1455 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1456 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1457 uint8_t *sense_datap, 1458 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1459 static void sd_sense_key_not_ready(struct sd_lun *un, 1460 uint8_t *sense_datap, 1461 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1462 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1463 uint8_t *sense_datap, 1464 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1465 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1466 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1467 static void sd_sense_key_unit_attention(struct sd_lun *un, 1468 uint8_t *sense_datap, 1469 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1470 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1471 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1472 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1473 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1474 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1475 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1476 static void sd_sense_key_default(struct sd_lun *un, 1477 uint8_t *sense_datap, 1478 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1479 1480 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1481 void *arg, int flag); 1482 1483 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1484 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1485 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1486 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1487 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1488 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1489 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1490 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1491 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1492 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1493 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1494 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1495 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1496 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1497 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1498 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1499 1500 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1501 1502 static void sd_start_stop_unit_callback(void *arg); 1503 static void sd_start_stop_unit_task(void *arg); 1504 1505 static void sd_taskq_create(void); 1506 static void sd_taskq_delete(void); 1507 static void sd_target_change_task(void *arg); 1508 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag); 1509 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag); 1510 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag); 1511 static void sd_media_change_task(void *arg); 1512 1513 static int sd_handle_mchange(struct sd_lun *un); 1514 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag); 1515 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, 1516 uint32_t *lbap, int path_flag); 1517 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 1518 uint32_t *lbap, uint32_t *psp, int path_flag); 1519 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, 1520 int flag, int path_flag); 1521 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, 1522 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1523 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag); 1524 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, 1525 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1526 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, 1527 uchar_t usr_cmd, uchar_t *usr_bufp); 1528 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1529 struct dk_callback *dkc); 1530 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1531 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, 1532 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1533 uchar_t *bufaddr, uint_t buflen, int path_flag); 1534 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 1535 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1536 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1537 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, 1538 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1539 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, 1540 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1541 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 1542 size_t buflen, daddr_t start_block, int path_flag); 1543 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \ 1544 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \ 1545 path_flag) 1546 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\ 1547 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\ 1548 path_flag) 1549 1550 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, 1551 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1552 uint16_t param_ptr, int path_flag); 1553 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, 1554 uchar_t *bufaddr, size_t buflen, uchar_t class_req); 1555 static boolean_t sd_gesn_media_data_valid(uchar_t *data); 1556 1557 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1558 static void sd_free_rqs(struct sd_lun *un); 1559 1560 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1561 uchar_t *data, int len, int fmt); 1562 static void sd_panic_for_res_conflict(struct sd_lun *un); 1563 1564 /* 1565 * Disk Ioctl Function Prototypes 1566 */ 1567 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1568 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag); 1569 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1570 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1571 1572 /* 1573 * Multi-host Ioctl Prototypes 1574 */ 1575 static int sd_check_mhd(dev_t dev, int interval); 1576 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1577 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1578 static char *sd_sname(uchar_t status); 1579 static void sd_mhd_resvd_recover(void *arg); 1580 static void sd_resv_reclaim_thread(); 1581 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1582 static int sd_reserve_release(dev_t dev, int cmd); 1583 static void sd_rmv_resv_reclaim_req(dev_t dev); 1584 static void sd_mhd_reset_notify_cb(caddr_t arg); 1585 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1586 mhioc_inkeys_t *usrp, int flag); 1587 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1588 mhioc_inresvs_t *usrp, int flag); 1589 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1590 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1591 static int sd_mhdioc_release(dev_t dev); 1592 static int sd_mhdioc_register_devid(dev_t dev); 1593 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1594 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1595 1596 /* 1597 * SCSI removable prototypes 1598 */ 1599 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1600 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1601 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1602 static int sr_pause_resume(dev_t dev, int mode); 1603 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1604 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1605 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1606 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1607 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1609 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1610 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1611 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1612 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1613 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1614 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1615 static int sr_eject(dev_t dev); 1616 static void sr_ejected(register struct sd_lun *un); 1617 static int sr_check_wp(dev_t dev); 1618 static opaque_t sd_watch_request_submit(struct sd_lun *un); 1619 static int sd_check_media(dev_t dev, enum dkio_state state); 1620 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1621 static void sd_delayed_cv_broadcast(void *arg); 1622 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1623 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1624 1625 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page); 1626 1627 /* 1628 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1629 */ 1630 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag); 1631 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1632 static void sd_wm_cache_destructor(void *wm, void *un); 1633 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1634 daddr_t endb, ushort_t typ); 1635 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1636 daddr_t endb); 1637 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1638 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1639 static void sd_read_modify_write_task(void * arg); 1640 static int 1641 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1642 struct buf **bpp); 1643 1644 1645 /* 1646 * Function prototypes for failfast support. 1647 */ 1648 static void sd_failfast_flushq(struct sd_lun *un); 1649 static int sd_failfast_flushq_callback(struct buf *bp); 1650 1651 /* 1652 * Function prototypes to check for lsi devices 1653 */ 1654 static void sd_is_lsi(struct sd_lun *un); 1655 1656 /* 1657 * Function prototypes for partial DMA support 1658 */ 1659 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1660 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1661 1662 1663 /* Function prototypes for cmlb */ 1664 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1665 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1666 1667 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1668 1669 /* 1670 * For printing RMW warning message timely 1671 */ 1672 static void sd_rmw_msg_print_handler(void *arg); 1673 1674 /* 1675 * Constants for failfast support: 1676 * 1677 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1678 * failfast processing being performed. 1679 * 1680 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1681 * failfast processing on all bufs with B_FAILFAST set. 1682 */ 1683 1684 #define SD_FAILFAST_INACTIVE 0 1685 #define SD_FAILFAST_ACTIVE 1 1686 1687 /* 1688 * Bitmask to control behavior of buf(9S) flushes when a transition to 1689 * the failfast state occurs. Optional bits include: 1690 * 1691 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1692 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1693 * be flushed. 1694 * 1695 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1696 * driver, in addition to the regular wait queue. This includes the xbuf 1697 * queues. When clear, only the driver's wait queue will be flushed. 1698 */ 1699 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1700 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1701 1702 /* 1703 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1704 * to flush all queues within the driver. 1705 */ 1706 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1707 1708 1709 /* 1710 * SD Testing Fault Injection 1711 */ 1712 #ifdef SD_FAULT_INJECTION 1713 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1714 static void sd_faultinjection(struct scsi_pkt *pktp); 1715 static void sd_injection_log(char *buf, struct sd_lun *un); 1716 #endif 1717 1718 /* 1719 * Device driver ops vector 1720 */ 1721 static struct cb_ops sd_cb_ops = { 1722 sdopen, /* open */ 1723 sdclose, /* close */ 1724 sdstrategy, /* strategy */ 1725 nodev, /* print */ 1726 sddump, /* dump */ 1727 sdread, /* read */ 1728 sdwrite, /* write */ 1729 sdioctl, /* ioctl */ 1730 nodev, /* devmap */ 1731 nodev, /* mmap */ 1732 nodev, /* segmap */ 1733 nochpoll, /* poll */ 1734 sd_prop_op, /* cb_prop_op */ 1735 0, /* streamtab */ 1736 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1737 CB_REV, /* cb_rev */ 1738 sdaread, /* async I/O read entry point */ 1739 sdawrite /* async I/O write entry point */ 1740 }; 1741 1742 struct dev_ops sd_ops = { 1743 DEVO_REV, /* devo_rev, */ 1744 0, /* refcnt */ 1745 sdinfo, /* info */ 1746 nulldev, /* identify */ 1747 sdprobe, /* probe */ 1748 sdattach, /* attach */ 1749 sddetach, /* detach */ 1750 nodev, /* reset */ 1751 &sd_cb_ops, /* driver operations */ 1752 NULL, /* bus operations */ 1753 sdpower, /* power */ 1754 ddi_quiesce_not_needed, /* quiesce */ 1755 }; 1756 1757 /* 1758 * This is the loadable module wrapper. 1759 */ 1760 #include <sys/modctl.h> 1761 1762 #ifndef XPV_HVM_DRIVER 1763 static struct modldrv modldrv = { 1764 &mod_driverops, /* Type of module. This one is a driver */ 1765 SD_MODULE_NAME, /* Module name. */ 1766 &sd_ops /* driver ops */ 1767 }; 1768 1769 static struct modlinkage modlinkage = { 1770 MODREV_1, &modldrv, NULL 1771 }; 1772 1773 #else /* XPV_HVM_DRIVER */ 1774 static struct modlmisc modlmisc = { 1775 &mod_miscops, /* Type of module. This one is a misc */ 1776 "HVM " SD_MODULE_NAME, /* Module name. */ 1777 }; 1778 1779 static struct modlinkage modlinkage = { 1780 MODREV_1, &modlmisc, NULL 1781 }; 1782 1783 #endif /* XPV_HVM_DRIVER */ 1784 1785 static cmlb_tg_ops_t sd_tgops = { 1786 TG_DK_OPS_VERSION_1, 1787 sd_tg_rdwr, 1788 sd_tg_getinfo 1789 }; 1790 1791 static struct scsi_asq_key_strings sd_additional_codes[] = { 1792 0x81, 0, "Logical Unit is Reserved", 1793 0x85, 0, "Audio Address Not Valid", 1794 0xb6, 0, "Media Load Mechanism Failed", 1795 0xB9, 0, "Audio Play Operation Aborted", 1796 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1797 0x53, 2, "Medium removal prevented", 1798 0x6f, 0, "Authentication failed during key exchange", 1799 0x6f, 1, "Key not present", 1800 0x6f, 2, "Key not established", 1801 0x6f, 3, "Read without proper authentication", 1802 0x6f, 4, "Mismatched region to this logical unit", 1803 0x6f, 5, "Region reset count error", 1804 0xffff, 0x0, NULL 1805 }; 1806 1807 1808 /* 1809 * Struct for passing printing information for sense data messages 1810 */ 1811 struct sd_sense_info { 1812 int ssi_severity; 1813 int ssi_pfa_flag; 1814 }; 1815 1816 /* 1817 * Table of function pointers for iostart-side routines. Separate "chains" 1818 * of layered function calls are formed by placing the function pointers 1819 * sequentially in the desired order. Functions are called according to an 1820 * incrementing table index ordering. The last function in each chain must 1821 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1822 * in the sd_iodone_chain[] array. 1823 * 1824 * Note: It may seem more natural to organize both the iostart and iodone 1825 * functions together, into an array of structures (or some similar 1826 * organization) with a common index, rather than two separate arrays which 1827 * must be maintained in synchronization. The purpose of this division is 1828 * to achieve improved performance: individual arrays allows for more 1829 * effective cache line utilization on certain platforms. 1830 */ 1831 1832 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1833 1834 1835 static sd_chain_t sd_iostart_chain[] = { 1836 1837 /* Chain for buf IO for disk drive targets (PM enabled) */ 1838 sd_mapblockaddr_iostart, /* Index: 0 */ 1839 sd_pm_iostart, /* Index: 1 */ 1840 sd_core_iostart, /* Index: 2 */ 1841 1842 /* Chain for buf IO for disk drive targets (PM disabled) */ 1843 sd_mapblockaddr_iostart, /* Index: 3 */ 1844 sd_core_iostart, /* Index: 4 */ 1845 1846 /* 1847 * Chain for buf IO for removable-media or large sector size 1848 * disk drive targets with RMW needed (PM enabled) 1849 */ 1850 sd_mapblockaddr_iostart, /* Index: 5 */ 1851 sd_mapblocksize_iostart, /* Index: 6 */ 1852 sd_pm_iostart, /* Index: 7 */ 1853 sd_core_iostart, /* Index: 8 */ 1854 1855 /* 1856 * Chain for buf IO for removable-media or large sector size 1857 * disk drive targets with RMW needed (PM disabled) 1858 */ 1859 sd_mapblockaddr_iostart, /* Index: 9 */ 1860 sd_mapblocksize_iostart, /* Index: 10 */ 1861 sd_core_iostart, /* Index: 11 */ 1862 1863 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1864 sd_mapblockaddr_iostart, /* Index: 12 */ 1865 sd_checksum_iostart, /* Index: 13 */ 1866 sd_pm_iostart, /* Index: 14 */ 1867 sd_core_iostart, /* Index: 15 */ 1868 1869 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1870 sd_mapblockaddr_iostart, /* Index: 16 */ 1871 sd_checksum_iostart, /* Index: 17 */ 1872 sd_core_iostart, /* Index: 18 */ 1873 1874 /* Chain for USCSI commands (all targets) */ 1875 sd_pm_iostart, /* Index: 19 */ 1876 sd_core_iostart, /* Index: 20 */ 1877 1878 /* Chain for checksumming USCSI commands (all targets) */ 1879 sd_checksum_uscsi_iostart, /* Index: 21 */ 1880 sd_pm_iostart, /* Index: 22 */ 1881 sd_core_iostart, /* Index: 23 */ 1882 1883 /* Chain for "direct" USCSI commands (all targets) */ 1884 sd_core_iostart, /* Index: 24 */ 1885 1886 /* Chain for "direct priority" USCSI commands (all targets) */ 1887 sd_core_iostart, /* Index: 25 */ 1888 1889 /* 1890 * Chain for buf IO for large sector size disk drive targets 1891 * with RMW needed with checksumming (PM enabled) 1892 */ 1893 sd_mapblockaddr_iostart, /* Index: 26 */ 1894 sd_mapblocksize_iostart, /* Index: 27 */ 1895 sd_checksum_iostart, /* Index: 28 */ 1896 sd_pm_iostart, /* Index: 29 */ 1897 sd_core_iostart, /* Index: 30 */ 1898 1899 /* 1900 * Chain for buf IO for large sector size disk drive targets 1901 * with RMW needed with checksumming (PM disabled) 1902 */ 1903 sd_mapblockaddr_iostart, /* Index: 31 */ 1904 sd_mapblocksize_iostart, /* Index: 32 */ 1905 sd_checksum_iostart, /* Index: 33 */ 1906 sd_core_iostart, /* Index: 34 */ 1907 1908 }; 1909 1910 /* 1911 * Macros to locate the first function of each iostart chain in the 1912 * sd_iostart_chain[] array. These are located by the index in the array. 1913 */ 1914 #define SD_CHAIN_DISK_IOSTART 0 1915 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1916 #define SD_CHAIN_MSS_DISK_IOSTART 5 1917 #define SD_CHAIN_RMMEDIA_IOSTART 5 1918 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9 1919 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1920 #define SD_CHAIN_CHKSUM_IOSTART 12 1921 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1922 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1923 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1924 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1925 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1926 #define SD_CHAIN_MSS_CHKSUM_IOSTART 26 1927 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31 1928 1929 1930 /* 1931 * Table of function pointers for the iodone-side routines for the driver- 1932 * internal layering mechanism. The calling sequence for iodone routines 1933 * uses a decrementing table index, so the last routine called in a chain 1934 * must be at the lowest array index location for that chain. The last 1935 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1936 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1937 * of the functions in an iodone side chain must correspond to the ordering 1938 * of the iostart routines for that chain. Note that there is no iodone 1939 * side routine that corresponds to sd_core_iostart(), so there is no 1940 * entry in the table for this. 1941 */ 1942 1943 static sd_chain_t sd_iodone_chain[] = { 1944 1945 /* Chain for buf IO for disk drive targets (PM enabled) */ 1946 sd_buf_iodone, /* Index: 0 */ 1947 sd_mapblockaddr_iodone, /* Index: 1 */ 1948 sd_pm_iodone, /* Index: 2 */ 1949 1950 /* Chain for buf IO for disk drive targets (PM disabled) */ 1951 sd_buf_iodone, /* Index: 3 */ 1952 sd_mapblockaddr_iodone, /* Index: 4 */ 1953 1954 /* 1955 * Chain for buf IO for removable-media or large sector size 1956 * disk drive targets with RMW needed (PM enabled) 1957 */ 1958 sd_buf_iodone, /* Index: 5 */ 1959 sd_mapblockaddr_iodone, /* Index: 6 */ 1960 sd_mapblocksize_iodone, /* Index: 7 */ 1961 sd_pm_iodone, /* Index: 8 */ 1962 1963 /* 1964 * Chain for buf IO for removable-media or large sector size 1965 * disk drive targets with RMW needed (PM disabled) 1966 */ 1967 sd_buf_iodone, /* Index: 9 */ 1968 sd_mapblockaddr_iodone, /* Index: 10 */ 1969 sd_mapblocksize_iodone, /* Index: 11 */ 1970 1971 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1972 sd_buf_iodone, /* Index: 12 */ 1973 sd_mapblockaddr_iodone, /* Index: 13 */ 1974 sd_checksum_iodone, /* Index: 14 */ 1975 sd_pm_iodone, /* Index: 15 */ 1976 1977 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1978 sd_buf_iodone, /* Index: 16 */ 1979 sd_mapblockaddr_iodone, /* Index: 17 */ 1980 sd_checksum_iodone, /* Index: 18 */ 1981 1982 /* Chain for USCSI commands (non-checksum targets) */ 1983 sd_uscsi_iodone, /* Index: 19 */ 1984 sd_pm_iodone, /* Index: 20 */ 1985 1986 /* Chain for USCSI commands (checksum targets) */ 1987 sd_uscsi_iodone, /* Index: 21 */ 1988 sd_checksum_uscsi_iodone, /* Index: 22 */ 1989 sd_pm_iodone, /* Index: 22 */ 1990 1991 /* Chain for "direct" USCSI commands (all targets) */ 1992 sd_uscsi_iodone, /* Index: 24 */ 1993 1994 /* Chain for "direct priority" USCSI commands (all targets) */ 1995 sd_uscsi_iodone, /* Index: 25 */ 1996 1997 /* 1998 * Chain for buf IO for large sector size disk drive targets 1999 * with checksumming (PM enabled) 2000 */ 2001 sd_buf_iodone, /* Index: 26 */ 2002 sd_mapblockaddr_iodone, /* Index: 27 */ 2003 sd_mapblocksize_iodone, /* Index: 28 */ 2004 sd_checksum_iodone, /* Index: 29 */ 2005 sd_pm_iodone, /* Index: 30 */ 2006 2007 /* 2008 * Chain for buf IO for large sector size disk drive targets 2009 * with checksumming (PM disabled) 2010 */ 2011 sd_buf_iodone, /* Index: 31 */ 2012 sd_mapblockaddr_iodone, /* Index: 32 */ 2013 sd_mapblocksize_iodone, /* Index: 33 */ 2014 sd_checksum_iodone, /* Index: 34 */ 2015 }; 2016 2017 2018 /* 2019 * Macros to locate the "first" function in the sd_iodone_chain[] array for 2020 * each iodone-side chain. These are located by the array index, but as the 2021 * iodone side functions are called in a decrementing-index order, the 2022 * highest index number in each chain must be specified (as these correspond 2023 * to the first function in the iodone chain that will be called by the core 2024 * at IO completion time). 2025 */ 2026 2027 #define SD_CHAIN_DISK_IODONE 2 2028 #define SD_CHAIN_DISK_IODONE_NO_PM 4 2029 #define SD_CHAIN_RMMEDIA_IODONE 8 2030 #define SD_CHAIN_MSS_DISK_IODONE 8 2031 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 2032 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11 2033 #define SD_CHAIN_CHKSUM_IODONE 15 2034 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 2035 #define SD_CHAIN_USCSI_CMD_IODONE 20 2036 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 2037 #define SD_CHAIN_DIRECT_CMD_IODONE 24 2038 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 2039 #define SD_CHAIN_MSS_CHKSUM_IODONE 30 2040 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34 2041 2042 2043 2044 /* 2045 * Array to map a layering chain index to the appropriate initpkt routine. 2046 * The redundant entries are present so that the index used for accessing 2047 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2048 * with this table as well. 2049 */ 2050 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 2051 2052 static sd_initpkt_t sd_initpkt_map[] = { 2053 2054 /* Chain for buf IO for disk drive targets (PM enabled) */ 2055 sd_initpkt_for_buf, /* Index: 0 */ 2056 sd_initpkt_for_buf, /* Index: 1 */ 2057 sd_initpkt_for_buf, /* Index: 2 */ 2058 2059 /* Chain for buf IO for disk drive targets (PM disabled) */ 2060 sd_initpkt_for_buf, /* Index: 3 */ 2061 sd_initpkt_for_buf, /* Index: 4 */ 2062 2063 /* 2064 * Chain for buf IO for removable-media or large sector size 2065 * disk drive targets (PM enabled) 2066 */ 2067 sd_initpkt_for_buf, /* Index: 5 */ 2068 sd_initpkt_for_buf, /* Index: 6 */ 2069 sd_initpkt_for_buf, /* Index: 7 */ 2070 sd_initpkt_for_buf, /* Index: 8 */ 2071 2072 /* 2073 * Chain for buf IO for removable-media or large sector size 2074 * disk drive targets (PM disabled) 2075 */ 2076 sd_initpkt_for_buf, /* Index: 9 */ 2077 sd_initpkt_for_buf, /* Index: 10 */ 2078 sd_initpkt_for_buf, /* Index: 11 */ 2079 2080 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2081 sd_initpkt_for_buf, /* Index: 12 */ 2082 sd_initpkt_for_buf, /* Index: 13 */ 2083 sd_initpkt_for_buf, /* Index: 14 */ 2084 sd_initpkt_for_buf, /* Index: 15 */ 2085 2086 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2087 sd_initpkt_for_buf, /* Index: 16 */ 2088 sd_initpkt_for_buf, /* Index: 17 */ 2089 sd_initpkt_for_buf, /* Index: 18 */ 2090 2091 /* Chain for USCSI commands (non-checksum targets) */ 2092 sd_initpkt_for_uscsi, /* Index: 19 */ 2093 sd_initpkt_for_uscsi, /* Index: 20 */ 2094 2095 /* Chain for USCSI commands (checksum targets) */ 2096 sd_initpkt_for_uscsi, /* Index: 21 */ 2097 sd_initpkt_for_uscsi, /* Index: 22 */ 2098 sd_initpkt_for_uscsi, /* Index: 22 */ 2099 2100 /* Chain for "direct" USCSI commands (all targets) */ 2101 sd_initpkt_for_uscsi, /* Index: 24 */ 2102 2103 /* Chain for "direct priority" USCSI commands (all targets) */ 2104 sd_initpkt_for_uscsi, /* Index: 25 */ 2105 2106 /* 2107 * Chain for buf IO for large sector size disk drive targets 2108 * with checksumming (PM enabled) 2109 */ 2110 sd_initpkt_for_buf, /* Index: 26 */ 2111 sd_initpkt_for_buf, /* Index: 27 */ 2112 sd_initpkt_for_buf, /* Index: 28 */ 2113 sd_initpkt_for_buf, /* Index: 29 */ 2114 sd_initpkt_for_buf, /* Index: 30 */ 2115 2116 /* 2117 * Chain for buf IO for large sector size disk drive targets 2118 * with checksumming (PM disabled) 2119 */ 2120 sd_initpkt_for_buf, /* Index: 31 */ 2121 sd_initpkt_for_buf, /* Index: 32 */ 2122 sd_initpkt_for_buf, /* Index: 33 */ 2123 sd_initpkt_for_buf, /* Index: 34 */ 2124 }; 2125 2126 2127 /* 2128 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2129 * The redundant entries are present so that the index used for accessing 2130 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2131 * with this table as well. 2132 */ 2133 typedef void (*sd_destroypkt_t)(struct buf *); 2134 2135 static sd_destroypkt_t sd_destroypkt_map[] = { 2136 2137 /* Chain for buf IO for disk drive targets (PM enabled) */ 2138 sd_destroypkt_for_buf, /* Index: 0 */ 2139 sd_destroypkt_for_buf, /* Index: 1 */ 2140 sd_destroypkt_for_buf, /* Index: 2 */ 2141 2142 /* Chain for buf IO for disk drive targets (PM disabled) */ 2143 sd_destroypkt_for_buf, /* Index: 3 */ 2144 sd_destroypkt_for_buf, /* Index: 4 */ 2145 2146 /* 2147 * Chain for buf IO for removable-media or large sector size 2148 * disk drive targets (PM enabled) 2149 */ 2150 sd_destroypkt_for_buf, /* Index: 5 */ 2151 sd_destroypkt_for_buf, /* Index: 6 */ 2152 sd_destroypkt_for_buf, /* Index: 7 */ 2153 sd_destroypkt_for_buf, /* Index: 8 */ 2154 2155 /* 2156 * Chain for buf IO for removable-media or large sector size 2157 * disk drive targets (PM disabled) 2158 */ 2159 sd_destroypkt_for_buf, /* Index: 9 */ 2160 sd_destroypkt_for_buf, /* Index: 10 */ 2161 sd_destroypkt_for_buf, /* Index: 11 */ 2162 2163 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2164 sd_destroypkt_for_buf, /* Index: 12 */ 2165 sd_destroypkt_for_buf, /* Index: 13 */ 2166 sd_destroypkt_for_buf, /* Index: 14 */ 2167 sd_destroypkt_for_buf, /* Index: 15 */ 2168 2169 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2170 sd_destroypkt_for_buf, /* Index: 16 */ 2171 sd_destroypkt_for_buf, /* Index: 17 */ 2172 sd_destroypkt_for_buf, /* Index: 18 */ 2173 2174 /* Chain for USCSI commands (non-checksum targets) */ 2175 sd_destroypkt_for_uscsi, /* Index: 19 */ 2176 sd_destroypkt_for_uscsi, /* Index: 20 */ 2177 2178 /* Chain for USCSI commands (checksum targets) */ 2179 sd_destroypkt_for_uscsi, /* Index: 21 */ 2180 sd_destroypkt_for_uscsi, /* Index: 22 */ 2181 sd_destroypkt_for_uscsi, /* Index: 22 */ 2182 2183 /* Chain for "direct" USCSI commands (all targets) */ 2184 sd_destroypkt_for_uscsi, /* Index: 24 */ 2185 2186 /* Chain for "direct priority" USCSI commands (all targets) */ 2187 sd_destroypkt_for_uscsi, /* Index: 25 */ 2188 2189 /* 2190 * Chain for buf IO for large sector size disk drive targets 2191 * with checksumming (PM disabled) 2192 */ 2193 sd_destroypkt_for_buf, /* Index: 26 */ 2194 sd_destroypkt_for_buf, /* Index: 27 */ 2195 sd_destroypkt_for_buf, /* Index: 28 */ 2196 sd_destroypkt_for_buf, /* Index: 29 */ 2197 sd_destroypkt_for_buf, /* Index: 30 */ 2198 2199 /* 2200 * Chain for buf IO for large sector size disk drive targets 2201 * with checksumming (PM enabled) 2202 */ 2203 sd_destroypkt_for_buf, /* Index: 31 */ 2204 sd_destroypkt_for_buf, /* Index: 32 */ 2205 sd_destroypkt_for_buf, /* Index: 33 */ 2206 sd_destroypkt_for_buf, /* Index: 34 */ 2207 }; 2208 2209 2210 2211 /* 2212 * Array to map a layering chain index to the appropriate chain "type". 2213 * The chain type indicates a specific property/usage of the chain. 2214 * The redundant entries are present so that the index used for accessing 2215 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2216 * with this table as well. 2217 */ 2218 2219 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2220 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2221 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2222 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2223 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2224 /* (for error recovery) */ 2225 2226 static int sd_chain_type_map[] = { 2227 2228 /* Chain for buf IO for disk drive targets (PM enabled) */ 2229 SD_CHAIN_BUFIO, /* Index: 0 */ 2230 SD_CHAIN_BUFIO, /* Index: 1 */ 2231 SD_CHAIN_BUFIO, /* Index: 2 */ 2232 2233 /* Chain for buf IO for disk drive targets (PM disabled) */ 2234 SD_CHAIN_BUFIO, /* Index: 3 */ 2235 SD_CHAIN_BUFIO, /* Index: 4 */ 2236 2237 /* 2238 * Chain for buf IO for removable-media or large sector size 2239 * disk drive targets (PM enabled) 2240 */ 2241 SD_CHAIN_BUFIO, /* Index: 5 */ 2242 SD_CHAIN_BUFIO, /* Index: 6 */ 2243 SD_CHAIN_BUFIO, /* Index: 7 */ 2244 SD_CHAIN_BUFIO, /* Index: 8 */ 2245 2246 /* 2247 * Chain for buf IO for removable-media or large sector size 2248 * disk drive targets (PM disabled) 2249 */ 2250 SD_CHAIN_BUFIO, /* Index: 9 */ 2251 SD_CHAIN_BUFIO, /* Index: 10 */ 2252 SD_CHAIN_BUFIO, /* Index: 11 */ 2253 2254 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2255 SD_CHAIN_BUFIO, /* Index: 12 */ 2256 SD_CHAIN_BUFIO, /* Index: 13 */ 2257 SD_CHAIN_BUFIO, /* Index: 14 */ 2258 SD_CHAIN_BUFIO, /* Index: 15 */ 2259 2260 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2261 SD_CHAIN_BUFIO, /* Index: 16 */ 2262 SD_CHAIN_BUFIO, /* Index: 17 */ 2263 SD_CHAIN_BUFIO, /* Index: 18 */ 2264 2265 /* Chain for USCSI commands (non-checksum targets) */ 2266 SD_CHAIN_USCSI, /* Index: 19 */ 2267 SD_CHAIN_USCSI, /* Index: 20 */ 2268 2269 /* Chain for USCSI commands (checksum targets) */ 2270 SD_CHAIN_USCSI, /* Index: 21 */ 2271 SD_CHAIN_USCSI, /* Index: 22 */ 2272 SD_CHAIN_USCSI, /* Index: 23 */ 2273 2274 /* Chain for "direct" USCSI commands (all targets) */ 2275 SD_CHAIN_DIRECT, /* Index: 24 */ 2276 2277 /* Chain for "direct priority" USCSI commands (all targets) */ 2278 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2279 2280 /* 2281 * Chain for buf IO for large sector size disk drive targets 2282 * with checksumming (PM enabled) 2283 */ 2284 SD_CHAIN_BUFIO, /* Index: 26 */ 2285 SD_CHAIN_BUFIO, /* Index: 27 */ 2286 SD_CHAIN_BUFIO, /* Index: 28 */ 2287 SD_CHAIN_BUFIO, /* Index: 29 */ 2288 SD_CHAIN_BUFIO, /* Index: 30 */ 2289 2290 /* 2291 * Chain for buf IO for large sector size disk drive targets 2292 * with checksumming (PM disabled) 2293 */ 2294 SD_CHAIN_BUFIO, /* Index: 31 */ 2295 SD_CHAIN_BUFIO, /* Index: 32 */ 2296 SD_CHAIN_BUFIO, /* Index: 33 */ 2297 SD_CHAIN_BUFIO, /* Index: 34 */ 2298 }; 2299 2300 2301 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2302 #define SD_IS_BUFIO(xp) \ 2303 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2304 2305 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2306 #define SD_IS_DIRECT_PRIORITY(xp) \ 2307 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2308 2309 2310 2311 /* 2312 * Struct, array, and macros to map a specific chain to the appropriate 2313 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2314 * 2315 * The sd_chain_index_map[] array is used at attach time to set the various 2316 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2317 * chain to be used with the instance. This allows different instances to use 2318 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2319 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2320 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2321 * dynamically & without the use of locking; and (2) a layer may update the 2322 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2323 * to allow for deferred processing of an IO within the same chain from a 2324 * different execution context. 2325 */ 2326 2327 struct sd_chain_index { 2328 int sci_iostart_index; 2329 int sci_iodone_index; 2330 }; 2331 2332 static struct sd_chain_index sd_chain_index_map[] = { 2333 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2334 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2335 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2336 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2337 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2338 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2339 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2340 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2341 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2342 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2343 { SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE }, 2344 { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM }, 2345 2346 }; 2347 2348 2349 /* 2350 * The following are indexes into the sd_chain_index_map[] array. 2351 */ 2352 2353 /* un->un_buf_chain_type must be set to one of these */ 2354 #define SD_CHAIN_INFO_DISK 0 2355 #define SD_CHAIN_INFO_DISK_NO_PM 1 2356 #define SD_CHAIN_INFO_RMMEDIA 2 2357 #define SD_CHAIN_INFO_MSS_DISK 2 2358 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2359 #define SD_CHAIN_INFO_MSS_DSK_NO_PM 3 2360 #define SD_CHAIN_INFO_CHKSUM 4 2361 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2362 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10 2363 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11 2364 2365 /* un->un_uscsi_chain_type must be set to one of these */ 2366 #define SD_CHAIN_INFO_USCSI_CMD 6 2367 /* USCSI with PM disabled is the same as DIRECT */ 2368 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2369 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2370 2371 /* un->un_direct_chain_type must be set to one of these */ 2372 #define SD_CHAIN_INFO_DIRECT_CMD 8 2373 2374 /* un->un_priority_chain_type must be set to one of these */ 2375 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2376 2377 /* size for devid inquiries */ 2378 #define MAX_INQUIRY_SIZE 0xF0 2379 2380 /* 2381 * Macros used by functions to pass a given buf(9S) struct along to the 2382 * next function in the layering chain for further processing. 2383 * 2384 * In the following macros, passing more than three arguments to the called 2385 * routines causes the optimizer for the SPARC compiler to stop doing tail 2386 * call elimination which results in significant performance degradation. 2387 */ 2388 #define SD_BEGIN_IOSTART(index, un, bp) \ 2389 ((*(sd_iostart_chain[index]))(index, un, bp)) 2390 2391 #define SD_BEGIN_IODONE(index, un, bp) \ 2392 ((*(sd_iodone_chain[index]))(index, un, bp)) 2393 2394 #define SD_NEXT_IOSTART(index, un, bp) \ 2395 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2396 2397 #define SD_NEXT_IODONE(index, un, bp) \ 2398 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2399 2400 /* 2401 * Function: _init 2402 * 2403 * Description: This is the driver _init(9E) entry point. 2404 * 2405 * Return Code: Returns the value from mod_install(9F) or 2406 * ddi_soft_state_init(9F) as appropriate. 2407 * 2408 * Context: Called when driver module loaded. 2409 */ 2410 2411 int 2412 _init(void) 2413 { 2414 int err; 2415 2416 /* establish driver name from module name */ 2417 sd_label = (char *)mod_modname(&modlinkage); 2418 2419 #ifndef XPV_HVM_DRIVER 2420 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2421 SD_MAXUNIT); 2422 if (err != 0) { 2423 return (err); 2424 } 2425 2426 #else /* XPV_HVM_DRIVER */ 2427 /* Remove the leading "hvm_" from the module name */ 2428 ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0); 2429 sd_label += strlen("hvm_"); 2430 2431 #endif /* XPV_HVM_DRIVER */ 2432 2433 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2434 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2435 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2436 2437 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2438 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2439 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2440 2441 /* 2442 * it's ok to init here even for fibre device 2443 */ 2444 sd_scsi_probe_cache_init(); 2445 2446 sd_scsi_target_lun_init(); 2447 2448 /* 2449 * Creating taskq before mod_install ensures that all callers (threads) 2450 * that enter the module after a successful mod_install encounter 2451 * a valid taskq. 2452 */ 2453 sd_taskq_create(); 2454 2455 err = mod_install(&modlinkage); 2456 if (err != 0) { 2457 /* delete taskq if install fails */ 2458 sd_taskq_delete(); 2459 2460 mutex_destroy(&sd_detach_mutex); 2461 mutex_destroy(&sd_log_mutex); 2462 mutex_destroy(&sd_label_mutex); 2463 2464 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2465 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2466 cv_destroy(&sd_tr.srq_inprocess_cv); 2467 2468 sd_scsi_probe_cache_fini(); 2469 2470 sd_scsi_target_lun_fini(); 2471 2472 #ifndef XPV_HVM_DRIVER 2473 ddi_soft_state_fini(&sd_state); 2474 #endif /* !XPV_HVM_DRIVER */ 2475 return (err); 2476 } 2477 2478 return (err); 2479 } 2480 2481 2482 /* 2483 * Function: _fini 2484 * 2485 * Description: This is the driver _fini(9E) entry point. 2486 * 2487 * Return Code: Returns the value from mod_remove(9F) 2488 * 2489 * Context: Called when driver module is unloaded. 2490 */ 2491 2492 int 2493 _fini(void) 2494 { 2495 int err; 2496 2497 if ((err = mod_remove(&modlinkage)) != 0) { 2498 return (err); 2499 } 2500 2501 sd_taskq_delete(); 2502 2503 mutex_destroy(&sd_detach_mutex); 2504 mutex_destroy(&sd_log_mutex); 2505 mutex_destroy(&sd_label_mutex); 2506 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2507 2508 sd_scsi_probe_cache_fini(); 2509 2510 sd_scsi_target_lun_fini(); 2511 2512 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2513 cv_destroy(&sd_tr.srq_inprocess_cv); 2514 2515 #ifndef XPV_HVM_DRIVER 2516 ddi_soft_state_fini(&sd_state); 2517 #endif /* !XPV_HVM_DRIVER */ 2518 2519 return (err); 2520 } 2521 2522 2523 /* 2524 * Function: _info 2525 * 2526 * Description: This is the driver _info(9E) entry point. 2527 * 2528 * Arguments: modinfop - pointer to the driver modinfo structure 2529 * 2530 * Return Code: Returns the value from mod_info(9F). 2531 * 2532 * Context: Kernel thread context 2533 */ 2534 2535 int 2536 _info(struct modinfo *modinfop) 2537 { 2538 return (mod_info(&modlinkage, modinfop)); 2539 } 2540 2541 2542 /* 2543 * The following routines implement the driver message logging facility. 2544 * They provide component- and level- based debug output filtering. 2545 * Output may also be restricted to messages for a single instance by 2546 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2547 * to NULL, then messages for all instances are printed. 2548 * 2549 * These routines have been cloned from each other due to the language 2550 * constraints of macros and variable argument list processing. 2551 */ 2552 2553 2554 /* 2555 * Function: sd_log_err 2556 * 2557 * Description: This routine is called by the SD_ERROR macro for debug 2558 * logging of error conditions. 2559 * 2560 * Arguments: comp - driver component being logged 2561 * dev - pointer to driver info structure 2562 * fmt - error string and format to be logged 2563 */ 2564 2565 static void 2566 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2567 { 2568 va_list ap; 2569 dev_info_t *dev; 2570 2571 ASSERT(un != NULL); 2572 dev = SD_DEVINFO(un); 2573 ASSERT(dev != NULL); 2574 2575 /* 2576 * Filter messages based on the global component and level masks. 2577 * Also print if un matches the value of sd_debug_un, or if 2578 * sd_debug_un is set to NULL. 2579 */ 2580 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2581 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2582 mutex_enter(&sd_log_mutex); 2583 va_start(ap, fmt); 2584 (void) vsprintf(sd_log_buf, fmt, ap); 2585 va_end(ap); 2586 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2587 mutex_exit(&sd_log_mutex); 2588 } 2589 #ifdef SD_FAULT_INJECTION 2590 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2591 if (un->sd_injection_mask & comp) { 2592 mutex_enter(&sd_log_mutex); 2593 va_start(ap, fmt); 2594 (void) vsprintf(sd_log_buf, fmt, ap); 2595 va_end(ap); 2596 sd_injection_log(sd_log_buf, un); 2597 mutex_exit(&sd_log_mutex); 2598 } 2599 #endif 2600 } 2601 2602 2603 /* 2604 * Function: sd_log_info 2605 * 2606 * Description: This routine is called by the SD_INFO macro for debug 2607 * logging of general purpose informational conditions. 2608 * 2609 * Arguments: comp - driver component being logged 2610 * dev - pointer to driver info structure 2611 * fmt - info string and format to be logged 2612 */ 2613 2614 static void 2615 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2616 { 2617 va_list ap; 2618 dev_info_t *dev; 2619 2620 ASSERT(un != NULL); 2621 dev = SD_DEVINFO(un); 2622 ASSERT(dev != NULL); 2623 2624 /* 2625 * Filter messages based on the global component and level masks. 2626 * Also print if un matches the value of sd_debug_un, or if 2627 * sd_debug_un is set to NULL. 2628 */ 2629 if ((sd_component_mask & component) && 2630 (sd_level_mask & SD_LOGMASK_INFO) && 2631 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2632 mutex_enter(&sd_log_mutex); 2633 va_start(ap, fmt); 2634 (void) vsprintf(sd_log_buf, fmt, ap); 2635 va_end(ap); 2636 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2637 mutex_exit(&sd_log_mutex); 2638 } 2639 #ifdef SD_FAULT_INJECTION 2640 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2641 if (un->sd_injection_mask & component) { 2642 mutex_enter(&sd_log_mutex); 2643 va_start(ap, fmt); 2644 (void) vsprintf(sd_log_buf, fmt, ap); 2645 va_end(ap); 2646 sd_injection_log(sd_log_buf, un); 2647 mutex_exit(&sd_log_mutex); 2648 } 2649 #endif 2650 } 2651 2652 2653 /* 2654 * Function: sd_log_trace 2655 * 2656 * Description: This routine is called by the SD_TRACE macro for debug 2657 * logging of trace conditions (i.e. function entry/exit). 2658 * 2659 * Arguments: comp - driver component being logged 2660 * dev - pointer to driver info structure 2661 * fmt - trace string and format to be logged 2662 */ 2663 2664 static void 2665 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2666 { 2667 va_list ap; 2668 dev_info_t *dev; 2669 2670 ASSERT(un != NULL); 2671 dev = SD_DEVINFO(un); 2672 ASSERT(dev != NULL); 2673 2674 /* 2675 * Filter messages based on the global component and level masks. 2676 * Also print if un matches the value of sd_debug_un, or if 2677 * sd_debug_un is set to NULL. 2678 */ 2679 if ((sd_component_mask & component) && 2680 (sd_level_mask & SD_LOGMASK_TRACE) && 2681 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2682 mutex_enter(&sd_log_mutex); 2683 va_start(ap, fmt); 2684 (void) vsprintf(sd_log_buf, fmt, ap); 2685 va_end(ap); 2686 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2687 mutex_exit(&sd_log_mutex); 2688 } 2689 #ifdef SD_FAULT_INJECTION 2690 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2691 if (un->sd_injection_mask & component) { 2692 mutex_enter(&sd_log_mutex); 2693 va_start(ap, fmt); 2694 (void) vsprintf(sd_log_buf, fmt, ap); 2695 va_end(ap); 2696 sd_injection_log(sd_log_buf, un); 2697 mutex_exit(&sd_log_mutex); 2698 } 2699 #endif 2700 } 2701 2702 2703 /* 2704 * Function: sdprobe 2705 * 2706 * Description: This is the driver probe(9e) entry point function. 2707 * 2708 * Arguments: devi - opaque device info handle 2709 * 2710 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2711 * DDI_PROBE_FAILURE: If the probe failed. 2712 * DDI_PROBE_PARTIAL: If the instance is not present now, 2713 * but may be present in the future. 2714 */ 2715 2716 static int 2717 sdprobe(dev_info_t *devi) 2718 { 2719 struct scsi_device *devp; 2720 int rval; 2721 #ifndef XPV_HVM_DRIVER 2722 int instance = ddi_get_instance(devi); 2723 #endif /* !XPV_HVM_DRIVER */ 2724 2725 /* 2726 * if it wasn't for pln, sdprobe could actually be nulldev 2727 * in the "__fibre" case. 2728 */ 2729 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2730 return (DDI_PROBE_DONTCARE); 2731 } 2732 2733 devp = ddi_get_driver_private(devi); 2734 2735 if (devp == NULL) { 2736 /* Ooops... nexus driver is mis-configured... */ 2737 return (DDI_PROBE_FAILURE); 2738 } 2739 2740 #ifndef XPV_HVM_DRIVER 2741 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2742 return (DDI_PROBE_PARTIAL); 2743 } 2744 #endif /* !XPV_HVM_DRIVER */ 2745 2746 /* 2747 * Call the SCSA utility probe routine to see if we actually 2748 * have a target at this SCSI nexus. 2749 */ 2750 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2751 case SCSIPROBE_EXISTS: 2752 switch (devp->sd_inq->inq_dtype) { 2753 case DTYPE_DIRECT: 2754 rval = DDI_PROBE_SUCCESS; 2755 break; 2756 case DTYPE_RODIRECT: 2757 /* CDs etc. Can be removable media */ 2758 rval = DDI_PROBE_SUCCESS; 2759 break; 2760 case DTYPE_OPTICAL: 2761 /* 2762 * Rewritable optical driver HP115AA 2763 * Can also be removable media 2764 */ 2765 2766 /* 2767 * Do not attempt to bind to DTYPE_OPTICAL if 2768 * pre solaris 9 sparc sd behavior is required 2769 * 2770 * If first time through and sd_dtype_optical_bind 2771 * has not been set in /etc/system check properties 2772 */ 2773 2774 if (sd_dtype_optical_bind < 0) { 2775 sd_dtype_optical_bind = ddi_prop_get_int 2776 (DDI_DEV_T_ANY, devi, 0, 2777 "optical-device-bind", 1); 2778 } 2779 2780 if (sd_dtype_optical_bind == 0) { 2781 rval = DDI_PROBE_FAILURE; 2782 } else { 2783 rval = DDI_PROBE_SUCCESS; 2784 } 2785 break; 2786 2787 case DTYPE_NOTPRESENT: 2788 default: 2789 rval = DDI_PROBE_FAILURE; 2790 break; 2791 } 2792 break; 2793 default: 2794 rval = DDI_PROBE_PARTIAL; 2795 break; 2796 } 2797 2798 /* 2799 * This routine checks for resource allocation prior to freeing, 2800 * so it will take care of the "smart probing" case where a 2801 * scsi_probe() may or may not have been issued and will *not* 2802 * free previously-freed resources. 2803 */ 2804 scsi_unprobe(devp); 2805 return (rval); 2806 } 2807 2808 2809 /* 2810 * Function: sdinfo 2811 * 2812 * Description: This is the driver getinfo(9e) entry point function. 2813 * Given the device number, return the devinfo pointer from 2814 * the scsi_device structure or the instance number 2815 * associated with the dev_t. 2816 * 2817 * Arguments: dip - pointer to device info structure 2818 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2819 * DDI_INFO_DEVT2INSTANCE) 2820 * arg - driver dev_t 2821 * resultp - user buffer for request response 2822 * 2823 * Return Code: DDI_SUCCESS 2824 * DDI_FAILURE 2825 */ 2826 /* ARGSUSED */ 2827 static int 2828 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2829 { 2830 struct sd_lun *un; 2831 dev_t dev; 2832 int instance; 2833 int error; 2834 2835 switch (infocmd) { 2836 case DDI_INFO_DEVT2DEVINFO: 2837 dev = (dev_t)arg; 2838 instance = SDUNIT(dev); 2839 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2840 return (DDI_FAILURE); 2841 } 2842 *result = (void *) SD_DEVINFO(un); 2843 error = DDI_SUCCESS; 2844 break; 2845 case DDI_INFO_DEVT2INSTANCE: 2846 dev = (dev_t)arg; 2847 instance = SDUNIT(dev); 2848 *result = (void *)(uintptr_t)instance; 2849 error = DDI_SUCCESS; 2850 break; 2851 default: 2852 error = DDI_FAILURE; 2853 } 2854 return (error); 2855 } 2856 2857 /* 2858 * Function: sd_prop_op 2859 * 2860 * Description: This is the driver prop_op(9e) entry point function. 2861 * Return the number of blocks for the partition in question 2862 * or forward the request to the property facilities. 2863 * 2864 * Arguments: dev - device number 2865 * dip - pointer to device info structure 2866 * prop_op - property operator 2867 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2868 * name - pointer to property name 2869 * valuep - pointer or address of the user buffer 2870 * lengthp - property length 2871 * 2872 * Return Code: DDI_PROP_SUCCESS 2873 * DDI_PROP_NOT_FOUND 2874 * DDI_PROP_UNDEFINED 2875 * DDI_PROP_NO_MEMORY 2876 * DDI_PROP_BUF_TOO_SMALL 2877 */ 2878 2879 static int 2880 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2881 char *name, caddr_t valuep, int *lengthp) 2882 { 2883 struct sd_lun *un; 2884 2885 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL) 2886 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2887 name, valuep, lengthp)); 2888 2889 return (cmlb_prop_op(un->un_cmlbhandle, 2890 dev, dip, prop_op, mod_flags, name, valuep, lengthp, 2891 SDPART(dev), (void *)SD_PATH_DIRECT)); 2892 } 2893 2894 /* 2895 * The following functions are for smart probing: 2896 * sd_scsi_probe_cache_init() 2897 * sd_scsi_probe_cache_fini() 2898 * sd_scsi_clear_probe_cache() 2899 * sd_scsi_probe_with_cache() 2900 */ 2901 2902 /* 2903 * Function: sd_scsi_probe_cache_init 2904 * 2905 * Description: Initializes the probe response cache mutex and head pointer. 2906 * 2907 * Context: Kernel thread context 2908 */ 2909 2910 static void 2911 sd_scsi_probe_cache_init(void) 2912 { 2913 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2914 sd_scsi_probe_cache_head = NULL; 2915 } 2916 2917 2918 /* 2919 * Function: sd_scsi_probe_cache_fini 2920 * 2921 * Description: Frees all resources associated with the probe response cache. 2922 * 2923 * Context: Kernel thread context 2924 */ 2925 2926 static void 2927 sd_scsi_probe_cache_fini(void) 2928 { 2929 struct sd_scsi_probe_cache *cp; 2930 struct sd_scsi_probe_cache *ncp; 2931 2932 /* Clean up our smart probing linked list */ 2933 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2934 ncp = cp->next; 2935 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2936 } 2937 sd_scsi_probe_cache_head = NULL; 2938 mutex_destroy(&sd_scsi_probe_cache_mutex); 2939 } 2940 2941 2942 /* 2943 * Function: sd_scsi_clear_probe_cache 2944 * 2945 * Description: This routine clears the probe response cache. This is 2946 * done when open() returns ENXIO so that when deferred 2947 * attach is attempted (possibly after a device has been 2948 * turned on) we will retry the probe. Since we don't know 2949 * which target we failed to open, we just clear the 2950 * entire cache. 2951 * 2952 * Context: Kernel thread context 2953 */ 2954 2955 static void 2956 sd_scsi_clear_probe_cache(void) 2957 { 2958 struct sd_scsi_probe_cache *cp; 2959 int i; 2960 2961 mutex_enter(&sd_scsi_probe_cache_mutex); 2962 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2963 /* 2964 * Reset all entries to SCSIPROBE_EXISTS. This will 2965 * force probing to be performed the next time 2966 * sd_scsi_probe_with_cache is called. 2967 */ 2968 for (i = 0; i < NTARGETS_WIDE; i++) { 2969 cp->cache[i] = SCSIPROBE_EXISTS; 2970 } 2971 } 2972 mutex_exit(&sd_scsi_probe_cache_mutex); 2973 } 2974 2975 2976 /* 2977 * Function: sd_scsi_probe_with_cache 2978 * 2979 * Description: This routine implements support for a scsi device probe 2980 * with cache. The driver maintains a cache of the target 2981 * responses to scsi probes. If we get no response from a 2982 * target during a probe inquiry, we remember that, and we 2983 * avoid additional calls to scsi_probe on non-zero LUNs 2984 * on the same target until the cache is cleared. By doing 2985 * so we avoid the 1/4 sec selection timeout for nonzero 2986 * LUNs. lun0 of a target is always probed. 2987 * 2988 * Arguments: devp - Pointer to a scsi_device(9S) structure 2989 * waitfunc - indicates what the allocator routines should 2990 * do when resources are not available. This value 2991 * is passed on to scsi_probe() when that routine 2992 * is called. 2993 * 2994 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2995 * otherwise the value returned by scsi_probe(9F). 2996 * 2997 * Context: Kernel thread context 2998 */ 2999 3000 static int 3001 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 3002 { 3003 struct sd_scsi_probe_cache *cp; 3004 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 3005 int lun, tgt; 3006 3007 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 3008 SCSI_ADDR_PROP_LUN, 0); 3009 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 3010 SCSI_ADDR_PROP_TARGET, -1); 3011 3012 /* Make sure caching enabled and target in range */ 3013 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 3014 /* do it the old way (no cache) */ 3015 return (scsi_probe(devp, waitfn)); 3016 } 3017 3018 mutex_enter(&sd_scsi_probe_cache_mutex); 3019 3020 /* Find the cache for this scsi bus instance */ 3021 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 3022 if (cp->pdip == pdip) { 3023 break; 3024 } 3025 } 3026 3027 /* If we can't find a cache for this pdip, create one */ 3028 if (cp == NULL) { 3029 int i; 3030 3031 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 3032 KM_SLEEP); 3033 cp->pdip = pdip; 3034 cp->next = sd_scsi_probe_cache_head; 3035 sd_scsi_probe_cache_head = cp; 3036 for (i = 0; i < NTARGETS_WIDE; i++) { 3037 cp->cache[i] = SCSIPROBE_EXISTS; 3038 } 3039 } 3040 3041 mutex_exit(&sd_scsi_probe_cache_mutex); 3042 3043 /* Recompute the cache for this target if LUN zero */ 3044 if (lun == 0) { 3045 cp->cache[tgt] = SCSIPROBE_EXISTS; 3046 } 3047 3048 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 3049 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 3050 return (SCSIPROBE_NORESP); 3051 } 3052 3053 /* Do the actual probe; save & return the result */ 3054 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 3055 } 3056 3057 3058 /* 3059 * Function: sd_scsi_target_lun_init 3060 * 3061 * Description: Initializes the attached lun chain mutex and head pointer. 3062 * 3063 * Context: Kernel thread context 3064 */ 3065 3066 static void 3067 sd_scsi_target_lun_init(void) 3068 { 3069 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 3070 sd_scsi_target_lun_head = NULL; 3071 } 3072 3073 3074 /* 3075 * Function: sd_scsi_target_lun_fini 3076 * 3077 * Description: Frees all resources associated with the attached lun 3078 * chain 3079 * 3080 * Context: Kernel thread context 3081 */ 3082 3083 static void 3084 sd_scsi_target_lun_fini(void) 3085 { 3086 struct sd_scsi_hba_tgt_lun *cp; 3087 struct sd_scsi_hba_tgt_lun *ncp; 3088 3089 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 3090 ncp = cp->next; 3091 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 3092 } 3093 sd_scsi_target_lun_head = NULL; 3094 mutex_destroy(&sd_scsi_target_lun_mutex); 3095 } 3096 3097 3098 /* 3099 * Function: sd_scsi_get_target_lun_count 3100 * 3101 * Description: This routine will check in the attached lun chain to see 3102 * how many luns are attached on the required SCSI controller 3103 * and target. Currently, some capabilities like tagged queue 3104 * are supported per target based by HBA. So all luns in a 3105 * target have the same capabilities. Based on this assumption, 3106 * sd should only set these capabilities once per target. This 3107 * function is called when sd needs to decide how many luns 3108 * already attached on a target. 3109 * 3110 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 3111 * controller device. 3112 * target - The target ID on the controller's SCSI bus. 3113 * 3114 * Return Code: The number of luns attached on the required target and 3115 * controller. 3116 * -1 if target ID is not in parallel SCSI scope or the given 3117 * dip is not in the chain. 3118 * 3119 * Context: Kernel thread context 3120 */ 3121 3122 static int 3123 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 3124 { 3125 struct sd_scsi_hba_tgt_lun *cp; 3126 3127 if ((target < 0) || (target >= NTARGETS_WIDE)) { 3128 return (-1); 3129 } 3130 3131 mutex_enter(&sd_scsi_target_lun_mutex); 3132 3133 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3134 if (cp->pdip == dip) { 3135 break; 3136 } 3137 } 3138 3139 mutex_exit(&sd_scsi_target_lun_mutex); 3140 3141 if (cp == NULL) { 3142 return (-1); 3143 } 3144 3145 return (cp->nlun[target]); 3146 } 3147 3148 3149 /* 3150 * Function: sd_scsi_update_lun_on_target 3151 * 3152 * Description: This routine is used to update the attached lun chain when a 3153 * lun is attached or detached on a target. 3154 * 3155 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 3156 * controller device. 3157 * target - The target ID on the controller's SCSI bus. 3158 * flag - Indicate the lun is attached or detached. 3159 * 3160 * Context: Kernel thread context 3161 */ 3162 3163 static void 3164 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 3165 { 3166 struct sd_scsi_hba_tgt_lun *cp; 3167 3168 mutex_enter(&sd_scsi_target_lun_mutex); 3169 3170 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3171 if (cp->pdip == dip) { 3172 break; 3173 } 3174 } 3175 3176 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3177 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3178 KM_SLEEP); 3179 cp->pdip = dip; 3180 cp->next = sd_scsi_target_lun_head; 3181 sd_scsi_target_lun_head = cp; 3182 } 3183 3184 mutex_exit(&sd_scsi_target_lun_mutex); 3185 3186 if (cp != NULL) { 3187 if (flag == SD_SCSI_LUN_ATTACH) { 3188 cp->nlun[target] ++; 3189 } else { 3190 cp->nlun[target] --; 3191 } 3192 } 3193 } 3194 3195 3196 /* 3197 * Function: sd_spin_up_unit 3198 * 3199 * Description: Issues the following commands to spin-up the device: 3200 * START STOP UNIT, and INQUIRY. 3201 * 3202 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3203 * structure for this target. 3204 * 3205 * Return Code: 0 - success 3206 * EIO - failure 3207 * EACCES - reservation conflict 3208 * 3209 * Context: Kernel thread context 3210 */ 3211 3212 static int 3213 sd_spin_up_unit(sd_ssc_t *ssc) 3214 { 3215 size_t resid = 0; 3216 int has_conflict = FALSE; 3217 uchar_t *bufaddr; 3218 int status; 3219 struct sd_lun *un; 3220 3221 ASSERT(ssc != NULL); 3222 un = ssc->ssc_un; 3223 ASSERT(un != NULL); 3224 3225 /* 3226 * Send a throwaway START UNIT command. 3227 * 3228 * If we fail on this, we don't care presently what precisely 3229 * is wrong. EMC's arrays will also fail this with a check 3230 * condition (0x2/0x4/0x3) if the device is "inactive," but 3231 * we don't want to fail the attach because it may become 3232 * "active" later. 3233 * We don't know if power condition is supported or not at 3234 * this stage, use START STOP bit. 3235 */ 3236 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 3237 SD_TARGET_START, SD_PATH_DIRECT); 3238 3239 if (status != 0) { 3240 if (status == EACCES) 3241 has_conflict = TRUE; 3242 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3243 } 3244 3245 /* 3246 * Send another INQUIRY command to the target. This is necessary for 3247 * non-removable media direct access devices because their INQUIRY data 3248 * may not be fully qualified until they are spun up (perhaps via the 3249 * START command above). Note: This seems to be needed for some 3250 * legacy devices only.) The INQUIRY command should succeed even if a 3251 * Reservation Conflict is present. 3252 */ 3253 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3254 3255 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid) 3256 != 0) { 3257 kmem_free(bufaddr, SUN_INQSIZE); 3258 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 3259 return (EIO); 3260 } 3261 3262 /* 3263 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3264 * Note that this routine does not return a failure here even if the 3265 * INQUIRY command did not return any data. This is a legacy behavior. 3266 */ 3267 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3268 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3269 } 3270 3271 kmem_free(bufaddr, SUN_INQSIZE); 3272 3273 /* If we hit a reservation conflict above, tell the caller. */ 3274 if (has_conflict == TRUE) { 3275 return (EACCES); 3276 } 3277 3278 return (0); 3279 } 3280 3281 #ifdef _LP64 3282 /* 3283 * Function: sd_enable_descr_sense 3284 * 3285 * Description: This routine attempts to select descriptor sense format 3286 * using the Control mode page. Devices that support 64 bit 3287 * LBAs (for >2TB luns) should also implement descriptor 3288 * sense data so we will call this function whenever we see 3289 * a lun larger than 2TB. If for some reason the device 3290 * supports 64 bit LBAs but doesn't support descriptor sense 3291 * presumably the mode select will fail. Everything will 3292 * continue to work normally except that we will not get 3293 * complete sense data for commands that fail with an LBA 3294 * larger than 32 bits. 3295 * 3296 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3297 * structure for this target. 3298 * 3299 * Context: Kernel thread context only 3300 */ 3301 3302 static void 3303 sd_enable_descr_sense(sd_ssc_t *ssc) 3304 { 3305 uchar_t *header; 3306 struct mode_control_scsi3 *ctrl_bufp; 3307 size_t buflen; 3308 size_t bd_len; 3309 int status; 3310 struct sd_lun *un; 3311 3312 ASSERT(ssc != NULL); 3313 un = ssc->ssc_un; 3314 ASSERT(un != NULL); 3315 3316 /* 3317 * Read MODE SENSE page 0xA, Control Mode Page 3318 */ 3319 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3320 sizeof (struct mode_control_scsi3); 3321 header = kmem_zalloc(buflen, KM_SLEEP); 3322 3323 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 3324 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT); 3325 3326 if (status != 0) { 3327 SD_ERROR(SD_LOG_COMMON, un, 3328 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3329 goto eds_exit; 3330 } 3331 3332 /* 3333 * Determine size of Block Descriptors in order to locate 3334 * the mode page data. ATAPI devices return 0, SCSI devices 3335 * should return MODE_BLK_DESC_LENGTH. 3336 */ 3337 bd_len = ((struct mode_header *)header)->bdesc_length; 3338 3339 /* Clear the mode data length field for MODE SELECT */ 3340 ((struct mode_header *)header)->length = 0; 3341 3342 ctrl_bufp = (struct mode_control_scsi3 *) 3343 (header + MODE_HEADER_LENGTH + bd_len); 3344 3345 /* 3346 * If the page length is smaller than the expected value, 3347 * the target device doesn't support D_SENSE. Bail out here. 3348 */ 3349 if (ctrl_bufp->mode_page.length < 3350 sizeof (struct mode_control_scsi3) - 2) { 3351 SD_ERROR(SD_LOG_COMMON, un, 3352 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3353 goto eds_exit; 3354 } 3355 3356 /* 3357 * Clear PS bit for MODE SELECT 3358 */ 3359 ctrl_bufp->mode_page.ps = 0; 3360 3361 /* 3362 * Set D_SENSE to enable descriptor sense format. 3363 */ 3364 ctrl_bufp->d_sense = 1; 3365 3366 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3367 3368 /* 3369 * Use MODE SELECT to commit the change to the D_SENSE bit 3370 */ 3371 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 3372 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT); 3373 3374 if (status != 0) { 3375 SD_INFO(SD_LOG_COMMON, un, 3376 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3377 } else { 3378 kmem_free(header, buflen); 3379 return; 3380 } 3381 3382 eds_exit: 3383 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3384 kmem_free(header, buflen); 3385 } 3386 3387 /* 3388 * Function: sd_reenable_dsense_task 3389 * 3390 * Description: Re-enable descriptor sense after device or bus reset 3391 * 3392 * Context: Executes in a taskq() thread context 3393 */ 3394 static void 3395 sd_reenable_dsense_task(void *arg) 3396 { 3397 struct sd_lun *un = arg; 3398 sd_ssc_t *ssc; 3399 3400 ASSERT(un != NULL); 3401 3402 ssc = sd_ssc_init(un); 3403 sd_enable_descr_sense(ssc); 3404 sd_ssc_fini(ssc); 3405 } 3406 #endif /* _LP64 */ 3407 3408 /* 3409 * Function: sd_set_mmc_caps 3410 * 3411 * Description: This routine determines if the device is MMC compliant and if 3412 * the device supports CDDA via a mode sense of the CDVD 3413 * capabilities mode page. Also checks if the device is a 3414 * dvdram writable device. 3415 * 3416 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3417 * structure for this target. 3418 * 3419 * Context: Kernel thread context only 3420 */ 3421 3422 static void 3423 sd_set_mmc_caps(sd_ssc_t *ssc) 3424 { 3425 struct mode_header_grp2 *sense_mhp; 3426 uchar_t *sense_page; 3427 caddr_t buf; 3428 int bd_len; 3429 int status; 3430 struct uscsi_cmd com; 3431 int rtn; 3432 uchar_t *out_data_rw, *out_data_hd; 3433 uchar_t *rqbuf_rw, *rqbuf_hd; 3434 uchar_t *out_data_gesn; 3435 int gesn_len; 3436 struct sd_lun *un; 3437 3438 ASSERT(ssc != NULL); 3439 un = ssc->ssc_un; 3440 ASSERT(un != NULL); 3441 3442 /* 3443 * The flags which will be set in this function are - mmc compliant, 3444 * dvdram writable device, cdda support. Initialize them to FALSE 3445 * and if a capability is detected - it will be set to TRUE. 3446 */ 3447 un->un_f_mmc_cap = FALSE; 3448 un->un_f_dvdram_writable_device = FALSE; 3449 un->un_f_cfg_cdda = FALSE; 3450 3451 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3452 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3453 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3454 3455 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3456 3457 if (status != 0) { 3458 /* command failed; just return */ 3459 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3460 return; 3461 } 3462 /* 3463 * If the mode sense request for the CDROM CAPABILITIES 3464 * page (0x2A) succeeds the device is assumed to be MMC. 3465 */ 3466 un->un_f_mmc_cap = TRUE; 3467 3468 /* See if GET STATUS EVENT NOTIFICATION is supported */ 3469 if (un->un_f_mmc_gesn_polling) { 3470 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN; 3471 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP); 3472 3473 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc, 3474 out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS); 3475 3476 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3477 3478 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) { 3479 un->un_f_mmc_gesn_polling = FALSE; 3480 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3481 "sd_set_mmc_caps: gesn not supported " 3482 "%d %x %x %x %x\n", rtn, 3483 out_data_gesn[0], out_data_gesn[1], 3484 out_data_gesn[2], out_data_gesn[3]); 3485 } 3486 3487 kmem_free(out_data_gesn, gesn_len); 3488 } 3489 3490 /* Get to the page data */ 3491 sense_mhp = (struct mode_header_grp2 *)buf; 3492 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3493 sense_mhp->bdesc_length_lo; 3494 if (bd_len > MODE_BLK_DESC_LENGTH) { 3495 /* 3496 * We did not get back the expected block descriptor 3497 * length so we cannot determine if the device supports 3498 * CDDA. However, we still indicate the device is MMC 3499 * according to the successful response to the page 3500 * 0x2A mode sense request. 3501 */ 3502 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3503 "sd_set_mmc_caps: Mode Sense returned " 3504 "invalid block descriptor length\n"); 3505 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3506 return; 3507 } 3508 3509 /* See if read CDDA is supported */ 3510 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3511 bd_len); 3512 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3513 3514 /* See if writing DVD RAM is supported. */ 3515 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3516 if (un->un_f_dvdram_writable_device == TRUE) { 3517 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3518 return; 3519 } 3520 3521 /* 3522 * If the device presents DVD or CD capabilities in the mode 3523 * page, we can return here since a RRD will not have 3524 * these capabilities. 3525 */ 3526 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3527 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3528 return; 3529 } 3530 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3531 3532 /* 3533 * If un->un_f_dvdram_writable_device is still FALSE, 3534 * check for a Removable Rigid Disk (RRD). A RRD 3535 * device is identified by the features RANDOM_WRITABLE and 3536 * HARDWARE_DEFECT_MANAGEMENT. 3537 */ 3538 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3539 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3540 3541 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3542 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3543 RANDOM_WRITABLE, SD_PATH_STANDARD); 3544 3545 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3546 3547 if (rtn != 0) { 3548 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3549 kmem_free(rqbuf_rw, SENSE_LENGTH); 3550 return; 3551 } 3552 3553 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3554 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3555 3556 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3557 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3558 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3559 3560 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3561 3562 if (rtn == 0) { 3563 /* 3564 * We have good information, check for random writable 3565 * and hardware defect features. 3566 */ 3567 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3568 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3569 un->un_f_dvdram_writable_device = TRUE; 3570 } 3571 } 3572 3573 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3574 kmem_free(rqbuf_rw, SENSE_LENGTH); 3575 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3576 kmem_free(rqbuf_hd, SENSE_LENGTH); 3577 } 3578 3579 /* 3580 * Function: sd_check_for_writable_cd 3581 * 3582 * Description: This routine determines if the media in the device is 3583 * writable or not. It uses the get configuration command (0x46) 3584 * to determine if the media is writable 3585 * 3586 * Arguments: un - driver soft state (unit) structure 3587 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3588 * chain and the normal command waitq, or 3589 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3590 * "direct" chain and bypass the normal command 3591 * waitq. 3592 * 3593 * Context: Never called at interrupt context. 3594 */ 3595 3596 static void 3597 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag) 3598 { 3599 struct uscsi_cmd com; 3600 uchar_t *out_data; 3601 uchar_t *rqbuf; 3602 int rtn; 3603 uchar_t *out_data_rw, *out_data_hd; 3604 uchar_t *rqbuf_rw, *rqbuf_hd; 3605 struct mode_header_grp2 *sense_mhp; 3606 uchar_t *sense_page; 3607 caddr_t buf; 3608 int bd_len; 3609 int status; 3610 struct sd_lun *un; 3611 3612 ASSERT(ssc != NULL); 3613 un = ssc->ssc_un; 3614 ASSERT(un != NULL); 3615 ASSERT(mutex_owned(SD_MUTEX(un))); 3616 3617 /* 3618 * Initialize the writable media to false, if configuration info. 3619 * tells us otherwise then only we will set it. 3620 */ 3621 un->un_f_mmc_writable_media = FALSE; 3622 mutex_exit(SD_MUTEX(un)); 3623 3624 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3625 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3626 3627 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH, 3628 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3629 3630 if (rtn != 0) 3631 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3632 3633 mutex_enter(SD_MUTEX(un)); 3634 if (rtn == 0) { 3635 /* 3636 * We have good information, check for writable DVD. 3637 */ 3638 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3639 un->un_f_mmc_writable_media = TRUE; 3640 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3641 kmem_free(rqbuf, SENSE_LENGTH); 3642 return; 3643 } 3644 } 3645 3646 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3647 kmem_free(rqbuf, SENSE_LENGTH); 3648 3649 /* 3650 * Determine if this is a RRD type device. 3651 */ 3652 mutex_exit(SD_MUTEX(un)); 3653 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3654 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3655 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3656 3657 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3658 3659 mutex_enter(SD_MUTEX(un)); 3660 if (status != 0) { 3661 /* command failed; just return */ 3662 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3663 return; 3664 } 3665 3666 /* Get to the page data */ 3667 sense_mhp = (struct mode_header_grp2 *)buf; 3668 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3669 if (bd_len > MODE_BLK_DESC_LENGTH) { 3670 /* 3671 * We did not get back the expected block descriptor length so 3672 * we cannot check the mode page. 3673 */ 3674 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3675 "sd_check_for_writable_cd: Mode Sense returned " 3676 "invalid block descriptor length\n"); 3677 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3678 return; 3679 } 3680 3681 /* 3682 * If the device presents DVD or CD capabilities in the mode 3683 * page, we can return here since a RRD device will not have 3684 * these capabilities. 3685 */ 3686 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3687 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3688 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3689 return; 3690 } 3691 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3692 3693 /* 3694 * If un->un_f_mmc_writable_media is still FALSE, 3695 * check for RRD type media. A RRD device is identified 3696 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3697 */ 3698 mutex_exit(SD_MUTEX(un)); 3699 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3700 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3701 3702 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3703 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3704 RANDOM_WRITABLE, path_flag); 3705 3706 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3707 if (rtn != 0) { 3708 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3709 kmem_free(rqbuf_rw, SENSE_LENGTH); 3710 mutex_enter(SD_MUTEX(un)); 3711 return; 3712 } 3713 3714 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3715 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3716 3717 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3718 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3719 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3720 3721 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3722 mutex_enter(SD_MUTEX(un)); 3723 if (rtn == 0) { 3724 /* 3725 * We have good information, check for random writable 3726 * and hardware defect features as current. 3727 */ 3728 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3729 (out_data_rw[10] & 0x1) && 3730 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3731 (out_data_hd[10] & 0x1)) { 3732 un->un_f_mmc_writable_media = TRUE; 3733 } 3734 } 3735 3736 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3737 kmem_free(rqbuf_rw, SENSE_LENGTH); 3738 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3739 kmem_free(rqbuf_hd, SENSE_LENGTH); 3740 } 3741 3742 /* 3743 * Function: sd_read_unit_properties 3744 * 3745 * Description: The following implements a property lookup mechanism. 3746 * Properties for particular disks (keyed on vendor, model 3747 * and rev numbers) are sought in the sd.conf file via 3748 * sd_process_sdconf_file(), and if not found there, are 3749 * looked for in a list hardcoded in this driver via 3750 * sd_process_sdconf_table() Once located the properties 3751 * are used to update the driver unit structure. 3752 * 3753 * Arguments: un - driver soft state (unit) structure 3754 */ 3755 3756 static void 3757 sd_read_unit_properties(struct sd_lun *un) 3758 { 3759 /* 3760 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3761 * the "sd-config-list" property (from the sd.conf file) or if 3762 * there was not a match for the inquiry vid/pid. If this event 3763 * occurs the static driver configuration table is searched for 3764 * a match. 3765 */ 3766 ASSERT(un != NULL); 3767 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3768 sd_process_sdconf_table(un); 3769 } 3770 3771 /* check for LSI device */ 3772 sd_is_lsi(un); 3773 3774 3775 } 3776 3777 3778 /* 3779 * Function: sd_process_sdconf_file 3780 * 3781 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the 3782 * driver's config file (ie, sd.conf) and update the driver 3783 * soft state structure accordingly. 3784 * 3785 * Arguments: un - driver soft state (unit) structure 3786 * 3787 * Return Code: SD_SUCCESS - The properties were successfully set according 3788 * to the driver configuration file. 3789 * SD_FAILURE - The driver config list was not obtained or 3790 * there was no vid/pid match. This indicates that 3791 * the static config table should be used. 3792 * 3793 * The config file has a property, "sd-config-list". Currently we support 3794 * two kinds of formats. For both formats, the value of this property 3795 * is a list of duplets: 3796 * 3797 * sd-config-list= 3798 * <duplet>, 3799 * [,<duplet>]*; 3800 * 3801 * For the improved format, where 3802 * 3803 * <duplet>:= "<vid+pid>","<tunable-list>" 3804 * 3805 * and 3806 * 3807 * <tunable-list>:= <tunable> [, <tunable> ]*; 3808 * <tunable> = <name> : <value> 3809 * 3810 * The <vid+pid> is the string that is returned by the target device on a 3811 * SCSI inquiry command, the <tunable-list> contains one or more tunables 3812 * to apply to all target devices with the specified <vid+pid>. 3813 * 3814 * Each <tunable> is a "<name> : <value>" pair. 3815 * 3816 * For the old format, the structure of each duplet is as follows: 3817 * 3818 * <duplet>:= "<vid+pid>","<data-property-name_list>" 3819 * 3820 * The first entry of the duplet is the device ID string (the concatenated 3821 * vid & pid; not to be confused with a device_id). This is defined in 3822 * the same way as in the sd_disk_table. 3823 * 3824 * The second part of the duplet is a string that identifies a 3825 * data-property-name-list. The data-property-name-list is defined as 3826 * follows: 3827 * 3828 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3829 * 3830 * The syntax of <data-property-name> depends on the <version> field. 3831 * 3832 * If version = SD_CONF_VERSION_1 we have the following syntax: 3833 * 3834 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3835 * 3836 * where the prop0 value will be used to set prop0 if bit0 set in the 3837 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3838 * 3839 */ 3840 3841 static int 3842 sd_process_sdconf_file(struct sd_lun *un) 3843 { 3844 char **config_list = NULL; 3845 uint_t nelements; 3846 char *vidptr; 3847 int vidlen; 3848 char *dnlist_ptr; 3849 char *dataname_ptr; 3850 char *dataname_lasts; 3851 int *data_list = NULL; 3852 uint_t data_list_len; 3853 int rval = SD_FAILURE; 3854 int i; 3855 3856 ASSERT(un != NULL); 3857 3858 /* Obtain the configuration list associated with the .conf file */ 3859 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un), 3860 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list, 3861 &config_list, &nelements) != DDI_PROP_SUCCESS) { 3862 return (SD_FAILURE); 3863 } 3864 3865 /* 3866 * Compare vids in each duplet to the inquiry vid - if a match is 3867 * made, get the data value and update the soft state structure 3868 * accordingly. 3869 * 3870 * Each duplet should show as a pair of strings, return SD_FAILURE 3871 * otherwise. 3872 */ 3873 if (nelements & 1) { 3874 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3875 "sd-config-list should show as pairs of strings.\n"); 3876 if (config_list) 3877 ddi_prop_free(config_list); 3878 return (SD_FAILURE); 3879 } 3880 3881 for (i = 0; i < nelements; i += 2) { 3882 /* 3883 * Note: The assumption here is that each vid entry is on 3884 * a unique line from its associated duplet. 3885 */ 3886 vidptr = config_list[i]; 3887 vidlen = (int)strlen(vidptr); 3888 if ((vidlen == 0) || 3889 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3890 continue; 3891 } 3892 3893 /* 3894 * dnlist contains 1 or more blank separated 3895 * data-property-name entries 3896 */ 3897 dnlist_ptr = config_list[i + 1]; 3898 3899 if (strchr(dnlist_ptr, ':') != NULL) { 3900 /* 3901 * Decode the improved format sd-config-list. 3902 */ 3903 sd_nvpair_str_decode(un, dnlist_ptr); 3904 } else { 3905 /* 3906 * The old format sd-config-list, loop through all 3907 * data-property-name entries in the 3908 * data-property-name-list 3909 * setting the properties for each. 3910 */ 3911 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t", 3912 &dataname_lasts); dataname_ptr != NULL; 3913 dataname_ptr = sd_strtok_r(NULL, " \t", 3914 &dataname_lasts)) { 3915 int version; 3916 3917 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3918 "sd_process_sdconf_file: disk:%s, " 3919 "data:%s\n", vidptr, dataname_ptr); 3920 3921 /* Get the data list */ 3922 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, 3923 SD_DEVINFO(un), 0, dataname_ptr, &data_list, 3924 &data_list_len) != DDI_PROP_SUCCESS) { 3925 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3926 "sd_process_sdconf_file: data " 3927 "property (%s) has no value\n", 3928 dataname_ptr); 3929 continue; 3930 } 3931 3932 version = data_list[0]; 3933 3934 if (version == SD_CONF_VERSION_1) { 3935 sd_tunables values; 3936 3937 /* Set the properties */ 3938 if (sd_chk_vers1_data(un, data_list[1], 3939 &data_list[2], data_list_len, 3940 dataname_ptr) == SD_SUCCESS) { 3941 sd_get_tunables_from_conf(un, 3942 data_list[1], &data_list[2], 3943 &values); 3944 sd_set_vers1_properties(un, 3945 data_list[1], &values); 3946 rval = SD_SUCCESS; 3947 } else { 3948 rval = SD_FAILURE; 3949 } 3950 } else { 3951 scsi_log(SD_DEVINFO(un), sd_label, 3952 CE_WARN, "data property %s version " 3953 "0x%x is invalid.", 3954 dataname_ptr, version); 3955 rval = SD_FAILURE; 3956 } 3957 if (data_list) 3958 ddi_prop_free(data_list); 3959 } 3960 } 3961 } 3962 3963 /* free up the memory allocated by ddi_prop_lookup_string_array(). */ 3964 if (config_list) { 3965 ddi_prop_free(config_list); 3966 } 3967 3968 return (rval); 3969 } 3970 3971 /* 3972 * Function: sd_nvpair_str_decode() 3973 * 3974 * Description: Parse the improved format sd-config-list to get 3975 * each entry of tunable, which includes a name-value pair. 3976 * Then call sd_set_properties() to set the property. 3977 * 3978 * Arguments: un - driver soft state (unit) structure 3979 * nvpair_str - the tunable list 3980 */ 3981 static void 3982 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) 3983 { 3984 char *nv, *name, *value, *token; 3985 char *nv_lasts, *v_lasts, *x_lasts; 3986 3987 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; 3988 nv = sd_strtok_r(NULL, ",", &nv_lasts)) { 3989 token = sd_strtok_r(nv, ":", &v_lasts); 3990 name = sd_strtok_r(token, " \t", &x_lasts); 3991 token = sd_strtok_r(NULL, ":", &v_lasts); 3992 value = sd_strtok_r(token, " \t", &x_lasts); 3993 if (name == NULL || value == NULL) { 3994 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3995 "sd_nvpair_str_decode: " 3996 "name or value is not valid!\n"); 3997 } else { 3998 sd_set_properties(un, name, value); 3999 } 4000 } 4001 } 4002 4003 /* 4004 * Function: sd_strtok_r() 4005 * 4006 * Description: This function uses strpbrk and strspn to break 4007 * string into tokens on sequentially subsequent calls. Return 4008 * NULL when no non-separator characters remain. The first 4009 * argument is NULL for subsequent calls. 4010 */ 4011 static char * 4012 sd_strtok_r(char *string, const char *sepset, char **lasts) 4013 { 4014 char *q, *r; 4015 4016 /* First or subsequent call */ 4017 if (string == NULL) 4018 string = *lasts; 4019 4020 if (string == NULL) 4021 return (NULL); 4022 4023 /* Skip leading separators */ 4024 q = string + strspn(string, sepset); 4025 4026 if (*q == '\0') 4027 return (NULL); 4028 4029 if ((r = strpbrk(q, sepset)) == NULL) 4030 *lasts = NULL; 4031 else { 4032 *r = '\0'; 4033 *lasts = r + 1; 4034 } 4035 return (q); 4036 } 4037 4038 /* 4039 * Function: sd_set_properties() 4040 * 4041 * Description: Set device properties based on the improved 4042 * format sd-config-list. 4043 * 4044 * Arguments: un - driver soft state (unit) structure 4045 * name - supported tunable name 4046 * value - tunable value 4047 */ 4048 static void 4049 sd_set_properties(struct sd_lun *un, char *name, char *value) 4050 { 4051 char *endptr = NULL; 4052 long val = 0; 4053 4054 if (strcasecmp(name, "cache-nonvolatile") == 0) { 4055 if (strcasecmp(value, "true") == 0) { 4056 un->un_f_suppress_cache_flush = TRUE; 4057 } else if (strcasecmp(value, "false") == 0) { 4058 un->un_f_suppress_cache_flush = FALSE; 4059 } else { 4060 goto value_invalid; 4061 } 4062 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4063 "suppress_cache_flush flag set to %d\n", 4064 un->un_f_suppress_cache_flush); 4065 return; 4066 } 4067 4068 if (strcasecmp(name, "controller-type") == 0) { 4069 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4070 un->un_ctype = val; 4071 } else { 4072 goto value_invalid; 4073 } 4074 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4075 "ctype set to %d\n", un->un_ctype); 4076 return; 4077 } 4078 4079 if (strcasecmp(name, "delay-busy") == 0) { 4080 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4081 un->un_busy_timeout = drv_usectohz(val / 1000); 4082 } else { 4083 goto value_invalid; 4084 } 4085 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4086 "busy_timeout set to %d\n", un->un_busy_timeout); 4087 return; 4088 } 4089 4090 if (strcasecmp(name, "disksort") == 0) { 4091 if (strcasecmp(value, "true") == 0) { 4092 un->un_f_disksort_disabled = FALSE; 4093 } else if (strcasecmp(value, "false") == 0) { 4094 un->un_f_disksort_disabled = TRUE; 4095 } else { 4096 goto value_invalid; 4097 } 4098 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4099 "disksort disabled flag set to %d\n", 4100 un->un_f_disksort_disabled); 4101 return; 4102 } 4103 4104 if (strcasecmp(name, "power-condition") == 0) { 4105 if (strcasecmp(value, "true") == 0) { 4106 un->un_f_power_condition_disabled = FALSE; 4107 } else if (strcasecmp(value, "false") == 0) { 4108 un->un_f_power_condition_disabled = TRUE; 4109 } else { 4110 goto value_invalid; 4111 } 4112 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4113 "power condition disabled flag set to %d\n", 4114 un->un_f_power_condition_disabled); 4115 return; 4116 } 4117 4118 if (strcasecmp(name, "timeout-releasereservation") == 0) { 4119 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4120 un->un_reserve_release_time = val; 4121 } else { 4122 goto value_invalid; 4123 } 4124 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4125 "reservation release timeout set to %d\n", 4126 un->un_reserve_release_time); 4127 return; 4128 } 4129 4130 if (strcasecmp(name, "reset-lun") == 0) { 4131 if (strcasecmp(value, "true") == 0) { 4132 un->un_f_lun_reset_enabled = TRUE; 4133 } else if (strcasecmp(value, "false") == 0) { 4134 un->un_f_lun_reset_enabled = FALSE; 4135 } else { 4136 goto value_invalid; 4137 } 4138 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4139 "lun reset enabled flag set to %d\n", 4140 un->un_f_lun_reset_enabled); 4141 return; 4142 } 4143 4144 if (strcasecmp(name, "retries-busy") == 0) { 4145 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4146 un->un_busy_retry_count = val; 4147 } else { 4148 goto value_invalid; 4149 } 4150 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4151 "busy retry count set to %d\n", un->un_busy_retry_count); 4152 return; 4153 } 4154 4155 if (strcasecmp(name, "retries-timeout") == 0) { 4156 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4157 un->un_retry_count = val; 4158 } else { 4159 goto value_invalid; 4160 } 4161 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4162 "timeout retry count set to %d\n", un->un_retry_count); 4163 return; 4164 } 4165 4166 if (strcasecmp(name, "retries-notready") == 0) { 4167 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4168 un->un_notready_retry_count = val; 4169 } else { 4170 goto value_invalid; 4171 } 4172 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4173 "notready retry count set to %d\n", 4174 un->un_notready_retry_count); 4175 return; 4176 } 4177 4178 if (strcasecmp(name, "retries-reset") == 0) { 4179 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4180 un->un_reset_retry_count = val; 4181 } else { 4182 goto value_invalid; 4183 } 4184 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4185 "reset retry count set to %d\n", 4186 un->un_reset_retry_count); 4187 return; 4188 } 4189 4190 if (strcasecmp(name, "throttle-max") == 0) { 4191 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4192 un->un_saved_throttle = un->un_throttle = val; 4193 } else { 4194 goto value_invalid; 4195 } 4196 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4197 "throttle set to %d\n", un->un_throttle); 4198 } 4199 4200 if (strcasecmp(name, "throttle-min") == 0) { 4201 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4202 un->un_min_throttle = val; 4203 } else { 4204 goto value_invalid; 4205 } 4206 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4207 "min throttle set to %d\n", un->un_min_throttle); 4208 } 4209 4210 if (strcasecmp(name, "rmw-type") == 0) { 4211 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 4212 un->un_f_rmw_type = val; 4213 } else { 4214 goto value_invalid; 4215 } 4216 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4217 "RMW type set to %d\n", un->un_f_rmw_type); 4218 } 4219 4220 /* 4221 * Validate the throttle values. 4222 * If any of the numbers are invalid, set everything to defaults. 4223 */ 4224 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4225 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4226 (un->un_min_throttle > un->un_throttle)) { 4227 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4228 un->un_min_throttle = sd_min_throttle; 4229 } 4230 4231 if (strcasecmp(name, "mmc-gesn-polling") == 0) { 4232 if (strcasecmp(value, "true") == 0) { 4233 un->un_f_mmc_gesn_polling = TRUE; 4234 } else if (strcasecmp(value, "false") == 0) { 4235 un->un_f_mmc_gesn_polling = FALSE; 4236 } else { 4237 goto value_invalid; 4238 } 4239 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4240 "mmc-gesn-polling set to %d\n", 4241 un->un_f_mmc_gesn_polling); 4242 } 4243 4244 return; 4245 4246 value_invalid: 4247 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 4248 "value of prop %s is invalid\n", name); 4249 } 4250 4251 /* 4252 * Function: sd_get_tunables_from_conf() 4253 * 4254 * 4255 * This function reads the data list from the sd.conf file and pulls 4256 * the values that can have numeric values as arguments and places 4257 * the values in the appropriate sd_tunables member. 4258 * Since the order of the data list members varies across platforms 4259 * This function reads them from the data list in a platform specific 4260 * order and places them into the correct sd_tunable member that is 4261 * consistent across all platforms. 4262 */ 4263 static void 4264 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 4265 sd_tunables *values) 4266 { 4267 int i; 4268 int mask; 4269 4270 bzero(values, sizeof (sd_tunables)); 4271 4272 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4273 4274 mask = 1 << i; 4275 if (mask > flags) { 4276 break; 4277 } 4278 4279 switch (mask & flags) { 4280 case 0: /* This mask bit not set in flags */ 4281 continue; 4282 case SD_CONF_BSET_THROTTLE: 4283 values->sdt_throttle = data_list[i]; 4284 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4285 "sd_get_tunables_from_conf: throttle = %d\n", 4286 values->sdt_throttle); 4287 break; 4288 case SD_CONF_BSET_CTYPE: 4289 values->sdt_ctype = data_list[i]; 4290 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4291 "sd_get_tunables_from_conf: ctype = %d\n", 4292 values->sdt_ctype); 4293 break; 4294 case SD_CONF_BSET_NRR_COUNT: 4295 values->sdt_not_rdy_retries = data_list[i]; 4296 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4297 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 4298 values->sdt_not_rdy_retries); 4299 break; 4300 case SD_CONF_BSET_BSY_RETRY_COUNT: 4301 values->sdt_busy_retries = data_list[i]; 4302 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4303 "sd_get_tunables_from_conf: busy_retries = %d\n", 4304 values->sdt_busy_retries); 4305 break; 4306 case SD_CONF_BSET_RST_RETRIES: 4307 values->sdt_reset_retries = data_list[i]; 4308 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4309 "sd_get_tunables_from_conf: reset_retries = %d\n", 4310 values->sdt_reset_retries); 4311 break; 4312 case SD_CONF_BSET_RSV_REL_TIME: 4313 values->sdt_reserv_rel_time = data_list[i]; 4314 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4315 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 4316 values->sdt_reserv_rel_time); 4317 break; 4318 case SD_CONF_BSET_MIN_THROTTLE: 4319 values->sdt_min_throttle = data_list[i]; 4320 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4321 "sd_get_tunables_from_conf: min_throttle = %d\n", 4322 values->sdt_min_throttle); 4323 break; 4324 case SD_CONF_BSET_DISKSORT_DISABLED: 4325 values->sdt_disk_sort_dis = data_list[i]; 4326 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4327 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 4328 values->sdt_disk_sort_dis); 4329 break; 4330 case SD_CONF_BSET_LUN_RESET_ENABLED: 4331 values->sdt_lun_reset_enable = data_list[i]; 4332 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4333 "sd_get_tunables_from_conf: lun_reset_enable = %d" 4334 "\n", values->sdt_lun_reset_enable); 4335 break; 4336 case SD_CONF_BSET_CACHE_IS_NV: 4337 values->sdt_suppress_cache_flush = data_list[i]; 4338 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4339 "sd_get_tunables_from_conf: \ 4340 suppress_cache_flush = %d" 4341 "\n", values->sdt_suppress_cache_flush); 4342 break; 4343 case SD_CONF_BSET_PC_DISABLED: 4344 values->sdt_disk_sort_dis = data_list[i]; 4345 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4346 "sd_get_tunables_from_conf: power_condition_dis = " 4347 "%d\n", values->sdt_power_condition_dis); 4348 break; 4349 } 4350 } 4351 } 4352 4353 /* 4354 * Function: sd_process_sdconf_table 4355 * 4356 * Description: Search the static configuration table for a match on the 4357 * inquiry vid/pid and update the driver soft state structure 4358 * according to the table property values for the device. 4359 * 4360 * The form of a configuration table entry is: 4361 * <vid+pid>,<flags>,<property-data> 4362 * "SEAGATE ST42400N",1,0x40000, 4363 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 4364 * 4365 * Arguments: un - driver soft state (unit) structure 4366 */ 4367 4368 static void 4369 sd_process_sdconf_table(struct sd_lun *un) 4370 { 4371 char *id = NULL; 4372 int table_index; 4373 int idlen; 4374 4375 ASSERT(un != NULL); 4376 for (table_index = 0; table_index < sd_disk_table_size; 4377 table_index++) { 4378 id = sd_disk_table[table_index].device_id; 4379 idlen = strlen(id); 4380 if (idlen == 0) { 4381 continue; 4382 } 4383 4384 /* 4385 * The static configuration table currently does not 4386 * implement version 10 properties. Additionally, 4387 * multiple data-property-name entries are not 4388 * implemented in the static configuration table. 4389 */ 4390 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4391 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4392 "sd_process_sdconf_table: disk %s\n", id); 4393 sd_set_vers1_properties(un, 4394 sd_disk_table[table_index].flags, 4395 sd_disk_table[table_index].properties); 4396 break; 4397 } 4398 } 4399 } 4400 4401 4402 /* 4403 * Function: sd_sdconf_id_match 4404 * 4405 * Description: This local function implements a case sensitive vid/pid 4406 * comparison as well as the boundary cases of wild card and 4407 * multiple blanks. 4408 * 4409 * Note: An implicit assumption made here is that the scsi 4410 * inquiry structure will always keep the vid, pid and 4411 * revision strings in consecutive sequence, so they can be 4412 * read as a single string. If this assumption is not the 4413 * case, a separate string, to be used for the check, needs 4414 * to be built with these strings concatenated. 4415 * 4416 * Arguments: un - driver soft state (unit) structure 4417 * id - table or config file vid/pid 4418 * idlen - length of the vid/pid (bytes) 4419 * 4420 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4421 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4422 */ 4423 4424 static int 4425 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 4426 { 4427 struct scsi_inquiry *sd_inq; 4428 int rval = SD_SUCCESS; 4429 4430 ASSERT(un != NULL); 4431 ASSERT(id != NULL); 4432 sd_inq = SD_INQUIRY(un); 4433 4434 /* 4435 * We use the inq_vid as a pointer to a buffer containing the 4436 * vid and pid and use the entire vid/pid length of the table 4437 * entry for the comparison. This works because the inq_pid 4438 * data member follows inq_vid in the scsi_inquiry structure. 4439 */ 4440 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 4441 /* 4442 * The user id string is compared to the inquiry vid/pid 4443 * using a case insensitive comparison and ignoring 4444 * multiple spaces. 4445 */ 4446 rval = sd_blank_cmp(un, id, idlen); 4447 if (rval != SD_SUCCESS) { 4448 /* 4449 * User id strings that start and end with a "*" 4450 * are a special case. These do not have a 4451 * specific vendor, and the product string can 4452 * appear anywhere in the 16 byte PID portion of 4453 * the inquiry data. This is a simple strstr() 4454 * type search for the user id in the inquiry data. 4455 */ 4456 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 4457 char *pidptr = &id[1]; 4458 int i; 4459 int j; 4460 int pidstrlen = idlen - 2; 4461 j = sizeof (SD_INQUIRY(un)->inq_pid) - 4462 pidstrlen; 4463 4464 if (j < 0) { 4465 return (SD_FAILURE); 4466 } 4467 for (i = 0; i < j; i++) { 4468 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 4469 pidptr, pidstrlen) == 0) { 4470 rval = SD_SUCCESS; 4471 break; 4472 } 4473 } 4474 } 4475 } 4476 } 4477 return (rval); 4478 } 4479 4480 4481 /* 4482 * Function: sd_blank_cmp 4483 * 4484 * Description: If the id string starts and ends with a space, treat 4485 * multiple consecutive spaces as equivalent to a single 4486 * space. For example, this causes a sd_disk_table entry 4487 * of " NEC CDROM " to match a device's id string of 4488 * "NEC CDROM". 4489 * 4490 * Note: The success exit condition for this routine is if 4491 * the pointer to the table entry is '\0' and the cnt of 4492 * the inquiry length is zero. This will happen if the inquiry 4493 * string returned by the device is padded with spaces to be 4494 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 4495 * SCSI spec states that the inquiry string is to be padded with 4496 * spaces. 4497 * 4498 * Arguments: un - driver soft state (unit) structure 4499 * id - table or config file vid/pid 4500 * idlen - length of the vid/pid (bytes) 4501 * 4502 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4503 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4504 */ 4505 4506 static int 4507 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 4508 { 4509 char *p1; 4510 char *p2; 4511 int cnt; 4512 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 4513 sizeof (SD_INQUIRY(un)->inq_pid); 4514 4515 ASSERT(un != NULL); 4516 p2 = SD_INQUIRY(un)->inq_vid; 4517 ASSERT(id != NULL); 4518 p1 = id; 4519 4520 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 4521 /* 4522 * Note: string p1 is terminated by a NUL but string p2 4523 * isn't. The end of p2 is determined by cnt. 4524 */ 4525 for (;;) { 4526 /* skip over any extra blanks in both strings */ 4527 while ((*p1 != '\0') && (*p1 == ' ')) { 4528 p1++; 4529 } 4530 while ((cnt != 0) && (*p2 == ' ')) { 4531 p2++; 4532 cnt--; 4533 } 4534 4535 /* compare the two strings */ 4536 if ((cnt == 0) || 4537 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 4538 break; 4539 } 4540 while ((cnt > 0) && 4541 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 4542 p1++; 4543 p2++; 4544 cnt--; 4545 } 4546 } 4547 } 4548 4549 /* return SD_SUCCESS if both strings match */ 4550 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 4551 } 4552 4553 4554 /* 4555 * Function: sd_chk_vers1_data 4556 * 4557 * Description: Verify the version 1 device properties provided by the 4558 * user via the configuration file 4559 * 4560 * Arguments: un - driver soft state (unit) structure 4561 * flags - integer mask indicating properties to be set 4562 * prop_list - integer list of property values 4563 * list_len - number of the elements 4564 * 4565 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4566 * SD_FAILURE - Indicates the user provided data is invalid 4567 */ 4568 4569 static int 4570 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4571 int list_len, char *dataname_ptr) 4572 { 4573 int i; 4574 int mask = 1; 4575 int index = 0; 4576 4577 ASSERT(un != NULL); 4578 4579 /* Check for a NULL property name and list */ 4580 if (dataname_ptr == NULL) { 4581 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4582 "sd_chk_vers1_data: NULL data property name."); 4583 return (SD_FAILURE); 4584 } 4585 if (prop_list == NULL) { 4586 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4587 "sd_chk_vers1_data: %s NULL data property list.", 4588 dataname_ptr); 4589 return (SD_FAILURE); 4590 } 4591 4592 /* Display a warning if undefined bits are set in the flags */ 4593 if (flags & ~SD_CONF_BIT_MASK) { 4594 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4595 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4596 "Properties not set.", 4597 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4598 return (SD_FAILURE); 4599 } 4600 4601 /* 4602 * Verify the length of the list by identifying the highest bit set 4603 * in the flags and validating that the property list has a length 4604 * up to the index of this bit. 4605 */ 4606 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4607 if (flags & mask) { 4608 index++; 4609 } 4610 mask = 1 << i; 4611 } 4612 if (list_len < (index + 2)) { 4613 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4614 "sd_chk_vers1_data: " 4615 "Data property list %s size is incorrect. " 4616 "Properties not set.", dataname_ptr); 4617 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4618 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4619 return (SD_FAILURE); 4620 } 4621 return (SD_SUCCESS); 4622 } 4623 4624 4625 /* 4626 * Function: sd_set_vers1_properties 4627 * 4628 * Description: Set version 1 device properties based on a property list 4629 * retrieved from the driver configuration file or static 4630 * configuration table. Version 1 properties have the format: 4631 * 4632 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4633 * 4634 * where the prop0 value will be used to set prop0 if bit0 4635 * is set in the flags 4636 * 4637 * Arguments: un - driver soft state (unit) structure 4638 * flags - integer mask indicating properties to be set 4639 * prop_list - integer list of property values 4640 */ 4641 4642 static void 4643 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4644 { 4645 ASSERT(un != NULL); 4646 4647 /* 4648 * Set the flag to indicate cache is to be disabled. An attempt 4649 * to disable the cache via sd_cache_control() will be made 4650 * later during attach once the basic initialization is complete. 4651 */ 4652 if (flags & SD_CONF_BSET_NOCACHE) { 4653 un->un_f_opt_disable_cache = TRUE; 4654 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4655 "sd_set_vers1_properties: caching disabled flag set\n"); 4656 } 4657 4658 /* CD-specific configuration parameters */ 4659 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4660 un->un_f_cfg_playmsf_bcd = TRUE; 4661 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4662 "sd_set_vers1_properties: playmsf_bcd set\n"); 4663 } 4664 if (flags & SD_CONF_BSET_READSUB_BCD) { 4665 un->un_f_cfg_readsub_bcd = TRUE; 4666 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4667 "sd_set_vers1_properties: readsub_bcd set\n"); 4668 } 4669 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4670 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4671 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4672 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4673 } 4674 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4675 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4676 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4677 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4678 } 4679 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4680 un->un_f_cfg_no_read_header = TRUE; 4681 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4682 "sd_set_vers1_properties: no_read_header set\n"); 4683 } 4684 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4685 un->un_f_cfg_read_cd_xd4 = TRUE; 4686 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4687 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4688 } 4689 4690 /* Support for devices which do not have valid/unique serial numbers */ 4691 if (flags & SD_CONF_BSET_FAB_DEVID) { 4692 un->un_f_opt_fab_devid = TRUE; 4693 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4694 "sd_set_vers1_properties: fab_devid bit set\n"); 4695 } 4696 4697 /* Support for user throttle configuration */ 4698 if (flags & SD_CONF_BSET_THROTTLE) { 4699 ASSERT(prop_list != NULL); 4700 un->un_saved_throttle = un->un_throttle = 4701 prop_list->sdt_throttle; 4702 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4703 "sd_set_vers1_properties: throttle set to %d\n", 4704 prop_list->sdt_throttle); 4705 } 4706 4707 /* Set the per disk retry count according to the conf file or table. */ 4708 if (flags & SD_CONF_BSET_NRR_COUNT) { 4709 ASSERT(prop_list != NULL); 4710 if (prop_list->sdt_not_rdy_retries) { 4711 un->un_notready_retry_count = 4712 prop_list->sdt_not_rdy_retries; 4713 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4714 "sd_set_vers1_properties: not ready retry count" 4715 " set to %d\n", un->un_notready_retry_count); 4716 } 4717 } 4718 4719 /* The controller type is reported for generic disk driver ioctls */ 4720 if (flags & SD_CONF_BSET_CTYPE) { 4721 ASSERT(prop_list != NULL); 4722 switch (prop_list->sdt_ctype) { 4723 case CTYPE_CDROM: 4724 un->un_ctype = prop_list->sdt_ctype; 4725 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4726 "sd_set_vers1_properties: ctype set to " 4727 "CTYPE_CDROM\n"); 4728 break; 4729 case CTYPE_CCS: 4730 un->un_ctype = prop_list->sdt_ctype; 4731 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4732 "sd_set_vers1_properties: ctype set to " 4733 "CTYPE_CCS\n"); 4734 break; 4735 case CTYPE_ROD: /* RW optical */ 4736 un->un_ctype = prop_list->sdt_ctype; 4737 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4738 "sd_set_vers1_properties: ctype set to " 4739 "CTYPE_ROD\n"); 4740 break; 4741 default: 4742 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4743 "sd_set_vers1_properties: Could not set " 4744 "invalid ctype value (%d)", 4745 prop_list->sdt_ctype); 4746 } 4747 } 4748 4749 /* Purple failover timeout */ 4750 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4751 ASSERT(prop_list != NULL); 4752 un->un_busy_retry_count = 4753 prop_list->sdt_busy_retries; 4754 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4755 "sd_set_vers1_properties: " 4756 "busy retry count set to %d\n", 4757 un->un_busy_retry_count); 4758 } 4759 4760 /* Purple reset retry count */ 4761 if (flags & SD_CONF_BSET_RST_RETRIES) { 4762 ASSERT(prop_list != NULL); 4763 un->un_reset_retry_count = 4764 prop_list->sdt_reset_retries; 4765 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4766 "sd_set_vers1_properties: " 4767 "reset retry count set to %d\n", 4768 un->un_reset_retry_count); 4769 } 4770 4771 /* Purple reservation release timeout */ 4772 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4773 ASSERT(prop_list != NULL); 4774 un->un_reserve_release_time = 4775 prop_list->sdt_reserv_rel_time; 4776 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4777 "sd_set_vers1_properties: " 4778 "reservation release timeout set to %d\n", 4779 un->un_reserve_release_time); 4780 } 4781 4782 /* 4783 * Driver flag telling the driver to verify that no commands are pending 4784 * for a device before issuing a Test Unit Ready. This is a workaround 4785 * for a firmware bug in some Seagate eliteI drives. 4786 */ 4787 if (flags & SD_CONF_BSET_TUR_CHECK) { 4788 un->un_f_cfg_tur_check = TRUE; 4789 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4790 "sd_set_vers1_properties: tur queue check set\n"); 4791 } 4792 4793 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4794 un->un_min_throttle = prop_list->sdt_min_throttle; 4795 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4796 "sd_set_vers1_properties: min throttle set to %d\n", 4797 un->un_min_throttle); 4798 } 4799 4800 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4801 un->un_f_disksort_disabled = 4802 (prop_list->sdt_disk_sort_dis != 0) ? 4803 TRUE : FALSE; 4804 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4805 "sd_set_vers1_properties: disksort disabled " 4806 "flag set to %d\n", 4807 prop_list->sdt_disk_sort_dis); 4808 } 4809 4810 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4811 un->un_f_lun_reset_enabled = 4812 (prop_list->sdt_lun_reset_enable != 0) ? 4813 TRUE : FALSE; 4814 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4815 "sd_set_vers1_properties: lun reset enabled " 4816 "flag set to %d\n", 4817 prop_list->sdt_lun_reset_enable); 4818 } 4819 4820 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4821 un->un_f_suppress_cache_flush = 4822 (prop_list->sdt_suppress_cache_flush != 0) ? 4823 TRUE : FALSE; 4824 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4825 "sd_set_vers1_properties: suppress_cache_flush " 4826 "flag set to %d\n", 4827 prop_list->sdt_suppress_cache_flush); 4828 } 4829 4830 if (flags & SD_CONF_BSET_PC_DISABLED) { 4831 un->un_f_power_condition_disabled = 4832 (prop_list->sdt_power_condition_dis != 0) ? 4833 TRUE : FALSE; 4834 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4835 "sd_set_vers1_properties: power_condition_disabled " 4836 "flag set to %d\n", 4837 prop_list->sdt_power_condition_dis); 4838 } 4839 4840 /* 4841 * Validate the throttle values. 4842 * If any of the numbers are invalid, set everything to defaults. 4843 */ 4844 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4845 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4846 (un->un_min_throttle > un->un_throttle)) { 4847 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4848 un->un_min_throttle = sd_min_throttle; 4849 } 4850 } 4851 4852 /* 4853 * Function: sd_is_lsi() 4854 * 4855 * Description: Check for lsi devices, step through the static device 4856 * table to match vid/pid. 4857 * 4858 * Args: un - ptr to sd_lun 4859 * 4860 * Notes: When creating new LSI property, need to add the new LSI property 4861 * to this function. 4862 */ 4863 static void 4864 sd_is_lsi(struct sd_lun *un) 4865 { 4866 char *id = NULL; 4867 int table_index; 4868 int idlen; 4869 void *prop; 4870 4871 ASSERT(un != NULL); 4872 for (table_index = 0; table_index < sd_disk_table_size; 4873 table_index++) { 4874 id = sd_disk_table[table_index].device_id; 4875 idlen = strlen(id); 4876 if (idlen == 0) { 4877 continue; 4878 } 4879 4880 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4881 prop = sd_disk_table[table_index].properties; 4882 if (prop == &lsi_properties || 4883 prop == &lsi_oem_properties || 4884 prop == &lsi_properties_scsi || 4885 prop == &symbios_properties) { 4886 un->un_f_cfg_is_lsi = TRUE; 4887 } 4888 break; 4889 } 4890 } 4891 } 4892 4893 /* 4894 * Function: sd_get_physical_geometry 4895 * 4896 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4897 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4898 * target, and use this information to initialize the physical 4899 * geometry cache specified by pgeom_p. 4900 * 4901 * MODE SENSE is an optional command, so failure in this case 4902 * does not necessarily denote an error. We want to use the 4903 * MODE SENSE commands to derive the physical geometry of the 4904 * device, but if either command fails, the logical geometry is 4905 * used as the fallback for disk label geometry in cmlb. 4906 * 4907 * This requires that un->un_blockcount and un->un_tgt_blocksize 4908 * have already been initialized for the current target and 4909 * that the current values be passed as args so that we don't 4910 * end up ever trying to use -1 as a valid value. This could 4911 * happen if either value is reset while we're not holding 4912 * the mutex. 4913 * 4914 * Arguments: un - driver soft state (unit) structure 4915 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4916 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4917 * to use the USCSI "direct" chain and bypass the normal 4918 * command waitq. 4919 * 4920 * Context: Kernel thread only (can sleep). 4921 */ 4922 4923 static int 4924 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4925 diskaddr_t capacity, int lbasize, int path_flag) 4926 { 4927 struct mode_format *page3p; 4928 struct mode_geometry *page4p; 4929 struct mode_header *headerp; 4930 int sector_size; 4931 int nsect; 4932 int nhead; 4933 int ncyl; 4934 int intrlv; 4935 int spc; 4936 diskaddr_t modesense_capacity; 4937 int rpm; 4938 int bd_len; 4939 int mode_header_length; 4940 uchar_t *p3bufp; 4941 uchar_t *p4bufp; 4942 int cdbsize; 4943 int ret = EIO; 4944 sd_ssc_t *ssc; 4945 int status; 4946 4947 ASSERT(un != NULL); 4948 4949 if (lbasize == 0) { 4950 if (ISCD(un)) { 4951 lbasize = 2048; 4952 } else { 4953 lbasize = un->un_sys_blocksize; 4954 } 4955 } 4956 pgeom_p->g_secsize = (unsigned short)lbasize; 4957 4958 /* 4959 * If the unit is a cd/dvd drive or a SBC-3 block device, 4960 * MODE SENSE page three and MODE SENSE page four are reserved 4961 * (see SBC spec and MMC spec). To prevent soft errors just return 4962 * using the default LBA size. 4963 */ 4964 if (ISCD(un) || (un->un_f_sbc3_supported == TRUE) || 4965 (un->un_f_is_solid_state == TRUE)) 4966 return (ret); 4967 4968 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4969 4970 /* 4971 * Retrieve MODE SENSE page 3 - Format Device Page 4972 */ 4973 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4974 ssc = sd_ssc_init(un); 4975 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp, 4976 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag); 4977 if (status != 0) { 4978 SD_ERROR(SD_LOG_COMMON, un, 4979 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4980 goto page3_exit; 4981 } 4982 4983 /* 4984 * Determine size of Block Descriptors in order to locate the mode 4985 * page data. ATAPI devices return 0, SCSI devices should return 4986 * MODE_BLK_DESC_LENGTH. 4987 */ 4988 headerp = (struct mode_header *)p3bufp; 4989 if (un->un_f_cfg_is_atapi == TRUE) { 4990 struct mode_header_grp2 *mhp = 4991 (struct mode_header_grp2 *)headerp; 4992 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4993 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4994 } else { 4995 mode_header_length = MODE_HEADER_LENGTH; 4996 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4997 } 4998 4999 if (bd_len > MODE_BLK_DESC_LENGTH) { 5000 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5001 "sd_get_physical_geometry: received unexpected bd_len " 5002 "of %d, page3\n", bd_len); 5003 status = EIO; 5004 goto page3_exit; 5005 } 5006 5007 page3p = (struct mode_format *) 5008 ((caddr_t)headerp + mode_header_length + bd_len); 5009 5010 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5011 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5012 "sd_get_physical_geometry: mode sense pg3 code mismatch " 5013 "%d\n", page3p->mode_page.code); 5014 status = EIO; 5015 goto page3_exit; 5016 } 5017 5018 /* 5019 * Use this physical geometry data only if BOTH MODE SENSE commands 5020 * complete successfully; otherwise, revert to the logical geometry. 5021 * So, we need to save everything in temporary variables. 5022 */ 5023 sector_size = BE_16(page3p->data_bytes_sect); 5024 5025 /* 5026 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5027 */ 5028 if (sector_size == 0) { 5029 sector_size = un->un_sys_blocksize; 5030 } else { 5031 sector_size &= ~(un->un_sys_blocksize - 1); 5032 } 5033 5034 nsect = BE_16(page3p->sect_track); 5035 intrlv = BE_16(page3p->interleave); 5036 5037 SD_INFO(SD_LOG_COMMON, un, 5038 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5039 SD_INFO(SD_LOG_COMMON, un, 5040 " mode page: %d; nsect: %d; sector size: %d;\n", 5041 page3p->mode_page.code, nsect, sector_size); 5042 SD_INFO(SD_LOG_COMMON, un, 5043 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5044 BE_16(page3p->track_skew), 5045 BE_16(page3p->cylinder_skew)); 5046 5047 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5048 5049 /* 5050 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5051 */ 5052 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5053 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp, 5054 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag); 5055 if (status != 0) { 5056 SD_ERROR(SD_LOG_COMMON, un, 5057 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5058 goto page4_exit; 5059 } 5060 5061 /* 5062 * Determine size of Block Descriptors in order to locate the mode 5063 * page data. ATAPI devices return 0, SCSI devices should return 5064 * MODE_BLK_DESC_LENGTH. 5065 */ 5066 headerp = (struct mode_header *)p4bufp; 5067 if (un->un_f_cfg_is_atapi == TRUE) { 5068 struct mode_header_grp2 *mhp = 5069 (struct mode_header_grp2 *)headerp; 5070 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5071 } else { 5072 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5073 } 5074 5075 if (bd_len > MODE_BLK_DESC_LENGTH) { 5076 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5077 "sd_get_physical_geometry: received unexpected bd_len of " 5078 "%d, page4\n", bd_len); 5079 status = EIO; 5080 goto page4_exit; 5081 } 5082 5083 page4p = (struct mode_geometry *) 5084 ((caddr_t)headerp + mode_header_length + bd_len); 5085 5086 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5087 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 5088 "sd_get_physical_geometry: mode sense pg4 code mismatch " 5089 "%d\n", page4p->mode_page.code); 5090 status = EIO; 5091 goto page4_exit; 5092 } 5093 5094 /* 5095 * Stash the data now, after we know that both commands completed. 5096 */ 5097 5098 5099 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5100 spc = nhead * nsect; 5101 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5102 rpm = BE_16(page4p->rpm); 5103 5104 modesense_capacity = spc * ncyl; 5105 5106 SD_INFO(SD_LOG_COMMON, un, 5107 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5108 SD_INFO(SD_LOG_COMMON, un, 5109 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5110 SD_INFO(SD_LOG_COMMON, un, 5111 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5112 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5113 (void *)pgeom_p, capacity); 5114 5115 /* 5116 * Compensate if the drive's geometry is not rectangular, i.e., 5117 * the product of C * H * S returned by MODE SENSE >= that returned 5118 * by read capacity. This is an idiosyncrasy of the original x86 5119 * disk subsystem. 5120 */ 5121 if (modesense_capacity >= capacity) { 5122 SD_INFO(SD_LOG_COMMON, un, 5123 "sd_get_physical_geometry: adjusting acyl; " 5124 "old: %d; new: %d\n", pgeom_p->g_acyl, 5125 (modesense_capacity - capacity + spc - 1) / spc); 5126 if (sector_size != 0) { 5127 /* 1243403: NEC D38x7 drives don't support sec size */ 5128 pgeom_p->g_secsize = (unsigned short)sector_size; 5129 } 5130 pgeom_p->g_nsect = (unsigned short)nsect; 5131 pgeom_p->g_nhead = (unsigned short)nhead; 5132 pgeom_p->g_capacity = capacity; 5133 pgeom_p->g_acyl = 5134 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5135 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5136 } 5137 5138 pgeom_p->g_rpm = (unsigned short)rpm; 5139 pgeom_p->g_intrlv = (unsigned short)intrlv; 5140 ret = 0; 5141 5142 SD_INFO(SD_LOG_COMMON, un, 5143 "sd_get_physical_geometry: mode sense geometry:\n"); 5144 SD_INFO(SD_LOG_COMMON, un, 5145 " nsect: %d; sector size: %d; interlv: %d\n", 5146 nsect, sector_size, intrlv); 5147 SD_INFO(SD_LOG_COMMON, un, 5148 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5149 nhead, ncyl, rpm, modesense_capacity); 5150 SD_INFO(SD_LOG_COMMON, un, 5151 "sd_get_physical_geometry: (cached)\n"); 5152 SD_INFO(SD_LOG_COMMON, un, 5153 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5154 pgeom_p->g_ncyl, pgeom_p->g_acyl, 5155 pgeom_p->g_nhead, pgeom_p->g_nsect); 5156 SD_INFO(SD_LOG_COMMON, un, 5157 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5158 pgeom_p->g_secsize, pgeom_p->g_capacity, 5159 pgeom_p->g_intrlv, pgeom_p->g_rpm); 5160 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 5161 5162 page4_exit: 5163 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5164 5165 page3_exit: 5166 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5167 5168 if (status != 0) { 5169 if (status == EIO) { 5170 /* 5171 * Some disks do not support mode sense(6), we 5172 * should ignore this kind of error(sense key is 5173 * 0x5 - illegal request). 5174 */ 5175 uint8_t *sensep; 5176 int senlen; 5177 5178 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 5179 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 5180 ssc->ssc_uscsi_cmd->uscsi_rqresid); 5181 5182 if (senlen > 0 && 5183 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 5184 sd_ssc_assessment(ssc, 5185 SD_FMT_IGNORE_COMPROMISE); 5186 } else { 5187 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 5188 } 5189 } else { 5190 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5191 } 5192 } 5193 sd_ssc_fini(ssc); 5194 return (ret); 5195 } 5196 5197 /* 5198 * Function: sd_get_virtual_geometry 5199 * 5200 * Description: Ask the controller to tell us about the target device. 5201 * 5202 * Arguments: un - pointer to softstate 5203 * capacity - disk capacity in #blocks 5204 * lbasize - disk block size in bytes 5205 * 5206 * Context: Kernel thread only 5207 */ 5208 5209 static int 5210 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 5211 diskaddr_t capacity, int lbasize) 5212 { 5213 uint_t geombuf; 5214 int spc; 5215 5216 ASSERT(un != NULL); 5217 5218 /* Set sector size, and total number of sectors */ 5219 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5220 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5221 5222 /* Let the HBA tell us its geometry */ 5223 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5224 5225 /* A value of -1 indicates an undefined "geometry" property */ 5226 if (geombuf == (-1)) { 5227 return (EINVAL); 5228 } 5229 5230 /* Initialize the logical geometry cache. */ 5231 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5232 lgeom_p->g_nsect = geombuf & 0xffff; 5233 lgeom_p->g_secsize = un->un_sys_blocksize; 5234 5235 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5236 5237 /* 5238 * Note: The driver originally converted the capacity value from 5239 * target blocks to system blocks. However, the capacity value passed 5240 * to this routine is already in terms of system blocks (this scaling 5241 * is done when the READ CAPACITY command is issued and processed). 5242 * This 'error' may have gone undetected because the usage of g_ncyl 5243 * (which is based upon g_capacity) is very limited within the driver 5244 */ 5245 lgeom_p->g_capacity = capacity; 5246 5247 /* 5248 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5249 * hba may return zero values if the device has been removed. 5250 */ 5251 if (spc == 0) { 5252 lgeom_p->g_ncyl = 0; 5253 } else { 5254 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5255 } 5256 lgeom_p->g_acyl = 0; 5257 5258 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5259 return (0); 5260 5261 } 5262 /* 5263 * Function: sd_update_block_info 5264 * 5265 * Description: Calculate a byte count to sector count bitshift value 5266 * from sector size. 5267 * 5268 * Arguments: un: unit struct. 5269 * lbasize: new target sector size 5270 * capacity: new target capacity, ie. block count 5271 * 5272 * Context: Kernel thread context 5273 */ 5274 5275 static void 5276 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5277 { 5278 if (lbasize != 0) { 5279 un->un_tgt_blocksize = lbasize; 5280 un->un_f_tgt_blocksize_is_valid = TRUE; 5281 if (!un->un_f_has_removable_media) { 5282 un->un_sys_blocksize = lbasize; 5283 } 5284 } 5285 5286 if (capacity != 0) { 5287 if ((lbasize != 0) && (capacity > un->un_blockcount)) { 5288 /* LU was expanded dynamically */ 5289 un->un_f_expnevent = B_FALSE; 5290 } 5291 un->un_blockcount = capacity; 5292 un->un_f_blockcount_is_valid = TRUE; 5293 } 5294 } 5295 5296 5297 /* 5298 * Function: sd_register_devid 5299 * 5300 * Description: This routine will obtain the device id information from the 5301 * target, obtain the serial number, and register the device 5302 * id with the ddi framework. 5303 * 5304 * Arguments: devi - the system's dev_info_t for the device. 5305 * un - driver soft state (unit) structure 5306 * reservation_flag - indicates if a reservation conflict 5307 * occurred during attach 5308 * 5309 * Context: Kernel Thread 5310 */ 5311 static void 5312 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag) 5313 { 5314 int rval = 0; 5315 uchar_t *inq80 = NULL; 5316 size_t inq80_len = MAX_INQUIRY_SIZE; 5317 size_t inq80_resid = 0; 5318 uchar_t *inq83 = NULL; 5319 size_t inq83_len = MAX_INQUIRY_SIZE; 5320 size_t inq83_resid = 0; 5321 int dlen, len; 5322 char *sn; 5323 struct sd_lun *un; 5324 5325 ASSERT(ssc != NULL); 5326 un = ssc->ssc_un; 5327 ASSERT(un != NULL); 5328 ASSERT(mutex_owned(SD_MUTEX(un))); 5329 ASSERT((SD_DEVINFO(un)) == devi); 5330 5331 5332 /* 5333 * We check the availability of the World Wide Name (0x83) and Unit 5334 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5335 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5336 * 0x83 is available, that is the best choice. Our next choice is 5337 * 0x80. If neither are available, we munge the devid from the device 5338 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5339 * to fabricate a devid for non-Sun qualified disks. 5340 */ 5341 if (un->un_vpd_page_mask != 0) { 5342 /* collect page 80 data if available */ 5343 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5344 5345 mutex_exit(SD_MUTEX(un)); 5346 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5347 5348 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len, 5349 0x01, 0x80, &inq80_resid); 5350 5351 if (rval != 0) { 5352 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5353 kmem_free(inq80, inq80_len); 5354 inq80 = NULL; 5355 inq80_len = 0; 5356 } else if (ddi_prop_exists( 5357 DDI_DEV_T_NONE, SD_DEVINFO(un), 5358 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 5359 INQUIRY_SERIAL_NO) == 0) { 5360 /* 5361 * If we don't already have a serial number 5362 * property, do quick verify of data returned 5363 * and define property. 5364 */ 5365 dlen = inq80_len - inq80_resid; 5366 len = (size_t)inq80[3]; 5367 if ((dlen >= 4) && ((len + 4) <= dlen)) { 5368 /* 5369 * Ensure sn termination, skip leading 5370 * blanks, and create property 5371 * 'inquiry-serial-no'. 5372 */ 5373 sn = (char *)&inq80[4]; 5374 sn[len] = 0; 5375 while (*sn && (*sn == ' ')) 5376 sn++; 5377 if (*sn) { 5378 (void) ddi_prop_update_string( 5379 DDI_DEV_T_NONE, 5380 SD_DEVINFO(un), 5381 INQUIRY_SERIAL_NO, sn); 5382 } 5383 } 5384 } 5385 mutex_enter(SD_MUTEX(un)); 5386 } 5387 5388 /* collect page 83 data if available */ 5389 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5390 mutex_exit(SD_MUTEX(un)); 5391 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5392 5393 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len, 5394 0x01, 0x83, &inq83_resid); 5395 5396 if (rval != 0) { 5397 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5398 kmem_free(inq83, inq83_len); 5399 inq83 = NULL; 5400 inq83_len = 0; 5401 } 5402 mutex_enter(SD_MUTEX(un)); 5403 } 5404 } 5405 5406 /* 5407 * If transport has already registered a devid for this target 5408 * then that takes precedence over the driver's determination 5409 * of the devid. 5410 * 5411 * NOTE: The reason this check is done here instead of at the beginning 5412 * of the function is to allow the code above to create the 5413 * 'inquiry-serial-no' property. 5414 */ 5415 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 5416 ASSERT(un->un_devid); 5417 un->un_f_devid_transport_defined = TRUE; 5418 goto cleanup; /* use devid registered by the transport */ 5419 } 5420 5421 /* 5422 * This is the case of antiquated Sun disk drives that have the 5423 * FAB_DEVID property set in the disk_table. These drives 5424 * manage the devid's by storing them in last 2 available sectors 5425 * on the drive and have them fabricated by the ddi layer by calling 5426 * ddi_devid_init and passing the DEVID_FAB flag. 5427 */ 5428 if (un->un_f_opt_fab_devid == TRUE) { 5429 /* 5430 * Depending on EINVAL isn't reliable, since a reserved disk 5431 * may result in invalid geometry, so check to make sure a 5432 * reservation conflict did not occur during attach. 5433 */ 5434 if ((sd_get_devid(ssc) == EINVAL) && 5435 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5436 /* 5437 * The devid is invalid AND there is no reservation 5438 * conflict. Fabricate a new devid. 5439 */ 5440 (void) sd_create_devid(ssc); 5441 } 5442 5443 /* Register the devid if it exists */ 5444 if (un->un_devid != NULL) { 5445 (void) ddi_devid_register(SD_DEVINFO(un), 5446 un->un_devid); 5447 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5448 "sd_register_devid: Devid Fabricated\n"); 5449 } 5450 goto cleanup; 5451 } 5452 5453 /* encode best devid possible based on data available */ 5454 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 5455 (char *)ddi_driver_name(SD_DEVINFO(un)), 5456 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 5457 inq80, inq80_len - inq80_resid, inq83, inq83_len - 5458 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 5459 5460 /* devid successfully encoded, register devid */ 5461 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 5462 5463 } else { 5464 /* 5465 * Unable to encode a devid based on data available. 5466 * This is not a Sun qualified disk. Older Sun disk 5467 * drives that have the SD_FAB_DEVID property 5468 * set in the disk_table and non Sun qualified 5469 * disks are treated in the same manner. These 5470 * drives manage the devid's by storing them in 5471 * last 2 available sectors on the drive and 5472 * have them fabricated by the ddi layer by 5473 * calling ddi_devid_init and passing the 5474 * DEVID_FAB flag. 5475 * Create a fabricate devid only if there's no 5476 * fabricate devid existed. 5477 */ 5478 if (sd_get_devid(ssc) == EINVAL) { 5479 (void) sd_create_devid(ssc); 5480 } 5481 un->un_f_opt_fab_devid = TRUE; 5482 5483 /* Register the devid if it exists */ 5484 if (un->un_devid != NULL) { 5485 (void) ddi_devid_register(SD_DEVINFO(un), 5486 un->un_devid); 5487 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5488 "sd_register_devid: devid fabricated using " 5489 "ddi framework\n"); 5490 } 5491 } 5492 5493 cleanup: 5494 /* clean up resources */ 5495 if (inq80 != NULL) { 5496 kmem_free(inq80, inq80_len); 5497 } 5498 if (inq83 != NULL) { 5499 kmem_free(inq83, inq83_len); 5500 } 5501 } 5502 5503 5504 5505 /* 5506 * Function: sd_get_devid 5507 * 5508 * Description: This routine will return 0 if a valid device id has been 5509 * obtained from the target and stored in the soft state. If a 5510 * valid device id has not been previously read and stored, a 5511 * read attempt will be made. 5512 * 5513 * Arguments: un - driver soft state (unit) structure 5514 * 5515 * Return Code: 0 if we successfully get the device id 5516 * 5517 * Context: Kernel Thread 5518 */ 5519 5520 static int 5521 sd_get_devid(sd_ssc_t *ssc) 5522 { 5523 struct dk_devid *dkdevid; 5524 ddi_devid_t tmpid; 5525 uint_t *ip; 5526 size_t sz; 5527 diskaddr_t blk; 5528 int status; 5529 int chksum; 5530 int i; 5531 size_t buffer_size; 5532 struct sd_lun *un; 5533 5534 ASSERT(ssc != NULL); 5535 un = ssc->ssc_un; 5536 ASSERT(un != NULL); 5537 ASSERT(mutex_owned(SD_MUTEX(un))); 5538 5539 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 5540 un); 5541 5542 if (un->un_devid != NULL) { 5543 return (0); 5544 } 5545 5546 mutex_exit(SD_MUTEX(un)); 5547 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5548 (void *)SD_PATH_DIRECT) != 0) { 5549 mutex_enter(SD_MUTEX(un)); 5550 return (EINVAL); 5551 } 5552 5553 /* 5554 * Read and verify device id, stored in the reserved cylinders at the 5555 * end of the disk. Backup label is on the odd sectors of the last 5556 * track of the last cylinder. Device id will be on track of the next 5557 * to last cylinder. 5558 */ 5559 mutex_enter(SD_MUTEX(un)); 5560 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 5561 mutex_exit(SD_MUTEX(un)); 5562 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 5563 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk, 5564 SD_PATH_DIRECT); 5565 5566 if (status != 0) { 5567 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5568 goto error; 5569 } 5570 5571 /* Validate the revision */ 5572 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 5573 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 5574 status = EINVAL; 5575 goto error; 5576 } 5577 5578 /* Calculate the checksum */ 5579 chksum = 0; 5580 ip = (uint_t *)dkdevid; 5581 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5582 i++) { 5583 chksum ^= ip[i]; 5584 } 5585 5586 /* Compare the checksums */ 5587 if (DKD_GETCHKSUM(dkdevid) != chksum) { 5588 status = EINVAL; 5589 goto error; 5590 } 5591 5592 /* Validate the device id */ 5593 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 5594 status = EINVAL; 5595 goto error; 5596 } 5597 5598 /* 5599 * Store the device id in the driver soft state 5600 */ 5601 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 5602 tmpid = kmem_alloc(sz, KM_SLEEP); 5603 5604 mutex_enter(SD_MUTEX(un)); 5605 5606 un->un_devid = tmpid; 5607 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 5608 5609 kmem_free(dkdevid, buffer_size); 5610 5611 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 5612 5613 return (status); 5614 error: 5615 mutex_enter(SD_MUTEX(un)); 5616 kmem_free(dkdevid, buffer_size); 5617 return (status); 5618 } 5619 5620 5621 /* 5622 * Function: sd_create_devid 5623 * 5624 * Description: This routine will fabricate the device id and write it 5625 * to the disk. 5626 * 5627 * Arguments: un - driver soft state (unit) structure 5628 * 5629 * Return Code: value of the fabricated device id 5630 * 5631 * Context: Kernel Thread 5632 */ 5633 5634 static ddi_devid_t 5635 sd_create_devid(sd_ssc_t *ssc) 5636 { 5637 struct sd_lun *un; 5638 5639 ASSERT(ssc != NULL); 5640 un = ssc->ssc_un; 5641 ASSERT(un != NULL); 5642 5643 /* Fabricate the devid */ 5644 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 5645 == DDI_FAILURE) { 5646 return (NULL); 5647 } 5648 5649 /* Write the devid to disk */ 5650 if (sd_write_deviceid(ssc) != 0) { 5651 ddi_devid_free(un->un_devid); 5652 un->un_devid = NULL; 5653 } 5654 5655 return (un->un_devid); 5656 } 5657 5658 5659 /* 5660 * Function: sd_write_deviceid 5661 * 5662 * Description: This routine will write the device id to the disk 5663 * reserved sector. 5664 * 5665 * Arguments: un - driver soft state (unit) structure 5666 * 5667 * Return Code: EINVAL 5668 * value returned by sd_send_scsi_cmd 5669 * 5670 * Context: Kernel Thread 5671 */ 5672 5673 static int 5674 sd_write_deviceid(sd_ssc_t *ssc) 5675 { 5676 struct dk_devid *dkdevid; 5677 uchar_t *buf; 5678 diskaddr_t blk; 5679 uint_t *ip, chksum; 5680 int status; 5681 int i; 5682 struct sd_lun *un; 5683 5684 ASSERT(ssc != NULL); 5685 un = ssc->ssc_un; 5686 ASSERT(un != NULL); 5687 ASSERT(mutex_owned(SD_MUTEX(un))); 5688 5689 mutex_exit(SD_MUTEX(un)); 5690 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5691 (void *)SD_PATH_DIRECT) != 0) { 5692 mutex_enter(SD_MUTEX(un)); 5693 return (-1); 5694 } 5695 5696 5697 /* Allocate the buffer */ 5698 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 5699 dkdevid = (struct dk_devid *)buf; 5700 5701 /* Fill in the revision */ 5702 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 5703 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 5704 5705 /* Copy in the device id */ 5706 mutex_enter(SD_MUTEX(un)); 5707 bcopy(un->un_devid, &dkdevid->dkd_devid, 5708 ddi_devid_sizeof(un->un_devid)); 5709 mutex_exit(SD_MUTEX(un)); 5710 5711 /* Calculate the checksum */ 5712 chksum = 0; 5713 ip = (uint_t *)dkdevid; 5714 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); 5715 i++) { 5716 chksum ^= ip[i]; 5717 } 5718 5719 /* Fill-in checksum */ 5720 DKD_FORMCHKSUM(chksum, dkdevid); 5721 5722 /* Write the reserved sector */ 5723 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk, 5724 SD_PATH_DIRECT); 5725 if (status != 0) 5726 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5727 5728 kmem_free(buf, un->un_sys_blocksize); 5729 5730 mutex_enter(SD_MUTEX(un)); 5731 return (status); 5732 } 5733 5734 5735 /* 5736 * Function: sd_check_vpd_page_support 5737 * 5738 * Description: This routine sends an inquiry command with the EVPD bit set and 5739 * a page code of 0x00 to the device. It is used to determine which 5740 * vital product pages are available to find the devid. We are 5741 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1, 5742 * the device does not support that command. 5743 * 5744 * Arguments: un - driver soft state (unit) structure 5745 * 5746 * Return Code: 0 - success 5747 * 1 - check condition 5748 * 5749 * Context: This routine can sleep. 5750 */ 5751 5752 static int 5753 sd_check_vpd_page_support(sd_ssc_t *ssc) 5754 { 5755 uchar_t *page_list = NULL; 5756 uchar_t page_length = 0xff; /* Use max possible length */ 5757 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5758 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5759 int rval = 0; 5760 int counter; 5761 struct sd_lun *un; 5762 5763 ASSERT(ssc != NULL); 5764 un = ssc->ssc_un; 5765 ASSERT(un != NULL); 5766 ASSERT(mutex_owned(SD_MUTEX(un))); 5767 5768 mutex_exit(SD_MUTEX(un)); 5769 5770 /* 5771 * We'll set the page length to the maximum to save figuring it out 5772 * with an additional call. 5773 */ 5774 page_list = kmem_zalloc(page_length, KM_SLEEP); 5775 5776 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd, 5777 page_code, NULL); 5778 5779 if (rval != 0) 5780 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5781 5782 mutex_enter(SD_MUTEX(un)); 5783 5784 /* 5785 * Now we must validate that the device accepted the command, as some 5786 * drives do not support it. If the drive does support it, we will 5787 * return 0, and the supported pages will be in un_vpd_page_mask. If 5788 * not, we return -1. 5789 */ 5790 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5791 /* Loop to find one of the 2 pages we need */ 5792 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5793 5794 /* 5795 * Pages are returned in ascending order, and 0x83 is what we 5796 * are hoping for. 5797 */ 5798 while ((page_list[counter] <= 0xB1) && 5799 (counter <= (page_list[VPD_PAGE_LENGTH] + 5800 VPD_HEAD_OFFSET))) { 5801 /* 5802 * Add 3 because page_list[3] is the number of 5803 * pages minus 3 5804 */ 5805 5806 switch (page_list[counter]) { 5807 case 0x00: 5808 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5809 break; 5810 case 0x80: 5811 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5812 break; 5813 case 0x81: 5814 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5815 break; 5816 case 0x82: 5817 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5818 break; 5819 case 0x83: 5820 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5821 break; 5822 case 0x86: 5823 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5824 break; 5825 case 0xB1: 5826 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG; 5827 break; 5828 } 5829 counter++; 5830 } 5831 5832 } else { 5833 rval = -1; 5834 5835 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5836 "sd_check_vpd_page_support: This drive does not implement " 5837 "VPD pages.\n"); 5838 } 5839 5840 kmem_free(page_list, page_length); 5841 5842 return (rval); 5843 } 5844 5845 5846 /* 5847 * Function: sd_setup_pm 5848 * 5849 * Description: Initialize Power Management on the device 5850 * 5851 * Context: Kernel Thread 5852 */ 5853 5854 static void 5855 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi) 5856 { 5857 uint_t log_page_size; 5858 uchar_t *log_page_data; 5859 int rval = 0; 5860 struct sd_lun *un; 5861 5862 ASSERT(ssc != NULL); 5863 un = ssc->ssc_un; 5864 ASSERT(un != NULL); 5865 5866 /* 5867 * Since we are called from attach, holding a mutex for 5868 * un is unnecessary. Because some of the routines called 5869 * from here require SD_MUTEX to not be held, assert this 5870 * right up front. 5871 */ 5872 ASSERT(!mutex_owned(SD_MUTEX(un))); 5873 /* 5874 * Since the sd device does not have the 'reg' property, 5875 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5876 * The following code is to tell cpr that this device 5877 * DOES need to be suspended and resumed. 5878 */ 5879 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5880 "pm-hardware-state", "needs-suspend-resume"); 5881 5882 /* 5883 * This complies with the new power management framework 5884 * for certain desktop machines. Create the pm_components 5885 * property as a string array property. 5886 * If un_f_pm_supported is TRUE, that means the disk 5887 * attached HBA has set the "pm-capable" property and 5888 * the value of this property is bigger than 0. 5889 */ 5890 if (un->un_f_pm_supported) { 5891 /* 5892 * not all devices have a motor, try it first. 5893 * some devices may return ILLEGAL REQUEST, some 5894 * will hang 5895 * The following START_STOP_UNIT is used to check if target 5896 * device has a motor. 5897 */ 5898 un->un_f_start_stop_supported = TRUE; 5899 5900 if (un->un_f_power_condition_supported) { 5901 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5902 SD_POWER_CONDITION, SD_TARGET_ACTIVE, 5903 SD_PATH_DIRECT); 5904 if (rval != 0) { 5905 un->un_f_power_condition_supported = FALSE; 5906 } 5907 } 5908 if (!un->un_f_power_condition_supported) { 5909 rval = sd_send_scsi_START_STOP_UNIT(ssc, 5910 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT); 5911 } 5912 if (rval != 0) { 5913 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5914 un->un_f_start_stop_supported = FALSE; 5915 } 5916 5917 /* 5918 * create pm properties anyways otherwise the parent can't 5919 * go to sleep 5920 */ 5921 un->un_f_pm_is_enabled = TRUE; 5922 (void) sd_create_pm_components(devi, un); 5923 5924 /* 5925 * If it claims that log sense is supported, check it out. 5926 */ 5927 if (un->un_f_log_sense_supported) { 5928 rval = sd_log_page_supported(ssc, 5929 START_STOP_CYCLE_PAGE); 5930 if (rval == 1) { 5931 /* Page found, use it. */ 5932 un->un_start_stop_cycle_page = 5933 START_STOP_CYCLE_PAGE; 5934 } else { 5935 /* 5936 * Page not found or log sense is not 5937 * supported. 5938 * Notice we do not check the old style 5939 * START_STOP_CYCLE_VU_PAGE because this 5940 * code path does not apply to old disks. 5941 */ 5942 un->un_f_log_sense_supported = FALSE; 5943 un->un_f_pm_log_sense_smart = FALSE; 5944 } 5945 } 5946 5947 return; 5948 } 5949 5950 /* 5951 * For the disk whose attached HBA has not set the "pm-capable" 5952 * property, check if it supports the power management. 5953 */ 5954 if (!un->un_f_log_sense_supported) { 5955 un->un_power_level = SD_SPINDLE_ON; 5956 un->un_f_pm_is_enabled = FALSE; 5957 return; 5958 } 5959 5960 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE); 5961 5962 #ifdef SDDEBUG 5963 if (sd_force_pm_supported) { 5964 /* Force a successful result */ 5965 rval = 1; 5966 } 5967 #endif 5968 5969 /* 5970 * If the start-stop cycle counter log page is not supported 5971 * or if the pm-capable property is set to be false (0), 5972 * then we should not create the pm_components property. 5973 */ 5974 if (rval == -1) { 5975 /* 5976 * Error. 5977 * Reading log sense failed, most likely this is 5978 * an older drive that does not support log sense. 5979 * If this fails auto-pm is not supported. 5980 */ 5981 un->un_power_level = SD_SPINDLE_ON; 5982 un->un_f_pm_is_enabled = FALSE; 5983 5984 } else if (rval == 0) { 5985 /* 5986 * Page not found. 5987 * The start stop cycle counter is implemented as page 5988 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5989 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 5990 */ 5991 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) { 5992 /* 5993 * Page found, use this one. 5994 */ 5995 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 5996 un->un_f_pm_is_enabled = TRUE; 5997 } else { 5998 /* 5999 * Error or page not found. 6000 * auto-pm is not supported for this device. 6001 */ 6002 un->un_power_level = SD_SPINDLE_ON; 6003 un->un_f_pm_is_enabled = FALSE; 6004 } 6005 } else { 6006 /* 6007 * Page found, use it. 6008 */ 6009 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6010 un->un_f_pm_is_enabled = TRUE; 6011 } 6012 6013 6014 if (un->un_f_pm_is_enabled == TRUE) { 6015 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6016 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6017 6018 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6019 log_page_size, un->un_start_stop_cycle_page, 6020 0x01, 0, SD_PATH_DIRECT); 6021 6022 if (rval != 0) { 6023 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6024 } 6025 6026 #ifdef SDDEBUG 6027 if (sd_force_pm_supported) { 6028 /* Force a successful result */ 6029 rval = 0; 6030 } 6031 #endif 6032 6033 /* 6034 * If the Log sense for Page( Start/stop cycle counter page) 6035 * succeeds, then power management is supported and we can 6036 * enable auto-pm. 6037 */ 6038 if (rval == 0) { 6039 (void) sd_create_pm_components(devi, un); 6040 } else { 6041 un->un_power_level = SD_SPINDLE_ON; 6042 un->un_f_pm_is_enabled = FALSE; 6043 } 6044 6045 kmem_free(log_page_data, log_page_size); 6046 } 6047 } 6048 6049 6050 /* 6051 * Function: sd_create_pm_components 6052 * 6053 * Description: Initialize PM property. 6054 * 6055 * Context: Kernel thread context 6056 */ 6057 6058 static void 6059 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6060 { 6061 ASSERT(!mutex_owned(SD_MUTEX(un))); 6062 6063 if (un->un_f_power_condition_supported) { 6064 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6065 "pm-components", sd_pwr_pc.pm_comp, 5) 6066 != DDI_PROP_SUCCESS) { 6067 un->un_power_level = SD_SPINDLE_ACTIVE; 6068 un->un_f_pm_is_enabled = FALSE; 6069 return; 6070 } 6071 } else { 6072 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6073 "pm-components", sd_pwr_ss.pm_comp, 3) 6074 != DDI_PROP_SUCCESS) { 6075 un->un_power_level = SD_SPINDLE_ON; 6076 un->un_f_pm_is_enabled = FALSE; 6077 return; 6078 } 6079 } 6080 /* 6081 * When components are initially created they are idle, 6082 * power up any non-removables. 6083 * Note: the return value of pm_raise_power can't be used 6084 * for determining if PM should be enabled for this device. 6085 * Even if you check the return values and remove this 6086 * property created above, the PM framework will not honor the 6087 * change after the first call to pm_raise_power. Hence, 6088 * removal of that property does not help if pm_raise_power 6089 * fails. In the case of removable media, the start/stop 6090 * will fail if the media is not present. 6091 */ 6092 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6093 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) { 6094 mutex_enter(SD_MUTEX(un)); 6095 un->un_power_level = SD_PM_STATE_ACTIVE(un); 6096 mutex_enter(&un->un_pm_mutex); 6097 /* Set to on and not busy. */ 6098 un->un_pm_count = 0; 6099 } else { 6100 mutex_enter(SD_MUTEX(un)); 6101 un->un_power_level = SD_PM_STATE_STOPPED(un); 6102 mutex_enter(&un->un_pm_mutex); 6103 /* Set to off. */ 6104 un->un_pm_count = -1; 6105 } 6106 mutex_exit(&un->un_pm_mutex); 6107 mutex_exit(SD_MUTEX(un)); 6108 } 6109 6110 6111 /* 6112 * Function: sd_ddi_suspend 6113 * 6114 * Description: Performs system power-down operations. This includes 6115 * setting the drive state to indicate its suspended so 6116 * that no new commands will be accepted. Also, wait for 6117 * all commands that are in transport or queued to a timer 6118 * for retry to complete. All timeout threads are cancelled. 6119 * 6120 * Return Code: DDI_FAILURE or DDI_SUCCESS 6121 * 6122 * Context: Kernel thread context 6123 */ 6124 6125 static int 6126 sd_ddi_suspend(dev_info_t *devi) 6127 { 6128 struct sd_lun *un; 6129 clock_t wait_cmds_complete; 6130 6131 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6132 if (un == NULL) { 6133 return (DDI_FAILURE); 6134 } 6135 6136 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6137 6138 mutex_enter(SD_MUTEX(un)); 6139 6140 /* Return success if the device is already suspended. */ 6141 if (un->un_state == SD_STATE_SUSPENDED) { 6142 mutex_exit(SD_MUTEX(un)); 6143 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6144 "device already suspended, exiting\n"); 6145 return (DDI_SUCCESS); 6146 } 6147 6148 /* Return failure if the device is being used by HA */ 6149 if (un->un_resvd_status & 6150 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6151 mutex_exit(SD_MUTEX(un)); 6152 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6153 "device in use by HA, exiting\n"); 6154 return (DDI_FAILURE); 6155 } 6156 6157 /* 6158 * Return failure if the device is in a resource wait 6159 * or power changing state. 6160 */ 6161 if ((un->un_state == SD_STATE_RWAIT) || 6162 (un->un_state == SD_STATE_PM_CHANGING)) { 6163 mutex_exit(SD_MUTEX(un)); 6164 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6165 "device in resource wait state, exiting\n"); 6166 return (DDI_FAILURE); 6167 } 6168 6169 6170 un->un_save_state = un->un_last_state; 6171 New_state(un, SD_STATE_SUSPENDED); 6172 6173 /* 6174 * Wait for all commands that are in transport or queued to a timer 6175 * for retry to complete. 6176 * 6177 * While waiting, no new commands will be accepted or sent because of 6178 * the new state we set above. 6179 * 6180 * Wait till current operation has completed. If we are in the resource 6181 * wait state (with an intr outstanding) then we need to wait till the 6182 * intr completes and starts the next cmd. We want to wait for 6183 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 6184 */ 6185 wait_cmds_complete = ddi_get_lbolt() + 6186 (sd_wait_cmds_complete * drv_usectohz(1000000)); 6187 6188 while (un->un_ncmds_in_transport != 0) { 6189 /* 6190 * Fail if commands do not finish in the specified time. 6191 */ 6192 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 6193 wait_cmds_complete) == -1) { 6194 /* 6195 * Undo the state changes made above. Everything 6196 * must go back to it's original value. 6197 */ 6198 Restore_state(un); 6199 un->un_last_state = un->un_save_state; 6200 /* Wake up any threads that might be waiting. */ 6201 cv_broadcast(&un->un_suspend_cv); 6202 mutex_exit(SD_MUTEX(un)); 6203 SD_ERROR(SD_LOG_IO_PM, un, 6204 "sd_ddi_suspend: failed due to outstanding cmds\n"); 6205 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 6206 return (DDI_FAILURE); 6207 } 6208 } 6209 6210 /* 6211 * Cancel SCSI watch thread and timeouts, if any are active 6212 */ 6213 6214 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 6215 opaque_t temp_token = un->un_swr_token; 6216 mutex_exit(SD_MUTEX(un)); 6217 scsi_watch_suspend(temp_token); 6218 mutex_enter(SD_MUTEX(un)); 6219 } 6220 6221 if (un->un_reset_throttle_timeid != NULL) { 6222 timeout_id_t temp_id = un->un_reset_throttle_timeid; 6223 un->un_reset_throttle_timeid = NULL; 6224 mutex_exit(SD_MUTEX(un)); 6225 (void) untimeout(temp_id); 6226 mutex_enter(SD_MUTEX(un)); 6227 } 6228 6229 if (un->un_dcvb_timeid != NULL) { 6230 timeout_id_t temp_id = un->un_dcvb_timeid; 6231 un->un_dcvb_timeid = NULL; 6232 mutex_exit(SD_MUTEX(un)); 6233 (void) untimeout(temp_id); 6234 mutex_enter(SD_MUTEX(un)); 6235 } 6236 6237 mutex_enter(&un->un_pm_mutex); 6238 if (un->un_pm_timeid != NULL) { 6239 timeout_id_t temp_id = un->un_pm_timeid; 6240 un->un_pm_timeid = NULL; 6241 mutex_exit(&un->un_pm_mutex); 6242 mutex_exit(SD_MUTEX(un)); 6243 (void) untimeout(temp_id); 6244 mutex_enter(SD_MUTEX(un)); 6245 } else { 6246 mutex_exit(&un->un_pm_mutex); 6247 } 6248 6249 if (un->un_rmw_msg_timeid != NULL) { 6250 timeout_id_t temp_id = un->un_rmw_msg_timeid; 6251 un->un_rmw_msg_timeid = NULL; 6252 mutex_exit(SD_MUTEX(un)); 6253 (void) untimeout(temp_id); 6254 mutex_enter(SD_MUTEX(un)); 6255 } 6256 6257 if (un->un_retry_timeid != NULL) { 6258 timeout_id_t temp_id = un->un_retry_timeid; 6259 un->un_retry_timeid = NULL; 6260 mutex_exit(SD_MUTEX(un)); 6261 (void) untimeout(temp_id); 6262 mutex_enter(SD_MUTEX(un)); 6263 6264 if (un->un_retry_bp != NULL) { 6265 un->un_retry_bp->av_forw = un->un_waitq_headp; 6266 un->un_waitq_headp = un->un_retry_bp; 6267 if (un->un_waitq_tailp == NULL) { 6268 un->un_waitq_tailp = un->un_retry_bp; 6269 } 6270 un->un_retry_bp = NULL; 6271 un->un_retry_statp = NULL; 6272 } 6273 } 6274 6275 if (un->un_direct_priority_timeid != NULL) { 6276 timeout_id_t temp_id = un->un_direct_priority_timeid; 6277 un->un_direct_priority_timeid = NULL; 6278 mutex_exit(SD_MUTEX(un)); 6279 (void) untimeout(temp_id); 6280 mutex_enter(SD_MUTEX(un)); 6281 } 6282 6283 if (un->un_f_is_fibre == TRUE) { 6284 /* 6285 * Remove callbacks for insert and remove events 6286 */ 6287 if (un->un_insert_event != NULL) { 6288 mutex_exit(SD_MUTEX(un)); 6289 (void) ddi_remove_event_handler(un->un_insert_cb_id); 6290 mutex_enter(SD_MUTEX(un)); 6291 un->un_insert_event = NULL; 6292 } 6293 6294 if (un->un_remove_event != NULL) { 6295 mutex_exit(SD_MUTEX(un)); 6296 (void) ddi_remove_event_handler(un->un_remove_cb_id); 6297 mutex_enter(SD_MUTEX(un)); 6298 un->un_remove_event = NULL; 6299 } 6300 } 6301 6302 mutex_exit(SD_MUTEX(un)); 6303 6304 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 6305 6306 return (DDI_SUCCESS); 6307 } 6308 6309 6310 /* 6311 * Function: sd_ddi_resume 6312 * 6313 * Description: Performs system power-up operations.. 6314 * 6315 * Return Code: DDI_SUCCESS 6316 * DDI_FAILURE 6317 * 6318 * Context: Kernel thread context 6319 */ 6320 6321 static int 6322 sd_ddi_resume(dev_info_t *devi) 6323 { 6324 struct sd_lun *un; 6325 6326 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6327 if (un == NULL) { 6328 return (DDI_FAILURE); 6329 } 6330 6331 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6332 6333 mutex_enter(SD_MUTEX(un)); 6334 Restore_state(un); 6335 6336 /* 6337 * Restore the state which was saved to give the 6338 * the right state in un_last_state 6339 */ 6340 un->un_last_state = un->un_save_state; 6341 /* 6342 * Note: throttle comes back at full. 6343 * Also note: this MUST be done before calling pm_raise_power 6344 * otherwise the system can get hung in biowait. The scenario where 6345 * this'll happen is under cpr suspend. Writing of the system 6346 * state goes through sddump, which writes 0 to un_throttle. If 6347 * writing the system state then fails, example if the partition is 6348 * too small, then cpr attempts a resume. If throttle isn't restored 6349 * from the saved value until after calling pm_raise_power then 6350 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6351 * in biowait. 6352 */ 6353 un->un_throttle = un->un_saved_throttle; 6354 6355 /* 6356 * The chance of failure is very rare as the only command done in power 6357 * entry point is START command when you transition from 0->1 or 6358 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6359 * which suspend was done. Ignore the return value as the resume should 6360 * not be failed. In the case of removable media the media need not be 6361 * inserted and hence there is a chance that raise power will fail with 6362 * media not present. 6363 */ 6364 if (un->un_f_attach_spinup) { 6365 mutex_exit(SD_MUTEX(un)); 6366 (void) pm_raise_power(SD_DEVINFO(un), 0, 6367 SD_PM_STATE_ACTIVE(un)); 6368 mutex_enter(SD_MUTEX(un)); 6369 } 6370 6371 /* 6372 * Don't broadcast to the suspend cv and therefore possibly 6373 * start I/O until after power has been restored. 6374 */ 6375 cv_broadcast(&un->un_suspend_cv); 6376 cv_broadcast(&un->un_state_cv); 6377 6378 /* restart thread */ 6379 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6380 scsi_watch_resume(un->un_swr_token); 6381 } 6382 6383 #if (defined(__fibre)) 6384 if (un->un_f_is_fibre == TRUE) { 6385 /* 6386 * Add callbacks for insert and remove events 6387 */ 6388 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6389 sd_init_event_callbacks(un); 6390 } 6391 } 6392 #endif 6393 6394 /* 6395 * Transport any pending commands to the target. 6396 * 6397 * If this is a low-activity device commands in queue will have to wait 6398 * until new commands come in, which may take awhile. Also, we 6399 * specifically don't check un_ncmds_in_transport because we know that 6400 * there really are no commands in progress after the unit was 6401 * suspended and we could have reached the throttle level, been 6402 * suspended, and have no new commands coming in for awhile. Highly 6403 * unlikely, but so is the low-activity disk scenario. 6404 */ 6405 ddi_xbuf_dispatch(un->un_xbuf_attr); 6406 6407 sd_start_cmds(un, NULL); 6408 mutex_exit(SD_MUTEX(un)); 6409 6410 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6411 6412 return (DDI_SUCCESS); 6413 } 6414 6415 6416 /* 6417 * Function: sd_pm_state_change 6418 * 6419 * Description: Change the driver power state. 6420 * Someone else is required to actually change the driver 6421 * power level. 6422 * 6423 * Arguments: un - driver soft state (unit) structure 6424 * level - the power level that is changed to 6425 * flag - to decide how to change the power state 6426 * 6427 * Return Code: DDI_SUCCESS 6428 * 6429 * Context: Kernel thread context 6430 */ 6431 static int 6432 sd_pm_state_change(struct sd_lun *un, int level, int flag) 6433 { 6434 ASSERT(un != NULL); 6435 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n"); 6436 6437 ASSERT(!mutex_owned(SD_MUTEX(un))); 6438 mutex_enter(SD_MUTEX(un)); 6439 6440 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) { 6441 un->un_power_level = level; 6442 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6443 mutex_enter(&un->un_pm_mutex); 6444 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6445 un->un_pm_count++; 6446 ASSERT(un->un_pm_count == 0); 6447 } 6448 mutex_exit(&un->un_pm_mutex); 6449 } else { 6450 /* 6451 * Exit if power management is not enabled for this device, 6452 * or if the device is being used by HA. 6453 */ 6454 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 6455 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 6456 mutex_exit(SD_MUTEX(un)); 6457 SD_TRACE(SD_LOG_POWER, un, 6458 "sd_pm_state_change: exiting\n"); 6459 return (DDI_FAILURE); 6460 } 6461 6462 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: " 6463 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver); 6464 6465 /* 6466 * See if the device is not busy, ie.: 6467 * - we have no commands in the driver for this device 6468 * - not waiting for resources 6469 */ 6470 if ((un->un_ncmds_in_driver == 0) && 6471 (un->un_state != SD_STATE_RWAIT)) { 6472 /* 6473 * The device is not busy, so it is OK to go to low 6474 * power state. Indicate low power, but rely on someone 6475 * else to actually change it. 6476 */ 6477 mutex_enter(&un->un_pm_mutex); 6478 un->un_pm_count = -1; 6479 mutex_exit(&un->un_pm_mutex); 6480 un->un_power_level = level; 6481 } 6482 } 6483 6484 mutex_exit(SD_MUTEX(un)); 6485 6486 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n"); 6487 6488 return (DDI_SUCCESS); 6489 } 6490 6491 6492 /* 6493 * Function: sd_pm_idletimeout_handler 6494 * 6495 * Description: A timer routine that's active only while a device is busy. 6496 * The purpose is to extend slightly the pm framework's busy 6497 * view of the device to prevent busy/idle thrashing for 6498 * back-to-back commands. Do this by comparing the current time 6499 * to the time at which the last command completed and when the 6500 * difference is greater than sd_pm_idletime, call 6501 * pm_idle_component. In addition to indicating idle to the pm 6502 * framework, update the chain type to again use the internal pm 6503 * layers of the driver. 6504 * 6505 * Arguments: arg - driver soft state (unit) structure 6506 * 6507 * Context: Executes in a timeout(9F) thread context 6508 */ 6509 6510 static void 6511 sd_pm_idletimeout_handler(void *arg) 6512 { 6513 struct sd_lun *un = arg; 6514 6515 time_t now; 6516 6517 mutex_enter(&sd_detach_mutex); 6518 if (un->un_detach_count != 0) { 6519 /* Abort if the instance is detaching */ 6520 mutex_exit(&sd_detach_mutex); 6521 return; 6522 } 6523 mutex_exit(&sd_detach_mutex); 6524 6525 now = ddi_get_time(); 6526 /* 6527 * Grab both mutexes, in the proper order, since we're accessing 6528 * both PM and softstate variables. 6529 */ 6530 mutex_enter(SD_MUTEX(un)); 6531 mutex_enter(&un->un_pm_mutex); 6532 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 6533 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 6534 /* 6535 * Update the chain types. 6536 * This takes affect on the next new command received. 6537 */ 6538 if (un->un_f_non_devbsize_supported) { 6539 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6540 } else { 6541 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6542 } 6543 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6544 6545 SD_TRACE(SD_LOG_IO_PM, un, 6546 "sd_pm_idletimeout_handler: idling device\n"); 6547 (void) pm_idle_component(SD_DEVINFO(un), 0); 6548 un->un_pm_idle_timeid = NULL; 6549 } else { 6550 un->un_pm_idle_timeid = 6551 timeout(sd_pm_idletimeout_handler, un, 6552 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 6553 } 6554 mutex_exit(&un->un_pm_mutex); 6555 mutex_exit(SD_MUTEX(un)); 6556 } 6557 6558 6559 /* 6560 * Function: sd_pm_timeout_handler 6561 * 6562 * Description: Callback to tell framework we are idle. 6563 * 6564 * Context: timeout(9f) thread context. 6565 */ 6566 6567 static void 6568 sd_pm_timeout_handler(void *arg) 6569 { 6570 struct sd_lun *un = arg; 6571 6572 (void) pm_idle_component(SD_DEVINFO(un), 0); 6573 mutex_enter(&un->un_pm_mutex); 6574 un->un_pm_timeid = NULL; 6575 mutex_exit(&un->un_pm_mutex); 6576 } 6577 6578 6579 /* 6580 * Function: sdpower 6581 * 6582 * Description: PM entry point. 6583 * 6584 * Return Code: DDI_SUCCESS 6585 * DDI_FAILURE 6586 * 6587 * Context: Kernel thread context 6588 */ 6589 6590 static int 6591 sdpower(dev_info_t *devi, int component, int level) 6592 { 6593 struct sd_lun *un; 6594 int instance; 6595 int rval = DDI_SUCCESS; 6596 uint_t i, log_page_size, maxcycles, ncycles; 6597 uchar_t *log_page_data; 6598 int log_sense_page; 6599 int medium_present; 6600 time_t intvlp; 6601 struct pm_trans_data sd_pm_tran_data; 6602 uchar_t save_state; 6603 int sval; 6604 uchar_t state_before_pm; 6605 int got_semaphore_here; 6606 sd_ssc_t *ssc; 6607 int last_power_level; 6608 6609 instance = ddi_get_instance(devi); 6610 6611 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 6612 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) { 6613 return (DDI_FAILURE); 6614 } 6615 6616 ssc = sd_ssc_init(un); 6617 6618 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 6619 6620 /* 6621 * Must synchronize power down with close. 6622 * Attempt to decrement/acquire the open/close semaphore, 6623 * but do NOT wait on it. If it's not greater than zero, 6624 * ie. it can't be decremented without waiting, then 6625 * someone else, either open or close, already has it 6626 * and the try returns 0. Use that knowledge here to determine 6627 * if it's OK to change the device power level. 6628 * Also, only increment it on exit if it was decremented, ie. gotten, 6629 * here. 6630 */ 6631 got_semaphore_here = sema_tryp(&un->un_semoclose); 6632 6633 mutex_enter(SD_MUTEX(un)); 6634 6635 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 6636 un->un_ncmds_in_driver); 6637 6638 /* 6639 * If un_ncmds_in_driver is non-zero it indicates commands are 6640 * already being processed in the driver, or if the semaphore was 6641 * not gotten here it indicates an open or close is being processed. 6642 * At the same time somebody is requesting to go to a lower power 6643 * that can't perform I/O, which can't happen, therefore we need to 6644 * return failure. 6645 */ 6646 if ((!SD_PM_IS_IO_CAPABLE(un, level)) && 6647 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 6648 mutex_exit(SD_MUTEX(un)); 6649 6650 if (got_semaphore_here != 0) { 6651 sema_v(&un->un_semoclose); 6652 } 6653 SD_TRACE(SD_LOG_IO_PM, un, 6654 "sdpower: exit, device has queued cmds.\n"); 6655 6656 goto sdpower_failed; 6657 } 6658 6659 /* 6660 * if it is OFFLINE that means the disk is completely dead 6661 * in our case we have to put the disk in on or off by sending commands 6662 * Of course that will fail anyway so return back here. 6663 * 6664 * Power changes to a device that's OFFLINE or SUSPENDED 6665 * are not allowed. 6666 */ 6667 if ((un->un_state == SD_STATE_OFFLINE) || 6668 (un->un_state == SD_STATE_SUSPENDED)) { 6669 mutex_exit(SD_MUTEX(un)); 6670 6671 if (got_semaphore_here != 0) { 6672 sema_v(&un->un_semoclose); 6673 } 6674 SD_TRACE(SD_LOG_IO_PM, un, 6675 "sdpower: exit, device is off-line.\n"); 6676 6677 goto sdpower_failed; 6678 } 6679 6680 /* 6681 * Change the device's state to indicate it's power level 6682 * is being changed. Do this to prevent a power off in the 6683 * middle of commands, which is especially bad on devices 6684 * that are really powered off instead of just spun down. 6685 */ 6686 state_before_pm = un->un_state; 6687 un->un_state = SD_STATE_PM_CHANGING; 6688 6689 mutex_exit(SD_MUTEX(un)); 6690 6691 /* 6692 * If log sense command is not supported, bypass the 6693 * following checking, otherwise, check the log sense 6694 * information for this device. 6695 */ 6696 if (SD_PM_STOP_MOTOR_NEEDED(un, level) && 6697 un->un_f_log_sense_supported) { 6698 /* 6699 * Get the log sense information to understand whether the 6700 * the powercycle counts have gone beyond the threshhold. 6701 */ 6702 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6703 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6704 6705 mutex_enter(SD_MUTEX(un)); 6706 log_sense_page = un->un_start_stop_cycle_page; 6707 mutex_exit(SD_MUTEX(un)); 6708 6709 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6710 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 6711 6712 if (rval != 0) { 6713 if (rval == EIO) 6714 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6715 else 6716 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6717 } 6718 6719 #ifdef SDDEBUG 6720 if (sd_force_pm_supported) { 6721 /* Force a successful result */ 6722 rval = 0; 6723 } 6724 #endif 6725 if (rval != 0) { 6726 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 6727 "Log Sense Failed\n"); 6728 6729 kmem_free(log_page_data, log_page_size); 6730 /* Cannot support power management on those drives */ 6731 6732 if (got_semaphore_here != 0) { 6733 sema_v(&un->un_semoclose); 6734 } 6735 /* 6736 * On exit put the state back to it's original value 6737 * and broadcast to anyone waiting for the power 6738 * change completion. 6739 */ 6740 mutex_enter(SD_MUTEX(un)); 6741 un->un_state = state_before_pm; 6742 cv_broadcast(&un->un_suspend_cv); 6743 mutex_exit(SD_MUTEX(un)); 6744 SD_TRACE(SD_LOG_IO_PM, un, 6745 "sdpower: exit, Log Sense Failed.\n"); 6746 6747 goto sdpower_failed; 6748 } 6749 6750 /* 6751 * From the page data - Convert the essential information to 6752 * pm_trans_data 6753 */ 6754 maxcycles = 6755 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 6756 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 6757 6758 ncycles = 6759 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 6760 (log_page_data[0x26] << 8) | log_page_data[0x27]; 6761 6762 if (un->un_f_pm_log_sense_smart) { 6763 sd_pm_tran_data.un.smart_count.allowed = maxcycles; 6764 sd_pm_tran_data.un.smart_count.consumed = ncycles; 6765 sd_pm_tran_data.un.smart_count.flag = 0; 6766 sd_pm_tran_data.format = DC_SMART_FORMAT; 6767 } else { 6768 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 6769 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 6770 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 6771 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 6772 log_page_data[8+i]; 6773 } 6774 sd_pm_tran_data.un.scsi_cycles.flag = 0; 6775 sd_pm_tran_data.format = DC_SCSI_FORMAT; 6776 } 6777 6778 kmem_free(log_page_data, log_page_size); 6779 6780 /* 6781 * Call pm_trans_check routine to get the Ok from 6782 * the global policy 6783 */ 6784 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 6785 #ifdef SDDEBUG 6786 if (sd_force_pm_supported) { 6787 /* Force a successful result */ 6788 rval = 1; 6789 } 6790 #endif 6791 switch (rval) { 6792 case 0: 6793 /* 6794 * Not Ok to Power cycle or error in parameters passed 6795 * Would have given the advised time to consider power 6796 * cycle. Based on the new intvlp parameter we are 6797 * supposed to pretend we are busy so that pm framework 6798 * will never call our power entry point. Because of 6799 * that install a timeout handler and wait for the 6800 * recommended time to elapse so that power management 6801 * can be effective again. 6802 * 6803 * To effect this behavior, call pm_busy_component to 6804 * indicate to the framework this device is busy. 6805 * By not adjusting un_pm_count the rest of PM in 6806 * the driver will function normally, and independent 6807 * of this but because the framework is told the device 6808 * is busy it won't attempt powering down until it gets 6809 * a matching idle. The timeout handler sends this. 6810 * Note: sd_pm_entry can't be called here to do this 6811 * because sdpower may have been called as a result 6812 * of a call to pm_raise_power from within sd_pm_entry. 6813 * 6814 * If a timeout handler is already active then 6815 * don't install another. 6816 */ 6817 mutex_enter(&un->un_pm_mutex); 6818 if (un->un_pm_timeid == NULL) { 6819 un->un_pm_timeid = 6820 timeout(sd_pm_timeout_handler, 6821 un, intvlp * drv_usectohz(1000000)); 6822 mutex_exit(&un->un_pm_mutex); 6823 (void) pm_busy_component(SD_DEVINFO(un), 0); 6824 } else { 6825 mutex_exit(&un->un_pm_mutex); 6826 } 6827 if (got_semaphore_here != 0) { 6828 sema_v(&un->un_semoclose); 6829 } 6830 /* 6831 * On exit put the state back to it's original value 6832 * and broadcast to anyone waiting for the power 6833 * change completion. 6834 */ 6835 mutex_enter(SD_MUTEX(un)); 6836 un->un_state = state_before_pm; 6837 cv_broadcast(&un->un_suspend_cv); 6838 mutex_exit(SD_MUTEX(un)); 6839 6840 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6841 "trans check Failed, not ok to power cycle.\n"); 6842 6843 goto sdpower_failed; 6844 case -1: 6845 if (got_semaphore_here != 0) { 6846 sema_v(&un->un_semoclose); 6847 } 6848 /* 6849 * On exit put the state back to it's original value 6850 * and broadcast to anyone waiting for the power 6851 * change completion. 6852 */ 6853 mutex_enter(SD_MUTEX(un)); 6854 un->un_state = state_before_pm; 6855 cv_broadcast(&un->un_suspend_cv); 6856 mutex_exit(SD_MUTEX(un)); 6857 SD_TRACE(SD_LOG_IO_PM, un, 6858 "sdpower: exit, trans check command Failed.\n"); 6859 6860 goto sdpower_failed; 6861 } 6862 } 6863 6864 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6865 /* 6866 * Save the last state... if the STOP FAILS we need it 6867 * for restoring 6868 */ 6869 mutex_enter(SD_MUTEX(un)); 6870 save_state = un->un_last_state; 6871 last_power_level = un->un_power_level; 6872 /* 6873 * There must not be any cmds. getting processed 6874 * in the driver when we get here. Power to the 6875 * device is potentially going off. 6876 */ 6877 ASSERT(un->un_ncmds_in_driver == 0); 6878 mutex_exit(SD_MUTEX(un)); 6879 6880 /* 6881 * For now PM suspend the device completely before spindle is 6882 * turned off 6883 */ 6884 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE)) 6885 == DDI_FAILURE) { 6886 if (got_semaphore_here != 0) { 6887 sema_v(&un->un_semoclose); 6888 } 6889 /* 6890 * On exit put the state back to it's original value 6891 * and broadcast to anyone waiting for the power 6892 * change completion. 6893 */ 6894 mutex_enter(SD_MUTEX(un)); 6895 un->un_state = state_before_pm; 6896 un->un_power_level = last_power_level; 6897 cv_broadcast(&un->un_suspend_cv); 6898 mutex_exit(SD_MUTEX(un)); 6899 SD_TRACE(SD_LOG_IO_PM, un, 6900 "sdpower: exit, PM suspend Failed.\n"); 6901 6902 goto sdpower_failed; 6903 } 6904 } 6905 6906 /* 6907 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6908 * close, or strategy. Dump no long uses this routine, it uses it's 6909 * own code so it can be done in polled mode. 6910 */ 6911 6912 medium_present = TRUE; 6913 6914 /* 6915 * When powering up, issue a TUR in case the device is at unit 6916 * attention. Don't do retries. Bypass the PM layer, otherwise 6917 * a deadlock on un_pm_busy_cv will occur. 6918 */ 6919 if (SD_PM_IS_IO_CAPABLE(un, level)) { 6920 sval = sd_send_scsi_TEST_UNIT_READY(ssc, 6921 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6922 if (sval != 0) 6923 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6924 } 6925 6926 if (un->un_f_power_condition_supported) { 6927 char *pm_condition_name[] = {"STOPPED", "STANDBY", 6928 "IDLE", "ACTIVE"}; 6929 SD_TRACE(SD_LOG_IO_PM, un, 6930 "sdpower: sending \'%s\' power condition", 6931 pm_condition_name[level]); 6932 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 6933 sd_pl2pc[level], SD_PATH_DIRECT); 6934 } else { 6935 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6936 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6937 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 6938 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : 6939 SD_TARGET_STOP), SD_PATH_DIRECT); 6940 } 6941 if (sval != 0) { 6942 if (sval == EIO) 6943 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6944 else 6945 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6946 } 6947 6948 /* Command failed, check for media present. */ 6949 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6950 medium_present = FALSE; 6951 } 6952 6953 /* 6954 * The conditions of interest here are: 6955 * if a spindle off with media present fails, 6956 * then restore the state and return an error. 6957 * else if a spindle on fails, 6958 * then return an error (there's no state to restore). 6959 * In all other cases we setup for the new state 6960 * and return success. 6961 */ 6962 if (!SD_PM_IS_IO_CAPABLE(un, level)) { 6963 if ((medium_present == TRUE) && (sval != 0)) { 6964 /* The stop command from above failed */ 6965 rval = DDI_FAILURE; 6966 /* 6967 * The stop command failed, and we have media 6968 * present. Put the level back by calling the 6969 * sd_pm_resume() and set the state back to 6970 * it's previous value. 6971 */ 6972 (void) sd_pm_state_change(un, last_power_level, 6973 SD_PM_STATE_ROLLBACK); 6974 mutex_enter(SD_MUTEX(un)); 6975 un->un_last_state = save_state; 6976 mutex_exit(SD_MUTEX(un)); 6977 } else if (un->un_f_monitor_media_state) { 6978 /* 6979 * The stop command from above succeeded. 6980 * Terminate watch thread in case of removable media 6981 * devices going into low power state. This is as per 6982 * the requirements of pm framework, otherwise commands 6983 * will be generated for the device (through watch 6984 * thread), even when the device is in low power state. 6985 */ 6986 mutex_enter(SD_MUTEX(un)); 6987 un->un_f_watcht_stopped = FALSE; 6988 if (un->un_swr_token != NULL) { 6989 opaque_t temp_token = un->un_swr_token; 6990 un->un_f_watcht_stopped = TRUE; 6991 un->un_swr_token = NULL; 6992 mutex_exit(SD_MUTEX(un)); 6993 (void) scsi_watch_request_terminate(temp_token, 6994 SCSI_WATCH_TERMINATE_ALL_WAIT); 6995 } else { 6996 mutex_exit(SD_MUTEX(un)); 6997 } 6998 } 6999 } else { 7000 /* 7001 * The level requested is I/O capable. 7002 * Legacy behavior: return success on a failed spinup 7003 * if there is no media in the drive. 7004 * Do this by looking at medium_present here. 7005 */ 7006 if ((sval != 0) && medium_present) { 7007 /* The start command from above failed */ 7008 rval = DDI_FAILURE; 7009 } else { 7010 /* 7011 * The start command from above succeeded 7012 * PM resume the devices now that we have 7013 * started the disks 7014 */ 7015 (void) sd_pm_state_change(un, level, 7016 SD_PM_STATE_CHANGE); 7017 7018 /* 7019 * Resume the watch thread since it was suspended 7020 * when the device went into low power mode. 7021 */ 7022 if (un->un_f_monitor_media_state) { 7023 mutex_enter(SD_MUTEX(un)); 7024 if (un->un_f_watcht_stopped == TRUE) { 7025 opaque_t temp_token; 7026 7027 un->un_f_watcht_stopped = FALSE; 7028 mutex_exit(SD_MUTEX(un)); 7029 temp_token = 7030 sd_watch_request_submit(un); 7031 mutex_enter(SD_MUTEX(un)); 7032 un->un_swr_token = temp_token; 7033 } 7034 mutex_exit(SD_MUTEX(un)); 7035 } 7036 } 7037 } 7038 7039 if (got_semaphore_here != 0) { 7040 sema_v(&un->un_semoclose); 7041 } 7042 /* 7043 * On exit put the state back to it's original value 7044 * and broadcast to anyone waiting for the power 7045 * change completion. 7046 */ 7047 mutex_enter(SD_MUTEX(un)); 7048 un->un_state = state_before_pm; 7049 cv_broadcast(&un->un_suspend_cv); 7050 mutex_exit(SD_MUTEX(un)); 7051 7052 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7053 7054 sd_ssc_fini(ssc); 7055 return (rval); 7056 7057 sdpower_failed: 7058 7059 sd_ssc_fini(ssc); 7060 return (DDI_FAILURE); 7061 } 7062 7063 7064 7065 /* 7066 * Function: sdattach 7067 * 7068 * Description: Driver's attach(9e) entry point function. 7069 * 7070 * Arguments: devi - opaque device info handle 7071 * cmd - attach type 7072 * 7073 * Return Code: DDI_SUCCESS 7074 * DDI_FAILURE 7075 * 7076 * Context: Kernel thread context 7077 */ 7078 7079 static int 7080 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7081 { 7082 switch (cmd) { 7083 case DDI_ATTACH: 7084 return (sd_unit_attach(devi)); 7085 case DDI_RESUME: 7086 return (sd_ddi_resume(devi)); 7087 default: 7088 break; 7089 } 7090 return (DDI_FAILURE); 7091 } 7092 7093 7094 /* 7095 * Function: sddetach 7096 * 7097 * Description: Driver's detach(9E) entry point function. 7098 * 7099 * Arguments: devi - opaque device info handle 7100 * cmd - detach type 7101 * 7102 * Return Code: DDI_SUCCESS 7103 * DDI_FAILURE 7104 * 7105 * Context: Kernel thread context 7106 */ 7107 7108 static int 7109 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7110 { 7111 switch (cmd) { 7112 case DDI_DETACH: 7113 return (sd_unit_detach(devi)); 7114 case DDI_SUSPEND: 7115 return (sd_ddi_suspend(devi)); 7116 default: 7117 break; 7118 } 7119 return (DDI_FAILURE); 7120 } 7121 7122 7123 /* 7124 * Function: sd_sync_with_callback 7125 * 7126 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7127 * state while the callback routine is active. 7128 * 7129 * Arguments: un: softstate structure for the instance 7130 * 7131 * Context: Kernel thread context 7132 */ 7133 7134 static void 7135 sd_sync_with_callback(struct sd_lun *un) 7136 { 7137 ASSERT(un != NULL); 7138 7139 mutex_enter(SD_MUTEX(un)); 7140 7141 ASSERT(un->un_in_callback >= 0); 7142 7143 while (un->un_in_callback > 0) { 7144 mutex_exit(SD_MUTEX(un)); 7145 delay(2); 7146 mutex_enter(SD_MUTEX(un)); 7147 } 7148 7149 mutex_exit(SD_MUTEX(un)); 7150 } 7151 7152 /* 7153 * Function: sd_unit_attach 7154 * 7155 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7156 * the soft state structure for the device and performs 7157 * all necessary structure and device initializations. 7158 * 7159 * Arguments: devi: the system's dev_info_t for the device. 7160 * 7161 * Return Code: DDI_SUCCESS if attach is successful. 7162 * DDI_FAILURE if any part of the attach fails. 7163 * 7164 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7165 * Kernel thread context only. Can sleep. 7166 */ 7167 7168 static int 7169 sd_unit_attach(dev_info_t *devi) 7170 { 7171 struct scsi_device *devp; 7172 struct sd_lun *un; 7173 char *variantp; 7174 char name_str[48]; 7175 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7176 int instance; 7177 int rval; 7178 int wc_enabled; 7179 int tgt; 7180 dev_info_t *pdip = ddi_get_parent(devi); 7181 int offbyone = 0; 7182 int geom_label_valid = 0; 7183 sd_ssc_t *ssc; 7184 int status; 7185 struct sd_fm_internal *sfip = NULL; 7186 int max_xfer_size; 7187 7188 /* 7189 * Retrieve the target driver's private data area. This was set 7190 * up by the HBA. 7191 */ 7192 devp = ddi_get_driver_private(devi); 7193 7194 /* 7195 * Retrieve the target ID of the device. 7196 */ 7197 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7198 SCSI_ADDR_PROP_TARGET, -1); 7199 7200 /* 7201 * Since we have no idea what state things were left in by the last 7202 * user of the device, set up some 'default' settings, ie. turn 'em 7203 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7204 * Do this before the scsi_probe, which sends an inquiry. 7205 * This is a fix for bug (4430280). 7206 * Of special importance is wide-xfer. The drive could have been left 7207 * in wide transfer mode by the last driver to communicate with it, 7208 * this includes us. If that's the case, and if the following is not 7209 * setup properly or we don't re-negotiate with the drive prior to 7210 * transferring data to/from the drive, it causes bus parity errors, 7211 * data overruns, and unexpected interrupts. This first occurred when 7212 * the fix for bug (4378686) was made. 7213 */ 7214 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 7215 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 7216 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 7217 7218 /* 7219 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 7220 * on a target. Setting it per lun instance actually sets the 7221 * capability of this target, which affects those luns already 7222 * attached on the same target. So during attach, we can only disable 7223 * this capability only when no other lun has been attached on this 7224 * target. By doing this, we assume a target has the same tagged-qing 7225 * capability for every lun. The condition can be removed when HBA 7226 * is changed to support per lun based tagged-qing capability. 7227 */ 7228 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7229 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 7230 } 7231 7232 /* 7233 * Use scsi_probe() to issue an INQUIRY command to the device. 7234 * This call will allocate and fill in the scsi_inquiry structure 7235 * and point the sd_inq member of the scsi_device structure to it. 7236 * If the attach succeeds, then this memory will not be de-allocated 7237 * (via scsi_unprobe()) until the instance is detached. 7238 */ 7239 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 7240 goto probe_failed; 7241 } 7242 7243 /* 7244 * Check the device type as specified in the inquiry data and 7245 * claim it if it is of a type that we support. 7246 */ 7247 switch (devp->sd_inq->inq_dtype) { 7248 case DTYPE_DIRECT: 7249 break; 7250 case DTYPE_RODIRECT: 7251 break; 7252 case DTYPE_OPTICAL: 7253 break; 7254 case DTYPE_NOTPRESENT: 7255 default: 7256 /* Unsupported device type; fail the attach. */ 7257 goto probe_failed; 7258 } 7259 7260 /* 7261 * Allocate the soft state structure for this unit. 7262 * 7263 * We rely upon this memory being set to all zeroes by 7264 * ddi_soft_state_zalloc(). We assume that any member of the 7265 * soft state structure that is not explicitly initialized by 7266 * this routine will have a value of zero. 7267 */ 7268 instance = ddi_get_instance(devp->sd_dev); 7269 #ifndef XPV_HVM_DRIVER 7270 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 7271 goto probe_failed; 7272 } 7273 #endif /* !XPV_HVM_DRIVER */ 7274 7275 /* 7276 * Retrieve a pointer to the newly-allocated soft state. 7277 * 7278 * This should NEVER fail if the ddi_soft_state_zalloc() call above 7279 * was successful, unless something has gone horribly wrong and the 7280 * ddi's soft state internals are corrupt (in which case it is 7281 * probably better to halt here than just fail the attach....) 7282 */ 7283 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 7284 panic("sd_unit_attach: NULL soft state on instance:0x%x", 7285 instance); 7286 /*NOTREACHED*/ 7287 } 7288 7289 /* 7290 * Link the back ptr of the driver soft state to the scsi_device 7291 * struct for this lun. 7292 * Save a pointer to the softstate in the driver-private area of 7293 * the scsi_device struct. 7294 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 7295 * we first set un->un_sd below. 7296 */ 7297 un->un_sd = devp; 7298 devp->sd_private = (opaque_t)un; 7299 7300 /* 7301 * The following must be after devp is stored in the soft state struct. 7302 */ 7303 #ifdef SDDEBUG 7304 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7305 "%s_unit_attach: un:0x%p instance:%d\n", 7306 ddi_driver_name(devi), un, instance); 7307 #endif 7308 7309 /* 7310 * Set up the device type and node type (for the minor nodes). 7311 * By default we assume that the device can at least support the 7312 * Common Command Set. Call it a CD-ROM if it reports itself 7313 * as a RODIRECT device. 7314 */ 7315 switch (devp->sd_inq->inq_dtype) { 7316 case DTYPE_RODIRECT: 7317 un->un_node_type = DDI_NT_CD_CHAN; 7318 un->un_ctype = CTYPE_CDROM; 7319 break; 7320 case DTYPE_OPTICAL: 7321 un->un_node_type = DDI_NT_BLOCK_CHAN; 7322 un->un_ctype = CTYPE_ROD; 7323 break; 7324 default: 7325 un->un_node_type = DDI_NT_BLOCK_CHAN; 7326 un->un_ctype = CTYPE_CCS; 7327 break; 7328 } 7329 7330 /* 7331 * Try to read the interconnect type from the HBA. 7332 * 7333 * Note: This driver is currently compiled as two binaries, a parallel 7334 * scsi version (sd) and a fibre channel version (ssd). All functional 7335 * differences are determined at compile time. In the future a single 7336 * binary will be provided and the interconnect type will be used to 7337 * differentiate between fibre and parallel scsi behaviors. At that time 7338 * it will be necessary for all fibre channel HBAs to support this 7339 * property. 7340 * 7341 * set un_f_is_fiber to TRUE ( default fiber ) 7342 */ 7343 un->un_f_is_fibre = TRUE; 7344 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 7345 case INTERCONNECT_SSA: 7346 un->un_interconnect_type = SD_INTERCONNECT_SSA; 7347 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7348 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 7349 break; 7350 case INTERCONNECT_PARALLEL: 7351 un->un_f_is_fibre = FALSE; 7352 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7353 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7354 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 7355 break; 7356 case INTERCONNECT_SAS: 7357 un->un_f_is_fibre = FALSE; 7358 un->un_interconnect_type = SD_INTERCONNECT_SAS; 7359 un->un_node_type = DDI_NT_BLOCK_SAS; 7360 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7361 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un); 7362 break; 7363 case INTERCONNECT_SATA: 7364 un->un_f_is_fibre = FALSE; 7365 un->un_interconnect_type = SD_INTERCONNECT_SATA; 7366 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7367 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 7368 break; 7369 case INTERCONNECT_FIBRE: 7370 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7371 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7372 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7373 break; 7374 case INTERCONNECT_FABRIC: 7375 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7376 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7377 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7378 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7379 break; 7380 default: 7381 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7382 /* 7383 * The HBA does not support the "interconnect-type" property 7384 * (or did not provide a recognized type). 7385 * 7386 * Note: This will be obsoleted when a single fibre channel 7387 * and parallel scsi driver is delivered. In the meantime the 7388 * interconnect type will be set to the platform default.If that 7389 * type is not parallel SCSI, it means that we should be 7390 * assuming "ssd" semantics. However, here this also means that 7391 * the FC HBA is not supporting the "interconnect-type" property 7392 * like we expect it to, so log this occurrence. 7393 */ 7394 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7395 if (!SD_IS_PARALLEL_SCSI(un)) { 7396 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7397 "sd_unit_attach: un:0x%p Assuming " 7398 "INTERCONNECT_FIBRE\n", un); 7399 } else { 7400 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7401 "sd_unit_attach: un:0x%p Assuming " 7402 "INTERCONNECT_PARALLEL\n", un); 7403 un->un_f_is_fibre = FALSE; 7404 } 7405 #else 7406 /* 7407 * Note: This source will be implemented when a single fibre 7408 * channel and parallel scsi driver is delivered. The default 7409 * will be to assume that if a device does not support the 7410 * "interconnect-type" property it is a parallel SCSI HBA and 7411 * we will set the interconnect type for parallel scsi. 7412 */ 7413 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7414 un->un_f_is_fibre = FALSE; 7415 #endif 7416 break; 7417 } 7418 7419 if (un->un_f_is_fibre == TRUE) { 7420 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7421 SCSI_VERSION_3) { 7422 switch (un->un_interconnect_type) { 7423 case SD_INTERCONNECT_FIBRE: 7424 case SD_INTERCONNECT_SSA: 7425 un->un_node_type = DDI_NT_BLOCK_WWN; 7426 break; 7427 default: 7428 break; 7429 } 7430 } 7431 } 7432 7433 /* 7434 * Initialize the Request Sense command for the target 7435 */ 7436 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7437 goto alloc_rqs_failed; 7438 } 7439 7440 /* 7441 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7442 * with separate binary for sd and ssd. 7443 * 7444 * x86 has 1 binary, un_retry_count is set base on connection type. 7445 * The hardcoded values will go away when Sparc uses 1 binary 7446 * for sd and ssd. This hardcoded values need to match 7447 * SD_RETRY_COUNT in sddef.h 7448 * The value used is base on interconnect type. 7449 * fibre = 3, parallel = 5 7450 */ 7451 #if defined(__i386) || defined(__amd64) 7452 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7453 #else 7454 un->un_retry_count = SD_RETRY_COUNT; 7455 #endif 7456 7457 /* 7458 * Set the per disk retry count to the default number of retries 7459 * for disks and CDROMs. This value can be overridden by the 7460 * disk property list or an entry in sd.conf. 7461 */ 7462 un->un_notready_retry_count = 7463 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7464 : DISK_NOT_READY_RETRY_COUNT(un); 7465 7466 /* 7467 * Set the busy retry count to the default value of un_retry_count. 7468 * This can be overridden by entries in sd.conf or the device 7469 * config table. 7470 */ 7471 un->un_busy_retry_count = un->un_retry_count; 7472 7473 /* 7474 * Init the reset threshold for retries. This number determines 7475 * how many retries must be performed before a reset can be issued 7476 * (for certain error conditions). This can be overridden by entries 7477 * in sd.conf or the device config table. 7478 */ 7479 un->un_reset_retry_count = (un->un_retry_count / 2); 7480 7481 /* 7482 * Set the victim_retry_count to the default un_retry_count 7483 */ 7484 un->un_victim_retry_count = (2 * un->un_retry_count); 7485 7486 /* 7487 * Set the reservation release timeout to the default value of 7488 * 5 seconds. This can be overridden by entries in ssd.conf or the 7489 * device config table. 7490 */ 7491 un->un_reserve_release_time = 5; 7492 7493 /* 7494 * Set up the default maximum transfer size. Note that this may 7495 * get updated later in the attach, when setting up default wide 7496 * operations for disks. 7497 */ 7498 #if defined(__i386) || defined(__amd64) 7499 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7500 un->un_partial_dma_supported = 1; 7501 #else 7502 un->un_max_xfer_size = (uint_t)maxphys; 7503 #endif 7504 7505 /* 7506 * Get "allow bus device reset" property (defaults to "enabled" if 7507 * the property was not defined). This is to disable bus resets for 7508 * certain kinds of error recovery. Note: In the future when a run-time 7509 * fibre check is available the soft state flag should default to 7510 * enabled. 7511 */ 7512 if (un->un_f_is_fibre == TRUE) { 7513 un->un_f_allow_bus_device_reset = TRUE; 7514 } else { 7515 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7516 "allow-bus-device-reset", 1) != 0) { 7517 un->un_f_allow_bus_device_reset = TRUE; 7518 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7519 "sd_unit_attach: un:0x%p Bus device reset " 7520 "enabled\n", un); 7521 } else { 7522 un->un_f_allow_bus_device_reset = FALSE; 7523 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7524 "sd_unit_attach: un:0x%p Bus device reset " 7525 "disabled\n", un); 7526 } 7527 } 7528 7529 /* 7530 * Check if this is an ATAPI device. ATAPI devices use Group 1 7531 * Read/Write commands and Group 2 Mode Sense/Select commands. 7532 * 7533 * Note: The "obsolete" way of doing this is to check for the "atapi" 7534 * property. The new "variant" property with a value of "atapi" has been 7535 * introduced so that future 'variants' of standard SCSI behavior (like 7536 * atapi) could be specified by the underlying HBA drivers by supplying 7537 * a new value for the "variant" property, instead of having to define a 7538 * new property. 7539 */ 7540 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 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 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7546 &variantp) == DDI_PROP_SUCCESS) { 7547 if (strcmp(variantp, "atapi") == 0) { 7548 un->un_f_cfg_is_atapi = TRUE; 7549 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7550 "sd_unit_attach: un:0x%p Atapi device\n", un); 7551 } 7552 ddi_prop_free(variantp); 7553 } 7554 7555 un->un_cmd_timeout = SD_IO_TIME; 7556 7557 un->un_busy_timeout = SD_BSY_TIMEOUT; 7558 7559 /* Info on current states, statuses, etc. (Updated frequently) */ 7560 un->un_state = SD_STATE_NORMAL; 7561 un->un_last_state = SD_STATE_NORMAL; 7562 7563 /* Control & status info for command throttling */ 7564 un->un_throttle = sd_max_throttle; 7565 un->un_saved_throttle = sd_max_throttle; 7566 un->un_min_throttle = sd_min_throttle; 7567 7568 if (un->un_f_is_fibre == TRUE) { 7569 un->un_f_use_adaptive_throttle = TRUE; 7570 } else { 7571 un->un_f_use_adaptive_throttle = FALSE; 7572 } 7573 7574 /* Removable media support. */ 7575 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7576 un->un_mediastate = DKIO_NONE; 7577 un->un_specified_mediastate = DKIO_NONE; 7578 7579 /* CVs for suspend/resume (PM or DR) */ 7580 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7581 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7582 7583 /* Power management support. */ 7584 un->un_power_level = SD_SPINDLE_UNINIT; 7585 7586 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 7587 un->un_f_wcc_inprog = 0; 7588 7589 /* 7590 * The open/close semaphore is used to serialize threads executing 7591 * in the driver's open & close entry point routines for a given 7592 * instance. 7593 */ 7594 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 7595 7596 /* 7597 * The conf file entry and softstate variable is a forceful override, 7598 * meaning a non-zero value must be entered to change the default. 7599 */ 7600 un->un_f_disksort_disabled = FALSE; 7601 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT; 7602 un->un_f_enable_rmw = FALSE; 7603 7604 /* 7605 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but 7606 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property. 7607 */ 7608 un->un_f_mmc_gesn_polling = TRUE; 7609 7610 /* 7611 * Retrieve the properties from the static driver table or the driver 7612 * configuration file (.conf) for this unit and update the soft state 7613 * for the device as needed for the indicated properties. 7614 * Note: the property configuration needs to occur here as some of the 7615 * following routines may have dependencies on soft state flags set 7616 * as part of the driver property configuration. 7617 */ 7618 sd_read_unit_properties(un); 7619 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7620 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 7621 7622 /* 7623 * Only if a device has "hotpluggable" property, it is 7624 * treated as hotpluggable device. Otherwise, it is 7625 * regarded as non-hotpluggable one. 7626 */ 7627 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 7628 -1) != -1) { 7629 un->un_f_is_hotpluggable = TRUE; 7630 } 7631 7632 /* 7633 * set unit's attributes(flags) according to "hotpluggable" and 7634 * RMB bit in INQUIRY data. 7635 */ 7636 sd_set_unit_attributes(un, devi); 7637 7638 /* 7639 * By default, we mark the capacity, lbasize, and geometry 7640 * as invalid. Only if we successfully read a valid capacity 7641 * will we update the un_blockcount and un_tgt_blocksize with the 7642 * valid values (the geometry will be validated later). 7643 */ 7644 un->un_f_blockcount_is_valid = FALSE; 7645 un->un_f_tgt_blocksize_is_valid = FALSE; 7646 7647 /* 7648 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 7649 * otherwise. 7650 */ 7651 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 7652 un->un_blockcount = 0; 7653 7654 /* 7655 * physical sector size default to DEV_BSIZE currently. 7656 */ 7657 un->un_phy_blocksize = DEV_BSIZE; 7658 7659 /* 7660 * Set up the per-instance info needed to determine the correct 7661 * CDBs and other info for issuing commands to the target. 7662 */ 7663 sd_init_cdb_limits(un); 7664 7665 /* 7666 * Set up the IO chains to use, based upon the target type. 7667 */ 7668 if (un->un_f_non_devbsize_supported) { 7669 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7670 } else { 7671 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7672 } 7673 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7674 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 7675 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 7676 7677 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 7678 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 7679 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 7680 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 7681 7682 7683 if (ISCD(un)) { 7684 un->un_additional_codes = sd_additional_codes; 7685 } else { 7686 un->un_additional_codes = NULL; 7687 } 7688 7689 /* 7690 * Create the kstats here so they can be available for attach-time 7691 * routines that send commands to the unit (either polled or via 7692 * sd_send_scsi_cmd). 7693 * 7694 * Note: This is a critical sequence that needs to be maintained: 7695 * 1) Instantiate the kstats here, before any routines using the 7696 * iopath (i.e. sd_send_scsi_cmd). 7697 * 2) Instantiate and initialize the partition stats 7698 * (sd_set_pstats). 7699 * 3) Initialize the error stats (sd_set_errstats), following 7700 * sd_validate_geometry(),sd_register_devid(), 7701 * and sd_cache_control(). 7702 */ 7703 7704 un->un_stats = kstat_create(sd_label, instance, 7705 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 7706 if (un->un_stats != NULL) { 7707 un->un_stats->ks_lock = SD_MUTEX(un); 7708 kstat_install(un->un_stats); 7709 } 7710 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7711 "sd_unit_attach: un:0x%p un_stats created\n", un); 7712 7713 sd_create_errstats(un, instance); 7714 if (un->un_errstats == NULL) { 7715 goto create_errstats_failed; 7716 } 7717 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7718 "sd_unit_attach: un:0x%p errstats created\n", un); 7719 7720 /* 7721 * The following if/else code was relocated here from below as part 7722 * of the fix for bug (4430280). However with the default setup added 7723 * on entry to this routine, it's no longer absolutely necessary for 7724 * this to be before the call to sd_spin_up_unit. 7725 */ 7726 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 7727 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) || 7728 (devp->sd_inq->inq_ansi == 5)) && 7729 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque; 7730 7731 /* 7732 * If tagged queueing is supported by the target 7733 * and by the host adapter then we will enable it 7734 */ 7735 un->un_tagflags = 0; 7736 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag && 7737 (un->un_f_arq_enabled == TRUE)) { 7738 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 7739 1, 1) == 1) { 7740 un->un_tagflags = FLAG_STAG; 7741 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7742 "sd_unit_attach: un:0x%p tag queueing " 7743 "enabled\n", un); 7744 } else if (scsi_ifgetcap(SD_ADDRESS(un), 7745 "untagged-qing", 0) == 1) { 7746 un->un_f_opt_queueing = TRUE; 7747 un->un_saved_throttle = un->un_throttle = 7748 min(un->un_throttle, 3); 7749 } else { 7750 un->un_f_opt_queueing = FALSE; 7751 un->un_saved_throttle = un->un_throttle = 1; 7752 } 7753 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 7754 == 1) && (un->un_f_arq_enabled == TRUE)) { 7755 /* The Host Adapter supports internal queueing. */ 7756 un->un_f_opt_queueing = TRUE; 7757 un->un_saved_throttle = un->un_throttle = 7758 min(un->un_throttle, 3); 7759 } else { 7760 un->un_f_opt_queueing = FALSE; 7761 un->un_saved_throttle = un->un_throttle = 1; 7762 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7763 "sd_unit_attach: un:0x%p no tag queueing\n", un); 7764 } 7765 7766 /* 7767 * Enable large transfers for SATA/SAS drives 7768 */ 7769 if (SD_IS_SERIAL(un)) { 7770 un->un_max_xfer_size = 7771 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7772 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7773 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7774 "sd_unit_attach: un:0x%p max transfer " 7775 "size=0x%x\n", un, un->un_max_xfer_size); 7776 7777 } 7778 7779 /* Setup or tear down default wide operations for disks */ 7780 7781 /* 7782 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 7783 * and "ssd_max_xfer_size" to exist simultaneously on the same 7784 * system and be set to different values. In the future this 7785 * code may need to be updated when the ssd module is 7786 * obsoleted and removed from the system. (4299588) 7787 */ 7788 if (SD_IS_PARALLEL_SCSI(un) && 7789 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 7790 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 7791 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7792 1, 1) == 1) { 7793 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7794 "sd_unit_attach: un:0x%p Wide Transfer " 7795 "enabled\n", un); 7796 } 7797 7798 /* 7799 * If tagged queuing has also been enabled, then 7800 * enable large xfers 7801 */ 7802 if (un->un_saved_throttle == sd_max_throttle) { 7803 un->un_max_xfer_size = 7804 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7805 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7806 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7807 "sd_unit_attach: un:0x%p max transfer " 7808 "size=0x%x\n", un, un->un_max_xfer_size); 7809 } 7810 } else { 7811 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7812 0, 1) == 1) { 7813 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7814 "sd_unit_attach: un:0x%p " 7815 "Wide Transfer disabled\n", un); 7816 } 7817 } 7818 } else { 7819 un->un_tagflags = FLAG_STAG; 7820 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 7821 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 7822 } 7823 7824 /* 7825 * If this target supports LUN reset, try to enable it. 7826 */ 7827 if (un->un_f_lun_reset_enabled) { 7828 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 7829 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7830 "un:0x%p lun_reset capability set\n", un); 7831 } else { 7832 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7833 "un:0x%p lun-reset capability not set\n", un); 7834 } 7835 } 7836 7837 /* 7838 * Adjust the maximum transfer size. This is to fix 7839 * the problem of partial DMA support on SPARC. Some 7840 * HBA driver, like aac, has very small dma_attr_maxxfer 7841 * size, which requires partial DMA support on SPARC. 7842 * In the future the SPARC pci nexus driver may solve 7843 * the problem instead of this fix. 7844 */ 7845 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 7846 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 7847 /* We need DMA partial even on sparc to ensure sddump() works */ 7848 un->un_max_xfer_size = max_xfer_size; 7849 if (un->un_partial_dma_supported == 0) 7850 un->un_partial_dma_supported = 1; 7851 } 7852 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7853 DDI_PROP_DONTPASS, "buf_break", 0) == 1) { 7854 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr, 7855 un->un_max_xfer_size) == 1) { 7856 un->un_buf_breakup_supported = 1; 7857 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7858 "un:0x%p Buf breakup enabled\n", un); 7859 } 7860 } 7861 7862 /* 7863 * Set PKT_DMA_PARTIAL flag. 7864 */ 7865 if (un->un_partial_dma_supported == 1) { 7866 un->un_pkt_flags = PKT_DMA_PARTIAL; 7867 } else { 7868 un->un_pkt_flags = 0; 7869 } 7870 7871 /* Initialize sd_ssc_t for internal uscsi commands */ 7872 ssc = sd_ssc_init(un); 7873 scsi_fm_init(devp); 7874 7875 /* 7876 * Allocate memory for SCSI FMA stuffs. 7877 */ 7878 un->un_fm_private = 7879 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP); 7880 sfip = (struct sd_fm_internal *)un->un_fm_private; 7881 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd; 7882 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo; 7883 sfip->fm_ssc.ssc_un = un; 7884 7885 if (ISCD(un) || 7886 un->un_f_has_removable_media || 7887 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) { 7888 /* 7889 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device. 7890 * Their log are unchanged. 7891 */ 7892 sfip->fm_log_level = SD_FM_LOG_NSUP; 7893 } else { 7894 /* 7895 * If enter here, it should be non-CDROM and FM-capable 7896 * device, and it will not keep the old scsi_log as before 7897 * in /var/adm/messages. However, the property 7898 * "fm-scsi-log" will control whether the FM telemetry will 7899 * be logged in /var/adm/messages. 7900 */ 7901 int fm_scsi_log; 7902 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7903 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0); 7904 7905 if (fm_scsi_log) 7906 sfip->fm_log_level = SD_FM_LOG_EREPORT; 7907 else 7908 sfip->fm_log_level = SD_FM_LOG_SILENT; 7909 } 7910 7911 /* 7912 * At this point in the attach, we have enough info in the 7913 * soft state to be able to issue commands to the target. 7914 * 7915 * All command paths used below MUST issue their commands as 7916 * SD_PATH_DIRECT. This is important as intermediate layers 7917 * are not all initialized yet (such as PM). 7918 */ 7919 7920 /* 7921 * Send a TEST UNIT READY command to the device. This should clear 7922 * any outstanding UNIT ATTENTION that may be present. 7923 * 7924 * Note: Don't check for success, just track if there is a reservation, 7925 * this is a throw away command to clear any unit attentions. 7926 * 7927 * Note: This MUST be the first command issued to the target during 7928 * attach to ensure power on UNIT ATTENTIONS are cleared. 7929 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7930 * with attempts at spinning up a device with no media. 7931 */ 7932 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 7933 if (status != 0) { 7934 if (status == EACCES) 7935 reservation_flag = SD_TARGET_IS_RESERVED; 7936 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7937 } 7938 7939 /* 7940 * If the device is NOT a removable media device, attempt to spin 7941 * it up (using the START_STOP_UNIT command) and read its capacity 7942 * (using the READ CAPACITY command). Note, however, that either 7943 * of these could fail and in some cases we would continue with 7944 * the attach despite the failure (see below). 7945 */ 7946 if (ISDIRECT(un)) { 7947 7948 switch (sd_spin_up_unit(ssc)) { 7949 case 0: 7950 /* 7951 * Spin-up was successful; now try to read the 7952 * capacity. If successful then save the results 7953 * and mark the capacity & lbasize as valid. 7954 */ 7955 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7956 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7957 7958 status = sd_read_capacity(ssc, SD_PATH_DIRECT); 7959 switch (status) { 7960 case 0: { 7961 if (un->un_blockcount > DK_MAX_BLOCKS) { 7962 #ifdef _LP64 7963 if ((un->un_blockcount + 1) > 7964 SD_GROUP1_MAX_ADDRESS) { 7965 /* 7966 * Enable descriptor format 7967 * sense data so that we can 7968 * get 64 bit sense data 7969 * fields. 7970 */ 7971 sd_enable_descr_sense(ssc); 7972 } 7973 #else 7974 /* 32-bit kernels can't handle this */ 7975 scsi_log(SD_DEVINFO(un), 7976 sd_label, CE_WARN, 7977 "disk has %llu blocks, which " 7978 "is too large for a 32-bit " 7979 "kernel", un->un_blockcount); 7980 7981 #if defined(__i386) || defined(__amd64) 7982 /* 7983 * 1TB disk was treated as (1T - 512)B 7984 * in the past, so that it might have 7985 * valid VTOC and solaris partitions, 7986 * we have to allow it to continue to 7987 * work. 7988 */ 7989 if (un->un_blockcount - 1 > 7990 DK_MAX_BLOCKS) 7991 #endif 7992 goto spinup_failed; 7993 #endif 7994 } 7995 7996 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7997 "sd_unit_attach: un:0x%p capacity = %lld " 7998 "blocks; lbasize= %ld.\n", un, 7999 un->un_blockcount, un->un_tgt_blocksize); 8000 8001 break; 8002 } 8003 case EINVAL: 8004 /* 8005 * In the case where the max-cdb-length property 8006 * is smaller than the required CDB length for 8007 * a SCSI device, a target driver can fail to 8008 * attach to that device. 8009 */ 8010 scsi_log(SD_DEVINFO(un), 8011 sd_label, CE_WARN, 8012 "disk capacity is too large " 8013 "for current cdb length"); 8014 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8015 8016 goto spinup_failed; 8017 case EACCES: 8018 /* 8019 * Should never get here if the spin-up 8020 * succeeded, but code it in anyway. 8021 * From here, just continue with the attach... 8022 */ 8023 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8024 "sd_unit_attach: un:0x%p " 8025 "sd_send_scsi_READ_CAPACITY " 8026 "returned reservation conflict\n", un); 8027 reservation_flag = SD_TARGET_IS_RESERVED; 8028 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8029 break; 8030 default: 8031 /* 8032 * Likewise, should never get here if the 8033 * spin-up succeeded. Just continue with 8034 * the attach... 8035 */ 8036 if (status == EIO) 8037 sd_ssc_assessment(ssc, 8038 SD_FMT_STATUS_CHECK); 8039 else 8040 sd_ssc_assessment(ssc, 8041 SD_FMT_IGNORE); 8042 break; 8043 } 8044 break; 8045 case EACCES: 8046 /* 8047 * Device is reserved by another host. In this case 8048 * we could not spin it up or read the capacity, but 8049 * we continue with the attach anyway. 8050 */ 8051 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8052 "sd_unit_attach: un:0x%p spin-up reservation " 8053 "conflict.\n", un); 8054 reservation_flag = SD_TARGET_IS_RESERVED; 8055 break; 8056 default: 8057 /* Fail the attach if the spin-up failed. */ 8058 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8059 "sd_unit_attach: un:0x%p spin-up failed.", un); 8060 goto spinup_failed; 8061 } 8062 8063 } 8064 8065 /* 8066 * Check to see if this is a MMC drive 8067 */ 8068 if (ISCD(un)) { 8069 sd_set_mmc_caps(ssc); 8070 } 8071 8072 /* 8073 * Add a zero-length attribute to tell the world we support 8074 * kernel ioctls (for layered drivers) 8075 */ 8076 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8077 DDI_KERNEL_IOCTL, NULL, 0); 8078 8079 /* 8080 * Add a boolean property to tell the world we support 8081 * the B_FAILFAST flag (for layered drivers) 8082 */ 8083 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8084 "ddi-failfast-supported", NULL, 0); 8085 8086 /* 8087 * Initialize power management 8088 */ 8089 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8090 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8091 sd_setup_pm(ssc, devi); 8092 if (un->un_f_pm_is_enabled == FALSE) { 8093 /* 8094 * For performance, point to a jump table that does 8095 * not include pm. 8096 * The direct and priority chains don't change with PM. 8097 * 8098 * Note: this is currently done based on individual device 8099 * capabilities. When an interface for determining system 8100 * power enabled state becomes available, or when additional 8101 * layers are added to the command chain, these values will 8102 * have to be re-evaluated for correctness. 8103 */ 8104 if (un->un_f_non_devbsize_supported) { 8105 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8106 } else { 8107 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8108 } 8109 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8110 } 8111 8112 /* 8113 * This property is set to 0 by HA software to avoid retries 8114 * on a reserved disk. (The preferred property name is 8115 * "retry-on-reservation-conflict") (1189689) 8116 * 8117 * Note: The use of a global here can have unintended consequences. A 8118 * per instance variable is preferable to match the capabilities of 8119 * different underlying hba's (4402600) 8120 */ 8121 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8122 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8123 sd_retry_on_reservation_conflict); 8124 if (sd_retry_on_reservation_conflict != 0) { 8125 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8126 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8127 sd_retry_on_reservation_conflict); 8128 } 8129 8130 /* Set up options for QFULL handling. */ 8131 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8132 "qfull-retries", -1)) != -1) { 8133 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8134 rval, 1); 8135 } 8136 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8137 "qfull-retry-interval", -1)) != -1) { 8138 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8139 rval, 1); 8140 } 8141 8142 /* 8143 * This just prints a message that announces the existence of the 8144 * device. The message is always printed in the system logfile, but 8145 * only appears on the console if the system is booted with the 8146 * -v (verbose) argument. 8147 */ 8148 ddi_report_dev(devi); 8149 8150 un->un_mediastate = DKIO_NONE; 8151 8152 /* 8153 * Check if this device supports vital product descriptor pages. 8154 */ 8155 (void) sd_check_vpd_page_support(ssc); 8156 8157 /* 8158 * Check if this is a SSD(Solid State Drive). 8159 */ 8160 sd_check_solid_state(ssc); 8161 8162 8163 cmlb_alloc_handle(&un->un_cmlbhandle); 8164 8165 #if defined(__i386) || defined(__amd64) 8166 /* 8167 * On x86, compensate for off-by-1 legacy error 8168 */ 8169 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 8170 (un->un_tgt_blocksize == un->un_sys_blocksize)) 8171 offbyone = CMLB_OFF_BY_ONE; 8172 #endif 8173 8174 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 8175 VOID2BOOLEAN(un->un_f_has_removable_media != 0), 8176 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0), 8177 un->un_node_type, offbyone, un->un_cmlbhandle, 8178 (void *)SD_PATH_DIRECT) != 0) { 8179 goto cmlb_attach_failed; 8180 } 8181 8182 8183 /* 8184 * Read and validate the device's geometry (ie, disk label) 8185 * A new unformatted drive will not have a valid geometry, but 8186 * the driver needs to successfully attach to this device so 8187 * the drive can be formatted via ioctls. 8188 */ 8189 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 8190 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 8191 8192 mutex_enter(SD_MUTEX(un)); 8193 8194 /* 8195 * Read and initialize the devid for the unit. 8196 */ 8197 if (un->un_f_devid_supported) { 8198 sd_register_devid(ssc, devi, reservation_flag); 8199 } 8200 mutex_exit(SD_MUTEX(un)); 8201 8202 #if (defined(__fibre)) 8203 /* 8204 * Register callbacks for fibre only. You can't do this solely 8205 * on the basis of the devid_type because this is hba specific. 8206 * We need to query our hba capabilities to find out whether to 8207 * register or not. 8208 */ 8209 if (un->un_f_is_fibre) { 8210 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8211 sd_init_event_callbacks(un); 8212 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8213 "sd_unit_attach: un:0x%p event callbacks inserted", 8214 un); 8215 } 8216 } 8217 #endif 8218 8219 if (un->un_f_opt_disable_cache == TRUE) { 8220 /* 8221 * Disable both read cache and write cache. This is 8222 * the historic behavior of the keywords in the config file. 8223 */ 8224 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8225 0) { 8226 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8227 "sd_unit_attach: un:0x%p Could not disable " 8228 "caching", un); 8229 goto devid_failed; 8230 } 8231 } 8232 8233 /* 8234 * Check the value of the WCE bit now and 8235 * set un_f_write_cache_enabled accordingly. 8236 */ 8237 (void) sd_get_write_cache_enabled(ssc, &wc_enabled); 8238 mutex_enter(SD_MUTEX(un)); 8239 un->un_f_write_cache_enabled = (wc_enabled != 0); 8240 mutex_exit(SD_MUTEX(un)); 8241 8242 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR && 8243 un->un_tgt_blocksize != DEV_BSIZE) || 8244 un->un_f_enable_rmw) { 8245 if (!(un->un_wm_cache)) { 8246 (void) snprintf(name_str, sizeof (name_str), 8247 "%s%d_cache", 8248 ddi_driver_name(SD_DEVINFO(un)), 8249 ddi_get_instance(SD_DEVINFO(un))); 8250 un->un_wm_cache = kmem_cache_create( 8251 name_str, sizeof (struct sd_w_map), 8252 8, sd_wm_cache_constructor, 8253 sd_wm_cache_destructor, NULL, 8254 (void *)un, NULL, 0); 8255 if (!(un->un_wm_cache)) { 8256 goto wm_cache_failed; 8257 } 8258 } 8259 } 8260 8261 /* 8262 * Check the value of the NV_SUP bit and set 8263 * un_f_suppress_cache_flush accordingly. 8264 */ 8265 sd_get_nv_sup(ssc); 8266 8267 /* 8268 * Find out what type of reservation this disk supports. 8269 */ 8270 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL); 8271 8272 switch (status) { 8273 case 0: 8274 /* 8275 * SCSI-3 reservations are supported. 8276 */ 8277 un->un_reservation_type = SD_SCSI3_RESERVATION; 8278 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8279 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8280 break; 8281 case ENOTSUP: 8282 /* 8283 * The PERSISTENT RESERVE IN command would not be recognized by 8284 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8285 */ 8286 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8287 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8288 un->un_reservation_type = SD_SCSI2_RESERVATION; 8289 8290 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8291 break; 8292 default: 8293 /* 8294 * default to SCSI-3 reservations 8295 */ 8296 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8297 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 8298 un->un_reservation_type = SD_SCSI3_RESERVATION; 8299 8300 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8301 break; 8302 } 8303 8304 /* 8305 * Set the pstat and error stat values here, so data obtained during the 8306 * previous attach-time routines is available. 8307 * 8308 * Note: This is a critical sequence that needs to be maintained: 8309 * 1) Instantiate the kstats before any routines using the iopath 8310 * (i.e. sd_send_scsi_cmd). 8311 * 2) Initialize the error stats (sd_set_errstats) and partition 8312 * stats (sd_set_pstats)here, following 8313 * cmlb_validate_geometry(), sd_register_devid(), and 8314 * sd_cache_control(). 8315 */ 8316 8317 if (un->un_f_pkstats_enabled && geom_label_valid) { 8318 sd_set_pstats(un); 8319 SD_TRACE(SD_LOG_IO_PARTITION, un, 8320 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8321 } 8322 8323 sd_set_errstats(un); 8324 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8325 "sd_unit_attach: un:0x%p errstats set\n", un); 8326 8327 8328 /* 8329 * After successfully attaching an instance, we record the information 8330 * of how many luns have been attached on the relative target and 8331 * controller for parallel SCSI. This information is used when sd tries 8332 * to set the tagged queuing capability in HBA. 8333 */ 8334 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8335 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 8336 } 8337 8338 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8339 "sd_unit_attach: un:0x%p exit success\n", un); 8340 8341 /* Uninitialize sd_ssc_t pointer */ 8342 sd_ssc_fini(ssc); 8343 8344 return (DDI_SUCCESS); 8345 8346 /* 8347 * An error occurred during the attach; clean up & return failure. 8348 */ 8349 wm_cache_failed: 8350 devid_failed: 8351 8352 setup_pm_failed: 8353 ddi_remove_minor_node(devi, NULL); 8354 8355 cmlb_attach_failed: 8356 /* 8357 * Cleanup from the scsi_ifsetcap() calls (437868) 8358 */ 8359 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8360 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8361 8362 /* 8363 * Refer to the comments of setting tagged-qing in the beginning of 8364 * sd_unit_attach. We can only disable tagged queuing when there is 8365 * no lun attached on the target. 8366 */ 8367 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8368 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8369 } 8370 8371 if (un->un_f_is_fibre == FALSE) { 8372 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8373 } 8374 8375 spinup_failed: 8376 8377 /* Uninitialize sd_ssc_t pointer */ 8378 sd_ssc_fini(ssc); 8379 8380 mutex_enter(SD_MUTEX(un)); 8381 8382 /* Deallocate SCSI FMA memory spaces */ 8383 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8384 8385 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 8386 if (un->un_direct_priority_timeid != NULL) { 8387 timeout_id_t temp_id = un->un_direct_priority_timeid; 8388 un->un_direct_priority_timeid = NULL; 8389 mutex_exit(SD_MUTEX(un)); 8390 (void) untimeout(temp_id); 8391 mutex_enter(SD_MUTEX(un)); 8392 } 8393 8394 /* Cancel any pending start/stop timeouts */ 8395 if (un->un_startstop_timeid != NULL) { 8396 timeout_id_t temp_id = un->un_startstop_timeid; 8397 un->un_startstop_timeid = NULL; 8398 mutex_exit(SD_MUTEX(un)); 8399 (void) untimeout(temp_id); 8400 mutex_enter(SD_MUTEX(un)); 8401 } 8402 8403 /* Cancel any pending reset-throttle timeouts */ 8404 if (un->un_reset_throttle_timeid != NULL) { 8405 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8406 un->un_reset_throttle_timeid = NULL; 8407 mutex_exit(SD_MUTEX(un)); 8408 (void) untimeout(temp_id); 8409 mutex_enter(SD_MUTEX(un)); 8410 } 8411 8412 /* Cancel rmw warning message timeouts */ 8413 if (un->un_rmw_msg_timeid != NULL) { 8414 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8415 un->un_rmw_msg_timeid = NULL; 8416 mutex_exit(SD_MUTEX(un)); 8417 (void) untimeout(temp_id); 8418 mutex_enter(SD_MUTEX(un)); 8419 } 8420 8421 /* Cancel any pending retry timeouts */ 8422 if (un->un_retry_timeid != NULL) { 8423 timeout_id_t temp_id = un->un_retry_timeid; 8424 un->un_retry_timeid = NULL; 8425 mutex_exit(SD_MUTEX(un)); 8426 (void) untimeout(temp_id); 8427 mutex_enter(SD_MUTEX(un)); 8428 } 8429 8430 /* Cancel any pending delayed cv broadcast timeouts */ 8431 if (un->un_dcvb_timeid != NULL) { 8432 timeout_id_t temp_id = un->un_dcvb_timeid; 8433 un->un_dcvb_timeid = NULL; 8434 mutex_exit(SD_MUTEX(un)); 8435 (void) untimeout(temp_id); 8436 mutex_enter(SD_MUTEX(un)); 8437 } 8438 8439 mutex_exit(SD_MUTEX(un)); 8440 8441 /* There should not be any in-progress I/O so ASSERT this check */ 8442 ASSERT(un->un_ncmds_in_transport == 0); 8443 ASSERT(un->un_ncmds_in_driver == 0); 8444 8445 /* Do not free the softstate if the callback routine is active */ 8446 sd_sync_with_callback(un); 8447 8448 /* 8449 * Partition stats apparently are not used with removables. These would 8450 * not have been created during attach, so no need to clean them up... 8451 */ 8452 if (un->un_errstats != NULL) { 8453 kstat_delete(un->un_errstats); 8454 un->un_errstats = NULL; 8455 } 8456 8457 create_errstats_failed: 8458 8459 if (un->un_stats != NULL) { 8460 kstat_delete(un->un_stats); 8461 un->un_stats = NULL; 8462 } 8463 8464 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8465 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8466 8467 ddi_prop_remove_all(devi); 8468 sema_destroy(&un->un_semoclose); 8469 cv_destroy(&un->un_state_cv); 8470 8471 getrbuf_failed: 8472 8473 sd_free_rqs(un); 8474 8475 alloc_rqs_failed: 8476 8477 devp->sd_private = NULL; 8478 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8479 8480 get_softstate_failed: 8481 /* 8482 * Note: the man pages are unclear as to whether or not doing a 8483 * ddi_soft_state_free(sd_state, instance) is the right way to 8484 * clean up after the ddi_soft_state_zalloc() if the subsequent 8485 * ddi_get_soft_state() fails. The implication seems to be 8486 * that the get_soft_state cannot fail if the zalloc succeeds. 8487 */ 8488 #ifndef XPV_HVM_DRIVER 8489 ddi_soft_state_free(sd_state, instance); 8490 #endif /* !XPV_HVM_DRIVER */ 8491 8492 probe_failed: 8493 scsi_unprobe(devp); 8494 8495 return (DDI_FAILURE); 8496 } 8497 8498 8499 /* 8500 * Function: sd_unit_detach 8501 * 8502 * Description: Performs DDI_DETACH processing for sddetach(). 8503 * 8504 * Return Code: DDI_SUCCESS 8505 * DDI_FAILURE 8506 * 8507 * Context: Kernel thread context 8508 */ 8509 8510 static int 8511 sd_unit_detach(dev_info_t *devi) 8512 { 8513 struct scsi_device *devp; 8514 struct sd_lun *un; 8515 int i; 8516 int tgt; 8517 dev_t dev; 8518 dev_info_t *pdip = ddi_get_parent(devi); 8519 #ifndef XPV_HVM_DRIVER 8520 int instance = ddi_get_instance(devi); 8521 #endif /* !XPV_HVM_DRIVER */ 8522 8523 mutex_enter(&sd_detach_mutex); 8524 8525 /* 8526 * Fail the detach for any of the following: 8527 * - Unable to get the sd_lun struct for the instance 8528 * - A layered driver has an outstanding open on the instance 8529 * - Another thread is already detaching this instance 8530 * - Another thread is currently performing an open 8531 */ 8532 devp = ddi_get_driver_private(devi); 8533 if ((devp == NULL) || 8534 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8535 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8536 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8537 mutex_exit(&sd_detach_mutex); 8538 return (DDI_FAILURE); 8539 } 8540 8541 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8542 8543 /* 8544 * Mark this instance as currently in a detach, to inhibit any 8545 * opens from a layered driver. 8546 */ 8547 un->un_detach_count++; 8548 mutex_exit(&sd_detach_mutex); 8549 8550 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8551 SCSI_ADDR_PROP_TARGET, -1); 8552 8553 dev = sd_make_device(SD_DEVINFO(un)); 8554 8555 #ifndef lint 8556 _NOTE(COMPETING_THREADS_NOW); 8557 #endif 8558 8559 mutex_enter(SD_MUTEX(un)); 8560 8561 /* 8562 * Fail the detach if there are any outstanding layered 8563 * opens on this device. 8564 */ 8565 for (i = 0; i < NDKMAP; i++) { 8566 if (un->un_ocmap.lyropen[i] != 0) { 8567 goto err_notclosed; 8568 } 8569 } 8570 8571 /* 8572 * Verify there are NO outstanding commands issued to this device. 8573 * ie, un_ncmds_in_transport == 0. 8574 * It's possible to have outstanding commands through the physio 8575 * code path, even though everything's closed. 8576 */ 8577 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8578 (un->un_direct_priority_timeid != NULL) || 8579 (un->un_state == SD_STATE_RWAIT)) { 8580 mutex_exit(SD_MUTEX(un)); 8581 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8582 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8583 goto err_stillbusy; 8584 } 8585 8586 /* 8587 * If we have the device reserved, release the reservation. 8588 */ 8589 if ((un->un_resvd_status & SD_RESERVE) && 8590 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8591 mutex_exit(SD_MUTEX(un)); 8592 /* 8593 * Note: sd_reserve_release sends a command to the device 8594 * via the sd_ioctlcmd() path, and can sleep. 8595 */ 8596 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8597 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8598 "sd_dr_detach: Cannot release reservation \n"); 8599 } 8600 } else { 8601 mutex_exit(SD_MUTEX(un)); 8602 } 8603 8604 /* 8605 * Untimeout any reserve recover, throttle reset, restart unit 8606 * and delayed broadcast timeout threads. Protect the timeout pointer 8607 * from getting nulled by their callback functions. 8608 */ 8609 mutex_enter(SD_MUTEX(un)); 8610 if (un->un_resvd_timeid != NULL) { 8611 timeout_id_t temp_id = un->un_resvd_timeid; 8612 un->un_resvd_timeid = NULL; 8613 mutex_exit(SD_MUTEX(un)); 8614 (void) untimeout(temp_id); 8615 mutex_enter(SD_MUTEX(un)); 8616 } 8617 8618 if (un->un_reset_throttle_timeid != NULL) { 8619 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8620 un->un_reset_throttle_timeid = NULL; 8621 mutex_exit(SD_MUTEX(un)); 8622 (void) untimeout(temp_id); 8623 mutex_enter(SD_MUTEX(un)); 8624 } 8625 8626 if (un->un_startstop_timeid != NULL) { 8627 timeout_id_t temp_id = un->un_startstop_timeid; 8628 un->un_startstop_timeid = NULL; 8629 mutex_exit(SD_MUTEX(un)); 8630 (void) untimeout(temp_id); 8631 mutex_enter(SD_MUTEX(un)); 8632 } 8633 8634 if (un->un_rmw_msg_timeid != NULL) { 8635 timeout_id_t temp_id = un->un_rmw_msg_timeid; 8636 un->un_rmw_msg_timeid = NULL; 8637 mutex_exit(SD_MUTEX(un)); 8638 (void) untimeout(temp_id); 8639 mutex_enter(SD_MUTEX(un)); 8640 } 8641 8642 if (un->un_dcvb_timeid != NULL) { 8643 timeout_id_t temp_id = un->un_dcvb_timeid; 8644 un->un_dcvb_timeid = NULL; 8645 mutex_exit(SD_MUTEX(un)); 8646 (void) untimeout(temp_id); 8647 } else { 8648 mutex_exit(SD_MUTEX(un)); 8649 } 8650 8651 /* Remove any pending reservation reclaim requests for this device */ 8652 sd_rmv_resv_reclaim_req(dev); 8653 8654 mutex_enter(SD_MUTEX(un)); 8655 8656 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8657 if (un->un_direct_priority_timeid != NULL) { 8658 timeout_id_t temp_id = un->un_direct_priority_timeid; 8659 un->un_direct_priority_timeid = NULL; 8660 mutex_exit(SD_MUTEX(un)); 8661 (void) untimeout(temp_id); 8662 mutex_enter(SD_MUTEX(un)); 8663 } 8664 8665 /* Cancel any active multi-host disk watch thread requests */ 8666 if (un->un_mhd_token != NULL) { 8667 mutex_exit(SD_MUTEX(un)); 8668 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8669 if (scsi_watch_request_terminate(un->un_mhd_token, 8670 SCSI_WATCH_TERMINATE_NOWAIT)) { 8671 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8672 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8673 /* 8674 * Note: We are returning here after having removed 8675 * some driver timeouts above. This is consistent with 8676 * the legacy implementation but perhaps the watch 8677 * terminate call should be made with the wait flag set. 8678 */ 8679 goto err_stillbusy; 8680 } 8681 mutex_enter(SD_MUTEX(un)); 8682 un->un_mhd_token = NULL; 8683 } 8684 8685 if (un->un_swr_token != NULL) { 8686 mutex_exit(SD_MUTEX(un)); 8687 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8688 if (scsi_watch_request_terminate(un->un_swr_token, 8689 SCSI_WATCH_TERMINATE_NOWAIT)) { 8690 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8691 "sd_dr_detach: Cannot cancel swr watch request\n"); 8692 /* 8693 * Note: We are returning here after having removed 8694 * some driver timeouts above. This is consistent with 8695 * the legacy implementation but perhaps the watch 8696 * terminate call should be made with the wait flag set. 8697 */ 8698 goto err_stillbusy; 8699 } 8700 mutex_enter(SD_MUTEX(un)); 8701 un->un_swr_token = NULL; 8702 } 8703 8704 mutex_exit(SD_MUTEX(un)); 8705 8706 /* 8707 * Clear any scsi_reset_notifies. We clear the reset notifies 8708 * if we have not registered one. 8709 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8710 */ 8711 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8712 sd_mhd_reset_notify_cb, (caddr_t)un); 8713 8714 /* 8715 * protect the timeout pointers from getting nulled by 8716 * their callback functions during the cancellation process. 8717 * In such a scenario untimeout can be invoked with a null value. 8718 */ 8719 _NOTE(NO_COMPETING_THREADS_NOW); 8720 8721 mutex_enter(&un->un_pm_mutex); 8722 if (un->un_pm_idle_timeid != NULL) { 8723 timeout_id_t temp_id = un->un_pm_idle_timeid; 8724 un->un_pm_idle_timeid = NULL; 8725 mutex_exit(&un->un_pm_mutex); 8726 8727 /* 8728 * Timeout is active; cancel it. 8729 * Note that it'll never be active on a device 8730 * that does not support PM therefore we don't 8731 * have to check before calling pm_idle_component. 8732 */ 8733 (void) untimeout(temp_id); 8734 (void) pm_idle_component(SD_DEVINFO(un), 0); 8735 mutex_enter(&un->un_pm_mutex); 8736 } 8737 8738 /* 8739 * Check whether there is already a timeout scheduled for power 8740 * management. If yes then don't lower the power here, that's. 8741 * the timeout handler's job. 8742 */ 8743 if (un->un_pm_timeid != NULL) { 8744 timeout_id_t temp_id = un->un_pm_timeid; 8745 un->un_pm_timeid = NULL; 8746 mutex_exit(&un->un_pm_mutex); 8747 /* 8748 * Timeout is active; cancel it. 8749 * Note that it'll never be active on a device 8750 * that does not support PM therefore we don't 8751 * have to check before calling pm_idle_component. 8752 */ 8753 (void) untimeout(temp_id); 8754 (void) pm_idle_component(SD_DEVINFO(un), 0); 8755 8756 } else { 8757 mutex_exit(&un->un_pm_mutex); 8758 if ((un->un_f_pm_is_enabled == TRUE) && 8759 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un)) 8760 != DDI_SUCCESS)) { 8761 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8762 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8763 /* 8764 * Fix for bug: 4297749, item # 13 8765 * The above test now includes a check to see if PM is 8766 * supported by this device before call 8767 * pm_lower_power(). 8768 * Note, the following is not dead code. The call to 8769 * pm_lower_power above will generate a call back into 8770 * our sdpower routine which might result in a timeout 8771 * handler getting activated. Therefore the following 8772 * code is valid and necessary. 8773 */ 8774 mutex_enter(&un->un_pm_mutex); 8775 if (un->un_pm_timeid != NULL) { 8776 timeout_id_t temp_id = un->un_pm_timeid; 8777 un->un_pm_timeid = NULL; 8778 mutex_exit(&un->un_pm_mutex); 8779 (void) untimeout(temp_id); 8780 (void) pm_idle_component(SD_DEVINFO(un), 0); 8781 } else { 8782 mutex_exit(&un->un_pm_mutex); 8783 } 8784 } 8785 } 8786 8787 /* 8788 * Cleanup from the scsi_ifsetcap() calls (437868) 8789 * Relocated here from above to be after the call to 8790 * pm_lower_power, which was getting errors. 8791 */ 8792 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8793 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8794 8795 /* 8796 * Currently, tagged queuing is supported per target based by HBA. 8797 * Setting this per lun instance actually sets the capability of this 8798 * target in HBA, which affects those luns already attached on the 8799 * same target. So during detach, we can only disable this capability 8800 * only when this is the only lun left on this target. By doing 8801 * this, we assume a target has the same tagged queuing capability 8802 * for every lun. The condition can be removed when HBA is changed to 8803 * support per lun based tagged queuing capability. 8804 */ 8805 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 8806 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8807 } 8808 8809 if (un->un_f_is_fibre == FALSE) { 8810 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8811 } 8812 8813 /* 8814 * Remove any event callbacks, fibre only 8815 */ 8816 if (un->un_f_is_fibre == TRUE) { 8817 if ((un->un_insert_event != NULL) && 8818 (ddi_remove_event_handler(un->un_insert_cb_id) != 8819 DDI_SUCCESS)) { 8820 /* 8821 * Note: We are returning here after having done 8822 * substantial cleanup above. This is consistent 8823 * with the legacy implementation but this may not 8824 * be the right thing to do. 8825 */ 8826 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8827 "sd_dr_detach: Cannot cancel insert event\n"); 8828 goto err_remove_event; 8829 } 8830 un->un_insert_event = NULL; 8831 8832 if ((un->un_remove_event != NULL) && 8833 (ddi_remove_event_handler(un->un_remove_cb_id) != 8834 DDI_SUCCESS)) { 8835 /* 8836 * Note: We are returning here after having done 8837 * substantial cleanup above. This is consistent 8838 * with the legacy implementation but this may not 8839 * be the right thing to do. 8840 */ 8841 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8842 "sd_dr_detach: Cannot cancel remove event\n"); 8843 goto err_remove_event; 8844 } 8845 un->un_remove_event = NULL; 8846 } 8847 8848 /* Do not free the softstate if the callback routine is active */ 8849 sd_sync_with_callback(un); 8850 8851 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 8852 cmlb_free_handle(&un->un_cmlbhandle); 8853 8854 /* 8855 * Hold the detach mutex here, to make sure that no other threads ever 8856 * can access a (partially) freed soft state structure. 8857 */ 8858 mutex_enter(&sd_detach_mutex); 8859 8860 /* 8861 * Clean up the soft state struct. 8862 * Cleanup is done in reverse order of allocs/inits. 8863 * At this point there should be no competing threads anymore. 8864 */ 8865 8866 scsi_fm_fini(devp); 8867 8868 /* 8869 * Deallocate memory for SCSI FMA. 8870 */ 8871 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8872 8873 /* 8874 * Unregister and free device id if it was not registered 8875 * by the transport. 8876 */ 8877 if (un->un_f_devid_transport_defined == FALSE) 8878 ddi_devid_unregister(devi); 8879 8880 /* 8881 * free the devid structure if allocated before (by ddi_devid_init() 8882 * or ddi_devid_get()). 8883 */ 8884 if (un->un_devid) { 8885 ddi_devid_free(un->un_devid); 8886 un->un_devid = NULL; 8887 } 8888 8889 /* 8890 * Destroy wmap cache if it exists. 8891 */ 8892 if (un->un_wm_cache != NULL) { 8893 kmem_cache_destroy(un->un_wm_cache); 8894 un->un_wm_cache = NULL; 8895 } 8896 8897 /* 8898 * kstat cleanup is done in detach for all device types (4363169). 8899 * We do not want to fail detach if the device kstats are not deleted 8900 * since there is a confusion about the devo_refcnt for the device. 8901 * We just delete the kstats and let detach complete successfully. 8902 */ 8903 if (un->un_stats != NULL) { 8904 kstat_delete(un->un_stats); 8905 un->un_stats = NULL; 8906 } 8907 if (un->un_errstats != NULL) { 8908 kstat_delete(un->un_errstats); 8909 un->un_errstats = NULL; 8910 } 8911 8912 /* Remove partition stats */ 8913 if (un->un_f_pkstats_enabled) { 8914 for (i = 0; i < NSDMAP; i++) { 8915 if (un->un_pstats[i] != NULL) { 8916 kstat_delete(un->un_pstats[i]); 8917 un->un_pstats[i] = NULL; 8918 } 8919 } 8920 } 8921 8922 /* Remove xbuf registration */ 8923 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8924 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8925 8926 /* Remove driver properties */ 8927 ddi_prop_remove_all(devi); 8928 8929 mutex_destroy(&un->un_pm_mutex); 8930 cv_destroy(&un->un_pm_busy_cv); 8931 8932 cv_destroy(&un->un_wcc_cv); 8933 8934 /* Open/close semaphore */ 8935 sema_destroy(&un->un_semoclose); 8936 8937 /* Removable media condvar. */ 8938 cv_destroy(&un->un_state_cv); 8939 8940 /* Suspend/resume condvar. */ 8941 cv_destroy(&un->un_suspend_cv); 8942 cv_destroy(&un->un_disk_busy_cv); 8943 8944 sd_free_rqs(un); 8945 8946 /* Free up soft state */ 8947 devp->sd_private = NULL; 8948 8949 bzero(un, sizeof (struct sd_lun)); 8950 #ifndef XPV_HVM_DRIVER 8951 ddi_soft_state_free(sd_state, instance); 8952 #endif /* !XPV_HVM_DRIVER */ 8953 8954 mutex_exit(&sd_detach_mutex); 8955 8956 /* This frees up the INQUIRY data associated with the device. */ 8957 scsi_unprobe(devp); 8958 8959 /* 8960 * After successfully detaching an instance, we update the information 8961 * of how many luns have been attached in the relative target and 8962 * controller for parallel SCSI. This information is used when sd tries 8963 * to set the tagged queuing capability in HBA. 8964 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 8965 * check if the device is parallel SCSI. However, we don't need to 8966 * check here because we've already checked during attach. No device 8967 * that is not parallel SCSI is in the chain. 8968 */ 8969 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8970 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 8971 } 8972 8973 return (DDI_SUCCESS); 8974 8975 err_notclosed: 8976 mutex_exit(SD_MUTEX(un)); 8977 8978 err_stillbusy: 8979 _NOTE(NO_COMPETING_THREADS_NOW); 8980 8981 err_remove_event: 8982 mutex_enter(&sd_detach_mutex); 8983 un->un_detach_count--; 8984 mutex_exit(&sd_detach_mutex); 8985 8986 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 8987 return (DDI_FAILURE); 8988 } 8989 8990 8991 /* 8992 * Function: sd_create_errstats 8993 * 8994 * Description: This routine instantiates the device error stats. 8995 * 8996 * Note: During attach the stats are instantiated first so they are 8997 * available for attach-time routines that utilize the driver 8998 * iopath to send commands to the device. The stats are initialized 8999 * separately so data obtained during some attach-time routines is 9000 * available. (4362483) 9001 * 9002 * Arguments: un - driver soft state (unit) structure 9003 * instance - driver instance 9004 * 9005 * Context: Kernel thread context 9006 */ 9007 9008 static void 9009 sd_create_errstats(struct sd_lun *un, int instance) 9010 { 9011 struct sd_errstats *stp; 9012 char kstatmodule_err[KSTAT_STRLEN]; 9013 char kstatname[KSTAT_STRLEN]; 9014 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9015 9016 ASSERT(un != NULL); 9017 9018 if (un->un_errstats != NULL) { 9019 return; 9020 } 9021 9022 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9023 "%serr", sd_label); 9024 (void) snprintf(kstatname, sizeof (kstatname), 9025 "%s%d,err", sd_label, instance); 9026 9027 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9028 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9029 9030 if (un->un_errstats == NULL) { 9031 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9032 "sd_create_errstats: Failed kstat_create\n"); 9033 return; 9034 } 9035 9036 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9037 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9038 KSTAT_DATA_UINT32); 9039 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9040 KSTAT_DATA_UINT32); 9041 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9042 KSTAT_DATA_UINT32); 9043 kstat_named_init(&stp->sd_vid, "Vendor", 9044 KSTAT_DATA_CHAR); 9045 kstat_named_init(&stp->sd_pid, "Product", 9046 KSTAT_DATA_CHAR); 9047 kstat_named_init(&stp->sd_revision, "Revision", 9048 KSTAT_DATA_CHAR); 9049 kstat_named_init(&stp->sd_serial, "Serial No", 9050 KSTAT_DATA_CHAR); 9051 kstat_named_init(&stp->sd_capacity, "Size", 9052 KSTAT_DATA_ULONGLONG); 9053 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9054 KSTAT_DATA_UINT32); 9055 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9056 KSTAT_DATA_UINT32); 9057 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9058 KSTAT_DATA_UINT32); 9059 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9060 KSTAT_DATA_UINT32); 9061 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9062 KSTAT_DATA_UINT32); 9063 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9064 KSTAT_DATA_UINT32); 9065 9066 un->un_errstats->ks_private = un; 9067 un->un_errstats->ks_update = nulldev; 9068 9069 kstat_install(un->un_errstats); 9070 } 9071 9072 9073 /* 9074 * Function: sd_set_errstats 9075 * 9076 * Description: This routine sets the value of the vendor id, product id, 9077 * revision, serial number, and capacity device error stats. 9078 * 9079 * Note: During attach the stats are instantiated first so they are 9080 * available for attach-time routines that utilize the driver 9081 * iopath to send commands to the device. The stats are initialized 9082 * separately so data obtained during some attach-time routines is 9083 * available. (4362483) 9084 * 9085 * Arguments: un - driver soft state (unit) structure 9086 * 9087 * Context: Kernel thread context 9088 */ 9089 9090 static void 9091 sd_set_errstats(struct sd_lun *un) 9092 { 9093 struct sd_errstats *stp; 9094 struct scsi_inquiry *sd_inq; 9095 char *sn; 9096 9097 ASSERT(un != NULL); 9098 ASSERT(un->un_errstats != NULL); 9099 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9100 ASSERT(stp != NULL); 9101 sd_inq = SD_INQUIRY(un); 9102 9103 (void) strncpy(stp->sd_vid.value.c, sd_inq->inq_vid, 8); 9104 (void) strncpy(stp->sd_pid.value.c, sd_inq->inq_pid, 16); 9105 (void) strncpy(stp->sd_revision.value.c, sd_inq->inq_revision, 4); 9106 9107 /* 9108 * All the errstats are persistent across detach/attach, 9109 * so reset all the errstats here in case of the hot 9110 * replacement of disk drives, except for not changed 9111 * Sun qualified drives. 9112 */ 9113 if ((bcmp(&sd_inq->inq_pid[9], "SUN", 3) != 0) || 9114 (bcmp(&sd_inq->inq_serial, stp->sd_serial.value.c, 9115 sizeof (sd_inq->inq_serial)) != 0)) { 9116 stp->sd_softerrs.value.ui32 = 0; 9117 stp->sd_harderrs.value.ui32 = 0; 9118 stp->sd_transerrs.value.ui32 = 0; 9119 stp->sd_rq_media_err.value.ui32 = 0; 9120 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9121 stp->sd_rq_nodev_err.value.ui32 = 0; 9122 stp->sd_rq_recov_err.value.ui32 = 0; 9123 stp->sd_rq_illrq_err.value.ui32 = 0; 9124 stp->sd_rq_pfa_err.value.ui32 = 0; 9125 } 9126 9127 /* 9128 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9129 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9130 * (4376302)) 9131 */ 9132 if (bcmp(&sd_inq->inq_pid[9], "SUN", 3) == 0) { 9133 bcopy(&sd_inq->inq_serial, stp->sd_serial.value.c, 9134 sizeof (sd_inq->inq_serial)); 9135 } else { 9136 /* 9137 * Set the "Serial No" kstat for non-Sun qualified drives 9138 */ 9139 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un), 9140 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 9141 INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) { 9142 (void) strlcpy(stp->sd_serial.value.c, sn, 9143 sizeof (stp->sd_serial.value.c)); 9144 ddi_prop_free(sn); 9145 } 9146 } 9147 9148 if (un->un_f_blockcount_is_valid != TRUE) { 9149 /* 9150 * Set capacity error stat to 0 for no media. This ensures 9151 * a valid capacity is displayed in response to 'iostat -E' 9152 * when no media is present in the device. 9153 */ 9154 stp->sd_capacity.value.ui64 = 0; 9155 } else { 9156 /* 9157 * Multiply un_blockcount by un->un_sys_blocksize to get 9158 * capacity. 9159 * 9160 * Note: for non-512 blocksize devices "un_blockcount" has been 9161 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9162 * (un_tgt_blocksize / un->un_sys_blocksize). 9163 */ 9164 stp->sd_capacity.value.ui64 = (uint64_t) 9165 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9166 } 9167 } 9168 9169 9170 /* 9171 * Function: sd_set_pstats 9172 * 9173 * Description: This routine instantiates and initializes the partition 9174 * stats for each partition with more than zero blocks. 9175 * (4363169) 9176 * 9177 * Arguments: un - driver soft state (unit) structure 9178 * 9179 * Context: Kernel thread context 9180 */ 9181 9182 static void 9183 sd_set_pstats(struct sd_lun *un) 9184 { 9185 char kstatname[KSTAT_STRLEN]; 9186 int instance; 9187 int i; 9188 diskaddr_t nblks = 0; 9189 char *partname = NULL; 9190 9191 ASSERT(un != NULL); 9192 9193 instance = ddi_get_instance(SD_DEVINFO(un)); 9194 9195 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9196 for (i = 0; i < NSDMAP; i++) { 9197 9198 if (cmlb_partinfo(un->un_cmlbhandle, i, 9199 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 9200 continue; 9201 mutex_enter(SD_MUTEX(un)); 9202 9203 if ((un->un_pstats[i] == NULL) && 9204 (nblks != 0)) { 9205 9206 (void) snprintf(kstatname, sizeof (kstatname), 9207 "%s%d,%s", sd_label, instance, 9208 partname); 9209 9210 un->un_pstats[i] = kstat_create(sd_label, 9211 instance, kstatname, "partition", KSTAT_TYPE_IO, 9212 1, KSTAT_FLAG_PERSISTENT); 9213 if (un->un_pstats[i] != NULL) { 9214 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9215 kstat_install(un->un_pstats[i]); 9216 } 9217 } 9218 mutex_exit(SD_MUTEX(un)); 9219 } 9220 } 9221 9222 9223 #if (defined(__fibre)) 9224 /* 9225 * Function: sd_init_event_callbacks 9226 * 9227 * Description: This routine initializes the insertion and removal event 9228 * callbacks. (fibre only) 9229 * 9230 * Arguments: un - driver soft state (unit) structure 9231 * 9232 * Context: Kernel thread context 9233 */ 9234 9235 static void 9236 sd_init_event_callbacks(struct sd_lun *un) 9237 { 9238 ASSERT(un != NULL); 9239 9240 if ((un->un_insert_event == NULL) && 9241 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 9242 &un->un_insert_event) == DDI_SUCCESS)) { 9243 /* 9244 * Add the callback for an insertion event 9245 */ 9246 (void) ddi_add_event_handler(SD_DEVINFO(un), 9247 un->un_insert_event, sd_event_callback, (void *)un, 9248 &(un->un_insert_cb_id)); 9249 } 9250 9251 if ((un->un_remove_event == NULL) && 9252 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 9253 &un->un_remove_event) == DDI_SUCCESS)) { 9254 /* 9255 * Add the callback for a removal event 9256 */ 9257 (void) ddi_add_event_handler(SD_DEVINFO(un), 9258 un->un_remove_event, sd_event_callback, (void *)un, 9259 &(un->un_remove_cb_id)); 9260 } 9261 } 9262 9263 9264 /* 9265 * Function: sd_event_callback 9266 * 9267 * Description: This routine handles insert/remove events (photon). The 9268 * state is changed to OFFLINE which can be used to supress 9269 * error msgs. (fibre only) 9270 * 9271 * Arguments: un - driver soft state (unit) structure 9272 * 9273 * Context: Callout thread context 9274 */ 9275 /* ARGSUSED */ 9276 static void 9277 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 9278 void *bus_impldata) 9279 { 9280 struct sd_lun *un = (struct sd_lun *)arg; 9281 9282 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 9283 if (event == un->un_insert_event) { 9284 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 9285 mutex_enter(SD_MUTEX(un)); 9286 if (un->un_state == SD_STATE_OFFLINE) { 9287 if (un->un_last_state != SD_STATE_SUSPENDED) { 9288 un->un_state = un->un_last_state; 9289 } else { 9290 /* 9291 * We have gone through SUSPEND/RESUME while 9292 * we were offline. Restore the last state 9293 */ 9294 un->un_state = un->un_save_state; 9295 } 9296 } 9297 mutex_exit(SD_MUTEX(un)); 9298 9299 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 9300 } else if (event == un->un_remove_event) { 9301 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 9302 mutex_enter(SD_MUTEX(un)); 9303 /* 9304 * We need to handle an event callback that occurs during 9305 * the suspend operation, since we don't prevent it. 9306 */ 9307 if (un->un_state != SD_STATE_OFFLINE) { 9308 if (un->un_state != SD_STATE_SUSPENDED) { 9309 New_state(un, SD_STATE_OFFLINE); 9310 } else { 9311 un->un_last_state = SD_STATE_OFFLINE; 9312 } 9313 } 9314 mutex_exit(SD_MUTEX(un)); 9315 } else { 9316 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 9317 "!Unknown event\n"); 9318 } 9319 9320 } 9321 #endif 9322 9323 /* 9324 * Function: sd_cache_control() 9325 * 9326 * Description: This routine is the driver entry point for setting 9327 * read and write caching by modifying the WCE (write cache 9328 * enable) and RCD (read cache disable) bits of mode 9329 * page 8 (MODEPAGE_CACHING). 9330 * 9331 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9332 * structure for this target. 9333 * rcd_flag - flag for controlling the read cache 9334 * wce_flag - flag for controlling the write cache 9335 * 9336 * Return Code: EIO 9337 * code returned by sd_send_scsi_MODE_SENSE and 9338 * sd_send_scsi_MODE_SELECT 9339 * 9340 * Context: Kernel Thread 9341 */ 9342 9343 static int 9344 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag) 9345 { 9346 struct mode_caching *mode_caching_page; 9347 uchar_t *header; 9348 size_t buflen; 9349 int hdrlen; 9350 int bd_len; 9351 int rval = 0; 9352 struct mode_header_grp2 *mhp; 9353 struct sd_lun *un; 9354 int status; 9355 9356 ASSERT(ssc != NULL); 9357 un = ssc->ssc_un; 9358 ASSERT(un != NULL); 9359 9360 /* 9361 * Do a test unit ready, otherwise a mode sense may not work if this 9362 * is the first command sent to the device after boot. 9363 */ 9364 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9365 if (status != 0) 9366 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9367 9368 if (un->un_f_cfg_is_atapi == TRUE) { 9369 hdrlen = MODE_HEADER_LENGTH_GRP2; 9370 } else { 9371 hdrlen = MODE_HEADER_LENGTH; 9372 } 9373 9374 /* 9375 * Allocate memory for the retrieved mode page and its headers. Set 9376 * a pointer to the page itself. Use mode_cache_scsi3 to insure 9377 * we get all of the mode sense data otherwise, the mode select 9378 * will fail. mode_cache_scsi3 is a superset of mode_caching. 9379 */ 9380 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 9381 sizeof (struct mode_cache_scsi3); 9382 9383 header = kmem_zalloc(buflen, KM_SLEEP); 9384 9385 /* Get the information from the device. */ 9386 if (un->un_f_cfg_is_atapi == TRUE) { 9387 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9388 MODEPAGE_CACHING, SD_PATH_DIRECT); 9389 } else { 9390 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9391 MODEPAGE_CACHING, SD_PATH_DIRECT); 9392 } 9393 9394 if (rval != 0) { 9395 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9396 "sd_cache_control: Mode Sense Failed\n"); 9397 goto mode_sense_failed; 9398 } 9399 9400 /* 9401 * Determine size of Block Descriptors in order to locate 9402 * the mode page data. ATAPI devices return 0, SCSI devices 9403 * should return MODE_BLK_DESC_LENGTH. 9404 */ 9405 if (un->un_f_cfg_is_atapi == TRUE) { 9406 mhp = (struct mode_header_grp2 *)header; 9407 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9408 } else { 9409 bd_len = ((struct mode_header *)header)->bdesc_length; 9410 } 9411 9412 if (bd_len > MODE_BLK_DESC_LENGTH) { 9413 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9414 "sd_cache_control: Mode Sense returned invalid block " 9415 "descriptor length\n"); 9416 rval = EIO; 9417 goto mode_sense_failed; 9418 } 9419 9420 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9421 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9422 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9423 "sd_cache_control: Mode Sense caching page code mismatch " 9424 "%d\n", mode_caching_page->mode_page.code); 9425 rval = EIO; 9426 goto mode_sense_failed; 9427 } 9428 9429 /* Check the relevant bits on successful mode sense. */ 9430 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 9431 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 9432 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 9433 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 9434 9435 size_t sbuflen; 9436 uchar_t save_pg; 9437 9438 /* 9439 * Construct select buffer length based on the 9440 * length of the sense data returned. 9441 */ 9442 sbuflen = hdrlen + bd_len + 9443 sizeof (struct mode_page) + 9444 (int)mode_caching_page->mode_page.length; 9445 9446 /* 9447 * Set the caching bits as requested. 9448 */ 9449 if (rcd_flag == SD_CACHE_ENABLE) 9450 mode_caching_page->rcd = 0; 9451 else if (rcd_flag == SD_CACHE_DISABLE) 9452 mode_caching_page->rcd = 1; 9453 9454 if (wce_flag == SD_CACHE_ENABLE) 9455 mode_caching_page->wce = 1; 9456 else if (wce_flag == SD_CACHE_DISABLE) 9457 mode_caching_page->wce = 0; 9458 9459 /* 9460 * Save the page if the mode sense says the 9461 * drive supports it. 9462 */ 9463 save_pg = mode_caching_page->mode_page.ps ? 9464 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9465 9466 /* Clear reserved bits before mode select. */ 9467 mode_caching_page->mode_page.ps = 0; 9468 9469 /* 9470 * Clear out mode header for mode select. 9471 * The rest of the retrieved page will be reused. 9472 */ 9473 bzero(header, hdrlen); 9474 9475 if (un->un_f_cfg_is_atapi == TRUE) { 9476 mhp = (struct mode_header_grp2 *)header; 9477 mhp->bdesc_length_hi = bd_len >> 8; 9478 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9479 } else { 9480 ((struct mode_header *)header)->bdesc_length = bd_len; 9481 } 9482 9483 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9484 9485 /* Issue mode select to change the cache settings */ 9486 if (un->un_f_cfg_is_atapi == TRUE) { 9487 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header, 9488 sbuflen, save_pg, SD_PATH_DIRECT); 9489 } else { 9490 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 9491 sbuflen, save_pg, SD_PATH_DIRECT); 9492 } 9493 9494 } 9495 9496 9497 mode_sense_failed: 9498 9499 kmem_free(header, buflen); 9500 9501 if (rval != 0) { 9502 if (rval == EIO) 9503 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9504 else 9505 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9506 } 9507 return (rval); 9508 } 9509 9510 9511 /* 9512 * Function: sd_get_write_cache_enabled() 9513 * 9514 * Description: This routine is the driver entry point for determining if 9515 * write caching is enabled. It examines the WCE (write cache 9516 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9517 * 9518 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9519 * structure for this target. 9520 * is_enabled - pointer to int where write cache enabled state 9521 * is returned (non-zero -> write cache enabled) 9522 * 9523 * 9524 * Return Code: EIO 9525 * code returned by sd_send_scsi_MODE_SENSE 9526 * 9527 * Context: Kernel Thread 9528 * 9529 * NOTE: If ioctl is added to disable write cache, this sequence should 9530 * be followed so that no locking is required for accesses to 9531 * un->un_f_write_cache_enabled: 9532 * do mode select to clear wce 9533 * do synchronize cache to flush cache 9534 * set un->un_f_write_cache_enabled = FALSE 9535 * 9536 * Conversely, an ioctl to enable the write cache should be done 9537 * in this order: 9538 * set un->un_f_write_cache_enabled = TRUE 9539 * do mode select to set wce 9540 */ 9541 9542 static int 9543 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled) 9544 { 9545 struct mode_caching *mode_caching_page; 9546 uchar_t *header; 9547 size_t buflen; 9548 int hdrlen; 9549 int bd_len; 9550 int rval = 0; 9551 struct sd_lun *un; 9552 int status; 9553 9554 ASSERT(ssc != NULL); 9555 un = ssc->ssc_un; 9556 ASSERT(un != NULL); 9557 ASSERT(is_enabled != NULL); 9558 9559 /* in case of error, flag as enabled */ 9560 *is_enabled = TRUE; 9561 9562 /* 9563 * Do a test unit ready, otherwise a mode sense may not work if this 9564 * is the first command sent to the device after boot. 9565 */ 9566 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9567 9568 if (status != 0) 9569 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9570 9571 if (un->un_f_cfg_is_atapi == TRUE) { 9572 hdrlen = MODE_HEADER_LENGTH_GRP2; 9573 } else { 9574 hdrlen = MODE_HEADER_LENGTH; 9575 } 9576 9577 /* 9578 * Allocate memory for the retrieved mode page and its headers. Set 9579 * a pointer to the page itself. 9580 */ 9581 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9582 header = kmem_zalloc(buflen, KM_SLEEP); 9583 9584 /* Get the information from the device. */ 9585 if (un->un_f_cfg_is_atapi == TRUE) { 9586 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9587 MODEPAGE_CACHING, SD_PATH_DIRECT); 9588 } else { 9589 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9590 MODEPAGE_CACHING, SD_PATH_DIRECT); 9591 } 9592 9593 if (rval != 0) { 9594 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9595 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9596 goto mode_sense_failed; 9597 } 9598 9599 /* 9600 * Determine size of Block Descriptors in order to locate 9601 * the mode page data. ATAPI devices return 0, SCSI devices 9602 * should return MODE_BLK_DESC_LENGTH. 9603 */ 9604 if (un->un_f_cfg_is_atapi == TRUE) { 9605 struct mode_header_grp2 *mhp; 9606 mhp = (struct mode_header_grp2 *)header; 9607 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9608 } else { 9609 bd_len = ((struct mode_header *)header)->bdesc_length; 9610 } 9611 9612 if (bd_len > MODE_BLK_DESC_LENGTH) { 9613 /* FMA should make upset complain here */ 9614 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9615 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9616 "block descriptor length\n"); 9617 rval = EIO; 9618 goto mode_sense_failed; 9619 } 9620 9621 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9622 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9623 /* FMA could make upset complain here */ 9624 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9625 "sd_get_write_cache_enabled: Mode Sense caching page " 9626 "code mismatch %d\n", mode_caching_page->mode_page.code); 9627 rval = EIO; 9628 goto mode_sense_failed; 9629 } 9630 *is_enabled = mode_caching_page->wce; 9631 9632 mode_sense_failed: 9633 if (rval == 0) { 9634 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 9635 } else if (rval == EIO) { 9636 /* 9637 * Some disks do not support mode sense(6), we 9638 * should ignore this kind of error(sense key is 9639 * 0x5 - illegal request). 9640 */ 9641 uint8_t *sensep; 9642 int senlen; 9643 9644 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 9645 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 9646 ssc->ssc_uscsi_cmd->uscsi_rqresid); 9647 9648 if (senlen > 0 && 9649 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 9650 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 9651 } else { 9652 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9653 } 9654 } else { 9655 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9656 } 9657 kmem_free(header, buflen); 9658 return (rval); 9659 } 9660 9661 /* 9662 * Function: sd_get_nv_sup() 9663 * 9664 * Description: This routine is the driver entry point for 9665 * determining whether non-volatile cache is supported. This 9666 * determination process works as follows: 9667 * 9668 * 1. sd first queries sd.conf on whether 9669 * suppress_cache_flush bit is set for this device. 9670 * 9671 * 2. if not there, then queries the internal disk table. 9672 * 9673 * 3. if either sd.conf or internal disk table specifies 9674 * cache flush be suppressed, we don't bother checking 9675 * NV_SUP bit. 9676 * 9677 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 9678 * the optional INQUIRY VPD page 0x86. If the device 9679 * supports VPD page 0x86, sd examines the NV_SUP 9680 * (non-volatile cache support) bit in the INQUIRY VPD page 9681 * 0x86: 9682 * o If NV_SUP bit is set, sd assumes the device has a 9683 * non-volatile cache and set the 9684 * un_f_sync_nv_supported to TRUE. 9685 * o Otherwise cache is not non-volatile, 9686 * un_f_sync_nv_supported is set to FALSE. 9687 * 9688 * Arguments: un - driver soft state (unit) structure 9689 * 9690 * Return Code: 9691 * 9692 * Context: Kernel Thread 9693 */ 9694 9695 static void 9696 sd_get_nv_sup(sd_ssc_t *ssc) 9697 { 9698 int rval = 0; 9699 uchar_t *inq86 = NULL; 9700 size_t inq86_len = MAX_INQUIRY_SIZE; 9701 size_t inq86_resid = 0; 9702 struct dk_callback *dkc; 9703 struct sd_lun *un; 9704 9705 ASSERT(ssc != NULL); 9706 un = ssc->ssc_un; 9707 ASSERT(un != NULL); 9708 9709 mutex_enter(SD_MUTEX(un)); 9710 9711 /* 9712 * Be conservative on the device's support of 9713 * SYNC_NV bit: un_f_sync_nv_supported is 9714 * initialized to be false. 9715 */ 9716 un->un_f_sync_nv_supported = FALSE; 9717 9718 /* 9719 * If either sd.conf or internal disk table 9720 * specifies cache flush be suppressed, then 9721 * we don't bother checking NV_SUP bit. 9722 */ 9723 if (un->un_f_suppress_cache_flush == TRUE) { 9724 mutex_exit(SD_MUTEX(un)); 9725 return; 9726 } 9727 9728 if (un->un_vpd_page_mask != 0 && 9729 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 9730 mutex_exit(SD_MUTEX(un)); 9731 /* collect page 86 data if available */ 9732 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 9733 9734 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len, 9735 0x01, 0x86, &inq86_resid); 9736 9737 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 9738 SD_TRACE(SD_LOG_COMMON, un, 9739 "sd_get_nv_sup: \ 9740 successfully get VPD page: %x \ 9741 PAGE LENGTH: %x BYTE 6: %x\n", 9742 inq86[1], inq86[3], inq86[6]); 9743 9744 mutex_enter(SD_MUTEX(un)); 9745 /* 9746 * check the value of NV_SUP bit: only if the device 9747 * reports NV_SUP bit to be 1, the 9748 * un_f_sync_nv_supported bit will be set to true. 9749 */ 9750 if (inq86[6] & SD_VPD_NV_SUP) { 9751 un->un_f_sync_nv_supported = TRUE; 9752 } 9753 mutex_exit(SD_MUTEX(un)); 9754 } else if (rval != 0) { 9755 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9756 } 9757 9758 kmem_free(inq86, inq86_len); 9759 } else { 9760 mutex_exit(SD_MUTEX(un)); 9761 } 9762 9763 /* 9764 * Send a SYNC CACHE command to check whether 9765 * SYNC_NV bit is supported. This command should have 9766 * un_f_sync_nv_supported set to correct value. 9767 */ 9768 mutex_enter(SD_MUTEX(un)); 9769 if (un->un_f_sync_nv_supported) { 9770 mutex_exit(SD_MUTEX(un)); 9771 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 9772 dkc->dkc_flag = FLUSH_VOLATILE; 9773 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 9774 9775 /* 9776 * Send a TEST UNIT READY command to the device. This should 9777 * clear any outstanding UNIT ATTENTION that may be present. 9778 */ 9779 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 9780 if (rval != 0) 9781 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9782 9783 kmem_free(dkc, sizeof (struct dk_callback)); 9784 } else { 9785 mutex_exit(SD_MUTEX(un)); 9786 } 9787 9788 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 9789 un_f_suppress_cache_flush is set to %d\n", 9790 un->un_f_suppress_cache_flush); 9791 } 9792 9793 /* 9794 * Function: sd_make_device 9795 * 9796 * Description: Utility routine to return the Solaris device number from 9797 * the data in the device's dev_info structure. 9798 * 9799 * Return Code: The Solaris device number 9800 * 9801 * Context: Any 9802 */ 9803 9804 static dev_t 9805 sd_make_device(dev_info_t *devi) 9806 { 9807 return (makedevice(ddi_driver_major(devi), 9808 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9809 } 9810 9811 9812 /* 9813 * Function: sd_pm_entry 9814 * 9815 * Description: Called at the start of a new command to manage power 9816 * and busy status of a device. This includes determining whether 9817 * the current power state of the device is sufficient for 9818 * performing the command or whether it must be changed. 9819 * The PM framework is notified appropriately. 9820 * Only with a return status of DDI_SUCCESS will the 9821 * component be busy to the framework. 9822 * 9823 * All callers of sd_pm_entry must check the return status 9824 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9825 * of DDI_FAILURE indicates the device failed to power up. 9826 * In this case un_pm_count has been adjusted so the result 9827 * on exit is still powered down, ie. count is less than 0. 9828 * Calling sd_pm_exit with this count value hits an ASSERT. 9829 * 9830 * Return Code: DDI_SUCCESS or DDI_FAILURE 9831 * 9832 * Context: Kernel thread context. 9833 */ 9834 9835 static int 9836 sd_pm_entry(struct sd_lun *un) 9837 { 9838 int return_status = DDI_SUCCESS; 9839 9840 ASSERT(!mutex_owned(SD_MUTEX(un))); 9841 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9842 9843 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9844 9845 if (un->un_f_pm_is_enabled == FALSE) { 9846 SD_TRACE(SD_LOG_IO_PM, un, 9847 "sd_pm_entry: exiting, PM not enabled\n"); 9848 return (return_status); 9849 } 9850 9851 /* 9852 * Just increment a counter if PM is enabled. On the transition from 9853 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9854 * the count with each IO and mark the device as idle when the count 9855 * hits 0. 9856 * 9857 * If the count is less than 0 the device is powered down. If a powered 9858 * down device is successfully powered up then the count must be 9859 * incremented to reflect the power up. Note that it'll get incremented 9860 * a second time to become busy. 9861 * 9862 * Because the following has the potential to change the device state 9863 * and must release the un_pm_mutex to do so, only one thread can be 9864 * allowed through at a time. 9865 */ 9866 9867 mutex_enter(&un->un_pm_mutex); 9868 while (un->un_pm_busy == TRUE) { 9869 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9870 } 9871 un->un_pm_busy = TRUE; 9872 9873 if (un->un_pm_count < 1) { 9874 9875 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9876 9877 /* 9878 * Indicate we are now busy so the framework won't attempt to 9879 * power down the device. This call will only fail if either 9880 * we passed a bad component number or the device has no 9881 * components. Neither of these should ever happen. 9882 */ 9883 mutex_exit(&un->un_pm_mutex); 9884 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9885 ASSERT(return_status == DDI_SUCCESS); 9886 9887 mutex_enter(&un->un_pm_mutex); 9888 9889 if (un->un_pm_count < 0) { 9890 mutex_exit(&un->un_pm_mutex); 9891 9892 SD_TRACE(SD_LOG_IO_PM, un, 9893 "sd_pm_entry: power up component\n"); 9894 9895 /* 9896 * pm_raise_power will cause sdpower to be called 9897 * which brings the device power level to the 9898 * desired state, If successful, un_pm_count and 9899 * un_power_level will be updated appropriately. 9900 */ 9901 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9902 SD_PM_STATE_ACTIVE(un)); 9903 9904 mutex_enter(&un->un_pm_mutex); 9905 9906 if (return_status != DDI_SUCCESS) { 9907 /* 9908 * Power up failed. 9909 * Idle the device and adjust the count 9910 * so the result on exit is that we're 9911 * still powered down, ie. count is less than 0. 9912 */ 9913 SD_TRACE(SD_LOG_IO_PM, un, 9914 "sd_pm_entry: power up failed," 9915 " idle the component\n"); 9916 9917 (void) pm_idle_component(SD_DEVINFO(un), 0); 9918 un->un_pm_count--; 9919 } else { 9920 /* 9921 * Device is powered up, verify the 9922 * count is non-negative. 9923 * This is debug only. 9924 */ 9925 ASSERT(un->un_pm_count == 0); 9926 } 9927 } 9928 9929 if (return_status == DDI_SUCCESS) { 9930 /* 9931 * For performance, now that the device has been tagged 9932 * as busy, and it's known to be powered up, update the 9933 * chain types to use jump tables that do not include 9934 * pm. This significantly lowers the overhead and 9935 * therefore improves performance. 9936 */ 9937 9938 mutex_exit(&un->un_pm_mutex); 9939 mutex_enter(SD_MUTEX(un)); 9940 SD_TRACE(SD_LOG_IO_PM, un, 9941 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9942 un->un_uscsi_chain_type); 9943 9944 if (un->un_f_non_devbsize_supported) { 9945 un->un_buf_chain_type = 9946 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9947 } else { 9948 un->un_buf_chain_type = 9949 SD_CHAIN_INFO_DISK_NO_PM; 9950 } 9951 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9952 9953 SD_TRACE(SD_LOG_IO_PM, un, 9954 " changed uscsi_chain_type to %d\n", 9955 un->un_uscsi_chain_type); 9956 mutex_exit(SD_MUTEX(un)); 9957 mutex_enter(&un->un_pm_mutex); 9958 9959 if (un->un_pm_idle_timeid == NULL) { 9960 /* 300 ms. */ 9961 un->un_pm_idle_timeid = 9962 timeout(sd_pm_idletimeout_handler, un, 9963 (drv_usectohz((clock_t)300000))); 9964 /* 9965 * Include an extra call to busy which keeps the 9966 * device busy with-respect-to the PM layer 9967 * until the timer fires, at which time it'll 9968 * get the extra idle call. 9969 */ 9970 (void) pm_busy_component(SD_DEVINFO(un), 0); 9971 } 9972 } 9973 } 9974 un->un_pm_busy = FALSE; 9975 /* Next... */ 9976 cv_signal(&un->un_pm_busy_cv); 9977 9978 un->un_pm_count++; 9979 9980 SD_TRACE(SD_LOG_IO_PM, un, 9981 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 9982 9983 mutex_exit(&un->un_pm_mutex); 9984 9985 return (return_status); 9986 } 9987 9988 9989 /* 9990 * Function: sd_pm_exit 9991 * 9992 * Description: Called at the completion of a command to manage busy 9993 * status for the device. If the device becomes idle the 9994 * PM framework is notified. 9995 * 9996 * Context: Kernel thread context 9997 */ 9998 9999 static void 10000 sd_pm_exit(struct sd_lun *un) 10001 { 10002 ASSERT(!mutex_owned(SD_MUTEX(un))); 10003 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10004 10005 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10006 10007 /* 10008 * After attach the following flag is only read, so don't 10009 * take the penalty of acquiring a mutex for it. 10010 */ 10011 if (un->un_f_pm_is_enabled == TRUE) { 10012 10013 mutex_enter(&un->un_pm_mutex); 10014 un->un_pm_count--; 10015 10016 SD_TRACE(SD_LOG_IO_PM, un, 10017 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10018 10019 ASSERT(un->un_pm_count >= 0); 10020 if (un->un_pm_count == 0) { 10021 mutex_exit(&un->un_pm_mutex); 10022 10023 SD_TRACE(SD_LOG_IO_PM, un, 10024 "sd_pm_exit: idle component\n"); 10025 10026 (void) pm_idle_component(SD_DEVINFO(un), 0); 10027 10028 } else { 10029 mutex_exit(&un->un_pm_mutex); 10030 } 10031 } 10032 10033 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10034 } 10035 10036 10037 /* 10038 * Function: sdopen 10039 * 10040 * Description: Driver's open(9e) entry point function. 10041 * 10042 * Arguments: dev_i - pointer to device number 10043 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10044 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10045 * cred_p - user credential pointer 10046 * 10047 * Return Code: EINVAL 10048 * ENXIO 10049 * EIO 10050 * EROFS 10051 * EBUSY 10052 * 10053 * Context: Kernel thread context 10054 */ 10055 /* ARGSUSED */ 10056 static int 10057 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10058 { 10059 struct sd_lun *un; 10060 int nodelay; 10061 int part; 10062 uint64_t partmask; 10063 int instance; 10064 dev_t dev; 10065 int rval = EIO; 10066 diskaddr_t nblks = 0; 10067 diskaddr_t label_cap; 10068 10069 /* Validate the open type */ 10070 if (otyp >= OTYPCNT) { 10071 return (EINVAL); 10072 } 10073 10074 dev = *dev_p; 10075 instance = SDUNIT(dev); 10076 mutex_enter(&sd_detach_mutex); 10077 10078 /* 10079 * Fail the open if there is no softstate for the instance, or 10080 * if another thread somewhere is trying to detach the instance. 10081 */ 10082 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10083 (un->un_detach_count != 0)) { 10084 mutex_exit(&sd_detach_mutex); 10085 /* 10086 * The probe cache only needs to be cleared when open (9e) fails 10087 * with ENXIO (4238046). 10088 */ 10089 /* 10090 * un-conditionally clearing probe cache is ok with 10091 * separate sd/ssd binaries 10092 * x86 platform can be an issue with both parallel 10093 * and fibre in 1 binary 10094 */ 10095 sd_scsi_clear_probe_cache(); 10096 return (ENXIO); 10097 } 10098 10099 /* 10100 * The un_layer_count is to prevent another thread in specfs from 10101 * trying to detach the instance, which can happen when we are 10102 * called from a higher-layer driver instead of thru specfs. 10103 * This will not be needed when DDI provides a layered driver 10104 * interface that allows specfs to know that an instance is in 10105 * use by a layered driver & should not be detached. 10106 * 10107 * Note: the semantics for layered driver opens are exactly one 10108 * close for every open. 10109 */ 10110 if (otyp == OTYP_LYR) { 10111 un->un_layer_count++; 10112 } 10113 10114 /* 10115 * Keep a count of the current # of opens in progress. This is because 10116 * some layered drivers try to call us as a regular open. This can 10117 * cause problems that we cannot prevent, however by keeping this count 10118 * we can at least keep our open and detach routines from racing against 10119 * each other under such conditions. 10120 */ 10121 un->un_opens_in_progress++; 10122 mutex_exit(&sd_detach_mutex); 10123 10124 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10125 part = SDPART(dev); 10126 partmask = 1 << part; 10127 10128 /* 10129 * We use a semaphore here in order to serialize 10130 * open and close requests on the device. 10131 */ 10132 sema_p(&un->un_semoclose); 10133 10134 mutex_enter(SD_MUTEX(un)); 10135 10136 /* 10137 * All device accesses go thru sdstrategy() where we check 10138 * on suspend status but there could be a scsi_poll command, 10139 * which bypasses sdstrategy(), so we need to check pm 10140 * status. 10141 */ 10142 10143 if (!nodelay) { 10144 while ((un->un_state == SD_STATE_SUSPENDED) || 10145 (un->un_state == SD_STATE_PM_CHANGING)) { 10146 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10147 } 10148 10149 mutex_exit(SD_MUTEX(un)); 10150 if (sd_pm_entry(un) != DDI_SUCCESS) { 10151 rval = EIO; 10152 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10153 "sdopen: sd_pm_entry failed\n"); 10154 goto open_failed_with_pm; 10155 } 10156 mutex_enter(SD_MUTEX(un)); 10157 } 10158 10159 /* check for previous exclusive open */ 10160 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10161 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10162 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10163 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10164 10165 if (un->un_exclopen & (partmask)) { 10166 goto excl_open_fail; 10167 } 10168 10169 if (flag & FEXCL) { 10170 int i; 10171 if (un->un_ocmap.lyropen[part]) { 10172 goto excl_open_fail; 10173 } 10174 for (i = 0; i < (OTYPCNT - 1); i++) { 10175 if (un->un_ocmap.regopen[i] & (partmask)) { 10176 goto excl_open_fail; 10177 } 10178 } 10179 } 10180 10181 /* 10182 * Check the write permission if this is a removable media device, 10183 * NDELAY has not been set, and writable permission is requested. 10184 * 10185 * Note: If NDELAY was set and this is write-protected media the WRITE 10186 * attempt will fail with EIO as part of the I/O processing. This is a 10187 * more permissive implementation that allows the open to succeed and 10188 * WRITE attempts to fail when appropriate. 10189 */ 10190 if (un->un_f_chk_wp_open) { 10191 if ((flag & FWRITE) && (!nodelay)) { 10192 mutex_exit(SD_MUTEX(un)); 10193 /* 10194 * Defer the check for write permission on writable 10195 * DVD drive till sdstrategy and will not fail open even 10196 * if FWRITE is set as the device can be writable 10197 * depending upon the media and the media can change 10198 * after the call to open(). 10199 */ 10200 if (un->un_f_dvdram_writable_device == FALSE) { 10201 if (ISCD(un) || sr_check_wp(dev)) { 10202 rval = EROFS; 10203 mutex_enter(SD_MUTEX(un)); 10204 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10205 "write to cd or write protected media\n"); 10206 goto open_fail; 10207 } 10208 } 10209 mutex_enter(SD_MUTEX(un)); 10210 } 10211 } 10212 10213 /* 10214 * If opening in NDELAY/NONBLOCK mode, just return. 10215 * Check if disk is ready and has a valid geometry later. 10216 */ 10217 if (!nodelay) { 10218 sd_ssc_t *ssc; 10219 10220 mutex_exit(SD_MUTEX(un)); 10221 ssc = sd_ssc_init(un); 10222 rval = sd_ready_and_valid(ssc, part); 10223 sd_ssc_fini(ssc); 10224 mutex_enter(SD_MUTEX(un)); 10225 /* 10226 * Fail if device is not ready or if the number of disk 10227 * blocks is zero or negative for non CD devices. 10228 */ 10229 10230 nblks = 0; 10231 10232 if (rval == SD_READY_VALID && (!ISCD(un))) { 10233 /* if cmlb_partinfo fails, nblks remains 0 */ 10234 mutex_exit(SD_MUTEX(un)); 10235 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 10236 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 10237 mutex_enter(SD_MUTEX(un)); 10238 } 10239 10240 if ((rval != SD_READY_VALID) || 10241 (!ISCD(un) && nblks <= 0)) { 10242 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10243 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10244 "device not ready or invalid disk block value\n"); 10245 goto open_fail; 10246 } 10247 #if defined(__i386) || defined(__amd64) 10248 } else { 10249 uchar_t *cp; 10250 /* 10251 * x86 requires special nodelay handling, so that p0 is 10252 * always defined and accessible. 10253 * Invalidate geometry only if device is not already open. 10254 */ 10255 cp = &un->un_ocmap.chkd[0]; 10256 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10257 if (*cp != (uchar_t)0) { 10258 break; 10259 } 10260 cp++; 10261 } 10262 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10263 mutex_exit(SD_MUTEX(un)); 10264 cmlb_invalidate(un->un_cmlbhandle, 10265 (void *)SD_PATH_DIRECT); 10266 mutex_enter(SD_MUTEX(un)); 10267 } 10268 10269 #endif 10270 } 10271 10272 if (otyp == OTYP_LYR) { 10273 un->un_ocmap.lyropen[part]++; 10274 } else { 10275 un->un_ocmap.regopen[otyp] |= partmask; 10276 } 10277 10278 /* Set up open and exclusive open flags */ 10279 if (flag & FEXCL) { 10280 un->un_exclopen |= (partmask); 10281 } 10282 10283 /* 10284 * If the lun is EFI labeled and lun capacity is greater than the 10285 * capacity contained in the label, log a sys-event to notify the 10286 * interested module. 10287 * To avoid an infinite loop of logging sys-event, we only log the 10288 * event when the lun is not opened in NDELAY mode. The event handler 10289 * should open the lun in NDELAY mode. 10290 */ 10291 if (!nodelay) { 10292 mutex_exit(SD_MUTEX(un)); 10293 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 10294 (void*)SD_PATH_DIRECT) == 0) { 10295 mutex_enter(SD_MUTEX(un)); 10296 if (un->un_f_blockcount_is_valid && 10297 un->un_blockcount > label_cap && 10298 un->un_f_expnevent == B_FALSE) { 10299 un->un_f_expnevent = B_TRUE; 10300 mutex_exit(SD_MUTEX(un)); 10301 sd_log_lun_expansion_event(un, 10302 (nodelay ? KM_NOSLEEP : KM_SLEEP)); 10303 mutex_enter(SD_MUTEX(un)); 10304 } 10305 } else { 10306 mutex_enter(SD_MUTEX(un)); 10307 } 10308 } 10309 10310 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10311 "open of part %d type %d\n", part, otyp); 10312 10313 mutex_exit(SD_MUTEX(un)); 10314 if (!nodelay) { 10315 sd_pm_exit(un); 10316 } 10317 10318 sema_v(&un->un_semoclose); 10319 10320 mutex_enter(&sd_detach_mutex); 10321 un->un_opens_in_progress--; 10322 mutex_exit(&sd_detach_mutex); 10323 10324 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10325 return (DDI_SUCCESS); 10326 10327 excl_open_fail: 10328 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10329 rval = EBUSY; 10330 10331 open_fail: 10332 mutex_exit(SD_MUTEX(un)); 10333 10334 /* 10335 * On a failed open we must exit the pm management. 10336 */ 10337 if (!nodelay) { 10338 sd_pm_exit(un); 10339 } 10340 open_failed_with_pm: 10341 sema_v(&un->un_semoclose); 10342 10343 mutex_enter(&sd_detach_mutex); 10344 un->un_opens_in_progress--; 10345 if (otyp == OTYP_LYR) { 10346 un->un_layer_count--; 10347 } 10348 mutex_exit(&sd_detach_mutex); 10349 10350 return (rval); 10351 } 10352 10353 10354 /* 10355 * Function: sdclose 10356 * 10357 * Description: Driver's close(9e) entry point function. 10358 * 10359 * Arguments: dev - device number 10360 * flag - file status flag, informational only 10361 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10362 * cred_p - user credential pointer 10363 * 10364 * Return Code: ENXIO 10365 * 10366 * Context: Kernel thread context 10367 */ 10368 /* ARGSUSED */ 10369 static int 10370 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10371 { 10372 struct sd_lun *un; 10373 uchar_t *cp; 10374 int part; 10375 int nodelay; 10376 int rval = 0; 10377 10378 /* Validate the open type */ 10379 if (otyp >= OTYPCNT) { 10380 return (ENXIO); 10381 } 10382 10383 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10384 return (ENXIO); 10385 } 10386 10387 part = SDPART(dev); 10388 nodelay = flag & (FNDELAY | FNONBLOCK); 10389 10390 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10391 "sdclose: close of part %d type %d\n", part, otyp); 10392 10393 /* 10394 * We use a semaphore here in order to serialize 10395 * open and close requests on the device. 10396 */ 10397 sema_p(&un->un_semoclose); 10398 10399 mutex_enter(SD_MUTEX(un)); 10400 10401 /* Don't proceed if power is being changed. */ 10402 while (un->un_state == SD_STATE_PM_CHANGING) { 10403 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10404 } 10405 10406 if (un->un_exclopen & (1 << part)) { 10407 un->un_exclopen &= ~(1 << part); 10408 } 10409 10410 /* Update the open partition map */ 10411 if (otyp == OTYP_LYR) { 10412 un->un_ocmap.lyropen[part] -= 1; 10413 } else { 10414 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10415 } 10416 10417 cp = &un->un_ocmap.chkd[0]; 10418 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10419 if (*cp != NULL) { 10420 break; 10421 } 10422 cp++; 10423 } 10424 10425 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10426 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10427 10428 /* 10429 * We avoid persistance upon the last close, and set 10430 * the throttle back to the maximum. 10431 */ 10432 un->un_throttle = un->un_saved_throttle; 10433 10434 if (un->un_state == SD_STATE_OFFLINE) { 10435 if (un->un_f_is_fibre == FALSE) { 10436 scsi_log(SD_DEVINFO(un), sd_label, 10437 CE_WARN, "offline\n"); 10438 } 10439 mutex_exit(SD_MUTEX(un)); 10440 cmlb_invalidate(un->un_cmlbhandle, 10441 (void *)SD_PATH_DIRECT); 10442 mutex_enter(SD_MUTEX(un)); 10443 10444 } else { 10445 /* 10446 * Flush any outstanding writes in NVRAM cache. 10447 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10448 * cmd, it may not work for non-Pluto devices. 10449 * SYNCHRONIZE CACHE is not required for removables, 10450 * except DVD-RAM drives. 10451 * 10452 * Also note: because SYNCHRONIZE CACHE is currently 10453 * the only command issued here that requires the 10454 * drive be powered up, only do the power up before 10455 * sending the Sync Cache command. If additional 10456 * commands are added which require a powered up 10457 * drive, the following sequence may have to change. 10458 * 10459 * And finally, note that parallel SCSI on SPARC 10460 * only issues a Sync Cache to DVD-RAM, a newly 10461 * supported device. 10462 */ 10463 #if defined(__i386) || defined(__amd64) 10464 if ((un->un_f_sync_cache_supported && 10465 un->un_f_sync_cache_required) || 10466 un->un_f_dvdram_writable_device == TRUE) { 10467 #else 10468 if (un->un_f_dvdram_writable_device == TRUE) { 10469 #endif 10470 mutex_exit(SD_MUTEX(un)); 10471 if (sd_pm_entry(un) == DDI_SUCCESS) { 10472 rval = 10473 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10474 NULL); 10475 /* ignore error if not supported */ 10476 if (rval == ENOTSUP) { 10477 rval = 0; 10478 } else if (rval != 0) { 10479 rval = EIO; 10480 } 10481 sd_pm_exit(un); 10482 } else { 10483 rval = EIO; 10484 } 10485 mutex_enter(SD_MUTEX(un)); 10486 } 10487 10488 /* 10489 * For devices which supports DOOR_LOCK, send an ALLOW 10490 * MEDIA REMOVAL command, but don't get upset if it 10491 * fails. We need to raise the power of the drive before 10492 * we can call sd_send_scsi_DOORLOCK() 10493 */ 10494 if (un->un_f_doorlock_supported) { 10495 mutex_exit(SD_MUTEX(un)); 10496 if (sd_pm_entry(un) == DDI_SUCCESS) { 10497 sd_ssc_t *ssc; 10498 10499 ssc = sd_ssc_init(un); 10500 rval = sd_send_scsi_DOORLOCK(ssc, 10501 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10502 if (rval != 0) 10503 sd_ssc_assessment(ssc, 10504 SD_FMT_IGNORE); 10505 sd_ssc_fini(ssc); 10506 10507 sd_pm_exit(un); 10508 if (ISCD(un) && (rval != 0) && 10509 (nodelay != 0)) { 10510 rval = ENXIO; 10511 } 10512 } else { 10513 rval = EIO; 10514 } 10515 mutex_enter(SD_MUTEX(un)); 10516 } 10517 10518 /* 10519 * If a device has removable media, invalidate all 10520 * parameters related to media, such as geometry, 10521 * blocksize, and blockcount. 10522 */ 10523 if (un->un_f_has_removable_media) { 10524 sr_ejected(un); 10525 } 10526 10527 /* 10528 * Destroy the cache (if it exists) which was 10529 * allocated for the write maps since this is 10530 * the last close for this media. 10531 */ 10532 if (un->un_wm_cache) { 10533 /* 10534 * Check if there are pending commands. 10535 * and if there are give a warning and 10536 * do not destroy the cache. 10537 */ 10538 if (un->un_ncmds_in_driver > 0) { 10539 scsi_log(SD_DEVINFO(un), 10540 sd_label, CE_WARN, 10541 "Unable to clean up memory " 10542 "because of pending I/O\n"); 10543 } else { 10544 kmem_cache_destroy( 10545 un->un_wm_cache); 10546 un->un_wm_cache = NULL; 10547 } 10548 } 10549 } 10550 } 10551 10552 mutex_exit(SD_MUTEX(un)); 10553 sema_v(&un->un_semoclose); 10554 10555 if (otyp == OTYP_LYR) { 10556 mutex_enter(&sd_detach_mutex); 10557 /* 10558 * The detach routine may run when the layer count 10559 * drops to zero. 10560 */ 10561 un->un_layer_count--; 10562 mutex_exit(&sd_detach_mutex); 10563 } 10564 10565 return (rval); 10566 } 10567 10568 10569 /* 10570 * Function: sd_ready_and_valid 10571 * 10572 * Description: Test if device is ready and has a valid geometry. 10573 * 10574 * Arguments: ssc - sd_ssc_t will contain un 10575 * un - driver soft state (unit) structure 10576 * 10577 * Return Code: SD_READY_VALID ready and valid label 10578 * SD_NOT_READY_VALID not ready, no label 10579 * SD_RESERVED_BY_OTHERS reservation conflict 10580 * 10581 * Context: Never called at interrupt context. 10582 */ 10583 10584 static int 10585 sd_ready_and_valid(sd_ssc_t *ssc, int part) 10586 { 10587 struct sd_errstats *stp; 10588 int rval = SD_READY_VALID; 10589 char name_str[48]; 10590 boolean_t is_valid; 10591 struct sd_lun *un; 10592 int status; 10593 10594 ASSERT(ssc != NULL); 10595 un = ssc->ssc_un; 10596 ASSERT(un != NULL); 10597 ASSERT(!mutex_owned(SD_MUTEX(un))); 10598 10599 mutex_enter(SD_MUTEX(un)); 10600 /* 10601 * If a device has removable media, we must check if media is 10602 * ready when checking if this device is ready and valid. 10603 */ 10604 if (un->un_f_has_removable_media) { 10605 mutex_exit(SD_MUTEX(un)); 10606 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10607 10608 if (status != 0) { 10609 rval = SD_NOT_READY_VALID; 10610 mutex_enter(SD_MUTEX(un)); 10611 10612 /* Ignore all failed status for removalbe media */ 10613 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10614 10615 goto done; 10616 } 10617 10618 is_valid = SD_IS_VALID_LABEL(un); 10619 mutex_enter(SD_MUTEX(un)); 10620 if (!is_valid || 10621 (un->un_f_blockcount_is_valid == FALSE) || 10622 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10623 10624 /* capacity has to be read every open. */ 10625 mutex_exit(SD_MUTEX(un)); 10626 status = sd_read_capacity(ssc, 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 } 10638 } 10639 10640 /* 10641 * Check if the media in the device is writable or not. 10642 */ 10643 if (!is_valid && ISCD(un)) { 10644 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 10645 } 10646 10647 } else { 10648 /* 10649 * Do a test unit ready to clear any unit attention from non-cd 10650 * devices. 10651 */ 10652 mutex_exit(SD_MUTEX(un)); 10653 10654 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10655 if (status != 0) { 10656 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10657 } 10658 10659 mutex_enter(SD_MUTEX(un)); 10660 } 10661 10662 10663 /* 10664 * If this is a non 512 block device, allocate space for 10665 * the wmap cache. This is being done here since every time 10666 * a media is changed this routine will be called and the 10667 * block size is a function of media rather than device. 10668 */ 10669 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR || 10670 un->un_f_non_devbsize_supported) && 10671 un->un_tgt_blocksize != DEV_BSIZE) || 10672 un->un_f_enable_rmw) { 10673 if (!(un->un_wm_cache)) { 10674 (void) snprintf(name_str, sizeof (name_str), 10675 "%s%d_cache", 10676 ddi_driver_name(SD_DEVINFO(un)), 10677 ddi_get_instance(SD_DEVINFO(un))); 10678 un->un_wm_cache = kmem_cache_create( 10679 name_str, sizeof (struct sd_w_map), 10680 8, sd_wm_cache_constructor, 10681 sd_wm_cache_destructor, NULL, 10682 (void *)un, NULL, 0); 10683 if (!(un->un_wm_cache)) { 10684 rval = ENOMEM; 10685 goto done; 10686 } 10687 } 10688 } 10689 10690 if (un->un_state == SD_STATE_NORMAL) { 10691 /* 10692 * If the target is not yet ready here (defined by a TUR 10693 * failure), invalidate the geometry and print an 'offline' 10694 * message. This is a legacy message, as the state of the 10695 * target is not actually changed to SD_STATE_OFFLINE. 10696 * 10697 * If the TUR fails for EACCES (Reservation Conflict), 10698 * SD_RESERVED_BY_OTHERS will be returned to indicate 10699 * reservation conflict. If the TUR fails for other 10700 * reasons, SD_NOT_READY_VALID will be returned. 10701 */ 10702 int err; 10703 10704 mutex_exit(SD_MUTEX(un)); 10705 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10706 mutex_enter(SD_MUTEX(un)); 10707 10708 if (err != 0) { 10709 mutex_exit(SD_MUTEX(un)); 10710 cmlb_invalidate(un->un_cmlbhandle, 10711 (void *)SD_PATH_DIRECT); 10712 mutex_enter(SD_MUTEX(un)); 10713 if (err == EACCES) { 10714 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10715 "reservation conflict\n"); 10716 rval = SD_RESERVED_BY_OTHERS; 10717 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10718 } else { 10719 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10720 "drive offline\n"); 10721 rval = SD_NOT_READY_VALID; 10722 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 10723 } 10724 goto done; 10725 } 10726 } 10727 10728 if (un->un_f_format_in_progress == FALSE) { 10729 mutex_exit(SD_MUTEX(un)); 10730 10731 (void) cmlb_validate(un->un_cmlbhandle, 0, 10732 (void *)SD_PATH_DIRECT); 10733 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL, 10734 NULL, (void *) SD_PATH_DIRECT) != 0) { 10735 rval = SD_NOT_READY_VALID; 10736 mutex_enter(SD_MUTEX(un)); 10737 10738 goto done; 10739 } 10740 if (un->un_f_pkstats_enabled) { 10741 sd_set_pstats(un); 10742 SD_TRACE(SD_LOG_IO_PARTITION, un, 10743 "sd_ready_and_valid: un:0x%p pstats created and " 10744 "set\n", un); 10745 } 10746 mutex_enter(SD_MUTEX(un)); 10747 } 10748 10749 /* 10750 * If this device supports DOOR_LOCK command, try and send 10751 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10752 * if it fails. For a CD, however, it is an error 10753 */ 10754 if (un->un_f_doorlock_supported) { 10755 mutex_exit(SD_MUTEX(un)); 10756 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 10757 SD_PATH_DIRECT); 10758 10759 if ((status != 0) && ISCD(un)) { 10760 rval = SD_NOT_READY_VALID; 10761 mutex_enter(SD_MUTEX(un)); 10762 10763 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10764 10765 goto done; 10766 } else if (status != 0) 10767 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10768 mutex_enter(SD_MUTEX(un)); 10769 } 10770 10771 /* The state has changed, inform the media watch routines */ 10772 un->un_mediastate = DKIO_INSERTED; 10773 cv_broadcast(&un->un_state_cv); 10774 rval = SD_READY_VALID; 10775 10776 done: 10777 10778 /* 10779 * Initialize the capacity kstat value, if no media previously 10780 * (capacity kstat is 0) and a media has been inserted 10781 * (un_blockcount > 0). 10782 */ 10783 if (un->un_errstats != NULL) { 10784 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10785 if ((stp->sd_capacity.value.ui64 == 0) && 10786 (un->un_f_blockcount_is_valid == TRUE)) { 10787 stp->sd_capacity.value.ui64 = 10788 (uint64_t)((uint64_t)un->un_blockcount * 10789 un->un_sys_blocksize); 10790 } 10791 } 10792 10793 mutex_exit(SD_MUTEX(un)); 10794 return (rval); 10795 } 10796 10797 10798 /* 10799 * Function: sdmin 10800 * 10801 * Description: Routine to limit the size of a data transfer. Used in 10802 * conjunction with physio(9F). 10803 * 10804 * Arguments: bp - pointer to the indicated buf(9S) struct. 10805 * 10806 * Context: Kernel thread context. 10807 */ 10808 10809 static void 10810 sdmin(struct buf *bp) 10811 { 10812 struct sd_lun *un; 10813 int instance; 10814 10815 instance = SDUNIT(bp->b_edev); 10816 10817 un = ddi_get_soft_state(sd_state, instance); 10818 ASSERT(un != NULL); 10819 10820 /* 10821 * We depend on buf breakup to restrict 10822 * IO size if it is enabled. 10823 */ 10824 if (un->un_buf_breakup_supported) { 10825 return; 10826 } 10827 10828 if (bp->b_bcount > un->un_max_xfer_size) { 10829 bp->b_bcount = un->un_max_xfer_size; 10830 } 10831 } 10832 10833 10834 /* 10835 * Function: sdread 10836 * 10837 * Description: Driver's read(9e) entry point function. 10838 * 10839 * Arguments: dev - device number 10840 * uio - structure pointer describing where data is to be stored 10841 * in user's space 10842 * cred_p - user credential pointer 10843 * 10844 * Return Code: ENXIO 10845 * EIO 10846 * EINVAL 10847 * value returned by physio 10848 * 10849 * Context: Kernel thread context. 10850 */ 10851 /* ARGSUSED */ 10852 static int 10853 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10854 { 10855 struct sd_lun *un = NULL; 10856 int secmask; 10857 int err = 0; 10858 sd_ssc_t *ssc; 10859 10860 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10861 return (ENXIO); 10862 } 10863 10864 ASSERT(!mutex_owned(SD_MUTEX(un))); 10865 10866 10867 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10868 mutex_enter(SD_MUTEX(un)); 10869 /* 10870 * Because the call to sd_ready_and_valid will issue I/O we 10871 * must wait here if either the device is suspended or 10872 * if it's power level is changing. 10873 */ 10874 while ((un->un_state == SD_STATE_SUSPENDED) || 10875 (un->un_state == SD_STATE_PM_CHANGING)) { 10876 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10877 } 10878 un->un_ncmds_in_driver++; 10879 mutex_exit(SD_MUTEX(un)); 10880 10881 /* Initialize sd_ssc_t for internal uscsi commands */ 10882 ssc = sd_ssc_init(un); 10883 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10884 err = EIO; 10885 } else { 10886 err = 0; 10887 } 10888 sd_ssc_fini(ssc); 10889 10890 mutex_enter(SD_MUTEX(un)); 10891 un->un_ncmds_in_driver--; 10892 ASSERT(un->un_ncmds_in_driver >= 0); 10893 mutex_exit(SD_MUTEX(un)); 10894 if (err != 0) 10895 return (err); 10896 } 10897 10898 /* 10899 * Read requests are restricted to multiples of the system block size. 10900 */ 10901 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 10902 !un->un_f_enable_rmw) 10903 secmask = un->un_tgt_blocksize - 1; 10904 else 10905 secmask = DEV_BSIZE - 1; 10906 10907 if (uio->uio_loffset & ((offset_t)(secmask))) { 10908 SD_ERROR(SD_LOG_READ_WRITE, un, 10909 "sdread: file offset not modulo %d\n", 10910 secmask + 1); 10911 err = EINVAL; 10912 } else if (uio->uio_iov->iov_len & (secmask)) { 10913 SD_ERROR(SD_LOG_READ_WRITE, un, 10914 "sdread: transfer length not modulo %d\n", 10915 secmask + 1); 10916 err = EINVAL; 10917 } else { 10918 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10919 } 10920 10921 return (err); 10922 } 10923 10924 10925 /* 10926 * Function: sdwrite 10927 * 10928 * Description: Driver's write(9e) entry point function. 10929 * 10930 * Arguments: dev - device number 10931 * uio - structure pointer describing where data is stored in 10932 * user's space 10933 * cred_p - user credential pointer 10934 * 10935 * Return Code: ENXIO 10936 * EIO 10937 * EINVAL 10938 * value returned by physio 10939 * 10940 * Context: Kernel thread context. 10941 */ 10942 /* ARGSUSED */ 10943 static int 10944 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10945 { 10946 struct sd_lun *un = NULL; 10947 int secmask; 10948 int err = 0; 10949 sd_ssc_t *ssc; 10950 10951 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10952 return (ENXIO); 10953 } 10954 10955 ASSERT(!mutex_owned(SD_MUTEX(un))); 10956 10957 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10958 mutex_enter(SD_MUTEX(un)); 10959 /* 10960 * Because the call to sd_ready_and_valid will issue I/O we 10961 * must wait here if either the device is suspended or 10962 * if it's power level is changing. 10963 */ 10964 while ((un->un_state == SD_STATE_SUSPENDED) || 10965 (un->un_state == SD_STATE_PM_CHANGING)) { 10966 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10967 } 10968 un->un_ncmds_in_driver++; 10969 mutex_exit(SD_MUTEX(un)); 10970 10971 /* Initialize sd_ssc_t for internal uscsi commands */ 10972 ssc = sd_ssc_init(un); 10973 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10974 err = EIO; 10975 } else { 10976 err = 0; 10977 } 10978 sd_ssc_fini(ssc); 10979 10980 mutex_enter(SD_MUTEX(un)); 10981 un->un_ncmds_in_driver--; 10982 ASSERT(un->un_ncmds_in_driver >= 0); 10983 mutex_exit(SD_MUTEX(un)); 10984 if (err != 0) 10985 return (err); 10986 } 10987 10988 /* 10989 * Write requests are restricted to multiples of the system block size. 10990 */ 10991 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 10992 !un->un_f_enable_rmw) 10993 secmask = un->un_tgt_blocksize - 1; 10994 else 10995 secmask = DEV_BSIZE - 1; 10996 10997 if (uio->uio_loffset & ((offset_t)(secmask))) { 10998 SD_ERROR(SD_LOG_READ_WRITE, un, 10999 "sdwrite: file offset not modulo %d\n", 11000 secmask + 1); 11001 err = EINVAL; 11002 } else if (uio->uio_iov->iov_len & (secmask)) { 11003 SD_ERROR(SD_LOG_READ_WRITE, un, 11004 "sdwrite: transfer length not modulo %d\n", 11005 secmask + 1); 11006 err = EINVAL; 11007 } else { 11008 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11009 } 11010 11011 return (err); 11012 } 11013 11014 11015 /* 11016 * Function: sdaread 11017 * 11018 * Description: Driver's aread(9e) entry point function. 11019 * 11020 * Arguments: dev - device number 11021 * aio - structure pointer describing where data is to be stored 11022 * cred_p - user credential pointer 11023 * 11024 * Return Code: ENXIO 11025 * EIO 11026 * EINVAL 11027 * value returned by aphysio 11028 * 11029 * Context: Kernel thread context. 11030 */ 11031 /* ARGSUSED */ 11032 static int 11033 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11034 { 11035 struct sd_lun *un = NULL; 11036 struct uio *uio = aio->aio_uio; 11037 int secmask; 11038 int err = 0; 11039 sd_ssc_t *ssc; 11040 11041 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11042 return (ENXIO); 11043 } 11044 11045 ASSERT(!mutex_owned(SD_MUTEX(un))); 11046 11047 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11048 mutex_enter(SD_MUTEX(un)); 11049 /* 11050 * Because the call to sd_ready_and_valid will issue I/O we 11051 * must wait here if either the device is suspended or 11052 * if it's power level is changing. 11053 */ 11054 while ((un->un_state == SD_STATE_SUSPENDED) || 11055 (un->un_state == SD_STATE_PM_CHANGING)) { 11056 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11057 } 11058 un->un_ncmds_in_driver++; 11059 mutex_exit(SD_MUTEX(un)); 11060 11061 /* Initialize sd_ssc_t for internal uscsi commands */ 11062 ssc = sd_ssc_init(un); 11063 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11064 err = EIO; 11065 } else { 11066 err = 0; 11067 } 11068 sd_ssc_fini(ssc); 11069 11070 mutex_enter(SD_MUTEX(un)); 11071 un->un_ncmds_in_driver--; 11072 ASSERT(un->un_ncmds_in_driver >= 0); 11073 mutex_exit(SD_MUTEX(un)); 11074 if (err != 0) 11075 return (err); 11076 } 11077 11078 /* 11079 * Read requests are restricted to multiples of the system block size. 11080 */ 11081 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11082 !un->un_f_enable_rmw) 11083 secmask = un->un_tgt_blocksize - 1; 11084 else 11085 secmask = DEV_BSIZE - 1; 11086 11087 if (uio->uio_loffset & ((offset_t)(secmask))) { 11088 SD_ERROR(SD_LOG_READ_WRITE, un, 11089 "sdaread: file offset not modulo %d\n", 11090 secmask + 1); 11091 err = EINVAL; 11092 } else if (uio->uio_iov->iov_len & (secmask)) { 11093 SD_ERROR(SD_LOG_READ_WRITE, un, 11094 "sdaread: transfer length not modulo %d\n", 11095 secmask + 1); 11096 err = EINVAL; 11097 } else { 11098 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11099 } 11100 11101 return (err); 11102 } 11103 11104 11105 /* 11106 * Function: sdawrite 11107 * 11108 * Description: Driver's awrite(9e) entry point function. 11109 * 11110 * Arguments: dev - device number 11111 * aio - structure pointer describing where data is stored 11112 * cred_p - user credential pointer 11113 * 11114 * Return Code: ENXIO 11115 * EIO 11116 * EINVAL 11117 * value returned by aphysio 11118 * 11119 * Context: Kernel thread context. 11120 */ 11121 /* ARGSUSED */ 11122 static int 11123 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11124 { 11125 struct sd_lun *un = NULL; 11126 struct uio *uio = aio->aio_uio; 11127 int secmask; 11128 int err = 0; 11129 sd_ssc_t *ssc; 11130 11131 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11132 return (ENXIO); 11133 } 11134 11135 ASSERT(!mutex_owned(SD_MUTEX(un))); 11136 11137 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 11138 mutex_enter(SD_MUTEX(un)); 11139 /* 11140 * Because the call to sd_ready_and_valid will issue I/O we 11141 * must wait here if either the device is suspended or 11142 * if it's power level is changing. 11143 */ 11144 while ((un->un_state == SD_STATE_SUSPENDED) || 11145 (un->un_state == SD_STATE_PM_CHANGING)) { 11146 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11147 } 11148 un->un_ncmds_in_driver++; 11149 mutex_exit(SD_MUTEX(un)); 11150 11151 /* Initialize sd_ssc_t for internal uscsi commands */ 11152 ssc = sd_ssc_init(un); 11153 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 11154 err = EIO; 11155 } else { 11156 err = 0; 11157 } 11158 sd_ssc_fini(ssc); 11159 11160 mutex_enter(SD_MUTEX(un)); 11161 un->un_ncmds_in_driver--; 11162 ASSERT(un->un_ncmds_in_driver >= 0); 11163 mutex_exit(SD_MUTEX(un)); 11164 if (err != 0) 11165 return (err); 11166 } 11167 11168 /* 11169 * Write requests are restricted to multiples of the system block size. 11170 */ 11171 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR && 11172 !un->un_f_enable_rmw) 11173 secmask = un->un_tgt_blocksize - 1; 11174 else 11175 secmask = DEV_BSIZE - 1; 11176 11177 if (uio->uio_loffset & ((offset_t)(secmask))) { 11178 SD_ERROR(SD_LOG_READ_WRITE, un, 11179 "sdawrite: file offset not modulo %d\n", 11180 secmask + 1); 11181 err = EINVAL; 11182 } else if (uio->uio_iov->iov_len & (secmask)) { 11183 SD_ERROR(SD_LOG_READ_WRITE, un, 11184 "sdawrite: transfer length not modulo %d\n", 11185 secmask + 1); 11186 err = EINVAL; 11187 } else { 11188 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11189 } 11190 11191 return (err); 11192 } 11193 11194 11195 11196 11197 11198 /* 11199 * Driver IO processing follows the following sequence: 11200 * 11201 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11202 * | | ^ 11203 * v v | 11204 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11205 * | | | | 11206 * v | | | 11207 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11208 * | | ^ ^ 11209 * v v | | 11210 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11211 * | | | | 11212 * +---+ | +------------+ +-------+ 11213 * | | | | 11214 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11215 * | v | | 11216 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11217 * | | ^ | 11218 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11219 * | v | | 11220 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11221 * | | ^ | 11222 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11223 * | v | | 11224 * | sd_checksum_iostart() sd_checksum_iodone() | 11225 * | | ^ | 11226 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11227 * | v | | 11228 * | sd_pm_iostart() sd_pm_iodone() | 11229 * | | ^ | 11230 * | | | | 11231 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11232 * | ^ 11233 * v | 11234 * sd_core_iostart() | 11235 * | | 11236 * | +------>(*destroypkt)() 11237 * +-> sd_start_cmds() <-+ | | 11238 * | | | v 11239 * | | | scsi_destroy_pkt(9F) 11240 * | | | 11241 * +->(*initpkt)() +- sdintr() 11242 * | | | | 11243 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11244 * | +-> scsi_setup_cdb(9F) | 11245 * | | 11246 * +--> scsi_transport(9F) | 11247 * | | 11248 * +----> SCSA ---->+ 11249 * 11250 * 11251 * This code is based upon the following presumptions: 11252 * 11253 * - iostart and iodone functions operate on buf(9S) structures. These 11254 * functions perform the necessary operations on the buf(9S) and pass 11255 * them along to the next function in the chain by using the macros 11256 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11257 * (for iodone side functions). 11258 * 11259 * - The iostart side functions may sleep. The iodone side functions 11260 * are called under interrupt context and may NOT sleep. Therefore 11261 * iodone side functions also may not call iostart side functions. 11262 * (NOTE: iostart side functions should NOT sleep for memory, as 11263 * this could result in deadlock.) 11264 * 11265 * - An iostart side function may call its corresponding iodone side 11266 * function directly (if necessary). 11267 * 11268 * - In the event of an error, an iostart side function can return a buf(9S) 11269 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11270 * b_error in the usual way of course). 11271 * 11272 * - The taskq mechanism may be used by the iodone side functions to dispatch 11273 * requests to the iostart side functions. The iostart side functions in 11274 * this case would be called under the context of a taskq thread, so it's 11275 * OK for them to block/sleep/spin in this case. 11276 * 11277 * - iostart side functions may allocate "shadow" buf(9S) structs and 11278 * pass them along to the next function in the chain. The corresponding 11279 * iodone side functions must coalesce the "shadow" bufs and return 11280 * the "original" buf to the next higher layer. 11281 * 11282 * - The b_private field of the buf(9S) struct holds a pointer to 11283 * an sd_xbuf struct, which contains information needed to 11284 * construct the scsi_pkt for the command. 11285 * 11286 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11287 * layer must acquire & release the SD_MUTEX(un) as needed. 11288 */ 11289 11290 11291 /* 11292 * Create taskq for all targets in the system. This is created at 11293 * _init(9E) and destroyed at _fini(9E). 11294 * 11295 * Note: here we set the minalloc to a reasonably high number to ensure that 11296 * we will have an adequate supply of task entries available at interrupt time. 11297 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11298 * sd_create_taskq(). Since we do not want to sleep for allocations at 11299 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11300 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11301 * requests any one instant in time. 11302 */ 11303 #define SD_TASKQ_NUMTHREADS 8 11304 #define SD_TASKQ_MINALLOC 256 11305 #define SD_TASKQ_MAXALLOC 256 11306 11307 static taskq_t *sd_tq = NULL; 11308 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11309 11310 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11311 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11312 11313 /* 11314 * The following task queue is being created for the write part of 11315 * read-modify-write of non-512 block size devices. 11316 * Limit the number of threads to 1 for now. This number has been chosen 11317 * considering the fact that it applies only to dvd ram drives/MO drives 11318 * currently. Performance for which is not main criteria at this stage. 11319 * Note: It needs to be explored if we can use a single taskq in future 11320 */ 11321 #define SD_WMR_TASKQ_NUMTHREADS 1 11322 static taskq_t *sd_wmr_tq = NULL; 11323 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11324 11325 /* 11326 * Function: sd_taskq_create 11327 * 11328 * Description: Create taskq thread(s) and preallocate task entries 11329 * 11330 * Return Code: Returns a pointer to the allocated taskq_t. 11331 * 11332 * Context: Can sleep. Requires blockable context. 11333 * 11334 * Notes: - The taskq() facility currently is NOT part of the DDI. 11335 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11336 * - taskq_create() will block for memory, also it will panic 11337 * if it cannot create the requested number of threads. 11338 * - Currently taskq_create() creates threads that cannot be 11339 * swapped. 11340 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11341 * supply of taskq entries at interrupt time (ie, so that we 11342 * do not have to sleep for memory) 11343 */ 11344 11345 static void 11346 sd_taskq_create(void) 11347 { 11348 char taskq_name[TASKQ_NAMELEN]; 11349 11350 ASSERT(sd_tq == NULL); 11351 ASSERT(sd_wmr_tq == NULL); 11352 11353 (void) snprintf(taskq_name, sizeof (taskq_name), 11354 "%s_drv_taskq", sd_label); 11355 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11356 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11357 TASKQ_PREPOPULATE)); 11358 11359 (void) snprintf(taskq_name, sizeof (taskq_name), 11360 "%s_rmw_taskq", sd_label); 11361 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11362 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11363 TASKQ_PREPOPULATE)); 11364 } 11365 11366 11367 /* 11368 * Function: sd_taskq_delete 11369 * 11370 * Description: Complementary cleanup routine for sd_taskq_create(). 11371 * 11372 * Context: Kernel thread context. 11373 */ 11374 11375 static void 11376 sd_taskq_delete(void) 11377 { 11378 ASSERT(sd_tq != NULL); 11379 ASSERT(sd_wmr_tq != NULL); 11380 taskq_destroy(sd_tq); 11381 taskq_destroy(sd_wmr_tq); 11382 sd_tq = NULL; 11383 sd_wmr_tq = NULL; 11384 } 11385 11386 11387 /* 11388 * Function: sdstrategy 11389 * 11390 * Description: Driver's strategy (9E) entry point function. 11391 * 11392 * Arguments: bp - pointer to buf(9S) 11393 * 11394 * Return Code: Always returns zero 11395 * 11396 * Context: Kernel thread context. 11397 */ 11398 11399 static int 11400 sdstrategy(struct buf *bp) 11401 { 11402 struct sd_lun *un; 11403 11404 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11405 if (un == NULL) { 11406 bioerror(bp, EIO); 11407 bp->b_resid = bp->b_bcount; 11408 biodone(bp); 11409 return (0); 11410 } 11411 11412 /* As was done in the past, fail new cmds. if state is dumping. */ 11413 if (un->un_state == SD_STATE_DUMPING) { 11414 bioerror(bp, ENXIO); 11415 bp->b_resid = bp->b_bcount; 11416 biodone(bp); 11417 return (0); 11418 } 11419 11420 ASSERT(!mutex_owned(SD_MUTEX(un))); 11421 11422 /* 11423 * Commands may sneak in while we released the mutex in 11424 * DDI_SUSPEND, we should block new commands. However, old 11425 * commands that are still in the driver at this point should 11426 * still be allowed to drain. 11427 */ 11428 mutex_enter(SD_MUTEX(un)); 11429 /* 11430 * Must wait here if either the device is suspended or 11431 * if it's power level is changing. 11432 */ 11433 while ((un->un_state == SD_STATE_SUSPENDED) || 11434 (un->un_state == SD_STATE_PM_CHANGING)) { 11435 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11436 } 11437 11438 un->un_ncmds_in_driver++; 11439 11440 /* 11441 * atapi: Since we are running the CD for now in PIO mode we need to 11442 * call bp_mapin here to avoid bp_mapin called interrupt context under 11443 * the HBA's init_pkt routine. 11444 */ 11445 if (un->un_f_cfg_is_atapi == TRUE) { 11446 mutex_exit(SD_MUTEX(un)); 11447 bp_mapin(bp); 11448 mutex_enter(SD_MUTEX(un)); 11449 } 11450 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11451 un->un_ncmds_in_driver); 11452 11453 if (bp->b_flags & B_WRITE) 11454 un->un_f_sync_cache_required = TRUE; 11455 11456 mutex_exit(SD_MUTEX(un)); 11457 11458 /* 11459 * This will (eventually) allocate the sd_xbuf area and 11460 * call sd_xbuf_strategy(). We just want to return the 11461 * result of ddi_xbuf_qstrategy so that we have an opt- 11462 * imized tail call which saves us a stack frame. 11463 */ 11464 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11465 } 11466 11467 11468 /* 11469 * Function: sd_xbuf_strategy 11470 * 11471 * Description: Function for initiating IO operations via the 11472 * ddi_xbuf_qstrategy() mechanism. 11473 * 11474 * Context: Kernel thread context. 11475 */ 11476 11477 static void 11478 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11479 { 11480 struct sd_lun *un = arg; 11481 11482 ASSERT(bp != NULL); 11483 ASSERT(xp != NULL); 11484 ASSERT(un != NULL); 11485 ASSERT(!mutex_owned(SD_MUTEX(un))); 11486 11487 /* 11488 * Initialize the fields in the xbuf and save a pointer to the 11489 * xbuf in bp->b_private. 11490 */ 11491 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11492 11493 /* Send the buf down the iostart chain */ 11494 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11495 } 11496 11497 11498 /* 11499 * Function: sd_xbuf_init 11500 * 11501 * Description: Prepare the given sd_xbuf struct for use. 11502 * 11503 * Arguments: un - ptr to softstate 11504 * bp - ptr to associated buf(9S) 11505 * xp - ptr to associated sd_xbuf 11506 * chain_type - IO chain type to use: 11507 * SD_CHAIN_NULL 11508 * SD_CHAIN_BUFIO 11509 * SD_CHAIN_USCSI 11510 * SD_CHAIN_DIRECT 11511 * SD_CHAIN_DIRECT_PRIORITY 11512 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11513 * initialization; may be NULL if none. 11514 * 11515 * Context: Kernel thread context 11516 */ 11517 11518 static void 11519 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11520 uchar_t chain_type, void *pktinfop) 11521 { 11522 int index; 11523 11524 ASSERT(un != NULL); 11525 ASSERT(bp != NULL); 11526 ASSERT(xp != NULL); 11527 11528 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11529 bp, chain_type); 11530 11531 xp->xb_un = un; 11532 xp->xb_pktp = NULL; 11533 xp->xb_pktinfo = pktinfop; 11534 xp->xb_private = bp->b_private; 11535 xp->xb_blkno = (daddr_t)bp->b_blkno; 11536 11537 /* 11538 * Set up the iostart and iodone chain indexes in the xbuf, based 11539 * upon the specified chain type to use. 11540 */ 11541 switch (chain_type) { 11542 case SD_CHAIN_NULL: 11543 /* 11544 * Fall thru to just use the values for the buf type, even 11545 * tho for the NULL chain these values will never be used. 11546 */ 11547 /* FALLTHRU */ 11548 case SD_CHAIN_BUFIO: 11549 index = un->un_buf_chain_type; 11550 if ((!un->un_f_has_removable_media) && 11551 (un->un_tgt_blocksize != 0) && 11552 (un->un_tgt_blocksize != DEV_BSIZE || 11553 un->un_f_enable_rmw)) { 11554 int secmask = 0, blknomask = 0; 11555 if (un->un_f_enable_rmw) { 11556 blknomask = 11557 (un->un_phy_blocksize / DEV_BSIZE) - 1; 11558 secmask = un->un_phy_blocksize - 1; 11559 } else { 11560 blknomask = 11561 (un->un_tgt_blocksize / DEV_BSIZE) - 1; 11562 secmask = un->un_tgt_blocksize - 1; 11563 } 11564 11565 if ((bp->b_lblkno & (blknomask)) || 11566 (bp->b_bcount & (secmask))) { 11567 if ((un->un_f_rmw_type != 11568 SD_RMW_TYPE_RETURN_ERROR) || 11569 un->un_f_enable_rmw) { 11570 if (un->un_f_pm_is_enabled == FALSE) 11571 index = 11572 SD_CHAIN_INFO_MSS_DSK_NO_PM; 11573 else 11574 index = 11575 SD_CHAIN_INFO_MSS_DISK; 11576 } 11577 } 11578 } 11579 break; 11580 case SD_CHAIN_USCSI: 11581 index = un->un_uscsi_chain_type; 11582 break; 11583 case SD_CHAIN_DIRECT: 11584 index = un->un_direct_chain_type; 11585 break; 11586 case SD_CHAIN_DIRECT_PRIORITY: 11587 index = un->un_priority_chain_type; 11588 break; 11589 default: 11590 /* We're really broken if we ever get here... */ 11591 panic("sd_xbuf_init: illegal chain type!"); 11592 /*NOTREACHED*/ 11593 } 11594 11595 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11596 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11597 11598 /* 11599 * It might be a bit easier to simply bzero the entire xbuf above, 11600 * but it turns out that since we init a fair number of members anyway, 11601 * we save a fair number cycles by doing explicit assignment of zero. 11602 */ 11603 xp->xb_pkt_flags = 0; 11604 xp->xb_dma_resid = 0; 11605 xp->xb_retry_count = 0; 11606 xp->xb_victim_retry_count = 0; 11607 xp->xb_ua_retry_count = 0; 11608 xp->xb_nr_retry_count = 0; 11609 xp->xb_sense_bp = NULL; 11610 xp->xb_sense_status = 0; 11611 xp->xb_sense_state = 0; 11612 xp->xb_sense_resid = 0; 11613 xp->xb_ena = 0; 11614 11615 bp->b_private = xp; 11616 bp->b_flags &= ~(B_DONE | B_ERROR); 11617 bp->b_resid = 0; 11618 bp->av_forw = NULL; 11619 bp->av_back = NULL; 11620 bioerror(bp, 0); 11621 11622 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11623 } 11624 11625 11626 /* 11627 * Function: sd_uscsi_strategy 11628 * 11629 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11630 * 11631 * Arguments: bp - buf struct ptr 11632 * 11633 * Return Code: Always returns 0 11634 * 11635 * Context: Kernel thread context 11636 */ 11637 11638 static int 11639 sd_uscsi_strategy(struct buf *bp) 11640 { 11641 struct sd_lun *un; 11642 struct sd_uscsi_info *uip; 11643 struct sd_xbuf *xp; 11644 uchar_t chain_type; 11645 uchar_t cmd; 11646 11647 ASSERT(bp != NULL); 11648 11649 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11650 if (un == NULL) { 11651 bioerror(bp, EIO); 11652 bp->b_resid = bp->b_bcount; 11653 biodone(bp); 11654 return (0); 11655 } 11656 11657 ASSERT(!mutex_owned(SD_MUTEX(un))); 11658 11659 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11660 11661 /* 11662 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11663 */ 11664 ASSERT(bp->b_private != NULL); 11665 uip = (struct sd_uscsi_info *)bp->b_private; 11666 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0]; 11667 11668 mutex_enter(SD_MUTEX(un)); 11669 /* 11670 * atapi: Since we are running the CD for now in PIO mode we need to 11671 * call bp_mapin here to avoid bp_mapin called interrupt context under 11672 * the HBA's init_pkt routine. 11673 */ 11674 if (un->un_f_cfg_is_atapi == TRUE) { 11675 mutex_exit(SD_MUTEX(un)); 11676 bp_mapin(bp); 11677 mutex_enter(SD_MUTEX(un)); 11678 } 11679 un->un_ncmds_in_driver++; 11680 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11681 un->un_ncmds_in_driver); 11682 11683 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) && 11684 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1)) 11685 un->un_f_sync_cache_required = TRUE; 11686 11687 mutex_exit(SD_MUTEX(un)); 11688 11689 switch (uip->ui_flags) { 11690 case SD_PATH_DIRECT: 11691 chain_type = SD_CHAIN_DIRECT; 11692 break; 11693 case SD_PATH_DIRECT_PRIORITY: 11694 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11695 break; 11696 default: 11697 chain_type = SD_CHAIN_USCSI; 11698 break; 11699 } 11700 11701 /* 11702 * We may allocate extra buf for external USCSI commands. If the 11703 * application asks for bigger than 20-byte sense data via USCSI, 11704 * SCSA layer will allocate 252 bytes sense buf for that command. 11705 */ 11706 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 11707 SENSE_LENGTH) { 11708 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 11709 MAX_SENSE_LENGTH, KM_SLEEP); 11710 } else { 11711 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 11712 } 11713 11714 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11715 11716 /* Use the index obtained within xbuf_init */ 11717 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11718 11719 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11720 11721 return (0); 11722 } 11723 11724 /* 11725 * Function: sd_send_scsi_cmd 11726 * 11727 * Description: Runs a USCSI command for user (when called thru sdioctl), 11728 * or for the driver 11729 * 11730 * Arguments: dev - the dev_t for the device 11731 * incmd - ptr to a valid uscsi_cmd struct 11732 * flag - bit flag, indicating open settings, 32/64 bit type 11733 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11734 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11735 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11736 * to use the USCSI "direct" chain and bypass the normal 11737 * command waitq. 11738 * 11739 * Return Code: 0 - successful completion of the given command 11740 * EIO - scsi_uscsi_handle_command() failed 11741 * ENXIO - soft state not found for specified dev 11742 * EINVAL 11743 * EFAULT - copyin/copyout error 11744 * return code of scsi_uscsi_handle_command(): 11745 * EIO 11746 * ENXIO 11747 * EACCES 11748 * 11749 * Context: Waits for command to complete. Can sleep. 11750 */ 11751 11752 static int 11753 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 11754 enum uio_seg dataspace, int path_flag) 11755 { 11756 struct sd_lun *un; 11757 sd_ssc_t *ssc; 11758 int rval; 11759 11760 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11761 if (un == NULL) { 11762 return (ENXIO); 11763 } 11764 11765 /* 11766 * Using sd_ssc_send to handle uscsi cmd 11767 */ 11768 ssc = sd_ssc_init(un); 11769 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag); 11770 sd_ssc_fini(ssc); 11771 11772 return (rval); 11773 } 11774 11775 /* 11776 * Function: sd_ssc_init 11777 * 11778 * Description: Uscsi end-user call this function to initialize necessary 11779 * fields, such as uscsi_cmd and sd_uscsi_info struct. 11780 * 11781 * The return value of sd_send_scsi_cmd will be treated as a 11782 * fault in various conditions. Even it is not Zero, some 11783 * callers may ignore the return value. That is to say, we can 11784 * not make an accurate assessment in sdintr, since if a 11785 * command is failed in sdintr it does not mean the caller of 11786 * sd_send_scsi_cmd will treat it as a real failure. 11787 * 11788 * To avoid printing too many error logs for a failed uscsi 11789 * packet that the caller may not treat it as a failure, the 11790 * sd will keep silent for handling all uscsi commands. 11791 * 11792 * During detach->attach and attach-open, for some types of 11793 * problems, the driver should be providing information about 11794 * the problem encountered. Device use USCSI_SILENT, which 11795 * suppresses all driver information. The result is that no 11796 * information about the problem is available. Being 11797 * completely silent during this time is inappropriate. The 11798 * driver needs a more selective filter than USCSI_SILENT, so 11799 * that information related to faults is provided. 11800 * 11801 * To make the accurate accessment, the caller of 11802 * sd_send_scsi_USCSI_CMD should take the ownership and 11803 * get necessary information to print error messages. 11804 * 11805 * If we want to print necessary info of uscsi command, we need to 11806 * keep the uscsi_cmd and sd_uscsi_info till we can make the 11807 * assessment. We use sd_ssc_init to alloc necessary 11808 * structs for sending an uscsi command and we are also 11809 * responsible for free the memory by calling 11810 * sd_ssc_fini. 11811 * 11812 * The calling secquences will look like: 11813 * sd_ssc_init-> 11814 * 11815 * ... 11816 * 11817 * sd_send_scsi_USCSI_CMD-> 11818 * sd_ssc_send-> - - - sdintr 11819 * ... 11820 * 11821 * if we think the return value should be treated as a 11822 * failure, we make the accessment here and print out 11823 * necessary by retrieving uscsi_cmd and sd_uscsi_info' 11824 * 11825 * ... 11826 * 11827 * sd_ssc_fini 11828 * 11829 * 11830 * Arguments: un - pointer to driver soft state (unit) structure for this 11831 * target. 11832 * 11833 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains 11834 * uscsi_cmd and sd_uscsi_info. 11835 * NULL - if can not alloc memory for sd_ssc_t struct 11836 * 11837 * Context: Kernel Thread. 11838 */ 11839 static sd_ssc_t * 11840 sd_ssc_init(struct sd_lun *un) 11841 { 11842 sd_ssc_t *ssc; 11843 struct uscsi_cmd *ucmdp; 11844 struct sd_uscsi_info *uip; 11845 11846 ASSERT(un != NULL); 11847 ASSERT(!mutex_owned(SD_MUTEX(un))); 11848 11849 /* 11850 * Allocate sd_ssc_t structure 11851 */ 11852 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP); 11853 11854 /* 11855 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine 11856 */ 11857 ucmdp = scsi_uscsi_alloc(); 11858 11859 /* 11860 * Allocate sd_uscsi_info structure 11861 */ 11862 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11863 11864 ssc->ssc_uscsi_cmd = ucmdp; 11865 ssc->ssc_uscsi_info = uip; 11866 ssc->ssc_un = un; 11867 11868 return (ssc); 11869 } 11870 11871 /* 11872 * Function: sd_ssc_fini 11873 * 11874 * Description: To free sd_ssc_t and it's hanging off 11875 * 11876 * Arguments: ssc - struct pointer of sd_ssc_t. 11877 */ 11878 static void 11879 sd_ssc_fini(sd_ssc_t *ssc) 11880 { 11881 scsi_uscsi_free(ssc->ssc_uscsi_cmd); 11882 11883 if (ssc->ssc_uscsi_info != NULL) { 11884 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info)); 11885 ssc->ssc_uscsi_info = NULL; 11886 } 11887 11888 kmem_free(ssc, sizeof (sd_ssc_t)); 11889 ssc = NULL; 11890 } 11891 11892 /* 11893 * Function: sd_ssc_send 11894 * 11895 * Description: Runs a USCSI command for user when called through sdioctl, 11896 * or for the driver. 11897 * 11898 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11899 * sd_uscsi_info in. 11900 * incmd - ptr to a valid uscsi_cmd struct 11901 * flag - bit flag, indicating open settings, 32/64 bit type 11902 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11903 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11904 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11905 * to use the USCSI "direct" chain and bypass the normal 11906 * command waitq. 11907 * 11908 * Return Code: 0 - successful completion of the given command 11909 * EIO - scsi_uscsi_handle_command() failed 11910 * ENXIO - soft state not found for specified dev 11911 * ECANCELED - command cancelled due to low power 11912 * EINVAL 11913 * EFAULT - copyin/copyout error 11914 * return code of scsi_uscsi_handle_command(): 11915 * EIO 11916 * ENXIO 11917 * EACCES 11918 * 11919 * Context: Kernel Thread; 11920 * Waits for command to complete. Can sleep. 11921 */ 11922 static int 11923 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag, 11924 enum uio_seg dataspace, int path_flag) 11925 { 11926 struct sd_uscsi_info *uip; 11927 struct uscsi_cmd *uscmd; 11928 struct sd_lun *un; 11929 dev_t dev; 11930 11931 int format = 0; 11932 int rval; 11933 11934 ASSERT(ssc != NULL); 11935 un = ssc->ssc_un; 11936 ASSERT(un != NULL); 11937 uscmd = ssc->ssc_uscsi_cmd; 11938 ASSERT(uscmd != NULL); 11939 ASSERT(!mutex_owned(SD_MUTEX(un))); 11940 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 11941 /* 11942 * If enter here, it indicates that the previous uscsi 11943 * command has not been processed by sd_ssc_assessment. 11944 * This is violating our rules of FMA telemetry processing. 11945 * We should print out this message and the last undisposed 11946 * uscsi command. 11947 */ 11948 if (uscmd->uscsi_cdb != NULL) { 11949 SD_INFO(SD_LOG_SDTEST, un, 11950 "sd_ssc_send is missing the alternative " 11951 "sd_ssc_assessment when running command 0x%x.\n", 11952 uscmd->uscsi_cdb[0]); 11953 } 11954 /* 11955 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be 11956 * the initial status. 11957 */ 11958 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11959 } 11960 11961 /* 11962 * We need to make sure sd_ssc_send will have sd_ssc_assessment 11963 * followed to avoid missing FMA telemetries. 11964 */ 11965 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT; 11966 11967 /* 11968 * if USCSI_PMFAILFAST is set and un is in low power, fail the 11969 * command immediately. 11970 */ 11971 mutex_enter(SD_MUTEX(un)); 11972 mutex_enter(&un->un_pm_mutex); 11973 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) && 11974 SD_DEVICE_IS_IN_LOW_POWER(un)) { 11975 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:" 11976 "un:0x%p is in low power\n", un); 11977 mutex_exit(&un->un_pm_mutex); 11978 mutex_exit(SD_MUTEX(un)); 11979 return (ECANCELED); 11980 } 11981 mutex_exit(&un->un_pm_mutex); 11982 mutex_exit(SD_MUTEX(un)); 11983 11984 #ifdef SDDEBUG 11985 switch (dataspace) { 11986 case UIO_USERSPACE: 11987 SD_TRACE(SD_LOG_IO, un, 11988 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un); 11989 break; 11990 case UIO_SYSSPACE: 11991 SD_TRACE(SD_LOG_IO, un, 11992 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un); 11993 break; 11994 default: 11995 SD_TRACE(SD_LOG_IO, un, 11996 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un); 11997 break; 11998 } 11999 #endif 12000 12001 rval = scsi_uscsi_copyin((intptr_t)incmd, flag, 12002 SD_ADDRESS(un), &uscmd); 12003 if (rval != 0) { 12004 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 12005 "scsi_uscsi_alloc_and_copyin failed\n", un); 12006 return (rval); 12007 } 12008 12009 if ((uscmd->uscsi_cdb != NULL) && 12010 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 12011 mutex_enter(SD_MUTEX(un)); 12012 un->un_f_format_in_progress = TRUE; 12013 mutex_exit(SD_MUTEX(un)); 12014 format = 1; 12015 } 12016 12017 /* 12018 * Allocate an sd_uscsi_info struct and fill it with the info 12019 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12020 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12021 * since we allocate the buf here in this function, we do not 12022 * need to preserve the prior contents of b_private. 12023 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12024 */ 12025 uip = ssc->ssc_uscsi_info; 12026 uip->ui_flags = path_flag; 12027 uip->ui_cmdp = uscmd; 12028 12029 /* 12030 * Commands sent with priority are intended for error recovery 12031 * situations, and do not have retries performed. 12032 */ 12033 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12034 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12035 } 12036 uscmd->uscsi_flags &= ~USCSI_NOINTR; 12037 12038 dev = SD_GET_DEV(un); 12039 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 12040 sd_uscsi_strategy, NULL, uip); 12041 12042 /* 12043 * mark ssc_flags right after handle_cmd to make sure 12044 * the uscsi has been sent 12045 */ 12046 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED; 12047 12048 #ifdef SDDEBUG 12049 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12050 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12051 uscmd->uscsi_status, uscmd->uscsi_resid); 12052 if (uscmd->uscsi_bufaddr != NULL) { 12053 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 12054 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12055 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12056 if (dataspace == UIO_SYSSPACE) { 12057 SD_DUMP_MEMORY(un, SD_LOG_IO, 12058 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12059 uscmd->uscsi_buflen, SD_LOG_HEX); 12060 } 12061 } 12062 #endif 12063 12064 if (format == 1) { 12065 mutex_enter(SD_MUTEX(un)); 12066 un->un_f_format_in_progress = FALSE; 12067 mutex_exit(SD_MUTEX(un)); 12068 } 12069 12070 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd); 12071 12072 return (rval); 12073 } 12074 12075 /* 12076 * Function: sd_ssc_print 12077 * 12078 * Description: Print information available to the console. 12079 * 12080 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12081 * sd_uscsi_info in. 12082 * sd_severity - log level. 12083 * Context: Kernel thread or interrupt context. 12084 */ 12085 static void 12086 sd_ssc_print(sd_ssc_t *ssc, int sd_severity) 12087 { 12088 struct uscsi_cmd *ucmdp; 12089 struct scsi_device *devp; 12090 dev_info_t *devinfo; 12091 uchar_t *sensep; 12092 int senlen; 12093 union scsi_cdb *cdbp; 12094 uchar_t com; 12095 extern struct scsi_key_strings scsi_cmds[]; 12096 12097 ASSERT(ssc != NULL); 12098 ASSERT(ssc->ssc_un != NULL); 12099 12100 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT) 12101 return; 12102 ucmdp = ssc->ssc_uscsi_cmd; 12103 devp = SD_SCSI_DEVP(ssc->ssc_un); 12104 devinfo = SD_DEVINFO(ssc->ssc_un); 12105 ASSERT(ucmdp != NULL); 12106 ASSERT(devp != NULL); 12107 ASSERT(devinfo != NULL); 12108 sensep = (uint8_t *)ucmdp->uscsi_rqbuf; 12109 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid; 12110 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb; 12111 12112 /* In certain case (like DOORLOCK), the cdb could be NULL. */ 12113 if (cdbp == NULL) 12114 return; 12115 /* We don't print log if no sense data available. */ 12116 if (senlen == 0) 12117 sensep = NULL; 12118 com = cdbp->scc_cmd; 12119 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com, 12120 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL); 12121 } 12122 12123 /* 12124 * Function: sd_ssc_assessment 12125 * 12126 * Description: We use this function to make an assessment at the point 12127 * where SD driver may encounter a potential error. 12128 * 12129 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12130 * sd_uscsi_info in. 12131 * tp_assess - a hint of strategy for ereport posting. 12132 * Possible values of tp_assess include: 12133 * SD_FMT_IGNORE - we don't post any ereport because we're 12134 * sure that it is ok to ignore the underlying problems. 12135 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now 12136 * but it might be not correct to ignore the underlying hardware 12137 * error. 12138 * SD_FMT_STATUS_CHECK - we will post an ereport with the 12139 * payload driver-assessment of value "fail" or 12140 * "fatal"(depending on what information we have here). This 12141 * assessment value is usually set when SD driver think there 12142 * is a potential error occurred(Typically, when return value 12143 * of the SCSI command is EIO). 12144 * SD_FMT_STANDARD - we will post an ereport with the payload 12145 * driver-assessment of value "info". This assessment value is 12146 * set when the SCSI command returned successfully and with 12147 * sense data sent back. 12148 * 12149 * Context: Kernel thread. 12150 */ 12151 static void 12152 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess) 12153 { 12154 int senlen = 0; 12155 struct uscsi_cmd *ucmdp = NULL; 12156 struct sd_lun *un; 12157 12158 ASSERT(ssc != NULL); 12159 un = ssc->ssc_un; 12160 ASSERT(un != NULL); 12161 ucmdp = ssc->ssc_uscsi_cmd; 12162 ASSERT(ucmdp != NULL); 12163 12164 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 12165 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT; 12166 } else { 12167 /* 12168 * If enter here, it indicates that we have a wrong 12169 * calling sequence of sd_ssc_send and sd_ssc_assessment, 12170 * both of which should be called in a pair in case of 12171 * loss of FMA telemetries. 12172 */ 12173 if (ucmdp->uscsi_cdb != NULL) { 12174 SD_INFO(SD_LOG_SDTEST, un, 12175 "sd_ssc_assessment is missing the " 12176 "alternative sd_ssc_send when running 0x%x, " 12177 "or there are superfluous sd_ssc_assessment for " 12178 "the same sd_ssc_send.\n", 12179 ucmdp->uscsi_cdb[0]); 12180 } 12181 /* 12182 * Set the ssc_flags to the initial value to avoid passing 12183 * down dirty flags to the following sd_ssc_send function. 12184 */ 12185 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12186 return; 12187 } 12188 12189 /* 12190 * Only handle an issued command which is waiting for assessment. 12191 * A command which is not issued will not have 12192 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here. 12193 */ 12194 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) { 12195 sd_ssc_print(ssc, SCSI_ERR_INFO); 12196 return; 12197 } else { 12198 /* 12199 * For an issued command, we should clear this flag in 12200 * order to make the sd_ssc_t structure be used off 12201 * multiple uscsi commands. 12202 */ 12203 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED; 12204 } 12205 12206 /* 12207 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set) 12208 * commands here. And we should clear the ssc_flags before return. 12209 */ 12210 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) { 12211 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12212 return; 12213 } 12214 12215 switch (tp_assess) { 12216 case SD_FMT_IGNORE: 12217 case SD_FMT_IGNORE_COMPROMISE: 12218 break; 12219 case SD_FMT_STATUS_CHECK: 12220 /* 12221 * For a failed command(including the succeeded command 12222 * with invalid data sent back). 12223 */ 12224 sd_ssc_post(ssc, SD_FM_DRV_FATAL); 12225 break; 12226 case SD_FMT_STANDARD: 12227 /* 12228 * Always for the succeeded commands probably with sense 12229 * data sent back. 12230 * Limitation: 12231 * We can only handle a succeeded command with sense 12232 * data sent back when auto-request-sense is enabled. 12233 */ 12234 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen - 12235 ssc->ssc_uscsi_cmd->uscsi_rqresid; 12236 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) && 12237 (un->un_f_arq_enabled == TRUE) && 12238 senlen > 0 && 12239 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) { 12240 sd_ssc_post(ssc, SD_FM_DRV_NOTICE); 12241 } 12242 break; 12243 default: 12244 /* 12245 * Should not have other type of assessment. 12246 */ 12247 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 12248 "sd_ssc_assessment got wrong " 12249 "sd_type_assessment %d.\n", tp_assess); 12250 break; 12251 } 12252 /* 12253 * Clear up the ssc_flags before return. 12254 */ 12255 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12256 } 12257 12258 /* 12259 * Function: sd_ssc_post 12260 * 12261 * Description: 1. read the driver property to get fm-scsi-log flag. 12262 * 2. print log if fm_log_capable is non-zero. 12263 * 3. call sd_ssc_ereport_post to post ereport if possible. 12264 * 12265 * Context: May be called from kernel thread or interrupt context. 12266 */ 12267 static void 12268 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess) 12269 { 12270 struct sd_lun *un; 12271 int sd_severity; 12272 12273 ASSERT(ssc != NULL); 12274 un = ssc->ssc_un; 12275 ASSERT(un != NULL); 12276 12277 /* 12278 * We may enter here from sd_ssc_assessment(for USCSI command) or 12279 * by directly called from sdintr context. 12280 * We don't handle a non-disk drive(CD-ROM, removable media). 12281 * Clear the ssc_flags before return in case we've set 12282 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk 12283 * driver. 12284 */ 12285 if (ISCD(un) || un->un_f_has_removable_media) { 12286 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 12287 return; 12288 } 12289 12290 switch (sd_assess) { 12291 case SD_FM_DRV_FATAL: 12292 sd_severity = SCSI_ERR_FATAL; 12293 break; 12294 case SD_FM_DRV_RECOVERY: 12295 sd_severity = SCSI_ERR_RECOVERED; 12296 break; 12297 case SD_FM_DRV_RETRY: 12298 sd_severity = SCSI_ERR_RETRYABLE; 12299 break; 12300 case SD_FM_DRV_NOTICE: 12301 sd_severity = SCSI_ERR_INFO; 12302 break; 12303 default: 12304 sd_severity = SCSI_ERR_UNKNOWN; 12305 } 12306 /* print log */ 12307 sd_ssc_print(ssc, sd_severity); 12308 12309 /* always post ereport */ 12310 sd_ssc_ereport_post(ssc, sd_assess); 12311 } 12312 12313 /* 12314 * Function: sd_ssc_set_info 12315 * 12316 * Description: Mark ssc_flags and set ssc_info which would be the 12317 * payload of uderr ereport. This function will cause 12318 * sd_ssc_ereport_post to post uderr ereport only. 12319 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI), 12320 * the function will also call SD_ERROR or scsi_log for a 12321 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device. 12322 * 12323 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 12324 * sd_uscsi_info in. 12325 * ssc_flags - indicate the sub-category of a uderr. 12326 * comp - this argument is meaningful only when 12327 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible 12328 * values include: 12329 * > 0, SD_ERROR is used with comp as the driver logging 12330 * component; 12331 * = 0, scsi-log is used to log error telemetries; 12332 * < 0, no log available for this telemetry. 12333 * 12334 * Context: Kernel thread or interrupt context 12335 */ 12336 static void 12337 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...) 12338 { 12339 va_list ap; 12340 12341 ASSERT(ssc != NULL); 12342 ASSERT(ssc->ssc_un != NULL); 12343 12344 ssc->ssc_flags |= ssc_flags; 12345 va_start(ap, fmt); 12346 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap); 12347 va_end(ap); 12348 12349 /* 12350 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command 12351 * with invalid data sent back. For non-uscsi command, the 12352 * following code will be bypassed. 12353 */ 12354 if (ssc_flags & SSC_FLAGS_INVALID_DATA) { 12355 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) { 12356 /* 12357 * If the error belong to certain component and we 12358 * do not want it to show up on the console, we 12359 * will use SD_ERROR, otherwise scsi_log is 12360 * preferred. 12361 */ 12362 if (comp > 0) { 12363 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info); 12364 } else if (comp == 0) { 12365 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label, 12366 CE_WARN, ssc->ssc_info); 12367 } 12368 } 12369 } 12370 } 12371 12372 /* 12373 * Function: sd_buf_iodone 12374 * 12375 * Description: Frees the sd_xbuf & returns the buf to its originator. 12376 * 12377 * Context: May be called from interrupt context. 12378 */ 12379 /* ARGSUSED */ 12380 static void 12381 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12382 { 12383 struct sd_xbuf *xp; 12384 12385 ASSERT(un != NULL); 12386 ASSERT(bp != NULL); 12387 ASSERT(!mutex_owned(SD_MUTEX(un))); 12388 12389 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12390 12391 xp = SD_GET_XBUF(bp); 12392 ASSERT(xp != NULL); 12393 12394 /* xbuf is gone after this */ 12395 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) { 12396 mutex_enter(SD_MUTEX(un)); 12397 12398 /* 12399 * Grab time when the cmd completed. 12400 * This is used for determining if the system has been 12401 * idle long enough to make it idle to the PM framework. 12402 * This is for lowering the overhead, and therefore improving 12403 * performance per I/O operation. 12404 */ 12405 un->un_pm_idle_time = ddi_get_time(); 12406 12407 un->un_ncmds_in_driver--; 12408 ASSERT(un->un_ncmds_in_driver >= 0); 12409 SD_INFO(SD_LOG_IO, un, 12410 "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12411 un->un_ncmds_in_driver); 12412 12413 mutex_exit(SD_MUTEX(un)); 12414 } 12415 12416 biodone(bp); /* bp is gone after this */ 12417 12418 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12419 } 12420 12421 12422 /* 12423 * Function: sd_uscsi_iodone 12424 * 12425 * Description: Frees the sd_xbuf & returns the buf to its originator. 12426 * 12427 * Context: May be called from interrupt context. 12428 */ 12429 /* ARGSUSED */ 12430 static void 12431 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12432 { 12433 struct sd_xbuf *xp; 12434 12435 ASSERT(un != NULL); 12436 ASSERT(bp != NULL); 12437 12438 xp = SD_GET_XBUF(bp); 12439 ASSERT(xp != NULL); 12440 ASSERT(!mutex_owned(SD_MUTEX(un))); 12441 12442 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12443 12444 bp->b_private = xp->xb_private; 12445 12446 mutex_enter(SD_MUTEX(un)); 12447 12448 /* 12449 * Grab time when the cmd completed. 12450 * This is used for determining if the system has been 12451 * idle long enough to make it idle to the PM framework. 12452 * This is for lowering the overhead, and therefore improving 12453 * performance per I/O operation. 12454 */ 12455 un->un_pm_idle_time = ddi_get_time(); 12456 12457 un->un_ncmds_in_driver--; 12458 ASSERT(un->un_ncmds_in_driver >= 0); 12459 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12460 un->un_ncmds_in_driver); 12461 12462 mutex_exit(SD_MUTEX(un)); 12463 12464 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 12465 SENSE_LENGTH) { 12466 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 12467 MAX_SENSE_LENGTH); 12468 } else { 12469 kmem_free(xp, sizeof (struct sd_xbuf)); 12470 } 12471 12472 biodone(bp); 12473 12474 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12475 } 12476 12477 12478 /* 12479 * Function: sd_mapblockaddr_iostart 12480 * 12481 * Description: Verify request lies within the partition limits for 12482 * the indicated minor device. Issue "overrun" buf if 12483 * request would exceed partition range. Converts 12484 * partition-relative block address to absolute. 12485 * 12486 * Upon exit of this function: 12487 * 1.I/O is aligned 12488 * xp->xb_blkno represents the absolute sector address 12489 * 2.I/O is misaligned 12490 * xp->xb_blkno represents the absolute logical block address 12491 * based on DEV_BSIZE. The logical block address will be 12492 * converted to physical sector address in sd_mapblocksize_\ 12493 * iostart. 12494 * 3.I/O is misaligned but is aligned in "overrun" buf 12495 * xp->xb_blkno represents the absolute logical block address 12496 * based on DEV_BSIZE. The logical block address will be 12497 * converted to physical sector address in sd_mapblocksize_\ 12498 * iostart. But no RMW will be issued in this case. 12499 * 12500 * Context: Can sleep 12501 * 12502 * Issues: This follows what the old code did, in terms of accessing 12503 * some of the partition info in the unit struct without holding 12504 * the mutext. This is a general issue, if the partition info 12505 * can be altered while IO is in progress... as soon as we send 12506 * a buf, its partitioning can be invalid before it gets to the 12507 * device. Probably the right fix is to move partitioning out 12508 * of the driver entirely. 12509 */ 12510 12511 static void 12512 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12513 { 12514 diskaddr_t nblocks; /* #blocks in the given partition */ 12515 daddr_t blocknum; /* Block number specified by the buf */ 12516 size_t requested_nblocks; 12517 size_t available_nblocks; 12518 int partition; 12519 diskaddr_t partition_offset; 12520 struct sd_xbuf *xp; 12521 int secmask = 0, blknomask = 0; 12522 ushort_t is_aligned = TRUE; 12523 12524 ASSERT(un != NULL); 12525 ASSERT(bp != NULL); 12526 ASSERT(!mutex_owned(SD_MUTEX(un))); 12527 12528 SD_TRACE(SD_LOG_IO_PARTITION, un, 12529 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12530 12531 xp = SD_GET_XBUF(bp); 12532 ASSERT(xp != NULL); 12533 12534 /* 12535 * If the geometry is not indicated as valid, attempt to access 12536 * the unit & verify the geometry/label. This can be the case for 12537 * removable-media devices, of if the device was opened in 12538 * NDELAY/NONBLOCK mode. 12539 */ 12540 partition = SDPART(bp->b_edev); 12541 12542 if (!SD_IS_VALID_LABEL(un)) { 12543 sd_ssc_t *ssc; 12544 /* 12545 * Initialize sd_ssc_t for internal uscsi commands 12546 * In case of potential porformance issue, we need 12547 * to alloc memory only if there is invalid label 12548 */ 12549 ssc = sd_ssc_init(un); 12550 12551 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) { 12552 /* 12553 * For removable devices it is possible to start an 12554 * I/O without a media by opening the device in nodelay 12555 * mode. Also for writable CDs there can be many 12556 * scenarios where there is no geometry yet but volume 12557 * manager is trying to issue a read() just because 12558 * it can see TOC on the CD. So do not print a message 12559 * for removables. 12560 */ 12561 if (!un->un_f_has_removable_media) { 12562 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12563 "i/o to invalid geometry\n"); 12564 } 12565 bioerror(bp, EIO); 12566 bp->b_resid = bp->b_bcount; 12567 SD_BEGIN_IODONE(index, un, bp); 12568 12569 sd_ssc_fini(ssc); 12570 return; 12571 } 12572 sd_ssc_fini(ssc); 12573 } 12574 12575 nblocks = 0; 12576 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 12577 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 12578 12579 if (un->un_f_enable_rmw) { 12580 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1; 12581 secmask = un->un_phy_blocksize - 1; 12582 } else { 12583 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 12584 secmask = un->un_tgt_blocksize - 1; 12585 } 12586 12587 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) { 12588 is_aligned = FALSE; 12589 } 12590 12591 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) { 12592 /* 12593 * If I/O is aligned, no need to involve RMW(Read Modify Write) 12594 * Convert the logical block number to target's physical sector 12595 * number. 12596 */ 12597 if (is_aligned) { 12598 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno); 12599 } else { 12600 switch (un->un_f_rmw_type) { 12601 case SD_RMW_TYPE_RETURN_ERROR: 12602 if (un->un_f_enable_rmw) 12603 break; 12604 else { 12605 bp->b_flags |= B_ERROR; 12606 goto error_exit; 12607 } 12608 12609 case SD_RMW_TYPE_DEFAULT: 12610 mutex_enter(SD_MUTEX(un)); 12611 if (!un->un_f_enable_rmw && 12612 un->un_rmw_msg_timeid == NULL) { 12613 scsi_log(SD_DEVINFO(un), sd_label, 12614 CE_WARN, "I/O request is not " 12615 "aligned with %d disk sector size. " 12616 "It is handled through Read Modify " 12617 "Write but the performance is " 12618 "very low.\n", 12619 un->un_tgt_blocksize); 12620 un->un_rmw_msg_timeid = 12621 timeout(sd_rmw_msg_print_handler, 12622 un, SD_RMW_MSG_PRINT_TIMEOUT); 12623 } else { 12624 un->un_rmw_incre_count ++; 12625 } 12626 mutex_exit(SD_MUTEX(un)); 12627 break; 12628 12629 case SD_RMW_TYPE_NO_WARNING: 12630 default: 12631 break; 12632 } 12633 12634 nblocks = SD_TGT2SYSBLOCK(un, nblocks); 12635 partition_offset = SD_TGT2SYSBLOCK(un, 12636 partition_offset); 12637 } 12638 } 12639 12640 /* 12641 * blocknum is the starting block number of the request. At this 12642 * point it is still relative to the start of the minor device. 12643 */ 12644 blocknum = xp->xb_blkno; 12645 12646 /* 12647 * Legacy: If the starting block number is one past the last block 12648 * in the partition, do not set B_ERROR in the buf. 12649 */ 12650 if (blocknum == nblocks) { 12651 goto error_exit; 12652 } 12653 12654 /* 12655 * Confirm that the first block of the request lies within the 12656 * partition limits. Also the requested number of bytes must be 12657 * a multiple of the system block size. 12658 */ 12659 if ((blocknum < 0) || (blocknum >= nblocks) || 12660 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) { 12661 bp->b_flags |= B_ERROR; 12662 goto error_exit; 12663 } 12664 12665 /* 12666 * If the requsted # blocks exceeds the available # blocks, that 12667 * is an overrun of the partition. 12668 */ 12669 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12670 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 12671 } else { 12672 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount); 12673 } 12674 12675 available_nblocks = (size_t)(nblocks - blocknum); 12676 ASSERT(nblocks >= blocknum); 12677 12678 if (requested_nblocks > available_nblocks) { 12679 size_t resid; 12680 12681 /* 12682 * Allocate an "overrun" buf to allow the request to proceed 12683 * for the amount of space available in the partition. The 12684 * amount not transferred will be added into the b_resid 12685 * when the operation is complete. The overrun buf 12686 * replaces the original buf here, and the original buf 12687 * is saved inside the overrun buf, for later use. 12688 */ 12689 if ((!NOT_DEVBSIZE(un)) && is_aligned) { 12690 resid = SD_TGTBLOCKS2BYTES(un, 12691 (offset_t)(requested_nblocks - available_nblocks)); 12692 } else { 12693 resid = SD_SYSBLOCKS2BYTES( 12694 (offset_t)(requested_nblocks - available_nblocks)); 12695 } 12696 12697 size_t count = bp->b_bcount - resid; 12698 /* 12699 * Note: count is an unsigned entity thus it'll NEVER 12700 * be less than 0 so ASSERT the original values are 12701 * correct. 12702 */ 12703 ASSERT(bp->b_bcount >= resid); 12704 12705 bp = sd_bioclone_alloc(bp, count, blocknum, 12706 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12707 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12708 ASSERT(xp != NULL); 12709 } 12710 12711 /* At this point there should be no residual for this buf. */ 12712 ASSERT(bp->b_resid == 0); 12713 12714 /* Convert the block number to an absolute address. */ 12715 xp->xb_blkno += partition_offset; 12716 12717 SD_NEXT_IOSTART(index, un, bp); 12718 12719 SD_TRACE(SD_LOG_IO_PARTITION, un, 12720 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12721 12722 return; 12723 12724 error_exit: 12725 bp->b_resid = bp->b_bcount; 12726 SD_BEGIN_IODONE(index, un, bp); 12727 SD_TRACE(SD_LOG_IO_PARTITION, un, 12728 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12729 } 12730 12731 12732 /* 12733 * Function: sd_mapblockaddr_iodone 12734 * 12735 * Description: Completion-side processing for partition management. 12736 * 12737 * Context: May be called under interrupt context 12738 */ 12739 12740 static void 12741 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12742 { 12743 /* int partition; */ /* Not used, see below. */ 12744 ASSERT(un != NULL); 12745 ASSERT(bp != NULL); 12746 ASSERT(!mutex_owned(SD_MUTEX(un))); 12747 12748 SD_TRACE(SD_LOG_IO_PARTITION, un, 12749 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12750 12751 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12752 /* 12753 * We have an "overrun" buf to deal with... 12754 */ 12755 struct sd_xbuf *xp; 12756 struct buf *obp; /* ptr to the original buf */ 12757 12758 xp = SD_GET_XBUF(bp); 12759 ASSERT(xp != NULL); 12760 12761 /* Retrieve the pointer to the original buf */ 12762 obp = (struct buf *)xp->xb_private; 12763 ASSERT(obp != NULL); 12764 12765 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12766 bioerror(obp, bp->b_error); 12767 12768 sd_bioclone_free(bp); 12769 12770 /* 12771 * Get back the original buf. 12772 * Note that since the restoration of xb_blkno below 12773 * was removed, the sd_xbuf is not needed. 12774 */ 12775 bp = obp; 12776 /* 12777 * xp = SD_GET_XBUF(bp); 12778 * ASSERT(xp != NULL); 12779 */ 12780 } 12781 12782 /* 12783 * Convert sd->xb_blkno back to a minor-device relative value. 12784 * Note: this has been commented out, as it is not needed in the 12785 * current implementation of the driver (ie, since this function 12786 * is at the top of the layering chains, so the info will be 12787 * discarded) and it is in the "hot" IO path. 12788 * 12789 * partition = getminor(bp->b_edev) & SDPART_MASK; 12790 * xp->xb_blkno -= un->un_offset[partition]; 12791 */ 12792 12793 SD_NEXT_IODONE(index, un, bp); 12794 12795 SD_TRACE(SD_LOG_IO_PARTITION, un, 12796 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12797 } 12798 12799 12800 /* 12801 * Function: sd_mapblocksize_iostart 12802 * 12803 * Description: Convert between system block size (un->un_sys_blocksize) 12804 * and target block size (un->un_tgt_blocksize). 12805 * 12806 * Context: Can sleep to allocate resources. 12807 * 12808 * Assumptions: A higher layer has already performed any partition validation, 12809 * and converted the xp->xb_blkno to an absolute value relative 12810 * to the start of the device. 12811 * 12812 * It is also assumed that the higher layer has implemented 12813 * an "overrun" mechanism for the case where the request would 12814 * read/write beyond the end of a partition. In this case we 12815 * assume (and ASSERT) that bp->b_resid == 0. 12816 * 12817 * Note: The implementation for this routine assumes the target 12818 * block size remains constant between allocation and transport. 12819 */ 12820 12821 static void 12822 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12823 { 12824 struct sd_mapblocksize_info *bsp; 12825 struct sd_xbuf *xp; 12826 offset_t first_byte; 12827 daddr_t start_block, end_block; 12828 daddr_t request_bytes; 12829 ushort_t is_aligned = FALSE; 12830 12831 ASSERT(un != NULL); 12832 ASSERT(bp != NULL); 12833 ASSERT(!mutex_owned(SD_MUTEX(un))); 12834 ASSERT(bp->b_resid == 0); 12835 12836 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12837 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12838 12839 /* 12840 * For a non-writable CD, a write request is an error 12841 */ 12842 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12843 (un->un_f_mmc_writable_media == FALSE)) { 12844 bioerror(bp, EIO); 12845 bp->b_resid = bp->b_bcount; 12846 SD_BEGIN_IODONE(index, un, bp); 12847 return; 12848 } 12849 12850 /* 12851 * We do not need a shadow buf if the device is using 12852 * un->un_sys_blocksize as its block size or if bcount == 0. 12853 * In this case there is no layer-private data block allocated. 12854 */ 12855 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 12856 (bp->b_bcount == 0)) { 12857 goto done; 12858 } 12859 12860 #if defined(__i386) || defined(__amd64) 12861 /* We do not support non-block-aligned transfers for ROD devices */ 12862 ASSERT(!ISROD(un)); 12863 #endif 12864 12865 xp = SD_GET_XBUF(bp); 12866 ASSERT(xp != NULL); 12867 12868 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12869 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12870 un->un_tgt_blocksize, DEV_BSIZE); 12871 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12872 "request start block:0x%x\n", xp->xb_blkno); 12873 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12874 "request len:0x%x\n", bp->b_bcount); 12875 12876 /* 12877 * Allocate the layer-private data area for the mapblocksize layer. 12878 * Layers are allowed to use the xp_private member of the sd_xbuf 12879 * struct to store the pointer to their layer-private data block, but 12880 * each layer also has the responsibility of restoring the prior 12881 * contents of xb_private before returning the buf/xbuf to the 12882 * higher layer that sent it. 12883 * 12884 * Here we save the prior contents of xp->xb_private into the 12885 * bsp->mbs_oprivate field of our layer-private data area. This value 12886 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12887 * the layer-private area and returning the buf/xbuf to the layer 12888 * that sent it. 12889 * 12890 * Note that here we use kmem_zalloc for the allocation as there are 12891 * parts of the mapblocksize code that expect certain fields to be 12892 * zero unless explicitly set to a required value. 12893 */ 12894 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12895 bsp->mbs_oprivate = xp->xb_private; 12896 xp->xb_private = bsp; 12897 12898 /* 12899 * This treats the data on the disk (target) as an array of bytes. 12900 * first_byte is the byte offset, from the beginning of the device, 12901 * to the location of the request. This is converted from a 12902 * un->un_sys_blocksize block address to a byte offset, and then back 12903 * to a block address based upon a un->un_tgt_blocksize block size. 12904 * 12905 * xp->xb_blkno should be absolute upon entry into this function, 12906 * but, but it is based upon partitions that use the "system" 12907 * block size. It must be adjusted to reflect the block size of 12908 * the target. 12909 * 12910 * Note that end_block is actually the block that follows the last 12911 * block of the request, but that's what is needed for the computation. 12912 */ 12913 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 12914 if (un->un_f_enable_rmw) { 12915 start_block = xp->xb_blkno = 12916 (first_byte / un->un_phy_blocksize) * 12917 (un->un_phy_blocksize / DEV_BSIZE); 12918 end_block = ((first_byte + bp->b_bcount + 12919 un->un_phy_blocksize - 1) / un->un_phy_blocksize) * 12920 (un->un_phy_blocksize / DEV_BSIZE); 12921 } else { 12922 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12923 end_block = (first_byte + bp->b_bcount + 12924 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 12925 } 12926 12927 /* request_bytes is rounded up to a multiple of the target block size */ 12928 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12929 12930 /* 12931 * See if the starting address of the request and the request 12932 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12933 * then we do not need to allocate a shadow buf to handle the request. 12934 */ 12935 if (un->un_f_enable_rmw) { 12936 if (((first_byte % un->un_phy_blocksize) == 0) && 12937 ((bp->b_bcount % un->un_phy_blocksize) == 0)) { 12938 is_aligned = TRUE; 12939 } 12940 } else { 12941 if (((first_byte % un->un_tgt_blocksize) == 0) && 12942 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12943 is_aligned = TRUE; 12944 } 12945 } 12946 12947 if ((bp->b_flags & B_READ) == 0) { 12948 /* 12949 * Lock the range for a write operation. An aligned request is 12950 * considered a simple write; otherwise the request must be a 12951 * read-modify-write. 12952 */ 12953 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12954 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12955 } 12956 12957 /* 12958 * Alloc a shadow buf if the request is not aligned. Also, this is 12959 * where the READ command is generated for a read-modify-write. (The 12960 * write phase is deferred until after the read completes.) 12961 */ 12962 if (is_aligned == FALSE) { 12963 12964 struct sd_mapblocksize_info *shadow_bsp; 12965 struct sd_xbuf *shadow_xp; 12966 struct buf *shadow_bp; 12967 12968 /* 12969 * Allocate the shadow buf and it associated xbuf. Note that 12970 * after this call the xb_blkno value in both the original 12971 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12972 * same: absolute relative to the start of the device, and 12973 * adjusted for the target block size. The b_blkno in the 12974 * shadow buf will also be set to this value. We should never 12975 * change b_blkno in the original bp however. 12976 * 12977 * Note also that the shadow buf will always need to be a 12978 * READ command, regardless of whether the incoming command 12979 * is a READ or a WRITE. 12980 */ 12981 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12982 xp->xb_blkno, 12983 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12984 12985 shadow_xp = SD_GET_XBUF(shadow_bp); 12986 12987 /* 12988 * Allocate the layer-private data for the shadow buf. 12989 * (No need to preserve xb_private in the shadow xbuf.) 12990 */ 12991 shadow_xp->xb_private = shadow_bsp = 12992 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12993 12994 /* 12995 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12996 * to figure out where the start of the user data is (based upon 12997 * the system block size) in the data returned by the READ 12998 * command (which will be based upon the target blocksize). Note 12999 * that this is only really used if the request is unaligned. 13000 */ 13001 if (un->un_f_enable_rmw) { 13002 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13003 ((offset_t)xp->xb_blkno * un->un_sys_blocksize)); 13004 ASSERT((bsp->mbs_copy_offset >= 0) && 13005 (bsp->mbs_copy_offset < un->un_phy_blocksize)); 13006 } else { 13007 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13008 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13009 ASSERT((bsp->mbs_copy_offset >= 0) && 13010 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13011 } 13012 13013 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13014 13015 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13016 13017 /* Transfer the wmap (if any) to the shadow buf */ 13018 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13019 bsp->mbs_wmp = NULL; 13020 13021 /* 13022 * The shadow buf goes on from here in place of the 13023 * original buf. 13024 */ 13025 shadow_bsp->mbs_orig_bp = bp; 13026 bp = shadow_bp; 13027 } 13028 13029 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13030 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13031 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13032 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13033 request_bytes); 13034 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13035 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13036 13037 done: 13038 SD_NEXT_IOSTART(index, un, bp); 13039 13040 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13041 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13042 } 13043 13044 13045 /* 13046 * Function: sd_mapblocksize_iodone 13047 * 13048 * Description: Completion side processing for block-size mapping. 13049 * 13050 * Context: May be called under interrupt context 13051 */ 13052 13053 static void 13054 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13055 { 13056 struct sd_mapblocksize_info *bsp; 13057 struct sd_xbuf *xp; 13058 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13059 struct buf *orig_bp; /* ptr to the original buf */ 13060 offset_t shadow_end; 13061 offset_t request_end; 13062 offset_t shadow_start; 13063 ssize_t copy_offset; 13064 size_t copy_length; 13065 size_t shortfall; 13066 uint_t is_write; /* TRUE if this bp is a WRITE */ 13067 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13068 13069 ASSERT(un != NULL); 13070 ASSERT(bp != NULL); 13071 13072 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13073 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13074 13075 /* 13076 * There is no shadow buf or layer-private data if the target is 13077 * using un->un_sys_blocksize as its block size or if bcount == 0. 13078 */ 13079 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) || 13080 (bp->b_bcount == 0)) { 13081 goto exit; 13082 } 13083 13084 xp = SD_GET_XBUF(bp); 13085 ASSERT(xp != NULL); 13086 13087 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13088 bsp = xp->xb_private; 13089 13090 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13091 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13092 13093 if (is_write) { 13094 /* 13095 * For a WRITE request we must free up the block range that 13096 * we have locked up. This holds regardless of whether this is 13097 * an aligned write request or a read-modify-write request. 13098 */ 13099 sd_range_unlock(un, bsp->mbs_wmp); 13100 bsp->mbs_wmp = NULL; 13101 } 13102 13103 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13104 /* 13105 * An aligned read or write command will have no shadow buf; 13106 * there is not much else to do with it. 13107 */ 13108 goto done; 13109 } 13110 13111 orig_bp = bsp->mbs_orig_bp; 13112 ASSERT(orig_bp != NULL); 13113 orig_xp = SD_GET_XBUF(orig_bp); 13114 ASSERT(orig_xp != NULL); 13115 ASSERT(!mutex_owned(SD_MUTEX(un))); 13116 13117 if (!is_write && has_wmap) { 13118 /* 13119 * A READ with a wmap means this is the READ phase of a 13120 * read-modify-write. If an error occurred on the READ then 13121 * we do not proceed with the WRITE phase or copy any data. 13122 * Just release the write maps and return with an error. 13123 */ 13124 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13125 orig_bp->b_resid = orig_bp->b_bcount; 13126 bioerror(orig_bp, bp->b_error); 13127 sd_range_unlock(un, bsp->mbs_wmp); 13128 goto freebuf_done; 13129 } 13130 } 13131 13132 /* 13133 * Here is where we set up to copy the data from the shadow buf 13134 * into the space associated with the original buf. 13135 * 13136 * To deal with the conversion between block sizes, these 13137 * computations treat the data as an array of bytes, with the 13138 * first byte (byte 0) corresponding to the first byte in the 13139 * first block on the disk. 13140 */ 13141 13142 /* 13143 * shadow_start and shadow_len indicate the location and size of 13144 * the data returned with the shadow IO request. 13145 */ 13146 if (un->un_f_enable_rmw) { 13147 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno); 13148 } else { 13149 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13150 } 13151 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13152 13153 /* 13154 * copy_offset gives the offset (in bytes) from the start of the first 13155 * block of the READ request to the beginning of the data. We retrieve 13156 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13157 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13158 * data to be copied (in bytes). 13159 */ 13160 copy_offset = bsp->mbs_copy_offset; 13161 if (un->un_f_enable_rmw) { 13162 ASSERT((copy_offset >= 0) && 13163 (copy_offset < un->un_phy_blocksize)); 13164 } else { 13165 ASSERT((copy_offset >= 0) && 13166 (copy_offset < un->un_tgt_blocksize)); 13167 } 13168 13169 copy_length = orig_bp->b_bcount; 13170 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13171 13172 /* 13173 * Set up the resid and error fields of orig_bp as appropriate. 13174 */ 13175 if (shadow_end >= request_end) { 13176 /* We got all the requested data; set resid to zero */ 13177 orig_bp->b_resid = 0; 13178 } else { 13179 /* 13180 * We failed to get enough data to fully satisfy the original 13181 * request. Just copy back whatever data we got and set 13182 * up the residual and error code as required. 13183 * 13184 * 'shortfall' is the amount by which the data received with the 13185 * shadow buf has "fallen short" of the requested amount. 13186 */ 13187 shortfall = (size_t)(request_end - shadow_end); 13188 13189 if (shortfall > orig_bp->b_bcount) { 13190 /* 13191 * We did not get enough data to even partially 13192 * fulfill the original request. The residual is 13193 * equal to the amount requested. 13194 */ 13195 orig_bp->b_resid = orig_bp->b_bcount; 13196 } else { 13197 /* 13198 * We did not get all the data that we requested 13199 * from the device, but we will try to return what 13200 * portion we did get. 13201 */ 13202 orig_bp->b_resid = shortfall; 13203 } 13204 ASSERT(copy_length >= orig_bp->b_resid); 13205 copy_length -= orig_bp->b_resid; 13206 } 13207 13208 /* Propagate the error code from the shadow buf to the original buf */ 13209 bioerror(orig_bp, bp->b_error); 13210 13211 if (is_write) { 13212 goto freebuf_done; /* No data copying for a WRITE */ 13213 } 13214 13215 if (has_wmap) { 13216 /* 13217 * This is a READ command from the READ phase of a 13218 * read-modify-write request. We have to copy the data given 13219 * by the user OVER the data returned by the READ command, 13220 * then convert the command from a READ to a WRITE and send 13221 * it back to the target. 13222 */ 13223 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13224 copy_length); 13225 13226 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13227 13228 /* 13229 * Dispatch the WRITE command to the taskq thread, which 13230 * will in turn send the command to the target. When the 13231 * WRITE command completes, we (sd_mapblocksize_iodone()) 13232 * will get called again as part of the iodone chain 13233 * processing for it. Note that we will still be dealing 13234 * with the shadow buf at that point. 13235 */ 13236 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13237 KM_NOSLEEP) != 0) { 13238 /* 13239 * Dispatch was successful so we are done. Return 13240 * without going any higher up the iodone chain. Do 13241 * not free up any layer-private data until after the 13242 * WRITE completes. 13243 */ 13244 return; 13245 } 13246 13247 /* 13248 * Dispatch of the WRITE command failed; set up the error 13249 * condition and send this IO back up the iodone chain. 13250 */ 13251 bioerror(orig_bp, EIO); 13252 orig_bp->b_resid = orig_bp->b_bcount; 13253 13254 } else { 13255 /* 13256 * This is a regular READ request (ie, not a RMW). Copy the 13257 * data from the shadow buf into the original buf. The 13258 * copy_offset compensates for any "misalignment" between the 13259 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13260 * original buf (with its un->un_sys_blocksize blocks). 13261 */ 13262 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13263 copy_length); 13264 } 13265 13266 freebuf_done: 13267 13268 /* 13269 * At this point we still have both the shadow buf AND the original 13270 * buf to deal with, as well as the layer-private data area in each. 13271 * Local variables are as follows: 13272 * 13273 * bp -- points to shadow buf 13274 * xp -- points to xbuf of shadow buf 13275 * bsp -- points to layer-private data area of shadow buf 13276 * orig_bp -- points to original buf 13277 * 13278 * First free the shadow buf and its associated xbuf, then free the 13279 * layer-private data area from the shadow buf. There is no need to 13280 * restore xb_private in the shadow xbuf. 13281 */ 13282 sd_shadow_buf_free(bp); 13283 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13284 13285 /* 13286 * Now update the local variables to point to the original buf, xbuf, 13287 * and layer-private area. 13288 */ 13289 bp = orig_bp; 13290 xp = SD_GET_XBUF(bp); 13291 ASSERT(xp != NULL); 13292 ASSERT(xp == orig_xp); 13293 bsp = xp->xb_private; 13294 ASSERT(bsp != NULL); 13295 13296 done: 13297 /* 13298 * Restore xb_private to whatever it was set to by the next higher 13299 * layer in the chain, then free the layer-private data area. 13300 */ 13301 xp->xb_private = bsp->mbs_oprivate; 13302 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13303 13304 exit: 13305 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13306 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13307 13308 SD_NEXT_IODONE(index, un, bp); 13309 } 13310 13311 13312 /* 13313 * Function: sd_checksum_iostart 13314 * 13315 * Description: A stub function for a layer that's currently not used. 13316 * For now just a placeholder. 13317 * 13318 * Context: Kernel thread context 13319 */ 13320 13321 static void 13322 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13323 { 13324 ASSERT(un != NULL); 13325 ASSERT(bp != NULL); 13326 ASSERT(!mutex_owned(SD_MUTEX(un))); 13327 SD_NEXT_IOSTART(index, un, bp); 13328 } 13329 13330 13331 /* 13332 * Function: sd_checksum_iodone 13333 * 13334 * Description: A stub function for a layer that's currently not used. 13335 * For now just a placeholder. 13336 * 13337 * Context: May be called under interrupt context 13338 */ 13339 13340 static void 13341 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13342 { 13343 ASSERT(un != NULL); 13344 ASSERT(bp != NULL); 13345 ASSERT(!mutex_owned(SD_MUTEX(un))); 13346 SD_NEXT_IODONE(index, un, bp); 13347 } 13348 13349 13350 /* 13351 * Function: sd_checksum_uscsi_iostart 13352 * 13353 * Description: A stub function for a layer that's currently not used. 13354 * For now just a placeholder. 13355 * 13356 * Context: Kernel thread context 13357 */ 13358 13359 static void 13360 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13361 { 13362 ASSERT(un != NULL); 13363 ASSERT(bp != NULL); 13364 ASSERT(!mutex_owned(SD_MUTEX(un))); 13365 SD_NEXT_IOSTART(index, un, bp); 13366 } 13367 13368 13369 /* 13370 * Function: sd_checksum_uscsi_iodone 13371 * 13372 * Description: A stub function for a layer that's currently not used. 13373 * For now just a placeholder. 13374 * 13375 * Context: May be called under interrupt context 13376 */ 13377 13378 static void 13379 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13380 { 13381 ASSERT(un != NULL); 13382 ASSERT(bp != NULL); 13383 ASSERT(!mutex_owned(SD_MUTEX(un))); 13384 SD_NEXT_IODONE(index, un, bp); 13385 } 13386 13387 13388 /* 13389 * Function: sd_pm_iostart 13390 * 13391 * Description: iostart-side routine for Power mangement. 13392 * 13393 * Context: Kernel thread context 13394 */ 13395 13396 static void 13397 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13398 { 13399 ASSERT(un != NULL); 13400 ASSERT(bp != NULL); 13401 ASSERT(!mutex_owned(SD_MUTEX(un))); 13402 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13403 13404 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13405 13406 if (sd_pm_entry(un) != DDI_SUCCESS) { 13407 /* 13408 * Set up to return the failed buf back up the 'iodone' 13409 * side of the calling chain. 13410 */ 13411 bioerror(bp, EIO); 13412 bp->b_resid = bp->b_bcount; 13413 13414 SD_BEGIN_IODONE(index, un, bp); 13415 13416 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13417 return; 13418 } 13419 13420 SD_NEXT_IOSTART(index, un, bp); 13421 13422 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13423 } 13424 13425 13426 /* 13427 * Function: sd_pm_iodone 13428 * 13429 * Description: iodone-side routine for power mangement. 13430 * 13431 * Context: may be called from interrupt context 13432 */ 13433 13434 static void 13435 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13436 { 13437 ASSERT(un != NULL); 13438 ASSERT(bp != NULL); 13439 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13440 13441 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13442 13443 /* 13444 * After attach the following flag is only read, so don't 13445 * take the penalty of acquiring a mutex for it. 13446 */ 13447 if (un->un_f_pm_is_enabled == TRUE) { 13448 sd_pm_exit(un); 13449 } 13450 13451 SD_NEXT_IODONE(index, un, bp); 13452 13453 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13454 } 13455 13456 13457 /* 13458 * Function: sd_core_iostart 13459 * 13460 * Description: Primary driver function for enqueuing buf(9S) structs from 13461 * the system and initiating IO to the target device 13462 * 13463 * Context: Kernel thread context. Can sleep. 13464 * 13465 * Assumptions: - The given xp->xb_blkno is absolute 13466 * (ie, relative to the start of the device). 13467 * - The IO is to be done using the native blocksize of 13468 * the device, as specified in un->un_tgt_blocksize. 13469 */ 13470 /* ARGSUSED */ 13471 static void 13472 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13473 { 13474 struct sd_xbuf *xp; 13475 13476 ASSERT(un != NULL); 13477 ASSERT(bp != NULL); 13478 ASSERT(!mutex_owned(SD_MUTEX(un))); 13479 ASSERT(bp->b_resid == 0); 13480 13481 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13482 13483 xp = SD_GET_XBUF(bp); 13484 ASSERT(xp != NULL); 13485 13486 mutex_enter(SD_MUTEX(un)); 13487 13488 /* 13489 * If we are currently in the failfast state, fail any new IO 13490 * that has B_FAILFAST set, then return. 13491 */ 13492 if ((bp->b_flags & B_FAILFAST) && 13493 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13494 mutex_exit(SD_MUTEX(un)); 13495 bioerror(bp, EIO); 13496 bp->b_resid = bp->b_bcount; 13497 SD_BEGIN_IODONE(index, un, bp); 13498 return; 13499 } 13500 13501 if (SD_IS_DIRECT_PRIORITY(xp)) { 13502 /* 13503 * Priority command -- transport it immediately. 13504 * 13505 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13506 * because all direct priority commands should be associated 13507 * with error recovery actions which we don't want to retry. 13508 */ 13509 sd_start_cmds(un, bp); 13510 } else { 13511 /* 13512 * Normal command -- add it to the wait queue, then start 13513 * transporting commands from the wait queue. 13514 */ 13515 sd_add_buf_to_waitq(un, bp); 13516 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13517 sd_start_cmds(un, NULL); 13518 } 13519 13520 mutex_exit(SD_MUTEX(un)); 13521 13522 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13523 } 13524 13525 13526 /* 13527 * Function: sd_init_cdb_limits 13528 * 13529 * Description: This is to handle scsi_pkt initialization differences 13530 * between the driver platforms. 13531 * 13532 * Legacy behaviors: 13533 * 13534 * If the block number or the sector count exceeds the 13535 * capabilities of a Group 0 command, shift over to a 13536 * Group 1 command. We don't blindly use Group 1 13537 * commands because a) some drives (CDC Wren IVs) get a 13538 * bit confused, and b) there is probably a fair amount 13539 * of speed difference for a target to receive and decode 13540 * a 10 byte command instead of a 6 byte command. 13541 * 13542 * The xfer time difference of 6 vs 10 byte CDBs is 13543 * still significant so this code is still worthwhile. 13544 * 10 byte CDBs are very inefficient with the fas HBA driver 13545 * and older disks. Each CDB byte took 1 usec with some 13546 * popular disks. 13547 * 13548 * Context: Must be called at attach time 13549 */ 13550 13551 static void 13552 sd_init_cdb_limits(struct sd_lun *un) 13553 { 13554 int hba_cdb_limit; 13555 13556 /* 13557 * Use CDB_GROUP1 commands for most devices except for 13558 * parallel SCSI fixed drives in which case we get better 13559 * performance using CDB_GROUP0 commands (where applicable). 13560 */ 13561 un->un_mincdb = SD_CDB_GROUP1; 13562 #if !defined(__fibre) 13563 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13564 !un->un_f_has_removable_media) { 13565 un->un_mincdb = SD_CDB_GROUP0; 13566 } 13567 #endif 13568 13569 /* 13570 * Try to read the max-cdb-length supported by HBA. 13571 */ 13572 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13573 if (0 >= un->un_max_hba_cdb) { 13574 un->un_max_hba_cdb = CDB_GROUP4; 13575 hba_cdb_limit = SD_CDB_GROUP4; 13576 } else if (0 < un->un_max_hba_cdb && 13577 un->un_max_hba_cdb < CDB_GROUP1) { 13578 hba_cdb_limit = SD_CDB_GROUP0; 13579 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13580 un->un_max_hba_cdb < CDB_GROUP5) { 13581 hba_cdb_limit = SD_CDB_GROUP1; 13582 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13583 un->un_max_hba_cdb < CDB_GROUP4) { 13584 hba_cdb_limit = SD_CDB_GROUP5; 13585 } else { 13586 hba_cdb_limit = SD_CDB_GROUP4; 13587 } 13588 13589 /* 13590 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13591 * commands for fixed disks unless we are building for a 32 bit 13592 * kernel. 13593 */ 13594 #ifdef _LP64 13595 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13596 min(hba_cdb_limit, SD_CDB_GROUP4); 13597 #else 13598 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13599 min(hba_cdb_limit, SD_CDB_GROUP1); 13600 #endif 13601 13602 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13603 ? sizeof (struct scsi_arq_status) : 1); 13604 un->un_cmd_timeout = (ushort_t)sd_io_time; 13605 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13606 } 13607 13608 13609 /* 13610 * Function: sd_initpkt_for_buf 13611 * 13612 * Description: Allocate and initialize for transport a scsi_pkt struct, 13613 * based upon the info specified in the given buf struct. 13614 * 13615 * Assumes the xb_blkno in the request is absolute (ie, 13616 * relative to the start of the device (NOT partition!). 13617 * Also assumes that the request is using the native block 13618 * size of the device (as returned by the READ CAPACITY 13619 * command). 13620 * 13621 * Return Code: SD_PKT_ALLOC_SUCCESS 13622 * SD_PKT_ALLOC_FAILURE 13623 * SD_PKT_ALLOC_FAILURE_NO_DMA 13624 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13625 * 13626 * Context: Kernel thread and may be called from software interrupt context 13627 * as part of a sdrunout callback. This function may not block or 13628 * call routines that block 13629 */ 13630 13631 static int 13632 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13633 { 13634 struct sd_xbuf *xp; 13635 struct scsi_pkt *pktp = NULL; 13636 struct sd_lun *un; 13637 size_t blockcount; 13638 daddr_t startblock; 13639 int rval; 13640 int cmd_flags; 13641 13642 ASSERT(bp != NULL); 13643 ASSERT(pktpp != NULL); 13644 xp = SD_GET_XBUF(bp); 13645 ASSERT(xp != NULL); 13646 un = SD_GET_UN(bp); 13647 ASSERT(un != NULL); 13648 ASSERT(mutex_owned(SD_MUTEX(un))); 13649 ASSERT(bp->b_resid == 0); 13650 13651 SD_TRACE(SD_LOG_IO_CORE, un, 13652 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13653 13654 mutex_exit(SD_MUTEX(un)); 13655 13656 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13657 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13658 /* 13659 * Already have a scsi_pkt -- just need DMA resources. 13660 * We must recompute the CDB in case the mapping returns 13661 * a nonzero pkt_resid. 13662 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13663 * that is being retried, the unmap/remap of the DMA resouces 13664 * will result in the entire transfer starting over again 13665 * from the very first block. 13666 */ 13667 ASSERT(xp->xb_pktp != NULL); 13668 pktp = xp->xb_pktp; 13669 } else { 13670 pktp = NULL; 13671 } 13672 #endif /* __i386 || __amd64 */ 13673 13674 startblock = xp->xb_blkno; /* Absolute block num. */ 13675 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13676 13677 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13678 13679 /* 13680 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13681 * call scsi_init_pkt, and build the CDB. 13682 */ 13683 rval = sd_setup_rw_pkt(un, &pktp, bp, 13684 cmd_flags, sdrunout, (caddr_t)un, 13685 startblock, blockcount); 13686 13687 if (rval == 0) { 13688 /* 13689 * Success. 13690 * 13691 * If partial DMA is being used and required for this transfer. 13692 * set it up here. 13693 */ 13694 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13695 (pktp->pkt_resid != 0)) { 13696 13697 /* 13698 * Save the CDB length and pkt_resid for the 13699 * next xfer 13700 */ 13701 xp->xb_dma_resid = pktp->pkt_resid; 13702 13703 /* rezero resid */ 13704 pktp->pkt_resid = 0; 13705 13706 } else { 13707 xp->xb_dma_resid = 0; 13708 } 13709 13710 pktp->pkt_flags = un->un_tagflags; 13711 pktp->pkt_time = un->un_cmd_timeout; 13712 pktp->pkt_comp = sdintr; 13713 13714 pktp->pkt_private = bp; 13715 *pktpp = pktp; 13716 13717 SD_TRACE(SD_LOG_IO_CORE, un, 13718 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13719 13720 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13721 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13722 #endif 13723 13724 mutex_enter(SD_MUTEX(un)); 13725 return (SD_PKT_ALLOC_SUCCESS); 13726 13727 } 13728 13729 /* 13730 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13731 * from sd_setup_rw_pkt. 13732 */ 13733 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13734 13735 if (rval == SD_PKT_ALLOC_FAILURE) { 13736 *pktpp = NULL; 13737 /* 13738 * Set the driver state to RWAIT to indicate the driver 13739 * is waiting on resource allocations. The driver will not 13740 * suspend, pm_suspend, or detatch while the state is RWAIT. 13741 */ 13742 mutex_enter(SD_MUTEX(un)); 13743 New_state(un, SD_STATE_RWAIT); 13744 13745 SD_ERROR(SD_LOG_IO_CORE, un, 13746 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13747 13748 if ((bp->b_flags & B_ERROR) != 0) { 13749 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13750 } 13751 return (SD_PKT_ALLOC_FAILURE); 13752 } else { 13753 /* 13754 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13755 * 13756 * This should never happen. Maybe someone messed with the 13757 * kernel's minphys? 13758 */ 13759 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13760 "Request rejected: too large for CDB: " 13761 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13762 SD_ERROR(SD_LOG_IO_CORE, un, 13763 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13764 mutex_enter(SD_MUTEX(un)); 13765 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13766 13767 } 13768 } 13769 13770 13771 /* 13772 * Function: sd_destroypkt_for_buf 13773 * 13774 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13775 * 13776 * Context: Kernel thread or interrupt context 13777 */ 13778 13779 static void 13780 sd_destroypkt_for_buf(struct buf *bp) 13781 { 13782 ASSERT(bp != NULL); 13783 ASSERT(SD_GET_UN(bp) != NULL); 13784 13785 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13786 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13787 13788 ASSERT(SD_GET_PKTP(bp) != NULL); 13789 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13790 13791 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13792 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13793 } 13794 13795 /* 13796 * Function: sd_setup_rw_pkt 13797 * 13798 * Description: Determines appropriate CDB group for the requested LBA 13799 * and transfer length, calls scsi_init_pkt, and builds 13800 * the CDB. Do not use for partial DMA transfers except 13801 * for the initial transfer since the CDB size must 13802 * remain constant. 13803 * 13804 * Context: Kernel thread and may be called from software interrupt 13805 * context as part of a sdrunout callback. This function may not 13806 * block or call routines that block 13807 */ 13808 13809 13810 int 13811 sd_setup_rw_pkt(struct sd_lun *un, 13812 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13813 int (*callback)(caddr_t), caddr_t callback_arg, 13814 diskaddr_t lba, uint32_t blockcount) 13815 { 13816 struct scsi_pkt *return_pktp; 13817 union scsi_cdb *cdbp; 13818 struct sd_cdbinfo *cp = NULL; 13819 int i; 13820 13821 /* 13822 * See which size CDB to use, based upon the request. 13823 */ 13824 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13825 13826 /* 13827 * Check lba and block count against sd_cdbtab limits. 13828 * In the partial DMA case, we have to use the same size 13829 * CDB for all the transfers. Check lba + blockcount 13830 * against the max LBA so we know that segment of the 13831 * transfer can use the CDB we select. 13832 */ 13833 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13834 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13835 13836 /* 13837 * The command will fit into the CDB type 13838 * specified by sd_cdbtab[i]. 13839 */ 13840 cp = sd_cdbtab + i; 13841 13842 /* 13843 * Call scsi_init_pkt so we can fill in the 13844 * CDB. 13845 */ 13846 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13847 bp, cp->sc_grpcode, un->un_status_len, 0, 13848 flags, callback, callback_arg); 13849 13850 if (return_pktp != NULL) { 13851 13852 /* 13853 * Return new value of pkt 13854 */ 13855 *pktpp = return_pktp; 13856 13857 /* 13858 * To be safe, zero the CDB insuring there is 13859 * no leftover data from a previous command. 13860 */ 13861 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13862 13863 /* 13864 * Handle partial DMA mapping 13865 */ 13866 if (return_pktp->pkt_resid != 0) { 13867 13868 /* 13869 * Not going to xfer as many blocks as 13870 * originally expected 13871 */ 13872 blockcount -= 13873 SD_BYTES2TGTBLOCKS(un, 13874 return_pktp->pkt_resid); 13875 } 13876 13877 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13878 13879 /* 13880 * Set command byte based on the CDB 13881 * type we matched. 13882 */ 13883 cdbp->scc_cmd = cp->sc_grpmask | 13884 ((bp->b_flags & B_READ) ? 13885 SCMD_READ : SCMD_WRITE); 13886 13887 SD_FILL_SCSI1_LUN(un, return_pktp); 13888 13889 /* 13890 * Fill in LBA and length 13891 */ 13892 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13893 (cp->sc_grpcode == CDB_GROUP4) || 13894 (cp->sc_grpcode == CDB_GROUP0) || 13895 (cp->sc_grpcode == CDB_GROUP5)); 13896 13897 if (cp->sc_grpcode == CDB_GROUP1) { 13898 FORMG1ADDR(cdbp, lba); 13899 FORMG1COUNT(cdbp, blockcount); 13900 return (0); 13901 } else if (cp->sc_grpcode == CDB_GROUP4) { 13902 FORMG4LONGADDR(cdbp, lba); 13903 FORMG4COUNT(cdbp, blockcount); 13904 return (0); 13905 } else if (cp->sc_grpcode == CDB_GROUP0) { 13906 FORMG0ADDR(cdbp, lba); 13907 FORMG0COUNT(cdbp, blockcount); 13908 return (0); 13909 } else if (cp->sc_grpcode == CDB_GROUP5) { 13910 FORMG5ADDR(cdbp, lba); 13911 FORMG5COUNT(cdbp, blockcount); 13912 return (0); 13913 } 13914 13915 /* 13916 * It should be impossible to not match one 13917 * of the CDB types above, so we should never 13918 * reach this point. Set the CDB command byte 13919 * to test-unit-ready to avoid writing 13920 * to somewhere we don't intend. 13921 */ 13922 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13923 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13924 } else { 13925 /* 13926 * Couldn't get scsi_pkt 13927 */ 13928 return (SD_PKT_ALLOC_FAILURE); 13929 } 13930 } 13931 } 13932 13933 /* 13934 * None of the available CDB types were suitable. This really 13935 * should never happen: on a 64 bit system we support 13936 * READ16/WRITE16 which will hold an entire 64 bit disk address 13937 * and on a 32 bit system we will refuse to bind to a device 13938 * larger than 2TB so addresses will never be larger than 32 bits. 13939 */ 13940 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13941 } 13942 13943 /* 13944 * Function: sd_setup_next_rw_pkt 13945 * 13946 * Description: Setup packet for partial DMA transfers, except for the 13947 * initial transfer. sd_setup_rw_pkt should be used for 13948 * the initial transfer. 13949 * 13950 * Context: Kernel thread and may be called from interrupt context. 13951 */ 13952 13953 int 13954 sd_setup_next_rw_pkt(struct sd_lun *un, 13955 struct scsi_pkt *pktp, struct buf *bp, 13956 diskaddr_t lba, uint32_t blockcount) 13957 { 13958 uchar_t com; 13959 union scsi_cdb *cdbp; 13960 uchar_t cdb_group_id; 13961 13962 ASSERT(pktp != NULL); 13963 ASSERT(pktp->pkt_cdbp != NULL); 13964 13965 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13966 com = cdbp->scc_cmd; 13967 cdb_group_id = CDB_GROUPID(com); 13968 13969 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13970 (cdb_group_id == CDB_GROUPID_1) || 13971 (cdb_group_id == CDB_GROUPID_4) || 13972 (cdb_group_id == CDB_GROUPID_5)); 13973 13974 /* 13975 * Move pkt to the next portion of the xfer. 13976 * func is NULL_FUNC so we do not have to release 13977 * the disk mutex here. 13978 */ 13979 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13980 NULL_FUNC, NULL) == pktp) { 13981 /* Success. Handle partial DMA */ 13982 if (pktp->pkt_resid != 0) { 13983 blockcount -= 13984 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13985 } 13986 13987 cdbp->scc_cmd = com; 13988 SD_FILL_SCSI1_LUN(un, pktp); 13989 if (cdb_group_id == CDB_GROUPID_1) { 13990 FORMG1ADDR(cdbp, lba); 13991 FORMG1COUNT(cdbp, blockcount); 13992 return (0); 13993 } else if (cdb_group_id == CDB_GROUPID_4) { 13994 FORMG4LONGADDR(cdbp, lba); 13995 FORMG4COUNT(cdbp, blockcount); 13996 return (0); 13997 } else if (cdb_group_id == CDB_GROUPID_0) { 13998 FORMG0ADDR(cdbp, lba); 13999 FORMG0COUNT(cdbp, blockcount); 14000 return (0); 14001 } else if (cdb_group_id == CDB_GROUPID_5) { 14002 FORMG5ADDR(cdbp, lba); 14003 FORMG5COUNT(cdbp, blockcount); 14004 return (0); 14005 } 14006 14007 /* Unreachable */ 14008 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14009 } 14010 14011 /* 14012 * Error setting up next portion of cmd transfer. 14013 * Something is definitely very wrong and this 14014 * should not happen. 14015 */ 14016 return (SD_PKT_ALLOC_FAILURE); 14017 } 14018 14019 /* 14020 * Function: sd_initpkt_for_uscsi 14021 * 14022 * Description: Allocate and initialize for transport a scsi_pkt struct, 14023 * based upon the info specified in the given uscsi_cmd struct. 14024 * 14025 * Return Code: SD_PKT_ALLOC_SUCCESS 14026 * SD_PKT_ALLOC_FAILURE 14027 * SD_PKT_ALLOC_FAILURE_NO_DMA 14028 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14029 * 14030 * Context: Kernel thread and may be called from software interrupt context 14031 * as part of a sdrunout callback. This function may not block or 14032 * call routines that block 14033 */ 14034 14035 static int 14036 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14037 { 14038 struct uscsi_cmd *uscmd; 14039 struct sd_xbuf *xp; 14040 struct scsi_pkt *pktp; 14041 struct sd_lun *un; 14042 uint32_t flags = 0; 14043 14044 ASSERT(bp != NULL); 14045 ASSERT(pktpp != NULL); 14046 xp = SD_GET_XBUF(bp); 14047 ASSERT(xp != NULL); 14048 un = SD_GET_UN(bp); 14049 ASSERT(un != NULL); 14050 ASSERT(mutex_owned(SD_MUTEX(un))); 14051 14052 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14053 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14054 ASSERT(uscmd != NULL); 14055 14056 SD_TRACE(SD_LOG_IO_CORE, un, 14057 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14058 14059 /* 14060 * Allocate the scsi_pkt for the command. 14061 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14062 * during scsi_init_pkt time and will continue to use the 14063 * same path as long as the same scsi_pkt is used without 14064 * intervening scsi_dma_free(). Since uscsi command does 14065 * not call scsi_dmafree() before retry failed command, it 14066 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14067 * set such that scsi_vhci can use other available path for 14068 * retry. Besides, ucsci command does not allow DMA breakup, 14069 * so there is no need to set PKT_DMA_PARTIAL flag. 14070 */ 14071 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14072 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14073 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14074 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 14075 - sizeof (struct scsi_extended_sense)), 0, 14076 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 14077 sdrunout, (caddr_t)un); 14078 } else { 14079 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14080 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14081 sizeof (struct scsi_arq_status), 0, 14082 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14083 sdrunout, (caddr_t)un); 14084 } 14085 14086 if (pktp == NULL) { 14087 *pktpp = NULL; 14088 /* 14089 * Set the driver state to RWAIT to indicate the driver 14090 * is waiting on resource allocations. The driver will not 14091 * suspend, pm_suspend, or detatch while the state is RWAIT. 14092 */ 14093 New_state(un, SD_STATE_RWAIT); 14094 14095 SD_ERROR(SD_LOG_IO_CORE, un, 14096 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14097 14098 if ((bp->b_flags & B_ERROR) != 0) { 14099 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14100 } 14101 return (SD_PKT_ALLOC_FAILURE); 14102 } 14103 14104 /* 14105 * We do not do DMA breakup for USCSI commands, so return failure 14106 * here if all the needed DMA resources were not allocated. 14107 */ 14108 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14109 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14110 scsi_destroy_pkt(pktp); 14111 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14112 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14113 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14114 } 14115 14116 /* Init the cdb from the given uscsi struct */ 14117 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14118 uscmd->uscsi_cdb[0], 0, 0, 0); 14119 14120 SD_FILL_SCSI1_LUN(un, pktp); 14121 14122 /* 14123 * Set up the optional USCSI flags. See the uscsi (7I) man page 14124 * for listing of the supported flags. 14125 */ 14126 14127 if (uscmd->uscsi_flags & USCSI_SILENT) { 14128 flags |= FLAG_SILENT; 14129 } 14130 14131 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14132 flags |= FLAG_DIAGNOSE; 14133 } 14134 14135 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14136 flags |= FLAG_ISOLATE; 14137 } 14138 14139 if (un->un_f_is_fibre == FALSE) { 14140 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14141 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14142 } 14143 } 14144 14145 /* 14146 * Set the pkt flags here so we save time later. 14147 * Note: These flags are NOT in the uscsi man page!!! 14148 */ 14149 if (uscmd->uscsi_flags & USCSI_HEAD) { 14150 flags |= FLAG_HEAD; 14151 } 14152 14153 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14154 flags |= FLAG_NOINTR; 14155 } 14156 14157 /* 14158 * For tagged queueing, things get a bit complicated. 14159 * Check first for head of queue and last for ordered queue. 14160 * If neither head nor order, use the default driver tag flags. 14161 */ 14162 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14163 if (uscmd->uscsi_flags & USCSI_HTAG) { 14164 flags |= FLAG_HTAG; 14165 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14166 flags |= FLAG_OTAG; 14167 } else { 14168 flags |= un->un_tagflags & FLAG_TAGMASK; 14169 } 14170 } 14171 14172 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14173 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14174 } 14175 14176 pktp->pkt_flags = flags; 14177 14178 /* Transfer uscsi information to scsi_pkt */ 14179 (void) scsi_uscsi_pktinit(uscmd, pktp); 14180 14181 /* Copy the caller's CDB into the pkt... */ 14182 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14183 14184 if (uscmd->uscsi_timeout == 0) { 14185 pktp->pkt_time = un->un_uscsi_timeout; 14186 } else { 14187 pktp->pkt_time = uscmd->uscsi_timeout; 14188 } 14189 14190 /* need it later to identify USCSI request in sdintr */ 14191 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14192 14193 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14194 14195 pktp->pkt_private = bp; 14196 pktp->pkt_comp = sdintr; 14197 *pktpp = pktp; 14198 14199 SD_TRACE(SD_LOG_IO_CORE, un, 14200 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14201 14202 return (SD_PKT_ALLOC_SUCCESS); 14203 } 14204 14205 14206 /* 14207 * Function: sd_destroypkt_for_uscsi 14208 * 14209 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14210 * IOs.. Also saves relevant info into the associated uscsi_cmd 14211 * struct. 14212 * 14213 * Context: May be called under interrupt context 14214 */ 14215 14216 static void 14217 sd_destroypkt_for_uscsi(struct buf *bp) 14218 { 14219 struct uscsi_cmd *uscmd; 14220 struct sd_xbuf *xp; 14221 struct scsi_pkt *pktp; 14222 struct sd_lun *un; 14223 struct sd_uscsi_info *suip; 14224 14225 ASSERT(bp != NULL); 14226 xp = SD_GET_XBUF(bp); 14227 ASSERT(xp != NULL); 14228 un = SD_GET_UN(bp); 14229 ASSERT(un != NULL); 14230 ASSERT(!mutex_owned(SD_MUTEX(un))); 14231 pktp = SD_GET_PKTP(bp); 14232 ASSERT(pktp != NULL); 14233 14234 SD_TRACE(SD_LOG_IO_CORE, un, 14235 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14236 14237 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14238 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14239 ASSERT(uscmd != NULL); 14240 14241 /* Save the status and the residual into the uscsi_cmd struct */ 14242 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14243 uscmd->uscsi_resid = bp->b_resid; 14244 14245 /* Transfer scsi_pkt information to uscsi */ 14246 (void) scsi_uscsi_pktfini(pktp, uscmd); 14247 14248 /* 14249 * If enabled, copy any saved sense data into the area specified 14250 * by the uscsi command. 14251 */ 14252 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14253 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14254 /* 14255 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14256 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14257 */ 14258 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14259 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14260 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 14261 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14262 MAX_SENSE_LENGTH); 14263 } else { 14264 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 14265 SENSE_LENGTH); 14266 } 14267 } 14268 /* 14269 * The following assignments are for SCSI FMA. 14270 */ 14271 ASSERT(xp->xb_private != NULL); 14272 suip = (struct sd_uscsi_info *)xp->xb_private; 14273 suip->ui_pkt_reason = pktp->pkt_reason; 14274 suip->ui_pkt_state = pktp->pkt_state; 14275 suip->ui_pkt_statistics = pktp->pkt_statistics; 14276 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 14277 14278 /* We are done with the scsi_pkt; free it now */ 14279 ASSERT(SD_GET_PKTP(bp) != NULL); 14280 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14281 14282 SD_TRACE(SD_LOG_IO_CORE, un, 14283 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14284 } 14285 14286 14287 /* 14288 * Function: sd_bioclone_alloc 14289 * 14290 * Description: Allocate a buf(9S) and init it as per the given buf 14291 * and the various arguments. The associated sd_xbuf 14292 * struct is (nearly) duplicated. The struct buf *bp 14293 * argument is saved in new_xp->xb_private. 14294 * 14295 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14296 * datalen - size of data area for the shadow bp 14297 * blkno - starting LBA 14298 * func - function pointer for b_iodone in the shadow buf. (May 14299 * be NULL if none.) 14300 * 14301 * Return Code: Pointer to allocates buf(9S) struct 14302 * 14303 * Context: Can sleep. 14304 */ 14305 14306 static struct buf * 14307 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14308 daddr_t blkno, int (*func)(struct buf *)) 14309 { 14310 struct sd_lun *un; 14311 struct sd_xbuf *xp; 14312 struct sd_xbuf *new_xp; 14313 struct buf *new_bp; 14314 14315 ASSERT(bp != NULL); 14316 xp = SD_GET_XBUF(bp); 14317 ASSERT(xp != NULL); 14318 un = SD_GET_UN(bp); 14319 ASSERT(un != NULL); 14320 ASSERT(!mutex_owned(SD_MUTEX(un))); 14321 14322 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14323 NULL, KM_SLEEP); 14324 14325 new_bp->b_lblkno = blkno; 14326 14327 /* 14328 * Allocate an xbuf for the shadow bp and copy the contents of the 14329 * original xbuf into it. 14330 */ 14331 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14332 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14333 14334 /* 14335 * The given bp is automatically saved in the xb_private member 14336 * of the new xbuf. Callers are allowed to depend on this. 14337 */ 14338 new_xp->xb_private = bp; 14339 14340 new_bp->b_private = new_xp; 14341 14342 return (new_bp); 14343 } 14344 14345 /* 14346 * Function: sd_shadow_buf_alloc 14347 * 14348 * Description: Allocate a buf(9S) and init it as per the given buf 14349 * and the various arguments. The associated sd_xbuf 14350 * struct is (nearly) duplicated. The struct buf *bp 14351 * argument is saved in new_xp->xb_private. 14352 * 14353 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14354 * datalen - size of data area for the shadow bp 14355 * bflags - B_READ or B_WRITE (pseudo flag) 14356 * blkno - starting LBA 14357 * func - function pointer for b_iodone in the shadow buf. (May 14358 * be NULL if none.) 14359 * 14360 * Return Code: Pointer to allocates buf(9S) struct 14361 * 14362 * Context: Can sleep. 14363 */ 14364 14365 static struct buf * 14366 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14367 daddr_t blkno, int (*func)(struct buf *)) 14368 { 14369 struct sd_lun *un; 14370 struct sd_xbuf *xp; 14371 struct sd_xbuf *new_xp; 14372 struct buf *new_bp; 14373 14374 ASSERT(bp != NULL); 14375 xp = SD_GET_XBUF(bp); 14376 ASSERT(xp != NULL); 14377 un = SD_GET_UN(bp); 14378 ASSERT(un != NULL); 14379 ASSERT(!mutex_owned(SD_MUTEX(un))); 14380 14381 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14382 bp_mapin(bp); 14383 } 14384 14385 bflags &= (B_READ | B_WRITE); 14386 #if defined(__i386) || defined(__amd64) 14387 new_bp = getrbuf(KM_SLEEP); 14388 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14389 new_bp->b_bcount = datalen; 14390 new_bp->b_flags = bflags | 14391 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14392 #else 14393 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14394 datalen, bflags, SLEEP_FUNC, NULL); 14395 #endif 14396 new_bp->av_forw = NULL; 14397 new_bp->av_back = NULL; 14398 new_bp->b_dev = bp->b_dev; 14399 new_bp->b_blkno = blkno; 14400 new_bp->b_iodone = func; 14401 new_bp->b_edev = bp->b_edev; 14402 new_bp->b_resid = 0; 14403 14404 /* We need to preserve the B_FAILFAST flag */ 14405 if (bp->b_flags & B_FAILFAST) { 14406 new_bp->b_flags |= B_FAILFAST; 14407 } 14408 14409 /* 14410 * Allocate an xbuf for the shadow bp and copy the contents of the 14411 * original xbuf into it. 14412 */ 14413 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14414 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14415 14416 /* Need later to copy data between the shadow buf & original buf! */ 14417 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14418 14419 /* 14420 * The given bp is automatically saved in the xb_private member 14421 * of the new xbuf. Callers are allowed to depend on this. 14422 */ 14423 new_xp->xb_private = bp; 14424 14425 new_bp->b_private = new_xp; 14426 14427 return (new_bp); 14428 } 14429 14430 /* 14431 * Function: sd_bioclone_free 14432 * 14433 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14434 * in the larger than partition operation. 14435 * 14436 * Context: May be called under interrupt context 14437 */ 14438 14439 static void 14440 sd_bioclone_free(struct buf *bp) 14441 { 14442 struct sd_xbuf *xp; 14443 14444 ASSERT(bp != NULL); 14445 xp = SD_GET_XBUF(bp); 14446 ASSERT(xp != NULL); 14447 14448 /* 14449 * Call bp_mapout() before freeing the buf, in case a lower 14450 * layer or HBA had done a bp_mapin(). we must do this here 14451 * as we are the "originator" of the shadow buf. 14452 */ 14453 bp_mapout(bp); 14454 14455 /* 14456 * Null out b_iodone before freeing the bp, to ensure that the driver 14457 * never gets confused by a stale value in this field. (Just a little 14458 * extra defensiveness here.) 14459 */ 14460 bp->b_iodone = NULL; 14461 14462 freerbuf(bp); 14463 14464 kmem_free(xp, sizeof (struct sd_xbuf)); 14465 } 14466 14467 /* 14468 * Function: sd_shadow_buf_free 14469 * 14470 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14471 * 14472 * Context: May be called under interrupt context 14473 */ 14474 14475 static void 14476 sd_shadow_buf_free(struct buf *bp) 14477 { 14478 struct sd_xbuf *xp; 14479 14480 ASSERT(bp != NULL); 14481 xp = SD_GET_XBUF(bp); 14482 ASSERT(xp != NULL); 14483 14484 #if defined(__sparc) 14485 /* 14486 * Call bp_mapout() before freeing the buf, in case a lower 14487 * layer or HBA had done a bp_mapin(). we must do this here 14488 * as we are the "originator" of the shadow buf. 14489 */ 14490 bp_mapout(bp); 14491 #endif 14492 14493 /* 14494 * Null out b_iodone before freeing the bp, to ensure that the driver 14495 * never gets confused by a stale value in this field. (Just a little 14496 * extra defensiveness here.) 14497 */ 14498 bp->b_iodone = NULL; 14499 14500 #if defined(__i386) || defined(__amd64) 14501 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14502 freerbuf(bp); 14503 #else 14504 scsi_free_consistent_buf(bp); 14505 #endif 14506 14507 kmem_free(xp, sizeof (struct sd_xbuf)); 14508 } 14509 14510 14511 /* 14512 * Function: sd_print_transport_rejected_message 14513 * 14514 * Description: This implements the ludicrously complex rules for printing 14515 * a "transport rejected" message. This is to address the 14516 * specific problem of having a flood of this error message 14517 * produced when a failover occurs. 14518 * 14519 * Context: Any. 14520 */ 14521 14522 static void 14523 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14524 int code) 14525 { 14526 ASSERT(un != NULL); 14527 ASSERT(mutex_owned(SD_MUTEX(un))); 14528 ASSERT(xp != NULL); 14529 14530 /* 14531 * Print the "transport rejected" message under the following 14532 * conditions: 14533 * 14534 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14535 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14536 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14537 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14538 * scsi_transport(9F) (which indicates that the target might have 14539 * gone off-line). This uses the un->un_tran_fatal_count 14540 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14541 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14542 * from scsi_transport(). 14543 * 14544 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14545 * the preceeding cases in order for the message to be printed. 14546 */ 14547 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) && 14548 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) { 14549 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14550 (code != TRAN_FATAL_ERROR) || 14551 (un->un_tran_fatal_count == 1)) { 14552 switch (code) { 14553 case TRAN_BADPKT: 14554 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14555 "transport rejected bad packet\n"); 14556 break; 14557 case TRAN_FATAL_ERROR: 14558 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14559 "transport rejected fatal error\n"); 14560 break; 14561 default: 14562 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14563 "transport rejected (%d)\n", code); 14564 break; 14565 } 14566 } 14567 } 14568 } 14569 14570 14571 /* 14572 * Function: sd_add_buf_to_waitq 14573 * 14574 * Description: Add the given buf(9S) struct to the wait queue for the 14575 * instance. If sorting is enabled, then the buf is added 14576 * to the queue via an elevator sort algorithm (a la 14577 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14578 * If sorting is not enabled, then the buf is just added 14579 * to the end of the wait queue. 14580 * 14581 * Return Code: void 14582 * 14583 * Context: Does not sleep/block, therefore technically can be called 14584 * from any context. However if sorting is enabled then the 14585 * execution time is indeterminate, and may take long if 14586 * the wait queue grows large. 14587 */ 14588 14589 static void 14590 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14591 { 14592 struct buf *ap; 14593 14594 ASSERT(bp != NULL); 14595 ASSERT(un != NULL); 14596 ASSERT(mutex_owned(SD_MUTEX(un))); 14597 14598 /* If the queue is empty, add the buf as the only entry & return. */ 14599 if (un->un_waitq_headp == NULL) { 14600 ASSERT(un->un_waitq_tailp == NULL); 14601 un->un_waitq_headp = un->un_waitq_tailp = bp; 14602 bp->av_forw = NULL; 14603 return; 14604 } 14605 14606 ASSERT(un->un_waitq_tailp != NULL); 14607 14608 /* 14609 * If sorting is disabled, just add the buf to the tail end of 14610 * the wait queue and return. 14611 */ 14612 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) { 14613 un->un_waitq_tailp->av_forw = bp; 14614 un->un_waitq_tailp = bp; 14615 bp->av_forw = NULL; 14616 return; 14617 } 14618 14619 /* 14620 * Sort thru the list of requests currently on the wait queue 14621 * and add the new buf request at the appropriate position. 14622 * 14623 * The un->un_waitq_headp is an activity chain pointer on which 14624 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14625 * first queue holds those requests which are positioned after 14626 * the current SD_GET_BLKNO() (in the first request); the second holds 14627 * requests which came in after their SD_GET_BLKNO() number was passed. 14628 * Thus we implement a one way scan, retracting after reaching 14629 * the end of the drive to the first request on the second 14630 * queue, at which time it becomes the first queue. 14631 * A one-way scan is natural because of the way UNIX read-ahead 14632 * blocks are allocated. 14633 * 14634 * If we lie after the first request, then we must locate the 14635 * second request list and add ourselves to it. 14636 */ 14637 ap = un->un_waitq_headp; 14638 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14639 while (ap->av_forw != NULL) { 14640 /* 14641 * Look for an "inversion" in the (normally 14642 * ascending) block numbers. This indicates 14643 * the start of the second request list. 14644 */ 14645 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14646 /* 14647 * Search the second request list for the 14648 * first request at a larger block number. 14649 * We go before that; however if there is 14650 * no such request, we go at the end. 14651 */ 14652 do { 14653 if (SD_GET_BLKNO(bp) < 14654 SD_GET_BLKNO(ap->av_forw)) { 14655 goto insert; 14656 } 14657 ap = ap->av_forw; 14658 } while (ap->av_forw != NULL); 14659 goto insert; /* after last */ 14660 } 14661 ap = ap->av_forw; 14662 } 14663 14664 /* 14665 * No inversions... we will go after the last, and 14666 * be the first request in the second request list. 14667 */ 14668 goto insert; 14669 } 14670 14671 /* 14672 * Request is at/after the current request... 14673 * sort in the first request list. 14674 */ 14675 while (ap->av_forw != NULL) { 14676 /* 14677 * We want to go after the current request (1) if 14678 * there is an inversion after it (i.e. it is the end 14679 * of the first request list), or (2) if the next 14680 * request is a larger block no. than our request. 14681 */ 14682 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14683 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14684 goto insert; 14685 } 14686 ap = ap->av_forw; 14687 } 14688 14689 /* 14690 * Neither a second list nor a larger request, therefore 14691 * we go at the end of the first list (which is the same 14692 * as the end of the whole schebang). 14693 */ 14694 insert: 14695 bp->av_forw = ap->av_forw; 14696 ap->av_forw = bp; 14697 14698 /* 14699 * If we inserted onto the tail end of the waitq, make sure the 14700 * tail pointer is updated. 14701 */ 14702 if (ap == un->un_waitq_tailp) { 14703 un->un_waitq_tailp = bp; 14704 } 14705 } 14706 14707 14708 /* 14709 * Function: sd_start_cmds 14710 * 14711 * Description: Remove and transport cmds from the driver queues. 14712 * 14713 * Arguments: un - pointer to the unit (soft state) struct for the target. 14714 * 14715 * immed_bp - ptr to a buf to be transported immediately. Only 14716 * the immed_bp is transported; bufs on the waitq are not 14717 * processed and the un_retry_bp is not checked. If immed_bp is 14718 * NULL, then normal queue processing is performed. 14719 * 14720 * Context: May be called from kernel thread context, interrupt context, 14721 * or runout callback context. This function may not block or 14722 * call routines that block. 14723 */ 14724 14725 static void 14726 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14727 { 14728 struct sd_xbuf *xp; 14729 struct buf *bp; 14730 void (*statp)(kstat_io_t *); 14731 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14732 void (*saved_statp)(kstat_io_t *); 14733 #endif 14734 int rval; 14735 struct sd_fm_internal *sfip = NULL; 14736 14737 ASSERT(un != NULL); 14738 ASSERT(mutex_owned(SD_MUTEX(un))); 14739 ASSERT(un->un_ncmds_in_transport >= 0); 14740 ASSERT(un->un_throttle >= 0); 14741 14742 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14743 14744 do { 14745 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14746 saved_statp = NULL; 14747 #endif 14748 14749 /* 14750 * If we are syncing or dumping, fail the command to 14751 * avoid recursively calling back into scsi_transport(). 14752 * The dump I/O itself uses a separate code path so this 14753 * only prevents non-dump I/O from being sent while dumping. 14754 * File system sync takes place before dumping begins. 14755 * During panic, filesystem I/O is allowed provided 14756 * un_in_callback is <= 1. This is to prevent recursion 14757 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14758 * sd_start_cmds and so on. See panic.c for more information 14759 * about the states the system can be in during panic. 14760 */ 14761 if ((un->un_state == SD_STATE_DUMPING) || 14762 (ddi_in_panic() && (un->un_in_callback > 1))) { 14763 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14764 "sd_start_cmds: panicking\n"); 14765 goto exit; 14766 } 14767 14768 if ((bp = immed_bp) != NULL) { 14769 /* 14770 * We have a bp that must be transported immediately. 14771 * It's OK to transport the immed_bp here without doing 14772 * the throttle limit check because the immed_bp is 14773 * always used in a retry/recovery case. This means 14774 * that we know we are not at the throttle limit by 14775 * virtue of the fact that to get here we must have 14776 * already gotten a command back via sdintr(). This also 14777 * relies on (1) the command on un_retry_bp preventing 14778 * further commands from the waitq from being issued; 14779 * and (2) the code in sd_retry_command checking the 14780 * throttle limit before issuing a delayed or immediate 14781 * retry. This holds even if the throttle limit is 14782 * currently ratcheted down from its maximum value. 14783 */ 14784 statp = kstat_runq_enter; 14785 if (bp == un->un_retry_bp) { 14786 ASSERT((un->un_retry_statp == NULL) || 14787 (un->un_retry_statp == kstat_waitq_enter) || 14788 (un->un_retry_statp == 14789 kstat_runq_back_to_waitq)); 14790 /* 14791 * If the waitq kstat was incremented when 14792 * sd_set_retry_bp() queued this bp for a retry, 14793 * then we must set up statp so that the waitq 14794 * count will get decremented correctly below. 14795 * Also we must clear un->un_retry_statp to 14796 * ensure that we do not act on a stale value 14797 * in this field. 14798 */ 14799 if ((un->un_retry_statp == kstat_waitq_enter) || 14800 (un->un_retry_statp == 14801 kstat_runq_back_to_waitq)) { 14802 statp = kstat_waitq_to_runq; 14803 } 14804 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14805 saved_statp = un->un_retry_statp; 14806 #endif 14807 un->un_retry_statp = NULL; 14808 14809 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14810 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14811 "un_throttle:%d un_ncmds_in_transport:%d\n", 14812 un, un->un_retry_bp, un->un_throttle, 14813 un->un_ncmds_in_transport); 14814 } else { 14815 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14816 "processing priority bp:0x%p\n", bp); 14817 } 14818 14819 } else if ((bp = un->un_waitq_headp) != NULL) { 14820 /* 14821 * A command on the waitq is ready to go, but do not 14822 * send it if: 14823 * 14824 * (1) the throttle limit has been reached, or 14825 * (2) a retry is pending, or 14826 * (3) a START_STOP_UNIT callback pending, or 14827 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14828 * command is pending. 14829 * 14830 * For all of these conditions, IO processing will 14831 * restart after the condition is cleared. 14832 */ 14833 if (un->un_ncmds_in_transport >= un->un_throttle) { 14834 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14835 "sd_start_cmds: exiting, " 14836 "throttle limit reached!\n"); 14837 goto exit; 14838 } 14839 if (un->un_retry_bp != NULL) { 14840 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14841 "sd_start_cmds: exiting, retry pending!\n"); 14842 goto exit; 14843 } 14844 if (un->un_startstop_timeid != NULL) { 14845 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14846 "sd_start_cmds: exiting, " 14847 "START_STOP pending!\n"); 14848 goto exit; 14849 } 14850 if (un->un_direct_priority_timeid != NULL) { 14851 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14852 "sd_start_cmds: exiting, " 14853 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14854 goto exit; 14855 } 14856 14857 /* Dequeue the command */ 14858 un->un_waitq_headp = bp->av_forw; 14859 if (un->un_waitq_headp == NULL) { 14860 un->un_waitq_tailp = NULL; 14861 } 14862 bp->av_forw = NULL; 14863 statp = kstat_waitq_to_runq; 14864 SD_TRACE(SD_LOG_IO_CORE, un, 14865 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14866 14867 } else { 14868 /* No work to do so bail out now */ 14869 SD_TRACE(SD_LOG_IO_CORE, un, 14870 "sd_start_cmds: no more work, exiting!\n"); 14871 goto exit; 14872 } 14873 14874 /* 14875 * Reset the state to normal. This is the mechanism by which 14876 * the state transitions from either SD_STATE_RWAIT or 14877 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14878 * If state is SD_STATE_PM_CHANGING then this command is 14879 * part of the device power control and the state must 14880 * not be put back to normal. Doing so would would 14881 * allow new commands to proceed when they shouldn't, 14882 * the device may be going off. 14883 */ 14884 if ((un->un_state != SD_STATE_SUSPENDED) && 14885 (un->un_state != SD_STATE_PM_CHANGING)) { 14886 New_state(un, SD_STATE_NORMAL); 14887 } 14888 14889 xp = SD_GET_XBUF(bp); 14890 ASSERT(xp != NULL); 14891 14892 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14893 /* 14894 * Allocate the scsi_pkt if we need one, or attach DMA 14895 * resources if we have a scsi_pkt that needs them. The 14896 * latter should only occur for commands that are being 14897 * retried. 14898 */ 14899 if ((xp->xb_pktp == NULL) || 14900 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14901 #else 14902 if (xp->xb_pktp == NULL) { 14903 #endif 14904 /* 14905 * There is no scsi_pkt allocated for this buf. Call 14906 * the initpkt function to allocate & init one. 14907 * 14908 * The scsi_init_pkt runout callback functionality is 14909 * implemented as follows: 14910 * 14911 * 1) The initpkt function always calls 14912 * scsi_init_pkt(9F) with sdrunout specified as the 14913 * callback routine. 14914 * 2) A successful packet allocation is initialized and 14915 * the I/O is transported. 14916 * 3) The I/O associated with an allocation resource 14917 * failure is left on its queue to be retried via 14918 * runout or the next I/O. 14919 * 4) The I/O associated with a DMA error is removed 14920 * from the queue and failed with EIO. Processing of 14921 * the transport queues is also halted to be 14922 * restarted via runout or the next I/O. 14923 * 5) The I/O associated with a CDB size or packet 14924 * size error is removed from the queue and failed 14925 * with EIO. Processing of the transport queues is 14926 * continued. 14927 * 14928 * Note: there is no interface for canceling a runout 14929 * callback. To prevent the driver from detaching or 14930 * suspending while a runout is pending the driver 14931 * state is set to SD_STATE_RWAIT 14932 * 14933 * Note: using the scsi_init_pkt callback facility can 14934 * result in an I/O request persisting at the head of 14935 * the list which cannot be satisfied even after 14936 * multiple retries. In the future the driver may 14937 * implement some kind of maximum runout count before 14938 * failing an I/O. 14939 * 14940 * Note: the use of funcp below may seem superfluous, 14941 * but it helps warlock figure out the correct 14942 * initpkt function calls (see [s]sd.wlcmd). 14943 */ 14944 struct scsi_pkt *pktp; 14945 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14946 14947 ASSERT(bp != un->un_rqs_bp); 14948 14949 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14950 switch ((*funcp)(bp, &pktp)) { 14951 case SD_PKT_ALLOC_SUCCESS: 14952 xp->xb_pktp = pktp; 14953 SD_TRACE(SD_LOG_IO_CORE, un, 14954 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14955 pktp); 14956 goto got_pkt; 14957 14958 case SD_PKT_ALLOC_FAILURE: 14959 /* 14960 * Temporary (hopefully) resource depletion. 14961 * Since retries and RQS commands always have a 14962 * scsi_pkt allocated, these cases should never 14963 * get here. So the only cases this needs to 14964 * handle is a bp from the waitq (which we put 14965 * back onto the waitq for sdrunout), or a bp 14966 * sent as an immed_bp (which we just fail). 14967 */ 14968 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14969 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14970 14971 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14972 14973 if (bp == immed_bp) { 14974 /* 14975 * If SD_XB_DMA_FREED is clear, then 14976 * this is a failure to allocate a 14977 * scsi_pkt, and we must fail the 14978 * command. 14979 */ 14980 if ((xp->xb_pkt_flags & 14981 SD_XB_DMA_FREED) == 0) { 14982 break; 14983 } 14984 14985 /* 14986 * If this immediate command is NOT our 14987 * un_retry_bp, then we must fail it. 14988 */ 14989 if (bp != un->un_retry_bp) { 14990 break; 14991 } 14992 14993 /* 14994 * We get here if this cmd is our 14995 * un_retry_bp that was DMAFREED, but 14996 * scsi_init_pkt() failed to reallocate 14997 * DMA resources when we attempted to 14998 * retry it. This can happen when an 14999 * mpxio failover is in progress, but 15000 * we don't want to just fail the 15001 * command in this case. 15002 * 15003 * Use timeout(9F) to restart it after 15004 * a 100ms delay. We don't want to 15005 * let sdrunout() restart it, because 15006 * sdrunout() is just supposed to start 15007 * commands that are sitting on the 15008 * wait queue. The un_retry_bp stays 15009 * set until the command completes, but 15010 * sdrunout can be called many times 15011 * before that happens. Since sdrunout 15012 * cannot tell if the un_retry_bp is 15013 * already in the transport, it could 15014 * end up calling scsi_transport() for 15015 * the un_retry_bp multiple times. 15016 * 15017 * Also: don't schedule the callback 15018 * if some other callback is already 15019 * pending. 15020 */ 15021 if (un->un_retry_statp == NULL) { 15022 /* 15023 * restore the kstat pointer to 15024 * keep kstat counts coherent 15025 * when we do retry the command. 15026 */ 15027 un->un_retry_statp = 15028 saved_statp; 15029 } 15030 15031 if ((un->un_startstop_timeid == NULL) && 15032 (un->un_retry_timeid == NULL) && 15033 (un->un_direct_priority_timeid == 15034 NULL)) { 15035 15036 un->un_retry_timeid = 15037 timeout( 15038 sd_start_retry_command, 15039 un, SD_RESTART_TIMEOUT); 15040 } 15041 goto exit; 15042 } 15043 15044 #else 15045 if (bp == immed_bp) { 15046 break; /* Just fail the command */ 15047 } 15048 #endif 15049 15050 /* Add the buf back to the head of the waitq */ 15051 bp->av_forw = un->un_waitq_headp; 15052 un->un_waitq_headp = bp; 15053 if (un->un_waitq_tailp == NULL) { 15054 un->un_waitq_tailp = bp; 15055 } 15056 goto exit; 15057 15058 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15059 /* 15060 * HBA DMA resource failure. Fail the command 15061 * and continue processing of the queues. 15062 */ 15063 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15064 "sd_start_cmds: " 15065 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15066 break; 15067 15068 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15069 /* 15070 * Note:x86: Partial DMA mapping not supported 15071 * for USCSI commands, and all the needed DMA 15072 * resources were not allocated. 15073 */ 15074 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15075 "sd_start_cmds: " 15076 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15077 break; 15078 15079 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15080 /* 15081 * Note:x86: Request cannot fit into CDB based 15082 * on lba and len. 15083 */ 15084 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15085 "sd_start_cmds: " 15086 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15087 break; 15088 15089 default: 15090 /* Should NEVER get here! */ 15091 panic("scsi_initpkt error"); 15092 /*NOTREACHED*/ 15093 } 15094 15095 /* 15096 * Fatal error in allocating a scsi_pkt for this buf. 15097 * Update kstats & return the buf with an error code. 15098 * We must use sd_return_failed_command_no_restart() to 15099 * avoid a recursive call back into sd_start_cmds(). 15100 * However this also means that we must keep processing 15101 * the waitq here in order to avoid stalling. 15102 */ 15103 if (statp == kstat_waitq_to_runq) { 15104 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15105 } 15106 sd_return_failed_command_no_restart(un, bp, EIO); 15107 if (bp == immed_bp) { 15108 /* immed_bp is gone by now, so clear this */ 15109 immed_bp = NULL; 15110 } 15111 continue; 15112 } 15113 got_pkt: 15114 if (bp == immed_bp) { 15115 /* goto the head of the class.... */ 15116 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15117 } 15118 15119 un->un_ncmds_in_transport++; 15120 SD_UPDATE_KSTATS(un, statp, bp); 15121 15122 /* 15123 * Call scsi_transport() to send the command to the target. 15124 * According to SCSA architecture, we must drop the mutex here 15125 * before calling scsi_transport() in order to avoid deadlock. 15126 * Note that the scsi_pkt's completion routine can be executed 15127 * (from interrupt context) even before the call to 15128 * scsi_transport() returns. 15129 */ 15130 SD_TRACE(SD_LOG_IO_CORE, un, 15131 "sd_start_cmds: calling scsi_transport()\n"); 15132 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15133 15134 mutex_exit(SD_MUTEX(un)); 15135 rval = scsi_transport(xp->xb_pktp); 15136 mutex_enter(SD_MUTEX(un)); 15137 15138 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15139 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15140 15141 switch (rval) { 15142 case TRAN_ACCEPT: 15143 /* Clear this with every pkt accepted by the HBA */ 15144 un->un_tran_fatal_count = 0; 15145 break; /* Success; try the next cmd (if any) */ 15146 15147 case TRAN_BUSY: 15148 un->un_ncmds_in_transport--; 15149 ASSERT(un->un_ncmds_in_transport >= 0); 15150 15151 /* 15152 * Don't retry request sense, the sense data 15153 * is lost when another request is sent. 15154 * Free up the rqs buf and retry 15155 * the original failed cmd. Update kstat. 15156 */ 15157 if (bp == un->un_rqs_bp) { 15158 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15159 bp = sd_mark_rqs_idle(un, xp); 15160 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15161 NULL, NULL, EIO, un->un_busy_timeout / 500, 15162 kstat_waitq_enter); 15163 goto exit; 15164 } 15165 15166 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15167 /* 15168 * Free the DMA resources for the scsi_pkt. This will 15169 * allow mpxio to select another path the next time 15170 * we call scsi_transport() with this scsi_pkt. 15171 * See sdintr() for the rationalization behind this. 15172 */ 15173 if ((un->un_f_is_fibre == TRUE) && 15174 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15175 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15176 scsi_dmafree(xp->xb_pktp); 15177 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15178 } 15179 #endif 15180 15181 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15182 /* 15183 * Commands that are SD_PATH_DIRECT_PRIORITY 15184 * are for error recovery situations. These do 15185 * not use the normal command waitq, so if they 15186 * get a TRAN_BUSY we cannot put them back onto 15187 * the waitq for later retry. One possible 15188 * problem is that there could already be some 15189 * other command on un_retry_bp that is waiting 15190 * for this one to complete, so we would be 15191 * deadlocked if we put this command back onto 15192 * the waitq for later retry (since un_retry_bp 15193 * must complete before the driver gets back to 15194 * commands on the waitq). 15195 * 15196 * To avoid deadlock we must schedule a callback 15197 * that will restart this command after a set 15198 * interval. This should keep retrying for as 15199 * long as the underlying transport keeps 15200 * returning TRAN_BUSY (just like for other 15201 * commands). Use the same timeout interval as 15202 * for the ordinary TRAN_BUSY retry. 15203 */ 15204 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15205 "sd_start_cmds: scsi_transport() returned " 15206 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15207 15208 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15209 un->un_direct_priority_timeid = 15210 timeout(sd_start_direct_priority_command, 15211 bp, un->un_busy_timeout / 500); 15212 15213 goto exit; 15214 } 15215 15216 /* 15217 * For TRAN_BUSY, we want to reduce the throttle value, 15218 * unless we are retrying a command. 15219 */ 15220 if (bp != un->un_retry_bp) { 15221 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15222 } 15223 15224 /* 15225 * Set up the bp to be tried again 10 ms later. 15226 * Note:x86: Is there a timeout value in the sd_lun 15227 * for this condition? 15228 */ 15229 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500, 15230 kstat_runq_back_to_waitq); 15231 goto exit; 15232 15233 case TRAN_FATAL_ERROR: 15234 un->un_tran_fatal_count++; 15235 /* FALLTHRU */ 15236 15237 case TRAN_BADPKT: 15238 default: 15239 un->un_ncmds_in_transport--; 15240 ASSERT(un->un_ncmds_in_transport >= 0); 15241 15242 /* 15243 * If this is our REQUEST SENSE command with a 15244 * transport error, we must get back the pointers 15245 * to the original buf, and mark the REQUEST 15246 * SENSE command as "available". 15247 */ 15248 if (bp == un->un_rqs_bp) { 15249 bp = sd_mark_rqs_idle(un, xp); 15250 xp = SD_GET_XBUF(bp); 15251 } else { 15252 /* 15253 * Legacy behavior: do not update transport 15254 * error count for request sense commands. 15255 */ 15256 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15257 } 15258 15259 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15260 sd_print_transport_rejected_message(un, xp, rval); 15261 15262 /* 15263 * This command will be terminated by SD driver due 15264 * to a fatal transport error. We should post 15265 * ereport.io.scsi.cmd.disk.tran with driver-assessment 15266 * of "fail" for any command to indicate this 15267 * situation. 15268 */ 15269 if (xp->xb_ena > 0) { 15270 ASSERT(un->un_fm_private != NULL); 15271 sfip = un->un_fm_private; 15272 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT; 15273 sd_ssc_extract_info(&sfip->fm_ssc, un, 15274 xp->xb_pktp, bp, xp); 15275 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15276 } 15277 15278 /* 15279 * We must use sd_return_failed_command_no_restart() to 15280 * avoid a recursive call back into sd_start_cmds(). 15281 * However this also means that we must keep processing 15282 * the waitq here in order to avoid stalling. 15283 */ 15284 sd_return_failed_command_no_restart(un, bp, EIO); 15285 15286 /* 15287 * Notify any threads waiting in sd_ddi_suspend() that 15288 * a command completion has occurred. 15289 */ 15290 if (un->un_state == SD_STATE_SUSPENDED) { 15291 cv_broadcast(&un->un_disk_busy_cv); 15292 } 15293 15294 if (bp == immed_bp) { 15295 /* immed_bp is gone by now, so clear this */ 15296 immed_bp = NULL; 15297 } 15298 break; 15299 } 15300 15301 } while (immed_bp == NULL); 15302 15303 exit: 15304 ASSERT(mutex_owned(SD_MUTEX(un))); 15305 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15306 } 15307 15308 15309 /* 15310 * Function: sd_return_command 15311 * 15312 * Description: Returns a command to its originator (with or without an 15313 * error). Also starts commands waiting to be transported 15314 * to the target. 15315 * 15316 * Context: May be called from interrupt, kernel, or timeout context 15317 */ 15318 15319 static void 15320 sd_return_command(struct sd_lun *un, struct buf *bp) 15321 { 15322 struct sd_xbuf *xp; 15323 struct scsi_pkt *pktp; 15324 struct sd_fm_internal *sfip; 15325 15326 ASSERT(bp != NULL); 15327 ASSERT(un != NULL); 15328 ASSERT(mutex_owned(SD_MUTEX(un))); 15329 ASSERT(bp != un->un_rqs_bp); 15330 xp = SD_GET_XBUF(bp); 15331 ASSERT(xp != NULL); 15332 15333 pktp = SD_GET_PKTP(bp); 15334 sfip = (struct sd_fm_internal *)un->un_fm_private; 15335 ASSERT(sfip != NULL); 15336 15337 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15338 15339 /* 15340 * Note: check for the "sdrestart failed" case. 15341 */ 15342 if ((un->un_partial_dma_supported == 1) && 15343 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15344 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15345 (xp->xb_pktp->pkt_resid == 0)) { 15346 15347 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15348 /* 15349 * Successfully set up next portion of cmd 15350 * transfer, try sending it 15351 */ 15352 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15353 NULL, NULL, 0, (clock_t)0, NULL); 15354 sd_start_cmds(un, NULL); 15355 return; /* Note:x86: need a return here? */ 15356 } 15357 } 15358 15359 /* 15360 * If this is the failfast bp, clear it from un_failfast_bp. This 15361 * can happen if upon being re-tried the failfast bp either 15362 * succeeded or encountered another error (possibly even a different 15363 * error than the one that precipitated the failfast state, but in 15364 * that case it would have had to exhaust retries as well). Regardless, 15365 * this should not occur whenever the instance is in the active 15366 * failfast state. 15367 */ 15368 if (bp == un->un_failfast_bp) { 15369 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15370 un->un_failfast_bp = NULL; 15371 } 15372 15373 /* 15374 * Clear the failfast state upon successful completion of ANY cmd. 15375 */ 15376 if (bp->b_error == 0) { 15377 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15378 /* 15379 * If this is a successful command, but used to be retried, 15380 * we will take it as a recovered command and post an 15381 * ereport with driver-assessment of "recovered". 15382 */ 15383 if (xp->xb_ena > 0) { 15384 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15385 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY); 15386 } 15387 } else { 15388 /* 15389 * If this is a failed non-USCSI command we will post an 15390 * ereport with driver-assessment set accordingly("fail" or 15391 * "fatal"). 15392 */ 15393 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15394 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15395 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 15396 } 15397 } 15398 15399 /* 15400 * This is used if the command was retried one or more times. Show that 15401 * we are done with it, and allow processing of the waitq to resume. 15402 */ 15403 if (bp == un->un_retry_bp) { 15404 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15405 "sd_return_command: un:0x%p: " 15406 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15407 un->un_retry_bp = NULL; 15408 un->un_retry_statp = NULL; 15409 } 15410 15411 SD_UPDATE_RDWR_STATS(un, bp); 15412 SD_UPDATE_PARTITION_STATS(un, bp); 15413 15414 switch (un->un_state) { 15415 case SD_STATE_SUSPENDED: 15416 /* 15417 * Notify any threads waiting in sd_ddi_suspend() that 15418 * a command completion has occurred. 15419 */ 15420 cv_broadcast(&un->un_disk_busy_cv); 15421 break; 15422 default: 15423 sd_start_cmds(un, NULL); 15424 break; 15425 } 15426 15427 /* Return this command up the iodone chain to its originator. */ 15428 mutex_exit(SD_MUTEX(un)); 15429 15430 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15431 xp->xb_pktp = NULL; 15432 15433 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15434 15435 ASSERT(!mutex_owned(SD_MUTEX(un))); 15436 mutex_enter(SD_MUTEX(un)); 15437 15438 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15439 } 15440 15441 15442 /* 15443 * Function: sd_return_failed_command 15444 * 15445 * Description: Command completion when an error occurred. 15446 * 15447 * Context: May be called from interrupt context 15448 */ 15449 15450 static void 15451 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15452 { 15453 ASSERT(bp != NULL); 15454 ASSERT(un != NULL); 15455 ASSERT(mutex_owned(SD_MUTEX(un))); 15456 15457 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15458 "sd_return_failed_command: entry\n"); 15459 15460 /* 15461 * b_resid could already be nonzero due to a partial data 15462 * transfer, so do not change it here. 15463 */ 15464 SD_BIOERROR(bp, errcode); 15465 15466 sd_return_command(un, bp); 15467 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15468 "sd_return_failed_command: exit\n"); 15469 } 15470 15471 15472 /* 15473 * Function: sd_return_failed_command_no_restart 15474 * 15475 * Description: Same as sd_return_failed_command, but ensures that no 15476 * call back into sd_start_cmds will be issued. 15477 * 15478 * Context: May be called from interrupt context 15479 */ 15480 15481 static void 15482 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15483 int errcode) 15484 { 15485 struct sd_xbuf *xp; 15486 15487 ASSERT(bp != NULL); 15488 ASSERT(un != NULL); 15489 ASSERT(mutex_owned(SD_MUTEX(un))); 15490 xp = SD_GET_XBUF(bp); 15491 ASSERT(xp != NULL); 15492 ASSERT(errcode != 0); 15493 15494 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15495 "sd_return_failed_command_no_restart: entry\n"); 15496 15497 /* 15498 * b_resid could already be nonzero due to a partial data 15499 * transfer, so do not change it here. 15500 */ 15501 SD_BIOERROR(bp, errcode); 15502 15503 /* 15504 * If this is the failfast bp, clear it. This can happen if the 15505 * failfast bp encounterd a fatal error when we attempted to 15506 * re-try it (such as a scsi_transport(9F) failure). However 15507 * we should NOT be in an active failfast state if the failfast 15508 * bp is not NULL. 15509 */ 15510 if (bp == un->un_failfast_bp) { 15511 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15512 un->un_failfast_bp = NULL; 15513 } 15514 15515 if (bp == un->un_retry_bp) { 15516 /* 15517 * This command was retried one or more times. Show that we are 15518 * done with it, and allow processing of the waitq to resume. 15519 */ 15520 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15521 "sd_return_failed_command_no_restart: " 15522 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15523 un->un_retry_bp = NULL; 15524 un->un_retry_statp = NULL; 15525 } 15526 15527 SD_UPDATE_RDWR_STATS(un, bp); 15528 SD_UPDATE_PARTITION_STATS(un, bp); 15529 15530 mutex_exit(SD_MUTEX(un)); 15531 15532 if (xp->xb_pktp != NULL) { 15533 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15534 xp->xb_pktp = NULL; 15535 } 15536 15537 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15538 15539 mutex_enter(SD_MUTEX(un)); 15540 15541 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15542 "sd_return_failed_command_no_restart: exit\n"); 15543 } 15544 15545 15546 /* 15547 * Function: sd_retry_command 15548 * 15549 * Description: queue up a command for retry, or (optionally) fail it 15550 * if retry counts are exhausted. 15551 * 15552 * Arguments: un - Pointer to the sd_lun struct for the target. 15553 * 15554 * bp - Pointer to the buf for the command to be retried. 15555 * 15556 * retry_check_flag - Flag to see which (if any) of the retry 15557 * counts should be decremented/checked. If the indicated 15558 * retry count is exhausted, then the command will not be 15559 * retried; it will be failed instead. This should use a 15560 * value equal to one of the following: 15561 * 15562 * SD_RETRIES_NOCHECK 15563 * SD_RESD_RETRIES_STANDARD 15564 * SD_RETRIES_VICTIM 15565 * 15566 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15567 * if the check should be made to see of FLAG_ISOLATE is set 15568 * in the pkt. If FLAG_ISOLATE is set, then the command is 15569 * not retried, it is simply failed. 15570 * 15571 * user_funcp - Ptr to function to call before dispatching the 15572 * command. May be NULL if no action needs to be performed. 15573 * (Primarily intended for printing messages.) 15574 * 15575 * user_arg - Optional argument to be passed along to 15576 * the user_funcp call. 15577 * 15578 * failure_code - errno return code to set in the bp if the 15579 * command is going to be failed. 15580 * 15581 * retry_delay - Retry delay interval in (clock_t) units. May 15582 * be zero which indicates that the retry should be retried 15583 * immediately (ie, without an intervening delay). 15584 * 15585 * statp - Ptr to kstat function to be updated if the command 15586 * is queued for a delayed retry. May be NULL if no kstat 15587 * update is desired. 15588 * 15589 * Context: May be called from interrupt context. 15590 */ 15591 15592 static void 15593 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15594 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15595 code), void *user_arg, int failure_code, clock_t retry_delay, 15596 void (*statp)(kstat_io_t *)) 15597 { 15598 struct sd_xbuf *xp; 15599 struct scsi_pkt *pktp; 15600 struct sd_fm_internal *sfip; 15601 15602 ASSERT(un != NULL); 15603 ASSERT(mutex_owned(SD_MUTEX(un))); 15604 ASSERT(bp != NULL); 15605 xp = SD_GET_XBUF(bp); 15606 ASSERT(xp != NULL); 15607 pktp = SD_GET_PKTP(bp); 15608 ASSERT(pktp != NULL); 15609 15610 sfip = (struct sd_fm_internal *)un->un_fm_private; 15611 ASSERT(sfip != NULL); 15612 15613 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15614 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15615 15616 /* 15617 * If we are syncing or dumping, fail the command to avoid 15618 * recursively calling back into scsi_transport(). 15619 */ 15620 if (ddi_in_panic()) { 15621 goto fail_command_no_log; 15622 } 15623 15624 /* 15625 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15626 * log an error and fail the command. 15627 */ 15628 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15629 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15630 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15631 sd_dump_memory(un, SD_LOG_IO, "CDB", 15632 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15633 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15634 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15635 goto fail_command; 15636 } 15637 15638 /* 15639 * If we are suspended, then put the command onto head of the 15640 * wait queue since we don't want to start more commands, and 15641 * clear the un_retry_bp. Next time when we are resumed, will 15642 * handle the command in the wait queue. 15643 */ 15644 switch (un->un_state) { 15645 case SD_STATE_SUSPENDED: 15646 case SD_STATE_DUMPING: 15647 bp->av_forw = un->un_waitq_headp; 15648 un->un_waitq_headp = bp; 15649 if (un->un_waitq_tailp == NULL) { 15650 un->un_waitq_tailp = bp; 15651 } 15652 if (bp == un->un_retry_bp) { 15653 un->un_retry_bp = NULL; 15654 un->un_retry_statp = NULL; 15655 } 15656 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15657 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15658 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15659 return; 15660 default: 15661 break; 15662 } 15663 15664 /* 15665 * If the caller wants us to check FLAG_ISOLATE, then see if that 15666 * is set; if it is then we do not want to retry the command. 15667 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15668 */ 15669 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15670 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15671 goto fail_command; 15672 } 15673 } 15674 15675 15676 /* 15677 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15678 * command timeout or a selection timeout has occurred. This means 15679 * that we were unable to establish an kind of communication with 15680 * the target, and subsequent retries and/or commands are likely 15681 * to encounter similar results and take a long time to complete. 15682 * 15683 * If this is a failfast error condition, we need to update the 15684 * failfast state, even if this bp does not have B_FAILFAST set. 15685 */ 15686 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15687 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15688 ASSERT(un->un_failfast_bp == NULL); 15689 /* 15690 * If we are already in the active failfast state, and 15691 * another failfast error condition has been detected, 15692 * then fail this command if it has B_FAILFAST set. 15693 * If B_FAILFAST is clear, then maintain the legacy 15694 * behavior of retrying heroically, even tho this will 15695 * take a lot more time to fail the command. 15696 */ 15697 if (bp->b_flags & B_FAILFAST) { 15698 goto fail_command; 15699 } 15700 } else { 15701 /* 15702 * We're not in the active failfast state, but we 15703 * have a failfast error condition, so we must begin 15704 * transition to the next state. We do this regardless 15705 * of whether or not this bp has B_FAILFAST set. 15706 */ 15707 if (un->un_failfast_bp == NULL) { 15708 /* 15709 * This is the first bp to meet a failfast 15710 * condition so save it on un_failfast_bp & 15711 * do normal retry processing. Do not enter 15712 * active failfast state yet. This marks 15713 * entry into the "failfast pending" state. 15714 */ 15715 un->un_failfast_bp = bp; 15716 15717 } else if (un->un_failfast_bp == bp) { 15718 /* 15719 * This is the second time *this* bp has 15720 * encountered a failfast error condition, 15721 * so enter active failfast state & flush 15722 * queues as appropriate. 15723 */ 15724 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15725 un->un_failfast_bp = NULL; 15726 sd_failfast_flushq(un); 15727 15728 /* 15729 * Fail this bp now if B_FAILFAST set; 15730 * otherwise continue with retries. (It would 15731 * be pretty ironic if this bp succeeded on a 15732 * subsequent retry after we just flushed all 15733 * the queues). 15734 */ 15735 if (bp->b_flags & B_FAILFAST) { 15736 goto fail_command; 15737 } 15738 15739 #if !defined(lint) && !defined(__lint) 15740 } else { 15741 /* 15742 * If neither of the preceeding conditionals 15743 * was true, it means that there is some 15744 * *other* bp that has met an inital failfast 15745 * condition and is currently either being 15746 * retried or is waiting to be retried. In 15747 * that case we should perform normal retry 15748 * processing on *this* bp, since there is a 15749 * chance that the current failfast condition 15750 * is transient and recoverable. If that does 15751 * not turn out to be the case, then retries 15752 * will be cleared when the wait queue is 15753 * flushed anyway. 15754 */ 15755 #endif 15756 } 15757 } 15758 } else { 15759 /* 15760 * SD_RETRIES_FAILFAST is clear, which indicates that we 15761 * likely were able to at least establish some level of 15762 * communication with the target and subsequent commands 15763 * and/or retries are likely to get through to the target, 15764 * In this case we want to be aggressive about clearing 15765 * the failfast state. Note that this does not affect 15766 * the "failfast pending" condition. 15767 */ 15768 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15769 } 15770 15771 15772 /* 15773 * Check the specified retry count to see if we can still do 15774 * any retries with this pkt before we should fail it. 15775 */ 15776 switch (retry_check_flag & SD_RETRIES_MASK) { 15777 case SD_RETRIES_VICTIM: 15778 /* 15779 * Check the victim retry count. If exhausted, then fall 15780 * thru & check against the standard retry count. 15781 */ 15782 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15783 /* Increment count & proceed with the retry */ 15784 xp->xb_victim_retry_count++; 15785 break; 15786 } 15787 /* Victim retries exhausted, fall back to std. retries... */ 15788 /* FALLTHRU */ 15789 15790 case SD_RETRIES_STANDARD: 15791 if (xp->xb_retry_count >= un->un_retry_count) { 15792 /* Retries exhausted, fail the command */ 15793 SD_TRACE(SD_LOG_IO_CORE, un, 15794 "sd_retry_command: retries exhausted!\n"); 15795 /* 15796 * update b_resid for failed SCMD_READ & SCMD_WRITE 15797 * commands with nonzero pkt_resid. 15798 */ 15799 if ((pktp->pkt_reason == CMD_CMPLT) && 15800 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15801 (pktp->pkt_resid != 0)) { 15802 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15803 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15804 SD_UPDATE_B_RESID(bp, pktp); 15805 } 15806 } 15807 goto fail_command; 15808 } 15809 xp->xb_retry_count++; 15810 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15811 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15812 break; 15813 15814 case SD_RETRIES_UA: 15815 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15816 /* Retries exhausted, fail the command */ 15817 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15818 "Unit Attention retries exhausted. " 15819 "Check the target.\n"); 15820 goto fail_command; 15821 } 15822 xp->xb_ua_retry_count++; 15823 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15824 "sd_retry_command: retry count:%d\n", 15825 xp->xb_ua_retry_count); 15826 break; 15827 15828 case SD_RETRIES_BUSY: 15829 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15830 /* Retries exhausted, fail the command */ 15831 SD_TRACE(SD_LOG_IO_CORE, un, 15832 "sd_retry_command: retries exhausted!\n"); 15833 goto fail_command; 15834 } 15835 xp->xb_retry_count++; 15836 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15837 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15838 break; 15839 15840 case SD_RETRIES_NOCHECK: 15841 default: 15842 /* No retry count to check. Just proceed with the retry */ 15843 break; 15844 } 15845 15846 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15847 15848 /* 15849 * If this is a non-USCSI command being retried 15850 * during execution last time, we should post an ereport with 15851 * driver-assessment of the value "retry". 15852 * For partial DMA, request sense and STATUS_QFULL, there are no 15853 * hardware errors, we bypass ereport posting. 15854 */ 15855 if (failure_code != 0) { 15856 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15857 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15858 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY); 15859 } 15860 } 15861 15862 /* 15863 * If we were given a zero timeout, we must attempt to retry the 15864 * command immediately (ie, without a delay). 15865 */ 15866 if (retry_delay == 0) { 15867 /* 15868 * Check some limiting conditions to see if we can actually 15869 * do the immediate retry. If we cannot, then we must 15870 * fall back to queueing up a delayed retry. 15871 */ 15872 if (un->un_ncmds_in_transport >= un->un_throttle) { 15873 /* 15874 * We are at the throttle limit for the target, 15875 * fall back to delayed retry. 15876 */ 15877 retry_delay = un->un_busy_timeout; 15878 statp = kstat_waitq_enter; 15879 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15880 "sd_retry_command: immed. retry hit " 15881 "throttle!\n"); 15882 } else { 15883 /* 15884 * We're clear to proceed with the immediate retry. 15885 * First call the user-provided function (if any) 15886 */ 15887 if (user_funcp != NULL) { 15888 (*user_funcp)(un, bp, user_arg, 15889 SD_IMMEDIATE_RETRY_ISSUED); 15890 #ifdef __lock_lint 15891 sd_print_incomplete_msg(un, bp, user_arg, 15892 SD_IMMEDIATE_RETRY_ISSUED); 15893 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15894 SD_IMMEDIATE_RETRY_ISSUED); 15895 sd_print_sense_failed_msg(un, bp, user_arg, 15896 SD_IMMEDIATE_RETRY_ISSUED); 15897 #endif 15898 } 15899 15900 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15901 "sd_retry_command: issuing immediate retry\n"); 15902 15903 /* 15904 * Call sd_start_cmds() to transport the command to 15905 * the target. 15906 */ 15907 sd_start_cmds(un, bp); 15908 15909 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15910 "sd_retry_command exit\n"); 15911 return; 15912 } 15913 } 15914 15915 /* 15916 * Set up to retry the command after a delay. 15917 * First call the user-provided function (if any) 15918 */ 15919 if (user_funcp != NULL) { 15920 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15921 } 15922 15923 sd_set_retry_bp(un, bp, retry_delay, statp); 15924 15925 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15926 return; 15927 15928 fail_command: 15929 15930 if (user_funcp != NULL) { 15931 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15932 } 15933 15934 fail_command_no_log: 15935 15936 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15937 "sd_retry_command: returning failed command\n"); 15938 15939 sd_return_failed_command(un, bp, failure_code); 15940 15941 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15942 } 15943 15944 15945 /* 15946 * Function: sd_set_retry_bp 15947 * 15948 * Description: Set up the given bp for retry. 15949 * 15950 * Arguments: un - ptr to associated softstate 15951 * bp - ptr to buf(9S) for the command 15952 * retry_delay - time interval before issuing retry (may be 0) 15953 * statp - optional pointer to kstat function 15954 * 15955 * Context: May be called under interrupt context 15956 */ 15957 15958 static void 15959 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15960 void (*statp)(kstat_io_t *)) 15961 { 15962 ASSERT(un != NULL); 15963 ASSERT(mutex_owned(SD_MUTEX(un))); 15964 ASSERT(bp != NULL); 15965 15966 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15967 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15968 15969 /* 15970 * Indicate that the command is being retried. This will not allow any 15971 * other commands on the wait queue to be transported to the target 15972 * until this command has been completed (success or failure). The 15973 * "retry command" is not transported to the target until the given 15974 * time delay expires, unless the user specified a 0 retry_delay. 15975 * 15976 * Note: the timeout(9F) callback routine is what actually calls 15977 * sd_start_cmds() to transport the command, with the exception of a 15978 * zero retry_delay. The only current implementor of a zero retry delay 15979 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15980 */ 15981 if (un->un_retry_bp == NULL) { 15982 ASSERT(un->un_retry_statp == NULL); 15983 un->un_retry_bp = bp; 15984 15985 /* 15986 * If the user has not specified a delay the command should 15987 * be queued and no timeout should be scheduled. 15988 */ 15989 if (retry_delay == 0) { 15990 /* 15991 * Save the kstat pointer that will be used in the 15992 * call to SD_UPDATE_KSTATS() below, so that 15993 * sd_start_cmds() can correctly decrement the waitq 15994 * count when it is time to transport this command. 15995 */ 15996 un->un_retry_statp = statp; 15997 goto done; 15998 } 15999 } 16000 16001 if (un->un_retry_bp == bp) { 16002 /* 16003 * Save the kstat pointer that will be used in the call to 16004 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 16005 * correctly decrement the waitq count when it is time to 16006 * transport this command. 16007 */ 16008 un->un_retry_statp = statp; 16009 16010 /* 16011 * Schedule a timeout if: 16012 * 1) The user has specified a delay. 16013 * 2) There is not a START_STOP_UNIT callback pending. 16014 * 16015 * If no delay has been specified, then it is up to the caller 16016 * to ensure that IO processing continues without stalling. 16017 * Effectively, this means that the caller will issue the 16018 * required call to sd_start_cmds(). The START_STOP_UNIT 16019 * callback does this after the START STOP UNIT command has 16020 * completed. In either of these cases we should not schedule 16021 * a timeout callback here. Also don't schedule the timeout if 16022 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 16023 */ 16024 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 16025 (un->un_direct_priority_timeid == NULL)) { 16026 un->un_retry_timeid = 16027 timeout(sd_start_retry_command, un, retry_delay); 16028 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16029 "sd_set_retry_bp: setting timeout: un: 0x%p" 16030 " bp:0x%p un_retry_timeid:0x%p\n", 16031 un, bp, un->un_retry_timeid); 16032 } 16033 } else { 16034 /* 16035 * We only get in here if there is already another command 16036 * waiting to be retried. In this case, we just put the 16037 * given command onto the wait queue, so it can be transported 16038 * after the current retry command has completed. 16039 * 16040 * Also we have to make sure that if the command at the head 16041 * of the wait queue is the un_failfast_bp, that we do not 16042 * put ahead of it any other commands that are to be retried. 16043 */ 16044 if ((un->un_failfast_bp != NULL) && 16045 (un->un_failfast_bp == un->un_waitq_headp)) { 16046 /* 16047 * Enqueue this command AFTER the first command on 16048 * the wait queue (which is also un_failfast_bp). 16049 */ 16050 bp->av_forw = un->un_waitq_headp->av_forw; 16051 un->un_waitq_headp->av_forw = bp; 16052 if (un->un_waitq_headp == un->un_waitq_tailp) { 16053 un->un_waitq_tailp = bp; 16054 } 16055 } else { 16056 /* Enqueue this command at the head of the waitq. */ 16057 bp->av_forw = un->un_waitq_headp; 16058 un->un_waitq_headp = bp; 16059 if (un->un_waitq_tailp == NULL) { 16060 un->un_waitq_tailp = bp; 16061 } 16062 } 16063 16064 if (statp == NULL) { 16065 statp = kstat_waitq_enter; 16066 } 16067 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16068 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 16069 } 16070 16071 done: 16072 if (statp != NULL) { 16073 SD_UPDATE_KSTATS(un, statp, bp); 16074 } 16075 16076 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16077 "sd_set_retry_bp: exit un:0x%p\n", un); 16078 } 16079 16080 16081 /* 16082 * Function: sd_start_retry_command 16083 * 16084 * Description: Start the command that has been waiting on the target's 16085 * retry queue. Called from timeout(9F) context after the 16086 * retry delay interval has expired. 16087 * 16088 * Arguments: arg - pointer to associated softstate for the device. 16089 * 16090 * Context: timeout(9F) thread context. May not sleep. 16091 */ 16092 16093 static void 16094 sd_start_retry_command(void *arg) 16095 { 16096 struct sd_lun *un = arg; 16097 16098 ASSERT(un != NULL); 16099 ASSERT(!mutex_owned(SD_MUTEX(un))); 16100 16101 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16102 "sd_start_retry_command: entry\n"); 16103 16104 mutex_enter(SD_MUTEX(un)); 16105 16106 un->un_retry_timeid = NULL; 16107 16108 if (un->un_retry_bp != NULL) { 16109 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16110 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16111 un, un->un_retry_bp); 16112 sd_start_cmds(un, un->un_retry_bp); 16113 } 16114 16115 mutex_exit(SD_MUTEX(un)); 16116 16117 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16118 "sd_start_retry_command: exit\n"); 16119 } 16120 16121 /* 16122 * Function: sd_rmw_msg_print_handler 16123 * 16124 * Description: If RMW mode is enabled and warning message is triggered 16125 * print I/O count during a fixed interval. 16126 * 16127 * Arguments: arg - pointer to associated softstate for the device. 16128 * 16129 * Context: timeout(9F) thread context. May not sleep. 16130 */ 16131 static void 16132 sd_rmw_msg_print_handler(void *arg) 16133 { 16134 struct sd_lun *un = arg; 16135 16136 ASSERT(un != NULL); 16137 ASSERT(!mutex_owned(SD_MUTEX(un))); 16138 16139 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16140 "sd_rmw_msg_print_handler: entry\n"); 16141 16142 mutex_enter(SD_MUTEX(un)); 16143 16144 if (un->un_rmw_incre_count > 0) { 16145 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16146 "%"PRIu64" I/O requests are not aligned with %d disk " 16147 "sector size in %ld seconds. They are handled through " 16148 "Read Modify Write but the performance is very low!\n", 16149 un->un_rmw_incre_count, un->un_tgt_blocksize, 16150 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000); 16151 un->un_rmw_incre_count = 0; 16152 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler, 16153 un, SD_RMW_MSG_PRINT_TIMEOUT); 16154 } else { 16155 un->un_rmw_msg_timeid = NULL; 16156 } 16157 16158 mutex_exit(SD_MUTEX(un)); 16159 16160 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16161 "sd_rmw_msg_print_handler: exit\n"); 16162 } 16163 16164 /* 16165 * Function: sd_start_direct_priority_command 16166 * 16167 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16168 * received TRAN_BUSY when we called scsi_transport() to send it 16169 * to the underlying HBA. This function is called from timeout(9F) 16170 * context after the delay interval has expired. 16171 * 16172 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16173 * 16174 * Context: timeout(9F) thread context. May not sleep. 16175 */ 16176 16177 static void 16178 sd_start_direct_priority_command(void *arg) 16179 { 16180 struct buf *priority_bp = arg; 16181 struct sd_lun *un; 16182 16183 ASSERT(priority_bp != NULL); 16184 un = SD_GET_UN(priority_bp); 16185 ASSERT(un != NULL); 16186 ASSERT(!mutex_owned(SD_MUTEX(un))); 16187 16188 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16189 "sd_start_direct_priority_command: entry\n"); 16190 16191 mutex_enter(SD_MUTEX(un)); 16192 un->un_direct_priority_timeid = NULL; 16193 sd_start_cmds(un, priority_bp); 16194 mutex_exit(SD_MUTEX(un)); 16195 16196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16197 "sd_start_direct_priority_command: exit\n"); 16198 } 16199 16200 16201 /* 16202 * Function: sd_send_request_sense_command 16203 * 16204 * Description: Sends a REQUEST SENSE command to the target 16205 * 16206 * Context: May be called from interrupt context. 16207 */ 16208 16209 static void 16210 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16211 struct scsi_pkt *pktp) 16212 { 16213 ASSERT(bp != NULL); 16214 ASSERT(un != NULL); 16215 ASSERT(mutex_owned(SD_MUTEX(un))); 16216 16217 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16218 "entry: buf:0x%p\n", bp); 16219 16220 /* 16221 * If we are syncing or dumping, then fail the command to avoid a 16222 * recursive callback into scsi_transport(). Also fail the command 16223 * if we are suspended (legacy behavior). 16224 */ 16225 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16226 (un->un_state == SD_STATE_DUMPING)) { 16227 sd_return_failed_command(un, bp, EIO); 16228 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16229 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16230 return; 16231 } 16232 16233 /* 16234 * Retry the failed command and don't issue the request sense if: 16235 * 1) the sense buf is busy 16236 * 2) we have 1 or more outstanding commands on the target 16237 * (the sense data will be cleared or invalidated any way) 16238 * 16239 * Note: There could be an issue with not checking a retry limit here, 16240 * the problem is determining which retry limit to check. 16241 */ 16242 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16243 /* Don't retry if the command is flagged as non-retryable */ 16244 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16245 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16246 NULL, NULL, 0, un->un_busy_timeout, 16247 kstat_waitq_enter); 16248 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16249 "sd_send_request_sense_command: " 16250 "at full throttle, retrying exit\n"); 16251 } else { 16252 sd_return_failed_command(un, bp, EIO); 16253 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16254 "sd_send_request_sense_command: " 16255 "at full throttle, non-retryable exit\n"); 16256 } 16257 return; 16258 } 16259 16260 sd_mark_rqs_busy(un, bp); 16261 sd_start_cmds(un, un->un_rqs_bp); 16262 16263 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16264 "sd_send_request_sense_command: exit\n"); 16265 } 16266 16267 16268 /* 16269 * Function: sd_mark_rqs_busy 16270 * 16271 * Description: Indicate that the request sense bp for this instance is 16272 * in use. 16273 * 16274 * Context: May be called under interrupt context 16275 */ 16276 16277 static void 16278 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16279 { 16280 struct sd_xbuf *sense_xp; 16281 16282 ASSERT(un != NULL); 16283 ASSERT(bp != NULL); 16284 ASSERT(mutex_owned(SD_MUTEX(un))); 16285 ASSERT(un->un_sense_isbusy == 0); 16286 16287 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16288 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16289 16290 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16291 ASSERT(sense_xp != NULL); 16292 16293 SD_INFO(SD_LOG_IO, un, 16294 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16295 16296 ASSERT(sense_xp->xb_pktp != NULL); 16297 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16298 == (FLAG_SENSING | FLAG_HEAD)); 16299 16300 un->un_sense_isbusy = 1; 16301 un->un_rqs_bp->b_resid = 0; 16302 sense_xp->xb_pktp->pkt_resid = 0; 16303 sense_xp->xb_pktp->pkt_reason = 0; 16304 16305 /* So we can get back the bp at interrupt time! */ 16306 sense_xp->xb_sense_bp = bp; 16307 16308 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16309 16310 /* 16311 * Mark this buf as awaiting sense data. (This is already set in 16312 * the pkt_flags for the RQS packet.) 16313 */ 16314 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16315 16316 /* Request sense down same path */ 16317 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) && 16318 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance) 16319 sense_xp->xb_pktp->pkt_path_instance = 16320 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance; 16321 16322 sense_xp->xb_retry_count = 0; 16323 sense_xp->xb_victim_retry_count = 0; 16324 sense_xp->xb_ua_retry_count = 0; 16325 sense_xp->xb_nr_retry_count = 0; 16326 sense_xp->xb_dma_resid = 0; 16327 16328 /* Clean up the fields for auto-request sense */ 16329 sense_xp->xb_sense_status = 0; 16330 sense_xp->xb_sense_state = 0; 16331 sense_xp->xb_sense_resid = 0; 16332 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16333 16334 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16335 } 16336 16337 16338 /* 16339 * Function: sd_mark_rqs_idle 16340 * 16341 * Description: SD_MUTEX must be held continuously through this routine 16342 * to prevent reuse of the rqs struct before the caller can 16343 * complete it's processing. 16344 * 16345 * Return Code: Pointer to the RQS buf 16346 * 16347 * Context: May be called under interrupt context 16348 */ 16349 16350 static struct buf * 16351 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16352 { 16353 struct buf *bp; 16354 ASSERT(un != NULL); 16355 ASSERT(sense_xp != NULL); 16356 ASSERT(mutex_owned(SD_MUTEX(un))); 16357 ASSERT(un->un_sense_isbusy != 0); 16358 16359 un->un_sense_isbusy = 0; 16360 bp = sense_xp->xb_sense_bp; 16361 sense_xp->xb_sense_bp = NULL; 16362 16363 /* This pkt is no longer interested in getting sense data */ 16364 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16365 16366 return (bp); 16367 } 16368 16369 16370 16371 /* 16372 * Function: sd_alloc_rqs 16373 * 16374 * Description: Set up the unit to receive auto request sense data 16375 * 16376 * Return Code: DDI_SUCCESS or DDI_FAILURE 16377 * 16378 * Context: Called under attach(9E) context 16379 */ 16380 16381 static int 16382 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16383 { 16384 struct sd_xbuf *xp; 16385 16386 ASSERT(un != NULL); 16387 ASSERT(!mutex_owned(SD_MUTEX(un))); 16388 ASSERT(un->un_rqs_bp == NULL); 16389 ASSERT(un->un_rqs_pktp == NULL); 16390 16391 /* 16392 * First allocate the required buf and scsi_pkt structs, then set up 16393 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16394 */ 16395 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16396 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16397 if (un->un_rqs_bp == NULL) { 16398 return (DDI_FAILURE); 16399 } 16400 16401 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16402 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16403 16404 if (un->un_rqs_pktp == NULL) { 16405 sd_free_rqs(un); 16406 return (DDI_FAILURE); 16407 } 16408 16409 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16410 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16411 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 16412 16413 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16414 16415 /* Set up the other needed members in the ARQ scsi_pkt. */ 16416 un->un_rqs_pktp->pkt_comp = sdintr; 16417 un->un_rqs_pktp->pkt_time = sd_io_time; 16418 un->un_rqs_pktp->pkt_flags |= 16419 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16420 16421 /* 16422 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16423 * provide any intpkt, destroypkt routines as we take care of 16424 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16425 */ 16426 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16427 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16428 xp->xb_pktp = un->un_rqs_pktp; 16429 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16430 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16431 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16432 16433 /* 16434 * Save the pointer to the request sense private bp so it can 16435 * be retrieved in sdintr. 16436 */ 16437 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16438 ASSERT(un->un_rqs_bp->b_private == xp); 16439 16440 /* 16441 * See if the HBA supports auto-request sense for the specified 16442 * target/lun. If it does, then try to enable it (if not already 16443 * enabled). 16444 * 16445 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16446 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16447 * return success. However, in both of these cases ARQ is always 16448 * enabled and scsi_ifgetcap will always return true. The best approach 16449 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16450 * 16451 * The 3rd case is the HBA (adp) always return enabled on 16452 * scsi_ifgetgetcap even when it's not enable, the best approach 16453 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16454 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16455 */ 16456 16457 if (un->un_f_is_fibre == TRUE) { 16458 un->un_f_arq_enabled = TRUE; 16459 } else { 16460 #if defined(__i386) || defined(__amd64) 16461 /* 16462 * Circumvent the Adaptec bug, remove this code when 16463 * the bug is fixed 16464 */ 16465 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16466 #endif 16467 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16468 case 0: 16469 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16470 "sd_alloc_rqs: HBA supports ARQ\n"); 16471 /* 16472 * ARQ is supported by this HBA but currently is not 16473 * enabled. Attempt to enable it and if successful then 16474 * mark this instance as ARQ enabled. 16475 */ 16476 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16477 == 1) { 16478 /* Successfully enabled ARQ in the HBA */ 16479 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16480 "sd_alloc_rqs: ARQ enabled\n"); 16481 un->un_f_arq_enabled = TRUE; 16482 } else { 16483 /* Could not enable ARQ in the HBA */ 16484 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16485 "sd_alloc_rqs: failed ARQ enable\n"); 16486 un->un_f_arq_enabled = FALSE; 16487 } 16488 break; 16489 case 1: 16490 /* 16491 * ARQ is supported by this HBA and is already enabled. 16492 * Just mark ARQ as enabled for this instance. 16493 */ 16494 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16495 "sd_alloc_rqs: ARQ already enabled\n"); 16496 un->un_f_arq_enabled = TRUE; 16497 break; 16498 default: 16499 /* 16500 * ARQ is not supported by this HBA; disable it for this 16501 * instance. 16502 */ 16503 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16504 "sd_alloc_rqs: HBA does not support ARQ\n"); 16505 un->un_f_arq_enabled = FALSE; 16506 break; 16507 } 16508 } 16509 16510 return (DDI_SUCCESS); 16511 } 16512 16513 16514 /* 16515 * Function: sd_free_rqs 16516 * 16517 * Description: Cleanup for the pre-instance RQS command. 16518 * 16519 * Context: Kernel thread context 16520 */ 16521 16522 static void 16523 sd_free_rqs(struct sd_lun *un) 16524 { 16525 ASSERT(un != NULL); 16526 16527 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16528 16529 /* 16530 * If consistent memory is bound to a scsi_pkt, the pkt 16531 * has to be destroyed *before* freeing the consistent memory. 16532 * Don't change the sequence of this operations. 16533 * scsi_destroy_pkt() might access memory, which isn't allowed, 16534 * after it was freed in scsi_free_consistent_buf(). 16535 */ 16536 if (un->un_rqs_pktp != NULL) { 16537 scsi_destroy_pkt(un->un_rqs_pktp); 16538 un->un_rqs_pktp = NULL; 16539 } 16540 16541 if (un->un_rqs_bp != NULL) { 16542 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 16543 if (xp != NULL) { 16544 kmem_free(xp, sizeof (struct sd_xbuf)); 16545 } 16546 scsi_free_consistent_buf(un->un_rqs_bp); 16547 un->un_rqs_bp = NULL; 16548 } 16549 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16550 } 16551 16552 16553 16554 /* 16555 * Function: sd_reduce_throttle 16556 * 16557 * Description: Reduces the maximum # of outstanding commands on a 16558 * target to the current number of outstanding commands. 16559 * Queues a tiemout(9F) callback to restore the limit 16560 * after a specified interval has elapsed. 16561 * Typically used when we get a TRAN_BUSY return code 16562 * back from scsi_transport(). 16563 * 16564 * Arguments: un - ptr to the sd_lun softstate struct 16565 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16566 * 16567 * Context: May be called from interrupt context 16568 */ 16569 16570 static void 16571 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16572 { 16573 ASSERT(un != NULL); 16574 ASSERT(mutex_owned(SD_MUTEX(un))); 16575 ASSERT(un->un_ncmds_in_transport >= 0); 16576 16577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16578 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16579 un, un->un_throttle, un->un_ncmds_in_transport); 16580 16581 if (un->un_throttle > 1) { 16582 if (un->un_f_use_adaptive_throttle == TRUE) { 16583 switch (throttle_type) { 16584 case SD_THROTTLE_TRAN_BUSY: 16585 if (un->un_busy_throttle == 0) { 16586 un->un_busy_throttle = un->un_throttle; 16587 } 16588 break; 16589 case SD_THROTTLE_QFULL: 16590 un->un_busy_throttle = 0; 16591 break; 16592 default: 16593 ASSERT(FALSE); 16594 } 16595 16596 if (un->un_ncmds_in_transport > 0) { 16597 un->un_throttle = un->un_ncmds_in_transport; 16598 } 16599 16600 } else { 16601 if (un->un_ncmds_in_transport == 0) { 16602 un->un_throttle = 1; 16603 } else { 16604 un->un_throttle = un->un_ncmds_in_transport; 16605 } 16606 } 16607 } 16608 16609 /* Reschedule the timeout if none is currently active */ 16610 if (un->un_reset_throttle_timeid == NULL) { 16611 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16612 un, SD_THROTTLE_RESET_INTERVAL); 16613 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16614 "sd_reduce_throttle: timeout scheduled!\n"); 16615 } 16616 16617 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16618 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16619 } 16620 16621 16622 16623 /* 16624 * Function: sd_restore_throttle 16625 * 16626 * Description: Callback function for timeout(9F). Resets the current 16627 * value of un->un_throttle to its default. 16628 * 16629 * Arguments: arg - pointer to associated softstate for the device. 16630 * 16631 * Context: May be called from interrupt context 16632 */ 16633 16634 static void 16635 sd_restore_throttle(void *arg) 16636 { 16637 struct sd_lun *un = arg; 16638 16639 ASSERT(un != NULL); 16640 ASSERT(!mutex_owned(SD_MUTEX(un))); 16641 16642 mutex_enter(SD_MUTEX(un)); 16643 16644 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16645 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16646 16647 un->un_reset_throttle_timeid = NULL; 16648 16649 if (un->un_f_use_adaptive_throttle == TRUE) { 16650 /* 16651 * If un_busy_throttle is nonzero, then it contains the 16652 * value that un_throttle was when we got a TRAN_BUSY back 16653 * from scsi_transport(). We want to revert back to this 16654 * value. 16655 * 16656 * In the QFULL case, the throttle limit will incrementally 16657 * increase until it reaches max throttle. 16658 */ 16659 if (un->un_busy_throttle > 0) { 16660 un->un_throttle = un->un_busy_throttle; 16661 un->un_busy_throttle = 0; 16662 } else { 16663 /* 16664 * increase throttle by 10% open gate slowly, schedule 16665 * another restore if saved throttle has not been 16666 * reached 16667 */ 16668 short throttle; 16669 if (sd_qfull_throttle_enable) { 16670 throttle = un->un_throttle + 16671 max((un->un_throttle / 10), 1); 16672 un->un_throttle = 16673 (throttle < un->un_saved_throttle) ? 16674 throttle : un->un_saved_throttle; 16675 if (un->un_throttle < un->un_saved_throttle) { 16676 un->un_reset_throttle_timeid = 16677 timeout(sd_restore_throttle, 16678 un, 16679 SD_QFULL_THROTTLE_RESET_INTERVAL); 16680 } 16681 } 16682 } 16683 16684 /* 16685 * If un_throttle has fallen below the low-water mark, we 16686 * restore the maximum value here (and allow it to ratchet 16687 * down again if necessary). 16688 */ 16689 if (un->un_throttle < un->un_min_throttle) { 16690 un->un_throttle = un->un_saved_throttle; 16691 } 16692 } else { 16693 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16694 "restoring limit from 0x%x to 0x%x\n", 16695 un->un_throttle, un->un_saved_throttle); 16696 un->un_throttle = un->un_saved_throttle; 16697 } 16698 16699 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16700 "sd_restore_throttle: calling sd_start_cmds!\n"); 16701 16702 sd_start_cmds(un, NULL); 16703 16704 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16705 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16706 un, un->un_throttle); 16707 16708 mutex_exit(SD_MUTEX(un)); 16709 16710 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16711 } 16712 16713 /* 16714 * Function: sdrunout 16715 * 16716 * Description: Callback routine for scsi_init_pkt when a resource allocation 16717 * fails. 16718 * 16719 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16720 * soft state instance. 16721 * 16722 * Return Code: The scsi_init_pkt routine allows for the callback function to 16723 * return a 0 indicating the callback should be rescheduled or a 1 16724 * indicating not to reschedule. This routine always returns 1 16725 * because the driver always provides a callback function to 16726 * scsi_init_pkt. This results in a callback always being scheduled 16727 * (via the scsi_init_pkt callback implementation) if a resource 16728 * failure occurs. 16729 * 16730 * Context: This callback function may not block or call routines that block 16731 * 16732 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16733 * request persisting at the head of the list which cannot be 16734 * satisfied even after multiple retries. In the future the driver 16735 * may implement some time of maximum runout count before failing 16736 * an I/O. 16737 */ 16738 16739 static int 16740 sdrunout(caddr_t arg) 16741 { 16742 struct sd_lun *un = (struct sd_lun *)arg; 16743 16744 ASSERT(un != NULL); 16745 ASSERT(!mutex_owned(SD_MUTEX(un))); 16746 16747 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16748 16749 mutex_enter(SD_MUTEX(un)); 16750 sd_start_cmds(un, NULL); 16751 mutex_exit(SD_MUTEX(un)); 16752 /* 16753 * This callback routine always returns 1 (i.e. do not reschedule) 16754 * because we always specify sdrunout as the callback handler for 16755 * scsi_init_pkt inside the call to sd_start_cmds. 16756 */ 16757 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16758 return (1); 16759 } 16760 16761 16762 /* 16763 * Function: sdintr 16764 * 16765 * Description: Completion callback routine for scsi_pkt(9S) structs 16766 * sent to the HBA driver via scsi_transport(9F). 16767 * 16768 * Context: Interrupt context 16769 */ 16770 16771 static void 16772 sdintr(struct scsi_pkt *pktp) 16773 { 16774 struct buf *bp; 16775 struct sd_xbuf *xp; 16776 struct sd_lun *un; 16777 size_t actual_len; 16778 sd_ssc_t *sscp; 16779 16780 ASSERT(pktp != NULL); 16781 bp = (struct buf *)pktp->pkt_private; 16782 ASSERT(bp != NULL); 16783 xp = SD_GET_XBUF(bp); 16784 ASSERT(xp != NULL); 16785 ASSERT(xp->xb_pktp != NULL); 16786 un = SD_GET_UN(bp); 16787 ASSERT(un != NULL); 16788 ASSERT(!mutex_owned(SD_MUTEX(un))); 16789 16790 #ifdef SD_FAULT_INJECTION 16791 16792 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16793 /* SD FaultInjection */ 16794 sd_faultinjection(pktp); 16795 16796 #endif /* SD_FAULT_INJECTION */ 16797 16798 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16799 " xp:0x%p, un:0x%p\n", bp, xp, un); 16800 16801 mutex_enter(SD_MUTEX(un)); 16802 16803 ASSERT(un->un_fm_private != NULL); 16804 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 16805 ASSERT(sscp != NULL); 16806 16807 /* Reduce the count of the #commands currently in transport */ 16808 un->un_ncmds_in_transport--; 16809 ASSERT(un->un_ncmds_in_transport >= 0); 16810 16811 /* Increment counter to indicate that the callback routine is active */ 16812 un->un_in_callback++; 16813 16814 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16815 16816 #ifdef SDDEBUG 16817 if (bp == un->un_retry_bp) { 16818 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16819 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16820 un, un->un_retry_bp, un->un_ncmds_in_transport); 16821 } 16822 #endif 16823 16824 /* 16825 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 16826 * state if needed. 16827 */ 16828 if (pktp->pkt_reason == CMD_DEV_GONE) { 16829 /* Prevent multiple console messages for the same failure. */ 16830 if (un->un_last_pkt_reason != CMD_DEV_GONE) { 16831 un->un_last_pkt_reason = CMD_DEV_GONE; 16832 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16833 "Command failed to complete...Device is gone\n"); 16834 } 16835 if (un->un_mediastate != DKIO_DEV_GONE) { 16836 un->un_mediastate = DKIO_DEV_GONE; 16837 cv_broadcast(&un->un_state_cv); 16838 } 16839 /* 16840 * If the command happens to be the REQUEST SENSE command, 16841 * free up the rqs buf and fail the original command. 16842 */ 16843 if (bp == un->un_rqs_bp) { 16844 bp = sd_mark_rqs_idle(un, xp); 16845 } 16846 sd_return_failed_command(un, bp, EIO); 16847 goto exit; 16848 } 16849 16850 if (pktp->pkt_state & STATE_XARQ_DONE) { 16851 SD_TRACE(SD_LOG_COMMON, un, 16852 "sdintr: extra sense data received. pkt=%p\n", pktp); 16853 } 16854 16855 /* 16856 * First see if the pkt has auto-request sense data with it.... 16857 * Look at the packet state first so we don't take a performance 16858 * hit looking at the arq enabled flag unless absolutely necessary. 16859 */ 16860 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16861 (un->un_f_arq_enabled == TRUE)) { 16862 /* 16863 * The HBA did an auto request sense for this command so check 16864 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16865 * driver command that should not be retried. 16866 */ 16867 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16868 /* 16869 * Save the relevant sense info into the xp for the 16870 * original cmd. 16871 */ 16872 struct scsi_arq_status *asp; 16873 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16874 xp->xb_sense_status = 16875 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16876 xp->xb_sense_state = asp->sts_rqpkt_state; 16877 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16878 if (pktp->pkt_state & STATE_XARQ_DONE) { 16879 actual_len = MAX_SENSE_LENGTH - 16880 xp->xb_sense_resid; 16881 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16882 MAX_SENSE_LENGTH); 16883 } else { 16884 if (xp->xb_sense_resid > SENSE_LENGTH) { 16885 actual_len = MAX_SENSE_LENGTH - 16886 xp->xb_sense_resid; 16887 } else { 16888 actual_len = SENSE_LENGTH - 16889 xp->xb_sense_resid; 16890 } 16891 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16892 if ((((struct uscsi_cmd *) 16893 (xp->xb_pktinfo))->uscsi_rqlen) > 16894 actual_len) { 16895 xp->xb_sense_resid = 16896 (((struct uscsi_cmd *) 16897 (xp->xb_pktinfo))-> 16898 uscsi_rqlen) - actual_len; 16899 } else { 16900 xp->xb_sense_resid = 0; 16901 } 16902 } 16903 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16904 SENSE_LENGTH); 16905 } 16906 16907 /* fail the command */ 16908 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16909 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16910 sd_return_failed_command(un, bp, EIO); 16911 goto exit; 16912 } 16913 16914 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16915 /* 16916 * We want to either retry or fail this command, so free 16917 * the DMA resources here. If we retry the command then 16918 * the DMA resources will be reallocated in sd_start_cmds(). 16919 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16920 * causes the *entire* transfer to start over again from the 16921 * beginning of the request, even for PARTIAL chunks that 16922 * have already transferred successfully. 16923 */ 16924 if ((un->un_f_is_fibre == TRUE) && 16925 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16926 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16927 scsi_dmafree(pktp); 16928 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16929 } 16930 #endif 16931 16932 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16933 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16934 16935 sd_handle_auto_request_sense(un, bp, xp, pktp); 16936 goto exit; 16937 } 16938 16939 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16940 if (pktp->pkt_flags & FLAG_SENSING) { 16941 /* This pktp is from the unit's REQUEST_SENSE command */ 16942 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16943 "sdintr: sd_handle_request_sense\n"); 16944 sd_handle_request_sense(un, bp, xp, pktp); 16945 goto exit; 16946 } 16947 16948 /* 16949 * Check to see if the command successfully completed as requested; 16950 * this is the most common case (and also the hot performance path). 16951 * 16952 * Requirements for successful completion are: 16953 * pkt_reason is CMD_CMPLT and packet status is status good. 16954 * In addition: 16955 * - A residual of zero indicates successful completion no matter what 16956 * the command is. 16957 * - If the residual is not zero and the command is not a read or 16958 * write, then it's still defined as successful completion. In other 16959 * words, if the command is a read or write the residual must be 16960 * zero for successful completion. 16961 * - If the residual is not zero and the command is a read or 16962 * write, and it's a USCSICMD, then it's still defined as 16963 * successful completion. 16964 */ 16965 if ((pktp->pkt_reason == CMD_CMPLT) && 16966 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16967 16968 /* 16969 * Since this command is returned with a good status, we 16970 * can reset the count for Sonoma failover. 16971 */ 16972 un->un_sonoma_failure_count = 0; 16973 16974 /* 16975 * Return all USCSI commands on good status 16976 */ 16977 if (pktp->pkt_resid == 0) { 16978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16979 "sdintr: returning command for resid == 0\n"); 16980 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16981 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16982 SD_UPDATE_B_RESID(bp, pktp); 16983 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16984 "sdintr: returning command for resid != 0\n"); 16985 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16986 SD_UPDATE_B_RESID(bp, pktp); 16987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16988 "sdintr: returning uscsi command\n"); 16989 } else { 16990 goto not_successful; 16991 } 16992 sd_return_command(un, bp); 16993 16994 /* 16995 * Decrement counter to indicate that the callback routine 16996 * is done. 16997 */ 16998 un->un_in_callback--; 16999 ASSERT(un->un_in_callback >= 0); 17000 mutex_exit(SD_MUTEX(un)); 17001 17002 return; 17003 } 17004 17005 not_successful: 17006 17007 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 17008 /* 17009 * The following is based upon knowledge of the underlying transport 17010 * and its use of DMA resources. This code should be removed when 17011 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 17012 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 17013 * and sd_start_cmds(). 17014 * 17015 * Free any DMA resources associated with this command if there 17016 * is a chance it could be retried or enqueued for later retry. 17017 * If we keep the DMA binding then mpxio cannot reissue the 17018 * command on another path whenever a path failure occurs. 17019 * 17020 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 17021 * causes the *entire* transfer to start over again from the 17022 * beginning of the request, even for PARTIAL chunks that 17023 * have already transferred successfully. 17024 * 17025 * This is only done for non-uscsi commands (and also skipped for the 17026 * driver's internal RQS command). Also just do this for Fibre Channel 17027 * devices as these are the only ones that support mpxio. 17028 */ 17029 if ((un->un_f_is_fibre == TRUE) && 17030 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 17031 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 17032 scsi_dmafree(pktp); 17033 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 17034 } 17035 #endif 17036 17037 /* 17038 * The command did not successfully complete as requested so check 17039 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 17040 * driver command that should not be retried so just return. If 17041 * FLAG_DIAGNOSE is not set the error will be processed below. 17042 */ 17043 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 17044 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17045 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 17046 /* 17047 * Issue a request sense if a check condition caused the error 17048 * (we handle the auto request sense case above), otherwise 17049 * just fail the command. 17050 */ 17051 if ((pktp->pkt_reason == CMD_CMPLT) && 17052 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 17053 sd_send_request_sense_command(un, bp, pktp); 17054 } else { 17055 sd_return_failed_command(un, bp, EIO); 17056 } 17057 goto exit; 17058 } 17059 17060 /* 17061 * The command did not successfully complete as requested so process 17062 * the error, retry, and/or attempt recovery. 17063 */ 17064 switch (pktp->pkt_reason) { 17065 case CMD_CMPLT: 17066 switch (SD_GET_PKT_STATUS(pktp)) { 17067 case STATUS_GOOD: 17068 /* 17069 * The command completed successfully with a non-zero 17070 * residual 17071 */ 17072 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17073 "sdintr: STATUS_GOOD \n"); 17074 sd_pkt_status_good(un, bp, xp, pktp); 17075 break; 17076 17077 case STATUS_CHECK: 17078 case STATUS_TERMINATED: 17079 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17080 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 17081 sd_pkt_status_check_condition(un, bp, xp, pktp); 17082 break; 17083 17084 case STATUS_BUSY: 17085 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17086 "sdintr: STATUS_BUSY\n"); 17087 sd_pkt_status_busy(un, bp, xp, pktp); 17088 break; 17089 17090 case STATUS_RESERVATION_CONFLICT: 17091 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17092 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 17093 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17094 break; 17095 17096 case STATUS_QFULL: 17097 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17098 "sdintr: STATUS_QFULL\n"); 17099 sd_pkt_status_qfull(un, bp, xp, pktp); 17100 break; 17101 17102 case STATUS_MET: 17103 case STATUS_INTERMEDIATE: 17104 case STATUS_SCSI2: 17105 case STATUS_INTERMEDIATE_MET: 17106 case STATUS_ACA_ACTIVE: 17107 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17108 "Unexpected SCSI status received: 0x%x\n", 17109 SD_GET_PKT_STATUS(pktp)); 17110 /* 17111 * Mark the ssc_flags when detected invalid status 17112 * code for non-USCSI command. 17113 */ 17114 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17115 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17116 0, "stat-code"); 17117 } 17118 sd_return_failed_command(un, bp, EIO); 17119 break; 17120 17121 default: 17122 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17123 "Invalid SCSI status received: 0x%x\n", 17124 SD_GET_PKT_STATUS(pktp)); 17125 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17126 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 17127 0, "stat-code"); 17128 } 17129 sd_return_failed_command(un, bp, EIO); 17130 break; 17131 17132 } 17133 break; 17134 17135 case CMD_INCOMPLETE: 17136 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17137 "sdintr: CMD_INCOMPLETE\n"); 17138 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 17139 break; 17140 case CMD_TRAN_ERR: 17141 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17142 "sdintr: CMD_TRAN_ERR\n"); 17143 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 17144 break; 17145 case CMD_RESET: 17146 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17147 "sdintr: CMD_RESET \n"); 17148 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 17149 break; 17150 case CMD_ABORTED: 17151 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17152 "sdintr: CMD_ABORTED \n"); 17153 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 17154 break; 17155 case CMD_TIMEOUT: 17156 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17157 "sdintr: CMD_TIMEOUT\n"); 17158 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 17159 break; 17160 case CMD_UNX_BUS_FREE: 17161 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17162 "sdintr: CMD_UNX_BUS_FREE \n"); 17163 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 17164 break; 17165 case CMD_TAG_REJECT: 17166 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17167 "sdintr: CMD_TAG_REJECT\n"); 17168 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 17169 break; 17170 default: 17171 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17172 "sdintr: default\n"); 17173 /* 17174 * Mark the ssc_flags for detecting invliad pkt_reason. 17175 */ 17176 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17177 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON, 17178 0, "pkt-reason"); 17179 } 17180 sd_pkt_reason_default(un, bp, xp, pktp); 17181 break; 17182 } 17183 17184 exit: 17185 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 17186 17187 /* Decrement counter to indicate that the callback routine is done. */ 17188 un->un_in_callback--; 17189 ASSERT(un->un_in_callback >= 0); 17190 17191 /* 17192 * At this point, the pkt has been dispatched, ie, it is either 17193 * being re-tried or has been returned to its caller and should 17194 * not be referenced. 17195 */ 17196 17197 mutex_exit(SD_MUTEX(un)); 17198 } 17199 17200 17201 /* 17202 * Function: sd_print_incomplete_msg 17203 * 17204 * Description: Prints the error message for a CMD_INCOMPLETE error. 17205 * 17206 * Arguments: un - ptr to associated softstate for the device. 17207 * bp - ptr to the buf(9S) for the command. 17208 * arg - message string ptr 17209 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17210 * or SD_NO_RETRY_ISSUED. 17211 * 17212 * Context: May be called under interrupt context 17213 */ 17214 17215 static void 17216 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17217 { 17218 struct scsi_pkt *pktp; 17219 char *msgp; 17220 char *cmdp = arg; 17221 17222 ASSERT(un != NULL); 17223 ASSERT(mutex_owned(SD_MUTEX(un))); 17224 ASSERT(bp != NULL); 17225 ASSERT(arg != NULL); 17226 pktp = SD_GET_PKTP(bp); 17227 ASSERT(pktp != NULL); 17228 17229 switch (code) { 17230 case SD_DELAYED_RETRY_ISSUED: 17231 case SD_IMMEDIATE_RETRY_ISSUED: 17232 msgp = "retrying"; 17233 break; 17234 case SD_NO_RETRY_ISSUED: 17235 default: 17236 msgp = "giving up"; 17237 break; 17238 } 17239 17240 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17241 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17242 "incomplete %s- %s\n", cmdp, msgp); 17243 } 17244 } 17245 17246 17247 17248 /* 17249 * Function: sd_pkt_status_good 17250 * 17251 * Description: Processing for a STATUS_GOOD code in pkt_status. 17252 * 17253 * Context: May be called under interrupt context 17254 */ 17255 17256 static void 17257 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17258 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17259 { 17260 char *cmdp; 17261 17262 ASSERT(un != NULL); 17263 ASSERT(mutex_owned(SD_MUTEX(un))); 17264 ASSERT(bp != NULL); 17265 ASSERT(xp != NULL); 17266 ASSERT(pktp != NULL); 17267 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17268 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17269 ASSERT(pktp->pkt_resid != 0); 17270 17271 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17272 17273 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17274 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17275 case SCMD_READ: 17276 cmdp = "read"; 17277 break; 17278 case SCMD_WRITE: 17279 cmdp = "write"; 17280 break; 17281 default: 17282 SD_UPDATE_B_RESID(bp, pktp); 17283 sd_return_command(un, bp); 17284 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17285 return; 17286 } 17287 17288 /* 17289 * See if we can retry the read/write, preferrably immediately. 17290 * If retries are exhaused, then sd_retry_command() will update 17291 * the b_resid count. 17292 */ 17293 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17294 cmdp, EIO, (clock_t)0, NULL); 17295 17296 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17297 } 17298 17299 17300 17301 17302 17303 /* 17304 * Function: sd_handle_request_sense 17305 * 17306 * Description: Processing for non-auto Request Sense command. 17307 * 17308 * Arguments: un - ptr to associated softstate 17309 * sense_bp - ptr to buf(9S) for the RQS command 17310 * sense_xp - ptr to the sd_xbuf for the RQS command 17311 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17312 * 17313 * Context: May be called under interrupt context 17314 */ 17315 17316 static void 17317 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17318 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17319 { 17320 struct buf *cmd_bp; /* buf for the original command */ 17321 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17322 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17323 size_t actual_len; /* actual sense data length */ 17324 17325 ASSERT(un != NULL); 17326 ASSERT(mutex_owned(SD_MUTEX(un))); 17327 ASSERT(sense_bp != NULL); 17328 ASSERT(sense_xp != NULL); 17329 ASSERT(sense_pktp != NULL); 17330 17331 /* 17332 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17333 * RQS command and not the original command. 17334 */ 17335 ASSERT(sense_pktp == un->un_rqs_pktp); 17336 ASSERT(sense_bp == un->un_rqs_bp); 17337 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17338 (FLAG_SENSING | FLAG_HEAD)); 17339 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17340 FLAG_SENSING) == FLAG_SENSING); 17341 17342 /* These are the bp, xp, and pktp for the original command */ 17343 cmd_bp = sense_xp->xb_sense_bp; 17344 cmd_xp = SD_GET_XBUF(cmd_bp); 17345 cmd_pktp = SD_GET_PKTP(cmd_bp); 17346 17347 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17348 /* 17349 * The REQUEST SENSE command failed. Release the REQUEST 17350 * SENSE command for re-use, get back the bp for the original 17351 * command, and attempt to re-try the original command if 17352 * FLAG_DIAGNOSE is not set in the original packet. 17353 */ 17354 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17355 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17356 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17357 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17358 NULL, NULL, EIO, (clock_t)0, NULL); 17359 return; 17360 } 17361 } 17362 17363 /* 17364 * Save the relevant sense info into the xp for the original cmd. 17365 * 17366 * Note: if the request sense failed the state info will be zero 17367 * as set in sd_mark_rqs_busy() 17368 */ 17369 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17370 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17371 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 17372 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 17373 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 17374 SENSE_LENGTH)) { 17375 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17376 MAX_SENSE_LENGTH); 17377 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17378 } else { 17379 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 17380 SENSE_LENGTH); 17381 if (actual_len < SENSE_LENGTH) { 17382 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 17383 } else { 17384 cmd_xp->xb_sense_resid = 0; 17385 } 17386 } 17387 17388 /* 17389 * Free up the RQS command.... 17390 * NOTE: 17391 * Must do this BEFORE calling sd_validate_sense_data! 17392 * sd_validate_sense_data may return the original command in 17393 * which case the pkt will be freed and the flags can no 17394 * longer be touched. 17395 * SD_MUTEX is held through this process until the command 17396 * is dispatched based upon the sense data, so there are 17397 * no race conditions. 17398 */ 17399 (void) sd_mark_rqs_idle(un, sense_xp); 17400 17401 /* 17402 * For a retryable command see if we have valid sense data, if so then 17403 * turn it over to sd_decode_sense() to figure out the right course of 17404 * action. Just fail a non-retryable command. 17405 */ 17406 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17407 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 17408 SD_SENSE_DATA_IS_VALID) { 17409 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17410 } 17411 } else { 17412 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17413 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17414 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17415 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17416 sd_return_failed_command(un, cmd_bp, EIO); 17417 } 17418 } 17419 17420 17421 17422 17423 /* 17424 * Function: sd_handle_auto_request_sense 17425 * 17426 * Description: Processing for auto-request sense information. 17427 * 17428 * Arguments: un - ptr to associated softstate 17429 * bp - ptr to buf(9S) for the command 17430 * xp - ptr to the sd_xbuf for the command 17431 * pktp - ptr to the scsi_pkt(9S) for the command 17432 * 17433 * Context: May be called under interrupt context 17434 */ 17435 17436 static void 17437 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17438 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17439 { 17440 struct scsi_arq_status *asp; 17441 size_t actual_len; 17442 17443 ASSERT(un != NULL); 17444 ASSERT(mutex_owned(SD_MUTEX(un))); 17445 ASSERT(bp != NULL); 17446 ASSERT(xp != NULL); 17447 ASSERT(pktp != NULL); 17448 ASSERT(pktp != un->un_rqs_pktp); 17449 ASSERT(bp != un->un_rqs_bp); 17450 17451 /* 17452 * For auto-request sense, we get a scsi_arq_status back from 17453 * the HBA, with the sense data in the sts_sensedata member. 17454 * The pkt_scbp of the packet points to this scsi_arq_status. 17455 */ 17456 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17457 17458 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17459 /* 17460 * The auto REQUEST SENSE failed; see if we can re-try 17461 * the original command. 17462 */ 17463 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17464 "auto request sense failed (reason=%s)\n", 17465 scsi_rname(asp->sts_rqpkt_reason)); 17466 17467 sd_reset_target(un, pktp); 17468 17469 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17470 NULL, NULL, EIO, (clock_t)0, NULL); 17471 return; 17472 } 17473 17474 /* Save the relevant sense info into the xp for the original cmd. */ 17475 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17476 xp->xb_sense_state = asp->sts_rqpkt_state; 17477 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17478 if (xp->xb_sense_state & STATE_XARQ_DONE) { 17479 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17480 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17481 MAX_SENSE_LENGTH); 17482 } else { 17483 if (xp->xb_sense_resid > SENSE_LENGTH) { 17484 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 17485 } else { 17486 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 17487 } 17488 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 17489 if ((((struct uscsi_cmd *) 17490 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) { 17491 xp->xb_sense_resid = (((struct uscsi_cmd *) 17492 (xp->xb_pktinfo))->uscsi_rqlen) - 17493 actual_len; 17494 } else { 17495 xp->xb_sense_resid = 0; 17496 } 17497 } 17498 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 17499 } 17500 17501 /* 17502 * See if we have valid sense data, if so then turn it over to 17503 * sd_decode_sense() to figure out the right course of action. 17504 */ 17505 if (sd_validate_sense_data(un, bp, xp, actual_len) == 17506 SD_SENSE_DATA_IS_VALID) { 17507 sd_decode_sense(un, bp, xp, pktp); 17508 } 17509 } 17510 17511 17512 /* 17513 * Function: sd_print_sense_failed_msg 17514 * 17515 * Description: Print log message when RQS has failed. 17516 * 17517 * Arguments: un - ptr to associated softstate 17518 * bp - ptr to buf(9S) for the command 17519 * arg - generic message string ptr 17520 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17521 * or SD_NO_RETRY_ISSUED 17522 * 17523 * Context: May be called from interrupt context 17524 */ 17525 17526 static void 17527 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17528 int code) 17529 { 17530 char *msgp = arg; 17531 17532 ASSERT(un != NULL); 17533 ASSERT(mutex_owned(SD_MUTEX(un))); 17534 ASSERT(bp != NULL); 17535 17536 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17537 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17538 } 17539 } 17540 17541 17542 /* 17543 * Function: sd_validate_sense_data 17544 * 17545 * Description: Check the given sense data for validity. 17546 * If the sense data is not valid, the command will 17547 * be either failed or retried! 17548 * 17549 * Return Code: SD_SENSE_DATA_IS_INVALID 17550 * SD_SENSE_DATA_IS_VALID 17551 * 17552 * Context: May be called from interrupt context 17553 */ 17554 17555 static int 17556 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17557 size_t actual_len) 17558 { 17559 struct scsi_extended_sense *esp; 17560 struct scsi_pkt *pktp; 17561 char *msgp = NULL; 17562 sd_ssc_t *sscp; 17563 17564 ASSERT(un != NULL); 17565 ASSERT(mutex_owned(SD_MUTEX(un))); 17566 ASSERT(bp != NULL); 17567 ASSERT(bp != un->un_rqs_bp); 17568 ASSERT(xp != NULL); 17569 ASSERT(un->un_fm_private != NULL); 17570 17571 pktp = SD_GET_PKTP(bp); 17572 ASSERT(pktp != NULL); 17573 17574 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 17575 ASSERT(sscp != NULL); 17576 17577 /* 17578 * Check the status of the RQS command (auto or manual). 17579 */ 17580 switch (xp->xb_sense_status & STATUS_MASK) { 17581 case STATUS_GOOD: 17582 break; 17583 17584 case STATUS_RESERVATION_CONFLICT: 17585 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17586 return (SD_SENSE_DATA_IS_INVALID); 17587 17588 case STATUS_BUSY: 17589 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17590 "Busy Status on REQUEST SENSE\n"); 17591 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17592 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17593 return (SD_SENSE_DATA_IS_INVALID); 17594 17595 case STATUS_QFULL: 17596 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17597 "QFULL Status on REQUEST SENSE\n"); 17598 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17599 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 17600 return (SD_SENSE_DATA_IS_INVALID); 17601 17602 case STATUS_CHECK: 17603 case STATUS_TERMINATED: 17604 msgp = "Check Condition on REQUEST SENSE\n"; 17605 goto sense_failed; 17606 17607 default: 17608 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17609 goto sense_failed; 17610 } 17611 17612 /* 17613 * See if we got the minimum required amount of sense data. 17614 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17615 * or less. 17616 */ 17617 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17618 (actual_len == 0)) { 17619 msgp = "Request Sense couldn't get sense data\n"; 17620 goto sense_failed; 17621 } 17622 17623 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17624 msgp = "Not enough sense information\n"; 17625 /* Mark the ssc_flags for detecting invalid sense data */ 17626 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17627 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17628 "sense-data"); 17629 } 17630 goto sense_failed; 17631 } 17632 17633 /* 17634 * We require the extended sense data 17635 */ 17636 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17637 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17638 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17639 static char tmp[8]; 17640 static char buf[148]; 17641 char *p = (char *)(xp->xb_sense_data); 17642 int i; 17643 17644 mutex_enter(&sd_sense_mutex); 17645 (void) strcpy(buf, "undecodable sense information:"); 17646 for (i = 0; i < actual_len; i++) { 17647 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17648 (void) strcpy(&buf[strlen(buf)], tmp); 17649 } 17650 i = strlen(buf); 17651 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17652 17653 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 17654 scsi_log(SD_DEVINFO(un), sd_label, 17655 CE_WARN, buf); 17656 } 17657 mutex_exit(&sd_sense_mutex); 17658 } 17659 17660 /* Mark the ssc_flags for detecting invalid sense data */ 17661 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17662 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17663 "sense-data"); 17664 } 17665 17666 /* Note: Legacy behavior, fail the command with no retry */ 17667 sd_return_failed_command(un, bp, EIO); 17668 return (SD_SENSE_DATA_IS_INVALID); 17669 } 17670 17671 /* 17672 * Check that es_code is valid (es_class concatenated with es_code 17673 * make up the "response code" field. es_class will always be 7, so 17674 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17675 * format. 17676 */ 17677 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17678 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17679 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17680 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17681 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17682 /* Mark the ssc_flags for detecting invalid sense data */ 17683 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 17684 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 17685 "sense-data"); 17686 } 17687 goto sense_failed; 17688 } 17689 17690 return (SD_SENSE_DATA_IS_VALID); 17691 17692 sense_failed: 17693 /* 17694 * If the request sense failed (for whatever reason), attempt 17695 * to retry the original command. 17696 */ 17697 #if defined(__i386) || defined(__amd64) 17698 /* 17699 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17700 * sddef.h for Sparc platform, and x86 uses 1 binary 17701 * for both SCSI/FC. 17702 * The SD_RETRY_DELAY value need to be adjusted here 17703 * when SD_RETRY_DELAY change in sddef.h 17704 */ 17705 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17706 sd_print_sense_failed_msg, msgp, EIO, 17707 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17708 #else 17709 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17710 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17711 #endif 17712 17713 return (SD_SENSE_DATA_IS_INVALID); 17714 } 17715 17716 /* 17717 * Function: sd_decode_sense 17718 * 17719 * Description: Take recovery action(s) when SCSI Sense Data is received. 17720 * 17721 * Context: Interrupt context. 17722 */ 17723 17724 static void 17725 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17726 struct scsi_pkt *pktp) 17727 { 17728 uint8_t sense_key; 17729 17730 ASSERT(un != NULL); 17731 ASSERT(mutex_owned(SD_MUTEX(un))); 17732 ASSERT(bp != NULL); 17733 ASSERT(bp != un->un_rqs_bp); 17734 ASSERT(xp != NULL); 17735 ASSERT(pktp != NULL); 17736 17737 sense_key = scsi_sense_key(xp->xb_sense_data); 17738 17739 switch (sense_key) { 17740 case KEY_NO_SENSE: 17741 sd_sense_key_no_sense(un, bp, xp, pktp); 17742 break; 17743 case KEY_RECOVERABLE_ERROR: 17744 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17745 bp, xp, pktp); 17746 break; 17747 case KEY_NOT_READY: 17748 sd_sense_key_not_ready(un, xp->xb_sense_data, 17749 bp, xp, pktp); 17750 break; 17751 case KEY_MEDIUM_ERROR: 17752 case KEY_HARDWARE_ERROR: 17753 sd_sense_key_medium_or_hardware_error(un, 17754 xp->xb_sense_data, bp, xp, pktp); 17755 break; 17756 case KEY_ILLEGAL_REQUEST: 17757 sd_sense_key_illegal_request(un, bp, xp, pktp); 17758 break; 17759 case KEY_UNIT_ATTENTION: 17760 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17761 bp, xp, pktp); 17762 break; 17763 case KEY_WRITE_PROTECT: 17764 case KEY_VOLUME_OVERFLOW: 17765 case KEY_MISCOMPARE: 17766 sd_sense_key_fail_command(un, bp, xp, pktp); 17767 break; 17768 case KEY_BLANK_CHECK: 17769 sd_sense_key_blank_check(un, bp, xp, pktp); 17770 break; 17771 case KEY_ABORTED_COMMAND: 17772 sd_sense_key_aborted_command(un, bp, xp, pktp); 17773 break; 17774 case KEY_VENDOR_UNIQUE: 17775 case KEY_COPY_ABORTED: 17776 case KEY_EQUAL: 17777 case KEY_RESERVED: 17778 default: 17779 sd_sense_key_default(un, xp->xb_sense_data, 17780 bp, xp, pktp); 17781 break; 17782 } 17783 } 17784 17785 17786 /* 17787 * Function: sd_dump_memory 17788 * 17789 * Description: Debug logging routine to print the contents of a user provided 17790 * buffer. The output of the buffer is broken up into 256 byte 17791 * segments due to a size constraint of the scsi_log. 17792 * implementation. 17793 * 17794 * Arguments: un - ptr to softstate 17795 * comp - component mask 17796 * title - "title" string to preceed data when printed 17797 * data - ptr to data block to be printed 17798 * len - size of data block to be printed 17799 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17800 * 17801 * Context: May be called from interrupt context 17802 */ 17803 17804 #define SD_DUMP_MEMORY_BUF_SIZE 256 17805 17806 static char *sd_dump_format_string[] = { 17807 " 0x%02x", 17808 " %c" 17809 }; 17810 17811 static void 17812 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17813 int len, int fmt) 17814 { 17815 int i, j; 17816 int avail_count; 17817 int start_offset; 17818 int end_offset; 17819 size_t entry_len; 17820 char *bufp; 17821 char *local_buf; 17822 char *format_string; 17823 17824 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17825 17826 /* 17827 * In the debug version of the driver, this function is called from a 17828 * number of places which are NOPs in the release driver. 17829 * The debug driver therefore has additional methods of filtering 17830 * debug output. 17831 */ 17832 #ifdef SDDEBUG 17833 /* 17834 * In the debug version of the driver we can reduce the amount of debug 17835 * messages by setting sd_error_level to something other than 17836 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17837 * sd_component_mask. 17838 */ 17839 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17840 (sd_error_level != SCSI_ERR_ALL)) { 17841 return; 17842 } 17843 if (((sd_component_mask & comp) == 0) || 17844 (sd_error_level != SCSI_ERR_ALL)) { 17845 return; 17846 } 17847 #else 17848 if (sd_error_level != SCSI_ERR_ALL) { 17849 return; 17850 } 17851 #endif 17852 17853 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17854 bufp = local_buf; 17855 /* 17856 * Available length is the length of local_buf[], minus the 17857 * length of the title string, minus one for the ":", minus 17858 * one for the newline, minus one for the NULL terminator. 17859 * This gives the #bytes available for holding the printed 17860 * values from the given data buffer. 17861 */ 17862 if (fmt == SD_LOG_HEX) { 17863 format_string = sd_dump_format_string[0]; 17864 } else /* SD_LOG_CHAR */ { 17865 format_string = sd_dump_format_string[1]; 17866 } 17867 /* 17868 * Available count is the number of elements from the given 17869 * data buffer that we can fit into the available length. 17870 * This is based upon the size of the format string used. 17871 * Make one entry and find it's size. 17872 */ 17873 (void) sprintf(bufp, format_string, data[0]); 17874 entry_len = strlen(bufp); 17875 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17876 17877 j = 0; 17878 while (j < len) { 17879 bufp = local_buf; 17880 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17881 start_offset = j; 17882 17883 end_offset = start_offset + avail_count; 17884 17885 (void) sprintf(bufp, "%s:", title); 17886 bufp += strlen(bufp); 17887 for (i = start_offset; ((i < end_offset) && (j < len)); 17888 i++, j++) { 17889 (void) sprintf(bufp, format_string, data[i]); 17890 bufp += entry_len; 17891 } 17892 (void) sprintf(bufp, "\n"); 17893 17894 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17895 } 17896 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17897 } 17898 17899 /* 17900 * Function: sd_print_sense_msg 17901 * 17902 * Description: Log a message based upon the given sense data. 17903 * 17904 * Arguments: un - ptr to associated softstate 17905 * bp - ptr to buf(9S) for the command 17906 * arg - ptr to associate sd_sense_info struct 17907 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17908 * or SD_NO_RETRY_ISSUED 17909 * 17910 * Context: May be called from interrupt context 17911 */ 17912 17913 static void 17914 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17915 { 17916 struct sd_xbuf *xp; 17917 struct scsi_pkt *pktp; 17918 uint8_t *sensep; 17919 daddr_t request_blkno; 17920 diskaddr_t err_blkno; 17921 int severity; 17922 int pfa_flag; 17923 extern struct scsi_key_strings scsi_cmds[]; 17924 17925 ASSERT(un != NULL); 17926 ASSERT(mutex_owned(SD_MUTEX(un))); 17927 ASSERT(bp != NULL); 17928 xp = SD_GET_XBUF(bp); 17929 ASSERT(xp != NULL); 17930 pktp = SD_GET_PKTP(bp); 17931 ASSERT(pktp != NULL); 17932 ASSERT(arg != NULL); 17933 17934 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17935 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17936 17937 if ((code == SD_DELAYED_RETRY_ISSUED) || 17938 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17939 severity = SCSI_ERR_RETRYABLE; 17940 } 17941 17942 /* Use absolute block number for the request block number */ 17943 request_blkno = xp->xb_blkno; 17944 17945 /* 17946 * Now try to get the error block number from the sense data 17947 */ 17948 sensep = xp->xb_sense_data; 17949 17950 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17951 (uint64_t *)&err_blkno)) { 17952 /* 17953 * We retrieved the error block number from the information 17954 * portion of the sense data. 17955 * 17956 * For USCSI commands we are better off using the error 17957 * block no. as the requested block no. (This is the best 17958 * we can estimate.) 17959 */ 17960 if ((SD_IS_BUFIO(xp) == FALSE) && 17961 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17962 request_blkno = err_blkno; 17963 } 17964 } else { 17965 /* 17966 * Without the es_valid bit set (for fixed format) or an 17967 * information descriptor (for descriptor format) we cannot 17968 * be certain of the error blkno, so just use the 17969 * request_blkno. 17970 */ 17971 err_blkno = (diskaddr_t)request_blkno; 17972 } 17973 17974 /* 17975 * The following will log the buffer contents for the release driver 17976 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17977 * level is set to verbose. 17978 */ 17979 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17980 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17981 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17982 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17983 17984 if (pfa_flag == FALSE) { 17985 /* This is normally only set for USCSI */ 17986 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17987 return; 17988 } 17989 17990 if ((SD_IS_BUFIO(xp) == TRUE) && 17991 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17992 (severity < sd_error_level))) { 17993 return; 17994 } 17995 } 17996 /* 17997 * Check for Sonoma Failover and keep a count of how many failed I/O's 17998 */ 17999 if ((SD_IS_LSI(un)) && 18000 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 18001 (scsi_sense_asc(sensep) == 0x94) && 18002 (scsi_sense_ascq(sensep) == 0x01)) { 18003 un->un_sonoma_failure_count++; 18004 if (un->un_sonoma_failure_count > 1) { 18005 return; 18006 } 18007 } 18008 18009 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP || 18010 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) && 18011 (pktp->pkt_resid == 0))) { 18012 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 18013 request_blkno, err_blkno, scsi_cmds, 18014 (struct scsi_extended_sense *)sensep, 18015 un->un_additional_codes, NULL); 18016 } 18017 } 18018 18019 /* 18020 * Function: sd_sense_key_no_sense 18021 * 18022 * Description: Recovery action when sense data was not received. 18023 * 18024 * Context: May be called from interrupt context 18025 */ 18026 18027 static void 18028 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 18029 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18030 { 18031 struct sd_sense_info si; 18032 18033 ASSERT(un != NULL); 18034 ASSERT(mutex_owned(SD_MUTEX(un))); 18035 ASSERT(bp != NULL); 18036 ASSERT(xp != NULL); 18037 ASSERT(pktp != NULL); 18038 18039 si.ssi_severity = SCSI_ERR_FATAL; 18040 si.ssi_pfa_flag = FALSE; 18041 18042 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18043 18044 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18045 &si, EIO, (clock_t)0, NULL); 18046 } 18047 18048 18049 /* 18050 * Function: sd_sense_key_recoverable_error 18051 * 18052 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 18053 * 18054 * Context: May be called from interrupt context 18055 */ 18056 18057 static void 18058 sd_sense_key_recoverable_error(struct sd_lun *un, 18059 uint8_t *sense_datap, 18060 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18061 { 18062 struct sd_sense_info si; 18063 uint8_t asc = scsi_sense_asc(sense_datap); 18064 18065 ASSERT(un != NULL); 18066 ASSERT(mutex_owned(SD_MUTEX(un))); 18067 ASSERT(bp != NULL); 18068 ASSERT(xp != NULL); 18069 ASSERT(pktp != NULL); 18070 18071 /* 18072 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 18073 */ 18074 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 18075 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18076 si.ssi_severity = SCSI_ERR_INFO; 18077 si.ssi_pfa_flag = TRUE; 18078 } else { 18079 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18080 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 18081 si.ssi_severity = SCSI_ERR_RECOVERED; 18082 si.ssi_pfa_flag = FALSE; 18083 } 18084 18085 if (pktp->pkt_resid == 0) { 18086 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18087 sd_return_command(un, bp); 18088 return; 18089 } 18090 18091 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18092 &si, EIO, (clock_t)0, NULL); 18093 } 18094 18095 18096 18097 18098 /* 18099 * Function: sd_sense_key_not_ready 18100 * 18101 * Description: Recovery actions for a SCSI "Not Ready" sense key. 18102 * 18103 * Context: May be called from interrupt context 18104 */ 18105 18106 static void 18107 sd_sense_key_not_ready(struct sd_lun *un, 18108 uint8_t *sense_datap, 18109 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18110 { 18111 struct sd_sense_info si; 18112 uint8_t asc = scsi_sense_asc(sense_datap); 18113 uint8_t ascq = scsi_sense_ascq(sense_datap); 18114 18115 ASSERT(un != NULL); 18116 ASSERT(mutex_owned(SD_MUTEX(un))); 18117 ASSERT(bp != NULL); 18118 ASSERT(xp != NULL); 18119 ASSERT(pktp != NULL); 18120 18121 si.ssi_severity = SCSI_ERR_FATAL; 18122 si.ssi_pfa_flag = FALSE; 18123 18124 /* 18125 * Update error stats after first NOT READY error. Disks may have 18126 * been powered down and may need to be restarted. For CDROMs, 18127 * report NOT READY errors only if media is present. 18128 */ 18129 if ((ISCD(un) && (asc == 0x3A)) || 18130 (xp->xb_nr_retry_count > 0)) { 18131 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18132 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 18133 } 18134 18135 /* 18136 * Just fail if the "not ready" retry limit has been reached. 18137 */ 18138 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 18139 /* Special check for error message printing for removables. */ 18140 if (un->un_f_has_removable_media && (asc == 0x04) && 18141 (ascq >= 0x04)) { 18142 si.ssi_severity = SCSI_ERR_ALL; 18143 } 18144 goto fail_command; 18145 } 18146 18147 /* 18148 * Check the ASC and ASCQ in the sense data as needed, to determine 18149 * what to do. 18150 */ 18151 switch (asc) { 18152 case 0x04: /* LOGICAL UNIT NOT READY */ 18153 /* 18154 * disk drives that don't spin up result in a very long delay 18155 * in format without warning messages. We will log a message 18156 * if the error level is set to verbose. 18157 */ 18158 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18159 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18160 "logical unit not ready, resetting disk\n"); 18161 } 18162 18163 /* 18164 * There are different requirements for CDROMs and disks for 18165 * the number of retries. If a CD-ROM is giving this, it is 18166 * probably reading TOC and is in the process of getting 18167 * ready, so we should keep on trying for a long time to make 18168 * sure that all types of media are taken in account (for 18169 * some media the drive takes a long time to read TOC). For 18170 * disks we do not want to retry this too many times as this 18171 * can cause a long hang in format when the drive refuses to 18172 * spin up (a very common failure). 18173 */ 18174 switch (ascq) { 18175 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 18176 /* 18177 * Disk drives frequently refuse to spin up which 18178 * results in a very long hang in format without 18179 * warning messages. 18180 * 18181 * Note: This code preserves the legacy behavior of 18182 * comparing xb_nr_retry_count against zero for fibre 18183 * channel targets instead of comparing against the 18184 * un_reset_retry_count value. The reason for this 18185 * discrepancy has been so utterly lost beneath the 18186 * Sands of Time that even Indiana Jones could not 18187 * find it. 18188 */ 18189 if (un->un_f_is_fibre == TRUE) { 18190 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18191 (xp->xb_nr_retry_count > 0)) && 18192 (un->un_startstop_timeid == NULL)) { 18193 scsi_log(SD_DEVINFO(un), sd_label, 18194 CE_WARN, "logical unit not ready, " 18195 "resetting disk\n"); 18196 sd_reset_target(un, pktp); 18197 } 18198 } else { 18199 if (((sd_level_mask & SD_LOGMASK_DIAG) || 18200 (xp->xb_nr_retry_count > 18201 un->un_reset_retry_count)) && 18202 (un->un_startstop_timeid == NULL)) { 18203 scsi_log(SD_DEVINFO(un), sd_label, 18204 CE_WARN, "logical unit not ready, " 18205 "resetting disk\n"); 18206 sd_reset_target(un, pktp); 18207 } 18208 } 18209 break; 18210 18211 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 18212 /* 18213 * If the target is in the process of becoming 18214 * ready, just proceed with the retry. This can 18215 * happen with CD-ROMs that take a long time to 18216 * read TOC after a power cycle or reset. 18217 */ 18218 goto do_retry; 18219 18220 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 18221 break; 18222 18223 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 18224 /* 18225 * Retries cannot help here so just fail right away. 18226 */ 18227 goto fail_command; 18228 18229 case 0x88: 18230 /* 18231 * Vendor-unique code for T3/T4: it indicates a 18232 * path problem in a mutipathed config, but as far as 18233 * the target driver is concerned it equates to a fatal 18234 * error, so we should just fail the command right away 18235 * (without printing anything to the console). If this 18236 * is not a T3/T4, fall thru to the default recovery 18237 * action. 18238 * T3/T4 is FC only, don't need to check is_fibre 18239 */ 18240 if (SD_IS_T3(un) || SD_IS_T4(un)) { 18241 sd_return_failed_command(un, bp, EIO); 18242 return; 18243 } 18244 /* FALLTHRU */ 18245 18246 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 18247 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 18248 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 18249 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 18250 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 18251 default: /* Possible future codes in SCSI spec? */ 18252 /* 18253 * For removable-media devices, do not retry if 18254 * ASCQ > 2 as these result mostly from USCSI commands 18255 * on MMC devices issued to check status of an 18256 * operation initiated in immediate mode. Also for 18257 * ASCQ >= 4 do not print console messages as these 18258 * mainly represent a user-initiated operation 18259 * instead of a system failure. 18260 */ 18261 if (un->un_f_has_removable_media) { 18262 si.ssi_severity = SCSI_ERR_ALL; 18263 goto fail_command; 18264 } 18265 break; 18266 } 18267 18268 /* 18269 * As part of our recovery attempt for the NOT READY 18270 * condition, we issue a START STOP UNIT command. However 18271 * we want to wait for a short delay before attempting this 18272 * as there may still be more commands coming back from the 18273 * target with the check condition. To do this we use 18274 * timeout(9F) to call sd_start_stop_unit_callback() after 18275 * the delay interval expires. (sd_start_stop_unit_callback() 18276 * dispatches sd_start_stop_unit_task(), which will issue 18277 * the actual START STOP UNIT command. The delay interval 18278 * is one-half of the delay that we will use to retry the 18279 * command that generated the NOT READY condition. 18280 * 18281 * Note that we could just dispatch sd_start_stop_unit_task() 18282 * from here and allow it to sleep for the delay interval, 18283 * but then we would be tying up the taskq thread 18284 * uncesessarily for the duration of the delay. 18285 * 18286 * Do not issue the START STOP UNIT if the current command 18287 * is already a START STOP UNIT. 18288 */ 18289 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18290 break; 18291 } 18292 18293 /* 18294 * Do not schedule the timeout if one is already pending. 18295 */ 18296 if (un->un_startstop_timeid != NULL) { 18297 SD_INFO(SD_LOG_ERROR, un, 18298 "sd_sense_key_not_ready: restart already issued to" 18299 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18300 ddi_get_instance(SD_DEVINFO(un))); 18301 break; 18302 } 18303 18304 /* 18305 * Schedule the START STOP UNIT command, then queue the command 18306 * for a retry. 18307 * 18308 * Note: A timeout is not scheduled for this retry because we 18309 * want the retry to be serial with the START_STOP_UNIT. The 18310 * retry will be started when the START_STOP_UNIT is completed 18311 * in sd_start_stop_unit_task. 18312 */ 18313 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18314 un, un->un_busy_timeout / 2); 18315 xp->xb_nr_retry_count++; 18316 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18317 return; 18318 18319 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18320 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18321 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18322 "unit does not respond to selection\n"); 18323 } 18324 break; 18325 18326 case 0x3A: /* MEDIUM NOT PRESENT */ 18327 if (sd_error_level >= SCSI_ERR_FATAL) { 18328 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18329 "Caddy not inserted in drive\n"); 18330 } 18331 18332 sr_ejected(un); 18333 un->un_mediastate = DKIO_EJECTED; 18334 /* The state has changed, inform the media watch routines */ 18335 cv_broadcast(&un->un_state_cv); 18336 /* Just fail if no media is present in the drive. */ 18337 goto fail_command; 18338 18339 default: 18340 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18341 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18342 "Unit not Ready. Additional sense code 0x%x\n", 18343 asc); 18344 } 18345 break; 18346 } 18347 18348 do_retry: 18349 18350 /* 18351 * Retry the command, as some targets may report NOT READY for 18352 * several seconds after being reset. 18353 */ 18354 xp->xb_nr_retry_count++; 18355 si.ssi_severity = SCSI_ERR_RETRYABLE; 18356 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18357 &si, EIO, un->un_busy_timeout, NULL); 18358 18359 return; 18360 18361 fail_command: 18362 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18363 sd_return_failed_command(un, bp, EIO); 18364 } 18365 18366 18367 18368 /* 18369 * Function: sd_sense_key_medium_or_hardware_error 18370 * 18371 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18372 * sense key. 18373 * 18374 * Context: May be called from interrupt context 18375 */ 18376 18377 static void 18378 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18379 uint8_t *sense_datap, 18380 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18381 { 18382 struct sd_sense_info si; 18383 uint8_t sense_key = scsi_sense_key(sense_datap); 18384 uint8_t asc = scsi_sense_asc(sense_datap); 18385 18386 ASSERT(un != NULL); 18387 ASSERT(mutex_owned(SD_MUTEX(un))); 18388 ASSERT(bp != NULL); 18389 ASSERT(xp != NULL); 18390 ASSERT(pktp != NULL); 18391 18392 si.ssi_severity = SCSI_ERR_FATAL; 18393 si.ssi_pfa_flag = FALSE; 18394 18395 if (sense_key == KEY_MEDIUM_ERROR) { 18396 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18397 } 18398 18399 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18400 18401 if ((un->un_reset_retry_count != 0) && 18402 (xp->xb_retry_count == un->un_reset_retry_count)) { 18403 mutex_exit(SD_MUTEX(un)); 18404 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18405 if (un->un_f_allow_bus_device_reset == TRUE) { 18406 18407 boolean_t try_resetting_target = B_TRUE; 18408 18409 /* 18410 * We need to be able to handle specific ASC when we are 18411 * handling a KEY_HARDWARE_ERROR. In particular 18412 * taking the default action of resetting the target may 18413 * not be the appropriate way to attempt recovery. 18414 * Resetting a target because of a single LUN failure 18415 * victimizes all LUNs on that target. 18416 * 18417 * This is true for the LSI arrays, if an LSI 18418 * array controller returns an ASC of 0x84 (LUN Dead) we 18419 * should trust it. 18420 */ 18421 18422 if (sense_key == KEY_HARDWARE_ERROR) { 18423 switch (asc) { 18424 case 0x84: 18425 if (SD_IS_LSI(un)) { 18426 try_resetting_target = B_FALSE; 18427 } 18428 break; 18429 default: 18430 break; 18431 } 18432 } 18433 18434 if (try_resetting_target == B_TRUE) { 18435 int reset_retval = 0; 18436 if (un->un_f_lun_reset_enabled == TRUE) { 18437 SD_TRACE(SD_LOG_IO_CORE, un, 18438 "sd_sense_key_medium_or_hardware_" 18439 "error: issuing RESET_LUN\n"); 18440 reset_retval = 18441 scsi_reset(SD_ADDRESS(un), 18442 RESET_LUN); 18443 } 18444 if (reset_retval == 0) { 18445 SD_TRACE(SD_LOG_IO_CORE, un, 18446 "sd_sense_key_medium_or_hardware_" 18447 "error: issuing RESET_TARGET\n"); 18448 (void) scsi_reset(SD_ADDRESS(un), 18449 RESET_TARGET); 18450 } 18451 } 18452 } 18453 mutex_enter(SD_MUTEX(un)); 18454 } 18455 18456 /* 18457 * This really ought to be a fatal error, but we will retry anyway 18458 * as some drives report this as a spurious error. 18459 */ 18460 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18461 &si, EIO, (clock_t)0, NULL); 18462 } 18463 18464 18465 18466 /* 18467 * Function: sd_sense_key_illegal_request 18468 * 18469 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18470 * 18471 * Context: May be called from interrupt context 18472 */ 18473 18474 static void 18475 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18476 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18477 { 18478 struct sd_sense_info si; 18479 18480 ASSERT(un != NULL); 18481 ASSERT(mutex_owned(SD_MUTEX(un))); 18482 ASSERT(bp != NULL); 18483 ASSERT(xp != NULL); 18484 ASSERT(pktp != NULL); 18485 18486 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18487 18488 si.ssi_severity = SCSI_ERR_INFO; 18489 si.ssi_pfa_flag = FALSE; 18490 18491 /* Pointless to retry if the target thinks it's an illegal request */ 18492 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18493 sd_return_failed_command(un, bp, EIO); 18494 } 18495 18496 18497 18498 18499 /* 18500 * Function: sd_sense_key_unit_attention 18501 * 18502 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18503 * 18504 * Context: May be called from interrupt context 18505 */ 18506 18507 static void 18508 sd_sense_key_unit_attention(struct sd_lun *un, 18509 uint8_t *sense_datap, 18510 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18511 { 18512 /* 18513 * For UNIT ATTENTION we allow retries for one minute. Devices 18514 * like Sonoma can return UNIT ATTENTION close to a minute 18515 * under certain conditions. 18516 */ 18517 int retry_check_flag = SD_RETRIES_UA; 18518 boolean_t kstat_updated = B_FALSE; 18519 struct sd_sense_info si; 18520 uint8_t asc = scsi_sense_asc(sense_datap); 18521 uint8_t ascq = scsi_sense_ascq(sense_datap); 18522 18523 ASSERT(un != NULL); 18524 ASSERT(mutex_owned(SD_MUTEX(un))); 18525 ASSERT(bp != NULL); 18526 ASSERT(xp != NULL); 18527 ASSERT(pktp != NULL); 18528 18529 si.ssi_severity = SCSI_ERR_INFO; 18530 si.ssi_pfa_flag = FALSE; 18531 18532 18533 switch (asc) { 18534 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18535 if (sd_report_pfa != 0) { 18536 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18537 si.ssi_pfa_flag = TRUE; 18538 retry_check_flag = SD_RETRIES_STANDARD; 18539 goto do_retry; 18540 } 18541 18542 break; 18543 18544 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18545 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18546 un->un_resvd_status |= 18547 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18548 } 18549 #ifdef _LP64 18550 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18551 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18552 un, KM_NOSLEEP) == 0) { 18553 /* 18554 * If we can't dispatch the task we'll just 18555 * live without descriptor sense. We can 18556 * try again on the next "unit attention" 18557 */ 18558 SD_ERROR(SD_LOG_ERROR, un, 18559 "sd_sense_key_unit_attention: " 18560 "Could not dispatch " 18561 "sd_reenable_dsense_task\n"); 18562 } 18563 } 18564 #endif /* _LP64 */ 18565 /* FALLTHRU */ 18566 18567 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18568 if (!un->un_f_has_removable_media) { 18569 break; 18570 } 18571 18572 /* 18573 * When we get a unit attention from a removable-media device, 18574 * it may be in a state that will take a long time to recover 18575 * (e.g., from a reset). Since we are executing in interrupt 18576 * context here, we cannot wait around for the device to come 18577 * back. So hand this command off to sd_media_change_task() 18578 * for deferred processing under taskq thread context. (Note 18579 * that the command still may be failed if a problem is 18580 * encountered at a later time.) 18581 */ 18582 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18583 KM_NOSLEEP) == 0) { 18584 /* 18585 * Cannot dispatch the request so fail the command. 18586 */ 18587 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18588 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18589 si.ssi_severity = SCSI_ERR_FATAL; 18590 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18591 sd_return_failed_command(un, bp, EIO); 18592 } 18593 18594 /* 18595 * If failed to dispatch sd_media_change_task(), we already 18596 * updated kstat. If succeed to dispatch sd_media_change_task(), 18597 * we should update kstat later if it encounters an error. So, 18598 * we update kstat_updated flag here. 18599 */ 18600 kstat_updated = B_TRUE; 18601 18602 /* 18603 * Either the command has been successfully dispatched to a 18604 * task Q for retrying, or the dispatch failed. In either case 18605 * do NOT retry again by calling sd_retry_command. This sets up 18606 * two retries of the same command and when one completes and 18607 * frees the resources the other will access freed memory, 18608 * a bad thing. 18609 */ 18610 return; 18611 18612 default: 18613 break; 18614 } 18615 18616 /* 18617 * ASC ASCQ 18618 * 2A 09 Capacity data has changed 18619 * 2A 01 Mode parameters changed 18620 * 3F 0E Reported luns data has changed 18621 * Arrays that support logical unit expansion should report 18622 * capacity changes(2Ah/09). Mode parameters changed and 18623 * reported luns data has changed are the approximation. 18624 */ 18625 if (((asc == 0x2a) && (ascq == 0x09)) || 18626 ((asc == 0x2a) && (ascq == 0x01)) || 18627 ((asc == 0x3f) && (ascq == 0x0e))) { 18628 if (taskq_dispatch(sd_tq, sd_target_change_task, un, 18629 KM_NOSLEEP) == 0) { 18630 SD_ERROR(SD_LOG_ERROR, un, 18631 "sd_sense_key_unit_attention: " 18632 "Could not dispatch sd_target_change_task\n"); 18633 } 18634 } 18635 18636 /* 18637 * Update kstat if we haven't done that. 18638 */ 18639 if (!kstat_updated) { 18640 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18641 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18642 } 18643 18644 do_retry: 18645 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18646 EIO, SD_UA_RETRY_DELAY, NULL); 18647 } 18648 18649 18650 18651 /* 18652 * Function: sd_sense_key_fail_command 18653 * 18654 * Description: Use to fail a command when we don't like the sense key that 18655 * was returned. 18656 * 18657 * Context: May be called from interrupt context 18658 */ 18659 18660 static void 18661 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18662 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18663 { 18664 struct sd_sense_info si; 18665 18666 ASSERT(un != NULL); 18667 ASSERT(mutex_owned(SD_MUTEX(un))); 18668 ASSERT(bp != NULL); 18669 ASSERT(xp != NULL); 18670 ASSERT(pktp != NULL); 18671 18672 si.ssi_severity = SCSI_ERR_FATAL; 18673 si.ssi_pfa_flag = FALSE; 18674 18675 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18676 sd_return_failed_command(un, bp, EIO); 18677 } 18678 18679 18680 18681 /* 18682 * Function: sd_sense_key_blank_check 18683 * 18684 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18685 * Has no monetary connotation. 18686 * 18687 * Context: May be called from interrupt context 18688 */ 18689 18690 static void 18691 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18692 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18693 { 18694 struct sd_sense_info si; 18695 18696 ASSERT(un != NULL); 18697 ASSERT(mutex_owned(SD_MUTEX(un))); 18698 ASSERT(bp != NULL); 18699 ASSERT(xp != NULL); 18700 ASSERT(pktp != NULL); 18701 18702 /* 18703 * Blank check is not fatal for removable devices, therefore 18704 * it does not require a console message. 18705 */ 18706 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18707 SCSI_ERR_FATAL; 18708 si.ssi_pfa_flag = FALSE; 18709 18710 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18711 sd_return_failed_command(un, bp, EIO); 18712 } 18713 18714 18715 18716 18717 /* 18718 * Function: sd_sense_key_aborted_command 18719 * 18720 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18721 * 18722 * Context: May be called from interrupt context 18723 */ 18724 18725 static void 18726 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18727 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18728 { 18729 struct sd_sense_info si; 18730 18731 ASSERT(un != NULL); 18732 ASSERT(mutex_owned(SD_MUTEX(un))); 18733 ASSERT(bp != NULL); 18734 ASSERT(xp != NULL); 18735 ASSERT(pktp != NULL); 18736 18737 si.ssi_severity = SCSI_ERR_FATAL; 18738 si.ssi_pfa_flag = FALSE; 18739 18740 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18741 18742 /* 18743 * This really ought to be a fatal error, but we will retry anyway 18744 * as some drives report this as a spurious error. 18745 */ 18746 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18747 &si, EIO, drv_usectohz(100000), NULL); 18748 } 18749 18750 18751 18752 /* 18753 * Function: sd_sense_key_default 18754 * 18755 * Description: Default recovery action for several SCSI sense keys (basically 18756 * attempts a retry). 18757 * 18758 * Context: May be called from interrupt context 18759 */ 18760 18761 static void 18762 sd_sense_key_default(struct sd_lun *un, 18763 uint8_t *sense_datap, 18764 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18765 { 18766 struct sd_sense_info si; 18767 uint8_t sense_key = scsi_sense_key(sense_datap); 18768 18769 ASSERT(un != NULL); 18770 ASSERT(mutex_owned(SD_MUTEX(un))); 18771 ASSERT(bp != NULL); 18772 ASSERT(xp != NULL); 18773 ASSERT(pktp != NULL); 18774 18775 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18776 18777 /* 18778 * Undecoded sense key. Attempt retries and hope that will fix 18779 * the problem. Otherwise, we're dead. 18780 */ 18781 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18782 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18783 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18784 } 18785 18786 si.ssi_severity = SCSI_ERR_FATAL; 18787 si.ssi_pfa_flag = FALSE; 18788 18789 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18790 &si, EIO, (clock_t)0, NULL); 18791 } 18792 18793 18794 18795 /* 18796 * Function: sd_print_retry_msg 18797 * 18798 * Description: Print a message indicating the retry action being taken. 18799 * 18800 * Arguments: un - ptr to associated softstate 18801 * bp - ptr to buf(9S) for the command 18802 * arg - not used. 18803 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18804 * or SD_NO_RETRY_ISSUED 18805 * 18806 * Context: May be called from interrupt context 18807 */ 18808 /* ARGSUSED */ 18809 static void 18810 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18811 { 18812 struct sd_xbuf *xp; 18813 struct scsi_pkt *pktp; 18814 char *reasonp; 18815 char *msgp; 18816 18817 ASSERT(un != NULL); 18818 ASSERT(mutex_owned(SD_MUTEX(un))); 18819 ASSERT(bp != NULL); 18820 pktp = SD_GET_PKTP(bp); 18821 ASSERT(pktp != NULL); 18822 xp = SD_GET_XBUF(bp); 18823 ASSERT(xp != NULL); 18824 18825 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18826 mutex_enter(&un->un_pm_mutex); 18827 if ((un->un_state == SD_STATE_SUSPENDED) || 18828 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18829 (pktp->pkt_flags & FLAG_SILENT)) { 18830 mutex_exit(&un->un_pm_mutex); 18831 goto update_pkt_reason; 18832 } 18833 mutex_exit(&un->un_pm_mutex); 18834 18835 /* 18836 * Suppress messages if they are all the same pkt_reason; with 18837 * TQ, many (up to 256) are returned with the same pkt_reason. 18838 * If we are in panic, then suppress the retry messages. 18839 */ 18840 switch (flag) { 18841 case SD_NO_RETRY_ISSUED: 18842 msgp = "giving up"; 18843 break; 18844 case SD_IMMEDIATE_RETRY_ISSUED: 18845 case SD_DELAYED_RETRY_ISSUED: 18846 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18847 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18848 (sd_error_level != SCSI_ERR_ALL))) { 18849 return; 18850 } 18851 msgp = "retrying command"; 18852 break; 18853 default: 18854 goto update_pkt_reason; 18855 } 18856 18857 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18858 scsi_rname(pktp->pkt_reason)); 18859 18860 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 18861 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18862 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18863 } 18864 18865 update_pkt_reason: 18866 /* 18867 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18868 * This is to prevent multiple console messages for the same failure 18869 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18870 * when the command is retried successfully because there still may be 18871 * more commands coming back with the same value of pktp->pkt_reason. 18872 */ 18873 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18874 un->un_last_pkt_reason = pktp->pkt_reason; 18875 } 18876 } 18877 18878 18879 /* 18880 * Function: sd_print_cmd_incomplete_msg 18881 * 18882 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18883 * 18884 * Arguments: un - ptr to associated softstate 18885 * bp - ptr to buf(9S) for the command 18886 * arg - passed to sd_print_retry_msg() 18887 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18888 * or SD_NO_RETRY_ISSUED 18889 * 18890 * Context: May be called from interrupt context 18891 */ 18892 18893 static void 18894 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18895 int code) 18896 { 18897 dev_info_t *dip; 18898 18899 ASSERT(un != NULL); 18900 ASSERT(mutex_owned(SD_MUTEX(un))); 18901 ASSERT(bp != NULL); 18902 18903 switch (code) { 18904 case SD_NO_RETRY_ISSUED: 18905 /* Command was failed. Someone turned off this target? */ 18906 if (un->un_state != SD_STATE_OFFLINE) { 18907 /* 18908 * Suppress message if we are detaching and 18909 * device has been disconnected 18910 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18911 * private interface and not part of the DDI 18912 */ 18913 dip = un->un_sd->sd_dev; 18914 if (!(DEVI_IS_DETACHING(dip) && 18915 DEVI_IS_DEVICE_REMOVED(dip))) { 18916 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18917 "disk not responding to selection\n"); 18918 } 18919 New_state(un, SD_STATE_OFFLINE); 18920 } 18921 break; 18922 18923 case SD_DELAYED_RETRY_ISSUED: 18924 case SD_IMMEDIATE_RETRY_ISSUED: 18925 default: 18926 /* Command was successfully queued for retry */ 18927 sd_print_retry_msg(un, bp, arg, code); 18928 break; 18929 } 18930 } 18931 18932 18933 /* 18934 * Function: sd_pkt_reason_cmd_incomplete 18935 * 18936 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18937 * 18938 * Context: May be called from interrupt context 18939 */ 18940 18941 static void 18942 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18943 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18944 { 18945 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18946 18947 ASSERT(un != NULL); 18948 ASSERT(mutex_owned(SD_MUTEX(un))); 18949 ASSERT(bp != NULL); 18950 ASSERT(xp != NULL); 18951 ASSERT(pktp != NULL); 18952 18953 /* Do not do a reset if selection did not complete */ 18954 /* Note: Should this not just check the bit? */ 18955 if (pktp->pkt_state != STATE_GOT_BUS) { 18956 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18957 sd_reset_target(un, pktp); 18958 } 18959 18960 /* 18961 * If the target was not successfully selected, then set 18962 * SD_RETRIES_FAILFAST to indicate that we lost communication 18963 * with the target, and further retries and/or commands are 18964 * likely to take a long time. 18965 */ 18966 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18967 flag |= SD_RETRIES_FAILFAST; 18968 } 18969 18970 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18971 18972 sd_retry_command(un, bp, flag, 18973 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18974 } 18975 18976 18977 18978 /* 18979 * Function: sd_pkt_reason_cmd_tran_err 18980 * 18981 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18982 * 18983 * Context: May be called from interrupt context 18984 */ 18985 18986 static void 18987 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18988 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18989 { 18990 ASSERT(un != NULL); 18991 ASSERT(mutex_owned(SD_MUTEX(un))); 18992 ASSERT(bp != NULL); 18993 ASSERT(xp != NULL); 18994 ASSERT(pktp != NULL); 18995 18996 /* 18997 * Do not reset if we got a parity error, or if 18998 * selection did not complete. 18999 */ 19000 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19001 /* Note: Should this not just check the bit for pkt_state? */ 19002 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 19003 (pktp->pkt_state != STATE_GOT_BUS)) { 19004 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19005 sd_reset_target(un, pktp); 19006 } 19007 19008 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19009 19010 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19011 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19012 } 19013 19014 19015 19016 /* 19017 * Function: sd_pkt_reason_cmd_reset 19018 * 19019 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 19020 * 19021 * Context: May be called from interrupt context 19022 */ 19023 19024 static void 19025 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 19026 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19027 { 19028 ASSERT(un != NULL); 19029 ASSERT(mutex_owned(SD_MUTEX(un))); 19030 ASSERT(bp != NULL); 19031 ASSERT(xp != NULL); 19032 ASSERT(pktp != NULL); 19033 19034 /* The target may still be running the command, so try to reset. */ 19035 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19036 sd_reset_target(un, pktp); 19037 19038 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19039 19040 /* 19041 * If pkt_reason is CMD_RESET chances are that this pkt got 19042 * reset because another target on this bus caused it. The target 19043 * that caused it should get CMD_TIMEOUT with pkt_statistics 19044 * of STAT_TIMEOUT/STAT_DEV_RESET. 19045 */ 19046 19047 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19048 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19049 } 19050 19051 19052 19053 19054 /* 19055 * Function: sd_pkt_reason_cmd_aborted 19056 * 19057 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 19058 * 19059 * Context: May be called from interrupt context 19060 */ 19061 19062 static void 19063 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 19064 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19065 { 19066 ASSERT(un != NULL); 19067 ASSERT(mutex_owned(SD_MUTEX(un))); 19068 ASSERT(bp != NULL); 19069 ASSERT(xp != NULL); 19070 ASSERT(pktp != NULL); 19071 19072 /* The target may still be running the command, so try to reset. */ 19073 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19074 sd_reset_target(un, pktp); 19075 19076 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19077 19078 /* 19079 * If pkt_reason is CMD_ABORTED chances are that this pkt got 19080 * aborted because another target on this bus caused it. The target 19081 * that caused it should get CMD_TIMEOUT with pkt_statistics 19082 * of STAT_TIMEOUT/STAT_DEV_RESET. 19083 */ 19084 19085 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 19086 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19087 } 19088 19089 19090 19091 /* 19092 * Function: sd_pkt_reason_cmd_timeout 19093 * 19094 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 19095 * 19096 * Context: May be called from interrupt context 19097 */ 19098 19099 static void 19100 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 19101 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19102 { 19103 ASSERT(un != NULL); 19104 ASSERT(mutex_owned(SD_MUTEX(un))); 19105 ASSERT(bp != NULL); 19106 ASSERT(xp != NULL); 19107 ASSERT(pktp != NULL); 19108 19109 19110 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19111 sd_reset_target(un, pktp); 19112 19113 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19114 19115 /* 19116 * A command timeout indicates that we could not establish 19117 * communication with the target, so set SD_RETRIES_FAILFAST 19118 * as further retries/commands are likely to take a long time. 19119 */ 19120 sd_retry_command(un, bp, 19121 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 19122 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19123 } 19124 19125 19126 19127 /* 19128 * Function: sd_pkt_reason_cmd_unx_bus_free 19129 * 19130 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 19131 * 19132 * Context: May be called from interrupt context 19133 */ 19134 19135 static void 19136 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 19137 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19138 { 19139 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 19140 19141 ASSERT(un != NULL); 19142 ASSERT(mutex_owned(SD_MUTEX(un))); 19143 ASSERT(bp != NULL); 19144 ASSERT(xp != NULL); 19145 ASSERT(pktp != NULL); 19146 19147 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19148 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19149 19150 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 19151 sd_print_retry_msg : NULL; 19152 19153 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19154 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19155 } 19156 19157 19158 /* 19159 * Function: sd_pkt_reason_cmd_tag_reject 19160 * 19161 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 19162 * 19163 * Context: May be called from interrupt context 19164 */ 19165 19166 static void 19167 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 19168 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19169 { 19170 ASSERT(un != NULL); 19171 ASSERT(mutex_owned(SD_MUTEX(un))); 19172 ASSERT(bp != NULL); 19173 ASSERT(xp != NULL); 19174 ASSERT(pktp != NULL); 19175 19176 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19177 pktp->pkt_flags = 0; 19178 un->un_tagflags = 0; 19179 if (un->un_f_opt_queueing == TRUE) { 19180 un->un_throttle = min(un->un_throttle, 3); 19181 } else { 19182 un->un_throttle = 1; 19183 } 19184 mutex_exit(SD_MUTEX(un)); 19185 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 19186 mutex_enter(SD_MUTEX(un)); 19187 19188 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19189 19190 /* Legacy behavior not to check retry counts here. */ 19191 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 19192 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19193 } 19194 19195 19196 /* 19197 * Function: sd_pkt_reason_default 19198 * 19199 * Description: Default recovery actions for SCSA pkt_reason values that 19200 * do not have more explicit recovery actions. 19201 * 19202 * Context: May be called from interrupt context 19203 */ 19204 19205 static void 19206 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 19207 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19208 { 19209 ASSERT(un != NULL); 19210 ASSERT(mutex_owned(SD_MUTEX(un))); 19211 ASSERT(bp != NULL); 19212 ASSERT(xp != NULL); 19213 ASSERT(pktp != NULL); 19214 19215 SD_UPDATE_ERRSTATS(un, sd_transerrs); 19216 sd_reset_target(un, pktp); 19217 19218 SD_UPDATE_RESERVATION_STATUS(un, pktp); 19219 19220 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 19221 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 19222 } 19223 19224 19225 19226 /* 19227 * Function: sd_pkt_status_check_condition 19228 * 19229 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 19230 * 19231 * Context: May be called from interrupt context 19232 */ 19233 19234 static void 19235 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 19236 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19237 { 19238 ASSERT(un != NULL); 19239 ASSERT(mutex_owned(SD_MUTEX(un))); 19240 ASSERT(bp != NULL); 19241 ASSERT(xp != NULL); 19242 ASSERT(pktp != NULL); 19243 19244 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 19245 "entry: buf:0x%p xp:0x%p\n", bp, xp); 19246 19247 /* 19248 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 19249 * command will be retried after the request sense). Otherwise, retry 19250 * the command. Note: we are issuing the request sense even though the 19251 * retry limit may have been reached for the failed command. 19252 */ 19253 if (un->un_f_arq_enabled == FALSE) { 19254 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19255 "no ARQ, sending request sense command\n"); 19256 sd_send_request_sense_command(un, bp, pktp); 19257 } else { 19258 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 19259 "ARQ,retrying request sense command\n"); 19260 #if defined(__i386) || defined(__amd64) 19261 /* 19262 * The SD_RETRY_DELAY value need to be adjusted here 19263 * when SD_RETRY_DELAY change in sddef.h 19264 */ 19265 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19266 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 19267 NULL); 19268 #else 19269 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 19270 EIO, SD_RETRY_DELAY, NULL); 19271 #endif 19272 } 19273 19274 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 19275 } 19276 19277 19278 /* 19279 * Function: sd_pkt_status_busy 19280 * 19281 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 19282 * 19283 * Context: May be called from interrupt context 19284 */ 19285 19286 static void 19287 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 19288 struct scsi_pkt *pktp) 19289 { 19290 ASSERT(un != NULL); 19291 ASSERT(mutex_owned(SD_MUTEX(un))); 19292 ASSERT(bp != NULL); 19293 ASSERT(xp != NULL); 19294 ASSERT(pktp != NULL); 19295 19296 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19297 "sd_pkt_status_busy: entry\n"); 19298 19299 /* If retries are exhausted, just fail the command. */ 19300 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19301 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19302 "device busy too long\n"); 19303 sd_return_failed_command(un, bp, EIO); 19304 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19305 "sd_pkt_status_busy: exit\n"); 19306 return; 19307 } 19308 xp->xb_retry_count++; 19309 19310 /* 19311 * Try to reset the target. However, we do not want to perform 19312 * more than one reset if the device continues to fail. The reset 19313 * will be performed when the retry count reaches the reset 19314 * threshold. This threshold should be set such that at least 19315 * one retry is issued before the reset is performed. 19316 */ 19317 if (xp->xb_retry_count == 19318 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19319 int rval = 0; 19320 mutex_exit(SD_MUTEX(un)); 19321 if (un->un_f_allow_bus_device_reset == TRUE) { 19322 /* 19323 * First try to reset the LUN; if we cannot then 19324 * try to reset the target. 19325 */ 19326 if (un->un_f_lun_reset_enabled == TRUE) { 19327 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19328 "sd_pkt_status_busy: RESET_LUN\n"); 19329 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19330 } 19331 if (rval == 0) { 19332 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19333 "sd_pkt_status_busy: RESET_TARGET\n"); 19334 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19335 } 19336 } 19337 if (rval == 0) { 19338 /* 19339 * If the RESET_LUN and/or RESET_TARGET failed, 19340 * try RESET_ALL 19341 */ 19342 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19343 "sd_pkt_status_busy: RESET_ALL\n"); 19344 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19345 } 19346 mutex_enter(SD_MUTEX(un)); 19347 if (rval == 0) { 19348 /* 19349 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19350 * At this point we give up & fail the command. 19351 */ 19352 sd_return_failed_command(un, bp, EIO); 19353 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19354 "sd_pkt_status_busy: exit (failed cmd)\n"); 19355 return; 19356 } 19357 } 19358 19359 /* 19360 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19361 * we have already checked the retry counts above. 19362 */ 19363 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19364 EIO, un->un_busy_timeout, NULL); 19365 19366 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19367 "sd_pkt_status_busy: exit\n"); 19368 } 19369 19370 19371 /* 19372 * Function: sd_pkt_status_reservation_conflict 19373 * 19374 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19375 * command status. 19376 * 19377 * Context: May be called from interrupt context 19378 */ 19379 19380 static void 19381 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19382 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19383 { 19384 ASSERT(un != NULL); 19385 ASSERT(mutex_owned(SD_MUTEX(un))); 19386 ASSERT(bp != NULL); 19387 ASSERT(xp != NULL); 19388 ASSERT(pktp != NULL); 19389 19390 /* 19391 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19392 * conflict could be due to various reasons like incorrect keys, not 19393 * registered or not reserved etc. So, we return EACCES to the caller. 19394 */ 19395 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19396 int cmd = SD_GET_PKT_OPCODE(pktp); 19397 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19398 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19399 sd_return_failed_command(un, bp, EACCES); 19400 return; 19401 } 19402 } 19403 19404 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19405 19406 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19407 if (sd_failfast_enable != 0) { 19408 /* By definition, we must panic here.... */ 19409 sd_panic_for_res_conflict(un); 19410 /*NOTREACHED*/ 19411 } 19412 SD_ERROR(SD_LOG_IO, un, 19413 "sd_handle_resv_conflict: Disk Reserved\n"); 19414 sd_return_failed_command(un, bp, EACCES); 19415 return; 19416 } 19417 19418 /* 19419 * 1147670: retry only if sd_retry_on_reservation_conflict 19420 * property is set (default is 1). Retries will not succeed 19421 * on a disk reserved by another initiator. HA systems 19422 * may reset this via sd.conf to avoid these retries. 19423 * 19424 * Note: The legacy return code for this failure is EIO, however EACCES 19425 * seems more appropriate for a reservation conflict. 19426 */ 19427 if (sd_retry_on_reservation_conflict == 0) { 19428 SD_ERROR(SD_LOG_IO, un, 19429 "sd_handle_resv_conflict: Device Reserved\n"); 19430 sd_return_failed_command(un, bp, EIO); 19431 return; 19432 } 19433 19434 /* 19435 * Retry the command if we can. 19436 * 19437 * Note: The legacy return code for this failure is EIO, however EACCES 19438 * seems more appropriate for a reservation conflict. 19439 */ 19440 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19441 (clock_t)2, NULL); 19442 } 19443 19444 19445 19446 /* 19447 * Function: sd_pkt_status_qfull 19448 * 19449 * Description: Handle a QUEUE FULL condition from the target. This can 19450 * occur if the HBA does not handle the queue full condition. 19451 * (Basically this means third-party HBAs as Sun HBAs will 19452 * handle the queue full condition.) Note that if there are 19453 * some commands already in the transport, then the queue full 19454 * has occurred because the queue for this nexus is actually 19455 * full. If there are no commands in the transport, then the 19456 * queue full is resulting from some other initiator or lun 19457 * consuming all the resources at the target. 19458 * 19459 * Context: May be called from interrupt context 19460 */ 19461 19462 static void 19463 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19464 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19465 { 19466 ASSERT(un != NULL); 19467 ASSERT(mutex_owned(SD_MUTEX(un))); 19468 ASSERT(bp != NULL); 19469 ASSERT(xp != NULL); 19470 ASSERT(pktp != NULL); 19471 19472 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19473 "sd_pkt_status_qfull: entry\n"); 19474 19475 /* 19476 * Just lower the QFULL throttle and retry the command. Note that 19477 * we do not limit the number of retries here. 19478 */ 19479 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19480 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19481 SD_RESTART_TIMEOUT, NULL); 19482 19483 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19484 "sd_pkt_status_qfull: exit\n"); 19485 } 19486 19487 19488 /* 19489 * Function: sd_reset_target 19490 * 19491 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19492 * RESET_TARGET, or RESET_ALL. 19493 * 19494 * Context: May be called under interrupt context. 19495 */ 19496 19497 static void 19498 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19499 { 19500 int rval = 0; 19501 19502 ASSERT(un != NULL); 19503 ASSERT(mutex_owned(SD_MUTEX(un))); 19504 ASSERT(pktp != NULL); 19505 19506 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19507 19508 /* 19509 * No need to reset if the transport layer has already done so. 19510 */ 19511 if ((pktp->pkt_statistics & 19512 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19513 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19514 "sd_reset_target: no reset\n"); 19515 return; 19516 } 19517 19518 mutex_exit(SD_MUTEX(un)); 19519 19520 if (un->un_f_allow_bus_device_reset == TRUE) { 19521 if (un->un_f_lun_reset_enabled == TRUE) { 19522 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19523 "sd_reset_target: RESET_LUN\n"); 19524 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19525 } 19526 if (rval == 0) { 19527 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19528 "sd_reset_target: RESET_TARGET\n"); 19529 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19530 } 19531 } 19532 19533 if (rval == 0) { 19534 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19535 "sd_reset_target: RESET_ALL\n"); 19536 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19537 } 19538 19539 mutex_enter(SD_MUTEX(un)); 19540 19541 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19542 } 19543 19544 /* 19545 * Function: sd_target_change_task 19546 * 19547 * Description: Handle dynamic target change 19548 * 19549 * Context: Executes in a taskq() thread context 19550 */ 19551 static void 19552 sd_target_change_task(void *arg) 19553 { 19554 struct sd_lun *un = arg; 19555 diskaddr_t label_cap; 19556 sd_ssc_t *ssc; 19557 19558 ASSERT(un != NULL); 19559 ASSERT(!mutex_owned(SD_MUTEX(un))); 19560 19561 if ((un->un_f_blockcount_is_valid == FALSE) || 19562 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 19563 return; 19564 } 19565 19566 ssc = sd_ssc_init(un); 19567 19568 if (sd_read_capacity(ssc, SD_PATH_DIRECT) != 0) { 19569 SD_ERROR(SD_LOG_ERROR, un, 19570 "sd_target_change_task: fail to read capacity\n"); 19571 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19572 goto task_exit; 19573 } 19574 19575 /* 19576 * If lun is EFI labeled and lun capacity is greater than the 19577 * capacity contained in the label, log a sys event. 19578 */ 19579 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 19580 (void*)SD_PATH_DIRECT) == 0) { 19581 mutex_enter(SD_MUTEX(un)); 19582 if (un->un_f_blockcount_is_valid && 19583 un->un_blockcount > label_cap) { 19584 mutex_exit(SD_MUTEX(un)); 19585 sd_log_lun_expansion_event(un, KM_SLEEP); 19586 } else { 19587 mutex_exit(SD_MUTEX(un)); 19588 } 19589 } 19590 19591 task_exit: 19592 sd_ssc_fini(ssc); 19593 } 19594 19595 19596 /* 19597 * Function: sd_log_dev_status_event 19598 * 19599 * Description: Log EC_dev_status sysevent 19600 * 19601 * Context: Never called from interrupt context 19602 */ 19603 static void 19604 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag) 19605 { 19606 int err; 19607 char *path; 19608 nvlist_t *attr_list; 19609 19610 /* Allocate and build sysevent attribute list */ 19611 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag); 19612 if (err != 0) { 19613 SD_ERROR(SD_LOG_ERROR, un, 19614 "sd_log_dev_status_event: fail to allocate space\n"); 19615 return; 19616 } 19617 19618 path = kmem_alloc(MAXPATHLEN, km_flag); 19619 if (path == NULL) { 19620 nvlist_free(attr_list); 19621 SD_ERROR(SD_LOG_ERROR, un, 19622 "sd_log_dev_status_event: fail to allocate space\n"); 19623 return; 19624 } 19625 /* 19626 * Add path attribute to identify the lun. 19627 * We are using minor node 'a' as the sysevent attribute. 19628 */ 19629 (void) snprintf(path, MAXPATHLEN, "/devices"); 19630 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path)); 19631 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path), 19632 ":a"); 19633 19634 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path); 19635 if (err != 0) { 19636 nvlist_free(attr_list); 19637 kmem_free(path, MAXPATHLEN); 19638 SD_ERROR(SD_LOG_ERROR, un, 19639 "sd_log_dev_status_event: fail to add attribute\n"); 19640 return; 19641 } 19642 19643 /* Log dynamic lun expansion sysevent */ 19644 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS, 19645 esc, attr_list, NULL, km_flag); 19646 if (err != DDI_SUCCESS) { 19647 SD_ERROR(SD_LOG_ERROR, un, 19648 "sd_log_dev_status_event: fail to log sysevent\n"); 19649 } 19650 19651 nvlist_free(attr_list); 19652 kmem_free(path, MAXPATHLEN); 19653 } 19654 19655 19656 /* 19657 * Function: sd_log_lun_expansion_event 19658 * 19659 * Description: Log lun expansion sys event 19660 * 19661 * Context: Never called from interrupt context 19662 */ 19663 static void 19664 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag) 19665 { 19666 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag); 19667 } 19668 19669 19670 /* 19671 * Function: sd_log_eject_request_event 19672 * 19673 * Description: Log eject request sysevent 19674 * 19675 * Context: Never called from interrupt context 19676 */ 19677 static void 19678 sd_log_eject_request_event(struct sd_lun *un, int km_flag) 19679 { 19680 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag); 19681 } 19682 19683 19684 /* 19685 * Function: sd_media_change_task 19686 * 19687 * Description: Recovery action for CDROM to become available. 19688 * 19689 * Context: Executes in a taskq() thread context 19690 */ 19691 19692 static void 19693 sd_media_change_task(void *arg) 19694 { 19695 struct scsi_pkt *pktp = arg; 19696 struct sd_lun *un; 19697 struct buf *bp; 19698 struct sd_xbuf *xp; 19699 int err = 0; 19700 int retry_count = 0; 19701 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19702 struct sd_sense_info si; 19703 19704 ASSERT(pktp != NULL); 19705 bp = (struct buf *)pktp->pkt_private; 19706 ASSERT(bp != NULL); 19707 xp = SD_GET_XBUF(bp); 19708 ASSERT(xp != NULL); 19709 un = SD_GET_UN(bp); 19710 ASSERT(un != NULL); 19711 ASSERT(!mutex_owned(SD_MUTEX(un))); 19712 ASSERT(un->un_f_monitor_media_state); 19713 19714 si.ssi_severity = SCSI_ERR_INFO; 19715 si.ssi_pfa_flag = FALSE; 19716 19717 /* 19718 * When a reset is issued on a CDROM, it takes a long time to 19719 * recover. First few attempts to read capacity and other things 19720 * related to handling unit attention fail (with a ASC 0x4 and 19721 * ASCQ 0x1). In that case we want to do enough retries and we want 19722 * to limit the retries in other cases of genuine failures like 19723 * no media in drive. 19724 */ 19725 while (retry_count++ < retry_limit) { 19726 if ((err = sd_handle_mchange(un)) == 0) { 19727 break; 19728 } 19729 if (err == EAGAIN) { 19730 retry_limit = SD_UNIT_ATTENTION_RETRY; 19731 } 19732 /* Sleep for 0.5 sec. & try again */ 19733 delay(drv_usectohz(500000)); 19734 } 19735 19736 /* 19737 * Dispatch (retry or fail) the original command here, 19738 * along with appropriate console messages.... 19739 * 19740 * Must grab the mutex before calling sd_retry_command, 19741 * sd_print_sense_msg and sd_return_failed_command. 19742 */ 19743 mutex_enter(SD_MUTEX(un)); 19744 if (err != SD_CMD_SUCCESS) { 19745 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19746 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19747 si.ssi_severity = SCSI_ERR_FATAL; 19748 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19749 sd_return_failed_command(un, bp, EIO); 19750 } else { 19751 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg, 19752 &si, EIO, (clock_t)0, NULL); 19753 } 19754 mutex_exit(SD_MUTEX(un)); 19755 } 19756 19757 19758 19759 /* 19760 * Function: sd_handle_mchange 19761 * 19762 * Description: Perform geometry validation & other recovery when CDROM 19763 * has been removed from drive. 19764 * 19765 * Return Code: 0 for success 19766 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19767 * sd_send_scsi_READ_CAPACITY() 19768 * 19769 * Context: Executes in a taskq() thread context 19770 */ 19771 19772 static int 19773 sd_handle_mchange(struct sd_lun *un) 19774 { 19775 int rval; 19776 sd_ssc_t *ssc; 19777 19778 ASSERT(!mutex_owned(SD_MUTEX(un))); 19779 ASSERT(un->un_f_monitor_media_state); 19780 19781 ssc = sd_ssc_init(un); 19782 rval = sd_read_capacity(ssc, SD_PATH_DIRECT_PRIORITY); 19783 19784 if (rval != 0) 19785 goto failed; 19786 19787 mutex_enter(SD_MUTEX(un)); 19788 19789 if (un->un_errstats != NULL) { 19790 struct sd_errstats *stp = 19791 (struct sd_errstats *)un->un_errstats->ks_data; 19792 stp->sd_capacity.value.ui64 = (uint64_t) 19793 ((uint64_t)un->un_blockcount * 19794 (uint64_t)un->un_tgt_blocksize); 19795 } 19796 19797 /* 19798 * Check if the media in the device is writable or not 19799 */ 19800 if (ISCD(un)) { 19801 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY); 19802 } 19803 19804 /* 19805 * Note: Maybe let the strategy/partitioning chain worry about getting 19806 * valid geometry. 19807 */ 19808 mutex_exit(SD_MUTEX(un)); 19809 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 19810 19811 19812 if (cmlb_validate(un->un_cmlbhandle, 0, 19813 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 19814 sd_ssc_fini(ssc); 19815 return (EIO); 19816 } else { 19817 if (un->un_f_pkstats_enabled) { 19818 sd_set_pstats(un); 19819 SD_TRACE(SD_LOG_IO_PARTITION, un, 19820 "sd_handle_mchange: un:0x%p pstats created and " 19821 "set\n", un); 19822 } 19823 } 19824 19825 /* 19826 * Try to lock the door 19827 */ 19828 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 19829 SD_PATH_DIRECT_PRIORITY); 19830 failed: 19831 if (rval != 0) 19832 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19833 sd_ssc_fini(ssc); 19834 return (rval); 19835 } 19836 19837 19838 /* 19839 * Function: sd_send_scsi_DOORLOCK 19840 * 19841 * Description: Issue the scsi DOOR LOCK command 19842 * 19843 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19844 * structure for this target. 19845 * flag - SD_REMOVAL_ALLOW 19846 * SD_REMOVAL_PREVENT 19847 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19848 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19849 * to use the USCSI "direct" chain and bypass the normal 19850 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19851 * command is issued as part of an error recovery action. 19852 * 19853 * Return Code: 0 - Success 19854 * errno return code from sd_ssc_send() 19855 * 19856 * Context: Can sleep. 19857 */ 19858 19859 static int 19860 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag) 19861 { 19862 struct scsi_extended_sense sense_buf; 19863 union scsi_cdb cdb; 19864 struct uscsi_cmd ucmd_buf; 19865 int status; 19866 struct sd_lun *un; 19867 19868 ASSERT(ssc != NULL); 19869 un = ssc->ssc_un; 19870 ASSERT(un != NULL); 19871 ASSERT(!mutex_owned(SD_MUTEX(un))); 19872 19873 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19874 19875 /* already determined doorlock is not supported, fake success */ 19876 if (un->un_f_doorlock_supported == FALSE) { 19877 return (0); 19878 } 19879 19880 /* 19881 * If we are ejecting and see an SD_REMOVAL_PREVENT 19882 * ignore the command so we can complete the eject 19883 * operation. 19884 */ 19885 if (flag == SD_REMOVAL_PREVENT) { 19886 mutex_enter(SD_MUTEX(un)); 19887 if (un->un_f_ejecting == TRUE) { 19888 mutex_exit(SD_MUTEX(un)); 19889 return (EAGAIN); 19890 } 19891 mutex_exit(SD_MUTEX(un)); 19892 } 19893 19894 bzero(&cdb, sizeof (cdb)); 19895 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19896 19897 cdb.scc_cmd = SCMD_DOORLOCK; 19898 cdb.cdb_opaque[4] = (uchar_t)flag; 19899 19900 ucmd_buf.uscsi_cdb = (char *)&cdb; 19901 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19902 ucmd_buf.uscsi_bufaddr = NULL; 19903 ucmd_buf.uscsi_buflen = 0; 19904 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19905 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19906 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19907 ucmd_buf.uscsi_timeout = 15; 19908 19909 SD_TRACE(SD_LOG_IO, un, 19910 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n"); 19911 19912 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19913 UIO_SYSSPACE, path_flag); 19914 19915 if (status == 0) 19916 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19917 19918 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19919 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19920 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19921 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19922 19923 /* fake success and skip subsequent doorlock commands */ 19924 un->un_f_doorlock_supported = FALSE; 19925 return (0); 19926 } 19927 19928 return (status); 19929 } 19930 19931 /* 19932 * Function: sd_send_scsi_READ_CAPACITY 19933 * 19934 * Description: This routine uses the scsi READ CAPACITY command to determine 19935 * the device capacity in number of blocks and the device native 19936 * block size. If this function returns a failure, then the 19937 * values in *capp and *lbap are undefined. If the capacity 19938 * returned is 0xffffffff then the lun is too large for a 19939 * normal READ CAPACITY command and the results of a 19940 * READ CAPACITY 16 will be used instead. 19941 * 19942 * Arguments: ssc - ssc contains ptr to soft state struct for the target 19943 * capp - ptr to unsigned 64-bit variable to receive the 19944 * capacity value from the command. 19945 * lbap - ptr to unsigned 32-bit varaible to receive the 19946 * block size value from the command 19947 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19948 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19949 * to use the USCSI "direct" chain and bypass the normal 19950 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19951 * command is issued as part of an error recovery action. 19952 * 19953 * Return Code: 0 - Success 19954 * EIO - IO error 19955 * EACCES - Reservation conflict detected 19956 * EAGAIN - Device is becoming ready 19957 * errno return code from sd_ssc_send() 19958 * 19959 * Context: Can sleep. Blocks until command completes. 19960 */ 19961 19962 static int 19963 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap, 19964 int path_flag) 19965 { 19966 struct scsi_extended_sense sense_buf; 19967 struct uscsi_cmd ucmd_buf; 19968 union scsi_cdb cdb; 19969 struct scsi_capacity capacity_buf; 19970 uint64_t capacity; 19971 uint32_t lbasize; 19972 uint32_t pbsize; 19973 int status; 19974 struct sd_lun *un; 19975 19976 ASSERT(ssc != NULL); 19977 19978 un = ssc->ssc_un; 19979 ASSERT(un != NULL); 19980 ASSERT(!mutex_owned(SD_MUTEX(un))); 19981 ASSERT(capp != NULL); 19982 ASSERT(lbap != NULL); 19983 19984 SD_TRACE(SD_LOG_IO, un, 19985 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19986 19987 /* 19988 * First send a READ_CAPACITY command to the target. 19989 * (This command is mandatory under SCSI-2.) 19990 * 19991 * Set up the CDB for the READ_CAPACITY command. The Partial 19992 * Medium Indicator bit is cleared. The address field must be 19993 * zero if the PMI bit is zero. 19994 */ 19995 bzero(&cdb, sizeof (cdb)); 19996 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19997 bzero(&capacity_buf, sizeof (capacity_buf)); 19998 19999 cdb.scc_cmd = SCMD_READ_CAPACITY; 20000 20001 ucmd_buf.uscsi_cdb = (char *)&cdb; 20002 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20003 ucmd_buf.uscsi_bufaddr = (caddr_t)&capacity_buf; 20004 ucmd_buf.uscsi_buflen = sizeof (capacity_buf); 20005 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20006 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20007 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20008 ucmd_buf.uscsi_timeout = 60; 20009 20010 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20011 UIO_SYSSPACE, path_flag); 20012 20013 switch (status) { 20014 case 0: 20015 /* Return failure if we did not get valid capacity data. */ 20016 if (ucmd_buf.uscsi_resid != 0) { 20017 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20018 "sd_send_scsi_READ_CAPACITY received invalid " 20019 "capacity data"); 20020 return (EIO); 20021 } 20022 /* 20023 * Read capacity and block size from the READ CAPACITY 10 data. 20024 * This data may be adjusted later due to device specific 20025 * issues. 20026 * 20027 * According to the SCSI spec, the READ CAPACITY 10 20028 * command returns the following: 20029 * 20030 * bytes 0-3: Maximum logical block address available. 20031 * (MSB in byte:0 & LSB in byte:3) 20032 * 20033 * bytes 4-7: Block length in bytes 20034 * (MSB in byte:4 & LSB in byte:7) 20035 * 20036 */ 20037 capacity = BE_32(capacity_buf.capacity); 20038 lbasize = BE_32(capacity_buf.lbasize); 20039 20040 /* 20041 * if the reported capacity is set to all 0xf's, then 20042 * this disk is too large and requires SBC-2 commands. 20043 * Reissue the request using READ CAPACITY 16. 20044 */ 20045 if (capacity == 0xffffffff) { 20046 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20047 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, 20048 &lbasize, &pbsize, path_flag); 20049 if (status != 0) { 20050 return (status); 20051 } else { 20052 goto rc16_done; 20053 } 20054 } 20055 break; /* Success! */ 20056 case EIO: 20057 switch (ucmd_buf.uscsi_status) { 20058 case STATUS_RESERVATION_CONFLICT: 20059 status = EACCES; 20060 break; 20061 case STATUS_CHECK: 20062 /* 20063 * Check condition; look for ASC/ASCQ of 0x04/0x01 20064 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20065 */ 20066 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20067 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20068 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20069 return (EAGAIN); 20070 } 20071 break; 20072 default: 20073 break; 20074 } 20075 /* FALLTHRU */ 20076 default: 20077 return (status); 20078 } 20079 20080 /* 20081 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20082 * (2352 and 0 are common) so for these devices always force the value 20083 * to 2048 as required by the ATAPI specs. 20084 */ 20085 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20086 lbasize = 2048; 20087 } 20088 20089 /* 20090 * Get the maximum LBA value from the READ CAPACITY data. 20091 * Here we assume that the Partial Medium Indicator (PMI) bit 20092 * was cleared when issuing the command. This means that the LBA 20093 * returned from the device is the LBA of the last logical block 20094 * on the logical unit. The actual logical block count will be 20095 * this value plus one. 20096 */ 20097 capacity += 1; 20098 20099 rc16_done: 20100 20101 /* 20102 * Copy the values from the READ CAPACITY command into the space 20103 * provided by the caller. 20104 */ 20105 *capp = capacity; 20106 *lbap = lbasize; 20107 20108 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 20109 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 20110 20111 /* 20112 * Both the lbasize and capacity from the device must be nonzero, 20113 * otherwise we assume that the values are not valid and return 20114 * failure to the caller. (4203735) 20115 */ 20116 if ((capacity == 0) || (lbasize == 0)) { 20117 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20118 "sd_send_scsi_READ_CAPACITY received invalid value " 20119 "capacity %llu lbasize %d", capacity, lbasize); 20120 return (EIO); 20121 } 20122 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20123 return (0); 20124 } 20125 20126 /* 20127 * Function: sd_send_scsi_READ_CAPACITY_16 20128 * 20129 * Description: This routine uses the scsi READ CAPACITY 16 command to 20130 * determine the device capacity in number of blocks and the 20131 * device native block size. If this function returns a failure, 20132 * then the values in *capp and *lbap are undefined. 20133 * This routine should be called by sd_send_scsi_READ_CAPACITY 20134 * which will apply any device specific adjustments to capacity 20135 * and lbasize. One exception is it is also called by 20136 * sd_get_media_info_ext. In that function, there is no need to 20137 * adjust the capacity and lbasize. 20138 * 20139 * Arguments: ssc - ssc contains ptr to soft state struct for the target 20140 * capp - ptr to unsigned 64-bit variable to receive the 20141 * capacity value from the command. 20142 * lbap - ptr to unsigned 32-bit varaible to receive the 20143 * block size value from the command 20144 * psp - ptr to unsigned 32-bit variable to receive the 20145 * physical block size value from the command 20146 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20147 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20148 * to use the USCSI "direct" chain and bypass the normal 20149 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 20150 * this command is issued as part of an error recovery 20151 * action. 20152 * 20153 * Return Code: 0 - Success 20154 * EIO - IO error 20155 * EACCES - Reservation conflict detected 20156 * EAGAIN - Device is becoming ready 20157 * errno return code from sd_ssc_send() 20158 * 20159 * Context: Can sleep. Blocks until command completes. 20160 */ 20161 20162 static int 20163 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 20164 uint32_t *lbap, uint32_t *psp, int path_flag) 20165 { 20166 struct scsi_extended_sense sense_buf; 20167 struct uscsi_cmd ucmd_buf; 20168 union scsi_cdb cdb; 20169 struct scsi_capacity_16 capacity16_buf; 20170 uint64_t capacity; 20171 uint32_t lbasize; 20172 uint32_t pbsize; 20173 uint32_t lbpb_exp; 20174 int status; 20175 struct sd_lun *un; 20176 20177 ASSERT(ssc != NULL); 20178 20179 un = ssc->ssc_un; 20180 ASSERT(un != NULL); 20181 ASSERT(!mutex_owned(SD_MUTEX(un))); 20182 ASSERT(capp != NULL); 20183 ASSERT(lbap != NULL); 20184 20185 SD_TRACE(SD_LOG_IO, un, 20186 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 20187 20188 /* 20189 * First send a READ_CAPACITY_16 command to the target. 20190 * 20191 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 20192 * Medium Indicator bit is cleared. The address field must be 20193 * zero if the PMI bit is zero. 20194 */ 20195 bzero(&cdb, sizeof (cdb)); 20196 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20197 bzero(&capacity16_buf, sizeof (capacity16_buf)); 20198 20199 ucmd_buf.uscsi_cdb = (char *)&cdb; 20200 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 20201 ucmd_buf.uscsi_bufaddr = (caddr_t)&capacity16_buf; 20202 ucmd_buf.uscsi_buflen = sizeof (capacity16_buf); 20203 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20204 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 20205 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20206 ucmd_buf.uscsi_timeout = 60; 20207 20208 /* 20209 * Read Capacity (16) is a Service Action In command. One 20210 * command byte (0x9E) is overloaded for multiple operations, 20211 * with the second CDB byte specifying the desired operation 20212 */ 20213 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 20214 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 20215 20216 /* 20217 * Fill in allocation length field 20218 */ 20219 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 20220 20221 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20222 UIO_SYSSPACE, path_flag); 20223 20224 switch (status) { 20225 case 0: 20226 /* Return failure if we did not get valid capacity data. */ 20227 if (ucmd_buf.uscsi_resid > 20) { 20228 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20229 "sd_send_scsi_READ_CAPACITY_16 received invalid " 20230 "capacity data"); 20231 return (EIO); 20232 } 20233 20234 /* 20235 * Read capacity and block size from the READ CAPACITY 16 data. 20236 * This data may be adjusted later due to device specific 20237 * issues. 20238 * 20239 * According to the SCSI spec, the READ CAPACITY 16 20240 * command returns the following: 20241 * 20242 * bytes 0-7: Maximum logical block address available. 20243 * (MSB in byte:0 & LSB in byte:7) 20244 * 20245 * bytes 8-11: Block length in bytes 20246 * (MSB in byte:8 & LSB in byte:11) 20247 * 20248 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT 20249 */ 20250 capacity = BE_64(capacity16_buf.sc_capacity); 20251 lbasize = BE_32(capacity16_buf.sc_lbasize); 20252 lbpb_exp = capacity16_buf.sc_l2p_exponent; 20253 20254 pbsize = lbasize << lbpb_exp; 20255 20256 /* 20257 * if the reported capacity is set to all 0xf's, then 20258 * this disk is too large. This could only happen with 20259 * a device that supports LBAs larger than 64 bits which 20260 * are not defined by any current T10 standards. 20261 */ 20262 if (capacity == 0xffffffffffffffff) { 20263 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20264 "disk is too large"); 20265 return (EIO); 20266 } 20267 break; /* Success! */ 20268 case EIO: 20269 switch (ucmd_buf.uscsi_status) { 20270 case STATUS_RESERVATION_CONFLICT: 20271 status = EACCES; 20272 break; 20273 case STATUS_CHECK: 20274 /* 20275 * Check condition; look for ASC/ASCQ of 0x04/0x01 20276 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 20277 */ 20278 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20279 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 20280 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 20281 return (EAGAIN); 20282 } 20283 break; 20284 default: 20285 break; 20286 } 20287 /* FALLTHRU */ 20288 default: 20289 return (status); 20290 } 20291 20292 /* 20293 * Some ATAPI CD-ROM drives report inaccurate LBA size values 20294 * (2352 and 0 are common) so for these devices always force the value 20295 * to 2048 as required by the ATAPI specs. 20296 */ 20297 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 20298 lbasize = 2048; 20299 } 20300 20301 /* 20302 * Get the maximum LBA value from the READ CAPACITY 16 data. 20303 * Here we assume that the Partial Medium Indicator (PMI) bit 20304 * was cleared when issuing the command. This means that the LBA 20305 * returned from the device is the LBA of the last logical block 20306 * on the logical unit. The actual logical block count will be 20307 * this value plus one. 20308 */ 20309 capacity += 1; 20310 20311 *capp = capacity; 20312 *lbap = lbasize; 20313 *psp = pbsize; 20314 20315 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 20316 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n", 20317 capacity, lbasize, pbsize); 20318 20319 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) { 20320 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20321 "sd_send_scsi_READ_CAPACITY_16 received invalid value " 20322 "capacity %llu lbasize %d pbsize %d", capacity, lbasize); 20323 return (EIO); 20324 } 20325 20326 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20327 return (0); 20328 } 20329 20330 20331 /* 20332 * Function: sd_send_scsi_START_STOP_UNIT 20333 * 20334 * Description: Issue a scsi START STOP UNIT command to the target. 20335 * 20336 * Arguments: ssc - ssc contatins pointer to driver soft state (unit) 20337 * structure for this target. 20338 * pc_flag - SD_POWER_CONDITION 20339 * SD_START_STOP 20340 * flag - SD_TARGET_START 20341 * SD_TARGET_STOP 20342 * SD_TARGET_EJECT 20343 * SD_TARGET_CLOSE 20344 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20345 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20346 * to use the USCSI "direct" chain and bypass the normal 20347 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 20348 * command is issued as part of an error recovery action. 20349 * 20350 * Return Code: 0 - Success 20351 * EIO - IO error 20352 * EACCES - Reservation conflict detected 20353 * ENXIO - Not Ready, medium not present 20354 * errno return code from sd_ssc_send() 20355 * 20356 * Context: Can sleep. 20357 */ 20358 20359 static int 20360 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag, 20361 int path_flag) 20362 { 20363 struct scsi_extended_sense sense_buf; 20364 union scsi_cdb cdb; 20365 struct uscsi_cmd ucmd_buf; 20366 int status; 20367 struct sd_lun *un; 20368 20369 ASSERT(ssc != NULL); 20370 un = ssc->ssc_un; 20371 ASSERT(un != NULL); 20372 ASSERT(!mutex_owned(SD_MUTEX(un))); 20373 20374 SD_TRACE(SD_LOG_IO, un, 20375 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 20376 20377 if (un->un_f_check_start_stop && 20378 (pc_flag == SD_START_STOP) && 20379 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 20380 (un->un_f_start_stop_supported != TRUE)) { 20381 return (0); 20382 } 20383 20384 /* 20385 * If we are performing an eject operation and 20386 * we receive any command other than SD_TARGET_EJECT 20387 * we should immediately return. 20388 */ 20389 if (flag != SD_TARGET_EJECT) { 20390 mutex_enter(SD_MUTEX(un)); 20391 if (un->un_f_ejecting == TRUE) { 20392 mutex_exit(SD_MUTEX(un)); 20393 return (EAGAIN); 20394 } 20395 mutex_exit(SD_MUTEX(un)); 20396 } 20397 20398 bzero(&cdb, sizeof (cdb)); 20399 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20400 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20401 20402 cdb.scc_cmd = SCMD_START_STOP; 20403 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ? 20404 (uchar_t)(flag << 4) : (uchar_t)flag; 20405 20406 ucmd_buf.uscsi_cdb = (char *)&cdb; 20407 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20408 ucmd_buf.uscsi_bufaddr = NULL; 20409 ucmd_buf.uscsi_buflen = 0; 20410 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20411 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20412 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20413 ucmd_buf.uscsi_timeout = 200; 20414 20415 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20416 UIO_SYSSPACE, path_flag); 20417 20418 switch (status) { 20419 case 0: 20420 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20421 break; /* Success! */ 20422 case EIO: 20423 switch (ucmd_buf.uscsi_status) { 20424 case STATUS_RESERVATION_CONFLICT: 20425 status = EACCES; 20426 break; 20427 case STATUS_CHECK: 20428 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 20429 switch (scsi_sense_key( 20430 (uint8_t *)&sense_buf)) { 20431 case KEY_ILLEGAL_REQUEST: 20432 status = ENOTSUP; 20433 break; 20434 case KEY_NOT_READY: 20435 if (scsi_sense_asc( 20436 (uint8_t *)&sense_buf) 20437 == 0x3A) { 20438 status = ENXIO; 20439 } 20440 break; 20441 default: 20442 break; 20443 } 20444 } 20445 break; 20446 default: 20447 break; 20448 } 20449 break; 20450 default: 20451 break; 20452 } 20453 20454 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 20455 20456 return (status); 20457 } 20458 20459 20460 /* 20461 * Function: sd_start_stop_unit_callback 20462 * 20463 * Description: timeout(9F) callback to begin recovery process for a 20464 * device that has spun down. 20465 * 20466 * Arguments: arg - pointer to associated softstate struct. 20467 * 20468 * Context: Executes in a timeout(9F) thread context 20469 */ 20470 20471 static void 20472 sd_start_stop_unit_callback(void *arg) 20473 { 20474 struct sd_lun *un = arg; 20475 ASSERT(un != NULL); 20476 ASSERT(!mutex_owned(SD_MUTEX(un))); 20477 20478 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 20479 20480 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 20481 } 20482 20483 20484 /* 20485 * Function: sd_start_stop_unit_task 20486 * 20487 * Description: Recovery procedure when a drive is spun down. 20488 * 20489 * Arguments: arg - pointer to associated softstate struct. 20490 * 20491 * Context: Executes in a taskq() thread context 20492 */ 20493 20494 static void 20495 sd_start_stop_unit_task(void *arg) 20496 { 20497 struct sd_lun *un = arg; 20498 sd_ssc_t *ssc; 20499 int power_level; 20500 int rval; 20501 20502 ASSERT(un != NULL); 20503 ASSERT(!mutex_owned(SD_MUTEX(un))); 20504 20505 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 20506 20507 /* 20508 * Some unformatted drives report not ready error, no need to 20509 * restart if format has been initiated. 20510 */ 20511 mutex_enter(SD_MUTEX(un)); 20512 if (un->un_f_format_in_progress == TRUE) { 20513 mutex_exit(SD_MUTEX(un)); 20514 return; 20515 } 20516 mutex_exit(SD_MUTEX(un)); 20517 20518 ssc = sd_ssc_init(un); 20519 /* 20520 * When a START STOP command is issued from here, it is part of a 20521 * failure recovery operation and must be issued before any other 20522 * commands, including any pending retries. Thus it must be sent 20523 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 20524 * succeeds or not, we will start I/O after the attempt. 20525 * If power condition is supported and the current power level 20526 * is capable of performing I/O, we should set the power condition 20527 * to that level. Otherwise, set the power condition to ACTIVE. 20528 */ 20529 if (un->un_f_power_condition_supported) { 20530 mutex_enter(SD_MUTEX(un)); 20531 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level)); 20532 power_level = sd_pwr_pc.ran_perf[un->un_power_level] 20533 > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE; 20534 mutex_exit(SD_MUTEX(un)); 20535 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION, 20536 sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY); 20537 } else { 20538 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 20539 SD_TARGET_START, SD_PATH_DIRECT_PRIORITY); 20540 } 20541 20542 if (rval != 0) 20543 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 20544 sd_ssc_fini(ssc); 20545 /* 20546 * The above call blocks until the START_STOP_UNIT command completes. 20547 * Now that it has completed, we must re-try the original IO that 20548 * received the NOT READY condition in the first place. There are 20549 * three possible conditions here: 20550 * 20551 * (1) The original IO is on un_retry_bp. 20552 * (2) The original IO is on the regular wait queue, and un_retry_bp 20553 * is NULL. 20554 * (3) The original IO is on the regular wait queue, and un_retry_bp 20555 * points to some other, unrelated bp. 20556 * 20557 * For each case, we must call sd_start_cmds() with un_retry_bp 20558 * as the argument. If un_retry_bp is NULL, this will initiate 20559 * processing of the regular wait queue. If un_retry_bp is not NULL, 20560 * then this will process the bp on un_retry_bp. That may or may not 20561 * be the original IO, but that does not matter: the important thing 20562 * is to keep the IO processing going at this point. 20563 * 20564 * Note: This is a very specific error recovery sequence associated 20565 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20566 * serialize the I/O with completion of the spin-up. 20567 */ 20568 mutex_enter(SD_MUTEX(un)); 20569 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20570 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20571 un, un->un_retry_bp); 20572 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20573 sd_start_cmds(un, un->un_retry_bp); 20574 mutex_exit(SD_MUTEX(un)); 20575 20576 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20577 } 20578 20579 20580 /* 20581 * Function: sd_send_scsi_INQUIRY 20582 * 20583 * Description: Issue the scsi INQUIRY command. 20584 * 20585 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20586 * structure for this target. 20587 * bufaddr 20588 * buflen 20589 * evpd 20590 * page_code 20591 * page_length 20592 * 20593 * Return Code: 0 - Success 20594 * errno return code from sd_ssc_send() 20595 * 20596 * Context: Can sleep. Does not return until command is completed. 20597 */ 20598 20599 static int 20600 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen, 20601 uchar_t evpd, uchar_t page_code, size_t *residp) 20602 { 20603 union scsi_cdb cdb; 20604 struct uscsi_cmd ucmd_buf; 20605 int status; 20606 struct sd_lun *un; 20607 20608 ASSERT(ssc != NULL); 20609 un = ssc->ssc_un; 20610 ASSERT(un != NULL); 20611 ASSERT(!mutex_owned(SD_MUTEX(un))); 20612 ASSERT(bufaddr != NULL); 20613 20614 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20615 20616 bzero(&cdb, sizeof (cdb)); 20617 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20618 bzero(bufaddr, buflen); 20619 20620 cdb.scc_cmd = SCMD_INQUIRY; 20621 cdb.cdb_opaque[1] = evpd; 20622 cdb.cdb_opaque[2] = page_code; 20623 FORMG0COUNT(&cdb, buflen); 20624 20625 ucmd_buf.uscsi_cdb = (char *)&cdb; 20626 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20627 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20628 ucmd_buf.uscsi_buflen = buflen; 20629 ucmd_buf.uscsi_rqbuf = NULL; 20630 ucmd_buf.uscsi_rqlen = 0; 20631 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20632 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20633 20634 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20635 UIO_SYSSPACE, SD_PATH_DIRECT); 20636 20637 /* 20638 * Only handle status == 0, the upper-level caller 20639 * will put different assessment based on the context. 20640 */ 20641 if (status == 0) 20642 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20643 20644 if ((status == 0) && (residp != NULL)) { 20645 *residp = ucmd_buf.uscsi_resid; 20646 } 20647 20648 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20649 20650 return (status); 20651 } 20652 20653 20654 /* 20655 * Function: sd_send_scsi_TEST_UNIT_READY 20656 * 20657 * Description: Issue the scsi TEST UNIT READY command. 20658 * This routine can be told to set the flag USCSI_DIAGNOSE to 20659 * prevent retrying failed commands. Use this when the intent 20660 * is either to check for device readiness, to clear a Unit 20661 * Attention, or to clear any outstanding sense data. 20662 * However under specific conditions the expected behavior 20663 * is for retries to bring a device ready, so use the flag 20664 * with caution. 20665 * 20666 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20667 * structure for this target. 20668 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20669 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20670 * 0: dont check for media present, do retries on cmd. 20671 * 20672 * Return Code: 0 - Success 20673 * EIO - IO error 20674 * EACCES - Reservation conflict detected 20675 * ENXIO - Not Ready, medium not present 20676 * errno return code from sd_ssc_send() 20677 * 20678 * Context: Can sleep. Does not return until command is completed. 20679 */ 20680 20681 static int 20682 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag) 20683 { 20684 struct scsi_extended_sense sense_buf; 20685 union scsi_cdb cdb; 20686 struct uscsi_cmd ucmd_buf; 20687 int status; 20688 struct sd_lun *un; 20689 20690 ASSERT(ssc != NULL); 20691 un = ssc->ssc_un; 20692 ASSERT(un != NULL); 20693 ASSERT(!mutex_owned(SD_MUTEX(un))); 20694 20695 SD_TRACE(SD_LOG_IO, un, 20696 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20697 20698 /* 20699 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20700 * timeouts when they receive a TUR and the queue is not empty. Check 20701 * the configuration flag set during attach (indicating the drive has 20702 * this firmware bug) and un_ncmds_in_transport before issuing the 20703 * TUR. If there are 20704 * pending commands return success, this is a bit arbitrary but is ok 20705 * for non-removables (i.e. the eliteI disks) and non-clustering 20706 * configurations. 20707 */ 20708 if (un->un_f_cfg_tur_check == TRUE) { 20709 mutex_enter(SD_MUTEX(un)); 20710 if (un->un_ncmds_in_transport != 0) { 20711 mutex_exit(SD_MUTEX(un)); 20712 return (0); 20713 } 20714 mutex_exit(SD_MUTEX(un)); 20715 } 20716 20717 bzero(&cdb, sizeof (cdb)); 20718 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20719 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20720 20721 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20722 20723 ucmd_buf.uscsi_cdb = (char *)&cdb; 20724 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20725 ucmd_buf.uscsi_bufaddr = NULL; 20726 ucmd_buf.uscsi_buflen = 0; 20727 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20728 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20729 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20730 20731 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20732 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20733 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20734 } 20735 ucmd_buf.uscsi_timeout = 60; 20736 20737 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20738 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 20739 SD_PATH_STANDARD)); 20740 20741 switch (status) { 20742 case 0: 20743 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20744 break; /* Success! */ 20745 case EIO: 20746 switch (ucmd_buf.uscsi_status) { 20747 case STATUS_RESERVATION_CONFLICT: 20748 status = EACCES; 20749 break; 20750 case STATUS_CHECK: 20751 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20752 break; 20753 } 20754 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20755 (scsi_sense_key((uint8_t *)&sense_buf) == 20756 KEY_NOT_READY) && 20757 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20758 status = ENXIO; 20759 } 20760 break; 20761 default: 20762 break; 20763 } 20764 break; 20765 default: 20766 break; 20767 } 20768 20769 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20770 20771 return (status); 20772 } 20773 20774 /* 20775 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20776 * 20777 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20778 * 20779 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20780 * structure for this target. 20781 * 20782 * Return Code: 0 - Success 20783 * EACCES 20784 * ENOTSUP 20785 * errno return code from sd_ssc_send() 20786 * 20787 * Context: Can sleep. Does not return until command is completed. 20788 */ 20789 20790 static int 20791 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd, 20792 uint16_t data_len, uchar_t *data_bufp) 20793 { 20794 struct scsi_extended_sense sense_buf; 20795 union scsi_cdb cdb; 20796 struct uscsi_cmd ucmd_buf; 20797 int status; 20798 int no_caller_buf = FALSE; 20799 struct sd_lun *un; 20800 20801 ASSERT(ssc != NULL); 20802 un = ssc->ssc_un; 20803 ASSERT(un != NULL); 20804 ASSERT(!mutex_owned(SD_MUTEX(un))); 20805 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20806 20807 SD_TRACE(SD_LOG_IO, un, 20808 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20809 20810 bzero(&cdb, sizeof (cdb)); 20811 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20812 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20813 if (data_bufp == NULL) { 20814 /* Allocate a default buf if the caller did not give one */ 20815 ASSERT(data_len == 0); 20816 data_len = MHIOC_RESV_KEY_SIZE; 20817 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20818 no_caller_buf = TRUE; 20819 } 20820 20821 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20822 cdb.cdb_opaque[1] = usr_cmd; 20823 FORMG1COUNT(&cdb, data_len); 20824 20825 ucmd_buf.uscsi_cdb = (char *)&cdb; 20826 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20827 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20828 ucmd_buf.uscsi_buflen = data_len; 20829 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20830 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20831 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20832 ucmd_buf.uscsi_timeout = 60; 20833 20834 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20835 UIO_SYSSPACE, SD_PATH_STANDARD); 20836 20837 switch (status) { 20838 case 0: 20839 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20840 20841 break; /* Success! */ 20842 case EIO: 20843 switch (ucmd_buf.uscsi_status) { 20844 case STATUS_RESERVATION_CONFLICT: 20845 status = EACCES; 20846 break; 20847 case STATUS_CHECK: 20848 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20849 (scsi_sense_key((uint8_t *)&sense_buf) == 20850 KEY_ILLEGAL_REQUEST)) { 20851 status = ENOTSUP; 20852 } 20853 break; 20854 default: 20855 break; 20856 } 20857 break; 20858 default: 20859 break; 20860 } 20861 20862 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20863 20864 if (no_caller_buf == TRUE) { 20865 kmem_free(data_bufp, data_len); 20866 } 20867 20868 return (status); 20869 } 20870 20871 20872 /* 20873 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20874 * 20875 * Description: This routine is the driver entry point for handling CD-ROM 20876 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20877 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20878 * device. 20879 * 20880 * Arguments: ssc - ssc contains un - pointer to soft state struct 20881 * for the target. 20882 * usr_cmd SCSI-3 reservation facility command (one of 20883 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20884 * SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR) 20885 * usr_bufp - user provided pointer register, reserve descriptor or 20886 * preempt and abort structure (mhioc_register_t, 20887 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20888 * 20889 * Return Code: 0 - Success 20890 * EACCES 20891 * ENOTSUP 20892 * errno return code from sd_ssc_send() 20893 * 20894 * Context: Can sleep. Does not return until command is completed. 20895 */ 20896 20897 static int 20898 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd, 20899 uchar_t *usr_bufp) 20900 { 20901 struct scsi_extended_sense sense_buf; 20902 union scsi_cdb cdb; 20903 struct uscsi_cmd ucmd_buf; 20904 int status; 20905 uchar_t data_len = sizeof (sd_prout_t); 20906 sd_prout_t *prp; 20907 struct sd_lun *un; 20908 20909 ASSERT(ssc != NULL); 20910 un = ssc->ssc_un; 20911 ASSERT(un != NULL); 20912 ASSERT(!mutex_owned(SD_MUTEX(un))); 20913 ASSERT(data_len == 24); /* required by scsi spec */ 20914 20915 SD_TRACE(SD_LOG_IO, un, 20916 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20917 20918 if (usr_bufp == NULL) { 20919 return (EINVAL); 20920 } 20921 20922 bzero(&cdb, sizeof (cdb)); 20923 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20924 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20925 prp = kmem_zalloc(data_len, KM_SLEEP); 20926 20927 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20928 cdb.cdb_opaque[1] = usr_cmd; 20929 FORMG1COUNT(&cdb, data_len); 20930 20931 ucmd_buf.uscsi_cdb = (char *)&cdb; 20932 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20933 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20934 ucmd_buf.uscsi_buflen = data_len; 20935 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20936 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20937 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20938 ucmd_buf.uscsi_timeout = 60; 20939 20940 switch (usr_cmd) { 20941 case SD_SCSI3_REGISTER: { 20942 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20943 20944 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20945 bcopy(ptr->newkey.key, prp->service_key, 20946 MHIOC_RESV_KEY_SIZE); 20947 prp->aptpl = ptr->aptpl; 20948 break; 20949 } 20950 case SD_SCSI3_CLEAR: { 20951 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20952 20953 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20954 break; 20955 } 20956 case SD_SCSI3_RESERVE: 20957 case SD_SCSI3_RELEASE: { 20958 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20959 20960 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20961 prp->scope_address = BE_32(ptr->scope_specific_addr); 20962 cdb.cdb_opaque[2] = ptr->type; 20963 break; 20964 } 20965 case SD_SCSI3_PREEMPTANDABORT: { 20966 mhioc_preemptandabort_t *ptr = 20967 (mhioc_preemptandabort_t *)usr_bufp; 20968 20969 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20970 bcopy(ptr->victim_key.key, prp->service_key, 20971 MHIOC_RESV_KEY_SIZE); 20972 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20973 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20974 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20975 break; 20976 } 20977 case SD_SCSI3_REGISTERANDIGNOREKEY: 20978 { 20979 mhioc_registerandignorekey_t *ptr; 20980 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20981 bcopy(ptr->newkey.key, 20982 prp->service_key, MHIOC_RESV_KEY_SIZE); 20983 prp->aptpl = ptr->aptpl; 20984 break; 20985 } 20986 default: 20987 ASSERT(FALSE); 20988 break; 20989 } 20990 20991 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20992 UIO_SYSSPACE, SD_PATH_STANDARD); 20993 20994 switch (status) { 20995 case 0: 20996 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20997 break; /* Success! */ 20998 case EIO: 20999 switch (ucmd_buf.uscsi_status) { 21000 case STATUS_RESERVATION_CONFLICT: 21001 status = EACCES; 21002 break; 21003 case STATUS_CHECK: 21004 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21005 (scsi_sense_key((uint8_t *)&sense_buf) == 21006 KEY_ILLEGAL_REQUEST)) { 21007 status = ENOTSUP; 21008 } 21009 break; 21010 default: 21011 break; 21012 } 21013 break; 21014 default: 21015 break; 21016 } 21017 21018 kmem_free(prp, data_len); 21019 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 21020 return (status); 21021 } 21022 21023 21024 /* 21025 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 21026 * 21027 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 21028 * 21029 * Arguments: un - pointer to the target's soft state struct 21030 * dkc - pointer to the callback structure 21031 * 21032 * Return Code: 0 - success 21033 * errno-type error code 21034 * 21035 * Context: kernel thread context only. 21036 * 21037 * _______________________________________________________________ 21038 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 21039 * |FLUSH_VOLATILE| | operation | 21040 * |______________|______________|_________________________________| 21041 * | 0 | NULL | Synchronous flush on both | 21042 * | | | volatile and non-volatile cache | 21043 * |______________|______________|_________________________________| 21044 * | 1 | NULL | Synchronous flush on volatile | 21045 * | | | cache; disk drivers may suppress| 21046 * | | | flush if disk table indicates | 21047 * | | | non-volatile cache | 21048 * |______________|______________|_________________________________| 21049 * | 0 | !NULL | Asynchronous flush on both | 21050 * | | | volatile and non-volatile cache;| 21051 * |______________|______________|_________________________________| 21052 * | 1 | !NULL | Asynchronous flush on volatile | 21053 * | | | cache; disk drivers may suppress| 21054 * | | | flush if disk table indicates | 21055 * | | | non-volatile cache | 21056 * |______________|______________|_________________________________| 21057 * 21058 */ 21059 21060 static int 21061 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 21062 { 21063 struct sd_uscsi_info *uip; 21064 struct uscsi_cmd *uscmd; 21065 union scsi_cdb *cdb; 21066 struct buf *bp; 21067 int rval = 0; 21068 int is_async; 21069 21070 SD_TRACE(SD_LOG_IO, un, 21071 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 21072 21073 ASSERT(un != NULL); 21074 ASSERT(!mutex_owned(SD_MUTEX(un))); 21075 21076 if (dkc == NULL || dkc->dkc_callback == NULL) { 21077 is_async = FALSE; 21078 } else { 21079 is_async = TRUE; 21080 } 21081 21082 mutex_enter(SD_MUTEX(un)); 21083 /* check whether cache flush should be suppressed */ 21084 if (un->un_f_suppress_cache_flush == TRUE) { 21085 mutex_exit(SD_MUTEX(un)); 21086 /* 21087 * suppress the cache flush if the device is told to do 21088 * so by sd.conf or disk table 21089 */ 21090 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 21091 skip the cache flush since suppress_cache_flush is %d!\n", 21092 un->un_f_suppress_cache_flush); 21093 21094 if (is_async == TRUE) { 21095 /* invoke callback for asynchronous flush */ 21096 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 21097 } 21098 return (rval); 21099 } 21100 mutex_exit(SD_MUTEX(un)); 21101 21102 /* 21103 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 21104 * set properly 21105 */ 21106 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 21107 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 21108 21109 mutex_enter(SD_MUTEX(un)); 21110 if (dkc != NULL && un->un_f_sync_nv_supported && 21111 (dkc->dkc_flag & FLUSH_VOLATILE)) { 21112 /* 21113 * if the device supports SYNC_NV bit, turn on 21114 * the SYNC_NV bit to only flush volatile cache 21115 */ 21116 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 21117 } 21118 mutex_exit(SD_MUTEX(un)); 21119 21120 /* 21121 * First get some memory for the uscsi_cmd struct and cdb 21122 * and initialize for SYNCHRONIZE_CACHE cmd. 21123 */ 21124 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 21125 uscmd->uscsi_cdblen = CDB_GROUP1; 21126 uscmd->uscsi_cdb = (caddr_t)cdb; 21127 uscmd->uscsi_bufaddr = NULL; 21128 uscmd->uscsi_buflen = 0; 21129 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 21130 uscmd->uscsi_rqlen = SENSE_LENGTH; 21131 uscmd->uscsi_rqresid = SENSE_LENGTH; 21132 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 21133 uscmd->uscsi_timeout = sd_io_time; 21134 21135 /* 21136 * Allocate an sd_uscsi_info struct and fill it with the info 21137 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 21138 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 21139 * since we allocate the buf here in this function, we do not 21140 * need to preserve the prior contents of b_private. 21141 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 21142 */ 21143 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 21144 uip->ui_flags = SD_PATH_DIRECT; 21145 uip->ui_cmdp = uscmd; 21146 21147 bp = getrbuf(KM_SLEEP); 21148 bp->b_private = uip; 21149 21150 /* 21151 * Setup buffer to carry uscsi request. 21152 */ 21153 bp->b_flags = B_BUSY; 21154 bp->b_bcount = 0; 21155 bp->b_blkno = 0; 21156 21157 if (is_async == TRUE) { 21158 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 21159 uip->ui_dkc = *dkc; 21160 } 21161 21162 bp->b_edev = SD_GET_DEV(un); 21163 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 21164 21165 /* 21166 * Unset un_f_sync_cache_required flag 21167 */ 21168 mutex_enter(SD_MUTEX(un)); 21169 un->un_f_sync_cache_required = FALSE; 21170 mutex_exit(SD_MUTEX(un)); 21171 21172 (void) sd_uscsi_strategy(bp); 21173 21174 /* 21175 * If synchronous request, wait for completion 21176 * If async just return and let b_iodone callback 21177 * cleanup. 21178 * NOTE: On return, u_ncmds_in_driver will be decremented, 21179 * but it was also incremented in sd_uscsi_strategy(), so 21180 * we should be ok. 21181 */ 21182 if (is_async == FALSE) { 21183 (void) biowait(bp); 21184 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 21185 } 21186 21187 return (rval); 21188 } 21189 21190 21191 static int 21192 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 21193 { 21194 struct sd_uscsi_info *uip; 21195 struct uscsi_cmd *uscmd; 21196 uint8_t *sense_buf; 21197 struct sd_lun *un; 21198 int status; 21199 union scsi_cdb *cdb; 21200 21201 uip = (struct sd_uscsi_info *)(bp->b_private); 21202 ASSERT(uip != NULL); 21203 21204 uscmd = uip->ui_cmdp; 21205 ASSERT(uscmd != NULL); 21206 21207 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 21208 ASSERT(sense_buf != NULL); 21209 21210 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 21211 ASSERT(un != NULL); 21212 21213 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 21214 21215 status = geterror(bp); 21216 switch (status) { 21217 case 0: 21218 break; /* Success! */ 21219 case EIO: 21220 switch (uscmd->uscsi_status) { 21221 case STATUS_RESERVATION_CONFLICT: 21222 /* Ignore reservation conflict */ 21223 status = 0; 21224 goto done; 21225 21226 case STATUS_CHECK: 21227 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 21228 (scsi_sense_key(sense_buf) == 21229 KEY_ILLEGAL_REQUEST)) { 21230 /* Ignore Illegal Request error */ 21231 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) { 21232 mutex_enter(SD_MUTEX(un)); 21233 un->un_f_sync_nv_supported = FALSE; 21234 mutex_exit(SD_MUTEX(un)); 21235 status = 0; 21236 SD_TRACE(SD_LOG_IO, un, 21237 "un_f_sync_nv_supported \ 21238 is set to false.\n"); 21239 goto done; 21240 } 21241 21242 mutex_enter(SD_MUTEX(un)); 21243 un->un_f_sync_cache_supported = FALSE; 21244 mutex_exit(SD_MUTEX(un)); 21245 SD_TRACE(SD_LOG_IO, un, 21246 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 21247 un_f_sync_cache_supported set to false \ 21248 with asc = %x, ascq = %x\n", 21249 scsi_sense_asc(sense_buf), 21250 scsi_sense_ascq(sense_buf)); 21251 status = ENOTSUP; 21252 goto done; 21253 } 21254 break; 21255 default: 21256 break; 21257 } 21258 /* FALLTHRU */ 21259 default: 21260 /* 21261 * Turn on the un_f_sync_cache_required flag 21262 * since the SYNC CACHE command failed 21263 */ 21264 mutex_enter(SD_MUTEX(un)); 21265 un->un_f_sync_cache_required = TRUE; 21266 mutex_exit(SD_MUTEX(un)); 21267 21268 /* 21269 * Don't log an error message if this device 21270 * has removable media. 21271 */ 21272 if (!un->un_f_has_removable_media) { 21273 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 21274 "SYNCHRONIZE CACHE command failed (%d)\n", status); 21275 } 21276 break; 21277 } 21278 21279 done: 21280 if (uip->ui_dkc.dkc_callback != NULL) { 21281 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 21282 } 21283 21284 ASSERT((bp->b_flags & B_REMAPPED) == 0); 21285 freerbuf(bp); 21286 kmem_free(uip, sizeof (struct sd_uscsi_info)); 21287 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 21288 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 21289 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 21290 21291 return (status); 21292 } 21293 21294 21295 /* 21296 * Function: sd_send_scsi_GET_CONFIGURATION 21297 * 21298 * Description: Issues the get configuration command to the device. 21299 * Called from sd_check_for_writable_cd & sd_get_media_info 21300 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 21301 * Arguments: ssc 21302 * ucmdbuf 21303 * rqbuf 21304 * rqbuflen 21305 * bufaddr 21306 * buflen 21307 * path_flag 21308 * 21309 * Return Code: 0 - Success 21310 * errno return code from sd_ssc_send() 21311 * 21312 * Context: Can sleep. Does not return until command is completed. 21313 * 21314 */ 21315 21316 static int 21317 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf, 21318 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 21319 int path_flag) 21320 { 21321 char cdb[CDB_GROUP1]; 21322 int status; 21323 struct sd_lun *un; 21324 21325 ASSERT(ssc != NULL); 21326 un = ssc->ssc_un; 21327 ASSERT(un != NULL); 21328 ASSERT(!mutex_owned(SD_MUTEX(un))); 21329 ASSERT(bufaddr != NULL); 21330 ASSERT(ucmdbuf != NULL); 21331 ASSERT(rqbuf != NULL); 21332 21333 SD_TRACE(SD_LOG_IO, un, 21334 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 21335 21336 bzero(cdb, sizeof (cdb)); 21337 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21338 bzero(rqbuf, rqbuflen); 21339 bzero(bufaddr, buflen); 21340 21341 /* 21342 * Set up cdb field for the get configuration command. 21343 */ 21344 cdb[0] = SCMD_GET_CONFIGURATION; 21345 cdb[1] = 0x02; /* Requested Type */ 21346 cdb[8] = SD_PROFILE_HEADER_LEN; 21347 ucmdbuf->uscsi_cdb = cdb; 21348 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21349 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21350 ucmdbuf->uscsi_buflen = buflen; 21351 ucmdbuf->uscsi_timeout = sd_io_time; 21352 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21353 ucmdbuf->uscsi_rqlen = rqbuflen; 21354 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21355 21356 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21357 UIO_SYSSPACE, path_flag); 21358 21359 switch (status) { 21360 case 0: 21361 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21362 break; /* Success! */ 21363 case EIO: 21364 switch (ucmdbuf->uscsi_status) { 21365 case STATUS_RESERVATION_CONFLICT: 21366 status = EACCES; 21367 break; 21368 default: 21369 break; 21370 } 21371 break; 21372 default: 21373 break; 21374 } 21375 21376 if (status == 0) { 21377 SD_DUMP_MEMORY(un, SD_LOG_IO, 21378 "sd_send_scsi_GET_CONFIGURATION: data", 21379 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21380 } 21381 21382 SD_TRACE(SD_LOG_IO, un, 21383 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 21384 21385 return (status); 21386 } 21387 21388 /* 21389 * Function: sd_send_scsi_feature_GET_CONFIGURATION 21390 * 21391 * Description: Issues the get configuration command to the device to 21392 * retrieve a specific feature. Called from 21393 * sd_check_for_writable_cd & sd_set_mmc_caps. 21394 * Arguments: ssc 21395 * ucmdbuf 21396 * rqbuf 21397 * rqbuflen 21398 * bufaddr 21399 * buflen 21400 * feature 21401 * 21402 * Return Code: 0 - Success 21403 * errno return code from sd_ssc_send() 21404 * 21405 * Context: Can sleep. Does not return until command is completed. 21406 * 21407 */ 21408 static int 21409 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 21410 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 21411 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 21412 { 21413 char cdb[CDB_GROUP1]; 21414 int status; 21415 struct sd_lun *un; 21416 21417 ASSERT(ssc != NULL); 21418 un = ssc->ssc_un; 21419 ASSERT(un != NULL); 21420 ASSERT(!mutex_owned(SD_MUTEX(un))); 21421 ASSERT(bufaddr != NULL); 21422 ASSERT(ucmdbuf != NULL); 21423 ASSERT(rqbuf != NULL); 21424 21425 SD_TRACE(SD_LOG_IO, un, 21426 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 21427 21428 bzero(cdb, sizeof (cdb)); 21429 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 21430 bzero(rqbuf, rqbuflen); 21431 bzero(bufaddr, buflen); 21432 21433 /* 21434 * Set up cdb field for the get configuration command. 21435 */ 21436 cdb[0] = SCMD_GET_CONFIGURATION; 21437 cdb[1] = 0x02; /* Requested Type */ 21438 cdb[3] = feature; 21439 cdb[8] = buflen; 21440 ucmdbuf->uscsi_cdb = cdb; 21441 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 21442 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 21443 ucmdbuf->uscsi_buflen = buflen; 21444 ucmdbuf->uscsi_timeout = sd_io_time; 21445 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 21446 ucmdbuf->uscsi_rqlen = rqbuflen; 21447 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 21448 21449 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 21450 UIO_SYSSPACE, path_flag); 21451 21452 switch (status) { 21453 case 0: 21454 21455 break; /* Success! */ 21456 case EIO: 21457 switch (ucmdbuf->uscsi_status) { 21458 case STATUS_RESERVATION_CONFLICT: 21459 status = EACCES; 21460 break; 21461 default: 21462 break; 21463 } 21464 break; 21465 default: 21466 break; 21467 } 21468 21469 if (status == 0) { 21470 SD_DUMP_MEMORY(un, SD_LOG_IO, 21471 "sd_send_scsi_feature_GET_CONFIGURATION: data", 21472 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 21473 } 21474 21475 SD_TRACE(SD_LOG_IO, un, 21476 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 21477 21478 return (status); 21479 } 21480 21481 21482 /* 21483 * Function: sd_send_scsi_MODE_SENSE 21484 * 21485 * Description: Utility function for issuing a scsi MODE SENSE command. 21486 * Note: This routine uses a consistent implementation for Group0, 21487 * Group1, and Group2 commands across all platforms. ATAPI devices 21488 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21489 * 21490 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21491 * structure for this target. 21492 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21493 * CDB_GROUP[1|2] (10 byte). 21494 * bufaddr - buffer for page data retrieved from the target. 21495 * buflen - size of page to be retrieved. 21496 * page_code - page code of data to be retrieved from the target. 21497 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21498 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21499 * to use the USCSI "direct" chain and bypass the normal 21500 * command waitq. 21501 * 21502 * Return Code: 0 - Success 21503 * errno return code from sd_ssc_send() 21504 * 21505 * Context: Can sleep. Does not return until command is completed. 21506 */ 21507 21508 static int 21509 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21510 size_t buflen, uchar_t page_code, int path_flag) 21511 { 21512 struct scsi_extended_sense sense_buf; 21513 union scsi_cdb cdb; 21514 struct uscsi_cmd ucmd_buf; 21515 int status; 21516 int headlen; 21517 struct sd_lun *un; 21518 21519 ASSERT(ssc != NULL); 21520 un = ssc->ssc_un; 21521 ASSERT(un != NULL); 21522 ASSERT(!mutex_owned(SD_MUTEX(un))); 21523 ASSERT(bufaddr != NULL); 21524 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21525 (cdbsize == CDB_GROUP2)); 21526 21527 SD_TRACE(SD_LOG_IO, un, 21528 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 21529 21530 bzero(&cdb, sizeof (cdb)); 21531 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21532 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21533 bzero(bufaddr, buflen); 21534 21535 if (cdbsize == CDB_GROUP0) { 21536 cdb.scc_cmd = SCMD_MODE_SENSE; 21537 cdb.cdb_opaque[2] = page_code; 21538 FORMG0COUNT(&cdb, buflen); 21539 headlen = MODE_HEADER_LENGTH; 21540 } else { 21541 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 21542 cdb.cdb_opaque[2] = page_code; 21543 FORMG1COUNT(&cdb, buflen); 21544 headlen = MODE_HEADER_LENGTH_GRP2; 21545 } 21546 21547 ASSERT(headlen <= buflen); 21548 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21549 21550 ucmd_buf.uscsi_cdb = (char *)&cdb; 21551 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21552 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21553 ucmd_buf.uscsi_buflen = buflen; 21554 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21555 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21556 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21557 ucmd_buf.uscsi_timeout = 60; 21558 21559 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21560 UIO_SYSSPACE, path_flag); 21561 21562 switch (status) { 21563 case 0: 21564 /* 21565 * sr_check_wp() uses 0x3f page code and check the header of 21566 * mode page to determine if target device is write-protected. 21567 * But some USB devices return 0 bytes for 0x3f page code. For 21568 * this case, make sure that mode page header is returned at 21569 * least. 21570 */ 21571 if (buflen - ucmd_buf.uscsi_resid < headlen) { 21572 status = EIO; 21573 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 21574 "mode page header is not returned"); 21575 } 21576 break; /* Success! */ 21577 case EIO: 21578 switch (ucmd_buf.uscsi_status) { 21579 case STATUS_RESERVATION_CONFLICT: 21580 status = EACCES; 21581 break; 21582 default: 21583 break; 21584 } 21585 break; 21586 default: 21587 break; 21588 } 21589 21590 if (status == 0) { 21591 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 21592 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21593 } 21594 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 21595 21596 return (status); 21597 } 21598 21599 21600 /* 21601 * Function: sd_send_scsi_MODE_SELECT 21602 * 21603 * Description: Utility function for issuing a scsi MODE SELECT command. 21604 * Note: This routine uses a consistent implementation for Group0, 21605 * Group1, and Group2 commands across all platforms. ATAPI devices 21606 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 21607 * 21608 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21609 * structure for this target. 21610 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 21611 * CDB_GROUP[1|2] (10 byte). 21612 * bufaddr - buffer for page data retrieved from the target. 21613 * buflen - size of page to be retrieved. 21614 * save_page - boolean to determin if SP bit should be set. 21615 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21616 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21617 * to use the USCSI "direct" chain and bypass the normal 21618 * command waitq. 21619 * 21620 * Return Code: 0 - Success 21621 * errno return code from sd_ssc_send() 21622 * 21623 * Context: Can sleep. Does not return until command is completed. 21624 */ 21625 21626 static int 21627 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 21628 size_t buflen, uchar_t save_page, int path_flag) 21629 { 21630 struct scsi_extended_sense sense_buf; 21631 union scsi_cdb cdb; 21632 struct uscsi_cmd ucmd_buf; 21633 int status; 21634 struct sd_lun *un; 21635 21636 ASSERT(ssc != NULL); 21637 un = ssc->ssc_un; 21638 ASSERT(un != NULL); 21639 ASSERT(!mutex_owned(SD_MUTEX(un))); 21640 ASSERT(bufaddr != NULL); 21641 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 21642 (cdbsize == CDB_GROUP2)); 21643 21644 SD_TRACE(SD_LOG_IO, un, 21645 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 21646 21647 bzero(&cdb, sizeof (cdb)); 21648 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21649 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21650 21651 /* Set the PF bit for many third party drives */ 21652 cdb.cdb_opaque[1] = 0x10; 21653 21654 /* Set the savepage(SP) bit if given */ 21655 if (save_page == SD_SAVE_PAGE) { 21656 cdb.cdb_opaque[1] |= 0x01; 21657 } 21658 21659 if (cdbsize == CDB_GROUP0) { 21660 cdb.scc_cmd = SCMD_MODE_SELECT; 21661 FORMG0COUNT(&cdb, buflen); 21662 } else { 21663 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 21664 FORMG1COUNT(&cdb, buflen); 21665 } 21666 21667 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21668 21669 ucmd_buf.uscsi_cdb = (char *)&cdb; 21670 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21671 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21672 ucmd_buf.uscsi_buflen = buflen; 21673 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21674 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21675 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 21676 ucmd_buf.uscsi_timeout = 60; 21677 21678 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21679 UIO_SYSSPACE, path_flag); 21680 21681 switch (status) { 21682 case 0: 21683 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21684 break; /* Success! */ 21685 case EIO: 21686 switch (ucmd_buf.uscsi_status) { 21687 case STATUS_RESERVATION_CONFLICT: 21688 status = EACCES; 21689 break; 21690 default: 21691 break; 21692 } 21693 break; 21694 default: 21695 break; 21696 } 21697 21698 if (status == 0) { 21699 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21700 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21701 } 21702 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21703 21704 return (status); 21705 } 21706 21707 21708 /* 21709 * Function: sd_send_scsi_RDWR 21710 * 21711 * Description: Issue a scsi READ or WRITE command with the given parameters. 21712 * 21713 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21714 * structure for this target. 21715 * cmd: SCMD_READ or SCMD_WRITE 21716 * bufaddr: Address of caller's buffer to receive the RDWR data 21717 * buflen: Length of caller's buffer receive the RDWR data. 21718 * start_block: Block number for the start of the RDWR operation. 21719 * (Assumes target-native block size.) 21720 * residp: Pointer to variable to receive the redisual of the 21721 * RDWR operation (may be NULL of no residual requested). 21722 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21723 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21724 * to use the USCSI "direct" chain and bypass the normal 21725 * command waitq. 21726 * 21727 * Return Code: 0 - Success 21728 * errno return code from sd_ssc_send() 21729 * 21730 * Context: Can sleep. Does not return until command is completed. 21731 */ 21732 21733 static int 21734 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 21735 size_t buflen, daddr_t start_block, int path_flag) 21736 { 21737 struct scsi_extended_sense sense_buf; 21738 union scsi_cdb cdb; 21739 struct uscsi_cmd ucmd_buf; 21740 uint32_t block_count; 21741 int status; 21742 int cdbsize; 21743 uchar_t flag; 21744 struct sd_lun *un; 21745 21746 ASSERT(ssc != NULL); 21747 un = ssc->ssc_un; 21748 ASSERT(un != NULL); 21749 ASSERT(!mutex_owned(SD_MUTEX(un))); 21750 ASSERT(bufaddr != NULL); 21751 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21752 21753 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21754 21755 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21756 return (EINVAL); 21757 } 21758 21759 mutex_enter(SD_MUTEX(un)); 21760 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21761 mutex_exit(SD_MUTEX(un)); 21762 21763 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21764 21765 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21766 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21767 bufaddr, buflen, start_block, block_count); 21768 21769 bzero(&cdb, sizeof (cdb)); 21770 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21771 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21772 21773 /* Compute CDB size to use */ 21774 if (start_block > 0xffffffff) 21775 cdbsize = CDB_GROUP4; 21776 else if ((start_block & 0xFFE00000) || 21777 (un->un_f_cfg_is_atapi == TRUE)) 21778 cdbsize = CDB_GROUP1; 21779 else 21780 cdbsize = CDB_GROUP0; 21781 21782 switch (cdbsize) { 21783 case CDB_GROUP0: /* 6-byte CDBs */ 21784 cdb.scc_cmd = cmd; 21785 FORMG0ADDR(&cdb, start_block); 21786 FORMG0COUNT(&cdb, block_count); 21787 break; 21788 case CDB_GROUP1: /* 10-byte CDBs */ 21789 cdb.scc_cmd = cmd | SCMD_GROUP1; 21790 FORMG1ADDR(&cdb, start_block); 21791 FORMG1COUNT(&cdb, block_count); 21792 break; 21793 case CDB_GROUP4: /* 16-byte CDBs */ 21794 cdb.scc_cmd = cmd | SCMD_GROUP4; 21795 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21796 FORMG4COUNT(&cdb, block_count); 21797 break; 21798 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21799 default: 21800 /* All others reserved */ 21801 return (EINVAL); 21802 } 21803 21804 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21805 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21806 21807 ucmd_buf.uscsi_cdb = (char *)&cdb; 21808 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21809 ucmd_buf.uscsi_bufaddr = bufaddr; 21810 ucmd_buf.uscsi_buflen = buflen; 21811 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21812 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21813 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21814 ucmd_buf.uscsi_timeout = 60; 21815 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21816 UIO_SYSSPACE, path_flag); 21817 21818 switch (status) { 21819 case 0: 21820 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21821 break; /* Success! */ 21822 case EIO: 21823 switch (ucmd_buf.uscsi_status) { 21824 case STATUS_RESERVATION_CONFLICT: 21825 status = EACCES; 21826 break; 21827 default: 21828 break; 21829 } 21830 break; 21831 default: 21832 break; 21833 } 21834 21835 if (status == 0) { 21836 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21837 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21838 } 21839 21840 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21841 21842 return (status); 21843 } 21844 21845 21846 /* 21847 * Function: sd_send_scsi_LOG_SENSE 21848 * 21849 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21850 * 21851 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21852 * structure for this target. 21853 * 21854 * Return Code: 0 - Success 21855 * errno return code from sd_ssc_send() 21856 * 21857 * Context: Can sleep. Does not return until command is completed. 21858 */ 21859 21860 static int 21861 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen, 21862 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21863 int path_flag) 21864 21865 { 21866 struct scsi_extended_sense sense_buf; 21867 union scsi_cdb cdb; 21868 struct uscsi_cmd ucmd_buf; 21869 int status; 21870 struct sd_lun *un; 21871 21872 ASSERT(ssc != NULL); 21873 un = ssc->ssc_un; 21874 ASSERT(un != NULL); 21875 ASSERT(!mutex_owned(SD_MUTEX(un))); 21876 21877 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21878 21879 bzero(&cdb, sizeof (cdb)); 21880 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21881 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21882 21883 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21884 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21885 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21886 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21887 FORMG1COUNT(&cdb, buflen); 21888 21889 ucmd_buf.uscsi_cdb = (char *)&cdb; 21890 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21891 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21892 ucmd_buf.uscsi_buflen = buflen; 21893 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21894 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21895 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21896 ucmd_buf.uscsi_timeout = 60; 21897 21898 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21899 UIO_SYSSPACE, path_flag); 21900 21901 switch (status) { 21902 case 0: 21903 break; 21904 case EIO: 21905 switch (ucmd_buf.uscsi_status) { 21906 case STATUS_RESERVATION_CONFLICT: 21907 status = EACCES; 21908 break; 21909 case STATUS_CHECK: 21910 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21911 (scsi_sense_key((uint8_t *)&sense_buf) == 21912 KEY_ILLEGAL_REQUEST) && 21913 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21914 /* 21915 * ASC 0x24: INVALID FIELD IN CDB 21916 */ 21917 switch (page_code) { 21918 case START_STOP_CYCLE_PAGE: 21919 /* 21920 * The start stop cycle counter is 21921 * implemented as page 0x31 in earlier 21922 * generation disks. In new generation 21923 * disks the start stop cycle counter is 21924 * implemented as page 0xE. To properly 21925 * handle this case if an attempt for 21926 * log page 0xE is made and fails we 21927 * will try again using page 0x31. 21928 * 21929 * Network storage BU committed to 21930 * maintain the page 0x31 for this 21931 * purpose and will not have any other 21932 * page implemented with page code 0x31 21933 * until all disks transition to the 21934 * standard page. 21935 */ 21936 mutex_enter(SD_MUTEX(un)); 21937 un->un_start_stop_cycle_page = 21938 START_STOP_CYCLE_VU_PAGE; 21939 cdb.cdb_opaque[2] = 21940 (char)(page_control << 6) | 21941 un->un_start_stop_cycle_page; 21942 mutex_exit(SD_MUTEX(un)); 21943 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 21944 status = sd_ssc_send( 21945 ssc, &ucmd_buf, FKIOCTL, 21946 UIO_SYSSPACE, path_flag); 21947 21948 break; 21949 case TEMPERATURE_PAGE: 21950 status = ENOTTY; 21951 break; 21952 default: 21953 break; 21954 } 21955 } 21956 break; 21957 default: 21958 break; 21959 } 21960 break; 21961 default: 21962 break; 21963 } 21964 21965 if (status == 0) { 21966 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21967 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21968 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21969 } 21970 21971 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21972 21973 return (status); 21974 } 21975 21976 21977 /* 21978 * Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION 21979 * 21980 * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command. 21981 * 21982 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21983 * structure for this target. 21984 * bufaddr 21985 * buflen 21986 * class_req 21987 * 21988 * Return Code: 0 - Success 21989 * errno return code from sd_ssc_send() 21990 * 21991 * Context: Can sleep. Does not return until command is completed. 21992 */ 21993 21994 static int 21995 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr, 21996 size_t buflen, uchar_t class_req) 21997 { 21998 union scsi_cdb cdb; 21999 struct uscsi_cmd ucmd_buf; 22000 int status; 22001 struct sd_lun *un; 22002 22003 ASSERT(ssc != NULL); 22004 un = ssc->ssc_un; 22005 ASSERT(un != NULL); 22006 ASSERT(!mutex_owned(SD_MUTEX(un))); 22007 ASSERT(bufaddr != NULL); 22008 22009 SD_TRACE(SD_LOG_IO, un, 22010 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un); 22011 22012 bzero(&cdb, sizeof (cdb)); 22013 bzero(&ucmd_buf, sizeof (ucmd_buf)); 22014 bzero(bufaddr, buflen); 22015 22016 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION; 22017 cdb.cdb_opaque[1] = 1; /* polled */ 22018 cdb.cdb_opaque[4] = class_req; 22019 FORMG1COUNT(&cdb, buflen); 22020 22021 ucmd_buf.uscsi_cdb = (char *)&cdb; 22022 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 22023 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 22024 ucmd_buf.uscsi_buflen = buflen; 22025 ucmd_buf.uscsi_rqbuf = NULL; 22026 ucmd_buf.uscsi_rqlen = 0; 22027 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 22028 ucmd_buf.uscsi_timeout = 60; 22029 22030 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 22031 UIO_SYSSPACE, SD_PATH_DIRECT); 22032 22033 /* 22034 * Only handle status == 0, the upper-level caller 22035 * will put different assessment based on the context. 22036 */ 22037 if (status == 0) { 22038 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22039 22040 if (ucmd_buf.uscsi_resid != 0) { 22041 status = EIO; 22042 } 22043 } 22044 22045 SD_TRACE(SD_LOG_IO, un, 22046 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n"); 22047 22048 return (status); 22049 } 22050 22051 22052 static boolean_t 22053 sd_gesn_media_data_valid(uchar_t *data) 22054 { 22055 uint16_t len; 22056 22057 len = (data[1] << 8) | data[0]; 22058 return ((len >= 6) && 22059 ((data[2] & SD_GESN_HEADER_NEA) == 0) && 22060 ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) && 22061 ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0)); 22062 } 22063 22064 22065 /* 22066 * Function: sdioctl 22067 * 22068 * Description: Driver's ioctl(9e) entry point function. 22069 * 22070 * Arguments: dev - device number 22071 * cmd - ioctl operation to be performed 22072 * arg - user argument, contains data to be set or reference 22073 * parameter for get 22074 * flag - bit flag, indicating open settings, 32/64 bit type 22075 * cred_p - user credential pointer 22076 * rval_p - calling process return value (OPT) 22077 * 22078 * Return Code: EINVAL 22079 * ENOTTY 22080 * ENXIO 22081 * EIO 22082 * EFAULT 22083 * ENOTSUP 22084 * EPERM 22085 * 22086 * Context: Called from the device switch at normal priority. 22087 */ 22088 22089 static int 22090 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 22091 { 22092 struct sd_lun *un = NULL; 22093 int err = 0; 22094 int i = 0; 22095 cred_t *cr; 22096 int tmprval = EINVAL; 22097 boolean_t is_valid; 22098 sd_ssc_t *ssc; 22099 22100 /* 22101 * All device accesses go thru sdstrategy where we check on suspend 22102 * status 22103 */ 22104 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22105 return (ENXIO); 22106 } 22107 22108 ASSERT(!mutex_owned(SD_MUTEX(un))); 22109 22110 /* Initialize sd_ssc_t for internal uscsi commands */ 22111 ssc = sd_ssc_init(un); 22112 22113 is_valid = SD_IS_VALID_LABEL(un); 22114 22115 /* 22116 * Moved this wait from sd_uscsi_strategy to here for 22117 * reasons of deadlock prevention. Internal driver commands, 22118 * specifically those to change a devices power level, result 22119 * in a call to sd_uscsi_strategy. 22120 */ 22121 mutex_enter(SD_MUTEX(un)); 22122 while ((un->un_state == SD_STATE_SUSPENDED) || 22123 (un->un_state == SD_STATE_PM_CHANGING)) { 22124 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 22125 } 22126 /* 22127 * Twiddling the counter here protects commands from now 22128 * through to the top of sd_uscsi_strategy. Without the 22129 * counter inc. a power down, for example, could get in 22130 * after the above check for state is made and before 22131 * execution gets to the top of sd_uscsi_strategy. 22132 * That would cause problems. 22133 */ 22134 un->un_ncmds_in_driver++; 22135 22136 if (!is_valid && 22137 (flag & (FNDELAY | FNONBLOCK))) { 22138 switch (cmd) { 22139 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 22140 case DKIOCGVTOC: 22141 case DKIOCGEXTVTOC: 22142 case DKIOCGAPART: 22143 case DKIOCPARTINFO: 22144 case DKIOCEXTPARTINFO: 22145 case DKIOCSGEOM: 22146 case DKIOCSAPART: 22147 case DKIOCGETEFI: 22148 case DKIOCPARTITION: 22149 case DKIOCSVTOC: 22150 case DKIOCSEXTVTOC: 22151 case DKIOCSETEFI: 22152 case DKIOCGMBOOT: 22153 case DKIOCSMBOOT: 22154 case DKIOCG_PHYGEOM: 22155 case DKIOCG_VIRTGEOM: 22156 #if defined(__i386) || defined(__amd64) 22157 case DKIOCSETEXTPART: 22158 #endif 22159 /* let cmlb handle it */ 22160 goto skip_ready_valid; 22161 22162 case CDROMPAUSE: 22163 case CDROMRESUME: 22164 case CDROMPLAYMSF: 22165 case CDROMPLAYTRKIND: 22166 case CDROMREADTOCHDR: 22167 case CDROMREADTOCENTRY: 22168 case CDROMSTOP: 22169 case CDROMSTART: 22170 case CDROMVOLCTRL: 22171 case CDROMSUBCHNL: 22172 case CDROMREADMODE2: 22173 case CDROMREADMODE1: 22174 case CDROMREADOFFSET: 22175 case CDROMSBLKMODE: 22176 case CDROMGBLKMODE: 22177 case CDROMGDRVSPEED: 22178 case CDROMSDRVSPEED: 22179 case CDROMCDDA: 22180 case CDROMCDXA: 22181 case CDROMSUBCODE: 22182 if (!ISCD(un)) { 22183 un->un_ncmds_in_driver--; 22184 ASSERT(un->un_ncmds_in_driver >= 0); 22185 mutex_exit(SD_MUTEX(un)); 22186 err = ENOTTY; 22187 goto done_without_assess; 22188 } 22189 break; 22190 case FDEJECT: 22191 case DKIOCEJECT: 22192 case CDROMEJECT: 22193 if (!un->un_f_eject_media_supported) { 22194 un->un_ncmds_in_driver--; 22195 ASSERT(un->un_ncmds_in_driver >= 0); 22196 mutex_exit(SD_MUTEX(un)); 22197 err = ENOTTY; 22198 goto done_without_assess; 22199 } 22200 break; 22201 case DKIOCFLUSHWRITECACHE: 22202 mutex_exit(SD_MUTEX(un)); 22203 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22204 if (err != 0) { 22205 mutex_enter(SD_MUTEX(un)); 22206 un->un_ncmds_in_driver--; 22207 ASSERT(un->un_ncmds_in_driver >= 0); 22208 mutex_exit(SD_MUTEX(un)); 22209 err = EIO; 22210 goto done_quick_assess; 22211 } 22212 mutex_enter(SD_MUTEX(un)); 22213 /* FALLTHROUGH */ 22214 case DKIOCREMOVABLE: 22215 case DKIOCHOTPLUGGABLE: 22216 case DKIOCINFO: 22217 case DKIOCGMEDIAINFO: 22218 case DKIOCGMEDIAINFOEXT: 22219 case MHIOCENFAILFAST: 22220 case MHIOCSTATUS: 22221 case MHIOCTKOWN: 22222 case MHIOCRELEASE: 22223 case MHIOCGRP_INKEYS: 22224 case MHIOCGRP_INRESV: 22225 case MHIOCGRP_REGISTER: 22226 case MHIOCGRP_CLEAR: 22227 case MHIOCGRP_RESERVE: 22228 case MHIOCGRP_PREEMPTANDABORT: 22229 case MHIOCGRP_REGISTERANDIGNOREKEY: 22230 case CDROMCLOSETRAY: 22231 case USCSICMD: 22232 goto skip_ready_valid; 22233 default: 22234 break; 22235 } 22236 22237 mutex_exit(SD_MUTEX(un)); 22238 err = sd_ready_and_valid(ssc, SDPART(dev)); 22239 mutex_enter(SD_MUTEX(un)); 22240 22241 if (err != SD_READY_VALID) { 22242 switch (cmd) { 22243 case DKIOCSTATE: 22244 case CDROMGDRVSPEED: 22245 case CDROMSDRVSPEED: 22246 case FDEJECT: /* for eject command */ 22247 case DKIOCEJECT: 22248 case CDROMEJECT: 22249 case DKIOCREMOVABLE: 22250 case DKIOCHOTPLUGGABLE: 22251 break; 22252 default: 22253 if (un->un_f_has_removable_media) { 22254 err = ENXIO; 22255 } else { 22256 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 22257 if (err == SD_RESERVED_BY_OTHERS) { 22258 err = EACCES; 22259 } else { 22260 err = EIO; 22261 } 22262 } 22263 un->un_ncmds_in_driver--; 22264 ASSERT(un->un_ncmds_in_driver >= 0); 22265 mutex_exit(SD_MUTEX(un)); 22266 22267 goto done_without_assess; 22268 } 22269 } 22270 } 22271 22272 skip_ready_valid: 22273 mutex_exit(SD_MUTEX(un)); 22274 22275 switch (cmd) { 22276 case DKIOCINFO: 22277 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 22278 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 22279 break; 22280 22281 case DKIOCGMEDIAINFO: 22282 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 22283 err = sd_get_media_info(dev, (caddr_t)arg, flag); 22284 break; 22285 22286 case DKIOCGMEDIAINFOEXT: 22287 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n"); 22288 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag); 22289 break; 22290 22291 case DKIOCGGEOM: 22292 case DKIOCGVTOC: 22293 case DKIOCGEXTVTOC: 22294 case DKIOCGAPART: 22295 case DKIOCPARTINFO: 22296 case DKIOCEXTPARTINFO: 22297 case DKIOCSGEOM: 22298 case DKIOCSAPART: 22299 case DKIOCGETEFI: 22300 case DKIOCPARTITION: 22301 case DKIOCSVTOC: 22302 case DKIOCSEXTVTOC: 22303 case DKIOCSETEFI: 22304 case DKIOCGMBOOT: 22305 case DKIOCSMBOOT: 22306 case DKIOCG_PHYGEOM: 22307 case DKIOCG_VIRTGEOM: 22308 #if defined(__i386) || defined(__amd64) 22309 case DKIOCSETEXTPART: 22310 #endif 22311 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 22312 22313 /* TUR should spin up */ 22314 22315 if (un->un_f_has_removable_media) 22316 err = sd_send_scsi_TEST_UNIT_READY(ssc, 22317 SD_CHECK_FOR_MEDIA); 22318 22319 else 22320 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 22321 22322 if (err != 0) 22323 goto done_with_assess; 22324 22325 err = cmlb_ioctl(un->un_cmlbhandle, dev, 22326 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 22327 22328 if ((err == 0) && 22329 ((cmd == DKIOCSETEFI) || 22330 (un->un_f_pkstats_enabled) && 22331 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC || 22332 cmd == DKIOCSEXTVTOC))) { 22333 22334 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 22335 (void *)SD_PATH_DIRECT); 22336 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 22337 sd_set_pstats(un); 22338 SD_TRACE(SD_LOG_IO_PARTITION, un, 22339 "sd_ioctl: un:0x%p pstats created and " 22340 "set\n", un); 22341 } 22342 } 22343 22344 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) || 22345 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 22346 22347 mutex_enter(SD_MUTEX(un)); 22348 if (un->un_f_devid_supported && 22349 (un->un_f_opt_fab_devid == TRUE)) { 22350 if (un->un_devid == NULL) { 22351 sd_register_devid(ssc, SD_DEVINFO(un), 22352 SD_TARGET_IS_UNRESERVED); 22353 } else { 22354 /* 22355 * The device id for this disk 22356 * has been fabricated. The 22357 * device id must be preserved 22358 * by writing it back out to 22359 * disk. 22360 */ 22361 if (sd_write_deviceid(ssc) != 0) { 22362 ddi_devid_free(un->un_devid); 22363 un->un_devid = NULL; 22364 } 22365 } 22366 } 22367 mutex_exit(SD_MUTEX(un)); 22368 } 22369 22370 break; 22371 22372 case DKIOCLOCK: 22373 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 22374 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 22375 SD_PATH_STANDARD); 22376 goto done_with_assess; 22377 22378 case DKIOCUNLOCK: 22379 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 22380 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 22381 SD_PATH_STANDARD); 22382 goto done_with_assess; 22383 22384 case DKIOCSTATE: { 22385 enum dkio_state state; 22386 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 22387 22388 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 22389 err = EFAULT; 22390 } else { 22391 err = sd_check_media(dev, state); 22392 if (err == 0) { 22393 if (ddi_copyout(&un->un_mediastate, (void *)arg, 22394 sizeof (int), flag) != 0) 22395 err = EFAULT; 22396 } 22397 } 22398 break; 22399 } 22400 22401 case DKIOCREMOVABLE: 22402 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 22403 i = un->un_f_has_removable_media ? 1 : 0; 22404 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22405 err = EFAULT; 22406 } else { 22407 err = 0; 22408 } 22409 break; 22410 22411 case DKIOCHOTPLUGGABLE: 22412 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 22413 i = un->un_f_is_hotpluggable ? 1 : 0; 22414 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22415 err = EFAULT; 22416 } else { 22417 err = 0; 22418 } 22419 break; 22420 22421 case DKIOCREADONLY: 22422 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n"); 22423 i = 0; 22424 if ((ISCD(un) && !un->un_f_mmc_writable_media) || 22425 (sr_check_wp(dev) != 0)) { 22426 i = 1; 22427 } 22428 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 22429 err = EFAULT; 22430 } else { 22431 err = 0; 22432 } 22433 break; 22434 22435 case DKIOCGTEMPERATURE: 22436 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 22437 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 22438 break; 22439 22440 case MHIOCENFAILFAST: 22441 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 22442 if ((err = drv_priv(cred_p)) == 0) { 22443 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 22444 } 22445 break; 22446 22447 case MHIOCTKOWN: 22448 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 22449 if ((err = drv_priv(cred_p)) == 0) { 22450 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 22451 } 22452 break; 22453 22454 case MHIOCRELEASE: 22455 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 22456 if ((err = drv_priv(cred_p)) == 0) { 22457 err = sd_mhdioc_release(dev); 22458 } 22459 break; 22460 22461 case MHIOCSTATUS: 22462 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 22463 if ((err = drv_priv(cred_p)) == 0) { 22464 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) { 22465 case 0: 22466 err = 0; 22467 break; 22468 case EACCES: 22469 *rval_p = 1; 22470 err = 0; 22471 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22472 break; 22473 default: 22474 err = EIO; 22475 goto done_with_assess; 22476 } 22477 } 22478 break; 22479 22480 case MHIOCQRESERVE: 22481 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 22482 if ((err = drv_priv(cred_p)) == 0) { 22483 err = sd_reserve_release(dev, SD_RESERVE); 22484 } 22485 break; 22486 22487 case MHIOCREREGISTERDEVID: 22488 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 22489 if (drv_priv(cred_p) == EPERM) { 22490 err = EPERM; 22491 } else if (!un->un_f_devid_supported) { 22492 err = ENOTTY; 22493 } else { 22494 err = sd_mhdioc_register_devid(dev); 22495 } 22496 break; 22497 22498 case MHIOCGRP_INKEYS: 22499 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 22500 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22501 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22502 err = ENOTSUP; 22503 } else { 22504 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 22505 flag); 22506 } 22507 } 22508 break; 22509 22510 case MHIOCGRP_INRESV: 22511 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 22512 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 22513 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22514 err = ENOTSUP; 22515 } else { 22516 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 22517 } 22518 } 22519 break; 22520 22521 case MHIOCGRP_REGISTER: 22522 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 22523 if ((err = drv_priv(cred_p)) != EPERM) { 22524 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22525 err = ENOTSUP; 22526 } else if (arg != NULL) { 22527 mhioc_register_t reg; 22528 if (ddi_copyin((void *)arg, ®, 22529 sizeof (mhioc_register_t), flag) != 0) { 22530 err = EFAULT; 22531 } else { 22532 err = 22533 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22534 ssc, SD_SCSI3_REGISTER, 22535 (uchar_t *)®); 22536 if (err != 0) 22537 goto done_with_assess; 22538 } 22539 } 22540 } 22541 break; 22542 22543 case MHIOCGRP_CLEAR: 22544 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n"); 22545 if ((err = drv_priv(cred_p)) != EPERM) { 22546 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22547 err = ENOTSUP; 22548 } else if (arg != NULL) { 22549 mhioc_register_t reg; 22550 if (ddi_copyin((void *)arg, ®, 22551 sizeof (mhioc_register_t), flag) != 0) { 22552 err = EFAULT; 22553 } else { 22554 err = 22555 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22556 ssc, SD_SCSI3_CLEAR, 22557 (uchar_t *)®); 22558 if (err != 0) 22559 goto done_with_assess; 22560 } 22561 } 22562 } 22563 break; 22564 22565 case MHIOCGRP_RESERVE: 22566 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 22567 if ((err = drv_priv(cred_p)) != EPERM) { 22568 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22569 err = ENOTSUP; 22570 } else if (arg != NULL) { 22571 mhioc_resv_desc_t resv_desc; 22572 if (ddi_copyin((void *)arg, &resv_desc, 22573 sizeof (mhioc_resv_desc_t), flag) != 0) { 22574 err = EFAULT; 22575 } else { 22576 err = 22577 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22578 ssc, SD_SCSI3_RESERVE, 22579 (uchar_t *)&resv_desc); 22580 if (err != 0) 22581 goto done_with_assess; 22582 } 22583 } 22584 } 22585 break; 22586 22587 case MHIOCGRP_PREEMPTANDABORT: 22588 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 22589 if ((err = drv_priv(cred_p)) != EPERM) { 22590 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22591 err = ENOTSUP; 22592 } else if (arg != NULL) { 22593 mhioc_preemptandabort_t preempt_abort; 22594 if (ddi_copyin((void *)arg, &preempt_abort, 22595 sizeof (mhioc_preemptandabort_t), 22596 flag) != 0) { 22597 err = EFAULT; 22598 } else { 22599 err = 22600 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22601 ssc, SD_SCSI3_PREEMPTANDABORT, 22602 (uchar_t *)&preempt_abort); 22603 if (err != 0) 22604 goto done_with_assess; 22605 } 22606 } 22607 } 22608 break; 22609 22610 case MHIOCGRP_REGISTERANDIGNOREKEY: 22611 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 22612 if ((err = drv_priv(cred_p)) != EPERM) { 22613 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 22614 err = ENOTSUP; 22615 } else if (arg != NULL) { 22616 mhioc_registerandignorekey_t r_and_i; 22617 if (ddi_copyin((void *)arg, (void *)&r_and_i, 22618 sizeof (mhioc_registerandignorekey_t), 22619 flag) != 0) { 22620 err = EFAULT; 22621 } else { 22622 err = 22623 sd_send_scsi_PERSISTENT_RESERVE_OUT( 22624 ssc, SD_SCSI3_REGISTERANDIGNOREKEY, 22625 (uchar_t *)&r_and_i); 22626 if (err != 0) 22627 goto done_with_assess; 22628 } 22629 } 22630 } 22631 break; 22632 22633 case USCSICMD: 22634 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 22635 cr = ddi_get_cred(); 22636 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 22637 err = EPERM; 22638 } else { 22639 enum uio_seg uioseg; 22640 22641 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 22642 UIO_USERSPACE; 22643 if (un->un_f_format_in_progress == TRUE) { 22644 err = EAGAIN; 22645 break; 22646 } 22647 22648 err = sd_ssc_send(ssc, 22649 (struct uscsi_cmd *)arg, 22650 flag, uioseg, SD_PATH_STANDARD); 22651 if (err != 0) 22652 goto done_with_assess; 22653 else 22654 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 22655 } 22656 break; 22657 22658 case CDROMPAUSE: 22659 case CDROMRESUME: 22660 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 22661 if (!ISCD(un)) { 22662 err = ENOTTY; 22663 } else { 22664 err = sr_pause_resume(dev, cmd); 22665 } 22666 break; 22667 22668 case CDROMPLAYMSF: 22669 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 22670 if (!ISCD(un)) { 22671 err = ENOTTY; 22672 } else { 22673 err = sr_play_msf(dev, (caddr_t)arg, flag); 22674 } 22675 break; 22676 22677 case CDROMPLAYTRKIND: 22678 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 22679 #if defined(__i386) || defined(__amd64) 22680 /* 22681 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 22682 */ 22683 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22684 #else 22685 if (!ISCD(un)) { 22686 #endif 22687 err = ENOTTY; 22688 } else { 22689 err = sr_play_trkind(dev, (caddr_t)arg, flag); 22690 } 22691 break; 22692 22693 case CDROMREADTOCHDR: 22694 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 22695 if (!ISCD(un)) { 22696 err = ENOTTY; 22697 } else { 22698 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 22699 } 22700 break; 22701 22702 case CDROMREADTOCENTRY: 22703 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 22704 if (!ISCD(un)) { 22705 err = ENOTTY; 22706 } else { 22707 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 22708 } 22709 break; 22710 22711 case CDROMSTOP: 22712 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 22713 if (!ISCD(un)) { 22714 err = ENOTTY; 22715 } else { 22716 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22717 SD_TARGET_STOP, SD_PATH_STANDARD); 22718 goto done_with_assess; 22719 } 22720 break; 22721 22722 case CDROMSTART: 22723 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 22724 if (!ISCD(un)) { 22725 err = ENOTTY; 22726 } else { 22727 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22728 SD_TARGET_START, SD_PATH_STANDARD); 22729 goto done_with_assess; 22730 } 22731 break; 22732 22733 case CDROMCLOSETRAY: 22734 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 22735 if (!ISCD(un)) { 22736 err = ENOTTY; 22737 } else { 22738 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 22739 SD_TARGET_CLOSE, SD_PATH_STANDARD); 22740 goto done_with_assess; 22741 } 22742 break; 22743 22744 case FDEJECT: /* for eject command */ 22745 case DKIOCEJECT: 22746 case CDROMEJECT: 22747 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 22748 if (!un->un_f_eject_media_supported) { 22749 err = ENOTTY; 22750 } else { 22751 err = sr_eject(dev); 22752 } 22753 break; 22754 22755 case CDROMVOLCTRL: 22756 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 22757 if (!ISCD(un)) { 22758 err = ENOTTY; 22759 } else { 22760 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 22761 } 22762 break; 22763 22764 case CDROMSUBCHNL: 22765 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 22766 if (!ISCD(un)) { 22767 err = ENOTTY; 22768 } else { 22769 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 22770 } 22771 break; 22772 22773 case CDROMREADMODE2: 22774 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 22775 if (!ISCD(un)) { 22776 err = ENOTTY; 22777 } else if (un->un_f_cfg_is_atapi == TRUE) { 22778 /* 22779 * If the drive supports READ CD, use that instead of 22780 * switching the LBA size via a MODE SELECT 22781 * Block Descriptor 22782 */ 22783 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 22784 } else { 22785 err = sr_read_mode2(dev, (caddr_t)arg, flag); 22786 } 22787 break; 22788 22789 case CDROMREADMODE1: 22790 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 22791 if (!ISCD(un)) { 22792 err = ENOTTY; 22793 } else { 22794 err = sr_read_mode1(dev, (caddr_t)arg, flag); 22795 } 22796 break; 22797 22798 case CDROMREADOFFSET: 22799 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 22800 if (!ISCD(un)) { 22801 err = ENOTTY; 22802 } else { 22803 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 22804 flag); 22805 } 22806 break; 22807 22808 case CDROMSBLKMODE: 22809 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 22810 /* 22811 * There is no means of changing block size in case of atapi 22812 * drives, thus return ENOTTY if drive type is atapi 22813 */ 22814 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 22815 err = ENOTTY; 22816 } else if (un->un_f_mmc_cap == TRUE) { 22817 22818 /* 22819 * MMC Devices do not support changing the 22820 * logical block size 22821 * 22822 * Note: EINVAL is being returned instead of ENOTTY to 22823 * maintain consistancy with the original mmc 22824 * driver update. 22825 */ 22826 err = EINVAL; 22827 } else { 22828 mutex_enter(SD_MUTEX(un)); 22829 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 22830 (un->un_ncmds_in_transport > 0)) { 22831 mutex_exit(SD_MUTEX(un)); 22832 err = EINVAL; 22833 } else { 22834 mutex_exit(SD_MUTEX(un)); 22835 err = sr_change_blkmode(dev, cmd, arg, flag); 22836 } 22837 } 22838 break; 22839 22840 case CDROMGBLKMODE: 22841 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 22842 if (!ISCD(un)) { 22843 err = ENOTTY; 22844 } else if ((un->un_f_cfg_is_atapi != FALSE) && 22845 (un->un_f_blockcount_is_valid != FALSE)) { 22846 /* 22847 * Drive is an ATAPI drive so return target block 22848 * size for ATAPI drives since we cannot change the 22849 * blocksize on ATAPI drives. Used primarily to detect 22850 * if an ATAPI cdrom is present. 22851 */ 22852 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 22853 sizeof (int), flag) != 0) { 22854 err = EFAULT; 22855 } else { 22856 err = 0; 22857 } 22858 22859 } else { 22860 /* 22861 * Drive supports changing block sizes via a Mode 22862 * Select. 22863 */ 22864 err = sr_change_blkmode(dev, cmd, arg, flag); 22865 } 22866 break; 22867 22868 case CDROMGDRVSPEED: 22869 case CDROMSDRVSPEED: 22870 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 22871 if (!ISCD(un)) { 22872 err = ENOTTY; 22873 } else if (un->un_f_mmc_cap == TRUE) { 22874 /* 22875 * Note: In the future the driver implementation 22876 * for getting and 22877 * setting cd speed should entail: 22878 * 1) If non-mmc try the Toshiba mode page 22879 * (sr_change_speed) 22880 * 2) If mmc but no support for Real Time Streaming try 22881 * the SET CD SPEED (0xBB) command 22882 * (sr_atapi_change_speed) 22883 * 3) If mmc and support for Real Time Streaming 22884 * try the GET PERFORMANCE and SET STREAMING 22885 * commands (not yet implemented, 4380808) 22886 */ 22887 /* 22888 * As per recent MMC spec, CD-ROM speed is variable 22889 * and changes with LBA. Since there is no such 22890 * things as drive speed now, fail this ioctl. 22891 * 22892 * Note: EINVAL is returned for consistancy of original 22893 * implementation which included support for getting 22894 * the drive speed of mmc devices but not setting 22895 * the drive speed. Thus EINVAL would be returned 22896 * if a set request was made for an mmc device. 22897 * We no longer support get or set speed for 22898 * mmc but need to remain consistent with regard 22899 * to the error code returned. 22900 */ 22901 err = EINVAL; 22902 } else if (un->un_f_cfg_is_atapi == TRUE) { 22903 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22904 } else { 22905 err = sr_change_speed(dev, cmd, arg, flag); 22906 } 22907 break; 22908 22909 case CDROMCDDA: 22910 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22911 if (!ISCD(un)) { 22912 err = ENOTTY; 22913 } else { 22914 err = sr_read_cdda(dev, (void *)arg, flag); 22915 } 22916 break; 22917 22918 case CDROMCDXA: 22919 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22920 if (!ISCD(un)) { 22921 err = ENOTTY; 22922 } else { 22923 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22924 } 22925 break; 22926 22927 case CDROMSUBCODE: 22928 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22929 if (!ISCD(un)) { 22930 err = ENOTTY; 22931 } else { 22932 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22933 } 22934 break; 22935 22936 22937 #ifdef SDDEBUG 22938 /* RESET/ABORTS testing ioctls */ 22939 case DKIOCRESET: { 22940 int reset_level; 22941 22942 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22943 err = EFAULT; 22944 } else { 22945 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22946 "reset_level = 0x%lx\n", reset_level); 22947 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22948 err = 0; 22949 } else { 22950 err = EIO; 22951 } 22952 } 22953 break; 22954 } 22955 22956 case DKIOCABORT: 22957 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22958 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22959 err = 0; 22960 } else { 22961 err = EIO; 22962 } 22963 break; 22964 #endif 22965 22966 #ifdef SD_FAULT_INJECTION 22967 /* SDIOC FaultInjection testing ioctls */ 22968 case SDIOCSTART: 22969 case SDIOCSTOP: 22970 case SDIOCINSERTPKT: 22971 case SDIOCINSERTXB: 22972 case SDIOCINSERTUN: 22973 case SDIOCINSERTARQ: 22974 case SDIOCPUSH: 22975 case SDIOCRETRIEVE: 22976 case SDIOCRUN: 22977 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22978 "SDIOC detected cmd:0x%X:\n", cmd); 22979 /* call error generator */ 22980 sd_faultinjection_ioctl(cmd, arg, un); 22981 err = 0; 22982 break; 22983 22984 #endif /* SD_FAULT_INJECTION */ 22985 22986 case DKIOCFLUSHWRITECACHE: 22987 { 22988 struct dk_callback *dkc = (struct dk_callback *)arg; 22989 22990 mutex_enter(SD_MUTEX(un)); 22991 if (!un->un_f_sync_cache_supported || 22992 !un->un_f_write_cache_enabled) { 22993 err = un->un_f_sync_cache_supported ? 22994 0 : ENOTSUP; 22995 mutex_exit(SD_MUTEX(un)); 22996 if ((flag & FKIOCTL) && dkc != NULL && 22997 dkc->dkc_callback != NULL) { 22998 (*dkc->dkc_callback)(dkc->dkc_cookie, 22999 err); 23000 /* 23001 * Did callback and reported error. 23002 * Since we did a callback, ioctl 23003 * should return 0. 23004 */ 23005 err = 0; 23006 } 23007 break; 23008 } 23009 mutex_exit(SD_MUTEX(un)); 23010 23011 if ((flag & FKIOCTL) && dkc != NULL && 23012 dkc->dkc_callback != NULL) { 23013 /* async SYNC CACHE request */ 23014 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 23015 } else { 23016 /* synchronous SYNC CACHE request */ 23017 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 23018 } 23019 } 23020 break; 23021 23022 case DKIOCGETWCE: { 23023 23024 int wce; 23025 23026 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) { 23027 break; 23028 } 23029 23030 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 23031 err = EFAULT; 23032 } 23033 break; 23034 } 23035 23036 case DKIOCSETWCE: { 23037 23038 int wce, sync_supported; 23039 int cur_wce = 0; 23040 23041 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 23042 err = EFAULT; 23043 break; 23044 } 23045 23046 /* 23047 * Synchronize multiple threads trying to enable 23048 * or disable the cache via the un_f_wcc_cv 23049 * condition variable. 23050 */ 23051 mutex_enter(SD_MUTEX(un)); 23052 23053 /* 23054 * Don't allow the cache to be enabled if the 23055 * config file has it disabled. 23056 */ 23057 if (un->un_f_opt_disable_cache && wce) { 23058 mutex_exit(SD_MUTEX(un)); 23059 err = EINVAL; 23060 break; 23061 } 23062 23063 /* 23064 * Wait for write cache change in progress 23065 * bit to be clear before proceeding. 23066 */ 23067 while (un->un_f_wcc_inprog) 23068 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 23069 23070 un->un_f_wcc_inprog = 1; 23071 23072 mutex_exit(SD_MUTEX(un)); 23073 23074 /* 23075 * Get the current write cache state 23076 */ 23077 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) { 23078 mutex_enter(SD_MUTEX(un)); 23079 un->un_f_wcc_inprog = 0; 23080 cv_broadcast(&un->un_wcc_cv); 23081 mutex_exit(SD_MUTEX(un)); 23082 break; 23083 } 23084 23085 mutex_enter(SD_MUTEX(un)); 23086 un->un_f_write_cache_enabled = (cur_wce != 0); 23087 23088 if (un->un_f_write_cache_enabled && wce == 0) { 23089 /* 23090 * Disable the write cache. Don't clear 23091 * un_f_write_cache_enabled until after 23092 * the mode select and flush are complete. 23093 */ 23094 sync_supported = un->un_f_sync_cache_supported; 23095 23096 /* 23097 * If cache flush is suppressed, we assume that the 23098 * controller firmware will take care of managing the 23099 * write cache for us: no need to explicitly 23100 * disable it. 23101 */ 23102 if (!un->un_f_suppress_cache_flush) { 23103 mutex_exit(SD_MUTEX(un)); 23104 if ((err = sd_cache_control(ssc, 23105 SD_CACHE_NOCHANGE, 23106 SD_CACHE_DISABLE)) == 0 && 23107 sync_supported) { 23108 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 23109 NULL); 23110 } 23111 } else { 23112 mutex_exit(SD_MUTEX(un)); 23113 } 23114 23115 mutex_enter(SD_MUTEX(un)); 23116 if (err == 0) { 23117 un->un_f_write_cache_enabled = 0; 23118 } 23119 23120 } else if (!un->un_f_write_cache_enabled && wce != 0) { 23121 /* 23122 * Set un_f_write_cache_enabled first, so there is 23123 * no window where the cache is enabled, but the 23124 * bit says it isn't. 23125 */ 23126 un->un_f_write_cache_enabled = 1; 23127 23128 /* 23129 * If cache flush is suppressed, we assume that the 23130 * controller firmware will take care of managing the 23131 * write cache for us: no need to explicitly 23132 * enable it. 23133 */ 23134 if (!un->un_f_suppress_cache_flush) { 23135 mutex_exit(SD_MUTEX(un)); 23136 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE, 23137 SD_CACHE_ENABLE); 23138 } else { 23139 mutex_exit(SD_MUTEX(un)); 23140 } 23141 23142 mutex_enter(SD_MUTEX(un)); 23143 23144 if (err) { 23145 un->un_f_write_cache_enabled = 0; 23146 } 23147 } 23148 23149 un->un_f_wcc_inprog = 0; 23150 cv_broadcast(&un->un_wcc_cv); 23151 mutex_exit(SD_MUTEX(un)); 23152 break; 23153 } 23154 23155 default: 23156 err = ENOTTY; 23157 break; 23158 } 23159 mutex_enter(SD_MUTEX(un)); 23160 un->un_ncmds_in_driver--; 23161 ASSERT(un->un_ncmds_in_driver >= 0); 23162 mutex_exit(SD_MUTEX(un)); 23163 23164 23165 done_without_assess: 23166 sd_ssc_fini(ssc); 23167 23168 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23169 return (err); 23170 23171 done_with_assess: 23172 mutex_enter(SD_MUTEX(un)); 23173 un->un_ncmds_in_driver--; 23174 ASSERT(un->un_ncmds_in_driver >= 0); 23175 mutex_exit(SD_MUTEX(un)); 23176 23177 done_quick_assess: 23178 if (err != 0) 23179 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23180 /* Uninitialize sd_ssc_t pointer */ 23181 sd_ssc_fini(ssc); 23182 23183 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 23184 return (err); 23185 } 23186 23187 23188 /* 23189 * Function: sd_dkio_ctrl_info 23190 * 23191 * Description: This routine is the driver entry point for handling controller 23192 * information ioctl requests (DKIOCINFO). 23193 * 23194 * Arguments: dev - the device number 23195 * arg - pointer to user provided dk_cinfo structure 23196 * specifying the controller type and attributes. 23197 * flag - this argument is a pass through to ddi_copyxxx() 23198 * directly from the mode argument of ioctl(). 23199 * 23200 * Return Code: 0 23201 * EFAULT 23202 * ENXIO 23203 */ 23204 23205 static int 23206 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 23207 { 23208 struct sd_lun *un = NULL; 23209 struct dk_cinfo *info; 23210 dev_info_t *pdip; 23211 int lun, tgt; 23212 23213 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23214 return (ENXIO); 23215 } 23216 23217 info = (struct dk_cinfo *) 23218 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 23219 23220 switch (un->un_ctype) { 23221 case CTYPE_CDROM: 23222 info->dki_ctype = DKC_CDROM; 23223 break; 23224 default: 23225 info->dki_ctype = DKC_SCSI_CCS; 23226 break; 23227 } 23228 pdip = ddi_get_parent(SD_DEVINFO(un)); 23229 info->dki_cnum = ddi_get_instance(pdip); 23230 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 23231 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 23232 } else { 23233 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 23234 DK_DEVLEN - 1); 23235 } 23236 23237 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23238 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 23239 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 23240 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 23241 23242 /* Unit Information */ 23243 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 23244 info->dki_slave = ((tgt << 3) | lun); 23245 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 23246 DK_DEVLEN - 1); 23247 info->dki_flags = DKI_FMTVOL; 23248 info->dki_partition = SDPART(dev); 23249 23250 /* Max Transfer size of this device in blocks */ 23251 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 23252 info->dki_addr = 0; 23253 info->dki_space = 0; 23254 info->dki_prio = 0; 23255 info->dki_vec = 0; 23256 23257 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 23258 kmem_free(info, sizeof (struct dk_cinfo)); 23259 return (EFAULT); 23260 } else { 23261 kmem_free(info, sizeof (struct dk_cinfo)); 23262 return (0); 23263 } 23264 } 23265 23266 /* 23267 * Function: sd_get_media_info_com 23268 * 23269 * Description: This routine returns the information required to populate 23270 * the fields for the dk_minfo/dk_minfo_ext structures. 23271 * 23272 * Arguments: dev - the device number 23273 * dki_media_type - media_type 23274 * dki_lbsize - logical block size 23275 * dki_capacity - capacity in blocks 23276 * dki_pbsize - physical block size (if requested) 23277 * 23278 * Return Code: 0 23279 * EACCESS 23280 * EFAULT 23281 * ENXIO 23282 * EIO 23283 */ 23284 static int 23285 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize, 23286 diskaddr_t *dki_capacity, uint_t *dki_pbsize) 23287 { 23288 struct sd_lun *un = NULL; 23289 struct uscsi_cmd com; 23290 struct scsi_inquiry *sinq; 23291 uint64_t media_capacity; 23292 uchar_t *out_data; 23293 uchar_t *rqbuf; 23294 int rval = 0; 23295 int rtn; 23296 sd_ssc_t *ssc; 23297 23298 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 23299 (un->un_state == SD_STATE_OFFLINE)) { 23300 return (ENXIO); 23301 } 23302 23303 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n"); 23304 23305 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 23306 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 23307 ssc = sd_ssc_init(un); 23308 23309 /* Issue a TUR to determine if the drive is ready with media present */ 23310 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA); 23311 if (rval == ENXIO) { 23312 goto done; 23313 } else if (rval != 0) { 23314 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23315 } 23316 23317 /* Now get configuration data */ 23318 if (ISCD(un)) { 23319 *dki_media_type = DK_CDROM; 23320 23321 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 23322 if (un->un_f_mmc_cap == TRUE) { 23323 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, 23324 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 23325 SD_PATH_STANDARD); 23326 23327 if (rtn) { 23328 /* 23329 * We ignore all failures for CD and need to 23330 * put the assessment before processing code 23331 * to avoid missing assessment for FMA. 23332 */ 23333 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23334 /* 23335 * Failed for other than an illegal request 23336 * or command not supported 23337 */ 23338 if ((com.uscsi_status == STATUS_CHECK) && 23339 (com.uscsi_rqstatus == STATUS_GOOD)) { 23340 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 23341 (rqbuf[12] != 0x20)) { 23342 rval = EIO; 23343 goto no_assessment; 23344 } 23345 } 23346 } else { 23347 /* 23348 * The GET CONFIGURATION command succeeded 23349 * so set the media type according to the 23350 * returned data 23351 */ 23352 *dki_media_type = out_data[6]; 23353 *dki_media_type <<= 8; 23354 *dki_media_type |= out_data[7]; 23355 } 23356 } 23357 } else { 23358 /* 23359 * The profile list is not available, so we attempt to identify 23360 * the media type based on the inquiry data 23361 */ 23362 sinq = SD_INQUIRY(un); 23363 if ((sinq->inq_dtype == DTYPE_DIRECT) || 23364 (sinq->inq_dtype == DTYPE_OPTICAL)) { 23365 /* This is a direct access device or optical disk */ 23366 *dki_media_type = DK_FIXED_DISK; 23367 23368 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 23369 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 23370 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 23371 *dki_media_type = DK_ZIP; 23372 } else if ( 23373 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 23374 *dki_media_type = DK_JAZ; 23375 } 23376 } 23377 } else { 23378 /* 23379 * Not a CD, direct access or optical disk so return 23380 * unknown media 23381 */ 23382 *dki_media_type = DK_UNKNOWN; 23383 } 23384 } 23385 23386 /* 23387 * Now read the capacity so we can provide the lbasize, 23388 * pbsize and capacity. 23389 */ 23390 rval = sd_read_capacity(ssc, SD_PATH_DIRECT); 23391 switch (rval) { 23392 case 0: 23393 media_capacity = un->un_blockcount; 23394 /* 23395 * sd_send_scsi_READ_CAPACITY() reports capacity in 23396 * un->un_sys_blocksize chunks. So we need to convert 23397 * it into cap.lbsize chunks. 23398 */ 23399 if (un->un_f_has_removable_media && 23400 (un->un_tgt_blocksize != 0)) { 23401 media_capacity *= un->un_sys_blocksize; 23402 media_capacity /= un->un_tgt_blocksize; 23403 } 23404 *dki_lbsize = un->un_tgt_blocksize; 23405 *dki_capacity = media_capacity; 23406 if (dki_pbsize != NULL) 23407 *dki_pbsize = un->un_phy_blocksize; 23408 break; 23409 case EACCES: 23410 rval = EACCES; 23411 break; 23412 default: 23413 rval = EIO; 23414 } 23415 23416 done: 23417 if (rval != 0) { 23418 if (rval == EIO) 23419 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23420 else 23421 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23422 } 23423 no_assessment: 23424 sd_ssc_fini(ssc); 23425 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 23426 kmem_free(rqbuf, SENSE_LENGTH); 23427 return (rval); 23428 } 23429 23430 /* 23431 * Function: sd_get_media_info 23432 * 23433 * Description: This routine is the driver entry point for handling ioctl 23434 * requests for the media type or command set profile used by the 23435 * drive to operate on the media (DKIOCGMEDIAINFO). 23436 * 23437 * Arguments: dev - the device number 23438 * arg - pointer to user provided dk_minfo structure 23439 * specifying the media type, logical block size and 23440 * drive capacity. 23441 * flag - this argument is a pass through to ddi_copyxxx() 23442 * directly from the mode argument of ioctl(). 23443 * 23444 * Return Code: returns the value from sd_get_media_info_com 23445 */ 23446 static int 23447 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 23448 { 23449 struct dk_minfo mi; 23450 int rval; 23451 23452 rval = sd_get_media_info_com(dev, &mi.dki_media_type, 23453 &mi.dki_lbsize, &mi.dki_capacity, NULL); 23454 23455 if (rval) 23456 return (rval); 23457 if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag)) 23458 rval = EFAULT; 23459 return (rval); 23460 } 23461 23462 /* 23463 * Function: sd_get_media_info_ext 23464 * 23465 * Description: This routine is the driver entry point for handling ioctl 23466 * requests for the media type or command set profile used by the 23467 * drive to operate on the media (DKIOCGMEDIAINFOEXT). The 23468 * difference this ioctl and DKIOCGMEDIAINFO is the return value 23469 * of this ioctl contains both logical block size and physical 23470 * block size. 23471 * 23472 * 23473 * Arguments: dev - the device number 23474 * arg - pointer to user provided dk_minfo_ext structure 23475 * specifying the media type, logical block size, 23476 * physical block size and disk capacity. 23477 * flag - this argument is a pass through to ddi_copyxxx() 23478 * directly from the mode argument of ioctl(). 23479 * 23480 * Return Code: returns the value from sd_get_media_info_com 23481 */ 23482 static int 23483 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag) 23484 { 23485 struct dk_minfo_ext mie; 23486 int rval = 0; 23487 23488 rval = sd_get_media_info_com(dev, &mie.dki_media_type, 23489 &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize); 23490 23491 if (rval) 23492 return (rval); 23493 if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag)) 23494 rval = EFAULT; 23495 return (rval); 23496 23497 } 23498 23499 /* 23500 * Function: sd_watch_request_submit 23501 * 23502 * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit 23503 * depending on which is supported by device. 23504 */ 23505 static opaque_t 23506 sd_watch_request_submit(struct sd_lun *un) 23507 { 23508 dev_t dev; 23509 23510 /* All submissions are unified to use same device number */ 23511 dev = sd_make_device(SD_DEVINFO(un)); 23512 23513 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23514 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un), 23515 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23516 (caddr_t)dev)); 23517 } else { 23518 return (scsi_watch_request_submit(SD_SCSI_DEVP(un), 23519 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 23520 (caddr_t)dev)); 23521 } 23522 } 23523 23524 23525 /* 23526 * Function: sd_check_media 23527 * 23528 * Description: This utility routine implements the functionality for the 23529 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 23530 * driver state changes from that specified by the user 23531 * (inserted or ejected). For example, if the user specifies 23532 * DKIO_EJECTED and the current media state is inserted this 23533 * routine will immediately return DKIO_INSERTED. However, if the 23534 * current media state is not inserted the user thread will be 23535 * blocked until the drive state changes. If DKIO_NONE is specified 23536 * the user thread will block until a drive state change occurs. 23537 * 23538 * Arguments: dev - the device number 23539 * state - user pointer to a dkio_state, updated with the current 23540 * drive state at return. 23541 * 23542 * Return Code: ENXIO 23543 * EIO 23544 * EAGAIN 23545 * EINTR 23546 */ 23547 23548 static int 23549 sd_check_media(dev_t dev, enum dkio_state state) 23550 { 23551 struct sd_lun *un = NULL; 23552 enum dkio_state prev_state; 23553 opaque_t token = NULL; 23554 int rval = 0; 23555 sd_ssc_t *ssc; 23556 23557 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23558 return (ENXIO); 23559 } 23560 23561 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 23562 23563 ssc = sd_ssc_init(un); 23564 23565 mutex_enter(SD_MUTEX(un)); 23566 23567 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 23568 "state=%x, mediastate=%x\n", state, un->un_mediastate); 23569 23570 prev_state = un->un_mediastate; 23571 23572 /* is there anything to do? */ 23573 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 23574 /* 23575 * submit the request to the scsi_watch service; 23576 * scsi_media_watch_cb() does the real work 23577 */ 23578 mutex_exit(SD_MUTEX(un)); 23579 23580 /* 23581 * This change handles the case where a scsi watch request is 23582 * added to a device that is powered down. To accomplish this 23583 * we power up the device before adding the scsi watch request, 23584 * since the scsi watch sends a TUR directly to the device 23585 * which the device cannot handle if it is powered down. 23586 */ 23587 if (sd_pm_entry(un) != DDI_SUCCESS) { 23588 mutex_enter(SD_MUTEX(un)); 23589 goto done; 23590 } 23591 23592 token = sd_watch_request_submit(un); 23593 23594 sd_pm_exit(un); 23595 23596 mutex_enter(SD_MUTEX(un)); 23597 if (token == NULL) { 23598 rval = EAGAIN; 23599 goto done; 23600 } 23601 23602 /* 23603 * This is a special case IOCTL that doesn't return 23604 * until the media state changes. Routine sdpower 23605 * knows about and handles this so don't count it 23606 * as an active cmd in the driver, which would 23607 * keep the device busy to the pm framework. 23608 * If the count isn't decremented the device can't 23609 * be powered down. 23610 */ 23611 un->un_ncmds_in_driver--; 23612 ASSERT(un->un_ncmds_in_driver >= 0); 23613 23614 /* 23615 * if a prior request had been made, this will be the same 23616 * token, as scsi_watch was designed that way. 23617 */ 23618 un->un_swr_token = token; 23619 un->un_specified_mediastate = state; 23620 23621 /* 23622 * now wait for media change 23623 * we will not be signalled unless mediastate == state but it is 23624 * still better to test for this condition, since there is a 23625 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 23626 */ 23627 SD_TRACE(SD_LOG_COMMON, un, 23628 "sd_check_media: waiting for media state change\n"); 23629 while (un->un_mediastate == state) { 23630 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 23631 SD_TRACE(SD_LOG_COMMON, un, 23632 "sd_check_media: waiting for media state " 23633 "was interrupted\n"); 23634 un->un_ncmds_in_driver++; 23635 rval = EINTR; 23636 goto done; 23637 } 23638 SD_TRACE(SD_LOG_COMMON, un, 23639 "sd_check_media: received signal, state=%x\n", 23640 un->un_mediastate); 23641 } 23642 /* 23643 * Inc the counter to indicate the device once again 23644 * has an active outstanding cmd. 23645 */ 23646 un->un_ncmds_in_driver++; 23647 } 23648 23649 /* invalidate geometry */ 23650 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 23651 sr_ejected(un); 23652 } 23653 23654 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 23655 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 23656 mutex_exit(SD_MUTEX(un)); 23657 /* 23658 * Since the following routines use SD_PATH_DIRECT, we must 23659 * call PM directly before the upcoming disk accesses. This 23660 * may cause the disk to be power/spin up. 23661 */ 23662 23663 if (sd_pm_entry(un) == DDI_SUCCESS) { 23664 rval = sd_read_capacity(ssc, SD_PATH_DIRECT); 23665 if (rval != 0) { 23666 sd_pm_exit(un); 23667 if (rval == EIO) 23668 sd_ssc_assessment(ssc, 23669 SD_FMT_STATUS_CHECK); 23670 else 23671 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23672 mutex_enter(SD_MUTEX(un)); 23673 goto done; 23674 } 23675 } else { 23676 rval = EIO; 23677 mutex_enter(SD_MUTEX(un)); 23678 goto done; 23679 } 23680 mutex_enter(SD_MUTEX(un)); 23681 23682 /* 23683 * Check if the media in the device is writable or not 23684 */ 23685 if (ISCD(un)) { 23686 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 23687 } 23688 23689 mutex_exit(SD_MUTEX(un)); 23690 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 23691 if ((cmlb_validate(un->un_cmlbhandle, 0, 23692 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 23693 sd_set_pstats(un); 23694 SD_TRACE(SD_LOG_IO_PARTITION, un, 23695 "sd_check_media: un:0x%p pstats created and " 23696 "set\n", un); 23697 } 23698 23699 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 23700 SD_PATH_DIRECT); 23701 23702 sd_pm_exit(un); 23703 23704 if (rval != 0) { 23705 if (rval == EIO) 23706 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23707 else 23708 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23709 } 23710 23711 mutex_enter(SD_MUTEX(un)); 23712 } 23713 done: 23714 sd_ssc_fini(ssc); 23715 un->un_f_watcht_stopped = FALSE; 23716 if (token != NULL && un->un_swr_token != NULL) { 23717 /* 23718 * Use of this local token and the mutex ensures that we avoid 23719 * some race conditions associated with terminating the 23720 * scsi watch. 23721 */ 23722 token = un->un_swr_token; 23723 mutex_exit(SD_MUTEX(un)); 23724 (void) scsi_watch_request_terminate(token, 23725 SCSI_WATCH_TERMINATE_WAIT); 23726 if (scsi_watch_get_ref_count(token) == 0) { 23727 mutex_enter(SD_MUTEX(un)); 23728 un->un_swr_token = (opaque_t)NULL; 23729 } else { 23730 mutex_enter(SD_MUTEX(un)); 23731 } 23732 } 23733 23734 /* 23735 * Update the capacity kstat value, if no media previously 23736 * (capacity kstat is 0) and a media has been inserted 23737 * (un_f_blockcount_is_valid == TRUE) 23738 */ 23739 if (un->un_errstats) { 23740 struct sd_errstats *stp = NULL; 23741 23742 stp = (struct sd_errstats *)un->un_errstats->ks_data; 23743 if ((stp->sd_capacity.value.ui64 == 0) && 23744 (un->un_f_blockcount_is_valid == TRUE)) { 23745 stp->sd_capacity.value.ui64 = 23746 (uint64_t)((uint64_t)un->un_blockcount * 23747 un->un_sys_blocksize); 23748 } 23749 } 23750 mutex_exit(SD_MUTEX(un)); 23751 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 23752 return (rval); 23753 } 23754 23755 23756 /* 23757 * Function: sd_delayed_cv_broadcast 23758 * 23759 * Description: Delayed cv_broadcast to allow for target to recover from media 23760 * insertion. 23761 * 23762 * Arguments: arg - driver soft state (unit) structure 23763 */ 23764 23765 static void 23766 sd_delayed_cv_broadcast(void *arg) 23767 { 23768 struct sd_lun *un = arg; 23769 23770 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 23771 23772 mutex_enter(SD_MUTEX(un)); 23773 un->un_dcvb_timeid = NULL; 23774 cv_broadcast(&un->un_state_cv); 23775 mutex_exit(SD_MUTEX(un)); 23776 } 23777 23778 23779 /* 23780 * Function: sd_media_watch_cb 23781 * 23782 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 23783 * routine processes the TUR sense data and updates the driver 23784 * state if a transition has occurred. The user thread 23785 * (sd_check_media) is then signalled. 23786 * 23787 * Arguments: arg - the device 'dev_t' is used for context to discriminate 23788 * among multiple watches that share this callback function 23789 * resultp - scsi watch facility result packet containing scsi 23790 * packet, status byte and sense data 23791 * 23792 * Return Code: 0 for success, -1 for failure 23793 */ 23794 23795 static int 23796 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 23797 { 23798 struct sd_lun *un; 23799 struct scsi_status *statusp = resultp->statusp; 23800 uint8_t *sensep = (uint8_t *)resultp->sensep; 23801 enum dkio_state state = DKIO_NONE; 23802 dev_t dev = (dev_t)arg; 23803 uchar_t actual_sense_length; 23804 uint8_t skey, asc, ascq; 23805 23806 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23807 return (-1); 23808 } 23809 actual_sense_length = resultp->actual_sense_length; 23810 23811 mutex_enter(SD_MUTEX(un)); 23812 SD_TRACE(SD_LOG_COMMON, un, 23813 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 23814 *((char *)statusp), (void *)sensep, actual_sense_length); 23815 23816 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 23817 un->un_mediastate = DKIO_DEV_GONE; 23818 cv_broadcast(&un->un_state_cv); 23819 mutex_exit(SD_MUTEX(un)); 23820 23821 return (0); 23822 } 23823 23824 if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) { 23825 if (sd_gesn_media_data_valid(resultp->mmc_data)) { 23826 if ((resultp->mmc_data[5] & 23827 SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) { 23828 state = DKIO_INSERTED; 23829 } else { 23830 state = DKIO_EJECTED; 23831 } 23832 if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) == 23833 SD_GESN_MEDIA_EVENT_EJECTREQUEST) { 23834 sd_log_eject_request_event(un, KM_NOSLEEP); 23835 } 23836 } 23837 } else if (sensep != NULL) { 23838 /* 23839 * If there was a check condition then sensep points to valid 23840 * sense data. If status was not a check condition but a 23841 * reservation or busy status then the new state is DKIO_NONE. 23842 */ 23843 skey = scsi_sense_key(sensep); 23844 asc = scsi_sense_asc(sensep); 23845 ascq = scsi_sense_ascq(sensep); 23846 23847 SD_INFO(SD_LOG_COMMON, un, 23848 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 23849 skey, asc, ascq); 23850 /* This routine only uses up to 13 bytes of sense data. */ 23851 if (actual_sense_length >= 13) { 23852 if (skey == KEY_UNIT_ATTENTION) { 23853 if (asc == 0x28) { 23854 state = DKIO_INSERTED; 23855 } 23856 } else if (skey == KEY_NOT_READY) { 23857 /* 23858 * Sense data of 02/06/00 means that the 23859 * drive could not read the media (No 23860 * reference position found). In this case 23861 * to prevent a hang on the DKIOCSTATE IOCTL 23862 * we set the media state to DKIO_INSERTED. 23863 */ 23864 if (asc == 0x06 && ascq == 0x00) 23865 state = DKIO_INSERTED; 23866 23867 /* 23868 * if 02/04/02 means that the host 23869 * should send start command. Explicitly 23870 * leave the media state as is 23871 * (inserted) as the media is inserted 23872 * and host has stopped device for PM 23873 * reasons. Upon next true read/write 23874 * to this media will bring the 23875 * device to the right state good for 23876 * media access. 23877 */ 23878 if (asc == 0x3a) { 23879 state = DKIO_EJECTED; 23880 } else { 23881 /* 23882 * If the drive is busy with an 23883 * operation or long write, keep the 23884 * media in an inserted state. 23885 */ 23886 23887 if ((asc == 0x04) && 23888 ((ascq == 0x02) || 23889 (ascq == 0x07) || 23890 (ascq == 0x08))) { 23891 state = DKIO_INSERTED; 23892 } 23893 } 23894 } else if (skey == KEY_NO_SENSE) { 23895 if ((asc == 0x00) && (ascq == 0x00)) { 23896 /* 23897 * Sense Data 00/00/00 does not provide 23898 * any information about the state of 23899 * the media. Ignore it. 23900 */ 23901 mutex_exit(SD_MUTEX(un)); 23902 return (0); 23903 } 23904 } 23905 } 23906 } else if ((*((char *)statusp) == STATUS_GOOD) && 23907 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 23908 state = DKIO_INSERTED; 23909 } 23910 23911 SD_TRACE(SD_LOG_COMMON, un, 23912 "sd_media_watch_cb: state=%x, specified=%x\n", 23913 state, un->un_specified_mediastate); 23914 23915 /* 23916 * now signal the waiting thread if this is *not* the specified state; 23917 * delay the signal if the state is DKIO_INSERTED to allow the target 23918 * to recover 23919 */ 23920 if (state != un->un_specified_mediastate) { 23921 un->un_mediastate = state; 23922 if (state == DKIO_INSERTED) { 23923 /* 23924 * delay the signal to give the drive a chance 23925 * to do what it apparently needs to do 23926 */ 23927 SD_TRACE(SD_LOG_COMMON, un, 23928 "sd_media_watch_cb: delayed cv_broadcast\n"); 23929 if (un->un_dcvb_timeid == NULL) { 23930 un->un_dcvb_timeid = 23931 timeout(sd_delayed_cv_broadcast, un, 23932 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 23933 } 23934 } else { 23935 SD_TRACE(SD_LOG_COMMON, un, 23936 "sd_media_watch_cb: immediate cv_broadcast\n"); 23937 cv_broadcast(&un->un_state_cv); 23938 } 23939 } 23940 mutex_exit(SD_MUTEX(un)); 23941 return (0); 23942 } 23943 23944 23945 /* 23946 * Function: sd_dkio_get_temp 23947 * 23948 * Description: This routine is the driver entry point for handling ioctl 23949 * requests to get the disk temperature. 23950 * 23951 * Arguments: dev - the device number 23952 * arg - pointer to user provided dk_temperature structure. 23953 * flag - this argument is a pass through to ddi_copyxxx() 23954 * directly from the mode argument of ioctl(). 23955 * 23956 * Return Code: 0 23957 * EFAULT 23958 * ENXIO 23959 * EAGAIN 23960 */ 23961 23962 static int 23963 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 23964 { 23965 struct sd_lun *un = NULL; 23966 struct dk_temperature *dktemp = NULL; 23967 uchar_t *temperature_page; 23968 int rval = 0; 23969 int path_flag = SD_PATH_STANDARD; 23970 sd_ssc_t *ssc; 23971 23972 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23973 return (ENXIO); 23974 } 23975 23976 ssc = sd_ssc_init(un); 23977 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 23978 23979 /* copyin the disk temp argument to get the user flags */ 23980 if (ddi_copyin((void *)arg, dktemp, 23981 sizeof (struct dk_temperature), flag) != 0) { 23982 rval = EFAULT; 23983 goto done; 23984 } 23985 23986 /* Initialize the temperature to invalid. */ 23987 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 23988 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 23989 23990 /* 23991 * Note: Investigate removing the "bypass pm" semantic. 23992 * Can we just bypass PM always? 23993 */ 23994 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 23995 path_flag = SD_PATH_DIRECT; 23996 ASSERT(!mutex_owned(&un->un_pm_mutex)); 23997 mutex_enter(&un->un_pm_mutex); 23998 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 23999 /* 24000 * If DKT_BYPASS_PM is set, and the drive happens to be 24001 * in low power mode, we can not wake it up, Need to 24002 * return EAGAIN. 24003 */ 24004 mutex_exit(&un->un_pm_mutex); 24005 rval = EAGAIN; 24006 goto done; 24007 } else { 24008 /* 24009 * Indicate to PM the device is busy. This is required 24010 * to avoid a race - i.e. the ioctl is issuing a 24011 * command and the pm framework brings down the device 24012 * to low power mode (possible power cut-off on some 24013 * platforms). 24014 */ 24015 mutex_exit(&un->un_pm_mutex); 24016 if (sd_pm_entry(un) != DDI_SUCCESS) { 24017 rval = EAGAIN; 24018 goto done; 24019 } 24020 } 24021 } 24022 24023 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 24024 24025 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page, 24026 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag); 24027 if (rval != 0) 24028 goto done2; 24029 24030 /* 24031 * For the current temperature verify that the parameter length is 0x02 24032 * and the parameter code is 0x00 24033 */ 24034 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 24035 (temperature_page[5] == 0x00)) { 24036 if (temperature_page[9] == 0xFF) { 24037 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24038 } else { 24039 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 24040 } 24041 } 24042 24043 /* 24044 * For the reference temperature verify that the parameter 24045 * length is 0x02 and the parameter code is 0x01 24046 */ 24047 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 24048 (temperature_page[11] == 0x01)) { 24049 if (temperature_page[15] == 0xFF) { 24050 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24051 } else { 24052 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 24053 } 24054 } 24055 24056 /* Do the copyout regardless of the temperature commands status. */ 24057 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 24058 flag) != 0) { 24059 rval = EFAULT; 24060 goto done1; 24061 } 24062 24063 done2: 24064 if (rval != 0) { 24065 if (rval == EIO) 24066 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24067 else 24068 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24069 } 24070 done1: 24071 if (path_flag == SD_PATH_DIRECT) { 24072 sd_pm_exit(un); 24073 } 24074 24075 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 24076 done: 24077 sd_ssc_fini(ssc); 24078 if (dktemp != NULL) { 24079 kmem_free(dktemp, sizeof (struct dk_temperature)); 24080 } 24081 24082 return (rval); 24083 } 24084 24085 24086 /* 24087 * Function: sd_log_page_supported 24088 * 24089 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 24090 * supported log pages. 24091 * 24092 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 24093 * structure for this target. 24094 * log_page - 24095 * 24096 * Return Code: -1 - on error (log sense is optional and may not be supported). 24097 * 0 - log page not found. 24098 * 1 - log page found. 24099 */ 24100 24101 static int 24102 sd_log_page_supported(sd_ssc_t *ssc, int log_page) 24103 { 24104 uchar_t *log_page_data; 24105 int i; 24106 int match = 0; 24107 int log_size; 24108 int status = 0; 24109 struct sd_lun *un; 24110 24111 ASSERT(ssc != NULL); 24112 un = ssc->ssc_un; 24113 ASSERT(un != NULL); 24114 24115 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 24116 24117 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0, 24118 SD_PATH_DIRECT); 24119 24120 if (status != 0) { 24121 if (status == EIO) { 24122 /* 24123 * Some disks do not support log sense, we 24124 * should ignore this kind of error(sense key is 24125 * 0x5 - illegal request). 24126 */ 24127 uint8_t *sensep; 24128 int senlen; 24129 24130 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 24131 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 24132 ssc->ssc_uscsi_cmd->uscsi_rqresid); 24133 24134 if (senlen > 0 && 24135 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 24136 sd_ssc_assessment(ssc, 24137 SD_FMT_IGNORE_COMPROMISE); 24138 } else { 24139 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24140 } 24141 } else { 24142 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24143 } 24144 24145 SD_ERROR(SD_LOG_COMMON, un, 24146 "sd_log_page_supported: failed log page retrieval\n"); 24147 kmem_free(log_page_data, 0xFF); 24148 return (-1); 24149 } 24150 24151 log_size = log_page_data[3]; 24152 24153 /* 24154 * The list of supported log pages start from the fourth byte. Check 24155 * until we run out of log pages or a match is found. 24156 */ 24157 for (i = 4; (i < (log_size + 4)) && !match; i++) { 24158 if (log_page_data[i] == log_page) { 24159 match++; 24160 } 24161 } 24162 kmem_free(log_page_data, 0xFF); 24163 return (match); 24164 } 24165 24166 24167 /* 24168 * Function: sd_mhdioc_failfast 24169 * 24170 * Description: This routine is the driver entry point for handling ioctl 24171 * requests to enable/disable the multihost failfast option. 24172 * (MHIOCENFAILFAST) 24173 * 24174 * Arguments: dev - the device number 24175 * arg - user specified probing interval. 24176 * flag - this argument is a pass through to ddi_copyxxx() 24177 * directly from the mode argument of ioctl(). 24178 * 24179 * Return Code: 0 24180 * EFAULT 24181 * ENXIO 24182 */ 24183 24184 static int 24185 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 24186 { 24187 struct sd_lun *un = NULL; 24188 int mh_time; 24189 int rval = 0; 24190 24191 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24192 return (ENXIO); 24193 } 24194 24195 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 24196 return (EFAULT); 24197 24198 if (mh_time) { 24199 mutex_enter(SD_MUTEX(un)); 24200 un->un_resvd_status |= SD_FAILFAST; 24201 mutex_exit(SD_MUTEX(un)); 24202 /* 24203 * If mh_time is INT_MAX, then this ioctl is being used for 24204 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 24205 */ 24206 if (mh_time != INT_MAX) { 24207 rval = sd_check_mhd(dev, mh_time); 24208 } 24209 } else { 24210 (void) sd_check_mhd(dev, 0); 24211 mutex_enter(SD_MUTEX(un)); 24212 un->un_resvd_status &= ~SD_FAILFAST; 24213 mutex_exit(SD_MUTEX(un)); 24214 } 24215 return (rval); 24216 } 24217 24218 24219 /* 24220 * Function: sd_mhdioc_takeown 24221 * 24222 * Description: This routine is the driver entry point for handling ioctl 24223 * requests to forcefully acquire exclusive access rights to the 24224 * multihost disk (MHIOCTKOWN). 24225 * 24226 * Arguments: dev - the device number 24227 * arg - user provided structure specifying the delay 24228 * parameters in milliseconds 24229 * flag - this argument is a pass through to ddi_copyxxx() 24230 * directly from the mode argument of ioctl(). 24231 * 24232 * Return Code: 0 24233 * EFAULT 24234 * ENXIO 24235 */ 24236 24237 static int 24238 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 24239 { 24240 struct sd_lun *un = NULL; 24241 struct mhioctkown *tkown = NULL; 24242 int rval = 0; 24243 24244 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24245 return (ENXIO); 24246 } 24247 24248 if (arg != NULL) { 24249 tkown = (struct mhioctkown *) 24250 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 24251 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 24252 if (rval != 0) { 24253 rval = EFAULT; 24254 goto error; 24255 } 24256 } 24257 24258 rval = sd_take_ownership(dev, tkown); 24259 mutex_enter(SD_MUTEX(un)); 24260 if (rval == 0) { 24261 un->un_resvd_status |= SD_RESERVE; 24262 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 24263 sd_reinstate_resv_delay = 24264 tkown->reinstate_resv_delay * 1000; 24265 } else { 24266 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 24267 } 24268 /* 24269 * Give the scsi_watch routine interval set by 24270 * the MHIOCENFAILFAST ioctl precedence here. 24271 */ 24272 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 24273 mutex_exit(SD_MUTEX(un)); 24274 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 24275 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24276 "sd_mhdioc_takeown : %d\n", 24277 sd_reinstate_resv_delay); 24278 } else { 24279 mutex_exit(SD_MUTEX(un)); 24280 } 24281 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 24282 sd_mhd_reset_notify_cb, (caddr_t)un); 24283 } else { 24284 un->un_resvd_status &= ~SD_RESERVE; 24285 mutex_exit(SD_MUTEX(un)); 24286 } 24287 24288 error: 24289 if (tkown != NULL) { 24290 kmem_free(tkown, sizeof (struct mhioctkown)); 24291 } 24292 return (rval); 24293 } 24294 24295 24296 /* 24297 * Function: sd_mhdioc_release 24298 * 24299 * Description: This routine is the driver entry point for handling ioctl 24300 * requests to release exclusive access rights to the multihost 24301 * disk (MHIOCRELEASE). 24302 * 24303 * Arguments: dev - the device number 24304 * 24305 * Return Code: 0 24306 * ENXIO 24307 */ 24308 24309 static int 24310 sd_mhdioc_release(dev_t dev) 24311 { 24312 struct sd_lun *un = NULL; 24313 timeout_id_t resvd_timeid_save; 24314 int resvd_status_save; 24315 int rval = 0; 24316 24317 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24318 return (ENXIO); 24319 } 24320 24321 mutex_enter(SD_MUTEX(un)); 24322 resvd_status_save = un->un_resvd_status; 24323 un->un_resvd_status &= 24324 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 24325 if (un->un_resvd_timeid) { 24326 resvd_timeid_save = un->un_resvd_timeid; 24327 un->un_resvd_timeid = NULL; 24328 mutex_exit(SD_MUTEX(un)); 24329 (void) untimeout(resvd_timeid_save); 24330 } else { 24331 mutex_exit(SD_MUTEX(un)); 24332 } 24333 24334 /* 24335 * destroy any pending timeout thread that may be attempting to 24336 * reinstate reservation on this device. 24337 */ 24338 sd_rmv_resv_reclaim_req(dev); 24339 24340 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 24341 mutex_enter(SD_MUTEX(un)); 24342 if ((un->un_mhd_token) && 24343 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 24344 mutex_exit(SD_MUTEX(un)); 24345 (void) sd_check_mhd(dev, 0); 24346 } else { 24347 mutex_exit(SD_MUTEX(un)); 24348 } 24349 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 24350 sd_mhd_reset_notify_cb, (caddr_t)un); 24351 } else { 24352 /* 24353 * sd_mhd_watch_cb will restart the resvd recover timeout thread 24354 */ 24355 mutex_enter(SD_MUTEX(un)); 24356 un->un_resvd_status = resvd_status_save; 24357 mutex_exit(SD_MUTEX(un)); 24358 } 24359 return (rval); 24360 } 24361 24362 24363 /* 24364 * Function: sd_mhdioc_register_devid 24365 * 24366 * Description: This routine is the driver entry point for handling ioctl 24367 * requests to register the device id (MHIOCREREGISTERDEVID). 24368 * 24369 * Note: The implementation for this ioctl has been updated to 24370 * be consistent with the original PSARC case (1999/357) 24371 * (4375899, 4241671, 4220005) 24372 * 24373 * Arguments: dev - the device number 24374 * 24375 * Return Code: 0 24376 * ENXIO 24377 */ 24378 24379 static int 24380 sd_mhdioc_register_devid(dev_t dev) 24381 { 24382 struct sd_lun *un = NULL; 24383 int rval = 0; 24384 sd_ssc_t *ssc; 24385 24386 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24387 return (ENXIO); 24388 } 24389 24390 ASSERT(!mutex_owned(SD_MUTEX(un))); 24391 24392 mutex_enter(SD_MUTEX(un)); 24393 24394 /* If a devid already exists, de-register it */ 24395 if (un->un_devid != NULL) { 24396 ddi_devid_unregister(SD_DEVINFO(un)); 24397 /* 24398 * After unregister devid, needs to free devid memory 24399 */ 24400 ddi_devid_free(un->un_devid); 24401 un->un_devid = NULL; 24402 } 24403 24404 /* Check for reservation conflict */ 24405 mutex_exit(SD_MUTEX(un)); 24406 ssc = sd_ssc_init(un); 24407 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 24408 mutex_enter(SD_MUTEX(un)); 24409 24410 switch (rval) { 24411 case 0: 24412 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 24413 break; 24414 case EACCES: 24415 break; 24416 default: 24417 rval = EIO; 24418 } 24419 24420 mutex_exit(SD_MUTEX(un)); 24421 if (rval != 0) { 24422 if (rval == EIO) 24423 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 24424 else 24425 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 24426 } 24427 sd_ssc_fini(ssc); 24428 return (rval); 24429 } 24430 24431 24432 /* 24433 * Function: sd_mhdioc_inkeys 24434 * 24435 * Description: This routine is the driver entry point for handling ioctl 24436 * requests to issue the SCSI-3 Persistent In Read Keys command 24437 * to the device (MHIOCGRP_INKEYS). 24438 * 24439 * Arguments: dev - the device number 24440 * arg - user provided in_keys structure 24441 * flag - this argument is a pass through to ddi_copyxxx() 24442 * directly from the mode argument of ioctl(). 24443 * 24444 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 24445 * ENXIO 24446 * EFAULT 24447 */ 24448 24449 static int 24450 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 24451 { 24452 struct sd_lun *un; 24453 mhioc_inkeys_t inkeys; 24454 int rval = 0; 24455 24456 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24457 return (ENXIO); 24458 } 24459 24460 #ifdef _MULTI_DATAMODEL 24461 switch (ddi_model_convert_from(flag & FMODELS)) { 24462 case DDI_MODEL_ILP32: { 24463 struct mhioc_inkeys32 inkeys32; 24464 24465 if (ddi_copyin(arg, &inkeys32, 24466 sizeof (struct mhioc_inkeys32), flag) != 0) { 24467 return (EFAULT); 24468 } 24469 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 24470 if ((rval = sd_persistent_reservation_in_read_keys(un, 24471 &inkeys, flag)) != 0) { 24472 return (rval); 24473 } 24474 inkeys32.generation = inkeys.generation; 24475 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 24476 flag) != 0) { 24477 return (EFAULT); 24478 } 24479 break; 24480 } 24481 case DDI_MODEL_NONE: 24482 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 24483 flag) != 0) { 24484 return (EFAULT); 24485 } 24486 if ((rval = sd_persistent_reservation_in_read_keys(un, 24487 &inkeys, flag)) != 0) { 24488 return (rval); 24489 } 24490 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 24491 flag) != 0) { 24492 return (EFAULT); 24493 } 24494 break; 24495 } 24496 24497 #else /* ! _MULTI_DATAMODEL */ 24498 24499 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 24500 return (EFAULT); 24501 } 24502 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 24503 if (rval != 0) { 24504 return (rval); 24505 } 24506 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 24507 return (EFAULT); 24508 } 24509 24510 #endif /* _MULTI_DATAMODEL */ 24511 24512 return (rval); 24513 } 24514 24515 24516 /* 24517 * Function: sd_mhdioc_inresv 24518 * 24519 * Description: This routine is the driver entry point for handling ioctl 24520 * requests to issue the SCSI-3 Persistent In Read Reservations 24521 * command to the device (MHIOCGRP_INKEYS). 24522 * 24523 * Arguments: dev - the device number 24524 * arg - user provided in_resv structure 24525 * flag - this argument is a pass through to ddi_copyxxx() 24526 * directly from the mode argument of ioctl(). 24527 * 24528 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 24529 * ENXIO 24530 * EFAULT 24531 */ 24532 24533 static int 24534 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 24535 { 24536 struct sd_lun *un; 24537 mhioc_inresvs_t inresvs; 24538 int rval = 0; 24539 24540 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24541 return (ENXIO); 24542 } 24543 24544 #ifdef _MULTI_DATAMODEL 24545 24546 switch (ddi_model_convert_from(flag & FMODELS)) { 24547 case DDI_MODEL_ILP32: { 24548 struct mhioc_inresvs32 inresvs32; 24549 24550 if (ddi_copyin(arg, &inresvs32, 24551 sizeof (struct mhioc_inresvs32), flag) != 0) { 24552 return (EFAULT); 24553 } 24554 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 24555 if ((rval = sd_persistent_reservation_in_read_resv(un, 24556 &inresvs, flag)) != 0) { 24557 return (rval); 24558 } 24559 inresvs32.generation = inresvs.generation; 24560 if (ddi_copyout(&inresvs32, arg, 24561 sizeof (struct mhioc_inresvs32), flag) != 0) { 24562 return (EFAULT); 24563 } 24564 break; 24565 } 24566 case DDI_MODEL_NONE: 24567 if (ddi_copyin(arg, &inresvs, 24568 sizeof (mhioc_inresvs_t), flag) != 0) { 24569 return (EFAULT); 24570 } 24571 if ((rval = sd_persistent_reservation_in_read_resv(un, 24572 &inresvs, flag)) != 0) { 24573 return (rval); 24574 } 24575 if (ddi_copyout(&inresvs, arg, 24576 sizeof (mhioc_inresvs_t), flag) != 0) { 24577 return (EFAULT); 24578 } 24579 break; 24580 } 24581 24582 #else /* ! _MULTI_DATAMODEL */ 24583 24584 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 24585 return (EFAULT); 24586 } 24587 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 24588 if (rval != 0) { 24589 return (rval); 24590 } 24591 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 24592 return (EFAULT); 24593 } 24594 24595 #endif /* ! _MULTI_DATAMODEL */ 24596 24597 return (rval); 24598 } 24599 24600 24601 /* 24602 * The following routines support the clustering functionality described below 24603 * and implement lost reservation reclaim functionality. 24604 * 24605 * Clustering 24606 * ---------- 24607 * The clustering code uses two different, independent forms of SCSI 24608 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 24609 * Persistent Group Reservations. For any particular disk, it will use either 24610 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 24611 * 24612 * SCSI-2 24613 * The cluster software takes ownership of a multi-hosted disk by issuing the 24614 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 24615 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 24616 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 24617 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 24618 * driver. The meaning of failfast is that if the driver (on this host) ever 24619 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 24620 * it should immediately panic the host. The motivation for this ioctl is that 24621 * if this host does encounter reservation conflict, the underlying cause is 24622 * that some other host of the cluster has decided that this host is no longer 24623 * in the cluster and has seized control of the disks for itself. Since this 24624 * host is no longer in the cluster, it ought to panic itself. The 24625 * MHIOCENFAILFAST ioctl does two things: 24626 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 24627 * error to panic the host 24628 * (b) it sets up a periodic timer to test whether this host still has 24629 * "access" (in that no other host has reserved the device): if the 24630 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 24631 * purpose of that periodic timer is to handle scenarios where the host is 24632 * otherwise temporarily quiescent, temporarily doing no real i/o. 24633 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 24634 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 24635 * the device itself. 24636 * 24637 * SCSI-3 PGR 24638 * A direct semantic implementation of the SCSI-3 Persistent Reservation 24639 * facility is supported through the shared multihost disk ioctls 24640 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 24641 * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR) 24642 * 24643 * Reservation Reclaim: 24644 * -------------------- 24645 * To support the lost reservation reclaim operations this driver creates a 24646 * single thread to handle reinstating reservations on all devices that have 24647 * lost reservations sd_resv_reclaim_requests are logged for all devices that 24648 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 24649 * and the reservation reclaim thread loops through the requests to regain the 24650 * lost reservations. 24651 */ 24652 24653 /* 24654 * Function: sd_check_mhd() 24655 * 24656 * Description: This function sets up and submits a scsi watch request or 24657 * terminates an existing watch request. This routine is used in 24658 * support of reservation reclaim. 24659 * 24660 * Arguments: dev - the device 'dev_t' is used for context to discriminate 24661 * among multiple watches that share the callback function 24662 * interval - the number of microseconds specifying the watch 24663 * interval for issuing TEST UNIT READY commands. If 24664 * set to 0 the watch should be terminated. If the 24665 * interval is set to 0 and if the device is required 24666 * to hold reservation while disabling failfast, the 24667 * watch is restarted with an interval of 24668 * reinstate_resv_delay. 24669 * 24670 * Return Code: 0 - Successful submit/terminate of scsi watch request 24671 * ENXIO - Indicates an invalid device was specified 24672 * EAGAIN - Unable to submit the scsi watch request 24673 */ 24674 24675 static int 24676 sd_check_mhd(dev_t dev, int interval) 24677 { 24678 struct sd_lun *un; 24679 opaque_t token; 24680 24681 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24682 return (ENXIO); 24683 } 24684 24685 /* is this a watch termination request? */ 24686 if (interval == 0) { 24687 mutex_enter(SD_MUTEX(un)); 24688 /* if there is an existing watch task then terminate it */ 24689 if (un->un_mhd_token) { 24690 token = un->un_mhd_token; 24691 un->un_mhd_token = NULL; 24692 mutex_exit(SD_MUTEX(un)); 24693 (void) scsi_watch_request_terminate(token, 24694 SCSI_WATCH_TERMINATE_ALL_WAIT); 24695 mutex_enter(SD_MUTEX(un)); 24696 } else { 24697 mutex_exit(SD_MUTEX(un)); 24698 /* 24699 * Note: If we return here we don't check for the 24700 * failfast case. This is the original legacy 24701 * implementation but perhaps we should be checking 24702 * the failfast case. 24703 */ 24704 return (0); 24705 } 24706 /* 24707 * If the device is required to hold reservation while 24708 * disabling failfast, we need to restart the scsi_watch 24709 * routine with an interval of reinstate_resv_delay. 24710 */ 24711 if (un->un_resvd_status & SD_RESERVE) { 24712 interval = sd_reinstate_resv_delay/1000; 24713 } else { 24714 /* no failfast so bail */ 24715 mutex_exit(SD_MUTEX(un)); 24716 return (0); 24717 } 24718 mutex_exit(SD_MUTEX(un)); 24719 } 24720 24721 /* 24722 * adjust minimum time interval to 1 second, 24723 * and convert from msecs to usecs 24724 */ 24725 if (interval > 0 && interval < 1000) { 24726 interval = 1000; 24727 } 24728 interval *= 1000; 24729 24730 /* 24731 * submit the request to the scsi_watch service 24732 */ 24733 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 24734 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 24735 if (token == NULL) { 24736 return (EAGAIN); 24737 } 24738 24739 /* 24740 * save token for termination later on 24741 */ 24742 mutex_enter(SD_MUTEX(un)); 24743 un->un_mhd_token = token; 24744 mutex_exit(SD_MUTEX(un)); 24745 return (0); 24746 } 24747 24748 24749 /* 24750 * Function: sd_mhd_watch_cb() 24751 * 24752 * Description: This function is the call back function used by the scsi watch 24753 * facility. The scsi watch facility sends the "Test Unit Ready" 24754 * and processes the status. If applicable (i.e. a "Unit Attention" 24755 * status and automatic "Request Sense" not used) the scsi watch 24756 * facility will send a "Request Sense" and retrieve the sense data 24757 * to be passed to this callback function. In either case the 24758 * automatic "Request Sense" or the facility submitting one, this 24759 * callback is passed the status and sense data. 24760 * 24761 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24762 * among multiple watches that share this callback function 24763 * resultp - scsi watch facility result packet containing scsi 24764 * packet, status byte and sense data 24765 * 24766 * Return Code: 0 - continue the watch task 24767 * non-zero - terminate the watch task 24768 */ 24769 24770 static int 24771 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24772 { 24773 struct sd_lun *un; 24774 struct scsi_status *statusp; 24775 uint8_t *sensep; 24776 struct scsi_pkt *pkt; 24777 uchar_t actual_sense_length; 24778 dev_t dev = (dev_t)arg; 24779 24780 ASSERT(resultp != NULL); 24781 statusp = resultp->statusp; 24782 sensep = (uint8_t *)resultp->sensep; 24783 pkt = resultp->pkt; 24784 actual_sense_length = resultp->actual_sense_length; 24785 24786 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24787 return (ENXIO); 24788 } 24789 24790 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24791 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 24792 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 24793 24794 /* Begin processing of the status and/or sense data */ 24795 if (pkt->pkt_reason != CMD_CMPLT) { 24796 /* Handle the incomplete packet */ 24797 sd_mhd_watch_incomplete(un, pkt); 24798 return (0); 24799 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 24800 if (*((unsigned char *)statusp) 24801 == STATUS_RESERVATION_CONFLICT) { 24802 /* 24803 * Handle a reservation conflict by panicking if 24804 * configured for failfast or by logging the conflict 24805 * and updating the reservation status 24806 */ 24807 mutex_enter(SD_MUTEX(un)); 24808 if ((un->un_resvd_status & SD_FAILFAST) && 24809 (sd_failfast_enable)) { 24810 sd_panic_for_res_conflict(un); 24811 /*NOTREACHED*/ 24812 } 24813 SD_INFO(SD_LOG_IOCTL_MHD, un, 24814 "sd_mhd_watch_cb: Reservation Conflict\n"); 24815 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 24816 mutex_exit(SD_MUTEX(un)); 24817 } 24818 } 24819 24820 if (sensep != NULL) { 24821 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 24822 mutex_enter(SD_MUTEX(un)); 24823 if ((scsi_sense_asc(sensep) == 24824 SD_SCSI_RESET_SENSE_CODE) && 24825 (un->un_resvd_status & SD_RESERVE)) { 24826 /* 24827 * The additional sense code indicates a power 24828 * on or bus device reset has occurred; update 24829 * the reservation status. 24830 */ 24831 un->un_resvd_status |= 24832 (SD_LOST_RESERVE | SD_WANT_RESERVE); 24833 SD_INFO(SD_LOG_IOCTL_MHD, un, 24834 "sd_mhd_watch_cb: Lost Reservation\n"); 24835 } 24836 } else { 24837 return (0); 24838 } 24839 } else { 24840 mutex_enter(SD_MUTEX(un)); 24841 } 24842 24843 if ((un->un_resvd_status & SD_RESERVE) && 24844 (un->un_resvd_status & SD_LOST_RESERVE)) { 24845 if (un->un_resvd_status & SD_WANT_RESERVE) { 24846 /* 24847 * A reset occurred in between the last probe and this 24848 * one so if a timeout is pending cancel it. 24849 */ 24850 if (un->un_resvd_timeid) { 24851 timeout_id_t temp_id = un->un_resvd_timeid; 24852 un->un_resvd_timeid = NULL; 24853 mutex_exit(SD_MUTEX(un)); 24854 (void) untimeout(temp_id); 24855 mutex_enter(SD_MUTEX(un)); 24856 } 24857 un->un_resvd_status &= ~SD_WANT_RESERVE; 24858 } 24859 if (un->un_resvd_timeid == 0) { 24860 /* Schedule a timeout to handle the lost reservation */ 24861 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 24862 (void *)dev, 24863 drv_usectohz(sd_reinstate_resv_delay)); 24864 } 24865 } 24866 mutex_exit(SD_MUTEX(un)); 24867 return (0); 24868 } 24869 24870 24871 /* 24872 * Function: sd_mhd_watch_incomplete() 24873 * 24874 * Description: This function is used to find out why a scsi pkt sent by the 24875 * scsi watch facility was not completed. Under some scenarios this 24876 * routine will return. Otherwise it will send a bus reset to see 24877 * if the drive is still online. 24878 * 24879 * Arguments: un - driver soft state (unit) structure 24880 * pkt - incomplete scsi pkt 24881 */ 24882 24883 static void 24884 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 24885 { 24886 int be_chatty; 24887 int perr; 24888 24889 ASSERT(pkt != NULL); 24890 ASSERT(un != NULL); 24891 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 24892 perr = (pkt->pkt_statistics & STAT_PERR); 24893 24894 mutex_enter(SD_MUTEX(un)); 24895 if (un->un_state == SD_STATE_DUMPING) { 24896 mutex_exit(SD_MUTEX(un)); 24897 return; 24898 } 24899 24900 switch (pkt->pkt_reason) { 24901 case CMD_UNX_BUS_FREE: 24902 /* 24903 * If we had a parity error that caused the target to drop BSY*, 24904 * don't be chatty about it. 24905 */ 24906 if (perr && be_chatty) { 24907 be_chatty = 0; 24908 } 24909 break; 24910 case CMD_TAG_REJECT: 24911 /* 24912 * The SCSI-2 spec states that a tag reject will be sent by the 24913 * target if tagged queuing is not supported. A tag reject may 24914 * also be sent during certain initialization periods or to 24915 * control internal resources. For the latter case the target 24916 * may also return Queue Full. 24917 * 24918 * If this driver receives a tag reject from a target that is 24919 * going through an init period or controlling internal 24920 * resources tagged queuing will be disabled. This is a less 24921 * than optimal behavior but the driver is unable to determine 24922 * the target state and assumes tagged queueing is not supported 24923 */ 24924 pkt->pkt_flags = 0; 24925 un->un_tagflags = 0; 24926 24927 if (un->un_f_opt_queueing == TRUE) { 24928 un->un_throttle = min(un->un_throttle, 3); 24929 } else { 24930 un->un_throttle = 1; 24931 } 24932 mutex_exit(SD_MUTEX(un)); 24933 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 24934 mutex_enter(SD_MUTEX(un)); 24935 break; 24936 case CMD_INCOMPLETE: 24937 /* 24938 * The transport stopped with an abnormal state, fallthrough and 24939 * reset the target and/or bus unless selection did not complete 24940 * (indicated by STATE_GOT_BUS) in which case we don't want to 24941 * go through a target/bus reset 24942 */ 24943 if (pkt->pkt_state == STATE_GOT_BUS) { 24944 break; 24945 } 24946 /*FALLTHROUGH*/ 24947 24948 case CMD_TIMEOUT: 24949 default: 24950 /* 24951 * The lun may still be running the command, so a lun reset 24952 * should be attempted. If the lun reset fails or cannot be 24953 * issued, than try a target reset. Lastly try a bus reset. 24954 */ 24955 if ((pkt->pkt_statistics & 24956 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 24957 int reset_retval = 0; 24958 mutex_exit(SD_MUTEX(un)); 24959 if (un->un_f_allow_bus_device_reset == TRUE) { 24960 if (un->un_f_lun_reset_enabled == TRUE) { 24961 reset_retval = 24962 scsi_reset(SD_ADDRESS(un), 24963 RESET_LUN); 24964 } 24965 if (reset_retval == 0) { 24966 reset_retval = 24967 scsi_reset(SD_ADDRESS(un), 24968 RESET_TARGET); 24969 } 24970 } 24971 if (reset_retval == 0) { 24972 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 24973 } 24974 mutex_enter(SD_MUTEX(un)); 24975 } 24976 break; 24977 } 24978 24979 /* A device/bus reset has occurred; update the reservation status. */ 24980 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 24981 (STAT_BUS_RESET | STAT_DEV_RESET))) { 24982 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 24983 un->un_resvd_status |= 24984 (SD_LOST_RESERVE | SD_WANT_RESERVE); 24985 SD_INFO(SD_LOG_IOCTL_MHD, un, 24986 "sd_mhd_watch_incomplete: Lost Reservation\n"); 24987 } 24988 } 24989 24990 /* 24991 * The disk has been turned off; Update the device state. 24992 * 24993 * Note: Should we be offlining the disk here? 24994 */ 24995 if (pkt->pkt_state == STATE_GOT_BUS) { 24996 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 24997 "Disk not responding to selection\n"); 24998 if (un->un_state != SD_STATE_OFFLINE) { 24999 New_state(un, SD_STATE_OFFLINE); 25000 } 25001 } else if (be_chatty) { 25002 /* 25003 * suppress messages if they are all the same pkt reason; 25004 * with TQ, many (up to 256) are returned with the same 25005 * pkt_reason 25006 */ 25007 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25008 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25009 "sd_mhd_watch_incomplete: " 25010 "SCSI transport failed: reason '%s'\n", 25011 scsi_rname(pkt->pkt_reason)); 25012 } 25013 } 25014 un->un_last_pkt_reason = pkt->pkt_reason; 25015 mutex_exit(SD_MUTEX(un)); 25016 } 25017 25018 25019 /* 25020 * Function: sd_sname() 25021 * 25022 * Description: This is a simple little routine to return a string containing 25023 * a printable description of command status byte for use in 25024 * logging. 25025 * 25026 * Arguments: status - pointer to a status byte 25027 * 25028 * Return Code: char * - string containing status description. 25029 */ 25030 25031 static char * 25032 sd_sname(uchar_t status) 25033 { 25034 switch (status & STATUS_MASK) { 25035 case STATUS_GOOD: 25036 return ("good status"); 25037 case STATUS_CHECK: 25038 return ("check condition"); 25039 case STATUS_MET: 25040 return ("condition met"); 25041 case STATUS_BUSY: 25042 return ("busy"); 25043 case STATUS_INTERMEDIATE: 25044 return ("intermediate"); 25045 case STATUS_INTERMEDIATE_MET: 25046 return ("intermediate - condition met"); 25047 case STATUS_RESERVATION_CONFLICT: 25048 return ("reservation_conflict"); 25049 case STATUS_TERMINATED: 25050 return ("command terminated"); 25051 case STATUS_QFULL: 25052 return ("queue full"); 25053 default: 25054 return ("<unknown status>"); 25055 } 25056 } 25057 25058 25059 /* 25060 * Function: sd_mhd_resvd_recover() 25061 * 25062 * Description: This function adds a reservation entry to the 25063 * sd_resv_reclaim_request list and signals the reservation 25064 * reclaim thread that there is work pending. If the reservation 25065 * reclaim thread has not been previously created this function 25066 * will kick it off. 25067 * 25068 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25069 * among multiple watches that share this callback function 25070 * 25071 * Context: This routine is called by timeout() and is run in interrupt 25072 * context. It must not sleep or call other functions which may 25073 * sleep. 25074 */ 25075 25076 static void 25077 sd_mhd_resvd_recover(void *arg) 25078 { 25079 dev_t dev = (dev_t)arg; 25080 struct sd_lun *un; 25081 struct sd_thr_request *sd_treq = NULL; 25082 struct sd_thr_request *sd_cur = NULL; 25083 struct sd_thr_request *sd_prev = NULL; 25084 int already_there = 0; 25085 25086 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25087 return; 25088 } 25089 25090 mutex_enter(SD_MUTEX(un)); 25091 un->un_resvd_timeid = NULL; 25092 if (un->un_resvd_status & SD_WANT_RESERVE) { 25093 /* 25094 * There was a reset so don't issue the reserve, allow the 25095 * sd_mhd_watch_cb callback function to notice this and 25096 * reschedule the timeout for reservation. 25097 */ 25098 mutex_exit(SD_MUTEX(un)); 25099 return; 25100 } 25101 mutex_exit(SD_MUTEX(un)); 25102 25103 /* 25104 * Add this device to the sd_resv_reclaim_request list and the 25105 * sd_resv_reclaim_thread should take care of the rest. 25106 * 25107 * Note: We can't sleep in this context so if the memory allocation 25108 * fails allow the sd_mhd_watch_cb callback function to notice this and 25109 * reschedule the timeout for reservation. (4378460) 25110 */ 25111 sd_treq = (struct sd_thr_request *) 25112 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 25113 if (sd_treq == NULL) { 25114 return; 25115 } 25116 25117 sd_treq->sd_thr_req_next = NULL; 25118 sd_treq->dev = dev; 25119 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25120 if (sd_tr.srq_thr_req_head == NULL) { 25121 sd_tr.srq_thr_req_head = sd_treq; 25122 } else { 25123 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 25124 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 25125 if (sd_cur->dev == dev) { 25126 /* 25127 * already in Queue so don't log 25128 * another request for the device 25129 */ 25130 already_there = 1; 25131 break; 25132 } 25133 sd_prev = sd_cur; 25134 } 25135 if (!already_there) { 25136 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 25137 "logging request for %lx\n", dev); 25138 sd_prev->sd_thr_req_next = sd_treq; 25139 } else { 25140 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 25141 } 25142 } 25143 25144 /* 25145 * Create a kernel thread to do the reservation reclaim and free up this 25146 * thread. We cannot block this thread while we go away to do the 25147 * reservation reclaim 25148 */ 25149 if (sd_tr.srq_resv_reclaim_thread == NULL) 25150 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 25151 sd_resv_reclaim_thread, NULL, 25152 0, &p0, TS_RUN, v.v_maxsyspri - 2); 25153 25154 /* Tell the reservation reclaim thread that it has work to do */ 25155 cv_signal(&sd_tr.srq_resv_reclaim_cv); 25156 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25157 } 25158 25159 /* 25160 * Function: sd_resv_reclaim_thread() 25161 * 25162 * Description: This function implements the reservation reclaim operations 25163 * 25164 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25165 * among multiple watches that share this callback function 25166 */ 25167 25168 static void 25169 sd_resv_reclaim_thread() 25170 { 25171 struct sd_lun *un; 25172 struct sd_thr_request *sd_mhreq; 25173 25174 /* Wait for work */ 25175 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25176 if (sd_tr.srq_thr_req_head == NULL) { 25177 cv_wait(&sd_tr.srq_resv_reclaim_cv, 25178 &sd_tr.srq_resv_reclaim_mutex); 25179 } 25180 25181 /* Loop while we have work */ 25182 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 25183 un = ddi_get_soft_state(sd_state, 25184 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 25185 if (un == NULL) { 25186 /* 25187 * softstate structure is NULL so just 25188 * dequeue the request and continue 25189 */ 25190 sd_tr.srq_thr_req_head = 25191 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25192 kmem_free(sd_tr.srq_thr_cur_req, 25193 sizeof (struct sd_thr_request)); 25194 continue; 25195 } 25196 25197 /* dequeue the request */ 25198 sd_mhreq = sd_tr.srq_thr_cur_req; 25199 sd_tr.srq_thr_req_head = 25200 sd_tr.srq_thr_cur_req->sd_thr_req_next; 25201 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25202 25203 /* 25204 * Reclaim reservation only if SD_RESERVE is still set. There 25205 * may have been a call to MHIOCRELEASE before we got here. 25206 */ 25207 mutex_enter(SD_MUTEX(un)); 25208 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25209 /* 25210 * Note: The SD_LOST_RESERVE flag is cleared before 25211 * reclaiming the reservation. If this is done after the 25212 * call to sd_reserve_release a reservation loss in the 25213 * window between pkt completion of reserve cmd and 25214 * mutex_enter below may not be recognized 25215 */ 25216 un->un_resvd_status &= ~SD_LOST_RESERVE; 25217 mutex_exit(SD_MUTEX(un)); 25218 25219 if (sd_reserve_release(sd_mhreq->dev, 25220 SD_RESERVE) == 0) { 25221 mutex_enter(SD_MUTEX(un)); 25222 un->un_resvd_status |= SD_RESERVE; 25223 mutex_exit(SD_MUTEX(un)); 25224 SD_INFO(SD_LOG_IOCTL_MHD, un, 25225 "sd_resv_reclaim_thread: " 25226 "Reservation Recovered\n"); 25227 } else { 25228 mutex_enter(SD_MUTEX(un)); 25229 un->un_resvd_status |= SD_LOST_RESERVE; 25230 mutex_exit(SD_MUTEX(un)); 25231 SD_INFO(SD_LOG_IOCTL_MHD, un, 25232 "sd_resv_reclaim_thread: Failed " 25233 "Reservation Recovery\n"); 25234 } 25235 } else { 25236 mutex_exit(SD_MUTEX(un)); 25237 } 25238 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25239 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 25240 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25241 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 25242 /* 25243 * wakeup the destroy thread if anyone is waiting on 25244 * us to complete. 25245 */ 25246 cv_signal(&sd_tr.srq_inprocess_cv); 25247 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25248 "sd_resv_reclaim_thread: cv_signalling current request \n"); 25249 } 25250 25251 /* 25252 * cleanup the sd_tr structure now that this thread will not exist 25253 */ 25254 ASSERT(sd_tr.srq_thr_req_head == NULL); 25255 ASSERT(sd_tr.srq_thr_cur_req == NULL); 25256 sd_tr.srq_resv_reclaim_thread = NULL; 25257 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25258 thread_exit(); 25259 } 25260 25261 25262 /* 25263 * Function: sd_rmv_resv_reclaim_req() 25264 * 25265 * Description: This function removes any pending reservation reclaim requests 25266 * for the specified device. 25267 * 25268 * Arguments: dev - the device 'dev_t' 25269 */ 25270 25271 static void 25272 sd_rmv_resv_reclaim_req(dev_t dev) 25273 { 25274 struct sd_thr_request *sd_mhreq; 25275 struct sd_thr_request *sd_prev; 25276 25277 /* Remove a reservation reclaim request from the list */ 25278 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 25279 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 25280 /* 25281 * We are attempting to reinstate reservation for 25282 * this device. We wait for sd_reserve_release() 25283 * to return before we return. 25284 */ 25285 cv_wait(&sd_tr.srq_inprocess_cv, 25286 &sd_tr.srq_resv_reclaim_mutex); 25287 } else { 25288 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 25289 if (sd_mhreq && sd_mhreq->dev == dev) { 25290 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 25291 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25292 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25293 return; 25294 } 25295 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 25296 if (sd_mhreq && sd_mhreq->dev == dev) { 25297 break; 25298 } 25299 sd_prev = sd_mhreq; 25300 } 25301 if (sd_mhreq != NULL) { 25302 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 25303 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 25304 } 25305 } 25306 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 25307 } 25308 25309 25310 /* 25311 * Function: sd_mhd_reset_notify_cb() 25312 * 25313 * Description: This is a call back function for scsi_reset_notify. This 25314 * function updates the softstate reserved status and logs the 25315 * reset. The driver scsi watch facility callback function 25316 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 25317 * will reclaim the reservation. 25318 * 25319 * Arguments: arg - driver soft state (unit) structure 25320 */ 25321 25322 static void 25323 sd_mhd_reset_notify_cb(caddr_t arg) 25324 { 25325 struct sd_lun *un = (struct sd_lun *)arg; 25326 25327 mutex_enter(SD_MUTEX(un)); 25328 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25329 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 25330 SD_INFO(SD_LOG_IOCTL_MHD, un, 25331 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 25332 } 25333 mutex_exit(SD_MUTEX(un)); 25334 } 25335 25336 25337 /* 25338 * Function: sd_take_ownership() 25339 * 25340 * Description: This routine implements an algorithm to achieve a stable 25341 * reservation on disks which don't implement priority reserve, 25342 * and makes sure that other host lose re-reservation attempts. 25343 * This algorithm contains of a loop that keeps issuing the RESERVE 25344 * for some period of time (min_ownership_delay, default 6 seconds) 25345 * During that loop, it looks to see if there has been a bus device 25346 * reset or bus reset (both of which cause an existing reservation 25347 * to be lost). If the reservation is lost issue RESERVE until a 25348 * period of min_ownership_delay with no resets has gone by, or 25349 * until max_ownership_delay has expired. This loop ensures that 25350 * the host really did manage to reserve the device, in spite of 25351 * resets. The looping for min_ownership_delay (default six 25352 * seconds) is important to early generation clustering products, 25353 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 25354 * MHIOCENFAILFAST periodic timer of two seconds. By having 25355 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 25356 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 25357 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 25358 * have already noticed, via the MHIOCENFAILFAST polling, that it 25359 * no longer "owns" the disk and will have panicked itself. Thus, 25360 * the host issuing the MHIOCTKOWN is assured (with timing 25361 * dependencies) that by the time it actually starts to use the 25362 * disk for real work, the old owner is no longer accessing it. 25363 * 25364 * min_ownership_delay is the minimum amount of time for which the 25365 * disk must be reserved continuously devoid of resets before the 25366 * MHIOCTKOWN ioctl will return success. 25367 * 25368 * max_ownership_delay indicates the amount of time by which the 25369 * take ownership should succeed or timeout with an error. 25370 * 25371 * Arguments: dev - the device 'dev_t' 25372 * *p - struct containing timing info. 25373 * 25374 * Return Code: 0 for success or error code 25375 */ 25376 25377 static int 25378 sd_take_ownership(dev_t dev, struct mhioctkown *p) 25379 { 25380 struct sd_lun *un; 25381 int rval; 25382 int err; 25383 int reservation_count = 0; 25384 int min_ownership_delay = 6000000; /* in usec */ 25385 int max_ownership_delay = 30000000; /* in usec */ 25386 clock_t start_time; /* starting time of this algorithm */ 25387 clock_t end_time; /* time limit for giving up */ 25388 clock_t ownership_time; /* time limit for stable ownership */ 25389 clock_t current_time; 25390 clock_t previous_current_time; 25391 25392 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25393 return (ENXIO); 25394 } 25395 25396 /* 25397 * Attempt a device reservation. A priority reservation is requested. 25398 */ 25399 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 25400 != SD_SUCCESS) { 25401 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25402 "sd_take_ownership: return(1)=%d\n", rval); 25403 return (rval); 25404 } 25405 25406 /* Update the softstate reserved status to indicate the reservation */ 25407 mutex_enter(SD_MUTEX(un)); 25408 un->un_resvd_status |= SD_RESERVE; 25409 un->un_resvd_status &= 25410 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 25411 mutex_exit(SD_MUTEX(un)); 25412 25413 if (p != NULL) { 25414 if (p->min_ownership_delay != 0) { 25415 min_ownership_delay = p->min_ownership_delay * 1000; 25416 } 25417 if (p->max_ownership_delay != 0) { 25418 max_ownership_delay = p->max_ownership_delay * 1000; 25419 } 25420 } 25421 SD_INFO(SD_LOG_IOCTL_MHD, un, 25422 "sd_take_ownership: min, max delays: %d, %d\n", 25423 min_ownership_delay, max_ownership_delay); 25424 25425 start_time = ddi_get_lbolt(); 25426 current_time = start_time; 25427 ownership_time = current_time + drv_usectohz(min_ownership_delay); 25428 end_time = start_time + drv_usectohz(max_ownership_delay); 25429 25430 while (current_time - end_time < 0) { 25431 delay(drv_usectohz(500000)); 25432 25433 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 25434 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 25435 mutex_enter(SD_MUTEX(un)); 25436 rval = (un->un_resvd_status & 25437 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 25438 mutex_exit(SD_MUTEX(un)); 25439 break; 25440 } 25441 } 25442 previous_current_time = current_time; 25443 current_time = ddi_get_lbolt(); 25444 mutex_enter(SD_MUTEX(un)); 25445 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 25446 ownership_time = ddi_get_lbolt() + 25447 drv_usectohz(min_ownership_delay); 25448 reservation_count = 0; 25449 } else { 25450 reservation_count++; 25451 } 25452 un->un_resvd_status |= SD_RESERVE; 25453 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 25454 mutex_exit(SD_MUTEX(un)); 25455 25456 SD_INFO(SD_LOG_IOCTL_MHD, un, 25457 "sd_take_ownership: ticks for loop iteration=%ld, " 25458 "reservation=%s\n", (current_time - previous_current_time), 25459 reservation_count ? "ok" : "reclaimed"); 25460 25461 if (current_time - ownership_time >= 0 && 25462 reservation_count >= 4) { 25463 rval = 0; /* Achieved a stable ownership */ 25464 break; 25465 } 25466 if (current_time - end_time >= 0) { 25467 rval = EACCES; /* No ownership in max possible time */ 25468 break; 25469 } 25470 } 25471 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25472 "sd_take_ownership: return(2)=%d\n", rval); 25473 return (rval); 25474 } 25475 25476 25477 /* 25478 * Function: sd_reserve_release() 25479 * 25480 * Description: This function builds and sends scsi RESERVE, RELEASE, and 25481 * PRIORITY RESERVE commands based on a user specified command type 25482 * 25483 * Arguments: dev - the device 'dev_t' 25484 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 25485 * SD_RESERVE, SD_RELEASE 25486 * 25487 * Return Code: 0 or Error Code 25488 */ 25489 25490 static int 25491 sd_reserve_release(dev_t dev, int cmd) 25492 { 25493 struct uscsi_cmd *com = NULL; 25494 struct sd_lun *un = NULL; 25495 char cdb[CDB_GROUP0]; 25496 int rval; 25497 25498 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 25499 (cmd == SD_PRIORITY_RESERVE)); 25500 25501 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25502 return (ENXIO); 25503 } 25504 25505 /* instantiate and initialize the command and cdb */ 25506 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25507 bzero(cdb, CDB_GROUP0); 25508 com->uscsi_flags = USCSI_SILENT; 25509 com->uscsi_timeout = un->un_reserve_release_time; 25510 com->uscsi_cdblen = CDB_GROUP0; 25511 com->uscsi_cdb = cdb; 25512 if (cmd == SD_RELEASE) { 25513 cdb[0] = SCMD_RELEASE; 25514 } else { 25515 cdb[0] = SCMD_RESERVE; 25516 } 25517 25518 /* Send the command. */ 25519 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25520 SD_PATH_STANDARD); 25521 25522 /* 25523 * "break" a reservation that is held by another host, by issuing a 25524 * reset if priority reserve is desired, and we could not get the 25525 * device. 25526 */ 25527 if ((cmd == SD_PRIORITY_RESERVE) && 25528 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25529 /* 25530 * First try to reset the LUN. If we cannot, then try a target 25531 * reset, followed by a bus reset if the target reset fails. 25532 */ 25533 int reset_retval = 0; 25534 if (un->un_f_lun_reset_enabled == TRUE) { 25535 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 25536 } 25537 if (reset_retval == 0) { 25538 /* The LUN reset either failed or was not issued */ 25539 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25540 } 25541 if ((reset_retval == 0) && 25542 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 25543 rval = EIO; 25544 kmem_free(com, sizeof (*com)); 25545 return (rval); 25546 } 25547 25548 bzero(com, sizeof (struct uscsi_cmd)); 25549 com->uscsi_flags = USCSI_SILENT; 25550 com->uscsi_cdb = cdb; 25551 com->uscsi_cdblen = CDB_GROUP0; 25552 com->uscsi_timeout = 5; 25553 25554 /* 25555 * Reissue the last reserve command, this time without request 25556 * sense. Assume that it is just a regular reserve command. 25557 */ 25558 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 25559 SD_PATH_STANDARD); 25560 } 25561 25562 /* Return an error if still getting a reservation conflict. */ 25563 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 25564 rval = EACCES; 25565 } 25566 25567 kmem_free(com, sizeof (*com)); 25568 return (rval); 25569 } 25570 25571 25572 #define SD_NDUMP_RETRIES 12 25573 /* 25574 * System Crash Dump routine 25575 */ 25576 25577 static int 25578 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 25579 { 25580 int instance; 25581 int partition; 25582 int i; 25583 int err; 25584 struct sd_lun *un; 25585 struct scsi_pkt *wr_pktp; 25586 struct buf *wr_bp; 25587 struct buf wr_buf; 25588 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 25589 daddr_t tgt_blkno; /* rmw - blkno for target */ 25590 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 25591 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 25592 size_t io_start_offset; 25593 int doing_rmw = FALSE; 25594 int rval; 25595 ssize_t dma_resid; 25596 daddr_t oblkno; 25597 diskaddr_t nblks = 0; 25598 diskaddr_t start_block; 25599 25600 instance = SDUNIT(dev); 25601 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 25602 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 25603 return (ENXIO); 25604 } 25605 25606 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 25607 25608 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 25609 25610 partition = SDPART(dev); 25611 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 25612 25613 if (!(NOT_DEVBSIZE(un))) { 25614 int secmask = 0; 25615 int blknomask = 0; 25616 25617 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1; 25618 secmask = un->un_tgt_blocksize - 1; 25619 25620 if (blkno & blknomask) { 25621 SD_TRACE(SD_LOG_DUMP, un, 25622 "sddump: dump start block not modulo %d\n", 25623 un->un_tgt_blocksize); 25624 return (EINVAL); 25625 } 25626 25627 if ((nblk * DEV_BSIZE) & secmask) { 25628 SD_TRACE(SD_LOG_DUMP, un, 25629 "sddump: dump length not modulo %d\n", 25630 un->un_tgt_blocksize); 25631 return (EINVAL); 25632 } 25633 25634 } 25635 25636 /* Validate blocks to dump at against partition size. */ 25637 25638 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 25639 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 25640 25641 if (NOT_DEVBSIZE(un)) { 25642 if ((blkno + nblk) > nblks) { 25643 SD_TRACE(SD_LOG_DUMP, un, 25644 "sddump: dump range larger than partition: " 25645 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25646 blkno, nblk, nblks); 25647 return (EINVAL); 25648 } 25649 } else { 25650 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) + 25651 (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) { 25652 SD_TRACE(SD_LOG_DUMP, un, 25653 "sddump: dump range larger than partition: " 25654 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 25655 blkno, nblk, nblks); 25656 return (EINVAL); 25657 } 25658 } 25659 25660 mutex_enter(&un->un_pm_mutex); 25661 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25662 struct scsi_pkt *start_pktp; 25663 25664 mutex_exit(&un->un_pm_mutex); 25665 25666 /* 25667 * use pm framework to power on HBA 1st 25668 */ 25669 (void) pm_raise_power(SD_DEVINFO(un), 0, 25670 SD_PM_STATE_ACTIVE(un)); 25671 25672 /* 25673 * Dump no long uses sdpower to power on a device, it's 25674 * in-line here so it can be done in polled mode. 25675 */ 25676 25677 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 25678 25679 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 25680 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 25681 25682 if (start_pktp == NULL) { 25683 /* We were not given a SCSI packet, fail. */ 25684 return (EIO); 25685 } 25686 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 25687 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 25688 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 25689 start_pktp->pkt_flags = FLAG_NOINTR; 25690 25691 mutex_enter(SD_MUTEX(un)); 25692 SD_FILL_SCSI1_LUN(un, start_pktp); 25693 mutex_exit(SD_MUTEX(un)); 25694 /* 25695 * Scsi_poll returns 0 (success) if the command completes and 25696 * the status block is STATUS_GOOD. 25697 */ 25698 if (sd_scsi_poll(un, start_pktp) != 0) { 25699 scsi_destroy_pkt(start_pktp); 25700 return (EIO); 25701 } 25702 scsi_destroy_pkt(start_pktp); 25703 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un), 25704 SD_PM_STATE_CHANGE); 25705 } else { 25706 mutex_exit(&un->un_pm_mutex); 25707 } 25708 25709 mutex_enter(SD_MUTEX(un)); 25710 un->un_throttle = 0; 25711 25712 /* 25713 * The first time through, reset the specific target device. 25714 * However, when cpr calls sddump we know that sd is in a 25715 * a good state so no bus reset is required. 25716 * Clear sense data via Request Sense cmd. 25717 * In sddump we don't care about allow_bus_device_reset anymore 25718 */ 25719 25720 if ((un->un_state != SD_STATE_SUSPENDED) && 25721 (un->un_state != SD_STATE_DUMPING)) { 25722 25723 New_state(un, SD_STATE_DUMPING); 25724 25725 if (un->un_f_is_fibre == FALSE) { 25726 mutex_exit(SD_MUTEX(un)); 25727 /* 25728 * Attempt a bus reset for parallel scsi. 25729 * 25730 * Note: A bus reset is required because on some host 25731 * systems (i.e. E420R) a bus device reset is 25732 * insufficient to reset the state of the target. 25733 * 25734 * Note: Don't issue the reset for fibre-channel, 25735 * because this tends to hang the bus (loop) for 25736 * too long while everyone is logging out and in 25737 * and the deadman timer for dumping will fire 25738 * before the dump is complete. 25739 */ 25740 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 25741 mutex_enter(SD_MUTEX(un)); 25742 Restore_state(un); 25743 mutex_exit(SD_MUTEX(un)); 25744 return (EIO); 25745 } 25746 25747 /* Delay to give the device some recovery time. */ 25748 drv_usecwait(10000); 25749 25750 if (sd_send_polled_RQS(un) == SD_FAILURE) { 25751 SD_INFO(SD_LOG_DUMP, un, 25752 "sddump: sd_send_polled_RQS failed\n"); 25753 } 25754 mutex_enter(SD_MUTEX(un)); 25755 } 25756 } 25757 25758 /* 25759 * Convert the partition-relative block number to a 25760 * disk physical block number. 25761 */ 25762 if (NOT_DEVBSIZE(un)) { 25763 blkno += start_block; 25764 } else { 25765 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE); 25766 blkno += start_block; 25767 } 25768 25769 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 25770 25771 25772 /* 25773 * Check if the device has a non-512 block size. 25774 */ 25775 wr_bp = NULL; 25776 if (NOT_DEVBSIZE(un)) { 25777 tgt_byte_offset = blkno * un->un_sys_blocksize; 25778 tgt_byte_count = nblk * un->un_sys_blocksize; 25779 if ((tgt_byte_offset % un->un_tgt_blocksize) || 25780 (tgt_byte_count % un->un_tgt_blocksize)) { 25781 doing_rmw = TRUE; 25782 /* 25783 * Calculate the block number and number of block 25784 * in terms of the media block size. 25785 */ 25786 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25787 tgt_nblk = 25788 ((tgt_byte_offset + tgt_byte_count + 25789 (un->un_tgt_blocksize - 1)) / 25790 un->un_tgt_blocksize) - tgt_blkno; 25791 25792 /* 25793 * Invoke the routine which is going to do read part 25794 * of read-modify-write. 25795 * Note that this routine returns a pointer to 25796 * a valid bp in wr_bp. 25797 */ 25798 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 25799 &wr_bp); 25800 if (err) { 25801 mutex_exit(SD_MUTEX(un)); 25802 return (err); 25803 } 25804 /* 25805 * Offset is being calculated as - 25806 * (original block # * system block size) - 25807 * (new block # * target block size) 25808 */ 25809 io_start_offset = 25810 ((uint64_t)(blkno * un->un_sys_blocksize)) - 25811 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 25812 25813 ASSERT((io_start_offset >= 0) && 25814 (io_start_offset < un->un_tgt_blocksize)); 25815 /* 25816 * Do the modify portion of read modify write. 25817 */ 25818 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 25819 (size_t)nblk * un->un_sys_blocksize); 25820 } else { 25821 doing_rmw = FALSE; 25822 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 25823 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 25824 } 25825 25826 /* Convert blkno and nblk to target blocks */ 25827 blkno = tgt_blkno; 25828 nblk = tgt_nblk; 25829 } else { 25830 wr_bp = &wr_buf; 25831 bzero(wr_bp, sizeof (struct buf)); 25832 wr_bp->b_flags = B_BUSY; 25833 wr_bp->b_un.b_addr = addr; 25834 wr_bp->b_bcount = nblk << DEV_BSHIFT; 25835 wr_bp->b_resid = 0; 25836 } 25837 25838 mutex_exit(SD_MUTEX(un)); 25839 25840 /* 25841 * Obtain a SCSI packet for the write command. 25842 * It should be safe to call the allocator here without 25843 * worrying about being locked for DVMA mapping because 25844 * the address we're passed is already a DVMA mapping 25845 * 25846 * We are also not going to worry about semaphore ownership 25847 * in the dump buffer. Dumping is single threaded at present. 25848 */ 25849 25850 wr_pktp = NULL; 25851 25852 dma_resid = wr_bp->b_bcount; 25853 oblkno = blkno; 25854 25855 if (!(NOT_DEVBSIZE(un))) { 25856 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE); 25857 } 25858 25859 while (dma_resid != 0) { 25860 25861 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 25862 wr_bp->b_flags &= ~B_ERROR; 25863 25864 if (un->un_partial_dma_supported == 1) { 25865 blkno = oblkno + 25866 ((wr_bp->b_bcount - dma_resid) / 25867 un->un_tgt_blocksize); 25868 nblk = dma_resid / un->un_tgt_blocksize; 25869 25870 if (wr_pktp) { 25871 /* 25872 * Partial DMA transfers after initial transfer 25873 */ 25874 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 25875 blkno, nblk); 25876 } else { 25877 /* Initial transfer */ 25878 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 25879 un->un_pkt_flags, NULL_FUNC, NULL, 25880 blkno, nblk); 25881 } 25882 } else { 25883 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 25884 0, NULL_FUNC, NULL, blkno, nblk); 25885 } 25886 25887 if (rval == 0) { 25888 /* We were given a SCSI packet, continue. */ 25889 break; 25890 } 25891 25892 if (i == 0) { 25893 if (wr_bp->b_flags & B_ERROR) { 25894 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25895 "no resources for dumping; " 25896 "error code: 0x%x, retrying", 25897 geterror(wr_bp)); 25898 } else { 25899 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25900 "no resources for dumping; retrying"); 25901 } 25902 } else if (i != (SD_NDUMP_RETRIES - 1)) { 25903 if (wr_bp->b_flags & B_ERROR) { 25904 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25905 "no resources for dumping; error code: " 25906 "0x%x, retrying\n", geterror(wr_bp)); 25907 } 25908 } else { 25909 if (wr_bp->b_flags & B_ERROR) { 25910 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25911 "no resources for dumping; " 25912 "error code: 0x%x, retries failed, " 25913 "giving up.\n", geterror(wr_bp)); 25914 } else { 25915 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 25916 "no resources for dumping; " 25917 "retries failed, giving up.\n"); 25918 } 25919 mutex_enter(SD_MUTEX(un)); 25920 Restore_state(un); 25921 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 25922 mutex_exit(SD_MUTEX(un)); 25923 scsi_free_consistent_buf(wr_bp); 25924 } else { 25925 mutex_exit(SD_MUTEX(un)); 25926 } 25927 return (EIO); 25928 } 25929 drv_usecwait(10000); 25930 } 25931 25932 if (un->un_partial_dma_supported == 1) { 25933 /* 25934 * save the resid from PARTIAL_DMA 25935 */ 25936 dma_resid = wr_pktp->pkt_resid; 25937 if (dma_resid != 0) 25938 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 25939 wr_pktp->pkt_resid = 0; 25940 } else { 25941 dma_resid = 0; 25942 } 25943 25944 /* SunBug 1222170 */ 25945 wr_pktp->pkt_flags = FLAG_NOINTR; 25946 25947 err = EIO; 25948 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 25949 25950 /* 25951 * Scsi_poll returns 0 (success) if the command completes and 25952 * the status block is STATUS_GOOD. We should only check 25953 * errors if this condition is not true. Even then we should 25954 * send our own request sense packet only if we have a check 25955 * condition and auto request sense has not been performed by 25956 * the hba. 25957 */ 25958 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 25959 25960 if ((sd_scsi_poll(un, wr_pktp) == 0) && 25961 (wr_pktp->pkt_resid == 0)) { 25962 err = SD_SUCCESS; 25963 break; 25964 } 25965 25966 /* 25967 * Check CMD_DEV_GONE 1st, give up if device is gone. 25968 */ 25969 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 25970 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25971 "Error while dumping state...Device is gone\n"); 25972 break; 25973 } 25974 25975 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 25976 SD_INFO(SD_LOG_DUMP, un, 25977 "sddump: write failed with CHECK, try # %d\n", i); 25978 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 25979 (void) sd_send_polled_RQS(un); 25980 } 25981 25982 continue; 25983 } 25984 25985 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 25986 int reset_retval = 0; 25987 25988 SD_INFO(SD_LOG_DUMP, un, 25989 "sddump: write failed with BUSY, try # %d\n", i); 25990 25991 if (un->un_f_lun_reset_enabled == TRUE) { 25992 reset_retval = scsi_reset(SD_ADDRESS(un), 25993 RESET_LUN); 25994 } 25995 if (reset_retval == 0) { 25996 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 25997 } 25998 (void) sd_send_polled_RQS(un); 25999 26000 } else { 26001 SD_INFO(SD_LOG_DUMP, un, 26002 "sddump: write failed with 0x%x, try # %d\n", 26003 SD_GET_PKT_STATUS(wr_pktp), i); 26004 mutex_enter(SD_MUTEX(un)); 26005 sd_reset_target(un, wr_pktp); 26006 mutex_exit(SD_MUTEX(un)); 26007 } 26008 26009 /* 26010 * If we are not getting anywhere with lun/target resets, 26011 * let's reset the bus. 26012 */ 26013 if (i == SD_NDUMP_RETRIES/2) { 26014 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26015 (void) sd_send_polled_RQS(un); 26016 } 26017 } 26018 } 26019 26020 scsi_destroy_pkt(wr_pktp); 26021 mutex_enter(SD_MUTEX(un)); 26022 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26023 mutex_exit(SD_MUTEX(un)); 26024 scsi_free_consistent_buf(wr_bp); 26025 } else { 26026 mutex_exit(SD_MUTEX(un)); 26027 } 26028 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26029 return (err); 26030 } 26031 26032 /* 26033 * Function: sd_scsi_poll() 26034 * 26035 * Description: This is a wrapper for the scsi_poll call. 26036 * 26037 * Arguments: sd_lun - The unit structure 26038 * scsi_pkt - The scsi packet being sent to the device. 26039 * 26040 * Return Code: 0 - Command completed successfully with good status 26041 * -1 - Command failed. This could indicate a check condition 26042 * or other status value requiring recovery action. 26043 * 26044 * NOTE: This code is only called off sddump(). 26045 */ 26046 26047 static int 26048 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26049 { 26050 int status; 26051 26052 ASSERT(un != NULL); 26053 ASSERT(!mutex_owned(SD_MUTEX(un))); 26054 ASSERT(pktp != NULL); 26055 26056 status = SD_SUCCESS; 26057 26058 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26059 pktp->pkt_flags |= un->un_tagflags; 26060 pktp->pkt_flags &= ~FLAG_NODISCON; 26061 } 26062 26063 status = sd_ddi_scsi_poll(pktp); 26064 /* 26065 * Scsi_poll returns 0 (success) if the command completes and the 26066 * status block is STATUS_GOOD. We should only check errors if this 26067 * condition is not true. Even then we should send our own request 26068 * sense packet only if we have a check condition and auto 26069 * request sense has not been performed by the hba. 26070 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26071 */ 26072 if ((status != SD_SUCCESS) && 26073 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26074 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26075 (pktp->pkt_reason != CMD_DEV_GONE)) 26076 (void) sd_send_polled_RQS(un); 26077 26078 return (status); 26079 } 26080 26081 /* 26082 * Function: sd_send_polled_RQS() 26083 * 26084 * Description: This sends the request sense command to a device. 26085 * 26086 * Arguments: sd_lun - The unit structure 26087 * 26088 * Return Code: 0 - Command completed successfully with good status 26089 * -1 - Command failed. 26090 * 26091 */ 26092 26093 static int 26094 sd_send_polled_RQS(struct sd_lun *un) 26095 { 26096 int ret_val; 26097 struct scsi_pkt *rqs_pktp; 26098 struct buf *rqs_bp; 26099 26100 ASSERT(un != NULL); 26101 ASSERT(!mutex_owned(SD_MUTEX(un))); 26102 26103 ret_val = SD_SUCCESS; 26104 26105 rqs_pktp = un->un_rqs_pktp; 26106 rqs_bp = un->un_rqs_bp; 26107 26108 mutex_enter(SD_MUTEX(un)); 26109 26110 if (un->un_sense_isbusy) { 26111 ret_val = SD_FAILURE; 26112 mutex_exit(SD_MUTEX(un)); 26113 return (ret_val); 26114 } 26115 26116 /* 26117 * If the request sense buffer (and packet) is not in use, 26118 * let's set the un_sense_isbusy and send our packet 26119 */ 26120 un->un_sense_isbusy = 1; 26121 rqs_pktp->pkt_resid = 0; 26122 rqs_pktp->pkt_reason = 0; 26123 rqs_pktp->pkt_flags |= FLAG_NOINTR; 26124 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 26125 26126 mutex_exit(SD_MUTEX(un)); 26127 26128 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 26129 " 0x%p\n", rqs_bp->b_un.b_addr); 26130 26131 /* 26132 * Can't send this to sd_scsi_poll, we wrap ourselves around the 26133 * axle - it has a call into us! 26134 */ 26135 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 26136 SD_INFO(SD_LOG_COMMON, un, 26137 "sd_send_polled_RQS: RQS failed\n"); 26138 } 26139 26140 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 26141 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 26142 26143 mutex_enter(SD_MUTEX(un)); 26144 un->un_sense_isbusy = 0; 26145 mutex_exit(SD_MUTEX(un)); 26146 26147 return (ret_val); 26148 } 26149 26150 /* 26151 * Defines needed for localized version of the scsi_poll routine. 26152 */ 26153 #define CSEC 10000 /* usecs */ 26154 #define SEC_TO_CSEC (1000000/CSEC) 26155 26156 /* 26157 * Function: sd_ddi_scsi_poll() 26158 * 26159 * Description: Localized version of the scsi_poll routine. The purpose is to 26160 * send a scsi_pkt to a device as a polled command. This version 26161 * is to ensure more robust handling of transport errors. 26162 * Specifically this routine cures not ready, coming ready 26163 * transition for power up and reset of sonoma's. This can take 26164 * up to 45 seconds for power-on and 20 seconds for reset of a 26165 * sonoma lun. 26166 * 26167 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 26168 * 26169 * Return Code: 0 - Command completed successfully with good status 26170 * -1 - Command failed. 26171 * 26172 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can 26173 * be fixed (removing this code), we need to determine how to handle the 26174 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump(). 26175 * 26176 * NOTE: This code is only called off sddump(). 26177 */ 26178 static int 26179 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 26180 { 26181 int rval = -1; 26182 int savef; 26183 long savet; 26184 void (*savec)(); 26185 int timeout; 26186 int busy_count; 26187 int poll_delay; 26188 int rc; 26189 uint8_t *sensep; 26190 struct scsi_arq_status *arqstat; 26191 extern int do_polled_io; 26192 26193 ASSERT(pkt->pkt_scbp); 26194 26195 /* 26196 * save old flags.. 26197 */ 26198 savef = pkt->pkt_flags; 26199 savec = pkt->pkt_comp; 26200 savet = pkt->pkt_time; 26201 26202 pkt->pkt_flags |= FLAG_NOINTR; 26203 26204 /* 26205 * XXX there is nothing in the SCSA spec that states that we should not 26206 * do a callback for polled cmds; however, removing this will break sd 26207 * and probably other target drivers 26208 */ 26209 pkt->pkt_comp = NULL; 26210 26211 /* 26212 * we don't like a polled command without timeout. 26213 * 60 seconds seems long enough. 26214 */ 26215 if (pkt->pkt_time == 0) 26216 pkt->pkt_time = SCSI_POLL_TIMEOUT; 26217 26218 /* 26219 * Send polled cmd. 26220 * 26221 * We do some error recovery for various errors. Tran_busy, 26222 * queue full, and non-dispatched commands are retried every 10 msec. 26223 * as they are typically transient failures. Busy status and Not 26224 * Ready are retried every second as this status takes a while to 26225 * change. 26226 */ 26227 timeout = pkt->pkt_time * SEC_TO_CSEC; 26228 26229 for (busy_count = 0; busy_count < timeout; busy_count++) { 26230 /* 26231 * Initialize pkt status variables. 26232 */ 26233 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 26234 26235 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 26236 if (rc != TRAN_BUSY) { 26237 /* Transport failed - give up. */ 26238 break; 26239 } else { 26240 /* Transport busy - try again. */ 26241 poll_delay = 1 * CSEC; /* 10 msec. */ 26242 } 26243 } else { 26244 /* 26245 * Transport accepted - check pkt status. 26246 */ 26247 rc = (*pkt->pkt_scbp) & STATUS_MASK; 26248 if ((pkt->pkt_reason == CMD_CMPLT) && 26249 (rc == STATUS_CHECK) && 26250 (pkt->pkt_state & STATE_ARQ_DONE)) { 26251 arqstat = 26252 (struct scsi_arq_status *)(pkt->pkt_scbp); 26253 sensep = (uint8_t *)&arqstat->sts_sensedata; 26254 } else { 26255 sensep = NULL; 26256 } 26257 26258 if ((pkt->pkt_reason == CMD_CMPLT) && 26259 (rc == STATUS_GOOD)) { 26260 /* No error - we're done */ 26261 rval = 0; 26262 break; 26263 26264 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 26265 /* Lost connection - give up */ 26266 break; 26267 26268 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 26269 (pkt->pkt_state == 0)) { 26270 /* Pkt not dispatched - try again. */ 26271 poll_delay = 1 * CSEC; /* 10 msec. */ 26272 26273 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26274 (rc == STATUS_QFULL)) { 26275 /* Queue full - try again. */ 26276 poll_delay = 1 * CSEC; /* 10 msec. */ 26277 26278 } else if ((pkt->pkt_reason == CMD_CMPLT) && 26279 (rc == STATUS_BUSY)) { 26280 /* Busy - try again. */ 26281 poll_delay = 100 * CSEC; /* 1 sec. */ 26282 busy_count += (SEC_TO_CSEC - 1); 26283 26284 } else if ((sensep != NULL) && 26285 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) { 26286 /* 26287 * Unit Attention - try again. 26288 * Pretend it took 1 sec. 26289 * NOTE: 'continue' avoids poll_delay 26290 */ 26291 busy_count += (SEC_TO_CSEC - 1); 26292 continue; 26293 26294 } else if ((sensep != NULL) && 26295 (scsi_sense_key(sensep) == KEY_NOT_READY) && 26296 (scsi_sense_asc(sensep) == 0x04) && 26297 (scsi_sense_ascq(sensep) == 0x01)) { 26298 /* 26299 * Not ready -> ready - try again. 26300 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY 26301 * ...same as STATUS_BUSY 26302 */ 26303 poll_delay = 100 * CSEC; /* 1 sec. */ 26304 busy_count += (SEC_TO_CSEC - 1); 26305 26306 } else { 26307 /* BAD status - give up. */ 26308 break; 26309 } 26310 } 26311 26312 if (((curthread->t_flag & T_INTR_THREAD) == 0) && 26313 !do_polled_io) { 26314 delay(drv_usectohz(poll_delay)); 26315 } else { 26316 /* we busy wait during cpr_dump or interrupt threads */ 26317 drv_usecwait(poll_delay); 26318 } 26319 } 26320 26321 pkt->pkt_flags = savef; 26322 pkt->pkt_comp = savec; 26323 pkt->pkt_time = savet; 26324 26325 /* return on error */ 26326 if (rval) 26327 return (rval); 26328 26329 /* 26330 * This is not a performance critical code path. 26331 * 26332 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync() 26333 * issues associated with looking at DMA memory prior to 26334 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return. 26335 */ 26336 scsi_sync_pkt(pkt); 26337 return (0); 26338 } 26339 26340 26341 26342 /* 26343 * Function: sd_persistent_reservation_in_read_keys 26344 * 26345 * Description: This routine is the driver entry point for handling CD-ROM 26346 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 26347 * by sending the SCSI-3 PRIN commands to the device. 26348 * Processes the read keys command response by copying the 26349 * reservation key information into the user provided buffer. 26350 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 26351 * 26352 * Arguments: un - Pointer to soft state struct for the target. 26353 * usrp - user provided pointer to multihost Persistent In Read 26354 * Keys structure (mhioc_inkeys_t) 26355 * flag - this argument is a pass through to ddi_copyxxx() 26356 * directly from the mode argument of ioctl(). 26357 * 26358 * Return Code: 0 - Success 26359 * EACCES 26360 * ENOTSUP 26361 * errno return code from sd_send_scsi_cmd() 26362 * 26363 * Context: Can sleep. Does not return until command is completed. 26364 */ 26365 26366 static int 26367 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 26368 mhioc_inkeys_t *usrp, int flag) 26369 { 26370 #ifdef _MULTI_DATAMODEL 26371 struct mhioc_key_list32 li32; 26372 #endif 26373 sd_prin_readkeys_t *in; 26374 mhioc_inkeys_t *ptr; 26375 mhioc_key_list_t li; 26376 uchar_t *data_bufp; 26377 int data_len; 26378 int rval = 0; 26379 size_t copysz; 26380 sd_ssc_t *ssc; 26381 26382 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 26383 return (EINVAL); 26384 } 26385 bzero(&li, sizeof (mhioc_key_list_t)); 26386 26387 ssc = sd_ssc_init(un); 26388 26389 /* 26390 * Get the listsize from user 26391 */ 26392 #ifdef _MULTI_DATAMODEL 26393 26394 switch (ddi_model_convert_from(flag & FMODELS)) { 26395 case DDI_MODEL_ILP32: 26396 copysz = sizeof (struct mhioc_key_list32); 26397 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 26398 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26399 "sd_persistent_reservation_in_read_keys: " 26400 "failed ddi_copyin: mhioc_key_list32_t\n"); 26401 rval = EFAULT; 26402 goto done; 26403 } 26404 li.listsize = li32.listsize; 26405 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 26406 break; 26407 26408 case DDI_MODEL_NONE: 26409 copysz = sizeof (mhioc_key_list_t); 26410 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26411 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26412 "sd_persistent_reservation_in_read_keys: " 26413 "failed ddi_copyin: mhioc_key_list_t\n"); 26414 rval = EFAULT; 26415 goto done; 26416 } 26417 break; 26418 } 26419 26420 #else /* ! _MULTI_DATAMODEL */ 26421 copysz = sizeof (mhioc_key_list_t); 26422 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 26423 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26424 "sd_persistent_reservation_in_read_keys: " 26425 "failed ddi_copyin: mhioc_key_list_t\n"); 26426 rval = EFAULT; 26427 goto done; 26428 } 26429 #endif 26430 26431 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 26432 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 26433 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26434 26435 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 26436 data_len, data_bufp); 26437 if (rval != 0) { 26438 if (rval == EIO) 26439 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26440 else 26441 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26442 goto done; 26443 } 26444 in = (sd_prin_readkeys_t *)data_bufp; 26445 ptr->generation = BE_32(in->generation); 26446 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 26447 26448 /* 26449 * Return the min(listsize, listlen) keys 26450 */ 26451 #ifdef _MULTI_DATAMODEL 26452 26453 switch (ddi_model_convert_from(flag & FMODELS)) { 26454 case DDI_MODEL_ILP32: 26455 li32.listlen = li.listlen; 26456 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 26457 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26458 "sd_persistent_reservation_in_read_keys: " 26459 "failed ddi_copyout: mhioc_key_list32_t\n"); 26460 rval = EFAULT; 26461 goto done; 26462 } 26463 break; 26464 26465 case DDI_MODEL_NONE: 26466 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26467 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26468 "sd_persistent_reservation_in_read_keys: " 26469 "failed ddi_copyout: mhioc_key_list_t\n"); 26470 rval = EFAULT; 26471 goto done; 26472 } 26473 break; 26474 } 26475 26476 #else /* ! _MULTI_DATAMODEL */ 26477 26478 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 26479 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26480 "sd_persistent_reservation_in_read_keys: " 26481 "failed ddi_copyout: mhioc_key_list_t\n"); 26482 rval = EFAULT; 26483 goto done; 26484 } 26485 26486 #endif /* _MULTI_DATAMODEL */ 26487 26488 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 26489 li.listsize * MHIOC_RESV_KEY_SIZE); 26490 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 26491 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26492 "sd_persistent_reservation_in_read_keys: " 26493 "failed ddi_copyout: keylist\n"); 26494 rval = EFAULT; 26495 } 26496 done: 26497 sd_ssc_fini(ssc); 26498 kmem_free(data_bufp, data_len); 26499 return (rval); 26500 } 26501 26502 26503 /* 26504 * Function: sd_persistent_reservation_in_read_resv 26505 * 26506 * Description: This routine is the driver entry point for handling CD-ROM 26507 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 26508 * by sending the SCSI-3 PRIN commands to the device. 26509 * Process the read persistent reservations command response by 26510 * copying the reservation information into the user provided 26511 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 26512 * 26513 * Arguments: un - Pointer to soft state struct for the target. 26514 * usrp - user provided pointer to multihost Persistent In Read 26515 * Keys structure (mhioc_inkeys_t) 26516 * flag - this argument is a pass through to ddi_copyxxx() 26517 * directly from the mode argument of ioctl(). 26518 * 26519 * Return Code: 0 - Success 26520 * EACCES 26521 * ENOTSUP 26522 * errno return code from sd_send_scsi_cmd() 26523 * 26524 * Context: Can sleep. Does not return until command is completed. 26525 */ 26526 26527 static int 26528 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 26529 mhioc_inresvs_t *usrp, int flag) 26530 { 26531 #ifdef _MULTI_DATAMODEL 26532 struct mhioc_resv_desc_list32 resvlist32; 26533 #endif 26534 sd_prin_readresv_t *in; 26535 mhioc_inresvs_t *ptr; 26536 sd_readresv_desc_t *readresv_ptr; 26537 mhioc_resv_desc_list_t resvlist; 26538 mhioc_resv_desc_t resvdesc; 26539 uchar_t *data_bufp = NULL; 26540 int data_len; 26541 int rval = 0; 26542 int i; 26543 size_t copysz; 26544 mhioc_resv_desc_t *bufp; 26545 sd_ssc_t *ssc; 26546 26547 if ((ptr = usrp) == NULL) { 26548 return (EINVAL); 26549 } 26550 26551 ssc = sd_ssc_init(un); 26552 26553 /* 26554 * Get the listsize from user 26555 */ 26556 #ifdef _MULTI_DATAMODEL 26557 switch (ddi_model_convert_from(flag & FMODELS)) { 26558 case DDI_MODEL_ILP32: 26559 copysz = sizeof (struct mhioc_resv_desc_list32); 26560 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 26561 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26562 "sd_persistent_reservation_in_read_resv: " 26563 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26564 rval = EFAULT; 26565 goto done; 26566 } 26567 resvlist.listsize = resvlist32.listsize; 26568 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 26569 break; 26570 26571 case DDI_MODEL_NONE: 26572 copysz = sizeof (mhioc_resv_desc_list_t); 26573 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26574 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26575 "sd_persistent_reservation_in_read_resv: " 26576 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26577 rval = EFAULT; 26578 goto done; 26579 } 26580 break; 26581 } 26582 #else /* ! _MULTI_DATAMODEL */ 26583 copysz = sizeof (mhioc_resv_desc_list_t); 26584 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 26585 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26586 "sd_persistent_reservation_in_read_resv: " 26587 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 26588 rval = EFAULT; 26589 goto done; 26590 } 26591 #endif /* ! _MULTI_DATAMODEL */ 26592 26593 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 26594 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 26595 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 26596 26597 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV, 26598 data_len, data_bufp); 26599 if (rval != 0) { 26600 if (rval == EIO) 26601 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 26602 else 26603 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 26604 goto done; 26605 } 26606 in = (sd_prin_readresv_t *)data_bufp; 26607 ptr->generation = BE_32(in->generation); 26608 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 26609 26610 /* 26611 * Return the min(listsize, listlen( keys 26612 */ 26613 #ifdef _MULTI_DATAMODEL 26614 26615 switch (ddi_model_convert_from(flag & FMODELS)) { 26616 case DDI_MODEL_ILP32: 26617 resvlist32.listlen = resvlist.listlen; 26618 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 26619 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26620 "sd_persistent_reservation_in_read_resv: " 26621 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26622 rval = EFAULT; 26623 goto done; 26624 } 26625 break; 26626 26627 case DDI_MODEL_NONE: 26628 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26629 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26630 "sd_persistent_reservation_in_read_resv: " 26631 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26632 rval = EFAULT; 26633 goto done; 26634 } 26635 break; 26636 } 26637 26638 #else /* ! _MULTI_DATAMODEL */ 26639 26640 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 26641 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26642 "sd_persistent_reservation_in_read_resv: " 26643 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 26644 rval = EFAULT; 26645 goto done; 26646 } 26647 26648 #endif /* ! _MULTI_DATAMODEL */ 26649 26650 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 26651 bufp = resvlist.list; 26652 copysz = sizeof (mhioc_resv_desc_t); 26653 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 26654 i++, readresv_ptr++, bufp++) { 26655 26656 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 26657 MHIOC_RESV_KEY_SIZE); 26658 resvdesc.type = readresv_ptr->type; 26659 resvdesc.scope = readresv_ptr->scope; 26660 resvdesc.scope_specific_addr = 26661 BE_32(readresv_ptr->scope_specific_addr); 26662 26663 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 26664 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26665 "sd_persistent_reservation_in_read_resv: " 26666 "failed ddi_copyout: resvlist\n"); 26667 rval = EFAULT; 26668 goto done; 26669 } 26670 } 26671 done: 26672 sd_ssc_fini(ssc); 26673 /* only if data_bufp is allocated, we need to free it */ 26674 if (data_bufp) { 26675 kmem_free(data_bufp, data_len); 26676 } 26677 return (rval); 26678 } 26679 26680 26681 /* 26682 * Function: sr_change_blkmode() 26683 * 26684 * Description: This routine is the driver entry point for handling CD-ROM 26685 * block mode ioctl requests. Support for returning and changing 26686 * the current block size in use by the device is implemented. The 26687 * LBA size is changed via a MODE SELECT Block Descriptor. 26688 * 26689 * This routine issues a mode sense with an allocation length of 26690 * 12 bytes for the mode page header and a single block descriptor. 26691 * 26692 * Arguments: dev - the device 'dev_t' 26693 * cmd - the request type; one of CDROMGBLKMODE (get) or 26694 * CDROMSBLKMODE (set) 26695 * data - current block size or requested block size 26696 * flag - this argument is a pass through to ddi_copyxxx() directly 26697 * from the mode argument of ioctl(). 26698 * 26699 * Return Code: the code returned by sd_send_scsi_cmd() 26700 * EINVAL if invalid arguments are provided 26701 * EFAULT if ddi_copyxxx() fails 26702 * ENXIO if fail ddi_get_soft_state 26703 * EIO if invalid mode sense block descriptor length 26704 * 26705 */ 26706 26707 static int 26708 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 26709 { 26710 struct sd_lun *un = NULL; 26711 struct mode_header *sense_mhp, *select_mhp; 26712 struct block_descriptor *sense_desc, *select_desc; 26713 int current_bsize; 26714 int rval = EINVAL; 26715 uchar_t *sense = NULL; 26716 uchar_t *select = NULL; 26717 sd_ssc_t *ssc; 26718 26719 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 26720 26721 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26722 return (ENXIO); 26723 } 26724 26725 /* 26726 * The block length is changed via the Mode Select block descriptor, the 26727 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 26728 * required as part of this routine. Therefore the mode sense allocation 26729 * length is specified to be the length of a mode page header and a 26730 * block descriptor. 26731 */ 26732 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26733 26734 ssc = sd_ssc_init(un); 26735 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 26736 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 26737 sd_ssc_fini(ssc); 26738 if (rval != 0) { 26739 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26740 "sr_change_blkmode: Mode Sense Failed\n"); 26741 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26742 return (rval); 26743 } 26744 26745 /* Check the block descriptor len to handle only 1 block descriptor */ 26746 sense_mhp = (struct mode_header *)sense; 26747 if ((sense_mhp->bdesc_length == 0) || 26748 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 26749 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26750 "sr_change_blkmode: Mode Sense returned invalid block" 26751 " descriptor length\n"); 26752 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26753 return (EIO); 26754 } 26755 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 26756 current_bsize = ((sense_desc->blksize_hi << 16) | 26757 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 26758 26759 /* Process command */ 26760 switch (cmd) { 26761 case CDROMGBLKMODE: 26762 /* Return the block size obtained during the mode sense */ 26763 if (ddi_copyout(¤t_bsize, (void *)data, 26764 sizeof (int), flag) != 0) 26765 rval = EFAULT; 26766 break; 26767 case CDROMSBLKMODE: 26768 /* Validate the requested block size */ 26769 switch (data) { 26770 case CDROM_BLK_512: 26771 case CDROM_BLK_1024: 26772 case CDROM_BLK_2048: 26773 case CDROM_BLK_2056: 26774 case CDROM_BLK_2336: 26775 case CDROM_BLK_2340: 26776 case CDROM_BLK_2352: 26777 case CDROM_BLK_2368: 26778 case CDROM_BLK_2448: 26779 case CDROM_BLK_2646: 26780 case CDROM_BLK_2647: 26781 break; 26782 default: 26783 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26784 "sr_change_blkmode: " 26785 "Block Size '%ld' Not Supported\n", data); 26786 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26787 return (EINVAL); 26788 } 26789 26790 /* 26791 * The current block size matches the requested block size so 26792 * there is no need to send the mode select to change the size 26793 */ 26794 if (current_bsize == data) { 26795 break; 26796 } 26797 26798 /* Build the select data for the requested block size */ 26799 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 26800 select_mhp = (struct mode_header *)select; 26801 select_desc = 26802 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 26803 /* 26804 * The LBA size is changed via the block descriptor, so the 26805 * descriptor is built according to the user data 26806 */ 26807 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 26808 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 26809 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 26810 select_desc->blksize_lo = (char)((data) & 0x000000ff); 26811 26812 /* Send the mode select for the requested block size */ 26813 ssc = sd_ssc_init(un); 26814 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 26815 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26816 SD_PATH_STANDARD); 26817 sd_ssc_fini(ssc); 26818 if (rval != 0) { 26819 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26820 "sr_change_blkmode: Mode Select Failed\n"); 26821 /* 26822 * The mode select failed for the requested block size, 26823 * so reset the data for the original block size and 26824 * send it to the target. The error is indicated by the 26825 * return value for the failed mode select. 26826 */ 26827 select_desc->blksize_hi = sense_desc->blksize_hi; 26828 select_desc->blksize_mid = sense_desc->blksize_mid; 26829 select_desc->blksize_lo = sense_desc->blksize_lo; 26830 ssc = sd_ssc_init(un); 26831 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 26832 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 26833 SD_PATH_STANDARD); 26834 sd_ssc_fini(ssc); 26835 } else { 26836 ASSERT(!mutex_owned(SD_MUTEX(un))); 26837 mutex_enter(SD_MUTEX(un)); 26838 sd_update_block_info(un, (uint32_t)data, 0); 26839 mutex_exit(SD_MUTEX(un)); 26840 } 26841 break; 26842 default: 26843 /* should not reach here, but check anyway */ 26844 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26845 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 26846 rval = EINVAL; 26847 break; 26848 } 26849 26850 if (select) { 26851 kmem_free(select, BUFLEN_CHG_BLK_MODE); 26852 } 26853 if (sense) { 26854 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 26855 } 26856 return (rval); 26857 } 26858 26859 26860 /* 26861 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 26862 * implement driver support for getting and setting the CD speed. The command 26863 * set used will be based on the device type. If the device has not been 26864 * identified as MMC the Toshiba vendor specific mode page will be used. If 26865 * the device is MMC but does not support the Real Time Streaming feature 26866 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 26867 * be used to read the speed. 26868 */ 26869 26870 /* 26871 * Function: sr_change_speed() 26872 * 26873 * Description: This routine is the driver entry point for handling CD-ROM 26874 * drive speed ioctl requests for devices supporting the Toshiba 26875 * vendor specific drive speed mode page. Support for returning 26876 * and changing the current drive speed in use by the device is 26877 * implemented. 26878 * 26879 * Arguments: dev - the device 'dev_t' 26880 * cmd - the request type; one of CDROMGDRVSPEED (get) or 26881 * CDROMSDRVSPEED (set) 26882 * data - current drive speed or requested drive speed 26883 * flag - this argument is a pass through to ddi_copyxxx() directly 26884 * from the mode argument of ioctl(). 26885 * 26886 * Return Code: the code returned by sd_send_scsi_cmd() 26887 * EINVAL if invalid arguments are provided 26888 * EFAULT if ddi_copyxxx() fails 26889 * ENXIO if fail ddi_get_soft_state 26890 * EIO if invalid mode sense block descriptor length 26891 */ 26892 26893 static int 26894 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 26895 { 26896 struct sd_lun *un = NULL; 26897 struct mode_header *sense_mhp, *select_mhp; 26898 struct mode_speed *sense_page, *select_page; 26899 int current_speed; 26900 int rval = EINVAL; 26901 int bd_len; 26902 uchar_t *sense = NULL; 26903 uchar_t *select = NULL; 26904 sd_ssc_t *ssc; 26905 26906 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 26907 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26908 return (ENXIO); 26909 } 26910 26911 /* 26912 * Note: The drive speed is being modified here according to a Toshiba 26913 * vendor specific mode page (0x31). 26914 */ 26915 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 26916 26917 ssc = sd_ssc_init(un); 26918 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 26919 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 26920 SD_PATH_STANDARD); 26921 sd_ssc_fini(ssc); 26922 if (rval != 0) { 26923 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26924 "sr_change_speed: Mode Sense Failed\n"); 26925 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26926 return (rval); 26927 } 26928 sense_mhp = (struct mode_header *)sense; 26929 26930 /* Check the block descriptor len to handle only 1 block descriptor */ 26931 bd_len = sense_mhp->bdesc_length; 26932 if (bd_len > MODE_BLK_DESC_LENGTH) { 26933 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26934 "sr_change_speed: Mode Sense returned invalid block " 26935 "descriptor length\n"); 26936 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26937 return (EIO); 26938 } 26939 26940 sense_page = (struct mode_speed *) 26941 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 26942 current_speed = sense_page->speed; 26943 26944 /* Process command */ 26945 switch (cmd) { 26946 case CDROMGDRVSPEED: 26947 /* Return the drive speed obtained during the mode sense */ 26948 if (current_speed == 0x2) { 26949 current_speed = CDROM_TWELVE_SPEED; 26950 } 26951 if (ddi_copyout(¤t_speed, (void *)data, 26952 sizeof (int), flag) != 0) { 26953 rval = EFAULT; 26954 } 26955 break; 26956 case CDROMSDRVSPEED: 26957 /* Validate the requested drive speed */ 26958 switch ((uchar_t)data) { 26959 case CDROM_TWELVE_SPEED: 26960 data = 0x2; 26961 /*FALLTHROUGH*/ 26962 case CDROM_NORMAL_SPEED: 26963 case CDROM_DOUBLE_SPEED: 26964 case CDROM_QUAD_SPEED: 26965 case CDROM_MAXIMUM_SPEED: 26966 break; 26967 default: 26968 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26969 "sr_change_speed: " 26970 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 26971 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26972 return (EINVAL); 26973 } 26974 26975 /* 26976 * The current drive speed matches the requested drive speed so 26977 * there is no need to send the mode select to change the speed 26978 */ 26979 if (current_speed == data) { 26980 break; 26981 } 26982 26983 /* Build the select data for the requested drive speed */ 26984 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 26985 select_mhp = (struct mode_header *)select; 26986 select_mhp->bdesc_length = 0; 26987 select_page = 26988 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 26989 select_page = 26990 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 26991 select_page->mode_page.code = CDROM_MODE_SPEED; 26992 select_page->mode_page.length = 2; 26993 select_page->speed = (uchar_t)data; 26994 26995 /* Send the mode select for the requested block size */ 26996 ssc = sd_ssc_init(un); 26997 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 26998 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 26999 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27000 sd_ssc_fini(ssc); 27001 if (rval != 0) { 27002 /* 27003 * The mode select failed for the requested drive speed, 27004 * so reset the data for the original drive speed and 27005 * send it to the target. The error is indicated by the 27006 * return value for the failed mode select. 27007 */ 27008 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27009 "sr_drive_speed: Mode Select Failed\n"); 27010 select_page->speed = sense_page->speed; 27011 ssc = sd_ssc_init(un); 27012 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27013 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27014 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27015 sd_ssc_fini(ssc); 27016 } 27017 break; 27018 default: 27019 /* should not reach here, but check anyway */ 27020 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27021 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27022 rval = EINVAL; 27023 break; 27024 } 27025 27026 if (select) { 27027 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27028 } 27029 if (sense) { 27030 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27031 } 27032 27033 return (rval); 27034 } 27035 27036 27037 /* 27038 * Function: sr_atapi_change_speed() 27039 * 27040 * Description: This routine is the driver entry point for handling CD-ROM 27041 * drive speed ioctl requests for MMC devices that do not support 27042 * the Real Time Streaming feature (0x107). 27043 * 27044 * Note: This routine will use the SET SPEED command which may not 27045 * be supported by all devices. 27046 * 27047 * Arguments: dev- the device 'dev_t' 27048 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27049 * CDROMSDRVSPEED (set) 27050 * data- current drive speed or requested drive speed 27051 * flag- this argument is a pass through to ddi_copyxxx() directly 27052 * from the mode argument of ioctl(). 27053 * 27054 * Return Code: the code returned by sd_send_scsi_cmd() 27055 * EINVAL if invalid arguments are provided 27056 * EFAULT if ddi_copyxxx() fails 27057 * ENXIO if fail ddi_get_soft_state 27058 * EIO if invalid mode sense block descriptor length 27059 */ 27060 27061 static int 27062 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27063 { 27064 struct sd_lun *un; 27065 struct uscsi_cmd *com = NULL; 27066 struct mode_header_grp2 *sense_mhp; 27067 uchar_t *sense_page; 27068 uchar_t *sense = NULL; 27069 char cdb[CDB_GROUP5]; 27070 int bd_len; 27071 int current_speed = 0; 27072 int max_speed = 0; 27073 int rval; 27074 sd_ssc_t *ssc; 27075 27076 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27077 27078 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27079 return (ENXIO); 27080 } 27081 27082 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27083 27084 ssc = sd_ssc_init(un); 27085 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 27086 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27087 SD_PATH_STANDARD); 27088 sd_ssc_fini(ssc); 27089 if (rval != 0) { 27090 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27091 "sr_atapi_change_speed: Mode Sense Failed\n"); 27092 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27093 return (rval); 27094 } 27095 27096 /* Check the block descriptor len to handle only 1 block descriptor */ 27097 sense_mhp = (struct mode_header_grp2 *)sense; 27098 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27099 if (bd_len > MODE_BLK_DESC_LENGTH) { 27100 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27101 "sr_atapi_change_speed: Mode Sense returned invalid " 27102 "block descriptor length\n"); 27103 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27104 return (EIO); 27105 } 27106 27107 /* Calculate the current and maximum drive speeds */ 27108 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27109 current_speed = (sense_page[14] << 8) | sense_page[15]; 27110 max_speed = (sense_page[8] << 8) | sense_page[9]; 27111 27112 /* Process the command */ 27113 switch (cmd) { 27114 case CDROMGDRVSPEED: 27115 current_speed /= SD_SPEED_1X; 27116 if (ddi_copyout(¤t_speed, (void *)data, 27117 sizeof (int), flag) != 0) 27118 rval = EFAULT; 27119 break; 27120 case CDROMSDRVSPEED: 27121 /* Convert the speed code to KB/sec */ 27122 switch ((uchar_t)data) { 27123 case CDROM_NORMAL_SPEED: 27124 current_speed = SD_SPEED_1X; 27125 break; 27126 case CDROM_DOUBLE_SPEED: 27127 current_speed = 2 * SD_SPEED_1X; 27128 break; 27129 case CDROM_QUAD_SPEED: 27130 current_speed = 4 * SD_SPEED_1X; 27131 break; 27132 case CDROM_TWELVE_SPEED: 27133 current_speed = 12 * SD_SPEED_1X; 27134 break; 27135 case CDROM_MAXIMUM_SPEED: 27136 current_speed = 0xffff; 27137 break; 27138 default: 27139 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27140 "sr_atapi_change_speed: invalid drive speed %d\n", 27141 (uchar_t)data); 27142 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27143 return (EINVAL); 27144 } 27145 27146 /* Check the request against the drive's max speed. */ 27147 if (current_speed != 0xffff) { 27148 if (current_speed > max_speed) { 27149 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27150 return (EINVAL); 27151 } 27152 } 27153 27154 /* 27155 * Build and send the SET SPEED command 27156 * 27157 * Note: The SET SPEED (0xBB) command used in this routine is 27158 * obsolete per the SCSI MMC spec but still supported in the 27159 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27160 * therefore the command is still implemented in this routine. 27161 */ 27162 bzero(cdb, sizeof (cdb)); 27163 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27164 cdb[2] = (uchar_t)(current_speed >> 8); 27165 cdb[3] = (uchar_t)current_speed; 27166 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27167 com->uscsi_cdb = (caddr_t)cdb; 27168 com->uscsi_cdblen = CDB_GROUP5; 27169 com->uscsi_bufaddr = NULL; 27170 com->uscsi_buflen = 0; 27171 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27172 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 27173 break; 27174 default: 27175 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27176 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 27177 rval = EINVAL; 27178 } 27179 27180 if (sense) { 27181 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27182 } 27183 if (com) { 27184 kmem_free(com, sizeof (*com)); 27185 } 27186 return (rval); 27187 } 27188 27189 27190 /* 27191 * Function: sr_pause_resume() 27192 * 27193 * Description: This routine is the driver entry point for handling CD-ROM 27194 * pause/resume ioctl requests. This only affects the audio play 27195 * operation. 27196 * 27197 * Arguments: dev - the device 'dev_t' 27198 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 27199 * for setting the resume bit of the cdb. 27200 * 27201 * Return Code: the code returned by sd_send_scsi_cmd() 27202 * EINVAL if invalid mode specified 27203 * 27204 */ 27205 27206 static int 27207 sr_pause_resume(dev_t dev, int cmd) 27208 { 27209 struct sd_lun *un; 27210 struct uscsi_cmd *com; 27211 char cdb[CDB_GROUP1]; 27212 int rval; 27213 27214 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27215 return (ENXIO); 27216 } 27217 27218 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27219 bzero(cdb, CDB_GROUP1); 27220 cdb[0] = SCMD_PAUSE_RESUME; 27221 switch (cmd) { 27222 case CDROMRESUME: 27223 cdb[8] = 1; 27224 break; 27225 case CDROMPAUSE: 27226 cdb[8] = 0; 27227 break; 27228 default: 27229 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 27230 " Command '%x' Not Supported\n", cmd); 27231 rval = EINVAL; 27232 goto done; 27233 } 27234 27235 com->uscsi_cdb = cdb; 27236 com->uscsi_cdblen = CDB_GROUP1; 27237 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27238 27239 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27240 SD_PATH_STANDARD); 27241 27242 done: 27243 kmem_free(com, sizeof (*com)); 27244 return (rval); 27245 } 27246 27247 27248 /* 27249 * Function: sr_play_msf() 27250 * 27251 * Description: This routine is the driver entry point for handling CD-ROM 27252 * ioctl requests to output the audio signals at the specified 27253 * starting address and continue the audio play until the specified 27254 * ending address (CDROMPLAYMSF) The address is in Minute Second 27255 * Frame (MSF) format. 27256 * 27257 * Arguments: dev - the device 'dev_t' 27258 * data - pointer to user provided audio msf structure, 27259 * specifying start/end addresses. 27260 * flag - this argument is a pass through to ddi_copyxxx() 27261 * directly from the mode argument of ioctl(). 27262 * 27263 * Return Code: the code returned by sd_send_scsi_cmd() 27264 * EFAULT if ddi_copyxxx() fails 27265 * ENXIO if fail ddi_get_soft_state 27266 * EINVAL if data pointer is NULL 27267 */ 27268 27269 static int 27270 sr_play_msf(dev_t dev, caddr_t data, int flag) 27271 { 27272 struct sd_lun *un; 27273 struct uscsi_cmd *com; 27274 struct cdrom_msf msf_struct; 27275 struct cdrom_msf *msf = &msf_struct; 27276 char cdb[CDB_GROUP1]; 27277 int rval; 27278 27279 if (data == NULL) { 27280 return (EINVAL); 27281 } 27282 27283 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27284 return (ENXIO); 27285 } 27286 27287 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 27288 return (EFAULT); 27289 } 27290 27291 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27292 bzero(cdb, CDB_GROUP1); 27293 cdb[0] = SCMD_PLAYAUDIO_MSF; 27294 if (un->un_f_cfg_playmsf_bcd == TRUE) { 27295 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 27296 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 27297 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 27298 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 27299 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 27300 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 27301 } else { 27302 cdb[3] = msf->cdmsf_min0; 27303 cdb[4] = msf->cdmsf_sec0; 27304 cdb[5] = msf->cdmsf_frame0; 27305 cdb[6] = msf->cdmsf_min1; 27306 cdb[7] = msf->cdmsf_sec1; 27307 cdb[8] = msf->cdmsf_frame1; 27308 } 27309 com->uscsi_cdb = cdb; 27310 com->uscsi_cdblen = CDB_GROUP1; 27311 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27312 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27313 SD_PATH_STANDARD); 27314 kmem_free(com, sizeof (*com)); 27315 return (rval); 27316 } 27317 27318 27319 /* 27320 * Function: sr_play_trkind() 27321 * 27322 * Description: This routine is the driver entry point for handling CD-ROM 27323 * ioctl requests to output the audio signals at the specified 27324 * starting address and continue the audio play until the specified 27325 * ending address (CDROMPLAYTRKIND). The address is in Track Index 27326 * format. 27327 * 27328 * Arguments: dev - the device 'dev_t' 27329 * data - pointer to user provided audio track/index structure, 27330 * specifying start/end addresses. 27331 * flag - this argument is a pass through to ddi_copyxxx() 27332 * directly from the mode argument of ioctl(). 27333 * 27334 * Return Code: the code returned by sd_send_scsi_cmd() 27335 * EFAULT if ddi_copyxxx() fails 27336 * ENXIO if fail ddi_get_soft_state 27337 * EINVAL if data pointer is NULL 27338 */ 27339 27340 static int 27341 sr_play_trkind(dev_t dev, caddr_t data, int flag) 27342 { 27343 struct cdrom_ti ti_struct; 27344 struct cdrom_ti *ti = &ti_struct; 27345 struct uscsi_cmd *com = NULL; 27346 char cdb[CDB_GROUP1]; 27347 int rval; 27348 27349 if (data == NULL) { 27350 return (EINVAL); 27351 } 27352 27353 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 27354 return (EFAULT); 27355 } 27356 27357 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27358 bzero(cdb, CDB_GROUP1); 27359 cdb[0] = SCMD_PLAYAUDIO_TI; 27360 cdb[4] = ti->cdti_trk0; 27361 cdb[5] = ti->cdti_ind0; 27362 cdb[7] = ti->cdti_trk1; 27363 cdb[8] = ti->cdti_ind1; 27364 com->uscsi_cdb = cdb; 27365 com->uscsi_cdblen = CDB_GROUP1; 27366 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 27367 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27368 SD_PATH_STANDARD); 27369 kmem_free(com, sizeof (*com)); 27370 return (rval); 27371 } 27372 27373 27374 /* 27375 * Function: sr_read_all_subcodes() 27376 * 27377 * Description: This routine is the driver entry point for handling CD-ROM 27378 * ioctl requests to return raw subcode data while the target is 27379 * playing audio (CDROMSUBCODE). 27380 * 27381 * Arguments: dev - the device 'dev_t' 27382 * data - pointer to user provided cdrom subcode structure, 27383 * specifying the transfer length and address. 27384 * flag - this argument is a pass through to ddi_copyxxx() 27385 * directly from the mode argument of ioctl(). 27386 * 27387 * Return Code: the code returned by sd_send_scsi_cmd() 27388 * EFAULT if ddi_copyxxx() fails 27389 * ENXIO if fail ddi_get_soft_state 27390 * EINVAL if data pointer is NULL 27391 */ 27392 27393 static int 27394 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 27395 { 27396 struct sd_lun *un = NULL; 27397 struct uscsi_cmd *com = NULL; 27398 struct cdrom_subcode *subcode = NULL; 27399 int rval; 27400 size_t buflen; 27401 char cdb[CDB_GROUP5]; 27402 27403 #ifdef _MULTI_DATAMODEL 27404 /* To support ILP32 applications in an LP64 world */ 27405 struct cdrom_subcode32 cdrom_subcode32; 27406 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 27407 #endif 27408 if (data == NULL) { 27409 return (EINVAL); 27410 } 27411 27412 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27413 return (ENXIO); 27414 } 27415 27416 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 27417 27418 #ifdef _MULTI_DATAMODEL 27419 switch (ddi_model_convert_from(flag & FMODELS)) { 27420 case DDI_MODEL_ILP32: 27421 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 27422 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27423 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27424 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27425 return (EFAULT); 27426 } 27427 /* Convert the ILP32 uscsi data from the application to LP64 */ 27428 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 27429 break; 27430 case DDI_MODEL_NONE: 27431 if (ddi_copyin(data, subcode, 27432 sizeof (struct cdrom_subcode), flag)) { 27433 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27434 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27435 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27436 return (EFAULT); 27437 } 27438 break; 27439 } 27440 #else /* ! _MULTI_DATAMODEL */ 27441 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 27442 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27443 "sr_read_all_subcodes: ddi_copyin Failed\n"); 27444 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27445 return (EFAULT); 27446 } 27447 #endif /* _MULTI_DATAMODEL */ 27448 27449 /* 27450 * Since MMC-2 expects max 3 bytes for length, check if the 27451 * length input is greater than 3 bytes 27452 */ 27453 if ((subcode->cdsc_length & 0xFF000000) != 0) { 27454 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27455 "sr_read_all_subcodes: " 27456 "cdrom transfer length too large: %d (limit %d)\n", 27457 subcode->cdsc_length, 0xFFFFFF); 27458 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27459 return (EINVAL); 27460 } 27461 27462 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 27463 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27464 bzero(cdb, CDB_GROUP5); 27465 27466 if (un->un_f_mmc_cap == TRUE) { 27467 cdb[0] = (char)SCMD_READ_CD; 27468 cdb[2] = (char)0xff; 27469 cdb[3] = (char)0xff; 27470 cdb[4] = (char)0xff; 27471 cdb[5] = (char)0xff; 27472 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27473 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27474 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 27475 cdb[10] = 1; 27476 } else { 27477 /* 27478 * Note: A vendor specific command (0xDF) is being used her to 27479 * request a read of all subcodes. 27480 */ 27481 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 27482 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 27483 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 27484 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 27485 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 27486 } 27487 com->uscsi_cdb = cdb; 27488 com->uscsi_cdblen = CDB_GROUP5; 27489 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 27490 com->uscsi_buflen = buflen; 27491 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27492 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27493 SD_PATH_STANDARD); 27494 kmem_free(subcode, sizeof (struct cdrom_subcode)); 27495 kmem_free(com, sizeof (*com)); 27496 return (rval); 27497 } 27498 27499 27500 /* 27501 * Function: sr_read_subchannel() 27502 * 27503 * Description: This routine is the driver entry point for handling CD-ROM 27504 * ioctl requests to return the Q sub-channel data of the CD 27505 * current position block. (CDROMSUBCHNL) The data includes the 27506 * track number, index number, absolute CD-ROM address (LBA or MSF 27507 * format per the user) , track relative CD-ROM address (LBA or MSF 27508 * format per the user), control data and audio status. 27509 * 27510 * Arguments: dev - the device 'dev_t' 27511 * data - pointer to user provided cdrom sub-channel structure 27512 * flag - this argument is a pass through to ddi_copyxxx() 27513 * directly from the mode argument of ioctl(). 27514 * 27515 * Return Code: the code returned by sd_send_scsi_cmd() 27516 * EFAULT if ddi_copyxxx() fails 27517 * ENXIO if fail ddi_get_soft_state 27518 * EINVAL if data pointer is NULL 27519 */ 27520 27521 static int 27522 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 27523 { 27524 struct sd_lun *un; 27525 struct uscsi_cmd *com; 27526 struct cdrom_subchnl subchanel; 27527 struct cdrom_subchnl *subchnl = &subchanel; 27528 char cdb[CDB_GROUP1]; 27529 caddr_t buffer; 27530 int rval; 27531 27532 if (data == NULL) { 27533 return (EINVAL); 27534 } 27535 27536 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27537 (un->un_state == SD_STATE_OFFLINE)) { 27538 return (ENXIO); 27539 } 27540 27541 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 27542 return (EFAULT); 27543 } 27544 27545 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 27546 bzero(cdb, CDB_GROUP1); 27547 cdb[0] = SCMD_READ_SUBCHANNEL; 27548 /* Set the MSF bit based on the user requested address format */ 27549 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 27550 /* 27551 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 27552 * returned 27553 */ 27554 cdb[2] = 0x40; 27555 /* 27556 * Set byte 3 to specify the return data format. A value of 0x01 27557 * indicates that the CD-ROM current position should be returned. 27558 */ 27559 cdb[3] = 0x01; 27560 cdb[8] = 0x10; 27561 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27562 com->uscsi_cdb = cdb; 27563 com->uscsi_cdblen = CDB_GROUP1; 27564 com->uscsi_bufaddr = buffer; 27565 com->uscsi_buflen = 16; 27566 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27567 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27568 SD_PATH_STANDARD); 27569 if (rval != 0) { 27570 kmem_free(buffer, 16); 27571 kmem_free(com, sizeof (*com)); 27572 return (rval); 27573 } 27574 27575 /* Process the returned Q sub-channel data */ 27576 subchnl->cdsc_audiostatus = buffer[1]; 27577 subchnl->cdsc_adr = (buffer[5] & 0xF0) >> 4; 27578 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 27579 subchnl->cdsc_trk = buffer[6]; 27580 subchnl->cdsc_ind = buffer[7]; 27581 if (subchnl->cdsc_format & CDROM_LBA) { 27582 subchnl->cdsc_absaddr.lba = 27583 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27584 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27585 subchnl->cdsc_reladdr.lba = 27586 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 27587 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 27588 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 27589 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 27590 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 27591 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 27592 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 27593 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 27594 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 27595 } else { 27596 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 27597 subchnl->cdsc_absaddr.msf.second = buffer[10]; 27598 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 27599 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 27600 subchnl->cdsc_reladdr.msf.second = buffer[14]; 27601 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 27602 } 27603 kmem_free(buffer, 16); 27604 kmem_free(com, sizeof (*com)); 27605 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 27606 != 0) { 27607 return (EFAULT); 27608 } 27609 return (rval); 27610 } 27611 27612 27613 /* 27614 * Function: sr_read_tocentry() 27615 * 27616 * Description: This routine is the driver entry point for handling CD-ROM 27617 * ioctl requests to read from the Table of Contents (TOC) 27618 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 27619 * fields, the starting address (LBA or MSF format per the user) 27620 * and the data mode if the user specified track is a data track. 27621 * 27622 * Note: The READ HEADER (0x44) command used in this routine is 27623 * obsolete per the SCSI MMC spec but still supported in the 27624 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27625 * therefore the command is still implemented in this routine. 27626 * 27627 * Arguments: dev - the device 'dev_t' 27628 * data - pointer to user provided toc entry structure, 27629 * specifying the track # and the address format 27630 * (LBA or MSF). 27631 * flag - this argument is a pass through to ddi_copyxxx() 27632 * directly from the mode argument of ioctl(). 27633 * 27634 * Return Code: the code returned by sd_send_scsi_cmd() 27635 * EFAULT if ddi_copyxxx() fails 27636 * ENXIO if fail ddi_get_soft_state 27637 * EINVAL if data pointer is NULL 27638 */ 27639 27640 static int 27641 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 27642 { 27643 struct sd_lun *un = NULL; 27644 struct uscsi_cmd *com; 27645 struct cdrom_tocentry toc_entry; 27646 struct cdrom_tocentry *entry = &toc_entry; 27647 caddr_t buffer; 27648 int rval; 27649 char cdb[CDB_GROUP1]; 27650 27651 if (data == NULL) { 27652 return (EINVAL); 27653 } 27654 27655 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27656 (un->un_state == SD_STATE_OFFLINE)) { 27657 return (ENXIO); 27658 } 27659 27660 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 27661 return (EFAULT); 27662 } 27663 27664 /* Validate the requested track and address format */ 27665 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 27666 return (EINVAL); 27667 } 27668 27669 if (entry->cdte_track == 0) { 27670 return (EINVAL); 27671 } 27672 27673 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 27674 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27675 bzero(cdb, CDB_GROUP1); 27676 27677 cdb[0] = SCMD_READ_TOC; 27678 /* Set the MSF bit based on the user requested address format */ 27679 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 27680 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27681 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 27682 } else { 27683 cdb[6] = entry->cdte_track; 27684 } 27685 27686 /* 27687 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27688 * (4 byte TOC response header + 8 byte track descriptor) 27689 */ 27690 cdb[8] = 12; 27691 com->uscsi_cdb = cdb; 27692 com->uscsi_cdblen = CDB_GROUP1; 27693 com->uscsi_bufaddr = buffer; 27694 com->uscsi_buflen = 0x0C; 27695 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 27696 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27697 SD_PATH_STANDARD); 27698 if (rval != 0) { 27699 kmem_free(buffer, 12); 27700 kmem_free(com, sizeof (*com)); 27701 return (rval); 27702 } 27703 27704 /* Process the toc entry */ 27705 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 27706 entry->cdte_ctrl = (buffer[5] & 0x0F); 27707 if (entry->cdte_format & CDROM_LBA) { 27708 entry->cdte_addr.lba = 27709 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 27710 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 27711 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 27712 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 27713 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 27714 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 27715 /* 27716 * Send a READ TOC command using the LBA address format to get 27717 * the LBA for the track requested so it can be used in the 27718 * READ HEADER request 27719 * 27720 * Note: The MSF bit of the READ HEADER command specifies the 27721 * output format. The block address specified in that command 27722 * must be in LBA format. 27723 */ 27724 cdb[1] = 0; 27725 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27726 SD_PATH_STANDARD); 27727 if (rval != 0) { 27728 kmem_free(buffer, 12); 27729 kmem_free(com, sizeof (*com)); 27730 return (rval); 27731 } 27732 } else { 27733 entry->cdte_addr.msf.minute = buffer[9]; 27734 entry->cdte_addr.msf.second = buffer[10]; 27735 entry->cdte_addr.msf.frame = buffer[11]; 27736 /* 27737 * Send a READ TOC command using the LBA address format to get 27738 * the LBA for the track requested so it can be used in the 27739 * READ HEADER request 27740 * 27741 * Note: The MSF bit of the READ HEADER command specifies the 27742 * output format. The block address specified in that command 27743 * must be in LBA format. 27744 */ 27745 cdb[1] = 0; 27746 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27747 SD_PATH_STANDARD); 27748 if (rval != 0) { 27749 kmem_free(buffer, 12); 27750 kmem_free(com, sizeof (*com)); 27751 return (rval); 27752 } 27753 } 27754 27755 /* 27756 * Build and send the READ HEADER command to determine the data mode of 27757 * the user specified track. 27758 */ 27759 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 27760 (entry->cdte_track != CDROM_LEADOUT)) { 27761 bzero(cdb, CDB_GROUP1); 27762 cdb[0] = SCMD_READ_HEADER; 27763 cdb[2] = buffer[8]; 27764 cdb[3] = buffer[9]; 27765 cdb[4] = buffer[10]; 27766 cdb[5] = buffer[11]; 27767 cdb[8] = 0x08; 27768 com->uscsi_buflen = 0x08; 27769 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27770 SD_PATH_STANDARD); 27771 if (rval == 0) { 27772 entry->cdte_datamode = buffer[0]; 27773 } else { 27774 /* 27775 * READ HEADER command failed, since this is 27776 * obsoleted in one spec, its better to return 27777 * -1 for an invlid track so that we can still 27778 * receive the rest of the TOC data. 27779 */ 27780 entry->cdte_datamode = (uchar_t)-1; 27781 } 27782 } else { 27783 entry->cdte_datamode = (uchar_t)-1; 27784 } 27785 27786 kmem_free(buffer, 12); 27787 kmem_free(com, sizeof (*com)); 27788 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 27789 return (EFAULT); 27790 27791 return (rval); 27792 } 27793 27794 27795 /* 27796 * Function: sr_read_tochdr() 27797 * 27798 * Description: This routine is the driver entry point for handling CD-ROM 27799 * ioctl requests to read the Table of Contents (TOC) header 27800 * (CDROMREADTOHDR). The TOC header consists of the disk starting 27801 * and ending track numbers 27802 * 27803 * Arguments: dev - the device 'dev_t' 27804 * data - pointer to user provided toc header structure, 27805 * specifying the starting and ending track numbers. 27806 * flag - this argument is a pass through to ddi_copyxxx() 27807 * directly from the mode argument of ioctl(). 27808 * 27809 * Return Code: the code returned by sd_send_scsi_cmd() 27810 * EFAULT if ddi_copyxxx() fails 27811 * ENXIO if fail ddi_get_soft_state 27812 * EINVAL if data pointer is NULL 27813 */ 27814 27815 static int 27816 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 27817 { 27818 struct sd_lun *un; 27819 struct uscsi_cmd *com; 27820 struct cdrom_tochdr toc_header; 27821 struct cdrom_tochdr *hdr = &toc_header; 27822 char cdb[CDB_GROUP1]; 27823 int rval; 27824 caddr_t buffer; 27825 27826 if (data == NULL) { 27827 return (EINVAL); 27828 } 27829 27830 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27831 (un->un_state == SD_STATE_OFFLINE)) { 27832 return (ENXIO); 27833 } 27834 27835 buffer = kmem_zalloc(4, KM_SLEEP); 27836 bzero(cdb, CDB_GROUP1); 27837 cdb[0] = SCMD_READ_TOC; 27838 /* 27839 * Specifying a track number of 0x00 in the READ TOC command indicates 27840 * that the TOC header should be returned 27841 */ 27842 cdb[6] = 0x00; 27843 /* 27844 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 27845 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 27846 */ 27847 cdb[8] = 0x04; 27848 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27849 com->uscsi_cdb = cdb; 27850 com->uscsi_cdblen = CDB_GROUP1; 27851 com->uscsi_bufaddr = buffer; 27852 com->uscsi_buflen = 0x04; 27853 com->uscsi_timeout = 300; 27854 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27855 27856 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 27857 SD_PATH_STANDARD); 27858 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 27859 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 27860 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 27861 } else { 27862 hdr->cdth_trk0 = buffer[2]; 27863 hdr->cdth_trk1 = buffer[3]; 27864 } 27865 kmem_free(buffer, 4); 27866 kmem_free(com, sizeof (*com)); 27867 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 27868 return (EFAULT); 27869 } 27870 return (rval); 27871 } 27872 27873 27874 /* 27875 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 27876 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 27877 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 27878 * digital audio and extended architecture digital audio. These modes are 27879 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 27880 * MMC specs. 27881 * 27882 * In addition to support for the various data formats these routines also 27883 * include support for devices that implement only the direct access READ 27884 * commands (0x08, 0x28), devices that implement the READ_CD commands 27885 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 27886 * READ CDXA commands (0xD8, 0xDB) 27887 */ 27888 27889 /* 27890 * Function: sr_read_mode1() 27891 * 27892 * Description: This routine is the driver entry point for handling CD-ROM 27893 * ioctl read mode1 requests (CDROMREADMODE1). 27894 * 27895 * Arguments: dev - the device 'dev_t' 27896 * data - pointer to user provided cd read structure specifying 27897 * the lba buffer address and length. 27898 * flag - this argument is a pass through to ddi_copyxxx() 27899 * directly from the mode argument of ioctl(). 27900 * 27901 * Return Code: the code returned by sd_send_scsi_cmd() 27902 * EFAULT if ddi_copyxxx() fails 27903 * ENXIO if fail ddi_get_soft_state 27904 * EINVAL if data pointer is NULL 27905 */ 27906 27907 static int 27908 sr_read_mode1(dev_t dev, caddr_t data, int flag) 27909 { 27910 struct sd_lun *un; 27911 struct cdrom_read mode1_struct; 27912 struct cdrom_read *mode1 = &mode1_struct; 27913 int rval; 27914 sd_ssc_t *ssc; 27915 27916 #ifdef _MULTI_DATAMODEL 27917 /* To support ILP32 applications in an LP64 world */ 27918 struct cdrom_read32 cdrom_read32; 27919 struct cdrom_read32 *cdrd32 = &cdrom_read32; 27920 #endif /* _MULTI_DATAMODEL */ 27921 27922 if (data == NULL) { 27923 return (EINVAL); 27924 } 27925 27926 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27927 (un->un_state == SD_STATE_OFFLINE)) { 27928 return (ENXIO); 27929 } 27930 27931 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27932 "sd_read_mode1: entry: un:0x%p\n", un); 27933 27934 #ifdef _MULTI_DATAMODEL 27935 switch (ddi_model_convert_from(flag & FMODELS)) { 27936 case DDI_MODEL_ILP32: 27937 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 27938 return (EFAULT); 27939 } 27940 /* Convert the ILP32 uscsi data from the application to LP64 */ 27941 cdrom_read32tocdrom_read(cdrd32, mode1); 27942 break; 27943 case DDI_MODEL_NONE: 27944 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 27945 return (EFAULT); 27946 } 27947 } 27948 #else /* ! _MULTI_DATAMODEL */ 27949 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 27950 return (EFAULT); 27951 } 27952 #endif /* _MULTI_DATAMODEL */ 27953 27954 ssc = sd_ssc_init(un); 27955 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr, 27956 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 27957 sd_ssc_fini(ssc); 27958 27959 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27960 "sd_read_mode1: exit: un:0x%p\n", un); 27961 27962 return (rval); 27963 } 27964 27965 27966 /* 27967 * Function: sr_read_cd_mode2() 27968 * 27969 * Description: This routine is the driver entry point for handling CD-ROM 27970 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 27971 * support the READ CD (0xBE) command or the 1st generation 27972 * READ CD (0xD4) command. 27973 * 27974 * Arguments: dev - the device 'dev_t' 27975 * data - pointer to user provided cd read structure specifying 27976 * the lba buffer address and length. 27977 * flag - this argument is a pass through to ddi_copyxxx() 27978 * directly from the mode argument of ioctl(). 27979 * 27980 * Return Code: the code returned by sd_send_scsi_cmd() 27981 * EFAULT if ddi_copyxxx() fails 27982 * ENXIO if fail ddi_get_soft_state 27983 * EINVAL if data pointer is NULL 27984 */ 27985 27986 static int 27987 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 27988 { 27989 struct sd_lun *un; 27990 struct uscsi_cmd *com; 27991 struct cdrom_read mode2_struct; 27992 struct cdrom_read *mode2 = &mode2_struct; 27993 uchar_t cdb[CDB_GROUP5]; 27994 int nblocks; 27995 int rval; 27996 #ifdef _MULTI_DATAMODEL 27997 /* To support ILP32 applications in an LP64 world */ 27998 struct cdrom_read32 cdrom_read32; 27999 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28000 #endif /* _MULTI_DATAMODEL */ 28001 28002 if (data == NULL) { 28003 return (EINVAL); 28004 } 28005 28006 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28007 (un->un_state == SD_STATE_OFFLINE)) { 28008 return (ENXIO); 28009 } 28010 28011 #ifdef _MULTI_DATAMODEL 28012 switch (ddi_model_convert_from(flag & FMODELS)) { 28013 case DDI_MODEL_ILP32: 28014 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28015 return (EFAULT); 28016 } 28017 /* Convert the ILP32 uscsi data from the application to LP64 */ 28018 cdrom_read32tocdrom_read(cdrd32, mode2); 28019 break; 28020 case DDI_MODEL_NONE: 28021 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28022 return (EFAULT); 28023 } 28024 break; 28025 } 28026 28027 #else /* ! _MULTI_DATAMODEL */ 28028 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28029 return (EFAULT); 28030 } 28031 #endif /* _MULTI_DATAMODEL */ 28032 28033 bzero(cdb, sizeof (cdb)); 28034 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28035 /* Read command supported by 1st generation atapi drives */ 28036 cdb[0] = SCMD_READ_CDD4; 28037 } else { 28038 /* Universal CD Access Command */ 28039 cdb[0] = SCMD_READ_CD; 28040 } 28041 28042 /* 28043 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28044 */ 28045 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28046 28047 /* set the start address */ 28048 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28049 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28050 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28051 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28052 28053 /* set the transfer length */ 28054 nblocks = mode2->cdread_buflen / 2336; 28055 cdb[6] = (uchar_t)(nblocks >> 16); 28056 cdb[7] = (uchar_t)(nblocks >> 8); 28057 cdb[8] = (uchar_t)nblocks; 28058 28059 /* set the filter bits */ 28060 cdb[9] = CDROM_READ_CD_USERDATA; 28061 28062 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28063 com->uscsi_cdb = (caddr_t)cdb; 28064 com->uscsi_cdblen = sizeof (cdb); 28065 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28066 com->uscsi_buflen = mode2->cdread_buflen; 28067 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28068 28069 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28070 SD_PATH_STANDARD); 28071 kmem_free(com, sizeof (*com)); 28072 return (rval); 28073 } 28074 28075 28076 /* 28077 * Function: sr_read_mode2() 28078 * 28079 * Description: This routine is the driver entry point for handling CD-ROM 28080 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28081 * do not support the READ CD (0xBE) command. 28082 * 28083 * Arguments: dev - the device 'dev_t' 28084 * data - pointer to user provided cd read structure specifying 28085 * the lba buffer address and length. 28086 * flag - this argument is a pass through to ddi_copyxxx() 28087 * directly from the mode argument of ioctl(). 28088 * 28089 * Return Code: the code returned by sd_send_scsi_cmd() 28090 * EFAULT if ddi_copyxxx() fails 28091 * ENXIO if fail ddi_get_soft_state 28092 * EINVAL if data pointer is NULL 28093 * EIO if fail to reset block size 28094 * EAGAIN if commands are in progress in the driver 28095 */ 28096 28097 static int 28098 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28099 { 28100 struct sd_lun *un; 28101 struct cdrom_read mode2_struct; 28102 struct cdrom_read *mode2 = &mode2_struct; 28103 int rval; 28104 uint32_t restore_blksize; 28105 struct uscsi_cmd *com; 28106 uchar_t cdb[CDB_GROUP0]; 28107 int nblocks; 28108 28109 #ifdef _MULTI_DATAMODEL 28110 /* To support ILP32 applications in an LP64 world */ 28111 struct cdrom_read32 cdrom_read32; 28112 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28113 #endif /* _MULTI_DATAMODEL */ 28114 28115 if (data == NULL) { 28116 return (EINVAL); 28117 } 28118 28119 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28120 (un->un_state == SD_STATE_OFFLINE)) { 28121 return (ENXIO); 28122 } 28123 28124 /* 28125 * Because this routine will update the device and driver block size 28126 * being used we want to make sure there are no commands in progress. 28127 * If commands are in progress the user will have to try again. 28128 * 28129 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28130 * in sdioctl to protect commands from sdioctl through to the top of 28131 * sd_uscsi_strategy. See sdioctl for details. 28132 */ 28133 mutex_enter(SD_MUTEX(un)); 28134 if (un->un_ncmds_in_driver != 1) { 28135 mutex_exit(SD_MUTEX(un)); 28136 return (EAGAIN); 28137 } 28138 mutex_exit(SD_MUTEX(un)); 28139 28140 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28141 "sd_read_mode2: entry: un:0x%p\n", un); 28142 28143 #ifdef _MULTI_DATAMODEL 28144 switch (ddi_model_convert_from(flag & FMODELS)) { 28145 case DDI_MODEL_ILP32: 28146 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28147 return (EFAULT); 28148 } 28149 /* Convert the ILP32 uscsi data from the application to LP64 */ 28150 cdrom_read32tocdrom_read(cdrd32, mode2); 28151 break; 28152 case DDI_MODEL_NONE: 28153 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28154 return (EFAULT); 28155 } 28156 break; 28157 } 28158 #else /* ! _MULTI_DATAMODEL */ 28159 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28160 return (EFAULT); 28161 } 28162 #endif /* _MULTI_DATAMODEL */ 28163 28164 /* Store the current target block size for restoration later */ 28165 restore_blksize = un->un_tgt_blocksize; 28166 28167 /* Change the device and soft state target block size to 2336 */ 28168 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28169 rval = EIO; 28170 goto done; 28171 } 28172 28173 28174 bzero(cdb, sizeof (cdb)); 28175 28176 /* set READ operation */ 28177 cdb[0] = SCMD_READ; 28178 28179 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 28180 mode2->cdread_lba >>= 2; 28181 28182 /* set the start address */ 28183 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 28184 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28185 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 28186 28187 /* set the transfer length */ 28188 nblocks = mode2->cdread_buflen / 2336; 28189 cdb[4] = (uchar_t)nblocks & 0xFF; 28190 28191 /* build command */ 28192 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28193 com->uscsi_cdb = (caddr_t)cdb; 28194 com->uscsi_cdblen = sizeof (cdb); 28195 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28196 com->uscsi_buflen = mode2->cdread_buflen; 28197 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28198 28199 /* 28200 * Issue SCSI command with user space address for read buffer. 28201 * 28202 * This sends the command through main channel in the driver. 28203 * 28204 * Since this is accessed via an IOCTL call, we go through the 28205 * standard path, so that if the device was powered down, then 28206 * it would be 'awakened' to handle the command. 28207 */ 28208 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28209 SD_PATH_STANDARD); 28210 28211 kmem_free(com, sizeof (*com)); 28212 28213 /* Restore the device and soft state target block size */ 28214 if (sr_sector_mode(dev, restore_blksize) != 0) { 28215 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28216 "can't do switch back to mode 1\n"); 28217 /* 28218 * If sd_send_scsi_READ succeeded we still need to report 28219 * an error because we failed to reset the block size 28220 */ 28221 if (rval == 0) { 28222 rval = EIO; 28223 } 28224 } 28225 28226 done: 28227 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28228 "sd_read_mode2: exit: un:0x%p\n", un); 28229 28230 return (rval); 28231 } 28232 28233 28234 /* 28235 * Function: sr_sector_mode() 28236 * 28237 * Description: This utility function is used by sr_read_mode2 to set the target 28238 * block size based on the user specified size. This is a legacy 28239 * implementation based upon a vendor specific mode page 28240 * 28241 * Arguments: dev - the device 'dev_t' 28242 * data - flag indicating if block size is being set to 2336 or 28243 * 512. 28244 * 28245 * Return Code: the code returned by sd_send_scsi_cmd() 28246 * EFAULT if ddi_copyxxx() fails 28247 * ENXIO if fail ddi_get_soft_state 28248 * EINVAL if data pointer is NULL 28249 */ 28250 28251 static int 28252 sr_sector_mode(dev_t dev, uint32_t blksize) 28253 { 28254 struct sd_lun *un; 28255 uchar_t *sense; 28256 uchar_t *select; 28257 int rval; 28258 sd_ssc_t *ssc; 28259 28260 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28261 (un->un_state == SD_STATE_OFFLINE)) { 28262 return (ENXIO); 28263 } 28264 28265 sense = kmem_zalloc(20, KM_SLEEP); 28266 28267 /* Note: This is a vendor specific mode page (0x81) */ 28268 ssc = sd_ssc_init(un); 28269 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81, 28270 SD_PATH_STANDARD); 28271 sd_ssc_fini(ssc); 28272 if (rval != 0) { 28273 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28274 "sr_sector_mode: Mode Sense failed\n"); 28275 kmem_free(sense, 20); 28276 return (rval); 28277 } 28278 select = kmem_zalloc(20, KM_SLEEP); 28279 select[3] = 0x08; 28280 select[10] = ((blksize >> 8) & 0xff); 28281 select[11] = (blksize & 0xff); 28282 select[12] = 0x01; 28283 select[13] = 0x06; 28284 select[14] = sense[14]; 28285 select[15] = sense[15]; 28286 if (blksize == SD_MODE2_BLKSIZE) { 28287 select[14] |= 0x01; 28288 } 28289 28290 ssc = sd_ssc_init(un); 28291 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20, 28292 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28293 sd_ssc_fini(ssc); 28294 if (rval != 0) { 28295 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28296 "sr_sector_mode: Mode Select failed\n"); 28297 } else { 28298 /* 28299 * Only update the softstate block size if we successfully 28300 * changed the device block mode. 28301 */ 28302 mutex_enter(SD_MUTEX(un)); 28303 sd_update_block_info(un, blksize, 0); 28304 mutex_exit(SD_MUTEX(un)); 28305 } 28306 kmem_free(sense, 20); 28307 kmem_free(select, 20); 28308 return (rval); 28309 } 28310 28311 28312 /* 28313 * Function: sr_read_cdda() 28314 * 28315 * Description: This routine is the driver entry point for handling CD-ROM 28316 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 28317 * the target supports CDDA these requests are handled via a vendor 28318 * specific command (0xD8) If the target does not support CDDA 28319 * these requests are handled via the READ CD command (0xBE). 28320 * 28321 * Arguments: dev - the device 'dev_t' 28322 * data - pointer to user provided CD-DA structure specifying 28323 * the track starting address, transfer length, and 28324 * subcode options. 28325 * flag - this argument is a pass through to ddi_copyxxx() 28326 * directly from the mode argument of ioctl(). 28327 * 28328 * Return Code: the code returned by sd_send_scsi_cmd() 28329 * EFAULT if ddi_copyxxx() fails 28330 * ENXIO if fail ddi_get_soft_state 28331 * EINVAL if invalid arguments are provided 28332 * ENOTTY 28333 */ 28334 28335 static int 28336 sr_read_cdda(dev_t dev, caddr_t data, int flag) 28337 { 28338 struct sd_lun *un; 28339 struct uscsi_cmd *com; 28340 struct cdrom_cdda *cdda; 28341 int rval; 28342 size_t buflen; 28343 char cdb[CDB_GROUP5]; 28344 28345 #ifdef _MULTI_DATAMODEL 28346 /* To support ILP32 applications in an LP64 world */ 28347 struct cdrom_cdda32 cdrom_cdda32; 28348 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 28349 #endif /* _MULTI_DATAMODEL */ 28350 28351 if (data == NULL) { 28352 return (EINVAL); 28353 } 28354 28355 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28356 return (ENXIO); 28357 } 28358 28359 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 28360 28361 #ifdef _MULTI_DATAMODEL 28362 switch (ddi_model_convert_from(flag & FMODELS)) { 28363 case DDI_MODEL_ILP32: 28364 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 28365 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28366 "sr_read_cdda: ddi_copyin Failed\n"); 28367 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28368 return (EFAULT); 28369 } 28370 /* Convert the ILP32 uscsi data from the application to LP64 */ 28371 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 28372 break; 28373 case DDI_MODEL_NONE: 28374 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28375 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28376 "sr_read_cdda: ddi_copyin Failed\n"); 28377 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28378 return (EFAULT); 28379 } 28380 break; 28381 } 28382 #else /* ! _MULTI_DATAMODEL */ 28383 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 28384 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28385 "sr_read_cdda: ddi_copyin Failed\n"); 28386 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28387 return (EFAULT); 28388 } 28389 #endif /* _MULTI_DATAMODEL */ 28390 28391 /* 28392 * Since MMC-2 expects max 3 bytes for length, check if the 28393 * length input is greater than 3 bytes 28394 */ 28395 if ((cdda->cdda_length & 0xFF000000) != 0) { 28396 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 28397 "cdrom transfer length too large: %d (limit %d)\n", 28398 cdda->cdda_length, 0xFFFFFF); 28399 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28400 return (EINVAL); 28401 } 28402 28403 switch (cdda->cdda_subcode) { 28404 case CDROM_DA_NO_SUBCODE: 28405 buflen = CDROM_BLK_2352 * cdda->cdda_length; 28406 break; 28407 case CDROM_DA_SUBQ: 28408 buflen = CDROM_BLK_2368 * cdda->cdda_length; 28409 break; 28410 case CDROM_DA_ALL_SUBCODE: 28411 buflen = CDROM_BLK_2448 * cdda->cdda_length; 28412 break; 28413 case CDROM_DA_SUBCODE_ONLY: 28414 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 28415 break; 28416 default: 28417 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28418 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 28419 cdda->cdda_subcode); 28420 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28421 return (EINVAL); 28422 } 28423 28424 /* Build and send the command */ 28425 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28426 bzero(cdb, CDB_GROUP5); 28427 28428 if (un->un_f_cfg_cdda == TRUE) { 28429 cdb[0] = (char)SCMD_READ_CD; 28430 cdb[1] = 0x04; 28431 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28432 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28433 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28434 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28435 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28436 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28437 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 28438 cdb[9] = 0x10; 28439 switch (cdda->cdda_subcode) { 28440 case CDROM_DA_NO_SUBCODE : 28441 cdb[10] = 0x0; 28442 break; 28443 case CDROM_DA_SUBQ : 28444 cdb[10] = 0x2; 28445 break; 28446 case CDROM_DA_ALL_SUBCODE : 28447 cdb[10] = 0x1; 28448 break; 28449 case CDROM_DA_SUBCODE_ONLY : 28450 /* FALLTHROUGH */ 28451 default : 28452 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28453 kmem_free(com, sizeof (*com)); 28454 return (ENOTTY); 28455 } 28456 } else { 28457 cdb[0] = (char)SCMD_READ_CDDA; 28458 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 28459 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 28460 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 28461 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 28462 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 28463 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 28464 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 28465 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 28466 cdb[10] = cdda->cdda_subcode; 28467 } 28468 28469 com->uscsi_cdb = cdb; 28470 com->uscsi_cdblen = CDB_GROUP5; 28471 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 28472 com->uscsi_buflen = buflen; 28473 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28474 28475 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28476 SD_PATH_STANDARD); 28477 28478 kmem_free(cdda, sizeof (struct cdrom_cdda)); 28479 kmem_free(com, sizeof (*com)); 28480 return (rval); 28481 } 28482 28483 28484 /* 28485 * Function: sr_read_cdxa() 28486 * 28487 * Description: This routine is the driver entry point for handling CD-ROM 28488 * ioctl requests to return CD-XA (Extended Architecture) data. 28489 * (CDROMCDXA). 28490 * 28491 * Arguments: dev - the device 'dev_t' 28492 * data - pointer to user provided CD-XA structure specifying 28493 * the data starting address, transfer length, and format 28494 * flag - this argument is a pass through to ddi_copyxxx() 28495 * directly from the mode argument of ioctl(). 28496 * 28497 * Return Code: the code returned by sd_send_scsi_cmd() 28498 * EFAULT if ddi_copyxxx() fails 28499 * ENXIO if fail ddi_get_soft_state 28500 * EINVAL if data pointer is NULL 28501 */ 28502 28503 static int 28504 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 28505 { 28506 struct sd_lun *un; 28507 struct uscsi_cmd *com; 28508 struct cdrom_cdxa *cdxa; 28509 int rval; 28510 size_t buflen; 28511 char cdb[CDB_GROUP5]; 28512 uchar_t read_flags; 28513 28514 #ifdef _MULTI_DATAMODEL 28515 /* To support ILP32 applications in an LP64 world */ 28516 struct cdrom_cdxa32 cdrom_cdxa32; 28517 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 28518 #endif /* _MULTI_DATAMODEL */ 28519 28520 if (data == NULL) { 28521 return (EINVAL); 28522 } 28523 28524 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28525 return (ENXIO); 28526 } 28527 28528 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 28529 28530 #ifdef _MULTI_DATAMODEL 28531 switch (ddi_model_convert_from(flag & FMODELS)) { 28532 case DDI_MODEL_ILP32: 28533 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 28534 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28535 return (EFAULT); 28536 } 28537 /* 28538 * Convert the ILP32 uscsi data from the 28539 * application to LP64 for internal use. 28540 */ 28541 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 28542 break; 28543 case DDI_MODEL_NONE: 28544 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28545 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28546 return (EFAULT); 28547 } 28548 break; 28549 } 28550 #else /* ! _MULTI_DATAMODEL */ 28551 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 28552 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28553 return (EFAULT); 28554 } 28555 #endif /* _MULTI_DATAMODEL */ 28556 28557 /* 28558 * Since MMC-2 expects max 3 bytes for length, check if the 28559 * length input is greater than 3 bytes 28560 */ 28561 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 28562 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 28563 "cdrom transfer length too large: %d (limit %d)\n", 28564 cdxa->cdxa_length, 0xFFFFFF); 28565 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28566 return (EINVAL); 28567 } 28568 28569 switch (cdxa->cdxa_format) { 28570 case CDROM_XA_DATA: 28571 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 28572 read_flags = 0x10; 28573 break; 28574 case CDROM_XA_SECTOR_DATA: 28575 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 28576 read_flags = 0xf8; 28577 break; 28578 case CDROM_XA_DATA_W_ERROR: 28579 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 28580 read_flags = 0xfc; 28581 break; 28582 default: 28583 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28584 "sr_read_cdxa: Format '0x%x' Not Supported\n", 28585 cdxa->cdxa_format); 28586 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28587 return (EINVAL); 28588 } 28589 28590 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28591 bzero(cdb, CDB_GROUP5); 28592 if (un->un_f_mmc_cap == TRUE) { 28593 cdb[0] = (char)SCMD_READ_CD; 28594 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28595 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28596 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28597 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28598 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28599 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28600 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 28601 cdb[9] = (char)read_flags; 28602 } else { 28603 /* 28604 * Note: A vendor specific command (0xDB) is being used her to 28605 * request a read of all subcodes. 28606 */ 28607 cdb[0] = (char)SCMD_READ_CDXA; 28608 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 28609 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 28610 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 28611 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 28612 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 28613 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 28614 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 28615 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 28616 cdb[10] = cdxa->cdxa_format; 28617 } 28618 com->uscsi_cdb = cdb; 28619 com->uscsi_cdblen = CDB_GROUP5; 28620 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 28621 com->uscsi_buflen = buflen; 28622 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28623 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 28624 SD_PATH_STANDARD); 28625 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 28626 kmem_free(com, sizeof (*com)); 28627 return (rval); 28628 } 28629 28630 28631 /* 28632 * Function: sr_eject() 28633 * 28634 * Description: This routine is the driver entry point for handling CD-ROM 28635 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 28636 * 28637 * Arguments: dev - the device 'dev_t' 28638 * 28639 * Return Code: the code returned by sd_send_scsi_cmd() 28640 */ 28641 28642 static int 28643 sr_eject(dev_t dev) 28644 { 28645 struct sd_lun *un; 28646 int rval; 28647 sd_ssc_t *ssc; 28648 28649 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28650 (un->un_state == SD_STATE_OFFLINE)) { 28651 return (ENXIO); 28652 } 28653 28654 /* 28655 * To prevent race conditions with the eject 28656 * command, keep track of an eject command as 28657 * it progresses. If we are already handling 28658 * an eject command in the driver for the given 28659 * unit and another request to eject is received 28660 * immediately return EAGAIN so we don't lose 28661 * the command if the current eject command fails. 28662 */ 28663 mutex_enter(SD_MUTEX(un)); 28664 if (un->un_f_ejecting == TRUE) { 28665 mutex_exit(SD_MUTEX(un)); 28666 return (EAGAIN); 28667 } 28668 un->un_f_ejecting = TRUE; 28669 mutex_exit(SD_MUTEX(un)); 28670 28671 ssc = sd_ssc_init(un); 28672 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 28673 SD_PATH_STANDARD); 28674 sd_ssc_fini(ssc); 28675 28676 if (rval != 0) { 28677 mutex_enter(SD_MUTEX(un)); 28678 un->un_f_ejecting = FALSE; 28679 mutex_exit(SD_MUTEX(un)); 28680 return (rval); 28681 } 28682 28683 ssc = sd_ssc_init(un); 28684 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP, 28685 SD_TARGET_EJECT, SD_PATH_STANDARD); 28686 sd_ssc_fini(ssc); 28687 28688 if (rval == 0) { 28689 mutex_enter(SD_MUTEX(un)); 28690 sr_ejected(un); 28691 un->un_mediastate = DKIO_EJECTED; 28692 un->un_f_ejecting = FALSE; 28693 cv_broadcast(&un->un_state_cv); 28694 mutex_exit(SD_MUTEX(un)); 28695 } else { 28696 mutex_enter(SD_MUTEX(un)); 28697 un->un_f_ejecting = FALSE; 28698 mutex_exit(SD_MUTEX(un)); 28699 } 28700 return (rval); 28701 } 28702 28703 28704 /* 28705 * Function: sr_ejected() 28706 * 28707 * Description: This routine updates the soft state structure to invalidate the 28708 * geometry information after the media has been ejected or a 28709 * media eject has been detected. 28710 * 28711 * Arguments: un - driver soft state (unit) structure 28712 */ 28713 28714 static void 28715 sr_ejected(struct sd_lun *un) 28716 { 28717 struct sd_errstats *stp; 28718 28719 ASSERT(un != NULL); 28720 ASSERT(mutex_owned(SD_MUTEX(un))); 28721 28722 un->un_f_blockcount_is_valid = FALSE; 28723 un->un_f_tgt_blocksize_is_valid = FALSE; 28724 mutex_exit(SD_MUTEX(un)); 28725 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 28726 mutex_enter(SD_MUTEX(un)); 28727 28728 if (un->un_errstats != NULL) { 28729 stp = (struct sd_errstats *)un->un_errstats->ks_data; 28730 stp->sd_capacity.value.ui64 = 0; 28731 } 28732 } 28733 28734 28735 /* 28736 * Function: sr_check_wp() 28737 * 28738 * Description: This routine checks the write protection of a removable 28739 * media disk and hotpluggable devices via the write protect bit of 28740 * the Mode Page Header device specific field. Some devices choke 28741 * on unsupported mode page. In order to workaround this issue, 28742 * this routine has been implemented to use 0x3f mode page(request 28743 * for all pages) for all device types. 28744 * 28745 * Arguments: dev - the device 'dev_t' 28746 * 28747 * Return Code: int indicating if the device is write protected (1) or not (0) 28748 * 28749 * Context: Kernel thread. 28750 * 28751 */ 28752 28753 static int 28754 sr_check_wp(dev_t dev) 28755 { 28756 struct sd_lun *un; 28757 uchar_t device_specific; 28758 uchar_t *sense; 28759 int hdrlen; 28760 int rval = FALSE; 28761 int status; 28762 sd_ssc_t *ssc; 28763 28764 /* 28765 * Note: The return codes for this routine should be reworked to 28766 * properly handle the case of a NULL softstate. 28767 */ 28768 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28769 return (FALSE); 28770 } 28771 28772 if (un->un_f_cfg_is_atapi == TRUE) { 28773 /* 28774 * The mode page contents are not required; set the allocation 28775 * length for the mode page header only 28776 */ 28777 hdrlen = MODE_HEADER_LENGTH_GRP2; 28778 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28779 ssc = sd_ssc_init(un); 28780 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen, 28781 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28782 sd_ssc_fini(ssc); 28783 if (status != 0) 28784 goto err_exit; 28785 device_specific = 28786 ((struct mode_header_grp2 *)sense)->device_specific; 28787 } else { 28788 hdrlen = MODE_HEADER_LENGTH; 28789 sense = kmem_zalloc(hdrlen, KM_SLEEP); 28790 ssc = sd_ssc_init(un); 28791 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen, 28792 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 28793 sd_ssc_fini(ssc); 28794 if (status != 0) 28795 goto err_exit; 28796 device_specific = 28797 ((struct mode_header *)sense)->device_specific; 28798 } 28799 28800 28801 /* 28802 * Write protect mode sense failed; not all disks 28803 * understand this query. Return FALSE assuming that 28804 * these devices are not writable. 28805 */ 28806 if (device_specific & WRITE_PROTECT) { 28807 rval = TRUE; 28808 } 28809 28810 err_exit: 28811 kmem_free(sense, hdrlen); 28812 return (rval); 28813 } 28814 28815 /* 28816 * Function: sr_volume_ctrl() 28817 * 28818 * Description: This routine is the driver entry point for handling CD-ROM 28819 * audio output volume ioctl requests. (CDROMVOLCTRL) 28820 * 28821 * Arguments: dev - the device 'dev_t' 28822 * data - pointer to user audio volume control structure 28823 * flag - this argument is a pass through to ddi_copyxxx() 28824 * directly from the mode argument of ioctl(). 28825 * 28826 * Return Code: the code returned by sd_send_scsi_cmd() 28827 * EFAULT if ddi_copyxxx() fails 28828 * ENXIO if fail ddi_get_soft_state 28829 * EINVAL if data pointer is NULL 28830 * 28831 */ 28832 28833 static int 28834 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 28835 { 28836 struct sd_lun *un; 28837 struct cdrom_volctrl volume; 28838 struct cdrom_volctrl *vol = &volume; 28839 uchar_t *sense_page; 28840 uchar_t *select_page; 28841 uchar_t *sense; 28842 uchar_t *select; 28843 int sense_buflen; 28844 int select_buflen; 28845 int rval; 28846 sd_ssc_t *ssc; 28847 28848 if (data == NULL) { 28849 return (EINVAL); 28850 } 28851 28852 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28853 (un->un_state == SD_STATE_OFFLINE)) { 28854 return (ENXIO); 28855 } 28856 28857 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 28858 return (EFAULT); 28859 } 28860 28861 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28862 struct mode_header_grp2 *sense_mhp; 28863 struct mode_header_grp2 *select_mhp; 28864 int bd_len; 28865 28866 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 28867 select_buflen = MODE_HEADER_LENGTH_GRP2 + 28868 MODEPAGE_AUDIO_CTRL_LEN; 28869 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28870 select = kmem_zalloc(select_buflen, KM_SLEEP); 28871 ssc = sd_ssc_init(un); 28872 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 28873 sense_buflen, MODEPAGE_AUDIO_CTRL, 28874 SD_PATH_STANDARD); 28875 sd_ssc_fini(ssc); 28876 28877 if (rval != 0) { 28878 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 28879 "sr_volume_ctrl: Mode Sense Failed\n"); 28880 kmem_free(sense, sense_buflen); 28881 kmem_free(select, select_buflen); 28882 return (rval); 28883 } 28884 sense_mhp = (struct mode_header_grp2 *)sense; 28885 select_mhp = (struct mode_header_grp2 *)select; 28886 bd_len = (sense_mhp->bdesc_length_hi << 8) | 28887 sense_mhp->bdesc_length_lo; 28888 if (bd_len > MODE_BLK_DESC_LENGTH) { 28889 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28890 "sr_volume_ctrl: Mode Sense returned invalid " 28891 "block descriptor length\n"); 28892 kmem_free(sense, sense_buflen); 28893 kmem_free(select, select_buflen); 28894 return (EIO); 28895 } 28896 sense_page = (uchar_t *) 28897 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 28898 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 28899 select_mhp->length_msb = 0; 28900 select_mhp->length_lsb = 0; 28901 select_mhp->bdesc_length_hi = 0; 28902 select_mhp->bdesc_length_lo = 0; 28903 } else { 28904 struct mode_header *sense_mhp, *select_mhp; 28905 28906 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28907 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 28908 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 28909 select = kmem_zalloc(select_buflen, KM_SLEEP); 28910 ssc = sd_ssc_init(un); 28911 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 28912 sense_buflen, MODEPAGE_AUDIO_CTRL, 28913 SD_PATH_STANDARD); 28914 sd_ssc_fini(ssc); 28915 28916 if (rval != 0) { 28917 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28918 "sr_volume_ctrl: Mode Sense Failed\n"); 28919 kmem_free(sense, sense_buflen); 28920 kmem_free(select, select_buflen); 28921 return (rval); 28922 } 28923 sense_mhp = (struct mode_header *)sense; 28924 select_mhp = (struct mode_header *)select; 28925 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 28926 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28927 "sr_volume_ctrl: Mode Sense returned invalid " 28928 "block descriptor length\n"); 28929 kmem_free(sense, sense_buflen); 28930 kmem_free(select, select_buflen); 28931 return (EIO); 28932 } 28933 sense_page = (uchar_t *) 28934 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 28935 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 28936 select_mhp->length = 0; 28937 select_mhp->bdesc_length = 0; 28938 } 28939 /* 28940 * Note: An audio control data structure could be created and overlayed 28941 * on the following in place of the array indexing method implemented. 28942 */ 28943 28944 /* Build the select data for the user volume data */ 28945 select_page[0] = MODEPAGE_AUDIO_CTRL; 28946 select_page[1] = 0xE; 28947 /* Set the immediate bit */ 28948 select_page[2] = 0x04; 28949 /* Zero out reserved fields */ 28950 select_page[3] = 0x00; 28951 select_page[4] = 0x00; 28952 /* Return sense data for fields not to be modified */ 28953 select_page[5] = sense_page[5]; 28954 select_page[6] = sense_page[6]; 28955 select_page[7] = sense_page[7]; 28956 /* Set the user specified volume levels for channel 0 and 1 */ 28957 select_page[8] = 0x01; 28958 select_page[9] = vol->channel0; 28959 select_page[10] = 0x02; 28960 select_page[11] = vol->channel1; 28961 /* Channel 2 and 3 are currently unsupported so return the sense data */ 28962 select_page[12] = sense_page[12]; 28963 select_page[13] = sense_page[13]; 28964 select_page[14] = sense_page[14]; 28965 select_page[15] = sense_page[15]; 28966 28967 ssc = sd_ssc_init(un); 28968 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 28969 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select, 28970 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28971 } else { 28972 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 28973 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 28974 } 28975 sd_ssc_fini(ssc); 28976 28977 kmem_free(sense, sense_buflen); 28978 kmem_free(select, select_buflen); 28979 return (rval); 28980 } 28981 28982 28983 /* 28984 * Function: sr_read_sony_session_offset() 28985 * 28986 * Description: This routine is the driver entry point for handling CD-ROM 28987 * ioctl requests for session offset information. (CDROMREADOFFSET) 28988 * The address of the first track in the last session of a 28989 * multi-session CD-ROM is returned 28990 * 28991 * Note: This routine uses a vendor specific key value in the 28992 * command control field without implementing any vendor check here 28993 * or in the ioctl routine. 28994 * 28995 * Arguments: dev - the device 'dev_t' 28996 * data - pointer to an int to hold the requested address 28997 * flag - this argument is a pass through to ddi_copyxxx() 28998 * directly from the mode argument of ioctl(). 28999 * 29000 * Return Code: the code returned by sd_send_scsi_cmd() 29001 * EFAULT if ddi_copyxxx() fails 29002 * ENXIO if fail ddi_get_soft_state 29003 * EINVAL if data pointer is NULL 29004 */ 29005 29006 static int 29007 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29008 { 29009 struct sd_lun *un; 29010 struct uscsi_cmd *com; 29011 caddr_t buffer; 29012 char cdb[CDB_GROUP1]; 29013 int session_offset = 0; 29014 int rval; 29015 29016 if (data == NULL) { 29017 return (EINVAL); 29018 } 29019 29020 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29021 (un->un_state == SD_STATE_OFFLINE)) { 29022 return (ENXIO); 29023 } 29024 29025 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29026 bzero(cdb, CDB_GROUP1); 29027 cdb[0] = SCMD_READ_TOC; 29028 /* 29029 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29030 * (4 byte TOC response header + 8 byte response data) 29031 */ 29032 cdb[8] = SONY_SESSION_OFFSET_LEN; 29033 /* Byte 9 is the control byte. A vendor specific value is used */ 29034 cdb[9] = SONY_SESSION_OFFSET_KEY; 29035 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29036 com->uscsi_cdb = cdb; 29037 com->uscsi_cdblen = CDB_GROUP1; 29038 com->uscsi_bufaddr = buffer; 29039 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29040 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29041 29042 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 29043 SD_PATH_STANDARD); 29044 if (rval != 0) { 29045 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29046 kmem_free(com, sizeof (*com)); 29047 return (rval); 29048 } 29049 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29050 session_offset = 29051 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29052 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29053 /* 29054 * Offset returned offset in current lbasize block's. Convert to 29055 * 2k block's to return to the user 29056 */ 29057 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29058 session_offset >>= 2; 29059 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29060 session_offset >>= 1; 29061 } 29062 } 29063 29064 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29065 rval = EFAULT; 29066 } 29067 29068 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29069 kmem_free(com, sizeof (*com)); 29070 return (rval); 29071 } 29072 29073 29074 /* 29075 * Function: sd_wm_cache_constructor() 29076 * 29077 * Description: Cache Constructor for the wmap cache for the read/modify/write 29078 * devices. 29079 * 29080 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29081 * un - sd_lun structure for the device. 29082 * flag - the km flags passed to constructor 29083 * 29084 * Return Code: 0 on success. 29085 * -1 on failure. 29086 */ 29087 29088 /*ARGSUSED*/ 29089 static int 29090 sd_wm_cache_constructor(void *wm, void *un, int flags) 29091 { 29092 bzero(wm, sizeof (struct sd_w_map)); 29093 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29094 return (0); 29095 } 29096 29097 29098 /* 29099 * Function: sd_wm_cache_destructor() 29100 * 29101 * Description: Cache destructor for the wmap cache for the read/modify/write 29102 * devices. 29103 * 29104 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29105 * un - sd_lun structure for the device. 29106 */ 29107 /*ARGSUSED*/ 29108 static void 29109 sd_wm_cache_destructor(void *wm, void *un) 29110 { 29111 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29112 } 29113 29114 29115 /* 29116 * Function: sd_range_lock() 29117 * 29118 * Description: Lock the range of blocks specified as parameter to ensure 29119 * that read, modify write is atomic and no other i/o writes 29120 * to the same location. The range is specified in terms 29121 * of start and end blocks. Block numbers are the actual 29122 * media block numbers and not system. 29123 * 29124 * Arguments: un - sd_lun structure for the device. 29125 * startb - The starting block number 29126 * endb - The end block number 29127 * typ - type of i/o - simple/read_modify_write 29128 * 29129 * Return Code: wm - pointer to the wmap structure. 29130 * 29131 * Context: This routine can sleep. 29132 */ 29133 29134 static struct sd_w_map * 29135 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29136 { 29137 struct sd_w_map *wmp = NULL; 29138 struct sd_w_map *sl_wmp = NULL; 29139 struct sd_w_map *tmp_wmp; 29140 wm_state state = SD_WM_CHK_LIST; 29141 29142 29143 ASSERT(un != NULL); 29144 ASSERT(!mutex_owned(SD_MUTEX(un))); 29145 29146 mutex_enter(SD_MUTEX(un)); 29147 29148 while (state != SD_WM_DONE) { 29149 29150 switch (state) { 29151 case SD_WM_CHK_LIST: 29152 /* 29153 * This is the starting state. Check the wmap list 29154 * to see if the range is currently available. 29155 */ 29156 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29157 /* 29158 * If this is a simple write and no rmw 29159 * i/o is pending then try to lock the 29160 * range as the range should be available. 29161 */ 29162 state = SD_WM_LOCK_RANGE; 29163 } else { 29164 tmp_wmp = sd_get_range(un, startb, endb); 29165 if (tmp_wmp != NULL) { 29166 if ((wmp != NULL) && ONLIST(un, wmp)) { 29167 /* 29168 * Should not keep onlist wmps 29169 * while waiting this macro 29170 * will also do wmp = NULL; 29171 */ 29172 FREE_ONLIST_WMAP(un, wmp); 29173 } 29174 /* 29175 * sl_wmp is the wmap on which wait 29176 * is done, since the tmp_wmp points 29177 * to the inuse wmap, set sl_wmp to 29178 * tmp_wmp and change the state to sleep 29179 */ 29180 sl_wmp = tmp_wmp; 29181 state = SD_WM_WAIT_MAP; 29182 } else { 29183 state = SD_WM_LOCK_RANGE; 29184 } 29185 29186 } 29187 break; 29188 29189 case SD_WM_LOCK_RANGE: 29190 ASSERT(un->un_wm_cache); 29191 /* 29192 * The range need to be locked, try to get a wmap. 29193 * First attempt it with NO_SLEEP, want to avoid a sleep 29194 * if possible as we will have to release the sd mutex 29195 * if we have to sleep. 29196 */ 29197 if (wmp == NULL) 29198 wmp = kmem_cache_alloc(un->un_wm_cache, 29199 KM_NOSLEEP); 29200 if (wmp == NULL) { 29201 mutex_exit(SD_MUTEX(un)); 29202 _NOTE(DATA_READABLE_WITHOUT_LOCK 29203 (sd_lun::un_wm_cache)) 29204 wmp = kmem_cache_alloc(un->un_wm_cache, 29205 KM_SLEEP); 29206 mutex_enter(SD_MUTEX(un)); 29207 /* 29208 * we released the mutex so recheck and go to 29209 * check list state. 29210 */ 29211 state = SD_WM_CHK_LIST; 29212 } else { 29213 /* 29214 * We exit out of state machine since we 29215 * have the wmap. Do the housekeeping first. 29216 * place the wmap on the wmap list if it is not 29217 * on it already and then set the state to done. 29218 */ 29219 wmp->wm_start = startb; 29220 wmp->wm_end = endb; 29221 wmp->wm_flags = typ | SD_WM_BUSY; 29222 if (typ & SD_WTYPE_RMW) { 29223 un->un_rmw_count++; 29224 } 29225 /* 29226 * If not already on the list then link 29227 */ 29228 if (!ONLIST(un, wmp)) { 29229 wmp->wm_next = un->un_wm; 29230 wmp->wm_prev = NULL; 29231 if (wmp->wm_next) 29232 wmp->wm_next->wm_prev = wmp; 29233 un->un_wm = wmp; 29234 } 29235 state = SD_WM_DONE; 29236 } 29237 break; 29238 29239 case SD_WM_WAIT_MAP: 29240 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 29241 /* 29242 * Wait is done on sl_wmp, which is set in the 29243 * check_list state. 29244 */ 29245 sl_wmp->wm_wanted_count++; 29246 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 29247 sl_wmp->wm_wanted_count--; 29248 /* 29249 * We can reuse the memory from the completed sl_wmp 29250 * lock range for our new lock, but only if noone is 29251 * waiting for it. 29252 */ 29253 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 29254 if (sl_wmp->wm_wanted_count == 0) { 29255 if (wmp != NULL) 29256 CHK_N_FREEWMP(un, wmp); 29257 wmp = sl_wmp; 29258 } 29259 sl_wmp = NULL; 29260 /* 29261 * After waking up, need to recheck for availability of 29262 * range. 29263 */ 29264 state = SD_WM_CHK_LIST; 29265 break; 29266 29267 default: 29268 panic("sd_range_lock: " 29269 "Unknown state %d in sd_range_lock", state); 29270 /*NOTREACHED*/ 29271 } /* switch(state) */ 29272 29273 } /* while(state != SD_WM_DONE) */ 29274 29275 mutex_exit(SD_MUTEX(un)); 29276 29277 ASSERT(wmp != NULL); 29278 29279 return (wmp); 29280 } 29281 29282 29283 /* 29284 * Function: sd_get_range() 29285 * 29286 * Description: Find if there any overlapping I/O to this one 29287 * Returns the write-map of 1st such I/O, NULL otherwise. 29288 * 29289 * Arguments: un - sd_lun structure for the device. 29290 * startb - The starting block number 29291 * endb - The end block number 29292 * 29293 * Return Code: wm - pointer to the wmap structure. 29294 */ 29295 29296 static struct sd_w_map * 29297 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 29298 { 29299 struct sd_w_map *wmp; 29300 29301 ASSERT(un != NULL); 29302 29303 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 29304 if (!(wmp->wm_flags & SD_WM_BUSY)) { 29305 continue; 29306 } 29307 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 29308 break; 29309 } 29310 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 29311 break; 29312 } 29313 } 29314 29315 return (wmp); 29316 } 29317 29318 29319 /* 29320 * Function: sd_free_inlist_wmap() 29321 * 29322 * Description: Unlink and free a write map struct. 29323 * 29324 * Arguments: un - sd_lun structure for the device. 29325 * wmp - sd_w_map which needs to be unlinked. 29326 */ 29327 29328 static void 29329 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 29330 { 29331 ASSERT(un != NULL); 29332 29333 if (un->un_wm == wmp) { 29334 un->un_wm = wmp->wm_next; 29335 } else { 29336 wmp->wm_prev->wm_next = wmp->wm_next; 29337 } 29338 29339 if (wmp->wm_next) { 29340 wmp->wm_next->wm_prev = wmp->wm_prev; 29341 } 29342 29343 wmp->wm_next = wmp->wm_prev = NULL; 29344 29345 kmem_cache_free(un->un_wm_cache, wmp); 29346 } 29347 29348 29349 /* 29350 * Function: sd_range_unlock() 29351 * 29352 * Description: Unlock the range locked by wm. 29353 * Free write map if nobody else is waiting on it. 29354 * 29355 * Arguments: un - sd_lun structure for the device. 29356 * wmp - sd_w_map which needs to be unlinked. 29357 */ 29358 29359 static void 29360 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 29361 { 29362 ASSERT(un != NULL); 29363 ASSERT(wm != NULL); 29364 ASSERT(!mutex_owned(SD_MUTEX(un))); 29365 29366 mutex_enter(SD_MUTEX(un)); 29367 29368 if (wm->wm_flags & SD_WTYPE_RMW) { 29369 un->un_rmw_count--; 29370 } 29371 29372 if (wm->wm_wanted_count) { 29373 wm->wm_flags = 0; 29374 /* 29375 * Broadcast that the wmap is available now. 29376 */ 29377 cv_broadcast(&wm->wm_avail); 29378 } else { 29379 /* 29380 * If no one is waiting on the map, it should be free'ed. 29381 */ 29382 sd_free_inlist_wmap(un, wm); 29383 } 29384 29385 mutex_exit(SD_MUTEX(un)); 29386 } 29387 29388 29389 /* 29390 * Function: sd_read_modify_write_task 29391 * 29392 * Description: Called from a taskq thread to initiate the write phase of 29393 * a read-modify-write request. This is used for targets where 29394 * un->un_sys_blocksize != un->un_tgt_blocksize. 29395 * 29396 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 29397 * 29398 * Context: Called under taskq thread context. 29399 */ 29400 29401 static void 29402 sd_read_modify_write_task(void *arg) 29403 { 29404 struct sd_mapblocksize_info *bsp; 29405 struct buf *bp; 29406 struct sd_xbuf *xp; 29407 struct sd_lun *un; 29408 29409 bp = arg; /* The bp is given in arg */ 29410 ASSERT(bp != NULL); 29411 29412 /* Get the pointer to the layer-private data struct */ 29413 xp = SD_GET_XBUF(bp); 29414 ASSERT(xp != NULL); 29415 bsp = xp->xb_private; 29416 ASSERT(bsp != NULL); 29417 29418 un = SD_GET_UN(bp); 29419 ASSERT(un != NULL); 29420 ASSERT(!mutex_owned(SD_MUTEX(un))); 29421 29422 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29423 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 29424 29425 /* 29426 * This is the write phase of a read-modify-write request, called 29427 * under the context of a taskq thread in response to the completion 29428 * of the read portion of the rmw request completing under interrupt 29429 * context. The write request must be sent from here down the iostart 29430 * chain as if it were being sent from sd_mapblocksize_iostart(), so 29431 * we use the layer index saved in the layer-private data area. 29432 */ 29433 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 29434 29435 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 29436 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 29437 } 29438 29439 29440 /* 29441 * Function: sddump_do_read_of_rmw() 29442 * 29443 * Description: This routine will be called from sddump, If sddump is called 29444 * with an I/O which not aligned on device blocksize boundary 29445 * then the write has to be converted to read-modify-write. 29446 * Do the read part here in order to keep sddump simple. 29447 * Note - That the sd_mutex is held across the call to this 29448 * routine. 29449 * 29450 * Arguments: un - sd_lun 29451 * blkno - block number in terms of media block size. 29452 * nblk - number of blocks. 29453 * bpp - pointer to pointer to the buf structure. On return 29454 * from this function, *bpp points to the valid buffer 29455 * to which the write has to be done. 29456 * 29457 * Return Code: 0 for success or errno-type return code 29458 */ 29459 29460 static int 29461 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 29462 struct buf **bpp) 29463 { 29464 int err; 29465 int i; 29466 int rval; 29467 struct buf *bp; 29468 struct scsi_pkt *pkt = NULL; 29469 uint32_t target_blocksize; 29470 29471 ASSERT(un != NULL); 29472 ASSERT(mutex_owned(SD_MUTEX(un))); 29473 29474 target_blocksize = un->un_tgt_blocksize; 29475 29476 mutex_exit(SD_MUTEX(un)); 29477 29478 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 29479 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 29480 if (bp == NULL) { 29481 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29482 "no resources for dumping; giving up"); 29483 err = ENOMEM; 29484 goto done; 29485 } 29486 29487 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 29488 blkno, nblk); 29489 if (rval != 0) { 29490 scsi_free_consistent_buf(bp); 29491 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29492 "no resources for dumping; giving up"); 29493 err = ENOMEM; 29494 goto done; 29495 } 29496 29497 pkt->pkt_flags |= FLAG_NOINTR; 29498 29499 err = EIO; 29500 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 29501 29502 /* 29503 * Scsi_poll returns 0 (success) if the command completes and 29504 * the status block is STATUS_GOOD. We should only check 29505 * errors if this condition is not true. Even then we should 29506 * send our own request sense packet only if we have a check 29507 * condition and auto request sense has not been performed by 29508 * the hba. 29509 */ 29510 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 29511 29512 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 29513 err = 0; 29514 break; 29515 } 29516 29517 /* 29518 * Check CMD_DEV_GONE 1st, give up if device is gone, 29519 * no need to read RQS data. 29520 */ 29521 if (pkt->pkt_reason == CMD_DEV_GONE) { 29522 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29523 "Error while dumping state with rmw..." 29524 "Device is gone\n"); 29525 break; 29526 } 29527 29528 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 29529 SD_INFO(SD_LOG_DUMP, un, 29530 "sddump: read failed with CHECK, try # %d\n", i); 29531 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 29532 (void) sd_send_polled_RQS(un); 29533 } 29534 29535 continue; 29536 } 29537 29538 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 29539 int reset_retval = 0; 29540 29541 SD_INFO(SD_LOG_DUMP, un, 29542 "sddump: read failed with BUSY, try # %d\n", i); 29543 29544 if (un->un_f_lun_reset_enabled == TRUE) { 29545 reset_retval = scsi_reset(SD_ADDRESS(un), 29546 RESET_LUN); 29547 } 29548 if (reset_retval == 0) { 29549 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 29550 } 29551 (void) sd_send_polled_RQS(un); 29552 29553 } else { 29554 SD_INFO(SD_LOG_DUMP, un, 29555 "sddump: read failed with 0x%x, try # %d\n", 29556 SD_GET_PKT_STATUS(pkt), i); 29557 mutex_enter(SD_MUTEX(un)); 29558 sd_reset_target(un, pkt); 29559 mutex_exit(SD_MUTEX(un)); 29560 } 29561 29562 /* 29563 * If we are not getting anywhere with lun/target resets, 29564 * let's reset the bus. 29565 */ 29566 if (i > SD_NDUMP_RETRIES/2) { 29567 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 29568 (void) sd_send_polled_RQS(un); 29569 } 29570 29571 } 29572 scsi_destroy_pkt(pkt); 29573 29574 if (err != 0) { 29575 scsi_free_consistent_buf(bp); 29576 *bpp = NULL; 29577 } else { 29578 *bpp = bp; 29579 } 29580 29581 done: 29582 mutex_enter(SD_MUTEX(un)); 29583 return (err); 29584 } 29585 29586 29587 /* 29588 * Function: sd_failfast_flushq 29589 * 29590 * Description: Take all bp's on the wait queue that have B_FAILFAST set 29591 * in b_flags and move them onto the failfast queue, then kick 29592 * off a thread to return all bp's on the failfast queue to 29593 * their owners with an error set. 29594 * 29595 * Arguments: un - pointer to the soft state struct for the instance. 29596 * 29597 * Context: may execute in interrupt context. 29598 */ 29599 29600 static void 29601 sd_failfast_flushq(struct sd_lun *un) 29602 { 29603 struct buf *bp; 29604 struct buf *next_waitq_bp; 29605 struct buf *prev_waitq_bp = NULL; 29606 29607 ASSERT(un != NULL); 29608 ASSERT(mutex_owned(SD_MUTEX(un))); 29609 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 29610 ASSERT(un->un_failfast_bp == NULL); 29611 29612 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29613 "sd_failfast_flushq: entry: un:0x%p\n", un); 29614 29615 /* 29616 * Check if we should flush all bufs when entering failfast state, or 29617 * just those with B_FAILFAST set. 29618 */ 29619 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 29620 /* 29621 * Move *all* bp's on the wait queue to the failfast flush 29622 * queue, including those that do NOT have B_FAILFAST set. 29623 */ 29624 if (un->un_failfast_headp == NULL) { 29625 ASSERT(un->un_failfast_tailp == NULL); 29626 un->un_failfast_headp = un->un_waitq_headp; 29627 } else { 29628 ASSERT(un->un_failfast_tailp != NULL); 29629 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 29630 } 29631 29632 un->un_failfast_tailp = un->un_waitq_tailp; 29633 29634 /* update kstat for each bp moved out of the waitq */ 29635 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 29636 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29637 } 29638 29639 /* empty the waitq */ 29640 un->un_waitq_headp = un->un_waitq_tailp = NULL; 29641 29642 } else { 29643 /* 29644 * Go thru the wait queue, pick off all entries with 29645 * B_FAILFAST set, and move these onto the failfast queue. 29646 */ 29647 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 29648 /* 29649 * Save the pointer to the next bp on the wait queue, 29650 * so we get to it on the next iteration of this loop. 29651 */ 29652 next_waitq_bp = bp->av_forw; 29653 29654 /* 29655 * If this bp from the wait queue does NOT have 29656 * B_FAILFAST set, just move on to the next element 29657 * in the wait queue. Note, this is the only place 29658 * where it is correct to set prev_waitq_bp. 29659 */ 29660 if ((bp->b_flags & B_FAILFAST) == 0) { 29661 prev_waitq_bp = bp; 29662 continue; 29663 } 29664 29665 /* 29666 * Remove the bp from the wait queue. 29667 */ 29668 if (bp == un->un_waitq_headp) { 29669 /* The bp is the first element of the waitq. */ 29670 un->un_waitq_headp = next_waitq_bp; 29671 if (un->un_waitq_headp == NULL) { 29672 /* The wait queue is now empty */ 29673 un->un_waitq_tailp = NULL; 29674 } 29675 } else { 29676 /* 29677 * The bp is either somewhere in the middle 29678 * or at the end of the wait queue. 29679 */ 29680 ASSERT(un->un_waitq_headp != NULL); 29681 ASSERT(prev_waitq_bp != NULL); 29682 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 29683 == 0); 29684 if (bp == un->un_waitq_tailp) { 29685 /* bp is the last entry on the waitq. */ 29686 ASSERT(next_waitq_bp == NULL); 29687 un->un_waitq_tailp = prev_waitq_bp; 29688 } 29689 prev_waitq_bp->av_forw = next_waitq_bp; 29690 } 29691 bp->av_forw = NULL; 29692 29693 /* 29694 * update kstat since the bp is moved out of 29695 * the waitq 29696 */ 29697 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 29698 29699 /* 29700 * Now put the bp onto the failfast queue. 29701 */ 29702 if (un->un_failfast_headp == NULL) { 29703 /* failfast queue is currently empty */ 29704 ASSERT(un->un_failfast_tailp == NULL); 29705 un->un_failfast_headp = 29706 un->un_failfast_tailp = bp; 29707 } else { 29708 /* Add the bp to the end of the failfast q */ 29709 ASSERT(un->un_failfast_tailp != NULL); 29710 ASSERT(un->un_failfast_tailp->b_flags & 29711 B_FAILFAST); 29712 un->un_failfast_tailp->av_forw = bp; 29713 un->un_failfast_tailp = bp; 29714 } 29715 } 29716 } 29717 29718 /* 29719 * Now return all bp's on the failfast queue to their owners. 29720 */ 29721 while ((bp = un->un_failfast_headp) != NULL) { 29722 29723 un->un_failfast_headp = bp->av_forw; 29724 if (un->un_failfast_headp == NULL) { 29725 un->un_failfast_tailp = NULL; 29726 } 29727 29728 /* 29729 * We want to return the bp with a failure error code, but 29730 * we do not want a call to sd_start_cmds() to occur here, 29731 * so use sd_return_failed_command_no_restart() instead of 29732 * sd_return_failed_command(). 29733 */ 29734 sd_return_failed_command_no_restart(un, bp, EIO); 29735 } 29736 29737 /* Flush the xbuf queues if required. */ 29738 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 29739 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 29740 } 29741 29742 SD_TRACE(SD_LOG_IO_FAILFAST, un, 29743 "sd_failfast_flushq: exit: un:0x%p\n", un); 29744 } 29745 29746 29747 /* 29748 * Function: sd_failfast_flushq_callback 29749 * 29750 * Description: Return TRUE if the given bp meets the criteria for failfast 29751 * flushing. Used with ddi_xbuf_flushq(9F). 29752 * 29753 * Arguments: bp - ptr to buf struct to be examined. 29754 * 29755 * Context: Any 29756 */ 29757 29758 static int 29759 sd_failfast_flushq_callback(struct buf *bp) 29760 { 29761 /* 29762 * Return TRUE if (1) we want to flush ALL bufs when the failfast 29763 * state is entered; OR (2) the given bp has B_FAILFAST set. 29764 */ 29765 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 29766 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 29767 } 29768 29769 29770 29771 /* 29772 * Function: sd_setup_next_xfer 29773 * 29774 * Description: Prepare next I/O operation using DMA_PARTIAL 29775 * 29776 */ 29777 29778 static int 29779 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 29780 struct scsi_pkt *pkt, struct sd_xbuf *xp) 29781 { 29782 ssize_t num_blks_not_xfered; 29783 daddr_t strt_blk_num; 29784 ssize_t bytes_not_xfered; 29785 int rval; 29786 29787 ASSERT(pkt->pkt_resid == 0); 29788 29789 /* 29790 * Calculate next block number and amount to be transferred. 29791 * 29792 * How much data NOT transfered to the HBA yet. 29793 */ 29794 bytes_not_xfered = xp->xb_dma_resid; 29795 29796 /* 29797 * figure how many blocks NOT transfered to the HBA yet. 29798 */ 29799 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 29800 29801 /* 29802 * set starting block number to the end of what WAS transfered. 29803 */ 29804 strt_blk_num = xp->xb_blkno + 29805 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 29806 29807 /* 29808 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 29809 * will call scsi_initpkt with NULL_FUNC so we do not have to release 29810 * the disk mutex here. 29811 */ 29812 rval = sd_setup_next_rw_pkt(un, pkt, bp, 29813 strt_blk_num, num_blks_not_xfered); 29814 29815 if (rval == 0) { 29816 29817 /* 29818 * Success. 29819 * 29820 * Adjust things if there are still more blocks to be 29821 * transfered. 29822 */ 29823 xp->xb_dma_resid = pkt->pkt_resid; 29824 pkt->pkt_resid = 0; 29825 29826 return (1); 29827 } 29828 29829 /* 29830 * There's really only one possible return value from 29831 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 29832 * returns NULL. 29833 */ 29834 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 29835 29836 bp->b_resid = bp->b_bcount; 29837 bp->b_flags |= B_ERROR; 29838 29839 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29840 "Error setting up next portion of DMA transfer\n"); 29841 29842 return (0); 29843 } 29844 29845 /* 29846 * Function: sd_panic_for_res_conflict 29847 * 29848 * Description: Call panic with a string formatted with "Reservation Conflict" 29849 * and a human readable identifier indicating the SD instance 29850 * that experienced the reservation conflict. 29851 * 29852 * Arguments: un - pointer to the soft state struct for the instance. 29853 * 29854 * Context: may execute in interrupt context. 29855 */ 29856 29857 #define SD_RESV_CONFLICT_FMT_LEN 40 29858 void 29859 sd_panic_for_res_conflict(struct sd_lun *un) 29860 { 29861 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 29862 char path_str[MAXPATHLEN]; 29863 29864 (void) snprintf(panic_str, sizeof (panic_str), 29865 "Reservation Conflict\nDisk: %s", 29866 ddi_pathname(SD_DEVINFO(un), path_str)); 29867 29868 panic(panic_str); 29869 } 29870 29871 /* 29872 * Note: The following sd_faultinjection_ioctl( ) routines implement 29873 * driver support for handling fault injection for error analysis 29874 * causing faults in multiple layers of the driver. 29875 * 29876 */ 29877 29878 #ifdef SD_FAULT_INJECTION 29879 static uint_t sd_fault_injection_on = 0; 29880 29881 /* 29882 * Function: sd_faultinjection_ioctl() 29883 * 29884 * Description: This routine is the driver entry point for handling 29885 * faultinjection ioctls to inject errors into the 29886 * layer model 29887 * 29888 * Arguments: cmd - the ioctl cmd received 29889 * arg - the arguments from user and returns 29890 */ 29891 29892 static void 29893 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 29894 29895 uint_t i = 0; 29896 uint_t rval; 29897 29898 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 29899 29900 mutex_enter(SD_MUTEX(un)); 29901 29902 switch (cmd) { 29903 case SDIOCRUN: 29904 /* Allow pushed faults to be injected */ 29905 SD_INFO(SD_LOG_SDTEST, un, 29906 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 29907 29908 sd_fault_injection_on = 1; 29909 29910 SD_INFO(SD_LOG_IOERR, un, 29911 "sd_faultinjection_ioctl: run finished\n"); 29912 break; 29913 29914 case SDIOCSTART: 29915 /* Start Injection Session */ 29916 SD_INFO(SD_LOG_SDTEST, un, 29917 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 29918 29919 sd_fault_injection_on = 0; 29920 un->sd_injection_mask = 0xFFFFFFFF; 29921 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 29922 un->sd_fi_fifo_pkt[i] = NULL; 29923 un->sd_fi_fifo_xb[i] = NULL; 29924 un->sd_fi_fifo_un[i] = NULL; 29925 un->sd_fi_fifo_arq[i] = NULL; 29926 } 29927 un->sd_fi_fifo_start = 0; 29928 un->sd_fi_fifo_end = 0; 29929 29930 mutex_enter(&(un->un_fi_mutex)); 29931 un->sd_fi_log[0] = '\0'; 29932 un->sd_fi_buf_len = 0; 29933 mutex_exit(&(un->un_fi_mutex)); 29934 29935 SD_INFO(SD_LOG_IOERR, un, 29936 "sd_faultinjection_ioctl: start finished\n"); 29937 break; 29938 29939 case SDIOCSTOP: 29940 /* Stop Injection Session */ 29941 SD_INFO(SD_LOG_SDTEST, un, 29942 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 29943 sd_fault_injection_on = 0; 29944 un->sd_injection_mask = 0x0; 29945 29946 /* Empty stray or unuseds structs from fifo */ 29947 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 29948 if (un->sd_fi_fifo_pkt[i] != NULL) { 29949 kmem_free(un->sd_fi_fifo_pkt[i], 29950 sizeof (struct sd_fi_pkt)); 29951 } 29952 if (un->sd_fi_fifo_xb[i] != NULL) { 29953 kmem_free(un->sd_fi_fifo_xb[i], 29954 sizeof (struct sd_fi_xb)); 29955 } 29956 if (un->sd_fi_fifo_un[i] != NULL) { 29957 kmem_free(un->sd_fi_fifo_un[i], 29958 sizeof (struct sd_fi_un)); 29959 } 29960 if (un->sd_fi_fifo_arq[i] != NULL) { 29961 kmem_free(un->sd_fi_fifo_arq[i], 29962 sizeof (struct sd_fi_arq)); 29963 } 29964 un->sd_fi_fifo_pkt[i] = NULL; 29965 un->sd_fi_fifo_un[i] = NULL; 29966 un->sd_fi_fifo_xb[i] = NULL; 29967 un->sd_fi_fifo_arq[i] = NULL; 29968 } 29969 un->sd_fi_fifo_start = 0; 29970 un->sd_fi_fifo_end = 0; 29971 29972 SD_INFO(SD_LOG_IOERR, un, 29973 "sd_faultinjection_ioctl: stop finished\n"); 29974 break; 29975 29976 case SDIOCINSERTPKT: 29977 /* Store a packet struct to be pushed onto fifo */ 29978 SD_INFO(SD_LOG_SDTEST, un, 29979 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 29980 29981 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29982 29983 sd_fault_injection_on = 0; 29984 29985 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 29986 if (un->sd_fi_fifo_pkt[i] != NULL) { 29987 kmem_free(un->sd_fi_fifo_pkt[i], 29988 sizeof (struct sd_fi_pkt)); 29989 } 29990 if (arg != NULL) { 29991 un->sd_fi_fifo_pkt[i] = 29992 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 29993 if (un->sd_fi_fifo_pkt[i] == NULL) { 29994 /* Alloc failed don't store anything */ 29995 break; 29996 } 29997 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 29998 sizeof (struct sd_fi_pkt), 0); 29999 if (rval == -1) { 30000 kmem_free(un->sd_fi_fifo_pkt[i], 30001 sizeof (struct sd_fi_pkt)); 30002 un->sd_fi_fifo_pkt[i] = NULL; 30003 } 30004 } else { 30005 SD_INFO(SD_LOG_IOERR, un, 30006 "sd_faultinjection_ioctl: pkt null\n"); 30007 } 30008 break; 30009 30010 case SDIOCINSERTXB: 30011 /* Store a xb struct to be pushed onto fifo */ 30012 SD_INFO(SD_LOG_SDTEST, un, 30013 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30014 30015 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30016 30017 sd_fault_injection_on = 0; 30018 30019 if (un->sd_fi_fifo_xb[i] != NULL) { 30020 kmem_free(un->sd_fi_fifo_xb[i], 30021 sizeof (struct sd_fi_xb)); 30022 un->sd_fi_fifo_xb[i] = NULL; 30023 } 30024 if (arg != NULL) { 30025 un->sd_fi_fifo_xb[i] = 30026 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30027 if (un->sd_fi_fifo_xb[i] == NULL) { 30028 /* Alloc failed don't store anything */ 30029 break; 30030 } 30031 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30032 sizeof (struct sd_fi_xb), 0); 30033 30034 if (rval == -1) { 30035 kmem_free(un->sd_fi_fifo_xb[i], 30036 sizeof (struct sd_fi_xb)); 30037 un->sd_fi_fifo_xb[i] = NULL; 30038 } 30039 } else { 30040 SD_INFO(SD_LOG_IOERR, un, 30041 "sd_faultinjection_ioctl: xb null\n"); 30042 } 30043 break; 30044 30045 case SDIOCINSERTUN: 30046 /* Store a un struct to be pushed onto fifo */ 30047 SD_INFO(SD_LOG_SDTEST, un, 30048 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30049 30050 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30051 30052 sd_fault_injection_on = 0; 30053 30054 if (un->sd_fi_fifo_un[i] != NULL) { 30055 kmem_free(un->sd_fi_fifo_un[i], 30056 sizeof (struct sd_fi_un)); 30057 un->sd_fi_fifo_un[i] = NULL; 30058 } 30059 if (arg != NULL) { 30060 un->sd_fi_fifo_un[i] = 30061 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30062 if (un->sd_fi_fifo_un[i] == NULL) { 30063 /* Alloc failed don't store anything */ 30064 break; 30065 } 30066 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30067 sizeof (struct sd_fi_un), 0); 30068 if (rval == -1) { 30069 kmem_free(un->sd_fi_fifo_un[i], 30070 sizeof (struct sd_fi_un)); 30071 un->sd_fi_fifo_un[i] = NULL; 30072 } 30073 30074 } else { 30075 SD_INFO(SD_LOG_IOERR, un, 30076 "sd_faultinjection_ioctl: un null\n"); 30077 } 30078 30079 break; 30080 30081 case SDIOCINSERTARQ: 30082 /* Store a arq struct to be pushed onto fifo */ 30083 SD_INFO(SD_LOG_SDTEST, un, 30084 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30085 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30086 30087 sd_fault_injection_on = 0; 30088 30089 if (un->sd_fi_fifo_arq[i] != NULL) { 30090 kmem_free(un->sd_fi_fifo_arq[i], 30091 sizeof (struct sd_fi_arq)); 30092 un->sd_fi_fifo_arq[i] = NULL; 30093 } 30094 if (arg != NULL) { 30095 un->sd_fi_fifo_arq[i] = 30096 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30097 if (un->sd_fi_fifo_arq[i] == NULL) { 30098 /* Alloc failed don't store anything */ 30099 break; 30100 } 30101 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30102 sizeof (struct sd_fi_arq), 0); 30103 if (rval == -1) { 30104 kmem_free(un->sd_fi_fifo_arq[i], 30105 sizeof (struct sd_fi_arq)); 30106 un->sd_fi_fifo_arq[i] = NULL; 30107 } 30108 30109 } else { 30110 SD_INFO(SD_LOG_IOERR, un, 30111 "sd_faultinjection_ioctl: arq null\n"); 30112 } 30113 30114 break; 30115 30116 case SDIOCPUSH: 30117 /* Push stored xb, pkt, un, and arq onto fifo */ 30118 sd_fault_injection_on = 0; 30119 30120 if (arg != NULL) { 30121 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30122 if (rval != -1 && 30123 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30124 un->sd_fi_fifo_end += i; 30125 } 30126 } else { 30127 SD_INFO(SD_LOG_IOERR, un, 30128 "sd_faultinjection_ioctl: push arg null\n"); 30129 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30130 un->sd_fi_fifo_end++; 30131 } 30132 } 30133 SD_INFO(SD_LOG_IOERR, un, 30134 "sd_faultinjection_ioctl: push to end=%d\n", 30135 un->sd_fi_fifo_end); 30136 break; 30137 30138 case SDIOCRETRIEVE: 30139 /* Return buffer of log from Injection session */ 30140 SD_INFO(SD_LOG_SDTEST, un, 30141 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30142 30143 sd_fault_injection_on = 0; 30144 30145 mutex_enter(&(un->un_fi_mutex)); 30146 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30147 un->sd_fi_buf_len+1, 0); 30148 mutex_exit(&(un->un_fi_mutex)); 30149 30150 if (rval == -1) { 30151 /* 30152 * arg is possibly invalid setting 30153 * it to NULL for return 30154 */ 30155 arg = NULL; 30156 } 30157 break; 30158 } 30159 30160 mutex_exit(SD_MUTEX(un)); 30161 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30162 " exit\n"); 30163 } 30164 30165 30166 /* 30167 * Function: sd_injection_log() 30168 * 30169 * Description: This routine adds buff to the already existing injection log 30170 * for retrieval via faultinjection_ioctl for use in fault 30171 * detection and recovery 30172 * 30173 * Arguments: buf - the string to add to the log 30174 */ 30175 30176 static void 30177 sd_injection_log(char *buf, struct sd_lun *un) 30178 { 30179 uint_t len; 30180 30181 ASSERT(un != NULL); 30182 ASSERT(buf != NULL); 30183 30184 mutex_enter(&(un->un_fi_mutex)); 30185 30186 len = min(strlen(buf), 255); 30187 /* Add logged value to Injection log to be returned later */ 30188 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30189 uint_t offset = strlen((char *)un->sd_fi_log); 30190 char *destp = (char *)un->sd_fi_log + offset; 30191 int i; 30192 for (i = 0; i < len; i++) { 30193 *destp++ = *buf++; 30194 } 30195 un->sd_fi_buf_len += len; 30196 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30197 } 30198 30199 mutex_exit(&(un->un_fi_mutex)); 30200 } 30201 30202 30203 /* 30204 * Function: sd_faultinjection() 30205 * 30206 * Description: This routine takes the pkt and changes its 30207 * content based on error injection scenerio. 30208 * 30209 * Arguments: pktp - packet to be changed 30210 */ 30211 30212 static void 30213 sd_faultinjection(struct scsi_pkt *pktp) 30214 { 30215 uint_t i; 30216 struct sd_fi_pkt *fi_pkt; 30217 struct sd_fi_xb *fi_xb; 30218 struct sd_fi_un *fi_un; 30219 struct sd_fi_arq *fi_arq; 30220 struct buf *bp; 30221 struct sd_xbuf *xb; 30222 struct sd_lun *un; 30223 30224 ASSERT(pktp != NULL); 30225 30226 /* pull bp xb and un from pktp */ 30227 bp = (struct buf *)pktp->pkt_private; 30228 xb = SD_GET_XBUF(bp); 30229 un = SD_GET_UN(bp); 30230 30231 ASSERT(un != NULL); 30232 30233 mutex_enter(SD_MUTEX(un)); 30234 30235 SD_TRACE(SD_LOG_SDTEST, un, 30236 "sd_faultinjection: entry Injection from sdintr\n"); 30237 30238 /* if injection is off return */ 30239 if (sd_fault_injection_on == 0 || 30240 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 30241 mutex_exit(SD_MUTEX(un)); 30242 return; 30243 } 30244 30245 SD_INFO(SD_LOG_SDTEST, un, 30246 "sd_faultinjection: is working for copying\n"); 30247 30248 /* take next set off fifo */ 30249 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 30250 30251 fi_pkt = un->sd_fi_fifo_pkt[i]; 30252 fi_xb = un->sd_fi_fifo_xb[i]; 30253 fi_un = un->sd_fi_fifo_un[i]; 30254 fi_arq = un->sd_fi_fifo_arq[i]; 30255 30256 30257 /* set variables accordingly */ 30258 /* set pkt if it was on fifo */ 30259 if (fi_pkt != NULL) { 30260 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 30261 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 30262 if (fi_pkt->pkt_cdbp != 0xff) 30263 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 30264 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 30265 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 30266 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 30267 30268 } 30269 /* set xb if it was on fifo */ 30270 if (fi_xb != NULL) { 30271 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 30272 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 30273 if (fi_xb->xb_retry_count != 0) 30274 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 30275 SD_CONDSET(xb, xb, xb_victim_retry_count, 30276 "xb_victim_retry_count"); 30277 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 30278 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 30279 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 30280 30281 /* copy in block data from sense */ 30282 /* 30283 * if (fi_xb->xb_sense_data[0] != -1) { 30284 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 30285 * SENSE_LENGTH); 30286 * } 30287 */ 30288 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH); 30289 30290 /* copy in extended sense codes */ 30291 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30292 xb, es_code, "es_code"); 30293 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30294 xb, es_key, "es_key"); 30295 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30296 xb, es_add_code, "es_add_code"); 30297 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 30298 xb, es_qual_code, "es_qual_code"); 30299 struct scsi_extended_sense *esp; 30300 esp = (struct scsi_extended_sense *)xb->xb_sense_data; 30301 esp->es_class = CLASS_EXTENDED_SENSE; 30302 } 30303 30304 /* set un if it was on fifo */ 30305 if (fi_un != NULL) { 30306 SD_CONDSET(SD_INQUIRY(un), un, inq_rmb, "inq_rmb"); 30307 SD_CONDSET(un, un, un_ctype, "un_ctype"); 30308 SD_CONDSET(un, un, un_reset_retry_count, 30309 "un_reset_retry_count"); 30310 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 30311 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 30312 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 30313 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 30314 "un_f_allow_bus_device_reset"); 30315 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 30316 30317 } 30318 30319 /* copy in auto request sense if it was on fifo */ 30320 if (fi_arq != NULL) { 30321 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 30322 } 30323 30324 /* free structs */ 30325 if (un->sd_fi_fifo_pkt[i] != NULL) { 30326 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 30327 } 30328 if (un->sd_fi_fifo_xb[i] != NULL) { 30329 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 30330 } 30331 if (un->sd_fi_fifo_un[i] != NULL) { 30332 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 30333 } 30334 if (un->sd_fi_fifo_arq[i] != NULL) { 30335 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 30336 } 30337 30338 /* 30339 * kmem_free does not gurantee to set to NULL 30340 * since we uses these to determine if we set 30341 * values or not lets confirm they are always 30342 * NULL after free 30343 */ 30344 un->sd_fi_fifo_pkt[i] = NULL; 30345 un->sd_fi_fifo_un[i] = NULL; 30346 un->sd_fi_fifo_xb[i] = NULL; 30347 un->sd_fi_fifo_arq[i] = NULL; 30348 30349 un->sd_fi_fifo_start++; 30350 30351 mutex_exit(SD_MUTEX(un)); 30352 30353 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 30354 } 30355 30356 #endif /* SD_FAULT_INJECTION */ 30357 30358 /* 30359 * This routine is invoked in sd_unit_attach(). Before calling it, the 30360 * properties in conf file should be processed already, and "hotpluggable" 30361 * property was processed also. 30362 * 30363 * The sd driver distinguishes 3 different type of devices: removable media, 30364 * non-removable media, and hotpluggable. Below the differences are defined: 30365 * 30366 * 1. Device ID 30367 * 30368 * The device ID of a device is used to identify this device. Refer to 30369 * ddi_devid_register(9F). 30370 * 30371 * For a non-removable media disk device which can provide 0x80 or 0x83 30372 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 30373 * device ID is created to identify this device. For other non-removable 30374 * media devices, a default device ID is created only if this device has 30375 * at least 2 alter cylinders. Otherwise, this device has no devid. 30376 * 30377 * ------------------------------------------------------- 30378 * removable media hotpluggable | Can Have Device ID 30379 * ------------------------------------------------------- 30380 * false false | Yes 30381 * false true | Yes 30382 * true x | No 30383 * ------------------------------------------------------ 30384 * 30385 * 30386 * 2. SCSI group 4 commands 30387 * 30388 * In SCSI specs, only some commands in group 4 command set can use 30389 * 8-byte addresses that can be used to access >2TB storage spaces. 30390 * Other commands have no such capability. Without supporting group4, 30391 * it is impossible to make full use of storage spaces of a disk with 30392 * capacity larger than 2TB. 30393 * 30394 * ----------------------------------------------- 30395 * removable media hotpluggable LP64 | Group 30396 * ----------------------------------------------- 30397 * false false false | 1 30398 * false false true | 4 30399 * false true false | 1 30400 * false true true | 4 30401 * true x x | 5 30402 * ----------------------------------------------- 30403 * 30404 * 30405 * 3. Check for VTOC Label 30406 * 30407 * If a direct-access disk has no EFI label, sd will check if it has a 30408 * valid VTOC label. Now, sd also does that check for removable media 30409 * and hotpluggable devices. 30410 * 30411 * -------------------------------------------------------------- 30412 * Direct-Access removable media hotpluggable | Check Label 30413 * ------------------------------------------------------------- 30414 * false false false | No 30415 * false false true | No 30416 * false true false | Yes 30417 * false true true | Yes 30418 * true x x | Yes 30419 * -------------------------------------------------------------- 30420 * 30421 * 30422 * 4. Building default VTOC label 30423 * 30424 * As section 3 says, sd checks if some kinds of devices have VTOC label. 30425 * If those devices have no valid VTOC label, sd(7d) will attempt to 30426 * create default VTOC for them. Currently sd creates default VTOC label 30427 * for all devices on x86 platform (VTOC_16), but only for removable 30428 * media devices on SPARC (VTOC_8). 30429 * 30430 * ----------------------------------------------------------- 30431 * removable media hotpluggable platform | Default Label 30432 * ----------------------------------------------------------- 30433 * false false sparc | No 30434 * false true x86 | Yes 30435 * false true sparc | Yes 30436 * true x x | Yes 30437 * ---------------------------------------------------------- 30438 * 30439 * 30440 * 5. Supported blocksizes of target devices 30441 * 30442 * Sd supports non-512-byte blocksize for removable media devices only. 30443 * For other devices, only 512-byte blocksize is supported. This may be 30444 * changed in near future because some RAID devices require non-512-byte 30445 * blocksize 30446 * 30447 * ----------------------------------------------------------- 30448 * removable media hotpluggable | non-512-byte blocksize 30449 * ----------------------------------------------------------- 30450 * false false | No 30451 * false true | No 30452 * true x | Yes 30453 * ----------------------------------------------------------- 30454 * 30455 * 30456 * 6. Automatic mount & unmount 30457 * 30458 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 30459 * if a device is removable media device. It return 1 for removable media 30460 * devices, and 0 for others. 30461 * 30462 * The automatic mounting subsystem should distinguish between the types 30463 * of devices and apply automounting policies to each. 30464 * 30465 * 30466 * 7. fdisk partition management 30467 * 30468 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 30469 * just supports fdisk partitions on x86 platform. On sparc platform, sd 30470 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 30471 * fdisk partitions on both x86 and SPARC platform. 30472 * 30473 * ----------------------------------------------------------- 30474 * platform removable media USB/1394 | fdisk supported 30475 * ----------------------------------------------------------- 30476 * x86 X X | true 30477 * ------------------------------------------------------------ 30478 * sparc X X | false 30479 * ------------------------------------------------------------ 30480 * 30481 * 30482 * 8. MBOOT/MBR 30483 * 30484 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 30485 * read/write mboot for removable media devices on sparc platform. 30486 * 30487 * ----------------------------------------------------------- 30488 * platform removable media USB/1394 | mboot supported 30489 * ----------------------------------------------------------- 30490 * x86 X X | true 30491 * ------------------------------------------------------------ 30492 * sparc false false | false 30493 * sparc false true | true 30494 * sparc true false | true 30495 * sparc true true | true 30496 * ------------------------------------------------------------ 30497 * 30498 * 30499 * 9. error handling during opening device 30500 * 30501 * If failed to open a disk device, an errno is returned. For some kinds 30502 * of errors, different errno is returned depending on if this device is 30503 * a removable media device. This brings USB/1394 hard disks in line with 30504 * expected hard disk behavior. It is not expected that this breaks any 30505 * application. 30506 * 30507 * ------------------------------------------------------ 30508 * removable media hotpluggable | errno 30509 * ------------------------------------------------------ 30510 * false false | EIO 30511 * false true | EIO 30512 * true x | ENXIO 30513 * ------------------------------------------------------ 30514 * 30515 * 30516 * 11. ioctls: DKIOCEJECT, CDROMEJECT 30517 * 30518 * These IOCTLs are applicable only to removable media devices. 30519 * 30520 * ----------------------------------------------------------- 30521 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 30522 * ----------------------------------------------------------- 30523 * false false | No 30524 * false true | No 30525 * true x | Yes 30526 * ----------------------------------------------------------- 30527 * 30528 * 30529 * 12. Kstats for partitions 30530 * 30531 * sd creates partition kstat for non-removable media devices. USB and 30532 * Firewire hard disks now have partition kstats 30533 * 30534 * ------------------------------------------------------ 30535 * removable media hotpluggable | kstat 30536 * ------------------------------------------------------ 30537 * false false | Yes 30538 * false true | Yes 30539 * true x | No 30540 * ------------------------------------------------------ 30541 * 30542 * 30543 * 13. Removable media & hotpluggable properties 30544 * 30545 * Sd driver creates a "removable-media" property for removable media 30546 * devices. Parent nexus drivers create a "hotpluggable" property if 30547 * it supports hotplugging. 30548 * 30549 * --------------------------------------------------------------------- 30550 * removable media hotpluggable | "removable-media" " hotpluggable" 30551 * --------------------------------------------------------------------- 30552 * false false | No No 30553 * false true | No Yes 30554 * true false | Yes No 30555 * true true | Yes Yes 30556 * --------------------------------------------------------------------- 30557 * 30558 * 30559 * 14. Power Management 30560 * 30561 * sd only power manages removable media devices or devices that support 30562 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 30563 * 30564 * A parent nexus that supports hotplugging can also set "pm-capable" 30565 * if the disk can be power managed. 30566 * 30567 * ------------------------------------------------------------ 30568 * removable media hotpluggable pm-capable | power manage 30569 * ------------------------------------------------------------ 30570 * false false false | No 30571 * false false true | Yes 30572 * false true false | No 30573 * false true true | Yes 30574 * true x x | Yes 30575 * ------------------------------------------------------------ 30576 * 30577 * USB and firewire hard disks can now be power managed independently 30578 * of the framebuffer 30579 * 30580 * 30581 * 15. Support for USB disks with capacity larger than 1TB 30582 * 30583 * Currently, sd doesn't permit a fixed disk device with capacity 30584 * larger than 1TB to be used in a 32-bit operating system environment. 30585 * However, sd doesn't do that for removable media devices. Instead, it 30586 * assumes that removable media devices cannot have a capacity larger 30587 * than 1TB. Therefore, using those devices on 32-bit system is partially 30588 * supported, which can cause some unexpected results. 30589 * 30590 * --------------------------------------------------------------------- 30591 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 30592 * --------------------------------------------------------------------- 30593 * false false | true | no 30594 * false true | true | no 30595 * true false | true | Yes 30596 * true true | true | Yes 30597 * --------------------------------------------------------------------- 30598 * 30599 * 30600 * 16. Check write-protection at open time 30601 * 30602 * When a removable media device is being opened for writing without NDELAY 30603 * flag, sd will check if this device is writable. If attempting to open 30604 * without NDELAY flag a write-protected device, this operation will abort. 30605 * 30606 * ------------------------------------------------------------ 30607 * removable media USB/1394 | WP Check 30608 * ------------------------------------------------------------ 30609 * false false | No 30610 * false true | No 30611 * true false | Yes 30612 * true true | Yes 30613 * ------------------------------------------------------------ 30614 * 30615 * 30616 * 17. syslog when corrupted VTOC is encountered 30617 * 30618 * Currently, if an invalid VTOC is encountered, sd only print syslog 30619 * for fixed SCSI disks. 30620 * ------------------------------------------------------------ 30621 * removable media USB/1394 | print syslog 30622 * ------------------------------------------------------------ 30623 * false false | Yes 30624 * false true | No 30625 * true false | No 30626 * true true | No 30627 * ------------------------------------------------------------ 30628 */ 30629 static void 30630 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 30631 { 30632 int pm_cap; 30633 30634 ASSERT(un->un_sd); 30635 ASSERT(SD_INQUIRY(un)); 30636 30637 /* 30638 * Enable SYNC CACHE support for all devices. 30639 */ 30640 un->un_f_sync_cache_supported = TRUE; 30641 30642 /* 30643 * Set the sync cache required flag to false. 30644 * This would ensure that there is no SYNC CACHE 30645 * sent when there are no writes 30646 */ 30647 un->un_f_sync_cache_required = FALSE; 30648 30649 if (SD_INQUIRY(un)->inq_rmb) { 30650 /* 30651 * The media of this device is removable. And for this kind 30652 * of devices, it is possible to change medium after opening 30653 * devices. Thus we should support this operation. 30654 */ 30655 un->un_f_has_removable_media = TRUE; 30656 30657 /* 30658 * support non-512-byte blocksize of removable media devices 30659 */ 30660 un->un_f_non_devbsize_supported = TRUE; 30661 30662 /* 30663 * Assume that all removable media devices support DOOR_LOCK 30664 */ 30665 un->un_f_doorlock_supported = TRUE; 30666 30667 /* 30668 * For a removable media device, it is possible to be opened 30669 * with NDELAY flag when there is no media in drive, in this 30670 * case we don't care if device is writable. But if without 30671 * NDELAY flag, we need to check if media is write-protected. 30672 */ 30673 un->un_f_chk_wp_open = TRUE; 30674 30675 /* 30676 * need to start a SCSI watch thread to monitor media state, 30677 * when media is being inserted or ejected, notify syseventd. 30678 */ 30679 un->un_f_monitor_media_state = TRUE; 30680 30681 /* 30682 * Some devices don't support START_STOP_UNIT command. 30683 * Therefore, we'd better check if a device supports it 30684 * before sending it. 30685 */ 30686 un->un_f_check_start_stop = TRUE; 30687 30688 /* 30689 * support eject media ioctl: 30690 * FDEJECT, DKIOCEJECT, CDROMEJECT 30691 */ 30692 un->un_f_eject_media_supported = TRUE; 30693 30694 /* 30695 * Because many removable-media devices don't support 30696 * LOG_SENSE, we couldn't use this command to check if 30697 * a removable media device support power-management. 30698 * We assume that they support power-management via 30699 * START_STOP_UNIT command and can be spun up and down 30700 * without limitations. 30701 */ 30702 un->un_f_pm_supported = TRUE; 30703 30704 /* 30705 * Need to create a zero length (Boolean) property 30706 * removable-media for the removable media devices. 30707 * Note that the return value of the property is not being 30708 * checked, since if unable to create the property 30709 * then do not want the attach to fail altogether. Consistent 30710 * with other property creation in attach. 30711 */ 30712 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 30713 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 30714 30715 } else { 30716 /* 30717 * create device ID for device 30718 */ 30719 un->un_f_devid_supported = TRUE; 30720 30721 /* 30722 * Spin up non-removable-media devices once it is attached 30723 */ 30724 un->un_f_attach_spinup = TRUE; 30725 30726 /* 30727 * According to SCSI specification, Sense data has two kinds of 30728 * format: fixed format, and descriptor format. At present, we 30729 * don't support descriptor format sense data for removable 30730 * media. 30731 */ 30732 if (ISDIRECT(un)) { 30733 struct inq_vd *vdp; 30734 int i; 30735 int vd; 30736 30737 un->un_f_descr_format_supported = TRUE; 30738 vdp = SD_INQUIRY(un)->inq_vd; 30739 30740 /* Assume SBC-3 if SCSI version is SPC-3 or later */ 30741 un->un_f_sbc3_supported = TRUE; 30742 if (SD_INQUIRY(un)->inq_ansi < 5) 30743 un->un_f_sbc3_supported = FALSE; 30744 /* Check version descriptors for SBC-3 */ 30745 for (i = 0; i < sizeof (SD_INQUIRY(un)->inq_vd); i++) { 30746 vd = (vdp[i].inq_vd_msb << 8) | 30747 vdp[i].inq_vd_lsb; 30748 switch (vd) { 30749 case 0x0180: /* SBC (no version claimed) */ 30750 case 0x019b: /* SBC T10/0996-D rev 08c */ 30751 case 0x019c: /* SBC ANSI INCITS 306-1998 */ 30752 case 0x0320: /* SBC-2 (no version claimed) */ 30753 case 0x0322: /* SBC-2 T10/1417-D rev 5a */ 30754 case 0x0324: /* SBC-2 T10/1417-D rev 15 */ 30755 case 0x033b: /* SBC-2 T10/1417-D rev 16 */ 30756 case 0x033d: /* SBC-2 ANSI INCITS 405-2005 */ 30757 case 0x033e: /* SBC-2 ISO/IEC 14776-32 */ 30758 un->un_f_sbc3_supported = FALSE; 30759 break; 30760 case 0x04c0: /* SBC-3 (no version claimed) */ 30761 un->un_f_sbc3_supported = TRUE; 30762 i = sizeof (SD_INQUIRY(un)->inq_vd); 30763 break; 30764 default: 30765 break; 30766 } 30767 30768 } 30769 } 30770 30771 /* 30772 * kstats are created only for non-removable media devices. 30773 * 30774 * Set this in sd.conf to 0 in order to disable kstats. The 30775 * default is 1, so they are enabled by default. 30776 */ 30777 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 30778 SD_DEVINFO(un), DDI_PROP_DONTPASS, 30779 "enable-partition-kstats", 1)); 30780 30781 /* 30782 * Check if HBA has set the "pm-capable" property. 30783 * If "pm-capable" exists and is non-zero then we can 30784 * power manage the device without checking the start/stop 30785 * cycle count log sense page. 30786 * 30787 * If "pm-capable" exists and is set to be false (0), 30788 * then we should not power manage the device. 30789 * 30790 * If "pm-capable" doesn't exist then pm_cap will 30791 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 30792 * sd will check the start/stop cycle count log sense page 30793 * and power manage the device if the cycle count limit has 30794 * not been exceeded. 30795 */ 30796 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 30797 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 30798 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) { 30799 un->un_f_log_sense_supported = TRUE; 30800 if (!un->un_f_power_condition_disabled && 30801 un->un_f_sbc3_supported == TRUE) { 30802 un->un_f_power_condition_supported = TRUE; 30803 } 30804 } else { 30805 /* 30806 * pm-capable property exists. 30807 * 30808 * Convert "TRUE" values for pm_cap to 30809 * SD_PM_CAPABLE_IS_TRUE to make it easier to check 30810 * later. "TRUE" values are any values defined in 30811 * inquiry.h. 30812 */ 30813 if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) { 30814 un->un_f_log_sense_supported = FALSE; 30815 } else { 30816 /* SD_PM_CAPABLE_IS_TRUE case */ 30817 un->un_f_pm_supported = TRUE; 30818 if (!un->un_f_power_condition_disabled && 30819 SD_PM_CAPABLE_IS_SPC_4(pm_cap)) { 30820 un->un_f_power_condition_supported = 30821 TRUE; 30822 } 30823 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) { 30824 un->un_f_log_sense_supported = TRUE; 30825 un->un_f_pm_log_sense_smart = 30826 SD_PM_CAP_SMART_LOG(pm_cap); 30827 } 30828 } 30829 30830 SD_INFO(SD_LOG_ATTACH_DETACH, un, 30831 "sd_unit_attach: un:0x%p pm-capable " 30832 "property set to %d.\n", un, un->un_f_pm_supported); 30833 } 30834 } 30835 30836 if (un->un_f_is_hotpluggable) { 30837 30838 /* 30839 * Have to watch hotpluggable devices as well, since 30840 * that's the only way for userland applications to 30841 * detect hot removal while device is busy/mounted. 30842 */ 30843 un->un_f_monitor_media_state = TRUE; 30844 30845 un->un_f_check_start_stop = TRUE; 30846 30847 } 30848 } 30849 30850 /* 30851 * sd_tg_rdwr: 30852 * Provides rdwr access for cmlb via sd_tgops. The start_block is 30853 * in sys block size, req_length in bytes. 30854 * 30855 */ 30856 static int 30857 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 30858 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 30859 { 30860 struct sd_lun *un; 30861 int path_flag = (int)(uintptr_t)tg_cookie; 30862 char *dkl = NULL; 30863 diskaddr_t real_addr = start_block; 30864 diskaddr_t first_byte, end_block; 30865 30866 size_t buffer_size = reqlength; 30867 int rval = 0; 30868 diskaddr_t cap; 30869 uint32_t lbasize; 30870 sd_ssc_t *ssc; 30871 30872 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 30873 if (un == NULL) 30874 return (ENXIO); 30875 30876 if (cmd != TG_READ && cmd != TG_WRITE) 30877 return (EINVAL); 30878 30879 ssc = sd_ssc_init(un); 30880 mutex_enter(SD_MUTEX(un)); 30881 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 30882 mutex_exit(SD_MUTEX(un)); 30883 rval = sd_read_capacity(ssc, path_flag); 30884 if (rval != 0) 30885 goto done1; 30886 mutex_enter(SD_MUTEX(un)); 30887 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 30888 mutex_exit(SD_MUTEX(un)); 30889 rval = EIO; 30890 goto done; 30891 } 30892 } 30893 30894 if (NOT_DEVBSIZE(un)) { 30895 /* 30896 * sys_blocksize != tgt_blocksize, need to re-adjust 30897 * blkno and save the index to beginning of dk_label 30898 */ 30899 first_byte = SD_SYSBLOCKS2BYTES(start_block); 30900 real_addr = first_byte / un->un_tgt_blocksize; 30901 30902 end_block = (first_byte + reqlength + 30903 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 30904 30905 /* round up buffer size to multiple of target block size */ 30906 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 30907 30908 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 30909 "label_addr: 0x%x allocation size: 0x%x\n", 30910 real_addr, buffer_size); 30911 30912 if (((first_byte % un->un_tgt_blocksize) != 0) || 30913 (reqlength % un->un_tgt_blocksize) != 0) 30914 /* the request is not aligned */ 30915 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 30916 } 30917 30918 /* 30919 * The MMC standard allows READ CAPACITY to be 30920 * inaccurate by a bounded amount (in the interest of 30921 * response latency). As a result, failed READs are 30922 * commonplace (due to the reading of metadata and not 30923 * data). Depending on the per-Vendor/drive Sense data, 30924 * the failed READ can cause many (unnecessary) retries. 30925 */ 30926 30927 if (ISCD(un) && (cmd == TG_READ) && 30928 (un->un_f_blockcount_is_valid == TRUE) && 30929 ((start_block == (un->un_blockcount - 1))|| 30930 (start_block == (un->un_blockcount - 2)))) { 30931 path_flag = SD_PATH_DIRECT_PRIORITY; 30932 } 30933 30934 mutex_exit(SD_MUTEX(un)); 30935 if (cmd == TG_READ) { 30936 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr, 30937 buffer_size, real_addr, path_flag); 30938 if (dkl != NULL) 30939 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 30940 real_addr), bufaddr, reqlength); 30941 } else { 30942 if (dkl) { 30943 rval = sd_send_scsi_READ(ssc, dkl, buffer_size, 30944 real_addr, path_flag); 30945 if (rval) { 30946 goto done1; 30947 } 30948 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 30949 real_addr), reqlength); 30950 } 30951 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr, 30952 buffer_size, real_addr, path_flag); 30953 } 30954 30955 done1: 30956 if (dkl != NULL) 30957 kmem_free(dkl, buffer_size); 30958 30959 if (rval != 0) { 30960 if (rval == EIO) 30961 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 30962 else 30963 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 30964 } 30965 done: 30966 sd_ssc_fini(ssc); 30967 return (rval); 30968 } 30969 30970 30971 static int 30972 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 30973 { 30974 30975 struct sd_lun *un; 30976 diskaddr_t cap; 30977 uint32_t lbasize; 30978 int path_flag = (int)(uintptr_t)tg_cookie; 30979 int ret = 0; 30980 30981 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 30982 if (un == NULL) 30983 return (ENXIO); 30984 30985 switch (cmd) { 30986 case TG_GETPHYGEOM: 30987 case TG_GETVIRTGEOM: 30988 case TG_GETCAPACITY: 30989 case TG_GETBLOCKSIZE: 30990 mutex_enter(SD_MUTEX(un)); 30991 30992 if ((un->un_f_blockcount_is_valid == TRUE) && 30993 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 30994 cap = un->un_blockcount; 30995 lbasize = un->un_tgt_blocksize; 30996 mutex_exit(SD_MUTEX(un)); 30997 } else { 30998 sd_ssc_t *ssc; 30999 mutex_exit(SD_MUTEX(un)); 31000 ssc = sd_ssc_init(un); 31001 ret = sd_read_capacity(ssc, path_flag); 31002 if (ret != 0) { 31003 if (ret == EIO) 31004 sd_ssc_assessment(ssc, 31005 SD_FMT_STATUS_CHECK); 31006 else 31007 sd_ssc_assessment(ssc, 31008 SD_FMT_IGNORE); 31009 sd_ssc_fini(ssc); 31010 return (ret); 31011 } 31012 sd_ssc_fini(ssc); 31013 mutex_enter(SD_MUTEX(un)); 31014 if ((un->un_f_blockcount_is_valid == FALSE) || 31015 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 31016 mutex_exit(SD_MUTEX(un)); 31017 return (EIO); 31018 } 31019 cap = un->un_blockcount; 31020 lbasize = un->un_tgt_blocksize; 31021 mutex_exit(SD_MUTEX(un)); 31022 } 31023 31024 if (cmd == TG_GETCAPACITY) { 31025 *(diskaddr_t *)arg = cap; 31026 return (0); 31027 } 31028 31029 if (cmd == TG_GETBLOCKSIZE) { 31030 *(uint32_t *)arg = lbasize; 31031 return (0); 31032 } 31033 31034 if (cmd == TG_GETPHYGEOM) 31035 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 31036 cap, lbasize, path_flag); 31037 else 31038 /* TG_GETVIRTGEOM */ 31039 ret = sd_get_virtual_geometry(un, 31040 (cmlb_geom_t *)arg, cap, lbasize); 31041 31042 return (ret); 31043 31044 case TG_GETATTR: 31045 mutex_enter(SD_MUTEX(un)); 31046 ((tg_attribute_t *)arg)->media_is_writable = 31047 un->un_f_mmc_writable_media; 31048 ((tg_attribute_t *)arg)->media_is_solid_state = 31049 un->un_f_is_solid_state; 31050 mutex_exit(SD_MUTEX(un)); 31051 return (0); 31052 default: 31053 return (ENOTTY); 31054 31055 } 31056 } 31057 31058 /* 31059 * Function: sd_ssc_ereport_post 31060 * 31061 * Description: Will be called when SD driver need to post an ereport. 31062 * 31063 * Context: Kernel thread or interrupt context. 31064 */ 31065 31066 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown" 31067 31068 static void 31069 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess) 31070 { 31071 int uscsi_path_instance = 0; 31072 uchar_t uscsi_pkt_reason; 31073 uint32_t uscsi_pkt_state; 31074 uint32_t uscsi_pkt_statistics; 31075 uint64_t uscsi_ena; 31076 uchar_t op_code; 31077 uint8_t *sensep; 31078 union scsi_cdb *cdbp; 31079 uint_t cdblen = 0; 31080 uint_t senlen = 0; 31081 struct sd_lun *un; 31082 dev_info_t *dip; 31083 char *devid; 31084 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON | 31085 SSC_FLAGS_INVALID_STATUS | 31086 SSC_FLAGS_INVALID_SENSE | 31087 SSC_FLAGS_INVALID_DATA; 31088 char assessment[16]; 31089 31090 ASSERT(ssc != NULL); 31091 ASSERT(ssc->ssc_uscsi_cmd != NULL); 31092 ASSERT(ssc->ssc_uscsi_info != NULL); 31093 31094 un = ssc->ssc_un; 31095 ASSERT(un != NULL); 31096 31097 dip = un->un_sd->sd_dev; 31098 31099 /* 31100 * Get the devid: 31101 * devid will only be passed to non-transport error reports. 31102 */ 31103 devid = DEVI(dip)->devi_devid_str; 31104 31105 /* 31106 * If we are syncing or dumping, the command will not be executed 31107 * so we bypass this situation. 31108 */ 31109 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 31110 (un->un_state == SD_STATE_DUMPING)) 31111 return; 31112 31113 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason; 31114 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance; 31115 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state; 31116 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics; 31117 uscsi_ena = ssc->ssc_uscsi_info->ui_ena; 31118 31119 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 31120 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb; 31121 31122 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */ 31123 if (cdbp == NULL) { 31124 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 31125 "sd_ssc_ereport_post meet empty cdb\n"); 31126 return; 31127 } 31128 31129 op_code = cdbp->scc_cmd; 31130 31131 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen; 31132 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 31133 ssc->ssc_uscsi_cmd->uscsi_rqresid); 31134 31135 if (senlen > 0) 31136 ASSERT(sensep != NULL); 31137 31138 /* 31139 * Initialize drv_assess to corresponding values. 31140 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending 31141 * on the sense-key returned back. 31142 */ 31143 switch (drv_assess) { 31144 case SD_FM_DRV_RECOVERY: 31145 (void) sprintf(assessment, "%s", "recovered"); 31146 break; 31147 case SD_FM_DRV_RETRY: 31148 (void) sprintf(assessment, "%s", "retry"); 31149 break; 31150 case SD_FM_DRV_NOTICE: 31151 (void) sprintf(assessment, "%s", "info"); 31152 break; 31153 case SD_FM_DRV_FATAL: 31154 default: 31155 (void) sprintf(assessment, "%s", "unknown"); 31156 } 31157 /* 31158 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered 31159 * command, we will post ereport.io.scsi.cmd.disk.recovered. 31160 * driver-assessment will always be "recovered" here. 31161 */ 31162 if (drv_assess == SD_FM_DRV_RECOVERY) { 31163 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31164 "cmd.disk.recovered", uscsi_ena, devid, NULL, 31165 DDI_NOSLEEP, NULL, 31166 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31167 DEVID_IF_KNOWN(devid), 31168 "driver-assessment", DATA_TYPE_STRING, assessment, 31169 "op-code", DATA_TYPE_UINT8, op_code, 31170 "cdb", DATA_TYPE_UINT8_ARRAY, 31171 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31172 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31173 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31174 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31175 NULL); 31176 return; 31177 } 31178 31179 /* 31180 * If there is un-expected/un-decodable data, we should post 31181 * ereport.io.scsi.cmd.disk.dev.uderr. 31182 * driver-assessment will be set based on parameter drv_assess. 31183 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back. 31184 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered. 31185 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered. 31186 * SSC_FLAGS_INVALID_DATA - invalid data sent back. 31187 */ 31188 if (ssc->ssc_flags & ssc_invalid_flags) { 31189 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) { 31190 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31191 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid, 31192 NULL, DDI_NOSLEEP, NULL, 31193 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31194 DEVID_IF_KNOWN(devid), 31195 "driver-assessment", DATA_TYPE_STRING, 31196 drv_assess == SD_FM_DRV_FATAL ? 31197 "fail" : assessment, 31198 "op-code", DATA_TYPE_UINT8, op_code, 31199 "cdb", DATA_TYPE_UINT8_ARRAY, 31200 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31201 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31202 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31203 "pkt-stats", DATA_TYPE_UINT32, 31204 uscsi_pkt_statistics, 31205 "stat-code", DATA_TYPE_UINT8, 31206 ssc->ssc_uscsi_cmd->uscsi_status, 31207 "un-decode-info", DATA_TYPE_STRING, 31208 ssc->ssc_info, 31209 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31210 senlen, sensep, 31211 NULL); 31212 } else { 31213 /* 31214 * For other type of invalid data, the 31215 * un-decode-value field would be empty because the 31216 * un-decodable content could be seen from upper 31217 * level payload or inside un-decode-info. 31218 */ 31219 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31220 NULL, 31221 "cmd.disk.dev.uderr", uscsi_ena, devid, 31222 NULL, DDI_NOSLEEP, NULL, 31223 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31224 DEVID_IF_KNOWN(devid), 31225 "driver-assessment", DATA_TYPE_STRING, 31226 drv_assess == SD_FM_DRV_FATAL ? 31227 "fail" : assessment, 31228 "op-code", DATA_TYPE_UINT8, op_code, 31229 "cdb", DATA_TYPE_UINT8_ARRAY, 31230 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31231 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31232 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 31233 "pkt-stats", DATA_TYPE_UINT32, 31234 uscsi_pkt_statistics, 31235 "stat-code", DATA_TYPE_UINT8, 31236 ssc->ssc_uscsi_cmd->uscsi_status, 31237 "un-decode-info", DATA_TYPE_STRING, 31238 ssc->ssc_info, 31239 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 31240 0, NULL, 31241 NULL); 31242 } 31243 ssc->ssc_flags &= ~ssc_invalid_flags; 31244 return; 31245 } 31246 31247 if (uscsi_pkt_reason != CMD_CMPLT || 31248 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) { 31249 /* 31250 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was 31251 * set inside sd_start_cmds due to errors(bad packet or 31252 * fatal transport error), we should take it as a 31253 * transport error, so we post ereport.io.scsi.cmd.disk.tran. 31254 * driver-assessment will be set based on drv_assess. 31255 * We will set devid to NULL because it is a transport 31256 * error. 31257 */ 31258 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT) 31259 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT; 31260 31261 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL, 31262 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL, 31263 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31264 DEVID_IF_KNOWN(devid), 31265 "driver-assessment", DATA_TYPE_STRING, 31266 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31267 "op-code", DATA_TYPE_UINT8, op_code, 31268 "cdb", DATA_TYPE_UINT8_ARRAY, 31269 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31270 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 31271 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state, 31272 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 31273 NULL); 31274 } else { 31275 /* 31276 * If we got here, we have a completed command, and we need 31277 * to further investigate the sense data to see what kind 31278 * of ereport we should post. 31279 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr 31280 * if sense-key == 0x3. 31281 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise. 31282 * driver-assessment will be set based on the parameter 31283 * drv_assess. 31284 */ 31285 if (senlen > 0) { 31286 /* 31287 * Here we have sense data available. 31288 */ 31289 uint8_t sense_key; 31290 sense_key = scsi_sense_key(sensep); 31291 if (sense_key == 0x3) { 31292 /* 31293 * sense-key == 0x3(medium error), 31294 * driver-assessment should be "fatal" if 31295 * drv_assess is SD_FM_DRV_FATAL. 31296 */ 31297 scsi_fm_ereport_post(un->un_sd, 31298 uscsi_path_instance, NULL, 31299 "cmd.disk.dev.rqs.merr", 31300 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL, 31301 FM_VERSION, DATA_TYPE_UINT8, 31302 FM_EREPORT_VERS0, 31303 DEVID_IF_KNOWN(devid), 31304 "driver-assessment", 31305 DATA_TYPE_STRING, 31306 drv_assess == SD_FM_DRV_FATAL ? 31307 "fatal" : assessment, 31308 "op-code", 31309 DATA_TYPE_UINT8, op_code, 31310 "cdb", 31311 DATA_TYPE_UINT8_ARRAY, cdblen, 31312 ssc->ssc_uscsi_cmd->uscsi_cdb, 31313 "pkt-reason", 31314 DATA_TYPE_UINT8, uscsi_pkt_reason, 31315 "pkt-state", 31316 DATA_TYPE_UINT8, uscsi_pkt_state, 31317 "pkt-stats", 31318 DATA_TYPE_UINT32, 31319 uscsi_pkt_statistics, 31320 "stat-code", 31321 DATA_TYPE_UINT8, 31322 ssc->ssc_uscsi_cmd->uscsi_status, 31323 "key", 31324 DATA_TYPE_UINT8, 31325 scsi_sense_key(sensep), 31326 "asc", 31327 DATA_TYPE_UINT8, 31328 scsi_sense_asc(sensep), 31329 "ascq", 31330 DATA_TYPE_UINT8, 31331 scsi_sense_ascq(sensep), 31332 "sense-data", 31333 DATA_TYPE_UINT8_ARRAY, 31334 senlen, sensep, 31335 "lba", 31336 DATA_TYPE_UINT64, 31337 ssc->ssc_uscsi_info->ui_lba, 31338 NULL); 31339 } else { 31340 /* 31341 * if sense-key == 0x4(hardware 31342 * error), driver-assessment should 31343 * be "fatal" if drv_assess is 31344 * SD_FM_DRV_FATAL. 31345 */ 31346 scsi_fm_ereport_post(un->un_sd, 31347 uscsi_path_instance, NULL, 31348 "cmd.disk.dev.rqs.derr", 31349 uscsi_ena, devid, 31350 NULL, DDI_NOSLEEP, NULL, 31351 FM_VERSION, 31352 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31353 DEVID_IF_KNOWN(devid), 31354 "driver-assessment", 31355 DATA_TYPE_STRING, 31356 drv_assess == SD_FM_DRV_FATAL ? 31357 (sense_key == 0x4 ? 31358 "fatal" : "fail") : assessment, 31359 "op-code", 31360 DATA_TYPE_UINT8, op_code, 31361 "cdb", 31362 DATA_TYPE_UINT8_ARRAY, cdblen, 31363 ssc->ssc_uscsi_cmd->uscsi_cdb, 31364 "pkt-reason", 31365 DATA_TYPE_UINT8, uscsi_pkt_reason, 31366 "pkt-state", 31367 DATA_TYPE_UINT8, uscsi_pkt_state, 31368 "pkt-stats", 31369 DATA_TYPE_UINT32, 31370 uscsi_pkt_statistics, 31371 "stat-code", 31372 DATA_TYPE_UINT8, 31373 ssc->ssc_uscsi_cmd->uscsi_status, 31374 "key", 31375 DATA_TYPE_UINT8, 31376 scsi_sense_key(sensep), 31377 "asc", 31378 DATA_TYPE_UINT8, 31379 scsi_sense_asc(sensep), 31380 "ascq", 31381 DATA_TYPE_UINT8, 31382 scsi_sense_ascq(sensep), 31383 "sense-data", 31384 DATA_TYPE_UINT8_ARRAY, 31385 senlen, sensep, 31386 NULL); 31387 } 31388 } else { 31389 /* 31390 * For stat_code == STATUS_GOOD, this is not a 31391 * hardware error. 31392 */ 31393 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) 31394 return; 31395 31396 /* 31397 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the 31398 * stat-code but with sense data unavailable. 31399 * driver-assessment will be set based on parameter 31400 * drv_assess. 31401 */ 31402 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 31403 NULL, 31404 "cmd.disk.dev.serr", uscsi_ena, 31405 devid, NULL, DDI_NOSLEEP, NULL, 31406 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 31407 DEVID_IF_KNOWN(devid), 31408 "driver-assessment", DATA_TYPE_STRING, 31409 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 31410 "op-code", DATA_TYPE_UINT8, op_code, 31411 "cdb", 31412 DATA_TYPE_UINT8_ARRAY, 31413 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 31414 "pkt-reason", 31415 DATA_TYPE_UINT8, uscsi_pkt_reason, 31416 "pkt-state", 31417 DATA_TYPE_UINT8, uscsi_pkt_state, 31418 "pkt-stats", 31419 DATA_TYPE_UINT32, uscsi_pkt_statistics, 31420 "stat-code", 31421 DATA_TYPE_UINT8, 31422 ssc->ssc_uscsi_cmd->uscsi_status, 31423 NULL); 31424 } 31425 } 31426 } 31427 31428 /* 31429 * Function: sd_ssc_extract_info 31430 * 31431 * Description: Extract information available to help generate ereport. 31432 * 31433 * Context: Kernel thread or interrupt context. 31434 */ 31435 static void 31436 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp, 31437 struct buf *bp, struct sd_xbuf *xp) 31438 { 31439 size_t senlen = 0; 31440 union scsi_cdb *cdbp; 31441 int path_instance; 31442 /* 31443 * Need scsi_cdb_size array to determine the cdb length. 31444 */ 31445 extern uchar_t scsi_cdb_size[]; 31446 31447 ASSERT(un != NULL); 31448 ASSERT(pktp != NULL); 31449 ASSERT(bp != NULL); 31450 ASSERT(xp != NULL); 31451 ASSERT(ssc != NULL); 31452 ASSERT(mutex_owned(SD_MUTEX(un))); 31453 31454 /* 31455 * Transfer the cdb buffer pointer here. 31456 */ 31457 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 31458 31459 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 31460 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp; 31461 31462 /* 31463 * Transfer the sense data buffer pointer if sense data is available, 31464 * calculate the sense data length first. 31465 */ 31466 if ((xp->xb_sense_state & STATE_XARQ_DONE) || 31467 (xp->xb_sense_state & STATE_ARQ_DONE)) { 31468 /* 31469 * For arq case, we will enter here. 31470 */ 31471 if (xp->xb_sense_state & STATE_XARQ_DONE) { 31472 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid; 31473 } else { 31474 senlen = SENSE_LENGTH; 31475 } 31476 } else { 31477 /* 31478 * For non-arq case, we will enter this branch. 31479 */ 31480 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK && 31481 (xp->xb_sense_state & STATE_XFERRED_DATA)) { 31482 senlen = SENSE_LENGTH - xp->xb_sense_resid; 31483 } 31484 31485 } 31486 31487 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff); 31488 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0; 31489 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data; 31490 31491 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 31492 31493 /* 31494 * Only transfer path_instance when scsi_pkt was properly allocated. 31495 */ 31496 path_instance = pktp->pkt_path_instance; 31497 if (scsi_pkt_allocated_correctly(pktp) && path_instance) 31498 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance; 31499 else 31500 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0; 31501 31502 /* 31503 * Copy in the other fields we may need when posting ereport. 31504 */ 31505 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason; 31506 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state; 31507 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics; 31508 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 31509 31510 /* 31511 * For partially read/write command, we will not create ena 31512 * in case of a successful command be reconized as recovered. 31513 */ 31514 if ((pktp->pkt_reason == CMD_CMPLT) && 31515 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) && 31516 (senlen == 0)) { 31517 return; 31518 } 31519 31520 /* 31521 * To associate ereports of a single command execution flow, we 31522 * need a shared ena for a specific command. 31523 */ 31524 if (xp->xb_ena == 0) 31525 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1); 31526 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena; 31527 } 31528 31529 31530 /* 31531 * Function: sd_check_solid_state 31532 * 31533 * Description: Query the optional INQUIRY VPD page 0xb1. If the device 31534 * supports VPD page 0xb1, sd examines the MEDIUM ROTATION 31535 * RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the 31536 * device is a solid state drive. 31537 * 31538 * Context: Kernel thread or interrupt context. 31539 */ 31540 31541 static void 31542 sd_check_solid_state(sd_ssc_t *ssc) 31543 { 31544 int rval = 0; 31545 uchar_t *inqb1 = NULL; 31546 size_t inqb1_len = MAX_INQUIRY_SIZE; 31547 size_t inqb1_resid = 0; 31548 struct sd_lun *un; 31549 31550 ASSERT(ssc != NULL); 31551 un = ssc->ssc_un; 31552 ASSERT(un != NULL); 31553 ASSERT(!mutex_owned(SD_MUTEX(un))); 31554 31555 mutex_enter(SD_MUTEX(un)); 31556 un->un_f_is_solid_state = FALSE; 31557 31558 if (ISDIRECT(un) && un->un_vpd_page_mask != 0 && 31559 un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) { 31560 mutex_exit(SD_MUTEX(un)); 31561 /* collect page b1 data */ 31562 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP); 31563 31564 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len, 31565 0x01, 0xB1, &inqb1_resid); 31566 31567 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) { 31568 SD_TRACE(SD_LOG_COMMON, un, 31569 "sd_check_solid_state: \ 31570 successfully get VPD page: %x \ 31571 PAGE LENGTH: %x BYTE 4: %x \ 31572 BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4], 31573 inqb1[5]); 31574 31575 mutex_enter(SD_MUTEX(un)); 31576 /* 31577 * Check the MEDIUM ROTATION RATE. If it is set 31578 * to 1, the device is a solid state drive. 31579 */ 31580 if (inqb1[4] == 0 && inqb1[5] == 1) { 31581 un->un_f_is_solid_state = TRUE; 31582 /* solid state drives don't need disksort */ 31583 un->un_f_disksort_disabled = TRUE; 31584 } 31585 mutex_exit(SD_MUTEX(un)); 31586 } else if (rval != 0) { 31587 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 31588 } 31589 31590 kmem_free(inqb1, inqb1_len); 31591 } else { 31592 mutex_exit(SD_MUTEX(un)); 31593 } 31594 } 31595 31596 /* 31597 * Function: sd_read_capacity 31598 * 31599 * Description: Read and update capacity and logical and physical 31600 * block sizes of the device. 31601 * 31602 * Context: Kernel thread or interrupt context. 31603 */ 31604 31605 static int 31606 sd_read_capacity(sd_ssc_t *ssc, int path_flag) 31607 { 31608 int rval = -1; 31609 uint64_t capacity; 31610 uint_t lbasize; 31611 uint_t pbsize; 31612 int i; 31613 int devid_len; 31614 struct sd_lun *un; 31615 31616 ASSERT(ssc != NULL); 31617 un = ssc->ssc_un; 31618 ASSERT(un != NULL); 31619 ASSERT(!mutex_owned(SD_MUTEX(un))); 31620 31621 mutex_enter(SD_MUTEX(un)); 31622 31623 if (ISDIRECT(un) && (un->un_f_sbc3_supported == TRUE)) { 31624 mutex_exit(SD_MUTEX(un)); 31625 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, 31626 &pbsize, path_flag); 31627 mutex_enter(SD_MUTEX(un)); 31628 } 31629 31630 if (rval != 0) { 31631 mutex_exit(SD_MUTEX(un)); 31632 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 31633 &lbasize, path_flag); 31634 pbsize = DEV_BSIZE; 31635 mutex_enter(SD_MUTEX(un)); 31636 } 31637 31638 if (rval == 0) { 31639 /* 31640 * Currently, for removable media, the capacity is saved in terms 31641 * of un->un_sys_blocksize, so scale the capacity value to reflect this. 31642 */ 31643 if (un->un_f_has_removable_media) 31644 capacity *= (lbasize / un->un_sys_blocksize); 31645 /* 31646 * The following relies on 31647 * sd_send_scsi_READ_CAPACITY never 31648 * returning 0 for capacity and/or lbasize. 31649 */ 31650 sd_update_block_info(un, lbasize, capacity); 31651 for (i = 0; i < sd_flash_dev_table_size; i++) { 31652 devid_len = (int)strlen(sd_flash_dev_table[i]); 31653 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], 31654 devid_len) == SD_SUCCESS) { 31655 pbsize = SSD_SECSIZE; 31656 if (pbsize > lbasize) 31657 un->un_f_enable_rmw = TRUE; 31658 break; 31659 } 31660 } 31661 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) { 31662 un->un_phy_blocksize = DEV_BSIZE; 31663 } else { 31664 un->un_phy_blocksize = pbsize; 31665 } 31666 31667 } 31668 31669 mutex_exit(SD_MUTEX(un)); 31670 31671 return (rval); 31672 }