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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * SCSI disk target driver. 29 */ 30 #include <sys/scsi/scsi.h> 31 #include <sys/dkbad.h> 32 #include <sys/dklabel.h> 33 #include <sys/dkio.h> 34 #include <sys/fdio.h> 35 #include <sys/cdio.h> 36 #include <sys/mhd.h> 37 #include <sys/vtoc.h> 38 #include <sys/dktp/fdisk.h> 39 #include <sys/kstat.h> 40 #include <sys/vtrace.h> 41 #include <sys/note.h> 42 #include <sys/thread.h> 43 #include <sys/proc.h> 44 #include <sys/efi_partition.h> 45 #include <sys/var.h> 46 #include <sys/aio_req.h> 47 48 #ifdef __lock_lint 49 #define _LP64 50 #define __amd64 51 #endif 52 53 #if (defined(__fibre)) 54 /* Note: is there a leadville version of the following? */ 55 #include <sys/fc4/fcal_linkapp.h> 56 #endif 57 #include <sys/taskq.h> 58 #include <sys/uuid.h> 59 #include <sys/byteorder.h> 60 #include <sys/sdt.h> 61 62 #include "sd_xbuf.h" 63 64 #include <sys/scsi/targets/sddef.h> 65 #include <sys/cmlb.h> 66 #include <sys/sysevent/eventdefs.h> 67 #include <sys/sysevent/dev.h> 68 69 #include <sys/fm/protocol.h> 70 71 /* 72 * Loadable module info. 73 */ 74 #if (defined(__fibre)) 75 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver" 76 char _depends_on[] = "misc/scsi misc/cmlb drv/fcp"; 77 #else 78 #define SD_MODULE_NAME "SCSI Disk Driver" 79 char _depends_on[] = "misc/scsi misc/cmlb"; 80 #endif 81 82 /* 83 * Define the interconnect type, to allow the driver to distinguish 84 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 85 * 86 * This is really for backward compatibility. In the future, the driver 87 * should actually check the "interconnect-type" property as reported by 88 * the HBA; however at present this property is not defined by all HBAs, 89 * so we will use this #define (1) to permit the driver to run in 90 * backward-compatibility mode; and (2) to print a notification message 91 * if an FC HBA does not support the "interconnect-type" property. The 92 * behavior of the driver will be to assume parallel SCSI behaviors unless 93 * the "interconnect-type" property is defined by the HBA **AND** has a 94 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 95 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 96 * Channel behaviors (as per the old ssd). (Note that the 97 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 98 * will result in the driver assuming parallel SCSI behaviors.) 99 * 100 * (see common/sys/scsi/impl/services.h) 101 * 102 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 103 * since some FC HBAs may already support that, and there is some code in 104 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 105 * default would confuse that code, and besides things should work fine 106 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 107 * "interconnect_type" property. 108 * 109 */ 110 #if (defined(__fibre)) 111 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 112 #else 113 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 114 #endif 115 116 /* 117 * The name of the driver, established from the module name in _init. 118 */ 119 static char *sd_label = NULL; 120 121 /* 122 * Driver name is unfortunately prefixed on some driver.conf properties. 123 */ 124 #if (defined(__fibre)) 125 #define sd_max_xfer_size ssd_max_xfer_size 126 #define sd_config_list ssd_config_list 127 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 128 static char *sd_config_list = "ssd-config-list"; 129 #else 130 static char *sd_max_xfer_size = "sd_max_xfer_size"; 131 static char *sd_config_list = "sd-config-list"; 132 #endif 133 134 /* 135 * Driver global variables 136 */ 137 138 #if (defined(__fibre)) 139 /* 140 * These #defines are to avoid namespace collisions that occur because this 141 * code is currently used to compile two separate driver modules: sd and ssd. 142 * All global variables need to be treated this way (even if declared static) 143 * in order to allow the debugger to resolve the names properly. 144 * It is anticipated that in the near future the ssd module will be obsoleted, 145 * at which time this namespace issue should go away. 146 */ 147 #define sd_state ssd_state 148 #define sd_io_time ssd_io_time 149 #define sd_failfast_enable ssd_failfast_enable 150 #define sd_ua_retry_count ssd_ua_retry_count 151 #define sd_report_pfa ssd_report_pfa 152 #define sd_max_throttle ssd_max_throttle 153 #define sd_min_throttle ssd_min_throttle 154 #define sd_rot_delay ssd_rot_delay 155 156 #define sd_retry_on_reservation_conflict \ 157 ssd_retry_on_reservation_conflict 158 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 159 #define sd_resv_conflict_name ssd_resv_conflict_name 160 161 #define sd_component_mask ssd_component_mask 162 #define sd_level_mask ssd_level_mask 163 #define sd_debug_un ssd_debug_un 164 #define sd_error_level ssd_error_level 165 166 #define sd_xbuf_active_limit ssd_xbuf_active_limit 167 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 168 169 #define sd_tr ssd_tr 170 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 171 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 172 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 173 #define sd_check_media_time ssd_check_media_time 174 #define sd_wait_cmds_complete ssd_wait_cmds_complete 175 #define sd_label_mutex ssd_label_mutex 176 #define sd_detach_mutex ssd_detach_mutex 177 #define sd_log_buf ssd_log_buf 178 #define sd_log_mutex ssd_log_mutex 179 180 #define sd_disk_table ssd_disk_table 181 #define sd_disk_table_size ssd_disk_table_size 182 #define sd_sense_mutex ssd_sense_mutex 183 #define sd_cdbtab ssd_cdbtab 184 185 #define sd_cb_ops ssd_cb_ops 186 #define sd_ops ssd_ops 187 #define sd_additional_codes ssd_additional_codes 188 #define sd_tgops ssd_tgops 189 190 #define sd_minor_data ssd_minor_data 191 #define sd_minor_data_efi ssd_minor_data_efi 192 193 #define sd_tq ssd_tq 194 #define sd_wmr_tq ssd_wmr_tq 195 #define sd_taskq_name ssd_taskq_name 196 #define sd_wmr_taskq_name ssd_wmr_taskq_name 197 #define sd_taskq_minalloc ssd_taskq_minalloc 198 #define sd_taskq_maxalloc ssd_taskq_maxalloc 199 200 #define sd_dump_format_string ssd_dump_format_string 201 202 #define sd_iostart_chain ssd_iostart_chain 203 #define sd_iodone_chain ssd_iodone_chain 204 205 #define sd_pm_idletime ssd_pm_idletime 206 207 #define sd_force_pm_supported ssd_force_pm_supported 208 209 #define sd_dtype_optical_bind ssd_dtype_optical_bind 210 211 #define sd_ssc_init ssd_ssc_init 212 #define sd_ssc_send ssd_ssc_send 213 #define sd_ssc_fini ssd_ssc_fini 214 #define sd_ssc_assessment ssd_ssc_assessment 215 #define sd_ssc_post ssd_ssc_post 216 #define sd_ssc_print ssd_ssc_print 217 #define sd_ssc_ereport_post ssd_ssc_ereport_post 218 #define sd_ssc_set_info ssd_ssc_set_info 219 #define sd_ssc_extract_info ssd_ssc_extract_info 220 221 #endif 222 223 #ifdef SDDEBUG 224 int sd_force_pm_supported = 0; 225 #endif /* SDDEBUG */ 226 227 void *sd_state = NULL; 228 int sd_io_time = SD_IO_TIME; 229 int sd_failfast_enable = 1; 230 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 231 int sd_report_pfa = 1; 232 int sd_max_throttle = SD_MAX_THROTTLE; 233 int sd_min_throttle = SD_MIN_THROTTLE; 234 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 235 int sd_qfull_throttle_enable = TRUE; 236 237 int sd_retry_on_reservation_conflict = 1; 238 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 239 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 240 241 static int sd_dtype_optical_bind = -1; 242 243 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 244 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 245 246 /* 247 * Global data for debug logging. To enable debug printing, sd_component_mask 248 * and sd_level_mask should be set to the desired bit patterns as outlined in 249 * sddef.h. 250 */ 251 uint_t sd_component_mask = 0x0; 252 uint_t sd_level_mask = 0x0; 253 struct sd_lun *sd_debug_un = NULL; 254 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 255 256 /* Note: these may go away in the future... */ 257 static uint32_t sd_xbuf_active_limit = 512; 258 static uint32_t sd_xbuf_reserve_limit = 16; 259 260 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 261 262 /* 263 * Timer value used to reset the throttle after it has been reduced 264 * (typically in response to TRAN_BUSY or STATUS_QFULL) 265 */ 266 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 267 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 268 269 /* 270 * Interval value associated with the media change scsi watch. 271 */ 272 static int sd_check_media_time = 3000000; 273 274 /* 275 * Wait value used for in progress operations during a DDI_SUSPEND 276 */ 277 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 278 279 /* 280 * sd_label_mutex protects a static buffer used in the disk label 281 * component of the driver 282 */ 283 static kmutex_t sd_label_mutex; 284 285 /* 286 * sd_detach_mutex protects un_layer_count, un_detach_count, and 287 * un_opens_in_progress in the sd_lun structure. 288 */ 289 static kmutex_t sd_detach_mutex; 290 291 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 292 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 293 294 /* 295 * Global buffer and mutex for debug logging 296 */ 297 static char sd_log_buf[1024]; 298 static kmutex_t sd_log_mutex; 299 300 /* 301 * Structs and globals for recording attached lun information. 302 * This maintains a chain. Each node in the chain represents a SCSI controller. 303 * The structure records the number of luns attached to each target connected 304 * with the controller. 305 * For parallel scsi device only. 306 */ 307 struct sd_scsi_hba_tgt_lun { 308 struct sd_scsi_hba_tgt_lun *next; 309 dev_info_t *pdip; 310 int nlun[NTARGETS_WIDE]; 311 }; 312 313 /* 314 * Flag to indicate the lun is attached or detached 315 */ 316 #define SD_SCSI_LUN_ATTACH 0 317 #define SD_SCSI_LUN_DETACH 1 318 319 static kmutex_t sd_scsi_target_lun_mutex; 320 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 321 322 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 323 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 324 325 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 326 sd_scsi_target_lun_head)) 327 328 /* 329 * "Smart" Probe Caching structs, globals, #defines, etc. 330 * For parallel scsi and non-self-identify device only. 331 */ 332 333 /* 334 * The following resources and routines are implemented to support 335 * "smart" probing, which caches the scsi_probe() results in an array, 336 * in order to help avoid long probe times. 337 */ 338 struct sd_scsi_probe_cache { 339 struct sd_scsi_probe_cache *next; 340 dev_info_t *pdip; 341 int cache[NTARGETS_WIDE]; 342 }; 343 344 static kmutex_t sd_scsi_probe_cache_mutex; 345 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 346 347 /* 348 * Really we only need protection on the head of the linked list, but 349 * better safe than sorry. 350 */ 351 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 352 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 353 354 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 355 sd_scsi_probe_cache_head)) 356 357 358 /* 359 * Vendor specific data name property declarations 360 */ 361 362 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 363 364 static sd_tunables seagate_properties = { 365 SEAGATE_THROTTLE_VALUE, 366 0, 367 0, 368 0, 369 0, 370 0, 371 0, 372 0, 373 0 374 }; 375 376 377 static sd_tunables fujitsu_properties = { 378 FUJITSU_THROTTLE_VALUE, 379 0, 380 0, 381 0, 382 0, 383 0, 384 0, 385 0, 386 0 387 }; 388 389 static sd_tunables ibm_properties = { 390 IBM_THROTTLE_VALUE, 391 0, 392 0, 393 0, 394 0, 395 0, 396 0, 397 0, 398 0 399 }; 400 401 static sd_tunables purple_properties = { 402 PURPLE_THROTTLE_VALUE, 403 0, 404 0, 405 PURPLE_BUSY_RETRIES, 406 PURPLE_RESET_RETRY_COUNT, 407 PURPLE_RESERVE_RELEASE_TIME, 408 0, 409 0, 410 0 411 }; 412 413 static sd_tunables sve_properties = { 414 SVE_THROTTLE_VALUE, 415 0, 416 0, 417 SVE_BUSY_RETRIES, 418 SVE_RESET_RETRY_COUNT, 419 SVE_RESERVE_RELEASE_TIME, 420 SVE_MIN_THROTTLE_VALUE, 421 SVE_DISKSORT_DISABLED_FLAG, 422 0 423 }; 424 425 static sd_tunables maserati_properties = { 426 0, 427 0, 428 0, 429 0, 430 0, 431 0, 432 0, 433 MASERATI_DISKSORT_DISABLED_FLAG, 434 MASERATI_LUN_RESET_ENABLED_FLAG 435 }; 436 437 static sd_tunables pirus_properties = { 438 PIRUS_THROTTLE_VALUE, 439 0, 440 PIRUS_NRR_COUNT, 441 PIRUS_BUSY_RETRIES, 442 PIRUS_RESET_RETRY_COUNT, 443 0, 444 PIRUS_MIN_THROTTLE_VALUE, 445 PIRUS_DISKSORT_DISABLED_FLAG, 446 PIRUS_LUN_RESET_ENABLED_FLAG 447 }; 448 449 #endif 450 451 #if (defined(__sparc) && !defined(__fibre)) || \ 452 (defined(__i386) || defined(__amd64)) 453 454 455 static sd_tunables elite_properties = { 456 ELITE_THROTTLE_VALUE, 457 0, 458 0, 459 0, 460 0, 461 0, 462 0, 463 0, 464 0 465 }; 466 467 static sd_tunables st31200n_properties = { 468 ST31200N_THROTTLE_VALUE, 469 0, 470 0, 471 0, 472 0, 473 0, 474 0, 475 0, 476 0 477 }; 478 479 #endif /* Fibre or not */ 480 481 static sd_tunables lsi_properties_scsi = { 482 LSI_THROTTLE_VALUE, 483 0, 484 LSI_NOTREADY_RETRIES, 485 0, 486 0, 487 0, 488 0, 489 0, 490 0 491 }; 492 493 static sd_tunables symbios_properties = { 494 SYMBIOS_THROTTLE_VALUE, 495 0, 496 SYMBIOS_NOTREADY_RETRIES, 497 0, 498 0, 499 0, 500 0, 501 0, 502 0 503 }; 504 505 static sd_tunables lsi_properties = { 506 0, 507 0, 508 LSI_NOTREADY_RETRIES, 509 0, 510 0, 511 0, 512 0, 513 0, 514 0 515 }; 516 517 static sd_tunables lsi_oem_properties = { 518 0, 519 0, 520 LSI_OEM_NOTREADY_RETRIES, 521 0, 522 0, 523 0, 524 0, 525 0, 526 0, 527 1 528 }; 529 530 531 532 #if (defined(SD_PROP_TST)) 533 534 #define SD_TST_CTYPE_VAL CTYPE_CDROM 535 #define SD_TST_THROTTLE_VAL 16 536 #define SD_TST_NOTREADY_VAL 12 537 #define SD_TST_BUSY_VAL 60 538 #define SD_TST_RST_RETRY_VAL 36 539 #define SD_TST_RSV_REL_TIME 60 540 541 static sd_tunables tst_properties = { 542 SD_TST_THROTTLE_VAL, 543 SD_TST_CTYPE_VAL, 544 SD_TST_NOTREADY_VAL, 545 SD_TST_BUSY_VAL, 546 SD_TST_RST_RETRY_VAL, 547 SD_TST_RSV_REL_TIME, 548 0, 549 0, 550 0 551 }; 552 #endif 553 554 /* This is similar to the ANSI toupper implementation */ 555 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 556 557 /* 558 * Static Driver Configuration Table 559 * 560 * This is the table of disks which need throttle adjustment (or, perhaps 561 * something else as defined by the flags at a future time.) device_id 562 * is a string consisting of concatenated vid (vendor), pid (product/model) 563 * and revision strings as defined in the scsi_inquiry structure. Offsets of 564 * the parts of the string are as defined by the sizes in the scsi_inquiry 565 * structure. Device type is searched as far as the device_id string is 566 * defined. Flags defines which values are to be set in the driver from the 567 * properties list. 568 * 569 * Entries below which begin and end with a "*" are a special case. 570 * These do not have a specific vendor, and the string which follows 571 * can appear anywhere in the 16 byte PID portion of the inquiry data. 572 * 573 * Entries below which begin and end with a " " (blank) are a special 574 * case. The comparison function will treat multiple consecutive blanks 575 * as equivalent to a single blank. For example, this causes a 576 * sd_disk_table entry of " NEC CDROM " to match a device's id string 577 * of "NEC CDROM". 578 * 579 * Note: The MD21 controller type has been obsoleted. 580 * ST318202F is a Legacy device 581 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 582 * made with an FC connection. The entries here are a legacy. 583 */ 584 static sd_disk_config_t sd_disk_table[] = { 585 #if defined(__fibre) || defined(__i386) || defined(__amd64) 586 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 587 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 588 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 589 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 590 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 591 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 592 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 593 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 594 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 595 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 596 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 597 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 598 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 599 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 600 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 601 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 602 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 603 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 604 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 605 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 606 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 607 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 608 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 609 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 610 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 611 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 612 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 613 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 614 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 615 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 616 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 617 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 618 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 619 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 620 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 621 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 622 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 623 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 624 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 625 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 626 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 627 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 628 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 629 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 630 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 631 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 632 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 633 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 634 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 635 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT | 636 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 637 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT | 638 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 639 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 640 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 641 { "SUN T3", SD_CONF_BSET_THROTTLE | 642 SD_CONF_BSET_BSY_RETRY_COUNT| 643 SD_CONF_BSET_RST_RETRIES| 644 SD_CONF_BSET_RSV_REL_TIME, 645 &purple_properties }, 646 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 647 SD_CONF_BSET_BSY_RETRY_COUNT| 648 SD_CONF_BSET_RST_RETRIES| 649 SD_CONF_BSET_RSV_REL_TIME| 650 SD_CONF_BSET_MIN_THROTTLE| 651 SD_CONF_BSET_DISKSORT_DISABLED, 652 &sve_properties }, 653 { "SUN T4", SD_CONF_BSET_THROTTLE | 654 SD_CONF_BSET_BSY_RETRY_COUNT| 655 SD_CONF_BSET_RST_RETRIES| 656 SD_CONF_BSET_RSV_REL_TIME, 657 &purple_properties }, 658 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 659 SD_CONF_BSET_LUN_RESET_ENABLED, 660 &maserati_properties }, 661 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 662 SD_CONF_BSET_NRR_COUNT| 663 SD_CONF_BSET_BSY_RETRY_COUNT| 664 SD_CONF_BSET_RST_RETRIES| 665 SD_CONF_BSET_MIN_THROTTLE| 666 SD_CONF_BSET_DISKSORT_DISABLED| 667 SD_CONF_BSET_LUN_RESET_ENABLED, 668 &pirus_properties }, 669 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 670 SD_CONF_BSET_NRR_COUNT| 671 SD_CONF_BSET_BSY_RETRY_COUNT| 672 SD_CONF_BSET_RST_RETRIES| 673 SD_CONF_BSET_MIN_THROTTLE| 674 SD_CONF_BSET_DISKSORT_DISABLED| 675 SD_CONF_BSET_LUN_RESET_ENABLED, 676 &pirus_properties }, 677 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 678 SD_CONF_BSET_NRR_COUNT| 679 SD_CONF_BSET_BSY_RETRY_COUNT| 680 SD_CONF_BSET_RST_RETRIES| 681 SD_CONF_BSET_MIN_THROTTLE| 682 SD_CONF_BSET_DISKSORT_DISABLED| 683 SD_CONF_BSET_LUN_RESET_ENABLED, 684 &pirus_properties }, 685 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 686 SD_CONF_BSET_NRR_COUNT| 687 SD_CONF_BSET_BSY_RETRY_COUNT| 688 SD_CONF_BSET_RST_RETRIES| 689 SD_CONF_BSET_MIN_THROTTLE| 690 SD_CONF_BSET_DISKSORT_DISABLED| 691 SD_CONF_BSET_LUN_RESET_ENABLED, 692 &pirus_properties }, 693 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 694 SD_CONF_BSET_NRR_COUNT| 695 SD_CONF_BSET_BSY_RETRY_COUNT| 696 SD_CONF_BSET_RST_RETRIES| 697 SD_CONF_BSET_MIN_THROTTLE| 698 SD_CONF_BSET_DISKSORT_DISABLED| 699 SD_CONF_BSET_LUN_RESET_ENABLED, 700 &pirus_properties }, 701 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 702 SD_CONF_BSET_NRR_COUNT| 703 SD_CONF_BSET_BSY_RETRY_COUNT| 704 SD_CONF_BSET_RST_RETRIES| 705 SD_CONF_BSET_MIN_THROTTLE| 706 SD_CONF_BSET_DISKSORT_DISABLED| 707 SD_CONF_BSET_LUN_RESET_ENABLED, 708 &pirus_properties }, 709 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 710 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 711 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 712 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 713 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 714 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 715 #endif /* fibre or NON-sparc platforms */ 716 #if ((defined(__sparc) && !defined(__fibre)) ||\ 717 (defined(__i386) || defined(__amd64))) 718 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 719 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 720 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 721 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 722 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 723 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 724 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 725 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 726 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 727 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 728 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 729 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 730 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 731 &symbios_properties }, 732 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 733 &lsi_properties_scsi }, 734 #if defined(__i386) || defined(__amd64) 735 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 736 | SD_CONF_BSET_READSUB_BCD 737 | SD_CONF_BSET_READ_TOC_ADDR_BCD 738 | SD_CONF_BSET_NO_READ_HEADER 739 | SD_CONF_BSET_READ_CD_XD4), NULL }, 740 741 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 742 | SD_CONF_BSET_READSUB_BCD 743 | SD_CONF_BSET_READ_TOC_ADDR_BCD 744 | SD_CONF_BSET_NO_READ_HEADER 745 | SD_CONF_BSET_READ_CD_XD4), NULL }, 746 #endif /* __i386 || __amd64 */ 747 #endif /* sparc NON-fibre or NON-sparc platforms */ 748 749 #if (defined(SD_PROP_TST)) 750 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 751 | SD_CONF_BSET_CTYPE 752 | SD_CONF_BSET_NRR_COUNT 753 | SD_CONF_BSET_FAB_DEVID 754 | SD_CONF_BSET_NOCACHE 755 | SD_CONF_BSET_BSY_RETRY_COUNT 756 | SD_CONF_BSET_PLAYMSF_BCD 757 | SD_CONF_BSET_READSUB_BCD 758 | SD_CONF_BSET_READ_TOC_TRK_BCD 759 | SD_CONF_BSET_READ_TOC_ADDR_BCD 760 | SD_CONF_BSET_NO_READ_HEADER 761 | SD_CONF_BSET_READ_CD_XD4 762 | SD_CONF_BSET_RST_RETRIES 763 | SD_CONF_BSET_RSV_REL_TIME 764 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 765 #endif 766 }; 767 768 static const int sd_disk_table_size = 769 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 770 771 772 773 #define SD_INTERCONNECT_PARALLEL 0 774 #define SD_INTERCONNECT_FABRIC 1 775 #define SD_INTERCONNECT_FIBRE 2 776 #define SD_INTERCONNECT_SSA 3 777 #define SD_INTERCONNECT_SATA 4 778 #define SD_IS_PARALLEL_SCSI(un) \ 779 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 780 #define SD_IS_SERIAL(un) \ 781 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 782 783 /* 784 * Definitions used by device id registration routines 785 */ 786 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 787 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 788 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 789 790 static kmutex_t sd_sense_mutex = {0}; 791 792 /* 793 * Macros for updates of the driver state 794 */ 795 #define New_state(un, s) \ 796 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 797 #define Restore_state(un) \ 798 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 799 800 static struct sd_cdbinfo sd_cdbtab[] = { 801 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 802 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 803 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 804 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 805 }; 806 807 /* 808 * Specifies the number of seconds that must have elapsed since the last 809 * cmd. has completed for a device to be declared idle to the PM framework. 810 */ 811 static int sd_pm_idletime = 1; 812 813 /* 814 * Internal function prototypes 815 */ 816 817 #if (defined(__fibre)) 818 /* 819 * These #defines are to avoid namespace collisions that occur because this 820 * code is currently used to compile two separate driver modules: sd and ssd. 821 * All function names need to be treated this way (even if declared static) 822 * in order to allow the debugger to resolve the names properly. 823 * It is anticipated that in the near future the ssd module will be obsoleted, 824 * at which time this ugliness should go away. 825 */ 826 #define sd_log_trace ssd_log_trace 827 #define sd_log_info ssd_log_info 828 #define sd_log_err ssd_log_err 829 #define sdprobe ssdprobe 830 #define sdinfo ssdinfo 831 #define sd_prop_op ssd_prop_op 832 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 833 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 834 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 835 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 836 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 837 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 838 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 839 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 840 #define sd_spin_up_unit ssd_spin_up_unit 841 #define sd_enable_descr_sense ssd_enable_descr_sense 842 #define sd_reenable_dsense_task ssd_reenable_dsense_task 843 #define sd_set_mmc_caps ssd_set_mmc_caps 844 #define sd_read_unit_properties ssd_read_unit_properties 845 #define sd_process_sdconf_file ssd_process_sdconf_file 846 #define sd_process_sdconf_table ssd_process_sdconf_table 847 #define sd_sdconf_id_match ssd_sdconf_id_match 848 #define sd_blank_cmp ssd_blank_cmp 849 #define sd_chk_vers1_data ssd_chk_vers1_data 850 #define sd_set_vers1_properties ssd_set_vers1_properties 851 852 #define sd_get_physical_geometry ssd_get_physical_geometry 853 #define sd_get_virtual_geometry ssd_get_virtual_geometry 854 #define sd_update_block_info ssd_update_block_info 855 #define sd_register_devid ssd_register_devid 856 #define sd_get_devid ssd_get_devid 857 #define sd_create_devid ssd_create_devid 858 #define sd_write_deviceid ssd_write_deviceid 859 #define sd_check_vpd_page_support ssd_check_vpd_page_support 860 #define sd_setup_pm ssd_setup_pm 861 #define sd_create_pm_components ssd_create_pm_components 862 #define sd_ddi_suspend ssd_ddi_suspend 863 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 864 #define sd_ddi_resume ssd_ddi_resume 865 #define sd_ddi_pm_resume ssd_ddi_pm_resume 866 #define sdpower ssdpower 867 #define sdattach ssdattach 868 #define sddetach ssddetach 869 #define sd_unit_attach ssd_unit_attach 870 #define sd_unit_detach ssd_unit_detach 871 #define sd_set_unit_attributes ssd_set_unit_attributes 872 #define sd_create_errstats ssd_create_errstats 873 #define sd_set_errstats ssd_set_errstats 874 #define sd_set_pstats ssd_set_pstats 875 #define sddump ssddump 876 #define sd_scsi_poll ssd_scsi_poll 877 #define sd_send_polled_RQS ssd_send_polled_RQS 878 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 879 #define sd_init_event_callbacks ssd_init_event_callbacks 880 #define sd_event_callback ssd_event_callback 881 #define sd_cache_control ssd_cache_control 882 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 883 #define sd_get_nv_sup ssd_get_nv_sup 884 #define sd_make_device ssd_make_device 885 #define sdopen ssdopen 886 #define sdclose ssdclose 887 #define sd_ready_and_valid ssd_ready_and_valid 888 #define sdmin ssdmin 889 #define sdread ssdread 890 #define sdwrite ssdwrite 891 #define sdaread ssdaread 892 #define sdawrite ssdawrite 893 #define sdstrategy ssdstrategy 894 #define sdioctl ssdioctl 895 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 896 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 897 #define sd_checksum_iostart ssd_checksum_iostart 898 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 899 #define sd_pm_iostart ssd_pm_iostart 900 #define sd_core_iostart ssd_core_iostart 901 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 902 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 903 #define sd_checksum_iodone ssd_checksum_iodone 904 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 905 #define sd_pm_iodone ssd_pm_iodone 906 #define sd_initpkt_for_buf ssd_initpkt_for_buf 907 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 908 #define sd_setup_rw_pkt ssd_setup_rw_pkt 909 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 910 #define sd_buf_iodone ssd_buf_iodone 911 #define sd_uscsi_strategy ssd_uscsi_strategy 912 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 913 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 914 #define sd_uscsi_iodone ssd_uscsi_iodone 915 #define sd_xbuf_strategy ssd_xbuf_strategy 916 #define sd_xbuf_init ssd_xbuf_init 917 #define sd_pm_entry ssd_pm_entry 918 #define sd_pm_exit ssd_pm_exit 919 920 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 921 #define sd_pm_timeout_handler ssd_pm_timeout_handler 922 923 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 924 #define sdintr ssdintr 925 #define sd_start_cmds ssd_start_cmds 926 #define sd_send_scsi_cmd ssd_send_scsi_cmd 927 #define sd_bioclone_alloc ssd_bioclone_alloc 928 #define sd_bioclone_free ssd_bioclone_free 929 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 930 #define sd_shadow_buf_free ssd_shadow_buf_free 931 #define sd_print_transport_rejected_message \ 932 ssd_print_transport_rejected_message 933 #define sd_retry_command ssd_retry_command 934 #define sd_set_retry_bp ssd_set_retry_bp 935 #define sd_send_request_sense_command ssd_send_request_sense_command 936 #define sd_start_retry_command ssd_start_retry_command 937 #define sd_start_direct_priority_command \ 938 ssd_start_direct_priority_command 939 #define sd_return_failed_command ssd_return_failed_command 940 #define sd_return_failed_command_no_restart \ 941 ssd_return_failed_command_no_restart 942 #define sd_return_command ssd_return_command 943 #define sd_sync_with_callback ssd_sync_with_callback 944 #define sdrunout ssdrunout 945 #define sd_mark_rqs_busy ssd_mark_rqs_busy 946 #define sd_mark_rqs_idle ssd_mark_rqs_idle 947 #define sd_reduce_throttle ssd_reduce_throttle 948 #define sd_restore_throttle ssd_restore_throttle 949 #define sd_print_incomplete_msg ssd_print_incomplete_msg 950 #define sd_init_cdb_limits ssd_init_cdb_limits 951 #define sd_pkt_status_good ssd_pkt_status_good 952 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 953 #define sd_pkt_status_busy ssd_pkt_status_busy 954 #define sd_pkt_status_reservation_conflict \ 955 ssd_pkt_status_reservation_conflict 956 #define sd_pkt_status_qfull ssd_pkt_status_qfull 957 #define sd_handle_request_sense ssd_handle_request_sense 958 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 959 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 960 #define sd_validate_sense_data ssd_validate_sense_data 961 #define sd_decode_sense ssd_decode_sense 962 #define sd_print_sense_msg ssd_print_sense_msg 963 #define sd_sense_key_no_sense ssd_sense_key_no_sense 964 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 965 #define sd_sense_key_not_ready ssd_sense_key_not_ready 966 #define sd_sense_key_medium_or_hardware_error \ 967 ssd_sense_key_medium_or_hardware_error 968 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 969 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 970 #define sd_sense_key_fail_command ssd_sense_key_fail_command 971 #define sd_sense_key_blank_check ssd_sense_key_blank_check 972 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 973 #define sd_sense_key_default ssd_sense_key_default 974 #define sd_print_retry_msg ssd_print_retry_msg 975 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 976 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 977 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 978 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 979 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 980 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 981 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 982 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 983 #define sd_pkt_reason_default ssd_pkt_reason_default 984 #define sd_reset_target ssd_reset_target 985 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 986 #define sd_start_stop_unit_task ssd_start_stop_unit_task 987 #define sd_taskq_create ssd_taskq_create 988 #define sd_taskq_delete ssd_taskq_delete 989 #define sd_target_change_task ssd_target_change_task 990 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event 991 #define sd_media_change_task ssd_media_change_task 992 #define sd_handle_mchange ssd_handle_mchange 993 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 994 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 995 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 996 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 997 #define sd_send_scsi_feature_GET_CONFIGURATION \ 998 sd_send_scsi_feature_GET_CONFIGURATION 999 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1000 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1001 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1002 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1003 ssd_send_scsi_PERSISTENT_RESERVE_IN 1004 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1005 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1006 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1007 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1008 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1009 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1010 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1011 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1012 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1013 #define sd_alloc_rqs ssd_alloc_rqs 1014 #define sd_free_rqs ssd_free_rqs 1015 #define sd_dump_memory ssd_dump_memory 1016 #define sd_get_media_info ssd_get_media_info 1017 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1018 #define sd_nvpair_str_decode ssd_nvpair_str_decode 1019 #define sd_strtok_r ssd_strtok_r 1020 #define sd_set_properties ssd_set_properties 1021 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1022 #define sd_setup_next_xfer ssd_setup_next_xfer 1023 #define sd_dkio_get_temp ssd_dkio_get_temp 1024 #define sd_check_mhd ssd_check_mhd 1025 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1026 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1027 #define sd_sname ssd_sname 1028 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1029 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1030 #define sd_take_ownership ssd_take_ownership 1031 #define sd_reserve_release ssd_reserve_release 1032 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1033 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1034 #define sd_persistent_reservation_in_read_keys \ 1035 ssd_persistent_reservation_in_read_keys 1036 #define sd_persistent_reservation_in_read_resv \ 1037 ssd_persistent_reservation_in_read_resv 1038 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1039 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1040 #define sd_mhdioc_release ssd_mhdioc_release 1041 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1042 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1043 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1044 #define sr_change_blkmode ssr_change_blkmode 1045 #define sr_change_speed ssr_change_speed 1046 #define sr_atapi_change_speed ssr_atapi_change_speed 1047 #define sr_pause_resume ssr_pause_resume 1048 #define sr_play_msf ssr_play_msf 1049 #define sr_play_trkind ssr_play_trkind 1050 #define sr_read_all_subcodes ssr_read_all_subcodes 1051 #define sr_read_subchannel ssr_read_subchannel 1052 #define sr_read_tocentry ssr_read_tocentry 1053 #define sr_read_tochdr ssr_read_tochdr 1054 #define sr_read_cdda ssr_read_cdda 1055 #define sr_read_cdxa ssr_read_cdxa 1056 #define sr_read_mode1 ssr_read_mode1 1057 #define sr_read_mode2 ssr_read_mode2 1058 #define sr_read_cd_mode2 ssr_read_cd_mode2 1059 #define sr_sector_mode ssr_sector_mode 1060 #define sr_eject ssr_eject 1061 #define sr_ejected ssr_ejected 1062 #define sr_check_wp ssr_check_wp 1063 #define sd_check_media ssd_check_media 1064 #define sd_media_watch_cb ssd_media_watch_cb 1065 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1066 #define sr_volume_ctrl ssr_volume_ctrl 1067 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1068 #define sd_log_page_supported ssd_log_page_supported 1069 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1070 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1071 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1072 #define sd_range_lock ssd_range_lock 1073 #define sd_get_range ssd_get_range 1074 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1075 #define sd_range_unlock ssd_range_unlock 1076 #define sd_read_modify_write_task ssd_read_modify_write_task 1077 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1078 1079 #define sd_iostart_chain ssd_iostart_chain 1080 #define sd_iodone_chain ssd_iodone_chain 1081 #define sd_initpkt_map ssd_initpkt_map 1082 #define sd_destroypkt_map ssd_destroypkt_map 1083 #define sd_chain_type_map ssd_chain_type_map 1084 #define sd_chain_index_map ssd_chain_index_map 1085 1086 #define sd_failfast_flushctl ssd_failfast_flushctl 1087 #define sd_failfast_flushq ssd_failfast_flushq 1088 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1089 1090 #define sd_is_lsi ssd_is_lsi 1091 #define sd_tg_rdwr ssd_tg_rdwr 1092 #define sd_tg_getinfo ssd_tg_getinfo 1093 1094 #endif /* #if (defined(__fibre)) */ 1095 1096 1097 int _init(void); 1098 int _fini(void); 1099 int _info(struct modinfo *modinfop); 1100 1101 /*PRINTFLIKE3*/ 1102 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1103 /*PRINTFLIKE3*/ 1104 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1105 /*PRINTFLIKE3*/ 1106 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1107 1108 static int sdprobe(dev_info_t *devi); 1109 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1110 void **result); 1111 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1112 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1113 1114 /* 1115 * Smart probe for parallel scsi 1116 */ 1117 static void sd_scsi_probe_cache_init(void); 1118 static void sd_scsi_probe_cache_fini(void); 1119 static void sd_scsi_clear_probe_cache(void); 1120 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1121 1122 /* 1123 * Attached luns on target for parallel scsi 1124 */ 1125 static void sd_scsi_target_lun_init(void); 1126 static void sd_scsi_target_lun_fini(void); 1127 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1128 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1129 1130 static int sd_spin_up_unit(sd_ssc_t *ssc); 1131 1132 /* 1133 * Using sd_ssc_init to establish sd_ssc_t struct 1134 * Using sd_ssc_send to send uscsi internal command 1135 * Using sd_ssc_fini to free sd_ssc_t struct 1136 */ 1137 static sd_ssc_t *sd_ssc_init(struct sd_lun *un); 1138 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, 1139 int flag, enum uio_seg dataspace, int path_flag); 1140 static void sd_ssc_fini(sd_ssc_t *ssc); 1141 1142 /* 1143 * Using sd_ssc_assessment to set correct type-of-assessment 1144 * Using sd_ssc_post to post ereport & system log 1145 * sd_ssc_post will call sd_ssc_print to print system log 1146 * sd_ssc_post will call sd_ssd_ereport_post to post ereport 1147 */ 1148 static void sd_ssc_assessment(sd_ssc_t *ssc, 1149 enum sd_type_assessment tp_assess); 1150 1151 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess); 1152 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity); 1153 static void sd_ssc_ereport_post(sd_ssc_t *ssc, 1154 enum sd_driver_assessment drv_assess); 1155 1156 /* 1157 * Using sd_ssc_set_info to mark an un-decodable-data error. 1158 * Using sd_ssc_extract_info to transfer information from internal 1159 * data structures to sd_ssc_t. 1160 */ 1161 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, 1162 const char *fmt, ...); 1163 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, 1164 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp); 1165 1166 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1167 enum uio_seg dataspace, int path_flag); 1168 1169 #ifdef _LP64 1170 static void sd_enable_descr_sense(sd_ssc_t *ssc); 1171 static void sd_reenable_dsense_task(void *arg); 1172 #endif /* _LP64 */ 1173 1174 static void sd_set_mmc_caps(sd_ssc_t *ssc); 1175 1176 static void sd_read_unit_properties(struct sd_lun *un); 1177 static int sd_process_sdconf_file(struct sd_lun *un); 1178 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str); 1179 static char *sd_strtok_r(char *string, const char *sepset, char **lasts); 1180 static void sd_set_properties(struct sd_lun *un, char *name, char *value); 1181 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1182 int *data_list, sd_tunables *values); 1183 static void sd_process_sdconf_table(struct sd_lun *un); 1184 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1185 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1186 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1187 int list_len, char *dataname_ptr); 1188 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1189 sd_tunables *prop_list); 1190 1191 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, 1192 int reservation_flag); 1193 static int sd_get_devid(sd_ssc_t *ssc); 1194 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc); 1195 static int sd_write_deviceid(sd_ssc_t *ssc); 1196 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1197 static int sd_check_vpd_page_support(sd_ssc_t *ssc); 1198 1199 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi); 1200 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1201 1202 static int sd_ddi_suspend(dev_info_t *devi); 1203 static int sd_ddi_pm_suspend(struct sd_lun *un); 1204 static int sd_ddi_resume(dev_info_t *devi); 1205 static int sd_ddi_pm_resume(struct sd_lun *un); 1206 static int sdpower(dev_info_t *devi, int component, int level); 1207 1208 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1209 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1210 static int sd_unit_attach(dev_info_t *devi); 1211 static int sd_unit_detach(dev_info_t *devi); 1212 1213 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1214 static void sd_create_errstats(struct sd_lun *un, int instance); 1215 static void sd_set_errstats(struct sd_lun *un); 1216 static void sd_set_pstats(struct sd_lun *un); 1217 1218 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1219 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1220 static int sd_send_polled_RQS(struct sd_lun *un); 1221 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1222 1223 #if (defined(__fibre)) 1224 /* 1225 * Event callbacks (photon) 1226 */ 1227 static void sd_init_event_callbacks(struct sd_lun *un); 1228 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1229 #endif 1230 1231 /* 1232 * Defines for sd_cache_control 1233 */ 1234 1235 #define SD_CACHE_ENABLE 1 1236 #define SD_CACHE_DISABLE 0 1237 #define SD_CACHE_NOCHANGE -1 1238 1239 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag); 1240 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled); 1241 static void sd_get_nv_sup(sd_ssc_t *ssc); 1242 static dev_t sd_make_device(dev_info_t *devi); 1243 1244 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1245 uint64_t capacity); 1246 1247 /* 1248 * Driver entry point functions. 1249 */ 1250 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1251 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1252 static int sd_ready_and_valid(sd_ssc_t *ssc, int part); 1253 1254 static void sdmin(struct buf *bp); 1255 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1256 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1257 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1258 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1259 1260 static int sdstrategy(struct buf *bp); 1261 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1262 1263 /* 1264 * Function prototypes for layering functions in the iostart chain. 1265 */ 1266 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1267 struct buf *bp); 1268 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1269 struct buf *bp); 1270 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1271 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1272 struct buf *bp); 1273 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1274 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1275 1276 /* 1277 * Function prototypes for layering functions in the iodone chain. 1278 */ 1279 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1280 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1281 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1282 struct buf *bp); 1283 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1284 struct buf *bp); 1285 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1286 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1287 struct buf *bp); 1288 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1289 1290 /* 1291 * Prototypes for functions to support buf(9S) based IO. 1292 */ 1293 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1294 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1295 static void sd_destroypkt_for_buf(struct buf *); 1296 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1297 struct buf *bp, int flags, 1298 int (*callback)(caddr_t), caddr_t callback_arg, 1299 diskaddr_t lba, uint32_t blockcount); 1300 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1301 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1302 1303 /* 1304 * Prototypes for functions to support USCSI IO. 1305 */ 1306 static int sd_uscsi_strategy(struct buf *bp); 1307 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1308 static void sd_destroypkt_for_uscsi(struct buf *); 1309 1310 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1311 uchar_t chain_type, void *pktinfop); 1312 1313 static int sd_pm_entry(struct sd_lun *un); 1314 static void sd_pm_exit(struct sd_lun *un); 1315 1316 static void sd_pm_idletimeout_handler(void *arg); 1317 1318 /* 1319 * sd_core internal functions (used at the sd_core_io layer). 1320 */ 1321 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1322 static void sdintr(struct scsi_pkt *pktp); 1323 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1324 1325 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1326 enum uio_seg dataspace, int path_flag); 1327 1328 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1329 daddr_t blkno, int (*func)(struct buf *)); 1330 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1331 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1332 static void sd_bioclone_free(struct buf *bp); 1333 static void sd_shadow_buf_free(struct buf *bp); 1334 1335 static void sd_print_transport_rejected_message(struct sd_lun *un, 1336 struct sd_xbuf *xp, int code); 1337 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1338 void *arg, int code); 1339 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1340 void *arg, int code); 1341 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1342 void *arg, int code); 1343 1344 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1345 int retry_check_flag, 1346 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1347 int c), 1348 void *user_arg, int failure_code, clock_t retry_delay, 1349 void (*statp)(kstat_io_t *)); 1350 1351 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1352 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1353 1354 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1355 struct scsi_pkt *pktp); 1356 static void sd_start_retry_command(void *arg); 1357 static void sd_start_direct_priority_command(void *arg); 1358 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1359 int errcode); 1360 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1361 struct buf *bp, int errcode); 1362 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1363 static void sd_sync_with_callback(struct sd_lun *un); 1364 static int sdrunout(caddr_t arg); 1365 1366 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1367 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1368 1369 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1370 static void sd_restore_throttle(void *arg); 1371 1372 static void sd_init_cdb_limits(struct sd_lun *un); 1373 1374 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1375 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1376 1377 /* 1378 * Error handling functions 1379 */ 1380 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1381 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1382 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1383 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1384 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1385 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1386 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1387 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1388 1389 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1390 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1391 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1392 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1393 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1394 struct sd_xbuf *xp, size_t actual_len); 1395 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1396 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1397 1398 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1399 void *arg, int code); 1400 1401 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1402 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1403 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1404 uint8_t *sense_datap, 1405 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1406 static void sd_sense_key_not_ready(struct sd_lun *un, 1407 uint8_t *sense_datap, 1408 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1409 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1410 uint8_t *sense_datap, 1411 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1412 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1413 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1414 static void sd_sense_key_unit_attention(struct sd_lun *un, 1415 uint8_t *sense_datap, 1416 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1417 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1418 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1419 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1420 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1421 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1422 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1423 static void sd_sense_key_default(struct sd_lun *un, 1424 uint8_t *sense_datap, 1425 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1426 1427 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1428 void *arg, int flag); 1429 1430 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1431 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1432 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1433 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1434 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1435 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1436 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1437 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1438 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1439 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1440 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1441 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1442 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1443 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1444 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1445 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1446 1447 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1448 1449 static void sd_start_stop_unit_callback(void *arg); 1450 static void sd_start_stop_unit_task(void *arg); 1451 1452 static void sd_taskq_create(void); 1453 static void sd_taskq_delete(void); 1454 static void sd_target_change_task(void *arg); 1455 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag); 1456 static void sd_media_change_task(void *arg); 1457 1458 static int sd_handle_mchange(struct sd_lun *un); 1459 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag); 1460 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, 1461 uint32_t *lbap, int path_flag); 1462 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 1463 uint32_t *lbap, int path_flag); 1464 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int flag, 1465 int path_flag); 1466 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, 1467 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1468 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag); 1469 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, 1470 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1471 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, 1472 uchar_t usr_cmd, uchar_t *usr_bufp); 1473 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1474 struct dk_callback *dkc); 1475 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1476 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, 1477 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1478 uchar_t *bufaddr, uint_t buflen, int path_flag); 1479 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 1480 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1481 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1482 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, 1483 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1484 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, 1485 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1486 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 1487 size_t buflen, daddr_t start_block, int path_flag); 1488 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \ 1489 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \ 1490 path_flag) 1491 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\ 1492 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\ 1493 path_flag) 1494 1495 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, 1496 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1497 uint16_t param_ptr, int path_flag); 1498 1499 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1500 static void sd_free_rqs(struct sd_lun *un); 1501 1502 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1503 uchar_t *data, int len, int fmt); 1504 static void sd_panic_for_res_conflict(struct sd_lun *un); 1505 1506 /* 1507 * Disk Ioctl Function Prototypes 1508 */ 1509 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1510 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1511 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1512 1513 /* 1514 * Multi-host Ioctl Prototypes 1515 */ 1516 static int sd_check_mhd(dev_t dev, int interval); 1517 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1518 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1519 static char *sd_sname(uchar_t status); 1520 static void sd_mhd_resvd_recover(void *arg); 1521 static void sd_resv_reclaim_thread(); 1522 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1523 static int sd_reserve_release(dev_t dev, int cmd); 1524 static void sd_rmv_resv_reclaim_req(dev_t dev); 1525 static void sd_mhd_reset_notify_cb(caddr_t arg); 1526 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1527 mhioc_inkeys_t *usrp, int flag); 1528 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1529 mhioc_inresvs_t *usrp, int flag); 1530 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1531 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1532 static int sd_mhdioc_release(dev_t dev); 1533 static int sd_mhdioc_register_devid(dev_t dev); 1534 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1535 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1536 1537 /* 1538 * SCSI removable prototypes 1539 */ 1540 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1541 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1542 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1543 static int sr_pause_resume(dev_t dev, int mode); 1544 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1545 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1546 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1547 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1548 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1549 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1550 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1551 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1552 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1553 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1554 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1555 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1556 static int sr_eject(dev_t dev); 1557 static void sr_ejected(register struct sd_lun *un); 1558 static int sr_check_wp(dev_t dev); 1559 static int sd_check_media(dev_t dev, enum dkio_state state); 1560 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1561 static void sd_delayed_cv_broadcast(void *arg); 1562 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1563 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1564 1565 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page); 1566 1567 /* 1568 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1569 */ 1570 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag); 1571 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1572 static void sd_wm_cache_destructor(void *wm, void *un); 1573 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1574 daddr_t endb, ushort_t typ); 1575 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1576 daddr_t endb); 1577 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1578 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1579 static void sd_read_modify_write_task(void * arg); 1580 static int 1581 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1582 struct buf **bpp); 1583 1584 1585 /* 1586 * Function prototypes for failfast support. 1587 */ 1588 static void sd_failfast_flushq(struct sd_lun *un); 1589 static int sd_failfast_flushq_callback(struct buf *bp); 1590 1591 /* 1592 * Function prototypes to check for lsi devices 1593 */ 1594 static void sd_is_lsi(struct sd_lun *un); 1595 1596 /* 1597 * Function prototypes for partial DMA support 1598 */ 1599 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1600 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1601 1602 1603 /* Function prototypes for cmlb */ 1604 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1605 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1606 1607 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1608 1609 /* 1610 * Constants for failfast support: 1611 * 1612 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1613 * failfast processing being performed. 1614 * 1615 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1616 * failfast processing on all bufs with B_FAILFAST set. 1617 */ 1618 1619 #define SD_FAILFAST_INACTIVE 0 1620 #define SD_FAILFAST_ACTIVE 1 1621 1622 /* 1623 * Bitmask to control behavior of buf(9S) flushes when a transition to 1624 * the failfast state occurs. Optional bits include: 1625 * 1626 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1627 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1628 * be flushed. 1629 * 1630 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1631 * driver, in addition to the regular wait queue. This includes the xbuf 1632 * queues. When clear, only the driver's wait queue will be flushed. 1633 */ 1634 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1635 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1636 1637 /* 1638 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1639 * to flush all queues within the driver. 1640 */ 1641 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1642 1643 1644 /* 1645 * SD Testing Fault Injection 1646 */ 1647 #ifdef SD_FAULT_INJECTION 1648 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1649 static void sd_faultinjection(struct scsi_pkt *pktp); 1650 static void sd_injection_log(char *buf, struct sd_lun *un); 1651 #endif 1652 1653 /* 1654 * Device driver ops vector 1655 */ 1656 static struct cb_ops sd_cb_ops = { 1657 sdopen, /* open */ 1658 sdclose, /* close */ 1659 sdstrategy, /* strategy */ 1660 nodev, /* print */ 1661 sddump, /* dump */ 1662 sdread, /* read */ 1663 sdwrite, /* write */ 1664 sdioctl, /* ioctl */ 1665 nodev, /* devmap */ 1666 nodev, /* mmap */ 1667 nodev, /* segmap */ 1668 nochpoll, /* poll */ 1669 sd_prop_op, /* cb_prop_op */ 1670 0, /* streamtab */ 1671 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1672 CB_REV, /* cb_rev */ 1673 sdaread, /* async I/O read entry point */ 1674 sdawrite /* async I/O write entry point */ 1675 }; 1676 1677 static struct dev_ops sd_ops = { 1678 DEVO_REV, /* devo_rev, */ 1679 0, /* refcnt */ 1680 sdinfo, /* info */ 1681 nulldev, /* identify */ 1682 sdprobe, /* probe */ 1683 sdattach, /* attach */ 1684 sddetach, /* detach */ 1685 nodev, /* reset */ 1686 &sd_cb_ops, /* driver operations */ 1687 NULL, /* bus operations */ 1688 sdpower, /* power */ 1689 ddi_quiesce_not_needed, /* quiesce */ 1690 }; 1691 1692 1693 /* 1694 * This is the loadable module wrapper. 1695 */ 1696 #include <sys/modctl.h> 1697 1698 static struct modldrv modldrv = { 1699 &mod_driverops, /* Type of module. This one is a driver */ 1700 SD_MODULE_NAME, /* Module name. */ 1701 &sd_ops /* driver ops */ 1702 }; 1703 1704 1705 static struct modlinkage modlinkage = { 1706 MODREV_1, 1707 &modldrv, 1708 NULL 1709 }; 1710 1711 static cmlb_tg_ops_t sd_tgops = { 1712 TG_DK_OPS_VERSION_1, 1713 sd_tg_rdwr, 1714 sd_tg_getinfo 1715 }; 1716 1717 static struct scsi_asq_key_strings sd_additional_codes[] = { 1718 0x81, 0, "Logical Unit is Reserved", 1719 0x85, 0, "Audio Address Not Valid", 1720 0xb6, 0, "Media Load Mechanism Failed", 1721 0xB9, 0, "Audio Play Operation Aborted", 1722 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1723 0x53, 2, "Medium removal prevented", 1724 0x6f, 0, "Authentication failed during key exchange", 1725 0x6f, 1, "Key not present", 1726 0x6f, 2, "Key not established", 1727 0x6f, 3, "Read without proper authentication", 1728 0x6f, 4, "Mismatched region to this logical unit", 1729 0x6f, 5, "Region reset count error", 1730 0xffff, 0x0, NULL 1731 }; 1732 1733 1734 /* 1735 * Struct for passing printing information for sense data messages 1736 */ 1737 struct sd_sense_info { 1738 int ssi_severity; 1739 int ssi_pfa_flag; 1740 }; 1741 1742 /* 1743 * Table of function pointers for iostart-side routines. Separate "chains" 1744 * of layered function calls are formed by placing the function pointers 1745 * sequentially in the desired order. Functions are called according to an 1746 * incrementing table index ordering. The last function in each chain must 1747 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1748 * in the sd_iodone_chain[] array. 1749 * 1750 * Note: It may seem more natural to organize both the iostart and iodone 1751 * functions together, into an array of structures (or some similar 1752 * organization) with a common index, rather than two separate arrays which 1753 * must be maintained in synchronization. The purpose of this division is 1754 * to achieve improved performance: individual arrays allows for more 1755 * effective cache line utilization on certain platforms. 1756 */ 1757 1758 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1759 1760 1761 static sd_chain_t sd_iostart_chain[] = { 1762 1763 /* Chain for buf IO for disk drive targets (PM enabled) */ 1764 sd_mapblockaddr_iostart, /* Index: 0 */ 1765 sd_pm_iostart, /* Index: 1 */ 1766 sd_core_iostart, /* Index: 2 */ 1767 1768 /* Chain for buf IO for disk drive targets (PM disabled) */ 1769 sd_mapblockaddr_iostart, /* Index: 3 */ 1770 sd_core_iostart, /* Index: 4 */ 1771 1772 /* Chain for buf IO for removable-media targets (PM enabled) */ 1773 sd_mapblockaddr_iostart, /* Index: 5 */ 1774 sd_mapblocksize_iostart, /* Index: 6 */ 1775 sd_pm_iostart, /* Index: 7 */ 1776 sd_core_iostart, /* Index: 8 */ 1777 1778 /* Chain for buf IO for removable-media targets (PM disabled) */ 1779 sd_mapblockaddr_iostart, /* Index: 9 */ 1780 sd_mapblocksize_iostart, /* Index: 10 */ 1781 sd_core_iostart, /* Index: 11 */ 1782 1783 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1784 sd_mapblockaddr_iostart, /* Index: 12 */ 1785 sd_checksum_iostart, /* Index: 13 */ 1786 sd_pm_iostart, /* Index: 14 */ 1787 sd_core_iostart, /* Index: 15 */ 1788 1789 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1790 sd_mapblockaddr_iostart, /* Index: 16 */ 1791 sd_checksum_iostart, /* Index: 17 */ 1792 sd_core_iostart, /* Index: 18 */ 1793 1794 /* Chain for USCSI commands (all targets) */ 1795 sd_pm_iostart, /* Index: 19 */ 1796 sd_core_iostart, /* Index: 20 */ 1797 1798 /* Chain for checksumming USCSI commands (all targets) */ 1799 sd_checksum_uscsi_iostart, /* Index: 21 */ 1800 sd_pm_iostart, /* Index: 22 */ 1801 sd_core_iostart, /* Index: 23 */ 1802 1803 /* Chain for "direct" USCSI commands (all targets) */ 1804 sd_core_iostart, /* Index: 24 */ 1805 1806 /* Chain for "direct priority" USCSI commands (all targets) */ 1807 sd_core_iostart, /* Index: 25 */ 1808 }; 1809 1810 /* 1811 * Macros to locate the first function of each iostart chain in the 1812 * sd_iostart_chain[] array. These are located by the index in the array. 1813 */ 1814 #define SD_CHAIN_DISK_IOSTART 0 1815 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1816 #define SD_CHAIN_RMMEDIA_IOSTART 5 1817 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1818 #define SD_CHAIN_CHKSUM_IOSTART 12 1819 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1820 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1821 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1822 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1823 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1824 1825 1826 /* 1827 * Table of function pointers for the iodone-side routines for the driver- 1828 * internal layering mechanism. The calling sequence for iodone routines 1829 * uses a decrementing table index, so the last routine called in a chain 1830 * must be at the lowest array index location for that chain. The last 1831 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1832 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1833 * of the functions in an iodone side chain must correspond to the ordering 1834 * of the iostart routines for that chain. Note that there is no iodone 1835 * side routine that corresponds to sd_core_iostart(), so there is no 1836 * entry in the table for this. 1837 */ 1838 1839 static sd_chain_t sd_iodone_chain[] = { 1840 1841 /* Chain for buf IO for disk drive targets (PM enabled) */ 1842 sd_buf_iodone, /* Index: 0 */ 1843 sd_mapblockaddr_iodone, /* Index: 1 */ 1844 sd_pm_iodone, /* Index: 2 */ 1845 1846 /* Chain for buf IO for disk drive targets (PM disabled) */ 1847 sd_buf_iodone, /* Index: 3 */ 1848 sd_mapblockaddr_iodone, /* Index: 4 */ 1849 1850 /* Chain for buf IO for removable-media targets (PM enabled) */ 1851 sd_buf_iodone, /* Index: 5 */ 1852 sd_mapblockaddr_iodone, /* Index: 6 */ 1853 sd_mapblocksize_iodone, /* Index: 7 */ 1854 sd_pm_iodone, /* Index: 8 */ 1855 1856 /* Chain for buf IO for removable-media targets (PM disabled) */ 1857 sd_buf_iodone, /* Index: 9 */ 1858 sd_mapblockaddr_iodone, /* Index: 10 */ 1859 sd_mapblocksize_iodone, /* Index: 11 */ 1860 1861 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1862 sd_buf_iodone, /* Index: 12 */ 1863 sd_mapblockaddr_iodone, /* Index: 13 */ 1864 sd_checksum_iodone, /* Index: 14 */ 1865 sd_pm_iodone, /* Index: 15 */ 1866 1867 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1868 sd_buf_iodone, /* Index: 16 */ 1869 sd_mapblockaddr_iodone, /* Index: 17 */ 1870 sd_checksum_iodone, /* Index: 18 */ 1871 1872 /* Chain for USCSI commands (non-checksum targets) */ 1873 sd_uscsi_iodone, /* Index: 19 */ 1874 sd_pm_iodone, /* Index: 20 */ 1875 1876 /* Chain for USCSI commands (checksum targets) */ 1877 sd_uscsi_iodone, /* Index: 21 */ 1878 sd_checksum_uscsi_iodone, /* Index: 22 */ 1879 sd_pm_iodone, /* Index: 22 */ 1880 1881 /* Chain for "direct" USCSI commands (all targets) */ 1882 sd_uscsi_iodone, /* Index: 24 */ 1883 1884 /* Chain for "direct priority" USCSI commands (all targets) */ 1885 sd_uscsi_iodone, /* Index: 25 */ 1886 }; 1887 1888 1889 /* 1890 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1891 * each iodone-side chain. These are located by the array index, but as the 1892 * iodone side functions are called in a decrementing-index order, the 1893 * highest index number in each chain must be specified (as these correspond 1894 * to the first function in the iodone chain that will be called by the core 1895 * at IO completion time). 1896 */ 1897 1898 #define SD_CHAIN_DISK_IODONE 2 1899 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1900 #define SD_CHAIN_RMMEDIA_IODONE 8 1901 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1902 #define SD_CHAIN_CHKSUM_IODONE 15 1903 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1904 #define SD_CHAIN_USCSI_CMD_IODONE 20 1905 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1906 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1907 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1908 1909 1910 1911 1912 /* 1913 * Array to map a layering chain index to the appropriate initpkt routine. 1914 * The redundant entries are present so that the index used for accessing 1915 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1916 * with this table as well. 1917 */ 1918 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1919 1920 static sd_initpkt_t sd_initpkt_map[] = { 1921 1922 /* Chain for buf IO for disk drive targets (PM enabled) */ 1923 sd_initpkt_for_buf, /* Index: 0 */ 1924 sd_initpkt_for_buf, /* Index: 1 */ 1925 sd_initpkt_for_buf, /* Index: 2 */ 1926 1927 /* Chain for buf IO for disk drive targets (PM disabled) */ 1928 sd_initpkt_for_buf, /* Index: 3 */ 1929 sd_initpkt_for_buf, /* Index: 4 */ 1930 1931 /* Chain for buf IO for removable-media targets (PM enabled) */ 1932 sd_initpkt_for_buf, /* Index: 5 */ 1933 sd_initpkt_for_buf, /* Index: 6 */ 1934 sd_initpkt_for_buf, /* Index: 7 */ 1935 sd_initpkt_for_buf, /* Index: 8 */ 1936 1937 /* Chain for buf IO for removable-media targets (PM disabled) */ 1938 sd_initpkt_for_buf, /* Index: 9 */ 1939 sd_initpkt_for_buf, /* Index: 10 */ 1940 sd_initpkt_for_buf, /* Index: 11 */ 1941 1942 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1943 sd_initpkt_for_buf, /* Index: 12 */ 1944 sd_initpkt_for_buf, /* Index: 13 */ 1945 sd_initpkt_for_buf, /* Index: 14 */ 1946 sd_initpkt_for_buf, /* Index: 15 */ 1947 1948 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1949 sd_initpkt_for_buf, /* Index: 16 */ 1950 sd_initpkt_for_buf, /* Index: 17 */ 1951 sd_initpkt_for_buf, /* Index: 18 */ 1952 1953 /* Chain for USCSI commands (non-checksum targets) */ 1954 sd_initpkt_for_uscsi, /* Index: 19 */ 1955 sd_initpkt_for_uscsi, /* Index: 20 */ 1956 1957 /* Chain for USCSI commands (checksum targets) */ 1958 sd_initpkt_for_uscsi, /* Index: 21 */ 1959 sd_initpkt_for_uscsi, /* Index: 22 */ 1960 sd_initpkt_for_uscsi, /* Index: 22 */ 1961 1962 /* Chain for "direct" USCSI commands (all targets) */ 1963 sd_initpkt_for_uscsi, /* Index: 24 */ 1964 1965 /* Chain for "direct priority" USCSI commands (all targets) */ 1966 sd_initpkt_for_uscsi, /* Index: 25 */ 1967 1968 }; 1969 1970 1971 /* 1972 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1973 * The redundant entries are present so that the index used for accessing 1974 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1975 * with this table as well. 1976 */ 1977 typedef void (*sd_destroypkt_t)(struct buf *); 1978 1979 static sd_destroypkt_t sd_destroypkt_map[] = { 1980 1981 /* Chain for buf IO for disk drive targets (PM enabled) */ 1982 sd_destroypkt_for_buf, /* Index: 0 */ 1983 sd_destroypkt_for_buf, /* Index: 1 */ 1984 sd_destroypkt_for_buf, /* Index: 2 */ 1985 1986 /* Chain for buf IO for disk drive targets (PM disabled) */ 1987 sd_destroypkt_for_buf, /* Index: 3 */ 1988 sd_destroypkt_for_buf, /* Index: 4 */ 1989 1990 /* Chain for buf IO for removable-media targets (PM enabled) */ 1991 sd_destroypkt_for_buf, /* Index: 5 */ 1992 sd_destroypkt_for_buf, /* Index: 6 */ 1993 sd_destroypkt_for_buf, /* Index: 7 */ 1994 sd_destroypkt_for_buf, /* Index: 8 */ 1995 1996 /* Chain for buf IO for removable-media targets (PM disabled) */ 1997 sd_destroypkt_for_buf, /* Index: 9 */ 1998 sd_destroypkt_for_buf, /* Index: 10 */ 1999 sd_destroypkt_for_buf, /* Index: 11 */ 2000 2001 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2002 sd_destroypkt_for_buf, /* Index: 12 */ 2003 sd_destroypkt_for_buf, /* Index: 13 */ 2004 sd_destroypkt_for_buf, /* Index: 14 */ 2005 sd_destroypkt_for_buf, /* Index: 15 */ 2006 2007 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2008 sd_destroypkt_for_buf, /* Index: 16 */ 2009 sd_destroypkt_for_buf, /* Index: 17 */ 2010 sd_destroypkt_for_buf, /* Index: 18 */ 2011 2012 /* Chain for USCSI commands (non-checksum targets) */ 2013 sd_destroypkt_for_uscsi, /* Index: 19 */ 2014 sd_destroypkt_for_uscsi, /* Index: 20 */ 2015 2016 /* Chain for USCSI commands (checksum targets) */ 2017 sd_destroypkt_for_uscsi, /* Index: 21 */ 2018 sd_destroypkt_for_uscsi, /* Index: 22 */ 2019 sd_destroypkt_for_uscsi, /* Index: 22 */ 2020 2021 /* Chain for "direct" USCSI commands (all targets) */ 2022 sd_destroypkt_for_uscsi, /* Index: 24 */ 2023 2024 /* Chain for "direct priority" USCSI commands (all targets) */ 2025 sd_destroypkt_for_uscsi, /* Index: 25 */ 2026 2027 }; 2028 2029 2030 2031 /* 2032 * Array to map a layering chain index to the appropriate chain "type". 2033 * The chain type indicates a specific property/usage of the chain. 2034 * The redundant entries are present so that the index used for accessing 2035 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2036 * with this table as well. 2037 */ 2038 2039 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2040 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2041 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2042 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2043 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2044 /* (for error recovery) */ 2045 2046 static int sd_chain_type_map[] = { 2047 2048 /* Chain for buf IO for disk drive targets (PM enabled) */ 2049 SD_CHAIN_BUFIO, /* Index: 0 */ 2050 SD_CHAIN_BUFIO, /* Index: 1 */ 2051 SD_CHAIN_BUFIO, /* Index: 2 */ 2052 2053 /* Chain for buf IO for disk drive targets (PM disabled) */ 2054 SD_CHAIN_BUFIO, /* Index: 3 */ 2055 SD_CHAIN_BUFIO, /* Index: 4 */ 2056 2057 /* Chain for buf IO for removable-media targets (PM enabled) */ 2058 SD_CHAIN_BUFIO, /* Index: 5 */ 2059 SD_CHAIN_BUFIO, /* Index: 6 */ 2060 SD_CHAIN_BUFIO, /* Index: 7 */ 2061 SD_CHAIN_BUFIO, /* Index: 8 */ 2062 2063 /* Chain for buf IO for removable-media targets (PM disabled) */ 2064 SD_CHAIN_BUFIO, /* Index: 9 */ 2065 SD_CHAIN_BUFIO, /* Index: 10 */ 2066 SD_CHAIN_BUFIO, /* Index: 11 */ 2067 2068 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2069 SD_CHAIN_BUFIO, /* Index: 12 */ 2070 SD_CHAIN_BUFIO, /* Index: 13 */ 2071 SD_CHAIN_BUFIO, /* Index: 14 */ 2072 SD_CHAIN_BUFIO, /* Index: 15 */ 2073 2074 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2075 SD_CHAIN_BUFIO, /* Index: 16 */ 2076 SD_CHAIN_BUFIO, /* Index: 17 */ 2077 SD_CHAIN_BUFIO, /* Index: 18 */ 2078 2079 /* Chain for USCSI commands (non-checksum targets) */ 2080 SD_CHAIN_USCSI, /* Index: 19 */ 2081 SD_CHAIN_USCSI, /* Index: 20 */ 2082 2083 /* Chain for USCSI commands (checksum targets) */ 2084 SD_CHAIN_USCSI, /* Index: 21 */ 2085 SD_CHAIN_USCSI, /* Index: 22 */ 2086 SD_CHAIN_USCSI, /* Index: 22 */ 2087 2088 /* Chain for "direct" USCSI commands (all targets) */ 2089 SD_CHAIN_DIRECT, /* Index: 24 */ 2090 2091 /* Chain for "direct priority" USCSI commands (all targets) */ 2092 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2093 }; 2094 2095 2096 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2097 #define SD_IS_BUFIO(xp) \ 2098 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2099 2100 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2101 #define SD_IS_DIRECT_PRIORITY(xp) \ 2102 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2103 2104 2105 2106 /* 2107 * Struct, array, and macros to map a specific chain to the appropriate 2108 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2109 * 2110 * The sd_chain_index_map[] array is used at attach time to set the various 2111 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2112 * chain to be used with the instance. This allows different instances to use 2113 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2114 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2115 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2116 * dynamically & without the use of locking; and (2) a layer may update the 2117 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2118 * to allow for deferred processing of an IO within the same chain from a 2119 * different execution context. 2120 */ 2121 2122 struct sd_chain_index { 2123 int sci_iostart_index; 2124 int sci_iodone_index; 2125 }; 2126 2127 static struct sd_chain_index sd_chain_index_map[] = { 2128 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2129 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2130 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2131 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2132 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2133 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2134 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2135 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2136 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2137 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2138 }; 2139 2140 2141 /* 2142 * The following are indexes into the sd_chain_index_map[] array. 2143 */ 2144 2145 /* un->un_buf_chain_type must be set to one of these */ 2146 #define SD_CHAIN_INFO_DISK 0 2147 #define SD_CHAIN_INFO_DISK_NO_PM 1 2148 #define SD_CHAIN_INFO_RMMEDIA 2 2149 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2150 #define SD_CHAIN_INFO_CHKSUM 4 2151 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2152 2153 /* un->un_uscsi_chain_type must be set to one of these */ 2154 #define SD_CHAIN_INFO_USCSI_CMD 6 2155 /* USCSI with PM disabled is the same as DIRECT */ 2156 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2157 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2158 2159 /* un->un_direct_chain_type must be set to one of these */ 2160 #define SD_CHAIN_INFO_DIRECT_CMD 8 2161 2162 /* un->un_priority_chain_type must be set to one of these */ 2163 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2164 2165 /* size for devid inquiries */ 2166 #define MAX_INQUIRY_SIZE 0xF0 2167 2168 /* 2169 * Macros used by functions to pass a given buf(9S) struct along to the 2170 * next function in the layering chain for further processing. 2171 * 2172 * In the following macros, passing more than three arguments to the called 2173 * routines causes the optimizer for the SPARC compiler to stop doing tail 2174 * call elimination which results in significant performance degradation. 2175 */ 2176 #define SD_BEGIN_IOSTART(index, un, bp) \ 2177 ((*(sd_iostart_chain[index]))(index, un, bp)) 2178 2179 #define SD_BEGIN_IODONE(index, un, bp) \ 2180 ((*(sd_iodone_chain[index]))(index, un, bp)) 2181 2182 #define SD_NEXT_IOSTART(index, un, bp) \ 2183 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2184 2185 #define SD_NEXT_IODONE(index, un, bp) \ 2186 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2187 2188 /* 2189 * Function: _init 2190 * 2191 * Description: This is the driver _init(9E) entry point. 2192 * 2193 * Return Code: Returns the value from mod_install(9F) or 2194 * ddi_soft_state_init(9F) as appropriate. 2195 * 2196 * Context: Called when driver module loaded. 2197 */ 2198 2199 int 2200 _init(void) 2201 { 2202 int err; 2203 2204 /* establish driver name from module name */ 2205 sd_label = (char *)mod_modname(&modlinkage); 2206 2207 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2208 SD_MAXUNIT); 2209 2210 if (err != 0) { 2211 return (err); 2212 } 2213 2214 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2215 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2216 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2217 2218 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2219 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2220 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2221 2222 /* 2223 * it's ok to init here even for fibre device 2224 */ 2225 sd_scsi_probe_cache_init(); 2226 2227 sd_scsi_target_lun_init(); 2228 2229 /* 2230 * Creating taskq before mod_install ensures that all callers (threads) 2231 * that enter the module after a successful mod_install encounter 2232 * a valid taskq. 2233 */ 2234 sd_taskq_create(); 2235 2236 err = mod_install(&modlinkage); 2237 if (err != 0) { 2238 /* delete taskq if install fails */ 2239 sd_taskq_delete(); 2240 2241 mutex_destroy(&sd_detach_mutex); 2242 mutex_destroy(&sd_log_mutex); 2243 mutex_destroy(&sd_label_mutex); 2244 2245 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2246 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2247 cv_destroy(&sd_tr.srq_inprocess_cv); 2248 2249 sd_scsi_probe_cache_fini(); 2250 2251 sd_scsi_target_lun_fini(); 2252 2253 ddi_soft_state_fini(&sd_state); 2254 return (err); 2255 } 2256 2257 return (err); 2258 } 2259 2260 2261 /* 2262 * Function: _fini 2263 * 2264 * Description: This is the driver _fini(9E) entry point. 2265 * 2266 * Return Code: Returns the value from mod_remove(9F) 2267 * 2268 * Context: Called when driver module is unloaded. 2269 */ 2270 2271 int 2272 _fini(void) 2273 { 2274 int err; 2275 2276 if ((err = mod_remove(&modlinkage)) != 0) { 2277 return (err); 2278 } 2279 2280 sd_taskq_delete(); 2281 2282 mutex_destroy(&sd_detach_mutex); 2283 mutex_destroy(&sd_log_mutex); 2284 mutex_destroy(&sd_label_mutex); 2285 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2286 2287 sd_scsi_probe_cache_fini(); 2288 2289 sd_scsi_target_lun_fini(); 2290 2291 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2292 cv_destroy(&sd_tr.srq_inprocess_cv); 2293 2294 ddi_soft_state_fini(&sd_state); 2295 2296 return (err); 2297 } 2298 2299 2300 /* 2301 * Function: _info 2302 * 2303 * Description: This is the driver _info(9E) entry point. 2304 * 2305 * Arguments: modinfop - pointer to the driver modinfo structure 2306 * 2307 * Return Code: Returns the value from mod_info(9F). 2308 * 2309 * Context: Kernel thread context 2310 */ 2311 2312 int 2313 _info(struct modinfo *modinfop) 2314 { 2315 return (mod_info(&modlinkage, modinfop)); 2316 } 2317 2318 2319 /* 2320 * The following routines implement the driver message logging facility. 2321 * They provide component- and level- based debug output filtering. 2322 * Output may also be restricted to messages for a single instance by 2323 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2324 * to NULL, then messages for all instances are printed. 2325 * 2326 * These routines have been cloned from each other due to the language 2327 * constraints of macros and variable argument list processing. 2328 */ 2329 2330 2331 /* 2332 * Function: sd_log_err 2333 * 2334 * Description: This routine is called by the SD_ERROR macro for debug 2335 * logging of error conditions. 2336 * 2337 * Arguments: comp - driver component being logged 2338 * dev - pointer to driver info structure 2339 * fmt - error string and format to be logged 2340 */ 2341 2342 static void 2343 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2344 { 2345 va_list ap; 2346 dev_info_t *dev; 2347 2348 ASSERT(un != NULL); 2349 dev = SD_DEVINFO(un); 2350 ASSERT(dev != NULL); 2351 2352 /* 2353 * Filter messages based on the global component and level masks. 2354 * Also print if un matches the value of sd_debug_un, or if 2355 * sd_debug_un is set to NULL. 2356 */ 2357 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2358 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2359 mutex_enter(&sd_log_mutex); 2360 va_start(ap, fmt); 2361 (void) vsprintf(sd_log_buf, fmt, ap); 2362 va_end(ap); 2363 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2364 mutex_exit(&sd_log_mutex); 2365 } 2366 #ifdef SD_FAULT_INJECTION 2367 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2368 if (un->sd_injection_mask & comp) { 2369 mutex_enter(&sd_log_mutex); 2370 va_start(ap, fmt); 2371 (void) vsprintf(sd_log_buf, fmt, ap); 2372 va_end(ap); 2373 sd_injection_log(sd_log_buf, un); 2374 mutex_exit(&sd_log_mutex); 2375 } 2376 #endif 2377 } 2378 2379 2380 /* 2381 * Function: sd_log_info 2382 * 2383 * Description: This routine is called by the SD_INFO macro for debug 2384 * logging of general purpose informational conditions. 2385 * 2386 * Arguments: comp - driver component being logged 2387 * dev - pointer to driver info structure 2388 * fmt - info string and format to be logged 2389 */ 2390 2391 static void 2392 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2393 { 2394 va_list ap; 2395 dev_info_t *dev; 2396 2397 ASSERT(un != NULL); 2398 dev = SD_DEVINFO(un); 2399 ASSERT(dev != NULL); 2400 2401 /* 2402 * Filter messages based on the global component and level masks. 2403 * Also print if un matches the value of sd_debug_un, or if 2404 * sd_debug_un is set to NULL. 2405 */ 2406 if ((sd_component_mask & component) && 2407 (sd_level_mask & SD_LOGMASK_INFO) && 2408 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2409 mutex_enter(&sd_log_mutex); 2410 va_start(ap, fmt); 2411 (void) vsprintf(sd_log_buf, fmt, ap); 2412 va_end(ap); 2413 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2414 mutex_exit(&sd_log_mutex); 2415 } 2416 #ifdef SD_FAULT_INJECTION 2417 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2418 if (un->sd_injection_mask & component) { 2419 mutex_enter(&sd_log_mutex); 2420 va_start(ap, fmt); 2421 (void) vsprintf(sd_log_buf, fmt, ap); 2422 va_end(ap); 2423 sd_injection_log(sd_log_buf, un); 2424 mutex_exit(&sd_log_mutex); 2425 } 2426 #endif 2427 } 2428 2429 2430 /* 2431 * Function: sd_log_trace 2432 * 2433 * Description: This routine is called by the SD_TRACE macro for debug 2434 * logging of trace conditions (i.e. function entry/exit). 2435 * 2436 * Arguments: comp - driver component being logged 2437 * dev - pointer to driver info structure 2438 * fmt - trace string and format to be logged 2439 */ 2440 2441 static void 2442 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2443 { 2444 va_list ap; 2445 dev_info_t *dev; 2446 2447 ASSERT(un != NULL); 2448 dev = SD_DEVINFO(un); 2449 ASSERT(dev != NULL); 2450 2451 /* 2452 * Filter messages based on the global component and level masks. 2453 * Also print if un matches the value of sd_debug_un, or if 2454 * sd_debug_un is set to NULL. 2455 */ 2456 if ((sd_component_mask & component) && 2457 (sd_level_mask & SD_LOGMASK_TRACE) && 2458 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2459 mutex_enter(&sd_log_mutex); 2460 va_start(ap, fmt); 2461 (void) vsprintf(sd_log_buf, fmt, ap); 2462 va_end(ap); 2463 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2464 mutex_exit(&sd_log_mutex); 2465 } 2466 #ifdef SD_FAULT_INJECTION 2467 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2468 if (un->sd_injection_mask & component) { 2469 mutex_enter(&sd_log_mutex); 2470 va_start(ap, fmt); 2471 (void) vsprintf(sd_log_buf, fmt, ap); 2472 va_end(ap); 2473 sd_injection_log(sd_log_buf, un); 2474 mutex_exit(&sd_log_mutex); 2475 } 2476 #endif 2477 } 2478 2479 2480 /* 2481 * Function: sdprobe 2482 * 2483 * Description: This is the driver probe(9e) entry point function. 2484 * 2485 * Arguments: devi - opaque device info handle 2486 * 2487 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2488 * DDI_PROBE_FAILURE: If the probe failed. 2489 * DDI_PROBE_PARTIAL: If the instance is not present now, 2490 * but may be present in the future. 2491 */ 2492 2493 static int 2494 sdprobe(dev_info_t *devi) 2495 { 2496 struct scsi_device *devp; 2497 int rval; 2498 int instance; 2499 2500 /* 2501 * if it wasn't for pln, sdprobe could actually be nulldev 2502 * in the "__fibre" case. 2503 */ 2504 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2505 return (DDI_PROBE_DONTCARE); 2506 } 2507 2508 devp = ddi_get_driver_private(devi); 2509 2510 if (devp == NULL) { 2511 /* Ooops... nexus driver is mis-configured... */ 2512 return (DDI_PROBE_FAILURE); 2513 } 2514 2515 instance = ddi_get_instance(devi); 2516 2517 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2518 return (DDI_PROBE_PARTIAL); 2519 } 2520 2521 /* 2522 * Call the SCSA utility probe routine to see if we actually 2523 * have a target at this SCSI nexus. 2524 */ 2525 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2526 case SCSIPROBE_EXISTS: 2527 switch (devp->sd_inq->inq_dtype) { 2528 case DTYPE_DIRECT: 2529 rval = DDI_PROBE_SUCCESS; 2530 break; 2531 case DTYPE_RODIRECT: 2532 /* CDs etc. Can be removable media */ 2533 rval = DDI_PROBE_SUCCESS; 2534 break; 2535 case DTYPE_OPTICAL: 2536 /* 2537 * Rewritable optical driver HP115AA 2538 * Can also be removable media 2539 */ 2540 2541 /* 2542 * Do not attempt to bind to DTYPE_OPTICAL if 2543 * pre solaris 9 sparc sd behavior is required 2544 * 2545 * If first time through and sd_dtype_optical_bind 2546 * has not been set in /etc/system check properties 2547 */ 2548 2549 if (sd_dtype_optical_bind < 0) { 2550 sd_dtype_optical_bind = ddi_prop_get_int 2551 (DDI_DEV_T_ANY, devi, 0, 2552 "optical-device-bind", 1); 2553 } 2554 2555 if (sd_dtype_optical_bind == 0) { 2556 rval = DDI_PROBE_FAILURE; 2557 } else { 2558 rval = DDI_PROBE_SUCCESS; 2559 } 2560 break; 2561 2562 case DTYPE_NOTPRESENT: 2563 default: 2564 rval = DDI_PROBE_FAILURE; 2565 break; 2566 } 2567 break; 2568 default: 2569 rval = DDI_PROBE_PARTIAL; 2570 break; 2571 } 2572 2573 /* 2574 * This routine checks for resource allocation prior to freeing, 2575 * so it will take care of the "smart probing" case where a 2576 * scsi_probe() may or may not have been issued and will *not* 2577 * free previously-freed resources. 2578 */ 2579 scsi_unprobe(devp); 2580 return (rval); 2581 } 2582 2583 2584 /* 2585 * Function: sdinfo 2586 * 2587 * Description: This is the driver getinfo(9e) entry point function. 2588 * Given the device number, return the devinfo pointer from 2589 * the scsi_device structure or the instance number 2590 * associated with the dev_t. 2591 * 2592 * Arguments: dip - pointer to device info structure 2593 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2594 * DDI_INFO_DEVT2INSTANCE) 2595 * arg - driver dev_t 2596 * resultp - user buffer for request response 2597 * 2598 * Return Code: DDI_SUCCESS 2599 * DDI_FAILURE 2600 */ 2601 /* ARGSUSED */ 2602 static int 2603 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2604 { 2605 struct sd_lun *un; 2606 dev_t dev; 2607 int instance; 2608 int error; 2609 2610 switch (infocmd) { 2611 case DDI_INFO_DEVT2DEVINFO: 2612 dev = (dev_t)arg; 2613 instance = SDUNIT(dev); 2614 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2615 return (DDI_FAILURE); 2616 } 2617 *result = (void *) SD_DEVINFO(un); 2618 error = DDI_SUCCESS; 2619 break; 2620 case DDI_INFO_DEVT2INSTANCE: 2621 dev = (dev_t)arg; 2622 instance = SDUNIT(dev); 2623 *result = (void *)(uintptr_t)instance; 2624 error = DDI_SUCCESS; 2625 break; 2626 default: 2627 error = DDI_FAILURE; 2628 } 2629 return (error); 2630 } 2631 2632 /* 2633 * Function: sd_prop_op 2634 * 2635 * Description: This is the driver prop_op(9e) entry point function. 2636 * Return the number of blocks for the partition in question 2637 * or forward the request to the property facilities. 2638 * 2639 * Arguments: dev - device number 2640 * dip - pointer to device info structure 2641 * prop_op - property operator 2642 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2643 * name - pointer to property name 2644 * valuep - pointer or address of the user buffer 2645 * lengthp - property length 2646 * 2647 * Return Code: DDI_PROP_SUCCESS 2648 * DDI_PROP_NOT_FOUND 2649 * DDI_PROP_UNDEFINED 2650 * DDI_PROP_NO_MEMORY 2651 * DDI_PROP_BUF_TOO_SMALL 2652 */ 2653 2654 static int 2655 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2656 char *name, caddr_t valuep, int *lengthp) 2657 { 2658 struct sd_lun *un; 2659 2660 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL) 2661 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2662 name, valuep, lengthp)); 2663 2664 return (cmlb_prop_op(un->un_cmlbhandle, 2665 dev, dip, prop_op, mod_flags, name, valuep, lengthp, 2666 SDPART(dev), (void *)SD_PATH_DIRECT)); 2667 } 2668 2669 /* 2670 * The following functions are for smart probing: 2671 * sd_scsi_probe_cache_init() 2672 * sd_scsi_probe_cache_fini() 2673 * sd_scsi_clear_probe_cache() 2674 * sd_scsi_probe_with_cache() 2675 */ 2676 2677 /* 2678 * Function: sd_scsi_probe_cache_init 2679 * 2680 * Description: Initializes the probe response cache mutex and head pointer. 2681 * 2682 * Context: Kernel thread context 2683 */ 2684 2685 static void 2686 sd_scsi_probe_cache_init(void) 2687 { 2688 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2689 sd_scsi_probe_cache_head = NULL; 2690 } 2691 2692 2693 /* 2694 * Function: sd_scsi_probe_cache_fini 2695 * 2696 * Description: Frees all resources associated with the probe response cache. 2697 * 2698 * Context: Kernel thread context 2699 */ 2700 2701 static void 2702 sd_scsi_probe_cache_fini(void) 2703 { 2704 struct sd_scsi_probe_cache *cp; 2705 struct sd_scsi_probe_cache *ncp; 2706 2707 /* Clean up our smart probing linked list */ 2708 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2709 ncp = cp->next; 2710 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2711 } 2712 sd_scsi_probe_cache_head = NULL; 2713 mutex_destroy(&sd_scsi_probe_cache_mutex); 2714 } 2715 2716 2717 /* 2718 * Function: sd_scsi_clear_probe_cache 2719 * 2720 * Description: This routine clears the probe response cache. This is 2721 * done when open() returns ENXIO so that when deferred 2722 * attach is attempted (possibly after a device has been 2723 * turned on) we will retry the probe. Since we don't know 2724 * which target we failed to open, we just clear the 2725 * entire cache. 2726 * 2727 * Context: Kernel thread context 2728 */ 2729 2730 static void 2731 sd_scsi_clear_probe_cache(void) 2732 { 2733 struct sd_scsi_probe_cache *cp; 2734 int i; 2735 2736 mutex_enter(&sd_scsi_probe_cache_mutex); 2737 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2738 /* 2739 * Reset all entries to SCSIPROBE_EXISTS. This will 2740 * force probing to be performed the next time 2741 * sd_scsi_probe_with_cache is called. 2742 */ 2743 for (i = 0; i < NTARGETS_WIDE; i++) { 2744 cp->cache[i] = SCSIPROBE_EXISTS; 2745 } 2746 } 2747 mutex_exit(&sd_scsi_probe_cache_mutex); 2748 } 2749 2750 2751 /* 2752 * Function: sd_scsi_probe_with_cache 2753 * 2754 * Description: This routine implements support for a scsi device probe 2755 * with cache. The driver maintains a cache of the target 2756 * responses to scsi probes. If we get no response from a 2757 * target during a probe inquiry, we remember that, and we 2758 * avoid additional calls to scsi_probe on non-zero LUNs 2759 * on the same target until the cache is cleared. By doing 2760 * so we avoid the 1/4 sec selection timeout for nonzero 2761 * LUNs. lun0 of a target is always probed. 2762 * 2763 * Arguments: devp - Pointer to a scsi_device(9S) structure 2764 * waitfunc - indicates what the allocator routines should 2765 * do when resources are not available. This value 2766 * is passed on to scsi_probe() when that routine 2767 * is called. 2768 * 2769 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2770 * otherwise the value returned by scsi_probe(9F). 2771 * 2772 * Context: Kernel thread context 2773 */ 2774 2775 static int 2776 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2777 { 2778 struct sd_scsi_probe_cache *cp; 2779 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2780 int lun, tgt; 2781 2782 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2783 SCSI_ADDR_PROP_LUN, 0); 2784 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2785 SCSI_ADDR_PROP_TARGET, -1); 2786 2787 /* Make sure caching enabled and target in range */ 2788 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2789 /* do it the old way (no cache) */ 2790 return (scsi_probe(devp, waitfn)); 2791 } 2792 2793 mutex_enter(&sd_scsi_probe_cache_mutex); 2794 2795 /* Find the cache for this scsi bus instance */ 2796 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2797 if (cp->pdip == pdip) { 2798 break; 2799 } 2800 } 2801 2802 /* If we can't find a cache for this pdip, create one */ 2803 if (cp == NULL) { 2804 int i; 2805 2806 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2807 KM_SLEEP); 2808 cp->pdip = pdip; 2809 cp->next = sd_scsi_probe_cache_head; 2810 sd_scsi_probe_cache_head = cp; 2811 for (i = 0; i < NTARGETS_WIDE; i++) { 2812 cp->cache[i] = SCSIPROBE_EXISTS; 2813 } 2814 } 2815 2816 mutex_exit(&sd_scsi_probe_cache_mutex); 2817 2818 /* Recompute the cache for this target if LUN zero */ 2819 if (lun == 0) { 2820 cp->cache[tgt] = SCSIPROBE_EXISTS; 2821 } 2822 2823 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2824 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2825 return (SCSIPROBE_NORESP); 2826 } 2827 2828 /* Do the actual probe; save & return the result */ 2829 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2830 } 2831 2832 2833 /* 2834 * Function: sd_scsi_target_lun_init 2835 * 2836 * Description: Initializes the attached lun chain mutex and head pointer. 2837 * 2838 * Context: Kernel thread context 2839 */ 2840 2841 static void 2842 sd_scsi_target_lun_init(void) 2843 { 2844 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2845 sd_scsi_target_lun_head = NULL; 2846 } 2847 2848 2849 /* 2850 * Function: sd_scsi_target_lun_fini 2851 * 2852 * Description: Frees all resources associated with the attached lun 2853 * chain 2854 * 2855 * Context: Kernel thread context 2856 */ 2857 2858 static void 2859 sd_scsi_target_lun_fini(void) 2860 { 2861 struct sd_scsi_hba_tgt_lun *cp; 2862 struct sd_scsi_hba_tgt_lun *ncp; 2863 2864 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2865 ncp = cp->next; 2866 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2867 } 2868 sd_scsi_target_lun_head = NULL; 2869 mutex_destroy(&sd_scsi_target_lun_mutex); 2870 } 2871 2872 2873 /* 2874 * Function: sd_scsi_get_target_lun_count 2875 * 2876 * Description: This routine will check in the attached lun chain to see 2877 * how many luns are attached on the required SCSI controller 2878 * and target. Currently, some capabilities like tagged queue 2879 * are supported per target based by HBA. So all luns in a 2880 * target have the same capabilities. Based on this assumption, 2881 * sd should only set these capabilities once per target. This 2882 * function is called when sd needs to decide how many luns 2883 * already attached on a target. 2884 * 2885 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2886 * controller device. 2887 * target - The target ID on the controller's SCSI bus. 2888 * 2889 * Return Code: The number of luns attached on the required target and 2890 * controller. 2891 * -1 if target ID is not in parallel SCSI scope or the given 2892 * dip is not in the chain. 2893 * 2894 * Context: Kernel thread context 2895 */ 2896 2897 static int 2898 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2899 { 2900 struct sd_scsi_hba_tgt_lun *cp; 2901 2902 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2903 return (-1); 2904 } 2905 2906 mutex_enter(&sd_scsi_target_lun_mutex); 2907 2908 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2909 if (cp->pdip == dip) { 2910 break; 2911 } 2912 } 2913 2914 mutex_exit(&sd_scsi_target_lun_mutex); 2915 2916 if (cp == NULL) { 2917 return (-1); 2918 } 2919 2920 return (cp->nlun[target]); 2921 } 2922 2923 2924 /* 2925 * Function: sd_scsi_update_lun_on_target 2926 * 2927 * Description: This routine is used to update the attached lun chain when a 2928 * lun is attached or detached on a target. 2929 * 2930 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2931 * controller device. 2932 * target - The target ID on the controller's SCSI bus. 2933 * flag - Indicate the lun is attached or detached. 2934 * 2935 * Context: Kernel thread context 2936 */ 2937 2938 static void 2939 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 2940 { 2941 struct sd_scsi_hba_tgt_lun *cp; 2942 2943 mutex_enter(&sd_scsi_target_lun_mutex); 2944 2945 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2946 if (cp->pdip == dip) { 2947 break; 2948 } 2949 } 2950 2951 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 2952 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 2953 KM_SLEEP); 2954 cp->pdip = dip; 2955 cp->next = sd_scsi_target_lun_head; 2956 sd_scsi_target_lun_head = cp; 2957 } 2958 2959 mutex_exit(&sd_scsi_target_lun_mutex); 2960 2961 if (cp != NULL) { 2962 if (flag == SD_SCSI_LUN_ATTACH) { 2963 cp->nlun[target] ++; 2964 } else { 2965 cp->nlun[target] --; 2966 } 2967 } 2968 } 2969 2970 2971 /* 2972 * Function: sd_spin_up_unit 2973 * 2974 * Description: Issues the following commands to spin-up the device: 2975 * START STOP UNIT, and INQUIRY. 2976 * 2977 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 2978 * structure for this target. 2979 * 2980 * Return Code: 0 - success 2981 * EIO - failure 2982 * EACCES - reservation conflict 2983 * 2984 * Context: Kernel thread context 2985 */ 2986 2987 static int 2988 sd_spin_up_unit(sd_ssc_t *ssc) 2989 { 2990 size_t resid = 0; 2991 int has_conflict = FALSE; 2992 uchar_t *bufaddr; 2993 int status; 2994 struct sd_lun *un; 2995 2996 ASSERT(ssc != NULL); 2997 un = ssc->ssc_un; 2998 ASSERT(un != NULL); 2999 3000 /* 3001 * Send a throwaway START UNIT command. 3002 * 3003 * If we fail on this, we don't care presently what precisely 3004 * is wrong. EMC's arrays will also fail this with a check 3005 * condition (0x2/0x4/0x3) if the device is "inactive," but 3006 * we don't want to fail the attach because it may become 3007 * "active" later. 3008 */ 3009 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_START, 3010 SD_PATH_DIRECT); 3011 3012 if (status != 0) { 3013 if (status == EACCES) 3014 has_conflict = TRUE; 3015 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3016 } 3017 3018 /* 3019 * Send another INQUIRY command to the target. This is necessary for 3020 * non-removable media direct access devices because their INQUIRY data 3021 * may not be fully qualified until they are spun up (perhaps via the 3022 * START command above). Note: This seems to be needed for some 3023 * legacy devices only.) The INQUIRY command should succeed even if a 3024 * Reservation Conflict is present. 3025 */ 3026 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3027 3028 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid) 3029 != 0) { 3030 kmem_free(bufaddr, SUN_INQSIZE); 3031 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 3032 return (EIO); 3033 } 3034 3035 /* 3036 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3037 * Note that this routine does not return a failure here even if the 3038 * INQUIRY command did not return any data. This is a legacy behavior. 3039 */ 3040 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3041 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3042 } 3043 3044 kmem_free(bufaddr, SUN_INQSIZE); 3045 3046 /* If we hit a reservation conflict above, tell the caller. */ 3047 if (has_conflict == TRUE) { 3048 return (EACCES); 3049 } 3050 3051 return (0); 3052 } 3053 3054 #ifdef _LP64 3055 /* 3056 * Function: sd_enable_descr_sense 3057 * 3058 * Description: This routine attempts to select descriptor sense format 3059 * using the Control mode page. Devices that support 64 bit 3060 * LBAs (for >2TB luns) should also implement descriptor 3061 * sense data so we will call this function whenever we see 3062 * a lun larger than 2TB. If for some reason the device 3063 * supports 64 bit LBAs but doesn't support descriptor sense 3064 * presumably the mode select will fail. Everything will 3065 * continue to work normally except that we will not get 3066 * complete sense data for commands that fail with an LBA 3067 * larger than 32 bits. 3068 * 3069 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3070 * structure for this target. 3071 * 3072 * Context: Kernel thread context only 3073 */ 3074 3075 static void 3076 sd_enable_descr_sense(sd_ssc_t *ssc) 3077 { 3078 uchar_t *header; 3079 struct mode_control_scsi3 *ctrl_bufp; 3080 size_t buflen; 3081 size_t bd_len; 3082 int status; 3083 struct sd_lun *un; 3084 3085 ASSERT(ssc != NULL); 3086 un = ssc->ssc_un; 3087 ASSERT(un != NULL); 3088 3089 /* 3090 * Read MODE SENSE page 0xA, Control Mode Page 3091 */ 3092 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3093 sizeof (struct mode_control_scsi3); 3094 header = kmem_zalloc(buflen, KM_SLEEP); 3095 3096 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 3097 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT); 3098 3099 if (status != 0) { 3100 SD_ERROR(SD_LOG_COMMON, un, 3101 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3102 goto eds_exit; 3103 } 3104 3105 /* 3106 * Determine size of Block Descriptors in order to locate 3107 * the mode page data. ATAPI devices return 0, SCSI devices 3108 * should return MODE_BLK_DESC_LENGTH. 3109 */ 3110 bd_len = ((struct mode_header *)header)->bdesc_length; 3111 3112 /* Clear the mode data length field for MODE SELECT */ 3113 ((struct mode_header *)header)->length = 0; 3114 3115 ctrl_bufp = (struct mode_control_scsi3 *) 3116 (header + MODE_HEADER_LENGTH + bd_len); 3117 3118 /* 3119 * If the page length is smaller than the expected value, 3120 * the target device doesn't support D_SENSE. Bail out here. 3121 */ 3122 if (ctrl_bufp->mode_page.length < 3123 sizeof (struct mode_control_scsi3) - 2) { 3124 SD_ERROR(SD_LOG_COMMON, un, 3125 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3126 goto eds_exit; 3127 } 3128 3129 /* 3130 * Clear PS bit for MODE SELECT 3131 */ 3132 ctrl_bufp->mode_page.ps = 0; 3133 3134 /* 3135 * Set D_SENSE to enable descriptor sense format. 3136 */ 3137 ctrl_bufp->d_sense = 1; 3138 3139 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3140 3141 /* 3142 * Use MODE SELECT to commit the change to the D_SENSE bit 3143 */ 3144 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 3145 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT); 3146 3147 if (status != 0) { 3148 SD_INFO(SD_LOG_COMMON, un, 3149 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3150 } else { 3151 kmem_free(header, buflen); 3152 return; 3153 } 3154 3155 eds_exit: 3156 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3157 kmem_free(header, buflen); 3158 } 3159 3160 /* 3161 * Function: sd_reenable_dsense_task 3162 * 3163 * Description: Re-enable descriptor sense after device or bus reset 3164 * 3165 * Context: Executes in a taskq() thread context 3166 */ 3167 static void 3168 sd_reenable_dsense_task(void *arg) 3169 { 3170 struct sd_lun *un = arg; 3171 sd_ssc_t *ssc; 3172 3173 ASSERT(un != NULL); 3174 3175 ssc = sd_ssc_init(un); 3176 sd_enable_descr_sense(ssc); 3177 sd_ssc_fini(ssc); 3178 } 3179 #endif /* _LP64 */ 3180 3181 /* 3182 * Function: sd_set_mmc_caps 3183 * 3184 * Description: This routine determines if the device is MMC compliant and if 3185 * the device supports CDDA via a mode sense of the CDVD 3186 * capabilities mode page. Also checks if the device is a 3187 * dvdram writable device. 3188 * 3189 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 3190 * structure for this target. 3191 * 3192 * Context: Kernel thread context only 3193 */ 3194 3195 static void 3196 sd_set_mmc_caps(sd_ssc_t *ssc) 3197 { 3198 struct mode_header_grp2 *sense_mhp; 3199 uchar_t *sense_page; 3200 caddr_t buf; 3201 int bd_len; 3202 int status; 3203 struct uscsi_cmd com; 3204 int rtn; 3205 uchar_t *out_data_rw, *out_data_hd; 3206 uchar_t *rqbuf_rw, *rqbuf_hd; 3207 struct sd_lun *un; 3208 3209 ASSERT(ssc != NULL); 3210 un = ssc->ssc_un; 3211 ASSERT(un != NULL); 3212 3213 /* 3214 * The flags which will be set in this function are - mmc compliant, 3215 * dvdram writable device, cdda support. Initialize them to FALSE 3216 * and if a capability is detected - it will be set to TRUE. 3217 */ 3218 un->un_f_mmc_cap = FALSE; 3219 un->un_f_dvdram_writable_device = FALSE; 3220 un->un_f_cfg_cdda = FALSE; 3221 3222 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3223 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3224 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3225 3226 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3227 3228 if (status != 0) { 3229 /* command failed; just return */ 3230 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3231 return; 3232 } 3233 /* 3234 * If the mode sense request for the CDROM CAPABILITIES 3235 * page (0x2A) succeeds the device is assumed to be MMC. 3236 */ 3237 un->un_f_mmc_cap = TRUE; 3238 3239 /* Get to the page data */ 3240 sense_mhp = (struct mode_header_grp2 *)buf; 3241 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3242 sense_mhp->bdesc_length_lo; 3243 if (bd_len > MODE_BLK_DESC_LENGTH) { 3244 /* 3245 * We did not get back the expected block descriptor 3246 * length so we cannot determine if the device supports 3247 * CDDA. However, we still indicate the device is MMC 3248 * according to the successful response to the page 3249 * 0x2A mode sense request. 3250 */ 3251 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3252 "sd_set_mmc_caps: Mode Sense returned " 3253 "invalid block descriptor length\n"); 3254 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3255 return; 3256 } 3257 3258 /* See if read CDDA is supported */ 3259 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3260 bd_len); 3261 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3262 3263 /* See if writing DVD RAM is supported. */ 3264 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3265 if (un->un_f_dvdram_writable_device == TRUE) { 3266 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3267 return; 3268 } 3269 3270 /* 3271 * If the device presents DVD or CD capabilities in the mode 3272 * page, we can return here since a RRD will not have 3273 * these capabilities. 3274 */ 3275 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3276 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3277 return; 3278 } 3279 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3280 3281 /* 3282 * If un->un_f_dvdram_writable_device is still FALSE, 3283 * check for a Removable Rigid Disk (RRD). A RRD 3284 * device is identified by the features RANDOM_WRITABLE and 3285 * HARDWARE_DEFECT_MANAGEMENT. 3286 */ 3287 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3288 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3289 3290 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3291 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3292 RANDOM_WRITABLE, SD_PATH_STANDARD); 3293 3294 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3295 3296 if (rtn != 0) { 3297 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3298 kmem_free(rqbuf_rw, SENSE_LENGTH); 3299 return; 3300 } 3301 3302 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3303 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3304 3305 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3306 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3307 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3308 3309 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3310 3311 if (rtn == 0) { 3312 /* 3313 * We have good information, check for random writable 3314 * and hardware defect features. 3315 */ 3316 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3317 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3318 un->un_f_dvdram_writable_device = TRUE; 3319 } 3320 } 3321 3322 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3323 kmem_free(rqbuf_rw, SENSE_LENGTH); 3324 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3325 kmem_free(rqbuf_hd, SENSE_LENGTH); 3326 } 3327 3328 /* 3329 * Function: sd_check_for_writable_cd 3330 * 3331 * Description: This routine determines if the media in the device is 3332 * writable or not. It uses the get configuration command (0x46) 3333 * to determine if the media is writable 3334 * 3335 * Arguments: un - driver soft state (unit) structure 3336 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3337 * chain and the normal command waitq, or 3338 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3339 * "direct" chain and bypass the normal command 3340 * waitq. 3341 * 3342 * Context: Never called at interrupt context. 3343 */ 3344 3345 static void 3346 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag) 3347 { 3348 struct uscsi_cmd com; 3349 uchar_t *out_data; 3350 uchar_t *rqbuf; 3351 int rtn; 3352 uchar_t *out_data_rw, *out_data_hd; 3353 uchar_t *rqbuf_rw, *rqbuf_hd; 3354 struct mode_header_grp2 *sense_mhp; 3355 uchar_t *sense_page; 3356 caddr_t buf; 3357 int bd_len; 3358 int status; 3359 struct sd_lun *un; 3360 3361 ASSERT(ssc != NULL); 3362 un = ssc->ssc_un; 3363 ASSERT(un != NULL); 3364 ASSERT(mutex_owned(SD_MUTEX(un))); 3365 3366 /* 3367 * Initialize the writable media to false, if configuration info. 3368 * tells us otherwise then only we will set it. 3369 */ 3370 un->un_f_mmc_writable_media = FALSE; 3371 mutex_exit(SD_MUTEX(un)); 3372 3373 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3374 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3375 3376 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH, 3377 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3378 3379 if (rtn != 0) 3380 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3381 3382 mutex_enter(SD_MUTEX(un)); 3383 if (rtn == 0) { 3384 /* 3385 * We have good information, check for writable DVD. 3386 */ 3387 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3388 un->un_f_mmc_writable_media = TRUE; 3389 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3390 kmem_free(rqbuf, SENSE_LENGTH); 3391 return; 3392 } 3393 } 3394 3395 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3396 kmem_free(rqbuf, SENSE_LENGTH); 3397 3398 /* 3399 * Determine if this is a RRD type device. 3400 */ 3401 mutex_exit(SD_MUTEX(un)); 3402 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3403 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf, 3404 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3405 3406 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3407 3408 mutex_enter(SD_MUTEX(un)); 3409 if (status != 0) { 3410 /* command failed; just return */ 3411 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3412 return; 3413 } 3414 3415 /* Get to the page data */ 3416 sense_mhp = (struct mode_header_grp2 *)buf; 3417 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3418 if (bd_len > MODE_BLK_DESC_LENGTH) { 3419 /* 3420 * We did not get back the expected block descriptor length so 3421 * we cannot check the mode page. 3422 */ 3423 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3424 "sd_check_for_writable_cd: Mode Sense returned " 3425 "invalid block descriptor length\n"); 3426 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3427 return; 3428 } 3429 3430 /* 3431 * If the device presents DVD or CD capabilities in the mode 3432 * page, we can return here since a RRD device will not have 3433 * these capabilities. 3434 */ 3435 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3436 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3437 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3438 return; 3439 } 3440 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3441 3442 /* 3443 * If un->un_f_mmc_writable_media is still FALSE, 3444 * check for RRD type media. A RRD device is identified 3445 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3446 */ 3447 mutex_exit(SD_MUTEX(un)); 3448 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3449 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3450 3451 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw, 3452 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3453 RANDOM_WRITABLE, path_flag); 3454 3455 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3456 if (rtn != 0) { 3457 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3458 kmem_free(rqbuf_rw, SENSE_LENGTH); 3459 mutex_enter(SD_MUTEX(un)); 3460 return; 3461 } 3462 3463 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3464 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3465 3466 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd, 3467 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3468 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3469 3470 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 3471 mutex_enter(SD_MUTEX(un)); 3472 if (rtn == 0) { 3473 /* 3474 * We have good information, check for random writable 3475 * and hardware defect features as current. 3476 */ 3477 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3478 (out_data_rw[10] & 0x1) && 3479 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3480 (out_data_hd[10] & 0x1)) { 3481 un->un_f_mmc_writable_media = TRUE; 3482 } 3483 } 3484 3485 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3486 kmem_free(rqbuf_rw, SENSE_LENGTH); 3487 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3488 kmem_free(rqbuf_hd, SENSE_LENGTH); 3489 } 3490 3491 /* 3492 * Function: sd_read_unit_properties 3493 * 3494 * Description: The following implements a property lookup mechanism. 3495 * Properties for particular disks (keyed on vendor, model 3496 * and rev numbers) are sought in the sd.conf file via 3497 * sd_process_sdconf_file(), and if not found there, are 3498 * looked for in a list hardcoded in this driver via 3499 * sd_process_sdconf_table() Once located the properties 3500 * are used to update the driver unit structure. 3501 * 3502 * Arguments: un - driver soft state (unit) structure 3503 */ 3504 3505 static void 3506 sd_read_unit_properties(struct sd_lun *un) 3507 { 3508 /* 3509 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3510 * the "sd-config-list" property (from the sd.conf file) or if 3511 * there was not a match for the inquiry vid/pid. If this event 3512 * occurs the static driver configuration table is searched for 3513 * a match. 3514 */ 3515 ASSERT(un != NULL); 3516 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3517 sd_process_sdconf_table(un); 3518 } 3519 3520 /* check for LSI device */ 3521 sd_is_lsi(un); 3522 3523 3524 } 3525 3526 3527 /* 3528 * Function: sd_process_sdconf_file 3529 * 3530 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the 3531 * driver's config file (ie, sd.conf) and update the driver 3532 * soft state structure accordingly. 3533 * 3534 * Arguments: un - driver soft state (unit) structure 3535 * 3536 * Return Code: SD_SUCCESS - The properties were successfully set according 3537 * to the driver configuration file. 3538 * SD_FAILURE - The driver config list was not obtained or 3539 * there was no vid/pid match. This indicates that 3540 * the static config table should be used. 3541 * 3542 * The config file has a property, "sd-config-list". Currently we support 3543 * two kinds of formats. For both formats, the value of this property 3544 * is a list of duplets: 3545 * 3546 * sd-config-list= 3547 * <duplet>, 3548 * [,<duplet>]*; 3549 * 3550 * For the improved format, where 3551 * 3552 * <duplet>:= "<vid+pid>","<tunable-list>" 3553 * 3554 * and 3555 * 3556 * <tunable-list>:= <tunable> [, <tunable> ]*; 3557 * <tunable> = <name> : <value> 3558 * 3559 * The <vid+pid> is the string that is returned by the target device on a 3560 * SCSI inquiry command, the <tunable-list> contains one or more tunables 3561 * to apply to all target devices with the specified <vid+pid>. 3562 * 3563 * Each <tunable> is a "<name> : <value>" pair. 3564 * 3565 * For the old format, the structure of each duplet is as follows: 3566 * 3567 * <duplet>:= "<vid+pid>","<data-property-name_list>" 3568 * 3569 * The first entry of the duplet is the device ID string (the concatenated 3570 * vid & pid; not to be confused with a device_id). This is defined in 3571 * the same way as in the sd_disk_table. 3572 * 3573 * The second part of the duplet is a string that identifies a 3574 * data-property-name-list. The data-property-name-list is defined as 3575 * follows: 3576 * 3577 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3578 * 3579 * The syntax of <data-property-name> depends on the <version> field. 3580 * 3581 * If version = SD_CONF_VERSION_1 we have the following syntax: 3582 * 3583 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3584 * 3585 * where the prop0 value will be used to set prop0 if bit0 set in the 3586 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3587 * 3588 */ 3589 3590 static int 3591 sd_process_sdconf_file(struct sd_lun *un) 3592 { 3593 char **config_list = NULL; 3594 uint_t nelements; 3595 char *vidptr; 3596 int vidlen; 3597 char *dnlist_ptr; 3598 char *dataname_ptr; 3599 char *dataname_lasts; 3600 int *data_list = NULL; 3601 uint_t data_list_len; 3602 int rval = SD_FAILURE; 3603 int i; 3604 3605 ASSERT(un != NULL); 3606 3607 /* Obtain the configuration list associated with the .conf file */ 3608 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un), 3609 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list, 3610 &config_list, &nelements) != DDI_PROP_SUCCESS) { 3611 return (SD_FAILURE); 3612 } 3613 3614 /* 3615 * Compare vids in each duplet to the inquiry vid - if a match is 3616 * made, get the data value and update the soft state structure 3617 * accordingly. 3618 * 3619 * Each duplet should show as a pair of strings, return SD_FAILURE 3620 * otherwise. 3621 */ 3622 if (nelements & 1) { 3623 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3624 "sd-config-list should show as pairs of strings.\n"); 3625 if (config_list) 3626 ddi_prop_free(config_list); 3627 return (SD_FAILURE); 3628 } 3629 3630 for (i = 0; i < nelements; i += 2) { 3631 /* 3632 * Note: The assumption here is that each vid entry is on 3633 * a unique line from its associated duplet. 3634 */ 3635 vidptr = config_list[i]; 3636 vidlen = (int)strlen(vidptr); 3637 if ((vidlen == 0) || 3638 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3639 continue; 3640 } 3641 3642 /* 3643 * dnlist contains 1 or more blank separated 3644 * data-property-name entries 3645 */ 3646 dnlist_ptr = config_list[i + 1]; 3647 3648 if (strchr(dnlist_ptr, ':') != NULL) { 3649 /* 3650 * Decode the improved format sd-config-list. 3651 */ 3652 sd_nvpair_str_decode(un, dnlist_ptr); 3653 } else { 3654 /* 3655 * The old format sd-config-list, loop through all 3656 * data-property-name entries in the 3657 * data-property-name-list 3658 * setting the properties for each. 3659 */ 3660 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t", 3661 &dataname_lasts); dataname_ptr != NULL; 3662 dataname_ptr = sd_strtok_r(NULL, " \t", 3663 &dataname_lasts)) { 3664 int version; 3665 3666 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3667 "sd_process_sdconf_file: disk:%s, " 3668 "data:%s\n", vidptr, dataname_ptr); 3669 3670 /* Get the data list */ 3671 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, 3672 SD_DEVINFO(un), 0, dataname_ptr, &data_list, 3673 &data_list_len) != DDI_PROP_SUCCESS) { 3674 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3675 "sd_process_sdconf_file: data " 3676 "property (%s) has no value\n", 3677 dataname_ptr); 3678 continue; 3679 } 3680 3681 version = data_list[0]; 3682 3683 if (version == SD_CONF_VERSION_1) { 3684 sd_tunables values; 3685 3686 /* Set the properties */ 3687 if (sd_chk_vers1_data(un, data_list[1], 3688 &data_list[2], data_list_len, 3689 dataname_ptr) == SD_SUCCESS) { 3690 sd_get_tunables_from_conf(un, 3691 data_list[1], &data_list[2], 3692 &values); 3693 sd_set_vers1_properties(un, 3694 data_list[1], &values); 3695 rval = SD_SUCCESS; 3696 } else { 3697 rval = SD_FAILURE; 3698 } 3699 } else { 3700 scsi_log(SD_DEVINFO(un), sd_label, 3701 CE_WARN, "data property %s version " 3702 "0x%x is invalid.", 3703 dataname_ptr, version); 3704 rval = SD_FAILURE; 3705 } 3706 if (data_list) 3707 ddi_prop_free(data_list); 3708 } 3709 } 3710 } 3711 3712 /* free up the memory allocated by ddi_prop_lookup_string_array(). */ 3713 if (config_list) { 3714 ddi_prop_free(config_list); 3715 } 3716 3717 return (rval); 3718 } 3719 3720 /* 3721 * Function: sd_nvpair_str_decode() 3722 * 3723 * Description: Parse the improved format sd-config-list to get 3724 * each entry of tunable, which includes a name-value pair. 3725 * Then call sd_set_properties() to set the property. 3726 * 3727 * Arguments: un - driver soft state (unit) structure 3728 * nvpair_str - the tunable list 3729 */ 3730 static void 3731 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) 3732 { 3733 char *nv, *name, *value, *token; 3734 char *nv_lasts, *v_lasts, *x_lasts; 3735 3736 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; 3737 nv = sd_strtok_r(NULL, ",", &nv_lasts)) { 3738 token = sd_strtok_r(nv, ":", &v_lasts); 3739 name = sd_strtok_r(token, " \t", &x_lasts); 3740 token = sd_strtok_r(NULL, ":", &v_lasts); 3741 value = sd_strtok_r(token, " \t", &x_lasts); 3742 if (name == NULL || value == NULL) { 3743 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3744 "sd_nvpair_str_decode: " 3745 "name or value is not valid!\n"); 3746 } else { 3747 sd_set_properties(un, name, value); 3748 } 3749 } 3750 } 3751 3752 /* 3753 * Function: sd_strtok_r() 3754 * 3755 * Description: This function uses strpbrk and strspn to break 3756 * string into tokens on sequentially subsequent calls. Return 3757 * NULL when no non-separator characters remain. The first 3758 * argument is NULL for subsequent calls. 3759 */ 3760 static char * 3761 sd_strtok_r(char *string, const char *sepset, char **lasts) 3762 { 3763 char *q, *r; 3764 3765 /* First or subsequent call */ 3766 if (string == NULL) 3767 string = *lasts; 3768 3769 if (string == NULL) 3770 return (NULL); 3771 3772 /* Skip leading separators */ 3773 q = string + strspn(string, sepset); 3774 3775 if (*q == '\0') 3776 return (NULL); 3777 3778 if ((r = strpbrk(q, sepset)) == NULL) 3779 *lasts = NULL; 3780 else { 3781 *r = '\0'; 3782 *lasts = r + 1; 3783 } 3784 return (q); 3785 } 3786 3787 /* 3788 * Function: sd_set_properties() 3789 * 3790 * Description: Set device properties based on the improved 3791 * format sd-config-list. 3792 * 3793 * Arguments: un - driver soft state (unit) structure 3794 * name - supported tunable name 3795 * value - tunable value 3796 */ 3797 static void 3798 sd_set_properties(struct sd_lun *un, char *name, char *value) 3799 { 3800 char *endptr = NULL; 3801 long val = 0; 3802 3803 if (strcasecmp(name, "cache-nonvolatile") == 0) { 3804 if (strcasecmp(value, "true") == 0) { 3805 un->un_f_suppress_cache_flush = TRUE; 3806 } else if (strcasecmp(value, "false") == 0) { 3807 un->un_f_suppress_cache_flush = FALSE; 3808 } else { 3809 goto value_invalid; 3810 } 3811 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3812 "suppress_cache_flush flag set to %d\n", 3813 un->un_f_suppress_cache_flush); 3814 return; 3815 } 3816 3817 if (strcasecmp(name, "controller-type") == 0) { 3818 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3819 un->un_ctype = val; 3820 } else { 3821 goto value_invalid; 3822 } 3823 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3824 "ctype set to %d\n", un->un_ctype); 3825 return; 3826 } 3827 3828 if (strcasecmp(name, "delay-busy") == 0) { 3829 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3830 un->un_busy_timeout = drv_usectohz(val / 1000); 3831 } else { 3832 goto value_invalid; 3833 } 3834 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3835 "busy_timeout set to %d\n", un->un_busy_timeout); 3836 return; 3837 } 3838 3839 if (strcasecmp(name, "disksort") == 0) { 3840 if (strcasecmp(value, "true") == 0) { 3841 un->un_f_disksort_disabled = FALSE; 3842 } else if (strcasecmp(value, "false") == 0) { 3843 un->un_f_disksort_disabled = TRUE; 3844 } else { 3845 goto value_invalid; 3846 } 3847 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3848 "disksort disabled flag set to %d\n", 3849 un->un_f_disksort_disabled); 3850 return; 3851 } 3852 3853 if (strcasecmp(name, "timeout-releasereservation") == 0) { 3854 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3855 un->un_reserve_release_time = val; 3856 } else { 3857 goto value_invalid; 3858 } 3859 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3860 "reservation release timeout set to %d\n", 3861 un->un_reserve_release_time); 3862 return; 3863 } 3864 3865 if (strcasecmp(name, "reset-lun") == 0) { 3866 if (strcasecmp(value, "true") == 0) { 3867 un->un_f_lun_reset_enabled = TRUE; 3868 } else if (strcasecmp(value, "false") == 0) { 3869 un->un_f_lun_reset_enabled = FALSE; 3870 } else { 3871 goto value_invalid; 3872 } 3873 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3874 "lun reset enabled flag set to %d\n", 3875 un->un_f_lun_reset_enabled); 3876 return; 3877 } 3878 3879 if (strcasecmp(name, "retries-busy") == 0) { 3880 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3881 un->un_busy_retry_count = val; 3882 } else { 3883 goto value_invalid; 3884 } 3885 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3886 "busy retry count set to %d\n", un->un_busy_retry_count); 3887 return; 3888 } 3889 3890 if (strcasecmp(name, "retries-timeout") == 0) { 3891 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3892 un->un_retry_count = val; 3893 } else { 3894 goto value_invalid; 3895 } 3896 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3897 "timeout retry count set to %d\n", un->un_retry_count); 3898 return; 3899 } 3900 3901 if (strcasecmp(name, "retries-notready") == 0) { 3902 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3903 un->un_notready_retry_count = val; 3904 } else { 3905 goto value_invalid; 3906 } 3907 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3908 "notready retry count set to %d\n", 3909 un->un_notready_retry_count); 3910 return; 3911 } 3912 3913 if (strcasecmp(name, "retries-reset") == 0) { 3914 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3915 un->un_reset_retry_count = val; 3916 } else { 3917 goto value_invalid; 3918 } 3919 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3920 "reset retry count set to %d\n", 3921 un->un_reset_retry_count); 3922 return; 3923 } 3924 3925 if (strcasecmp(name, "throttle-max") == 0) { 3926 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3927 un->un_saved_throttle = un->un_throttle = val; 3928 } else { 3929 goto value_invalid; 3930 } 3931 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3932 "throttle set to %d\n", un->un_throttle); 3933 } 3934 3935 if (strcasecmp(name, "throttle-min") == 0) { 3936 if (ddi_strtol(value, &endptr, 0, &val) == 0) { 3937 un->un_min_throttle = val; 3938 } else { 3939 goto value_invalid; 3940 } 3941 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3942 "min throttle set to %d\n", un->un_min_throttle); 3943 } 3944 3945 /* 3946 * Validate the throttle values. 3947 * If any of the numbers are invalid, set everything to defaults. 3948 */ 3949 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 3950 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 3951 (un->un_min_throttle > un->un_throttle)) { 3952 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 3953 un->un_min_throttle = sd_min_throttle; 3954 } 3955 return; 3956 3957 value_invalid: 3958 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: " 3959 "value of prop %s is invalid\n", name); 3960 } 3961 3962 /* 3963 * Function: sd_get_tunables_from_conf() 3964 * 3965 * 3966 * This function reads the data list from the sd.conf file and pulls 3967 * the values that can have numeric values as arguments and places 3968 * the values in the appropriate sd_tunables member. 3969 * Since the order of the data list members varies across platforms 3970 * This function reads them from the data list in a platform specific 3971 * order and places them into the correct sd_tunable member that is 3972 * consistent across all platforms. 3973 */ 3974 static void 3975 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3976 sd_tunables *values) 3977 { 3978 int i; 3979 int mask; 3980 3981 bzero(values, sizeof (sd_tunables)); 3982 3983 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3984 3985 mask = 1 << i; 3986 if (mask > flags) { 3987 break; 3988 } 3989 3990 switch (mask & flags) { 3991 case 0: /* This mask bit not set in flags */ 3992 continue; 3993 case SD_CONF_BSET_THROTTLE: 3994 values->sdt_throttle = data_list[i]; 3995 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3996 "sd_get_tunables_from_conf: throttle = %d\n", 3997 values->sdt_throttle); 3998 break; 3999 case SD_CONF_BSET_CTYPE: 4000 values->sdt_ctype = data_list[i]; 4001 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4002 "sd_get_tunables_from_conf: ctype = %d\n", 4003 values->sdt_ctype); 4004 break; 4005 case SD_CONF_BSET_NRR_COUNT: 4006 values->sdt_not_rdy_retries = data_list[i]; 4007 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4008 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 4009 values->sdt_not_rdy_retries); 4010 break; 4011 case SD_CONF_BSET_BSY_RETRY_COUNT: 4012 values->sdt_busy_retries = data_list[i]; 4013 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4014 "sd_get_tunables_from_conf: busy_retries = %d\n", 4015 values->sdt_busy_retries); 4016 break; 4017 case SD_CONF_BSET_RST_RETRIES: 4018 values->sdt_reset_retries = data_list[i]; 4019 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4020 "sd_get_tunables_from_conf: reset_retries = %d\n", 4021 values->sdt_reset_retries); 4022 break; 4023 case SD_CONF_BSET_RSV_REL_TIME: 4024 values->sdt_reserv_rel_time = data_list[i]; 4025 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4026 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 4027 values->sdt_reserv_rel_time); 4028 break; 4029 case SD_CONF_BSET_MIN_THROTTLE: 4030 values->sdt_min_throttle = data_list[i]; 4031 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4032 "sd_get_tunables_from_conf: min_throttle = %d\n", 4033 values->sdt_min_throttle); 4034 break; 4035 case SD_CONF_BSET_DISKSORT_DISABLED: 4036 values->sdt_disk_sort_dis = data_list[i]; 4037 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4038 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 4039 values->sdt_disk_sort_dis); 4040 break; 4041 case SD_CONF_BSET_LUN_RESET_ENABLED: 4042 values->sdt_lun_reset_enable = data_list[i]; 4043 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4044 "sd_get_tunables_from_conf: lun_reset_enable = %d" 4045 "\n", values->sdt_lun_reset_enable); 4046 break; 4047 case SD_CONF_BSET_CACHE_IS_NV: 4048 values->sdt_suppress_cache_flush = data_list[i]; 4049 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4050 "sd_get_tunables_from_conf: \ 4051 suppress_cache_flush = %d" 4052 "\n", values->sdt_suppress_cache_flush); 4053 break; 4054 } 4055 } 4056 } 4057 4058 /* 4059 * Function: sd_process_sdconf_table 4060 * 4061 * Description: Search the static configuration table for a match on the 4062 * inquiry vid/pid and update the driver soft state structure 4063 * according to the table property values for the device. 4064 * 4065 * The form of a configuration table entry is: 4066 * <vid+pid>,<flags>,<property-data> 4067 * "SEAGATE ST42400N",1,0x40000, 4068 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 4069 * 4070 * Arguments: un - driver soft state (unit) structure 4071 */ 4072 4073 static void 4074 sd_process_sdconf_table(struct sd_lun *un) 4075 { 4076 char *id = NULL; 4077 int table_index; 4078 int idlen; 4079 4080 ASSERT(un != NULL); 4081 for (table_index = 0; table_index < sd_disk_table_size; 4082 table_index++) { 4083 id = sd_disk_table[table_index].device_id; 4084 idlen = strlen(id); 4085 if (idlen == 0) { 4086 continue; 4087 } 4088 4089 /* 4090 * The static configuration table currently does not 4091 * implement version 10 properties. Additionally, 4092 * multiple data-property-name entries are not 4093 * implemented in the static configuration table. 4094 */ 4095 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4096 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4097 "sd_process_sdconf_table: disk %s\n", id); 4098 sd_set_vers1_properties(un, 4099 sd_disk_table[table_index].flags, 4100 sd_disk_table[table_index].properties); 4101 break; 4102 } 4103 } 4104 } 4105 4106 4107 /* 4108 * Function: sd_sdconf_id_match 4109 * 4110 * Description: This local function implements a case sensitive vid/pid 4111 * comparison as well as the boundary cases of wild card and 4112 * multiple blanks. 4113 * 4114 * Note: An implicit assumption made here is that the scsi 4115 * inquiry structure will always keep the vid, pid and 4116 * revision strings in consecutive sequence, so they can be 4117 * read as a single string. If this assumption is not the 4118 * case, a separate string, to be used for the check, needs 4119 * to be built with these strings concatenated. 4120 * 4121 * Arguments: un - driver soft state (unit) structure 4122 * id - table or config file vid/pid 4123 * idlen - length of the vid/pid (bytes) 4124 * 4125 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4126 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4127 */ 4128 4129 static int 4130 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 4131 { 4132 struct scsi_inquiry *sd_inq; 4133 int rval = SD_SUCCESS; 4134 4135 ASSERT(un != NULL); 4136 sd_inq = un->un_sd->sd_inq; 4137 ASSERT(id != NULL); 4138 4139 /* 4140 * We use the inq_vid as a pointer to a buffer containing the 4141 * vid and pid and use the entire vid/pid length of the table 4142 * entry for the comparison. This works because the inq_pid 4143 * data member follows inq_vid in the scsi_inquiry structure. 4144 */ 4145 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 4146 /* 4147 * The user id string is compared to the inquiry vid/pid 4148 * using a case insensitive comparison and ignoring 4149 * multiple spaces. 4150 */ 4151 rval = sd_blank_cmp(un, id, idlen); 4152 if (rval != SD_SUCCESS) { 4153 /* 4154 * User id strings that start and end with a "*" 4155 * are a special case. These do not have a 4156 * specific vendor, and the product string can 4157 * appear anywhere in the 16 byte PID portion of 4158 * the inquiry data. This is a simple strstr() 4159 * type search for the user id in the inquiry data. 4160 */ 4161 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 4162 char *pidptr = &id[1]; 4163 int i; 4164 int j; 4165 int pidstrlen = idlen - 2; 4166 j = sizeof (SD_INQUIRY(un)->inq_pid) - 4167 pidstrlen; 4168 4169 if (j < 0) { 4170 return (SD_FAILURE); 4171 } 4172 for (i = 0; i < j; i++) { 4173 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 4174 pidptr, pidstrlen) == 0) { 4175 rval = SD_SUCCESS; 4176 break; 4177 } 4178 } 4179 } 4180 } 4181 } 4182 return (rval); 4183 } 4184 4185 4186 /* 4187 * Function: sd_blank_cmp 4188 * 4189 * Description: If the id string starts and ends with a space, treat 4190 * multiple consecutive spaces as equivalent to a single 4191 * space. For example, this causes a sd_disk_table entry 4192 * of " NEC CDROM " to match a device's id string of 4193 * "NEC CDROM". 4194 * 4195 * Note: The success exit condition for this routine is if 4196 * the pointer to the table entry is '\0' and the cnt of 4197 * the inquiry length is zero. This will happen if the inquiry 4198 * string returned by the device is padded with spaces to be 4199 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 4200 * SCSI spec states that the inquiry string is to be padded with 4201 * spaces. 4202 * 4203 * Arguments: un - driver soft state (unit) structure 4204 * id - table or config file vid/pid 4205 * idlen - length of the vid/pid (bytes) 4206 * 4207 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 4208 * SD_FAILURE - Indicates no match with the inquiry vid/pid 4209 */ 4210 4211 static int 4212 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 4213 { 4214 char *p1; 4215 char *p2; 4216 int cnt; 4217 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 4218 sizeof (SD_INQUIRY(un)->inq_pid); 4219 4220 ASSERT(un != NULL); 4221 p2 = un->un_sd->sd_inq->inq_vid; 4222 ASSERT(id != NULL); 4223 p1 = id; 4224 4225 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 4226 /* 4227 * Note: string p1 is terminated by a NUL but string p2 4228 * isn't. The end of p2 is determined by cnt. 4229 */ 4230 for (;;) { 4231 /* skip over any extra blanks in both strings */ 4232 while ((*p1 != '\0') && (*p1 == ' ')) { 4233 p1++; 4234 } 4235 while ((cnt != 0) && (*p2 == ' ')) { 4236 p2++; 4237 cnt--; 4238 } 4239 4240 /* compare the two strings */ 4241 if ((cnt == 0) || 4242 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 4243 break; 4244 } 4245 while ((cnt > 0) && 4246 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 4247 p1++; 4248 p2++; 4249 cnt--; 4250 } 4251 } 4252 } 4253 4254 /* return SD_SUCCESS if both strings match */ 4255 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 4256 } 4257 4258 4259 /* 4260 * Function: sd_chk_vers1_data 4261 * 4262 * Description: Verify the version 1 device properties provided by the 4263 * user via the configuration file 4264 * 4265 * Arguments: un - driver soft state (unit) structure 4266 * flags - integer mask indicating properties to be set 4267 * prop_list - integer list of property values 4268 * list_len - number of the elements 4269 * 4270 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4271 * SD_FAILURE - Indicates the user provided data is invalid 4272 */ 4273 4274 static int 4275 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4276 int list_len, char *dataname_ptr) 4277 { 4278 int i; 4279 int mask = 1; 4280 int index = 0; 4281 4282 ASSERT(un != NULL); 4283 4284 /* Check for a NULL property name and list */ 4285 if (dataname_ptr == NULL) { 4286 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4287 "sd_chk_vers1_data: NULL data property name."); 4288 return (SD_FAILURE); 4289 } 4290 if (prop_list == NULL) { 4291 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4292 "sd_chk_vers1_data: %s NULL data property list.", 4293 dataname_ptr); 4294 return (SD_FAILURE); 4295 } 4296 4297 /* Display a warning if undefined bits are set in the flags */ 4298 if (flags & ~SD_CONF_BIT_MASK) { 4299 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4300 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4301 "Properties not set.", 4302 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4303 return (SD_FAILURE); 4304 } 4305 4306 /* 4307 * Verify the length of the list by identifying the highest bit set 4308 * in the flags and validating that the property list has a length 4309 * up to the index of this bit. 4310 */ 4311 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4312 if (flags & mask) { 4313 index++; 4314 } 4315 mask = 1 << i; 4316 } 4317 if (list_len < (index + 2)) { 4318 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4319 "sd_chk_vers1_data: " 4320 "Data property list %s size is incorrect. " 4321 "Properties not set.", dataname_ptr); 4322 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4323 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4324 return (SD_FAILURE); 4325 } 4326 return (SD_SUCCESS); 4327 } 4328 4329 4330 /* 4331 * Function: sd_set_vers1_properties 4332 * 4333 * Description: Set version 1 device properties based on a property list 4334 * retrieved from the driver configuration file or static 4335 * configuration table. Version 1 properties have the format: 4336 * 4337 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4338 * 4339 * where the prop0 value will be used to set prop0 if bit0 4340 * is set in the flags 4341 * 4342 * Arguments: un - driver soft state (unit) structure 4343 * flags - integer mask indicating properties to be set 4344 * prop_list - integer list of property values 4345 */ 4346 4347 static void 4348 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4349 { 4350 ASSERT(un != NULL); 4351 4352 /* 4353 * Set the flag to indicate cache is to be disabled. An attempt 4354 * to disable the cache via sd_cache_control() will be made 4355 * later during attach once the basic initialization is complete. 4356 */ 4357 if (flags & SD_CONF_BSET_NOCACHE) { 4358 un->un_f_opt_disable_cache = TRUE; 4359 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4360 "sd_set_vers1_properties: caching disabled flag set\n"); 4361 } 4362 4363 /* CD-specific configuration parameters */ 4364 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4365 un->un_f_cfg_playmsf_bcd = TRUE; 4366 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4367 "sd_set_vers1_properties: playmsf_bcd set\n"); 4368 } 4369 if (flags & SD_CONF_BSET_READSUB_BCD) { 4370 un->un_f_cfg_readsub_bcd = TRUE; 4371 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4372 "sd_set_vers1_properties: readsub_bcd set\n"); 4373 } 4374 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4375 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4376 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4377 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4378 } 4379 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4380 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4381 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4382 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4383 } 4384 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4385 un->un_f_cfg_no_read_header = TRUE; 4386 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4387 "sd_set_vers1_properties: no_read_header set\n"); 4388 } 4389 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4390 un->un_f_cfg_read_cd_xd4 = TRUE; 4391 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4392 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4393 } 4394 4395 /* Support for devices which do not have valid/unique serial numbers */ 4396 if (flags & SD_CONF_BSET_FAB_DEVID) { 4397 un->un_f_opt_fab_devid = TRUE; 4398 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4399 "sd_set_vers1_properties: fab_devid bit set\n"); 4400 } 4401 4402 /* Support for user throttle configuration */ 4403 if (flags & SD_CONF_BSET_THROTTLE) { 4404 ASSERT(prop_list != NULL); 4405 un->un_saved_throttle = un->un_throttle = 4406 prop_list->sdt_throttle; 4407 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4408 "sd_set_vers1_properties: throttle set to %d\n", 4409 prop_list->sdt_throttle); 4410 } 4411 4412 /* Set the per disk retry count according to the conf file or table. */ 4413 if (flags & SD_CONF_BSET_NRR_COUNT) { 4414 ASSERT(prop_list != NULL); 4415 if (prop_list->sdt_not_rdy_retries) { 4416 un->un_notready_retry_count = 4417 prop_list->sdt_not_rdy_retries; 4418 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4419 "sd_set_vers1_properties: not ready retry count" 4420 " set to %d\n", un->un_notready_retry_count); 4421 } 4422 } 4423 4424 /* The controller type is reported for generic disk driver ioctls */ 4425 if (flags & SD_CONF_BSET_CTYPE) { 4426 ASSERT(prop_list != NULL); 4427 switch (prop_list->sdt_ctype) { 4428 case CTYPE_CDROM: 4429 un->un_ctype = prop_list->sdt_ctype; 4430 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4431 "sd_set_vers1_properties: ctype set to " 4432 "CTYPE_CDROM\n"); 4433 break; 4434 case CTYPE_CCS: 4435 un->un_ctype = prop_list->sdt_ctype; 4436 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4437 "sd_set_vers1_properties: ctype set to " 4438 "CTYPE_CCS\n"); 4439 break; 4440 case CTYPE_ROD: /* RW optical */ 4441 un->un_ctype = prop_list->sdt_ctype; 4442 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4443 "sd_set_vers1_properties: ctype set to " 4444 "CTYPE_ROD\n"); 4445 break; 4446 default: 4447 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4448 "sd_set_vers1_properties: Could not set " 4449 "invalid ctype value (%d)", 4450 prop_list->sdt_ctype); 4451 } 4452 } 4453 4454 /* Purple failover timeout */ 4455 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4456 ASSERT(prop_list != NULL); 4457 un->un_busy_retry_count = 4458 prop_list->sdt_busy_retries; 4459 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4460 "sd_set_vers1_properties: " 4461 "busy retry count set to %d\n", 4462 un->un_busy_retry_count); 4463 } 4464 4465 /* Purple reset retry count */ 4466 if (flags & SD_CONF_BSET_RST_RETRIES) { 4467 ASSERT(prop_list != NULL); 4468 un->un_reset_retry_count = 4469 prop_list->sdt_reset_retries; 4470 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4471 "sd_set_vers1_properties: " 4472 "reset retry count set to %d\n", 4473 un->un_reset_retry_count); 4474 } 4475 4476 /* Purple reservation release timeout */ 4477 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4478 ASSERT(prop_list != NULL); 4479 un->un_reserve_release_time = 4480 prop_list->sdt_reserv_rel_time; 4481 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4482 "sd_set_vers1_properties: " 4483 "reservation release timeout set to %d\n", 4484 un->un_reserve_release_time); 4485 } 4486 4487 /* 4488 * Driver flag telling the driver to verify that no commands are pending 4489 * for a device before issuing a Test Unit Ready. This is a workaround 4490 * for a firmware bug in some Seagate eliteI drives. 4491 */ 4492 if (flags & SD_CONF_BSET_TUR_CHECK) { 4493 un->un_f_cfg_tur_check = TRUE; 4494 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4495 "sd_set_vers1_properties: tur queue check set\n"); 4496 } 4497 4498 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4499 un->un_min_throttle = prop_list->sdt_min_throttle; 4500 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4501 "sd_set_vers1_properties: min throttle set to %d\n", 4502 un->un_min_throttle); 4503 } 4504 4505 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4506 un->un_f_disksort_disabled = 4507 (prop_list->sdt_disk_sort_dis != 0) ? 4508 TRUE : FALSE; 4509 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4510 "sd_set_vers1_properties: disksort disabled " 4511 "flag set to %d\n", 4512 prop_list->sdt_disk_sort_dis); 4513 } 4514 4515 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4516 un->un_f_lun_reset_enabled = 4517 (prop_list->sdt_lun_reset_enable != 0) ? 4518 TRUE : FALSE; 4519 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4520 "sd_set_vers1_properties: lun reset enabled " 4521 "flag set to %d\n", 4522 prop_list->sdt_lun_reset_enable); 4523 } 4524 4525 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4526 un->un_f_suppress_cache_flush = 4527 (prop_list->sdt_suppress_cache_flush != 0) ? 4528 TRUE : FALSE; 4529 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4530 "sd_set_vers1_properties: suppress_cache_flush " 4531 "flag set to %d\n", 4532 prop_list->sdt_suppress_cache_flush); 4533 } 4534 4535 /* 4536 * Validate the throttle values. 4537 * If any of the numbers are invalid, set everything to defaults. 4538 */ 4539 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4540 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4541 (un->un_min_throttle > un->un_throttle)) { 4542 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4543 un->un_min_throttle = sd_min_throttle; 4544 } 4545 } 4546 4547 /* 4548 * Function: sd_is_lsi() 4549 * 4550 * Description: Check for lsi devices, step through the static device 4551 * table to match vid/pid. 4552 * 4553 * Args: un - ptr to sd_lun 4554 * 4555 * Notes: When creating new LSI property, need to add the new LSI property 4556 * to this function. 4557 */ 4558 static void 4559 sd_is_lsi(struct sd_lun *un) 4560 { 4561 char *id = NULL; 4562 int table_index; 4563 int idlen; 4564 void *prop; 4565 4566 ASSERT(un != NULL); 4567 for (table_index = 0; table_index < sd_disk_table_size; 4568 table_index++) { 4569 id = sd_disk_table[table_index].device_id; 4570 idlen = strlen(id); 4571 if (idlen == 0) { 4572 continue; 4573 } 4574 4575 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4576 prop = sd_disk_table[table_index].properties; 4577 if (prop == &lsi_properties || 4578 prop == &lsi_oem_properties || 4579 prop == &lsi_properties_scsi || 4580 prop == &symbios_properties) { 4581 un->un_f_cfg_is_lsi = TRUE; 4582 } 4583 break; 4584 } 4585 } 4586 } 4587 4588 /* 4589 * Function: sd_get_physical_geometry 4590 * 4591 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4592 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4593 * target, and use this information to initialize the physical 4594 * geometry cache specified by pgeom_p. 4595 * 4596 * MODE SENSE is an optional command, so failure in this case 4597 * does not necessarily denote an error. We want to use the 4598 * MODE SENSE commands to derive the physical geometry of the 4599 * device, but if either command fails, the logical geometry is 4600 * used as the fallback for disk label geometry in cmlb. 4601 * 4602 * This requires that un->un_blockcount and un->un_tgt_blocksize 4603 * have already been initialized for the current target and 4604 * that the current values be passed as args so that we don't 4605 * end up ever trying to use -1 as a valid value. This could 4606 * happen if either value is reset while we're not holding 4607 * the mutex. 4608 * 4609 * Arguments: un - driver soft state (unit) structure 4610 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4611 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4612 * to use the USCSI "direct" chain and bypass the normal 4613 * command waitq. 4614 * 4615 * Context: Kernel thread only (can sleep). 4616 */ 4617 4618 static int 4619 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4620 diskaddr_t capacity, int lbasize, int path_flag) 4621 { 4622 struct mode_format *page3p; 4623 struct mode_geometry *page4p; 4624 struct mode_header *headerp; 4625 int sector_size; 4626 int nsect; 4627 int nhead; 4628 int ncyl; 4629 int intrlv; 4630 int spc; 4631 diskaddr_t modesense_capacity; 4632 int rpm; 4633 int bd_len; 4634 int mode_header_length; 4635 uchar_t *p3bufp; 4636 uchar_t *p4bufp; 4637 int cdbsize; 4638 int ret = EIO; 4639 sd_ssc_t *ssc; 4640 int status; 4641 4642 ASSERT(un != NULL); 4643 4644 if (lbasize == 0) { 4645 if (ISCD(un)) { 4646 lbasize = 2048; 4647 } else { 4648 lbasize = un->un_sys_blocksize; 4649 } 4650 } 4651 pgeom_p->g_secsize = (unsigned short)lbasize; 4652 4653 /* 4654 * If the unit is a cd/dvd drive MODE SENSE page three 4655 * and MODE SENSE page four are reserved (see SBC spec 4656 * and MMC spec). To prevent soft errors just return 4657 * using the default LBA size. 4658 */ 4659 if (ISCD(un)) 4660 return (ret); 4661 4662 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4663 4664 /* 4665 * Retrieve MODE SENSE page 3 - Format Device Page 4666 */ 4667 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4668 ssc = sd_ssc_init(un); 4669 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp, 4670 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag); 4671 if (status != 0) { 4672 SD_ERROR(SD_LOG_COMMON, un, 4673 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4674 goto page3_exit; 4675 } 4676 4677 /* 4678 * Determine size of Block Descriptors in order to locate the mode 4679 * page data. ATAPI devices return 0, SCSI devices should return 4680 * MODE_BLK_DESC_LENGTH. 4681 */ 4682 headerp = (struct mode_header *)p3bufp; 4683 if (un->un_f_cfg_is_atapi == TRUE) { 4684 struct mode_header_grp2 *mhp = 4685 (struct mode_header_grp2 *)headerp; 4686 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4687 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4688 } else { 4689 mode_header_length = MODE_HEADER_LENGTH; 4690 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4691 } 4692 4693 if (bd_len > MODE_BLK_DESC_LENGTH) { 4694 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 4695 "sd_get_physical_geometry: received unexpected bd_len " 4696 "of %d, page3\n", bd_len); 4697 status = EIO; 4698 goto page3_exit; 4699 } 4700 4701 page3p = (struct mode_format *) 4702 ((caddr_t)headerp + mode_header_length + bd_len); 4703 4704 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4705 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 4706 "sd_get_physical_geometry: mode sense pg3 code mismatch " 4707 "%d\n", page3p->mode_page.code); 4708 status = EIO; 4709 goto page3_exit; 4710 } 4711 4712 /* 4713 * Use this physical geometry data only if BOTH MODE SENSE commands 4714 * complete successfully; otherwise, revert to the logical geometry. 4715 * So, we need to save everything in temporary variables. 4716 */ 4717 sector_size = BE_16(page3p->data_bytes_sect); 4718 4719 /* 4720 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4721 */ 4722 if (sector_size == 0) { 4723 sector_size = un->un_sys_blocksize; 4724 } else { 4725 sector_size &= ~(un->un_sys_blocksize - 1); 4726 } 4727 4728 nsect = BE_16(page3p->sect_track); 4729 intrlv = BE_16(page3p->interleave); 4730 4731 SD_INFO(SD_LOG_COMMON, un, 4732 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4733 SD_INFO(SD_LOG_COMMON, un, 4734 " mode page: %d; nsect: %d; sector size: %d;\n", 4735 page3p->mode_page.code, nsect, sector_size); 4736 SD_INFO(SD_LOG_COMMON, un, 4737 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4738 BE_16(page3p->track_skew), 4739 BE_16(page3p->cylinder_skew)); 4740 4741 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 4742 4743 /* 4744 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4745 */ 4746 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4747 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp, 4748 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag); 4749 if (status != 0) { 4750 SD_ERROR(SD_LOG_COMMON, un, 4751 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4752 goto page4_exit; 4753 } 4754 4755 /* 4756 * Determine size of Block Descriptors in order to locate the mode 4757 * page data. ATAPI devices return 0, SCSI devices should return 4758 * MODE_BLK_DESC_LENGTH. 4759 */ 4760 headerp = (struct mode_header *)p4bufp; 4761 if (un->un_f_cfg_is_atapi == TRUE) { 4762 struct mode_header_grp2 *mhp = 4763 (struct mode_header_grp2 *)headerp; 4764 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4765 } else { 4766 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4767 } 4768 4769 if (bd_len > MODE_BLK_DESC_LENGTH) { 4770 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 4771 "sd_get_physical_geometry: received unexpected bd_len of " 4772 "%d, page4\n", bd_len); 4773 status = EIO; 4774 goto page4_exit; 4775 } 4776 4777 page4p = (struct mode_geometry *) 4778 ((caddr_t)headerp + mode_header_length + bd_len); 4779 4780 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4781 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 4782 "sd_get_physical_geometry: mode sense pg4 code mismatch " 4783 "%d\n", page4p->mode_page.code); 4784 status = EIO; 4785 goto page4_exit; 4786 } 4787 4788 /* 4789 * Stash the data now, after we know that both commands completed. 4790 */ 4791 4792 4793 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4794 spc = nhead * nsect; 4795 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4796 rpm = BE_16(page4p->rpm); 4797 4798 modesense_capacity = spc * ncyl; 4799 4800 SD_INFO(SD_LOG_COMMON, un, 4801 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4802 SD_INFO(SD_LOG_COMMON, un, 4803 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4804 SD_INFO(SD_LOG_COMMON, un, 4805 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4806 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4807 (void *)pgeom_p, capacity); 4808 4809 /* 4810 * Compensate if the drive's geometry is not rectangular, i.e., 4811 * the product of C * H * S returned by MODE SENSE >= that returned 4812 * by read capacity. This is an idiosyncrasy of the original x86 4813 * disk subsystem. 4814 */ 4815 if (modesense_capacity >= capacity) { 4816 SD_INFO(SD_LOG_COMMON, un, 4817 "sd_get_physical_geometry: adjusting acyl; " 4818 "old: %d; new: %d\n", pgeom_p->g_acyl, 4819 (modesense_capacity - capacity + spc - 1) / spc); 4820 if (sector_size != 0) { 4821 /* 1243403: NEC D38x7 drives don't support sec size */ 4822 pgeom_p->g_secsize = (unsigned short)sector_size; 4823 } 4824 pgeom_p->g_nsect = (unsigned short)nsect; 4825 pgeom_p->g_nhead = (unsigned short)nhead; 4826 pgeom_p->g_capacity = capacity; 4827 pgeom_p->g_acyl = 4828 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 4829 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 4830 } 4831 4832 pgeom_p->g_rpm = (unsigned short)rpm; 4833 pgeom_p->g_intrlv = (unsigned short)intrlv; 4834 ret = 0; 4835 4836 SD_INFO(SD_LOG_COMMON, un, 4837 "sd_get_physical_geometry: mode sense geometry:\n"); 4838 SD_INFO(SD_LOG_COMMON, un, 4839 " nsect: %d; sector size: %d; interlv: %d\n", 4840 nsect, sector_size, intrlv); 4841 SD_INFO(SD_LOG_COMMON, un, 4842 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 4843 nhead, ncyl, rpm, modesense_capacity); 4844 SD_INFO(SD_LOG_COMMON, un, 4845 "sd_get_physical_geometry: (cached)\n"); 4846 SD_INFO(SD_LOG_COMMON, un, 4847 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4848 pgeom_p->g_ncyl, pgeom_p->g_acyl, 4849 pgeom_p->g_nhead, pgeom_p->g_nsect); 4850 SD_INFO(SD_LOG_COMMON, un, 4851 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 4852 pgeom_p->g_secsize, pgeom_p->g_capacity, 4853 pgeom_p->g_intrlv, pgeom_p->g_rpm); 4854 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 4855 4856 page4_exit: 4857 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 4858 4859 page3_exit: 4860 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 4861 4862 if (status != 0) { 4863 if (status == EIO) { 4864 /* 4865 * Some disks do not support mode sense(6), we 4866 * should ignore this kind of error(sense key is 4867 * 0x5 - illegal request). 4868 */ 4869 uint8_t *sensep; 4870 int senlen; 4871 4872 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 4873 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 4874 ssc->ssc_uscsi_cmd->uscsi_rqresid); 4875 4876 if (senlen > 0 && 4877 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 4878 sd_ssc_assessment(ssc, 4879 SD_FMT_IGNORE_COMPROMISE); 4880 } else { 4881 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 4882 } 4883 } else { 4884 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 4885 } 4886 } 4887 sd_ssc_fini(ssc); 4888 return (ret); 4889 } 4890 4891 /* 4892 * Function: sd_get_virtual_geometry 4893 * 4894 * Description: Ask the controller to tell us about the target device. 4895 * 4896 * Arguments: un - pointer to softstate 4897 * capacity - disk capacity in #blocks 4898 * lbasize - disk block size in bytes 4899 * 4900 * Context: Kernel thread only 4901 */ 4902 4903 static int 4904 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 4905 diskaddr_t capacity, int lbasize) 4906 { 4907 uint_t geombuf; 4908 int spc; 4909 4910 ASSERT(un != NULL); 4911 4912 /* Set sector size, and total number of sectors */ 4913 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 4914 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 4915 4916 /* Let the HBA tell us its geometry */ 4917 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 4918 4919 /* A value of -1 indicates an undefined "geometry" property */ 4920 if (geombuf == (-1)) { 4921 return (EINVAL); 4922 } 4923 4924 /* Initialize the logical geometry cache. */ 4925 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 4926 lgeom_p->g_nsect = geombuf & 0xffff; 4927 lgeom_p->g_secsize = un->un_sys_blocksize; 4928 4929 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 4930 4931 /* 4932 * Note: The driver originally converted the capacity value from 4933 * target blocks to system blocks. However, the capacity value passed 4934 * to this routine is already in terms of system blocks (this scaling 4935 * is done when the READ CAPACITY command is issued and processed). 4936 * This 'error' may have gone undetected because the usage of g_ncyl 4937 * (which is based upon g_capacity) is very limited within the driver 4938 */ 4939 lgeom_p->g_capacity = capacity; 4940 4941 /* 4942 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 4943 * hba may return zero values if the device has been removed. 4944 */ 4945 if (spc == 0) { 4946 lgeom_p->g_ncyl = 0; 4947 } else { 4948 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 4949 } 4950 lgeom_p->g_acyl = 0; 4951 4952 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 4953 return (0); 4954 4955 } 4956 /* 4957 * Function: sd_update_block_info 4958 * 4959 * Description: Calculate a byte count to sector count bitshift value 4960 * from sector size. 4961 * 4962 * Arguments: un: unit struct. 4963 * lbasize: new target sector size 4964 * capacity: new target capacity, ie. block count 4965 * 4966 * Context: Kernel thread context 4967 */ 4968 4969 static void 4970 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 4971 { 4972 if (lbasize != 0) { 4973 un->un_tgt_blocksize = lbasize; 4974 un->un_f_tgt_blocksize_is_valid = TRUE; 4975 } 4976 4977 if (capacity != 0) { 4978 un->un_blockcount = capacity; 4979 un->un_f_blockcount_is_valid = TRUE; 4980 } 4981 } 4982 4983 4984 /* 4985 * Function: sd_register_devid 4986 * 4987 * Description: This routine will obtain the device id information from the 4988 * target, obtain the serial number, and register the device 4989 * id with the ddi framework. 4990 * 4991 * Arguments: devi - the system's dev_info_t for the device. 4992 * un - driver soft state (unit) structure 4993 * reservation_flag - indicates if a reservation conflict 4994 * occurred during attach 4995 * 4996 * Context: Kernel Thread 4997 */ 4998 static void 4999 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag) 5000 { 5001 int rval = 0; 5002 uchar_t *inq80 = NULL; 5003 size_t inq80_len = MAX_INQUIRY_SIZE; 5004 size_t inq80_resid = 0; 5005 uchar_t *inq83 = NULL; 5006 size_t inq83_len = MAX_INQUIRY_SIZE; 5007 size_t inq83_resid = 0; 5008 int dlen, len; 5009 char *sn; 5010 struct sd_lun *un; 5011 5012 ASSERT(ssc != NULL); 5013 un = ssc->ssc_un; 5014 ASSERT(un != NULL); 5015 ASSERT(mutex_owned(SD_MUTEX(un))); 5016 ASSERT((SD_DEVINFO(un)) == devi); 5017 5018 /* 5019 * If transport has already registered a devid for this target 5020 * then that takes precedence over the driver's determination 5021 * of the devid. 5022 */ 5023 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 5024 ASSERT(un->un_devid); 5025 return; /* use devid registered by the transport */ 5026 } 5027 5028 /* 5029 * This is the case of antiquated Sun disk drives that have the 5030 * FAB_DEVID property set in the disk_table. These drives 5031 * manage the devid's by storing them in last 2 available sectors 5032 * on the drive and have them fabricated by the ddi layer by calling 5033 * ddi_devid_init and passing the DEVID_FAB flag. 5034 */ 5035 if (un->un_f_opt_fab_devid == TRUE) { 5036 /* 5037 * Depending on EINVAL isn't reliable, since a reserved disk 5038 * may result in invalid geometry, so check to make sure a 5039 * reservation conflict did not occur during attach. 5040 */ 5041 if ((sd_get_devid(ssc) == EINVAL) && 5042 (reservation_flag != SD_TARGET_IS_RESERVED)) { 5043 /* 5044 * The devid is invalid AND there is no reservation 5045 * conflict. Fabricate a new devid. 5046 */ 5047 (void) sd_create_devid(ssc); 5048 } 5049 5050 /* Register the devid if it exists */ 5051 if (un->un_devid != NULL) { 5052 (void) ddi_devid_register(SD_DEVINFO(un), 5053 un->un_devid); 5054 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5055 "sd_register_devid: Devid Fabricated\n"); 5056 } 5057 return; 5058 } 5059 5060 /* 5061 * We check the availability of the World Wide Name (0x83) and Unit 5062 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 5063 * un_vpd_page_mask from them, we decide which way to get the WWN. If 5064 * 0x83 is available, that is the best choice. Our next choice is 5065 * 0x80. If neither are available, we munge the devid from the device 5066 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 5067 * to fabricate a devid for non-Sun qualified disks. 5068 */ 5069 if (sd_check_vpd_page_support(ssc) == 0) { 5070 /* collect page 80 data if available */ 5071 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 5072 5073 mutex_exit(SD_MUTEX(un)); 5074 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 5075 5076 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len, 5077 0x01, 0x80, &inq80_resid); 5078 5079 if (rval != 0) { 5080 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5081 kmem_free(inq80, inq80_len); 5082 inq80 = NULL; 5083 inq80_len = 0; 5084 } else if (ddi_prop_exists( 5085 DDI_DEV_T_NONE, SD_DEVINFO(un), 5086 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 5087 INQUIRY_SERIAL_NO) == 0) { 5088 /* 5089 * If we don't already have a serial number 5090 * property, do quick verify of data returned 5091 * and define property. 5092 */ 5093 dlen = inq80_len - inq80_resid; 5094 len = (size_t)inq80[3]; 5095 if ((dlen >= 4) && ((len + 4) <= dlen)) { 5096 /* 5097 * Ensure sn termination, skip leading 5098 * blanks, and create property 5099 * 'inquiry-serial-no'. 5100 */ 5101 sn = (char *)&inq80[4]; 5102 sn[len] = 0; 5103 while (*sn && (*sn == ' ')) 5104 sn++; 5105 if (*sn) { 5106 (void) ddi_prop_update_string( 5107 DDI_DEV_T_NONE, 5108 SD_DEVINFO(un), 5109 INQUIRY_SERIAL_NO, sn); 5110 } 5111 } 5112 } 5113 mutex_enter(SD_MUTEX(un)); 5114 } 5115 5116 /* collect page 83 data if available */ 5117 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 5118 mutex_exit(SD_MUTEX(un)); 5119 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 5120 5121 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len, 5122 0x01, 0x83, &inq83_resid); 5123 5124 if (rval != 0) { 5125 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5126 kmem_free(inq83, inq83_len); 5127 inq83 = NULL; 5128 inq83_len = 0; 5129 } 5130 mutex_enter(SD_MUTEX(un)); 5131 } 5132 } 5133 5134 /* encode best devid possible based on data available */ 5135 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 5136 (char *)ddi_driver_name(SD_DEVINFO(un)), 5137 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 5138 inq80, inq80_len - inq80_resid, inq83, inq83_len - 5139 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 5140 5141 /* devid successfully encoded, register devid */ 5142 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 5143 5144 } else { 5145 /* 5146 * Unable to encode a devid based on data available. 5147 * This is not a Sun qualified disk. Older Sun disk 5148 * drives that have the SD_FAB_DEVID property 5149 * set in the disk_table and non Sun qualified 5150 * disks are treated in the same manner. These 5151 * drives manage the devid's by storing them in 5152 * last 2 available sectors on the drive and 5153 * have them fabricated by the ddi layer by 5154 * calling ddi_devid_init and passing the 5155 * DEVID_FAB flag. 5156 * Create a fabricate devid only if there's no 5157 * fabricate devid existed. 5158 */ 5159 if (sd_get_devid(ssc) == EINVAL) { 5160 (void) sd_create_devid(ssc); 5161 } 5162 un->un_f_opt_fab_devid = TRUE; 5163 5164 /* Register the devid if it exists */ 5165 if (un->un_devid != NULL) { 5166 (void) ddi_devid_register(SD_DEVINFO(un), 5167 un->un_devid); 5168 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5169 "sd_register_devid: devid fabricated using " 5170 "ddi framework\n"); 5171 } 5172 } 5173 5174 /* clean up resources */ 5175 if (inq80 != NULL) { 5176 kmem_free(inq80, inq80_len); 5177 } 5178 if (inq83 != NULL) { 5179 kmem_free(inq83, inq83_len); 5180 } 5181 } 5182 5183 5184 5185 /* 5186 * Function: sd_get_devid 5187 * 5188 * Description: This routine will return 0 if a valid device id has been 5189 * obtained from the target and stored in the soft state. If a 5190 * valid device id has not been previously read and stored, a 5191 * read attempt will be made. 5192 * 5193 * Arguments: un - driver soft state (unit) structure 5194 * 5195 * Return Code: 0 if we successfully get the device id 5196 * 5197 * Context: Kernel Thread 5198 */ 5199 5200 static int 5201 sd_get_devid(sd_ssc_t *ssc) 5202 { 5203 struct dk_devid *dkdevid; 5204 ddi_devid_t tmpid; 5205 uint_t *ip; 5206 size_t sz; 5207 diskaddr_t blk; 5208 int status; 5209 int chksum; 5210 int i; 5211 size_t buffer_size; 5212 struct sd_lun *un; 5213 5214 ASSERT(ssc != NULL); 5215 un = ssc->ssc_un; 5216 ASSERT(un != NULL); 5217 ASSERT(mutex_owned(SD_MUTEX(un))); 5218 5219 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 5220 un); 5221 5222 if (un->un_devid != NULL) { 5223 return (0); 5224 } 5225 5226 mutex_exit(SD_MUTEX(un)); 5227 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5228 (void *)SD_PATH_DIRECT) != 0) { 5229 mutex_enter(SD_MUTEX(un)); 5230 return (EINVAL); 5231 } 5232 5233 /* 5234 * Read and verify device id, stored in the reserved cylinders at the 5235 * end of the disk. Backup label is on the odd sectors of the last 5236 * track of the last cylinder. Device id will be on track of the next 5237 * to last cylinder. 5238 */ 5239 mutex_enter(SD_MUTEX(un)); 5240 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 5241 mutex_exit(SD_MUTEX(un)); 5242 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 5243 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk, 5244 SD_PATH_DIRECT); 5245 5246 if (status != 0) { 5247 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5248 goto error; 5249 } 5250 5251 /* Validate the revision */ 5252 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 5253 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 5254 status = EINVAL; 5255 goto error; 5256 } 5257 5258 /* Calculate the checksum */ 5259 chksum = 0; 5260 ip = (uint_t *)dkdevid; 5261 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 5262 i++) { 5263 chksum ^= ip[i]; 5264 } 5265 5266 /* Compare the checksums */ 5267 if (DKD_GETCHKSUM(dkdevid) != chksum) { 5268 status = EINVAL; 5269 goto error; 5270 } 5271 5272 /* Validate the device id */ 5273 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 5274 status = EINVAL; 5275 goto error; 5276 } 5277 5278 /* 5279 * Store the device id in the driver soft state 5280 */ 5281 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 5282 tmpid = kmem_alloc(sz, KM_SLEEP); 5283 5284 mutex_enter(SD_MUTEX(un)); 5285 5286 un->un_devid = tmpid; 5287 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 5288 5289 kmem_free(dkdevid, buffer_size); 5290 5291 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 5292 5293 return (status); 5294 error: 5295 mutex_enter(SD_MUTEX(un)); 5296 kmem_free(dkdevid, buffer_size); 5297 return (status); 5298 } 5299 5300 5301 /* 5302 * Function: sd_create_devid 5303 * 5304 * Description: This routine will fabricate the device id and write it 5305 * to the disk. 5306 * 5307 * Arguments: un - driver soft state (unit) structure 5308 * 5309 * Return Code: value of the fabricated device id 5310 * 5311 * Context: Kernel Thread 5312 */ 5313 5314 static ddi_devid_t 5315 sd_create_devid(sd_ssc_t *ssc) 5316 { 5317 struct sd_lun *un; 5318 5319 ASSERT(ssc != NULL); 5320 un = ssc->ssc_un; 5321 ASSERT(un != NULL); 5322 5323 /* Fabricate the devid */ 5324 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 5325 == DDI_FAILURE) { 5326 return (NULL); 5327 } 5328 5329 /* Write the devid to disk */ 5330 if (sd_write_deviceid(ssc) != 0) { 5331 ddi_devid_free(un->un_devid); 5332 un->un_devid = NULL; 5333 } 5334 5335 return (un->un_devid); 5336 } 5337 5338 5339 /* 5340 * Function: sd_write_deviceid 5341 * 5342 * Description: This routine will write the device id to the disk 5343 * reserved sector. 5344 * 5345 * Arguments: un - driver soft state (unit) structure 5346 * 5347 * Return Code: EINVAL 5348 * value returned by sd_send_scsi_cmd 5349 * 5350 * Context: Kernel Thread 5351 */ 5352 5353 static int 5354 sd_write_deviceid(sd_ssc_t *ssc) 5355 { 5356 struct dk_devid *dkdevid; 5357 diskaddr_t blk; 5358 uint_t *ip, chksum; 5359 int status; 5360 int i; 5361 struct sd_lun *un; 5362 5363 ASSERT(ssc != NULL); 5364 un = ssc->ssc_un; 5365 ASSERT(un != NULL); 5366 ASSERT(mutex_owned(SD_MUTEX(un))); 5367 5368 mutex_exit(SD_MUTEX(un)); 5369 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 5370 (void *)SD_PATH_DIRECT) != 0) { 5371 mutex_enter(SD_MUTEX(un)); 5372 return (-1); 5373 } 5374 5375 5376 /* Allocate the buffer */ 5377 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 5378 5379 /* Fill in the revision */ 5380 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 5381 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 5382 5383 /* Copy in the device id */ 5384 mutex_enter(SD_MUTEX(un)); 5385 bcopy(un->un_devid, &dkdevid->dkd_devid, 5386 ddi_devid_sizeof(un->un_devid)); 5387 mutex_exit(SD_MUTEX(un)); 5388 5389 /* Calculate the checksum */ 5390 chksum = 0; 5391 ip = (uint_t *)dkdevid; 5392 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 5393 i++) { 5394 chksum ^= ip[i]; 5395 } 5396 5397 /* Fill-in checksum */ 5398 DKD_FORMCHKSUM(chksum, dkdevid); 5399 5400 /* Write the reserved sector */ 5401 status = sd_send_scsi_WRITE(ssc, dkdevid, un->un_sys_blocksize, blk, 5402 SD_PATH_DIRECT); 5403 if (status != 0) 5404 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5405 5406 kmem_free(dkdevid, un->un_sys_blocksize); 5407 5408 mutex_enter(SD_MUTEX(un)); 5409 return (status); 5410 } 5411 5412 5413 /* 5414 * Function: sd_check_vpd_page_support 5415 * 5416 * Description: This routine sends an inquiry command with the EVPD bit set and 5417 * a page code of 0x00 to the device. It is used to determine which 5418 * vital product pages are available to find the devid. We are 5419 * looking for pages 0x83 or 0x80. If we return a negative 1, the 5420 * device does not support that command. 5421 * 5422 * Arguments: un - driver soft state (unit) structure 5423 * 5424 * Return Code: 0 - success 5425 * 1 - check condition 5426 * 5427 * Context: This routine can sleep. 5428 */ 5429 5430 static int 5431 sd_check_vpd_page_support(sd_ssc_t *ssc) 5432 { 5433 uchar_t *page_list = NULL; 5434 uchar_t page_length = 0xff; /* Use max possible length */ 5435 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5436 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5437 int rval = 0; 5438 int counter; 5439 struct sd_lun *un; 5440 5441 ASSERT(ssc != NULL); 5442 un = ssc->ssc_un; 5443 ASSERT(un != NULL); 5444 ASSERT(mutex_owned(SD_MUTEX(un))); 5445 5446 mutex_exit(SD_MUTEX(un)); 5447 5448 /* 5449 * We'll set the page length to the maximum to save figuring it out 5450 * with an additional call. 5451 */ 5452 page_list = kmem_zalloc(page_length, KM_SLEEP); 5453 5454 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd, 5455 page_code, NULL); 5456 5457 if (rval != 0) 5458 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5459 5460 mutex_enter(SD_MUTEX(un)); 5461 5462 /* 5463 * Now we must validate that the device accepted the command, as some 5464 * drives do not support it. If the drive does support it, we will 5465 * return 0, and the supported pages will be in un_vpd_page_mask. If 5466 * not, we return -1. 5467 */ 5468 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5469 /* Loop to find one of the 2 pages we need */ 5470 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5471 5472 /* 5473 * Pages are returned in ascending order, and 0x83 is what we 5474 * are hoping for. 5475 */ 5476 while ((page_list[counter] <= 0x86) && 5477 (counter <= (page_list[VPD_PAGE_LENGTH] + 5478 VPD_HEAD_OFFSET))) { 5479 /* 5480 * Add 3 because page_list[3] is the number of 5481 * pages minus 3 5482 */ 5483 5484 switch (page_list[counter]) { 5485 case 0x00: 5486 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5487 break; 5488 case 0x80: 5489 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5490 break; 5491 case 0x81: 5492 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5493 break; 5494 case 0x82: 5495 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5496 break; 5497 case 0x83: 5498 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5499 break; 5500 case 0x86: 5501 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5502 break; 5503 } 5504 counter++; 5505 } 5506 5507 } else { 5508 rval = -1; 5509 5510 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5511 "sd_check_vpd_page_support: This drive does not implement " 5512 "VPD pages.\n"); 5513 } 5514 5515 kmem_free(page_list, page_length); 5516 5517 return (rval); 5518 } 5519 5520 5521 /* 5522 * Function: sd_setup_pm 5523 * 5524 * Description: Initialize Power Management on the device 5525 * 5526 * Context: Kernel Thread 5527 */ 5528 5529 static void 5530 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi) 5531 { 5532 uint_t log_page_size; 5533 uchar_t *log_page_data; 5534 int rval = 0; 5535 struct sd_lun *un; 5536 5537 ASSERT(ssc != NULL); 5538 un = ssc->ssc_un; 5539 ASSERT(un != NULL); 5540 5541 /* 5542 * Since we are called from attach, holding a mutex for 5543 * un is unnecessary. Because some of the routines called 5544 * from here require SD_MUTEX to not be held, assert this 5545 * right up front. 5546 */ 5547 ASSERT(!mutex_owned(SD_MUTEX(un))); 5548 /* 5549 * Since the sd device does not have the 'reg' property, 5550 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5551 * The following code is to tell cpr that this device 5552 * DOES need to be suspended and resumed. 5553 */ 5554 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5555 "pm-hardware-state", "needs-suspend-resume"); 5556 5557 /* 5558 * This complies with the new power management framework 5559 * for certain desktop machines. Create the pm_components 5560 * property as a string array property. 5561 */ 5562 if (un->un_f_pm_supported) { 5563 /* 5564 * not all devices have a motor, try it first. 5565 * some devices may return ILLEGAL REQUEST, some 5566 * will hang 5567 * The following START_STOP_UNIT is used to check if target 5568 * device has a motor. 5569 */ 5570 un->un_f_start_stop_supported = TRUE; 5571 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_START, 5572 SD_PATH_DIRECT); 5573 5574 if (rval != 0) { 5575 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5576 un->un_f_start_stop_supported = FALSE; 5577 } 5578 5579 /* 5580 * create pm properties anyways otherwise the parent can't 5581 * go to sleep 5582 */ 5583 (void) sd_create_pm_components(devi, un); 5584 un->un_f_pm_is_enabled = TRUE; 5585 return; 5586 } 5587 5588 if (!un->un_f_log_sense_supported) { 5589 un->un_power_level = SD_SPINDLE_ON; 5590 un->un_f_pm_is_enabled = FALSE; 5591 return; 5592 } 5593 5594 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE); 5595 5596 #ifdef SDDEBUG 5597 if (sd_force_pm_supported) { 5598 /* Force a successful result */ 5599 rval = 1; 5600 } 5601 #endif 5602 5603 /* 5604 * If the start-stop cycle counter log page is not supported 5605 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 5606 * then we should not create the pm_components property. 5607 */ 5608 if (rval == -1) { 5609 /* 5610 * Error. 5611 * Reading log sense failed, most likely this is 5612 * an older drive that does not support log sense. 5613 * If this fails auto-pm is not supported. 5614 */ 5615 un->un_power_level = SD_SPINDLE_ON; 5616 un->un_f_pm_is_enabled = FALSE; 5617 5618 } else if (rval == 0) { 5619 /* 5620 * Page not found. 5621 * The start stop cycle counter is implemented as page 5622 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5623 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 5624 */ 5625 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) { 5626 /* 5627 * Page found, use this one. 5628 */ 5629 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 5630 un->un_f_pm_is_enabled = TRUE; 5631 } else { 5632 /* 5633 * Error or page not found. 5634 * auto-pm is not supported for this device. 5635 */ 5636 un->un_power_level = SD_SPINDLE_ON; 5637 un->un_f_pm_is_enabled = FALSE; 5638 } 5639 } else { 5640 /* 5641 * Page found, use it. 5642 */ 5643 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 5644 un->un_f_pm_is_enabled = TRUE; 5645 } 5646 5647 5648 if (un->un_f_pm_is_enabled == TRUE) { 5649 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5650 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5651 5652 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 5653 log_page_size, un->un_start_stop_cycle_page, 5654 0x01, 0, SD_PATH_DIRECT); 5655 5656 if (rval != 0) { 5657 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 5658 } 5659 5660 #ifdef SDDEBUG 5661 if (sd_force_pm_supported) { 5662 /* Force a successful result */ 5663 rval = 0; 5664 } 5665 #endif 5666 5667 /* 5668 * If the Log sense for Page( Start/stop cycle counter page) 5669 * succeeds, then power management is supported and we can 5670 * enable auto-pm. 5671 */ 5672 if (rval == 0) { 5673 (void) sd_create_pm_components(devi, un); 5674 } else { 5675 un->un_power_level = SD_SPINDLE_ON; 5676 un->un_f_pm_is_enabled = FALSE; 5677 } 5678 5679 kmem_free(log_page_data, log_page_size); 5680 } 5681 } 5682 5683 5684 /* 5685 * Function: sd_create_pm_components 5686 * 5687 * Description: Initialize PM property. 5688 * 5689 * Context: Kernel thread context 5690 */ 5691 5692 static void 5693 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 5694 { 5695 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 5696 5697 ASSERT(!mutex_owned(SD_MUTEX(un))); 5698 5699 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 5700 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 5701 /* 5702 * When components are initially created they are idle, 5703 * power up any non-removables. 5704 * Note: the return value of pm_raise_power can't be used 5705 * for determining if PM should be enabled for this device. 5706 * Even if you check the return values and remove this 5707 * property created above, the PM framework will not honor the 5708 * change after the first call to pm_raise_power. Hence, 5709 * removal of that property does not help if pm_raise_power 5710 * fails. In the case of removable media, the start/stop 5711 * will fail if the media is not present. 5712 */ 5713 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 5714 SD_SPINDLE_ON) == DDI_SUCCESS)) { 5715 mutex_enter(SD_MUTEX(un)); 5716 un->un_power_level = SD_SPINDLE_ON; 5717 mutex_enter(&un->un_pm_mutex); 5718 /* Set to on and not busy. */ 5719 un->un_pm_count = 0; 5720 } else { 5721 mutex_enter(SD_MUTEX(un)); 5722 un->un_power_level = SD_SPINDLE_OFF; 5723 mutex_enter(&un->un_pm_mutex); 5724 /* Set to off. */ 5725 un->un_pm_count = -1; 5726 } 5727 mutex_exit(&un->un_pm_mutex); 5728 mutex_exit(SD_MUTEX(un)); 5729 } else { 5730 un->un_power_level = SD_SPINDLE_ON; 5731 un->un_f_pm_is_enabled = FALSE; 5732 } 5733 } 5734 5735 5736 /* 5737 * Function: sd_ddi_suspend 5738 * 5739 * Description: Performs system power-down operations. This includes 5740 * setting the drive state to indicate its suspended so 5741 * that no new commands will be accepted. Also, wait for 5742 * all commands that are in transport or queued to a timer 5743 * for retry to complete. All timeout threads are cancelled. 5744 * 5745 * Return Code: DDI_FAILURE or DDI_SUCCESS 5746 * 5747 * Context: Kernel thread context 5748 */ 5749 5750 static int 5751 sd_ddi_suspend(dev_info_t *devi) 5752 { 5753 struct sd_lun *un; 5754 clock_t wait_cmds_complete; 5755 5756 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5757 if (un == NULL) { 5758 return (DDI_FAILURE); 5759 } 5760 5761 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 5762 5763 mutex_enter(SD_MUTEX(un)); 5764 5765 /* Return success if the device is already suspended. */ 5766 if (un->un_state == SD_STATE_SUSPENDED) { 5767 mutex_exit(SD_MUTEX(un)); 5768 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5769 "device already suspended, exiting\n"); 5770 return (DDI_SUCCESS); 5771 } 5772 5773 /* Return failure if the device is being used by HA */ 5774 if (un->un_resvd_status & 5775 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 5776 mutex_exit(SD_MUTEX(un)); 5777 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5778 "device in use by HA, exiting\n"); 5779 return (DDI_FAILURE); 5780 } 5781 5782 /* 5783 * Return failure if the device is in a resource wait 5784 * or power changing state. 5785 */ 5786 if ((un->un_state == SD_STATE_RWAIT) || 5787 (un->un_state == SD_STATE_PM_CHANGING)) { 5788 mutex_exit(SD_MUTEX(un)); 5789 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5790 "device in resource wait state, exiting\n"); 5791 return (DDI_FAILURE); 5792 } 5793 5794 5795 un->un_save_state = un->un_last_state; 5796 New_state(un, SD_STATE_SUSPENDED); 5797 5798 /* 5799 * Wait for all commands that are in transport or queued to a timer 5800 * for retry to complete. 5801 * 5802 * While waiting, no new commands will be accepted or sent because of 5803 * the new state we set above. 5804 * 5805 * Wait till current operation has completed. If we are in the resource 5806 * wait state (with an intr outstanding) then we need to wait till the 5807 * intr completes and starts the next cmd. We want to wait for 5808 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 5809 */ 5810 wait_cmds_complete = ddi_get_lbolt() + 5811 (sd_wait_cmds_complete * drv_usectohz(1000000)); 5812 5813 while (un->un_ncmds_in_transport != 0) { 5814 /* 5815 * Fail if commands do not finish in the specified time. 5816 */ 5817 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 5818 wait_cmds_complete) == -1) { 5819 /* 5820 * Undo the state changes made above. Everything 5821 * must go back to it's original value. 5822 */ 5823 Restore_state(un); 5824 un->un_last_state = un->un_save_state; 5825 /* Wake up any threads that might be waiting. */ 5826 cv_broadcast(&un->un_suspend_cv); 5827 mutex_exit(SD_MUTEX(un)); 5828 SD_ERROR(SD_LOG_IO_PM, un, 5829 "sd_ddi_suspend: failed due to outstanding cmds\n"); 5830 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 5831 return (DDI_FAILURE); 5832 } 5833 } 5834 5835 /* 5836 * Cancel SCSI watch thread and timeouts, if any are active 5837 */ 5838 5839 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 5840 opaque_t temp_token = un->un_swr_token; 5841 mutex_exit(SD_MUTEX(un)); 5842 scsi_watch_suspend(temp_token); 5843 mutex_enter(SD_MUTEX(un)); 5844 } 5845 5846 if (un->un_reset_throttle_timeid != NULL) { 5847 timeout_id_t temp_id = un->un_reset_throttle_timeid; 5848 un->un_reset_throttle_timeid = NULL; 5849 mutex_exit(SD_MUTEX(un)); 5850 (void) untimeout(temp_id); 5851 mutex_enter(SD_MUTEX(un)); 5852 } 5853 5854 if (un->un_dcvb_timeid != NULL) { 5855 timeout_id_t temp_id = un->un_dcvb_timeid; 5856 un->un_dcvb_timeid = NULL; 5857 mutex_exit(SD_MUTEX(un)); 5858 (void) untimeout(temp_id); 5859 mutex_enter(SD_MUTEX(un)); 5860 } 5861 5862 mutex_enter(&un->un_pm_mutex); 5863 if (un->un_pm_timeid != NULL) { 5864 timeout_id_t temp_id = un->un_pm_timeid; 5865 un->un_pm_timeid = NULL; 5866 mutex_exit(&un->un_pm_mutex); 5867 mutex_exit(SD_MUTEX(un)); 5868 (void) untimeout(temp_id); 5869 mutex_enter(SD_MUTEX(un)); 5870 } else { 5871 mutex_exit(&un->un_pm_mutex); 5872 } 5873 5874 if (un->un_retry_timeid != NULL) { 5875 timeout_id_t temp_id = un->un_retry_timeid; 5876 un->un_retry_timeid = NULL; 5877 mutex_exit(SD_MUTEX(un)); 5878 (void) untimeout(temp_id); 5879 mutex_enter(SD_MUTEX(un)); 5880 5881 if (un->un_retry_bp != NULL) { 5882 un->un_retry_bp->av_forw = un->un_waitq_headp; 5883 un->un_waitq_headp = un->un_retry_bp; 5884 if (un->un_waitq_tailp == NULL) { 5885 un->un_waitq_tailp = un->un_retry_bp; 5886 } 5887 un->un_retry_bp = NULL; 5888 un->un_retry_statp = NULL; 5889 } 5890 } 5891 5892 if (un->un_direct_priority_timeid != NULL) { 5893 timeout_id_t temp_id = un->un_direct_priority_timeid; 5894 un->un_direct_priority_timeid = NULL; 5895 mutex_exit(SD_MUTEX(un)); 5896 (void) untimeout(temp_id); 5897 mutex_enter(SD_MUTEX(un)); 5898 } 5899 5900 if (un->un_f_is_fibre == TRUE) { 5901 /* 5902 * Remove callbacks for insert and remove events 5903 */ 5904 if (un->un_insert_event != NULL) { 5905 mutex_exit(SD_MUTEX(un)); 5906 (void) ddi_remove_event_handler(un->un_insert_cb_id); 5907 mutex_enter(SD_MUTEX(un)); 5908 un->un_insert_event = NULL; 5909 } 5910 5911 if (un->un_remove_event != NULL) { 5912 mutex_exit(SD_MUTEX(un)); 5913 (void) ddi_remove_event_handler(un->un_remove_cb_id); 5914 mutex_enter(SD_MUTEX(un)); 5915 un->un_remove_event = NULL; 5916 } 5917 } 5918 5919 mutex_exit(SD_MUTEX(un)); 5920 5921 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 5922 5923 return (DDI_SUCCESS); 5924 } 5925 5926 5927 /* 5928 * Function: sd_ddi_pm_suspend 5929 * 5930 * Description: Set the drive state to low power. 5931 * Someone else is required to actually change the drive 5932 * power level. 5933 * 5934 * Arguments: un - driver soft state (unit) structure 5935 * 5936 * Return Code: DDI_FAILURE or DDI_SUCCESS 5937 * 5938 * Context: Kernel thread context 5939 */ 5940 5941 static int 5942 sd_ddi_pm_suspend(struct sd_lun *un) 5943 { 5944 ASSERT(un != NULL); 5945 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 5946 5947 ASSERT(!mutex_owned(SD_MUTEX(un))); 5948 mutex_enter(SD_MUTEX(un)); 5949 5950 /* 5951 * Exit if power management is not enabled for this device, or if 5952 * the device is being used by HA. 5953 */ 5954 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 5955 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 5956 mutex_exit(SD_MUTEX(un)); 5957 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 5958 return (DDI_SUCCESS); 5959 } 5960 5961 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 5962 un->un_ncmds_in_driver); 5963 5964 /* 5965 * See if the device is not busy, ie.: 5966 * - we have no commands in the driver for this device 5967 * - not waiting for resources 5968 */ 5969 if ((un->un_ncmds_in_driver == 0) && 5970 (un->un_state != SD_STATE_RWAIT)) { 5971 /* 5972 * The device is not busy, so it is OK to go to low power state. 5973 * Indicate low power, but rely on someone else to actually 5974 * change it. 5975 */ 5976 mutex_enter(&un->un_pm_mutex); 5977 un->un_pm_count = -1; 5978 mutex_exit(&un->un_pm_mutex); 5979 un->un_power_level = SD_SPINDLE_OFF; 5980 } 5981 5982 mutex_exit(SD_MUTEX(un)); 5983 5984 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 5985 5986 return (DDI_SUCCESS); 5987 } 5988 5989 5990 /* 5991 * Function: sd_ddi_resume 5992 * 5993 * Description: Performs system power-up operations.. 5994 * 5995 * Return Code: DDI_SUCCESS 5996 * DDI_FAILURE 5997 * 5998 * Context: Kernel thread context 5999 */ 6000 6001 static int 6002 sd_ddi_resume(dev_info_t *devi) 6003 { 6004 struct sd_lun *un; 6005 6006 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6007 if (un == NULL) { 6008 return (DDI_FAILURE); 6009 } 6010 6011 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 6012 6013 mutex_enter(SD_MUTEX(un)); 6014 Restore_state(un); 6015 6016 /* 6017 * Restore the state which was saved to give the 6018 * the right state in un_last_state 6019 */ 6020 un->un_last_state = un->un_save_state; 6021 /* 6022 * Note: throttle comes back at full. 6023 * Also note: this MUST be done before calling pm_raise_power 6024 * otherwise the system can get hung in biowait. The scenario where 6025 * this'll happen is under cpr suspend. Writing of the system 6026 * state goes through sddump, which writes 0 to un_throttle. If 6027 * writing the system state then fails, example if the partition is 6028 * too small, then cpr attempts a resume. If throttle isn't restored 6029 * from the saved value until after calling pm_raise_power then 6030 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 6031 * in biowait. 6032 */ 6033 un->un_throttle = un->un_saved_throttle; 6034 6035 /* 6036 * The chance of failure is very rare as the only command done in power 6037 * entry point is START command when you transition from 0->1 or 6038 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 6039 * which suspend was done. Ignore the return value as the resume should 6040 * not be failed. In the case of removable media the media need not be 6041 * inserted and hence there is a chance that raise power will fail with 6042 * media not present. 6043 */ 6044 if (un->un_f_attach_spinup) { 6045 mutex_exit(SD_MUTEX(un)); 6046 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 6047 mutex_enter(SD_MUTEX(un)); 6048 } 6049 6050 /* 6051 * Don't broadcast to the suspend cv and therefore possibly 6052 * start I/O until after power has been restored. 6053 */ 6054 cv_broadcast(&un->un_suspend_cv); 6055 cv_broadcast(&un->un_state_cv); 6056 6057 /* restart thread */ 6058 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 6059 scsi_watch_resume(un->un_swr_token); 6060 } 6061 6062 #if (defined(__fibre)) 6063 if (un->un_f_is_fibre == TRUE) { 6064 /* 6065 * Add callbacks for insert and remove events 6066 */ 6067 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 6068 sd_init_event_callbacks(un); 6069 } 6070 } 6071 #endif 6072 6073 /* 6074 * Transport any pending commands to the target. 6075 * 6076 * If this is a low-activity device commands in queue will have to wait 6077 * until new commands come in, which may take awhile. Also, we 6078 * specifically don't check un_ncmds_in_transport because we know that 6079 * there really are no commands in progress after the unit was 6080 * suspended and we could have reached the throttle level, been 6081 * suspended, and have no new commands coming in for awhile. Highly 6082 * unlikely, but so is the low-activity disk scenario. 6083 */ 6084 ddi_xbuf_dispatch(un->un_xbuf_attr); 6085 6086 sd_start_cmds(un, NULL); 6087 mutex_exit(SD_MUTEX(un)); 6088 6089 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 6090 6091 return (DDI_SUCCESS); 6092 } 6093 6094 6095 /* 6096 * Function: sd_ddi_pm_resume 6097 * 6098 * Description: Set the drive state to powered on. 6099 * Someone else is required to actually change the drive 6100 * power level. 6101 * 6102 * Arguments: un - driver soft state (unit) structure 6103 * 6104 * Return Code: DDI_SUCCESS 6105 * 6106 * Context: Kernel thread context 6107 */ 6108 6109 static int 6110 sd_ddi_pm_resume(struct sd_lun *un) 6111 { 6112 ASSERT(un != NULL); 6113 6114 ASSERT(!mutex_owned(SD_MUTEX(un))); 6115 mutex_enter(SD_MUTEX(un)); 6116 un->un_power_level = SD_SPINDLE_ON; 6117 6118 ASSERT(!mutex_owned(&un->un_pm_mutex)); 6119 mutex_enter(&un->un_pm_mutex); 6120 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 6121 un->un_pm_count++; 6122 ASSERT(un->un_pm_count == 0); 6123 /* 6124 * Note: no longer do the cv_broadcast on un_suspend_cv. The 6125 * un_suspend_cv is for a system resume, not a power management 6126 * device resume. (4297749) 6127 * cv_broadcast(&un->un_suspend_cv); 6128 */ 6129 } 6130 mutex_exit(&un->un_pm_mutex); 6131 mutex_exit(SD_MUTEX(un)); 6132 6133 return (DDI_SUCCESS); 6134 } 6135 6136 6137 /* 6138 * Function: sd_pm_idletimeout_handler 6139 * 6140 * Description: A timer routine that's active only while a device is busy. 6141 * The purpose is to extend slightly the pm framework's busy 6142 * view of the device to prevent busy/idle thrashing for 6143 * back-to-back commands. Do this by comparing the current time 6144 * to the time at which the last command completed and when the 6145 * difference is greater than sd_pm_idletime, call 6146 * pm_idle_component. In addition to indicating idle to the pm 6147 * framework, update the chain type to again use the internal pm 6148 * layers of the driver. 6149 * 6150 * Arguments: arg - driver soft state (unit) structure 6151 * 6152 * Context: Executes in a timeout(9F) thread context 6153 */ 6154 6155 static void 6156 sd_pm_idletimeout_handler(void *arg) 6157 { 6158 struct sd_lun *un = arg; 6159 6160 time_t now; 6161 6162 mutex_enter(&sd_detach_mutex); 6163 if (un->un_detach_count != 0) { 6164 /* Abort if the instance is detaching */ 6165 mutex_exit(&sd_detach_mutex); 6166 return; 6167 } 6168 mutex_exit(&sd_detach_mutex); 6169 6170 now = ddi_get_time(); 6171 /* 6172 * Grab both mutexes, in the proper order, since we're accessing 6173 * both PM and softstate variables. 6174 */ 6175 mutex_enter(SD_MUTEX(un)); 6176 mutex_enter(&un->un_pm_mutex); 6177 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 6178 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 6179 /* 6180 * Update the chain types. 6181 * This takes affect on the next new command received. 6182 */ 6183 if (un->un_f_non_devbsize_supported) { 6184 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6185 } else { 6186 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6187 } 6188 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6189 6190 SD_TRACE(SD_LOG_IO_PM, un, 6191 "sd_pm_idletimeout_handler: idling device\n"); 6192 (void) pm_idle_component(SD_DEVINFO(un), 0); 6193 un->un_pm_idle_timeid = NULL; 6194 } else { 6195 un->un_pm_idle_timeid = 6196 timeout(sd_pm_idletimeout_handler, un, 6197 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 6198 } 6199 mutex_exit(&un->un_pm_mutex); 6200 mutex_exit(SD_MUTEX(un)); 6201 } 6202 6203 6204 /* 6205 * Function: sd_pm_timeout_handler 6206 * 6207 * Description: Callback to tell framework we are idle. 6208 * 6209 * Context: timeout(9f) thread context. 6210 */ 6211 6212 static void 6213 sd_pm_timeout_handler(void *arg) 6214 { 6215 struct sd_lun *un = arg; 6216 6217 (void) pm_idle_component(SD_DEVINFO(un), 0); 6218 mutex_enter(&un->un_pm_mutex); 6219 un->un_pm_timeid = NULL; 6220 mutex_exit(&un->un_pm_mutex); 6221 } 6222 6223 6224 /* 6225 * Function: sdpower 6226 * 6227 * Description: PM entry point. 6228 * 6229 * Return Code: DDI_SUCCESS 6230 * DDI_FAILURE 6231 * 6232 * Context: Kernel thread context 6233 */ 6234 6235 static int 6236 sdpower(dev_info_t *devi, int component, int level) 6237 { 6238 struct sd_lun *un; 6239 int instance; 6240 int rval = DDI_SUCCESS; 6241 uint_t i, log_page_size, maxcycles, ncycles; 6242 uchar_t *log_page_data; 6243 int log_sense_page; 6244 int medium_present; 6245 time_t intvlp; 6246 dev_t dev; 6247 struct pm_trans_data sd_pm_tran_data; 6248 uchar_t save_state; 6249 int sval; 6250 uchar_t state_before_pm; 6251 int got_semaphore_here; 6252 sd_ssc_t *ssc; 6253 6254 instance = ddi_get_instance(devi); 6255 6256 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 6257 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 6258 component != 0) { 6259 return (DDI_FAILURE); 6260 } 6261 6262 dev = sd_make_device(SD_DEVINFO(un)); 6263 ssc = sd_ssc_init(un); 6264 6265 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 6266 6267 /* 6268 * Must synchronize power down with close. 6269 * Attempt to decrement/acquire the open/close semaphore, 6270 * but do NOT wait on it. If it's not greater than zero, 6271 * ie. it can't be decremented without waiting, then 6272 * someone else, either open or close, already has it 6273 * and the try returns 0. Use that knowledge here to determine 6274 * if it's OK to change the device power level. 6275 * Also, only increment it on exit if it was decremented, ie. gotten, 6276 * here. 6277 */ 6278 got_semaphore_here = sema_tryp(&un->un_semoclose); 6279 6280 mutex_enter(SD_MUTEX(un)); 6281 6282 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 6283 un->un_ncmds_in_driver); 6284 6285 /* 6286 * If un_ncmds_in_driver is non-zero it indicates commands are 6287 * already being processed in the driver, or if the semaphore was 6288 * not gotten here it indicates an open or close is being processed. 6289 * At the same time somebody is requesting to go low power which 6290 * can't happen, therefore we need to return failure. 6291 */ 6292 if ((level == SD_SPINDLE_OFF) && 6293 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 6294 mutex_exit(SD_MUTEX(un)); 6295 6296 if (got_semaphore_here != 0) { 6297 sema_v(&un->un_semoclose); 6298 } 6299 SD_TRACE(SD_LOG_IO_PM, un, 6300 "sdpower: exit, device has queued cmds.\n"); 6301 6302 goto sdpower_failed; 6303 } 6304 6305 /* 6306 * if it is OFFLINE that means the disk is completely dead 6307 * in our case we have to put the disk in on or off by sending commands 6308 * Of course that will fail anyway so return back here. 6309 * 6310 * Power changes to a device that's OFFLINE or SUSPENDED 6311 * are not allowed. 6312 */ 6313 if ((un->un_state == SD_STATE_OFFLINE) || 6314 (un->un_state == SD_STATE_SUSPENDED)) { 6315 mutex_exit(SD_MUTEX(un)); 6316 6317 if (got_semaphore_here != 0) { 6318 sema_v(&un->un_semoclose); 6319 } 6320 SD_TRACE(SD_LOG_IO_PM, un, 6321 "sdpower: exit, device is off-line.\n"); 6322 6323 goto sdpower_failed; 6324 } 6325 6326 /* 6327 * Change the device's state to indicate it's power level 6328 * is being changed. Do this to prevent a power off in the 6329 * middle of commands, which is especially bad on devices 6330 * that are really powered off instead of just spun down. 6331 */ 6332 state_before_pm = un->un_state; 6333 un->un_state = SD_STATE_PM_CHANGING; 6334 6335 mutex_exit(SD_MUTEX(un)); 6336 6337 /* 6338 * If "pm-capable" property is set to TRUE by HBA drivers, 6339 * bypass the following checking, otherwise, check the log 6340 * sense information for this device 6341 */ 6342 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 6343 /* 6344 * Get the log sense information to understand whether the 6345 * the powercycle counts have gone beyond the threshhold. 6346 */ 6347 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6348 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6349 6350 mutex_enter(SD_MUTEX(un)); 6351 log_sense_page = un->un_start_stop_cycle_page; 6352 mutex_exit(SD_MUTEX(un)); 6353 6354 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 6355 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 6356 6357 if (rval != 0) { 6358 if (rval == EIO) 6359 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6360 else 6361 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6362 } 6363 6364 #ifdef SDDEBUG 6365 if (sd_force_pm_supported) { 6366 /* Force a successful result */ 6367 rval = 0; 6368 } 6369 #endif 6370 if (rval != 0) { 6371 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 6372 "Log Sense Failed\n"); 6373 6374 kmem_free(log_page_data, log_page_size); 6375 /* Cannot support power management on those drives */ 6376 6377 if (got_semaphore_here != 0) { 6378 sema_v(&un->un_semoclose); 6379 } 6380 /* 6381 * On exit put the state back to it's original value 6382 * and broadcast to anyone waiting for the power 6383 * change completion. 6384 */ 6385 mutex_enter(SD_MUTEX(un)); 6386 un->un_state = state_before_pm; 6387 cv_broadcast(&un->un_suspend_cv); 6388 mutex_exit(SD_MUTEX(un)); 6389 SD_TRACE(SD_LOG_IO_PM, un, 6390 "sdpower: exit, Log Sense Failed.\n"); 6391 6392 goto sdpower_failed; 6393 } 6394 6395 /* 6396 * From the page data - Convert the essential information to 6397 * pm_trans_data 6398 */ 6399 maxcycles = 6400 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 6401 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 6402 6403 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 6404 6405 ncycles = 6406 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 6407 (log_page_data[0x26] << 8) | log_page_data[0x27]; 6408 6409 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 6410 6411 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 6412 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 6413 log_page_data[8+i]; 6414 } 6415 6416 kmem_free(log_page_data, log_page_size); 6417 6418 /* 6419 * Call pm_trans_check routine to get the Ok from 6420 * the global policy 6421 */ 6422 6423 sd_pm_tran_data.format = DC_SCSI_FORMAT; 6424 sd_pm_tran_data.un.scsi_cycles.flag = 0; 6425 6426 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 6427 #ifdef SDDEBUG 6428 if (sd_force_pm_supported) { 6429 /* Force a successful result */ 6430 rval = 1; 6431 } 6432 #endif 6433 switch (rval) { 6434 case 0: 6435 /* 6436 * Not Ok to Power cycle or error in parameters passed 6437 * Would have given the advised time to consider power 6438 * cycle. Based on the new intvlp parameter we are 6439 * supposed to pretend we are busy so that pm framework 6440 * will never call our power entry point. Because of 6441 * that install a timeout handler and wait for the 6442 * recommended time to elapse so that power management 6443 * can be effective again. 6444 * 6445 * To effect this behavior, call pm_busy_component to 6446 * indicate to the framework this device is busy. 6447 * By not adjusting un_pm_count the rest of PM in 6448 * the driver will function normally, and independent 6449 * of this but because the framework is told the device 6450 * is busy it won't attempt powering down until it gets 6451 * a matching idle. The timeout handler sends this. 6452 * Note: sd_pm_entry can't be called here to do this 6453 * because sdpower may have been called as a result 6454 * of a call to pm_raise_power from within sd_pm_entry. 6455 * 6456 * If a timeout handler is already active then 6457 * don't install another. 6458 */ 6459 mutex_enter(&un->un_pm_mutex); 6460 if (un->un_pm_timeid == NULL) { 6461 un->un_pm_timeid = 6462 timeout(sd_pm_timeout_handler, 6463 un, intvlp * drv_usectohz(1000000)); 6464 mutex_exit(&un->un_pm_mutex); 6465 (void) pm_busy_component(SD_DEVINFO(un), 0); 6466 } else { 6467 mutex_exit(&un->un_pm_mutex); 6468 } 6469 if (got_semaphore_here != 0) { 6470 sema_v(&un->un_semoclose); 6471 } 6472 /* 6473 * On exit put the state back to it's original value 6474 * and broadcast to anyone waiting for the power 6475 * change completion. 6476 */ 6477 mutex_enter(SD_MUTEX(un)); 6478 un->un_state = state_before_pm; 6479 cv_broadcast(&un->un_suspend_cv); 6480 mutex_exit(SD_MUTEX(un)); 6481 6482 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6483 "trans check Failed, not ok to power cycle.\n"); 6484 6485 goto sdpower_failed; 6486 case -1: 6487 if (got_semaphore_here != 0) { 6488 sema_v(&un->un_semoclose); 6489 } 6490 /* 6491 * On exit put the state back to it's original value 6492 * and broadcast to anyone waiting for the power 6493 * change completion. 6494 */ 6495 mutex_enter(SD_MUTEX(un)); 6496 un->un_state = state_before_pm; 6497 cv_broadcast(&un->un_suspend_cv); 6498 mutex_exit(SD_MUTEX(un)); 6499 SD_TRACE(SD_LOG_IO_PM, un, 6500 "sdpower: exit, trans check command Failed.\n"); 6501 6502 goto sdpower_failed; 6503 } 6504 } 6505 6506 if (level == SD_SPINDLE_OFF) { 6507 /* 6508 * Save the last state... if the STOP FAILS we need it 6509 * for restoring 6510 */ 6511 mutex_enter(SD_MUTEX(un)); 6512 save_state = un->un_last_state; 6513 /* 6514 * There must not be any cmds. getting processed 6515 * in the driver when we get here. Power to the 6516 * device is potentially going off. 6517 */ 6518 ASSERT(un->un_ncmds_in_driver == 0); 6519 mutex_exit(SD_MUTEX(un)); 6520 6521 /* 6522 * For now suspend the device completely before spindle is 6523 * turned off 6524 */ 6525 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 6526 if (got_semaphore_here != 0) { 6527 sema_v(&un->un_semoclose); 6528 } 6529 /* 6530 * On exit put the state back to it's original value 6531 * and broadcast to anyone waiting for the power 6532 * change completion. 6533 */ 6534 mutex_enter(SD_MUTEX(un)); 6535 un->un_state = state_before_pm; 6536 cv_broadcast(&un->un_suspend_cv); 6537 mutex_exit(SD_MUTEX(un)); 6538 SD_TRACE(SD_LOG_IO_PM, un, 6539 "sdpower: exit, PM suspend Failed.\n"); 6540 6541 goto sdpower_failed; 6542 } 6543 } 6544 6545 /* 6546 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6547 * close, or strategy. Dump no long uses this routine, it uses it's 6548 * own code so it can be done in polled mode. 6549 */ 6550 6551 medium_present = TRUE; 6552 6553 /* 6554 * When powering up, issue a TUR in case the device is at unit 6555 * attention. Don't do retries. Bypass the PM layer, otherwise 6556 * a deadlock on un_pm_busy_cv will occur. 6557 */ 6558 if (level == SD_SPINDLE_ON) { 6559 sval = sd_send_scsi_TEST_UNIT_READY(ssc, 6560 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6561 if (sval != 0) 6562 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6563 } 6564 6565 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6566 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6567 6568 sval = sd_send_scsi_START_STOP_UNIT(ssc, 6569 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 6570 SD_PATH_DIRECT); 6571 if (sval != 0) { 6572 if (sval == EIO) 6573 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 6574 else 6575 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 6576 } 6577 6578 /* Command failed, check for media present. */ 6579 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6580 medium_present = FALSE; 6581 } 6582 6583 /* 6584 * The conditions of interest here are: 6585 * if a spindle off with media present fails, 6586 * then restore the state and return an error. 6587 * else if a spindle on fails, 6588 * then return an error (there's no state to restore). 6589 * In all other cases we setup for the new state 6590 * and return success. 6591 */ 6592 switch (level) { 6593 case SD_SPINDLE_OFF: 6594 if ((medium_present == TRUE) && (sval != 0)) { 6595 /* The stop command from above failed */ 6596 rval = DDI_FAILURE; 6597 /* 6598 * The stop command failed, and we have media 6599 * present. Put the level back by calling the 6600 * sd_pm_resume() and set the state back to 6601 * it's previous value. 6602 */ 6603 (void) sd_ddi_pm_resume(un); 6604 mutex_enter(SD_MUTEX(un)); 6605 un->un_last_state = save_state; 6606 mutex_exit(SD_MUTEX(un)); 6607 break; 6608 } 6609 /* 6610 * The stop command from above succeeded. 6611 */ 6612 if (un->un_f_monitor_media_state) { 6613 /* 6614 * Terminate watch thread in case of removable media 6615 * devices going into low power state. This is as per 6616 * the requirements of pm framework, otherwise commands 6617 * will be generated for the device (through watch 6618 * thread), even when the device is in low power state. 6619 */ 6620 mutex_enter(SD_MUTEX(un)); 6621 un->un_f_watcht_stopped = FALSE; 6622 if (un->un_swr_token != NULL) { 6623 opaque_t temp_token = un->un_swr_token; 6624 un->un_f_watcht_stopped = TRUE; 6625 un->un_swr_token = NULL; 6626 mutex_exit(SD_MUTEX(un)); 6627 (void) scsi_watch_request_terminate(temp_token, 6628 SCSI_WATCH_TERMINATE_ALL_WAIT); 6629 } else { 6630 mutex_exit(SD_MUTEX(un)); 6631 } 6632 } 6633 break; 6634 6635 default: /* The level requested is spindle on... */ 6636 /* 6637 * Legacy behavior: return success on a failed spinup 6638 * if there is no media in the drive. 6639 * Do this by looking at medium_present here. 6640 */ 6641 if ((sval != 0) && medium_present) { 6642 /* The start command from above failed */ 6643 rval = DDI_FAILURE; 6644 break; 6645 } 6646 /* 6647 * The start command from above succeeded 6648 * Resume the devices now that we have 6649 * started the disks 6650 */ 6651 (void) sd_ddi_pm_resume(un); 6652 6653 /* 6654 * Resume the watch thread since it was suspended 6655 * when the device went into low power mode. 6656 */ 6657 if (un->un_f_monitor_media_state) { 6658 mutex_enter(SD_MUTEX(un)); 6659 if (un->un_f_watcht_stopped == TRUE) { 6660 opaque_t temp_token; 6661 6662 un->un_f_watcht_stopped = FALSE; 6663 mutex_exit(SD_MUTEX(un)); 6664 temp_token = scsi_watch_request_submit( 6665 SD_SCSI_DEVP(un), 6666 sd_check_media_time, 6667 SENSE_LENGTH, sd_media_watch_cb, 6668 (caddr_t)dev); 6669 mutex_enter(SD_MUTEX(un)); 6670 un->un_swr_token = temp_token; 6671 } 6672 mutex_exit(SD_MUTEX(un)); 6673 } 6674 } 6675 if (got_semaphore_here != 0) { 6676 sema_v(&un->un_semoclose); 6677 } 6678 /* 6679 * On exit put the state back to it's original value 6680 * and broadcast to anyone waiting for the power 6681 * change completion. 6682 */ 6683 mutex_enter(SD_MUTEX(un)); 6684 un->un_state = state_before_pm; 6685 cv_broadcast(&un->un_suspend_cv); 6686 mutex_exit(SD_MUTEX(un)); 6687 6688 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 6689 6690 sd_ssc_fini(ssc); 6691 return (rval); 6692 6693 sdpower_failed: 6694 6695 sd_ssc_fini(ssc); 6696 return (DDI_FAILURE); 6697 } 6698 6699 6700 6701 /* 6702 * Function: sdattach 6703 * 6704 * Description: Driver's attach(9e) entry point function. 6705 * 6706 * Arguments: devi - opaque device info handle 6707 * cmd - attach type 6708 * 6709 * Return Code: DDI_SUCCESS 6710 * DDI_FAILURE 6711 * 6712 * Context: Kernel thread context 6713 */ 6714 6715 static int 6716 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 6717 { 6718 switch (cmd) { 6719 case DDI_ATTACH: 6720 return (sd_unit_attach(devi)); 6721 case DDI_RESUME: 6722 return (sd_ddi_resume(devi)); 6723 default: 6724 break; 6725 } 6726 return (DDI_FAILURE); 6727 } 6728 6729 6730 /* 6731 * Function: sddetach 6732 * 6733 * Description: Driver's detach(9E) entry point function. 6734 * 6735 * Arguments: devi - opaque device info handle 6736 * cmd - detach type 6737 * 6738 * Return Code: DDI_SUCCESS 6739 * DDI_FAILURE 6740 * 6741 * Context: Kernel thread context 6742 */ 6743 6744 static int 6745 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 6746 { 6747 switch (cmd) { 6748 case DDI_DETACH: 6749 return (sd_unit_detach(devi)); 6750 case DDI_SUSPEND: 6751 return (sd_ddi_suspend(devi)); 6752 default: 6753 break; 6754 } 6755 return (DDI_FAILURE); 6756 } 6757 6758 6759 /* 6760 * Function: sd_sync_with_callback 6761 * 6762 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 6763 * state while the callback routine is active. 6764 * 6765 * Arguments: un: softstate structure for the instance 6766 * 6767 * Context: Kernel thread context 6768 */ 6769 6770 static void 6771 sd_sync_with_callback(struct sd_lun *un) 6772 { 6773 ASSERT(un != NULL); 6774 6775 mutex_enter(SD_MUTEX(un)); 6776 6777 ASSERT(un->un_in_callback >= 0); 6778 6779 while (un->un_in_callback > 0) { 6780 mutex_exit(SD_MUTEX(un)); 6781 delay(2); 6782 mutex_enter(SD_MUTEX(un)); 6783 } 6784 6785 mutex_exit(SD_MUTEX(un)); 6786 } 6787 6788 /* 6789 * Function: sd_unit_attach 6790 * 6791 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 6792 * the soft state structure for the device and performs 6793 * all necessary structure and device initializations. 6794 * 6795 * Arguments: devi: the system's dev_info_t for the device. 6796 * 6797 * Return Code: DDI_SUCCESS if attach is successful. 6798 * DDI_FAILURE if any part of the attach fails. 6799 * 6800 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 6801 * Kernel thread context only. Can sleep. 6802 */ 6803 6804 static int 6805 sd_unit_attach(dev_info_t *devi) 6806 { 6807 struct scsi_device *devp; 6808 struct sd_lun *un; 6809 char *variantp; 6810 int reservation_flag = SD_TARGET_IS_UNRESERVED; 6811 int instance; 6812 int rval; 6813 int wc_enabled; 6814 int tgt; 6815 uint64_t capacity; 6816 uint_t lbasize = 0; 6817 dev_info_t *pdip = ddi_get_parent(devi); 6818 int offbyone = 0; 6819 int geom_label_valid = 0; 6820 sd_ssc_t *ssc; 6821 int status; 6822 struct sd_fm_internal *sfip = NULL; 6823 #if defined(__sparc) 6824 int max_xfer_size; 6825 #endif 6826 6827 /* 6828 * Retrieve the target driver's private data area. This was set 6829 * up by the HBA. 6830 */ 6831 devp = ddi_get_driver_private(devi); 6832 6833 /* 6834 * Retrieve the target ID of the device. 6835 */ 6836 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6837 SCSI_ADDR_PROP_TARGET, -1); 6838 6839 /* 6840 * Since we have no idea what state things were left in by the last 6841 * user of the device, set up some 'default' settings, ie. turn 'em 6842 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 6843 * Do this before the scsi_probe, which sends an inquiry. 6844 * This is a fix for bug (4430280). 6845 * Of special importance is wide-xfer. The drive could have been left 6846 * in wide transfer mode by the last driver to communicate with it, 6847 * this includes us. If that's the case, and if the following is not 6848 * setup properly or we don't re-negotiate with the drive prior to 6849 * transferring data to/from the drive, it causes bus parity errors, 6850 * data overruns, and unexpected interrupts. This first occurred when 6851 * the fix for bug (4378686) was made. 6852 */ 6853 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 6854 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 6855 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 6856 6857 /* 6858 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 6859 * on a target. Setting it per lun instance actually sets the 6860 * capability of this target, which affects those luns already 6861 * attached on the same target. So during attach, we can only disable 6862 * this capability only when no other lun has been attached on this 6863 * target. By doing this, we assume a target has the same tagged-qing 6864 * capability for every lun. The condition can be removed when HBA 6865 * is changed to support per lun based tagged-qing capability. 6866 */ 6867 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 6868 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 6869 } 6870 6871 /* 6872 * Use scsi_probe() to issue an INQUIRY command to the device. 6873 * This call will allocate and fill in the scsi_inquiry structure 6874 * and point the sd_inq member of the scsi_device structure to it. 6875 * If the attach succeeds, then this memory will not be de-allocated 6876 * (via scsi_unprobe()) until the instance is detached. 6877 */ 6878 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 6879 goto probe_failed; 6880 } 6881 6882 /* 6883 * Check the device type as specified in the inquiry data and 6884 * claim it if it is of a type that we support. 6885 */ 6886 switch (devp->sd_inq->inq_dtype) { 6887 case DTYPE_DIRECT: 6888 break; 6889 case DTYPE_RODIRECT: 6890 break; 6891 case DTYPE_OPTICAL: 6892 break; 6893 case DTYPE_NOTPRESENT: 6894 default: 6895 /* Unsupported device type; fail the attach. */ 6896 goto probe_failed; 6897 } 6898 6899 /* 6900 * Allocate the soft state structure for this unit. 6901 * 6902 * We rely upon this memory being set to all zeroes by 6903 * ddi_soft_state_zalloc(). We assume that any member of the 6904 * soft state structure that is not explicitly initialized by 6905 * this routine will have a value of zero. 6906 */ 6907 instance = ddi_get_instance(devp->sd_dev); 6908 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 6909 goto probe_failed; 6910 } 6911 6912 /* 6913 * Retrieve a pointer to the newly-allocated soft state. 6914 * 6915 * This should NEVER fail if the ddi_soft_state_zalloc() call above 6916 * was successful, unless something has gone horribly wrong and the 6917 * ddi's soft state internals are corrupt (in which case it is 6918 * probably better to halt here than just fail the attach....) 6919 */ 6920 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 6921 panic("sd_unit_attach: NULL soft state on instance:0x%x", 6922 instance); 6923 /*NOTREACHED*/ 6924 } 6925 6926 /* 6927 * Link the back ptr of the driver soft state to the scsi_device 6928 * struct for this lun. 6929 * Save a pointer to the softstate in the driver-private area of 6930 * the scsi_device struct. 6931 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 6932 * we first set un->un_sd below. 6933 */ 6934 un->un_sd = devp; 6935 devp->sd_private = (opaque_t)un; 6936 6937 /* 6938 * The following must be after devp is stored in the soft state struct. 6939 */ 6940 #ifdef SDDEBUG 6941 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6942 "%s_unit_attach: un:0x%p instance:%d\n", 6943 ddi_driver_name(devi), un, instance); 6944 #endif 6945 6946 /* 6947 * Set up the device type and node type (for the minor nodes). 6948 * By default we assume that the device can at least support the 6949 * Common Command Set. Call it a CD-ROM if it reports itself 6950 * as a RODIRECT device. 6951 */ 6952 switch (devp->sd_inq->inq_dtype) { 6953 case DTYPE_RODIRECT: 6954 un->un_node_type = DDI_NT_CD_CHAN; 6955 un->un_ctype = CTYPE_CDROM; 6956 break; 6957 case DTYPE_OPTICAL: 6958 un->un_node_type = DDI_NT_BLOCK_CHAN; 6959 un->un_ctype = CTYPE_ROD; 6960 break; 6961 default: 6962 un->un_node_type = DDI_NT_BLOCK_CHAN; 6963 un->un_ctype = CTYPE_CCS; 6964 break; 6965 } 6966 6967 /* 6968 * Try to read the interconnect type from the HBA. 6969 * 6970 * Note: This driver is currently compiled as two binaries, a parallel 6971 * scsi version (sd) and a fibre channel version (ssd). All functional 6972 * differences are determined at compile time. In the future a single 6973 * binary will be provided and the interconnect type will be used to 6974 * differentiate between fibre and parallel scsi behaviors. At that time 6975 * it will be necessary for all fibre channel HBAs to support this 6976 * property. 6977 * 6978 * set un_f_is_fiber to TRUE ( default fiber ) 6979 */ 6980 un->un_f_is_fibre = TRUE; 6981 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 6982 case INTERCONNECT_SSA: 6983 un->un_interconnect_type = SD_INTERCONNECT_SSA; 6984 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6985 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 6986 break; 6987 case INTERCONNECT_PARALLEL: 6988 un->un_f_is_fibre = FALSE; 6989 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6990 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6991 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 6992 break; 6993 case INTERCONNECT_SATA: 6994 un->un_f_is_fibre = FALSE; 6995 un->un_interconnect_type = SD_INTERCONNECT_SATA; 6996 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6997 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 6998 break; 6999 case INTERCONNECT_FIBRE: 7000 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 7001 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7002 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 7003 break; 7004 case INTERCONNECT_FABRIC: 7005 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 7006 un->un_node_type = DDI_NT_BLOCK_FABRIC; 7007 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7008 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 7009 break; 7010 default: 7011 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 7012 /* 7013 * The HBA does not support the "interconnect-type" property 7014 * (or did not provide a recognized type). 7015 * 7016 * Note: This will be obsoleted when a single fibre channel 7017 * and parallel scsi driver is delivered. In the meantime the 7018 * interconnect type will be set to the platform default.If that 7019 * type is not parallel SCSI, it means that we should be 7020 * assuming "ssd" semantics. However, here this also means that 7021 * the FC HBA is not supporting the "interconnect-type" property 7022 * like we expect it to, so log this occurrence. 7023 */ 7024 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 7025 if (!SD_IS_PARALLEL_SCSI(un)) { 7026 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7027 "sd_unit_attach: un:0x%p Assuming " 7028 "INTERCONNECT_FIBRE\n", un); 7029 } else { 7030 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7031 "sd_unit_attach: un:0x%p Assuming " 7032 "INTERCONNECT_PARALLEL\n", un); 7033 un->un_f_is_fibre = FALSE; 7034 } 7035 #else 7036 /* 7037 * Note: This source will be implemented when a single fibre 7038 * channel and parallel scsi driver is delivered. The default 7039 * will be to assume that if a device does not support the 7040 * "interconnect-type" property it is a parallel SCSI HBA and 7041 * we will set the interconnect type for parallel scsi. 7042 */ 7043 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 7044 un->un_f_is_fibre = FALSE; 7045 #endif 7046 break; 7047 } 7048 7049 if (un->un_f_is_fibre == TRUE) { 7050 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 7051 SCSI_VERSION_3) { 7052 switch (un->un_interconnect_type) { 7053 case SD_INTERCONNECT_FIBRE: 7054 case SD_INTERCONNECT_SSA: 7055 un->un_node_type = DDI_NT_BLOCK_WWN; 7056 break; 7057 default: 7058 break; 7059 } 7060 } 7061 } 7062 7063 /* 7064 * Initialize the Request Sense command for the target 7065 */ 7066 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 7067 goto alloc_rqs_failed; 7068 } 7069 7070 /* 7071 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 7072 * with separate binary for sd and ssd. 7073 * 7074 * x86 has 1 binary, un_retry_count is set base on connection type. 7075 * The hardcoded values will go away when Sparc uses 1 binary 7076 * for sd and ssd. This hardcoded values need to match 7077 * SD_RETRY_COUNT in sddef.h 7078 * The value used is base on interconnect type. 7079 * fibre = 3, parallel = 5 7080 */ 7081 #if defined(__i386) || defined(__amd64) 7082 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 7083 #else 7084 un->un_retry_count = SD_RETRY_COUNT; 7085 #endif 7086 7087 /* 7088 * Set the per disk retry count to the default number of retries 7089 * for disks and CDROMs. This value can be overridden by the 7090 * disk property list or an entry in sd.conf. 7091 */ 7092 un->un_notready_retry_count = 7093 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 7094 : DISK_NOT_READY_RETRY_COUNT(un); 7095 7096 /* 7097 * Set the busy retry count to the default value of un_retry_count. 7098 * This can be overridden by entries in sd.conf or the device 7099 * config table. 7100 */ 7101 un->un_busy_retry_count = un->un_retry_count; 7102 7103 /* 7104 * Init the reset threshold for retries. This number determines 7105 * how many retries must be performed before a reset can be issued 7106 * (for certain error conditions). This can be overridden by entries 7107 * in sd.conf or the device config table. 7108 */ 7109 un->un_reset_retry_count = (un->un_retry_count / 2); 7110 7111 /* 7112 * Set the victim_retry_count to the default un_retry_count 7113 */ 7114 un->un_victim_retry_count = (2 * un->un_retry_count); 7115 7116 /* 7117 * Set the reservation release timeout to the default value of 7118 * 5 seconds. This can be overridden by entries in ssd.conf or the 7119 * device config table. 7120 */ 7121 un->un_reserve_release_time = 5; 7122 7123 /* 7124 * Set up the default maximum transfer size. Note that this may 7125 * get updated later in the attach, when setting up default wide 7126 * operations for disks. 7127 */ 7128 #if defined(__i386) || defined(__amd64) 7129 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 7130 un->un_partial_dma_supported = 1; 7131 #else 7132 un->un_max_xfer_size = (uint_t)maxphys; 7133 #endif 7134 7135 /* 7136 * Get "allow bus device reset" property (defaults to "enabled" if 7137 * the property was not defined). This is to disable bus resets for 7138 * certain kinds of error recovery. Note: In the future when a run-time 7139 * fibre check is available the soft state flag should default to 7140 * enabled. 7141 */ 7142 if (un->un_f_is_fibre == TRUE) { 7143 un->un_f_allow_bus_device_reset = TRUE; 7144 } else { 7145 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7146 "allow-bus-device-reset", 1) != 0) { 7147 un->un_f_allow_bus_device_reset = TRUE; 7148 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7149 "sd_unit_attach: un:0x%p Bus device reset " 7150 "enabled\n", un); 7151 } else { 7152 un->un_f_allow_bus_device_reset = FALSE; 7153 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7154 "sd_unit_attach: un:0x%p Bus device reset " 7155 "disabled\n", un); 7156 } 7157 } 7158 7159 /* 7160 * Check if this is an ATAPI device. ATAPI devices use Group 1 7161 * Read/Write commands and Group 2 Mode Sense/Select commands. 7162 * 7163 * Note: The "obsolete" way of doing this is to check for the "atapi" 7164 * property. The new "variant" property with a value of "atapi" has been 7165 * introduced so that future 'variants' of standard SCSI behavior (like 7166 * atapi) could be specified by the underlying HBA drivers by supplying 7167 * a new value for the "variant" property, instead of having to define a 7168 * new property. 7169 */ 7170 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 7171 un->un_f_cfg_is_atapi = TRUE; 7172 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7173 "sd_unit_attach: un:0x%p Atapi device\n", un); 7174 } 7175 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 7176 &variantp) == DDI_PROP_SUCCESS) { 7177 if (strcmp(variantp, "atapi") == 0) { 7178 un->un_f_cfg_is_atapi = TRUE; 7179 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7180 "sd_unit_attach: un:0x%p Atapi device\n", un); 7181 } 7182 ddi_prop_free(variantp); 7183 } 7184 7185 un->un_cmd_timeout = SD_IO_TIME; 7186 7187 un->un_busy_timeout = SD_BSY_TIMEOUT; 7188 7189 /* Info on current states, statuses, etc. (Updated frequently) */ 7190 un->un_state = SD_STATE_NORMAL; 7191 un->un_last_state = SD_STATE_NORMAL; 7192 7193 /* Control & status info for command throttling */ 7194 un->un_throttle = sd_max_throttle; 7195 un->un_saved_throttle = sd_max_throttle; 7196 un->un_min_throttle = sd_min_throttle; 7197 7198 if (un->un_f_is_fibre == TRUE) { 7199 un->un_f_use_adaptive_throttle = TRUE; 7200 } else { 7201 un->un_f_use_adaptive_throttle = FALSE; 7202 } 7203 7204 /* Removable media support. */ 7205 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 7206 un->un_mediastate = DKIO_NONE; 7207 un->un_specified_mediastate = DKIO_NONE; 7208 7209 /* CVs for suspend/resume (PM or DR) */ 7210 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 7211 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 7212 7213 /* Power management support. */ 7214 un->un_power_level = SD_SPINDLE_UNINIT; 7215 7216 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 7217 un->un_f_wcc_inprog = 0; 7218 7219 /* 7220 * The open/close semaphore is used to serialize threads executing 7221 * in the driver's open & close entry point routines for a given 7222 * instance. 7223 */ 7224 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 7225 7226 /* 7227 * The conf file entry and softstate variable is a forceful override, 7228 * meaning a non-zero value must be entered to change the default. 7229 */ 7230 un->un_f_disksort_disabled = FALSE; 7231 7232 /* 7233 * Retrieve the properties from the static driver table or the driver 7234 * configuration file (.conf) for this unit and update the soft state 7235 * for the device as needed for the indicated properties. 7236 * Note: the property configuration needs to occur here as some of the 7237 * following routines may have dependencies on soft state flags set 7238 * as part of the driver property configuration. 7239 */ 7240 sd_read_unit_properties(un); 7241 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7242 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 7243 7244 /* 7245 * Only if a device has "hotpluggable" property, it is 7246 * treated as hotpluggable device. Otherwise, it is 7247 * regarded as non-hotpluggable one. 7248 */ 7249 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 7250 -1) != -1) { 7251 un->un_f_is_hotpluggable = TRUE; 7252 } 7253 7254 /* 7255 * set unit's attributes(flags) according to "hotpluggable" and 7256 * RMB bit in INQUIRY data. 7257 */ 7258 sd_set_unit_attributes(un, devi); 7259 7260 /* 7261 * By default, we mark the capacity, lbasize, and geometry 7262 * as invalid. Only if we successfully read a valid capacity 7263 * will we update the un_blockcount and un_tgt_blocksize with the 7264 * valid values (the geometry will be validated later). 7265 */ 7266 un->un_f_blockcount_is_valid = FALSE; 7267 un->un_f_tgt_blocksize_is_valid = FALSE; 7268 7269 /* 7270 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 7271 * otherwise. 7272 */ 7273 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 7274 un->un_blockcount = 0; 7275 7276 /* 7277 * Set up the per-instance info needed to determine the correct 7278 * CDBs and other info for issuing commands to the target. 7279 */ 7280 sd_init_cdb_limits(un); 7281 7282 /* 7283 * Set up the IO chains to use, based upon the target type. 7284 */ 7285 if (un->un_f_non_devbsize_supported) { 7286 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7287 } else { 7288 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7289 } 7290 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7291 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 7292 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 7293 7294 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 7295 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 7296 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 7297 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 7298 7299 7300 if (ISCD(un)) { 7301 un->un_additional_codes = sd_additional_codes; 7302 } else { 7303 un->un_additional_codes = NULL; 7304 } 7305 7306 /* 7307 * Create the kstats here so they can be available for attach-time 7308 * routines that send commands to the unit (either polled or via 7309 * sd_send_scsi_cmd). 7310 * 7311 * Note: This is a critical sequence that needs to be maintained: 7312 * 1) Instantiate the kstats here, before any routines using the 7313 * iopath (i.e. sd_send_scsi_cmd). 7314 * 2) Instantiate and initialize the partition stats 7315 * (sd_set_pstats). 7316 * 3) Initialize the error stats (sd_set_errstats), following 7317 * sd_validate_geometry(),sd_register_devid(), 7318 * and sd_cache_control(). 7319 */ 7320 7321 un->un_stats = kstat_create(sd_label, instance, 7322 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 7323 if (un->un_stats != NULL) { 7324 un->un_stats->ks_lock = SD_MUTEX(un); 7325 kstat_install(un->un_stats); 7326 } 7327 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7328 "sd_unit_attach: un:0x%p un_stats created\n", un); 7329 7330 sd_create_errstats(un, instance); 7331 if (un->un_errstats == NULL) { 7332 goto create_errstats_failed; 7333 } 7334 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7335 "sd_unit_attach: un:0x%p errstats created\n", un); 7336 7337 /* 7338 * The following if/else code was relocated here from below as part 7339 * of the fix for bug (4430280). However with the default setup added 7340 * on entry to this routine, it's no longer absolutely necessary for 7341 * this to be before the call to sd_spin_up_unit. 7342 */ 7343 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 7344 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) || 7345 (devp->sd_inq->inq_ansi == 5)) && 7346 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque; 7347 7348 /* 7349 * If tagged queueing is supported by the target 7350 * and by the host adapter then we will enable it 7351 */ 7352 un->un_tagflags = 0; 7353 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag && 7354 (un->un_f_arq_enabled == TRUE)) { 7355 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 7356 1, 1) == 1) { 7357 un->un_tagflags = FLAG_STAG; 7358 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7359 "sd_unit_attach: un:0x%p tag queueing " 7360 "enabled\n", un); 7361 } else if (scsi_ifgetcap(SD_ADDRESS(un), 7362 "untagged-qing", 0) == 1) { 7363 un->un_f_opt_queueing = TRUE; 7364 un->un_saved_throttle = un->un_throttle = 7365 min(un->un_throttle, 3); 7366 } else { 7367 un->un_f_opt_queueing = FALSE; 7368 un->un_saved_throttle = un->un_throttle = 1; 7369 } 7370 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 7371 == 1) && (un->un_f_arq_enabled == TRUE)) { 7372 /* The Host Adapter supports internal queueing. */ 7373 un->un_f_opt_queueing = TRUE; 7374 un->un_saved_throttle = un->un_throttle = 7375 min(un->un_throttle, 3); 7376 } else { 7377 un->un_f_opt_queueing = FALSE; 7378 un->un_saved_throttle = un->un_throttle = 1; 7379 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7380 "sd_unit_attach: un:0x%p no tag queueing\n", un); 7381 } 7382 7383 /* 7384 * Enable large transfers for SATA/SAS drives 7385 */ 7386 if (SD_IS_SERIAL(un)) { 7387 un->un_max_xfer_size = 7388 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7389 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7390 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7391 "sd_unit_attach: un:0x%p max transfer " 7392 "size=0x%x\n", un, un->un_max_xfer_size); 7393 7394 } 7395 7396 /* Setup or tear down default wide operations for disks */ 7397 7398 /* 7399 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 7400 * and "ssd_max_xfer_size" to exist simultaneously on the same 7401 * system and be set to different values. In the future this 7402 * code may need to be updated when the ssd module is 7403 * obsoleted and removed from the system. (4299588) 7404 */ 7405 if (SD_IS_PARALLEL_SCSI(un) && 7406 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 7407 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 7408 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7409 1, 1) == 1) { 7410 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7411 "sd_unit_attach: un:0x%p Wide Transfer " 7412 "enabled\n", un); 7413 } 7414 7415 /* 7416 * If tagged queuing has also been enabled, then 7417 * enable large xfers 7418 */ 7419 if (un->un_saved_throttle == sd_max_throttle) { 7420 un->un_max_xfer_size = 7421 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7422 sd_max_xfer_size, SD_MAX_XFER_SIZE); 7423 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7424 "sd_unit_attach: un:0x%p max transfer " 7425 "size=0x%x\n", un, un->un_max_xfer_size); 7426 } 7427 } else { 7428 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 7429 0, 1) == 1) { 7430 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7431 "sd_unit_attach: un:0x%p " 7432 "Wide Transfer disabled\n", un); 7433 } 7434 } 7435 } else { 7436 un->un_tagflags = FLAG_STAG; 7437 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 7438 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 7439 } 7440 7441 /* 7442 * If this target supports LUN reset, try to enable it. 7443 */ 7444 if (un->un_f_lun_reset_enabled) { 7445 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 7446 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7447 "un:0x%p lun_reset capability set\n", un); 7448 } else { 7449 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 7450 "un:0x%p lun-reset capability not set\n", un); 7451 } 7452 } 7453 7454 /* 7455 * Adjust the maximum transfer size. This is to fix 7456 * the problem of partial DMA support on SPARC. Some 7457 * HBA driver, like aac, has very small dma_attr_maxxfer 7458 * size, which requires partial DMA support on SPARC. 7459 * In the future the SPARC pci nexus driver may solve 7460 * the problem instead of this fix. 7461 */ 7462 #if defined(__sparc) 7463 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 7464 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 7465 un->un_max_xfer_size = max_xfer_size; 7466 un->un_partial_dma_supported = 1; 7467 } 7468 #endif 7469 7470 /* 7471 * Set PKT_DMA_PARTIAL flag. 7472 */ 7473 if (un->un_partial_dma_supported == 1) { 7474 un->un_pkt_flags = PKT_DMA_PARTIAL; 7475 } else { 7476 un->un_pkt_flags = 0; 7477 } 7478 7479 /* Initialize sd_ssc_t for internal uscsi commands */ 7480 ssc = sd_ssc_init(un); 7481 scsi_fm_init(devp); 7482 7483 /* 7484 * Allocate memory for SCSI FMA stuffs. 7485 */ 7486 un->un_fm_private = 7487 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP); 7488 sfip = (struct sd_fm_internal *)un->un_fm_private; 7489 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd; 7490 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo; 7491 sfip->fm_ssc.ssc_un = un; 7492 7493 if (ISCD(un) || 7494 un->un_f_has_removable_media || 7495 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) { 7496 /* 7497 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device. 7498 * Their log are unchanged. 7499 */ 7500 sfip->fm_log_level = SD_FM_LOG_NSUP; 7501 } else { 7502 /* 7503 * If enter here, it should be non-CDROM and FM-capable 7504 * device, and it will not keep the old scsi_log as before 7505 * in /var/adm/messages. However, the property 7506 * "fm-scsi-log" will control whether the FM telemetry will 7507 * be logged in /var/adm/messages. 7508 */ 7509 int fm_scsi_log; 7510 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 7511 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0); 7512 7513 if (fm_scsi_log) 7514 sfip->fm_log_level = SD_FM_LOG_EREPORT; 7515 else 7516 sfip->fm_log_level = SD_FM_LOG_SILENT; 7517 } 7518 7519 /* 7520 * At this point in the attach, we have enough info in the 7521 * soft state to be able to issue commands to the target. 7522 * 7523 * All command paths used below MUST issue their commands as 7524 * SD_PATH_DIRECT. This is important as intermediate layers 7525 * are not all initialized yet (such as PM). 7526 */ 7527 7528 /* 7529 * Send a TEST UNIT READY command to the device. This should clear 7530 * any outstanding UNIT ATTENTION that may be present. 7531 * 7532 * Note: Don't check for success, just track if there is a reservation, 7533 * this is a throw away command to clear any unit attentions. 7534 * 7535 * Note: This MUST be the first command issued to the target during 7536 * attach to ensure power on UNIT ATTENTIONS are cleared. 7537 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7538 * with attempts at spinning up a device with no media. 7539 */ 7540 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 7541 if (status != 0) { 7542 if (status == EACCES) 7543 reservation_flag = SD_TARGET_IS_RESERVED; 7544 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7545 } 7546 7547 /* 7548 * If the device is NOT a removable media device, attempt to spin 7549 * it up (using the START_STOP_UNIT command) and read its capacity 7550 * (using the READ CAPACITY command). Note, however, that either 7551 * of these could fail and in some cases we would continue with 7552 * the attach despite the failure (see below). 7553 */ 7554 if (un->un_f_descr_format_supported) { 7555 7556 switch (sd_spin_up_unit(ssc)) { 7557 case 0: 7558 /* 7559 * Spin-up was successful; now try to read the 7560 * capacity. If successful then save the results 7561 * and mark the capacity & lbasize as valid. 7562 */ 7563 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7564 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7565 7566 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 7567 &lbasize, SD_PATH_DIRECT); 7568 7569 switch (status) { 7570 case 0: { 7571 if (capacity > DK_MAX_BLOCKS) { 7572 #ifdef _LP64 7573 if ((capacity + 1) > 7574 SD_GROUP1_MAX_ADDRESS) { 7575 /* 7576 * Enable descriptor format 7577 * sense data so that we can 7578 * get 64 bit sense data 7579 * fields. 7580 */ 7581 sd_enable_descr_sense(ssc); 7582 } 7583 #else 7584 /* 32-bit kernels can't handle this */ 7585 scsi_log(SD_DEVINFO(un), 7586 sd_label, CE_WARN, 7587 "disk has %llu blocks, which " 7588 "is too large for a 32-bit " 7589 "kernel", capacity); 7590 7591 #if defined(__i386) || defined(__amd64) 7592 /* 7593 * 1TB disk was treated as (1T - 512)B 7594 * in the past, so that it might have 7595 * valid VTOC and solaris partitions, 7596 * we have to allow it to continue to 7597 * work. 7598 */ 7599 if (capacity -1 > DK_MAX_BLOCKS) 7600 #endif 7601 goto spinup_failed; 7602 #endif 7603 } 7604 7605 /* 7606 * Here it's not necessary to check the case: 7607 * the capacity of the device is bigger than 7608 * what the max hba cdb can support. Because 7609 * sd_send_scsi_READ_CAPACITY will retrieve 7610 * the capacity by sending USCSI command, which 7611 * is constrained by the max hba cdb. Actually, 7612 * sd_send_scsi_READ_CAPACITY will return 7613 * EINVAL when using bigger cdb than required 7614 * cdb length. Will handle this case in 7615 * "case EINVAL". 7616 */ 7617 7618 /* 7619 * The following relies on 7620 * sd_send_scsi_READ_CAPACITY never 7621 * returning 0 for capacity and/or lbasize. 7622 */ 7623 sd_update_block_info(un, lbasize, capacity); 7624 7625 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7626 "sd_unit_attach: un:0x%p capacity = %ld " 7627 "blocks; lbasize= %ld.\n", un, 7628 un->un_blockcount, un->un_tgt_blocksize); 7629 7630 break; 7631 } 7632 case EINVAL: 7633 /* 7634 * In the case where the max-cdb-length property 7635 * is smaller than the required CDB length for 7636 * a SCSI device, a target driver can fail to 7637 * attach to that device. 7638 */ 7639 scsi_log(SD_DEVINFO(un), 7640 sd_label, CE_WARN, 7641 "disk capacity is too large " 7642 "for current cdb length"); 7643 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7644 7645 goto spinup_failed; 7646 case EACCES: 7647 /* 7648 * Should never get here if the spin-up 7649 * succeeded, but code it in anyway. 7650 * From here, just continue with the attach... 7651 */ 7652 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7653 "sd_unit_attach: un:0x%p " 7654 "sd_send_scsi_READ_CAPACITY " 7655 "returned reservation conflict\n", un); 7656 reservation_flag = SD_TARGET_IS_RESERVED; 7657 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7658 break; 7659 default: 7660 /* 7661 * Likewise, should never get here if the 7662 * spin-up succeeded. Just continue with 7663 * the attach... 7664 */ 7665 if (status == EIO) 7666 sd_ssc_assessment(ssc, 7667 SD_FMT_STATUS_CHECK); 7668 else 7669 sd_ssc_assessment(ssc, 7670 SD_FMT_IGNORE); 7671 break; 7672 } 7673 break; 7674 case EACCES: 7675 /* 7676 * Device is reserved by another host. In this case 7677 * we could not spin it up or read the capacity, but 7678 * we continue with the attach anyway. 7679 */ 7680 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7681 "sd_unit_attach: un:0x%p spin-up reservation " 7682 "conflict.\n", un); 7683 reservation_flag = SD_TARGET_IS_RESERVED; 7684 break; 7685 default: 7686 /* Fail the attach if the spin-up failed. */ 7687 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7688 "sd_unit_attach: un:0x%p spin-up failed.", un); 7689 goto spinup_failed; 7690 } 7691 7692 } 7693 7694 /* 7695 * Check to see if this is a MMC drive 7696 */ 7697 if (ISCD(un)) { 7698 sd_set_mmc_caps(ssc); 7699 } 7700 7701 7702 /* 7703 * Add a zero-length attribute to tell the world we support 7704 * kernel ioctls (for layered drivers) 7705 */ 7706 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7707 DDI_KERNEL_IOCTL, NULL, 0); 7708 7709 /* 7710 * Add a boolean property to tell the world we support 7711 * the B_FAILFAST flag (for layered drivers) 7712 */ 7713 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7714 "ddi-failfast-supported", NULL, 0); 7715 7716 /* 7717 * Initialize power management 7718 */ 7719 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 7720 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 7721 sd_setup_pm(ssc, devi); 7722 if (un->un_f_pm_is_enabled == FALSE) { 7723 /* 7724 * For performance, point to a jump table that does 7725 * not include pm. 7726 * The direct and priority chains don't change with PM. 7727 * 7728 * Note: this is currently done based on individual device 7729 * capabilities. When an interface for determining system 7730 * power enabled state becomes available, or when additional 7731 * layers are added to the command chain, these values will 7732 * have to be re-evaluated for correctness. 7733 */ 7734 if (un->un_f_non_devbsize_supported) { 7735 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 7736 } else { 7737 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 7738 } 7739 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 7740 } 7741 7742 /* 7743 * This property is set to 0 by HA software to avoid retries 7744 * on a reserved disk. (The preferred property name is 7745 * "retry-on-reservation-conflict") (1189689) 7746 * 7747 * Note: The use of a global here can have unintended consequences. A 7748 * per instance variable is preferable to match the capabilities of 7749 * different underlying hba's (4402600) 7750 */ 7751 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 7752 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 7753 sd_retry_on_reservation_conflict); 7754 if (sd_retry_on_reservation_conflict != 0) { 7755 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 7756 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 7757 sd_retry_on_reservation_conflict); 7758 } 7759 7760 /* Set up options for QFULL handling. */ 7761 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7762 "qfull-retries", -1)) != -1) { 7763 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 7764 rval, 1); 7765 } 7766 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7767 "qfull-retry-interval", -1)) != -1) { 7768 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 7769 rval, 1); 7770 } 7771 7772 /* 7773 * This just prints a message that announces the existence of the 7774 * device. The message is always printed in the system logfile, but 7775 * only appears on the console if the system is booted with the 7776 * -v (verbose) argument. 7777 */ 7778 ddi_report_dev(devi); 7779 7780 un->un_mediastate = DKIO_NONE; 7781 7782 cmlb_alloc_handle(&un->un_cmlbhandle); 7783 7784 #if defined(__i386) || defined(__amd64) 7785 /* 7786 * On x86, compensate for off-by-1 legacy error 7787 */ 7788 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 7789 (lbasize == un->un_sys_blocksize)) 7790 offbyone = CMLB_OFF_BY_ONE; 7791 #endif 7792 7793 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 7794 un->un_f_has_removable_media, un->un_f_is_hotpluggable, 7795 un->un_node_type, offbyone, un->un_cmlbhandle, 7796 (void *)SD_PATH_DIRECT) != 0) { 7797 goto cmlb_attach_failed; 7798 } 7799 7800 7801 /* 7802 * Read and validate the device's geometry (ie, disk label) 7803 * A new unformatted drive will not have a valid geometry, but 7804 * the driver needs to successfully attach to this device so 7805 * the drive can be formatted via ioctls. 7806 */ 7807 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 7808 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 7809 7810 mutex_enter(SD_MUTEX(un)); 7811 7812 /* 7813 * Read and initialize the devid for the unit. 7814 */ 7815 if (un->un_f_devid_supported) { 7816 sd_register_devid(ssc, devi, reservation_flag); 7817 } 7818 mutex_exit(SD_MUTEX(un)); 7819 7820 #if (defined(__fibre)) 7821 /* 7822 * Register callbacks for fibre only. You can't do this solely 7823 * on the basis of the devid_type because this is hba specific. 7824 * We need to query our hba capabilities to find out whether to 7825 * register or not. 7826 */ 7827 if (un->un_f_is_fibre) { 7828 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7829 sd_init_event_callbacks(un); 7830 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7831 "sd_unit_attach: un:0x%p event callbacks inserted", 7832 un); 7833 } 7834 } 7835 #endif 7836 7837 if (un->un_f_opt_disable_cache == TRUE) { 7838 /* 7839 * Disable both read cache and write cache. This is 7840 * the historic behavior of the keywords in the config file. 7841 */ 7842 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 7843 0) { 7844 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7845 "sd_unit_attach: un:0x%p Could not disable " 7846 "caching", un); 7847 goto devid_failed; 7848 } 7849 } 7850 7851 /* 7852 * Check the value of the WCE bit now and 7853 * set un_f_write_cache_enabled accordingly. 7854 */ 7855 (void) sd_get_write_cache_enabled(ssc, &wc_enabled); 7856 mutex_enter(SD_MUTEX(un)); 7857 un->un_f_write_cache_enabled = (wc_enabled != 0); 7858 mutex_exit(SD_MUTEX(un)); 7859 7860 /* 7861 * Check the value of the NV_SUP bit and set 7862 * un_f_suppress_cache_flush accordingly. 7863 */ 7864 sd_get_nv_sup(ssc); 7865 7866 /* 7867 * Find out what type of reservation this disk supports. 7868 */ 7869 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL); 7870 7871 switch (status) { 7872 case 0: 7873 /* 7874 * SCSI-3 reservations are supported. 7875 */ 7876 un->un_reservation_type = SD_SCSI3_RESERVATION; 7877 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7878 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 7879 break; 7880 case ENOTSUP: 7881 /* 7882 * The PERSISTENT RESERVE IN command would not be recognized by 7883 * a SCSI-2 device, so assume the reservation type is SCSI-2. 7884 */ 7885 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7886 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 7887 un->un_reservation_type = SD_SCSI2_RESERVATION; 7888 7889 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7890 break; 7891 default: 7892 /* 7893 * default to SCSI-3 reservations 7894 */ 7895 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7896 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 7897 un->un_reservation_type = SD_SCSI3_RESERVATION; 7898 7899 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 7900 break; 7901 } 7902 7903 /* 7904 * Set the pstat and error stat values here, so data obtained during the 7905 * previous attach-time routines is available. 7906 * 7907 * Note: This is a critical sequence that needs to be maintained: 7908 * 1) Instantiate the kstats before any routines using the iopath 7909 * (i.e. sd_send_scsi_cmd). 7910 * 2) Initialize the error stats (sd_set_errstats) and partition 7911 * stats (sd_set_pstats)here, following 7912 * cmlb_validate_geometry(), sd_register_devid(), and 7913 * sd_cache_control(). 7914 */ 7915 7916 if (un->un_f_pkstats_enabled && geom_label_valid) { 7917 sd_set_pstats(un); 7918 SD_TRACE(SD_LOG_IO_PARTITION, un, 7919 "sd_unit_attach: un:0x%p pstats created and set\n", un); 7920 } 7921 7922 sd_set_errstats(un); 7923 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7924 "sd_unit_attach: un:0x%p errstats set\n", un); 7925 7926 7927 /* 7928 * After successfully attaching an instance, we record the information 7929 * of how many luns have been attached on the relative target and 7930 * controller for parallel SCSI. This information is used when sd tries 7931 * to set the tagged queuing capability in HBA. 7932 */ 7933 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 7934 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 7935 } 7936 7937 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7938 "sd_unit_attach: un:0x%p exit success\n", un); 7939 7940 /* Uninitialize sd_ssc_t pointer */ 7941 sd_ssc_fini(ssc); 7942 7943 return (DDI_SUCCESS); 7944 7945 /* 7946 * An error occurred during the attach; clean up & return failure. 7947 */ 7948 7949 devid_failed: 7950 7951 setup_pm_failed: 7952 ddi_remove_minor_node(devi, NULL); 7953 7954 cmlb_attach_failed: 7955 /* 7956 * Cleanup from the scsi_ifsetcap() calls (437868) 7957 */ 7958 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7959 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7960 7961 /* 7962 * Refer to the comments of setting tagged-qing in the beginning of 7963 * sd_unit_attach. We can only disable tagged queuing when there is 7964 * no lun attached on the target. 7965 */ 7966 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7967 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7968 } 7969 7970 if (un->un_f_is_fibre == FALSE) { 7971 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7972 } 7973 7974 spinup_failed: 7975 7976 /* Uninitialize sd_ssc_t pointer */ 7977 sd_ssc_fini(ssc); 7978 7979 mutex_enter(SD_MUTEX(un)); 7980 7981 /* Deallocate SCSI FMA memory spaces */ 7982 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 7983 7984 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 7985 if (un->un_direct_priority_timeid != NULL) { 7986 timeout_id_t temp_id = un->un_direct_priority_timeid; 7987 un->un_direct_priority_timeid = NULL; 7988 mutex_exit(SD_MUTEX(un)); 7989 (void) untimeout(temp_id); 7990 mutex_enter(SD_MUTEX(un)); 7991 } 7992 7993 /* Cancel any pending start/stop timeouts */ 7994 if (un->un_startstop_timeid != NULL) { 7995 timeout_id_t temp_id = un->un_startstop_timeid; 7996 un->un_startstop_timeid = NULL; 7997 mutex_exit(SD_MUTEX(un)); 7998 (void) untimeout(temp_id); 7999 mutex_enter(SD_MUTEX(un)); 8000 } 8001 8002 /* Cancel any pending reset-throttle timeouts */ 8003 if (un->un_reset_throttle_timeid != NULL) { 8004 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8005 un->un_reset_throttle_timeid = NULL; 8006 mutex_exit(SD_MUTEX(un)); 8007 (void) untimeout(temp_id); 8008 mutex_enter(SD_MUTEX(un)); 8009 } 8010 8011 /* Cancel any pending retry timeouts */ 8012 if (un->un_retry_timeid != NULL) { 8013 timeout_id_t temp_id = un->un_retry_timeid; 8014 un->un_retry_timeid = NULL; 8015 mutex_exit(SD_MUTEX(un)); 8016 (void) untimeout(temp_id); 8017 mutex_enter(SD_MUTEX(un)); 8018 } 8019 8020 /* Cancel any pending delayed cv broadcast timeouts */ 8021 if (un->un_dcvb_timeid != NULL) { 8022 timeout_id_t temp_id = un->un_dcvb_timeid; 8023 un->un_dcvb_timeid = NULL; 8024 mutex_exit(SD_MUTEX(un)); 8025 (void) untimeout(temp_id); 8026 mutex_enter(SD_MUTEX(un)); 8027 } 8028 8029 mutex_exit(SD_MUTEX(un)); 8030 8031 /* There should not be any in-progress I/O so ASSERT this check */ 8032 ASSERT(un->un_ncmds_in_transport == 0); 8033 ASSERT(un->un_ncmds_in_driver == 0); 8034 8035 /* Do not free the softstate if the callback routine is active */ 8036 sd_sync_with_callback(un); 8037 8038 /* 8039 * Partition stats apparently are not used with removables. These would 8040 * not have been created during attach, so no need to clean them up... 8041 */ 8042 if (un->un_errstats != NULL) { 8043 kstat_delete(un->un_errstats); 8044 un->un_errstats = NULL; 8045 } 8046 8047 create_errstats_failed: 8048 8049 if (un->un_stats != NULL) { 8050 kstat_delete(un->un_stats); 8051 un->un_stats = NULL; 8052 } 8053 8054 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8055 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8056 8057 ddi_prop_remove_all(devi); 8058 sema_destroy(&un->un_semoclose); 8059 cv_destroy(&un->un_state_cv); 8060 8061 getrbuf_failed: 8062 8063 sd_free_rqs(un); 8064 8065 alloc_rqs_failed: 8066 8067 devp->sd_private = NULL; 8068 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 8069 8070 get_softstate_failed: 8071 /* 8072 * Note: the man pages are unclear as to whether or not doing a 8073 * ddi_soft_state_free(sd_state, instance) is the right way to 8074 * clean up after the ddi_soft_state_zalloc() if the subsequent 8075 * ddi_get_soft_state() fails. The implication seems to be 8076 * that the get_soft_state cannot fail if the zalloc succeeds. 8077 */ 8078 ddi_soft_state_free(sd_state, instance); 8079 8080 probe_failed: 8081 scsi_unprobe(devp); 8082 8083 return (DDI_FAILURE); 8084 } 8085 8086 8087 /* 8088 * Function: sd_unit_detach 8089 * 8090 * Description: Performs DDI_DETACH processing for sddetach(). 8091 * 8092 * Return Code: DDI_SUCCESS 8093 * DDI_FAILURE 8094 * 8095 * Context: Kernel thread context 8096 */ 8097 8098 static int 8099 sd_unit_detach(dev_info_t *devi) 8100 { 8101 struct scsi_device *devp; 8102 struct sd_lun *un; 8103 int i; 8104 int tgt; 8105 dev_t dev; 8106 dev_info_t *pdip = ddi_get_parent(devi); 8107 int instance = ddi_get_instance(devi); 8108 8109 mutex_enter(&sd_detach_mutex); 8110 8111 /* 8112 * Fail the detach for any of the following: 8113 * - Unable to get the sd_lun struct for the instance 8114 * - A layered driver has an outstanding open on the instance 8115 * - Another thread is already detaching this instance 8116 * - Another thread is currently performing an open 8117 */ 8118 devp = ddi_get_driver_private(devi); 8119 if ((devp == NULL) || 8120 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 8121 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 8122 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 8123 mutex_exit(&sd_detach_mutex); 8124 return (DDI_FAILURE); 8125 } 8126 8127 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 8128 8129 /* 8130 * Mark this instance as currently in a detach, to inhibit any 8131 * opens from a layered driver. 8132 */ 8133 un->un_detach_count++; 8134 mutex_exit(&sd_detach_mutex); 8135 8136 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8137 SCSI_ADDR_PROP_TARGET, -1); 8138 8139 dev = sd_make_device(SD_DEVINFO(un)); 8140 8141 #ifndef lint 8142 _NOTE(COMPETING_THREADS_NOW); 8143 #endif 8144 8145 mutex_enter(SD_MUTEX(un)); 8146 8147 /* 8148 * Fail the detach if there are any outstanding layered 8149 * opens on this device. 8150 */ 8151 for (i = 0; i < NDKMAP; i++) { 8152 if (un->un_ocmap.lyropen[i] != 0) { 8153 goto err_notclosed; 8154 } 8155 } 8156 8157 /* 8158 * Verify there are NO outstanding commands issued to this device. 8159 * ie, un_ncmds_in_transport == 0. 8160 * It's possible to have outstanding commands through the physio 8161 * code path, even though everything's closed. 8162 */ 8163 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 8164 (un->un_direct_priority_timeid != NULL) || 8165 (un->un_state == SD_STATE_RWAIT)) { 8166 mutex_exit(SD_MUTEX(un)); 8167 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8168 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 8169 goto err_stillbusy; 8170 } 8171 8172 /* 8173 * If we have the device reserved, release the reservation. 8174 */ 8175 if ((un->un_resvd_status & SD_RESERVE) && 8176 !(un->un_resvd_status & SD_LOST_RESERVE)) { 8177 mutex_exit(SD_MUTEX(un)); 8178 /* 8179 * Note: sd_reserve_release sends a command to the device 8180 * via the sd_ioctlcmd() path, and can sleep. 8181 */ 8182 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 8183 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8184 "sd_dr_detach: Cannot release reservation \n"); 8185 } 8186 } else { 8187 mutex_exit(SD_MUTEX(un)); 8188 } 8189 8190 /* 8191 * Untimeout any reserve recover, throttle reset, restart unit 8192 * and delayed broadcast timeout threads. Protect the timeout pointer 8193 * from getting nulled by their callback functions. 8194 */ 8195 mutex_enter(SD_MUTEX(un)); 8196 if (un->un_resvd_timeid != NULL) { 8197 timeout_id_t temp_id = un->un_resvd_timeid; 8198 un->un_resvd_timeid = NULL; 8199 mutex_exit(SD_MUTEX(un)); 8200 (void) untimeout(temp_id); 8201 mutex_enter(SD_MUTEX(un)); 8202 } 8203 8204 if (un->un_reset_throttle_timeid != NULL) { 8205 timeout_id_t temp_id = un->un_reset_throttle_timeid; 8206 un->un_reset_throttle_timeid = NULL; 8207 mutex_exit(SD_MUTEX(un)); 8208 (void) untimeout(temp_id); 8209 mutex_enter(SD_MUTEX(un)); 8210 } 8211 8212 if (un->un_startstop_timeid != NULL) { 8213 timeout_id_t temp_id = un->un_startstop_timeid; 8214 un->un_startstop_timeid = NULL; 8215 mutex_exit(SD_MUTEX(un)); 8216 (void) untimeout(temp_id); 8217 mutex_enter(SD_MUTEX(un)); 8218 } 8219 8220 if (un->un_dcvb_timeid != NULL) { 8221 timeout_id_t temp_id = un->un_dcvb_timeid; 8222 un->un_dcvb_timeid = NULL; 8223 mutex_exit(SD_MUTEX(un)); 8224 (void) untimeout(temp_id); 8225 } else { 8226 mutex_exit(SD_MUTEX(un)); 8227 } 8228 8229 /* Remove any pending reservation reclaim requests for this device */ 8230 sd_rmv_resv_reclaim_req(dev); 8231 8232 mutex_enter(SD_MUTEX(un)); 8233 8234 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 8235 if (un->un_direct_priority_timeid != NULL) { 8236 timeout_id_t temp_id = un->un_direct_priority_timeid; 8237 un->un_direct_priority_timeid = NULL; 8238 mutex_exit(SD_MUTEX(un)); 8239 (void) untimeout(temp_id); 8240 mutex_enter(SD_MUTEX(un)); 8241 } 8242 8243 /* Cancel any active multi-host disk watch thread requests */ 8244 if (un->un_mhd_token != NULL) { 8245 mutex_exit(SD_MUTEX(un)); 8246 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 8247 if (scsi_watch_request_terminate(un->un_mhd_token, 8248 SCSI_WATCH_TERMINATE_NOWAIT)) { 8249 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8250 "sd_dr_detach: Cannot cancel mhd watch request\n"); 8251 /* 8252 * Note: We are returning here after having removed 8253 * some driver timeouts above. This is consistent with 8254 * the legacy implementation but perhaps the watch 8255 * terminate call should be made with the wait flag set. 8256 */ 8257 goto err_stillbusy; 8258 } 8259 mutex_enter(SD_MUTEX(un)); 8260 un->un_mhd_token = NULL; 8261 } 8262 8263 if (un->un_swr_token != NULL) { 8264 mutex_exit(SD_MUTEX(un)); 8265 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 8266 if (scsi_watch_request_terminate(un->un_swr_token, 8267 SCSI_WATCH_TERMINATE_NOWAIT)) { 8268 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8269 "sd_dr_detach: Cannot cancel swr watch request\n"); 8270 /* 8271 * Note: We are returning here after having removed 8272 * some driver timeouts above. This is consistent with 8273 * the legacy implementation but perhaps the watch 8274 * terminate call should be made with the wait flag set. 8275 */ 8276 goto err_stillbusy; 8277 } 8278 mutex_enter(SD_MUTEX(un)); 8279 un->un_swr_token = NULL; 8280 } 8281 8282 mutex_exit(SD_MUTEX(un)); 8283 8284 /* 8285 * Clear any scsi_reset_notifies. We clear the reset notifies 8286 * if we have not registered one. 8287 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 8288 */ 8289 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 8290 sd_mhd_reset_notify_cb, (caddr_t)un); 8291 8292 /* 8293 * protect the timeout pointers from getting nulled by 8294 * their callback functions during the cancellation process. 8295 * In such a scenario untimeout can be invoked with a null value. 8296 */ 8297 _NOTE(NO_COMPETING_THREADS_NOW); 8298 8299 mutex_enter(&un->un_pm_mutex); 8300 if (un->un_pm_idle_timeid != NULL) { 8301 timeout_id_t temp_id = un->un_pm_idle_timeid; 8302 un->un_pm_idle_timeid = NULL; 8303 mutex_exit(&un->un_pm_mutex); 8304 8305 /* 8306 * Timeout is active; cancel it. 8307 * Note that it'll never be active on a device 8308 * that does not support PM therefore we don't 8309 * have to check before calling pm_idle_component. 8310 */ 8311 (void) untimeout(temp_id); 8312 (void) pm_idle_component(SD_DEVINFO(un), 0); 8313 mutex_enter(&un->un_pm_mutex); 8314 } 8315 8316 /* 8317 * Check whether there is already a timeout scheduled for power 8318 * management. If yes then don't lower the power here, that's. 8319 * the timeout handler's job. 8320 */ 8321 if (un->un_pm_timeid != NULL) { 8322 timeout_id_t temp_id = un->un_pm_timeid; 8323 un->un_pm_timeid = NULL; 8324 mutex_exit(&un->un_pm_mutex); 8325 /* 8326 * Timeout is active; cancel it. 8327 * Note that it'll never be active on a device 8328 * that does not support PM therefore we don't 8329 * have to check before calling pm_idle_component. 8330 */ 8331 (void) untimeout(temp_id); 8332 (void) pm_idle_component(SD_DEVINFO(un), 0); 8333 8334 } else { 8335 mutex_exit(&un->un_pm_mutex); 8336 if ((un->un_f_pm_is_enabled == TRUE) && 8337 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 8338 DDI_SUCCESS)) { 8339 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8340 "sd_dr_detach: Lower power request failed, ignoring.\n"); 8341 /* 8342 * Fix for bug: 4297749, item # 13 8343 * The above test now includes a check to see if PM is 8344 * supported by this device before call 8345 * pm_lower_power(). 8346 * Note, the following is not dead code. The call to 8347 * pm_lower_power above will generate a call back into 8348 * our sdpower routine which might result in a timeout 8349 * handler getting activated. Therefore the following 8350 * code is valid and necessary. 8351 */ 8352 mutex_enter(&un->un_pm_mutex); 8353 if (un->un_pm_timeid != NULL) { 8354 timeout_id_t temp_id = un->un_pm_timeid; 8355 un->un_pm_timeid = NULL; 8356 mutex_exit(&un->un_pm_mutex); 8357 (void) untimeout(temp_id); 8358 (void) pm_idle_component(SD_DEVINFO(un), 0); 8359 } else { 8360 mutex_exit(&un->un_pm_mutex); 8361 } 8362 } 8363 } 8364 8365 /* 8366 * Cleanup from the scsi_ifsetcap() calls (437868) 8367 * Relocated here from above to be after the call to 8368 * pm_lower_power, which was getting errors. 8369 */ 8370 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 8371 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 8372 8373 /* 8374 * Currently, tagged queuing is supported per target based by HBA. 8375 * Setting this per lun instance actually sets the capability of this 8376 * target in HBA, which affects those luns already attached on the 8377 * same target. So during detach, we can only disable this capability 8378 * only when this is the only lun left on this target. By doing 8379 * this, we assume a target has the same tagged queuing capability 8380 * for every lun. The condition can be removed when HBA is changed to 8381 * support per lun based tagged queuing capability. 8382 */ 8383 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 8384 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 8385 } 8386 8387 if (un->un_f_is_fibre == FALSE) { 8388 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 8389 } 8390 8391 /* 8392 * Remove any event callbacks, fibre only 8393 */ 8394 if (un->un_f_is_fibre == TRUE) { 8395 if ((un->un_insert_event != NULL) && 8396 (ddi_remove_event_handler(un->un_insert_cb_id) != 8397 DDI_SUCCESS)) { 8398 /* 8399 * Note: We are returning here after having done 8400 * substantial cleanup above. This is consistent 8401 * with the legacy implementation but this may not 8402 * be the right thing to do. 8403 */ 8404 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8405 "sd_dr_detach: Cannot cancel insert event\n"); 8406 goto err_remove_event; 8407 } 8408 un->un_insert_event = NULL; 8409 8410 if ((un->un_remove_event != NULL) && 8411 (ddi_remove_event_handler(un->un_remove_cb_id) != 8412 DDI_SUCCESS)) { 8413 /* 8414 * Note: We are returning here after having done 8415 * substantial cleanup above. This is consistent 8416 * with the legacy implementation but this may not 8417 * be the right thing to do. 8418 */ 8419 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8420 "sd_dr_detach: Cannot cancel remove event\n"); 8421 goto err_remove_event; 8422 } 8423 un->un_remove_event = NULL; 8424 } 8425 8426 /* Do not free the softstate if the callback routine is active */ 8427 sd_sync_with_callback(un); 8428 8429 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 8430 cmlb_free_handle(&un->un_cmlbhandle); 8431 8432 /* 8433 * Hold the detach mutex here, to make sure that no other threads ever 8434 * can access a (partially) freed soft state structure. 8435 */ 8436 mutex_enter(&sd_detach_mutex); 8437 8438 /* 8439 * Clean up the soft state struct. 8440 * Cleanup is done in reverse order of allocs/inits. 8441 * At this point there should be no competing threads anymore. 8442 */ 8443 8444 scsi_fm_fini(devp); 8445 8446 /* 8447 * Deallocate memory for SCSI FMA. 8448 */ 8449 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal)); 8450 8451 /* Unregister and free device id. */ 8452 ddi_devid_unregister(devi); 8453 if (un->un_devid) { 8454 ddi_devid_free(un->un_devid); 8455 un->un_devid = NULL; 8456 } 8457 8458 /* 8459 * Destroy wmap cache if it exists. 8460 */ 8461 if (un->un_wm_cache != NULL) { 8462 kmem_cache_destroy(un->un_wm_cache); 8463 un->un_wm_cache = NULL; 8464 } 8465 8466 /* 8467 * kstat cleanup is done in detach for all device types (4363169). 8468 * We do not want to fail detach if the device kstats are not deleted 8469 * since there is a confusion about the devo_refcnt for the device. 8470 * We just delete the kstats and let detach complete successfully. 8471 */ 8472 if (un->un_stats != NULL) { 8473 kstat_delete(un->un_stats); 8474 un->un_stats = NULL; 8475 } 8476 if (un->un_errstats != NULL) { 8477 kstat_delete(un->un_errstats); 8478 un->un_errstats = NULL; 8479 } 8480 8481 /* Remove partition stats */ 8482 if (un->un_f_pkstats_enabled) { 8483 for (i = 0; i < NSDMAP; i++) { 8484 if (un->un_pstats[i] != NULL) { 8485 kstat_delete(un->un_pstats[i]); 8486 un->un_pstats[i] = NULL; 8487 } 8488 } 8489 } 8490 8491 /* Remove xbuf registration */ 8492 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 8493 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 8494 8495 /* Remove driver properties */ 8496 ddi_prop_remove_all(devi); 8497 8498 mutex_destroy(&un->un_pm_mutex); 8499 cv_destroy(&un->un_pm_busy_cv); 8500 8501 cv_destroy(&un->un_wcc_cv); 8502 8503 /* Open/close semaphore */ 8504 sema_destroy(&un->un_semoclose); 8505 8506 /* Removable media condvar. */ 8507 cv_destroy(&un->un_state_cv); 8508 8509 /* Suspend/resume condvar. */ 8510 cv_destroy(&un->un_suspend_cv); 8511 cv_destroy(&un->un_disk_busy_cv); 8512 8513 sd_free_rqs(un); 8514 8515 /* Free up soft state */ 8516 devp->sd_private = NULL; 8517 8518 bzero(un, sizeof (struct sd_lun)); 8519 ddi_soft_state_free(sd_state, instance); 8520 8521 mutex_exit(&sd_detach_mutex); 8522 8523 /* This frees up the INQUIRY data associated with the device. */ 8524 scsi_unprobe(devp); 8525 8526 /* 8527 * After successfully detaching an instance, we update the information 8528 * of how many luns have been attached in the relative target and 8529 * controller for parallel SCSI. This information is used when sd tries 8530 * to set the tagged queuing capability in HBA. 8531 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 8532 * check if the device is parallel SCSI. However, we don't need to 8533 * check here because we've already checked during attach. No device 8534 * that is not parallel SCSI is in the chain. 8535 */ 8536 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8537 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 8538 } 8539 8540 return (DDI_SUCCESS); 8541 8542 err_notclosed: 8543 mutex_exit(SD_MUTEX(un)); 8544 8545 err_stillbusy: 8546 _NOTE(NO_COMPETING_THREADS_NOW); 8547 8548 err_remove_event: 8549 mutex_enter(&sd_detach_mutex); 8550 un->un_detach_count--; 8551 mutex_exit(&sd_detach_mutex); 8552 8553 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 8554 return (DDI_FAILURE); 8555 } 8556 8557 8558 /* 8559 * Function: sd_create_errstats 8560 * 8561 * Description: This routine instantiates the device error stats. 8562 * 8563 * Note: During attach the stats are instantiated first so they are 8564 * available for attach-time routines that utilize the driver 8565 * iopath to send commands to the device. The stats are initialized 8566 * separately so data obtained during some attach-time routines is 8567 * available. (4362483) 8568 * 8569 * Arguments: un - driver soft state (unit) structure 8570 * instance - driver instance 8571 * 8572 * Context: Kernel thread context 8573 */ 8574 8575 static void 8576 sd_create_errstats(struct sd_lun *un, int instance) 8577 { 8578 struct sd_errstats *stp; 8579 char kstatmodule_err[KSTAT_STRLEN]; 8580 char kstatname[KSTAT_STRLEN]; 8581 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 8582 8583 ASSERT(un != NULL); 8584 8585 if (un->un_errstats != NULL) { 8586 return; 8587 } 8588 8589 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 8590 "%serr", sd_label); 8591 (void) snprintf(kstatname, sizeof (kstatname), 8592 "%s%d,err", sd_label, instance); 8593 8594 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 8595 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 8596 8597 if (un->un_errstats == NULL) { 8598 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8599 "sd_create_errstats: Failed kstat_create\n"); 8600 return; 8601 } 8602 8603 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8604 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 8605 KSTAT_DATA_UINT32); 8606 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 8607 KSTAT_DATA_UINT32); 8608 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 8609 KSTAT_DATA_UINT32); 8610 kstat_named_init(&stp->sd_vid, "Vendor", 8611 KSTAT_DATA_CHAR); 8612 kstat_named_init(&stp->sd_pid, "Product", 8613 KSTAT_DATA_CHAR); 8614 kstat_named_init(&stp->sd_revision, "Revision", 8615 KSTAT_DATA_CHAR); 8616 kstat_named_init(&stp->sd_serial, "Serial No", 8617 KSTAT_DATA_CHAR); 8618 kstat_named_init(&stp->sd_capacity, "Size", 8619 KSTAT_DATA_ULONGLONG); 8620 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 8621 KSTAT_DATA_UINT32); 8622 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 8623 KSTAT_DATA_UINT32); 8624 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 8625 KSTAT_DATA_UINT32); 8626 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 8627 KSTAT_DATA_UINT32); 8628 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 8629 KSTAT_DATA_UINT32); 8630 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 8631 KSTAT_DATA_UINT32); 8632 8633 un->un_errstats->ks_private = un; 8634 un->un_errstats->ks_update = nulldev; 8635 8636 kstat_install(un->un_errstats); 8637 } 8638 8639 8640 /* 8641 * Function: sd_set_errstats 8642 * 8643 * Description: This routine sets the value of the vendor id, product id, 8644 * revision, serial number, and capacity device error stats. 8645 * 8646 * Note: During attach the stats are instantiated first so they are 8647 * available for attach-time routines that utilize the driver 8648 * iopath to send commands to the device. The stats are initialized 8649 * separately so data obtained during some attach-time routines is 8650 * available. (4362483) 8651 * 8652 * Arguments: un - driver soft state (unit) structure 8653 * 8654 * Context: Kernel thread context 8655 */ 8656 8657 static void 8658 sd_set_errstats(struct sd_lun *un) 8659 { 8660 struct sd_errstats *stp; 8661 8662 ASSERT(un != NULL); 8663 ASSERT(un->un_errstats != NULL); 8664 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8665 ASSERT(stp != NULL); 8666 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 8667 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 8668 (void) strncpy(stp->sd_revision.value.c, 8669 un->un_sd->sd_inq->inq_revision, 4); 8670 8671 /* 8672 * All the errstats are persistent across detach/attach, 8673 * so reset all the errstats here in case of the hot 8674 * replacement of disk drives, except for not changed 8675 * Sun qualified drives. 8676 */ 8677 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 8678 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8679 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 8680 stp->sd_softerrs.value.ui32 = 0; 8681 stp->sd_harderrs.value.ui32 = 0; 8682 stp->sd_transerrs.value.ui32 = 0; 8683 stp->sd_rq_media_err.value.ui32 = 0; 8684 stp->sd_rq_ntrdy_err.value.ui32 = 0; 8685 stp->sd_rq_nodev_err.value.ui32 = 0; 8686 stp->sd_rq_recov_err.value.ui32 = 0; 8687 stp->sd_rq_illrq_err.value.ui32 = 0; 8688 stp->sd_rq_pfa_err.value.ui32 = 0; 8689 } 8690 8691 /* 8692 * Set the "Serial No" kstat for Sun qualified drives (indicated by 8693 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 8694 * (4376302)) 8695 */ 8696 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 8697 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8698 sizeof (SD_INQUIRY(un)->inq_serial)); 8699 } 8700 8701 if (un->un_f_blockcount_is_valid != TRUE) { 8702 /* 8703 * Set capacity error stat to 0 for no media. This ensures 8704 * a valid capacity is displayed in response to 'iostat -E' 8705 * when no media is present in the device. 8706 */ 8707 stp->sd_capacity.value.ui64 = 0; 8708 } else { 8709 /* 8710 * Multiply un_blockcount by un->un_sys_blocksize to get 8711 * capacity. 8712 * 8713 * Note: for non-512 blocksize devices "un_blockcount" has been 8714 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 8715 * (un_tgt_blocksize / un->un_sys_blocksize). 8716 */ 8717 stp->sd_capacity.value.ui64 = (uint64_t) 8718 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 8719 } 8720 } 8721 8722 8723 /* 8724 * Function: sd_set_pstats 8725 * 8726 * Description: This routine instantiates and initializes the partition 8727 * stats for each partition with more than zero blocks. 8728 * (4363169) 8729 * 8730 * Arguments: un - driver soft state (unit) structure 8731 * 8732 * Context: Kernel thread context 8733 */ 8734 8735 static void 8736 sd_set_pstats(struct sd_lun *un) 8737 { 8738 char kstatname[KSTAT_STRLEN]; 8739 int instance; 8740 int i; 8741 diskaddr_t nblks = 0; 8742 char *partname = NULL; 8743 8744 ASSERT(un != NULL); 8745 8746 instance = ddi_get_instance(SD_DEVINFO(un)); 8747 8748 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 8749 for (i = 0; i < NSDMAP; i++) { 8750 8751 if (cmlb_partinfo(un->un_cmlbhandle, i, 8752 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 8753 continue; 8754 mutex_enter(SD_MUTEX(un)); 8755 8756 if ((un->un_pstats[i] == NULL) && 8757 (nblks != 0)) { 8758 8759 (void) snprintf(kstatname, sizeof (kstatname), 8760 "%s%d,%s", sd_label, instance, 8761 partname); 8762 8763 un->un_pstats[i] = kstat_create(sd_label, 8764 instance, kstatname, "partition", KSTAT_TYPE_IO, 8765 1, KSTAT_FLAG_PERSISTENT); 8766 if (un->un_pstats[i] != NULL) { 8767 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 8768 kstat_install(un->un_pstats[i]); 8769 } 8770 } 8771 mutex_exit(SD_MUTEX(un)); 8772 } 8773 } 8774 8775 8776 #if (defined(__fibre)) 8777 /* 8778 * Function: sd_init_event_callbacks 8779 * 8780 * Description: This routine initializes the insertion and removal event 8781 * callbacks. (fibre only) 8782 * 8783 * Arguments: un - driver soft state (unit) structure 8784 * 8785 * Context: Kernel thread context 8786 */ 8787 8788 static void 8789 sd_init_event_callbacks(struct sd_lun *un) 8790 { 8791 ASSERT(un != NULL); 8792 8793 if ((un->un_insert_event == NULL) && 8794 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 8795 &un->un_insert_event) == DDI_SUCCESS)) { 8796 /* 8797 * Add the callback for an insertion event 8798 */ 8799 (void) ddi_add_event_handler(SD_DEVINFO(un), 8800 un->un_insert_event, sd_event_callback, (void *)un, 8801 &(un->un_insert_cb_id)); 8802 } 8803 8804 if ((un->un_remove_event == NULL) && 8805 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 8806 &un->un_remove_event) == DDI_SUCCESS)) { 8807 /* 8808 * Add the callback for a removal event 8809 */ 8810 (void) ddi_add_event_handler(SD_DEVINFO(un), 8811 un->un_remove_event, sd_event_callback, (void *)un, 8812 &(un->un_remove_cb_id)); 8813 } 8814 } 8815 8816 8817 /* 8818 * Function: sd_event_callback 8819 * 8820 * Description: This routine handles insert/remove events (photon). The 8821 * state is changed to OFFLINE which can be used to supress 8822 * error msgs. (fibre only) 8823 * 8824 * Arguments: un - driver soft state (unit) structure 8825 * 8826 * Context: Callout thread context 8827 */ 8828 /* ARGSUSED */ 8829 static void 8830 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 8831 void *bus_impldata) 8832 { 8833 struct sd_lun *un = (struct sd_lun *)arg; 8834 8835 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 8836 if (event == un->un_insert_event) { 8837 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 8838 mutex_enter(SD_MUTEX(un)); 8839 if (un->un_state == SD_STATE_OFFLINE) { 8840 if (un->un_last_state != SD_STATE_SUSPENDED) { 8841 un->un_state = un->un_last_state; 8842 } else { 8843 /* 8844 * We have gone through SUSPEND/RESUME while 8845 * we were offline. Restore the last state 8846 */ 8847 un->un_state = un->un_save_state; 8848 } 8849 } 8850 mutex_exit(SD_MUTEX(un)); 8851 8852 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 8853 } else if (event == un->un_remove_event) { 8854 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 8855 mutex_enter(SD_MUTEX(un)); 8856 /* 8857 * We need to handle an event callback that occurs during 8858 * the suspend operation, since we don't prevent it. 8859 */ 8860 if (un->un_state != SD_STATE_OFFLINE) { 8861 if (un->un_state != SD_STATE_SUSPENDED) { 8862 New_state(un, SD_STATE_OFFLINE); 8863 } else { 8864 un->un_last_state = SD_STATE_OFFLINE; 8865 } 8866 } 8867 mutex_exit(SD_MUTEX(un)); 8868 } else { 8869 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 8870 "!Unknown event\n"); 8871 } 8872 8873 } 8874 #endif 8875 8876 /* 8877 * Function: sd_cache_control() 8878 * 8879 * Description: This routine is the driver entry point for setting 8880 * read and write caching by modifying the WCE (write cache 8881 * enable) and RCD (read cache disable) bits of mode 8882 * page 8 (MODEPAGE_CACHING). 8883 * 8884 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 8885 * structure for this target. 8886 * rcd_flag - flag for controlling the read cache 8887 * wce_flag - flag for controlling the write cache 8888 * 8889 * Return Code: EIO 8890 * code returned by sd_send_scsi_MODE_SENSE and 8891 * sd_send_scsi_MODE_SELECT 8892 * 8893 * Context: Kernel Thread 8894 */ 8895 8896 static int 8897 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag) 8898 { 8899 struct mode_caching *mode_caching_page; 8900 uchar_t *header; 8901 size_t buflen; 8902 int hdrlen; 8903 int bd_len; 8904 int rval = 0; 8905 struct mode_header_grp2 *mhp; 8906 struct sd_lun *un; 8907 int status; 8908 8909 ASSERT(ssc != NULL); 8910 un = ssc->ssc_un; 8911 ASSERT(un != NULL); 8912 8913 /* 8914 * Do a test unit ready, otherwise a mode sense may not work if this 8915 * is the first command sent to the device after boot. 8916 */ 8917 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 8918 if (status != 0) 8919 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 8920 8921 if (un->un_f_cfg_is_atapi == TRUE) { 8922 hdrlen = MODE_HEADER_LENGTH_GRP2; 8923 } else { 8924 hdrlen = MODE_HEADER_LENGTH; 8925 } 8926 8927 /* 8928 * Allocate memory for the retrieved mode page and its headers. Set 8929 * a pointer to the page itself. Use mode_cache_scsi3 to insure 8930 * we get all of the mode sense data otherwise, the mode select 8931 * will fail. mode_cache_scsi3 is a superset of mode_caching. 8932 */ 8933 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 8934 sizeof (struct mode_cache_scsi3); 8935 8936 header = kmem_zalloc(buflen, KM_SLEEP); 8937 8938 /* Get the information from the device. */ 8939 if (un->un_f_cfg_is_atapi == TRUE) { 8940 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 8941 MODEPAGE_CACHING, SD_PATH_DIRECT); 8942 } else { 8943 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 8944 MODEPAGE_CACHING, SD_PATH_DIRECT); 8945 } 8946 8947 if (rval != 0) { 8948 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8949 "sd_cache_control: Mode Sense Failed\n"); 8950 goto mode_sense_failed; 8951 } 8952 8953 /* 8954 * Determine size of Block Descriptors in order to locate 8955 * the mode page data. ATAPI devices return 0, SCSI devices 8956 * should return MODE_BLK_DESC_LENGTH. 8957 */ 8958 if (un->un_f_cfg_is_atapi == TRUE) { 8959 mhp = (struct mode_header_grp2 *)header; 8960 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8961 } else { 8962 bd_len = ((struct mode_header *)header)->bdesc_length; 8963 } 8964 8965 if (bd_len > MODE_BLK_DESC_LENGTH) { 8966 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 8967 "sd_cache_control: Mode Sense returned invalid block " 8968 "descriptor length\n"); 8969 rval = EIO; 8970 goto mode_sense_failed; 8971 } 8972 8973 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8974 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8975 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 8976 "sd_cache_control: Mode Sense caching page code mismatch " 8977 "%d\n", mode_caching_page->mode_page.code); 8978 rval = EIO; 8979 goto mode_sense_failed; 8980 } 8981 8982 /* Check the relevant bits on successful mode sense. */ 8983 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 8984 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 8985 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 8986 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 8987 8988 size_t sbuflen; 8989 uchar_t save_pg; 8990 8991 /* 8992 * Construct select buffer length based on the 8993 * length of the sense data returned. 8994 */ 8995 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 8996 sizeof (struct mode_page) + 8997 (int)mode_caching_page->mode_page.length; 8998 8999 /* 9000 * Set the caching bits as requested. 9001 */ 9002 if (rcd_flag == SD_CACHE_ENABLE) 9003 mode_caching_page->rcd = 0; 9004 else if (rcd_flag == SD_CACHE_DISABLE) 9005 mode_caching_page->rcd = 1; 9006 9007 if (wce_flag == SD_CACHE_ENABLE) 9008 mode_caching_page->wce = 1; 9009 else if (wce_flag == SD_CACHE_DISABLE) 9010 mode_caching_page->wce = 0; 9011 9012 /* 9013 * Save the page if the mode sense says the 9014 * drive supports it. 9015 */ 9016 save_pg = mode_caching_page->mode_page.ps ? 9017 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 9018 9019 /* Clear reserved bits before mode select. */ 9020 mode_caching_page->mode_page.ps = 0; 9021 9022 /* 9023 * Clear out mode header for mode select. 9024 * The rest of the retrieved page will be reused. 9025 */ 9026 bzero(header, hdrlen); 9027 9028 if (un->un_f_cfg_is_atapi == TRUE) { 9029 mhp = (struct mode_header_grp2 *)header; 9030 mhp->bdesc_length_hi = bd_len >> 8; 9031 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 9032 } else { 9033 ((struct mode_header *)header)->bdesc_length = bd_len; 9034 } 9035 9036 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9037 9038 /* Issue mode select to change the cache settings */ 9039 if (un->un_f_cfg_is_atapi == TRUE) { 9040 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header, 9041 sbuflen, save_pg, SD_PATH_DIRECT); 9042 } else { 9043 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header, 9044 sbuflen, save_pg, SD_PATH_DIRECT); 9045 } 9046 9047 } 9048 9049 9050 mode_sense_failed: 9051 9052 kmem_free(header, buflen); 9053 9054 if (rval != 0) { 9055 if (rval == EIO) 9056 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9057 else 9058 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9059 } 9060 return (rval); 9061 } 9062 9063 9064 /* 9065 * Function: sd_get_write_cache_enabled() 9066 * 9067 * Description: This routine is the driver entry point for determining if 9068 * write caching is enabled. It examines the WCE (write cache 9069 * enable) bits of mode page 8 (MODEPAGE_CACHING). 9070 * 9071 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 9072 * structure for this target. 9073 * is_enabled - pointer to int where write cache enabled state 9074 * is returned (non-zero -> write cache enabled) 9075 * 9076 * 9077 * Return Code: EIO 9078 * code returned by sd_send_scsi_MODE_SENSE 9079 * 9080 * Context: Kernel Thread 9081 * 9082 * NOTE: If ioctl is added to disable write cache, this sequence should 9083 * be followed so that no locking is required for accesses to 9084 * un->un_f_write_cache_enabled: 9085 * do mode select to clear wce 9086 * do synchronize cache to flush cache 9087 * set un->un_f_write_cache_enabled = FALSE 9088 * 9089 * Conversely, an ioctl to enable the write cache should be done 9090 * in this order: 9091 * set un->un_f_write_cache_enabled = TRUE 9092 * do mode select to set wce 9093 */ 9094 9095 static int 9096 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled) 9097 { 9098 struct mode_caching *mode_caching_page; 9099 uchar_t *header; 9100 size_t buflen; 9101 int hdrlen; 9102 int bd_len; 9103 int rval = 0; 9104 struct sd_lun *un; 9105 int status; 9106 9107 ASSERT(ssc != NULL); 9108 un = ssc->ssc_un; 9109 ASSERT(un != NULL); 9110 ASSERT(is_enabled != NULL); 9111 9112 /* in case of error, flag as enabled */ 9113 *is_enabled = TRUE; 9114 9115 /* 9116 * Do a test unit ready, otherwise a mode sense may not work if this 9117 * is the first command sent to the device after boot. 9118 */ 9119 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 9120 9121 if (status != 0) 9122 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9123 9124 if (un->un_f_cfg_is_atapi == TRUE) { 9125 hdrlen = MODE_HEADER_LENGTH_GRP2; 9126 } else { 9127 hdrlen = MODE_HEADER_LENGTH; 9128 } 9129 9130 /* 9131 * Allocate memory for the retrieved mode page and its headers. Set 9132 * a pointer to the page itself. 9133 */ 9134 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 9135 header = kmem_zalloc(buflen, KM_SLEEP); 9136 9137 /* Get the information from the device. */ 9138 if (un->un_f_cfg_is_atapi == TRUE) { 9139 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen, 9140 MODEPAGE_CACHING, SD_PATH_DIRECT); 9141 } else { 9142 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen, 9143 MODEPAGE_CACHING, SD_PATH_DIRECT); 9144 } 9145 9146 if (rval != 0) { 9147 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 9148 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 9149 goto mode_sense_failed; 9150 } 9151 9152 /* 9153 * Determine size of Block Descriptors in order to locate 9154 * the mode page data. ATAPI devices return 0, SCSI devices 9155 * should return MODE_BLK_DESC_LENGTH. 9156 */ 9157 if (un->un_f_cfg_is_atapi == TRUE) { 9158 struct mode_header_grp2 *mhp; 9159 mhp = (struct mode_header_grp2 *)header; 9160 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 9161 } else { 9162 bd_len = ((struct mode_header *)header)->bdesc_length; 9163 } 9164 9165 if (bd_len > MODE_BLK_DESC_LENGTH) { 9166 /* FMA should make upset complain here */ 9167 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0, 9168 "sd_get_write_cache_enabled: Mode Sense returned invalid " 9169 "block descriptor length\n"); 9170 rval = EIO; 9171 goto mode_sense_failed; 9172 } 9173 9174 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 9175 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 9176 /* FMA could make upset complain here */ 9177 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON, 9178 "sd_get_write_cache_enabled: Mode Sense caching page " 9179 "code mismatch %d\n", mode_caching_page->mode_page.code); 9180 rval = EIO; 9181 goto mode_sense_failed; 9182 } 9183 *is_enabled = mode_caching_page->wce; 9184 9185 mode_sense_failed: 9186 if (rval == 0) { 9187 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 9188 } else if (rval == EIO) { 9189 /* 9190 * Some disks do not support mode sense(6), we 9191 * should ignore this kind of error(sense key is 9192 * 0x5 - illegal request). 9193 */ 9194 uint8_t *sensep; 9195 int senlen; 9196 9197 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 9198 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 9199 ssc->ssc_uscsi_cmd->uscsi_rqresid); 9200 9201 if (senlen > 0 && 9202 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 9203 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 9204 } else { 9205 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 9206 } 9207 } else { 9208 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9209 } 9210 kmem_free(header, buflen); 9211 return (rval); 9212 } 9213 9214 /* 9215 * Function: sd_get_nv_sup() 9216 * 9217 * Description: This routine is the driver entry point for 9218 * determining whether non-volatile cache is supported. This 9219 * determination process works as follows: 9220 * 9221 * 1. sd first queries sd.conf on whether 9222 * suppress_cache_flush bit is set for this device. 9223 * 9224 * 2. if not there, then queries the internal disk table. 9225 * 9226 * 3. if either sd.conf or internal disk table specifies 9227 * cache flush be suppressed, we don't bother checking 9228 * NV_SUP bit. 9229 * 9230 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 9231 * the optional INQUIRY VPD page 0x86. If the device 9232 * supports VPD page 0x86, sd examines the NV_SUP 9233 * (non-volatile cache support) bit in the INQUIRY VPD page 9234 * 0x86: 9235 * o If NV_SUP bit is set, sd assumes the device has a 9236 * non-volatile cache and set the 9237 * un_f_sync_nv_supported to TRUE. 9238 * o Otherwise cache is not non-volatile, 9239 * un_f_sync_nv_supported is set to FALSE. 9240 * 9241 * Arguments: un - driver soft state (unit) structure 9242 * 9243 * Return Code: 9244 * 9245 * Context: Kernel Thread 9246 */ 9247 9248 static void 9249 sd_get_nv_sup(sd_ssc_t *ssc) 9250 { 9251 int rval = 0; 9252 uchar_t *inq86 = NULL; 9253 size_t inq86_len = MAX_INQUIRY_SIZE; 9254 size_t inq86_resid = 0; 9255 struct dk_callback *dkc; 9256 struct sd_lun *un; 9257 9258 ASSERT(ssc != NULL); 9259 un = ssc->ssc_un; 9260 ASSERT(un != NULL); 9261 9262 mutex_enter(SD_MUTEX(un)); 9263 9264 /* 9265 * Be conservative on the device's support of 9266 * SYNC_NV bit: un_f_sync_nv_supported is 9267 * initialized to be false. 9268 */ 9269 un->un_f_sync_nv_supported = FALSE; 9270 9271 /* 9272 * If either sd.conf or internal disk table 9273 * specifies cache flush be suppressed, then 9274 * we don't bother checking NV_SUP bit. 9275 */ 9276 if (un->un_f_suppress_cache_flush == TRUE) { 9277 mutex_exit(SD_MUTEX(un)); 9278 return; 9279 } 9280 9281 if (sd_check_vpd_page_support(ssc) == 0 && 9282 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 9283 mutex_exit(SD_MUTEX(un)); 9284 /* collect page 86 data if available */ 9285 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 9286 9287 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len, 9288 0x01, 0x86, &inq86_resid); 9289 9290 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 9291 SD_TRACE(SD_LOG_COMMON, un, 9292 "sd_get_nv_sup: \ 9293 successfully get VPD page: %x \ 9294 PAGE LENGTH: %x BYTE 6: %x\n", 9295 inq86[1], inq86[3], inq86[6]); 9296 9297 mutex_enter(SD_MUTEX(un)); 9298 /* 9299 * check the value of NV_SUP bit: only if the device 9300 * reports NV_SUP bit to be 1, the 9301 * un_f_sync_nv_supported bit will be set to true. 9302 */ 9303 if (inq86[6] & SD_VPD_NV_SUP) { 9304 un->un_f_sync_nv_supported = TRUE; 9305 } 9306 mutex_exit(SD_MUTEX(un)); 9307 } else if (rval != 0) { 9308 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9309 } 9310 9311 kmem_free(inq86, inq86_len); 9312 } else { 9313 mutex_exit(SD_MUTEX(un)); 9314 } 9315 9316 /* 9317 * Send a SYNC CACHE command to check whether 9318 * SYNC_NV bit is supported. This command should have 9319 * un_f_sync_nv_supported set to correct value. 9320 */ 9321 mutex_enter(SD_MUTEX(un)); 9322 if (un->un_f_sync_nv_supported) { 9323 mutex_exit(SD_MUTEX(un)); 9324 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 9325 dkc->dkc_flag = FLUSH_VOLATILE; 9326 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 9327 9328 /* 9329 * Send a TEST UNIT READY command to the device. This should 9330 * clear any outstanding UNIT ATTENTION that may be present. 9331 */ 9332 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR); 9333 if (rval != 0) 9334 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 9335 9336 kmem_free(dkc, sizeof (struct dk_callback)); 9337 } else { 9338 mutex_exit(SD_MUTEX(un)); 9339 } 9340 9341 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 9342 un_f_suppress_cache_flush is set to %d\n", 9343 un->un_f_suppress_cache_flush); 9344 } 9345 9346 /* 9347 * Function: sd_make_device 9348 * 9349 * Description: Utility routine to return the Solaris device number from 9350 * the data in the device's dev_info structure. 9351 * 9352 * Return Code: The Solaris device number 9353 * 9354 * Context: Any 9355 */ 9356 9357 static dev_t 9358 sd_make_device(dev_info_t *devi) 9359 { 9360 return (makedevice(ddi_driver_major(devi), 9361 ddi_get_instance(devi) << SDUNIT_SHIFT)); 9362 } 9363 9364 9365 /* 9366 * Function: sd_pm_entry 9367 * 9368 * Description: Called at the start of a new command to manage power 9369 * and busy status of a device. This includes determining whether 9370 * the current power state of the device is sufficient for 9371 * performing the command or whether it must be changed. 9372 * The PM framework is notified appropriately. 9373 * Only with a return status of DDI_SUCCESS will the 9374 * component be busy to the framework. 9375 * 9376 * All callers of sd_pm_entry must check the return status 9377 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 9378 * of DDI_FAILURE indicates the device failed to power up. 9379 * In this case un_pm_count has been adjusted so the result 9380 * on exit is still powered down, ie. count is less than 0. 9381 * Calling sd_pm_exit with this count value hits an ASSERT. 9382 * 9383 * Return Code: DDI_SUCCESS or DDI_FAILURE 9384 * 9385 * Context: Kernel thread context. 9386 */ 9387 9388 static int 9389 sd_pm_entry(struct sd_lun *un) 9390 { 9391 int return_status = DDI_SUCCESS; 9392 9393 ASSERT(!mutex_owned(SD_MUTEX(un))); 9394 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9395 9396 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 9397 9398 if (un->un_f_pm_is_enabled == FALSE) { 9399 SD_TRACE(SD_LOG_IO_PM, un, 9400 "sd_pm_entry: exiting, PM not enabled\n"); 9401 return (return_status); 9402 } 9403 9404 /* 9405 * Just increment a counter if PM is enabled. On the transition from 9406 * 0 ==> 1, mark the device as busy. The iodone side will decrement 9407 * the count with each IO and mark the device as idle when the count 9408 * hits 0. 9409 * 9410 * If the count is less than 0 the device is powered down. If a powered 9411 * down device is successfully powered up then the count must be 9412 * incremented to reflect the power up. Note that it'll get incremented 9413 * a second time to become busy. 9414 * 9415 * Because the following has the potential to change the device state 9416 * and must release the un_pm_mutex to do so, only one thread can be 9417 * allowed through at a time. 9418 */ 9419 9420 mutex_enter(&un->un_pm_mutex); 9421 while (un->un_pm_busy == TRUE) { 9422 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 9423 } 9424 un->un_pm_busy = TRUE; 9425 9426 if (un->un_pm_count < 1) { 9427 9428 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 9429 9430 /* 9431 * Indicate we are now busy so the framework won't attempt to 9432 * power down the device. This call will only fail if either 9433 * we passed a bad component number or the device has no 9434 * components. Neither of these should ever happen. 9435 */ 9436 mutex_exit(&un->un_pm_mutex); 9437 return_status = pm_busy_component(SD_DEVINFO(un), 0); 9438 ASSERT(return_status == DDI_SUCCESS); 9439 9440 mutex_enter(&un->un_pm_mutex); 9441 9442 if (un->un_pm_count < 0) { 9443 mutex_exit(&un->un_pm_mutex); 9444 9445 SD_TRACE(SD_LOG_IO_PM, un, 9446 "sd_pm_entry: power up component\n"); 9447 9448 /* 9449 * pm_raise_power will cause sdpower to be called 9450 * which brings the device power level to the 9451 * desired state, ON in this case. If successful, 9452 * un_pm_count and un_power_level will be updated 9453 * appropriately. 9454 */ 9455 return_status = pm_raise_power(SD_DEVINFO(un), 0, 9456 SD_SPINDLE_ON); 9457 9458 mutex_enter(&un->un_pm_mutex); 9459 9460 if (return_status != DDI_SUCCESS) { 9461 /* 9462 * Power up failed. 9463 * Idle the device and adjust the count 9464 * so the result on exit is that we're 9465 * still powered down, ie. count is less than 0. 9466 */ 9467 SD_TRACE(SD_LOG_IO_PM, un, 9468 "sd_pm_entry: power up failed," 9469 " idle the component\n"); 9470 9471 (void) pm_idle_component(SD_DEVINFO(un), 0); 9472 un->un_pm_count--; 9473 } else { 9474 /* 9475 * Device is powered up, verify the 9476 * count is non-negative. 9477 * This is debug only. 9478 */ 9479 ASSERT(un->un_pm_count == 0); 9480 } 9481 } 9482 9483 if (return_status == DDI_SUCCESS) { 9484 /* 9485 * For performance, now that the device has been tagged 9486 * as busy, and it's known to be powered up, update the 9487 * chain types to use jump tables that do not include 9488 * pm. This significantly lowers the overhead and 9489 * therefore improves performance. 9490 */ 9491 9492 mutex_exit(&un->un_pm_mutex); 9493 mutex_enter(SD_MUTEX(un)); 9494 SD_TRACE(SD_LOG_IO_PM, un, 9495 "sd_pm_entry: changing uscsi_chain_type from %d\n", 9496 un->un_uscsi_chain_type); 9497 9498 if (un->un_f_non_devbsize_supported) { 9499 un->un_buf_chain_type = 9500 SD_CHAIN_INFO_RMMEDIA_NO_PM; 9501 } else { 9502 un->un_buf_chain_type = 9503 SD_CHAIN_INFO_DISK_NO_PM; 9504 } 9505 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 9506 9507 SD_TRACE(SD_LOG_IO_PM, un, 9508 " changed uscsi_chain_type to %d\n", 9509 un->un_uscsi_chain_type); 9510 mutex_exit(SD_MUTEX(un)); 9511 mutex_enter(&un->un_pm_mutex); 9512 9513 if (un->un_pm_idle_timeid == NULL) { 9514 /* 300 ms. */ 9515 un->un_pm_idle_timeid = 9516 timeout(sd_pm_idletimeout_handler, un, 9517 (drv_usectohz((clock_t)300000))); 9518 /* 9519 * Include an extra call to busy which keeps the 9520 * device busy with-respect-to the PM layer 9521 * until the timer fires, at which time it'll 9522 * get the extra idle call. 9523 */ 9524 (void) pm_busy_component(SD_DEVINFO(un), 0); 9525 } 9526 } 9527 } 9528 un->un_pm_busy = FALSE; 9529 /* Next... */ 9530 cv_signal(&un->un_pm_busy_cv); 9531 9532 un->un_pm_count++; 9533 9534 SD_TRACE(SD_LOG_IO_PM, un, 9535 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 9536 9537 mutex_exit(&un->un_pm_mutex); 9538 9539 return (return_status); 9540 } 9541 9542 9543 /* 9544 * Function: sd_pm_exit 9545 * 9546 * Description: Called at the completion of a command to manage busy 9547 * status for the device. If the device becomes idle the 9548 * PM framework is notified. 9549 * 9550 * Context: Kernel thread context 9551 */ 9552 9553 static void 9554 sd_pm_exit(struct sd_lun *un) 9555 { 9556 ASSERT(!mutex_owned(SD_MUTEX(un))); 9557 ASSERT(!mutex_owned(&un->un_pm_mutex)); 9558 9559 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 9560 9561 /* 9562 * After attach the following flag is only read, so don't 9563 * take the penalty of acquiring a mutex for it. 9564 */ 9565 if (un->un_f_pm_is_enabled == TRUE) { 9566 9567 mutex_enter(&un->un_pm_mutex); 9568 un->un_pm_count--; 9569 9570 SD_TRACE(SD_LOG_IO_PM, un, 9571 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 9572 9573 ASSERT(un->un_pm_count >= 0); 9574 if (un->un_pm_count == 0) { 9575 mutex_exit(&un->un_pm_mutex); 9576 9577 SD_TRACE(SD_LOG_IO_PM, un, 9578 "sd_pm_exit: idle component\n"); 9579 9580 (void) pm_idle_component(SD_DEVINFO(un), 0); 9581 9582 } else { 9583 mutex_exit(&un->un_pm_mutex); 9584 } 9585 } 9586 9587 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 9588 } 9589 9590 9591 /* 9592 * Function: sdopen 9593 * 9594 * Description: Driver's open(9e) entry point function. 9595 * 9596 * Arguments: dev_i - pointer to device number 9597 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 9598 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9599 * cred_p - user credential pointer 9600 * 9601 * Return Code: EINVAL 9602 * ENXIO 9603 * EIO 9604 * EROFS 9605 * EBUSY 9606 * 9607 * Context: Kernel thread context 9608 */ 9609 /* ARGSUSED */ 9610 static int 9611 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 9612 { 9613 struct sd_lun *un; 9614 int nodelay; 9615 int part; 9616 uint64_t partmask; 9617 int instance; 9618 dev_t dev; 9619 int rval = EIO; 9620 diskaddr_t nblks = 0; 9621 diskaddr_t label_cap; 9622 9623 /* Validate the open type */ 9624 if (otyp >= OTYPCNT) { 9625 return (EINVAL); 9626 } 9627 9628 dev = *dev_p; 9629 instance = SDUNIT(dev); 9630 mutex_enter(&sd_detach_mutex); 9631 9632 /* 9633 * Fail the open if there is no softstate for the instance, or 9634 * if another thread somewhere is trying to detach the instance. 9635 */ 9636 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 9637 (un->un_detach_count != 0)) { 9638 mutex_exit(&sd_detach_mutex); 9639 /* 9640 * The probe cache only needs to be cleared when open (9e) fails 9641 * with ENXIO (4238046). 9642 */ 9643 /* 9644 * un-conditionally clearing probe cache is ok with 9645 * separate sd/ssd binaries 9646 * x86 platform can be an issue with both parallel 9647 * and fibre in 1 binary 9648 */ 9649 sd_scsi_clear_probe_cache(); 9650 return (ENXIO); 9651 } 9652 9653 /* 9654 * The un_layer_count is to prevent another thread in specfs from 9655 * trying to detach the instance, which can happen when we are 9656 * called from a higher-layer driver instead of thru specfs. 9657 * This will not be needed when DDI provides a layered driver 9658 * interface that allows specfs to know that an instance is in 9659 * use by a layered driver & should not be detached. 9660 * 9661 * Note: the semantics for layered driver opens are exactly one 9662 * close for every open. 9663 */ 9664 if (otyp == OTYP_LYR) { 9665 un->un_layer_count++; 9666 } 9667 9668 /* 9669 * Keep a count of the current # of opens in progress. This is because 9670 * some layered drivers try to call us as a regular open. This can 9671 * cause problems that we cannot prevent, however by keeping this count 9672 * we can at least keep our open and detach routines from racing against 9673 * each other under such conditions. 9674 */ 9675 un->un_opens_in_progress++; 9676 mutex_exit(&sd_detach_mutex); 9677 9678 nodelay = (flag & (FNDELAY | FNONBLOCK)); 9679 part = SDPART(dev); 9680 partmask = 1 << part; 9681 9682 /* 9683 * We use a semaphore here in order to serialize 9684 * open and close requests on the device. 9685 */ 9686 sema_p(&un->un_semoclose); 9687 9688 mutex_enter(SD_MUTEX(un)); 9689 9690 /* 9691 * All device accesses go thru sdstrategy() where we check 9692 * on suspend status but there could be a scsi_poll command, 9693 * which bypasses sdstrategy(), so we need to check pm 9694 * status. 9695 */ 9696 9697 if (!nodelay) { 9698 while ((un->un_state == SD_STATE_SUSPENDED) || 9699 (un->un_state == SD_STATE_PM_CHANGING)) { 9700 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9701 } 9702 9703 mutex_exit(SD_MUTEX(un)); 9704 if (sd_pm_entry(un) != DDI_SUCCESS) { 9705 rval = EIO; 9706 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 9707 "sdopen: sd_pm_entry failed\n"); 9708 goto open_failed_with_pm; 9709 } 9710 mutex_enter(SD_MUTEX(un)); 9711 } 9712 9713 /* check for previous exclusive open */ 9714 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 9715 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9716 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 9717 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 9718 9719 if (un->un_exclopen & (partmask)) { 9720 goto excl_open_fail; 9721 } 9722 9723 if (flag & FEXCL) { 9724 int i; 9725 if (un->un_ocmap.lyropen[part]) { 9726 goto excl_open_fail; 9727 } 9728 for (i = 0; i < (OTYPCNT - 1); i++) { 9729 if (un->un_ocmap.regopen[i] & (partmask)) { 9730 goto excl_open_fail; 9731 } 9732 } 9733 } 9734 9735 /* 9736 * Check the write permission if this is a removable media device, 9737 * NDELAY has not been set, and writable permission is requested. 9738 * 9739 * Note: If NDELAY was set and this is write-protected media the WRITE 9740 * attempt will fail with EIO as part of the I/O processing. This is a 9741 * more permissive implementation that allows the open to succeed and 9742 * WRITE attempts to fail when appropriate. 9743 */ 9744 if (un->un_f_chk_wp_open) { 9745 if ((flag & FWRITE) && (!nodelay)) { 9746 mutex_exit(SD_MUTEX(un)); 9747 /* 9748 * Defer the check for write permission on writable 9749 * DVD drive till sdstrategy and will not fail open even 9750 * if FWRITE is set as the device can be writable 9751 * depending upon the media and the media can change 9752 * after the call to open(). 9753 */ 9754 if (un->un_f_dvdram_writable_device == FALSE) { 9755 if (ISCD(un) || sr_check_wp(dev)) { 9756 rval = EROFS; 9757 mutex_enter(SD_MUTEX(un)); 9758 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9759 "write to cd or write protected media\n"); 9760 goto open_fail; 9761 } 9762 } 9763 mutex_enter(SD_MUTEX(un)); 9764 } 9765 } 9766 9767 /* 9768 * If opening in NDELAY/NONBLOCK mode, just return. 9769 * Check if disk is ready and has a valid geometry later. 9770 */ 9771 if (!nodelay) { 9772 sd_ssc_t *ssc; 9773 9774 mutex_exit(SD_MUTEX(un)); 9775 ssc = sd_ssc_init(un); 9776 rval = sd_ready_and_valid(ssc, part); 9777 sd_ssc_fini(ssc); 9778 mutex_enter(SD_MUTEX(un)); 9779 /* 9780 * Fail if device is not ready or if the number of disk 9781 * blocks is zero or negative for non CD devices. 9782 */ 9783 9784 nblks = 0; 9785 9786 if (rval == SD_READY_VALID && (!ISCD(un))) { 9787 /* if cmlb_partinfo fails, nblks remains 0 */ 9788 mutex_exit(SD_MUTEX(un)); 9789 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 9790 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 9791 mutex_enter(SD_MUTEX(un)); 9792 } 9793 9794 if ((rval != SD_READY_VALID) || 9795 (!ISCD(un) && nblks <= 0)) { 9796 rval = un->un_f_has_removable_media ? ENXIO : EIO; 9797 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9798 "device not ready or invalid disk block value\n"); 9799 goto open_fail; 9800 } 9801 #if defined(__i386) || defined(__amd64) 9802 } else { 9803 uchar_t *cp; 9804 /* 9805 * x86 requires special nodelay handling, so that p0 is 9806 * always defined and accessible. 9807 * Invalidate geometry only if device is not already open. 9808 */ 9809 cp = &un->un_ocmap.chkd[0]; 9810 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9811 if (*cp != (uchar_t)0) { 9812 break; 9813 } 9814 cp++; 9815 } 9816 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9817 mutex_exit(SD_MUTEX(un)); 9818 cmlb_invalidate(un->un_cmlbhandle, 9819 (void *)SD_PATH_DIRECT); 9820 mutex_enter(SD_MUTEX(un)); 9821 } 9822 9823 #endif 9824 } 9825 9826 if (otyp == OTYP_LYR) { 9827 un->un_ocmap.lyropen[part]++; 9828 } else { 9829 un->un_ocmap.regopen[otyp] |= partmask; 9830 } 9831 9832 /* Set up open and exclusive open flags */ 9833 if (flag & FEXCL) { 9834 un->un_exclopen |= (partmask); 9835 } 9836 9837 /* 9838 * If the lun is EFI labeled and lun capacity is greater than the 9839 * capacity contained in the label, log a sys-event to notify the 9840 * interested module. 9841 * To avoid an infinite loop of logging sys-event, we only log the 9842 * event when the lun is not opened in NDELAY mode. The event handler 9843 * should open the lun in NDELAY mode. 9844 */ 9845 if (!(flag & FNDELAY)) { 9846 mutex_exit(SD_MUTEX(un)); 9847 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 9848 (void*)SD_PATH_DIRECT) == 0) { 9849 mutex_enter(SD_MUTEX(un)); 9850 if (un->un_f_blockcount_is_valid && 9851 un->un_blockcount > label_cap) { 9852 mutex_exit(SD_MUTEX(un)); 9853 sd_log_lun_expansion_event(un, 9854 (nodelay ? KM_NOSLEEP : KM_SLEEP)); 9855 mutex_enter(SD_MUTEX(un)); 9856 } 9857 } else { 9858 mutex_enter(SD_MUTEX(un)); 9859 } 9860 } 9861 9862 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9863 "open of part %d type %d\n", part, otyp); 9864 9865 mutex_exit(SD_MUTEX(un)); 9866 if (!nodelay) { 9867 sd_pm_exit(un); 9868 } 9869 9870 sema_v(&un->un_semoclose); 9871 9872 mutex_enter(&sd_detach_mutex); 9873 un->un_opens_in_progress--; 9874 mutex_exit(&sd_detach_mutex); 9875 9876 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 9877 return (DDI_SUCCESS); 9878 9879 excl_open_fail: 9880 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 9881 rval = EBUSY; 9882 9883 open_fail: 9884 mutex_exit(SD_MUTEX(un)); 9885 9886 /* 9887 * On a failed open we must exit the pm management. 9888 */ 9889 if (!nodelay) { 9890 sd_pm_exit(un); 9891 } 9892 open_failed_with_pm: 9893 sema_v(&un->un_semoclose); 9894 9895 mutex_enter(&sd_detach_mutex); 9896 un->un_opens_in_progress--; 9897 if (otyp == OTYP_LYR) { 9898 un->un_layer_count--; 9899 } 9900 mutex_exit(&sd_detach_mutex); 9901 9902 return (rval); 9903 } 9904 9905 9906 /* 9907 * Function: sdclose 9908 * 9909 * Description: Driver's close(9e) entry point function. 9910 * 9911 * Arguments: dev - device number 9912 * flag - file status flag, informational only 9913 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9914 * cred_p - user credential pointer 9915 * 9916 * Return Code: ENXIO 9917 * 9918 * Context: Kernel thread context 9919 */ 9920 /* ARGSUSED */ 9921 static int 9922 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 9923 { 9924 struct sd_lun *un; 9925 uchar_t *cp; 9926 int part; 9927 int nodelay; 9928 int rval = 0; 9929 9930 /* Validate the open type */ 9931 if (otyp >= OTYPCNT) { 9932 return (ENXIO); 9933 } 9934 9935 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9936 return (ENXIO); 9937 } 9938 9939 part = SDPART(dev); 9940 nodelay = flag & (FNDELAY | FNONBLOCK); 9941 9942 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9943 "sdclose: close of part %d type %d\n", part, otyp); 9944 9945 /* 9946 * We use a semaphore here in order to serialize 9947 * open and close requests on the device. 9948 */ 9949 sema_p(&un->un_semoclose); 9950 9951 mutex_enter(SD_MUTEX(un)); 9952 9953 /* Don't proceed if power is being changed. */ 9954 while (un->un_state == SD_STATE_PM_CHANGING) { 9955 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9956 } 9957 9958 if (un->un_exclopen & (1 << part)) { 9959 un->un_exclopen &= ~(1 << part); 9960 } 9961 9962 /* Update the open partition map */ 9963 if (otyp == OTYP_LYR) { 9964 un->un_ocmap.lyropen[part] -= 1; 9965 } else { 9966 un->un_ocmap.regopen[otyp] &= ~(1 << part); 9967 } 9968 9969 cp = &un->un_ocmap.chkd[0]; 9970 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9971 if (*cp != NULL) { 9972 break; 9973 } 9974 cp++; 9975 } 9976 9977 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9978 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 9979 9980 /* 9981 * We avoid persistance upon the last close, and set 9982 * the throttle back to the maximum. 9983 */ 9984 un->un_throttle = un->un_saved_throttle; 9985 9986 if (un->un_state == SD_STATE_OFFLINE) { 9987 if (un->un_f_is_fibre == FALSE) { 9988 scsi_log(SD_DEVINFO(un), sd_label, 9989 CE_WARN, "offline\n"); 9990 } 9991 mutex_exit(SD_MUTEX(un)); 9992 cmlb_invalidate(un->un_cmlbhandle, 9993 (void *)SD_PATH_DIRECT); 9994 mutex_enter(SD_MUTEX(un)); 9995 9996 } else { 9997 /* 9998 * Flush any outstanding writes in NVRAM cache. 9999 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10000 * cmd, it may not work for non-Pluto devices. 10001 * SYNCHRONIZE CACHE is not required for removables, 10002 * except DVD-RAM drives. 10003 * 10004 * Also note: because SYNCHRONIZE CACHE is currently 10005 * the only command issued here that requires the 10006 * drive be powered up, only do the power up before 10007 * sending the Sync Cache command. If additional 10008 * commands are added which require a powered up 10009 * drive, the following sequence may have to change. 10010 * 10011 * And finally, note that parallel SCSI on SPARC 10012 * only issues a Sync Cache to DVD-RAM, a newly 10013 * supported device. 10014 */ 10015 #if defined(__i386) || defined(__amd64) 10016 if ((un->un_f_sync_cache_supported && 10017 un->un_f_sync_cache_required) || 10018 un->un_f_dvdram_writable_device == TRUE) { 10019 #else 10020 if (un->un_f_dvdram_writable_device == TRUE) { 10021 #endif 10022 mutex_exit(SD_MUTEX(un)); 10023 if (sd_pm_entry(un) == DDI_SUCCESS) { 10024 rval = 10025 sd_send_scsi_SYNCHRONIZE_CACHE(un, 10026 NULL); 10027 /* ignore error if not supported */ 10028 if (rval == ENOTSUP) { 10029 rval = 0; 10030 } else if (rval != 0) { 10031 rval = EIO; 10032 } 10033 sd_pm_exit(un); 10034 } else { 10035 rval = EIO; 10036 } 10037 mutex_enter(SD_MUTEX(un)); 10038 } 10039 10040 /* 10041 * For devices which supports DOOR_LOCK, send an ALLOW 10042 * MEDIA REMOVAL command, but don't get upset if it 10043 * fails. We need to raise the power of the drive before 10044 * we can call sd_send_scsi_DOORLOCK() 10045 */ 10046 if (un->un_f_doorlock_supported) { 10047 mutex_exit(SD_MUTEX(un)); 10048 if (sd_pm_entry(un) == DDI_SUCCESS) { 10049 sd_ssc_t *ssc; 10050 10051 ssc = sd_ssc_init(un); 10052 rval = sd_send_scsi_DOORLOCK(ssc, 10053 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 10054 if (rval != 0) 10055 sd_ssc_assessment(ssc, 10056 SD_FMT_IGNORE); 10057 sd_ssc_fini(ssc); 10058 10059 sd_pm_exit(un); 10060 if (ISCD(un) && (rval != 0) && 10061 (nodelay != 0)) { 10062 rval = ENXIO; 10063 } 10064 } else { 10065 rval = EIO; 10066 } 10067 mutex_enter(SD_MUTEX(un)); 10068 } 10069 10070 /* 10071 * If a device has removable media, invalidate all 10072 * parameters related to media, such as geometry, 10073 * blocksize, and blockcount. 10074 */ 10075 if (un->un_f_has_removable_media) { 10076 sr_ejected(un); 10077 } 10078 10079 /* 10080 * Destroy the cache (if it exists) which was 10081 * allocated for the write maps since this is 10082 * the last close for this media. 10083 */ 10084 if (un->un_wm_cache) { 10085 /* 10086 * Check if there are pending commands. 10087 * and if there are give a warning and 10088 * do not destroy the cache. 10089 */ 10090 if (un->un_ncmds_in_driver > 0) { 10091 scsi_log(SD_DEVINFO(un), 10092 sd_label, CE_WARN, 10093 "Unable to clean up memory " 10094 "because of pending I/O\n"); 10095 } else { 10096 kmem_cache_destroy( 10097 un->un_wm_cache); 10098 un->un_wm_cache = NULL; 10099 } 10100 } 10101 } 10102 } 10103 10104 mutex_exit(SD_MUTEX(un)); 10105 sema_v(&un->un_semoclose); 10106 10107 if (otyp == OTYP_LYR) { 10108 mutex_enter(&sd_detach_mutex); 10109 /* 10110 * The detach routine may run when the layer count 10111 * drops to zero. 10112 */ 10113 un->un_layer_count--; 10114 mutex_exit(&sd_detach_mutex); 10115 } 10116 10117 return (rval); 10118 } 10119 10120 10121 /* 10122 * Function: sd_ready_and_valid 10123 * 10124 * Description: Test if device is ready and has a valid geometry. 10125 * 10126 * Arguments: ssc - sd_ssc_t will contain un 10127 * un - driver soft state (unit) structure 10128 * 10129 * Return Code: SD_READY_VALID ready and valid label 10130 * SD_NOT_READY_VALID not ready, no label 10131 * SD_RESERVED_BY_OTHERS reservation conflict 10132 * 10133 * Context: Never called at interrupt context. 10134 */ 10135 10136 static int 10137 sd_ready_and_valid(sd_ssc_t *ssc, int part) 10138 { 10139 struct sd_errstats *stp; 10140 uint64_t capacity; 10141 uint_t lbasize; 10142 int rval = SD_READY_VALID; 10143 char name_str[48]; 10144 int is_valid; 10145 struct sd_lun *un; 10146 int status; 10147 10148 ASSERT(ssc != NULL); 10149 un = ssc->ssc_un; 10150 ASSERT(un != NULL); 10151 ASSERT(!mutex_owned(SD_MUTEX(un))); 10152 10153 mutex_enter(SD_MUTEX(un)); 10154 /* 10155 * If a device has removable media, we must check if media is 10156 * ready when checking if this device is ready and valid. 10157 */ 10158 if (un->un_f_has_removable_media) { 10159 mutex_exit(SD_MUTEX(un)); 10160 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10161 10162 if (status != 0) { 10163 rval = SD_NOT_READY_VALID; 10164 mutex_enter(SD_MUTEX(un)); 10165 10166 /* Ignore all failed status for removalbe media */ 10167 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10168 10169 goto done; 10170 } 10171 10172 is_valid = SD_IS_VALID_LABEL(un); 10173 mutex_enter(SD_MUTEX(un)); 10174 if (!is_valid || 10175 (un->un_f_blockcount_is_valid == FALSE) || 10176 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 10177 10178 /* capacity has to be read every open. */ 10179 mutex_exit(SD_MUTEX(un)); 10180 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity, 10181 &lbasize, SD_PATH_DIRECT); 10182 10183 if (status != 0) { 10184 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10185 10186 cmlb_invalidate(un->un_cmlbhandle, 10187 (void *)SD_PATH_DIRECT); 10188 mutex_enter(SD_MUTEX(un)); 10189 rval = SD_NOT_READY_VALID; 10190 10191 goto done; 10192 } else { 10193 mutex_enter(SD_MUTEX(un)); 10194 sd_update_block_info(un, lbasize, capacity); 10195 } 10196 } 10197 10198 /* 10199 * Check if the media in the device is writable or not. 10200 */ 10201 if (!is_valid && ISCD(un)) { 10202 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 10203 } 10204 10205 } else { 10206 /* 10207 * Do a test unit ready to clear any unit attention from non-cd 10208 * devices. 10209 */ 10210 mutex_exit(SD_MUTEX(un)); 10211 10212 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10213 if (status != 0) { 10214 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10215 } 10216 10217 mutex_enter(SD_MUTEX(un)); 10218 } 10219 10220 10221 /* 10222 * If this is a non 512 block device, allocate space for 10223 * the wmap cache. This is being done here since every time 10224 * a media is changed this routine will be called and the 10225 * block size is a function of media rather than device. 10226 */ 10227 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 10228 if (!(un->un_wm_cache)) { 10229 (void) snprintf(name_str, sizeof (name_str), 10230 "%s%d_cache", 10231 ddi_driver_name(SD_DEVINFO(un)), 10232 ddi_get_instance(SD_DEVINFO(un))); 10233 un->un_wm_cache = kmem_cache_create( 10234 name_str, sizeof (struct sd_w_map), 10235 8, sd_wm_cache_constructor, 10236 sd_wm_cache_destructor, NULL, 10237 (void *)un, NULL, 0); 10238 if (!(un->un_wm_cache)) { 10239 rval = ENOMEM; 10240 goto done; 10241 } 10242 } 10243 } 10244 10245 if (un->un_state == SD_STATE_NORMAL) { 10246 /* 10247 * If the target is not yet ready here (defined by a TUR 10248 * failure), invalidate the geometry and print an 'offline' 10249 * message. This is a legacy message, as the state of the 10250 * target is not actually changed to SD_STATE_OFFLINE. 10251 * 10252 * If the TUR fails for EACCES (Reservation Conflict), 10253 * SD_RESERVED_BY_OTHERS will be returned to indicate 10254 * reservation conflict. If the TUR fails for other 10255 * reasons, SD_NOT_READY_VALID will be returned. 10256 */ 10257 int err; 10258 10259 mutex_exit(SD_MUTEX(un)); 10260 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 10261 mutex_enter(SD_MUTEX(un)); 10262 10263 if (err != 0) { 10264 mutex_exit(SD_MUTEX(un)); 10265 cmlb_invalidate(un->un_cmlbhandle, 10266 (void *)SD_PATH_DIRECT); 10267 mutex_enter(SD_MUTEX(un)); 10268 if (err == EACCES) { 10269 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10270 "reservation conflict\n"); 10271 rval = SD_RESERVED_BY_OTHERS; 10272 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10273 } else { 10274 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10275 "drive offline\n"); 10276 rval = SD_NOT_READY_VALID; 10277 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 10278 } 10279 goto done; 10280 } 10281 } 10282 10283 if (un->un_f_format_in_progress == FALSE) { 10284 mutex_exit(SD_MUTEX(un)); 10285 10286 (void) cmlb_validate(un->un_cmlbhandle, 0, 10287 (void *)SD_PATH_DIRECT); 10288 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL, 10289 NULL, (void *) SD_PATH_DIRECT) != 0) { 10290 rval = SD_NOT_READY_VALID; 10291 mutex_enter(SD_MUTEX(un)); 10292 10293 goto done; 10294 } 10295 if (un->un_f_pkstats_enabled) { 10296 sd_set_pstats(un); 10297 SD_TRACE(SD_LOG_IO_PARTITION, un, 10298 "sd_ready_and_valid: un:0x%p pstats created and " 10299 "set\n", un); 10300 } 10301 mutex_enter(SD_MUTEX(un)); 10302 } 10303 10304 /* 10305 * If this device supports DOOR_LOCK command, try and send 10306 * this command to PREVENT MEDIA REMOVAL, but don't get upset 10307 * if it fails. For a CD, however, it is an error 10308 */ 10309 if (un->un_f_doorlock_supported) { 10310 mutex_exit(SD_MUTEX(un)); 10311 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 10312 SD_PATH_DIRECT); 10313 10314 if ((status != 0) && ISCD(un)) { 10315 rval = SD_NOT_READY_VALID; 10316 mutex_enter(SD_MUTEX(un)); 10317 10318 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10319 10320 goto done; 10321 } else if (status != 0) 10322 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 10323 mutex_enter(SD_MUTEX(un)); 10324 } 10325 10326 /* The state has changed, inform the media watch routines */ 10327 un->un_mediastate = DKIO_INSERTED; 10328 cv_broadcast(&un->un_state_cv); 10329 rval = SD_READY_VALID; 10330 10331 done: 10332 10333 /* 10334 * Initialize the capacity kstat value, if no media previously 10335 * (capacity kstat is 0) and a media has been inserted 10336 * (un_blockcount > 0). 10337 */ 10338 if (un->un_errstats != NULL) { 10339 stp = (struct sd_errstats *)un->un_errstats->ks_data; 10340 if ((stp->sd_capacity.value.ui64 == 0) && 10341 (un->un_f_blockcount_is_valid == TRUE)) { 10342 stp->sd_capacity.value.ui64 = 10343 (uint64_t)((uint64_t)un->un_blockcount * 10344 un->un_sys_blocksize); 10345 } 10346 } 10347 10348 mutex_exit(SD_MUTEX(un)); 10349 return (rval); 10350 } 10351 10352 10353 /* 10354 * Function: sdmin 10355 * 10356 * Description: Routine to limit the size of a data transfer. Used in 10357 * conjunction with physio(9F). 10358 * 10359 * Arguments: bp - pointer to the indicated buf(9S) struct. 10360 * 10361 * Context: Kernel thread context. 10362 */ 10363 10364 static void 10365 sdmin(struct buf *bp) 10366 { 10367 struct sd_lun *un; 10368 int instance; 10369 10370 instance = SDUNIT(bp->b_edev); 10371 10372 un = ddi_get_soft_state(sd_state, instance); 10373 ASSERT(un != NULL); 10374 10375 if (bp->b_bcount > un->un_max_xfer_size) { 10376 bp->b_bcount = un->un_max_xfer_size; 10377 } 10378 } 10379 10380 10381 /* 10382 * Function: sdread 10383 * 10384 * Description: Driver's read(9e) entry point function. 10385 * 10386 * Arguments: dev - device number 10387 * uio - structure pointer describing where data is to be stored 10388 * in user's space 10389 * cred_p - user credential pointer 10390 * 10391 * Return Code: ENXIO 10392 * EIO 10393 * EINVAL 10394 * value returned by physio 10395 * 10396 * Context: Kernel thread context. 10397 */ 10398 /* ARGSUSED */ 10399 static int 10400 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 10401 { 10402 struct sd_lun *un = NULL; 10403 int secmask; 10404 int err = 0; 10405 sd_ssc_t *ssc; 10406 10407 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10408 return (ENXIO); 10409 } 10410 10411 ASSERT(!mutex_owned(SD_MUTEX(un))); 10412 10413 10414 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10415 mutex_enter(SD_MUTEX(un)); 10416 /* 10417 * Because the call to sd_ready_and_valid will issue I/O we 10418 * must wait here if either the device is suspended or 10419 * if it's power level is changing. 10420 */ 10421 while ((un->un_state == SD_STATE_SUSPENDED) || 10422 (un->un_state == SD_STATE_PM_CHANGING)) { 10423 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10424 } 10425 un->un_ncmds_in_driver++; 10426 mutex_exit(SD_MUTEX(un)); 10427 10428 /* Initialize sd_ssc_t for internal uscsi commands */ 10429 ssc = sd_ssc_init(un); 10430 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10431 err = EIO; 10432 } else { 10433 err = 0; 10434 } 10435 sd_ssc_fini(ssc); 10436 10437 mutex_enter(SD_MUTEX(un)); 10438 un->un_ncmds_in_driver--; 10439 ASSERT(un->un_ncmds_in_driver >= 0); 10440 mutex_exit(SD_MUTEX(un)); 10441 if (err != 0) 10442 return (err); 10443 } 10444 10445 /* 10446 * Read requests are restricted to multiples of the system block size. 10447 */ 10448 secmask = un->un_sys_blocksize - 1; 10449 10450 if (uio->uio_loffset & ((offset_t)(secmask))) { 10451 SD_ERROR(SD_LOG_READ_WRITE, un, 10452 "sdread: file offset not modulo %d\n", 10453 un->un_sys_blocksize); 10454 err = EINVAL; 10455 } else if (uio->uio_iov->iov_len & (secmask)) { 10456 SD_ERROR(SD_LOG_READ_WRITE, un, 10457 "sdread: transfer length not modulo %d\n", 10458 un->un_sys_blocksize); 10459 err = EINVAL; 10460 } else { 10461 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 10462 } 10463 10464 return (err); 10465 } 10466 10467 10468 /* 10469 * Function: sdwrite 10470 * 10471 * Description: Driver's write(9e) entry point function. 10472 * 10473 * Arguments: dev - device number 10474 * uio - structure pointer describing where data is stored in 10475 * user's space 10476 * cred_p - user credential pointer 10477 * 10478 * Return Code: ENXIO 10479 * EIO 10480 * EINVAL 10481 * value returned by physio 10482 * 10483 * Context: Kernel thread context. 10484 */ 10485 /* ARGSUSED */ 10486 static int 10487 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 10488 { 10489 struct sd_lun *un = NULL; 10490 int secmask; 10491 int err = 0; 10492 sd_ssc_t *ssc; 10493 10494 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10495 return (ENXIO); 10496 } 10497 10498 ASSERT(!mutex_owned(SD_MUTEX(un))); 10499 10500 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10501 mutex_enter(SD_MUTEX(un)); 10502 /* 10503 * Because the call to sd_ready_and_valid will issue I/O we 10504 * must wait here if either the device is suspended or 10505 * if it's power level is changing. 10506 */ 10507 while ((un->un_state == SD_STATE_SUSPENDED) || 10508 (un->un_state == SD_STATE_PM_CHANGING)) { 10509 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10510 } 10511 un->un_ncmds_in_driver++; 10512 mutex_exit(SD_MUTEX(un)); 10513 10514 /* Initialize sd_ssc_t for internal uscsi commands */ 10515 ssc = sd_ssc_init(un); 10516 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10517 err = EIO; 10518 } else { 10519 err = 0; 10520 } 10521 sd_ssc_fini(ssc); 10522 10523 mutex_enter(SD_MUTEX(un)); 10524 un->un_ncmds_in_driver--; 10525 ASSERT(un->un_ncmds_in_driver >= 0); 10526 mutex_exit(SD_MUTEX(un)); 10527 if (err != 0) 10528 return (err); 10529 } 10530 10531 /* 10532 * Write requests are restricted to multiples of the system block size. 10533 */ 10534 secmask = un->un_sys_blocksize - 1; 10535 10536 if (uio->uio_loffset & ((offset_t)(secmask))) { 10537 SD_ERROR(SD_LOG_READ_WRITE, un, 10538 "sdwrite: file offset not modulo %d\n", 10539 un->un_sys_blocksize); 10540 err = EINVAL; 10541 } else if (uio->uio_iov->iov_len & (secmask)) { 10542 SD_ERROR(SD_LOG_READ_WRITE, un, 10543 "sdwrite: transfer length not modulo %d\n", 10544 un->un_sys_blocksize); 10545 err = EINVAL; 10546 } else { 10547 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 10548 } 10549 10550 return (err); 10551 } 10552 10553 10554 /* 10555 * Function: sdaread 10556 * 10557 * Description: Driver's aread(9e) entry point function. 10558 * 10559 * Arguments: dev - device number 10560 * aio - structure pointer describing where data is to be stored 10561 * cred_p - user credential pointer 10562 * 10563 * Return Code: ENXIO 10564 * EIO 10565 * EINVAL 10566 * value returned by aphysio 10567 * 10568 * Context: Kernel thread context. 10569 */ 10570 /* ARGSUSED */ 10571 static int 10572 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10573 { 10574 struct sd_lun *un = NULL; 10575 struct uio *uio = aio->aio_uio; 10576 int secmask; 10577 int err = 0; 10578 sd_ssc_t *ssc; 10579 10580 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10581 return (ENXIO); 10582 } 10583 10584 ASSERT(!mutex_owned(SD_MUTEX(un))); 10585 10586 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10587 mutex_enter(SD_MUTEX(un)); 10588 /* 10589 * Because the call to sd_ready_and_valid will issue I/O we 10590 * must wait here if either the device is suspended or 10591 * if it's power level is changing. 10592 */ 10593 while ((un->un_state == SD_STATE_SUSPENDED) || 10594 (un->un_state == SD_STATE_PM_CHANGING)) { 10595 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10596 } 10597 un->un_ncmds_in_driver++; 10598 mutex_exit(SD_MUTEX(un)); 10599 10600 /* Initialize sd_ssc_t for internal uscsi commands */ 10601 ssc = sd_ssc_init(un); 10602 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10603 err = EIO; 10604 } else { 10605 err = 0; 10606 } 10607 sd_ssc_fini(ssc); 10608 10609 mutex_enter(SD_MUTEX(un)); 10610 un->un_ncmds_in_driver--; 10611 ASSERT(un->un_ncmds_in_driver >= 0); 10612 mutex_exit(SD_MUTEX(un)); 10613 if (err != 0) 10614 return (err); 10615 } 10616 10617 /* 10618 * Read requests are restricted to multiples of the system block size. 10619 */ 10620 secmask = un->un_sys_blocksize - 1; 10621 10622 if (uio->uio_loffset & ((offset_t)(secmask))) { 10623 SD_ERROR(SD_LOG_READ_WRITE, un, 10624 "sdaread: file offset not modulo %d\n", 10625 un->un_sys_blocksize); 10626 err = EINVAL; 10627 } else if (uio->uio_iov->iov_len & (secmask)) { 10628 SD_ERROR(SD_LOG_READ_WRITE, un, 10629 "sdaread: transfer length not modulo %d\n", 10630 un->un_sys_blocksize); 10631 err = EINVAL; 10632 } else { 10633 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 10634 } 10635 10636 return (err); 10637 } 10638 10639 10640 /* 10641 * Function: sdawrite 10642 * 10643 * Description: Driver's awrite(9e) entry point function. 10644 * 10645 * Arguments: dev - device number 10646 * aio - structure pointer describing where data is stored 10647 * cred_p - user credential pointer 10648 * 10649 * Return Code: ENXIO 10650 * EIO 10651 * EINVAL 10652 * value returned by aphysio 10653 * 10654 * Context: Kernel thread context. 10655 */ 10656 /* ARGSUSED */ 10657 static int 10658 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 10659 { 10660 struct sd_lun *un = NULL; 10661 struct uio *uio = aio->aio_uio; 10662 int secmask; 10663 int err = 0; 10664 sd_ssc_t *ssc; 10665 10666 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10667 return (ENXIO); 10668 } 10669 10670 ASSERT(!mutex_owned(SD_MUTEX(un))); 10671 10672 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 10673 mutex_enter(SD_MUTEX(un)); 10674 /* 10675 * Because the call to sd_ready_and_valid will issue I/O we 10676 * must wait here if either the device is suspended or 10677 * if it's power level is changing. 10678 */ 10679 while ((un->un_state == SD_STATE_SUSPENDED) || 10680 (un->un_state == SD_STATE_PM_CHANGING)) { 10681 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10682 } 10683 un->un_ncmds_in_driver++; 10684 mutex_exit(SD_MUTEX(un)); 10685 10686 /* Initialize sd_ssc_t for internal uscsi commands */ 10687 ssc = sd_ssc_init(un); 10688 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) { 10689 err = EIO; 10690 } else { 10691 err = 0; 10692 } 10693 sd_ssc_fini(ssc); 10694 10695 mutex_enter(SD_MUTEX(un)); 10696 un->un_ncmds_in_driver--; 10697 ASSERT(un->un_ncmds_in_driver >= 0); 10698 mutex_exit(SD_MUTEX(un)); 10699 if (err != 0) 10700 return (err); 10701 } 10702 10703 /* 10704 * Write requests are restricted to multiples of the system block size. 10705 */ 10706 secmask = un->un_sys_blocksize - 1; 10707 10708 if (uio->uio_loffset & ((offset_t)(secmask))) { 10709 SD_ERROR(SD_LOG_READ_WRITE, un, 10710 "sdawrite: file offset not modulo %d\n", 10711 un->un_sys_blocksize); 10712 err = EINVAL; 10713 } else if (uio->uio_iov->iov_len & (secmask)) { 10714 SD_ERROR(SD_LOG_READ_WRITE, un, 10715 "sdawrite: transfer length not modulo %d\n", 10716 un->un_sys_blocksize); 10717 err = EINVAL; 10718 } else { 10719 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 10720 } 10721 10722 return (err); 10723 } 10724 10725 10726 10727 10728 10729 /* 10730 * Driver IO processing follows the following sequence: 10731 * 10732 * sdioctl(9E) sdstrategy(9E) biodone(9F) 10733 * | | ^ 10734 * v v | 10735 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 10736 * | | | | 10737 * v | | | 10738 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 10739 * | | ^ ^ 10740 * v v | | 10741 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 10742 * | | | | 10743 * +---+ | +------------+ +-------+ 10744 * | | | | 10745 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10746 * | v | | 10747 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 10748 * | | ^ | 10749 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10750 * | v | | 10751 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 10752 * | | ^ | 10753 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10754 * | v | | 10755 * | sd_checksum_iostart() sd_checksum_iodone() | 10756 * | | ^ | 10757 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 10758 * | v | | 10759 * | sd_pm_iostart() sd_pm_iodone() | 10760 * | | ^ | 10761 * | | | | 10762 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 10763 * | ^ 10764 * v | 10765 * sd_core_iostart() | 10766 * | | 10767 * | +------>(*destroypkt)() 10768 * +-> sd_start_cmds() <-+ | | 10769 * | | | v 10770 * | | | scsi_destroy_pkt(9F) 10771 * | | | 10772 * +->(*initpkt)() +- sdintr() 10773 * | | | | 10774 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 10775 * | +-> scsi_setup_cdb(9F) | 10776 * | | 10777 * +--> scsi_transport(9F) | 10778 * | | 10779 * +----> SCSA ---->+ 10780 * 10781 * 10782 * This code is based upon the following presumptions: 10783 * 10784 * - iostart and iodone functions operate on buf(9S) structures. These 10785 * functions perform the necessary operations on the buf(9S) and pass 10786 * them along to the next function in the chain by using the macros 10787 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 10788 * (for iodone side functions). 10789 * 10790 * - The iostart side functions may sleep. The iodone side functions 10791 * are called under interrupt context and may NOT sleep. Therefore 10792 * iodone side functions also may not call iostart side functions. 10793 * (NOTE: iostart side functions should NOT sleep for memory, as 10794 * this could result in deadlock.) 10795 * 10796 * - An iostart side function may call its corresponding iodone side 10797 * function directly (if necessary). 10798 * 10799 * - In the event of an error, an iostart side function can return a buf(9S) 10800 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 10801 * b_error in the usual way of course). 10802 * 10803 * - The taskq mechanism may be used by the iodone side functions to dispatch 10804 * requests to the iostart side functions. The iostart side functions in 10805 * this case would be called under the context of a taskq thread, so it's 10806 * OK for them to block/sleep/spin in this case. 10807 * 10808 * - iostart side functions may allocate "shadow" buf(9S) structs and 10809 * pass them along to the next function in the chain. The corresponding 10810 * iodone side functions must coalesce the "shadow" bufs and return 10811 * the "original" buf to the next higher layer. 10812 * 10813 * - The b_private field of the buf(9S) struct holds a pointer to 10814 * an sd_xbuf struct, which contains information needed to 10815 * construct the scsi_pkt for the command. 10816 * 10817 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 10818 * layer must acquire & release the SD_MUTEX(un) as needed. 10819 */ 10820 10821 10822 /* 10823 * Create taskq for all targets in the system. This is created at 10824 * _init(9E) and destroyed at _fini(9E). 10825 * 10826 * Note: here we set the minalloc to a reasonably high number to ensure that 10827 * we will have an adequate supply of task entries available at interrupt time. 10828 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 10829 * sd_create_taskq(). Since we do not want to sleep for allocations at 10830 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 10831 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 10832 * requests any one instant in time. 10833 */ 10834 #define SD_TASKQ_NUMTHREADS 8 10835 #define SD_TASKQ_MINALLOC 256 10836 #define SD_TASKQ_MAXALLOC 256 10837 10838 static taskq_t *sd_tq = NULL; 10839 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 10840 10841 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 10842 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 10843 10844 /* 10845 * The following task queue is being created for the write part of 10846 * read-modify-write of non-512 block size devices. 10847 * Limit the number of threads to 1 for now. This number has been chosen 10848 * considering the fact that it applies only to dvd ram drives/MO drives 10849 * currently. Performance for which is not main criteria at this stage. 10850 * Note: It needs to be explored if we can use a single taskq in future 10851 */ 10852 #define SD_WMR_TASKQ_NUMTHREADS 1 10853 static taskq_t *sd_wmr_tq = NULL; 10854 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 10855 10856 /* 10857 * Function: sd_taskq_create 10858 * 10859 * Description: Create taskq thread(s) and preallocate task entries 10860 * 10861 * Return Code: Returns a pointer to the allocated taskq_t. 10862 * 10863 * Context: Can sleep. Requires blockable context. 10864 * 10865 * Notes: - The taskq() facility currently is NOT part of the DDI. 10866 * (definitely NOT recommeded for 3rd-party drivers!) :-) 10867 * - taskq_create() will block for memory, also it will panic 10868 * if it cannot create the requested number of threads. 10869 * - Currently taskq_create() creates threads that cannot be 10870 * swapped. 10871 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 10872 * supply of taskq entries at interrupt time (ie, so that we 10873 * do not have to sleep for memory) 10874 */ 10875 10876 static void 10877 sd_taskq_create(void) 10878 { 10879 char taskq_name[TASKQ_NAMELEN]; 10880 10881 ASSERT(sd_tq == NULL); 10882 ASSERT(sd_wmr_tq == NULL); 10883 10884 (void) snprintf(taskq_name, sizeof (taskq_name), 10885 "%s_drv_taskq", sd_label); 10886 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 10887 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10888 TASKQ_PREPOPULATE)); 10889 10890 (void) snprintf(taskq_name, sizeof (taskq_name), 10891 "%s_rmw_taskq", sd_label); 10892 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 10893 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10894 TASKQ_PREPOPULATE)); 10895 } 10896 10897 10898 /* 10899 * Function: sd_taskq_delete 10900 * 10901 * Description: Complementary cleanup routine for sd_taskq_create(). 10902 * 10903 * Context: Kernel thread context. 10904 */ 10905 10906 static void 10907 sd_taskq_delete(void) 10908 { 10909 ASSERT(sd_tq != NULL); 10910 ASSERT(sd_wmr_tq != NULL); 10911 taskq_destroy(sd_tq); 10912 taskq_destroy(sd_wmr_tq); 10913 sd_tq = NULL; 10914 sd_wmr_tq = NULL; 10915 } 10916 10917 10918 /* 10919 * Function: sdstrategy 10920 * 10921 * Description: Driver's strategy (9E) entry point function. 10922 * 10923 * Arguments: bp - pointer to buf(9S) 10924 * 10925 * Return Code: Always returns zero 10926 * 10927 * Context: Kernel thread context. 10928 */ 10929 10930 static int 10931 sdstrategy(struct buf *bp) 10932 { 10933 struct sd_lun *un; 10934 10935 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10936 if (un == NULL) { 10937 bioerror(bp, EIO); 10938 bp->b_resid = bp->b_bcount; 10939 biodone(bp); 10940 return (0); 10941 } 10942 /* As was done in the past, fail new cmds. if state is dumping. */ 10943 if (un->un_state == SD_STATE_DUMPING) { 10944 bioerror(bp, ENXIO); 10945 bp->b_resid = bp->b_bcount; 10946 biodone(bp); 10947 return (0); 10948 } 10949 10950 ASSERT(!mutex_owned(SD_MUTEX(un))); 10951 10952 /* 10953 * Commands may sneak in while we released the mutex in 10954 * DDI_SUSPEND, we should block new commands. However, old 10955 * commands that are still in the driver at this point should 10956 * still be allowed to drain. 10957 */ 10958 mutex_enter(SD_MUTEX(un)); 10959 /* 10960 * Must wait here if either the device is suspended or 10961 * if it's power level is changing. 10962 */ 10963 while ((un->un_state == SD_STATE_SUSPENDED) || 10964 (un->un_state == SD_STATE_PM_CHANGING)) { 10965 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10966 } 10967 10968 un->un_ncmds_in_driver++; 10969 10970 /* 10971 * atapi: Since we are running the CD for now in PIO mode we need to 10972 * call bp_mapin here to avoid bp_mapin called interrupt context under 10973 * the HBA's init_pkt routine. 10974 */ 10975 if (un->un_f_cfg_is_atapi == TRUE) { 10976 mutex_exit(SD_MUTEX(un)); 10977 bp_mapin(bp); 10978 mutex_enter(SD_MUTEX(un)); 10979 } 10980 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 10981 un->un_ncmds_in_driver); 10982 10983 if (bp->b_flags & B_WRITE) 10984 un->un_f_sync_cache_required = TRUE; 10985 10986 mutex_exit(SD_MUTEX(un)); 10987 10988 /* 10989 * This will (eventually) allocate the sd_xbuf area and 10990 * call sd_xbuf_strategy(). We just want to return the 10991 * result of ddi_xbuf_qstrategy so that we have an opt- 10992 * imized tail call which saves us a stack frame. 10993 */ 10994 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 10995 } 10996 10997 10998 /* 10999 * Function: sd_xbuf_strategy 11000 * 11001 * Description: Function for initiating IO operations via the 11002 * ddi_xbuf_qstrategy() mechanism. 11003 * 11004 * Context: Kernel thread context. 11005 */ 11006 11007 static void 11008 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11009 { 11010 struct sd_lun *un = arg; 11011 11012 ASSERT(bp != NULL); 11013 ASSERT(xp != NULL); 11014 ASSERT(un != NULL); 11015 ASSERT(!mutex_owned(SD_MUTEX(un))); 11016 11017 /* 11018 * Initialize the fields in the xbuf and save a pointer to the 11019 * xbuf in bp->b_private. 11020 */ 11021 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11022 11023 /* Send the buf down the iostart chain */ 11024 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11025 } 11026 11027 11028 /* 11029 * Function: sd_xbuf_init 11030 * 11031 * Description: Prepare the given sd_xbuf struct for use. 11032 * 11033 * Arguments: un - ptr to softstate 11034 * bp - ptr to associated buf(9S) 11035 * xp - ptr to associated sd_xbuf 11036 * chain_type - IO chain type to use: 11037 * SD_CHAIN_NULL 11038 * SD_CHAIN_BUFIO 11039 * SD_CHAIN_USCSI 11040 * SD_CHAIN_DIRECT 11041 * SD_CHAIN_DIRECT_PRIORITY 11042 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11043 * initialization; may be NULL if none. 11044 * 11045 * Context: Kernel thread context 11046 */ 11047 11048 static void 11049 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11050 uchar_t chain_type, void *pktinfop) 11051 { 11052 int index; 11053 11054 ASSERT(un != NULL); 11055 ASSERT(bp != NULL); 11056 ASSERT(xp != NULL); 11057 11058 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11059 bp, chain_type); 11060 11061 xp->xb_un = un; 11062 xp->xb_pktp = NULL; 11063 xp->xb_pktinfo = pktinfop; 11064 xp->xb_private = bp->b_private; 11065 xp->xb_blkno = (daddr_t)bp->b_blkno; 11066 11067 /* 11068 * Set up the iostart and iodone chain indexes in the xbuf, based 11069 * upon the specified chain type to use. 11070 */ 11071 switch (chain_type) { 11072 case SD_CHAIN_NULL: 11073 /* 11074 * Fall thru to just use the values for the buf type, even 11075 * tho for the NULL chain these values will never be used. 11076 */ 11077 /* FALLTHRU */ 11078 case SD_CHAIN_BUFIO: 11079 index = un->un_buf_chain_type; 11080 break; 11081 case SD_CHAIN_USCSI: 11082 index = un->un_uscsi_chain_type; 11083 break; 11084 case SD_CHAIN_DIRECT: 11085 index = un->un_direct_chain_type; 11086 break; 11087 case SD_CHAIN_DIRECT_PRIORITY: 11088 index = un->un_priority_chain_type; 11089 break; 11090 default: 11091 /* We're really broken if we ever get here... */ 11092 panic("sd_xbuf_init: illegal chain type!"); 11093 /*NOTREACHED*/ 11094 } 11095 11096 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 11097 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 11098 11099 /* 11100 * It might be a bit easier to simply bzero the entire xbuf above, 11101 * but it turns out that since we init a fair number of members anyway, 11102 * we save a fair number cycles by doing explicit assignment of zero. 11103 */ 11104 xp->xb_pkt_flags = 0; 11105 xp->xb_dma_resid = 0; 11106 xp->xb_retry_count = 0; 11107 xp->xb_victim_retry_count = 0; 11108 xp->xb_ua_retry_count = 0; 11109 xp->xb_nr_retry_count = 0; 11110 xp->xb_sense_bp = NULL; 11111 xp->xb_sense_status = 0; 11112 xp->xb_sense_state = 0; 11113 xp->xb_sense_resid = 0; 11114 xp->xb_ena = 0; 11115 11116 bp->b_private = xp; 11117 bp->b_flags &= ~(B_DONE | B_ERROR); 11118 bp->b_resid = 0; 11119 bp->av_forw = NULL; 11120 bp->av_back = NULL; 11121 bioerror(bp, 0); 11122 11123 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 11124 } 11125 11126 11127 /* 11128 * Function: sd_uscsi_strategy 11129 * 11130 * Description: Wrapper for calling into the USCSI chain via physio(9F) 11131 * 11132 * Arguments: bp - buf struct ptr 11133 * 11134 * Return Code: Always returns 0 11135 * 11136 * Context: Kernel thread context 11137 */ 11138 11139 static int 11140 sd_uscsi_strategy(struct buf *bp) 11141 { 11142 struct sd_lun *un; 11143 struct sd_uscsi_info *uip; 11144 struct sd_xbuf *xp; 11145 uchar_t chain_type; 11146 uchar_t cmd; 11147 11148 ASSERT(bp != NULL); 11149 11150 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11151 if (un == NULL) { 11152 bioerror(bp, EIO); 11153 bp->b_resid = bp->b_bcount; 11154 biodone(bp); 11155 return (0); 11156 } 11157 11158 ASSERT(!mutex_owned(SD_MUTEX(un))); 11159 11160 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 11161 11162 /* 11163 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 11164 */ 11165 ASSERT(bp->b_private != NULL); 11166 uip = (struct sd_uscsi_info *)bp->b_private; 11167 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0]; 11168 11169 mutex_enter(SD_MUTEX(un)); 11170 /* 11171 * atapi: Since we are running the CD for now in PIO mode we need to 11172 * call bp_mapin here to avoid bp_mapin called interrupt context under 11173 * the HBA's init_pkt routine. 11174 */ 11175 if (un->un_f_cfg_is_atapi == TRUE) { 11176 mutex_exit(SD_MUTEX(un)); 11177 bp_mapin(bp); 11178 mutex_enter(SD_MUTEX(un)); 11179 } 11180 un->un_ncmds_in_driver++; 11181 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 11182 un->un_ncmds_in_driver); 11183 11184 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) && 11185 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1)) 11186 un->un_f_sync_cache_required = TRUE; 11187 11188 mutex_exit(SD_MUTEX(un)); 11189 11190 switch (uip->ui_flags) { 11191 case SD_PATH_DIRECT: 11192 chain_type = SD_CHAIN_DIRECT; 11193 break; 11194 case SD_PATH_DIRECT_PRIORITY: 11195 chain_type = SD_CHAIN_DIRECT_PRIORITY; 11196 break; 11197 default: 11198 chain_type = SD_CHAIN_USCSI; 11199 break; 11200 } 11201 11202 /* 11203 * We may allocate extra buf for external USCSI commands. If the 11204 * application asks for bigger than 20-byte sense data via USCSI, 11205 * SCSA layer will allocate 252 bytes sense buf for that command. 11206 */ 11207 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 11208 SENSE_LENGTH) { 11209 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 11210 MAX_SENSE_LENGTH, KM_SLEEP); 11211 } else { 11212 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 11213 } 11214 11215 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 11216 11217 /* Use the index obtained within xbuf_init */ 11218 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 11219 11220 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 11221 11222 return (0); 11223 } 11224 11225 /* 11226 * Function: sd_send_scsi_cmd 11227 * 11228 * Description: Runs a USCSI command for user (when called thru sdioctl), 11229 * or for the driver 11230 * 11231 * Arguments: dev - the dev_t for the device 11232 * incmd - ptr to a valid uscsi_cmd struct 11233 * flag - bit flag, indicating open settings, 32/64 bit type 11234 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11235 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11236 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11237 * to use the USCSI "direct" chain and bypass the normal 11238 * command waitq. 11239 * 11240 * Return Code: 0 - successful completion of the given command 11241 * EIO - scsi_uscsi_handle_command() failed 11242 * ENXIO - soft state not found for specified dev 11243 * EINVAL 11244 * EFAULT - copyin/copyout error 11245 * return code of scsi_uscsi_handle_command(): 11246 * EIO 11247 * ENXIO 11248 * EACCES 11249 * 11250 * Context: Waits for command to complete. Can sleep. 11251 */ 11252 11253 static int 11254 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 11255 enum uio_seg dataspace, int path_flag) 11256 { 11257 struct sd_lun *un; 11258 sd_ssc_t *ssc; 11259 int rval; 11260 11261 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 11262 if (un == NULL) { 11263 return (ENXIO); 11264 } 11265 11266 /* 11267 * Using sd_ssc_send to handle uscsi cmd 11268 */ 11269 ssc = sd_ssc_init(un); 11270 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag); 11271 sd_ssc_fini(ssc); 11272 11273 return (rval); 11274 } 11275 11276 /* 11277 * Function: sd_ssc_init 11278 * 11279 * Description: Uscsi end-user call this function to initialize necessary 11280 * fields, such as uscsi_cmd and sd_uscsi_info struct. 11281 * 11282 * The return value of sd_send_scsi_cmd will be treated as a 11283 * fault in various conditions. Even it is not Zero, some 11284 * callers may ignore the return value. That is to say, we can 11285 * not make an accurate assessment in sdintr, since if a 11286 * command is failed in sdintr it does not mean the caller of 11287 * sd_send_scsi_cmd will treat it as a real failure. 11288 * 11289 * To avoid printing too many error logs for a failed uscsi 11290 * packet that the caller may not treat it as a failure, the 11291 * sd will keep silent for handling all uscsi commands. 11292 * 11293 * During detach->attach and attach-open, for some types of 11294 * problems, the driver should be providing information about 11295 * the problem encountered. Device use USCSI_SILENT, which 11296 * suppresses all driver information. The result is that no 11297 * information about the problem is available. Being 11298 * completely silent during this time is inappropriate. The 11299 * driver needs a more selective filter than USCSI_SILENT, so 11300 * that information related to faults is provided. 11301 * 11302 * To make the accurate accessment, the caller of 11303 * sd_send_scsi_USCSI_CMD should take the ownership and 11304 * get necessary information to print error messages. 11305 * 11306 * If we want to print necessary info of uscsi command, we need to 11307 * keep the uscsi_cmd and sd_uscsi_info till we can make the 11308 * assessment. We use sd_ssc_init to alloc necessary 11309 * structs for sending an uscsi command and we are also 11310 * responsible for free the memory by calling 11311 * sd_ssc_fini. 11312 * 11313 * The calling secquences will look like: 11314 * sd_ssc_init-> 11315 * 11316 * ... 11317 * 11318 * sd_send_scsi_USCSI_CMD-> 11319 * sd_ssc_send-> - - - sdintr 11320 * ... 11321 * 11322 * if we think the return value should be treated as a 11323 * failure, we make the accessment here and print out 11324 * necessary by retrieving uscsi_cmd and sd_uscsi_info' 11325 * 11326 * ... 11327 * 11328 * sd_ssc_fini 11329 * 11330 * 11331 * Arguments: un - pointer to driver soft state (unit) structure for this 11332 * target. 11333 * 11334 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains 11335 * uscsi_cmd and sd_uscsi_info. 11336 * NULL - if can not alloc memory for sd_ssc_t struct 11337 * 11338 * Context: Kernel Thread. 11339 */ 11340 static sd_ssc_t * 11341 sd_ssc_init(struct sd_lun *un) 11342 { 11343 sd_ssc_t *ssc; 11344 struct uscsi_cmd *ucmdp; 11345 struct sd_uscsi_info *uip; 11346 11347 ASSERT(un != NULL); 11348 ASSERT(!mutex_owned(SD_MUTEX(un))); 11349 11350 /* 11351 * Allocate sd_ssc_t structure 11352 */ 11353 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP); 11354 11355 /* 11356 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine 11357 */ 11358 ucmdp = scsi_uscsi_alloc(); 11359 11360 /* 11361 * Allocate sd_uscsi_info structure 11362 */ 11363 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 11364 11365 ssc->ssc_uscsi_cmd = ucmdp; 11366 ssc->ssc_uscsi_info = uip; 11367 ssc->ssc_un = un; 11368 11369 return (ssc); 11370 } 11371 11372 /* 11373 * Function: sd_ssc_fini 11374 * 11375 * Description: To free sd_ssc_t and it's hanging off 11376 * 11377 * Arguments: ssc - struct pointer of sd_ssc_t. 11378 */ 11379 static void 11380 sd_ssc_fini(sd_ssc_t *ssc) 11381 { 11382 scsi_uscsi_free(ssc->ssc_uscsi_cmd); 11383 11384 if (ssc->ssc_uscsi_info != NULL) { 11385 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info)); 11386 ssc->ssc_uscsi_info = NULL; 11387 } 11388 11389 kmem_free(ssc, sizeof (sd_ssc_t)); 11390 ssc = NULL; 11391 } 11392 11393 /* 11394 * Function: sd_ssc_send 11395 * 11396 * Description: Runs a USCSI command for user when called through sdioctl, 11397 * or for the driver. 11398 * 11399 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11400 * sd_uscsi_info in. 11401 * incmd - ptr to a valid uscsi_cmd struct 11402 * flag - bit flag, indicating open settings, 32/64 bit type 11403 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 11404 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 11405 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 11406 * to use the USCSI "direct" chain and bypass the normal 11407 * command waitq. 11408 * 11409 * Return Code: 0 - successful completion of the given command 11410 * EIO - scsi_uscsi_handle_command() failed 11411 * ENXIO - soft state not found for specified dev 11412 * EINVAL 11413 * EFAULT - copyin/copyout error 11414 * return code of scsi_uscsi_handle_command(): 11415 * EIO 11416 * ENXIO 11417 * EACCES 11418 * 11419 * Context: Kernel Thread; 11420 * Waits for command to complete. Can sleep. 11421 */ 11422 static int 11423 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag, 11424 enum uio_seg dataspace, int path_flag) 11425 { 11426 struct sd_uscsi_info *uip; 11427 struct uscsi_cmd *uscmd; 11428 struct sd_lun *un; 11429 dev_t dev; 11430 11431 int format = 0; 11432 int rval; 11433 11434 ASSERT(ssc != NULL); 11435 un = ssc->ssc_un; 11436 ASSERT(un != NULL); 11437 uscmd = ssc->ssc_uscsi_cmd; 11438 ASSERT(uscmd != NULL); 11439 ASSERT(!mutex_owned(SD_MUTEX(un))); 11440 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 11441 /* 11442 * If enter here, it indicates that the previous uscsi 11443 * command has not been processed by sd_ssc_assessment. 11444 * This is violating our rules of FMA telemetry processing. 11445 * We should print out this message and the last undisposed 11446 * uscsi command. 11447 */ 11448 if (uscmd->uscsi_cdb != NULL) { 11449 SD_INFO(SD_LOG_SDTEST, un, 11450 "sd_ssc_send is missing the alternative " 11451 "sd_ssc_assessment when running command 0x%x.\n", 11452 uscmd->uscsi_cdb[0]); 11453 } 11454 /* 11455 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be 11456 * the initial status. 11457 */ 11458 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11459 } 11460 11461 /* 11462 * We need to make sure sd_ssc_send will have sd_ssc_assessment 11463 * followed to avoid missing FMA telemetries. 11464 */ 11465 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT; 11466 11467 #ifdef SDDEBUG 11468 switch (dataspace) { 11469 case UIO_USERSPACE: 11470 SD_TRACE(SD_LOG_IO, un, 11471 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un); 11472 break; 11473 case UIO_SYSSPACE: 11474 SD_TRACE(SD_LOG_IO, un, 11475 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un); 11476 break; 11477 default: 11478 SD_TRACE(SD_LOG_IO, un, 11479 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un); 11480 break; 11481 } 11482 #endif 11483 11484 rval = scsi_uscsi_copyin((intptr_t)incmd, flag, 11485 SD_ADDRESS(un), &uscmd); 11486 if (rval != 0) { 11487 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 11488 "scsi_uscsi_alloc_and_copyin failed\n", un); 11489 return (rval); 11490 } 11491 11492 if ((uscmd->uscsi_cdb != NULL) && 11493 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 11494 mutex_enter(SD_MUTEX(un)); 11495 un->un_f_format_in_progress = TRUE; 11496 mutex_exit(SD_MUTEX(un)); 11497 format = 1; 11498 } 11499 11500 /* 11501 * Allocate an sd_uscsi_info struct and fill it with the info 11502 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 11503 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 11504 * since we allocate the buf here in this function, we do not 11505 * need to preserve the prior contents of b_private. 11506 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 11507 */ 11508 uip = ssc->ssc_uscsi_info; 11509 uip->ui_flags = path_flag; 11510 uip->ui_cmdp = uscmd; 11511 11512 /* 11513 * Commands sent with priority are intended for error recovery 11514 * situations, and do not have retries performed. 11515 */ 11516 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 11517 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 11518 } 11519 uscmd->uscsi_flags &= ~USCSI_NOINTR; 11520 11521 dev = SD_GET_DEV(un); 11522 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 11523 sd_uscsi_strategy, NULL, uip); 11524 11525 /* 11526 * mark ssc_flags right after handle_cmd to make sure 11527 * the uscsi has been sent 11528 */ 11529 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED; 11530 11531 #ifdef SDDEBUG 11532 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 11533 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 11534 uscmd->uscsi_status, uscmd->uscsi_resid); 11535 if (uscmd->uscsi_bufaddr != NULL) { 11536 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: " 11537 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 11538 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 11539 if (dataspace == UIO_SYSSPACE) { 11540 SD_DUMP_MEMORY(un, SD_LOG_IO, 11541 "data", (uchar_t *)uscmd->uscsi_bufaddr, 11542 uscmd->uscsi_buflen, SD_LOG_HEX); 11543 } 11544 } 11545 #endif 11546 11547 if (format == 1) { 11548 mutex_enter(SD_MUTEX(un)); 11549 un->un_f_format_in_progress = FALSE; 11550 mutex_exit(SD_MUTEX(un)); 11551 } 11552 11553 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd); 11554 11555 return (rval); 11556 } 11557 11558 /* 11559 * Function: sd_ssc_print 11560 * 11561 * Description: Print information available to the console. 11562 * 11563 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11564 * sd_uscsi_info in. 11565 * sd_severity - log level. 11566 * Context: Kernel thread or interrupt context. 11567 */ 11568 static void 11569 sd_ssc_print(sd_ssc_t *ssc, int sd_severity) 11570 { 11571 struct uscsi_cmd *ucmdp; 11572 struct scsi_device *devp; 11573 dev_info_t *devinfo; 11574 uchar_t *sensep; 11575 int senlen; 11576 union scsi_cdb *cdbp; 11577 uchar_t com; 11578 extern struct scsi_key_strings scsi_cmds[]; 11579 11580 ASSERT(ssc != NULL); 11581 ASSERT(ssc->ssc_un != NULL); 11582 11583 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT) 11584 return; 11585 ucmdp = ssc->ssc_uscsi_cmd; 11586 devp = SD_SCSI_DEVP(ssc->ssc_un); 11587 devinfo = SD_DEVINFO(ssc->ssc_un); 11588 ASSERT(ucmdp != NULL); 11589 ASSERT(devp != NULL); 11590 ASSERT(devinfo != NULL); 11591 sensep = (uint8_t *)ucmdp->uscsi_rqbuf; 11592 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid; 11593 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb; 11594 11595 /* In certain case (like DOORLOCK), the cdb could be NULL. */ 11596 if (cdbp == NULL) 11597 return; 11598 /* We don't print log if no sense data available. */ 11599 if (senlen == 0) 11600 sensep = NULL; 11601 com = cdbp->scc_cmd; 11602 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com, 11603 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL); 11604 } 11605 11606 /* 11607 * Function: sd_ssc_assessment 11608 * 11609 * Description: We use this function to make an assessment at the point 11610 * where SD driver may encounter a potential error. 11611 * 11612 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11613 * sd_uscsi_info in. 11614 * tp_assess - a hint of strategy for ereport posting. 11615 * Possible values of tp_assess include: 11616 * SD_FMT_IGNORE - we don't post any ereport because we're 11617 * sure that it is ok to ignore the underlying problems. 11618 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now 11619 * but it might be not correct to ignore the underlying hardware 11620 * error. 11621 * SD_FMT_STATUS_CHECK - we will post an ereport with the 11622 * payload driver-assessment of value "fail" or 11623 * "fatal"(depending on what information we have here). This 11624 * assessment value is usually set when SD driver think there 11625 * is a potential error occurred(Typically, when return value 11626 * of the SCSI command is EIO). 11627 * SD_FMT_STANDARD - we will post an ereport with the payload 11628 * driver-assessment of value "info". This assessment value is 11629 * set when the SCSI command returned successfully and with 11630 * sense data sent back. 11631 * 11632 * Context: Kernel thread. 11633 */ 11634 static void 11635 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess) 11636 { 11637 int senlen = 0; 11638 struct uscsi_cmd *ucmdp = NULL; 11639 struct sd_lun *un; 11640 11641 ASSERT(ssc != NULL); 11642 un = ssc->ssc_un; 11643 ASSERT(un != NULL); 11644 ucmdp = ssc->ssc_uscsi_cmd; 11645 ASSERT(ucmdp != NULL); 11646 11647 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) { 11648 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT; 11649 } else { 11650 /* 11651 * If enter here, it indicates that we have a wrong 11652 * calling sequence of sd_ssc_send and sd_ssc_assessment, 11653 * both of which should be called in a pair in case of 11654 * loss of FMA telemetries. 11655 */ 11656 if (ucmdp->uscsi_cdb != NULL) { 11657 SD_INFO(SD_LOG_SDTEST, un, 11658 "sd_ssc_assessment is missing the " 11659 "alternative sd_ssc_send when running 0x%x, " 11660 "or there are superfluous sd_ssc_assessment for " 11661 "the same sd_ssc_send.\n", 11662 ucmdp->uscsi_cdb[0]); 11663 } 11664 /* 11665 * Set the ssc_flags to the initial value to avoid passing 11666 * down dirty flags to the following sd_ssc_send function. 11667 */ 11668 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11669 return; 11670 } 11671 11672 /* 11673 * Only handle an issued command which is waiting for assessment. 11674 * A command which is not issued will not have 11675 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here. 11676 */ 11677 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) { 11678 sd_ssc_print(ssc, SCSI_ERR_INFO); 11679 return; 11680 } else { 11681 /* 11682 * For an issued command, we should clear this flag in 11683 * order to make the sd_ssc_t structure be used off 11684 * multiple uscsi commands. 11685 */ 11686 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED; 11687 } 11688 11689 /* 11690 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set) 11691 * commands here. And we should clear the ssc_flags before return. 11692 */ 11693 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) { 11694 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11695 return; 11696 } 11697 11698 switch (tp_assess) { 11699 case SD_FMT_IGNORE: 11700 case SD_FMT_IGNORE_COMPROMISE: 11701 break; 11702 case SD_FMT_STATUS_CHECK: 11703 /* 11704 * For a failed command(including the succeeded command 11705 * with invalid data sent back). 11706 */ 11707 sd_ssc_post(ssc, SD_FM_DRV_FATAL); 11708 break; 11709 case SD_FMT_STANDARD: 11710 /* 11711 * Always for the succeeded commands probably with sense 11712 * data sent back. 11713 * Limitation: 11714 * We can only handle a succeeded command with sense 11715 * data sent back when auto-request-sense is enabled. 11716 */ 11717 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen - 11718 ssc->ssc_uscsi_cmd->uscsi_rqresid; 11719 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) && 11720 (un->un_f_arq_enabled == TRUE) && 11721 senlen > 0 && 11722 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) { 11723 sd_ssc_post(ssc, SD_FM_DRV_NOTICE); 11724 } 11725 break; 11726 default: 11727 /* 11728 * Should not have other type of assessment. 11729 */ 11730 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 11731 "sd_ssc_assessment got wrong " 11732 "sd_type_assessment %d.\n", tp_assess); 11733 break; 11734 } 11735 /* 11736 * Clear up the ssc_flags before return. 11737 */ 11738 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11739 } 11740 11741 /* 11742 * Function: sd_ssc_post 11743 * 11744 * Description: 1. read the driver property to get fm-scsi-log flag. 11745 * 2. print log if fm_log_capable is non-zero. 11746 * 3. call sd_ssc_ereport_post to post ereport if possible. 11747 * 11748 * Context: May be called from kernel thread or interrupt context. 11749 */ 11750 static void 11751 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess) 11752 { 11753 struct sd_lun *un; 11754 int sd_severity; 11755 11756 ASSERT(ssc != NULL); 11757 un = ssc->ssc_un; 11758 ASSERT(un != NULL); 11759 11760 /* 11761 * We may enter here from sd_ssc_assessment(for USCSI command) or 11762 * by directly called from sdintr context. 11763 * We don't handle a non-disk drive(CD-ROM, removable media). 11764 * Clear the ssc_flags before return in case we've set 11765 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk 11766 * driver. 11767 */ 11768 if (ISCD(un) || un->un_f_has_removable_media) { 11769 ssc->ssc_flags = SSC_FLAGS_UNKNOWN; 11770 return; 11771 } 11772 11773 switch (sd_assess) { 11774 case SD_FM_DRV_FATAL: 11775 sd_severity = SCSI_ERR_FATAL; 11776 break; 11777 case SD_FM_DRV_RECOVERY: 11778 sd_severity = SCSI_ERR_RECOVERED; 11779 break; 11780 case SD_FM_DRV_RETRY: 11781 sd_severity = SCSI_ERR_RETRYABLE; 11782 break; 11783 case SD_FM_DRV_NOTICE: 11784 sd_severity = SCSI_ERR_INFO; 11785 break; 11786 default: 11787 sd_severity = SCSI_ERR_UNKNOWN; 11788 } 11789 /* print log */ 11790 sd_ssc_print(ssc, sd_severity); 11791 11792 /* always post ereport */ 11793 sd_ssc_ereport_post(ssc, sd_assess); 11794 } 11795 11796 /* 11797 * Function: sd_ssc_set_info 11798 * 11799 * Description: Mark ssc_flags and set ssc_info which would be the 11800 * payload of uderr ereport. This function will cause 11801 * sd_ssc_ereport_post to post uderr ereport only. 11802 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI), 11803 * the function will also call SD_ERROR or scsi_log for a 11804 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device. 11805 * 11806 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and 11807 * sd_uscsi_info in. 11808 * ssc_flags - indicate the sub-category of a uderr. 11809 * comp - this argument is meaningful only when 11810 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible 11811 * values include: 11812 * > 0, SD_ERROR is used with comp as the driver logging 11813 * component; 11814 * = 0, scsi-log is used to log error telemetries; 11815 * < 0, no log available for this telemetry. 11816 * 11817 * Context: Kernel thread or interrupt context 11818 */ 11819 static void 11820 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...) 11821 { 11822 va_list ap; 11823 11824 ASSERT(ssc != NULL); 11825 ASSERT(ssc->ssc_un != NULL); 11826 11827 ssc->ssc_flags |= ssc_flags; 11828 va_start(ap, fmt); 11829 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap); 11830 va_end(ap); 11831 11832 /* 11833 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command 11834 * with invalid data sent back. For non-uscsi command, the 11835 * following code will be bypassed. 11836 */ 11837 if (ssc_flags & SSC_FLAGS_INVALID_DATA) { 11838 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) { 11839 /* 11840 * If the error belong to certain component and we 11841 * do not want it to show up on the console, we 11842 * will use SD_ERROR, otherwise scsi_log is 11843 * preferred. 11844 */ 11845 if (comp > 0) { 11846 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info); 11847 } else if (comp == 0) { 11848 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label, 11849 CE_WARN, ssc->ssc_info); 11850 } 11851 } 11852 } 11853 } 11854 11855 /* 11856 * Function: sd_buf_iodone 11857 * 11858 * Description: Frees the sd_xbuf & returns the buf to its originator. 11859 * 11860 * Context: May be called from interrupt context. 11861 */ 11862 /* ARGSUSED */ 11863 static void 11864 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 11865 { 11866 struct sd_xbuf *xp; 11867 11868 ASSERT(un != NULL); 11869 ASSERT(bp != NULL); 11870 ASSERT(!mutex_owned(SD_MUTEX(un))); 11871 11872 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 11873 11874 xp = SD_GET_XBUF(bp); 11875 ASSERT(xp != NULL); 11876 11877 mutex_enter(SD_MUTEX(un)); 11878 11879 /* 11880 * Grab time when the cmd completed. 11881 * This is used for determining if the system has been 11882 * idle long enough to make it idle to the PM framework. 11883 * This is for lowering the overhead, and therefore improving 11884 * performance per I/O operation. 11885 */ 11886 un->un_pm_idle_time = ddi_get_time(); 11887 11888 un->un_ncmds_in_driver--; 11889 ASSERT(un->un_ncmds_in_driver >= 0); 11890 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 11891 un->un_ncmds_in_driver); 11892 11893 mutex_exit(SD_MUTEX(un)); 11894 11895 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 11896 biodone(bp); /* bp is gone after this */ 11897 11898 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 11899 } 11900 11901 11902 /* 11903 * Function: sd_uscsi_iodone 11904 * 11905 * Description: Frees the sd_xbuf & returns the buf to its originator. 11906 * 11907 * Context: May be called from interrupt context. 11908 */ 11909 /* ARGSUSED */ 11910 static void 11911 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 11912 { 11913 struct sd_xbuf *xp; 11914 11915 ASSERT(un != NULL); 11916 ASSERT(bp != NULL); 11917 11918 xp = SD_GET_XBUF(bp); 11919 ASSERT(xp != NULL); 11920 ASSERT(!mutex_owned(SD_MUTEX(un))); 11921 11922 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 11923 11924 bp->b_private = xp->xb_private; 11925 11926 mutex_enter(SD_MUTEX(un)); 11927 11928 /* 11929 * Grab time when the cmd completed. 11930 * This is used for determining if the system has been 11931 * idle long enough to make it idle to the PM framework. 11932 * This is for lowering the overhead, and therefore improving 11933 * performance per I/O operation. 11934 */ 11935 un->un_pm_idle_time = ddi_get_time(); 11936 11937 un->un_ncmds_in_driver--; 11938 ASSERT(un->un_ncmds_in_driver >= 0); 11939 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 11940 un->un_ncmds_in_driver); 11941 11942 mutex_exit(SD_MUTEX(un)); 11943 11944 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 11945 SENSE_LENGTH) { 11946 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 11947 MAX_SENSE_LENGTH); 11948 } else { 11949 kmem_free(xp, sizeof (struct sd_xbuf)); 11950 } 11951 11952 biodone(bp); 11953 11954 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 11955 } 11956 11957 11958 /* 11959 * Function: sd_mapblockaddr_iostart 11960 * 11961 * Description: Verify request lies within the partition limits for 11962 * the indicated minor device. Issue "overrun" buf if 11963 * request would exceed partition range. Converts 11964 * partition-relative block address to absolute. 11965 * 11966 * Context: Can sleep 11967 * 11968 * Issues: This follows what the old code did, in terms of accessing 11969 * some of the partition info in the unit struct without holding 11970 * the mutext. This is a general issue, if the partition info 11971 * can be altered while IO is in progress... as soon as we send 11972 * a buf, its partitioning can be invalid before it gets to the 11973 * device. Probably the right fix is to move partitioning out 11974 * of the driver entirely. 11975 */ 11976 11977 static void 11978 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 11979 { 11980 diskaddr_t nblocks; /* #blocks in the given partition */ 11981 daddr_t blocknum; /* Block number specified by the buf */ 11982 size_t requested_nblocks; 11983 size_t available_nblocks; 11984 int partition; 11985 diskaddr_t partition_offset; 11986 struct sd_xbuf *xp; 11987 11988 ASSERT(un != NULL); 11989 ASSERT(bp != NULL); 11990 ASSERT(!mutex_owned(SD_MUTEX(un))); 11991 11992 SD_TRACE(SD_LOG_IO_PARTITION, un, 11993 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 11994 11995 xp = SD_GET_XBUF(bp); 11996 ASSERT(xp != NULL); 11997 11998 /* 11999 * If the geometry is not indicated as valid, attempt to access 12000 * the unit & verify the geometry/label. This can be the case for 12001 * removable-media devices, of if the device was opened in 12002 * NDELAY/NONBLOCK mode. 12003 */ 12004 partition = SDPART(bp->b_edev); 12005 12006 if (!SD_IS_VALID_LABEL(un)) { 12007 sd_ssc_t *ssc; 12008 /* 12009 * Initialize sd_ssc_t for internal uscsi commands 12010 * In case of potential porformance issue, we need 12011 * to alloc memory only if there is invalid label 12012 */ 12013 ssc = sd_ssc_init(un); 12014 12015 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) { 12016 /* 12017 * For removable devices it is possible to start an 12018 * I/O without a media by opening the device in nodelay 12019 * mode. Also for writable CDs there can be many 12020 * scenarios where there is no geometry yet but volume 12021 * manager is trying to issue a read() just because 12022 * it can see TOC on the CD. So do not print a message 12023 * for removables. 12024 */ 12025 if (!un->un_f_has_removable_media) { 12026 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12027 "i/o to invalid geometry\n"); 12028 } 12029 bioerror(bp, EIO); 12030 bp->b_resid = bp->b_bcount; 12031 SD_BEGIN_IODONE(index, un, bp); 12032 12033 sd_ssc_fini(ssc); 12034 return; 12035 } 12036 sd_ssc_fini(ssc); 12037 } 12038 12039 nblocks = 0; 12040 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 12041 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 12042 12043 /* 12044 * blocknum is the starting block number of the request. At this 12045 * point it is still relative to the start of the minor device. 12046 */ 12047 blocknum = xp->xb_blkno; 12048 12049 /* 12050 * Legacy: If the starting block number is one past the last block 12051 * in the partition, do not set B_ERROR in the buf. 12052 */ 12053 if (blocknum == nblocks) { 12054 goto error_exit; 12055 } 12056 12057 /* 12058 * Confirm that the first block of the request lies within the 12059 * partition limits. Also the requested number of bytes must be 12060 * a multiple of the system block size. 12061 */ 12062 if ((blocknum < 0) || (blocknum >= nblocks) || 12063 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12064 bp->b_flags |= B_ERROR; 12065 goto error_exit; 12066 } 12067 12068 /* 12069 * If the requsted # blocks exceeds the available # blocks, that 12070 * is an overrun of the partition. 12071 */ 12072 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12073 available_nblocks = (size_t)(nblocks - blocknum); 12074 ASSERT(nblocks >= blocknum); 12075 12076 if (requested_nblocks > available_nblocks) { 12077 /* 12078 * Allocate an "overrun" buf to allow the request to proceed 12079 * for the amount of space available in the partition. The 12080 * amount not transferred will be added into the b_resid 12081 * when the operation is complete. The overrun buf 12082 * replaces the original buf here, and the original buf 12083 * is saved inside the overrun buf, for later use. 12084 */ 12085 size_t resid = SD_SYSBLOCKS2BYTES(un, 12086 (offset_t)(requested_nblocks - available_nblocks)); 12087 size_t count = bp->b_bcount - resid; 12088 /* 12089 * Note: count is an unsigned entity thus it'll NEVER 12090 * be less than 0 so ASSERT the original values are 12091 * correct. 12092 */ 12093 ASSERT(bp->b_bcount >= resid); 12094 12095 bp = sd_bioclone_alloc(bp, count, blocknum, 12096 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12097 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12098 ASSERT(xp != NULL); 12099 } 12100 12101 /* At this point there should be no residual for this buf. */ 12102 ASSERT(bp->b_resid == 0); 12103 12104 /* Convert the block number to an absolute address. */ 12105 xp->xb_blkno += partition_offset; 12106 12107 SD_NEXT_IOSTART(index, un, bp); 12108 12109 SD_TRACE(SD_LOG_IO_PARTITION, un, 12110 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12111 12112 return; 12113 12114 error_exit: 12115 bp->b_resid = bp->b_bcount; 12116 SD_BEGIN_IODONE(index, un, bp); 12117 SD_TRACE(SD_LOG_IO_PARTITION, un, 12118 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12119 } 12120 12121 12122 /* 12123 * Function: sd_mapblockaddr_iodone 12124 * 12125 * Description: Completion-side processing for partition management. 12126 * 12127 * Context: May be called under interrupt context 12128 */ 12129 12130 static void 12131 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12132 { 12133 /* int partition; */ /* Not used, see below. */ 12134 ASSERT(un != NULL); 12135 ASSERT(bp != NULL); 12136 ASSERT(!mutex_owned(SD_MUTEX(un))); 12137 12138 SD_TRACE(SD_LOG_IO_PARTITION, un, 12139 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12140 12141 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12142 /* 12143 * We have an "overrun" buf to deal with... 12144 */ 12145 struct sd_xbuf *xp; 12146 struct buf *obp; /* ptr to the original buf */ 12147 12148 xp = SD_GET_XBUF(bp); 12149 ASSERT(xp != NULL); 12150 12151 /* Retrieve the pointer to the original buf */ 12152 obp = (struct buf *)xp->xb_private; 12153 ASSERT(obp != NULL); 12154 12155 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12156 bioerror(obp, bp->b_error); 12157 12158 sd_bioclone_free(bp); 12159 12160 /* 12161 * Get back the original buf. 12162 * Note that since the restoration of xb_blkno below 12163 * was removed, the sd_xbuf is not needed. 12164 */ 12165 bp = obp; 12166 /* 12167 * xp = SD_GET_XBUF(bp); 12168 * ASSERT(xp != NULL); 12169 */ 12170 } 12171 12172 /* 12173 * Convert sd->xb_blkno back to a minor-device relative value. 12174 * Note: this has been commented out, as it is not needed in the 12175 * current implementation of the driver (ie, since this function 12176 * is at the top of the layering chains, so the info will be 12177 * discarded) and it is in the "hot" IO path. 12178 * 12179 * partition = getminor(bp->b_edev) & SDPART_MASK; 12180 * xp->xb_blkno -= un->un_offset[partition]; 12181 */ 12182 12183 SD_NEXT_IODONE(index, un, bp); 12184 12185 SD_TRACE(SD_LOG_IO_PARTITION, un, 12186 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12187 } 12188 12189 12190 /* 12191 * Function: sd_mapblocksize_iostart 12192 * 12193 * Description: Convert between system block size (un->un_sys_blocksize) 12194 * and target block size (un->un_tgt_blocksize). 12195 * 12196 * Context: Can sleep to allocate resources. 12197 * 12198 * Assumptions: A higher layer has already performed any partition validation, 12199 * and converted the xp->xb_blkno to an absolute value relative 12200 * to the start of the device. 12201 * 12202 * It is also assumed that the higher layer has implemented 12203 * an "overrun" mechanism for the case where the request would 12204 * read/write beyond the end of a partition. In this case we 12205 * assume (and ASSERT) that bp->b_resid == 0. 12206 * 12207 * Note: The implementation for this routine assumes the target 12208 * block size remains constant between allocation and transport. 12209 */ 12210 12211 static void 12212 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12213 { 12214 struct sd_mapblocksize_info *bsp; 12215 struct sd_xbuf *xp; 12216 offset_t first_byte; 12217 daddr_t start_block, end_block; 12218 daddr_t request_bytes; 12219 ushort_t is_aligned = FALSE; 12220 12221 ASSERT(un != NULL); 12222 ASSERT(bp != NULL); 12223 ASSERT(!mutex_owned(SD_MUTEX(un))); 12224 ASSERT(bp->b_resid == 0); 12225 12226 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12227 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12228 12229 /* 12230 * For a non-writable CD, a write request is an error 12231 */ 12232 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12233 (un->un_f_mmc_writable_media == FALSE)) { 12234 bioerror(bp, EIO); 12235 bp->b_resid = bp->b_bcount; 12236 SD_BEGIN_IODONE(index, un, bp); 12237 return; 12238 } 12239 12240 /* 12241 * We do not need a shadow buf if the device is using 12242 * un->un_sys_blocksize as its block size or if bcount == 0. 12243 * In this case there is no layer-private data block allocated. 12244 */ 12245 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12246 (bp->b_bcount == 0)) { 12247 goto done; 12248 } 12249 12250 #if defined(__i386) || defined(__amd64) 12251 /* We do not support non-block-aligned transfers for ROD devices */ 12252 ASSERT(!ISROD(un)); 12253 #endif 12254 12255 xp = SD_GET_XBUF(bp); 12256 ASSERT(xp != NULL); 12257 12258 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12259 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12260 un->un_tgt_blocksize, un->un_sys_blocksize); 12261 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12262 "request start block:0x%x\n", xp->xb_blkno); 12263 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12264 "request len:0x%x\n", bp->b_bcount); 12265 12266 /* 12267 * Allocate the layer-private data area for the mapblocksize layer. 12268 * Layers are allowed to use the xp_private member of the sd_xbuf 12269 * struct to store the pointer to their layer-private data block, but 12270 * each layer also has the responsibility of restoring the prior 12271 * contents of xb_private before returning the buf/xbuf to the 12272 * higher layer that sent it. 12273 * 12274 * Here we save the prior contents of xp->xb_private into the 12275 * bsp->mbs_oprivate field of our layer-private data area. This value 12276 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12277 * the layer-private area and returning the buf/xbuf to the layer 12278 * that sent it. 12279 * 12280 * Note that here we use kmem_zalloc for the allocation as there are 12281 * parts of the mapblocksize code that expect certain fields to be 12282 * zero unless explicitly set to a required value. 12283 */ 12284 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12285 bsp->mbs_oprivate = xp->xb_private; 12286 xp->xb_private = bsp; 12287 12288 /* 12289 * This treats the data on the disk (target) as an array of bytes. 12290 * first_byte is the byte offset, from the beginning of the device, 12291 * to the location of the request. This is converted from a 12292 * un->un_sys_blocksize block address to a byte offset, and then back 12293 * to a block address based upon a un->un_tgt_blocksize block size. 12294 * 12295 * xp->xb_blkno should be absolute upon entry into this function, 12296 * but, but it is based upon partitions that use the "system" 12297 * block size. It must be adjusted to reflect the block size of 12298 * the target. 12299 * 12300 * Note that end_block is actually the block that follows the last 12301 * block of the request, but that's what is needed for the computation. 12302 */ 12303 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12304 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12305 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12306 un->un_tgt_blocksize; 12307 12308 /* request_bytes is rounded up to a multiple of the target block size */ 12309 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12310 12311 /* 12312 * See if the starting address of the request and the request 12313 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12314 * then we do not need to allocate a shadow buf to handle the request. 12315 */ 12316 if (((first_byte % un->un_tgt_blocksize) == 0) && 12317 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12318 is_aligned = TRUE; 12319 } 12320 12321 if ((bp->b_flags & B_READ) == 0) { 12322 /* 12323 * Lock the range for a write operation. An aligned request is 12324 * considered a simple write; otherwise the request must be a 12325 * read-modify-write. 12326 */ 12327 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12328 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12329 } 12330 12331 /* 12332 * Alloc a shadow buf if the request is not aligned. Also, this is 12333 * where the READ command is generated for a read-modify-write. (The 12334 * write phase is deferred until after the read completes.) 12335 */ 12336 if (is_aligned == FALSE) { 12337 12338 struct sd_mapblocksize_info *shadow_bsp; 12339 struct sd_xbuf *shadow_xp; 12340 struct buf *shadow_bp; 12341 12342 /* 12343 * Allocate the shadow buf and it associated xbuf. Note that 12344 * after this call the xb_blkno value in both the original 12345 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12346 * same: absolute relative to the start of the device, and 12347 * adjusted for the target block size. The b_blkno in the 12348 * shadow buf will also be set to this value. We should never 12349 * change b_blkno in the original bp however. 12350 * 12351 * Note also that the shadow buf will always need to be a 12352 * READ command, regardless of whether the incoming command 12353 * is a READ or a WRITE. 12354 */ 12355 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12356 xp->xb_blkno, 12357 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12358 12359 shadow_xp = SD_GET_XBUF(shadow_bp); 12360 12361 /* 12362 * Allocate the layer-private data for the shadow buf. 12363 * (No need to preserve xb_private in the shadow xbuf.) 12364 */ 12365 shadow_xp->xb_private = shadow_bsp = 12366 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12367 12368 /* 12369 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12370 * to figure out where the start of the user data is (based upon 12371 * the system block size) in the data returned by the READ 12372 * command (which will be based upon the target blocksize). Note 12373 * that this is only really used if the request is unaligned. 12374 */ 12375 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12376 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12377 ASSERT((bsp->mbs_copy_offset >= 0) && 12378 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12379 12380 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12381 12382 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12383 12384 /* Transfer the wmap (if any) to the shadow buf */ 12385 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 12386 bsp->mbs_wmp = NULL; 12387 12388 /* 12389 * The shadow buf goes on from here in place of the 12390 * original buf. 12391 */ 12392 shadow_bsp->mbs_orig_bp = bp; 12393 bp = shadow_bp; 12394 } 12395 12396 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12397 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 12398 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12399 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 12400 request_bytes); 12401 SD_INFO(SD_LOG_IO_RMMEDIA, un, 12402 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 12403 12404 done: 12405 SD_NEXT_IOSTART(index, un, bp); 12406 12407 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12408 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 12409 } 12410 12411 12412 /* 12413 * Function: sd_mapblocksize_iodone 12414 * 12415 * Description: Completion side processing for block-size mapping. 12416 * 12417 * Context: May be called under interrupt context 12418 */ 12419 12420 static void 12421 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 12422 { 12423 struct sd_mapblocksize_info *bsp; 12424 struct sd_xbuf *xp; 12425 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 12426 struct buf *orig_bp; /* ptr to the original buf */ 12427 offset_t shadow_end; 12428 offset_t request_end; 12429 offset_t shadow_start; 12430 ssize_t copy_offset; 12431 size_t copy_length; 12432 size_t shortfall; 12433 uint_t is_write; /* TRUE if this bp is a WRITE */ 12434 uint_t has_wmap; /* TRUE is this bp has a wmap */ 12435 12436 ASSERT(un != NULL); 12437 ASSERT(bp != NULL); 12438 12439 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12440 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 12441 12442 /* 12443 * There is no shadow buf or layer-private data if the target is 12444 * using un->un_sys_blocksize as its block size or if bcount == 0. 12445 */ 12446 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12447 (bp->b_bcount == 0)) { 12448 goto exit; 12449 } 12450 12451 xp = SD_GET_XBUF(bp); 12452 ASSERT(xp != NULL); 12453 12454 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 12455 bsp = xp->xb_private; 12456 12457 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 12458 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 12459 12460 if (is_write) { 12461 /* 12462 * For a WRITE request we must free up the block range that 12463 * we have locked up. This holds regardless of whether this is 12464 * an aligned write request or a read-modify-write request. 12465 */ 12466 sd_range_unlock(un, bsp->mbs_wmp); 12467 bsp->mbs_wmp = NULL; 12468 } 12469 12470 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 12471 /* 12472 * An aligned read or write command will have no shadow buf; 12473 * there is not much else to do with it. 12474 */ 12475 goto done; 12476 } 12477 12478 orig_bp = bsp->mbs_orig_bp; 12479 ASSERT(orig_bp != NULL); 12480 orig_xp = SD_GET_XBUF(orig_bp); 12481 ASSERT(orig_xp != NULL); 12482 ASSERT(!mutex_owned(SD_MUTEX(un))); 12483 12484 if (!is_write && has_wmap) { 12485 /* 12486 * A READ with a wmap means this is the READ phase of a 12487 * read-modify-write. If an error occurred on the READ then 12488 * we do not proceed with the WRITE phase or copy any data. 12489 * Just release the write maps and return with an error. 12490 */ 12491 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 12492 orig_bp->b_resid = orig_bp->b_bcount; 12493 bioerror(orig_bp, bp->b_error); 12494 sd_range_unlock(un, bsp->mbs_wmp); 12495 goto freebuf_done; 12496 } 12497 } 12498 12499 /* 12500 * Here is where we set up to copy the data from the shadow buf 12501 * into the space associated with the original buf. 12502 * 12503 * To deal with the conversion between block sizes, these 12504 * computations treat the data as an array of bytes, with the 12505 * first byte (byte 0) corresponding to the first byte in the 12506 * first block on the disk. 12507 */ 12508 12509 /* 12510 * shadow_start and shadow_len indicate the location and size of 12511 * the data returned with the shadow IO request. 12512 */ 12513 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12514 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 12515 12516 /* 12517 * copy_offset gives the offset (in bytes) from the start of the first 12518 * block of the READ request to the beginning of the data. We retrieve 12519 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 12520 * there by sd_mapblockize_iostart(). copy_length gives the amount of 12521 * data to be copied (in bytes). 12522 */ 12523 copy_offset = bsp->mbs_copy_offset; 12524 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 12525 copy_length = orig_bp->b_bcount; 12526 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 12527 12528 /* 12529 * Set up the resid and error fields of orig_bp as appropriate. 12530 */ 12531 if (shadow_end >= request_end) { 12532 /* We got all the requested data; set resid to zero */ 12533 orig_bp->b_resid = 0; 12534 } else { 12535 /* 12536 * We failed to get enough data to fully satisfy the original 12537 * request. Just copy back whatever data we got and set 12538 * up the residual and error code as required. 12539 * 12540 * 'shortfall' is the amount by which the data received with the 12541 * shadow buf has "fallen short" of the requested amount. 12542 */ 12543 shortfall = (size_t)(request_end - shadow_end); 12544 12545 if (shortfall > orig_bp->b_bcount) { 12546 /* 12547 * We did not get enough data to even partially 12548 * fulfill the original request. The residual is 12549 * equal to the amount requested. 12550 */ 12551 orig_bp->b_resid = orig_bp->b_bcount; 12552 } else { 12553 /* 12554 * We did not get all the data that we requested 12555 * from the device, but we will try to return what 12556 * portion we did get. 12557 */ 12558 orig_bp->b_resid = shortfall; 12559 } 12560 ASSERT(copy_length >= orig_bp->b_resid); 12561 copy_length -= orig_bp->b_resid; 12562 } 12563 12564 /* Propagate the error code from the shadow buf to the original buf */ 12565 bioerror(orig_bp, bp->b_error); 12566 12567 if (is_write) { 12568 goto freebuf_done; /* No data copying for a WRITE */ 12569 } 12570 12571 if (has_wmap) { 12572 /* 12573 * This is a READ command from the READ phase of a 12574 * read-modify-write request. We have to copy the data given 12575 * by the user OVER the data returned by the READ command, 12576 * then convert the command from a READ to a WRITE and send 12577 * it back to the target. 12578 */ 12579 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 12580 copy_length); 12581 12582 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 12583 12584 /* 12585 * Dispatch the WRITE command to the taskq thread, which 12586 * will in turn send the command to the target. When the 12587 * WRITE command completes, we (sd_mapblocksize_iodone()) 12588 * will get called again as part of the iodone chain 12589 * processing for it. Note that we will still be dealing 12590 * with the shadow buf at that point. 12591 */ 12592 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 12593 KM_NOSLEEP) != 0) { 12594 /* 12595 * Dispatch was successful so we are done. Return 12596 * without going any higher up the iodone chain. Do 12597 * not free up any layer-private data until after the 12598 * WRITE completes. 12599 */ 12600 return; 12601 } 12602 12603 /* 12604 * Dispatch of the WRITE command failed; set up the error 12605 * condition and send this IO back up the iodone chain. 12606 */ 12607 bioerror(orig_bp, EIO); 12608 orig_bp->b_resid = orig_bp->b_bcount; 12609 12610 } else { 12611 /* 12612 * This is a regular READ request (ie, not a RMW). Copy the 12613 * data from the shadow buf into the original buf. The 12614 * copy_offset compensates for any "misalignment" between the 12615 * shadow buf (with its un->un_tgt_blocksize blocks) and the 12616 * original buf (with its un->un_sys_blocksize blocks). 12617 */ 12618 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 12619 copy_length); 12620 } 12621 12622 freebuf_done: 12623 12624 /* 12625 * At this point we still have both the shadow buf AND the original 12626 * buf to deal with, as well as the layer-private data area in each. 12627 * Local variables are as follows: 12628 * 12629 * bp -- points to shadow buf 12630 * xp -- points to xbuf of shadow buf 12631 * bsp -- points to layer-private data area of shadow buf 12632 * orig_bp -- points to original buf 12633 * 12634 * First free the shadow buf and its associated xbuf, then free the 12635 * layer-private data area from the shadow buf. There is no need to 12636 * restore xb_private in the shadow xbuf. 12637 */ 12638 sd_shadow_buf_free(bp); 12639 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12640 12641 /* 12642 * Now update the local variables to point to the original buf, xbuf, 12643 * and layer-private area. 12644 */ 12645 bp = orig_bp; 12646 xp = SD_GET_XBUF(bp); 12647 ASSERT(xp != NULL); 12648 ASSERT(xp == orig_xp); 12649 bsp = xp->xb_private; 12650 ASSERT(bsp != NULL); 12651 12652 done: 12653 /* 12654 * Restore xb_private to whatever it was set to by the next higher 12655 * layer in the chain, then free the layer-private data area. 12656 */ 12657 xp->xb_private = bsp->mbs_oprivate; 12658 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 12659 12660 exit: 12661 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 12662 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 12663 12664 SD_NEXT_IODONE(index, un, bp); 12665 } 12666 12667 12668 /* 12669 * Function: sd_checksum_iostart 12670 * 12671 * Description: A stub function for a layer that's currently not used. 12672 * For now just a placeholder. 12673 * 12674 * Context: Kernel thread context 12675 */ 12676 12677 static void 12678 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 12679 { 12680 ASSERT(un != NULL); 12681 ASSERT(bp != NULL); 12682 ASSERT(!mutex_owned(SD_MUTEX(un))); 12683 SD_NEXT_IOSTART(index, un, bp); 12684 } 12685 12686 12687 /* 12688 * Function: sd_checksum_iodone 12689 * 12690 * Description: A stub function for a layer that's currently not used. 12691 * For now just a placeholder. 12692 * 12693 * Context: May be called under interrupt context 12694 */ 12695 12696 static void 12697 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 12698 { 12699 ASSERT(un != NULL); 12700 ASSERT(bp != NULL); 12701 ASSERT(!mutex_owned(SD_MUTEX(un))); 12702 SD_NEXT_IODONE(index, un, bp); 12703 } 12704 12705 12706 /* 12707 * Function: sd_checksum_uscsi_iostart 12708 * 12709 * Description: A stub function for a layer that's currently not used. 12710 * For now just a placeholder. 12711 * 12712 * Context: Kernel thread context 12713 */ 12714 12715 static void 12716 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 12717 { 12718 ASSERT(un != NULL); 12719 ASSERT(bp != NULL); 12720 ASSERT(!mutex_owned(SD_MUTEX(un))); 12721 SD_NEXT_IOSTART(index, un, bp); 12722 } 12723 12724 12725 /* 12726 * Function: sd_checksum_uscsi_iodone 12727 * 12728 * Description: A stub function for a layer that's currently not used. 12729 * For now just a placeholder. 12730 * 12731 * Context: May be called under interrupt context 12732 */ 12733 12734 static void 12735 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12736 { 12737 ASSERT(un != NULL); 12738 ASSERT(bp != NULL); 12739 ASSERT(!mutex_owned(SD_MUTEX(un))); 12740 SD_NEXT_IODONE(index, un, bp); 12741 } 12742 12743 12744 /* 12745 * Function: sd_pm_iostart 12746 * 12747 * Description: iostart-side routine for Power mangement. 12748 * 12749 * Context: Kernel thread context 12750 */ 12751 12752 static void 12753 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 12754 { 12755 ASSERT(un != NULL); 12756 ASSERT(bp != NULL); 12757 ASSERT(!mutex_owned(SD_MUTEX(un))); 12758 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12759 12760 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 12761 12762 if (sd_pm_entry(un) != DDI_SUCCESS) { 12763 /* 12764 * Set up to return the failed buf back up the 'iodone' 12765 * side of the calling chain. 12766 */ 12767 bioerror(bp, EIO); 12768 bp->b_resid = bp->b_bcount; 12769 12770 SD_BEGIN_IODONE(index, un, bp); 12771 12772 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12773 return; 12774 } 12775 12776 SD_NEXT_IOSTART(index, un, bp); 12777 12778 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 12779 } 12780 12781 12782 /* 12783 * Function: sd_pm_iodone 12784 * 12785 * Description: iodone-side routine for power mangement. 12786 * 12787 * Context: may be called from interrupt context 12788 */ 12789 12790 static void 12791 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 12792 { 12793 ASSERT(un != NULL); 12794 ASSERT(bp != NULL); 12795 ASSERT(!mutex_owned(&un->un_pm_mutex)); 12796 12797 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 12798 12799 /* 12800 * After attach the following flag is only read, so don't 12801 * take the penalty of acquiring a mutex for it. 12802 */ 12803 if (un->un_f_pm_is_enabled == TRUE) { 12804 sd_pm_exit(un); 12805 } 12806 12807 SD_NEXT_IODONE(index, un, bp); 12808 12809 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 12810 } 12811 12812 12813 /* 12814 * Function: sd_core_iostart 12815 * 12816 * Description: Primary driver function for enqueuing buf(9S) structs from 12817 * the system and initiating IO to the target device 12818 * 12819 * Context: Kernel thread context. Can sleep. 12820 * 12821 * Assumptions: - The given xp->xb_blkno is absolute 12822 * (ie, relative to the start of the device). 12823 * - The IO is to be done using the native blocksize of 12824 * the device, as specified in un->un_tgt_blocksize. 12825 */ 12826 /* ARGSUSED */ 12827 static void 12828 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 12829 { 12830 struct sd_xbuf *xp; 12831 12832 ASSERT(un != NULL); 12833 ASSERT(bp != NULL); 12834 ASSERT(!mutex_owned(SD_MUTEX(un))); 12835 ASSERT(bp->b_resid == 0); 12836 12837 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 12838 12839 xp = SD_GET_XBUF(bp); 12840 ASSERT(xp != NULL); 12841 12842 mutex_enter(SD_MUTEX(un)); 12843 12844 /* 12845 * If we are currently in the failfast state, fail any new IO 12846 * that has B_FAILFAST set, then return. 12847 */ 12848 if ((bp->b_flags & B_FAILFAST) && 12849 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 12850 mutex_exit(SD_MUTEX(un)); 12851 bioerror(bp, EIO); 12852 bp->b_resid = bp->b_bcount; 12853 SD_BEGIN_IODONE(index, un, bp); 12854 return; 12855 } 12856 12857 if (SD_IS_DIRECT_PRIORITY(xp)) { 12858 /* 12859 * Priority command -- transport it immediately. 12860 * 12861 * Note: We may want to assert that USCSI_DIAGNOSE is set, 12862 * because all direct priority commands should be associated 12863 * with error recovery actions which we don't want to retry. 12864 */ 12865 sd_start_cmds(un, bp); 12866 } else { 12867 /* 12868 * Normal command -- add it to the wait queue, then start 12869 * transporting commands from the wait queue. 12870 */ 12871 sd_add_buf_to_waitq(un, bp); 12872 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 12873 sd_start_cmds(un, NULL); 12874 } 12875 12876 mutex_exit(SD_MUTEX(un)); 12877 12878 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 12879 } 12880 12881 12882 /* 12883 * Function: sd_init_cdb_limits 12884 * 12885 * Description: This is to handle scsi_pkt initialization differences 12886 * between the driver platforms. 12887 * 12888 * Legacy behaviors: 12889 * 12890 * If the block number or the sector count exceeds the 12891 * capabilities of a Group 0 command, shift over to a 12892 * Group 1 command. We don't blindly use Group 1 12893 * commands because a) some drives (CDC Wren IVs) get a 12894 * bit confused, and b) there is probably a fair amount 12895 * of speed difference for a target to receive and decode 12896 * a 10 byte command instead of a 6 byte command. 12897 * 12898 * The xfer time difference of 6 vs 10 byte CDBs is 12899 * still significant so this code is still worthwhile. 12900 * 10 byte CDBs are very inefficient with the fas HBA driver 12901 * and older disks. Each CDB byte took 1 usec with some 12902 * popular disks. 12903 * 12904 * Context: Must be called at attach time 12905 */ 12906 12907 static void 12908 sd_init_cdb_limits(struct sd_lun *un) 12909 { 12910 int hba_cdb_limit; 12911 12912 /* 12913 * Use CDB_GROUP1 commands for most devices except for 12914 * parallel SCSI fixed drives in which case we get better 12915 * performance using CDB_GROUP0 commands (where applicable). 12916 */ 12917 un->un_mincdb = SD_CDB_GROUP1; 12918 #if !defined(__fibre) 12919 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 12920 !un->un_f_has_removable_media) { 12921 un->un_mincdb = SD_CDB_GROUP0; 12922 } 12923 #endif 12924 12925 /* 12926 * Try to read the max-cdb-length supported by HBA. 12927 */ 12928 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 12929 if (0 >= un->un_max_hba_cdb) { 12930 un->un_max_hba_cdb = CDB_GROUP4; 12931 hba_cdb_limit = SD_CDB_GROUP4; 12932 } else if (0 < un->un_max_hba_cdb && 12933 un->un_max_hba_cdb < CDB_GROUP1) { 12934 hba_cdb_limit = SD_CDB_GROUP0; 12935 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 12936 un->un_max_hba_cdb < CDB_GROUP5) { 12937 hba_cdb_limit = SD_CDB_GROUP1; 12938 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 12939 un->un_max_hba_cdb < CDB_GROUP4) { 12940 hba_cdb_limit = SD_CDB_GROUP5; 12941 } else { 12942 hba_cdb_limit = SD_CDB_GROUP4; 12943 } 12944 12945 /* 12946 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 12947 * commands for fixed disks unless we are building for a 32 bit 12948 * kernel. 12949 */ 12950 #ifdef _LP64 12951 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 12952 min(hba_cdb_limit, SD_CDB_GROUP4); 12953 #else 12954 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 12955 min(hba_cdb_limit, SD_CDB_GROUP1); 12956 #endif 12957 12958 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 12959 ? sizeof (struct scsi_arq_status) : 1); 12960 un->un_cmd_timeout = (ushort_t)sd_io_time; 12961 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 12962 } 12963 12964 12965 /* 12966 * Function: sd_initpkt_for_buf 12967 * 12968 * Description: Allocate and initialize for transport a scsi_pkt struct, 12969 * based upon the info specified in the given buf struct. 12970 * 12971 * Assumes the xb_blkno in the request is absolute (ie, 12972 * relative to the start of the device (NOT partition!). 12973 * Also assumes that the request is using the native block 12974 * size of the device (as returned by the READ CAPACITY 12975 * command). 12976 * 12977 * Return Code: SD_PKT_ALLOC_SUCCESS 12978 * SD_PKT_ALLOC_FAILURE 12979 * SD_PKT_ALLOC_FAILURE_NO_DMA 12980 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 12981 * 12982 * Context: Kernel thread and may be called from software interrupt context 12983 * as part of a sdrunout callback. This function may not block or 12984 * call routines that block 12985 */ 12986 12987 static int 12988 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 12989 { 12990 struct sd_xbuf *xp; 12991 struct scsi_pkt *pktp = NULL; 12992 struct sd_lun *un; 12993 size_t blockcount; 12994 daddr_t startblock; 12995 int rval; 12996 int cmd_flags; 12997 12998 ASSERT(bp != NULL); 12999 ASSERT(pktpp != NULL); 13000 xp = SD_GET_XBUF(bp); 13001 ASSERT(xp != NULL); 13002 un = SD_GET_UN(bp); 13003 ASSERT(un != NULL); 13004 ASSERT(mutex_owned(SD_MUTEX(un))); 13005 ASSERT(bp->b_resid == 0); 13006 13007 SD_TRACE(SD_LOG_IO_CORE, un, 13008 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13009 13010 mutex_exit(SD_MUTEX(un)); 13011 13012 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13013 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13014 /* 13015 * Already have a scsi_pkt -- just need DMA resources. 13016 * We must recompute the CDB in case the mapping returns 13017 * a nonzero pkt_resid. 13018 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13019 * that is being retried, the unmap/remap of the DMA resouces 13020 * will result in the entire transfer starting over again 13021 * from the very first block. 13022 */ 13023 ASSERT(xp->xb_pktp != NULL); 13024 pktp = xp->xb_pktp; 13025 } else { 13026 pktp = NULL; 13027 } 13028 #endif /* __i386 || __amd64 */ 13029 13030 startblock = xp->xb_blkno; /* Absolute block num. */ 13031 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13032 13033 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13034 13035 /* 13036 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13037 * call scsi_init_pkt, and build the CDB. 13038 */ 13039 rval = sd_setup_rw_pkt(un, &pktp, bp, 13040 cmd_flags, sdrunout, (caddr_t)un, 13041 startblock, blockcount); 13042 13043 if (rval == 0) { 13044 /* 13045 * Success. 13046 * 13047 * If partial DMA is being used and required for this transfer. 13048 * set it up here. 13049 */ 13050 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13051 (pktp->pkt_resid != 0)) { 13052 13053 /* 13054 * Save the CDB length and pkt_resid for the 13055 * next xfer 13056 */ 13057 xp->xb_dma_resid = pktp->pkt_resid; 13058 13059 /* rezero resid */ 13060 pktp->pkt_resid = 0; 13061 13062 } else { 13063 xp->xb_dma_resid = 0; 13064 } 13065 13066 pktp->pkt_flags = un->un_tagflags; 13067 pktp->pkt_time = un->un_cmd_timeout; 13068 pktp->pkt_comp = sdintr; 13069 13070 pktp->pkt_private = bp; 13071 *pktpp = pktp; 13072 13073 SD_TRACE(SD_LOG_IO_CORE, un, 13074 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13075 13076 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13077 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13078 #endif 13079 13080 mutex_enter(SD_MUTEX(un)); 13081 return (SD_PKT_ALLOC_SUCCESS); 13082 13083 } 13084 13085 /* 13086 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13087 * from sd_setup_rw_pkt. 13088 */ 13089 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13090 13091 if (rval == SD_PKT_ALLOC_FAILURE) { 13092 *pktpp = NULL; 13093 /* 13094 * Set the driver state to RWAIT to indicate the driver 13095 * is waiting on resource allocations. The driver will not 13096 * suspend, pm_suspend, or detatch while the state is RWAIT. 13097 */ 13098 mutex_enter(SD_MUTEX(un)); 13099 New_state(un, SD_STATE_RWAIT); 13100 13101 SD_ERROR(SD_LOG_IO_CORE, un, 13102 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13103 13104 if ((bp->b_flags & B_ERROR) != 0) { 13105 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13106 } 13107 return (SD_PKT_ALLOC_FAILURE); 13108 } else { 13109 /* 13110 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13111 * 13112 * This should never happen. Maybe someone messed with the 13113 * kernel's minphys? 13114 */ 13115 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13116 "Request rejected: too large for CDB: " 13117 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13118 SD_ERROR(SD_LOG_IO_CORE, un, 13119 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13120 mutex_enter(SD_MUTEX(un)); 13121 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13122 13123 } 13124 } 13125 13126 13127 /* 13128 * Function: sd_destroypkt_for_buf 13129 * 13130 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13131 * 13132 * Context: Kernel thread or interrupt context 13133 */ 13134 13135 static void 13136 sd_destroypkt_for_buf(struct buf *bp) 13137 { 13138 ASSERT(bp != NULL); 13139 ASSERT(SD_GET_UN(bp) != NULL); 13140 13141 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13142 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13143 13144 ASSERT(SD_GET_PKTP(bp) != NULL); 13145 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13146 13147 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13148 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13149 } 13150 13151 /* 13152 * Function: sd_setup_rw_pkt 13153 * 13154 * Description: Determines appropriate CDB group for the requested LBA 13155 * and transfer length, calls scsi_init_pkt, and builds 13156 * the CDB. Do not use for partial DMA transfers except 13157 * for the initial transfer since the CDB size must 13158 * remain constant. 13159 * 13160 * Context: Kernel thread and may be called from software interrupt 13161 * context as part of a sdrunout callback. This function may not 13162 * block or call routines that block 13163 */ 13164 13165 13166 int 13167 sd_setup_rw_pkt(struct sd_lun *un, 13168 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13169 int (*callback)(caddr_t), caddr_t callback_arg, 13170 diskaddr_t lba, uint32_t blockcount) 13171 { 13172 struct scsi_pkt *return_pktp; 13173 union scsi_cdb *cdbp; 13174 struct sd_cdbinfo *cp = NULL; 13175 int i; 13176 13177 /* 13178 * See which size CDB to use, based upon the request. 13179 */ 13180 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13181 13182 /* 13183 * Check lba and block count against sd_cdbtab limits. 13184 * In the partial DMA case, we have to use the same size 13185 * CDB for all the transfers. Check lba + blockcount 13186 * against the max LBA so we know that segment of the 13187 * transfer can use the CDB we select. 13188 */ 13189 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13190 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13191 13192 /* 13193 * The command will fit into the CDB type 13194 * specified by sd_cdbtab[i]. 13195 */ 13196 cp = sd_cdbtab + i; 13197 13198 /* 13199 * Call scsi_init_pkt so we can fill in the 13200 * CDB. 13201 */ 13202 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13203 bp, cp->sc_grpcode, un->un_status_len, 0, 13204 flags, callback, callback_arg); 13205 13206 if (return_pktp != NULL) { 13207 13208 /* 13209 * Return new value of pkt 13210 */ 13211 *pktpp = return_pktp; 13212 13213 /* 13214 * To be safe, zero the CDB insuring there is 13215 * no leftover data from a previous command. 13216 */ 13217 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13218 13219 /* 13220 * Handle partial DMA mapping 13221 */ 13222 if (return_pktp->pkt_resid != 0) { 13223 13224 /* 13225 * Not going to xfer as many blocks as 13226 * originally expected 13227 */ 13228 blockcount -= 13229 SD_BYTES2TGTBLOCKS(un, 13230 return_pktp->pkt_resid); 13231 } 13232 13233 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13234 13235 /* 13236 * Set command byte based on the CDB 13237 * type we matched. 13238 */ 13239 cdbp->scc_cmd = cp->sc_grpmask | 13240 ((bp->b_flags & B_READ) ? 13241 SCMD_READ : SCMD_WRITE); 13242 13243 SD_FILL_SCSI1_LUN(un, return_pktp); 13244 13245 /* 13246 * Fill in LBA and length 13247 */ 13248 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13249 (cp->sc_grpcode == CDB_GROUP4) || 13250 (cp->sc_grpcode == CDB_GROUP0) || 13251 (cp->sc_grpcode == CDB_GROUP5)); 13252 13253 if (cp->sc_grpcode == CDB_GROUP1) { 13254 FORMG1ADDR(cdbp, lba); 13255 FORMG1COUNT(cdbp, blockcount); 13256 return (0); 13257 } else if (cp->sc_grpcode == CDB_GROUP4) { 13258 FORMG4LONGADDR(cdbp, lba); 13259 FORMG4COUNT(cdbp, blockcount); 13260 return (0); 13261 } else if (cp->sc_grpcode == CDB_GROUP0) { 13262 FORMG0ADDR(cdbp, lba); 13263 FORMG0COUNT(cdbp, blockcount); 13264 return (0); 13265 } else if (cp->sc_grpcode == CDB_GROUP5) { 13266 FORMG5ADDR(cdbp, lba); 13267 FORMG5COUNT(cdbp, blockcount); 13268 return (0); 13269 } 13270 13271 /* 13272 * It should be impossible to not match one 13273 * of the CDB types above, so we should never 13274 * reach this point. Set the CDB command byte 13275 * to test-unit-ready to avoid writing 13276 * to somewhere we don't intend. 13277 */ 13278 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13279 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13280 } else { 13281 /* 13282 * Couldn't get scsi_pkt 13283 */ 13284 return (SD_PKT_ALLOC_FAILURE); 13285 } 13286 } 13287 } 13288 13289 /* 13290 * None of the available CDB types were suitable. This really 13291 * should never happen: on a 64 bit system we support 13292 * READ16/WRITE16 which will hold an entire 64 bit disk address 13293 * and on a 32 bit system we will refuse to bind to a device 13294 * larger than 2TB so addresses will never be larger than 32 bits. 13295 */ 13296 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13297 } 13298 13299 /* 13300 * Function: sd_setup_next_rw_pkt 13301 * 13302 * Description: Setup packet for partial DMA transfers, except for the 13303 * initial transfer. sd_setup_rw_pkt should be used for 13304 * the initial transfer. 13305 * 13306 * Context: Kernel thread and may be called from interrupt context. 13307 */ 13308 13309 int 13310 sd_setup_next_rw_pkt(struct sd_lun *un, 13311 struct scsi_pkt *pktp, struct buf *bp, 13312 diskaddr_t lba, uint32_t blockcount) 13313 { 13314 uchar_t com; 13315 union scsi_cdb *cdbp; 13316 uchar_t cdb_group_id; 13317 13318 ASSERT(pktp != NULL); 13319 ASSERT(pktp->pkt_cdbp != NULL); 13320 13321 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13322 com = cdbp->scc_cmd; 13323 cdb_group_id = CDB_GROUPID(com); 13324 13325 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13326 (cdb_group_id == CDB_GROUPID_1) || 13327 (cdb_group_id == CDB_GROUPID_4) || 13328 (cdb_group_id == CDB_GROUPID_5)); 13329 13330 /* 13331 * Move pkt to the next portion of the xfer. 13332 * func is NULL_FUNC so we do not have to release 13333 * the disk mutex here. 13334 */ 13335 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13336 NULL_FUNC, NULL) == pktp) { 13337 /* Success. Handle partial DMA */ 13338 if (pktp->pkt_resid != 0) { 13339 blockcount -= 13340 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13341 } 13342 13343 cdbp->scc_cmd = com; 13344 SD_FILL_SCSI1_LUN(un, pktp); 13345 if (cdb_group_id == CDB_GROUPID_1) { 13346 FORMG1ADDR(cdbp, lba); 13347 FORMG1COUNT(cdbp, blockcount); 13348 return (0); 13349 } else if (cdb_group_id == CDB_GROUPID_4) { 13350 FORMG4LONGADDR(cdbp, lba); 13351 FORMG4COUNT(cdbp, blockcount); 13352 return (0); 13353 } else if (cdb_group_id == CDB_GROUPID_0) { 13354 FORMG0ADDR(cdbp, lba); 13355 FORMG0COUNT(cdbp, blockcount); 13356 return (0); 13357 } else if (cdb_group_id == CDB_GROUPID_5) { 13358 FORMG5ADDR(cdbp, lba); 13359 FORMG5COUNT(cdbp, blockcount); 13360 return (0); 13361 } 13362 13363 /* Unreachable */ 13364 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13365 } 13366 13367 /* 13368 * Error setting up next portion of cmd transfer. 13369 * Something is definitely very wrong and this 13370 * should not happen. 13371 */ 13372 return (SD_PKT_ALLOC_FAILURE); 13373 } 13374 13375 /* 13376 * Function: sd_initpkt_for_uscsi 13377 * 13378 * Description: Allocate and initialize for transport a scsi_pkt struct, 13379 * based upon the info specified in the given uscsi_cmd struct. 13380 * 13381 * Return Code: SD_PKT_ALLOC_SUCCESS 13382 * SD_PKT_ALLOC_FAILURE 13383 * SD_PKT_ALLOC_FAILURE_NO_DMA 13384 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13385 * 13386 * Context: Kernel thread and may be called from software interrupt context 13387 * as part of a sdrunout callback. This function may not block or 13388 * call routines that block 13389 */ 13390 13391 static int 13392 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 13393 { 13394 struct uscsi_cmd *uscmd; 13395 struct sd_xbuf *xp; 13396 struct scsi_pkt *pktp; 13397 struct sd_lun *un; 13398 uint32_t flags = 0; 13399 13400 ASSERT(bp != NULL); 13401 ASSERT(pktpp != NULL); 13402 xp = SD_GET_XBUF(bp); 13403 ASSERT(xp != NULL); 13404 un = SD_GET_UN(bp); 13405 ASSERT(un != NULL); 13406 ASSERT(mutex_owned(SD_MUTEX(un))); 13407 13408 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13409 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13410 ASSERT(uscmd != NULL); 13411 13412 SD_TRACE(SD_LOG_IO_CORE, un, 13413 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 13414 13415 /* 13416 * Allocate the scsi_pkt for the command. 13417 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 13418 * during scsi_init_pkt time and will continue to use the 13419 * same path as long as the same scsi_pkt is used without 13420 * intervening scsi_dma_free(). Since uscsi command does 13421 * not call scsi_dmafree() before retry failed command, it 13422 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 13423 * set such that scsi_vhci can use other available path for 13424 * retry. Besides, ucsci command does not allow DMA breakup, 13425 * so there is no need to set PKT_DMA_PARTIAL flag. 13426 */ 13427 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 13428 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13429 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13430 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 13431 - sizeof (struct scsi_extended_sense)), 0, 13432 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 13433 sdrunout, (caddr_t)un); 13434 } else { 13435 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 13436 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 13437 sizeof (struct scsi_arq_status), 0, 13438 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 13439 sdrunout, (caddr_t)un); 13440 } 13441 13442 if (pktp == NULL) { 13443 *pktpp = NULL; 13444 /* 13445 * Set the driver state to RWAIT to indicate the driver 13446 * is waiting on resource allocations. The driver will not 13447 * suspend, pm_suspend, or detatch while the state is RWAIT. 13448 */ 13449 New_state(un, SD_STATE_RWAIT); 13450 13451 SD_ERROR(SD_LOG_IO_CORE, un, 13452 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 13453 13454 if ((bp->b_flags & B_ERROR) != 0) { 13455 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13456 } 13457 return (SD_PKT_ALLOC_FAILURE); 13458 } 13459 13460 /* 13461 * We do not do DMA breakup for USCSI commands, so return failure 13462 * here if all the needed DMA resources were not allocated. 13463 */ 13464 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 13465 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 13466 scsi_destroy_pkt(pktp); 13467 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 13468 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 13469 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 13470 } 13471 13472 /* Init the cdb from the given uscsi struct */ 13473 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 13474 uscmd->uscsi_cdb[0], 0, 0, 0); 13475 13476 SD_FILL_SCSI1_LUN(un, pktp); 13477 13478 /* 13479 * Set up the optional USCSI flags. See the uscsi (7I) man page 13480 * for listing of the supported flags. 13481 */ 13482 13483 if (uscmd->uscsi_flags & USCSI_SILENT) { 13484 flags |= FLAG_SILENT; 13485 } 13486 13487 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 13488 flags |= FLAG_DIAGNOSE; 13489 } 13490 13491 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 13492 flags |= FLAG_ISOLATE; 13493 } 13494 13495 if (un->un_f_is_fibre == FALSE) { 13496 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 13497 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 13498 } 13499 } 13500 13501 /* 13502 * Set the pkt flags here so we save time later. 13503 * Note: These flags are NOT in the uscsi man page!!! 13504 */ 13505 if (uscmd->uscsi_flags & USCSI_HEAD) { 13506 flags |= FLAG_HEAD; 13507 } 13508 13509 if (uscmd->uscsi_flags & USCSI_NOINTR) { 13510 flags |= FLAG_NOINTR; 13511 } 13512 13513 /* 13514 * For tagged queueing, things get a bit complicated. 13515 * Check first for head of queue and last for ordered queue. 13516 * If neither head nor order, use the default driver tag flags. 13517 */ 13518 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 13519 if (uscmd->uscsi_flags & USCSI_HTAG) { 13520 flags |= FLAG_HTAG; 13521 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 13522 flags |= FLAG_OTAG; 13523 } else { 13524 flags |= un->un_tagflags & FLAG_TAGMASK; 13525 } 13526 } 13527 13528 if (uscmd->uscsi_flags & USCSI_NODISCON) { 13529 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 13530 } 13531 13532 pktp->pkt_flags = flags; 13533 13534 /* Transfer uscsi information to scsi_pkt */ 13535 (void) scsi_uscsi_pktinit(uscmd, pktp); 13536 13537 /* Copy the caller's CDB into the pkt... */ 13538 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 13539 13540 if (uscmd->uscsi_timeout == 0) { 13541 pktp->pkt_time = un->un_uscsi_timeout; 13542 } else { 13543 pktp->pkt_time = uscmd->uscsi_timeout; 13544 } 13545 13546 /* need it later to identify USCSI request in sdintr */ 13547 xp->xb_pkt_flags |= SD_XB_USCSICMD; 13548 13549 xp->xb_sense_resid = uscmd->uscsi_rqresid; 13550 13551 pktp->pkt_private = bp; 13552 pktp->pkt_comp = sdintr; 13553 *pktpp = pktp; 13554 13555 SD_TRACE(SD_LOG_IO_CORE, un, 13556 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 13557 13558 return (SD_PKT_ALLOC_SUCCESS); 13559 } 13560 13561 13562 /* 13563 * Function: sd_destroypkt_for_uscsi 13564 * 13565 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 13566 * IOs.. Also saves relevant info into the associated uscsi_cmd 13567 * struct. 13568 * 13569 * Context: May be called under interrupt context 13570 */ 13571 13572 static void 13573 sd_destroypkt_for_uscsi(struct buf *bp) 13574 { 13575 struct uscsi_cmd *uscmd; 13576 struct sd_xbuf *xp; 13577 struct scsi_pkt *pktp; 13578 struct sd_lun *un; 13579 struct sd_uscsi_info *suip; 13580 13581 ASSERT(bp != NULL); 13582 xp = SD_GET_XBUF(bp); 13583 ASSERT(xp != NULL); 13584 un = SD_GET_UN(bp); 13585 ASSERT(un != NULL); 13586 ASSERT(!mutex_owned(SD_MUTEX(un))); 13587 pktp = SD_GET_PKTP(bp); 13588 ASSERT(pktp != NULL); 13589 13590 SD_TRACE(SD_LOG_IO_CORE, un, 13591 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 13592 13593 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 13594 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 13595 ASSERT(uscmd != NULL); 13596 13597 /* Save the status and the residual into the uscsi_cmd struct */ 13598 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 13599 uscmd->uscsi_resid = bp->b_resid; 13600 13601 /* Transfer scsi_pkt information to uscsi */ 13602 (void) scsi_uscsi_pktfini(pktp, uscmd); 13603 13604 /* 13605 * If enabled, copy any saved sense data into the area specified 13606 * by the uscsi command. 13607 */ 13608 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 13609 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 13610 /* 13611 * Note: uscmd->uscsi_rqbuf should always point to a buffer 13612 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 13613 */ 13614 uscmd->uscsi_rqstatus = xp->xb_sense_status; 13615 uscmd->uscsi_rqresid = xp->xb_sense_resid; 13616 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 13617 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 13618 MAX_SENSE_LENGTH); 13619 } else { 13620 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 13621 SENSE_LENGTH); 13622 } 13623 } 13624 /* 13625 * The following assignments are for SCSI FMA. 13626 */ 13627 ASSERT(xp->xb_private != NULL); 13628 suip = (struct sd_uscsi_info *)xp->xb_private; 13629 suip->ui_pkt_reason = pktp->pkt_reason; 13630 suip->ui_pkt_state = pktp->pkt_state; 13631 suip->ui_pkt_statistics = pktp->pkt_statistics; 13632 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 13633 13634 /* We are done with the scsi_pkt; free it now */ 13635 ASSERT(SD_GET_PKTP(bp) != NULL); 13636 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13637 13638 SD_TRACE(SD_LOG_IO_CORE, un, 13639 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 13640 } 13641 13642 13643 /* 13644 * Function: sd_bioclone_alloc 13645 * 13646 * Description: Allocate a buf(9S) and init it as per the given buf 13647 * and the various arguments. The associated sd_xbuf 13648 * struct is (nearly) duplicated. The struct buf *bp 13649 * argument is saved in new_xp->xb_private. 13650 * 13651 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13652 * datalen - size of data area for the shadow bp 13653 * blkno - starting LBA 13654 * func - function pointer for b_iodone in the shadow buf. (May 13655 * be NULL if none.) 13656 * 13657 * Return Code: Pointer to allocates buf(9S) struct 13658 * 13659 * Context: Can sleep. 13660 */ 13661 13662 static struct buf * 13663 sd_bioclone_alloc(struct buf *bp, size_t datalen, 13664 daddr_t blkno, int (*func)(struct buf *)) 13665 { 13666 struct sd_lun *un; 13667 struct sd_xbuf *xp; 13668 struct sd_xbuf *new_xp; 13669 struct buf *new_bp; 13670 13671 ASSERT(bp != NULL); 13672 xp = SD_GET_XBUF(bp); 13673 ASSERT(xp != NULL); 13674 un = SD_GET_UN(bp); 13675 ASSERT(un != NULL); 13676 ASSERT(!mutex_owned(SD_MUTEX(un))); 13677 13678 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 13679 NULL, KM_SLEEP); 13680 13681 new_bp->b_lblkno = blkno; 13682 13683 /* 13684 * Allocate an xbuf for the shadow bp and copy the contents of the 13685 * original xbuf into it. 13686 */ 13687 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13688 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13689 13690 /* 13691 * The given bp is automatically saved in the xb_private member 13692 * of the new xbuf. Callers are allowed to depend on this. 13693 */ 13694 new_xp->xb_private = bp; 13695 13696 new_bp->b_private = new_xp; 13697 13698 return (new_bp); 13699 } 13700 13701 /* 13702 * Function: sd_shadow_buf_alloc 13703 * 13704 * Description: Allocate a buf(9S) and init it as per the given buf 13705 * and the various arguments. The associated sd_xbuf 13706 * struct is (nearly) duplicated. The struct buf *bp 13707 * argument is saved in new_xp->xb_private. 13708 * 13709 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 13710 * datalen - size of data area for the shadow bp 13711 * bflags - B_READ or B_WRITE (pseudo flag) 13712 * blkno - starting LBA 13713 * func - function pointer for b_iodone in the shadow buf. (May 13714 * be NULL if none.) 13715 * 13716 * Return Code: Pointer to allocates buf(9S) struct 13717 * 13718 * Context: Can sleep. 13719 */ 13720 13721 static struct buf * 13722 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 13723 daddr_t blkno, int (*func)(struct buf *)) 13724 { 13725 struct sd_lun *un; 13726 struct sd_xbuf *xp; 13727 struct sd_xbuf *new_xp; 13728 struct buf *new_bp; 13729 13730 ASSERT(bp != NULL); 13731 xp = SD_GET_XBUF(bp); 13732 ASSERT(xp != NULL); 13733 un = SD_GET_UN(bp); 13734 ASSERT(un != NULL); 13735 ASSERT(!mutex_owned(SD_MUTEX(un))); 13736 13737 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 13738 bp_mapin(bp); 13739 } 13740 13741 bflags &= (B_READ | B_WRITE); 13742 #if defined(__i386) || defined(__amd64) 13743 new_bp = getrbuf(KM_SLEEP); 13744 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 13745 new_bp->b_bcount = datalen; 13746 new_bp->b_flags = bflags | 13747 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 13748 #else 13749 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 13750 datalen, bflags, SLEEP_FUNC, NULL); 13751 #endif 13752 new_bp->av_forw = NULL; 13753 new_bp->av_back = NULL; 13754 new_bp->b_dev = bp->b_dev; 13755 new_bp->b_blkno = blkno; 13756 new_bp->b_iodone = func; 13757 new_bp->b_edev = bp->b_edev; 13758 new_bp->b_resid = 0; 13759 13760 /* We need to preserve the B_FAILFAST flag */ 13761 if (bp->b_flags & B_FAILFAST) { 13762 new_bp->b_flags |= B_FAILFAST; 13763 } 13764 13765 /* 13766 * Allocate an xbuf for the shadow bp and copy the contents of the 13767 * original xbuf into it. 13768 */ 13769 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 13770 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 13771 13772 /* Need later to copy data between the shadow buf & original buf! */ 13773 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 13774 13775 /* 13776 * The given bp is automatically saved in the xb_private member 13777 * of the new xbuf. Callers are allowed to depend on this. 13778 */ 13779 new_xp->xb_private = bp; 13780 13781 new_bp->b_private = new_xp; 13782 13783 return (new_bp); 13784 } 13785 13786 /* 13787 * Function: sd_bioclone_free 13788 * 13789 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 13790 * in the larger than partition operation. 13791 * 13792 * Context: May be called under interrupt context 13793 */ 13794 13795 static void 13796 sd_bioclone_free(struct buf *bp) 13797 { 13798 struct sd_xbuf *xp; 13799 13800 ASSERT(bp != NULL); 13801 xp = SD_GET_XBUF(bp); 13802 ASSERT(xp != NULL); 13803 13804 /* 13805 * Call bp_mapout() before freeing the buf, in case a lower 13806 * layer or HBA had done a bp_mapin(). we must do this here 13807 * as we are the "originator" of the shadow buf. 13808 */ 13809 bp_mapout(bp); 13810 13811 /* 13812 * Null out b_iodone before freeing the bp, to ensure that the driver 13813 * never gets confused by a stale value in this field. (Just a little 13814 * extra defensiveness here.) 13815 */ 13816 bp->b_iodone = NULL; 13817 13818 freerbuf(bp); 13819 13820 kmem_free(xp, sizeof (struct sd_xbuf)); 13821 } 13822 13823 /* 13824 * Function: sd_shadow_buf_free 13825 * 13826 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 13827 * 13828 * Context: May be called under interrupt context 13829 */ 13830 13831 static void 13832 sd_shadow_buf_free(struct buf *bp) 13833 { 13834 struct sd_xbuf *xp; 13835 13836 ASSERT(bp != NULL); 13837 xp = SD_GET_XBUF(bp); 13838 ASSERT(xp != NULL); 13839 13840 #if defined(__sparc) 13841 /* 13842 * Call bp_mapout() before freeing the buf, in case a lower 13843 * layer or HBA had done a bp_mapin(). we must do this here 13844 * as we are the "originator" of the shadow buf. 13845 */ 13846 bp_mapout(bp); 13847 #endif 13848 13849 /* 13850 * Null out b_iodone before freeing the bp, to ensure that the driver 13851 * never gets confused by a stale value in this field. (Just a little 13852 * extra defensiveness here.) 13853 */ 13854 bp->b_iodone = NULL; 13855 13856 #if defined(__i386) || defined(__amd64) 13857 kmem_free(bp->b_un.b_addr, bp->b_bcount); 13858 freerbuf(bp); 13859 #else 13860 scsi_free_consistent_buf(bp); 13861 #endif 13862 13863 kmem_free(xp, sizeof (struct sd_xbuf)); 13864 } 13865 13866 13867 /* 13868 * Function: sd_print_transport_rejected_message 13869 * 13870 * Description: This implements the ludicrously complex rules for printing 13871 * a "transport rejected" message. This is to address the 13872 * specific problem of having a flood of this error message 13873 * produced when a failover occurs. 13874 * 13875 * Context: Any. 13876 */ 13877 13878 static void 13879 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 13880 int code) 13881 { 13882 ASSERT(un != NULL); 13883 ASSERT(mutex_owned(SD_MUTEX(un))); 13884 ASSERT(xp != NULL); 13885 13886 /* 13887 * Print the "transport rejected" message under the following 13888 * conditions: 13889 * 13890 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 13891 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 13892 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 13893 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 13894 * scsi_transport(9F) (which indicates that the target might have 13895 * gone off-line). This uses the un->un_tran_fatal_count 13896 * count, which is incremented whenever a TRAN_FATAL_ERROR is 13897 * received, and reset to zero whenver a TRAN_ACCEPT is returned 13898 * from scsi_transport(). 13899 * 13900 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 13901 * the preceeding cases in order for the message to be printed. 13902 */ 13903 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) && 13904 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) { 13905 if ((sd_level_mask & SD_LOGMASK_DIAG) || 13906 (code != TRAN_FATAL_ERROR) || 13907 (un->un_tran_fatal_count == 1)) { 13908 switch (code) { 13909 case TRAN_BADPKT: 13910 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13911 "transport rejected bad packet\n"); 13912 break; 13913 case TRAN_FATAL_ERROR: 13914 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13915 "transport rejected fatal error\n"); 13916 break; 13917 default: 13918 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13919 "transport rejected (%d)\n", code); 13920 break; 13921 } 13922 } 13923 } 13924 } 13925 13926 13927 /* 13928 * Function: sd_add_buf_to_waitq 13929 * 13930 * Description: Add the given buf(9S) struct to the wait queue for the 13931 * instance. If sorting is enabled, then the buf is added 13932 * to the queue via an elevator sort algorithm (a la 13933 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 13934 * If sorting is not enabled, then the buf is just added 13935 * to the end of the wait queue. 13936 * 13937 * Return Code: void 13938 * 13939 * Context: Does not sleep/block, therefore technically can be called 13940 * from any context. However if sorting is enabled then the 13941 * execution time is indeterminate, and may take long if 13942 * the wait queue grows large. 13943 */ 13944 13945 static void 13946 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 13947 { 13948 struct buf *ap; 13949 13950 ASSERT(bp != NULL); 13951 ASSERT(un != NULL); 13952 ASSERT(mutex_owned(SD_MUTEX(un))); 13953 13954 /* If the queue is empty, add the buf as the only entry & return. */ 13955 if (un->un_waitq_headp == NULL) { 13956 ASSERT(un->un_waitq_tailp == NULL); 13957 un->un_waitq_headp = un->un_waitq_tailp = bp; 13958 bp->av_forw = NULL; 13959 return; 13960 } 13961 13962 ASSERT(un->un_waitq_tailp != NULL); 13963 13964 /* 13965 * If sorting is disabled, just add the buf to the tail end of 13966 * the wait queue and return. 13967 */ 13968 if (un->un_f_disksort_disabled) { 13969 un->un_waitq_tailp->av_forw = bp; 13970 un->un_waitq_tailp = bp; 13971 bp->av_forw = NULL; 13972 return; 13973 } 13974 13975 /* 13976 * Sort thru the list of requests currently on the wait queue 13977 * and add the new buf request at the appropriate position. 13978 * 13979 * The un->un_waitq_headp is an activity chain pointer on which 13980 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 13981 * first queue holds those requests which are positioned after 13982 * the current SD_GET_BLKNO() (in the first request); the second holds 13983 * requests which came in after their SD_GET_BLKNO() number was passed. 13984 * Thus we implement a one way scan, retracting after reaching 13985 * the end of the drive to the first request on the second 13986 * queue, at which time it becomes the first queue. 13987 * A one-way scan is natural because of the way UNIX read-ahead 13988 * blocks are allocated. 13989 * 13990 * If we lie after the first request, then we must locate the 13991 * second request list and add ourselves to it. 13992 */ 13993 ap = un->un_waitq_headp; 13994 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 13995 while (ap->av_forw != NULL) { 13996 /* 13997 * Look for an "inversion" in the (normally 13998 * ascending) block numbers. This indicates 13999 * the start of the second request list. 14000 */ 14001 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14002 /* 14003 * Search the second request list for the 14004 * first request at a larger block number. 14005 * We go before that; however if there is 14006 * no such request, we go at the end. 14007 */ 14008 do { 14009 if (SD_GET_BLKNO(bp) < 14010 SD_GET_BLKNO(ap->av_forw)) { 14011 goto insert; 14012 } 14013 ap = ap->av_forw; 14014 } while (ap->av_forw != NULL); 14015 goto insert; /* after last */ 14016 } 14017 ap = ap->av_forw; 14018 } 14019 14020 /* 14021 * No inversions... we will go after the last, and 14022 * be the first request in the second request list. 14023 */ 14024 goto insert; 14025 } 14026 14027 /* 14028 * Request is at/after the current request... 14029 * sort in the first request list. 14030 */ 14031 while (ap->av_forw != NULL) { 14032 /* 14033 * We want to go after the current request (1) if 14034 * there is an inversion after it (i.e. it is the end 14035 * of the first request list), or (2) if the next 14036 * request is a larger block no. than our request. 14037 */ 14038 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14039 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14040 goto insert; 14041 } 14042 ap = ap->av_forw; 14043 } 14044 14045 /* 14046 * Neither a second list nor a larger request, therefore 14047 * we go at the end of the first list (which is the same 14048 * as the end of the whole schebang). 14049 */ 14050 insert: 14051 bp->av_forw = ap->av_forw; 14052 ap->av_forw = bp; 14053 14054 /* 14055 * If we inserted onto the tail end of the waitq, make sure the 14056 * tail pointer is updated. 14057 */ 14058 if (ap == un->un_waitq_tailp) { 14059 un->un_waitq_tailp = bp; 14060 } 14061 } 14062 14063 14064 /* 14065 * Function: sd_start_cmds 14066 * 14067 * Description: Remove and transport cmds from the driver queues. 14068 * 14069 * Arguments: un - pointer to the unit (soft state) struct for the target. 14070 * 14071 * immed_bp - ptr to a buf to be transported immediately. Only 14072 * the immed_bp is transported; bufs on the waitq are not 14073 * processed and the un_retry_bp is not checked. If immed_bp is 14074 * NULL, then normal queue processing is performed. 14075 * 14076 * Context: May be called from kernel thread context, interrupt context, 14077 * or runout callback context. This function may not block or 14078 * call routines that block. 14079 */ 14080 14081 static void 14082 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14083 { 14084 struct sd_xbuf *xp; 14085 struct buf *bp; 14086 void (*statp)(kstat_io_t *); 14087 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14088 void (*saved_statp)(kstat_io_t *); 14089 #endif 14090 int rval; 14091 struct sd_fm_internal *sfip = NULL; 14092 14093 ASSERT(un != NULL); 14094 ASSERT(mutex_owned(SD_MUTEX(un))); 14095 ASSERT(un->un_ncmds_in_transport >= 0); 14096 ASSERT(un->un_throttle >= 0); 14097 14098 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14099 14100 do { 14101 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14102 saved_statp = NULL; 14103 #endif 14104 14105 /* 14106 * If we are syncing or dumping, fail the command to 14107 * avoid recursively calling back into scsi_transport(). 14108 * The dump I/O itself uses a separate code path so this 14109 * only prevents non-dump I/O from being sent while dumping. 14110 * File system sync takes place before dumping begins. 14111 * During panic, filesystem I/O is allowed provided 14112 * un_in_callback is <= 1. This is to prevent recursion 14113 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14114 * sd_start_cmds and so on. See panic.c for more information 14115 * about the states the system can be in during panic. 14116 */ 14117 if ((un->un_state == SD_STATE_DUMPING) || 14118 (ddi_in_panic() && (un->un_in_callback > 1))) { 14119 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14120 "sd_start_cmds: panicking\n"); 14121 goto exit; 14122 } 14123 14124 if ((bp = immed_bp) != NULL) { 14125 /* 14126 * We have a bp that must be transported immediately. 14127 * It's OK to transport the immed_bp here without doing 14128 * the throttle limit check because the immed_bp is 14129 * always used in a retry/recovery case. This means 14130 * that we know we are not at the throttle limit by 14131 * virtue of the fact that to get here we must have 14132 * already gotten a command back via sdintr(). This also 14133 * relies on (1) the command on un_retry_bp preventing 14134 * further commands from the waitq from being issued; 14135 * and (2) the code in sd_retry_command checking the 14136 * throttle limit before issuing a delayed or immediate 14137 * retry. This holds even if the throttle limit is 14138 * currently ratcheted down from its maximum value. 14139 */ 14140 statp = kstat_runq_enter; 14141 if (bp == un->un_retry_bp) { 14142 ASSERT((un->un_retry_statp == NULL) || 14143 (un->un_retry_statp == kstat_waitq_enter) || 14144 (un->un_retry_statp == 14145 kstat_runq_back_to_waitq)); 14146 /* 14147 * If the waitq kstat was incremented when 14148 * sd_set_retry_bp() queued this bp for a retry, 14149 * then we must set up statp so that the waitq 14150 * count will get decremented correctly below. 14151 * Also we must clear un->un_retry_statp to 14152 * ensure that we do not act on a stale value 14153 * in this field. 14154 */ 14155 if ((un->un_retry_statp == kstat_waitq_enter) || 14156 (un->un_retry_statp == 14157 kstat_runq_back_to_waitq)) { 14158 statp = kstat_waitq_to_runq; 14159 } 14160 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14161 saved_statp = un->un_retry_statp; 14162 #endif 14163 un->un_retry_statp = NULL; 14164 14165 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14166 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14167 "un_throttle:%d un_ncmds_in_transport:%d\n", 14168 un, un->un_retry_bp, un->un_throttle, 14169 un->un_ncmds_in_transport); 14170 } else { 14171 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14172 "processing priority bp:0x%p\n", bp); 14173 } 14174 14175 } else if ((bp = un->un_waitq_headp) != NULL) { 14176 /* 14177 * A command on the waitq is ready to go, but do not 14178 * send it if: 14179 * 14180 * (1) the throttle limit has been reached, or 14181 * (2) a retry is pending, or 14182 * (3) a START_STOP_UNIT callback pending, or 14183 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14184 * command is pending. 14185 * 14186 * For all of these conditions, IO processing will 14187 * restart after the condition is cleared. 14188 */ 14189 if (un->un_ncmds_in_transport >= un->un_throttle) { 14190 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14191 "sd_start_cmds: exiting, " 14192 "throttle limit reached!\n"); 14193 goto exit; 14194 } 14195 if (un->un_retry_bp != NULL) { 14196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14197 "sd_start_cmds: exiting, retry pending!\n"); 14198 goto exit; 14199 } 14200 if (un->un_startstop_timeid != NULL) { 14201 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14202 "sd_start_cmds: exiting, " 14203 "START_STOP pending!\n"); 14204 goto exit; 14205 } 14206 if (un->un_direct_priority_timeid != NULL) { 14207 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14208 "sd_start_cmds: exiting, " 14209 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14210 goto exit; 14211 } 14212 14213 /* Dequeue the command */ 14214 un->un_waitq_headp = bp->av_forw; 14215 if (un->un_waitq_headp == NULL) { 14216 un->un_waitq_tailp = NULL; 14217 } 14218 bp->av_forw = NULL; 14219 statp = kstat_waitq_to_runq; 14220 SD_TRACE(SD_LOG_IO_CORE, un, 14221 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14222 14223 } else { 14224 /* No work to do so bail out now */ 14225 SD_TRACE(SD_LOG_IO_CORE, un, 14226 "sd_start_cmds: no more work, exiting!\n"); 14227 goto exit; 14228 } 14229 14230 /* 14231 * Reset the state to normal. This is the mechanism by which 14232 * the state transitions from either SD_STATE_RWAIT or 14233 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14234 * If state is SD_STATE_PM_CHANGING then this command is 14235 * part of the device power control and the state must 14236 * not be put back to normal. Doing so would would 14237 * allow new commands to proceed when they shouldn't, 14238 * the device may be going off. 14239 */ 14240 if ((un->un_state != SD_STATE_SUSPENDED) && 14241 (un->un_state != SD_STATE_PM_CHANGING)) { 14242 New_state(un, SD_STATE_NORMAL); 14243 } 14244 14245 xp = SD_GET_XBUF(bp); 14246 ASSERT(xp != NULL); 14247 14248 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14249 /* 14250 * Allocate the scsi_pkt if we need one, or attach DMA 14251 * resources if we have a scsi_pkt that needs them. The 14252 * latter should only occur for commands that are being 14253 * retried. 14254 */ 14255 if ((xp->xb_pktp == NULL) || 14256 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14257 #else 14258 if (xp->xb_pktp == NULL) { 14259 #endif 14260 /* 14261 * There is no scsi_pkt allocated for this buf. Call 14262 * the initpkt function to allocate & init one. 14263 * 14264 * The scsi_init_pkt runout callback functionality is 14265 * implemented as follows: 14266 * 14267 * 1) The initpkt function always calls 14268 * scsi_init_pkt(9F) with sdrunout specified as the 14269 * callback routine. 14270 * 2) A successful packet allocation is initialized and 14271 * the I/O is transported. 14272 * 3) The I/O associated with an allocation resource 14273 * failure is left on its queue to be retried via 14274 * runout or the next I/O. 14275 * 4) The I/O associated with a DMA error is removed 14276 * from the queue and failed with EIO. Processing of 14277 * the transport queues is also halted to be 14278 * restarted via runout or the next I/O. 14279 * 5) The I/O associated with a CDB size or packet 14280 * size error is removed from the queue and failed 14281 * with EIO. Processing of the transport queues is 14282 * continued. 14283 * 14284 * Note: there is no interface for canceling a runout 14285 * callback. To prevent the driver from detaching or 14286 * suspending while a runout is pending the driver 14287 * state is set to SD_STATE_RWAIT 14288 * 14289 * Note: using the scsi_init_pkt callback facility can 14290 * result in an I/O request persisting at the head of 14291 * the list which cannot be satisfied even after 14292 * multiple retries. In the future the driver may 14293 * implement some kind of maximum runout count before 14294 * failing an I/O. 14295 * 14296 * Note: the use of funcp below may seem superfluous, 14297 * but it helps warlock figure out the correct 14298 * initpkt function calls (see [s]sd.wlcmd). 14299 */ 14300 struct scsi_pkt *pktp; 14301 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14302 14303 ASSERT(bp != un->un_rqs_bp); 14304 14305 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14306 switch ((*funcp)(bp, &pktp)) { 14307 case SD_PKT_ALLOC_SUCCESS: 14308 xp->xb_pktp = pktp; 14309 SD_TRACE(SD_LOG_IO_CORE, un, 14310 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14311 pktp); 14312 goto got_pkt; 14313 14314 case SD_PKT_ALLOC_FAILURE: 14315 /* 14316 * Temporary (hopefully) resource depletion. 14317 * Since retries and RQS commands always have a 14318 * scsi_pkt allocated, these cases should never 14319 * get here. So the only cases this needs to 14320 * handle is a bp from the waitq (which we put 14321 * back onto the waitq for sdrunout), or a bp 14322 * sent as an immed_bp (which we just fail). 14323 */ 14324 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14325 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14326 14327 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14328 14329 if (bp == immed_bp) { 14330 /* 14331 * If SD_XB_DMA_FREED is clear, then 14332 * this is a failure to allocate a 14333 * scsi_pkt, and we must fail the 14334 * command. 14335 */ 14336 if ((xp->xb_pkt_flags & 14337 SD_XB_DMA_FREED) == 0) { 14338 break; 14339 } 14340 14341 /* 14342 * If this immediate command is NOT our 14343 * un_retry_bp, then we must fail it. 14344 */ 14345 if (bp != un->un_retry_bp) { 14346 break; 14347 } 14348 14349 /* 14350 * We get here if this cmd is our 14351 * un_retry_bp that was DMAFREED, but 14352 * scsi_init_pkt() failed to reallocate 14353 * DMA resources when we attempted to 14354 * retry it. This can happen when an 14355 * mpxio failover is in progress, but 14356 * we don't want to just fail the 14357 * command in this case. 14358 * 14359 * Use timeout(9F) to restart it after 14360 * a 100ms delay. We don't want to 14361 * let sdrunout() restart it, because 14362 * sdrunout() is just supposed to start 14363 * commands that are sitting on the 14364 * wait queue. The un_retry_bp stays 14365 * set until the command completes, but 14366 * sdrunout can be called many times 14367 * before that happens. Since sdrunout 14368 * cannot tell if the un_retry_bp is 14369 * already in the transport, it could 14370 * end up calling scsi_transport() for 14371 * the un_retry_bp multiple times. 14372 * 14373 * Also: don't schedule the callback 14374 * if some other callback is already 14375 * pending. 14376 */ 14377 if (un->un_retry_statp == NULL) { 14378 /* 14379 * restore the kstat pointer to 14380 * keep kstat counts coherent 14381 * when we do retry the command. 14382 */ 14383 un->un_retry_statp = 14384 saved_statp; 14385 } 14386 14387 if ((un->un_startstop_timeid == NULL) && 14388 (un->un_retry_timeid == NULL) && 14389 (un->un_direct_priority_timeid == 14390 NULL)) { 14391 14392 un->un_retry_timeid = 14393 timeout( 14394 sd_start_retry_command, 14395 un, SD_RESTART_TIMEOUT); 14396 } 14397 goto exit; 14398 } 14399 14400 #else 14401 if (bp == immed_bp) { 14402 break; /* Just fail the command */ 14403 } 14404 #endif 14405 14406 /* Add the buf back to the head of the waitq */ 14407 bp->av_forw = un->un_waitq_headp; 14408 un->un_waitq_headp = bp; 14409 if (un->un_waitq_tailp == NULL) { 14410 un->un_waitq_tailp = bp; 14411 } 14412 goto exit; 14413 14414 case SD_PKT_ALLOC_FAILURE_NO_DMA: 14415 /* 14416 * HBA DMA resource failure. Fail the command 14417 * and continue processing of the queues. 14418 */ 14419 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14420 "sd_start_cmds: " 14421 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 14422 break; 14423 14424 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 14425 /* 14426 * Note:x86: Partial DMA mapping not supported 14427 * for USCSI commands, and all the needed DMA 14428 * resources were not allocated. 14429 */ 14430 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14431 "sd_start_cmds: " 14432 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 14433 break; 14434 14435 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 14436 /* 14437 * Note:x86: Request cannot fit into CDB based 14438 * on lba and len. 14439 */ 14440 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14441 "sd_start_cmds: " 14442 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 14443 break; 14444 14445 default: 14446 /* Should NEVER get here! */ 14447 panic("scsi_initpkt error"); 14448 /*NOTREACHED*/ 14449 } 14450 14451 /* 14452 * Fatal error in allocating a scsi_pkt for this buf. 14453 * Update kstats & return the buf with an error code. 14454 * We must use sd_return_failed_command_no_restart() to 14455 * avoid a recursive call back into sd_start_cmds(). 14456 * However this also means that we must keep processing 14457 * the waitq here in order to avoid stalling. 14458 */ 14459 if (statp == kstat_waitq_to_runq) { 14460 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 14461 } 14462 sd_return_failed_command_no_restart(un, bp, EIO); 14463 if (bp == immed_bp) { 14464 /* immed_bp is gone by now, so clear this */ 14465 immed_bp = NULL; 14466 } 14467 continue; 14468 } 14469 got_pkt: 14470 if (bp == immed_bp) { 14471 /* goto the head of the class.... */ 14472 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 14473 } 14474 14475 un->un_ncmds_in_transport++; 14476 SD_UPDATE_KSTATS(un, statp, bp); 14477 14478 /* 14479 * Call scsi_transport() to send the command to the target. 14480 * According to SCSA architecture, we must drop the mutex here 14481 * before calling scsi_transport() in order to avoid deadlock. 14482 * Note that the scsi_pkt's completion routine can be executed 14483 * (from interrupt context) even before the call to 14484 * scsi_transport() returns. 14485 */ 14486 SD_TRACE(SD_LOG_IO_CORE, un, 14487 "sd_start_cmds: calling scsi_transport()\n"); 14488 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 14489 14490 mutex_exit(SD_MUTEX(un)); 14491 rval = scsi_transport(xp->xb_pktp); 14492 mutex_enter(SD_MUTEX(un)); 14493 14494 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14495 "sd_start_cmds: scsi_transport() returned %d\n", rval); 14496 14497 switch (rval) { 14498 case TRAN_ACCEPT: 14499 /* Clear this with every pkt accepted by the HBA */ 14500 un->un_tran_fatal_count = 0; 14501 break; /* Success; try the next cmd (if any) */ 14502 14503 case TRAN_BUSY: 14504 un->un_ncmds_in_transport--; 14505 ASSERT(un->un_ncmds_in_transport >= 0); 14506 14507 /* 14508 * Don't retry request sense, the sense data 14509 * is lost when another request is sent. 14510 * Free up the rqs buf and retry 14511 * the original failed cmd. Update kstat. 14512 */ 14513 if (bp == un->un_rqs_bp) { 14514 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14515 bp = sd_mark_rqs_idle(un, xp); 14516 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 14517 NULL, NULL, EIO, un->un_busy_timeout / 500, 14518 kstat_waitq_enter); 14519 goto exit; 14520 } 14521 14522 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14523 /* 14524 * Free the DMA resources for the scsi_pkt. This will 14525 * allow mpxio to select another path the next time 14526 * we call scsi_transport() with this scsi_pkt. 14527 * See sdintr() for the rationalization behind this. 14528 */ 14529 if ((un->un_f_is_fibre == TRUE) && 14530 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14531 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 14532 scsi_dmafree(xp->xb_pktp); 14533 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14534 } 14535 #endif 14536 14537 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 14538 /* 14539 * Commands that are SD_PATH_DIRECT_PRIORITY 14540 * are for error recovery situations. These do 14541 * not use the normal command waitq, so if they 14542 * get a TRAN_BUSY we cannot put them back onto 14543 * the waitq for later retry. One possible 14544 * problem is that there could already be some 14545 * other command on un_retry_bp that is waiting 14546 * for this one to complete, so we would be 14547 * deadlocked if we put this command back onto 14548 * the waitq for later retry (since un_retry_bp 14549 * must complete before the driver gets back to 14550 * commands on the waitq). 14551 * 14552 * To avoid deadlock we must schedule a callback 14553 * that will restart this command after a set 14554 * interval. This should keep retrying for as 14555 * long as the underlying transport keeps 14556 * returning TRAN_BUSY (just like for other 14557 * commands). Use the same timeout interval as 14558 * for the ordinary TRAN_BUSY retry. 14559 */ 14560 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14561 "sd_start_cmds: scsi_transport() returned " 14562 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 14563 14564 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14565 un->un_direct_priority_timeid = 14566 timeout(sd_start_direct_priority_command, 14567 bp, un->un_busy_timeout / 500); 14568 14569 goto exit; 14570 } 14571 14572 /* 14573 * For TRAN_BUSY, we want to reduce the throttle value, 14574 * unless we are retrying a command. 14575 */ 14576 if (bp != un->un_retry_bp) { 14577 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 14578 } 14579 14580 /* 14581 * Set up the bp to be tried again 10 ms later. 14582 * Note:x86: Is there a timeout value in the sd_lun 14583 * for this condition? 14584 */ 14585 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500, 14586 kstat_runq_back_to_waitq); 14587 goto exit; 14588 14589 case TRAN_FATAL_ERROR: 14590 un->un_tran_fatal_count++; 14591 /* FALLTHRU */ 14592 14593 case TRAN_BADPKT: 14594 default: 14595 un->un_ncmds_in_transport--; 14596 ASSERT(un->un_ncmds_in_transport >= 0); 14597 14598 /* 14599 * If this is our REQUEST SENSE command with a 14600 * transport error, we must get back the pointers 14601 * to the original buf, and mark the REQUEST 14602 * SENSE command as "available". 14603 */ 14604 if (bp == un->un_rqs_bp) { 14605 bp = sd_mark_rqs_idle(un, xp); 14606 xp = SD_GET_XBUF(bp); 14607 } else { 14608 /* 14609 * Legacy behavior: do not update transport 14610 * error count for request sense commands. 14611 */ 14612 SD_UPDATE_ERRSTATS(un, sd_transerrs); 14613 } 14614 14615 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14616 sd_print_transport_rejected_message(un, xp, rval); 14617 14618 /* 14619 * This command will be terminated by SD driver due 14620 * to a fatal transport error. We should post 14621 * ereport.io.scsi.cmd.disk.tran with driver-assessment 14622 * of "fail" for any command to indicate this 14623 * situation. 14624 */ 14625 if (xp->xb_ena > 0) { 14626 ASSERT(un->un_fm_private != NULL); 14627 sfip = un->un_fm_private; 14628 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT; 14629 sd_ssc_extract_info(&sfip->fm_ssc, un, 14630 xp->xb_pktp, bp, xp); 14631 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 14632 } 14633 14634 /* 14635 * We must use sd_return_failed_command_no_restart() to 14636 * avoid a recursive call back into sd_start_cmds(). 14637 * However this also means that we must keep processing 14638 * the waitq here in order to avoid stalling. 14639 */ 14640 sd_return_failed_command_no_restart(un, bp, EIO); 14641 14642 /* 14643 * Notify any threads waiting in sd_ddi_suspend() that 14644 * a command completion has occurred. 14645 */ 14646 if (un->un_state == SD_STATE_SUSPENDED) { 14647 cv_broadcast(&un->un_disk_busy_cv); 14648 } 14649 14650 if (bp == immed_bp) { 14651 /* immed_bp is gone by now, so clear this */ 14652 immed_bp = NULL; 14653 } 14654 break; 14655 } 14656 14657 } while (immed_bp == NULL); 14658 14659 exit: 14660 ASSERT(mutex_owned(SD_MUTEX(un))); 14661 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 14662 } 14663 14664 14665 /* 14666 * Function: sd_return_command 14667 * 14668 * Description: Returns a command to its originator (with or without an 14669 * error). Also starts commands waiting to be transported 14670 * to the target. 14671 * 14672 * Context: May be called from interrupt, kernel, or timeout context 14673 */ 14674 14675 static void 14676 sd_return_command(struct sd_lun *un, struct buf *bp) 14677 { 14678 struct sd_xbuf *xp; 14679 struct scsi_pkt *pktp; 14680 struct sd_fm_internal *sfip; 14681 14682 ASSERT(bp != NULL); 14683 ASSERT(un != NULL); 14684 ASSERT(mutex_owned(SD_MUTEX(un))); 14685 ASSERT(bp != un->un_rqs_bp); 14686 xp = SD_GET_XBUF(bp); 14687 ASSERT(xp != NULL); 14688 14689 pktp = SD_GET_PKTP(bp); 14690 sfip = (struct sd_fm_internal *)un->un_fm_private; 14691 ASSERT(sfip != NULL); 14692 14693 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 14694 14695 /* 14696 * Note: check for the "sdrestart failed" case. 14697 */ 14698 if ((un->un_partial_dma_supported == 1) && 14699 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 14700 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 14701 (xp->xb_pktp->pkt_resid == 0)) { 14702 14703 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 14704 /* 14705 * Successfully set up next portion of cmd 14706 * transfer, try sending it 14707 */ 14708 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14709 NULL, NULL, 0, (clock_t)0, NULL); 14710 sd_start_cmds(un, NULL); 14711 return; /* Note:x86: need a return here? */ 14712 } 14713 } 14714 14715 /* 14716 * If this is the failfast bp, clear it from un_failfast_bp. This 14717 * can happen if upon being re-tried the failfast bp either 14718 * succeeded or encountered another error (possibly even a different 14719 * error than the one that precipitated the failfast state, but in 14720 * that case it would have had to exhaust retries as well). Regardless, 14721 * this should not occur whenever the instance is in the active 14722 * failfast state. 14723 */ 14724 if (bp == un->un_failfast_bp) { 14725 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14726 un->un_failfast_bp = NULL; 14727 } 14728 14729 /* 14730 * Clear the failfast state upon successful completion of ANY cmd. 14731 */ 14732 if (bp->b_error == 0) { 14733 un->un_failfast_state = SD_FAILFAST_INACTIVE; 14734 /* 14735 * If this is a successful command, but used to be retried, 14736 * we will take it as a recovered command and post an 14737 * ereport with driver-assessment of "recovered". 14738 */ 14739 if (xp->xb_ena > 0) { 14740 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 14741 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY); 14742 } 14743 } else { 14744 /* 14745 * If this is a failed non-USCSI command we will post an 14746 * ereport with driver-assessment set accordingly("fail" or 14747 * "fatal"). 14748 */ 14749 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 14750 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 14751 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL); 14752 } 14753 } 14754 14755 /* 14756 * This is used if the command was retried one or more times. Show that 14757 * we are done with it, and allow processing of the waitq to resume. 14758 */ 14759 if (bp == un->un_retry_bp) { 14760 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14761 "sd_return_command: un:0x%p: " 14762 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14763 un->un_retry_bp = NULL; 14764 un->un_retry_statp = NULL; 14765 } 14766 14767 SD_UPDATE_RDWR_STATS(un, bp); 14768 SD_UPDATE_PARTITION_STATS(un, bp); 14769 14770 switch (un->un_state) { 14771 case SD_STATE_SUSPENDED: 14772 /* 14773 * Notify any threads waiting in sd_ddi_suspend() that 14774 * a command completion has occurred. 14775 */ 14776 cv_broadcast(&un->un_disk_busy_cv); 14777 break; 14778 default: 14779 sd_start_cmds(un, NULL); 14780 break; 14781 } 14782 14783 /* Return this command up the iodone chain to its originator. */ 14784 mutex_exit(SD_MUTEX(un)); 14785 14786 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14787 xp->xb_pktp = NULL; 14788 14789 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14790 14791 ASSERT(!mutex_owned(SD_MUTEX(un))); 14792 mutex_enter(SD_MUTEX(un)); 14793 14794 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 14795 } 14796 14797 14798 /* 14799 * Function: sd_return_failed_command 14800 * 14801 * Description: Command completion when an error occurred. 14802 * 14803 * Context: May be called from interrupt context 14804 */ 14805 14806 static void 14807 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 14808 { 14809 ASSERT(bp != NULL); 14810 ASSERT(un != NULL); 14811 ASSERT(mutex_owned(SD_MUTEX(un))); 14812 14813 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14814 "sd_return_failed_command: entry\n"); 14815 14816 /* 14817 * b_resid could already be nonzero due to a partial data 14818 * transfer, so do not change it here. 14819 */ 14820 SD_BIOERROR(bp, errcode); 14821 14822 sd_return_command(un, bp); 14823 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14824 "sd_return_failed_command: exit\n"); 14825 } 14826 14827 14828 /* 14829 * Function: sd_return_failed_command_no_restart 14830 * 14831 * Description: Same as sd_return_failed_command, but ensures that no 14832 * call back into sd_start_cmds will be issued. 14833 * 14834 * Context: May be called from interrupt context 14835 */ 14836 14837 static void 14838 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 14839 int errcode) 14840 { 14841 struct sd_xbuf *xp; 14842 14843 ASSERT(bp != NULL); 14844 ASSERT(un != NULL); 14845 ASSERT(mutex_owned(SD_MUTEX(un))); 14846 xp = SD_GET_XBUF(bp); 14847 ASSERT(xp != NULL); 14848 ASSERT(errcode != 0); 14849 14850 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14851 "sd_return_failed_command_no_restart: entry\n"); 14852 14853 /* 14854 * b_resid could already be nonzero due to a partial data 14855 * transfer, so do not change it here. 14856 */ 14857 SD_BIOERROR(bp, errcode); 14858 14859 /* 14860 * If this is the failfast bp, clear it. This can happen if the 14861 * failfast bp encounterd a fatal error when we attempted to 14862 * re-try it (such as a scsi_transport(9F) failure). However 14863 * we should NOT be in an active failfast state if the failfast 14864 * bp is not NULL. 14865 */ 14866 if (bp == un->un_failfast_bp) { 14867 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 14868 un->un_failfast_bp = NULL; 14869 } 14870 14871 if (bp == un->un_retry_bp) { 14872 /* 14873 * This command was retried one or more times. Show that we are 14874 * done with it, and allow processing of the waitq to resume. 14875 */ 14876 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14877 "sd_return_failed_command_no_restart: " 14878 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 14879 un->un_retry_bp = NULL; 14880 un->un_retry_statp = NULL; 14881 } 14882 14883 SD_UPDATE_RDWR_STATS(un, bp); 14884 SD_UPDATE_PARTITION_STATS(un, bp); 14885 14886 mutex_exit(SD_MUTEX(un)); 14887 14888 if (xp->xb_pktp != NULL) { 14889 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 14890 xp->xb_pktp = NULL; 14891 } 14892 14893 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 14894 14895 mutex_enter(SD_MUTEX(un)); 14896 14897 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14898 "sd_return_failed_command_no_restart: exit\n"); 14899 } 14900 14901 14902 /* 14903 * Function: sd_retry_command 14904 * 14905 * Description: queue up a command for retry, or (optionally) fail it 14906 * if retry counts are exhausted. 14907 * 14908 * Arguments: un - Pointer to the sd_lun struct for the target. 14909 * 14910 * bp - Pointer to the buf for the command to be retried. 14911 * 14912 * retry_check_flag - Flag to see which (if any) of the retry 14913 * counts should be decremented/checked. If the indicated 14914 * retry count is exhausted, then the command will not be 14915 * retried; it will be failed instead. This should use a 14916 * value equal to one of the following: 14917 * 14918 * SD_RETRIES_NOCHECK 14919 * SD_RESD_RETRIES_STANDARD 14920 * SD_RETRIES_VICTIM 14921 * 14922 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 14923 * if the check should be made to see of FLAG_ISOLATE is set 14924 * in the pkt. If FLAG_ISOLATE is set, then the command is 14925 * not retried, it is simply failed. 14926 * 14927 * user_funcp - Ptr to function to call before dispatching the 14928 * command. May be NULL if no action needs to be performed. 14929 * (Primarily intended for printing messages.) 14930 * 14931 * user_arg - Optional argument to be passed along to 14932 * the user_funcp call. 14933 * 14934 * failure_code - errno return code to set in the bp if the 14935 * command is going to be failed. 14936 * 14937 * retry_delay - Retry delay interval in (clock_t) units. May 14938 * be zero which indicates that the retry should be retried 14939 * immediately (ie, without an intervening delay). 14940 * 14941 * statp - Ptr to kstat function to be updated if the command 14942 * is queued for a delayed retry. May be NULL if no kstat 14943 * update is desired. 14944 * 14945 * Context: May be called from interrupt context. 14946 */ 14947 14948 static void 14949 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 14950 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 14951 code), void *user_arg, int failure_code, clock_t retry_delay, 14952 void (*statp)(kstat_io_t *)) 14953 { 14954 struct sd_xbuf *xp; 14955 struct scsi_pkt *pktp; 14956 struct sd_fm_internal *sfip; 14957 14958 ASSERT(un != NULL); 14959 ASSERT(mutex_owned(SD_MUTEX(un))); 14960 ASSERT(bp != NULL); 14961 xp = SD_GET_XBUF(bp); 14962 ASSERT(xp != NULL); 14963 pktp = SD_GET_PKTP(bp); 14964 ASSERT(pktp != NULL); 14965 14966 sfip = (struct sd_fm_internal *)un->un_fm_private; 14967 ASSERT(sfip != NULL); 14968 14969 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14970 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 14971 14972 /* 14973 * If we are syncing or dumping, fail the command to avoid 14974 * recursively calling back into scsi_transport(). 14975 */ 14976 if (ddi_in_panic()) { 14977 goto fail_command_no_log; 14978 } 14979 14980 /* 14981 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 14982 * log an error and fail the command. 14983 */ 14984 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14985 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 14986 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 14987 sd_dump_memory(un, SD_LOG_IO, "CDB", 14988 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 14989 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 14990 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 14991 goto fail_command; 14992 } 14993 14994 /* 14995 * If we are suspended, then put the command onto head of the 14996 * wait queue since we don't want to start more commands, and 14997 * clear the un_retry_bp. Next time when we are resumed, will 14998 * handle the command in the wait queue. 14999 */ 15000 switch (un->un_state) { 15001 case SD_STATE_SUSPENDED: 15002 case SD_STATE_DUMPING: 15003 bp->av_forw = un->un_waitq_headp; 15004 un->un_waitq_headp = bp; 15005 if (un->un_waitq_tailp == NULL) { 15006 un->un_waitq_tailp = bp; 15007 } 15008 if (bp == un->un_retry_bp) { 15009 un->un_retry_bp = NULL; 15010 un->un_retry_statp = NULL; 15011 } 15012 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15013 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15014 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15015 return; 15016 default: 15017 break; 15018 } 15019 15020 /* 15021 * If the caller wants us to check FLAG_ISOLATE, then see if that 15022 * is set; if it is then we do not want to retry the command. 15023 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15024 */ 15025 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15026 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15027 goto fail_command; 15028 } 15029 } 15030 15031 15032 /* 15033 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15034 * command timeout or a selection timeout has occurred. This means 15035 * that we were unable to establish an kind of communication with 15036 * the target, and subsequent retries and/or commands are likely 15037 * to encounter similar results and take a long time to complete. 15038 * 15039 * If this is a failfast error condition, we need to update the 15040 * failfast state, even if this bp does not have B_FAILFAST set. 15041 */ 15042 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15043 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15044 ASSERT(un->un_failfast_bp == NULL); 15045 /* 15046 * If we are already in the active failfast state, and 15047 * another failfast error condition has been detected, 15048 * then fail this command if it has B_FAILFAST set. 15049 * If B_FAILFAST is clear, then maintain the legacy 15050 * behavior of retrying heroically, even tho this will 15051 * take a lot more time to fail the command. 15052 */ 15053 if (bp->b_flags & B_FAILFAST) { 15054 goto fail_command; 15055 } 15056 } else { 15057 /* 15058 * We're not in the active failfast state, but we 15059 * have a failfast error condition, so we must begin 15060 * transition to the next state. We do this regardless 15061 * of whether or not this bp has B_FAILFAST set. 15062 */ 15063 if (un->un_failfast_bp == NULL) { 15064 /* 15065 * This is the first bp to meet a failfast 15066 * condition so save it on un_failfast_bp & 15067 * do normal retry processing. Do not enter 15068 * active failfast state yet. This marks 15069 * entry into the "failfast pending" state. 15070 */ 15071 un->un_failfast_bp = bp; 15072 15073 } else if (un->un_failfast_bp == bp) { 15074 /* 15075 * This is the second time *this* bp has 15076 * encountered a failfast error condition, 15077 * so enter active failfast state & flush 15078 * queues as appropriate. 15079 */ 15080 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15081 un->un_failfast_bp = NULL; 15082 sd_failfast_flushq(un); 15083 15084 /* 15085 * Fail this bp now if B_FAILFAST set; 15086 * otherwise continue with retries. (It would 15087 * be pretty ironic if this bp succeeded on a 15088 * subsequent retry after we just flushed all 15089 * the queues). 15090 */ 15091 if (bp->b_flags & B_FAILFAST) { 15092 goto fail_command; 15093 } 15094 15095 #if !defined(lint) && !defined(__lint) 15096 } else { 15097 /* 15098 * If neither of the preceeding conditionals 15099 * was true, it means that there is some 15100 * *other* bp that has met an inital failfast 15101 * condition and is currently either being 15102 * retried or is waiting to be retried. In 15103 * that case we should perform normal retry 15104 * processing on *this* bp, since there is a 15105 * chance that the current failfast condition 15106 * is transient and recoverable. If that does 15107 * not turn out to be the case, then retries 15108 * will be cleared when the wait queue is 15109 * flushed anyway. 15110 */ 15111 #endif 15112 } 15113 } 15114 } else { 15115 /* 15116 * SD_RETRIES_FAILFAST is clear, which indicates that we 15117 * likely were able to at least establish some level of 15118 * communication with the target and subsequent commands 15119 * and/or retries are likely to get through to the target, 15120 * In this case we want to be aggressive about clearing 15121 * the failfast state. Note that this does not affect 15122 * the "failfast pending" condition. 15123 */ 15124 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15125 } 15126 15127 15128 /* 15129 * Check the specified retry count to see if we can still do 15130 * any retries with this pkt before we should fail it. 15131 */ 15132 switch (retry_check_flag & SD_RETRIES_MASK) { 15133 case SD_RETRIES_VICTIM: 15134 /* 15135 * Check the victim retry count. If exhausted, then fall 15136 * thru & check against the standard retry count. 15137 */ 15138 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15139 /* Increment count & proceed with the retry */ 15140 xp->xb_victim_retry_count++; 15141 break; 15142 } 15143 /* Victim retries exhausted, fall back to std. retries... */ 15144 /* FALLTHRU */ 15145 15146 case SD_RETRIES_STANDARD: 15147 if (xp->xb_retry_count >= un->un_retry_count) { 15148 /* Retries exhausted, fail the command */ 15149 SD_TRACE(SD_LOG_IO_CORE, un, 15150 "sd_retry_command: retries exhausted!\n"); 15151 /* 15152 * update b_resid for failed SCMD_READ & SCMD_WRITE 15153 * commands with nonzero pkt_resid. 15154 */ 15155 if ((pktp->pkt_reason == CMD_CMPLT) && 15156 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15157 (pktp->pkt_resid != 0)) { 15158 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15159 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15160 SD_UPDATE_B_RESID(bp, pktp); 15161 } 15162 } 15163 goto fail_command; 15164 } 15165 xp->xb_retry_count++; 15166 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15167 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15168 break; 15169 15170 case SD_RETRIES_UA: 15171 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15172 /* Retries exhausted, fail the command */ 15173 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15174 "Unit Attention retries exhausted. " 15175 "Check the target.\n"); 15176 goto fail_command; 15177 } 15178 xp->xb_ua_retry_count++; 15179 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15180 "sd_retry_command: retry count:%d\n", 15181 xp->xb_ua_retry_count); 15182 break; 15183 15184 case SD_RETRIES_BUSY: 15185 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15186 /* Retries exhausted, fail the command */ 15187 SD_TRACE(SD_LOG_IO_CORE, un, 15188 "sd_retry_command: retries exhausted!\n"); 15189 goto fail_command; 15190 } 15191 xp->xb_retry_count++; 15192 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15193 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15194 break; 15195 15196 case SD_RETRIES_NOCHECK: 15197 default: 15198 /* No retry count to check. Just proceed with the retry */ 15199 break; 15200 } 15201 15202 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15203 15204 /* 15205 * If this is a non-USCSI command being retried 15206 * during execution last time, we should post an ereport with 15207 * driver-assessment of the value "retry". 15208 * For partial DMA, request sense and STATUS_QFULL, there are no 15209 * hardware errors, we bypass ereport posting. 15210 */ 15211 if (failure_code != 0) { 15212 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 15213 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp); 15214 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY); 15215 } 15216 } 15217 15218 /* 15219 * If we were given a zero timeout, we must attempt to retry the 15220 * command immediately (ie, without a delay). 15221 */ 15222 if (retry_delay == 0) { 15223 /* 15224 * Check some limiting conditions to see if we can actually 15225 * do the immediate retry. If we cannot, then we must 15226 * fall back to queueing up a delayed retry. 15227 */ 15228 if (un->un_ncmds_in_transport >= un->un_throttle) { 15229 /* 15230 * We are at the throttle limit for the target, 15231 * fall back to delayed retry. 15232 */ 15233 retry_delay = un->un_busy_timeout; 15234 statp = kstat_waitq_enter; 15235 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15236 "sd_retry_command: immed. retry hit " 15237 "throttle!\n"); 15238 } else { 15239 /* 15240 * We're clear to proceed with the immediate retry. 15241 * First call the user-provided function (if any) 15242 */ 15243 if (user_funcp != NULL) { 15244 (*user_funcp)(un, bp, user_arg, 15245 SD_IMMEDIATE_RETRY_ISSUED); 15246 #ifdef __lock_lint 15247 sd_print_incomplete_msg(un, bp, user_arg, 15248 SD_IMMEDIATE_RETRY_ISSUED); 15249 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15250 SD_IMMEDIATE_RETRY_ISSUED); 15251 sd_print_sense_failed_msg(un, bp, user_arg, 15252 SD_IMMEDIATE_RETRY_ISSUED); 15253 #endif 15254 } 15255 15256 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15257 "sd_retry_command: issuing immediate retry\n"); 15258 15259 /* 15260 * Call sd_start_cmds() to transport the command to 15261 * the target. 15262 */ 15263 sd_start_cmds(un, bp); 15264 15265 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15266 "sd_retry_command exit\n"); 15267 return; 15268 } 15269 } 15270 15271 /* 15272 * Set up to retry the command after a delay. 15273 * First call the user-provided function (if any) 15274 */ 15275 if (user_funcp != NULL) { 15276 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15277 } 15278 15279 sd_set_retry_bp(un, bp, retry_delay, statp); 15280 15281 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15282 return; 15283 15284 fail_command: 15285 15286 if (user_funcp != NULL) { 15287 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15288 } 15289 15290 fail_command_no_log: 15291 15292 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15293 "sd_retry_command: returning failed command\n"); 15294 15295 sd_return_failed_command(un, bp, failure_code); 15296 15297 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15298 } 15299 15300 15301 /* 15302 * Function: sd_set_retry_bp 15303 * 15304 * Description: Set up the given bp for retry. 15305 * 15306 * Arguments: un - ptr to associated softstate 15307 * bp - ptr to buf(9S) for the command 15308 * retry_delay - time interval before issuing retry (may be 0) 15309 * statp - optional pointer to kstat function 15310 * 15311 * Context: May be called under interrupt context 15312 */ 15313 15314 static void 15315 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15316 void (*statp)(kstat_io_t *)) 15317 { 15318 ASSERT(un != NULL); 15319 ASSERT(mutex_owned(SD_MUTEX(un))); 15320 ASSERT(bp != NULL); 15321 15322 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15323 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15324 15325 /* 15326 * Indicate that the command is being retried. This will not allow any 15327 * other commands on the wait queue to be transported to the target 15328 * until this command has been completed (success or failure). The 15329 * "retry command" is not transported to the target until the given 15330 * time delay expires, unless the user specified a 0 retry_delay. 15331 * 15332 * Note: the timeout(9F) callback routine is what actually calls 15333 * sd_start_cmds() to transport the command, with the exception of a 15334 * zero retry_delay. The only current implementor of a zero retry delay 15335 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15336 */ 15337 if (un->un_retry_bp == NULL) { 15338 ASSERT(un->un_retry_statp == NULL); 15339 un->un_retry_bp = bp; 15340 15341 /* 15342 * If the user has not specified a delay the command should 15343 * be queued and no timeout should be scheduled. 15344 */ 15345 if (retry_delay == 0) { 15346 /* 15347 * Save the kstat pointer that will be used in the 15348 * call to SD_UPDATE_KSTATS() below, so that 15349 * sd_start_cmds() can correctly decrement the waitq 15350 * count when it is time to transport this command. 15351 */ 15352 un->un_retry_statp = statp; 15353 goto done; 15354 } 15355 } 15356 15357 if (un->un_retry_bp == bp) { 15358 /* 15359 * Save the kstat pointer that will be used in the call to 15360 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15361 * correctly decrement the waitq count when it is time to 15362 * transport this command. 15363 */ 15364 un->un_retry_statp = statp; 15365 15366 /* 15367 * Schedule a timeout if: 15368 * 1) The user has specified a delay. 15369 * 2) There is not a START_STOP_UNIT callback pending. 15370 * 15371 * If no delay has been specified, then it is up to the caller 15372 * to ensure that IO processing continues without stalling. 15373 * Effectively, this means that the caller will issue the 15374 * required call to sd_start_cmds(). The START_STOP_UNIT 15375 * callback does this after the START STOP UNIT command has 15376 * completed. In either of these cases we should not schedule 15377 * a timeout callback here. Also don't schedule the timeout if 15378 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15379 */ 15380 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15381 (un->un_direct_priority_timeid == NULL)) { 15382 un->un_retry_timeid = 15383 timeout(sd_start_retry_command, un, retry_delay); 15384 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15385 "sd_set_retry_bp: setting timeout: un: 0x%p" 15386 " bp:0x%p un_retry_timeid:0x%p\n", 15387 un, bp, un->un_retry_timeid); 15388 } 15389 } else { 15390 /* 15391 * We only get in here if there is already another command 15392 * waiting to be retried. In this case, we just put the 15393 * given command onto the wait queue, so it can be transported 15394 * after the current retry command has completed. 15395 * 15396 * Also we have to make sure that if the command at the head 15397 * of the wait queue is the un_failfast_bp, that we do not 15398 * put ahead of it any other commands that are to be retried. 15399 */ 15400 if ((un->un_failfast_bp != NULL) && 15401 (un->un_failfast_bp == un->un_waitq_headp)) { 15402 /* 15403 * Enqueue this command AFTER the first command on 15404 * the wait queue (which is also un_failfast_bp). 15405 */ 15406 bp->av_forw = un->un_waitq_headp->av_forw; 15407 un->un_waitq_headp->av_forw = bp; 15408 if (un->un_waitq_headp == un->un_waitq_tailp) { 15409 un->un_waitq_tailp = bp; 15410 } 15411 } else { 15412 /* Enqueue this command at the head of the waitq. */ 15413 bp->av_forw = un->un_waitq_headp; 15414 un->un_waitq_headp = bp; 15415 if (un->un_waitq_tailp == NULL) { 15416 un->un_waitq_tailp = bp; 15417 } 15418 } 15419 15420 if (statp == NULL) { 15421 statp = kstat_waitq_enter; 15422 } 15423 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15424 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15425 } 15426 15427 done: 15428 if (statp != NULL) { 15429 SD_UPDATE_KSTATS(un, statp, bp); 15430 } 15431 15432 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15433 "sd_set_retry_bp: exit un:0x%p\n", un); 15434 } 15435 15436 15437 /* 15438 * Function: sd_start_retry_command 15439 * 15440 * Description: Start the command that has been waiting on the target's 15441 * retry queue. Called from timeout(9F) context after the 15442 * retry delay interval has expired. 15443 * 15444 * Arguments: arg - pointer to associated softstate for the device. 15445 * 15446 * Context: timeout(9F) thread context. May not sleep. 15447 */ 15448 15449 static void 15450 sd_start_retry_command(void *arg) 15451 { 15452 struct sd_lun *un = arg; 15453 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_start_retry_command: entry\n"); 15459 15460 mutex_enter(SD_MUTEX(un)); 15461 15462 un->un_retry_timeid = NULL; 15463 15464 if (un->un_retry_bp != NULL) { 15465 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15466 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 15467 un, un->un_retry_bp); 15468 sd_start_cmds(un, un->un_retry_bp); 15469 } 15470 15471 mutex_exit(SD_MUTEX(un)); 15472 15473 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15474 "sd_start_retry_command: exit\n"); 15475 } 15476 15477 15478 /* 15479 * Function: sd_start_direct_priority_command 15480 * 15481 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 15482 * received TRAN_BUSY when we called scsi_transport() to send it 15483 * to the underlying HBA. This function is called from timeout(9F) 15484 * context after the delay interval has expired. 15485 * 15486 * Arguments: arg - pointer to associated buf(9S) to be restarted. 15487 * 15488 * Context: timeout(9F) thread context. May not sleep. 15489 */ 15490 15491 static void 15492 sd_start_direct_priority_command(void *arg) 15493 { 15494 struct buf *priority_bp = arg; 15495 struct sd_lun *un; 15496 15497 ASSERT(priority_bp != NULL); 15498 un = SD_GET_UN(priority_bp); 15499 ASSERT(un != NULL); 15500 ASSERT(!mutex_owned(SD_MUTEX(un))); 15501 15502 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15503 "sd_start_direct_priority_command: entry\n"); 15504 15505 mutex_enter(SD_MUTEX(un)); 15506 un->un_direct_priority_timeid = NULL; 15507 sd_start_cmds(un, priority_bp); 15508 mutex_exit(SD_MUTEX(un)); 15509 15510 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15511 "sd_start_direct_priority_command: exit\n"); 15512 } 15513 15514 15515 /* 15516 * Function: sd_send_request_sense_command 15517 * 15518 * Description: Sends a REQUEST SENSE command to the target 15519 * 15520 * Context: May be called from interrupt context. 15521 */ 15522 15523 static void 15524 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 15525 struct scsi_pkt *pktp) 15526 { 15527 ASSERT(bp != NULL); 15528 ASSERT(un != NULL); 15529 ASSERT(mutex_owned(SD_MUTEX(un))); 15530 15531 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 15532 "entry: buf:0x%p\n", bp); 15533 15534 /* 15535 * If we are syncing or dumping, then fail the command to avoid a 15536 * recursive callback into scsi_transport(). Also fail the command 15537 * if we are suspended (legacy behavior). 15538 */ 15539 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 15540 (un->un_state == SD_STATE_DUMPING)) { 15541 sd_return_failed_command(un, bp, EIO); 15542 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15543 "sd_send_request_sense_command: syncing/dumping, exit\n"); 15544 return; 15545 } 15546 15547 /* 15548 * Retry the failed command and don't issue the request sense if: 15549 * 1) the sense buf is busy 15550 * 2) we have 1 or more outstanding commands on the target 15551 * (the sense data will be cleared or invalidated any way) 15552 * 15553 * Note: There could be an issue with not checking a retry limit here, 15554 * the problem is determining which retry limit to check. 15555 */ 15556 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 15557 /* Don't retry if the command is flagged as non-retryable */ 15558 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15559 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15560 NULL, NULL, 0, un->un_busy_timeout, 15561 kstat_waitq_enter); 15562 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15563 "sd_send_request_sense_command: " 15564 "at full throttle, retrying exit\n"); 15565 } else { 15566 sd_return_failed_command(un, bp, EIO); 15567 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15568 "sd_send_request_sense_command: " 15569 "at full throttle, non-retryable exit\n"); 15570 } 15571 return; 15572 } 15573 15574 sd_mark_rqs_busy(un, bp); 15575 sd_start_cmds(un, un->un_rqs_bp); 15576 15577 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15578 "sd_send_request_sense_command: exit\n"); 15579 } 15580 15581 15582 /* 15583 * Function: sd_mark_rqs_busy 15584 * 15585 * Description: Indicate that the request sense bp for this instance is 15586 * in use. 15587 * 15588 * Context: May be called under interrupt context 15589 */ 15590 15591 static void 15592 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 15593 { 15594 struct sd_xbuf *sense_xp; 15595 15596 ASSERT(un != NULL); 15597 ASSERT(bp != NULL); 15598 ASSERT(mutex_owned(SD_MUTEX(un))); 15599 ASSERT(un->un_sense_isbusy == 0); 15600 15601 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 15602 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 15603 15604 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 15605 ASSERT(sense_xp != NULL); 15606 15607 SD_INFO(SD_LOG_IO, un, 15608 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 15609 15610 ASSERT(sense_xp->xb_pktp != NULL); 15611 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 15612 == (FLAG_SENSING | FLAG_HEAD)); 15613 15614 un->un_sense_isbusy = 1; 15615 un->un_rqs_bp->b_resid = 0; 15616 sense_xp->xb_pktp->pkt_resid = 0; 15617 sense_xp->xb_pktp->pkt_reason = 0; 15618 15619 /* So we can get back the bp at interrupt time! */ 15620 sense_xp->xb_sense_bp = bp; 15621 15622 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 15623 15624 /* 15625 * Mark this buf as awaiting sense data. (This is already set in 15626 * the pkt_flags for the RQS packet.) 15627 */ 15628 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 15629 15630 /* Request sense down same path */ 15631 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) && 15632 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance) 15633 sense_xp->xb_pktp->pkt_path_instance = 15634 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance; 15635 15636 sense_xp->xb_retry_count = 0; 15637 sense_xp->xb_victim_retry_count = 0; 15638 sense_xp->xb_ua_retry_count = 0; 15639 sense_xp->xb_nr_retry_count = 0; 15640 sense_xp->xb_dma_resid = 0; 15641 15642 /* Clean up the fields for auto-request sense */ 15643 sense_xp->xb_sense_status = 0; 15644 sense_xp->xb_sense_state = 0; 15645 sense_xp->xb_sense_resid = 0; 15646 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 15647 15648 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 15649 } 15650 15651 15652 /* 15653 * Function: sd_mark_rqs_idle 15654 * 15655 * Description: SD_MUTEX must be held continuously through this routine 15656 * to prevent reuse of the rqs struct before the caller can 15657 * complete it's processing. 15658 * 15659 * Return Code: Pointer to the RQS buf 15660 * 15661 * Context: May be called under interrupt context 15662 */ 15663 15664 static struct buf * 15665 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 15666 { 15667 struct buf *bp; 15668 ASSERT(un != NULL); 15669 ASSERT(sense_xp != NULL); 15670 ASSERT(mutex_owned(SD_MUTEX(un))); 15671 ASSERT(un->un_sense_isbusy != 0); 15672 15673 un->un_sense_isbusy = 0; 15674 bp = sense_xp->xb_sense_bp; 15675 sense_xp->xb_sense_bp = NULL; 15676 15677 /* This pkt is no longer interested in getting sense data */ 15678 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 15679 15680 return (bp); 15681 } 15682 15683 15684 15685 /* 15686 * Function: sd_alloc_rqs 15687 * 15688 * Description: Set up the unit to receive auto request sense data 15689 * 15690 * Return Code: DDI_SUCCESS or DDI_FAILURE 15691 * 15692 * Context: Called under attach(9E) context 15693 */ 15694 15695 static int 15696 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 15697 { 15698 struct sd_xbuf *xp; 15699 15700 ASSERT(un != NULL); 15701 ASSERT(!mutex_owned(SD_MUTEX(un))); 15702 ASSERT(un->un_rqs_bp == NULL); 15703 ASSERT(un->un_rqs_pktp == NULL); 15704 15705 /* 15706 * First allocate the required buf and scsi_pkt structs, then set up 15707 * the CDB in the scsi_pkt for a REQUEST SENSE command. 15708 */ 15709 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 15710 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 15711 if (un->un_rqs_bp == NULL) { 15712 return (DDI_FAILURE); 15713 } 15714 15715 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 15716 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 15717 15718 if (un->un_rqs_pktp == NULL) { 15719 sd_free_rqs(un); 15720 return (DDI_FAILURE); 15721 } 15722 15723 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 15724 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 15725 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 15726 15727 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 15728 15729 /* Set up the other needed members in the ARQ scsi_pkt. */ 15730 un->un_rqs_pktp->pkt_comp = sdintr; 15731 un->un_rqs_pktp->pkt_time = sd_io_time; 15732 un->un_rqs_pktp->pkt_flags |= 15733 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 15734 15735 /* 15736 * Allocate & init the sd_xbuf struct for the RQS command. Do not 15737 * provide any intpkt, destroypkt routines as we take care of 15738 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 15739 */ 15740 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 15741 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 15742 xp->xb_pktp = un->un_rqs_pktp; 15743 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15744 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 15745 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 15746 15747 /* 15748 * Save the pointer to the request sense private bp so it can 15749 * be retrieved in sdintr. 15750 */ 15751 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 15752 ASSERT(un->un_rqs_bp->b_private == xp); 15753 15754 /* 15755 * See if the HBA supports auto-request sense for the specified 15756 * target/lun. If it does, then try to enable it (if not already 15757 * enabled). 15758 * 15759 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 15760 * failure, while for other HBAs (pln) scsi_ifsetcap will always 15761 * return success. However, in both of these cases ARQ is always 15762 * enabled and scsi_ifgetcap will always return true. The best approach 15763 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 15764 * 15765 * The 3rd case is the HBA (adp) always return enabled on 15766 * scsi_ifgetgetcap even when it's not enable, the best approach 15767 * is issue a scsi_ifsetcap then a scsi_ifgetcap 15768 * Note: this case is to circumvent the Adaptec bug. (x86 only) 15769 */ 15770 15771 if (un->un_f_is_fibre == TRUE) { 15772 un->un_f_arq_enabled = TRUE; 15773 } else { 15774 #if defined(__i386) || defined(__amd64) 15775 /* 15776 * Circumvent the Adaptec bug, remove this code when 15777 * the bug is fixed 15778 */ 15779 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 15780 #endif 15781 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 15782 case 0: 15783 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15784 "sd_alloc_rqs: HBA supports ARQ\n"); 15785 /* 15786 * ARQ is supported by this HBA but currently is not 15787 * enabled. Attempt to enable it and if successful then 15788 * mark this instance as ARQ enabled. 15789 */ 15790 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 15791 == 1) { 15792 /* Successfully enabled ARQ in the HBA */ 15793 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15794 "sd_alloc_rqs: ARQ enabled\n"); 15795 un->un_f_arq_enabled = TRUE; 15796 } else { 15797 /* Could not enable ARQ in the HBA */ 15798 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15799 "sd_alloc_rqs: failed ARQ enable\n"); 15800 un->un_f_arq_enabled = FALSE; 15801 } 15802 break; 15803 case 1: 15804 /* 15805 * ARQ is supported by this HBA and is already enabled. 15806 * Just mark ARQ as enabled for this instance. 15807 */ 15808 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15809 "sd_alloc_rqs: ARQ already enabled\n"); 15810 un->un_f_arq_enabled = TRUE; 15811 break; 15812 default: 15813 /* 15814 * ARQ is not supported by this HBA; disable it for this 15815 * instance. 15816 */ 15817 SD_INFO(SD_LOG_ATTACH_DETACH, un, 15818 "sd_alloc_rqs: HBA does not support ARQ\n"); 15819 un->un_f_arq_enabled = FALSE; 15820 break; 15821 } 15822 } 15823 15824 return (DDI_SUCCESS); 15825 } 15826 15827 15828 /* 15829 * Function: sd_free_rqs 15830 * 15831 * Description: Cleanup for the pre-instance RQS command. 15832 * 15833 * Context: Kernel thread context 15834 */ 15835 15836 static void 15837 sd_free_rqs(struct sd_lun *un) 15838 { 15839 ASSERT(un != NULL); 15840 15841 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 15842 15843 /* 15844 * If consistent memory is bound to a scsi_pkt, the pkt 15845 * has to be destroyed *before* freeing the consistent memory. 15846 * Don't change the sequence of this operations. 15847 * scsi_destroy_pkt() might access memory, which isn't allowed, 15848 * after it was freed in scsi_free_consistent_buf(). 15849 */ 15850 if (un->un_rqs_pktp != NULL) { 15851 scsi_destroy_pkt(un->un_rqs_pktp); 15852 un->un_rqs_pktp = NULL; 15853 } 15854 15855 if (un->un_rqs_bp != NULL) { 15856 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 15857 if (xp != NULL) { 15858 kmem_free(xp, sizeof (struct sd_xbuf)); 15859 } 15860 scsi_free_consistent_buf(un->un_rqs_bp); 15861 un->un_rqs_bp = NULL; 15862 } 15863 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 15864 } 15865 15866 15867 15868 /* 15869 * Function: sd_reduce_throttle 15870 * 15871 * Description: Reduces the maximum # of outstanding commands on a 15872 * target to the current number of outstanding commands. 15873 * Queues a tiemout(9F) callback to restore the limit 15874 * after a specified interval has elapsed. 15875 * Typically used when we get a TRAN_BUSY return code 15876 * back from scsi_transport(). 15877 * 15878 * Arguments: un - ptr to the sd_lun softstate struct 15879 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 15880 * 15881 * Context: May be called from interrupt context 15882 */ 15883 15884 static void 15885 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 15886 { 15887 ASSERT(un != NULL); 15888 ASSERT(mutex_owned(SD_MUTEX(un))); 15889 ASSERT(un->un_ncmds_in_transport >= 0); 15890 15891 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15892 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 15893 un, un->un_throttle, un->un_ncmds_in_transport); 15894 15895 if (un->un_throttle > 1) { 15896 if (un->un_f_use_adaptive_throttle == TRUE) { 15897 switch (throttle_type) { 15898 case SD_THROTTLE_TRAN_BUSY: 15899 if (un->un_busy_throttle == 0) { 15900 un->un_busy_throttle = un->un_throttle; 15901 } 15902 break; 15903 case SD_THROTTLE_QFULL: 15904 un->un_busy_throttle = 0; 15905 break; 15906 default: 15907 ASSERT(FALSE); 15908 } 15909 15910 if (un->un_ncmds_in_transport > 0) { 15911 un->un_throttle = un->un_ncmds_in_transport; 15912 } 15913 15914 } else { 15915 if (un->un_ncmds_in_transport == 0) { 15916 un->un_throttle = 1; 15917 } else { 15918 un->un_throttle = un->un_ncmds_in_transport; 15919 } 15920 } 15921 } 15922 15923 /* Reschedule the timeout if none is currently active */ 15924 if (un->un_reset_throttle_timeid == NULL) { 15925 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 15926 un, SD_THROTTLE_RESET_INTERVAL); 15927 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15928 "sd_reduce_throttle: timeout scheduled!\n"); 15929 } 15930 15931 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 15932 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15933 } 15934 15935 15936 15937 /* 15938 * Function: sd_restore_throttle 15939 * 15940 * Description: Callback function for timeout(9F). Resets the current 15941 * value of un->un_throttle to its default. 15942 * 15943 * Arguments: arg - pointer to associated softstate for the device. 15944 * 15945 * Context: May be called from interrupt context 15946 */ 15947 15948 static void 15949 sd_restore_throttle(void *arg) 15950 { 15951 struct sd_lun *un = arg; 15952 15953 ASSERT(un != NULL); 15954 ASSERT(!mutex_owned(SD_MUTEX(un))); 15955 15956 mutex_enter(SD_MUTEX(un)); 15957 15958 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 15959 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 15960 15961 un->un_reset_throttle_timeid = NULL; 15962 15963 if (un->un_f_use_adaptive_throttle == TRUE) { 15964 /* 15965 * If un_busy_throttle is nonzero, then it contains the 15966 * value that un_throttle was when we got a TRAN_BUSY back 15967 * from scsi_transport(). We want to revert back to this 15968 * value. 15969 * 15970 * In the QFULL case, the throttle limit will incrementally 15971 * increase until it reaches max throttle. 15972 */ 15973 if (un->un_busy_throttle > 0) { 15974 un->un_throttle = un->un_busy_throttle; 15975 un->un_busy_throttle = 0; 15976 } else { 15977 /* 15978 * increase throttle by 10% open gate slowly, schedule 15979 * another restore if saved throttle has not been 15980 * reached 15981 */ 15982 short throttle; 15983 if (sd_qfull_throttle_enable) { 15984 throttle = un->un_throttle + 15985 max((un->un_throttle / 10), 1); 15986 un->un_throttle = 15987 (throttle < un->un_saved_throttle) ? 15988 throttle : un->un_saved_throttle; 15989 if (un->un_throttle < un->un_saved_throttle) { 15990 un->un_reset_throttle_timeid = 15991 timeout(sd_restore_throttle, 15992 un, 15993 SD_QFULL_THROTTLE_RESET_INTERVAL); 15994 } 15995 } 15996 } 15997 15998 /* 15999 * If un_throttle has fallen below the low-water mark, we 16000 * restore the maximum value here (and allow it to ratchet 16001 * down again if necessary). 16002 */ 16003 if (un->un_throttle < un->un_min_throttle) { 16004 un->un_throttle = un->un_saved_throttle; 16005 } 16006 } else { 16007 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16008 "restoring limit from 0x%x to 0x%x\n", 16009 un->un_throttle, un->un_saved_throttle); 16010 un->un_throttle = un->un_saved_throttle; 16011 } 16012 16013 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16014 "sd_restore_throttle: calling sd_start_cmds!\n"); 16015 16016 sd_start_cmds(un, NULL); 16017 16018 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16019 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16020 un, un->un_throttle); 16021 16022 mutex_exit(SD_MUTEX(un)); 16023 16024 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16025 } 16026 16027 /* 16028 * Function: sdrunout 16029 * 16030 * Description: Callback routine for scsi_init_pkt when a resource allocation 16031 * fails. 16032 * 16033 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16034 * soft state instance. 16035 * 16036 * Return Code: The scsi_init_pkt routine allows for the callback function to 16037 * return a 0 indicating the callback should be rescheduled or a 1 16038 * indicating not to reschedule. This routine always returns 1 16039 * because the driver always provides a callback function to 16040 * scsi_init_pkt. This results in a callback always being scheduled 16041 * (via the scsi_init_pkt callback implementation) if a resource 16042 * failure occurs. 16043 * 16044 * Context: This callback function may not block or call routines that block 16045 * 16046 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16047 * request persisting at the head of the list which cannot be 16048 * satisfied even after multiple retries. In the future the driver 16049 * may implement some time of maximum runout count before failing 16050 * an I/O. 16051 */ 16052 16053 static int 16054 sdrunout(caddr_t arg) 16055 { 16056 struct sd_lun *un = (struct sd_lun *)arg; 16057 16058 ASSERT(un != NULL); 16059 ASSERT(!mutex_owned(SD_MUTEX(un))); 16060 16061 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16062 16063 mutex_enter(SD_MUTEX(un)); 16064 sd_start_cmds(un, NULL); 16065 mutex_exit(SD_MUTEX(un)); 16066 /* 16067 * This callback routine always returns 1 (i.e. do not reschedule) 16068 * because we always specify sdrunout as the callback handler for 16069 * scsi_init_pkt inside the call to sd_start_cmds. 16070 */ 16071 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16072 return (1); 16073 } 16074 16075 16076 /* 16077 * Function: sdintr 16078 * 16079 * Description: Completion callback routine for scsi_pkt(9S) structs 16080 * sent to the HBA driver via scsi_transport(9F). 16081 * 16082 * Context: Interrupt context 16083 */ 16084 16085 static void 16086 sdintr(struct scsi_pkt *pktp) 16087 { 16088 struct buf *bp; 16089 struct sd_xbuf *xp; 16090 struct sd_lun *un; 16091 size_t actual_len; 16092 sd_ssc_t *sscp; 16093 16094 ASSERT(pktp != NULL); 16095 bp = (struct buf *)pktp->pkt_private; 16096 ASSERT(bp != NULL); 16097 xp = SD_GET_XBUF(bp); 16098 ASSERT(xp != NULL); 16099 ASSERT(xp->xb_pktp != NULL); 16100 un = SD_GET_UN(bp); 16101 ASSERT(un != NULL); 16102 ASSERT(!mutex_owned(SD_MUTEX(un))); 16103 16104 #ifdef SD_FAULT_INJECTION 16105 16106 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16107 /* SD FaultInjection */ 16108 sd_faultinjection(pktp); 16109 16110 #endif /* SD_FAULT_INJECTION */ 16111 16112 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16113 " xp:0x%p, un:0x%p\n", bp, xp, un); 16114 16115 mutex_enter(SD_MUTEX(un)); 16116 16117 ASSERT(un->un_fm_private != NULL); 16118 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 16119 ASSERT(sscp != NULL); 16120 16121 /* Reduce the count of the #commands currently in transport */ 16122 un->un_ncmds_in_transport--; 16123 ASSERT(un->un_ncmds_in_transport >= 0); 16124 16125 /* Increment counter to indicate that the callback routine is active */ 16126 un->un_in_callback++; 16127 16128 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16129 16130 #ifdef SDDEBUG 16131 if (bp == un->un_retry_bp) { 16132 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16133 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16134 un, un->un_retry_bp, un->un_ncmds_in_transport); 16135 } 16136 #endif 16137 16138 /* 16139 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 16140 * state if needed. 16141 */ 16142 if (pktp->pkt_reason == CMD_DEV_GONE) { 16143 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16144 "Command failed to complete...Device is gone\n"); 16145 if (un->un_mediastate != DKIO_DEV_GONE) { 16146 un->un_mediastate = DKIO_DEV_GONE; 16147 cv_broadcast(&un->un_state_cv); 16148 } 16149 sd_return_failed_command(un, bp, EIO); 16150 goto exit; 16151 } 16152 16153 if (pktp->pkt_state & STATE_XARQ_DONE) { 16154 SD_TRACE(SD_LOG_COMMON, un, 16155 "sdintr: extra sense data received. pkt=%p\n", pktp); 16156 } 16157 16158 /* 16159 * First see if the pkt has auto-request sense data with it.... 16160 * Look at the packet state first so we don't take a performance 16161 * hit looking at the arq enabled flag unless absolutely necessary. 16162 */ 16163 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16164 (un->un_f_arq_enabled == TRUE)) { 16165 /* 16166 * The HBA did an auto request sense for this command so check 16167 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16168 * driver command that should not be retried. 16169 */ 16170 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16171 /* 16172 * Save the relevant sense info into the xp for the 16173 * original cmd. 16174 */ 16175 struct scsi_arq_status *asp; 16176 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16177 xp->xb_sense_status = 16178 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16179 xp->xb_sense_state = asp->sts_rqpkt_state; 16180 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16181 if (pktp->pkt_state & STATE_XARQ_DONE) { 16182 actual_len = MAX_SENSE_LENGTH - 16183 xp->xb_sense_resid; 16184 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16185 MAX_SENSE_LENGTH); 16186 } else { 16187 if (xp->xb_sense_resid > SENSE_LENGTH) { 16188 actual_len = MAX_SENSE_LENGTH - 16189 xp->xb_sense_resid; 16190 } else { 16191 actual_len = SENSE_LENGTH - 16192 xp->xb_sense_resid; 16193 } 16194 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16195 if ((((struct uscsi_cmd *) 16196 (xp->xb_pktinfo))->uscsi_rqlen) > 16197 actual_len) { 16198 xp->xb_sense_resid = 16199 (((struct uscsi_cmd *) 16200 (xp->xb_pktinfo))-> 16201 uscsi_rqlen) - actual_len; 16202 } else { 16203 xp->xb_sense_resid = 0; 16204 } 16205 } 16206 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16207 SENSE_LENGTH); 16208 } 16209 16210 /* fail the command */ 16211 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16212 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16213 sd_return_failed_command(un, bp, EIO); 16214 goto exit; 16215 } 16216 16217 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16218 /* 16219 * We want to either retry or fail this command, so free 16220 * the DMA resources here. If we retry the command then 16221 * the DMA resources will be reallocated in sd_start_cmds(). 16222 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16223 * causes the *entire* transfer to start over again from the 16224 * beginning of the request, even for PARTIAL chunks that 16225 * have already transferred successfully. 16226 */ 16227 if ((un->un_f_is_fibre == TRUE) && 16228 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16229 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16230 scsi_dmafree(pktp); 16231 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16232 } 16233 #endif 16234 16235 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16236 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16237 16238 sd_handle_auto_request_sense(un, bp, xp, pktp); 16239 goto exit; 16240 } 16241 16242 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16243 if (pktp->pkt_flags & FLAG_SENSING) { 16244 /* This pktp is from the unit's REQUEST_SENSE command */ 16245 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16246 "sdintr: sd_handle_request_sense\n"); 16247 sd_handle_request_sense(un, bp, xp, pktp); 16248 goto exit; 16249 } 16250 16251 /* 16252 * Check to see if the command successfully completed as requested; 16253 * this is the most common case (and also the hot performance path). 16254 * 16255 * Requirements for successful completion are: 16256 * pkt_reason is CMD_CMPLT and packet status is status good. 16257 * In addition: 16258 * - A residual of zero indicates successful completion no matter what 16259 * the command is. 16260 * - If the residual is not zero and the command is not a read or 16261 * write, then it's still defined as successful completion. In other 16262 * words, if the command is a read or write the residual must be 16263 * zero for successful completion. 16264 * - If the residual is not zero and the command is a read or 16265 * write, and it's a USCSICMD, then it's still defined as 16266 * successful completion. 16267 */ 16268 if ((pktp->pkt_reason == CMD_CMPLT) && 16269 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16270 16271 /* 16272 * Since this command is returned with a good status, we 16273 * can reset the count for Sonoma failover. 16274 */ 16275 un->un_sonoma_failure_count = 0; 16276 16277 /* 16278 * Return all USCSI commands on good status 16279 */ 16280 if (pktp->pkt_resid == 0) { 16281 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16282 "sdintr: returning command for resid == 0\n"); 16283 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16284 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16285 SD_UPDATE_B_RESID(bp, pktp); 16286 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16287 "sdintr: returning command for resid != 0\n"); 16288 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16289 SD_UPDATE_B_RESID(bp, pktp); 16290 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16291 "sdintr: returning uscsi command\n"); 16292 } else { 16293 goto not_successful; 16294 } 16295 sd_return_command(un, bp); 16296 16297 /* 16298 * Decrement counter to indicate that the callback routine 16299 * is done. 16300 */ 16301 un->un_in_callback--; 16302 ASSERT(un->un_in_callback >= 0); 16303 mutex_exit(SD_MUTEX(un)); 16304 16305 return; 16306 } 16307 16308 not_successful: 16309 16310 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16311 /* 16312 * The following is based upon knowledge of the underlying transport 16313 * and its use of DMA resources. This code should be removed when 16314 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16315 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16316 * and sd_start_cmds(). 16317 * 16318 * Free any DMA resources associated with this command if there 16319 * is a chance it could be retried or enqueued for later retry. 16320 * If we keep the DMA binding then mpxio cannot reissue the 16321 * command on another path whenever a path failure occurs. 16322 * 16323 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16324 * causes the *entire* transfer to start over again from the 16325 * beginning of the request, even for PARTIAL chunks that 16326 * have already transferred successfully. 16327 * 16328 * This is only done for non-uscsi commands (and also skipped for the 16329 * driver's internal RQS command). Also just do this for Fibre Channel 16330 * devices as these are the only ones that support mpxio. 16331 */ 16332 if ((un->un_f_is_fibre == TRUE) && 16333 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16334 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16335 scsi_dmafree(pktp); 16336 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16337 } 16338 #endif 16339 16340 /* 16341 * The command did not successfully complete as requested so check 16342 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16343 * driver command that should not be retried so just return. If 16344 * FLAG_DIAGNOSE is not set the error will be processed below. 16345 */ 16346 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16347 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16348 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16349 /* 16350 * Issue a request sense if a check condition caused the error 16351 * (we handle the auto request sense case above), otherwise 16352 * just fail the command. 16353 */ 16354 if ((pktp->pkt_reason == CMD_CMPLT) && 16355 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16356 sd_send_request_sense_command(un, bp, pktp); 16357 } else { 16358 sd_return_failed_command(un, bp, EIO); 16359 } 16360 goto exit; 16361 } 16362 16363 /* 16364 * The command did not successfully complete as requested so process 16365 * the error, retry, and/or attempt recovery. 16366 */ 16367 switch (pktp->pkt_reason) { 16368 case CMD_CMPLT: 16369 switch (SD_GET_PKT_STATUS(pktp)) { 16370 case STATUS_GOOD: 16371 /* 16372 * The command completed successfully with a non-zero 16373 * residual 16374 */ 16375 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16376 "sdintr: STATUS_GOOD \n"); 16377 sd_pkt_status_good(un, bp, xp, pktp); 16378 break; 16379 16380 case STATUS_CHECK: 16381 case STATUS_TERMINATED: 16382 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16383 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16384 sd_pkt_status_check_condition(un, bp, xp, pktp); 16385 break; 16386 16387 case STATUS_BUSY: 16388 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16389 "sdintr: STATUS_BUSY\n"); 16390 sd_pkt_status_busy(un, bp, xp, pktp); 16391 break; 16392 16393 case STATUS_RESERVATION_CONFLICT: 16394 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16395 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16396 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16397 break; 16398 16399 case STATUS_QFULL: 16400 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16401 "sdintr: STATUS_QFULL\n"); 16402 sd_pkt_status_qfull(un, bp, xp, pktp); 16403 break; 16404 16405 case STATUS_MET: 16406 case STATUS_INTERMEDIATE: 16407 case STATUS_SCSI2: 16408 case STATUS_INTERMEDIATE_MET: 16409 case STATUS_ACA_ACTIVE: 16410 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16411 "Unexpected SCSI status received: 0x%x\n", 16412 SD_GET_PKT_STATUS(pktp)); 16413 /* 16414 * Mark the ssc_flags when detected invalid status 16415 * code for non-USCSI command. 16416 */ 16417 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 16418 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 16419 0, "stat-code"); 16420 } 16421 sd_return_failed_command(un, bp, EIO); 16422 break; 16423 16424 default: 16425 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16426 "Invalid SCSI status received: 0x%x\n", 16427 SD_GET_PKT_STATUS(pktp)); 16428 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 16429 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS, 16430 0, "stat-code"); 16431 } 16432 sd_return_failed_command(un, bp, EIO); 16433 break; 16434 16435 } 16436 break; 16437 16438 case CMD_INCOMPLETE: 16439 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16440 "sdintr: CMD_INCOMPLETE\n"); 16441 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16442 break; 16443 case CMD_TRAN_ERR: 16444 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16445 "sdintr: CMD_TRAN_ERR\n"); 16446 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16447 break; 16448 case CMD_RESET: 16449 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16450 "sdintr: CMD_RESET \n"); 16451 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16452 break; 16453 case CMD_ABORTED: 16454 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16455 "sdintr: CMD_ABORTED \n"); 16456 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16457 break; 16458 case CMD_TIMEOUT: 16459 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16460 "sdintr: CMD_TIMEOUT\n"); 16461 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16462 break; 16463 case CMD_UNX_BUS_FREE: 16464 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16465 "sdintr: CMD_UNX_BUS_FREE \n"); 16466 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16467 break; 16468 case CMD_TAG_REJECT: 16469 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16470 "sdintr: CMD_TAG_REJECT\n"); 16471 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16472 break; 16473 default: 16474 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16475 "sdintr: default\n"); 16476 /* 16477 * Mark the ssc_flags for detecting invliad pkt_reason. 16478 */ 16479 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 16480 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON, 16481 0, "pkt-reason"); 16482 } 16483 sd_pkt_reason_default(un, bp, xp, pktp); 16484 break; 16485 } 16486 16487 exit: 16488 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16489 16490 /* Decrement counter to indicate that the callback routine is done. */ 16491 un->un_in_callback--; 16492 ASSERT(un->un_in_callback >= 0); 16493 16494 /* 16495 * At this point, the pkt has been dispatched, ie, it is either 16496 * being re-tried or has been returned to its caller and should 16497 * not be referenced. 16498 */ 16499 16500 mutex_exit(SD_MUTEX(un)); 16501 } 16502 16503 16504 /* 16505 * Function: sd_print_incomplete_msg 16506 * 16507 * Description: Prints the error message for a CMD_INCOMPLETE error. 16508 * 16509 * Arguments: un - ptr to associated softstate for the device. 16510 * bp - ptr to the buf(9S) for the command. 16511 * arg - message string ptr 16512 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16513 * or SD_NO_RETRY_ISSUED. 16514 * 16515 * Context: May be called under interrupt context 16516 */ 16517 16518 static void 16519 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16520 { 16521 struct scsi_pkt *pktp; 16522 char *msgp; 16523 char *cmdp = arg; 16524 16525 ASSERT(un != NULL); 16526 ASSERT(mutex_owned(SD_MUTEX(un))); 16527 ASSERT(bp != NULL); 16528 ASSERT(arg != NULL); 16529 pktp = SD_GET_PKTP(bp); 16530 ASSERT(pktp != NULL); 16531 16532 switch (code) { 16533 case SD_DELAYED_RETRY_ISSUED: 16534 case SD_IMMEDIATE_RETRY_ISSUED: 16535 msgp = "retrying"; 16536 break; 16537 case SD_NO_RETRY_ISSUED: 16538 default: 16539 msgp = "giving up"; 16540 break; 16541 } 16542 16543 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16544 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16545 "incomplete %s- %s\n", cmdp, msgp); 16546 } 16547 } 16548 16549 16550 16551 /* 16552 * Function: sd_pkt_status_good 16553 * 16554 * Description: Processing for a STATUS_GOOD code in pkt_status. 16555 * 16556 * Context: May be called under interrupt context 16557 */ 16558 16559 static void 16560 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 16561 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16562 { 16563 char *cmdp; 16564 16565 ASSERT(un != NULL); 16566 ASSERT(mutex_owned(SD_MUTEX(un))); 16567 ASSERT(bp != NULL); 16568 ASSERT(xp != NULL); 16569 ASSERT(pktp != NULL); 16570 ASSERT(pktp->pkt_reason == CMD_CMPLT); 16571 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 16572 ASSERT(pktp->pkt_resid != 0); 16573 16574 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 16575 16576 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16577 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 16578 case SCMD_READ: 16579 cmdp = "read"; 16580 break; 16581 case SCMD_WRITE: 16582 cmdp = "write"; 16583 break; 16584 default: 16585 SD_UPDATE_B_RESID(bp, pktp); 16586 sd_return_command(un, bp); 16587 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16588 return; 16589 } 16590 16591 /* 16592 * See if we can retry the read/write, preferrably immediately. 16593 * If retries are exhaused, then sd_retry_command() will update 16594 * the b_resid count. 16595 */ 16596 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 16597 cmdp, EIO, (clock_t)0, NULL); 16598 16599 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 16600 } 16601 16602 16603 16604 16605 16606 /* 16607 * Function: sd_handle_request_sense 16608 * 16609 * Description: Processing for non-auto Request Sense command. 16610 * 16611 * Arguments: un - ptr to associated softstate 16612 * sense_bp - ptr to buf(9S) for the RQS command 16613 * sense_xp - ptr to the sd_xbuf for the RQS command 16614 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 16615 * 16616 * Context: May be called under interrupt context 16617 */ 16618 16619 static void 16620 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 16621 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 16622 { 16623 struct buf *cmd_bp; /* buf for the original command */ 16624 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 16625 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 16626 size_t actual_len; /* actual sense data length */ 16627 16628 ASSERT(un != NULL); 16629 ASSERT(mutex_owned(SD_MUTEX(un))); 16630 ASSERT(sense_bp != NULL); 16631 ASSERT(sense_xp != NULL); 16632 ASSERT(sense_pktp != NULL); 16633 16634 /* 16635 * Note the sense_bp, sense_xp, and sense_pktp here are for the 16636 * RQS command and not the original command. 16637 */ 16638 ASSERT(sense_pktp == un->un_rqs_pktp); 16639 ASSERT(sense_bp == un->un_rqs_bp); 16640 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 16641 (FLAG_SENSING | FLAG_HEAD)); 16642 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 16643 FLAG_SENSING) == FLAG_SENSING); 16644 16645 /* These are the bp, xp, and pktp for the original command */ 16646 cmd_bp = sense_xp->xb_sense_bp; 16647 cmd_xp = SD_GET_XBUF(cmd_bp); 16648 cmd_pktp = SD_GET_PKTP(cmd_bp); 16649 16650 if (sense_pktp->pkt_reason != CMD_CMPLT) { 16651 /* 16652 * The REQUEST SENSE command failed. Release the REQUEST 16653 * SENSE command for re-use, get back the bp for the original 16654 * command, and attempt to re-try the original command if 16655 * FLAG_DIAGNOSE is not set in the original packet. 16656 */ 16657 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16658 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16659 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 16660 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 16661 NULL, NULL, EIO, (clock_t)0, NULL); 16662 return; 16663 } 16664 } 16665 16666 /* 16667 * Save the relevant sense info into the xp for the original cmd. 16668 * 16669 * Note: if the request sense failed the state info will be zero 16670 * as set in sd_mark_rqs_busy() 16671 */ 16672 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 16673 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 16674 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 16675 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 16676 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 16677 SENSE_LENGTH)) { 16678 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 16679 MAX_SENSE_LENGTH); 16680 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 16681 } else { 16682 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 16683 SENSE_LENGTH); 16684 if (actual_len < SENSE_LENGTH) { 16685 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 16686 } else { 16687 cmd_xp->xb_sense_resid = 0; 16688 } 16689 } 16690 16691 /* 16692 * Free up the RQS command.... 16693 * NOTE: 16694 * Must do this BEFORE calling sd_validate_sense_data! 16695 * sd_validate_sense_data may return the original command in 16696 * which case the pkt will be freed and the flags can no 16697 * longer be touched. 16698 * SD_MUTEX is held through this process until the command 16699 * is dispatched based upon the sense data, so there are 16700 * no race conditions. 16701 */ 16702 (void) sd_mark_rqs_idle(un, sense_xp); 16703 16704 /* 16705 * For a retryable command see if we have valid sense data, if so then 16706 * turn it over to sd_decode_sense() to figure out the right course of 16707 * action. Just fail a non-retryable command. 16708 */ 16709 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16710 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 16711 SD_SENSE_DATA_IS_VALID) { 16712 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 16713 } 16714 } else { 16715 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 16716 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 16717 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 16718 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 16719 sd_return_failed_command(un, cmd_bp, EIO); 16720 } 16721 } 16722 16723 16724 16725 16726 /* 16727 * Function: sd_handle_auto_request_sense 16728 * 16729 * Description: Processing for auto-request sense information. 16730 * 16731 * Arguments: un - ptr to associated softstate 16732 * bp - ptr to buf(9S) for the command 16733 * xp - ptr to the sd_xbuf for the command 16734 * pktp - ptr to the scsi_pkt(9S) for the command 16735 * 16736 * Context: May be called under interrupt context 16737 */ 16738 16739 static void 16740 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 16741 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16742 { 16743 struct scsi_arq_status *asp; 16744 size_t actual_len; 16745 16746 ASSERT(un != NULL); 16747 ASSERT(mutex_owned(SD_MUTEX(un))); 16748 ASSERT(bp != NULL); 16749 ASSERT(xp != NULL); 16750 ASSERT(pktp != NULL); 16751 ASSERT(pktp != un->un_rqs_pktp); 16752 ASSERT(bp != un->un_rqs_bp); 16753 16754 /* 16755 * For auto-request sense, we get a scsi_arq_status back from 16756 * the HBA, with the sense data in the sts_sensedata member. 16757 * The pkt_scbp of the packet points to this scsi_arq_status. 16758 */ 16759 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16760 16761 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 16762 /* 16763 * The auto REQUEST SENSE failed; see if we can re-try 16764 * the original command. 16765 */ 16766 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16767 "auto request sense failed (reason=%s)\n", 16768 scsi_rname(asp->sts_rqpkt_reason)); 16769 16770 sd_reset_target(un, pktp); 16771 16772 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 16773 NULL, NULL, EIO, (clock_t)0, NULL); 16774 return; 16775 } 16776 16777 /* Save the relevant sense info into the xp for the original cmd. */ 16778 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 16779 xp->xb_sense_state = asp->sts_rqpkt_state; 16780 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16781 if (xp->xb_sense_state & STATE_XARQ_DONE) { 16782 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 16783 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16784 MAX_SENSE_LENGTH); 16785 } else { 16786 if (xp->xb_sense_resid > SENSE_LENGTH) { 16787 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 16788 } else { 16789 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 16790 } 16791 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16792 if ((((struct uscsi_cmd *) 16793 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) { 16794 xp->xb_sense_resid = (((struct uscsi_cmd *) 16795 (xp->xb_pktinfo))->uscsi_rqlen) - 16796 actual_len; 16797 } else { 16798 xp->xb_sense_resid = 0; 16799 } 16800 } 16801 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 16802 } 16803 16804 /* 16805 * See if we have valid sense data, if so then turn it over to 16806 * sd_decode_sense() to figure out the right course of action. 16807 */ 16808 if (sd_validate_sense_data(un, bp, xp, actual_len) == 16809 SD_SENSE_DATA_IS_VALID) { 16810 sd_decode_sense(un, bp, xp, pktp); 16811 } 16812 } 16813 16814 16815 /* 16816 * Function: sd_print_sense_failed_msg 16817 * 16818 * Description: Print log message when RQS has failed. 16819 * 16820 * Arguments: un - ptr to associated softstate 16821 * bp - ptr to buf(9S) for the command 16822 * arg - generic message string ptr 16823 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16824 * or SD_NO_RETRY_ISSUED 16825 * 16826 * Context: May be called from interrupt context 16827 */ 16828 16829 static void 16830 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 16831 int code) 16832 { 16833 char *msgp = arg; 16834 16835 ASSERT(un != NULL); 16836 ASSERT(mutex_owned(SD_MUTEX(un))); 16837 ASSERT(bp != NULL); 16838 16839 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 16840 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 16841 } 16842 } 16843 16844 16845 /* 16846 * Function: sd_validate_sense_data 16847 * 16848 * Description: Check the given sense data for validity. 16849 * If the sense data is not valid, the command will 16850 * be either failed or retried! 16851 * 16852 * Return Code: SD_SENSE_DATA_IS_INVALID 16853 * SD_SENSE_DATA_IS_VALID 16854 * 16855 * Context: May be called from interrupt context 16856 */ 16857 16858 static int 16859 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 16860 size_t actual_len) 16861 { 16862 struct scsi_extended_sense *esp; 16863 struct scsi_pkt *pktp; 16864 char *msgp = NULL; 16865 sd_ssc_t *sscp; 16866 16867 ASSERT(un != NULL); 16868 ASSERT(mutex_owned(SD_MUTEX(un))); 16869 ASSERT(bp != NULL); 16870 ASSERT(bp != un->un_rqs_bp); 16871 ASSERT(xp != NULL); 16872 ASSERT(un->un_fm_private != NULL); 16873 16874 pktp = SD_GET_PKTP(bp); 16875 ASSERT(pktp != NULL); 16876 16877 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc; 16878 ASSERT(sscp != NULL); 16879 16880 /* 16881 * Check the status of the RQS command (auto or manual). 16882 */ 16883 switch (xp->xb_sense_status & STATUS_MASK) { 16884 case STATUS_GOOD: 16885 break; 16886 16887 case STATUS_RESERVATION_CONFLICT: 16888 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16889 return (SD_SENSE_DATA_IS_INVALID); 16890 16891 case STATUS_BUSY: 16892 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16893 "Busy Status on REQUEST SENSE\n"); 16894 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 16895 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 16896 return (SD_SENSE_DATA_IS_INVALID); 16897 16898 case STATUS_QFULL: 16899 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16900 "QFULL Status on REQUEST SENSE\n"); 16901 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 16902 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter); 16903 return (SD_SENSE_DATA_IS_INVALID); 16904 16905 case STATUS_CHECK: 16906 case STATUS_TERMINATED: 16907 msgp = "Check Condition on REQUEST SENSE\n"; 16908 goto sense_failed; 16909 16910 default: 16911 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 16912 goto sense_failed; 16913 } 16914 16915 /* 16916 * See if we got the minimum required amount of sense data. 16917 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 16918 * or less. 16919 */ 16920 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 16921 (actual_len == 0)) { 16922 msgp = "Request Sense couldn't get sense data\n"; 16923 goto sense_failed; 16924 } 16925 16926 if (actual_len < SUN_MIN_SENSE_LENGTH) { 16927 msgp = "Not enough sense information\n"; 16928 /* Mark the ssc_flags for detecting invalid sense data */ 16929 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 16930 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 16931 "sense-data"); 16932 } 16933 goto sense_failed; 16934 } 16935 16936 /* 16937 * We require the extended sense data 16938 */ 16939 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 16940 if (esp->es_class != CLASS_EXTENDED_SENSE) { 16941 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16942 static char tmp[8]; 16943 static char buf[148]; 16944 char *p = (char *)(xp->xb_sense_data); 16945 int i; 16946 16947 mutex_enter(&sd_sense_mutex); 16948 (void) strcpy(buf, "undecodable sense information:"); 16949 for (i = 0; i < actual_len; i++) { 16950 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 16951 (void) strcpy(&buf[strlen(buf)], tmp); 16952 } 16953 i = strlen(buf); 16954 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 16955 16956 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 16957 scsi_log(SD_DEVINFO(un), sd_label, 16958 CE_WARN, buf); 16959 } 16960 mutex_exit(&sd_sense_mutex); 16961 } 16962 16963 /* Mark the ssc_flags for detecting invalid sense data */ 16964 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 16965 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 16966 "sense-data"); 16967 } 16968 16969 /* Note: Legacy behavior, fail the command with no retry */ 16970 sd_return_failed_command(un, bp, EIO); 16971 return (SD_SENSE_DATA_IS_INVALID); 16972 } 16973 16974 /* 16975 * Check that es_code is valid (es_class concatenated with es_code 16976 * make up the "response code" field. es_class will always be 7, so 16977 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 16978 * format. 16979 */ 16980 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 16981 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 16982 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 16983 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 16984 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 16985 /* Mark the ssc_flags for detecting invalid sense data */ 16986 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) { 16987 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0, 16988 "sense-data"); 16989 } 16990 goto sense_failed; 16991 } 16992 16993 return (SD_SENSE_DATA_IS_VALID); 16994 16995 sense_failed: 16996 /* 16997 * If the request sense failed (for whatever reason), attempt 16998 * to retry the original command. 16999 */ 17000 #if defined(__i386) || defined(__amd64) 17001 /* 17002 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17003 * sddef.h for Sparc platform, and x86 uses 1 binary 17004 * for both SCSI/FC. 17005 * The SD_RETRY_DELAY value need to be adjusted here 17006 * when SD_RETRY_DELAY change in sddef.h 17007 */ 17008 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17009 sd_print_sense_failed_msg, msgp, EIO, 17010 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17011 #else 17012 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17013 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17014 #endif 17015 17016 return (SD_SENSE_DATA_IS_INVALID); 17017 } 17018 17019 /* 17020 * Function: sd_decode_sense 17021 * 17022 * Description: Take recovery action(s) when SCSI Sense Data is received. 17023 * 17024 * Context: Interrupt context. 17025 */ 17026 17027 static void 17028 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17029 struct scsi_pkt *pktp) 17030 { 17031 uint8_t sense_key; 17032 17033 ASSERT(un != NULL); 17034 ASSERT(mutex_owned(SD_MUTEX(un))); 17035 ASSERT(bp != NULL); 17036 ASSERT(bp != un->un_rqs_bp); 17037 ASSERT(xp != NULL); 17038 ASSERT(pktp != NULL); 17039 17040 sense_key = scsi_sense_key(xp->xb_sense_data); 17041 17042 switch (sense_key) { 17043 case KEY_NO_SENSE: 17044 sd_sense_key_no_sense(un, bp, xp, pktp); 17045 break; 17046 case KEY_RECOVERABLE_ERROR: 17047 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17048 bp, xp, pktp); 17049 break; 17050 case KEY_NOT_READY: 17051 sd_sense_key_not_ready(un, xp->xb_sense_data, 17052 bp, xp, pktp); 17053 break; 17054 case KEY_MEDIUM_ERROR: 17055 case KEY_HARDWARE_ERROR: 17056 sd_sense_key_medium_or_hardware_error(un, 17057 xp->xb_sense_data, bp, xp, pktp); 17058 break; 17059 case KEY_ILLEGAL_REQUEST: 17060 sd_sense_key_illegal_request(un, bp, xp, pktp); 17061 break; 17062 case KEY_UNIT_ATTENTION: 17063 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17064 bp, xp, pktp); 17065 break; 17066 case KEY_WRITE_PROTECT: 17067 case KEY_VOLUME_OVERFLOW: 17068 case KEY_MISCOMPARE: 17069 sd_sense_key_fail_command(un, bp, xp, pktp); 17070 break; 17071 case KEY_BLANK_CHECK: 17072 sd_sense_key_blank_check(un, bp, xp, pktp); 17073 break; 17074 case KEY_ABORTED_COMMAND: 17075 sd_sense_key_aborted_command(un, bp, xp, pktp); 17076 break; 17077 case KEY_VENDOR_UNIQUE: 17078 case KEY_COPY_ABORTED: 17079 case KEY_EQUAL: 17080 case KEY_RESERVED: 17081 default: 17082 sd_sense_key_default(un, xp->xb_sense_data, 17083 bp, xp, pktp); 17084 break; 17085 } 17086 } 17087 17088 17089 /* 17090 * Function: sd_dump_memory 17091 * 17092 * Description: Debug logging routine to print the contents of a user provided 17093 * buffer. The output of the buffer is broken up into 256 byte 17094 * segments due to a size constraint of the scsi_log. 17095 * implementation. 17096 * 17097 * Arguments: un - ptr to softstate 17098 * comp - component mask 17099 * title - "title" string to preceed data when printed 17100 * data - ptr to data block to be printed 17101 * len - size of data block to be printed 17102 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17103 * 17104 * Context: May be called from interrupt context 17105 */ 17106 17107 #define SD_DUMP_MEMORY_BUF_SIZE 256 17108 17109 static char *sd_dump_format_string[] = { 17110 " 0x%02x", 17111 " %c" 17112 }; 17113 17114 static void 17115 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17116 int len, int fmt) 17117 { 17118 int i, j; 17119 int avail_count; 17120 int start_offset; 17121 int end_offset; 17122 size_t entry_len; 17123 char *bufp; 17124 char *local_buf; 17125 char *format_string; 17126 17127 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17128 17129 /* 17130 * In the debug version of the driver, this function is called from a 17131 * number of places which are NOPs in the release driver. 17132 * The debug driver therefore has additional methods of filtering 17133 * debug output. 17134 */ 17135 #ifdef SDDEBUG 17136 /* 17137 * In the debug version of the driver we can reduce the amount of debug 17138 * messages by setting sd_error_level to something other than 17139 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17140 * sd_component_mask. 17141 */ 17142 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17143 (sd_error_level != SCSI_ERR_ALL)) { 17144 return; 17145 } 17146 if (((sd_component_mask & comp) == 0) || 17147 (sd_error_level != SCSI_ERR_ALL)) { 17148 return; 17149 } 17150 #else 17151 if (sd_error_level != SCSI_ERR_ALL) { 17152 return; 17153 } 17154 #endif 17155 17156 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17157 bufp = local_buf; 17158 /* 17159 * Available length is the length of local_buf[], minus the 17160 * length of the title string, minus one for the ":", minus 17161 * one for the newline, minus one for the NULL terminator. 17162 * This gives the #bytes available for holding the printed 17163 * values from the given data buffer. 17164 */ 17165 if (fmt == SD_LOG_HEX) { 17166 format_string = sd_dump_format_string[0]; 17167 } else /* SD_LOG_CHAR */ { 17168 format_string = sd_dump_format_string[1]; 17169 } 17170 /* 17171 * Available count is the number of elements from the given 17172 * data buffer that we can fit into the available length. 17173 * This is based upon the size of the format string used. 17174 * Make one entry and find it's size. 17175 */ 17176 (void) sprintf(bufp, format_string, data[0]); 17177 entry_len = strlen(bufp); 17178 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17179 17180 j = 0; 17181 while (j < len) { 17182 bufp = local_buf; 17183 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17184 start_offset = j; 17185 17186 end_offset = start_offset + avail_count; 17187 17188 (void) sprintf(bufp, "%s:", title); 17189 bufp += strlen(bufp); 17190 for (i = start_offset; ((i < end_offset) && (j < len)); 17191 i++, j++) { 17192 (void) sprintf(bufp, format_string, data[i]); 17193 bufp += entry_len; 17194 } 17195 (void) sprintf(bufp, "\n"); 17196 17197 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17198 } 17199 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17200 } 17201 17202 /* 17203 * Function: sd_print_sense_msg 17204 * 17205 * Description: Log a message based upon the given sense data. 17206 * 17207 * Arguments: un - ptr to associated softstate 17208 * bp - ptr to buf(9S) for the command 17209 * arg - ptr to associate sd_sense_info struct 17210 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17211 * or SD_NO_RETRY_ISSUED 17212 * 17213 * Context: May be called from interrupt context 17214 */ 17215 17216 static void 17217 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17218 { 17219 struct sd_xbuf *xp; 17220 struct scsi_pkt *pktp; 17221 uint8_t *sensep; 17222 daddr_t request_blkno; 17223 diskaddr_t err_blkno; 17224 int severity; 17225 int pfa_flag; 17226 extern struct scsi_key_strings scsi_cmds[]; 17227 17228 ASSERT(un != NULL); 17229 ASSERT(mutex_owned(SD_MUTEX(un))); 17230 ASSERT(bp != NULL); 17231 xp = SD_GET_XBUF(bp); 17232 ASSERT(xp != NULL); 17233 pktp = SD_GET_PKTP(bp); 17234 ASSERT(pktp != NULL); 17235 ASSERT(arg != NULL); 17236 17237 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17238 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17239 17240 if ((code == SD_DELAYED_RETRY_ISSUED) || 17241 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17242 severity = SCSI_ERR_RETRYABLE; 17243 } 17244 17245 /* Use absolute block number for the request block number */ 17246 request_blkno = xp->xb_blkno; 17247 17248 /* 17249 * Now try to get the error block number from the sense data 17250 */ 17251 sensep = xp->xb_sense_data; 17252 17253 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17254 (uint64_t *)&err_blkno)) { 17255 /* 17256 * We retrieved the error block number from the information 17257 * portion of the sense data. 17258 * 17259 * For USCSI commands we are better off using the error 17260 * block no. as the requested block no. (This is the best 17261 * we can estimate.) 17262 */ 17263 if ((SD_IS_BUFIO(xp) == FALSE) && 17264 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17265 request_blkno = err_blkno; 17266 } 17267 } else { 17268 /* 17269 * Without the es_valid bit set (for fixed format) or an 17270 * information descriptor (for descriptor format) we cannot 17271 * be certain of the error blkno, so just use the 17272 * request_blkno. 17273 */ 17274 err_blkno = (diskaddr_t)request_blkno; 17275 } 17276 17277 /* 17278 * The following will log the buffer contents for the release driver 17279 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17280 * level is set to verbose. 17281 */ 17282 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17283 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17284 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17285 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17286 17287 if (pfa_flag == FALSE) { 17288 /* This is normally only set for USCSI */ 17289 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17290 return; 17291 } 17292 17293 if ((SD_IS_BUFIO(xp) == TRUE) && 17294 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17295 (severity < sd_error_level))) { 17296 return; 17297 } 17298 } 17299 /* 17300 * Check for Sonoma Failover and keep a count of how many failed I/O's 17301 */ 17302 if ((SD_IS_LSI(un)) && 17303 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17304 (scsi_sense_asc(sensep) == 0x94) && 17305 (scsi_sense_ascq(sensep) == 0x01)) { 17306 un->un_sonoma_failure_count++; 17307 if (un->un_sonoma_failure_count > 1) { 17308 return; 17309 } 17310 } 17311 17312 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP || 17313 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) && 17314 (pktp->pkt_resid == 0))) { 17315 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17316 request_blkno, err_blkno, scsi_cmds, 17317 (struct scsi_extended_sense *)sensep, 17318 un->un_additional_codes, NULL); 17319 } 17320 } 17321 17322 /* 17323 * Function: sd_sense_key_no_sense 17324 * 17325 * Description: Recovery action when sense data was not received. 17326 * 17327 * Context: May be called from interrupt context 17328 */ 17329 17330 static void 17331 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17332 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17333 { 17334 struct sd_sense_info si; 17335 17336 ASSERT(un != NULL); 17337 ASSERT(mutex_owned(SD_MUTEX(un))); 17338 ASSERT(bp != NULL); 17339 ASSERT(xp != NULL); 17340 ASSERT(pktp != NULL); 17341 17342 si.ssi_severity = SCSI_ERR_FATAL; 17343 si.ssi_pfa_flag = FALSE; 17344 17345 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17346 17347 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17348 &si, EIO, (clock_t)0, NULL); 17349 } 17350 17351 17352 /* 17353 * Function: sd_sense_key_recoverable_error 17354 * 17355 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17356 * 17357 * Context: May be called from interrupt context 17358 */ 17359 17360 static void 17361 sd_sense_key_recoverable_error(struct sd_lun *un, 17362 uint8_t *sense_datap, 17363 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17364 { 17365 struct sd_sense_info si; 17366 uint8_t asc = scsi_sense_asc(sense_datap); 17367 17368 ASSERT(un != NULL); 17369 ASSERT(mutex_owned(SD_MUTEX(un))); 17370 ASSERT(bp != NULL); 17371 ASSERT(xp != NULL); 17372 ASSERT(pktp != NULL); 17373 17374 /* 17375 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17376 */ 17377 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17378 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17379 si.ssi_severity = SCSI_ERR_INFO; 17380 si.ssi_pfa_flag = TRUE; 17381 } else { 17382 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17383 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17384 si.ssi_severity = SCSI_ERR_RECOVERED; 17385 si.ssi_pfa_flag = FALSE; 17386 } 17387 17388 if (pktp->pkt_resid == 0) { 17389 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17390 sd_return_command(un, bp); 17391 return; 17392 } 17393 17394 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17395 &si, EIO, (clock_t)0, NULL); 17396 } 17397 17398 17399 17400 17401 /* 17402 * Function: sd_sense_key_not_ready 17403 * 17404 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17405 * 17406 * Context: May be called from interrupt context 17407 */ 17408 17409 static void 17410 sd_sense_key_not_ready(struct sd_lun *un, 17411 uint8_t *sense_datap, 17412 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17413 { 17414 struct sd_sense_info si; 17415 uint8_t asc = scsi_sense_asc(sense_datap); 17416 uint8_t ascq = scsi_sense_ascq(sense_datap); 17417 17418 ASSERT(un != NULL); 17419 ASSERT(mutex_owned(SD_MUTEX(un))); 17420 ASSERT(bp != NULL); 17421 ASSERT(xp != NULL); 17422 ASSERT(pktp != NULL); 17423 17424 si.ssi_severity = SCSI_ERR_FATAL; 17425 si.ssi_pfa_flag = FALSE; 17426 17427 /* 17428 * Update error stats after first NOT READY error. Disks may have 17429 * been powered down and may need to be restarted. For CDROMs, 17430 * report NOT READY errors only if media is present. 17431 */ 17432 if ((ISCD(un) && (asc == 0x3A)) || 17433 (xp->xb_nr_retry_count > 0)) { 17434 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17435 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17436 } 17437 17438 /* 17439 * Just fail if the "not ready" retry limit has been reached. 17440 */ 17441 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 17442 /* Special check for error message printing for removables. */ 17443 if (un->un_f_has_removable_media && (asc == 0x04) && 17444 (ascq >= 0x04)) { 17445 si.ssi_severity = SCSI_ERR_ALL; 17446 } 17447 goto fail_command; 17448 } 17449 17450 /* 17451 * Check the ASC and ASCQ in the sense data as needed, to determine 17452 * what to do. 17453 */ 17454 switch (asc) { 17455 case 0x04: /* LOGICAL UNIT NOT READY */ 17456 /* 17457 * disk drives that don't spin up result in a very long delay 17458 * in format without warning messages. We will log a message 17459 * if the error level is set to verbose. 17460 */ 17461 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17462 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17463 "logical unit not ready, resetting disk\n"); 17464 } 17465 17466 /* 17467 * There are different requirements for CDROMs and disks for 17468 * the number of retries. If a CD-ROM is giving this, it is 17469 * probably reading TOC and is in the process of getting 17470 * ready, so we should keep on trying for a long time to make 17471 * sure that all types of media are taken in account (for 17472 * some media the drive takes a long time to read TOC). For 17473 * disks we do not want to retry this too many times as this 17474 * can cause a long hang in format when the drive refuses to 17475 * spin up (a very common failure). 17476 */ 17477 switch (ascq) { 17478 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17479 /* 17480 * Disk drives frequently refuse to spin up which 17481 * results in a very long hang in format without 17482 * warning messages. 17483 * 17484 * Note: This code preserves the legacy behavior of 17485 * comparing xb_nr_retry_count against zero for fibre 17486 * channel targets instead of comparing against the 17487 * un_reset_retry_count value. The reason for this 17488 * discrepancy has been so utterly lost beneath the 17489 * Sands of Time that even Indiana Jones could not 17490 * find it. 17491 */ 17492 if (un->un_f_is_fibre == TRUE) { 17493 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17494 (xp->xb_nr_retry_count > 0)) && 17495 (un->un_startstop_timeid == NULL)) { 17496 scsi_log(SD_DEVINFO(un), sd_label, 17497 CE_WARN, "logical unit not ready, " 17498 "resetting disk\n"); 17499 sd_reset_target(un, pktp); 17500 } 17501 } else { 17502 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17503 (xp->xb_nr_retry_count > 17504 un->un_reset_retry_count)) && 17505 (un->un_startstop_timeid == NULL)) { 17506 scsi_log(SD_DEVINFO(un), sd_label, 17507 CE_WARN, "logical unit not ready, " 17508 "resetting disk\n"); 17509 sd_reset_target(un, pktp); 17510 } 17511 } 17512 break; 17513 17514 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17515 /* 17516 * If the target is in the process of becoming 17517 * ready, just proceed with the retry. This can 17518 * happen with CD-ROMs that take a long time to 17519 * read TOC after a power cycle or reset. 17520 */ 17521 goto do_retry; 17522 17523 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17524 break; 17525 17526 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17527 /* 17528 * Retries cannot help here so just fail right away. 17529 */ 17530 goto fail_command; 17531 17532 case 0x88: 17533 /* 17534 * Vendor-unique code for T3/T4: it indicates a 17535 * path problem in a mutipathed config, but as far as 17536 * the target driver is concerned it equates to a fatal 17537 * error, so we should just fail the command right away 17538 * (without printing anything to the console). If this 17539 * is not a T3/T4, fall thru to the default recovery 17540 * action. 17541 * T3/T4 is FC only, don't need to check is_fibre 17542 */ 17543 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17544 sd_return_failed_command(un, bp, EIO); 17545 return; 17546 } 17547 /* FALLTHRU */ 17548 17549 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17550 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17551 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17552 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17553 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17554 default: /* Possible future codes in SCSI spec? */ 17555 /* 17556 * For removable-media devices, do not retry if 17557 * ASCQ > 2 as these result mostly from USCSI commands 17558 * on MMC devices issued to check status of an 17559 * operation initiated in immediate mode. Also for 17560 * ASCQ >= 4 do not print console messages as these 17561 * mainly represent a user-initiated operation 17562 * instead of a system failure. 17563 */ 17564 if (un->un_f_has_removable_media) { 17565 si.ssi_severity = SCSI_ERR_ALL; 17566 goto fail_command; 17567 } 17568 break; 17569 } 17570 17571 /* 17572 * As part of our recovery attempt for the NOT READY 17573 * condition, we issue a START STOP UNIT command. However 17574 * we want to wait for a short delay before attempting this 17575 * as there may still be more commands coming back from the 17576 * target with the check condition. To do this we use 17577 * timeout(9F) to call sd_start_stop_unit_callback() after 17578 * the delay interval expires. (sd_start_stop_unit_callback() 17579 * dispatches sd_start_stop_unit_task(), which will issue 17580 * the actual START STOP UNIT command. The delay interval 17581 * is one-half of the delay that we will use to retry the 17582 * command that generated the NOT READY condition. 17583 * 17584 * Note that we could just dispatch sd_start_stop_unit_task() 17585 * from here and allow it to sleep for the delay interval, 17586 * but then we would be tying up the taskq thread 17587 * uncesessarily for the duration of the delay. 17588 * 17589 * Do not issue the START STOP UNIT if the current command 17590 * is already a START STOP UNIT. 17591 */ 17592 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17593 break; 17594 } 17595 17596 /* 17597 * Do not schedule the timeout if one is already pending. 17598 */ 17599 if (un->un_startstop_timeid != NULL) { 17600 SD_INFO(SD_LOG_ERROR, un, 17601 "sd_sense_key_not_ready: restart already issued to" 17602 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 17603 ddi_get_instance(SD_DEVINFO(un))); 17604 break; 17605 } 17606 17607 /* 17608 * Schedule the START STOP UNIT command, then queue the command 17609 * for a retry. 17610 * 17611 * Note: A timeout is not scheduled for this retry because we 17612 * want the retry to be serial with the START_STOP_UNIT. The 17613 * retry will be started when the START_STOP_UNIT is completed 17614 * in sd_start_stop_unit_task. 17615 */ 17616 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 17617 un, un->un_busy_timeout / 2); 17618 xp->xb_nr_retry_count++; 17619 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 17620 return; 17621 17622 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 17623 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17624 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17625 "unit does not respond to selection\n"); 17626 } 17627 break; 17628 17629 case 0x3A: /* MEDIUM NOT PRESENT */ 17630 if (sd_error_level >= SCSI_ERR_FATAL) { 17631 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17632 "Caddy not inserted in drive\n"); 17633 } 17634 17635 sr_ejected(un); 17636 un->un_mediastate = DKIO_EJECTED; 17637 /* The state has changed, inform the media watch routines */ 17638 cv_broadcast(&un->un_state_cv); 17639 /* Just fail if no media is present in the drive. */ 17640 goto fail_command; 17641 17642 default: 17643 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17644 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 17645 "Unit not Ready. Additional sense code 0x%x\n", 17646 asc); 17647 } 17648 break; 17649 } 17650 17651 do_retry: 17652 17653 /* 17654 * Retry the command, as some targets may report NOT READY for 17655 * several seconds after being reset. 17656 */ 17657 xp->xb_nr_retry_count++; 17658 si.ssi_severity = SCSI_ERR_RETRYABLE; 17659 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17660 &si, EIO, un->un_busy_timeout, NULL); 17661 17662 return; 17663 17664 fail_command: 17665 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17666 sd_return_failed_command(un, bp, EIO); 17667 } 17668 17669 17670 17671 /* 17672 * Function: sd_sense_key_medium_or_hardware_error 17673 * 17674 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 17675 * sense key. 17676 * 17677 * Context: May be called from interrupt context 17678 */ 17679 17680 static void 17681 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 17682 uint8_t *sense_datap, 17683 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17684 { 17685 struct sd_sense_info si; 17686 uint8_t sense_key = scsi_sense_key(sense_datap); 17687 uint8_t asc = scsi_sense_asc(sense_datap); 17688 17689 ASSERT(un != NULL); 17690 ASSERT(mutex_owned(SD_MUTEX(un))); 17691 ASSERT(bp != NULL); 17692 ASSERT(xp != NULL); 17693 ASSERT(pktp != NULL); 17694 17695 si.ssi_severity = SCSI_ERR_FATAL; 17696 si.ssi_pfa_flag = FALSE; 17697 17698 if (sense_key == KEY_MEDIUM_ERROR) { 17699 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 17700 } 17701 17702 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17703 17704 if ((un->un_reset_retry_count != 0) && 17705 (xp->xb_retry_count == un->un_reset_retry_count)) { 17706 mutex_exit(SD_MUTEX(un)); 17707 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 17708 if (un->un_f_allow_bus_device_reset == TRUE) { 17709 17710 boolean_t try_resetting_target = B_TRUE; 17711 17712 /* 17713 * We need to be able to handle specific ASC when we are 17714 * handling a KEY_HARDWARE_ERROR. In particular 17715 * taking the default action of resetting the target may 17716 * not be the appropriate way to attempt recovery. 17717 * Resetting a target because of a single LUN failure 17718 * victimizes all LUNs on that target. 17719 * 17720 * This is true for the LSI arrays, if an LSI 17721 * array controller returns an ASC of 0x84 (LUN Dead) we 17722 * should trust it. 17723 */ 17724 17725 if (sense_key == KEY_HARDWARE_ERROR) { 17726 switch (asc) { 17727 case 0x84: 17728 if (SD_IS_LSI(un)) { 17729 try_resetting_target = B_FALSE; 17730 } 17731 break; 17732 default: 17733 break; 17734 } 17735 } 17736 17737 if (try_resetting_target == B_TRUE) { 17738 int reset_retval = 0; 17739 if (un->un_f_lun_reset_enabled == TRUE) { 17740 SD_TRACE(SD_LOG_IO_CORE, un, 17741 "sd_sense_key_medium_or_hardware_" 17742 "error: issuing RESET_LUN\n"); 17743 reset_retval = 17744 scsi_reset(SD_ADDRESS(un), 17745 RESET_LUN); 17746 } 17747 if (reset_retval == 0) { 17748 SD_TRACE(SD_LOG_IO_CORE, un, 17749 "sd_sense_key_medium_or_hardware_" 17750 "error: issuing RESET_TARGET\n"); 17751 (void) scsi_reset(SD_ADDRESS(un), 17752 RESET_TARGET); 17753 } 17754 } 17755 } 17756 mutex_enter(SD_MUTEX(un)); 17757 } 17758 17759 /* 17760 * This really ought to be a fatal error, but we will retry anyway 17761 * as some drives report this as a spurious error. 17762 */ 17763 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17764 &si, EIO, (clock_t)0, NULL); 17765 } 17766 17767 17768 17769 /* 17770 * Function: sd_sense_key_illegal_request 17771 * 17772 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 17773 * 17774 * Context: May be called from interrupt context 17775 */ 17776 17777 static void 17778 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 17779 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17780 { 17781 struct sd_sense_info si; 17782 17783 ASSERT(un != NULL); 17784 ASSERT(mutex_owned(SD_MUTEX(un))); 17785 ASSERT(bp != NULL); 17786 ASSERT(xp != NULL); 17787 ASSERT(pktp != NULL); 17788 17789 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 17790 17791 si.ssi_severity = SCSI_ERR_INFO; 17792 si.ssi_pfa_flag = FALSE; 17793 17794 /* Pointless to retry if the target thinks it's an illegal request */ 17795 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17796 sd_return_failed_command(un, bp, EIO); 17797 } 17798 17799 17800 17801 17802 /* 17803 * Function: sd_sense_key_unit_attention 17804 * 17805 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 17806 * 17807 * Context: May be called from interrupt context 17808 */ 17809 17810 static void 17811 sd_sense_key_unit_attention(struct sd_lun *un, 17812 uint8_t *sense_datap, 17813 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17814 { 17815 /* 17816 * For UNIT ATTENTION we allow retries for one minute. Devices 17817 * like Sonoma can return UNIT ATTENTION close to a minute 17818 * under certain conditions. 17819 */ 17820 int retry_check_flag = SD_RETRIES_UA; 17821 boolean_t kstat_updated = B_FALSE; 17822 struct sd_sense_info si; 17823 uint8_t asc = scsi_sense_asc(sense_datap); 17824 uint8_t ascq = scsi_sense_ascq(sense_datap); 17825 17826 ASSERT(un != NULL); 17827 ASSERT(mutex_owned(SD_MUTEX(un))); 17828 ASSERT(bp != NULL); 17829 ASSERT(xp != NULL); 17830 ASSERT(pktp != NULL); 17831 17832 si.ssi_severity = SCSI_ERR_INFO; 17833 si.ssi_pfa_flag = FALSE; 17834 17835 17836 switch (asc) { 17837 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 17838 if (sd_report_pfa != 0) { 17839 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17840 si.ssi_pfa_flag = TRUE; 17841 retry_check_flag = SD_RETRIES_STANDARD; 17842 goto do_retry; 17843 } 17844 17845 break; 17846 17847 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 17848 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 17849 un->un_resvd_status |= 17850 (SD_LOST_RESERVE | SD_WANT_RESERVE); 17851 } 17852 #ifdef _LP64 17853 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 17854 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 17855 un, KM_NOSLEEP) == 0) { 17856 /* 17857 * If we can't dispatch the task we'll just 17858 * live without descriptor sense. We can 17859 * try again on the next "unit attention" 17860 */ 17861 SD_ERROR(SD_LOG_ERROR, un, 17862 "sd_sense_key_unit_attention: " 17863 "Could not dispatch " 17864 "sd_reenable_dsense_task\n"); 17865 } 17866 } 17867 #endif /* _LP64 */ 17868 /* FALLTHRU */ 17869 17870 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 17871 if (!un->un_f_has_removable_media) { 17872 break; 17873 } 17874 17875 /* 17876 * When we get a unit attention from a removable-media device, 17877 * it may be in a state that will take a long time to recover 17878 * (e.g., from a reset). Since we are executing in interrupt 17879 * context here, we cannot wait around for the device to come 17880 * back. So hand this command off to sd_media_change_task() 17881 * for deferred processing under taskq thread context. (Note 17882 * that the command still may be failed if a problem is 17883 * encountered at a later time.) 17884 */ 17885 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 17886 KM_NOSLEEP) == 0) { 17887 /* 17888 * Cannot dispatch the request so fail the command. 17889 */ 17890 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17891 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17892 si.ssi_severity = SCSI_ERR_FATAL; 17893 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17894 sd_return_failed_command(un, bp, EIO); 17895 } 17896 17897 /* 17898 * If failed to dispatch sd_media_change_task(), we already 17899 * updated kstat. If succeed to dispatch sd_media_change_task(), 17900 * we should update kstat later if it encounters an error. So, 17901 * we update kstat_updated flag here. 17902 */ 17903 kstat_updated = B_TRUE; 17904 17905 /* 17906 * Either the command has been successfully dispatched to a 17907 * task Q for retrying, or the dispatch failed. In either case 17908 * do NOT retry again by calling sd_retry_command. This sets up 17909 * two retries of the same command and when one completes and 17910 * frees the resources the other will access freed memory, 17911 * a bad thing. 17912 */ 17913 return; 17914 17915 default: 17916 break; 17917 } 17918 17919 /* 17920 * ASC ASCQ 17921 * 2A 09 Capacity data has changed 17922 * 2A 01 Mode parameters changed 17923 * 3F 0E Reported luns data has changed 17924 * Arrays that support logical unit expansion should report 17925 * capacity changes(2Ah/09). Mode parameters changed and 17926 * reported luns data has changed are the approximation. 17927 */ 17928 if (((asc == 0x2a) && (ascq == 0x09)) || 17929 ((asc == 0x2a) && (ascq == 0x01)) || 17930 ((asc == 0x3f) && (ascq == 0x0e))) { 17931 if (taskq_dispatch(sd_tq, sd_target_change_task, un, 17932 KM_NOSLEEP) == 0) { 17933 SD_ERROR(SD_LOG_ERROR, un, 17934 "sd_sense_key_unit_attention: " 17935 "Could not dispatch sd_target_change_task\n"); 17936 } 17937 } 17938 17939 /* 17940 * Update kstat if we haven't done that. 17941 */ 17942 if (!kstat_updated) { 17943 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17944 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17945 } 17946 17947 do_retry: 17948 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 17949 EIO, SD_UA_RETRY_DELAY, NULL); 17950 } 17951 17952 17953 17954 /* 17955 * Function: sd_sense_key_fail_command 17956 * 17957 * Description: Use to fail a command when we don't like the sense key that 17958 * was returned. 17959 * 17960 * Context: May be called from interrupt context 17961 */ 17962 17963 static void 17964 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 17965 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17966 { 17967 struct sd_sense_info si; 17968 17969 ASSERT(un != NULL); 17970 ASSERT(mutex_owned(SD_MUTEX(un))); 17971 ASSERT(bp != NULL); 17972 ASSERT(xp != NULL); 17973 ASSERT(pktp != NULL); 17974 17975 si.ssi_severity = SCSI_ERR_FATAL; 17976 si.ssi_pfa_flag = FALSE; 17977 17978 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17979 sd_return_failed_command(un, bp, EIO); 17980 } 17981 17982 17983 17984 /* 17985 * Function: sd_sense_key_blank_check 17986 * 17987 * Description: Recovery actions for a SCSI "Blank Check" sense key. 17988 * Has no monetary connotation. 17989 * 17990 * Context: May be called from interrupt context 17991 */ 17992 17993 static void 17994 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 17995 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17996 { 17997 struct sd_sense_info si; 17998 17999 ASSERT(un != NULL); 18000 ASSERT(mutex_owned(SD_MUTEX(un))); 18001 ASSERT(bp != NULL); 18002 ASSERT(xp != NULL); 18003 ASSERT(pktp != NULL); 18004 18005 /* 18006 * Blank check is not fatal for removable devices, therefore 18007 * it does not require a console message. 18008 */ 18009 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18010 SCSI_ERR_FATAL; 18011 si.ssi_pfa_flag = FALSE; 18012 18013 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18014 sd_return_failed_command(un, bp, EIO); 18015 } 18016 18017 18018 18019 18020 /* 18021 * Function: sd_sense_key_aborted_command 18022 * 18023 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18024 * 18025 * Context: May be called from interrupt context 18026 */ 18027 18028 static void 18029 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18030 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18031 { 18032 struct sd_sense_info si; 18033 18034 ASSERT(un != NULL); 18035 ASSERT(mutex_owned(SD_MUTEX(un))); 18036 ASSERT(bp != NULL); 18037 ASSERT(xp != NULL); 18038 ASSERT(pktp != NULL); 18039 18040 si.ssi_severity = SCSI_ERR_FATAL; 18041 si.ssi_pfa_flag = FALSE; 18042 18043 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18044 18045 /* 18046 * This really ought to be a fatal error, but we will retry anyway 18047 * as some drives report this as a spurious error. 18048 */ 18049 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18050 &si, EIO, drv_usectohz(100000), NULL); 18051 } 18052 18053 18054 18055 /* 18056 * Function: sd_sense_key_default 18057 * 18058 * Description: Default recovery action for several SCSI sense keys (basically 18059 * attempts a retry). 18060 * 18061 * Context: May be called from interrupt context 18062 */ 18063 18064 static void 18065 sd_sense_key_default(struct sd_lun *un, 18066 uint8_t *sense_datap, 18067 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18068 { 18069 struct sd_sense_info si; 18070 uint8_t sense_key = scsi_sense_key(sense_datap); 18071 18072 ASSERT(un != NULL); 18073 ASSERT(mutex_owned(SD_MUTEX(un))); 18074 ASSERT(bp != NULL); 18075 ASSERT(xp != NULL); 18076 ASSERT(pktp != NULL); 18077 18078 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18079 18080 /* 18081 * Undecoded sense key. Attempt retries and hope that will fix 18082 * the problem. Otherwise, we're dead. 18083 */ 18084 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18085 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18086 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18087 } 18088 18089 si.ssi_severity = SCSI_ERR_FATAL; 18090 si.ssi_pfa_flag = FALSE; 18091 18092 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18093 &si, EIO, (clock_t)0, NULL); 18094 } 18095 18096 18097 18098 /* 18099 * Function: sd_print_retry_msg 18100 * 18101 * Description: Print a message indicating the retry action being taken. 18102 * 18103 * Arguments: un - ptr to associated softstate 18104 * bp - ptr to buf(9S) for the command 18105 * arg - not used. 18106 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18107 * or SD_NO_RETRY_ISSUED 18108 * 18109 * Context: May be called from interrupt context 18110 */ 18111 /* ARGSUSED */ 18112 static void 18113 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18114 { 18115 struct sd_xbuf *xp; 18116 struct scsi_pkt *pktp; 18117 char *reasonp; 18118 char *msgp; 18119 18120 ASSERT(un != NULL); 18121 ASSERT(mutex_owned(SD_MUTEX(un))); 18122 ASSERT(bp != NULL); 18123 pktp = SD_GET_PKTP(bp); 18124 ASSERT(pktp != NULL); 18125 xp = SD_GET_XBUF(bp); 18126 ASSERT(xp != NULL); 18127 18128 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18129 mutex_enter(&un->un_pm_mutex); 18130 if ((un->un_state == SD_STATE_SUSPENDED) || 18131 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18132 (pktp->pkt_flags & FLAG_SILENT)) { 18133 mutex_exit(&un->un_pm_mutex); 18134 goto update_pkt_reason; 18135 } 18136 mutex_exit(&un->un_pm_mutex); 18137 18138 /* 18139 * Suppress messages if they are all the same pkt_reason; with 18140 * TQ, many (up to 256) are returned with the same pkt_reason. 18141 * If we are in panic, then suppress the retry messages. 18142 */ 18143 switch (flag) { 18144 case SD_NO_RETRY_ISSUED: 18145 msgp = "giving up"; 18146 break; 18147 case SD_IMMEDIATE_RETRY_ISSUED: 18148 case SD_DELAYED_RETRY_ISSUED: 18149 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18150 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18151 (sd_error_level != SCSI_ERR_ALL))) { 18152 return; 18153 } 18154 msgp = "retrying command"; 18155 break; 18156 default: 18157 goto update_pkt_reason; 18158 } 18159 18160 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18161 scsi_rname(pktp->pkt_reason)); 18162 18163 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) { 18164 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18165 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18166 } 18167 18168 update_pkt_reason: 18169 /* 18170 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18171 * This is to prevent multiple console messages for the same failure 18172 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18173 * when the command is retried successfully because there still may be 18174 * more commands coming back with the same value of pktp->pkt_reason. 18175 */ 18176 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18177 un->un_last_pkt_reason = pktp->pkt_reason; 18178 } 18179 } 18180 18181 18182 /* 18183 * Function: sd_print_cmd_incomplete_msg 18184 * 18185 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18186 * 18187 * Arguments: un - ptr to associated softstate 18188 * bp - ptr to buf(9S) for the command 18189 * arg - passed to sd_print_retry_msg() 18190 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18191 * or SD_NO_RETRY_ISSUED 18192 * 18193 * Context: May be called from interrupt context 18194 */ 18195 18196 static void 18197 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18198 int code) 18199 { 18200 dev_info_t *dip; 18201 18202 ASSERT(un != NULL); 18203 ASSERT(mutex_owned(SD_MUTEX(un))); 18204 ASSERT(bp != NULL); 18205 18206 switch (code) { 18207 case SD_NO_RETRY_ISSUED: 18208 /* Command was failed. Someone turned off this target? */ 18209 if (un->un_state != SD_STATE_OFFLINE) { 18210 /* 18211 * Suppress message if we are detaching and 18212 * device has been disconnected 18213 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18214 * private interface and not part of the DDI 18215 */ 18216 dip = un->un_sd->sd_dev; 18217 if (!(DEVI_IS_DETACHING(dip) && 18218 DEVI_IS_DEVICE_REMOVED(dip))) { 18219 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18220 "disk not responding to selection\n"); 18221 } 18222 New_state(un, SD_STATE_OFFLINE); 18223 } 18224 break; 18225 18226 case SD_DELAYED_RETRY_ISSUED: 18227 case SD_IMMEDIATE_RETRY_ISSUED: 18228 default: 18229 /* Command was successfully queued for retry */ 18230 sd_print_retry_msg(un, bp, arg, code); 18231 break; 18232 } 18233 } 18234 18235 18236 /* 18237 * Function: sd_pkt_reason_cmd_incomplete 18238 * 18239 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18240 * 18241 * Context: May be called from interrupt context 18242 */ 18243 18244 static void 18245 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18246 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18247 { 18248 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18249 18250 ASSERT(un != NULL); 18251 ASSERT(mutex_owned(SD_MUTEX(un))); 18252 ASSERT(bp != NULL); 18253 ASSERT(xp != NULL); 18254 ASSERT(pktp != NULL); 18255 18256 /* Do not do a reset if selection did not complete */ 18257 /* Note: Should this not just check the bit? */ 18258 if (pktp->pkt_state != STATE_GOT_BUS) { 18259 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18260 sd_reset_target(un, pktp); 18261 } 18262 18263 /* 18264 * If the target was not successfully selected, then set 18265 * SD_RETRIES_FAILFAST to indicate that we lost communication 18266 * with the target, and further retries and/or commands are 18267 * likely to take a long time. 18268 */ 18269 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18270 flag |= SD_RETRIES_FAILFAST; 18271 } 18272 18273 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18274 18275 sd_retry_command(un, bp, flag, 18276 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18277 } 18278 18279 18280 18281 /* 18282 * Function: sd_pkt_reason_cmd_tran_err 18283 * 18284 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18285 * 18286 * Context: May be called from interrupt context 18287 */ 18288 18289 static void 18290 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18291 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18292 { 18293 ASSERT(un != NULL); 18294 ASSERT(mutex_owned(SD_MUTEX(un))); 18295 ASSERT(bp != NULL); 18296 ASSERT(xp != NULL); 18297 ASSERT(pktp != NULL); 18298 18299 /* 18300 * Do not reset if we got a parity error, or if 18301 * selection did not complete. 18302 */ 18303 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18304 /* Note: Should this not just check the bit for pkt_state? */ 18305 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18306 (pktp->pkt_state != STATE_GOT_BUS)) { 18307 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18308 sd_reset_target(un, pktp); 18309 } 18310 18311 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18312 18313 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18314 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18315 } 18316 18317 18318 18319 /* 18320 * Function: sd_pkt_reason_cmd_reset 18321 * 18322 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18323 * 18324 * Context: May be called from interrupt context 18325 */ 18326 18327 static void 18328 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18329 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18330 { 18331 ASSERT(un != NULL); 18332 ASSERT(mutex_owned(SD_MUTEX(un))); 18333 ASSERT(bp != NULL); 18334 ASSERT(xp != NULL); 18335 ASSERT(pktp != NULL); 18336 18337 /* The target may still be running the command, so try to reset. */ 18338 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18339 sd_reset_target(un, pktp); 18340 18341 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18342 18343 /* 18344 * If pkt_reason is CMD_RESET chances are that this pkt got 18345 * reset because another target on this bus caused it. The target 18346 * that caused it should get CMD_TIMEOUT with pkt_statistics 18347 * of STAT_TIMEOUT/STAT_DEV_RESET. 18348 */ 18349 18350 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18351 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18352 } 18353 18354 18355 18356 18357 /* 18358 * Function: sd_pkt_reason_cmd_aborted 18359 * 18360 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18361 * 18362 * Context: May be called from interrupt context 18363 */ 18364 18365 static void 18366 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18367 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18368 { 18369 ASSERT(un != NULL); 18370 ASSERT(mutex_owned(SD_MUTEX(un))); 18371 ASSERT(bp != NULL); 18372 ASSERT(xp != NULL); 18373 ASSERT(pktp != NULL); 18374 18375 /* The target may still be running the command, so try to reset. */ 18376 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18377 sd_reset_target(un, pktp); 18378 18379 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18380 18381 /* 18382 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18383 * aborted because another target on this bus caused it. The target 18384 * that caused it should get CMD_TIMEOUT with pkt_statistics 18385 * of STAT_TIMEOUT/STAT_DEV_RESET. 18386 */ 18387 18388 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18389 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18390 } 18391 18392 18393 18394 /* 18395 * Function: sd_pkt_reason_cmd_timeout 18396 * 18397 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18398 * 18399 * Context: May be called from interrupt context 18400 */ 18401 18402 static void 18403 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18404 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18405 { 18406 ASSERT(un != NULL); 18407 ASSERT(mutex_owned(SD_MUTEX(un))); 18408 ASSERT(bp != NULL); 18409 ASSERT(xp != NULL); 18410 ASSERT(pktp != NULL); 18411 18412 18413 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18414 sd_reset_target(un, pktp); 18415 18416 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18417 18418 /* 18419 * A command timeout indicates that we could not establish 18420 * communication with the target, so set SD_RETRIES_FAILFAST 18421 * as further retries/commands are likely to take a long time. 18422 */ 18423 sd_retry_command(un, bp, 18424 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18425 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18426 } 18427 18428 18429 18430 /* 18431 * Function: sd_pkt_reason_cmd_unx_bus_free 18432 * 18433 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18434 * 18435 * Context: May be called from interrupt context 18436 */ 18437 18438 static void 18439 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18440 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18441 { 18442 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18443 18444 ASSERT(un != NULL); 18445 ASSERT(mutex_owned(SD_MUTEX(un))); 18446 ASSERT(bp != NULL); 18447 ASSERT(xp != NULL); 18448 ASSERT(pktp != NULL); 18449 18450 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18451 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18452 18453 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18454 sd_print_retry_msg : NULL; 18455 18456 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18457 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18458 } 18459 18460 18461 /* 18462 * Function: sd_pkt_reason_cmd_tag_reject 18463 * 18464 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18465 * 18466 * Context: May be called from interrupt context 18467 */ 18468 18469 static void 18470 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18471 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18472 { 18473 ASSERT(un != NULL); 18474 ASSERT(mutex_owned(SD_MUTEX(un))); 18475 ASSERT(bp != NULL); 18476 ASSERT(xp != NULL); 18477 ASSERT(pktp != NULL); 18478 18479 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18480 pktp->pkt_flags = 0; 18481 un->un_tagflags = 0; 18482 if (un->un_f_opt_queueing == TRUE) { 18483 un->un_throttle = min(un->un_throttle, 3); 18484 } else { 18485 un->un_throttle = 1; 18486 } 18487 mutex_exit(SD_MUTEX(un)); 18488 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18489 mutex_enter(SD_MUTEX(un)); 18490 18491 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18492 18493 /* Legacy behavior not to check retry counts here. */ 18494 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18495 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18496 } 18497 18498 18499 /* 18500 * Function: sd_pkt_reason_default 18501 * 18502 * Description: Default recovery actions for SCSA pkt_reason values that 18503 * do not have more explicit recovery actions. 18504 * 18505 * Context: May be called from interrupt context 18506 */ 18507 18508 static void 18509 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18510 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18511 { 18512 ASSERT(un != NULL); 18513 ASSERT(mutex_owned(SD_MUTEX(un))); 18514 ASSERT(bp != NULL); 18515 ASSERT(xp != NULL); 18516 ASSERT(pktp != NULL); 18517 18518 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18519 sd_reset_target(un, pktp); 18520 18521 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18522 18523 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18524 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18525 } 18526 18527 18528 18529 /* 18530 * Function: sd_pkt_status_check_condition 18531 * 18532 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18533 * 18534 * Context: May be called from interrupt context 18535 */ 18536 18537 static void 18538 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18539 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18540 { 18541 ASSERT(un != NULL); 18542 ASSERT(mutex_owned(SD_MUTEX(un))); 18543 ASSERT(bp != NULL); 18544 ASSERT(xp != NULL); 18545 ASSERT(pktp != NULL); 18546 18547 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18548 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18549 18550 /* 18551 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18552 * command will be retried after the request sense). Otherwise, retry 18553 * the command. Note: we are issuing the request sense even though the 18554 * retry limit may have been reached for the failed command. 18555 */ 18556 if (un->un_f_arq_enabled == FALSE) { 18557 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18558 "no ARQ, sending request sense command\n"); 18559 sd_send_request_sense_command(un, bp, pktp); 18560 } else { 18561 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18562 "ARQ,retrying request sense command\n"); 18563 #if defined(__i386) || defined(__amd64) 18564 /* 18565 * The SD_RETRY_DELAY value need to be adjusted here 18566 * when SD_RETRY_DELAY change in sddef.h 18567 */ 18568 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18569 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18570 NULL); 18571 #else 18572 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18573 EIO, SD_RETRY_DELAY, NULL); 18574 #endif 18575 } 18576 18577 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18578 } 18579 18580 18581 /* 18582 * Function: sd_pkt_status_busy 18583 * 18584 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18585 * 18586 * Context: May be called from interrupt context 18587 */ 18588 18589 static void 18590 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18591 struct scsi_pkt *pktp) 18592 { 18593 ASSERT(un != NULL); 18594 ASSERT(mutex_owned(SD_MUTEX(un))); 18595 ASSERT(bp != NULL); 18596 ASSERT(xp != NULL); 18597 ASSERT(pktp != NULL); 18598 18599 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18600 "sd_pkt_status_busy: entry\n"); 18601 18602 /* If retries are exhausted, just fail the command. */ 18603 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18605 "device busy too long\n"); 18606 sd_return_failed_command(un, bp, EIO); 18607 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18608 "sd_pkt_status_busy: exit\n"); 18609 return; 18610 } 18611 xp->xb_retry_count++; 18612 18613 /* 18614 * Try to reset the target. However, we do not want to perform 18615 * more than one reset if the device continues to fail. The reset 18616 * will be performed when the retry count reaches the reset 18617 * threshold. This threshold should be set such that at least 18618 * one retry is issued before the reset is performed. 18619 */ 18620 if (xp->xb_retry_count == 18621 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 18622 int rval = 0; 18623 mutex_exit(SD_MUTEX(un)); 18624 if (un->un_f_allow_bus_device_reset == TRUE) { 18625 /* 18626 * First try to reset the LUN; if we cannot then 18627 * try to reset the target. 18628 */ 18629 if (un->un_f_lun_reset_enabled == TRUE) { 18630 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18631 "sd_pkt_status_busy: RESET_LUN\n"); 18632 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18633 } 18634 if (rval == 0) { 18635 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18636 "sd_pkt_status_busy: RESET_TARGET\n"); 18637 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18638 } 18639 } 18640 if (rval == 0) { 18641 /* 18642 * If the RESET_LUN and/or RESET_TARGET failed, 18643 * try RESET_ALL 18644 */ 18645 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18646 "sd_pkt_status_busy: RESET_ALL\n"); 18647 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 18648 } 18649 mutex_enter(SD_MUTEX(un)); 18650 if (rval == 0) { 18651 /* 18652 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 18653 * At this point we give up & fail the command. 18654 */ 18655 sd_return_failed_command(un, bp, EIO); 18656 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18657 "sd_pkt_status_busy: exit (failed cmd)\n"); 18658 return; 18659 } 18660 } 18661 18662 /* 18663 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 18664 * we have already checked the retry counts above. 18665 */ 18666 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 18667 EIO, un->un_busy_timeout, NULL); 18668 18669 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18670 "sd_pkt_status_busy: exit\n"); 18671 } 18672 18673 18674 /* 18675 * Function: sd_pkt_status_reservation_conflict 18676 * 18677 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 18678 * command status. 18679 * 18680 * Context: May be called from interrupt context 18681 */ 18682 18683 static void 18684 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 18685 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18686 { 18687 ASSERT(un != NULL); 18688 ASSERT(mutex_owned(SD_MUTEX(un))); 18689 ASSERT(bp != NULL); 18690 ASSERT(xp != NULL); 18691 ASSERT(pktp != NULL); 18692 18693 /* 18694 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 18695 * conflict could be due to various reasons like incorrect keys, not 18696 * registered or not reserved etc. So, we return EACCES to the caller. 18697 */ 18698 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 18699 int cmd = SD_GET_PKT_OPCODE(pktp); 18700 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 18701 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 18702 sd_return_failed_command(un, bp, EACCES); 18703 return; 18704 } 18705 } 18706 18707 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 18708 18709 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 18710 if (sd_failfast_enable != 0) { 18711 /* By definition, we must panic here.... */ 18712 sd_panic_for_res_conflict(un); 18713 /*NOTREACHED*/ 18714 } 18715 SD_ERROR(SD_LOG_IO, un, 18716 "sd_handle_resv_conflict: Disk Reserved\n"); 18717 sd_return_failed_command(un, bp, EACCES); 18718 return; 18719 } 18720 18721 /* 18722 * 1147670: retry only if sd_retry_on_reservation_conflict 18723 * property is set (default is 1). Retries will not succeed 18724 * on a disk reserved by another initiator. HA systems 18725 * may reset this via sd.conf to avoid these retries. 18726 * 18727 * Note: The legacy return code for this failure is EIO, however EACCES 18728 * seems more appropriate for a reservation conflict. 18729 */ 18730 if (sd_retry_on_reservation_conflict == 0) { 18731 SD_ERROR(SD_LOG_IO, un, 18732 "sd_handle_resv_conflict: Device Reserved\n"); 18733 sd_return_failed_command(un, bp, EIO); 18734 return; 18735 } 18736 18737 /* 18738 * Retry the command if we can. 18739 * 18740 * Note: The legacy return code for this failure is EIO, however EACCES 18741 * seems more appropriate for a reservation conflict. 18742 */ 18743 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18744 (clock_t)2, NULL); 18745 } 18746 18747 18748 18749 /* 18750 * Function: sd_pkt_status_qfull 18751 * 18752 * Description: Handle a QUEUE FULL condition from the target. This can 18753 * occur if the HBA does not handle the queue full condition. 18754 * (Basically this means third-party HBAs as Sun HBAs will 18755 * handle the queue full condition.) Note that if there are 18756 * some commands already in the transport, then the queue full 18757 * has occurred because the queue for this nexus is actually 18758 * full. If there are no commands in the transport, then the 18759 * queue full is resulting from some other initiator or lun 18760 * consuming all the resources at the target. 18761 * 18762 * Context: May be called from interrupt context 18763 */ 18764 18765 static void 18766 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 18767 struct sd_xbuf *xp, struct scsi_pkt *pktp) 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_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18776 "sd_pkt_status_qfull: entry\n"); 18777 18778 /* 18779 * Just lower the QFULL throttle and retry the command. Note that 18780 * we do not limit the number of retries here. 18781 */ 18782 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 18783 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 18784 SD_RESTART_TIMEOUT, NULL); 18785 18786 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18787 "sd_pkt_status_qfull: exit\n"); 18788 } 18789 18790 18791 /* 18792 * Function: sd_reset_target 18793 * 18794 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 18795 * RESET_TARGET, or RESET_ALL. 18796 * 18797 * Context: May be called under interrupt context. 18798 */ 18799 18800 static void 18801 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 18802 { 18803 int rval = 0; 18804 18805 ASSERT(un != NULL); 18806 ASSERT(mutex_owned(SD_MUTEX(un))); 18807 ASSERT(pktp != NULL); 18808 18809 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 18810 18811 /* 18812 * No need to reset if the transport layer has already done so. 18813 */ 18814 if ((pktp->pkt_statistics & 18815 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 18816 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18817 "sd_reset_target: no reset\n"); 18818 return; 18819 } 18820 18821 mutex_exit(SD_MUTEX(un)); 18822 18823 if (un->un_f_allow_bus_device_reset == TRUE) { 18824 if (un->un_f_lun_reset_enabled == TRUE) { 18825 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18826 "sd_reset_target: RESET_LUN\n"); 18827 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 18828 } 18829 if (rval == 0) { 18830 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18831 "sd_reset_target: RESET_TARGET\n"); 18832 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 18833 } 18834 } 18835 18836 if (rval == 0) { 18837 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18838 "sd_reset_target: RESET_ALL\n"); 18839 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 18840 } 18841 18842 mutex_enter(SD_MUTEX(un)); 18843 18844 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 18845 } 18846 18847 /* 18848 * Function: sd_target_change_task 18849 * 18850 * Description: Handle dynamic target change 18851 * 18852 * Context: Executes in a taskq() thread context 18853 */ 18854 static void 18855 sd_target_change_task(void *arg) 18856 { 18857 struct sd_lun *un = arg; 18858 uint64_t capacity; 18859 diskaddr_t label_cap; 18860 uint_t lbasize; 18861 sd_ssc_t *ssc; 18862 18863 ASSERT(un != NULL); 18864 ASSERT(!mutex_owned(SD_MUTEX(un))); 18865 18866 if ((un->un_f_blockcount_is_valid == FALSE) || 18867 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 18868 return; 18869 } 18870 18871 ssc = sd_ssc_init(un); 18872 18873 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity, 18874 &lbasize, SD_PATH_DIRECT) != 0) { 18875 SD_ERROR(SD_LOG_ERROR, un, 18876 "sd_target_change_task: fail to read capacity\n"); 18877 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 18878 goto task_exit; 18879 } 18880 18881 mutex_enter(SD_MUTEX(un)); 18882 if (capacity <= un->un_blockcount) { 18883 mutex_exit(SD_MUTEX(un)); 18884 goto task_exit; 18885 } 18886 18887 sd_update_block_info(un, lbasize, capacity); 18888 mutex_exit(SD_MUTEX(un)); 18889 18890 /* 18891 * If lun is EFI labeled and lun capacity is greater than the 18892 * capacity contained in the label, log a sys event. 18893 */ 18894 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap, 18895 (void*)SD_PATH_DIRECT) == 0) { 18896 mutex_enter(SD_MUTEX(un)); 18897 if (un->un_f_blockcount_is_valid && 18898 un->un_blockcount > label_cap) { 18899 mutex_exit(SD_MUTEX(un)); 18900 sd_log_lun_expansion_event(un, KM_SLEEP); 18901 } else { 18902 mutex_exit(SD_MUTEX(un)); 18903 } 18904 } 18905 18906 task_exit: 18907 sd_ssc_fini(ssc); 18908 } 18909 18910 /* 18911 * Function: sd_log_lun_expansion_event 18912 * 18913 * Description: Log lun expansion sys event 18914 * 18915 * Context: Never called from interrupt context 18916 */ 18917 static void 18918 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag) 18919 { 18920 int err; 18921 char *path; 18922 nvlist_t *dle_attr_list; 18923 18924 /* Allocate and build sysevent attribute list */ 18925 err = nvlist_alloc(&dle_attr_list, NV_UNIQUE_NAME_TYPE, km_flag); 18926 if (err != 0) { 18927 SD_ERROR(SD_LOG_ERROR, un, 18928 "sd_log_lun_expansion_event: fail to allocate space\n"); 18929 return; 18930 } 18931 18932 path = kmem_alloc(MAXPATHLEN, km_flag); 18933 if (path == NULL) { 18934 nvlist_free(dle_attr_list); 18935 SD_ERROR(SD_LOG_ERROR, un, 18936 "sd_log_lun_expansion_event: fail to allocate space\n"); 18937 return; 18938 } 18939 /* 18940 * Add path attribute to identify the lun. 18941 * We are using minor node 'a' as the sysevent attribute. 18942 */ 18943 (void) snprintf(path, MAXPATHLEN, "/devices"); 18944 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path)); 18945 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path), 18946 ":a"); 18947 18948 err = nvlist_add_string(dle_attr_list, DEV_PHYS_PATH, path); 18949 if (err != 0) { 18950 nvlist_free(dle_attr_list); 18951 kmem_free(path, MAXPATHLEN); 18952 SD_ERROR(SD_LOG_ERROR, un, 18953 "sd_log_lun_expansion_event: fail to add attribute\n"); 18954 return; 18955 } 18956 18957 /* Log dynamic lun expansion sysevent */ 18958 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS, 18959 ESC_DEV_DLE, dle_attr_list, NULL, km_flag); 18960 if (err != DDI_SUCCESS) { 18961 SD_ERROR(SD_LOG_ERROR, un, 18962 "sd_log_lun_expansion_event: fail to log sysevent\n"); 18963 } 18964 18965 nvlist_free(dle_attr_list); 18966 kmem_free(path, MAXPATHLEN); 18967 } 18968 18969 /* 18970 * Function: sd_media_change_task 18971 * 18972 * Description: Recovery action for CDROM to become available. 18973 * 18974 * Context: Executes in a taskq() thread context 18975 */ 18976 18977 static void 18978 sd_media_change_task(void *arg) 18979 { 18980 struct scsi_pkt *pktp = arg; 18981 struct sd_lun *un; 18982 struct buf *bp; 18983 struct sd_xbuf *xp; 18984 int err = 0; 18985 int retry_count = 0; 18986 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 18987 struct sd_sense_info si; 18988 18989 ASSERT(pktp != NULL); 18990 bp = (struct buf *)pktp->pkt_private; 18991 ASSERT(bp != NULL); 18992 xp = SD_GET_XBUF(bp); 18993 ASSERT(xp != NULL); 18994 un = SD_GET_UN(bp); 18995 ASSERT(un != NULL); 18996 ASSERT(!mutex_owned(SD_MUTEX(un))); 18997 ASSERT(un->un_f_monitor_media_state); 18998 18999 si.ssi_severity = SCSI_ERR_INFO; 19000 si.ssi_pfa_flag = FALSE; 19001 19002 /* 19003 * When a reset is issued on a CDROM, it takes a long time to 19004 * recover. First few attempts to read capacity and other things 19005 * related to handling unit attention fail (with a ASC 0x4 and 19006 * ASCQ 0x1). In that case we want to do enough retries and we want 19007 * to limit the retries in other cases of genuine failures like 19008 * no media in drive. 19009 */ 19010 while (retry_count++ < retry_limit) { 19011 if ((err = sd_handle_mchange(un)) == 0) { 19012 break; 19013 } 19014 if (err == EAGAIN) { 19015 retry_limit = SD_UNIT_ATTENTION_RETRY; 19016 } 19017 /* Sleep for 0.5 sec. & try again */ 19018 delay(drv_usectohz(500000)); 19019 } 19020 19021 /* 19022 * Dispatch (retry or fail) the original command here, 19023 * along with appropriate console messages.... 19024 * 19025 * Must grab the mutex before calling sd_retry_command, 19026 * sd_print_sense_msg and sd_return_failed_command. 19027 */ 19028 mutex_enter(SD_MUTEX(un)); 19029 if (err != SD_CMD_SUCCESS) { 19030 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19031 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19032 si.ssi_severity = SCSI_ERR_FATAL; 19033 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19034 sd_return_failed_command(un, bp, EIO); 19035 } else { 19036 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19037 &si, EIO, (clock_t)0, NULL); 19038 } 19039 mutex_exit(SD_MUTEX(un)); 19040 } 19041 19042 19043 19044 /* 19045 * Function: sd_handle_mchange 19046 * 19047 * Description: Perform geometry validation & other recovery when CDROM 19048 * has been removed from drive. 19049 * 19050 * Return Code: 0 for success 19051 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19052 * sd_send_scsi_READ_CAPACITY() 19053 * 19054 * Context: Executes in a taskq() thread context 19055 */ 19056 19057 static int 19058 sd_handle_mchange(struct sd_lun *un) 19059 { 19060 uint64_t capacity; 19061 uint32_t lbasize; 19062 int rval; 19063 sd_ssc_t *ssc; 19064 19065 ASSERT(!mutex_owned(SD_MUTEX(un))); 19066 ASSERT(un->un_f_monitor_media_state); 19067 19068 ssc = sd_ssc_init(un); 19069 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 19070 SD_PATH_DIRECT_PRIORITY); 19071 19072 if (rval != 0) 19073 goto failed; 19074 19075 mutex_enter(SD_MUTEX(un)); 19076 sd_update_block_info(un, lbasize, capacity); 19077 19078 if (un->un_errstats != NULL) { 19079 struct sd_errstats *stp = 19080 (struct sd_errstats *)un->un_errstats->ks_data; 19081 stp->sd_capacity.value.ui64 = (uint64_t) 19082 ((uint64_t)un->un_blockcount * 19083 (uint64_t)un->un_tgt_blocksize); 19084 } 19085 19086 /* 19087 * Check if the media in the device is writable or not 19088 */ 19089 if (ISCD(un)) { 19090 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY); 19091 } 19092 19093 /* 19094 * Note: Maybe let the strategy/partitioning chain worry about getting 19095 * valid geometry. 19096 */ 19097 mutex_exit(SD_MUTEX(un)); 19098 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 19099 19100 19101 if (cmlb_validate(un->un_cmlbhandle, 0, 19102 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 19103 sd_ssc_fini(ssc); 19104 return (EIO); 19105 } else { 19106 if (un->un_f_pkstats_enabled) { 19107 sd_set_pstats(un); 19108 SD_TRACE(SD_LOG_IO_PARTITION, un, 19109 "sd_handle_mchange: un:0x%p pstats created and " 19110 "set\n", un); 19111 } 19112 } 19113 19114 /* 19115 * Try to lock the door 19116 */ 19117 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 19118 SD_PATH_DIRECT_PRIORITY); 19119 failed: 19120 if (rval != 0) 19121 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19122 sd_ssc_fini(ssc); 19123 return (rval); 19124 } 19125 19126 19127 /* 19128 * Function: sd_send_scsi_DOORLOCK 19129 * 19130 * Description: Issue the scsi DOOR LOCK command 19131 * 19132 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19133 * structure for this target. 19134 * flag - SD_REMOVAL_ALLOW 19135 * SD_REMOVAL_PREVENT 19136 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19137 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19138 * to use the USCSI "direct" chain and bypass the normal 19139 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19140 * command is issued as part of an error recovery action. 19141 * 19142 * Return Code: 0 - Success 19143 * errno return code from sd_ssc_send() 19144 * 19145 * Context: Can sleep. 19146 */ 19147 19148 static int 19149 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag) 19150 { 19151 struct scsi_extended_sense sense_buf; 19152 union scsi_cdb cdb; 19153 struct uscsi_cmd ucmd_buf; 19154 int status; 19155 struct sd_lun *un; 19156 19157 ASSERT(ssc != NULL); 19158 un = ssc->ssc_un; 19159 ASSERT(un != NULL); 19160 ASSERT(!mutex_owned(SD_MUTEX(un))); 19161 19162 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19163 19164 /* already determined doorlock is not supported, fake success */ 19165 if (un->un_f_doorlock_supported == FALSE) { 19166 return (0); 19167 } 19168 19169 /* 19170 * If we are ejecting and see an SD_REMOVAL_PREVENT 19171 * ignore the command so we can complete the eject 19172 * operation. 19173 */ 19174 if (flag == SD_REMOVAL_PREVENT) { 19175 mutex_enter(SD_MUTEX(un)); 19176 if (un->un_f_ejecting == TRUE) { 19177 mutex_exit(SD_MUTEX(un)); 19178 return (EAGAIN); 19179 } 19180 mutex_exit(SD_MUTEX(un)); 19181 } 19182 19183 bzero(&cdb, sizeof (cdb)); 19184 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19185 19186 cdb.scc_cmd = SCMD_DOORLOCK; 19187 cdb.cdb_opaque[4] = (uchar_t)flag; 19188 19189 ucmd_buf.uscsi_cdb = (char *)&cdb; 19190 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19191 ucmd_buf.uscsi_bufaddr = NULL; 19192 ucmd_buf.uscsi_buflen = 0; 19193 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19194 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19195 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19196 ucmd_buf.uscsi_timeout = 15; 19197 19198 SD_TRACE(SD_LOG_IO, un, 19199 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n"); 19200 19201 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19202 UIO_SYSSPACE, path_flag); 19203 19204 if (status == 0) 19205 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19206 19207 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19208 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19209 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19210 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19211 19212 /* fake success and skip subsequent doorlock commands */ 19213 un->un_f_doorlock_supported = FALSE; 19214 return (0); 19215 } 19216 19217 return (status); 19218 } 19219 19220 /* 19221 * Function: sd_send_scsi_READ_CAPACITY 19222 * 19223 * Description: This routine uses the scsi READ CAPACITY command to determine 19224 * the device capacity in number of blocks and the device native 19225 * block size. If this function returns a failure, then the 19226 * values in *capp and *lbap are undefined. If the capacity 19227 * returned is 0xffffffff then the lun is too large for a 19228 * normal READ CAPACITY command and the results of a 19229 * READ CAPACITY 16 will be used instead. 19230 * 19231 * Arguments: ssc - ssc contains ptr to soft state struct for the target 19232 * capp - ptr to unsigned 64-bit variable to receive the 19233 * capacity value from the command. 19234 * lbap - ptr to unsigned 32-bit varaible to receive the 19235 * block size value from the command 19236 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19237 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19238 * to use the USCSI "direct" chain and bypass the normal 19239 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19240 * command is issued as part of an error recovery action. 19241 * 19242 * Return Code: 0 - Success 19243 * EIO - IO error 19244 * EACCES - Reservation conflict detected 19245 * EAGAIN - Device is becoming ready 19246 * errno return code from sd_ssc_send() 19247 * 19248 * Context: Can sleep. Blocks until command completes. 19249 */ 19250 19251 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19252 19253 static int 19254 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap, 19255 int path_flag) 19256 { 19257 struct scsi_extended_sense sense_buf; 19258 struct uscsi_cmd ucmd_buf; 19259 union scsi_cdb cdb; 19260 uint32_t *capacity_buf; 19261 uint64_t capacity; 19262 uint32_t lbasize; 19263 int status; 19264 struct sd_lun *un; 19265 19266 ASSERT(ssc != NULL); 19267 19268 un = ssc->ssc_un; 19269 ASSERT(un != NULL); 19270 ASSERT(!mutex_owned(SD_MUTEX(un))); 19271 ASSERT(capp != NULL); 19272 ASSERT(lbap != NULL); 19273 19274 SD_TRACE(SD_LOG_IO, un, 19275 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19276 19277 /* 19278 * First send a READ_CAPACITY command to the target. 19279 * (This command is mandatory under SCSI-2.) 19280 * 19281 * Set up the CDB for the READ_CAPACITY command. The Partial 19282 * Medium Indicator bit is cleared. The address field must be 19283 * zero if the PMI bit is zero. 19284 */ 19285 bzero(&cdb, sizeof (cdb)); 19286 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19287 19288 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19289 19290 cdb.scc_cmd = SCMD_READ_CAPACITY; 19291 19292 ucmd_buf.uscsi_cdb = (char *)&cdb; 19293 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19294 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19295 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19296 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19297 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19298 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19299 ucmd_buf.uscsi_timeout = 60; 19300 19301 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19302 UIO_SYSSPACE, path_flag); 19303 19304 switch (status) { 19305 case 0: 19306 /* Return failure if we did not get valid capacity data. */ 19307 if (ucmd_buf.uscsi_resid != 0) { 19308 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 19309 "sd_send_scsi_READ_CAPACITY received invalid " 19310 "capacity data"); 19311 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19312 return (EIO); 19313 } 19314 /* 19315 * Read capacity and block size from the READ CAPACITY 10 data. 19316 * This data may be adjusted later due to device specific 19317 * issues. 19318 * 19319 * According to the SCSI spec, the READ CAPACITY 10 19320 * command returns the following: 19321 * 19322 * bytes 0-3: Maximum logical block address available. 19323 * (MSB in byte:0 & LSB in byte:3) 19324 * 19325 * bytes 4-7: Block length in bytes 19326 * (MSB in byte:4 & LSB in byte:7) 19327 * 19328 */ 19329 capacity = BE_32(capacity_buf[0]); 19330 lbasize = BE_32(capacity_buf[1]); 19331 19332 /* 19333 * Done with capacity_buf 19334 */ 19335 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19336 19337 /* 19338 * if the reported capacity is set to all 0xf's, then 19339 * this disk is too large and requires SBC-2 commands. 19340 * Reissue the request using READ CAPACITY 16. 19341 */ 19342 if (capacity == 0xffffffff) { 19343 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19344 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, 19345 &lbasize, path_flag); 19346 if (status != 0) { 19347 return (status); 19348 } 19349 } 19350 break; /* Success! */ 19351 case EIO: 19352 switch (ucmd_buf.uscsi_status) { 19353 case STATUS_RESERVATION_CONFLICT: 19354 status = EACCES; 19355 break; 19356 case STATUS_CHECK: 19357 /* 19358 * Check condition; look for ASC/ASCQ of 0x04/0x01 19359 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19360 */ 19361 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19362 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19363 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19364 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19365 return (EAGAIN); 19366 } 19367 break; 19368 default: 19369 break; 19370 } 19371 /* FALLTHRU */ 19372 default: 19373 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19374 return (status); 19375 } 19376 19377 /* 19378 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19379 * (2352 and 0 are common) so for these devices always force the value 19380 * to 2048 as required by the ATAPI specs. 19381 */ 19382 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19383 lbasize = 2048; 19384 } 19385 19386 /* 19387 * Get the maximum LBA value from the READ CAPACITY data. 19388 * Here we assume that the Partial Medium Indicator (PMI) bit 19389 * was cleared when issuing the command. This means that the LBA 19390 * returned from the device is the LBA of the last logical block 19391 * on the logical unit. The actual logical block count will be 19392 * this value plus one. 19393 * 19394 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19395 * so scale the capacity value to reflect this. 19396 */ 19397 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19398 19399 /* 19400 * Copy the values from the READ CAPACITY command into the space 19401 * provided by the caller. 19402 */ 19403 *capp = capacity; 19404 *lbap = lbasize; 19405 19406 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19407 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19408 19409 /* 19410 * Both the lbasize and capacity from the device must be nonzero, 19411 * otherwise we assume that the values are not valid and return 19412 * failure to the caller. (4203735) 19413 */ 19414 if ((capacity == 0) || (lbasize == 0)) { 19415 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 19416 "sd_send_scsi_READ_CAPACITY received invalid value " 19417 "capacity %llu lbasize %d", capacity, lbasize); 19418 return (EIO); 19419 } 19420 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19421 return (0); 19422 } 19423 19424 /* 19425 * Function: sd_send_scsi_READ_CAPACITY_16 19426 * 19427 * Description: This routine uses the scsi READ CAPACITY 16 command to 19428 * determine the device capacity in number of blocks and the 19429 * device native block size. If this function returns a failure, 19430 * then the values in *capp and *lbap are undefined. 19431 * This routine should always be called by 19432 * sd_send_scsi_READ_CAPACITY which will appy any device 19433 * specific adjustments to capacity and lbasize. 19434 * 19435 * Arguments: ssc - ssc contains ptr to soft state struct for the target 19436 * capp - ptr to unsigned 64-bit variable to receive the 19437 * capacity value from the command. 19438 * lbap - ptr to unsigned 32-bit varaible to receive the 19439 * block size value from the command 19440 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19441 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19442 * to use the USCSI "direct" chain and bypass the normal 19443 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19444 * this command is issued as part of an error recovery 19445 * action. 19446 * 19447 * Return Code: 0 - Success 19448 * EIO - IO error 19449 * EACCES - Reservation conflict detected 19450 * EAGAIN - Device is becoming ready 19451 * errno return code from sd_ssc_send() 19452 * 19453 * Context: Can sleep. Blocks until command completes. 19454 */ 19455 19456 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19457 19458 static int 19459 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, 19460 uint32_t *lbap, int path_flag) 19461 { 19462 struct scsi_extended_sense sense_buf; 19463 struct uscsi_cmd ucmd_buf; 19464 union scsi_cdb cdb; 19465 uint64_t *capacity16_buf; 19466 uint64_t capacity; 19467 uint32_t lbasize; 19468 int status; 19469 struct sd_lun *un; 19470 19471 ASSERT(ssc != NULL); 19472 19473 un = ssc->ssc_un; 19474 ASSERT(un != NULL); 19475 ASSERT(!mutex_owned(SD_MUTEX(un))); 19476 ASSERT(capp != NULL); 19477 ASSERT(lbap != NULL); 19478 19479 SD_TRACE(SD_LOG_IO, un, 19480 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19481 19482 /* 19483 * First send a READ_CAPACITY_16 command to the target. 19484 * 19485 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19486 * Medium Indicator bit is cleared. The address field must be 19487 * zero if the PMI bit is zero. 19488 */ 19489 bzero(&cdb, sizeof (cdb)); 19490 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19491 19492 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19493 19494 ucmd_buf.uscsi_cdb = (char *)&cdb; 19495 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19496 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19497 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19498 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19499 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19500 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19501 ucmd_buf.uscsi_timeout = 60; 19502 19503 /* 19504 * Read Capacity (16) is a Service Action In command. One 19505 * command byte (0x9E) is overloaded for multiple operations, 19506 * with the second CDB byte specifying the desired operation 19507 */ 19508 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19509 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19510 19511 /* 19512 * Fill in allocation length field 19513 */ 19514 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19515 19516 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19517 UIO_SYSSPACE, path_flag); 19518 19519 switch (status) { 19520 case 0: 19521 /* Return failure if we did not get valid capacity data. */ 19522 if (ucmd_buf.uscsi_resid > 20) { 19523 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 19524 "sd_send_scsi_READ_CAPACITY_16 received invalid " 19525 "capacity data"); 19526 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19527 return (EIO); 19528 } 19529 19530 /* 19531 * Read capacity and block size from the READ CAPACITY 10 data. 19532 * This data may be adjusted later due to device specific 19533 * issues. 19534 * 19535 * According to the SCSI spec, the READ CAPACITY 10 19536 * command returns the following: 19537 * 19538 * bytes 0-7: Maximum logical block address available. 19539 * (MSB in byte:0 & LSB in byte:7) 19540 * 19541 * bytes 8-11: Block length in bytes 19542 * (MSB in byte:8 & LSB in byte:11) 19543 * 19544 */ 19545 capacity = BE_64(capacity16_buf[0]); 19546 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19547 19548 /* 19549 * Done with capacity16_buf 19550 */ 19551 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19552 19553 /* 19554 * if the reported capacity is set to all 0xf's, then 19555 * this disk is too large. This could only happen with 19556 * a device that supports LBAs larger than 64 bits which 19557 * are not defined by any current T10 standards. 19558 */ 19559 if (capacity == 0xffffffffffffffff) { 19560 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 19561 "disk is too large"); 19562 return (EIO); 19563 } 19564 break; /* Success! */ 19565 case EIO: 19566 switch (ucmd_buf.uscsi_status) { 19567 case STATUS_RESERVATION_CONFLICT: 19568 status = EACCES; 19569 break; 19570 case STATUS_CHECK: 19571 /* 19572 * Check condition; look for ASC/ASCQ of 0x04/0x01 19573 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19574 */ 19575 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19576 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19577 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19578 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19579 return (EAGAIN); 19580 } 19581 break; 19582 default: 19583 break; 19584 } 19585 /* FALLTHRU */ 19586 default: 19587 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19588 return (status); 19589 } 19590 19591 *capp = capacity; 19592 *lbap = lbasize; 19593 19594 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19595 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19596 19597 return (0); 19598 } 19599 19600 19601 /* 19602 * Function: sd_send_scsi_START_STOP_UNIT 19603 * 19604 * Description: Issue a scsi START STOP UNIT command to the target. 19605 * 19606 * Arguments: ssc - ssc contatins pointer to driver soft state (unit) 19607 * structure for this target. 19608 * flag - SD_TARGET_START 19609 * SD_TARGET_STOP 19610 * SD_TARGET_EJECT 19611 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19612 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19613 * to use the USCSI "direct" chain and bypass the normal 19614 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19615 * command is issued as part of an error recovery action. 19616 * 19617 * Return Code: 0 - Success 19618 * EIO - IO error 19619 * EACCES - Reservation conflict detected 19620 * ENXIO - Not Ready, medium not present 19621 * errno return code from sd_ssc_send() 19622 * 19623 * Context: Can sleep. 19624 */ 19625 19626 static int 19627 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int flag, int path_flag) 19628 { 19629 struct scsi_extended_sense sense_buf; 19630 union scsi_cdb cdb; 19631 struct uscsi_cmd ucmd_buf; 19632 int status; 19633 struct sd_lun *un; 19634 19635 ASSERT(ssc != NULL); 19636 un = ssc->ssc_un; 19637 ASSERT(un != NULL); 19638 ASSERT(!mutex_owned(SD_MUTEX(un))); 19639 19640 SD_TRACE(SD_LOG_IO, un, 19641 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19642 19643 if (un->un_f_check_start_stop && 19644 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19645 (un->un_f_start_stop_supported != TRUE)) { 19646 return (0); 19647 } 19648 19649 /* 19650 * If we are performing an eject operation and 19651 * we receive any command other than SD_TARGET_EJECT 19652 * we should immediately return. 19653 */ 19654 if (flag != SD_TARGET_EJECT) { 19655 mutex_enter(SD_MUTEX(un)); 19656 if (un->un_f_ejecting == TRUE) { 19657 mutex_exit(SD_MUTEX(un)); 19658 return (EAGAIN); 19659 } 19660 mutex_exit(SD_MUTEX(un)); 19661 } 19662 19663 bzero(&cdb, sizeof (cdb)); 19664 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19665 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19666 19667 cdb.scc_cmd = SCMD_START_STOP; 19668 cdb.cdb_opaque[4] = (uchar_t)flag; 19669 19670 ucmd_buf.uscsi_cdb = (char *)&cdb; 19671 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19672 ucmd_buf.uscsi_bufaddr = NULL; 19673 ucmd_buf.uscsi_buflen = 0; 19674 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19675 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19676 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19677 ucmd_buf.uscsi_timeout = 200; 19678 19679 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19680 UIO_SYSSPACE, path_flag); 19681 19682 switch (status) { 19683 case 0: 19684 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19685 break; /* Success! */ 19686 case EIO: 19687 switch (ucmd_buf.uscsi_status) { 19688 case STATUS_RESERVATION_CONFLICT: 19689 status = EACCES; 19690 break; 19691 case STATUS_CHECK: 19692 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19693 switch (scsi_sense_key( 19694 (uint8_t *)&sense_buf)) { 19695 case KEY_ILLEGAL_REQUEST: 19696 status = ENOTSUP; 19697 break; 19698 case KEY_NOT_READY: 19699 if (scsi_sense_asc( 19700 (uint8_t *)&sense_buf) 19701 == 0x3A) { 19702 status = ENXIO; 19703 } 19704 break; 19705 default: 19706 break; 19707 } 19708 } 19709 break; 19710 default: 19711 break; 19712 } 19713 break; 19714 default: 19715 break; 19716 } 19717 19718 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19719 19720 return (status); 19721 } 19722 19723 19724 /* 19725 * Function: sd_start_stop_unit_callback 19726 * 19727 * Description: timeout(9F) callback to begin recovery process for a 19728 * device that has spun down. 19729 * 19730 * Arguments: arg - pointer to associated softstate struct. 19731 * 19732 * Context: Executes in a timeout(9F) thread context 19733 */ 19734 19735 static void 19736 sd_start_stop_unit_callback(void *arg) 19737 { 19738 struct sd_lun *un = arg; 19739 ASSERT(un != NULL); 19740 ASSERT(!mutex_owned(SD_MUTEX(un))); 19741 19742 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19743 19744 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19745 } 19746 19747 19748 /* 19749 * Function: sd_start_stop_unit_task 19750 * 19751 * Description: Recovery procedure when a drive is spun down. 19752 * 19753 * Arguments: arg - pointer to associated softstate struct. 19754 * 19755 * Context: Executes in a taskq() thread context 19756 */ 19757 19758 static void 19759 sd_start_stop_unit_task(void *arg) 19760 { 19761 struct sd_lun *un = arg; 19762 sd_ssc_t *ssc; 19763 int rval; 19764 19765 ASSERT(un != NULL); 19766 ASSERT(!mutex_owned(SD_MUTEX(un))); 19767 19768 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19769 19770 /* 19771 * Some unformatted drives report not ready error, no need to 19772 * restart if format has been initiated. 19773 */ 19774 mutex_enter(SD_MUTEX(un)); 19775 if (un->un_f_format_in_progress == TRUE) { 19776 mutex_exit(SD_MUTEX(un)); 19777 return; 19778 } 19779 mutex_exit(SD_MUTEX(un)); 19780 19781 /* 19782 * When a START STOP command is issued from here, it is part of a 19783 * failure recovery operation and must be issued before any other 19784 * commands, including any pending retries. Thus it must be sent 19785 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19786 * succeeds or not, we will start I/O after the attempt. 19787 */ 19788 ssc = sd_ssc_init(un); 19789 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_START, 19790 SD_PATH_DIRECT_PRIORITY); 19791 if (rval != 0) 19792 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 19793 sd_ssc_fini(ssc); 19794 /* 19795 * The above call blocks until the START_STOP_UNIT command completes. 19796 * Now that it has completed, we must re-try the original IO that 19797 * received the NOT READY condition in the first place. There are 19798 * three possible conditions here: 19799 * 19800 * (1) The original IO is on un_retry_bp. 19801 * (2) The original IO is on the regular wait queue, and un_retry_bp 19802 * is NULL. 19803 * (3) The original IO is on the regular wait queue, and un_retry_bp 19804 * points to some other, unrelated bp. 19805 * 19806 * For each case, we must call sd_start_cmds() with un_retry_bp 19807 * as the argument. If un_retry_bp is NULL, this will initiate 19808 * processing of the regular wait queue. If un_retry_bp is not NULL, 19809 * then this will process the bp on un_retry_bp. That may or may not 19810 * be the original IO, but that does not matter: the important thing 19811 * is to keep the IO processing going at this point. 19812 * 19813 * Note: This is a very specific error recovery sequence associated 19814 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19815 * serialize the I/O with completion of the spin-up. 19816 */ 19817 mutex_enter(SD_MUTEX(un)); 19818 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19819 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 19820 un, un->un_retry_bp); 19821 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 19822 sd_start_cmds(un, un->un_retry_bp); 19823 mutex_exit(SD_MUTEX(un)); 19824 19825 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 19826 } 19827 19828 19829 /* 19830 * Function: sd_send_scsi_INQUIRY 19831 * 19832 * Description: Issue the scsi INQUIRY command. 19833 * 19834 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19835 * structure for this target. 19836 * bufaddr 19837 * buflen 19838 * evpd 19839 * page_code 19840 * page_length 19841 * 19842 * Return Code: 0 - Success 19843 * errno return code from sd_ssc_send() 19844 * 19845 * Context: Can sleep. Does not return until command is completed. 19846 */ 19847 19848 static int 19849 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen, 19850 uchar_t evpd, uchar_t page_code, size_t *residp) 19851 { 19852 union scsi_cdb cdb; 19853 struct uscsi_cmd ucmd_buf; 19854 int status; 19855 struct sd_lun *un; 19856 19857 ASSERT(ssc != NULL); 19858 un = ssc->ssc_un; 19859 ASSERT(un != NULL); 19860 ASSERT(!mutex_owned(SD_MUTEX(un))); 19861 ASSERT(bufaddr != NULL); 19862 19863 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 19864 19865 bzero(&cdb, sizeof (cdb)); 19866 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19867 bzero(bufaddr, buflen); 19868 19869 cdb.scc_cmd = SCMD_INQUIRY; 19870 cdb.cdb_opaque[1] = evpd; 19871 cdb.cdb_opaque[2] = page_code; 19872 FORMG0COUNT(&cdb, buflen); 19873 19874 ucmd_buf.uscsi_cdb = (char *)&cdb; 19875 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19876 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19877 ucmd_buf.uscsi_buflen = buflen; 19878 ucmd_buf.uscsi_rqbuf = NULL; 19879 ucmd_buf.uscsi_rqlen = 0; 19880 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 19881 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 19882 19883 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19884 UIO_SYSSPACE, SD_PATH_DIRECT); 19885 19886 /* 19887 * Only handle status == 0, the upper-level caller 19888 * will put different assessment based on the context. 19889 */ 19890 if (status == 0) 19891 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19892 19893 if ((status == 0) && (residp != NULL)) { 19894 *residp = ucmd_buf.uscsi_resid; 19895 } 19896 19897 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 19898 19899 return (status); 19900 } 19901 19902 19903 /* 19904 * Function: sd_send_scsi_TEST_UNIT_READY 19905 * 19906 * Description: Issue the scsi TEST UNIT READY command. 19907 * This routine can be told to set the flag USCSI_DIAGNOSE to 19908 * prevent retrying failed commands. Use this when the intent 19909 * is either to check for device readiness, to clear a Unit 19910 * Attention, or to clear any outstanding sense data. 19911 * However under specific conditions the expected behavior 19912 * is for retries to bring a device ready, so use the flag 19913 * with caution. 19914 * 19915 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 19916 * structure for this target. 19917 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 19918 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 19919 * 0: dont check for media present, do retries on cmd. 19920 * 19921 * Return Code: 0 - Success 19922 * EIO - IO error 19923 * EACCES - Reservation conflict detected 19924 * ENXIO - Not Ready, medium not present 19925 * errno return code from sd_ssc_send() 19926 * 19927 * Context: Can sleep. Does not return until command is completed. 19928 */ 19929 19930 static int 19931 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag) 19932 { 19933 struct scsi_extended_sense sense_buf; 19934 union scsi_cdb cdb; 19935 struct uscsi_cmd ucmd_buf; 19936 int status; 19937 struct sd_lun *un; 19938 19939 ASSERT(ssc != NULL); 19940 un = ssc->ssc_un; 19941 ASSERT(un != NULL); 19942 ASSERT(!mutex_owned(SD_MUTEX(un))); 19943 19944 SD_TRACE(SD_LOG_IO, un, 19945 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 19946 19947 /* 19948 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 19949 * timeouts when they receive a TUR and the queue is not empty. Check 19950 * the configuration flag set during attach (indicating the drive has 19951 * this firmware bug) and un_ncmds_in_transport before issuing the 19952 * TUR. If there are 19953 * pending commands return success, this is a bit arbitrary but is ok 19954 * for non-removables (i.e. the eliteI disks) and non-clustering 19955 * configurations. 19956 */ 19957 if (un->un_f_cfg_tur_check == TRUE) { 19958 mutex_enter(SD_MUTEX(un)); 19959 if (un->un_ncmds_in_transport != 0) { 19960 mutex_exit(SD_MUTEX(un)); 19961 return (0); 19962 } 19963 mutex_exit(SD_MUTEX(un)); 19964 } 19965 19966 bzero(&cdb, sizeof (cdb)); 19967 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19968 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19969 19970 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 19971 19972 ucmd_buf.uscsi_cdb = (char *)&cdb; 19973 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19974 ucmd_buf.uscsi_bufaddr = NULL; 19975 ucmd_buf.uscsi_buflen = 0; 19976 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19977 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19978 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19979 19980 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 19981 if ((flag & SD_DONT_RETRY_TUR) != 0) { 19982 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 19983 } 19984 ucmd_buf.uscsi_timeout = 60; 19985 19986 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 19987 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 19988 SD_PATH_STANDARD)); 19989 19990 switch (status) { 19991 case 0: 19992 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 19993 break; /* Success! */ 19994 case EIO: 19995 switch (ucmd_buf.uscsi_status) { 19996 case STATUS_RESERVATION_CONFLICT: 19997 status = EACCES; 19998 break; 19999 case STATUS_CHECK: 20000 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20001 break; 20002 } 20003 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20004 (scsi_sense_key((uint8_t *)&sense_buf) == 20005 KEY_NOT_READY) && 20006 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20007 status = ENXIO; 20008 } 20009 break; 20010 default: 20011 break; 20012 } 20013 break; 20014 default: 20015 break; 20016 } 20017 20018 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20019 20020 return (status); 20021 } 20022 20023 /* 20024 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20025 * 20026 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20027 * 20028 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20029 * structure for this target. 20030 * 20031 * Return Code: 0 - Success 20032 * EACCES 20033 * ENOTSUP 20034 * errno return code from sd_ssc_send() 20035 * 20036 * Context: Can sleep. Does not return until command is completed. 20037 */ 20038 20039 static int 20040 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd, 20041 uint16_t data_len, uchar_t *data_bufp) 20042 { 20043 struct scsi_extended_sense sense_buf; 20044 union scsi_cdb cdb; 20045 struct uscsi_cmd ucmd_buf; 20046 int status; 20047 int no_caller_buf = FALSE; 20048 struct sd_lun *un; 20049 20050 ASSERT(ssc != NULL); 20051 un = ssc->ssc_un; 20052 ASSERT(un != NULL); 20053 ASSERT(!mutex_owned(SD_MUTEX(un))); 20054 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20055 20056 SD_TRACE(SD_LOG_IO, un, 20057 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20058 20059 bzero(&cdb, sizeof (cdb)); 20060 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20061 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20062 if (data_bufp == NULL) { 20063 /* Allocate a default buf if the caller did not give one */ 20064 ASSERT(data_len == 0); 20065 data_len = MHIOC_RESV_KEY_SIZE; 20066 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20067 no_caller_buf = TRUE; 20068 } 20069 20070 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20071 cdb.cdb_opaque[1] = usr_cmd; 20072 FORMG1COUNT(&cdb, data_len); 20073 20074 ucmd_buf.uscsi_cdb = (char *)&cdb; 20075 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20076 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20077 ucmd_buf.uscsi_buflen = data_len; 20078 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20079 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20080 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20081 ucmd_buf.uscsi_timeout = 60; 20082 20083 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20084 UIO_SYSSPACE, SD_PATH_STANDARD); 20085 20086 switch (status) { 20087 case 0: 20088 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20089 20090 break; /* Success! */ 20091 case EIO: 20092 switch (ucmd_buf.uscsi_status) { 20093 case STATUS_RESERVATION_CONFLICT: 20094 status = EACCES; 20095 break; 20096 case STATUS_CHECK: 20097 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20098 (scsi_sense_key((uint8_t *)&sense_buf) == 20099 KEY_ILLEGAL_REQUEST)) { 20100 status = ENOTSUP; 20101 } 20102 break; 20103 default: 20104 break; 20105 } 20106 break; 20107 default: 20108 break; 20109 } 20110 20111 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20112 20113 if (no_caller_buf == TRUE) { 20114 kmem_free(data_bufp, data_len); 20115 } 20116 20117 return (status); 20118 } 20119 20120 20121 /* 20122 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20123 * 20124 * Description: This routine is the driver entry point for handling CD-ROM 20125 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20126 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20127 * device. 20128 * 20129 * Arguments: ssc - ssc contains un - pointer to soft state struct 20130 * for the target. 20131 * usr_cmd SCSI-3 reservation facility command (one of 20132 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20133 * SD_SCSI3_PREEMPTANDABORT) 20134 * usr_bufp - user provided pointer register, reserve descriptor or 20135 * preempt and abort structure (mhioc_register_t, 20136 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20137 * 20138 * Return Code: 0 - Success 20139 * EACCES 20140 * ENOTSUP 20141 * errno return code from sd_ssc_send() 20142 * 20143 * Context: Can sleep. Does not return until command is completed. 20144 */ 20145 20146 static int 20147 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd, 20148 uchar_t *usr_bufp) 20149 { 20150 struct scsi_extended_sense sense_buf; 20151 union scsi_cdb cdb; 20152 struct uscsi_cmd ucmd_buf; 20153 int status; 20154 uchar_t data_len = sizeof (sd_prout_t); 20155 sd_prout_t *prp; 20156 struct sd_lun *un; 20157 20158 ASSERT(ssc != NULL); 20159 un = ssc->ssc_un; 20160 ASSERT(un != NULL); 20161 ASSERT(!mutex_owned(SD_MUTEX(un))); 20162 ASSERT(data_len == 24); /* required by scsi spec */ 20163 20164 SD_TRACE(SD_LOG_IO, un, 20165 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20166 20167 if (usr_bufp == NULL) { 20168 return (EINVAL); 20169 } 20170 20171 bzero(&cdb, sizeof (cdb)); 20172 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20173 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20174 prp = kmem_zalloc(data_len, KM_SLEEP); 20175 20176 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20177 cdb.cdb_opaque[1] = usr_cmd; 20178 FORMG1COUNT(&cdb, data_len); 20179 20180 ucmd_buf.uscsi_cdb = (char *)&cdb; 20181 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20182 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20183 ucmd_buf.uscsi_buflen = data_len; 20184 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20185 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20186 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20187 ucmd_buf.uscsi_timeout = 60; 20188 20189 switch (usr_cmd) { 20190 case SD_SCSI3_REGISTER: { 20191 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20192 20193 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20194 bcopy(ptr->newkey.key, prp->service_key, 20195 MHIOC_RESV_KEY_SIZE); 20196 prp->aptpl = ptr->aptpl; 20197 break; 20198 } 20199 case SD_SCSI3_RESERVE: 20200 case SD_SCSI3_RELEASE: { 20201 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20202 20203 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20204 prp->scope_address = BE_32(ptr->scope_specific_addr); 20205 cdb.cdb_opaque[2] = ptr->type; 20206 break; 20207 } 20208 case SD_SCSI3_PREEMPTANDABORT: { 20209 mhioc_preemptandabort_t *ptr = 20210 (mhioc_preemptandabort_t *)usr_bufp; 20211 20212 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20213 bcopy(ptr->victim_key.key, prp->service_key, 20214 MHIOC_RESV_KEY_SIZE); 20215 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20216 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20217 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20218 break; 20219 } 20220 case SD_SCSI3_REGISTERANDIGNOREKEY: 20221 { 20222 mhioc_registerandignorekey_t *ptr; 20223 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20224 bcopy(ptr->newkey.key, 20225 prp->service_key, MHIOC_RESV_KEY_SIZE); 20226 prp->aptpl = ptr->aptpl; 20227 break; 20228 } 20229 default: 20230 ASSERT(FALSE); 20231 break; 20232 } 20233 20234 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20235 UIO_SYSSPACE, SD_PATH_STANDARD); 20236 20237 switch (status) { 20238 case 0: 20239 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20240 break; /* Success! */ 20241 case EIO: 20242 switch (ucmd_buf.uscsi_status) { 20243 case STATUS_RESERVATION_CONFLICT: 20244 status = EACCES; 20245 break; 20246 case STATUS_CHECK: 20247 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20248 (scsi_sense_key((uint8_t *)&sense_buf) == 20249 KEY_ILLEGAL_REQUEST)) { 20250 status = ENOTSUP; 20251 } 20252 break; 20253 default: 20254 break; 20255 } 20256 break; 20257 default: 20258 break; 20259 } 20260 20261 kmem_free(prp, data_len); 20262 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20263 return (status); 20264 } 20265 20266 20267 /* 20268 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20269 * 20270 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20271 * 20272 * Arguments: un - pointer to the target's soft state struct 20273 * dkc - pointer to the callback structure 20274 * 20275 * Return Code: 0 - success 20276 * errno-type error code 20277 * 20278 * Context: kernel thread context only. 20279 * 20280 * _______________________________________________________________ 20281 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 20282 * |FLUSH_VOLATILE| | operation | 20283 * |______________|______________|_________________________________| 20284 * | 0 | NULL | Synchronous flush on both | 20285 * | | | volatile and non-volatile cache | 20286 * |______________|______________|_________________________________| 20287 * | 1 | NULL | Synchronous flush on volatile | 20288 * | | | cache; disk drivers may suppress| 20289 * | | | flush if disk table indicates | 20290 * | | | non-volatile cache | 20291 * |______________|______________|_________________________________| 20292 * | 0 | !NULL | Asynchronous flush on both | 20293 * | | | volatile and non-volatile cache;| 20294 * |______________|______________|_________________________________| 20295 * | 1 | !NULL | Asynchronous flush on volatile | 20296 * | | | cache; disk drivers may suppress| 20297 * | | | flush if disk table indicates | 20298 * | | | non-volatile cache | 20299 * |______________|______________|_________________________________| 20300 * 20301 */ 20302 20303 static int 20304 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20305 { 20306 struct sd_uscsi_info *uip; 20307 struct uscsi_cmd *uscmd; 20308 union scsi_cdb *cdb; 20309 struct buf *bp; 20310 int rval = 0; 20311 int is_async; 20312 20313 SD_TRACE(SD_LOG_IO, un, 20314 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20315 20316 ASSERT(un != NULL); 20317 ASSERT(!mutex_owned(SD_MUTEX(un))); 20318 20319 if (dkc == NULL || dkc->dkc_callback == NULL) { 20320 is_async = FALSE; 20321 } else { 20322 is_async = TRUE; 20323 } 20324 20325 mutex_enter(SD_MUTEX(un)); 20326 /* check whether cache flush should be suppressed */ 20327 if (un->un_f_suppress_cache_flush == TRUE) { 20328 mutex_exit(SD_MUTEX(un)); 20329 /* 20330 * suppress the cache flush if the device is told to do 20331 * so by sd.conf or disk table 20332 */ 20333 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 20334 skip the cache flush since suppress_cache_flush is %d!\n", 20335 un->un_f_suppress_cache_flush); 20336 20337 if (is_async == TRUE) { 20338 /* invoke callback for asynchronous flush */ 20339 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 20340 } 20341 return (rval); 20342 } 20343 mutex_exit(SD_MUTEX(un)); 20344 20345 /* 20346 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 20347 * set properly 20348 */ 20349 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20350 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20351 20352 mutex_enter(SD_MUTEX(un)); 20353 if (dkc != NULL && un->un_f_sync_nv_supported && 20354 (dkc->dkc_flag & FLUSH_VOLATILE)) { 20355 /* 20356 * if the device supports SYNC_NV bit, turn on 20357 * the SYNC_NV bit to only flush volatile cache 20358 */ 20359 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 20360 } 20361 mutex_exit(SD_MUTEX(un)); 20362 20363 /* 20364 * First get some memory for the uscsi_cmd struct and cdb 20365 * and initialize for SYNCHRONIZE_CACHE cmd. 20366 */ 20367 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20368 uscmd->uscsi_cdblen = CDB_GROUP1; 20369 uscmd->uscsi_cdb = (caddr_t)cdb; 20370 uscmd->uscsi_bufaddr = NULL; 20371 uscmd->uscsi_buflen = 0; 20372 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20373 uscmd->uscsi_rqlen = SENSE_LENGTH; 20374 uscmd->uscsi_rqresid = SENSE_LENGTH; 20375 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20376 uscmd->uscsi_timeout = sd_io_time; 20377 20378 /* 20379 * Allocate an sd_uscsi_info struct and fill it with the info 20380 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20381 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20382 * since we allocate the buf here in this function, we do not 20383 * need to preserve the prior contents of b_private. 20384 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20385 */ 20386 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20387 uip->ui_flags = SD_PATH_DIRECT; 20388 uip->ui_cmdp = uscmd; 20389 20390 bp = getrbuf(KM_SLEEP); 20391 bp->b_private = uip; 20392 20393 /* 20394 * Setup buffer to carry uscsi request. 20395 */ 20396 bp->b_flags = B_BUSY; 20397 bp->b_bcount = 0; 20398 bp->b_blkno = 0; 20399 20400 if (is_async == TRUE) { 20401 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20402 uip->ui_dkc = *dkc; 20403 } 20404 20405 bp->b_edev = SD_GET_DEV(un); 20406 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20407 20408 /* 20409 * Unset un_f_sync_cache_required flag 20410 */ 20411 mutex_enter(SD_MUTEX(un)); 20412 un->un_f_sync_cache_required = FALSE; 20413 mutex_exit(SD_MUTEX(un)); 20414 20415 (void) sd_uscsi_strategy(bp); 20416 20417 /* 20418 * If synchronous request, wait for completion 20419 * If async just return and let b_iodone callback 20420 * cleanup. 20421 * NOTE: On return, u_ncmds_in_driver will be decremented, 20422 * but it was also incremented in sd_uscsi_strategy(), so 20423 * we should be ok. 20424 */ 20425 if (is_async == FALSE) { 20426 (void) biowait(bp); 20427 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20428 } 20429 20430 return (rval); 20431 } 20432 20433 20434 static int 20435 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20436 { 20437 struct sd_uscsi_info *uip; 20438 struct uscsi_cmd *uscmd; 20439 uint8_t *sense_buf; 20440 struct sd_lun *un; 20441 int status; 20442 union scsi_cdb *cdb; 20443 20444 uip = (struct sd_uscsi_info *)(bp->b_private); 20445 ASSERT(uip != NULL); 20446 20447 uscmd = uip->ui_cmdp; 20448 ASSERT(uscmd != NULL); 20449 20450 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20451 ASSERT(sense_buf != NULL); 20452 20453 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20454 ASSERT(un != NULL); 20455 20456 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 20457 20458 status = geterror(bp); 20459 switch (status) { 20460 case 0: 20461 break; /* Success! */ 20462 case EIO: 20463 switch (uscmd->uscsi_status) { 20464 case STATUS_RESERVATION_CONFLICT: 20465 /* Ignore reservation conflict */ 20466 status = 0; 20467 goto done; 20468 20469 case STATUS_CHECK: 20470 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20471 (scsi_sense_key(sense_buf) == 20472 KEY_ILLEGAL_REQUEST)) { 20473 /* Ignore Illegal Request error */ 20474 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) { 20475 mutex_enter(SD_MUTEX(un)); 20476 un->un_f_sync_nv_supported = FALSE; 20477 mutex_exit(SD_MUTEX(un)); 20478 status = 0; 20479 SD_TRACE(SD_LOG_IO, un, 20480 "un_f_sync_nv_supported \ 20481 is set to false.\n"); 20482 goto done; 20483 } 20484 20485 mutex_enter(SD_MUTEX(un)); 20486 un->un_f_sync_cache_supported = FALSE; 20487 mutex_exit(SD_MUTEX(un)); 20488 SD_TRACE(SD_LOG_IO, un, 20489 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 20490 un_f_sync_cache_supported set to false \ 20491 with asc = %x, ascq = %x\n", 20492 scsi_sense_asc(sense_buf), 20493 scsi_sense_ascq(sense_buf)); 20494 status = ENOTSUP; 20495 goto done; 20496 } 20497 break; 20498 default: 20499 break; 20500 } 20501 /* FALLTHRU */ 20502 default: 20503 /* 20504 * Turn on the un_f_sync_cache_required flag 20505 * since the SYNC CACHE command failed 20506 */ 20507 mutex_enter(SD_MUTEX(un)); 20508 un->un_f_sync_cache_required = TRUE; 20509 mutex_exit(SD_MUTEX(un)); 20510 20511 /* 20512 * Don't log an error message if this device 20513 * has removable media. 20514 */ 20515 if (!un->un_f_has_removable_media) { 20516 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20517 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20518 } 20519 break; 20520 } 20521 20522 done: 20523 if (uip->ui_dkc.dkc_callback != NULL) { 20524 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20525 } 20526 20527 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20528 freerbuf(bp); 20529 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20530 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20531 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20532 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20533 20534 return (status); 20535 } 20536 20537 20538 /* 20539 * Function: sd_send_scsi_GET_CONFIGURATION 20540 * 20541 * Description: Issues the get configuration command to the device. 20542 * Called from sd_check_for_writable_cd & sd_get_media_info 20543 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20544 * Arguments: ssc 20545 * ucmdbuf 20546 * rqbuf 20547 * rqbuflen 20548 * bufaddr 20549 * buflen 20550 * path_flag 20551 * 20552 * Return Code: 0 - Success 20553 * errno return code from sd_ssc_send() 20554 * 20555 * Context: Can sleep. Does not return until command is completed. 20556 * 20557 */ 20558 20559 static int 20560 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf, 20561 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 20562 int path_flag) 20563 { 20564 char cdb[CDB_GROUP1]; 20565 int status; 20566 struct sd_lun *un; 20567 20568 ASSERT(ssc != NULL); 20569 un = ssc->ssc_un; 20570 ASSERT(un != NULL); 20571 ASSERT(!mutex_owned(SD_MUTEX(un))); 20572 ASSERT(bufaddr != NULL); 20573 ASSERT(ucmdbuf != NULL); 20574 ASSERT(rqbuf != NULL); 20575 20576 SD_TRACE(SD_LOG_IO, un, 20577 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20578 20579 bzero(cdb, sizeof (cdb)); 20580 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20581 bzero(rqbuf, rqbuflen); 20582 bzero(bufaddr, buflen); 20583 20584 /* 20585 * Set up cdb field for the get configuration command. 20586 */ 20587 cdb[0] = SCMD_GET_CONFIGURATION; 20588 cdb[1] = 0x02; /* Requested Type */ 20589 cdb[8] = SD_PROFILE_HEADER_LEN; 20590 ucmdbuf->uscsi_cdb = cdb; 20591 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20592 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20593 ucmdbuf->uscsi_buflen = buflen; 20594 ucmdbuf->uscsi_timeout = sd_io_time; 20595 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20596 ucmdbuf->uscsi_rqlen = rqbuflen; 20597 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20598 20599 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 20600 UIO_SYSSPACE, path_flag); 20601 20602 switch (status) { 20603 case 0: 20604 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20605 break; /* Success! */ 20606 case EIO: 20607 switch (ucmdbuf->uscsi_status) { 20608 case STATUS_RESERVATION_CONFLICT: 20609 status = EACCES; 20610 break; 20611 default: 20612 break; 20613 } 20614 break; 20615 default: 20616 break; 20617 } 20618 20619 if (status == 0) { 20620 SD_DUMP_MEMORY(un, SD_LOG_IO, 20621 "sd_send_scsi_GET_CONFIGURATION: data", 20622 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20623 } 20624 20625 SD_TRACE(SD_LOG_IO, un, 20626 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20627 20628 return (status); 20629 } 20630 20631 /* 20632 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20633 * 20634 * Description: Issues the get configuration command to the device to 20635 * retrieve a specific feature. Called from 20636 * sd_check_for_writable_cd & sd_set_mmc_caps. 20637 * Arguments: ssc 20638 * ucmdbuf 20639 * rqbuf 20640 * rqbuflen 20641 * bufaddr 20642 * buflen 20643 * feature 20644 * 20645 * Return Code: 0 - Success 20646 * errno return code from sd_ssc_send() 20647 * 20648 * Context: Can sleep. Does not return until command is completed. 20649 * 20650 */ 20651 static int 20652 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, 20653 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20654 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 20655 { 20656 char cdb[CDB_GROUP1]; 20657 int status; 20658 struct sd_lun *un; 20659 20660 ASSERT(ssc != NULL); 20661 un = ssc->ssc_un; 20662 ASSERT(un != NULL); 20663 ASSERT(!mutex_owned(SD_MUTEX(un))); 20664 ASSERT(bufaddr != NULL); 20665 ASSERT(ucmdbuf != NULL); 20666 ASSERT(rqbuf != NULL); 20667 20668 SD_TRACE(SD_LOG_IO, un, 20669 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20670 20671 bzero(cdb, sizeof (cdb)); 20672 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20673 bzero(rqbuf, rqbuflen); 20674 bzero(bufaddr, buflen); 20675 20676 /* 20677 * Set up cdb field for the get configuration command. 20678 */ 20679 cdb[0] = SCMD_GET_CONFIGURATION; 20680 cdb[1] = 0x02; /* Requested Type */ 20681 cdb[3] = feature; 20682 cdb[8] = buflen; 20683 ucmdbuf->uscsi_cdb = cdb; 20684 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20685 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20686 ucmdbuf->uscsi_buflen = buflen; 20687 ucmdbuf->uscsi_timeout = sd_io_time; 20688 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20689 ucmdbuf->uscsi_rqlen = rqbuflen; 20690 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20691 20692 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL, 20693 UIO_SYSSPACE, path_flag); 20694 20695 switch (status) { 20696 case 0: 20697 20698 break; /* Success! */ 20699 case EIO: 20700 switch (ucmdbuf->uscsi_status) { 20701 case STATUS_RESERVATION_CONFLICT: 20702 status = EACCES; 20703 break; 20704 default: 20705 break; 20706 } 20707 break; 20708 default: 20709 break; 20710 } 20711 20712 if (status == 0) { 20713 SD_DUMP_MEMORY(un, SD_LOG_IO, 20714 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20715 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20716 } 20717 20718 SD_TRACE(SD_LOG_IO, un, 20719 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20720 20721 return (status); 20722 } 20723 20724 20725 /* 20726 * Function: sd_send_scsi_MODE_SENSE 20727 * 20728 * Description: Utility function for issuing a scsi MODE SENSE command. 20729 * Note: This routine uses a consistent implementation for Group0, 20730 * Group1, and Group2 commands across all platforms. ATAPI devices 20731 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20732 * 20733 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20734 * structure for this target. 20735 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20736 * CDB_GROUP[1|2] (10 byte). 20737 * bufaddr - buffer for page data retrieved from the target. 20738 * buflen - size of page to be retrieved. 20739 * page_code - page code of data to be retrieved from the target. 20740 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20741 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20742 * to use the USCSI "direct" chain and bypass the normal 20743 * command waitq. 20744 * 20745 * Return Code: 0 - Success 20746 * errno return code from sd_ssc_send() 20747 * 20748 * Context: Can sleep. Does not return until command is completed. 20749 */ 20750 20751 static int 20752 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 20753 size_t buflen, uchar_t page_code, int path_flag) 20754 { 20755 struct scsi_extended_sense sense_buf; 20756 union scsi_cdb cdb; 20757 struct uscsi_cmd ucmd_buf; 20758 int status; 20759 int headlen; 20760 struct sd_lun *un; 20761 20762 ASSERT(ssc != NULL); 20763 un = ssc->ssc_un; 20764 ASSERT(un != NULL); 20765 ASSERT(!mutex_owned(SD_MUTEX(un))); 20766 ASSERT(bufaddr != NULL); 20767 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20768 (cdbsize == CDB_GROUP2)); 20769 20770 SD_TRACE(SD_LOG_IO, un, 20771 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20772 20773 bzero(&cdb, sizeof (cdb)); 20774 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20775 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20776 bzero(bufaddr, buflen); 20777 20778 if (cdbsize == CDB_GROUP0) { 20779 cdb.scc_cmd = SCMD_MODE_SENSE; 20780 cdb.cdb_opaque[2] = page_code; 20781 FORMG0COUNT(&cdb, buflen); 20782 headlen = MODE_HEADER_LENGTH; 20783 } else { 20784 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20785 cdb.cdb_opaque[2] = page_code; 20786 FORMG1COUNT(&cdb, buflen); 20787 headlen = MODE_HEADER_LENGTH_GRP2; 20788 } 20789 20790 ASSERT(headlen <= buflen); 20791 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20792 20793 ucmd_buf.uscsi_cdb = (char *)&cdb; 20794 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20795 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20796 ucmd_buf.uscsi_buflen = buflen; 20797 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20798 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20799 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20800 ucmd_buf.uscsi_timeout = 60; 20801 20802 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20803 UIO_SYSSPACE, path_flag); 20804 20805 switch (status) { 20806 case 0: 20807 /* 20808 * sr_check_wp() uses 0x3f page code and check the header of 20809 * mode page to determine if target device is write-protected. 20810 * But some USB devices return 0 bytes for 0x3f page code. For 20811 * this case, make sure that mode page header is returned at 20812 * least. 20813 */ 20814 if (buflen - ucmd_buf.uscsi_resid < headlen) { 20815 status = EIO; 20816 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1, 20817 "mode page header is not returned"); 20818 } 20819 break; /* Success! */ 20820 case EIO: 20821 switch (ucmd_buf.uscsi_status) { 20822 case STATUS_RESERVATION_CONFLICT: 20823 status = EACCES; 20824 break; 20825 default: 20826 break; 20827 } 20828 break; 20829 default: 20830 break; 20831 } 20832 20833 if (status == 0) { 20834 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20835 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20836 } 20837 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20838 20839 return (status); 20840 } 20841 20842 20843 /* 20844 * Function: sd_send_scsi_MODE_SELECT 20845 * 20846 * Description: Utility function for issuing a scsi MODE SELECT command. 20847 * Note: This routine uses a consistent implementation for Group0, 20848 * Group1, and Group2 commands across all platforms. ATAPI devices 20849 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20850 * 20851 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20852 * structure for this target. 20853 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20854 * CDB_GROUP[1|2] (10 byte). 20855 * bufaddr - buffer for page data retrieved from the target. 20856 * buflen - size of page to be retrieved. 20857 * save_page - boolean to determin if SP bit should be set. 20858 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20859 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20860 * to use the USCSI "direct" chain and bypass the normal 20861 * command waitq. 20862 * 20863 * Return Code: 0 - Success 20864 * errno return code from sd_ssc_send() 20865 * 20866 * Context: Can sleep. Does not return until command is completed. 20867 */ 20868 20869 static int 20870 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr, 20871 size_t buflen, uchar_t save_page, int path_flag) 20872 { 20873 struct scsi_extended_sense sense_buf; 20874 union scsi_cdb cdb; 20875 struct uscsi_cmd ucmd_buf; 20876 int status; 20877 struct sd_lun *un; 20878 20879 ASSERT(ssc != NULL); 20880 un = ssc->ssc_un; 20881 ASSERT(un != NULL); 20882 ASSERT(!mutex_owned(SD_MUTEX(un))); 20883 ASSERT(bufaddr != NULL); 20884 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20885 (cdbsize == CDB_GROUP2)); 20886 20887 SD_TRACE(SD_LOG_IO, un, 20888 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20889 20890 bzero(&cdb, sizeof (cdb)); 20891 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20892 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20893 20894 /* Set the PF bit for many third party drives */ 20895 cdb.cdb_opaque[1] = 0x10; 20896 20897 /* Set the savepage(SP) bit if given */ 20898 if (save_page == SD_SAVE_PAGE) { 20899 cdb.cdb_opaque[1] |= 0x01; 20900 } 20901 20902 if (cdbsize == CDB_GROUP0) { 20903 cdb.scc_cmd = SCMD_MODE_SELECT; 20904 FORMG0COUNT(&cdb, buflen); 20905 } else { 20906 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20907 FORMG1COUNT(&cdb, buflen); 20908 } 20909 20910 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20911 20912 ucmd_buf.uscsi_cdb = (char *)&cdb; 20913 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20914 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20915 ucmd_buf.uscsi_buflen = buflen; 20916 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20917 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20918 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20919 ucmd_buf.uscsi_timeout = 60; 20920 20921 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 20922 UIO_SYSSPACE, path_flag); 20923 20924 switch (status) { 20925 case 0: 20926 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 20927 break; /* Success! */ 20928 case EIO: 20929 switch (ucmd_buf.uscsi_status) { 20930 case STATUS_RESERVATION_CONFLICT: 20931 status = EACCES; 20932 break; 20933 default: 20934 break; 20935 } 20936 break; 20937 default: 20938 break; 20939 } 20940 20941 if (status == 0) { 20942 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20943 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20944 } 20945 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20946 20947 return (status); 20948 } 20949 20950 20951 /* 20952 * Function: sd_send_scsi_RDWR 20953 * 20954 * Description: Issue a scsi READ or WRITE command with the given parameters. 20955 * 20956 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 20957 * structure for this target. 20958 * cmd: SCMD_READ or SCMD_WRITE 20959 * bufaddr: Address of caller's buffer to receive the RDWR data 20960 * buflen: Length of caller's buffer receive the RDWR data. 20961 * start_block: Block number for the start of the RDWR operation. 20962 * (Assumes target-native block size.) 20963 * residp: Pointer to variable to receive the redisual of the 20964 * RDWR operation (may be NULL of no residual requested). 20965 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20966 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20967 * to use the USCSI "direct" chain and bypass the normal 20968 * command waitq. 20969 * 20970 * Return Code: 0 - Success 20971 * errno return code from sd_ssc_send() 20972 * 20973 * Context: Can sleep. Does not return until command is completed. 20974 */ 20975 20976 static int 20977 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr, 20978 size_t buflen, daddr_t start_block, int path_flag) 20979 { 20980 struct scsi_extended_sense sense_buf; 20981 union scsi_cdb cdb; 20982 struct uscsi_cmd ucmd_buf; 20983 uint32_t block_count; 20984 int status; 20985 int cdbsize; 20986 uchar_t flag; 20987 struct sd_lun *un; 20988 20989 ASSERT(ssc != NULL); 20990 un = ssc->ssc_un; 20991 ASSERT(un != NULL); 20992 ASSERT(!mutex_owned(SD_MUTEX(un))); 20993 ASSERT(bufaddr != NULL); 20994 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 20995 20996 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 20997 20998 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 20999 return (EINVAL); 21000 } 21001 21002 mutex_enter(SD_MUTEX(un)); 21003 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21004 mutex_exit(SD_MUTEX(un)); 21005 21006 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21007 21008 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21009 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21010 bufaddr, buflen, start_block, block_count); 21011 21012 bzero(&cdb, sizeof (cdb)); 21013 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21014 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21015 21016 /* Compute CDB size to use */ 21017 if (start_block > 0xffffffff) 21018 cdbsize = CDB_GROUP4; 21019 else if ((start_block & 0xFFE00000) || 21020 (un->un_f_cfg_is_atapi == TRUE)) 21021 cdbsize = CDB_GROUP1; 21022 else 21023 cdbsize = CDB_GROUP0; 21024 21025 switch (cdbsize) { 21026 case CDB_GROUP0: /* 6-byte CDBs */ 21027 cdb.scc_cmd = cmd; 21028 FORMG0ADDR(&cdb, start_block); 21029 FORMG0COUNT(&cdb, block_count); 21030 break; 21031 case CDB_GROUP1: /* 10-byte CDBs */ 21032 cdb.scc_cmd = cmd | SCMD_GROUP1; 21033 FORMG1ADDR(&cdb, start_block); 21034 FORMG1COUNT(&cdb, block_count); 21035 break; 21036 case CDB_GROUP4: /* 16-byte CDBs */ 21037 cdb.scc_cmd = cmd | SCMD_GROUP4; 21038 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21039 FORMG4COUNT(&cdb, block_count); 21040 break; 21041 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21042 default: 21043 /* All others reserved */ 21044 return (EINVAL); 21045 } 21046 21047 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21048 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21049 21050 ucmd_buf.uscsi_cdb = (char *)&cdb; 21051 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21052 ucmd_buf.uscsi_bufaddr = bufaddr; 21053 ucmd_buf.uscsi_buflen = buflen; 21054 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21055 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21056 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21057 ucmd_buf.uscsi_timeout = 60; 21058 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21059 UIO_SYSSPACE, path_flag); 21060 21061 switch (status) { 21062 case 0: 21063 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21064 break; /* Success! */ 21065 case EIO: 21066 switch (ucmd_buf.uscsi_status) { 21067 case STATUS_RESERVATION_CONFLICT: 21068 status = EACCES; 21069 break; 21070 default: 21071 break; 21072 } 21073 break; 21074 default: 21075 break; 21076 } 21077 21078 if (status == 0) { 21079 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21080 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21081 } 21082 21083 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21084 21085 return (status); 21086 } 21087 21088 21089 /* 21090 * Function: sd_send_scsi_LOG_SENSE 21091 * 21092 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21093 * 21094 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 21095 * structure for this target. 21096 * 21097 * Return Code: 0 - Success 21098 * errno return code from sd_ssc_send() 21099 * 21100 * Context: Can sleep. Does not return until command is completed. 21101 */ 21102 21103 static int 21104 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen, 21105 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21106 int path_flag) 21107 21108 { 21109 struct scsi_extended_sense sense_buf; 21110 union scsi_cdb cdb; 21111 struct uscsi_cmd ucmd_buf; 21112 int status; 21113 struct sd_lun *un; 21114 21115 ASSERT(ssc != NULL); 21116 un = ssc->ssc_un; 21117 ASSERT(un != NULL); 21118 ASSERT(!mutex_owned(SD_MUTEX(un))); 21119 21120 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21121 21122 bzero(&cdb, sizeof (cdb)); 21123 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21124 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21125 21126 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21127 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21128 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21129 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21130 FORMG1COUNT(&cdb, buflen); 21131 21132 ucmd_buf.uscsi_cdb = (char *)&cdb; 21133 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21134 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21135 ucmd_buf.uscsi_buflen = buflen; 21136 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21137 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21138 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21139 ucmd_buf.uscsi_timeout = 60; 21140 21141 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, 21142 UIO_SYSSPACE, path_flag); 21143 21144 switch (status) { 21145 case 0: 21146 break; 21147 case EIO: 21148 switch (ucmd_buf.uscsi_status) { 21149 case STATUS_RESERVATION_CONFLICT: 21150 status = EACCES; 21151 break; 21152 case STATUS_CHECK: 21153 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21154 (scsi_sense_key((uint8_t *)&sense_buf) == 21155 KEY_ILLEGAL_REQUEST) && 21156 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21157 /* 21158 * ASC 0x24: INVALID FIELD IN CDB 21159 */ 21160 switch (page_code) { 21161 case START_STOP_CYCLE_PAGE: 21162 /* 21163 * The start stop cycle counter is 21164 * implemented as page 0x31 in earlier 21165 * generation disks. In new generation 21166 * disks the start stop cycle counter is 21167 * implemented as page 0xE. To properly 21168 * handle this case if an attempt for 21169 * log page 0xE is made and fails we 21170 * will try again using page 0x31. 21171 * 21172 * Network storage BU committed to 21173 * maintain the page 0x31 for this 21174 * purpose and will not have any other 21175 * page implemented with page code 0x31 21176 * until all disks transition to the 21177 * standard page. 21178 */ 21179 mutex_enter(SD_MUTEX(un)); 21180 un->un_start_stop_cycle_page = 21181 START_STOP_CYCLE_VU_PAGE; 21182 cdb.cdb_opaque[2] = 21183 (char)(page_control << 6) | 21184 un->un_start_stop_cycle_page; 21185 mutex_exit(SD_MUTEX(un)); 21186 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 21187 status = sd_ssc_send( 21188 ssc, &ucmd_buf, FKIOCTL, 21189 UIO_SYSSPACE, path_flag); 21190 21191 break; 21192 case TEMPERATURE_PAGE: 21193 status = ENOTTY; 21194 break; 21195 default: 21196 break; 21197 } 21198 } 21199 break; 21200 default: 21201 break; 21202 } 21203 break; 21204 default: 21205 break; 21206 } 21207 21208 if (status == 0) { 21209 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21210 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21211 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21212 } 21213 21214 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21215 21216 return (status); 21217 } 21218 21219 21220 /* 21221 * Function: sdioctl 21222 * 21223 * Description: Driver's ioctl(9e) entry point function. 21224 * 21225 * Arguments: dev - device number 21226 * cmd - ioctl operation to be performed 21227 * arg - user argument, contains data to be set or reference 21228 * parameter for get 21229 * flag - bit flag, indicating open settings, 32/64 bit type 21230 * cred_p - user credential pointer 21231 * rval_p - calling process return value (OPT) 21232 * 21233 * Return Code: EINVAL 21234 * ENOTTY 21235 * ENXIO 21236 * EIO 21237 * EFAULT 21238 * ENOTSUP 21239 * EPERM 21240 * 21241 * Context: Called from the device switch at normal priority. 21242 */ 21243 21244 static int 21245 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 21246 { 21247 struct sd_lun *un = NULL; 21248 int err = 0; 21249 int i = 0; 21250 cred_t *cr; 21251 int tmprval = EINVAL; 21252 int is_valid; 21253 sd_ssc_t *ssc; 21254 21255 /* 21256 * All device accesses go thru sdstrategy where we check on suspend 21257 * status 21258 */ 21259 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21260 return (ENXIO); 21261 } 21262 21263 ASSERT(!mutex_owned(SD_MUTEX(un))); 21264 21265 /* Initialize sd_ssc_t for internal uscsi commands */ 21266 ssc = sd_ssc_init(un); 21267 21268 is_valid = SD_IS_VALID_LABEL(un); 21269 21270 /* 21271 * Moved this wait from sd_uscsi_strategy to here for 21272 * reasons of deadlock prevention. Internal driver commands, 21273 * specifically those to change a devices power level, result 21274 * in a call to sd_uscsi_strategy. 21275 */ 21276 mutex_enter(SD_MUTEX(un)); 21277 while ((un->un_state == SD_STATE_SUSPENDED) || 21278 (un->un_state == SD_STATE_PM_CHANGING)) { 21279 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 21280 } 21281 /* 21282 * Twiddling the counter here protects commands from now 21283 * through to the top of sd_uscsi_strategy. Without the 21284 * counter inc. a power down, for example, could get in 21285 * after the above check for state is made and before 21286 * execution gets to the top of sd_uscsi_strategy. 21287 * That would cause problems. 21288 */ 21289 un->un_ncmds_in_driver++; 21290 21291 if (!is_valid && 21292 (flag & (FNDELAY | FNONBLOCK))) { 21293 switch (cmd) { 21294 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 21295 case DKIOCGVTOC: 21296 case DKIOCGEXTVTOC: 21297 case DKIOCGAPART: 21298 case DKIOCPARTINFO: 21299 case DKIOCEXTPARTINFO: 21300 case DKIOCSGEOM: 21301 case DKIOCSAPART: 21302 case DKIOCGETEFI: 21303 case DKIOCPARTITION: 21304 case DKIOCSVTOC: 21305 case DKIOCSEXTVTOC: 21306 case DKIOCSETEFI: 21307 case DKIOCGMBOOT: 21308 case DKIOCSMBOOT: 21309 case DKIOCG_PHYGEOM: 21310 case DKIOCG_VIRTGEOM: 21311 /* let cmlb handle it */ 21312 goto skip_ready_valid; 21313 21314 case CDROMPAUSE: 21315 case CDROMRESUME: 21316 case CDROMPLAYMSF: 21317 case CDROMPLAYTRKIND: 21318 case CDROMREADTOCHDR: 21319 case CDROMREADTOCENTRY: 21320 case CDROMSTOP: 21321 case CDROMSTART: 21322 case CDROMVOLCTRL: 21323 case CDROMSUBCHNL: 21324 case CDROMREADMODE2: 21325 case CDROMREADMODE1: 21326 case CDROMREADOFFSET: 21327 case CDROMSBLKMODE: 21328 case CDROMGBLKMODE: 21329 case CDROMGDRVSPEED: 21330 case CDROMSDRVSPEED: 21331 case CDROMCDDA: 21332 case CDROMCDXA: 21333 case CDROMSUBCODE: 21334 if (!ISCD(un)) { 21335 un->un_ncmds_in_driver--; 21336 ASSERT(un->un_ncmds_in_driver >= 0); 21337 mutex_exit(SD_MUTEX(un)); 21338 err = ENOTTY; 21339 goto done_without_assess; 21340 } 21341 break; 21342 case FDEJECT: 21343 case DKIOCEJECT: 21344 case CDROMEJECT: 21345 if (!un->un_f_eject_media_supported) { 21346 un->un_ncmds_in_driver--; 21347 ASSERT(un->un_ncmds_in_driver >= 0); 21348 mutex_exit(SD_MUTEX(un)); 21349 err = ENOTTY; 21350 goto done_without_assess; 21351 } 21352 break; 21353 case DKIOCFLUSHWRITECACHE: 21354 mutex_exit(SD_MUTEX(un)); 21355 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 21356 if (err != 0) { 21357 mutex_enter(SD_MUTEX(un)); 21358 un->un_ncmds_in_driver--; 21359 ASSERT(un->un_ncmds_in_driver >= 0); 21360 mutex_exit(SD_MUTEX(un)); 21361 err = EIO; 21362 goto done_quick_assess; 21363 } 21364 mutex_enter(SD_MUTEX(un)); 21365 /* FALLTHROUGH */ 21366 case DKIOCREMOVABLE: 21367 case DKIOCHOTPLUGGABLE: 21368 case DKIOCINFO: 21369 case DKIOCGMEDIAINFO: 21370 case MHIOCENFAILFAST: 21371 case MHIOCSTATUS: 21372 case MHIOCTKOWN: 21373 case MHIOCRELEASE: 21374 case MHIOCGRP_INKEYS: 21375 case MHIOCGRP_INRESV: 21376 case MHIOCGRP_REGISTER: 21377 case MHIOCGRP_RESERVE: 21378 case MHIOCGRP_PREEMPTANDABORT: 21379 case MHIOCGRP_REGISTERANDIGNOREKEY: 21380 case CDROMCLOSETRAY: 21381 case USCSICMD: 21382 goto skip_ready_valid; 21383 default: 21384 break; 21385 } 21386 21387 mutex_exit(SD_MUTEX(un)); 21388 err = sd_ready_and_valid(ssc, SDPART(dev)); 21389 mutex_enter(SD_MUTEX(un)); 21390 21391 if (err != SD_READY_VALID) { 21392 switch (cmd) { 21393 case DKIOCSTATE: 21394 case CDROMGDRVSPEED: 21395 case CDROMSDRVSPEED: 21396 case FDEJECT: /* for eject command */ 21397 case DKIOCEJECT: 21398 case CDROMEJECT: 21399 case DKIOCREMOVABLE: 21400 case DKIOCHOTPLUGGABLE: 21401 break; 21402 default: 21403 if (un->un_f_has_removable_media) { 21404 err = ENXIO; 21405 } else { 21406 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 21407 if (err == SD_RESERVED_BY_OTHERS) { 21408 err = EACCES; 21409 } else { 21410 err = EIO; 21411 } 21412 } 21413 un->un_ncmds_in_driver--; 21414 ASSERT(un->un_ncmds_in_driver >= 0); 21415 mutex_exit(SD_MUTEX(un)); 21416 21417 goto done_without_assess; 21418 } 21419 } 21420 } 21421 21422 skip_ready_valid: 21423 mutex_exit(SD_MUTEX(un)); 21424 21425 switch (cmd) { 21426 case DKIOCINFO: 21427 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21428 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21429 break; 21430 21431 case DKIOCGMEDIAINFO: 21432 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21433 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21434 break; 21435 21436 case DKIOCGGEOM: 21437 case DKIOCGVTOC: 21438 case DKIOCGEXTVTOC: 21439 case DKIOCGAPART: 21440 case DKIOCPARTINFO: 21441 case DKIOCEXTPARTINFO: 21442 case DKIOCSGEOM: 21443 case DKIOCSAPART: 21444 case DKIOCGETEFI: 21445 case DKIOCPARTITION: 21446 case DKIOCSVTOC: 21447 case DKIOCSEXTVTOC: 21448 case DKIOCSETEFI: 21449 case DKIOCGMBOOT: 21450 case DKIOCSMBOOT: 21451 case DKIOCG_PHYGEOM: 21452 case DKIOCG_VIRTGEOM: 21453 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 21454 21455 /* TUR should spin up */ 21456 21457 if (un->un_f_has_removable_media) 21458 err = sd_send_scsi_TEST_UNIT_READY(ssc, 21459 SD_CHECK_FOR_MEDIA); 21460 21461 else 21462 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 21463 21464 if (err != 0) 21465 goto done_with_assess; 21466 21467 err = cmlb_ioctl(un->un_cmlbhandle, dev, 21468 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 21469 21470 if ((err == 0) && 21471 ((cmd == DKIOCSETEFI) || 21472 (un->un_f_pkstats_enabled) && 21473 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC || 21474 cmd == DKIOCSEXTVTOC))) { 21475 21476 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 21477 (void *)SD_PATH_DIRECT); 21478 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 21479 sd_set_pstats(un); 21480 SD_TRACE(SD_LOG_IO_PARTITION, un, 21481 "sd_ioctl: un:0x%p pstats created and " 21482 "set\n", un); 21483 } 21484 } 21485 21486 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) || 21487 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 21488 21489 mutex_enter(SD_MUTEX(un)); 21490 if (un->un_f_devid_supported && 21491 (un->un_f_opt_fab_devid == TRUE)) { 21492 if (un->un_devid == NULL) { 21493 sd_register_devid(ssc, SD_DEVINFO(un), 21494 SD_TARGET_IS_UNRESERVED); 21495 } else { 21496 /* 21497 * The device id for this disk 21498 * has been fabricated. The 21499 * device id must be preserved 21500 * by writing it back out to 21501 * disk. 21502 */ 21503 if (sd_write_deviceid(ssc) != 0) { 21504 ddi_devid_free(un->un_devid); 21505 un->un_devid = NULL; 21506 } 21507 } 21508 } 21509 mutex_exit(SD_MUTEX(un)); 21510 } 21511 21512 break; 21513 21514 case DKIOCLOCK: 21515 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21516 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 21517 SD_PATH_STANDARD); 21518 goto done_with_assess; 21519 21520 case DKIOCUNLOCK: 21521 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21522 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 21523 SD_PATH_STANDARD); 21524 goto done_with_assess; 21525 21526 case DKIOCSTATE: { 21527 enum dkio_state state; 21528 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21529 21530 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21531 err = EFAULT; 21532 } else { 21533 err = sd_check_media(dev, state); 21534 if (err == 0) { 21535 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21536 sizeof (int), flag) != 0) 21537 err = EFAULT; 21538 } 21539 } 21540 break; 21541 } 21542 21543 case DKIOCREMOVABLE: 21544 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21545 i = un->un_f_has_removable_media ? 1 : 0; 21546 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21547 err = EFAULT; 21548 } else { 21549 err = 0; 21550 } 21551 break; 21552 21553 case DKIOCHOTPLUGGABLE: 21554 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21555 i = un->un_f_is_hotpluggable ? 1 : 0; 21556 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21557 err = EFAULT; 21558 } else { 21559 err = 0; 21560 } 21561 break; 21562 21563 case DKIOCGTEMPERATURE: 21564 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21565 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21566 break; 21567 21568 case MHIOCENFAILFAST: 21569 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21570 if ((err = drv_priv(cred_p)) == 0) { 21571 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21572 } 21573 break; 21574 21575 case MHIOCTKOWN: 21576 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21577 if ((err = drv_priv(cred_p)) == 0) { 21578 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21579 } 21580 break; 21581 21582 case MHIOCRELEASE: 21583 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21584 if ((err = drv_priv(cred_p)) == 0) { 21585 err = sd_mhdioc_release(dev); 21586 } 21587 break; 21588 21589 case MHIOCSTATUS: 21590 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21591 if ((err = drv_priv(cred_p)) == 0) { 21592 switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) { 21593 case 0: 21594 err = 0; 21595 break; 21596 case EACCES: 21597 *rval_p = 1; 21598 err = 0; 21599 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 21600 break; 21601 default: 21602 err = EIO; 21603 goto done_with_assess; 21604 } 21605 } 21606 break; 21607 21608 case MHIOCQRESERVE: 21609 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21610 if ((err = drv_priv(cred_p)) == 0) { 21611 err = sd_reserve_release(dev, SD_RESERVE); 21612 } 21613 break; 21614 21615 case MHIOCREREGISTERDEVID: 21616 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21617 if (drv_priv(cred_p) == EPERM) { 21618 err = EPERM; 21619 } else if (!un->un_f_devid_supported) { 21620 err = ENOTTY; 21621 } else { 21622 err = sd_mhdioc_register_devid(dev); 21623 } 21624 break; 21625 21626 case MHIOCGRP_INKEYS: 21627 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21628 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21629 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21630 err = ENOTSUP; 21631 } else { 21632 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21633 flag); 21634 } 21635 } 21636 break; 21637 21638 case MHIOCGRP_INRESV: 21639 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21640 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21641 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21642 err = ENOTSUP; 21643 } else { 21644 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21645 } 21646 } 21647 break; 21648 21649 case MHIOCGRP_REGISTER: 21650 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21651 if ((err = drv_priv(cred_p)) != EPERM) { 21652 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21653 err = ENOTSUP; 21654 } else if (arg != NULL) { 21655 mhioc_register_t reg; 21656 if (ddi_copyin((void *)arg, ®, 21657 sizeof (mhioc_register_t), flag) != 0) { 21658 err = EFAULT; 21659 } else { 21660 err = 21661 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21662 ssc, SD_SCSI3_REGISTER, 21663 (uchar_t *)®); 21664 if (err != 0) 21665 goto done_with_assess; 21666 } 21667 } 21668 } 21669 break; 21670 21671 case MHIOCGRP_RESERVE: 21672 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21673 if ((err = drv_priv(cred_p)) != EPERM) { 21674 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21675 err = ENOTSUP; 21676 } else if (arg != NULL) { 21677 mhioc_resv_desc_t resv_desc; 21678 if (ddi_copyin((void *)arg, &resv_desc, 21679 sizeof (mhioc_resv_desc_t), flag) != 0) { 21680 err = EFAULT; 21681 } else { 21682 err = 21683 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21684 ssc, SD_SCSI3_RESERVE, 21685 (uchar_t *)&resv_desc); 21686 if (err != 0) 21687 goto done_with_assess; 21688 } 21689 } 21690 } 21691 break; 21692 21693 case MHIOCGRP_PREEMPTANDABORT: 21694 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21695 if ((err = drv_priv(cred_p)) != EPERM) { 21696 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21697 err = ENOTSUP; 21698 } else if (arg != NULL) { 21699 mhioc_preemptandabort_t preempt_abort; 21700 if (ddi_copyin((void *)arg, &preempt_abort, 21701 sizeof (mhioc_preemptandabort_t), 21702 flag) != 0) { 21703 err = EFAULT; 21704 } else { 21705 err = 21706 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21707 ssc, SD_SCSI3_PREEMPTANDABORT, 21708 (uchar_t *)&preempt_abort); 21709 if (err != 0) 21710 goto done_with_assess; 21711 } 21712 } 21713 } 21714 break; 21715 21716 case MHIOCGRP_REGISTERANDIGNOREKEY: 21717 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 21718 if ((err = drv_priv(cred_p)) != EPERM) { 21719 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21720 err = ENOTSUP; 21721 } else if (arg != NULL) { 21722 mhioc_registerandignorekey_t r_and_i; 21723 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21724 sizeof (mhioc_registerandignorekey_t), 21725 flag) != 0) { 21726 err = EFAULT; 21727 } else { 21728 err = 21729 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21730 ssc, SD_SCSI3_REGISTERANDIGNOREKEY, 21731 (uchar_t *)&r_and_i); 21732 if (err != 0) 21733 goto done_with_assess; 21734 } 21735 } 21736 } 21737 break; 21738 21739 case USCSICMD: 21740 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21741 cr = ddi_get_cred(); 21742 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21743 err = EPERM; 21744 } else { 21745 enum uio_seg uioseg; 21746 21747 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 21748 UIO_USERSPACE; 21749 if (un->un_f_format_in_progress == TRUE) { 21750 err = EAGAIN; 21751 break; 21752 } 21753 21754 err = sd_ssc_send(ssc, 21755 (struct uscsi_cmd *)arg, 21756 flag, uioseg, SD_PATH_STANDARD); 21757 if (err != 0) 21758 goto done_with_assess; 21759 else 21760 sd_ssc_assessment(ssc, SD_FMT_STANDARD); 21761 } 21762 break; 21763 21764 case CDROMPAUSE: 21765 case CDROMRESUME: 21766 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21767 if (!ISCD(un)) { 21768 err = ENOTTY; 21769 } else { 21770 err = sr_pause_resume(dev, cmd); 21771 } 21772 break; 21773 21774 case CDROMPLAYMSF: 21775 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21776 if (!ISCD(un)) { 21777 err = ENOTTY; 21778 } else { 21779 err = sr_play_msf(dev, (caddr_t)arg, flag); 21780 } 21781 break; 21782 21783 case CDROMPLAYTRKIND: 21784 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21785 #if defined(__i386) || defined(__amd64) 21786 /* 21787 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21788 */ 21789 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21790 #else 21791 if (!ISCD(un)) { 21792 #endif 21793 err = ENOTTY; 21794 } else { 21795 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21796 } 21797 break; 21798 21799 case CDROMREADTOCHDR: 21800 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21801 if (!ISCD(un)) { 21802 err = ENOTTY; 21803 } else { 21804 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21805 } 21806 break; 21807 21808 case CDROMREADTOCENTRY: 21809 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21810 if (!ISCD(un)) { 21811 err = ENOTTY; 21812 } else { 21813 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21814 } 21815 break; 21816 21817 case CDROMSTOP: 21818 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21819 if (!ISCD(un)) { 21820 err = ENOTTY; 21821 } else { 21822 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_STOP, 21823 SD_PATH_STANDARD); 21824 goto done_with_assess; 21825 } 21826 break; 21827 21828 case CDROMSTART: 21829 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21830 if (!ISCD(un)) { 21831 err = ENOTTY; 21832 } else { 21833 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_START, 21834 SD_PATH_STANDARD); 21835 goto done_with_assess; 21836 } 21837 break; 21838 21839 case CDROMCLOSETRAY: 21840 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21841 if (!ISCD(un)) { 21842 err = ENOTTY; 21843 } else { 21844 err = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_CLOSE, 21845 SD_PATH_STANDARD); 21846 goto done_with_assess; 21847 } 21848 break; 21849 21850 case FDEJECT: /* for eject command */ 21851 case DKIOCEJECT: 21852 case CDROMEJECT: 21853 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21854 if (!un->un_f_eject_media_supported) { 21855 err = ENOTTY; 21856 } else { 21857 err = sr_eject(dev); 21858 } 21859 break; 21860 21861 case CDROMVOLCTRL: 21862 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21863 if (!ISCD(un)) { 21864 err = ENOTTY; 21865 } else { 21866 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21867 } 21868 break; 21869 21870 case CDROMSUBCHNL: 21871 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21872 if (!ISCD(un)) { 21873 err = ENOTTY; 21874 } else { 21875 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21876 } 21877 break; 21878 21879 case CDROMREADMODE2: 21880 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21881 if (!ISCD(un)) { 21882 err = ENOTTY; 21883 } else if (un->un_f_cfg_is_atapi == TRUE) { 21884 /* 21885 * If the drive supports READ CD, use that instead of 21886 * switching the LBA size via a MODE SELECT 21887 * Block Descriptor 21888 */ 21889 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21890 } else { 21891 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21892 } 21893 break; 21894 21895 case CDROMREADMODE1: 21896 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21897 if (!ISCD(un)) { 21898 err = ENOTTY; 21899 } else { 21900 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21901 } 21902 break; 21903 21904 case CDROMREADOFFSET: 21905 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21906 if (!ISCD(un)) { 21907 err = ENOTTY; 21908 } else { 21909 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21910 flag); 21911 } 21912 break; 21913 21914 case CDROMSBLKMODE: 21915 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21916 /* 21917 * There is no means of changing block size in case of atapi 21918 * drives, thus return ENOTTY if drive type is atapi 21919 */ 21920 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21921 err = ENOTTY; 21922 } else if (un->un_f_mmc_cap == TRUE) { 21923 21924 /* 21925 * MMC Devices do not support changing the 21926 * logical block size 21927 * 21928 * Note: EINVAL is being returned instead of ENOTTY to 21929 * maintain consistancy with the original mmc 21930 * driver update. 21931 */ 21932 err = EINVAL; 21933 } else { 21934 mutex_enter(SD_MUTEX(un)); 21935 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21936 (un->un_ncmds_in_transport > 0)) { 21937 mutex_exit(SD_MUTEX(un)); 21938 err = EINVAL; 21939 } else { 21940 mutex_exit(SD_MUTEX(un)); 21941 err = sr_change_blkmode(dev, cmd, arg, flag); 21942 } 21943 } 21944 break; 21945 21946 case CDROMGBLKMODE: 21947 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21948 if (!ISCD(un)) { 21949 err = ENOTTY; 21950 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21951 (un->un_f_blockcount_is_valid != FALSE)) { 21952 /* 21953 * Drive is an ATAPI drive so return target block 21954 * size for ATAPI drives since we cannot change the 21955 * blocksize on ATAPI drives. Used primarily to detect 21956 * if an ATAPI cdrom is present. 21957 */ 21958 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21959 sizeof (int), flag) != 0) { 21960 err = EFAULT; 21961 } else { 21962 err = 0; 21963 } 21964 21965 } else { 21966 /* 21967 * Drive supports changing block sizes via a Mode 21968 * Select. 21969 */ 21970 err = sr_change_blkmode(dev, cmd, arg, flag); 21971 } 21972 break; 21973 21974 case CDROMGDRVSPEED: 21975 case CDROMSDRVSPEED: 21976 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21977 if (!ISCD(un)) { 21978 err = ENOTTY; 21979 } else if (un->un_f_mmc_cap == TRUE) { 21980 /* 21981 * Note: In the future the driver implementation 21982 * for getting and 21983 * setting cd speed should entail: 21984 * 1) If non-mmc try the Toshiba mode page 21985 * (sr_change_speed) 21986 * 2) If mmc but no support for Real Time Streaming try 21987 * the SET CD SPEED (0xBB) command 21988 * (sr_atapi_change_speed) 21989 * 3) If mmc and support for Real Time Streaming 21990 * try the GET PERFORMANCE and SET STREAMING 21991 * commands (not yet implemented, 4380808) 21992 */ 21993 /* 21994 * As per recent MMC spec, CD-ROM speed is variable 21995 * and changes with LBA. Since there is no such 21996 * things as drive speed now, fail this ioctl. 21997 * 21998 * Note: EINVAL is returned for consistancy of original 21999 * implementation which included support for getting 22000 * the drive speed of mmc devices but not setting 22001 * the drive speed. Thus EINVAL would be returned 22002 * if a set request was made for an mmc device. 22003 * We no longer support get or set speed for 22004 * mmc but need to remain consistent with regard 22005 * to the error code returned. 22006 */ 22007 err = EINVAL; 22008 } else if (un->un_f_cfg_is_atapi == TRUE) { 22009 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22010 } else { 22011 err = sr_change_speed(dev, cmd, arg, flag); 22012 } 22013 break; 22014 22015 case CDROMCDDA: 22016 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22017 if (!ISCD(un)) { 22018 err = ENOTTY; 22019 } else { 22020 err = sr_read_cdda(dev, (void *)arg, flag); 22021 } 22022 break; 22023 22024 case CDROMCDXA: 22025 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22026 if (!ISCD(un)) { 22027 err = ENOTTY; 22028 } else { 22029 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22030 } 22031 break; 22032 22033 case CDROMSUBCODE: 22034 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22035 if (!ISCD(un)) { 22036 err = ENOTTY; 22037 } else { 22038 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22039 } 22040 break; 22041 22042 22043 #ifdef SDDEBUG 22044 /* RESET/ABORTS testing ioctls */ 22045 case DKIOCRESET: { 22046 int reset_level; 22047 22048 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22049 err = EFAULT; 22050 } else { 22051 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22052 "reset_level = 0x%lx\n", reset_level); 22053 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22054 err = 0; 22055 } else { 22056 err = EIO; 22057 } 22058 } 22059 break; 22060 } 22061 22062 case DKIOCABORT: 22063 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22064 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22065 err = 0; 22066 } else { 22067 err = EIO; 22068 } 22069 break; 22070 #endif 22071 22072 #ifdef SD_FAULT_INJECTION 22073 /* SDIOC FaultInjection testing ioctls */ 22074 case SDIOCSTART: 22075 case SDIOCSTOP: 22076 case SDIOCINSERTPKT: 22077 case SDIOCINSERTXB: 22078 case SDIOCINSERTUN: 22079 case SDIOCINSERTARQ: 22080 case SDIOCPUSH: 22081 case SDIOCRETRIEVE: 22082 case SDIOCRUN: 22083 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22084 "SDIOC detected cmd:0x%X:\n", cmd); 22085 /* call error generator */ 22086 sd_faultinjection_ioctl(cmd, arg, un); 22087 err = 0; 22088 break; 22089 22090 #endif /* SD_FAULT_INJECTION */ 22091 22092 case DKIOCFLUSHWRITECACHE: 22093 { 22094 struct dk_callback *dkc = (struct dk_callback *)arg; 22095 22096 mutex_enter(SD_MUTEX(un)); 22097 if (!un->un_f_sync_cache_supported || 22098 !un->un_f_write_cache_enabled) { 22099 err = un->un_f_sync_cache_supported ? 22100 0 : ENOTSUP; 22101 mutex_exit(SD_MUTEX(un)); 22102 if ((flag & FKIOCTL) && dkc != NULL && 22103 dkc->dkc_callback != NULL) { 22104 (*dkc->dkc_callback)(dkc->dkc_cookie, 22105 err); 22106 /* 22107 * Did callback and reported error. 22108 * Since we did a callback, ioctl 22109 * should return 0. 22110 */ 22111 err = 0; 22112 } 22113 break; 22114 } 22115 mutex_exit(SD_MUTEX(un)); 22116 22117 if ((flag & FKIOCTL) && dkc != NULL && 22118 dkc->dkc_callback != NULL) { 22119 /* async SYNC CACHE request */ 22120 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 22121 } else { 22122 /* synchronous SYNC CACHE request */ 22123 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22124 } 22125 } 22126 break; 22127 22128 case DKIOCGETWCE: { 22129 22130 int wce; 22131 22132 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) { 22133 break; 22134 } 22135 22136 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 22137 err = EFAULT; 22138 } 22139 break; 22140 } 22141 22142 case DKIOCSETWCE: { 22143 22144 int wce, sync_supported; 22145 22146 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 22147 err = EFAULT; 22148 break; 22149 } 22150 22151 /* 22152 * Synchronize multiple threads trying to enable 22153 * or disable the cache via the un_f_wcc_cv 22154 * condition variable. 22155 */ 22156 mutex_enter(SD_MUTEX(un)); 22157 22158 /* 22159 * Don't allow the cache to be enabled if the 22160 * config file has it disabled. 22161 */ 22162 if (un->un_f_opt_disable_cache && wce) { 22163 mutex_exit(SD_MUTEX(un)); 22164 err = EINVAL; 22165 break; 22166 } 22167 22168 /* 22169 * Wait for write cache change in progress 22170 * bit to be clear before proceeding. 22171 */ 22172 while (un->un_f_wcc_inprog) 22173 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22174 22175 un->un_f_wcc_inprog = 1; 22176 22177 if (un->un_f_write_cache_enabled && wce == 0) { 22178 /* 22179 * Disable the write cache. Don't clear 22180 * un_f_write_cache_enabled until after 22181 * the mode select and flush are complete. 22182 */ 22183 sync_supported = un->un_f_sync_cache_supported; 22184 22185 /* 22186 * If cache flush is suppressed, we assume that the 22187 * controller firmware will take care of managing the 22188 * write cache for us: no need to explicitly 22189 * disable it. 22190 */ 22191 if (!un->un_f_suppress_cache_flush) { 22192 mutex_exit(SD_MUTEX(un)); 22193 if ((err = sd_cache_control(ssc, 22194 SD_CACHE_NOCHANGE, 22195 SD_CACHE_DISABLE)) == 0 && 22196 sync_supported) { 22197 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 22198 NULL); 22199 } 22200 } else { 22201 mutex_exit(SD_MUTEX(un)); 22202 } 22203 22204 mutex_enter(SD_MUTEX(un)); 22205 if (err == 0) { 22206 un->un_f_write_cache_enabled = 0; 22207 } 22208 22209 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22210 /* 22211 * Set un_f_write_cache_enabled first, so there is 22212 * no window where the cache is enabled, but the 22213 * bit says it isn't. 22214 */ 22215 un->un_f_write_cache_enabled = 1; 22216 22217 /* 22218 * If cache flush is suppressed, we assume that the 22219 * controller firmware will take care of managing the 22220 * write cache for us: no need to explicitly 22221 * enable it. 22222 */ 22223 if (!un->un_f_suppress_cache_flush) { 22224 mutex_exit(SD_MUTEX(un)); 22225 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE, 22226 SD_CACHE_ENABLE); 22227 } else { 22228 mutex_exit(SD_MUTEX(un)); 22229 } 22230 22231 mutex_enter(SD_MUTEX(un)); 22232 22233 if (err) { 22234 un->un_f_write_cache_enabled = 0; 22235 } 22236 } 22237 22238 un->un_f_wcc_inprog = 0; 22239 cv_broadcast(&un->un_wcc_cv); 22240 mutex_exit(SD_MUTEX(un)); 22241 break; 22242 } 22243 22244 default: 22245 err = ENOTTY; 22246 break; 22247 } 22248 mutex_enter(SD_MUTEX(un)); 22249 un->un_ncmds_in_driver--; 22250 ASSERT(un->un_ncmds_in_driver >= 0); 22251 mutex_exit(SD_MUTEX(un)); 22252 22253 22254 done_without_assess: 22255 sd_ssc_fini(ssc); 22256 22257 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22258 return (err); 22259 22260 done_with_assess: 22261 mutex_enter(SD_MUTEX(un)); 22262 un->un_ncmds_in_driver--; 22263 ASSERT(un->un_ncmds_in_driver >= 0); 22264 mutex_exit(SD_MUTEX(un)); 22265 22266 done_quick_assess: 22267 if (err != 0) 22268 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22269 /* Uninitialize sd_ssc_t pointer */ 22270 sd_ssc_fini(ssc); 22271 22272 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22273 return (err); 22274 } 22275 22276 22277 /* 22278 * Function: sd_dkio_ctrl_info 22279 * 22280 * Description: This routine is the driver entry point for handling controller 22281 * information ioctl requests (DKIOCINFO). 22282 * 22283 * Arguments: dev - the device number 22284 * arg - pointer to user provided dk_cinfo structure 22285 * specifying the controller type and attributes. 22286 * flag - this argument is a pass through to ddi_copyxxx() 22287 * directly from the mode argument of ioctl(). 22288 * 22289 * Return Code: 0 22290 * EFAULT 22291 * ENXIO 22292 */ 22293 22294 static int 22295 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22296 { 22297 struct sd_lun *un = NULL; 22298 struct dk_cinfo *info; 22299 dev_info_t *pdip; 22300 int lun, tgt; 22301 22302 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22303 return (ENXIO); 22304 } 22305 22306 info = (struct dk_cinfo *) 22307 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22308 22309 switch (un->un_ctype) { 22310 case CTYPE_CDROM: 22311 info->dki_ctype = DKC_CDROM; 22312 break; 22313 default: 22314 info->dki_ctype = DKC_SCSI_CCS; 22315 break; 22316 } 22317 pdip = ddi_get_parent(SD_DEVINFO(un)); 22318 info->dki_cnum = ddi_get_instance(pdip); 22319 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22320 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22321 } else { 22322 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22323 DK_DEVLEN - 1); 22324 } 22325 22326 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22327 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22328 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22329 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22330 22331 /* Unit Information */ 22332 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22333 info->dki_slave = ((tgt << 3) | lun); 22334 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22335 DK_DEVLEN - 1); 22336 info->dki_flags = DKI_FMTVOL; 22337 info->dki_partition = SDPART(dev); 22338 22339 /* Max Transfer size of this device in blocks */ 22340 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22341 info->dki_addr = 0; 22342 info->dki_space = 0; 22343 info->dki_prio = 0; 22344 info->dki_vec = 0; 22345 22346 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22347 kmem_free(info, sizeof (struct dk_cinfo)); 22348 return (EFAULT); 22349 } else { 22350 kmem_free(info, sizeof (struct dk_cinfo)); 22351 return (0); 22352 } 22353 } 22354 22355 22356 /* 22357 * Function: sd_get_media_info 22358 * 22359 * Description: This routine is the driver entry point for handling ioctl 22360 * requests for the media type or command set profile used by the 22361 * drive to operate on the media (DKIOCGMEDIAINFO). 22362 * 22363 * Arguments: dev - the device number 22364 * arg - pointer to user provided dk_minfo structure 22365 * specifying the media type, logical block size and 22366 * drive capacity. 22367 * flag - this argument is a pass through to ddi_copyxxx() 22368 * directly from the mode argument of ioctl(). 22369 * 22370 * Return Code: 0 22371 * EACCESS 22372 * EFAULT 22373 * ENXIO 22374 * EIO 22375 */ 22376 22377 static int 22378 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22379 { 22380 struct sd_lun *un = NULL; 22381 struct uscsi_cmd com; 22382 struct scsi_inquiry *sinq; 22383 struct dk_minfo media_info; 22384 u_longlong_t media_capacity; 22385 uint64_t capacity; 22386 uint_t lbasize; 22387 uchar_t *out_data; 22388 uchar_t *rqbuf; 22389 int rval = 0; 22390 int rtn; 22391 sd_ssc_t *ssc; 22392 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22393 (un->un_state == SD_STATE_OFFLINE)) { 22394 return (ENXIO); 22395 } 22396 22397 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22398 22399 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22400 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22401 22402 /* Issue a TUR to determine if the drive is ready with media present */ 22403 ssc = sd_ssc_init(un); 22404 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA); 22405 if (rval == ENXIO) { 22406 goto done; 22407 } else if (rval != 0) { 22408 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22409 } 22410 22411 /* Now get configuration data */ 22412 if (ISCD(un)) { 22413 media_info.dki_media_type = DK_CDROM; 22414 22415 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22416 if (un->un_f_mmc_cap == TRUE) { 22417 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, 22418 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 22419 SD_PATH_STANDARD); 22420 22421 if (rtn) { 22422 /* 22423 * We ignore all failures for CD and need to 22424 * put the assessment before processing code 22425 * to avoid missing assessment for FMA. 22426 */ 22427 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22428 /* 22429 * Failed for other than an illegal request 22430 * or command not supported 22431 */ 22432 if ((com.uscsi_status == STATUS_CHECK) && 22433 (com.uscsi_rqstatus == STATUS_GOOD)) { 22434 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22435 (rqbuf[12] != 0x20)) { 22436 rval = EIO; 22437 goto no_assessment; 22438 } 22439 } 22440 } else { 22441 /* 22442 * The GET CONFIGURATION command succeeded 22443 * so set the media type according to the 22444 * returned data 22445 */ 22446 media_info.dki_media_type = out_data[6]; 22447 media_info.dki_media_type <<= 8; 22448 media_info.dki_media_type |= out_data[7]; 22449 } 22450 } 22451 } else { 22452 /* 22453 * The profile list is not available, so we attempt to identify 22454 * the media type based on the inquiry data 22455 */ 22456 sinq = un->un_sd->sd_inq; 22457 if ((sinq->inq_dtype == DTYPE_DIRECT) || 22458 (sinq->inq_dtype == DTYPE_OPTICAL)) { 22459 /* This is a direct access device or optical disk */ 22460 media_info.dki_media_type = DK_FIXED_DISK; 22461 22462 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22463 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22464 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22465 media_info.dki_media_type = DK_ZIP; 22466 } else if ( 22467 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22468 media_info.dki_media_type = DK_JAZ; 22469 } 22470 } 22471 } else { 22472 /* 22473 * Not a CD, direct access or optical disk so return 22474 * unknown media 22475 */ 22476 media_info.dki_media_type = DK_UNKNOWN; 22477 } 22478 } 22479 22480 /* Now read the capacity so we can provide the lbasize and capacity */ 22481 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize, 22482 SD_PATH_DIRECT); 22483 switch (rval) { 22484 case 0: 22485 break; 22486 case EACCES: 22487 rval = EACCES; 22488 goto done; 22489 default: 22490 rval = EIO; 22491 goto done; 22492 } 22493 22494 /* 22495 * If lun is expanded dynamically, update the un structure. 22496 */ 22497 mutex_enter(SD_MUTEX(un)); 22498 if ((un->un_f_blockcount_is_valid == TRUE) && 22499 (un->un_f_tgt_blocksize_is_valid == TRUE) && 22500 (capacity > un->un_blockcount)) { 22501 sd_update_block_info(un, lbasize, capacity); 22502 } 22503 mutex_exit(SD_MUTEX(un)); 22504 22505 media_info.dki_lbsize = lbasize; 22506 media_capacity = capacity; 22507 22508 /* 22509 * sd_send_scsi_READ_CAPACITY() reports capacity in 22510 * un->un_sys_blocksize chunks. So we need to convert it into 22511 * cap.lbasize chunks. 22512 */ 22513 media_capacity *= un->un_sys_blocksize; 22514 media_capacity /= lbasize; 22515 media_info.dki_capacity = media_capacity; 22516 22517 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22518 rval = EFAULT; 22519 /* Put goto. Anybody might add some code below in future */ 22520 goto no_assessment; 22521 } 22522 done: 22523 if (rval != 0) { 22524 if (rval == EIO) 22525 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 22526 else 22527 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22528 } 22529 no_assessment: 22530 sd_ssc_fini(ssc); 22531 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22532 kmem_free(rqbuf, SENSE_LENGTH); 22533 return (rval); 22534 } 22535 22536 22537 /* 22538 * Function: sd_check_media 22539 * 22540 * Description: This utility routine implements the functionality for the 22541 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 22542 * driver state changes from that specified by the user 22543 * (inserted or ejected). For example, if the user specifies 22544 * DKIO_EJECTED and the current media state is inserted this 22545 * routine will immediately return DKIO_INSERTED. However, if the 22546 * current media state is not inserted the user thread will be 22547 * blocked until the drive state changes. If DKIO_NONE is specified 22548 * the user thread will block until a drive state change occurs. 22549 * 22550 * Arguments: dev - the device number 22551 * state - user pointer to a dkio_state, updated with the current 22552 * drive state at return. 22553 * 22554 * Return Code: ENXIO 22555 * EIO 22556 * EAGAIN 22557 * EINTR 22558 */ 22559 22560 static int 22561 sd_check_media(dev_t dev, enum dkio_state state) 22562 { 22563 struct sd_lun *un = NULL; 22564 enum dkio_state prev_state; 22565 opaque_t token = NULL; 22566 int rval = 0; 22567 sd_ssc_t *ssc; 22568 dev_t sub_dev; 22569 22570 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22571 return (ENXIO); 22572 } 22573 22574 /* 22575 * sub_dev is used when submitting request to scsi watch. 22576 * All submissions are unified to use same device number. 22577 */ 22578 sub_dev = sd_make_device(SD_DEVINFO(un)); 22579 22580 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 22581 22582 ssc = sd_ssc_init(un); 22583 22584 mutex_enter(SD_MUTEX(un)); 22585 22586 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 22587 "state=%x, mediastate=%x\n", state, un->un_mediastate); 22588 22589 prev_state = un->un_mediastate; 22590 22591 /* is there anything to do? */ 22592 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 22593 /* 22594 * submit the request to the scsi_watch service; 22595 * scsi_media_watch_cb() does the real work 22596 */ 22597 mutex_exit(SD_MUTEX(un)); 22598 22599 /* 22600 * This change handles the case where a scsi watch request is 22601 * added to a device that is powered down. To accomplish this 22602 * we power up the device before adding the scsi watch request, 22603 * since the scsi watch sends a TUR directly to the device 22604 * which the device cannot handle if it is powered down. 22605 */ 22606 if (sd_pm_entry(un) != DDI_SUCCESS) { 22607 mutex_enter(SD_MUTEX(un)); 22608 goto done; 22609 } 22610 22611 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 22612 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 22613 (caddr_t)sub_dev); 22614 22615 sd_pm_exit(un); 22616 22617 mutex_enter(SD_MUTEX(un)); 22618 if (token == NULL) { 22619 rval = EAGAIN; 22620 goto done; 22621 } 22622 22623 /* 22624 * This is a special case IOCTL that doesn't return 22625 * until the media state changes. Routine sdpower 22626 * knows about and handles this so don't count it 22627 * as an active cmd in the driver, which would 22628 * keep the device busy to the pm framework. 22629 * If the count isn't decremented the device can't 22630 * be powered down. 22631 */ 22632 un->un_ncmds_in_driver--; 22633 ASSERT(un->un_ncmds_in_driver >= 0); 22634 22635 /* 22636 * if a prior request had been made, this will be the same 22637 * token, as scsi_watch was designed that way. 22638 */ 22639 un->un_swr_token = token; 22640 un->un_specified_mediastate = state; 22641 22642 /* 22643 * now wait for media change 22644 * we will not be signalled unless mediastate == state but it is 22645 * still better to test for this condition, since there is a 22646 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 22647 */ 22648 SD_TRACE(SD_LOG_COMMON, un, 22649 "sd_check_media: waiting for media state change\n"); 22650 while (un->un_mediastate == state) { 22651 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 22652 SD_TRACE(SD_LOG_COMMON, un, 22653 "sd_check_media: waiting for media state " 22654 "was interrupted\n"); 22655 un->un_ncmds_in_driver++; 22656 rval = EINTR; 22657 goto done; 22658 } 22659 SD_TRACE(SD_LOG_COMMON, un, 22660 "sd_check_media: received signal, state=%x\n", 22661 un->un_mediastate); 22662 } 22663 /* 22664 * Inc the counter to indicate the device once again 22665 * has an active outstanding cmd. 22666 */ 22667 un->un_ncmds_in_driver++; 22668 } 22669 22670 /* invalidate geometry */ 22671 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 22672 sr_ejected(un); 22673 } 22674 22675 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 22676 uint64_t capacity; 22677 uint_t lbasize; 22678 22679 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 22680 mutex_exit(SD_MUTEX(un)); 22681 /* 22682 * Since the following routines use SD_PATH_DIRECT, we must 22683 * call PM directly before the upcoming disk accesses. This 22684 * may cause the disk to be power/spin up. 22685 */ 22686 22687 if (sd_pm_entry(un) == DDI_SUCCESS) { 22688 rval = sd_send_scsi_READ_CAPACITY(ssc, 22689 &capacity, &lbasize, SD_PATH_DIRECT); 22690 if (rval != 0) { 22691 sd_pm_exit(un); 22692 if (rval == EIO) 22693 sd_ssc_assessment(ssc, 22694 SD_FMT_STATUS_CHECK); 22695 else 22696 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22697 mutex_enter(SD_MUTEX(un)); 22698 goto done; 22699 } 22700 } else { 22701 rval = EIO; 22702 mutex_enter(SD_MUTEX(un)); 22703 goto done; 22704 } 22705 mutex_enter(SD_MUTEX(un)); 22706 22707 sd_update_block_info(un, lbasize, capacity); 22708 22709 /* 22710 * Check if the media in the device is writable or not 22711 */ 22712 if (ISCD(un)) { 22713 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT); 22714 } 22715 22716 mutex_exit(SD_MUTEX(un)); 22717 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 22718 if ((cmlb_validate(un->un_cmlbhandle, 0, 22719 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 22720 sd_set_pstats(un); 22721 SD_TRACE(SD_LOG_IO_PARTITION, un, 22722 "sd_check_media: un:0x%p pstats created and " 22723 "set\n", un); 22724 } 22725 22726 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT, 22727 SD_PATH_DIRECT); 22728 22729 sd_pm_exit(un); 22730 22731 if (rval != 0) { 22732 if (rval == EIO) 22733 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 22734 else 22735 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 22736 } 22737 22738 mutex_enter(SD_MUTEX(un)); 22739 } 22740 done: 22741 sd_ssc_fini(ssc); 22742 un->un_f_watcht_stopped = FALSE; 22743 if (token != NULL && un->un_swr_token != NULL) { 22744 /* 22745 * Use of this local token and the mutex ensures that we avoid 22746 * some race conditions associated with terminating the 22747 * scsi watch. 22748 */ 22749 token = un->un_swr_token; 22750 mutex_exit(SD_MUTEX(un)); 22751 (void) scsi_watch_request_terminate(token, 22752 SCSI_WATCH_TERMINATE_WAIT); 22753 if (scsi_watch_get_ref_count(token) == 0) { 22754 mutex_enter(SD_MUTEX(un)); 22755 un->un_swr_token = (opaque_t)NULL; 22756 } else { 22757 mutex_enter(SD_MUTEX(un)); 22758 } 22759 } 22760 22761 /* 22762 * Update the capacity kstat value, if no media previously 22763 * (capacity kstat is 0) and a media has been inserted 22764 * (un_f_blockcount_is_valid == TRUE) 22765 */ 22766 if (un->un_errstats) { 22767 struct sd_errstats *stp = NULL; 22768 22769 stp = (struct sd_errstats *)un->un_errstats->ks_data; 22770 if ((stp->sd_capacity.value.ui64 == 0) && 22771 (un->un_f_blockcount_is_valid == TRUE)) { 22772 stp->sd_capacity.value.ui64 = 22773 (uint64_t)((uint64_t)un->un_blockcount * 22774 un->un_sys_blocksize); 22775 } 22776 } 22777 mutex_exit(SD_MUTEX(un)); 22778 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 22779 return (rval); 22780 } 22781 22782 22783 /* 22784 * Function: sd_delayed_cv_broadcast 22785 * 22786 * Description: Delayed cv_broadcast to allow for target to recover from media 22787 * insertion. 22788 * 22789 * Arguments: arg - driver soft state (unit) structure 22790 */ 22791 22792 static void 22793 sd_delayed_cv_broadcast(void *arg) 22794 { 22795 struct sd_lun *un = arg; 22796 22797 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 22798 22799 mutex_enter(SD_MUTEX(un)); 22800 un->un_dcvb_timeid = NULL; 22801 cv_broadcast(&un->un_state_cv); 22802 mutex_exit(SD_MUTEX(un)); 22803 } 22804 22805 22806 /* 22807 * Function: sd_media_watch_cb 22808 * 22809 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 22810 * routine processes the TUR sense data and updates the driver 22811 * state if a transition has occurred. The user thread 22812 * (sd_check_media) is then signalled. 22813 * 22814 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22815 * among multiple watches that share this callback function 22816 * resultp - scsi watch facility result packet containing scsi 22817 * packet, status byte and sense data 22818 * 22819 * Return Code: 0 for success, -1 for failure 22820 */ 22821 22822 static int 22823 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 22824 { 22825 struct sd_lun *un; 22826 struct scsi_status *statusp = resultp->statusp; 22827 uint8_t *sensep = (uint8_t *)resultp->sensep; 22828 enum dkio_state state = DKIO_NONE; 22829 dev_t dev = (dev_t)arg; 22830 uchar_t actual_sense_length; 22831 uint8_t skey, asc, ascq; 22832 22833 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22834 return (-1); 22835 } 22836 actual_sense_length = resultp->actual_sense_length; 22837 22838 mutex_enter(SD_MUTEX(un)); 22839 SD_TRACE(SD_LOG_COMMON, un, 22840 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 22841 *((char *)statusp), (void *)sensep, actual_sense_length); 22842 22843 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 22844 un->un_mediastate = DKIO_DEV_GONE; 22845 cv_broadcast(&un->un_state_cv); 22846 mutex_exit(SD_MUTEX(un)); 22847 22848 return (0); 22849 } 22850 22851 /* 22852 * If there was a check condition then sensep points to valid sense data 22853 * If status was not a check condition but a reservation or busy status 22854 * then the new state is DKIO_NONE 22855 */ 22856 if (sensep != NULL) { 22857 skey = scsi_sense_key(sensep); 22858 asc = scsi_sense_asc(sensep); 22859 ascq = scsi_sense_ascq(sensep); 22860 22861 SD_INFO(SD_LOG_COMMON, un, 22862 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 22863 skey, asc, ascq); 22864 /* This routine only uses up to 13 bytes of sense data. */ 22865 if (actual_sense_length >= 13) { 22866 if (skey == KEY_UNIT_ATTENTION) { 22867 if (asc == 0x28) { 22868 state = DKIO_INSERTED; 22869 } 22870 } else if (skey == KEY_NOT_READY) { 22871 /* 22872 * Sense data of 02/06/00 means that the 22873 * drive could not read the media (No 22874 * reference position found). In this case 22875 * to prevent a hang on the DKIOCSTATE IOCTL 22876 * we set the media state to DKIO_INSERTED. 22877 */ 22878 if (asc == 0x06 && ascq == 0x00) 22879 state = DKIO_INSERTED; 22880 22881 /* 22882 * if 02/04/02 means that the host 22883 * should send start command. Explicitly 22884 * leave the media state as is 22885 * (inserted) as the media is inserted 22886 * and host has stopped device for PM 22887 * reasons. Upon next true read/write 22888 * to this media will bring the 22889 * device to the right state good for 22890 * media access. 22891 */ 22892 if (asc == 0x3a) { 22893 state = DKIO_EJECTED; 22894 } else { 22895 /* 22896 * If the drive is busy with an 22897 * operation or long write, keep the 22898 * media in an inserted state. 22899 */ 22900 22901 if ((asc == 0x04) && 22902 ((ascq == 0x02) || 22903 (ascq == 0x07) || 22904 (ascq == 0x08))) { 22905 state = DKIO_INSERTED; 22906 } 22907 } 22908 } else if (skey == KEY_NO_SENSE) { 22909 if ((asc == 0x00) && (ascq == 0x00)) { 22910 /* 22911 * Sense Data 00/00/00 does not provide 22912 * any information about the state of 22913 * the media. Ignore it. 22914 */ 22915 mutex_exit(SD_MUTEX(un)); 22916 return (0); 22917 } 22918 } 22919 } 22920 } else if ((*((char *)statusp) == STATUS_GOOD) && 22921 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 22922 state = DKIO_INSERTED; 22923 } 22924 22925 SD_TRACE(SD_LOG_COMMON, un, 22926 "sd_media_watch_cb: state=%x, specified=%x\n", 22927 state, un->un_specified_mediastate); 22928 22929 /* 22930 * now signal the waiting thread if this is *not* the specified state; 22931 * delay the signal if the state is DKIO_INSERTED to allow the target 22932 * to recover 22933 */ 22934 if (state != un->un_specified_mediastate) { 22935 un->un_mediastate = state; 22936 if (state == DKIO_INSERTED) { 22937 /* 22938 * delay the signal to give the drive a chance 22939 * to do what it apparently needs to do 22940 */ 22941 SD_TRACE(SD_LOG_COMMON, un, 22942 "sd_media_watch_cb: delayed cv_broadcast\n"); 22943 if (un->un_dcvb_timeid == NULL) { 22944 un->un_dcvb_timeid = 22945 timeout(sd_delayed_cv_broadcast, un, 22946 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 22947 } 22948 } else { 22949 SD_TRACE(SD_LOG_COMMON, un, 22950 "sd_media_watch_cb: immediate cv_broadcast\n"); 22951 cv_broadcast(&un->un_state_cv); 22952 } 22953 } 22954 mutex_exit(SD_MUTEX(un)); 22955 return (0); 22956 } 22957 22958 22959 /* 22960 * Function: sd_dkio_get_temp 22961 * 22962 * Description: This routine is the driver entry point for handling ioctl 22963 * requests to get the disk temperature. 22964 * 22965 * Arguments: dev - the device number 22966 * arg - pointer to user provided dk_temperature structure. 22967 * flag - this argument is a pass through to ddi_copyxxx() 22968 * directly from the mode argument of ioctl(). 22969 * 22970 * Return Code: 0 22971 * EFAULT 22972 * ENXIO 22973 * EAGAIN 22974 */ 22975 22976 static int 22977 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 22978 { 22979 struct sd_lun *un = NULL; 22980 struct dk_temperature *dktemp = NULL; 22981 uchar_t *temperature_page; 22982 int rval = 0; 22983 int path_flag = SD_PATH_STANDARD; 22984 sd_ssc_t *ssc; 22985 22986 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22987 return (ENXIO); 22988 } 22989 22990 ssc = sd_ssc_init(un); 22991 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 22992 22993 /* copyin the disk temp argument to get the user flags */ 22994 if (ddi_copyin((void *)arg, dktemp, 22995 sizeof (struct dk_temperature), flag) != 0) { 22996 rval = EFAULT; 22997 goto done; 22998 } 22999 23000 /* Initialize the temperature to invalid. */ 23001 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 23002 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 23003 23004 /* 23005 * Note: Investigate removing the "bypass pm" semantic. 23006 * Can we just bypass PM always? 23007 */ 23008 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 23009 path_flag = SD_PATH_DIRECT; 23010 ASSERT(!mutex_owned(&un->un_pm_mutex)); 23011 mutex_enter(&un->un_pm_mutex); 23012 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 23013 /* 23014 * If DKT_BYPASS_PM is set, and the drive happens to be 23015 * in low power mode, we can not wake it up, Need to 23016 * return EAGAIN. 23017 */ 23018 mutex_exit(&un->un_pm_mutex); 23019 rval = EAGAIN; 23020 goto done; 23021 } else { 23022 /* 23023 * Indicate to PM the device is busy. This is required 23024 * to avoid a race - i.e. the ioctl is issuing a 23025 * command and the pm framework brings down the device 23026 * to low power mode (possible power cut-off on some 23027 * platforms). 23028 */ 23029 mutex_exit(&un->un_pm_mutex); 23030 if (sd_pm_entry(un) != DDI_SUCCESS) { 23031 rval = EAGAIN; 23032 goto done; 23033 } 23034 } 23035 } 23036 23037 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 23038 23039 rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page, 23040 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag); 23041 if (rval != 0) 23042 goto done2; 23043 23044 /* 23045 * For the current temperature verify that the parameter length is 0x02 23046 * and the parameter code is 0x00 23047 */ 23048 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 23049 (temperature_page[5] == 0x00)) { 23050 if (temperature_page[9] == 0xFF) { 23051 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 23052 } else { 23053 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 23054 } 23055 } 23056 23057 /* 23058 * For the reference temperature verify that the parameter 23059 * length is 0x02 and the parameter code is 0x01 23060 */ 23061 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 23062 (temperature_page[11] == 0x01)) { 23063 if (temperature_page[15] == 0xFF) { 23064 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 23065 } else { 23066 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 23067 } 23068 } 23069 23070 /* Do the copyout regardless of the temperature commands status. */ 23071 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 23072 flag) != 0) { 23073 rval = EFAULT; 23074 goto done1; 23075 } 23076 23077 done2: 23078 if (rval != 0) { 23079 if (rval == EIO) 23080 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23081 else 23082 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23083 } 23084 done1: 23085 if (path_flag == SD_PATH_DIRECT) { 23086 sd_pm_exit(un); 23087 } 23088 23089 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 23090 done: 23091 sd_ssc_fini(ssc); 23092 if (dktemp != NULL) { 23093 kmem_free(dktemp, sizeof (struct dk_temperature)); 23094 } 23095 23096 return (rval); 23097 } 23098 23099 23100 /* 23101 * Function: sd_log_page_supported 23102 * 23103 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 23104 * supported log pages. 23105 * 23106 * Arguments: ssc - ssc contains pointer to driver soft state (unit) 23107 * structure for this target. 23108 * log_page - 23109 * 23110 * Return Code: -1 - on error (log sense is optional and may not be supported). 23111 * 0 - log page not found. 23112 * 1 - log page found. 23113 */ 23114 23115 static int 23116 sd_log_page_supported(sd_ssc_t *ssc, int log_page) 23117 { 23118 uchar_t *log_page_data; 23119 int i; 23120 int match = 0; 23121 int log_size; 23122 int status = 0; 23123 struct sd_lun *un; 23124 23125 ASSERT(ssc != NULL); 23126 un = ssc->ssc_un; 23127 ASSERT(un != NULL); 23128 23129 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 23130 23131 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0, 23132 SD_PATH_DIRECT); 23133 23134 if (status != 0) { 23135 if (status == EIO) { 23136 /* 23137 * Some disks do not support log sense, we 23138 * should ignore this kind of error(sense key is 23139 * 0x5 - illegal request). 23140 */ 23141 uint8_t *sensep; 23142 int senlen; 23143 23144 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 23145 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 23146 ssc->ssc_uscsi_cmd->uscsi_rqresid); 23147 23148 if (senlen > 0 && 23149 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) { 23150 sd_ssc_assessment(ssc, 23151 SD_FMT_IGNORE_COMPROMISE); 23152 } else { 23153 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23154 } 23155 } else { 23156 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23157 } 23158 23159 SD_ERROR(SD_LOG_COMMON, un, 23160 "sd_log_page_supported: failed log page retrieval\n"); 23161 kmem_free(log_page_data, 0xFF); 23162 return (-1); 23163 } 23164 23165 log_size = log_page_data[3]; 23166 23167 /* 23168 * The list of supported log pages start from the fourth byte. Check 23169 * until we run out of log pages or a match is found. 23170 */ 23171 for (i = 4; (i < (log_size + 4)) && !match; i++) { 23172 if (log_page_data[i] == log_page) { 23173 match++; 23174 } 23175 } 23176 kmem_free(log_page_data, 0xFF); 23177 return (match); 23178 } 23179 23180 23181 /* 23182 * Function: sd_mhdioc_failfast 23183 * 23184 * Description: This routine is the driver entry point for handling ioctl 23185 * requests to enable/disable the multihost failfast option. 23186 * (MHIOCENFAILFAST) 23187 * 23188 * Arguments: dev - the device number 23189 * arg - user specified probing interval. 23190 * flag - this argument is a pass through to ddi_copyxxx() 23191 * directly from the mode argument of ioctl(). 23192 * 23193 * Return Code: 0 23194 * EFAULT 23195 * ENXIO 23196 */ 23197 23198 static int 23199 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 23200 { 23201 struct sd_lun *un = NULL; 23202 int mh_time; 23203 int rval = 0; 23204 23205 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23206 return (ENXIO); 23207 } 23208 23209 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 23210 return (EFAULT); 23211 23212 if (mh_time) { 23213 mutex_enter(SD_MUTEX(un)); 23214 un->un_resvd_status |= SD_FAILFAST; 23215 mutex_exit(SD_MUTEX(un)); 23216 /* 23217 * If mh_time is INT_MAX, then this ioctl is being used for 23218 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 23219 */ 23220 if (mh_time != INT_MAX) { 23221 rval = sd_check_mhd(dev, mh_time); 23222 } 23223 } else { 23224 (void) sd_check_mhd(dev, 0); 23225 mutex_enter(SD_MUTEX(un)); 23226 un->un_resvd_status &= ~SD_FAILFAST; 23227 mutex_exit(SD_MUTEX(un)); 23228 } 23229 return (rval); 23230 } 23231 23232 23233 /* 23234 * Function: sd_mhdioc_takeown 23235 * 23236 * Description: This routine is the driver entry point for handling ioctl 23237 * requests to forcefully acquire exclusive access rights to the 23238 * multihost disk (MHIOCTKOWN). 23239 * 23240 * Arguments: dev - the device number 23241 * arg - user provided structure specifying the delay 23242 * parameters in milliseconds 23243 * flag - this argument is a pass through to ddi_copyxxx() 23244 * directly from the mode argument of ioctl(). 23245 * 23246 * Return Code: 0 23247 * EFAULT 23248 * ENXIO 23249 */ 23250 23251 static int 23252 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 23253 { 23254 struct sd_lun *un = NULL; 23255 struct mhioctkown *tkown = NULL; 23256 int rval = 0; 23257 23258 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23259 return (ENXIO); 23260 } 23261 23262 if (arg != NULL) { 23263 tkown = (struct mhioctkown *) 23264 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 23265 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 23266 if (rval != 0) { 23267 rval = EFAULT; 23268 goto error; 23269 } 23270 } 23271 23272 rval = sd_take_ownership(dev, tkown); 23273 mutex_enter(SD_MUTEX(un)); 23274 if (rval == 0) { 23275 un->un_resvd_status |= SD_RESERVE; 23276 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 23277 sd_reinstate_resv_delay = 23278 tkown->reinstate_resv_delay * 1000; 23279 } else { 23280 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 23281 } 23282 /* 23283 * Give the scsi_watch routine interval set by 23284 * the MHIOCENFAILFAST ioctl precedence here. 23285 */ 23286 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 23287 mutex_exit(SD_MUTEX(un)); 23288 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 23289 SD_TRACE(SD_LOG_IOCTL_MHD, un, 23290 "sd_mhdioc_takeown : %d\n", 23291 sd_reinstate_resv_delay); 23292 } else { 23293 mutex_exit(SD_MUTEX(un)); 23294 } 23295 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 23296 sd_mhd_reset_notify_cb, (caddr_t)un); 23297 } else { 23298 un->un_resvd_status &= ~SD_RESERVE; 23299 mutex_exit(SD_MUTEX(un)); 23300 } 23301 23302 error: 23303 if (tkown != NULL) { 23304 kmem_free(tkown, sizeof (struct mhioctkown)); 23305 } 23306 return (rval); 23307 } 23308 23309 23310 /* 23311 * Function: sd_mhdioc_release 23312 * 23313 * Description: This routine is the driver entry point for handling ioctl 23314 * requests to release exclusive access rights to the multihost 23315 * disk (MHIOCRELEASE). 23316 * 23317 * Arguments: dev - the device number 23318 * 23319 * Return Code: 0 23320 * ENXIO 23321 */ 23322 23323 static int 23324 sd_mhdioc_release(dev_t dev) 23325 { 23326 struct sd_lun *un = NULL; 23327 timeout_id_t resvd_timeid_save; 23328 int resvd_status_save; 23329 int rval = 0; 23330 23331 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23332 return (ENXIO); 23333 } 23334 23335 mutex_enter(SD_MUTEX(un)); 23336 resvd_status_save = un->un_resvd_status; 23337 un->un_resvd_status &= 23338 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 23339 if (un->un_resvd_timeid) { 23340 resvd_timeid_save = un->un_resvd_timeid; 23341 un->un_resvd_timeid = NULL; 23342 mutex_exit(SD_MUTEX(un)); 23343 (void) untimeout(resvd_timeid_save); 23344 } else { 23345 mutex_exit(SD_MUTEX(un)); 23346 } 23347 23348 /* 23349 * destroy any pending timeout thread that may be attempting to 23350 * reinstate reservation on this device. 23351 */ 23352 sd_rmv_resv_reclaim_req(dev); 23353 23354 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 23355 mutex_enter(SD_MUTEX(un)); 23356 if ((un->un_mhd_token) && 23357 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 23358 mutex_exit(SD_MUTEX(un)); 23359 (void) sd_check_mhd(dev, 0); 23360 } else { 23361 mutex_exit(SD_MUTEX(un)); 23362 } 23363 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 23364 sd_mhd_reset_notify_cb, (caddr_t)un); 23365 } else { 23366 /* 23367 * sd_mhd_watch_cb will restart the resvd recover timeout thread 23368 */ 23369 mutex_enter(SD_MUTEX(un)); 23370 un->un_resvd_status = resvd_status_save; 23371 mutex_exit(SD_MUTEX(un)); 23372 } 23373 return (rval); 23374 } 23375 23376 23377 /* 23378 * Function: sd_mhdioc_register_devid 23379 * 23380 * Description: This routine is the driver entry point for handling ioctl 23381 * requests to register the device id (MHIOCREREGISTERDEVID). 23382 * 23383 * Note: The implementation for this ioctl has been updated to 23384 * be consistent with the original PSARC case (1999/357) 23385 * (4375899, 4241671, 4220005) 23386 * 23387 * Arguments: dev - the device number 23388 * 23389 * Return Code: 0 23390 * ENXIO 23391 */ 23392 23393 static int 23394 sd_mhdioc_register_devid(dev_t dev) 23395 { 23396 struct sd_lun *un = NULL; 23397 int rval = 0; 23398 sd_ssc_t *ssc; 23399 23400 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23401 return (ENXIO); 23402 } 23403 23404 ASSERT(!mutex_owned(SD_MUTEX(un))); 23405 23406 mutex_enter(SD_MUTEX(un)); 23407 23408 /* If a devid already exists, de-register it */ 23409 if (un->un_devid != NULL) { 23410 ddi_devid_unregister(SD_DEVINFO(un)); 23411 /* 23412 * After unregister devid, needs to free devid memory 23413 */ 23414 ddi_devid_free(un->un_devid); 23415 un->un_devid = NULL; 23416 } 23417 23418 /* Check for reservation conflict */ 23419 mutex_exit(SD_MUTEX(un)); 23420 ssc = sd_ssc_init(un); 23421 rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0); 23422 mutex_enter(SD_MUTEX(un)); 23423 23424 switch (rval) { 23425 case 0: 23426 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 23427 break; 23428 case EACCES: 23429 break; 23430 default: 23431 rval = EIO; 23432 } 23433 23434 mutex_exit(SD_MUTEX(un)); 23435 if (rval != 0) { 23436 if (rval == EIO) 23437 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 23438 else 23439 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 23440 } 23441 sd_ssc_fini(ssc); 23442 return (rval); 23443 } 23444 23445 23446 /* 23447 * Function: sd_mhdioc_inkeys 23448 * 23449 * Description: This routine is the driver entry point for handling ioctl 23450 * requests to issue the SCSI-3 Persistent In Read Keys command 23451 * to the device (MHIOCGRP_INKEYS). 23452 * 23453 * Arguments: dev - the device number 23454 * arg - user provided in_keys structure 23455 * flag - this argument is a pass through to ddi_copyxxx() 23456 * directly from the mode argument of ioctl(). 23457 * 23458 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 23459 * ENXIO 23460 * EFAULT 23461 */ 23462 23463 static int 23464 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 23465 { 23466 struct sd_lun *un; 23467 mhioc_inkeys_t inkeys; 23468 int rval = 0; 23469 23470 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23471 return (ENXIO); 23472 } 23473 23474 #ifdef _MULTI_DATAMODEL 23475 switch (ddi_model_convert_from(flag & FMODELS)) { 23476 case DDI_MODEL_ILP32: { 23477 struct mhioc_inkeys32 inkeys32; 23478 23479 if (ddi_copyin(arg, &inkeys32, 23480 sizeof (struct mhioc_inkeys32), flag) != 0) { 23481 return (EFAULT); 23482 } 23483 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 23484 if ((rval = sd_persistent_reservation_in_read_keys(un, 23485 &inkeys, flag)) != 0) { 23486 return (rval); 23487 } 23488 inkeys32.generation = inkeys.generation; 23489 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 23490 flag) != 0) { 23491 return (EFAULT); 23492 } 23493 break; 23494 } 23495 case DDI_MODEL_NONE: 23496 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 23497 flag) != 0) { 23498 return (EFAULT); 23499 } 23500 if ((rval = sd_persistent_reservation_in_read_keys(un, 23501 &inkeys, flag)) != 0) { 23502 return (rval); 23503 } 23504 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 23505 flag) != 0) { 23506 return (EFAULT); 23507 } 23508 break; 23509 } 23510 23511 #else /* ! _MULTI_DATAMODEL */ 23512 23513 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 23514 return (EFAULT); 23515 } 23516 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 23517 if (rval != 0) { 23518 return (rval); 23519 } 23520 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 23521 return (EFAULT); 23522 } 23523 23524 #endif /* _MULTI_DATAMODEL */ 23525 23526 return (rval); 23527 } 23528 23529 23530 /* 23531 * Function: sd_mhdioc_inresv 23532 * 23533 * Description: This routine is the driver entry point for handling ioctl 23534 * requests to issue the SCSI-3 Persistent In Read Reservations 23535 * command to the device (MHIOCGRP_INKEYS). 23536 * 23537 * Arguments: dev - the device number 23538 * arg - user provided in_resv structure 23539 * flag - this argument is a pass through to ddi_copyxxx() 23540 * directly from the mode argument of ioctl(). 23541 * 23542 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 23543 * ENXIO 23544 * EFAULT 23545 */ 23546 23547 static int 23548 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 23549 { 23550 struct sd_lun *un; 23551 mhioc_inresvs_t inresvs; 23552 int rval = 0; 23553 23554 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23555 return (ENXIO); 23556 } 23557 23558 #ifdef _MULTI_DATAMODEL 23559 23560 switch (ddi_model_convert_from(flag & FMODELS)) { 23561 case DDI_MODEL_ILP32: { 23562 struct mhioc_inresvs32 inresvs32; 23563 23564 if (ddi_copyin(arg, &inresvs32, 23565 sizeof (struct mhioc_inresvs32), flag) != 0) { 23566 return (EFAULT); 23567 } 23568 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 23569 if ((rval = sd_persistent_reservation_in_read_resv(un, 23570 &inresvs, flag)) != 0) { 23571 return (rval); 23572 } 23573 inresvs32.generation = inresvs.generation; 23574 if (ddi_copyout(&inresvs32, arg, 23575 sizeof (struct mhioc_inresvs32), flag) != 0) { 23576 return (EFAULT); 23577 } 23578 break; 23579 } 23580 case DDI_MODEL_NONE: 23581 if (ddi_copyin(arg, &inresvs, 23582 sizeof (mhioc_inresvs_t), flag) != 0) { 23583 return (EFAULT); 23584 } 23585 if ((rval = sd_persistent_reservation_in_read_resv(un, 23586 &inresvs, flag)) != 0) { 23587 return (rval); 23588 } 23589 if (ddi_copyout(&inresvs, arg, 23590 sizeof (mhioc_inresvs_t), flag) != 0) { 23591 return (EFAULT); 23592 } 23593 break; 23594 } 23595 23596 #else /* ! _MULTI_DATAMODEL */ 23597 23598 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 23599 return (EFAULT); 23600 } 23601 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 23602 if (rval != 0) { 23603 return (rval); 23604 } 23605 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 23606 return (EFAULT); 23607 } 23608 23609 #endif /* ! _MULTI_DATAMODEL */ 23610 23611 return (rval); 23612 } 23613 23614 23615 /* 23616 * The following routines support the clustering functionality described below 23617 * and implement lost reservation reclaim functionality. 23618 * 23619 * Clustering 23620 * ---------- 23621 * The clustering code uses two different, independent forms of SCSI 23622 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 23623 * Persistent Group Reservations. For any particular disk, it will use either 23624 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 23625 * 23626 * SCSI-2 23627 * The cluster software takes ownership of a multi-hosted disk by issuing the 23628 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 23629 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 23630 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 23631 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 23632 * driver. The meaning of failfast is that if the driver (on this host) ever 23633 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 23634 * it should immediately panic the host. The motivation for this ioctl is that 23635 * if this host does encounter reservation conflict, the underlying cause is 23636 * that some other host of the cluster has decided that this host is no longer 23637 * in the cluster and has seized control of the disks for itself. Since this 23638 * host is no longer in the cluster, it ought to panic itself. The 23639 * MHIOCENFAILFAST ioctl does two things: 23640 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 23641 * error to panic the host 23642 * (b) it sets up a periodic timer to test whether this host still has 23643 * "access" (in that no other host has reserved the device): if the 23644 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 23645 * purpose of that periodic timer is to handle scenarios where the host is 23646 * otherwise temporarily quiescent, temporarily doing no real i/o. 23647 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 23648 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 23649 * the device itself. 23650 * 23651 * SCSI-3 PGR 23652 * A direct semantic implementation of the SCSI-3 Persistent Reservation 23653 * facility is supported through the shared multihost disk ioctls 23654 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 23655 * MHIOCGRP_PREEMPTANDABORT) 23656 * 23657 * Reservation Reclaim: 23658 * -------------------- 23659 * To support the lost reservation reclaim operations this driver creates a 23660 * single thread to handle reinstating reservations on all devices that have 23661 * lost reservations sd_resv_reclaim_requests are logged for all devices that 23662 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 23663 * and the reservation reclaim thread loops through the requests to regain the 23664 * lost reservations. 23665 */ 23666 23667 /* 23668 * Function: sd_check_mhd() 23669 * 23670 * Description: This function sets up and submits a scsi watch request or 23671 * terminates an existing watch request. This routine is used in 23672 * support of reservation reclaim. 23673 * 23674 * Arguments: dev - the device 'dev_t' is used for context to discriminate 23675 * among multiple watches that share the callback function 23676 * interval - the number of microseconds specifying the watch 23677 * interval for issuing TEST UNIT READY commands. If 23678 * set to 0 the watch should be terminated. If the 23679 * interval is set to 0 and if the device is required 23680 * to hold reservation while disabling failfast, the 23681 * watch is restarted with an interval of 23682 * reinstate_resv_delay. 23683 * 23684 * Return Code: 0 - Successful submit/terminate of scsi watch request 23685 * ENXIO - Indicates an invalid device was specified 23686 * EAGAIN - Unable to submit the scsi watch request 23687 */ 23688 23689 static int 23690 sd_check_mhd(dev_t dev, int interval) 23691 { 23692 struct sd_lun *un; 23693 opaque_t token; 23694 23695 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23696 return (ENXIO); 23697 } 23698 23699 /* is this a watch termination request? */ 23700 if (interval == 0) { 23701 mutex_enter(SD_MUTEX(un)); 23702 /* if there is an existing watch task then terminate it */ 23703 if (un->un_mhd_token) { 23704 token = un->un_mhd_token; 23705 un->un_mhd_token = NULL; 23706 mutex_exit(SD_MUTEX(un)); 23707 (void) scsi_watch_request_terminate(token, 23708 SCSI_WATCH_TERMINATE_ALL_WAIT); 23709 mutex_enter(SD_MUTEX(un)); 23710 } else { 23711 mutex_exit(SD_MUTEX(un)); 23712 /* 23713 * Note: If we return here we don't check for the 23714 * failfast case. This is the original legacy 23715 * implementation but perhaps we should be checking 23716 * the failfast case. 23717 */ 23718 return (0); 23719 } 23720 /* 23721 * If the device is required to hold reservation while 23722 * disabling failfast, we need to restart the scsi_watch 23723 * routine with an interval of reinstate_resv_delay. 23724 */ 23725 if (un->un_resvd_status & SD_RESERVE) { 23726 interval = sd_reinstate_resv_delay/1000; 23727 } else { 23728 /* no failfast so bail */ 23729 mutex_exit(SD_MUTEX(un)); 23730 return (0); 23731 } 23732 mutex_exit(SD_MUTEX(un)); 23733 } 23734 23735 /* 23736 * adjust minimum time interval to 1 second, 23737 * and convert from msecs to usecs 23738 */ 23739 if (interval > 0 && interval < 1000) { 23740 interval = 1000; 23741 } 23742 interval *= 1000; 23743 23744 /* 23745 * submit the request to the scsi_watch service 23746 */ 23747 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 23748 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 23749 if (token == NULL) { 23750 return (EAGAIN); 23751 } 23752 23753 /* 23754 * save token for termination later on 23755 */ 23756 mutex_enter(SD_MUTEX(un)); 23757 un->un_mhd_token = token; 23758 mutex_exit(SD_MUTEX(un)); 23759 return (0); 23760 } 23761 23762 23763 /* 23764 * Function: sd_mhd_watch_cb() 23765 * 23766 * Description: This function is the call back function used by the scsi watch 23767 * facility. The scsi watch facility sends the "Test Unit Ready" 23768 * and processes the status. If applicable (i.e. a "Unit Attention" 23769 * status and automatic "Request Sense" not used) the scsi watch 23770 * facility will send a "Request Sense" and retrieve the sense data 23771 * to be passed to this callback function. In either case the 23772 * automatic "Request Sense" or the facility submitting one, this 23773 * callback is passed the status and sense data. 23774 * 23775 * Arguments: arg - the device 'dev_t' is used for context to discriminate 23776 * among multiple watches that share this callback function 23777 * resultp - scsi watch facility result packet containing scsi 23778 * packet, status byte and sense data 23779 * 23780 * Return Code: 0 - continue the watch task 23781 * non-zero - terminate the watch task 23782 */ 23783 23784 static int 23785 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 23786 { 23787 struct sd_lun *un; 23788 struct scsi_status *statusp; 23789 uint8_t *sensep; 23790 struct scsi_pkt *pkt; 23791 uchar_t actual_sense_length; 23792 dev_t dev = (dev_t)arg; 23793 23794 ASSERT(resultp != NULL); 23795 statusp = resultp->statusp; 23796 sensep = (uint8_t *)resultp->sensep; 23797 pkt = resultp->pkt; 23798 actual_sense_length = resultp->actual_sense_length; 23799 23800 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23801 return (ENXIO); 23802 } 23803 23804 SD_TRACE(SD_LOG_IOCTL_MHD, un, 23805 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 23806 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 23807 23808 /* Begin processing of the status and/or sense data */ 23809 if (pkt->pkt_reason != CMD_CMPLT) { 23810 /* Handle the incomplete packet */ 23811 sd_mhd_watch_incomplete(un, pkt); 23812 return (0); 23813 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 23814 if (*((unsigned char *)statusp) 23815 == STATUS_RESERVATION_CONFLICT) { 23816 /* 23817 * Handle a reservation conflict by panicking if 23818 * configured for failfast or by logging the conflict 23819 * and updating the reservation status 23820 */ 23821 mutex_enter(SD_MUTEX(un)); 23822 if ((un->un_resvd_status & SD_FAILFAST) && 23823 (sd_failfast_enable)) { 23824 sd_panic_for_res_conflict(un); 23825 /*NOTREACHED*/ 23826 } 23827 SD_INFO(SD_LOG_IOCTL_MHD, un, 23828 "sd_mhd_watch_cb: Reservation Conflict\n"); 23829 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 23830 mutex_exit(SD_MUTEX(un)); 23831 } 23832 } 23833 23834 if (sensep != NULL) { 23835 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 23836 mutex_enter(SD_MUTEX(un)); 23837 if ((scsi_sense_asc(sensep) == 23838 SD_SCSI_RESET_SENSE_CODE) && 23839 (un->un_resvd_status & SD_RESERVE)) { 23840 /* 23841 * The additional sense code indicates a power 23842 * on or bus device reset has occurred; update 23843 * the reservation status. 23844 */ 23845 un->un_resvd_status |= 23846 (SD_LOST_RESERVE | SD_WANT_RESERVE); 23847 SD_INFO(SD_LOG_IOCTL_MHD, un, 23848 "sd_mhd_watch_cb: Lost Reservation\n"); 23849 } 23850 } else { 23851 return (0); 23852 } 23853 } else { 23854 mutex_enter(SD_MUTEX(un)); 23855 } 23856 23857 if ((un->un_resvd_status & SD_RESERVE) && 23858 (un->un_resvd_status & SD_LOST_RESERVE)) { 23859 if (un->un_resvd_status & SD_WANT_RESERVE) { 23860 /* 23861 * A reset occurred in between the last probe and this 23862 * one so if a timeout is pending cancel it. 23863 */ 23864 if (un->un_resvd_timeid) { 23865 timeout_id_t temp_id = un->un_resvd_timeid; 23866 un->un_resvd_timeid = NULL; 23867 mutex_exit(SD_MUTEX(un)); 23868 (void) untimeout(temp_id); 23869 mutex_enter(SD_MUTEX(un)); 23870 } 23871 un->un_resvd_status &= ~SD_WANT_RESERVE; 23872 } 23873 if (un->un_resvd_timeid == 0) { 23874 /* Schedule a timeout to handle the lost reservation */ 23875 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 23876 (void *)dev, 23877 drv_usectohz(sd_reinstate_resv_delay)); 23878 } 23879 } 23880 mutex_exit(SD_MUTEX(un)); 23881 return (0); 23882 } 23883 23884 23885 /* 23886 * Function: sd_mhd_watch_incomplete() 23887 * 23888 * Description: This function is used to find out why a scsi pkt sent by the 23889 * scsi watch facility was not completed. Under some scenarios this 23890 * routine will return. Otherwise it will send a bus reset to see 23891 * if the drive is still online. 23892 * 23893 * Arguments: un - driver soft state (unit) structure 23894 * pkt - incomplete scsi pkt 23895 */ 23896 23897 static void 23898 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 23899 { 23900 int be_chatty; 23901 int perr; 23902 23903 ASSERT(pkt != NULL); 23904 ASSERT(un != NULL); 23905 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 23906 perr = (pkt->pkt_statistics & STAT_PERR); 23907 23908 mutex_enter(SD_MUTEX(un)); 23909 if (un->un_state == SD_STATE_DUMPING) { 23910 mutex_exit(SD_MUTEX(un)); 23911 return; 23912 } 23913 23914 switch (pkt->pkt_reason) { 23915 case CMD_UNX_BUS_FREE: 23916 /* 23917 * If we had a parity error that caused the target to drop BSY*, 23918 * don't be chatty about it. 23919 */ 23920 if (perr && be_chatty) { 23921 be_chatty = 0; 23922 } 23923 break; 23924 case CMD_TAG_REJECT: 23925 /* 23926 * The SCSI-2 spec states that a tag reject will be sent by the 23927 * target if tagged queuing is not supported. A tag reject may 23928 * also be sent during certain initialization periods or to 23929 * control internal resources. For the latter case the target 23930 * may also return Queue Full. 23931 * 23932 * If this driver receives a tag reject from a target that is 23933 * going through an init period or controlling internal 23934 * resources tagged queuing will be disabled. This is a less 23935 * than optimal behavior but the driver is unable to determine 23936 * the target state and assumes tagged queueing is not supported 23937 */ 23938 pkt->pkt_flags = 0; 23939 un->un_tagflags = 0; 23940 23941 if (un->un_f_opt_queueing == TRUE) { 23942 un->un_throttle = min(un->un_throttle, 3); 23943 } else { 23944 un->un_throttle = 1; 23945 } 23946 mutex_exit(SD_MUTEX(un)); 23947 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 23948 mutex_enter(SD_MUTEX(un)); 23949 break; 23950 case CMD_INCOMPLETE: 23951 /* 23952 * The transport stopped with an abnormal state, fallthrough and 23953 * reset the target and/or bus unless selection did not complete 23954 * (indicated by STATE_GOT_BUS) in which case we don't want to 23955 * go through a target/bus reset 23956 */ 23957 if (pkt->pkt_state == STATE_GOT_BUS) { 23958 break; 23959 } 23960 /*FALLTHROUGH*/ 23961 23962 case CMD_TIMEOUT: 23963 default: 23964 /* 23965 * The lun may still be running the command, so a lun reset 23966 * should be attempted. If the lun reset fails or cannot be 23967 * issued, than try a target reset. Lastly try a bus reset. 23968 */ 23969 if ((pkt->pkt_statistics & 23970 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 23971 int reset_retval = 0; 23972 mutex_exit(SD_MUTEX(un)); 23973 if (un->un_f_allow_bus_device_reset == TRUE) { 23974 if (un->un_f_lun_reset_enabled == TRUE) { 23975 reset_retval = 23976 scsi_reset(SD_ADDRESS(un), 23977 RESET_LUN); 23978 } 23979 if (reset_retval == 0) { 23980 reset_retval = 23981 scsi_reset(SD_ADDRESS(un), 23982 RESET_TARGET); 23983 } 23984 } 23985 if (reset_retval == 0) { 23986 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 23987 } 23988 mutex_enter(SD_MUTEX(un)); 23989 } 23990 break; 23991 } 23992 23993 /* A device/bus reset has occurred; update the reservation status. */ 23994 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 23995 (STAT_BUS_RESET | STAT_DEV_RESET))) { 23996 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 23997 un->un_resvd_status |= 23998 (SD_LOST_RESERVE | SD_WANT_RESERVE); 23999 SD_INFO(SD_LOG_IOCTL_MHD, un, 24000 "sd_mhd_watch_incomplete: Lost Reservation\n"); 24001 } 24002 } 24003 24004 /* 24005 * The disk has been turned off; Update the device state. 24006 * 24007 * Note: Should we be offlining the disk here? 24008 */ 24009 if (pkt->pkt_state == STATE_GOT_BUS) { 24010 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 24011 "Disk not responding to selection\n"); 24012 if (un->un_state != SD_STATE_OFFLINE) { 24013 New_state(un, SD_STATE_OFFLINE); 24014 } 24015 } else if (be_chatty) { 24016 /* 24017 * suppress messages if they are all the same pkt reason; 24018 * with TQ, many (up to 256) are returned with the same 24019 * pkt_reason 24020 */ 24021 if (pkt->pkt_reason != un->un_last_pkt_reason) { 24022 SD_ERROR(SD_LOG_IOCTL_MHD, un, 24023 "sd_mhd_watch_incomplete: " 24024 "SCSI transport failed: reason '%s'\n", 24025 scsi_rname(pkt->pkt_reason)); 24026 } 24027 } 24028 un->un_last_pkt_reason = pkt->pkt_reason; 24029 mutex_exit(SD_MUTEX(un)); 24030 } 24031 24032 24033 /* 24034 * Function: sd_sname() 24035 * 24036 * Description: This is a simple little routine to return a string containing 24037 * a printable description of command status byte for use in 24038 * logging. 24039 * 24040 * Arguments: status - pointer to a status byte 24041 * 24042 * Return Code: char * - string containing status description. 24043 */ 24044 24045 static char * 24046 sd_sname(uchar_t status) 24047 { 24048 switch (status & STATUS_MASK) { 24049 case STATUS_GOOD: 24050 return ("good status"); 24051 case STATUS_CHECK: 24052 return ("check condition"); 24053 case STATUS_MET: 24054 return ("condition met"); 24055 case STATUS_BUSY: 24056 return ("busy"); 24057 case STATUS_INTERMEDIATE: 24058 return ("intermediate"); 24059 case STATUS_INTERMEDIATE_MET: 24060 return ("intermediate - condition met"); 24061 case STATUS_RESERVATION_CONFLICT: 24062 return ("reservation_conflict"); 24063 case STATUS_TERMINATED: 24064 return ("command terminated"); 24065 case STATUS_QFULL: 24066 return ("queue full"); 24067 default: 24068 return ("<unknown status>"); 24069 } 24070 } 24071 24072 24073 /* 24074 * Function: sd_mhd_resvd_recover() 24075 * 24076 * Description: This function adds a reservation entry to the 24077 * sd_resv_reclaim_request list and signals the reservation 24078 * reclaim thread that there is work pending. If the reservation 24079 * reclaim thread has not been previously created this function 24080 * will kick it off. 24081 * 24082 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24083 * among multiple watches that share this callback function 24084 * 24085 * Context: This routine is called by timeout() and is run in interrupt 24086 * context. It must not sleep or call other functions which may 24087 * sleep. 24088 */ 24089 24090 static void 24091 sd_mhd_resvd_recover(void *arg) 24092 { 24093 dev_t dev = (dev_t)arg; 24094 struct sd_lun *un; 24095 struct sd_thr_request *sd_treq = NULL; 24096 struct sd_thr_request *sd_cur = NULL; 24097 struct sd_thr_request *sd_prev = NULL; 24098 int already_there = 0; 24099 24100 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24101 return; 24102 } 24103 24104 mutex_enter(SD_MUTEX(un)); 24105 un->un_resvd_timeid = NULL; 24106 if (un->un_resvd_status & SD_WANT_RESERVE) { 24107 /* 24108 * There was a reset so don't issue the reserve, allow the 24109 * sd_mhd_watch_cb callback function to notice this and 24110 * reschedule the timeout for reservation. 24111 */ 24112 mutex_exit(SD_MUTEX(un)); 24113 return; 24114 } 24115 mutex_exit(SD_MUTEX(un)); 24116 24117 /* 24118 * Add this device to the sd_resv_reclaim_request list and the 24119 * sd_resv_reclaim_thread should take care of the rest. 24120 * 24121 * Note: We can't sleep in this context so if the memory allocation 24122 * fails allow the sd_mhd_watch_cb callback function to notice this and 24123 * reschedule the timeout for reservation. (4378460) 24124 */ 24125 sd_treq = (struct sd_thr_request *) 24126 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 24127 if (sd_treq == NULL) { 24128 return; 24129 } 24130 24131 sd_treq->sd_thr_req_next = NULL; 24132 sd_treq->dev = dev; 24133 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 24134 if (sd_tr.srq_thr_req_head == NULL) { 24135 sd_tr.srq_thr_req_head = sd_treq; 24136 } else { 24137 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 24138 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 24139 if (sd_cur->dev == dev) { 24140 /* 24141 * already in Queue so don't log 24142 * another request for the device 24143 */ 24144 already_there = 1; 24145 break; 24146 } 24147 sd_prev = sd_cur; 24148 } 24149 if (!already_there) { 24150 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 24151 "logging request for %lx\n", dev); 24152 sd_prev->sd_thr_req_next = sd_treq; 24153 } else { 24154 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 24155 } 24156 } 24157 24158 /* 24159 * Create a kernel thread to do the reservation reclaim and free up this 24160 * thread. We cannot block this thread while we go away to do the 24161 * reservation reclaim 24162 */ 24163 if (sd_tr.srq_resv_reclaim_thread == NULL) 24164 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 24165 sd_resv_reclaim_thread, NULL, 24166 0, &p0, TS_RUN, v.v_maxsyspri - 2); 24167 24168 /* Tell the reservation reclaim thread that it has work to do */ 24169 cv_signal(&sd_tr.srq_resv_reclaim_cv); 24170 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24171 } 24172 24173 /* 24174 * Function: sd_resv_reclaim_thread() 24175 * 24176 * Description: This function implements the reservation reclaim operations 24177 * 24178 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24179 * among multiple watches that share this callback function 24180 */ 24181 24182 static void 24183 sd_resv_reclaim_thread() 24184 { 24185 struct sd_lun *un; 24186 struct sd_thr_request *sd_mhreq; 24187 24188 /* Wait for work */ 24189 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 24190 if (sd_tr.srq_thr_req_head == NULL) { 24191 cv_wait(&sd_tr.srq_resv_reclaim_cv, 24192 &sd_tr.srq_resv_reclaim_mutex); 24193 } 24194 24195 /* Loop while we have work */ 24196 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 24197 un = ddi_get_soft_state(sd_state, 24198 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 24199 if (un == NULL) { 24200 /* 24201 * softstate structure is NULL so just 24202 * dequeue the request and continue 24203 */ 24204 sd_tr.srq_thr_req_head = 24205 sd_tr.srq_thr_cur_req->sd_thr_req_next; 24206 kmem_free(sd_tr.srq_thr_cur_req, 24207 sizeof (struct sd_thr_request)); 24208 continue; 24209 } 24210 24211 /* dequeue the request */ 24212 sd_mhreq = sd_tr.srq_thr_cur_req; 24213 sd_tr.srq_thr_req_head = 24214 sd_tr.srq_thr_cur_req->sd_thr_req_next; 24215 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24216 24217 /* 24218 * Reclaim reservation only if SD_RESERVE is still set. There 24219 * may have been a call to MHIOCRELEASE before we got here. 24220 */ 24221 mutex_enter(SD_MUTEX(un)); 24222 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 24223 /* 24224 * Note: The SD_LOST_RESERVE flag is cleared before 24225 * reclaiming the reservation. If this is done after the 24226 * call to sd_reserve_release a reservation loss in the 24227 * window between pkt completion of reserve cmd and 24228 * mutex_enter below may not be recognized 24229 */ 24230 un->un_resvd_status &= ~SD_LOST_RESERVE; 24231 mutex_exit(SD_MUTEX(un)); 24232 24233 if (sd_reserve_release(sd_mhreq->dev, 24234 SD_RESERVE) == 0) { 24235 mutex_enter(SD_MUTEX(un)); 24236 un->un_resvd_status |= SD_RESERVE; 24237 mutex_exit(SD_MUTEX(un)); 24238 SD_INFO(SD_LOG_IOCTL_MHD, un, 24239 "sd_resv_reclaim_thread: " 24240 "Reservation Recovered\n"); 24241 } else { 24242 mutex_enter(SD_MUTEX(un)); 24243 un->un_resvd_status |= SD_LOST_RESERVE; 24244 mutex_exit(SD_MUTEX(un)); 24245 SD_INFO(SD_LOG_IOCTL_MHD, un, 24246 "sd_resv_reclaim_thread: Failed " 24247 "Reservation Recovery\n"); 24248 } 24249 } else { 24250 mutex_exit(SD_MUTEX(un)); 24251 } 24252 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 24253 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 24254 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 24255 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 24256 /* 24257 * wakeup the destroy thread if anyone is waiting on 24258 * us to complete. 24259 */ 24260 cv_signal(&sd_tr.srq_inprocess_cv); 24261 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24262 "sd_resv_reclaim_thread: cv_signalling current request \n"); 24263 } 24264 24265 /* 24266 * cleanup the sd_tr structure now that this thread will not exist 24267 */ 24268 ASSERT(sd_tr.srq_thr_req_head == NULL); 24269 ASSERT(sd_tr.srq_thr_cur_req == NULL); 24270 sd_tr.srq_resv_reclaim_thread = NULL; 24271 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24272 thread_exit(); 24273 } 24274 24275 24276 /* 24277 * Function: sd_rmv_resv_reclaim_req() 24278 * 24279 * Description: This function removes any pending reservation reclaim requests 24280 * for the specified device. 24281 * 24282 * Arguments: dev - the device 'dev_t' 24283 */ 24284 24285 static void 24286 sd_rmv_resv_reclaim_req(dev_t dev) 24287 { 24288 struct sd_thr_request *sd_mhreq; 24289 struct sd_thr_request *sd_prev; 24290 24291 /* Remove a reservation reclaim request from the list */ 24292 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 24293 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 24294 /* 24295 * We are attempting to reinstate reservation for 24296 * this device. We wait for sd_reserve_release() 24297 * to return before we return. 24298 */ 24299 cv_wait(&sd_tr.srq_inprocess_cv, 24300 &sd_tr.srq_resv_reclaim_mutex); 24301 } else { 24302 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 24303 if (sd_mhreq && sd_mhreq->dev == dev) { 24304 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 24305 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 24306 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24307 return; 24308 } 24309 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 24310 if (sd_mhreq && sd_mhreq->dev == dev) { 24311 break; 24312 } 24313 sd_prev = sd_mhreq; 24314 } 24315 if (sd_mhreq != NULL) { 24316 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 24317 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 24318 } 24319 } 24320 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 24321 } 24322 24323 24324 /* 24325 * Function: sd_mhd_reset_notify_cb() 24326 * 24327 * Description: This is a call back function for scsi_reset_notify. This 24328 * function updates the softstate reserved status and logs the 24329 * reset. The driver scsi watch facility callback function 24330 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 24331 * will reclaim the reservation. 24332 * 24333 * Arguments: arg - driver soft state (unit) structure 24334 */ 24335 24336 static void 24337 sd_mhd_reset_notify_cb(caddr_t arg) 24338 { 24339 struct sd_lun *un = (struct sd_lun *)arg; 24340 24341 mutex_enter(SD_MUTEX(un)); 24342 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 24343 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 24344 SD_INFO(SD_LOG_IOCTL_MHD, un, 24345 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 24346 } 24347 mutex_exit(SD_MUTEX(un)); 24348 } 24349 24350 24351 /* 24352 * Function: sd_take_ownership() 24353 * 24354 * Description: This routine implements an algorithm to achieve a stable 24355 * reservation on disks which don't implement priority reserve, 24356 * and makes sure that other host lose re-reservation attempts. 24357 * This algorithm contains of a loop that keeps issuing the RESERVE 24358 * for some period of time (min_ownership_delay, default 6 seconds) 24359 * During that loop, it looks to see if there has been a bus device 24360 * reset or bus reset (both of which cause an existing reservation 24361 * to be lost). If the reservation is lost issue RESERVE until a 24362 * period of min_ownership_delay with no resets has gone by, or 24363 * until max_ownership_delay has expired. This loop ensures that 24364 * the host really did manage to reserve the device, in spite of 24365 * resets. The looping for min_ownership_delay (default six 24366 * seconds) is important to early generation clustering products, 24367 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 24368 * MHIOCENFAILFAST periodic timer of two seconds. By having 24369 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 24370 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 24371 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 24372 * have already noticed, via the MHIOCENFAILFAST polling, that it 24373 * no longer "owns" the disk and will have panicked itself. Thus, 24374 * the host issuing the MHIOCTKOWN is assured (with timing 24375 * dependencies) that by the time it actually starts to use the 24376 * disk for real work, the old owner is no longer accessing it. 24377 * 24378 * min_ownership_delay is the minimum amount of time for which the 24379 * disk must be reserved continuously devoid of resets before the 24380 * MHIOCTKOWN ioctl will return success. 24381 * 24382 * max_ownership_delay indicates the amount of time by which the 24383 * take ownership should succeed or timeout with an error. 24384 * 24385 * Arguments: dev - the device 'dev_t' 24386 * *p - struct containing timing info. 24387 * 24388 * Return Code: 0 for success or error code 24389 */ 24390 24391 static int 24392 sd_take_ownership(dev_t dev, struct mhioctkown *p) 24393 { 24394 struct sd_lun *un; 24395 int rval; 24396 int err; 24397 int reservation_count = 0; 24398 int min_ownership_delay = 6000000; /* in usec */ 24399 int max_ownership_delay = 30000000; /* in usec */ 24400 clock_t start_time; /* starting time of this algorithm */ 24401 clock_t end_time; /* time limit for giving up */ 24402 clock_t ownership_time; /* time limit for stable ownership */ 24403 clock_t current_time; 24404 clock_t previous_current_time; 24405 24406 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24407 return (ENXIO); 24408 } 24409 24410 /* 24411 * Attempt a device reservation. A priority reservation is requested. 24412 */ 24413 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 24414 != SD_SUCCESS) { 24415 SD_ERROR(SD_LOG_IOCTL_MHD, un, 24416 "sd_take_ownership: return(1)=%d\n", rval); 24417 return (rval); 24418 } 24419 24420 /* Update the softstate reserved status to indicate the reservation */ 24421 mutex_enter(SD_MUTEX(un)); 24422 un->un_resvd_status |= SD_RESERVE; 24423 un->un_resvd_status &= 24424 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 24425 mutex_exit(SD_MUTEX(un)); 24426 24427 if (p != NULL) { 24428 if (p->min_ownership_delay != 0) { 24429 min_ownership_delay = p->min_ownership_delay * 1000; 24430 } 24431 if (p->max_ownership_delay != 0) { 24432 max_ownership_delay = p->max_ownership_delay * 1000; 24433 } 24434 } 24435 SD_INFO(SD_LOG_IOCTL_MHD, un, 24436 "sd_take_ownership: min, max delays: %d, %d\n", 24437 min_ownership_delay, max_ownership_delay); 24438 24439 start_time = ddi_get_lbolt(); 24440 current_time = start_time; 24441 ownership_time = current_time + drv_usectohz(min_ownership_delay); 24442 end_time = start_time + drv_usectohz(max_ownership_delay); 24443 24444 while (current_time - end_time < 0) { 24445 delay(drv_usectohz(500000)); 24446 24447 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 24448 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 24449 mutex_enter(SD_MUTEX(un)); 24450 rval = (un->un_resvd_status & 24451 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 24452 mutex_exit(SD_MUTEX(un)); 24453 break; 24454 } 24455 } 24456 previous_current_time = current_time; 24457 current_time = ddi_get_lbolt(); 24458 mutex_enter(SD_MUTEX(un)); 24459 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 24460 ownership_time = ddi_get_lbolt() + 24461 drv_usectohz(min_ownership_delay); 24462 reservation_count = 0; 24463 } else { 24464 reservation_count++; 24465 } 24466 un->un_resvd_status |= SD_RESERVE; 24467 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 24468 mutex_exit(SD_MUTEX(un)); 24469 24470 SD_INFO(SD_LOG_IOCTL_MHD, un, 24471 "sd_take_ownership: ticks for loop iteration=%ld, " 24472 "reservation=%s\n", (current_time - previous_current_time), 24473 reservation_count ? "ok" : "reclaimed"); 24474 24475 if (current_time - ownership_time >= 0 && 24476 reservation_count >= 4) { 24477 rval = 0; /* Achieved a stable ownership */ 24478 break; 24479 } 24480 if (current_time - end_time >= 0) { 24481 rval = EACCES; /* No ownership in max possible time */ 24482 break; 24483 } 24484 } 24485 SD_TRACE(SD_LOG_IOCTL_MHD, un, 24486 "sd_take_ownership: return(2)=%d\n", rval); 24487 return (rval); 24488 } 24489 24490 24491 /* 24492 * Function: sd_reserve_release() 24493 * 24494 * Description: This function builds and sends scsi RESERVE, RELEASE, and 24495 * PRIORITY RESERVE commands based on a user specified command type 24496 * 24497 * Arguments: dev - the device 'dev_t' 24498 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 24499 * SD_RESERVE, SD_RELEASE 24500 * 24501 * Return Code: 0 or Error Code 24502 */ 24503 24504 static int 24505 sd_reserve_release(dev_t dev, int cmd) 24506 { 24507 struct uscsi_cmd *com = NULL; 24508 struct sd_lun *un = NULL; 24509 char cdb[CDB_GROUP0]; 24510 int rval; 24511 24512 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 24513 (cmd == SD_PRIORITY_RESERVE)); 24514 24515 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24516 return (ENXIO); 24517 } 24518 24519 /* instantiate and initialize the command and cdb */ 24520 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24521 bzero(cdb, CDB_GROUP0); 24522 com->uscsi_flags = USCSI_SILENT; 24523 com->uscsi_timeout = un->un_reserve_release_time; 24524 com->uscsi_cdblen = CDB_GROUP0; 24525 com->uscsi_cdb = cdb; 24526 if (cmd == SD_RELEASE) { 24527 cdb[0] = SCMD_RELEASE; 24528 } else { 24529 cdb[0] = SCMD_RESERVE; 24530 } 24531 24532 /* Send the command. */ 24533 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24534 SD_PATH_STANDARD); 24535 24536 /* 24537 * "break" a reservation that is held by another host, by issuing a 24538 * reset if priority reserve is desired, and we could not get the 24539 * device. 24540 */ 24541 if ((cmd == SD_PRIORITY_RESERVE) && 24542 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 24543 /* 24544 * First try to reset the LUN. If we cannot, then try a target 24545 * reset, followed by a bus reset if the target reset fails. 24546 */ 24547 int reset_retval = 0; 24548 if (un->un_f_lun_reset_enabled == TRUE) { 24549 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 24550 } 24551 if (reset_retval == 0) { 24552 /* The LUN reset either failed or was not issued */ 24553 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 24554 } 24555 if ((reset_retval == 0) && 24556 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 24557 rval = EIO; 24558 kmem_free(com, sizeof (*com)); 24559 return (rval); 24560 } 24561 24562 bzero(com, sizeof (struct uscsi_cmd)); 24563 com->uscsi_flags = USCSI_SILENT; 24564 com->uscsi_cdb = cdb; 24565 com->uscsi_cdblen = CDB_GROUP0; 24566 com->uscsi_timeout = 5; 24567 24568 /* 24569 * Reissue the last reserve command, this time without request 24570 * sense. Assume that it is just a regular reserve command. 24571 */ 24572 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24573 SD_PATH_STANDARD); 24574 } 24575 24576 /* Return an error if still getting a reservation conflict. */ 24577 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 24578 rval = EACCES; 24579 } 24580 24581 kmem_free(com, sizeof (*com)); 24582 return (rval); 24583 } 24584 24585 24586 #define SD_NDUMP_RETRIES 12 24587 /* 24588 * System Crash Dump routine 24589 */ 24590 24591 static int 24592 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 24593 { 24594 int instance; 24595 int partition; 24596 int i; 24597 int err; 24598 struct sd_lun *un; 24599 struct scsi_pkt *wr_pktp; 24600 struct buf *wr_bp; 24601 struct buf wr_buf; 24602 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 24603 daddr_t tgt_blkno; /* rmw - blkno for target */ 24604 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 24605 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 24606 size_t io_start_offset; 24607 int doing_rmw = FALSE; 24608 int rval; 24609 ssize_t dma_resid; 24610 daddr_t oblkno; 24611 diskaddr_t nblks = 0; 24612 diskaddr_t start_block; 24613 24614 instance = SDUNIT(dev); 24615 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 24616 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 24617 return (ENXIO); 24618 } 24619 24620 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 24621 24622 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 24623 24624 partition = SDPART(dev); 24625 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 24626 24627 /* Validate blocks to dump at against partition size. */ 24628 24629 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 24630 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 24631 24632 if ((blkno + nblk) > nblks) { 24633 SD_TRACE(SD_LOG_DUMP, un, 24634 "sddump: dump range larger than partition: " 24635 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 24636 blkno, nblk, nblks); 24637 return (EINVAL); 24638 } 24639 24640 mutex_enter(&un->un_pm_mutex); 24641 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24642 struct scsi_pkt *start_pktp; 24643 24644 mutex_exit(&un->un_pm_mutex); 24645 24646 /* 24647 * use pm framework to power on HBA 1st 24648 */ 24649 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 24650 24651 /* 24652 * Dump no long uses sdpower to power on a device, it's 24653 * in-line here so it can be done in polled mode. 24654 */ 24655 24656 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 24657 24658 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 24659 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 24660 24661 if (start_pktp == NULL) { 24662 /* We were not given a SCSI packet, fail. */ 24663 return (EIO); 24664 } 24665 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 24666 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 24667 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 24668 start_pktp->pkt_flags = FLAG_NOINTR; 24669 24670 mutex_enter(SD_MUTEX(un)); 24671 SD_FILL_SCSI1_LUN(un, start_pktp); 24672 mutex_exit(SD_MUTEX(un)); 24673 /* 24674 * Scsi_poll returns 0 (success) if the command completes and 24675 * the status block is STATUS_GOOD. 24676 */ 24677 if (sd_scsi_poll(un, start_pktp) != 0) { 24678 scsi_destroy_pkt(start_pktp); 24679 return (EIO); 24680 } 24681 scsi_destroy_pkt(start_pktp); 24682 (void) sd_ddi_pm_resume(un); 24683 } else { 24684 mutex_exit(&un->un_pm_mutex); 24685 } 24686 24687 mutex_enter(SD_MUTEX(un)); 24688 un->un_throttle = 0; 24689 24690 /* 24691 * The first time through, reset the specific target device. 24692 * However, when cpr calls sddump we know that sd is in a 24693 * a good state so no bus reset is required. 24694 * Clear sense data via Request Sense cmd. 24695 * In sddump we don't care about allow_bus_device_reset anymore 24696 */ 24697 24698 if ((un->un_state != SD_STATE_SUSPENDED) && 24699 (un->un_state != SD_STATE_DUMPING)) { 24700 24701 New_state(un, SD_STATE_DUMPING); 24702 24703 if (un->un_f_is_fibre == FALSE) { 24704 mutex_exit(SD_MUTEX(un)); 24705 /* 24706 * Attempt a bus reset for parallel scsi. 24707 * 24708 * Note: A bus reset is required because on some host 24709 * systems (i.e. E420R) a bus device reset is 24710 * insufficient to reset the state of the target. 24711 * 24712 * Note: Don't issue the reset for fibre-channel, 24713 * because this tends to hang the bus (loop) for 24714 * too long while everyone is logging out and in 24715 * and the deadman timer for dumping will fire 24716 * before the dump is complete. 24717 */ 24718 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 24719 mutex_enter(SD_MUTEX(un)); 24720 Restore_state(un); 24721 mutex_exit(SD_MUTEX(un)); 24722 return (EIO); 24723 } 24724 24725 /* Delay to give the device some recovery time. */ 24726 drv_usecwait(10000); 24727 24728 if (sd_send_polled_RQS(un) == SD_FAILURE) { 24729 SD_INFO(SD_LOG_DUMP, un, 24730 "sddump: sd_send_polled_RQS failed\n"); 24731 } 24732 mutex_enter(SD_MUTEX(un)); 24733 } 24734 } 24735 24736 /* 24737 * Convert the partition-relative block number to a 24738 * disk physical block number. 24739 */ 24740 blkno += start_block; 24741 24742 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 24743 24744 24745 /* 24746 * Check if the device has a non-512 block size. 24747 */ 24748 wr_bp = NULL; 24749 if (NOT_DEVBSIZE(un)) { 24750 tgt_byte_offset = blkno * un->un_sys_blocksize; 24751 tgt_byte_count = nblk * un->un_sys_blocksize; 24752 if ((tgt_byte_offset % un->un_tgt_blocksize) || 24753 (tgt_byte_count % un->un_tgt_blocksize)) { 24754 doing_rmw = TRUE; 24755 /* 24756 * Calculate the block number and number of block 24757 * in terms of the media block size. 24758 */ 24759 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 24760 tgt_nblk = 24761 ((tgt_byte_offset + tgt_byte_count + 24762 (un->un_tgt_blocksize - 1)) / 24763 un->un_tgt_blocksize) - tgt_blkno; 24764 24765 /* 24766 * Invoke the routine which is going to do read part 24767 * of read-modify-write. 24768 * Note that this routine returns a pointer to 24769 * a valid bp in wr_bp. 24770 */ 24771 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 24772 &wr_bp); 24773 if (err) { 24774 mutex_exit(SD_MUTEX(un)); 24775 return (err); 24776 } 24777 /* 24778 * Offset is being calculated as - 24779 * (original block # * system block size) - 24780 * (new block # * target block size) 24781 */ 24782 io_start_offset = 24783 ((uint64_t)(blkno * un->un_sys_blocksize)) - 24784 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 24785 24786 ASSERT((io_start_offset >= 0) && 24787 (io_start_offset < un->un_tgt_blocksize)); 24788 /* 24789 * Do the modify portion of read modify write. 24790 */ 24791 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 24792 (size_t)nblk * un->un_sys_blocksize); 24793 } else { 24794 doing_rmw = FALSE; 24795 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 24796 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 24797 } 24798 24799 /* Convert blkno and nblk to target blocks */ 24800 blkno = tgt_blkno; 24801 nblk = tgt_nblk; 24802 } else { 24803 wr_bp = &wr_buf; 24804 bzero(wr_bp, sizeof (struct buf)); 24805 wr_bp->b_flags = B_BUSY; 24806 wr_bp->b_un.b_addr = addr; 24807 wr_bp->b_bcount = nblk << DEV_BSHIFT; 24808 wr_bp->b_resid = 0; 24809 } 24810 24811 mutex_exit(SD_MUTEX(un)); 24812 24813 /* 24814 * Obtain a SCSI packet for the write command. 24815 * It should be safe to call the allocator here without 24816 * worrying about being locked for DVMA mapping because 24817 * the address we're passed is already a DVMA mapping 24818 * 24819 * We are also not going to worry about semaphore ownership 24820 * in the dump buffer. Dumping is single threaded at present. 24821 */ 24822 24823 wr_pktp = NULL; 24824 24825 dma_resid = wr_bp->b_bcount; 24826 oblkno = blkno; 24827 24828 while (dma_resid != 0) { 24829 24830 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 24831 wr_bp->b_flags &= ~B_ERROR; 24832 24833 if (un->un_partial_dma_supported == 1) { 24834 blkno = oblkno + 24835 ((wr_bp->b_bcount - dma_resid) / 24836 un->un_tgt_blocksize); 24837 nblk = dma_resid / un->un_tgt_blocksize; 24838 24839 if (wr_pktp) { 24840 /* 24841 * Partial DMA transfers after initial transfer 24842 */ 24843 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 24844 blkno, nblk); 24845 } else { 24846 /* Initial transfer */ 24847 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 24848 un->un_pkt_flags, NULL_FUNC, NULL, 24849 blkno, nblk); 24850 } 24851 } else { 24852 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 24853 0, NULL_FUNC, NULL, blkno, nblk); 24854 } 24855 24856 if (rval == 0) { 24857 /* We were given a SCSI packet, continue. */ 24858 break; 24859 } 24860 24861 if (i == 0) { 24862 if (wr_bp->b_flags & B_ERROR) { 24863 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24864 "no resources for dumping; " 24865 "error code: 0x%x, retrying", 24866 geterror(wr_bp)); 24867 } else { 24868 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24869 "no resources for dumping; retrying"); 24870 } 24871 } else if (i != (SD_NDUMP_RETRIES - 1)) { 24872 if (wr_bp->b_flags & B_ERROR) { 24873 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 24874 "no resources for dumping; error code: " 24875 "0x%x, retrying\n", geterror(wr_bp)); 24876 } 24877 } else { 24878 if (wr_bp->b_flags & B_ERROR) { 24879 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 24880 "no resources for dumping; " 24881 "error code: 0x%x, retries failed, " 24882 "giving up.\n", geterror(wr_bp)); 24883 } else { 24884 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 24885 "no resources for dumping; " 24886 "retries failed, giving up.\n"); 24887 } 24888 mutex_enter(SD_MUTEX(un)); 24889 Restore_state(un); 24890 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 24891 mutex_exit(SD_MUTEX(un)); 24892 scsi_free_consistent_buf(wr_bp); 24893 } else { 24894 mutex_exit(SD_MUTEX(un)); 24895 } 24896 return (EIO); 24897 } 24898 drv_usecwait(10000); 24899 } 24900 24901 if (un->un_partial_dma_supported == 1) { 24902 /* 24903 * save the resid from PARTIAL_DMA 24904 */ 24905 dma_resid = wr_pktp->pkt_resid; 24906 if (dma_resid != 0) 24907 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 24908 wr_pktp->pkt_resid = 0; 24909 } else { 24910 dma_resid = 0; 24911 } 24912 24913 /* SunBug 1222170 */ 24914 wr_pktp->pkt_flags = FLAG_NOINTR; 24915 24916 err = EIO; 24917 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 24918 24919 /* 24920 * Scsi_poll returns 0 (success) if the command completes and 24921 * the status block is STATUS_GOOD. We should only check 24922 * errors if this condition is not true. Even then we should 24923 * send our own request sense packet only if we have a check 24924 * condition and auto request sense has not been performed by 24925 * the hba. 24926 */ 24927 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 24928 24929 if ((sd_scsi_poll(un, wr_pktp) == 0) && 24930 (wr_pktp->pkt_resid == 0)) { 24931 err = SD_SUCCESS; 24932 break; 24933 } 24934 24935 /* 24936 * Check CMD_DEV_GONE 1st, give up if device is gone. 24937 */ 24938 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 24939 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24940 "Error while dumping state...Device is gone\n"); 24941 break; 24942 } 24943 24944 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 24945 SD_INFO(SD_LOG_DUMP, un, 24946 "sddump: write failed with CHECK, try # %d\n", i); 24947 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 24948 (void) sd_send_polled_RQS(un); 24949 } 24950 24951 continue; 24952 } 24953 24954 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 24955 int reset_retval = 0; 24956 24957 SD_INFO(SD_LOG_DUMP, un, 24958 "sddump: write failed with BUSY, try # %d\n", i); 24959 24960 if (un->un_f_lun_reset_enabled == TRUE) { 24961 reset_retval = scsi_reset(SD_ADDRESS(un), 24962 RESET_LUN); 24963 } 24964 if (reset_retval == 0) { 24965 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 24966 } 24967 (void) sd_send_polled_RQS(un); 24968 24969 } else { 24970 SD_INFO(SD_LOG_DUMP, un, 24971 "sddump: write failed with 0x%x, try # %d\n", 24972 SD_GET_PKT_STATUS(wr_pktp), i); 24973 mutex_enter(SD_MUTEX(un)); 24974 sd_reset_target(un, wr_pktp); 24975 mutex_exit(SD_MUTEX(un)); 24976 } 24977 24978 /* 24979 * If we are not getting anywhere with lun/target resets, 24980 * let's reset the bus. 24981 */ 24982 if (i == SD_NDUMP_RETRIES/2) { 24983 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 24984 (void) sd_send_polled_RQS(un); 24985 } 24986 } 24987 } 24988 24989 scsi_destroy_pkt(wr_pktp); 24990 mutex_enter(SD_MUTEX(un)); 24991 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 24992 mutex_exit(SD_MUTEX(un)); 24993 scsi_free_consistent_buf(wr_bp); 24994 } else { 24995 mutex_exit(SD_MUTEX(un)); 24996 } 24997 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 24998 return (err); 24999 } 25000 25001 /* 25002 * Function: sd_scsi_poll() 25003 * 25004 * Description: This is a wrapper for the scsi_poll call. 25005 * 25006 * Arguments: sd_lun - The unit structure 25007 * scsi_pkt - The scsi packet being sent to the device. 25008 * 25009 * Return Code: 0 - Command completed successfully with good status 25010 * -1 - Command failed. This could indicate a check condition 25011 * or other status value requiring recovery action. 25012 * 25013 * NOTE: This code is only called off sddump(). 25014 */ 25015 25016 static int 25017 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 25018 { 25019 int status; 25020 25021 ASSERT(un != NULL); 25022 ASSERT(!mutex_owned(SD_MUTEX(un))); 25023 ASSERT(pktp != NULL); 25024 25025 status = SD_SUCCESS; 25026 25027 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 25028 pktp->pkt_flags |= un->un_tagflags; 25029 pktp->pkt_flags &= ~FLAG_NODISCON; 25030 } 25031 25032 status = sd_ddi_scsi_poll(pktp); 25033 /* 25034 * Scsi_poll returns 0 (success) if the command completes and the 25035 * status block is STATUS_GOOD. We should only check errors if this 25036 * condition is not true. Even then we should send our own request 25037 * sense packet only if we have a check condition and auto 25038 * request sense has not been performed by the hba. 25039 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 25040 */ 25041 if ((status != SD_SUCCESS) && 25042 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 25043 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 25044 (pktp->pkt_reason != CMD_DEV_GONE)) 25045 (void) sd_send_polled_RQS(un); 25046 25047 return (status); 25048 } 25049 25050 /* 25051 * Function: sd_send_polled_RQS() 25052 * 25053 * Description: This sends the request sense command to a device. 25054 * 25055 * Arguments: sd_lun - The unit structure 25056 * 25057 * Return Code: 0 - Command completed successfully with good status 25058 * -1 - Command failed. 25059 * 25060 */ 25061 25062 static int 25063 sd_send_polled_RQS(struct sd_lun *un) 25064 { 25065 int ret_val; 25066 struct scsi_pkt *rqs_pktp; 25067 struct buf *rqs_bp; 25068 25069 ASSERT(un != NULL); 25070 ASSERT(!mutex_owned(SD_MUTEX(un))); 25071 25072 ret_val = SD_SUCCESS; 25073 25074 rqs_pktp = un->un_rqs_pktp; 25075 rqs_bp = un->un_rqs_bp; 25076 25077 mutex_enter(SD_MUTEX(un)); 25078 25079 if (un->un_sense_isbusy) { 25080 ret_val = SD_FAILURE; 25081 mutex_exit(SD_MUTEX(un)); 25082 return (ret_val); 25083 } 25084 25085 /* 25086 * If the request sense buffer (and packet) is not in use, 25087 * let's set the un_sense_isbusy and send our packet 25088 */ 25089 un->un_sense_isbusy = 1; 25090 rqs_pktp->pkt_resid = 0; 25091 rqs_pktp->pkt_reason = 0; 25092 rqs_pktp->pkt_flags |= FLAG_NOINTR; 25093 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 25094 25095 mutex_exit(SD_MUTEX(un)); 25096 25097 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 25098 " 0x%p\n", rqs_bp->b_un.b_addr); 25099 25100 /* 25101 * Can't send this to sd_scsi_poll, we wrap ourselves around the 25102 * axle - it has a call into us! 25103 */ 25104 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 25105 SD_INFO(SD_LOG_COMMON, un, 25106 "sd_send_polled_RQS: RQS failed\n"); 25107 } 25108 25109 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 25110 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 25111 25112 mutex_enter(SD_MUTEX(un)); 25113 un->un_sense_isbusy = 0; 25114 mutex_exit(SD_MUTEX(un)); 25115 25116 return (ret_val); 25117 } 25118 25119 /* 25120 * Defines needed for localized version of the scsi_poll routine. 25121 */ 25122 #define CSEC 10000 /* usecs */ 25123 #define SEC_TO_CSEC (1000000/CSEC) 25124 25125 /* 25126 * Function: sd_ddi_scsi_poll() 25127 * 25128 * Description: Localized version of the scsi_poll routine. The purpose is to 25129 * send a scsi_pkt to a device as a polled command. This version 25130 * is to ensure more robust handling of transport errors. 25131 * Specifically this routine cures not ready, coming ready 25132 * transition for power up and reset of sonoma's. This can take 25133 * up to 45 seconds for power-on and 20 seconds for reset of a 25134 * sonoma lun. 25135 * 25136 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 25137 * 25138 * Return Code: 0 - Command completed successfully with good status 25139 * -1 - Command failed. 25140 * 25141 * NOTE: This code is almost identical to scsi_poll, however before 6668774 can 25142 * be fixed (removing this code), we need to determine how to handle the 25143 * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump(). 25144 * 25145 * NOTE: This code is only called off sddump(). 25146 */ 25147 static int 25148 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 25149 { 25150 int rval = -1; 25151 int savef; 25152 long savet; 25153 void (*savec)(); 25154 int timeout; 25155 int busy_count; 25156 int poll_delay; 25157 int rc; 25158 uint8_t *sensep; 25159 struct scsi_arq_status *arqstat; 25160 extern int do_polled_io; 25161 25162 ASSERT(pkt->pkt_scbp); 25163 25164 /* 25165 * save old flags.. 25166 */ 25167 savef = pkt->pkt_flags; 25168 savec = pkt->pkt_comp; 25169 savet = pkt->pkt_time; 25170 25171 pkt->pkt_flags |= FLAG_NOINTR; 25172 25173 /* 25174 * XXX there is nothing in the SCSA spec that states that we should not 25175 * do a callback for polled cmds; however, removing this will break sd 25176 * and probably other target drivers 25177 */ 25178 pkt->pkt_comp = NULL; 25179 25180 /* 25181 * we don't like a polled command without timeout. 25182 * 60 seconds seems long enough. 25183 */ 25184 if (pkt->pkt_time == 0) 25185 pkt->pkt_time = SCSI_POLL_TIMEOUT; 25186 25187 /* 25188 * Send polled cmd. 25189 * 25190 * We do some error recovery for various errors. Tran_busy, 25191 * queue full, and non-dispatched commands are retried every 10 msec. 25192 * as they are typically transient failures. Busy status and Not 25193 * Ready are retried every second as this status takes a while to 25194 * change. 25195 */ 25196 timeout = pkt->pkt_time * SEC_TO_CSEC; 25197 25198 for (busy_count = 0; busy_count < timeout; busy_count++) { 25199 /* 25200 * Initialize pkt status variables. 25201 */ 25202 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 25203 25204 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 25205 if (rc != TRAN_BUSY) { 25206 /* Transport failed - give up. */ 25207 break; 25208 } else { 25209 /* Transport busy - try again. */ 25210 poll_delay = 1 * CSEC; /* 10 msec. */ 25211 } 25212 } else { 25213 /* 25214 * Transport accepted - check pkt status. 25215 */ 25216 rc = (*pkt->pkt_scbp) & STATUS_MASK; 25217 if ((pkt->pkt_reason == CMD_CMPLT) && 25218 (rc == STATUS_CHECK) && 25219 (pkt->pkt_state & STATE_ARQ_DONE)) { 25220 arqstat = 25221 (struct scsi_arq_status *)(pkt->pkt_scbp); 25222 sensep = (uint8_t *)&arqstat->sts_sensedata; 25223 } else { 25224 sensep = NULL; 25225 } 25226 25227 if ((pkt->pkt_reason == CMD_CMPLT) && 25228 (rc == STATUS_GOOD)) { 25229 /* No error - we're done */ 25230 rval = 0; 25231 break; 25232 25233 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 25234 /* Lost connection - give up */ 25235 break; 25236 25237 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 25238 (pkt->pkt_state == 0)) { 25239 /* Pkt not dispatched - try again. */ 25240 poll_delay = 1 * CSEC; /* 10 msec. */ 25241 25242 } else if ((pkt->pkt_reason == CMD_CMPLT) && 25243 (rc == STATUS_QFULL)) { 25244 /* Queue full - try again. */ 25245 poll_delay = 1 * CSEC; /* 10 msec. */ 25246 25247 } else if ((pkt->pkt_reason == CMD_CMPLT) && 25248 (rc == STATUS_BUSY)) { 25249 /* Busy - try again. */ 25250 poll_delay = 100 * CSEC; /* 1 sec. */ 25251 busy_count += (SEC_TO_CSEC - 1); 25252 25253 } else if ((sensep != NULL) && 25254 (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) { 25255 /* 25256 * Unit Attention - try again. 25257 * Pretend it took 1 sec. 25258 * NOTE: 'continue' avoids poll_delay 25259 */ 25260 busy_count += (SEC_TO_CSEC - 1); 25261 continue; 25262 25263 } else if ((sensep != NULL) && 25264 (scsi_sense_key(sensep) == KEY_NOT_READY) && 25265 (scsi_sense_asc(sensep) == 0x04) && 25266 (scsi_sense_ascq(sensep) == 0x01)) { 25267 /* 25268 * Not ready -> ready - try again. 25269 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY 25270 * ...same as STATUS_BUSY 25271 */ 25272 poll_delay = 100 * CSEC; /* 1 sec. */ 25273 busy_count += (SEC_TO_CSEC - 1); 25274 25275 } else { 25276 /* BAD status - give up. */ 25277 break; 25278 } 25279 } 25280 25281 if (((curthread->t_flag & T_INTR_THREAD) == 0) && 25282 !do_polled_io) { 25283 delay(drv_usectohz(poll_delay)); 25284 } else { 25285 /* we busy wait during cpr_dump or interrupt threads */ 25286 drv_usecwait(poll_delay); 25287 } 25288 } 25289 25290 pkt->pkt_flags = savef; 25291 pkt->pkt_comp = savec; 25292 pkt->pkt_time = savet; 25293 25294 /* return on error */ 25295 if (rval) 25296 return (rval); 25297 25298 /* 25299 * This is not a performance critical code path. 25300 * 25301 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync() 25302 * issues associated with looking at DMA memory prior to 25303 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return. 25304 */ 25305 scsi_sync_pkt(pkt); 25306 return (0); 25307 } 25308 25309 25310 25311 /* 25312 * Function: sd_persistent_reservation_in_read_keys 25313 * 25314 * Description: This routine is the driver entry point for handling CD-ROM 25315 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 25316 * by sending the SCSI-3 PRIN commands to the device. 25317 * Processes the read keys command response by copying the 25318 * reservation key information into the user provided buffer. 25319 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 25320 * 25321 * Arguments: un - Pointer to soft state struct for the target. 25322 * usrp - user provided pointer to multihost Persistent In Read 25323 * Keys structure (mhioc_inkeys_t) 25324 * flag - this argument is a pass through to ddi_copyxxx() 25325 * directly from the mode argument of ioctl(). 25326 * 25327 * Return Code: 0 - Success 25328 * EACCES 25329 * ENOTSUP 25330 * errno return code from sd_send_scsi_cmd() 25331 * 25332 * Context: Can sleep. Does not return until command is completed. 25333 */ 25334 25335 static int 25336 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 25337 mhioc_inkeys_t *usrp, int flag) 25338 { 25339 #ifdef _MULTI_DATAMODEL 25340 struct mhioc_key_list32 li32; 25341 #endif 25342 sd_prin_readkeys_t *in; 25343 mhioc_inkeys_t *ptr; 25344 mhioc_key_list_t li; 25345 uchar_t *data_bufp; 25346 int data_len; 25347 int rval = 0; 25348 size_t copysz; 25349 sd_ssc_t *ssc; 25350 25351 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 25352 return (EINVAL); 25353 } 25354 bzero(&li, sizeof (mhioc_key_list_t)); 25355 25356 ssc = sd_ssc_init(un); 25357 25358 /* 25359 * Get the listsize from user 25360 */ 25361 #ifdef _MULTI_DATAMODEL 25362 25363 switch (ddi_model_convert_from(flag & FMODELS)) { 25364 case DDI_MODEL_ILP32: 25365 copysz = sizeof (struct mhioc_key_list32); 25366 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 25367 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25368 "sd_persistent_reservation_in_read_keys: " 25369 "failed ddi_copyin: mhioc_key_list32_t\n"); 25370 rval = EFAULT; 25371 goto done; 25372 } 25373 li.listsize = li32.listsize; 25374 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 25375 break; 25376 25377 case DDI_MODEL_NONE: 25378 copysz = sizeof (mhioc_key_list_t); 25379 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 25380 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25381 "sd_persistent_reservation_in_read_keys: " 25382 "failed ddi_copyin: mhioc_key_list_t\n"); 25383 rval = EFAULT; 25384 goto done; 25385 } 25386 break; 25387 } 25388 25389 #else /* ! _MULTI_DATAMODEL */ 25390 copysz = sizeof (mhioc_key_list_t); 25391 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 25392 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25393 "sd_persistent_reservation_in_read_keys: " 25394 "failed ddi_copyin: mhioc_key_list_t\n"); 25395 rval = EFAULT; 25396 goto done; 25397 } 25398 #endif 25399 25400 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 25401 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 25402 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 25403 25404 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 25405 data_len, data_bufp); 25406 if (rval != 0) { 25407 if (rval == EIO) 25408 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 25409 else 25410 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 25411 goto done; 25412 } 25413 in = (sd_prin_readkeys_t *)data_bufp; 25414 ptr->generation = BE_32(in->generation); 25415 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 25416 25417 /* 25418 * Return the min(listsize, listlen) keys 25419 */ 25420 #ifdef _MULTI_DATAMODEL 25421 25422 switch (ddi_model_convert_from(flag & FMODELS)) { 25423 case DDI_MODEL_ILP32: 25424 li32.listlen = li.listlen; 25425 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 25426 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25427 "sd_persistent_reservation_in_read_keys: " 25428 "failed ddi_copyout: mhioc_key_list32_t\n"); 25429 rval = EFAULT; 25430 goto done; 25431 } 25432 break; 25433 25434 case DDI_MODEL_NONE: 25435 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 25436 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25437 "sd_persistent_reservation_in_read_keys: " 25438 "failed ddi_copyout: mhioc_key_list_t\n"); 25439 rval = EFAULT; 25440 goto done; 25441 } 25442 break; 25443 } 25444 25445 #else /* ! _MULTI_DATAMODEL */ 25446 25447 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 25448 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25449 "sd_persistent_reservation_in_read_keys: " 25450 "failed ddi_copyout: mhioc_key_list_t\n"); 25451 rval = EFAULT; 25452 goto done; 25453 } 25454 25455 #endif /* _MULTI_DATAMODEL */ 25456 25457 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 25458 li.listsize * MHIOC_RESV_KEY_SIZE); 25459 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 25460 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25461 "sd_persistent_reservation_in_read_keys: " 25462 "failed ddi_copyout: keylist\n"); 25463 rval = EFAULT; 25464 } 25465 done: 25466 sd_ssc_fini(ssc); 25467 kmem_free(data_bufp, data_len); 25468 return (rval); 25469 } 25470 25471 25472 /* 25473 * Function: sd_persistent_reservation_in_read_resv 25474 * 25475 * Description: This routine is the driver entry point for handling CD-ROM 25476 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 25477 * by sending the SCSI-3 PRIN commands to the device. 25478 * Process the read persistent reservations command response by 25479 * copying the reservation information into the user provided 25480 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 25481 * 25482 * Arguments: un - Pointer to soft state struct for the target. 25483 * usrp - user provided pointer to multihost Persistent In Read 25484 * Keys structure (mhioc_inkeys_t) 25485 * flag - this argument is a pass through to ddi_copyxxx() 25486 * directly from the mode argument of ioctl(). 25487 * 25488 * Return Code: 0 - Success 25489 * EACCES 25490 * ENOTSUP 25491 * errno return code from sd_send_scsi_cmd() 25492 * 25493 * Context: Can sleep. Does not return until command is completed. 25494 */ 25495 25496 static int 25497 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 25498 mhioc_inresvs_t *usrp, int flag) 25499 { 25500 #ifdef _MULTI_DATAMODEL 25501 struct mhioc_resv_desc_list32 resvlist32; 25502 #endif 25503 sd_prin_readresv_t *in; 25504 mhioc_inresvs_t *ptr; 25505 sd_readresv_desc_t *readresv_ptr; 25506 mhioc_resv_desc_list_t resvlist; 25507 mhioc_resv_desc_t resvdesc; 25508 uchar_t *data_bufp = NULL; 25509 int data_len; 25510 int rval = 0; 25511 int i; 25512 size_t copysz; 25513 mhioc_resv_desc_t *bufp; 25514 sd_ssc_t *ssc; 25515 25516 if ((ptr = usrp) == NULL) { 25517 return (EINVAL); 25518 } 25519 25520 ssc = sd_ssc_init(un); 25521 25522 /* 25523 * Get the listsize from user 25524 */ 25525 #ifdef _MULTI_DATAMODEL 25526 switch (ddi_model_convert_from(flag & FMODELS)) { 25527 case DDI_MODEL_ILP32: 25528 copysz = sizeof (struct mhioc_resv_desc_list32); 25529 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 25530 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25531 "sd_persistent_reservation_in_read_resv: " 25532 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 25533 rval = EFAULT; 25534 goto done; 25535 } 25536 resvlist.listsize = resvlist32.listsize; 25537 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 25538 break; 25539 25540 case DDI_MODEL_NONE: 25541 copysz = sizeof (mhioc_resv_desc_list_t); 25542 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 25543 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25544 "sd_persistent_reservation_in_read_resv: " 25545 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 25546 rval = EFAULT; 25547 goto done; 25548 } 25549 break; 25550 } 25551 #else /* ! _MULTI_DATAMODEL */ 25552 copysz = sizeof (mhioc_resv_desc_list_t); 25553 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 25554 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25555 "sd_persistent_reservation_in_read_resv: " 25556 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 25557 rval = EFAULT; 25558 goto done; 25559 } 25560 #endif /* ! _MULTI_DATAMODEL */ 25561 25562 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 25563 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 25564 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 25565 25566 rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV, 25567 data_len, data_bufp); 25568 if (rval != 0) { 25569 if (rval == EIO) 25570 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE); 25571 else 25572 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 25573 goto done; 25574 } 25575 in = (sd_prin_readresv_t *)data_bufp; 25576 ptr->generation = BE_32(in->generation); 25577 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 25578 25579 /* 25580 * Return the min(listsize, listlen( keys 25581 */ 25582 #ifdef _MULTI_DATAMODEL 25583 25584 switch (ddi_model_convert_from(flag & FMODELS)) { 25585 case DDI_MODEL_ILP32: 25586 resvlist32.listlen = resvlist.listlen; 25587 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 25588 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25589 "sd_persistent_reservation_in_read_resv: " 25590 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 25591 rval = EFAULT; 25592 goto done; 25593 } 25594 break; 25595 25596 case DDI_MODEL_NONE: 25597 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 25598 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25599 "sd_persistent_reservation_in_read_resv: " 25600 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 25601 rval = EFAULT; 25602 goto done; 25603 } 25604 break; 25605 } 25606 25607 #else /* ! _MULTI_DATAMODEL */ 25608 25609 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 25610 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25611 "sd_persistent_reservation_in_read_resv: " 25612 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 25613 rval = EFAULT; 25614 goto done; 25615 } 25616 25617 #endif /* ! _MULTI_DATAMODEL */ 25618 25619 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 25620 bufp = resvlist.list; 25621 copysz = sizeof (mhioc_resv_desc_t); 25622 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 25623 i++, readresv_ptr++, bufp++) { 25624 25625 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 25626 MHIOC_RESV_KEY_SIZE); 25627 resvdesc.type = readresv_ptr->type; 25628 resvdesc.scope = readresv_ptr->scope; 25629 resvdesc.scope_specific_addr = 25630 BE_32(readresv_ptr->scope_specific_addr); 25631 25632 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 25633 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25634 "sd_persistent_reservation_in_read_resv: " 25635 "failed ddi_copyout: resvlist\n"); 25636 rval = EFAULT; 25637 goto done; 25638 } 25639 } 25640 done: 25641 sd_ssc_fini(ssc); 25642 /* only if data_bufp is allocated, we need to free it */ 25643 if (data_bufp) { 25644 kmem_free(data_bufp, data_len); 25645 } 25646 return (rval); 25647 } 25648 25649 25650 /* 25651 * Function: sr_change_blkmode() 25652 * 25653 * Description: This routine is the driver entry point for handling CD-ROM 25654 * block mode ioctl requests. Support for returning and changing 25655 * the current block size in use by the device is implemented. The 25656 * LBA size is changed via a MODE SELECT Block Descriptor. 25657 * 25658 * This routine issues a mode sense with an allocation length of 25659 * 12 bytes for the mode page header and a single block descriptor. 25660 * 25661 * Arguments: dev - the device 'dev_t' 25662 * cmd - the request type; one of CDROMGBLKMODE (get) or 25663 * CDROMSBLKMODE (set) 25664 * data - current block size or requested block size 25665 * flag - this argument is a pass through to ddi_copyxxx() directly 25666 * from the mode argument of ioctl(). 25667 * 25668 * Return Code: the code returned by sd_send_scsi_cmd() 25669 * EINVAL if invalid arguments are provided 25670 * EFAULT if ddi_copyxxx() fails 25671 * ENXIO if fail ddi_get_soft_state 25672 * EIO if invalid mode sense block descriptor length 25673 * 25674 */ 25675 25676 static int 25677 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 25678 { 25679 struct sd_lun *un = NULL; 25680 struct mode_header *sense_mhp, *select_mhp; 25681 struct block_descriptor *sense_desc, *select_desc; 25682 int current_bsize; 25683 int rval = EINVAL; 25684 uchar_t *sense = NULL; 25685 uchar_t *select = NULL; 25686 sd_ssc_t *ssc; 25687 25688 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 25689 25690 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25691 return (ENXIO); 25692 } 25693 25694 /* 25695 * The block length is changed via the Mode Select block descriptor, the 25696 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 25697 * required as part of this routine. Therefore the mode sense allocation 25698 * length is specified to be the length of a mode page header and a 25699 * block descriptor. 25700 */ 25701 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 25702 25703 ssc = sd_ssc_init(un); 25704 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 25705 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD); 25706 sd_ssc_fini(ssc); 25707 if (rval != 0) { 25708 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25709 "sr_change_blkmode: Mode Sense Failed\n"); 25710 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 25711 return (rval); 25712 } 25713 25714 /* Check the block descriptor len to handle only 1 block descriptor */ 25715 sense_mhp = (struct mode_header *)sense; 25716 if ((sense_mhp->bdesc_length == 0) || 25717 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 25718 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25719 "sr_change_blkmode: Mode Sense returned invalid block" 25720 " descriptor length\n"); 25721 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 25722 return (EIO); 25723 } 25724 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 25725 current_bsize = ((sense_desc->blksize_hi << 16) | 25726 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 25727 25728 /* Process command */ 25729 switch (cmd) { 25730 case CDROMGBLKMODE: 25731 /* Return the block size obtained during the mode sense */ 25732 if (ddi_copyout(¤t_bsize, (void *)data, 25733 sizeof (int), flag) != 0) 25734 rval = EFAULT; 25735 break; 25736 case CDROMSBLKMODE: 25737 /* Validate the requested block size */ 25738 switch (data) { 25739 case CDROM_BLK_512: 25740 case CDROM_BLK_1024: 25741 case CDROM_BLK_2048: 25742 case CDROM_BLK_2056: 25743 case CDROM_BLK_2336: 25744 case CDROM_BLK_2340: 25745 case CDROM_BLK_2352: 25746 case CDROM_BLK_2368: 25747 case CDROM_BLK_2448: 25748 case CDROM_BLK_2646: 25749 case CDROM_BLK_2647: 25750 break; 25751 default: 25752 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25753 "sr_change_blkmode: " 25754 "Block Size '%ld' Not Supported\n", data); 25755 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 25756 return (EINVAL); 25757 } 25758 25759 /* 25760 * The current block size matches the requested block size so 25761 * there is no need to send the mode select to change the size 25762 */ 25763 if (current_bsize == data) { 25764 break; 25765 } 25766 25767 /* Build the select data for the requested block size */ 25768 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 25769 select_mhp = (struct mode_header *)select; 25770 select_desc = 25771 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 25772 /* 25773 * The LBA size is changed via the block descriptor, so the 25774 * descriptor is built according to the user data 25775 */ 25776 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 25777 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 25778 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 25779 select_desc->blksize_lo = (char)((data) & 0x000000ff); 25780 25781 /* Send the mode select for the requested block size */ 25782 ssc = sd_ssc_init(un); 25783 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 25784 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 25785 SD_PATH_STANDARD); 25786 sd_ssc_fini(ssc); 25787 if (rval != 0) { 25788 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25789 "sr_change_blkmode: Mode Select Failed\n"); 25790 /* 25791 * The mode select failed for the requested block size, 25792 * so reset the data for the original block size and 25793 * send it to the target. The error is indicated by the 25794 * return value for the failed mode select. 25795 */ 25796 select_desc->blksize_hi = sense_desc->blksize_hi; 25797 select_desc->blksize_mid = sense_desc->blksize_mid; 25798 select_desc->blksize_lo = sense_desc->blksize_lo; 25799 ssc = sd_ssc_init(un); 25800 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, 25801 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 25802 SD_PATH_STANDARD); 25803 sd_ssc_fini(ssc); 25804 } else { 25805 ASSERT(!mutex_owned(SD_MUTEX(un))); 25806 mutex_enter(SD_MUTEX(un)); 25807 sd_update_block_info(un, (uint32_t)data, 0); 25808 mutex_exit(SD_MUTEX(un)); 25809 } 25810 break; 25811 default: 25812 /* should not reach here, but check anyway */ 25813 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25814 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 25815 rval = EINVAL; 25816 break; 25817 } 25818 25819 if (select) { 25820 kmem_free(select, BUFLEN_CHG_BLK_MODE); 25821 } 25822 if (sense) { 25823 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 25824 } 25825 return (rval); 25826 } 25827 25828 25829 /* 25830 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 25831 * implement driver support for getting and setting the CD speed. The command 25832 * set used will be based on the device type. If the device has not been 25833 * identified as MMC the Toshiba vendor specific mode page will be used. If 25834 * the device is MMC but does not support the Real Time Streaming feature 25835 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 25836 * be used to read the speed. 25837 */ 25838 25839 /* 25840 * Function: sr_change_speed() 25841 * 25842 * Description: This routine is the driver entry point for handling CD-ROM 25843 * drive speed ioctl requests for devices supporting the Toshiba 25844 * vendor specific drive speed mode page. Support for returning 25845 * and changing the current drive speed in use by the device is 25846 * implemented. 25847 * 25848 * Arguments: dev - the device 'dev_t' 25849 * cmd - the request type; one of CDROMGDRVSPEED (get) or 25850 * CDROMSDRVSPEED (set) 25851 * data - current drive speed or requested drive speed 25852 * flag - this argument is a pass through to ddi_copyxxx() directly 25853 * from the mode argument of ioctl(). 25854 * 25855 * Return Code: the code returned by sd_send_scsi_cmd() 25856 * EINVAL if invalid arguments are provided 25857 * EFAULT if ddi_copyxxx() fails 25858 * ENXIO if fail ddi_get_soft_state 25859 * EIO if invalid mode sense block descriptor length 25860 */ 25861 25862 static int 25863 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 25864 { 25865 struct sd_lun *un = NULL; 25866 struct mode_header *sense_mhp, *select_mhp; 25867 struct mode_speed *sense_page, *select_page; 25868 int current_speed; 25869 int rval = EINVAL; 25870 int bd_len; 25871 uchar_t *sense = NULL; 25872 uchar_t *select = NULL; 25873 sd_ssc_t *ssc; 25874 25875 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 25876 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25877 return (ENXIO); 25878 } 25879 25880 /* 25881 * Note: The drive speed is being modified here according to a Toshiba 25882 * vendor specific mode page (0x31). 25883 */ 25884 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 25885 25886 ssc = sd_ssc_init(un); 25887 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 25888 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 25889 SD_PATH_STANDARD); 25890 sd_ssc_fini(ssc); 25891 if (rval != 0) { 25892 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25893 "sr_change_speed: Mode Sense Failed\n"); 25894 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 25895 return (rval); 25896 } 25897 sense_mhp = (struct mode_header *)sense; 25898 25899 /* Check the block descriptor len to handle only 1 block descriptor */ 25900 bd_len = sense_mhp->bdesc_length; 25901 if (bd_len > MODE_BLK_DESC_LENGTH) { 25902 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25903 "sr_change_speed: Mode Sense returned invalid block " 25904 "descriptor length\n"); 25905 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 25906 return (EIO); 25907 } 25908 25909 sense_page = (struct mode_speed *) 25910 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 25911 current_speed = sense_page->speed; 25912 25913 /* Process command */ 25914 switch (cmd) { 25915 case CDROMGDRVSPEED: 25916 /* Return the drive speed obtained during the mode sense */ 25917 if (current_speed == 0x2) { 25918 current_speed = CDROM_TWELVE_SPEED; 25919 } 25920 if (ddi_copyout(¤t_speed, (void *)data, 25921 sizeof (int), flag) != 0) { 25922 rval = EFAULT; 25923 } 25924 break; 25925 case CDROMSDRVSPEED: 25926 /* Validate the requested drive speed */ 25927 switch ((uchar_t)data) { 25928 case CDROM_TWELVE_SPEED: 25929 data = 0x2; 25930 /*FALLTHROUGH*/ 25931 case CDROM_NORMAL_SPEED: 25932 case CDROM_DOUBLE_SPEED: 25933 case CDROM_QUAD_SPEED: 25934 case CDROM_MAXIMUM_SPEED: 25935 break; 25936 default: 25937 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25938 "sr_change_speed: " 25939 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 25940 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 25941 return (EINVAL); 25942 } 25943 25944 /* 25945 * The current drive speed matches the requested drive speed so 25946 * there is no need to send the mode select to change the speed 25947 */ 25948 if (current_speed == data) { 25949 break; 25950 } 25951 25952 /* Build the select data for the requested drive speed */ 25953 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 25954 select_mhp = (struct mode_header *)select; 25955 select_mhp->bdesc_length = 0; 25956 select_page = 25957 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 25958 select_page = 25959 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 25960 select_page->mode_page.code = CDROM_MODE_SPEED; 25961 select_page->mode_page.length = 2; 25962 select_page->speed = (uchar_t)data; 25963 25964 /* Send the mode select for the requested block size */ 25965 ssc = sd_ssc_init(un); 25966 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 25967 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 25968 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 25969 sd_ssc_fini(ssc); 25970 if (rval != 0) { 25971 /* 25972 * The mode select failed for the requested drive speed, 25973 * so reset the data for the original drive speed and 25974 * send it to the target. The error is indicated by the 25975 * return value for the failed mode select. 25976 */ 25977 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25978 "sr_drive_speed: Mode Select Failed\n"); 25979 select_page->speed = sense_page->speed; 25980 ssc = sd_ssc_init(un); 25981 (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 25982 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 25983 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 25984 sd_ssc_fini(ssc); 25985 } 25986 break; 25987 default: 25988 /* should not reach here, but check anyway */ 25989 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25990 "sr_change_speed: Command '%x' Not Supported\n", cmd); 25991 rval = EINVAL; 25992 break; 25993 } 25994 25995 if (select) { 25996 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 25997 } 25998 if (sense) { 25999 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 26000 } 26001 26002 return (rval); 26003 } 26004 26005 26006 /* 26007 * Function: sr_atapi_change_speed() 26008 * 26009 * Description: This routine is the driver entry point for handling CD-ROM 26010 * drive speed ioctl requests for MMC devices that do not support 26011 * the Real Time Streaming feature (0x107). 26012 * 26013 * Note: This routine will use the SET SPEED command which may not 26014 * be supported by all devices. 26015 * 26016 * Arguments: dev- the device 'dev_t' 26017 * cmd- the request type; one of CDROMGDRVSPEED (get) or 26018 * CDROMSDRVSPEED (set) 26019 * data- current drive speed or requested drive speed 26020 * flag- this argument is a pass through to ddi_copyxxx() directly 26021 * from the mode argument of ioctl(). 26022 * 26023 * Return Code: the code returned by sd_send_scsi_cmd() 26024 * EINVAL if invalid arguments are provided 26025 * EFAULT if ddi_copyxxx() fails 26026 * ENXIO if fail ddi_get_soft_state 26027 * EIO if invalid mode sense block descriptor length 26028 */ 26029 26030 static int 26031 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 26032 { 26033 struct sd_lun *un; 26034 struct uscsi_cmd *com = NULL; 26035 struct mode_header_grp2 *sense_mhp; 26036 uchar_t *sense_page; 26037 uchar_t *sense = NULL; 26038 char cdb[CDB_GROUP5]; 26039 int bd_len; 26040 int current_speed = 0; 26041 int max_speed = 0; 26042 int rval; 26043 sd_ssc_t *ssc; 26044 26045 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 26046 26047 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26048 return (ENXIO); 26049 } 26050 26051 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 26052 26053 ssc = sd_ssc_init(un); 26054 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 26055 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 26056 SD_PATH_STANDARD); 26057 sd_ssc_fini(ssc); 26058 if (rval != 0) { 26059 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26060 "sr_atapi_change_speed: Mode Sense Failed\n"); 26061 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26062 return (rval); 26063 } 26064 26065 /* Check the block descriptor len to handle only 1 block descriptor */ 26066 sense_mhp = (struct mode_header_grp2 *)sense; 26067 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 26068 if (bd_len > MODE_BLK_DESC_LENGTH) { 26069 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26070 "sr_atapi_change_speed: Mode Sense returned invalid " 26071 "block descriptor length\n"); 26072 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26073 return (EIO); 26074 } 26075 26076 /* Calculate the current and maximum drive speeds */ 26077 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 26078 current_speed = (sense_page[14] << 8) | sense_page[15]; 26079 max_speed = (sense_page[8] << 8) | sense_page[9]; 26080 26081 /* Process the command */ 26082 switch (cmd) { 26083 case CDROMGDRVSPEED: 26084 current_speed /= SD_SPEED_1X; 26085 if (ddi_copyout(¤t_speed, (void *)data, 26086 sizeof (int), flag) != 0) 26087 rval = EFAULT; 26088 break; 26089 case CDROMSDRVSPEED: 26090 /* Convert the speed code to KB/sec */ 26091 switch ((uchar_t)data) { 26092 case CDROM_NORMAL_SPEED: 26093 current_speed = SD_SPEED_1X; 26094 break; 26095 case CDROM_DOUBLE_SPEED: 26096 current_speed = 2 * SD_SPEED_1X; 26097 break; 26098 case CDROM_QUAD_SPEED: 26099 current_speed = 4 * SD_SPEED_1X; 26100 break; 26101 case CDROM_TWELVE_SPEED: 26102 current_speed = 12 * SD_SPEED_1X; 26103 break; 26104 case CDROM_MAXIMUM_SPEED: 26105 current_speed = 0xffff; 26106 break; 26107 default: 26108 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26109 "sr_atapi_change_speed: invalid drive speed %d\n", 26110 (uchar_t)data); 26111 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26112 return (EINVAL); 26113 } 26114 26115 /* Check the request against the drive's max speed. */ 26116 if (current_speed != 0xffff) { 26117 if (current_speed > max_speed) { 26118 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26119 return (EINVAL); 26120 } 26121 } 26122 26123 /* 26124 * Build and send the SET SPEED command 26125 * 26126 * Note: The SET SPEED (0xBB) command used in this routine is 26127 * obsolete per the SCSI MMC spec but still supported in the 26128 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 26129 * therefore the command is still implemented in this routine. 26130 */ 26131 bzero(cdb, sizeof (cdb)); 26132 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 26133 cdb[2] = (uchar_t)(current_speed >> 8); 26134 cdb[3] = (uchar_t)current_speed; 26135 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26136 com->uscsi_cdb = (caddr_t)cdb; 26137 com->uscsi_cdblen = CDB_GROUP5; 26138 com->uscsi_bufaddr = NULL; 26139 com->uscsi_buflen = 0; 26140 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26141 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 26142 break; 26143 default: 26144 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26145 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 26146 rval = EINVAL; 26147 } 26148 26149 if (sense) { 26150 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 26151 } 26152 if (com) { 26153 kmem_free(com, sizeof (*com)); 26154 } 26155 return (rval); 26156 } 26157 26158 26159 /* 26160 * Function: sr_pause_resume() 26161 * 26162 * Description: This routine is the driver entry point for handling CD-ROM 26163 * pause/resume ioctl requests. This only affects the audio play 26164 * operation. 26165 * 26166 * Arguments: dev - the device 'dev_t' 26167 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 26168 * for setting the resume bit of the cdb. 26169 * 26170 * Return Code: the code returned by sd_send_scsi_cmd() 26171 * EINVAL if invalid mode specified 26172 * 26173 */ 26174 26175 static int 26176 sr_pause_resume(dev_t dev, int cmd) 26177 { 26178 struct sd_lun *un; 26179 struct uscsi_cmd *com; 26180 char cdb[CDB_GROUP1]; 26181 int rval; 26182 26183 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26184 return (ENXIO); 26185 } 26186 26187 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26188 bzero(cdb, CDB_GROUP1); 26189 cdb[0] = SCMD_PAUSE_RESUME; 26190 switch (cmd) { 26191 case CDROMRESUME: 26192 cdb[8] = 1; 26193 break; 26194 case CDROMPAUSE: 26195 cdb[8] = 0; 26196 break; 26197 default: 26198 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 26199 " Command '%x' Not Supported\n", cmd); 26200 rval = EINVAL; 26201 goto done; 26202 } 26203 26204 com->uscsi_cdb = cdb; 26205 com->uscsi_cdblen = CDB_GROUP1; 26206 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26207 26208 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26209 SD_PATH_STANDARD); 26210 26211 done: 26212 kmem_free(com, sizeof (*com)); 26213 return (rval); 26214 } 26215 26216 26217 /* 26218 * Function: sr_play_msf() 26219 * 26220 * Description: This routine is the driver entry point for handling CD-ROM 26221 * ioctl requests to output the audio signals at the specified 26222 * starting address and continue the audio play until the specified 26223 * ending address (CDROMPLAYMSF) The address is in Minute Second 26224 * Frame (MSF) format. 26225 * 26226 * Arguments: dev - the device 'dev_t' 26227 * data - pointer to user provided audio msf structure, 26228 * specifying start/end addresses. 26229 * flag - this argument is a pass through to ddi_copyxxx() 26230 * directly from the mode argument of ioctl(). 26231 * 26232 * Return Code: the code returned by sd_send_scsi_cmd() 26233 * EFAULT if ddi_copyxxx() fails 26234 * ENXIO if fail ddi_get_soft_state 26235 * EINVAL if data pointer is NULL 26236 */ 26237 26238 static int 26239 sr_play_msf(dev_t dev, caddr_t data, int flag) 26240 { 26241 struct sd_lun *un; 26242 struct uscsi_cmd *com; 26243 struct cdrom_msf msf_struct; 26244 struct cdrom_msf *msf = &msf_struct; 26245 char cdb[CDB_GROUP1]; 26246 int rval; 26247 26248 if (data == NULL) { 26249 return (EINVAL); 26250 } 26251 26252 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26253 return (ENXIO); 26254 } 26255 26256 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 26257 return (EFAULT); 26258 } 26259 26260 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26261 bzero(cdb, CDB_GROUP1); 26262 cdb[0] = SCMD_PLAYAUDIO_MSF; 26263 if (un->un_f_cfg_playmsf_bcd == TRUE) { 26264 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 26265 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 26266 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 26267 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 26268 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 26269 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 26270 } else { 26271 cdb[3] = msf->cdmsf_min0; 26272 cdb[4] = msf->cdmsf_sec0; 26273 cdb[5] = msf->cdmsf_frame0; 26274 cdb[6] = msf->cdmsf_min1; 26275 cdb[7] = msf->cdmsf_sec1; 26276 cdb[8] = msf->cdmsf_frame1; 26277 } 26278 com->uscsi_cdb = cdb; 26279 com->uscsi_cdblen = CDB_GROUP1; 26280 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26281 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26282 SD_PATH_STANDARD); 26283 kmem_free(com, sizeof (*com)); 26284 return (rval); 26285 } 26286 26287 26288 /* 26289 * Function: sr_play_trkind() 26290 * 26291 * Description: This routine is the driver entry point for handling CD-ROM 26292 * ioctl requests to output the audio signals at the specified 26293 * starting address and continue the audio play until the specified 26294 * ending address (CDROMPLAYTRKIND). The address is in Track Index 26295 * format. 26296 * 26297 * Arguments: dev - the device 'dev_t' 26298 * data - pointer to user provided audio track/index structure, 26299 * specifying start/end addresses. 26300 * flag - this argument is a pass through to ddi_copyxxx() 26301 * directly from the mode argument of ioctl(). 26302 * 26303 * Return Code: the code returned by sd_send_scsi_cmd() 26304 * EFAULT if ddi_copyxxx() fails 26305 * ENXIO if fail ddi_get_soft_state 26306 * EINVAL if data pointer is NULL 26307 */ 26308 26309 static int 26310 sr_play_trkind(dev_t dev, caddr_t data, int flag) 26311 { 26312 struct cdrom_ti ti_struct; 26313 struct cdrom_ti *ti = &ti_struct; 26314 struct uscsi_cmd *com = NULL; 26315 char cdb[CDB_GROUP1]; 26316 int rval; 26317 26318 if (data == NULL) { 26319 return (EINVAL); 26320 } 26321 26322 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 26323 return (EFAULT); 26324 } 26325 26326 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26327 bzero(cdb, CDB_GROUP1); 26328 cdb[0] = SCMD_PLAYAUDIO_TI; 26329 cdb[4] = ti->cdti_trk0; 26330 cdb[5] = ti->cdti_ind0; 26331 cdb[7] = ti->cdti_trk1; 26332 cdb[8] = ti->cdti_ind1; 26333 com->uscsi_cdb = cdb; 26334 com->uscsi_cdblen = CDB_GROUP1; 26335 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 26336 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26337 SD_PATH_STANDARD); 26338 kmem_free(com, sizeof (*com)); 26339 return (rval); 26340 } 26341 26342 26343 /* 26344 * Function: sr_read_all_subcodes() 26345 * 26346 * Description: This routine is the driver entry point for handling CD-ROM 26347 * ioctl requests to return raw subcode data while the target is 26348 * playing audio (CDROMSUBCODE). 26349 * 26350 * Arguments: dev - the device 'dev_t' 26351 * data - pointer to user provided cdrom subcode structure, 26352 * specifying the transfer length and address. 26353 * flag - this argument is a pass through to ddi_copyxxx() 26354 * directly from the mode argument of ioctl(). 26355 * 26356 * Return Code: the code returned by sd_send_scsi_cmd() 26357 * EFAULT if ddi_copyxxx() fails 26358 * ENXIO if fail ddi_get_soft_state 26359 * EINVAL if data pointer is NULL 26360 */ 26361 26362 static int 26363 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 26364 { 26365 struct sd_lun *un = NULL; 26366 struct uscsi_cmd *com = NULL; 26367 struct cdrom_subcode *subcode = NULL; 26368 int rval; 26369 size_t buflen; 26370 char cdb[CDB_GROUP5]; 26371 26372 #ifdef _MULTI_DATAMODEL 26373 /* To support ILP32 applications in an LP64 world */ 26374 struct cdrom_subcode32 cdrom_subcode32; 26375 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 26376 #endif 26377 if (data == NULL) { 26378 return (EINVAL); 26379 } 26380 26381 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26382 return (ENXIO); 26383 } 26384 26385 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 26386 26387 #ifdef _MULTI_DATAMODEL 26388 switch (ddi_model_convert_from(flag & FMODELS)) { 26389 case DDI_MODEL_ILP32: 26390 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 26391 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26392 "sr_read_all_subcodes: ddi_copyin Failed\n"); 26393 kmem_free(subcode, sizeof (struct cdrom_subcode)); 26394 return (EFAULT); 26395 } 26396 /* Convert the ILP32 uscsi data from the application to LP64 */ 26397 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 26398 break; 26399 case DDI_MODEL_NONE: 26400 if (ddi_copyin(data, subcode, 26401 sizeof (struct cdrom_subcode), flag)) { 26402 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26403 "sr_read_all_subcodes: ddi_copyin Failed\n"); 26404 kmem_free(subcode, sizeof (struct cdrom_subcode)); 26405 return (EFAULT); 26406 } 26407 break; 26408 } 26409 #else /* ! _MULTI_DATAMODEL */ 26410 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 26411 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26412 "sr_read_all_subcodes: ddi_copyin Failed\n"); 26413 kmem_free(subcode, sizeof (struct cdrom_subcode)); 26414 return (EFAULT); 26415 } 26416 #endif /* _MULTI_DATAMODEL */ 26417 26418 /* 26419 * Since MMC-2 expects max 3 bytes for length, check if the 26420 * length input is greater than 3 bytes 26421 */ 26422 if ((subcode->cdsc_length & 0xFF000000) != 0) { 26423 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26424 "sr_read_all_subcodes: " 26425 "cdrom transfer length too large: %d (limit %d)\n", 26426 subcode->cdsc_length, 0xFFFFFF); 26427 kmem_free(subcode, sizeof (struct cdrom_subcode)); 26428 return (EINVAL); 26429 } 26430 26431 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 26432 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26433 bzero(cdb, CDB_GROUP5); 26434 26435 if (un->un_f_mmc_cap == TRUE) { 26436 cdb[0] = (char)SCMD_READ_CD; 26437 cdb[2] = (char)0xff; 26438 cdb[3] = (char)0xff; 26439 cdb[4] = (char)0xff; 26440 cdb[5] = (char)0xff; 26441 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 26442 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 26443 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 26444 cdb[10] = 1; 26445 } else { 26446 /* 26447 * Note: A vendor specific command (0xDF) is being used her to 26448 * request a read of all subcodes. 26449 */ 26450 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 26451 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 26452 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 26453 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 26454 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 26455 } 26456 com->uscsi_cdb = cdb; 26457 com->uscsi_cdblen = CDB_GROUP5; 26458 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 26459 com->uscsi_buflen = buflen; 26460 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 26461 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 26462 SD_PATH_STANDARD); 26463 kmem_free(subcode, sizeof (struct cdrom_subcode)); 26464 kmem_free(com, sizeof (*com)); 26465 return (rval); 26466 } 26467 26468 26469 /* 26470 * Function: sr_read_subchannel() 26471 * 26472 * Description: This routine is the driver entry point for handling CD-ROM 26473 * ioctl requests to return the Q sub-channel data of the CD 26474 * current position block. (CDROMSUBCHNL) The data includes the 26475 * track number, index number, absolute CD-ROM address (LBA or MSF 26476 * format per the user) , track relative CD-ROM address (LBA or MSF 26477 * format per the user), control data and audio status. 26478 * 26479 * Arguments: dev - the device 'dev_t' 26480 * data - pointer to user provided cdrom sub-channel structure 26481 * flag - this argument is a pass through to ddi_copyxxx() 26482 * directly from the mode argument of ioctl(). 26483 * 26484 * Return Code: the code returned by sd_send_scsi_cmd() 26485 * EFAULT if ddi_copyxxx() fails 26486 * ENXIO if fail ddi_get_soft_state 26487 * EINVAL if data pointer is NULL 26488 */ 26489 26490 static int 26491 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 26492 { 26493 struct sd_lun *un; 26494 struct uscsi_cmd *com; 26495 struct cdrom_subchnl subchanel; 26496 struct cdrom_subchnl *subchnl = &subchanel; 26497 char cdb[CDB_GROUP1]; 26498 caddr_t buffer; 26499 int rval; 26500 26501 if (data == NULL) { 26502 return (EINVAL); 26503 } 26504 26505 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26506 (un->un_state == SD_STATE_OFFLINE)) { 26507 return (ENXIO); 26508 } 26509 26510 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 26511 return (EFAULT); 26512 } 26513 26514 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 26515 bzero(cdb, CDB_GROUP1); 26516 cdb[0] = SCMD_READ_SUBCHANNEL; 26517 /* Set the MSF bit based on the user requested address format */ 26518 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 26519 /* 26520 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 26521 * returned 26522 */ 26523 cdb[2] = 0x40; 26524 /* 26525 * Set byte 3 to specify the return data format. A value of 0x01 26526 * indicates that the CD-ROM current position should be returned. 26527 */ 26528 cdb[3] = 0x01; 26529 cdb[8] = 0x10; 26530 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26531 com->uscsi_cdb = cdb; 26532 com->uscsi_cdblen = CDB_GROUP1; 26533 com->uscsi_bufaddr = buffer; 26534 com->uscsi_buflen = 16; 26535 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 26536 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26537 SD_PATH_STANDARD); 26538 if (rval != 0) { 26539 kmem_free(buffer, 16); 26540 kmem_free(com, sizeof (*com)); 26541 return (rval); 26542 } 26543 26544 /* Process the returned Q sub-channel data */ 26545 subchnl->cdsc_audiostatus = buffer[1]; 26546 subchnl->cdsc_adr = (buffer[5] & 0xF0); 26547 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 26548 subchnl->cdsc_trk = buffer[6]; 26549 subchnl->cdsc_ind = buffer[7]; 26550 if (subchnl->cdsc_format & CDROM_LBA) { 26551 subchnl->cdsc_absaddr.lba = 26552 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 26553 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 26554 subchnl->cdsc_reladdr.lba = 26555 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 26556 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 26557 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 26558 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 26559 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 26560 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 26561 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 26562 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 26563 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 26564 } else { 26565 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 26566 subchnl->cdsc_absaddr.msf.second = buffer[10]; 26567 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 26568 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 26569 subchnl->cdsc_reladdr.msf.second = buffer[14]; 26570 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 26571 } 26572 kmem_free(buffer, 16); 26573 kmem_free(com, sizeof (*com)); 26574 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 26575 != 0) { 26576 return (EFAULT); 26577 } 26578 return (rval); 26579 } 26580 26581 26582 /* 26583 * Function: sr_read_tocentry() 26584 * 26585 * Description: This routine is the driver entry point for handling CD-ROM 26586 * ioctl requests to read from the Table of Contents (TOC) 26587 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 26588 * fields, the starting address (LBA or MSF format per the user) 26589 * and the data mode if the user specified track is a data track. 26590 * 26591 * Note: The READ HEADER (0x44) command used in this routine is 26592 * obsolete per the SCSI MMC spec but still supported in the 26593 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 26594 * therefore the command is still implemented in this routine. 26595 * 26596 * Arguments: dev - the device 'dev_t' 26597 * data - pointer to user provided toc entry structure, 26598 * specifying the track # and the address format 26599 * (LBA or MSF). 26600 * flag - this argument is a pass through to ddi_copyxxx() 26601 * directly from the mode argument of ioctl(). 26602 * 26603 * Return Code: the code returned by sd_send_scsi_cmd() 26604 * EFAULT if ddi_copyxxx() fails 26605 * ENXIO if fail ddi_get_soft_state 26606 * EINVAL if data pointer is NULL 26607 */ 26608 26609 static int 26610 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 26611 { 26612 struct sd_lun *un = NULL; 26613 struct uscsi_cmd *com; 26614 struct cdrom_tocentry toc_entry; 26615 struct cdrom_tocentry *entry = &toc_entry; 26616 caddr_t buffer; 26617 int rval; 26618 char cdb[CDB_GROUP1]; 26619 26620 if (data == NULL) { 26621 return (EINVAL); 26622 } 26623 26624 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26625 (un->un_state == SD_STATE_OFFLINE)) { 26626 return (ENXIO); 26627 } 26628 26629 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 26630 return (EFAULT); 26631 } 26632 26633 /* Validate the requested track and address format */ 26634 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 26635 return (EINVAL); 26636 } 26637 26638 if (entry->cdte_track == 0) { 26639 return (EINVAL); 26640 } 26641 26642 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 26643 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26644 bzero(cdb, CDB_GROUP1); 26645 26646 cdb[0] = SCMD_READ_TOC; 26647 /* Set the MSF bit based on the user requested address format */ 26648 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 26649 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 26650 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 26651 } else { 26652 cdb[6] = entry->cdte_track; 26653 } 26654 26655 /* 26656 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 26657 * (4 byte TOC response header + 8 byte track descriptor) 26658 */ 26659 cdb[8] = 12; 26660 com->uscsi_cdb = cdb; 26661 com->uscsi_cdblen = CDB_GROUP1; 26662 com->uscsi_bufaddr = buffer; 26663 com->uscsi_buflen = 0x0C; 26664 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 26665 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26666 SD_PATH_STANDARD); 26667 if (rval != 0) { 26668 kmem_free(buffer, 12); 26669 kmem_free(com, sizeof (*com)); 26670 return (rval); 26671 } 26672 26673 /* Process the toc entry */ 26674 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 26675 entry->cdte_ctrl = (buffer[5] & 0x0F); 26676 if (entry->cdte_format & CDROM_LBA) { 26677 entry->cdte_addr.lba = 26678 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 26679 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 26680 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 26681 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 26682 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 26683 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 26684 /* 26685 * Send a READ TOC command using the LBA address format to get 26686 * the LBA for the track requested so it can be used in the 26687 * READ HEADER request 26688 * 26689 * Note: The MSF bit of the READ HEADER command specifies the 26690 * output format. The block address specified in that command 26691 * must be in LBA format. 26692 */ 26693 cdb[1] = 0; 26694 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26695 SD_PATH_STANDARD); 26696 if (rval != 0) { 26697 kmem_free(buffer, 12); 26698 kmem_free(com, sizeof (*com)); 26699 return (rval); 26700 } 26701 } else { 26702 entry->cdte_addr.msf.minute = buffer[9]; 26703 entry->cdte_addr.msf.second = buffer[10]; 26704 entry->cdte_addr.msf.frame = buffer[11]; 26705 /* 26706 * Send a READ TOC command using the LBA address format to get 26707 * the LBA for the track requested so it can be used in the 26708 * READ HEADER request 26709 * 26710 * Note: The MSF bit of the READ HEADER command specifies the 26711 * output format. The block address specified in that command 26712 * must be in LBA format. 26713 */ 26714 cdb[1] = 0; 26715 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26716 SD_PATH_STANDARD); 26717 if (rval != 0) { 26718 kmem_free(buffer, 12); 26719 kmem_free(com, sizeof (*com)); 26720 return (rval); 26721 } 26722 } 26723 26724 /* 26725 * Build and send the READ HEADER command to determine the data mode of 26726 * the user specified track. 26727 */ 26728 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 26729 (entry->cdte_track != CDROM_LEADOUT)) { 26730 bzero(cdb, CDB_GROUP1); 26731 cdb[0] = SCMD_READ_HEADER; 26732 cdb[2] = buffer[8]; 26733 cdb[3] = buffer[9]; 26734 cdb[4] = buffer[10]; 26735 cdb[5] = buffer[11]; 26736 cdb[8] = 0x08; 26737 com->uscsi_buflen = 0x08; 26738 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26739 SD_PATH_STANDARD); 26740 if (rval == 0) { 26741 entry->cdte_datamode = buffer[0]; 26742 } else { 26743 /* 26744 * READ HEADER command failed, since this is 26745 * obsoleted in one spec, its better to return 26746 * -1 for an invlid track so that we can still 26747 * receive the rest of the TOC data. 26748 */ 26749 entry->cdte_datamode = (uchar_t)-1; 26750 } 26751 } else { 26752 entry->cdte_datamode = (uchar_t)-1; 26753 } 26754 26755 kmem_free(buffer, 12); 26756 kmem_free(com, sizeof (*com)); 26757 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 26758 return (EFAULT); 26759 26760 return (rval); 26761 } 26762 26763 26764 /* 26765 * Function: sr_read_tochdr() 26766 * 26767 * Description: This routine is the driver entry point for handling CD-ROM 26768 * ioctl requests to read the Table of Contents (TOC) header 26769 * (CDROMREADTOHDR). The TOC header consists of the disk starting 26770 * and ending track numbers 26771 * 26772 * Arguments: dev - the device 'dev_t' 26773 * data - pointer to user provided toc header structure, 26774 * specifying the starting and ending track numbers. 26775 * flag - this argument is a pass through to ddi_copyxxx() 26776 * directly from the mode argument of ioctl(). 26777 * 26778 * Return Code: the code returned by sd_send_scsi_cmd() 26779 * EFAULT if ddi_copyxxx() fails 26780 * ENXIO if fail ddi_get_soft_state 26781 * EINVAL if data pointer is NULL 26782 */ 26783 26784 static int 26785 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 26786 { 26787 struct sd_lun *un; 26788 struct uscsi_cmd *com; 26789 struct cdrom_tochdr toc_header; 26790 struct cdrom_tochdr *hdr = &toc_header; 26791 char cdb[CDB_GROUP1]; 26792 int rval; 26793 caddr_t buffer; 26794 26795 if (data == NULL) { 26796 return (EINVAL); 26797 } 26798 26799 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26800 (un->un_state == SD_STATE_OFFLINE)) { 26801 return (ENXIO); 26802 } 26803 26804 buffer = kmem_zalloc(4, KM_SLEEP); 26805 bzero(cdb, CDB_GROUP1); 26806 cdb[0] = SCMD_READ_TOC; 26807 /* 26808 * Specifying a track number of 0x00 in the READ TOC command indicates 26809 * that the TOC header should be returned 26810 */ 26811 cdb[6] = 0x00; 26812 /* 26813 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 26814 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 26815 */ 26816 cdb[8] = 0x04; 26817 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26818 com->uscsi_cdb = cdb; 26819 com->uscsi_cdblen = CDB_GROUP1; 26820 com->uscsi_bufaddr = buffer; 26821 com->uscsi_buflen = 0x04; 26822 com->uscsi_timeout = 300; 26823 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 26824 26825 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26826 SD_PATH_STANDARD); 26827 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 26828 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 26829 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 26830 } else { 26831 hdr->cdth_trk0 = buffer[2]; 26832 hdr->cdth_trk1 = buffer[3]; 26833 } 26834 kmem_free(buffer, 4); 26835 kmem_free(com, sizeof (*com)); 26836 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 26837 return (EFAULT); 26838 } 26839 return (rval); 26840 } 26841 26842 26843 /* 26844 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 26845 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 26846 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 26847 * digital audio and extended architecture digital audio. These modes are 26848 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 26849 * MMC specs. 26850 * 26851 * In addition to support for the various data formats these routines also 26852 * include support for devices that implement only the direct access READ 26853 * commands (0x08, 0x28), devices that implement the READ_CD commands 26854 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 26855 * READ CDXA commands (0xD8, 0xDB) 26856 */ 26857 26858 /* 26859 * Function: sr_read_mode1() 26860 * 26861 * Description: This routine is the driver entry point for handling CD-ROM 26862 * ioctl read mode1 requests (CDROMREADMODE1). 26863 * 26864 * Arguments: dev - the device 'dev_t' 26865 * data - pointer to user provided cd read structure specifying 26866 * the lba buffer address and length. 26867 * flag - this argument is a pass through to ddi_copyxxx() 26868 * directly from the mode argument of ioctl(). 26869 * 26870 * Return Code: the code returned by sd_send_scsi_cmd() 26871 * EFAULT if ddi_copyxxx() fails 26872 * ENXIO if fail ddi_get_soft_state 26873 * EINVAL if data pointer is NULL 26874 */ 26875 26876 static int 26877 sr_read_mode1(dev_t dev, caddr_t data, int flag) 26878 { 26879 struct sd_lun *un; 26880 struct cdrom_read mode1_struct; 26881 struct cdrom_read *mode1 = &mode1_struct; 26882 int rval; 26883 sd_ssc_t *ssc; 26884 26885 #ifdef _MULTI_DATAMODEL 26886 /* To support ILP32 applications in an LP64 world */ 26887 struct cdrom_read32 cdrom_read32; 26888 struct cdrom_read32 *cdrd32 = &cdrom_read32; 26889 #endif /* _MULTI_DATAMODEL */ 26890 26891 if (data == NULL) { 26892 return (EINVAL); 26893 } 26894 26895 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26896 (un->un_state == SD_STATE_OFFLINE)) { 26897 return (ENXIO); 26898 } 26899 26900 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 26901 "sd_read_mode1: entry: un:0x%p\n", un); 26902 26903 #ifdef _MULTI_DATAMODEL 26904 switch (ddi_model_convert_from(flag & FMODELS)) { 26905 case DDI_MODEL_ILP32: 26906 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 26907 return (EFAULT); 26908 } 26909 /* Convert the ILP32 uscsi data from the application to LP64 */ 26910 cdrom_read32tocdrom_read(cdrd32, mode1); 26911 break; 26912 case DDI_MODEL_NONE: 26913 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 26914 return (EFAULT); 26915 } 26916 } 26917 #else /* ! _MULTI_DATAMODEL */ 26918 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 26919 return (EFAULT); 26920 } 26921 #endif /* _MULTI_DATAMODEL */ 26922 26923 ssc = sd_ssc_init(un); 26924 rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr, 26925 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 26926 sd_ssc_fini(ssc); 26927 26928 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 26929 "sd_read_mode1: exit: un:0x%p\n", un); 26930 26931 return (rval); 26932 } 26933 26934 26935 /* 26936 * Function: sr_read_cd_mode2() 26937 * 26938 * Description: This routine is the driver entry point for handling CD-ROM 26939 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 26940 * support the READ CD (0xBE) command or the 1st generation 26941 * READ CD (0xD4) command. 26942 * 26943 * Arguments: dev - the device 'dev_t' 26944 * data - pointer to user provided cd read structure specifying 26945 * the lba buffer address and length. 26946 * flag - this argument is a pass through to ddi_copyxxx() 26947 * directly from the mode argument of ioctl(). 26948 * 26949 * Return Code: the code returned by sd_send_scsi_cmd() 26950 * EFAULT if ddi_copyxxx() fails 26951 * ENXIO if fail ddi_get_soft_state 26952 * EINVAL if data pointer is NULL 26953 */ 26954 26955 static int 26956 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 26957 { 26958 struct sd_lun *un; 26959 struct uscsi_cmd *com; 26960 struct cdrom_read mode2_struct; 26961 struct cdrom_read *mode2 = &mode2_struct; 26962 uchar_t cdb[CDB_GROUP5]; 26963 int nblocks; 26964 int rval; 26965 #ifdef _MULTI_DATAMODEL 26966 /* To support ILP32 applications in an LP64 world */ 26967 struct cdrom_read32 cdrom_read32; 26968 struct cdrom_read32 *cdrd32 = &cdrom_read32; 26969 #endif /* _MULTI_DATAMODEL */ 26970 26971 if (data == NULL) { 26972 return (EINVAL); 26973 } 26974 26975 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26976 (un->un_state == SD_STATE_OFFLINE)) { 26977 return (ENXIO); 26978 } 26979 26980 #ifdef _MULTI_DATAMODEL 26981 switch (ddi_model_convert_from(flag & FMODELS)) { 26982 case DDI_MODEL_ILP32: 26983 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 26984 return (EFAULT); 26985 } 26986 /* Convert the ILP32 uscsi data from the application to LP64 */ 26987 cdrom_read32tocdrom_read(cdrd32, mode2); 26988 break; 26989 case DDI_MODEL_NONE: 26990 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 26991 return (EFAULT); 26992 } 26993 break; 26994 } 26995 26996 #else /* ! _MULTI_DATAMODEL */ 26997 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 26998 return (EFAULT); 26999 } 27000 #endif /* _MULTI_DATAMODEL */ 27001 27002 bzero(cdb, sizeof (cdb)); 27003 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 27004 /* Read command supported by 1st generation atapi drives */ 27005 cdb[0] = SCMD_READ_CDD4; 27006 } else { 27007 /* Universal CD Access Command */ 27008 cdb[0] = SCMD_READ_CD; 27009 } 27010 27011 /* 27012 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 27013 */ 27014 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 27015 27016 /* set the start address */ 27017 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 27018 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 27019 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 27020 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 27021 27022 /* set the transfer length */ 27023 nblocks = mode2->cdread_buflen / 2336; 27024 cdb[6] = (uchar_t)(nblocks >> 16); 27025 cdb[7] = (uchar_t)(nblocks >> 8); 27026 cdb[8] = (uchar_t)nblocks; 27027 27028 /* set the filter bits */ 27029 cdb[9] = CDROM_READ_CD_USERDATA; 27030 27031 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27032 com->uscsi_cdb = (caddr_t)cdb; 27033 com->uscsi_cdblen = sizeof (cdb); 27034 com->uscsi_bufaddr = mode2->cdread_bufaddr; 27035 com->uscsi_buflen = mode2->cdread_buflen; 27036 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27037 27038 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27039 SD_PATH_STANDARD); 27040 kmem_free(com, sizeof (*com)); 27041 return (rval); 27042 } 27043 27044 27045 /* 27046 * Function: sr_read_mode2() 27047 * 27048 * Description: This routine is the driver entry point for handling CD-ROM 27049 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 27050 * do not support the READ CD (0xBE) command. 27051 * 27052 * Arguments: dev - the device 'dev_t' 27053 * data - pointer to user provided cd read structure specifying 27054 * the lba buffer address and length. 27055 * flag - this argument is a pass through to ddi_copyxxx() 27056 * directly from the mode argument of ioctl(). 27057 * 27058 * Return Code: the code returned by sd_send_scsi_cmd() 27059 * EFAULT if ddi_copyxxx() fails 27060 * ENXIO if fail ddi_get_soft_state 27061 * EINVAL if data pointer is NULL 27062 * EIO if fail to reset block size 27063 * EAGAIN if commands are in progress in the driver 27064 */ 27065 27066 static int 27067 sr_read_mode2(dev_t dev, caddr_t data, int flag) 27068 { 27069 struct sd_lun *un; 27070 struct cdrom_read mode2_struct; 27071 struct cdrom_read *mode2 = &mode2_struct; 27072 int rval; 27073 uint32_t restore_blksize; 27074 struct uscsi_cmd *com; 27075 uchar_t cdb[CDB_GROUP0]; 27076 int nblocks; 27077 27078 #ifdef _MULTI_DATAMODEL 27079 /* To support ILP32 applications in an LP64 world */ 27080 struct cdrom_read32 cdrom_read32; 27081 struct cdrom_read32 *cdrd32 = &cdrom_read32; 27082 #endif /* _MULTI_DATAMODEL */ 27083 27084 if (data == NULL) { 27085 return (EINVAL); 27086 } 27087 27088 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27089 (un->un_state == SD_STATE_OFFLINE)) { 27090 return (ENXIO); 27091 } 27092 27093 /* 27094 * Because this routine will update the device and driver block size 27095 * being used we want to make sure there are no commands in progress. 27096 * If commands are in progress the user will have to try again. 27097 * 27098 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 27099 * in sdioctl to protect commands from sdioctl through to the top of 27100 * sd_uscsi_strategy. See sdioctl for details. 27101 */ 27102 mutex_enter(SD_MUTEX(un)); 27103 if (un->un_ncmds_in_driver != 1) { 27104 mutex_exit(SD_MUTEX(un)); 27105 return (EAGAIN); 27106 } 27107 mutex_exit(SD_MUTEX(un)); 27108 27109 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27110 "sd_read_mode2: entry: un:0x%p\n", un); 27111 27112 #ifdef _MULTI_DATAMODEL 27113 switch (ddi_model_convert_from(flag & FMODELS)) { 27114 case DDI_MODEL_ILP32: 27115 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 27116 return (EFAULT); 27117 } 27118 /* Convert the ILP32 uscsi data from the application to LP64 */ 27119 cdrom_read32tocdrom_read(cdrd32, mode2); 27120 break; 27121 case DDI_MODEL_NONE: 27122 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 27123 return (EFAULT); 27124 } 27125 break; 27126 } 27127 #else /* ! _MULTI_DATAMODEL */ 27128 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 27129 return (EFAULT); 27130 } 27131 #endif /* _MULTI_DATAMODEL */ 27132 27133 /* Store the current target block size for restoration later */ 27134 restore_blksize = un->un_tgt_blocksize; 27135 27136 /* Change the device and soft state target block size to 2336 */ 27137 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 27138 rval = EIO; 27139 goto done; 27140 } 27141 27142 27143 bzero(cdb, sizeof (cdb)); 27144 27145 /* set READ operation */ 27146 cdb[0] = SCMD_READ; 27147 27148 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 27149 mode2->cdread_lba >>= 2; 27150 27151 /* set the start address */ 27152 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 27153 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 27154 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 27155 27156 /* set the transfer length */ 27157 nblocks = mode2->cdread_buflen / 2336; 27158 cdb[4] = (uchar_t)nblocks & 0xFF; 27159 27160 /* build command */ 27161 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27162 com->uscsi_cdb = (caddr_t)cdb; 27163 com->uscsi_cdblen = sizeof (cdb); 27164 com->uscsi_bufaddr = mode2->cdread_bufaddr; 27165 com->uscsi_buflen = mode2->cdread_buflen; 27166 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27167 27168 /* 27169 * Issue SCSI command with user space address for read buffer. 27170 * 27171 * This sends the command through main channel in the driver. 27172 * 27173 * Since this is accessed via an IOCTL call, we go through the 27174 * standard path, so that if the device was powered down, then 27175 * it would be 'awakened' to handle the command. 27176 */ 27177 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27178 SD_PATH_STANDARD); 27179 27180 kmem_free(com, sizeof (*com)); 27181 27182 /* Restore the device and soft state target block size */ 27183 if (sr_sector_mode(dev, restore_blksize) != 0) { 27184 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27185 "can't do switch back to mode 1\n"); 27186 /* 27187 * If sd_send_scsi_READ succeeded we still need to report 27188 * an error because we failed to reset the block size 27189 */ 27190 if (rval == 0) { 27191 rval = EIO; 27192 } 27193 } 27194 27195 done: 27196 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 27197 "sd_read_mode2: exit: un:0x%p\n", un); 27198 27199 return (rval); 27200 } 27201 27202 27203 /* 27204 * Function: sr_sector_mode() 27205 * 27206 * Description: This utility function is used by sr_read_mode2 to set the target 27207 * block size based on the user specified size. This is a legacy 27208 * implementation based upon a vendor specific mode page 27209 * 27210 * Arguments: dev - the device 'dev_t' 27211 * data - flag indicating if block size is being set to 2336 or 27212 * 512. 27213 * 27214 * Return Code: the code returned by sd_send_scsi_cmd() 27215 * EFAULT if ddi_copyxxx() fails 27216 * ENXIO if fail ddi_get_soft_state 27217 * EINVAL if data pointer is NULL 27218 */ 27219 27220 static int 27221 sr_sector_mode(dev_t dev, uint32_t blksize) 27222 { 27223 struct sd_lun *un; 27224 uchar_t *sense; 27225 uchar_t *select; 27226 int rval; 27227 sd_ssc_t *ssc; 27228 27229 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27230 (un->un_state == SD_STATE_OFFLINE)) { 27231 return (ENXIO); 27232 } 27233 27234 sense = kmem_zalloc(20, KM_SLEEP); 27235 27236 /* Note: This is a vendor specific mode page (0x81) */ 27237 ssc = sd_ssc_init(un); 27238 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81, 27239 SD_PATH_STANDARD); 27240 sd_ssc_fini(ssc); 27241 if (rval != 0) { 27242 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 27243 "sr_sector_mode: Mode Sense failed\n"); 27244 kmem_free(sense, 20); 27245 return (rval); 27246 } 27247 select = kmem_zalloc(20, KM_SLEEP); 27248 select[3] = 0x08; 27249 select[10] = ((blksize >> 8) & 0xff); 27250 select[11] = (blksize & 0xff); 27251 select[12] = 0x01; 27252 select[13] = 0x06; 27253 select[14] = sense[14]; 27254 select[15] = sense[15]; 27255 if (blksize == SD_MODE2_BLKSIZE) { 27256 select[14] |= 0x01; 27257 } 27258 27259 ssc = sd_ssc_init(un); 27260 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20, 27261 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27262 sd_ssc_fini(ssc); 27263 if (rval != 0) { 27264 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 27265 "sr_sector_mode: Mode Select failed\n"); 27266 } else { 27267 /* 27268 * Only update the softstate block size if we successfully 27269 * changed the device block mode. 27270 */ 27271 mutex_enter(SD_MUTEX(un)); 27272 sd_update_block_info(un, blksize, 0); 27273 mutex_exit(SD_MUTEX(un)); 27274 } 27275 kmem_free(sense, 20); 27276 kmem_free(select, 20); 27277 return (rval); 27278 } 27279 27280 27281 /* 27282 * Function: sr_read_cdda() 27283 * 27284 * Description: This routine is the driver entry point for handling CD-ROM 27285 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 27286 * the target supports CDDA these requests are handled via a vendor 27287 * specific command (0xD8) If the target does not support CDDA 27288 * these requests are handled via the READ CD command (0xBE). 27289 * 27290 * Arguments: dev - the device 'dev_t' 27291 * data - pointer to user provided CD-DA structure specifying 27292 * the track starting address, transfer length, and 27293 * subcode options. 27294 * flag - this argument is a pass through to ddi_copyxxx() 27295 * directly from the mode argument of ioctl(). 27296 * 27297 * Return Code: the code returned by sd_send_scsi_cmd() 27298 * EFAULT if ddi_copyxxx() fails 27299 * ENXIO if fail ddi_get_soft_state 27300 * EINVAL if invalid arguments are provided 27301 * ENOTTY 27302 */ 27303 27304 static int 27305 sr_read_cdda(dev_t dev, caddr_t data, int flag) 27306 { 27307 struct sd_lun *un; 27308 struct uscsi_cmd *com; 27309 struct cdrom_cdda *cdda; 27310 int rval; 27311 size_t buflen; 27312 char cdb[CDB_GROUP5]; 27313 27314 #ifdef _MULTI_DATAMODEL 27315 /* To support ILP32 applications in an LP64 world */ 27316 struct cdrom_cdda32 cdrom_cdda32; 27317 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 27318 #endif /* _MULTI_DATAMODEL */ 27319 27320 if (data == NULL) { 27321 return (EINVAL); 27322 } 27323 27324 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27325 return (ENXIO); 27326 } 27327 27328 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 27329 27330 #ifdef _MULTI_DATAMODEL 27331 switch (ddi_model_convert_from(flag & FMODELS)) { 27332 case DDI_MODEL_ILP32: 27333 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 27334 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27335 "sr_read_cdda: ddi_copyin Failed\n"); 27336 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27337 return (EFAULT); 27338 } 27339 /* Convert the ILP32 uscsi data from the application to LP64 */ 27340 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 27341 break; 27342 case DDI_MODEL_NONE: 27343 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 27344 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27345 "sr_read_cdda: ddi_copyin Failed\n"); 27346 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27347 return (EFAULT); 27348 } 27349 break; 27350 } 27351 #else /* ! _MULTI_DATAMODEL */ 27352 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 27353 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27354 "sr_read_cdda: ddi_copyin Failed\n"); 27355 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27356 return (EFAULT); 27357 } 27358 #endif /* _MULTI_DATAMODEL */ 27359 27360 /* 27361 * Since MMC-2 expects max 3 bytes for length, check if the 27362 * length input is greater than 3 bytes 27363 */ 27364 if ((cdda->cdda_length & 0xFF000000) != 0) { 27365 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 27366 "cdrom transfer length too large: %d (limit %d)\n", 27367 cdda->cdda_length, 0xFFFFFF); 27368 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27369 return (EINVAL); 27370 } 27371 27372 switch (cdda->cdda_subcode) { 27373 case CDROM_DA_NO_SUBCODE: 27374 buflen = CDROM_BLK_2352 * cdda->cdda_length; 27375 break; 27376 case CDROM_DA_SUBQ: 27377 buflen = CDROM_BLK_2368 * cdda->cdda_length; 27378 break; 27379 case CDROM_DA_ALL_SUBCODE: 27380 buflen = CDROM_BLK_2448 * cdda->cdda_length; 27381 break; 27382 case CDROM_DA_SUBCODE_ONLY: 27383 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 27384 break; 27385 default: 27386 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27387 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 27388 cdda->cdda_subcode); 27389 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27390 return (EINVAL); 27391 } 27392 27393 /* Build and send the command */ 27394 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27395 bzero(cdb, CDB_GROUP5); 27396 27397 if (un->un_f_cfg_cdda == TRUE) { 27398 cdb[0] = (char)SCMD_READ_CD; 27399 cdb[1] = 0x04; 27400 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 27401 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 27402 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 27403 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 27404 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 27405 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 27406 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 27407 cdb[9] = 0x10; 27408 switch (cdda->cdda_subcode) { 27409 case CDROM_DA_NO_SUBCODE : 27410 cdb[10] = 0x0; 27411 break; 27412 case CDROM_DA_SUBQ : 27413 cdb[10] = 0x2; 27414 break; 27415 case CDROM_DA_ALL_SUBCODE : 27416 cdb[10] = 0x1; 27417 break; 27418 case CDROM_DA_SUBCODE_ONLY : 27419 /* FALLTHROUGH */ 27420 default : 27421 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27422 kmem_free(com, sizeof (*com)); 27423 return (ENOTTY); 27424 } 27425 } else { 27426 cdb[0] = (char)SCMD_READ_CDDA; 27427 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 27428 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 27429 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 27430 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 27431 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 27432 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 27433 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 27434 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 27435 cdb[10] = cdda->cdda_subcode; 27436 } 27437 27438 com->uscsi_cdb = cdb; 27439 com->uscsi_cdblen = CDB_GROUP5; 27440 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 27441 com->uscsi_buflen = buflen; 27442 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27443 27444 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27445 SD_PATH_STANDARD); 27446 27447 kmem_free(cdda, sizeof (struct cdrom_cdda)); 27448 kmem_free(com, sizeof (*com)); 27449 return (rval); 27450 } 27451 27452 27453 /* 27454 * Function: sr_read_cdxa() 27455 * 27456 * Description: This routine is the driver entry point for handling CD-ROM 27457 * ioctl requests to return CD-XA (Extended Architecture) data. 27458 * (CDROMCDXA). 27459 * 27460 * Arguments: dev - the device 'dev_t' 27461 * data - pointer to user provided CD-XA structure specifying 27462 * the data starting address, transfer length, and format 27463 * flag - this argument is a pass through to ddi_copyxxx() 27464 * directly from the mode argument of ioctl(). 27465 * 27466 * Return Code: the code returned by sd_send_scsi_cmd() 27467 * EFAULT if ddi_copyxxx() fails 27468 * ENXIO if fail ddi_get_soft_state 27469 * EINVAL if data pointer is NULL 27470 */ 27471 27472 static int 27473 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 27474 { 27475 struct sd_lun *un; 27476 struct uscsi_cmd *com; 27477 struct cdrom_cdxa *cdxa; 27478 int rval; 27479 size_t buflen; 27480 char cdb[CDB_GROUP5]; 27481 uchar_t read_flags; 27482 27483 #ifdef _MULTI_DATAMODEL 27484 /* To support ILP32 applications in an LP64 world */ 27485 struct cdrom_cdxa32 cdrom_cdxa32; 27486 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 27487 #endif /* _MULTI_DATAMODEL */ 27488 27489 if (data == NULL) { 27490 return (EINVAL); 27491 } 27492 27493 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27494 return (ENXIO); 27495 } 27496 27497 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 27498 27499 #ifdef _MULTI_DATAMODEL 27500 switch (ddi_model_convert_from(flag & FMODELS)) { 27501 case DDI_MODEL_ILP32: 27502 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 27503 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 27504 return (EFAULT); 27505 } 27506 /* 27507 * Convert the ILP32 uscsi data from the 27508 * application to LP64 for internal use. 27509 */ 27510 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 27511 break; 27512 case DDI_MODEL_NONE: 27513 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 27514 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 27515 return (EFAULT); 27516 } 27517 break; 27518 } 27519 #else /* ! _MULTI_DATAMODEL */ 27520 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 27521 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 27522 return (EFAULT); 27523 } 27524 #endif /* _MULTI_DATAMODEL */ 27525 27526 /* 27527 * Since MMC-2 expects max 3 bytes for length, check if the 27528 * length input is greater than 3 bytes 27529 */ 27530 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 27531 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 27532 "cdrom transfer length too large: %d (limit %d)\n", 27533 cdxa->cdxa_length, 0xFFFFFF); 27534 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 27535 return (EINVAL); 27536 } 27537 27538 switch (cdxa->cdxa_format) { 27539 case CDROM_XA_DATA: 27540 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 27541 read_flags = 0x10; 27542 break; 27543 case CDROM_XA_SECTOR_DATA: 27544 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 27545 read_flags = 0xf8; 27546 break; 27547 case CDROM_XA_DATA_W_ERROR: 27548 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 27549 read_flags = 0xfc; 27550 break; 27551 default: 27552 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27553 "sr_read_cdxa: Format '0x%x' Not Supported\n", 27554 cdxa->cdxa_format); 27555 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 27556 return (EINVAL); 27557 } 27558 27559 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27560 bzero(cdb, CDB_GROUP5); 27561 if (un->un_f_mmc_cap == TRUE) { 27562 cdb[0] = (char)SCMD_READ_CD; 27563 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 27564 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 27565 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 27566 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 27567 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 27568 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 27569 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 27570 cdb[9] = (char)read_flags; 27571 } else { 27572 /* 27573 * Note: A vendor specific command (0xDB) is being used her to 27574 * request a read of all subcodes. 27575 */ 27576 cdb[0] = (char)SCMD_READ_CDXA; 27577 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 27578 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 27579 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 27580 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 27581 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 27582 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 27583 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 27584 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 27585 cdb[10] = cdxa->cdxa_format; 27586 } 27587 com->uscsi_cdb = cdb; 27588 com->uscsi_cdblen = CDB_GROUP5; 27589 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 27590 com->uscsi_buflen = buflen; 27591 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 27592 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 27593 SD_PATH_STANDARD); 27594 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 27595 kmem_free(com, sizeof (*com)); 27596 return (rval); 27597 } 27598 27599 27600 /* 27601 * Function: sr_eject() 27602 * 27603 * Description: This routine is the driver entry point for handling CD-ROM 27604 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 27605 * 27606 * Arguments: dev - the device 'dev_t' 27607 * 27608 * Return Code: the code returned by sd_send_scsi_cmd() 27609 */ 27610 27611 static int 27612 sr_eject(dev_t dev) 27613 { 27614 struct sd_lun *un; 27615 int rval; 27616 sd_ssc_t *ssc; 27617 27618 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27619 (un->un_state == SD_STATE_OFFLINE)) { 27620 return (ENXIO); 27621 } 27622 27623 /* 27624 * To prevent race conditions with the eject 27625 * command, keep track of an eject command as 27626 * it progresses. If we are already handling 27627 * an eject command in the driver for the given 27628 * unit and another request to eject is received 27629 * immediately return EAGAIN so we don't lose 27630 * the command if the current eject command fails. 27631 */ 27632 mutex_enter(SD_MUTEX(un)); 27633 if (un->un_f_ejecting == TRUE) { 27634 mutex_exit(SD_MUTEX(un)); 27635 return (EAGAIN); 27636 } 27637 un->un_f_ejecting = TRUE; 27638 mutex_exit(SD_MUTEX(un)); 27639 27640 ssc = sd_ssc_init(un); 27641 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW, 27642 SD_PATH_STANDARD); 27643 sd_ssc_fini(ssc); 27644 27645 if (rval != 0) { 27646 mutex_enter(SD_MUTEX(un)); 27647 un->un_f_ejecting = FALSE; 27648 mutex_exit(SD_MUTEX(un)); 27649 return (rval); 27650 } 27651 27652 ssc = sd_ssc_init(un); 27653 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_TARGET_EJECT, 27654 SD_PATH_STANDARD); 27655 sd_ssc_fini(ssc); 27656 27657 if (rval == 0) { 27658 mutex_enter(SD_MUTEX(un)); 27659 sr_ejected(un); 27660 un->un_mediastate = DKIO_EJECTED; 27661 un->un_f_ejecting = FALSE; 27662 cv_broadcast(&un->un_state_cv); 27663 mutex_exit(SD_MUTEX(un)); 27664 } else { 27665 mutex_enter(SD_MUTEX(un)); 27666 un->un_f_ejecting = FALSE; 27667 mutex_exit(SD_MUTEX(un)); 27668 } 27669 return (rval); 27670 } 27671 27672 27673 /* 27674 * Function: sr_ejected() 27675 * 27676 * Description: This routine updates the soft state structure to invalidate the 27677 * geometry information after the media has been ejected or a 27678 * media eject has been detected. 27679 * 27680 * Arguments: un - driver soft state (unit) structure 27681 */ 27682 27683 static void 27684 sr_ejected(struct sd_lun *un) 27685 { 27686 struct sd_errstats *stp; 27687 27688 ASSERT(un != NULL); 27689 ASSERT(mutex_owned(SD_MUTEX(un))); 27690 27691 un->un_f_blockcount_is_valid = FALSE; 27692 un->un_f_tgt_blocksize_is_valid = FALSE; 27693 mutex_exit(SD_MUTEX(un)); 27694 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 27695 mutex_enter(SD_MUTEX(un)); 27696 27697 if (un->un_errstats != NULL) { 27698 stp = (struct sd_errstats *)un->un_errstats->ks_data; 27699 stp->sd_capacity.value.ui64 = 0; 27700 } 27701 } 27702 27703 27704 /* 27705 * Function: sr_check_wp() 27706 * 27707 * Description: This routine checks the write protection of a removable 27708 * media disk and hotpluggable devices via the write protect bit of 27709 * the Mode Page Header device specific field. Some devices choke 27710 * on unsupported mode page. In order to workaround this issue, 27711 * this routine has been implemented to use 0x3f mode page(request 27712 * for all pages) for all device types. 27713 * 27714 * Arguments: dev - the device 'dev_t' 27715 * 27716 * Return Code: int indicating if the device is write protected (1) or not (0) 27717 * 27718 * Context: Kernel thread. 27719 * 27720 */ 27721 27722 static int 27723 sr_check_wp(dev_t dev) 27724 { 27725 struct sd_lun *un; 27726 uchar_t device_specific; 27727 uchar_t *sense; 27728 int hdrlen; 27729 int rval = FALSE; 27730 int status; 27731 sd_ssc_t *ssc; 27732 27733 /* 27734 * Note: The return codes for this routine should be reworked to 27735 * properly handle the case of a NULL softstate. 27736 */ 27737 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27738 return (FALSE); 27739 } 27740 27741 if (un->un_f_cfg_is_atapi == TRUE) { 27742 /* 27743 * The mode page contents are not required; set the allocation 27744 * length for the mode page header only 27745 */ 27746 hdrlen = MODE_HEADER_LENGTH_GRP2; 27747 sense = kmem_zalloc(hdrlen, KM_SLEEP); 27748 ssc = sd_ssc_init(un); 27749 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen, 27750 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 27751 sd_ssc_fini(ssc); 27752 if (status != 0) 27753 goto err_exit; 27754 device_specific = 27755 ((struct mode_header_grp2 *)sense)->device_specific; 27756 } else { 27757 hdrlen = MODE_HEADER_LENGTH; 27758 sense = kmem_zalloc(hdrlen, KM_SLEEP); 27759 ssc = sd_ssc_init(un); 27760 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen, 27761 MODEPAGE_ALLPAGES, SD_PATH_STANDARD); 27762 sd_ssc_fini(ssc); 27763 if (status != 0) 27764 goto err_exit; 27765 device_specific = 27766 ((struct mode_header *)sense)->device_specific; 27767 } 27768 27769 27770 /* 27771 * Write protect mode sense failed; not all disks 27772 * understand this query. Return FALSE assuming that 27773 * these devices are not writable. 27774 */ 27775 if (device_specific & WRITE_PROTECT) { 27776 rval = TRUE; 27777 } 27778 27779 err_exit: 27780 kmem_free(sense, hdrlen); 27781 return (rval); 27782 } 27783 27784 /* 27785 * Function: sr_volume_ctrl() 27786 * 27787 * Description: This routine is the driver entry point for handling CD-ROM 27788 * audio output volume ioctl requests. (CDROMVOLCTRL) 27789 * 27790 * Arguments: dev - the device 'dev_t' 27791 * data - pointer to user audio volume control structure 27792 * flag - this argument is a pass through to ddi_copyxxx() 27793 * directly from the mode argument of ioctl(). 27794 * 27795 * Return Code: the code returned by sd_send_scsi_cmd() 27796 * EFAULT if ddi_copyxxx() fails 27797 * ENXIO if fail ddi_get_soft_state 27798 * EINVAL if data pointer is NULL 27799 * 27800 */ 27801 27802 static int 27803 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 27804 { 27805 struct sd_lun *un; 27806 struct cdrom_volctrl volume; 27807 struct cdrom_volctrl *vol = &volume; 27808 uchar_t *sense_page; 27809 uchar_t *select_page; 27810 uchar_t *sense; 27811 uchar_t *select; 27812 int sense_buflen; 27813 int select_buflen; 27814 int rval; 27815 sd_ssc_t *ssc; 27816 27817 if (data == NULL) { 27818 return (EINVAL); 27819 } 27820 27821 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27822 (un->un_state == SD_STATE_OFFLINE)) { 27823 return (ENXIO); 27824 } 27825 27826 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 27827 return (EFAULT); 27828 } 27829 27830 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 27831 struct mode_header_grp2 *sense_mhp; 27832 struct mode_header_grp2 *select_mhp; 27833 int bd_len; 27834 27835 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 27836 select_buflen = MODE_HEADER_LENGTH_GRP2 + 27837 MODEPAGE_AUDIO_CTRL_LEN; 27838 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 27839 select = kmem_zalloc(select_buflen, KM_SLEEP); 27840 ssc = sd_ssc_init(un); 27841 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, 27842 sense_buflen, MODEPAGE_AUDIO_CTRL, 27843 SD_PATH_STANDARD); 27844 sd_ssc_fini(ssc); 27845 27846 if (rval != 0) { 27847 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 27848 "sr_volume_ctrl: Mode Sense Failed\n"); 27849 kmem_free(sense, sense_buflen); 27850 kmem_free(select, select_buflen); 27851 return (rval); 27852 } 27853 sense_mhp = (struct mode_header_grp2 *)sense; 27854 select_mhp = (struct mode_header_grp2 *)select; 27855 bd_len = (sense_mhp->bdesc_length_hi << 8) | 27856 sense_mhp->bdesc_length_lo; 27857 if (bd_len > MODE_BLK_DESC_LENGTH) { 27858 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27859 "sr_volume_ctrl: Mode Sense returned invalid " 27860 "block descriptor length\n"); 27861 kmem_free(sense, sense_buflen); 27862 kmem_free(select, select_buflen); 27863 return (EIO); 27864 } 27865 sense_page = (uchar_t *) 27866 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27867 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 27868 select_mhp->length_msb = 0; 27869 select_mhp->length_lsb = 0; 27870 select_mhp->bdesc_length_hi = 0; 27871 select_mhp->bdesc_length_lo = 0; 27872 } else { 27873 struct mode_header *sense_mhp, *select_mhp; 27874 27875 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 27876 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 27877 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 27878 select = kmem_zalloc(select_buflen, KM_SLEEP); 27879 ssc = sd_ssc_init(un); 27880 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 27881 sense_buflen, MODEPAGE_AUDIO_CTRL, 27882 SD_PATH_STANDARD); 27883 sd_ssc_fini(ssc); 27884 27885 if (rval != 0) { 27886 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27887 "sr_volume_ctrl: Mode Sense Failed\n"); 27888 kmem_free(sense, sense_buflen); 27889 kmem_free(select, select_buflen); 27890 return (rval); 27891 } 27892 sense_mhp = (struct mode_header *)sense; 27893 select_mhp = (struct mode_header *)select; 27894 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 27895 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27896 "sr_volume_ctrl: Mode Sense returned invalid " 27897 "block descriptor length\n"); 27898 kmem_free(sense, sense_buflen); 27899 kmem_free(select, select_buflen); 27900 return (EIO); 27901 } 27902 sense_page = (uchar_t *) 27903 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27904 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 27905 select_mhp->length = 0; 27906 select_mhp->bdesc_length = 0; 27907 } 27908 /* 27909 * Note: An audio control data structure could be created and overlayed 27910 * on the following in place of the array indexing method implemented. 27911 */ 27912 27913 /* Build the select data for the user volume data */ 27914 select_page[0] = MODEPAGE_AUDIO_CTRL; 27915 select_page[1] = 0xE; 27916 /* Set the immediate bit */ 27917 select_page[2] = 0x04; 27918 /* Zero out reserved fields */ 27919 select_page[3] = 0x00; 27920 select_page[4] = 0x00; 27921 /* Return sense data for fields not to be modified */ 27922 select_page[5] = sense_page[5]; 27923 select_page[6] = sense_page[6]; 27924 select_page[7] = sense_page[7]; 27925 /* Set the user specified volume levels for channel 0 and 1 */ 27926 select_page[8] = 0x01; 27927 select_page[9] = vol->channel0; 27928 select_page[10] = 0x02; 27929 select_page[11] = vol->channel1; 27930 /* Channel 2 and 3 are currently unsupported so return the sense data */ 27931 select_page[12] = sense_page[12]; 27932 select_page[13] = sense_page[13]; 27933 select_page[14] = sense_page[14]; 27934 select_page[15] = sense_page[15]; 27935 27936 ssc = sd_ssc_init(un); 27937 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 27938 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select, 27939 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27940 } else { 27941 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 27942 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27943 } 27944 sd_ssc_fini(ssc); 27945 27946 kmem_free(sense, sense_buflen); 27947 kmem_free(select, select_buflen); 27948 return (rval); 27949 } 27950 27951 27952 /* 27953 * Function: sr_read_sony_session_offset() 27954 * 27955 * Description: This routine is the driver entry point for handling CD-ROM 27956 * ioctl requests for session offset information. (CDROMREADOFFSET) 27957 * The address of the first track in the last session of a 27958 * multi-session CD-ROM is returned 27959 * 27960 * Note: This routine uses a vendor specific key value in the 27961 * command control field without implementing any vendor check here 27962 * or in the ioctl routine. 27963 * 27964 * Arguments: dev - the device 'dev_t' 27965 * data - pointer to an int to hold the requested address 27966 * flag - this argument is a pass through to ddi_copyxxx() 27967 * directly from the mode argument of ioctl(). 27968 * 27969 * Return Code: the code returned by sd_send_scsi_cmd() 27970 * EFAULT if ddi_copyxxx() fails 27971 * ENXIO if fail ddi_get_soft_state 27972 * EINVAL if data pointer is NULL 27973 */ 27974 27975 static int 27976 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 27977 { 27978 struct sd_lun *un; 27979 struct uscsi_cmd *com; 27980 caddr_t buffer; 27981 char cdb[CDB_GROUP1]; 27982 int session_offset = 0; 27983 int rval; 27984 27985 if (data == NULL) { 27986 return (EINVAL); 27987 } 27988 27989 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 27990 (un->un_state == SD_STATE_OFFLINE)) { 27991 return (ENXIO); 27992 } 27993 27994 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 27995 bzero(cdb, CDB_GROUP1); 27996 cdb[0] = SCMD_READ_TOC; 27997 /* 27998 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 27999 * (4 byte TOC response header + 8 byte response data) 28000 */ 28001 cdb[8] = SONY_SESSION_OFFSET_LEN; 28002 /* Byte 9 is the control byte. A vendor specific value is used */ 28003 cdb[9] = SONY_SESSION_OFFSET_KEY; 28004 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28005 com->uscsi_cdb = cdb; 28006 com->uscsi_cdblen = CDB_GROUP1; 28007 com->uscsi_bufaddr = buffer; 28008 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 28009 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28010 28011 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 28012 SD_PATH_STANDARD); 28013 if (rval != 0) { 28014 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 28015 kmem_free(com, sizeof (*com)); 28016 return (rval); 28017 } 28018 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 28019 session_offset = 28020 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28021 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28022 /* 28023 * Offset returned offset in current lbasize block's. Convert to 28024 * 2k block's to return to the user 28025 */ 28026 if (un->un_tgt_blocksize == CDROM_BLK_512) { 28027 session_offset >>= 2; 28028 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 28029 session_offset >>= 1; 28030 } 28031 } 28032 28033 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 28034 rval = EFAULT; 28035 } 28036 28037 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 28038 kmem_free(com, sizeof (*com)); 28039 return (rval); 28040 } 28041 28042 28043 /* 28044 * Function: sd_wm_cache_constructor() 28045 * 28046 * Description: Cache Constructor for the wmap cache for the read/modify/write 28047 * devices. 28048 * 28049 * Arguments: wm - A pointer to the sd_w_map to be initialized. 28050 * un - sd_lun structure for the device. 28051 * flag - the km flags passed to constructor 28052 * 28053 * Return Code: 0 on success. 28054 * -1 on failure. 28055 */ 28056 28057 /*ARGSUSED*/ 28058 static int 28059 sd_wm_cache_constructor(void *wm, void *un, int flags) 28060 { 28061 bzero(wm, sizeof (struct sd_w_map)); 28062 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 28063 return (0); 28064 } 28065 28066 28067 /* 28068 * Function: sd_wm_cache_destructor() 28069 * 28070 * Description: Cache destructor for the wmap cache for the read/modify/write 28071 * devices. 28072 * 28073 * Arguments: wm - A pointer to the sd_w_map to be initialized. 28074 * un - sd_lun structure for the device. 28075 */ 28076 /*ARGSUSED*/ 28077 static void 28078 sd_wm_cache_destructor(void *wm, void *un) 28079 { 28080 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 28081 } 28082 28083 28084 /* 28085 * Function: sd_range_lock() 28086 * 28087 * Description: Lock the range of blocks specified as parameter to ensure 28088 * that read, modify write is atomic and no other i/o writes 28089 * to the same location. The range is specified in terms 28090 * of start and end blocks. Block numbers are the actual 28091 * media block numbers and not system. 28092 * 28093 * Arguments: un - sd_lun structure for the device. 28094 * startb - The starting block number 28095 * endb - The end block number 28096 * typ - type of i/o - simple/read_modify_write 28097 * 28098 * Return Code: wm - pointer to the wmap structure. 28099 * 28100 * Context: This routine can sleep. 28101 */ 28102 28103 static struct sd_w_map * 28104 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 28105 { 28106 struct sd_w_map *wmp = NULL; 28107 struct sd_w_map *sl_wmp = NULL; 28108 struct sd_w_map *tmp_wmp; 28109 wm_state state = SD_WM_CHK_LIST; 28110 28111 28112 ASSERT(un != NULL); 28113 ASSERT(!mutex_owned(SD_MUTEX(un))); 28114 28115 mutex_enter(SD_MUTEX(un)); 28116 28117 while (state != SD_WM_DONE) { 28118 28119 switch (state) { 28120 case SD_WM_CHK_LIST: 28121 /* 28122 * This is the starting state. Check the wmap list 28123 * to see if the range is currently available. 28124 */ 28125 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 28126 /* 28127 * If this is a simple write and no rmw 28128 * i/o is pending then try to lock the 28129 * range as the range should be available. 28130 */ 28131 state = SD_WM_LOCK_RANGE; 28132 } else { 28133 tmp_wmp = sd_get_range(un, startb, endb); 28134 if (tmp_wmp != NULL) { 28135 if ((wmp != NULL) && ONLIST(un, wmp)) { 28136 /* 28137 * Should not keep onlist wmps 28138 * while waiting this macro 28139 * will also do wmp = NULL; 28140 */ 28141 FREE_ONLIST_WMAP(un, wmp); 28142 } 28143 /* 28144 * sl_wmp is the wmap on which wait 28145 * is done, since the tmp_wmp points 28146 * to the inuse wmap, set sl_wmp to 28147 * tmp_wmp and change the state to sleep 28148 */ 28149 sl_wmp = tmp_wmp; 28150 state = SD_WM_WAIT_MAP; 28151 } else { 28152 state = SD_WM_LOCK_RANGE; 28153 } 28154 28155 } 28156 break; 28157 28158 case SD_WM_LOCK_RANGE: 28159 ASSERT(un->un_wm_cache); 28160 /* 28161 * The range need to be locked, try to get a wmap. 28162 * First attempt it with NO_SLEEP, want to avoid a sleep 28163 * if possible as we will have to release the sd mutex 28164 * if we have to sleep. 28165 */ 28166 if (wmp == NULL) 28167 wmp = kmem_cache_alloc(un->un_wm_cache, 28168 KM_NOSLEEP); 28169 if (wmp == NULL) { 28170 mutex_exit(SD_MUTEX(un)); 28171 _NOTE(DATA_READABLE_WITHOUT_LOCK 28172 (sd_lun::un_wm_cache)) 28173 wmp = kmem_cache_alloc(un->un_wm_cache, 28174 KM_SLEEP); 28175 mutex_enter(SD_MUTEX(un)); 28176 /* 28177 * we released the mutex so recheck and go to 28178 * check list state. 28179 */ 28180 state = SD_WM_CHK_LIST; 28181 } else { 28182 /* 28183 * We exit out of state machine since we 28184 * have the wmap. Do the housekeeping first. 28185 * place the wmap on the wmap list if it is not 28186 * on it already and then set the state to done. 28187 */ 28188 wmp->wm_start = startb; 28189 wmp->wm_end = endb; 28190 wmp->wm_flags = typ | SD_WM_BUSY; 28191 if (typ & SD_WTYPE_RMW) { 28192 un->un_rmw_count++; 28193 } 28194 /* 28195 * If not already on the list then link 28196 */ 28197 if (!ONLIST(un, wmp)) { 28198 wmp->wm_next = un->un_wm; 28199 wmp->wm_prev = NULL; 28200 if (wmp->wm_next) 28201 wmp->wm_next->wm_prev = wmp; 28202 un->un_wm = wmp; 28203 } 28204 state = SD_WM_DONE; 28205 } 28206 break; 28207 28208 case SD_WM_WAIT_MAP: 28209 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 28210 /* 28211 * Wait is done on sl_wmp, which is set in the 28212 * check_list state. 28213 */ 28214 sl_wmp->wm_wanted_count++; 28215 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 28216 sl_wmp->wm_wanted_count--; 28217 /* 28218 * We can reuse the memory from the completed sl_wmp 28219 * lock range for our new lock, but only if noone is 28220 * waiting for it. 28221 */ 28222 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 28223 if (sl_wmp->wm_wanted_count == 0) { 28224 if (wmp != NULL) 28225 CHK_N_FREEWMP(un, wmp); 28226 wmp = sl_wmp; 28227 } 28228 sl_wmp = NULL; 28229 /* 28230 * After waking up, need to recheck for availability of 28231 * range. 28232 */ 28233 state = SD_WM_CHK_LIST; 28234 break; 28235 28236 default: 28237 panic("sd_range_lock: " 28238 "Unknown state %d in sd_range_lock", state); 28239 /*NOTREACHED*/ 28240 } /* switch(state) */ 28241 28242 } /* while(state != SD_WM_DONE) */ 28243 28244 mutex_exit(SD_MUTEX(un)); 28245 28246 ASSERT(wmp != NULL); 28247 28248 return (wmp); 28249 } 28250 28251 28252 /* 28253 * Function: sd_get_range() 28254 * 28255 * Description: Find if there any overlapping I/O to this one 28256 * Returns the write-map of 1st such I/O, NULL otherwise. 28257 * 28258 * Arguments: un - sd_lun structure for the device. 28259 * startb - The starting block number 28260 * endb - The end block number 28261 * 28262 * Return Code: wm - pointer to the wmap structure. 28263 */ 28264 28265 static struct sd_w_map * 28266 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 28267 { 28268 struct sd_w_map *wmp; 28269 28270 ASSERT(un != NULL); 28271 28272 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 28273 if (!(wmp->wm_flags & SD_WM_BUSY)) { 28274 continue; 28275 } 28276 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 28277 break; 28278 } 28279 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 28280 break; 28281 } 28282 } 28283 28284 return (wmp); 28285 } 28286 28287 28288 /* 28289 * Function: sd_free_inlist_wmap() 28290 * 28291 * Description: Unlink and free a write map struct. 28292 * 28293 * Arguments: un - sd_lun structure for the device. 28294 * wmp - sd_w_map which needs to be unlinked. 28295 */ 28296 28297 static void 28298 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 28299 { 28300 ASSERT(un != NULL); 28301 28302 if (un->un_wm == wmp) { 28303 un->un_wm = wmp->wm_next; 28304 } else { 28305 wmp->wm_prev->wm_next = wmp->wm_next; 28306 } 28307 28308 if (wmp->wm_next) { 28309 wmp->wm_next->wm_prev = wmp->wm_prev; 28310 } 28311 28312 wmp->wm_next = wmp->wm_prev = NULL; 28313 28314 kmem_cache_free(un->un_wm_cache, wmp); 28315 } 28316 28317 28318 /* 28319 * Function: sd_range_unlock() 28320 * 28321 * Description: Unlock the range locked by wm. 28322 * Free write map if nobody else is waiting on it. 28323 * 28324 * Arguments: un - sd_lun structure for the device. 28325 * wmp - sd_w_map which needs to be unlinked. 28326 */ 28327 28328 static void 28329 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 28330 { 28331 ASSERT(un != NULL); 28332 ASSERT(wm != NULL); 28333 ASSERT(!mutex_owned(SD_MUTEX(un))); 28334 28335 mutex_enter(SD_MUTEX(un)); 28336 28337 if (wm->wm_flags & SD_WTYPE_RMW) { 28338 un->un_rmw_count--; 28339 } 28340 28341 if (wm->wm_wanted_count) { 28342 wm->wm_flags = 0; 28343 /* 28344 * Broadcast that the wmap is available now. 28345 */ 28346 cv_broadcast(&wm->wm_avail); 28347 } else { 28348 /* 28349 * If no one is waiting on the map, it should be free'ed. 28350 */ 28351 sd_free_inlist_wmap(un, wm); 28352 } 28353 28354 mutex_exit(SD_MUTEX(un)); 28355 } 28356 28357 28358 /* 28359 * Function: sd_read_modify_write_task 28360 * 28361 * Description: Called from a taskq thread to initiate the write phase of 28362 * a read-modify-write request. This is used for targets where 28363 * un->un_sys_blocksize != un->un_tgt_blocksize. 28364 * 28365 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 28366 * 28367 * Context: Called under taskq thread context. 28368 */ 28369 28370 static void 28371 sd_read_modify_write_task(void *arg) 28372 { 28373 struct sd_mapblocksize_info *bsp; 28374 struct buf *bp; 28375 struct sd_xbuf *xp; 28376 struct sd_lun *un; 28377 28378 bp = arg; /* The bp is given in arg */ 28379 ASSERT(bp != NULL); 28380 28381 /* Get the pointer to the layer-private data struct */ 28382 xp = SD_GET_XBUF(bp); 28383 ASSERT(xp != NULL); 28384 bsp = xp->xb_private; 28385 ASSERT(bsp != NULL); 28386 28387 un = SD_GET_UN(bp); 28388 ASSERT(un != NULL); 28389 ASSERT(!mutex_owned(SD_MUTEX(un))); 28390 28391 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 28392 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 28393 28394 /* 28395 * This is the write phase of a read-modify-write request, called 28396 * under the context of a taskq thread in response to the completion 28397 * of the read portion of the rmw request completing under interrupt 28398 * context. The write request must be sent from here down the iostart 28399 * chain as if it were being sent from sd_mapblocksize_iostart(), so 28400 * we use the layer index saved in the layer-private data area. 28401 */ 28402 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 28403 28404 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 28405 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 28406 } 28407 28408 28409 /* 28410 * Function: sddump_do_read_of_rmw() 28411 * 28412 * Description: This routine will be called from sddump, If sddump is called 28413 * with an I/O which not aligned on device blocksize boundary 28414 * then the write has to be converted to read-modify-write. 28415 * Do the read part here in order to keep sddump simple. 28416 * Note - That the sd_mutex is held across the call to this 28417 * routine. 28418 * 28419 * Arguments: un - sd_lun 28420 * blkno - block number in terms of media block size. 28421 * nblk - number of blocks. 28422 * bpp - pointer to pointer to the buf structure. On return 28423 * from this function, *bpp points to the valid buffer 28424 * to which the write has to be done. 28425 * 28426 * Return Code: 0 for success or errno-type return code 28427 */ 28428 28429 static int 28430 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 28431 struct buf **bpp) 28432 { 28433 int err; 28434 int i; 28435 int rval; 28436 struct buf *bp; 28437 struct scsi_pkt *pkt = NULL; 28438 uint32_t target_blocksize; 28439 28440 ASSERT(un != NULL); 28441 ASSERT(mutex_owned(SD_MUTEX(un))); 28442 28443 target_blocksize = un->un_tgt_blocksize; 28444 28445 mutex_exit(SD_MUTEX(un)); 28446 28447 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 28448 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 28449 if (bp == NULL) { 28450 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28451 "no resources for dumping; giving up"); 28452 err = ENOMEM; 28453 goto done; 28454 } 28455 28456 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 28457 blkno, nblk); 28458 if (rval != 0) { 28459 scsi_free_consistent_buf(bp); 28460 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28461 "no resources for dumping; giving up"); 28462 err = ENOMEM; 28463 goto done; 28464 } 28465 28466 pkt->pkt_flags |= FLAG_NOINTR; 28467 28468 err = EIO; 28469 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 28470 28471 /* 28472 * Scsi_poll returns 0 (success) if the command completes and 28473 * the status block is STATUS_GOOD. We should only check 28474 * errors if this condition is not true. Even then we should 28475 * send our own request sense packet only if we have a check 28476 * condition and auto request sense has not been performed by 28477 * the hba. 28478 */ 28479 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 28480 28481 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 28482 err = 0; 28483 break; 28484 } 28485 28486 /* 28487 * Check CMD_DEV_GONE 1st, give up if device is gone, 28488 * no need to read RQS data. 28489 */ 28490 if (pkt->pkt_reason == CMD_DEV_GONE) { 28491 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28492 "Error while dumping state with rmw..." 28493 "Device is gone\n"); 28494 break; 28495 } 28496 28497 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 28498 SD_INFO(SD_LOG_DUMP, un, 28499 "sddump: read failed with CHECK, try # %d\n", i); 28500 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 28501 (void) sd_send_polled_RQS(un); 28502 } 28503 28504 continue; 28505 } 28506 28507 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 28508 int reset_retval = 0; 28509 28510 SD_INFO(SD_LOG_DUMP, un, 28511 "sddump: read failed with BUSY, try # %d\n", i); 28512 28513 if (un->un_f_lun_reset_enabled == TRUE) { 28514 reset_retval = scsi_reset(SD_ADDRESS(un), 28515 RESET_LUN); 28516 } 28517 if (reset_retval == 0) { 28518 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 28519 } 28520 (void) sd_send_polled_RQS(un); 28521 28522 } else { 28523 SD_INFO(SD_LOG_DUMP, un, 28524 "sddump: read failed with 0x%x, try # %d\n", 28525 SD_GET_PKT_STATUS(pkt), i); 28526 mutex_enter(SD_MUTEX(un)); 28527 sd_reset_target(un, pkt); 28528 mutex_exit(SD_MUTEX(un)); 28529 } 28530 28531 /* 28532 * If we are not getting anywhere with lun/target resets, 28533 * let's reset the bus. 28534 */ 28535 if (i > SD_NDUMP_RETRIES/2) { 28536 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 28537 (void) sd_send_polled_RQS(un); 28538 } 28539 28540 } 28541 scsi_destroy_pkt(pkt); 28542 28543 if (err != 0) { 28544 scsi_free_consistent_buf(bp); 28545 *bpp = NULL; 28546 } else { 28547 *bpp = bp; 28548 } 28549 28550 done: 28551 mutex_enter(SD_MUTEX(un)); 28552 return (err); 28553 } 28554 28555 28556 /* 28557 * Function: sd_failfast_flushq 28558 * 28559 * Description: Take all bp's on the wait queue that have B_FAILFAST set 28560 * in b_flags and move them onto the failfast queue, then kick 28561 * off a thread to return all bp's on the failfast queue to 28562 * their owners with an error set. 28563 * 28564 * Arguments: un - pointer to the soft state struct for the instance. 28565 * 28566 * Context: may execute in interrupt context. 28567 */ 28568 28569 static void 28570 sd_failfast_flushq(struct sd_lun *un) 28571 { 28572 struct buf *bp; 28573 struct buf *next_waitq_bp; 28574 struct buf *prev_waitq_bp = NULL; 28575 28576 ASSERT(un != NULL); 28577 ASSERT(mutex_owned(SD_MUTEX(un))); 28578 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 28579 ASSERT(un->un_failfast_bp == NULL); 28580 28581 SD_TRACE(SD_LOG_IO_FAILFAST, un, 28582 "sd_failfast_flushq: entry: un:0x%p\n", un); 28583 28584 /* 28585 * Check if we should flush all bufs when entering failfast state, or 28586 * just those with B_FAILFAST set. 28587 */ 28588 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 28589 /* 28590 * Move *all* bp's on the wait queue to the failfast flush 28591 * queue, including those that do NOT have B_FAILFAST set. 28592 */ 28593 if (un->un_failfast_headp == NULL) { 28594 ASSERT(un->un_failfast_tailp == NULL); 28595 un->un_failfast_headp = un->un_waitq_headp; 28596 } else { 28597 ASSERT(un->un_failfast_tailp != NULL); 28598 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 28599 } 28600 28601 un->un_failfast_tailp = un->un_waitq_tailp; 28602 28603 /* update kstat for each bp moved out of the waitq */ 28604 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 28605 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 28606 } 28607 28608 /* empty the waitq */ 28609 un->un_waitq_headp = un->un_waitq_tailp = NULL; 28610 28611 } else { 28612 /* 28613 * Go thru the wait queue, pick off all entries with 28614 * B_FAILFAST set, and move these onto the failfast queue. 28615 */ 28616 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 28617 /* 28618 * Save the pointer to the next bp on the wait queue, 28619 * so we get to it on the next iteration of this loop. 28620 */ 28621 next_waitq_bp = bp->av_forw; 28622 28623 /* 28624 * If this bp from the wait queue does NOT have 28625 * B_FAILFAST set, just move on to the next element 28626 * in the wait queue. Note, this is the only place 28627 * where it is correct to set prev_waitq_bp. 28628 */ 28629 if ((bp->b_flags & B_FAILFAST) == 0) { 28630 prev_waitq_bp = bp; 28631 continue; 28632 } 28633 28634 /* 28635 * Remove the bp from the wait queue. 28636 */ 28637 if (bp == un->un_waitq_headp) { 28638 /* The bp is the first element of the waitq. */ 28639 un->un_waitq_headp = next_waitq_bp; 28640 if (un->un_waitq_headp == NULL) { 28641 /* The wait queue is now empty */ 28642 un->un_waitq_tailp = NULL; 28643 } 28644 } else { 28645 /* 28646 * The bp is either somewhere in the middle 28647 * or at the end of the wait queue. 28648 */ 28649 ASSERT(un->un_waitq_headp != NULL); 28650 ASSERT(prev_waitq_bp != NULL); 28651 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 28652 == 0); 28653 if (bp == un->un_waitq_tailp) { 28654 /* bp is the last entry on the waitq. */ 28655 ASSERT(next_waitq_bp == NULL); 28656 un->un_waitq_tailp = prev_waitq_bp; 28657 } 28658 prev_waitq_bp->av_forw = next_waitq_bp; 28659 } 28660 bp->av_forw = NULL; 28661 28662 /* 28663 * update kstat since the bp is moved out of 28664 * the waitq 28665 */ 28666 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 28667 28668 /* 28669 * Now put the bp onto the failfast queue. 28670 */ 28671 if (un->un_failfast_headp == NULL) { 28672 /* failfast queue is currently empty */ 28673 ASSERT(un->un_failfast_tailp == NULL); 28674 un->un_failfast_headp = 28675 un->un_failfast_tailp = bp; 28676 } else { 28677 /* Add the bp to the end of the failfast q */ 28678 ASSERT(un->un_failfast_tailp != NULL); 28679 ASSERT(un->un_failfast_tailp->b_flags & 28680 B_FAILFAST); 28681 un->un_failfast_tailp->av_forw = bp; 28682 un->un_failfast_tailp = bp; 28683 } 28684 } 28685 } 28686 28687 /* 28688 * Now return all bp's on the failfast queue to their owners. 28689 */ 28690 while ((bp = un->un_failfast_headp) != NULL) { 28691 28692 un->un_failfast_headp = bp->av_forw; 28693 if (un->un_failfast_headp == NULL) { 28694 un->un_failfast_tailp = NULL; 28695 } 28696 28697 /* 28698 * We want to return the bp with a failure error code, but 28699 * we do not want a call to sd_start_cmds() to occur here, 28700 * so use sd_return_failed_command_no_restart() instead of 28701 * sd_return_failed_command(). 28702 */ 28703 sd_return_failed_command_no_restart(un, bp, EIO); 28704 } 28705 28706 /* Flush the xbuf queues if required. */ 28707 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 28708 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 28709 } 28710 28711 SD_TRACE(SD_LOG_IO_FAILFAST, un, 28712 "sd_failfast_flushq: exit: un:0x%p\n", un); 28713 } 28714 28715 28716 /* 28717 * Function: sd_failfast_flushq_callback 28718 * 28719 * Description: Return TRUE if the given bp meets the criteria for failfast 28720 * flushing. Used with ddi_xbuf_flushq(9F). 28721 * 28722 * Arguments: bp - ptr to buf struct to be examined. 28723 * 28724 * Context: Any 28725 */ 28726 28727 static int 28728 sd_failfast_flushq_callback(struct buf *bp) 28729 { 28730 /* 28731 * Return TRUE if (1) we want to flush ALL bufs when the failfast 28732 * state is entered; OR (2) the given bp has B_FAILFAST set. 28733 */ 28734 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 28735 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 28736 } 28737 28738 28739 28740 /* 28741 * Function: sd_setup_next_xfer 28742 * 28743 * Description: Prepare next I/O operation using DMA_PARTIAL 28744 * 28745 */ 28746 28747 static int 28748 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 28749 struct scsi_pkt *pkt, struct sd_xbuf *xp) 28750 { 28751 ssize_t num_blks_not_xfered; 28752 daddr_t strt_blk_num; 28753 ssize_t bytes_not_xfered; 28754 int rval; 28755 28756 ASSERT(pkt->pkt_resid == 0); 28757 28758 /* 28759 * Calculate next block number and amount to be transferred. 28760 * 28761 * How much data NOT transfered to the HBA yet. 28762 */ 28763 bytes_not_xfered = xp->xb_dma_resid; 28764 28765 /* 28766 * figure how many blocks NOT transfered to the HBA yet. 28767 */ 28768 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 28769 28770 /* 28771 * set starting block number to the end of what WAS transfered. 28772 */ 28773 strt_blk_num = xp->xb_blkno + 28774 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 28775 28776 /* 28777 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 28778 * will call scsi_initpkt with NULL_FUNC so we do not have to release 28779 * the disk mutex here. 28780 */ 28781 rval = sd_setup_next_rw_pkt(un, pkt, bp, 28782 strt_blk_num, num_blks_not_xfered); 28783 28784 if (rval == 0) { 28785 28786 /* 28787 * Success. 28788 * 28789 * Adjust things if there are still more blocks to be 28790 * transfered. 28791 */ 28792 xp->xb_dma_resid = pkt->pkt_resid; 28793 pkt->pkt_resid = 0; 28794 28795 return (1); 28796 } 28797 28798 /* 28799 * There's really only one possible return value from 28800 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 28801 * returns NULL. 28802 */ 28803 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 28804 28805 bp->b_resid = bp->b_bcount; 28806 bp->b_flags |= B_ERROR; 28807 28808 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28809 "Error setting up next portion of DMA transfer\n"); 28810 28811 return (0); 28812 } 28813 28814 /* 28815 * Function: sd_panic_for_res_conflict 28816 * 28817 * Description: Call panic with a string formatted with "Reservation Conflict" 28818 * and a human readable identifier indicating the SD instance 28819 * that experienced the reservation conflict. 28820 * 28821 * Arguments: un - pointer to the soft state struct for the instance. 28822 * 28823 * Context: may execute in interrupt context. 28824 */ 28825 28826 #define SD_RESV_CONFLICT_FMT_LEN 40 28827 void 28828 sd_panic_for_res_conflict(struct sd_lun *un) 28829 { 28830 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 28831 char path_str[MAXPATHLEN]; 28832 28833 (void) snprintf(panic_str, sizeof (panic_str), 28834 "Reservation Conflict\nDisk: %s", 28835 ddi_pathname(SD_DEVINFO(un), path_str)); 28836 28837 panic(panic_str); 28838 } 28839 28840 /* 28841 * Note: The following sd_faultinjection_ioctl( ) routines implement 28842 * driver support for handling fault injection for error analysis 28843 * causing faults in multiple layers of the driver. 28844 * 28845 */ 28846 28847 #ifdef SD_FAULT_INJECTION 28848 static uint_t sd_fault_injection_on = 0; 28849 28850 /* 28851 * Function: sd_faultinjection_ioctl() 28852 * 28853 * Description: This routine is the driver entry point for handling 28854 * faultinjection ioctls to inject errors into the 28855 * layer model 28856 * 28857 * Arguments: cmd - the ioctl cmd received 28858 * arg - the arguments from user and returns 28859 */ 28860 28861 static void 28862 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 28863 28864 uint_t i = 0; 28865 uint_t rval; 28866 28867 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 28868 28869 mutex_enter(SD_MUTEX(un)); 28870 28871 switch (cmd) { 28872 case SDIOCRUN: 28873 /* Allow pushed faults to be injected */ 28874 SD_INFO(SD_LOG_SDTEST, un, 28875 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 28876 28877 sd_fault_injection_on = 1; 28878 28879 SD_INFO(SD_LOG_IOERR, un, 28880 "sd_faultinjection_ioctl: run finished\n"); 28881 break; 28882 28883 case SDIOCSTART: 28884 /* Start Injection Session */ 28885 SD_INFO(SD_LOG_SDTEST, un, 28886 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 28887 28888 sd_fault_injection_on = 0; 28889 un->sd_injection_mask = 0xFFFFFFFF; 28890 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 28891 un->sd_fi_fifo_pkt[i] = NULL; 28892 un->sd_fi_fifo_xb[i] = NULL; 28893 un->sd_fi_fifo_un[i] = NULL; 28894 un->sd_fi_fifo_arq[i] = NULL; 28895 } 28896 un->sd_fi_fifo_start = 0; 28897 un->sd_fi_fifo_end = 0; 28898 28899 mutex_enter(&(un->un_fi_mutex)); 28900 un->sd_fi_log[0] = '\0'; 28901 un->sd_fi_buf_len = 0; 28902 mutex_exit(&(un->un_fi_mutex)); 28903 28904 SD_INFO(SD_LOG_IOERR, un, 28905 "sd_faultinjection_ioctl: start finished\n"); 28906 break; 28907 28908 case SDIOCSTOP: 28909 /* Stop Injection Session */ 28910 SD_INFO(SD_LOG_SDTEST, un, 28911 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 28912 sd_fault_injection_on = 0; 28913 un->sd_injection_mask = 0x0; 28914 28915 /* Empty stray or unuseds structs from fifo */ 28916 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 28917 if (un->sd_fi_fifo_pkt[i] != NULL) { 28918 kmem_free(un->sd_fi_fifo_pkt[i], 28919 sizeof (struct sd_fi_pkt)); 28920 } 28921 if (un->sd_fi_fifo_xb[i] != NULL) { 28922 kmem_free(un->sd_fi_fifo_xb[i], 28923 sizeof (struct sd_fi_xb)); 28924 } 28925 if (un->sd_fi_fifo_un[i] != NULL) { 28926 kmem_free(un->sd_fi_fifo_un[i], 28927 sizeof (struct sd_fi_un)); 28928 } 28929 if (un->sd_fi_fifo_arq[i] != NULL) { 28930 kmem_free(un->sd_fi_fifo_arq[i], 28931 sizeof (struct sd_fi_arq)); 28932 } 28933 un->sd_fi_fifo_pkt[i] = NULL; 28934 un->sd_fi_fifo_un[i] = NULL; 28935 un->sd_fi_fifo_xb[i] = NULL; 28936 un->sd_fi_fifo_arq[i] = NULL; 28937 } 28938 un->sd_fi_fifo_start = 0; 28939 un->sd_fi_fifo_end = 0; 28940 28941 SD_INFO(SD_LOG_IOERR, un, 28942 "sd_faultinjection_ioctl: stop finished\n"); 28943 break; 28944 28945 case SDIOCINSERTPKT: 28946 /* Store a packet struct to be pushed onto fifo */ 28947 SD_INFO(SD_LOG_SDTEST, un, 28948 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 28949 28950 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 28951 28952 sd_fault_injection_on = 0; 28953 28954 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 28955 if (un->sd_fi_fifo_pkt[i] != NULL) { 28956 kmem_free(un->sd_fi_fifo_pkt[i], 28957 sizeof (struct sd_fi_pkt)); 28958 } 28959 if (arg != NULL) { 28960 un->sd_fi_fifo_pkt[i] = 28961 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 28962 if (un->sd_fi_fifo_pkt[i] == NULL) { 28963 /* Alloc failed don't store anything */ 28964 break; 28965 } 28966 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 28967 sizeof (struct sd_fi_pkt), 0); 28968 if (rval == -1) { 28969 kmem_free(un->sd_fi_fifo_pkt[i], 28970 sizeof (struct sd_fi_pkt)); 28971 un->sd_fi_fifo_pkt[i] = NULL; 28972 } 28973 } else { 28974 SD_INFO(SD_LOG_IOERR, un, 28975 "sd_faultinjection_ioctl: pkt null\n"); 28976 } 28977 break; 28978 28979 case SDIOCINSERTXB: 28980 /* Store a xb struct to be pushed onto fifo */ 28981 SD_INFO(SD_LOG_SDTEST, un, 28982 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 28983 28984 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 28985 28986 sd_fault_injection_on = 0; 28987 28988 if (un->sd_fi_fifo_xb[i] != NULL) { 28989 kmem_free(un->sd_fi_fifo_xb[i], 28990 sizeof (struct sd_fi_xb)); 28991 un->sd_fi_fifo_xb[i] = NULL; 28992 } 28993 if (arg != NULL) { 28994 un->sd_fi_fifo_xb[i] = 28995 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 28996 if (un->sd_fi_fifo_xb[i] == NULL) { 28997 /* Alloc failed don't store anything */ 28998 break; 28999 } 29000 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 29001 sizeof (struct sd_fi_xb), 0); 29002 29003 if (rval == -1) { 29004 kmem_free(un->sd_fi_fifo_xb[i], 29005 sizeof (struct sd_fi_xb)); 29006 un->sd_fi_fifo_xb[i] = NULL; 29007 } 29008 } else { 29009 SD_INFO(SD_LOG_IOERR, un, 29010 "sd_faultinjection_ioctl: xb null\n"); 29011 } 29012 break; 29013 29014 case SDIOCINSERTUN: 29015 /* Store a un struct to be pushed onto fifo */ 29016 SD_INFO(SD_LOG_SDTEST, un, 29017 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 29018 29019 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29020 29021 sd_fault_injection_on = 0; 29022 29023 if (un->sd_fi_fifo_un[i] != NULL) { 29024 kmem_free(un->sd_fi_fifo_un[i], 29025 sizeof (struct sd_fi_un)); 29026 un->sd_fi_fifo_un[i] = NULL; 29027 } 29028 if (arg != NULL) { 29029 un->sd_fi_fifo_un[i] = 29030 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 29031 if (un->sd_fi_fifo_un[i] == NULL) { 29032 /* Alloc failed don't store anything */ 29033 break; 29034 } 29035 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 29036 sizeof (struct sd_fi_un), 0); 29037 if (rval == -1) { 29038 kmem_free(un->sd_fi_fifo_un[i], 29039 sizeof (struct sd_fi_un)); 29040 un->sd_fi_fifo_un[i] = NULL; 29041 } 29042 29043 } else { 29044 SD_INFO(SD_LOG_IOERR, un, 29045 "sd_faultinjection_ioctl: un null\n"); 29046 } 29047 29048 break; 29049 29050 case SDIOCINSERTARQ: 29051 /* Store a arq struct to be pushed onto fifo */ 29052 SD_INFO(SD_LOG_SDTEST, un, 29053 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 29054 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 29055 29056 sd_fault_injection_on = 0; 29057 29058 if (un->sd_fi_fifo_arq[i] != NULL) { 29059 kmem_free(un->sd_fi_fifo_arq[i], 29060 sizeof (struct sd_fi_arq)); 29061 un->sd_fi_fifo_arq[i] = NULL; 29062 } 29063 if (arg != NULL) { 29064 un->sd_fi_fifo_arq[i] = 29065 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 29066 if (un->sd_fi_fifo_arq[i] == NULL) { 29067 /* Alloc failed don't store anything */ 29068 break; 29069 } 29070 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 29071 sizeof (struct sd_fi_arq), 0); 29072 if (rval == -1) { 29073 kmem_free(un->sd_fi_fifo_arq[i], 29074 sizeof (struct sd_fi_arq)); 29075 un->sd_fi_fifo_arq[i] = NULL; 29076 } 29077 29078 } else { 29079 SD_INFO(SD_LOG_IOERR, un, 29080 "sd_faultinjection_ioctl: arq null\n"); 29081 } 29082 29083 break; 29084 29085 case SDIOCPUSH: 29086 /* Push stored xb, pkt, un, and arq onto fifo */ 29087 sd_fault_injection_on = 0; 29088 29089 if (arg != NULL) { 29090 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 29091 if (rval != -1 && 29092 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 29093 un->sd_fi_fifo_end += i; 29094 } 29095 } else { 29096 SD_INFO(SD_LOG_IOERR, un, 29097 "sd_faultinjection_ioctl: push arg null\n"); 29098 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 29099 un->sd_fi_fifo_end++; 29100 } 29101 } 29102 SD_INFO(SD_LOG_IOERR, un, 29103 "sd_faultinjection_ioctl: push to end=%d\n", 29104 un->sd_fi_fifo_end); 29105 break; 29106 29107 case SDIOCRETRIEVE: 29108 /* Return buffer of log from Injection session */ 29109 SD_INFO(SD_LOG_SDTEST, un, 29110 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 29111 29112 sd_fault_injection_on = 0; 29113 29114 mutex_enter(&(un->un_fi_mutex)); 29115 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 29116 un->sd_fi_buf_len+1, 0); 29117 mutex_exit(&(un->un_fi_mutex)); 29118 29119 if (rval == -1) { 29120 /* 29121 * arg is possibly invalid setting 29122 * it to NULL for return 29123 */ 29124 arg = NULL; 29125 } 29126 break; 29127 } 29128 29129 mutex_exit(SD_MUTEX(un)); 29130 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 29131 " exit\n"); 29132 } 29133 29134 29135 /* 29136 * Function: sd_injection_log() 29137 * 29138 * Description: This routine adds buff to the already existing injection log 29139 * for retrieval via faultinjection_ioctl for use in fault 29140 * detection and recovery 29141 * 29142 * Arguments: buf - the string to add to the log 29143 */ 29144 29145 static void 29146 sd_injection_log(char *buf, struct sd_lun *un) 29147 { 29148 uint_t len; 29149 29150 ASSERT(un != NULL); 29151 ASSERT(buf != NULL); 29152 29153 mutex_enter(&(un->un_fi_mutex)); 29154 29155 len = min(strlen(buf), 255); 29156 /* Add logged value to Injection log to be returned later */ 29157 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 29158 uint_t offset = strlen((char *)un->sd_fi_log); 29159 char *destp = (char *)un->sd_fi_log + offset; 29160 int i; 29161 for (i = 0; i < len; i++) { 29162 *destp++ = *buf++; 29163 } 29164 un->sd_fi_buf_len += len; 29165 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 29166 } 29167 29168 mutex_exit(&(un->un_fi_mutex)); 29169 } 29170 29171 29172 /* 29173 * Function: sd_faultinjection() 29174 * 29175 * Description: This routine takes the pkt and changes its 29176 * content based on error injection scenerio. 29177 * 29178 * Arguments: pktp - packet to be changed 29179 */ 29180 29181 static void 29182 sd_faultinjection(struct scsi_pkt *pktp) 29183 { 29184 uint_t i; 29185 struct sd_fi_pkt *fi_pkt; 29186 struct sd_fi_xb *fi_xb; 29187 struct sd_fi_un *fi_un; 29188 struct sd_fi_arq *fi_arq; 29189 struct buf *bp; 29190 struct sd_xbuf *xb; 29191 struct sd_lun *un; 29192 29193 ASSERT(pktp != NULL); 29194 29195 /* pull bp xb and un from pktp */ 29196 bp = (struct buf *)pktp->pkt_private; 29197 xb = SD_GET_XBUF(bp); 29198 un = SD_GET_UN(bp); 29199 29200 ASSERT(un != NULL); 29201 29202 mutex_enter(SD_MUTEX(un)); 29203 29204 SD_TRACE(SD_LOG_SDTEST, un, 29205 "sd_faultinjection: entry Injection from sdintr\n"); 29206 29207 /* if injection is off return */ 29208 if (sd_fault_injection_on == 0 || 29209 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 29210 mutex_exit(SD_MUTEX(un)); 29211 return; 29212 } 29213 29214 SD_INFO(SD_LOG_SDTEST, un, 29215 "sd_faultinjection: is working for copying\n"); 29216 29217 /* take next set off fifo */ 29218 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 29219 29220 fi_pkt = un->sd_fi_fifo_pkt[i]; 29221 fi_xb = un->sd_fi_fifo_xb[i]; 29222 fi_un = un->sd_fi_fifo_un[i]; 29223 fi_arq = un->sd_fi_fifo_arq[i]; 29224 29225 29226 /* set variables accordingly */ 29227 /* set pkt if it was on fifo */ 29228 if (fi_pkt != NULL) { 29229 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 29230 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 29231 if (fi_pkt->pkt_cdbp != 0xff) 29232 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 29233 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 29234 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 29235 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 29236 29237 } 29238 /* set xb if it was on fifo */ 29239 if (fi_xb != NULL) { 29240 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 29241 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 29242 if (fi_xb->xb_retry_count != 0) 29243 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 29244 SD_CONDSET(xb, xb, xb_victim_retry_count, 29245 "xb_victim_retry_count"); 29246 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 29247 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 29248 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 29249 29250 /* copy in block data from sense */ 29251 /* 29252 * if (fi_xb->xb_sense_data[0] != -1) { 29253 * bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 29254 * SENSE_LENGTH); 29255 * } 29256 */ 29257 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH); 29258 29259 /* copy in extended sense codes */ 29260 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 29261 xb, es_code, "es_code"); 29262 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 29263 xb, es_key, "es_key"); 29264 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 29265 xb, es_add_code, "es_add_code"); 29266 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data), 29267 xb, es_qual_code, "es_qual_code"); 29268 struct scsi_extended_sense *esp; 29269 esp = (struct scsi_extended_sense *)xb->xb_sense_data; 29270 esp->es_class = CLASS_EXTENDED_SENSE; 29271 } 29272 29273 /* set un if it was on fifo */ 29274 if (fi_un != NULL) { 29275 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 29276 SD_CONDSET(un, un, un_ctype, "un_ctype"); 29277 SD_CONDSET(un, un, un_reset_retry_count, 29278 "un_reset_retry_count"); 29279 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 29280 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 29281 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 29282 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 29283 "un_f_allow_bus_device_reset"); 29284 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 29285 29286 } 29287 29288 /* copy in auto request sense if it was on fifo */ 29289 if (fi_arq != NULL) { 29290 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 29291 } 29292 29293 /* free structs */ 29294 if (un->sd_fi_fifo_pkt[i] != NULL) { 29295 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 29296 } 29297 if (un->sd_fi_fifo_xb[i] != NULL) { 29298 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 29299 } 29300 if (un->sd_fi_fifo_un[i] != NULL) { 29301 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 29302 } 29303 if (un->sd_fi_fifo_arq[i] != NULL) { 29304 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 29305 } 29306 29307 /* 29308 * kmem_free does not gurantee to set to NULL 29309 * since we uses these to determine if we set 29310 * values or not lets confirm they are always 29311 * NULL after free 29312 */ 29313 un->sd_fi_fifo_pkt[i] = NULL; 29314 un->sd_fi_fifo_un[i] = NULL; 29315 un->sd_fi_fifo_xb[i] = NULL; 29316 un->sd_fi_fifo_arq[i] = NULL; 29317 29318 un->sd_fi_fifo_start++; 29319 29320 mutex_exit(SD_MUTEX(un)); 29321 29322 SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 29323 } 29324 29325 #endif /* SD_FAULT_INJECTION */ 29326 29327 /* 29328 * This routine is invoked in sd_unit_attach(). Before calling it, the 29329 * properties in conf file should be processed already, and "hotpluggable" 29330 * property was processed also. 29331 * 29332 * The sd driver distinguishes 3 different type of devices: removable media, 29333 * non-removable media, and hotpluggable. Below the differences are defined: 29334 * 29335 * 1. Device ID 29336 * 29337 * The device ID of a device is used to identify this device. Refer to 29338 * ddi_devid_register(9F). 29339 * 29340 * For a non-removable media disk device which can provide 0x80 or 0x83 29341 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 29342 * device ID is created to identify this device. For other non-removable 29343 * media devices, a default device ID is created only if this device has 29344 * at least 2 alter cylinders. Otherwise, this device has no devid. 29345 * 29346 * ------------------------------------------------------- 29347 * removable media hotpluggable | Can Have Device ID 29348 * ------------------------------------------------------- 29349 * false false | Yes 29350 * false true | Yes 29351 * true x | No 29352 * ------------------------------------------------------ 29353 * 29354 * 29355 * 2. SCSI group 4 commands 29356 * 29357 * In SCSI specs, only some commands in group 4 command set can use 29358 * 8-byte addresses that can be used to access >2TB storage spaces. 29359 * Other commands have no such capability. Without supporting group4, 29360 * it is impossible to make full use of storage spaces of a disk with 29361 * capacity larger than 2TB. 29362 * 29363 * ----------------------------------------------- 29364 * removable media hotpluggable LP64 | Group 29365 * ----------------------------------------------- 29366 * false false false | 1 29367 * false false true | 4 29368 * false true false | 1 29369 * false true true | 4 29370 * true x x | 5 29371 * ----------------------------------------------- 29372 * 29373 * 29374 * 3. Check for VTOC Label 29375 * 29376 * If a direct-access disk has no EFI label, sd will check if it has a 29377 * valid VTOC label. Now, sd also does that check for removable media 29378 * and hotpluggable devices. 29379 * 29380 * -------------------------------------------------------------- 29381 * Direct-Access removable media hotpluggable | Check Label 29382 * ------------------------------------------------------------- 29383 * false false false | No 29384 * false false true | No 29385 * false true false | Yes 29386 * false true true | Yes 29387 * true x x | Yes 29388 * -------------------------------------------------------------- 29389 * 29390 * 29391 * 4. Building default VTOC label 29392 * 29393 * As section 3 says, sd checks if some kinds of devices have VTOC label. 29394 * If those devices have no valid VTOC label, sd(7d) will attempt to 29395 * create default VTOC for them. Currently sd creates default VTOC label 29396 * for all devices on x86 platform (VTOC_16), but only for removable 29397 * media devices on SPARC (VTOC_8). 29398 * 29399 * ----------------------------------------------------------- 29400 * removable media hotpluggable platform | Default Label 29401 * ----------------------------------------------------------- 29402 * false false sparc | No 29403 * false true x86 | Yes 29404 * false true sparc | Yes 29405 * true x x | Yes 29406 * ---------------------------------------------------------- 29407 * 29408 * 29409 * 5. Supported blocksizes of target devices 29410 * 29411 * Sd supports non-512-byte blocksize for removable media devices only. 29412 * For other devices, only 512-byte blocksize is supported. This may be 29413 * changed in near future because some RAID devices require non-512-byte 29414 * blocksize 29415 * 29416 * ----------------------------------------------------------- 29417 * removable media hotpluggable | non-512-byte blocksize 29418 * ----------------------------------------------------------- 29419 * false false | No 29420 * false true | No 29421 * true x | Yes 29422 * ----------------------------------------------------------- 29423 * 29424 * 29425 * 6. Automatic mount & unmount 29426 * 29427 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 29428 * if a device is removable media device. It return 1 for removable media 29429 * devices, and 0 for others. 29430 * 29431 * The automatic mounting subsystem should distinguish between the types 29432 * of devices and apply automounting policies to each. 29433 * 29434 * 29435 * 7. fdisk partition management 29436 * 29437 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 29438 * just supports fdisk partitions on x86 platform. On sparc platform, sd 29439 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 29440 * fdisk partitions on both x86 and SPARC platform. 29441 * 29442 * ----------------------------------------------------------- 29443 * platform removable media USB/1394 | fdisk supported 29444 * ----------------------------------------------------------- 29445 * x86 X X | true 29446 * ------------------------------------------------------------ 29447 * sparc X X | false 29448 * ------------------------------------------------------------ 29449 * 29450 * 29451 * 8. MBOOT/MBR 29452 * 29453 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 29454 * read/write mboot for removable media devices on sparc platform. 29455 * 29456 * ----------------------------------------------------------- 29457 * platform removable media USB/1394 | mboot supported 29458 * ----------------------------------------------------------- 29459 * x86 X X | true 29460 * ------------------------------------------------------------ 29461 * sparc false false | false 29462 * sparc false true | true 29463 * sparc true false | true 29464 * sparc true true | true 29465 * ------------------------------------------------------------ 29466 * 29467 * 29468 * 9. error handling during opening device 29469 * 29470 * If failed to open a disk device, an errno is returned. For some kinds 29471 * of errors, different errno is returned depending on if this device is 29472 * a removable media device. This brings USB/1394 hard disks in line with 29473 * expected hard disk behavior. It is not expected that this breaks any 29474 * application. 29475 * 29476 * ------------------------------------------------------ 29477 * removable media hotpluggable | errno 29478 * ------------------------------------------------------ 29479 * false false | EIO 29480 * false true | EIO 29481 * true x | ENXIO 29482 * ------------------------------------------------------ 29483 * 29484 * 29485 * 11. ioctls: DKIOCEJECT, CDROMEJECT 29486 * 29487 * These IOCTLs are applicable only to removable media devices. 29488 * 29489 * ----------------------------------------------------------- 29490 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 29491 * ----------------------------------------------------------- 29492 * false false | No 29493 * false true | No 29494 * true x | Yes 29495 * ----------------------------------------------------------- 29496 * 29497 * 29498 * 12. Kstats for partitions 29499 * 29500 * sd creates partition kstat for non-removable media devices. USB and 29501 * Firewire hard disks now have partition kstats 29502 * 29503 * ------------------------------------------------------ 29504 * removable media hotpluggable | kstat 29505 * ------------------------------------------------------ 29506 * false false | Yes 29507 * false true | Yes 29508 * true x | No 29509 * ------------------------------------------------------ 29510 * 29511 * 29512 * 13. Removable media & hotpluggable properties 29513 * 29514 * Sd driver creates a "removable-media" property for removable media 29515 * devices. Parent nexus drivers create a "hotpluggable" property if 29516 * it supports hotplugging. 29517 * 29518 * --------------------------------------------------------------------- 29519 * removable media hotpluggable | "removable-media" " hotpluggable" 29520 * --------------------------------------------------------------------- 29521 * false false | No No 29522 * false true | No Yes 29523 * true false | Yes No 29524 * true true | Yes Yes 29525 * --------------------------------------------------------------------- 29526 * 29527 * 29528 * 14. Power Management 29529 * 29530 * sd only power manages removable media devices or devices that support 29531 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 29532 * 29533 * A parent nexus that supports hotplugging can also set "pm-capable" 29534 * if the disk can be power managed. 29535 * 29536 * ------------------------------------------------------------ 29537 * removable media hotpluggable pm-capable | power manage 29538 * ------------------------------------------------------------ 29539 * false false false | No 29540 * false false true | Yes 29541 * false true false | No 29542 * false true true | Yes 29543 * true x x | Yes 29544 * ------------------------------------------------------------ 29545 * 29546 * USB and firewire hard disks can now be power managed independently 29547 * of the framebuffer 29548 * 29549 * 29550 * 15. Support for USB disks with capacity larger than 1TB 29551 * 29552 * Currently, sd doesn't permit a fixed disk device with capacity 29553 * larger than 1TB to be used in a 32-bit operating system environment. 29554 * However, sd doesn't do that for removable media devices. Instead, it 29555 * assumes that removable media devices cannot have a capacity larger 29556 * than 1TB. Therefore, using those devices on 32-bit system is partially 29557 * supported, which can cause some unexpected results. 29558 * 29559 * --------------------------------------------------------------------- 29560 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 29561 * --------------------------------------------------------------------- 29562 * false false | true | no 29563 * false true | true | no 29564 * true false | true | Yes 29565 * true true | true | Yes 29566 * --------------------------------------------------------------------- 29567 * 29568 * 29569 * 16. Check write-protection at open time 29570 * 29571 * When a removable media device is being opened for writing without NDELAY 29572 * flag, sd will check if this device is writable. If attempting to open 29573 * without NDELAY flag a write-protected device, this operation will abort. 29574 * 29575 * ------------------------------------------------------------ 29576 * removable media USB/1394 | WP Check 29577 * ------------------------------------------------------------ 29578 * false false | No 29579 * false true | No 29580 * true false | Yes 29581 * true true | Yes 29582 * ------------------------------------------------------------ 29583 * 29584 * 29585 * 17. syslog when corrupted VTOC is encountered 29586 * 29587 * Currently, if an invalid VTOC is encountered, sd only print syslog 29588 * for fixed SCSI disks. 29589 * ------------------------------------------------------------ 29590 * removable media USB/1394 | print syslog 29591 * ------------------------------------------------------------ 29592 * false false | Yes 29593 * false true | No 29594 * true false | No 29595 * true true | No 29596 * ------------------------------------------------------------ 29597 */ 29598 static void 29599 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 29600 { 29601 int pm_capable_prop; 29602 29603 ASSERT(un->un_sd); 29604 ASSERT(un->un_sd->sd_inq); 29605 29606 /* 29607 * Enable SYNC CACHE support for all devices. 29608 */ 29609 un->un_f_sync_cache_supported = TRUE; 29610 29611 /* 29612 * Set the sync cache required flag to false. 29613 * This would ensure that there is no SYNC CACHE 29614 * sent when there are no writes 29615 */ 29616 un->un_f_sync_cache_required = FALSE; 29617 29618 if (un->un_sd->sd_inq->inq_rmb) { 29619 /* 29620 * The media of this device is removable. And for this kind 29621 * of devices, it is possible to change medium after opening 29622 * devices. Thus we should support this operation. 29623 */ 29624 un->un_f_has_removable_media = TRUE; 29625 29626 /* 29627 * support non-512-byte blocksize of removable media devices 29628 */ 29629 un->un_f_non_devbsize_supported = TRUE; 29630 29631 /* 29632 * Assume that all removable media devices support DOOR_LOCK 29633 */ 29634 un->un_f_doorlock_supported = TRUE; 29635 29636 /* 29637 * For a removable media device, it is possible to be opened 29638 * with NDELAY flag when there is no media in drive, in this 29639 * case we don't care if device is writable. But if without 29640 * NDELAY flag, we need to check if media is write-protected. 29641 */ 29642 un->un_f_chk_wp_open = TRUE; 29643 29644 /* 29645 * need to start a SCSI watch thread to monitor media state, 29646 * when media is being inserted or ejected, notify syseventd. 29647 */ 29648 un->un_f_monitor_media_state = TRUE; 29649 29650 /* 29651 * Some devices don't support START_STOP_UNIT command. 29652 * Therefore, we'd better check if a device supports it 29653 * before sending it. 29654 */ 29655 un->un_f_check_start_stop = TRUE; 29656 29657 /* 29658 * support eject media ioctl: 29659 * FDEJECT, DKIOCEJECT, CDROMEJECT 29660 */ 29661 un->un_f_eject_media_supported = TRUE; 29662 29663 /* 29664 * Because many removable-media devices don't support 29665 * LOG_SENSE, we couldn't use this command to check if 29666 * a removable media device support power-management. 29667 * We assume that they support power-management via 29668 * START_STOP_UNIT command and can be spun up and down 29669 * without limitations. 29670 */ 29671 un->un_f_pm_supported = TRUE; 29672 29673 /* 29674 * Need to create a zero length (Boolean) property 29675 * removable-media for the removable media devices. 29676 * Note that the return value of the property is not being 29677 * checked, since if unable to create the property 29678 * then do not want the attach to fail altogether. Consistent 29679 * with other property creation in attach. 29680 */ 29681 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 29682 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 29683 29684 } else { 29685 /* 29686 * create device ID for device 29687 */ 29688 un->un_f_devid_supported = TRUE; 29689 29690 /* 29691 * Spin up non-removable-media devices once it is attached 29692 */ 29693 un->un_f_attach_spinup = TRUE; 29694 29695 /* 29696 * According to SCSI specification, Sense data has two kinds of 29697 * format: fixed format, and descriptor format. At present, we 29698 * don't support descriptor format sense data for removable 29699 * media. 29700 */ 29701 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 29702 un->un_f_descr_format_supported = TRUE; 29703 } 29704 29705 /* 29706 * kstats are created only for non-removable media devices. 29707 * 29708 * Set this in sd.conf to 0 in order to disable kstats. The 29709 * default is 1, so they are enabled by default. 29710 */ 29711 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 29712 SD_DEVINFO(un), DDI_PROP_DONTPASS, 29713 "enable-partition-kstats", 1)); 29714 29715 /* 29716 * Check if HBA has set the "pm-capable" property. 29717 * If "pm-capable" exists and is non-zero then we can 29718 * power manage the device without checking the start/stop 29719 * cycle count log sense page. 29720 * 29721 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 29722 * then we should not power manage the device. 29723 * 29724 * If "pm-capable" doesn't exist then pm_capable_prop will 29725 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 29726 * sd will check the start/stop cycle count log sense page 29727 * and power manage the device if the cycle count limit has 29728 * not been exceeded. 29729 */ 29730 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 29731 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 29732 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 29733 un->un_f_log_sense_supported = TRUE; 29734 } else { 29735 /* 29736 * pm-capable property exists. 29737 * 29738 * Convert "TRUE" values for pm_capable_prop to 29739 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 29740 * later. "TRUE" values are any values except 29741 * SD_PM_CAPABLE_FALSE (0) and 29742 * SD_PM_CAPABLE_UNDEFINED (-1) 29743 */ 29744 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 29745 un->un_f_log_sense_supported = FALSE; 29746 } else { 29747 un->un_f_pm_supported = TRUE; 29748 } 29749 29750 SD_INFO(SD_LOG_ATTACH_DETACH, un, 29751 "sd_unit_attach: un:0x%p pm-capable " 29752 "property set to %d.\n", un, un->un_f_pm_supported); 29753 } 29754 } 29755 29756 if (un->un_f_is_hotpluggable) { 29757 29758 /* 29759 * Have to watch hotpluggable devices as well, since 29760 * that's the only way for userland applications to 29761 * detect hot removal while device is busy/mounted. 29762 */ 29763 un->un_f_monitor_media_state = TRUE; 29764 29765 un->un_f_check_start_stop = TRUE; 29766 29767 } 29768 } 29769 29770 /* 29771 * sd_tg_rdwr: 29772 * Provides rdwr access for cmlb via sd_tgops. The start_block is 29773 * in sys block size, req_length in bytes. 29774 * 29775 */ 29776 static int 29777 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 29778 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 29779 { 29780 struct sd_lun *un; 29781 int path_flag = (int)(uintptr_t)tg_cookie; 29782 char *dkl = NULL; 29783 diskaddr_t real_addr = start_block; 29784 diskaddr_t first_byte, end_block; 29785 29786 size_t buffer_size = reqlength; 29787 int rval = 0; 29788 diskaddr_t cap; 29789 uint32_t lbasize; 29790 sd_ssc_t *ssc; 29791 29792 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 29793 if (un == NULL) 29794 return (ENXIO); 29795 29796 if (cmd != TG_READ && cmd != TG_WRITE) 29797 return (EINVAL); 29798 29799 ssc = sd_ssc_init(un); 29800 mutex_enter(SD_MUTEX(un)); 29801 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 29802 mutex_exit(SD_MUTEX(un)); 29803 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 29804 &lbasize, path_flag); 29805 if (rval != 0) 29806 goto done1; 29807 mutex_enter(SD_MUTEX(un)); 29808 sd_update_block_info(un, lbasize, cap); 29809 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 29810 mutex_exit(SD_MUTEX(un)); 29811 rval = EIO; 29812 goto done; 29813 } 29814 } 29815 29816 if (NOT_DEVBSIZE(un)) { 29817 /* 29818 * sys_blocksize != tgt_blocksize, need to re-adjust 29819 * blkno and save the index to beginning of dk_label 29820 */ 29821 first_byte = SD_SYSBLOCKS2BYTES(un, start_block); 29822 real_addr = first_byte / un->un_tgt_blocksize; 29823 29824 end_block = (first_byte + reqlength + 29825 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 29826 29827 /* round up buffer size to multiple of target block size */ 29828 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 29829 29830 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 29831 "label_addr: 0x%x allocation size: 0x%x\n", 29832 real_addr, buffer_size); 29833 29834 if (((first_byte % un->un_tgt_blocksize) != 0) || 29835 (reqlength % un->un_tgt_blocksize) != 0) 29836 /* the request is not aligned */ 29837 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 29838 } 29839 29840 /* 29841 * The MMC standard allows READ CAPACITY to be 29842 * inaccurate by a bounded amount (in the interest of 29843 * response latency). As a result, failed READs are 29844 * commonplace (due to the reading of metadata and not 29845 * data). Depending on the per-Vendor/drive Sense data, 29846 * the failed READ can cause many (unnecessary) retries. 29847 */ 29848 29849 if (ISCD(un) && (cmd == TG_READ) && 29850 (un->un_f_blockcount_is_valid == TRUE) && 29851 ((start_block == (un->un_blockcount - 1))|| 29852 (start_block == (un->un_blockcount - 2)))) { 29853 path_flag = SD_PATH_DIRECT_PRIORITY; 29854 } 29855 29856 mutex_exit(SD_MUTEX(un)); 29857 if (cmd == TG_READ) { 29858 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr, 29859 buffer_size, real_addr, path_flag); 29860 if (dkl != NULL) 29861 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 29862 real_addr), bufaddr, reqlength); 29863 } else { 29864 if (dkl) { 29865 rval = sd_send_scsi_READ(ssc, dkl, buffer_size, 29866 real_addr, path_flag); 29867 if (rval) { 29868 goto done1; 29869 } 29870 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 29871 real_addr), reqlength); 29872 } 29873 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr, 29874 buffer_size, real_addr, path_flag); 29875 } 29876 29877 done1: 29878 if (dkl != NULL) 29879 kmem_free(dkl, buffer_size); 29880 29881 if (rval != 0) { 29882 if (rval == EIO) 29883 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK); 29884 else 29885 sd_ssc_assessment(ssc, SD_FMT_IGNORE); 29886 } 29887 done: 29888 sd_ssc_fini(ssc); 29889 return (rval); 29890 } 29891 29892 29893 static int 29894 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 29895 { 29896 29897 struct sd_lun *un; 29898 diskaddr_t cap; 29899 uint32_t lbasize; 29900 int path_flag = (int)(uintptr_t)tg_cookie; 29901 int ret = 0; 29902 29903 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 29904 if (un == NULL) 29905 return (ENXIO); 29906 29907 switch (cmd) { 29908 case TG_GETPHYGEOM: 29909 case TG_GETVIRTGEOM: 29910 case TG_GETCAPACITY: 29911 case TG_GETBLOCKSIZE: 29912 mutex_enter(SD_MUTEX(un)); 29913 29914 if ((un->un_f_blockcount_is_valid == TRUE) && 29915 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 29916 cap = un->un_blockcount; 29917 lbasize = un->un_tgt_blocksize; 29918 mutex_exit(SD_MUTEX(un)); 29919 } else { 29920 sd_ssc_t *ssc; 29921 mutex_exit(SD_MUTEX(un)); 29922 ssc = sd_ssc_init(un); 29923 ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap, 29924 &lbasize, path_flag); 29925 if (ret != 0) { 29926 if (ret == EIO) 29927 sd_ssc_assessment(ssc, 29928 SD_FMT_STATUS_CHECK); 29929 else 29930 sd_ssc_assessment(ssc, 29931 SD_FMT_IGNORE); 29932 sd_ssc_fini(ssc); 29933 return (ret); 29934 } 29935 sd_ssc_fini(ssc); 29936 mutex_enter(SD_MUTEX(un)); 29937 sd_update_block_info(un, lbasize, cap); 29938 if ((un->un_f_blockcount_is_valid == FALSE) || 29939 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 29940 mutex_exit(SD_MUTEX(un)); 29941 return (EIO); 29942 } 29943 mutex_exit(SD_MUTEX(un)); 29944 } 29945 29946 if (cmd == TG_GETCAPACITY) { 29947 *(diskaddr_t *)arg = cap; 29948 return (0); 29949 } 29950 29951 if (cmd == TG_GETBLOCKSIZE) { 29952 *(uint32_t *)arg = lbasize; 29953 return (0); 29954 } 29955 29956 if (cmd == TG_GETPHYGEOM) 29957 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 29958 cap, lbasize, path_flag); 29959 else 29960 /* TG_GETVIRTGEOM */ 29961 ret = sd_get_virtual_geometry(un, 29962 (cmlb_geom_t *)arg, cap, lbasize); 29963 29964 return (ret); 29965 29966 case TG_GETATTR: 29967 mutex_enter(SD_MUTEX(un)); 29968 ((tg_attribute_t *)arg)->media_is_writable = 29969 un->un_f_mmc_writable_media; 29970 mutex_exit(SD_MUTEX(un)); 29971 return (0); 29972 default: 29973 return (ENOTTY); 29974 29975 } 29976 } 29977 29978 /* 29979 * Function: sd_ssc_ereport_post 29980 * 29981 * Description: Will be called when SD driver need to post an ereport. 29982 * 29983 * Context: Kernel thread or interrupt context. 29984 */ 29985 static void 29986 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess) 29987 { 29988 int uscsi_path_instance = 0; 29989 uchar_t uscsi_pkt_reason; 29990 uint32_t uscsi_pkt_state; 29991 uint32_t uscsi_pkt_statistics; 29992 uint64_t uscsi_ena; 29993 uchar_t op_code; 29994 uint8_t *sensep; 29995 union scsi_cdb *cdbp; 29996 uint_t cdblen = 0; 29997 uint_t senlen = 0; 29998 struct sd_lun *un; 29999 dev_info_t *dip; 30000 char *devid; 30001 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON | 30002 SSC_FLAGS_INVALID_STATUS | 30003 SSC_FLAGS_INVALID_SENSE | 30004 SSC_FLAGS_INVALID_DATA; 30005 char assessment[16]; 30006 30007 ASSERT(ssc != NULL); 30008 ASSERT(ssc->ssc_uscsi_cmd != NULL); 30009 ASSERT(ssc->ssc_uscsi_info != NULL); 30010 30011 un = ssc->ssc_un; 30012 ASSERT(un != NULL); 30013 30014 dip = un->un_sd->sd_dev; 30015 30016 /* 30017 * Get the devid: 30018 * devid will only be passed to non-transport error reports. 30019 */ 30020 devid = DEVI(dip)->devi_devid_str; 30021 30022 /* 30023 * If we are syncing or dumping, the command will not be executed 30024 * so we bypass this situation. 30025 */ 30026 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 30027 (un->un_state == SD_STATE_DUMPING)) 30028 return; 30029 30030 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason; 30031 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance; 30032 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state; 30033 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics; 30034 uscsi_ena = ssc->ssc_uscsi_info->ui_ena; 30035 30036 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf; 30037 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb; 30038 30039 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */ 30040 if (cdbp == NULL) { 30041 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30042 "sd_ssc_ereport_post meet empty cdb\n"); 30043 return; 30044 } 30045 30046 op_code = cdbp->scc_cmd; 30047 30048 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen; 30049 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen - 30050 ssc->ssc_uscsi_cmd->uscsi_rqresid); 30051 30052 if (senlen > 0) 30053 ASSERT(sensep != NULL); 30054 30055 /* 30056 * Initialize drv_assess to corresponding values. 30057 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending 30058 * on the sense-key returned back. 30059 */ 30060 switch (drv_assess) { 30061 case SD_FM_DRV_RECOVERY: 30062 (void) sprintf(assessment, "%s", "recovered"); 30063 break; 30064 case SD_FM_DRV_RETRY: 30065 (void) sprintf(assessment, "%s", "retry"); 30066 break; 30067 case SD_FM_DRV_NOTICE: 30068 (void) sprintf(assessment, "%s", "info"); 30069 break; 30070 case SD_FM_DRV_FATAL: 30071 default: 30072 (void) sprintf(assessment, "%s", "unknown"); 30073 } 30074 /* 30075 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered 30076 * command, we will post ereport.io.scsi.cmd.disk.recovered. 30077 * driver-assessment will always be "recovered" here. 30078 */ 30079 if (drv_assess == SD_FM_DRV_RECOVERY) { 30080 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 30081 "cmd.disk.recovered", uscsi_ena, devid, DDI_NOSLEEP, 30082 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 30083 "driver-assessment", DATA_TYPE_STRING, assessment, 30084 "op-code", DATA_TYPE_UINT8, op_code, 30085 "cdb", DATA_TYPE_UINT8_ARRAY, 30086 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 30087 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 30088 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 30089 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 30090 NULL); 30091 return; 30092 } 30093 30094 /* 30095 * If there is un-expected/un-decodable data, we should post 30096 * ereport.io.scsi.cmd.disk.dev.uderr. 30097 * driver-assessment will be set based on parameter drv_assess. 30098 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back. 30099 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered. 30100 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered. 30101 * SSC_FLAGS_INVALID_DATA - invalid data sent back. 30102 */ 30103 if (ssc->ssc_flags & ssc_invalid_flags) { 30104 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) { 30105 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 30106 "cmd.disk.dev.uderr", uscsi_ena, devid, DDI_NOSLEEP, 30107 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 30108 "driver-assessment", DATA_TYPE_STRING, 30109 drv_assess == SD_FM_DRV_FATAL ? 30110 "fail" : assessment, 30111 "op-code", DATA_TYPE_UINT8, op_code, 30112 "cdb", DATA_TYPE_UINT8_ARRAY, 30113 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 30114 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 30115 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 30116 "pkt-stats", DATA_TYPE_UINT32, 30117 uscsi_pkt_statistics, 30118 "stat-code", DATA_TYPE_UINT8, 30119 ssc->ssc_uscsi_cmd->uscsi_status, 30120 "un-decode-info", DATA_TYPE_STRING, 30121 ssc->ssc_info, 30122 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 30123 senlen, sensep, 30124 NULL); 30125 } else { 30126 /* 30127 * For other type of invalid data, the 30128 * un-decode-value field would be empty because the 30129 * un-decodable content could be seen from upper 30130 * level payload or inside un-decode-info. 30131 */ 30132 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 30133 "cmd.disk.dev.uderr", uscsi_ena, devid, DDI_NOSLEEP, 30134 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, 30135 "driver-assessment", DATA_TYPE_STRING, 30136 drv_assess == SD_FM_DRV_FATAL ? 30137 "fail" : assessment, 30138 "op-code", DATA_TYPE_UINT8, op_code, 30139 "cdb", DATA_TYPE_UINT8_ARRAY, 30140 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 30141 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 30142 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state, 30143 "pkt-stats", DATA_TYPE_UINT32, 30144 uscsi_pkt_statistics, 30145 "stat-code", DATA_TYPE_UINT8, 30146 ssc->ssc_uscsi_cmd->uscsi_status, 30147 "un-decode-info", DATA_TYPE_STRING, 30148 ssc->ssc_info, 30149 "un-decode-value", DATA_TYPE_UINT8_ARRAY, 30150 0, NULL, 30151 NULL); 30152 } 30153 ssc->ssc_flags &= ~ssc_invalid_flags; 30154 return; 30155 } 30156 30157 if (uscsi_pkt_reason != CMD_CMPLT || 30158 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) { 30159 /* 30160 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was 30161 * set inside sd_start_cmds due to errors(bad packet or 30162 * fatal transport error), we should take it as a 30163 * transport error, so we post ereport.io.scsi.cmd.disk.tran. 30164 * driver-assessment will be set based on drv_assess. 30165 * We will set devid to NULL because it is a transport 30166 * error. 30167 */ 30168 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT) 30169 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT; 30170 30171 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, 30172 "cmd.disk.tran", uscsi_ena, NULL, DDI_NOSLEEP, FM_VERSION, 30173 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 30174 "driver-assessment", DATA_TYPE_STRING, 30175 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 30176 "op-code", DATA_TYPE_UINT8, op_code, 30177 "cdb", DATA_TYPE_UINT8_ARRAY, 30178 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 30179 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason, 30180 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state, 30181 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics, 30182 NULL); 30183 } else { 30184 /* 30185 * If we got here, we have a completed command, and we need 30186 * to further investigate the sense data to see what kind 30187 * of ereport we should post. 30188 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr 30189 * if sense-key == 0x3. 30190 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise. 30191 * driver-assessment will be set based on the parameter 30192 * drv_assess. 30193 */ 30194 if (senlen > 0) { 30195 /* 30196 * Here we have sense data available. 30197 */ 30198 uint8_t sense_key; 30199 sense_key = scsi_sense_key(sensep); 30200 if (sense_key == 0x3) { 30201 /* 30202 * sense-key == 0x3(medium error), 30203 * driver-assessment should be "fatal" if 30204 * drv_assess is SD_FM_DRV_FATAL. 30205 */ 30206 scsi_fm_ereport_post(un->un_sd, 30207 uscsi_path_instance, 30208 "cmd.disk.dev.rqs.merr", 30209 uscsi_ena, devid, DDI_NOSLEEP, FM_VERSION, 30210 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 30211 "driver-assessment", 30212 DATA_TYPE_STRING, 30213 drv_assess == SD_FM_DRV_FATAL ? 30214 "fatal" : assessment, 30215 "op-code", 30216 DATA_TYPE_UINT8, op_code, 30217 "cdb", 30218 DATA_TYPE_UINT8_ARRAY, cdblen, 30219 ssc->ssc_uscsi_cmd->uscsi_cdb, 30220 "pkt-reason", 30221 DATA_TYPE_UINT8, uscsi_pkt_reason, 30222 "pkt-state", 30223 DATA_TYPE_UINT8, uscsi_pkt_state, 30224 "pkt-stats", 30225 DATA_TYPE_UINT32, 30226 uscsi_pkt_statistics, 30227 "stat-code", 30228 DATA_TYPE_UINT8, 30229 ssc->ssc_uscsi_cmd->uscsi_status, 30230 "key", 30231 DATA_TYPE_UINT8, 30232 scsi_sense_key(sensep), 30233 "asc", 30234 DATA_TYPE_UINT8, 30235 scsi_sense_asc(sensep), 30236 "ascq", 30237 DATA_TYPE_UINT8, 30238 scsi_sense_ascq(sensep), 30239 "sense-data", 30240 DATA_TYPE_UINT8_ARRAY, 30241 senlen, sensep, 30242 "lba", 30243 DATA_TYPE_UINT64, 30244 ssc->ssc_uscsi_info->ui_lba, 30245 NULL); 30246 } else { 30247 /* 30248 * if sense-key == 0x4(hardware 30249 * error), driver-assessment should 30250 * be "fatal" if drv_assess is 30251 * SD_FM_DRV_FATAL. 30252 */ 30253 scsi_fm_ereport_post(un->un_sd, 30254 uscsi_path_instance, 30255 "cmd.disk.dev.rqs.derr", 30256 uscsi_ena, devid, DDI_NOSLEEP, 30257 FM_VERSION, 30258 DATA_TYPE_UINT8, FM_EREPORT_VERS0, 30259 "driver-assessment", 30260 DATA_TYPE_STRING, 30261 drv_assess == SD_FM_DRV_FATAL ? 30262 (sense_key == 0x4 ? 30263 "fatal" : "fail") : assessment, 30264 "op-code", 30265 DATA_TYPE_UINT8, op_code, 30266 "cdb", 30267 DATA_TYPE_UINT8_ARRAY, cdblen, 30268 ssc->ssc_uscsi_cmd->uscsi_cdb, 30269 "pkt-reason", 30270 DATA_TYPE_UINT8, uscsi_pkt_reason, 30271 "pkt-state", 30272 DATA_TYPE_UINT8, uscsi_pkt_state, 30273 "pkt-stats", 30274 DATA_TYPE_UINT32, 30275 uscsi_pkt_statistics, 30276 "stat-code", 30277 DATA_TYPE_UINT8, 30278 ssc->ssc_uscsi_cmd->uscsi_status, 30279 "key", 30280 DATA_TYPE_UINT8, 30281 scsi_sense_key(sensep), 30282 "asc", 30283 DATA_TYPE_UINT8, 30284 scsi_sense_asc(sensep), 30285 "ascq", 30286 DATA_TYPE_UINT8, 30287 scsi_sense_ascq(sensep), 30288 "sense-data", 30289 DATA_TYPE_UINT8_ARRAY, 30290 senlen, sensep, 30291 NULL); 30292 } 30293 } else { 30294 /* 30295 * For stat_code == STATUS_GOOD, this is not a 30296 * hardware error. 30297 */ 30298 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) 30299 return; 30300 30301 /* 30302 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the 30303 * stat-code but with sense data unavailable. 30304 * driver-assessment will be set based on parameter 30305 * drv_assess. 30306 */ 30307 scsi_fm_ereport_post(un->un_sd, 30308 uscsi_path_instance, "cmd.disk.dev.serr", uscsi_ena, 30309 devid, DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 30310 FM_EREPORT_VERS0, 30311 "driver-assessment", DATA_TYPE_STRING, 30312 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment, 30313 "op-code", DATA_TYPE_UINT8, op_code, 30314 "cdb", 30315 DATA_TYPE_UINT8_ARRAY, 30316 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb, 30317 "pkt-reason", 30318 DATA_TYPE_UINT8, uscsi_pkt_reason, 30319 "pkt-state", 30320 DATA_TYPE_UINT8, uscsi_pkt_state, 30321 "pkt-stats", 30322 DATA_TYPE_UINT32, uscsi_pkt_statistics, 30323 "stat-code", 30324 DATA_TYPE_UINT8, 30325 ssc->ssc_uscsi_cmd->uscsi_status, 30326 NULL); 30327 } 30328 } 30329 } 30330 30331 /* 30332 * Function: sd_ssc_extract_info 30333 * 30334 * Description: Extract information available to help generate ereport. 30335 * 30336 * Context: Kernel thread or interrupt context. 30337 */ 30338 static void 30339 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp, 30340 struct buf *bp, struct sd_xbuf *xp) 30341 { 30342 size_t senlen = 0; 30343 union scsi_cdb *cdbp; 30344 int path_instance; 30345 /* 30346 * Need scsi_cdb_size array to determine the cdb length. 30347 */ 30348 extern uchar_t scsi_cdb_size[]; 30349 30350 ASSERT(un != NULL); 30351 ASSERT(pktp != NULL); 30352 ASSERT(bp != NULL); 30353 ASSERT(xp != NULL); 30354 ASSERT(ssc != NULL); 30355 ASSERT(mutex_owned(SD_MUTEX(un))); 30356 30357 /* 30358 * Transfer the cdb buffer pointer here. 30359 */ 30360 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 30361 30362 ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 30363 ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp; 30364 30365 /* 30366 * Transfer the sense data buffer pointer if sense data is available, 30367 * calculate the sense data length first. 30368 */ 30369 if ((xp->xb_sense_state & STATE_XARQ_DONE) || 30370 (xp->xb_sense_state & STATE_ARQ_DONE)) { 30371 /* 30372 * For arq case, we will enter here. 30373 */ 30374 if (xp->xb_sense_state & STATE_XARQ_DONE) { 30375 senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid; 30376 } else { 30377 senlen = SENSE_LENGTH; 30378 } 30379 } else { 30380 /* 30381 * For non-arq case, we will enter this branch. 30382 */ 30383 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK && 30384 (xp->xb_sense_state & STATE_XFERRED_DATA)) { 30385 senlen = SENSE_LENGTH - xp->xb_sense_resid; 30386 } 30387 30388 } 30389 30390 ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff); 30391 ssc->ssc_uscsi_cmd->uscsi_rqresid = 0; 30392 ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data; 30393 30394 ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 30395 30396 /* 30397 * Only transfer path_instance when scsi_pkt was properly allocated. 30398 */ 30399 path_instance = pktp->pkt_path_instance; 30400 if (scsi_pkt_allocated_correctly(pktp) && path_instance) 30401 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance; 30402 else 30403 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0; 30404 30405 /* 30406 * Copy in the other fields we may need when posting ereport. 30407 */ 30408 ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason; 30409 ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state; 30410 ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics; 30411 ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp); 30412 30413 /* 30414 * For partially read/write command, we will not create ena 30415 * in case of a successful command be reconized as recovered. 30416 */ 30417 if ((pktp->pkt_reason == CMD_CMPLT) && 30418 (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) && 30419 (senlen == 0)) { 30420 return; 30421 } 30422 30423 /* 30424 * To associate ereports of a single command execution flow, we 30425 * need a shared ena for a specific command. 30426 */ 30427 if (xp->xb_ena == 0) 30428 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1); 30429 ssc->ssc_uscsi_info->ui_ena = xp->xb_ena; 30430 } 30431