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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * SCSI disk target driver. 30 */ 31 #include <sys/scsi/scsi.h> 32 #include <sys/dkbad.h> 33 #include <sys/dklabel.h> 34 #include <sys/dkio.h> 35 #include <sys/fdio.h> 36 #include <sys/cdio.h> 37 #include <sys/mhd.h> 38 #include <sys/vtoc.h> 39 #include <sys/dktp/fdisk.h> 40 #include <sys/kstat.h> 41 #include <sys/vtrace.h> 42 #include <sys/note.h> 43 #include <sys/thread.h> 44 #include <sys/proc.h> 45 #include <sys/efi_partition.h> 46 #include <sys/var.h> 47 #include <sys/aio_req.h> 48 49 #ifdef __lock_lint 50 #define _LP64 51 #define __amd64 52 #endif 53 54 #if (defined(__fibre)) 55 /* Note: is there a leadville version of the following? */ 56 #include <sys/fc4/fcal_linkapp.h> 57 #endif 58 #include <sys/taskq.h> 59 #include <sys/uuid.h> 60 #include <sys/byteorder.h> 61 #include <sys/sdt.h> 62 63 #include "sd_xbuf.h" 64 65 #include <sys/scsi/targets/sddef.h> 66 #include <sys/cmlb.h> 67 68 69 /* 70 * Loadable module info. 71 */ 72 #if (defined(__fibre)) 73 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 74 char _depends_on[] = "misc/scsi misc/cmlb drv/fcp"; 75 #else 76 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 77 char _depends_on[] = "misc/scsi misc/cmlb"; 78 #endif 79 80 /* 81 * Define the interconnect type, to allow the driver to distinguish 82 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 83 * 84 * This is really for backward compatibility. In the future, the driver 85 * should actually check the "interconnect-type" property as reported by 86 * the HBA; however at present this property is not defined by all HBAs, 87 * so we will use this #define (1) to permit the driver to run in 88 * backward-compatibility mode; and (2) to print a notification message 89 * if an FC HBA does not support the "interconnect-type" property. The 90 * behavior of the driver will be to assume parallel SCSI behaviors unless 91 * the "interconnect-type" property is defined by the HBA **AND** has a 92 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 93 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 94 * Channel behaviors (as per the old ssd). (Note that the 95 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 96 * will result in the driver assuming parallel SCSI behaviors.) 97 * 98 * (see common/sys/scsi/impl/services.h) 99 * 100 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 101 * since some FC HBAs may already support that, and there is some code in 102 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 103 * default would confuse that code, and besides things should work fine 104 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 105 * "interconnect_type" property. 106 * 107 */ 108 #if (defined(__fibre)) 109 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 110 #else 111 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 112 #endif 113 114 /* 115 * The name of the driver, established from the module name in _init. 116 */ 117 static char *sd_label = NULL; 118 119 /* 120 * Driver name is unfortunately prefixed on some driver.conf properties. 121 */ 122 #if (defined(__fibre)) 123 #define sd_max_xfer_size ssd_max_xfer_size 124 #define sd_config_list ssd_config_list 125 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 126 static char *sd_config_list = "ssd-config-list"; 127 #else 128 static char *sd_max_xfer_size = "sd_max_xfer_size"; 129 static char *sd_config_list = "sd-config-list"; 130 #endif 131 132 /* 133 * Driver global variables 134 */ 135 136 #if (defined(__fibre)) 137 /* 138 * These #defines are to avoid namespace collisions that occur because this 139 * code is currently used to compile two separate driver modules: sd and ssd. 140 * All global variables need to be treated this way (even if declared static) 141 * in order to allow the debugger to resolve the names properly. 142 * It is anticipated that in the near future the ssd module will be obsoleted, 143 * at which time this namespace issue should go away. 144 */ 145 #define sd_state ssd_state 146 #define sd_io_time ssd_io_time 147 #define sd_failfast_enable ssd_failfast_enable 148 #define sd_ua_retry_count ssd_ua_retry_count 149 #define sd_report_pfa ssd_report_pfa 150 #define sd_max_throttle ssd_max_throttle 151 #define sd_min_throttle ssd_min_throttle 152 #define sd_rot_delay ssd_rot_delay 153 154 #define sd_retry_on_reservation_conflict \ 155 ssd_retry_on_reservation_conflict 156 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 157 #define sd_resv_conflict_name ssd_resv_conflict_name 158 159 #define sd_component_mask ssd_component_mask 160 #define sd_level_mask ssd_level_mask 161 #define sd_debug_un ssd_debug_un 162 #define sd_error_level ssd_error_level 163 164 #define sd_xbuf_active_limit ssd_xbuf_active_limit 165 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 166 167 #define sd_tr ssd_tr 168 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 169 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 170 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 171 #define sd_check_media_time ssd_check_media_time 172 #define sd_wait_cmds_complete ssd_wait_cmds_complete 173 #define sd_label_mutex ssd_label_mutex 174 #define sd_detach_mutex ssd_detach_mutex 175 #define sd_log_buf ssd_log_buf 176 #define sd_log_mutex ssd_log_mutex 177 178 #define sd_disk_table ssd_disk_table 179 #define sd_disk_table_size ssd_disk_table_size 180 #define sd_sense_mutex ssd_sense_mutex 181 #define sd_cdbtab ssd_cdbtab 182 183 #define sd_cb_ops ssd_cb_ops 184 #define sd_ops ssd_ops 185 #define sd_additional_codes ssd_additional_codes 186 #define sd_tgops ssd_tgops 187 188 #define sd_minor_data ssd_minor_data 189 #define sd_minor_data_efi ssd_minor_data_efi 190 191 #define sd_tq ssd_tq 192 #define sd_wmr_tq ssd_wmr_tq 193 #define sd_taskq_name ssd_taskq_name 194 #define sd_wmr_taskq_name ssd_wmr_taskq_name 195 #define sd_taskq_minalloc ssd_taskq_minalloc 196 #define sd_taskq_maxalloc ssd_taskq_maxalloc 197 198 #define sd_dump_format_string ssd_dump_format_string 199 200 #define sd_iostart_chain ssd_iostart_chain 201 #define sd_iodone_chain ssd_iodone_chain 202 203 #define sd_pm_idletime ssd_pm_idletime 204 205 #define sd_force_pm_supported ssd_force_pm_supported 206 207 #define sd_dtype_optical_bind ssd_dtype_optical_bind 208 209 #endif 210 211 212 #ifdef SDDEBUG 213 int sd_force_pm_supported = 0; 214 #endif /* SDDEBUG */ 215 216 void *sd_state = NULL; 217 int sd_io_time = SD_IO_TIME; 218 int sd_failfast_enable = 1; 219 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 220 int sd_report_pfa = 1; 221 int sd_max_throttle = SD_MAX_THROTTLE; 222 int sd_min_throttle = SD_MIN_THROTTLE; 223 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 224 int sd_qfull_throttle_enable = TRUE; 225 226 int sd_retry_on_reservation_conflict = 1; 227 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 228 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 229 230 static int sd_dtype_optical_bind = -1; 231 232 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 233 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 234 235 /* 236 * Global data for debug logging. To enable debug printing, sd_component_mask 237 * and sd_level_mask should be set to the desired bit patterns as outlined in 238 * sddef.h. 239 */ 240 uint_t sd_component_mask = 0x0; 241 uint_t sd_level_mask = 0x0; 242 struct sd_lun *sd_debug_un = NULL; 243 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 244 245 /* Note: these may go away in the future... */ 246 static uint32_t sd_xbuf_active_limit = 512; 247 static uint32_t sd_xbuf_reserve_limit = 16; 248 249 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 250 251 /* 252 * Timer value used to reset the throttle after it has been reduced 253 * (typically in response to TRAN_BUSY or STATUS_QFULL) 254 */ 255 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 256 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 257 258 /* 259 * Interval value associated with the media change scsi watch. 260 */ 261 static int sd_check_media_time = 3000000; 262 263 /* 264 * Wait value used for in progress operations during a DDI_SUSPEND 265 */ 266 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 267 268 /* 269 * sd_label_mutex protects a static buffer used in the disk label 270 * component of the driver 271 */ 272 static kmutex_t sd_label_mutex; 273 274 /* 275 * sd_detach_mutex protects un_layer_count, un_detach_count, and 276 * un_opens_in_progress in the sd_lun structure. 277 */ 278 static kmutex_t sd_detach_mutex; 279 280 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 281 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 282 283 /* 284 * Global buffer and mutex for debug logging 285 */ 286 static char sd_log_buf[1024]; 287 static kmutex_t sd_log_mutex; 288 289 /* 290 * Structs and globals for recording attached lun information. 291 * This maintains a chain. Each node in the chain represents a SCSI controller. 292 * The structure records the number of luns attached to each target connected 293 * with the controller. 294 * For parallel scsi device only. 295 */ 296 struct sd_scsi_hba_tgt_lun { 297 struct sd_scsi_hba_tgt_lun *next; 298 dev_info_t *pdip; 299 int nlun[NTARGETS_WIDE]; 300 }; 301 302 /* 303 * Flag to indicate the lun is attached or detached 304 */ 305 #define SD_SCSI_LUN_ATTACH 0 306 #define SD_SCSI_LUN_DETACH 1 307 308 static kmutex_t sd_scsi_target_lun_mutex; 309 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 310 311 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 312 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 313 314 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 315 sd_scsi_target_lun_head)) 316 317 /* 318 * "Smart" Probe Caching structs, globals, #defines, etc. 319 * For parallel scsi and non-self-identify device only. 320 */ 321 322 /* 323 * The following resources and routines are implemented to support 324 * "smart" probing, which caches the scsi_probe() results in an array, 325 * in order to help avoid long probe times. 326 */ 327 struct sd_scsi_probe_cache { 328 struct sd_scsi_probe_cache *next; 329 dev_info_t *pdip; 330 int cache[NTARGETS_WIDE]; 331 }; 332 333 static kmutex_t sd_scsi_probe_cache_mutex; 334 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 335 336 /* 337 * Really we only need protection on the head of the linked list, but 338 * better safe than sorry. 339 */ 340 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 341 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 342 343 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 344 sd_scsi_probe_cache_head)) 345 346 347 /* 348 * Vendor specific data name property declarations 349 */ 350 351 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 352 353 static sd_tunables seagate_properties = { 354 SEAGATE_THROTTLE_VALUE, 355 0, 356 0, 357 0, 358 0, 359 0, 360 0, 361 0, 362 0 363 }; 364 365 366 static sd_tunables fujitsu_properties = { 367 FUJITSU_THROTTLE_VALUE, 368 0, 369 0, 370 0, 371 0, 372 0, 373 0, 374 0, 375 0 376 }; 377 378 static sd_tunables ibm_properties = { 379 IBM_THROTTLE_VALUE, 380 0, 381 0, 382 0, 383 0, 384 0, 385 0, 386 0, 387 0 388 }; 389 390 static sd_tunables purple_properties = { 391 PURPLE_THROTTLE_VALUE, 392 0, 393 0, 394 PURPLE_BUSY_RETRIES, 395 PURPLE_RESET_RETRY_COUNT, 396 PURPLE_RESERVE_RELEASE_TIME, 397 0, 398 0, 399 0 400 }; 401 402 static sd_tunables sve_properties = { 403 SVE_THROTTLE_VALUE, 404 0, 405 0, 406 SVE_BUSY_RETRIES, 407 SVE_RESET_RETRY_COUNT, 408 SVE_RESERVE_RELEASE_TIME, 409 SVE_MIN_THROTTLE_VALUE, 410 SVE_DISKSORT_DISABLED_FLAG, 411 0 412 }; 413 414 static sd_tunables maserati_properties = { 415 0, 416 0, 417 0, 418 0, 419 0, 420 0, 421 0, 422 MASERATI_DISKSORT_DISABLED_FLAG, 423 MASERATI_LUN_RESET_ENABLED_FLAG 424 }; 425 426 static sd_tunables pirus_properties = { 427 PIRUS_THROTTLE_VALUE, 428 0, 429 PIRUS_NRR_COUNT, 430 PIRUS_BUSY_RETRIES, 431 PIRUS_RESET_RETRY_COUNT, 432 0, 433 PIRUS_MIN_THROTTLE_VALUE, 434 PIRUS_DISKSORT_DISABLED_FLAG, 435 PIRUS_LUN_RESET_ENABLED_FLAG 436 }; 437 438 #endif 439 440 #if (defined(__sparc) && !defined(__fibre)) || \ 441 (defined(__i386) || defined(__amd64)) 442 443 444 static sd_tunables elite_properties = { 445 ELITE_THROTTLE_VALUE, 446 0, 447 0, 448 0, 449 0, 450 0, 451 0, 452 0, 453 0 454 }; 455 456 static sd_tunables st31200n_properties = { 457 ST31200N_THROTTLE_VALUE, 458 0, 459 0, 460 0, 461 0, 462 0, 463 0, 464 0, 465 0 466 }; 467 468 #endif /* Fibre or not */ 469 470 static sd_tunables lsi_properties_scsi = { 471 LSI_THROTTLE_VALUE, 472 0, 473 LSI_NOTREADY_RETRIES, 474 0, 475 0, 476 0, 477 0, 478 0, 479 0 480 }; 481 482 static sd_tunables symbios_properties = { 483 SYMBIOS_THROTTLE_VALUE, 484 0, 485 SYMBIOS_NOTREADY_RETRIES, 486 0, 487 0, 488 0, 489 0, 490 0, 491 0 492 }; 493 494 static sd_tunables lsi_properties = { 495 0, 496 0, 497 LSI_NOTREADY_RETRIES, 498 0, 499 0, 500 0, 501 0, 502 0, 503 0 504 }; 505 506 static sd_tunables lsi_oem_properties = { 507 0, 508 0, 509 LSI_OEM_NOTREADY_RETRIES, 510 0, 511 0, 512 0, 513 0, 514 0, 515 0, 516 1 517 }; 518 519 520 521 #if (defined(SD_PROP_TST)) 522 523 #define SD_TST_CTYPE_VAL CTYPE_CDROM 524 #define SD_TST_THROTTLE_VAL 16 525 #define SD_TST_NOTREADY_VAL 12 526 #define SD_TST_BUSY_VAL 60 527 #define SD_TST_RST_RETRY_VAL 36 528 #define SD_TST_RSV_REL_TIME 60 529 530 static sd_tunables tst_properties = { 531 SD_TST_THROTTLE_VAL, 532 SD_TST_CTYPE_VAL, 533 SD_TST_NOTREADY_VAL, 534 SD_TST_BUSY_VAL, 535 SD_TST_RST_RETRY_VAL, 536 SD_TST_RSV_REL_TIME, 537 0, 538 0, 539 0 540 }; 541 #endif 542 543 /* This is similar to the ANSI toupper implementation */ 544 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 545 546 /* 547 * Static Driver Configuration Table 548 * 549 * This is the table of disks which need throttle adjustment (or, perhaps 550 * something else as defined by the flags at a future time.) device_id 551 * is a string consisting of concatenated vid (vendor), pid (product/model) 552 * and revision strings as defined in the scsi_inquiry structure. Offsets of 553 * the parts of the string are as defined by the sizes in the scsi_inquiry 554 * structure. Device type is searched as far as the device_id string is 555 * defined. Flags defines which values are to be set in the driver from the 556 * properties list. 557 * 558 * Entries below which begin and end with a "*" are a special case. 559 * These do not have a specific vendor, and the string which follows 560 * can appear anywhere in the 16 byte PID portion of the inquiry data. 561 * 562 * Entries below which begin and end with a " " (blank) are a special 563 * case. The comparison function will treat multiple consecutive blanks 564 * as equivalent to a single blank. For example, this causes a 565 * sd_disk_table entry of " NEC CDROM " to match a device's id string 566 * of "NEC CDROM". 567 * 568 * Note: The MD21 controller type has been obsoleted. 569 * ST318202F is a Legacy device 570 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 571 * made with an FC connection. The entries here are a legacy. 572 */ 573 static sd_disk_config_t sd_disk_table[] = { 574 #if defined(__fibre) || defined(__i386) || defined(__amd64) 575 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 576 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 577 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 578 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 579 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 580 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 581 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 582 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 583 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 584 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 585 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 586 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 587 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 588 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 589 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 590 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 591 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 592 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 593 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 594 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 595 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 596 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 597 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 598 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 599 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 600 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 601 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 602 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 603 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 604 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 605 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 606 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 607 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 608 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 609 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 610 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 611 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 612 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 613 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 614 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 615 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 616 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 617 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 618 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 619 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 620 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 621 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT | 622 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 623 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT | 624 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties }, 625 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 626 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 627 { "SUN T3", SD_CONF_BSET_THROTTLE | 628 SD_CONF_BSET_BSY_RETRY_COUNT| 629 SD_CONF_BSET_RST_RETRIES| 630 SD_CONF_BSET_RSV_REL_TIME, 631 &purple_properties }, 632 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 633 SD_CONF_BSET_BSY_RETRY_COUNT| 634 SD_CONF_BSET_RST_RETRIES| 635 SD_CONF_BSET_RSV_REL_TIME| 636 SD_CONF_BSET_MIN_THROTTLE| 637 SD_CONF_BSET_DISKSORT_DISABLED, 638 &sve_properties }, 639 { "SUN T4", SD_CONF_BSET_THROTTLE | 640 SD_CONF_BSET_BSY_RETRY_COUNT| 641 SD_CONF_BSET_RST_RETRIES| 642 SD_CONF_BSET_RSV_REL_TIME, 643 &purple_properties }, 644 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 645 SD_CONF_BSET_LUN_RESET_ENABLED, 646 &maserati_properties }, 647 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 648 SD_CONF_BSET_NRR_COUNT| 649 SD_CONF_BSET_BSY_RETRY_COUNT| 650 SD_CONF_BSET_RST_RETRIES| 651 SD_CONF_BSET_MIN_THROTTLE| 652 SD_CONF_BSET_DISKSORT_DISABLED| 653 SD_CONF_BSET_LUN_RESET_ENABLED, 654 &pirus_properties }, 655 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 656 SD_CONF_BSET_NRR_COUNT| 657 SD_CONF_BSET_BSY_RETRY_COUNT| 658 SD_CONF_BSET_RST_RETRIES| 659 SD_CONF_BSET_MIN_THROTTLE| 660 SD_CONF_BSET_DISKSORT_DISABLED| 661 SD_CONF_BSET_LUN_RESET_ENABLED, 662 &pirus_properties }, 663 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 664 SD_CONF_BSET_NRR_COUNT| 665 SD_CONF_BSET_BSY_RETRY_COUNT| 666 SD_CONF_BSET_RST_RETRIES| 667 SD_CONF_BSET_MIN_THROTTLE| 668 SD_CONF_BSET_DISKSORT_DISABLED| 669 SD_CONF_BSET_LUN_RESET_ENABLED, 670 &pirus_properties }, 671 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 672 SD_CONF_BSET_NRR_COUNT| 673 SD_CONF_BSET_BSY_RETRY_COUNT| 674 SD_CONF_BSET_RST_RETRIES| 675 SD_CONF_BSET_MIN_THROTTLE| 676 SD_CONF_BSET_DISKSORT_DISABLED| 677 SD_CONF_BSET_LUN_RESET_ENABLED, 678 &pirus_properties }, 679 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 680 SD_CONF_BSET_NRR_COUNT| 681 SD_CONF_BSET_BSY_RETRY_COUNT| 682 SD_CONF_BSET_RST_RETRIES| 683 SD_CONF_BSET_MIN_THROTTLE| 684 SD_CONF_BSET_DISKSORT_DISABLED| 685 SD_CONF_BSET_LUN_RESET_ENABLED, 686 &pirus_properties }, 687 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 688 SD_CONF_BSET_NRR_COUNT| 689 SD_CONF_BSET_BSY_RETRY_COUNT| 690 SD_CONF_BSET_RST_RETRIES| 691 SD_CONF_BSET_MIN_THROTTLE| 692 SD_CONF_BSET_DISKSORT_DISABLED| 693 SD_CONF_BSET_LUN_RESET_ENABLED, 694 &pirus_properties }, 695 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 696 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 697 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 698 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 699 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 700 #endif /* fibre or NON-sparc platforms */ 701 #if ((defined(__sparc) && !defined(__fibre)) ||\ 702 (defined(__i386) || defined(__amd64))) 703 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 704 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 705 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 706 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 707 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 708 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 709 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 710 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 711 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 712 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 713 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 714 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 715 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 716 &symbios_properties }, 717 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 718 &lsi_properties_scsi }, 719 #if defined(__i386) || defined(__amd64) 720 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 721 | SD_CONF_BSET_READSUB_BCD 722 | SD_CONF_BSET_READ_TOC_ADDR_BCD 723 | SD_CONF_BSET_NO_READ_HEADER 724 | SD_CONF_BSET_READ_CD_XD4), NULL }, 725 726 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 727 | SD_CONF_BSET_READSUB_BCD 728 | SD_CONF_BSET_READ_TOC_ADDR_BCD 729 | SD_CONF_BSET_NO_READ_HEADER 730 | SD_CONF_BSET_READ_CD_XD4), NULL }, 731 #endif /* __i386 || __amd64 */ 732 #endif /* sparc NON-fibre or NON-sparc platforms */ 733 734 #if (defined(SD_PROP_TST)) 735 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 736 | SD_CONF_BSET_CTYPE 737 | SD_CONF_BSET_NRR_COUNT 738 | SD_CONF_BSET_FAB_DEVID 739 | SD_CONF_BSET_NOCACHE 740 | SD_CONF_BSET_BSY_RETRY_COUNT 741 | SD_CONF_BSET_PLAYMSF_BCD 742 | SD_CONF_BSET_READSUB_BCD 743 | SD_CONF_BSET_READ_TOC_TRK_BCD 744 | SD_CONF_BSET_READ_TOC_ADDR_BCD 745 | SD_CONF_BSET_NO_READ_HEADER 746 | SD_CONF_BSET_READ_CD_XD4 747 | SD_CONF_BSET_RST_RETRIES 748 | SD_CONF_BSET_RSV_REL_TIME 749 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 750 #endif 751 }; 752 753 static const int sd_disk_table_size = 754 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 755 756 757 758 #define SD_INTERCONNECT_PARALLEL 0 759 #define SD_INTERCONNECT_FABRIC 1 760 #define SD_INTERCONNECT_FIBRE 2 761 #define SD_INTERCONNECT_SSA 3 762 #define SD_INTERCONNECT_SATA 4 763 #define SD_IS_PARALLEL_SCSI(un) \ 764 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 765 #define SD_IS_SERIAL(un) \ 766 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 767 768 /* 769 * Definitions used by device id registration routines 770 */ 771 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 772 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 773 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 774 775 static kmutex_t sd_sense_mutex = {0}; 776 777 /* 778 * Macros for updates of the driver state 779 */ 780 #define New_state(un, s) \ 781 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 782 #define Restore_state(un) \ 783 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 784 785 static struct sd_cdbinfo sd_cdbtab[] = { 786 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 787 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 788 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 789 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 790 }; 791 792 /* 793 * Specifies the number of seconds that must have elapsed since the last 794 * cmd. has completed for a device to be declared idle to the PM framework. 795 */ 796 static int sd_pm_idletime = 1; 797 798 /* 799 * Internal function prototypes 800 */ 801 802 #if (defined(__fibre)) 803 /* 804 * These #defines are to avoid namespace collisions that occur because this 805 * code is currently used to compile two separate driver modules: sd and ssd. 806 * All function names need to be treated this way (even if declared static) 807 * in order to allow the debugger to resolve the names properly. 808 * It is anticipated that in the near future the ssd module will be obsoleted, 809 * at which time this ugliness should go away. 810 */ 811 #define sd_log_trace ssd_log_trace 812 #define sd_log_info ssd_log_info 813 #define sd_log_err ssd_log_err 814 #define sdprobe ssdprobe 815 #define sdinfo ssdinfo 816 #define sd_prop_op ssd_prop_op 817 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 818 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 819 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 820 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 821 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 822 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 823 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 824 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 825 #define sd_spin_up_unit ssd_spin_up_unit 826 #define sd_enable_descr_sense ssd_enable_descr_sense 827 #define sd_reenable_dsense_task ssd_reenable_dsense_task 828 #define sd_set_mmc_caps ssd_set_mmc_caps 829 #define sd_read_unit_properties ssd_read_unit_properties 830 #define sd_process_sdconf_file ssd_process_sdconf_file 831 #define sd_process_sdconf_table ssd_process_sdconf_table 832 #define sd_sdconf_id_match ssd_sdconf_id_match 833 #define sd_blank_cmp ssd_blank_cmp 834 #define sd_chk_vers1_data ssd_chk_vers1_data 835 #define sd_set_vers1_properties ssd_set_vers1_properties 836 837 #define sd_get_physical_geometry ssd_get_physical_geometry 838 #define sd_get_virtual_geometry ssd_get_virtual_geometry 839 #define sd_update_block_info ssd_update_block_info 840 #define sd_register_devid ssd_register_devid 841 #define sd_get_devid ssd_get_devid 842 #define sd_create_devid ssd_create_devid 843 #define sd_write_deviceid ssd_write_deviceid 844 #define sd_check_vpd_page_support ssd_check_vpd_page_support 845 #define sd_setup_pm ssd_setup_pm 846 #define sd_create_pm_components ssd_create_pm_components 847 #define sd_ddi_suspend ssd_ddi_suspend 848 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 849 #define sd_ddi_resume ssd_ddi_resume 850 #define sd_ddi_pm_resume ssd_ddi_pm_resume 851 #define sdpower ssdpower 852 #define sdattach ssdattach 853 #define sddetach ssddetach 854 #define sd_unit_attach ssd_unit_attach 855 #define sd_unit_detach ssd_unit_detach 856 #define sd_set_unit_attributes ssd_set_unit_attributes 857 #define sd_create_errstats ssd_create_errstats 858 #define sd_set_errstats ssd_set_errstats 859 #define sd_set_pstats ssd_set_pstats 860 #define sddump ssddump 861 #define sd_scsi_poll ssd_scsi_poll 862 #define sd_send_polled_RQS ssd_send_polled_RQS 863 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 864 #define sd_init_event_callbacks ssd_init_event_callbacks 865 #define sd_event_callback ssd_event_callback 866 #define sd_cache_control ssd_cache_control 867 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 868 #define sd_get_nv_sup ssd_get_nv_sup 869 #define sd_make_device ssd_make_device 870 #define sdopen ssdopen 871 #define sdclose ssdclose 872 #define sd_ready_and_valid ssd_ready_and_valid 873 #define sdmin ssdmin 874 #define sdread ssdread 875 #define sdwrite ssdwrite 876 #define sdaread ssdaread 877 #define sdawrite ssdawrite 878 #define sdstrategy ssdstrategy 879 #define sdioctl ssdioctl 880 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 881 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 882 #define sd_checksum_iostart ssd_checksum_iostart 883 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 884 #define sd_pm_iostart ssd_pm_iostart 885 #define sd_core_iostart ssd_core_iostart 886 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 887 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 888 #define sd_checksum_iodone ssd_checksum_iodone 889 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 890 #define sd_pm_iodone ssd_pm_iodone 891 #define sd_initpkt_for_buf ssd_initpkt_for_buf 892 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 893 #define sd_setup_rw_pkt ssd_setup_rw_pkt 894 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 895 #define sd_buf_iodone ssd_buf_iodone 896 #define sd_uscsi_strategy ssd_uscsi_strategy 897 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 898 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 899 #define sd_uscsi_iodone ssd_uscsi_iodone 900 #define sd_xbuf_strategy ssd_xbuf_strategy 901 #define sd_xbuf_init ssd_xbuf_init 902 #define sd_pm_entry ssd_pm_entry 903 #define sd_pm_exit ssd_pm_exit 904 905 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 906 #define sd_pm_timeout_handler ssd_pm_timeout_handler 907 908 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 909 #define sdintr ssdintr 910 #define sd_start_cmds ssd_start_cmds 911 #define sd_send_scsi_cmd ssd_send_scsi_cmd 912 #define sd_bioclone_alloc ssd_bioclone_alloc 913 #define sd_bioclone_free ssd_bioclone_free 914 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 915 #define sd_shadow_buf_free ssd_shadow_buf_free 916 #define sd_print_transport_rejected_message \ 917 ssd_print_transport_rejected_message 918 #define sd_retry_command ssd_retry_command 919 #define sd_set_retry_bp ssd_set_retry_bp 920 #define sd_send_request_sense_command ssd_send_request_sense_command 921 #define sd_start_retry_command ssd_start_retry_command 922 #define sd_start_direct_priority_command \ 923 ssd_start_direct_priority_command 924 #define sd_return_failed_command ssd_return_failed_command 925 #define sd_return_failed_command_no_restart \ 926 ssd_return_failed_command_no_restart 927 #define sd_return_command ssd_return_command 928 #define sd_sync_with_callback ssd_sync_with_callback 929 #define sdrunout ssdrunout 930 #define sd_mark_rqs_busy ssd_mark_rqs_busy 931 #define sd_mark_rqs_idle ssd_mark_rqs_idle 932 #define sd_reduce_throttle ssd_reduce_throttle 933 #define sd_restore_throttle ssd_restore_throttle 934 #define sd_print_incomplete_msg ssd_print_incomplete_msg 935 #define sd_init_cdb_limits ssd_init_cdb_limits 936 #define sd_pkt_status_good ssd_pkt_status_good 937 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 938 #define sd_pkt_status_busy ssd_pkt_status_busy 939 #define sd_pkt_status_reservation_conflict \ 940 ssd_pkt_status_reservation_conflict 941 #define sd_pkt_status_qfull ssd_pkt_status_qfull 942 #define sd_handle_request_sense ssd_handle_request_sense 943 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 944 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 945 #define sd_validate_sense_data ssd_validate_sense_data 946 #define sd_decode_sense ssd_decode_sense 947 #define sd_print_sense_msg ssd_print_sense_msg 948 #define sd_sense_key_no_sense ssd_sense_key_no_sense 949 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 950 #define sd_sense_key_not_ready ssd_sense_key_not_ready 951 #define sd_sense_key_medium_or_hardware_error \ 952 ssd_sense_key_medium_or_hardware_error 953 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 954 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 955 #define sd_sense_key_fail_command ssd_sense_key_fail_command 956 #define sd_sense_key_blank_check ssd_sense_key_blank_check 957 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 958 #define sd_sense_key_default ssd_sense_key_default 959 #define sd_print_retry_msg ssd_print_retry_msg 960 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 961 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 962 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 963 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 964 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 965 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 966 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 967 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 968 #define sd_pkt_reason_default ssd_pkt_reason_default 969 #define sd_reset_target ssd_reset_target 970 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 971 #define sd_start_stop_unit_task ssd_start_stop_unit_task 972 #define sd_taskq_create ssd_taskq_create 973 #define sd_taskq_delete ssd_taskq_delete 974 #define sd_media_change_task ssd_media_change_task 975 #define sd_handle_mchange ssd_handle_mchange 976 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 977 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 978 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 979 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 980 #define sd_send_scsi_feature_GET_CONFIGURATION \ 981 sd_send_scsi_feature_GET_CONFIGURATION 982 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 983 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 984 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 985 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 986 ssd_send_scsi_PERSISTENT_RESERVE_IN 987 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 988 ssd_send_scsi_PERSISTENT_RESERVE_OUT 989 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 990 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 991 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 992 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 993 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 994 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 995 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 996 #define sd_alloc_rqs ssd_alloc_rqs 997 #define sd_free_rqs ssd_free_rqs 998 #define sd_dump_memory ssd_dump_memory 999 #define sd_get_media_info ssd_get_media_info 1000 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1001 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1002 #define sd_setup_next_xfer ssd_setup_next_xfer 1003 #define sd_dkio_get_temp ssd_dkio_get_temp 1004 #define sd_check_mhd ssd_check_mhd 1005 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1006 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1007 #define sd_sname ssd_sname 1008 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1009 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1010 #define sd_take_ownership ssd_take_ownership 1011 #define sd_reserve_release ssd_reserve_release 1012 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1013 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1014 #define sd_persistent_reservation_in_read_keys \ 1015 ssd_persistent_reservation_in_read_keys 1016 #define sd_persistent_reservation_in_read_resv \ 1017 ssd_persistent_reservation_in_read_resv 1018 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1019 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1020 #define sd_mhdioc_release ssd_mhdioc_release 1021 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1022 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1023 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1024 #define sr_change_blkmode ssr_change_blkmode 1025 #define sr_change_speed ssr_change_speed 1026 #define sr_atapi_change_speed ssr_atapi_change_speed 1027 #define sr_pause_resume ssr_pause_resume 1028 #define sr_play_msf ssr_play_msf 1029 #define sr_play_trkind ssr_play_trkind 1030 #define sr_read_all_subcodes ssr_read_all_subcodes 1031 #define sr_read_subchannel ssr_read_subchannel 1032 #define sr_read_tocentry ssr_read_tocentry 1033 #define sr_read_tochdr ssr_read_tochdr 1034 #define sr_read_cdda ssr_read_cdda 1035 #define sr_read_cdxa ssr_read_cdxa 1036 #define sr_read_mode1 ssr_read_mode1 1037 #define sr_read_mode2 ssr_read_mode2 1038 #define sr_read_cd_mode2 ssr_read_cd_mode2 1039 #define sr_sector_mode ssr_sector_mode 1040 #define sr_eject ssr_eject 1041 #define sr_ejected ssr_ejected 1042 #define sr_check_wp ssr_check_wp 1043 #define sd_check_media ssd_check_media 1044 #define sd_media_watch_cb ssd_media_watch_cb 1045 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1046 #define sr_volume_ctrl ssr_volume_ctrl 1047 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1048 #define sd_log_page_supported ssd_log_page_supported 1049 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1050 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1051 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1052 #define sd_range_lock ssd_range_lock 1053 #define sd_get_range ssd_get_range 1054 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1055 #define sd_range_unlock ssd_range_unlock 1056 #define sd_read_modify_write_task ssd_read_modify_write_task 1057 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1058 1059 #define sd_iostart_chain ssd_iostart_chain 1060 #define sd_iodone_chain ssd_iodone_chain 1061 #define sd_initpkt_map ssd_initpkt_map 1062 #define sd_destroypkt_map ssd_destroypkt_map 1063 #define sd_chain_type_map ssd_chain_type_map 1064 #define sd_chain_index_map ssd_chain_index_map 1065 1066 #define sd_failfast_flushctl ssd_failfast_flushctl 1067 #define sd_failfast_flushq ssd_failfast_flushq 1068 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1069 1070 #define sd_is_lsi ssd_is_lsi 1071 #define sd_tg_rdwr ssd_tg_rdwr 1072 #define sd_tg_getinfo ssd_tg_getinfo 1073 1074 #endif /* #if (defined(__fibre)) */ 1075 1076 1077 int _init(void); 1078 int _fini(void); 1079 int _info(struct modinfo *modinfop); 1080 1081 /*PRINTFLIKE3*/ 1082 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1083 /*PRINTFLIKE3*/ 1084 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1085 /*PRINTFLIKE3*/ 1086 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1087 1088 static int sdprobe(dev_info_t *devi); 1089 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1090 void **result); 1091 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1092 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1093 1094 /* 1095 * Smart probe for parallel scsi 1096 */ 1097 static void sd_scsi_probe_cache_init(void); 1098 static void sd_scsi_probe_cache_fini(void); 1099 static void sd_scsi_clear_probe_cache(void); 1100 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1101 1102 /* 1103 * Attached luns on target for parallel scsi 1104 */ 1105 static void sd_scsi_target_lun_init(void); 1106 static void sd_scsi_target_lun_fini(void); 1107 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1108 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1109 1110 static int sd_spin_up_unit(struct sd_lun *un); 1111 #ifdef _LP64 1112 static void sd_enable_descr_sense(struct sd_lun *un); 1113 static void sd_reenable_dsense_task(void *arg); 1114 #endif /* _LP64 */ 1115 1116 static void sd_set_mmc_caps(struct sd_lun *un); 1117 1118 static void sd_read_unit_properties(struct sd_lun *un); 1119 static int sd_process_sdconf_file(struct sd_lun *un); 1120 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1121 int *data_list, sd_tunables *values); 1122 static void sd_process_sdconf_table(struct sd_lun *un); 1123 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1124 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1125 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1126 int list_len, char *dataname_ptr); 1127 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1128 sd_tunables *prop_list); 1129 1130 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1131 int reservation_flag); 1132 static int sd_get_devid(struct sd_lun *un); 1133 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1134 static int sd_write_deviceid(struct sd_lun *un); 1135 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1136 static int sd_check_vpd_page_support(struct sd_lun *un); 1137 1138 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1139 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1140 1141 static int sd_ddi_suspend(dev_info_t *devi); 1142 static int sd_ddi_pm_suspend(struct sd_lun *un); 1143 static int sd_ddi_resume(dev_info_t *devi); 1144 static int sd_ddi_pm_resume(struct sd_lun *un); 1145 static int sdpower(dev_info_t *devi, int component, int level); 1146 1147 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1148 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1149 static int sd_unit_attach(dev_info_t *devi); 1150 static int sd_unit_detach(dev_info_t *devi); 1151 1152 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1153 static void sd_create_errstats(struct sd_lun *un, int instance); 1154 static void sd_set_errstats(struct sd_lun *un); 1155 static void sd_set_pstats(struct sd_lun *un); 1156 1157 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1158 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1159 static int sd_send_polled_RQS(struct sd_lun *un); 1160 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1161 1162 #if (defined(__fibre)) 1163 /* 1164 * Event callbacks (photon) 1165 */ 1166 static void sd_init_event_callbacks(struct sd_lun *un); 1167 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1168 #endif 1169 1170 /* 1171 * Defines for sd_cache_control 1172 */ 1173 1174 #define SD_CACHE_ENABLE 1 1175 #define SD_CACHE_DISABLE 0 1176 #define SD_CACHE_NOCHANGE -1 1177 1178 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1179 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1180 static void sd_get_nv_sup(struct sd_lun *un); 1181 static dev_t sd_make_device(dev_info_t *devi); 1182 1183 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1184 uint64_t capacity); 1185 1186 /* 1187 * Driver entry point functions. 1188 */ 1189 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1190 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1191 static int sd_ready_and_valid(struct sd_lun *un); 1192 1193 static void sdmin(struct buf *bp); 1194 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1195 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1196 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1197 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1198 1199 static int sdstrategy(struct buf *bp); 1200 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1201 1202 /* 1203 * Function prototypes for layering functions in the iostart chain. 1204 */ 1205 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1206 struct buf *bp); 1207 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1208 struct buf *bp); 1209 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1210 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1211 struct buf *bp); 1212 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1213 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1214 1215 /* 1216 * Function prototypes for layering functions in the iodone chain. 1217 */ 1218 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1219 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1220 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1221 struct buf *bp); 1222 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1223 struct buf *bp); 1224 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1225 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1226 struct buf *bp); 1227 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1228 1229 /* 1230 * Prototypes for functions to support buf(9S) based IO. 1231 */ 1232 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1233 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1234 static void sd_destroypkt_for_buf(struct buf *); 1235 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1236 struct buf *bp, int flags, 1237 int (*callback)(caddr_t), caddr_t callback_arg, 1238 diskaddr_t lba, uint32_t blockcount); 1239 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1240 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1241 1242 /* 1243 * Prototypes for functions to support USCSI IO. 1244 */ 1245 static int sd_uscsi_strategy(struct buf *bp); 1246 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1247 static void sd_destroypkt_for_uscsi(struct buf *); 1248 1249 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1250 uchar_t chain_type, void *pktinfop); 1251 1252 static int sd_pm_entry(struct sd_lun *un); 1253 static void sd_pm_exit(struct sd_lun *un); 1254 1255 static void sd_pm_idletimeout_handler(void *arg); 1256 1257 /* 1258 * sd_core internal functions (used at the sd_core_io layer). 1259 */ 1260 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1261 static void sdintr(struct scsi_pkt *pktp); 1262 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1263 1264 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 1265 enum uio_seg dataspace, int path_flag); 1266 1267 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1268 daddr_t blkno, int (*func)(struct buf *)); 1269 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1270 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1271 static void sd_bioclone_free(struct buf *bp); 1272 static void sd_shadow_buf_free(struct buf *bp); 1273 1274 static void sd_print_transport_rejected_message(struct sd_lun *un, 1275 struct sd_xbuf *xp, int code); 1276 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1277 void *arg, int code); 1278 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1279 void *arg, int code); 1280 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1281 void *arg, int code); 1282 1283 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1284 int retry_check_flag, 1285 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1286 int c), 1287 void *user_arg, int failure_code, clock_t retry_delay, 1288 void (*statp)(kstat_io_t *)); 1289 1290 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1291 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1292 1293 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1294 struct scsi_pkt *pktp); 1295 static void sd_start_retry_command(void *arg); 1296 static void sd_start_direct_priority_command(void *arg); 1297 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1298 int errcode); 1299 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1300 struct buf *bp, int errcode); 1301 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1302 static void sd_sync_with_callback(struct sd_lun *un); 1303 static int sdrunout(caddr_t arg); 1304 1305 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1306 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1307 1308 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1309 static void sd_restore_throttle(void *arg); 1310 1311 static void sd_init_cdb_limits(struct sd_lun *un); 1312 1313 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1314 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1315 1316 /* 1317 * Error handling functions 1318 */ 1319 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1320 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1321 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1322 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1323 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1324 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1325 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1326 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1327 1328 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1329 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1330 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1331 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1332 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1333 struct sd_xbuf *xp, size_t actual_len); 1334 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1335 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1336 1337 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1338 void *arg, int code); 1339 1340 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1341 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1342 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1343 uint8_t *sense_datap, 1344 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1345 static void sd_sense_key_not_ready(struct sd_lun *un, 1346 uint8_t *sense_datap, 1347 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1348 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1349 uint8_t *sense_datap, 1350 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1351 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1352 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1353 static void sd_sense_key_unit_attention(struct sd_lun *un, 1354 uint8_t *sense_datap, 1355 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1356 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1357 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1358 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1359 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1360 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1361 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1362 static void sd_sense_key_default(struct sd_lun *un, 1363 uint8_t *sense_datap, 1364 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1365 1366 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1367 void *arg, int flag); 1368 1369 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1370 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1371 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1372 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1373 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1374 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1375 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1376 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1377 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1378 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1379 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1380 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1381 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1382 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1383 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1384 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1385 1386 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1387 1388 static void sd_start_stop_unit_callback(void *arg); 1389 static void sd_start_stop_unit_task(void *arg); 1390 1391 static void sd_taskq_create(void); 1392 static void sd_taskq_delete(void); 1393 static void sd_media_change_task(void *arg); 1394 1395 static int sd_handle_mchange(struct sd_lun *un); 1396 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1397 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1398 uint32_t *lbap, int path_flag); 1399 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1400 uint32_t *lbap, int path_flag); 1401 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1402 int path_flag); 1403 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1404 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1405 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1406 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1407 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1408 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1409 uchar_t usr_cmd, uchar_t *usr_bufp); 1410 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1411 struct dk_callback *dkc); 1412 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1413 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1414 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1415 uchar_t *bufaddr, uint_t buflen, int path_flag); 1416 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1417 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1418 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag); 1419 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1420 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1421 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1422 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1423 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1424 size_t buflen, daddr_t start_block, int path_flag); 1425 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1426 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1427 path_flag) 1428 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1429 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1430 path_flag) 1431 1432 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1433 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1434 uint16_t param_ptr, int path_flag); 1435 1436 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1437 static void sd_free_rqs(struct sd_lun *un); 1438 1439 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1440 uchar_t *data, int len, int fmt); 1441 static void sd_panic_for_res_conflict(struct sd_lun *un); 1442 1443 /* 1444 * Disk Ioctl Function Prototypes 1445 */ 1446 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1447 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1448 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1449 1450 /* 1451 * Multi-host Ioctl Prototypes 1452 */ 1453 static int sd_check_mhd(dev_t dev, int interval); 1454 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1455 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1456 static char *sd_sname(uchar_t status); 1457 static void sd_mhd_resvd_recover(void *arg); 1458 static void sd_resv_reclaim_thread(); 1459 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1460 static int sd_reserve_release(dev_t dev, int cmd); 1461 static void sd_rmv_resv_reclaim_req(dev_t dev); 1462 static void sd_mhd_reset_notify_cb(caddr_t arg); 1463 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1464 mhioc_inkeys_t *usrp, int flag); 1465 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1466 mhioc_inresvs_t *usrp, int flag); 1467 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1468 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1469 static int sd_mhdioc_release(dev_t dev); 1470 static int sd_mhdioc_register_devid(dev_t dev); 1471 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1472 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1473 1474 /* 1475 * SCSI removable prototypes 1476 */ 1477 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1478 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1479 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1480 static int sr_pause_resume(dev_t dev, int mode); 1481 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1482 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1483 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1484 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1485 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1486 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1487 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1488 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1489 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1490 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1491 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1492 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1493 static int sr_eject(dev_t dev); 1494 static void sr_ejected(register struct sd_lun *un); 1495 static int sr_check_wp(dev_t dev); 1496 static int sd_check_media(dev_t dev, enum dkio_state state); 1497 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1498 static void sd_delayed_cv_broadcast(void *arg); 1499 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1500 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1501 1502 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1503 1504 /* 1505 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1506 */ 1507 static void sd_check_for_writable_cd(struct sd_lun *un, int path_flag); 1508 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1509 static void sd_wm_cache_destructor(void *wm, void *un); 1510 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1511 daddr_t endb, ushort_t typ); 1512 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1513 daddr_t endb); 1514 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1515 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1516 static void sd_read_modify_write_task(void * arg); 1517 static int 1518 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1519 struct buf **bpp); 1520 1521 1522 /* 1523 * Function prototypes for failfast support. 1524 */ 1525 static void sd_failfast_flushq(struct sd_lun *un); 1526 static int sd_failfast_flushq_callback(struct buf *bp); 1527 1528 /* 1529 * Function prototypes to check for lsi devices 1530 */ 1531 static void sd_is_lsi(struct sd_lun *un); 1532 1533 /* 1534 * Function prototypes for partial DMA support 1535 */ 1536 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1537 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1538 1539 1540 /* Function prototypes for cmlb */ 1541 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 1542 diskaddr_t start_block, size_t reqlength, void *tg_cookie); 1543 1544 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie); 1545 1546 /* 1547 * Constants for failfast support: 1548 * 1549 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1550 * failfast processing being performed. 1551 * 1552 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1553 * failfast processing on all bufs with B_FAILFAST set. 1554 */ 1555 1556 #define SD_FAILFAST_INACTIVE 0 1557 #define SD_FAILFAST_ACTIVE 1 1558 1559 /* 1560 * Bitmask to control behavior of buf(9S) flushes when a transition to 1561 * the failfast state occurs. Optional bits include: 1562 * 1563 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1564 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1565 * be flushed. 1566 * 1567 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1568 * driver, in addition to the regular wait queue. This includes the xbuf 1569 * queues. When clear, only the driver's wait queue will be flushed. 1570 */ 1571 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1572 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1573 1574 /* 1575 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1576 * to flush all queues within the driver. 1577 */ 1578 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1579 1580 1581 /* 1582 * SD Testing Fault Injection 1583 */ 1584 #ifdef SD_FAULT_INJECTION 1585 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1586 static void sd_faultinjection(struct scsi_pkt *pktp); 1587 static void sd_injection_log(char *buf, struct sd_lun *un); 1588 #endif 1589 1590 /* 1591 * Device driver ops vector 1592 */ 1593 static struct cb_ops sd_cb_ops = { 1594 sdopen, /* open */ 1595 sdclose, /* close */ 1596 sdstrategy, /* strategy */ 1597 nodev, /* print */ 1598 sddump, /* dump */ 1599 sdread, /* read */ 1600 sdwrite, /* write */ 1601 sdioctl, /* ioctl */ 1602 nodev, /* devmap */ 1603 nodev, /* mmap */ 1604 nodev, /* segmap */ 1605 nochpoll, /* poll */ 1606 sd_prop_op, /* cb_prop_op */ 1607 0, /* streamtab */ 1608 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1609 CB_REV, /* cb_rev */ 1610 sdaread, /* async I/O read entry point */ 1611 sdawrite /* async I/O write entry point */ 1612 }; 1613 1614 static struct dev_ops sd_ops = { 1615 DEVO_REV, /* devo_rev, */ 1616 0, /* refcnt */ 1617 sdinfo, /* info */ 1618 nulldev, /* identify */ 1619 sdprobe, /* probe */ 1620 sdattach, /* attach */ 1621 sddetach, /* detach */ 1622 nodev, /* reset */ 1623 &sd_cb_ops, /* driver operations */ 1624 NULL, /* bus operations */ 1625 sdpower /* power */ 1626 }; 1627 1628 1629 /* 1630 * This is the loadable module wrapper. 1631 */ 1632 #include <sys/modctl.h> 1633 1634 static struct modldrv modldrv = { 1635 &mod_driverops, /* Type of module. This one is a driver */ 1636 SD_MODULE_NAME, /* Module name. */ 1637 &sd_ops /* driver ops */ 1638 }; 1639 1640 1641 static struct modlinkage modlinkage = { 1642 MODREV_1, 1643 &modldrv, 1644 NULL 1645 }; 1646 1647 static cmlb_tg_ops_t sd_tgops = { 1648 TG_DK_OPS_VERSION_1, 1649 sd_tg_rdwr, 1650 sd_tg_getinfo 1651 }; 1652 1653 static struct scsi_asq_key_strings sd_additional_codes[] = { 1654 0x81, 0, "Logical Unit is Reserved", 1655 0x85, 0, "Audio Address Not Valid", 1656 0xb6, 0, "Media Load Mechanism Failed", 1657 0xB9, 0, "Audio Play Operation Aborted", 1658 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1659 0x53, 2, "Medium removal prevented", 1660 0x6f, 0, "Authentication failed during key exchange", 1661 0x6f, 1, "Key not present", 1662 0x6f, 2, "Key not established", 1663 0x6f, 3, "Read without proper authentication", 1664 0x6f, 4, "Mismatched region to this logical unit", 1665 0x6f, 5, "Region reset count error", 1666 0xffff, 0x0, NULL 1667 }; 1668 1669 1670 /* 1671 * Struct for passing printing information for sense data messages 1672 */ 1673 struct sd_sense_info { 1674 int ssi_severity; 1675 int ssi_pfa_flag; 1676 }; 1677 1678 /* 1679 * Table of function pointers for iostart-side routines. Separate "chains" 1680 * of layered function calls are formed by placing the function pointers 1681 * sequentially in the desired order. Functions are called according to an 1682 * incrementing table index ordering. The last function in each chain must 1683 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1684 * in the sd_iodone_chain[] array. 1685 * 1686 * Note: It may seem more natural to organize both the iostart and iodone 1687 * functions together, into an array of structures (or some similar 1688 * organization) with a common index, rather than two separate arrays which 1689 * must be maintained in synchronization. The purpose of this division is 1690 * to achieve improved performance: individual arrays allows for more 1691 * effective cache line utilization on certain platforms. 1692 */ 1693 1694 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1695 1696 1697 static sd_chain_t sd_iostart_chain[] = { 1698 1699 /* Chain for buf IO for disk drive targets (PM enabled) */ 1700 sd_mapblockaddr_iostart, /* Index: 0 */ 1701 sd_pm_iostart, /* Index: 1 */ 1702 sd_core_iostart, /* Index: 2 */ 1703 1704 /* Chain for buf IO for disk drive targets (PM disabled) */ 1705 sd_mapblockaddr_iostart, /* Index: 3 */ 1706 sd_core_iostart, /* Index: 4 */ 1707 1708 /* Chain for buf IO for removable-media targets (PM enabled) */ 1709 sd_mapblockaddr_iostart, /* Index: 5 */ 1710 sd_mapblocksize_iostart, /* Index: 6 */ 1711 sd_pm_iostart, /* Index: 7 */ 1712 sd_core_iostart, /* Index: 8 */ 1713 1714 /* Chain for buf IO for removable-media targets (PM disabled) */ 1715 sd_mapblockaddr_iostart, /* Index: 9 */ 1716 sd_mapblocksize_iostart, /* Index: 10 */ 1717 sd_core_iostart, /* Index: 11 */ 1718 1719 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1720 sd_mapblockaddr_iostart, /* Index: 12 */ 1721 sd_checksum_iostart, /* Index: 13 */ 1722 sd_pm_iostart, /* Index: 14 */ 1723 sd_core_iostart, /* Index: 15 */ 1724 1725 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1726 sd_mapblockaddr_iostart, /* Index: 16 */ 1727 sd_checksum_iostart, /* Index: 17 */ 1728 sd_core_iostart, /* Index: 18 */ 1729 1730 /* Chain for USCSI commands (all targets) */ 1731 sd_pm_iostart, /* Index: 19 */ 1732 sd_core_iostart, /* Index: 20 */ 1733 1734 /* Chain for checksumming USCSI commands (all targets) */ 1735 sd_checksum_uscsi_iostart, /* Index: 21 */ 1736 sd_pm_iostart, /* Index: 22 */ 1737 sd_core_iostart, /* Index: 23 */ 1738 1739 /* Chain for "direct" USCSI commands (all targets) */ 1740 sd_core_iostart, /* Index: 24 */ 1741 1742 /* Chain for "direct priority" USCSI commands (all targets) */ 1743 sd_core_iostart, /* Index: 25 */ 1744 }; 1745 1746 /* 1747 * Macros to locate the first function of each iostart chain in the 1748 * sd_iostart_chain[] array. These are located by the index in the array. 1749 */ 1750 #define SD_CHAIN_DISK_IOSTART 0 1751 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1752 #define SD_CHAIN_RMMEDIA_IOSTART 5 1753 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1754 #define SD_CHAIN_CHKSUM_IOSTART 12 1755 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1756 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1757 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1758 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1759 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1760 1761 1762 /* 1763 * Table of function pointers for the iodone-side routines for the driver- 1764 * internal layering mechanism. The calling sequence for iodone routines 1765 * uses a decrementing table index, so the last routine called in a chain 1766 * must be at the lowest array index location for that chain. The last 1767 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1768 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1769 * of the functions in an iodone side chain must correspond to the ordering 1770 * of the iostart routines for that chain. Note that there is no iodone 1771 * side routine that corresponds to sd_core_iostart(), so there is no 1772 * entry in the table for this. 1773 */ 1774 1775 static sd_chain_t sd_iodone_chain[] = { 1776 1777 /* Chain for buf IO for disk drive targets (PM enabled) */ 1778 sd_buf_iodone, /* Index: 0 */ 1779 sd_mapblockaddr_iodone, /* Index: 1 */ 1780 sd_pm_iodone, /* Index: 2 */ 1781 1782 /* Chain for buf IO for disk drive targets (PM disabled) */ 1783 sd_buf_iodone, /* Index: 3 */ 1784 sd_mapblockaddr_iodone, /* Index: 4 */ 1785 1786 /* Chain for buf IO for removable-media targets (PM enabled) */ 1787 sd_buf_iodone, /* Index: 5 */ 1788 sd_mapblockaddr_iodone, /* Index: 6 */ 1789 sd_mapblocksize_iodone, /* Index: 7 */ 1790 sd_pm_iodone, /* Index: 8 */ 1791 1792 /* Chain for buf IO for removable-media targets (PM disabled) */ 1793 sd_buf_iodone, /* Index: 9 */ 1794 sd_mapblockaddr_iodone, /* Index: 10 */ 1795 sd_mapblocksize_iodone, /* Index: 11 */ 1796 1797 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1798 sd_buf_iodone, /* Index: 12 */ 1799 sd_mapblockaddr_iodone, /* Index: 13 */ 1800 sd_checksum_iodone, /* Index: 14 */ 1801 sd_pm_iodone, /* Index: 15 */ 1802 1803 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1804 sd_buf_iodone, /* Index: 16 */ 1805 sd_mapblockaddr_iodone, /* Index: 17 */ 1806 sd_checksum_iodone, /* Index: 18 */ 1807 1808 /* Chain for USCSI commands (non-checksum targets) */ 1809 sd_uscsi_iodone, /* Index: 19 */ 1810 sd_pm_iodone, /* Index: 20 */ 1811 1812 /* Chain for USCSI commands (checksum targets) */ 1813 sd_uscsi_iodone, /* Index: 21 */ 1814 sd_checksum_uscsi_iodone, /* Index: 22 */ 1815 sd_pm_iodone, /* Index: 22 */ 1816 1817 /* Chain for "direct" USCSI commands (all targets) */ 1818 sd_uscsi_iodone, /* Index: 24 */ 1819 1820 /* Chain for "direct priority" USCSI commands (all targets) */ 1821 sd_uscsi_iodone, /* Index: 25 */ 1822 }; 1823 1824 1825 /* 1826 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1827 * each iodone-side chain. These are located by the array index, but as the 1828 * iodone side functions are called in a decrementing-index order, the 1829 * highest index number in each chain must be specified (as these correspond 1830 * to the first function in the iodone chain that will be called by the core 1831 * at IO completion time). 1832 */ 1833 1834 #define SD_CHAIN_DISK_IODONE 2 1835 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1836 #define SD_CHAIN_RMMEDIA_IODONE 8 1837 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1838 #define SD_CHAIN_CHKSUM_IODONE 15 1839 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1840 #define SD_CHAIN_USCSI_CMD_IODONE 20 1841 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1842 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1843 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1844 1845 1846 1847 1848 /* 1849 * Array to map a layering chain index to the appropriate initpkt routine. 1850 * The redundant entries are present so that the index used for accessing 1851 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1852 * with this table as well. 1853 */ 1854 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1855 1856 static sd_initpkt_t sd_initpkt_map[] = { 1857 1858 /* Chain for buf IO for disk drive targets (PM enabled) */ 1859 sd_initpkt_for_buf, /* Index: 0 */ 1860 sd_initpkt_for_buf, /* Index: 1 */ 1861 sd_initpkt_for_buf, /* Index: 2 */ 1862 1863 /* Chain for buf IO for disk drive targets (PM disabled) */ 1864 sd_initpkt_for_buf, /* Index: 3 */ 1865 sd_initpkt_for_buf, /* Index: 4 */ 1866 1867 /* Chain for buf IO for removable-media targets (PM enabled) */ 1868 sd_initpkt_for_buf, /* Index: 5 */ 1869 sd_initpkt_for_buf, /* Index: 6 */ 1870 sd_initpkt_for_buf, /* Index: 7 */ 1871 sd_initpkt_for_buf, /* Index: 8 */ 1872 1873 /* Chain for buf IO for removable-media targets (PM disabled) */ 1874 sd_initpkt_for_buf, /* Index: 9 */ 1875 sd_initpkt_for_buf, /* Index: 10 */ 1876 sd_initpkt_for_buf, /* Index: 11 */ 1877 1878 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1879 sd_initpkt_for_buf, /* Index: 12 */ 1880 sd_initpkt_for_buf, /* Index: 13 */ 1881 sd_initpkt_for_buf, /* Index: 14 */ 1882 sd_initpkt_for_buf, /* Index: 15 */ 1883 1884 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1885 sd_initpkt_for_buf, /* Index: 16 */ 1886 sd_initpkt_for_buf, /* Index: 17 */ 1887 sd_initpkt_for_buf, /* Index: 18 */ 1888 1889 /* Chain for USCSI commands (non-checksum targets) */ 1890 sd_initpkt_for_uscsi, /* Index: 19 */ 1891 sd_initpkt_for_uscsi, /* Index: 20 */ 1892 1893 /* Chain for USCSI commands (checksum targets) */ 1894 sd_initpkt_for_uscsi, /* Index: 21 */ 1895 sd_initpkt_for_uscsi, /* Index: 22 */ 1896 sd_initpkt_for_uscsi, /* Index: 22 */ 1897 1898 /* Chain for "direct" USCSI commands (all targets) */ 1899 sd_initpkt_for_uscsi, /* Index: 24 */ 1900 1901 /* Chain for "direct priority" USCSI commands (all targets) */ 1902 sd_initpkt_for_uscsi, /* Index: 25 */ 1903 1904 }; 1905 1906 1907 /* 1908 * Array to map a layering chain index to the appropriate destroypktpkt routine. 1909 * The redundant entries are present so that the index used for accessing 1910 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1911 * with this table as well. 1912 */ 1913 typedef void (*sd_destroypkt_t)(struct buf *); 1914 1915 static sd_destroypkt_t sd_destroypkt_map[] = { 1916 1917 /* Chain for buf IO for disk drive targets (PM enabled) */ 1918 sd_destroypkt_for_buf, /* Index: 0 */ 1919 sd_destroypkt_for_buf, /* Index: 1 */ 1920 sd_destroypkt_for_buf, /* Index: 2 */ 1921 1922 /* Chain for buf IO for disk drive targets (PM disabled) */ 1923 sd_destroypkt_for_buf, /* Index: 3 */ 1924 sd_destroypkt_for_buf, /* Index: 4 */ 1925 1926 /* Chain for buf IO for removable-media targets (PM enabled) */ 1927 sd_destroypkt_for_buf, /* Index: 5 */ 1928 sd_destroypkt_for_buf, /* Index: 6 */ 1929 sd_destroypkt_for_buf, /* Index: 7 */ 1930 sd_destroypkt_for_buf, /* Index: 8 */ 1931 1932 /* Chain for buf IO for removable-media targets (PM disabled) */ 1933 sd_destroypkt_for_buf, /* Index: 9 */ 1934 sd_destroypkt_for_buf, /* Index: 10 */ 1935 sd_destroypkt_for_buf, /* Index: 11 */ 1936 1937 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1938 sd_destroypkt_for_buf, /* Index: 12 */ 1939 sd_destroypkt_for_buf, /* Index: 13 */ 1940 sd_destroypkt_for_buf, /* Index: 14 */ 1941 sd_destroypkt_for_buf, /* Index: 15 */ 1942 1943 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1944 sd_destroypkt_for_buf, /* Index: 16 */ 1945 sd_destroypkt_for_buf, /* Index: 17 */ 1946 sd_destroypkt_for_buf, /* Index: 18 */ 1947 1948 /* Chain for USCSI commands (non-checksum targets) */ 1949 sd_destroypkt_for_uscsi, /* Index: 19 */ 1950 sd_destroypkt_for_uscsi, /* Index: 20 */ 1951 1952 /* Chain for USCSI commands (checksum targets) */ 1953 sd_destroypkt_for_uscsi, /* Index: 21 */ 1954 sd_destroypkt_for_uscsi, /* Index: 22 */ 1955 sd_destroypkt_for_uscsi, /* Index: 22 */ 1956 1957 /* Chain for "direct" USCSI commands (all targets) */ 1958 sd_destroypkt_for_uscsi, /* Index: 24 */ 1959 1960 /* Chain for "direct priority" USCSI commands (all targets) */ 1961 sd_destroypkt_for_uscsi, /* Index: 25 */ 1962 1963 }; 1964 1965 1966 1967 /* 1968 * Array to map a layering chain index to the appropriate chain "type". 1969 * The chain type indicates a specific property/usage of the chain. 1970 * The redundant entries are present so that the index used for accessing 1971 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1972 * with this table as well. 1973 */ 1974 1975 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 1976 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 1977 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 1978 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 1979 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 1980 /* (for error recovery) */ 1981 1982 static int sd_chain_type_map[] = { 1983 1984 /* Chain for buf IO for disk drive targets (PM enabled) */ 1985 SD_CHAIN_BUFIO, /* Index: 0 */ 1986 SD_CHAIN_BUFIO, /* Index: 1 */ 1987 SD_CHAIN_BUFIO, /* Index: 2 */ 1988 1989 /* Chain for buf IO for disk drive targets (PM disabled) */ 1990 SD_CHAIN_BUFIO, /* Index: 3 */ 1991 SD_CHAIN_BUFIO, /* Index: 4 */ 1992 1993 /* Chain for buf IO for removable-media targets (PM enabled) */ 1994 SD_CHAIN_BUFIO, /* Index: 5 */ 1995 SD_CHAIN_BUFIO, /* Index: 6 */ 1996 SD_CHAIN_BUFIO, /* Index: 7 */ 1997 SD_CHAIN_BUFIO, /* Index: 8 */ 1998 1999 /* Chain for buf IO for removable-media targets (PM disabled) */ 2000 SD_CHAIN_BUFIO, /* Index: 9 */ 2001 SD_CHAIN_BUFIO, /* Index: 10 */ 2002 SD_CHAIN_BUFIO, /* Index: 11 */ 2003 2004 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2005 SD_CHAIN_BUFIO, /* Index: 12 */ 2006 SD_CHAIN_BUFIO, /* Index: 13 */ 2007 SD_CHAIN_BUFIO, /* Index: 14 */ 2008 SD_CHAIN_BUFIO, /* Index: 15 */ 2009 2010 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2011 SD_CHAIN_BUFIO, /* Index: 16 */ 2012 SD_CHAIN_BUFIO, /* Index: 17 */ 2013 SD_CHAIN_BUFIO, /* Index: 18 */ 2014 2015 /* Chain for USCSI commands (non-checksum targets) */ 2016 SD_CHAIN_USCSI, /* Index: 19 */ 2017 SD_CHAIN_USCSI, /* Index: 20 */ 2018 2019 /* Chain for USCSI commands (checksum targets) */ 2020 SD_CHAIN_USCSI, /* Index: 21 */ 2021 SD_CHAIN_USCSI, /* Index: 22 */ 2022 SD_CHAIN_USCSI, /* Index: 22 */ 2023 2024 /* Chain for "direct" USCSI commands (all targets) */ 2025 SD_CHAIN_DIRECT, /* Index: 24 */ 2026 2027 /* Chain for "direct priority" USCSI commands (all targets) */ 2028 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2029 }; 2030 2031 2032 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2033 #define SD_IS_BUFIO(xp) \ 2034 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2035 2036 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2037 #define SD_IS_DIRECT_PRIORITY(xp) \ 2038 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2039 2040 2041 2042 /* 2043 * Struct, array, and macros to map a specific chain to the appropriate 2044 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2045 * 2046 * The sd_chain_index_map[] array is used at attach time to set the various 2047 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2048 * chain to be used with the instance. This allows different instances to use 2049 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2050 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2051 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2052 * dynamically & without the use of locking; and (2) a layer may update the 2053 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2054 * to allow for deferred processing of an IO within the same chain from a 2055 * different execution context. 2056 */ 2057 2058 struct sd_chain_index { 2059 int sci_iostart_index; 2060 int sci_iodone_index; 2061 }; 2062 2063 static struct sd_chain_index sd_chain_index_map[] = { 2064 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2065 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2066 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2067 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2068 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2069 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2070 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2071 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2072 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2073 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2074 }; 2075 2076 2077 /* 2078 * The following are indexes into the sd_chain_index_map[] array. 2079 */ 2080 2081 /* un->un_buf_chain_type must be set to one of these */ 2082 #define SD_CHAIN_INFO_DISK 0 2083 #define SD_CHAIN_INFO_DISK_NO_PM 1 2084 #define SD_CHAIN_INFO_RMMEDIA 2 2085 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2086 #define SD_CHAIN_INFO_CHKSUM 4 2087 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2088 2089 /* un->un_uscsi_chain_type must be set to one of these */ 2090 #define SD_CHAIN_INFO_USCSI_CMD 6 2091 /* USCSI with PM disabled is the same as DIRECT */ 2092 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2093 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2094 2095 /* un->un_direct_chain_type must be set to one of these */ 2096 #define SD_CHAIN_INFO_DIRECT_CMD 8 2097 2098 /* un->un_priority_chain_type must be set to one of these */ 2099 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2100 2101 /* size for devid inquiries */ 2102 #define MAX_INQUIRY_SIZE 0xF0 2103 2104 /* 2105 * Macros used by functions to pass a given buf(9S) struct along to the 2106 * next function in the layering chain for further processing. 2107 * 2108 * In the following macros, passing more than three arguments to the called 2109 * routines causes the optimizer for the SPARC compiler to stop doing tail 2110 * call elimination which results in significant performance degradation. 2111 */ 2112 #define SD_BEGIN_IOSTART(index, un, bp) \ 2113 ((*(sd_iostart_chain[index]))(index, un, bp)) 2114 2115 #define SD_BEGIN_IODONE(index, un, bp) \ 2116 ((*(sd_iodone_chain[index]))(index, un, bp)) 2117 2118 #define SD_NEXT_IOSTART(index, un, bp) \ 2119 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2120 2121 #define SD_NEXT_IODONE(index, un, bp) \ 2122 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2123 2124 /* 2125 * Function: _init 2126 * 2127 * Description: This is the driver _init(9E) entry point. 2128 * 2129 * Return Code: Returns the value from mod_install(9F) or 2130 * ddi_soft_state_init(9F) as appropriate. 2131 * 2132 * Context: Called when driver module loaded. 2133 */ 2134 2135 int 2136 _init(void) 2137 { 2138 int err; 2139 2140 /* establish driver name from module name */ 2141 sd_label = mod_modname(&modlinkage); 2142 2143 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2144 SD_MAXUNIT); 2145 2146 if (err != 0) { 2147 return (err); 2148 } 2149 2150 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2151 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2152 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2153 2154 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2155 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2156 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2157 2158 /* 2159 * it's ok to init here even for fibre device 2160 */ 2161 sd_scsi_probe_cache_init(); 2162 2163 sd_scsi_target_lun_init(); 2164 2165 /* 2166 * Creating taskq before mod_install ensures that all callers (threads) 2167 * that enter the module after a successfull mod_install encounter 2168 * a valid taskq. 2169 */ 2170 sd_taskq_create(); 2171 2172 err = mod_install(&modlinkage); 2173 if (err != 0) { 2174 /* delete taskq if install fails */ 2175 sd_taskq_delete(); 2176 2177 mutex_destroy(&sd_detach_mutex); 2178 mutex_destroy(&sd_log_mutex); 2179 mutex_destroy(&sd_label_mutex); 2180 2181 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2182 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2183 cv_destroy(&sd_tr.srq_inprocess_cv); 2184 2185 sd_scsi_probe_cache_fini(); 2186 2187 sd_scsi_target_lun_fini(); 2188 2189 ddi_soft_state_fini(&sd_state); 2190 return (err); 2191 } 2192 2193 return (err); 2194 } 2195 2196 2197 /* 2198 * Function: _fini 2199 * 2200 * Description: This is the driver _fini(9E) entry point. 2201 * 2202 * Return Code: Returns the value from mod_remove(9F) 2203 * 2204 * Context: Called when driver module is unloaded. 2205 */ 2206 2207 int 2208 _fini(void) 2209 { 2210 int err; 2211 2212 if ((err = mod_remove(&modlinkage)) != 0) { 2213 return (err); 2214 } 2215 2216 sd_taskq_delete(); 2217 2218 mutex_destroy(&sd_detach_mutex); 2219 mutex_destroy(&sd_log_mutex); 2220 mutex_destroy(&sd_label_mutex); 2221 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2222 2223 sd_scsi_probe_cache_fini(); 2224 2225 sd_scsi_target_lun_fini(); 2226 2227 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2228 cv_destroy(&sd_tr.srq_inprocess_cv); 2229 2230 ddi_soft_state_fini(&sd_state); 2231 2232 return (err); 2233 } 2234 2235 2236 /* 2237 * Function: _info 2238 * 2239 * Description: This is the driver _info(9E) entry point. 2240 * 2241 * Arguments: modinfop - pointer to the driver modinfo structure 2242 * 2243 * Return Code: Returns the value from mod_info(9F). 2244 * 2245 * Context: Kernel thread context 2246 */ 2247 2248 int 2249 _info(struct modinfo *modinfop) 2250 { 2251 return (mod_info(&modlinkage, modinfop)); 2252 } 2253 2254 2255 /* 2256 * The following routines implement the driver message logging facility. 2257 * They provide component- and level- based debug output filtering. 2258 * Output may also be restricted to messages for a single instance by 2259 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2260 * to NULL, then messages for all instances are printed. 2261 * 2262 * These routines have been cloned from each other due to the language 2263 * constraints of macros and variable argument list processing. 2264 */ 2265 2266 2267 /* 2268 * Function: sd_log_err 2269 * 2270 * Description: This routine is called by the SD_ERROR macro for debug 2271 * logging of error conditions. 2272 * 2273 * Arguments: comp - driver component being logged 2274 * dev - pointer to driver info structure 2275 * fmt - error string and format to be logged 2276 */ 2277 2278 static void 2279 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2280 { 2281 va_list ap; 2282 dev_info_t *dev; 2283 2284 ASSERT(un != NULL); 2285 dev = SD_DEVINFO(un); 2286 ASSERT(dev != NULL); 2287 2288 /* 2289 * Filter messages based on the global component and level masks. 2290 * Also print if un matches the value of sd_debug_un, or if 2291 * sd_debug_un is set to NULL. 2292 */ 2293 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2294 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2295 mutex_enter(&sd_log_mutex); 2296 va_start(ap, fmt); 2297 (void) vsprintf(sd_log_buf, fmt, ap); 2298 va_end(ap); 2299 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2300 mutex_exit(&sd_log_mutex); 2301 } 2302 #ifdef SD_FAULT_INJECTION 2303 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2304 if (un->sd_injection_mask & comp) { 2305 mutex_enter(&sd_log_mutex); 2306 va_start(ap, fmt); 2307 (void) vsprintf(sd_log_buf, fmt, ap); 2308 va_end(ap); 2309 sd_injection_log(sd_log_buf, un); 2310 mutex_exit(&sd_log_mutex); 2311 } 2312 #endif 2313 } 2314 2315 2316 /* 2317 * Function: sd_log_info 2318 * 2319 * Description: This routine is called by the SD_INFO macro for debug 2320 * logging of general purpose informational conditions. 2321 * 2322 * Arguments: comp - driver component being logged 2323 * dev - pointer to driver info structure 2324 * fmt - info string and format to be logged 2325 */ 2326 2327 static void 2328 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2329 { 2330 va_list ap; 2331 dev_info_t *dev; 2332 2333 ASSERT(un != NULL); 2334 dev = SD_DEVINFO(un); 2335 ASSERT(dev != NULL); 2336 2337 /* 2338 * Filter messages based on the global component and level masks. 2339 * Also print if un matches the value of sd_debug_un, or if 2340 * sd_debug_un is set to NULL. 2341 */ 2342 if ((sd_component_mask & component) && 2343 (sd_level_mask & SD_LOGMASK_INFO) && 2344 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2345 mutex_enter(&sd_log_mutex); 2346 va_start(ap, fmt); 2347 (void) vsprintf(sd_log_buf, fmt, ap); 2348 va_end(ap); 2349 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2350 mutex_exit(&sd_log_mutex); 2351 } 2352 #ifdef SD_FAULT_INJECTION 2353 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2354 if (un->sd_injection_mask & component) { 2355 mutex_enter(&sd_log_mutex); 2356 va_start(ap, fmt); 2357 (void) vsprintf(sd_log_buf, fmt, ap); 2358 va_end(ap); 2359 sd_injection_log(sd_log_buf, un); 2360 mutex_exit(&sd_log_mutex); 2361 } 2362 #endif 2363 } 2364 2365 2366 /* 2367 * Function: sd_log_trace 2368 * 2369 * Description: This routine is called by the SD_TRACE macro for debug 2370 * logging of trace conditions (i.e. function entry/exit). 2371 * 2372 * Arguments: comp - driver component being logged 2373 * dev - pointer to driver info structure 2374 * fmt - trace string and format to be logged 2375 */ 2376 2377 static void 2378 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2379 { 2380 va_list ap; 2381 dev_info_t *dev; 2382 2383 ASSERT(un != NULL); 2384 dev = SD_DEVINFO(un); 2385 ASSERT(dev != NULL); 2386 2387 /* 2388 * Filter messages based on the global component and level masks. 2389 * Also print if un matches the value of sd_debug_un, or if 2390 * sd_debug_un is set to NULL. 2391 */ 2392 if ((sd_component_mask & component) && 2393 (sd_level_mask & SD_LOGMASK_TRACE) && 2394 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2395 mutex_enter(&sd_log_mutex); 2396 va_start(ap, fmt); 2397 (void) vsprintf(sd_log_buf, fmt, ap); 2398 va_end(ap); 2399 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2400 mutex_exit(&sd_log_mutex); 2401 } 2402 #ifdef SD_FAULT_INJECTION 2403 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2404 if (un->sd_injection_mask & component) { 2405 mutex_enter(&sd_log_mutex); 2406 va_start(ap, fmt); 2407 (void) vsprintf(sd_log_buf, fmt, ap); 2408 va_end(ap); 2409 sd_injection_log(sd_log_buf, un); 2410 mutex_exit(&sd_log_mutex); 2411 } 2412 #endif 2413 } 2414 2415 2416 /* 2417 * Function: sdprobe 2418 * 2419 * Description: This is the driver probe(9e) entry point function. 2420 * 2421 * Arguments: devi - opaque device info handle 2422 * 2423 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2424 * DDI_PROBE_FAILURE: If the probe failed. 2425 * DDI_PROBE_PARTIAL: If the instance is not present now, 2426 * but may be present in the future. 2427 */ 2428 2429 static int 2430 sdprobe(dev_info_t *devi) 2431 { 2432 struct scsi_device *devp; 2433 int rval; 2434 int instance; 2435 2436 /* 2437 * if it wasn't for pln, sdprobe could actually be nulldev 2438 * in the "__fibre" case. 2439 */ 2440 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2441 return (DDI_PROBE_DONTCARE); 2442 } 2443 2444 devp = ddi_get_driver_private(devi); 2445 2446 if (devp == NULL) { 2447 /* Ooops... nexus driver is mis-configured... */ 2448 return (DDI_PROBE_FAILURE); 2449 } 2450 2451 instance = ddi_get_instance(devi); 2452 2453 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2454 return (DDI_PROBE_PARTIAL); 2455 } 2456 2457 /* 2458 * Call the SCSA utility probe routine to see if we actually 2459 * have a target at this SCSI nexus. 2460 */ 2461 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2462 case SCSIPROBE_EXISTS: 2463 switch (devp->sd_inq->inq_dtype) { 2464 case DTYPE_DIRECT: 2465 rval = DDI_PROBE_SUCCESS; 2466 break; 2467 case DTYPE_RODIRECT: 2468 /* CDs etc. Can be removable media */ 2469 rval = DDI_PROBE_SUCCESS; 2470 break; 2471 case DTYPE_OPTICAL: 2472 /* 2473 * Rewritable optical driver HP115AA 2474 * Can also be removable media 2475 */ 2476 2477 /* 2478 * Do not attempt to bind to DTYPE_OPTICAL if 2479 * pre solaris 9 sparc sd behavior is required 2480 * 2481 * If first time through and sd_dtype_optical_bind 2482 * has not been set in /etc/system check properties 2483 */ 2484 2485 if (sd_dtype_optical_bind < 0) { 2486 sd_dtype_optical_bind = ddi_prop_get_int 2487 (DDI_DEV_T_ANY, devi, 0, 2488 "optical-device-bind", 1); 2489 } 2490 2491 if (sd_dtype_optical_bind == 0) { 2492 rval = DDI_PROBE_FAILURE; 2493 } else { 2494 rval = DDI_PROBE_SUCCESS; 2495 } 2496 break; 2497 2498 case DTYPE_NOTPRESENT: 2499 default: 2500 rval = DDI_PROBE_FAILURE; 2501 break; 2502 } 2503 break; 2504 default: 2505 rval = DDI_PROBE_PARTIAL; 2506 break; 2507 } 2508 2509 /* 2510 * This routine checks for resource allocation prior to freeing, 2511 * so it will take care of the "smart probing" case where a 2512 * scsi_probe() may or may not have been issued and will *not* 2513 * free previously-freed resources. 2514 */ 2515 scsi_unprobe(devp); 2516 return (rval); 2517 } 2518 2519 2520 /* 2521 * Function: sdinfo 2522 * 2523 * Description: This is the driver getinfo(9e) entry point function. 2524 * Given the device number, return the devinfo pointer from 2525 * the scsi_device structure or the instance number 2526 * associated with the dev_t. 2527 * 2528 * Arguments: dip - pointer to device info structure 2529 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2530 * DDI_INFO_DEVT2INSTANCE) 2531 * arg - driver dev_t 2532 * resultp - user buffer for request response 2533 * 2534 * Return Code: DDI_SUCCESS 2535 * DDI_FAILURE 2536 */ 2537 /* ARGSUSED */ 2538 static int 2539 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2540 { 2541 struct sd_lun *un; 2542 dev_t dev; 2543 int instance; 2544 int error; 2545 2546 switch (infocmd) { 2547 case DDI_INFO_DEVT2DEVINFO: 2548 dev = (dev_t)arg; 2549 instance = SDUNIT(dev); 2550 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2551 return (DDI_FAILURE); 2552 } 2553 *result = (void *) SD_DEVINFO(un); 2554 error = DDI_SUCCESS; 2555 break; 2556 case DDI_INFO_DEVT2INSTANCE: 2557 dev = (dev_t)arg; 2558 instance = SDUNIT(dev); 2559 *result = (void *)(uintptr_t)instance; 2560 error = DDI_SUCCESS; 2561 break; 2562 default: 2563 error = DDI_FAILURE; 2564 } 2565 return (error); 2566 } 2567 2568 /* 2569 * Function: sd_prop_op 2570 * 2571 * Description: This is the driver prop_op(9e) entry point function. 2572 * Return the number of blocks for the partition in question 2573 * or forward the request to the property facilities. 2574 * 2575 * Arguments: dev - device number 2576 * dip - pointer to device info structure 2577 * prop_op - property operator 2578 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2579 * name - pointer to property name 2580 * valuep - pointer or address of the user buffer 2581 * lengthp - property length 2582 * 2583 * Return Code: DDI_PROP_SUCCESS 2584 * DDI_PROP_NOT_FOUND 2585 * DDI_PROP_UNDEFINED 2586 * DDI_PROP_NO_MEMORY 2587 * DDI_PROP_BUF_TOO_SMALL 2588 */ 2589 2590 static int 2591 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2592 char *name, caddr_t valuep, int *lengthp) 2593 { 2594 int instance = ddi_get_instance(dip); 2595 struct sd_lun *un; 2596 uint64_t nblocks64; 2597 uint_t dblk; 2598 2599 /* 2600 * Our dynamic properties are all device specific and size oriented. 2601 * Requests issued under conditions where size is valid are passed 2602 * to ddi_prop_op_nblocks with the size information, otherwise the 2603 * request is passed to ddi_prop_op. Size depends on valid geometry. 2604 */ 2605 un = ddi_get_soft_state(sd_state, instance); 2606 if ((dev == DDI_DEV_T_ANY) || (un == NULL)) { 2607 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2608 name, valuep, lengthp)); 2609 } else if (!SD_IS_VALID_LABEL(un)) { 2610 return (ddi_prop_op(dev, dip, prop_op, mod_flags, name, 2611 valuep, lengthp)); 2612 } 2613 2614 /* get nblocks value */ 2615 ASSERT(!mutex_owned(SD_MUTEX(un))); 2616 2617 (void) cmlb_partinfo(un->un_cmlbhandle, SDPART(dev), 2618 (diskaddr_t *)&nblocks64, NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 2619 2620 /* report size in target size blocks */ 2621 dblk = un->un_tgt_blocksize / un->un_sys_blocksize; 2622 return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags, 2623 name, valuep, lengthp, nblocks64 / dblk, un->un_tgt_blocksize)); 2624 } 2625 2626 /* 2627 * The following functions are for smart probing: 2628 * sd_scsi_probe_cache_init() 2629 * sd_scsi_probe_cache_fini() 2630 * sd_scsi_clear_probe_cache() 2631 * sd_scsi_probe_with_cache() 2632 */ 2633 2634 /* 2635 * Function: sd_scsi_probe_cache_init 2636 * 2637 * Description: Initializes the probe response cache mutex and head pointer. 2638 * 2639 * Context: Kernel thread context 2640 */ 2641 2642 static void 2643 sd_scsi_probe_cache_init(void) 2644 { 2645 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2646 sd_scsi_probe_cache_head = NULL; 2647 } 2648 2649 2650 /* 2651 * Function: sd_scsi_probe_cache_fini 2652 * 2653 * Description: Frees all resources associated with the probe response cache. 2654 * 2655 * Context: Kernel thread context 2656 */ 2657 2658 static void 2659 sd_scsi_probe_cache_fini(void) 2660 { 2661 struct sd_scsi_probe_cache *cp; 2662 struct sd_scsi_probe_cache *ncp; 2663 2664 /* Clean up our smart probing linked list */ 2665 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2666 ncp = cp->next; 2667 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2668 } 2669 sd_scsi_probe_cache_head = NULL; 2670 mutex_destroy(&sd_scsi_probe_cache_mutex); 2671 } 2672 2673 2674 /* 2675 * Function: sd_scsi_clear_probe_cache 2676 * 2677 * Description: This routine clears the probe response cache. This is 2678 * done when open() returns ENXIO so that when deferred 2679 * attach is attempted (possibly after a device has been 2680 * turned on) we will retry the probe. Since we don't know 2681 * which target we failed to open, we just clear the 2682 * entire cache. 2683 * 2684 * Context: Kernel thread context 2685 */ 2686 2687 static void 2688 sd_scsi_clear_probe_cache(void) 2689 { 2690 struct sd_scsi_probe_cache *cp; 2691 int i; 2692 2693 mutex_enter(&sd_scsi_probe_cache_mutex); 2694 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2695 /* 2696 * Reset all entries to SCSIPROBE_EXISTS. This will 2697 * force probing to be performed the next time 2698 * sd_scsi_probe_with_cache is called. 2699 */ 2700 for (i = 0; i < NTARGETS_WIDE; i++) { 2701 cp->cache[i] = SCSIPROBE_EXISTS; 2702 } 2703 } 2704 mutex_exit(&sd_scsi_probe_cache_mutex); 2705 } 2706 2707 2708 /* 2709 * Function: sd_scsi_probe_with_cache 2710 * 2711 * Description: This routine implements support for a scsi device probe 2712 * with cache. The driver maintains a cache of the target 2713 * responses to scsi probes. If we get no response from a 2714 * target during a probe inquiry, we remember that, and we 2715 * avoid additional calls to scsi_probe on non-zero LUNs 2716 * on the same target until the cache is cleared. By doing 2717 * so we avoid the 1/4 sec selection timeout for nonzero 2718 * LUNs. lun0 of a target is always probed. 2719 * 2720 * Arguments: devp - Pointer to a scsi_device(9S) structure 2721 * waitfunc - indicates what the allocator routines should 2722 * do when resources are not available. This value 2723 * is passed on to scsi_probe() when that routine 2724 * is called. 2725 * 2726 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2727 * otherwise the value returned by scsi_probe(9F). 2728 * 2729 * Context: Kernel thread context 2730 */ 2731 2732 static int 2733 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2734 { 2735 struct sd_scsi_probe_cache *cp; 2736 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2737 int lun, tgt; 2738 2739 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2740 SCSI_ADDR_PROP_LUN, 0); 2741 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2742 SCSI_ADDR_PROP_TARGET, -1); 2743 2744 /* Make sure caching enabled and target in range */ 2745 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2746 /* do it the old way (no cache) */ 2747 return (scsi_probe(devp, waitfn)); 2748 } 2749 2750 mutex_enter(&sd_scsi_probe_cache_mutex); 2751 2752 /* Find the cache for this scsi bus instance */ 2753 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2754 if (cp->pdip == pdip) { 2755 break; 2756 } 2757 } 2758 2759 /* If we can't find a cache for this pdip, create one */ 2760 if (cp == NULL) { 2761 int i; 2762 2763 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2764 KM_SLEEP); 2765 cp->pdip = pdip; 2766 cp->next = sd_scsi_probe_cache_head; 2767 sd_scsi_probe_cache_head = cp; 2768 for (i = 0; i < NTARGETS_WIDE; i++) { 2769 cp->cache[i] = SCSIPROBE_EXISTS; 2770 } 2771 } 2772 2773 mutex_exit(&sd_scsi_probe_cache_mutex); 2774 2775 /* Recompute the cache for this target if LUN zero */ 2776 if (lun == 0) { 2777 cp->cache[tgt] = SCSIPROBE_EXISTS; 2778 } 2779 2780 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2781 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2782 return (SCSIPROBE_NORESP); 2783 } 2784 2785 /* Do the actual probe; save & return the result */ 2786 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2787 } 2788 2789 2790 /* 2791 * Function: sd_scsi_target_lun_init 2792 * 2793 * Description: Initializes the attached lun chain mutex and head pointer. 2794 * 2795 * Context: Kernel thread context 2796 */ 2797 2798 static void 2799 sd_scsi_target_lun_init(void) 2800 { 2801 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2802 sd_scsi_target_lun_head = NULL; 2803 } 2804 2805 2806 /* 2807 * Function: sd_scsi_target_lun_fini 2808 * 2809 * Description: Frees all resources associated with the attached lun 2810 * chain 2811 * 2812 * Context: Kernel thread context 2813 */ 2814 2815 static void 2816 sd_scsi_target_lun_fini(void) 2817 { 2818 struct sd_scsi_hba_tgt_lun *cp; 2819 struct sd_scsi_hba_tgt_lun *ncp; 2820 2821 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2822 ncp = cp->next; 2823 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2824 } 2825 sd_scsi_target_lun_head = NULL; 2826 mutex_destroy(&sd_scsi_target_lun_mutex); 2827 } 2828 2829 2830 /* 2831 * Function: sd_scsi_get_target_lun_count 2832 * 2833 * Description: This routine will check in the attached lun chain to see 2834 * how many luns are attached on the required SCSI controller 2835 * and target. Currently, some capabilities like tagged queue 2836 * are supported per target based by HBA. So all luns in a 2837 * target have the same capabilities. Based on this assumption, 2838 * sd should only set these capabilities once per target. This 2839 * function is called when sd needs to decide how many luns 2840 * already attached on a target. 2841 * 2842 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2843 * controller device. 2844 * target - The target ID on the controller's SCSI bus. 2845 * 2846 * Return Code: The number of luns attached on the required target and 2847 * controller. 2848 * -1 if target ID is not in parallel SCSI scope or the given 2849 * dip is not in the chain. 2850 * 2851 * Context: Kernel thread context 2852 */ 2853 2854 static int 2855 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2856 { 2857 struct sd_scsi_hba_tgt_lun *cp; 2858 2859 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2860 return (-1); 2861 } 2862 2863 mutex_enter(&sd_scsi_target_lun_mutex); 2864 2865 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2866 if (cp->pdip == dip) { 2867 break; 2868 } 2869 } 2870 2871 mutex_exit(&sd_scsi_target_lun_mutex); 2872 2873 if (cp == NULL) { 2874 return (-1); 2875 } 2876 2877 return (cp->nlun[target]); 2878 } 2879 2880 2881 /* 2882 * Function: sd_scsi_update_lun_on_target 2883 * 2884 * Description: This routine is used to update the attached lun chain when a 2885 * lun is attached or detached on a target. 2886 * 2887 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2888 * controller device. 2889 * target - The target ID on the controller's SCSI bus. 2890 * flag - Indicate the lun is attached or detached. 2891 * 2892 * Context: Kernel thread context 2893 */ 2894 2895 static void 2896 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 2897 { 2898 struct sd_scsi_hba_tgt_lun *cp; 2899 2900 mutex_enter(&sd_scsi_target_lun_mutex); 2901 2902 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2903 if (cp->pdip == dip) { 2904 break; 2905 } 2906 } 2907 2908 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 2909 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 2910 KM_SLEEP); 2911 cp->pdip = dip; 2912 cp->next = sd_scsi_target_lun_head; 2913 sd_scsi_target_lun_head = cp; 2914 } 2915 2916 mutex_exit(&sd_scsi_target_lun_mutex); 2917 2918 if (cp != NULL) { 2919 if (flag == SD_SCSI_LUN_ATTACH) { 2920 cp->nlun[target] ++; 2921 } else { 2922 cp->nlun[target] --; 2923 } 2924 } 2925 } 2926 2927 2928 /* 2929 * Function: sd_spin_up_unit 2930 * 2931 * Description: Issues the following commands to spin-up the device: 2932 * START STOP UNIT, and INQUIRY. 2933 * 2934 * Arguments: un - driver soft state (unit) structure 2935 * 2936 * Return Code: 0 - success 2937 * EIO - failure 2938 * EACCES - reservation conflict 2939 * 2940 * Context: Kernel thread context 2941 */ 2942 2943 static int 2944 sd_spin_up_unit(struct sd_lun *un) 2945 { 2946 size_t resid = 0; 2947 int has_conflict = FALSE; 2948 uchar_t *bufaddr; 2949 2950 ASSERT(un != NULL); 2951 2952 /* 2953 * Send a throwaway START UNIT command. 2954 * 2955 * If we fail on this, we don't care presently what precisely 2956 * is wrong. EMC's arrays will also fail this with a check 2957 * condition (0x2/0x4/0x3) if the device is "inactive," but 2958 * we don't want to fail the attach because it may become 2959 * "active" later. 2960 */ 2961 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 2962 == EACCES) 2963 has_conflict = TRUE; 2964 2965 /* 2966 * Send another INQUIRY command to the target. This is necessary for 2967 * non-removable media direct access devices because their INQUIRY data 2968 * may not be fully qualified until they are spun up (perhaps via the 2969 * START command above). Note: This seems to be needed for some 2970 * legacy devices only.) The INQUIRY command should succeed even if a 2971 * Reservation Conflict is present. 2972 */ 2973 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 2974 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 2975 kmem_free(bufaddr, SUN_INQSIZE); 2976 return (EIO); 2977 } 2978 2979 /* 2980 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 2981 * Note that this routine does not return a failure here even if the 2982 * INQUIRY command did not return any data. This is a legacy behavior. 2983 */ 2984 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 2985 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 2986 } 2987 2988 kmem_free(bufaddr, SUN_INQSIZE); 2989 2990 /* If we hit a reservation conflict above, tell the caller. */ 2991 if (has_conflict == TRUE) { 2992 return (EACCES); 2993 } 2994 2995 return (0); 2996 } 2997 2998 #ifdef _LP64 2999 /* 3000 * Function: sd_enable_descr_sense 3001 * 3002 * Description: This routine attempts to select descriptor sense format 3003 * using the Control mode page. Devices that support 64 bit 3004 * LBAs (for >2TB luns) should also implement descriptor 3005 * sense data so we will call this function whenever we see 3006 * a lun larger than 2TB. If for some reason the device 3007 * supports 64 bit LBAs but doesn't support descriptor sense 3008 * presumably the mode select will fail. Everything will 3009 * continue to work normally except that we will not get 3010 * complete sense data for commands that fail with an LBA 3011 * larger than 32 bits. 3012 * 3013 * Arguments: un - driver soft state (unit) structure 3014 * 3015 * Context: Kernel thread context only 3016 */ 3017 3018 static void 3019 sd_enable_descr_sense(struct sd_lun *un) 3020 { 3021 uchar_t *header; 3022 struct mode_control_scsi3 *ctrl_bufp; 3023 size_t buflen; 3024 size_t bd_len; 3025 3026 /* 3027 * Read MODE SENSE page 0xA, Control Mode Page 3028 */ 3029 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3030 sizeof (struct mode_control_scsi3); 3031 header = kmem_zalloc(buflen, KM_SLEEP); 3032 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3033 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3034 SD_ERROR(SD_LOG_COMMON, un, 3035 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3036 goto eds_exit; 3037 } 3038 3039 /* 3040 * Determine size of Block Descriptors in order to locate 3041 * the mode page data. ATAPI devices return 0, SCSI devices 3042 * should return MODE_BLK_DESC_LENGTH. 3043 */ 3044 bd_len = ((struct mode_header *)header)->bdesc_length; 3045 3046 /* Clear the mode data length field for MODE SELECT */ 3047 ((struct mode_header *)header)->length = 0; 3048 3049 ctrl_bufp = (struct mode_control_scsi3 *) 3050 (header + MODE_HEADER_LENGTH + bd_len); 3051 3052 /* 3053 * If the page length is smaller than the expected value, 3054 * the target device doesn't support D_SENSE. Bail out here. 3055 */ 3056 if (ctrl_bufp->mode_page.length < 3057 sizeof (struct mode_control_scsi3) - 2) { 3058 SD_ERROR(SD_LOG_COMMON, un, 3059 "sd_enable_descr_sense: enable D_SENSE failed\n"); 3060 goto eds_exit; 3061 } 3062 3063 /* 3064 * Clear PS bit for MODE SELECT 3065 */ 3066 ctrl_bufp->mode_page.ps = 0; 3067 3068 /* 3069 * Set D_SENSE to enable descriptor sense format. 3070 */ 3071 ctrl_bufp->d_sense = 1; 3072 3073 /* 3074 * Use MODE SELECT to commit the change to the D_SENSE bit 3075 */ 3076 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3077 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3078 SD_INFO(SD_LOG_COMMON, un, 3079 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3080 goto eds_exit; 3081 } 3082 3083 eds_exit: 3084 kmem_free(header, buflen); 3085 } 3086 3087 /* 3088 * Function: sd_reenable_dsense_task 3089 * 3090 * Description: Re-enable descriptor sense after device or bus reset 3091 * 3092 * Context: Executes in a taskq() thread context 3093 */ 3094 static void 3095 sd_reenable_dsense_task(void *arg) 3096 { 3097 struct sd_lun *un = arg; 3098 3099 ASSERT(un != NULL); 3100 sd_enable_descr_sense(un); 3101 } 3102 #endif /* _LP64 */ 3103 3104 /* 3105 * Function: sd_set_mmc_caps 3106 * 3107 * Description: This routine determines if the device is MMC compliant and if 3108 * the device supports CDDA via a mode sense of the CDVD 3109 * capabilities mode page. Also checks if the device is a 3110 * dvdram writable device. 3111 * 3112 * Arguments: un - driver soft state (unit) structure 3113 * 3114 * Context: Kernel thread context only 3115 */ 3116 3117 static void 3118 sd_set_mmc_caps(struct sd_lun *un) 3119 { 3120 struct mode_header_grp2 *sense_mhp; 3121 uchar_t *sense_page; 3122 caddr_t buf; 3123 int bd_len; 3124 int status; 3125 struct uscsi_cmd com; 3126 int rtn; 3127 uchar_t *out_data_rw, *out_data_hd; 3128 uchar_t *rqbuf_rw, *rqbuf_hd; 3129 3130 ASSERT(un != NULL); 3131 3132 /* 3133 * The flags which will be set in this function are - mmc compliant, 3134 * dvdram writable device, cdda support. Initialize them to FALSE 3135 * and if a capability is detected - it will be set to TRUE. 3136 */ 3137 un->un_f_mmc_cap = FALSE; 3138 un->un_f_dvdram_writable_device = FALSE; 3139 un->un_f_cfg_cdda = FALSE; 3140 3141 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3142 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3143 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3144 3145 if (status != 0) { 3146 /* command failed; just return */ 3147 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3148 return; 3149 } 3150 /* 3151 * If the mode sense request for the CDROM CAPABILITIES 3152 * page (0x2A) succeeds the device is assumed to be MMC. 3153 */ 3154 un->un_f_mmc_cap = TRUE; 3155 3156 /* Get to the page data */ 3157 sense_mhp = (struct mode_header_grp2 *)buf; 3158 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3159 sense_mhp->bdesc_length_lo; 3160 if (bd_len > MODE_BLK_DESC_LENGTH) { 3161 /* 3162 * We did not get back the expected block descriptor 3163 * length so we cannot determine if the device supports 3164 * CDDA. However, we still indicate the device is MMC 3165 * according to the successful response to the page 3166 * 0x2A mode sense request. 3167 */ 3168 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3169 "sd_set_mmc_caps: Mode Sense returned " 3170 "invalid block descriptor length\n"); 3171 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3172 return; 3173 } 3174 3175 /* See if read CDDA is supported */ 3176 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3177 bd_len); 3178 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3179 3180 /* See if writing DVD RAM is supported. */ 3181 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3182 if (un->un_f_dvdram_writable_device == TRUE) { 3183 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3184 return; 3185 } 3186 3187 /* 3188 * If the device presents DVD or CD capabilities in the mode 3189 * page, we can return here since a RRD will not have 3190 * these capabilities. 3191 */ 3192 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3193 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3194 return; 3195 } 3196 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3197 3198 /* 3199 * If un->un_f_dvdram_writable_device is still FALSE, 3200 * check for a Removable Rigid Disk (RRD). A RRD 3201 * device is identified by the features RANDOM_WRITABLE and 3202 * HARDWARE_DEFECT_MANAGEMENT. 3203 */ 3204 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3205 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3206 3207 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3208 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3209 RANDOM_WRITABLE, SD_PATH_STANDARD); 3210 if (rtn != 0) { 3211 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3212 kmem_free(rqbuf_rw, SENSE_LENGTH); 3213 return; 3214 } 3215 3216 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3217 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3218 3219 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3220 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3221 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD); 3222 if (rtn == 0) { 3223 /* 3224 * We have good information, check for random writable 3225 * and hardware defect features. 3226 */ 3227 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3228 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3229 un->un_f_dvdram_writable_device = TRUE; 3230 } 3231 } 3232 3233 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3234 kmem_free(rqbuf_rw, SENSE_LENGTH); 3235 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3236 kmem_free(rqbuf_hd, SENSE_LENGTH); 3237 } 3238 3239 /* 3240 * Function: sd_check_for_writable_cd 3241 * 3242 * Description: This routine determines if the media in the device is 3243 * writable or not. It uses the get configuration command (0x46) 3244 * to determine if the media is writable 3245 * 3246 * Arguments: un - driver soft state (unit) structure 3247 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" 3248 * chain and the normal command waitq, or 3249 * SD_PATH_DIRECT_PRIORITY to use the USCSI 3250 * "direct" chain and bypass the normal command 3251 * waitq. 3252 * 3253 * Context: Never called at interrupt context. 3254 */ 3255 3256 static void 3257 sd_check_for_writable_cd(struct sd_lun *un, int path_flag) 3258 { 3259 struct uscsi_cmd com; 3260 uchar_t *out_data; 3261 uchar_t *rqbuf; 3262 int rtn; 3263 uchar_t *out_data_rw, *out_data_hd; 3264 uchar_t *rqbuf_rw, *rqbuf_hd; 3265 struct mode_header_grp2 *sense_mhp; 3266 uchar_t *sense_page; 3267 caddr_t buf; 3268 int bd_len; 3269 int status; 3270 3271 ASSERT(un != NULL); 3272 ASSERT(mutex_owned(SD_MUTEX(un))); 3273 3274 /* 3275 * Initialize the writable media to false, if configuration info. 3276 * tells us otherwise then only we will set it. 3277 */ 3278 un->un_f_mmc_writable_media = FALSE; 3279 mutex_exit(SD_MUTEX(un)); 3280 3281 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3282 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3283 3284 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3285 out_data, SD_PROFILE_HEADER_LEN, path_flag); 3286 3287 mutex_enter(SD_MUTEX(un)); 3288 if (rtn == 0) { 3289 /* 3290 * We have good information, check for writable DVD. 3291 */ 3292 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3293 un->un_f_mmc_writable_media = TRUE; 3294 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3295 kmem_free(rqbuf, SENSE_LENGTH); 3296 return; 3297 } 3298 } 3299 3300 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3301 kmem_free(rqbuf, SENSE_LENGTH); 3302 3303 /* 3304 * Determine if this is a RRD type device. 3305 */ 3306 mutex_exit(SD_MUTEX(un)); 3307 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3308 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3309 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag); 3310 mutex_enter(SD_MUTEX(un)); 3311 if (status != 0) { 3312 /* command failed; just return */ 3313 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3314 return; 3315 } 3316 3317 /* Get to the page data */ 3318 sense_mhp = (struct mode_header_grp2 *)buf; 3319 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3320 if (bd_len > MODE_BLK_DESC_LENGTH) { 3321 /* 3322 * We did not get back the expected block descriptor length so 3323 * we cannot check the mode page. 3324 */ 3325 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3326 "sd_check_for_writable_cd: Mode Sense returned " 3327 "invalid block descriptor length\n"); 3328 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3329 return; 3330 } 3331 3332 /* 3333 * If the device presents DVD or CD capabilities in the mode 3334 * page, we can return here since a RRD device will not have 3335 * these capabilities. 3336 */ 3337 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3338 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3339 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3340 return; 3341 } 3342 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3343 3344 /* 3345 * If un->un_f_mmc_writable_media is still FALSE, 3346 * check for RRD type media. A RRD device is identified 3347 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3348 */ 3349 mutex_exit(SD_MUTEX(un)); 3350 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3351 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3352 3353 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3354 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3355 RANDOM_WRITABLE, path_flag); 3356 if (rtn != 0) { 3357 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3358 kmem_free(rqbuf_rw, SENSE_LENGTH); 3359 mutex_enter(SD_MUTEX(un)); 3360 return; 3361 } 3362 3363 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3364 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3365 3366 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3367 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3368 HARDWARE_DEFECT_MANAGEMENT, path_flag); 3369 mutex_enter(SD_MUTEX(un)); 3370 if (rtn == 0) { 3371 /* 3372 * We have good information, check for random writable 3373 * and hardware defect features as current. 3374 */ 3375 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3376 (out_data_rw[10] & 0x1) && 3377 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3378 (out_data_hd[10] & 0x1)) { 3379 un->un_f_mmc_writable_media = TRUE; 3380 } 3381 } 3382 3383 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3384 kmem_free(rqbuf_rw, SENSE_LENGTH); 3385 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3386 kmem_free(rqbuf_hd, SENSE_LENGTH); 3387 } 3388 3389 /* 3390 * Function: sd_read_unit_properties 3391 * 3392 * Description: The following implements a property lookup mechanism. 3393 * Properties for particular disks (keyed on vendor, model 3394 * and rev numbers) are sought in the sd.conf file via 3395 * sd_process_sdconf_file(), and if not found there, are 3396 * looked for in a list hardcoded in this driver via 3397 * sd_process_sdconf_table() Once located the properties 3398 * are used to update the driver unit structure. 3399 * 3400 * Arguments: un - driver soft state (unit) structure 3401 */ 3402 3403 static void 3404 sd_read_unit_properties(struct sd_lun *un) 3405 { 3406 /* 3407 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3408 * the "sd-config-list" property (from the sd.conf file) or if 3409 * there was not a match for the inquiry vid/pid. If this event 3410 * occurs the static driver configuration table is searched for 3411 * a match. 3412 */ 3413 ASSERT(un != NULL); 3414 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3415 sd_process_sdconf_table(un); 3416 } 3417 3418 /* check for LSI device */ 3419 sd_is_lsi(un); 3420 3421 3422 } 3423 3424 3425 /* 3426 * Function: sd_process_sdconf_file 3427 * 3428 * Description: Use ddi_getlongprop to obtain the properties from the 3429 * driver's config file (ie, sd.conf) and update the driver 3430 * soft state structure accordingly. 3431 * 3432 * Arguments: un - driver soft state (unit) structure 3433 * 3434 * Return Code: SD_SUCCESS - The properties were successfully set according 3435 * to the driver configuration file. 3436 * SD_FAILURE - The driver config list was not obtained or 3437 * there was no vid/pid match. This indicates that 3438 * the static config table should be used. 3439 * 3440 * The config file has a property, "sd-config-list", which consists of 3441 * one or more duplets as follows: 3442 * 3443 * sd-config-list= 3444 * <duplet>, 3445 * [<duplet>,] 3446 * [<duplet>]; 3447 * 3448 * The structure of each duplet is as follows: 3449 * 3450 * <duplet>:= <vid+pid>,<data-property-name_list> 3451 * 3452 * The first entry of the duplet is the device ID string (the concatenated 3453 * vid & pid; not to be confused with a device_id). This is defined in 3454 * the same way as in the sd_disk_table. 3455 * 3456 * The second part of the duplet is a string that identifies a 3457 * data-property-name-list. The data-property-name-list is defined as 3458 * follows: 3459 * 3460 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3461 * 3462 * The syntax of <data-property-name> depends on the <version> field. 3463 * 3464 * If version = SD_CONF_VERSION_1 we have the following syntax: 3465 * 3466 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3467 * 3468 * where the prop0 value will be used to set prop0 if bit0 set in the 3469 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3470 * 3471 */ 3472 3473 static int 3474 sd_process_sdconf_file(struct sd_lun *un) 3475 { 3476 char *config_list = NULL; 3477 int config_list_len; 3478 int len; 3479 int dupletlen = 0; 3480 char *vidptr; 3481 int vidlen; 3482 char *dnlist_ptr; 3483 char *dataname_ptr; 3484 int dnlist_len; 3485 int dataname_len; 3486 int *data_list; 3487 int data_list_len; 3488 int rval = SD_FAILURE; 3489 int i; 3490 3491 ASSERT(un != NULL); 3492 3493 /* Obtain the configuration list associated with the .conf file */ 3494 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3495 sd_config_list, (caddr_t)&config_list, &config_list_len) 3496 != DDI_PROP_SUCCESS) { 3497 return (SD_FAILURE); 3498 } 3499 3500 /* 3501 * Compare vids in each duplet to the inquiry vid - if a match is 3502 * made, get the data value and update the soft state structure 3503 * accordingly. 3504 * 3505 * Note: This algorithm is complex and difficult to maintain. It should 3506 * be replaced with a more robust implementation. 3507 */ 3508 for (len = config_list_len, vidptr = config_list; len > 0; 3509 vidptr += dupletlen, len -= dupletlen) { 3510 /* 3511 * Note: The assumption here is that each vid entry is on 3512 * a unique line from its associated duplet. 3513 */ 3514 vidlen = dupletlen = (int)strlen(vidptr); 3515 if ((vidlen == 0) || 3516 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3517 dupletlen++; 3518 continue; 3519 } 3520 3521 /* 3522 * dnlist contains 1 or more blank separated 3523 * data-property-name entries 3524 */ 3525 dnlist_ptr = vidptr + vidlen + 1; 3526 dnlist_len = (int)strlen(dnlist_ptr); 3527 dupletlen += dnlist_len + 2; 3528 3529 /* 3530 * Set a pointer for the first data-property-name 3531 * entry in the list 3532 */ 3533 dataname_ptr = dnlist_ptr; 3534 dataname_len = 0; 3535 3536 /* 3537 * Loop through all data-property-name entries in the 3538 * data-property-name-list setting the properties for each. 3539 */ 3540 while (dataname_len < dnlist_len) { 3541 int version; 3542 3543 /* 3544 * Determine the length of the current 3545 * data-property-name entry by indexing until a 3546 * blank or NULL is encountered. When the space is 3547 * encountered reset it to a NULL for compliance 3548 * with ddi_getlongprop(). 3549 */ 3550 for (i = 0; ((dataname_ptr[i] != ' ') && 3551 (dataname_ptr[i] != '\0')); i++) { 3552 ; 3553 } 3554 3555 dataname_len += i; 3556 /* If not null terminated, Make it so */ 3557 if (dataname_ptr[i] == ' ') { 3558 dataname_ptr[i] = '\0'; 3559 } 3560 dataname_len++; 3561 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3562 "sd_process_sdconf_file: disk:%s, data:%s\n", 3563 vidptr, dataname_ptr); 3564 3565 /* Get the data list */ 3566 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3567 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3568 != DDI_PROP_SUCCESS) { 3569 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3570 "sd_process_sdconf_file: data property (%s)" 3571 " has no value\n", dataname_ptr); 3572 dataname_ptr = dnlist_ptr + dataname_len; 3573 continue; 3574 } 3575 3576 version = data_list[0]; 3577 3578 if (version == SD_CONF_VERSION_1) { 3579 sd_tunables values; 3580 3581 /* Set the properties */ 3582 if (sd_chk_vers1_data(un, data_list[1], 3583 &data_list[2], data_list_len, dataname_ptr) 3584 == SD_SUCCESS) { 3585 sd_get_tunables_from_conf(un, 3586 data_list[1], &data_list[2], 3587 &values); 3588 sd_set_vers1_properties(un, 3589 data_list[1], &values); 3590 rval = SD_SUCCESS; 3591 } else { 3592 rval = SD_FAILURE; 3593 } 3594 } else { 3595 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3596 "data property %s version 0x%x is invalid.", 3597 dataname_ptr, version); 3598 rval = SD_FAILURE; 3599 } 3600 kmem_free(data_list, data_list_len); 3601 dataname_ptr = dnlist_ptr + dataname_len; 3602 } 3603 } 3604 3605 /* free up the memory allocated by ddi_getlongprop */ 3606 if (config_list) { 3607 kmem_free(config_list, config_list_len); 3608 } 3609 3610 return (rval); 3611 } 3612 3613 /* 3614 * Function: sd_get_tunables_from_conf() 3615 * 3616 * 3617 * This function reads the data list from the sd.conf file and pulls 3618 * the values that can have numeric values as arguments and places 3619 * the values in the appropriate sd_tunables member. 3620 * Since the order of the data list members varies across platforms 3621 * This function reads them from the data list in a platform specific 3622 * order and places them into the correct sd_tunable member that is 3623 * consistent across all platforms. 3624 */ 3625 static void 3626 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3627 sd_tunables *values) 3628 { 3629 int i; 3630 int mask; 3631 3632 bzero(values, sizeof (sd_tunables)); 3633 3634 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3635 3636 mask = 1 << i; 3637 if (mask > flags) { 3638 break; 3639 } 3640 3641 switch (mask & flags) { 3642 case 0: /* This mask bit not set in flags */ 3643 continue; 3644 case SD_CONF_BSET_THROTTLE: 3645 values->sdt_throttle = data_list[i]; 3646 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3647 "sd_get_tunables_from_conf: throttle = %d\n", 3648 values->sdt_throttle); 3649 break; 3650 case SD_CONF_BSET_CTYPE: 3651 values->sdt_ctype = data_list[i]; 3652 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3653 "sd_get_tunables_from_conf: ctype = %d\n", 3654 values->sdt_ctype); 3655 break; 3656 case SD_CONF_BSET_NRR_COUNT: 3657 values->sdt_not_rdy_retries = data_list[i]; 3658 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3659 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3660 values->sdt_not_rdy_retries); 3661 break; 3662 case SD_CONF_BSET_BSY_RETRY_COUNT: 3663 values->sdt_busy_retries = data_list[i]; 3664 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3665 "sd_get_tunables_from_conf: busy_retries = %d\n", 3666 values->sdt_busy_retries); 3667 break; 3668 case SD_CONF_BSET_RST_RETRIES: 3669 values->sdt_reset_retries = data_list[i]; 3670 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3671 "sd_get_tunables_from_conf: reset_retries = %d\n", 3672 values->sdt_reset_retries); 3673 break; 3674 case SD_CONF_BSET_RSV_REL_TIME: 3675 values->sdt_reserv_rel_time = data_list[i]; 3676 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3677 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3678 values->sdt_reserv_rel_time); 3679 break; 3680 case SD_CONF_BSET_MIN_THROTTLE: 3681 values->sdt_min_throttle = data_list[i]; 3682 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3683 "sd_get_tunables_from_conf: min_throttle = %d\n", 3684 values->sdt_min_throttle); 3685 break; 3686 case SD_CONF_BSET_DISKSORT_DISABLED: 3687 values->sdt_disk_sort_dis = data_list[i]; 3688 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3689 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3690 values->sdt_disk_sort_dis); 3691 break; 3692 case SD_CONF_BSET_LUN_RESET_ENABLED: 3693 values->sdt_lun_reset_enable = data_list[i]; 3694 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3695 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3696 "\n", values->sdt_lun_reset_enable); 3697 break; 3698 case SD_CONF_BSET_CACHE_IS_NV: 3699 values->sdt_suppress_cache_flush = data_list[i]; 3700 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3701 "sd_get_tunables_from_conf: \ 3702 suppress_cache_flush = %d" 3703 "\n", values->sdt_suppress_cache_flush); 3704 break; 3705 } 3706 } 3707 } 3708 3709 /* 3710 * Function: sd_process_sdconf_table 3711 * 3712 * Description: Search the static configuration table for a match on the 3713 * inquiry vid/pid and update the driver soft state structure 3714 * according to the table property values for the device. 3715 * 3716 * The form of a configuration table entry is: 3717 * <vid+pid>,<flags>,<property-data> 3718 * "SEAGATE ST42400N",1,0x40000, 3719 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1; 3720 * 3721 * Arguments: un - driver soft state (unit) structure 3722 */ 3723 3724 static void 3725 sd_process_sdconf_table(struct sd_lun *un) 3726 { 3727 char *id = NULL; 3728 int table_index; 3729 int idlen; 3730 3731 ASSERT(un != NULL); 3732 for (table_index = 0; table_index < sd_disk_table_size; 3733 table_index++) { 3734 id = sd_disk_table[table_index].device_id; 3735 idlen = strlen(id); 3736 if (idlen == 0) { 3737 continue; 3738 } 3739 3740 /* 3741 * The static configuration table currently does not 3742 * implement version 10 properties. Additionally, 3743 * multiple data-property-name entries are not 3744 * implemented in the static configuration table. 3745 */ 3746 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3747 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3748 "sd_process_sdconf_table: disk %s\n", id); 3749 sd_set_vers1_properties(un, 3750 sd_disk_table[table_index].flags, 3751 sd_disk_table[table_index].properties); 3752 break; 3753 } 3754 } 3755 } 3756 3757 3758 /* 3759 * Function: sd_sdconf_id_match 3760 * 3761 * Description: This local function implements a case sensitive vid/pid 3762 * comparison as well as the boundary cases of wild card and 3763 * multiple blanks. 3764 * 3765 * Note: An implicit assumption made here is that the scsi 3766 * inquiry structure will always keep the vid, pid and 3767 * revision strings in consecutive sequence, so they can be 3768 * read as a single string. If this assumption is not the 3769 * case, a separate string, to be used for the check, needs 3770 * to be built with these strings concatenated. 3771 * 3772 * Arguments: un - driver soft state (unit) structure 3773 * id - table or config file vid/pid 3774 * idlen - length of the vid/pid (bytes) 3775 * 3776 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3777 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3778 */ 3779 3780 static int 3781 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3782 { 3783 struct scsi_inquiry *sd_inq; 3784 int rval = SD_SUCCESS; 3785 3786 ASSERT(un != NULL); 3787 sd_inq = un->un_sd->sd_inq; 3788 ASSERT(id != NULL); 3789 3790 /* 3791 * We use the inq_vid as a pointer to a buffer containing the 3792 * vid and pid and use the entire vid/pid length of the table 3793 * entry for the comparison. This works because the inq_pid 3794 * data member follows inq_vid in the scsi_inquiry structure. 3795 */ 3796 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3797 /* 3798 * The user id string is compared to the inquiry vid/pid 3799 * using a case insensitive comparison and ignoring 3800 * multiple spaces. 3801 */ 3802 rval = sd_blank_cmp(un, id, idlen); 3803 if (rval != SD_SUCCESS) { 3804 /* 3805 * User id strings that start and end with a "*" 3806 * are a special case. These do not have a 3807 * specific vendor, and the product string can 3808 * appear anywhere in the 16 byte PID portion of 3809 * the inquiry data. This is a simple strstr() 3810 * type search for the user id in the inquiry data. 3811 */ 3812 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3813 char *pidptr = &id[1]; 3814 int i; 3815 int j; 3816 int pidstrlen = idlen - 2; 3817 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3818 pidstrlen; 3819 3820 if (j < 0) { 3821 return (SD_FAILURE); 3822 } 3823 for (i = 0; i < j; i++) { 3824 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3825 pidptr, pidstrlen) == 0) { 3826 rval = SD_SUCCESS; 3827 break; 3828 } 3829 } 3830 } 3831 } 3832 } 3833 return (rval); 3834 } 3835 3836 3837 /* 3838 * Function: sd_blank_cmp 3839 * 3840 * Description: If the id string starts and ends with a space, treat 3841 * multiple consecutive spaces as equivalent to a single 3842 * space. For example, this causes a sd_disk_table entry 3843 * of " NEC CDROM " to match a device's id string of 3844 * "NEC CDROM". 3845 * 3846 * Note: The success exit condition for this routine is if 3847 * the pointer to the table entry is '\0' and the cnt of 3848 * the inquiry length is zero. This will happen if the inquiry 3849 * string returned by the device is padded with spaces to be 3850 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3851 * SCSI spec states that the inquiry string is to be padded with 3852 * spaces. 3853 * 3854 * Arguments: un - driver soft state (unit) structure 3855 * id - table or config file vid/pid 3856 * idlen - length of the vid/pid (bytes) 3857 * 3858 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3859 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3860 */ 3861 3862 static int 3863 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3864 { 3865 char *p1; 3866 char *p2; 3867 int cnt; 3868 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3869 sizeof (SD_INQUIRY(un)->inq_pid); 3870 3871 ASSERT(un != NULL); 3872 p2 = un->un_sd->sd_inq->inq_vid; 3873 ASSERT(id != NULL); 3874 p1 = id; 3875 3876 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3877 /* 3878 * Note: string p1 is terminated by a NUL but string p2 3879 * isn't. The end of p2 is determined by cnt. 3880 */ 3881 for (;;) { 3882 /* skip over any extra blanks in both strings */ 3883 while ((*p1 != '\0') && (*p1 == ' ')) { 3884 p1++; 3885 } 3886 while ((cnt != 0) && (*p2 == ' ')) { 3887 p2++; 3888 cnt--; 3889 } 3890 3891 /* compare the two strings */ 3892 if ((cnt == 0) || 3893 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3894 break; 3895 } 3896 while ((cnt > 0) && 3897 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3898 p1++; 3899 p2++; 3900 cnt--; 3901 } 3902 } 3903 } 3904 3905 /* return SD_SUCCESS if both strings match */ 3906 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3907 } 3908 3909 3910 /* 3911 * Function: sd_chk_vers1_data 3912 * 3913 * Description: Verify the version 1 device properties provided by the 3914 * user via the configuration file 3915 * 3916 * Arguments: un - driver soft state (unit) structure 3917 * flags - integer mask indicating properties to be set 3918 * prop_list - integer list of property values 3919 * list_len - length of user provided data 3920 * 3921 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3922 * SD_FAILURE - Indicates the user provided data is invalid 3923 */ 3924 3925 static int 3926 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 3927 int list_len, char *dataname_ptr) 3928 { 3929 int i; 3930 int mask = 1; 3931 int index = 0; 3932 3933 ASSERT(un != NULL); 3934 3935 /* Check for a NULL property name and list */ 3936 if (dataname_ptr == NULL) { 3937 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3938 "sd_chk_vers1_data: NULL data property name."); 3939 return (SD_FAILURE); 3940 } 3941 if (prop_list == NULL) { 3942 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3943 "sd_chk_vers1_data: %s NULL data property list.", 3944 dataname_ptr); 3945 return (SD_FAILURE); 3946 } 3947 3948 /* Display a warning if undefined bits are set in the flags */ 3949 if (flags & ~SD_CONF_BIT_MASK) { 3950 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3951 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 3952 "Properties not set.", 3953 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 3954 return (SD_FAILURE); 3955 } 3956 3957 /* 3958 * Verify the length of the list by identifying the highest bit set 3959 * in the flags and validating that the property list has a length 3960 * up to the index of this bit. 3961 */ 3962 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3963 if (flags & mask) { 3964 index++; 3965 } 3966 mask = 1 << i; 3967 } 3968 if ((list_len / sizeof (int)) < (index + 2)) { 3969 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3970 "sd_chk_vers1_data: " 3971 "Data property list %s size is incorrect. " 3972 "Properties not set.", dataname_ptr); 3973 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 3974 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 3975 return (SD_FAILURE); 3976 } 3977 return (SD_SUCCESS); 3978 } 3979 3980 3981 /* 3982 * Function: sd_set_vers1_properties 3983 * 3984 * Description: Set version 1 device properties based on a property list 3985 * retrieved from the driver configuration file or static 3986 * configuration table. Version 1 properties have the format: 3987 * 3988 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3989 * 3990 * where the prop0 value will be used to set prop0 if bit0 3991 * is set in the flags 3992 * 3993 * Arguments: un - driver soft state (unit) structure 3994 * flags - integer mask indicating properties to be set 3995 * prop_list - integer list of property values 3996 */ 3997 3998 static void 3999 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4000 { 4001 ASSERT(un != NULL); 4002 4003 /* 4004 * Set the flag to indicate cache is to be disabled. An attempt 4005 * to disable the cache via sd_cache_control() will be made 4006 * later during attach once the basic initialization is complete. 4007 */ 4008 if (flags & SD_CONF_BSET_NOCACHE) { 4009 un->un_f_opt_disable_cache = TRUE; 4010 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4011 "sd_set_vers1_properties: caching disabled flag set\n"); 4012 } 4013 4014 /* CD-specific configuration parameters */ 4015 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4016 un->un_f_cfg_playmsf_bcd = TRUE; 4017 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4018 "sd_set_vers1_properties: playmsf_bcd set\n"); 4019 } 4020 if (flags & SD_CONF_BSET_READSUB_BCD) { 4021 un->un_f_cfg_readsub_bcd = TRUE; 4022 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4023 "sd_set_vers1_properties: readsub_bcd set\n"); 4024 } 4025 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4026 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4027 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4028 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4029 } 4030 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4031 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4032 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4033 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4034 } 4035 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4036 un->un_f_cfg_no_read_header = TRUE; 4037 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4038 "sd_set_vers1_properties: no_read_header set\n"); 4039 } 4040 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4041 un->un_f_cfg_read_cd_xd4 = TRUE; 4042 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4043 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4044 } 4045 4046 /* Support for devices which do not have valid/unique serial numbers */ 4047 if (flags & SD_CONF_BSET_FAB_DEVID) { 4048 un->un_f_opt_fab_devid = TRUE; 4049 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4050 "sd_set_vers1_properties: fab_devid bit set\n"); 4051 } 4052 4053 /* Support for user throttle configuration */ 4054 if (flags & SD_CONF_BSET_THROTTLE) { 4055 ASSERT(prop_list != NULL); 4056 un->un_saved_throttle = un->un_throttle = 4057 prop_list->sdt_throttle; 4058 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4059 "sd_set_vers1_properties: throttle set to %d\n", 4060 prop_list->sdt_throttle); 4061 } 4062 4063 /* Set the per disk retry count according to the conf file or table. */ 4064 if (flags & SD_CONF_BSET_NRR_COUNT) { 4065 ASSERT(prop_list != NULL); 4066 if (prop_list->sdt_not_rdy_retries) { 4067 un->un_notready_retry_count = 4068 prop_list->sdt_not_rdy_retries; 4069 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4070 "sd_set_vers1_properties: not ready retry count" 4071 " set to %d\n", un->un_notready_retry_count); 4072 } 4073 } 4074 4075 /* The controller type is reported for generic disk driver ioctls */ 4076 if (flags & SD_CONF_BSET_CTYPE) { 4077 ASSERT(prop_list != NULL); 4078 switch (prop_list->sdt_ctype) { 4079 case CTYPE_CDROM: 4080 un->un_ctype = prop_list->sdt_ctype; 4081 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4082 "sd_set_vers1_properties: ctype set to " 4083 "CTYPE_CDROM\n"); 4084 break; 4085 case CTYPE_CCS: 4086 un->un_ctype = prop_list->sdt_ctype; 4087 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4088 "sd_set_vers1_properties: ctype set to " 4089 "CTYPE_CCS\n"); 4090 break; 4091 case CTYPE_ROD: /* RW optical */ 4092 un->un_ctype = prop_list->sdt_ctype; 4093 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4094 "sd_set_vers1_properties: ctype set to " 4095 "CTYPE_ROD\n"); 4096 break; 4097 default: 4098 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4099 "sd_set_vers1_properties: Could not set " 4100 "invalid ctype value (%d)", 4101 prop_list->sdt_ctype); 4102 } 4103 } 4104 4105 /* Purple failover timeout */ 4106 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4107 ASSERT(prop_list != NULL); 4108 un->un_busy_retry_count = 4109 prop_list->sdt_busy_retries; 4110 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4111 "sd_set_vers1_properties: " 4112 "busy retry count set to %d\n", 4113 un->un_busy_retry_count); 4114 } 4115 4116 /* Purple reset retry count */ 4117 if (flags & SD_CONF_BSET_RST_RETRIES) { 4118 ASSERT(prop_list != NULL); 4119 un->un_reset_retry_count = 4120 prop_list->sdt_reset_retries; 4121 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4122 "sd_set_vers1_properties: " 4123 "reset retry count set to %d\n", 4124 un->un_reset_retry_count); 4125 } 4126 4127 /* Purple reservation release timeout */ 4128 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4129 ASSERT(prop_list != NULL); 4130 un->un_reserve_release_time = 4131 prop_list->sdt_reserv_rel_time; 4132 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4133 "sd_set_vers1_properties: " 4134 "reservation release timeout set to %d\n", 4135 un->un_reserve_release_time); 4136 } 4137 4138 /* 4139 * Driver flag telling the driver to verify that no commands are pending 4140 * for a device before issuing a Test Unit Ready. This is a workaround 4141 * for a firmware bug in some Seagate eliteI drives. 4142 */ 4143 if (flags & SD_CONF_BSET_TUR_CHECK) { 4144 un->un_f_cfg_tur_check = TRUE; 4145 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4146 "sd_set_vers1_properties: tur queue check set\n"); 4147 } 4148 4149 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4150 un->un_min_throttle = prop_list->sdt_min_throttle; 4151 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4152 "sd_set_vers1_properties: min throttle set to %d\n", 4153 un->un_min_throttle); 4154 } 4155 4156 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4157 un->un_f_disksort_disabled = 4158 (prop_list->sdt_disk_sort_dis != 0) ? 4159 TRUE : FALSE; 4160 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4161 "sd_set_vers1_properties: disksort disabled " 4162 "flag set to %d\n", 4163 prop_list->sdt_disk_sort_dis); 4164 } 4165 4166 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4167 un->un_f_lun_reset_enabled = 4168 (prop_list->sdt_lun_reset_enable != 0) ? 4169 TRUE : FALSE; 4170 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4171 "sd_set_vers1_properties: lun reset enabled " 4172 "flag set to %d\n", 4173 prop_list->sdt_lun_reset_enable); 4174 } 4175 4176 if (flags & SD_CONF_BSET_CACHE_IS_NV) { 4177 un->un_f_suppress_cache_flush = 4178 (prop_list->sdt_suppress_cache_flush != 0) ? 4179 TRUE : FALSE; 4180 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4181 "sd_set_vers1_properties: suppress_cache_flush " 4182 "flag set to %d\n", 4183 prop_list->sdt_suppress_cache_flush); 4184 } 4185 4186 /* 4187 * Validate the throttle values. 4188 * If any of the numbers are invalid, set everything to defaults. 4189 */ 4190 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4191 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4192 (un->un_min_throttle > un->un_throttle)) { 4193 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4194 un->un_min_throttle = sd_min_throttle; 4195 } 4196 } 4197 4198 /* 4199 * Function: sd_is_lsi() 4200 * 4201 * Description: Check for lsi devices, step through the static device 4202 * table to match vid/pid. 4203 * 4204 * Args: un - ptr to sd_lun 4205 * 4206 * Notes: When creating new LSI property, need to add the new LSI property 4207 * to this function. 4208 */ 4209 static void 4210 sd_is_lsi(struct sd_lun *un) 4211 { 4212 char *id = NULL; 4213 int table_index; 4214 int idlen; 4215 void *prop; 4216 4217 ASSERT(un != NULL); 4218 for (table_index = 0; table_index < sd_disk_table_size; 4219 table_index++) { 4220 id = sd_disk_table[table_index].device_id; 4221 idlen = strlen(id); 4222 if (idlen == 0) { 4223 continue; 4224 } 4225 4226 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4227 prop = sd_disk_table[table_index].properties; 4228 if (prop == &lsi_properties || 4229 prop == &lsi_oem_properties || 4230 prop == &lsi_properties_scsi || 4231 prop == &symbios_properties) { 4232 un->un_f_cfg_is_lsi = TRUE; 4233 } 4234 break; 4235 } 4236 } 4237 } 4238 4239 /* 4240 * Function: sd_get_physical_geometry 4241 * 4242 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 4243 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 4244 * target, and use this information to initialize the physical 4245 * geometry cache specified by pgeom_p. 4246 * 4247 * MODE SENSE is an optional command, so failure in this case 4248 * does not necessarily denote an error. We want to use the 4249 * MODE SENSE commands to derive the physical geometry of the 4250 * device, but if either command fails, the logical geometry is 4251 * used as the fallback for disk label geometry in cmlb. 4252 * 4253 * This requires that un->un_blockcount and un->un_tgt_blocksize 4254 * have already been initialized for the current target and 4255 * that the current values be passed as args so that we don't 4256 * end up ever trying to use -1 as a valid value. This could 4257 * happen if either value is reset while we're not holding 4258 * the mutex. 4259 * 4260 * Arguments: un - driver soft state (unit) structure 4261 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4262 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4263 * to use the USCSI "direct" chain and bypass the normal 4264 * command waitq. 4265 * 4266 * Context: Kernel thread only (can sleep). 4267 */ 4268 4269 static int 4270 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p, 4271 diskaddr_t capacity, int lbasize, int path_flag) 4272 { 4273 struct mode_format *page3p; 4274 struct mode_geometry *page4p; 4275 struct mode_header *headerp; 4276 int sector_size; 4277 int nsect; 4278 int nhead; 4279 int ncyl; 4280 int intrlv; 4281 int spc; 4282 diskaddr_t modesense_capacity; 4283 int rpm; 4284 int bd_len; 4285 int mode_header_length; 4286 uchar_t *p3bufp; 4287 uchar_t *p4bufp; 4288 int cdbsize; 4289 int ret = EIO; 4290 4291 ASSERT(un != NULL); 4292 4293 if (lbasize == 0) { 4294 if (ISCD(un)) { 4295 lbasize = 2048; 4296 } else { 4297 lbasize = un->un_sys_blocksize; 4298 } 4299 } 4300 pgeom_p->g_secsize = (unsigned short)lbasize; 4301 4302 /* 4303 * If the unit is a cd/dvd drive MODE SENSE page three 4304 * and MODE SENSE page four are reserved (see SBC spec 4305 * and MMC spec). To prevent soft errors just return 4306 * using the default LBA size. 4307 */ 4308 if (ISCD(un)) 4309 return (ret); 4310 4311 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 4312 4313 /* 4314 * Retrieve MODE SENSE page 3 - Format Device Page 4315 */ 4316 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 4317 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 4318 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 4319 != 0) { 4320 SD_ERROR(SD_LOG_COMMON, un, 4321 "sd_get_physical_geometry: mode sense page 3 failed\n"); 4322 goto page3_exit; 4323 } 4324 4325 /* 4326 * Determine size of Block Descriptors in order to locate the mode 4327 * page data. ATAPI devices return 0, SCSI devices should return 4328 * MODE_BLK_DESC_LENGTH. 4329 */ 4330 headerp = (struct mode_header *)p3bufp; 4331 if (un->un_f_cfg_is_atapi == TRUE) { 4332 struct mode_header_grp2 *mhp = 4333 (struct mode_header_grp2 *)headerp; 4334 mode_header_length = MODE_HEADER_LENGTH_GRP2; 4335 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4336 } else { 4337 mode_header_length = MODE_HEADER_LENGTH; 4338 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4339 } 4340 4341 if (bd_len > MODE_BLK_DESC_LENGTH) { 4342 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4343 "received unexpected bd_len of %d, page3\n", bd_len); 4344 goto page3_exit; 4345 } 4346 4347 page3p = (struct mode_format *) 4348 ((caddr_t)headerp + mode_header_length + bd_len); 4349 4350 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 4351 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4352 "mode sense pg3 code mismatch %d\n", 4353 page3p->mode_page.code); 4354 goto page3_exit; 4355 } 4356 4357 /* 4358 * Use this physical geometry data only if BOTH MODE SENSE commands 4359 * complete successfully; otherwise, revert to the logical geometry. 4360 * So, we need to save everything in temporary variables. 4361 */ 4362 sector_size = BE_16(page3p->data_bytes_sect); 4363 4364 /* 4365 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 4366 */ 4367 if (sector_size == 0) { 4368 sector_size = un->un_sys_blocksize; 4369 } else { 4370 sector_size &= ~(un->un_sys_blocksize - 1); 4371 } 4372 4373 nsect = BE_16(page3p->sect_track); 4374 intrlv = BE_16(page3p->interleave); 4375 4376 SD_INFO(SD_LOG_COMMON, un, 4377 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 4378 SD_INFO(SD_LOG_COMMON, un, 4379 " mode page: %d; nsect: %d; sector size: %d;\n", 4380 page3p->mode_page.code, nsect, sector_size); 4381 SD_INFO(SD_LOG_COMMON, un, 4382 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 4383 BE_16(page3p->track_skew), 4384 BE_16(page3p->cylinder_skew)); 4385 4386 4387 /* 4388 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 4389 */ 4390 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 4391 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 4392 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 4393 != 0) { 4394 SD_ERROR(SD_LOG_COMMON, un, 4395 "sd_get_physical_geometry: mode sense page 4 failed\n"); 4396 goto page4_exit; 4397 } 4398 4399 /* 4400 * Determine size of Block Descriptors in order to locate the mode 4401 * page data. ATAPI devices return 0, SCSI devices should return 4402 * MODE_BLK_DESC_LENGTH. 4403 */ 4404 headerp = (struct mode_header *)p4bufp; 4405 if (un->un_f_cfg_is_atapi == TRUE) { 4406 struct mode_header_grp2 *mhp = 4407 (struct mode_header_grp2 *)headerp; 4408 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 4409 } else { 4410 bd_len = ((struct mode_header *)headerp)->bdesc_length; 4411 } 4412 4413 if (bd_len > MODE_BLK_DESC_LENGTH) { 4414 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4415 "received unexpected bd_len of %d, page4\n", bd_len); 4416 goto page4_exit; 4417 } 4418 4419 page4p = (struct mode_geometry *) 4420 ((caddr_t)headerp + mode_header_length + bd_len); 4421 4422 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 4423 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 4424 "mode sense pg4 code mismatch %d\n", 4425 page4p->mode_page.code); 4426 goto page4_exit; 4427 } 4428 4429 /* 4430 * Stash the data now, after we know that both commands completed. 4431 */ 4432 4433 4434 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 4435 spc = nhead * nsect; 4436 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 4437 rpm = BE_16(page4p->rpm); 4438 4439 modesense_capacity = spc * ncyl; 4440 4441 SD_INFO(SD_LOG_COMMON, un, 4442 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 4443 SD_INFO(SD_LOG_COMMON, un, 4444 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 4445 SD_INFO(SD_LOG_COMMON, un, 4446 " computed capacity(h*s*c): %d;\n", modesense_capacity); 4447 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 4448 (void *)pgeom_p, capacity); 4449 4450 /* 4451 * Compensate if the drive's geometry is not rectangular, i.e., 4452 * the product of C * H * S returned by MODE SENSE >= that returned 4453 * by read capacity. This is an idiosyncrasy of the original x86 4454 * disk subsystem. 4455 */ 4456 if (modesense_capacity >= capacity) { 4457 SD_INFO(SD_LOG_COMMON, un, 4458 "sd_get_physical_geometry: adjusting acyl; " 4459 "old: %d; new: %d\n", pgeom_p->g_acyl, 4460 (modesense_capacity - capacity + spc - 1) / spc); 4461 if (sector_size != 0) { 4462 /* 1243403: NEC D38x7 drives don't support sec size */ 4463 pgeom_p->g_secsize = (unsigned short)sector_size; 4464 } 4465 pgeom_p->g_nsect = (unsigned short)nsect; 4466 pgeom_p->g_nhead = (unsigned short)nhead; 4467 pgeom_p->g_capacity = capacity; 4468 pgeom_p->g_acyl = 4469 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 4470 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 4471 } 4472 4473 pgeom_p->g_rpm = (unsigned short)rpm; 4474 pgeom_p->g_intrlv = (unsigned short)intrlv; 4475 ret = 0; 4476 4477 SD_INFO(SD_LOG_COMMON, un, 4478 "sd_get_physical_geometry: mode sense geometry:\n"); 4479 SD_INFO(SD_LOG_COMMON, un, 4480 " nsect: %d; sector size: %d; interlv: %d\n", 4481 nsect, sector_size, intrlv); 4482 SD_INFO(SD_LOG_COMMON, un, 4483 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 4484 nhead, ncyl, rpm, modesense_capacity); 4485 SD_INFO(SD_LOG_COMMON, un, 4486 "sd_get_physical_geometry: (cached)\n"); 4487 SD_INFO(SD_LOG_COMMON, un, 4488 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4489 pgeom_p->g_ncyl, pgeom_p->g_acyl, 4490 pgeom_p->g_nhead, pgeom_p->g_nsect); 4491 SD_INFO(SD_LOG_COMMON, un, 4492 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 4493 pgeom_p->g_secsize, pgeom_p->g_capacity, 4494 pgeom_p->g_intrlv, pgeom_p->g_rpm); 4495 4496 page4_exit: 4497 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 4498 page3_exit: 4499 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 4500 4501 return (ret); 4502 } 4503 4504 /* 4505 * Function: sd_get_virtual_geometry 4506 * 4507 * Description: Ask the controller to tell us about the target device. 4508 * 4509 * Arguments: un - pointer to softstate 4510 * capacity - disk capacity in #blocks 4511 * lbasize - disk block size in bytes 4512 * 4513 * Context: Kernel thread only 4514 */ 4515 4516 static int 4517 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p, 4518 diskaddr_t capacity, int lbasize) 4519 { 4520 uint_t geombuf; 4521 int spc; 4522 4523 ASSERT(un != NULL); 4524 4525 /* Set sector size, and total number of sectors */ 4526 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 4527 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 4528 4529 /* Let the HBA tell us its geometry */ 4530 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 4531 4532 /* A value of -1 indicates an undefined "geometry" property */ 4533 if (geombuf == (-1)) { 4534 return (EINVAL); 4535 } 4536 4537 /* Initialize the logical geometry cache. */ 4538 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 4539 lgeom_p->g_nsect = geombuf & 0xffff; 4540 lgeom_p->g_secsize = un->un_sys_blocksize; 4541 4542 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 4543 4544 /* 4545 * Note: The driver originally converted the capacity value from 4546 * target blocks to system blocks. However, the capacity value passed 4547 * to this routine is already in terms of system blocks (this scaling 4548 * is done when the READ CAPACITY command is issued and processed). 4549 * This 'error' may have gone undetected because the usage of g_ncyl 4550 * (which is based upon g_capacity) is very limited within the driver 4551 */ 4552 lgeom_p->g_capacity = capacity; 4553 4554 /* 4555 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 4556 * hba may return zero values if the device has been removed. 4557 */ 4558 if (spc == 0) { 4559 lgeom_p->g_ncyl = 0; 4560 } else { 4561 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 4562 } 4563 lgeom_p->g_acyl = 0; 4564 4565 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 4566 return (0); 4567 4568 } 4569 /* 4570 * Function: sd_update_block_info 4571 * 4572 * Description: Calculate a byte count to sector count bitshift value 4573 * from sector size. 4574 * 4575 * Arguments: un: unit struct. 4576 * lbasize: new target sector size 4577 * capacity: new target capacity, ie. block count 4578 * 4579 * Context: Kernel thread context 4580 */ 4581 4582 static void 4583 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 4584 { 4585 uint_t dblk; 4586 4587 if (lbasize != 0) { 4588 un->un_tgt_blocksize = lbasize; 4589 un->un_f_tgt_blocksize_is_valid = TRUE; 4590 } 4591 4592 if (capacity != 0) { 4593 un->un_blockcount = capacity; 4594 un->un_f_blockcount_is_valid = TRUE; 4595 } 4596 4597 /* 4598 * Update device capacity properties. 4599 * 4600 * 'device-nblocks' number of blocks in target's units 4601 * 'device-blksize' data bearing size of target's block 4602 * 4603 * NOTE: math is complicated by the fact that un_tgt_blocksize may 4604 * not be a power of two for checksumming disks with 520/528 byte 4605 * sectors. 4606 */ 4607 if (un->un_f_tgt_blocksize_is_valid && 4608 un->un_f_blockcount_is_valid && 4609 un->un_sys_blocksize) { 4610 dblk = un->un_tgt_blocksize / un->un_sys_blocksize; 4611 (void) ddi_prop_update_int64(DDI_DEV_T_NONE, SD_DEVINFO(un), 4612 "device-nblocks", un->un_blockcount / dblk); 4613 /* 4614 * To save memory, only define "device-blksize" when its 4615 * value is differnet than the default DEV_BSIZE value. 4616 */ 4617 if ((un->un_sys_blocksize * dblk) != DEV_BSIZE) 4618 (void) ddi_prop_update_int(DDI_DEV_T_NONE, 4619 SD_DEVINFO(un), "device-blksize", 4620 un->un_sys_blocksize * dblk); 4621 } 4622 } 4623 4624 4625 /* 4626 * Function: sd_register_devid 4627 * 4628 * Description: This routine will obtain the device id information from the 4629 * target, obtain the serial number, and register the device 4630 * id with the ddi framework. 4631 * 4632 * Arguments: devi - the system's dev_info_t for the device. 4633 * un - driver soft state (unit) structure 4634 * reservation_flag - indicates if a reservation conflict 4635 * occurred during attach 4636 * 4637 * Context: Kernel Thread 4638 */ 4639 static void 4640 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 4641 { 4642 int rval = 0; 4643 uchar_t *inq80 = NULL; 4644 size_t inq80_len = MAX_INQUIRY_SIZE; 4645 size_t inq80_resid = 0; 4646 uchar_t *inq83 = NULL; 4647 size_t inq83_len = MAX_INQUIRY_SIZE; 4648 size_t inq83_resid = 0; 4649 int dlen, len; 4650 char *sn; 4651 4652 ASSERT(un != NULL); 4653 ASSERT(mutex_owned(SD_MUTEX(un))); 4654 ASSERT((SD_DEVINFO(un)) == devi); 4655 4656 /* 4657 * If transport has already registered a devid for this target 4658 * then that takes precedence over the driver's determination 4659 * of the devid. 4660 */ 4661 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) { 4662 ASSERT(un->un_devid); 4663 return; /* use devid registered by the transport */ 4664 } 4665 4666 /* 4667 * This is the case of antiquated Sun disk drives that have the 4668 * FAB_DEVID property set in the disk_table. These drives 4669 * manage the devid's by storing them in last 2 available sectors 4670 * on the drive and have them fabricated by the ddi layer by calling 4671 * ddi_devid_init and passing the DEVID_FAB flag. 4672 */ 4673 if (un->un_f_opt_fab_devid == TRUE) { 4674 /* 4675 * Depending on EINVAL isn't reliable, since a reserved disk 4676 * may result in invalid geometry, so check to make sure a 4677 * reservation conflict did not occur during attach. 4678 */ 4679 if ((sd_get_devid(un) == EINVAL) && 4680 (reservation_flag != SD_TARGET_IS_RESERVED)) { 4681 /* 4682 * The devid is invalid AND there is no reservation 4683 * conflict. Fabricate a new devid. 4684 */ 4685 (void) sd_create_devid(un); 4686 } 4687 4688 /* Register the devid if it exists */ 4689 if (un->un_devid != NULL) { 4690 (void) ddi_devid_register(SD_DEVINFO(un), 4691 un->un_devid); 4692 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4693 "sd_register_devid: Devid Fabricated\n"); 4694 } 4695 return; 4696 } 4697 4698 /* 4699 * We check the availibility of the World Wide Name (0x83) and Unit 4700 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 4701 * un_vpd_page_mask from them, we decide which way to get the WWN. If 4702 * 0x83 is availible, that is the best choice. Our next choice is 4703 * 0x80. If neither are availible, we munge the devid from the device 4704 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 4705 * to fabricate a devid for non-Sun qualified disks. 4706 */ 4707 if (sd_check_vpd_page_support(un) == 0) { 4708 /* collect page 80 data if available */ 4709 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 4710 4711 mutex_exit(SD_MUTEX(un)); 4712 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 4713 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 4714 0x01, 0x80, &inq80_resid); 4715 4716 if (rval != 0) { 4717 kmem_free(inq80, inq80_len); 4718 inq80 = NULL; 4719 inq80_len = 0; 4720 } else if (ddi_prop_exists( 4721 DDI_DEV_T_NONE, SD_DEVINFO(un), 4722 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 4723 INQUIRY_SERIAL_NO) == 0) { 4724 /* 4725 * If we don't already have a serial number 4726 * property, do quick verify of data returned 4727 * and define property. 4728 */ 4729 dlen = inq80_len - inq80_resid; 4730 len = (size_t)inq80[3]; 4731 if ((dlen >= 4) && ((len + 4) <= dlen)) { 4732 /* 4733 * Ensure sn termination, skip leading 4734 * blanks, and create property 4735 * 'inquiry-serial-no'. 4736 */ 4737 sn = (char *)&inq80[4]; 4738 sn[len] = 0; 4739 while (*sn && (*sn == ' ')) 4740 sn++; 4741 if (*sn) { 4742 (void) ddi_prop_update_string( 4743 DDI_DEV_T_NONE, 4744 SD_DEVINFO(un), 4745 INQUIRY_SERIAL_NO, sn); 4746 } 4747 } 4748 } 4749 mutex_enter(SD_MUTEX(un)); 4750 } 4751 4752 /* collect page 83 data if available */ 4753 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 4754 mutex_exit(SD_MUTEX(un)); 4755 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 4756 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 4757 0x01, 0x83, &inq83_resid); 4758 4759 if (rval != 0) { 4760 kmem_free(inq83, inq83_len); 4761 inq83 = NULL; 4762 inq83_len = 0; 4763 } 4764 mutex_enter(SD_MUTEX(un)); 4765 } 4766 } 4767 4768 /* encode best devid possible based on data available */ 4769 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 4770 (char *)ddi_driver_name(SD_DEVINFO(un)), 4771 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 4772 inq80, inq80_len - inq80_resid, inq83, inq83_len - 4773 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 4774 4775 /* devid successfully encoded, register devid */ 4776 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 4777 4778 } else { 4779 /* 4780 * Unable to encode a devid based on data available. 4781 * This is not a Sun qualified disk. Older Sun disk 4782 * drives that have the SD_FAB_DEVID property 4783 * set in the disk_table and non Sun qualified 4784 * disks are treated in the same manner. These 4785 * drives manage the devid's by storing them in 4786 * last 2 available sectors on the drive and 4787 * have them fabricated by the ddi layer by 4788 * calling ddi_devid_init and passing the 4789 * DEVID_FAB flag. 4790 * Create a fabricate devid only if there's no 4791 * fabricate devid existed. 4792 */ 4793 if (sd_get_devid(un) == EINVAL) { 4794 (void) sd_create_devid(un); 4795 } 4796 un->un_f_opt_fab_devid = TRUE; 4797 4798 /* Register the devid if it exists */ 4799 if (un->un_devid != NULL) { 4800 (void) ddi_devid_register(SD_DEVINFO(un), 4801 un->un_devid); 4802 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4803 "sd_register_devid: devid fabricated using " 4804 "ddi framework\n"); 4805 } 4806 } 4807 4808 /* clean up resources */ 4809 if (inq80 != NULL) { 4810 kmem_free(inq80, inq80_len); 4811 } 4812 if (inq83 != NULL) { 4813 kmem_free(inq83, inq83_len); 4814 } 4815 } 4816 4817 4818 4819 /* 4820 * Function: sd_get_devid 4821 * 4822 * Description: This routine will return 0 if a valid device id has been 4823 * obtained from the target and stored in the soft state. If a 4824 * valid device id has not been previously read and stored, a 4825 * read attempt will be made. 4826 * 4827 * Arguments: un - driver soft state (unit) structure 4828 * 4829 * Return Code: 0 if we successfully get the device id 4830 * 4831 * Context: Kernel Thread 4832 */ 4833 4834 static int 4835 sd_get_devid(struct sd_lun *un) 4836 { 4837 struct dk_devid *dkdevid; 4838 ddi_devid_t tmpid; 4839 uint_t *ip; 4840 size_t sz; 4841 diskaddr_t blk; 4842 int status; 4843 int chksum; 4844 int i; 4845 size_t buffer_size; 4846 4847 ASSERT(un != NULL); 4848 ASSERT(mutex_owned(SD_MUTEX(un))); 4849 4850 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 4851 un); 4852 4853 if (un->un_devid != NULL) { 4854 return (0); 4855 } 4856 4857 mutex_exit(SD_MUTEX(un)); 4858 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 4859 (void *)SD_PATH_DIRECT) != 0) { 4860 mutex_enter(SD_MUTEX(un)); 4861 return (EINVAL); 4862 } 4863 4864 /* 4865 * Read and verify device id, stored in the reserved cylinders at the 4866 * end of the disk. Backup label is on the odd sectors of the last 4867 * track of the last cylinder. Device id will be on track of the next 4868 * to last cylinder. 4869 */ 4870 mutex_enter(SD_MUTEX(un)); 4871 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 4872 mutex_exit(SD_MUTEX(un)); 4873 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 4874 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 4875 SD_PATH_DIRECT); 4876 if (status != 0) { 4877 goto error; 4878 } 4879 4880 /* Validate the revision */ 4881 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 4882 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 4883 status = EINVAL; 4884 goto error; 4885 } 4886 4887 /* Calculate the checksum */ 4888 chksum = 0; 4889 ip = (uint_t *)dkdevid; 4890 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 4891 i++) { 4892 chksum ^= ip[i]; 4893 } 4894 4895 /* Compare the checksums */ 4896 if (DKD_GETCHKSUM(dkdevid) != chksum) { 4897 status = EINVAL; 4898 goto error; 4899 } 4900 4901 /* Validate the device id */ 4902 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 4903 status = EINVAL; 4904 goto error; 4905 } 4906 4907 /* 4908 * Store the device id in the driver soft state 4909 */ 4910 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 4911 tmpid = kmem_alloc(sz, KM_SLEEP); 4912 4913 mutex_enter(SD_MUTEX(un)); 4914 4915 un->un_devid = tmpid; 4916 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 4917 4918 kmem_free(dkdevid, buffer_size); 4919 4920 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 4921 4922 return (status); 4923 error: 4924 mutex_enter(SD_MUTEX(un)); 4925 kmem_free(dkdevid, buffer_size); 4926 return (status); 4927 } 4928 4929 4930 /* 4931 * Function: sd_create_devid 4932 * 4933 * Description: This routine will fabricate the device id and write it 4934 * to the disk. 4935 * 4936 * Arguments: un - driver soft state (unit) structure 4937 * 4938 * Return Code: value of the fabricated device id 4939 * 4940 * Context: Kernel Thread 4941 */ 4942 4943 static ddi_devid_t 4944 sd_create_devid(struct sd_lun *un) 4945 { 4946 ASSERT(un != NULL); 4947 4948 /* Fabricate the devid */ 4949 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 4950 == DDI_FAILURE) { 4951 return (NULL); 4952 } 4953 4954 /* Write the devid to disk */ 4955 if (sd_write_deviceid(un) != 0) { 4956 ddi_devid_free(un->un_devid); 4957 un->un_devid = NULL; 4958 } 4959 4960 return (un->un_devid); 4961 } 4962 4963 4964 /* 4965 * Function: sd_write_deviceid 4966 * 4967 * Description: This routine will write the device id to the disk 4968 * reserved sector. 4969 * 4970 * Arguments: un - driver soft state (unit) structure 4971 * 4972 * Return Code: EINVAL 4973 * value returned by sd_send_scsi_cmd 4974 * 4975 * Context: Kernel Thread 4976 */ 4977 4978 static int 4979 sd_write_deviceid(struct sd_lun *un) 4980 { 4981 struct dk_devid *dkdevid; 4982 diskaddr_t blk; 4983 uint_t *ip, chksum; 4984 int status; 4985 int i; 4986 4987 ASSERT(mutex_owned(SD_MUTEX(un))); 4988 4989 mutex_exit(SD_MUTEX(un)); 4990 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk, 4991 (void *)SD_PATH_DIRECT) != 0) { 4992 mutex_enter(SD_MUTEX(un)); 4993 return (-1); 4994 } 4995 4996 4997 /* Allocate the buffer */ 4998 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 4999 5000 /* Fill in the revision */ 5001 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 5002 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 5003 5004 /* Copy in the device id */ 5005 mutex_enter(SD_MUTEX(un)); 5006 bcopy(un->un_devid, &dkdevid->dkd_devid, 5007 ddi_devid_sizeof(un->un_devid)); 5008 mutex_exit(SD_MUTEX(un)); 5009 5010 /* Calculate the checksum */ 5011 chksum = 0; 5012 ip = (uint_t *)dkdevid; 5013 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 5014 i++) { 5015 chksum ^= ip[i]; 5016 } 5017 5018 /* Fill-in checksum */ 5019 DKD_FORMCHKSUM(chksum, dkdevid); 5020 5021 /* Write the reserved sector */ 5022 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 5023 SD_PATH_DIRECT); 5024 5025 kmem_free(dkdevid, un->un_sys_blocksize); 5026 5027 mutex_enter(SD_MUTEX(un)); 5028 return (status); 5029 } 5030 5031 5032 /* 5033 * Function: sd_check_vpd_page_support 5034 * 5035 * Description: This routine sends an inquiry command with the EVPD bit set and 5036 * a page code of 0x00 to the device. It is used to determine which 5037 * vital product pages are availible to find the devid. We are 5038 * looking for pages 0x83 or 0x80. If we return a negative 1, the 5039 * device does not support that command. 5040 * 5041 * Arguments: un - driver soft state (unit) structure 5042 * 5043 * Return Code: 0 - success 5044 * 1 - check condition 5045 * 5046 * Context: This routine can sleep. 5047 */ 5048 5049 static int 5050 sd_check_vpd_page_support(struct sd_lun *un) 5051 { 5052 uchar_t *page_list = NULL; 5053 uchar_t page_length = 0xff; /* Use max possible length */ 5054 uchar_t evpd = 0x01; /* Set the EVPD bit */ 5055 uchar_t page_code = 0x00; /* Supported VPD Pages */ 5056 int rval = 0; 5057 int counter; 5058 5059 ASSERT(un != NULL); 5060 ASSERT(mutex_owned(SD_MUTEX(un))); 5061 5062 mutex_exit(SD_MUTEX(un)); 5063 5064 /* 5065 * We'll set the page length to the maximum to save figuring it out 5066 * with an additional call. 5067 */ 5068 page_list = kmem_zalloc(page_length, KM_SLEEP); 5069 5070 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 5071 page_code, NULL); 5072 5073 mutex_enter(SD_MUTEX(un)); 5074 5075 /* 5076 * Now we must validate that the device accepted the command, as some 5077 * drives do not support it. If the drive does support it, we will 5078 * return 0, and the supported pages will be in un_vpd_page_mask. If 5079 * not, we return -1. 5080 */ 5081 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 5082 /* Loop to find one of the 2 pages we need */ 5083 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 5084 5085 /* 5086 * Pages are returned in ascending order, and 0x83 is what we 5087 * are hoping for. 5088 */ 5089 while ((page_list[counter] <= 0x86) && 5090 (counter <= (page_list[VPD_PAGE_LENGTH] + 5091 VPD_HEAD_OFFSET))) { 5092 /* 5093 * Add 3 because page_list[3] is the number of 5094 * pages minus 3 5095 */ 5096 5097 switch (page_list[counter]) { 5098 case 0x00: 5099 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 5100 break; 5101 case 0x80: 5102 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 5103 break; 5104 case 0x81: 5105 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 5106 break; 5107 case 0x82: 5108 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 5109 break; 5110 case 0x83: 5111 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 5112 break; 5113 case 0x86: 5114 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG; 5115 break; 5116 } 5117 counter++; 5118 } 5119 5120 } else { 5121 rval = -1; 5122 5123 SD_INFO(SD_LOG_ATTACH_DETACH, un, 5124 "sd_check_vpd_page_support: This drive does not implement " 5125 "VPD pages.\n"); 5126 } 5127 5128 kmem_free(page_list, page_length); 5129 5130 return (rval); 5131 } 5132 5133 5134 /* 5135 * Function: sd_setup_pm 5136 * 5137 * Description: Initialize Power Management on the device 5138 * 5139 * Context: Kernel Thread 5140 */ 5141 5142 static void 5143 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 5144 { 5145 uint_t log_page_size; 5146 uchar_t *log_page_data; 5147 int rval; 5148 5149 /* 5150 * Since we are called from attach, holding a mutex for 5151 * un is unnecessary. Because some of the routines called 5152 * from here require SD_MUTEX to not be held, assert this 5153 * right up front. 5154 */ 5155 ASSERT(!mutex_owned(SD_MUTEX(un))); 5156 /* 5157 * Since the sd device does not have the 'reg' property, 5158 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 5159 * The following code is to tell cpr that this device 5160 * DOES need to be suspended and resumed. 5161 */ 5162 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 5163 "pm-hardware-state", "needs-suspend-resume"); 5164 5165 /* 5166 * This complies with the new power management framework 5167 * for certain desktop machines. Create the pm_components 5168 * property as a string array property. 5169 */ 5170 if (un->un_f_pm_supported) { 5171 /* 5172 * not all devices have a motor, try it first. 5173 * some devices may return ILLEGAL REQUEST, some 5174 * will hang 5175 * The following START_STOP_UNIT is used to check if target 5176 * device has a motor. 5177 */ 5178 un->un_f_start_stop_supported = TRUE; 5179 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 5180 SD_PATH_DIRECT) != 0) { 5181 un->un_f_start_stop_supported = FALSE; 5182 } 5183 5184 /* 5185 * create pm properties anyways otherwise the parent can't 5186 * go to sleep 5187 */ 5188 (void) sd_create_pm_components(devi, un); 5189 un->un_f_pm_is_enabled = TRUE; 5190 return; 5191 } 5192 5193 if (!un->un_f_log_sense_supported) { 5194 un->un_power_level = SD_SPINDLE_ON; 5195 un->un_f_pm_is_enabled = FALSE; 5196 return; 5197 } 5198 5199 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 5200 5201 #ifdef SDDEBUG 5202 if (sd_force_pm_supported) { 5203 /* Force a successful result */ 5204 rval = 1; 5205 } 5206 #endif 5207 5208 /* 5209 * If the start-stop cycle counter log page is not supported 5210 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 5211 * then we should not create the pm_components property. 5212 */ 5213 if (rval == -1) { 5214 /* 5215 * Error. 5216 * Reading log sense failed, most likely this is 5217 * an older drive that does not support log sense. 5218 * If this fails auto-pm is not supported. 5219 */ 5220 un->un_power_level = SD_SPINDLE_ON; 5221 un->un_f_pm_is_enabled = FALSE; 5222 5223 } else if (rval == 0) { 5224 /* 5225 * Page not found. 5226 * The start stop cycle counter is implemented as page 5227 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 5228 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 5229 */ 5230 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 5231 /* 5232 * Page found, use this one. 5233 */ 5234 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 5235 un->un_f_pm_is_enabled = TRUE; 5236 } else { 5237 /* 5238 * Error or page not found. 5239 * auto-pm is not supported for this device. 5240 */ 5241 un->un_power_level = SD_SPINDLE_ON; 5242 un->un_f_pm_is_enabled = FALSE; 5243 } 5244 } else { 5245 /* 5246 * Page found, use it. 5247 */ 5248 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 5249 un->un_f_pm_is_enabled = TRUE; 5250 } 5251 5252 5253 if (un->un_f_pm_is_enabled == TRUE) { 5254 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5255 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5256 5257 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 5258 log_page_size, un->un_start_stop_cycle_page, 5259 0x01, 0, SD_PATH_DIRECT); 5260 #ifdef SDDEBUG 5261 if (sd_force_pm_supported) { 5262 /* Force a successful result */ 5263 rval = 0; 5264 } 5265 #endif 5266 5267 /* 5268 * If the Log sense for Page( Start/stop cycle counter page) 5269 * succeeds, then power managment is supported and we can 5270 * enable auto-pm. 5271 */ 5272 if (rval == 0) { 5273 (void) sd_create_pm_components(devi, un); 5274 } else { 5275 un->un_power_level = SD_SPINDLE_ON; 5276 un->un_f_pm_is_enabled = FALSE; 5277 } 5278 5279 kmem_free(log_page_data, log_page_size); 5280 } 5281 } 5282 5283 5284 /* 5285 * Function: sd_create_pm_components 5286 * 5287 * Description: Initialize PM property. 5288 * 5289 * Context: Kernel thread context 5290 */ 5291 5292 static void 5293 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 5294 { 5295 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 5296 5297 ASSERT(!mutex_owned(SD_MUTEX(un))); 5298 5299 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 5300 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 5301 /* 5302 * When components are initially created they are idle, 5303 * power up any non-removables. 5304 * Note: the return value of pm_raise_power can't be used 5305 * for determining if PM should be enabled for this device. 5306 * Even if you check the return values and remove this 5307 * property created above, the PM framework will not honor the 5308 * change after the first call to pm_raise_power. Hence, 5309 * removal of that property does not help if pm_raise_power 5310 * fails. In the case of removable media, the start/stop 5311 * will fail if the media is not present. 5312 */ 5313 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 5314 SD_SPINDLE_ON) == DDI_SUCCESS)) { 5315 mutex_enter(SD_MUTEX(un)); 5316 un->un_power_level = SD_SPINDLE_ON; 5317 mutex_enter(&un->un_pm_mutex); 5318 /* Set to on and not busy. */ 5319 un->un_pm_count = 0; 5320 } else { 5321 mutex_enter(SD_MUTEX(un)); 5322 un->un_power_level = SD_SPINDLE_OFF; 5323 mutex_enter(&un->un_pm_mutex); 5324 /* Set to off. */ 5325 un->un_pm_count = -1; 5326 } 5327 mutex_exit(&un->un_pm_mutex); 5328 mutex_exit(SD_MUTEX(un)); 5329 } else { 5330 un->un_power_level = SD_SPINDLE_ON; 5331 un->un_f_pm_is_enabled = FALSE; 5332 } 5333 } 5334 5335 5336 /* 5337 * Function: sd_ddi_suspend 5338 * 5339 * Description: Performs system power-down operations. This includes 5340 * setting the drive state to indicate its suspended so 5341 * that no new commands will be accepted. Also, wait for 5342 * all commands that are in transport or queued to a timer 5343 * for retry to complete. All timeout threads are cancelled. 5344 * 5345 * Return Code: DDI_FAILURE or DDI_SUCCESS 5346 * 5347 * Context: Kernel thread context 5348 */ 5349 5350 static int 5351 sd_ddi_suspend(dev_info_t *devi) 5352 { 5353 struct sd_lun *un; 5354 clock_t wait_cmds_complete; 5355 5356 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5357 if (un == NULL) { 5358 return (DDI_FAILURE); 5359 } 5360 5361 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 5362 5363 mutex_enter(SD_MUTEX(un)); 5364 5365 /* Return success if the device is already suspended. */ 5366 if (un->un_state == SD_STATE_SUSPENDED) { 5367 mutex_exit(SD_MUTEX(un)); 5368 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5369 "device already suspended, exiting\n"); 5370 return (DDI_SUCCESS); 5371 } 5372 5373 /* Return failure if the device is being used by HA */ 5374 if (un->un_resvd_status & 5375 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 5376 mutex_exit(SD_MUTEX(un)); 5377 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5378 "device in use by HA, exiting\n"); 5379 return (DDI_FAILURE); 5380 } 5381 5382 /* 5383 * Return failure if the device is in a resource wait 5384 * or power changing state. 5385 */ 5386 if ((un->un_state == SD_STATE_RWAIT) || 5387 (un->un_state == SD_STATE_PM_CHANGING)) { 5388 mutex_exit(SD_MUTEX(un)); 5389 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 5390 "device in resource wait state, exiting\n"); 5391 return (DDI_FAILURE); 5392 } 5393 5394 5395 un->un_save_state = un->un_last_state; 5396 New_state(un, SD_STATE_SUSPENDED); 5397 5398 /* 5399 * Wait for all commands that are in transport or queued to a timer 5400 * for retry to complete. 5401 * 5402 * While waiting, no new commands will be accepted or sent because of 5403 * the new state we set above. 5404 * 5405 * Wait till current operation has completed. If we are in the resource 5406 * wait state (with an intr outstanding) then we need to wait till the 5407 * intr completes and starts the next cmd. We want to wait for 5408 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 5409 */ 5410 wait_cmds_complete = ddi_get_lbolt() + 5411 (sd_wait_cmds_complete * drv_usectohz(1000000)); 5412 5413 while (un->un_ncmds_in_transport != 0) { 5414 /* 5415 * Fail if commands do not finish in the specified time. 5416 */ 5417 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 5418 wait_cmds_complete) == -1) { 5419 /* 5420 * Undo the state changes made above. Everything 5421 * must go back to it's original value. 5422 */ 5423 Restore_state(un); 5424 un->un_last_state = un->un_save_state; 5425 /* Wake up any threads that might be waiting. */ 5426 cv_broadcast(&un->un_suspend_cv); 5427 mutex_exit(SD_MUTEX(un)); 5428 SD_ERROR(SD_LOG_IO_PM, un, 5429 "sd_ddi_suspend: failed due to outstanding cmds\n"); 5430 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 5431 return (DDI_FAILURE); 5432 } 5433 } 5434 5435 /* 5436 * Cancel SCSI watch thread and timeouts, if any are active 5437 */ 5438 5439 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 5440 opaque_t temp_token = un->un_swr_token; 5441 mutex_exit(SD_MUTEX(un)); 5442 scsi_watch_suspend(temp_token); 5443 mutex_enter(SD_MUTEX(un)); 5444 } 5445 5446 if (un->un_reset_throttle_timeid != NULL) { 5447 timeout_id_t temp_id = un->un_reset_throttle_timeid; 5448 un->un_reset_throttle_timeid = NULL; 5449 mutex_exit(SD_MUTEX(un)); 5450 (void) untimeout(temp_id); 5451 mutex_enter(SD_MUTEX(un)); 5452 } 5453 5454 if (un->un_dcvb_timeid != NULL) { 5455 timeout_id_t temp_id = un->un_dcvb_timeid; 5456 un->un_dcvb_timeid = NULL; 5457 mutex_exit(SD_MUTEX(un)); 5458 (void) untimeout(temp_id); 5459 mutex_enter(SD_MUTEX(un)); 5460 } 5461 5462 mutex_enter(&un->un_pm_mutex); 5463 if (un->un_pm_timeid != NULL) { 5464 timeout_id_t temp_id = un->un_pm_timeid; 5465 un->un_pm_timeid = NULL; 5466 mutex_exit(&un->un_pm_mutex); 5467 mutex_exit(SD_MUTEX(un)); 5468 (void) untimeout(temp_id); 5469 mutex_enter(SD_MUTEX(un)); 5470 } else { 5471 mutex_exit(&un->un_pm_mutex); 5472 } 5473 5474 if (un->un_retry_timeid != NULL) { 5475 timeout_id_t temp_id = un->un_retry_timeid; 5476 un->un_retry_timeid = NULL; 5477 mutex_exit(SD_MUTEX(un)); 5478 (void) untimeout(temp_id); 5479 mutex_enter(SD_MUTEX(un)); 5480 } 5481 5482 if (un->un_direct_priority_timeid != NULL) { 5483 timeout_id_t temp_id = un->un_direct_priority_timeid; 5484 un->un_direct_priority_timeid = NULL; 5485 mutex_exit(SD_MUTEX(un)); 5486 (void) untimeout(temp_id); 5487 mutex_enter(SD_MUTEX(un)); 5488 } 5489 5490 if (un->un_f_is_fibre == TRUE) { 5491 /* 5492 * Remove callbacks for insert and remove events 5493 */ 5494 if (un->un_insert_event != NULL) { 5495 mutex_exit(SD_MUTEX(un)); 5496 (void) ddi_remove_event_handler(un->un_insert_cb_id); 5497 mutex_enter(SD_MUTEX(un)); 5498 un->un_insert_event = NULL; 5499 } 5500 5501 if (un->un_remove_event != NULL) { 5502 mutex_exit(SD_MUTEX(un)); 5503 (void) ddi_remove_event_handler(un->un_remove_cb_id); 5504 mutex_enter(SD_MUTEX(un)); 5505 un->un_remove_event = NULL; 5506 } 5507 } 5508 5509 mutex_exit(SD_MUTEX(un)); 5510 5511 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 5512 5513 return (DDI_SUCCESS); 5514 } 5515 5516 5517 /* 5518 * Function: sd_ddi_pm_suspend 5519 * 5520 * Description: Set the drive state to low power. 5521 * Someone else is required to actually change the drive 5522 * power level. 5523 * 5524 * Arguments: un - driver soft state (unit) structure 5525 * 5526 * Return Code: DDI_FAILURE or DDI_SUCCESS 5527 * 5528 * Context: Kernel thread context 5529 */ 5530 5531 static int 5532 sd_ddi_pm_suspend(struct sd_lun *un) 5533 { 5534 ASSERT(un != NULL); 5535 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 5536 5537 ASSERT(!mutex_owned(SD_MUTEX(un))); 5538 mutex_enter(SD_MUTEX(un)); 5539 5540 /* 5541 * Exit if power management is not enabled for this device, or if 5542 * the device is being used by HA. 5543 */ 5544 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 5545 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 5546 mutex_exit(SD_MUTEX(un)); 5547 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 5548 return (DDI_SUCCESS); 5549 } 5550 5551 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 5552 un->un_ncmds_in_driver); 5553 5554 /* 5555 * See if the device is not busy, ie.: 5556 * - we have no commands in the driver for this device 5557 * - not waiting for resources 5558 */ 5559 if ((un->un_ncmds_in_driver == 0) && 5560 (un->un_state != SD_STATE_RWAIT)) { 5561 /* 5562 * The device is not busy, so it is OK to go to low power state. 5563 * Indicate low power, but rely on someone else to actually 5564 * change it. 5565 */ 5566 mutex_enter(&un->un_pm_mutex); 5567 un->un_pm_count = -1; 5568 mutex_exit(&un->un_pm_mutex); 5569 un->un_power_level = SD_SPINDLE_OFF; 5570 } 5571 5572 mutex_exit(SD_MUTEX(un)); 5573 5574 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 5575 5576 return (DDI_SUCCESS); 5577 } 5578 5579 5580 /* 5581 * Function: sd_ddi_resume 5582 * 5583 * Description: Performs system power-up operations.. 5584 * 5585 * Return Code: DDI_SUCCESS 5586 * DDI_FAILURE 5587 * 5588 * Context: Kernel thread context 5589 */ 5590 5591 static int 5592 sd_ddi_resume(dev_info_t *devi) 5593 { 5594 struct sd_lun *un; 5595 5596 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 5597 if (un == NULL) { 5598 return (DDI_FAILURE); 5599 } 5600 5601 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 5602 5603 mutex_enter(SD_MUTEX(un)); 5604 Restore_state(un); 5605 5606 /* 5607 * Restore the state which was saved to give the 5608 * the right state in un_last_state 5609 */ 5610 un->un_last_state = un->un_save_state; 5611 /* 5612 * Note: throttle comes back at full. 5613 * Also note: this MUST be done before calling pm_raise_power 5614 * otherwise the system can get hung in biowait. The scenario where 5615 * this'll happen is under cpr suspend. Writing of the system 5616 * state goes through sddump, which writes 0 to un_throttle. If 5617 * writing the system state then fails, example if the partition is 5618 * too small, then cpr attempts a resume. If throttle isn't restored 5619 * from the saved value until after calling pm_raise_power then 5620 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 5621 * in biowait. 5622 */ 5623 un->un_throttle = un->un_saved_throttle; 5624 5625 /* 5626 * The chance of failure is very rare as the only command done in power 5627 * entry point is START command when you transition from 0->1 or 5628 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 5629 * which suspend was done. Ignore the return value as the resume should 5630 * not be failed. In the case of removable media the media need not be 5631 * inserted and hence there is a chance that raise power will fail with 5632 * media not present. 5633 */ 5634 if (un->un_f_attach_spinup) { 5635 mutex_exit(SD_MUTEX(un)); 5636 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 5637 mutex_enter(SD_MUTEX(un)); 5638 } 5639 5640 /* 5641 * Don't broadcast to the suspend cv and therefore possibly 5642 * start I/O until after power has been restored. 5643 */ 5644 cv_broadcast(&un->un_suspend_cv); 5645 cv_broadcast(&un->un_state_cv); 5646 5647 /* restart thread */ 5648 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 5649 scsi_watch_resume(un->un_swr_token); 5650 } 5651 5652 #if (defined(__fibre)) 5653 if (un->un_f_is_fibre == TRUE) { 5654 /* 5655 * Add callbacks for insert and remove events 5656 */ 5657 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 5658 sd_init_event_callbacks(un); 5659 } 5660 } 5661 #endif 5662 5663 /* 5664 * Transport any pending commands to the target. 5665 * 5666 * If this is a low-activity device commands in queue will have to wait 5667 * until new commands come in, which may take awhile. Also, we 5668 * specifically don't check un_ncmds_in_transport because we know that 5669 * there really are no commands in progress after the unit was 5670 * suspended and we could have reached the throttle level, been 5671 * suspended, and have no new commands coming in for awhile. Highly 5672 * unlikely, but so is the low-activity disk scenario. 5673 */ 5674 ddi_xbuf_dispatch(un->un_xbuf_attr); 5675 5676 sd_start_cmds(un, NULL); 5677 mutex_exit(SD_MUTEX(un)); 5678 5679 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 5680 5681 return (DDI_SUCCESS); 5682 } 5683 5684 5685 /* 5686 * Function: sd_ddi_pm_resume 5687 * 5688 * Description: Set the drive state to powered on. 5689 * Someone else is required to actually change the drive 5690 * power level. 5691 * 5692 * Arguments: un - driver soft state (unit) structure 5693 * 5694 * Return Code: DDI_SUCCESS 5695 * 5696 * Context: Kernel thread context 5697 */ 5698 5699 static int 5700 sd_ddi_pm_resume(struct sd_lun *un) 5701 { 5702 ASSERT(un != NULL); 5703 5704 ASSERT(!mutex_owned(SD_MUTEX(un))); 5705 mutex_enter(SD_MUTEX(un)); 5706 un->un_power_level = SD_SPINDLE_ON; 5707 5708 ASSERT(!mutex_owned(&un->un_pm_mutex)); 5709 mutex_enter(&un->un_pm_mutex); 5710 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 5711 un->un_pm_count++; 5712 ASSERT(un->un_pm_count == 0); 5713 /* 5714 * Note: no longer do the cv_broadcast on un_suspend_cv. The 5715 * un_suspend_cv is for a system resume, not a power management 5716 * device resume. (4297749) 5717 * cv_broadcast(&un->un_suspend_cv); 5718 */ 5719 } 5720 mutex_exit(&un->un_pm_mutex); 5721 mutex_exit(SD_MUTEX(un)); 5722 5723 return (DDI_SUCCESS); 5724 } 5725 5726 5727 /* 5728 * Function: sd_pm_idletimeout_handler 5729 * 5730 * Description: A timer routine that's active only while a device is busy. 5731 * The purpose is to extend slightly the pm framework's busy 5732 * view of the device to prevent busy/idle thrashing for 5733 * back-to-back commands. Do this by comparing the current time 5734 * to the time at which the last command completed and when the 5735 * difference is greater than sd_pm_idletime, call 5736 * pm_idle_component. In addition to indicating idle to the pm 5737 * framework, update the chain type to again use the internal pm 5738 * layers of the driver. 5739 * 5740 * Arguments: arg - driver soft state (unit) structure 5741 * 5742 * Context: Executes in a timeout(9F) thread context 5743 */ 5744 5745 static void 5746 sd_pm_idletimeout_handler(void *arg) 5747 { 5748 struct sd_lun *un = arg; 5749 5750 time_t now; 5751 5752 mutex_enter(&sd_detach_mutex); 5753 if (un->un_detach_count != 0) { 5754 /* Abort if the instance is detaching */ 5755 mutex_exit(&sd_detach_mutex); 5756 return; 5757 } 5758 mutex_exit(&sd_detach_mutex); 5759 5760 now = ddi_get_time(); 5761 /* 5762 * Grab both mutexes, in the proper order, since we're accessing 5763 * both PM and softstate variables. 5764 */ 5765 mutex_enter(SD_MUTEX(un)); 5766 mutex_enter(&un->un_pm_mutex); 5767 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 5768 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 5769 /* 5770 * Update the chain types. 5771 * This takes affect on the next new command received. 5772 */ 5773 if (un->un_f_non_devbsize_supported) { 5774 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 5775 } else { 5776 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 5777 } 5778 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 5779 5780 SD_TRACE(SD_LOG_IO_PM, un, 5781 "sd_pm_idletimeout_handler: idling device\n"); 5782 (void) pm_idle_component(SD_DEVINFO(un), 0); 5783 un->un_pm_idle_timeid = NULL; 5784 } else { 5785 un->un_pm_idle_timeid = 5786 timeout(sd_pm_idletimeout_handler, un, 5787 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 5788 } 5789 mutex_exit(&un->un_pm_mutex); 5790 mutex_exit(SD_MUTEX(un)); 5791 } 5792 5793 5794 /* 5795 * Function: sd_pm_timeout_handler 5796 * 5797 * Description: Callback to tell framework we are idle. 5798 * 5799 * Context: timeout(9f) thread context. 5800 */ 5801 5802 static void 5803 sd_pm_timeout_handler(void *arg) 5804 { 5805 struct sd_lun *un = arg; 5806 5807 (void) pm_idle_component(SD_DEVINFO(un), 0); 5808 mutex_enter(&un->un_pm_mutex); 5809 un->un_pm_timeid = NULL; 5810 mutex_exit(&un->un_pm_mutex); 5811 } 5812 5813 5814 /* 5815 * Function: sdpower 5816 * 5817 * Description: PM entry point. 5818 * 5819 * Return Code: DDI_SUCCESS 5820 * DDI_FAILURE 5821 * 5822 * Context: Kernel thread context 5823 */ 5824 5825 static int 5826 sdpower(dev_info_t *devi, int component, int level) 5827 { 5828 struct sd_lun *un; 5829 int instance; 5830 int rval = DDI_SUCCESS; 5831 uint_t i, log_page_size, maxcycles, ncycles; 5832 uchar_t *log_page_data; 5833 int log_sense_page; 5834 int medium_present; 5835 time_t intvlp; 5836 dev_t dev; 5837 struct pm_trans_data sd_pm_tran_data; 5838 uchar_t save_state; 5839 int sval; 5840 uchar_t state_before_pm; 5841 int got_semaphore_here; 5842 5843 instance = ddi_get_instance(devi); 5844 5845 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 5846 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 5847 component != 0) { 5848 return (DDI_FAILURE); 5849 } 5850 5851 dev = sd_make_device(SD_DEVINFO(un)); 5852 5853 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 5854 5855 /* 5856 * Must synchronize power down with close. 5857 * Attempt to decrement/acquire the open/close semaphore, 5858 * but do NOT wait on it. If it's not greater than zero, 5859 * ie. it can't be decremented without waiting, then 5860 * someone else, either open or close, already has it 5861 * and the try returns 0. Use that knowledge here to determine 5862 * if it's OK to change the device power level. 5863 * Also, only increment it on exit if it was decremented, ie. gotten, 5864 * here. 5865 */ 5866 got_semaphore_here = sema_tryp(&un->un_semoclose); 5867 5868 mutex_enter(SD_MUTEX(un)); 5869 5870 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 5871 un->un_ncmds_in_driver); 5872 5873 /* 5874 * If un_ncmds_in_driver is non-zero it indicates commands are 5875 * already being processed in the driver, or if the semaphore was 5876 * not gotten here it indicates an open or close is being processed. 5877 * At the same time somebody is requesting to go low power which 5878 * can't happen, therefore we need to return failure. 5879 */ 5880 if ((level == SD_SPINDLE_OFF) && 5881 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 5882 mutex_exit(SD_MUTEX(un)); 5883 5884 if (got_semaphore_here != 0) { 5885 sema_v(&un->un_semoclose); 5886 } 5887 SD_TRACE(SD_LOG_IO_PM, un, 5888 "sdpower: exit, device has queued cmds.\n"); 5889 return (DDI_FAILURE); 5890 } 5891 5892 /* 5893 * if it is OFFLINE that means the disk is completely dead 5894 * in our case we have to put the disk in on or off by sending commands 5895 * Of course that will fail anyway so return back here. 5896 * 5897 * Power changes to a device that's OFFLINE or SUSPENDED 5898 * are not allowed. 5899 */ 5900 if ((un->un_state == SD_STATE_OFFLINE) || 5901 (un->un_state == SD_STATE_SUSPENDED)) { 5902 mutex_exit(SD_MUTEX(un)); 5903 5904 if (got_semaphore_here != 0) { 5905 sema_v(&un->un_semoclose); 5906 } 5907 SD_TRACE(SD_LOG_IO_PM, un, 5908 "sdpower: exit, device is off-line.\n"); 5909 return (DDI_FAILURE); 5910 } 5911 5912 /* 5913 * Change the device's state to indicate it's power level 5914 * is being changed. Do this to prevent a power off in the 5915 * middle of commands, which is especially bad on devices 5916 * that are really powered off instead of just spun down. 5917 */ 5918 state_before_pm = un->un_state; 5919 un->un_state = SD_STATE_PM_CHANGING; 5920 5921 mutex_exit(SD_MUTEX(un)); 5922 5923 /* 5924 * If "pm-capable" property is set to TRUE by HBA drivers, 5925 * bypass the following checking, otherwise, check the log 5926 * sense information for this device 5927 */ 5928 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 5929 /* 5930 * Get the log sense information to understand whether the 5931 * the powercycle counts have gone beyond the threshhold. 5932 */ 5933 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 5934 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 5935 5936 mutex_enter(SD_MUTEX(un)); 5937 log_sense_page = un->un_start_stop_cycle_page; 5938 mutex_exit(SD_MUTEX(un)); 5939 5940 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 5941 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 5942 #ifdef SDDEBUG 5943 if (sd_force_pm_supported) { 5944 /* Force a successful result */ 5945 rval = 0; 5946 } 5947 #endif 5948 if (rval != 0) { 5949 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5950 "Log Sense Failed\n"); 5951 kmem_free(log_page_data, log_page_size); 5952 /* Cannot support power management on those drives */ 5953 5954 if (got_semaphore_here != 0) { 5955 sema_v(&un->un_semoclose); 5956 } 5957 /* 5958 * On exit put the state back to it's original value 5959 * and broadcast to anyone waiting for the power 5960 * change completion. 5961 */ 5962 mutex_enter(SD_MUTEX(un)); 5963 un->un_state = state_before_pm; 5964 cv_broadcast(&un->un_suspend_cv); 5965 mutex_exit(SD_MUTEX(un)); 5966 SD_TRACE(SD_LOG_IO_PM, un, 5967 "sdpower: exit, Log Sense Failed.\n"); 5968 return (DDI_FAILURE); 5969 } 5970 5971 /* 5972 * From the page data - Convert the essential information to 5973 * pm_trans_data 5974 */ 5975 maxcycles = 5976 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 5977 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 5978 5979 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 5980 5981 ncycles = 5982 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 5983 (log_page_data[0x26] << 8) | log_page_data[0x27]; 5984 5985 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 5986 5987 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 5988 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 5989 log_page_data[8+i]; 5990 } 5991 5992 kmem_free(log_page_data, log_page_size); 5993 5994 /* 5995 * Call pm_trans_check routine to get the Ok from 5996 * the global policy 5997 */ 5998 5999 sd_pm_tran_data.format = DC_SCSI_FORMAT; 6000 sd_pm_tran_data.un.scsi_cycles.flag = 0; 6001 6002 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 6003 #ifdef SDDEBUG 6004 if (sd_force_pm_supported) { 6005 /* Force a successful result */ 6006 rval = 1; 6007 } 6008 #endif 6009 switch (rval) { 6010 case 0: 6011 /* 6012 * Not Ok to Power cycle or error in parameters passed 6013 * Would have given the advised time to consider power 6014 * cycle. Based on the new intvlp parameter we are 6015 * supposed to pretend we are busy so that pm framework 6016 * will never call our power entry point. Because of 6017 * that install a timeout handler and wait for the 6018 * recommended time to elapse so that power management 6019 * can be effective again. 6020 * 6021 * To effect this behavior, call pm_busy_component to 6022 * indicate to the framework this device is busy. 6023 * By not adjusting un_pm_count the rest of PM in 6024 * the driver will function normally, and independant 6025 * of this but because the framework is told the device 6026 * is busy it won't attempt powering down until it gets 6027 * a matching idle. The timeout handler sends this. 6028 * Note: sd_pm_entry can't be called here to do this 6029 * because sdpower may have been called as a result 6030 * of a call to pm_raise_power from within sd_pm_entry. 6031 * 6032 * If a timeout handler is already active then 6033 * don't install another. 6034 */ 6035 mutex_enter(&un->un_pm_mutex); 6036 if (un->un_pm_timeid == NULL) { 6037 un->un_pm_timeid = 6038 timeout(sd_pm_timeout_handler, 6039 un, intvlp * drv_usectohz(1000000)); 6040 mutex_exit(&un->un_pm_mutex); 6041 (void) pm_busy_component(SD_DEVINFO(un), 0); 6042 } else { 6043 mutex_exit(&un->un_pm_mutex); 6044 } 6045 if (got_semaphore_here != 0) { 6046 sema_v(&un->un_semoclose); 6047 } 6048 /* 6049 * On exit put the state back to it's original value 6050 * and broadcast to anyone waiting for the power 6051 * change completion. 6052 */ 6053 mutex_enter(SD_MUTEX(un)); 6054 un->un_state = state_before_pm; 6055 cv_broadcast(&un->un_suspend_cv); 6056 mutex_exit(SD_MUTEX(un)); 6057 6058 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 6059 "trans check Failed, not ok to power cycle.\n"); 6060 return (DDI_FAILURE); 6061 6062 case -1: 6063 if (got_semaphore_here != 0) { 6064 sema_v(&un->un_semoclose); 6065 } 6066 /* 6067 * On exit put the state back to it's original value 6068 * and broadcast to anyone waiting for the power 6069 * change completion. 6070 */ 6071 mutex_enter(SD_MUTEX(un)); 6072 un->un_state = state_before_pm; 6073 cv_broadcast(&un->un_suspend_cv); 6074 mutex_exit(SD_MUTEX(un)); 6075 SD_TRACE(SD_LOG_IO_PM, un, 6076 "sdpower: exit, trans check command Failed.\n"); 6077 return (DDI_FAILURE); 6078 } 6079 } 6080 6081 if (level == SD_SPINDLE_OFF) { 6082 /* 6083 * Save the last state... if the STOP FAILS we need it 6084 * for restoring 6085 */ 6086 mutex_enter(SD_MUTEX(un)); 6087 save_state = un->un_last_state; 6088 /* 6089 * There must not be any cmds. getting processed 6090 * in the driver when we get here. Power to the 6091 * device is potentially going off. 6092 */ 6093 ASSERT(un->un_ncmds_in_driver == 0); 6094 mutex_exit(SD_MUTEX(un)); 6095 6096 /* 6097 * For now suspend the device completely before spindle is 6098 * turned off 6099 */ 6100 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 6101 if (got_semaphore_here != 0) { 6102 sema_v(&un->un_semoclose); 6103 } 6104 /* 6105 * On exit put the state back to it's original value 6106 * and broadcast to anyone waiting for the power 6107 * change completion. 6108 */ 6109 mutex_enter(SD_MUTEX(un)); 6110 un->un_state = state_before_pm; 6111 cv_broadcast(&un->un_suspend_cv); 6112 mutex_exit(SD_MUTEX(un)); 6113 SD_TRACE(SD_LOG_IO_PM, un, 6114 "sdpower: exit, PM suspend Failed.\n"); 6115 return (DDI_FAILURE); 6116 } 6117 } 6118 6119 /* 6120 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 6121 * close, or strategy. Dump no long uses this routine, it uses it's 6122 * own code so it can be done in polled mode. 6123 */ 6124 6125 medium_present = TRUE; 6126 6127 /* 6128 * When powering up, issue a TUR in case the device is at unit 6129 * attention. Don't do retries. Bypass the PM layer, otherwise 6130 * a deadlock on un_pm_busy_cv will occur. 6131 */ 6132 if (level == SD_SPINDLE_ON) { 6133 (void) sd_send_scsi_TEST_UNIT_READY(un, 6134 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 6135 } 6136 6137 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 6138 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 6139 6140 sval = sd_send_scsi_START_STOP_UNIT(un, 6141 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 6142 SD_PATH_DIRECT); 6143 /* Command failed, check for media present. */ 6144 if ((sval == ENXIO) && un->un_f_has_removable_media) { 6145 medium_present = FALSE; 6146 } 6147 6148 /* 6149 * The conditions of interest here are: 6150 * if a spindle off with media present fails, 6151 * then restore the state and return an error. 6152 * else if a spindle on fails, 6153 * then return an error (there's no state to restore). 6154 * In all other cases we setup for the new state 6155 * and return success. 6156 */ 6157 switch (level) { 6158 case SD_SPINDLE_OFF: 6159 if ((medium_present == TRUE) && (sval != 0)) { 6160 /* The stop command from above failed */ 6161 rval = DDI_FAILURE; 6162 /* 6163 * The stop command failed, and we have media 6164 * present. Put the level back by calling the 6165 * sd_pm_resume() and set the state back to 6166 * it's previous value. 6167 */ 6168 (void) sd_ddi_pm_resume(un); 6169 mutex_enter(SD_MUTEX(un)); 6170 un->un_last_state = save_state; 6171 mutex_exit(SD_MUTEX(un)); 6172 break; 6173 } 6174 /* 6175 * The stop command from above succeeded. 6176 */ 6177 if (un->un_f_monitor_media_state) { 6178 /* 6179 * Terminate watch thread in case of removable media 6180 * devices going into low power state. This is as per 6181 * the requirements of pm framework, otherwise commands 6182 * will be generated for the device (through watch 6183 * thread), even when the device is in low power state. 6184 */ 6185 mutex_enter(SD_MUTEX(un)); 6186 un->un_f_watcht_stopped = FALSE; 6187 if (un->un_swr_token != NULL) { 6188 opaque_t temp_token = un->un_swr_token; 6189 un->un_f_watcht_stopped = TRUE; 6190 un->un_swr_token = NULL; 6191 mutex_exit(SD_MUTEX(un)); 6192 (void) scsi_watch_request_terminate(temp_token, 6193 SCSI_WATCH_TERMINATE_WAIT); 6194 } else { 6195 mutex_exit(SD_MUTEX(un)); 6196 } 6197 } 6198 break; 6199 6200 default: /* The level requested is spindle on... */ 6201 /* 6202 * Legacy behavior: return success on a failed spinup 6203 * if there is no media in the drive. 6204 * Do this by looking at medium_present here. 6205 */ 6206 if ((sval != 0) && medium_present) { 6207 /* The start command from above failed */ 6208 rval = DDI_FAILURE; 6209 break; 6210 } 6211 /* 6212 * The start command from above succeeded 6213 * Resume the devices now that we have 6214 * started the disks 6215 */ 6216 (void) sd_ddi_pm_resume(un); 6217 6218 /* 6219 * Resume the watch thread since it was suspended 6220 * when the device went into low power mode. 6221 */ 6222 if (un->un_f_monitor_media_state) { 6223 mutex_enter(SD_MUTEX(un)); 6224 if (un->un_f_watcht_stopped == TRUE) { 6225 opaque_t temp_token; 6226 6227 un->un_f_watcht_stopped = FALSE; 6228 mutex_exit(SD_MUTEX(un)); 6229 temp_token = scsi_watch_request_submit( 6230 SD_SCSI_DEVP(un), 6231 sd_check_media_time, 6232 SENSE_LENGTH, sd_media_watch_cb, 6233 (caddr_t)dev); 6234 mutex_enter(SD_MUTEX(un)); 6235 un->un_swr_token = temp_token; 6236 } 6237 mutex_exit(SD_MUTEX(un)); 6238 } 6239 } 6240 if (got_semaphore_here != 0) { 6241 sema_v(&un->un_semoclose); 6242 } 6243 /* 6244 * On exit put the state back to it's original value 6245 * and broadcast to anyone waiting for the power 6246 * change completion. 6247 */ 6248 mutex_enter(SD_MUTEX(un)); 6249 un->un_state = state_before_pm; 6250 cv_broadcast(&un->un_suspend_cv); 6251 mutex_exit(SD_MUTEX(un)); 6252 6253 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 6254 6255 return (rval); 6256 } 6257 6258 6259 6260 /* 6261 * Function: sdattach 6262 * 6263 * Description: Driver's attach(9e) entry point function. 6264 * 6265 * Arguments: devi - opaque device info handle 6266 * cmd - attach type 6267 * 6268 * Return Code: DDI_SUCCESS 6269 * DDI_FAILURE 6270 * 6271 * Context: Kernel thread context 6272 */ 6273 6274 static int 6275 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 6276 { 6277 switch (cmd) { 6278 case DDI_ATTACH: 6279 return (sd_unit_attach(devi)); 6280 case DDI_RESUME: 6281 return (sd_ddi_resume(devi)); 6282 default: 6283 break; 6284 } 6285 return (DDI_FAILURE); 6286 } 6287 6288 6289 /* 6290 * Function: sddetach 6291 * 6292 * Description: Driver's detach(9E) entry point function. 6293 * 6294 * Arguments: devi - opaque device info handle 6295 * cmd - detach type 6296 * 6297 * Return Code: DDI_SUCCESS 6298 * DDI_FAILURE 6299 * 6300 * Context: Kernel thread context 6301 */ 6302 6303 static int 6304 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 6305 { 6306 switch (cmd) { 6307 case DDI_DETACH: 6308 return (sd_unit_detach(devi)); 6309 case DDI_SUSPEND: 6310 return (sd_ddi_suspend(devi)); 6311 default: 6312 break; 6313 } 6314 return (DDI_FAILURE); 6315 } 6316 6317 6318 /* 6319 * Function: sd_sync_with_callback 6320 * 6321 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 6322 * state while the callback routine is active. 6323 * 6324 * Arguments: un: softstate structure for the instance 6325 * 6326 * Context: Kernel thread context 6327 */ 6328 6329 static void 6330 sd_sync_with_callback(struct sd_lun *un) 6331 { 6332 ASSERT(un != NULL); 6333 6334 mutex_enter(SD_MUTEX(un)); 6335 6336 ASSERT(un->un_in_callback >= 0); 6337 6338 while (un->un_in_callback > 0) { 6339 mutex_exit(SD_MUTEX(un)); 6340 delay(2); 6341 mutex_enter(SD_MUTEX(un)); 6342 } 6343 6344 mutex_exit(SD_MUTEX(un)); 6345 } 6346 6347 /* 6348 * Function: sd_unit_attach 6349 * 6350 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 6351 * the soft state structure for the device and performs 6352 * all necessary structure and device initializations. 6353 * 6354 * Arguments: devi: the system's dev_info_t for the device. 6355 * 6356 * Return Code: DDI_SUCCESS if attach is successful. 6357 * DDI_FAILURE if any part of the attach fails. 6358 * 6359 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 6360 * Kernel thread context only. Can sleep. 6361 */ 6362 6363 static int 6364 sd_unit_attach(dev_info_t *devi) 6365 { 6366 struct scsi_device *devp; 6367 struct sd_lun *un; 6368 char *variantp; 6369 int reservation_flag = SD_TARGET_IS_UNRESERVED; 6370 int instance; 6371 int rval; 6372 int wc_enabled; 6373 int tgt; 6374 uint64_t capacity; 6375 uint_t lbasize = 0; 6376 dev_info_t *pdip = ddi_get_parent(devi); 6377 int offbyone = 0; 6378 int geom_label_valid = 0; 6379 #if defined(__sparc) 6380 int max_xfer_size; 6381 #endif 6382 6383 /* 6384 * Retrieve the target driver's private data area. This was set 6385 * up by the HBA. 6386 */ 6387 devp = ddi_get_driver_private(devi); 6388 6389 /* 6390 * Retrieve the target ID of the device. 6391 */ 6392 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6393 SCSI_ADDR_PROP_TARGET, -1); 6394 6395 /* 6396 * Since we have no idea what state things were left in by the last 6397 * user of the device, set up some 'default' settings, ie. turn 'em 6398 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 6399 * Do this before the scsi_probe, which sends an inquiry. 6400 * This is a fix for bug (4430280). 6401 * Of special importance is wide-xfer. The drive could have been left 6402 * in wide transfer mode by the last driver to communicate with it, 6403 * this includes us. If that's the case, and if the following is not 6404 * setup properly or we don't re-negotiate with the drive prior to 6405 * transferring data to/from the drive, it causes bus parity errors, 6406 * data overruns, and unexpected interrupts. This first occurred when 6407 * the fix for bug (4378686) was made. 6408 */ 6409 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 6410 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 6411 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 6412 6413 /* 6414 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 6415 * on a target. Setting it per lun instance actually sets the 6416 * capability of this target, which affects those luns already 6417 * attached on the same target. So during attach, we can only disable 6418 * this capability only when no other lun has been attached on this 6419 * target. By doing this, we assume a target has the same tagged-qing 6420 * capability for every lun. The condition can be removed when HBA 6421 * is changed to support per lun based tagged-qing capability. 6422 */ 6423 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 6424 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 6425 } 6426 6427 /* 6428 * Use scsi_probe() to issue an INQUIRY command to the device. 6429 * This call will allocate and fill in the scsi_inquiry structure 6430 * and point the sd_inq member of the scsi_device structure to it. 6431 * If the attach succeeds, then this memory will not be de-allocated 6432 * (via scsi_unprobe()) until the instance is detached. 6433 */ 6434 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 6435 goto probe_failed; 6436 } 6437 6438 /* 6439 * Check the device type as specified in the inquiry data and 6440 * claim it if it is of a type that we support. 6441 */ 6442 switch (devp->sd_inq->inq_dtype) { 6443 case DTYPE_DIRECT: 6444 break; 6445 case DTYPE_RODIRECT: 6446 break; 6447 case DTYPE_OPTICAL: 6448 break; 6449 case DTYPE_NOTPRESENT: 6450 default: 6451 /* Unsupported device type; fail the attach. */ 6452 goto probe_failed; 6453 } 6454 6455 /* 6456 * Allocate the soft state structure for this unit. 6457 * 6458 * We rely upon this memory being set to all zeroes by 6459 * ddi_soft_state_zalloc(). We assume that any member of the 6460 * soft state structure that is not explicitly initialized by 6461 * this routine will have a value of zero. 6462 */ 6463 instance = ddi_get_instance(devp->sd_dev); 6464 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 6465 goto probe_failed; 6466 } 6467 6468 /* 6469 * Retrieve a pointer to the newly-allocated soft state. 6470 * 6471 * This should NEVER fail if the ddi_soft_state_zalloc() call above 6472 * was successful, unless something has gone horribly wrong and the 6473 * ddi's soft state internals are corrupt (in which case it is 6474 * probably better to halt here than just fail the attach....) 6475 */ 6476 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 6477 panic("sd_unit_attach: NULL soft state on instance:0x%x", 6478 instance); 6479 /*NOTREACHED*/ 6480 } 6481 6482 /* 6483 * Link the back ptr of the driver soft state to the scsi_device 6484 * struct for this lun. 6485 * Save a pointer to the softstate in the driver-private area of 6486 * the scsi_device struct. 6487 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 6488 * we first set un->un_sd below. 6489 */ 6490 un->un_sd = devp; 6491 devp->sd_private = (opaque_t)un; 6492 6493 /* 6494 * The following must be after devp is stored in the soft state struct. 6495 */ 6496 #ifdef SDDEBUG 6497 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6498 "%s_unit_attach: un:0x%p instance:%d\n", 6499 ddi_driver_name(devi), un, instance); 6500 #endif 6501 6502 /* 6503 * Set up the device type and node type (for the minor nodes). 6504 * By default we assume that the device can at least support the 6505 * Common Command Set. Call it a CD-ROM if it reports itself 6506 * as a RODIRECT device. 6507 */ 6508 switch (devp->sd_inq->inq_dtype) { 6509 case DTYPE_RODIRECT: 6510 un->un_node_type = DDI_NT_CD_CHAN; 6511 un->un_ctype = CTYPE_CDROM; 6512 break; 6513 case DTYPE_OPTICAL: 6514 un->un_node_type = DDI_NT_BLOCK_CHAN; 6515 un->un_ctype = CTYPE_ROD; 6516 break; 6517 default: 6518 un->un_node_type = DDI_NT_BLOCK_CHAN; 6519 un->un_ctype = CTYPE_CCS; 6520 break; 6521 } 6522 6523 /* 6524 * Try to read the interconnect type from the HBA. 6525 * 6526 * Note: This driver is currently compiled as two binaries, a parallel 6527 * scsi version (sd) and a fibre channel version (ssd). All functional 6528 * differences are determined at compile time. In the future a single 6529 * binary will be provided and the inteconnect type will be used to 6530 * differentiate between fibre and parallel scsi behaviors. At that time 6531 * it will be necessary for all fibre channel HBAs to support this 6532 * property. 6533 * 6534 * set un_f_is_fiber to TRUE ( default fiber ) 6535 */ 6536 un->un_f_is_fibre = TRUE; 6537 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 6538 case INTERCONNECT_SSA: 6539 un->un_interconnect_type = SD_INTERCONNECT_SSA; 6540 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6541 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 6542 break; 6543 case INTERCONNECT_PARALLEL: 6544 un->un_f_is_fibre = FALSE; 6545 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6546 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6547 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 6548 break; 6549 case INTERCONNECT_SATA: 6550 un->un_f_is_fibre = FALSE; 6551 un->un_interconnect_type = SD_INTERCONNECT_SATA; 6552 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6553 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 6554 break; 6555 case INTERCONNECT_FIBRE: 6556 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 6557 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6558 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 6559 break; 6560 case INTERCONNECT_FABRIC: 6561 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 6562 un->un_node_type = DDI_NT_BLOCK_FABRIC; 6563 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6564 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 6565 break; 6566 default: 6567 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 6568 /* 6569 * The HBA does not support the "interconnect-type" property 6570 * (or did not provide a recognized type). 6571 * 6572 * Note: This will be obsoleted when a single fibre channel 6573 * and parallel scsi driver is delivered. In the meantime the 6574 * interconnect type will be set to the platform default.If that 6575 * type is not parallel SCSI, it means that we should be 6576 * assuming "ssd" semantics. However, here this also means that 6577 * the FC HBA is not supporting the "interconnect-type" property 6578 * like we expect it to, so log this occurrence. 6579 */ 6580 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 6581 if (!SD_IS_PARALLEL_SCSI(un)) { 6582 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6583 "sd_unit_attach: un:0x%p Assuming " 6584 "INTERCONNECT_FIBRE\n", un); 6585 } else { 6586 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6587 "sd_unit_attach: un:0x%p Assuming " 6588 "INTERCONNECT_PARALLEL\n", un); 6589 un->un_f_is_fibre = FALSE; 6590 } 6591 #else 6592 /* 6593 * Note: This source will be implemented when a single fibre 6594 * channel and parallel scsi driver is delivered. The default 6595 * will be to assume that if a device does not support the 6596 * "interconnect-type" property it is a parallel SCSI HBA and 6597 * we will set the interconnect type for parallel scsi. 6598 */ 6599 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 6600 un->un_f_is_fibre = FALSE; 6601 #endif 6602 break; 6603 } 6604 6605 if (un->un_f_is_fibre == TRUE) { 6606 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 6607 SCSI_VERSION_3) { 6608 switch (un->un_interconnect_type) { 6609 case SD_INTERCONNECT_FIBRE: 6610 case SD_INTERCONNECT_SSA: 6611 un->un_node_type = DDI_NT_BLOCK_WWN; 6612 break; 6613 default: 6614 break; 6615 } 6616 } 6617 } 6618 6619 /* 6620 * Initialize the Request Sense command for the target 6621 */ 6622 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 6623 goto alloc_rqs_failed; 6624 } 6625 6626 /* 6627 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 6628 * with separate binary for sd and ssd. 6629 * 6630 * x86 has 1 binary, un_retry_count is set base on connection type. 6631 * The hardcoded values will go away when Sparc uses 1 binary 6632 * for sd and ssd. This hardcoded values need to match 6633 * SD_RETRY_COUNT in sddef.h 6634 * The value used is base on interconnect type. 6635 * fibre = 3, parallel = 5 6636 */ 6637 #if defined(__i386) || defined(__amd64) 6638 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 6639 #else 6640 un->un_retry_count = SD_RETRY_COUNT; 6641 #endif 6642 6643 /* 6644 * Set the per disk retry count to the default number of retries 6645 * for disks and CDROMs. This value can be overridden by the 6646 * disk property list or an entry in sd.conf. 6647 */ 6648 un->un_notready_retry_count = 6649 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 6650 : DISK_NOT_READY_RETRY_COUNT(un); 6651 6652 /* 6653 * Set the busy retry count to the default value of un_retry_count. 6654 * This can be overridden by entries in sd.conf or the device 6655 * config table. 6656 */ 6657 un->un_busy_retry_count = un->un_retry_count; 6658 6659 /* 6660 * Init the reset threshold for retries. This number determines 6661 * how many retries must be performed before a reset can be issued 6662 * (for certain error conditions). This can be overridden by entries 6663 * in sd.conf or the device config table. 6664 */ 6665 un->un_reset_retry_count = (un->un_retry_count / 2); 6666 6667 /* 6668 * Set the victim_retry_count to the default un_retry_count 6669 */ 6670 un->un_victim_retry_count = (2 * un->un_retry_count); 6671 6672 /* 6673 * Set the reservation release timeout to the default value of 6674 * 5 seconds. This can be overridden by entries in ssd.conf or the 6675 * device config table. 6676 */ 6677 un->un_reserve_release_time = 5; 6678 6679 /* 6680 * Set up the default maximum transfer size. Note that this may 6681 * get updated later in the attach, when setting up default wide 6682 * operations for disks. 6683 */ 6684 #if defined(__i386) || defined(__amd64) 6685 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 6686 un->un_partial_dma_supported = 1; 6687 #else 6688 un->un_max_xfer_size = (uint_t)maxphys; 6689 #endif 6690 6691 /* 6692 * Get "allow bus device reset" property (defaults to "enabled" if 6693 * the property was not defined). This is to disable bus resets for 6694 * certain kinds of error recovery. Note: In the future when a run-time 6695 * fibre check is available the soft state flag should default to 6696 * enabled. 6697 */ 6698 if (un->un_f_is_fibre == TRUE) { 6699 un->un_f_allow_bus_device_reset = TRUE; 6700 } else { 6701 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 6702 "allow-bus-device-reset", 1) != 0) { 6703 un->un_f_allow_bus_device_reset = TRUE; 6704 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6705 "sd_unit_attach: un:0x%p Bus device reset " 6706 "enabled\n", un); 6707 } else { 6708 un->un_f_allow_bus_device_reset = FALSE; 6709 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6710 "sd_unit_attach: un:0x%p Bus device reset " 6711 "disabled\n", un); 6712 } 6713 } 6714 6715 /* 6716 * Check if this is an ATAPI device. ATAPI devices use Group 1 6717 * Read/Write commands and Group 2 Mode Sense/Select commands. 6718 * 6719 * Note: The "obsolete" way of doing this is to check for the "atapi" 6720 * property. The new "variant" property with a value of "atapi" has been 6721 * introduced so that future 'variants' of standard SCSI behavior (like 6722 * atapi) could be specified by the underlying HBA drivers by supplying 6723 * a new value for the "variant" property, instead of having to define a 6724 * new property. 6725 */ 6726 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 6727 un->un_f_cfg_is_atapi = TRUE; 6728 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6729 "sd_unit_attach: un:0x%p Atapi device\n", un); 6730 } 6731 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 6732 &variantp) == DDI_PROP_SUCCESS) { 6733 if (strcmp(variantp, "atapi") == 0) { 6734 un->un_f_cfg_is_atapi = TRUE; 6735 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6736 "sd_unit_attach: un:0x%p Atapi device\n", un); 6737 } 6738 ddi_prop_free(variantp); 6739 } 6740 6741 un->un_cmd_timeout = SD_IO_TIME; 6742 6743 /* Info on current states, statuses, etc. (Updated frequently) */ 6744 un->un_state = SD_STATE_NORMAL; 6745 un->un_last_state = SD_STATE_NORMAL; 6746 6747 /* Control & status info for command throttling */ 6748 un->un_throttle = sd_max_throttle; 6749 un->un_saved_throttle = sd_max_throttle; 6750 un->un_min_throttle = sd_min_throttle; 6751 6752 if (un->un_f_is_fibre == TRUE) { 6753 un->un_f_use_adaptive_throttle = TRUE; 6754 } else { 6755 un->un_f_use_adaptive_throttle = FALSE; 6756 } 6757 6758 /* Removable media support. */ 6759 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 6760 un->un_mediastate = DKIO_NONE; 6761 un->un_specified_mediastate = DKIO_NONE; 6762 6763 /* CVs for suspend/resume (PM or DR) */ 6764 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 6765 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 6766 6767 /* Power management support. */ 6768 un->un_power_level = SD_SPINDLE_UNINIT; 6769 6770 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 6771 un->un_f_wcc_inprog = 0; 6772 6773 /* 6774 * The open/close semaphore is used to serialize threads executing 6775 * in the driver's open & close entry point routines for a given 6776 * instance. 6777 */ 6778 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 6779 6780 /* 6781 * The conf file entry and softstate variable is a forceful override, 6782 * meaning a non-zero value must be entered to change the default. 6783 */ 6784 un->un_f_disksort_disabled = FALSE; 6785 6786 /* 6787 * Retrieve the properties from the static driver table or the driver 6788 * configuration file (.conf) for this unit and update the soft state 6789 * for the device as needed for the indicated properties. 6790 * Note: the property configuration needs to occur here as some of the 6791 * following routines may have dependancies on soft state flags set 6792 * as part of the driver property configuration. 6793 */ 6794 sd_read_unit_properties(un); 6795 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6796 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 6797 6798 /* 6799 * Only if a device has "hotpluggable" property, it is 6800 * treated as hotpluggable device. Otherwise, it is 6801 * regarded as non-hotpluggable one. 6802 */ 6803 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 6804 -1) != -1) { 6805 un->un_f_is_hotpluggable = TRUE; 6806 } 6807 6808 /* 6809 * set unit's attributes(flags) according to "hotpluggable" and 6810 * RMB bit in INQUIRY data. 6811 */ 6812 sd_set_unit_attributes(un, devi); 6813 6814 /* 6815 * By default, we mark the capacity, lbasize, and geometry 6816 * as invalid. Only if we successfully read a valid capacity 6817 * will we update the un_blockcount and un_tgt_blocksize with the 6818 * valid values (the geometry will be validated later). 6819 */ 6820 un->un_f_blockcount_is_valid = FALSE; 6821 un->un_f_tgt_blocksize_is_valid = FALSE; 6822 6823 /* 6824 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 6825 * otherwise. 6826 */ 6827 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 6828 un->un_blockcount = 0; 6829 6830 /* 6831 * Set up the per-instance info needed to determine the correct 6832 * CDBs and other info for issuing commands to the target. 6833 */ 6834 sd_init_cdb_limits(un); 6835 6836 /* 6837 * Set up the IO chains to use, based upon the target type. 6838 */ 6839 if (un->un_f_non_devbsize_supported) { 6840 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 6841 } else { 6842 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 6843 } 6844 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 6845 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 6846 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 6847 6848 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 6849 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 6850 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 6851 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 6852 6853 6854 if (ISCD(un)) { 6855 un->un_additional_codes = sd_additional_codes; 6856 } else { 6857 un->un_additional_codes = NULL; 6858 } 6859 6860 /* 6861 * Create the kstats here so they can be available for attach-time 6862 * routines that send commands to the unit (either polled or via 6863 * sd_send_scsi_cmd). 6864 * 6865 * Note: This is a critical sequence that needs to be maintained: 6866 * 1) Instantiate the kstats here, before any routines using the 6867 * iopath (i.e. sd_send_scsi_cmd). 6868 * 2) Instantiate and initialize the partition stats 6869 * (sd_set_pstats). 6870 * 3) Initialize the error stats (sd_set_errstats), following 6871 * sd_validate_geometry(),sd_register_devid(), 6872 * and sd_cache_control(). 6873 */ 6874 6875 un->un_stats = kstat_create(sd_label, instance, 6876 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 6877 if (un->un_stats != NULL) { 6878 un->un_stats->ks_lock = SD_MUTEX(un); 6879 kstat_install(un->un_stats); 6880 } 6881 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6882 "sd_unit_attach: un:0x%p un_stats created\n", un); 6883 6884 sd_create_errstats(un, instance); 6885 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 6886 "sd_unit_attach: un:0x%p errstats created\n", un); 6887 6888 /* 6889 * The following if/else code was relocated here from below as part 6890 * of the fix for bug (4430280). However with the default setup added 6891 * on entry to this routine, it's no longer absolutely necessary for 6892 * this to be before the call to sd_spin_up_unit. 6893 */ 6894 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 6895 /* 6896 * If SCSI-2 tagged queueing is supported by the target 6897 * and by the host adapter then we will enable it. 6898 */ 6899 un->un_tagflags = 0; 6900 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 6901 (devp->sd_inq->inq_cmdque) && 6902 (un->un_f_arq_enabled == TRUE)) { 6903 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 6904 1, 1) == 1) { 6905 un->un_tagflags = FLAG_STAG; 6906 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6907 "sd_unit_attach: un:0x%p tag queueing " 6908 "enabled\n", un); 6909 } else if (scsi_ifgetcap(SD_ADDRESS(un), 6910 "untagged-qing", 0) == 1) { 6911 un->un_f_opt_queueing = TRUE; 6912 un->un_saved_throttle = un->un_throttle = 6913 min(un->un_throttle, 3); 6914 } else { 6915 un->un_f_opt_queueing = FALSE; 6916 un->un_saved_throttle = un->un_throttle = 1; 6917 } 6918 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 6919 == 1) && (un->un_f_arq_enabled == TRUE)) { 6920 /* The Host Adapter supports internal queueing. */ 6921 un->un_f_opt_queueing = TRUE; 6922 un->un_saved_throttle = un->un_throttle = 6923 min(un->un_throttle, 3); 6924 } else { 6925 un->un_f_opt_queueing = FALSE; 6926 un->un_saved_throttle = un->un_throttle = 1; 6927 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6928 "sd_unit_attach: un:0x%p no tag queueing\n", un); 6929 } 6930 6931 /* 6932 * Enable large transfers for SATA/SAS drives 6933 */ 6934 if (SD_IS_SERIAL(un)) { 6935 un->un_max_xfer_size = 6936 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 6937 sd_max_xfer_size, SD_MAX_XFER_SIZE); 6938 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6939 "sd_unit_attach: un:0x%p max transfer " 6940 "size=0x%x\n", un, un->un_max_xfer_size); 6941 6942 } 6943 6944 /* Setup or tear down default wide operations for disks */ 6945 6946 /* 6947 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 6948 * and "ssd_max_xfer_size" to exist simultaneously on the same 6949 * system and be set to different values. In the future this 6950 * code may need to be updated when the ssd module is 6951 * obsoleted and removed from the system. (4299588) 6952 */ 6953 if (SD_IS_PARALLEL_SCSI(un) && 6954 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 6955 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 6956 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 6957 1, 1) == 1) { 6958 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6959 "sd_unit_attach: un:0x%p Wide Transfer " 6960 "enabled\n", un); 6961 } 6962 6963 /* 6964 * If tagged queuing has also been enabled, then 6965 * enable large xfers 6966 */ 6967 if (un->un_saved_throttle == sd_max_throttle) { 6968 un->un_max_xfer_size = 6969 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 6970 sd_max_xfer_size, SD_MAX_XFER_SIZE); 6971 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6972 "sd_unit_attach: un:0x%p max transfer " 6973 "size=0x%x\n", un, un->un_max_xfer_size); 6974 } 6975 } else { 6976 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 6977 0, 1) == 1) { 6978 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6979 "sd_unit_attach: un:0x%p " 6980 "Wide Transfer disabled\n", un); 6981 } 6982 } 6983 } else { 6984 un->un_tagflags = FLAG_STAG; 6985 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 6986 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 6987 } 6988 6989 /* 6990 * If this target supports LUN reset, try to enable it. 6991 */ 6992 if (un->un_f_lun_reset_enabled) { 6993 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 6994 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 6995 "un:0x%p lun_reset capability set\n", un); 6996 } else { 6997 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 6998 "un:0x%p lun-reset capability not set\n", un); 6999 } 7000 } 7001 7002 /* 7003 * Adjust the maximum transfer size. This is to fix 7004 * the problem of partial DMA support on SPARC. Some 7005 * HBA driver, like aac, has very small dma_attr_maxxfer 7006 * size, which requires partial DMA support on SPARC. 7007 * In the future the SPARC pci nexus driver may solve 7008 * the problem instead of this fix. 7009 */ 7010 #if defined(__sparc) 7011 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1); 7012 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) { 7013 un->un_max_xfer_size = max_xfer_size; 7014 un->un_partial_dma_supported = 1; 7015 } 7016 #endif 7017 7018 /* 7019 * Set PKT_DMA_PARTIAL flag. 7020 */ 7021 if (un->un_partial_dma_supported == 1) { 7022 un->un_pkt_flags = PKT_DMA_PARTIAL; 7023 } else { 7024 un->un_pkt_flags = 0; 7025 } 7026 7027 /* 7028 * At this point in the attach, we have enough info in the 7029 * soft state to be able to issue commands to the target. 7030 * 7031 * All command paths used below MUST issue their commands as 7032 * SD_PATH_DIRECT. This is important as intermediate layers 7033 * are not all initialized yet (such as PM). 7034 */ 7035 7036 /* 7037 * Send a TEST UNIT READY command to the device. This should clear 7038 * any outstanding UNIT ATTENTION that may be present. 7039 * 7040 * Note: Don't check for success, just track if there is a reservation, 7041 * this is a throw away command to clear any unit attentions. 7042 * 7043 * Note: This MUST be the first command issued to the target during 7044 * attach to ensure power on UNIT ATTENTIONS are cleared. 7045 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 7046 * with attempts at spinning up a device with no media. 7047 */ 7048 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 7049 reservation_flag = SD_TARGET_IS_RESERVED; 7050 } 7051 7052 /* 7053 * If the device is NOT a removable media device, attempt to spin 7054 * it up (using the START_STOP_UNIT command) and read its capacity 7055 * (using the READ CAPACITY command). Note, however, that either 7056 * of these could fail and in some cases we would continue with 7057 * the attach despite the failure (see below). 7058 */ 7059 if (un->un_f_descr_format_supported) { 7060 switch (sd_spin_up_unit(un)) { 7061 case 0: 7062 /* 7063 * Spin-up was successful; now try to read the 7064 * capacity. If successful then save the results 7065 * and mark the capacity & lbasize as valid. 7066 */ 7067 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7068 "sd_unit_attach: un:0x%p spin-up successful\n", un); 7069 7070 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 7071 &lbasize, SD_PATH_DIRECT)) { 7072 case 0: { 7073 if (capacity > DK_MAX_BLOCKS) { 7074 #ifdef _LP64 7075 if (capacity + 1 > 7076 SD_GROUP1_MAX_ADDRESS) { 7077 /* 7078 * Enable descriptor format 7079 * sense data so that we can 7080 * get 64 bit sense data 7081 * fields. 7082 */ 7083 sd_enable_descr_sense(un); 7084 } 7085 #else 7086 /* 32-bit kernels can't handle this */ 7087 scsi_log(SD_DEVINFO(un), 7088 sd_label, CE_WARN, 7089 "disk has %llu blocks, which " 7090 "is too large for a 32-bit " 7091 "kernel", capacity); 7092 7093 #if defined(__i386) || defined(__amd64) 7094 /* 7095 * 1TB disk was treated as (1T - 512)B 7096 * in the past, so that it might have 7097 * valid VTOC and solaris partitions, 7098 * we have to allow it to continue to 7099 * work. 7100 */ 7101 if (capacity -1 > DK_MAX_BLOCKS) 7102 #endif 7103 goto spinup_failed; 7104 #endif 7105 } 7106 7107 /* 7108 * Here it's not necessary to check the case: 7109 * the capacity of the device is bigger than 7110 * what the max hba cdb can support. Because 7111 * sd_send_scsi_READ_CAPACITY will retrieve 7112 * the capacity by sending USCSI command, which 7113 * is constrained by the max hba cdb. Actually, 7114 * sd_send_scsi_READ_CAPACITY will return 7115 * EINVAL when using bigger cdb than required 7116 * cdb length. Will handle this case in 7117 * "case EINVAL". 7118 */ 7119 7120 /* 7121 * The following relies on 7122 * sd_send_scsi_READ_CAPACITY never 7123 * returning 0 for capacity and/or lbasize. 7124 */ 7125 sd_update_block_info(un, lbasize, capacity); 7126 7127 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7128 "sd_unit_attach: un:0x%p capacity = %ld " 7129 "blocks; lbasize= %ld.\n", un, 7130 un->un_blockcount, un->un_tgt_blocksize); 7131 7132 break; 7133 } 7134 case EINVAL: 7135 /* 7136 * In the case where the max-cdb-length property 7137 * is smaller than the required CDB length for 7138 * a SCSI device, a target driver can fail to 7139 * attach to that device. 7140 */ 7141 scsi_log(SD_DEVINFO(un), 7142 sd_label, CE_WARN, 7143 "disk capacity is too large " 7144 "for current cdb length"); 7145 goto spinup_failed; 7146 case EACCES: 7147 /* 7148 * Should never get here if the spin-up 7149 * succeeded, but code it in anyway. 7150 * From here, just continue with the attach... 7151 */ 7152 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7153 "sd_unit_attach: un:0x%p " 7154 "sd_send_scsi_READ_CAPACITY " 7155 "returned reservation conflict\n", un); 7156 reservation_flag = SD_TARGET_IS_RESERVED; 7157 break; 7158 default: 7159 /* 7160 * Likewise, should never get here if the 7161 * spin-up succeeded. Just continue with 7162 * the attach... 7163 */ 7164 break; 7165 } 7166 break; 7167 case EACCES: 7168 /* 7169 * Device is reserved by another host. In this case 7170 * we could not spin it up or read the capacity, but 7171 * we continue with the attach anyway. 7172 */ 7173 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7174 "sd_unit_attach: un:0x%p spin-up reservation " 7175 "conflict.\n", un); 7176 reservation_flag = SD_TARGET_IS_RESERVED; 7177 break; 7178 default: 7179 /* Fail the attach if the spin-up failed. */ 7180 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7181 "sd_unit_attach: un:0x%p spin-up failed.", un); 7182 goto spinup_failed; 7183 } 7184 } 7185 7186 /* 7187 * Check to see if this is a MMC drive 7188 */ 7189 if (ISCD(un)) { 7190 sd_set_mmc_caps(un); 7191 } 7192 7193 7194 /* 7195 * Add a zero-length attribute to tell the world we support 7196 * kernel ioctls (for layered drivers) 7197 */ 7198 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7199 DDI_KERNEL_IOCTL, NULL, 0); 7200 7201 /* 7202 * Add a boolean property to tell the world we support 7203 * the B_FAILFAST flag (for layered drivers) 7204 */ 7205 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 7206 "ddi-failfast-supported", NULL, 0); 7207 7208 /* 7209 * Initialize power management 7210 */ 7211 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 7212 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 7213 sd_setup_pm(un, devi); 7214 if (un->un_f_pm_is_enabled == FALSE) { 7215 /* 7216 * For performance, point to a jump table that does 7217 * not include pm. 7218 * The direct and priority chains don't change with PM. 7219 * 7220 * Note: this is currently done based on individual device 7221 * capabilities. When an interface for determining system 7222 * power enabled state becomes available, or when additional 7223 * layers are added to the command chain, these values will 7224 * have to be re-evaluated for correctness. 7225 */ 7226 if (un->un_f_non_devbsize_supported) { 7227 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 7228 } else { 7229 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 7230 } 7231 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 7232 } 7233 7234 /* 7235 * This property is set to 0 by HA software to avoid retries 7236 * on a reserved disk. (The preferred property name is 7237 * "retry-on-reservation-conflict") (1189689) 7238 * 7239 * Note: The use of a global here can have unintended consequences. A 7240 * per instance variable is preferrable to match the capabilities of 7241 * different underlying hba's (4402600) 7242 */ 7243 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 7244 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 7245 sd_retry_on_reservation_conflict); 7246 if (sd_retry_on_reservation_conflict != 0) { 7247 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 7248 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 7249 sd_retry_on_reservation_conflict); 7250 } 7251 7252 /* Set up options for QFULL handling. */ 7253 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7254 "qfull-retries", -1)) != -1) { 7255 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 7256 rval, 1); 7257 } 7258 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 7259 "qfull-retry-interval", -1)) != -1) { 7260 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 7261 rval, 1); 7262 } 7263 7264 /* 7265 * This just prints a message that announces the existence of the 7266 * device. The message is always printed in the system logfile, but 7267 * only appears on the console if the system is booted with the 7268 * -v (verbose) argument. 7269 */ 7270 ddi_report_dev(devi); 7271 7272 un->un_mediastate = DKIO_NONE; 7273 7274 cmlb_alloc_handle(&un->un_cmlbhandle); 7275 7276 #if defined(__i386) || defined(__amd64) 7277 /* 7278 * On x86, compensate for off-by-1 legacy error 7279 */ 7280 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable && 7281 (lbasize == un->un_sys_blocksize)) 7282 offbyone = CMLB_OFF_BY_ONE; 7283 #endif 7284 7285 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype, 7286 un->un_f_has_removable_media, un->un_f_is_hotpluggable, 7287 un->un_node_type, offbyone, un->un_cmlbhandle, 7288 (void *)SD_PATH_DIRECT) != 0) { 7289 goto cmlb_attach_failed; 7290 } 7291 7292 7293 /* 7294 * Read and validate the device's geometry (ie, disk label) 7295 * A new unformatted drive will not have a valid geometry, but 7296 * the driver needs to successfully attach to this device so 7297 * the drive can be formatted via ioctls. 7298 */ 7299 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0, 7300 (void *)SD_PATH_DIRECT) == 0) ? 1: 0; 7301 7302 mutex_enter(SD_MUTEX(un)); 7303 7304 /* 7305 * Read and initialize the devid for the unit. 7306 */ 7307 ASSERT(un->un_errstats != NULL); 7308 if (un->un_f_devid_supported) { 7309 sd_register_devid(un, devi, reservation_flag); 7310 } 7311 mutex_exit(SD_MUTEX(un)); 7312 7313 #if (defined(__fibre)) 7314 /* 7315 * Register callbacks for fibre only. You can't do this soley 7316 * on the basis of the devid_type because this is hba specific. 7317 * We need to query our hba capabilities to find out whether to 7318 * register or not. 7319 */ 7320 if (un->un_f_is_fibre) { 7321 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7322 sd_init_event_callbacks(un); 7323 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7324 "sd_unit_attach: un:0x%p event callbacks inserted", 7325 un); 7326 } 7327 } 7328 #endif 7329 7330 if (un->un_f_opt_disable_cache == TRUE) { 7331 /* 7332 * Disable both read cache and write cache. This is 7333 * the historic behavior of the keywords in the config file. 7334 */ 7335 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 7336 0) { 7337 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7338 "sd_unit_attach: un:0x%p Could not disable " 7339 "caching", un); 7340 goto devid_failed; 7341 } 7342 } 7343 7344 /* 7345 * Check the value of the WCE bit now and 7346 * set un_f_write_cache_enabled accordingly. 7347 */ 7348 (void) sd_get_write_cache_enabled(un, &wc_enabled); 7349 mutex_enter(SD_MUTEX(un)); 7350 un->un_f_write_cache_enabled = (wc_enabled != 0); 7351 mutex_exit(SD_MUTEX(un)); 7352 7353 /* 7354 * Check the value of the NV_SUP bit and set 7355 * un_f_suppress_cache_flush accordingly. 7356 */ 7357 sd_get_nv_sup(un); 7358 7359 /* 7360 * Find out what type of reservation this disk supports. 7361 */ 7362 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 7363 case 0: 7364 /* 7365 * SCSI-3 reservations are supported. 7366 */ 7367 un->un_reservation_type = SD_SCSI3_RESERVATION; 7368 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7369 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 7370 break; 7371 case ENOTSUP: 7372 /* 7373 * The PERSISTENT RESERVE IN command would not be recognized by 7374 * a SCSI-2 device, so assume the reservation type is SCSI-2. 7375 */ 7376 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7377 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 7378 un->un_reservation_type = SD_SCSI2_RESERVATION; 7379 break; 7380 default: 7381 /* 7382 * default to SCSI-3 reservations 7383 */ 7384 SD_INFO(SD_LOG_ATTACH_DETACH, un, 7385 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 7386 un->un_reservation_type = SD_SCSI3_RESERVATION; 7387 break; 7388 } 7389 7390 /* 7391 * Set the pstat and error stat values here, so data obtained during the 7392 * previous attach-time routines is available. 7393 * 7394 * Note: This is a critical sequence that needs to be maintained: 7395 * 1) Instantiate the kstats before any routines using the iopath 7396 * (i.e. sd_send_scsi_cmd). 7397 * 2) Initialize the error stats (sd_set_errstats) and partition 7398 * stats (sd_set_pstats)here, following 7399 * cmlb_validate_geometry(), sd_register_devid(), and 7400 * sd_cache_control(). 7401 */ 7402 7403 if (un->un_f_pkstats_enabled && geom_label_valid) { 7404 sd_set_pstats(un); 7405 SD_TRACE(SD_LOG_IO_PARTITION, un, 7406 "sd_unit_attach: un:0x%p pstats created and set\n", un); 7407 } 7408 7409 sd_set_errstats(un); 7410 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7411 "sd_unit_attach: un:0x%p errstats set\n", un); 7412 7413 7414 /* 7415 * After successfully attaching an instance, we record the information 7416 * of how many luns have been attached on the relative target and 7417 * controller for parallel SCSI. This information is used when sd tries 7418 * to set the tagged queuing capability in HBA. 7419 */ 7420 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 7421 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 7422 } 7423 7424 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 7425 "sd_unit_attach: un:0x%p exit success\n", un); 7426 7427 return (DDI_SUCCESS); 7428 7429 /* 7430 * An error occurred during the attach; clean up & return failure. 7431 */ 7432 7433 devid_failed: 7434 7435 setup_pm_failed: 7436 ddi_remove_minor_node(devi, NULL); 7437 7438 cmlb_attach_failed: 7439 /* 7440 * Cleanup from the scsi_ifsetcap() calls (437868) 7441 */ 7442 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7443 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7444 7445 /* 7446 * Refer to the comments of setting tagged-qing in the beginning of 7447 * sd_unit_attach. We can only disable tagged queuing when there is 7448 * no lun attached on the target. 7449 */ 7450 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 7451 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7452 } 7453 7454 if (un->un_f_is_fibre == FALSE) { 7455 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7456 } 7457 7458 spinup_failed: 7459 7460 mutex_enter(SD_MUTEX(un)); 7461 7462 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 7463 if (un->un_direct_priority_timeid != NULL) { 7464 timeout_id_t temp_id = un->un_direct_priority_timeid; 7465 un->un_direct_priority_timeid = NULL; 7466 mutex_exit(SD_MUTEX(un)); 7467 (void) untimeout(temp_id); 7468 mutex_enter(SD_MUTEX(un)); 7469 } 7470 7471 /* Cancel any pending start/stop timeouts */ 7472 if (un->un_startstop_timeid != NULL) { 7473 timeout_id_t temp_id = un->un_startstop_timeid; 7474 un->un_startstop_timeid = NULL; 7475 mutex_exit(SD_MUTEX(un)); 7476 (void) untimeout(temp_id); 7477 mutex_enter(SD_MUTEX(un)); 7478 } 7479 7480 /* Cancel any pending reset-throttle timeouts */ 7481 if (un->un_reset_throttle_timeid != NULL) { 7482 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7483 un->un_reset_throttle_timeid = NULL; 7484 mutex_exit(SD_MUTEX(un)); 7485 (void) untimeout(temp_id); 7486 mutex_enter(SD_MUTEX(un)); 7487 } 7488 7489 /* Cancel any pending retry timeouts */ 7490 if (un->un_retry_timeid != NULL) { 7491 timeout_id_t temp_id = un->un_retry_timeid; 7492 un->un_retry_timeid = NULL; 7493 mutex_exit(SD_MUTEX(un)); 7494 (void) untimeout(temp_id); 7495 mutex_enter(SD_MUTEX(un)); 7496 } 7497 7498 /* Cancel any pending delayed cv broadcast timeouts */ 7499 if (un->un_dcvb_timeid != NULL) { 7500 timeout_id_t temp_id = un->un_dcvb_timeid; 7501 un->un_dcvb_timeid = NULL; 7502 mutex_exit(SD_MUTEX(un)); 7503 (void) untimeout(temp_id); 7504 mutex_enter(SD_MUTEX(un)); 7505 } 7506 7507 mutex_exit(SD_MUTEX(un)); 7508 7509 /* There should not be any in-progress I/O so ASSERT this check */ 7510 ASSERT(un->un_ncmds_in_transport == 0); 7511 ASSERT(un->un_ncmds_in_driver == 0); 7512 7513 /* Do not free the softstate if the callback routine is active */ 7514 sd_sync_with_callback(un); 7515 7516 /* 7517 * Partition stats apparently are not used with removables. These would 7518 * not have been created during attach, so no need to clean them up... 7519 */ 7520 if (un->un_stats != NULL) { 7521 kstat_delete(un->un_stats); 7522 un->un_stats = NULL; 7523 } 7524 if (un->un_errstats != NULL) { 7525 kstat_delete(un->un_errstats); 7526 un->un_errstats = NULL; 7527 } 7528 7529 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 7530 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 7531 7532 ddi_prop_remove_all(devi); 7533 sema_destroy(&un->un_semoclose); 7534 cv_destroy(&un->un_state_cv); 7535 7536 getrbuf_failed: 7537 7538 sd_free_rqs(un); 7539 7540 alloc_rqs_failed: 7541 7542 devp->sd_private = NULL; 7543 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 7544 7545 get_softstate_failed: 7546 /* 7547 * Note: the man pages are unclear as to whether or not doing a 7548 * ddi_soft_state_free(sd_state, instance) is the right way to 7549 * clean up after the ddi_soft_state_zalloc() if the subsequent 7550 * ddi_get_soft_state() fails. The implication seems to be 7551 * that the get_soft_state cannot fail if the zalloc succeeds. 7552 */ 7553 ddi_soft_state_free(sd_state, instance); 7554 7555 probe_failed: 7556 scsi_unprobe(devp); 7557 7558 return (DDI_FAILURE); 7559 } 7560 7561 7562 /* 7563 * Function: sd_unit_detach 7564 * 7565 * Description: Performs DDI_DETACH processing for sddetach(). 7566 * 7567 * Return Code: DDI_SUCCESS 7568 * DDI_FAILURE 7569 * 7570 * Context: Kernel thread context 7571 */ 7572 7573 static int 7574 sd_unit_detach(dev_info_t *devi) 7575 { 7576 struct scsi_device *devp; 7577 struct sd_lun *un; 7578 int i; 7579 int tgt; 7580 dev_t dev; 7581 dev_info_t *pdip = ddi_get_parent(devi); 7582 int instance = ddi_get_instance(devi); 7583 7584 mutex_enter(&sd_detach_mutex); 7585 7586 /* 7587 * Fail the detach for any of the following: 7588 * - Unable to get the sd_lun struct for the instance 7589 * - A layered driver has an outstanding open on the instance 7590 * - Another thread is already detaching this instance 7591 * - Another thread is currently performing an open 7592 */ 7593 devp = ddi_get_driver_private(devi); 7594 if ((devp == NULL) || 7595 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 7596 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 7597 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 7598 mutex_exit(&sd_detach_mutex); 7599 return (DDI_FAILURE); 7600 } 7601 7602 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 7603 7604 /* 7605 * Mark this instance as currently in a detach, to inhibit any 7606 * opens from a layered driver. 7607 */ 7608 un->un_detach_count++; 7609 mutex_exit(&sd_detach_mutex); 7610 7611 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7612 SCSI_ADDR_PROP_TARGET, -1); 7613 7614 dev = sd_make_device(SD_DEVINFO(un)); 7615 7616 #ifndef lint 7617 _NOTE(COMPETING_THREADS_NOW); 7618 #endif 7619 7620 mutex_enter(SD_MUTEX(un)); 7621 7622 /* 7623 * Fail the detach if there are any outstanding layered 7624 * opens on this device. 7625 */ 7626 for (i = 0; i < NDKMAP; i++) { 7627 if (un->un_ocmap.lyropen[i] != 0) { 7628 goto err_notclosed; 7629 } 7630 } 7631 7632 /* 7633 * Verify there are NO outstanding commands issued to this device. 7634 * ie, un_ncmds_in_transport == 0. 7635 * It's possible to have outstanding commands through the physio 7636 * code path, even though everything's closed. 7637 */ 7638 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 7639 (un->un_direct_priority_timeid != NULL) || 7640 (un->un_state == SD_STATE_RWAIT)) { 7641 mutex_exit(SD_MUTEX(un)); 7642 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7643 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 7644 goto err_stillbusy; 7645 } 7646 7647 /* 7648 * If we have the device reserved, release the reservation. 7649 */ 7650 if ((un->un_resvd_status & SD_RESERVE) && 7651 !(un->un_resvd_status & SD_LOST_RESERVE)) { 7652 mutex_exit(SD_MUTEX(un)); 7653 /* 7654 * Note: sd_reserve_release sends a command to the device 7655 * via the sd_ioctlcmd() path, and can sleep. 7656 */ 7657 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 7658 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7659 "sd_dr_detach: Cannot release reservation \n"); 7660 } 7661 } else { 7662 mutex_exit(SD_MUTEX(un)); 7663 } 7664 7665 /* 7666 * Untimeout any reserve recover, throttle reset, restart unit 7667 * and delayed broadcast timeout threads. Protect the timeout pointer 7668 * from getting nulled by their callback functions. 7669 */ 7670 mutex_enter(SD_MUTEX(un)); 7671 if (un->un_resvd_timeid != NULL) { 7672 timeout_id_t temp_id = un->un_resvd_timeid; 7673 un->un_resvd_timeid = NULL; 7674 mutex_exit(SD_MUTEX(un)); 7675 (void) untimeout(temp_id); 7676 mutex_enter(SD_MUTEX(un)); 7677 } 7678 7679 if (un->un_reset_throttle_timeid != NULL) { 7680 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7681 un->un_reset_throttle_timeid = NULL; 7682 mutex_exit(SD_MUTEX(un)); 7683 (void) untimeout(temp_id); 7684 mutex_enter(SD_MUTEX(un)); 7685 } 7686 7687 if (un->un_startstop_timeid != NULL) { 7688 timeout_id_t temp_id = un->un_startstop_timeid; 7689 un->un_startstop_timeid = NULL; 7690 mutex_exit(SD_MUTEX(un)); 7691 (void) untimeout(temp_id); 7692 mutex_enter(SD_MUTEX(un)); 7693 } 7694 7695 if (un->un_dcvb_timeid != NULL) { 7696 timeout_id_t temp_id = un->un_dcvb_timeid; 7697 un->un_dcvb_timeid = NULL; 7698 mutex_exit(SD_MUTEX(un)); 7699 (void) untimeout(temp_id); 7700 } else { 7701 mutex_exit(SD_MUTEX(un)); 7702 } 7703 7704 /* Remove any pending reservation reclaim requests for this device */ 7705 sd_rmv_resv_reclaim_req(dev); 7706 7707 mutex_enter(SD_MUTEX(un)); 7708 7709 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 7710 if (un->un_direct_priority_timeid != NULL) { 7711 timeout_id_t temp_id = un->un_direct_priority_timeid; 7712 un->un_direct_priority_timeid = NULL; 7713 mutex_exit(SD_MUTEX(un)); 7714 (void) untimeout(temp_id); 7715 mutex_enter(SD_MUTEX(un)); 7716 } 7717 7718 /* Cancel any active multi-host disk watch thread requests */ 7719 if (un->un_mhd_token != NULL) { 7720 mutex_exit(SD_MUTEX(un)); 7721 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 7722 if (scsi_watch_request_terminate(un->un_mhd_token, 7723 SCSI_WATCH_TERMINATE_NOWAIT)) { 7724 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7725 "sd_dr_detach: Cannot cancel mhd watch request\n"); 7726 /* 7727 * Note: We are returning here after having removed 7728 * some driver timeouts above. This is consistent with 7729 * the legacy implementation but perhaps the watch 7730 * terminate call should be made with the wait flag set. 7731 */ 7732 goto err_stillbusy; 7733 } 7734 mutex_enter(SD_MUTEX(un)); 7735 un->un_mhd_token = NULL; 7736 } 7737 7738 if (un->un_swr_token != NULL) { 7739 mutex_exit(SD_MUTEX(un)); 7740 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 7741 if (scsi_watch_request_terminate(un->un_swr_token, 7742 SCSI_WATCH_TERMINATE_NOWAIT)) { 7743 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7744 "sd_dr_detach: Cannot cancel swr watch request\n"); 7745 /* 7746 * Note: We are returning here after having removed 7747 * some driver timeouts above. This is consistent with 7748 * the legacy implementation but perhaps the watch 7749 * terminate call should be made with the wait flag set. 7750 */ 7751 goto err_stillbusy; 7752 } 7753 mutex_enter(SD_MUTEX(un)); 7754 un->un_swr_token = NULL; 7755 } 7756 7757 mutex_exit(SD_MUTEX(un)); 7758 7759 /* 7760 * Clear any scsi_reset_notifies. We clear the reset notifies 7761 * if we have not registered one. 7762 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 7763 */ 7764 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 7765 sd_mhd_reset_notify_cb, (caddr_t)un); 7766 7767 /* 7768 * protect the timeout pointers from getting nulled by 7769 * their callback functions during the cancellation process. 7770 * In such a scenario untimeout can be invoked with a null value. 7771 */ 7772 _NOTE(NO_COMPETING_THREADS_NOW); 7773 7774 mutex_enter(&un->un_pm_mutex); 7775 if (un->un_pm_idle_timeid != NULL) { 7776 timeout_id_t temp_id = un->un_pm_idle_timeid; 7777 un->un_pm_idle_timeid = NULL; 7778 mutex_exit(&un->un_pm_mutex); 7779 7780 /* 7781 * Timeout is active; cancel it. 7782 * Note that it'll never be active on a device 7783 * that does not support PM therefore we don't 7784 * have to check before calling pm_idle_component. 7785 */ 7786 (void) untimeout(temp_id); 7787 (void) pm_idle_component(SD_DEVINFO(un), 0); 7788 mutex_enter(&un->un_pm_mutex); 7789 } 7790 7791 /* 7792 * Check whether there is already a timeout scheduled for power 7793 * management. If yes then don't lower the power here, that's. 7794 * the timeout handler's job. 7795 */ 7796 if (un->un_pm_timeid != NULL) { 7797 timeout_id_t temp_id = un->un_pm_timeid; 7798 un->un_pm_timeid = NULL; 7799 mutex_exit(&un->un_pm_mutex); 7800 /* 7801 * Timeout is active; cancel it. 7802 * Note that it'll never be active on a device 7803 * that does not support PM therefore we don't 7804 * have to check before calling pm_idle_component. 7805 */ 7806 (void) untimeout(temp_id); 7807 (void) pm_idle_component(SD_DEVINFO(un), 0); 7808 7809 } else { 7810 mutex_exit(&un->un_pm_mutex); 7811 if ((un->un_f_pm_is_enabled == TRUE) && 7812 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 7813 DDI_SUCCESS)) { 7814 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7815 "sd_dr_detach: Lower power request failed, ignoring.\n"); 7816 /* 7817 * Fix for bug: 4297749, item # 13 7818 * The above test now includes a check to see if PM is 7819 * supported by this device before call 7820 * pm_lower_power(). 7821 * Note, the following is not dead code. The call to 7822 * pm_lower_power above will generate a call back into 7823 * our sdpower routine which might result in a timeout 7824 * handler getting activated. Therefore the following 7825 * code is valid and necessary. 7826 */ 7827 mutex_enter(&un->un_pm_mutex); 7828 if (un->un_pm_timeid != NULL) { 7829 timeout_id_t temp_id = un->un_pm_timeid; 7830 un->un_pm_timeid = NULL; 7831 mutex_exit(&un->un_pm_mutex); 7832 (void) untimeout(temp_id); 7833 (void) pm_idle_component(SD_DEVINFO(un), 0); 7834 } else { 7835 mutex_exit(&un->un_pm_mutex); 7836 } 7837 } 7838 } 7839 7840 /* 7841 * Cleanup from the scsi_ifsetcap() calls (437868) 7842 * Relocated here from above to be after the call to 7843 * pm_lower_power, which was getting errors. 7844 */ 7845 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 7846 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 7847 7848 /* 7849 * Currently, tagged queuing is supported per target based by HBA. 7850 * Setting this per lun instance actually sets the capability of this 7851 * target in HBA, which affects those luns already attached on the 7852 * same target. So during detach, we can only disable this capability 7853 * only when this is the only lun left on this target. By doing 7854 * this, we assume a target has the same tagged queuing capability 7855 * for every lun. The condition can be removed when HBA is changed to 7856 * support per lun based tagged queuing capability. 7857 */ 7858 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 7859 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 7860 } 7861 7862 if (un->un_f_is_fibre == FALSE) { 7863 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 7864 } 7865 7866 /* 7867 * Remove any event callbacks, fibre only 7868 */ 7869 if (un->un_f_is_fibre == TRUE) { 7870 if ((un->un_insert_event != NULL) && 7871 (ddi_remove_event_handler(un->un_insert_cb_id) != 7872 DDI_SUCCESS)) { 7873 /* 7874 * Note: We are returning here after having done 7875 * substantial cleanup above. This is consistent 7876 * with the legacy implementation but this may not 7877 * be the right thing to do. 7878 */ 7879 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7880 "sd_dr_detach: Cannot cancel insert event\n"); 7881 goto err_remove_event; 7882 } 7883 un->un_insert_event = NULL; 7884 7885 if ((un->un_remove_event != NULL) && 7886 (ddi_remove_event_handler(un->un_remove_cb_id) != 7887 DDI_SUCCESS)) { 7888 /* 7889 * Note: We are returning here after having done 7890 * substantial cleanup above. This is consistent 7891 * with the legacy implementation but this may not 7892 * be the right thing to do. 7893 */ 7894 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 7895 "sd_dr_detach: Cannot cancel remove event\n"); 7896 goto err_remove_event; 7897 } 7898 un->un_remove_event = NULL; 7899 } 7900 7901 /* Do not free the softstate if the callback routine is active */ 7902 sd_sync_with_callback(un); 7903 7904 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 7905 cmlb_free_handle(&un->un_cmlbhandle); 7906 7907 /* 7908 * Hold the detach mutex here, to make sure that no other threads ever 7909 * can access a (partially) freed soft state structure. 7910 */ 7911 mutex_enter(&sd_detach_mutex); 7912 7913 /* 7914 * Clean up the soft state struct. 7915 * Cleanup is done in reverse order of allocs/inits. 7916 * At this point there should be no competing threads anymore. 7917 */ 7918 7919 /* Unregister and free device id. */ 7920 ddi_devid_unregister(devi); 7921 if (un->un_devid) { 7922 ddi_devid_free(un->un_devid); 7923 un->un_devid = NULL; 7924 } 7925 7926 /* 7927 * Destroy wmap cache if it exists. 7928 */ 7929 if (un->un_wm_cache != NULL) { 7930 kmem_cache_destroy(un->un_wm_cache); 7931 un->un_wm_cache = NULL; 7932 } 7933 7934 /* 7935 * kstat cleanup is done in detach for all device types (4363169). 7936 * We do not want to fail detach if the device kstats are not deleted 7937 * since there is a confusion about the devo_refcnt for the device. 7938 * We just delete the kstats and let detach complete successfully. 7939 */ 7940 if (un->un_stats != NULL) { 7941 kstat_delete(un->un_stats); 7942 un->un_stats = NULL; 7943 } 7944 if (un->un_errstats != NULL) { 7945 kstat_delete(un->un_errstats); 7946 un->un_errstats = NULL; 7947 } 7948 7949 /* Remove partition stats */ 7950 if (un->un_f_pkstats_enabled) { 7951 for (i = 0; i < NSDMAP; i++) { 7952 if (un->un_pstats[i] != NULL) { 7953 kstat_delete(un->un_pstats[i]); 7954 un->un_pstats[i] = NULL; 7955 } 7956 } 7957 } 7958 7959 /* Remove xbuf registration */ 7960 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 7961 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 7962 7963 /* Remove driver properties */ 7964 ddi_prop_remove_all(devi); 7965 7966 mutex_destroy(&un->un_pm_mutex); 7967 cv_destroy(&un->un_pm_busy_cv); 7968 7969 cv_destroy(&un->un_wcc_cv); 7970 7971 /* Open/close semaphore */ 7972 sema_destroy(&un->un_semoclose); 7973 7974 /* Removable media condvar. */ 7975 cv_destroy(&un->un_state_cv); 7976 7977 /* Suspend/resume condvar. */ 7978 cv_destroy(&un->un_suspend_cv); 7979 cv_destroy(&un->un_disk_busy_cv); 7980 7981 sd_free_rqs(un); 7982 7983 /* Free up soft state */ 7984 devp->sd_private = NULL; 7985 7986 bzero(un, sizeof (struct sd_lun)); 7987 ddi_soft_state_free(sd_state, instance); 7988 7989 mutex_exit(&sd_detach_mutex); 7990 7991 /* This frees up the INQUIRY data associated with the device. */ 7992 scsi_unprobe(devp); 7993 7994 /* 7995 * After successfully detaching an instance, we update the information 7996 * of how many luns have been attached in the relative target and 7997 * controller for parallel SCSI. This information is used when sd tries 7998 * to set the tagged queuing capability in HBA. 7999 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 8000 * check if the device is parallel SCSI. However, we don't need to 8001 * check here because we've already checked during attach. No device 8002 * that is not parallel SCSI is in the chain. 8003 */ 8004 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 8005 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 8006 } 8007 8008 return (DDI_SUCCESS); 8009 8010 err_notclosed: 8011 mutex_exit(SD_MUTEX(un)); 8012 8013 err_stillbusy: 8014 _NOTE(NO_COMPETING_THREADS_NOW); 8015 8016 err_remove_event: 8017 mutex_enter(&sd_detach_mutex); 8018 un->un_detach_count--; 8019 mutex_exit(&sd_detach_mutex); 8020 8021 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 8022 return (DDI_FAILURE); 8023 } 8024 8025 8026 /* 8027 * Function: sd_create_errstats 8028 * 8029 * Description: This routine instantiates the device error stats. 8030 * 8031 * Note: During attach the stats are instantiated first so they are 8032 * available for attach-time routines that utilize the driver 8033 * iopath to send commands to the device. The stats are initialized 8034 * separately so data obtained during some attach-time routines is 8035 * available. (4362483) 8036 * 8037 * Arguments: un - driver soft state (unit) structure 8038 * instance - driver instance 8039 * 8040 * Context: Kernel thread context 8041 */ 8042 8043 static void 8044 sd_create_errstats(struct sd_lun *un, int instance) 8045 { 8046 struct sd_errstats *stp; 8047 char kstatmodule_err[KSTAT_STRLEN]; 8048 char kstatname[KSTAT_STRLEN]; 8049 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 8050 8051 ASSERT(un != NULL); 8052 8053 if (un->un_errstats != NULL) { 8054 return; 8055 } 8056 8057 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 8058 "%serr", sd_label); 8059 (void) snprintf(kstatname, sizeof (kstatname), 8060 "%s%d,err", sd_label, instance); 8061 8062 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 8063 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 8064 8065 if (un->un_errstats == NULL) { 8066 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8067 "sd_create_errstats: Failed kstat_create\n"); 8068 return; 8069 } 8070 8071 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8072 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 8073 KSTAT_DATA_UINT32); 8074 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 8075 KSTAT_DATA_UINT32); 8076 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 8077 KSTAT_DATA_UINT32); 8078 kstat_named_init(&stp->sd_vid, "Vendor", 8079 KSTAT_DATA_CHAR); 8080 kstat_named_init(&stp->sd_pid, "Product", 8081 KSTAT_DATA_CHAR); 8082 kstat_named_init(&stp->sd_revision, "Revision", 8083 KSTAT_DATA_CHAR); 8084 kstat_named_init(&stp->sd_serial, "Serial No", 8085 KSTAT_DATA_CHAR); 8086 kstat_named_init(&stp->sd_capacity, "Size", 8087 KSTAT_DATA_ULONGLONG); 8088 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 8089 KSTAT_DATA_UINT32); 8090 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 8091 KSTAT_DATA_UINT32); 8092 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 8093 KSTAT_DATA_UINT32); 8094 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 8095 KSTAT_DATA_UINT32); 8096 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 8097 KSTAT_DATA_UINT32); 8098 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 8099 KSTAT_DATA_UINT32); 8100 8101 un->un_errstats->ks_private = un; 8102 un->un_errstats->ks_update = nulldev; 8103 8104 kstat_install(un->un_errstats); 8105 } 8106 8107 8108 /* 8109 * Function: sd_set_errstats 8110 * 8111 * Description: This routine sets the value of the vendor id, product id, 8112 * revision, serial number, and capacity device error stats. 8113 * 8114 * Note: During attach the stats are instantiated first so they are 8115 * available for attach-time routines that utilize the driver 8116 * iopath to send commands to the device. The stats are initialized 8117 * separately so data obtained during some attach-time routines is 8118 * available. (4362483) 8119 * 8120 * Arguments: un - driver soft state (unit) structure 8121 * 8122 * Context: Kernel thread context 8123 */ 8124 8125 static void 8126 sd_set_errstats(struct sd_lun *un) 8127 { 8128 struct sd_errstats *stp; 8129 8130 ASSERT(un != NULL); 8131 ASSERT(un->un_errstats != NULL); 8132 stp = (struct sd_errstats *)un->un_errstats->ks_data; 8133 ASSERT(stp != NULL); 8134 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 8135 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 8136 (void) strncpy(stp->sd_revision.value.c, 8137 un->un_sd->sd_inq->inq_revision, 4); 8138 8139 /* 8140 * All the errstats are persistent across detach/attach, 8141 * so reset all the errstats here in case of the hot 8142 * replacement of disk drives, except for not changed 8143 * Sun qualified drives. 8144 */ 8145 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 8146 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8147 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 8148 stp->sd_softerrs.value.ui32 = 0; 8149 stp->sd_harderrs.value.ui32 = 0; 8150 stp->sd_transerrs.value.ui32 = 0; 8151 stp->sd_rq_media_err.value.ui32 = 0; 8152 stp->sd_rq_ntrdy_err.value.ui32 = 0; 8153 stp->sd_rq_nodev_err.value.ui32 = 0; 8154 stp->sd_rq_recov_err.value.ui32 = 0; 8155 stp->sd_rq_illrq_err.value.ui32 = 0; 8156 stp->sd_rq_pfa_err.value.ui32 = 0; 8157 } 8158 8159 /* 8160 * Set the "Serial No" kstat for Sun qualified drives (indicated by 8161 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 8162 * (4376302)) 8163 */ 8164 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 8165 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 8166 sizeof (SD_INQUIRY(un)->inq_serial)); 8167 } 8168 8169 if (un->un_f_blockcount_is_valid != TRUE) { 8170 /* 8171 * Set capacity error stat to 0 for no media. This ensures 8172 * a valid capacity is displayed in response to 'iostat -E' 8173 * when no media is present in the device. 8174 */ 8175 stp->sd_capacity.value.ui64 = 0; 8176 } else { 8177 /* 8178 * Multiply un_blockcount by un->un_sys_blocksize to get 8179 * capacity. 8180 * 8181 * Note: for non-512 blocksize devices "un_blockcount" has been 8182 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 8183 * (un_tgt_blocksize / un->un_sys_blocksize). 8184 */ 8185 stp->sd_capacity.value.ui64 = (uint64_t) 8186 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 8187 } 8188 } 8189 8190 8191 /* 8192 * Function: sd_set_pstats 8193 * 8194 * Description: This routine instantiates and initializes the partition 8195 * stats for each partition with more than zero blocks. 8196 * (4363169) 8197 * 8198 * Arguments: un - driver soft state (unit) structure 8199 * 8200 * Context: Kernel thread context 8201 */ 8202 8203 static void 8204 sd_set_pstats(struct sd_lun *un) 8205 { 8206 char kstatname[KSTAT_STRLEN]; 8207 int instance; 8208 int i; 8209 diskaddr_t nblks = 0; 8210 char *partname = NULL; 8211 8212 ASSERT(un != NULL); 8213 8214 instance = ddi_get_instance(SD_DEVINFO(un)); 8215 8216 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 8217 for (i = 0; i < NSDMAP; i++) { 8218 8219 if (cmlb_partinfo(un->un_cmlbhandle, i, 8220 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0) 8221 continue; 8222 mutex_enter(SD_MUTEX(un)); 8223 8224 if ((un->un_pstats[i] == NULL) && 8225 (nblks != 0)) { 8226 8227 (void) snprintf(kstatname, sizeof (kstatname), 8228 "%s%d,%s", sd_label, instance, 8229 partname); 8230 8231 un->un_pstats[i] = kstat_create(sd_label, 8232 instance, kstatname, "partition", KSTAT_TYPE_IO, 8233 1, KSTAT_FLAG_PERSISTENT); 8234 if (un->un_pstats[i] != NULL) { 8235 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 8236 kstat_install(un->un_pstats[i]); 8237 } 8238 } 8239 mutex_exit(SD_MUTEX(un)); 8240 } 8241 } 8242 8243 8244 #if (defined(__fibre)) 8245 /* 8246 * Function: sd_init_event_callbacks 8247 * 8248 * Description: This routine initializes the insertion and removal event 8249 * callbacks. (fibre only) 8250 * 8251 * Arguments: un - driver soft state (unit) structure 8252 * 8253 * Context: Kernel thread context 8254 */ 8255 8256 static void 8257 sd_init_event_callbacks(struct sd_lun *un) 8258 { 8259 ASSERT(un != NULL); 8260 8261 if ((un->un_insert_event == NULL) && 8262 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 8263 &un->un_insert_event) == DDI_SUCCESS)) { 8264 /* 8265 * Add the callback for an insertion event 8266 */ 8267 (void) ddi_add_event_handler(SD_DEVINFO(un), 8268 un->un_insert_event, sd_event_callback, (void *)un, 8269 &(un->un_insert_cb_id)); 8270 } 8271 8272 if ((un->un_remove_event == NULL) && 8273 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 8274 &un->un_remove_event) == DDI_SUCCESS)) { 8275 /* 8276 * Add the callback for a removal event 8277 */ 8278 (void) ddi_add_event_handler(SD_DEVINFO(un), 8279 un->un_remove_event, sd_event_callback, (void *)un, 8280 &(un->un_remove_cb_id)); 8281 } 8282 } 8283 8284 8285 /* 8286 * Function: sd_event_callback 8287 * 8288 * Description: This routine handles insert/remove events (photon). The 8289 * state is changed to OFFLINE which can be used to supress 8290 * error msgs. (fibre only) 8291 * 8292 * Arguments: un - driver soft state (unit) structure 8293 * 8294 * Context: Callout thread context 8295 */ 8296 /* ARGSUSED */ 8297 static void 8298 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 8299 void *bus_impldata) 8300 { 8301 struct sd_lun *un = (struct sd_lun *)arg; 8302 8303 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 8304 if (event == un->un_insert_event) { 8305 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 8306 mutex_enter(SD_MUTEX(un)); 8307 if (un->un_state == SD_STATE_OFFLINE) { 8308 if (un->un_last_state != SD_STATE_SUSPENDED) { 8309 un->un_state = un->un_last_state; 8310 } else { 8311 /* 8312 * We have gone through SUSPEND/RESUME while 8313 * we were offline. Restore the last state 8314 */ 8315 un->un_state = un->un_save_state; 8316 } 8317 } 8318 mutex_exit(SD_MUTEX(un)); 8319 8320 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 8321 } else if (event == un->un_remove_event) { 8322 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 8323 mutex_enter(SD_MUTEX(un)); 8324 /* 8325 * We need to handle an event callback that occurs during 8326 * the suspend operation, since we don't prevent it. 8327 */ 8328 if (un->un_state != SD_STATE_OFFLINE) { 8329 if (un->un_state != SD_STATE_SUSPENDED) { 8330 New_state(un, SD_STATE_OFFLINE); 8331 } else { 8332 un->un_last_state = SD_STATE_OFFLINE; 8333 } 8334 } 8335 mutex_exit(SD_MUTEX(un)); 8336 } else { 8337 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 8338 "!Unknown event\n"); 8339 } 8340 8341 } 8342 #endif 8343 8344 /* 8345 * Function: sd_cache_control() 8346 * 8347 * Description: This routine is the driver entry point for setting 8348 * read and write caching by modifying the WCE (write cache 8349 * enable) and RCD (read cache disable) bits of mode 8350 * page 8 (MODEPAGE_CACHING). 8351 * 8352 * Arguments: un - driver soft state (unit) structure 8353 * rcd_flag - flag for controlling the read cache 8354 * wce_flag - flag for controlling the write cache 8355 * 8356 * Return Code: EIO 8357 * code returned by sd_send_scsi_MODE_SENSE and 8358 * sd_send_scsi_MODE_SELECT 8359 * 8360 * Context: Kernel Thread 8361 */ 8362 8363 static int 8364 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 8365 { 8366 struct mode_caching *mode_caching_page; 8367 uchar_t *header; 8368 size_t buflen; 8369 int hdrlen; 8370 int bd_len; 8371 int rval = 0; 8372 struct mode_header_grp2 *mhp; 8373 8374 ASSERT(un != NULL); 8375 8376 /* 8377 * Do a test unit ready, otherwise a mode sense may not work if this 8378 * is the first command sent to the device after boot. 8379 */ 8380 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 8381 8382 if (un->un_f_cfg_is_atapi == TRUE) { 8383 hdrlen = MODE_HEADER_LENGTH_GRP2; 8384 } else { 8385 hdrlen = MODE_HEADER_LENGTH; 8386 } 8387 8388 /* 8389 * Allocate memory for the retrieved mode page and its headers. Set 8390 * a pointer to the page itself. Use mode_cache_scsi3 to insure 8391 * we get all of the mode sense data otherwise, the mode select 8392 * will fail. mode_cache_scsi3 is a superset of mode_caching. 8393 */ 8394 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 8395 sizeof (struct mode_cache_scsi3); 8396 8397 header = kmem_zalloc(buflen, KM_SLEEP); 8398 8399 /* Get the information from the device. */ 8400 if (un->un_f_cfg_is_atapi == TRUE) { 8401 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 8402 MODEPAGE_CACHING, SD_PATH_DIRECT); 8403 } else { 8404 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 8405 MODEPAGE_CACHING, SD_PATH_DIRECT); 8406 } 8407 if (rval != 0) { 8408 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8409 "sd_cache_control: Mode Sense Failed\n"); 8410 kmem_free(header, buflen); 8411 return (rval); 8412 } 8413 8414 /* 8415 * Determine size of Block Descriptors in order to locate 8416 * the mode page data. ATAPI devices return 0, SCSI devices 8417 * should return MODE_BLK_DESC_LENGTH. 8418 */ 8419 if (un->un_f_cfg_is_atapi == TRUE) { 8420 mhp = (struct mode_header_grp2 *)header; 8421 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8422 } else { 8423 bd_len = ((struct mode_header *)header)->bdesc_length; 8424 } 8425 8426 if (bd_len > MODE_BLK_DESC_LENGTH) { 8427 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 8428 "sd_cache_control: Mode Sense returned invalid " 8429 "block descriptor length\n"); 8430 kmem_free(header, buflen); 8431 return (EIO); 8432 } 8433 8434 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8435 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8436 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 8437 " caching page code mismatch %d\n", 8438 mode_caching_page->mode_page.code); 8439 kmem_free(header, buflen); 8440 return (EIO); 8441 } 8442 8443 /* Check the relevant bits on successful mode sense. */ 8444 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 8445 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 8446 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 8447 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 8448 8449 size_t sbuflen; 8450 uchar_t save_pg; 8451 8452 /* 8453 * Construct select buffer length based on the 8454 * length of the sense data returned. 8455 */ 8456 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 8457 sizeof (struct mode_page) + 8458 (int)mode_caching_page->mode_page.length; 8459 8460 /* 8461 * Set the caching bits as requested. 8462 */ 8463 if (rcd_flag == SD_CACHE_ENABLE) 8464 mode_caching_page->rcd = 0; 8465 else if (rcd_flag == SD_CACHE_DISABLE) 8466 mode_caching_page->rcd = 1; 8467 8468 if (wce_flag == SD_CACHE_ENABLE) 8469 mode_caching_page->wce = 1; 8470 else if (wce_flag == SD_CACHE_DISABLE) 8471 mode_caching_page->wce = 0; 8472 8473 /* 8474 * Save the page if the mode sense says the 8475 * drive supports it. 8476 */ 8477 save_pg = mode_caching_page->mode_page.ps ? 8478 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 8479 8480 /* Clear reserved bits before mode select. */ 8481 mode_caching_page->mode_page.ps = 0; 8482 8483 /* 8484 * Clear out mode header for mode select. 8485 * The rest of the retrieved page will be reused. 8486 */ 8487 bzero(header, hdrlen); 8488 8489 if (un->un_f_cfg_is_atapi == TRUE) { 8490 mhp = (struct mode_header_grp2 *)header; 8491 mhp->bdesc_length_hi = bd_len >> 8; 8492 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 8493 } else { 8494 ((struct mode_header *)header)->bdesc_length = bd_len; 8495 } 8496 8497 /* Issue mode select to change the cache settings */ 8498 if (un->un_f_cfg_is_atapi == TRUE) { 8499 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 8500 sbuflen, save_pg, SD_PATH_DIRECT); 8501 } else { 8502 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 8503 sbuflen, save_pg, SD_PATH_DIRECT); 8504 } 8505 } 8506 8507 kmem_free(header, buflen); 8508 return (rval); 8509 } 8510 8511 8512 /* 8513 * Function: sd_get_write_cache_enabled() 8514 * 8515 * Description: This routine is the driver entry point for determining if 8516 * write caching is enabled. It examines the WCE (write cache 8517 * enable) bits of mode page 8 (MODEPAGE_CACHING). 8518 * 8519 * Arguments: un - driver soft state (unit) structure 8520 * is_enabled - pointer to int where write cache enabled state 8521 * is returned (non-zero -> write cache enabled) 8522 * 8523 * 8524 * Return Code: EIO 8525 * code returned by sd_send_scsi_MODE_SENSE 8526 * 8527 * Context: Kernel Thread 8528 * 8529 * NOTE: If ioctl is added to disable write cache, this sequence should 8530 * be followed so that no locking is required for accesses to 8531 * un->un_f_write_cache_enabled: 8532 * do mode select to clear wce 8533 * do synchronize cache to flush cache 8534 * set un->un_f_write_cache_enabled = FALSE 8535 * 8536 * Conversely, an ioctl to enable the write cache should be done 8537 * in this order: 8538 * set un->un_f_write_cache_enabled = TRUE 8539 * do mode select to set wce 8540 */ 8541 8542 static int 8543 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 8544 { 8545 struct mode_caching *mode_caching_page; 8546 uchar_t *header; 8547 size_t buflen; 8548 int hdrlen; 8549 int bd_len; 8550 int rval = 0; 8551 8552 ASSERT(un != NULL); 8553 ASSERT(is_enabled != NULL); 8554 8555 /* in case of error, flag as enabled */ 8556 *is_enabled = TRUE; 8557 8558 /* 8559 * Do a test unit ready, otherwise a mode sense may not work if this 8560 * is the first command sent to the device after boot. 8561 */ 8562 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 8563 8564 if (un->un_f_cfg_is_atapi == TRUE) { 8565 hdrlen = MODE_HEADER_LENGTH_GRP2; 8566 } else { 8567 hdrlen = MODE_HEADER_LENGTH; 8568 } 8569 8570 /* 8571 * Allocate memory for the retrieved mode page and its headers. Set 8572 * a pointer to the page itself. 8573 */ 8574 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 8575 header = kmem_zalloc(buflen, KM_SLEEP); 8576 8577 /* Get the information from the device. */ 8578 if (un->un_f_cfg_is_atapi == TRUE) { 8579 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 8580 MODEPAGE_CACHING, SD_PATH_DIRECT); 8581 } else { 8582 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 8583 MODEPAGE_CACHING, SD_PATH_DIRECT); 8584 } 8585 if (rval != 0) { 8586 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 8587 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 8588 kmem_free(header, buflen); 8589 return (rval); 8590 } 8591 8592 /* 8593 * Determine size of Block Descriptors in order to locate 8594 * the mode page data. ATAPI devices return 0, SCSI devices 8595 * should return MODE_BLK_DESC_LENGTH. 8596 */ 8597 if (un->un_f_cfg_is_atapi == TRUE) { 8598 struct mode_header_grp2 *mhp; 8599 mhp = (struct mode_header_grp2 *)header; 8600 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 8601 } else { 8602 bd_len = ((struct mode_header *)header)->bdesc_length; 8603 } 8604 8605 if (bd_len > MODE_BLK_DESC_LENGTH) { 8606 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 8607 "sd_get_write_cache_enabled: Mode Sense returned invalid " 8608 "block descriptor length\n"); 8609 kmem_free(header, buflen); 8610 return (EIO); 8611 } 8612 8613 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 8614 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 8615 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 8616 " caching page code mismatch %d\n", 8617 mode_caching_page->mode_page.code); 8618 kmem_free(header, buflen); 8619 return (EIO); 8620 } 8621 *is_enabled = mode_caching_page->wce; 8622 8623 kmem_free(header, buflen); 8624 return (0); 8625 } 8626 8627 /* 8628 * Function: sd_get_nv_sup() 8629 * 8630 * Description: This routine is the driver entry point for 8631 * determining whether non-volatile cache is supported. This 8632 * determination process works as follows: 8633 * 8634 * 1. sd first queries sd.conf on whether 8635 * suppress_cache_flush bit is set for this device. 8636 * 8637 * 2. if not there, then queries the internal disk table. 8638 * 8639 * 3. if either sd.conf or internal disk table specifies 8640 * cache flush be suppressed, we don't bother checking 8641 * NV_SUP bit. 8642 * 8643 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries 8644 * the optional INQUIRY VPD page 0x86. If the device 8645 * supports VPD page 0x86, sd examines the NV_SUP 8646 * (non-volatile cache support) bit in the INQUIRY VPD page 8647 * 0x86: 8648 * o If NV_SUP bit is set, sd assumes the device has a 8649 * non-volatile cache and set the 8650 * un_f_sync_nv_supported to TRUE. 8651 * o Otherwise cache is not non-volatile, 8652 * un_f_sync_nv_supported is set to FALSE. 8653 * 8654 * Arguments: un - driver soft state (unit) structure 8655 * 8656 * Return Code: 8657 * 8658 * Context: Kernel Thread 8659 */ 8660 8661 static void 8662 sd_get_nv_sup(struct sd_lun *un) 8663 { 8664 int rval = 0; 8665 uchar_t *inq86 = NULL; 8666 size_t inq86_len = MAX_INQUIRY_SIZE; 8667 size_t inq86_resid = 0; 8668 struct dk_callback *dkc; 8669 8670 ASSERT(un != NULL); 8671 8672 mutex_enter(SD_MUTEX(un)); 8673 8674 /* 8675 * Be conservative on the device's support of 8676 * SYNC_NV bit: un_f_sync_nv_supported is 8677 * initialized to be false. 8678 */ 8679 un->un_f_sync_nv_supported = FALSE; 8680 8681 /* 8682 * If either sd.conf or internal disk table 8683 * specifies cache flush be suppressed, then 8684 * we don't bother checking NV_SUP bit. 8685 */ 8686 if (un->un_f_suppress_cache_flush == TRUE) { 8687 mutex_exit(SD_MUTEX(un)); 8688 return; 8689 } 8690 8691 if (sd_check_vpd_page_support(un) == 0 && 8692 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) { 8693 mutex_exit(SD_MUTEX(un)); 8694 /* collect page 86 data if available */ 8695 inq86 = kmem_zalloc(inq86_len, KM_SLEEP); 8696 rval = sd_send_scsi_INQUIRY(un, inq86, inq86_len, 8697 0x01, 0x86, &inq86_resid); 8698 8699 if (rval == 0 && (inq86_len - inq86_resid > 6)) { 8700 SD_TRACE(SD_LOG_COMMON, un, 8701 "sd_get_nv_sup: \ 8702 successfully get VPD page: %x \ 8703 PAGE LENGTH: %x BYTE 6: %x\n", 8704 inq86[1], inq86[3], inq86[6]); 8705 8706 mutex_enter(SD_MUTEX(un)); 8707 /* 8708 * check the value of NV_SUP bit: only if the device 8709 * reports NV_SUP bit to be 1, the 8710 * un_f_sync_nv_supported bit will be set to true. 8711 */ 8712 if (inq86[6] & SD_VPD_NV_SUP) { 8713 un->un_f_sync_nv_supported = TRUE; 8714 } 8715 mutex_exit(SD_MUTEX(un)); 8716 } 8717 kmem_free(inq86, inq86_len); 8718 } else { 8719 mutex_exit(SD_MUTEX(un)); 8720 } 8721 8722 /* 8723 * Send a SYNC CACHE command to check whether 8724 * SYNC_NV bit is supported. This command should have 8725 * un_f_sync_nv_supported set to correct value. 8726 */ 8727 mutex_enter(SD_MUTEX(un)); 8728 if (un->un_f_sync_nv_supported) { 8729 mutex_exit(SD_MUTEX(un)); 8730 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP); 8731 dkc->dkc_flag = FLUSH_VOLATILE; 8732 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 8733 8734 /* 8735 * Send a TEST UNIT READY command to the device. This should 8736 * clear any outstanding UNIT ATTENTION that may be present. 8737 */ 8738 (void) sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR); 8739 8740 kmem_free(dkc, sizeof (struct dk_callback)); 8741 } else { 8742 mutex_exit(SD_MUTEX(un)); 8743 } 8744 8745 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \ 8746 un_f_suppress_cache_flush is set to %d\n", 8747 un->un_f_suppress_cache_flush); 8748 } 8749 8750 /* 8751 * Function: sd_make_device 8752 * 8753 * Description: Utility routine to return the Solaris device number from 8754 * the data in the device's dev_info structure. 8755 * 8756 * Return Code: The Solaris device number 8757 * 8758 * Context: Any 8759 */ 8760 8761 static dev_t 8762 sd_make_device(dev_info_t *devi) 8763 { 8764 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 8765 ddi_get_instance(devi) << SDUNIT_SHIFT)); 8766 } 8767 8768 8769 /* 8770 * Function: sd_pm_entry 8771 * 8772 * Description: Called at the start of a new command to manage power 8773 * and busy status of a device. This includes determining whether 8774 * the current power state of the device is sufficient for 8775 * performing the command or whether it must be changed. 8776 * The PM framework is notified appropriately. 8777 * Only with a return status of DDI_SUCCESS will the 8778 * component be busy to the framework. 8779 * 8780 * All callers of sd_pm_entry must check the return status 8781 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 8782 * of DDI_FAILURE indicates the device failed to power up. 8783 * In this case un_pm_count has been adjusted so the result 8784 * on exit is still powered down, ie. count is less than 0. 8785 * Calling sd_pm_exit with this count value hits an ASSERT. 8786 * 8787 * Return Code: DDI_SUCCESS or DDI_FAILURE 8788 * 8789 * Context: Kernel thread context. 8790 */ 8791 8792 static int 8793 sd_pm_entry(struct sd_lun *un) 8794 { 8795 int return_status = DDI_SUCCESS; 8796 8797 ASSERT(!mutex_owned(SD_MUTEX(un))); 8798 ASSERT(!mutex_owned(&un->un_pm_mutex)); 8799 8800 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 8801 8802 if (un->un_f_pm_is_enabled == FALSE) { 8803 SD_TRACE(SD_LOG_IO_PM, un, 8804 "sd_pm_entry: exiting, PM not enabled\n"); 8805 return (return_status); 8806 } 8807 8808 /* 8809 * Just increment a counter if PM is enabled. On the transition from 8810 * 0 ==> 1, mark the device as busy. The iodone side will decrement 8811 * the count with each IO and mark the device as idle when the count 8812 * hits 0. 8813 * 8814 * If the count is less than 0 the device is powered down. If a powered 8815 * down device is successfully powered up then the count must be 8816 * incremented to reflect the power up. Note that it'll get incremented 8817 * a second time to become busy. 8818 * 8819 * Because the following has the potential to change the device state 8820 * and must release the un_pm_mutex to do so, only one thread can be 8821 * allowed through at a time. 8822 */ 8823 8824 mutex_enter(&un->un_pm_mutex); 8825 while (un->un_pm_busy == TRUE) { 8826 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 8827 } 8828 un->un_pm_busy = TRUE; 8829 8830 if (un->un_pm_count < 1) { 8831 8832 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 8833 8834 /* 8835 * Indicate we are now busy so the framework won't attempt to 8836 * power down the device. This call will only fail if either 8837 * we passed a bad component number or the device has no 8838 * components. Neither of these should ever happen. 8839 */ 8840 mutex_exit(&un->un_pm_mutex); 8841 return_status = pm_busy_component(SD_DEVINFO(un), 0); 8842 ASSERT(return_status == DDI_SUCCESS); 8843 8844 mutex_enter(&un->un_pm_mutex); 8845 8846 if (un->un_pm_count < 0) { 8847 mutex_exit(&un->un_pm_mutex); 8848 8849 SD_TRACE(SD_LOG_IO_PM, un, 8850 "sd_pm_entry: power up component\n"); 8851 8852 /* 8853 * pm_raise_power will cause sdpower to be called 8854 * which brings the device power level to the 8855 * desired state, ON in this case. If successful, 8856 * un_pm_count and un_power_level will be updated 8857 * appropriately. 8858 */ 8859 return_status = pm_raise_power(SD_DEVINFO(un), 0, 8860 SD_SPINDLE_ON); 8861 8862 mutex_enter(&un->un_pm_mutex); 8863 8864 if (return_status != DDI_SUCCESS) { 8865 /* 8866 * Power up failed. 8867 * Idle the device and adjust the count 8868 * so the result on exit is that we're 8869 * still powered down, ie. count is less than 0. 8870 */ 8871 SD_TRACE(SD_LOG_IO_PM, un, 8872 "sd_pm_entry: power up failed," 8873 " idle the component\n"); 8874 8875 (void) pm_idle_component(SD_DEVINFO(un), 0); 8876 un->un_pm_count--; 8877 } else { 8878 /* 8879 * Device is powered up, verify the 8880 * count is non-negative. 8881 * This is debug only. 8882 */ 8883 ASSERT(un->un_pm_count == 0); 8884 } 8885 } 8886 8887 if (return_status == DDI_SUCCESS) { 8888 /* 8889 * For performance, now that the device has been tagged 8890 * as busy, and it's known to be powered up, update the 8891 * chain types to use jump tables that do not include 8892 * pm. This significantly lowers the overhead and 8893 * therefore improves performance. 8894 */ 8895 8896 mutex_exit(&un->un_pm_mutex); 8897 mutex_enter(SD_MUTEX(un)); 8898 SD_TRACE(SD_LOG_IO_PM, un, 8899 "sd_pm_entry: changing uscsi_chain_type from %d\n", 8900 un->un_uscsi_chain_type); 8901 8902 if (un->un_f_non_devbsize_supported) { 8903 un->un_buf_chain_type = 8904 SD_CHAIN_INFO_RMMEDIA_NO_PM; 8905 } else { 8906 un->un_buf_chain_type = 8907 SD_CHAIN_INFO_DISK_NO_PM; 8908 } 8909 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8910 8911 SD_TRACE(SD_LOG_IO_PM, un, 8912 " changed uscsi_chain_type to %d\n", 8913 un->un_uscsi_chain_type); 8914 mutex_exit(SD_MUTEX(un)); 8915 mutex_enter(&un->un_pm_mutex); 8916 8917 if (un->un_pm_idle_timeid == NULL) { 8918 /* 300 ms. */ 8919 un->un_pm_idle_timeid = 8920 timeout(sd_pm_idletimeout_handler, un, 8921 (drv_usectohz((clock_t)300000))); 8922 /* 8923 * Include an extra call to busy which keeps the 8924 * device busy with-respect-to the PM layer 8925 * until the timer fires, at which time it'll 8926 * get the extra idle call. 8927 */ 8928 (void) pm_busy_component(SD_DEVINFO(un), 0); 8929 } 8930 } 8931 } 8932 un->un_pm_busy = FALSE; 8933 /* Next... */ 8934 cv_signal(&un->un_pm_busy_cv); 8935 8936 un->un_pm_count++; 8937 8938 SD_TRACE(SD_LOG_IO_PM, un, 8939 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 8940 8941 mutex_exit(&un->un_pm_mutex); 8942 8943 return (return_status); 8944 } 8945 8946 8947 /* 8948 * Function: sd_pm_exit 8949 * 8950 * Description: Called at the completion of a command to manage busy 8951 * status for the device. If the device becomes idle the 8952 * PM framework is notified. 8953 * 8954 * Context: Kernel thread context 8955 */ 8956 8957 static void 8958 sd_pm_exit(struct sd_lun *un) 8959 { 8960 ASSERT(!mutex_owned(SD_MUTEX(un))); 8961 ASSERT(!mutex_owned(&un->un_pm_mutex)); 8962 8963 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 8964 8965 /* 8966 * After attach the following flag is only read, so don't 8967 * take the penalty of acquiring a mutex for it. 8968 */ 8969 if (un->un_f_pm_is_enabled == TRUE) { 8970 8971 mutex_enter(&un->un_pm_mutex); 8972 un->un_pm_count--; 8973 8974 SD_TRACE(SD_LOG_IO_PM, un, 8975 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 8976 8977 ASSERT(un->un_pm_count >= 0); 8978 if (un->un_pm_count == 0) { 8979 mutex_exit(&un->un_pm_mutex); 8980 8981 SD_TRACE(SD_LOG_IO_PM, un, 8982 "sd_pm_exit: idle component\n"); 8983 8984 (void) pm_idle_component(SD_DEVINFO(un), 0); 8985 8986 } else { 8987 mutex_exit(&un->un_pm_mutex); 8988 } 8989 } 8990 8991 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 8992 } 8993 8994 8995 /* 8996 * Function: sdopen 8997 * 8998 * Description: Driver's open(9e) entry point function. 8999 * 9000 * Arguments: dev_i - pointer to device number 9001 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 9002 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9003 * cred_p - user credential pointer 9004 * 9005 * Return Code: EINVAL 9006 * ENXIO 9007 * EIO 9008 * EROFS 9009 * EBUSY 9010 * 9011 * Context: Kernel thread context 9012 */ 9013 /* ARGSUSED */ 9014 static int 9015 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 9016 { 9017 struct sd_lun *un; 9018 int nodelay; 9019 int part; 9020 uint64_t partmask; 9021 int instance; 9022 dev_t dev; 9023 int rval = EIO; 9024 diskaddr_t nblks = 0; 9025 9026 /* Validate the open type */ 9027 if (otyp >= OTYPCNT) { 9028 return (EINVAL); 9029 } 9030 9031 dev = *dev_p; 9032 instance = SDUNIT(dev); 9033 mutex_enter(&sd_detach_mutex); 9034 9035 /* 9036 * Fail the open if there is no softstate for the instance, or 9037 * if another thread somewhere is trying to detach the instance. 9038 */ 9039 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 9040 (un->un_detach_count != 0)) { 9041 mutex_exit(&sd_detach_mutex); 9042 /* 9043 * The probe cache only needs to be cleared when open (9e) fails 9044 * with ENXIO (4238046). 9045 */ 9046 /* 9047 * un-conditionally clearing probe cache is ok with 9048 * separate sd/ssd binaries 9049 * x86 platform can be an issue with both parallel 9050 * and fibre in 1 binary 9051 */ 9052 sd_scsi_clear_probe_cache(); 9053 return (ENXIO); 9054 } 9055 9056 /* 9057 * The un_layer_count is to prevent another thread in specfs from 9058 * trying to detach the instance, which can happen when we are 9059 * called from a higher-layer driver instead of thru specfs. 9060 * This will not be needed when DDI provides a layered driver 9061 * interface that allows specfs to know that an instance is in 9062 * use by a layered driver & should not be detached. 9063 * 9064 * Note: the semantics for layered driver opens are exactly one 9065 * close for every open. 9066 */ 9067 if (otyp == OTYP_LYR) { 9068 un->un_layer_count++; 9069 } 9070 9071 /* 9072 * Keep a count of the current # of opens in progress. This is because 9073 * some layered drivers try to call us as a regular open. This can 9074 * cause problems that we cannot prevent, however by keeping this count 9075 * we can at least keep our open and detach routines from racing against 9076 * each other under such conditions. 9077 */ 9078 un->un_opens_in_progress++; 9079 mutex_exit(&sd_detach_mutex); 9080 9081 nodelay = (flag & (FNDELAY | FNONBLOCK)); 9082 part = SDPART(dev); 9083 partmask = 1 << part; 9084 9085 /* 9086 * We use a semaphore here in order to serialize 9087 * open and close requests on the device. 9088 */ 9089 sema_p(&un->un_semoclose); 9090 9091 mutex_enter(SD_MUTEX(un)); 9092 9093 /* 9094 * All device accesses go thru sdstrategy() where we check 9095 * on suspend status but there could be a scsi_poll command, 9096 * which bypasses sdstrategy(), so we need to check pm 9097 * status. 9098 */ 9099 9100 if (!nodelay) { 9101 while ((un->un_state == SD_STATE_SUSPENDED) || 9102 (un->un_state == SD_STATE_PM_CHANGING)) { 9103 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9104 } 9105 9106 mutex_exit(SD_MUTEX(un)); 9107 if (sd_pm_entry(un) != DDI_SUCCESS) { 9108 rval = EIO; 9109 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 9110 "sdopen: sd_pm_entry failed\n"); 9111 goto open_failed_with_pm; 9112 } 9113 mutex_enter(SD_MUTEX(un)); 9114 } 9115 9116 /* check for previous exclusive open */ 9117 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 9118 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9119 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 9120 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 9121 9122 if (un->un_exclopen & (partmask)) { 9123 goto excl_open_fail; 9124 } 9125 9126 if (flag & FEXCL) { 9127 int i; 9128 if (un->un_ocmap.lyropen[part]) { 9129 goto excl_open_fail; 9130 } 9131 for (i = 0; i < (OTYPCNT - 1); i++) { 9132 if (un->un_ocmap.regopen[i] & (partmask)) { 9133 goto excl_open_fail; 9134 } 9135 } 9136 } 9137 9138 /* 9139 * Check the write permission if this is a removable media device, 9140 * NDELAY has not been set, and writable permission is requested. 9141 * 9142 * Note: If NDELAY was set and this is write-protected media the WRITE 9143 * attempt will fail with EIO as part of the I/O processing. This is a 9144 * more permissive implementation that allows the open to succeed and 9145 * WRITE attempts to fail when appropriate. 9146 */ 9147 if (un->un_f_chk_wp_open) { 9148 if ((flag & FWRITE) && (!nodelay)) { 9149 mutex_exit(SD_MUTEX(un)); 9150 /* 9151 * Defer the check for write permission on writable 9152 * DVD drive till sdstrategy and will not fail open even 9153 * if FWRITE is set as the device can be writable 9154 * depending upon the media and the media can change 9155 * after the call to open(). 9156 */ 9157 if (un->un_f_dvdram_writable_device == FALSE) { 9158 if (ISCD(un) || sr_check_wp(dev)) { 9159 rval = EROFS; 9160 mutex_enter(SD_MUTEX(un)); 9161 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9162 "write to cd or write protected media\n"); 9163 goto open_fail; 9164 } 9165 } 9166 mutex_enter(SD_MUTEX(un)); 9167 } 9168 } 9169 9170 /* 9171 * If opening in NDELAY/NONBLOCK mode, just return. 9172 * Check if disk is ready and has a valid geometry later. 9173 */ 9174 if (!nodelay) { 9175 mutex_exit(SD_MUTEX(un)); 9176 rval = sd_ready_and_valid(un); 9177 mutex_enter(SD_MUTEX(un)); 9178 /* 9179 * Fail if device is not ready or if the number of disk 9180 * blocks is zero or negative for non CD devices. 9181 */ 9182 9183 nblks = 0; 9184 9185 if (rval == SD_READY_VALID && (!ISCD(un))) { 9186 /* if cmlb_partinfo fails, nblks remains 0 */ 9187 mutex_exit(SD_MUTEX(un)); 9188 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks, 9189 NULL, NULL, NULL, (void *)SD_PATH_DIRECT); 9190 mutex_enter(SD_MUTEX(un)); 9191 } 9192 9193 if ((rval != SD_READY_VALID) || 9194 (!ISCD(un) && nblks <= 0)) { 9195 rval = un->un_f_has_removable_media ? ENXIO : EIO; 9196 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9197 "device not ready or invalid disk block value\n"); 9198 goto open_fail; 9199 } 9200 #if defined(__i386) || defined(__amd64) 9201 } else { 9202 uchar_t *cp; 9203 /* 9204 * x86 requires special nodelay handling, so that p0 is 9205 * always defined and accessible. 9206 * Invalidate geometry only if device is not already open. 9207 */ 9208 cp = &un->un_ocmap.chkd[0]; 9209 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9210 if (*cp != (uchar_t)0) { 9211 break; 9212 } 9213 cp++; 9214 } 9215 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9216 mutex_exit(SD_MUTEX(un)); 9217 cmlb_invalidate(un->un_cmlbhandle, 9218 (void *)SD_PATH_DIRECT); 9219 mutex_enter(SD_MUTEX(un)); 9220 } 9221 9222 #endif 9223 } 9224 9225 if (otyp == OTYP_LYR) { 9226 un->un_ocmap.lyropen[part]++; 9227 } else { 9228 un->un_ocmap.regopen[otyp] |= partmask; 9229 } 9230 9231 /* Set up open and exclusive open flags */ 9232 if (flag & FEXCL) { 9233 un->un_exclopen |= (partmask); 9234 } 9235 9236 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 9237 "open of part %d type %d\n", part, otyp); 9238 9239 mutex_exit(SD_MUTEX(un)); 9240 if (!nodelay) { 9241 sd_pm_exit(un); 9242 } 9243 9244 sema_v(&un->un_semoclose); 9245 9246 mutex_enter(&sd_detach_mutex); 9247 un->un_opens_in_progress--; 9248 mutex_exit(&sd_detach_mutex); 9249 9250 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 9251 return (DDI_SUCCESS); 9252 9253 excl_open_fail: 9254 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 9255 rval = EBUSY; 9256 9257 open_fail: 9258 mutex_exit(SD_MUTEX(un)); 9259 9260 /* 9261 * On a failed open we must exit the pm management. 9262 */ 9263 if (!nodelay) { 9264 sd_pm_exit(un); 9265 } 9266 open_failed_with_pm: 9267 sema_v(&un->un_semoclose); 9268 9269 mutex_enter(&sd_detach_mutex); 9270 un->un_opens_in_progress--; 9271 if (otyp == OTYP_LYR) { 9272 un->un_layer_count--; 9273 } 9274 mutex_exit(&sd_detach_mutex); 9275 9276 return (rval); 9277 } 9278 9279 9280 /* 9281 * Function: sdclose 9282 * 9283 * Description: Driver's close(9e) entry point function. 9284 * 9285 * Arguments: dev - device number 9286 * flag - file status flag, informational only 9287 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 9288 * cred_p - user credential pointer 9289 * 9290 * Return Code: ENXIO 9291 * 9292 * Context: Kernel thread context 9293 */ 9294 /* ARGSUSED */ 9295 static int 9296 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 9297 { 9298 struct sd_lun *un; 9299 uchar_t *cp; 9300 int part; 9301 int nodelay; 9302 int rval = 0; 9303 9304 /* Validate the open type */ 9305 if (otyp >= OTYPCNT) { 9306 return (ENXIO); 9307 } 9308 9309 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9310 return (ENXIO); 9311 } 9312 9313 part = SDPART(dev); 9314 nodelay = flag & (FNDELAY | FNONBLOCK); 9315 9316 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 9317 "sdclose: close of part %d type %d\n", part, otyp); 9318 9319 /* 9320 * We use a semaphore here in order to serialize 9321 * open and close requests on the device. 9322 */ 9323 sema_p(&un->un_semoclose); 9324 9325 mutex_enter(SD_MUTEX(un)); 9326 9327 /* Don't proceed if power is being changed. */ 9328 while (un->un_state == SD_STATE_PM_CHANGING) { 9329 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9330 } 9331 9332 if (un->un_exclopen & (1 << part)) { 9333 un->un_exclopen &= ~(1 << part); 9334 } 9335 9336 /* Update the open partition map */ 9337 if (otyp == OTYP_LYR) { 9338 un->un_ocmap.lyropen[part] -= 1; 9339 } else { 9340 un->un_ocmap.regopen[otyp] &= ~(1 << part); 9341 } 9342 9343 cp = &un->un_ocmap.chkd[0]; 9344 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 9345 if (*cp != NULL) { 9346 break; 9347 } 9348 cp++; 9349 } 9350 9351 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 9352 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 9353 9354 /* 9355 * We avoid persistance upon the last close, and set 9356 * the throttle back to the maximum. 9357 */ 9358 un->un_throttle = un->un_saved_throttle; 9359 9360 if (un->un_state == SD_STATE_OFFLINE) { 9361 if (un->un_f_is_fibre == FALSE) { 9362 scsi_log(SD_DEVINFO(un), sd_label, 9363 CE_WARN, "offline\n"); 9364 } 9365 mutex_exit(SD_MUTEX(un)); 9366 cmlb_invalidate(un->un_cmlbhandle, 9367 (void *)SD_PATH_DIRECT); 9368 mutex_enter(SD_MUTEX(un)); 9369 9370 } else { 9371 /* 9372 * Flush any outstanding writes in NVRAM cache. 9373 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 9374 * cmd, it may not work for non-Pluto devices. 9375 * SYNCHRONIZE CACHE is not required for removables, 9376 * except DVD-RAM drives. 9377 * 9378 * Also note: because SYNCHRONIZE CACHE is currently 9379 * the only command issued here that requires the 9380 * drive be powered up, only do the power up before 9381 * sending the Sync Cache command. If additional 9382 * commands are added which require a powered up 9383 * drive, the following sequence may have to change. 9384 * 9385 * And finally, note that parallel SCSI on SPARC 9386 * only issues a Sync Cache to DVD-RAM, a newly 9387 * supported device. 9388 */ 9389 #if defined(__i386) || defined(__amd64) 9390 if (un->un_f_sync_cache_supported || 9391 un->un_f_dvdram_writable_device == TRUE) { 9392 #else 9393 if (un->un_f_dvdram_writable_device == TRUE) { 9394 #endif 9395 mutex_exit(SD_MUTEX(un)); 9396 if (sd_pm_entry(un) == DDI_SUCCESS) { 9397 rval = 9398 sd_send_scsi_SYNCHRONIZE_CACHE(un, 9399 NULL); 9400 /* ignore error if not supported */ 9401 if (rval == ENOTSUP) { 9402 rval = 0; 9403 } else if (rval != 0) { 9404 rval = EIO; 9405 } 9406 sd_pm_exit(un); 9407 } else { 9408 rval = EIO; 9409 } 9410 mutex_enter(SD_MUTEX(un)); 9411 } 9412 9413 /* 9414 * For devices which supports DOOR_LOCK, send an ALLOW 9415 * MEDIA REMOVAL command, but don't get upset if it 9416 * fails. We need to raise the power of the drive before 9417 * we can call sd_send_scsi_DOORLOCK() 9418 */ 9419 if (un->un_f_doorlock_supported) { 9420 mutex_exit(SD_MUTEX(un)); 9421 if (sd_pm_entry(un) == DDI_SUCCESS) { 9422 rval = sd_send_scsi_DOORLOCK(un, 9423 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 9424 9425 sd_pm_exit(un); 9426 if (ISCD(un) && (rval != 0) && 9427 (nodelay != 0)) { 9428 rval = ENXIO; 9429 } 9430 } else { 9431 rval = EIO; 9432 } 9433 mutex_enter(SD_MUTEX(un)); 9434 } 9435 9436 /* 9437 * If a device has removable media, invalidate all 9438 * parameters related to media, such as geometry, 9439 * blocksize, and blockcount. 9440 */ 9441 if (un->un_f_has_removable_media) { 9442 sr_ejected(un); 9443 } 9444 9445 /* 9446 * Destroy the cache (if it exists) which was 9447 * allocated for the write maps since this is 9448 * the last close for this media. 9449 */ 9450 if (un->un_wm_cache) { 9451 /* 9452 * Check if there are pending commands. 9453 * and if there are give a warning and 9454 * do not destroy the cache. 9455 */ 9456 if (un->un_ncmds_in_driver > 0) { 9457 scsi_log(SD_DEVINFO(un), 9458 sd_label, CE_WARN, 9459 "Unable to clean up memory " 9460 "because of pending I/O\n"); 9461 } else { 9462 kmem_cache_destroy( 9463 un->un_wm_cache); 9464 un->un_wm_cache = NULL; 9465 } 9466 } 9467 } 9468 } 9469 9470 mutex_exit(SD_MUTEX(un)); 9471 sema_v(&un->un_semoclose); 9472 9473 if (otyp == OTYP_LYR) { 9474 mutex_enter(&sd_detach_mutex); 9475 /* 9476 * The detach routine may run when the layer count 9477 * drops to zero. 9478 */ 9479 un->un_layer_count--; 9480 mutex_exit(&sd_detach_mutex); 9481 } 9482 9483 return (rval); 9484 } 9485 9486 9487 /* 9488 * Function: sd_ready_and_valid 9489 * 9490 * Description: Test if device is ready and has a valid geometry. 9491 * 9492 * Arguments: dev - device number 9493 * un - driver soft state (unit) structure 9494 * 9495 * Return Code: SD_READY_VALID ready and valid label 9496 * SD_NOT_READY_VALID not ready, no label 9497 * SD_RESERVED_BY_OTHERS reservation conflict 9498 * 9499 * Context: Never called at interrupt context. 9500 */ 9501 9502 static int 9503 sd_ready_and_valid(struct sd_lun *un) 9504 { 9505 struct sd_errstats *stp; 9506 uint64_t capacity; 9507 uint_t lbasize; 9508 int rval = SD_READY_VALID; 9509 char name_str[48]; 9510 int is_valid; 9511 9512 ASSERT(un != NULL); 9513 ASSERT(!mutex_owned(SD_MUTEX(un))); 9514 9515 mutex_enter(SD_MUTEX(un)); 9516 /* 9517 * If a device has removable media, we must check if media is 9518 * ready when checking if this device is ready and valid. 9519 */ 9520 if (un->un_f_has_removable_media) { 9521 mutex_exit(SD_MUTEX(un)); 9522 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 9523 rval = SD_NOT_READY_VALID; 9524 mutex_enter(SD_MUTEX(un)); 9525 goto done; 9526 } 9527 9528 is_valid = SD_IS_VALID_LABEL(un); 9529 mutex_enter(SD_MUTEX(un)); 9530 if (!is_valid || 9531 (un->un_f_blockcount_is_valid == FALSE) || 9532 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 9533 9534 /* capacity has to be read every open. */ 9535 mutex_exit(SD_MUTEX(un)); 9536 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 9537 &lbasize, SD_PATH_DIRECT) != 0) { 9538 cmlb_invalidate(un->un_cmlbhandle, 9539 (void *)SD_PATH_DIRECT); 9540 mutex_enter(SD_MUTEX(un)); 9541 rval = SD_NOT_READY_VALID; 9542 goto done; 9543 } else { 9544 mutex_enter(SD_MUTEX(un)); 9545 sd_update_block_info(un, lbasize, capacity); 9546 } 9547 } 9548 9549 /* 9550 * Check if the media in the device is writable or not. 9551 */ 9552 if (!is_valid && ISCD(un)) { 9553 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 9554 } 9555 9556 } else { 9557 /* 9558 * Do a test unit ready to clear any unit attention from non-cd 9559 * devices. 9560 */ 9561 mutex_exit(SD_MUTEX(un)); 9562 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 9563 mutex_enter(SD_MUTEX(un)); 9564 } 9565 9566 9567 /* 9568 * If this is a non 512 block device, allocate space for 9569 * the wmap cache. This is being done here since every time 9570 * a media is changed this routine will be called and the 9571 * block size is a function of media rather than device. 9572 */ 9573 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 9574 if (!(un->un_wm_cache)) { 9575 (void) snprintf(name_str, sizeof (name_str), 9576 "%s%d_cache", 9577 ddi_driver_name(SD_DEVINFO(un)), 9578 ddi_get_instance(SD_DEVINFO(un))); 9579 un->un_wm_cache = kmem_cache_create( 9580 name_str, sizeof (struct sd_w_map), 9581 8, sd_wm_cache_constructor, 9582 sd_wm_cache_destructor, NULL, 9583 (void *)un, NULL, 0); 9584 if (!(un->un_wm_cache)) { 9585 rval = ENOMEM; 9586 goto done; 9587 } 9588 } 9589 } 9590 9591 if (un->un_state == SD_STATE_NORMAL) { 9592 /* 9593 * If the target is not yet ready here (defined by a TUR 9594 * failure), invalidate the geometry and print an 'offline' 9595 * message. This is a legacy message, as the state of the 9596 * target is not actually changed to SD_STATE_OFFLINE. 9597 * 9598 * If the TUR fails for EACCES (Reservation Conflict), 9599 * SD_RESERVED_BY_OTHERS will be returned to indicate 9600 * reservation conflict. If the TUR fails for other 9601 * reasons, SD_NOT_READY_VALID will be returned. 9602 */ 9603 int err; 9604 9605 mutex_exit(SD_MUTEX(un)); 9606 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 9607 mutex_enter(SD_MUTEX(un)); 9608 9609 if (err != 0) { 9610 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 9611 "offline or reservation conflict\n"); 9612 mutex_exit(SD_MUTEX(un)); 9613 cmlb_invalidate(un->un_cmlbhandle, 9614 (void *)SD_PATH_DIRECT); 9615 mutex_enter(SD_MUTEX(un)); 9616 if (err == EACCES) { 9617 rval = SD_RESERVED_BY_OTHERS; 9618 } else { 9619 rval = SD_NOT_READY_VALID; 9620 } 9621 goto done; 9622 } 9623 } 9624 9625 if (un->un_f_format_in_progress == FALSE) { 9626 mutex_exit(SD_MUTEX(un)); 9627 if (cmlb_validate(un->un_cmlbhandle, 0, 9628 (void *)SD_PATH_DIRECT) != 0) { 9629 rval = SD_NOT_READY_VALID; 9630 mutex_enter(SD_MUTEX(un)); 9631 goto done; 9632 } 9633 if (un->un_f_pkstats_enabled) { 9634 sd_set_pstats(un); 9635 SD_TRACE(SD_LOG_IO_PARTITION, un, 9636 "sd_ready_and_valid: un:0x%p pstats created and " 9637 "set\n", un); 9638 } 9639 mutex_enter(SD_MUTEX(un)); 9640 } 9641 9642 /* 9643 * If this device supports DOOR_LOCK command, try and send 9644 * this command to PREVENT MEDIA REMOVAL, but don't get upset 9645 * if it fails. For a CD, however, it is an error 9646 */ 9647 if (un->un_f_doorlock_supported) { 9648 mutex_exit(SD_MUTEX(un)); 9649 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 9650 SD_PATH_DIRECT) != 0) && ISCD(un)) { 9651 rval = SD_NOT_READY_VALID; 9652 mutex_enter(SD_MUTEX(un)); 9653 goto done; 9654 } 9655 mutex_enter(SD_MUTEX(un)); 9656 } 9657 9658 /* The state has changed, inform the media watch routines */ 9659 un->un_mediastate = DKIO_INSERTED; 9660 cv_broadcast(&un->un_state_cv); 9661 rval = SD_READY_VALID; 9662 9663 done: 9664 9665 /* 9666 * Initialize the capacity kstat value, if no media previously 9667 * (capacity kstat is 0) and a media has been inserted 9668 * (un_blockcount > 0). 9669 */ 9670 if (un->un_errstats != NULL) { 9671 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9672 if ((stp->sd_capacity.value.ui64 == 0) && 9673 (un->un_f_blockcount_is_valid == TRUE)) { 9674 stp->sd_capacity.value.ui64 = 9675 (uint64_t)((uint64_t)un->un_blockcount * 9676 un->un_sys_blocksize); 9677 } 9678 } 9679 9680 mutex_exit(SD_MUTEX(un)); 9681 return (rval); 9682 } 9683 9684 9685 /* 9686 * Function: sdmin 9687 * 9688 * Description: Routine to limit the size of a data transfer. Used in 9689 * conjunction with physio(9F). 9690 * 9691 * Arguments: bp - pointer to the indicated buf(9S) struct. 9692 * 9693 * Context: Kernel thread context. 9694 */ 9695 9696 static void 9697 sdmin(struct buf *bp) 9698 { 9699 struct sd_lun *un; 9700 int instance; 9701 9702 instance = SDUNIT(bp->b_edev); 9703 9704 un = ddi_get_soft_state(sd_state, instance); 9705 ASSERT(un != NULL); 9706 9707 if (bp->b_bcount > un->un_max_xfer_size) { 9708 bp->b_bcount = un->un_max_xfer_size; 9709 } 9710 } 9711 9712 9713 /* 9714 * Function: sdread 9715 * 9716 * Description: Driver's read(9e) entry point function. 9717 * 9718 * Arguments: dev - device number 9719 * uio - structure pointer describing where data is to be stored 9720 * in user's space 9721 * cred_p - user credential pointer 9722 * 9723 * Return Code: ENXIO 9724 * EIO 9725 * EINVAL 9726 * value returned by physio 9727 * 9728 * Context: Kernel thread context. 9729 */ 9730 /* ARGSUSED */ 9731 static int 9732 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 9733 { 9734 struct sd_lun *un = NULL; 9735 int secmask; 9736 int err; 9737 9738 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9739 return (ENXIO); 9740 } 9741 9742 ASSERT(!mutex_owned(SD_MUTEX(un))); 9743 9744 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9745 mutex_enter(SD_MUTEX(un)); 9746 /* 9747 * Because the call to sd_ready_and_valid will issue I/O we 9748 * must wait here if either the device is suspended or 9749 * if it's power level is changing. 9750 */ 9751 while ((un->un_state == SD_STATE_SUSPENDED) || 9752 (un->un_state == SD_STATE_PM_CHANGING)) { 9753 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9754 } 9755 un->un_ncmds_in_driver++; 9756 mutex_exit(SD_MUTEX(un)); 9757 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9758 mutex_enter(SD_MUTEX(un)); 9759 un->un_ncmds_in_driver--; 9760 ASSERT(un->un_ncmds_in_driver >= 0); 9761 mutex_exit(SD_MUTEX(un)); 9762 return (EIO); 9763 } 9764 mutex_enter(SD_MUTEX(un)); 9765 un->un_ncmds_in_driver--; 9766 ASSERT(un->un_ncmds_in_driver >= 0); 9767 mutex_exit(SD_MUTEX(un)); 9768 } 9769 9770 /* 9771 * Read requests are restricted to multiples of the system block size. 9772 */ 9773 secmask = un->un_sys_blocksize - 1; 9774 9775 if (uio->uio_loffset & ((offset_t)(secmask))) { 9776 SD_ERROR(SD_LOG_READ_WRITE, un, 9777 "sdread: file offset not modulo %d\n", 9778 un->un_sys_blocksize); 9779 err = EINVAL; 9780 } else if (uio->uio_iov->iov_len & (secmask)) { 9781 SD_ERROR(SD_LOG_READ_WRITE, un, 9782 "sdread: transfer length not modulo %d\n", 9783 un->un_sys_blocksize); 9784 err = EINVAL; 9785 } else { 9786 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 9787 } 9788 return (err); 9789 } 9790 9791 9792 /* 9793 * Function: sdwrite 9794 * 9795 * Description: Driver's write(9e) entry point function. 9796 * 9797 * Arguments: dev - device number 9798 * uio - structure pointer describing where data is stored in 9799 * user's space 9800 * cred_p - user credential pointer 9801 * 9802 * Return Code: ENXIO 9803 * EIO 9804 * EINVAL 9805 * value returned by physio 9806 * 9807 * Context: Kernel thread context. 9808 */ 9809 /* ARGSUSED */ 9810 static int 9811 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 9812 { 9813 struct sd_lun *un = NULL; 9814 int secmask; 9815 int err; 9816 9817 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9818 return (ENXIO); 9819 } 9820 9821 ASSERT(!mutex_owned(SD_MUTEX(un))); 9822 9823 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9824 mutex_enter(SD_MUTEX(un)); 9825 /* 9826 * Because the call to sd_ready_and_valid will issue I/O we 9827 * must wait here if either the device is suspended or 9828 * if it's power level is changing. 9829 */ 9830 while ((un->un_state == SD_STATE_SUSPENDED) || 9831 (un->un_state == SD_STATE_PM_CHANGING)) { 9832 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9833 } 9834 un->un_ncmds_in_driver++; 9835 mutex_exit(SD_MUTEX(un)); 9836 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9837 mutex_enter(SD_MUTEX(un)); 9838 un->un_ncmds_in_driver--; 9839 ASSERT(un->un_ncmds_in_driver >= 0); 9840 mutex_exit(SD_MUTEX(un)); 9841 return (EIO); 9842 } 9843 mutex_enter(SD_MUTEX(un)); 9844 un->un_ncmds_in_driver--; 9845 ASSERT(un->un_ncmds_in_driver >= 0); 9846 mutex_exit(SD_MUTEX(un)); 9847 } 9848 9849 /* 9850 * Write requests are restricted to multiples of the system block size. 9851 */ 9852 secmask = un->un_sys_blocksize - 1; 9853 9854 if (uio->uio_loffset & ((offset_t)(secmask))) { 9855 SD_ERROR(SD_LOG_READ_WRITE, un, 9856 "sdwrite: file offset not modulo %d\n", 9857 un->un_sys_blocksize); 9858 err = EINVAL; 9859 } else if (uio->uio_iov->iov_len & (secmask)) { 9860 SD_ERROR(SD_LOG_READ_WRITE, un, 9861 "sdwrite: transfer length not modulo %d\n", 9862 un->un_sys_blocksize); 9863 err = EINVAL; 9864 } else { 9865 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 9866 } 9867 return (err); 9868 } 9869 9870 9871 /* 9872 * Function: sdaread 9873 * 9874 * Description: Driver's aread(9e) entry point function. 9875 * 9876 * Arguments: dev - device number 9877 * aio - structure pointer describing where data is to be stored 9878 * cred_p - user credential pointer 9879 * 9880 * Return Code: ENXIO 9881 * EIO 9882 * EINVAL 9883 * value returned by aphysio 9884 * 9885 * Context: Kernel thread context. 9886 */ 9887 /* ARGSUSED */ 9888 static int 9889 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 9890 { 9891 struct sd_lun *un = NULL; 9892 struct uio *uio = aio->aio_uio; 9893 int secmask; 9894 int err; 9895 9896 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9897 return (ENXIO); 9898 } 9899 9900 ASSERT(!mutex_owned(SD_MUTEX(un))); 9901 9902 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9903 mutex_enter(SD_MUTEX(un)); 9904 /* 9905 * Because the call to sd_ready_and_valid will issue I/O we 9906 * must wait here if either the device is suspended or 9907 * if it's power level is changing. 9908 */ 9909 while ((un->un_state == SD_STATE_SUSPENDED) || 9910 (un->un_state == SD_STATE_PM_CHANGING)) { 9911 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9912 } 9913 un->un_ncmds_in_driver++; 9914 mutex_exit(SD_MUTEX(un)); 9915 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9916 mutex_enter(SD_MUTEX(un)); 9917 un->un_ncmds_in_driver--; 9918 ASSERT(un->un_ncmds_in_driver >= 0); 9919 mutex_exit(SD_MUTEX(un)); 9920 return (EIO); 9921 } 9922 mutex_enter(SD_MUTEX(un)); 9923 un->un_ncmds_in_driver--; 9924 ASSERT(un->un_ncmds_in_driver >= 0); 9925 mutex_exit(SD_MUTEX(un)); 9926 } 9927 9928 /* 9929 * Read requests are restricted to multiples of the system block size. 9930 */ 9931 secmask = un->un_sys_blocksize - 1; 9932 9933 if (uio->uio_loffset & ((offset_t)(secmask))) { 9934 SD_ERROR(SD_LOG_READ_WRITE, un, 9935 "sdaread: file offset not modulo %d\n", 9936 un->un_sys_blocksize); 9937 err = EINVAL; 9938 } else if (uio->uio_iov->iov_len & (secmask)) { 9939 SD_ERROR(SD_LOG_READ_WRITE, un, 9940 "sdaread: transfer length not modulo %d\n", 9941 un->un_sys_blocksize); 9942 err = EINVAL; 9943 } else { 9944 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 9945 } 9946 return (err); 9947 } 9948 9949 9950 /* 9951 * Function: sdawrite 9952 * 9953 * Description: Driver's awrite(9e) entry point function. 9954 * 9955 * Arguments: dev - device number 9956 * aio - structure pointer describing where data is stored 9957 * cred_p - user credential pointer 9958 * 9959 * Return Code: ENXIO 9960 * EIO 9961 * EINVAL 9962 * value returned by aphysio 9963 * 9964 * Context: Kernel thread context. 9965 */ 9966 /* ARGSUSED */ 9967 static int 9968 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 9969 { 9970 struct sd_lun *un = NULL; 9971 struct uio *uio = aio->aio_uio; 9972 int secmask; 9973 int err; 9974 9975 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 9976 return (ENXIO); 9977 } 9978 9979 ASSERT(!mutex_owned(SD_MUTEX(un))); 9980 9981 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) { 9982 mutex_enter(SD_MUTEX(un)); 9983 /* 9984 * Because the call to sd_ready_and_valid will issue I/O we 9985 * must wait here if either the device is suspended or 9986 * if it's power level is changing. 9987 */ 9988 while ((un->un_state == SD_STATE_SUSPENDED) || 9989 (un->un_state == SD_STATE_PM_CHANGING)) { 9990 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 9991 } 9992 un->un_ncmds_in_driver++; 9993 mutex_exit(SD_MUTEX(un)); 9994 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 9995 mutex_enter(SD_MUTEX(un)); 9996 un->un_ncmds_in_driver--; 9997 ASSERT(un->un_ncmds_in_driver >= 0); 9998 mutex_exit(SD_MUTEX(un)); 9999 return (EIO); 10000 } 10001 mutex_enter(SD_MUTEX(un)); 10002 un->un_ncmds_in_driver--; 10003 ASSERT(un->un_ncmds_in_driver >= 0); 10004 mutex_exit(SD_MUTEX(un)); 10005 } 10006 10007 /* 10008 * Write requests are restricted to multiples of the system block size. 10009 */ 10010 secmask = un->un_sys_blocksize - 1; 10011 10012 if (uio->uio_loffset & ((offset_t)(secmask))) { 10013 SD_ERROR(SD_LOG_READ_WRITE, un, 10014 "sdawrite: file offset not modulo %d\n", 10015 un->un_sys_blocksize); 10016 err = EINVAL; 10017 } else if (uio->uio_iov->iov_len & (secmask)) { 10018 SD_ERROR(SD_LOG_READ_WRITE, un, 10019 "sdawrite: transfer length not modulo %d\n", 10020 un->un_sys_blocksize); 10021 err = EINVAL; 10022 } else { 10023 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 10024 } 10025 return (err); 10026 } 10027 10028 10029 10030 10031 10032 /* 10033 * Driver IO processing follows the following sequence: 10034 * 10035 * sdioctl(9E) sdstrategy(9E) biodone(9F) 10036 * | | ^ 10037 * v v | 10038 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 10039 * | | | | 10040 * v | | | 10041 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 10042 * | | ^ ^ 10043 * v v | | 10044 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 10045 * | | | | 10046 * +---+ | +------------+ +-------+ 10047 * | | | | 10048 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10049 * | v | | 10050 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 10051 * | | ^ | 10052 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10053 * | v | | 10054 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 10055 * | | ^ | 10056 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 10057 * | v | | 10058 * | sd_checksum_iostart() sd_checksum_iodone() | 10059 * | | ^ | 10060 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 10061 * | v | | 10062 * | sd_pm_iostart() sd_pm_iodone() | 10063 * | | ^ | 10064 * | | | | 10065 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 10066 * | ^ 10067 * v | 10068 * sd_core_iostart() | 10069 * | | 10070 * | +------>(*destroypkt)() 10071 * +-> sd_start_cmds() <-+ | | 10072 * | | | v 10073 * | | | scsi_destroy_pkt(9F) 10074 * | | | 10075 * +->(*initpkt)() +- sdintr() 10076 * | | | | 10077 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 10078 * | +-> scsi_setup_cdb(9F) | 10079 * | | 10080 * +--> scsi_transport(9F) | 10081 * | | 10082 * +----> SCSA ---->+ 10083 * 10084 * 10085 * This code is based upon the following presumptions: 10086 * 10087 * - iostart and iodone functions operate on buf(9S) structures. These 10088 * functions perform the necessary operations on the buf(9S) and pass 10089 * them along to the next function in the chain by using the macros 10090 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 10091 * (for iodone side functions). 10092 * 10093 * - The iostart side functions may sleep. The iodone side functions 10094 * are called under interrupt context and may NOT sleep. Therefore 10095 * iodone side functions also may not call iostart side functions. 10096 * (NOTE: iostart side functions should NOT sleep for memory, as 10097 * this could result in deadlock.) 10098 * 10099 * - An iostart side function may call its corresponding iodone side 10100 * function directly (if necessary). 10101 * 10102 * - In the event of an error, an iostart side function can return a buf(9S) 10103 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 10104 * b_error in the usual way of course). 10105 * 10106 * - The taskq mechanism may be used by the iodone side functions to dispatch 10107 * requests to the iostart side functions. The iostart side functions in 10108 * this case would be called under the context of a taskq thread, so it's 10109 * OK for them to block/sleep/spin in this case. 10110 * 10111 * - iostart side functions may allocate "shadow" buf(9S) structs and 10112 * pass them along to the next function in the chain. The corresponding 10113 * iodone side functions must coalesce the "shadow" bufs and return 10114 * the "original" buf to the next higher layer. 10115 * 10116 * - The b_private field of the buf(9S) struct holds a pointer to 10117 * an sd_xbuf struct, which contains information needed to 10118 * construct the scsi_pkt for the command. 10119 * 10120 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 10121 * layer must acquire & release the SD_MUTEX(un) as needed. 10122 */ 10123 10124 10125 /* 10126 * Create taskq for all targets in the system. This is created at 10127 * _init(9E) and destroyed at _fini(9E). 10128 * 10129 * Note: here we set the minalloc to a reasonably high number to ensure that 10130 * we will have an adequate supply of task entries available at interrupt time. 10131 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 10132 * sd_create_taskq(). Since we do not want to sleep for allocations at 10133 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 10134 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 10135 * requests any one instant in time. 10136 */ 10137 #define SD_TASKQ_NUMTHREADS 8 10138 #define SD_TASKQ_MINALLOC 256 10139 #define SD_TASKQ_MAXALLOC 256 10140 10141 static taskq_t *sd_tq = NULL; 10142 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 10143 10144 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 10145 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 10146 10147 /* 10148 * The following task queue is being created for the write part of 10149 * read-modify-write of non-512 block size devices. 10150 * Limit the number of threads to 1 for now. This number has been chosen 10151 * considering the fact that it applies only to dvd ram drives/MO drives 10152 * currently. Performance for which is not main criteria at this stage. 10153 * Note: It needs to be explored if we can use a single taskq in future 10154 */ 10155 #define SD_WMR_TASKQ_NUMTHREADS 1 10156 static taskq_t *sd_wmr_tq = NULL; 10157 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 10158 10159 /* 10160 * Function: sd_taskq_create 10161 * 10162 * Description: Create taskq thread(s) and preallocate task entries 10163 * 10164 * Return Code: Returns a pointer to the allocated taskq_t. 10165 * 10166 * Context: Can sleep. Requires blockable context. 10167 * 10168 * Notes: - The taskq() facility currently is NOT part of the DDI. 10169 * (definitely NOT recommeded for 3rd-party drivers!) :-) 10170 * - taskq_create() will block for memory, also it will panic 10171 * if it cannot create the requested number of threads. 10172 * - Currently taskq_create() creates threads that cannot be 10173 * swapped. 10174 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 10175 * supply of taskq entries at interrupt time (ie, so that we 10176 * do not have to sleep for memory) 10177 */ 10178 10179 static void 10180 sd_taskq_create(void) 10181 { 10182 char taskq_name[TASKQ_NAMELEN]; 10183 10184 ASSERT(sd_tq == NULL); 10185 ASSERT(sd_wmr_tq == NULL); 10186 10187 (void) snprintf(taskq_name, sizeof (taskq_name), 10188 "%s_drv_taskq", sd_label); 10189 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 10190 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10191 TASKQ_PREPOPULATE)); 10192 10193 (void) snprintf(taskq_name, sizeof (taskq_name), 10194 "%s_rmw_taskq", sd_label); 10195 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 10196 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 10197 TASKQ_PREPOPULATE)); 10198 } 10199 10200 10201 /* 10202 * Function: sd_taskq_delete 10203 * 10204 * Description: Complementary cleanup routine for sd_taskq_create(). 10205 * 10206 * Context: Kernel thread context. 10207 */ 10208 10209 static void 10210 sd_taskq_delete(void) 10211 { 10212 ASSERT(sd_tq != NULL); 10213 ASSERT(sd_wmr_tq != NULL); 10214 taskq_destroy(sd_tq); 10215 taskq_destroy(sd_wmr_tq); 10216 sd_tq = NULL; 10217 sd_wmr_tq = NULL; 10218 } 10219 10220 10221 /* 10222 * Function: sdstrategy 10223 * 10224 * Description: Driver's strategy (9E) entry point function. 10225 * 10226 * Arguments: bp - pointer to buf(9S) 10227 * 10228 * Return Code: Always returns zero 10229 * 10230 * Context: Kernel thread context. 10231 */ 10232 10233 static int 10234 sdstrategy(struct buf *bp) 10235 { 10236 struct sd_lun *un; 10237 10238 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10239 if (un == NULL) { 10240 bioerror(bp, EIO); 10241 bp->b_resid = bp->b_bcount; 10242 biodone(bp); 10243 return (0); 10244 } 10245 /* As was done in the past, fail new cmds. if state is dumping. */ 10246 if (un->un_state == SD_STATE_DUMPING) { 10247 bioerror(bp, ENXIO); 10248 bp->b_resid = bp->b_bcount; 10249 biodone(bp); 10250 return (0); 10251 } 10252 10253 ASSERT(!mutex_owned(SD_MUTEX(un))); 10254 10255 /* 10256 * Commands may sneak in while we released the mutex in 10257 * DDI_SUSPEND, we should block new commands. However, old 10258 * commands that are still in the driver at this point should 10259 * still be allowed to drain. 10260 */ 10261 mutex_enter(SD_MUTEX(un)); 10262 /* 10263 * Must wait here if either the device is suspended or 10264 * if it's power level is changing. 10265 */ 10266 while ((un->un_state == SD_STATE_SUSPENDED) || 10267 (un->un_state == SD_STATE_PM_CHANGING)) { 10268 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10269 } 10270 10271 un->un_ncmds_in_driver++; 10272 10273 /* 10274 * atapi: Since we are running the CD for now in PIO mode we need to 10275 * call bp_mapin here to avoid bp_mapin called interrupt context under 10276 * the HBA's init_pkt routine. 10277 */ 10278 if (un->un_f_cfg_is_atapi == TRUE) { 10279 mutex_exit(SD_MUTEX(un)); 10280 bp_mapin(bp); 10281 mutex_enter(SD_MUTEX(un)); 10282 } 10283 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 10284 un->un_ncmds_in_driver); 10285 10286 mutex_exit(SD_MUTEX(un)); 10287 10288 /* 10289 * This will (eventually) allocate the sd_xbuf area and 10290 * call sd_xbuf_strategy(). We just want to return the 10291 * result of ddi_xbuf_qstrategy so that we have an opt- 10292 * imized tail call which saves us a stack frame. 10293 */ 10294 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 10295 } 10296 10297 10298 /* 10299 * Function: sd_xbuf_strategy 10300 * 10301 * Description: Function for initiating IO operations via the 10302 * ddi_xbuf_qstrategy() mechanism. 10303 * 10304 * Context: Kernel thread context. 10305 */ 10306 10307 static void 10308 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 10309 { 10310 struct sd_lun *un = arg; 10311 10312 ASSERT(bp != NULL); 10313 ASSERT(xp != NULL); 10314 ASSERT(un != NULL); 10315 ASSERT(!mutex_owned(SD_MUTEX(un))); 10316 10317 /* 10318 * Initialize the fields in the xbuf and save a pointer to the 10319 * xbuf in bp->b_private. 10320 */ 10321 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 10322 10323 /* Send the buf down the iostart chain */ 10324 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 10325 } 10326 10327 10328 /* 10329 * Function: sd_xbuf_init 10330 * 10331 * Description: Prepare the given sd_xbuf struct for use. 10332 * 10333 * Arguments: un - ptr to softstate 10334 * bp - ptr to associated buf(9S) 10335 * xp - ptr to associated sd_xbuf 10336 * chain_type - IO chain type to use: 10337 * SD_CHAIN_NULL 10338 * SD_CHAIN_BUFIO 10339 * SD_CHAIN_USCSI 10340 * SD_CHAIN_DIRECT 10341 * SD_CHAIN_DIRECT_PRIORITY 10342 * pktinfop - ptr to private data struct for scsi_pkt(9S) 10343 * initialization; may be NULL if none. 10344 * 10345 * Context: Kernel thread context 10346 */ 10347 10348 static void 10349 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 10350 uchar_t chain_type, void *pktinfop) 10351 { 10352 int index; 10353 10354 ASSERT(un != NULL); 10355 ASSERT(bp != NULL); 10356 ASSERT(xp != NULL); 10357 10358 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 10359 bp, chain_type); 10360 10361 xp->xb_un = un; 10362 xp->xb_pktp = NULL; 10363 xp->xb_pktinfo = pktinfop; 10364 xp->xb_private = bp->b_private; 10365 xp->xb_blkno = (daddr_t)bp->b_blkno; 10366 10367 /* 10368 * Set up the iostart and iodone chain indexes in the xbuf, based 10369 * upon the specified chain type to use. 10370 */ 10371 switch (chain_type) { 10372 case SD_CHAIN_NULL: 10373 /* 10374 * Fall thru to just use the values for the buf type, even 10375 * tho for the NULL chain these values will never be used. 10376 */ 10377 /* FALLTHRU */ 10378 case SD_CHAIN_BUFIO: 10379 index = un->un_buf_chain_type; 10380 break; 10381 case SD_CHAIN_USCSI: 10382 index = un->un_uscsi_chain_type; 10383 break; 10384 case SD_CHAIN_DIRECT: 10385 index = un->un_direct_chain_type; 10386 break; 10387 case SD_CHAIN_DIRECT_PRIORITY: 10388 index = un->un_priority_chain_type; 10389 break; 10390 default: 10391 /* We're really broken if we ever get here... */ 10392 panic("sd_xbuf_init: illegal chain type!"); 10393 /*NOTREACHED*/ 10394 } 10395 10396 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 10397 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 10398 10399 /* 10400 * It might be a bit easier to simply bzero the entire xbuf above, 10401 * but it turns out that since we init a fair number of members anyway, 10402 * we save a fair number cycles by doing explicit assignment of zero. 10403 */ 10404 xp->xb_pkt_flags = 0; 10405 xp->xb_dma_resid = 0; 10406 xp->xb_retry_count = 0; 10407 xp->xb_victim_retry_count = 0; 10408 xp->xb_ua_retry_count = 0; 10409 xp->xb_nr_retry_count = 0; 10410 xp->xb_sense_bp = NULL; 10411 xp->xb_sense_status = 0; 10412 xp->xb_sense_state = 0; 10413 xp->xb_sense_resid = 0; 10414 10415 bp->b_private = xp; 10416 bp->b_flags &= ~(B_DONE | B_ERROR); 10417 bp->b_resid = 0; 10418 bp->av_forw = NULL; 10419 bp->av_back = NULL; 10420 bioerror(bp, 0); 10421 10422 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 10423 } 10424 10425 10426 /* 10427 * Function: sd_uscsi_strategy 10428 * 10429 * Description: Wrapper for calling into the USCSI chain via physio(9F) 10430 * 10431 * Arguments: bp - buf struct ptr 10432 * 10433 * Return Code: Always returns 0 10434 * 10435 * Context: Kernel thread context 10436 */ 10437 10438 static int 10439 sd_uscsi_strategy(struct buf *bp) 10440 { 10441 struct sd_lun *un; 10442 struct sd_uscsi_info *uip; 10443 struct sd_xbuf *xp; 10444 uchar_t chain_type; 10445 10446 ASSERT(bp != NULL); 10447 10448 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 10449 if (un == NULL) { 10450 bioerror(bp, EIO); 10451 bp->b_resid = bp->b_bcount; 10452 biodone(bp); 10453 return (0); 10454 } 10455 10456 ASSERT(!mutex_owned(SD_MUTEX(un))); 10457 10458 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 10459 10460 mutex_enter(SD_MUTEX(un)); 10461 /* 10462 * atapi: Since we are running the CD for now in PIO mode we need to 10463 * call bp_mapin here to avoid bp_mapin called interrupt context under 10464 * the HBA's init_pkt routine. 10465 */ 10466 if (un->un_f_cfg_is_atapi == TRUE) { 10467 mutex_exit(SD_MUTEX(un)); 10468 bp_mapin(bp); 10469 mutex_enter(SD_MUTEX(un)); 10470 } 10471 un->un_ncmds_in_driver++; 10472 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 10473 un->un_ncmds_in_driver); 10474 mutex_exit(SD_MUTEX(un)); 10475 10476 /* 10477 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 10478 */ 10479 ASSERT(bp->b_private != NULL); 10480 uip = (struct sd_uscsi_info *)bp->b_private; 10481 10482 switch (uip->ui_flags) { 10483 case SD_PATH_DIRECT: 10484 chain_type = SD_CHAIN_DIRECT; 10485 break; 10486 case SD_PATH_DIRECT_PRIORITY: 10487 chain_type = SD_CHAIN_DIRECT_PRIORITY; 10488 break; 10489 default: 10490 chain_type = SD_CHAIN_USCSI; 10491 break; 10492 } 10493 10494 /* 10495 * We may allocate extra buf for external USCSI commands. If the 10496 * application asks for bigger than 20-byte sense data via USCSI, 10497 * SCSA layer will allocate 252 bytes sense buf for that command. 10498 */ 10499 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen > 10500 SENSE_LENGTH) { 10501 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH + 10502 MAX_SENSE_LENGTH, KM_SLEEP); 10503 } else { 10504 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP); 10505 } 10506 10507 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 10508 10509 /* Use the index obtained within xbuf_init */ 10510 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 10511 10512 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 10513 10514 return (0); 10515 } 10516 10517 /* 10518 * Function: sd_send_scsi_cmd 10519 * 10520 * Description: Runs a USCSI command for user (when called thru sdioctl), 10521 * or for the driver 10522 * 10523 * Arguments: dev - the dev_t for the device 10524 * incmd - ptr to a valid uscsi_cmd struct 10525 * flag - bit flag, indicating open settings, 32/64 bit type 10526 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 10527 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 10528 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 10529 * to use the USCSI "direct" chain and bypass the normal 10530 * command waitq. 10531 * 10532 * Return Code: 0 - successful completion of the given command 10533 * EIO - scsi_uscsi_handle_command() failed 10534 * ENXIO - soft state not found for specified dev 10535 * EINVAL 10536 * EFAULT - copyin/copyout error 10537 * return code of scsi_uscsi_handle_command(): 10538 * EIO 10539 * ENXIO 10540 * EACCES 10541 * 10542 * Context: Waits for command to complete. Can sleep. 10543 */ 10544 10545 static int 10546 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag, 10547 enum uio_seg dataspace, int path_flag) 10548 { 10549 struct sd_uscsi_info *uip; 10550 struct uscsi_cmd *uscmd; 10551 struct sd_lun *un; 10552 int format = 0; 10553 int rval; 10554 10555 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 10556 if (un == NULL) { 10557 return (ENXIO); 10558 } 10559 10560 ASSERT(!mutex_owned(SD_MUTEX(un))); 10561 10562 #ifdef SDDEBUG 10563 switch (dataspace) { 10564 case UIO_USERSPACE: 10565 SD_TRACE(SD_LOG_IO, un, 10566 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 10567 break; 10568 case UIO_SYSSPACE: 10569 SD_TRACE(SD_LOG_IO, un, 10570 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 10571 break; 10572 default: 10573 SD_TRACE(SD_LOG_IO, un, 10574 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 10575 break; 10576 } 10577 #endif 10578 10579 rval = scsi_uscsi_alloc_and_copyin((intptr_t)incmd, flag, 10580 SD_ADDRESS(un), &uscmd); 10581 if (rval != 0) { 10582 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: " 10583 "scsi_uscsi_alloc_and_copyin failed\n", un); 10584 return (rval); 10585 } 10586 10587 if ((uscmd->uscsi_cdb != NULL) && 10588 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) { 10589 mutex_enter(SD_MUTEX(un)); 10590 un->un_f_format_in_progress = TRUE; 10591 mutex_exit(SD_MUTEX(un)); 10592 format = 1; 10593 } 10594 10595 /* 10596 * Allocate an sd_uscsi_info struct and fill it with the info 10597 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 10598 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 10599 * since we allocate the buf here in this function, we do not 10600 * need to preserve the prior contents of b_private. 10601 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 10602 */ 10603 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 10604 uip->ui_flags = path_flag; 10605 uip->ui_cmdp = uscmd; 10606 10607 /* 10608 * Commands sent with priority are intended for error recovery 10609 * situations, and do not have retries performed. 10610 */ 10611 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 10612 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 10613 } 10614 uscmd->uscsi_flags &= ~USCSI_NOINTR; 10615 10616 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd, 10617 sd_uscsi_strategy, NULL, uip); 10618 10619 #ifdef SDDEBUG 10620 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 10621 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 10622 uscmd->uscsi_status, uscmd->uscsi_resid); 10623 if (uscmd->uscsi_bufaddr != NULL) { 10624 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 10625 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 10626 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 10627 if (dataspace == UIO_SYSSPACE) { 10628 SD_DUMP_MEMORY(un, SD_LOG_IO, 10629 "data", (uchar_t *)uscmd->uscsi_bufaddr, 10630 uscmd->uscsi_buflen, SD_LOG_HEX); 10631 } 10632 } 10633 #endif 10634 10635 if (format == 1) { 10636 mutex_enter(SD_MUTEX(un)); 10637 un->un_f_format_in_progress = FALSE; 10638 mutex_exit(SD_MUTEX(un)); 10639 } 10640 10641 (void) scsi_uscsi_copyout_and_free((intptr_t)incmd, uscmd); 10642 kmem_free(uip, sizeof (struct sd_uscsi_info)); 10643 10644 return (rval); 10645 } 10646 10647 10648 /* 10649 * Function: sd_buf_iodone 10650 * 10651 * Description: Frees the sd_xbuf & returns the buf to its originator. 10652 * 10653 * Context: May be called from interrupt context. 10654 */ 10655 /* ARGSUSED */ 10656 static void 10657 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 10658 { 10659 struct sd_xbuf *xp; 10660 10661 ASSERT(un != NULL); 10662 ASSERT(bp != NULL); 10663 ASSERT(!mutex_owned(SD_MUTEX(un))); 10664 10665 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 10666 10667 xp = SD_GET_XBUF(bp); 10668 ASSERT(xp != NULL); 10669 10670 mutex_enter(SD_MUTEX(un)); 10671 10672 /* 10673 * Grab time when the cmd completed. 10674 * This is used for determining if the system has been 10675 * idle long enough to make it idle to the PM framework. 10676 * This is for lowering the overhead, and therefore improving 10677 * performance per I/O operation. 10678 */ 10679 un->un_pm_idle_time = ddi_get_time(); 10680 10681 un->un_ncmds_in_driver--; 10682 ASSERT(un->un_ncmds_in_driver >= 0); 10683 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 10684 un->un_ncmds_in_driver); 10685 10686 mutex_exit(SD_MUTEX(un)); 10687 10688 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 10689 biodone(bp); /* bp is gone after this */ 10690 10691 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 10692 } 10693 10694 10695 /* 10696 * Function: sd_uscsi_iodone 10697 * 10698 * Description: Frees the sd_xbuf & returns the buf to its originator. 10699 * 10700 * Context: May be called from interrupt context. 10701 */ 10702 /* ARGSUSED */ 10703 static void 10704 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 10705 { 10706 struct sd_xbuf *xp; 10707 10708 ASSERT(un != NULL); 10709 ASSERT(bp != NULL); 10710 10711 xp = SD_GET_XBUF(bp); 10712 ASSERT(xp != NULL); 10713 ASSERT(!mutex_owned(SD_MUTEX(un))); 10714 10715 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 10716 10717 bp->b_private = xp->xb_private; 10718 10719 mutex_enter(SD_MUTEX(un)); 10720 10721 /* 10722 * Grab time when the cmd completed. 10723 * This is used for determining if the system has been 10724 * idle long enough to make it idle to the PM framework. 10725 * This is for lowering the overhead, and therefore improving 10726 * performance per I/O operation. 10727 */ 10728 un->un_pm_idle_time = ddi_get_time(); 10729 10730 un->un_ncmds_in_driver--; 10731 ASSERT(un->un_ncmds_in_driver >= 0); 10732 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 10733 un->un_ncmds_in_driver); 10734 10735 mutex_exit(SD_MUTEX(un)); 10736 10737 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen > 10738 SENSE_LENGTH) { 10739 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH + 10740 MAX_SENSE_LENGTH); 10741 } else { 10742 kmem_free(xp, sizeof (struct sd_xbuf)); 10743 } 10744 10745 biodone(bp); 10746 10747 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 10748 } 10749 10750 10751 /* 10752 * Function: sd_mapblockaddr_iostart 10753 * 10754 * Description: Verify request lies within the partition limits for 10755 * the indicated minor device. Issue "overrun" buf if 10756 * request would exceed partition range. Converts 10757 * partition-relative block address to absolute. 10758 * 10759 * Context: Can sleep 10760 * 10761 * Issues: This follows what the old code did, in terms of accessing 10762 * some of the partition info in the unit struct without holding 10763 * the mutext. This is a general issue, if the partition info 10764 * can be altered while IO is in progress... as soon as we send 10765 * a buf, its partitioning can be invalid before it gets to the 10766 * device. Probably the right fix is to move partitioning out 10767 * of the driver entirely. 10768 */ 10769 10770 static void 10771 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 10772 { 10773 diskaddr_t nblocks; /* #blocks in the given partition */ 10774 daddr_t blocknum; /* Block number specified by the buf */ 10775 size_t requested_nblocks; 10776 size_t available_nblocks; 10777 int partition; 10778 diskaddr_t partition_offset; 10779 struct sd_xbuf *xp; 10780 10781 10782 ASSERT(un != NULL); 10783 ASSERT(bp != NULL); 10784 ASSERT(!mutex_owned(SD_MUTEX(un))); 10785 10786 SD_TRACE(SD_LOG_IO_PARTITION, un, 10787 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 10788 10789 xp = SD_GET_XBUF(bp); 10790 ASSERT(xp != NULL); 10791 10792 /* 10793 * If the geometry is not indicated as valid, attempt to access 10794 * the unit & verify the geometry/label. This can be the case for 10795 * removable-media devices, of if the device was opened in 10796 * NDELAY/NONBLOCK mode. 10797 */ 10798 if (!SD_IS_VALID_LABEL(un) && 10799 (sd_ready_and_valid(un) != SD_READY_VALID)) { 10800 /* 10801 * For removable devices it is possible to start an I/O 10802 * without a media by opening the device in nodelay mode. 10803 * Also for writable CDs there can be many scenarios where 10804 * there is no geometry yet but volume manager is trying to 10805 * issue a read() just because it can see TOC on the CD. So 10806 * do not print a message for removables. 10807 */ 10808 if (!un->un_f_has_removable_media) { 10809 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10810 "i/o to invalid geometry\n"); 10811 } 10812 bioerror(bp, EIO); 10813 bp->b_resid = bp->b_bcount; 10814 SD_BEGIN_IODONE(index, un, bp); 10815 return; 10816 } 10817 10818 partition = SDPART(bp->b_edev); 10819 10820 nblocks = 0; 10821 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 10822 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT); 10823 10824 /* 10825 * blocknum is the starting block number of the request. At this 10826 * point it is still relative to the start of the minor device. 10827 */ 10828 blocknum = xp->xb_blkno; 10829 10830 /* 10831 * Legacy: If the starting block number is one past the last block 10832 * in the partition, do not set B_ERROR in the buf. 10833 */ 10834 if (blocknum == nblocks) { 10835 goto error_exit; 10836 } 10837 10838 /* 10839 * Confirm that the first block of the request lies within the 10840 * partition limits. Also the requested number of bytes must be 10841 * a multiple of the system block size. 10842 */ 10843 if ((blocknum < 0) || (blocknum >= nblocks) || 10844 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 10845 bp->b_flags |= B_ERROR; 10846 goto error_exit; 10847 } 10848 10849 /* 10850 * If the requsted # blocks exceeds the available # blocks, that 10851 * is an overrun of the partition. 10852 */ 10853 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 10854 available_nblocks = (size_t)(nblocks - blocknum); 10855 ASSERT(nblocks >= blocknum); 10856 10857 if (requested_nblocks > available_nblocks) { 10858 /* 10859 * Allocate an "overrun" buf to allow the request to proceed 10860 * for the amount of space available in the partition. The 10861 * amount not transferred will be added into the b_resid 10862 * when the operation is complete. The overrun buf 10863 * replaces the original buf here, and the original buf 10864 * is saved inside the overrun buf, for later use. 10865 */ 10866 size_t resid = SD_SYSBLOCKS2BYTES(un, 10867 (offset_t)(requested_nblocks - available_nblocks)); 10868 size_t count = bp->b_bcount - resid; 10869 /* 10870 * Note: count is an unsigned entity thus it'll NEVER 10871 * be less than 0 so ASSERT the original values are 10872 * correct. 10873 */ 10874 ASSERT(bp->b_bcount >= resid); 10875 10876 bp = sd_bioclone_alloc(bp, count, blocknum, 10877 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 10878 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 10879 ASSERT(xp != NULL); 10880 } 10881 10882 /* At this point there should be no residual for this buf. */ 10883 ASSERT(bp->b_resid == 0); 10884 10885 /* Convert the block number to an absolute address. */ 10886 xp->xb_blkno += partition_offset; 10887 10888 SD_NEXT_IOSTART(index, un, bp); 10889 10890 SD_TRACE(SD_LOG_IO_PARTITION, un, 10891 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 10892 10893 return; 10894 10895 error_exit: 10896 bp->b_resid = bp->b_bcount; 10897 SD_BEGIN_IODONE(index, un, bp); 10898 SD_TRACE(SD_LOG_IO_PARTITION, un, 10899 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 10900 } 10901 10902 10903 /* 10904 * Function: sd_mapblockaddr_iodone 10905 * 10906 * Description: Completion-side processing for partition management. 10907 * 10908 * Context: May be called under interrupt context 10909 */ 10910 10911 static void 10912 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 10913 { 10914 /* int partition; */ /* Not used, see below. */ 10915 ASSERT(un != NULL); 10916 ASSERT(bp != NULL); 10917 ASSERT(!mutex_owned(SD_MUTEX(un))); 10918 10919 SD_TRACE(SD_LOG_IO_PARTITION, un, 10920 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 10921 10922 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 10923 /* 10924 * We have an "overrun" buf to deal with... 10925 */ 10926 struct sd_xbuf *xp; 10927 struct buf *obp; /* ptr to the original buf */ 10928 10929 xp = SD_GET_XBUF(bp); 10930 ASSERT(xp != NULL); 10931 10932 /* Retrieve the pointer to the original buf */ 10933 obp = (struct buf *)xp->xb_private; 10934 ASSERT(obp != NULL); 10935 10936 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 10937 bioerror(obp, bp->b_error); 10938 10939 sd_bioclone_free(bp); 10940 10941 /* 10942 * Get back the original buf. 10943 * Note that since the restoration of xb_blkno below 10944 * was removed, the sd_xbuf is not needed. 10945 */ 10946 bp = obp; 10947 /* 10948 * xp = SD_GET_XBUF(bp); 10949 * ASSERT(xp != NULL); 10950 */ 10951 } 10952 10953 /* 10954 * Convert sd->xb_blkno back to a minor-device relative value. 10955 * Note: this has been commented out, as it is not needed in the 10956 * current implementation of the driver (ie, since this function 10957 * is at the top of the layering chains, so the info will be 10958 * discarded) and it is in the "hot" IO path. 10959 * 10960 * partition = getminor(bp->b_edev) & SDPART_MASK; 10961 * xp->xb_blkno -= un->un_offset[partition]; 10962 */ 10963 10964 SD_NEXT_IODONE(index, un, bp); 10965 10966 SD_TRACE(SD_LOG_IO_PARTITION, un, 10967 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 10968 } 10969 10970 10971 /* 10972 * Function: sd_mapblocksize_iostart 10973 * 10974 * Description: Convert between system block size (un->un_sys_blocksize) 10975 * and target block size (un->un_tgt_blocksize). 10976 * 10977 * Context: Can sleep to allocate resources. 10978 * 10979 * Assumptions: A higher layer has already performed any partition validation, 10980 * and converted the xp->xb_blkno to an absolute value relative 10981 * to the start of the device. 10982 * 10983 * It is also assumed that the higher layer has implemented 10984 * an "overrun" mechanism for the case where the request would 10985 * read/write beyond the end of a partition. In this case we 10986 * assume (and ASSERT) that bp->b_resid == 0. 10987 * 10988 * Note: The implementation for this routine assumes the target 10989 * block size remains constant between allocation and transport. 10990 */ 10991 10992 static void 10993 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 10994 { 10995 struct sd_mapblocksize_info *bsp; 10996 struct sd_xbuf *xp; 10997 offset_t first_byte; 10998 daddr_t start_block, end_block; 10999 daddr_t request_bytes; 11000 ushort_t is_aligned = FALSE; 11001 11002 ASSERT(un != NULL); 11003 ASSERT(bp != NULL); 11004 ASSERT(!mutex_owned(SD_MUTEX(un))); 11005 ASSERT(bp->b_resid == 0); 11006 11007 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11008 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 11009 11010 /* 11011 * For a non-writable CD, a write request is an error 11012 */ 11013 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 11014 (un->un_f_mmc_writable_media == FALSE)) { 11015 bioerror(bp, EIO); 11016 bp->b_resid = bp->b_bcount; 11017 SD_BEGIN_IODONE(index, un, bp); 11018 return; 11019 } 11020 11021 /* 11022 * We do not need a shadow buf if the device is using 11023 * un->un_sys_blocksize as its block size or if bcount == 0. 11024 * In this case there is no layer-private data block allocated. 11025 */ 11026 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 11027 (bp->b_bcount == 0)) { 11028 goto done; 11029 } 11030 11031 #if defined(__i386) || defined(__amd64) 11032 /* We do not support non-block-aligned transfers for ROD devices */ 11033 ASSERT(!ISROD(un)); 11034 #endif 11035 11036 xp = SD_GET_XBUF(bp); 11037 ASSERT(xp != NULL); 11038 11039 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 11040 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 11041 un->un_tgt_blocksize, un->un_sys_blocksize); 11042 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 11043 "request start block:0x%x\n", xp->xb_blkno); 11044 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 11045 "request len:0x%x\n", bp->b_bcount); 11046 11047 /* 11048 * Allocate the layer-private data area for the mapblocksize layer. 11049 * Layers are allowed to use the xp_private member of the sd_xbuf 11050 * struct to store the pointer to their layer-private data block, but 11051 * each layer also has the responsibility of restoring the prior 11052 * contents of xb_private before returning the buf/xbuf to the 11053 * higher layer that sent it. 11054 * 11055 * Here we save the prior contents of xp->xb_private into the 11056 * bsp->mbs_oprivate field of our layer-private data area. This value 11057 * is restored by sd_mapblocksize_iodone() just prior to freeing up 11058 * the layer-private area and returning the buf/xbuf to the layer 11059 * that sent it. 11060 * 11061 * Note that here we use kmem_zalloc for the allocation as there are 11062 * parts of the mapblocksize code that expect certain fields to be 11063 * zero unless explicitly set to a required value. 11064 */ 11065 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 11066 bsp->mbs_oprivate = xp->xb_private; 11067 xp->xb_private = bsp; 11068 11069 /* 11070 * This treats the data on the disk (target) as an array of bytes. 11071 * first_byte is the byte offset, from the beginning of the device, 11072 * to the location of the request. This is converted from a 11073 * un->un_sys_blocksize block address to a byte offset, and then back 11074 * to a block address based upon a un->un_tgt_blocksize block size. 11075 * 11076 * xp->xb_blkno should be absolute upon entry into this function, 11077 * but, but it is based upon partitions that use the "system" 11078 * block size. It must be adjusted to reflect the block size of 11079 * the target. 11080 * 11081 * Note that end_block is actually the block that follows the last 11082 * block of the request, but that's what is needed for the computation. 11083 */ 11084 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 11085 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 11086 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 11087 un->un_tgt_blocksize; 11088 11089 /* request_bytes is rounded up to a multiple of the target block size */ 11090 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 11091 11092 /* 11093 * See if the starting address of the request and the request 11094 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 11095 * then we do not need to allocate a shadow buf to handle the request. 11096 */ 11097 if (((first_byte % un->un_tgt_blocksize) == 0) && 11098 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 11099 is_aligned = TRUE; 11100 } 11101 11102 if ((bp->b_flags & B_READ) == 0) { 11103 /* 11104 * Lock the range for a write operation. An aligned request is 11105 * considered a simple write; otherwise the request must be a 11106 * read-modify-write. 11107 */ 11108 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 11109 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 11110 } 11111 11112 /* 11113 * Alloc a shadow buf if the request is not aligned. Also, this is 11114 * where the READ command is generated for a read-modify-write. (The 11115 * write phase is deferred until after the read completes.) 11116 */ 11117 if (is_aligned == FALSE) { 11118 11119 struct sd_mapblocksize_info *shadow_bsp; 11120 struct sd_xbuf *shadow_xp; 11121 struct buf *shadow_bp; 11122 11123 /* 11124 * Allocate the shadow buf and it associated xbuf. Note that 11125 * after this call the xb_blkno value in both the original 11126 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 11127 * same: absolute relative to the start of the device, and 11128 * adjusted for the target block size. The b_blkno in the 11129 * shadow buf will also be set to this value. We should never 11130 * change b_blkno in the original bp however. 11131 * 11132 * Note also that the shadow buf will always need to be a 11133 * READ command, regardless of whether the incoming command 11134 * is a READ or a WRITE. 11135 */ 11136 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 11137 xp->xb_blkno, 11138 (int (*)(struct buf *)) sd_mapblocksize_iodone); 11139 11140 shadow_xp = SD_GET_XBUF(shadow_bp); 11141 11142 /* 11143 * Allocate the layer-private data for the shadow buf. 11144 * (No need to preserve xb_private in the shadow xbuf.) 11145 */ 11146 shadow_xp->xb_private = shadow_bsp = 11147 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 11148 11149 /* 11150 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 11151 * to figure out where the start of the user data is (based upon 11152 * the system block size) in the data returned by the READ 11153 * command (which will be based upon the target blocksize). Note 11154 * that this is only really used if the request is unaligned. 11155 */ 11156 bsp->mbs_copy_offset = (ssize_t)(first_byte - 11157 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 11158 ASSERT((bsp->mbs_copy_offset >= 0) && 11159 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 11160 11161 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 11162 11163 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 11164 11165 /* Transfer the wmap (if any) to the shadow buf */ 11166 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 11167 bsp->mbs_wmp = NULL; 11168 11169 /* 11170 * The shadow buf goes on from here in place of the 11171 * original buf. 11172 */ 11173 shadow_bsp->mbs_orig_bp = bp; 11174 bp = shadow_bp; 11175 } 11176 11177 SD_INFO(SD_LOG_IO_RMMEDIA, un, 11178 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 11179 SD_INFO(SD_LOG_IO_RMMEDIA, un, 11180 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 11181 request_bytes); 11182 SD_INFO(SD_LOG_IO_RMMEDIA, un, 11183 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 11184 11185 done: 11186 SD_NEXT_IOSTART(index, un, bp); 11187 11188 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11189 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 11190 } 11191 11192 11193 /* 11194 * Function: sd_mapblocksize_iodone 11195 * 11196 * Description: Completion side processing for block-size mapping. 11197 * 11198 * Context: May be called under interrupt context 11199 */ 11200 11201 static void 11202 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 11203 { 11204 struct sd_mapblocksize_info *bsp; 11205 struct sd_xbuf *xp; 11206 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 11207 struct buf *orig_bp; /* ptr to the original buf */ 11208 offset_t shadow_end; 11209 offset_t request_end; 11210 offset_t shadow_start; 11211 ssize_t copy_offset; 11212 size_t copy_length; 11213 size_t shortfall; 11214 uint_t is_write; /* TRUE if this bp is a WRITE */ 11215 uint_t has_wmap; /* TRUE is this bp has a wmap */ 11216 11217 ASSERT(un != NULL); 11218 ASSERT(bp != NULL); 11219 11220 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 11221 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 11222 11223 /* 11224 * There is no shadow buf or layer-private data if the target is 11225 * using un->un_sys_blocksize as its block size or if bcount == 0. 11226 */ 11227 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 11228 (bp->b_bcount == 0)) { 11229 goto exit; 11230 } 11231 11232 xp = SD_GET_XBUF(bp); 11233 ASSERT(xp != NULL); 11234 11235 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 11236 bsp = xp->xb_private; 11237 11238 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 11239 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 11240 11241 if (is_write) { 11242 /* 11243 * For a WRITE request we must free up the block range that 11244 * we have locked up. This holds regardless of whether this is 11245 * an aligned write request or a read-modify-write request. 11246 */ 11247 sd_range_unlock(un, bsp->mbs_wmp); 11248 bsp->mbs_wmp = NULL; 11249 } 11250 11251 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 11252 /* 11253 * An aligned read or write command will have no shadow buf; 11254 * there is not much else to do with it. 11255 */ 11256 goto done; 11257 } 11258 11259 orig_bp = bsp->mbs_orig_bp; 11260 ASSERT(orig_bp != NULL); 11261 orig_xp = SD_GET_XBUF(orig_bp); 11262 ASSERT(orig_xp != NULL); 11263 ASSERT(!mutex_owned(SD_MUTEX(un))); 11264 11265 if (!is_write && has_wmap) { 11266 /* 11267 * A READ with a wmap means this is the READ phase of a 11268 * read-modify-write. If an error occurred on the READ then 11269 * we do not proceed with the WRITE phase or copy any data. 11270 * Just release the write maps and return with an error. 11271 */ 11272 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 11273 orig_bp->b_resid = orig_bp->b_bcount; 11274 bioerror(orig_bp, bp->b_error); 11275 sd_range_unlock(un, bsp->mbs_wmp); 11276 goto freebuf_done; 11277 } 11278 } 11279 11280 /* 11281 * Here is where we set up to copy the data from the shadow buf 11282 * into the space associated with the original buf. 11283 * 11284 * To deal with the conversion between block sizes, these 11285 * computations treat the data as an array of bytes, with the 11286 * first byte (byte 0) corresponding to the first byte in the 11287 * first block on the disk. 11288 */ 11289 11290 /* 11291 * shadow_start and shadow_len indicate the location and size of 11292 * the data returned with the shadow IO request. 11293 */ 11294 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 11295 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 11296 11297 /* 11298 * copy_offset gives the offset (in bytes) from the start of the first 11299 * block of the READ request to the beginning of the data. We retrieve 11300 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 11301 * there by sd_mapblockize_iostart(). copy_length gives the amount of 11302 * data to be copied (in bytes). 11303 */ 11304 copy_offset = bsp->mbs_copy_offset; 11305 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 11306 copy_length = orig_bp->b_bcount; 11307 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 11308 11309 /* 11310 * Set up the resid and error fields of orig_bp as appropriate. 11311 */ 11312 if (shadow_end >= request_end) { 11313 /* We got all the requested data; set resid to zero */ 11314 orig_bp->b_resid = 0; 11315 } else { 11316 /* 11317 * We failed to get enough data to fully satisfy the original 11318 * request. Just copy back whatever data we got and set 11319 * up the residual and error code as required. 11320 * 11321 * 'shortfall' is the amount by which the data received with the 11322 * shadow buf has "fallen short" of the requested amount. 11323 */ 11324 shortfall = (size_t)(request_end - shadow_end); 11325 11326 if (shortfall > orig_bp->b_bcount) { 11327 /* 11328 * We did not get enough data to even partially 11329 * fulfill the original request. The residual is 11330 * equal to the amount requested. 11331 */ 11332 orig_bp->b_resid = orig_bp->b_bcount; 11333 } else { 11334 /* 11335 * We did not get all the data that we requested 11336 * from the device, but we will try to return what 11337 * portion we did get. 11338 */ 11339 orig_bp->b_resid = shortfall; 11340 } 11341 ASSERT(copy_length >= orig_bp->b_resid); 11342 copy_length -= orig_bp->b_resid; 11343 } 11344 11345 /* Propagate the error code from the shadow buf to the original buf */ 11346 bioerror(orig_bp, bp->b_error); 11347 11348 if (is_write) { 11349 goto freebuf_done; /* No data copying for a WRITE */ 11350 } 11351 11352 if (has_wmap) { 11353 /* 11354 * This is a READ command from the READ phase of a 11355 * read-modify-write request. We have to copy the data given 11356 * by the user OVER the data returned by the READ command, 11357 * then convert the command from a READ to a WRITE and send 11358 * it back to the target. 11359 */ 11360 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 11361 copy_length); 11362 11363 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 11364 11365 /* 11366 * Dispatch the WRITE command to the taskq thread, which 11367 * will in turn send the command to the target. When the 11368 * WRITE command completes, we (sd_mapblocksize_iodone()) 11369 * will get called again as part of the iodone chain 11370 * processing for it. Note that we will still be dealing 11371 * with the shadow buf at that point. 11372 */ 11373 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 11374 KM_NOSLEEP) != 0) { 11375 /* 11376 * Dispatch was successful so we are done. Return 11377 * without going any higher up the iodone chain. Do 11378 * not free up any layer-private data until after the 11379 * WRITE completes. 11380 */ 11381 return; 11382 } 11383 11384 /* 11385 * Dispatch of the WRITE command failed; set up the error 11386 * condition and send this IO back up the iodone chain. 11387 */ 11388 bioerror(orig_bp, EIO); 11389 orig_bp->b_resid = orig_bp->b_bcount; 11390 11391 } else { 11392 /* 11393 * This is a regular READ request (ie, not a RMW). Copy the 11394 * data from the shadow buf into the original buf. The 11395 * copy_offset compensates for any "misalignment" between the 11396 * shadow buf (with its un->un_tgt_blocksize blocks) and the 11397 * original buf (with its un->un_sys_blocksize blocks). 11398 */ 11399 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 11400 copy_length); 11401 } 11402 11403 freebuf_done: 11404 11405 /* 11406 * At this point we still have both the shadow buf AND the original 11407 * buf to deal with, as well as the layer-private data area in each. 11408 * Local variables are as follows: 11409 * 11410 * bp -- points to shadow buf 11411 * xp -- points to xbuf of shadow buf 11412 * bsp -- points to layer-private data area of shadow buf 11413 * orig_bp -- points to original buf 11414 * 11415 * First free the shadow buf and its associated xbuf, then free the 11416 * layer-private data area from the shadow buf. There is no need to 11417 * restore xb_private in the shadow xbuf. 11418 */ 11419 sd_shadow_buf_free(bp); 11420 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 11421 11422 /* 11423 * Now update the local variables to point to the original buf, xbuf, 11424 * and layer-private area. 11425 */ 11426 bp = orig_bp; 11427 xp = SD_GET_XBUF(bp); 11428 ASSERT(xp != NULL); 11429 ASSERT(xp == orig_xp); 11430 bsp = xp->xb_private; 11431 ASSERT(bsp != NULL); 11432 11433 done: 11434 /* 11435 * Restore xb_private to whatever it was set to by the next higher 11436 * layer in the chain, then free the layer-private data area. 11437 */ 11438 xp->xb_private = bsp->mbs_oprivate; 11439 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 11440 11441 exit: 11442 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 11443 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 11444 11445 SD_NEXT_IODONE(index, un, bp); 11446 } 11447 11448 11449 /* 11450 * Function: sd_checksum_iostart 11451 * 11452 * Description: A stub function for a layer that's currently not used. 11453 * For now just a placeholder. 11454 * 11455 * Context: Kernel thread context 11456 */ 11457 11458 static void 11459 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 11460 { 11461 ASSERT(un != NULL); 11462 ASSERT(bp != NULL); 11463 ASSERT(!mutex_owned(SD_MUTEX(un))); 11464 SD_NEXT_IOSTART(index, un, bp); 11465 } 11466 11467 11468 /* 11469 * Function: sd_checksum_iodone 11470 * 11471 * Description: A stub function for a layer that's currently not used. 11472 * For now just a placeholder. 11473 * 11474 * Context: May be called under interrupt context 11475 */ 11476 11477 static void 11478 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 11479 { 11480 ASSERT(un != NULL); 11481 ASSERT(bp != NULL); 11482 ASSERT(!mutex_owned(SD_MUTEX(un))); 11483 SD_NEXT_IODONE(index, un, bp); 11484 } 11485 11486 11487 /* 11488 * Function: sd_checksum_uscsi_iostart 11489 * 11490 * Description: A stub function for a layer that's currently not used. 11491 * For now just a placeholder. 11492 * 11493 * Context: Kernel thread context 11494 */ 11495 11496 static void 11497 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 11498 { 11499 ASSERT(un != NULL); 11500 ASSERT(bp != NULL); 11501 ASSERT(!mutex_owned(SD_MUTEX(un))); 11502 SD_NEXT_IOSTART(index, un, bp); 11503 } 11504 11505 11506 /* 11507 * Function: sd_checksum_uscsi_iodone 11508 * 11509 * Description: A stub function for a layer that's currently not used. 11510 * For now just a placeholder. 11511 * 11512 * Context: May be called under interrupt context 11513 */ 11514 11515 static void 11516 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 11517 { 11518 ASSERT(un != NULL); 11519 ASSERT(bp != NULL); 11520 ASSERT(!mutex_owned(SD_MUTEX(un))); 11521 SD_NEXT_IODONE(index, un, bp); 11522 } 11523 11524 11525 /* 11526 * Function: sd_pm_iostart 11527 * 11528 * Description: iostart-side routine for Power mangement. 11529 * 11530 * Context: Kernel thread context 11531 */ 11532 11533 static void 11534 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 11535 { 11536 ASSERT(un != NULL); 11537 ASSERT(bp != NULL); 11538 ASSERT(!mutex_owned(SD_MUTEX(un))); 11539 ASSERT(!mutex_owned(&un->un_pm_mutex)); 11540 11541 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 11542 11543 if (sd_pm_entry(un) != DDI_SUCCESS) { 11544 /* 11545 * Set up to return the failed buf back up the 'iodone' 11546 * side of the calling chain. 11547 */ 11548 bioerror(bp, EIO); 11549 bp->b_resid = bp->b_bcount; 11550 11551 SD_BEGIN_IODONE(index, un, bp); 11552 11553 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 11554 return; 11555 } 11556 11557 SD_NEXT_IOSTART(index, un, bp); 11558 11559 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 11560 } 11561 11562 11563 /* 11564 * Function: sd_pm_iodone 11565 * 11566 * Description: iodone-side routine for power mangement. 11567 * 11568 * Context: may be called from interrupt context 11569 */ 11570 11571 static void 11572 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 11573 { 11574 ASSERT(un != NULL); 11575 ASSERT(bp != NULL); 11576 ASSERT(!mutex_owned(&un->un_pm_mutex)); 11577 11578 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 11579 11580 /* 11581 * After attach the following flag is only read, so don't 11582 * take the penalty of acquiring a mutex for it. 11583 */ 11584 if (un->un_f_pm_is_enabled == TRUE) { 11585 sd_pm_exit(un); 11586 } 11587 11588 SD_NEXT_IODONE(index, un, bp); 11589 11590 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 11591 } 11592 11593 11594 /* 11595 * Function: sd_core_iostart 11596 * 11597 * Description: Primary driver function for enqueuing buf(9S) structs from 11598 * the system and initiating IO to the target device 11599 * 11600 * Context: Kernel thread context. Can sleep. 11601 * 11602 * Assumptions: - The given xp->xb_blkno is absolute 11603 * (ie, relative to the start of the device). 11604 * - The IO is to be done using the native blocksize of 11605 * the device, as specified in un->un_tgt_blocksize. 11606 */ 11607 /* ARGSUSED */ 11608 static void 11609 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 11610 { 11611 struct sd_xbuf *xp; 11612 11613 ASSERT(un != NULL); 11614 ASSERT(bp != NULL); 11615 ASSERT(!mutex_owned(SD_MUTEX(un))); 11616 ASSERT(bp->b_resid == 0); 11617 11618 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 11619 11620 xp = SD_GET_XBUF(bp); 11621 ASSERT(xp != NULL); 11622 11623 mutex_enter(SD_MUTEX(un)); 11624 11625 /* 11626 * If we are currently in the failfast state, fail any new IO 11627 * that has B_FAILFAST set, then return. 11628 */ 11629 if ((bp->b_flags & B_FAILFAST) && 11630 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 11631 mutex_exit(SD_MUTEX(un)); 11632 bioerror(bp, EIO); 11633 bp->b_resid = bp->b_bcount; 11634 SD_BEGIN_IODONE(index, un, bp); 11635 return; 11636 } 11637 11638 if (SD_IS_DIRECT_PRIORITY(xp)) { 11639 /* 11640 * Priority command -- transport it immediately. 11641 * 11642 * Note: We may want to assert that USCSI_DIAGNOSE is set, 11643 * because all direct priority commands should be associated 11644 * with error recovery actions which we don't want to retry. 11645 */ 11646 sd_start_cmds(un, bp); 11647 } else { 11648 /* 11649 * Normal command -- add it to the wait queue, then start 11650 * transporting commands from the wait queue. 11651 */ 11652 sd_add_buf_to_waitq(un, bp); 11653 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 11654 sd_start_cmds(un, NULL); 11655 } 11656 11657 mutex_exit(SD_MUTEX(un)); 11658 11659 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 11660 } 11661 11662 11663 /* 11664 * Function: sd_init_cdb_limits 11665 * 11666 * Description: This is to handle scsi_pkt initialization differences 11667 * between the driver platforms. 11668 * 11669 * Legacy behaviors: 11670 * 11671 * If the block number or the sector count exceeds the 11672 * capabilities of a Group 0 command, shift over to a 11673 * Group 1 command. We don't blindly use Group 1 11674 * commands because a) some drives (CDC Wren IVs) get a 11675 * bit confused, and b) there is probably a fair amount 11676 * of speed difference for a target to receive and decode 11677 * a 10 byte command instead of a 6 byte command. 11678 * 11679 * The xfer time difference of 6 vs 10 byte CDBs is 11680 * still significant so this code is still worthwhile. 11681 * 10 byte CDBs are very inefficient with the fas HBA driver 11682 * and older disks. Each CDB byte took 1 usec with some 11683 * popular disks. 11684 * 11685 * Context: Must be called at attach time 11686 */ 11687 11688 static void 11689 sd_init_cdb_limits(struct sd_lun *un) 11690 { 11691 int hba_cdb_limit; 11692 11693 /* 11694 * Use CDB_GROUP1 commands for most devices except for 11695 * parallel SCSI fixed drives in which case we get better 11696 * performance using CDB_GROUP0 commands (where applicable). 11697 */ 11698 un->un_mincdb = SD_CDB_GROUP1; 11699 #if !defined(__fibre) 11700 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 11701 !un->un_f_has_removable_media) { 11702 un->un_mincdb = SD_CDB_GROUP0; 11703 } 11704 #endif 11705 11706 /* 11707 * Try to read the max-cdb-length supported by HBA. 11708 */ 11709 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 11710 if (0 >= un->un_max_hba_cdb) { 11711 un->un_max_hba_cdb = CDB_GROUP4; 11712 hba_cdb_limit = SD_CDB_GROUP4; 11713 } else if (0 < un->un_max_hba_cdb && 11714 un->un_max_hba_cdb < CDB_GROUP1) { 11715 hba_cdb_limit = SD_CDB_GROUP0; 11716 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 11717 un->un_max_hba_cdb < CDB_GROUP5) { 11718 hba_cdb_limit = SD_CDB_GROUP1; 11719 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 11720 un->un_max_hba_cdb < CDB_GROUP4) { 11721 hba_cdb_limit = SD_CDB_GROUP5; 11722 } else { 11723 hba_cdb_limit = SD_CDB_GROUP4; 11724 } 11725 11726 /* 11727 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 11728 * commands for fixed disks unless we are building for a 32 bit 11729 * kernel. 11730 */ 11731 #ifdef _LP64 11732 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 11733 min(hba_cdb_limit, SD_CDB_GROUP4); 11734 #else 11735 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 11736 min(hba_cdb_limit, SD_CDB_GROUP1); 11737 #endif 11738 11739 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 11740 ? sizeof (struct scsi_arq_status) : 1); 11741 un->un_cmd_timeout = (ushort_t)sd_io_time; 11742 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 11743 } 11744 11745 11746 /* 11747 * Function: sd_initpkt_for_buf 11748 * 11749 * Description: Allocate and initialize for transport a scsi_pkt struct, 11750 * based upon the info specified in the given buf struct. 11751 * 11752 * Assumes the xb_blkno in the request is absolute (ie, 11753 * relative to the start of the device (NOT partition!). 11754 * Also assumes that the request is using the native block 11755 * size of the device (as returned by the READ CAPACITY 11756 * command). 11757 * 11758 * Return Code: SD_PKT_ALLOC_SUCCESS 11759 * SD_PKT_ALLOC_FAILURE 11760 * SD_PKT_ALLOC_FAILURE_NO_DMA 11761 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11762 * 11763 * Context: Kernel thread and may be called from software interrupt context 11764 * as part of a sdrunout callback. This function may not block or 11765 * call routines that block 11766 */ 11767 11768 static int 11769 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 11770 { 11771 struct sd_xbuf *xp; 11772 struct scsi_pkt *pktp = NULL; 11773 struct sd_lun *un; 11774 size_t blockcount; 11775 daddr_t startblock; 11776 int rval; 11777 int cmd_flags; 11778 11779 ASSERT(bp != NULL); 11780 ASSERT(pktpp != NULL); 11781 xp = SD_GET_XBUF(bp); 11782 ASSERT(xp != NULL); 11783 un = SD_GET_UN(bp); 11784 ASSERT(un != NULL); 11785 ASSERT(mutex_owned(SD_MUTEX(un))); 11786 ASSERT(bp->b_resid == 0); 11787 11788 SD_TRACE(SD_LOG_IO_CORE, un, 11789 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 11790 11791 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11792 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 11793 /* 11794 * Already have a scsi_pkt -- just need DMA resources. 11795 * We must recompute the CDB in case the mapping returns 11796 * a nonzero pkt_resid. 11797 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 11798 * that is being retried, the unmap/remap of the DMA resouces 11799 * will result in the entire transfer starting over again 11800 * from the very first block. 11801 */ 11802 ASSERT(xp->xb_pktp != NULL); 11803 pktp = xp->xb_pktp; 11804 } else { 11805 pktp = NULL; 11806 } 11807 #endif /* __i386 || __amd64 */ 11808 11809 startblock = xp->xb_blkno; /* Absolute block num. */ 11810 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 11811 11812 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11813 11814 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 11815 11816 #else 11817 11818 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 11819 11820 #endif 11821 11822 /* 11823 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 11824 * call scsi_init_pkt, and build the CDB. 11825 */ 11826 rval = sd_setup_rw_pkt(un, &pktp, bp, 11827 cmd_flags, sdrunout, (caddr_t)un, 11828 startblock, blockcount); 11829 11830 if (rval == 0) { 11831 /* 11832 * Success. 11833 * 11834 * If partial DMA is being used and required for this transfer. 11835 * set it up here. 11836 */ 11837 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 11838 (pktp->pkt_resid != 0)) { 11839 11840 /* 11841 * Save the CDB length and pkt_resid for the 11842 * next xfer 11843 */ 11844 xp->xb_dma_resid = pktp->pkt_resid; 11845 11846 /* rezero resid */ 11847 pktp->pkt_resid = 0; 11848 11849 } else { 11850 xp->xb_dma_resid = 0; 11851 } 11852 11853 pktp->pkt_flags = un->un_tagflags; 11854 pktp->pkt_time = un->un_cmd_timeout; 11855 pktp->pkt_comp = sdintr; 11856 11857 pktp->pkt_private = bp; 11858 *pktpp = pktp; 11859 11860 SD_TRACE(SD_LOG_IO_CORE, un, 11861 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 11862 11863 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 11864 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 11865 #endif 11866 11867 return (SD_PKT_ALLOC_SUCCESS); 11868 11869 } 11870 11871 /* 11872 * SD_PKT_ALLOC_FAILURE is the only expected failure code 11873 * from sd_setup_rw_pkt. 11874 */ 11875 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 11876 11877 if (rval == SD_PKT_ALLOC_FAILURE) { 11878 *pktpp = NULL; 11879 /* 11880 * Set the driver state to RWAIT to indicate the driver 11881 * is waiting on resource allocations. The driver will not 11882 * suspend, pm_suspend, or detatch while the state is RWAIT. 11883 */ 11884 New_state(un, SD_STATE_RWAIT); 11885 11886 SD_ERROR(SD_LOG_IO_CORE, un, 11887 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 11888 11889 if ((bp->b_flags & B_ERROR) != 0) { 11890 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 11891 } 11892 return (SD_PKT_ALLOC_FAILURE); 11893 } else { 11894 /* 11895 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 11896 * 11897 * This should never happen. Maybe someone messed with the 11898 * kernel's minphys? 11899 */ 11900 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11901 "Request rejected: too large for CDB: " 11902 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 11903 SD_ERROR(SD_LOG_IO_CORE, un, 11904 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 11905 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 11906 11907 } 11908 } 11909 11910 11911 /* 11912 * Function: sd_destroypkt_for_buf 11913 * 11914 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 11915 * 11916 * Context: Kernel thread or interrupt context 11917 */ 11918 11919 static void 11920 sd_destroypkt_for_buf(struct buf *bp) 11921 { 11922 ASSERT(bp != NULL); 11923 ASSERT(SD_GET_UN(bp) != NULL); 11924 11925 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 11926 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 11927 11928 ASSERT(SD_GET_PKTP(bp) != NULL); 11929 scsi_destroy_pkt(SD_GET_PKTP(bp)); 11930 11931 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 11932 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 11933 } 11934 11935 /* 11936 * Function: sd_setup_rw_pkt 11937 * 11938 * Description: Determines appropriate CDB group for the requested LBA 11939 * and transfer length, calls scsi_init_pkt, and builds 11940 * the CDB. Do not use for partial DMA transfers except 11941 * for the initial transfer since the CDB size must 11942 * remain constant. 11943 * 11944 * Context: Kernel thread and may be called from software interrupt 11945 * context as part of a sdrunout callback. This function may not 11946 * block or call routines that block 11947 */ 11948 11949 11950 int 11951 sd_setup_rw_pkt(struct sd_lun *un, 11952 struct scsi_pkt **pktpp, struct buf *bp, int flags, 11953 int (*callback)(caddr_t), caddr_t callback_arg, 11954 diskaddr_t lba, uint32_t blockcount) 11955 { 11956 struct scsi_pkt *return_pktp; 11957 union scsi_cdb *cdbp; 11958 struct sd_cdbinfo *cp = NULL; 11959 int i; 11960 11961 /* 11962 * See which size CDB to use, based upon the request. 11963 */ 11964 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 11965 11966 /* 11967 * Check lba and block count against sd_cdbtab limits. 11968 * In the partial DMA case, we have to use the same size 11969 * CDB for all the transfers. Check lba + blockcount 11970 * against the max LBA so we know that segment of the 11971 * transfer can use the CDB we select. 11972 */ 11973 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 11974 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 11975 11976 /* 11977 * The command will fit into the CDB type 11978 * specified by sd_cdbtab[i]. 11979 */ 11980 cp = sd_cdbtab + i; 11981 11982 /* 11983 * Call scsi_init_pkt so we can fill in the 11984 * CDB. 11985 */ 11986 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 11987 bp, cp->sc_grpcode, un->un_status_len, 0, 11988 flags, callback, callback_arg); 11989 11990 if (return_pktp != NULL) { 11991 11992 /* 11993 * Return new value of pkt 11994 */ 11995 *pktpp = return_pktp; 11996 11997 /* 11998 * To be safe, zero the CDB insuring there is 11999 * no leftover data from a previous command. 12000 */ 12001 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 12002 12003 /* 12004 * Handle partial DMA mapping 12005 */ 12006 if (return_pktp->pkt_resid != 0) { 12007 12008 /* 12009 * Not going to xfer as many blocks as 12010 * originally expected 12011 */ 12012 blockcount -= 12013 SD_BYTES2TGTBLOCKS(un, 12014 return_pktp->pkt_resid); 12015 } 12016 12017 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 12018 12019 /* 12020 * Set command byte based on the CDB 12021 * type we matched. 12022 */ 12023 cdbp->scc_cmd = cp->sc_grpmask | 12024 ((bp->b_flags & B_READ) ? 12025 SCMD_READ : SCMD_WRITE); 12026 12027 SD_FILL_SCSI1_LUN(un, return_pktp); 12028 12029 /* 12030 * Fill in LBA and length 12031 */ 12032 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 12033 (cp->sc_grpcode == CDB_GROUP4) || 12034 (cp->sc_grpcode == CDB_GROUP0) || 12035 (cp->sc_grpcode == CDB_GROUP5)); 12036 12037 if (cp->sc_grpcode == CDB_GROUP1) { 12038 FORMG1ADDR(cdbp, lba); 12039 FORMG1COUNT(cdbp, blockcount); 12040 return (0); 12041 } else if (cp->sc_grpcode == CDB_GROUP4) { 12042 FORMG4LONGADDR(cdbp, lba); 12043 FORMG4COUNT(cdbp, blockcount); 12044 return (0); 12045 } else if (cp->sc_grpcode == CDB_GROUP0) { 12046 FORMG0ADDR(cdbp, lba); 12047 FORMG0COUNT(cdbp, blockcount); 12048 return (0); 12049 } else if (cp->sc_grpcode == CDB_GROUP5) { 12050 FORMG5ADDR(cdbp, lba); 12051 FORMG5COUNT(cdbp, blockcount); 12052 return (0); 12053 } 12054 12055 /* 12056 * It should be impossible to not match one 12057 * of the CDB types above, so we should never 12058 * reach this point. Set the CDB command byte 12059 * to test-unit-ready to avoid writing 12060 * to somewhere we don't intend. 12061 */ 12062 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 12063 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12064 } else { 12065 /* 12066 * Couldn't get scsi_pkt 12067 */ 12068 return (SD_PKT_ALLOC_FAILURE); 12069 } 12070 } 12071 } 12072 12073 /* 12074 * None of the available CDB types were suitable. This really 12075 * should never happen: on a 64 bit system we support 12076 * READ16/WRITE16 which will hold an entire 64 bit disk address 12077 * and on a 32 bit system we will refuse to bind to a device 12078 * larger than 2TB so addresses will never be larger than 32 bits. 12079 */ 12080 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12081 } 12082 12083 /* 12084 * Function: sd_setup_next_rw_pkt 12085 * 12086 * Description: Setup packet for partial DMA transfers, except for the 12087 * initial transfer. sd_setup_rw_pkt should be used for 12088 * the initial transfer. 12089 * 12090 * Context: Kernel thread and may be called from interrupt context. 12091 */ 12092 12093 int 12094 sd_setup_next_rw_pkt(struct sd_lun *un, 12095 struct scsi_pkt *pktp, struct buf *bp, 12096 diskaddr_t lba, uint32_t blockcount) 12097 { 12098 uchar_t com; 12099 union scsi_cdb *cdbp; 12100 uchar_t cdb_group_id; 12101 12102 ASSERT(pktp != NULL); 12103 ASSERT(pktp->pkt_cdbp != NULL); 12104 12105 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 12106 com = cdbp->scc_cmd; 12107 cdb_group_id = CDB_GROUPID(com); 12108 12109 ASSERT((cdb_group_id == CDB_GROUPID_0) || 12110 (cdb_group_id == CDB_GROUPID_1) || 12111 (cdb_group_id == CDB_GROUPID_4) || 12112 (cdb_group_id == CDB_GROUPID_5)); 12113 12114 /* 12115 * Move pkt to the next portion of the xfer. 12116 * func is NULL_FUNC so we do not have to release 12117 * the disk mutex here. 12118 */ 12119 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 12120 NULL_FUNC, NULL) == pktp) { 12121 /* Success. Handle partial DMA */ 12122 if (pktp->pkt_resid != 0) { 12123 blockcount -= 12124 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 12125 } 12126 12127 cdbp->scc_cmd = com; 12128 SD_FILL_SCSI1_LUN(un, pktp); 12129 if (cdb_group_id == CDB_GROUPID_1) { 12130 FORMG1ADDR(cdbp, lba); 12131 FORMG1COUNT(cdbp, blockcount); 12132 return (0); 12133 } else if (cdb_group_id == CDB_GROUPID_4) { 12134 FORMG4LONGADDR(cdbp, lba); 12135 FORMG4COUNT(cdbp, blockcount); 12136 return (0); 12137 } else if (cdb_group_id == CDB_GROUPID_0) { 12138 FORMG0ADDR(cdbp, lba); 12139 FORMG0COUNT(cdbp, blockcount); 12140 return (0); 12141 } else if (cdb_group_id == CDB_GROUPID_5) { 12142 FORMG5ADDR(cdbp, lba); 12143 FORMG5COUNT(cdbp, blockcount); 12144 return (0); 12145 } 12146 12147 /* Unreachable */ 12148 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 12149 } 12150 12151 /* 12152 * Error setting up next portion of cmd transfer. 12153 * Something is definitely very wrong and this 12154 * should not happen. 12155 */ 12156 return (SD_PKT_ALLOC_FAILURE); 12157 } 12158 12159 /* 12160 * Function: sd_initpkt_for_uscsi 12161 * 12162 * Description: Allocate and initialize for transport a scsi_pkt struct, 12163 * based upon the info specified in the given uscsi_cmd struct. 12164 * 12165 * Return Code: SD_PKT_ALLOC_SUCCESS 12166 * SD_PKT_ALLOC_FAILURE 12167 * SD_PKT_ALLOC_FAILURE_NO_DMA 12168 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 12169 * 12170 * Context: Kernel thread and may be called from software interrupt context 12171 * as part of a sdrunout callback. This function may not block or 12172 * call routines that block 12173 */ 12174 12175 static int 12176 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 12177 { 12178 struct uscsi_cmd *uscmd; 12179 struct sd_xbuf *xp; 12180 struct scsi_pkt *pktp; 12181 struct sd_lun *un; 12182 uint32_t flags = 0; 12183 12184 ASSERT(bp != NULL); 12185 ASSERT(pktpp != NULL); 12186 xp = SD_GET_XBUF(bp); 12187 ASSERT(xp != NULL); 12188 un = SD_GET_UN(bp); 12189 ASSERT(un != NULL); 12190 ASSERT(mutex_owned(SD_MUTEX(un))); 12191 12192 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 12193 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 12194 ASSERT(uscmd != NULL); 12195 12196 SD_TRACE(SD_LOG_IO_CORE, un, 12197 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 12198 12199 /* 12200 * Allocate the scsi_pkt for the command. 12201 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 12202 * during scsi_init_pkt time and will continue to use the 12203 * same path as long as the same scsi_pkt is used without 12204 * intervening scsi_dma_free(). Since uscsi command does 12205 * not call scsi_dmafree() before retry failed command, it 12206 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 12207 * set such that scsi_vhci can use other available path for 12208 * retry. Besides, ucsci command does not allow DMA breakup, 12209 * so there is no need to set PKT_DMA_PARTIAL flag. 12210 */ 12211 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 12212 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 12213 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 12214 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status) 12215 - sizeof (struct scsi_extended_sense)), 0, 12216 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ, 12217 sdrunout, (caddr_t)un); 12218 } else { 12219 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 12220 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 12221 sizeof (struct scsi_arq_status), 0, 12222 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 12223 sdrunout, (caddr_t)un); 12224 } 12225 12226 if (pktp == NULL) { 12227 *pktpp = NULL; 12228 /* 12229 * Set the driver state to RWAIT to indicate the driver 12230 * is waiting on resource allocations. The driver will not 12231 * suspend, pm_suspend, or detatch while the state is RWAIT. 12232 */ 12233 New_state(un, SD_STATE_RWAIT); 12234 12235 SD_ERROR(SD_LOG_IO_CORE, un, 12236 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 12237 12238 if ((bp->b_flags & B_ERROR) != 0) { 12239 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 12240 } 12241 return (SD_PKT_ALLOC_FAILURE); 12242 } 12243 12244 /* 12245 * We do not do DMA breakup for USCSI commands, so return failure 12246 * here if all the needed DMA resources were not allocated. 12247 */ 12248 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 12249 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 12250 scsi_destroy_pkt(pktp); 12251 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 12252 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 12253 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 12254 } 12255 12256 /* Init the cdb from the given uscsi struct */ 12257 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 12258 uscmd->uscsi_cdb[0], 0, 0, 0); 12259 12260 SD_FILL_SCSI1_LUN(un, pktp); 12261 12262 /* 12263 * Set up the optional USCSI flags. See the uscsi (7I) man page 12264 * for listing of the supported flags. 12265 */ 12266 12267 if (uscmd->uscsi_flags & USCSI_SILENT) { 12268 flags |= FLAG_SILENT; 12269 } 12270 12271 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 12272 flags |= FLAG_DIAGNOSE; 12273 } 12274 12275 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 12276 flags |= FLAG_ISOLATE; 12277 } 12278 12279 if (un->un_f_is_fibre == FALSE) { 12280 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 12281 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 12282 } 12283 } 12284 12285 /* 12286 * Set the pkt flags here so we save time later. 12287 * Note: These flags are NOT in the uscsi man page!!! 12288 */ 12289 if (uscmd->uscsi_flags & USCSI_HEAD) { 12290 flags |= FLAG_HEAD; 12291 } 12292 12293 if (uscmd->uscsi_flags & USCSI_NOINTR) { 12294 flags |= FLAG_NOINTR; 12295 } 12296 12297 /* 12298 * For tagged queueing, things get a bit complicated. 12299 * Check first for head of queue and last for ordered queue. 12300 * If neither head nor order, use the default driver tag flags. 12301 */ 12302 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 12303 if (uscmd->uscsi_flags & USCSI_HTAG) { 12304 flags |= FLAG_HTAG; 12305 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 12306 flags |= FLAG_OTAG; 12307 } else { 12308 flags |= un->un_tagflags & FLAG_TAGMASK; 12309 } 12310 } 12311 12312 if (uscmd->uscsi_flags & USCSI_NODISCON) { 12313 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 12314 } 12315 12316 pktp->pkt_flags = flags; 12317 12318 /* Copy the caller's CDB into the pkt... */ 12319 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 12320 12321 if (uscmd->uscsi_timeout == 0) { 12322 pktp->pkt_time = un->un_uscsi_timeout; 12323 } else { 12324 pktp->pkt_time = uscmd->uscsi_timeout; 12325 } 12326 12327 /* need it later to identify USCSI request in sdintr */ 12328 xp->xb_pkt_flags |= SD_XB_USCSICMD; 12329 12330 xp->xb_sense_resid = uscmd->uscsi_rqresid; 12331 12332 pktp->pkt_private = bp; 12333 pktp->pkt_comp = sdintr; 12334 *pktpp = pktp; 12335 12336 SD_TRACE(SD_LOG_IO_CORE, un, 12337 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 12338 12339 return (SD_PKT_ALLOC_SUCCESS); 12340 } 12341 12342 12343 /* 12344 * Function: sd_destroypkt_for_uscsi 12345 * 12346 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 12347 * IOs.. Also saves relevant info into the associated uscsi_cmd 12348 * struct. 12349 * 12350 * Context: May be called under interrupt context 12351 */ 12352 12353 static void 12354 sd_destroypkt_for_uscsi(struct buf *bp) 12355 { 12356 struct uscsi_cmd *uscmd; 12357 struct sd_xbuf *xp; 12358 struct scsi_pkt *pktp; 12359 struct sd_lun *un; 12360 12361 ASSERT(bp != NULL); 12362 xp = SD_GET_XBUF(bp); 12363 ASSERT(xp != NULL); 12364 un = SD_GET_UN(bp); 12365 ASSERT(un != NULL); 12366 ASSERT(!mutex_owned(SD_MUTEX(un))); 12367 pktp = SD_GET_PKTP(bp); 12368 ASSERT(pktp != NULL); 12369 12370 SD_TRACE(SD_LOG_IO_CORE, un, 12371 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 12372 12373 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 12374 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 12375 ASSERT(uscmd != NULL); 12376 12377 /* Save the status and the residual into the uscsi_cmd struct */ 12378 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 12379 uscmd->uscsi_resid = bp->b_resid; 12380 12381 /* 12382 * If enabled, copy any saved sense data into the area specified 12383 * by the uscsi command. 12384 */ 12385 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12386 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12387 /* 12388 * Note: uscmd->uscsi_rqbuf should always point to a buffer 12389 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 12390 */ 12391 uscmd->uscsi_rqstatus = xp->xb_sense_status; 12392 uscmd->uscsi_rqresid = xp->xb_sense_resid; 12393 if (uscmd->uscsi_rqlen > SENSE_LENGTH) { 12394 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 12395 MAX_SENSE_LENGTH); 12396 } else { 12397 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, 12398 SENSE_LENGTH); 12399 } 12400 } 12401 12402 /* We are done with the scsi_pkt; free it now */ 12403 ASSERT(SD_GET_PKTP(bp) != NULL); 12404 scsi_destroy_pkt(SD_GET_PKTP(bp)); 12405 12406 SD_TRACE(SD_LOG_IO_CORE, un, 12407 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 12408 } 12409 12410 12411 /* 12412 * Function: sd_bioclone_alloc 12413 * 12414 * Description: Allocate a buf(9S) and init it as per the given buf 12415 * and the various arguments. The associated sd_xbuf 12416 * struct is (nearly) duplicated. The struct buf *bp 12417 * argument is saved in new_xp->xb_private. 12418 * 12419 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 12420 * datalen - size of data area for the shadow bp 12421 * blkno - starting LBA 12422 * func - function pointer for b_iodone in the shadow buf. (May 12423 * be NULL if none.) 12424 * 12425 * Return Code: Pointer to allocates buf(9S) struct 12426 * 12427 * Context: Can sleep. 12428 */ 12429 12430 static struct buf * 12431 sd_bioclone_alloc(struct buf *bp, size_t datalen, 12432 daddr_t blkno, int (*func)(struct buf *)) 12433 { 12434 struct sd_lun *un; 12435 struct sd_xbuf *xp; 12436 struct sd_xbuf *new_xp; 12437 struct buf *new_bp; 12438 12439 ASSERT(bp != NULL); 12440 xp = SD_GET_XBUF(bp); 12441 ASSERT(xp != NULL); 12442 un = SD_GET_UN(bp); 12443 ASSERT(un != NULL); 12444 ASSERT(!mutex_owned(SD_MUTEX(un))); 12445 12446 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 12447 NULL, KM_SLEEP); 12448 12449 new_bp->b_lblkno = blkno; 12450 12451 /* 12452 * Allocate an xbuf for the shadow bp and copy the contents of the 12453 * original xbuf into it. 12454 */ 12455 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12456 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 12457 12458 /* 12459 * The given bp is automatically saved in the xb_private member 12460 * of the new xbuf. Callers are allowed to depend on this. 12461 */ 12462 new_xp->xb_private = bp; 12463 12464 new_bp->b_private = new_xp; 12465 12466 return (new_bp); 12467 } 12468 12469 /* 12470 * Function: sd_shadow_buf_alloc 12471 * 12472 * Description: Allocate a buf(9S) and init it as per the given buf 12473 * and the various arguments. The associated sd_xbuf 12474 * struct is (nearly) duplicated. The struct buf *bp 12475 * argument is saved in new_xp->xb_private. 12476 * 12477 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 12478 * datalen - size of data area for the shadow bp 12479 * bflags - B_READ or B_WRITE (pseudo flag) 12480 * blkno - starting LBA 12481 * func - function pointer for b_iodone in the shadow buf. (May 12482 * be NULL if none.) 12483 * 12484 * Return Code: Pointer to allocates buf(9S) struct 12485 * 12486 * Context: Can sleep. 12487 */ 12488 12489 static struct buf * 12490 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 12491 daddr_t blkno, int (*func)(struct buf *)) 12492 { 12493 struct sd_lun *un; 12494 struct sd_xbuf *xp; 12495 struct sd_xbuf *new_xp; 12496 struct buf *new_bp; 12497 12498 ASSERT(bp != NULL); 12499 xp = SD_GET_XBUF(bp); 12500 ASSERT(xp != NULL); 12501 un = SD_GET_UN(bp); 12502 ASSERT(un != NULL); 12503 ASSERT(!mutex_owned(SD_MUTEX(un))); 12504 12505 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 12506 bp_mapin(bp); 12507 } 12508 12509 bflags &= (B_READ | B_WRITE); 12510 #if defined(__i386) || defined(__amd64) 12511 new_bp = getrbuf(KM_SLEEP); 12512 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 12513 new_bp->b_bcount = datalen; 12514 new_bp->b_flags = bflags | 12515 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 12516 #else 12517 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 12518 datalen, bflags, SLEEP_FUNC, NULL); 12519 #endif 12520 new_bp->av_forw = NULL; 12521 new_bp->av_back = NULL; 12522 new_bp->b_dev = bp->b_dev; 12523 new_bp->b_blkno = blkno; 12524 new_bp->b_iodone = func; 12525 new_bp->b_edev = bp->b_edev; 12526 new_bp->b_resid = 0; 12527 12528 /* We need to preserve the B_FAILFAST flag */ 12529 if (bp->b_flags & B_FAILFAST) { 12530 new_bp->b_flags |= B_FAILFAST; 12531 } 12532 12533 /* 12534 * Allocate an xbuf for the shadow bp and copy the contents of the 12535 * original xbuf into it. 12536 */ 12537 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12538 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 12539 12540 /* Need later to copy data between the shadow buf & original buf! */ 12541 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 12542 12543 /* 12544 * The given bp is automatically saved in the xb_private member 12545 * of the new xbuf. Callers are allowed to depend on this. 12546 */ 12547 new_xp->xb_private = bp; 12548 12549 new_bp->b_private = new_xp; 12550 12551 return (new_bp); 12552 } 12553 12554 /* 12555 * Function: sd_bioclone_free 12556 * 12557 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 12558 * in the larger than partition operation. 12559 * 12560 * Context: May be called under interrupt context 12561 */ 12562 12563 static void 12564 sd_bioclone_free(struct buf *bp) 12565 { 12566 struct sd_xbuf *xp; 12567 12568 ASSERT(bp != NULL); 12569 xp = SD_GET_XBUF(bp); 12570 ASSERT(xp != NULL); 12571 12572 /* 12573 * Call bp_mapout() before freeing the buf, in case a lower 12574 * layer or HBA had done a bp_mapin(). we must do this here 12575 * as we are the "originator" of the shadow buf. 12576 */ 12577 bp_mapout(bp); 12578 12579 /* 12580 * Null out b_iodone before freeing the bp, to ensure that the driver 12581 * never gets confused by a stale value in this field. (Just a little 12582 * extra defensiveness here.) 12583 */ 12584 bp->b_iodone = NULL; 12585 12586 freerbuf(bp); 12587 12588 kmem_free(xp, sizeof (struct sd_xbuf)); 12589 } 12590 12591 /* 12592 * Function: sd_shadow_buf_free 12593 * 12594 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 12595 * 12596 * Context: May be called under interrupt context 12597 */ 12598 12599 static void 12600 sd_shadow_buf_free(struct buf *bp) 12601 { 12602 struct sd_xbuf *xp; 12603 12604 ASSERT(bp != NULL); 12605 xp = SD_GET_XBUF(bp); 12606 ASSERT(xp != NULL); 12607 12608 #if defined(__sparc) 12609 /* 12610 * Call bp_mapout() before freeing the buf, in case a lower 12611 * layer or HBA had done a bp_mapin(). we must do this here 12612 * as we are the "originator" of the shadow buf. 12613 */ 12614 bp_mapout(bp); 12615 #endif 12616 12617 /* 12618 * Null out b_iodone before freeing the bp, to ensure that the driver 12619 * never gets confused by a stale value in this field. (Just a little 12620 * extra defensiveness here.) 12621 */ 12622 bp->b_iodone = NULL; 12623 12624 #if defined(__i386) || defined(__amd64) 12625 kmem_free(bp->b_un.b_addr, bp->b_bcount); 12626 freerbuf(bp); 12627 #else 12628 scsi_free_consistent_buf(bp); 12629 #endif 12630 12631 kmem_free(xp, sizeof (struct sd_xbuf)); 12632 } 12633 12634 12635 /* 12636 * Function: sd_print_transport_rejected_message 12637 * 12638 * Description: This implements the ludicrously complex rules for printing 12639 * a "transport rejected" message. This is to address the 12640 * specific problem of having a flood of this error message 12641 * produced when a failover occurs. 12642 * 12643 * Context: Any. 12644 */ 12645 12646 static void 12647 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 12648 int code) 12649 { 12650 ASSERT(un != NULL); 12651 ASSERT(mutex_owned(SD_MUTEX(un))); 12652 ASSERT(xp != NULL); 12653 12654 /* 12655 * Print the "transport rejected" message under the following 12656 * conditions: 12657 * 12658 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 12659 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 12660 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 12661 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 12662 * scsi_transport(9F) (which indicates that the target might have 12663 * gone off-line). This uses the un->un_tran_fatal_count 12664 * count, which is incremented whenever a TRAN_FATAL_ERROR is 12665 * received, and reset to zero whenver a TRAN_ACCEPT is returned 12666 * from scsi_transport(). 12667 * 12668 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 12669 * the preceeding cases in order for the message to be printed. 12670 */ 12671 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 12672 if ((sd_level_mask & SD_LOGMASK_DIAG) || 12673 (code != TRAN_FATAL_ERROR) || 12674 (un->un_tran_fatal_count == 1)) { 12675 switch (code) { 12676 case TRAN_BADPKT: 12677 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12678 "transport rejected bad packet\n"); 12679 break; 12680 case TRAN_FATAL_ERROR: 12681 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12682 "transport rejected fatal error\n"); 12683 break; 12684 default: 12685 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12686 "transport rejected (%d)\n", code); 12687 break; 12688 } 12689 } 12690 } 12691 } 12692 12693 12694 /* 12695 * Function: sd_add_buf_to_waitq 12696 * 12697 * Description: Add the given buf(9S) struct to the wait queue for the 12698 * instance. If sorting is enabled, then the buf is added 12699 * to the queue via an elevator sort algorithm (a la 12700 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 12701 * If sorting is not enabled, then the buf is just added 12702 * to the end of the wait queue. 12703 * 12704 * Return Code: void 12705 * 12706 * Context: Does not sleep/block, therefore technically can be called 12707 * from any context. However if sorting is enabled then the 12708 * execution time is indeterminate, and may take long if 12709 * the wait queue grows large. 12710 */ 12711 12712 static void 12713 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 12714 { 12715 struct buf *ap; 12716 12717 ASSERT(bp != NULL); 12718 ASSERT(un != NULL); 12719 ASSERT(mutex_owned(SD_MUTEX(un))); 12720 12721 /* If the queue is empty, add the buf as the only entry & return. */ 12722 if (un->un_waitq_headp == NULL) { 12723 ASSERT(un->un_waitq_tailp == NULL); 12724 un->un_waitq_headp = un->un_waitq_tailp = bp; 12725 bp->av_forw = NULL; 12726 return; 12727 } 12728 12729 ASSERT(un->un_waitq_tailp != NULL); 12730 12731 /* 12732 * If sorting is disabled, just add the buf to the tail end of 12733 * the wait queue and return. 12734 */ 12735 if (un->un_f_disksort_disabled) { 12736 un->un_waitq_tailp->av_forw = bp; 12737 un->un_waitq_tailp = bp; 12738 bp->av_forw = NULL; 12739 return; 12740 } 12741 12742 /* 12743 * Sort thru the list of requests currently on the wait queue 12744 * and add the new buf request at the appropriate position. 12745 * 12746 * The un->un_waitq_headp is an activity chain pointer on which 12747 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 12748 * first queue holds those requests which are positioned after 12749 * the current SD_GET_BLKNO() (in the first request); the second holds 12750 * requests which came in after their SD_GET_BLKNO() number was passed. 12751 * Thus we implement a one way scan, retracting after reaching 12752 * the end of the drive to the first request on the second 12753 * queue, at which time it becomes the first queue. 12754 * A one-way scan is natural because of the way UNIX read-ahead 12755 * blocks are allocated. 12756 * 12757 * If we lie after the first request, then we must locate the 12758 * second request list and add ourselves to it. 12759 */ 12760 ap = un->un_waitq_headp; 12761 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 12762 while (ap->av_forw != NULL) { 12763 /* 12764 * Look for an "inversion" in the (normally 12765 * ascending) block numbers. This indicates 12766 * the start of the second request list. 12767 */ 12768 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 12769 /* 12770 * Search the second request list for the 12771 * first request at a larger block number. 12772 * We go before that; however if there is 12773 * no such request, we go at the end. 12774 */ 12775 do { 12776 if (SD_GET_BLKNO(bp) < 12777 SD_GET_BLKNO(ap->av_forw)) { 12778 goto insert; 12779 } 12780 ap = ap->av_forw; 12781 } while (ap->av_forw != NULL); 12782 goto insert; /* after last */ 12783 } 12784 ap = ap->av_forw; 12785 } 12786 12787 /* 12788 * No inversions... we will go after the last, and 12789 * be the first request in the second request list. 12790 */ 12791 goto insert; 12792 } 12793 12794 /* 12795 * Request is at/after the current request... 12796 * sort in the first request list. 12797 */ 12798 while (ap->av_forw != NULL) { 12799 /* 12800 * We want to go after the current request (1) if 12801 * there is an inversion after it (i.e. it is the end 12802 * of the first request list), or (2) if the next 12803 * request is a larger block no. than our request. 12804 */ 12805 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 12806 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 12807 goto insert; 12808 } 12809 ap = ap->av_forw; 12810 } 12811 12812 /* 12813 * Neither a second list nor a larger request, therefore 12814 * we go at the end of the first list (which is the same 12815 * as the end of the whole schebang). 12816 */ 12817 insert: 12818 bp->av_forw = ap->av_forw; 12819 ap->av_forw = bp; 12820 12821 /* 12822 * If we inserted onto the tail end of the waitq, make sure the 12823 * tail pointer is updated. 12824 */ 12825 if (ap == un->un_waitq_tailp) { 12826 un->un_waitq_tailp = bp; 12827 } 12828 } 12829 12830 12831 /* 12832 * Function: sd_start_cmds 12833 * 12834 * Description: Remove and transport cmds from the driver queues. 12835 * 12836 * Arguments: un - pointer to the unit (soft state) struct for the target. 12837 * 12838 * immed_bp - ptr to a buf to be transported immediately. Only 12839 * the immed_bp is transported; bufs on the waitq are not 12840 * processed and the un_retry_bp is not checked. If immed_bp is 12841 * NULL, then normal queue processing is performed. 12842 * 12843 * Context: May be called from kernel thread context, interrupt context, 12844 * or runout callback context. This function may not block or 12845 * call routines that block. 12846 */ 12847 12848 static void 12849 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 12850 { 12851 struct sd_xbuf *xp; 12852 struct buf *bp; 12853 void (*statp)(kstat_io_t *); 12854 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12855 void (*saved_statp)(kstat_io_t *); 12856 #endif 12857 int rval; 12858 12859 ASSERT(un != NULL); 12860 ASSERT(mutex_owned(SD_MUTEX(un))); 12861 ASSERT(un->un_ncmds_in_transport >= 0); 12862 ASSERT(un->un_throttle >= 0); 12863 12864 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 12865 12866 do { 12867 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12868 saved_statp = NULL; 12869 #endif 12870 12871 /* 12872 * If we are syncing or dumping, fail the command to 12873 * avoid recursively calling back into scsi_transport(). 12874 * The dump I/O itself uses a separate code path so this 12875 * only prevents non-dump I/O from being sent while dumping. 12876 * File system sync takes place before dumping begins. 12877 * During panic, filesystem I/O is allowed provided 12878 * un_in_callback is <= 1. This is to prevent recursion 12879 * such as sd_start_cmds -> scsi_transport -> sdintr -> 12880 * sd_start_cmds and so on. See panic.c for more information 12881 * about the states the system can be in during panic. 12882 */ 12883 if ((un->un_state == SD_STATE_DUMPING) || 12884 (ddi_in_panic() && (un->un_in_callback > 1))) { 12885 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12886 "sd_start_cmds: panicking\n"); 12887 goto exit; 12888 } 12889 12890 if ((bp = immed_bp) != NULL) { 12891 /* 12892 * We have a bp that must be transported immediately. 12893 * It's OK to transport the immed_bp here without doing 12894 * the throttle limit check because the immed_bp is 12895 * always used in a retry/recovery case. This means 12896 * that we know we are not at the throttle limit by 12897 * virtue of the fact that to get here we must have 12898 * already gotten a command back via sdintr(). This also 12899 * relies on (1) the command on un_retry_bp preventing 12900 * further commands from the waitq from being issued; 12901 * and (2) the code in sd_retry_command checking the 12902 * throttle limit before issuing a delayed or immediate 12903 * retry. This holds even if the throttle limit is 12904 * currently ratcheted down from its maximum value. 12905 */ 12906 statp = kstat_runq_enter; 12907 if (bp == un->un_retry_bp) { 12908 ASSERT((un->un_retry_statp == NULL) || 12909 (un->un_retry_statp == kstat_waitq_enter) || 12910 (un->un_retry_statp == 12911 kstat_runq_back_to_waitq)); 12912 /* 12913 * If the waitq kstat was incremented when 12914 * sd_set_retry_bp() queued this bp for a retry, 12915 * then we must set up statp so that the waitq 12916 * count will get decremented correctly below. 12917 * Also we must clear un->un_retry_statp to 12918 * ensure that we do not act on a stale value 12919 * in this field. 12920 */ 12921 if ((un->un_retry_statp == kstat_waitq_enter) || 12922 (un->un_retry_statp == 12923 kstat_runq_back_to_waitq)) { 12924 statp = kstat_waitq_to_runq; 12925 } 12926 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 12927 saved_statp = un->un_retry_statp; 12928 #endif 12929 un->un_retry_statp = NULL; 12930 12931 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 12932 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 12933 "un_throttle:%d un_ncmds_in_transport:%d\n", 12934 un, un->un_retry_bp, un->un_throttle, 12935 un->un_ncmds_in_transport); 12936 } else { 12937 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 12938 "processing priority bp:0x%p\n", bp); 12939 } 12940 12941 } else if ((bp = un->un_waitq_headp) != NULL) { 12942 /* 12943 * A command on the waitq is ready to go, but do not 12944 * send it if: 12945 * 12946 * (1) the throttle limit has been reached, or 12947 * (2) a retry is pending, or 12948 * (3) a START_STOP_UNIT callback pending, or 12949 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 12950 * command is pending. 12951 * 12952 * For all of these conditions, IO processing will 12953 * restart after the condition is cleared. 12954 */ 12955 if (un->un_ncmds_in_transport >= un->un_throttle) { 12956 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12957 "sd_start_cmds: exiting, " 12958 "throttle limit reached!\n"); 12959 goto exit; 12960 } 12961 if (un->un_retry_bp != NULL) { 12962 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12963 "sd_start_cmds: exiting, retry pending!\n"); 12964 goto exit; 12965 } 12966 if (un->un_startstop_timeid != NULL) { 12967 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12968 "sd_start_cmds: exiting, " 12969 "START_STOP pending!\n"); 12970 goto exit; 12971 } 12972 if (un->un_direct_priority_timeid != NULL) { 12973 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 12974 "sd_start_cmds: exiting, " 12975 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 12976 goto exit; 12977 } 12978 12979 /* Dequeue the command */ 12980 un->un_waitq_headp = bp->av_forw; 12981 if (un->un_waitq_headp == NULL) { 12982 un->un_waitq_tailp = NULL; 12983 } 12984 bp->av_forw = NULL; 12985 statp = kstat_waitq_to_runq; 12986 SD_TRACE(SD_LOG_IO_CORE, un, 12987 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 12988 12989 } else { 12990 /* No work to do so bail out now */ 12991 SD_TRACE(SD_LOG_IO_CORE, un, 12992 "sd_start_cmds: no more work, exiting!\n"); 12993 goto exit; 12994 } 12995 12996 /* 12997 * Reset the state to normal. This is the mechanism by which 12998 * the state transitions from either SD_STATE_RWAIT or 12999 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 13000 * If state is SD_STATE_PM_CHANGING then this command is 13001 * part of the device power control and the state must 13002 * not be put back to normal. Doing so would would 13003 * allow new commands to proceed when they shouldn't, 13004 * the device may be going off. 13005 */ 13006 if ((un->un_state != SD_STATE_SUSPENDED) && 13007 (un->un_state != SD_STATE_PM_CHANGING)) { 13008 New_state(un, SD_STATE_NORMAL); 13009 } 13010 13011 xp = SD_GET_XBUF(bp); 13012 ASSERT(xp != NULL); 13013 13014 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13015 /* 13016 * Allocate the scsi_pkt if we need one, or attach DMA 13017 * resources if we have a scsi_pkt that needs them. The 13018 * latter should only occur for commands that are being 13019 * retried. 13020 */ 13021 if ((xp->xb_pktp == NULL) || 13022 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 13023 #else 13024 if (xp->xb_pktp == NULL) { 13025 #endif 13026 /* 13027 * There is no scsi_pkt allocated for this buf. Call 13028 * the initpkt function to allocate & init one. 13029 * 13030 * The scsi_init_pkt runout callback functionality is 13031 * implemented as follows: 13032 * 13033 * 1) The initpkt function always calls 13034 * scsi_init_pkt(9F) with sdrunout specified as the 13035 * callback routine. 13036 * 2) A successful packet allocation is initialized and 13037 * the I/O is transported. 13038 * 3) The I/O associated with an allocation resource 13039 * failure is left on its queue to be retried via 13040 * runout or the next I/O. 13041 * 4) The I/O associated with a DMA error is removed 13042 * from the queue and failed with EIO. Processing of 13043 * the transport queues is also halted to be 13044 * restarted via runout or the next I/O. 13045 * 5) The I/O associated with a CDB size or packet 13046 * size error is removed from the queue and failed 13047 * with EIO. Processing of the transport queues is 13048 * continued. 13049 * 13050 * Note: there is no interface for canceling a runout 13051 * callback. To prevent the driver from detaching or 13052 * suspending while a runout is pending the driver 13053 * state is set to SD_STATE_RWAIT 13054 * 13055 * Note: using the scsi_init_pkt callback facility can 13056 * result in an I/O request persisting at the head of 13057 * the list which cannot be satisfied even after 13058 * multiple retries. In the future the driver may 13059 * implement some kind of maximum runout count before 13060 * failing an I/O. 13061 * 13062 * Note: the use of funcp below may seem superfluous, 13063 * but it helps warlock figure out the correct 13064 * initpkt function calls (see [s]sd.wlcmd). 13065 */ 13066 struct scsi_pkt *pktp; 13067 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 13068 13069 ASSERT(bp != un->un_rqs_bp); 13070 13071 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 13072 switch ((*funcp)(bp, &pktp)) { 13073 case SD_PKT_ALLOC_SUCCESS: 13074 xp->xb_pktp = pktp; 13075 SD_TRACE(SD_LOG_IO_CORE, un, 13076 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 13077 pktp); 13078 goto got_pkt; 13079 13080 case SD_PKT_ALLOC_FAILURE: 13081 /* 13082 * Temporary (hopefully) resource depletion. 13083 * Since retries and RQS commands always have a 13084 * scsi_pkt allocated, these cases should never 13085 * get here. So the only cases this needs to 13086 * handle is a bp from the waitq (which we put 13087 * back onto the waitq for sdrunout), or a bp 13088 * sent as an immed_bp (which we just fail). 13089 */ 13090 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13091 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 13092 13093 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13094 13095 if (bp == immed_bp) { 13096 /* 13097 * If SD_XB_DMA_FREED is clear, then 13098 * this is a failure to allocate a 13099 * scsi_pkt, and we must fail the 13100 * command. 13101 */ 13102 if ((xp->xb_pkt_flags & 13103 SD_XB_DMA_FREED) == 0) { 13104 break; 13105 } 13106 13107 /* 13108 * If this immediate command is NOT our 13109 * un_retry_bp, then we must fail it. 13110 */ 13111 if (bp != un->un_retry_bp) { 13112 break; 13113 } 13114 13115 /* 13116 * We get here if this cmd is our 13117 * un_retry_bp that was DMAFREED, but 13118 * scsi_init_pkt() failed to reallocate 13119 * DMA resources when we attempted to 13120 * retry it. This can happen when an 13121 * mpxio failover is in progress, but 13122 * we don't want to just fail the 13123 * command in this case. 13124 * 13125 * Use timeout(9F) to restart it after 13126 * a 100ms delay. We don't want to 13127 * let sdrunout() restart it, because 13128 * sdrunout() is just supposed to start 13129 * commands that are sitting on the 13130 * wait queue. The un_retry_bp stays 13131 * set until the command completes, but 13132 * sdrunout can be called many times 13133 * before that happens. Since sdrunout 13134 * cannot tell if the un_retry_bp is 13135 * already in the transport, it could 13136 * end up calling scsi_transport() for 13137 * the un_retry_bp multiple times. 13138 * 13139 * Also: don't schedule the callback 13140 * if some other callback is already 13141 * pending. 13142 */ 13143 if (un->un_retry_statp == NULL) { 13144 /* 13145 * restore the kstat pointer to 13146 * keep kstat counts coherent 13147 * when we do retry the command. 13148 */ 13149 un->un_retry_statp = 13150 saved_statp; 13151 } 13152 13153 if ((un->un_startstop_timeid == NULL) && 13154 (un->un_retry_timeid == NULL) && 13155 (un->un_direct_priority_timeid == 13156 NULL)) { 13157 13158 un->un_retry_timeid = 13159 timeout( 13160 sd_start_retry_command, 13161 un, SD_RESTART_TIMEOUT); 13162 } 13163 goto exit; 13164 } 13165 13166 #else 13167 if (bp == immed_bp) { 13168 break; /* Just fail the command */ 13169 } 13170 #endif 13171 13172 /* Add the buf back to the head of the waitq */ 13173 bp->av_forw = un->un_waitq_headp; 13174 un->un_waitq_headp = bp; 13175 if (un->un_waitq_tailp == NULL) { 13176 un->un_waitq_tailp = bp; 13177 } 13178 goto exit; 13179 13180 case SD_PKT_ALLOC_FAILURE_NO_DMA: 13181 /* 13182 * HBA DMA resource failure. Fail the command 13183 * and continue processing of the queues. 13184 */ 13185 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13186 "sd_start_cmds: " 13187 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 13188 break; 13189 13190 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 13191 /* 13192 * Note:x86: Partial DMA mapping not supported 13193 * for USCSI commands, and all the needed DMA 13194 * resources were not allocated. 13195 */ 13196 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13197 "sd_start_cmds: " 13198 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 13199 break; 13200 13201 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 13202 /* 13203 * Note:x86: Request cannot fit into CDB based 13204 * on lba and len. 13205 */ 13206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13207 "sd_start_cmds: " 13208 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 13209 break; 13210 13211 default: 13212 /* Should NEVER get here! */ 13213 panic("scsi_initpkt error"); 13214 /*NOTREACHED*/ 13215 } 13216 13217 /* 13218 * Fatal error in allocating a scsi_pkt for this buf. 13219 * Update kstats & return the buf with an error code. 13220 * We must use sd_return_failed_command_no_restart() to 13221 * avoid a recursive call back into sd_start_cmds(). 13222 * However this also means that we must keep processing 13223 * the waitq here in order to avoid stalling. 13224 */ 13225 if (statp == kstat_waitq_to_runq) { 13226 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 13227 } 13228 sd_return_failed_command_no_restart(un, bp, EIO); 13229 if (bp == immed_bp) { 13230 /* immed_bp is gone by now, so clear this */ 13231 immed_bp = NULL; 13232 } 13233 continue; 13234 } 13235 got_pkt: 13236 if (bp == immed_bp) { 13237 /* goto the head of the class.... */ 13238 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 13239 } 13240 13241 un->un_ncmds_in_transport++; 13242 SD_UPDATE_KSTATS(un, statp, bp); 13243 13244 /* 13245 * Call scsi_transport() to send the command to the target. 13246 * According to SCSA architecture, we must drop the mutex here 13247 * before calling scsi_transport() in order to avoid deadlock. 13248 * Note that the scsi_pkt's completion routine can be executed 13249 * (from interrupt context) even before the call to 13250 * scsi_transport() returns. 13251 */ 13252 SD_TRACE(SD_LOG_IO_CORE, un, 13253 "sd_start_cmds: calling scsi_transport()\n"); 13254 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 13255 13256 mutex_exit(SD_MUTEX(un)); 13257 rval = scsi_transport(xp->xb_pktp); 13258 mutex_enter(SD_MUTEX(un)); 13259 13260 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13261 "sd_start_cmds: scsi_transport() returned %d\n", rval); 13262 13263 switch (rval) { 13264 case TRAN_ACCEPT: 13265 /* Clear this with every pkt accepted by the HBA */ 13266 un->un_tran_fatal_count = 0; 13267 break; /* Success; try the next cmd (if any) */ 13268 13269 case TRAN_BUSY: 13270 un->un_ncmds_in_transport--; 13271 ASSERT(un->un_ncmds_in_transport >= 0); 13272 13273 /* 13274 * Don't retry request sense, the sense data 13275 * is lost when another request is sent. 13276 * Free up the rqs buf and retry 13277 * the original failed cmd. Update kstat. 13278 */ 13279 if (bp == un->un_rqs_bp) { 13280 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13281 bp = sd_mark_rqs_idle(un, xp); 13282 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 13283 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 13284 kstat_waitq_enter); 13285 goto exit; 13286 } 13287 13288 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13289 /* 13290 * Free the DMA resources for the scsi_pkt. This will 13291 * allow mpxio to select another path the next time 13292 * we call scsi_transport() with this scsi_pkt. 13293 * See sdintr() for the rationalization behind this. 13294 */ 13295 if ((un->un_f_is_fibre == TRUE) && 13296 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 13297 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 13298 scsi_dmafree(xp->xb_pktp); 13299 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 13300 } 13301 #endif 13302 13303 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 13304 /* 13305 * Commands that are SD_PATH_DIRECT_PRIORITY 13306 * are for error recovery situations. These do 13307 * not use the normal command waitq, so if they 13308 * get a TRAN_BUSY we cannot put them back onto 13309 * the waitq for later retry. One possible 13310 * problem is that there could already be some 13311 * other command on un_retry_bp that is waiting 13312 * for this one to complete, so we would be 13313 * deadlocked if we put this command back onto 13314 * the waitq for later retry (since un_retry_bp 13315 * must complete before the driver gets back to 13316 * commands on the waitq). 13317 * 13318 * To avoid deadlock we must schedule a callback 13319 * that will restart this command after a set 13320 * interval. This should keep retrying for as 13321 * long as the underlying transport keeps 13322 * returning TRAN_BUSY (just like for other 13323 * commands). Use the same timeout interval as 13324 * for the ordinary TRAN_BUSY retry. 13325 */ 13326 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13327 "sd_start_cmds: scsi_transport() returned " 13328 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 13329 13330 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13331 un->un_direct_priority_timeid = 13332 timeout(sd_start_direct_priority_command, 13333 bp, SD_BSY_TIMEOUT / 500); 13334 13335 goto exit; 13336 } 13337 13338 /* 13339 * For TRAN_BUSY, we want to reduce the throttle value, 13340 * unless we are retrying a command. 13341 */ 13342 if (bp != un->un_retry_bp) { 13343 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 13344 } 13345 13346 /* 13347 * Set up the bp to be tried again 10 ms later. 13348 * Note:x86: Is there a timeout value in the sd_lun 13349 * for this condition? 13350 */ 13351 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 13352 kstat_runq_back_to_waitq); 13353 goto exit; 13354 13355 case TRAN_FATAL_ERROR: 13356 un->un_tran_fatal_count++; 13357 /* FALLTHRU */ 13358 13359 case TRAN_BADPKT: 13360 default: 13361 un->un_ncmds_in_transport--; 13362 ASSERT(un->un_ncmds_in_transport >= 0); 13363 13364 /* 13365 * If this is our REQUEST SENSE command with a 13366 * transport error, we must get back the pointers 13367 * to the original buf, and mark the REQUEST 13368 * SENSE command as "available". 13369 */ 13370 if (bp == un->un_rqs_bp) { 13371 bp = sd_mark_rqs_idle(un, xp); 13372 xp = SD_GET_XBUF(bp); 13373 } else { 13374 /* 13375 * Legacy behavior: do not update transport 13376 * error count for request sense commands. 13377 */ 13378 SD_UPDATE_ERRSTATS(un, sd_transerrs); 13379 } 13380 13381 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 13382 sd_print_transport_rejected_message(un, xp, rval); 13383 13384 /* 13385 * We must use sd_return_failed_command_no_restart() to 13386 * avoid a recursive call back into sd_start_cmds(). 13387 * However this also means that we must keep processing 13388 * the waitq here in order to avoid stalling. 13389 */ 13390 sd_return_failed_command_no_restart(un, bp, EIO); 13391 13392 /* 13393 * Notify any threads waiting in sd_ddi_suspend() that 13394 * a command completion has occurred. 13395 */ 13396 if (un->un_state == SD_STATE_SUSPENDED) { 13397 cv_broadcast(&un->un_disk_busy_cv); 13398 } 13399 13400 if (bp == immed_bp) { 13401 /* immed_bp is gone by now, so clear this */ 13402 immed_bp = NULL; 13403 } 13404 break; 13405 } 13406 13407 } while (immed_bp == NULL); 13408 13409 exit: 13410 ASSERT(mutex_owned(SD_MUTEX(un))); 13411 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 13412 } 13413 13414 13415 /* 13416 * Function: sd_return_command 13417 * 13418 * Description: Returns a command to its originator (with or without an 13419 * error). Also starts commands waiting to be transported 13420 * to the target. 13421 * 13422 * Context: May be called from interrupt, kernel, or timeout context 13423 */ 13424 13425 static void 13426 sd_return_command(struct sd_lun *un, struct buf *bp) 13427 { 13428 struct sd_xbuf *xp; 13429 struct scsi_pkt *pktp; 13430 13431 ASSERT(bp != NULL); 13432 ASSERT(un != NULL); 13433 ASSERT(mutex_owned(SD_MUTEX(un))); 13434 ASSERT(bp != un->un_rqs_bp); 13435 xp = SD_GET_XBUF(bp); 13436 ASSERT(xp != NULL); 13437 13438 pktp = SD_GET_PKTP(bp); 13439 13440 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 13441 13442 /* 13443 * Note: check for the "sdrestart failed" case. 13444 */ 13445 if ((un->un_partial_dma_supported == 1) && 13446 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 13447 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 13448 (xp->xb_pktp->pkt_resid == 0)) { 13449 13450 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 13451 /* 13452 * Successfully set up next portion of cmd 13453 * transfer, try sending it 13454 */ 13455 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 13456 NULL, NULL, 0, (clock_t)0, NULL); 13457 sd_start_cmds(un, NULL); 13458 return; /* Note:x86: need a return here? */ 13459 } 13460 } 13461 13462 /* 13463 * If this is the failfast bp, clear it from un_failfast_bp. This 13464 * can happen if upon being re-tried the failfast bp either 13465 * succeeded or encountered another error (possibly even a different 13466 * error than the one that precipitated the failfast state, but in 13467 * that case it would have had to exhaust retries as well). Regardless, 13468 * this should not occur whenever the instance is in the active 13469 * failfast state. 13470 */ 13471 if (bp == un->un_failfast_bp) { 13472 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 13473 un->un_failfast_bp = NULL; 13474 } 13475 13476 /* 13477 * Clear the failfast state upon successful completion of ANY cmd. 13478 */ 13479 if (bp->b_error == 0) { 13480 un->un_failfast_state = SD_FAILFAST_INACTIVE; 13481 } 13482 13483 /* 13484 * This is used if the command was retried one or more times. Show that 13485 * we are done with it, and allow processing of the waitq to resume. 13486 */ 13487 if (bp == un->un_retry_bp) { 13488 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13489 "sd_return_command: un:0x%p: " 13490 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 13491 un->un_retry_bp = NULL; 13492 un->un_retry_statp = NULL; 13493 } 13494 13495 SD_UPDATE_RDWR_STATS(un, bp); 13496 SD_UPDATE_PARTITION_STATS(un, bp); 13497 13498 switch (un->un_state) { 13499 case SD_STATE_SUSPENDED: 13500 /* 13501 * Notify any threads waiting in sd_ddi_suspend() that 13502 * a command completion has occurred. 13503 */ 13504 cv_broadcast(&un->un_disk_busy_cv); 13505 break; 13506 default: 13507 sd_start_cmds(un, NULL); 13508 break; 13509 } 13510 13511 /* Return this command up the iodone chain to its originator. */ 13512 mutex_exit(SD_MUTEX(un)); 13513 13514 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 13515 xp->xb_pktp = NULL; 13516 13517 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 13518 13519 ASSERT(!mutex_owned(SD_MUTEX(un))); 13520 mutex_enter(SD_MUTEX(un)); 13521 13522 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 13523 } 13524 13525 13526 /* 13527 * Function: sd_return_failed_command 13528 * 13529 * Description: Command completion when an error occurred. 13530 * 13531 * Context: May be called from interrupt context 13532 */ 13533 13534 static void 13535 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 13536 { 13537 ASSERT(bp != NULL); 13538 ASSERT(un != NULL); 13539 ASSERT(mutex_owned(SD_MUTEX(un))); 13540 13541 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13542 "sd_return_failed_command: entry\n"); 13543 13544 /* 13545 * b_resid could already be nonzero due to a partial data 13546 * transfer, so do not change it here. 13547 */ 13548 SD_BIOERROR(bp, errcode); 13549 13550 sd_return_command(un, bp); 13551 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13552 "sd_return_failed_command: exit\n"); 13553 } 13554 13555 13556 /* 13557 * Function: sd_return_failed_command_no_restart 13558 * 13559 * Description: Same as sd_return_failed_command, but ensures that no 13560 * call back into sd_start_cmds will be issued. 13561 * 13562 * Context: May be called from interrupt context 13563 */ 13564 13565 static void 13566 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 13567 int errcode) 13568 { 13569 struct sd_xbuf *xp; 13570 13571 ASSERT(bp != NULL); 13572 ASSERT(un != NULL); 13573 ASSERT(mutex_owned(SD_MUTEX(un))); 13574 xp = SD_GET_XBUF(bp); 13575 ASSERT(xp != NULL); 13576 ASSERT(errcode != 0); 13577 13578 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13579 "sd_return_failed_command_no_restart: entry\n"); 13580 13581 /* 13582 * b_resid could already be nonzero due to a partial data 13583 * transfer, so do not change it here. 13584 */ 13585 SD_BIOERROR(bp, errcode); 13586 13587 /* 13588 * If this is the failfast bp, clear it. This can happen if the 13589 * failfast bp encounterd a fatal error when we attempted to 13590 * re-try it (such as a scsi_transport(9F) failure). However 13591 * we should NOT be in an active failfast state if the failfast 13592 * bp is not NULL. 13593 */ 13594 if (bp == un->un_failfast_bp) { 13595 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 13596 un->un_failfast_bp = NULL; 13597 } 13598 13599 if (bp == un->un_retry_bp) { 13600 /* 13601 * This command was retried one or more times. Show that we are 13602 * done with it, and allow processing of the waitq to resume. 13603 */ 13604 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13605 "sd_return_failed_command_no_restart: " 13606 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 13607 un->un_retry_bp = NULL; 13608 un->un_retry_statp = NULL; 13609 } 13610 13611 SD_UPDATE_RDWR_STATS(un, bp); 13612 SD_UPDATE_PARTITION_STATS(un, bp); 13613 13614 mutex_exit(SD_MUTEX(un)); 13615 13616 if (xp->xb_pktp != NULL) { 13617 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 13618 xp->xb_pktp = NULL; 13619 } 13620 13621 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 13622 13623 mutex_enter(SD_MUTEX(un)); 13624 13625 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13626 "sd_return_failed_command_no_restart: exit\n"); 13627 } 13628 13629 13630 /* 13631 * Function: sd_retry_command 13632 * 13633 * Description: queue up a command for retry, or (optionally) fail it 13634 * if retry counts are exhausted. 13635 * 13636 * Arguments: un - Pointer to the sd_lun struct for the target. 13637 * 13638 * bp - Pointer to the buf for the command to be retried. 13639 * 13640 * retry_check_flag - Flag to see which (if any) of the retry 13641 * counts should be decremented/checked. If the indicated 13642 * retry count is exhausted, then the command will not be 13643 * retried; it will be failed instead. This should use a 13644 * value equal to one of the following: 13645 * 13646 * SD_RETRIES_NOCHECK 13647 * SD_RESD_RETRIES_STANDARD 13648 * SD_RETRIES_VICTIM 13649 * 13650 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 13651 * if the check should be made to see of FLAG_ISOLATE is set 13652 * in the pkt. If FLAG_ISOLATE is set, then the command is 13653 * not retried, it is simply failed. 13654 * 13655 * user_funcp - Ptr to function to call before dispatching the 13656 * command. May be NULL if no action needs to be performed. 13657 * (Primarily intended for printing messages.) 13658 * 13659 * user_arg - Optional argument to be passed along to 13660 * the user_funcp call. 13661 * 13662 * failure_code - errno return code to set in the bp if the 13663 * command is going to be failed. 13664 * 13665 * retry_delay - Retry delay interval in (clock_t) units. May 13666 * be zero which indicates that the retry should be retried 13667 * immediately (ie, without an intervening delay). 13668 * 13669 * statp - Ptr to kstat function to be updated if the command 13670 * is queued for a delayed retry. May be NULL if no kstat 13671 * update is desired. 13672 * 13673 * Context: May be called from interrupt context. 13674 */ 13675 13676 static void 13677 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 13678 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 13679 code), void *user_arg, int failure_code, clock_t retry_delay, 13680 void (*statp)(kstat_io_t *)) 13681 { 13682 struct sd_xbuf *xp; 13683 struct scsi_pkt *pktp; 13684 13685 ASSERT(un != NULL); 13686 ASSERT(mutex_owned(SD_MUTEX(un))); 13687 ASSERT(bp != NULL); 13688 xp = SD_GET_XBUF(bp); 13689 ASSERT(xp != NULL); 13690 pktp = SD_GET_PKTP(bp); 13691 ASSERT(pktp != NULL); 13692 13693 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 13694 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 13695 13696 /* 13697 * If we are syncing or dumping, fail the command to avoid 13698 * recursively calling back into scsi_transport(). 13699 */ 13700 if (ddi_in_panic()) { 13701 goto fail_command_no_log; 13702 } 13703 13704 /* 13705 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 13706 * log an error and fail the command. 13707 */ 13708 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 13709 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 13710 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 13711 sd_dump_memory(un, SD_LOG_IO, "CDB", 13712 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 13713 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 13714 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 13715 goto fail_command; 13716 } 13717 13718 /* 13719 * If we are suspended, then put the command onto head of the 13720 * wait queue since we don't want to start more commands, and 13721 * clear the un_retry_bp. Next time when we are resumed, will 13722 * handle the command in the wait queue. 13723 */ 13724 switch (un->un_state) { 13725 case SD_STATE_SUSPENDED: 13726 case SD_STATE_DUMPING: 13727 bp->av_forw = un->un_waitq_headp; 13728 un->un_waitq_headp = bp; 13729 if (un->un_waitq_tailp == NULL) { 13730 un->un_waitq_tailp = bp; 13731 } 13732 if (bp == un->un_retry_bp) { 13733 un->un_retry_bp = NULL; 13734 un->un_retry_statp = NULL; 13735 } 13736 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13737 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 13738 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 13739 return; 13740 default: 13741 break; 13742 } 13743 13744 /* 13745 * If the caller wants us to check FLAG_ISOLATE, then see if that 13746 * is set; if it is then we do not want to retry the command. 13747 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 13748 */ 13749 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 13750 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 13751 goto fail_command; 13752 } 13753 } 13754 13755 13756 /* 13757 * If SD_RETRIES_FAILFAST is set, it indicates that either a 13758 * command timeout or a selection timeout has occurred. This means 13759 * that we were unable to establish an kind of communication with 13760 * the target, and subsequent retries and/or commands are likely 13761 * to encounter similar results and take a long time to complete. 13762 * 13763 * If this is a failfast error condition, we need to update the 13764 * failfast state, even if this bp does not have B_FAILFAST set. 13765 */ 13766 if (retry_check_flag & SD_RETRIES_FAILFAST) { 13767 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 13768 ASSERT(un->un_failfast_bp == NULL); 13769 /* 13770 * If we are already in the active failfast state, and 13771 * another failfast error condition has been detected, 13772 * then fail this command if it has B_FAILFAST set. 13773 * If B_FAILFAST is clear, then maintain the legacy 13774 * behavior of retrying heroically, even tho this will 13775 * take a lot more time to fail the command. 13776 */ 13777 if (bp->b_flags & B_FAILFAST) { 13778 goto fail_command; 13779 } 13780 } else { 13781 /* 13782 * We're not in the active failfast state, but we 13783 * have a failfast error condition, so we must begin 13784 * transition to the next state. We do this regardless 13785 * of whether or not this bp has B_FAILFAST set. 13786 */ 13787 if (un->un_failfast_bp == NULL) { 13788 /* 13789 * This is the first bp to meet a failfast 13790 * condition so save it on un_failfast_bp & 13791 * do normal retry processing. Do not enter 13792 * active failfast state yet. This marks 13793 * entry into the "failfast pending" state. 13794 */ 13795 un->un_failfast_bp = bp; 13796 13797 } else if (un->un_failfast_bp == bp) { 13798 /* 13799 * This is the second time *this* bp has 13800 * encountered a failfast error condition, 13801 * so enter active failfast state & flush 13802 * queues as appropriate. 13803 */ 13804 un->un_failfast_state = SD_FAILFAST_ACTIVE; 13805 un->un_failfast_bp = NULL; 13806 sd_failfast_flushq(un); 13807 13808 /* 13809 * Fail this bp now if B_FAILFAST set; 13810 * otherwise continue with retries. (It would 13811 * be pretty ironic if this bp succeeded on a 13812 * subsequent retry after we just flushed all 13813 * the queues). 13814 */ 13815 if (bp->b_flags & B_FAILFAST) { 13816 goto fail_command; 13817 } 13818 13819 #if !defined(lint) && !defined(__lint) 13820 } else { 13821 /* 13822 * If neither of the preceeding conditionals 13823 * was true, it means that there is some 13824 * *other* bp that has met an inital failfast 13825 * condition and is currently either being 13826 * retried or is waiting to be retried. In 13827 * that case we should perform normal retry 13828 * processing on *this* bp, since there is a 13829 * chance that the current failfast condition 13830 * is transient and recoverable. If that does 13831 * not turn out to be the case, then retries 13832 * will be cleared when the wait queue is 13833 * flushed anyway. 13834 */ 13835 #endif 13836 } 13837 } 13838 } else { 13839 /* 13840 * SD_RETRIES_FAILFAST is clear, which indicates that we 13841 * likely were able to at least establish some level of 13842 * communication with the target and subsequent commands 13843 * and/or retries are likely to get through to the target, 13844 * In this case we want to be aggressive about clearing 13845 * the failfast state. Note that this does not affect 13846 * the "failfast pending" condition. 13847 */ 13848 un->un_failfast_state = SD_FAILFAST_INACTIVE; 13849 } 13850 13851 13852 /* 13853 * Check the specified retry count to see if we can still do 13854 * any retries with this pkt before we should fail it. 13855 */ 13856 switch (retry_check_flag & SD_RETRIES_MASK) { 13857 case SD_RETRIES_VICTIM: 13858 /* 13859 * Check the victim retry count. If exhausted, then fall 13860 * thru & check against the standard retry count. 13861 */ 13862 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 13863 /* Increment count & proceed with the retry */ 13864 xp->xb_victim_retry_count++; 13865 break; 13866 } 13867 /* Victim retries exhausted, fall back to std. retries... */ 13868 /* FALLTHRU */ 13869 13870 case SD_RETRIES_STANDARD: 13871 if (xp->xb_retry_count >= un->un_retry_count) { 13872 /* Retries exhausted, fail the command */ 13873 SD_TRACE(SD_LOG_IO_CORE, un, 13874 "sd_retry_command: retries exhausted!\n"); 13875 /* 13876 * update b_resid for failed SCMD_READ & SCMD_WRITE 13877 * commands with nonzero pkt_resid. 13878 */ 13879 if ((pktp->pkt_reason == CMD_CMPLT) && 13880 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 13881 (pktp->pkt_resid != 0)) { 13882 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 13883 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 13884 SD_UPDATE_B_RESID(bp, pktp); 13885 } 13886 } 13887 goto fail_command; 13888 } 13889 xp->xb_retry_count++; 13890 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13891 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 13892 break; 13893 13894 case SD_RETRIES_UA: 13895 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 13896 /* Retries exhausted, fail the command */ 13897 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13898 "Unit Attention retries exhausted. " 13899 "Check the target.\n"); 13900 goto fail_command; 13901 } 13902 xp->xb_ua_retry_count++; 13903 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13904 "sd_retry_command: retry count:%d\n", 13905 xp->xb_ua_retry_count); 13906 break; 13907 13908 case SD_RETRIES_BUSY: 13909 if (xp->xb_retry_count >= un->un_busy_retry_count) { 13910 /* Retries exhausted, fail the command */ 13911 SD_TRACE(SD_LOG_IO_CORE, un, 13912 "sd_retry_command: retries exhausted!\n"); 13913 goto fail_command; 13914 } 13915 xp->xb_retry_count++; 13916 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13917 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 13918 break; 13919 13920 case SD_RETRIES_NOCHECK: 13921 default: 13922 /* No retry count to check. Just proceed with the retry */ 13923 break; 13924 } 13925 13926 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 13927 13928 /* 13929 * If we were given a zero timeout, we must attempt to retry the 13930 * command immediately (ie, without a delay). 13931 */ 13932 if (retry_delay == 0) { 13933 /* 13934 * Check some limiting conditions to see if we can actually 13935 * do the immediate retry. If we cannot, then we must 13936 * fall back to queueing up a delayed retry. 13937 */ 13938 if (un->un_ncmds_in_transport >= un->un_throttle) { 13939 /* 13940 * We are at the throttle limit for the target, 13941 * fall back to delayed retry. 13942 */ 13943 retry_delay = SD_BSY_TIMEOUT; 13944 statp = kstat_waitq_enter; 13945 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13946 "sd_retry_command: immed. retry hit " 13947 "throttle!\n"); 13948 } else { 13949 /* 13950 * We're clear to proceed with the immediate retry. 13951 * First call the user-provided function (if any) 13952 */ 13953 if (user_funcp != NULL) { 13954 (*user_funcp)(un, bp, user_arg, 13955 SD_IMMEDIATE_RETRY_ISSUED); 13956 #ifdef __lock_lint 13957 sd_print_incomplete_msg(un, bp, user_arg, 13958 SD_IMMEDIATE_RETRY_ISSUED); 13959 sd_print_cmd_incomplete_msg(un, bp, user_arg, 13960 SD_IMMEDIATE_RETRY_ISSUED); 13961 sd_print_sense_failed_msg(un, bp, user_arg, 13962 SD_IMMEDIATE_RETRY_ISSUED); 13963 #endif 13964 } 13965 13966 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13967 "sd_retry_command: issuing immediate retry\n"); 13968 13969 /* 13970 * Call sd_start_cmds() to transport the command to 13971 * the target. 13972 */ 13973 sd_start_cmds(un, bp); 13974 13975 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 13976 "sd_retry_command exit\n"); 13977 return; 13978 } 13979 } 13980 13981 /* 13982 * Set up to retry the command after a delay. 13983 * First call the user-provided function (if any) 13984 */ 13985 if (user_funcp != NULL) { 13986 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 13987 } 13988 13989 sd_set_retry_bp(un, bp, retry_delay, statp); 13990 13991 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 13992 return; 13993 13994 fail_command: 13995 13996 if (user_funcp != NULL) { 13997 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 13998 } 13999 14000 fail_command_no_log: 14001 14002 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14003 "sd_retry_command: returning failed command\n"); 14004 14005 sd_return_failed_command(un, bp, failure_code); 14006 14007 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 14008 } 14009 14010 14011 /* 14012 * Function: sd_set_retry_bp 14013 * 14014 * Description: Set up the given bp for retry. 14015 * 14016 * Arguments: un - ptr to associated softstate 14017 * bp - ptr to buf(9S) for the command 14018 * retry_delay - time interval before issuing retry (may be 0) 14019 * statp - optional pointer to kstat function 14020 * 14021 * Context: May be called under interrupt context 14022 */ 14023 14024 static void 14025 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 14026 void (*statp)(kstat_io_t *)) 14027 { 14028 ASSERT(un != NULL); 14029 ASSERT(mutex_owned(SD_MUTEX(un))); 14030 ASSERT(bp != NULL); 14031 14032 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14033 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 14034 14035 /* 14036 * Indicate that the command is being retried. This will not allow any 14037 * other commands on the wait queue to be transported to the target 14038 * until this command has been completed (success or failure). The 14039 * "retry command" is not transported to the target until the given 14040 * time delay expires, unless the user specified a 0 retry_delay. 14041 * 14042 * Note: the timeout(9F) callback routine is what actually calls 14043 * sd_start_cmds() to transport the command, with the exception of a 14044 * zero retry_delay. The only current implementor of a zero retry delay 14045 * is the case where a START_STOP_UNIT is sent to spin-up a device. 14046 */ 14047 if (un->un_retry_bp == NULL) { 14048 ASSERT(un->un_retry_statp == NULL); 14049 un->un_retry_bp = bp; 14050 14051 /* 14052 * If the user has not specified a delay the command should 14053 * be queued and no timeout should be scheduled. 14054 */ 14055 if (retry_delay == 0) { 14056 /* 14057 * Save the kstat pointer that will be used in the 14058 * call to SD_UPDATE_KSTATS() below, so that 14059 * sd_start_cmds() can correctly decrement the waitq 14060 * count when it is time to transport this command. 14061 */ 14062 un->un_retry_statp = statp; 14063 goto done; 14064 } 14065 } 14066 14067 if (un->un_retry_bp == bp) { 14068 /* 14069 * Save the kstat pointer that will be used in the call to 14070 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 14071 * correctly decrement the waitq count when it is time to 14072 * transport this command. 14073 */ 14074 un->un_retry_statp = statp; 14075 14076 /* 14077 * Schedule a timeout if: 14078 * 1) The user has specified a delay. 14079 * 2) There is not a START_STOP_UNIT callback pending. 14080 * 14081 * If no delay has been specified, then it is up to the caller 14082 * to ensure that IO processing continues without stalling. 14083 * Effectively, this means that the caller will issue the 14084 * required call to sd_start_cmds(). The START_STOP_UNIT 14085 * callback does this after the START STOP UNIT command has 14086 * completed. In either of these cases we should not schedule 14087 * a timeout callback here. Also don't schedule the timeout if 14088 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 14089 */ 14090 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 14091 (un->un_direct_priority_timeid == NULL)) { 14092 un->un_retry_timeid = 14093 timeout(sd_start_retry_command, un, retry_delay); 14094 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14095 "sd_set_retry_bp: setting timeout: un: 0x%p" 14096 " bp:0x%p un_retry_timeid:0x%p\n", 14097 un, bp, un->un_retry_timeid); 14098 } 14099 } else { 14100 /* 14101 * We only get in here if there is already another command 14102 * waiting to be retried. In this case, we just put the 14103 * given command onto the wait queue, so it can be transported 14104 * after the current retry command has completed. 14105 * 14106 * Also we have to make sure that if the command at the head 14107 * of the wait queue is the un_failfast_bp, that we do not 14108 * put ahead of it any other commands that are to be retried. 14109 */ 14110 if ((un->un_failfast_bp != NULL) && 14111 (un->un_failfast_bp == un->un_waitq_headp)) { 14112 /* 14113 * Enqueue this command AFTER the first command on 14114 * the wait queue (which is also un_failfast_bp). 14115 */ 14116 bp->av_forw = un->un_waitq_headp->av_forw; 14117 un->un_waitq_headp->av_forw = bp; 14118 if (un->un_waitq_headp == un->un_waitq_tailp) { 14119 un->un_waitq_tailp = bp; 14120 } 14121 } else { 14122 /* Enqueue this command at the head of the waitq. */ 14123 bp->av_forw = un->un_waitq_headp; 14124 un->un_waitq_headp = bp; 14125 if (un->un_waitq_tailp == NULL) { 14126 un->un_waitq_tailp = bp; 14127 } 14128 } 14129 14130 if (statp == NULL) { 14131 statp = kstat_waitq_enter; 14132 } 14133 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14134 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 14135 } 14136 14137 done: 14138 if (statp != NULL) { 14139 SD_UPDATE_KSTATS(un, statp, bp); 14140 } 14141 14142 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14143 "sd_set_retry_bp: exit un:0x%p\n", un); 14144 } 14145 14146 14147 /* 14148 * Function: sd_start_retry_command 14149 * 14150 * Description: Start the command that has been waiting on the target's 14151 * retry queue. Called from timeout(9F) context after the 14152 * retry delay interval has expired. 14153 * 14154 * Arguments: arg - pointer to associated softstate for the device. 14155 * 14156 * Context: timeout(9F) thread context. May not sleep. 14157 */ 14158 14159 static void 14160 sd_start_retry_command(void *arg) 14161 { 14162 struct sd_lun *un = arg; 14163 14164 ASSERT(un != NULL); 14165 ASSERT(!mutex_owned(SD_MUTEX(un))); 14166 14167 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14168 "sd_start_retry_command: entry\n"); 14169 14170 mutex_enter(SD_MUTEX(un)); 14171 14172 un->un_retry_timeid = NULL; 14173 14174 if (un->un_retry_bp != NULL) { 14175 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14176 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 14177 un, un->un_retry_bp); 14178 sd_start_cmds(un, un->un_retry_bp); 14179 } 14180 14181 mutex_exit(SD_MUTEX(un)); 14182 14183 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14184 "sd_start_retry_command: exit\n"); 14185 } 14186 14187 14188 /* 14189 * Function: sd_start_direct_priority_command 14190 * 14191 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 14192 * received TRAN_BUSY when we called scsi_transport() to send it 14193 * to the underlying HBA. This function is called from timeout(9F) 14194 * context after the delay interval has expired. 14195 * 14196 * Arguments: arg - pointer to associated buf(9S) to be restarted. 14197 * 14198 * Context: timeout(9F) thread context. May not sleep. 14199 */ 14200 14201 static void 14202 sd_start_direct_priority_command(void *arg) 14203 { 14204 struct buf *priority_bp = arg; 14205 struct sd_lun *un; 14206 14207 ASSERT(priority_bp != NULL); 14208 un = SD_GET_UN(priority_bp); 14209 ASSERT(un != NULL); 14210 ASSERT(!mutex_owned(SD_MUTEX(un))); 14211 14212 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14213 "sd_start_direct_priority_command: entry\n"); 14214 14215 mutex_enter(SD_MUTEX(un)); 14216 un->un_direct_priority_timeid = NULL; 14217 sd_start_cmds(un, priority_bp); 14218 mutex_exit(SD_MUTEX(un)); 14219 14220 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14221 "sd_start_direct_priority_command: exit\n"); 14222 } 14223 14224 14225 /* 14226 * Function: sd_send_request_sense_command 14227 * 14228 * Description: Sends a REQUEST SENSE command to the target 14229 * 14230 * Context: May be called from interrupt context. 14231 */ 14232 14233 static void 14234 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 14235 struct scsi_pkt *pktp) 14236 { 14237 ASSERT(bp != NULL); 14238 ASSERT(un != NULL); 14239 ASSERT(mutex_owned(SD_MUTEX(un))); 14240 14241 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 14242 "entry: buf:0x%p\n", bp); 14243 14244 /* 14245 * If we are syncing or dumping, then fail the command to avoid a 14246 * recursive callback into scsi_transport(). Also fail the command 14247 * if we are suspended (legacy behavior). 14248 */ 14249 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 14250 (un->un_state == SD_STATE_DUMPING)) { 14251 sd_return_failed_command(un, bp, EIO); 14252 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14253 "sd_send_request_sense_command: syncing/dumping, exit\n"); 14254 return; 14255 } 14256 14257 /* 14258 * Retry the failed command and don't issue the request sense if: 14259 * 1) the sense buf is busy 14260 * 2) we have 1 or more outstanding commands on the target 14261 * (the sense data will be cleared or invalidated any way) 14262 * 14263 * Note: There could be an issue with not checking a retry limit here, 14264 * the problem is determining which retry limit to check. 14265 */ 14266 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 14267 /* Don't retry if the command is flagged as non-retryable */ 14268 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 14269 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 14270 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 14271 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14272 "sd_send_request_sense_command: " 14273 "at full throttle, retrying exit\n"); 14274 } else { 14275 sd_return_failed_command(un, bp, EIO); 14276 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14277 "sd_send_request_sense_command: " 14278 "at full throttle, non-retryable exit\n"); 14279 } 14280 return; 14281 } 14282 14283 sd_mark_rqs_busy(un, bp); 14284 sd_start_cmds(un, un->un_rqs_bp); 14285 14286 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14287 "sd_send_request_sense_command: exit\n"); 14288 } 14289 14290 14291 /* 14292 * Function: sd_mark_rqs_busy 14293 * 14294 * Description: Indicate that the request sense bp for this instance is 14295 * in use. 14296 * 14297 * Context: May be called under interrupt context 14298 */ 14299 14300 static void 14301 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 14302 { 14303 struct sd_xbuf *sense_xp; 14304 14305 ASSERT(un != NULL); 14306 ASSERT(bp != NULL); 14307 ASSERT(mutex_owned(SD_MUTEX(un))); 14308 ASSERT(un->un_sense_isbusy == 0); 14309 14310 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 14311 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 14312 14313 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 14314 ASSERT(sense_xp != NULL); 14315 14316 SD_INFO(SD_LOG_IO, un, 14317 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 14318 14319 ASSERT(sense_xp->xb_pktp != NULL); 14320 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 14321 == (FLAG_SENSING | FLAG_HEAD)); 14322 14323 un->un_sense_isbusy = 1; 14324 un->un_rqs_bp->b_resid = 0; 14325 sense_xp->xb_pktp->pkt_resid = 0; 14326 sense_xp->xb_pktp->pkt_reason = 0; 14327 14328 /* So we can get back the bp at interrupt time! */ 14329 sense_xp->xb_sense_bp = bp; 14330 14331 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 14332 14333 /* 14334 * Mark this buf as awaiting sense data. (This is already set in 14335 * the pkt_flags for the RQS packet.) 14336 */ 14337 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 14338 14339 sense_xp->xb_retry_count = 0; 14340 sense_xp->xb_victim_retry_count = 0; 14341 sense_xp->xb_ua_retry_count = 0; 14342 sense_xp->xb_nr_retry_count = 0; 14343 sense_xp->xb_dma_resid = 0; 14344 14345 /* Clean up the fields for auto-request sense */ 14346 sense_xp->xb_sense_status = 0; 14347 sense_xp->xb_sense_state = 0; 14348 sense_xp->xb_sense_resid = 0; 14349 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 14350 14351 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 14352 } 14353 14354 14355 /* 14356 * Function: sd_mark_rqs_idle 14357 * 14358 * Description: SD_MUTEX must be held continuously through this routine 14359 * to prevent reuse of the rqs struct before the caller can 14360 * complete it's processing. 14361 * 14362 * Return Code: Pointer to the RQS buf 14363 * 14364 * Context: May be called under interrupt context 14365 */ 14366 14367 static struct buf * 14368 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 14369 { 14370 struct buf *bp; 14371 ASSERT(un != NULL); 14372 ASSERT(sense_xp != NULL); 14373 ASSERT(mutex_owned(SD_MUTEX(un))); 14374 ASSERT(un->un_sense_isbusy != 0); 14375 14376 un->un_sense_isbusy = 0; 14377 bp = sense_xp->xb_sense_bp; 14378 sense_xp->xb_sense_bp = NULL; 14379 14380 /* This pkt is no longer interested in getting sense data */ 14381 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 14382 14383 return (bp); 14384 } 14385 14386 14387 14388 /* 14389 * Function: sd_alloc_rqs 14390 * 14391 * Description: Set up the unit to receive auto request sense data 14392 * 14393 * Return Code: DDI_SUCCESS or DDI_FAILURE 14394 * 14395 * Context: Called under attach(9E) context 14396 */ 14397 14398 static int 14399 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 14400 { 14401 struct sd_xbuf *xp; 14402 14403 ASSERT(un != NULL); 14404 ASSERT(!mutex_owned(SD_MUTEX(un))); 14405 ASSERT(un->un_rqs_bp == NULL); 14406 ASSERT(un->un_rqs_pktp == NULL); 14407 14408 /* 14409 * First allocate the required buf and scsi_pkt structs, then set up 14410 * the CDB in the scsi_pkt for a REQUEST SENSE command. 14411 */ 14412 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 14413 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 14414 if (un->un_rqs_bp == NULL) { 14415 return (DDI_FAILURE); 14416 } 14417 14418 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 14419 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 14420 14421 if (un->un_rqs_pktp == NULL) { 14422 sd_free_rqs(un); 14423 return (DDI_FAILURE); 14424 } 14425 14426 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 14427 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 14428 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 14429 14430 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 14431 14432 /* Set up the other needed members in the ARQ scsi_pkt. */ 14433 un->un_rqs_pktp->pkt_comp = sdintr; 14434 un->un_rqs_pktp->pkt_time = sd_io_time; 14435 un->un_rqs_pktp->pkt_flags |= 14436 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 14437 14438 /* 14439 * Allocate & init the sd_xbuf struct for the RQS command. Do not 14440 * provide any intpkt, destroypkt routines as we take care of 14441 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 14442 */ 14443 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14444 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 14445 xp->xb_pktp = un->un_rqs_pktp; 14446 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14447 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 14448 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 14449 14450 /* 14451 * Save the pointer to the request sense private bp so it can 14452 * be retrieved in sdintr. 14453 */ 14454 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 14455 ASSERT(un->un_rqs_bp->b_private == xp); 14456 14457 /* 14458 * See if the HBA supports auto-request sense for the specified 14459 * target/lun. If it does, then try to enable it (if not already 14460 * enabled). 14461 * 14462 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 14463 * failure, while for other HBAs (pln) scsi_ifsetcap will always 14464 * return success. However, in both of these cases ARQ is always 14465 * enabled and scsi_ifgetcap will always return true. The best approach 14466 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 14467 * 14468 * The 3rd case is the HBA (adp) always return enabled on 14469 * scsi_ifgetgetcap even when it's not enable, the best approach 14470 * is issue a scsi_ifsetcap then a scsi_ifgetcap 14471 * Note: this case is to circumvent the Adaptec bug. (x86 only) 14472 */ 14473 14474 if (un->un_f_is_fibre == TRUE) { 14475 un->un_f_arq_enabled = TRUE; 14476 } else { 14477 #if defined(__i386) || defined(__amd64) 14478 /* 14479 * Circumvent the Adaptec bug, remove this code when 14480 * the bug is fixed 14481 */ 14482 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 14483 #endif 14484 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 14485 case 0: 14486 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14487 "sd_alloc_rqs: HBA supports ARQ\n"); 14488 /* 14489 * ARQ is supported by this HBA but currently is not 14490 * enabled. Attempt to enable it and if successful then 14491 * mark this instance as ARQ enabled. 14492 */ 14493 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 14494 == 1) { 14495 /* Successfully enabled ARQ in the HBA */ 14496 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14497 "sd_alloc_rqs: ARQ enabled\n"); 14498 un->un_f_arq_enabled = TRUE; 14499 } else { 14500 /* Could not enable ARQ in the HBA */ 14501 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14502 "sd_alloc_rqs: failed ARQ enable\n"); 14503 un->un_f_arq_enabled = FALSE; 14504 } 14505 break; 14506 case 1: 14507 /* 14508 * ARQ is supported by this HBA and is already enabled. 14509 * Just mark ARQ as enabled for this instance. 14510 */ 14511 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14512 "sd_alloc_rqs: ARQ already enabled\n"); 14513 un->un_f_arq_enabled = TRUE; 14514 break; 14515 default: 14516 /* 14517 * ARQ is not supported by this HBA; disable it for this 14518 * instance. 14519 */ 14520 SD_INFO(SD_LOG_ATTACH_DETACH, un, 14521 "sd_alloc_rqs: HBA does not support ARQ\n"); 14522 un->un_f_arq_enabled = FALSE; 14523 break; 14524 } 14525 } 14526 14527 return (DDI_SUCCESS); 14528 } 14529 14530 14531 /* 14532 * Function: sd_free_rqs 14533 * 14534 * Description: Cleanup for the pre-instance RQS command. 14535 * 14536 * Context: Kernel thread context 14537 */ 14538 14539 static void 14540 sd_free_rqs(struct sd_lun *un) 14541 { 14542 ASSERT(un != NULL); 14543 14544 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 14545 14546 /* 14547 * If consistent memory is bound to a scsi_pkt, the pkt 14548 * has to be destroyed *before* freeing the consistent memory. 14549 * Don't change the sequence of this operations. 14550 * scsi_destroy_pkt() might access memory, which isn't allowed, 14551 * after it was freed in scsi_free_consistent_buf(). 14552 */ 14553 if (un->un_rqs_pktp != NULL) { 14554 scsi_destroy_pkt(un->un_rqs_pktp); 14555 un->un_rqs_pktp = NULL; 14556 } 14557 14558 if (un->un_rqs_bp != NULL) { 14559 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp); 14560 if (xp != NULL) { 14561 kmem_free(xp, sizeof (struct sd_xbuf)); 14562 } 14563 scsi_free_consistent_buf(un->un_rqs_bp); 14564 un->un_rqs_bp = NULL; 14565 } 14566 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 14567 } 14568 14569 14570 14571 /* 14572 * Function: sd_reduce_throttle 14573 * 14574 * Description: Reduces the maximum # of outstanding commands on a 14575 * target to the current number of outstanding commands. 14576 * Queues a tiemout(9F) callback to restore the limit 14577 * after a specified interval has elapsed. 14578 * Typically used when we get a TRAN_BUSY return code 14579 * back from scsi_transport(). 14580 * 14581 * Arguments: un - ptr to the sd_lun softstate struct 14582 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 14583 * 14584 * Context: May be called from interrupt context 14585 */ 14586 14587 static void 14588 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 14589 { 14590 ASSERT(un != NULL); 14591 ASSERT(mutex_owned(SD_MUTEX(un))); 14592 ASSERT(un->un_ncmds_in_transport >= 0); 14593 14594 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 14595 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 14596 un, un->un_throttle, un->un_ncmds_in_transport); 14597 14598 if (un->un_throttle > 1) { 14599 if (un->un_f_use_adaptive_throttle == TRUE) { 14600 switch (throttle_type) { 14601 case SD_THROTTLE_TRAN_BUSY: 14602 if (un->un_busy_throttle == 0) { 14603 un->un_busy_throttle = un->un_throttle; 14604 } 14605 break; 14606 case SD_THROTTLE_QFULL: 14607 un->un_busy_throttle = 0; 14608 break; 14609 default: 14610 ASSERT(FALSE); 14611 } 14612 14613 if (un->un_ncmds_in_transport > 0) { 14614 un->un_throttle = un->un_ncmds_in_transport; 14615 } 14616 14617 } else { 14618 if (un->un_ncmds_in_transport == 0) { 14619 un->un_throttle = 1; 14620 } else { 14621 un->un_throttle = un->un_ncmds_in_transport; 14622 } 14623 } 14624 } 14625 14626 /* Reschedule the timeout if none is currently active */ 14627 if (un->un_reset_throttle_timeid == NULL) { 14628 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 14629 un, SD_THROTTLE_RESET_INTERVAL); 14630 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14631 "sd_reduce_throttle: timeout scheduled!\n"); 14632 } 14633 14634 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 14635 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 14636 } 14637 14638 14639 14640 /* 14641 * Function: sd_restore_throttle 14642 * 14643 * Description: Callback function for timeout(9F). Resets the current 14644 * value of un->un_throttle to its default. 14645 * 14646 * Arguments: arg - pointer to associated softstate for the device. 14647 * 14648 * Context: May be called from interrupt context 14649 */ 14650 14651 static void 14652 sd_restore_throttle(void *arg) 14653 { 14654 struct sd_lun *un = arg; 14655 14656 ASSERT(un != NULL); 14657 ASSERT(!mutex_owned(SD_MUTEX(un))); 14658 14659 mutex_enter(SD_MUTEX(un)); 14660 14661 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 14662 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 14663 14664 un->un_reset_throttle_timeid = NULL; 14665 14666 if (un->un_f_use_adaptive_throttle == TRUE) { 14667 /* 14668 * If un_busy_throttle is nonzero, then it contains the 14669 * value that un_throttle was when we got a TRAN_BUSY back 14670 * from scsi_transport(). We want to revert back to this 14671 * value. 14672 * 14673 * In the QFULL case, the throttle limit will incrementally 14674 * increase until it reaches max throttle. 14675 */ 14676 if (un->un_busy_throttle > 0) { 14677 un->un_throttle = un->un_busy_throttle; 14678 un->un_busy_throttle = 0; 14679 } else { 14680 /* 14681 * increase throttle by 10% open gate slowly, schedule 14682 * another restore if saved throttle has not been 14683 * reached 14684 */ 14685 short throttle; 14686 if (sd_qfull_throttle_enable) { 14687 throttle = un->un_throttle + 14688 max((un->un_throttle / 10), 1); 14689 un->un_throttle = 14690 (throttle < un->un_saved_throttle) ? 14691 throttle : un->un_saved_throttle; 14692 if (un->un_throttle < un->un_saved_throttle) { 14693 un->un_reset_throttle_timeid = 14694 timeout(sd_restore_throttle, 14695 un, 14696 SD_QFULL_THROTTLE_RESET_INTERVAL); 14697 } 14698 } 14699 } 14700 14701 /* 14702 * If un_throttle has fallen below the low-water mark, we 14703 * restore the maximum value here (and allow it to ratchet 14704 * down again if necessary). 14705 */ 14706 if (un->un_throttle < un->un_min_throttle) { 14707 un->un_throttle = un->un_saved_throttle; 14708 } 14709 } else { 14710 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 14711 "restoring limit from 0x%x to 0x%x\n", 14712 un->un_throttle, un->un_saved_throttle); 14713 un->un_throttle = un->un_saved_throttle; 14714 } 14715 14716 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14717 "sd_restore_throttle: calling sd_start_cmds!\n"); 14718 14719 sd_start_cmds(un, NULL); 14720 14721 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14722 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 14723 un, un->un_throttle); 14724 14725 mutex_exit(SD_MUTEX(un)); 14726 14727 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 14728 } 14729 14730 /* 14731 * Function: sdrunout 14732 * 14733 * Description: Callback routine for scsi_init_pkt when a resource allocation 14734 * fails. 14735 * 14736 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 14737 * soft state instance. 14738 * 14739 * Return Code: The scsi_init_pkt routine allows for the callback function to 14740 * return a 0 indicating the callback should be rescheduled or a 1 14741 * indicating not to reschedule. This routine always returns 1 14742 * because the driver always provides a callback function to 14743 * scsi_init_pkt. This results in a callback always being scheduled 14744 * (via the scsi_init_pkt callback implementation) if a resource 14745 * failure occurs. 14746 * 14747 * Context: This callback function may not block or call routines that block 14748 * 14749 * Note: Using the scsi_init_pkt callback facility can result in an I/O 14750 * request persisting at the head of the list which cannot be 14751 * satisfied even after multiple retries. In the future the driver 14752 * may implement some time of maximum runout count before failing 14753 * an I/O. 14754 */ 14755 14756 static int 14757 sdrunout(caddr_t arg) 14758 { 14759 struct sd_lun *un = (struct sd_lun *)arg; 14760 14761 ASSERT(un != NULL); 14762 ASSERT(!mutex_owned(SD_MUTEX(un))); 14763 14764 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 14765 14766 mutex_enter(SD_MUTEX(un)); 14767 sd_start_cmds(un, NULL); 14768 mutex_exit(SD_MUTEX(un)); 14769 /* 14770 * This callback routine always returns 1 (i.e. do not reschedule) 14771 * because we always specify sdrunout as the callback handler for 14772 * scsi_init_pkt inside the call to sd_start_cmds. 14773 */ 14774 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 14775 return (1); 14776 } 14777 14778 14779 /* 14780 * Function: sdintr 14781 * 14782 * Description: Completion callback routine for scsi_pkt(9S) structs 14783 * sent to the HBA driver via scsi_transport(9F). 14784 * 14785 * Context: Interrupt context 14786 */ 14787 14788 static void 14789 sdintr(struct scsi_pkt *pktp) 14790 { 14791 struct buf *bp; 14792 struct sd_xbuf *xp; 14793 struct sd_lun *un; 14794 size_t actual_len; 14795 14796 ASSERT(pktp != NULL); 14797 bp = (struct buf *)pktp->pkt_private; 14798 ASSERT(bp != NULL); 14799 xp = SD_GET_XBUF(bp); 14800 ASSERT(xp != NULL); 14801 ASSERT(xp->xb_pktp != NULL); 14802 un = SD_GET_UN(bp); 14803 ASSERT(un != NULL); 14804 ASSERT(!mutex_owned(SD_MUTEX(un))); 14805 14806 #ifdef SD_FAULT_INJECTION 14807 14808 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 14809 /* SD FaultInjection */ 14810 sd_faultinjection(pktp); 14811 14812 #endif /* SD_FAULT_INJECTION */ 14813 14814 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 14815 " xp:0x%p, un:0x%p\n", bp, xp, un); 14816 14817 mutex_enter(SD_MUTEX(un)); 14818 14819 /* Reduce the count of the #commands currently in transport */ 14820 un->un_ncmds_in_transport--; 14821 ASSERT(un->un_ncmds_in_transport >= 0); 14822 14823 /* Increment counter to indicate that the callback routine is active */ 14824 un->un_in_callback++; 14825 14826 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 14827 14828 #ifdef SDDEBUG 14829 if (bp == un->un_retry_bp) { 14830 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 14831 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 14832 un, un->un_retry_bp, un->un_ncmds_in_transport); 14833 } 14834 #endif 14835 14836 /* 14837 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media 14838 * state if needed. 14839 */ 14840 if (pktp->pkt_reason == CMD_DEV_GONE) { 14841 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14842 "Command failed to complete...Device is gone\n"); 14843 if (un->un_mediastate != DKIO_DEV_GONE) { 14844 un->un_mediastate = DKIO_DEV_GONE; 14845 cv_broadcast(&un->un_state_cv); 14846 } 14847 sd_return_failed_command(un, bp, EIO); 14848 goto exit; 14849 } 14850 14851 if (pktp->pkt_state & STATE_XARQ_DONE) { 14852 SD_TRACE(SD_LOG_COMMON, un, 14853 "sdintr: extra sense data received. pkt=%p\n", pktp); 14854 } 14855 14856 /* 14857 * First see if the pkt has auto-request sense data with it.... 14858 * Look at the packet state first so we don't take a performance 14859 * hit looking at the arq enabled flag unless absolutely necessary. 14860 */ 14861 if ((pktp->pkt_state & STATE_ARQ_DONE) && 14862 (un->un_f_arq_enabled == TRUE)) { 14863 /* 14864 * The HBA did an auto request sense for this command so check 14865 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 14866 * driver command that should not be retried. 14867 */ 14868 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 14869 /* 14870 * Save the relevant sense info into the xp for the 14871 * original cmd. 14872 */ 14873 struct scsi_arq_status *asp; 14874 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 14875 xp->xb_sense_status = 14876 *((uchar_t *)(&(asp->sts_rqpkt_status))); 14877 xp->xb_sense_state = asp->sts_rqpkt_state; 14878 xp->xb_sense_resid = asp->sts_rqpkt_resid; 14879 if (pktp->pkt_state & STATE_XARQ_DONE) { 14880 actual_len = MAX_SENSE_LENGTH - 14881 xp->xb_sense_resid; 14882 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 14883 MAX_SENSE_LENGTH); 14884 } else { 14885 if (xp->xb_sense_resid > SENSE_LENGTH) { 14886 actual_len = MAX_SENSE_LENGTH - 14887 xp->xb_sense_resid; 14888 } else { 14889 actual_len = SENSE_LENGTH - 14890 xp->xb_sense_resid; 14891 } 14892 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 14893 xp->xb_sense_resid = 14894 (int)(((struct uscsi_cmd *) 14895 (xp->xb_pktinfo))-> 14896 uscsi_rqlen) - actual_len; 14897 } 14898 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 14899 SENSE_LENGTH); 14900 } 14901 14902 /* fail the command */ 14903 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14904 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 14905 sd_return_failed_command(un, bp, EIO); 14906 goto exit; 14907 } 14908 14909 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 14910 /* 14911 * We want to either retry or fail this command, so free 14912 * the DMA resources here. If we retry the command then 14913 * the DMA resources will be reallocated in sd_start_cmds(). 14914 * Note that when PKT_DMA_PARTIAL is used, this reallocation 14915 * causes the *entire* transfer to start over again from the 14916 * beginning of the request, even for PARTIAL chunks that 14917 * have already transferred successfully. 14918 */ 14919 if ((un->un_f_is_fibre == TRUE) && 14920 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 14921 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 14922 scsi_dmafree(pktp); 14923 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 14924 } 14925 #endif 14926 14927 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14928 "sdintr: arq done, sd_handle_auto_request_sense\n"); 14929 14930 sd_handle_auto_request_sense(un, bp, xp, pktp); 14931 goto exit; 14932 } 14933 14934 /* Next see if this is the REQUEST SENSE pkt for the instance */ 14935 if (pktp->pkt_flags & FLAG_SENSING) { 14936 /* This pktp is from the unit's REQUEST_SENSE command */ 14937 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14938 "sdintr: sd_handle_request_sense\n"); 14939 sd_handle_request_sense(un, bp, xp, pktp); 14940 goto exit; 14941 } 14942 14943 /* 14944 * Check to see if the command successfully completed as requested; 14945 * this is the most common case (and also the hot performance path). 14946 * 14947 * Requirements for successful completion are: 14948 * pkt_reason is CMD_CMPLT and packet status is status good. 14949 * In addition: 14950 * - A residual of zero indicates successful completion no matter what 14951 * the command is. 14952 * - If the residual is not zero and the command is not a read or 14953 * write, then it's still defined as successful completion. In other 14954 * words, if the command is a read or write the residual must be 14955 * zero for successful completion. 14956 * - If the residual is not zero and the command is a read or 14957 * write, and it's a USCSICMD, then it's still defined as 14958 * successful completion. 14959 */ 14960 if ((pktp->pkt_reason == CMD_CMPLT) && 14961 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 14962 14963 /* 14964 * Since this command is returned with a good status, we 14965 * can reset the count for Sonoma failover. 14966 */ 14967 un->un_sonoma_failure_count = 0; 14968 14969 /* 14970 * Return all USCSI commands on good status 14971 */ 14972 if (pktp->pkt_resid == 0) { 14973 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14974 "sdintr: returning command for resid == 0\n"); 14975 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 14976 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 14977 SD_UPDATE_B_RESID(bp, pktp); 14978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14979 "sdintr: returning command for resid != 0\n"); 14980 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 14981 SD_UPDATE_B_RESID(bp, pktp); 14982 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14983 "sdintr: returning uscsi command\n"); 14984 } else { 14985 goto not_successful; 14986 } 14987 sd_return_command(un, bp); 14988 14989 /* 14990 * Decrement counter to indicate that the callback routine 14991 * is done. 14992 */ 14993 un->un_in_callback--; 14994 ASSERT(un->un_in_callback >= 0); 14995 mutex_exit(SD_MUTEX(un)); 14996 14997 return; 14998 } 14999 15000 not_successful: 15001 15002 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 15003 /* 15004 * The following is based upon knowledge of the underlying transport 15005 * and its use of DMA resources. This code should be removed when 15006 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 15007 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 15008 * and sd_start_cmds(). 15009 * 15010 * Free any DMA resources associated with this command if there 15011 * is a chance it could be retried or enqueued for later retry. 15012 * If we keep the DMA binding then mpxio cannot reissue the 15013 * command on another path whenever a path failure occurs. 15014 * 15015 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 15016 * causes the *entire* transfer to start over again from the 15017 * beginning of the request, even for PARTIAL chunks that 15018 * have already transferred successfully. 15019 * 15020 * This is only done for non-uscsi commands (and also skipped for the 15021 * driver's internal RQS command). Also just do this for Fibre Channel 15022 * devices as these are the only ones that support mpxio. 15023 */ 15024 if ((un->un_f_is_fibre == TRUE) && 15025 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15026 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 15027 scsi_dmafree(pktp); 15028 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15029 } 15030 #endif 15031 15032 /* 15033 * The command did not successfully complete as requested so check 15034 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 15035 * driver command that should not be retried so just return. If 15036 * FLAG_DIAGNOSE is not set the error will be processed below. 15037 */ 15038 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15039 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15040 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 15041 /* 15042 * Issue a request sense if a check condition caused the error 15043 * (we handle the auto request sense case above), otherwise 15044 * just fail the command. 15045 */ 15046 if ((pktp->pkt_reason == CMD_CMPLT) && 15047 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 15048 sd_send_request_sense_command(un, bp, pktp); 15049 } else { 15050 sd_return_failed_command(un, bp, EIO); 15051 } 15052 goto exit; 15053 } 15054 15055 /* 15056 * The command did not successfully complete as requested so process 15057 * the error, retry, and/or attempt recovery. 15058 */ 15059 switch (pktp->pkt_reason) { 15060 case CMD_CMPLT: 15061 switch (SD_GET_PKT_STATUS(pktp)) { 15062 case STATUS_GOOD: 15063 /* 15064 * The command completed successfully with a non-zero 15065 * residual 15066 */ 15067 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15068 "sdintr: STATUS_GOOD \n"); 15069 sd_pkt_status_good(un, bp, xp, pktp); 15070 break; 15071 15072 case STATUS_CHECK: 15073 case STATUS_TERMINATED: 15074 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15075 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 15076 sd_pkt_status_check_condition(un, bp, xp, pktp); 15077 break; 15078 15079 case STATUS_BUSY: 15080 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15081 "sdintr: STATUS_BUSY\n"); 15082 sd_pkt_status_busy(un, bp, xp, pktp); 15083 break; 15084 15085 case STATUS_RESERVATION_CONFLICT: 15086 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15087 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 15088 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 15089 break; 15090 15091 case STATUS_QFULL: 15092 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15093 "sdintr: STATUS_QFULL\n"); 15094 sd_pkt_status_qfull(un, bp, xp, pktp); 15095 break; 15096 15097 case STATUS_MET: 15098 case STATUS_INTERMEDIATE: 15099 case STATUS_SCSI2: 15100 case STATUS_INTERMEDIATE_MET: 15101 case STATUS_ACA_ACTIVE: 15102 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 15103 "Unexpected SCSI status received: 0x%x\n", 15104 SD_GET_PKT_STATUS(pktp)); 15105 sd_return_failed_command(un, bp, EIO); 15106 break; 15107 15108 default: 15109 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 15110 "Invalid SCSI status received: 0x%x\n", 15111 SD_GET_PKT_STATUS(pktp)); 15112 sd_return_failed_command(un, bp, EIO); 15113 break; 15114 15115 } 15116 break; 15117 15118 case CMD_INCOMPLETE: 15119 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15120 "sdintr: CMD_INCOMPLETE\n"); 15121 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 15122 break; 15123 case CMD_TRAN_ERR: 15124 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15125 "sdintr: CMD_TRAN_ERR\n"); 15126 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 15127 break; 15128 case CMD_RESET: 15129 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15130 "sdintr: CMD_RESET \n"); 15131 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 15132 break; 15133 case CMD_ABORTED: 15134 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15135 "sdintr: CMD_ABORTED \n"); 15136 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 15137 break; 15138 case CMD_TIMEOUT: 15139 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15140 "sdintr: CMD_TIMEOUT\n"); 15141 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 15142 break; 15143 case CMD_UNX_BUS_FREE: 15144 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15145 "sdintr: CMD_UNX_BUS_FREE \n"); 15146 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 15147 break; 15148 case CMD_TAG_REJECT: 15149 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15150 "sdintr: CMD_TAG_REJECT\n"); 15151 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 15152 break; 15153 default: 15154 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15155 "sdintr: default\n"); 15156 sd_pkt_reason_default(un, bp, xp, pktp); 15157 break; 15158 } 15159 15160 exit: 15161 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 15162 15163 /* Decrement counter to indicate that the callback routine is done. */ 15164 un->un_in_callback--; 15165 ASSERT(un->un_in_callback >= 0); 15166 15167 /* 15168 * At this point, the pkt has been dispatched, ie, it is either 15169 * being re-tried or has been returned to its caller and should 15170 * not be referenced. 15171 */ 15172 15173 mutex_exit(SD_MUTEX(un)); 15174 } 15175 15176 15177 /* 15178 * Function: sd_print_incomplete_msg 15179 * 15180 * Description: Prints the error message for a CMD_INCOMPLETE error. 15181 * 15182 * Arguments: un - ptr to associated softstate for the device. 15183 * bp - ptr to the buf(9S) for the command. 15184 * arg - message string ptr 15185 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 15186 * or SD_NO_RETRY_ISSUED. 15187 * 15188 * Context: May be called under interrupt context 15189 */ 15190 15191 static void 15192 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 15193 { 15194 struct scsi_pkt *pktp; 15195 char *msgp; 15196 char *cmdp = arg; 15197 15198 ASSERT(un != NULL); 15199 ASSERT(mutex_owned(SD_MUTEX(un))); 15200 ASSERT(bp != NULL); 15201 ASSERT(arg != NULL); 15202 pktp = SD_GET_PKTP(bp); 15203 ASSERT(pktp != NULL); 15204 15205 switch (code) { 15206 case SD_DELAYED_RETRY_ISSUED: 15207 case SD_IMMEDIATE_RETRY_ISSUED: 15208 msgp = "retrying"; 15209 break; 15210 case SD_NO_RETRY_ISSUED: 15211 default: 15212 msgp = "giving up"; 15213 break; 15214 } 15215 15216 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 15217 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15218 "incomplete %s- %s\n", cmdp, msgp); 15219 } 15220 } 15221 15222 15223 15224 /* 15225 * Function: sd_pkt_status_good 15226 * 15227 * Description: Processing for a STATUS_GOOD code in pkt_status. 15228 * 15229 * Context: May be called under interrupt context 15230 */ 15231 15232 static void 15233 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 15234 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15235 { 15236 char *cmdp; 15237 15238 ASSERT(un != NULL); 15239 ASSERT(mutex_owned(SD_MUTEX(un))); 15240 ASSERT(bp != NULL); 15241 ASSERT(xp != NULL); 15242 ASSERT(pktp != NULL); 15243 ASSERT(pktp->pkt_reason == CMD_CMPLT); 15244 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 15245 ASSERT(pktp->pkt_resid != 0); 15246 15247 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 15248 15249 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15250 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 15251 case SCMD_READ: 15252 cmdp = "read"; 15253 break; 15254 case SCMD_WRITE: 15255 cmdp = "write"; 15256 break; 15257 default: 15258 SD_UPDATE_B_RESID(bp, pktp); 15259 sd_return_command(un, bp); 15260 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 15261 return; 15262 } 15263 15264 /* 15265 * See if we can retry the read/write, preferrably immediately. 15266 * If retries are exhaused, then sd_retry_command() will update 15267 * the b_resid count. 15268 */ 15269 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 15270 cmdp, EIO, (clock_t)0, NULL); 15271 15272 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 15273 } 15274 15275 15276 15277 15278 15279 /* 15280 * Function: sd_handle_request_sense 15281 * 15282 * Description: Processing for non-auto Request Sense command. 15283 * 15284 * Arguments: un - ptr to associated softstate 15285 * sense_bp - ptr to buf(9S) for the RQS command 15286 * sense_xp - ptr to the sd_xbuf for the RQS command 15287 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 15288 * 15289 * Context: May be called under interrupt context 15290 */ 15291 15292 static void 15293 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 15294 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 15295 { 15296 struct buf *cmd_bp; /* buf for the original command */ 15297 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 15298 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 15299 size_t actual_len; /* actual sense data length */ 15300 15301 ASSERT(un != NULL); 15302 ASSERT(mutex_owned(SD_MUTEX(un))); 15303 ASSERT(sense_bp != NULL); 15304 ASSERT(sense_xp != NULL); 15305 ASSERT(sense_pktp != NULL); 15306 15307 /* 15308 * Note the sense_bp, sense_xp, and sense_pktp here are for the 15309 * RQS command and not the original command. 15310 */ 15311 ASSERT(sense_pktp == un->un_rqs_pktp); 15312 ASSERT(sense_bp == un->un_rqs_bp); 15313 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 15314 (FLAG_SENSING | FLAG_HEAD)); 15315 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 15316 FLAG_SENSING) == FLAG_SENSING); 15317 15318 /* These are the bp, xp, and pktp for the original command */ 15319 cmd_bp = sense_xp->xb_sense_bp; 15320 cmd_xp = SD_GET_XBUF(cmd_bp); 15321 cmd_pktp = SD_GET_PKTP(cmd_bp); 15322 15323 if (sense_pktp->pkt_reason != CMD_CMPLT) { 15324 /* 15325 * The REQUEST SENSE command failed. Release the REQUEST 15326 * SENSE command for re-use, get back the bp for the original 15327 * command, and attempt to re-try the original command if 15328 * FLAG_DIAGNOSE is not set in the original packet. 15329 */ 15330 SD_UPDATE_ERRSTATS(un, sd_harderrs); 15331 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15332 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 15333 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 15334 NULL, NULL, EIO, (clock_t)0, NULL); 15335 return; 15336 } 15337 } 15338 15339 /* 15340 * Save the relevant sense info into the xp for the original cmd. 15341 * 15342 * Note: if the request sense failed the state info will be zero 15343 * as set in sd_mark_rqs_busy() 15344 */ 15345 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 15346 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 15347 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid; 15348 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) && 15349 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen > 15350 SENSE_LENGTH)) { 15351 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 15352 MAX_SENSE_LENGTH); 15353 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 15354 } else { 15355 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, 15356 SENSE_LENGTH); 15357 if (actual_len < SENSE_LENGTH) { 15358 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len; 15359 } else { 15360 cmd_xp->xb_sense_resid = 0; 15361 } 15362 } 15363 15364 /* 15365 * Free up the RQS command.... 15366 * NOTE: 15367 * Must do this BEFORE calling sd_validate_sense_data! 15368 * sd_validate_sense_data may return the original command in 15369 * which case the pkt will be freed and the flags can no 15370 * longer be touched. 15371 * SD_MUTEX is held through this process until the command 15372 * is dispatched based upon the sense data, so there are 15373 * no race conditions. 15374 */ 15375 (void) sd_mark_rqs_idle(un, sense_xp); 15376 15377 /* 15378 * For a retryable command see if we have valid sense data, if so then 15379 * turn it over to sd_decode_sense() to figure out the right course of 15380 * action. Just fail a non-retryable command. 15381 */ 15382 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 15383 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) == 15384 SD_SENSE_DATA_IS_VALID) { 15385 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 15386 } 15387 } else { 15388 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 15389 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15390 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 15391 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15392 sd_return_failed_command(un, cmd_bp, EIO); 15393 } 15394 } 15395 15396 15397 15398 15399 /* 15400 * Function: sd_handle_auto_request_sense 15401 * 15402 * Description: Processing for auto-request sense information. 15403 * 15404 * Arguments: un - ptr to associated softstate 15405 * bp - ptr to buf(9S) for the command 15406 * xp - ptr to the sd_xbuf for the command 15407 * pktp - ptr to the scsi_pkt(9S) for the command 15408 * 15409 * Context: May be called under interrupt context 15410 */ 15411 15412 static void 15413 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 15414 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15415 { 15416 struct scsi_arq_status *asp; 15417 size_t actual_len; 15418 15419 ASSERT(un != NULL); 15420 ASSERT(mutex_owned(SD_MUTEX(un))); 15421 ASSERT(bp != NULL); 15422 ASSERT(xp != NULL); 15423 ASSERT(pktp != NULL); 15424 ASSERT(pktp != un->un_rqs_pktp); 15425 ASSERT(bp != un->un_rqs_bp); 15426 15427 /* 15428 * For auto-request sense, we get a scsi_arq_status back from 15429 * the HBA, with the sense data in the sts_sensedata member. 15430 * The pkt_scbp of the packet points to this scsi_arq_status. 15431 */ 15432 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 15433 15434 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 15435 /* 15436 * The auto REQUEST SENSE failed; see if we can re-try 15437 * the original command. 15438 */ 15439 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15440 "auto request sense failed (reason=%s)\n", 15441 scsi_rname(asp->sts_rqpkt_reason)); 15442 15443 sd_reset_target(un, pktp); 15444 15445 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15446 NULL, NULL, EIO, (clock_t)0, NULL); 15447 return; 15448 } 15449 15450 /* Save the relevant sense info into the xp for the original cmd. */ 15451 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 15452 xp->xb_sense_state = asp->sts_rqpkt_state; 15453 xp->xb_sense_resid = asp->sts_rqpkt_resid; 15454 if (xp->xb_sense_state & STATE_XARQ_DONE) { 15455 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 15456 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 15457 MAX_SENSE_LENGTH); 15458 } else { 15459 if (xp->xb_sense_resid > SENSE_LENGTH) { 15460 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid; 15461 } else { 15462 actual_len = SENSE_LENGTH - xp->xb_sense_resid; 15463 } 15464 if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 15465 xp->xb_sense_resid = (int)(((struct uscsi_cmd *) 15466 (xp->xb_pktinfo))->uscsi_rqlen) - actual_len; 15467 } 15468 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH); 15469 } 15470 15471 /* 15472 * See if we have valid sense data, if so then turn it over to 15473 * sd_decode_sense() to figure out the right course of action. 15474 */ 15475 if (sd_validate_sense_data(un, bp, xp, actual_len) == 15476 SD_SENSE_DATA_IS_VALID) { 15477 sd_decode_sense(un, bp, xp, pktp); 15478 } 15479 } 15480 15481 15482 /* 15483 * Function: sd_print_sense_failed_msg 15484 * 15485 * Description: Print log message when RQS has failed. 15486 * 15487 * Arguments: un - ptr to associated softstate 15488 * bp - ptr to buf(9S) for the command 15489 * arg - generic message string ptr 15490 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 15491 * or SD_NO_RETRY_ISSUED 15492 * 15493 * Context: May be called from interrupt context 15494 */ 15495 15496 static void 15497 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 15498 int code) 15499 { 15500 char *msgp = arg; 15501 15502 ASSERT(un != NULL); 15503 ASSERT(mutex_owned(SD_MUTEX(un))); 15504 ASSERT(bp != NULL); 15505 15506 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 15507 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 15508 } 15509 } 15510 15511 15512 /* 15513 * Function: sd_validate_sense_data 15514 * 15515 * Description: Check the given sense data for validity. 15516 * If the sense data is not valid, the command will 15517 * be either failed or retried! 15518 * 15519 * Return Code: SD_SENSE_DATA_IS_INVALID 15520 * SD_SENSE_DATA_IS_VALID 15521 * 15522 * Context: May be called from interrupt context 15523 */ 15524 15525 static int 15526 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 15527 size_t actual_len) 15528 { 15529 struct scsi_extended_sense *esp; 15530 struct scsi_pkt *pktp; 15531 char *msgp = NULL; 15532 15533 ASSERT(un != NULL); 15534 ASSERT(mutex_owned(SD_MUTEX(un))); 15535 ASSERT(bp != NULL); 15536 ASSERT(bp != un->un_rqs_bp); 15537 ASSERT(xp != NULL); 15538 15539 pktp = SD_GET_PKTP(bp); 15540 ASSERT(pktp != NULL); 15541 15542 /* 15543 * Check the status of the RQS command (auto or manual). 15544 */ 15545 switch (xp->xb_sense_status & STATUS_MASK) { 15546 case STATUS_GOOD: 15547 break; 15548 15549 case STATUS_RESERVATION_CONFLICT: 15550 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 15551 return (SD_SENSE_DATA_IS_INVALID); 15552 15553 case STATUS_BUSY: 15554 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15555 "Busy Status on REQUEST SENSE\n"); 15556 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 15557 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 15558 return (SD_SENSE_DATA_IS_INVALID); 15559 15560 case STATUS_QFULL: 15561 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15562 "QFULL Status on REQUEST SENSE\n"); 15563 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 15564 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 15565 return (SD_SENSE_DATA_IS_INVALID); 15566 15567 case STATUS_CHECK: 15568 case STATUS_TERMINATED: 15569 msgp = "Check Condition on REQUEST SENSE\n"; 15570 goto sense_failed; 15571 15572 default: 15573 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 15574 goto sense_failed; 15575 } 15576 15577 /* 15578 * See if we got the minimum required amount of sense data. 15579 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 15580 * or less. 15581 */ 15582 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 15583 (actual_len == 0)) { 15584 msgp = "Request Sense couldn't get sense data\n"; 15585 goto sense_failed; 15586 } 15587 15588 if (actual_len < SUN_MIN_SENSE_LENGTH) { 15589 msgp = "Not enough sense information\n"; 15590 goto sense_failed; 15591 } 15592 15593 /* 15594 * We require the extended sense data 15595 */ 15596 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 15597 if (esp->es_class != CLASS_EXTENDED_SENSE) { 15598 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 15599 static char tmp[8]; 15600 static char buf[148]; 15601 char *p = (char *)(xp->xb_sense_data); 15602 int i; 15603 15604 mutex_enter(&sd_sense_mutex); 15605 (void) strcpy(buf, "undecodable sense information:"); 15606 for (i = 0; i < actual_len; i++) { 15607 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 15608 (void) strcpy(&buf[strlen(buf)], tmp); 15609 } 15610 i = strlen(buf); 15611 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 15612 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 15613 mutex_exit(&sd_sense_mutex); 15614 } 15615 /* Note: Legacy behavior, fail the command with no retry */ 15616 sd_return_failed_command(un, bp, EIO); 15617 return (SD_SENSE_DATA_IS_INVALID); 15618 } 15619 15620 /* 15621 * Check that es_code is valid (es_class concatenated with es_code 15622 * make up the "response code" field. es_class will always be 7, so 15623 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 15624 * format. 15625 */ 15626 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 15627 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 15628 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 15629 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 15630 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 15631 goto sense_failed; 15632 } 15633 15634 return (SD_SENSE_DATA_IS_VALID); 15635 15636 sense_failed: 15637 /* 15638 * If the request sense failed (for whatever reason), attempt 15639 * to retry the original command. 15640 */ 15641 #if defined(__i386) || defined(__amd64) 15642 /* 15643 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 15644 * sddef.h for Sparc platform, and x86 uses 1 binary 15645 * for both SCSI/FC. 15646 * The SD_RETRY_DELAY value need to be adjusted here 15647 * when SD_RETRY_DELAY change in sddef.h 15648 */ 15649 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15650 sd_print_sense_failed_msg, msgp, EIO, 15651 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 15652 #else 15653 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15654 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 15655 #endif 15656 15657 return (SD_SENSE_DATA_IS_INVALID); 15658 } 15659 15660 15661 15662 /* 15663 * Function: sd_decode_sense 15664 * 15665 * Description: Take recovery action(s) when SCSI Sense Data is received. 15666 * 15667 * Context: Interrupt context. 15668 */ 15669 15670 static void 15671 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 15672 struct scsi_pkt *pktp) 15673 { 15674 uint8_t sense_key; 15675 15676 ASSERT(un != NULL); 15677 ASSERT(mutex_owned(SD_MUTEX(un))); 15678 ASSERT(bp != NULL); 15679 ASSERT(bp != un->un_rqs_bp); 15680 ASSERT(xp != NULL); 15681 ASSERT(pktp != NULL); 15682 15683 sense_key = scsi_sense_key(xp->xb_sense_data); 15684 15685 switch (sense_key) { 15686 case KEY_NO_SENSE: 15687 sd_sense_key_no_sense(un, bp, xp, pktp); 15688 break; 15689 case KEY_RECOVERABLE_ERROR: 15690 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 15691 bp, xp, pktp); 15692 break; 15693 case KEY_NOT_READY: 15694 sd_sense_key_not_ready(un, xp->xb_sense_data, 15695 bp, xp, pktp); 15696 break; 15697 case KEY_MEDIUM_ERROR: 15698 case KEY_HARDWARE_ERROR: 15699 sd_sense_key_medium_or_hardware_error(un, 15700 xp->xb_sense_data, bp, xp, pktp); 15701 break; 15702 case KEY_ILLEGAL_REQUEST: 15703 sd_sense_key_illegal_request(un, bp, xp, pktp); 15704 break; 15705 case KEY_UNIT_ATTENTION: 15706 sd_sense_key_unit_attention(un, xp->xb_sense_data, 15707 bp, xp, pktp); 15708 break; 15709 case KEY_WRITE_PROTECT: 15710 case KEY_VOLUME_OVERFLOW: 15711 case KEY_MISCOMPARE: 15712 sd_sense_key_fail_command(un, bp, xp, pktp); 15713 break; 15714 case KEY_BLANK_CHECK: 15715 sd_sense_key_blank_check(un, bp, xp, pktp); 15716 break; 15717 case KEY_ABORTED_COMMAND: 15718 sd_sense_key_aborted_command(un, bp, xp, pktp); 15719 break; 15720 case KEY_VENDOR_UNIQUE: 15721 case KEY_COPY_ABORTED: 15722 case KEY_EQUAL: 15723 case KEY_RESERVED: 15724 default: 15725 sd_sense_key_default(un, xp->xb_sense_data, 15726 bp, xp, pktp); 15727 break; 15728 } 15729 } 15730 15731 15732 /* 15733 * Function: sd_dump_memory 15734 * 15735 * Description: Debug logging routine to print the contents of a user provided 15736 * buffer. The output of the buffer is broken up into 256 byte 15737 * segments due to a size constraint of the scsi_log. 15738 * implementation. 15739 * 15740 * Arguments: un - ptr to softstate 15741 * comp - component mask 15742 * title - "title" string to preceed data when printed 15743 * data - ptr to data block to be printed 15744 * len - size of data block to be printed 15745 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 15746 * 15747 * Context: May be called from interrupt context 15748 */ 15749 15750 #define SD_DUMP_MEMORY_BUF_SIZE 256 15751 15752 static char *sd_dump_format_string[] = { 15753 " 0x%02x", 15754 " %c" 15755 }; 15756 15757 static void 15758 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 15759 int len, int fmt) 15760 { 15761 int i, j; 15762 int avail_count; 15763 int start_offset; 15764 int end_offset; 15765 size_t entry_len; 15766 char *bufp; 15767 char *local_buf; 15768 char *format_string; 15769 15770 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 15771 15772 /* 15773 * In the debug version of the driver, this function is called from a 15774 * number of places which are NOPs in the release driver. 15775 * The debug driver therefore has additional methods of filtering 15776 * debug output. 15777 */ 15778 #ifdef SDDEBUG 15779 /* 15780 * In the debug version of the driver we can reduce the amount of debug 15781 * messages by setting sd_error_level to something other than 15782 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 15783 * sd_component_mask. 15784 */ 15785 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 15786 (sd_error_level != SCSI_ERR_ALL)) { 15787 return; 15788 } 15789 if (((sd_component_mask & comp) == 0) || 15790 (sd_error_level != SCSI_ERR_ALL)) { 15791 return; 15792 } 15793 #else 15794 if (sd_error_level != SCSI_ERR_ALL) { 15795 return; 15796 } 15797 #endif 15798 15799 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 15800 bufp = local_buf; 15801 /* 15802 * Available length is the length of local_buf[], minus the 15803 * length of the title string, minus one for the ":", minus 15804 * one for the newline, minus one for the NULL terminator. 15805 * This gives the #bytes available for holding the printed 15806 * values from the given data buffer. 15807 */ 15808 if (fmt == SD_LOG_HEX) { 15809 format_string = sd_dump_format_string[0]; 15810 } else /* SD_LOG_CHAR */ { 15811 format_string = sd_dump_format_string[1]; 15812 } 15813 /* 15814 * Available count is the number of elements from the given 15815 * data buffer that we can fit into the available length. 15816 * This is based upon the size of the format string used. 15817 * Make one entry and find it's size. 15818 */ 15819 (void) sprintf(bufp, format_string, data[0]); 15820 entry_len = strlen(bufp); 15821 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 15822 15823 j = 0; 15824 while (j < len) { 15825 bufp = local_buf; 15826 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 15827 start_offset = j; 15828 15829 end_offset = start_offset + avail_count; 15830 15831 (void) sprintf(bufp, "%s:", title); 15832 bufp += strlen(bufp); 15833 for (i = start_offset; ((i < end_offset) && (j < len)); 15834 i++, j++) { 15835 (void) sprintf(bufp, format_string, data[i]); 15836 bufp += entry_len; 15837 } 15838 (void) sprintf(bufp, "\n"); 15839 15840 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 15841 } 15842 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 15843 } 15844 15845 /* 15846 * Function: sd_print_sense_msg 15847 * 15848 * Description: Log a message based upon the given sense data. 15849 * 15850 * Arguments: un - ptr to associated softstate 15851 * bp - ptr to buf(9S) for the command 15852 * arg - ptr to associate sd_sense_info struct 15853 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 15854 * or SD_NO_RETRY_ISSUED 15855 * 15856 * Context: May be called from interrupt context 15857 */ 15858 15859 static void 15860 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 15861 { 15862 struct sd_xbuf *xp; 15863 struct scsi_pkt *pktp; 15864 uint8_t *sensep; 15865 daddr_t request_blkno; 15866 diskaddr_t err_blkno; 15867 int severity; 15868 int pfa_flag; 15869 extern struct scsi_key_strings scsi_cmds[]; 15870 15871 ASSERT(un != NULL); 15872 ASSERT(mutex_owned(SD_MUTEX(un))); 15873 ASSERT(bp != NULL); 15874 xp = SD_GET_XBUF(bp); 15875 ASSERT(xp != NULL); 15876 pktp = SD_GET_PKTP(bp); 15877 ASSERT(pktp != NULL); 15878 ASSERT(arg != NULL); 15879 15880 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 15881 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 15882 15883 if ((code == SD_DELAYED_RETRY_ISSUED) || 15884 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 15885 severity = SCSI_ERR_RETRYABLE; 15886 } 15887 15888 /* Use absolute block number for the request block number */ 15889 request_blkno = xp->xb_blkno; 15890 15891 /* 15892 * Now try to get the error block number from the sense data 15893 */ 15894 sensep = xp->xb_sense_data; 15895 15896 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 15897 (uint64_t *)&err_blkno)) { 15898 /* 15899 * We retrieved the error block number from the information 15900 * portion of the sense data. 15901 * 15902 * For USCSI commands we are better off using the error 15903 * block no. as the requested block no. (This is the best 15904 * we can estimate.) 15905 */ 15906 if ((SD_IS_BUFIO(xp) == FALSE) && 15907 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 15908 request_blkno = err_blkno; 15909 } 15910 } else { 15911 /* 15912 * Without the es_valid bit set (for fixed format) or an 15913 * information descriptor (for descriptor format) we cannot 15914 * be certain of the error blkno, so just use the 15915 * request_blkno. 15916 */ 15917 err_blkno = (diskaddr_t)request_blkno; 15918 } 15919 15920 /* 15921 * The following will log the buffer contents for the release driver 15922 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 15923 * level is set to verbose. 15924 */ 15925 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 15926 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15927 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15928 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 15929 15930 if (pfa_flag == FALSE) { 15931 /* This is normally only set for USCSI */ 15932 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 15933 return; 15934 } 15935 15936 if ((SD_IS_BUFIO(xp) == TRUE) && 15937 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 15938 (severity < sd_error_level))) { 15939 return; 15940 } 15941 } 15942 15943 /* 15944 * Check for Sonoma Failover and keep a count of how many failed I/O's 15945 */ 15946 if ((SD_IS_LSI(un)) && 15947 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 15948 (scsi_sense_asc(sensep) == 0x94) && 15949 (scsi_sense_ascq(sensep) == 0x01)) { 15950 un->un_sonoma_failure_count++; 15951 if (un->un_sonoma_failure_count > 1) { 15952 return; 15953 } 15954 } 15955 15956 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 15957 request_blkno, err_blkno, scsi_cmds, 15958 (struct scsi_extended_sense *)sensep, 15959 un->un_additional_codes, NULL); 15960 } 15961 15962 /* 15963 * Function: sd_sense_key_no_sense 15964 * 15965 * Description: Recovery action when sense data was not received. 15966 * 15967 * Context: May be called from interrupt context 15968 */ 15969 15970 static void 15971 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 15972 struct sd_xbuf *xp, struct scsi_pkt *pktp) 15973 { 15974 struct sd_sense_info si; 15975 15976 ASSERT(un != NULL); 15977 ASSERT(mutex_owned(SD_MUTEX(un))); 15978 ASSERT(bp != NULL); 15979 ASSERT(xp != NULL); 15980 ASSERT(pktp != NULL); 15981 15982 si.ssi_severity = SCSI_ERR_FATAL; 15983 si.ssi_pfa_flag = FALSE; 15984 15985 SD_UPDATE_ERRSTATS(un, sd_softerrs); 15986 15987 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 15988 &si, EIO, (clock_t)0, NULL); 15989 } 15990 15991 15992 /* 15993 * Function: sd_sense_key_recoverable_error 15994 * 15995 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 15996 * 15997 * Context: May be called from interrupt context 15998 */ 15999 16000 static void 16001 sd_sense_key_recoverable_error(struct sd_lun *un, 16002 uint8_t *sense_datap, 16003 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16004 { 16005 struct sd_sense_info si; 16006 uint8_t asc = scsi_sense_asc(sense_datap); 16007 16008 ASSERT(un != NULL); 16009 ASSERT(mutex_owned(SD_MUTEX(un))); 16010 ASSERT(bp != NULL); 16011 ASSERT(xp != NULL); 16012 ASSERT(pktp != NULL); 16013 16014 /* 16015 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 16016 */ 16017 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 16018 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 16019 si.ssi_severity = SCSI_ERR_INFO; 16020 si.ssi_pfa_flag = TRUE; 16021 } else { 16022 SD_UPDATE_ERRSTATS(un, sd_softerrs); 16023 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 16024 si.ssi_severity = SCSI_ERR_RECOVERED; 16025 si.ssi_pfa_flag = FALSE; 16026 } 16027 16028 if (pktp->pkt_resid == 0) { 16029 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16030 sd_return_command(un, bp); 16031 return; 16032 } 16033 16034 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16035 &si, EIO, (clock_t)0, NULL); 16036 } 16037 16038 16039 16040 16041 /* 16042 * Function: sd_sense_key_not_ready 16043 * 16044 * Description: Recovery actions for a SCSI "Not Ready" sense key. 16045 * 16046 * Context: May be called from interrupt context 16047 */ 16048 16049 static void 16050 sd_sense_key_not_ready(struct sd_lun *un, 16051 uint8_t *sense_datap, 16052 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16053 { 16054 struct sd_sense_info si; 16055 uint8_t asc = scsi_sense_asc(sense_datap); 16056 uint8_t ascq = scsi_sense_ascq(sense_datap); 16057 16058 ASSERT(un != NULL); 16059 ASSERT(mutex_owned(SD_MUTEX(un))); 16060 ASSERT(bp != NULL); 16061 ASSERT(xp != NULL); 16062 ASSERT(pktp != NULL); 16063 16064 si.ssi_severity = SCSI_ERR_FATAL; 16065 si.ssi_pfa_flag = FALSE; 16066 16067 /* 16068 * Update error stats after first NOT READY error. Disks may have 16069 * been powered down and may need to be restarted. For CDROMs, 16070 * report NOT READY errors only if media is present. 16071 */ 16072 if ((ISCD(un) && (asc == 0x3A)) || 16073 (xp->xb_nr_retry_count > 0)) { 16074 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16075 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 16076 } 16077 16078 /* 16079 * Just fail if the "not ready" retry limit has been reached. 16080 */ 16081 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) { 16082 /* Special check for error message printing for removables. */ 16083 if (un->un_f_has_removable_media && (asc == 0x04) && 16084 (ascq >= 0x04)) { 16085 si.ssi_severity = SCSI_ERR_ALL; 16086 } 16087 goto fail_command; 16088 } 16089 16090 /* 16091 * Check the ASC and ASCQ in the sense data as needed, to determine 16092 * what to do. 16093 */ 16094 switch (asc) { 16095 case 0x04: /* LOGICAL UNIT NOT READY */ 16096 /* 16097 * disk drives that don't spin up result in a very long delay 16098 * in format without warning messages. We will log a message 16099 * if the error level is set to verbose. 16100 */ 16101 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16102 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16103 "logical unit not ready, resetting disk\n"); 16104 } 16105 16106 /* 16107 * There are different requirements for CDROMs and disks for 16108 * the number of retries. If a CD-ROM is giving this, it is 16109 * probably reading TOC and is in the process of getting 16110 * ready, so we should keep on trying for a long time to make 16111 * sure that all types of media are taken in account (for 16112 * some media the drive takes a long time to read TOC). For 16113 * disks we do not want to retry this too many times as this 16114 * can cause a long hang in format when the drive refuses to 16115 * spin up (a very common failure). 16116 */ 16117 switch (ascq) { 16118 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 16119 /* 16120 * Disk drives frequently refuse to spin up which 16121 * results in a very long hang in format without 16122 * warning messages. 16123 * 16124 * Note: This code preserves the legacy behavior of 16125 * comparing xb_nr_retry_count against zero for fibre 16126 * channel targets instead of comparing against the 16127 * un_reset_retry_count value. The reason for this 16128 * discrepancy has been so utterly lost beneath the 16129 * Sands of Time that even Indiana Jones could not 16130 * find it. 16131 */ 16132 if (un->un_f_is_fibre == TRUE) { 16133 if (((sd_level_mask & SD_LOGMASK_DIAG) || 16134 (xp->xb_nr_retry_count > 0)) && 16135 (un->un_startstop_timeid == NULL)) { 16136 scsi_log(SD_DEVINFO(un), sd_label, 16137 CE_WARN, "logical unit not ready, " 16138 "resetting disk\n"); 16139 sd_reset_target(un, pktp); 16140 } 16141 } else { 16142 if (((sd_level_mask & SD_LOGMASK_DIAG) || 16143 (xp->xb_nr_retry_count > 16144 un->un_reset_retry_count)) && 16145 (un->un_startstop_timeid == NULL)) { 16146 scsi_log(SD_DEVINFO(un), sd_label, 16147 CE_WARN, "logical unit not ready, " 16148 "resetting disk\n"); 16149 sd_reset_target(un, pktp); 16150 } 16151 } 16152 break; 16153 16154 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 16155 /* 16156 * If the target is in the process of becoming 16157 * ready, just proceed with the retry. This can 16158 * happen with CD-ROMs that take a long time to 16159 * read TOC after a power cycle or reset. 16160 */ 16161 goto do_retry; 16162 16163 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 16164 break; 16165 16166 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 16167 /* 16168 * Retries cannot help here so just fail right away. 16169 */ 16170 goto fail_command; 16171 16172 case 0x88: 16173 /* 16174 * Vendor-unique code for T3/T4: it indicates a 16175 * path problem in a mutipathed config, but as far as 16176 * the target driver is concerned it equates to a fatal 16177 * error, so we should just fail the command right away 16178 * (without printing anything to the console). If this 16179 * is not a T3/T4, fall thru to the default recovery 16180 * action. 16181 * T3/T4 is FC only, don't need to check is_fibre 16182 */ 16183 if (SD_IS_T3(un) || SD_IS_T4(un)) { 16184 sd_return_failed_command(un, bp, EIO); 16185 return; 16186 } 16187 /* FALLTHRU */ 16188 16189 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 16190 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 16191 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 16192 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 16193 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 16194 default: /* Possible future codes in SCSI spec? */ 16195 /* 16196 * For removable-media devices, do not retry if 16197 * ASCQ > 2 as these result mostly from USCSI commands 16198 * on MMC devices issued to check status of an 16199 * operation initiated in immediate mode. Also for 16200 * ASCQ >= 4 do not print console messages as these 16201 * mainly represent a user-initiated operation 16202 * instead of a system failure. 16203 */ 16204 if (un->un_f_has_removable_media) { 16205 si.ssi_severity = SCSI_ERR_ALL; 16206 goto fail_command; 16207 } 16208 break; 16209 } 16210 16211 /* 16212 * As part of our recovery attempt for the NOT READY 16213 * condition, we issue a START STOP UNIT command. However 16214 * we want to wait for a short delay before attempting this 16215 * as there may still be more commands coming back from the 16216 * target with the check condition. To do this we use 16217 * timeout(9F) to call sd_start_stop_unit_callback() after 16218 * the delay interval expires. (sd_start_stop_unit_callback() 16219 * dispatches sd_start_stop_unit_task(), which will issue 16220 * the actual START STOP UNIT command. The delay interval 16221 * is one-half of the delay that we will use to retry the 16222 * command that generated the NOT READY condition. 16223 * 16224 * Note that we could just dispatch sd_start_stop_unit_task() 16225 * from here and allow it to sleep for the delay interval, 16226 * but then we would be tying up the taskq thread 16227 * uncesessarily for the duration of the delay. 16228 * 16229 * Do not issue the START STOP UNIT if the current command 16230 * is already a START STOP UNIT. 16231 */ 16232 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 16233 break; 16234 } 16235 16236 /* 16237 * Do not schedule the timeout if one is already pending. 16238 */ 16239 if (un->un_startstop_timeid != NULL) { 16240 SD_INFO(SD_LOG_ERROR, un, 16241 "sd_sense_key_not_ready: restart already issued to" 16242 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 16243 ddi_get_instance(SD_DEVINFO(un))); 16244 break; 16245 } 16246 16247 /* 16248 * Schedule the START STOP UNIT command, then queue the command 16249 * for a retry. 16250 * 16251 * Note: A timeout is not scheduled for this retry because we 16252 * want the retry to be serial with the START_STOP_UNIT. The 16253 * retry will be started when the START_STOP_UNIT is completed 16254 * in sd_start_stop_unit_task. 16255 */ 16256 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 16257 un, SD_BSY_TIMEOUT / 2); 16258 xp->xb_nr_retry_count++; 16259 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 16260 return; 16261 16262 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 16263 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16264 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16265 "unit does not respond to selection\n"); 16266 } 16267 break; 16268 16269 case 0x3A: /* MEDIUM NOT PRESENT */ 16270 if (sd_error_level >= SCSI_ERR_FATAL) { 16271 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16272 "Caddy not inserted in drive\n"); 16273 } 16274 16275 sr_ejected(un); 16276 un->un_mediastate = DKIO_EJECTED; 16277 /* The state has changed, inform the media watch routines */ 16278 cv_broadcast(&un->un_state_cv); 16279 /* Just fail if no media is present in the drive. */ 16280 goto fail_command; 16281 16282 default: 16283 if (sd_error_level < SCSI_ERR_RETRYABLE) { 16284 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 16285 "Unit not Ready. Additional sense code 0x%x\n", 16286 asc); 16287 } 16288 break; 16289 } 16290 16291 do_retry: 16292 16293 /* 16294 * Retry the command, as some targets may report NOT READY for 16295 * several seconds after being reset. 16296 */ 16297 xp->xb_nr_retry_count++; 16298 si.ssi_severity = SCSI_ERR_RETRYABLE; 16299 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 16300 &si, EIO, SD_BSY_TIMEOUT, NULL); 16301 16302 return; 16303 16304 fail_command: 16305 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16306 sd_return_failed_command(un, bp, EIO); 16307 } 16308 16309 16310 16311 /* 16312 * Function: sd_sense_key_medium_or_hardware_error 16313 * 16314 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 16315 * sense key. 16316 * 16317 * Context: May be called from interrupt context 16318 */ 16319 16320 static void 16321 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 16322 uint8_t *sense_datap, 16323 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16324 { 16325 struct sd_sense_info si; 16326 uint8_t sense_key = scsi_sense_key(sense_datap); 16327 uint8_t asc = scsi_sense_asc(sense_datap); 16328 16329 ASSERT(un != NULL); 16330 ASSERT(mutex_owned(SD_MUTEX(un))); 16331 ASSERT(bp != NULL); 16332 ASSERT(xp != NULL); 16333 ASSERT(pktp != NULL); 16334 16335 si.ssi_severity = SCSI_ERR_FATAL; 16336 si.ssi_pfa_flag = FALSE; 16337 16338 if (sense_key == KEY_MEDIUM_ERROR) { 16339 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 16340 } 16341 16342 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16343 16344 if ((un->un_reset_retry_count != 0) && 16345 (xp->xb_retry_count == un->un_reset_retry_count)) { 16346 mutex_exit(SD_MUTEX(un)); 16347 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 16348 if (un->un_f_allow_bus_device_reset == TRUE) { 16349 16350 boolean_t try_resetting_target = B_TRUE; 16351 16352 /* 16353 * We need to be able to handle specific ASC when we are 16354 * handling a KEY_HARDWARE_ERROR. In particular 16355 * taking the default action of resetting the target may 16356 * not be the appropriate way to attempt recovery. 16357 * Resetting a target because of a single LUN failure 16358 * victimizes all LUNs on that target. 16359 * 16360 * This is true for the LSI arrays, if an LSI 16361 * array controller returns an ASC of 0x84 (LUN Dead) we 16362 * should trust it. 16363 */ 16364 16365 if (sense_key == KEY_HARDWARE_ERROR) { 16366 switch (asc) { 16367 case 0x84: 16368 if (SD_IS_LSI(un)) { 16369 try_resetting_target = B_FALSE; 16370 } 16371 break; 16372 default: 16373 break; 16374 } 16375 } 16376 16377 if (try_resetting_target == B_TRUE) { 16378 int reset_retval = 0; 16379 if (un->un_f_lun_reset_enabled == TRUE) { 16380 SD_TRACE(SD_LOG_IO_CORE, un, 16381 "sd_sense_key_medium_or_hardware_" 16382 "error: issuing RESET_LUN\n"); 16383 reset_retval = 16384 scsi_reset(SD_ADDRESS(un), 16385 RESET_LUN); 16386 } 16387 if (reset_retval == 0) { 16388 SD_TRACE(SD_LOG_IO_CORE, un, 16389 "sd_sense_key_medium_or_hardware_" 16390 "error: issuing RESET_TARGET\n"); 16391 (void) scsi_reset(SD_ADDRESS(un), 16392 RESET_TARGET); 16393 } 16394 } 16395 } 16396 mutex_enter(SD_MUTEX(un)); 16397 } 16398 16399 /* 16400 * This really ought to be a fatal error, but we will retry anyway 16401 * as some drives report this as a spurious error. 16402 */ 16403 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16404 &si, EIO, (clock_t)0, NULL); 16405 } 16406 16407 16408 16409 /* 16410 * Function: sd_sense_key_illegal_request 16411 * 16412 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 16413 * 16414 * Context: May be called from interrupt context 16415 */ 16416 16417 static void 16418 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 16419 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16420 { 16421 struct sd_sense_info si; 16422 16423 ASSERT(un != NULL); 16424 ASSERT(mutex_owned(SD_MUTEX(un))); 16425 ASSERT(bp != NULL); 16426 ASSERT(xp != NULL); 16427 ASSERT(pktp != NULL); 16428 16429 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 16430 16431 si.ssi_severity = SCSI_ERR_INFO; 16432 si.ssi_pfa_flag = FALSE; 16433 16434 /* Pointless to retry if the target thinks it's an illegal request */ 16435 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16436 sd_return_failed_command(un, bp, EIO); 16437 } 16438 16439 16440 16441 16442 /* 16443 * Function: sd_sense_key_unit_attention 16444 * 16445 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 16446 * 16447 * Context: May be called from interrupt context 16448 */ 16449 16450 static void 16451 sd_sense_key_unit_attention(struct sd_lun *un, 16452 uint8_t *sense_datap, 16453 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16454 { 16455 /* 16456 * For UNIT ATTENTION we allow retries for one minute. Devices 16457 * like Sonoma can return UNIT ATTENTION close to a minute 16458 * under certain conditions. 16459 */ 16460 int retry_check_flag = SD_RETRIES_UA; 16461 boolean_t kstat_updated = B_FALSE; 16462 struct sd_sense_info si; 16463 uint8_t asc = scsi_sense_asc(sense_datap); 16464 16465 ASSERT(un != NULL); 16466 ASSERT(mutex_owned(SD_MUTEX(un))); 16467 ASSERT(bp != NULL); 16468 ASSERT(xp != NULL); 16469 ASSERT(pktp != NULL); 16470 16471 si.ssi_severity = SCSI_ERR_INFO; 16472 si.ssi_pfa_flag = FALSE; 16473 16474 16475 switch (asc) { 16476 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 16477 if (sd_report_pfa != 0) { 16478 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 16479 si.ssi_pfa_flag = TRUE; 16480 retry_check_flag = SD_RETRIES_STANDARD; 16481 goto do_retry; 16482 } 16483 16484 break; 16485 16486 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 16487 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 16488 un->un_resvd_status |= 16489 (SD_LOST_RESERVE | SD_WANT_RESERVE); 16490 } 16491 #ifdef _LP64 16492 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 16493 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 16494 un, KM_NOSLEEP) == 0) { 16495 /* 16496 * If we can't dispatch the task we'll just 16497 * live without descriptor sense. We can 16498 * try again on the next "unit attention" 16499 */ 16500 SD_ERROR(SD_LOG_ERROR, un, 16501 "sd_sense_key_unit_attention: " 16502 "Could not dispatch " 16503 "sd_reenable_dsense_task\n"); 16504 } 16505 } 16506 #endif /* _LP64 */ 16507 /* FALLTHRU */ 16508 16509 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 16510 if (!un->un_f_has_removable_media) { 16511 break; 16512 } 16513 16514 /* 16515 * When we get a unit attention from a removable-media device, 16516 * it may be in a state that will take a long time to recover 16517 * (e.g., from a reset). Since we are executing in interrupt 16518 * context here, we cannot wait around for the device to come 16519 * back. So hand this command off to sd_media_change_task() 16520 * for deferred processing under taskq thread context. (Note 16521 * that the command still may be failed if a problem is 16522 * encountered at a later time.) 16523 */ 16524 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 16525 KM_NOSLEEP) == 0) { 16526 /* 16527 * Cannot dispatch the request so fail the command. 16528 */ 16529 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16530 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16531 si.ssi_severity = SCSI_ERR_FATAL; 16532 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16533 sd_return_failed_command(un, bp, EIO); 16534 } 16535 16536 /* 16537 * If failed to dispatch sd_media_change_task(), we already 16538 * updated kstat. If succeed to dispatch sd_media_change_task(), 16539 * we should update kstat later if it encounters an error. So, 16540 * we update kstat_updated flag here. 16541 */ 16542 kstat_updated = B_TRUE; 16543 16544 /* 16545 * Either the command has been successfully dispatched to a 16546 * task Q for retrying, or the dispatch failed. In either case 16547 * do NOT retry again by calling sd_retry_command. This sets up 16548 * two retries of the same command and when one completes and 16549 * frees the resources the other will access freed memory, 16550 * a bad thing. 16551 */ 16552 return; 16553 16554 default: 16555 break; 16556 } 16557 16558 /* 16559 * Update kstat if we haven't done that. 16560 */ 16561 if (!kstat_updated) { 16562 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16563 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16564 } 16565 16566 do_retry: 16567 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 16568 EIO, SD_UA_RETRY_DELAY, NULL); 16569 } 16570 16571 16572 16573 /* 16574 * Function: sd_sense_key_fail_command 16575 * 16576 * Description: Use to fail a command when we don't like the sense key that 16577 * was returned. 16578 * 16579 * Context: May be called from interrupt context 16580 */ 16581 16582 static void 16583 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 16584 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16585 { 16586 struct sd_sense_info si; 16587 16588 ASSERT(un != NULL); 16589 ASSERT(mutex_owned(SD_MUTEX(un))); 16590 ASSERT(bp != NULL); 16591 ASSERT(xp != NULL); 16592 ASSERT(pktp != NULL); 16593 16594 si.ssi_severity = SCSI_ERR_FATAL; 16595 si.ssi_pfa_flag = FALSE; 16596 16597 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16598 sd_return_failed_command(un, bp, EIO); 16599 } 16600 16601 16602 16603 /* 16604 * Function: sd_sense_key_blank_check 16605 * 16606 * Description: Recovery actions for a SCSI "Blank Check" sense key. 16607 * Has no monetary connotation. 16608 * 16609 * Context: May be called from interrupt context 16610 */ 16611 16612 static void 16613 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 16614 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16615 { 16616 struct sd_sense_info si; 16617 16618 ASSERT(un != NULL); 16619 ASSERT(mutex_owned(SD_MUTEX(un))); 16620 ASSERT(bp != NULL); 16621 ASSERT(xp != NULL); 16622 ASSERT(pktp != NULL); 16623 16624 /* 16625 * Blank check is not fatal for removable devices, therefore 16626 * it does not require a console message. 16627 */ 16628 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 16629 SCSI_ERR_FATAL; 16630 si.ssi_pfa_flag = FALSE; 16631 16632 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16633 sd_return_failed_command(un, bp, EIO); 16634 } 16635 16636 16637 16638 16639 /* 16640 * Function: sd_sense_key_aborted_command 16641 * 16642 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 16643 * 16644 * Context: May be called from interrupt context 16645 */ 16646 16647 static void 16648 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 16649 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16650 { 16651 struct sd_sense_info si; 16652 16653 ASSERT(un != NULL); 16654 ASSERT(mutex_owned(SD_MUTEX(un))); 16655 ASSERT(bp != NULL); 16656 ASSERT(xp != NULL); 16657 ASSERT(pktp != NULL); 16658 16659 si.ssi_severity = SCSI_ERR_FATAL; 16660 si.ssi_pfa_flag = FALSE; 16661 16662 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16663 16664 /* 16665 * This really ought to be a fatal error, but we will retry anyway 16666 * as some drives report this as a spurious error. 16667 */ 16668 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16669 &si, EIO, drv_usectohz(100000), NULL); 16670 } 16671 16672 16673 16674 /* 16675 * Function: sd_sense_key_default 16676 * 16677 * Description: Default recovery action for several SCSI sense keys (basically 16678 * attempts a retry). 16679 * 16680 * Context: May be called from interrupt context 16681 */ 16682 16683 static void 16684 sd_sense_key_default(struct sd_lun *un, 16685 uint8_t *sense_datap, 16686 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16687 { 16688 struct sd_sense_info si; 16689 uint8_t sense_key = scsi_sense_key(sense_datap); 16690 16691 ASSERT(un != NULL); 16692 ASSERT(mutex_owned(SD_MUTEX(un))); 16693 ASSERT(bp != NULL); 16694 ASSERT(xp != NULL); 16695 ASSERT(pktp != NULL); 16696 16697 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16698 16699 /* 16700 * Undecoded sense key. Attempt retries and hope that will fix 16701 * the problem. Otherwise, we're dead. 16702 */ 16703 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16704 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16705 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 16706 } 16707 16708 si.ssi_severity = SCSI_ERR_FATAL; 16709 si.ssi_pfa_flag = FALSE; 16710 16711 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16712 &si, EIO, (clock_t)0, NULL); 16713 } 16714 16715 16716 16717 /* 16718 * Function: sd_print_retry_msg 16719 * 16720 * Description: Print a message indicating the retry action being taken. 16721 * 16722 * Arguments: un - ptr to associated softstate 16723 * bp - ptr to buf(9S) for the command 16724 * arg - not used. 16725 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16726 * or SD_NO_RETRY_ISSUED 16727 * 16728 * Context: May be called from interrupt context 16729 */ 16730 /* ARGSUSED */ 16731 static void 16732 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 16733 { 16734 struct sd_xbuf *xp; 16735 struct scsi_pkt *pktp; 16736 char *reasonp; 16737 char *msgp; 16738 16739 ASSERT(un != NULL); 16740 ASSERT(mutex_owned(SD_MUTEX(un))); 16741 ASSERT(bp != NULL); 16742 pktp = SD_GET_PKTP(bp); 16743 ASSERT(pktp != NULL); 16744 xp = SD_GET_XBUF(bp); 16745 ASSERT(xp != NULL); 16746 16747 ASSERT(!mutex_owned(&un->un_pm_mutex)); 16748 mutex_enter(&un->un_pm_mutex); 16749 if ((un->un_state == SD_STATE_SUSPENDED) || 16750 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 16751 (pktp->pkt_flags & FLAG_SILENT)) { 16752 mutex_exit(&un->un_pm_mutex); 16753 goto update_pkt_reason; 16754 } 16755 mutex_exit(&un->un_pm_mutex); 16756 16757 /* 16758 * Suppress messages if they are all the same pkt_reason; with 16759 * TQ, many (up to 256) are returned with the same pkt_reason. 16760 * If we are in panic, then suppress the retry messages. 16761 */ 16762 switch (flag) { 16763 case SD_NO_RETRY_ISSUED: 16764 msgp = "giving up"; 16765 break; 16766 case SD_IMMEDIATE_RETRY_ISSUED: 16767 case SD_DELAYED_RETRY_ISSUED: 16768 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 16769 ((pktp->pkt_reason == un->un_last_pkt_reason) && 16770 (sd_error_level != SCSI_ERR_ALL))) { 16771 return; 16772 } 16773 msgp = "retrying command"; 16774 break; 16775 default: 16776 goto update_pkt_reason; 16777 } 16778 16779 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 16780 scsi_rname(pktp->pkt_reason)); 16781 16782 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16783 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 16784 16785 update_pkt_reason: 16786 /* 16787 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 16788 * This is to prevent multiple console messages for the same failure 16789 * condition. Note that un->un_last_pkt_reason is NOT restored if & 16790 * when the command is retried successfully because there still may be 16791 * more commands coming back with the same value of pktp->pkt_reason. 16792 */ 16793 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 16794 un->un_last_pkt_reason = pktp->pkt_reason; 16795 } 16796 } 16797 16798 16799 /* 16800 * Function: sd_print_cmd_incomplete_msg 16801 * 16802 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 16803 * 16804 * Arguments: un - ptr to associated softstate 16805 * bp - ptr to buf(9S) for the command 16806 * arg - passed to sd_print_retry_msg() 16807 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16808 * or SD_NO_RETRY_ISSUED 16809 * 16810 * Context: May be called from interrupt context 16811 */ 16812 16813 static void 16814 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 16815 int code) 16816 { 16817 dev_info_t *dip; 16818 16819 ASSERT(un != NULL); 16820 ASSERT(mutex_owned(SD_MUTEX(un))); 16821 ASSERT(bp != NULL); 16822 16823 switch (code) { 16824 case SD_NO_RETRY_ISSUED: 16825 /* Command was failed. Someone turned off this target? */ 16826 if (un->un_state != SD_STATE_OFFLINE) { 16827 /* 16828 * Suppress message if we are detaching and 16829 * device has been disconnected 16830 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 16831 * private interface and not part of the DDI 16832 */ 16833 dip = un->un_sd->sd_dev; 16834 if (!(DEVI_IS_DETACHING(dip) && 16835 DEVI_IS_DEVICE_REMOVED(dip))) { 16836 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16837 "disk not responding to selection\n"); 16838 } 16839 New_state(un, SD_STATE_OFFLINE); 16840 } 16841 break; 16842 16843 case SD_DELAYED_RETRY_ISSUED: 16844 case SD_IMMEDIATE_RETRY_ISSUED: 16845 default: 16846 /* Command was successfully queued for retry */ 16847 sd_print_retry_msg(un, bp, arg, code); 16848 break; 16849 } 16850 } 16851 16852 16853 /* 16854 * Function: sd_pkt_reason_cmd_incomplete 16855 * 16856 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 16857 * 16858 * Context: May be called from interrupt context 16859 */ 16860 16861 static void 16862 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 16863 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16864 { 16865 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 16866 16867 ASSERT(un != NULL); 16868 ASSERT(mutex_owned(SD_MUTEX(un))); 16869 ASSERT(bp != NULL); 16870 ASSERT(xp != NULL); 16871 ASSERT(pktp != NULL); 16872 16873 /* Do not do a reset if selection did not complete */ 16874 /* Note: Should this not just check the bit? */ 16875 if (pktp->pkt_state != STATE_GOT_BUS) { 16876 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16877 sd_reset_target(un, pktp); 16878 } 16879 16880 /* 16881 * If the target was not successfully selected, then set 16882 * SD_RETRIES_FAILFAST to indicate that we lost communication 16883 * with the target, and further retries and/or commands are 16884 * likely to take a long time. 16885 */ 16886 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 16887 flag |= SD_RETRIES_FAILFAST; 16888 } 16889 16890 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16891 16892 sd_retry_command(un, bp, flag, 16893 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16894 } 16895 16896 16897 16898 /* 16899 * Function: sd_pkt_reason_cmd_tran_err 16900 * 16901 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 16902 * 16903 * Context: May be called from interrupt context 16904 */ 16905 16906 static void 16907 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 16908 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16909 { 16910 ASSERT(un != NULL); 16911 ASSERT(mutex_owned(SD_MUTEX(un))); 16912 ASSERT(bp != NULL); 16913 ASSERT(xp != NULL); 16914 ASSERT(pktp != NULL); 16915 16916 /* 16917 * Do not reset if we got a parity error, or if 16918 * selection did not complete. 16919 */ 16920 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16921 /* Note: Should this not just check the bit for pkt_state? */ 16922 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 16923 (pktp->pkt_state != STATE_GOT_BUS)) { 16924 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16925 sd_reset_target(un, pktp); 16926 } 16927 16928 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16929 16930 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 16931 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16932 } 16933 16934 16935 16936 /* 16937 * Function: sd_pkt_reason_cmd_reset 16938 * 16939 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 16940 * 16941 * Context: May be called from interrupt context 16942 */ 16943 16944 static void 16945 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 16946 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16947 { 16948 ASSERT(un != NULL); 16949 ASSERT(mutex_owned(SD_MUTEX(un))); 16950 ASSERT(bp != NULL); 16951 ASSERT(xp != NULL); 16952 ASSERT(pktp != NULL); 16953 16954 /* The target may still be running the command, so try to reset. */ 16955 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16956 sd_reset_target(un, pktp); 16957 16958 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16959 16960 /* 16961 * If pkt_reason is CMD_RESET chances are that this pkt got 16962 * reset because another target on this bus caused it. The target 16963 * that caused it should get CMD_TIMEOUT with pkt_statistics 16964 * of STAT_TIMEOUT/STAT_DEV_RESET. 16965 */ 16966 16967 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 16968 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16969 } 16970 16971 16972 16973 16974 /* 16975 * Function: sd_pkt_reason_cmd_aborted 16976 * 16977 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 16978 * 16979 * Context: May be called from interrupt context 16980 */ 16981 16982 static void 16983 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 16984 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16985 { 16986 ASSERT(un != NULL); 16987 ASSERT(mutex_owned(SD_MUTEX(un))); 16988 ASSERT(bp != NULL); 16989 ASSERT(xp != NULL); 16990 ASSERT(pktp != NULL); 16991 16992 /* The target may still be running the command, so try to reset. */ 16993 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16994 sd_reset_target(un, pktp); 16995 16996 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16997 16998 /* 16999 * If pkt_reason is CMD_ABORTED chances are that this pkt got 17000 * aborted because another target on this bus caused it. The target 17001 * that caused it should get CMD_TIMEOUT with pkt_statistics 17002 * of STAT_TIMEOUT/STAT_DEV_RESET. 17003 */ 17004 17005 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 17006 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17007 } 17008 17009 17010 17011 /* 17012 * Function: sd_pkt_reason_cmd_timeout 17013 * 17014 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 17015 * 17016 * Context: May be called from interrupt context 17017 */ 17018 17019 static void 17020 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 17021 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17022 { 17023 ASSERT(un != NULL); 17024 ASSERT(mutex_owned(SD_MUTEX(un))); 17025 ASSERT(bp != NULL); 17026 ASSERT(xp != NULL); 17027 ASSERT(pktp != NULL); 17028 17029 17030 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17031 sd_reset_target(un, pktp); 17032 17033 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17034 17035 /* 17036 * A command timeout indicates that we could not establish 17037 * communication with the target, so set SD_RETRIES_FAILFAST 17038 * as further retries/commands are likely to take a long time. 17039 */ 17040 sd_retry_command(un, bp, 17041 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 17042 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17043 } 17044 17045 17046 17047 /* 17048 * Function: sd_pkt_reason_cmd_unx_bus_free 17049 * 17050 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 17051 * 17052 * Context: May be called from interrupt context 17053 */ 17054 17055 static void 17056 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 17057 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17058 { 17059 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 17060 17061 ASSERT(un != NULL); 17062 ASSERT(mutex_owned(SD_MUTEX(un))); 17063 ASSERT(bp != NULL); 17064 ASSERT(xp != NULL); 17065 ASSERT(pktp != NULL); 17066 17067 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17068 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17069 17070 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 17071 sd_print_retry_msg : NULL; 17072 17073 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17074 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17075 } 17076 17077 17078 /* 17079 * Function: sd_pkt_reason_cmd_tag_reject 17080 * 17081 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 17082 * 17083 * Context: May be called from interrupt context 17084 */ 17085 17086 static void 17087 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 17088 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17089 { 17090 ASSERT(un != NULL); 17091 ASSERT(mutex_owned(SD_MUTEX(un))); 17092 ASSERT(bp != NULL); 17093 ASSERT(xp != NULL); 17094 ASSERT(pktp != NULL); 17095 17096 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17097 pktp->pkt_flags = 0; 17098 un->un_tagflags = 0; 17099 if (un->un_f_opt_queueing == TRUE) { 17100 un->un_throttle = min(un->un_throttle, 3); 17101 } else { 17102 un->un_throttle = 1; 17103 } 17104 mutex_exit(SD_MUTEX(un)); 17105 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 17106 mutex_enter(SD_MUTEX(un)); 17107 17108 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17109 17110 /* Legacy behavior not to check retry counts here. */ 17111 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 17112 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17113 } 17114 17115 17116 /* 17117 * Function: sd_pkt_reason_default 17118 * 17119 * Description: Default recovery actions for SCSA pkt_reason values that 17120 * do not have more explicit recovery actions. 17121 * 17122 * Context: May be called from interrupt context 17123 */ 17124 17125 static void 17126 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 17127 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17128 { 17129 ASSERT(un != NULL); 17130 ASSERT(mutex_owned(SD_MUTEX(un))); 17131 ASSERT(bp != NULL); 17132 ASSERT(xp != NULL); 17133 ASSERT(pktp != NULL); 17134 17135 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17136 sd_reset_target(un, pktp); 17137 17138 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17139 17140 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17141 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17142 } 17143 17144 17145 17146 /* 17147 * Function: sd_pkt_status_check_condition 17148 * 17149 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 17150 * 17151 * Context: May be called from interrupt context 17152 */ 17153 17154 static void 17155 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 17156 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17157 { 17158 ASSERT(un != NULL); 17159 ASSERT(mutex_owned(SD_MUTEX(un))); 17160 ASSERT(bp != NULL); 17161 ASSERT(xp != NULL); 17162 ASSERT(pktp != NULL); 17163 17164 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 17165 "entry: buf:0x%p xp:0x%p\n", bp, xp); 17166 17167 /* 17168 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 17169 * command will be retried after the request sense). Otherwise, retry 17170 * the command. Note: we are issuing the request sense even though the 17171 * retry limit may have been reached for the failed command. 17172 */ 17173 if (un->un_f_arq_enabled == FALSE) { 17174 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 17175 "no ARQ, sending request sense command\n"); 17176 sd_send_request_sense_command(un, bp, pktp); 17177 } else { 17178 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 17179 "ARQ,retrying request sense command\n"); 17180 #if defined(__i386) || defined(__amd64) 17181 /* 17182 * The SD_RETRY_DELAY value need to be adjusted here 17183 * when SD_RETRY_DELAY change in sddef.h 17184 */ 17185 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17186 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 17187 NULL); 17188 #else 17189 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 17190 EIO, SD_RETRY_DELAY, NULL); 17191 #endif 17192 } 17193 17194 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 17195 } 17196 17197 17198 /* 17199 * Function: sd_pkt_status_busy 17200 * 17201 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 17202 * 17203 * Context: May be called from interrupt context 17204 */ 17205 17206 static void 17207 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17208 struct scsi_pkt *pktp) 17209 { 17210 ASSERT(un != NULL); 17211 ASSERT(mutex_owned(SD_MUTEX(un))); 17212 ASSERT(bp != NULL); 17213 ASSERT(xp != NULL); 17214 ASSERT(pktp != NULL); 17215 17216 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17217 "sd_pkt_status_busy: entry\n"); 17218 17219 /* If retries are exhausted, just fail the command. */ 17220 if (xp->xb_retry_count >= un->un_busy_retry_count) { 17221 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17222 "device busy too long\n"); 17223 sd_return_failed_command(un, bp, EIO); 17224 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17225 "sd_pkt_status_busy: exit\n"); 17226 return; 17227 } 17228 xp->xb_retry_count++; 17229 17230 /* 17231 * Try to reset the target. However, we do not want to perform 17232 * more than one reset if the device continues to fail. The reset 17233 * will be performed when the retry count reaches the reset 17234 * threshold. This threshold should be set such that at least 17235 * one retry is issued before the reset is performed. 17236 */ 17237 if (xp->xb_retry_count == 17238 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 17239 int rval = 0; 17240 mutex_exit(SD_MUTEX(un)); 17241 if (un->un_f_allow_bus_device_reset == TRUE) { 17242 /* 17243 * First try to reset the LUN; if we cannot then 17244 * try to reset the target. 17245 */ 17246 if (un->un_f_lun_reset_enabled == TRUE) { 17247 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17248 "sd_pkt_status_busy: RESET_LUN\n"); 17249 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17250 } 17251 if (rval == 0) { 17252 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17253 "sd_pkt_status_busy: RESET_TARGET\n"); 17254 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17255 } 17256 } 17257 if (rval == 0) { 17258 /* 17259 * If the RESET_LUN and/or RESET_TARGET failed, 17260 * try RESET_ALL 17261 */ 17262 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17263 "sd_pkt_status_busy: RESET_ALL\n"); 17264 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 17265 } 17266 mutex_enter(SD_MUTEX(un)); 17267 if (rval == 0) { 17268 /* 17269 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 17270 * At this point we give up & fail the command. 17271 */ 17272 sd_return_failed_command(un, bp, EIO); 17273 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17274 "sd_pkt_status_busy: exit (failed cmd)\n"); 17275 return; 17276 } 17277 } 17278 17279 /* 17280 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 17281 * we have already checked the retry counts above. 17282 */ 17283 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 17284 EIO, SD_BSY_TIMEOUT, NULL); 17285 17286 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17287 "sd_pkt_status_busy: exit\n"); 17288 } 17289 17290 17291 /* 17292 * Function: sd_pkt_status_reservation_conflict 17293 * 17294 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 17295 * command status. 17296 * 17297 * Context: May be called from interrupt context 17298 */ 17299 17300 static void 17301 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 17302 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17303 { 17304 ASSERT(un != NULL); 17305 ASSERT(mutex_owned(SD_MUTEX(un))); 17306 ASSERT(bp != NULL); 17307 ASSERT(xp != NULL); 17308 ASSERT(pktp != NULL); 17309 17310 /* 17311 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 17312 * conflict could be due to various reasons like incorrect keys, not 17313 * registered or not reserved etc. So, we return EACCES to the caller. 17314 */ 17315 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 17316 int cmd = SD_GET_PKT_OPCODE(pktp); 17317 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 17318 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 17319 sd_return_failed_command(un, bp, EACCES); 17320 return; 17321 } 17322 } 17323 17324 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 17325 17326 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 17327 if (sd_failfast_enable != 0) { 17328 /* By definition, we must panic here.... */ 17329 sd_panic_for_res_conflict(un); 17330 /*NOTREACHED*/ 17331 } 17332 SD_ERROR(SD_LOG_IO, un, 17333 "sd_handle_resv_conflict: Disk Reserved\n"); 17334 sd_return_failed_command(un, bp, EACCES); 17335 return; 17336 } 17337 17338 /* 17339 * 1147670: retry only if sd_retry_on_reservation_conflict 17340 * property is set (default is 1). Retries will not succeed 17341 * on a disk reserved by another initiator. HA systems 17342 * may reset this via sd.conf to avoid these retries. 17343 * 17344 * Note: The legacy return code for this failure is EIO, however EACCES 17345 * seems more appropriate for a reservation conflict. 17346 */ 17347 if (sd_retry_on_reservation_conflict == 0) { 17348 SD_ERROR(SD_LOG_IO, un, 17349 "sd_handle_resv_conflict: Device Reserved\n"); 17350 sd_return_failed_command(un, bp, EIO); 17351 return; 17352 } 17353 17354 /* 17355 * Retry the command if we can. 17356 * 17357 * Note: The legacy return code for this failure is EIO, however EACCES 17358 * seems more appropriate for a reservation conflict. 17359 */ 17360 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17361 (clock_t)2, NULL); 17362 } 17363 17364 17365 17366 /* 17367 * Function: sd_pkt_status_qfull 17368 * 17369 * Description: Handle a QUEUE FULL condition from the target. This can 17370 * occur if the HBA does not handle the queue full condition. 17371 * (Basically this means third-party HBAs as Sun HBAs will 17372 * handle the queue full condition.) Note that if there are 17373 * some commands already in the transport, then the queue full 17374 * has occurred because the queue for this nexus is actually 17375 * full. If there are no commands in the transport, then the 17376 * queue full is resulting from some other initiator or lun 17377 * consuming all the resources at the target. 17378 * 17379 * Context: May be called from interrupt context 17380 */ 17381 17382 static void 17383 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 17384 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17385 { 17386 ASSERT(un != NULL); 17387 ASSERT(mutex_owned(SD_MUTEX(un))); 17388 ASSERT(bp != NULL); 17389 ASSERT(xp != NULL); 17390 ASSERT(pktp != NULL); 17391 17392 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17393 "sd_pkt_status_qfull: entry\n"); 17394 17395 /* 17396 * Just lower the QFULL throttle and retry the command. Note that 17397 * we do not limit the number of retries here. 17398 */ 17399 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 17400 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 17401 SD_RESTART_TIMEOUT, NULL); 17402 17403 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17404 "sd_pkt_status_qfull: exit\n"); 17405 } 17406 17407 17408 /* 17409 * Function: sd_reset_target 17410 * 17411 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 17412 * RESET_TARGET, or RESET_ALL. 17413 * 17414 * Context: May be called under interrupt context. 17415 */ 17416 17417 static void 17418 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 17419 { 17420 int rval = 0; 17421 17422 ASSERT(un != NULL); 17423 ASSERT(mutex_owned(SD_MUTEX(un))); 17424 ASSERT(pktp != NULL); 17425 17426 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 17427 17428 /* 17429 * No need to reset if the transport layer has already done so. 17430 */ 17431 if ((pktp->pkt_statistics & 17432 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 17433 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17434 "sd_reset_target: no reset\n"); 17435 return; 17436 } 17437 17438 mutex_exit(SD_MUTEX(un)); 17439 17440 if (un->un_f_allow_bus_device_reset == TRUE) { 17441 if (un->un_f_lun_reset_enabled == TRUE) { 17442 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17443 "sd_reset_target: RESET_LUN\n"); 17444 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17445 } 17446 if (rval == 0) { 17447 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17448 "sd_reset_target: RESET_TARGET\n"); 17449 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17450 } 17451 } 17452 17453 if (rval == 0) { 17454 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17455 "sd_reset_target: RESET_ALL\n"); 17456 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 17457 } 17458 17459 mutex_enter(SD_MUTEX(un)); 17460 17461 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 17462 } 17463 17464 17465 /* 17466 * Function: sd_media_change_task 17467 * 17468 * Description: Recovery action for CDROM to become available. 17469 * 17470 * Context: Executes in a taskq() thread context 17471 */ 17472 17473 static void 17474 sd_media_change_task(void *arg) 17475 { 17476 struct scsi_pkt *pktp = arg; 17477 struct sd_lun *un; 17478 struct buf *bp; 17479 struct sd_xbuf *xp; 17480 int err = 0; 17481 int retry_count = 0; 17482 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 17483 struct sd_sense_info si; 17484 17485 ASSERT(pktp != NULL); 17486 bp = (struct buf *)pktp->pkt_private; 17487 ASSERT(bp != NULL); 17488 xp = SD_GET_XBUF(bp); 17489 ASSERT(xp != NULL); 17490 un = SD_GET_UN(bp); 17491 ASSERT(un != NULL); 17492 ASSERT(!mutex_owned(SD_MUTEX(un))); 17493 ASSERT(un->un_f_monitor_media_state); 17494 17495 si.ssi_severity = SCSI_ERR_INFO; 17496 si.ssi_pfa_flag = FALSE; 17497 17498 /* 17499 * When a reset is issued on a CDROM, it takes a long time to 17500 * recover. First few attempts to read capacity and other things 17501 * related to handling unit attention fail (with a ASC 0x4 and 17502 * ASCQ 0x1). In that case we want to do enough retries and we want 17503 * to limit the retries in other cases of genuine failures like 17504 * no media in drive. 17505 */ 17506 while (retry_count++ < retry_limit) { 17507 if ((err = sd_handle_mchange(un)) == 0) { 17508 break; 17509 } 17510 if (err == EAGAIN) { 17511 retry_limit = SD_UNIT_ATTENTION_RETRY; 17512 } 17513 /* Sleep for 0.5 sec. & try again */ 17514 delay(drv_usectohz(500000)); 17515 } 17516 17517 /* 17518 * Dispatch (retry or fail) the original command here, 17519 * along with appropriate console messages.... 17520 * 17521 * Must grab the mutex before calling sd_retry_command, 17522 * sd_print_sense_msg and sd_return_failed_command. 17523 */ 17524 mutex_enter(SD_MUTEX(un)); 17525 if (err != SD_CMD_SUCCESS) { 17526 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17527 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17528 si.ssi_severity = SCSI_ERR_FATAL; 17529 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17530 sd_return_failed_command(un, bp, EIO); 17531 } else { 17532 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17533 &si, EIO, (clock_t)0, NULL); 17534 } 17535 mutex_exit(SD_MUTEX(un)); 17536 } 17537 17538 17539 17540 /* 17541 * Function: sd_handle_mchange 17542 * 17543 * Description: Perform geometry validation & other recovery when CDROM 17544 * has been removed from drive. 17545 * 17546 * Return Code: 0 for success 17547 * errno-type return code of either sd_send_scsi_DOORLOCK() or 17548 * sd_send_scsi_READ_CAPACITY() 17549 * 17550 * Context: Executes in a taskq() thread context 17551 */ 17552 17553 static int 17554 sd_handle_mchange(struct sd_lun *un) 17555 { 17556 uint64_t capacity; 17557 uint32_t lbasize; 17558 int rval; 17559 17560 ASSERT(!mutex_owned(SD_MUTEX(un))); 17561 ASSERT(un->un_f_monitor_media_state); 17562 17563 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 17564 SD_PATH_DIRECT_PRIORITY)) != 0) { 17565 return (rval); 17566 } 17567 17568 mutex_enter(SD_MUTEX(un)); 17569 sd_update_block_info(un, lbasize, capacity); 17570 17571 if (un->un_errstats != NULL) { 17572 struct sd_errstats *stp = 17573 (struct sd_errstats *)un->un_errstats->ks_data; 17574 stp->sd_capacity.value.ui64 = (uint64_t) 17575 ((uint64_t)un->un_blockcount * 17576 (uint64_t)un->un_tgt_blocksize); 17577 } 17578 17579 17580 /* 17581 * Check if the media in the device is writable or not 17582 */ 17583 if (ISCD(un)) 17584 sd_check_for_writable_cd(un, SD_PATH_DIRECT_PRIORITY); 17585 17586 /* 17587 * Note: Maybe let the strategy/partitioning chain worry about getting 17588 * valid geometry. 17589 */ 17590 mutex_exit(SD_MUTEX(un)); 17591 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 17592 17593 17594 if (cmlb_validate(un->un_cmlbhandle, 0, 17595 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 17596 return (EIO); 17597 } else { 17598 if (un->un_f_pkstats_enabled) { 17599 sd_set_pstats(un); 17600 SD_TRACE(SD_LOG_IO_PARTITION, un, 17601 "sd_handle_mchange: un:0x%p pstats created and " 17602 "set\n", un); 17603 } 17604 } 17605 17606 17607 /* 17608 * Try to lock the door 17609 */ 17610 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 17611 SD_PATH_DIRECT_PRIORITY)); 17612 } 17613 17614 17615 /* 17616 * Function: sd_send_scsi_DOORLOCK 17617 * 17618 * Description: Issue the scsi DOOR LOCK command 17619 * 17620 * Arguments: un - pointer to driver soft state (unit) structure for 17621 * this target. 17622 * flag - SD_REMOVAL_ALLOW 17623 * SD_REMOVAL_PREVENT 17624 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17625 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17626 * to use the USCSI "direct" chain and bypass the normal 17627 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17628 * command is issued as part of an error recovery action. 17629 * 17630 * Return Code: 0 - Success 17631 * errno return code from sd_send_scsi_cmd() 17632 * 17633 * Context: Can sleep. 17634 */ 17635 17636 static int 17637 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 17638 { 17639 union scsi_cdb cdb; 17640 struct uscsi_cmd ucmd_buf; 17641 struct scsi_extended_sense sense_buf; 17642 int status; 17643 17644 ASSERT(un != NULL); 17645 ASSERT(!mutex_owned(SD_MUTEX(un))); 17646 17647 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 17648 17649 /* already determined doorlock is not supported, fake success */ 17650 if (un->un_f_doorlock_supported == FALSE) { 17651 return (0); 17652 } 17653 17654 /* 17655 * If we are ejecting and see an SD_REMOVAL_PREVENT 17656 * ignore the command so we can complete the eject 17657 * operation. 17658 */ 17659 if (flag == SD_REMOVAL_PREVENT) { 17660 mutex_enter(SD_MUTEX(un)); 17661 if (un->un_f_ejecting == TRUE) { 17662 mutex_exit(SD_MUTEX(un)); 17663 return (EAGAIN); 17664 } 17665 mutex_exit(SD_MUTEX(un)); 17666 } 17667 17668 bzero(&cdb, sizeof (cdb)); 17669 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17670 17671 cdb.scc_cmd = SCMD_DOORLOCK; 17672 cdb.cdb_opaque[4] = (uchar_t)flag; 17673 17674 ucmd_buf.uscsi_cdb = (char *)&cdb; 17675 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 17676 ucmd_buf.uscsi_bufaddr = NULL; 17677 ucmd_buf.uscsi_buflen = 0; 17678 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17679 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17680 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 17681 ucmd_buf.uscsi_timeout = 15; 17682 17683 SD_TRACE(SD_LOG_IO, un, 17684 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 17685 17686 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17687 UIO_SYSSPACE, path_flag); 17688 17689 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 17690 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17691 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 17692 /* fake success and skip subsequent doorlock commands */ 17693 un->un_f_doorlock_supported = FALSE; 17694 return (0); 17695 } 17696 17697 return (status); 17698 } 17699 17700 /* 17701 * Function: sd_send_scsi_READ_CAPACITY 17702 * 17703 * Description: This routine uses the scsi READ CAPACITY command to determine 17704 * the device capacity in number of blocks and the device native 17705 * block size. If this function returns a failure, then the 17706 * values in *capp and *lbap are undefined. If the capacity 17707 * returned is 0xffffffff then the lun is too large for a 17708 * normal READ CAPACITY command and the results of a 17709 * READ CAPACITY 16 will be used instead. 17710 * 17711 * Arguments: un - ptr to soft state struct for the target 17712 * capp - ptr to unsigned 64-bit variable to receive the 17713 * capacity value from the command. 17714 * lbap - ptr to unsigned 32-bit varaible to receive the 17715 * block size value from the command 17716 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17717 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17718 * to use the USCSI "direct" chain and bypass the normal 17719 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17720 * command is issued as part of an error recovery action. 17721 * 17722 * Return Code: 0 - Success 17723 * EIO - IO error 17724 * EACCES - Reservation conflict detected 17725 * EAGAIN - Device is becoming ready 17726 * errno return code from sd_send_scsi_cmd() 17727 * 17728 * Context: Can sleep. Blocks until command completes. 17729 */ 17730 17731 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 17732 17733 static int 17734 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 17735 int path_flag) 17736 { 17737 struct scsi_extended_sense sense_buf; 17738 struct uscsi_cmd ucmd_buf; 17739 union scsi_cdb cdb; 17740 uint32_t *capacity_buf; 17741 uint64_t capacity; 17742 uint32_t lbasize; 17743 int status; 17744 17745 ASSERT(un != NULL); 17746 ASSERT(!mutex_owned(SD_MUTEX(un))); 17747 ASSERT(capp != NULL); 17748 ASSERT(lbap != NULL); 17749 17750 SD_TRACE(SD_LOG_IO, un, 17751 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17752 17753 /* 17754 * First send a READ_CAPACITY command to the target. 17755 * (This command is mandatory under SCSI-2.) 17756 * 17757 * Set up the CDB for the READ_CAPACITY command. The Partial 17758 * Medium Indicator bit is cleared. The address field must be 17759 * zero if the PMI bit is zero. 17760 */ 17761 bzero(&cdb, sizeof (cdb)); 17762 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17763 17764 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 17765 17766 cdb.scc_cmd = SCMD_READ_CAPACITY; 17767 17768 ucmd_buf.uscsi_cdb = (char *)&cdb; 17769 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 17770 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 17771 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 17772 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17773 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17774 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17775 ucmd_buf.uscsi_timeout = 60; 17776 17777 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17778 UIO_SYSSPACE, path_flag); 17779 17780 switch (status) { 17781 case 0: 17782 /* Return failure if we did not get valid capacity data. */ 17783 if (ucmd_buf.uscsi_resid != 0) { 17784 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17785 return (EIO); 17786 } 17787 17788 /* 17789 * Read capacity and block size from the READ CAPACITY 10 data. 17790 * This data may be adjusted later due to device specific 17791 * issues. 17792 * 17793 * According to the SCSI spec, the READ CAPACITY 10 17794 * command returns the following: 17795 * 17796 * bytes 0-3: Maximum logical block address available. 17797 * (MSB in byte:0 & LSB in byte:3) 17798 * 17799 * bytes 4-7: Block length in bytes 17800 * (MSB in byte:4 & LSB in byte:7) 17801 * 17802 */ 17803 capacity = BE_32(capacity_buf[0]); 17804 lbasize = BE_32(capacity_buf[1]); 17805 17806 /* 17807 * Done with capacity_buf 17808 */ 17809 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17810 17811 /* 17812 * if the reported capacity is set to all 0xf's, then 17813 * this disk is too large and requires SBC-2 commands. 17814 * Reissue the request using READ CAPACITY 16. 17815 */ 17816 if (capacity == 0xffffffff) { 17817 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 17818 &lbasize, path_flag); 17819 if (status != 0) { 17820 return (status); 17821 } 17822 } 17823 break; /* Success! */ 17824 case EIO: 17825 switch (ucmd_buf.uscsi_status) { 17826 case STATUS_RESERVATION_CONFLICT: 17827 status = EACCES; 17828 break; 17829 case STATUS_CHECK: 17830 /* 17831 * Check condition; look for ASC/ASCQ of 0x04/0x01 17832 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 17833 */ 17834 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17835 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 17836 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 17837 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17838 return (EAGAIN); 17839 } 17840 break; 17841 default: 17842 break; 17843 } 17844 /* FALLTHRU */ 17845 default: 17846 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17847 return (status); 17848 } 17849 17850 /* 17851 * Some ATAPI CD-ROM drives report inaccurate LBA size values 17852 * (2352 and 0 are common) so for these devices always force the value 17853 * to 2048 as required by the ATAPI specs. 17854 */ 17855 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 17856 lbasize = 2048; 17857 } 17858 17859 /* 17860 * Get the maximum LBA value from the READ CAPACITY data. 17861 * Here we assume that the Partial Medium Indicator (PMI) bit 17862 * was cleared when issuing the command. This means that the LBA 17863 * returned from the device is the LBA of the last logical block 17864 * on the logical unit. The actual logical block count will be 17865 * this value plus one. 17866 * 17867 * Currently the capacity is saved in terms of un->un_sys_blocksize, 17868 * so scale the capacity value to reflect this. 17869 */ 17870 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 17871 17872 /* 17873 * Copy the values from the READ CAPACITY command into the space 17874 * provided by the caller. 17875 */ 17876 *capp = capacity; 17877 *lbap = lbasize; 17878 17879 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 17880 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 17881 17882 /* 17883 * Both the lbasize and capacity from the device must be nonzero, 17884 * otherwise we assume that the values are not valid and return 17885 * failure to the caller. (4203735) 17886 */ 17887 if ((capacity == 0) || (lbasize == 0)) { 17888 return (EIO); 17889 } 17890 17891 return (0); 17892 } 17893 17894 /* 17895 * Function: sd_send_scsi_READ_CAPACITY_16 17896 * 17897 * Description: This routine uses the scsi READ CAPACITY 16 command to 17898 * determine the device capacity in number of blocks and the 17899 * device native block size. If this function returns a failure, 17900 * then the values in *capp and *lbap are undefined. 17901 * This routine should always be called by 17902 * sd_send_scsi_READ_CAPACITY which will appy any device 17903 * specific adjustments to capacity and lbasize. 17904 * 17905 * Arguments: un - ptr to soft state struct for the target 17906 * capp - ptr to unsigned 64-bit variable to receive the 17907 * capacity value from the command. 17908 * lbap - ptr to unsigned 32-bit varaible to receive the 17909 * block size value from the command 17910 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17911 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17912 * to use the USCSI "direct" chain and bypass the normal 17913 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 17914 * this command is issued as part of an error recovery 17915 * action. 17916 * 17917 * Return Code: 0 - Success 17918 * EIO - IO error 17919 * EACCES - Reservation conflict detected 17920 * EAGAIN - Device is becoming ready 17921 * errno return code from sd_send_scsi_cmd() 17922 * 17923 * Context: Can sleep. Blocks until command completes. 17924 */ 17925 17926 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 17927 17928 static int 17929 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 17930 uint32_t *lbap, int path_flag) 17931 { 17932 struct scsi_extended_sense sense_buf; 17933 struct uscsi_cmd ucmd_buf; 17934 union scsi_cdb cdb; 17935 uint64_t *capacity16_buf; 17936 uint64_t capacity; 17937 uint32_t lbasize; 17938 int status; 17939 17940 ASSERT(un != NULL); 17941 ASSERT(!mutex_owned(SD_MUTEX(un))); 17942 ASSERT(capp != NULL); 17943 ASSERT(lbap != NULL); 17944 17945 SD_TRACE(SD_LOG_IO, un, 17946 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17947 17948 /* 17949 * First send a READ_CAPACITY_16 command to the target. 17950 * 17951 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 17952 * Medium Indicator bit is cleared. The address field must be 17953 * zero if the PMI bit is zero. 17954 */ 17955 bzero(&cdb, sizeof (cdb)); 17956 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17957 17958 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 17959 17960 ucmd_buf.uscsi_cdb = (char *)&cdb; 17961 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 17962 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 17963 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 17964 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17965 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17966 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17967 ucmd_buf.uscsi_timeout = 60; 17968 17969 /* 17970 * Read Capacity (16) is a Service Action In command. One 17971 * command byte (0x9E) is overloaded for multiple operations, 17972 * with the second CDB byte specifying the desired operation 17973 */ 17974 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 17975 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 17976 17977 /* 17978 * Fill in allocation length field 17979 */ 17980 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 17981 17982 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17983 UIO_SYSSPACE, path_flag); 17984 17985 switch (status) { 17986 case 0: 17987 /* Return failure if we did not get valid capacity data. */ 17988 if (ucmd_buf.uscsi_resid > 20) { 17989 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 17990 return (EIO); 17991 } 17992 17993 /* 17994 * Read capacity and block size from the READ CAPACITY 10 data. 17995 * This data may be adjusted later due to device specific 17996 * issues. 17997 * 17998 * According to the SCSI spec, the READ CAPACITY 10 17999 * command returns the following: 18000 * 18001 * bytes 0-7: Maximum logical block address available. 18002 * (MSB in byte:0 & LSB in byte:7) 18003 * 18004 * bytes 8-11: Block length in bytes 18005 * (MSB in byte:8 & LSB in byte:11) 18006 * 18007 */ 18008 capacity = BE_64(capacity16_buf[0]); 18009 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 18010 18011 /* 18012 * Done with capacity16_buf 18013 */ 18014 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18015 18016 /* 18017 * if the reported capacity is set to all 0xf's, then 18018 * this disk is too large. This could only happen with 18019 * a device that supports LBAs larger than 64 bits which 18020 * are not defined by any current T10 standards. 18021 */ 18022 if (capacity == 0xffffffffffffffff) { 18023 return (EIO); 18024 } 18025 break; /* Success! */ 18026 case EIO: 18027 switch (ucmd_buf.uscsi_status) { 18028 case STATUS_RESERVATION_CONFLICT: 18029 status = EACCES; 18030 break; 18031 case STATUS_CHECK: 18032 /* 18033 * Check condition; look for ASC/ASCQ of 0x04/0x01 18034 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 18035 */ 18036 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18037 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 18038 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 18039 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18040 return (EAGAIN); 18041 } 18042 break; 18043 default: 18044 break; 18045 } 18046 /* FALLTHRU */ 18047 default: 18048 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18049 return (status); 18050 } 18051 18052 *capp = capacity; 18053 *lbap = lbasize; 18054 18055 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 18056 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 18057 18058 return (0); 18059 } 18060 18061 18062 /* 18063 * Function: sd_send_scsi_START_STOP_UNIT 18064 * 18065 * Description: Issue a scsi START STOP UNIT command to the target. 18066 * 18067 * Arguments: un - pointer to driver soft state (unit) structure for 18068 * this target. 18069 * flag - SD_TARGET_START 18070 * SD_TARGET_STOP 18071 * SD_TARGET_EJECT 18072 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18073 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18074 * to use the USCSI "direct" chain and bypass the normal 18075 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18076 * command is issued as part of an error recovery action. 18077 * 18078 * Return Code: 0 - Success 18079 * EIO - IO error 18080 * EACCES - Reservation conflict detected 18081 * ENXIO - Not Ready, medium not present 18082 * errno return code from sd_send_scsi_cmd() 18083 * 18084 * Context: Can sleep. 18085 */ 18086 18087 static int 18088 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 18089 { 18090 struct scsi_extended_sense sense_buf; 18091 union scsi_cdb cdb; 18092 struct uscsi_cmd ucmd_buf; 18093 int status; 18094 18095 ASSERT(un != NULL); 18096 ASSERT(!mutex_owned(SD_MUTEX(un))); 18097 18098 SD_TRACE(SD_LOG_IO, un, 18099 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 18100 18101 if (un->un_f_check_start_stop && 18102 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 18103 (un->un_f_start_stop_supported != TRUE)) { 18104 return (0); 18105 } 18106 18107 /* 18108 * If we are performing an eject operation and 18109 * we receive any command other than SD_TARGET_EJECT 18110 * we should immediately return. 18111 */ 18112 if (flag != SD_TARGET_EJECT) { 18113 mutex_enter(SD_MUTEX(un)); 18114 if (un->un_f_ejecting == TRUE) { 18115 mutex_exit(SD_MUTEX(un)); 18116 return (EAGAIN); 18117 } 18118 mutex_exit(SD_MUTEX(un)); 18119 } 18120 18121 bzero(&cdb, sizeof (cdb)); 18122 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18123 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18124 18125 cdb.scc_cmd = SCMD_START_STOP; 18126 cdb.cdb_opaque[4] = (uchar_t)flag; 18127 18128 ucmd_buf.uscsi_cdb = (char *)&cdb; 18129 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18130 ucmd_buf.uscsi_bufaddr = NULL; 18131 ucmd_buf.uscsi_buflen = 0; 18132 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18133 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18134 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18135 ucmd_buf.uscsi_timeout = 200; 18136 18137 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18138 UIO_SYSSPACE, path_flag); 18139 18140 switch (status) { 18141 case 0: 18142 break; /* Success! */ 18143 case EIO: 18144 switch (ucmd_buf.uscsi_status) { 18145 case STATUS_RESERVATION_CONFLICT: 18146 status = EACCES; 18147 break; 18148 case STATUS_CHECK: 18149 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 18150 switch (scsi_sense_key( 18151 (uint8_t *)&sense_buf)) { 18152 case KEY_ILLEGAL_REQUEST: 18153 status = ENOTSUP; 18154 break; 18155 case KEY_NOT_READY: 18156 if (scsi_sense_asc( 18157 (uint8_t *)&sense_buf) 18158 == 0x3A) { 18159 status = ENXIO; 18160 } 18161 break; 18162 default: 18163 break; 18164 } 18165 } 18166 break; 18167 default: 18168 break; 18169 } 18170 break; 18171 default: 18172 break; 18173 } 18174 18175 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 18176 18177 return (status); 18178 } 18179 18180 18181 /* 18182 * Function: sd_start_stop_unit_callback 18183 * 18184 * Description: timeout(9F) callback to begin recovery process for a 18185 * device that has spun down. 18186 * 18187 * Arguments: arg - pointer to associated softstate struct. 18188 * 18189 * Context: Executes in a timeout(9F) thread context 18190 */ 18191 18192 static void 18193 sd_start_stop_unit_callback(void *arg) 18194 { 18195 struct sd_lun *un = arg; 18196 ASSERT(un != NULL); 18197 ASSERT(!mutex_owned(SD_MUTEX(un))); 18198 18199 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 18200 18201 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 18202 } 18203 18204 18205 /* 18206 * Function: sd_start_stop_unit_task 18207 * 18208 * Description: Recovery procedure when a drive is spun down. 18209 * 18210 * Arguments: arg - pointer to associated softstate struct. 18211 * 18212 * Context: Executes in a taskq() thread context 18213 */ 18214 18215 static void 18216 sd_start_stop_unit_task(void *arg) 18217 { 18218 struct sd_lun *un = arg; 18219 18220 ASSERT(un != NULL); 18221 ASSERT(!mutex_owned(SD_MUTEX(un))); 18222 18223 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 18224 18225 /* 18226 * Some unformatted drives report not ready error, no need to 18227 * restart if format has been initiated. 18228 */ 18229 mutex_enter(SD_MUTEX(un)); 18230 if (un->un_f_format_in_progress == TRUE) { 18231 mutex_exit(SD_MUTEX(un)); 18232 return; 18233 } 18234 mutex_exit(SD_MUTEX(un)); 18235 18236 /* 18237 * When a START STOP command is issued from here, it is part of a 18238 * failure recovery operation and must be issued before any other 18239 * commands, including any pending retries. Thus it must be sent 18240 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 18241 * succeeds or not, we will start I/O after the attempt. 18242 */ 18243 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 18244 SD_PATH_DIRECT_PRIORITY); 18245 18246 /* 18247 * The above call blocks until the START_STOP_UNIT command completes. 18248 * Now that it has completed, we must re-try the original IO that 18249 * received the NOT READY condition in the first place. There are 18250 * three possible conditions here: 18251 * 18252 * (1) The original IO is on un_retry_bp. 18253 * (2) The original IO is on the regular wait queue, and un_retry_bp 18254 * is NULL. 18255 * (3) The original IO is on the regular wait queue, and un_retry_bp 18256 * points to some other, unrelated bp. 18257 * 18258 * For each case, we must call sd_start_cmds() with un_retry_bp 18259 * as the argument. If un_retry_bp is NULL, this will initiate 18260 * processing of the regular wait queue. If un_retry_bp is not NULL, 18261 * then this will process the bp on un_retry_bp. That may or may not 18262 * be the original IO, but that does not matter: the important thing 18263 * is to keep the IO processing going at this point. 18264 * 18265 * Note: This is a very specific error recovery sequence associated 18266 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 18267 * serialize the I/O with completion of the spin-up. 18268 */ 18269 mutex_enter(SD_MUTEX(un)); 18270 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18271 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 18272 un, un->un_retry_bp); 18273 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 18274 sd_start_cmds(un, un->un_retry_bp); 18275 mutex_exit(SD_MUTEX(un)); 18276 18277 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 18278 } 18279 18280 18281 /* 18282 * Function: sd_send_scsi_INQUIRY 18283 * 18284 * Description: Issue the scsi INQUIRY command. 18285 * 18286 * Arguments: un 18287 * bufaddr 18288 * buflen 18289 * evpd 18290 * page_code 18291 * page_length 18292 * 18293 * Return Code: 0 - Success 18294 * errno return code from sd_send_scsi_cmd() 18295 * 18296 * Context: Can sleep. Does not return until command is completed. 18297 */ 18298 18299 static int 18300 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 18301 uchar_t evpd, uchar_t page_code, size_t *residp) 18302 { 18303 union scsi_cdb cdb; 18304 struct uscsi_cmd ucmd_buf; 18305 int status; 18306 18307 ASSERT(un != NULL); 18308 ASSERT(!mutex_owned(SD_MUTEX(un))); 18309 ASSERT(bufaddr != NULL); 18310 18311 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 18312 18313 bzero(&cdb, sizeof (cdb)); 18314 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18315 bzero(bufaddr, buflen); 18316 18317 cdb.scc_cmd = SCMD_INQUIRY; 18318 cdb.cdb_opaque[1] = evpd; 18319 cdb.cdb_opaque[2] = page_code; 18320 FORMG0COUNT(&cdb, buflen); 18321 18322 ucmd_buf.uscsi_cdb = (char *)&cdb; 18323 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18324 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 18325 ucmd_buf.uscsi_buflen = buflen; 18326 ucmd_buf.uscsi_rqbuf = NULL; 18327 ucmd_buf.uscsi_rqlen = 0; 18328 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 18329 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 18330 18331 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18332 UIO_SYSSPACE, SD_PATH_DIRECT); 18333 18334 if ((status == 0) && (residp != NULL)) { 18335 *residp = ucmd_buf.uscsi_resid; 18336 } 18337 18338 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 18339 18340 return (status); 18341 } 18342 18343 18344 /* 18345 * Function: sd_send_scsi_TEST_UNIT_READY 18346 * 18347 * Description: Issue the scsi TEST UNIT READY command. 18348 * This routine can be told to set the flag USCSI_DIAGNOSE to 18349 * prevent retrying failed commands. Use this when the intent 18350 * is either to check for device readiness, to clear a Unit 18351 * Attention, or to clear any outstanding sense data. 18352 * However under specific conditions the expected behavior 18353 * is for retries to bring a device ready, so use the flag 18354 * with caution. 18355 * 18356 * Arguments: un 18357 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 18358 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 18359 * 0: dont check for media present, do retries on cmd. 18360 * 18361 * Return Code: 0 - Success 18362 * EIO - IO error 18363 * EACCES - Reservation conflict detected 18364 * ENXIO - Not Ready, medium not present 18365 * errno return code from sd_send_scsi_cmd() 18366 * 18367 * Context: Can sleep. Does not return until command is completed. 18368 */ 18369 18370 static int 18371 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 18372 { 18373 struct scsi_extended_sense sense_buf; 18374 union scsi_cdb cdb; 18375 struct uscsi_cmd ucmd_buf; 18376 int status; 18377 18378 ASSERT(un != NULL); 18379 ASSERT(!mutex_owned(SD_MUTEX(un))); 18380 18381 SD_TRACE(SD_LOG_IO, un, 18382 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 18383 18384 /* 18385 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 18386 * timeouts when they receive a TUR and the queue is not empty. Check 18387 * the configuration flag set during attach (indicating the drive has 18388 * this firmware bug) and un_ncmds_in_transport before issuing the 18389 * TUR. If there are 18390 * pending commands return success, this is a bit arbitrary but is ok 18391 * for non-removables (i.e. the eliteI disks) and non-clustering 18392 * configurations. 18393 */ 18394 if (un->un_f_cfg_tur_check == TRUE) { 18395 mutex_enter(SD_MUTEX(un)); 18396 if (un->un_ncmds_in_transport != 0) { 18397 mutex_exit(SD_MUTEX(un)); 18398 return (0); 18399 } 18400 mutex_exit(SD_MUTEX(un)); 18401 } 18402 18403 bzero(&cdb, sizeof (cdb)); 18404 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18405 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18406 18407 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 18408 18409 ucmd_buf.uscsi_cdb = (char *)&cdb; 18410 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18411 ucmd_buf.uscsi_bufaddr = NULL; 18412 ucmd_buf.uscsi_buflen = 0; 18413 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18414 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18415 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18416 18417 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 18418 if ((flag & SD_DONT_RETRY_TUR) != 0) { 18419 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 18420 } 18421 ucmd_buf.uscsi_timeout = 60; 18422 18423 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18424 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 18425 SD_PATH_STANDARD)); 18426 18427 switch (status) { 18428 case 0: 18429 break; /* Success! */ 18430 case EIO: 18431 switch (ucmd_buf.uscsi_status) { 18432 case STATUS_RESERVATION_CONFLICT: 18433 status = EACCES; 18434 break; 18435 case STATUS_CHECK: 18436 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 18437 break; 18438 } 18439 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18440 (scsi_sense_key((uint8_t *)&sense_buf) == 18441 KEY_NOT_READY) && 18442 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 18443 status = ENXIO; 18444 } 18445 break; 18446 default: 18447 break; 18448 } 18449 break; 18450 default: 18451 break; 18452 } 18453 18454 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 18455 18456 return (status); 18457 } 18458 18459 18460 /* 18461 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 18462 * 18463 * Description: Issue the scsi PERSISTENT RESERVE IN command. 18464 * 18465 * Arguments: un 18466 * 18467 * Return Code: 0 - Success 18468 * EACCES 18469 * ENOTSUP 18470 * errno return code from sd_send_scsi_cmd() 18471 * 18472 * Context: Can sleep. Does not return until command is completed. 18473 */ 18474 18475 static int 18476 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 18477 uint16_t data_len, uchar_t *data_bufp) 18478 { 18479 struct scsi_extended_sense sense_buf; 18480 union scsi_cdb cdb; 18481 struct uscsi_cmd ucmd_buf; 18482 int status; 18483 int no_caller_buf = FALSE; 18484 18485 ASSERT(un != NULL); 18486 ASSERT(!mutex_owned(SD_MUTEX(un))); 18487 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 18488 18489 SD_TRACE(SD_LOG_IO, un, 18490 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 18491 18492 bzero(&cdb, sizeof (cdb)); 18493 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18494 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18495 if (data_bufp == NULL) { 18496 /* Allocate a default buf if the caller did not give one */ 18497 ASSERT(data_len == 0); 18498 data_len = MHIOC_RESV_KEY_SIZE; 18499 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 18500 no_caller_buf = TRUE; 18501 } 18502 18503 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 18504 cdb.cdb_opaque[1] = usr_cmd; 18505 FORMG1COUNT(&cdb, data_len); 18506 18507 ucmd_buf.uscsi_cdb = (char *)&cdb; 18508 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18509 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 18510 ucmd_buf.uscsi_buflen = data_len; 18511 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18512 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18513 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18514 ucmd_buf.uscsi_timeout = 60; 18515 18516 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18517 UIO_SYSSPACE, SD_PATH_STANDARD); 18518 18519 switch (status) { 18520 case 0: 18521 break; /* Success! */ 18522 case EIO: 18523 switch (ucmd_buf.uscsi_status) { 18524 case STATUS_RESERVATION_CONFLICT: 18525 status = EACCES; 18526 break; 18527 case STATUS_CHECK: 18528 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18529 (scsi_sense_key((uint8_t *)&sense_buf) == 18530 KEY_ILLEGAL_REQUEST)) { 18531 status = ENOTSUP; 18532 } 18533 break; 18534 default: 18535 break; 18536 } 18537 break; 18538 default: 18539 break; 18540 } 18541 18542 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 18543 18544 if (no_caller_buf == TRUE) { 18545 kmem_free(data_bufp, data_len); 18546 } 18547 18548 return (status); 18549 } 18550 18551 18552 /* 18553 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 18554 * 18555 * Description: This routine is the driver entry point for handling CD-ROM 18556 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 18557 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 18558 * device. 18559 * 18560 * Arguments: un - Pointer to soft state struct for the target. 18561 * usr_cmd SCSI-3 reservation facility command (one of 18562 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 18563 * SD_SCSI3_PREEMPTANDABORT) 18564 * usr_bufp - user provided pointer register, reserve descriptor or 18565 * preempt and abort structure (mhioc_register_t, 18566 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 18567 * 18568 * Return Code: 0 - Success 18569 * EACCES 18570 * ENOTSUP 18571 * errno return code from sd_send_scsi_cmd() 18572 * 18573 * Context: Can sleep. Does not return until command is completed. 18574 */ 18575 18576 static int 18577 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 18578 uchar_t *usr_bufp) 18579 { 18580 struct scsi_extended_sense sense_buf; 18581 union scsi_cdb cdb; 18582 struct uscsi_cmd ucmd_buf; 18583 int status; 18584 uchar_t data_len = sizeof (sd_prout_t); 18585 sd_prout_t *prp; 18586 18587 ASSERT(un != NULL); 18588 ASSERT(!mutex_owned(SD_MUTEX(un))); 18589 ASSERT(data_len == 24); /* required by scsi spec */ 18590 18591 SD_TRACE(SD_LOG_IO, un, 18592 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 18593 18594 if (usr_bufp == NULL) { 18595 return (EINVAL); 18596 } 18597 18598 bzero(&cdb, sizeof (cdb)); 18599 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18600 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18601 prp = kmem_zalloc(data_len, KM_SLEEP); 18602 18603 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 18604 cdb.cdb_opaque[1] = usr_cmd; 18605 FORMG1COUNT(&cdb, data_len); 18606 18607 ucmd_buf.uscsi_cdb = (char *)&cdb; 18608 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18609 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 18610 ucmd_buf.uscsi_buflen = data_len; 18611 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18612 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18613 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 18614 ucmd_buf.uscsi_timeout = 60; 18615 18616 switch (usr_cmd) { 18617 case SD_SCSI3_REGISTER: { 18618 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 18619 18620 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18621 bcopy(ptr->newkey.key, prp->service_key, 18622 MHIOC_RESV_KEY_SIZE); 18623 prp->aptpl = ptr->aptpl; 18624 break; 18625 } 18626 case SD_SCSI3_RESERVE: 18627 case SD_SCSI3_RELEASE: { 18628 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 18629 18630 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18631 prp->scope_address = BE_32(ptr->scope_specific_addr); 18632 cdb.cdb_opaque[2] = ptr->type; 18633 break; 18634 } 18635 case SD_SCSI3_PREEMPTANDABORT: { 18636 mhioc_preemptandabort_t *ptr = 18637 (mhioc_preemptandabort_t *)usr_bufp; 18638 18639 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18640 bcopy(ptr->victim_key.key, prp->service_key, 18641 MHIOC_RESV_KEY_SIZE); 18642 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 18643 cdb.cdb_opaque[2] = ptr->resvdesc.type; 18644 ucmd_buf.uscsi_flags |= USCSI_HEAD; 18645 break; 18646 } 18647 case SD_SCSI3_REGISTERANDIGNOREKEY: 18648 { 18649 mhioc_registerandignorekey_t *ptr; 18650 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 18651 bcopy(ptr->newkey.key, 18652 prp->service_key, MHIOC_RESV_KEY_SIZE); 18653 prp->aptpl = ptr->aptpl; 18654 break; 18655 } 18656 default: 18657 ASSERT(FALSE); 18658 break; 18659 } 18660 18661 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18662 UIO_SYSSPACE, SD_PATH_STANDARD); 18663 18664 switch (status) { 18665 case 0: 18666 break; /* Success! */ 18667 case EIO: 18668 switch (ucmd_buf.uscsi_status) { 18669 case STATUS_RESERVATION_CONFLICT: 18670 status = EACCES; 18671 break; 18672 case STATUS_CHECK: 18673 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18674 (scsi_sense_key((uint8_t *)&sense_buf) == 18675 KEY_ILLEGAL_REQUEST)) { 18676 status = ENOTSUP; 18677 } 18678 break; 18679 default: 18680 break; 18681 } 18682 break; 18683 default: 18684 break; 18685 } 18686 18687 kmem_free(prp, data_len); 18688 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 18689 return (status); 18690 } 18691 18692 18693 /* 18694 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 18695 * 18696 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 18697 * 18698 * Arguments: un - pointer to the target's soft state struct 18699 * dkc - pointer to the callback structure 18700 * 18701 * Return Code: 0 - success 18702 * errno-type error code 18703 * 18704 * Context: kernel thread context only. 18705 * 18706 * _______________________________________________________________ 18707 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 18708 * |FLUSH_VOLATILE| | operation | 18709 * |______________|______________|_________________________________| 18710 * | 0 | NULL | Synchronous flush on both | 18711 * | | | volatile and non-volatile cache | 18712 * |______________|______________|_________________________________| 18713 * | 1 | NULL | Synchronous flush on volatile | 18714 * | | | cache; disk drivers may suppress| 18715 * | | | flush if disk table indicates | 18716 * | | | non-volatile cache | 18717 * |______________|______________|_________________________________| 18718 * | 0 | !NULL | Asynchronous flush on both | 18719 * | | | volatile and non-volatile cache;| 18720 * |______________|______________|_________________________________| 18721 * | 1 | !NULL | Asynchronous flush on volatile | 18722 * | | | cache; disk drivers may suppress| 18723 * | | | flush if disk table indicates | 18724 * | | | non-volatile cache | 18725 * |______________|______________|_________________________________| 18726 * 18727 */ 18728 18729 static int 18730 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 18731 { 18732 struct sd_uscsi_info *uip; 18733 struct uscsi_cmd *uscmd; 18734 union scsi_cdb *cdb; 18735 struct buf *bp; 18736 int rval = 0; 18737 int is_async; 18738 18739 SD_TRACE(SD_LOG_IO, un, 18740 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 18741 18742 ASSERT(un != NULL); 18743 ASSERT(!mutex_owned(SD_MUTEX(un))); 18744 18745 if (dkc == NULL || dkc->dkc_callback == NULL) { 18746 is_async = FALSE; 18747 } else { 18748 is_async = TRUE; 18749 } 18750 18751 mutex_enter(SD_MUTEX(un)); 18752 /* check whether cache flush should be suppressed */ 18753 if (un->un_f_suppress_cache_flush == TRUE) { 18754 mutex_exit(SD_MUTEX(un)); 18755 /* 18756 * suppress the cache flush if the device is told to do 18757 * so by sd.conf or disk table 18758 */ 18759 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 18760 skip the cache flush since suppress_cache_flush is %d!\n", 18761 un->un_f_suppress_cache_flush); 18762 18763 if (is_async == TRUE) { 18764 /* invoke callback for asynchronous flush */ 18765 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 18766 } 18767 return (rval); 18768 } 18769 mutex_exit(SD_MUTEX(un)); 18770 18771 /* 18772 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 18773 * set properly 18774 */ 18775 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 18776 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 18777 18778 mutex_enter(SD_MUTEX(un)); 18779 if (dkc != NULL && un->un_f_sync_nv_supported && 18780 (dkc->dkc_flag & FLUSH_VOLATILE)) { 18781 /* 18782 * if the device supports SYNC_NV bit, turn on 18783 * the SYNC_NV bit to only flush volatile cache 18784 */ 18785 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 18786 } 18787 mutex_exit(SD_MUTEX(un)); 18788 18789 /* 18790 * First get some memory for the uscsi_cmd struct and cdb 18791 * and initialize for SYNCHRONIZE_CACHE cmd. 18792 */ 18793 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 18794 uscmd->uscsi_cdblen = CDB_GROUP1; 18795 uscmd->uscsi_cdb = (caddr_t)cdb; 18796 uscmd->uscsi_bufaddr = NULL; 18797 uscmd->uscsi_buflen = 0; 18798 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 18799 uscmd->uscsi_rqlen = SENSE_LENGTH; 18800 uscmd->uscsi_rqresid = SENSE_LENGTH; 18801 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18802 uscmd->uscsi_timeout = sd_io_time; 18803 18804 /* 18805 * Allocate an sd_uscsi_info struct and fill it with the info 18806 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 18807 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 18808 * since we allocate the buf here in this function, we do not 18809 * need to preserve the prior contents of b_private. 18810 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 18811 */ 18812 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 18813 uip->ui_flags = SD_PATH_DIRECT; 18814 uip->ui_cmdp = uscmd; 18815 18816 bp = getrbuf(KM_SLEEP); 18817 bp->b_private = uip; 18818 18819 /* 18820 * Setup buffer to carry uscsi request. 18821 */ 18822 bp->b_flags = B_BUSY; 18823 bp->b_bcount = 0; 18824 bp->b_blkno = 0; 18825 18826 if (is_async == TRUE) { 18827 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 18828 uip->ui_dkc = *dkc; 18829 } 18830 18831 bp->b_edev = SD_GET_DEV(un); 18832 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 18833 18834 (void) sd_uscsi_strategy(bp); 18835 18836 /* 18837 * If synchronous request, wait for completion 18838 * If async just return and let b_iodone callback 18839 * cleanup. 18840 * NOTE: On return, u_ncmds_in_driver will be decremented, 18841 * but it was also incremented in sd_uscsi_strategy(), so 18842 * we should be ok. 18843 */ 18844 if (is_async == FALSE) { 18845 (void) biowait(bp); 18846 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 18847 } 18848 18849 return (rval); 18850 } 18851 18852 18853 static int 18854 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 18855 { 18856 struct sd_uscsi_info *uip; 18857 struct uscsi_cmd *uscmd; 18858 uint8_t *sense_buf; 18859 struct sd_lun *un; 18860 int status; 18861 union scsi_cdb *cdb; 18862 18863 uip = (struct sd_uscsi_info *)(bp->b_private); 18864 ASSERT(uip != NULL); 18865 18866 uscmd = uip->ui_cmdp; 18867 ASSERT(uscmd != NULL); 18868 18869 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 18870 ASSERT(sense_buf != NULL); 18871 18872 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 18873 ASSERT(un != NULL); 18874 18875 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 18876 18877 status = geterror(bp); 18878 switch (status) { 18879 case 0: 18880 break; /* Success! */ 18881 case EIO: 18882 switch (uscmd->uscsi_status) { 18883 case STATUS_RESERVATION_CONFLICT: 18884 /* Ignore reservation conflict */ 18885 status = 0; 18886 goto done; 18887 18888 case STATUS_CHECK: 18889 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 18890 (scsi_sense_key(sense_buf) == 18891 KEY_ILLEGAL_REQUEST)) { 18892 /* Ignore Illegal Request error */ 18893 if (cdb->cdb_un.tag|SD_SYNC_NV_BIT) { 18894 mutex_enter(SD_MUTEX(un)); 18895 un->un_f_sync_nv_supported = FALSE; 18896 mutex_exit(SD_MUTEX(un)); 18897 status = 0; 18898 SD_TRACE(SD_LOG_IO, un, 18899 "un_f_sync_nv_supported \ 18900 is set to false.\n"); 18901 goto done; 18902 } 18903 18904 mutex_enter(SD_MUTEX(un)); 18905 un->un_f_sync_cache_supported = FALSE; 18906 mutex_exit(SD_MUTEX(un)); 18907 SD_TRACE(SD_LOG_IO, un, 18908 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 18909 un_f_sync_cache_supported set to false \ 18910 with asc = %x, ascq = %x\n", 18911 scsi_sense_asc(sense_buf), 18912 scsi_sense_ascq(sense_buf)); 18913 status = ENOTSUP; 18914 goto done; 18915 } 18916 break; 18917 default: 18918 break; 18919 } 18920 /* FALLTHRU */ 18921 default: 18922 /* 18923 * Don't log an error message if this device 18924 * has removable media. 18925 */ 18926 if (!un->un_f_has_removable_media) { 18927 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18928 "SYNCHRONIZE CACHE command failed (%d)\n", status); 18929 } 18930 break; 18931 } 18932 18933 done: 18934 if (uip->ui_dkc.dkc_callback != NULL) { 18935 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 18936 } 18937 18938 ASSERT((bp->b_flags & B_REMAPPED) == 0); 18939 freerbuf(bp); 18940 kmem_free(uip, sizeof (struct sd_uscsi_info)); 18941 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 18942 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 18943 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 18944 18945 return (status); 18946 } 18947 18948 18949 /* 18950 * Function: sd_send_scsi_GET_CONFIGURATION 18951 * 18952 * Description: Issues the get configuration command to the device. 18953 * Called from sd_check_for_writable_cd & sd_get_media_info 18954 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 18955 * Arguments: un 18956 * ucmdbuf 18957 * rqbuf 18958 * rqbuflen 18959 * bufaddr 18960 * buflen 18961 * path_flag 18962 * 18963 * Return Code: 0 - Success 18964 * errno return code from sd_send_scsi_cmd() 18965 * 18966 * Context: Can sleep. Does not return until command is completed. 18967 * 18968 */ 18969 18970 static int 18971 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 18972 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 18973 int path_flag) 18974 { 18975 char cdb[CDB_GROUP1]; 18976 int status; 18977 18978 ASSERT(un != NULL); 18979 ASSERT(!mutex_owned(SD_MUTEX(un))); 18980 ASSERT(bufaddr != NULL); 18981 ASSERT(ucmdbuf != NULL); 18982 ASSERT(rqbuf != NULL); 18983 18984 SD_TRACE(SD_LOG_IO, un, 18985 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 18986 18987 bzero(cdb, sizeof (cdb)); 18988 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 18989 bzero(rqbuf, rqbuflen); 18990 bzero(bufaddr, buflen); 18991 18992 /* 18993 * Set up cdb field for the get configuration command. 18994 */ 18995 cdb[0] = SCMD_GET_CONFIGURATION; 18996 cdb[1] = 0x02; /* Requested Type */ 18997 cdb[8] = SD_PROFILE_HEADER_LEN; 18998 ucmdbuf->uscsi_cdb = cdb; 18999 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19000 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19001 ucmdbuf->uscsi_buflen = buflen; 19002 ucmdbuf->uscsi_timeout = sd_io_time; 19003 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19004 ucmdbuf->uscsi_rqlen = rqbuflen; 19005 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19006 19007 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 19008 UIO_SYSSPACE, path_flag); 19009 19010 switch (status) { 19011 case 0: 19012 break; /* Success! */ 19013 case EIO: 19014 switch (ucmdbuf->uscsi_status) { 19015 case STATUS_RESERVATION_CONFLICT: 19016 status = EACCES; 19017 break; 19018 default: 19019 break; 19020 } 19021 break; 19022 default: 19023 break; 19024 } 19025 19026 if (status == 0) { 19027 SD_DUMP_MEMORY(un, SD_LOG_IO, 19028 "sd_send_scsi_GET_CONFIGURATION: data", 19029 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19030 } 19031 19032 SD_TRACE(SD_LOG_IO, un, 19033 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 19034 19035 return (status); 19036 } 19037 19038 /* 19039 * Function: sd_send_scsi_feature_GET_CONFIGURATION 19040 * 19041 * Description: Issues the get configuration command to the device to 19042 * retrieve a specific feature. Called from 19043 * sd_check_for_writable_cd & sd_set_mmc_caps. 19044 * Arguments: un 19045 * ucmdbuf 19046 * rqbuf 19047 * rqbuflen 19048 * bufaddr 19049 * buflen 19050 * feature 19051 * 19052 * Return Code: 0 - Success 19053 * errno return code from sd_send_scsi_cmd() 19054 * 19055 * Context: Can sleep. Does not return until command is completed. 19056 * 19057 */ 19058 static int 19059 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 19060 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 19061 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 19062 { 19063 char cdb[CDB_GROUP1]; 19064 int status; 19065 19066 ASSERT(un != NULL); 19067 ASSERT(!mutex_owned(SD_MUTEX(un))); 19068 ASSERT(bufaddr != NULL); 19069 ASSERT(ucmdbuf != NULL); 19070 ASSERT(rqbuf != NULL); 19071 19072 SD_TRACE(SD_LOG_IO, un, 19073 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 19074 19075 bzero(cdb, sizeof (cdb)); 19076 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 19077 bzero(rqbuf, rqbuflen); 19078 bzero(bufaddr, buflen); 19079 19080 /* 19081 * Set up cdb field for the get configuration command. 19082 */ 19083 cdb[0] = SCMD_GET_CONFIGURATION; 19084 cdb[1] = 0x02; /* Requested Type */ 19085 cdb[3] = feature; 19086 cdb[8] = buflen; 19087 ucmdbuf->uscsi_cdb = cdb; 19088 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19089 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19090 ucmdbuf->uscsi_buflen = buflen; 19091 ucmdbuf->uscsi_timeout = sd_io_time; 19092 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19093 ucmdbuf->uscsi_rqlen = rqbuflen; 19094 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19095 19096 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 19097 UIO_SYSSPACE, path_flag); 19098 19099 switch (status) { 19100 case 0: 19101 break; /* Success! */ 19102 case EIO: 19103 switch (ucmdbuf->uscsi_status) { 19104 case STATUS_RESERVATION_CONFLICT: 19105 status = EACCES; 19106 break; 19107 default: 19108 break; 19109 } 19110 break; 19111 default: 19112 break; 19113 } 19114 19115 if (status == 0) { 19116 SD_DUMP_MEMORY(un, SD_LOG_IO, 19117 "sd_send_scsi_feature_GET_CONFIGURATION: data", 19118 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19119 } 19120 19121 SD_TRACE(SD_LOG_IO, un, 19122 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 19123 19124 return (status); 19125 } 19126 19127 19128 /* 19129 * Function: sd_send_scsi_MODE_SENSE 19130 * 19131 * Description: Utility function for issuing a scsi MODE SENSE command. 19132 * Note: This routine uses a consistent implementation for Group0, 19133 * Group1, and Group2 commands across all platforms. ATAPI devices 19134 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19135 * 19136 * Arguments: un - pointer to the softstate struct for the target. 19137 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19138 * CDB_GROUP[1|2] (10 byte). 19139 * bufaddr - buffer for page data retrieved from the target. 19140 * buflen - size of page to be retrieved. 19141 * page_code - page code of data to be retrieved from the target. 19142 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19143 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19144 * to use the USCSI "direct" chain and bypass the normal 19145 * command waitq. 19146 * 19147 * Return Code: 0 - Success 19148 * errno return code from sd_send_scsi_cmd() 19149 * 19150 * Context: Can sleep. Does not return until command is completed. 19151 */ 19152 19153 static int 19154 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19155 size_t buflen, uchar_t page_code, int path_flag) 19156 { 19157 struct scsi_extended_sense sense_buf; 19158 union scsi_cdb cdb; 19159 struct uscsi_cmd ucmd_buf; 19160 int status; 19161 int headlen; 19162 19163 ASSERT(un != NULL); 19164 ASSERT(!mutex_owned(SD_MUTEX(un))); 19165 ASSERT(bufaddr != NULL); 19166 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19167 (cdbsize == CDB_GROUP2)); 19168 19169 SD_TRACE(SD_LOG_IO, un, 19170 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 19171 19172 bzero(&cdb, sizeof (cdb)); 19173 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19174 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19175 bzero(bufaddr, buflen); 19176 19177 if (cdbsize == CDB_GROUP0) { 19178 cdb.scc_cmd = SCMD_MODE_SENSE; 19179 cdb.cdb_opaque[2] = page_code; 19180 FORMG0COUNT(&cdb, buflen); 19181 headlen = MODE_HEADER_LENGTH; 19182 } else { 19183 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 19184 cdb.cdb_opaque[2] = page_code; 19185 FORMG1COUNT(&cdb, buflen); 19186 headlen = MODE_HEADER_LENGTH_GRP2; 19187 } 19188 19189 ASSERT(headlen <= buflen); 19190 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19191 19192 ucmd_buf.uscsi_cdb = (char *)&cdb; 19193 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19194 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19195 ucmd_buf.uscsi_buflen = buflen; 19196 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19197 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19198 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19199 ucmd_buf.uscsi_timeout = 60; 19200 19201 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19202 UIO_SYSSPACE, path_flag); 19203 19204 switch (status) { 19205 case 0: 19206 /* 19207 * sr_check_wp() uses 0x3f page code and check the header of 19208 * mode page to determine if target device is write-protected. 19209 * But some USB devices return 0 bytes for 0x3f page code. For 19210 * this case, make sure that mode page header is returned at 19211 * least. 19212 */ 19213 if (buflen - ucmd_buf.uscsi_resid < headlen) 19214 status = EIO; 19215 break; /* Success! */ 19216 case EIO: 19217 switch (ucmd_buf.uscsi_status) { 19218 case STATUS_RESERVATION_CONFLICT: 19219 status = EACCES; 19220 break; 19221 default: 19222 break; 19223 } 19224 break; 19225 default: 19226 break; 19227 } 19228 19229 if (status == 0) { 19230 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 19231 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19232 } 19233 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 19234 19235 return (status); 19236 } 19237 19238 19239 /* 19240 * Function: sd_send_scsi_MODE_SELECT 19241 * 19242 * Description: Utility function for issuing a scsi MODE SELECT command. 19243 * Note: This routine uses a consistent implementation for Group0, 19244 * Group1, and Group2 commands across all platforms. ATAPI devices 19245 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19246 * 19247 * Arguments: un - pointer to the softstate struct for the target. 19248 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19249 * CDB_GROUP[1|2] (10 byte). 19250 * bufaddr - buffer for page data retrieved from the target. 19251 * buflen - size of page to be retrieved. 19252 * save_page - boolean to determin if SP bit should be set. 19253 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19254 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19255 * to use the USCSI "direct" chain and bypass the normal 19256 * command waitq. 19257 * 19258 * Return Code: 0 - Success 19259 * errno return code from sd_send_scsi_cmd() 19260 * 19261 * Context: Can sleep. Does not return until command is completed. 19262 */ 19263 19264 static int 19265 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19266 size_t buflen, uchar_t save_page, int path_flag) 19267 { 19268 struct scsi_extended_sense sense_buf; 19269 union scsi_cdb cdb; 19270 struct uscsi_cmd ucmd_buf; 19271 int status; 19272 19273 ASSERT(un != NULL); 19274 ASSERT(!mutex_owned(SD_MUTEX(un))); 19275 ASSERT(bufaddr != NULL); 19276 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19277 (cdbsize == CDB_GROUP2)); 19278 19279 SD_TRACE(SD_LOG_IO, un, 19280 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 19281 19282 bzero(&cdb, sizeof (cdb)); 19283 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19284 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19285 19286 /* Set the PF bit for many third party drives */ 19287 cdb.cdb_opaque[1] = 0x10; 19288 19289 /* Set the savepage(SP) bit if given */ 19290 if (save_page == SD_SAVE_PAGE) { 19291 cdb.cdb_opaque[1] |= 0x01; 19292 } 19293 19294 if (cdbsize == CDB_GROUP0) { 19295 cdb.scc_cmd = SCMD_MODE_SELECT; 19296 FORMG0COUNT(&cdb, buflen); 19297 } else { 19298 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 19299 FORMG1COUNT(&cdb, buflen); 19300 } 19301 19302 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19303 19304 ucmd_buf.uscsi_cdb = (char *)&cdb; 19305 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19306 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19307 ucmd_buf.uscsi_buflen = buflen; 19308 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19309 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19310 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19311 ucmd_buf.uscsi_timeout = 60; 19312 19313 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19314 UIO_SYSSPACE, path_flag); 19315 19316 switch (status) { 19317 case 0: 19318 break; /* Success! */ 19319 case EIO: 19320 switch (ucmd_buf.uscsi_status) { 19321 case STATUS_RESERVATION_CONFLICT: 19322 status = EACCES; 19323 break; 19324 default: 19325 break; 19326 } 19327 break; 19328 default: 19329 break; 19330 } 19331 19332 if (status == 0) { 19333 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 19334 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19335 } 19336 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 19337 19338 return (status); 19339 } 19340 19341 19342 /* 19343 * Function: sd_send_scsi_RDWR 19344 * 19345 * Description: Issue a scsi READ or WRITE command with the given parameters. 19346 * 19347 * Arguments: un: Pointer to the sd_lun struct for the target. 19348 * cmd: SCMD_READ or SCMD_WRITE 19349 * bufaddr: Address of caller's buffer to receive the RDWR data 19350 * buflen: Length of caller's buffer receive the RDWR data. 19351 * start_block: Block number for the start of the RDWR operation. 19352 * (Assumes target-native block size.) 19353 * residp: Pointer to variable to receive the redisual of the 19354 * RDWR operation (may be NULL of no residual requested). 19355 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19356 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19357 * to use the USCSI "direct" chain and bypass the normal 19358 * command waitq. 19359 * 19360 * Return Code: 0 - Success 19361 * errno return code from sd_send_scsi_cmd() 19362 * 19363 * Context: Can sleep. Does not return until command is completed. 19364 */ 19365 19366 static int 19367 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 19368 size_t buflen, daddr_t start_block, int path_flag) 19369 { 19370 struct scsi_extended_sense sense_buf; 19371 union scsi_cdb cdb; 19372 struct uscsi_cmd ucmd_buf; 19373 uint32_t block_count; 19374 int status; 19375 int cdbsize; 19376 uchar_t flag; 19377 19378 ASSERT(un != NULL); 19379 ASSERT(!mutex_owned(SD_MUTEX(un))); 19380 ASSERT(bufaddr != NULL); 19381 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 19382 19383 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 19384 19385 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 19386 return (EINVAL); 19387 } 19388 19389 mutex_enter(SD_MUTEX(un)); 19390 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 19391 mutex_exit(SD_MUTEX(un)); 19392 19393 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 19394 19395 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 19396 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 19397 bufaddr, buflen, start_block, block_count); 19398 19399 bzero(&cdb, sizeof (cdb)); 19400 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19401 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19402 19403 /* Compute CDB size to use */ 19404 if (start_block > 0xffffffff) 19405 cdbsize = CDB_GROUP4; 19406 else if ((start_block & 0xFFE00000) || 19407 (un->un_f_cfg_is_atapi == TRUE)) 19408 cdbsize = CDB_GROUP1; 19409 else 19410 cdbsize = CDB_GROUP0; 19411 19412 switch (cdbsize) { 19413 case CDB_GROUP0: /* 6-byte CDBs */ 19414 cdb.scc_cmd = cmd; 19415 FORMG0ADDR(&cdb, start_block); 19416 FORMG0COUNT(&cdb, block_count); 19417 break; 19418 case CDB_GROUP1: /* 10-byte CDBs */ 19419 cdb.scc_cmd = cmd | SCMD_GROUP1; 19420 FORMG1ADDR(&cdb, start_block); 19421 FORMG1COUNT(&cdb, block_count); 19422 break; 19423 case CDB_GROUP4: /* 16-byte CDBs */ 19424 cdb.scc_cmd = cmd | SCMD_GROUP4; 19425 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 19426 FORMG4COUNT(&cdb, block_count); 19427 break; 19428 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 19429 default: 19430 /* All others reserved */ 19431 return (EINVAL); 19432 } 19433 19434 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 19435 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19436 19437 ucmd_buf.uscsi_cdb = (char *)&cdb; 19438 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19439 ucmd_buf.uscsi_bufaddr = bufaddr; 19440 ucmd_buf.uscsi_buflen = buflen; 19441 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19442 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19443 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 19444 ucmd_buf.uscsi_timeout = 60; 19445 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19446 UIO_SYSSPACE, path_flag); 19447 switch (status) { 19448 case 0: 19449 break; /* Success! */ 19450 case EIO: 19451 switch (ucmd_buf.uscsi_status) { 19452 case STATUS_RESERVATION_CONFLICT: 19453 status = EACCES; 19454 break; 19455 default: 19456 break; 19457 } 19458 break; 19459 default: 19460 break; 19461 } 19462 19463 if (status == 0) { 19464 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 19465 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19466 } 19467 19468 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 19469 19470 return (status); 19471 } 19472 19473 19474 /* 19475 * Function: sd_send_scsi_LOG_SENSE 19476 * 19477 * Description: Issue a scsi LOG_SENSE command with the given parameters. 19478 * 19479 * Arguments: un: Pointer to the sd_lun struct for the target. 19480 * 19481 * Return Code: 0 - Success 19482 * errno return code from sd_send_scsi_cmd() 19483 * 19484 * Context: Can sleep. Does not return until command is completed. 19485 */ 19486 19487 static int 19488 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 19489 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 19490 int path_flag) 19491 19492 { 19493 struct scsi_extended_sense sense_buf; 19494 union scsi_cdb cdb; 19495 struct uscsi_cmd ucmd_buf; 19496 int status; 19497 19498 ASSERT(un != NULL); 19499 ASSERT(!mutex_owned(SD_MUTEX(un))); 19500 19501 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 19502 19503 bzero(&cdb, sizeof (cdb)); 19504 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19505 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19506 19507 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 19508 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 19509 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 19510 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 19511 FORMG1COUNT(&cdb, buflen); 19512 19513 ucmd_buf.uscsi_cdb = (char *)&cdb; 19514 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19515 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19516 ucmd_buf.uscsi_buflen = buflen; 19517 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19518 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19519 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19520 ucmd_buf.uscsi_timeout = 60; 19521 19522 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19523 UIO_SYSSPACE, path_flag); 19524 19525 switch (status) { 19526 case 0: 19527 break; 19528 case EIO: 19529 switch (ucmd_buf.uscsi_status) { 19530 case STATUS_RESERVATION_CONFLICT: 19531 status = EACCES; 19532 break; 19533 case STATUS_CHECK: 19534 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19535 (scsi_sense_key((uint8_t *)&sense_buf) == 19536 KEY_ILLEGAL_REQUEST) && 19537 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 19538 /* 19539 * ASC 0x24: INVALID FIELD IN CDB 19540 */ 19541 switch (page_code) { 19542 case START_STOP_CYCLE_PAGE: 19543 /* 19544 * The start stop cycle counter is 19545 * implemented as page 0x31 in earlier 19546 * generation disks. In new generation 19547 * disks the start stop cycle counter is 19548 * implemented as page 0xE. To properly 19549 * handle this case if an attempt for 19550 * log page 0xE is made and fails we 19551 * will try again using page 0x31. 19552 * 19553 * Network storage BU committed to 19554 * maintain the page 0x31 for this 19555 * purpose and will not have any other 19556 * page implemented with page code 0x31 19557 * until all disks transition to the 19558 * standard page. 19559 */ 19560 mutex_enter(SD_MUTEX(un)); 19561 un->un_start_stop_cycle_page = 19562 START_STOP_CYCLE_VU_PAGE; 19563 cdb.cdb_opaque[2] = 19564 (char)(page_control << 6) | 19565 un->un_start_stop_cycle_page; 19566 mutex_exit(SD_MUTEX(un)); 19567 status = sd_send_scsi_cmd( 19568 SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19569 UIO_SYSSPACE, path_flag); 19570 19571 break; 19572 case TEMPERATURE_PAGE: 19573 status = ENOTTY; 19574 break; 19575 default: 19576 break; 19577 } 19578 } 19579 break; 19580 default: 19581 break; 19582 } 19583 break; 19584 default: 19585 break; 19586 } 19587 19588 if (status == 0) { 19589 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 19590 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19591 } 19592 19593 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 19594 19595 return (status); 19596 } 19597 19598 19599 /* 19600 * Function: sdioctl 19601 * 19602 * Description: Driver's ioctl(9e) entry point function. 19603 * 19604 * Arguments: dev - device number 19605 * cmd - ioctl operation to be performed 19606 * arg - user argument, contains data to be set or reference 19607 * parameter for get 19608 * flag - bit flag, indicating open settings, 32/64 bit type 19609 * cred_p - user credential pointer 19610 * rval_p - calling process return value (OPT) 19611 * 19612 * Return Code: EINVAL 19613 * ENOTTY 19614 * ENXIO 19615 * EIO 19616 * EFAULT 19617 * ENOTSUP 19618 * EPERM 19619 * 19620 * Context: Called from the device switch at normal priority. 19621 */ 19622 19623 static int 19624 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 19625 { 19626 struct sd_lun *un = NULL; 19627 int err = 0; 19628 int i = 0; 19629 cred_t *cr; 19630 int tmprval = EINVAL; 19631 int is_valid; 19632 19633 /* 19634 * All device accesses go thru sdstrategy where we check on suspend 19635 * status 19636 */ 19637 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 19638 return (ENXIO); 19639 } 19640 19641 ASSERT(!mutex_owned(SD_MUTEX(un))); 19642 19643 19644 is_valid = SD_IS_VALID_LABEL(un); 19645 19646 /* 19647 * Moved this wait from sd_uscsi_strategy to here for 19648 * reasons of deadlock prevention. Internal driver commands, 19649 * specifically those to change a devices power level, result 19650 * in a call to sd_uscsi_strategy. 19651 */ 19652 mutex_enter(SD_MUTEX(un)); 19653 while ((un->un_state == SD_STATE_SUSPENDED) || 19654 (un->un_state == SD_STATE_PM_CHANGING)) { 19655 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 19656 } 19657 /* 19658 * Twiddling the counter here protects commands from now 19659 * through to the top of sd_uscsi_strategy. Without the 19660 * counter inc. a power down, for example, could get in 19661 * after the above check for state is made and before 19662 * execution gets to the top of sd_uscsi_strategy. 19663 * That would cause problems. 19664 */ 19665 un->un_ncmds_in_driver++; 19666 19667 if (!is_valid && 19668 (flag & (FNDELAY | FNONBLOCK))) { 19669 switch (cmd) { 19670 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 19671 case DKIOCGVTOC: 19672 case DKIOCGAPART: 19673 case DKIOCPARTINFO: 19674 case DKIOCSGEOM: 19675 case DKIOCSAPART: 19676 case DKIOCGETEFI: 19677 case DKIOCPARTITION: 19678 case DKIOCSVTOC: 19679 case DKIOCSETEFI: 19680 case DKIOCGMBOOT: 19681 case DKIOCSMBOOT: 19682 case DKIOCG_PHYGEOM: 19683 case DKIOCG_VIRTGEOM: 19684 /* let cmlb handle it */ 19685 goto skip_ready_valid; 19686 19687 case CDROMPAUSE: 19688 case CDROMRESUME: 19689 case CDROMPLAYMSF: 19690 case CDROMPLAYTRKIND: 19691 case CDROMREADTOCHDR: 19692 case CDROMREADTOCENTRY: 19693 case CDROMSTOP: 19694 case CDROMSTART: 19695 case CDROMVOLCTRL: 19696 case CDROMSUBCHNL: 19697 case CDROMREADMODE2: 19698 case CDROMREADMODE1: 19699 case CDROMREADOFFSET: 19700 case CDROMSBLKMODE: 19701 case CDROMGBLKMODE: 19702 case CDROMGDRVSPEED: 19703 case CDROMSDRVSPEED: 19704 case CDROMCDDA: 19705 case CDROMCDXA: 19706 case CDROMSUBCODE: 19707 if (!ISCD(un)) { 19708 un->un_ncmds_in_driver--; 19709 ASSERT(un->un_ncmds_in_driver >= 0); 19710 mutex_exit(SD_MUTEX(un)); 19711 return (ENOTTY); 19712 } 19713 break; 19714 case FDEJECT: 19715 case DKIOCEJECT: 19716 case CDROMEJECT: 19717 if (!un->un_f_eject_media_supported) { 19718 un->un_ncmds_in_driver--; 19719 ASSERT(un->un_ncmds_in_driver >= 0); 19720 mutex_exit(SD_MUTEX(un)); 19721 return (ENOTTY); 19722 } 19723 break; 19724 case DKIOCFLUSHWRITECACHE: 19725 mutex_exit(SD_MUTEX(un)); 19726 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19727 if (err != 0) { 19728 mutex_enter(SD_MUTEX(un)); 19729 un->un_ncmds_in_driver--; 19730 ASSERT(un->un_ncmds_in_driver >= 0); 19731 mutex_exit(SD_MUTEX(un)); 19732 return (EIO); 19733 } 19734 mutex_enter(SD_MUTEX(un)); 19735 /* FALLTHROUGH */ 19736 case DKIOCREMOVABLE: 19737 case DKIOCHOTPLUGGABLE: 19738 case DKIOCINFO: 19739 case DKIOCGMEDIAINFO: 19740 case MHIOCENFAILFAST: 19741 case MHIOCSTATUS: 19742 case MHIOCTKOWN: 19743 case MHIOCRELEASE: 19744 case MHIOCGRP_INKEYS: 19745 case MHIOCGRP_INRESV: 19746 case MHIOCGRP_REGISTER: 19747 case MHIOCGRP_RESERVE: 19748 case MHIOCGRP_PREEMPTANDABORT: 19749 case MHIOCGRP_REGISTERANDIGNOREKEY: 19750 case CDROMCLOSETRAY: 19751 case USCSICMD: 19752 goto skip_ready_valid; 19753 default: 19754 break; 19755 } 19756 19757 mutex_exit(SD_MUTEX(un)); 19758 err = sd_ready_and_valid(un); 19759 mutex_enter(SD_MUTEX(un)); 19760 19761 if (err != SD_READY_VALID) { 19762 switch (cmd) { 19763 case DKIOCSTATE: 19764 case CDROMGDRVSPEED: 19765 case CDROMSDRVSPEED: 19766 case FDEJECT: /* for eject command */ 19767 case DKIOCEJECT: 19768 case CDROMEJECT: 19769 case DKIOCREMOVABLE: 19770 case DKIOCHOTPLUGGABLE: 19771 break; 19772 default: 19773 if (un->un_f_has_removable_media) { 19774 err = ENXIO; 19775 } else { 19776 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 19777 if (err == SD_RESERVED_BY_OTHERS) { 19778 err = EACCES; 19779 } else { 19780 err = EIO; 19781 } 19782 } 19783 un->un_ncmds_in_driver--; 19784 ASSERT(un->un_ncmds_in_driver >= 0); 19785 mutex_exit(SD_MUTEX(un)); 19786 return (err); 19787 } 19788 } 19789 } 19790 19791 skip_ready_valid: 19792 mutex_exit(SD_MUTEX(un)); 19793 19794 switch (cmd) { 19795 case DKIOCINFO: 19796 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 19797 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 19798 break; 19799 19800 case DKIOCGMEDIAINFO: 19801 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 19802 err = sd_get_media_info(dev, (caddr_t)arg, flag); 19803 break; 19804 19805 case DKIOCGGEOM: 19806 case DKIOCGVTOC: 19807 case DKIOCGAPART: 19808 case DKIOCPARTINFO: 19809 case DKIOCSGEOM: 19810 case DKIOCSAPART: 19811 case DKIOCGETEFI: 19812 case DKIOCPARTITION: 19813 case DKIOCSVTOC: 19814 case DKIOCSETEFI: 19815 case DKIOCGMBOOT: 19816 case DKIOCSMBOOT: 19817 case DKIOCG_PHYGEOM: 19818 case DKIOCG_VIRTGEOM: 19819 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 19820 19821 /* TUR should spin up */ 19822 19823 if (un->un_f_has_removable_media) 19824 err = sd_send_scsi_TEST_UNIT_READY(un, 19825 SD_CHECK_FOR_MEDIA); 19826 else 19827 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19828 19829 if (err != 0) 19830 break; 19831 19832 err = cmlb_ioctl(un->un_cmlbhandle, dev, 19833 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 19834 19835 if ((err == 0) && 19836 ((cmd == DKIOCSETEFI) || 19837 (un->un_f_pkstats_enabled) && 19838 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC))) { 19839 19840 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 19841 (void *)SD_PATH_DIRECT); 19842 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 19843 sd_set_pstats(un); 19844 SD_TRACE(SD_LOG_IO_PARTITION, un, 19845 "sd_ioctl: un:0x%p pstats created and " 19846 "set\n", un); 19847 } 19848 } 19849 19850 if ((cmd == DKIOCSVTOC) || 19851 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 19852 19853 mutex_enter(SD_MUTEX(un)); 19854 if (un->un_f_devid_supported && 19855 (un->un_f_opt_fab_devid == TRUE)) { 19856 if (un->un_devid == NULL) { 19857 sd_register_devid(un, SD_DEVINFO(un), 19858 SD_TARGET_IS_UNRESERVED); 19859 } else { 19860 /* 19861 * The device id for this disk 19862 * has been fabricated. The 19863 * device id must be preserved 19864 * by writing it back out to 19865 * disk. 19866 */ 19867 if (sd_write_deviceid(un) != 0) { 19868 ddi_devid_free(un->un_devid); 19869 un->un_devid = NULL; 19870 } 19871 } 19872 } 19873 mutex_exit(SD_MUTEX(un)); 19874 } 19875 19876 break; 19877 19878 case DKIOCLOCK: 19879 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 19880 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19881 SD_PATH_STANDARD); 19882 break; 19883 19884 case DKIOCUNLOCK: 19885 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 19886 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 19887 SD_PATH_STANDARD); 19888 break; 19889 19890 case DKIOCSTATE: { 19891 enum dkio_state state; 19892 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 19893 19894 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 19895 err = EFAULT; 19896 } else { 19897 err = sd_check_media(dev, state); 19898 if (err == 0) { 19899 if (ddi_copyout(&un->un_mediastate, (void *)arg, 19900 sizeof (int), flag) != 0) 19901 err = EFAULT; 19902 } 19903 } 19904 break; 19905 } 19906 19907 case DKIOCREMOVABLE: 19908 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 19909 i = un->un_f_has_removable_media ? 1 : 0; 19910 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 19911 err = EFAULT; 19912 } else { 19913 err = 0; 19914 } 19915 break; 19916 19917 case DKIOCHOTPLUGGABLE: 19918 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 19919 i = un->un_f_is_hotpluggable ? 1 : 0; 19920 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 19921 err = EFAULT; 19922 } else { 19923 err = 0; 19924 } 19925 break; 19926 19927 case DKIOCGTEMPERATURE: 19928 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 19929 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 19930 break; 19931 19932 case MHIOCENFAILFAST: 19933 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 19934 if ((err = drv_priv(cred_p)) == 0) { 19935 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 19936 } 19937 break; 19938 19939 case MHIOCTKOWN: 19940 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 19941 if ((err = drv_priv(cred_p)) == 0) { 19942 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 19943 } 19944 break; 19945 19946 case MHIOCRELEASE: 19947 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 19948 if ((err = drv_priv(cred_p)) == 0) { 19949 err = sd_mhdioc_release(dev); 19950 } 19951 break; 19952 19953 case MHIOCSTATUS: 19954 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 19955 if ((err = drv_priv(cred_p)) == 0) { 19956 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 19957 case 0: 19958 err = 0; 19959 break; 19960 case EACCES: 19961 *rval_p = 1; 19962 err = 0; 19963 break; 19964 default: 19965 err = EIO; 19966 break; 19967 } 19968 } 19969 break; 19970 19971 case MHIOCQRESERVE: 19972 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 19973 if ((err = drv_priv(cred_p)) == 0) { 19974 err = sd_reserve_release(dev, SD_RESERVE); 19975 } 19976 break; 19977 19978 case MHIOCREREGISTERDEVID: 19979 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 19980 if (drv_priv(cred_p) == EPERM) { 19981 err = EPERM; 19982 } else if (!un->un_f_devid_supported) { 19983 err = ENOTTY; 19984 } else { 19985 err = sd_mhdioc_register_devid(dev); 19986 } 19987 break; 19988 19989 case MHIOCGRP_INKEYS: 19990 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 19991 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 19992 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19993 err = ENOTSUP; 19994 } else { 19995 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 19996 flag); 19997 } 19998 } 19999 break; 20000 20001 case MHIOCGRP_INRESV: 20002 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 20003 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 20004 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20005 err = ENOTSUP; 20006 } else { 20007 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 20008 } 20009 } 20010 break; 20011 20012 case MHIOCGRP_REGISTER: 20013 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 20014 if ((err = drv_priv(cred_p)) != EPERM) { 20015 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20016 err = ENOTSUP; 20017 } else if (arg != NULL) { 20018 mhioc_register_t reg; 20019 if (ddi_copyin((void *)arg, ®, 20020 sizeof (mhioc_register_t), flag) != 0) { 20021 err = EFAULT; 20022 } else { 20023 err = 20024 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20025 un, SD_SCSI3_REGISTER, 20026 (uchar_t *)®); 20027 } 20028 } 20029 } 20030 break; 20031 20032 case MHIOCGRP_RESERVE: 20033 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 20034 if ((err = drv_priv(cred_p)) != EPERM) { 20035 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20036 err = ENOTSUP; 20037 } else if (arg != NULL) { 20038 mhioc_resv_desc_t resv_desc; 20039 if (ddi_copyin((void *)arg, &resv_desc, 20040 sizeof (mhioc_resv_desc_t), flag) != 0) { 20041 err = EFAULT; 20042 } else { 20043 err = 20044 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20045 un, SD_SCSI3_RESERVE, 20046 (uchar_t *)&resv_desc); 20047 } 20048 } 20049 } 20050 break; 20051 20052 case MHIOCGRP_PREEMPTANDABORT: 20053 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 20054 if ((err = drv_priv(cred_p)) != EPERM) { 20055 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20056 err = ENOTSUP; 20057 } else if (arg != NULL) { 20058 mhioc_preemptandabort_t preempt_abort; 20059 if (ddi_copyin((void *)arg, &preempt_abort, 20060 sizeof (mhioc_preemptandabort_t), 20061 flag) != 0) { 20062 err = EFAULT; 20063 } else { 20064 err = 20065 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20066 un, SD_SCSI3_PREEMPTANDABORT, 20067 (uchar_t *)&preempt_abort); 20068 } 20069 } 20070 } 20071 break; 20072 20073 case MHIOCGRP_REGISTERANDIGNOREKEY: 20074 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 20075 if ((err = drv_priv(cred_p)) != EPERM) { 20076 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20077 err = ENOTSUP; 20078 } else if (arg != NULL) { 20079 mhioc_registerandignorekey_t r_and_i; 20080 if (ddi_copyin((void *)arg, (void *)&r_and_i, 20081 sizeof (mhioc_registerandignorekey_t), 20082 flag) != 0) { 20083 err = EFAULT; 20084 } else { 20085 err = 20086 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20087 un, SD_SCSI3_REGISTERANDIGNOREKEY, 20088 (uchar_t *)&r_and_i); 20089 } 20090 } 20091 } 20092 break; 20093 20094 case USCSICMD: 20095 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 20096 cr = ddi_get_cred(); 20097 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 20098 err = EPERM; 20099 } else { 20100 enum uio_seg uioseg; 20101 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 20102 UIO_USERSPACE; 20103 if (un->un_f_format_in_progress == TRUE) { 20104 err = EAGAIN; 20105 break; 20106 } 20107 err = sd_send_scsi_cmd(dev, (struct uscsi_cmd *)arg, 20108 flag, uioseg, SD_PATH_STANDARD); 20109 } 20110 break; 20111 20112 case CDROMPAUSE: 20113 case CDROMRESUME: 20114 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 20115 if (!ISCD(un)) { 20116 err = ENOTTY; 20117 } else { 20118 err = sr_pause_resume(dev, cmd); 20119 } 20120 break; 20121 20122 case CDROMPLAYMSF: 20123 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 20124 if (!ISCD(un)) { 20125 err = ENOTTY; 20126 } else { 20127 err = sr_play_msf(dev, (caddr_t)arg, flag); 20128 } 20129 break; 20130 20131 case CDROMPLAYTRKIND: 20132 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 20133 #if defined(__i386) || defined(__amd64) 20134 /* 20135 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 20136 */ 20137 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20138 #else 20139 if (!ISCD(un)) { 20140 #endif 20141 err = ENOTTY; 20142 } else { 20143 err = sr_play_trkind(dev, (caddr_t)arg, flag); 20144 } 20145 break; 20146 20147 case CDROMREADTOCHDR: 20148 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 20149 if (!ISCD(un)) { 20150 err = ENOTTY; 20151 } else { 20152 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 20153 } 20154 break; 20155 20156 case CDROMREADTOCENTRY: 20157 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 20158 if (!ISCD(un)) { 20159 err = ENOTTY; 20160 } else { 20161 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 20162 } 20163 break; 20164 20165 case CDROMSTOP: 20166 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 20167 if (!ISCD(un)) { 20168 err = ENOTTY; 20169 } else { 20170 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 20171 SD_PATH_STANDARD); 20172 } 20173 break; 20174 20175 case CDROMSTART: 20176 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 20177 if (!ISCD(un)) { 20178 err = ENOTTY; 20179 } else { 20180 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 20181 SD_PATH_STANDARD); 20182 } 20183 break; 20184 20185 case CDROMCLOSETRAY: 20186 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 20187 if (!ISCD(un)) { 20188 err = ENOTTY; 20189 } else { 20190 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 20191 SD_PATH_STANDARD); 20192 } 20193 break; 20194 20195 case FDEJECT: /* for eject command */ 20196 case DKIOCEJECT: 20197 case CDROMEJECT: 20198 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 20199 if (!un->un_f_eject_media_supported) { 20200 err = ENOTTY; 20201 } else { 20202 err = sr_eject(dev); 20203 } 20204 break; 20205 20206 case CDROMVOLCTRL: 20207 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 20208 if (!ISCD(un)) { 20209 err = ENOTTY; 20210 } else { 20211 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 20212 } 20213 break; 20214 20215 case CDROMSUBCHNL: 20216 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 20217 if (!ISCD(un)) { 20218 err = ENOTTY; 20219 } else { 20220 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 20221 } 20222 break; 20223 20224 case CDROMREADMODE2: 20225 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 20226 if (!ISCD(un)) { 20227 err = ENOTTY; 20228 } else if (un->un_f_cfg_is_atapi == TRUE) { 20229 /* 20230 * If the drive supports READ CD, use that instead of 20231 * switching the LBA size via a MODE SELECT 20232 * Block Descriptor 20233 */ 20234 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 20235 } else { 20236 err = sr_read_mode2(dev, (caddr_t)arg, flag); 20237 } 20238 break; 20239 20240 case CDROMREADMODE1: 20241 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 20242 if (!ISCD(un)) { 20243 err = ENOTTY; 20244 } else { 20245 err = sr_read_mode1(dev, (caddr_t)arg, flag); 20246 } 20247 break; 20248 20249 case CDROMREADOFFSET: 20250 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 20251 if (!ISCD(un)) { 20252 err = ENOTTY; 20253 } else { 20254 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 20255 flag); 20256 } 20257 break; 20258 20259 case CDROMSBLKMODE: 20260 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 20261 /* 20262 * There is no means of changing block size in case of atapi 20263 * drives, thus return ENOTTY if drive type is atapi 20264 */ 20265 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20266 err = ENOTTY; 20267 } else if (un->un_f_mmc_cap == TRUE) { 20268 20269 /* 20270 * MMC Devices do not support changing the 20271 * logical block size 20272 * 20273 * Note: EINVAL is being returned instead of ENOTTY to 20274 * maintain consistancy with the original mmc 20275 * driver update. 20276 */ 20277 err = EINVAL; 20278 } else { 20279 mutex_enter(SD_MUTEX(un)); 20280 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 20281 (un->un_ncmds_in_transport > 0)) { 20282 mutex_exit(SD_MUTEX(un)); 20283 err = EINVAL; 20284 } else { 20285 mutex_exit(SD_MUTEX(un)); 20286 err = sr_change_blkmode(dev, cmd, arg, flag); 20287 } 20288 } 20289 break; 20290 20291 case CDROMGBLKMODE: 20292 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 20293 if (!ISCD(un)) { 20294 err = ENOTTY; 20295 } else if ((un->un_f_cfg_is_atapi != FALSE) && 20296 (un->un_f_blockcount_is_valid != FALSE)) { 20297 /* 20298 * Drive is an ATAPI drive so return target block 20299 * size for ATAPI drives since we cannot change the 20300 * blocksize on ATAPI drives. Used primarily to detect 20301 * if an ATAPI cdrom is present. 20302 */ 20303 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 20304 sizeof (int), flag) != 0) { 20305 err = EFAULT; 20306 } else { 20307 err = 0; 20308 } 20309 20310 } else { 20311 /* 20312 * Drive supports changing block sizes via a Mode 20313 * Select. 20314 */ 20315 err = sr_change_blkmode(dev, cmd, arg, flag); 20316 } 20317 break; 20318 20319 case CDROMGDRVSPEED: 20320 case CDROMSDRVSPEED: 20321 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 20322 if (!ISCD(un)) { 20323 err = ENOTTY; 20324 } else if (un->un_f_mmc_cap == TRUE) { 20325 /* 20326 * Note: In the future the driver implementation 20327 * for getting and 20328 * setting cd speed should entail: 20329 * 1) If non-mmc try the Toshiba mode page 20330 * (sr_change_speed) 20331 * 2) If mmc but no support for Real Time Streaming try 20332 * the SET CD SPEED (0xBB) command 20333 * (sr_atapi_change_speed) 20334 * 3) If mmc and support for Real Time Streaming 20335 * try the GET PERFORMANCE and SET STREAMING 20336 * commands (not yet implemented, 4380808) 20337 */ 20338 /* 20339 * As per recent MMC spec, CD-ROM speed is variable 20340 * and changes with LBA. Since there is no such 20341 * things as drive speed now, fail this ioctl. 20342 * 20343 * Note: EINVAL is returned for consistancy of original 20344 * implementation which included support for getting 20345 * the drive speed of mmc devices but not setting 20346 * the drive speed. Thus EINVAL would be returned 20347 * if a set request was made for an mmc device. 20348 * We no longer support get or set speed for 20349 * mmc but need to remain consistent with regard 20350 * to the error code returned. 20351 */ 20352 err = EINVAL; 20353 } else if (un->un_f_cfg_is_atapi == TRUE) { 20354 err = sr_atapi_change_speed(dev, cmd, arg, flag); 20355 } else { 20356 err = sr_change_speed(dev, cmd, arg, flag); 20357 } 20358 break; 20359 20360 case CDROMCDDA: 20361 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 20362 if (!ISCD(un)) { 20363 err = ENOTTY; 20364 } else { 20365 err = sr_read_cdda(dev, (void *)arg, flag); 20366 } 20367 break; 20368 20369 case CDROMCDXA: 20370 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 20371 if (!ISCD(un)) { 20372 err = ENOTTY; 20373 } else { 20374 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 20375 } 20376 break; 20377 20378 case CDROMSUBCODE: 20379 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 20380 if (!ISCD(un)) { 20381 err = ENOTTY; 20382 } else { 20383 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 20384 } 20385 break; 20386 20387 20388 #ifdef SDDEBUG 20389 /* RESET/ABORTS testing ioctls */ 20390 case DKIOCRESET: { 20391 int reset_level; 20392 20393 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 20394 err = EFAULT; 20395 } else { 20396 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 20397 "reset_level = 0x%lx\n", reset_level); 20398 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 20399 err = 0; 20400 } else { 20401 err = EIO; 20402 } 20403 } 20404 break; 20405 } 20406 20407 case DKIOCABORT: 20408 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 20409 if (scsi_abort(SD_ADDRESS(un), NULL)) { 20410 err = 0; 20411 } else { 20412 err = EIO; 20413 } 20414 break; 20415 #endif 20416 20417 #ifdef SD_FAULT_INJECTION 20418 /* SDIOC FaultInjection testing ioctls */ 20419 case SDIOCSTART: 20420 case SDIOCSTOP: 20421 case SDIOCINSERTPKT: 20422 case SDIOCINSERTXB: 20423 case SDIOCINSERTUN: 20424 case SDIOCINSERTARQ: 20425 case SDIOCPUSH: 20426 case SDIOCRETRIEVE: 20427 case SDIOCRUN: 20428 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 20429 "SDIOC detected cmd:0x%X:\n", cmd); 20430 /* call error generator */ 20431 sd_faultinjection_ioctl(cmd, arg, un); 20432 err = 0; 20433 break; 20434 20435 #endif /* SD_FAULT_INJECTION */ 20436 20437 case DKIOCFLUSHWRITECACHE: 20438 { 20439 struct dk_callback *dkc = (struct dk_callback *)arg; 20440 20441 mutex_enter(SD_MUTEX(un)); 20442 if (!un->un_f_sync_cache_supported || 20443 !un->un_f_write_cache_enabled) { 20444 err = un->un_f_sync_cache_supported ? 20445 0 : ENOTSUP; 20446 mutex_exit(SD_MUTEX(un)); 20447 if ((flag & FKIOCTL) && dkc != NULL && 20448 dkc->dkc_callback != NULL) { 20449 (*dkc->dkc_callback)(dkc->dkc_cookie, 20450 err); 20451 /* 20452 * Did callback and reported error. 20453 * Since we did a callback, ioctl 20454 * should return 0. 20455 */ 20456 err = 0; 20457 } 20458 break; 20459 } 20460 mutex_exit(SD_MUTEX(un)); 20461 20462 if ((flag & FKIOCTL) && dkc != NULL && 20463 dkc->dkc_callback != NULL) { 20464 /* async SYNC CACHE request */ 20465 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 20466 } else { 20467 /* synchronous SYNC CACHE request */ 20468 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 20469 } 20470 } 20471 break; 20472 20473 case DKIOCGETWCE: { 20474 20475 int wce; 20476 20477 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 20478 break; 20479 } 20480 20481 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 20482 err = EFAULT; 20483 } 20484 break; 20485 } 20486 20487 case DKIOCSETWCE: { 20488 20489 int wce, sync_supported; 20490 20491 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 20492 err = EFAULT; 20493 break; 20494 } 20495 20496 /* 20497 * Synchronize multiple threads trying to enable 20498 * or disable the cache via the un_f_wcc_cv 20499 * condition variable. 20500 */ 20501 mutex_enter(SD_MUTEX(un)); 20502 20503 /* 20504 * Don't allow the cache to be enabled if the 20505 * config file has it disabled. 20506 */ 20507 if (un->un_f_opt_disable_cache && wce) { 20508 mutex_exit(SD_MUTEX(un)); 20509 err = EINVAL; 20510 break; 20511 } 20512 20513 /* 20514 * Wait for write cache change in progress 20515 * bit to be clear before proceeding. 20516 */ 20517 while (un->un_f_wcc_inprog) 20518 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 20519 20520 un->un_f_wcc_inprog = 1; 20521 20522 if (un->un_f_write_cache_enabled && wce == 0) { 20523 /* 20524 * Disable the write cache. Don't clear 20525 * un_f_write_cache_enabled until after 20526 * the mode select and flush are complete. 20527 */ 20528 sync_supported = un->un_f_sync_cache_supported; 20529 20530 /* 20531 * If cache flush is suppressed, we assume that the 20532 * controller firmware will take care of managing the 20533 * write cache for us: no need to explicitly 20534 * disable it. 20535 */ 20536 if (!un->un_f_suppress_cache_flush) { 20537 mutex_exit(SD_MUTEX(un)); 20538 if ((err = sd_cache_control(un, 20539 SD_CACHE_NOCHANGE, 20540 SD_CACHE_DISABLE)) == 0 && 20541 sync_supported) { 20542 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 20543 NULL); 20544 } 20545 } else { 20546 mutex_exit(SD_MUTEX(un)); 20547 } 20548 20549 mutex_enter(SD_MUTEX(un)); 20550 if (err == 0) { 20551 un->un_f_write_cache_enabled = 0; 20552 } 20553 20554 } else if (!un->un_f_write_cache_enabled && wce != 0) { 20555 /* 20556 * Set un_f_write_cache_enabled first, so there is 20557 * no window where the cache is enabled, but the 20558 * bit says it isn't. 20559 */ 20560 un->un_f_write_cache_enabled = 1; 20561 20562 /* 20563 * If cache flush is suppressed, we assume that the 20564 * controller firmware will take care of managing the 20565 * write cache for us: no need to explicitly 20566 * enable it. 20567 */ 20568 if (!un->un_f_suppress_cache_flush) { 20569 mutex_exit(SD_MUTEX(un)); 20570 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 20571 SD_CACHE_ENABLE); 20572 } else { 20573 mutex_exit(SD_MUTEX(un)); 20574 } 20575 20576 mutex_enter(SD_MUTEX(un)); 20577 20578 if (err) { 20579 un->un_f_write_cache_enabled = 0; 20580 } 20581 } 20582 20583 un->un_f_wcc_inprog = 0; 20584 cv_broadcast(&un->un_wcc_cv); 20585 mutex_exit(SD_MUTEX(un)); 20586 break; 20587 } 20588 20589 default: 20590 err = ENOTTY; 20591 break; 20592 } 20593 mutex_enter(SD_MUTEX(un)); 20594 un->un_ncmds_in_driver--; 20595 ASSERT(un->un_ncmds_in_driver >= 0); 20596 mutex_exit(SD_MUTEX(un)); 20597 20598 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 20599 return (err); 20600 } 20601 20602 20603 /* 20604 * Function: sd_dkio_ctrl_info 20605 * 20606 * Description: This routine is the driver entry point for handling controller 20607 * information ioctl requests (DKIOCINFO). 20608 * 20609 * Arguments: dev - the device number 20610 * arg - pointer to user provided dk_cinfo structure 20611 * specifying the controller type and attributes. 20612 * flag - this argument is a pass through to ddi_copyxxx() 20613 * directly from the mode argument of ioctl(). 20614 * 20615 * Return Code: 0 20616 * EFAULT 20617 * ENXIO 20618 */ 20619 20620 static int 20621 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 20622 { 20623 struct sd_lun *un = NULL; 20624 struct dk_cinfo *info; 20625 dev_info_t *pdip; 20626 int lun, tgt; 20627 20628 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20629 return (ENXIO); 20630 } 20631 20632 info = (struct dk_cinfo *) 20633 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 20634 20635 switch (un->un_ctype) { 20636 case CTYPE_CDROM: 20637 info->dki_ctype = DKC_CDROM; 20638 break; 20639 default: 20640 info->dki_ctype = DKC_SCSI_CCS; 20641 break; 20642 } 20643 pdip = ddi_get_parent(SD_DEVINFO(un)); 20644 info->dki_cnum = ddi_get_instance(pdip); 20645 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 20646 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 20647 } else { 20648 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 20649 DK_DEVLEN - 1); 20650 } 20651 20652 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20653 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 20654 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20655 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 20656 20657 /* Unit Information */ 20658 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 20659 info->dki_slave = ((tgt << 3) | lun); 20660 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 20661 DK_DEVLEN - 1); 20662 info->dki_flags = DKI_FMTVOL; 20663 info->dki_partition = SDPART(dev); 20664 20665 /* Max Transfer size of this device in blocks */ 20666 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 20667 info->dki_addr = 0; 20668 info->dki_space = 0; 20669 info->dki_prio = 0; 20670 info->dki_vec = 0; 20671 20672 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 20673 kmem_free(info, sizeof (struct dk_cinfo)); 20674 return (EFAULT); 20675 } else { 20676 kmem_free(info, sizeof (struct dk_cinfo)); 20677 return (0); 20678 } 20679 } 20680 20681 20682 /* 20683 * Function: sd_get_media_info 20684 * 20685 * Description: This routine is the driver entry point for handling ioctl 20686 * requests for the media type or command set profile used by the 20687 * drive to operate on the media (DKIOCGMEDIAINFO). 20688 * 20689 * Arguments: dev - the device number 20690 * arg - pointer to user provided dk_minfo structure 20691 * specifying the media type, logical block size and 20692 * drive capacity. 20693 * flag - this argument is a pass through to ddi_copyxxx() 20694 * directly from the mode argument of ioctl(). 20695 * 20696 * Return Code: 0 20697 * EACCESS 20698 * EFAULT 20699 * ENXIO 20700 * EIO 20701 */ 20702 20703 static int 20704 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 20705 { 20706 struct sd_lun *un = NULL; 20707 struct uscsi_cmd com; 20708 struct scsi_inquiry *sinq; 20709 struct dk_minfo media_info; 20710 u_longlong_t media_capacity; 20711 uint64_t capacity; 20712 uint_t lbasize; 20713 uchar_t *out_data; 20714 uchar_t *rqbuf; 20715 int rval = 0; 20716 int rtn; 20717 20718 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 20719 (un->un_state == SD_STATE_OFFLINE)) { 20720 return (ENXIO); 20721 } 20722 20723 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 20724 20725 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 20726 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20727 20728 /* Issue a TUR to determine if the drive is ready with media present */ 20729 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 20730 if (rval == ENXIO) { 20731 goto done; 20732 } 20733 20734 /* Now get configuration data */ 20735 if (ISCD(un)) { 20736 media_info.dki_media_type = DK_CDROM; 20737 20738 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 20739 if (un->un_f_mmc_cap == TRUE) { 20740 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 20741 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 20742 SD_PATH_STANDARD); 20743 20744 if (rtn) { 20745 /* 20746 * Failed for other than an illegal request 20747 * or command not supported 20748 */ 20749 if ((com.uscsi_status == STATUS_CHECK) && 20750 (com.uscsi_rqstatus == STATUS_GOOD)) { 20751 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 20752 (rqbuf[12] != 0x20)) { 20753 rval = EIO; 20754 goto done; 20755 } 20756 } 20757 } else { 20758 /* 20759 * The GET CONFIGURATION command succeeded 20760 * so set the media type according to the 20761 * returned data 20762 */ 20763 media_info.dki_media_type = out_data[6]; 20764 media_info.dki_media_type <<= 8; 20765 media_info.dki_media_type |= out_data[7]; 20766 } 20767 } 20768 } else { 20769 /* 20770 * The profile list is not available, so we attempt to identify 20771 * the media type based on the inquiry data 20772 */ 20773 sinq = un->un_sd->sd_inq; 20774 if ((sinq->inq_dtype == DTYPE_DIRECT) || 20775 (sinq->inq_dtype == DTYPE_OPTICAL)) { 20776 /* This is a direct access device or optical disk */ 20777 media_info.dki_media_type = DK_FIXED_DISK; 20778 20779 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 20780 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 20781 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 20782 media_info.dki_media_type = DK_ZIP; 20783 } else if ( 20784 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 20785 media_info.dki_media_type = DK_JAZ; 20786 } 20787 } 20788 } else { 20789 /* 20790 * Not a CD, direct access or optical disk so return 20791 * unknown media 20792 */ 20793 media_info.dki_media_type = DK_UNKNOWN; 20794 } 20795 } 20796 20797 /* Now read the capacity so we can provide the lbasize and capacity */ 20798 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 20799 SD_PATH_DIRECT)) { 20800 case 0: 20801 break; 20802 case EACCES: 20803 rval = EACCES; 20804 goto done; 20805 default: 20806 rval = EIO; 20807 goto done; 20808 } 20809 20810 media_info.dki_lbsize = lbasize; 20811 media_capacity = capacity; 20812 20813 /* 20814 * sd_send_scsi_READ_CAPACITY() reports capacity in 20815 * un->un_sys_blocksize chunks. So we need to convert it into 20816 * cap.lbasize chunks. 20817 */ 20818 media_capacity *= un->un_sys_blocksize; 20819 media_capacity /= lbasize; 20820 media_info.dki_capacity = media_capacity; 20821 20822 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 20823 rval = EFAULT; 20824 /* Put goto. Anybody might add some code below in future */ 20825 goto done; 20826 } 20827 done: 20828 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 20829 kmem_free(rqbuf, SENSE_LENGTH); 20830 return (rval); 20831 } 20832 20833 20834 /* 20835 * Function: sd_check_media 20836 * 20837 * Description: This utility routine implements the functionality for the 20838 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 20839 * driver state changes from that specified by the user 20840 * (inserted or ejected). For example, if the user specifies 20841 * DKIO_EJECTED and the current media state is inserted this 20842 * routine will immediately return DKIO_INSERTED. However, if the 20843 * current media state is not inserted the user thread will be 20844 * blocked until the drive state changes. If DKIO_NONE is specified 20845 * the user thread will block until a drive state change occurs. 20846 * 20847 * Arguments: dev - the device number 20848 * state - user pointer to a dkio_state, updated with the current 20849 * drive state at return. 20850 * 20851 * Return Code: ENXIO 20852 * EIO 20853 * EAGAIN 20854 * EINTR 20855 */ 20856 20857 static int 20858 sd_check_media(dev_t dev, enum dkio_state state) 20859 { 20860 struct sd_lun *un = NULL; 20861 enum dkio_state prev_state; 20862 opaque_t token = NULL; 20863 int rval = 0; 20864 20865 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20866 return (ENXIO); 20867 } 20868 20869 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 20870 20871 mutex_enter(SD_MUTEX(un)); 20872 20873 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 20874 "state=%x, mediastate=%x\n", state, un->un_mediastate); 20875 20876 prev_state = un->un_mediastate; 20877 20878 /* is there anything to do? */ 20879 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 20880 /* 20881 * submit the request to the scsi_watch service; 20882 * scsi_media_watch_cb() does the real work 20883 */ 20884 mutex_exit(SD_MUTEX(un)); 20885 20886 /* 20887 * This change handles the case where a scsi watch request is 20888 * added to a device that is powered down. To accomplish this 20889 * we power up the device before adding the scsi watch request, 20890 * since the scsi watch sends a TUR directly to the device 20891 * which the device cannot handle if it is powered down. 20892 */ 20893 if (sd_pm_entry(un) != DDI_SUCCESS) { 20894 mutex_enter(SD_MUTEX(un)); 20895 goto done; 20896 } 20897 20898 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 20899 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 20900 (caddr_t)dev); 20901 20902 sd_pm_exit(un); 20903 20904 mutex_enter(SD_MUTEX(un)); 20905 if (token == NULL) { 20906 rval = EAGAIN; 20907 goto done; 20908 } 20909 20910 /* 20911 * This is a special case IOCTL that doesn't return 20912 * until the media state changes. Routine sdpower 20913 * knows about and handles this so don't count it 20914 * as an active cmd in the driver, which would 20915 * keep the device busy to the pm framework. 20916 * If the count isn't decremented the device can't 20917 * be powered down. 20918 */ 20919 un->un_ncmds_in_driver--; 20920 ASSERT(un->un_ncmds_in_driver >= 0); 20921 20922 /* 20923 * if a prior request had been made, this will be the same 20924 * token, as scsi_watch was designed that way. 20925 */ 20926 un->un_swr_token = token; 20927 un->un_specified_mediastate = state; 20928 20929 /* 20930 * now wait for media change 20931 * we will not be signalled unless mediastate == state but it is 20932 * still better to test for this condition, since there is a 20933 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 20934 */ 20935 SD_TRACE(SD_LOG_COMMON, un, 20936 "sd_check_media: waiting for media state change\n"); 20937 while (un->un_mediastate == state) { 20938 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 20939 SD_TRACE(SD_LOG_COMMON, un, 20940 "sd_check_media: waiting for media state " 20941 "was interrupted\n"); 20942 un->un_ncmds_in_driver++; 20943 rval = EINTR; 20944 goto done; 20945 } 20946 SD_TRACE(SD_LOG_COMMON, un, 20947 "sd_check_media: received signal, state=%x\n", 20948 un->un_mediastate); 20949 } 20950 /* 20951 * Inc the counter to indicate the device once again 20952 * has an active outstanding cmd. 20953 */ 20954 un->un_ncmds_in_driver++; 20955 } 20956 20957 /* invalidate geometry */ 20958 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 20959 sr_ejected(un); 20960 } 20961 20962 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 20963 uint64_t capacity; 20964 uint_t lbasize; 20965 20966 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 20967 mutex_exit(SD_MUTEX(un)); 20968 /* 20969 * Since the following routines use SD_PATH_DIRECT, we must 20970 * call PM directly before the upcoming disk accesses. This 20971 * may cause the disk to be power/spin up. 20972 */ 20973 20974 if (sd_pm_entry(un) == DDI_SUCCESS) { 20975 rval = sd_send_scsi_READ_CAPACITY(un, 20976 &capacity, 20977 &lbasize, SD_PATH_DIRECT); 20978 if (rval != 0) { 20979 sd_pm_exit(un); 20980 mutex_enter(SD_MUTEX(un)); 20981 goto done; 20982 } 20983 } else { 20984 rval = EIO; 20985 mutex_enter(SD_MUTEX(un)); 20986 goto done; 20987 } 20988 mutex_enter(SD_MUTEX(un)); 20989 20990 sd_update_block_info(un, lbasize, capacity); 20991 20992 /* 20993 * Check if the media in the device is writable or not 20994 */ 20995 if (ISCD(un)) 20996 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 20997 20998 mutex_exit(SD_MUTEX(un)); 20999 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 21000 if ((cmlb_validate(un->un_cmlbhandle, 0, 21001 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 21002 sd_set_pstats(un); 21003 SD_TRACE(SD_LOG_IO_PARTITION, un, 21004 "sd_check_media: un:0x%p pstats created and " 21005 "set\n", un); 21006 } 21007 21008 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21009 SD_PATH_DIRECT); 21010 sd_pm_exit(un); 21011 21012 mutex_enter(SD_MUTEX(un)); 21013 } 21014 done: 21015 un->un_f_watcht_stopped = FALSE; 21016 if (un->un_swr_token) { 21017 /* 21018 * Use of this local token and the mutex ensures that we avoid 21019 * some race conditions associated with terminating the 21020 * scsi watch. 21021 */ 21022 token = un->un_swr_token; 21023 un->un_swr_token = (opaque_t)NULL; 21024 mutex_exit(SD_MUTEX(un)); 21025 (void) scsi_watch_request_terminate(token, 21026 SCSI_WATCH_TERMINATE_WAIT); 21027 mutex_enter(SD_MUTEX(un)); 21028 } 21029 21030 /* 21031 * Update the capacity kstat value, if no media previously 21032 * (capacity kstat is 0) and a media has been inserted 21033 * (un_f_blockcount_is_valid == TRUE) 21034 */ 21035 if (un->un_errstats) { 21036 struct sd_errstats *stp = NULL; 21037 21038 stp = (struct sd_errstats *)un->un_errstats->ks_data; 21039 if ((stp->sd_capacity.value.ui64 == 0) && 21040 (un->un_f_blockcount_is_valid == TRUE)) { 21041 stp->sd_capacity.value.ui64 = 21042 (uint64_t)((uint64_t)un->un_blockcount * 21043 un->un_sys_blocksize); 21044 } 21045 } 21046 mutex_exit(SD_MUTEX(un)); 21047 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 21048 return (rval); 21049 } 21050 21051 21052 /* 21053 * Function: sd_delayed_cv_broadcast 21054 * 21055 * Description: Delayed cv_broadcast to allow for target to recover from media 21056 * insertion. 21057 * 21058 * Arguments: arg - driver soft state (unit) structure 21059 */ 21060 21061 static void 21062 sd_delayed_cv_broadcast(void *arg) 21063 { 21064 struct sd_lun *un = arg; 21065 21066 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 21067 21068 mutex_enter(SD_MUTEX(un)); 21069 un->un_dcvb_timeid = NULL; 21070 cv_broadcast(&un->un_state_cv); 21071 mutex_exit(SD_MUTEX(un)); 21072 } 21073 21074 21075 /* 21076 * Function: sd_media_watch_cb 21077 * 21078 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 21079 * routine processes the TUR sense data and updates the driver 21080 * state if a transition has occurred. The user thread 21081 * (sd_check_media) is then signalled. 21082 * 21083 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21084 * among multiple watches that share this callback function 21085 * resultp - scsi watch facility result packet containing scsi 21086 * packet, status byte and sense data 21087 * 21088 * Return Code: 0 for success, -1 for failure 21089 */ 21090 21091 static int 21092 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 21093 { 21094 struct sd_lun *un; 21095 struct scsi_status *statusp = resultp->statusp; 21096 uint8_t *sensep = (uint8_t *)resultp->sensep; 21097 enum dkio_state state = DKIO_NONE; 21098 dev_t dev = (dev_t)arg; 21099 uchar_t actual_sense_length; 21100 uint8_t skey, asc, ascq; 21101 21102 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21103 return (-1); 21104 } 21105 actual_sense_length = resultp->actual_sense_length; 21106 21107 mutex_enter(SD_MUTEX(un)); 21108 SD_TRACE(SD_LOG_COMMON, un, 21109 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 21110 *((char *)statusp), (void *)sensep, actual_sense_length); 21111 21112 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 21113 un->un_mediastate = DKIO_DEV_GONE; 21114 cv_broadcast(&un->un_state_cv); 21115 mutex_exit(SD_MUTEX(un)); 21116 21117 return (0); 21118 } 21119 21120 /* 21121 * If there was a check condition then sensep points to valid sense data 21122 * If status was not a check condition but a reservation or busy status 21123 * then the new state is DKIO_NONE 21124 */ 21125 if (sensep != NULL) { 21126 skey = scsi_sense_key(sensep); 21127 asc = scsi_sense_asc(sensep); 21128 ascq = scsi_sense_ascq(sensep); 21129 21130 SD_INFO(SD_LOG_COMMON, un, 21131 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 21132 skey, asc, ascq); 21133 /* This routine only uses up to 13 bytes of sense data. */ 21134 if (actual_sense_length >= 13) { 21135 if (skey == KEY_UNIT_ATTENTION) { 21136 if (asc == 0x28) { 21137 state = DKIO_INSERTED; 21138 } 21139 } else if (skey == KEY_NOT_READY) { 21140 /* 21141 * if 02/04/02 means that the host 21142 * should send start command. Explicitly 21143 * leave the media state as is 21144 * (inserted) as the media is inserted 21145 * and host has stopped device for PM 21146 * reasons. Upon next true read/write 21147 * to this media will bring the 21148 * device to the right state good for 21149 * media access. 21150 */ 21151 if (asc == 0x3a) { 21152 state = DKIO_EJECTED; 21153 } else { 21154 /* 21155 * If the drive is busy with an 21156 * operation or long write, keep the 21157 * media in an inserted state. 21158 */ 21159 21160 if ((asc == 0x04) && 21161 ((ascq == 0x02) || 21162 (ascq == 0x07) || 21163 (ascq == 0x08))) { 21164 state = DKIO_INSERTED; 21165 } 21166 } 21167 } else if (skey == KEY_NO_SENSE) { 21168 if ((asc == 0x00) && (ascq == 0x00)) { 21169 /* 21170 * Sense Data 00/00/00 does not provide 21171 * any information about the state of 21172 * the media. Ignore it. 21173 */ 21174 mutex_exit(SD_MUTEX(un)); 21175 return (0); 21176 } 21177 } 21178 } 21179 } else if ((*((char *)statusp) == STATUS_GOOD) && 21180 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 21181 state = DKIO_INSERTED; 21182 } 21183 21184 SD_TRACE(SD_LOG_COMMON, un, 21185 "sd_media_watch_cb: state=%x, specified=%x\n", 21186 state, un->un_specified_mediastate); 21187 21188 /* 21189 * now signal the waiting thread if this is *not* the specified state; 21190 * delay the signal if the state is DKIO_INSERTED to allow the target 21191 * to recover 21192 */ 21193 if (state != un->un_specified_mediastate) { 21194 un->un_mediastate = state; 21195 if (state == DKIO_INSERTED) { 21196 /* 21197 * delay the signal to give the drive a chance 21198 * to do what it apparently needs to do 21199 */ 21200 SD_TRACE(SD_LOG_COMMON, un, 21201 "sd_media_watch_cb: delayed cv_broadcast\n"); 21202 if (un->un_dcvb_timeid == NULL) { 21203 un->un_dcvb_timeid = 21204 timeout(sd_delayed_cv_broadcast, un, 21205 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 21206 } 21207 } else { 21208 SD_TRACE(SD_LOG_COMMON, un, 21209 "sd_media_watch_cb: immediate cv_broadcast\n"); 21210 cv_broadcast(&un->un_state_cv); 21211 } 21212 } 21213 mutex_exit(SD_MUTEX(un)); 21214 return (0); 21215 } 21216 21217 21218 /* 21219 * Function: sd_dkio_get_temp 21220 * 21221 * Description: This routine is the driver entry point for handling ioctl 21222 * requests to get the disk temperature. 21223 * 21224 * Arguments: dev - the device number 21225 * arg - pointer to user provided dk_temperature structure. 21226 * flag - this argument is a pass through to ddi_copyxxx() 21227 * directly from the mode argument of ioctl(). 21228 * 21229 * Return Code: 0 21230 * EFAULT 21231 * ENXIO 21232 * EAGAIN 21233 */ 21234 21235 static int 21236 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 21237 { 21238 struct sd_lun *un = NULL; 21239 struct dk_temperature *dktemp = NULL; 21240 uchar_t *temperature_page; 21241 int rval = 0; 21242 int path_flag = SD_PATH_STANDARD; 21243 21244 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21245 return (ENXIO); 21246 } 21247 21248 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 21249 21250 /* copyin the disk temp argument to get the user flags */ 21251 if (ddi_copyin((void *)arg, dktemp, 21252 sizeof (struct dk_temperature), flag) != 0) { 21253 rval = EFAULT; 21254 goto done; 21255 } 21256 21257 /* Initialize the temperature to invalid. */ 21258 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 21259 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 21260 21261 /* 21262 * Note: Investigate removing the "bypass pm" semantic. 21263 * Can we just bypass PM always? 21264 */ 21265 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 21266 path_flag = SD_PATH_DIRECT; 21267 ASSERT(!mutex_owned(&un->un_pm_mutex)); 21268 mutex_enter(&un->un_pm_mutex); 21269 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 21270 /* 21271 * If DKT_BYPASS_PM is set, and the drive happens to be 21272 * in low power mode, we can not wake it up, Need to 21273 * return EAGAIN. 21274 */ 21275 mutex_exit(&un->un_pm_mutex); 21276 rval = EAGAIN; 21277 goto done; 21278 } else { 21279 /* 21280 * Indicate to PM the device is busy. This is required 21281 * to avoid a race - i.e. the ioctl is issuing a 21282 * command and the pm framework brings down the device 21283 * to low power mode (possible power cut-off on some 21284 * platforms). 21285 */ 21286 mutex_exit(&un->un_pm_mutex); 21287 if (sd_pm_entry(un) != DDI_SUCCESS) { 21288 rval = EAGAIN; 21289 goto done; 21290 } 21291 } 21292 } 21293 21294 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 21295 21296 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 21297 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 21298 goto done2; 21299 } 21300 21301 /* 21302 * For the current temperature verify that the parameter length is 0x02 21303 * and the parameter code is 0x00 21304 */ 21305 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 21306 (temperature_page[5] == 0x00)) { 21307 if (temperature_page[9] == 0xFF) { 21308 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 21309 } else { 21310 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 21311 } 21312 } 21313 21314 /* 21315 * For the reference temperature verify that the parameter 21316 * length is 0x02 and the parameter code is 0x01 21317 */ 21318 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 21319 (temperature_page[11] == 0x01)) { 21320 if (temperature_page[15] == 0xFF) { 21321 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 21322 } else { 21323 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 21324 } 21325 } 21326 21327 /* Do the copyout regardless of the temperature commands status. */ 21328 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 21329 flag) != 0) { 21330 rval = EFAULT; 21331 } 21332 21333 done2: 21334 if (path_flag == SD_PATH_DIRECT) { 21335 sd_pm_exit(un); 21336 } 21337 21338 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 21339 done: 21340 if (dktemp != NULL) { 21341 kmem_free(dktemp, sizeof (struct dk_temperature)); 21342 } 21343 21344 return (rval); 21345 } 21346 21347 21348 /* 21349 * Function: sd_log_page_supported 21350 * 21351 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 21352 * supported log pages. 21353 * 21354 * Arguments: un - 21355 * log_page - 21356 * 21357 * Return Code: -1 - on error (log sense is optional and may not be supported). 21358 * 0 - log page not found. 21359 * 1 - log page found. 21360 */ 21361 21362 static int 21363 sd_log_page_supported(struct sd_lun *un, int log_page) 21364 { 21365 uchar_t *log_page_data; 21366 int i; 21367 int match = 0; 21368 int log_size; 21369 21370 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 21371 21372 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 21373 SD_PATH_DIRECT) != 0) { 21374 SD_ERROR(SD_LOG_COMMON, un, 21375 "sd_log_page_supported: failed log page retrieval\n"); 21376 kmem_free(log_page_data, 0xFF); 21377 return (-1); 21378 } 21379 log_size = log_page_data[3]; 21380 21381 /* 21382 * The list of supported log pages start from the fourth byte. Check 21383 * until we run out of log pages or a match is found. 21384 */ 21385 for (i = 4; (i < (log_size + 4)) && !match; i++) { 21386 if (log_page_data[i] == log_page) { 21387 match++; 21388 } 21389 } 21390 kmem_free(log_page_data, 0xFF); 21391 return (match); 21392 } 21393 21394 21395 /* 21396 * Function: sd_mhdioc_failfast 21397 * 21398 * Description: This routine is the driver entry point for handling ioctl 21399 * requests to enable/disable the multihost failfast option. 21400 * (MHIOCENFAILFAST) 21401 * 21402 * Arguments: dev - the device number 21403 * arg - user specified probing interval. 21404 * flag - this argument is a pass through to ddi_copyxxx() 21405 * directly from the mode argument of ioctl(). 21406 * 21407 * Return Code: 0 21408 * EFAULT 21409 * ENXIO 21410 */ 21411 21412 static int 21413 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 21414 { 21415 struct sd_lun *un = NULL; 21416 int mh_time; 21417 int rval = 0; 21418 21419 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21420 return (ENXIO); 21421 } 21422 21423 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 21424 return (EFAULT); 21425 21426 if (mh_time) { 21427 mutex_enter(SD_MUTEX(un)); 21428 un->un_resvd_status |= SD_FAILFAST; 21429 mutex_exit(SD_MUTEX(un)); 21430 /* 21431 * If mh_time is INT_MAX, then this ioctl is being used for 21432 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 21433 */ 21434 if (mh_time != INT_MAX) { 21435 rval = sd_check_mhd(dev, mh_time); 21436 } 21437 } else { 21438 (void) sd_check_mhd(dev, 0); 21439 mutex_enter(SD_MUTEX(un)); 21440 un->un_resvd_status &= ~SD_FAILFAST; 21441 mutex_exit(SD_MUTEX(un)); 21442 } 21443 return (rval); 21444 } 21445 21446 21447 /* 21448 * Function: sd_mhdioc_takeown 21449 * 21450 * Description: This routine is the driver entry point for handling ioctl 21451 * requests to forcefully acquire exclusive access rights to the 21452 * multihost disk (MHIOCTKOWN). 21453 * 21454 * Arguments: dev - the device number 21455 * arg - user provided structure specifying the delay 21456 * parameters in milliseconds 21457 * flag - this argument is a pass through to ddi_copyxxx() 21458 * directly from the mode argument of ioctl(). 21459 * 21460 * Return Code: 0 21461 * EFAULT 21462 * ENXIO 21463 */ 21464 21465 static int 21466 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 21467 { 21468 struct sd_lun *un = NULL; 21469 struct mhioctkown *tkown = NULL; 21470 int rval = 0; 21471 21472 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21473 return (ENXIO); 21474 } 21475 21476 if (arg != NULL) { 21477 tkown = (struct mhioctkown *) 21478 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 21479 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 21480 if (rval != 0) { 21481 rval = EFAULT; 21482 goto error; 21483 } 21484 } 21485 21486 rval = sd_take_ownership(dev, tkown); 21487 mutex_enter(SD_MUTEX(un)); 21488 if (rval == 0) { 21489 un->un_resvd_status |= SD_RESERVE; 21490 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 21491 sd_reinstate_resv_delay = 21492 tkown->reinstate_resv_delay * 1000; 21493 } else { 21494 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 21495 } 21496 /* 21497 * Give the scsi_watch routine interval set by 21498 * the MHIOCENFAILFAST ioctl precedence here. 21499 */ 21500 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 21501 mutex_exit(SD_MUTEX(un)); 21502 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 21503 SD_TRACE(SD_LOG_IOCTL_MHD, un, 21504 "sd_mhdioc_takeown : %d\n", 21505 sd_reinstate_resv_delay); 21506 } else { 21507 mutex_exit(SD_MUTEX(un)); 21508 } 21509 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 21510 sd_mhd_reset_notify_cb, (caddr_t)un); 21511 } else { 21512 un->un_resvd_status &= ~SD_RESERVE; 21513 mutex_exit(SD_MUTEX(un)); 21514 } 21515 21516 error: 21517 if (tkown != NULL) { 21518 kmem_free(tkown, sizeof (struct mhioctkown)); 21519 } 21520 return (rval); 21521 } 21522 21523 21524 /* 21525 * Function: sd_mhdioc_release 21526 * 21527 * Description: This routine is the driver entry point for handling ioctl 21528 * requests to release exclusive access rights to the multihost 21529 * disk (MHIOCRELEASE). 21530 * 21531 * Arguments: dev - the device number 21532 * 21533 * Return Code: 0 21534 * ENXIO 21535 */ 21536 21537 static int 21538 sd_mhdioc_release(dev_t dev) 21539 { 21540 struct sd_lun *un = NULL; 21541 timeout_id_t resvd_timeid_save; 21542 int resvd_status_save; 21543 int rval = 0; 21544 21545 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21546 return (ENXIO); 21547 } 21548 21549 mutex_enter(SD_MUTEX(un)); 21550 resvd_status_save = un->un_resvd_status; 21551 un->un_resvd_status &= 21552 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 21553 if (un->un_resvd_timeid) { 21554 resvd_timeid_save = un->un_resvd_timeid; 21555 un->un_resvd_timeid = NULL; 21556 mutex_exit(SD_MUTEX(un)); 21557 (void) untimeout(resvd_timeid_save); 21558 } else { 21559 mutex_exit(SD_MUTEX(un)); 21560 } 21561 21562 /* 21563 * destroy any pending timeout thread that may be attempting to 21564 * reinstate reservation on this device. 21565 */ 21566 sd_rmv_resv_reclaim_req(dev); 21567 21568 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 21569 mutex_enter(SD_MUTEX(un)); 21570 if ((un->un_mhd_token) && 21571 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 21572 mutex_exit(SD_MUTEX(un)); 21573 (void) sd_check_mhd(dev, 0); 21574 } else { 21575 mutex_exit(SD_MUTEX(un)); 21576 } 21577 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 21578 sd_mhd_reset_notify_cb, (caddr_t)un); 21579 } else { 21580 /* 21581 * sd_mhd_watch_cb will restart the resvd recover timeout thread 21582 */ 21583 mutex_enter(SD_MUTEX(un)); 21584 un->un_resvd_status = resvd_status_save; 21585 mutex_exit(SD_MUTEX(un)); 21586 } 21587 return (rval); 21588 } 21589 21590 21591 /* 21592 * Function: sd_mhdioc_register_devid 21593 * 21594 * Description: This routine is the driver entry point for handling ioctl 21595 * requests to register the device id (MHIOCREREGISTERDEVID). 21596 * 21597 * Note: The implementation for this ioctl has been updated to 21598 * be consistent with the original PSARC case (1999/357) 21599 * (4375899, 4241671, 4220005) 21600 * 21601 * Arguments: dev - the device number 21602 * 21603 * Return Code: 0 21604 * ENXIO 21605 */ 21606 21607 static int 21608 sd_mhdioc_register_devid(dev_t dev) 21609 { 21610 struct sd_lun *un = NULL; 21611 int rval = 0; 21612 21613 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21614 return (ENXIO); 21615 } 21616 21617 ASSERT(!mutex_owned(SD_MUTEX(un))); 21618 21619 mutex_enter(SD_MUTEX(un)); 21620 21621 /* If a devid already exists, de-register it */ 21622 if (un->un_devid != NULL) { 21623 ddi_devid_unregister(SD_DEVINFO(un)); 21624 /* 21625 * After unregister devid, needs to free devid memory 21626 */ 21627 ddi_devid_free(un->un_devid); 21628 un->un_devid = NULL; 21629 } 21630 21631 /* Check for reservation conflict */ 21632 mutex_exit(SD_MUTEX(un)); 21633 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 21634 mutex_enter(SD_MUTEX(un)); 21635 21636 switch (rval) { 21637 case 0: 21638 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 21639 break; 21640 case EACCES: 21641 break; 21642 default: 21643 rval = EIO; 21644 } 21645 21646 mutex_exit(SD_MUTEX(un)); 21647 return (rval); 21648 } 21649 21650 21651 /* 21652 * Function: sd_mhdioc_inkeys 21653 * 21654 * Description: This routine is the driver entry point for handling ioctl 21655 * requests to issue the SCSI-3 Persistent In Read Keys command 21656 * to the device (MHIOCGRP_INKEYS). 21657 * 21658 * Arguments: dev - the device number 21659 * arg - user provided in_keys structure 21660 * flag - this argument is a pass through to ddi_copyxxx() 21661 * directly from the mode argument of ioctl(). 21662 * 21663 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 21664 * ENXIO 21665 * EFAULT 21666 */ 21667 21668 static int 21669 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 21670 { 21671 struct sd_lun *un; 21672 mhioc_inkeys_t inkeys; 21673 int rval = 0; 21674 21675 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21676 return (ENXIO); 21677 } 21678 21679 #ifdef _MULTI_DATAMODEL 21680 switch (ddi_model_convert_from(flag & FMODELS)) { 21681 case DDI_MODEL_ILP32: { 21682 struct mhioc_inkeys32 inkeys32; 21683 21684 if (ddi_copyin(arg, &inkeys32, 21685 sizeof (struct mhioc_inkeys32), flag) != 0) { 21686 return (EFAULT); 21687 } 21688 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 21689 if ((rval = sd_persistent_reservation_in_read_keys(un, 21690 &inkeys, flag)) != 0) { 21691 return (rval); 21692 } 21693 inkeys32.generation = inkeys.generation; 21694 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 21695 flag) != 0) { 21696 return (EFAULT); 21697 } 21698 break; 21699 } 21700 case DDI_MODEL_NONE: 21701 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 21702 flag) != 0) { 21703 return (EFAULT); 21704 } 21705 if ((rval = sd_persistent_reservation_in_read_keys(un, 21706 &inkeys, flag)) != 0) { 21707 return (rval); 21708 } 21709 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 21710 flag) != 0) { 21711 return (EFAULT); 21712 } 21713 break; 21714 } 21715 21716 #else /* ! _MULTI_DATAMODEL */ 21717 21718 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 21719 return (EFAULT); 21720 } 21721 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 21722 if (rval != 0) { 21723 return (rval); 21724 } 21725 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 21726 return (EFAULT); 21727 } 21728 21729 #endif /* _MULTI_DATAMODEL */ 21730 21731 return (rval); 21732 } 21733 21734 21735 /* 21736 * Function: sd_mhdioc_inresv 21737 * 21738 * Description: This routine is the driver entry point for handling ioctl 21739 * requests to issue the SCSI-3 Persistent In Read Reservations 21740 * command to the device (MHIOCGRP_INKEYS). 21741 * 21742 * Arguments: dev - the device number 21743 * arg - user provided in_resv structure 21744 * flag - this argument is a pass through to ddi_copyxxx() 21745 * directly from the mode argument of ioctl(). 21746 * 21747 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 21748 * ENXIO 21749 * EFAULT 21750 */ 21751 21752 static int 21753 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 21754 { 21755 struct sd_lun *un; 21756 mhioc_inresvs_t inresvs; 21757 int rval = 0; 21758 21759 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21760 return (ENXIO); 21761 } 21762 21763 #ifdef _MULTI_DATAMODEL 21764 21765 switch (ddi_model_convert_from(flag & FMODELS)) { 21766 case DDI_MODEL_ILP32: { 21767 struct mhioc_inresvs32 inresvs32; 21768 21769 if (ddi_copyin(arg, &inresvs32, 21770 sizeof (struct mhioc_inresvs32), flag) != 0) { 21771 return (EFAULT); 21772 } 21773 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 21774 if ((rval = sd_persistent_reservation_in_read_resv(un, 21775 &inresvs, flag)) != 0) { 21776 return (rval); 21777 } 21778 inresvs32.generation = inresvs.generation; 21779 if (ddi_copyout(&inresvs32, arg, 21780 sizeof (struct mhioc_inresvs32), flag) != 0) { 21781 return (EFAULT); 21782 } 21783 break; 21784 } 21785 case DDI_MODEL_NONE: 21786 if (ddi_copyin(arg, &inresvs, 21787 sizeof (mhioc_inresvs_t), flag) != 0) { 21788 return (EFAULT); 21789 } 21790 if ((rval = sd_persistent_reservation_in_read_resv(un, 21791 &inresvs, flag)) != 0) { 21792 return (rval); 21793 } 21794 if (ddi_copyout(&inresvs, arg, 21795 sizeof (mhioc_inresvs_t), flag) != 0) { 21796 return (EFAULT); 21797 } 21798 break; 21799 } 21800 21801 #else /* ! _MULTI_DATAMODEL */ 21802 21803 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 21804 return (EFAULT); 21805 } 21806 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 21807 if (rval != 0) { 21808 return (rval); 21809 } 21810 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 21811 return (EFAULT); 21812 } 21813 21814 #endif /* ! _MULTI_DATAMODEL */ 21815 21816 return (rval); 21817 } 21818 21819 21820 /* 21821 * The following routines support the clustering functionality described below 21822 * and implement lost reservation reclaim functionality. 21823 * 21824 * Clustering 21825 * ---------- 21826 * The clustering code uses two different, independent forms of SCSI 21827 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 21828 * Persistent Group Reservations. For any particular disk, it will use either 21829 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 21830 * 21831 * SCSI-2 21832 * The cluster software takes ownership of a multi-hosted disk by issuing the 21833 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 21834 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 21835 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 21836 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 21837 * driver. The meaning of failfast is that if the driver (on this host) ever 21838 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 21839 * it should immediately panic the host. The motivation for this ioctl is that 21840 * if this host does encounter reservation conflict, the underlying cause is 21841 * that some other host of the cluster has decided that this host is no longer 21842 * in the cluster and has seized control of the disks for itself. Since this 21843 * host is no longer in the cluster, it ought to panic itself. The 21844 * MHIOCENFAILFAST ioctl does two things: 21845 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 21846 * error to panic the host 21847 * (b) it sets up a periodic timer to test whether this host still has 21848 * "access" (in that no other host has reserved the device): if the 21849 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 21850 * purpose of that periodic timer is to handle scenarios where the host is 21851 * otherwise temporarily quiescent, temporarily doing no real i/o. 21852 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 21853 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 21854 * the device itself. 21855 * 21856 * SCSI-3 PGR 21857 * A direct semantic implementation of the SCSI-3 Persistent Reservation 21858 * facility is supported through the shared multihost disk ioctls 21859 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 21860 * MHIOCGRP_PREEMPTANDABORT) 21861 * 21862 * Reservation Reclaim: 21863 * -------------------- 21864 * To support the lost reservation reclaim operations this driver creates a 21865 * single thread to handle reinstating reservations on all devices that have 21866 * lost reservations sd_resv_reclaim_requests are logged for all devices that 21867 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 21868 * and the reservation reclaim thread loops through the requests to regain the 21869 * lost reservations. 21870 */ 21871 21872 /* 21873 * Function: sd_check_mhd() 21874 * 21875 * Description: This function sets up and submits a scsi watch request or 21876 * terminates an existing watch request. This routine is used in 21877 * support of reservation reclaim. 21878 * 21879 * Arguments: dev - the device 'dev_t' is used for context to discriminate 21880 * among multiple watches that share the callback function 21881 * interval - the number of microseconds specifying the watch 21882 * interval for issuing TEST UNIT READY commands. If 21883 * set to 0 the watch should be terminated. If the 21884 * interval is set to 0 and if the device is required 21885 * to hold reservation while disabling failfast, the 21886 * watch is restarted with an interval of 21887 * reinstate_resv_delay. 21888 * 21889 * Return Code: 0 - Successful submit/terminate of scsi watch request 21890 * ENXIO - Indicates an invalid device was specified 21891 * EAGAIN - Unable to submit the scsi watch request 21892 */ 21893 21894 static int 21895 sd_check_mhd(dev_t dev, int interval) 21896 { 21897 struct sd_lun *un; 21898 opaque_t token; 21899 21900 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21901 return (ENXIO); 21902 } 21903 21904 /* is this a watch termination request? */ 21905 if (interval == 0) { 21906 mutex_enter(SD_MUTEX(un)); 21907 /* if there is an existing watch task then terminate it */ 21908 if (un->un_mhd_token) { 21909 token = un->un_mhd_token; 21910 un->un_mhd_token = NULL; 21911 mutex_exit(SD_MUTEX(un)); 21912 (void) scsi_watch_request_terminate(token, 21913 SCSI_WATCH_TERMINATE_WAIT); 21914 mutex_enter(SD_MUTEX(un)); 21915 } else { 21916 mutex_exit(SD_MUTEX(un)); 21917 /* 21918 * Note: If we return here we don't check for the 21919 * failfast case. This is the original legacy 21920 * implementation but perhaps we should be checking 21921 * the failfast case. 21922 */ 21923 return (0); 21924 } 21925 /* 21926 * If the device is required to hold reservation while 21927 * disabling failfast, we need to restart the scsi_watch 21928 * routine with an interval of reinstate_resv_delay. 21929 */ 21930 if (un->un_resvd_status & SD_RESERVE) { 21931 interval = sd_reinstate_resv_delay/1000; 21932 } else { 21933 /* no failfast so bail */ 21934 mutex_exit(SD_MUTEX(un)); 21935 return (0); 21936 } 21937 mutex_exit(SD_MUTEX(un)); 21938 } 21939 21940 /* 21941 * adjust minimum time interval to 1 second, 21942 * and convert from msecs to usecs 21943 */ 21944 if (interval > 0 && interval < 1000) { 21945 interval = 1000; 21946 } 21947 interval *= 1000; 21948 21949 /* 21950 * submit the request to the scsi_watch service 21951 */ 21952 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 21953 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 21954 if (token == NULL) { 21955 return (EAGAIN); 21956 } 21957 21958 /* 21959 * save token for termination later on 21960 */ 21961 mutex_enter(SD_MUTEX(un)); 21962 un->un_mhd_token = token; 21963 mutex_exit(SD_MUTEX(un)); 21964 return (0); 21965 } 21966 21967 21968 /* 21969 * Function: sd_mhd_watch_cb() 21970 * 21971 * Description: This function is the call back function used by the scsi watch 21972 * facility. The scsi watch facility sends the "Test Unit Ready" 21973 * and processes the status. If applicable (i.e. a "Unit Attention" 21974 * status and automatic "Request Sense" not used) the scsi watch 21975 * facility will send a "Request Sense" and retrieve the sense data 21976 * to be passed to this callback function. In either case the 21977 * automatic "Request Sense" or the facility submitting one, this 21978 * callback is passed the status and sense data. 21979 * 21980 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21981 * among multiple watches that share this callback function 21982 * resultp - scsi watch facility result packet containing scsi 21983 * packet, status byte and sense data 21984 * 21985 * Return Code: 0 - continue the watch task 21986 * non-zero - terminate the watch task 21987 */ 21988 21989 static int 21990 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 21991 { 21992 struct sd_lun *un; 21993 struct scsi_status *statusp; 21994 uint8_t *sensep; 21995 struct scsi_pkt *pkt; 21996 uchar_t actual_sense_length; 21997 dev_t dev = (dev_t)arg; 21998 21999 ASSERT(resultp != NULL); 22000 statusp = resultp->statusp; 22001 sensep = (uint8_t *)resultp->sensep; 22002 pkt = resultp->pkt; 22003 actual_sense_length = resultp->actual_sense_length; 22004 22005 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22006 return (ENXIO); 22007 } 22008 22009 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22010 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 22011 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 22012 22013 /* Begin processing of the status and/or sense data */ 22014 if (pkt->pkt_reason != CMD_CMPLT) { 22015 /* Handle the incomplete packet */ 22016 sd_mhd_watch_incomplete(un, pkt); 22017 return (0); 22018 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 22019 if (*((unsigned char *)statusp) 22020 == STATUS_RESERVATION_CONFLICT) { 22021 /* 22022 * Handle a reservation conflict by panicking if 22023 * configured for failfast or by logging the conflict 22024 * and updating the reservation status 22025 */ 22026 mutex_enter(SD_MUTEX(un)); 22027 if ((un->un_resvd_status & SD_FAILFAST) && 22028 (sd_failfast_enable)) { 22029 sd_panic_for_res_conflict(un); 22030 /*NOTREACHED*/ 22031 } 22032 SD_INFO(SD_LOG_IOCTL_MHD, un, 22033 "sd_mhd_watch_cb: Reservation Conflict\n"); 22034 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 22035 mutex_exit(SD_MUTEX(un)); 22036 } 22037 } 22038 22039 if (sensep != NULL) { 22040 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 22041 mutex_enter(SD_MUTEX(un)); 22042 if ((scsi_sense_asc(sensep) == 22043 SD_SCSI_RESET_SENSE_CODE) && 22044 (un->un_resvd_status & SD_RESERVE)) { 22045 /* 22046 * The additional sense code indicates a power 22047 * on or bus device reset has occurred; update 22048 * the reservation status. 22049 */ 22050 un->un_resvd_status |= 22051 (SD_LOST_RESERVE | SD_WANT_RESERVE); 22052 SD_INFO(SD_LOG_IOCTL_MHD, un, 22053 "sd_mhd_watch_cb: Lost Reservation\n"); 22054 } 22055 } else { 22056 return (0); 22057 } 22058 } else { 22059 mutex_enter(SD_MUTEX(un)); 22060 } 22061 22062 if ((un->un_resvd_status & SD_RESERVE) && 22063 (un->un_resvd_status & SD_LOST_RESERVE)) { 22064 if (un->un_resvd_status & SD_WANT_RESERVE) { 22065 /* 22066 * A reset occurred in between the last probe and this 22067 * one so if a timeout is pending cancel it. 22068 */ 22069 if (un->un_resvd_timeid) { 22070 timeout_id_t temp_id = un->un_resvd_timeid; 22071 un->un_resvd_timeid = NULL; 22072 mutex_exit(SD_MUTEX(un)); 22073 (void) untimeout(temp_id); 22074 mutex_enter(SD_MUTEX(un)); 22075 } 22076 un->un_resvd_status &= ~SD_WANT_RESERVE; 22077 } 22078 if (un->un_resvd_timeid == 0) { 22079 /* Schedule a timeout to handle the lost reservation */ 22080 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 22081 (void *)dev, 22082 drv_usectohz(sd_reinstate_resv_delay)); 22083 } 22084 } 22085 mutex_exit(SD_MUTEX(un)); 22086 return (0); 22087 } 22088 22089 22090 /* 22091 * Function: sd_mhd_watch_incomplete() 22092 * 22093 * Description: This function is used to find out why a scsi pkt sent by the 22094 * scsi watch facility was not completed. Under some scenarios this 22095 * routine will return. Otherwise it will send a bus reset to see 22096 * if the drive is still online. 22097 * 22098 * Arguments: un - driver soft state (unit) structure 22099 * pkt - incomplete scsi pkt 22100 */ 22101 22102 static void 22103 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 22104 { 22105 int be_chatty; 22106 int perr; 22107 22108 ASSERT(pkt != NULL); 22109 ASSERT(un != NULL); 22110 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 22111 perr = (pkt->pkt_statistics & STAT_PERR); 22112 22113 mutex_enter(SD_MUTEX(un)); 22114 if (un->un_state == SD_STATE_DUMPING) { 22115 mutex_exit(SD_MUTEX(un)); 22116 return; 22117 } 22118 22119 switch (pkt->pkt_reason) { 22120 case CMD_UNX_BUS_FREE: 22121 /* 22122 * If we had a parity error that caused the target to drop BSY*, 22123 * don't be chatty about it. 22124 */ 22125 if (perr && be_chatty) { 22126 be_chatty = 0; 22127 } 22128 break; 22129 case CMD_TAG_REJECT: 22130 /* 22131 * The SCSI-2 spec states that a tag reject will be sent by the 22132 * target if tagged queuing is not supported. A tag reject may 22133 * also be sent during certain initialization periods or to 22134 * control internal resources. For the latter case the target 22135 * may also return Queue Full. 22136 * 22137 * If this driver receives a tag reject from a target that is 22138 * going through an init period or controlling internal 22139 * resources tagged queuing will be disabled. This is a less 22140 * than optimal behavior but the driver is unable to determine 22141 * the target state and assumes tagged queueing is not supported 22142 */ 22143 pkt->pkt_flags = 0; 22144 un->un_tagflags = 0; 22145 22146 if (un->un_f_opt_queueing == TRUE) { 22147 un->un_throttle = min(un->un_throttle, 3); 22148 } else { 22149 un->un_throttle = 1; 22150 } 22151 mutex_exit(SD_MUTEX(un)); 22152 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 22153 mutex_enter(SD_MUTEX(un)); 22154 break; 22155 case CMD_INCOMPLETE: 22156 /* 22157 * The transport stopped with an abnormal state, fallthrough and 22158 * reset the target and/or bus unless selection did not complete 22159 * (indicated by STATE_GOT_BUS) in which case we don't want to 22160 * go through a target/bus reset 22161 */ 22162 if (pkt->pkt_state == STATE_GOT_BUS) { 22163 break; 22164 } 22165 /*FALLTHROUGH*/ 22166 22167 case CMD_TIMEOUT: 22168 default: 22169 /* 22170 * The lun may still be running the command, so a lun reset 22171 * should be attempted. If the lun reset fails or cannot be 22172 * issued, than try a target reset. Lastly try a bus reset. 22173 */ 22174 if ((pkt->pkt_statistics & 22175 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 22176 int reset_retval = 0; 22177 mutex_exit(SD_MUTEX(un)); 22178 if (un->un_f_allow_bus_device_reset == TRUE) { 22179 if (un->un_f_lun_reset_enabled == TRUE) { 22180 reset_retval = 22181 scsi_reset(SD_ADDRESS(un), 22182 RESET_LUN); 22183 } 22184 if (reset_retval == 0) { 22185 reset_retval = 22186 scsi_reset(SD_ADDRESS(un), 22187 RESET_TARGET); 22188 } 22189 } 22190 if (reset_retval == 0) { 22191 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 22192 } 22193 mutex_enter(SD_MUTEX(un)); 22194 } 22195 break; 22196 } 22197 22198 /* A device/bus reset has occurred; update the reservation status. */ 22199 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 22200 (STAT_BUS_RESET | STAT_DEV_RESET))) { 22201 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22202 un->un_resvd_status |= 22203 (SD_LOST_RESERVE | SD_WANT_RESERVE); 22204 SD_INFO(SD_LOG_IOCTL_MHD, un, 22205 "sd_mhd_watch_incomplete: Lost Reservation\n"); 22206 } 22207 } 22208 22209 /* 22210 * The disk has been turned off; Update the device state. 22211 * 22212 * Note: Should we be offlining the disk here? 22213 */ 22214 if (pkt->pkt_state == STATE_GOT_BUS) { 22215 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 22216 "Disk not responding to selection\n"); 22217 if (un->un_state != SD_STATE_OFFLINE) { 22218 New_state(un, SD_STATE_OFFLINE); 22219 } 22220 } else if (be_chatty) { 22221 /* 22222 * suppress messages if they are all the same pkt reason; 22223 * with TQ, many (up to 256) are returned with the same 22224 * pkt_reason 22225 */ 22226 if (pkt->pkt_reason != un->un_last_pkt_reason) { 22227 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22228 "sd_mhd_watch_incomplete: " 22229 "SCSI transport failed: reason '%s'\n", 22230 scsi_rname(pkt->pkt_reason)); 22231 } 22232 } 22233 un->un_last_pkt_reason = pkt->pkt_reason; 22234 mutex_exit(SD_MUTEX(un)); 22235 } 22236 22237 22238 /* 22239 * Function: sd_sname() 22240 * 22241 * Description: This is a simple little routine to return a string containing 22242 * a printable description of command status byte for use in 22243 * logging. 22244 * 22245 * Arguments: status - pointer to a status byte 22246 * 22247 * Return Code: char * - string containing status description. 22248 */ 22249 22250 static char * 22251 sd_sname(uchar_t status) 22252 { 22253 switch (status & STATUS_MASK) { 22254 case STATUS_GOOD: 22255 return ("good status"); 22256 case STATUS_CHECK: 22257 return ("check condition"); 22258 case STATUS_MET: 22259 return ("condition met"); 22260 case STATUS_BUSY: 22261 return ("busy"); 22262 case STATUS_INTERMEDIATE: 22263 return ("intermediate"); 22264 case STATUS_INTERMEDIATE_MET: 22265 return ("intermediate - condition met"); 22266 case STATUS_RESERVATION_CONFLICT: 22267 return ("reservation_conflict"); 22268 case STATUS_TERMINATED: 22269 return ("command terminated"); 22270 case STATUS_QFULL: 22271 return ("queue full"); 22272 default: 22273 return ("<unknown status>"); 22274 } 22275 } 22276 22277 22278 /* 22279 * Function: sd_mhd_resvd_recover() 22280 * 22281 * Description: This function adds a reservation entry to the 22282 * sd_resv_reclaim_request list and signals the reservation 22283 * reclaim thread that there is work pending. If the reservation 22284 * reclaim thread has not been previously created this function 22285 * will kick it off. 22286 * 22287 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22288 * among multiple watches that share this callback function 22289 * 22290 * Context: This routine is called by timeout() and is run in interrupt 22291 * context. It must not sleep or call other functions which may 22292 * sleep. 22293 */ 22294 22295 static void 22296 sd_mhd_resvd_recover(void *arg) 22297 { 22298 dev_t dev = (dev_t)arg; 22299 struct sd_lun *un; 22300 struct sd_thr_request *sd_treq = NULL; 22301 struct sd_thr_request *sd_cur = NULL; 22302 struct sd_thr_request *sd_prev = NULL; 22303 int already_there = 0; 22304 22305 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22306 return; 22307 } 22308 22309 mutex_enter(SD_MUTEX(un)); 22310 un->un_resvd_timeid = NULL; 22311 if (un->un_resvd_status & SD_WANT_RESERVE) { 22312 /* 22313 * There was a reset so don't issue the reserve, allow the 22314 * sd_mhd_watch_cb callback function to notice this and 22315 * reschedule the timeout for reservation. 22316 */ 22317 mutex_exit(SD_MUTEX(un)); 22318 return; 22319 } 22320 mutex_exit(SD_MUTEX(un)); 22321 22322 /* 22323 * Add this device to the sd_resv_reclaim_request list and the 22324 * sd_resv_reclaim_thread should take care of the rest. 22325 * 22326 * Note: We can't sleep in this context so if the memory allocation 22327 * fails allow the sd_mhd_watch_cb callback function to notice this and 22328 * reschedule the timeout for reservation. (4378460) 22329 */ 22330 sd_treq = (struct sd_thr_request *) 22331 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 22332 if (sd_treq == NULL) { 22333 return; 22334 } 22335 22336 sd_treq->sd_thr_req_next = NULL; 22337 sd_treq->dev = dev; 22338 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22339 if (sd_tr.srq_thr_req_head == NULL) { 22340 sd_tr.srq_thr_req_head = sd_treq; 22341 } else { 22342 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 22343 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 22344 if (sd_cur->dev == dev) { 22345 /* 22346 * already in Queue so don't log 22347 * another request for the device 22348 */ 22349 already_there = 1; 22350 break; 22351 } 22352 sd_prev = sd_cur; 22353 } 22354 if (!already_there) { 22355 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 22356 "logging request for %lx\n", dev); 22357 sd_prev->sd_thr_req_next = sd_treq; 22358 } else { 22359 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 22360 } 22361 } 22362 22363 /* 22364 * Create a kernel thread to do the reservation reclaim and free up this 22365 * thread. We cannot block this thread while we go away to do the 22366 * reservation reclaim 22367 */ 22368 if (sd_tr.srq_resv_reclaim_thread == NULL) 22369 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 22370 sd_resv_reclaim_thread, NULL, 22371 0, &p0, TS_RUN, v.v_maxsyspri - 2); 22372 22373 /* Tell the reservation reclaim thread that it has work to do */ 22374 cv_signal(&sd_tr.srq_resv_reclaim_cv); 22375 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22376 } 22377 22378 /* 22379 * Function: sd_resv_reclaim_thread() 22380 * 22381 * Description: This function implements the reservation reclaim operations 22382 * 22383 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22384 * among multiple watches that share this callback function 22385 */ 22386 22387 static void 22388 sd_resv_reclaim_thread() 22389 { 22390 struct sd_lun *un; 22391 struct sd_thr_request *sd_mhreq; 22392 22393 /* Wait for work */ 22394 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22395 if (sd_tr.srq_thr_req_head == NULL) { 22396 cv_wait(&sd_tr.srq_resv_reclaim_cv, 22397 &sd_tr.srq_resv_reclaim_mutex); 22398 } 22399 22400 /* Loop while we have work */ 22401 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 22402 un = ddi_get_soft_state(sd_state, 22403 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 22404 if (un == NULL) { 22405 /* 22406 * softstate structure is NULL so just 22407 * dequeue the request and continue 22408 */ 22409 sd_tr.srq_thr_req_head = 22410 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22411 kmem_free(sd_tr.srq_thr_cur_req, 22412 sizeof (struct sd_thr_request)); 22413 continue; 22414 } 22415 22416 /* dequeue the request */ 22417 sd_mhreq = sd_tr.srq_thr_cur_req; 22418 sd_tr.srq_thr_req_head = 22419 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22420 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22421 22422 /* 22423 * Reclaim reservation only if SD_RESERVE is still set. There 22424 * may have been a call to MHIOCRELEASE before we got here. 22425 */ 22426 mutex_enter(SD_MUTEX(un)); 22427 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22428 /* 22429 * Note: The SD_LOST_RESERVE flag is cleared before 22430 * reclaiming the reservation. If this is done after the 22431 * call to sd_reserve_release a reservation loss in the 22432 * window between pkt completion of reserve cmd and 22433 * mutex_enter below may not be recognized 22434 */ 22435 un->un_resvd_status &= ~SD_LOST_RESERVE; 22436 mutex_exit(SD_MUTEX(un)); 22437 22438 if (sd_reserve_release(sd_mhreq->dev, 22439 SD_RESERVE) == 0) { 22440 mutex_enter(SD_MUTEX(un)); 22441 un->un_resvd_status |= SD_RESERVE; 22442 mutex_exit(SD_MUTEX(un)); 22443 SD_INFO(SD_LOG_IOCTL_MHD, un, 22444 "sd_resv_reclaim_thread: " 22445 "Reservation Recovered\n"); 22446 } else { 22447 mutex_enter(SD_MUTEX(un)); 22448 un->un_resvd_status |= SD_LOST_RESERVE; 22449 mutex_exit(SD_MUTEX(un)); 22450 SD_INFO(SD_LOG_IOCTL_MHD, un, 22451 "sd_resv_reclaim_thread: Failed " 22452 "Reservation Recovery\n"); 22453 } 22454 } else { 22455 mutex_exit(SD_MUTEX(un)); 22456 } 22457 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22458 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 22459 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22460 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 22461 /* 22462 * wakeup the destroy thread if anyone is waiting on 22463 * us to complete. 22464 */ 22465 cv_signal(&sd_tr.srq_inprocess_cv); 22466 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22467 "sd_resv_reclaim_thread: cv_signalling current request \n"); 22468 } 22469 22470 /* 22471 * cleanup the sd_tr structure now that this thread will not exist 22472 */ 22473 ASSERT(sd_tr.srq_thr_req_head == NULL); 22474 ASSERT(sd_tr.srq_thr_cur_req == NULL); 22475 sd_tr.srq_resv_reclaim_thread = NULL; 22476 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22477 thread_exit(); 22478 } 22479 22480 22481 /* 22482 * Function: sd_rmv_resv_reclaim_req() 22483 * 22484 * Description: This function removes any pending reservation reclaim requests 22485 * for the specified device. 22486 * 22487 * Arguments: dev - the device 'dev_t' 22488 */ 22489 22490 static void 22491 sd_rmv_resv_reclaim_req(dev_t dev) 22492 { 22493 struct sd_thr_request *sd_mhreq; 22494 struct sd_thr_request *sd_prev; 22495 22496 /* Remove a reservation reclaim request from the list */ 22497 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22498 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 22499 /* 22500 * We are attempting to reinstate reservation for 22501 * this device. We wait for sd_reserve_release() 22502 * to return before we return. 22503 */ 22504 cv_wait(&sd_tr.srq_inprocess_cv, 22505 &sd_tr.srq_resv_reclaim_mutex); 22506 } else { 22507 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 22508 if (sd_mhreq && sd_mhreq->dev == dev) { 22509 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 22510 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22511 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22512 return; 22513 } 22514 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 22515 if (sd_mhreq && sd_mhreq->dev == dev) { 22516 break; 22517 } 22518 sd_prev = sd_mhreq; 22519 } 22520 if (sd_mhreq != NULL) { 22521 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 22522 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22523 } 22524 } 22525 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22526 } 22527 22528 22529 /* 22530 * Function: sd_mhd_reset_notify_cb() 22531 * 22532 * Description: This is a call back function for scsi_reset_notify. This 22533 * function updates the softstate reserved status and logs the 22534 * reset. The driver scsi watch facility callback function 22535 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 22536 * will reclaim the reservation. 22537 * 22538 * Arguments: arg - driver soft state (unit) structure 22539 */ 22540 22541 static void 22542 sd_mhd_reset_notify_cb(caddr_t arg) 22543 { 22544 struct sd_lun *un = (struct sd_lun *)arg; 22545 22546 mutex_enter(SD_MUTEX(un)); 22547 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22548 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 22549 SD_INFO(SD_LOG_IOCTL_MHD, un, 22550 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 22551 } 22552 mutex_exit(SD_MUTEX(un)); 22553 } 22554 22555 22556 /* 22557 * Function: sd_take_ownership() 22558 * 22559 * Description: This routine implements an algorithm to achieve a stable 22560 * reservation on disks which don't implement priority reserve, 22561 * and makes sure that other host lose re-reservation attempts. 22562 * This algorithm contains of a loop that keeps issuing the RESERVE 22563 * for some period of time (min_ownership_delay, default 6 seconds) 22564 * During that loop, it looks to see if there has been a bus device 22565 * reset or bus reset (both of which cause an existing reservation 22566 * to be lost). If the reservation is lost issue RESERVE until a 22567 * period of min_ownership_delay with no resets has gone by, or 22568 * until max_ownership_delay has expired. This loop ensures that 22569 * the host really did manage to reserve the device, in spite of 22570 * resets. The looping for min_ownership_delay (default six 22571 * seconds) is important to early generation clustering products, 22572 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 22573 * MHIOCENFAILFAST periodic timer of two seconds. By having 22574 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 22575 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 22576 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 22577 * have already noticed, via the MHIOCENFAILFAST polling, that it 22578 * no longer "owns" the disk and will have panicked itself. Thus, 22579 * the host issuing the MHIOCTKOWN is assured (with timing 22580 * dependencies) that by the time it actually starts to use the 22581 * disk for real work, the old owner is no longer accessing it. 22582 * 22583 * min_ownership_delay is the minimum amount of time for which the 22584 * disk must be reserved continuously devoid of resets before the 22585 * MHIOCTKOWN ioctl will return success. 22586 * 22587 * max_ownership_delay indicates the amount of time by which the 22588 * take ownership should succeed or timeout with an error. 22589 * 22590 * Arguments: dev - the device 'dev_t' 22591 * *p - struct containing timing info. 22592 * 22593 * Return Code: 0 for success or error code 22594 */ 22595 22596 static int 22597 sd_take_ownership(dev_t dev, struct mhioctkown *p) 22598 { 22599 struct sd_lun *un; 22600 int rval; 22601 int err; 22602 int reservation_count = 0; 22603 int min_ownership_delay = 6000000; /* in usec */ 22604 int max_ownership_delay = 30000000; /* in usec */ 22605 clock_t start_time; /* starting time of this algorithm */ 22606 clock_t end_time; /* time limit for giving up */ 22607 clock_t ownership_time; /* time limit for stable ownership */ 22608 clock_t current_time; 22609 clock_t previous_current_time; 22610 22611 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22612 return (ENXIO); 22613 } 22614 22615 /* 22616 * Attempt a device reservation. A priority reservation is requested. 22617 */ 22618 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 22619 != SD_SUCCESS) { 22620 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22621 "sd_take_ownership: return(1)=%d\n", rval); 22622 return (rval); 22623 } 22624 22625 /* Update the softstate reserved status to indicate the reservation */ 22626 mutex_enter(SD_MUTEX(un)); 22627 un->un_resvd_status |= SD_RESERVE; 22628 un->un_resvd_status &= 22629 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 22630 mutex_exit(SD_MUTEX(un)); 22631 22632 if (p != NULL) { 22633 if (p->min_ownership_delay != 0) { 22634 min_ownership_delay = p->min_ownership_delay * 1000; 22635 } 22636 if (p->max_ownership_delay != 0) { 22637 max_ownership_delay = p->max_ownership_delay * 1000; 22638 } 22639 } 22640 SD_INFO(SD_LOG_IOCTL_MHD, un, 22641 "sd_take_ownership: min, max delays: %d, %d\n", 22642 min_ownership_delay, max_ownership_delay); 22643 22644 start_time = ddi_get_lbolt(); 22645 current_time = start_time; 22646 ownership_time = current_time + drv_usectohz(min_ownership_delay); 22647 end_time = start_time + drv_usectohz(max_ownership_delay); 22648 22649 while (current_time - end_time < 0) { 22650 delay(drv_usectohz(500000)); 22651 22652 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 22653 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 22654 mutex_enter(SD_MUTEX(un)); 22655 rval = (un->un_resvd_status & 22656 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 22657 mutex_exit(SD_MUTEX(un)); 22658 break; 22659 } 22660 } 22661 previous_current_time = current_time; 22662 current_time = ddi_get_lbolt(); 22663 mutex_enter(SD_MUTEX(un)); 22664 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 22665 ownership_time = ddi_get_lbolt() + 22666 drv_usectohz(min_ownership_delay); 22667 reservation_count = 0; 22668 } else { 22669 reservation_count++; 22670 } 22671 un->un_resvd_status |= SD_RESERVE; 22672 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 22673 mutex_exit(SD_MUTEX(un)); 22674 22675 SD_INFO(SD_LOG_IOCTL_MHD, un, 22676 "sd_take_ownership: ticks for loop iteration=%ld, " 22677 "reservation=%s\n", (current_time - previous_current_time), 22678 reservation_count ? "ok" : "reclaimed"); 22679 22680 if (current_time - ownership_time >= 0 && 22681 reservation_count >= 4) { 22682 rval = 0; /* Achieved a stable ownership */ 22683 break; 22684 } 22685 if (current_time - end_time >= 0) { 22686 rval = EACCES; /* No ownership in max possible time */ 22687 break; 22688 } 22689 } 22690 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22691 "sd_take_ownership: return(2)=%d\n", rval); 22692 return (rval); 22693 } 22694 22695 22696 /* 22697 * Function: sd_reserve_release() 22698 * 22699 * Description: This function builds and sends scsi RESERVE, RELEASE, and 22700 * PRIORITY RESERVE commands based on a user specified command type 22701 * 22702 * Arguments: dev - the device 'dev_t' 22703 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 22704 * SD_RESERVE, SD_RELEASE 22705 * 22706 * Return Code: 0 or Error Code 22707 */ 22708 22709 static int 22710 sd_reserve_release(dev_t dev, int cmd) 22711 { 22712 struct uscsi_cmd *com = NULL; 22713 struct sd_lun *un = NULL; 22714 char cdb[CDB_GROUP0]; 22715 int rval; 22716 22717 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 22718 (cmd == SD_PRIORITY_RESERVE)); 22719 22720 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22721 return (ENXIO); 22722 } 22723 22724 /* instantiate and initialize the command and cdb */ 22725 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 22726 bzero(cdb, CDB_GROUP0); 22727 com->uscsi_flags = USCSI_SILENT; 22728 com->uscsi_timeout = un->un_reserve_release_time; 22729 com->uscsi_cdblen = CDB_GROUP0; 22730 com->uscsi_cdb = cdb; 22731 if (cmd == SD_RELEASE) { 22732 cdb[0] = SCMD_RELEASE; 22733 } else { 22734 cdb[0] = SCMD_RESERVE; 22735 } 22736 22737 /* Send the command. */ 22738 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22739 SD_PATH_STANDARD); 22740 22741 /* 22742 * "break" a reservation that is held by another host, by issuing a 22743 * reset if priority reserve is desired, and we could not get the 22744 * device. 22745 */ 22746 if ((cmd == SD_PRIORITY_RESERVE) && 22747 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22748 /* 22749 * First try to reset the LUN. If we cannot, then try a target 22750 * reset, followed by a bus reset if the target reset fails. 22751 */ 22752 int reset_retval = 0; 22753 if (un->un_f_lun_reset_enabled == TRUE) { 22754 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 22755 } 22756 if (reset_retval == 0) { 22757 /* The LUN reset either failed or was not issued */ 22758 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 22759 } 22760 if ((reset_retval == 0) && 22761 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 22762 rval = EIO; 22763 kmem_free(com, sizeof (*com)); 22764 return (rval); 22765 } 22766 22767 bzero(com, sizeof (struct uscsi_cmd)); 22768 com->uscsi_flags = USCSI_SILENT; 22769 com->uscsi_cdb = cdb; 22770 com->uscsi_cdblen = CDB_GROUP0; 22771 com->uscsi_timeout = 5; 22772 22773 /* 22774 * Reissue the last reserve command, this time without request 22775 * sense. Assume that it is just a regular reserve command. 22776 */ 22777 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22778 SD_PATH_STANDARD); 22779 } 22780 22781 /* Return an error if still getting a reservation conflict. */ 22782 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22783 rval = EACCES; 22784 } 22785 22786 kmem_free(com, sizeof (*com)); 22787 return (rval); 22788 } 22789 22790 22791 #define SD_NDUMP_RETRIES 12 22792 /* 22793 * System Crash Dump routine 22794 */ 22795 22796 static int 22797 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 22798 { 22799 int instance; 22800 int partition; 22801 int i; 22802 int err; 22803 struct sd_lun *un; 22804 struct scsi_pkt *wr_pktp; 22805 struct buf *wr_bp; 22806 struct buf wr_buf; 22807 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 22808 daddr_t tgt_blkno; /* rmw - blkno for target */ 22809 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 22810 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 22811 size_t io_start_offset; 22812 int doing_rmw = FALSE; 22813 int rval; 22814 ssize_t dma_resid; 22815 daddr_t oblkno; 22816 diskaddr_t nblks = 0; 22817 diskaddr_t start_block; 22818 22819 instance = SDUNIT(dev); 22820 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 22821 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 22822 return (ENXIO); 22823 } 22824 22825 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 22826 22827 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 22828 22829 partition = SDPART(dev); 22830 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 22831 22832 /* Validate blocks to dump at against partition size. */ 22833 22834 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 22835 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 22836 22837 if ((blkno + nblk) > nblks) { 22838 SD_TRACE(SD_LOG_DUMP, un, 22839 "sddump: dump range larger than partition: " 22840 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 22841 blkno, nblk, nblks); 22842 return (EINVAL); 22843 } 22844 22845 mutex_enter(&un->un_pm_mutex); 22846 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 22847 struct scsi_pkt *start_pktp; 22848 22849 mutex_exit(&un->un_pm_mutex); 22850 22851 /* 22852 * use pm framework to power on HBA 1st 22853 */ 22854 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 22855 22856 /* 22857 * Dump no long uses sdpower to power on a device, it's 22858 * in-line here so it can be done in polled mode. 22859 */ 22860 22861 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 22862 22863 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 22864 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 22865 22866 if (start_pktp == NULL) { 22867 /* We were not given a SCSI packet, fail. */ 22868 return (EIO); 22869 } 22870 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 22871 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 22872 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 22873 start_pktp->pkt_flags = FLAG_NOINTR; 22874 22875 mutex_enter(SD_MUTEX(un)); 22876 SD_FILL_SCSI1_LUN(un, start_pktp); 22877 mutex_exit(SD_MUTEX(un)); 22878 /* 22879 * Scsi_poll returns 0 (success) if the command completes and 22880 * the status block is STATUS_GOOD. 22881 */ 22882 if (sd_scsi_poll(un, start_pktp) != 0) { 22883 scsi_destroy_pkt(start_pktp); 22884 return (EIO); 22885 } 22886 scsi_destroy_pkt(start_pktp); 22887 (void) sd_ddi_pm_resume(un); 22888 } else { 22889 mutex_exit(&un->un_pm_mutex); 22890 } 22891 22892 mutex_enter(SD_MUTEX(un)); 22893 un->un_throttle = 0; 22894 22895 /* 22896 * The first time through, reset the specific target device. 22897 * However, when cpr calls sddump we know that sd is in a 22898 * a good state so no bus reset is required. 22899 * Clear sense data via Request Sense cmd. 22900 * In sddump we don't care about allow_bus_device_reset anymore 22901 */ 22902 22903 if ((un->un_state != SD_STATE_SUSPENDED) && 22904 (un->un_state != SD_STATE_DUMPING)) { 22905 22906 New_state(un, SD_STATE_DUMPING); 22907 22908 if (un->un_f_is_fibre == FALSE) { 22909 mutex_exit(SD_MUTEX(un)); 22910 /* 22911 * Attempt a bus reset for parallel scsi. 22912 * 22913 * Note: A bus reset is required because on some host 22914 * systems (i.e. E420R) a bus device reset is 22915 * insufficient to reset the state of the target. 22916 * 22917 * Note: Don't issue the reset for fibre-channel, 22918 * because this tends to hang the bus (loop) for 22919 * too long while everyone is logging out and in 22920 * and the deadman timer for dumping will fire 22921 * before the dump is complete. 22922 */ 22923 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 22924 mutex_enter(SD_MUTEX(un)); 22925 Restore_state(un); 22926 mutex_exit(SD_MUTEX(un)); 22927 return (EIO); 22928 } 22929 22930 /* Delay to give the device some recovery time. */ 22931 drv_usecwait(10000); 22932 22933 if (sd_send_polled_RQS(un) == SD_FAILURE) { 22934 SD_INFO(SD_LOG_DUMP, un, 22935 "sddump: sd_send_polled_RQS failed\n"); 22936 } 22937 mutex_enter(SD_MUTEX(un)); 22938 } 22939 } 22940 22941 /* 22942 * Convert the partition-relative block number to a 22943 * disk physical block number. 22944 */ 22945 blkno += start_block; 22946 22947 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 22948 22949 22950 /* 22951 * Check if the device has a non-512 block size. 22952 */ 22953 wr_bp = NULL; 22954 if (NOT_DEVBSIZE(un)) { 22955 tgt_byte_offset = blkno * un->un_sys_blocksize; 22956 tgt_byte_count = nblk * un->un_sys_blocksize; 22957 if ((tgt_byte_offset % un->un_tgt_blocksize) || 22958 (tgt_byte_count % un->un_tgt_blocksize)) { 22959 doing_rmw = TRUE; 22960 /* 22961 * Calculate the block number and number of block 22962 * in terms of the media block size. 22963 */ 22964 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 22965 tgt_nblk = 22966 ((tgt_byte_offset + tgt_byte_count + 22967 (un->un_tgt_blocksize - 1)) / 22968 un->un_tgt_blocksize) - tgt_blkno; 22969 22970 /* 22971 * Invoke the routine which is going to do read part 22972 * of read-modify-write. 22973 * Note that this routine returns a pointer to 22974 * a valid bp in wr_bp. 22975 */ 22976 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 22977 &wr_bp); 22978 if (err) { 22979 mutex_exit(SD_MUTEX(un)); 22980 return (err); 22981 } 22982 /* 22983 * Offset is being calculated as - 22984 * (original block # * system block size) - 22985 * (new block # * target block size) 22986 */ 22987 io_start_offset = 22988 ((uint64_t)(blkno * un->un_sys_blocksize)) - 22989 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 22990 22991 ASSERT((io_start_offset >= 0) && 22992 (io_start_offset < un->un_tgt_blocksize)); 22993 /* 22994 * Do the modify portion of read modify write. 22995 */ 22996 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 22997 (size_t)nblk * un->un_sys_blocksize); 22998 } else { 22999 doing_rmw = FALSE; 23000 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 23001 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 23002 } 23003 23004 /* Convert blkno and nblk to target blocks */ 23005 blkno = tgt_blkno; 23006 nblk = tgt_nblk; 23007 } else { 23008 wr_bp = &wr_buf; 23009 bzero(wr_bp, sizeof (struct buf)); 23010 wr_bp->b_flags = B_BUSY; 23011 wr_bp->b_un.b_addr = addr; 23012 wr_bp->b_bcount = nblk << DEV_BSHIFT; 23013 wr_bp->b_resid = 0; 23014 } 23015 23016 mutex_exit(SD_MUTEX(un)); 23017 23018 /* 23019 * Obtain a SCSI packet for the write command. 23020 * It should be safe to call the allocator here without 23021 * worrying about being locked for DVMA mapping because 23022 * the address we're passed is already a DVMA mapping 23023 * 23024 * We are also not going to worry about semaphore ownership 23025 * in the dump buffer. Dumping is single threaded at present. 23026 */ 23027 23028 wr_pktp = NULL; 23029 23030 dma_resid = wr_bp->b_bcount; 23031 oblkno = blkno; 23032 23033 while (dma_resid != 0) { 23034 23035 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 23036 wr_bp->b_flags &= ~B_ERROR; 23037 23038 if (un->un_partial_dma_supported == 1) { 23039 blkno = oblkno + 23040 ((wr_bp->b_bcount - dma_resid) / 23041 un->un_tgt_blocksize); 23042 nblk = dma_resid / un->un_tgt_blocksize; 23043 23044 if (wr_pktp) { 23045 /* 23046 * Partial DMA transfers after initial transfer 23047 */ 23048 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 23049 blkno, nblk); 23050 } else { 23051 /* Initial transfer */ 23052 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 23053 un->un_pkt_flags, NULL_FUNC, NULL, 23054 blkno, nblk); 23055 } 23056 } else { 23057 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 23058 0, NULL_FUNC, NULL, blkno, nblk); 23059 } 23060 23061 if (rval == 0) { 23062 /* We were given a SCSI packet, continue. */ 23063 break; 23064 } 23065 23066 if (i == 0) { 23067 if (wr_bp->b_flags & B_ERROR) { 23068 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23069 "no resources for dumping; " 23070 "error code: 0x%x, retrying", 23071 geterror(wr_bp)); 23072 } else { 23073 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23074 "no resources for dumping; retrying"); 23075 } 23076 } else if (i != (SD_NDUMP_RETRIES - 1)) { 23077 if (wr_bp->b_flags & B_ERROR) { 23078 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23079 "no resources for dumping; error code: " 23080 "0x%x, retrying\n", geterror(wr_bp)); 23081 } 23082 } else { 23083 if (wr_bp->b_flags & B_ERROR) { 23084 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23085 "no resources for dumping; " 23086 "error code: 0x%x, retries failed, " 23087 "giving up.\n", geterror(wr_bp)); 23088 } else { 23089 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23090 "no resources for dumping; " 23091 "retries failed, giving up.\n"); 23092 } 23093 mutex_enter(SD_MUTEX(un)); 23094 Restore_state(un); 23095 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 23096 mutex_exit(SD_MUTEX(un)); 23097 scsi_free_consistent_buf(wr_bp); 23098 } else { 23099 mutex_exit(SD_MUTEX(un)); 23100 } 23101 return (EIO); 23102 } 23103 drv_usecwait(10000); 23104 } 23105 23106 if (un->un_partial_dma_supported == 1) { 23107 /* 23108 * save the resid from PARTIAL_DMA 23109 */ 23110 dma_resid = wr_pktp->pkt_resid; 23111 if (dma_resid != 0) 23112 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 23113 wr_pktp->pkt_resid = 0; 23114 } else { 23115 dma_resid = 0; 23116 } 23117 23118 /* SunBug 1222170 */ 23119 wr_pktp->pkt_flags = FLAG_NOINTR; 23120 23121 err = EIO; 23122 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 23123 23124 /* 23125 * Scsi_poll returns 0 (success) if the command completes and 23126 * the status block is STATUS_GOOD. We should only check 23127 * errors if this condition is not true. Even then we should 23128 * send our own request sense packet only if we have a check 23129 * condition and auto request sense has not been performed by 23130 * the hba. 23131 */ 23132 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 23133 23134 if ((sd_scsi_poll(un, wr_pktp) == 0) && 23135 (wr_pktp->pkt_resid == 0)) { 23136 err = SD_SUCCESS; 23137 break; 23138 } 23139 23140 /* 23141 * Check CMD_DEV_GONE 1st, give up if device is gone. 23142 */ 23143 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 23144 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23145 "Error while dumping state...Device is gone\n"); 23146 break; 23147 } 23148 23149 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 23150 SD_INFO(SD_LOG_DUMP, un, 23151 "sddump: write failed with CHECK, try # %d\n", i); 23152 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 23153 (void) sd_send_polled_RQS(un); 23154 } 23155 23156 continue; 23157 } 23158 23159 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 23160 int reset_retval = 0; 23161 23162 SD_INFO(SD_LOG_DUMP, un, 23163 "sddump: write failed with BUSY, try # %d\n", i); 23164 23165 if (un->un_f_lun_reset_enabled == TRUE) { 23166 reset_retval = scsi_reset(SD_ADDRESS(un), 23167 RESET_LUN); 23168 } 23169 if (reset_retval == 0) { 23170 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 23171 } 23172 (void) sd_send_polled_RQS(un); 23173 23174 } else { 23175 SD_INFO(SD_LOG_DUMP, un, 23176 "sddump: write failed with 0x%x, try # %d\n", 23177 SD_GET_PKT_STATUS(wr_pktp), i); 23178 mutex_enter(SD_MUTEX(un)); 23179 sd_reset_target(un, wr_pktp); 23180 mutex_exit(SD_MUTEX(un)); 23181 } 23182 23183 /* 23184 * If we are not getting anywhere with lun/target resets, 23185 * let's reset the bus. 23186 */ 23187 if (i == SD_NDUMP_RETRIES/2) { 23188 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 23189 (void) sd_send_polled_RQS(un); 23190 } 23191 } 23192 } 23193 23194 scsi_destroy_pkt(wr_pktp); 23195 mutex_enter(SD_MUTEX(un)); 23196 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 23197 mutex_exit(SD_MUTEX(un)); 23198 scsi_free_consistent_buf(wr_bp); 23199 } else { 23200 mutex_exit(SD_MUTEX(un)); 23201 } 23202 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 23203 return (err); 23204 } 23205 23206 /* 23207 * Function: sd_scsi_poll() 23208 * 23209 * Description: This is a wrapper for the scsi_poll call. 23210 * 23211 * Arguments: sd_lun - The unit structure 23212 * scsi_pkt - The scsi packet being sent to the device. 23213 * 23214 * Return Code: 0 - Command completed successfully with good status 23215 * -1 - Command failed. This could indicate a check condition 23216 * or other status value requiring recovery action. 23217 * 23218 */ 23219 23220 static int 23221 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 23222 { 23223 int status; 23224 23225 ASSERT(un != NULL); 23226 ASSERT(!mutex_owned(SD_MUTEX(un))); 23227 ASSERT(pktp != NULL); 23228 23229 status = SD_SUCCESS; 23230 23231 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 23232 pktp->pkt_flags |= un->un_tagflags; 23233 pktp->pkt_flags &= ~FLAG_NODISCON; 23234 } 23235 23236 status = sd_ddi_scsi_poll(pktp); 23237 /* 23238 * Scsi_poll returns 0 (success) if the command completes and the 23239 * status block is STATUS_GOOD. We should only check errors if this 23240 * condition is not true. Even then we should send our own request 23241 * sense packet only if we have a check condition and auto 23242 * request sense has not been performed by the hba. 23243 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 23244 */ 23245 if ((status != SD_SUCCESS) && 23246 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 23247 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 23248 (pktp->pkt_reason != CMD_DEV_GONE)) 23249 (void) sd_send_polled_RQS(un); 23250 23251 return (status); 23252 } 23253 23254 /* 23255 * Function: sd_send_polled_RQS() 23256 * 23257 * Description: This sends the request sense command to a device. 23258 * 23259 * Arguments: sd_lun - The unit structure 23260 * 23261 * Return Code: 0 - Command completed successfully with good status 23262 * -1 - Command failed. 23263 * 23264 */ 23265 23266 static int 23267 sd_send_polled_RQS(struct sd_lun *un) 23268 { 23269 int ret_val; 23270 struct scsi_pkt *rqs_pktp; 23271 struct buf *rqs_bp; 23272 23273 ASSERT(un != NULL); 23274 ASSERT(!mutex_owned(SD_MUTEX(un))); 23275 23276 ret_val = SD_SUCCESS; 23277 23278 rqs_pktp = un->un_rqs_pktp; 23279 rqs_bp = un->un_rqs_bp; 23280 23281 mutex_enter(SD_MUTEX(un)); 23282 23283 if (un->un_sense_isbusy) { 23284 ret_val = SD_FAILURE; 23285 mutex_exit(SD_MUTEX(un)); 23286 return (ret_val); 23287 } 23288 23289 /* 23290 * If the request sense buffer (and packet) is not in use, 23291 * let's set the un_sense_isbusy and send our packet 23292 */ 23293 un->un_sense_isbusy = 1; 23294 rqs_pktp->pkt_resid = 0; 23295 rqs_pktp->pkt_reason = 0; 23296 rqs_pktp->pkt_flags |= FLAG_NOINTR; 23297 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 23298 23299 mutex_exit(SD_MUTEX(un)); 23300 23301 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 23302 " 0x%p\n", rqs_bp->b_un.b_addr); 23303 23304 /* 23305 * Can't send this to sd_scsi_poll, we wrap ourselves around the 23306 * axle - it has a call into us! 23307 */ 23308 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 23309 SD_INFO(SD_LOG_COMMON, un, 23310 "sd_send_polled_RQS: RQS failed\n"); 23311 } 23312 23313 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 23314 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 23315 23316 mutex_enter(SD_MUTEX(un)); 23317 un->un_sense_isbusy = 0; 23318 mutex_exit(SD_MUTEX(un)); 23319 23320 return (ret_val); 23321 } 23322 23323 /* 23324 * Defines needed for localized version of the scsi_poll routine. 23325 */ 23326 #define SD_CSEC 10000 /* usecs */ 23327 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 23328 23329 23330 /* 23331 * Function: sd_ddi_scsi_poll() 23332 * 23333 * Description: Localized version of the scsi_poll routine. The purpose is to 23334 * send a scsi_pkt to a device as a polled command. This version 23335 * is to ensure more robust handling of transport errors. 23336 * Specifically this routine cures not ready, coming ready 23337 * transition for power up and reset of sonoma's. This can take 23338 * up to 45 seconds for power-on and 20 seconds for reset of a 23339 * sonoma lun. 23340 * 23341 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 23342 * 23343 * Return Code: 0 - Command completed successfully with good status 23344 * -1 - Command failed. 23345 * 23346 */ 23347 23348 static int 23349 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 23350 { 23351 int busy_count; 23352 int timeout; 23353 int rval = SD_FAILURE; 23354 int savef; 23355 uint8_t *sensep; 23356 long savet; 23357 void (*savec)(); 23358 /* 23359 * The following is defined in machdep.c and is used in determining if 23360 * the scsi transport system will do polled I/O instead of interrupt 23361 * I/O when called from xx_dump(). 23362 */ 23363 extern int do_polled_io; 23364 23365 /* 23366 * save old flags in pkt, to restore at end 23367 */ 23368 savef = pkt->pkt_flags; 23369 savec = pkt->pkt_comp; 23370 savet = pkt->pkt_time; 23371 23372 pkt->pkt_flags |= FLAG_NOINTR; 23373 23374 /* 23375 * XXX there is nothing in the SCSA spec that states that we should not 23376 * do a callback for polled cmds; however, removing this will break sd 23377 * and probably other target drivers 23378 */ 23379 pkt->pkt_comp = NULL; 23380 23381 /* 23382 * we don't like a polled command without timeout. 23383 * 60 seconds seems long enough. 23384 */ 23385 if (pkt->pkt_time == 0) { 23386 pkt->pkt_time = SCSI_POLL_TIMEOUT; 23387 } 23388 23389 /* 23390 * Send polled cmd. 23391 * 23392 * We do some error recovery for various errors. Tran_busy, 23393 * queue full, and non-dispatched commands are retried every 10 msec. 23394 * as they are typically transient failures. Busy status and Not 23395 * Ready are retried every second as this status takes a while to 23396 * change. Unit attention is retried for pkt_time (60) times 23397 * with no delay. 23398 */ 23399 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 23400 23401 for (busy_count = 0; busy_count < timeout; busy_count++) { 23402 int rc; 23403 int poll_delay; 23404 23405 /* 23406 * Initialize pkt status variables. 23407 */ 23408 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 23409 23410 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 23411 if (rc != TRAN_BUSY) { 23412 /* Transport failed - give up. */ 23413 break; 23414 } else { 23415 /* Transport busy - try again. */ 23416 poll_delay = 1 * SD_CSEC; /* 10 msec */ 23417 } 23418 } else { 23419 /* 23420 * Transport accepted - check pkt status. 23421 */ 23422 rc = (*pkt->pkt_scbp) & STATUS_MASK; 23423 if (pkt->pkt_reason == CMD_CMPLT && 23424 rc == STATUS_CHECK && 23425 pkt->pkt_state & STATE_ARQ_DONE) { 23426 struct scsi_arq_status *arqstat = 23427 (struct scsi_arq_status *)(pkt->pkt_scbp); 23428 23429 sensep = (uint8_t *)&arqstat->sts_sensedata; 23430 } else { 23431 sensep = NULL; 23432 } 23433 23434 if ((pkt->pkt_reason == CMD_CMPLT) && 23435 (rc == STATUS_GOOD)) { 23436 /* No error - we're done */ 23437 rval = SD_SUCCESS; 23438 break; 23439 23440 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 23441 /* Lost connection - give up */ 23442 break; 23443 23444 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 23445 (pkt->pkt_state == 0)) { 23446 /* Pkt not dispatched - try again. */ 23447 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 23448 23449 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23450 (rc == STATUS_QFULL)) { 23451 /* Queue full - try again. */ 23452 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 23453 23454 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23455 (rc == STATUS_BUSY)) { 23456 /* Busy - try again. */ 23457 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 23458 busy_count += (SD_SEC_TO_CSEC - 1); 23459 23460 } else if ((sensep != NULL) && 23461 (scsi_sense_key(sensep) == 23462 KEY_UNIT_ATTENTION)) { 23463 /* Unit Attention - try again */ 23464 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 23465 continue; 23466 23467 } else if ((sensep != NULL) && 23468 (scsi_sense_key(sensep) == KEY_NOT_READY) && 23469 (scsi_sense_asc(sensep) == 0x04) && 23470 (scsi_sense_ascq(sensep) == 0x01)) { 23471 /* Not ready -> ready - try again. */ 23472 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 23473 busy_count += (SD_SEC_TO_CSEC - 1); 23474 23475 } else { 23476 /* BAD status - give up. */ 23477 break; 23478 } 23479 } 23480 23481 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 23482 !do_polled_io) { 23483 delay(drv_usectohz(poll_delay)); 23484 } else { 23485 /* we busy wait during cpr_dump or interrupt threads */ 23486 drv_usecwait(poll_delay); 23487 } 23488 } 23489 23490 pkt->pkt_flags = savef; 23491 pkt->pkt_comp = savec; 23492 pkt->pkt_time = savet; 23493 return (rval); 23494 } 23495 23496 23497 /* 23498 * Function: sd_persistent_reservation_in_read_keys 23499 * 23500 * Description: This routine is the driver entry point for handling CD-ROM 23501 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 23502 * by sending the SCSI-3 PRIN commands to the device. 23503 * Processes the read keys command response by copying the 23504 * reservation key information into the user provided buffer. 23505 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 23506 * 23507 * Arguments: un - Pointer to soft state struct for the target. 23508 * usrp - user provided pointer to multihost Persistent In Read 23509 * Keys structure (mhioc_inkeys_t) 23510 * flag - this argument is a pass through to ddi_copyxxx() 23511 * directly from the mode argument of ioctl(). 23512 * 23513 * Return Code: 0 - Success 23514 * EACCES 23515 * ENOTSUP 23516 * errno return code from sd_send_scsi_cmd() 23517 * 23518 * Context: Can sleep. Does not return until command is completed. 23519 */ 23520 23521 static int 23522 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 23523 mhioc_inkeys_t *usrp, int flag) 23524 { 23525 #ifdef _MULTI_DATAMODEL 23526 struct mhioc_key_list32 li32; 23527 #endif 23528 sd_prin_readkeys_t *in; 23529 mhioc_inkeys_t *ptr; 23530 mhioc_key_list_t li; 23531 uchar_t *data_bufp; 23532 int data_len; 23533 int rval; 23534 size_t copysz; 23535 23536 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 23537 return (EINVAL); 23538 } 23539 bzero(&li, sizeof (mhioc_key_list_t)); 23540 23541 /* 23542 * Get the listsize from user 23543 */ 23544 #ifdef _MULTI_DATAMODEL 23545 23546 switch (ddi_model_convert_from(flag & FMODELS)) { 23547 case DDI_MODEL_ILP32: 23548 copysz = sizeof (struct mhioc_key_list32); 23549 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 23550 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23551 "sd_persistent_reservation_in_read_keys: " 23552 "failed ddi_copyin: mhioc_key_list32_t\n"); 23553 rval = EFAULT; 23554 goto done; 23555 } 23556 li.listsize = li32.listsize; 23557 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 23558 break; 23559 23560 case DDI_MODEL_NONE: 23561 copysz = sizeof (mhioc_key_list_t); 23562 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23563 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23564 "sd_persistent_reservation_in_read_keys: " 23565 "failed ddi_copyin: mhioc_key_list_t\n"); 23566 rval = EFAULT; 23567 goto done; 23568 } 23569 break; 23570 } 23571 23572 #else /* ! _MULTI_DATAMODEL */ 23573 copysz = sizeof (mhioc_key_list_t); 23574 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23575 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23576 "sd_persistent_reservation_in_read_keys: " 23577 "failed ddi_copyin: mhioc_key_list_t\n"); 23578 rval = EFAULT; 23579 goto done; 23580 } 23581 #endif 23582 23583 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 23584 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 23585 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23586 23587 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 23588 data_len, data_bufp)) != 0) { 23589 goto done; 23590 } 23591 in = (sd_prin_readkeys_t *)data_bufp; 23592 ptr->generation = BE_32(in->generation); 23593 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 23594 23595 /* 23596 * Return the min(listsize, listlen) keys 23597 */ 23598 #ifdef _MULTI_DATAMODEL 23599 23600 switch (ddi_model_convert_from(flag & FMODELS)) { 23601 case DDI_MODEL_ILP32: 23602 li32.listlen = li.listlen; 23603 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 23604 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23605 "sd_persistent_reservation_in_read_keys: " 23606 "failed ddi_copyout: mhioc_key_list32_t\n"); 23607 rval = EFAULT; 23608 goto done; 23609 } 23610 break; 23611 23612 case DDI_MODEL_NONE: 23613 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23614 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23615 "sd_persistent_reservation_in_read_keys: " 23616 "failed ddi_copyout: mhioc_key_list_t\n"); 23617 rval = EFAULT; 23618 goto done; 23619 } 23620 break; 23621 } 23622 23623 #else /* ! _MULTI_DATAMODEL */ 23624 23625 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23626 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23627 "sd_persistent_reservation_in_read_keys: " 23628 "failed ddi_copyout: mhioc_key_list_t\n"); 23629 rval = EFAULT; 23630 goto done; 23631 } 23632 23633 #endif /* _MULTI_DATAMODEL */ 23634 23635 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 23636 li.listsize * MHIOC_RESV_KEY_SIZE); 23637 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 23638 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23639 "sd_persistent_reservation_in_read_keys: " 23640 "failed ddi_copyout: keylist\n"); 23641 rval = EFAULT; 23642 } 23643 done: 23644 kmem_free(data_bufp, data_len); 23645 return (rval); 23646 } 23647 23648 23649 /* 23650 * Function: sd_persistent_reservation_in_read_resv 23651 * 23652 * Description: This routine is the driver entry point for handling CD-ROM 23653 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 23654 * by sending the SCSI-3 PRIN commands to the device. 23655 * Process the read persistent reservations command response by 23656 * copying the reservation information into the user provided 23657 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 23658 * 23659 * Arguments: un - Pointer to soft state struct for the target. 23660 * usrp - user provided pointer to multihost Persistent In Read 23661 * Keys structure (mhioc_inkeys_t) 23662 * flag - this argument is a pass through to ddi_copyxxx() 23663 * directly from the mode argument of ioctl(). 23664 * 23665 * Return Code: 0 - Success 23666 * EACCES 23667 * ENOTSUP 23668 * errno return code from sd_send_scsi_cmd() 23669 * 23670 * Context: Can sleep. Does not return until command is completed. 23671 */ 23672 23673 static int 23674 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 23675 mhioc_inresvs_t *usrp, int flag) 23676 { 23677 #ifdef _MULTI_DATAMODEL 23678 struct mhioc_resv_desc_list32 resvlist32; 23679 #endif 23680 sd_prin_readresv_t *in; 23681 mhioc_inresvs_t *ptr; 23682 sd_readresv_desc_t *readresv_ptr; 23683 mhioc_resv_desc_list_t resvlist; 23684 mhioc_resv_desc_t resvdesc; 23685 uchar_t *data_bufp; 23686 int data_len; 23687 int rval; 23688 int i; 23689 size_t copysz; 23690 mhioc_resv_desc_t *bufp; 23691 23692 if ((ptr = usrp) == NULL) { 23693 return (EINVAL); 23694 } 23695 23696 /* 23697 * Get the listsize from user 23698 */ 23699 #ifdef _MULTI_DATAMODEL 23700 switch (ddi_model_convert_from(flag & FMODELS)) { 23701 case DDI_MODEL_ILP32: 23702 copysz = sizeof (struct mhioc_resv_desc_list32); 23703 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 23704 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23705 "sd_persistent_reservation_in_read_resv: " 23706 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23707 rval = EFAULT; 23708 goto done; 23709 } 23710 resvlist.listsize = resvlist32.listsize; 23711 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 23712 break; 23713 23714 case DDI_MODEL_NONE: 23715 copysz = sizeof (mhioc_resv_desc_list_t); 23716 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23717 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23718 "sd_persistent_reservation_in_read_resv: " 23719 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23720 rval = EFAULT; 23721 goto done; 23722 } 23723 break; 23724 } 23725 #else /* ! _MULTI_DATAMODEL */ 23726 copysz = sizeof (mhioc_resv_desc_list_t); 23727 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23728 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23729 "sd_persistent_reservation_in_read_resv: " 23730 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23731 rval = EFAULT; 23732 goto done; 23733 } 23734 #endif /* ! _MULTI_DATAMODEL */ 23735 23736 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 23737 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 23738 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23739 23740 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 23741 data_len, data_bufp)) != 0) { 23742 goto done; 23743 } 23744 in = (sd_prin_readresv_t *)data_bufp; 23745 ptr->generation = BE_32(in->generation); 23746 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 23747 23748 /* 23749 * Return the min(listsize, listlen( keys 23750 */ 23751 #ifdef _MULTI_DATAMODEL 23752 23753 switch (ddi_model_convert_from(flag & FMODELS)) { 23754 case DDI_MODEL_ILP32: 23755 resvlist32.listlen = resvlist.listlen; 23756 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 23757 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23758 "sd_persistent_reservation_in_read_resv: " 23759 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23760 rval = EFAULT; 23761 goto done; 23762 } 23763 break; 23764 23765 case DDI_MODEL_NONE: 23766 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23767 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23768 "sd_persistent_reservation_in_read_resv: " 23769 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23770 rval = EFAULT; 23771 goto done; 23772 } 23773 break; 23774 } 23775 23776 #else /* ! _MULTI_DATAMODEL */ 23777 23778 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23779 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23780 "sd_persistent_reservation_in_read_resv: " 23781 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23782 rval = EFAULT; 23783 goto done; 23784 } 23785 23786 #endif /* ! _MULTI_DATAMODEL */ 23787 23788 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 23789 bufp = resvlist.list; 23790 copysz = sizeof (mhioc_resv_desc_t); 23791 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 23792 i++, readresv_ptr++, bufp++) { 23793 23794 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 23795 MHIOC_RESV_KEY_SIZE); 23796 resvdesc.type = readresv_ptr->type; 23797 resvdesc.scope = readresv_ptr->scope; 23798 resvdesc.scope_specific_addr = 23799 BE_32(readresv_ptr->scope_specific_addr); 23800 23801 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 23802 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23803 "sd_persistent_reservation_in_read_resv: " 23804 "failed ddi_copyout: resvlist\n"); 23805 rval = EFAULT; 23806 goto done; 23807 } 23808 } 23809 done: 23810 kmem_free(data_bufp, data_len); 23811 return (rval); 23812 } 23813 23814 23815 /* 23816 * Function: sr_change_blkmode() 23817 * 23818 * Description: This routine is the driver entry point for handling CD-ROM 23819 * block mode ioctl requests. Support for returning and changing 23820 * the current block size in use by the device is implemented. The 23821 * LBA size is changed via a MODE SELECT Block Descriptor. 23822 * 23823 * This routine issues a mode sense with an allocation length of 23824 * 12 bytes for the mode page header and a single block descriptor. 23825 * 23826 * Arguments: dev - the device 'dev_t' 23827 * cmd - the request type; one of CDROMGBLKMODE (get) or 23828 * CDROMSBLKMODE (set) 23829 * data - current block size or requested block size 23830 * flag - this argument is a pass through to ddi_copyxxx() directly 23831 * from the mode argument of ioctl(). 23832 * 23833 * Return Code: the code returned by sd_send_scsi_cmd() 23834 * EINVAL if invalid arguments are provided 23835 * EFAULT if ddi_copyxxx() fails 23836 * ENXIO if fail ddi_get_soft_state 23837 * EIO if invalid mode sense block descriptor length 23838 * 23839 */ 23840 23841 static int 23842 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 23843 { 23844 struct sd_lun *un = NULL; 23845 struct mode_header *sense_mhp, *select_mhp; 23846 struct block_descriptor *sense_desc, *select_desc; 23847 int current_bsize; 23848 int rval = EINVAL; 23849 uchar_t *sense = NULL; 23850 uchar_t *select = NULL; 23851 23852 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 23853 23854 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23855 return (ENXIO); 23856 } 23857 23858 /* 23859 * The block length is changed via the Mode Select block descriptor, the 23860 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 23861 * required as part of this routine. Therefore the mode sense allocation 23862 * length is specified to be the length of a mode page header and a 23863 * block descriptor. 23864 */ 23865 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 23866 23867 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 23868 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 23869 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23870 "sr_change_blkmode: Mode Sense Failed\n"); 23871 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23872 return (rval); 23873 } 23874 23875 /* Check the block descriptor len to handle only 1 block descriptor */ 23876 sense_mhp = (struct mode_header *)sense; 23877 if ((sense_mhp->bdesc_length == 0) || 23878 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 23879 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23880 "sr_change_blkmode: Mode Sense returned invalid block" 23881 " descriptor length\n"); 23882 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23883 return (EIO); 23884 } 23885 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 23886 current_bsize = ((sense_desc->blksize_hi << 16) | 23887 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 23888 23889 /* Process command */ 23890 switch (cmd) { 23891 case CDROMGBLKMODE: 23892 /* Return the block size obtained during the mode sense */ 23893 if (ddi_copyout(¤t_bsize, (void *)data, 23894 sizeof (int), flag) != 0) 23895 rval = EFAULT; 23896 break; 23897 case CDROMSBLKMODE: 23898 /* Validate the requested block size */ 23899 switch (data) { 23900 case CDROM_BLK_512: 23901 case CDROM_BLK_1024: 23902 case CDROM_BLK_2048: 23903 case CDROM_BLK_2056: 23904 case CDROM_BLK_2336: 23905 case CDROM_BLK_2340: 23906 case CDROM_BLK_2352: 23907 case CDROM_BLK_2368: 23908 case CDROM_BLK_2448: 23909 case CDROM_BLK_2646: 23910 case CDROM_BLK_2647: 23911 break; 23912 default: 23913 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23914 "sr_change_blkmode: " 23915 "Block Size '%ld' Not Supported\n", data); 23916 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23917 return (EINVAL); 23918 } 23919 23920 /* 23921 * The current block size matches the requested block size so 23922 * there is no need to send the mode select to change the size 23923 */ 23924 if (current_bsize == data) { 23925 break; 23926 } 23927 23928 /* Build the select data for the requested block size */ 23929 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 23930 select_mhp = (struct mode_header *)select; 23931 select_desc = 23932 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 23933 /* 23934 * The LBA size is changed via the block descriptor, so the 23935 * descriptor is built according to the user data 23936 */ 23937 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 23938 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 23939 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 23940 select_desc->blksize_lo = (char)((data) & 0x000000ff); 23941 23942 /* Send the mode select for the requested block size */ 23943 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 23944 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 23945 SD_PATH_STANDARD)) != 0) { 23946 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23947 "sr_change_blkmode: Mode Select Failed\n"); 23948 /* 23949 * The mode select failed for the requested block size, 23950 * so reset the data for the original block size and 23951 * send it to the target. The error is indicated by the 23952 * return value for the failed mode select. 23953 */ 23954 select_desc->blksize_hi = sense_desc->blksize_hi; 23955 select_desc->blksize_mid = sense_desc->blksize_mid; 23956 select_desc->blksize_lo = sense_desc->blksize_lo; 23957 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 23958 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 23959 SD_PATH_STANDARD); 23960 } else { 23961 ASSERT(!mutex_owned(SD_MUTEX(un))); 23962 mutex_enter(SD_MUTEX(un)); 23963 sd_update_block_info(un, (uint32_t)data, 0); 23964 mutex_exit(SD_MUTEX(un)); 23965 } 23966 break; 23967 default: 23968 /* should not reach here, but check anyway */ 23969 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23970 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 23971 rval = EINVAL; 23972 break; 23973 } 23974 23975 if (select) { 23976 kmem_free(select, BUFLEN_CHG_BLK_MODE); 23977 } 23978 if (sense) { 23979 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23980 } 23981 return (rval); 23982 } 23983 23984 23985 /* 23986 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 23987 * implement driver support for getting and setting the CD speed. The command 23988 * set used will be based on the device type. If the device has not been 23989 * identified as MMC the Toshiba vendor specific mode page will be used. If 23990 * the device is MMC but does not support the Real Time Streaming feature 23991 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 23992 * be used to read the speed. 23993 */ 23994 23995 /* 23996 * Function: sr_change_speed() 23997 * 23998 * Description: This routine is the driver entry point for handling CD-ROM 23999 * drive speed ioctl requests for devices supporting the Toshiba 24000 * vendor specific drive speed mode page. Support for returning 24001 * and changing the current drive speed in use by the device is 24002 * implemented. 24003 * 24004 * Arguments: dev - the device 'dev_t' 24005 * cmd - the request type; one of CDROMGDRVSPEED (get) or 24006 * CDROMSDRVSPEED (set) 24007 * data - current drive speed or requested drive speed 24008 * flag - this argument is a pass through to ddi_copyxxx() directly 24009 * from the mode argument of ioctl(). 24010 * 24011 * Return Code: the code returned by sd_send_scsi_cmd() 24012 * EINVAL if invalid arguments are provided 24013 * EFAULT if ddi_copyxxx() fails 24014 * ENXIO if fail ddi_get_soft_state 24015 * EIO if invalid mode sense block descriptor length 24016 */ 24017 24018 static int 24019 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 24020 { 24021 struct sd_lun *un = NULL; 24022 struct mode_header *sense_mhp, *select_mhp; 24023 struct mode_speed *sense_page, *select_page; 24024 int current_speed; 24025 int rval = EINVAL; 24026 int bd_len; 24027 uchar_t *sense = NULL; 24028 uchar_t *select = NULL; 24029 24030 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 24031 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24032 return (ENXIO); 24033 } 24034 24035 /* 24036 * Note: The drive speed is being modified here according to a Toshiba 24037 * vendor specific mode page (0x31). 24038 */ 24039 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 24040 24041 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 24042 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 24043 SD_PATH_STANDARD)) != 0) { 24044 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24045 "sr_change_speed: Mode Sense Failed\n"); 24046 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24047 return (rval); 24048 } 24049 sense_mhp = (struct mode_header *)sense; 24050 24051 /* Check the block descriptor len to handle only 1 block descriptor */ 24052 bd_len = sense_mhp->bdesc_length; 24053 if (bd_len > MODE_BLK_DESC_LENGTH) { 24054 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24055 "sr_change_speed: Mode Sense returned invalid block " 24056 "descriptor length\n"); 24057 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24058 return (EIO); 24059 } 24060 24061 sense_page = (struct mode_speed *) 24062 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 24063 current_speed = sense_page->speed; 24064 24065 /* Process command */ 24066 switch (cmd) { 24067 case CDROMGDRVSPEED: 24068 /* Return the drive speed obtained during the mode sense */ 24069 if (current_speed == 0x2) { 24070 current_speed = CDROM_TWELVE_SPEED; 24071 } 24072 if (ddi_copyout(¤t_speed, (void *)data, 24073 sizeof (int), flag) != 0) { 24074 rval = EFAULT; 24075 } 24076 break; 24077 case CDROMSDRVSPEED: 24078 /* Validate the requested drive speed */ 24079 switch ((uchar_t)data) { 24080 case CDROM_TWELVE_SPEED: 24081 data = 0x2; 24082 /*FALLTHROUGH*/ 24083 case CDROM_NORMAL_SPEED: 24084 case CDROM_DOUBLE_SPEED: 24085 case CDROM_QUAD_SPEED: 24086 case CDROM_MAXIMUM_SPEED: 24087 break; 24088 default: 24089 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24090 "sr_change_speed: " 24091 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 24092 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24093 return (EINVAL); 24094 } 24095 24096 /* 24097 * The current drive speed matches the requested drive speed so 24098 * there is no need to send the mode select to change the speed 24099 */ 24100 if (current_speed == data) { 24101 break; 24102 } 24103 24104 /* Build the select data for the requested drive speed */ 24105 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 24106 select_mhp = (struct mode_header *)select; 24107 select_mhp->bdesc_length = 0; 24108 select_page = 24109 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 24110 select_page = 24111 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 24112 select_page->mode_page.code = CDROM_MODE_SPEED; 24113 select_page->mode_page.length = 2; 24114 select_page->speed = (uchar_t)data; 24115 24116 /* Send the mode select for the requested block size */ 24117 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 24118 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 24119 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 24120 /* 24121 * The mode select failed for the requested drive speed, 24122 * so reset the data for the original drive speed and 24123 * send it to the target. The error is indicated by the 24124 * return value for the failed mode select. 24125 */ 24126 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24127 "sr_drive_speed: Mode Select Failed\n"); 24128 select_page->speed = sense_page->speed; 24129 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 24130 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 24131 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 24132 } 24133 break; 24134 default: 24135 /* should not reach here, but check anyway */ 24136 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24137 "sr_change_speed: Command '%x' Not Supported\n", cmd); 24138 rval = EINVAL; 24139 break; 24140 } 24141 24142 if (select) { 24143 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 24144 } 24145 if (sense) { 24146 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24147 } 24148 24149 return (rval); 24150 } 24151 24152 24153 /* 24154 * Function: sr_atapi_change_speed() 24155 * 24156 * Description: This routine is the driver entry point for handling CD-ROM 24157 * drive speed ioctl requests for MMC devices that do not support 24158 * the Real Time Streaming feature (0x107). 24159 * 24160 * Note: This routine will use the SET SPEED command which may not 24161 * be supported by all devices. 24162 * 24163 * Arguments: dev- the device 'dev_t' 24164 * cmd- the request type; one of CDROMGDRVSPEED (get) or 24165 * CDROMSDRVSPEED (set) 24166 * data- current drive speed or requested drive speed 24167 * flag- this argument is a pass through to ddi_copyxxx() directly 24168 * from the mode argument of ioctl(). 24169 * 24170 * Return Code: the code returned by sd_send_scsi_cmd() 24171 * EINVAL if invalid arguments are provided 24172 * EFAULT if ddi_copyxxx() fails 24173 * ENXIO if fail ddi_get_soft_state 24174 * EIO if invalid mode sense block descriptor length 24175 */ 24176 24177 static int 24178 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 24179 { 24180 struct sd_lun *un; 24181 struct uscsi_cmd *com = NULL; 24182 struct mode_header_grp2 *sense_mhp; 24183 uchar_t *sense_page; 24184 uchar_t *sense = NULL; 24185 char cdb[CDB_GROUP5]; 24186 int bd_len; 24187 int current_speed = 0; 24188 int max_speed = 0; 24189 int rval; 24190 24191 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 24192 24193 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24194 return (ENXIO); 24195 } 24196 24197 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 24198 24199 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 24200 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 24201 SD_PATH_STANDARD)) != 0) { 24202 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24203 "sr_atapi_change_speed: Mode Sense Failed\n"); 24204 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24205 return (rval); 24206 } 24207 24208 /* Check the block descriptor len to handle only 1 block descriptor */ 24209 sense_mhp = (struct mode_header_grp2 *)sense; 24210 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 24211 if (bd_len > MODE_BLK_DESC_LENGTH) { 24212 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24213 "sr_atapi_change_speed: Mode Sense returned invalid " 24214 "block descriptor length\n"); 24215 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24216 return (EIO); 24217 } 24218 24219 /* Calculate the current and maximum drive speeds */ 24220 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 24221 current_speed = (sense_page[14] << 8) | sense_page[15]; 24222 max_speed = (sense_page[8] << 8) | sense_page[9]; 24223 24224 /* Process the command */ 24225 switch (cmd) { 24226 case CDROMGDRVSPEED: 24227 current_speed /= SD_SPEED_1X; 24228 if (ddi_copyout(¤t_speed, (void *)data, 24229 sizeof (int), flag) != 0) 24230 rval = EFAULT; 24231 break; 24232 case CDROMSDRVSPEED: 24233 /* Convert the speed code to KB/sec */ 24234 switch ((uchar_t)data) { 24235 case CDROM_NORMAL_SPEED: 24236 current_speed = SD_SPEED_1X; 24237 break; 24238 case CDROM_DOUBLE_SPEED: 24239 current_speed = 2 * SD_SPEED_1X; 24240 break; 24241 case CDROM_QUAD_SPEED: 24242 current_speed = 4 * SD_SPEED_1X; 24243 break; 24244 case CDROM_TWELVE_SPEED: 24245 current_speed = 12 * SD_SPEED_1X; 24246 break; 24247 case CDROM_MAXIMUM_SPEED: 24248 current_speed = 0xffff; 24249 break; 24250 default: 24251 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24252 "sr_atapi_change_speed: invalid drive speed %d\n", 24253 (uchar_t)data); 24254 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24255 return (EINVAL); 24256 } 24257 24258 /* Check the request against the drive's max speed. */ 24259 if (current_speed != 0xffff) { 24260 if (current_speed > max_speed) { 24261 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24262 return (EINVAL); 24263 } 24264 } 24265 24266 /* 24267 * Build and send the SET SPEED command 24268 * 24269 * Note: The SET SPEED (0xBB) command used in this routine is 24270 * obsolete per the SCSI MMC spec but still supported in the 24271 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24272 * therefore the command is still implemented in this routine. 24273 */ 24274 bzero(cdb, sizeof (cdb)); 24275 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 24276 cdb[2] = (uchar_t)(current_speed >> 8); 24277 cdb[3] = (uchar_t)current_speed; 24278 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24279 com->uscsi_cdb = (caddr_t)cdb; 24280 com->uscsi_cdblen = CDB_GROUP5; 24281 com->uscsi_bufaddr = NULL; 24282 com->uscsi_buflen = 0; 24283 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24284 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 24285 break; 24286 default: 24287 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24288 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 24289 rval = EINVAL; 24290 } 24291 24292 if (sense) { 24293 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24294 } 24295 if (com) { 24296 kmem_free(com, sizeof (*com)); 24297 } 24298 return (rval); 24299 } 24300 24301 24302 /* 24303 * Function: sr_pause_resume() 24304 * 24305 * Description: This routine is the driver entry point for handling CD-ROM 24306 * pause/resume ioctl requests. This only affects the audio play 24307 * operation. 24308 * 24309 * Arguments: dev - the device 'dev_t' 24310 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 24311 * for setting the resume bit of the cdb. 24312 * 24313 * Return Code: the code returned by sd_send_scsi_cmd() 24314 * EINVAL if invalid mode specified 24315 * 24316 */ 24317 24318 static int 24319 sr_pause_resume(dev_t dev, int cmd) 24320 { 24321 struct sd_lun *un; 24322 struct uscsi_cmd *com; 24323 char cdb[CDB_GROUP1]; 24324 int rval; 24325 24326 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24327 return (ENXIO); 24328 } 24329 24330 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24331 bzero(cdb, CDB_GROUP1); 24332 cdb[0] = SCMD_PAUSE_RESUME; 24333 switch (cmd) { 24334 case CDROMRESUME: 24335 cdb[8] = 1; 24336 break; 24337 case CDROMPAUSE: 24338 cdb[8] = 0; 24339 break; 24340 default: 24341 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 24342 " Command '%x' Not Supported\n", cmd); 24343 rval = EINVAL; 24344 goto done; 24345 } 24346 24347 com->uscsi_cdb = cdb; 24348 com->uscsi_cdblen = CDB_GROUP1; 24349 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24350 24351 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24352 SD_PATH_STANDARD); 24353 24354 done: 24355 kmem_free(com, sizeof (*com)); 24356 return (rval); 24357 } 24358 24359 24360 /* 24361 * Function: sr_play_msf() 24362 * 24363 * Description: This routine is the driver entry point for handling CD-ROM 24364 * ioctl requests to output the audio signals at the specified 24365 * starting address and continue the audio play until the specified 24366 * ending address (CDROMPLAYMSF) The address is in Minute Second 24367 * Frame (MSF) format. 24368 * 24369 * Arguments: dev - the device 'dev_t' 24370 * data - pointer to user provided audio msf structure, 24371 * specifying start/end addresses. 24372 * flag - this argument is a pass through to ddi_copyxxx() 24373 * directly from the mode argument of ioctl(). 24374 * 24375 * Return Code: the code returned by sd_send_scsi_cmd() 24376 * EFAULT if ddi_copyxxx() fails 24377 * ENXIO if fail ddi_get_soft_state 24378 * EINVAL if data pointer is NULL 24379 */ 24380 24381 static int 24382 sr_play_msf(dev_t dev, caddr_t data, int flag) 24383 { 24384 struct sd_lun *un; 24385 struct uscsi_cmd *com; 24386 struct cdrom_msf msf_struct; 24387 struct cdrom_msf *msf = &msf_struct; 24388 char cdb[CDB_GROUP1]; 24389 int rval; 24390 24391 if (data == NULL) { 24392 return (EINVAL); 24393 } 24394 24395 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24396 return (ENXIO); 24397 } 24398 24399 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 24400 return (EFAULT); 24401 } 24402 24403 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24404 bzero(cdb, CDB_GROUP1); 24405 cdb[0] = SCMD_PLAYAUDIO_MSF; 24406 if (un->un_f_cfg_playmsf_bcd == TRUE) { 24407 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 24408 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 24409 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 24410 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 24411 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 24412 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 24413 } else { 24414 cdb[3] = msf->cdmsf_min0; 24415 cdb[4] = msf->cdmsf_sec0; 24416 cdb[5] = msf->cdmsf_frame0; 24417 cdb[6] = msf->cdmsf_min1; 24418 cdb[7] = msf->cdmsf_sec1; 24419 cdb[8] = msf->cdmsf_frame1; 24420 } 24421 com->uscsi_cdb = cdb; 24422 com->uscsi_cdblen = CDB_GROUP1; 24423 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24424 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24425 SD_PATH_STANDARD); 24426 kmem_free(com, sizeof (*com)); 24427 return (rval); 24428 } 24429 24430 24431 /* 24432 * Function: sr_play_trkind() 24433 * 24434 * Description: This routine is the driver entry point for handling CD-ROM 24435 * ioctl requests to output the audio signals at the specified 24436 * starting address and continue the audio play until the specified 24437 * ending address (CDROMPLAYTRKIND). The address is in Track Index 24438 * format. 24439 * 24440 * Arguments: dev - the device 'dev_t' 24441 * data - pointer to user provided audio track/index structure, 24442 * specifying start/end addresses. 24443 * flag - this argument is a pass through to ddi_copyxxx() 24444 * directly from the mode argument of ioctl(). 24445 * 24446 * Return Code: the code returned by sd_send_scsi_cmd() 24447 * EFAULT if ddi_copyxxx() fails 24448 * ENXIO if fail ddi_get_soft_state 24449 * EINVAL if data pointer is NULL 24450 */ 24451 24452 static int 24453 sr_play_trkind(dev_t dev, caddr_t data, int flag) 24454 { 24455 struct cdrom_ti ti_struct; 24456 struct cdrom_ti *ti = &ti_struct; 24457 struct uscsi_cmd *com = NULL; 24458 char cdb[CDB_GROUP1]; 24459 int rval; 24460 24461 if (data == NULL) { 24462 return (EINVAL); 24463 } 24464 24465 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 24466 return (EFAULT); 24467 } 24468 24469 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24470 bzero(cdb, CDB_GROUP1); 24471 cdb[0] = SCMD_PLAYAUDIO_TI; 24472 cdb[4] = ti->cdti_trk0; 24473 cdb[5] = ti->cdti_ind0; 24474 cdb[7] = ti->cdti_trk1; 24475 cdb[8] = ti->cdti_ind1; 24476 com->uscsi_cdb = cdb; 24477 com->uscsi_cdblen = CDB_GROUP1; 24478 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24479 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24480 SD_PATH_STANDARD); 24481 kmem_free(com, sizeof (*com)); 24482 return (rval); 24483 } 24484 24485 24486 /* 24487 * Function: sr_read_all_subcodes() 24488 * 24489 * Description: This routine is the driver entry point for handling CD-ROM 24490 * ioctl requests to return raw subcode data while the target is 24491 * playing audio (CDROMSUBCODE). 24492 * 24493 * Arguments: dev - the device 'dev_t' 24494 * data - pointer to user provided cdrom subcode structure, 24495 * specifying the transfer length and address. 24496 * flag - this argument is a pass through to ddi_copyxxx() 24497 * directly from the mode argument of ioctl(). 24498 * 24499 * Return Code: the code returned by sd_send_scsi_cmd() 24500 * EFAULT if ddi_copyxxx() fails 24501 * ENXIO if fail ddi_get_soft_state 24502 * EINVAL if data pointer is NULL 24503 */ 24504 24505 static int 24506 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 24507 { 24508 struct sd_lun *un = NULL; 24509 struct uscsi_cmd *com = NULL; 24510 struct cdrom_subcode *subcode = NULL; 24511 int rval; 24512 size_t buflen; 24513 char cdb[CDB_GROUP5]; 24514 24515 #ifdef _MULTI_DATAMODEL 24516 /* To support ILP32 applications in an LP64 world */ 24517 struct cdrom_subcode32 cdrom_subcode32; 24518 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 24519 #endif 24520 if (data == NULL) { 24521 return (EINVAL); 24522 } 24523 24524 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24525 return (ENXIO); 24526 } 24527 24528 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 24529 24530 #ifdef _MULTI_DATAMODEL 24531 switch (ddi_model_convert_from(flag & FMODELS)) { 24532 case DDI_MODEL_ILP32: 24533 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 24534 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24535 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24536 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24537 return (EFAULT); 24538 } 24539 /* Convert the ILP32 uscsi data from the application to LP64 */ 24540 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 24541 break; 24542 case DDI_MODEL_NONE: 24543 if (ddi_copyin(data, subcode, 24544 sizeof (struct cdrom_subcode), flag)) { 24545 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24546 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24547 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24548 return (EFAULT); 24549 } 24550 break; 24551 } 24552 #else /* ! _MULTI_DATAMODEL */ 24553 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 24554 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24555 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24556 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24557 return (EFAULT); 24558 } 24559 #endif /* _MULTI_DATAMODEL */ 24560 24561 /* 24562 * Since MMC-2 expects max 3 bytes for length, check if the 24563 * length input is greater than 3 bytes 24564 */ 24565 if ((subcode->cdsc_length & 0xFF000000) != 0) { 24566 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24567 "sr_read_all_subcodes: " 24568 "cdrom transfer length too large: %d (limit %d)\n", 24569 subcode->cdsc_length, 0xFFFFFF); 24570 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24571 return (EINVAL); 24572 } 24573 24574 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 24575 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24576 bzero(cdb, CDB_GROUP5); 24577 24578 if (un->un_f_mmc_cap == TRUE) { 24579 cdb[0] = (char)SCMD_READ_CD; 24580 cdb[2] = (char)0xff; 24581 cdb[3] = (char)0xff; 24582 cdb[4] = (char)0xff; 24583 cdb[5] = (char)0xff; 24584 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24585 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24586 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 24587 cdb[10] = 1; 24588 } else { 24589 /* 24590 * Note: A vendor specific command (0xDF) is being used her to 24591 * request a read of all subcodes. 24592 */ 24593 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 24594 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 24595 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24596 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24597 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 24598 } 24599 com->uscsi_cdb = cdb; 24600 com->uscsi_cdblen = CDB_GROUP5; 24601 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 24602 com->uscsi_buflen = buflen; 24603 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24604 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 24605 SD_PATH_STANDARD); 24606 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24607 kmem_free(com, sizeof (*com)); 24608 return (rval); 24609 } 24610 24611 24612 /* 24613 * Function: sr_read_subchannel() 24614 * 24615 * Description: This routine is the driver entry point for handling CD-ROM 24616 * ioctl requests to return the Q sub-channel data of the CD 24617 * current position block. (CDROMSUBCHNL) The data includes the 24618 * track number, index number, absolute CD-ROM address (LBA or MSF 24619 * format per the user) , track relative CD-ROM address (LBA or MSF 24620 * format per the user), control data and audio status. 24621 * 24622 * Arguments: dev - the device 'dev_t' 24623 * data - pointer to user provided cdrom sub-channel structure 24624 * flag - this argument is a pass through to ddi_copyxxx() 24625 * directly from the mode argument of ioctl(). 24626 * 24627 * Return Code: the code returned by sd_send_scsi_cmd() 24628 * EFAULT if ddi_copyxxx() fails 24629 * ENXIO if fail ddi_get_soft_state 24630 * EINVAL if data pointer is NULL 24631 */ 24632 24633 static int 24634 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 24635 { 24636 struct sd_lun *un; 24637 struct uscsi_cmd *com; 24638 struct cdrom_subchnl subchanel; 24639 struct cdrom_subchnl *subchnl = &subchanel; 24640 char cdb[CDB_GROUP1]; 24641 caddr_t buffer; 24642 int rval; 24643 24644 if (data == NULL) { 24645 return (EINVAL); 24646 } 24647 24648 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24649 (un->un_state == SD_STATE_OFFLINE)) { 24650 return (ENXIO); 24651 } 24652 24653 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 24654 return (EFAULT); 24655 } 24656 24657 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 24658 bzero(cdb, CDB_GROUP1); 24659 cdb[0] = SCMD_READ_SUBCHANNEL; 24660 /* Set the MSF bit based on the user requested address format */ 24661 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 24662 /* 24663 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 24664 * returned 24665 */ 24666 cdb[2] = 0x40; 24667 /* 24668 * Set byte 3 to specify the return data format. A value of 0x01 24669 * indicates that the CD-ROM current position should be returned. 24670 */ 24671 cdb[3] = 0x01; 24672 cdb[8] = 0x10; 24673 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24674 com->uscsi_cdb = cdb; 24675 com->uscsi_cdblen = CDB_GROUP1; 24676 com->uscsi_bufaddr = buffer; 24677 com->uscsi_buflen = 16; 24678 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24679 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24680 SD_PATH_STANDARD); 24681 if (rval != 0) { 24682 kmem_free(buffer, 16); 24683 kmem_free(com, sizeof (*com)); 24684 return (rval); 24685 } 24686 24687 /* Process the returned Q sub-channel data */ 24688 subchnl->cdsc_audiostatus = buffer[1]; 24689 subchnl->cdsc_adr = (buffer[5] & 0xF0); 24690 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 24691 subchnl->cdsc_trk = buffer[6]; 24692 subchnl->cdsc_ind = buffer[7]; 24693 if (subchnl->cdsc_format & CDROM_LBA) { 24694 subchnl->cdsc_absaddr.lba = 24695 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24696 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24697 subchnl->cdsc_reladdr.lba = 24698 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 24699 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 24700 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 24701 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 24702 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 24703 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 24704 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 24705 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 24706 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 24707 } else { 24708 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 24709 subchnl->cdsc_absaddr.msf.second = buffer[10]; 24710 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 24711 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 24712 subchnl->cdsc_reladdr.msf.second = buffer[14]; 24713 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 24714 } 24715 kmem_free(buffer, 16); 24716 kmem_free(com, sizeof (*com)); 24717 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 24718 != 0) { 24719 return (EFAULT); 24720 } 24721 return (rval); 24722 } 24723 24724 24725 /* 24726 * Function: sr_read_tocentry() 24727 * 24728 * Description: This routine is the driver entry point for handling CD-ROM 24729 * ioctl requests to read from the Table of Contents (TOC) 24730 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 24731 * fields, the starting address (LBA or MSF format per the user) 24732 * and the data mode if the user specified track is a data track. 24733 * 24734 * Note: The READ HEADER (0x44) command used in this routine is 24735 * obsolete per the SCSI MMC spec but still supported in the 24736 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24737 * therefore the command is still implemented in this routine. 24738 * 24739 * Arguments: dev - the device 'dev_t' 24740 * data - pointer to user provided toc entry structure, 24741 * specifying the track # and the address format 24742 * (LBA or MSF). 24743 * flag - this argument is a pass through to ddi_copyxxx() 24744 * directly from the mode argument of ioctl(). 24745 * 24746 * Return Code: the code returned by sd_send_scsi_cmd() 24747 * EFAULT if ddi_copyxxx() fails 24748 * ENXIO if fail ddi_get_soft_state 24749 * EINVAL if data pointer is NULL 24750 */ 24751 24752 static int 24753 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 24754 { 24755 struct sd_lun *un = NULL; 24756 struct uscsi_cmd *com; 24757 struct cdrom_tocentry toc_entry; 24758 struct cdrom_tocentry *entry = &toc_entry; 24759 caddr_t buffer; 24760 int rval; 24761 char cdb[CDB_GROUP1]; 24762 24763 if (data == NULL) { 24764 return (EINVAL); 24765 } 24766 24767 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24768 (un->un_state == SD_STATE_OFFLINE)) { 24769 return (ENXIO); 24770 } 24771 24772 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 24773 return (EFAULT); 24774 } 24775 24776 /* Validate the requested track and address format */ 24777 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 24778 return (EINVAL); 24779 } 24780 24781 if (entry->cdte_track == 0) { 24782 return (EINVAL); 24783 } 24784 24785 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 24786 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24787 bzero(cdb, CDB_GROUP1); 24788 24789 cdb[0] = SCMD_READ_TOC; 24790 /* Set the MSF bit based on the user requested address format */ 24791 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 24792 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24793 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 24794 } else { 24795 cdb[6] = entry->cdte_track; 24796 } 24797 24798 /* 24799 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 24800 * (4 byte TOC response header + 8 byte track descriptor) 24801 */ 24802 cdb[8] = 12; 24803 com->uscsi_cdb = cdb; 24804 com->uscsi_cdblen = CDB_GROUP1; 24805 com->uscsi_bufaddr = buffer; 24806 com->uscsi_buflen = 0x0C; 24807 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 24808 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24809 SD_PATH_STANDARD); 24810 if (rval != 0) { 24811 kmem_free(buffer, 12); 24812 kmem_free(com, sizeof (*com)); 24813 return (rval); 24814 } 24815 24816 /* Process the toc entry */ 24817 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 24818 entry->cdte_ctrl = (buffer[5] & 0x0F); 24819 if (entry->cdte_format & CDROM_LBA) { 24820 entry->cdte_addr.lba = 24821 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24822 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24823 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 24824 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 24825 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 24826 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 24827 /* 24828 * Send a READ TOC command using the LBA address format to get 24829 * the LBA for the track requested so it can be used in the 24830 * READ HEADER request 24831 * 24832 * Note: The MSF bit of the READ HEADER command specifies the 24833 * output format. The block address specified in that command 24834 * must be in LBA format. 24835 */ 24836 cdb[1] = 0; 24837 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24838 SD_PATH_STANDARD); 24839 if (rval != 0) { 24840 kmem_free(buffer, 12); 24841 kmem_free(com, sizeof (*com)); 24842 return (rval); 24843 } 24844 } else { 24845 entry->cdte_addr.msf.minute = buffer[9]; 24846 entry->cdte_addr.msf.second = buffer[10]; 24847 entry->cdte_addr.msf.frame = buffer[11]; 24848 /* 24849 * Send a READ TOC command using the LBA address format to get 24850 * the LBA for the track requested so it can be used in the 24851 * READ HEADER request 24852 * 24853 * Note: The MSF bit of the READ HEADER command specifies the 24854 * output format. The block address specified in that command 24855 * must be in LBA format. 24856 */ 24857 cdb[1] = 0; 24858 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24859 SD_PATH_STANDARD); 24860 if (rval != 0) { 24861 kmem_free(buffer, 12); 24862 kmem_free(com, sizeof (*com)); 24863 return (rval); 24864 } 24865 } 24866 24867 /* 24868 * Build and send the READ HEADER command to determine the data mode of 24869 * the user specified track. 24870 */ 24871 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 24872 (entry->cdte_track != CDROM_LEADOUT)) { 24873 bzero(cdb, CDB_GROUP1); 24874 cdb[0] = SCMD_READ_HEADER; 24875 cdb[2] = buffer[8]; 24876 cdb[3] = buffer[9]; 24877 cdb[4] = buffer[10]; 24878 cdb[5] = buffer[11]; 24879 cdb[8] = 0x08; 24880 com->uscsi_buflen = 0x08; 24881 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24882 SD_PATH_STANDARD); 24883 if (rval == 0) { 24884 entry->cdte_datamode = buffer[0]; 24885 } else { 24886 /* 24887 * READ HEADER command failed, since this is 24888 * obsoleted in one spec, its better to return 24889 * -1 for an invlid track so that we can still 24890 * receive the rest of the TOC data. 24891 */ 24892 entry->cdte_datamode = (uchar_t)-1; 24893 } 24894 } else { 24895 entry->cdte_datamode = (uchar_t)-1; 24896 } 24897 24898 kmem_free(buffer, 12); 24899 kmem_free(com, sizeof (*com)); 24900 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 24901 return (EFAULT); 24902 24903 return (rval); 24904 } 24905 24906 24907 /* 24908 * Function: sr_read_tochdr() 24909 * 24910 * Description: This routine is the driver entry point for handling CD-ROM 24911 * ioctl requests to read the Table of Contents (TOC) header 24912 * (CDROMREADTOHDR). The TOC header consists of the disk starting 24913 * and ending track numbers 24914 * 24915 * Arguments: dev - the device 'dev_t' 24916 * data - pointer to user provided toc header structure, 24917 * specifying the starting and ending track numbers. 24918 * flag - this argument is a pass through to ddi_copyxxx() 24919 * directly from the mode argument of ioctl(). 24920 * 24921 * Return Code: the code returned by sd_send_scsi_cmd() 24922 * EFAULT if ddi_copyxxx() fails 24923 * ENXIO if fail ddi_get_soft_state 24924 * EINVAL if data pointer is NULL 24925 */ 24926 24927 static int 24928 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 24929 { 24930 struct sd_lun *un; 24931 struct uscsi_cmd *com; 24932 struct cdrom_tochdr toc_header; 24933 struct cdrom_tochdr *hdr = &toc_header; 24934 char cdb[CDB_GROUP1]; 24935 int rval; 24936 caddr_t buffer; 24937 24938 if (data == NULL) { 24939 return (EINVAL); 24940 } 24941 24942 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24943 (un->un_state == SD_STATE_OFFLINE)) { 24944 return (ENXIO); 24945 } 24946 24947 buffer = kmem_zalloc(4, KM_SLEEP); 24948 bzero(cdb, CDB_GROUP1); 24949 cdb[0] = SCMD_READ_TOC; 24950 /* 24951 * Specifying a track number of 0x00 in the READ TOC command indicates 24952 * that the TOC header should be returned 24953 */ 24954 cdb[6] = 0x00; 24955 /* 24956 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 24957 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 24958 */ 24959 cdb[8] = 0x04; 24960 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24961 com->uscsi_cdb = cdb; 24962 com->uscsi_cdblen = CDB_GROUP1; 24963 com->uscsi_bufaddr = buffer; 24964 com->uscsi_buflen = 0x04; 24965 com->uscsi_timeout = 300; 24966 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24967 24968 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24969 SD_PATH_STANDARD); 24970 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24971 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 24972 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 24973 } else { 24974 hdr->cdth_trk0 = buffer[2]; 24975 hdr->cdth_trk1 = buffer[3]; 24976 } 24977 kmem_free(buffer, 4); 24978 kmem_free(com, sizeof (*com)); 24979 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 24980 return (EFAULT); 24981 } 24982 return (rval); 24983 } 24984 24985 24986 /* 24987 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 24988 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 24989 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 24990 * digital audio and extended architecture digital audio. These modes are 24991 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 24992 * MMC specs. 24993 * 24994 * In addition to support for the various data formats these routines also 24995 * include support for devices that implement only the direct access READ 24996 * commands (0x08, 0x28), devices that implement the READ_CD commands 24997 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 24998 * READ CDXA commands (0xD8, 0xDB) 24999 */ 25000 25001 /* 25002 * Function: sr_read_mode1() 25003 * 25004 * Description: This routine is the driver entry point for handling CD-ROM 25005 * ioctl read mode1 requests (CDROMREADMODE1). 25006 * 25007 * Arguments: dev - the device 'dev_t' 25008 * data - pointer to user provided cd read structure specifying 25009 * the lba buffer address and length. 25010 * flag - this argument is a pass through to ddi_copyxxx() 25011 * directly from the mode argument of ioctl(). 25012 * 25013 * Return Code: the code returned by sd_send_scsi_cmd() 25014 * EFAULT if ddi_copyxxx() fails 25015 * ENXIO if fail ddi_get_soft_state 25016 * EINVAL if data pointer is NULL 25017 */ 25018 25019 static int 25020 sr_read_mode1(dev_t dev, caddr_t data, int flag) 25021 { 25022 struct sd_lun *un; 25023 struct cdrom_read mode1_struct; 25024 struct cdrom_read *mode1 = &mode1_struct; 25025 int rval; 25026 #ifdef _MULTI_DATAMODEL 25027 /* To support ILP32 applications in an LP64 world */ 25028 struct cdrom_read32 cdrom_read32; 25029 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25030 #endif /* _MULTI_DATAMODEL */ 25031 25032 if (data == NULL) { 25033 return (EINVAL); 25034 } 25035 25036 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25037 (un->un_state == SD_STATE_OFFLINE)) { 25038 return (ENXIO); 25039 } 25040 25041 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25042 "sd_read_mode1: entry: un:0x%p\n", un); 25043 25044 #ifdef _MULTI_DATAMODEL 25045 switch (ddi_model_convert_from(flag & FMODELS)) { 25046 case DDI_MODEL_ILP32: 25047 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25048 return (EFAULT); 25049 } 25050 /* Convert the ILP32 uscsi data from the application to LP64 */ 25051 cdrom_read32tocdrom_read(cdrd32, mode1); 25052 break; 25053 case DDI_MODEL_NONE: 25054 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 25055 return (EFAULT); 25056 } 25057 } 25058 #else /* ! _MULTI_DATAMODEL */ 25059 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 25060 return (EFAULT); 25061 } 25062 #endif /* _MULTI_DATAMODEL */ 25063 25064 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 25065 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 25066 25067 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25068 "sd_read_mode1: exit: un:0x%p\n", un); 25069 25070 return (rval); 25071 } 25072 25073 25074 /* 25075 * Function: sr_read_cd_mode2() 25076 * 25077 * Description: This routine is the driver entry point for handling CD-ROM 25078 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 25079 * support the READ CD (0xBE) command or the 1st generation 25080 * READ CD (0xD4) command. 25081 * 25082 * Arguments: dev - the device 'dev_t' 25083 * data - pointer to user provided cd read structure specifying 25084 * the lba buffer address and length. 25085 * flag - this argument is a pass through to ddi_copyxxx() 25086 * directly from the mode argument of ioctl(). 25087 * 25088 * Return Code: the code returned by sd_send_scsi_cmd() 25089 * EFAULT if ddi_copyxxx() fails 25090 * ENXIO if fail ddi_get_soft_state 25091 * EINVAL if data pointer is NULL 25092 */ 25093 25094 static int 25095 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 25096 { 25097 struct sd_lun *un; 25098 struct uscsi_cmd *com; 25099 struct cdrom_read mode2_struct; 25100 struct cdrom_read *mode2 = &mode2_struct; 25101 uchar_t cdb[CDB_GROUP5]; 25102 int nblocks; 25103 int rval; 25104 #ifdef _MULTI_DATAMODEL 25105 /* To support ILP32 applications in an LP64 world */ 25106 struct cdrom_read32 cdrom_read32; 25107 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25108 #endif /* _MULTI_DATAMODEL */ 25109 25110 if (data == NULL) { 25111 return (EINVAL); 25112 } 25113 25114 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25115 (un->un_state == SD_STATE_OFFLINE)) { 25116 return (ENXIO); 25117 } 25118 25119 #ifdef _MULTI_DATAMODEL 25120 switch (ddi_model_convert_from(flag & FMODELS)) { 25121 case DDI_MODEL_ILP32: 25122 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25123 return (EFAULT); 25124 } 25125 /* Convert the ILP32 uscsi data from the application to LP64 */ 25126 cdrom_read32tocdrom_read(cdrd32, mode2); 25127 break; 25128 case DDI_MODEL_NONE: 25129 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25130 return (EFAULT); 25131 } 25132 break; 25133 } 25134 25135 #else /* ! _MULTI_DATAMODEL */ 25136 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25137 return (EFAULT); 25138 } 25139 #endif /* _MULTI_DATAMODEL */ 25140 25141 bzero(cdb, sizeof (cdb)); 25142 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 25143 /* Read command supported by 1st generation atapi drives */ 25144 cdb[0] = SCMD_READ_CDD4; 25145 } else { 25146 /* Universal CD Access Command */ 25147 cdb[0] = SCMD_READ_CD; 25148 } 25149 25150 /* 25151 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 25152 */ 25153 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 25154 25155 /* set the start address */ 25156 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 25157 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 25158 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 25159 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 25160 25161 /* set the transfer length */ 25162 nblocks = mode2->cdread_buflen / 2336; 25163 cdb[6] = (uchar_t)(nblocks >> 16); 25164 cdb[7] = (uchar_t)(nblocks >> 8); 25165 cdb[8] = (uchar_t)nblocks; 25166 25167 /* set the filter bits */ 25168 cdb[9] = CDROM_READ_CD_USERDATA; 25169 25170 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25171 com->uscsi_cdb = (caddr_t)cdb; 25172 com->uscsi_cdblen = sizeof (cdb); 25173 com->uscsi_bufaddr = mode2->cdread_bufaddr; 25174 com->uscsi_buflen = mode2->cdread_buflen; 25175 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25176 25177 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25178 SD_PATH_STANDARD); 25179 kmem_free(com, sizeof (*com)); 25180 return (rval); 25181 } 25182 25183 25184 /* 25185 * Function: sr_read_mode2() 25186 * 25187 * Description: This routine is the driver entry point for handling CD-ROM 25188 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 25189 * do not support the READ CD (0xBE) command. 25190 * 25191 * Arguments: dev - the device 'dev_t' 25192 * data - pointer to user provided cd read structure specifying 25193 * the lba buffer address and length. 25194 * flag - this argument is a pass through to ddi_copyxxx() 25195 * directly from the mode argument of ioctl(). 25196 * 25197 * Return Code: the code returned by sd_send_scsi_cmd() 25198 * EFAULT if ddi_copyxxx() fails 25199 * ENXIO if fail ddi_get_soft_state 25200 * EINVAL if data pointer is NULL 25201 * EIO if fail to reset block size 25202 * EAGAIN if commands are in progress in the driver 25203 */ 25204 25205 static int 25206 sr_read_mode2(dev_t dev, caddr_t data, int flag) 25207 { 25208 struct sd_lun *un; 25209 struct cdrom_read mode2_struct; 25210 struct cdrom_read *mode2 = &mode2_struct; 25211 int rval; 25212 uint32_t restore_blksize; 25213 struct uscsi_cmd *com; 25214 uchar_t cdb[CDB_GROUP0]; 25215 int nblocks; 25216 25217 #ifdef _MULTI_DATAMODEL 25218 /* To support ILP32 applications in an LP64 world */ 25219 struct cdrom_read32 cdrom_read32; 25220 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25221 #endif /* _MULTI_DATAMODEL */ 25222 25223 if (data == NULL) { 25224 return (EINVAL); 25225 } 25226 25227 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25228 (un->un_state == SD_STATE_OFFLINE)) { 25229 return (ENXIO); 25230 } 25231 25232 /* 25233 * Because this routine will update the device and driver block size 25234 * being used we want to make sure there are no commands in progress. 25235 * If commands are in progress the user will have to try again. 25236 * 25237 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 25238 * in sdioctl to protect commands from sdioctl through to the top of 25239 * sd_uscsi_strategy. See sdioctl for details. 25240 */ 25241 mutex_enter(SD_MUTEX(un)); 25242 if (un->un_ncmds_in_driver != 1) { 25243 mutex_exit(SD_MUTEX(un)); 25244 return (EAGAIN); 25245 } 25246 mutex_exit(SD_MUTEX(un)); 25247 25248 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25249 "sd_read_mode2: entry: un:0x%p\n", un); 25250 25251 #ifdef _MULTI_DATAMODEL 25252 switch (ddi_model_convert_from(flag & FMODELS)) { 25253 case DDI_MODEL_ILP32: 25254 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25255 return (EFAULT); 25256 } 25257 /* Convert the ILP32 uscsi data from the application to LP64 */ 25258 cdrom_read32tocdrom_read(cdrd32, mode2); 25259 break; 25260 case DDI_MODEL_NONE: 25261 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25262 return (EFAULT); 25263 } 25264 break; 25265 } 25266 #else /* ! _MULTI_DATAMODEL */ 25267 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 25268 return (EFAULT); 25269 } 25270 #endif /* _MULTI_DATAMODEL */ 25271 25272 /* Store the current target block size for restoration later */ 25273 restore_blksize = un->un_tgt_blocksize; 25274 25275 /* Change the device and soft state target block size to 2336 */ 25276 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 25277 rval = EIO; 25278 goto done; 25279 } 25280 25281 25282 bzero(cdb, sizeof (cdb)); 25283 25284 /* set READ operation */ 25285 cdb[0] = SCMD_READ; 25286 25287 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 25288 mode2->cdread_lba >>= 2; 25289 25290 /* set the start address */ 25291 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 25292 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 25293 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 25294 25295 /* set the transfer length */ 25296 nblocks = mode2->cdread_buflen / 2336; 25297 cdb[4] = (uchar_t)nblocks & 0xFF; 25298 25299 /* build command */ 25300 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25301 com->uscsi_cdb = (caddr_t)cdb; 25302 com->uscsi_cdblen = sizeof (cdb); 25303 com->uscsi_bufaddr = mode2->cdread_bufaddr; 25304 com->uscsi_buflen = mode2->cdread_buflen; 25305 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25306 25307 /* 25308 * Issue SCSI command with user space address for read buffer. 25309 * 25310 * This sends the command through main channel in the driver. 25311 * 25312 * Since this is accessed via an IOCTL call, we go through the 25313 * standard path, so that if the device was powered down, then 25314 * it would be 'awakened' to handle the command. 25315 */ 25316 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25317 SD_PATH_STANDARD); 25318 25319 kmem_free(com, sizeof (*com)); 25320 25321 /* Restore the device and soft state target block size */ 25322 if (sr_sector_mode(dev, restore_blksize) != 0) { 25323 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25324 "can't do switch back to mode 1\n"); 25325 /* 25326 * If sd_send_scsi_READ succeeded we still need to report 25327 * an error because we failed to reset the block size 25328 */ 25329 if (rval == 0) { 25330 rval = EIO; 25331 } 25332 } 25333 25334 done: 25335 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25336 "sd_read_mode2: exit: un:0x%p\n", un); 25337 25338 return (rval); 25339 } 25340 25341 25342 /* 25343 * Function: sr_sector_mode() 25344 * 25345 * Description: This utility function is used by sr_read_mode2 to set the target 25346 * block size based on the user specified size. This is a legacy 25347 * implementation based upon a vendor specific mode page 25348 * 25349 * Arguments: dev - the device 'dev_t' 25350 * data - flag indicating if block size is being set to 2336 or 25351 * 512. 25352 * 25353 * Return Code: the code returned by sd_send_scsi_cmd() 25354 * EFAULT if ddi_copyxxx() fails 25355 * ENXIO if fail ddi_get_soft_state 25356 * EINVAL if data pointer is NULL 25357 */ 25358 25359 static int 25360 sr_sector_mode(dev_t dev, uint32_t blksize) 25361 { 25362 struct sd_lun *un; 25363 uchar_t *sense; 25364 uchar_t *select; 25365 int rval; 25366 25367 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25368 (un->un_state == SD_STATE_OFFLINE)) { 25369 return (ENXIO); 25370 } 25371 25372 sense = kmem_zalloc(20, KM_SLEEP); 25373 25374 /* Note: This is a vendor specific mode page (0x81) */ 25375 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 25376 SD_PATH_STANDARD)) != 0) { 25377 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25378 "sr_sector_mode: Mode Sense failed\n"); 25379 kmem_free(sense, 20); 25380 return (rval); 25381 } 25382 select = kmem_zalloc(20, KM_SLEEP); 25383 select[3] = 0x08; 25384 select[10] = ((blksize >> 8) & 0xff); 25385 select[11] = (blksize & 0xff); 25386 select[12] = 0x01; 25387 select[13] = 0x06; 25388 select[14] = sense[14]; 25389 select[15] = sense[15]; 25390 if (blksize == SD_MODE2_BLKSIZE) { 25391 select[14] |= 0x01; 25392 } 25393 25394 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 25395 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 25396 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25397 "sr_sector_mode: Mode Select failed\n"); 25398 } else { 25399 /* 25400 * Only update the softstate block size if we successfully 25401 * changed the device block mode. 25402 */ 25403 mutex_enter(SD_MUTEX(un)); 25404 sd_update_block_info(un, blksize, 0); 25405 mutex_exit(SD_MUTEX(un)); 25406 } 25407 kmem_free(sense, 20); 25408 kmem_free(select, 20); 25409 return (rval); 25410 } 25411 25412 25413 /* 25414 * Function: sr_read_cdda() 25415 * 25416 * Description: This routine is the driver entry point for handling CD-ROM 25417 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 25418 * the target supports CDDA these requests are handled via a vendor 25419 * specific command (0xD8) If the target does not support CDDA 25420 * these requests are handled via the READ CD command (0xBE). 25421 * 25422 * Arguments: dev - the device 'dev_t' 25423 * data - pointer to user provided CD-DA structure specifying 25424 * the track starting address, transfer length, and 25425 * subcode options. 25426 * flag - this argument is a pass through to ddi_copyxxx() 25427 * directly from the mode argument of ioctl(). 25428 * 25429 * Return Code: the code returned by sd_send_scsi_cmd() 25430 * EFAULT if ddi_copyxxx() fails 25431 * ENXIO if fail ddi_get_soft_state 25432 * EINVAL if invalid arguments are provided 25433 * ENOTTY 25434 */ 25435 25436 static int 25437 sr_read_cdda(dev_t dev, caddr_t data, int flag) 25438 { 25439 struct sd_lun *un; 25440 struct uscsi_cmd *com; 25441 struct cdrom_cdda *cdda; 25442 int rval; 25443 size_t buflen; 25444 char cdb[CDB_GROUP5]; 25445 25446 #ifdef _MULTI_DATAMODEL 25447 /* To support ILP32 applications in an LP64 world */ 25448 struct cdrom_cdda32 cdrom_cdda32; 25449 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 25450 #endif /* _MULTI_DATAMODEL */ 25451 25452 if (data == NULL) { 25453 return (EINVAL); 25454 } 25455 25456 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25457 return (ENXIO); 25458 } 25459 25460 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 25461 25462 #ifdef _MULTI_DATAMODEL 25463 switch (ddi_model_convert_from(flag & FMODELS)) { 25464 case DDI_MODEL_ILP32: 25465 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 25466 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25467 "sr_read_cdda: ddi_copyin Failed\n"); 25468 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25469 return (EFAULT); 25470 } 25471 /* Convert the ILP32 uscsi data from the application to LP64 */ 25472 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 25473 break; 25474 case DDI_MODEL_NONE: 25475 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25476 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25477 "sr_read_cdda: ddi_copyin Failed\n"); 25478 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25479 return (EFAULT); 25480 } 25481 break; 25482 } 25483 #else /* ! _MULTI_DATAMODEL */ 25484 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25485 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25486 "sr_read_cdda: ddi_copyin Failed\n"); 25487 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25488 return (EFAULT); 25489 } 25490 #endif /* _MULTI_DATAMODEL */ 25491 25492 /* 25493 * Since MMC-2 expects max 3 bytes for length, check if the 25494 * length input is greater than 3 bytes 25495 */ 25496 if ((cdda->cdda_length & 0xFF000000) != 0) { 25497 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 25498 "cdrom transfer length too large: %d (limit %d)\n", 25499 cdda->cdda_length, 0xFFFFFF); 25500 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25501 return (EINVAL); 25502 } 25503 25504 switch (cdda->cdda_subcode) { 25505 case CDROM_DA_NO_SUBCODE: 25506 buflen = CDROM_BLK_2352 * cdda->cdda_length; 25507 break; 25508 case CDROM_DA_SUBQ: 25509 buflen = CDROM_BLK_2368 * cdda->cdda_length; 25510 break; 25511 case CDROM_DA_ALL_SUBCODE: 25512 buflen = CDROM_BLK_2448 * cdda->cdda_length; 25513 break; 25514 case CDROM_DA_SUBCODE_ONLY: 25515 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 25516 break; 25517 default: 25518 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25519 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 25520 cdda->cdda_subcode); 25521 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25522 return (EINVAL); 25523 } 25524 25525 /* Build and send the command */ 25526 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25527 bzero(cdb, CDB_GROUP5); 25528 25529 if (un->un_f_cfg_cdda == TRUE) { 25530 cdb[0] = (char)SCMD_READ_CD; 25531 cdb[1] = 0x04; 25532 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25533 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25534 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25535 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25536 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25537 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25538 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 25539 cdb[9] = 0x10; 25540 switch (cdda->cdda_subcode) { 25541 case CDROM_DA_NO_SUBCODE : 25542 cdb[10] = 0x0; 25543 break; 25544 case CDROM_DA_SUBQ : 25545 cdb[10] = 0x2; 25546 break; 25547 case CDROM_DA_ALL_SUBCODE : 25548 cdb[10] = 0x1; 25549 break; 25550 case CDROM_DA_SUBCODE_ONLY : 25551 /* FALLTHROUGH */ 25552 default : 25553 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25554 kmem_free(com, sizeof (*com)); 25555 return (ENOTTY); 25556 } 25557 } else { 25558 cdb[0] = (char)SCMD_READ_CDDA; 25559 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25560 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25561 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25562 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25563 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 25564 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25565 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25566 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 25567 cdb[10] = cdda->cdda_subcode; 25568 } 25569 25570 com->uscsi_cdb = cdb; 25571 com->uscsi_cdblen = CDB_GROUP5; 25572 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 25573 com->uscsi_buflen = buflen; 25574 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25575 25576 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25577 SD_PATH_STANDARD); 25578 25579 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25580 kmem_free(com, sizeof (*com)); 25581 return (rval); 25582 } 25583 25584 25585 /* 25586 * Function: sr_read_cdxa() 25587 * 25588 * Description: This routine is the driver entry point for handling CD-ROM 25589 * ioctl requests to return CD-XA (Extended Architecture) data. 25590 * (CDROMCDXA). 25591 * 25592 * Arguments: dev - the device 'dev_t' 25593 * data - pointer to user provided CD-XA structure specifying 25594 * the data starting address, transfer length, and format 25595 * flag - this argument is a pass through to ddi_copyxxx() 25596 * directly from the mode argument of ioctl(). 25597 * 25598 * Return Code: the code returned by sd_send_scsi_cmd() 25599 * EFAULT if ddi_copyxxx() fails 25600 * ENXIO if fail ddi_get_soft_state 25601 * EINVAL if data pointer is NULL 25602 */ 25603 25604 static int 25605 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 25606 { 25607 struct sd_lun *un; 25608 struct uscsi_cmd *com; 25609 struct cdrom_cdxa *cdxa; 25610 int rval; 25611 size_t buflen; 25612 char cdb[CDB_GROUP5]; 25613 uchar_t read_flags; 25614 25615 #ifdef _MULTI_DATAMODEL 25616 /* To support ILP32 applications in an LP64 world */ 25617 struct cdrom_cdxa32 cdrom_cdxa32; 25618 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 25619 #endif /* _MULTI_DATAMODEL */ 25620 25621 if (data == NULL) { 25622 return (EINVAL); 25623 } 25624 25625 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25626 return (ENXIO); 25627 } 25628 25629 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 25630 25631 #ifdef _MULTI_DATAMODEL 25632 switch (ddi_model_convert_from(flag & FMODELS)) { 25633 case DDI_MODEL_ILP32: 25634 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 25635 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25636 return (EFAULT); 25637 } 25638 /* 25639 * Convert the ILP32 uscsi data from the 25640 * application to LP64 for internal use. 25641 */ 25642 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 25643 break; 25644 case DDI_MODEL_NONE: 25645 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25646 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25647 return (EFAULT); 25648 } 25649 break; 25650 } 25651 #else /* ! _MULTI_DATAMODEL */ 25652 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25653 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25654 return (EFAULT); 25655 } 25656 #endif /* _MULTI_DATAMODEL */ 25657 25658 /* 25659 * Since MMC-2 expects max 3 bytes for length, check if the 25660 * length input is greater than 3 bytes 25661 */ 25662 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 25663 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 25664 "cdrom transfer length too large: %d (limit %d)\n", 25665 cdxa->cdxa_length, 0xFFFFFF); 25666 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25667 return (EINVAL); 25668 } 25669 25670 switch (cdxa->cdxa_format) { 25671 case CDROM_XA_DATA: 25672 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 25673 read_flags = 0x10; 25674 break; 25675 case CDROM_XA_SECTOR_DATA: 25676 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 25677 read_flags = 0xf8; 25678 break; 25679 case CDROM_XA_DATA_W_ERROR: 25680 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 25681 read_flags = 0xfc; 25682 break; 25683 default: 25684 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25685 "sr_read_cdxa: Format '0x%x' Not Supported\n", 25686 cdxa->cdxa_format); 25687 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25688 return (EINVAL); 25689 } 25690 25691 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25692 bzero(cdb, CDB_GROUP5); 25693 if (un->un_f_mmc_cap == TRUE) { 25694 cdb[0] = (char)SCMD_READ_CD; 25695 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25696 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25697 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25698 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25699 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25700 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25701 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 25702 cdb[9] = (char)read_flags; 25703 } else { 25704 /* 25705 * Note: A vendor specific command (0xDB) is being used her to 25706 * request a read of all subcodes. 25707 */ 25708 cdb[0] = (char)SCMD_READ_CDXA; 25709 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25710 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25711 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25712 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25713 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 25714 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25715 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25716 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 25717 cdb[10] = cdxa->cdxa_format; 25718 } 25719 com->uscsi_cdb = cdb; 25720 com->uscsi_cdblen = CDB_GROUP5; 25721 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 25722 com->uscsi_buflen = buflen; 25723 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25724 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25725 SD_PATH_STANDARD); 25726 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25727 kmem_free(com, sizeof (*com)); 25728 return (rval); 25729 } 25730 25731 25732 /* 25733 * Function: sr_eject() 25734 * 25735 * Description: This routine is the driver entry point for handling CD-ROM 25736 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 25737 * 25738 * Arguments: dev - the device 'dev_t' 25739 * 25740 * Return Code: the code returned by sd_send_scsi_cmd() 25741 */ 25742 25743 static int 25744 sr_eject(dev_t dev) 25745 { 25746 struct sd_lun *un; 25747 int rval; 25748 25749 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25750 (un->un_state == SD_STATE_OFFLINE)) { 25751 return (ENXIO); 25752 } 25753 25754 /* 25755 * To prevent race conditions with the eject 25756 * command, keep track of an eject command as 25757 * it progresses. If we are already handling 25758 * an eject command in the driver for the given 25759 * unit and another request to eject is received 25760 * immediately return EAGAIN so we don't lose 25761 * the command if the current eject command fails. 25762 */ 25763 mutex_enter(SD_MUTEX(un)); 25764 if (un->un_f_ejecting == TRUE) { 25765 mutex_exit(SD_MUTEX(un)); 25766 return (EAGAIN); 25767 } 25768 un->un_f_ejecting = TRUE; 25769 mutex_exit(SD_MUTEX(un)); 25770 25771 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 25772 SD_PATH_STANDARD)) != 0) { 25773 mutex_enter(SD_MUTEX(un)); 25774 un->un_f_ejecting = FALSE; 25775 mutex_exit(SD_MUTEX(un)); 25776 return (rval); 25777 } 25778 25779 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 25780 SD_PATH_STANDARD); 25781 25782 if (rval == 0) { 25783 mutex_enter(SD_MUTEX(un)); 25784 sr_ejected(un); 25785 un->un_mediastate = DKIO_EJECTED; 25786 un->un_f_ejecting = FALSE; 25787 cv_broadcast(&un->un_state_cv); 25788 mutex_exit(SD_MUTEX(un)); 25789 } else { 25790 mutex_enter(SD_MUTEX(un)); 25791 un->un_f_ejecting = FALSE; 25792 mutex_exit(SD_MUTEX(un)); 25793 } 25794 return (rval); 25795 } 25796 25797 25798 /* 25799 * Function: sr_ejected() 25800 * 25801 * Description: This routine updates the soft state structure to invalidate the 25802 * geometry information after the media has been ejected or a 25803 * media eject has been detected. 25804 * 25805 * Arguments: un - driver soft state (unit) structure 25806 */ 25807 25808 static void 25809 sr_ejected(struct sd_lun *un) 25810 { 25811 struct sd_errstats *stp; 25812 25813 ASSERT(un != NULL); 25814 ASSERT(mutex_owned(SD_MUTEX(un))); 25815 25816 un->un_f_blockcount_is_valid = FALSE; 25817 un->un_f_tgt_blocksize_is_valid = FALSE; 25818 mutex_exit(SD_MUTEX(un)); 25819 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 25820 mutex_enter(SD_MUTEX(un)); 25821 25822 if (un->un_errstats != NULL) { 25823 stp = (struct sd_errstats *)un->un_errstats->ks_data; 25824 stp->sd_capacity.value.ui64 = 0; 25825 } 25826 25827 /* remove "capacity-of-device" properties */ 25828 (void) ddi_prop_remove(DDI_DEV_T_NONE, SD_DEVINFO(un), 25829 "device-nblocks"); 25830 (void) ddi_prop_remove(DDI_DEV_T_NONE, SD_DEVINFO(un), 25831 "device-blksize"); 25832 } 25833 25834 25835 /* 25836 * Function: sr_check_wp() 25837 * 25838 * Description: This routine checks the write protection of a removable 25839 * media disk and hotpluggable devices via the write protect bit of 25840 * the Mode Page Header device specific field. Some devices choke 25841 * on unsupported mode page. In order to workaround this issue, 25842 * this routine has been implemented to use 0x3f mode page(request 25843 * for all pages) for all device types. 25844 * 25845 * Arguments: dev - the device 'dev_t' 25846 * 25847 * Return Code: int indicating if the device is write protected (1) or not (0) 25848 * 25849 * Context: Kernel thread. 25850 * 25851 */ 25852 25853 static int 25854 sr_check_wp(dev_t dev) 25855 { 25856 struct sd_lun *un; 25857 uchar_t device_specific; 25858 uchar_t *sense; 25859 int hdrlen; 25860 int rval = FALSE; 25861 25862 /* 25863 * Note: The return codes for this routine should be reworked to 25864 * properly handle the case of a NULL softstate. 25865 */ 25866 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25867 return (FALSE); 25868 } 25869 25870 if (un->un_f_cfg_is_atapi == TRUE) { 25871 /* 25872 * The mode page contents are not required; set the allocation 25873 * length for the mode page header only 25874 */ 25875 hdrlen = MODE_HEADER_LENGTH_GRP2; 25876 sense = kmem_zalloc(hdrlen, KM_SLEEP); 25877 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 25878 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 25879 goto err_exit; 25880 device_specific = 25881 ((struct mode_header_grp2 *)sense)->device_specific; 25882 } else { 25883 hdrlen = MODE_HEADER_LENGTH; 25884 sense = kmem_zalloc(hdrlen, KM_SLEEP); 25885 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 25886 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 25887 goto err_exit; 25888 device_specific = 25889 ((struct mode_header *)sense)->device_specific; 25890 } 25891 25892 /* 25893 * Write protect mode sense failed; not all disks 25894 * understand this query. Return FALSE assuming that 25895 * these devices are not writable. 25896 */ 25897 if (device_specific & WRITE_PROTECT) { 25898 rval = TRUE; 25899 } 25900 25901 err_exit: 25902 kmem_free(sense, hdrlen); 25903 return (rval); 25904 } 25905 25906 /* 25907 * Function: sr_volume_ctrl() 25908 * 25909 * Description: This routine is the driver entry point for handling CD-ROM 25910 * audio output volume ioctl requests. (CDROMVOLCTRL) 25911 * 25912 * Arguments: dev - the device 'dev_t' 25913 * data - pointer to user audio volume control structure 25914 * flag - this argument is a pass through to ddi_copyxxx() 25915 * directly from the mode argument of ioctl(). 25916 * 25917 * Return Code: the code returned by sd_send_scsi_cmd() 25918 * EFAULT if ddi_copyxxx() fails 25919 * ENXIO if fail ddi_get_soft_state 25920 * EINVAL if data pointer is NULL 25921 * 25922 */ 25923 25924 static int 25925 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 25926 { 25927 struct sd_lun *un; 25928 struct cdrom_volctrl volume; 25929 struct cdrom_volctrl *vol = &volume; 25930 uchar_t *sense_page; 25931 uchar_t *select_page; 25932 uchar_t *sense; 25933 uchar_t *select; 25934 int sense_buflen; 25935 int select_buflen; 25936 int rval; 25937 25938 if (data == NULL) { 25939 return (EINVAL); 25940 } 25941 25942 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25943 (un->un_state == SD_STATE_OFFLINE)) { 25944 return (ENXIO); 25945 } 25946 25947 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 25948 return (EFAULT); 25949 } 25950 25951 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 25952 struct mode_header_grp2 *sense_mhp; 25953 struct mode_header_grp2 *select_mhp; 25954 int bd_len; 25955 25956 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 25957 select_buflen = MODE_HEADER_LENGTH_GRP2 + 25958 MODEPAGE_AUDIO_CTRL_LEN; 25959 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 25960 select = kmem_zalloc(select_buflen, KM_SLEEP); 25961 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 25962 sense_buflen, MODEPAGE_AUDIO_CTRL, 25963 SD_PATH_STANDARD)) != 0) { 25964 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25965 "sr_volume_ctrl: Mode Sense Failed\n"); 25966 kmem_free(sense, sense_buflen); 25967 kmem_free(select, select_buflen); 25968 return (rval); 25969 } 25970 sense_mhp = (struct mode_header_grp2 *)sense; 25971 select_mhp = (struct mode_header_grp2 *)select; 25972 bd_len = (sense_mhp->bdesc_length_hi << 8) | 25973 sense_mhp->bdesc_length_lo; 25974 if (bd_len > MODE_BLK_DESC_LENGTH) { 25975 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25976 "sr_volume_ctrl: Mode Sense returned invalid " 25977 "block descriptor length\n"); 25978 kmem_free(sense, sense_buflen); 25979 kmem_free(select, select_buflen); 25980 return (EIO); 25981 } 25982 sense_page = (uchar_t *) 25983 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 25984 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 25985 select_mhp->length_msb = 0; 25986 select_mhp->length_lsb = 0; 25987 select_mhp->bdesc_length_hi = 0; 25988 select_mhp->bdesc_length_lo = 0; 25989 } else { 25990 struct mode_header *sense_mhp, *select_mhp; 25991 25992 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 25993 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 25994 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 25995 select = kmem_zalloc(select_buflen, KM_SLEEP); 25996 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 25997 sense_buflen, MODEPAGE_AUDIO_CTRL, 25998 SD_PATH_STANDARD)) != 0) { 25999 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26000 "sr_volume_ctrl: Mode Sense Failed\n"); 26001 kmem_free(sense, sense_buflen); 26002 kmem_free(select, select_buflen); 26003 return (rval); 26004 } 26005 sense_mhp = (struct mode_header *)sense; 26006 select_mhp = (struct mode_header *)select; 26007 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 26008 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26009 "sr_volume_ctrl: Mode Sense returned invalid " 26010 "block descriptor length\n"); 26011 kmem_free(sense, sense_buflen); 26012 kmem_free(select, select_buflen); 26013 return (EIO); 26014 } 26015 sense_page = (uchar_t *) 26016 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 26017 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 26018 select_mhp->length = 0; 26019 select_mhp->bdesc_length = 0; 26020 } 26021 /* 26022 * Note: An audio control data structure could be created and overlayed 26023 * on the following in place of the array indexing method implemented. 26024 */ 26025 26026 /* Build the select data for the user volume data */ 26027 select_page[0] = MODEPAGE_AUDIO_CTRL; 26028 select_page[1] = 0xE; 26029 /* Set the immediate bit */ 26030 select_page[2] = 0x04; 26031 /* Zero out reserved fields */ 26032 select_page[3] = 0x00; 26033 select_page[4] = 0x00; 26034 /* Return sense data for fields not to be modified */ 26035 select_page[5] = sense_page[5]; 26036 select_page[6] = sense_page[6]; 26037 select_page[7] = sense_page[7]; 26038 /* Set the user specified volume levels for channel 0 and 1 */ 26039 select_page[8] = 0x01; 26040 select_page[9] = vol->channel0; 26041 select_page[10] = 0x02; 26042 select_page[11] = vol->channel1; 26043 /* Channel 2 and 3 are currently unsupported so return the sense data */ 26044 select_page[12] = sense_page[12]; 26045 select_page[13] = sense_page[13]; 26046 select_page[14] = sense_page[14]; 26047 select_page[15] = sense_page[15]; 26048 26049 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 26050 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 26051 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26052 } else { 26053 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 26054 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26055 } 26056 26057 kmem_free(sense, sense_buflen); 26058 kmem_free(select, select_buflen); 26059 return (rval); 26060 } 26061 26062 26063 /* 26064 * Function: sr_read_sony_session_offset() 26065 * 26066 * Description: This routine is the driver entry point for handling CD-ROM 26067 * ioctl requests for session offset information. (CDROMREADOFFSET) 26068 * The address of the first track in the last session of a 26069 * multi-session CD-ROM is returned 26070 * 26071 * Note: This routine uses a vendor specific key value in the 26072 * command control field without implementing any vendor check here 26073 * or in the ioctl routine. 26074 * 26075 * Arguments: dev - the device 'dev_t' 26076 * data - pointer to an int to hold the requested address 26077 * flag - this argument is a pass through to ddi_copyxxx() 26078 * directly from the mode argument of ioctl(). 26079 * 26080 * Return Code: the code returned by sd_send_scsi_cmd() 26081 * EFAULT if ddi_copyxxx() fails 26082 * ENXIO if fail ddi_get_soft_state 26083 * EINVAL if data pointer is NULL 26084 */ 26085 26086 static int 26087 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 26088 { 26089 struct sd_lun *un; 26090 struct uscsi_cmd *com; 26091 caddr_t buffer; 26092 char cdb[CDB_GROUP1]; 26093 int session_offset = 0; 26094 int rval; 26095 26096 if (data == NULL) { 26097 return (EINVAL); 26098 } 26099 26100 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26101 (un->un_state == SD_STATE_OFFLINE)) { 26102 return (ENXIO); 26103 } 26104 26105 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 26106 bzero(cdb, CDB_GROUP1); 26107 cdb[0] = SCMD_READ_TOC; 26108 /* 26109 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 26110 * (4 byte TOC response header + 8 byte response data) 26111 */ 26112 cdb[8] = SONY_SESSION_OFFSET_LEN; 26113 /* Byte 9 is the control byte. A vendor specific value is used */ 26114 cdb[9] = SONY_SESSION_OFFSET_KEY; 26115 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26116 com->uscsi_cdb = cdb; 26117 com->uscsi_cdblen = CDB_GROUP1; 26118 com->uscsi_bufaddr = buffer; 26119 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 26120 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 26121 26122 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26123 SD_PATH_STANDARD); 26124 if (rval != 0) { 26125 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 26126 kmem_free(com, sizeof (*com)); 26127 return (rval); 26128 } 26129 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 26130 session_offset = 26131 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 26132 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 26133 /* 26134 * Offset returned offset in current lbasize block's. Convert to 26135 * 2k block's to return to the user 26136 */ 26137 if (un->un_tgt_blocksize == CDROM_BLK_512) { 26138 session_offset >>= 2; 26139 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 26140 session_offset >>= 1; 26141 } 26142 } 26143 26144 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 26145 rval = EFAULT; 26146 } 26147 26148 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 26149 kmem_free(com, sizeof (*com)); 26150 return (rval); 26151 } 26152 26153 26154 /* 26155 * Function: sd_wm_cache_constructor() 26156 * 26157 * Description: Cache Constructor for the wmap cache for the read/modify/write 26158 * devices. 26159 * 26160 * Arguments: wm - A pointer to the sd_w_map to be initialized. 26161 * un - sd_lun structure for the device. 26162 * flag - the km flags passed to constructor 26163 * 26164 * Return Code: 0 on success. 26165 * -1 on failure. 26166 */ 26167 26168 /*ARGSUSED*/ 26169 static int 26170 sd_wm_cache_constructor(void *wm, void *un, int flags) 26171 { 26172 bzero(wm, sizeof (struct sd_w_map)); 26173 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 26174 return (0); 26175 } 26176 26177 26178 /* 26179 * Function: sd_wm_cache_destructor() 26180 * 26181 * Description: Cache destructor for the wmap cache for the read/modify/write 26182 * devices. 26183 * 26184 * Arguments: wm - A pointer to the sd_w_map to be initialized. 26185 * un - sd_lun structure for the device. 26186 */ 26187 /*ARGSUSED*/ 26188 static void 26189 sd_wm_cache_destructor(void *wm, void *un) 26190 { 26191 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 26192 } 26193 26194 26195 /* 26196 * Function: sd_range_lock() 26197 * 26198 * Description: Lock the range of blocks specified as parameter to ensure 26199 * that read, modify write is atomic and no other i/o writes 26200 * to the same location. The range is specified in terms 26201 * of start and end blocks. Block numbers are the actual 26202 * media block numbers and not system. 26203 * 26204 * Arguments: un - sd_lun structure for the device. 26205 * startb - The starting block number 26206 * endb - The end block number 26207 * typ - type of i/o - simple/read_modify_write 26208 * 26209 * Return Code: wm - pointer to the wmap structure. 26210 * 26211 * Context: This routine can sleep. 26212 */ 26213 26214 static struct sd_w_map * 26215 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 26216 { 26217 struct sd_w_map *wmp = NULL; 26218 struct sd_w_map *sl_wmp = NULL; 26219 struct sd_w_map *tmp_wmp; 26220 wm_state state = SD_WM_CHK_LIST; 26221 26222 26223 ASSERT(un != NULL); 26224 ASSERT(!mutex_owned(SD_MUTEX(un))); 26225 26226 mutex_enter(SD_MUTEX(un)); 26227 26228 while (state != SD_WM_DONE) { 26229 26230 switch (state) { 26231 case SD_WM_CHK_LIST: 26232 /* 26233 * This is the starting state. Check the wmap list 26234 * to see if the range is currently available. 26235 */ 26236 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 26237 /* 26238 * If this is a simple write and no rmw 26239 * i/o is pending then try to lock the 26240 * range as the range should be available. 26241 */ 26242 state = SD_WM_LOCK_RANGE; 26243 } else { 26244 tmp_wmp = sd_get_range(un, startb, endb); 26245 if (tmp_wmp != NULL) { 26246 if ((wmp != NULL) && ONLIST(un, wmp)) { 26247 /* 26248 * Should not keep onlist wmps 26249 * while waiting this macro 26250 * will also do wmp = NULL; 26251 */ 26252 FREE_ONLIST_WMAP(un, wmp); 26253 } 26254 /* 26255 * sl_wmp is the wmap on which wait 26256 * is done, since the tmp_wmp points 26257 * to the inuse wmap, set sl_wmp to 26258 * tmp_wmp and change the state to sleep 26259 */ 26260 sl_wmp = tmp_wmp; 26261 state = SD_WM_WAIT_MAP; 26262 } else { 26263 state = SD_WM_LOCK_RANGE; 26264 } 26265 26266 } 26267 break; 26268 26269 case SD_WM_LOCK_RANGE: 26270 ASSERT(un->un_wm_cache); 26271 /* 26272 * The range need to be locked, try to get a wmap. 26273 * First attempt it with NO_SLEEP, want to avoid a sleep 26274 * if possible as we will have to release the sd mutex 26275 * if we have to sleep. 26276 */ 26277 if (wmp == NULL) 26278 wmp = kmem_cache_alloc(un->un_wm_cache, 26279 KM_NOSLEEP); 26280 if (wmp == NULL) { 26281 mutex_exit(SD_MUTEX(un)); 26282 _NOTE(DATA_READABLE_WITHOUT_LOCK 26283 (sd_lun::un_wm_cache)) 26284 wmp = kmem_cache_alloc(un->un_wm_cache, 26285 KM_SLEEP); 26286 mutex_enter(SD_MUTEX(un)); 26287 /* 26288 * we released the mutex so recheck and go to 26289 * check list state. 26290 */ 26291 state = SD_WM_CHK_LIST; 26292 } else { 26293 /* 26294 * We exit out of state machine since we 26295 * have the wmap. Do the housekeeping first. 26296 * place the wmap on the wmap list if it is not 26297 * on it already and then set the state to done. 26298 */ 26299 wmp->wm_start = startb; 26300 wmp->wm_end = endb; 26301 wmp->wm_flags = typ | SD_WM_BUSY; 26302 if (typ & SD_WTYPE_RMW) { 26303 un->un_rmw_count++; 26304 } 26305 /* 26306 * If not already on the list then link 26307 */ 26308 if (!ONLIST(un, wmp)) { 26309 wmp->wm_next = un->un_wm; 26310 wmp->wm_prev = NULL; 26311 if (wmp->wm_next) 26312 wmp->wm_next->wm_prev = wmp; 26313 un->un_wm = wmp; 26314 } 26315 state = SD_WM_DONE; 26316 } 26317 break; 26318 26319 case SD_WM_WAIT_MAP: 26320 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 26321 /* 26322 * Wait is done on sl_wmp, which is set in the 26323 * check_list state. 26324 */ 26325 sl_wmp->wm_wanted_count++; 26326 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 26327 sl_wmp->wm_wanted_count--; 26328 /* 26329 * We can reuse the memory from the completed sl_wmp 26330 * lock range for our new lock, but only if noone is 26331 * waiting for it. 26332 */ 26333 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 26334 if (sl_wmp->wm_wanted_count == 0) { 26335 if (wmp != NULL) 26336 CHK_N_FREEWMP(un, wmp); 26337 wmp = sl_wmp; 26338 } 26339 sl_wmp = NULL; 26340 /* 26341 * After waking up, need to recheck for availability of 26342 * range. 26343 */ 26344 state = SD_WM_CHK_LIST; 26345 break; 26346 26347 default: 26348 panic("sd_range_lock: " 26349 "Unknown state %d in sd_range_lock", state); 26350 /*NOTREACHED*/ 26351 } /* switch(state) */ 26352 26353 } /* while(state != SD_WM_DONE) */ 26354 26355 mutex_exit(SD_MUTEX(un)); 26356 26357 ASSERT(wmp != NULL); 26358 26359 return (wmp); 26360 } 26361 26362 26363 /* 26364 * Function: sd_get_range() 26365 * 26366 * Description: Find if there any overlapping I/O to this one 26367 * Returns the write-map of 1st such I/O, NULL otherwise. 26368 * 26369 * Arguments: un - sd_lun structure for the device. 26370 * startb - The starting block number 26371 * endb - The end block number 26372 * 26373 * Return Code: wm - pointer to the wmap structure. 26374 */ 26375 26376 static struct sd_w_map * 26377 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 26378 { 26379 struct sd_w_map *wmp; 26380 26381 ASSERT(un != NULL); 26382 26383 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 26384 if (!(wmp->wm_flags & SD_WM_BUSY)) { 26385 continue; 26386 } 26387 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 26388 break; 26389 } 26390 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 26391 break; 26392 } 26393 } 26394 26395 return (wmp); 26396 } 26397 26398 26399 /* 26400 * Function: sd_free_inlist_wmap() 26401 * 26402 * Description: Unlink and free a write map struct. 26403 * 26404 * Arguments: un - sd_lun structure for the device. 26405 * wmp - sd_w_map which needs to be unlinked. 26406 */ 26407 26408 static void 26409 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 26410 { 26411 ASSERT(un != NULL); 26412 26413 if (un->un_wm == wmp) { 26414 un->un_wm = wmp->wm_next; 26415 } else { 26416 wmp->wm_prev->wm_next = wmp->wm_next; 26417 } 26418 26419 if (wmp->wm_next) { 26420 wmp->wm_next->wm_prev = wmp->wm_prev; 26421 } 26422 26423 wmp->wm_next = wmp->wm_prev = NULL; 26424 26425 kmem_cache_free(un->un_wm_cache, wmp); 26426 } 26427 26428 26429 /* 26430 * Function: sd_range_unlock() 26431 * 26432 * Description: Unlock the range locked by wm. 26433 * Free write map if nobody else is waiting on it. 26434 * 26435 * Arguments: un - sd_lun structure for the device. 26436 * wmp - sd_w_map which needs to be unlinked. 26437 */ 26438 26439 static void 26440 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 26441 { 26442 ASSERT(un != NULL); 26443 ASSERT(wm != NULL); 26444 ASSERT(!mutex_owned(SD_MUTEX(un))); 26445 26446 mutex_enter(SD_MUTEX(un)); 26447 26448 if (wm->wm_flags & SD_WTYPE_RMW) { 26449 un->un_rmw_count--; 26450 } 26451 26452 if (wm->wm_wanted_count) { 26453 wm->wm_flags = 0; 26454 /* 26455 * Broadcast that the wmap is available now. 26456 */ 26457 cv_broadcast(&wm->wm_avail); 26458 } else { 26459 /* 26460 * If no one is waiting on the map, it should be free'ed. 26461 */ 26462 sd_free_inlist_wmap(un, wm); 26463 } 26464 26465 mutex_exit(SD_MUTEX(un)); 26466 } 26467 26468 26469 /* 26470 * Function: sd_read_modify_write_task 26471 * 26472 * Description: Called from a taskq thread to initiate the write phase of 26473 * a read-modify-write request. This is used for targets where 26474 * un->un_sys_blocksize != un->un_tgt_blocksize. 26475 * 26476 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 26477 * 26478 * Context: Called under taskq thread context. 26479 */ 26480 26481 static void 26482 sd_read_modify_write_task(void *arg) 26483 { 26484 struct sd_mapblocksize_info *bsp; 26485 struct buf *bp; 26486 struct sd_xbuf *xp; 26487 struct sd_lun *un; 26488 26489 bp = arg; /* The bp is given in arg */ 26490 ASSERT(bp != NULL); 26491 26492 /* Get the pointer to the layer-private data struct */ 26493 xp = SD_GET_XBUF(bp); 26494 ASSERT(xp != NULL); 26495 bsp = xp->xb_private; 26496 ASSERT(bsp != NULL); 26497 26498 un = SD_GET_UN(bp); 26499 ASSERT(un != NULL); 26500 ASSERT(!mutex_owned(SD_MUTEX(un))); 26501 26502 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26503 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 26504 26505 /* 26506 * This is the write phase of a read-modify-write request, called 26507 * under the context of a taskq thread in response to the completion 26508 * of the read portion of the rmw request completing under interrupt 26509 * context. The write request must be sent from here down the iostart 26510 * chain as if it were being sent from sd_mapblocksize_iostart(), so 26511 * we use the layer index saved in the layer-private data area. 26512 */ 26513 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 26514 26515 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26516 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 26517 } 26518 26519 26520 /* 26521 * Function: sddump_do_read_of_rmw() 26522 * 26523 * Description: This routine will be called from sddump, If sddump is called 26524 * with an I/O which not aligned on device blocksize boundary 26525 * then the write has to be converted to read-modify-write. 26526 * Do the read part here in order to keep sddump simple. 26527 * Note - That the sd_mutex is held across the call to this 26528 * routine. 26529 * 26530 * Arguments: un - sd_lun 26531 * blkno - block number in terms of media block size. 26532 * nblk - number of blocks. 26533 * bpp - pointer to pointer to the buf structure. On return 26534 * from this function, *bpp points to the valid buffer 26535 * to which the write has to be done. 26536 * 26537 * Return Code: 0 for success or errno-type return code 26538 */ 26539 26540 static int 26541 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 26542 struct buf **bpp) 26543 { 26544 int err; 26545 int i; 26546 int rval; 26547 struct buf *bp; 26548 struct scsi_pkt *pkt = NULL; 26549 uint32_t target_blocksize; 26550 26551 ASSERT(un != NULL); 26552 ASSERT(mutex_owned(SD_MUTEX(un))); 26553 26554 target_blocksize = un->un_tgt_blocksize; 26555 26556 mutex_exit(SD_MUTEX(un)); 26557 26558 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 26559 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 26560 if (bp == NULL) { 26561 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26562 "no resources for dumping; giving up"); 26563 err = ENOMEM; 26564 goto done; 26565 } 26566 26567 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 26568 blkno, nblk); 26569 if (rval != 0) { 26570 scsi_free_consistent_buf(bp); 26571 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26572 "no resources for dumping; giving up"); 26573 err = ENOMEM; 26574 goto done; 26575 } 26576 26577 pkt->pkt_flags |= FLAG_NOINTR; 26578 26579 err = EIO; 26580 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26581 26582 /* 26583 * Scsi_poll returns 0 (success) if the command completes and 26584 * the status block is STATUS_GOOD. We should only check 26585 * errors if this condition is not true. Even then we should 26586 * send our own request sense packet only if we have a check 26587 * condition and auto request sense has not been performed by 26588 * the hba. 26589 */ 26590 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 26591 26592 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 26593 err = 0; 26594 break; 26595 } 26596 26597 /* 26598 * Check CMD_DEV_GONE 1st, give up if device is gone, 26599 * no need to read RQS data. 26600 */ 26601 if (pkt->pkt_reason == CMD_DEV_GONE) { 26602 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26603 "Error while dumping state with rmw..." 26604 "Device is gone\n"); 26605 break; 26606 } 26607 26608 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 26609 SD_INFO(SD_LOG_DUMP, un, 26610 "sddump: read failed with CHECK, try # %d\n", i); 26611 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 26612 (void) sd_send_polled_RQS(un); 26613 } 26614 26615 continue; 26616 } 26617 26618 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 26619 int reset_retval = 0; 26620 26621 SD_INFO(SD_LOG_DUMP, un, 26622 "sddump: read failed with BUSY, try # %d\n", i); 26623 26624 if (un->un_f_lun_reset_enabled == TRUE) { 26625 reset_retval = scsi_reset(SD_ADDRESS(un), 26626 RESET_LUN); 26627 } 26628 if (reset_retval == 0) { 26629 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26630 } 26631 (void) sd_send_polled_RQS(un); 26632 26633 } else { 26634 SD_INFO(SD_LOG_DUMP, un, 26635 "sddump: read failed with 0x%x, try # %d\n", 26636 SD_GET_PKT_STATUS(pkt), i); 26637 mutex_enter(SD_MUTEX(un)); 26638 sd_reset_target(un, pkt); 26639 mutex_exit(SD_MUTEX(un)); 26640 } 26641 26642 /* 26643 * If we are not getting anywhere with lun/target resets, 26644 * let's reset the bus. 26645 */ 26646 if (i > SD_NDUMP_RETRIES/2) { 26647 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26648 (void) sd_send_polled_RQS(un); 26649 } 26650 26651 } 26652 scsi_destroy_pkt(pkt); 26653 26654 if (err != 0) { 26655 scsi_free_consistent_buf(bp); 26656 *bpp = NULL; 26657 } else { 26658 *bpp = bp; 26659 } 26660 26661 done: 26662 mutex_enter(SD_MUTEX(un)); 26663 return (err); 26664 } 26665 26666 26667 /* 26668 * Function: sd_failfast_flushq 26669 * 26670 * Description: Take all bp's on the wait queue that have B_FAILFAST set 26671 * in b_flags and move them onto the failfast queue, then kick 26672 * off a thread to return all bp's on the failfast queue to 26673 * their owners with an error set. 26674 * 26675 * Arguments: un - pointer to the soft state struct for the instance. 26676 * 26677 * Context: may execute in interrupt context. 26678 */ 26679 26680 static void 26681 sd_failfast_flushq(struct sd_lun *un) 26682 { 26683 struct buf *bp; 26684 struct buf *next_waitq_bp; 26685 struct buf *prev_waitq_bp = NULL; 26686 26687 ASSERT(un != NULL); 26688 ASSERT(mutex_owned(SD_MUTEX(un))); 26689 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 26690 ASSERT(un->un_failfast_bp == NULL); 26691 26692 SD_TRACE(SD_LOG_IO_FAILFAST, un, 26693 "sd_failfast_flushq: entry: un:0x%p\n", un); 26694 26695 /* 26696 * Check if we should flush all bufs when entering failfast state, or 26697 * just those with B_FAILFAST set. 26698 */ 26699 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 26700 /* 26701 * Move *all* bp's on the wait queue to the failfast flush 26702 * queue, including those that do NOT have B_FAILFAST set. 26703 */ 26704 if (un->un_failfast_headp == NULL) { 26705 ASSERT(un->un_failfast_tailp == NULL); 26706 un->un_failfast_headp = un->un_waitq_headp; 26707 } else { 26708 ASSERT(un->un_failfast_tailp != NULL); 26709 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 26710 } 26711 26712 un->un_failfast_tailp = un->un_waitq_tailp; 26713 26714 /* update kstat for each bp moved out of the waitq */ 26715 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 26716 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 26717 } 26718 26719 /* empty the waitq */ 26720 un->un_waitq_headp = un->un_waitq_tailp = NULL; 26721 26722 } else { 26723 /* 26724 * Go thru the wait queue, pick off all entries with 26725 * B_FAILFAST set, and move these onto the failfast queue. 26726 */ 26727 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 26728 /* 26729 * Save the pointer to the next bp on the wait queue, 26730 * so we get to it on the next iteration of this loop. 26731 */ 26732 next_waitq_bp = bp->av_forw; 26733 26734 /* 26735 * If this bp from the wait queue does NOT have 26736 * B_FAILFAST set, just move on to the next element 26737 * in the wait queue. Note, this is the only place 26738 * where it is correct to set prev_waitq_bp. 26739 */ 26740 if ((bp->b_flags & B_FAILFAST) == 0) { 26741 prev_waitq_bp = bp; 26742 continue; 26743 } 26744 26745 /* 26746 * Remove the bp from the wait queue. 26747 */ 26748 if (bp == un->un_waitq_headp) { 26749 /* The bp is the first element of the waitq. */ 26750 un->un_waitq_headp = next_waitq_bp; 26751 if (un->un_waitq_headp == NULL) { 26752 /* The wait queue is now empty */ 26753 un->un_waitq_tailp = NULL; 26754 } 26755 } else { 26756 /* 26757 * The bp is either somewhere in the middle 26758 * or at the end of the wait queue. 26759 */ 26760 ASSERT(un->un_waitq_headp != NULL); 26761 ASSERT(prev_waitq_bp != NULL); 26762 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 26763 == 0); 26764 if (bp == un->un_waitq_tailp) { 26765 /* bp is the last entry on the waitq. */ 26766 ASSERT(next_waitq_bp == NULL); 26767 un->un_waitq_tailp = prev_waitq_bp; 26768 } 26769 prev_waitq_bp->av_forw = next_waitq_bp; 26770 } 26771 bp->av_forw = NULL; 26772 26773 /* 26774 * update kstat since the bp is moved out of 26775 * the waitq 26776 */ 26777 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 26778 26779 /* 26780 * Now put the bp onto the failfast queue. 26781 */ 26782 if (un->un_failfast_headp == NULL) { 26783 /* failfast queue is currently empty */ 26784 ASSERT(un->un_failfast_tailp == NULL); 26785 un->un_failfast_headp = 26786 un->un_failfast_tailp = bp; 26787 } else { 26788 /* Add the bp to the end of the failfast q */ 26789 ASSERT(un->un_failfast_tailp != NULL); 26790 ASSERT(un->un_failfast_tailp->b_flags & 26791 B_FAILFAST); 26792 un->un_failfast_tailp->av_forw = bp; 26793 un->un_failfast_tailp = bp; 26794 } 26795 } 26796 } 26797 26798 /* 26799 * Now return all bp's on the failfast queue to their owners. 26800 */ 26801 while ((bp = un->un_failfast_headp) != NULL) { 26802 26803 un->un_failfast_headp = bp->av_forw; 26804 if (un->un_failfast_headp == NULL) { 26805 un->un_failfast_tailp = NULL; 26806 } 26807 26808 /* 26809 * We want to return the bp with a failure error code, but 26810 * we do not want a call to sd_start_cmds() to occur here, 26811 * so use sd_return_failed_command_no_restart() instead of 26812 * sd_return_failed_command(). 26813 */ 26814 sd_return_failed_command_no_restart(un, bp, EIO); 26815 } 26816 26817 /* Flush the xbuf queues if required. */ 26818 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 26819 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 26820 } 26821 26822 SD_TRACE(SD_LOG_IO_FAILFAST, un, 26823 "sd_failfast_flushq: exit: un:0x%p\n", un); 26824 } 26825 26826 26827 /* 26828 * Function: sd_failfast_flushq_callback 26829 * 26830 * Description: Return TRUE if the given bp meets the criteria for failfast 26831 * flushing. Used with ddi_xbuf_flushq(9F). 26832 * 26833 * Arguments: bp - ptr to buf struct to be examined. 26834 * 26835 * Context: Any 26836 */ 26837 26838 static int 26839 sd_failfast_flushq_callback(struct buf *bp) 26840 { 26841 /* 26842 * Return TRUE if (1) we want to flush ALL bufs when the failfast 26843 * state is entered; OR (2) the given bp has B_FAILFAST set. 26844 */ 26845 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 26846 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 26847 } 26848 26849 26850 26851 /* 26852 * Function: sd_setup_next_xfer 26853 * 26854 * Description: Prepare next I/O operation using DMA_PARTIAL 26855 * 26856 */ 26857 26858 static int 26859 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 26860 struct scsi_pkt *pkt, struct sd_xbuf *xp) 26861 { 26862 ssize_t num_blks_not_xfered; 26863 daddr_t strt_blk_num; 26864 ssize_t bytes_not_xfered; 26865 int rval; 26866 26867 ASSERT(pkt->pkt_resid == 0); 26868 26869 /* 26870 * Calculate next block number and amount to be transferred. 26871 * 26872 * How much data NOT transfered to the HBA yet. 26873 */ 26874 bytes_not_xfered = xp->xb_dma_resid; 26875 26876 /* 26877 * figure how many blocks NOT transfered to the HBA yet. 26878 */ 26879 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 26880 26881 /* 26882 * set starting block number to the end of what WAS transfered. 26883 */ 26884 strt_blk_num = xp->xb_blkno + 26885 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 26886 26887 /* 26888 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 26889 * will call scsi_initpkt with NULL_FUNC so we do not have to release 26890 * the disk mutex here. 26891 */ 26892 rval = sd_setup_next_rw_pkt(un, pkt, bp, 26893 strt_blk_num, num_blks_not_xfered); 26894 26895 if (rval == 0) { 26896 26897 /* 26898 * Success. 26899 * 26900 * Adjust things if there are still more blocks to be 26901 * transfered. 26902 */ 26903 xp->xb_dma_resid = pkt->pkt_resid; 26904 pkt->pkt_resid = 0; 26905 26906 return (1); 26907 } 26908 26909 /* 26910 * There's really only one possible return value from 26911 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 26912 * returns NULL. 26913 */ 26914 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 26915 26916 bp->b_resid = bp->b_bcount; 26917 bp->b_flags |= B_ERROR; 26918 26919 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26920 "Error setting up next portion of DMA transfer\n"); 26921 26922 return (0); 26923 } 26924 26925 /* 26926 * Function: sd_panic_for_res_conflict 26927 * 26928 * Description: Call panic with a string formatted with "Reservation Conflict" 26929 * and a human readable identifier indicating the SD instance 26930 * that experienced the reservation conflict. 26931 * 26932 * Arguments: un - pointer to the soft state struct for the instance. 26933 * 26934 * Context: may execute in interrupt context. 26935 */ 26936 26937 #define SD_RESV_CONFLICT_FMT_LEN 40 26938 void 26939 sd_panic_for_res_conflict(struct sd_lun *un) 26940 { 26941 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 26942 char path_str[MAXPATHLEN]; 26943 26944 (void) snprintf(panic_str, sizeof (panic_str), 26945 "Reservation Conflict\nDisk: %s", 26946 ddi_pathname(SD_DEVINFO(un), path_str)); 26947 26948 panic(panic_str); 26949 } 26950 26951 /* 26952 * Note: The following sd_faultinjection_ioctl( ) routines implement 26953 * driver support for handling fault injection for error analysis 26954 * causing faults in multiple layers of the driver. 26955 * 26956 */ 26957 26958 #ifdef SD_FAULT_INJECTION 26959 static uint_t sd_fault_injection_on = 0; 26960 26961 /* 26962 * Function: sd_faultinjection_ioctl() 26963 * 26964 * Description: This routine is the driver entry point for handling 26965 * faultinjection ioctls to inject errors into the 26966 * layer model 26967 * 26968 * Arguments: cmd - the ioctl cmd received 26969 * arg - the arguments from user and returns 26970 */ 26971 26972 static void 26973 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 26974 26975 uint_t i; 26976 uint_t rval; 26977 26978 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 26979 26980 mutex_enter(SD_MUTEX(un)); 26981 26982 switch (cmd) { 26983 case SDIOCRUN: 26984 /* Allow pushed faults to be injected */ 26985 SD_INFO(SD_LOG_SDTEST, un, 26986 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 26987 26988 sd_fault_injection_on = 1; 26989 26990 SD_INFO(SD_LOG_IOERR, un, 26991 "sd_faultinjection_ioctl: run finished\n"); 26992 break; 26993 26994 case SDIOCSTART: 26995 /* Start Injection Session */ 26996 SD_INFO(SD_LOG_SDTEST, un, 26997 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 26998 26999 sd_fault_injection_on = 0; 27000 un->sd_injection_mask = 0xFFFFFFFF; 27001 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 27002 un->sd_fi_fifo_pkt[i] = NULL; 27003 un->sd_fi_fifo_xb[i] = NULL; 27004 un->sd_fi_fifo_un[i] = NULL; 27005 un->sd_fi_fifo_arq[i] = NULL; 27006 } 27007 un->sd_fi_fifo_start = 0; 27008 un->sd_fi_fifo_end = 0; 27009 27010 mutex_enter(&(un->un_fi_mutex)); 27011 un->sd_fi_log[0] = '\0'; 27012 un->sd_fi_buf_len = 0; 27013 mutex_exit(&(un->un_fi_mutex)); 27014 27015 SD_INFO(SD_LOG_IOERR, un, 27016 "sd_faultinjection_ioctl: start finished\n"); 27017 break; 27018 27019 case SDIOCSTOP: 27020 /* Stop Injection Session */ 27021 SD_INFO(SD_LOG_SDTEST, un, 27022 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 27023 sd_fault_injection_on = 0; 27024 un->sd_injection_mask = 0x0; 27025 27026 /* Empty stray or unuseds structs from fifo */ 27027 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 27028 if (un->sd_fi_fifo_pkt[i] != NULL) { 27029 kmem_free(un->sd_fi_fifo_pkt[i], 27030 sizeof (struct sd_fi_pkt)); 27031 } 27032 if (un->sd_fi_fifo_xb[i] != NULL) { 27033 kmem_free(un->sd_fi_fifo_xb[i], 27034 sizeof (struct sd_fi_xb)); 27035 } 27036 if (un->sd_fi_fifo_un[i] != NULL) { 27037 kmem_free(un->sd_fi_fifo_un[i], 27038 sizeof (struct sd_fi_un)); 27039 } 27040 if (un->sd_fi_fifo_arq[i] != NULL) { 27041 kmem_free(un->sd_fi_fifo_arq[i], 27042 sizeof (struct sd_fi_arq)); 27043 } 27044 un->sd_fi_fifo_pkt[i] = NULL; 27045 un->sd_fi_fifo_un[i] = NULL; 27046 un->sd_fi_fifo_xb[i] = NULL; 27047 un->sd_fi_fifo_arq[i] = NULL; 27048 } 27049 un->sd_fi_fifo_start = 0; 27050 un->sd_fi_fifo_end = 0; 27051 27052 SD_INFO(SD_LOG_IOERR, un, 27053 "sd_faultinjection_ioctl: stop finished\n"); 27054 break; 27055 27056 case SDIOCINSERTPKT: 27057 /* Store a packet struct to be pushed onto fifo */ 27058 SD_INFO(SD_LOG_SDTEST, un, 27059 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 27060 27061 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27062 27063 sd_fault_injection_on = 0; 27064 27065 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 27066 if (un->sd_fi_fifo_pkt[i] != NULL) { 27067 kmem_free(un->sd_fi_fifo_pkt[i], 27068 sizeof (struct sd_fi_pkt)); 27069 } 27070 if (arg != NULL) { 27071 un->sd_fi_fifo_pkt[i] = 27072 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 27073 if (un->sd_fi_fifo_pkt[i] == NULL) { 27074 /* Alloc failed don't store anything */ 27075 break; 27076 } 27077 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 27078 sizeof (struct sd_fi_pkt), 0); 27079 if (rval == -1) { 27080 kmem_free(un->sd_fi_fifo_pkt[i], 27081 sizeof (struct sd_fi_pkt)); 27082 un->sd_fi_fifo_pkt[i] = NULL; 27083 } 27084 } else { 27085 SD_INFO(SD_LOG_IOERR, un, 27086 "sd_faultinjection_ioctl: pkt null\n"); 27087 } 27088 break; 27089 27090 case SDIOCINSERTXB: 27091 /* Store a xb struct to be pushed onto fifo */ 27092 SD_INFO(SD_LOG_SDTEST, un, 27093 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 27094 27095 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27096 27097 sd_fault_injection_on = 0; 27098 27099 if (un->sd_fi_fifo_xb[i] != NULL) { 27100 kmem_free(un->sd_fi_fifo_xb[i], 27101 sizeof (struct sd_fi_xb)); 27102 un->sd_fi_fifo_xb[i] = NULL; 27103 } 27104 if (arg != NULL) { 27105 un->sd_fi_fifo_xb[i] = 27106 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 27107 if (un->sd_fi_fifo_xb[i] == NULL) { 27108 /* Alloc failed don't store anything */ 27109 break; 27110 } 27111 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 27112 sizeof (struct sd_fi_xb), 0); 27113 27114 if (rval == -1) { 27115 kmem_free(un->sd_fi_fifo_xb[i], 27116 sizeof (struct sd_fi_xb)); 27117 un->sd_fi_fifo_xb[i] = NULL; 27118 } 27119 } else { 27120 SD_INFO(SD_LOG_IOERR, un, 27121 "sd_faultinjection_ioctl: xb null\n"); 27122 } 27123 break; 27124 27125 case SDIOCINSERTUN: 27126 /* Store a un struct to be pushed onto fifo */ 27127 SD_INFO(SD_LOG_SDTEST, un, 27128 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 27129 27130 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27131 27132 sd_fault_injection_on = 0; 27133 27134 if (un->sd_fi_fifo_un[i] != NULL) { 27135 kmem_free(un->sd_fi_fifo_un[i], 27136 sizeof (struct sd_fi_un)); 27137 un->sd_fi_fifo_un[i] = NULL; 27138 } 27139 if (arg != NULL) { 27140 un->sd_fi_fifo_un[i] = 27141 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 27142 if (un->sd_fi_fifo_un[i] == NULL) { 27143 /* Alloc failed don't store anything */ 27144 break; 27145 } 27146 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 27147 sizeof (struct sd_fi_un), 0); 27148 if (rval == -1) { 27149 kmem_free(un->sd_fi_fifo_un[i], 27150 sizeof (struct sd_fi_un)); 27151 un->sd_fi_fifo_un[i] = NULL; 27152 } 27153 27154 } else { 27155 SD_INFO(SD_LOG_IOERR, un, 27156 "sd_faultinjection_ioctl: un null\n"); 27157 } 27158 27159 break; 27160 27161 case SDIOCINSERTARQ: 27162 /* Store a arq struct to be pushed onto fifo */ 27163 SD_INFO(SD_LOG_SDTEST, un, 27164 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 27165 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 27166 27167 sd_fault_injection_on = 0; 27168 27169 if (un->sd_fi_fifo_arq[i] != NULL) { 27170 kmem_free(un->sd_fi_fifo_arq[i], 27171 sizeof (struct sd_fi_arq)); 27172 un->sd_fi_fifo_arq[i] = NULL; 27173 } 27174 if (arg != NULL) { 27175 un->sd_fi_fifo_arq[i] = 27176 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 27177 if (un->sd_fi_fifo_arq[i] == NULL) { 27178 /* Alloc failed don't store anything */ 27179 break; 27180 } 27181 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 27182 sizeof (struct sd_fi_arq), 0); 27183 if (rval == -1) { 27184 kmem_free(un->sd_fi_fifo_arq[i], 27185 sizeof (struct sd_fi_arq)); 27186 un->sd_fi_fifo_arq[i] = NULL; 27187 } 27188 27189 } else { 27190 SD_INFO(SD_LOG_IOERR, un, 27191 "sd_faultinjection_ioctl: arq null\n"); 27192 } 27193 27194 break; 27195 27196 case SDIOCPUSH: 27197 /* Push stored xb, pkt, un, and arq onto fifo */ 27198 sd_fault_injection_on = 0; 27199 27200 if (arg != NULL) { 27201 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 27202 if (rval != -1 && 27203 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 27204 un->sd_fi_fifo_end += i; 27205 } 27206 } else { 27207 SD_INFO(SD_LOG_IOERR, un, 27208 "sd_faultinjection_ioctl: push arg null\n"); 27209 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 27210 un->sd_fi_fifo_end++; 27211 } 27212 } 27213 SD_INFO(SD_LOG_IOERR, un, 27214 "sd_faultinjection_ioctl: push to end=%d\n", 27215 un->sd_fi_fifo_end); 27216 break; 27217 27218 case SDIOCRETRIEVE: 27219 /* Return buffer of log from Injection session */ 27220 SD_INFO(SD_LOG_SDTEST, un, 27221 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 27222 27223 sd_fault_injection_on = 0; 27224 27225 mutex_enter(&(un->un_fi_mutex)); 27226 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 27227 un->sd_fi_buf_len+1, 0); 27228 mutex_exit(&(un->un_fi_mutex)); 27229 27230 if (rval == -1) { 27231 /* 27232 * arg is possibly invalid setting 27233 * it to NULL for return 27234 */ 27235 arg = NULL; 27236 } 27237 break; 27238 } 27239 27240 mutex_exit(SD_MUTEX(un)); 27241 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 27242 " exit\n"); 27243 } 27244 27245 27246 /* 27247 * Function: sd_injection_log() 27248 * 27249 * Description: This routine adds buff to the already existing injection log 27250 * for retrieval via faultinjection_ioctl for use in fault 27251 * detection and recovery 27252 * 27253 * Arguments: buf - the string to add to the log 27254 */ 27255 27256 static void 27257 sd_injection_log(char *buf, struct sd_lun *un) 27258 { 27259 uint_t len; 27260 27261 ASSERT(un != NULL); 27262 ASSERT(buf != NULL); 27263 27264 mutex_enter(&(un->un_fi_mutex)); 27265 27266 len = min(strlen(buf), 255); 27267 /* Add logged value to Injection log to be returned later */ 27268 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 27269 uint_t offset = strlen((char *)un->sd_fi_log); 27270 char *destp = (char *)un->sd_fi_log + offset; 27271 int i; 27272 for (i = 0; i < len; i++) { 27273 *destp++ = *buf++; 27274 } 27275 un->sd_fi_buf_len += len; 27276 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 27277 } 27278 27279 mutex_exit(&(un->un_fi_mutex)); 27280 } 27281 27282 27283 /* 27284 * Function: sd_faultinjection() 27285 * 27286 * Description: This routine takes the pkt and changes its 27287 * content based on error injection scenerio. 27288 * 27289 * Arguments: pktp - packet to be changed 27290 */ 27291 27292 static void 27293 sd_faultinjection(struct scsi_pkt *pktp) 27294 { 27295 uint_t i; 27296 struct sd_fi_pkt *fi_pkt; 27297 struct sd_fi_xb *fi_xb; 27298 struct sd_fi_un *fi_un; 27299 struct sd_fi_arq *fi_arq; 27300 struct buf *bp; 27301 struct sd_xbuf *xb; 27302 struct sd_lun *un; 27303 27304 ASSERT(pktp != NULL); 27305 27306 /* pull bp xb and un from pktp */ 27307 bp = (struct buf *)pktp->pkt_private; 27308 xb = SD_GET_XBUF(bp); 27309 un = SD_GET_UN(bp); 27310 27311 ASSERT(un != NULL); 27312 27313 mutex_enter(SD_MUTEX(un)); 27314 27315 SD_TRACE(SD_LOG_SDTEST, un, 27316 "sd_faultinjection: entry Injection from sdintr\n"); 27317 27318 /* if injection is off return */ 27319 if (sd_fault_injection_on == 0 || 27320 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 27321 mutex_exit(SD_MUTEX(un)); 27322 return; 27323 } 27324 27325 27326 /* take next set off fifo */ 27327 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 27328 27329 fi_pkt = un->sd_fi_fifo_pkt[i]; 27330 fi_xb = un->sd_fi_fifo_xb[i]; 27331 fi_un = un->sd_fi_fifo_un[i]; 27332 fi_arq = un->sd_fi_fifo_arq[i]; 27333 27334 27335 /* set variables accordingly */ 27336 /* set pkt if it was on fifo */ 27337 if (fi_pkt != NULL) { 27338 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 27339 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 27340 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 27341 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 27342 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 27343 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 27344 27345 } 27346 27347 /* set xb if it was on fifo */ 27348 if (fi_xb != NULL) { 27349 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 27350 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 27351 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 27352 SD_CONDSET(xb, xb, xb_victim_retry_count, 27353 "xb_victim_retry_count"); 27354 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 27355 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 27356 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 27357 27358 /* copy in block data from sense */ 27359 if (fi_xb->xb_sense_data[0] != -1) { 27360 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 27361 SENSE_LENGTH); 27362 } 27363 27364 /* copy in extended sense codes */ 27365 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 27366 "es_code"); 27367 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 27368 "es_key"); 27369 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 27370 "es_add_code"); 27371 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 27372 es_qual_code, "es_qual_code"); 27373 } 27374 27375 /* set un if it was on fifo */ 27376 if (fi_un != NULL) { 27377 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 27378 SD_CONDSET(un, un, un_ctype, "un_ctype"); 27379 SD_CONDSET(un, un, un_reset_retry_count, 27380 "un_reset_retry_count"); 27381 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 27382 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 27383 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 27384 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 27385 "un_f_allow_bus_device_reset"); 27386 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 27387 27388 } 27389 27390 /* copy in auto request sense if it was on fifo */ 27391 if (fi_arq != NULL) { 27392 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 27393 } 27394 27395 /* free structs */ 27396 if (un->sd_fi_fifo_pkt[i] != NULL) { 27397 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 27398 } 27399 if (un->sd_fi_fifo_xb[i] != NULL) { 27400 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 27401 } 27402 if (un->sd_fi_fifo_un[i] != NULL) { 27403 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 27404 } 27405 if (un->sd_fi_fifo_arq[i] != NULL) { 27406 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 27407 } 27408 27409 /* 27410 * kmem_free does not gurantee to set to NULL 27411 * since we uses these to determine if we set 27412 * values or not lets confirm they are always 27413 * NULL after free 27414 */ 27415 un->sd_fi_fifo_pkt[i] = NULL; 27416 un->sd_fi_fifo_un[i] = NULL; 27417 un->sd_fi_fifo_xb[i] = NULL; 27418 un->sd_fi_fifo_arq[i] = NULL; 27419 27420 un->sd_fi_fifo_start++; 27421 27422 mutex_exit(SD_MUTEX(un)); 27423 27424 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 27425 } 27426 27427 #endif /* SD_FAULT_INJECTION */ 27428 27429 /* 27430 * This routine is invoked in sd_unit_attach(). Before calling it, the 27431 * properties in conf file should be processed already, and "hotpluggable" 27432 * property was processed also. 27433 * 27434 * The sd driver distinguishes 3 different type of devices: removable media, 27435 * non-removable media, and hotpluggable. Below the differences are defined: 27436 * 27437 * 1. Device ID 27438 * 27439 * The device ID of a device is used to identify this device. Refer to 27440 * ddi_devid_register(9F). 27441 * 27442 * For a non-removable media disk device which can provide 0x80 or 0x83 27443 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 27444 * device ID is created to identify this device. For other non-removable 27445 * media devices, a default device ID is created only if this device has 27446 * at least 2 alter cylinders. Otherwise, this device has no devid. 27447 * 27448 * ------------------------------------------------------- 27449 * removable media hotpluggable | Can Have Device ID 27450 * ------------------------------------------------------- 27451 * false false | Yes 27452 * false true | Yes 27453 * true x | No 27454 * ------------------------------------------------------ 27455 * 27456 * 27457 * 2. SCSI group 4 commands 27458 * 27459 * In SCSI specs, only some commands in group 4 command set can use 27460 * 8-byte addresses that can be used to access >2TB storage spaces. 27461 * Other commands have no such capability. Without supporting group4, 27462 * it is impossible to make full use of storage spaces of a disk with 27463 * capacity larger than 2TB. 27464 * 27465 * ----------------------------------------------- 27466 * removable media hotpluggable LP64 | Group 27467 * ----------------------------------------------- 27468 * false false false | 1 27469 * false false true | 4 27470 * false true false | 1 27471 * false true true | 4 27472 * true x x | 5 27473 * ----------------------------------------------- 27474 * 27475 * 27476 * 3. Check for VTOC Label 27477 * 27478 * If a direct-access disk has no EFI label, sd will check if it has a 27479 * valid VTOC label. Now, sd also does that check for removable media 27480 * and hotpluggable devices. 27481 * 27482 * -------------------------------------------------------------- 27483 * Direct-Access removable media hotpluggable | Check Label 27484 * ------------------------------------------------------------- 27485 * false false false | No 27486 * false false true | No 27487 * false true false | Yes 27488 * false true true | Yes 27489 * true x x | Yes 27490 * -------------------------------------------------------------- 27491 * 27492 * 27493 * 4. Building default VTOC label 27494 * 27495 * As section 3 says, sd checks if some kinds of devices have VTOC label. 27496 * If those devices have no valid VTOC label, sd(7d) will attempt to 27497 * create default VTOC for them. Currently sd creates default VTOC label 27498 * for all devices on x86 platform (VTOC_16), but only for removable 27499 * media devices on SPARC (VTOC_8). 27500 * 27501 * ----------------------------------------------------------- 27502 * removable media hotpluggable platform | Default Label 27503 * ----------------------------------------------------------- 27504 * false false sparc | No 27505 * false true x86 | Yes 27506 * false true sparc | Yes 27507 * true x x | Yes 27508 * ---------------------------------------------------------- 27509 * 27510 * 27511 * 5. Supported blocksizes of target devices 27512 * 27513 * Sd supports non-512-byte blocksize for removable media devices only. 27514 * For other devices, only 512-byte blocksize is supported. This may be 27515 * changed in near future because some RAID devices require non-512-byte 27516 * blocksize 27517 * 27518 * ----------------------------------------------------------- 27519 * removable media hotpluggable | non-512-byte blocksize 27520 * ----------------------------------------------------------- 27521 * false false | No 27522 * false true | No 27523 * true x | Yes 27524 * ----------------------------------------------------------- 27525 * 27526 * 27527 * 6. Automatic mount & unmount 27528 * 27529 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 27530 * if a device is removable media device. It return 1 for removable media 27531 * devices, and 0 for others. 27532 * 27533 * The automatic mounting subsystem should distinguish between the types 27534 * of devices and apply automounting policies to each. 27535 * 27536 * 27537 * 7. fdisk partition management 27538 * 27539 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 27540 * just supports fdisk partitions on x86 platform. On sparc platform, sd 27541 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 27542 * fdisk partitions on both x86 and SPARC platform. 27543 * 27544 * ----------------------------------------------------------- 27545 * platform removable media USB/1394 | fdisk supported 27546 * ----------------------------------------------------------- 27547 * x86 X X | true 27548 * ------------------------------------------------------------ 27549 * sparc X X | false 27550 * ------------------------------------------------------------ 27551 * 27552 * 27553 * 8. MBOOT/MBR 27554 * 27555 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 27556 * read/write mboot for removable media devices on sparc platform. 27557 * 27558 * ----------------------------------------------------------- 27559 * platform removable media USB/1394 | mboot supported 27560 * ----------------------------------------------------------- 27561 * x86 X X | true 27562 * ------------------------------------------------------------ 27563 * sparc false false | false 27564 * sparc false true | true 27565 * sparc true false | true 27566 * sparc true true | true 27567 * ------------------------------------------------------------ 27568 * 27569 * 27570 * 9. error handling during opening device 27571 * 27572 * If failed to open a disk device, an errno is returned. For some kinds 27573 * of errors, different errno is returned depending on if this device is 27574 * a removable media device. This brings USB/1394 hard disks in line with 27575 * expected hard disk behavior. It is not expected that this breaks any 27576 * application. 27577 * 27578 * ------------------------------------------------------ 27579 * removable media hotpluggable | errno 27580 * ------------------------------------------------------ 27581 * false false | EIO 27582 * false true | EIO 27583 * true x | ENXIO 27584 * ------------------------------------------------------ 27585 * 27586 * 27587 * 11. ioctls: DKIOCEJECT, CDROMEJECT 27588 * 27589 * These IOCTLs are applicable only to removable media devices. 27590 * 27591 * ----------------------------------------------------------- 27592 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 27593 * ----------------------------------------------------------- 27594 * false false | No 27595 * false true | No 27596 * true x | Yes 27597 * ----------------------------------------------------------- 27598 * 27599 * 27600 * 12. Kstats for partitions 27601 * 27602 * sd creates partition kstat for non-removable media devices. USB and 27603 * Firewire hard disks now have partition kstats 27604 * 27605 * ------------------------------------------------------ 27606 * removable media hotpluggable | kstat 27607 * ------------------------------------------------------ 27608 * false false | Yes 27609 * false true | Yes 27610 * true x | No 27611 * ------------------------------------------------------ 27612 * 27613 * 27614 * 13. Removable media & hotpluggable properties 27615 * 27616 * Sd driver creates a "removable-media" property for removable media 27617 * devices. Parent nexus drivers create a "hotpluggable" property if 27618 * it supports hotplugging. 27619 * 27620 * --------------------------------------------------------------------- 27621 * removable media hotpluggable | "removable-media" " hotpluggable" 27622 * --------------------------------------------------------------------- 27623 * false false | No No 27624 * false true | No Yes 27625 * true false | Yes No 27626 * true true | Yes Yes 27627 * --------------------------------------------------------------------- 27628 * 27629 * 27630 * 14. Power Management 27631 * 27632 * sd only power manages removable media devices or devices that support 27633 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 27634 * 27635 * A parent nexus that supports hotplugging can also set "pm-capable" 27636 * if the disk can be power managed. 27637 * 27638 * ------------------------------------------------------------ 27639 * removable media hotpluggable pm-capable | power manage 27640 * ------------------------------------------------------------ 27641 * false false false | No 27642 * false false true | Yes 27643 * false true false | No 27644 * false true true | Yes 27645 * true x x | Yes 27646 * ------------------------------------------------------------ 27647 * 27648 * USB and firewire hard disks can now be power managed independently 27649 * of the framebuffer 27650 * 27651 * 27652 * 15. Support for USB disks with capacity larger than 1TB 27653 * 27654 * Currently, sd doesn't permit a fixed disk device with capacity 27655 * larger than 1TB to be used in a 32-bit operating system environment. 27656 * However, sd doesn't do that for removable media devices. Instead, it 27657 * assumes that removable media devices cannot have a capacity larger 27658 * than 1TB. Therefore, using those devices on 32-bit system is partially 27659 * supported, which can cause some unexpected results. 27660 * 27661 * --------------------------------------------------------------------- 27662 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 27663 * --------------------------------------------------------------------- 27664 * false false | true | no 27665 * false true | true | no 27666 * true false | true | Yes 27667 * true true | true | Yes 27668 * --------------------------------------------------------------------- 27669 * 27670 * 27671 * 16. Check write-protection at open time 27672 * 27673 * When a removable media device is being opened for writing without NDELAY 27674 * flag, sd will check if this device is writable. If attempting to open 27675 * without NDELAY flag a write-protected device, this operation will abort. 27676 * 27677 * ------------------------------------------------------------ 27678 * removable media USB/1394 | WP Check 27679 * ------------------------------------------------------------ 27680 * false false | No 27681 * false true | No 27682 * true false | Yes 27683 * true true | Yes 27684 * ------------------------------------------------------------ 27685 * 27686 * 27687 * 17. syslog when corrupted VTOC is encountered 27688 * 27689 * Currently, if an invalid VTOC is encountered, sd only print syslog 27690 * for fixed SCSI disks. 27691 * ------------------------------------------------------------ 27692 * removable media USB/1394 | print syslog 27693 * ------------------------------------------------------------ 27694 * false false | Yes 27695 * false true | No 27696 * true false | No 27697 * true true | No 27698 * ------------------------------------------------------------ 27699 */ 27700 static void 27701 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 27702 { 27703 int pm_capable_prop; 27704 27705 ASSERT(un->un_sd); 27706 ASSERT(un->un_sd->sd_inq); 27707 27708 /* 27709 * Enable SYNC CACHE support for all devices. 27710 */ 27711 un->un_f_sync_cache_supported = TRUE; 27712 27713 if (un->un_sd->sd_inq->inq_rmb) { 27714 /* 27715 * The media of this device is removable. And for this kind 27716 * of devices, it is possible to change medium after opening 27717 * devices. Thus we should support this operation. 27718 */ 27719 un->un_f_has_removable_media = TRUE; 27720 27721 /* 27722 * support non-512-byte blocksize of removable media devices 27723 */ 27724 un->un_f_non_devbsize_supported = TRUE; 27725 27726 /* 27727 * Assume that all removable media devices support DOOR_LOCK 27728 */ 27729 un->un_f_doorlock_supported = TRUE; 27730 27731 /* 27732 * For a removable media device, it is possible to be opened 27733 * with NDELAY flag when there is no media in drive, in this 27734 * case we don't care if device is writable. But if without 27735 * NDELAY flag, we need to check if media is write-protected. 27736 */ 27737 un->un_f_chk_wp_open = TRUE; 27738 27739 /* 27740 * need to start a SCSI watch thread to monitor media state, 27741 * when media is being inserted or ejected, notify syseventd. 27742 */ 27743 un->un_f_monitor_media_state = TRUE; 27744 27745 /* 27746 * Some devices don't support START_STOP_UNIT command. 27747 * Therefore, we'd better check if a device supports it 27748 * before sending it. 27749 */ 27750 un->un_f_check_start_stop = TRUE; 27751 27752 /* 27753 * support eject media ioctl: 27754 * FDEJECT, DKIOCEJECT, CDROMEJECT 27755 */ 27756 un->un_f_eject_media_supported = TRUE; 27757 27758 /* 27759 * Because many removable-media devices don't support 27760 * LOG_SENSE, we couldn't use this command to check if 27761 * a removable media device support power-management. 27762 * We assume that they support power-management via 27763 * START_STOP_UNIT command and can be spun up and down 27764 * without limitations. 27765 */ 27766 un->un_f_pm_supported = TRUE; 27767 27768 /* 27769 * Need to create a zero length (Boolean) property 27770 * removable-media for the removable media devices. 27771 * Note that the return value of the property is not being 27772 * checked, since if unable to create the property 27773 * then do not want the attach to fail altogether. Consistent 27774 * with other property creation in attach. 27775 */ 27776 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 27777 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 27778 27779 } else { 27780 /* 27781 * create device ID for device 27782 */ 27783 un->un_f_devid_supported = TRUE; 27784 27785 /* 27786 * Spin up non-removable-media devices once it is attached 27787 */ 27788 un->un_f_attach_spinup = TRUE; 27789 27790 /* 27791 * According to SCSI specification, Sense data has two kinds of 27792 * format: fixed format, and descriptor format. At present, we 27793 * don't support descriptor format sense data for removable 27794 * media. 27795 */ 27796 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 27797 un->un_f_descr_format_supported = TRUE; 27798 } 27799 27800 /* 27801 * kstats are created only for non-removable media devices. 27802 * 27803 * Set this in sd.conf to 0 in order to disable kstats. The 27804 * default is 1, so they are enabled by default. 27805 */ 27806 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 27807 SD_DEVINFO(un), DDI_PROP_DONTPASS, 27808 "enable-partition-kstats", 1)); 27809 27810 /* 27811 * Check if HBA has set the "pm-capable" property. 27812 * If "pm-capable" exists and is non-zero then we can 27813 * power manage the device without checking the start/stop 27814 * cycle count log sense page. 27815 * 27816 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 27817 * then we should not power manage the device. 27818 * 27819 * If "pm-capable" doesn't exist then pm_capable_prop will 27820 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 27821 * sd will check the start/stop cycle count log sense page 27822 * and power manage the device if the cycle count limit has 27823 * not been exceeded. 27824 */ 27825 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 27826 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 27827 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 27828 un->un_f_log_sense_supported = TRUE; 27829 } else { 27830 /* 27831 * pm-capable property exists. 27832 * 27833 * Convert "TRUE" values for pm_capable_prop to 27834 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 27835 * later. "TRUE" values are any values except 27836 * SD_PM_CAPABLE_FALSE (0) and 27837 * SD_PM_CAPABLE_UNDEFINED (-1) 27838 */ 27839 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 27840 un->un_f_log_sense_supported = FALSE; 27841 } else { 27842 un->un_f_pm_supported = TRUE; 27843 } 27844 27845 SD_INFO(SD_LOG_ATTACH_DETACH, un, 27846 "sd_unit_attach: un:0x%p pm-capable " 27847 "property set to %d.\n", un, un->un_f_pm_supported); 27848 } 27849 } 27850 27851 if (un->un_f_is_hotpluggable) { 27852 27853 /* 27854 * Have to watch hotpluggable devices as well, since 27855 * that's the only way for userland applications to 27856 * detect hot removal while device is busy/mounted. 27857 */ 27858 un->un_f_monitor_media_state = TRUE; 27859 27860 un->un_f_check_start_stop = TRUE; 27861 27862 } 27863 } 27864 27865 /* 27866 * sd_tg_rdwr: 27867 * Provides rdwr access for cmlb via sd_tgops. The start_block is 27868 * in sys block size, req_length in bytes. 27869 * 27870 */ 27871 static int 27872 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr, 27873 diskaddr_t start_block, size_t reqlength, void *tg_cookie) 27874 { 27875 struct sd_lun *un; 27876 int path_flag = (int)(uintptr_t)tg_cookie; 27877 char *dkl = NULL; 27878 diskaddr_t real_addr = start_block; 27879 diskaddr_t first_byte, end_block; 27880 27881 size_t buffer_size = reqlength; 27882 int rval; 27883 diskaddr_t cap; 27884 uint32_t lbasize; 27885 27886 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 27887 if (un == NULL) 27888 return (ENXIO); 27889 27890 if (cmd != TG_READ && cmd != TG_WRITE) 27891 return (EINVAL); 27892 27893 mutex_enter(SD_MUTEX(un)); 27894 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 27895 mutex_exit(SD_MUTEX(un)); 27896 rval = sd_send_scsi_READ_CAPACITY(un, (uint64_t *)&cap, 27897 &lbasize, path_flag); 27898 if (rval != 0) 27899 return (rval); 27900 mutex_enter(SD_MUTEX(un)); 27901 sd_update_block_info(un, lbasize, cap); 27902 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) { 27903 mutex_exit(SD_MUTEX(un)); 27904 return (EIO); 27905 } 27906 } 27907 27908 if (NOT_DEVBSIZE(un)) { 27909 /* 27910 * sys_blocksize != tgt_blocksize, need to re-adjust 27911 * blkno and save the index to beginning of dk_label 27912 */ 27913 first_byte = SD_SYSBLOCKS2BYTES(un, start_block); 27914 real_addr = first_byte / un->un_tgt_blocksize; 27915 27916 end_block = (first_byte + reqlength + 27917 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize; 27918 27919 /* round up buffer size to multiple of target block size */ 27920 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize; 27921 27922 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr", 27923 "label_addr: 0x%x allocation size: 0x%x\n", 27924 real_addr, buffer_size); 27925 27926 if (((first_byte % un->un_tgt_blocksize) != 0) || 27927 (reqlength % un->un_tgt_blocksize) != 0) 27928 /* the request is not aligned */ 27929 dkl = kmem_zalloc(buffer_size, KM_SLEEP); 27930 } 27931 27932 /* 27933 * The MMC standard allows READ CAPACITY to be 27934 * inaccurate by a bounded amount (in the interest of 27935 * response latency). As a result, failed READs are 27936 * commonplace (due to the reading of metadata and not 27937 * data). Depending on the per-Vendor/drive Sense data, 27938 * the failed READ can cause many (unnecessary) retries. 27939 */ 27940 27941 if (ISCD(un) && (cmd == TG_READ) && 27942 (un->un_f_blockcount_is_valid == TRUE) && 27943 ((start_block == (un->un_blockcount - 1))|| 27944 (start_block == (un->un_blockcount - 2)))) { 27945 path_flag = SD_PATH_DIRECT_PRIORITY; 27946 } 27947 27948 mutex_exit(SD_MUTEX(un)); 27949 if (cmd == TG_READ) { 27950 rval = sd_send_scsi_READ(un, (dkl != NULL)? dkl: bufaddr, 27951 buffer_size, real_addr, path_flag); 27952 if (dkl != NULL) 27953 bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block, 27954 real_addr), bufaddr, reqlength); 27955 } else { 27956 if (dkl) { 27957 rval = sd_send_scsi_READ(un, dkl, buffer_size, 27958 real_addr, path_flag); 27959 if (rval) { 27960 kmem_free(dkl, buffer_size); 27961 return (rval); 27962 } 27963 bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block, 27964 real_addr), reqlength); 27965 } 27966 rval = sd_send_scsi_WRITE(un, (dkl != NULL)? dkl: bufaddr, 27967 buffer_size, real_addr, path_flag); 27968 } 27969 27970 if (dkl != NULL) 27971 kmem_free(dkl, buffer_size); 27972 27973 return (rval); 27974 } 27975 27976 27977 static int 27978 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie) 27979 { 27980 27981 struct sd_lun *un; 27982 diskaddr_t cap; 27983 uint32_t lbasize; 27984 int path_flag = (int)(uintptr_t)tg_cookie; 27985 int ret = 0; 27986 27987 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 27988 if (un == NULL) 27989 return (ENXIO); 27990 27991 switch (cmd) { 27992 case TG_GETPHYGEOM: 27993 case TG_GETVIRTGEOM: 27994 case TG_GETCAPACITY: 27995 case TG_GETBLOCKSIZE: 27996 mutex_enter(SD_MUTEX(un)); 27997 27998 if ((un->un_f_blockcount_is_valid == TRUE) && 27999 (un->un_f_tgt_blocksize_is_valid == TRUE)) { 28000 cap = un->un_blockcount; 28001 lbasize = un->un_tgt_blocksize; 28002 mutex_exit(SD_MUTEX(un)); 28003 } else { 28004 mutex_exit(SD_MUTEX(un)); 28005 ret = sd_send_scsi_READ_CAPACITY(un, (uint64_t *)&cap, 28006 &lbasize, path_flag); 28007 if (ret != 0) 28008 return (ret); 28009 mutex_enter(SD_MUTEX(un)); 28010 sd_update_block_info(un, lbasize, cap); 28011 if ((un->un_f_blockcount_is_valid == FALSE) || 28012 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 28013 mutex_exit(SD_MUTEX(un)); 28014 return (EIO); 28015 } 28016 mutex_exit(SD_MUTEX(un)); 28017 } 28018 28019 if (cmd == TG_GETCAPACITY) { 28020 *(diskaddr_t *)arg = cap; 28021 return (0); 28022 } 28023 28024 if (cmd == TG_GETBLOCKSIZE) { 28025 *(uint32_t *)arg = lbasize; 28026 return (0); 28027 } 28028 28029 if (cmd == TG_GETPHYGEOM) 28030 ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg, 28031 cap, lbasize, path_flag); 28032 else 28033 /* TG_GETVIRTGEOM */ 28034 ret = sd_get_virtual_geometry(un, 28035 (cmlb_geom_t *)arg, cap, lbasize); 28036 28037 return (ret); 28038 28039 case TG_GETATTR: 28040 mutex_enter(SD_MUTEX(un)); 28041 ((tg_attribute_t *)arg)->media_is_writable = 28042 un->un_f_mmc_writable_media; 28043 mutex_exit(SD_MUTEX(un)); 28044 return (0); 28045 default: 28046 return (ENOTTY); 28047 28048 } 28049 28050 } 28051