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 2007 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_CONT, 14842 "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_softerrs); 16430 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 16431 16432 si.ssi_severity = SCSI_ERR_INFO; 16433 si.ssi_pfa_flag = FALSE; 16434 16435 /* Pointless to retry if the target thinks it's an illegal request */ 16436 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16437 sd_return_failed_command(un, bp, EIO); 16438 } 16439 16440 16441 16442 16443 /* 16444 * Function: sd_sense_key_unit_attention 16445 * 16446 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 16447 * 16448 * Context: May be called from interrupt context 16449 */ 16450 16451 static void 16452 sd_sense_key_unit_attention(struct sd_lun *un, 16453 uint8_t *sense_datap, 16454 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16455 { 16456 /* 16457 * For UNIT ATTENTION we allow retries for one minute. Devices 16458 * like Sonoma can return UNIT ATTENTION close to a minute 16459 * under certain conditions. 16460 */ 16461 int retry_check_flag = SD_RETRIES_UA; 16462 boolean_t kstat_updated = B_FALSE; 16463 struct sd_sense_info si; 16464 uint8_t asc = scsi_sense_asc(sense_datap); 16465 16466 ASSERT(un != NULL); 16467 ASSERT(mutex_owned(SD_MUTEX(un))); 16468 ASSERT(bp != NULL); 16469 ASSERT(xp != NULL); 16470 ASSERT(pktp != NULL); 16471 16472 si.ssi_severity = SCSI_ERR_INFO; 16473 si.ssi_pfa_flag = FALSE; 16474 16475 16476 switch (asc) { 16477 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 16478 if (sd_report_pfa != 0) { 16479 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 16480 si.ssi_pfa_flag = TRUE; 16481 retry_check_flag = SD_RETRIES_STANDARD; 16482 goto do_retry; 16483 } 16484 16485 break; 16486 16487 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 16488 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 16489 un->un_resvd_status |= 16490 (SD_LOST_RESERVE | SD_WANT_RESERVE); 16491 } 16492 #ifdef _LP64 16493 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 16494 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 16495 un, KM_NOSLEEP) == 0) { 16496 /* 16497 * If we can't dispatch the task we'll just 16498 * live without descriptor sense. We can 16499 * try again on the next "unit attention" 16500 */ 16501 SD_ERROR(SD_LOG_ERROR, un, 16502 "sd_sense_key_unit_attention: " 16503 "Could not dispatch " 16504 "sd_reenable_dsense_task\n"); 16505 } 16506 } 16507 #endif /* _LP64 */ 16508 /* FALLTHRU */ 16509 16510 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 16511 if (!un->un_f_has_removable_media) { 16512 break; 16513 } 16514 16515 /* 16516 * When we get a unit attention from a removable-media device, 16517 * it may be in a state that will take a long time to recover 16518 * (e.g., from a reset). Since we are executing in interrupt 16519 * context here, we cannot wait around for the device to come 16520 * back. So hand this command off to sd_media_change_task() 16521 * for deferred processing under taskq thread context. (Note 16522 * that the command still may be failed if a problem is 16523 * encountered at a later time.) 16524 */ 16525 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 16526 KM_NOSLEEP) == 0) { 16527 /* 16528 * Cannot dispatch the request so fail the command. 16529 */ 16530 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16531 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16532 si.ssi_severity = SCSI_ERR_FATAL; 16533 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16534 sd_return_failed_command(un, bp, EIO); 16535 } 16536 16537 /* 16538 * If failed to dispatch sd_media_change_task(), we already 16539 * updated kstat. If succeed to dispatch sd_media_change_task(), 16540 * we should update kstat later if it encounters an error. So, 16541 * we update kstat_updated flag here. 16542 */ 16543 kstat_updated = B_TRUE; 16544 16545 /* 16546 * Either the command has been successfully dispatched to a 16547 * task Q for retrying, or the dispatch failed. In either case 16548 * do NOT retry again by calling sd_retry_command. This sets up 16549 * two retries of the same command and when one completes and 16550 * frees the resources the other will access freed memory, 16551 * a bad thing. 16552 */ 16553 return; 16554 16555 default: 16556 break; 16557 } 16558 16559 /* 16560 * Update kstat if we haven't done that. 16561 */ 16562 if (!kstat_updated) { 16563 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16564 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 16565 } 16566 16567 do_retry: 16568 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 16569 EIO, SD_UA_RETRY_DELAY, NULL); 16570 } 16571 16572 16573 16574 /* 16575 * Function: sd_sense_key_fail_command 16576 * 16577 * Description: Use to fail a command when we don't like the sense key that 16578 * was returned. 16579 * 16580 * Context: May be called from interrupt context 16581 */ 16582 16583 static void 16584 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 16585 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16586 { 16587 struct sd_sense_info si; 16588 16589 ASSERT(un != NULL); 16590 ASSERT(mutex_owned(SD_MUTEX(un))); 16591 ASSERT(bp != NULL); 16592 ASSERT(xp != NULL); 16593 ASSERT(pktp != NULL); 16594 16595 si.ssi_severity = SCSI_ERR_FATAL; 16596 si.ssi_pfa_flag = FALSE; 16597 16598 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16599 sd_return_failed_command(un, bp, EIO); 16600 } 16601 16602 16603 16604 /* 16605 * Function: sd_sense_key_blank_check 16606 * 16607 * Description: Recovery actions for a SCSI "Blank Check" sense key. 16608 * Has no monetary connotation. 16609 * 16610 * Context: May be called from interrupt context 16611 */ 16612 16613 static void 16614 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 16615 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16616 { 16617 struct sd_sense_info si; 16618 16619 ASSERT(un != NULL); 16620 ASSERT(mutex_owned(SD_MUTEX(un))); 16621 ASSERT(bp != NULL); 16622 ASSERT(xp != NULL); 16623 ASSERT(pktp != NULL); 16624 16625 /* 16626 * Blank check is not fatal for removable devices, therefore 16627 * it does not require a console message. 16628 */ 16629 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 16630 SCSI_ERR_FATAL; 16631 si.ssi_pfa_flag = FALSE; 16632 16633 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 16634 sd_return_failed_command(un, bp, EIO); 16635 } 16636 16637 16638 16639 16640 /* 16641 * Function: sd_sense_key_aborted_command 16642 * 16643 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 16644 * 16645 * Context: May be called from interrupt context 16646 */ 16647 16648 static void 16649 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 16650 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16651 { 16652 struct sd_sense_info si; 16653 16654 ASSERT(un != NULL); 16655 ASSERT(mutex_owned(SD_MUTEX(un))); 16656 ASSERT(bp != NULL); 16657 ASSERT(xp != NULL); 16658 ASSERT(pktp != NULL); 16659 16660 si.ssi_severity = SCSI_ERR_FATAL; 16661 si.ssi_pfa_flag = FALSE; 16662 16663 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16664 16665 /* 16666 * This really ought to be a fatal error, but we will retry anyway 16667 * as some drives report this as a spurious error. 16668 */ 16669 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16670 &si, EIO, drv_usectohz(100000), NULL); 16671 } 16672 16673 16674 16675 /* 16676 * Function: sd_sense_key_default 16677 * 16678 * Description: Default recovery action for several SCSI sense keys (basically 16679 * attempts a retry). 16680 * 16681 * Context: May be called from interrupt context 16682 */ 16683 16684 static void 16685 sd_sense_key_default(struct sd_lun *un, 16686 uint8_t *sense_datap, 16687 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 16688 { 16689 struct sd_sense_info si; 16690 uint8_t sense_key = scsi_sense_key(sense_datap); 16691 16692 ASSERT(un != NULL); 16693 ASSERT(mutex_owned(SD_MUTEX(un))); 16694 ASSERT(bp != NULL); 16695 ASSERT(xp != NULL); 16696 ASSERT(pktp != NULL); 16697 16698 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16699 16700 /* 16701 * Undecoded sense key. Attempt retries and hope that will fix 16702 * the problem. Otherwise, we're dead. 16703 */ 16704 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 16705 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16706 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 16707 } 16708 16709 si.ssi_severity = SCSI_ERR_FATAL; 16710 si.ssi_pfa_flag = FALSE; 16711 16712 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 16713 &si, EIO, (clock_t)0, NULL); 16714 } 16715 16716 16717 16718 /* 16719 * Function: sd_print_retry_msg 16720 * 16721 * Description: Print a message indicating the retry action being taken. 16722 * 16723 * Arguments: un - ptr to associated softstate 16724 * bp - ptr to buf(9S) for the command 16725 * arg - not used. 16726 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16727 * or SD_NO_RETRY_ISSUED 16728 * 16729 * Context: May be called from interrupt context 16730 */ 16731 /* ARGSUSED */ 16732 static void 16733 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 16734 { 16735 struct sd_xbuf *xp; 16736 struct scsi_pkt *pktp; 16737 char *reasonp; 16738 char *msgp; 16739 16740 ASSERT(un != NULL); 16741 ASSERT(mutex_owned(SD_MUTEX(un))); 16742 ASSERT(bp != NULL); 16743 pktp = SD_GET_PKTP(bp); 16744 ASSERT(pktp != NULL); 16745 xp = SD_GET_XBUF(bp); 16746 ASSERT(xp != NULL); 16747 16748 ASSERT(!mutex_owned(&un->un_pm_mutex)); 16749 mutex_enter(&un->un_pm_mutex); 16750 if ((un->un_state == SD_STATE_SUSPENDED) || 16751 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 16752 (pktp->pkt_flags & FLAG_SILENT)) { 16753 mutex_exit(&un->un_pm_mutex); 16754 goto update_pkt_reason; 16755 } 16756 mutex_exit(&un->un_pm_mutex); 16757 16758 /* 16759 * Suppress messages if they are all the same pkt_reason; with 16760 * TQ, many (up to 256) are returned with the same pkt_reason. 16761 * If we are in panic, then suppress the retry messages. 16762 */ 16763 switch (flag) { 16764 case SD_NO_RETRY_ISSUED: 16765 msgp = "giving up"; 16766 break; 16767 case SD_IMMEDIATE_RETRY_ISSUED: 16768 case SD_DELAYED_RETRY_ISSUED: 16769 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 16770 ((pktp->pkt_reason == un->un_last_pkt_reason) && 16771 (sd_error_level != SCSI_ERR_ALL))) { 16772 return; 16773 } 16774 msgp = "retrying command"; 16775 break; 16776 default: 16777 goto update_pkt_reason; 16778 } 16779 16780 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 16781 scsi_rname(pktp->pkt_reason)); 16782 16783 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16784 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 16785 16786 update_pkt_reason: 16787 /* 16788 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 16789 * This is to prevent multiple console messages for the same failure 16790 * condition. Note that un->un_last_pkt_reason is NOT restored if & 16791 * when the command is retried successfully because there still may be 16792 * more commands coming back with the same value of pktp->pkt_reason. 16793 */ 16794 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 16795 un->un_last_pkt_reason = pktp->pkt_reason; 16796 } 16797 } 16798 16799 16800 /* 16801 * Function: sd_print_cmd_incomplete_msg 16802 * 16803 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 16804 * 16805 * Arguments: un - ptr to associated softstate 16806 * bp - ptr to buf(9S) for the command 16807 * arg - passed to sd_print_retry_msg() 16808 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 16809 * or SD_NO_RETRY_ISSUED 16810 * 16811 * Context: May be called from interrupt context 16812 */ 16813 16814 static void 16815 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 16816 int code) 16817 { 16818 dev_info_t *dip; 16819 16820 ASSERT(un != NULL); 16821 ASSERT(mutex_owned(SD_MUTEX(un))); 16822 ASSERT(bp != NULL); 16823 16824 switch (code) { 16825 case SD_NO_RETRY_ISSUED: 16826 /* Command was failed. Someone turned off this target? */ 16827 if (un->un_state != SD_STATE_OFFLINE) { 16828 /* 16829 * Suppress message if we are detaching and 16830 * device has been disconnected 16831 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 16832 * private interface and not part of the DDI 16833 */ 16834 dip = un->un_sd->sd_dev; 16835 if (!(DEVI_IS_DETACHING(dip) && 16836 DEVI_IS_DEVICE_REMOVED(dip))) { 16837 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 16838 "disk not responding to selection\n"); 16839 } 16840 New_state(un, SD_STATE_OFFLINE); 16841 } 16842 break; 16843 16844 case SD_DELAYED_RETRY_ISSUED: 16845 case SD_IMMEDIATE_RETRY_ISSUED: 16846 default: 16847 /* Command was successfully queued for retry */ 16848 sd_print_retry_msg(un, bp, arg, code); 16849 break; 16850 } 16851 } 16852 16853 16854 /* 16855 * Function: sd_pkt_reason_cmd_incomplete 16856 * 16857 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 16858 * 16859 * Context: May be called from interrupt context 16860 */ 16861 16862 static void 16863 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 16864 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16865 { 16866 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 16867 16868 ASSERT(un != NULL); 16869 ASSERT(mutex_owned(SD_MUTEX(un))); 16870 ASSERT(bp != NULL); 16871 ASSERT(xp != NULL); 16872 ASSERT(pktp != NULL); 16873 16874 /* Do not do a reset if selection did not complete */ 16875 /* Note: Should this not just check the bit? */ 16876 if (pktp->pkt_state != STATE_GOT_BUS) { 16877 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16878 sd_reset_target(un, pktp); 16879 } 16880 16881 /* 16882 * If the target was not successfully selected, then set 16883 * SD_RETRIES_FAILFAST to indicate that we lost communication 16884 * with the target, and further retries and/or commands are 16885 * likely to take a long time. 16886 */ 16887 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 16888 flag |= SD_RETRIES_FAILFAST; 16889 } 16890 16891 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16892 16893 sd_retry_command(un, bp, flag, 16894 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16895 } 16896 16897 16898 16899 /* 16900 * Function: sd_pkt_reason_cmd_tran_err 16901 * 16902 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 16903 * 16904 * Context: May be called from interrupt context 16905 */ 16906 16907 static void 16908 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 16909 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16910 { 16911 ASSERT(un != NULL); 16912 ASSERT(mutex_owned(SD_MUTEX(un))); 16913 ASSERT(bp != NULL); 16914 ASSERT(xp != NULL); 16915 ASSERT(pktp != NULL); 16916 16917 /* 16918 * Do not reset if we got a parity error, or if 16919 * selection did not complete. 16920 */ 16921 SD_UPDATE_ERRSTATS(un, sd_harderrs); 16922 /* Note: Should this not just check the bit for pkt_state? */ 16923 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 16924 (pktp->pkt_state != STATE_GOT_BUS)) { 16925 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16926 sd_reset_target(un, pktp); 16927 } 16928 16929 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16930 16931 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 16932 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16933 } 16934 16935 16936 16937 /* 16938 * Function: sd_pkt_reason_cmd_reset 16939 * 16940 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 16941 * 16942 * Context: May be called from interrupt context 16943 */ 16944 16945 static void 16946 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 16947 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16948 { 16949 ASSERT(un != NULL); 16950 ASSERT(mutex_owned(SD_MUTEX(un))); 16951 ASSERT(bp != NULL); 16952 ASSERT(xp != NULL); 16953 ASSERT(pktp != NULL); 16954 16955 /* The target may still be running the command, so try to reset. */ 16956 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16957 sd_reset_target(un, pktp); 16958 16959 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16960 16961 /* 16962 * If pkt_reason is CMD_RESET chances are that this pkt got 16963 * reset because another target on this bus caused it. The target 16964 * that caused it should get CMD_TIMEOUT with pkt_statistics 16965 * of STAT_TIMEOUT/STAT_DEV_RESET. 16966 */ 16967 16968 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 16969 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 16970 } 16971 16972 16973 16974 16975 /* 16976 * Function: sd_pkt_reason_cmd_aborted 16977 * 16978 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 16979 * 16980 * Context: May be called from interrupt context 16981 */ 16982 16983 static void 16984 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 16985 struct sd_xbuf *xp, struct scsi_pkt *pktp) 16986 { 16987 ASSERT(un != NULL); 16988 ASSERT(mutex_owned(SD_MUTEX(un))); 16989 ASSERT(bp != NULL); 16990 ASSERT(xp != NULL); 16991 ASSERT(pktp != NULL); 16992 16993 /* The target may still be running the command, so try to reset. */ 16994 SD_UPDATE_ERRSTATS(un, sd_transerrs); 16995 sd_reset_target(un, pktp); 16996 16997 SD_UPDATE_RESERVATION_STATUS(un, pktp); 16998 16999 /* 17000 * If pkt_reason is CMD_ABORTED chances are that this pkt got 17001 * aborted because another target on this bus caused it. The target 17002 * that caused it should get CMD_TIMEOUT with pkt_statistics 17003 * of STAT_TIMEOUT/STAT_DEV_RESET. 17004 */ 17005 17006 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 17007 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17008 } 17009 17010 17011 17012 /* 17013 * Function: sd_pkt_reason_cmd_timeout 17014 * 17015 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 17016 * 17017 * Context: May be called from interrupt context 17018 */ 17019 17020 static void 17021 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 17022 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17023 { 17024 ASSERT(un != NULL); 17025 ASSERT(mutex_owned(SD_MUTEX(un))); 17026 ASSERT(bp != NULL); 17027 ASSERT(xp != NULL); 17028 ASSERT(pktp != NULL); 17029 17030 17031 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17032 sd_reset_target(un, pktp); 17033 17034 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17035 17036 /* 17037 * A command timeout indicates that we could not establish 17038 * communication with the target, so set SD_RETRIES_FAILFAST 17039 * as further retries/commands are likely to take a long time. 17040 */ 17041 sd_retry_command(un, bp, 17042 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 17043 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17044 } 17045 17046 17047 17048 /* 17049 * Function: sd_pkt_reason_cmd_unx_bus_free 17050 * 17051 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 17052 * 17053 * Context: May be called from interrupt context 17054 */ 17055 17056 static void 17057 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 17058 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17059 { 17060 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 17061 17062 ASSERT(un != NULL); 17063 ASSERT(mutex_owned(SD_MUTEX(un))); 17064 ASSERT(bp != NULL); 17065 ASSERT(xp != NULL); 17066 ASSERT(pktp != NULL); 17067 17068 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17069 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17070 17071 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 17072 sd_print_retry_msg : NULL; 17073 17074 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17075 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17076 } 17077 17078 17079 /* 17080 * Function: sd_pkt_reason_cmd_tag_reject 17081 * 17082 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 17083 * 17084 * Context: May be called from interrupt context 17085 */ 17086 17087 static void 17088 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 17089 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17090 { 17091 ASSERT(un != NULL); 17092 ASSERT(mutex_owned(SD_MUTEX(un))); 17093 ASSERT(bp != NULL); 17094 ASSERT(xp != NULL); 17095 ASSERT(pktp != NULL); 17096 17097 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17098 pktp->pkt_flags = 0; 17099 un->un_tagflags = 0; 17100 if (un->un_f_opt_queueing == TRUE) { 17101 un->un_throttle = min(un->un_throttle, 3); 17102 } else { 17103 un->un_throttle = 1; 17104 } 17105 mutex_exit(SD_MUTEX(un)); 17106 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 17107 mutex_enter(SD_MUTEX(un)); 17108 17109 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17110 17111 /* Legacy behavior not to check retry counts here. */ 17112 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 17113 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17114 } 17115 17116 17117 /* 17118 * Function: sd_pkt_reason_default 17119 * 17120 * Description: Default recovery actions for SCSA pkt_reason values that 17121 * do not have more explicit recovery actions. 17122 * 17123 * Context: May be called from interrupt context 17124 */ 17125 17126 static void 17127 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 17128 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17129 { 17130 ASSERT(un != NULL); 17131 ASSERT(mutex_owned(SD_MUTEX(un))); 17132 ASSERT(bp != NULL); 17133 ASSERT(xp != NULL); 17134 ASSERT(pktp != NULL); 17135 17136 SD_UPDATE_ERRSTATS(un, sd_transerrs); 17137 sd_reset_target(un, pktp); 17138 17139 SD_UPDATE_RESERVATION_STATUS(un, pktp); 17140 17141 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 17142 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 17143 } 17144 17145 17146 17147 /* 17148 * Function: sd_pkt_status_check_condition 17149 * 17150 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 17151 * 17152 * Context: May be called from interrupt context 17153 */ 17154 17155 static void 17156 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 17157 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17158 { 17159 ASSERT(un != NULL); 17160 ASSERT(mutex_owned(SD_MUTEX(un))); 17161 ASSERT(bp != NULL); 17162 ASSERT(xp != NULL); 17163 ASSERT(pktp != NULL); 17164 17165 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 17166 "entry: buf:0x%p xp:0x%p\n", bp, xp); 17167 17168 /* 17169 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 17170 * command will be retried after the request sense). Otherwise, retry 17171 * the command. Note: we are issuing the request sense even though the 17172 * retry limit may have been reached for the failed command. 17173 */ 17174 if (un->un_f_arq_enabled == FALSE) { 17175 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 17176 "no ARQ, sending request sense command\n"); 17177 sd_send_request_sense_command(un, bp, pktp); 17178 } else { 17179 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 17180 "ARQ,retrying request sense command\n"); 17181 #if defined(__i386) || defined(__amd64) 17182 /* 17183 * The SD_RETRY_DELAY value need to be adjusted here 17184 * when SD_RETRY_DELAY change in sddef.h 17185 */ 17186 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17187 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 17188 NULL); 17189 #else 17190 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 17191 EIO, SD_RETRY_DELAY, NULL); 17192 #endif 17193 } 17194 17195 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 17196 } 17197 17198 17199 /* 17200 * Function: sd_pkt_status_busy 17201 * 17202 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 17203 * 17204 * Context: May be called from interrupt context 17205 */ 17206 17207 static void 17208 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17209 struct scsi_pkt *pktp) 17210 { 17211 ASSERT(un != NULL); 17212 ASSERT(mutex_owned(SD_MUTEX(un))); 17213 ASSERT(bp != NULL); 17214 ASSERT(xp != NULL); 17215 ASSERT(pktp != NULL); 17216 17217 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17218 "sd_pkt_status_busy: entry\n"); 17219 17220 /* If retries are exhausted, just fail the command. */ 17221 if (xp->xb_retry_count >= un->un_busy_retry_count) { 17222 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17223 "device busy too long\n"); 17224 sd_return_failed_command(un, bp, EIO); 17225 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17226 "sd_pkt_status_busy: exit\n"); 17227 return; 17228 } 17229 xp->xb_retry_count++; 17230 17231 /* 17232 * Try to reset the target. However, we do not want to perform 17233 * more than one reset if the device continues to fail. The reset 17234 * will be performed when the retry count reaches the reset 17235 * threshold. This threshold should be set such that at least 17236 * one retry is issued before the reset is performed. 17237 */ 17238 if (xp->xb_retry_count == 17239 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 17240 int rval = 0; 17241 mutex_exit(SD_MUTEX(un)); 17242 if (un->un_f_allow_bus_device_reset == TRUE) { 17243 /* 17244 * First try to reset the LUN; if we cannot then 17245 * try to reset the target. 17246 */ 17247 if (un->un_f_lun_reset_enabled == TRUE) { 17248 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17249 "sd_pkt_status_busy: RESET_LUN\n"); 17250 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17251 } 17252 if (rval == 0) { 17253 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17254 "sd_pkt_status_busy: RESET_TARGET\n"); 17255 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17256 } 17257 } 17258 if (rval == 0) { 17259 /* 17260 * If the RESET_LUN and/or RESET_TARGET failed, 17261 * try RESET_ALL 17262 */ 17263 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17264 "sd_pkt_status_busy: RESET_ALL\n"); 17265 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 17266 } 17267 mutex_enter(SD_MUTEX(un)); 17268 if (rval == 0) { 17269 /* 17270 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 17271 * At this point we give up & fail the command. 17272 */ 17273 sd_return_failed_command(un, bp, EIO); 17274 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17275 "sd_pkt_status_busy: exit (failed cmd)\n"); 17276 return; 17277 } 17278 } 17279 17280 /* 17281 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 17282 * we have already checked the retry counts above. 17283 */ 17284 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 17285 EIO, SD_BSY_TIMEOUT, NULL); 17286 17287 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17288 "sd_pkt_status_busy: exit\n"); 17289 } 17290 17291 17292 /* 17293 * Function: sd_pkt_status_reservation_conflict 17294 * 17295 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 17296 * command status. 17297 * 17298 * Context: May be called from interrupt context 17299 */ 17300 17301 static void 17302 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 17303 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17304 { 17305 ASSERT(un != NULL); 17306 ASSERT(mutex_owned(SD_MUTEX(un))); 17307 ASSERT(bp != NULL); 17308 ASSERT(xp != NULL); 17309 ASSERT(pktp != NULL); 17310 17311 /* 17312 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 17313 * conflict could be due to various reasons like incorrect keys, not 17314 * registered or not reserved etc. So, we return EACCES to the caller. 17315 */ 17316 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 17317 int cmd = SD_GET_PKT_OPCODE(pktp); 17318 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 17319 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 17320 sd_return_failed_command(un, bp, EACCES); 17321 return; 17322 } 17323 } 17324 17325 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 17326 17327 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 17328 if (sd_failfast_enable != 0) { 17329 /* By definition, we must panic here.... */ 17330 sd_panic_for_res_conflict(un); 17331 /*NOTREACHED*/ 17332 } 17333 SD_ERROR(SD_LOG_IO, un, 17334 "sd_handle_resv_conflict: Disk Reserved\n"); 17335 sd_return_failed_command(un, bp, EACCES); 17336 return; 17337 } 17338 17339 /* 17340 * 1147670: retry only if sd_retry_on_reservation_conflict 17341 * property is set (default is 1). Retries will not succeed 17342 * on a disk reserved by another initiator. HA systems 17343 * may reset this via sd.conf to avoid these retries. 17344 * 17345 * Note: The legacy return code for this failure is EIO, however EACCES 17346 * seems more appropriate for a reservation conflict. 17347 */ 17348 if (sd_retry_on_reservation_conflict == 0) { 17349 SD_ERROR(SD_LOG_IO, un, 17350 "sd_handle_resv_conflict: Device Reserved\n"); 17351 sd_return_failed_command(un, bp, EIO); 17352 return; 17353 } 17354 17355 /* 17356 * Retry the command if we can. 17357 * 17358 * Note: The legacy return code for this failure is EIO, however EACCES 17359 * seems more appropriate for a reservation conflict. 17360 */ 17361 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 17362 (clock_t)2, NULL); 17363 } 17364 17365 17366 17367 /* 17368 * Function: sd_pkt_status_qfull 17369 * 17370 * Description: Handle a QUEUE FULL condition from the target. This can 17371 * occur if the HBA does not handle the queue full condition. 17372 * (Basically this means third-party HBAs as Sun HBAs will 17373 * handle the queue full condition.) Note that if there are 17374 * some commands already in the transport, then the queue full 17375 * has occurred because the queue for this nexus is actually 17376 * full. If there are no commands in the transport, then the 17377 * queue full is resulting from some other initiator or lun 17378 * consuming all the resources at the target. 17379 * 17380 * Context: May be called from interrupt context 17381 */ 17382 17383 static void 17384 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 17385 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17386 { 17387 ASSERT(un != NULL); 17388 ASSERT(mutex_owned(SD_MUTEX(un))); 17389 ASSERT(bp != NULL); 17390 ASSERT(xp != NULL); 17391 ASSERT(pktp != NULL); 17392 17393 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17394 "sd_pkt_status_qfull: entry\n"); 17395 17396 /* 17397 * Just lower the QFULL throttle and retry the command. Note that 17398 * we do not limit the number of retries here. 17399 */ 17400 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 17401 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 17402 SD_RESTART_TIMEOUT, NULL); 17403 17404 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17405 "sd_pkt_status_qfull: exit\n"); 17406 } 17407 17408 17409 /* 17410 * Function: sd_reset_target 17411 * 17412 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 17413 * RESET_TARGET, or RESET_ALL. 17414 * 17415 * Context: May be called under interrupt context. 17416 */ 17417 17418 static void 17419 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 17420 { 17421 int rval = 0; 17422 17423 ASSERT(un != NULL); 17424 ASSERT(mutex_owned(SD_MUTEX(un))); 17425 ASSERT(pktp != NULL); 17426 17427 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 17428 17429 /* 17430 * No need to reset if the transport layer has already done so. 17431 */ 17432 if ((pktp->pkt_statistics & 17433 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 17434 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17435 "sd_reset_target: no reset\n"); 17436 return; 17437 } 17438 17439 mutex_exit(SD_MUTEX(un)); 17440 17441 if (un->un_f_allow_bus_device_reset == TRUE) { 17442 if (un->un_f_lun_reset_enabled == TRUE) { 17443 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17444 "sd_reset_target: RESET_LUN\n"); 17445 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 17446 } 17447 if (rval == 0) { 17448 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17449 "sd_reset_target: RESET_TARGET\n"); 17450 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 17451 } 17452 } 17453 17454 if (rval == 0) { 17455 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 17456 "sd_reset_target: RESET_ALL\n"); 17457 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 17458 } 17459 17460 mutex_enter(SD_MUTEX(un)); 17461 17462 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 17463 } 17464 17465 17466 /* 17467 * Function: sd_media_change_task 17468 * 17469 * Description: Recovery action for CDROM to become available. 17470 * 17471 * Context: Executes in a taskq() thread context 17472 */ 17473 17474 static void 17475 sd_media_change_task(void *arg) 17476 { 17477 struct scsi_pkt *pktp = arg; 17478 struct sd_lun *un; 17479 struct buf *bp; 17480 struct sd_xbuf *xp; 17481 int err = 0; 17482 int retry_count = 0; 17483 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 17484 struct sd_sense_info si; 17485 17486 ASSERT(pktp != NULL); 17487 bp = (struct buf *)pktp->pkt_private; 17488 ASSERT(bp != NULL); 17489 xp = SD_GET_XBUF(bp); 17490 ASSERT(xp != NULL); 17491 un = SD_GET_UN(bp); 17492 ASSERT(un != NULL); 17493 ASSERT(!mutex_owned(SD_MUTEX(un))); 17494 ASSERT(un->un_f_monitor_media_state); 17495 17496 si.ssi_severity = SCSI_ERR_INFO; 17497 si.ssi_pfa_flag = FALSE; 17498 17499 /* 17500 * When a reset is issued on a CDROM, it takes a long time to 17501 * recover. First few attempts to read capacity and other things 17502 * related to handling unit attention fail (with a ASC 0x4 and 17503 * ASCQ 0x1). In that case we want to do enough retries and we want 17504 * to limit the retries in other cases of genuine failures like 17505 * no media in drive. 17506 */ 17507 while (retry_count++ < retry_limit) { 17508 if ((err = sd_handle_mchange(un)) == 0) { 17509 break; 17510 } 17511 if (err == EAGAIN) { 17512 retry_limit = SD_UNIT_ATTENTION_RETRY; 17513 } 17514 /* Sleep for 0.5 sec. & try again */ 17515 delay(drv_usectohz(500000)); 17516 } 17517 17518 /* 17519 * Dispatch (retry or fail) the original command here, 17520 * along with appropriate console messages.... 17521 * 17522 * Must grab the mutex before calling sd_retry_command, 17523 * sd_print_sense_msg and sd_return_failed_command. 17524 */ 17525 mutex_enter(SD_MUTEX(un)); 17526 if (err != SD_CMD_SUCCESS) { 17527 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17528 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 17529 si.ssi_severity = SCSI_ERR_FATAL; 17530 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17531 sd_return_failed_command(un, bp, EIO); 17532 } else { 17533 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 17534 &si, EIO, (clock_t)0, NULL); 17535 } 17536 mutex_exit(SD_MUTEX(un)); 17537 } 17538 17539 17540 17541 /* 17542 * Function: sd_handle_mchange 17543 * 17544 * Description: Perform geometry validation & other recovery when CDROM 17545 * has been removed from drive. 17546 * 17547 * Return Code: 0 for success 17548 * errno-type return code of either sd_send_scsi_DOORLOCK() or 17549 * sd_send_scsi_READ_CAPACITY() 17550 * 17551 * Context: Executes in a taskq() thread context 17552 */ 17553 17554 static int 17555 sd_handle_mchange(struct sd_lun *un) 17556 { 17557 uint64_t capacity; 17558 uint32_t lbasize; 17559 int rval; 17560 17561 ASSERT(!mutex_owned(SD_MUTEX(un))); 17562 ASSERT(un->un_f_monitor_media_state); 17563 17564 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 17565 SD_PATH_DIRECT_PRIORITY)) != 0) { 17566 return (rval); 17567 } 17568 17569 mutex_enter(SD_MUTEX(un)); 17570 sd_update_block_info(un, lbasize, capacity); 17571 17572 if (un->un_errstats != NULL) { 17573 struct sd_errstats *stp = 17574 (struct sd_errstats *)un->un_errstats->ks_data; 17575 stp->sd_capacity.value.ui64 = (uint64_t) 17576 ((uint64_t)un->un_blockcount * 17577 (uint64_t)un->un_tgt_blocksize); 17578 } 17579 17580 17581 /* 17582 * Check if the media in the device is writable or not 17583 */ 17584 if (ISCD(un)) 17585 sd_check_for_writable_cd(un, SD_PATH_DIRECT_PRIORITY); 17586 17587 /* 17588 * Note: Maybe let the strategy/partitioning chain worry about getting 17589 * valid geometry. 17590 */ 17591 mutex_exit(SD_MUTEX(un)); 17592 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 17593 17594 17595 if (cmlb_validate(un->un_cmlbhandle, 0, 17596 (void *)SD_PATH_DIRECT_PRIORITY) != 0) { 17597 return (EIO); 17598 } else { 17599 if (un->un_f_pkstats_enabled) { 17600 sd_set_pstats(un); 17601 SD_TRACE(SD_LOG_IO_PARTITION, un, 17602 "sd_handle_mchange: un:0x%p pstats created and " 17603 "set\n", un); 17604 } 17605 } 17606 17607 17608 /* 17609 * Try to lock the door 17610 */ 17611 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 17612 SD_PATH_DIRECT_PRIORITY)); 17613 } 17614 17615 17616 /* 17617 * Function: sd_send_scsi_DOORLOCK 17618 * 17619 * Description: Issue the scsi DOOR LOCK command 17620 * 17621 * Arguments: un - pointer to driver soft state (unit) structure for 17622 * this target. 17623 * flag - SD_REMOVAL_ALLOW 17624 * SD_REMOVAL_PREVENT 17625 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17626 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17627 * to use the USCSI "direct" chain and bypass the normal 17628 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17629 * command is issued as part of an error recovery action. 17630 * 17631 * Return Code: 0 - Success 17632 * errno return code from sd_send_scsi_cmd() 17633 * 17634 * Context: Can sleep. 17635 */ 17636 17637 static int 17638 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 17639 { 17640 union scsi_cdb cdb; 17641 struct uscsi_cmd ucmd_buf; 17642 struct scsi_extended_sense sense_buf; 17643 int status; 17644 17645 ASSERT(un != NULL); 17646 ASSERT(!mutex_owned(SD_MUTEX(un))); 17647 17648 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 17649 17650 /* already determined doorlock is not supported, fake success */ 17651 if (un->un_f_doorlock_supported == FALSE) { 17652 return (0); 17653 } 17654 17655 /* 17656 * If we are ejecting and see an SD_REMOVAL_PREVENT 17657 * ignore the command so we can complete the eject 17658 * operation. 17659 */ 17660 if (flag == SD_REMOVAL_PREVENT) { 17661 mutex_enter(SD_MUTEX(un)); 17662 if (un->un_f_ejecting == TRUE) { 17663 mutex_exit(SD_MUTEX(un)); 17664 return (EAGAIN); 17665 } 17666 mutex_exit(SD_MUTEX(un)); 17667 } 17668 17669 bzero(&cdb, sizeof (cdb)); 17670 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17671 17672 cdb.scc_cmd = SCMD_DOORLOCK; 17673 cdb.cdb_opaque[4] = (uchar_t)flag; 17674 17675 ucmd_buf.uscsi_cdb = (char *)&cdb; 17676 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 17677 ucmd_buf.uscsi_bufaddr = NULL; 17678 ucmd_buf.uscsi_buflen = 0; 17679 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17680 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17681 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 17682 ucmd_buf.uscsi_timeout = 15; 17683 17684 SD_TRACE(SD_LOG_IO, un, 17685 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 17686 17687 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17688 UIO_SYSSPACE, path_flag); 17689 17690 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 17691 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17692 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 17693 /* fake success and skip subsequent doorlock commands */ 17694 un->un_f_doorlock_supported = FALSE; 17695 return (0); 17696 } 17697 17698 return (status); 17699 } 17700 17701 /* 17702 * Function: sd_send_scsi_READ_CAPACITY 17703 * 17704 * Description: This routine uses the scsi READ CAPACITY command to determine 17705 * the device capacity in number of blocks and the device native 17706 * block size. If this function returns a failure, then the 17707 * values in *capp and *lbap are undefined. If the capacity 17708 * returned is 0xffffffff then the lun is too large for a 17709 * normal READ CAPACITY command and the results of a 17710 * READ CAPACITY 16 will be used instead. 17711 * 17712 * Arguments: un - ptr to soft state struct for the target 17713 * capp - ptr to unsigned 64-bit variable to receive the 17714 * capacity value from the command. 17715 * lbap - ptr to unsigned 32-bit varaible to receive the 17716 * block size value from the command 17717 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17718 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17719 * to use the USCSI "direct" chain and bypass the normal 17720 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 17721 * command is issued as part of an error recovery action. 17722 * 17723 * Return Code: 0 - Success 17724 * EIO - IO error 17725 * EACCES - Reservation conflict detected 17726 * EAGAIN - Device is becoming ready 17727 * errno return code from sd_send_scsi_cmd() 17728 * 17729 * Context: Can sleep. Blocks until command completes. 17730 */ 17731 17732 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 17733 17734 static int 17735 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 17736 int path_flag) 17737 { 17738 struct scsi_extended_sense sense_buf; 17739 struct uscsi_cmd ucmd_buf; 17740 union scsi_cdb cdb; 17741 uint32_t *capacity_buf; 17742 uint64_t capacity; 17743 uint32_t lbasize; 17744 int status; 17745 17746 ASSERT(un != NULL); 17747 ASSERT(!mutex_owned(SD_MUTEX(un))); 17748 ASSERT(capp != NULL); 17749 ASSERT(lbap != NULL); 17750 17751 SD_TRACE(SD_LOG_IO, un, 17752 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17753 17754 /* 17755 * First send a READ_CAPACITY command to the target. 17756 * (This command is mandatory under SCSI-2.) 17757 * 17758 * Set up the CDB for the READ_CAPACITY command. The Partial 17759 * Medium Indicator bit is cleared. The address field must be 17760 * zero if the PMI bit is zero. 17761 */ 17762 bzero(&cdb, sizeof (cdb)); 17763 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17764 17765 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 17766 17767 cdb.scc_cmd = SCMD_READ_CAPACITY; 17768 17769 ucmd_buf.uscsi_cdb = (char *)&cdb; 17770 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 17771 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 17772 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 17773 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17774 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17775 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17776 ucmd_buf.uscsi_timeout = 60; 17777 17778 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17779 UIO_SYSSPACE, path_flag); 17780 17781 switch (status) { 17782 case 0: 17783 /* Return failure if we did not get valid capacity data. */ 17784 if (ucmd_buf.uscsi_resid != 0) { 17785 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17786 return (EIO); 17787 } 17788 17789 /* 17790 * Read capacity and block size from the READ CAPACITY 10 data. 17791 * This data may be adjusted later due to device specific 17792 * issues. 17793 * 17794 * According to the SCSI spec, the READ CAPACITY 10 17795 * command returns the following: 17796 * 17797 * bytes 0-3: Maximum logical block address available. 17798 * (MSB in byte:0 & LSB in byte:3) 17799 * 17800 * bytes 4-7: Block length in bytes 17801 * (MSB in byte:4 & LSB in byte:7) 17802 * 17803 */ 17804 capacity = BE_32(capacity_buf[0]); 17805 lbasize = BE_32(capacity_buf[1]); 17806 17807 /* 17808 * Done with capacity_buf 17809 */ 17810 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17811 17812 /* 17813 * if the reported capacity is set to all 0xf's, then 17814 * this disk is too large and requires SBC-2 commands. 17815 * Reissue the request using READ CAPACITY 16. 17816 */ 17817 if (capacity == 0xffffffff) { 17818 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 17819 &lbasize, path_flag); 17820 if (status != 0) { 17821 return (status); 17822 } 17823 } 17824 break; /* Success! */ 17825 case EIO: 17826 switch (ucmd_buf.uscsi_status) { 17827 case STATUS_RESERVATION_CONFLICT: 17828 status = EACCES; 17829 break; 17830 case STATUS_CHECK: 17831 /* 17832 * Check condition; look for ASC/ASCQ of 0x04/0x01 17833 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 17834 */ 17835 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 17836 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 17837 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 17838 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17839 return (EAGAIN); 17840 } 17841 break; 17842 default: 17843 break; 17844 } 17845 /* FALLTHRU */ 17846 default: 17847 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 17848 return (status); 17849 } 17850 17851 /* 17852 * Some ATAPI CD-ROM drives report inaccurate LBA size values 17853 * (2352 and 0 are common) so for these devices always force the value 17854 * to 2048 as required by the ATAPI specs. 17855 */ 17856 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 17857 lbasize = 2048; 17858 } 17859 17860 /* 17861 * Get the maximum LBA value from the READ CAPACITY data. 17862 * Here we assume that the Partial Medium Indicator (PMI) bit 17863 * was cleared when issuing the command. This means that the LBA 17864 * returned from the device is the LBA of the last logical block 17865 * on the logical unit. The actual logical block count will be 17866 * this value plus one. 17867 * 17868 * Currently the capacity is saved in terms of un->un_sys_blocksize, 17869 * so scale the capacity value to reflect this. 17870 */ 17871 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 17872 17873 /* 17874 * Copy the values from the READ CAPACITY command into the space 17875 * provided by the caller. 17876 */ 17877 *capp = capacity; 17878 *lbap = lbasize; 17879 17880 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 17881 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 17882 17883 /* 17884 * Both the lbasize and capacity from the device must be nonzero, 17885 * otherwise we assume that the values are not valid and return 17886 * failure to the caller. (4203735) 17887 */ 17888 if ((capacity == 0) || (lbasize == 0)) { 17889 return (EIO); 17890 } 17891 17892 return (0); 17893 } 17894 17895 /* 17896 * Function: sd_send_scsi_READ_CAPACITY_16 17897 * 17898 * Description: This routine uses the scsi READ CAPACITY 16 command to 17899 * determine the device capacity in number of blocks and the 17900 * device native block size. If this function returns a failure, 17901 * then the values in *capp and *lbap are undefined. 17902 * This routine should always be called by 17903 * sd_send_scsi_READ_CAPACITY which will appy any device 17904 * specific adjustments to capacity and lbasize. 17905 * 17906 * Arguments: un - ptr to soft state struct for the target 17907 * capp - ptr to unsigned 64-bit variable to receive the 17908 * capacity value from the command. 17909 * lbap - ptr to unsigned 32-bit varaible to receive the 17910 * block size value from the command 17911 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 17912 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 17913 * to use the USCSI "direct" chain and bypass the normal 17914 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 17915 * this command is issued as part of an error recovery 17916 * action. 17917 * 17918 * Return Code: 0 - Success 17919 * EIO - IO error 17920 * EACCES - Reservation conflict detected 17921 * EAGAIN - Device is becoming ready 17922 * errno return code from sd_send_scsi_cmd() 17923 * 17924 * Context: Can sleep. Blocks until command completes. 17925 */ 17926 17927 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 17928 17929 static int 17930 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 17931 uint32_t *lbap, int path_flag) 17932 { 17933 struct scsi_extended_sense sense_buf; 17934 struct uscsi_cmd ucmd_buf; 17935 union scsi_cdb cdb; 17936 uint64_t *capacity16_buf; 17937 uint64_t capacity; 17938 uint32_t lbasize; 17939 int status; 17940 17941 ASSERT(un != NULL); 17942 ASSERT(!mutex_owned(SD_MUTEX(un))); 17943 ASSERT(capp != NULL); 17944 ASSERT(lbap != NULL); 17945 17946 SD_TRACE(SD_LOG_IO, un, 17947 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 17948 17949 /* 17950 * First send a READ_CAPACITY_16 command to the target. 17951 * 17952 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 17953 * Medium Indicator bit is cleared. The address field must be 17954 * zero if the PMI bit is zero. 17955 */ 17956 bzero(&cdb, sizeof (cdb)); 17957 bzero(&ucmd_buf, sizeof (ucmd_buf)); 17958 17959 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 17960 17961 ucmd_buf.uscsi_cdb = (char *)&cdb; 17962 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 17963 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 17964 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 17965 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 17966 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 17967 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 17968 ucmd_buf.uscsi_timeout = 60; 17969 17970 /* 17971 * Read Capacity (16) is a Service Action In command. One 17972 * command byte (0x9E) is overloaded for multiple operations, 17973 * with the second CDB byte specifying the desired operation 17974 */ 17975 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 17976 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 17977 17978 /* 17979 * Fill in allocation length field 17980 */ 17981 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 17982 17983 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 17984 UIO_SYSSPACE, path_flag); 17985 17986 switch (status) { 17987 case 0: 17988 /* Return failure if we did not get valid capacity data. */ 17989 if (ucmd_buf.uscsi_resid > 20) { 17990 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 17991 return (EIO); 17992 } 17993 17994 /* 17995 * Read capacity and block size from the READ CAPACITY 10 data. 17996 * This data may be adjusted later due to device specific 17997 * issues. 17998 * 17999 * According to the SCSI spec, the READ CAPACITY 10 18000 * command returns the following: 18001 * 18002 * bytes 0-7: Maximum logical block address available. 18003 * (MSB in byte:0 & LSB in byte:7) 18004 * 18005 * bytes 8-11: Block length in bytes 18006 * (MSB in byte:8 & LSB in byte:11) 18007 * 18008 */ 18009 capacity = BE_64(capacity16_buf[0]); 18010 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 18011 18012 /* 18013 * Done with capacity16_buf 18014 */ 18015 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18016 18017 /* 18018 * if the reported capacity is set to all 0xf's, then 18019 * this disk is too large. This could only happen with 18020 * a device that supports LBAs larger than 64 bits which 18021 * are not defined by any current T10 standards. 18022 */ 18023 if (capacity == 0xffffffffffffffff) { 18024 return (EIO); 18025 } 18026 break; /* Success! */ 18027 case EIO: 18028 switch (ucmd_buf.uscsi_status) { 18029 case STATUS_RESERVATION_CONFLICT: 18030 status = EACCES; 18031 break; 18032 case STATUS_CHECK: 18033 /* 18034 * Check condition; look for ASC/ASCQ of 0x04/0x01 18035 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 18036 */ 18037 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18038 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 18039 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 18040 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18041 return (EAGAIN); 18042 } 18043 break; 18044 default: 18045 break; 18046 } 18047 /* FALLTHRU */ 18048 default: 18049 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 18050 return (status); 18051 } 18052 18053 *capp = capacity; 18054 *lbap = lbasize; 18055 18056 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 18057 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 18058 18059 return (0); 18060 } 18061 18062 18063 /* 18064 * Function: sd_send_scsi_START_STOP_UNIT 18065 * 18066 * Description: Issue a scsi START STOP UNIT command to the target. 18067 * 18068 * Arguments: un - pointer to driver soft state (unit) structure for 18069 * this target. 18070 * flag - SD_TARGET_START 18071 * SD_TARGET_STOP 18072 * SD_TARGET_EJECT 18073 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 18074 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 18075 * to use the USCSI "direct" chain and bypass the normal 18076 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 18077 * command is issued as part of an error recovery action. 18078 * 18079 * Return Code: 0 - Success 18080 * EIO - IO error 18081 * EACCES - Reservation conflict detected 18082 * ENXIO - Not Ready, medium not present 18083 * errno return code from sd_send_scsi_cmd() 18084 * 18085 * Context: Can sleep. 18086 */ 18087 18088 static int 18089 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 18090 { 18091 struct scsi_extended_sense sense_buf; 18092 union scsi_cdb cdb; 18093 struct uscsi_cmd ucmd_buf; 18094 int status; 18095 18096 ASSERT(un != NULL); 18097 ASSERT(!mutex_owned(SD_MUTEX(un))); 18098 18099 SD_TRACE(SD_LOG_IO, un, 18100 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 18101 18102 if (un->un_f_check_start_stop && 18103 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 18104 (un->un_f_start_stop_supported != TRUE)) { 18105 return (0); 18106 } 18107 18108 /* 18109 * If we are performing an eject operation and 18110 * we receive any command other than SD_TARGET_EJECT 18111 * we should immediately return. 18112 */ 18113 if (flag != SD_TARGET_EJECT) { 18114 mutex_enter(SD_MUTEX(un)); 18115 if (un->un_f_ejecting == TRUE) { 18116 mutex_exit(SD_MUTEX(un)); 18117 return (EAGAIN); 18118 } 18119 mutex_exit(SD_MUTEX(un)); 18120 } 18121 18122 bzero(&cdb, sizeof (cdb)); 18123 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18124 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18125 18126 cdb.scc_cmd = SCMD_START_STOP; 18127 cdb.cdb_opaque[4] = (uchar_t)flag; 18128 18129 ucmd_buf.uscsi_cdb = (char *)&cdb; 18130 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18131 ucmd_buf.uscsi_bufaddr = NULL; 18132 ucmd_buf.uscsi_buflen = 0; 18133 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18134 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18135 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18136 ucmd_buf.uscsi_timeout = 200; 18137 18138 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18139 UIO_SYSSPACE, path_flag); 18140 18141 switch (status) { 18142 case 0: 18143 break; /* Success! */ 18144 case EIO: 18145 switch (ucmd_buf.uscsi_status) { 18146 case STATUS_RESERVATION_CONFLICT: 18147 status = EACCES; 18148 break; 18149 case STATUS_CHECK: 18150 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 18151 switch (scsi_sense_key( 18152 (uint8_t *)&sense_buf)) { 18153 case KEY_ILLEGAL_REQUEST: 18154 status = ENOTSUP; 18155 break; 18156 case KEY_NOT_READY: 18157 if (scsi_sense_asc( 18158 (uint8_t *)&sense_buf) 18159 == 0x3A) { 18160 status = ENXIO; 18161 } 18162 break; 18163 default: 18164 break; 18165 } 18166 } 18167 break; 18168 default: 18169 break; 18170 } 18171 break; 18172 default: 18173 break; 18174 } 18175 18176 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 18177 18178 return (status); 18179 } 18180 18181 18182 /* 18183 * Function: sd_start_stop_unit_callback 18184 * 18185 * Description: timeout(9F) callback to begin recovery process for a 18186 * device that has spun down. 18187 * 18188 * Arguments: arg - pointer to associated softstate struct. 18189 * 18190 * Context: Executes in a timeout(9F) thread context 18191 */ 18192 18193 static void 18194 sd_start_stop_unit_callback(void *arg) 18195 { 18196 struct sd_lun *un = arg; 18197 ASSERT(un != NULL); 18198 ASSERT(!mutex_owned(SD_MUTEX(un))); 18199 18200 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 18201 18202 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 18203 } 18204 18205 18206 /* 18207 * Function: sd_start_stop_unit_task 18208 * 18209 * Description: Recovery procedure when a drive is spun down. 18210 * 18211 * Arguments: arg - pointer to associated softstate struct. 18212 * 18213 * Context: Executes in a taskq() thread context 18214 */ 18215 18216 static void 18217 sd_start_stop_unit_task(void *arg) 18218 { 18219 struct sd_lun *un = arg; 18220 18221 ASSERT(un != NULL); 18222 ASSERT(!mutex_owned(SD_MUTEX(un))); 18223 18224 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 18225 18226 /* 18227 * Some unformatted drives report not ready error, no need to 18228 * restart if format has been initiated. 18229 */ 18230 mutex_enter(SD_MUTEX(un)); 18231 if (un->un_f_format_in_progress == TRUE) { 18232 mutex_exit(SD_MUTEX(un)); 18233 return; 18234 } 18235 mutex_exit(SD_MUTEX(un)); 18236 18237 /* 18238 * When a START STOP command is issued from here, it is part of a 18239 * failure recovery operation and must be issued before any other 18240 * commands, including any pending retries. Thus it must be sent 18241 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 18242 * succeeds or not, we will start I/O after the attempt. 18243 */ 18244 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 18245 SD_PATH_DIRECT_PRIORITY); 18246 18247 /* 18248 * The above call blocks until the START_STOP_UNIT command completes. 18249 * Now that it has completed, we must re-try the original IO that 18250 * received the NOT READY condition in the first place. There are 18251 * three possible conditions here: 18252 * 18253 * (1) The original IO is on un_retry_bp. 18254 * (2) The original IO is on the regular wait queue, and un_retry_bp 18255 * is NULL. 18256 * (3) The original IO is on the regular wait queue, and un_retry_bp 18257 * points to some other, unrelated bp. 18258 * 18259 * For each case, we must call sd_start_cmds() with un_retry_bp 18260 * as the argument. If un_retry_bp is NULL, this will initiate 18261 * processing of the regular wait queue. If un_retry_bp is not NULL, 18262 * then this will process the bp on un_retry_bp. That may or may not 18263 * be the original IO, but that does not matter: the important thing 18264 * is to keep the IO processing going at this point. 18265 * 18266 * Note: This is a very specific error recovery sequence associated 18267 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 18268 * serialize the I/O with completion of the spin-up. 18269 */ 18270 mutex_enter(SD_MUTEX(un)); 18271 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18272 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 18273 un, un->un_retry_bp); 18274 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 18275 sd_start_cmds(un, un->un_retry_bp); 18276 mutex_exit(SD_MUTEX(un)); 18277 18278 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 18279 } 18280 18281 18282 /* 18283 * Function: sd_send_scsi_INQUIRY 18284 * 18285 * Description: Issue the scsi INQUIRY command. 18286 * 18287 * Arguments: un 18288 * bufaddr 18289 * buflen 18290 * evpd 18291 * page_code 18292 * page_length 18293 * 18294 * Return Code: 0 - Success 18295 * errno return code from sd_send_scsi_cmd() 18296 * 18297 * Context: Can sleep. Does not return until command is completed. 18298 */ 18299 18300 static int 18301 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 18302 uchar_t evpd, uchar_t page_code, size_t *residp) 18303 { 18304 union scsi_cdb cdb; 18305 struct uscsi_cmd ucmd_buf; 18306 int status; 18307 18308 ASSERT(un != NULL); 18309 ASSERT(!mutex_owned(SD_MUTEX(un))); 18310 ASSERT(bufaddr != NULL); 18311 18312 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 18313 18314 bzero(&cdb, sizeof (cdb)); 18315 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18316 bzero(bufaddr, buflen); 18317 18318 cdb.scc_cmd = SCMD_INQUIRY; 18319 cdb.cdb_opaque[1] = evpd; 18320 cdb.cdb_opaque[2] = page_code; 18321 FORMG0COUNT(&cdb, buflen); 18322 18323 ucmd_buf.uscsi_cdb = (char *)&cdb; 18324 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18325 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 18326 ucmd_buf.uscsi_buflen = buflen; 18327 ucmd_buf.uscsi_rqbuf = NULL; 18328 ucmd_buf.uscsi_rqlen = 0; 18329 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 18330 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 18331 18332 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18333 UIO_SYSSPACE, SD_PATH_DIRECT); 18334 18335 if ((status == 0) && (residp != NULL)) { 18336 *residp = ucmd_buf.uscsi_resid; 18337 } 18338 18339 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 18340 18341 return (status); 18342 } 18343 18344 18345 /* 18346 * Function: sd_send_scsi_TEST_UNIT_READY 18347 * 18348 * Description: Issue the scsi TEST UNIT READY command. 18349 * This routine can be told to set the flag USCSI_DIAGNOSE to 18350 * prevent retrying failed commands. Use this when the intent 18351 * is either to check for device readiness, to clear a Unit 18352 * Attention, or to clear any outstanding sense data. 18353 * However under specific conditions the expected behavior 18354 * is for retries to bring a device ready, so use the flag 18355 * with caution. 18356 * 18357 * Arguments: un 18358 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 18359 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 18360 * 0: dont check for media present, do retries on cmd. 18361 * 18362 * Return Code: 0 - Success 18363 * EIO - IO error 18364 * EACCES - Reservation conflict detected 18365 * ENXIO - Not Ready, medium not present 18366 * errno return code from sd_send_scsi_cmd() 18367 * 18368 * Context: Can sleep. Does not return until command is completed. 18369 */ 18370 18371 static int 18372 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 18373 { 18374 struct scsi_extended_sense sense_buf; 18375 union scsi_cdb cdb; 18376 struct uscsi_cmd ucmd_buf; 18377 int status; 18378 18379 ASSERT(un != NULL); 18380 ASSERT(!mutex_owned(SD_MUTEX(un))); 18381 18382 SD_TRACE(SD_LOG_IO, un, 18383 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 18384 18385 /* 18386 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 18387 * timeouts when they receive a TUR and the queue is not empty. Check 18388 * the configuration flag set during attach (indicating the drive has 18389 * this firmware bug) and un_ncmds_in_transport before issuing the 18390 * TUR. If there are 18391 * pending commands return success, this is a bit arbitrary but is ok 18392 * for non-removables (i.e. the eliteI disks) and non-clustering 18393 * configurations. 18394 */ 18395 if (un->un_f_cfg_tur_check == TRUE) { 18396 mutex_enter(SD_MUTEX(un)); 18397 if (un->un_ncmds_in_transport != 0) { 18398 mutex_exit(SD_MUTEX(un)); 18399 return (0); 18400 } 18401 mutex_exit(SD_MUTEX(un)); 18402 } 18403 18404 bzero(&cdb, sizeof (cdb)); 18405 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18406 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18407 18408 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 18409 18410 ucmd_buf.uscsi_cdb = (char *)&cdb; 18411 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 18412 ucmd_buf.uscsi_bufaddr = NULL; 18413 ucmd_buf.uscsi_buflen = 0; 18414 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18415 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18416 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18417 18418 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 18419 if ((flag & SD_DONT_RETRY_TUR) != 0) { 18420 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 18421 } 18422 ucmd_buf.uscsi_timeout = 60; 18423 18424 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18425 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : 18426 SD_PATH_STANDARD)); 18427 18428 switch (status) { 18429 case 0: 18430 break; /* Success! */ 18431 case EIO: 18432 switch (ucmd_buf.uscsi_status) { 18433 case STATUS_RESERVATION_CONFLICT: 18434 status = EACCES; 18435 break; 18436 case STATUS_CHECK: 18437 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 18438 break; 18439 } 18440 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18441 (scsi_sense_key((uint8_t *)&sense_buf) == 18442 KEY_NOT_READY) && 18443 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 18444 status = ENXIO; 18445 } 18446 break; 18447 default: 18448 break; 18449 } 18450 break; 18451 default: 18452 break; 18453 } 18454 18455 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 18456 18457 return (status); 18458 } 18459 18460 18461 /* 18462 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 18463 * 18464 * Description: Issue the scsi PERSISTENT RESERVE IN command. 18465 * 18466 * Arguments: un 18467 * 18468 * Return Code: 0 - Success 18469 * EACCES 18470 * ENOTSUP 18471 * errno return code from sd_send_scsi_cmd() 18472 * 18473 * Context: Can sleep. Does not return until command is completed. 18474 */ 18475 18476 static int 18477 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 18478 uint16_t data_len, uchar_t *data_bufp) 18479 { 18480 struct scsi_extended_sense sense_buf; 18481 union scsi_cdb cdb; 18482 struct uscsi_cmd ucmd_buf; 18483 int status; 18484 int no_caller_buf = FALSE; 18485 18486 ASSERT(un != NULL); 18487 ASSERT(!mutex_owned(SD_MUTEX(un))); 18488 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 18489 18490 SD_TRACE(SD_LOG_IO, un, 18491 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 18492 18493 bzero(&cdb, sizeof (cdb)); 18494 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18495 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18496 if (data_bufp == NULL) { 18497 /* Allocate a default buf if the caller did not give one */ 18498 ASSERT(data_len == 0); 18499 data_len = MHIOC_RESV_KEY_SIZE; 18500 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 18501 no_caller_buf = TRUE; 18502 } 18503 18504 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 18505 cdb.cdb_opaque[1] = usr_cmd; 18506 FORMG1COUNT(&cdb, data_len); 18507 18508 ucmd_buf.uscsi_cdb = (char *)&cdb; 18509 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18510 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 18511 ucmd_buf.uscsi_buflen = data_len; 18512 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18513 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18514 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 18515 ucmd_buf.uscsi_timeout = 60; 18516 18517 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18518 UIO_SYSSPACE, SD_PATH_STANDARD); 18519 18520 switch (status) { 18521 case 0: 18522 break; /* Success! */ 18523 case EIO: 18524 switch (ucmd_buf.uscsi_status) { 18525 case STATUS_RESERVATION_CONFLICT: 18526 status = EACCES; 18527 break; 18528 case STATUS_CHECK: 18529 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18530 (scsi_sense_key((uint8_t *)&sense_buf) == 18531 KEY_ILLEGAL_REQUEST)) { 18532 status = ENOTSUP; 18533 } 18534 break; 18535 default: 18536 break; 18537 } 18538 break; 18539 default: 18540 break; 18541 } 18542 18543 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 18544 18545 if (no_caller_buf == TRUE) { 18546 kmem_free(data_bufp, data_len); 18547 } 18548 18549 return (status); 18550 } 18551 18552 18553 /* 18554 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 18555 * 18556 * Description: This routine is the driver entry point for handling CD-ROM 18557 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 18558 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 18559 * device. 18560 * 18561 * Arguments: un - Pointer to soft state struct for the target. 18562 * usr_cmd SCSI-3 reservation facility command (one of 18563 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 18564 * SD_SCSI3_PREEMPTANDABORT) 18565 * usr_bufp - user provided pointer register, reserve descriptor or 18566 * preempt and abort structure (mhioc_register_t, 18567 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 18568 * 18569 * Return Code: 0 - Success 18570 * EACCES 18571 * ENOTSUP 18572 * errno return code from sd_send_scsi_cmd() 18573 * 18574 * Context: Can sleep. Does not return until command is completed. 18575 */ 18576 18577 static int 18578 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 18579 uchar_t *usr_bufp) 18580 { 18581 struct scsi_extended_sense sense_buf; 18582 union scsi_cdb cdb; 18583 struct uscsi_cmd ucmd_buf; 18584 int status; 18585 uchar_t data_len = sizeof (sd_prout_t); 18586 sd_prout_t *prp; 18587 18588 ASSERT(un != NULL); 18589 ASSERT(!mutex_owned(SD_MUTEX(un))); 18590 ASSERT(data_len == 24); /* required by scsi spec */ 18591 18592 SD_TRACE(SD_LOG_IO, un, 18593 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 18594 18595 if (usr_bufp == NULL) { 18596 return (EINVAL); 18597 } 18598 18599 bzero(&cdb, sizeof (cdb)); 18600 bzero(&ucmd_buf, sizeof (ucmd_buf)); 18601 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 18602 prp = kmem_zalloc(data_len, KM_SLEEP); 18603 18604 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 18605 cdb.cdb_opaque[1] = usr_cmd; 18606 FORMG1COUNT(&cdb, data_len); 18607 18608 ucmd_buf.uscsi_cdb = (char *)&cdb; 18609 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 18610 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 18611 ucmd_buf.uscsi_buflen = data_len; 18612 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 18613 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 18614 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 18615 ucmd_buf.uscsi_timeout = 60; 18616 18617 switch (usr_cmd) { 18618 case SD_SCSI3_REGISTER: { 18619 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 18620 18621 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18622 bcopy(ptr->newkey.key, prp->service_key, 18623 MHIOC_RESV_KEY_SIZE); 18624 prp->aptpl = ptr->aptpl; 18625 break; 18626 } 18627 case SD_SCSI3_RESERVE: 18628 case SD_SCSI3_RELEASE: { 18629 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 18630 18631 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18632 prp->scope_address = BE_32(ptr->scope_specific_addr); 18633 cdb.cdb_opaque[2] = ptr->type; 18634 break; 18635 } 18636 case SD_SCSI3_PREEMPTANDABORT: { 18637 mhioc_preemptandabort_t *ptr = 18638 (mhioc_preemptandabort_t *)usr_bufp; 18639 18640 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 18641 bcopy(ptr->victim_key.key, prp->service_key, 18642 MHIOC_RESV_KEY_SIZE); 18643 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 18644 cdb.cdb_opaque[2] = ptr->resvdesc.type; 18645 ucmd_buf.uscsi_flags |= USCSI_HEAD; 18646 break; 18647 } 18648 case SD_SCSI3_REGISTERANDIGNOREKEY: 18649 { 18650 mhioc_registerandignorekey_t *ptr; 18651 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 18652 bcopy(ptr->newkey.key, 18653 prp->service_key, MHIOC_RESV_KEY_SIZE); 18654 prp->aptpl = ptr->aptpl; 18655 break; 18656 } 18657 default: 18658 ASSERT(FALSE); 18659 break; 18660 } 18661 18662 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 18663 UIO_SYSSPACE, SD_PATH_STANDARD); 18664 18665 switch (status) { 18666 case 0: 18667 break; /* Success! */ 18668 case EIO: 18669 switch (ucmd_buf.uscsi_status) { 18670 case STATUS_RESERVATION_CONFLICT: 18671 status = EACCES; 18672 break; 18673 case STATUS_CHECK: 18674 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 18675 (scsi_sense_key((uint8_t *)&sense_buf) == 18676 KEY_ILLEGAL_REQUEST)) { 18677 status = ENOTSUP; 18678 } 18679 break; 18680 default: 18681 break; 18682 } 18683 break; 18684 default: 18685 break; 18686 } 18687 18688 kmem_free(prp, data_len); 18689 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 18690 return (status); 18691 } 18692 18693 18694 /* 18695 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 18696 * 18697 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 18698 * 18699 * Arguments: un - pointer to the target's soft state struct 18700 * dkc - pointer to the callback structure 18701 * 18702 * Return Code: 0 - success 18703 * errno-type error code 18704 * 18705 * Context: kernel thread context only. 18706 * 18707 * _______________________________________________________________ 18708 * | dkc_flag & | dkc_callback | DKIOCFLUSHWRITECACHE | 18709 * |FLUSH_VOLATILE| | operation | 18710 * |______________|______________|_________________________________| 18711 * | 0 | NULL | Synchronous flush on both | 18712 * | | | volatile and non-volatile cache | 18713 * |______________|______________|_________________________________| 18714 * | 1 | NULL | Synchronous flush on volatile | 18715 * | | | cache; disk drivers may suppress| 18716 * | | | flush if disk table indicates | 18717 * | | | non-volatile cache | 18718 * |______________|______________|_________________________________| 18719 * | 0 | !NULL | Asynchronous flush on both | 18720 * | | | volatile and non-volatile cache;| 18721 * |______________|______________|_________________________________| 18722 * | 1 | !NULL | Asynchronous flush on volatile | 18723 * | | | cache; disk drivers may suppress| 18724 * | | | flush if disk table indicates | 18725 * | | | non-volatile cache | 18726 * |______________|______________|_________________________________| 18727 * 18728 */ 18729 18730 static int 18731 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 18732 { 18733 struct sd_uscsi_info *uip; 18734 struct uscsi_cmd *uscmd; 18735 union scsi_cdb *cdb; 18736 struct buf *bp; 18737 int rval = 0; 18738 int is_async; 18739 18740 SD_TRACE(SD_LOG_IO, un, 18741 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 18742 18743 ASSERT(un != NULL); 18744 ASSERT(!mutex_owned(SD_MUTEX(un))); 18745 18746 if (dkc == NULL || dkc->dkc_callback == NULL) { 18747 is_async = FALSE; 18748 } else { 18749 is_async = TRUE; 18750 } 18751 18752 mutex_enter(SD_MUTEX(un)); 18753 /* check whether cache flush should be suppressed */ 18754 if (un->un_f_suppress_cache_flush == TRUE) { 18755 mutex_exit(SD_MUTEX(un)); 18756 /* 18757 * suppress the cache flush if the device is told to do 18758 * so by sd.conf or disk table 18759 */ 18760 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \ 18761 skip the cache flush since suppress_cache_flush is %d!\n", 18762 un->un_f_suppress_cache_flush); 18763 18764 if (is_async == TRUE) { 18765 /* invoke callback for asynchronous flush */ 18766 (*dkc->dkc_callback)(dkc->dkc_cookie, 0); 18767 } 18768 return (rval); 18769 } 18770 mutex_exit(SD_MUTEX(un)); 18771 18772 /* 18773 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be 18774 * set properly 18775 */ 18776 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 18777 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 18778 18779 mutex_enter(SD_MUTEX(un)); 18780 if (dkc != NULL && un->un_f_sync_nv_supported && 18781 (dkc->dkc_flag & FLUSH_VOLATILE)) { 18782 /* 18783 * if the device supports SYNC_NV bit, turn on 18784 * the SYNC_NV bit to only flush volatile cache 18785 */ 18786 cdb->cdb_un.tag |= SD_SYNC_NV_BIT; 18787 } 18788 mutex_exit(SD_MUTEX(un)); 18789 18790 /* 18791 * First get some memory for the uscsi_cmd struct and cdb 18792 * and initialize for SYNCHRONIZE_CACHE cmd. 18793 */ 18794 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 18795 uscmd->uscsi_cdblen = CDB_GROUP1; 18796 uscmd->uscsi_cdb = (caddr_t)cdb; 18797 uscmd->uscsi_bufaddr = NULL; 18798 uscmd->uscsi_buflen = 0; 18799 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 18800 uscmd->uscsi_rqlen = SENSE_LENGTH; 18801 uscmd->uscsi_rqresid = SENSE_LENGTH; 18802 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 18803 uscmd->uscsi_timeout = sd_io_time; 18804 18805 /* 18806 * Allocate an sd_uscsi_info struct and fill it with the info 18807 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 18808 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 18809 * since we allocate the buf here in this function, we do not 18810 * need to preserve the prior contents of b_private. 18811 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 18812 */ 18813 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 18814 uip->ui_flags = SD_PATH_DIRECT; 18815 uip->ui_cmdp = uscmd; 18816 18817 bp = getrbuf(KM_SLEEP); 18818 bp->b_private = uip; 18819 18820 /* 18821 * Setup buffer to carry uscsi request. 18822 */ 18823 bp->b_flags = B_BUSY; 18824 bp->b_bcount = 0; 18825 bp->b_blkno = 0; 18826 18827 if (is_async == TRUE) { 18828 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 18829 uip->ui_dkc = *dkc; 18830 } 18831 18832 bp->b_edev = SD_GET_DEV(un); 18833 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 18834 18835 (void) sd_uscsi_strategy(bp); 18836 18837 /* 18838 * If synchronous request, wait for completion 18839 * If async just return and let b_iodone callback 18840 * cleanup. 18841 * NOTE: On return, u_ncmds_in_driver will be decremented, 18842 * but it was also incremented in sd_uscsi_strategy(), so 18843 * we should be ok. 18844 */ 18845 if (is_async == FALSE) { 18846 (void) biowait(bp); 18847 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 18848 } 18849 18850 return (rval); 18851 } 18852 18853 18854 static int 18855 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 18856 { 18857 struct sd_uscsi_info *uip; 18858 struct uscsi_cmd *uscmd; 18859 uint8_t *sense_buf; 18860 struct sd_lun *un; 18861 int status; 18862 union scsi_cdb *cdb; 18863 18864 uip = (struct sd_uscsi_info *)(bp->b_private); 18865 ASSERT(uip != NULL); 18866 18867 uscmd = uip->ui_cmdp; 18868 ASSERT(uscmd != NULL); 18869 18870 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 18871 ASSERT(sense_buf != NULL); 18872 18873 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 18874 ASSERT(un != NULL); 18875 18876 cdb = (union scsi_cdb *)uscmd->uscsi_cdb; 18877 18878 status = geterror(bp); 18879 switch (status) { 18880 case 0: 18881 break; /* Success! */ 18882 case EIO: 18883 switch (uscmd->uscsi_status) { 18884 case STATUS_RESERVATION_CONFLICT: 18885 /* Ignore reservation conflict */ 18886 status = 0; 18887 goto done; 18888 18889 case STATUS_CHECK: 18890 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 18891 (scsi_sense_key(sense_buf) == 18892 KEY_ILLEGAL_REQUEST)) { 18893 /* Ignore Illegal Request error */ 18894 if (cdb->cdb_un.tag|SD_SYNC_NV_BIT) { 18895 mutex_enter(SD_MUTEX(un)); 18896 un->un_f_sync_nv_supported = FALSE; 18897 mutex_exit(SD_MUTEX(un)); 18898 status = 0; 18899 SD_TRACE(SD_LOG_IO, un, 18900 "un_f_sync_nv_supported \ 18901 is set to false.\n"); 18902 goto done; 18903 } 18904 18905 mutex_enter(SD_MUTEX(un)); 18906 un->un_f_sync_cache_supported = FALSE; 18907 mutex_exit(SD_MUTEX(un)); 18908 SD_TRACE(SD_LOG_IO, un, 18909 "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \ 18910 un_f_sync_cache_supported set to false \ 18911 with asc = %x, ascq = %x\n", 18912 scsi_sense_asc(sense_buf), 18913 scsi_sense_ascq(sense_buf)); 18914 status = ENOTSUP; 18915 goto done; 18916 } 18917 break; 18918 default: 18919 break; 18920 } 18921 /* FALLTHRU */ 18922 default: 18923 /* 18924 * Don't log an error message if this device 18925 * has removable media. 18926 */ 18927 if (!un->un_f_has_removable_media) { 18928 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18929 "SYNCHRONIZE CACHE command failed (%d)\n", status); 18930 } 18931 break; 18932 } 18933 18934 done: 18935 if (uip->ui_dkc.dkc_callback != NULL) { 18936 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 18937 } 18938 18939 ASSERT((bp->b_flags & B_REMAPPED) == 0); 18940 freerbuf(bp); 18941 kmem_free(uip, sizeof (struct sd_uscsi_info)); 18942 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 18943 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 18944 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 18945 18946 return (status); 18947 } 18948 18949 18950 /* 18951 * Function: sd_send_scsi_GET_CONFIGURATION 18952 * 18953 * Description: Issues the get configuration command to the device. 18954 * Called from sd_check_for_writable_cd & sd_get_media_info 18955 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 18956 * Arguments: un 18957 * ucmdbuf 18958 * rqbuf 18959 * rqbuflen 18960 * bufaddr 18961 * buflen 18962 * path_flag 18963 * 18964 * Return Code: 0 - Success 18965 * errno return code from sd_send_scsi_cmd() 18966 * 18967 * Context: Can sleep. Does not return until command is completed. 18968 * 18969 */ 18970 18971 static int 18972 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 18973 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen, 18974 int path_flag) 18975 { 18976 char cdb[CDB_GROUP1]; 18977 int status; 18978 18979 ASSERT(un != NULL); 18980 ASSERT(!mutex_owned(SD_MUTEX(un))); 18981 ASSERT(bufaddr != NULL); 18982 ASSERT(ucmdbuf != NULL); 18983 ASSERT(rqbuf != NULL); 18984 18985 SD_TRACE(SD_LOG_IO, un, 18986 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 18987 18988 bzero(cdb, sizeof (cdb)); 18989 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 18990 bzero(rqbuf, rqbuflen); 18991 bzero(bufaddr, buflen); 18992 18993 /* 18994 * Set up cdb field for the get configuration command. 18995 */ 18996 cdb[0] = SCMD_GET_CONFIGURATION; 18997 cdb[1] = 0x02; /* Requested Type */ 18998 cdb[8] = SD_PROFILE_HEADER_LEN; 18999 ucmdbuf->uscsi_cdb = cdb; 19000 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19001 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19002 ucmdbuf->uscsi_buflen = buflen; 19003 ucmdbuf->uscsi_timeout = sd_io_time; 19004 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19005 ucmdbuf->uscsi_rqlen = rqbuflen; 19006 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19007 19008 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 19009 UIO_SYSSPACE, path_flag); 19010 19011 switch (status) { 19012 case 0: 19013 break; /* Success! */ 19014 case EIO: 19015 switch (ucmdbuf->uscsi_status) { 19016 case STATUS_RESERVATION_CONFLICT: 19017 status = EACCES; 19018 break; 19019 default: 19020 break; 19021 } 19022 break; 19023 default: 19024 break; 19025 } 19026 19027 if (status == 0) { 19028 SD_DUMP_MEMORY(un, SD_LOG_IO, 19029 "sd_send_scsi_GET_CONFIGURATION: data", 19030 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19031 } 19032 19033 SD_TRACE(SD_LOG_IO, un, 19034 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 19035 19036 return (status); 19037 } 19038 19039 /* 19040 * Function: sd_send_scsi_feature_GET_CONFIGURATION 19041 * 19042 * Description: Issues the get configuration command to the device to 19043 * retrieve a specific feature. Called from 19044 * sd_check_for_writable_cd & sd_set_mmc_caps. 19045 * Arguments: un 19046 * ucmdbuf 19047 * rqbuf 19048 * rqbuflen 19049 * bufaddr 19050 * buflen 19051 * feature 19052 * 19053 * Return Code: 0 - Success 19054 * errno return code from sd_send_scsi_cmd() 19055 * 19056 * Context: Can sleep. Does not return until command is completed. 19057 * 19058 */ 19059 static int 19060 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 19061 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 19062 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag) 19063 { 19064 char cdb[CDB_GROUP1]; 19065 int status; 19066 19067 ASSERT(un != NULL); 19068 ASSERT(!mutex_owned(SD_MUTEX(un))); 19069 ASSERT(bufaddr != NULL); 19070 ASSERT(ucmdbuf != NULL); 19071 ASSERT(rqbuf != NULL); 19072 19073 SD_TRACE(SD_LOG_IO, un, 19074 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 19075 19076 bzero(cdb, sizeof (cdb)); 19077 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 19078 bzero(rqbuf, rqbuflen); 19079 bzero(bufaddr, buflen); 19080 19081 /* 19082 * Set up cdb field for the get configuration command. 19083 */ 19084 cdb[0] = SCMD_GET_CONFIGURATION; 19085 cdb[1] = 0x02; /* Requested Type */ 19086 cdb[3] = feature; 19087 cdb[8] = buflen; 19088 ucmdbuf->uscsi_cdb = cdb; 19089 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 19090 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 19091 ucmdbuf->uscsi_buflen = buflen; 19092 ucmdbuf->uscsi_timeout = sd_io_time; 19093 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 19094 ucmdbuf->uscsi_rqlen = rqbuflen; 19095 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 19096 19097 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, FKIOCTL, 19098 UIO_SYSSPACE, path_flag); 19099 19100 switch (status) { 19101 case 0: 19102 break; /* Success! */ 19103 case EIO: 19104 switch (ucmdbuf->uscsi_status) { 19105 case STATUS_RESERVATION_CONFLICT: 19106 status = EACCES; 19107 break; 19108 default: 19109 break; 19110 } 19111 break; 19112 default: 19113 break; 19114 } 19115 19116 if (status == 0) { 19117 SD_DUMP_MEMORY(un, SD_LOG_IO, 19118 "sd_send_scsi_feature_GET_CONFIGURATION: data", 19119 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 19120 } 19121 19122 SD_TRACE(SD_LOG_IO, un, 19123 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 19124 19125 return (status); 19126 } 19127 19128 19129 /* 19130 * Function: sd_send_scsi_MODE_SENSE 19131 * 19132 * Description: Utility function for issuing a scsi MODE SENSE command. 19133 * Note: This routine uses a consistent implementation for Group0, 19134 * Group1, and Group2 commands across all platforms. ATAPI devices 19135 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19136 * 19137 * Arguments: un - pointer to the softstate struct for the target. 19138 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19139 * CDB_GROUP[1|2] (10 byte). 19140 * bufaddr - buffer for page data retrieved from the target. 19141 * buflen - size of page to be retrieved. 19142 * page_code - page code of data to be retrieved from the target. 19143 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19144 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19145 * to use the USCSI "direct" chain and bypass the normal 19146 * command waitq. 19147 * 19148 * Return Code: 0 - Success 19149 * errno return code from sd_send_scsi_cmd() 19150 * 19151 * Context: Can sleep. Does not return until command is completed. 19152 */ 19153 19154 static int 19155 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19156 size_t buflen, uchar_t page_code, int path_flag) 19157 { 19158 struct scsi_extended_sense sense_buf; 19159 union scsi_cdb cdb; 19160 struct uscsi_cmd ucmd_buf; 19161 int status; 19162 int headlen; 19163 19164 ASSERT(un != NULL); 19165 ASSERT(!mutex_owned(SD_MUTEX(un))); 19166 ASSERT(bufaddr != NULL); 19167 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19168 (cdbsize == CDB_GROUP2)); 19169 19170 SD_TRACE(SD_LOG_IO, un, 19171 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 19172 19173 bzero(&cdb, sizeof (cdb)); 19174 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19175 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19176 bzero(bufaddr, buflen); 19177 19178 if (cdbsize == CDB_GROUP0) { 19179 cdb.scc_cmd = SCMD_MODE_SENSE; 19180 cdb.cdb_opaque[2] = page_code; 19181 FORMG0COUNT(&cdb, buflen); 19182 headlen = MODE_HEADER_LENGTH; 19183 } else { 19184 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 19185 cdb.cdb_opaque[2] = page_code; 19186 FORMG1COUNT(&cdb, buflen); 19187 headlen = MODE_HEADER_LENGTH_GRP2; 19188 } 19189 19190 ASSERT(headlen <= buflen); 19191 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19192 19193 ucmd_buf.uscsi_cdb = (char *)&cdb; 19194 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19195 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19196 ucmd_buf.uscsi_buflen = buflen; 19197 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19198 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19199 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19200 ucmd_buf.uscsi_timeout = 60; 19201 19202 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19203 UIO_SYSSPACE, path_flag); 19204 19205 switch (status) { 19206 case 0: 19207 /* 19208 * sr_check_wp() uses 0x3f page code and check the header of 19209 * mode page to determine if target device is write-protected. 19210 * But some USB devices return 0 bytes for 0x3f page code. For 19211 * this case, make sure that mode page header is returned at 19212 * least. 19213 */ 19214 if (buflen - ucmd_buf.uscsi_resid < headlen) 19215 status = EIO; 19216 break; /* Success! */ 19217 case EIO: 19218 switch (ucmd_buf.uscsi_status) { 19219 case STATUS_RESERVATION_CONFLICT: 19220 status = EACCES; 19221 break; 19222 default: 19223 break; 19224 } 19225 break; 19226 default: 19227 break; 19228 } 19229 19230 if (status == 0) { 19231 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 19232 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19233 } 19234 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 19235 19236 return (status); 19237 } 19238 19239 19240 /* 19241 * Function: sd_send_scsi_MODE_SELECT 19242 * 19243 * Description: Utility function for issuing a scsi MODE SELECT command. 19244 * Note: This routine uses a consistent implementation for Group0, 19245 * Group1, and Group2 commands across all platforms. ATAPI devices 19246 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 19247 * 19248 * Arguments: un - pointer to the softstate struct for the target. 19249 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 19250 * CDB_GROUP[1|2] (10 byte). 19251 * bufaddr - buffer for page data retrieved from the target. 19252 * buflen - size of page to be retrieved. 19253 * save_page - boolean to determin if SP bit should be set. 19254 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19255 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19256 * to use the USCSI "direct" chain and bypass the normal 19257 * command waitq. 19258 * 19259 * Return Code: 0 - Success 19260 * errno return code from sd_send_scsi_cmd() 19261 * 19262 * Context: Can sleep. Does not return until command is completed. 19263 */ 19264 19265 static int 19266 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 19267 size_t buflen, uchar_t save_page, int path_flag) 19268 { 19269 struct scsi_extended_sense sense_buf; 19270 union scsi_cdb cdb; 19271 struct uscsi_cmd ucmd_buf; 19272 int status; 19273 19274 ASSERT(un != NULL); 19275 ASSERT(!mutex_owned(SD_MUTEX(un))); 19276 ASSERT(bufaddr != NULL); 19277 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 19278 (cdbsize == CDB_GROUP2)); 19279 19280 SD_TRACE(SD_LOG_IO, un, 19281 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 19282 19283 bzero(&cdb, sizeof (cdb)); 19284 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19285 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19286 19287 /* Set the PF bit for many third party drives */ 19288 cdb.cdb_opaque[1] = 0x10; 19289 19290 /* Set the savepage(SP) bit if given */ 19291 if (save_page == SD_SAVE_PAGE) { 19292 cdb.cdb_opaque[1] |= 0x01; 19293 } 19294 19295 if (cdbsize == CDB_GROUP0) { 19296 cdb.scc_cmd = SCMD_MODE_SELECT; 19297 FORMG0COUNT(&cdb, buflen); 19298 } else { 19299 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 19300 FORMG1COUNT(&cdb, buflen); 19301 } 19302 19303 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19304 19305 ucmd_buf.uscsi_cdb = (char *)&cdb; 19306 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19307 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19308 ucmd_buf.uscsi_buflen = buflen; 19309 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19310 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19311 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 19312 ucmd_buf.uscsi_timeout = 60; 19313 19314 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19315 UIO_SYSSPACE, path_flag); 19316 19317 switch (status) { 19318 case 0: 19319 break; /* Success! */ 19320 case EIO: 19321 switch (ucmd_buf.uscsi_status) { 19322 case STATUS_RESERVATION_CONFLICT: 19323 status = EACCES; 19324 break; 19325 default: 19326 break; 19327 } 19328 break; 19329 default: 19330 break; 19331 } 19332 19333 if (status == 0) { 19334 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 19335 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19336 } 19337 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 19338 19339 return (status); 19340 } 19341 19342 19343 /* 19344 * Function: sd_send_scsi_RDWR 19345 * 19346 * Description: Issue a scsi READ or WRITE command with the given parameters. 19347 * 19348 * Arguments: un: Pointer to the sd_lun struct for the target. 19349 * cmd: SCMD_READ or SCMD_WRITE 19350 * bufaddr: Address of caller's buffer to receive the RDWR data 19351 * buflen: Length of caller's buffer receive the RDWR data. 19352 * start_block: Block number for the start of the RDWR operation. 19353 * (Assumes target-native block size.) 19354 * residp: Pointer to variable to receive the redisual of the 19355 * RDWR operation (may be NULL of no residual requested). 19356 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19357 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19358 * to use the USCSI "direct" chain and bypass the normal 19359 * command waitq. 19360 * 19361 * Return Code: 0 - Success 19362 * errno return code from sd_send_scsi_cmd() 19363 * 19364 * Context: Can sleep. Does not return until command is completed. 19365 */ 19366 19367 static int 19368 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 19369 size_t buflen, daddr_t start_block, int path_flag) 19370 { 19371 struct scsi_extended_sense sense_buf; 19372 union scsi_cdb cdb; 19373 struct uscsi_cmd ucmd_buf; 19374 uint32_t block_count; 19375 int status; 19376 int cdbsize; 19377 uchar_t flag; 19378 19379 ASSERT(un != NULL); 19380 ASSERT(!mutex_owned(SD_MUTEX(un))); 19381 ASSERT(bufaddr != NULL); 19382 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 19383 19384 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 19385 19386 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 19387 return (EINVAL); 19388 } 19389 19390 mutex_enter(SD_MUTEX(un)); 19391 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 19392 mutex_exit(SD_MUTEX(un)); 19393 19394 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 19395 19396 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 19397 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 19398 bufaddr, buflen, start_block, block_count); 19399 19400 bzero(&cdb, sizeof (cdb)); 19401 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19402 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19403 19404 /* Compute CDB size to use */ 19405 if (start_block > 0xffffffff) 19406 cdbsize = CDB_GROUP4; 19407 else if ((start_block & 0xFFE00000) || 19408 (un->un_f_cfg_is_atapi == TRUE)) 19409 cdbsize = CDB_GROUP1; 19410 else 19411 cdbsize = CDB_GROUP0; 19412 19413 switch (cdbsize) { 19414 case CDB_GROUP0: /* 6-byte CDBs */ 19415 cdb.scc_cmd = cmd; 19416 FORMG0ADDR(&cdb, start_block); 19417 FORMG0COUNT(&cdb, block_count); 19418 break; 19419 case CDB_GROUP1: /* 10-byte CDBs */ 19420 cdb.scc_cmd = cmd | SCMD_GROUP1; 19421 FORMG1ADDR(&cdb, start_block); 19422 FORMG1COUNT(&cdb, block_count); 19423 break; 19424 case CDB_GROUP4: /* 16-byte CDBs */ 19425 cdb.scc_cmd = cmd | SCMD_GROUP4; 19426 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 19427 FORMG4COUNT(&cdb, block_count); 19428 break; 19429 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 19430 default: 19431 /* All others reserved */ 19432 return (EINVAL); 19433 } 19434 19435 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 19436 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 19437 19438 ucmd_buf.uscsi_cdb = (char *)&cdb; 19439 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 19440 ucmd_buf.uscsi_bufaddr = bufaddr; 19441 ucmd_buf.uscsi_buflen = buflen; 19442 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19443 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19444 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 19445 ucmd_buf.uscsi_timeout = 60; 19446 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19447 UIO_SYSSPACE, path_flag); 19448 switch (status) { 19449 case 0: 19450 break; /* Success! */ 19451 case EIO: 19452 switch (ucmd_buf.uscsi_status) { 19453 case STATUS_RESERVATION_CONFLICT: 19454 status = EACCES; 19455 break; 19456 default: 19457 break; 19458 } 19459 break; 19460 default: 19461 break; 19462 } 19463 19464 if (status == 0) { 19465 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 19466 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19467 } 19468 19469 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 19470 19471 return (status); 19472 } 19473 19474 19475 /* 19476 * Function: sd_send_scsi_LOG_SENSE 19477 * 19478 * Description: Issue a scsi LOG_SENSE command with the given parameters. 19479 * 19480 * Arguments: un: Pointer to the sd_lun struct for the target. 19481 * 19482 * Return Code: 0 - Success 19483 * errno return code from sd_send_scsi_cmd() 19484 * 19485 * Context: Can sleep. Does not return until command is completed. 19486 */ 19487 19488 static int 19489 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 19490 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 19491 int path_flag) 19492 19493 { 19494 struct scsi_extended_sense sense_buf; 19495 union scsi_cdb cdb; 19496 struct uscsi_cmd ucmd_buf; 19497 int status; 19498 19499 ASSERT(un != NULL); 19500 ASSERT(!mutex_owned(SD_MUTEX(un))); 19501 19502 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 19503 19504 bzero(&cdb, sizeof (cdb)); 19505 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19506 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19507 19508 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 19509 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 19510 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 19511 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 19512 FORMG1COUNT(&cdb, buflen); 19513 19514 ucmd_buf.uscsi_cdb = (char *)&cdb; 19515 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19516 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 19517 ucmd_buf.uscsi_buflen = buflen; 19518 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19519 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19520 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19521 ucmd_buf.uscsi_timeout = 60; 19522 19523 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19524 UIO_SYSSPACE, path_flag); 19525 19526 switch (status) { 19527 case 0: 19528 break; 19529 case EIO: 19530 switch (ucmd_buf.uscsi_status) { 19531 case STATUS_RESERVATION_CONFLICT: 19532 status = EACCES; 19533 break; 19534 case STATUS_CHECK: 19535 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19536 (scsi_sense_key((uint8_t *)&sense_buf) == 19537 KEY_ILLEGAL_REQUEST) && 19538 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 19539 /* 19540 * ASC 0x24: INVALID FIELD IN CDB 19541 */ 19542 switch (page_code) { 19543 case START_STOP_CYCLE_PAGE: 19544 /* 19545 * The start stop cycle counter is 19546 * implemented as page 0x31 in earlier 19547 * generation disks. In new generation 19548 * disks the start stop cycle counter is 19549 * implemented as page 0xE. To properly 19550 * handle this case if an attempt for 19551 * log page 0xE is made and fails we 19552 * will try again using page 0x31. 19553 * 19554 * Network storage BU committed to 19555 * maintain the page 0x31 for this 19556 * purpose and will not have any other 19557 * page implemented with page code 0x31 19558 * until all disks transition to the 19559 * standard page. 19560 */ 19561 mutex_enter(SD_MUTEX(un)); 19562 un->un_start_stop_cycle_page = 19563 START_STOP_CYCLE_VU_PAGE; 19564 cdb.cdb_opaque[2] = 19565 (char)(page_control << 6) | 19566 un->un_start_stop_cycle_page; 19567 mutex_exit(SD_MUTEX(un)); 19568 status = sd_send_scsi_cmd( 19569 SD_GET_DEV(un), &ucmd_buf, FKIOCTL, 19570 UIO_SYSSPACE, path_flag); 19571 19572 break; 19573 case TEMPERATURE_PAGE: 19574 status = ENOTTY; 19575 break; 19576 default: 19577 break; 19578 } 19579 } 19580 break; 19581 default: 19582 break; 19583 } 19584 break; 19585 default: 19586 break; 19587 } 19588 19589 if (status == 0) { 19590 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 19591 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 19592 } 19593 19594 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 19595 19596 return (status); 19597 } 19598 19599 19600 /* 19601 * Function: sdioctl 19602 * 19603 * Description: Driver's ioctl(9e) entry point function. 19604 * 19605 * Arguments: dev - device number 19606 * cmd - ioctl operation to be performed 19607 * arg - user argument, contains data to be set or reference 19608 * parameter for get 19609 * flag - bit flag, indicating open settings, 32/64 bit type 19610 * cred_p - user credential pointer 19611 * rval_p - calling process return value (OPT) 19612 * 19613 * Return Code: EINVAL 19614 * ENOTTY 19615 * ENXIO 19616 * EIO 19617 * EFAULT 19618 * ENOTSUP 19619 * EPERM 19620 * 19621 * Context: Called from the device switch at normal priority. 19622 */ 19623 19624 static int 19625 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 19626 { 19627 struct sd_lun *un = NULL; 19628 int err = 0; 19629 int i = 0; 19630 cred_t *cr; 19631 int tmprval = EINVAL; 19632 int is_valid; 19633 19634 /* 19635 * All device accesses go thru sdstrategy where we check on suspend 19636 * status 19637 */ 19638 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 19639 return (ENXIO); 19640 } 19641 19642 ASSERT(!mutex_owned(SD_MUTEX(un))); 19643 19644 19645 is_valid = SD_IS_VALID_LABEL(un); 19646 19647 /* 19648 * Moved this wait from sd_uscsi_strategy to here for 19649 * reasons of deadlock prevention. Internal driver commands, 19650 * specifically those to change a devices power level, result 19651 * in a call to sd_uscsi_strategy. 19652 */ 19653 mutex_enter(SD_MUTEX(un)); 19654 while ((un->un_state == SD_STATE_SUSPENDED) || 19655 (un->un_state == SD_STATE_PM_CHANGING)) { 19656 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 19657 } 19658 /* 19659 * Twiddling the counter here protects commands from now 19660 * through to the top of sd_uscsi_strategy. Without the 19661 * counter inc. a power down, for example, could get in 19662 * after the above check for state is made and before 19663 * execution gets to the top of sd_uscsi_strategy. 19664 * That would cause problems. 19665 */ 19666 un->un_ncmds_in_driver++; 19667 19668 if (!is_valid && 19669 (flag & (FNDELAY | FNONBLOCK))) { 19670 switch (cmd) { 19671 case DKIOCGGEOM: /* SD_PATH_DIRECT */ 19672 case DKIOCGVTOC: 19673 case DKIOCGAPART: 19674 case DKIOCPARTINFO: 19675 case DKIOCSGEOM: 19676 case DKIOCSAPART: 19677 case DKIOCGETEFI: 19678 case DKIOCPARTITION: 19679 case DKIOCSVTOC: 19680 case DKIOCSETEFI: 19681 case DKIOCGMBOOT: 19682 case DKIOCSMBOOT: 19683 case DKIOCG_PHYGEOM: 19684 case DKIOCG_VIRTGEOM: 19685 /* let cmlb handle it */ 19686 goto skip_ready_valid; 19687 19688 case CDROMPAUSE: 19689 case CDROMRESUME: 19690 case CDROMPLAYMSF: 19691 case CDROMPLAYTRKIND: 19692 case CDROMREADTOCHDR: 19693 case CDROMREADTOCENTRY: 19694 case CDROMSTOP: 19695 case CDROMSTART: 19696 case CDROMVOLCTRL: 19697 case CDROMSUBCHNL: 19698 case CDROMREADMODE2: 19699 case CDROMREADMODE1: 19700 case CDROMREADOFFSET: 19701 case CDROMSBLKMODE: 19702 case CDROMGBLKMODE: 19703 case CDROMGDRVSPEED: 19704 case CDROMSDRVSPEED: 19705 case CDROMCDDA: 19706 case CDROMCDXA: 19707 case CDROMSUBCODE: 19708 if (!ISCD(un)) { 19709 un->un_ncmds_in_driver--; 19710 ASSERT(un->un_ncmds_in_driver >= 0); 19711 mutex_exit(SD_MUTEX(un)); 19712 return (ENOTTY); 19713 } 19714 break; 19715 case FDEJECT: 19716 case DKIOCEJECT: 19717 case CDROMEJECT: 19718 if (!un->un_f_eject_media_supported) { 19719 un->un_ncmds_in_driver--; 19720 ASSERT(un->un_ncmds_in_driver >= 0); 19721 mutex_exit(SD_MUTEX(un)); 19722 return (ENOTTY); 19723 } 19724 break; 19725 case DKIOCFLUSHWRITECACHE: 19726 mutex_exit(SD_MUTEX(un)); 19727 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19728 if (err != 0) { 19729 mutex_enter(SD_MUTEX(un)); 19730 un->un_ncmds_in_driver--; 19731 ASSERT(un->un_ncmds_in_driver >= 0); 19732 mutex_exit(SD_MUTEX(un)); 19733 return (EIO); 19734 } 19735 mutex_enter(SD_MUTEX(un)); 19736 /* FALLTHROUGH */ 19737 case DKIOCREMOVABLE: 19738 case DKIOCHOTPLUGGABLE: 19739 case DKIOCINFO: 19740 case DKIOCGMEDIAINFO: 19741 case MHIOCENFAILFAST: 19742 case MHIOCSTATUS: 19743 case MHIOCTKOWN: 19744 case MHIOCRELEASE: 19745 case MHIOCGRP_INKEYS: 19746 case MHIOCGRP_INRESV: 19747 case MHIOCGRP_REGISTER: 19748 case MHIOCGRP_RESERVE: 19749 case MHIOCGRP_PREEMPTANDABORT: 19750 case MHIOCGRP_REGISTERANDIGNOREKEY: 19751 case CDROMCLOSETRAY: 19752 case USCSICMD: 19753 goto skip_ready_valid; 19754 default: 19755 break; 19756 } 19757 19758 mutex_exit(SD_MUTEX(un)); 19759 err = sd_ready_and_valid(un); 19760 mutex_enter(SD_MUTEX(un)); 19761 19762 if (err != SD_READY_VALID) { 19763 switch (cmd) { 19764 case DKIOCSTATE: 19765 case CDROMGDRVSPEED: 19766 case CDROMSDRVSPEED: 19767 case FDEJECT: /* for eject command */ 19768 case DKIOCEJECT: 19769 case CDROMEJECT: 19770 case DKIOCREMOVABLE: 19771 case DKIOCHOTPLUGGABLE: 19772 break; 19773 default: 19774 if (un->un_f_has_removable_media) { 19775 err = ENXIO; 19776 } else { 19777 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 19778 if (err == SD_RESERVED_BY_OTHERS) { 19779 err = EACCES; 19780 } else { 19781 err = EIO; 19782 } 19783 } 19784 un->un_ncmds_in_driver--; 19785 ASSERT(un->un_ncmds_in_driver >= 0); 19786 mutex_exit(SD_MUTEX(un)); 19787 return (err); 19788 } 19789 } 19790 } 19791 19792 skip_ready_valid: 19793 mutex_exit(SD_MUTEX(un)); 19794 19795 switch (cmd) { 19796 case DKIOCINFO: 19797 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 19798 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 19799 break; 19800 19801 case DKIOCGMEDIAINFO: 19802 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 19803 err = sd_get_media_info(dev, (caddr_t)arg, flag); 19804 break; 19805 19806 case DKIOCGGEOM: 19807 case DKIOCGVTOC: 19808 case DKIOCGAPART: 19809 case DKIOCPARTINFO: 19810 case DKIOCSGEOM: 19811 case DKIOCSAPART: 19812 case DKIOCGETEFI: 19813 case DKIOCPARTITION: 19814 case DKIOCSVTOC: 19815 case DKIOCSETEFI: 19816 case DKIOCGMBOOT: 19817 case DKIOCSMBOOT: 19818 case DKIOCG_PHYGEOM: 19819 case DKIOCG_VIRTGEOM: 19820 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd); 19821 19822 /* TUR should spin up */ 19823 19824 if (un->un_f_has_removable_media) 19825 err = sd_send_scsi_TEST_UNIT_READY(un, 19826 SD_CHECK_FOR_MEDIA); 19827 else 19828 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 19829 19830 if (err != 0) 19831 break; 19832 19833 err = cmlb_ioctl(un->un_cmlbhandle, dev, 19834 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT); 19835 19836 if ((err == 0) && 19837 ((cmd == DKIOCSETEFI) || 19838 (un->un_f_pkstats_enabled) && 19839 (cmd == DKIOCSAPART || cmd == DKIOCSVTOC))) { 19840 19841 tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT, 19842 (void *)SD_PATH_DIRECT); 19843 if ((tmprval == 0) && un->un_f_pkstats_enabled) { 19844 sd_set_pstats(un); 19845 SD_TRACE(SD_LOG_IO_PARTITION, un, 19846 "sd_ioctl: un:0x%p pstats created and " 19847 "set\n", un); 19848 } 19849 } 19850 19851 if ((cmd == DKIOCSVTOC) || 19852 ((cmd == DKIOCSETEFI) && (tmprval == 0))) { 19853 19854 mutex_enter(SD_MUTEX(un)); 19855 if (un->un_f_devid_supported && 19856 (un->un_f_opt_fab_devid == TRUE)) { 19857 if (un->un_devid == NULL) { 19858 sd_register_devid(un, SD_DEVINFO(un), 19859 SD_TARGET_IS_UNRESERVED); 19860 } else { 19861 /* 19862 * The device id for this disk 19863 * has been fabricated. The 19864 * device id must be preserved 19865 * by writing it back out to 19866 * disk. 19867 */ 19868 if (sd_write_deviceid(un) != 0) { 19869 ddi_devid_free(un->un_devid); 19870 un->un_devid = NULL; 19871 } 19872 } 19873 } 19874 mutex_exit(SD_MUTEX(un)); 19875 } 19876 19877 break; 19878 19879 case DKIOCLOCK: 19880 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 19881 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19882 SD_PATH_STANDARD); 19883 break; 19884 19885 case DKIOCUNLOCK: 19886 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 19887 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 19888 SD_PATH_STANDARD); 19889 break; 19890 19891 case DKIOCSTATE: { 19892 enum dkio_state state; 19893 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 19894 19895 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 19896 err = EFAULT; 19897 } else { 19898 err = sd_check_media(dev, state); 19899 if (err == 0) { 19900 if (ddi_copyout(&un->un_mediastate, (void *)arg, 19901 sizeof (int), flag) != 0) 19902 err = EFAULT; 19903 } 19904 } 19905 break; 19906 } 19907 19908 case DKIOCREMOVABLE: 19909 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 19910 i = un->un_f_has_removable_media ? 1 : 0; 19911 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 19912 err = EFAULT; 19913 } else { 19914 err = 0; 19915 } 19916 break; 19917 19918 case DKIOCHOTPLUGGABLE: 19919 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 19920 i = un->un_f_is_hotpluggable ? 1 : 0; 19921 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 19922 err = EFAULT; 19923 } else { 19924 err = 0; 19925 } 19926 break; 19927 19928 case DKIOCGTEMPERATURE: 19929 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 19930 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 19931 break; 19932 19933 case MHIOCENFAILFAST: 19934 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 19935 if ((err = drv_priv(cred_p)) == 0) { 19936 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 19937 } 19938 break; 19939 19940 case MHIOCTKOWN: 19941 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 19942 if ((err = drv_priv(cred_p)) == 0) { 19943 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 19944 } 19945 break; 19946 19947 case MHIOCRELEASE: 19948 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 19949 if ((err = drv_priv(cred_p)) == 0) { 19950 err = sd_mhdioc_release(dev); 19951 } 19952 break; 19953 19954 case MHIOCSTATUS: 19955 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 19956 if ((err = drv_priv(cred_p)) == 0) { 19957 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 19958 case 0: 19959 err = 0; 19960 break; 19961 case EACCES: 19962 *rval_p = 1; 19963 err = 0; 19964 break; 19965 default: 19966 err = EIO; 19967 break; 19968 } 19969 } 19970 break; 19971 19972 case MHIOCQRESERVE: 19973 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 19974 if ((err = drv_priv(cred_p)) == 0) { 19975 err = sd_reserve_release(dev, SD_RESERVE); 19976 } 19977 break; 19978 19979 case MHIOCREREGISTERDEVID: 19980 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 19981 if (drv_priv(cred_p) == EPERM) { 19982 err = EPERM; 19983 } else if (!un->un_f_devid_supported) { 19984 err = ENOTTY; 19985 } else { 19986 err = sd_mhdioc_register_devid(dev); 19987 } 19988 break; 19989 19990 case MHIOCGRP_INKEYS: 19991 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 19992 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 19993 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 19994 err = ENOTSUP; 19995 } else { 19996 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 19997 flag); 19998 } 19999 } 20000 break; 20001 20002 case MHIOCGRP_INRESV: 20003 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 20004 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 20005 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20006 err = ENOTSUP; 20007 } else { 20008 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 20009 } 20010 } 20011 break; 20012 20013 case MHIOCGRP_REGISTER: 20014 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 20015 if ((err = drv_priv(cred_p)) != EPERM) { 20016 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20017 err = ENOTSUP; 20018 } else if (arg != NULL) { 20019 mhioc_register_t reg; 20020 if (ddi_copyin((void *)arg, ®, 20021 sizeof (mhioc_register_t), flag) != 0) { 20022 err = EFAULT; 20023 } else { 20024 err = 20025 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20026 un, SD_SCSI3_REGISTER, 20027 (uchar_t *)®); 20028 } 20029 } 20030 } 20031 break; 20032 20033 case MHIOCGRP_RESERVE: 20034 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 20035 if ((err = drv_priv(cred_p)) != EPERM) { 20036 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20037 err = ENOTSUP; 20038 } else if (arg != NULL) { 20039 mhioc_resv_desc_t resv_desc; 20040 if (ddi_copyin((void *)arg, &resv_desc, 20041 sizeof (mhioc_resv_desc_t), flag) != 0) { 20042 err = EFAULT; 20043 } else { 20044 err = 20045 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20046 un, SD_SCSI3_RESERVE, 20047 (uchar_t *)&resv_desc); 20048 } 20049 } 20050 } 20051 break; 20052 20053 case MHIOCGRP_PREEMPTANDABORT: 20054 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 20055 if ((err = drv_priv(cred_p)) != EPERM) { 20056 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20057 err = ENOTSUP; 20058 } else if (arg != NULL) { 20059 mhioc_preemptandabort_t preempt_abort; 20060 if (ddi_copyin((void *)arg, &preempt_abort, 20061 sizeof (mhioc_preemptandabort_t), 20062 flag) != 0) { 20063 err = EFAULT; 20064 } else { 20065 err = 20066 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20067 un, SD_SCSI3_PREEMPTANDABORT, 20068 (uchar_t *)&preempt_abort); 20069 } 20070 } 20071 } 20072 break; 20073 20074 case MHIOCGRP_REGISTERANDIGNOREKEY: 20075 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n"); 20076 if ((err = drv_priv(cred_p)) != EPERM) { 20077 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 20078 err = ENOTSUP; 20079 } else if (arg != NULL) { 20080 mhioc_registerandignorekey_t r_and_i; 20081 if (ddi_copyin((void *)arg, (void *)&r_and_i, 20082 sizeof (mhioc_registerandignorekey_t), 20083 flag) != 0) { 20084 err = EFAULT; 20085 } else { 20086 err = 20087 sd_send_scsi_PERSISTENT_RESERVE_OUT( 20088 un, SD_SCSI3_REGISTERANDIGNOREKEY, 20089 (uchar_t *)&r_and_i); 20090 } 20091 } 20092 } 20093 break; 20094 20095 case USCSICMD: 20096 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 20097 cr = ddi_get_cred(); 20098 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 20099 err = EPERM; 20100 } else { 20101 enum uio_seg uioseg; 20102 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : 20103 UIO_USERSPACE; 20104 if (un->un_f_format_in_progress == TRUE) { 20105 err = EAGAIN; 20106 break; 20107 } 20108 err = sd_send_scsi_cmd(dev, (struct uscsi_cmd *)arg, 20109 flag, uioseg, SD_PATH_STANDARD); 20110 } 20111 break; 20112 20113 case CDROMPAUSE: 20114 case CDROMRESUME: 20115 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 20116 if (!ISCD(un)) { 20117 err = ENOTTY; 20118 } else { 20119 err = sr_pause_resume(dev, cmd); 20120 } 20121 break; 20122 20123 case CDROMPLAYMSF: 20124 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 20125 if (!ISCD(un)) { 20126 err = ENOTTY; 20127 } else { 20128 err = sr_play_msf(dev, (caddr_t)arg, flag); 20129 } 20130 break; 20131 20132 case CDROMPLAYTRKIND: 20133 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 20134 #if defined(__i386) || defined(__amd64) 20135 /* 20136 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 20137 */ 20138 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20139 #else 20140 if (!ISCD(un)) { 20141 #endif 20142 err = ENOTTY; 20143 } else { 20144 err = sr_play_trkind(dev, (caddr_t)arg, flag); 20145 } 20146 break; 20147 20148 case CDROMREADTOCHDR: 20149 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 20150 if (!ISCD(un)) { 20151 err = ENOTTY; 20152 } else { 20153 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 20154 } 20155 break; 20156 20157 case CDROMREADTOCENTRY: 20158 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 20159 if (!ISCD(un)) { 20160 err = ENOTTY; 20161 } else { 20162 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 20163 } 20164 break; 20165 20166 case CDROMSTOP: 20167 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 20168 if (!ISCD(un)) { 20169 err = ENOTTY; 20170 } else { 20171 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 20172 SD_PATH_STANDARD); 20173 } 20174 break; 20175 20176 case CDROMSTART: 20177 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 20178 if (!ISCD(un)) { 20179 err = ENOTTY; 20180 } else { 20181 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 20182 SD_PATH_STANDARD); 20183 } 20184 break; 20185 20186 case CDROMCLOSETRAY: 20187 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 20188 if (!ISCD(un)) { 20189 err = ENOTTY; 20190 } else { 20191 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 20192 SD_PATH_STANDARD); 20193 } 20194 break; 20195 20196 case FDEJECT: /* for eject command */ 20197 case DKIOCEJECT: 20198 case CDROMEJECT: 20199 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 20200 if (!un->un_f_eject_media_supported) { 20201 err = ENOTTY; 20202 } else { 20203 err = sr_eject(dev); 20204 } 20205 break; 20206 20207 case CDROMVOLCTRL: 20208 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 20209 if (!ISCD(un)) { 20210 err = ENOTTY; 20211 } else { 20212 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 20213 } 20214 break; 20215 20216 case CDROMSUBCHNL: 20217 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 20218 if (!ISCD(un)) { 20219 err = ENOTTY; 20220 } else { 20221 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 20222 } 20223 break; 20224 20225 case CDROMREADMODE2: 20226 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 20227 if (!ISCD(un)) { 20228 err = ENOTTY; 20229 } else if (un->un_f_cfg_is_atapi == TRUE) { 20230 /* 20231 * If the drive supports READ CD, use that instead of 20232 * switching the LBA size via a MODE SELECT 20233 * Block Descriptor 20234 */ 20235 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 20236 } else { 20237 err = sr_read_mode2(dev, (caddr_t)arg, flag); 20238 } 20239 break; 20240 20241 case CDROMREADMODE1: 20242 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 20243 if (!ISCD(un)) { 20244 err = ENOTTY; 20245 } else { 20246 err = sr_read_mode1(dev, (caddr_t)arg, flag); 20247 } 20248 break; 20249 20250 case CDROMREADOFFSET: 20251 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 20252 if (!ISCD(un)) { 20253 err = ENOTTY; 20254 } else { 20255 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 20256 flag); 20257 } 20258 break; 20259 20260 case CDROMSBLKMODE: 20261 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 20262 /* 20263 * There is no means of changing block size in case of atapi 20264 * drives, thus return ENOTTY if drive type is atapi 20265 */ 20266 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 20267 err = ENOTTY; 20268 } else if (un->un_f_mmc_cap == TRUE) { 20269 20270 /* 20271 * MMC Devices do not support changing the 20272 * logical block size 20273 * 20274 * Note: EINVAL is being returned instead of ENOTTY to 20275 * maintain consistancy with the original mmc 20276 * driver update. 20277 */ 20278 err = EINVAL; 20279 } else { 20280 mutex_enter(SD_MUTEX(un)); 20281 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 20282 (un->un_ncmds_in_transport > 0)) { 20283 mutex_exit(SD_MUTEX(un)); 20284 err = EINVAL; 20285 } else { 20286 mutex_exit(SD_MUTEX(un)); 20287 err = sr_change_blkmode(dev, cmd, arg, flag); 20288 } 20289 } 20290 break; 20291 20292 case CDROMGBLKMODE: 20293 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 20294 if (!ISCD(un)) { 20295 err = ENOTTY; 20296 } else if ((un->un_f_cfg_is_atapi != FALSE) && 20297 (un->un_f_blockcount_is_valid != FALSE)) { 20298 /* 20299 * Drive is an ATAPI drive so return target block 20300 * size for ATAPI drives since we cannot change the 20301 * blocksize on ATAPI drives. Used primarily to detect 20302 * if an ATAPI cdrom is present. 20303 */ 20304 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 20305 sizeof (int), flag) != 0) { 20306 err = EFAULT; 20307 } else { 20308 err = 0; 20309 } 20310 20311 } else { 20312 /* 20313 * Drive supports changing block sizes via a Mode 20314 * Select. 20315 */ 20316 err = sr_change_blkmode(dev, cmd, arg, flag); 20317 } 20318 break; 20319 20320 case CDROMGDRVSPEED: 20321 case CDROMSDRVSPEED: 20322 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 20323 if (!ISCD(un)) { 20324 err = ENOTTY; 20325 } else if (un->un_f_mmc_cap == TRUE) { 20326 /* 20327 * Note: In the future the driver implementation 20328 * for getting and 20329 * setting cd speed should entail: 20330 * 1) If non-mmc try the Toshiba mode page 20331 * (sr_change_speed) 20332 * 2) If mmc but no support for Real Time Streaming try 20333 * the SET CD SPEED (0xBB) command 20334 * (sr_atapi_change_speed) 20335 * 3) If mmc and support for Real Time Streaming 20336 * try the GET PERFORMANCE and SET STREAMING 20337 * commands (not yet implemented, 4380808) 20338 */ 20339 /* 20340 * As per recent MMC spec, CD-ROM speed is variable 20341 * and changes with LBA. Since there is no such 20342 * things as drive speed now, fail this ioctl. 20343 * 20344 * Note: EINVAL is returned for consistancy of original 20345 * implementation which included support for getting 20346 * the drive speed of mmc devices but not setting 20347 * the drive speed. Thus EINVAL would be returned 20348 * if a set request was made for an mmc device. 20349 * We no longer support get or set speed for 20350 * mmc but need to remain consistent with regard 20351 * to the error code returned. 20352 */ 20353 err = EINVAL; 20354 } else if (un->un_f_cfg_is_atapi == TRUE) { 20355 err = sr_atapi_change_speed(dev, cmd, arg, flag); 20356 } else { 20357 err = sr_change_speed(dev, cmd, arg, flag); 20358 } 20359 break; 20360 20361 case CDROMCDDA: 20362 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 20363 if (!ISCD(un)) { 20364 err = ENOTTY; 20365 } else { 20366 err = sr_read_cdda(dev, (void *)arg, flag); 20367 } 20368 break; 20369 20370 case CDROMCDXA: 20371 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 20372 if (!ISCD(un)) { 20373 err = ENOTTY; 20374 } else { 20375 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 20376 } 20377 break; 20378 20379 case CDROMSUBCODE: 20380 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 20381 if (!ISCD(un)) { 20382 err = ENOTTY; 20383 } else { 20384 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 20385 } 20386 break; 20387 20388 20389 #ifdef SDDEBUG 20390 /* RESET/ABORTS testing ioctls */ 20391 case DKIOCRESET: { 20392 int reset_level; 20393 20394 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 20395 err = EFAULT; 20396 } else { 20397 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 20398 "reset_level = 0x%lx\n", reset_level); 20399 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 20400 err = 0; 20401 } else { 20402 err = EIO; 20403 } 20404 } 20405 break; 20406 } 20407 20408 case DKIOCABORT: 20409 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 20410 if (scsi_abort(SD_ADDRESS(un), NULL)) { 20411 err = 0; 20412 } else { 20413 err = EIO; 20414 } 20415 break; 20416 #endif 20417 20418 #ifdef SD_FAULT_INJECTION 20419 /* SDIOC FaultInjection testing ioctls */ 20420 case SDIOCSTART: 20421 case SDIOCSTOP: 20422 case SDIOCINSERTPKT: 20423 case SDIOCINSERTXB: 20424 case SDIOCINSERTUN: 20425 case SDIOCINSERTARQ: 20426 case SDIOCPUSH: 20427 case SDIOCRETRIEVE: 20428 case SDIOCRUN: 20429 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 20430 "SDIOC detected cmd:0x%X:\n", cmd); 20431 /* call error generator */ 20432 sd_faultinjection_ioctl(cmd, arg, un); 20433 err = 0; 20434 break; 20435 20436 #endif /* SD_FAULT_INJECTION */ 20437 20438 case DKIOCFLUSHWRITECACHE: 20439 { 20440 struct dk_callback *dkc = (struct dk_callback *)arg; 20441 20442 mutex_enter(SD_MUTEX(un)); 20443 if (!un->un_f_sync_cache_supported || 20444 !un->un_f_write_cache_enabled) { 20445 err = un->un_f_sync_cache_supported ? 20446 0 : ENOTSUP; 20447 mutex_exit(SD_MUTEX(un)); 20448 if ((flag & FKIOCTL) && dkc != NULL && 20449 dkc->dkc_callback != NULL) { 20450 (*dkc->dkc_callback)(dkc->dkc_cookie, 20451 err); 20452 /* 20453 * Did callback and reported error. 20454 * Since we did a callback, ioctl 20455 * should return 0. 20456 */ 20457 err = 0; 20458 } 20459 break; 20460 } 20461 mutex_exit(SD_MUTEX(un)); 20462 20463 if ((flag & FKIOCTL) && dkc != NULL && 20464 dkc->dkc_callback != NULL) { 20465 /* async SYNC CACHE request */ 20466 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 20467 } else { 20468 /* synchronous SYNC CACHE request */ 20469 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 20470 } 20471 } 20472 break; 20473 20474 case DKIOCGETWCE: { 20475 20476 int wce; 20477 20478 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 20479 break; 20480 } 20481 20482 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 20483 err = EFAULT; 20484 } 20485 break; 20486 } 20487 20488 case DKIOCSETWCE: { 20489 20490 int wce, sync_supported; 20491 20492 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 20493 err = EFAULT; 20494 break; 20495 } 20496 20497 /* 20498 * Synchronize multiple threads trying to enable 20499 * or disable the cache via the un_f_wcc_cv 20500 * condition variable. 20501 */ 20502 mutex_enter(SD_MUTEX(un)); 20503 20504 /* 20505 * Don't allow the cache to be enabled if the 20506 * config file has it disabled. 20507 */ 20508 if (un->un_f_opt_disable_cache && wce) { 20509 mutex_exit(SD_MUTEX(un)); 20510 err = EINVAL; 20511 break; 20512 } 20513 20514 /* 20515 * Wait for write cache change in progress 20516 * bit to be clear before proceeding. 20517 */ 20518 while (un->un_f_wcc_inprog) 20519 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 20520 20521 un->un_f_wcc_inprog = 1; 20522 20523 if (un->un_f_write_cache_enabled && wce == 0) { 20524 /* 20525 * Disable the write cache. Don't clear 20526 * un_f_write_cache_enabled until after 20527 * the mode select and flush are complete. 20528 */ 20529 sync_supported = un->un_f_sync_cache_supported; 20530 20531 /* 20532 * If cache flush is suppressed, we assume that the 20533 * controller firmware will take care of managing the 20534 * write cache for us: no need to explicitly 20535 * disable it. 20536 */ 20537 if (!un->un_f_suppress_cache_flush) { 20538 mutex_exit(SD_MUTEX(un)); 20539 if ((err = sd_cache_control(un, 20540 SD_CACHE_NOCHANGE, 20541 SD_CACHE_DISABLE)) == 0 && 20542 sync_supported) { 20543 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, 20544 NULL); 20545 } 20546 } else { 20547 mutex_exit(SD_MUTEX(un)); 20548 } 20549 20550 mutex_enter(SD_MUTEX(un)); 20551 if (err == 0) { 20552 un->un_f_write_cache_enabled = 0; 20553 } 20554 20555 } else if (!un->un_f_write_cache_enabled && wce != 0) { 20556 /* 20557 * Set un_f_write_cache_enabled first, so there is 20558 * no window where the cache is enabled, but the 20559 * bit says it isn't. 20560 */ 20561 un->un_f_write_cache_enabled = 1; 20562 20563 /* 20564 * If cache flush is suppressed, we assume that the 20565 * controller firmware will take care of managing the 20566 * write cache for us: no need to explicitly 20567 * enable it. 20568 */ 20569 if (!un->un_f_suppress_cache_flush) { 20570 mutex_exit(SD_MUTEX(un)); 20571 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 20572 SD_CACHE_ENABLE); 20573 } else { 20574 mutex_exit(SD_MUTEX(un)); 20575 } 20576 20577 mutex_enter(SD_MUTEX(un)); 20578 20579 if (err) { 20580 un->un_f_write_cache_enabled = 0; 20581 } 20582 } 20583 20584 un->un_f_wcc_inprog = 0; 20585 cv_broadcast(&un->un_wcc_cv); 20586 mutex_exit(SD_MUTEX(un)); 20587 break; 20588 } 20589 20590 default: 20591 err = ENOTTY; 20592 break; 20593 } 20594 mutex_enter(SD_MUTEX(un)); 20595 un->un_ncmds_in_driver--; 20596 ASSERT(un->un_ncmds_in_driver >= 0); 20597 mutex_exit(SD_MUTEX(un)); 20598 20599 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 20600 return (err); 20601 } 20602 20603 20604 /* 20605 * Function: sd_dkio_ctrl_info 20606 * 20607 * Description: This routine is the driver entry point for handling controller 20608 * information ioctl requests (DKIOCINFO). 20609 * 20610 * Arguments: dev - the device number 20611 * arg - pointer to user provided dk_cinfo structure 20612 * specifying the controller type and attributes. 20613 * flag - this argument is a pass through to ddi_copyxxx() 20614 * directly from the mode argument of ioctl(). 20615 * 20616 * Return Code: 0 20617 * EFAULT 20618 * ENXIO 20619 */ 20620 20621 static int 20622 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 20623 { 20624 struct sd_lun *un = NULL; 20625 struct dk_cinfo *info; 20626 dev_info_t *pdip; 20627 int lun, tgt; 20628 20629 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20630 return (ENXIO); 20631 } 20632 20633 info = (struct dk_cinfo *) 20634 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 20635 20636 switch (un->un_ctype) { 20637 case CTYPE_CDROM: 20638 info->dki_ctype = DKC_CDROM; 20639 break; 20640 default: 20641 info->dki_ctype = DKC_SCSI_CCS; 20642 break; 20643 } 20644 pdip = ddi_get_parent(SD_DEVINFO(un)); 20645 info->dki_cnum = ddi_get_instance(pdip); 20646 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 20647 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 20648 } else { 20649 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 20650 DK_DEVLEN - 1); 20651 } 20652 20653 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20654 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 20655 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 20656 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 20657 20658 /* Unit Information */ 20659 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 20660 info->dki_slave = ((tgt << 3) | lun); 20661 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 20662 DK_DEVLEN - 1); 20663 info->dki_flags = DKI_FMTVOL; 20664 info->dki_partition = SDPART(dev); 20665 20666 /* Max Transfer size of this device in blocks */ 20667 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 20668 info->dki_addr = 0; 20669 info->dki_space = 0; 20670 info->dki_prio = 0; 20671 info->dki_vec = 0; 20672 20673 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 20674 kmem_free(info, sizeof (struct dk_cinfo)); 20675 return (EFAULT); 20676 } else { 20677 kmem_free(info, sizeof (struct dk_cinfo)); 20678 return (0); 20679 } 20680 } 20681 20682 20683 /* 20684 * Function: sd_get_media_info 20685 * 20686 * Description: This routine is the driver entry point for handling ioctl 20687 * requests for the media type or command set profile used by the 20688 * drive to operate on the media (DKIOCGMEDIAINFO). 20689 * 20690 * Arguments: dev - the device number 20691 * arg - pointer to user provided dk_minfo structure 20692 * specifying the media type, logical block size and 20693 * drive capacity. 20694 * flag - this argument is a pass through to ddi_copyxxx() 20695 * directly from the mode argument of ioctl(). 20696 * 20697 * Return Code: 0 20698 * EACCESS 20699 * EFAULT 20700 * ENXIO 20701 * EIO 20702 */ 20703 20704 static int 20705 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 20706 { 20707 struct sd_lun *un = NULL; 20708 struct uscsi_cmd com; 20709 struct scsi_inquiry *sinq; 20710 struct dk_minfo media_info; 20711 u_longlong_t media_capacity; 20712 uint64_t capacity; 20713 uint_t lbasize; 20714 uchar_t *out_data; 20715 uchar_t *rqbuf; 20716 int rval = 0; 20717 int rtn; 20718 20719 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 20720 (un->un_state == SD_STATE_OFFLINE)) { 20721 return (ENXIO); 20722 } 20723 20724 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 20725 20726 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 20727 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20728 20729 /* Issue a TUR to determine if the drive is ready with media present */ 20730 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 20731 if (rval == ENXIO) { 20732 goto done; 20733 } 20734 20735 /* Now get configuration data */ 20736 if (ISCD(un)) { 20737 media_info.dki_media_type = DK_CDROM; 20738 20739 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 20740 if (un->un_f_mmc_cap == TRUE) { 20741 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 20742 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN, 20743 SD_PATH_STANDARD); 20744 20745 if (rtn) { 20746 /* 20747 * Failed for other than an illegal request 20748 * or command not supported 20749 */ 20750 if ((com.uscsi_status == STATUS_CHECK) && 20751 (com.uscsi_rqstatus == STATUS_GOOD)) { 20752 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 20753 (rqbuf[12] != 0x20)) { 20754 rval = EIO; 20755 goto done; 20756 } 20757 } 20758 } else { 20759 /* 20760 * The GET CONFIGURATION command succeeded 20761 * so set the media type according to the 20762 * returned data 20763 */ 20764 media_info.dki_media_type = out_data[6]; 20765 media_info.dki_media_type <<= 8; 20766 media_info.dki_media_type |= out_data[7]; 20767 } 20768 } 20769 } else { 20770 /* 20771 * The profile list is not available, so we attempt to identify 20772 * the media type based on the inquiry data 20773 */ 20774 sinq = un->un_sd->sd_inq; 20775 if ((sinq->inq_dtype == DTYPE_DIRECT) || 20776 (sinq->inq_dtype == DTYPE_OPTICAL)) { 20777 /* This is a direct access device or optical disk */ 20778 media_info.dki_media_type = DK_FIXED_DISK; 20779 20780 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 20781 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 20782 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 20783 media_info.dki_media_type = DK_ZIP; 20784 } else if ( 20785 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 20786 media_info.dki_media_type = DK_JAZ; 20787 } 20788 } 20789 } else { 20790 /* 20791 * Not a CD, direct access or optical disk so return 20792 * unknown media 20793 */ 20794 media_info.dki_media_type = DK_UNKNOWN; 20795 } 20796 } 20797 20798 /* Now read the capacity so we can provide the lbasize and capacity */ 20799 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 20800 SD_PATH_DIRECT)) { 20801 case 0: 20802 break; 20803 case EACCES: 20804 rval = EACCES; 20805 goto done; 20806 default: 20807 rval = EIO; 20808 goto done; 20809 } 20810 20811 media_info.dki_lbsize = lbasize; 20812 media_capacity = capacity; 20813 20814 /* 20815 * sd_send_scsi_READ_CAPACITY() reports capacity in 20816 * un->un_sys_blocksize chunks. So we need to convert it into 20817 * cap.lbasize chunks. 20818 */ 20819 media_capacity *= un->un_sys_blocksize; 20820 media_capacity /= lbasize; 20821 media_info.dki_capacity = media_capacity; 20822 20823 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 20824 rval = EFAULT; 20825 /* Put goto. Anybody might add some code below in future */ 20826 goto done; 20827 } 20828 done: 20829 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 20830 kmem_free(rqbuf, SENSE_LENGTH); 20831 return (rval); 20832 } 20833 20834 20835 /* 20836 * Function: sd_check_media 20837 * 20838 * Description: This utility routine implements the functionality for the 20839 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 20840 * driver state changes from that specified by the user 20841 * (inserted or ejected). For example, if the user specifies 20842 * DKIO_EJECTED and the current media state is inserted this 20843 * routine will immediately return DKIO_INSERTED. However, if the 20844 * current media state is not inserted the user thread will be 20845 * blocked until the drive state changes. If DKIO_NONE is specified 20846 * the user thread will block until a drive state change occurs. 20847 * 20848 * Arguments: dev - the device number 20849 * state - user pointer to a dkio_state, updated with the current 20850 * drive state at return. 20851 * 20852 * Return Code: ENXIO 20853 * EIO 20854 * EAGAIN 20855 * EINTR 20856 */ 20857 20858 static int 20859 sd_check_media(dev_t dev, enum dkio_state state) 20860 { 20861 struct sd_lun *un = NULL; 20862 enum dkio_state prev_state; 20863 opaque_t token = NULL; 20864 int rval = 0; 20865 20866 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 20867 return (ENXIO); 20868 } 20869 20870 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 20871 20872 mutex_enter(SD_MUTEX(un)); 20873 20874 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 20875 "state=%x, mediastate=%x\n", state, un->un_mediastate); 20876 20877 prev_state = un->un_mediastate; 20878 20879 /* is there anything to do? */ 20880 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 20881 /* 20882 * submit the request to the scsi_watch service; 20883 * scsi_media_watch_cb() does the real work 20884 */ 20885 mutex_exit(SD_MUTEX(un)); 20886 20887 /* 20888 * This change handles the case where a scsi watch request is 20889 * added to a device that is powered down. To accomplish this 20890 * we power up the device before adding the scsi watch request, 20891 * since the scsi watch sends a TUR directly to the device 20892 * which the device cannot handle if it is powered down. 20893 */ 20894 if (sd_pm_entry(un) != DDI_SUCCESS) { 20895 mutex_enter(SD_MUTEX(un)); 20896 goto done; 20897 } 20898 20899 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 20900 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 20901 (caddr_t)dev); 20902 20903 sd_pm_exit(un); 20904 20905 mutex_enter(SD_MUTEX(un)); 20906 if (token == NULL) { 20907 rval = EAGAIN; 20908 goto done; 20909 } 20910 20911 /* 20912 * This is a special case IOCTL that doesn't return 20913 * until the media state changes. Routine sdpower 20914 * knows about and handles this so don't count it 20915 * as an active cmd in the driver, which would 20916 * keep the device busy to the pm framework. 20917 * If the count isn't decremented the device can't 20918 * be powered down. 20919 */ 20920 un->un_ncmds_in_driver--; 20921 ASSERT(un->un_ncmds_in_driver >= 0); 20922 20923 /* 20924 * if a prior request had been made, this will be the same 20925 * token, as scsi_watch was designed that way. 20926 */ 20927 un->un_swr_token = token; 20928 un->un_specified_mediastate = state; 20929 20930 /* 20931 * now wait for media change 20932 * we will not be signalled unless mediastate == state but it is 20933 * still better to test for this condition, since there is a 20934 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 20935 */ 20936 SD_TRACE(SD_LOG_COMMON, un, 20937 "sd_check_media: waiting for media state change\n"); 20938 while (un->un_mediastate == state) { 20939 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 20940 SD_TRACE(SD_LOG_COMMON, un, 20941 "sd_check_media: waiting for media state " 20942 "was interrupted\n"); 20943 un->un_ncmds_in_driver++; 20944 rval = EINTR; 20945 goto done; 20946 } 20947 SD_TRACE(SD_LOG_COMMON, un, 20948 "sd_check_media: received signal, state=%x\n", 20949 un->un_mediastate); 20950 } 20951 /* 20952 * Inc the counter to indicate the device once again 20953 * has an active outstanding cmd. 20954 */ 20955 un->un_ncmds_in_driver++; 20956 } 20957 20958 /* invalidate geometry */ 20959 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 20960 sr_ejected(un); 20961 } 20962 20963 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 20964 uint64_t capacity; 20965 uint_t lbasize; 20966 20967 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 20968 mutex_exit(SD_MUTEX(un)); 20969 /* 20970 * Since the following routines use SD_PATH_DIRECT, we must 20971 * call PM directly before the upcoming disk accesses. This 20972 * may cause the disk to be power/spin up. 20973 */ 20974 20975 if (sd_pm_entry(un) == DDI_SUCCESS) { 20976 rval = sd_send_scsi_READ_CAPACITY(un, 20977 &capacity, 20978 &lbasize, SD_PATH_DIRECT); 20979 if (rval != 0) { 20980 sd_pm_exit(un); 20981 mutex_enter(SD_MUTEX(un)); 20982 goto done; 20983 } 20984 } else { 20985 rval = EIO; 20986 mutex_enter(SD_MUTEX(un)); 20987 goto done; 20988 } 20989 mutex_enter(SD_MUTEX(un)); 20990 20991 sd_update_block_info(un, lbasize, capacity); 20992 20993 /* 20994 * Check if the media in the device is writable or not 20995 */ 20996 if (ISCD(un)) 20997 sd_check_for_writable_cd(un, SD_PATH_DIRECT); 20998 20999 mutex_exit(SD_MUTEX(un)); 21000 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT); 21001 if ((cmlb_validate(un->un_cmlbhandle, 0, 21002 (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) { 21003 sd_set_pstats(un); 21004 SD_TRACE(SD_LOG_IO_PARTITION, un, 21005 "sd_check_media: un:0x%p pstats created and " 21006 "set\n", un); 21007 } 21008 21009 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21010 SD_PATH_DIRECT); 21011 sd_pm_exit(un); 21012 21013 mutex_enter(SD_MUTEX(un)); 21014 } 21015 done: 21016 un->un_f_watcht_stopped = FALSE; 21017 if (un->un_swr_token) { 21018 /* 21019 * Use of this local token and the mutex ensures that we avoid 21020 * some race conditions associated with terminating the 21021 * scsi watch. 21022 */ 21023 token = un->un_swr_token; 21024 un->un_swr_token = (opaque_t)NULL; 21025 mutex_exit(SD_MUTEX(un)); 21026 (void) scsi_watch_request_terminate(token, 21027 SCSI_WATCH_TERMINATE_WAIT); 21028 mutex_enter(SD_MUTEX(un)); 21029 } 21030 21031 /* 21032 * Update the capacity kstat value, if no media previously 21033 * (capacity kstat is 0) and a media has been inserted 21034 * (un_f_blockcount_is_valid == TRUE) 21035 */ 21036 if (un->un_errstats) { 21037 struct sd_errstats *stp = NULL; 21038 21039 stp = (struct sd_errstats *)un->un_errstats->ks_data; 21040 if ((stp->sd_capacity.value.ui64 == 0) && 21041 (un->un_f_blockcount_is_valid == TRUE)) { 21042 stp->sd_capacity.value.ui64 = 21043 (uint64_t)((uint64_t)un->un_blockcount * 21044 un->un_sys_blocksize); 21045 } 21046 } 21047 mutex_exit(SD_MUTEX(un)); 21048 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 21049 return (rval); 21050 } 21051 21052 21053 /* 21054 * Function: sd_delayed_cv_broadcast 21055 * 21056 * Description: Delayed cv_broadcast to allow for target to recover from media 21057 * insertion. 21058 * 21059 * Arguments: arg - driver soft state (unit) structure 21060 */ 21061 21062 static void 21063 sd_delayed_cv_broadcast(void *arg) 21064 { 21065 struct sd_lun *un = arg; 21066 21067 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 21068 21069 mutex_enter(SD_MUTEX(un)); 21070 un->un_dcvb_timeid = NULL; 21071 cv_broadcast(&un->un_state_cv); 21072 mutex_exit(SD_MUTEX(un)); 21073 } 21074 21075 21076 /* 21077 * Function: sd_media_watch_cb 21078 * 21079 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 21080 * routine processes the TUR sense data and updates the driver 21081 * state if a transition has occurred. The user thread 21082 * (sd_check_media) is then signalled. 21083 * 21084 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21085 * among multiple watches that share this callback function 21086 * resultp - scsi watch facility result packet containing scsi 21087 * packet, status byte and sense data 21088 * 21089 * Return Code: 0 for success, -1 for failure 21090 */ 21091 21092 static int 21093 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 21094 { 21095 struct sd_lun *un; 21096 struct scsi_status *statusp = resultp->statusp; 21097 uint8_t *sensep = (uint8_t *)resultp->sensep; 21098 enum dkio_state state = DKIO_NONE; 21099 dev_t dev = (dev_t)arg; 21100 uchar_t actual_sense_length; 21101 uint8_t skey, asc, ascq; 21102 21103 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21104 return (-1); 21105 } 21106 actual_sense_length = resultp->actual_sense_length; 21107 21108 mutex_enter(SD_MUTEX(un)); 21109 SD_TRACE(SD_LOG_COMMON, un, 21110 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 21111 *((char *)statusp), (void *)sensep, actual_sense_length); 21112 21113 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 21114 un->un_mediastate = DKIO_DEV_GONE; 21115 cv_broadcast(&un->un_state_cv); 21116 mutex_exit(SD_MUTEX(un)); 21117 21118 return (0); 21119 } 21120 21121 /* 21122 * If there was a check condition then sensep points to valid sense data 21123 * If status was not a check condition but a reservation or busy status 21124 * then the new state is DKIO_NONE 21125 */ 21126 if (sensep != NULL) { 21127 skey = scsi_sense_key(sensep); 21128 asc = scsi_sense_asc(sensep); 21129 ascq = scsi_sense_ascq(sensep); 21130 21131 SD_INFO(SD_LOG_COMMON, un, 21132 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 21133 skey, asc, ascq); 21134 /* This routine only uses up to 13 bytes of sense data. */ 21135 if (actual_sense_length >= 13) { 21136 if (skey == KEY_UNIT_ATTENTION) { 21137 if (asc == 0x28) { 21138 state = DKIO_INSERTED; 21139 } 21140 } else if (skey == KEY_NOT_READY) { 21141 /* 21142 * if 02/04/02 means that the host 21143 * should send start command. Explicitly 21144 * leave the media state as is 21145 * (inserted) as the media is inserted 21146 * and host has stopped device for PM 21147 * reasons. Upon next true read/write 21148 * to this media will bring the 21149 * device to the right state good for 21150 * media access. 21151 */ 21152 if (asc == 0x3a) { 21153 state = DKIO_EJECTED; 21154 } else { 21155 /* 21156 * If the drive is busy with an 21157 * operation or long write, keep the 21158 * media in an inserted state. 21159 */ 21160 21161 if ((asc == 0x04) && 21162 ((ascq == 0x02) || 21163 (ascq == 0x07) || 21164 (ascq == 0x08))) { 21165 state = DKIO_INSERTED; 21166 } 21167 } 21168 } else if (skey == KEY_NO_SENSE) { 21169 if ((asc == 0x00) && (ascq == 0x00)) { 21170 /* 21171 * Sense Data 00/00/00 does not provide 21172 * any information about the state of 21173 * the media. Ignore it. 21174 */ 21175 mutex_exit(SD_MUTEX(un)); 21176 return (0); 21177 } 21178 } 21179 } 21180 } else if ((*((char *)statusp) == STATUS_GOOD) && 21181 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 21182 state = DKIO_INSERTED; 21183 } 21184 21185 SD_TRACE(SD_LOG_COMMON, un, 21186 "sd_media_watch_cb: state=%x, specified=%x\n", 21187 state, un->un_specified_mediastate); 21188 21189 /* 21190 * now signal the waiting thread if this is *not* the specified state; 21191 * delay the signal if the state is DKIO_INSERTED to allow the target 21192 * to recover 21193 */ 21194 if (state != un->un_specified_mediastate) { 21195 un->un_mediastate = state; 21196 if (state == DKIO_INSERTED) { 21197 /* 21198 * delay the signal to give the drive a chance 21199 * to do what it apparently needs to do 21200 */ 21201 SD_TRACE(SD_LOG_COMMON, un, 21202 "sd_media_watch_cb: delayed cv_broadcast\n"); 21203 if (un->un_dcvb_timeid == NULL) { 21204 un->un_dcvb_timeid = 21205 timeout(sd_delayed_cv_broadcast, un, 21206 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 21207 } 21208 } else { 21209 SD_TRACE(SD_LOG_COMMON, un, 21210 "sd_media_watch_cb: immediate cv_broadcast\n"); 21211 cv_broadcast(&un->un_state_cv); 21212 } 21213 } 21214 mutex_exit(SD_MUTEX(un)); 21215 return (0); 21216 } 21217 21218 21219 /* 21220 * Function: sd_dkio_get_temp 21221 * 21222 * Description: This routine is the driver entry point for handling ioctl 21223 * requests to get the disk temperature. 21224 * 21225 * Arguments: dev - the device number 21226 * arg - pointer to user provided dk_temperature structure. 21227 * flag - this argument is a pass through to ddi_copyxxx() 21228 * directly from the mode argument of ioctl(). 21229 * 21230 * Return Code: 0 21231 * EFAULT 21232 * ENXIO 21233 * EAGAIN 21234 */ 21235 21236 static int 21237 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 21238 { 21239 struct sd_lun *un = NULL; 21240 struct dk_temperature *dktemp = NULL; 21241 uchar_t *temperature_page; 21242 int rval = 0; 21243 int path_flag = SD_PATH_STANDARD; 21244 21245 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21246 return (ENXIO); 21247 } 21248 21249 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 21250 21251 /* copyin the disk temp argument to get the user flags */ 21252 if (ddi_copyin((void *)arg, dktemp, 21253 sizeof (struct dk_temperature), flag) != 0) { 21254 rval = EFAULT; 21255 goto done; 21256 } 21257 21258 /* Initialize the temperature to invalid. */ 21259 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 21260 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 21261 21262 /* 21263 * Note: Investigate removing the "bypass pm" semantic. 21264 * Can we just bypass PM always? 21265 */ 21266 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 21267 path_flag = SD_PATH_DIRECT; 21268 ASSERT(!mutex_owned(&un->un_pm_mutex)); 21269 mutex_enter(&un->un_pm_mutex); 21270 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 21271 /* 21272 * If DKT_BYPASS_PM is set, and the drive happens to be 21273 * in low power mode, we can not wake it up, Need to 21274 * return EAGAIN. 21275 */ 21276 mutex_exit(&un->un_pm_mutex); 21277 rval = EAGAIN; 21278 goto done; 21279 } else { 21280 /* 21281 * Indicate to PM the device is busy. This is required 21282 * to avoid a race - i.e. the ioctl is issuing a 21283 * command and the pm framework brings down the device 21284 * to low power mode (possible power cut-off on some 21285 * platforms). 21286 */ 21287 mutex_exit(&un->un_pm_mutex); 21288 if (sd_pm_entry(un) != DDI_SUCCESS) { 21289 rval = EAGAIN; 21290 goto done; 21291 } 21292 } 21293 } 21294 21295 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 21296 21297 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 21298 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 21299 goto done2; 21300 } 21301 21302 /* 21303 * For the current temperature verify that the parameter length is 0x02 21304 * and the parameter code is 0x00 21305 */ 21306 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 21307 (temperature_page[5] == 0x00)) { 21308 if (temperature_page[9] == 0xFF) { 21309 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 21310 } else { 21311 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 21312 } 21313 } 21314 21315 /* 21316 * For the reference temperature verify that the parameter 21317 * length is 0x02 and the parameter code is 0x01 21318 */ 21319 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 21320 (temperature_page[11] == 0x01)) { 21321 if (temperature_page[15] == 0xFF) { 21322 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 21323 } else { 21324 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 21325 } 21326 } 21327 21328 /* Do the copyout regardless of the temperature commands status. */ 21329 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 21330 flag) != 0) { 21331 rval = EFAULT; 21332 } 21333 21334 done2: 21335 if (path_flag == SD_PATH_DIRECT) { 21336 sd_pm_exit(un); 21337 } 21338 21339 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 21340 done: 21341 if (dktemp != NULL) { 21342 kmem_free(dktemp, sizeof (struct dk_temperature)); 21343 } 21344 21345 return (rval); 21346 } 21347 21348 21349 /* 21350 * Function: sd_log_page_supported 21351 * 21352 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 21353 * supported log pages. 21354 * 21355 * Arguments: un - 21356 * log_page - 21357 * 21358 * Return Code: -1 - on error (log sense is optional and may not be supported). 21359 * 0 - log page not found. 21360 * 1 - log page found. 21361 */ 21362 21363 static int 21364 sd_log_page_supported(struct sd_lun *un, int log_page) 21365 { 21366 uchar_t *log_page_data; 21367 int i; 21368 int match = 0; 21369 int log_size; 21370 21371 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 21372 21373 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 21374 SD_PATH_DIRECT) != 0) { 21375 SD_ERROR(SD_LOG_COMMON, un, 21376 "sd_log_page_supported: failed log page retrieval\n"); 21377 kmem_free(log_page_data, 0xFF); 21378 return (-1); 21379 } 21380 log_size = log_page_data[3]; 21381 21382 /* 21383 * The list of supported log pages start from the fourth byte. Check 21384 * until we run out of log pages or a match is found. 21385 */ 21386 for (i = 4; (i < (log_size + 4)) && !match; i++) { 21387 if (log_page_data[i] == log_page) { 21388 match++; 21389 } 21390 } 21391 kmem_free(log_page_data, 0xFF); 21392 return (match); 21393 } 21394 21395 21396 /* 21397 * Function: sd_mhdioc_failfast 21398 * 21399 * Description: This routine is the driver entry point for handling ioctl 21400 * requests to enable/disable the multihost failfast option. 21401 * (MHIOCENFAILFAST) 21402 * 21403 * Arguments: dev - the device number 21404 * arg - user specified probing interval. 21405 * flag - this argument is a pass through to ddi_copyxxx() 21406 * directly from the mode argument of ioctl(). 21407 * 21408 * Return Code: 0 21409 * EFAULT 21410 * ENXIO 21411 */ 21412 21413 static int 21414 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 21415 { 21416 struct sd_lun *un = NULL; 21417 int mh_time; 21418 int rval = 0; 21419 21420 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21421 return (ENXIO); 21422 } 21423 21424 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 21425 return (EFAULT); 21426 21427 if (mh_time) { 21428 mutex_enter(SD_MUTEX(un)); 21429 un->un_resvd_status |= SD_FAILFAST; 21430 mutex_exit(SD_MUTEX(un)); 21431 /* 21432 * If mh_time is INT_MAX, then this ioctl is being used for 21433 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 21434 */ 21435 if (mh_time != INT_MAX) { 21436 rval = sd_check_mhd(dev, mh_time); 21437 } 21438 } else { 21439 (void) sd_check_mhd(dev, 0); 21440 mutex_enter(SD_MUTEX(un)); 21441 un->un_resvd_status &= ~SD_FAILFAST; 21442 mutex_exit(SD_MUTEX(un)); 21443 } 21444 return (rval); 21445 } 21446 21447 21448 /* 21449 * Function: sd_mhdioc_takeown 21450 * 21451 * Description: This routine is the driver entry point for handling ioctl 21452 * requests to forcefully acquire exclusive access rights to the 21453 * multihost disk (MHIOCTKOWN). 21454 * 21455 * Arguments: dev - the device number 21456 * arg - user provided structure specifying the delay 21457 * parameters in milliseconds 21458 * flag - this argument is a pass through to ddi_copyxxx() 21459 * directly from the mode argument of ioctl(). 21460 * 21461 * Return Code: 0 21462 * EFAULT 21463 * ENXIO 21464 */ 21465 21466 static int 21467 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 21468 { 21469 struct sd_lun *un = NULL; 21470 struct mhioctkown *tkown = NULL; 21471 int rval = 0; 21472 21473 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21474 return (ENXIO); 21475 } 21476 21477 if (arg != NULL) { 21478 tkown = (struct mhioctkown *) 21479 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 21480 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 21481 if (rval != 0) { 21482 rval = EFAULT; 21483 goto error; 21484 } 21485 } 21486 21487 rval = sd_take_ownership(dev, tkown); 21488 mutex_enter(SD_MUTEX(un)); 21489 if (rval == 0) { 21490 un->un_resvd_status |= SD_RESERVE; 21491 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 21492 sd_reinstate_resv_delay = 21493 tkown->reinstate_resv_delay * 1000; 21494 } else { 21495 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 21496 } 21497 /* 21498 * Give the scsi_watch routine interval set by 21499 * the MHIOCENFAILFAST ioctl precedence here. 21500 */ 21501 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 21502 mutex_exit(SD_MUTEX(un)); 21503 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 21504 SD_TRACE(SD_LOG_IOCTL_MHD, un, 21505 "sd_mhdioc_takeown : %d\n", 21506 sd_reinstate_resv_delay); 21507 } else { 21508 mutex_exit(SD_MUTEX(un)); 21509 } 21510 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 21511 sd_mhd_reset_notify_cb, (caddr_t)un); 21512 } else { 21513 un->un_resvd_status &= ~SD_RESERVE; 21514 mutex_exit(SD_MUTEX(un)); 21515 } 21516 21517 error: 21518 if (tkown != NULL) { 21519 kmem_free(tkown, sizeof (struct mhioctkown)); 21520 } 21521 return (rval); 21522 } 21523 21524 21525 /* 21526 * Function: sd_mhdioc_release 21527 * 21528 * Description: This routine is the driver entry point for handling ioctl 21529 * requests to release exclusive access rights to the multihost 21530 * disk (MHIOCRELEASE). 21531 * 21532 * Arguments: dev - the device number 21533 * 21534 * Return Code: 0 21535 * ENXIO 21536 */ 21537 21538 static int 21539 sd_mhdioc_release(dev_t dev) 21540 { 21541 struct sd_lun *un = NULL; 21542 timeout_id_t resvd_timeid_save; 21543 int resvd_status_save; 21544 int rval = 0; 21545 21546 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21547 return (ENXIO); 21548 } 21549 21550 mutex_enter(SD_MUTEX(un)); 21551 resvd_status_save = un->un_resvd_status; 21552 un->un_resvd_status &= 21553 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 21554 if (un->un_resvd_timeid) { 21555 resvd_timeid_save = un->un_resvd_timeid; 21556 un->un_resvd_timeid = NULL; 21557 mutex_exit(SD_MUTEX(un)); 21558 (void) untimeout(resvd_timeid_save); 21559 } else { 21560 mutex_exit(SD_MUTEX(un)); 21561 } 21562 21563 /* 21564 * destroy any pending timeout thread that may be attempting to 21565 * reinstate reservation on this device. 21566 */ 21567 sd_rmv_resv_reclaim_req(dev); 21568 21569 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 21570 mutex_enter(SD_MUTEX(un)); 21571 if ((un->un_mhd_token) && 21572 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 21573 mutex_exit(SD_MUTEX(un)); 21574 (void) sd_check_mhd(dev, 0); 21575 } else { 21576 mutex_exit(SD_MUTEX(un)); 21577 } 21578 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 21579 sd_mhd_reset_notify_cb, (caddr_t)un); 21580 } else { 21581 /* 21582 * sd_mhd_watch_cb will restart the resvd recover timeout thread 21583 */ 21584 mutex_enter(SD_MUTEX(un)); 21585 un->un_resvd_status = resvd_status_save; 21586 mutex_exit(SD_MUTEX(un)); 21587 } 21588 return (rval); 21589 } 21590 21591 21592 /* 21593 * Function: sd_mhdioc_register_devid 21594 * 21595 * Description: This routine is the driver entry point for handling ioctl 21596 * requests to register the device id (MHIOCREREGISTERDEVID). 21597 * 21598 * Note: The implementation for this ioctl has been updated to 21599 * be consistent with the original PSARC case (1999/357) 21600 * (4375899, 4241671, 4220005) 21601 * 21602 * Arguments: dev - the device number 21603 * 21604 * Return Code: 0 21605 * ENXIO 21606 */ 21607 21608 static int 21609 sd_mhdioc_register_devid(dev_t dev) 21610 { 21611 struct sd_lun *un = NULL; 21612 int rval = 0; 21613 21614 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21615 return (ENXIO); 21616 } 21617 21618 ASSERT(!mutex_owned(SD_MUTEX(un))); 21619 21620 mutex_enter(SD_MUTEX(un)); 21621 21622 /* If a devid already exists, de-register it */ 21623 if (un->un_devid != NULL) { 21624 ddi_devid_unregister(SD_DEVINFO(un)); 21625 /* 21626 * After unregister devid, needs to free devid memory 21627 */ 21628 ddi_devid_free(un->un_devid); 21629 un->un_devid = NULL; 21630 } 21631 21632 /* Check for reservation conflict */ 21633 mutex_exit(SD_MUTEX(un)); 21634 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 21635 mutex_enter(SD_MUTEX(un)); 21636 21637 switch (rval) { 21638 case 0: 21639 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 21640 break; 21641 case EACCES: 21642 break; 21643 default: 21644 rval = EIO; 21645 } 21646 21647 mutex_exit(SD_MUTEX(un)); 21648 return (rval); 21649 } 21650 21651 21652 /* 21653 * Function: sd_mhdioc_inkeys 21654 * 21655 * Description: This routine is the driver entry point for handling ioctl 21656 * requests to issue the SCSI-3 Persistent In Read Keys command 21657 * to the device (MHIOCGRP_INKEYS). 21658 * 21659 * Arguments: dev - the device number 21660 * arg - user provided in_keys structure 21661 * flag - this argument is a pass through to ddi_copyxxx() 21662 * directly from the mode argument of ioctl(). 21663 * 21664 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 21665 * ENXIO 21666 * EFAULT 21667 */ 21668 21669 static int 21670 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 21671 { 21672 struct sd_lun *un; 21673 mhioc_inkeys_t inkeys; 21674 int rval = 0; 21675 21676 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21677 return (ENXIO); 21678 } 21679 21680 #ifdef _MULTI_DATAMODEL 21681 switch (ddi_model_convert_from(flag & FMODELS)) { 21682 case DDI_MODEL_ILP32: { 21683 struct mhioc_inkeys32 inkeys32; 21684 21685 if (ddi_copyin(arg, &inkeys32, 21686 sizeof (struct mhioc_inkeys32), flag) != 0) { 21687 return (EFAULT); 21688 } 21689 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 21690 if ((rval = sd_persistent_reservation_in_read_keys(un, 21691 &inkeys, flag)) != 0) { 21692 return (rval); 21693 } 21694 inkeys32.generation = inkeys.generation; 21695 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 21696 flag) != 0) { 21697 return (EFAULT); 21698 } 21699 break; 21700 } 21701 case DDI_MODEL_NONE: 21702 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 21703 flag) != 0) { 21704 return (EFAULT); 21705 } 21706 if ((rval = sd_persistent_reservation_in_read_keys(un, 21707 &inkeys, flag)) != 0) { 21708 return (rval); 21709 } 21710 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 21711 flag) != 0) { 21712 return (EFAULT); 21713 } 21714 break; 21715 } 21716 21717 #else /* ! _MULTI_DATAMODEL */ 21718 21719 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 21720 return (EFAULT); 21721 } 21722 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 21723 if (rval != 0) { 21724 return (rval); 21725 } 21726 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 21727 return (EFAULT); 21728 } 21729 21730 #endif /* _MULTI_DATAMODEL */ 21731 21732 return (rval); 21733 } 21734 21735 21736 /* 21737 * Function: sd_mhdioc_inresv 21738 * 21739 * Description: This routine is the driver entry point for handling ioctl 21740 * requests to issue the SCSI-3 Persistent In Read Reservations 21741 * command to the device (MHIOCGRP_INKEYS). 21742 * 21743 * Arguments: dev - the device number 21744 * arg - user provided in_resv structure 21745 * flag - this argument is a pass through to ddi_copyxxx() 21746 * directly from the mode argument of ioctl(). 21747 * 21748 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 21749 * ENXIO 21750 * EFAULT 21751 */ 21752 21753 static int 21754 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 21755 { 21756 struct sd_lun *un; 21757 mhioc_inresvs_t inresvs; 21758 int rval = 0; 21759 21760 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21761 return (ENXIO); 21762 } 21763 21764 #ifdef _MULTI_DATAMODEL 21765 21766 switch (ddi_model_convert_from(flag & FMODELS)) { 21767 case DDI_MODEL_ILP32: { 21768 struct mhioc_inresvs32 inresvs32; 21769 21770 if (ddi_copyin(arg, &inresvs32, 21771 sizeof (struct mhioc_inresvs32), flag) != 0) { 21772 return (EFAULT); 21773 } 21774 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 21775 if ((rval = sd_persistent_reservation_in_read_resv(un, 21776 &inresvs, flag)) != 0) { 21777 return (rval); 21778 } 21779 inresvs32.generation = inresvs.generation; 21780 if (ddi_copyout(&inresvs32, arg, 21781 sizeof (struct mhioc_inresvs32), flag) != 0) { 21782 return (EFAULT); 21783 } 21784 break; 21785 } 21786 case DDI_MODEL_NONE: 21787 if (ddi_copyin(arg, &inresvs, 21788 sizeof (mhioc_inresvs_t), flag) != 0) { 21789 return (EFAULT); 21790 } 21791 if ((rval = sd_persistent_reservation_in_read_resv(un, 21792 &inresvs, flag)) != 0) { 21793 return (rval); 21794 } 21795 if (ddi_copyout(&inresvs, arg, 21796 sizeof (mhioc_inresvs_t), flag) != 0) { 21797 return (EFAULT); 21798 } 21799 break; 21800 } 21801 21802 #else /* ! _MULTI_DATAMODEL */ 21803 21804 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 21805 return (EFAULT); 21806 } 21807 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 21808 if (rval != 0) { 21809 return (rval); 21810 } 21811 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 21812 return (EFAULT); 21813 } 21814 21815 #endif /* ! _MULTI_DATAMODEL */ 21816 21817 return (rval); 21818 } 21819 21820 21821 /* 21822 * The following routines support the clustering functionality described below 21823 * and implement lost reservation reclaim functionality. 21824 * 21825 * Clustering 21826 * ---------- 21827 * The clustering code uses two different, independent forms of SCSI 21828 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 21829 * Persistent Group Reservations. For any particular disk, it will use either 21830 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 21831 * 21832 * SCSI-2 21833 * The cluster software takes ownership of a multi-hosted disk by issuing the 21834 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 21835 * MHIOCRELEASE ioctl. Closely related is the MHIOCENFAILFAST ioctl -- a 21836 * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl 21837 * then issues the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the 21838 * driver. The meaning of failfast is that if the driver (on this host) ever 21839 * encounters the scsi error return code RESERVATION_CONFLICT from the device, 21840 * it should immediately panic the host. The motivation for this ioctl is that 21841 * if this host does encounter reservation conflict, the underlying cause is 21842 * that some other host of the cluster has decided that this host is no longer 21843 * in the cluster and has seized control of the disks for itself. Since this 21844 * host is no longer in the cluster, it ought to panic itself. The 21845 * MHIOCENFAILFAST ioctl does two things: 21846 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 21847 * error to panic the host 21848 * (b) it sets up a periodic timer to test whether this host still has 21849 * "access" (in that no other host has reserved the device): if the 21850 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 21851 * purpose of that periodic timer is to handle scenarios where the host is 21852 * otherwise temporarily quiescent, temporarily doing no real i/o. 21853 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 21854 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 21855 * the device itself. 21856 * 21857 * SCSI-3 PGR 21858 * A direct semantic implementation of the SCSI-3 Persistent Reservation 21859 * facility is supported through the shared multihost disk ioctls 21860 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 21861 * MHIOCGRP_PREEMPTANDABORT) 21862 * 21863 * Reservation Reclaim: 21864 * -------------------- 21865 * To support the lost reservation reclaim operations this driver creates a 21866 * single thread to handle reinstating reservations on all devices that have 21867 * lost reservations sd_resv_reclaim_requests are logged for all devices that 21868 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 21869 * and the reservation reclaim thread loops through the requests to regain the 21870 * lost reservations. 21871 */ 21872 21873 /* 21874 * Function: sd_check_mhd() 21875 * 21876 * Description: This function sets up and submits a scsi watch request or 21877 * terminates an existing watch request. This routine is used in 21878 * support of reservation reclaim. 21879 * 21880 * Arguments: dev - the device 'dev_t' is used for context to discriminate 21881 * among multiple watches that share the callback function 21882 * interval - the number of microseconds specifying the watch 21883 * interval for issuing TEST UNIT READY commands. If 21884 * set to 0 the watch should be terminated. If the 21885 * interval is set to 0 and if the device is required 21886 * to hold reservation while disabling failfast, the 21887 * watch is restarted with an interval of 21888 * reinstate_resv_delay. 21889 * 21890 * Return Code: 0 - Successful submit/terminate of scsi watch request 21891 * ENXIO - Indicates an invalid device was specified 21892 * EAGAIN - Unable to submit the scsi watch request 21893 */ 21894 21895 static int 21896 sd_check_mhd(dev_t dev, int interval) 21897 { 21898 struct sd_lun *un; 21899 opaque_t token; 21900 21901 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21902 return (ENXIO); 21903 } 21904 21905 /* is this a watch termination request? */ 21906 if (interval == 0) { 21907 mutex_enter(SD_MUTEX(un)); 21908 /* if there is an existing watch task then terminate it */ 21909 if (un->un_mhd_token) { 21910 token = un->un_mhd_token; 21911 un->un_mhd_token = NULL; 21912 mutex_exit(SD_MUTEX(un)); 21913 (void) scsi_watch_request_terminate(token, 21914 SCSI_WATCH_TERMINATE_WAIT); 21915 mutex_enter(SD_MUTEX(un)); 21916 } else { 21917 mutex_exit(SD_MUTEX(un)); 21918 /* 21919 * Note: If we return here we don't check for the 21920 * failfast case. This is the original legacy 21921 * implementation but perhaps we should be checking 21922 * the failfast case. 21923 */ 21924 return (0); 21925 } 21926 /* 21927 * If the device is required to hold reservation while 21928 * disabling failfast, we need to restart the scsi_watch 21929 * routine with an interval of reinstate_resv_delay. 21930 */ 21931 if (un->un_resvd_status & SD_RESERVE) { 21932 interval = sd_reinstate_resv_delay/1000; 21933 } else { 21934 /* no failfast so bail */ 21935 mutex_exit(SD_MUTEX(un)); 21936 return (0); 21937 } 21938 mutex_exit(SD_MUTEX(un)); 21939 } 21940 21941 /* 21942 * adjust minimum time interval to 1 second, 21943 * and convert from msecs to usecs 21944 */ 21945 if (interval > 0 && interval < 1000) { 21946 interval = 1000; 21947 } 21948 interval *= 1000; 21949 21950 /* 21951 * submit the request to the scsi_watch service 21952 */ 21953 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 21954 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 21955 if (token == NULL) { 21956 return (EAGAIN); 21957 } 21958 21959 /* 21960 * save token for termination later on 21961 */ 21962 mutex_enter(SD_MUTEX(un)); 21963 un->un_mhd_token = token; 21964 mutex_exit(SD_MUTEX(un)); 21965 return (0); 21966 } 21967 21968 21969 /* 21970 * Function: sd_mhd_watch_cb() 21971 * 21972 * Description: This function is the call back function used by the scsi watch 21973 * facility. The scsi watch facility sends the "Test Unit Ready" 21974 * and processes the status. If applicable (i.e. a "Unit Attention" 21975 * status and automatic "Request Sense" not used) the scsi watch 21976 * facility will send a "Request Sense" and retrieve the sense data 21977 * to be passed to this callback function. In either case the 21978 * automatic "Request Sense" or the facility submitting one, this 21979 * callback is passed the status and sense data. 21980 * 21981 * Arguments: arg - the device 'dev_t' is used for context to discriminate 21982 * among multiple watches that share this callback function 21983 * resultp - scsi watch facility result packet containing scsi 21984 * packet, status byte and sense data 21985 * 21986 * Return Code: 0 - continue the watch task 21987 * non-zero - terminate the watch task 21988 */ 21989 21990 static int 21991 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 21992 { 21993 struct sd_lun *un; 21994 struct scsi_status *statusp; 21995 uint8_t *sensep; 21996 struct scsi_pkt *pkt; 21997 uchar_t actual_sense_length; 21998 dev_t dev = (dev_t)arg; 21999 22000 ASSERT(resultp != NULL); 22001 statusp = resultp->statusp; 22002 sensep = (uint8_t *)resultp->sensep; 22003 pkt = resultp->pkt; 22004 actual_sense_length = resultp->actual_sense_length; 22005 22006 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22007 return (ENXIO); 22008 } 22009 22010 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22011 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 22012 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 22013 22014 /* Begin processing of the status and/or sense data */ 22015 if (pkt->pkt_reason != CMD_CMPLT) { 22016 /* Handle the incomplete packet */ 22017 sd_mhd_watch_incomplete(un, pkt); 22018 return (0); 22019 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 22020 if (*((unsigned char *)statusp) 22021 == STATUS_RESERVATION_CONFLICT) { 22022 /* 22023 * Handle a reservation conflict by panicking if 22024 * configured for failfast or by logging the conflict 22025 * and updating the reservation status 22026 */ 22027 mutex_enter(SD_MUTEX(un)); 22028 if ((un->un_resvd_status & SD_FAILFAST) && 22029 (sd_failfast_enable)) { 22030 sd_panic_for_res_conflict(un); 22031 /*NOTREACHED*/ 22032 } 22033 SD_INFO(SD_LOG_IOCTL_MHD, un, 22034 "sd_mhd_watch_cb: Reservation Conflict\n"); 22035 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 22036 mutex_exit(SD_MUTEX(un)); 22037 } 22038 } 22039 22040 if (sensep != NULL) { 22041 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 22042 mutex_enter(SD_MUTEX(un)); 22043 if ((scsi_sense_asc(sensep) == 22044 SD_SCSI_RESET_SENSE_CODE) && 22045 (un->un_resvd_status & SD_RESERVE)) { 22046 /* 22047 * The additional sense code indicates a power 22048 * on or bus device reset has occurred; update 22049 * the reservation status. 22050 */ 22051 un->un_resvd_status |= 22052 (SD_LOST_RESERVE | SD_WANT_RESERVE); 22053 SD_INFO(SD_LOG_IOCTL_MHD, un, 22054 "sd_mhd_watch_cb: Lost Reservation\n"); 22055 } 22056 } else { 22057 return (0); 22058 } 22059 } else { 22060 mutex_enter(SD_MUTEX(un)); 22061 } 22062 22063 if ((un->un_resvd_status & SD_RESERVE) && 22064 (un->un_resvd_status & SD_LOST_RESERVE)) { 22065 if (un->un_resvd_status & SD_WANT_RESERVE) { 22066 /* 22067 * A reset occurred in between the last probe and this 22068 * one so if a timeout is pending cancel it. 22069 */ 22070 if (un->un_resvd_timeid) { 22071 timeout_id_t temp_id = un->un_resvd_timeid; 22072 un->un_resvd_timeid = NULL; 22073 mutex_exit(SD_MUTEX(un)); 22074 (void) untimeout(temp_id); 22075 mutex_enter(SD_MUTEX(un)); 22076 } 22077 un->un_resvd_status &= ~SD_WANT_RESERVE; 22078 } 22079 if (un->un_resvd_timeid == 0) { 22080 /* Schedule a timeout to handle the lost reservation */ 22081 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 22082 (void *)dev, 22083 drv_usectohz(sd_reinstate_resv_delay)); 22084 } 22085 } 22086 mutex_exit(SD_MUTEX(un)); 22087 return (0); 22088 } 22089 22090 22091 /* 22092 * Function: sd_mhd_watch_incomplete() 22093 * 22094 * Description: This function is used to find out why a scsi pkt sent by the 22095 * scsi watch facility was not completed. Under some scenarios this 22096 * routine will return. Otherwise it will send a bus reset to see 22097 * if the drive is still online. 22098 * 22099 * Arguments: un - driver soft state (unit) structure 22100 * pkt - incomplete scsi pkt 22101 */ 22102 22103 static void 22104 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 22105 { 22106 int be_chatty; 22107 int perr; 22108 22109 ASSERT(pkt != NULL); 22110 ASSERT(un != NULL); 22111 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 22112 perr = (pkt->pkt_statistics & STAT_PERR); 22113 22114 mutex_enter(SD_MUTEX(un)); 22115 if (un->un_state == SD_STATE_DUMPING) { 22116 mutex_exit(SD_MUTEX(un)); 22117 return; 22118 } 22119 22120 switch (pkt->pkt_reason) { 22121 case CMD_UNX_BUS_FREE: 22122 /* 22123 * If we had a parity error that caused the target to drop BSY*, 22124 * don't be chatty about it. 22125 */ 22126 if (perr && be_chatty) { 22127 be_chatty = 0; 22128 } 22129 break; 22130 case CMD_TAG_REJECT: 22131 /* 22132 * The SCSI-2 spec states that a tag reject will be sent by the 22133 * target if tagged queuing is not supported. A tag reject may 22134 * also be sent during certain initialization periods or to 22135 * control internal resources. For the latter case the target 22136 * may also return Queue Full. 22137 * 22138 * If this driver receives a tag reject from a target that is 22139 * going through an init period or controlling internal 22140 * resources tagged queuing will be disabled. This is a less 22141 * than optimal behavior but the driver is unable to determine 22142 * the target state and assumes tagged queueing is not supported 22143 */ 22144 pkt->pkt_flags = 0; 22145 un->un_tagflags = 0; 22146 22147 if (un->un_f_opt_queueing == TRUE) { 22148 un->un_throttle = min(un->un_throttle, 3); 22149 } else { 22150 un->un_throttle = 1; 22151 } 22152 mutex_exit(SD_MUTEX(un)); 22153 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 22154 mutex_enter(SD_MUTEX(un)); 22155 break; 22156 case CMD_INCOMPLETE: 22157 /* 22158 * The transport stopped with an abnormal state, fallthrough and 22159 * reset the target and/or bus unless selection did not complete 22160 * (indicated by STATE_GOT_BUS) in which case we don't want to 22161 * go through a target/bus reset 22162 */ 22163 if (pkt->pkt_state == STATE_GOT_BUS) { 22164 break; 22165 } 22166 /*FALLTHROUGH*/ 22167 22168 case CMD_TIMEOUT: 22169 default: 22170 /* 22171 * The lun may still be running the command, so a lun reset 22172 * should be attempted. If the lun reset fails or cannot be 22173 * issued, than try a target reset. Lastly try a bus reset. 22174 */ 22175 if ((pkt->pkt_statistics & 22176 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 22177 int reset_retval = 0; 22178 mutex_exit(SD_MUTEX(un)); 22179 if (un->un_f_allow_bus_device_reset == TRUE) { 22180 if (un->un_f_lun_reset_enabled == TRUE) { 22181 reset_retval = 22182 scsi_reset(SD_ADDRESS(un), 22183 RESET_LUN); 22184 } 22185 if (reset_retval == 0) { 22186 reset_retval = 22187 scsi_reset(SD_ADDRESS(un), 22188 RESET_TARGET); 22189 } 22190 } 22191 if (reset_retval == 0) { 22192 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 22193 } 22194 mutex_enter(SD_MUTEX(un)); 22195 } 22196 break; 22197 } 22198 22199 /* A device/bus reset has occurred; update the reservation status. */ 22200 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 22201 (STAT_BUS_RESET | STAT_DEV_RESET))) { 22202 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22203 un->un_resvd_status |= 22204 (SD_LOST_RESERVE | SD_WANT_RESERVE); 22205 SD_INFO(SD_LOG_IOCTL_MHD, un, 22206 "sd_mhd_watch_incomplete: Lost Reservation\n"); 22207 } 22208 } 22209 22210 /* 22211 * The disk has been turned off; Update the device state. 22212 * 22213 * Note: Should we be offlining the disk here? 22214 */ 22215 if (pkt->pkt_state == STATE_GOT_BUS) { 22216 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 22217 "Disk not responding to selection\n"); 22218 if (un->un_state != SD_STATE_OFFLINE) { 22219 New_state(un, SD_STATE_OFFLINE); 22220 } 22221 } else if (be_chatty) { 22222 /* 22223 * suppress messages if they are all the same pkt reason; 22224 * with TQ, many (up to 256) are returned with the same 22225 * pkt_reason 22226 */ 22227 if (pkt->pkt_reason != un->un_last_pkt_reason) { 22228 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22229 "sd_mhd_watch_incomplete: " 22230 "SCSI transport failed: reason '%s'\n", 22231 scsi_rname(pkt->pkt_reason)); 22232 } 22233 } 22234 un->un_last_pkt_reason = pkt->pkt_reason; 22235 mutex_exit(SD_MUTEX(un)); 22236 } 22237 22238 22239 /* 22240 * Function: sd_sname() 22241 * 22242 * Description: This is a simple little routine to return a string containing 22243 * a printable description of command status byte for use in 22244 * logging. 22245 * 22246 * Arguments: status - pointer to a status byte 22247 * 22248 * Return Code: char * - string containing status description. 22249 */ 22250 22251 static char * 22252 sd_sname(uchar_t status) 22253 { 22254 switch (status & STATUS_MASK) { 22255 case STATUS_GOOD: 22256 return ("good status"); 22257 case STATUS_CHECK: 22258 return ("check condition"); 22259 case STATUS_MET: 22260 return ("condition met"); 22261 case STATUS_BUSY: 22262 return ("busy"); 22263 case STATUS_INTERMEDIATE: 22264 return ("intermediate"); 22265 case STATUS_INTERMEDIATE_MET: 22266 return ("intermediate - condition met"); 22267 case STATUS_RESERVATION_CONFLICT: 22268 return ("reservation_conflict"); 22269 case STATUS_TERMINATED: 22270 return ("command terminated"); 22271 case STATUS_QFULL: 22272 return ("queue full"); 22273 default: 22274 return ("<unknown status>"); 22275 } 22276 } 22277 22278 22279 /* 22280 * Function: sd_mhd_resvd_recover() 22281 * 22282 * Description: This function adds a reservation entry to the 22283 * sd_resv_reclaim_request list and signals the reservation 22284 * reclaim thread that there is work pending. If the reservation 22285 * reclaim thread has not been previously created this function 22286 * will kick it off. 22287 * 22288 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22289 * among multiple watches that share this callback function 22290 * 22291 * Context: This routine is called by timeout() and is run in interrupt 22292 * context. It must not sleep or call other functions which may 22293 * sleep. 22294 */ 22295 22296 static void 22297 sd_mhd_resvd_recover(void *arg) 22298 { 22299 dev_t dev = (dev_t)arg; 22300 struct sd_lun *un; 22301 struct sd_thr_request *sd_treq = NULL; 22302 struct sd_thr_request *sd_cur = NULL; 22303 struct sd_thr_request *sd_prev = NULL; 22304 int already_there = 0; 22305 22306 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22307 return; 22308 } 22309 22310 mutex_enter(SD_MUTEX(un)); 22311 un->un_resvd_timeid = NULL; 22312 if (un->un_resvd_status & SD_WANT_RESERVE) { 22313 /* 22314 * There was a reset so don't issue the reserve, allow the 22315 * sd_mhd_watch_cb callback function to notice this and 22316 * reschedule the timeout for reservation. 22317 */ 22318 mutex_exit(SD_MUTEX(un)); 22319 return; 22320 } 22321 mutex_exit(SD_MUTEX(un)); 22322 22323 /* 22324 * Add this device to the sd_resv_reclaim_request list and the 22325 * sd_resv_reclaim_thread should take care of the rest. 22326 * 22327 * Note: We can't sleep in this context so if the memory allocation 22328 * fails allow the sd_mhd_watch_cb callback function to notice this and 22329 * reschedule the timeout for reservation. (4378460) 22330 */ 22331 sd_treq = (struct sd_thr_request *) 22332 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 22333 if (sd_treq == NULL) { 22334 return; 22335 } 22336 22337 sd_treq->sd_thr_req_next = NULL; 22338 sd_treq->dev = dev; 22339 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22340 if (sd_tr.srq_thr_req_head == NULL) { 22341 sd_tr.srq_thr_req_head = sd_treq; 22342 } else { 22343 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 22344 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 22345 if (sd_cur->dev == dev) { 22346 /* 22347 * already in Queue so don't log 22348 * another request for the device 22349 */ 22350 already_there = 1; 22351 break; 22352 } 22353 sd_prev = sd_cur; 22354 } 22355 if (!already_there) { 22356 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 22357 "logging request for %lx\n", dev); 22358 sd_prev->sd_thr_req_next = sd_treq; 22359 } else { 22360 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 22361 } 22362 } 22363 22364 /* 22365 * Create a kernel thread to do the reservation reclaim and free up this 22366 * thread. We cannot block this thread while we go away to do the 22367 * reservation reclaim 22368 */ 22369 if (sd_tr.srq_resv_reclaim_thread == NULL) 22370 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 22371 sd_resv_reclaim_thread, NULL, 22372 0, &p0, TS_RUN, v.v_maxsyspri - 2); 22373 22374 /* Tell the reservation reclaim thread that it has work to do */ 22375 cv_signal(&sd_tr.srq_resv_reclaim_cv); 22376 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22377 } 22378 22379 /* 22380 * Function: sd_resv_reclaim_thread() 22381 * 22382 * Description: This function implements the reservation reclaim operations 22383 * 22384 * Arguments: arg - the device 'dev_t' is used for context to discriminate 22385 * among multiple watches that share this callback function 22386 */ 22387 22388 static void 22389 sd_resv_reclaim_thread() 22390 { 22391 struct sd_lun *un; 22392 struct sd_thr_request *sd_mhreq; 22393 22394 /* Wait for work */ 22395 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22396 if (sd_tr.srq_thr_req_head == NULL) { 22397 cv_wait(&sd_tr.srq_resv_reclaim_cv, 22398 &sd_tr.srq_resv_reclaim_mutex); 22399 } 22400 22401 /* Loop while we have work */ 22402 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 22403 un = ddi_get_soft_state(sd_state, 22404 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 22405 if (un == NULL) { 22406 /* 22407 * softstate structure is NULL so just 22408 * dequeue the request and continue 22409 */ 22410 sd_tr.srq_thr_req_head = 22411 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22412 kmem_free(sd_tr.srq_thr_cur_req, 22413 sizeof (struct sd_thr_request)); 22414 continue; 22415 } 22416 22417 /* dequeue the request */ 22418 sd_mhreq = sd_tr.srq_thr_cur_req; 22419 sd_tr.srq_thr_req_head = 22420 sd_tr.srq_thr_cur_req->sd_thr_req_next; 22421 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22422 22423 /* 22424 * Reclaim reservation only if SD_RESERVE is still set. There 22425 * may have been a call to MHIOCRELEASE before we got here. 22426 */ 22427 mutex_enter(SD_MUTEX(un)); 22428 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22429 /* 22430 * Note: The SD_LOST_RESERVE flag is cleared before 22431 * reclaiming the reservation. If this is done after the 22432 * call to sd_reserve_release a reservation loss in the 22433 * window between pkt completion of reserve cmd and 22434 * mutex_enter below may not be recognized 22435 */ 22436 un->un_resvd_status &= ~SD_LOST_RESERVE; 22437 mutex_exit(SD_MUTEX(un)); 22438 22439 if (sd_reserve_release(sd_mhreq->dev, 22440 SD_RESERVE) == 0) { 22441 mutex_enter(SD_MUTEX(un)); 22442 un->un_resvd_status |= SD_RESERVE; 22443 mutex_exit(SD_MUTEX(un)); 22444 SD_INFO(SD_LOG_IOCTL_MHD, un, 22445 "sd_resv_reclaim_thread: " 22446 "Reservation Recovered\n"); 22447 } else { 22448 mutex_enter(SD_MUTEX(un)); 22449 un->un_resvd_status |= SD_LOST_RESERVE; 22450 mutex_exit(SD_MUTEX(un)); 22451 SD_INFO(SD_LOG_IOCTL_MHD, un, 22452 "sd_resv_reclaim_thread: Failed " 22453 "Reservation Recovery\n"); 22454 } 22455 } else { 22456 mutex_exit(SD_MUTEX(un)); 22457 } 22458 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22459 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 22460 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22461 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 22462 /* 22463 * wakeup the destroy thread if anyone is waiting on 22464 * us to complete. 22465 */ 22466 cv_signal(&sd_tr.srq_inprocess_cv); 22467 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22468 "sd_resv_reclaim_thread: cv_signalling current request \n"); 22469 } 22470 22471 /* 22472 * cleanup the sd_tr structure now that this thread will not exist 22473 */ 22474 ASSERT(sd_tr.srq_thr_req_head == NULL); 22475 ASSERT(sd_tr.srq_thr_cur_req == NULL); 22476 sd_tr.srq_resv_reclaim_thread = NULL; 22477 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22478 thread_exit(); 22479 } 22480 22481 22482 /* 22483 * Function: sd_rmv_resv_reclaim_req() 22484 * 22485 * Description: This function removes any pending reservation reclaim requests 22486 * for the specified device. 22487 * 22488 * Arguments: dev - the device 'dev_t' 22489 */ 22490 22491 static void 22492 sd_rmv_resv_reclaim_req(dev_t dev) 22493 { 22494 struct sd_thr_request *sd_mhreq; 22495 struct sd_thr_request *sd_prev; 22496 22497 /* Remove a reservation reclaim request from the list */ 22498 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 22499 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 22500 /* 22501 * We are attempting to reinstate reservation for 22502 * this device. We wait for sd_reserve_release() 22503 * to return before we return. 22504 */ 22505 cv_wait(&sd_tr.srq_inprocess_cv, 22506 &sd_tr.srq_resv_reclaim_mutex); 22507 } else { 22508 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 22509 if (sd_mhreq && sd_mhreq->dev == dev) { 22510 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 22511 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22512 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22513 return; 22514 } 22515 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 22516 if (sd_mhreq && sd_mhreq->dev == dev) { 22517 break; 22518 } 22519 sd_prev = sd_mhreq; 22520 } 22521 if (sd_mhreq != NULL) { 22522 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 22523 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 22524 } 22525 } 22526 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 22527 } 22528 22529 22530 /* 22531 * Function: sd_mhd_reset_notify_cb() 22532 * 22533 * Description: This is a call back function for scsi_reset_notify. This 22534 * function updates the softstate reserved status and logs the 22535 * reset. The driver scsi watch facility callback function 22536 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 22537 * will reclaim the reservation. 22538 * 22539 * Arguments: arg - driver soft state (unit) structure 22540 */ 22541 22542 static void 22543 sd_mhd_reset_notify_cb(caddr_t arg) 22544 { 22545 struct sd_lun *un = (struct sd_lun *)arg; 22546 22547 mutex_enter(SD_MUTEX(un)); 22548 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 22549 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 22550 SD_INFO(SD_LOG_IOCTL_MHD, un, 22551 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 22552 } 22553 mutex_exit(SD_MUTEX(un)); 22554 } 22555 22556 22557 /* 22558 * Function: sd_take_ownership() 22559 * 22560 * Description: This routine implements an algorithm to achieve a stable 22561 * reservation on disks which don't implement priority reserve, 22562 * and makes sure that other host lose re-reservation attempts. 22563 * This algorithm contains of a loop that keeps issuing the RESERVE 22564 * for some period of time (min_ownership_delay, default 6 seconds) 22565 * During that loop, it looks to see if there has been a bus device 22566 * reset or bus reset (both of which cause an existing reservation 22567 * to be lost). If the reservation is lost issue RESERVE until a 22568 * period of min_ownership_delay with no resets has gone by, or 22569 * until max_ownership_delay has expired. This loop ensures that 22570 * the host really did manage to reserve the device, in spite of 22571 * resets. The looping for min_ownership_delay (default six 22572 * seconds) is important to early generation clustering products, 22573 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 22574 * MHIOCENFAILFAST periodic timer of two seconds. By having 22575 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 22576 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 22577 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 22578 * have already noticed, via the MHIOCENFAILFAST polling, that it 22579 * no longer "owns" the disk and will have panicked itself. Thus, 22580 * the host issuing the MHIOCTKOWN is assured (with timing 22581 * dependencies) that by the time it actually starts to use the 22582 * disk for real work, the old owner is no longer accessing it. 22583 * 22584 * min_ownership_delay is the minimum amount of time for which the 22585 * disk must be reserved continuously devoid of resets before the 22586 * MHIOCTKOWN ioctl will return success. 22587 * 22588 * max_ownership_delay indicates the amount of time by which the 22589 * take ownership should succeed or timeout with an error. 22590 * 22591 * Arguments: dev - the device 'dev_t' 22592 * *p - struct containing timing info. 22593 * 22594 * Return Code: 0 for success or error code 22595 */ 22596 22597 static int 22598 sd_take_ownership(dev_t dev, struct mhioctkown *p) 22599 { 22600 struct sd_lun *un; 22601 int rval; 22602 int err; 22603 int reservation_count = 0; 22604 int min_ownership_delay = 6000000; /* in usec */ 22605 int max_ownership_delay = 30000000; /* in usec */ 22606 clock_t start_time; /* starting time of this algorithm */ 22607 clock_t end_time; /* time limit for giving up */ 22608 clock_t ownership_time; /* time limit for stable ownership */ 22609 clock_t current_time; 22610 clock_t previous_current_time; 22611 22612 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22613 return (ENXIO); 22614 } 22615 22616 /* 22617 * Attempt a device reservation. A priority reservation is requested. 22618 */ 22619 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 22620 != SD_SUCCESS) { 22621 SD_ERROR(SD_LOG_IOCTL_MHD, un, 22622 "sd_take_ownership: return(1)=%d\n", rval); 22623 return (rval); 22624 } 22625 22626 /* Update the softstate reserved status to indicate the reservation */ 22627 mutex_enter(SD_MUTEX(un)); 22628 un->un_resvd_status |= SD_RESERVE; 22629 un->un_resvd_status &= 22630 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 22631 mutex_exit(SD_MUTEX(un)); 22632 22633 if (p != NULL) { 22634 if (p->min_ownership_delay != 0) { 22635 min_ownership_delay = p->min_ownership_delay * 1000; 22636 } 22637 if (p->max_ownership_delay != 0) { 22638 max_ownership_delay = p->max_ownership_delay * 1000; 22639 } 22640 } 22641 SD_INFO(SD_LOG_IOCTL_MHD, un, 22642 "sd_take_ownership: min, max delays: %d, %d\n", 22643 min_ownership_delay, max_ownership_delay); 22644 22645 start_time = ddi_get_lbolt(); 22646 current_time = start_time; 22647 ownership_time = current_time + drv_usectohz(min_ownership_delay); 22648 end_time = start_time + drv_usectohz(max_ownership_delay); 22649 22650 while (current_time - end_time < 0) { 22651 delay(drv_usectohz(500000)); 22652 22653 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 22654 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 22655 mutex_enter(SD_MUTEX(un)); 22656 rval = (un->un_resvd_status & 22657 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 22658 mutex_exit(SD_MUTEX(un)); 22659 break; 22660 } 22661 } 22662 previous_current_time = current_time; 22663 current_time = ddi_get_lbolt(); 22664 mutex_enter(SD_MUTEX(un)); 22665 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 22666 ownership_time = ddi_get_lbolt() + 22667 drv_usectohz(min_ownership_delay); 22668 reservation_count = 0; 22669 } else { 22670 reservation_count++; 22671 } 22672 un->un_resvd_status |= SD_RESERVE; 22673 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 22674 mutex_exit(SD_MUTEX(un)); 22675 22676 SD_INFO(SD_LOG_IOCTL_MHD, un, 22677 "sd_take_ownership: ticks for loop iteration=%ld, " 22678 "reservation=%s\n", (current_time - previous_current_time), 22679 reservation_count ? "ok" : "reclaimed"); 22680 22681 if (current_time - ownership_time >= 0 && 22682 reservation_count >= 4) { 22683 rval = 0; /* Achieved a stable ownership */ 22684 break; 22685 } 22686 if (current_time - end_time >= 0) { 22687 rval = EACCES; /* No ownership in max possible time */ 22688 break; 22689 } 22690 } 22691 SD_TRACE(SD_LOG_IOCTL_MHD, un, 22692 "sd_take_ownership: return(2)=%d\n", rval); 22693 return (rval); 22694 } 22695 22696 22697 /* 22698 * Function: sd_reserve_release() 22699 * 22700 * Description: This function builds and sends scsi RESERVE, RELEASE, and 22701 * PRIORITY RESERVE commands based on a user specified command type 22702 * 22703 * Arguments: dev - the device 'dev_t' 22704 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 22705 * SD_RESERVE, SD_RELEASE 22706 * 22707 * Return Code: 0 or Error Code 22708 */ 22709 22710 static int 22711 sd_reserve_release(dev_t dev, int cmd) 22712 { 22713 struct uscsi_cmd *com = NULL; 22714 struct sd_lun *un = NULL; 22715 char cdb[CDB_GROUP0]; 22716 int rval; 22717 22718 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 22719 (cmd == SD_PRIORITY_RESERVE)); 22720 22721 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22722 return (ENXIO); 22723 } 22724 22725 /* instantiate and initialize the command and cdb */ 22726 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 22727 bzero(cdb, CDB_GROUP0); 22728 com->uscsi_flags = USCSI_SILENT; 22729 com->uscsi_timeout = un->un_reserve_release_time; 22730 com->uscsi_cdblen = CDB_GROUP0; 22731 com->uscsi_cdb = cdb; 22732 if (cmd == SD_RELEASE) { 22733 cdb[0] = SCMD_RELEASE; 22734 } else { 22735 cdb[0] = SCMD_RESERVE; 22736 } 22737 22738 /* Send the command. */ 22739 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22740 SD_PATH_STANDARD); 22741 22742 /* 22743 * "break" a reservation that is held by another host, by issuing a 22744 * reset if priority reserve is desired, and we could not get the 22745 * device. 22746 */ 22747 if ((cmd == SD_PRIORITY_RESERVE) && 22748 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22749 /* 22750 * First try to reset the LUN. If we cannot, then try a target 22751 * reset, followed by a bus reset if the target reset fails. 22752 */ 22753 int reset_retval = 0; 22754 if (un->un_f_lun_reset_enabled == TRUE) { 22755 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 22756 } 22757 if (reset_retval == 0) { 22758 /* The LUN reset either failed or was not issued */ 22759 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 22760 } 22761 if ((reset_retval == 0) && 22762 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 22763 rval = EIO; 22764 kmem_free(com, sizeof (*com)); 22765 return (rval); 22766 } 22767 22768 bzero(com, sizeof (struct uscsi_cmd)); 22769 com->uscsi_flags = USCSI_SILENT; 22770 com->uscsi_cdb = cdb; 22771 com->uscsi_cdblen = CDB_GROUP0; 22772 com->uscsi_timeout = 5; 22773 22774 /* 22775 * Reissue the last reserve command, this time without request 22776 * sense. Assume that it is just a regular reserve command. 22777 */ 22778 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 22779 SD_PATH_STANDARD); 22780 } 22781 22782 /* Return an error if still getting a reservation conflict. */ 22783 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 22784 rval = EACCES; 22785 } 22786 22787 kmem_free(com, sizeof (*com)); 22788 return (rval); 22789 } 22790 22791 22792 #define SD_NDUMP_RETRIES 12 22793 /* 22794 * System Crash Dump routine 22795 */ 22796 22797 static int 22798 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 22799 { 22800 int instance; 22801 int partition; 22802 int i; 22803 int err; 22804 struct sd_lun *un; 22805 struct scsi_pkt *wr_pktp; 22806 struct buf *wr_bp; 22807 struct buf wr_buf; 22808 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 22809 daddr_t tgt_blkno; /* rmw - blkno for target */ 22810 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 22811 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 22812 size_t io_start_offset; 22813 int doing_rmw = FALSE; 22814 int rval; 22815 ssize_t dma_resid; 22816 daddr_t oblkno; 22817 diskaddr_t nblks = 0; 22818 diskaddr_t start_block; 22819 22820 instance = SDUNIT(dev); 22821 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 22822 !SD_IS_VALID_LABEL(un) || ISCD(un)) { 22823 return (ENXIO); 22824 } 22825 22826 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 22827 22828 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 22829 22830 partition = SDPART(dev); 22831 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 22832 22833 /* Validate blocks to dump at against partition size. */ 22834 22835 (void) cmlb_partinfo(un->un_cmlbhandle, partition, 22836 &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT); 22837 22838 if ((blkno + nblk) > nblks) { 22839 SD_TRACE(SD_LOG_DUMP, un, 22840 "sddump: dump range larger than partition: " 22841 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 22842 blkno, nblk, nblks); 22843 return (EINVAL); 22844 } 22845 22846 mutex_enter(&un->un_pm_mutex); 22847 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 22848 struct scsi_pkt *start_pktp; 22849 22850 mutex_exit(&un->un_pm_mutex); 22851 22852 /* 22853 * use pm framework to power on HBA 1st 22854 */ 22855 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 22856 22857 /* 22858 * Dump no long uses sdpower to power on a device, it's 22859 * in-line here so it can be done in polled mode. 22860 */ 22861 22862 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 22863 22864 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 22865 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 22866 22867 if (start_pktp == NULL) { 22868 /* We were not given a SCSI packet, fail. */ 22869 return (EIO); 22870 } 22871 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 22872 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 22873 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 22874 start_pktp->pkt_flags = FLAG_NOINTR; 22875 22876 mutex_enter(SD_MUTEX(un)); 22877 SD_FILL_SCSI1_LUN(un, start_pktp); 22878 mutex_exit(SD_MUTEX(un)); 22879 /* 22880 * Scsi_poll returns 0 (success) if the command completes and 22881 * the status block is STATUS_GOOD. 22882 */ 22883 if (sd_scsi_poll(un, start_pktp) != 0) { 22884 scsi_destroy_pkt(start_pktp); 22885 return (EIO); 22886 } 22887 scsi_destroy_pkt(start_pktp); 22888 (void) sd_ddi_pm_resume(un); 22889 } else { 22890 mutex_exit(&un->un_pm_mutex); 22891 } 22892 22893 mutex_enter(SD_MUTEX(un)); 22894 un->un_throttle = 0; 22895 22896 /* 22897 * The first time through, reset the specific target device. 22898 * However, when cpr calls sddump we know that sd is in a 22899 * a good state so no bus reset is required. 22900 * Clear sense data via Request Sense cmd. 22901 * In sddump we don't care about allow_bus_device_reset anymore 22902 */ 22903 22904 if ((un->un_state != SD_STATE_SUSPENDED) && 22905 (un->un_state != SD_STATE_DUMPING)) { 22906 22907 New_state(un, SD_STATE_DUMPING); 22908 22909 if (un->un_f_is_fibre == FALSE) { 22910 mutex_exit(SD_MUTEX(un)); 22911 /* 22912 * Attempt a bus reset for parallel scsi. 22913 * 22914 * Note: A bus reset is required because on some host 22915 * systems (i.e. E420R) a bus device reset is 22916 * insufficient to reset the state of the target. 22917 * 22918 * Note: Don't issue the reset for fibre-channel, 22919 * because this tends to hang the bus (loop) for 22920 * too long while everyone is logging out and in 22921 * and the deadman timer for dumping will fire 22922 * before the dump is complete. 22923 */ 22924 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 22925 mutex_enter(SD_MUTEX(un)); 22926 Restore_state(un); 22927 mutex_exit(SD_MUTEX(un)); 22928 return (EIO); 22929 } 22930 22931 /* Delay to give the device some recovery time. */ 22932 drv_usecwait(10000); 22933 22934 if (sd_send_polled_RQS(un) == SD_FAILURE) { 22935 SD_INFO(SD_LOG_DUMP, un, 22936 "sddump: sd_send_polled_RQS failed\n"); 22937 } 22938 mutex_enter(SD_MUTEX(un)); 22939 } 22940 } 22941 22942 /* 22943 * Convert the partition-relative block number to a 22944 * disk physical block number. 22945 */ 22946 blkno += start_block; 22947 22948 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 22949 22950 22951 /* 22952 * Check if the device has a non-512 block size. 22953 */ 22954 wr_bp = NULL; 22955 if (NOT_DEVBSIZE(un)) { 22956 tgt_byte_offset = blkno * un->un_sys_blocksize; 22957 tgt_byte_count = nblk * un->un_sys_blocksize; 22958 if ((tgt_byte_offset % un->un_tgt_blocksize) || 22959 (tgt_byte_count % un->un_tgt_blocksize)) { 22960 doing_rmw = TRUE; 22961 /* 22962 * Calculate the block number and number of block 22963 * in terms of the media block size. 22964 */ 22965 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 22966 tgt_nblk = 22967 ((tgt_byte_offset + tgt_byte_count + 22968 (un->un_tgt_blocksize - 1)) / 22969 un->un_tgt_blocksize) - tgt_blkno; 22970 22971 /* 22972 * Invoke the routine which is going to do read part 22973 * of read-modify-write. 22974 * Note that this routine returns a pointer to 22975 * a valid bp in wr_bp. 22976 */ 22977 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 22978 &wr_bp); 22979 if (err) { 22980 mutex_exit(SD_MUTEX(un)); 22981 return (err); 22982 } 22983 /* 22984 * Offset is being calculated as - 22985 * (original block # * system block size) - 22986 * (new block # * target block size) 22987 */ 22988 io_start_offset = 22989 ((uint64_t)(blkno * un->un_sys_blocksize)) - 22990 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 22991 22992 ASSERT((io_start_offset >= 0) && 22993 (io_start_offset < un->un_tgt_blocksize)); 22994 /* 22995 * Do the modify portion of read modify write. 22996 */ 22997 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 22998 (size_t)nblk * un->un_sys_blocksize); 22999 } else { 23000 doing_rmw = FALSE; 23001 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 23002 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 23003 } 23004 23005 /* Convert blkno and nblk to target blocks */ 23006 blkno = tgt_blkno; 23007 nblk = tgt_nblk; 23008 } else { 23009 wr_bp = &wr_buf; 23010 bzero(wr_bp, sizeof (struct buf)); 23011 wr_bp->b_flags = B_BUSY; 23012 wr_bp->b_un.b_addr = addr; 23013 wr_bp->b_bcount = nblk << DEV_BSHIFT; 23014 wr_bp->b_resid = 0; 23015 } 23016 23017 mutex_exit(SD_MUTEX(un)); 23018 23019 /* 23020 * Obtain a SCSI packet for the write command. 23021 * It should be safe to call the allocator here without 23022 * worrying about being locked for DVMA mapping because 23023 * the address we're passed is already a DVMA mapping 23024 * 23025 * We are also not going to worry about semaphore ownership 23026 * in the dump buffer. Dumping is single threaded at present. 23027 */ 23028 23029 wr_pktp = NULL; 23030 23031 dma_resid = wr_bp->b_bcount; 23032 oblkno = blkno; 23033 23034 while (dma_resid != 0) { 23035 23036 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 23037 wr_bp->b_flags &= ~B_ERROR; 23038 23039 if (un->un_partial_dma_supported == 1) { 23040 blkno = oblkno + 23041 ((wr_bp->b_bcount - dma_resid) / 23042 un->un_tgt_blocksize); 23043 nblk = dma_resid / un->un_tgt_blocksize; 23044 23045 if (wr_pktp) { 23046 /* 23047 * Partial DMA transfers after initial transfer 23048 */ 23049 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 23050 blkno, nblk); 23051 } else { 23052 /* Initial transfer */ 23053 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 23054 un->un_pkt_flags, NULL_FUNC, NULL, 23055 blkno, nblk); 23056 } 23057 } else { 23058 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 23059 0, NULL_FUNC, NULL, blkno, nblk); 23060 } 23061 23062 if (rval == 0) { 23063 /* We were given a SCSI packet, continue. */ 23064 break; 23065 } 23066 23067 if (i == 0) { 23068 if (wr_bp->b_flags & B_ERROR) { 23069 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23070 "no resources for dumping; " 23071 "error code: 0x%x, retrying", 23072 geterror(wr_bp)); 23073 } else { 23074 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23075 "no resources for dumping; retrying"); 23076 } 23077 } else if (i != (SD_NDUMP_RETRIES - 1)) { 23078 if (wr_bp->b_flags & B_ERROR) { 23079 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23080 "no resources for dumping; error code: " 23081 "0x%x, retrying\n", geterror(wr_bp)); 23082 } 23083 } else { 23084 if (wr_bp->b_flags & B_ERROR) { 23085 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23086 "no resources for dumping; " 23087 "error code: 0x%x, retries failed, " 23088 "giving up.\n", geterror(wr_bp)); 23089 } else { 23090 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23091 "no resources for dumping; " 23092 "retries failed, giving up.\n"); 23093 } 23094 mutex_enter(SD_MUTEX(un)); 23095 Restore_state(un); 23096 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 23097 mutex_exit(SD_MUTEX(un)); 23098 scsi_free_consistent_buf(wr_bp); 23099 } else { 23100 mutex_exit(SD_MUTEX(un)); 23101 } 23102 return (EIO); 23103 } 23104 drv_usecwait(10000); 23105 } 23106 23107 if (un->un_partial_dma_supported == 1) { 23108 /* 23109 * save the resid from PARTIAL_DMA 23110 */ 23111 dma_resid = wr_pktp->pkt_resid; 23112 if (dma_resid != 0) 23113 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 23114 wr_pktp->pkt_resid = 0; 23115 } else { 23116 dma_resid = 0; 23117 } 23118 23119 /* SunBug 1222170 */ 23120 wr_pktp->pkt_flags = FLAG_NOINTR; 23121 23122 err = EIO; 23123 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 23124 23125 /* 23126 * Scsi_poll returns 0 (success) if the command completes and 23127 * the status block is STATUS_GOOD. We should only check 23128 * errors if this condition is not true. Even then we should 23129 * send our own request sense packet only if we have a check 23130 * condition and auto request sense has not been performed by 23131 * the hba. 23132 */ 23133 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 23134 23135 if ((sd_scsi_poll(un, wr_pktp) == 0) && 23136 (wr_pktp->pkt_resid == 0)) { 23137 err = SD_SUCCESS; 23138 break; 23139 } 23140 23141 /* 23142 * Check CMD_DEV_GONE 1st, give up if device is gone. 23143 */ 23144 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 23145 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 23146 "Device is gone\n"); 23147 break; 23148 } 23149 23150 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 23151 SD_INFO(SD_LOG_DUMP, un, 23152 "sddump: write failed with CHECK, try # %d\n", i); 23153 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 23154 (void) sd_send_polled_RQS(un); 23155 } 23156 23157 continue; 23158 } 23159 23160 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 23161 int reset_retval = 0; 23162 23163 SD_INFO(SD_LOG_DUMP, un, 23164 "sddump: write failed with BUSY, try # %d\n", i); 23165 23166 if (un->un_f_lun_reset_enabled == TRUE) { 23167 reset_retval = scsi_reset(SD_ADDRESS(un), 23168 RESET_LUN); 23169 } 23170 if (reset_retval == 0) { 23171 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 23172 } 23173 (void) sd_send_polled_RQS(un); 23174 23175 } else { 23176 SD_INFO(SD_LOG_DUMP, un, 23177 "sddump: write failed with 0x%x, try # %d\n", 23178 SD_GET_PKT_STATUS(wr_pktp), i); 23179 mutex_enter(SD_MUTEX(un)); 23180 sd_reset_target(un, wr_pktp); 23181 mutex_exit(SD_MUTEX(un)); 23182 } 23183 23184 /* 23185 * If we are not getting anywhere with lun/target resets, 23186 * let's reset the bus. 23187 */ 23188 if (i == SD_NDUMP_RETRIES/2) { 23189 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 23190 (void) sd_send_polled_RQS(un); 23191 } 23192 } 23193 } 23194 23195 scsi_destroy_pkt(wr_pktp); 23196 mutex_enter(SD_MUTEX(un)); 23197 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 23198 mutex_exit(SD_MUTEX(un)); 23199 scsi_free_consistent_buf(wr_bp); 23200 } else { 23201 mutex_exit(SD_MUTEX(un)); 23202 } 23203 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 23204 return (err); 23205 } 23206 23207 /* 23208 * Function: sd_scsi_poll() 23209 * 23210 * Description: This is a wrapper for the scsi_poll call. 23211 * 23212 * Arguments: sd_lun - The unit structure 23213 * scsi_pkt - The scsi packet being sent to the device. 23214 * 23215 * Return Code: 0 - Command completed successfully with good status 23216 * -1 - Command failed. This could indicate a check condition 23217 * or other status value requiring recovery action. 23218 * 23219 */ 23220 23221 static int 23222 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 23223 { 23224 int status; 23225 23226 ASSERT(un != NULL); 23227 ASSERT(!mutex_owned(SD_MUTEX(un))); 23228 ASSERT(pktp != NULL); 23229 23230 status = SD_SUCCESS; 23231 23232 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 23233 pktp->pkt_flags |= un->un_tagflags; 23234 pktp->pkt_flags &= ~FLAG_NODISCON; 23235 } 23236 23237 status = sd_ddi_scsi_poll(pktp); 23238 /* 23239 * Scsi_poll returns 0 (success) if the command completes and the 23240 * status block is STATUS_GOOD. We should only check errors if this 23241 * condition is not true. Even then we should send our own request 23242 * sense packet only if we have a check condition and auto 23243 * request sense has not been performed by the hba. 23244 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 23245 */ 23246 if ((status != SD_SUCCESS) && 23247 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 23248 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 23249 (pktp->pkt_reason != CMD_DEV_GONE)) 23250 (void) sd_send_polled_RQS(un); 23251 23252 return (status); 23253 } 23254 23255 /* 23256 * Function: sd_send_polled_RQS() 23257 * 23258 * Description: This sends the request sense command to a device. 23259 * 23260 * Arguments: sd_lun - The unit structure 23261 * 23262 * Return Code: 0 - Command completed successfully with good status 23263 * -1 - Command failed. 23264 * 23265 */ 23266 23267 static int 23268 sd_send_polled_RQS(struct sd_lun *un) 23269 { 23270 int ret_val; 23271 struct scsi_pkt *rqs_pktp; 23272 struct buf *rqs_bp; 23273 23274 ASSERT(un != NULL); 23275 ASSERT(!mutex_owned(SD_MUTEX(un))); 23276 23277 ret_val = SD_SUCCESS; 23278 23279 rqs_pktp = un->un_rqs_pktp; 23280 rqs_bp = un->un_rqs_bp; 23281 23282 mutex_enter(SD_MUTEX(un)); 23283 23284 if (un->un_sense_isbusy) { 23285 ret_val = SD_FAILURE; 23286 mutex_exit(SD_MUTEX(un)); 23287 return (ret_val); 23288 } 23289 23290 /* 23291 * If the request sense buffer (and packet) is not in use, 23292 * let's set the un_sense_isbusy and send our packet 23293 */ 23294 un->un_sense_isbusy = 1; 23295 rqs_pktp->pkt_resid = 0; 23296 rqs_pktp->pkt_reason = 0; 23297 rqs_pktp->pkt_flags |= FLAG_NOINTR; 23298 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 23299 23300 mutex_exit(SD_MUTEX(un)); 23301 23302 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 23303 " 0x%p\n", rqs_bp->b_un.b_addr); 23304 23305 /* 23306 * Can't send this to sd_scsi_poll, we wrap ourselves around the 23307 * axle - it has a call into us! 23308 */ 23309 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 23310 SD_INFO(SD_LOG_COMMON, un, 23311 "sd_send_polled_RQS: RQS failed\n"); 23312 } 23313 23314 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 23315 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 23316 23317 mutex_enter(SD_MUTEX(un)); 23318 un->un_sense_isbusy = 0; 23319 mutex_exit(SD_MUTEX(un)); 23320 23321 return (ret_val); 23322 } 23323 23324 /* 23325 * Defines needed for localized version of the scsi_poll routine. 23326 */ 23327 #define SD_CSEC 10000 /* usecs */ 23328 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 23329 23330 23331 /* 23332 * Function: sd_ddi_scsi_poll() 23333 * 23334 * Description: Localized version of the scsi_poll routine. The purpose is to 23335 * send a scsi_pkt to a device as a polled command. This version 23336 * is to ensure more robust handling of transport errors. 23337 * Specifically this routine cures not ready, coming ready 23338 * transition for power up and reset of sonoma's. This can take 23339 * up to 45 seconds for power-on and 20 seconds for reset of a 23340 * sonoma lun. 23341 * 23342 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 23343 * 23344 * Return Code: 0 - Command completed successfully with good status 23345 * -1 - Command failed. 23346 * 23347 */ 23348 23349 static int 23350 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 23351 { 23352 int busy_count; 23353 int timeout; 23354 int rval = SD_FAILURE; 23355 int savef; 23356 uint8_t *sensep; 23357 long savet; 23358 void (*savec)(); 23359 /* 23360 * The following is defined in machdep.c and is used in determining if 23361 * the scsi transport system will do polled I/O instead of interrupt 23362 * I/O when called from xx_dump(). 23363 */ 23364 extern int do_polled_io; 23365 23366 /* 23367 * save old flags in pkt, to restore at end 23368 */ 23369 savef = pkt->pkt_flags; 23370 savec = pkt->pkt_comp; 23371 savet = pkt->pkt_time; 23372 23373 pkt->pkt_flags |= FLAG_NOINTR; 23374 23375 /* 23376 * XXX there is nothing in the SCSA spec that states that we should not 23377 * do a callback for polled cmds; however, removing this will break sd 23378 * and probably other target drivers 23379 */ 23380 pkt->pkt_comp = NULL; 23381 23382 /* 23383 * we don't like a polled command without timeout. 23384 * 60 seconds seems long enough. 23385 */ 23386 if (pkt->pkt_time == 0) { 23387 pkt->pkt_time = SCSI_POLL_TIMEOUT; 23388 } 23389 23390 /* 23391 * Send polled cmd. 23392 * 23393 * We do some error recovery for various errors. Tran_busy, 23394 * queue full, and non-dispatched commands are retried every 10 msec. 23395 * as they are typically transient failures. Busy status and Not 23396 * Ready are retried every second as this status takes a while to 23397 * change. Unit attention is retried for pkt_time (60) times 23398 * with no delay. 23399 */ 23400 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 23401 23402 for (busy_count = 0; busy_count < timeout; busy_count++) { 23403 int rc; 23404 int poll_delay; 23405 23406 /* 23407 * Initialize pkt status variables. 23408 */ 23409 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 23410 23411 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 23412 if (rc != TRAN_BUSY) { 23413 /* Transport failed - give up. */ 23414 break; 23415 } else { 23416 /* Transport busy - try again. */ 23417 poll_delay = 1 * SD_CSEC; /* 10 msec */ 23418 } 23419 } else { 23420 /* 23421 * Transport accepted - check pkt status. 23422 */ 23423 rc = (*pkt->pkt_scbp) & STATUS_MASK; 23424 if (pkt->pkt_reason == CMD_CMPLT && 23425 rc == STATUS_CHECK && 23426 pkt->pkt_state & STATE_ARQ_DONE) { 23427 struct scsi_arq_status *arqstat = 23428 (struct scsi_arq_status *)(pkt->pkt_scbp); 23429 23430 sensep = (uint8_t *)&arqstat->sts_sensedata; 23431 } else { 23432 sensep = NULL; 23433 } 23434 23435 if ((pkt->pkt_reason == CMD_CMPLT) && 23436 (rc == STATUS_GOOD)) { 23437 /* No error - we're done */ 23438 rval = SD_SUCCESS; 23439 break; 23440 23441 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 23442 /* Lost connection - give up */ 23443 break; 23444 23445 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 23446 (pkt->pkt_state == 0)) { 23447 /* Pkt not dispatched - try again. */ 23448 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 23449 23450 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23451 (rc == STATUS_QFULL)) { 23452 /* Queue full - try again. */ 23453 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 23454 23455 } else if ((pkt->pkt_reason == CMD_CMPLT) && 23456 (rc == STATUS_BUSY)) { 23457 /* Busy - try again. */ 23458 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 23459 busy_count += (SD_SEC_TO_CSEC - 1); 23460 23461 } else if ((sensep != NULL) && 23462 (scsi_sense_key(sensep) == 23463 KEY_UNIT_ATTENTION)) { 23464 /* Unit Attention - try again */ 23465 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 23466 continue; 23467 23468 } else if ((sensep != NULL) && 23469 (scsi_sense_key(sensep) == KEY_NOT_READY) && 23470 (scsi_sense_asc(sensep) == 0x04) && 23471 (scsi_sense_ascq(sensep) == 0x01)) { 23472 /* Not ready -> ready - try again. */ 23473 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 23474 busy_count += (SD_SEC_TO_CSEC - 1); 23475 23476 } else { 23477 /* BAD status - give up. */ 23478 break; 23479 } 23480 } 23481 23482 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 23483 !do_polled_io) { 23484 delay(drv_usectohz(poll_delay)); 23485 } else { 23486 /* we busy wait during cpr_dump or interrupt threads */ 23487 drv_usecwait(poll_delay); 23488 } 23489 } 23490 23491 pkt->pkt_flags = savef; 23492 pkt->pkt_comp = savec; 23493 pkt->pkt_time = savet; 23494 return (rval); 23495 } 23496 23497 23498 /* 23499 * Function: sd_persistent_reservation_in_read_keys 23500 * 23501 * Description: This routine is the driver entry point for handling CD-ROM 23502 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 23503 * by sending the SCSI-3 PRIN commands to the device. 23504 * Processes the read keys command response by copying the 23505 * reservation key information into the user provided buffer. 23506 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 23507 * 23508 * Arguments: un - Pointer to soft state struct for the target. 23509 * usrp - user provided pointer to multihost Persistent In Read 23510 * Keys structure (mhioc_inkeys_t) 23511 * flag - this argument is a pass through to ddi_copyxxx() 23512 * directly from the mode argument of ioctl(). 23513 * 23514 * Return Code: 0 - Success 23515 * EACCES 23516 * ENOTSUP 23517 * errno return code from sd_send_scsi_cmd() 23518 * 23519 * Context: Can sleep. Does not return until command is completed. 23520 */ 23521 23522 static int 23523 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 23524 mhioc_inkeys_t *usrp, int flag) 23525 { 23526 #ifdef _MULTI_DATAMODEL 23527 struct mhioc_key_list32 li32; 23528 #endif 23529 sd_prin_readkeys_t *in; 23530 mhioc_inkeys_t *ptr; 23531 mhioc_key_list_t li; 23532 uchar_t *data_bufp; 23533 int data_len; 23534 int rval; 23535 size_t copysz; 23536 23537 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 23538 return (EINVAL); 23539 } 23540 bzero(&li, sizeof (mhioc_key_list_t)); 23541 23542 /* 23543 * Get the listsize from user 23544 */ 23545 #ifdef _MULTI_DATAMODEL 23546 23547 switch (ddi_model_convert_from(flag & FMODELS)) { 23548 case DDI_MODEL_ILP32: 23549 copysz = sizeof (struct mhioc_key_list32); 23550 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 23551 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23552 "sd_persistent_reservation_in_read_keys: " 23553 "failed ddi_copyin: mhioc_key_list32_t\n"); 23554 rval = EFAULT; 23555 goto done; 23556 } 23557 li.listsize = li32.listsize; 23558 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 23559 break; 23560 23561 case DDI_MODEL_NONE: 23562 copysz = sizeof (mhioc_key_list_t); 23563 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23564 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23565 "sd_persistent_reservation_in_read_keys: " 23566 "failed ddi_copyin: mhioc_key_list_t\n"); 23567 rval = EFAULT; 23568 goto done; 23569 } 23570 break; 23571 } 23572 23573 #else /* ! _MULTI_DATAMODEL */ 23574 copysz = sizeof (mhioc_key_list_t); 23575 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 23576 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23577 "sd_persistent_reservation_in_read_keys: " 23578 "failed ddi_copyin: mhioc_key_list_t\n"); 23579 rval = EFAULT; 23580 goto done; 23581 } 23582 #endif 23583 23584 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 23585 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 23586 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23587 23588 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 23589 data_len, data_bufp)) != 0) { 23590 goto done; 23591 } 23592 in = (sd_prin_readkeys_t *)data_bufp; 23593 ptr->generation = BE_32(in->generation); 23594 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 23595 23596 /* 23597 * Return the min(listsize, listlen) keys 23598 */ 23599 #ifdef _MULTI_DATAMODEL 23600 23601 switch (ddi_model_convert_from(flag & FMODELS)) { 23602 case DDI_MODEL_ILP32: 23603 li32.listlen = li.listlen; 23604 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 23605 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23606 "sd_persistent_reservation_in_read_keys: " 23607 "failed ddi_copyout: mhioc_key_list32_t\n"); 23608 rval = EFAULT; 23609 goto done; 23610 } 23611 break; 23612 23613 case DDI_MODEL_NONE: 23614 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23615 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23616 "sd_persistent_reservation_in_read_keys: " 23617 "failed ddi_copyout: mhioc_key_list_t\n"); 23618 rval = EFAULT; 23619 goto done; 23620 } 23621 break; 23622 } 23623 23624 #else /* ! _MULTI_DATAMODEL */ 23625 23626 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 23627 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23628 "sd_persistent_reservation_in_read_keys: " 23629 "failed ddi_copyout: mhioc_key_list_t\n"); 23630 rval = EFAULT; 23631 goto done; 23632 } 23633 23634 #endif /* _MULTI_DATAMODEL */ 23635 23636 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 23637 li.listsize * MHIOC_RESV_KEY_SIZE); 23638 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 23639 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23640 "sd_persistent_reservation_in_read_keys: " 23641 "failed ddi_copyout: keylist\n"); 23642 rval = EFAULT; 23643 } 23644 done: 23645 kmem_free(data_bufp, data_len); 23646 return (rval); 23647 } 23648 23649 23650 /* 23651 * Function: sd_persistent_reservation_in_read_resv 23652 * 23653 * Description: This routine is the driver entry point for handling CD-ROM 23654 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 23655 * by sending the SCSI-3 PRIN commands to the device. 23656 * Process the read persistent reservations command response by 23657 * copying the reservation information into the user provided 23658 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 23659 * 23660 * Arguments: un - Pointer to soft state struct for the target. 23661 * usrp - user provided pointer to multihost Persistent In Read 23662 * Keys structure (mhioc_inkeys_t) 23663 * flag - this argument is a pass through to ddi_copyxxx() 23664 * directly from the mode argument of ioctl(). 23665 * 23666 * Return Code: 0 - Success 23667 * EACCES 23668 * ENOTSUP 23669 * errno return code from sd_send_scsi_cmd() 23670 * 23671 * Context: Can sleep. Does not return until command is completed. 23672 */ 23673 23674 static int 23675 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 23676 mhioc_inresvs_t *usrp, int flag) 23677 { 23678 #ifdef _MULTI_DATAMODEL 23679 struct mhioc_resv_desc_list32 resvlist32; 23680 #endif 23681 sd_prin_readresv_t *in; 23682 mhioc_inresvs_t *ptr; 23683 sd_readresv_desc_t *readresv_ptr; 23684 mhioc_resv_desc_list_t resvlist; 23685 mhioc_resv_desc_t resvdesc; 23686 uchar_t *data_bufp; 23687 int data_len; 23688 int rval; 23689 int i; 23690 size_t copysz; 23691 mhioc_resv_desc_t *bufp; 23692 23693 if ((ptr = usrp) == NULL) { 23694 return (EINVAL); 23695 } 23696 23697 /* 23698 * Get the listsize from user 23699 */ 23700 #ifdef _MULTI_DATAMODEL 23701 switch (ddi_model_convert_from(flag & FMODELS)) { 23702 case DDI_MODEL_ILP32: 23703 copysz = sizeof (struct mhioc_resv_desc_list32); 23704 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 23705 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23706 "sd_persistent_reservation_in_read_resv: " 23707 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23708 rval = EFAULT; 23709 goto done; 23710 } 23711 resvlist.listsize = resvlist32.listsize; 23712 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 23713 break; 23714 23715 case DDI_MODEL_NONE: 23716 copysz = sizeof (mhioc_resv_desc_list_t); 23717 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23718 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23719 "sd_persistent_reservation_in_read_resv: " 23720 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23721 rval = EFAULT; 23722 goto done; 23723 } 23724 break; 23725 } 23726 #else /* ! _MULTI_DATAMODEL */ 23727 copysz = sizeof (mhioc_resv_desc_list_t); 23728 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 23729 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23730 "sd_persistent_reservation_in_read_resv: " 23731 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 23732 rval = EFAULT; 23733 goto done; 23734 } 23735 #endif /* ! _MULTI_DATAMODEL */ 23736 23737 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 23738 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 23739 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 23740 23741 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 23742 data_len, data_bufp)) != 0) { 23743 goto done; 23744 } 23745 in = (sd_prin_readresv_t *)data_bufp; 23746 ptr->generation = BE_32(in->generation); 23747 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 23748 23749 /* 23750 * Return the min(listsize, listlen( keys 23751 */ 23752 #ifdef _MULTI_DATAMODEL 23753 23754 switch (ddi_model_convert_from(flag & FMODELS)) { 23755 case DDI_MODEL_ILP32: 23756 resvlist32.listlen = resvlist.listlen; 23757 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 23758 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23759 "sd_persistent_reservation_in_read_resv: " 23760 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23761 rval = EFAULT; 23762 goto done; 23763 } 23764 break; 23765 23766 case DDI_MODEL_NONE: 23767 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23768 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23769 "sd_persistent_reservation_in_read_resv: " 23770 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23771 rval = EFAULT; 23772 goto done; 23773 } 23774 break; 23775 } 23776 23777 #else /* ! _MULTI_DATAMODEL */ 23778 23779 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 23780 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23781 "sd_persistent_reservation_in_read_resv: " 23782 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 23783 rval = EFAULT; 23784 goto done; 23785 } 23786 23787 #endif /* ! _MULTI_DATAMODEL */ 23788 23789 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 23790 bufp = resvlist.list; 23791 copysz = sizeof (mhioc_resv_desc_t); 23792 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 23793 i++, readresv_ptr++, bufp++) { 23794 23795 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 23796 MHIOC_RESV_KEY_SIZE); 23797 resvdesc.type = readresv_ptr->type; 23798 resvdesc.scope = readresv_ptr->scope; 23799 resvdesc.scope_specific_addr = 23800 BE_32(readresv_ptr->scope_specific_addr); 23801 23802 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 23803 SD_ERROR(SD_LOG_IOCTL_MHD, un, 23804 "sd_persistent_reservation_in_read_resv: " 23805 "failed ddi_copyout: resvlist\n"); 23806 rval = EFAULT; 23807 goto done; 23808 } 23809 } 23810 done: 23811 kmem_free(data_bufp, data_len); 23812 return (rval); 23813 } 23814 23815 23816 /* 23817 * Function: sr_change_blkmode() 23818 * 23819 * Description: This routine is the driver entry point for handling CD-ROM 23820 * block mode ioctl requests. Support for returning and changing 23821 * the current block size in use by the device is implemented. The 23822 * LBA size is changed via a MODE SELECT Block Descriptor. 23823 * 23824 * This routine issues a mode sense with an allocation length of 23825 * 12 bytes for the mode page header and a single block descriptor. 23826 * 23827 * Arguments: dev - the device 'dev_t' 23828 * cmd - the request type; one of CDROMGBLKMODE (get) or 23829 * CDROMSBLKMODE (set) 23830 * data - current block size or requested block size 23831 * flag - this argument is a pass through to ddi_copyxxx() directly 23832 * from the mode argument of ioctl(). 23833 * 23834 * Return Code: the code returned by sd_send_scsi_cmd() 23835 * EINVAL if invalid arguments are provided 23836 * EFAULT if ddi_copyxxx() fails 23837 * ENXIO if fail ddi_get_soft_state 23838 * EIO if invalid mode sense block descriptor length 23839 * 23840 */ 23841 23842 static int 23843 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 23844 { 23845 struct sd_lun *un = NULL; 23846 struct mode_header *sense_mhp, *select_mhp; 23847 struct block_descriptor *sense_desc, *select_desc; 23848 int current_bsize; 23849 int rval = EINVAL; 23850 uchar_t *sense = NULL; 23851 uchar_t *select = NULL; 23852 23853 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 23854 23855 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23856 return (ENXIO); 23857 } 23858 23859 /* 23860 * The block length is changed via the Mode Select block descriptor, the 23861 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 23862 * required as part of this routine. Therefore the mode sense allocation 23863 * length is specified to be the length of a mode page header and a 23864 * block descriptor. 23865 */ 23866 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 23867 23868 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 23869 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 23870 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23871 "sr_change_blkmode: Mode Sense Failed\n"); 23872 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23873 return (rval); 23874 } 23875 23876 /* Check the block descriptor len to handle only 1 block descriptor */ 23877 sense_mhp = (struct mode_header *)sense; 23878 if ((sense_mhp->bdesc_length == 0) || 23879 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 23880 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23881 "sr_change_blkmode: Mode Sense returned invalid block" 23882 " descriptor length\n"); 23883 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23884 return (EIO); 23885 } 23886 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 23887 current_bsize = ((sense_desc->blksize_hi << 16) | 23888 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 23889 23890 /* Process command */ 23891 switch (cmd) { 23892 case CDROMGBLKMODE: 23893 /* Return the block size obtained during the mode sense */ 23894 if (ddi_copyout(¤t_bsize, (void *)data, 23895 sizeof (int), flag) != 0) 23896 rval = EFAULT; 23897 break; 23898 case CDROMSBLKMODE: 23899 /* Validate the requested block size */ 23900 switch (data) { 23901 case CDROM_BLK_512: 23902 case CDROM_BLK_1024: 23903 case CDROM_BLK_2048: 23904 case CDROM_BLK_2056: 23905 case CDROM_BLK_2336: 23906 case CDROM_BLK_2340: 23907 case CDROM_BLK_2352: 23908 case CDROM_BLK_2368: 23909 case CDROM_BLK_2448: 23910 case CDROM_BLK_2646: 23911 case CDROM_BLK_2647: 23912 break; 23913 default: 23914 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23915 "sr_change_blkmode: " 23916 "Block Size '%ld' Not Supported\n", data); 23917 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23918 return (EINVAL); 23919 } 23920 23921 /* 23922 * The current block size matches the requested block size so 23923 * there is no need to send the mode select to change the size 23924 */ 23925 if (current_bsize == data) { 23926 break; 23927 } 23928 23929 /* Build the select data for the requested block size */ 23930 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 23931 select_mhp = (struct mode_header *)select; 23932 select_desc = 23933 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 23934 /* 23935 * The LBA size is changed via the block descriptor, so the 23936 * descriptor is built according to the user data 23937 */ 23938 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 23939 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 23940 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 23941 select_desc->blksize_lo = (char)((data) & 0x000000ff); 23942 23943 /* Send the mode select for the requested block size */ 23944 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 23945 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 23946 SD_PATH_STANDARD)) != 0) { 23947 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23948 "sr_change_blkmode: Mode Select Failed\n"); 23949 /* 23950 * The mode select failed for the requested block size, 23951 * so reset the data for the original block size and 23952 * send it to the target. The error is indicated by the 23953 * return value for the failed mode select. 23954 */ 23955 select_desc->blksize_hi = sense_desc->blksize_hi; 23956 select_desc->blksize_mid = sense_desc->blksize_mid; 23957 select_desc->blksize_lo = sense_desc->blksize_lo; 23958 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 23959 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 23960 SD_PATH_STANDARD); 23961 } else { 23962 ASSERT(!mutex_owned(SD_MUTEX(un))); 23963 mutex_enter(SD_MUTEX(un)); 23964 sd_update_block_info(un, (uint32_t)data, 0); 23965 mutex_exit(SD_MUTEX(un)); 23966 } 23967 break; 23968 default: 23969 /* should not reach here, but check anyway */ 23970 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 23971 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 23972 rval = EINVAL; 23973 break; 23974 } 23975 23976 if (select) { 23977 kmem_free(select, BUFLEN_CHG_BLK_MODE); 23978 } 23979 if (sense) { 23980 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 23981 } 23982 return (rval); 23983 } 23984 23985 23986 /* 23987 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 23988 * implement driver support for getting and setting the CD speed. The command 23989 * set used will be based on the device type. If the device has not been 23990 * identified as MMC the Toshiba vendor specific mode page will be used. If 23991 * the device is MMC but does not support the Real Time Streaming feature 23992 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 23993 * be used to read the speed. 23994 */ 23995 23996 /* 23997 * Function: sr_change_speed() 23998 * 23999 * Description: This routine is the driver entry point for handling CD-ROM 24000 * drive speed ioctl requests for devices supporting the Toshiba 24001 * vendor specific drive speed mode page. Support for returning 24002 * and changing the current drive speed in use by the device is 24003 * implemented. 24004 * 24005 * Arguments: dev - the device 'dev_t' 24006 * cmd - the request type; one of CDROMGDRVSPEED (get) or 24007 * CDROMSDRVSPEED (set) 24008 * data - current drive speed or requested drive speed 24009 * flag - this argument is a pass through to ddi_copyxxx() directly 24010 * from the mode argument of ioctl(). 24011 * 24012 * Return Code: the code returned by sd_send_scsi_cmd() 24013 * EINVAL if invalid arguments are provided 24014 * EFAULT if ddi_copyxxx() fails 24015 * ENXIO if fail ddi_get_soft_state 24016 * EIO if invalid mode sense block descriptor length 24017 */ 24018 24019 static int 24020 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 24021 { 24022 struct sd_lun *un = NULL; 24023 struct mode_header *sense_mhp, *select_mhp; 24024 struct mode_speed *sense_page, *select_page; 24025 int current_speed; 24026 int rval = EINVAL; 24027 int bd_len; 24028 uchar_t *sense = NULL; 24029 uchar_t *select = NULL; 24030 24031 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 24032 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24033 return (ENXIO); 24034 } 24035 24036 /* 24037 * Note: The drive speed is being modified here according to a Toshiba 24038 * vendor specific mode page (0x31). 24039 */ 24040 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 24041 24042 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 24043 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 24044 SD_PATH_STANDARD)) != 0) { 24045 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24046 "sr_change_speed: Mode Sense Failed\n"); 24047 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24048 return (rval); 24049 } 24050 sense_mhp = (struct mode_header *)sense; 24051 24052 /* Check the block descriptor len to handle only 1 block descriptor */ 24053 bd_len = sense_mhp->bdesc_length; 24054 if (bd_len > MODE_BLK_DESC_LENGTH) { 24055 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24056 "sr_change_speed: Mode Sense returned invalid block " 24057 "descriptor length\n"); 24058 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24059 return (EIO); 24060 } 24061 24062 sense_page = (struct mode_speed *) 24063 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 24064 current_speed = sense_page->speed; 24065 24066 /* Process command */ 24067 switch (cmd) { 24068 case CDROMGDRVSPEED: 24069 /* Return the drive speed obtained during the mode sense */ 24070 if (current_speed == 0x2) { 24071 current_speed = CDROM_TWELVE_SPEED; 24072 } 24073 if (ddi_copyout(¤t_speed, (void *)data, 24074 sizeof (int), flag) != 0) { 24075 rval = EFAULT; 24076 } 24077 break; 24078 case CDROMSDRVSPEED: 24079 /* Validate the requested drive speed */ 24080 switch ((uchar_t)data) { 24081 case CDROM_TWELVE_SPEED: 24082 data = 0x2; 24083 /*FALLTHROUGH*/ 24084 case CDROM_NORMAL_SPEED: 24085 case CDROM_DOUBLE_SPEED: 24086 case CDROM_QUAD_SPEED: 24087 case CDROM_MAXIMUM_SPEED: 24088 break; 24089 default: 24090 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24091 "sr_change_speed: " 24092 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 24093 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24094 return (EINVAL); 24095 } 24096 24097 /* 24098 * The current drive speed matches the requested drive speed so 24099 * there is no need to send the mode select to change the speed 24100 */ 24101 if (current_speed == data) { 24102 break; 24103 } 24104 24105 /* Build the select data for the requested drive speed */ 24106 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 24107 select_mhp = (struct mode_header *)select; 24108 select_mhp->bdesc_length = 0; 24109 select_page = 24110 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 24111 select_page = 24112 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 24113 select_page->mode_page.code = CDROM_MODE_SPEED; 24114 select_page->mode_page.length = 2; 24115 select_page->speed = (uchar_t)data; 24116 24117 /* Send the mode select for the requested block size */ 24118 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 24119 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 24120 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 24121 /* 24122 * The mode select failed for the requested drive speed, 24123 * so reset the data for the original drive speed and 24124 * send it to the target. The error is indicated by the 24125 * return value for the failed mode select. 24126 */ 24127 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24128 "sr_drive_speed: Mode Select Failed\n"); 24129 select_page->speed = sense_page->speed; 24130 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 24131 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 24132 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 24133 } 24134 break; 24135 default: 24136 /* should not reach here, but check anyway */ 24137 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24138 "sr_change_speed: Command '%x' Not Supported\n", cmd); 24139 rval = EINVAL; 24140 break; 24141 } 24142 24143 if (select) { 24144 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 24145 } 24146 if (sense) { 24147 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 24148 } 24149 24150 return (rval); 24151 } 24152 24153 24154 /* 24155 * Function: sr_atapi_change_speed() 24156 * 24157 * Description: This routine is the driver entry point for handling CD-ROM 24158 * drive speed ioctl requests for MMC devices that do not support 24159 * the Real Time Streaming feature (0x107). 24160 * 24161 * Note: This routine will use the SET SPEED command which may not 24162 * be supported by all devices. 24163 * 24164 * Arguments: dev- the device 'dev_t' 24165 * cmd- the request type; one of CDROMGDRVSPEED (get) or 24166 * CDROMSDRVSPEED (set) 24167 * data- current drive speed or requested drive speed 24168 * flag- this argument is a pass through to ddi_copyxxx() directly 24169 * from the mode argument of ioctl(). 24170 * 24171 * Return Code: the code returned by sd_send_scsi_cmd() 24172 * EINVAL if invalid arguments are provided 24173 * EFAULT if ddi_copyxxx() fails 24174 * ENXIO if fail ddi_get_soft_state 24175 * EIO if invalid mode sense block descriptor length 24176 */ 24177 24178 static int 24179 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 24180 { 24181 struct sd_lun *un; 24182 struct uscsi_cmd *com = NULL; 24183 struct mode_header_grp2 *sense_mhp; 24184 uchar_t *sense_page; 24185 uchar_t *sense = NULL; 24186 char cdb[CDB_GROUP5]; 24187 int bd_len; 24188 int current_speed = 0; 24189 int max_speed = 0; 24190 int rval; 24191 24192 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 24193 24194 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24195 return (ENXIO); 24196 } 24197 24198 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 24199 24200 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 24201 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 24202 SD_PATH_STANDARD)) != 0) { 24203 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24204 "sr_atapi_change_speed: Mode Sense Failed\n"); 24205 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24206 return (rval); 24207 } 24208 24209 /* Check the block descriptor len to handle only 1 block descriptor */ 24210 sense_mhp = (struct mode_header_grp2 *)sense; 24211 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 24212 if (bd_len > MODE_BLK_DESC_LENGTH) { 24213 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24214 "sr_atapi_change_speed: Mode Sense returned invalid " 24215 "block descriptor length\n"); 24216 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24217 return (EIO); 24218 } 24219 24220 /* Calculate the current and maximum drive speeds */ 24221 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 24222 current_speed = (sense_page[14] << 8) | sense_page[15]; 24223 max_speed = (sense_page[8] << 8) | sense_page[9]; 24224 24225 /* Process the command */ 24226 switch (cmd) { 24227 case CDROMGDRVSPEED: 24228 current_speed /= SD_SPEED_1X; 24229 if (ddi_copyout(¤t_speed, (void *)data, 24230 sizeof (int), flag) != 0) 24231 rval = EFAULT; 24232 break; 24233 case CDROMSDRVSPEED: 24234 /* Convert the speed code to KB/sec */ 24235 switch ((uchar_t)data) { 24236 case CDROM_NORMAL_SPEED: 24237 current_speed = SD_SPEED_1X; 24238 break; 24239 case CDROM_DOUBLE_SPEED: 24240 current_speed = 2 * SD_SPEED_1X; 24241 break; 24242 case CDROM_QUAD_SPEED: 24243 current_speed = 4 * SD_SPEED_1X; 24244 break; 24245 case CDROM_TWELVE_SPEED: 24246 current_speed = 12 * SD_SPEED_1X; 24247 break; 24248 case CDROM_MAXIMUM_SPEED: 24249 current_speed = 0xffff; 24250 break; 24251 default: 24252 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24253 "sr_atapi_change_speed: invalid drive speed %d\n", 24254 (uchar_t)data); 24255 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24256 return (EINVAL); 24257 } 24258 24259 /* Check the request against the drive's max speed. */ 24260 if (current_speed != 0xffff) { 24261 if (current_speed > max_speed) { 24262 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24263 return (EINVAL); 24264 } 24265 } 24266 24267 /* 24268 * Build and send the SET SPEED command 24269 * 24270 * Note: The SET SPEED (0xBB) command used in this routine is 24271 * obsolete per the SCSI MMC spec but still supported in the 24272 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24273 * therefore the command is still implemented in this routine. 24274 */ 24275 bzero(cdb, sizeof (cdb)); 24276 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 24277 cdb[2] = (uchar_t)(current_speed >> 8); 24278 cdb[3] = (uchar_t)current_speed; 24279 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24280 com->uscsi_cdb = (caddr_t)cdb; 24281 com->uscsi_cdblen = CDB_GROUP5; 24282 com->uscsi_bufaddr = NULL; 24283 com->uscsi_buflen = 0; 24284 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24285 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD); 24286 break; 24287 default: 24288 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24289 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 24290 rval = EINVAL; 24291 } 24292 24293 if (sense) { 24294 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 24295 } 24296 if (com) { 24297 kmem_free(com, sizeof (*com)); 24298 } 24299 return (rval); 24300 } 24301 24302 24303 /* 24304 * Function: sr_pause_resume() 24305 * 24306 * Description: This routine is the driver entry point for handling CD-ROM 24307 * pause/resume ioctl requests. This only affects the audio play 24308 * operation. 24309 * 24310 * Arguments: dev - the device 'dev_t' 24311 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 24312 * for setting the resume bit of the cdb. 24313 * 24314 * Return Code: the code returned by sd_send_scsi_cmd() 24315 * EINVAL if invalid mode specified 24316 * 24317 */ 24318 24319 static int 24320 sr_pause_resume(dev_t dev, int cmd) 24321 { 24322 struct sd_lun *un; 24323 struct uscsi_cmd *com; 24324 char cdb[CDB_GROUP1]; 24325 int rval; 24326 24327 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24328 return (ENXIO); 24329 } 24330 24331 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24332 bzero(cdb, CDB_GROUP1); 24333 cdb[0] = SCMD_PAUSE_RESUME; 24334 switch (cmd) { 24335 case CDROMRESUME: 24336 cdb[8] = 1; 24337 break; 24338 case CDROMPAUSE: 24339 cdb[8] = 0; 24340 break; 24341 default: 24342 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 24343 " Command '%x' Not Supported\n", cmd); 24344 rval = EINVAL; 24345 goto done; 24346 } 24347 24348 com->uscsi_cdb = cdb; 24349 com->uscsi_cdblen = CDB_GROUP1; 24350 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24351 24352 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24353 SD_PATH_STANDARD); 24354 24355 done: 24356 kmem_free(com, sizeof (*com)); 24357 return (rval); 24358 } 24359 24360 24361 /* 24362 * Function: sr_play_msf() 24363 * 24364 * Description: This routine is the driver entry point for handling CD-ROM 24365 * ioctl requests to output the audio signals at the specified 24366 * starting address and continue the audio play until the specified 24367 * ending address (CDROMPLAYMSF) The address is in Minute Second 24368 * Frame (MSF) format. 24369 * 24370 * Arguments: dev - the device 'dev_t' 24371 * data - pointer to user provided audio msf structure, 24372 * specifying start/end addresses. 24373 * flag - this argument is a pass through to ddi_copyxxx() 24374 * directly from the mode argument of ioctl(). 24375 * 24376 * Return Code: the code returned by sd_send_scsi_cmd() 24377 * EFAULT if ddi_copyxxx() fails 24378 * ENXIO if fail ddi_get_soft_state 24379 * EINVAL if data pointer is NULL 24380 */ 24381 24382 static int 24383 sr_play_msf(dev_t dev, caddr_t data, int flag) 24384 { 24385 struct sd_lun *un; 24386 struct uscsi_cmd *com; 24387 struct cdrom_msf msf_struct; 24388 struct cdrom_msf *msf = &msf_struct; 24389 char cdb[CDB_GROUP1]; 24390 int rval; 24391 24392 if (data == NULL) { 24393 return (EINVAL); 24394 } 24395 24396 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24397 return (ENXIO); 24398 } 24399 24400 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 24401 return (EFAULT); 24402 } 24403 24404 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24405 bzero(cdb, CDB_GROUP1); 24406 cdb[0] = SCMD_PLAYAUDIO_MSF; 24407 if (un->un_f_cfg_playmsf_bcd == TRUE) { 24408 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 24409 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 24410 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 24411 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 24412 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 24413 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 24414 } else { 24415 cdb[3] = msf->cdmsf_min0; 24416 cdb[4] = msf->cdmsf_sec0; 24417 cdb[5] = msf->cdmsf_frame0; 24418 cdb[6] = msf->cdmsf_min1; 24419 cdb[7] = msf->cdmsf_sec1; 24420 cdb[8] = msf->cdmsf_frame1; 24421 } 24422 com->uscsi_cdb = cdb; 24423 com->uscsi_cdblen = CDB_GROUP1; 24424 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24425 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24426 SD_PATH_STANDARD); 24427 kmem_free(com, sizeof (*com)); 24428 return (rval); 24429 } 24430 24431 24432 /* 24433 * Function: sr_play_trkind() 24434 * 24435 * Description: This routine is the driver entry point for handling CD-ROM 24436 * ioctl requests to output the audio signals at the specified 24437 * starting address and continue the audio play until the specified 24438 * ending address (CDROMPLAYTRKIND). The address is in Track Index 24439 * format. 24440 * 24441 * Arguments: dev - the device 'dev_t' 24442 * data - pointer to user provided audio track/index structure, 24443 * specifying start/end addresses. 24444 * flag - this argument is a pass through to ddi_copyxxx() 24445 * directly from the mode argument of ioctl(). 24446 * 24447 * Return Code: the code returned by sd_send_scsi_cmd() 24448 * EFAULT if ddi_copyxxx() fails 24449 * ENXIO if fail ddi_get_soft_state 24450 * EINVAL if data pointer is NULL 24451 */ 24452 24453 static int 24454 sr_play_trkind(dev_t dev, caddr_t data, int flag) 24455 { 24456 struct cdrom_ti ti_struct; 24457 struct cdrom_ti *ti = &ti_struct; 24458 struct uscsi_cmd *com = NULL; 24459 char cdb[CDB_GROUP1]; 24460 int rval; 24461 24462 if (data == NULL) { 24463 return (EINVAL); 24464 } 24465 24466 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 24467 return (EFAULT); 24468 } 24469 24470 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24471 bzero(cdb, CDB_GROUP1); 24472 cdb[0] = SCMD_PLAYAUDIO_TI; 24473 cdb[4] = ti->cdti_trk0; 24474 cdb[5] = ti->cdti_ind0; 24475 cdb[7] = ti->cdti_trk1; 24476 cdb[8] = ti->cdti_ind1; 24477 com->uscsi_cdb = cdb; 24478 com->uscsi_cdblen = CDB_GROUP1; 24479 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 24480 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24481 SD_PATH_STANDARD); 24482 kmem_free(com, sizeof (*com)); 24483 return (rval); 24484 } 24485 24486 24487 /* 24488 * Function: sr_read_all_subcodes() 24489 * 24490 * Description: This routine is the driver entry point for handling CD-ROM 24491 * ioctl requests to return raw subcode data while the target is 24492 * playing audio (CDROMSUBCODE). 24493 * 24494 * Arguments: dev - the device 'dev_t' 24495 * data - pointer to user provided cdrom subcode structure, 24496 * specifying the transfer length and address. 24497 * flag - this argument is a pass through to ddi_copyxxx() 24498 * directly from the mode argument of ioctl(). 24499 * 24500 * Return Code: the code returned by sd_send_scsi_cmd() 24501 * EFAULT if ddi_copyxxx() fails 24502 * ENXIO if fail ddi_get_soft_state 24503 * EINVAL if data pointer is NULL 24504 */ 24505 24506 static int 24507 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 24508 { 24509 struct sd_lun *un = NULL; 24510 struct uscsi_cmd *com = NULL; 24511 struct cdrom_subcode *subcode = NULL; 24512 int rval; 24513 size_t buflen; 24514 char cdb[CDB_GROUP5]; 24515 24516 #ifdef _MULTI_DATAMODEL 24517 /* To support ILP32 applications in an LP64 world */ 24518 struct cdrom_subcode32 cdrom_subcode32; 24519 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 24520 #endif 24521 if (data == NULL) { 24522 return (EINVAL); 24523 } 24524 24525 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24526 return (ENXIO); 24527 } 24528 24529 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 24530 24531 #ifdef _MULTI_DATAMODEL 24532 switch (ddi_model_convert_from(flag & FMODELS)) { 24533 case DDI_MODEL_ILP32: 24534 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 24535 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24536 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24537 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24538 return (EFAULT); 24539 } 24540 /* Convert the ILP32 uscsi data from the application to LP64 */ 24541 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 24542 break; 24543 case DDI_MODEL_NONE: 24544 if (ddi_copyin(data, subcode, 24545 sizeof (struct cdrom_subcode), flag)) { 24546 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24547 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24548 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24549 return (EFAULT); 24550 } 24551 break; 24552 } 24553 #else /* ! _MULTI_DATAMODEL */ 24554 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 24555 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24556 "sr_read_all_subcodes: ddi_copyin Failed\n"); 24557 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24558 return (EFAULT); 24559 } 24560 #endif /* _MULTI_DATAMODEL */ 24561 24562 /* 24563 * Since MMC-2 expects max 3 bytes for length, check if the 24564 * length input is greater than 3 bytes 24565 */ 24566 if ((subcode->cdsc_length & 0xFF000000) != 0) { 24567 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 24568 "sr_read_all_subcodes: " 24569 "cdrom transfer length too large: %d (limit %d)\n", 24570 subcode->cdsc_length, 0xFFFFFF); 24571 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24572 return (EINVAL); 24573 } 24574 24575 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 24576 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24577 bzero(cdb, CDB_GROUP5); 24578 24579 if (un->un_f_mmc_cap == TRUE) { 24580 cdb[0] = (char)SCMD_READ_CD; 24581 cdb[2] = (char)0xff; 24582 cdb[3] = (char)0xff; 24583 cdb[4] = (char)0xff; 24584 cdb[5] = (char)0xff; 24585 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24586 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24587 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 24588 cdb[10] = 1; 24589 } else { 24590 /* 24591 * Note: A vendor specific command (0xDF) is being used her to 24592 * request a read of all subcodes. 24593 */ 24594 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 24595 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 24596 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 24597 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 24598 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 24599 } 24600 com->uscsi_cdb = cdb; 24601 com->uscsi_cdblen = CDB_GROUP5; 24602 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 24603 com->uscsi_buflen = buflen; 24604 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24605 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 24606 SD_PATH_STANDARD); 24607 kmem_free(subcode, sizeof (struct cdrom_subcode)); 24608 kmem_free(com, sizeof (*com)); 24609 return (rval); 24610 } 24611 24612 24613 /* 24614 * Function: sr_read_subchannel() 24615 * 24616 * Description: This routine is the driver entry point for handling CD-ROM 24617 * ioctl requests to return the Q sub-channel data of the CD 24618 * current position block. (CDROMSUBCHNL) The data includes the 24619 * track number, index number, absolute CD-ROM address (LBA or MSF 24620 * format per the user) , track relative CD-ROM address (LBA or MSF 24621 * format per the user), control data and audio status. 24622 * 24623 * Arguments: dev - the device 'dev_t' 24624 * data - pointer to user provided cdrom sub-channel structure 24625 * flag - this argument is a pass through to ddi_copyxxx() 24626 * directly from the mode argument of ioctl(). 24627 * 24628 * Return Code: the code returned by sd_send_scsi_cmd() 24629 * EFAULT if ddi_copyxxx() fails 24630 * ENXIO if fail ddi_get_soft_state 24631 * EINVAL if data pointer is NULL 24632 */ 24633 24634 static int 24635 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 24636 { 24637 struct sd_lun *un; 24638 struct uscsi_cmd *com; 24639 struct cdrom_subchnl subchanel; 24640 struct cdrom_subchnl *subchnl = &subchanel; 24641 char cdb[CDB_GROUP1]; 24642 caddr_t buffer; 24643 int rval; 24644 24645 if (data == NULL) { 24646 return (EINVAL); 24647 } 24648 24649 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24650 (un->un_state == SD_STATE_OFFLINE)) { 24651 return (ENXIO); 24652 } 24653 24654 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 24655 return (EFAULT); 24656 } 24657 24658 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 24659 bzero(cdb, CDB_GROUP1); 24660 cdb[0] = SCMD_READ_SUBCHANNEL; 24661 /* Set the MSF bit based on the user requested address format */ 24662 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 24663 /* 24664 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 24665 * returned 24666 */ 24667 cdb[2] = 0x40; 24668 /* 24669 * Set byte 3 to specify the return data format. A value of 0x01 24670 * indicates that the CD-ROM current position should be returned. 24671 */ 24672 cdb[3] = 0x01; 24673 cdb[8] = 0x10; 24674 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24675 com->uscsi_cdb = cdb; 24676 com->uscsi_cdblen = CDB_GROUP1; 24677 com->uscsi_bufaddr = buffer; 24678 com->uscsi_buflen = 16; 24679 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24680 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24681 SD_PATH_STANDARD); 24682 if (rval != 0) { 24683 kmem_free(buffer, 16); 24684 kmem_free(com, sizeof (*com)); 24685 return (rval); 24686 } 24687 24688 /* Process the returned Q sub-channel data */ 24689 subchnl->cdsc_audiostatus = buffer[1]; 24690 subchnl->cdsc_adr = (buffer[5] & 0xF0); 24691 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 24692 subchnl->cdsc_trk = buffer[6]; 24693 subchnl->cdsc_ind = buffer[7]; 24694 if (subchnl->cdsc_format & CDROM_LBA) { 24695 subchnl->cdsc_absaddr.lba = 24696 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24697 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24698 subchnl->cdsc_reladdr.lba = 24699 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 24700 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 24701 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 24702 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 24703 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 24704 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 24705 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 24706 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 24707 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 24708 } else { 24709 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 24710 subchnl->cdsc_absaddr.msf.second = buffer[10]; 24711 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 24712 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 24713 subchnl->cdsc_reladdr.msf.second = buffer[14]; 24714 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 24715 } 24716 kmem_free(buffer, 16); 24717 kmem_free(com, sizeof (*com)); 24718 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 24719 != 0) { 24720 return (EFAULT); 24721 } 24722 return (rval); 24723 } 24724 24725 24726 /* 24727 * Function: sr_read_tocentry() 24728 * 24729 * Description: This routine is the driver entry point for handling CD-ROM 24730 * ioctl requests to read from the Table of Contents (TOC) 24731 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 24732 * fields, the starting address (LBA or MSF format per the user) 24733 * and the data mode if the user specified track is a data track. 24734 * 24735 * Note: The READ HEADER (0x44) command used in this routine is 24736 * obsolete per the SCSI MMC spec but still supported in the 24737 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 24738 * therefore the command is still implemented in this routine. 24739 * 24740 * Arguments: dev - the device 'dev_t' 24741 * data - pointer to user provided toc entry structure, 24742 * specifying the track # and the address format 24743 * (LBA or MSF). 24744 * flag - this argument is a pass through to ddi_copyxxx() 24745 * directly from the mode argument of ioctl(). 24746 * 24747 * Return Code: the code returned by sd_send_scsi_cmd() 24748 * EFAULT if ddi_copyxxx() fails 24749 * ENXIO if fail ddi_get_soft_state 24750 * EINVAL if data pointer is NULL 24751 */ 24752 24753 static int 24754 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 24755 { 24756 struct sd_lun *un = NULL; 24757 struct uscsi_cmd *com; 24758 struct cdrom_tocentry toc_entry; 24759 struct cdrom_tocentry *entry = &toc_entry; 24760 caddr_t buffer; 24761 int rval; 24762 char cdb[CDB_GROUP1]; 24763 24764 if (data == NULL) { 24765 return (EINVAL); 24766 } 24767 24768 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24769 (un->un_state == SD_STATE_OFFLINE)) { 24770 return (ENXIO); 24771 } 24772 24773 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 24774 return (EFAULT); 24775 } 24776 24777 /* Validate the requested track and address format */ 24778 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 24779 return (EINVAL); 24780 } 24781 24782 if (entry->cdte_track == 0) { 24783 return (EINVAL); 24784 } 24785 24786 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 24787 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24788 bzero(cdb, CDB_GROUP1); 24789 24790 cdb[0] = SCMD_READ_TOC; 24791 /* Set the MSF bit based on the user requested address format */ 24792 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 24793 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24794 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 24795 } else { 24796 cdb[6] = entry->cdte_track; 24797 } 24798 24799 /* 24800 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 24801 * (4 byte TOC response header + 8 byte track descriptor) 24802 */ 24803 cdb[8] = 12; 24804 com->uscsi_cdb = cdb; 24805 com->uscsi_cdblen = CDB_GROUP1; 24806 com->uscsi_bufaddr = buffer; 24807 com->uscsi_buflen = 0x0C; 24808 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 24809 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24810 SD_PATH_STANDARD); 24811 if (rval != 0) { 24812 kmem_free(buffer, 12); 24813 kmem_free(com, sizeof (*com)); 24814 return (rval); 24815 } 24816 24817 /* Process the toc entry */ 24818 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 24819 entry->cdte_ctrl = (buffer[5] & 0x0F); 24820 if (entry->cdte_format & CDROM_LBA) { 24821 entry->cdte_addr.lba = 24822 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 24823 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 24824 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 24825 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 24826 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 24827 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 24828 /* 24829 * Send a READ TOC command using the LBA address format to get 24830 * the LBA for the track requested so it can be used in the 24831 * READ HEADER request 24832 * 24833 * Note: The MSF bit of the READ HEADER command specifies the 24834 * output format. The block address specified in that command 24835 * must be in LBA format. 24836 */ 24837 cdb[1] = 0; 24838 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24839 SD_PATH_STANDARD); 24840 if (rval != 0) { 24841 kmem_free(buffer, 12); 24842 kmem_free(com, sizeof (*com)); 24843 return (rval); 24844 } 24845 } else { 24846 entry->cdte_addr.msf.minute = buffer[9]; 24847 entry->cdte_addr.msf.second = buffer[10]; 24848 entry->cdte_addr.msf.frame = buffer[11]; 24849 /* 24850 * Send a READ TOC command using the LBA address format to get 24851 * the LBA for the track requested so it can be used in the 24852 * READ HEADER request 24853 * 24854 * Note: The MSF bit of the READ HEADER command specifies the 24855 * output format. The block address specified in that command 24856 * must be in LBA format. 24857 */ 24858 cdb[1] = 0; 24859 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24860 SD_PATH_STANDARD); 24861 if (rval != 0) { 24862 kmem_free(buffer, 12); 24863 kmem_free(com, sizeof (*com)); 24864 return (rval); 24865 } 24866 } 24867 24868 /* 24869 * Build and send the READ HEADER command to determine the data mode of 24870 * the user specified track. 24871 */ 24872 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 24873 (entry->cdte_track != CDROM_LEADOUT)) { 24874 bzero(cdb, CDB_GROUP1); 24875 cdb[0] = SCMD_READ_HEADER; 24876 cdb[2] = buffer[8]; 24877 cdb[3] = buffer[9]; 24878 cdb[4] = buffer[10]; 24879 cdb[5] = buffer[11]; 24880 cdb[8] = 0x08; 24881 com->uscsi_buflen = 0x08; 24882 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24883 SD_PATH_STANDARD); 24884 if (rval == 0) { 24885 entry->cdte_datamode = buffer[0]; 24886 } else { 24887 /* 24888 * READ HEADER command failed, since this is 24889 * obsoleted in one spec, its better to return 24890 * -1 for an invlid track so that we can still 24891 * receive the rest of the TOC data. 24892 */ 24893 entry->cdte_datamode = (uchar_t)-1; 24894 } 24895 } else { 24896 entry->cdte_datamode = (uchar_t)-1; 24897 } 24898 24899 kmem_free(buffer, 12); 24900 kmem_free(com, sizeof (*com)); 24901 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 24902 return (EFAULT); 24903 24904 return (rval); 24905 } 24906 24907 24908 /* 24909 * Function: sr_read_tochdr() 24910 * 24911 * Description: This routine is the driver entry point for handling CD-ROM 24912 * ioctl requests to read the Table of Contents (TOC) header 24913 * (CDROMREADTOHDR). The TOC header consists of the disk starting 24914 * and ending track numbers 24915 * 24916 * Arguments: dev - the device 'dev_t' 24917 * data - pointer to user provided toc header structure, 24918 * specifying the starting and ending track numbers. 24919 * flag - this argument is a pass through to ddi_copyxxx() 24920 * directly from the mode argument of ioctl(). 24921 * 24922 * Return Code: the code returned by sd_send_scsi_cmd() 24923 * EFAULT if ddi_copyxxx() fails 24924 * ENXIO if fail ddi_get_soft_state 24925 * EINVAL if data pointer is NULL 24926 */ 24927 24928 static int 24929 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 24930 { 24931 struct sd_lun *un; 24932 struct uscsi_cmd *com; 24933 struct cdrom_tochdr toc_header; 24934 struct cdrom_tochdr *hdr = &toc_header; 24935 char cdb[CDB_GROUP1]; 24936 int rval; 24937 caddr_t buffer; 24938 24939 if (data == NULL) { 24940 return (EINVAL); 24941 } 24942 24943 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 24944 (un->un_state == SD_STATE_OFFLINE)) { 24945 return (ENXIO); 24946 } 24947 24948 buffer = kmem_zalloc(4, KM_SLEEP); 24949 bzero(cdb, CDB_GROUP1); 24950 cdb[0] = SCMD_READ_TOC; 24951 /* 24952 * Specifying a track number of 0x00 in the READ TOC command indicates 24953 * that the TOC header should be returned 24954 */ 24955 cdb[6] = 0x00; 24956 /* 24957 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 24958 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 24959 */ 24960 cdb[8] = 0x04; 24961 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 24962 com->uscsi_cdb = cdb; 24963 com->uscsi_cdblen = CDB_GROUP1; 24964 com->uscsi_bufaddr = buffer; 24965 com->uscsi_buflen = 0x04; 24966 com->uscsi_timeout = 300; 24967 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 24968 24969 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 24970 SD_PATH_STANDARD); 24971 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 24972 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 24973 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 24974 } else { 24975 hdr->cdth_trk0 = buffer[2]; 24976 hdr->cdth_trk1 = buffer[3]; 24977 } 24978 kmem_free(buffer, 4); 24979 kmem_free(com, sizeof (*com)); 24980 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 24981 return (EFAULT); 24982 } 24983 return (rval); 24984 } 24985 24986 24987 /* 24988 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 24989 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 24990 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 24991 * digital audio and extended architecture digital audio. These modes are 24992 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 24993 * MMC specs. 24994 * 24995 * In addition to support for the various data formats these routines also 24996 * include support for devices that implement only the direct access READ 24997 * commands (0x08, 0x28), devices that implement the READ_CD commands 24998 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 24999 * READ CDXA commands (0xD8, 0xDB) 25000 */ 25001 25002 /* 25003 * Function: sr_read_mode1() 25004 * 25005 * Description: This routine is the driver entry point for handling CD-ROM 25006 * ioctl read mode1 requests (CDROMREADMODE1). 25007 * 25008 * Arguments: dev - the device 'dev_t' 25009 * data - pointer to user provided cd read structure specifying 25010 * the lba buffer address and length. 25011 * flag - this argument is a pass through to ddi_copyxxx() 25012 * directly from the mode argument of ioctl(). 25013 * 25014 * Return Code: the code returned by sd_send_scsi_cmd() 25015 * EFAULT if ddi_copyxxx() fails 25016 * ENXIO if fail ddi_get_soft_state 25017 * EINVAL if data pointer is NULL 25018 */ 25019 25020 static int 25021 sr_read_mode1(dev_t dev, caddr_t data, int flag) 25022 { 25023 struct sd_lun *un; 25024 struct cdrom_read mode1_struct; 25025 struct cdrom_read *mode1 = &mode1_struct; 25026 int rval; 25027 #ifdef _MULTI_DATAMODEL 25028 /* To support ILP32 applications in an LP64 world */ 25029 struct cdrom_read32 cdrom_read32; 25030 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25031 #endif /* _MULTI_DATAMODEL */ 25032 25033 if (data == NULL) { 25034 return (EINVAL); 25035 } 25036 25037 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25038 (un->un_state == SD_STATE_OFFLINE)) { 25039 return (ENXIO); 25040 } 25041 25042 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25043 "sd_read_mode1: entry: un:0x%p\n", un); 25044 25045 #ifdef _MULTI_DATAMODEL 25046 switch (ddi_model_convert_from(flag & FMODELS)) { 25047 case DDI_MODEL_ILP32: 25048 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25049 return (EFAULT); 25050 } 25051 /* Convert the ILP32 uscsi data from the application to LP64 */ 25052 cdrom_read32tocdrom_read(cdrd32, mode1); 25053 break; 25054 case DDI_MODEL_NONE: 25055 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 25056 return (EFAULT); 25057 } 25058 } 25059 #else /* ! _MULTI_DATAMODEL */ 25060 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 25061 return (EFAULT); 25062 } 25063 #endif /* _MULTI_DATAMODEL */ 25064 25065 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 25066 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 25067 25068 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25069 "sd_read_mode1: exit: un:0x%p\n", un); 25070 25071 return (rval); 25072 } 25073 25074 25075 /* 25076 * Function: sr_read_cd_mode2() 25077 * 25078 * Description: This routine is the driver entry point for handling CD-ROM 25079 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 25080 * support the READ CD (0xBE) command or the 1st generation 25081 * READ CD (0xD4) command. 25082 * 25083 * Arguments: dev - the device 'dev_t' 25084 * data - pointer to user provided cd read structure specifying 25085 * the lba buffer address and length. 25086 * flag - this argument is a pass through to ddi_copyxxx() 25087 * directly from the mode argument of ioctl(). 25088 * 25089 * Return Code: the code returned by sd_send_scsi_cmd() 25090 * EFAULT if ddi_copyxxx() fails 25091 * ENXIO if fail ddi_get_soft_state 25092 * EINVAL if data pointer is NULL 25093 */ 25094 25095 static int 25096 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 25097 { 25098 struct sd_lun *un; 25099 struct uscsi_cmd *com; 25100 struct cdrom_read mode2_struct; 25101 struct cdrom_read *mode2 = &mode2_struct; 25102 uchar_t cdb[CDB_GROUP5]; 25103 int nblocks; 25104 int rval; 25105 #ifdef _MULTI_DATAMODEL 25106 /* To support ILP32 applications in an LP64 world */ 25107 struct cdrom_read32 cdrom_read32; 25108 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25109 #endif /* _MULTI_DATAMODEL */ 25110 25111 if (data == NULL) { 25112 return (EINVAL); 25113 } 25114 25115 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25116 (un->un_state == SD_STATE_OFFLINE)) { 25117 return (ENXIO); 25118 } 25119 25120 #ifdef _MULTI_DATAMODEL 25121 switch (ddi_model_convert_from(flag & FMODELS)) { 25122 case DDI_MODEL_ILP32: 25123 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25124 return (EFAULT); 25125 } 25126 /* Convert the ILP32 uscsi data from the application to LP64 */ 25127 cdrom_read32tocdrom_read(cdrd32, mode2); 25128 break; 25129 case DDI_MODEL_NONE: 25130 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25131 return (EFAULT); 25132 } 25133 break; 25134 } 25135 25136 #else /* ! _MULTI_DATAMODEL */ 25137 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25138 return (EFAULT); 25139 } 25140 #endif /* _MULTI_DATAMODEL */ 25141 25142 bzero(cdb, sizeof (cdb)); 25143 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 25144 /* Read command supported by 1st generation atapi drives */ 25145 cdb[0] = SCMD_READ_CDD4; 25146 } else { 25147 /* Universal CD Access Command */ 25148 cdb[0] = SCMD_READ_CD; 25149 } 25150 25151 /* 25152 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 25153 */ 25154 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 25155 25156 /* set the start address */ 25157 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 25158 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 25159 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 25160 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 25161 25162 /* set the transfer length */ 25163 nblocks = mode2->cdread_buflen / 2336; 25164 cdb[6] = (uchar_t)(nblocks >> 16); 25165 cdb[7] = (uchar_t)(nblocks >> 8); 25166 cdb[8] = (uchar_t)nblocks; 25167 25168 /* set the filter bits */ 25169 cdb[9] = CDROM_READ_CD_USERDATA; 25170 25171 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25172 com->uscsi_cdb = (caddr_t)cdb; 25173 com->uscsi_cdblen = sizeof (cdb); 25174 com->uscsi_bufaddr = mode2->cdread_bufaddr; 25175 com->uscsi_buflen = mode2->cdread_buflen; 25176 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25177 25178 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25179 SD_PATH_STANDARD); 25180 kmem_free(com, sizeof (*com)); 25181 return (rval); 25182 } 25183 25184 25185 /* 25186 * Function: sr_read_mode2() 25187 * 25188 * Description: This routine is the driver entry point for handling CD-ROM 25189 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 25190 * do not support the READ CD (0xBE) command. 25191 * 25192 * Arguments: dev - the device 'dev_t' 25193 * data - pointer to user provided cd read structure specifying 25194 * the lba buffer address and length. 25195 * flag - this argument is a pass through to ddi_copyxxx() 25196 * directly from the mode argument of ioctl(). 25197 * 25198 * Return Code: the code returned by sd_send_scsi_cmd() 25199 * EFAULT if ddi_copyxxx() fails 25200 * ENXIO if fail ddi_get_soft_state 25201 * EINVAL if data pointer is NULL 25202 * EIO if fail to reset block size 25203 * EAGAIN if commands are in progress in the driver 25204 */ 25205 25206 static int 25207 sr_read_mode2(dev_t dev, caddr_t data, int flag) 25208 { 25209 struct sd_lun *un; 25210 struct cdrom_read mode2_struct; 25211 struct cdrom_read *mode2 = &mode2_struct; 25212 int rval; 25213 uint32_t restore_blksize; 25214 struct uscsi_cmd *com; 25215 uchar_t cdb[CDB_GROUP0]; 25216 int nblocks; 25217 25218 #ifdef _MULTI_DATAMODEL 25219 /* To support ILP32 applications in an LP64 world */ 25220 struct cdrom_read32 cdrom_read32; 25221 struct cdrom_read32 *cdrd32 = &cdrom_read32; 25222 #endif /* _MULTI_DATAMODEL */ 25223 25224 if (data == NULL) { 25225 return (EINVAL); 25226 } 25227 25228 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25229 (un->un_state == SD_STATE_OFFLINE)) { 25230 return (ENXIO); 25231 } 25232 25233 /* 25234 * Because this routine will update the device and driver block size 25235 * being used we want to make sure there are no commands in progress. 25236 * If commands are in progress the user will have to try again. 25237 * 25238 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 25239 * in sdioctl to protect commands from sdioctl through to the top of 25240 * sd_uscsi_strategy. See sdioctl for details. 25241 */ 25242 mutex_enter(SD_MUTEX(un)); 25243 if (un->un_ncmds_in_driver != 1) { 25244 mutex_exit(SD_MUTEX(un)); 25245 return (EAGAIN); 25246 } 25247 mutex_exit(SD_MUTEX(un)); 25248 25249 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25250 "sd_read_mode2: entry: un:0x%p\n", un); 25251 25252 #ifdef _MULTI_DATAMODEL 25253 switch (ddi_model_convert_from(flag & FMODELS)) { 25254 case DDI_MODEL_ILP32: 25255 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 25256 return (EFAULT); 25257 } 25258 /* Convert the ILP32 uscsi data from the application to LP64 */ 25259 cdrom_read32tocdrom_read(cdrd32, mode2); 25260 break; 25261 case DDI_MODEL_NONE: 25262 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 25263 return (EFAULT); 25264 } 25265 break; 25266 } 25267 #else /* ! _MULTI_DATAMODEL */ 25268 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 25269 return (EFAULT); 25270 } 25271 #endif /* _MULTI_DATAMODEL */ 25272 25273 /* Store the current target block size for restoration later */ 25274 restore_blksize = un->un_tgt_blocksize; 25275 25276 /* Change the device and soft state target block size to 2336 */ 25277 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 25278 rval = EIO; 25279 goto done; 25280 } 25281 25282 25283 bzero(cdb, sizeof (cdb)); 25284 25285 /* set READ operation */ 25286 cdb[0] = SCMD_READ; 25287 25288 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 25289 mode2->cdread_lba >>= 2; 25290 25291 /* set the start address */ 25292 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 25293 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 25294 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 25295 25296 /* set the transfer length */ 25297 nblocks = mode2->cdread_buflen / 2336; 25298 cdb[4] = (uchar_t)nblocks & 0xFF; 25299 25300 /* build command */ 25301 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25302 com->uscsi_cdb = (caddr_t)cdb; 25303 com->uscsi_cdblen = sizeof (cdb); 25304 com->uscsi_bufaddr = mode2->cdread_bufaddr; 25305 com->uscsi_buflen = mode2->cdread_buflen; 25306 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25307 25308 /* 25309 * Issue SCSI command with user space address for read buffer. 25310 * 25311 * This sends the command through main channel in the driver. 25312 * 25313 * Since this is accessed via an IOCTL call, we go through the 25314 * standard path, so that if the device was powered down, then 25315 * it would be 'awakened' to handle the command. 25316 */ 25317 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25318 SD_PATH_STANDARD); 25319 25320 kmem_free(com, sizeof (*com)); 25321 25322 /* Restore the device and soft state target block size */ 25323 if (sr_sector_mode(dev, restore_blksize) != 0) { 25324 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25325 "can't do switch back to mode 1\n"); 25326 /* 25327 * If sd_send_scsi_READ succeeded we still need to report 25328 * an error because we failed to reset the block size 25329 */ 25330 if (rval == 0) { 25331 rval = EIO; 25332 } 25333 } 25334 25335 done: 25336 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 25337 "sd_read_mode2: exit: un:0x%p\n", un); 25338 25339 return (rval); 25340 } 25341 25342 25343 /* 25344 * Function: sr_sector_mode() 25345 * 25346 * Description: This utility function is used by sr_read_mode2 to set the target 25347 * block size based on the user specified size. This is a legacy 25348 * implementation based upon a vendor specific mode page 25349 * 25350 * Arguments: dev - the device 'dev_t' 25351 * data - flag indicating if block size is being set to 2336 or 25352 * 512. 25353 * 25354 * Return Code: the code returned by sd_send_scsi_cmd() 25355 * EFAULT if ddi_copyxxx() fails 25356 * ENXIO if fail ddi_get_soft_state 25357 * EINVAL if data pointer is NULL 25358 */ 25359 25360 static int 25361 sr_sector_mode(dev_t dev, uint32_t blksize) 25362 { 25363 struct sd_lun *un; 25364 uchar_t *sense; 25365 uchar_t *select; 25366 int rval; 25367 25368 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25369 (un->un_state == SD_STATE_OFFLINE)) { 25370 return (ENXIO); 25371 } 25372 25373 sense = kmem_zalloc(20, KM_SLEEP); 25374 25375 /* Note: This is a vendor specific mode page (0x81) */ 25376 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 25377 SD_PATH_STANDARD)) != 0) { 25378 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25379 "sr_sector_mode: Mode Sense failed\n"); 25380 kmem_free(sense, 20); 25381 return (rval); 25382 } 25383 select = kmem_zalloc(20, KM_SLEEP); 25384 select[3] = 0x08; 25385 select[10] = ((blksize >> 8) & 0xff); 25386 select[11] = (blksize & 0xff); 25387 select[12] = 0x01; 25388 select[13] = 0x06; 25389 select[14] = sense[14]; 25390 select[15] = sense[15]; 25391 if (blksize == SD_MODE2_BLKSIZE) { 25392 select[14] |= 0x01; 25393 } 25394 25395 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 25396 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 25397 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25398 "sr_sector_mode: Mode Select failed\n"); 25399 } else { 25400 /* 25401 * Only update the softstate block size if we successfully 25402 * changed the device block mode. 25403 */ 25404 mutex_enter(SD_MUTEX(un)); 25405 sd_update_block_info(un, blksize, 0); 25406 mutex_exit(SD_MUTEX(un)); 25407 } 25408 kmem_free(sense, 20); 25409 kmem_free(select, 20); 25410 return (rval); 25411 } 25412 25413 25414 /* 25415 * Function: sr_read_cdda() 25416 * 25417 * Description: This routine is the driver entry point for handling CD-ROM 25418 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 25419 * the target supports CDDA these requests are handled via a vendor 25420 * specific command (0xD8) If the target does not support CDDA 25421 * these requests are handled via the READ CD command (0xBE). 25422 * 25423 * Arguments: dev - the device 'dev_t' 25424 * data - pointer to user provided CD-DA structure specifying 25425 * the track starting address, transfer length, and 25426 * subcode options. 25427 * flag - this argument is a pass through to ddi_copyxxx() 25428 * directly from the mode argument of ioctl(). 25429 * 25430 * Return Code: the code returned by sd_send_scsi_cmd() 25431 * EFAULT if ddi_copyxxx() fails 25432 * ENXIO if fail ddi_get_soft_state 25433 * EINVAL if invalid arguments are provided 25434 * ENOTTY 25435 */ 25436 25437 static int 25438 sr_read_cdda(dev_t dev, caddr_t data, int flag) 25439 { 25440 struct sd_lun *un; 25441 struct uscsi_cmd *com; 25442 struct cdrom_cdda *cdda; 25443 int rval; 25444 size_t buflen; 25445 char cdb[CDB_GROUP5]; 25446 25447 #ifdef _MULTI_DATAMODEL 25448 /* To support ILP32 applications in an LP64 world */ 25449 struct cdrom_cdda32 cdrom_cdda32; 25450 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 25451 #endif /* _MULTI_DATAMODEL */ 25452 25453 if (data == NULL) { 25454 return (EINVAL); 25455 } 25456 25457 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25458 return (ENXIO); 25459 } 25460 25461 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 25462 25463 #ifdef _MULTI_DATAMODEL 25464 switch (ddi_model_convert_from(flag & FMODELS)) { 25465 case DDI_MODEL_ILP32: 25466 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 25467 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25468 "sr_read_cdda: ddi_copyin Failed\n"); 25469 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25470 return (EFAULT); 25471 } 25472 /* Convert the ILP32 uscsi data from the application to LP64 */ 25473 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 25474 break; 25475 case DDI_MODEL_NONE: 25476 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25477 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25478 "sr_read_cdda: ddi_copyin Failed\n"); 25479 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25480 return (EFAULT); 25481 } 25482 break; 25483 } 25484 #else /* ! _MULTI_DATAMODEL */ 25485 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 25486 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25487 "sr_read_cdda: ddi_copyin Failed\n"); 25488 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25489 return (EFAULT); 25490 } 25491 #endif /* _MULTI_DATAMODEL */ 25492 25493 /* 25494 * Since MMC-2 expects max 3 bytes for length, check if the 25495 * length input is greater than 3 bytes 25496 */ 25497 if ((cdda->cdda_length & 0xFF000000) != 0) { 25498 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 25499 "cdrom transfer length too large: %d (limit %d)\n", 25500 cdda->cdda_length, 0xFFFFFF); 25501 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25502 return (EINVAL); 25503 } 25504 25505 switch (cdda->cdda_subcode) { 25506 case CDROM_DA_NO_SUBCODE: 25507 buflen = CDROM_BLK_2352 * cdda->cdda_length; 25508 break; 25509 case CDROM_DA_SUBQ: 25510 buflen = CDROM_BLK_2368 * cdda->cdda_length; 25511 break; 25512 case CDROM_DA_ALL_SUBCODE: 25513 buflen = CDROM_BLK_2448 * cdda->cdda_length; 25514 break; 25515 case CDROM_DA_SUBCODE_ONLY: 25516 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 25517 break; 25518 default: 25519 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25520 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 25521 cdda->cdda_subcode); 25522 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25523 return (EINVAL); 25524 } 25525 25526 /* Build and send the command */ 25527 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25528 bzero(cdb, CDB_GROUP5); 25529 25530 if (un->un_f_cfg_cdda == TRUE) { 25531 cdb[0] = (char)SCMD_READ_CD; 25532 cdb[1] = 0x04; 25533 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25534 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25535 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25536 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25537 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25538 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25539 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 25540 cdb[9] = 0x10; 25541 switch (cdda->cdda_subcode) { 25542 case CDROM_DA_NO_SUBCODE : 25543 cdb[10] = 0x0; 25544 break; 25545 case CDROM_DA_SUBQ : 25546 cdb[10] = 0x2; 25547 break; 25548 case CDROM_DA_ALL_SUBCODE : 25549 cdb[10] = 0x1; 25550 break; 25551 case CDROM_DA_SUBCODE_ONLY : 25552 /* FALLTHROUGH */ 25553 default : 25554 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25555 kmem_free(com, sizeof (*com)); 25556 return (ENOTTY); 25557 } 25558 } else { 25559 cdb[0] = (char)SCMD_READ_CDDA; 25560 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 25561 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 25562 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 25563 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 25564 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 25565 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 25566 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 25567 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 25568 cdb[10] = cdda->cdda_subcode; 25569 } 25570 25571 com->uscsi_cdb = cdb; 25572 com->uscsi_cdblen = CDB_GROUP5; 25573 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 25574 com->uscsi_buflen = buflen; 25575 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25576 25577 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25578 SD_PATH_STANDARD); 25579 25580 kmem_free(cdda, sizeof (struct cdrom_cdda)); 25581 kmem_free(com, sizeof (*com)); 25582 return (rval); 25583 } 25584 25585 25586 /* 25587 * Function: sr_read_cdxa() 25588 * 25589 * Description: This routine is the driver entry point for handling CD-ROM 25590 * ioctl requests to return CD-XA (Extended Architecture) data. 25591 * (CDROMCDXA). 25592 * 25593 * Arguments: dev - the device 'dev_t' 25594 * data - pointer to user provided CD-XA structure specifying 25595 * the data starting address, transfer length, and format 25596 * flag - this argument is a pass through to ddi_copyxxx() 25597 * directly from the mode argument of ioctl(). 25598 * 25599 * Return Code: the code returned by sd_send_scsi_cmd() 25600 * EFAULT if ddi_copyxxx() fails 25601 * ENXIO if fail ddi_get_soft_state 25602 * EINVAL if data pointer is NULL 25603 */ 25604 25605 static int 25606 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 25607 { 25608 struct sd_lun *un; 25609 struct uscsi_cmd *com; 25610 struct cdrom_cdxa *cdxa; 25611 int rval; 25612 size_t buflen; 25613 char cdb[CDB_GROUP5]; 25614 uchar_t read_flags; 25615 25616 #ifdef _MULTI_DATAMODEL 25617 /* To support ILP32 applications in an LP64 world */ 25618 struct cdrom_cdxa32 cdrom_cdxa32; 25619 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 25620 #endif /* _MULTI_DATAMODEL */ 25621 25622 if (data == NULL) { 25623 return (EINVAL); 25624 } 25625 25626 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25627 return (ENXIO); 25628 } 25629 25630 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 25631 25632 #ifdef _MULTI_DATAMODEL 25633 switch (ddi_model_convert_from(flag & FMODELS)) { 25634 case DDI_MODEL_ILP32: 25635 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 25636 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25637 return (EFAULT); 25638 } 25639 /* 25640 * Convert the ILP32 uscsi data from the 25641 * application to LP64 for internal use. 25642 */ 25643 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 25644 break; 25645 case DDI_MODEL_NONE: 25646 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25647 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25648 return (EFAULT); 25649 } 25650 break; 25651 } 25652 #else /* ! _MULTI_DATAMODEL */ 25653 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 25654 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25655 return (EFAULT); 25656 } 25657 #endif /* _MULTI_DATAMODEL */ 25658 25659 /* 25660 * Since MMC-2 expects max 3 bytes for length, check if the 25661 * length input is greater than 3 bytes 25662 */ 25663 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 25664 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 25665 "cdrom transfer length too large: %d (limit %d)\n", 25666 cdxa->cdxa_length, 0xFFFFFF); 25667 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25668 return (EINVAL); 25669 } 25670 25671 switch (cdxa->cdxa_format) { 25672 case CDROM_XA_DATA: 25673 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 25674 read_flags = 0x10; 25675 break; 25676 case CDROM_XA_SECTOR_DATA: 25677 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 25678 read_flags = 0xf8; 25679 break; 25680 case CDROM_XA_DATA_W_ERROR: 25681 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 25682 read_flags = 0xfc; 25683 break; 25684 default: 25685 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25686 "sr_read_cdxa: Format '0x%x' Not Supported\n", 25687 cdxa->cdxa_format); 25688 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25689 return (EINVAL); 25690 } 25691 25692 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 25693 bzero(cdb, CDB_GROUP5); 25694 if (un->un_f_mmc_cap == TRUE) { 25695 cdb[0] = (char)SCMD_READ_CD; 25696 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25697 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25698 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25699 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25700 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25701 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25702 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 25703 cdb[9] = (char)read_flags; 25704 } else { 25705 /* 25706 * Note: A vendor specific command (0xDB) is being used her to 25707 * request a read of all subcodes. 25708 */ 25709 cdb[0] = (char)SCMD_READ_CDXA; 25710 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 25711 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 25712 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 25713 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 25714 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 25715 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 25716 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 25717 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 25718 cdb[10] = cdxa->cdxa_format; 25719 } 25720 com->uscsi_cdb = cdb; 25721 com->uscsi_cdblen = CDB_GROUP5; 25722 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 25723 com->uscsi_buflen = buflen; 25724 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 25725 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE, 25726 SD_PATH_STANDARD); 25727 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 25728 kmem_free(com, sizeof (*com)); 25729 return (rval); 25730 } 25731 25732 25733 /* 25734 * Function: sr_eject() 25735 * 25736 * Description: This routine is the driver entry point for handling CD-ROM 25737 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 25738 * 25739 * Arguments: dev - the device 'dev_t' 25740 * 25741 * Return Code: the code returned by sd_send_scsi_cmd() 25742 */ 25743 25744 static int 25745 sr_eject(dev_t dev) 25746 { 25747 struct sd_lun *un; 25748 int rval; 25749 25750 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25751 (un->un_state == SD_STATE_OFFLINE)) { 25752 return (ENXIO); 25753 } 25754 25755 /* 25756 * To prevent race conditions with the eject 25757 * command, keep track of an eject command as 25758 * it progresses. If we are already handling 25759 * an eject command in the driver for the given 25760 * unit and another request to eject is received 25761 * immediately return EAGAIN so we don't lose 25762 * the command if the current eject command fails. 25763 */ 25764 mutex_enter(SD_MUTEX(un)); 25765 if (un->un_f_ejecting == TRUE) { 25766 mutex_exit(SD_MUTEX(un)); 25767 return (EAGAIN); 25768 } 25769 un->un_f_ejecting = TRUE; 25770 mutex_exit(SD_MUTEX(un)); 25771 25772 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 25773 SD_PATH_STANDARD)) != 0) { 25774 mutex_enter(SD_MUTEX(un)); 25775 un->un_f_ejecting = FALSE; 25776 mutex_exit(SD_MUTEX(un)); 25777 return (rval); 25778 } 25779 25780 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 25781 SD_PATH_STANDARD); 25782 25783 if (rval == 0) { 25784 mutex_enter(SD_MUTEX(un)); 25785 sr_ejected(un); 25786 un->un_mediastate = DKIO_EJECTED; 25787 un->un_f_ejecting = FALSE; 25788 cv_broadcast(&un->un_state_cv); 25789 mutex_exit(SD_MUTEX(un)); 25790 } else { 25791 mutex_enter(SD_MUTEX(un)); 25792 un->un_f_ejecting = FALSE; 25793 mutex_exit(SD_MUTEX(un)); 25794 } 25795 return (rval); 25796 } 25797 25798 25799 /* 25800 * Function: sr_ejected() 25801 * 25802 * Description: This routine updates the soft state structure to invalidate the 25803 * geometry information after the media has been ejected or a 25804 * media eject has been detected. 25805 * 25806 * Arguments: un - driver soft state (unit) structure 25807 */ 25808 25809 static void 25810 sr_ejected(struct sd_lun *un) 25811 { 25812 struct sd_errstats *stp; 25813 25814 ASSERT(un != NULL); 25815 ASSERT(mutex_owned(SD_MUTEX(un))); 25816 25817 un->un_f_blockcount_is_valid = FALSE; 25818 un->un_f_tgt_blocksize_is_valid = FALSE; 25819 mutex_exit(SD_MUTEX(un)); 25820 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY); 25821 mutex_enter(SD_MUTEX(un)); 25822 25823 if (un->un_errstats != NULL) { 25824 stp = (struct sd_errstats *)un->un_errstats->ks_data; 25825 stp->sd_capacity.value.ui64 = 0; 25826 } 25827 25828 /* remove "capacity-of-device" properties */ 25829 (void) ddi_prop_remove(DDI_DEV_T_NONE, SD_DEVINFO(un), 25830 "device-nblocks"); 25831 (void) ddi_prop_remove(DDI_DEV_T_NONE, SD_DEVINFO(un), 25832 "device-blksize"); 25833 } 25834 25835 25836 /* 25837 * Function: sr_check_wp() 25838 * 25839 * Description: This routine checks the write protection of a removable 25840 * media disk and hotpluggable devices via the write protect bit of 25841 * the Mode Page Header device specific field. Some devices choke 25842 * on unsupported mode page. In order to workaround this issue, 25843 * this routine has been implemented to use 0x3f mode page(request 25844 * for all pages) for all device types. 25845 * 25846 * Arguments: dev - the device 'dev_t' 25847 * 25848 * Return Code: int indicating if the device is write protected (1) or not (0) 25849 * 25850 * Context: Kernel thread. 25851 * 25852 */ 25853 25854 static int 25855 sr_check_wp(dev_t dev) 25856 { 25857 struct sd_lun *un; 25858 uchar_t device_specific; 25859 uchar_t *sense; 25860 int hdrlen; 25861 int rval = FALSE; 25862 25863 /* 25864 * Note: The return codes for this routine should be reworked to 25865 * properly handle the case of a NULL softstate. 25866 */ 25867 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25868 return (FALSE); 25869 } 25870 25871 if (un->un_f_cfg_is_atapi == TRUE) { 25872 /* 25873 * The mode page contents are not required; set the allocation 25874 * length for the mode page header only 25875 */ 25876 hdrlen = MODE_HEADER_LENGTH_GRP2; 25877 sense = kmem_zalloc(hdrlen, KM_SLEEP); 25878 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 25879 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 25880 goto err_exit; 25881 device_specific = 25882 ((struct mode_header_grp2 *)sense)->device_specific; 25883 } else { 25884 hdrlen = MODE_HEADER_LENGTH; 25885 sense = kmem_zalloc(hdrlen, KM_SLEEP); 25886 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 25887 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 25888 goto err_exit; 25889 device_specific = 25890 ((struct mode_header *)sense)->device_specific; 25891 } 25892 25893 /* 25894 * Write protect mode sense failed; not all disks 25895 * understand this query. Return FALSE assuming that 25896 * these devices are not writable. 25897 */ 25898 if (device_specific & WRITE_PROTECT) { 25899 rval = TRUE; 25900 } 25901 25902 err_exit: 25903 kmem_free(sense, hdrlen); 25904 return (rval); 25905 } 25906 25907 /* 25908 * Function: sr_volume_ctrl() 25909 * 25910 * Description: This routine is the driver entry point for handling CD-ROM 25911 * audio output volume ioctl requests. (CDROMVOLCTRL) 25912 * 25913 * Arguments: dev - the device 'dev_t' 25914 * data - pointer to user audio volume control structure 25915 * flag - this argument is a pass through to ddi_copyxxx() 25916 * directly from the mode argument of ioctl(). 25917 * 25918 * Return Code: the code returned by sd_send_scsi_cmd() 25919 * EFAULT if ddi_copyxxx() fails 25920 * ENXIO if fail ddi_get_soft_state 25921 * EINVAL if data pointer is NULL 25922 * 25923 */ 25924 25925 static int 25926 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 25927 { 25928 struct sd_lun *un; 25929 struct cdrom_volctrl volume; 25930 struct cdrom_volctrl *vol = &volume; 25931 uchar_t *sense_page; 25932 uchar_t *select_page; 25933 uchar_t *sense; 25934 uchar_t *select; 25935 int sense_buflen; 25936 int select_buflen; 25937 int rval; 25938 25939 if (data == NULL) { 25940 return (EINVAL); 25941 } 25942 25943 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 25944 (un->un_state == SD_STATE_OFFLINE)) { 25945 return (ENXIO); 25946 } 25947 25948 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 25949 return (EFAULT); 25950 } 25951 25952 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 25953 struct mode_header_grp2 *sense_mhp; 25954 struct mode_header_grp2 *select_mhp; 25955 int bd_len; 25956 25957 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 25958 select_buflen = MODE_HEADER_LENGTH_GRP2 + 25959 MODEPAGE_AUDIO_CTRL_LEN; 25960 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 25961 select = kmem_zalloc(select_buflen, KM_SLEEP); 25962 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 25963 sense_buflen, MODEPAGE_AUDIO_CTRL, 25964 SD_PATH_STANDARD)) != 0) { 25965 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 25966 "sr_volume_ctrl: Mode Sense Failed\n"); 25967 kmem_free(sense, sense_buflen); 25968 kmem_free(select, select_buflen); 25969 return (rval); 25970 } 25971 sense_mhp = (struct mode_header_grp2 *)sense; 25972 select_mhp = (struct mode_header_grp2 *)select; 25973 bd_len = (sense_mhp->bdesc_length_hi << 8) | 25974 sense_mhp->bdesc_length_lo; 25975 if (bd_len > MODE_BLK_DESC_LENGTH) { 25976 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 25977 "sr_volume_ctrl: Mode Sense returned invalid " 25978 "block descriptor length\n"); 25979 kmem_free(sense, sense_buflen); 25980 kmem_free(select, select_buflen); 25981 return (EIO); 25982 } 25983 sense_page = (uchar_t *) 25984 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 25985 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 25986 select_mhp->length_msb = 0; 25987 select_mhp->length_lsb = 0; 25988 select_mhp->bdesc_length_hi = 0; 25989 select_mhp->bdesc_length_lo = 0; 25990 } else { 25991 struct mode_header *sense_mhp, *select_mhp; 25992 25993 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 25994 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 25995 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 25996 select = kmem_zalloc(select_buflen, KM_SLEEP); 25997 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 25998 sense_buflen, MODEPAGE_AUDIO_CTRL, 25999 SD_PATH_STANDARD)) != 0) { 26000 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26001 "sr_volume_ctrl: Mode Sense Failed\n"); 26002 kmem_free(sense, sense_buflen); 26003 kmem_free(select, select_buflen); 26004 return (rval); 26005 } 26006 sense_mhp = (struct mode_header *)sense; 26007 select_mhp = (struct mode_header *)select; 26008 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 26009 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26010 "sr_volume_ctrl: Mode Sense returned invalid " 26011 "block descriptor length\n"); 26012 kmem_free(sense, sense_buflen); 26013 kmem_free(select, select_buflen); 26014 return (EIO); 26015 } 26016 sense_page = (uchar_t *) 26017 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 26018 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 26019 select_mhp->length = 0; 26020 select_mhp->bdesc_length = 0; 26021 } 26022 /* 26023 * Note: An audio control data structure could be created and overlayed 26024 * on the following in place of the array indexing method implemented. 26025 */ 26026 26027 /* Build the select data for the user volume data */ 26028 select_page[0] = MODEPAGE_AUDIO_CTRL; 26029 select_page[1] = 0xE; 26030 /* Set the immediate bit */ 26031 select_page[2] = 0x04; 26032 /* Zero out reserved fields */ 26033 select_page[3] = 0x00; 26034 select_page[4] = 0x00; 26035 /* Return sense data for fields not to be modified */ 26036 select_page[5] = sense_page[5]; 26037 select_page[6] = sense_page[6]; 26038 select_page[7] = sense_page[7]; 26039 /* Set the user specified volume levels for channel 0 and 1 */ 26040 select_page[8] = 0x01; 26041 select_page[9] = vol->channel0; 26042 select_page[10] = 0x02; 26043 select_page[11] = vol->channel1; 26044 /* Channel 2 and 3 are currently unsupported so return the sense data */ 26045 select_page[12] = sense_page[12]; 26046 select_page[13] = sense_page[13]; 26047 select_page[14] = sense_page[14]; 26048 select_page[15] = sense_page[15]; 26049 26050 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 26051 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 26052 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26053 } else { 26054 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 26055 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 26056 } 26057 26058 kmem_free(sense, sense_buflen); 26059 kmem_free(select, select_buflen); 26060 return (rval); 26061 } 26062 26063 26064 /* 26065 * Function: sr_read_sony_session_offset() 26066 * 26067 * Description: This routine is the driver entry point for handling CD-ROM 26068 * ioctl requests for session offset information. (CDROMREADOFFSET) 26069 * The address of the first track in the last session of a 26070 * multi-session CD-ROM is returned 26071 * 26072 * Note: This routine uses a vendor specific key value in the 26073 * command control field without implementing any vendor check here 26074 * or in the ioctl routine. 26075 * 26076 * Arguments: dev - the device 'dev_t' 26077 * data - pointer to an int to hold the requested address 26078 * flag - this argument is a pass through to ddi_copyxxx() 26079 * directly from the mode argument of ioctl(). 26080 * 26081 * Return Code: the code returned by sd_send_scsi_cmd() 26082 * EFAULT if ddi_copyxxx() fails 26083 * ENXIO if fail ddi_get_soft_state 26084 * EINVAL if data pointer is NULL 26085 */ 26086 26087 static int 26088 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 26089 { 26090 struct sd_lun *un; 26091 struct uscsi_cmd *com; 26092 caddr_t buffer; 26093 char cdb[CDB_GROUP1]; 26094 int session_offset = 0; 26095 int rval; 26096 26097 if (data == NULL) { 26098 return (EINVAL); 26099 } 26100 26101 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 26102 (un->un_state == SD_STATE_OFFLINE)) { 26103 return (ENXIO); 26104 } 26105 26106 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 26107 bzero(cdb, CDB_GROUP1); 26108 cdb[0] = SCMD_READ_TOC; 26109 /* 26110 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 26111 * (4 byte TOC response header + 8 byte response data) 26112 */ 26113 cdb[8] = SONY_SESSION_OFFSET_LEN; 26114 /* Byte 9 is the control byte. A vendor specific value is used */ 26115 cdb[9] = SONY_SESSION_OFFSET_KEY; 26116 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26117 com->uscsi_cdb = cdb; 26118 com->uscsi_cdblen = CDB_GROUP1; 26119 com->uscsi_bufaddr = buffer; 26120 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 26121 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 26122 26123 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE, 26124 SD_PATH_STANDARD); 26125 if (rval != 0) { 26126 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 26127 kmem_free(com, sizeof (*com)); 26128 return (rval); 26129 } 26130 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 26131 session_offset = 26132 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 26133 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 26134 /* 26135 * Offset returned offset in current lbasize block's. Convert to 26136 * 2k block's to return to the user 26137 */ 26138 if (un->un_tgt_blocksize == CDROM_BLK_512) { 26139 session_offset >>= 2; 26140 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 26141 session_offset >>= 1; 26142 } 26143 } 26144 26145 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 26146 rval = EFAULT; 26147 } 26148 26149 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 26150 kmem_free(com, sizeof (*com)); 26151 return (rval); 26152 } 26153 26154 26155 /* 26156 * Function: sd_wm_cache_constructor() 26157 * 26158 * Description: Cache Constructor for the wmap cache for the read/modify/write 26159 * devices. 26160 * 26161 * Arguments: wm - A pointer to the sd_w_map to be initialized. 26162 * un - sd_lun structure for the device. 26163 * flag - the km flags passed to constructor 26164 * 26165 * Return Code: 0 on success. 26166 * -1 on failure. 26167 */ 26168 26169 /*ARGSUSED*/ 26170 static int 26171 sd_wm_cache_constructor(void *wm, void *un, int flags) 26172 { 26173 bzero(wm, sizeof (struct sd_w_map)); 26174 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 26175 return (0); 26176 } 26177 26178 26179 /* 26180 * Function: sd_wm_cache_destructor() 26181 * 26182 * Description: Cache destructor for the wmap cache for the read/modify/write 26183 * devices. 26184 * 26185 * Arguments: wm - A pointer to the sd_w_map to be initialized. 26186 * un - sd_lun structure for the device. 26187 */ 26188 /*ARGSUSED*/ 26189 static void 26190 sd_wm_cache_destructor(void *wm, void *un) 26191 { 26192 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 26193 } 26194 26195 26196 /* 26197 * Function: sd_range_lock() 26198 * 26199 * Description: Lock the range of blocks specified as parameter to ensure 26200 * that read, modify write is atomic and no other i/o writes 26201 * to the same location. The range is specified in terms 26202 * of start and end blocks. Block numbers are the actual 26203 * media block numbers and not system. 26204 * 26205 * Arguments: un - sd_lun structure for the device. 26206 * startb - The starting block number 26207 * endb - The end block number 26208 * typ - type of i/o - simple/read_modify_write 26209 * 26210 * Return Code: wm - pointer to the wmap structure. 26211 * 26212 * Context: This routine can sleep. 26213 */ 26214 26215 static struct sd_w_map * 26216 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 26217 { 26218 struct sd_w_map *wmp = NULL; 26219 struct sd_w_map *sl_wmp = NULL; 26220 struct sd_w_map *tmp_wmp; 26221 wm_state state = SD_WM_CHK_LIST; 26222 26223 26224 ASSERT(un != NULL); 26225 ASSERT(!mutex_owned(SD_MUTEX(un))); 26226 26227 mutex_enter(SD_MUTEX(un)); 26228 26229 while (state != SD_WM_DONE) { 26230 26231 switch (state) { 26232 case SD_WM_CHK_LIST: 26233 /* 26234 * This is the starting state. Check the wmap list 26235 * to see if the range is currently available. 26236 */ 26237 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 26238 /* 26239 * If this is a simple write and no rmw 26240 * i/o is pending then try to lock the 26241 * range as the range should be available. 26242 */ 26243 state = SD_WM_LOCK_RANGE; 26244 } else { 26245 tmp_wmp = sd_get_range(un, startb, endb); 26246 if (tmp_wmp != NULL) { 26247 if ((wmp != NULL) && ONLIST(un, wmp)) { 26248 /* 26249 * Should not keep onlist wmps 26250 * while waiting this macro 26251 * will also do wmp = NULL; 26252 */ 26253 FREE_ONLIST_WMAP(un, wmp); 26254 } 26255 /* 26256 * sl_wmp is the wmap on which wait 26257 * is done, since the tmp_wmp points 26258 * to the inuse wmap, set sl_wmp to 26259 * tmp_wmp and change the state to sleep 26260 */ 26261 sl_wmp = tmp_wmp; 26262 state = SD_WM_WAIT_MAP; 26263 } else { 26264 state = SD_WM_LOCK_RANGE; 26265 } 26266 26267 } 26268 break; 26269 26270 case SD_WM_LOCK_RANGE: 26271 ASSERT(un->un_wm_cache); 26272 /* 26273 * The range need to be locked, try to get a wmap. 26274 * First attempt it with NO_SLEEP, want to avoid a sleep 26275 * if possible as we will have to release the sd mutex 26276 * if we have to sleep. 26277 */ 26278 if (wmp == NULL) 26279 wmp = kmem_cache_alloc(un->un_wm_cache, 26280 KM_NOSLEEP); 26281 if (wmp == NULL) { 26282 mutex_exit(SD_MUTEX(un)); 26283 _NOTE(DATA_READABLE_WITHOUT_LOCK 26284 (sd_lun::un_wm_cache)) 26285 wmp = kmem_cache_alloc(un->un_wm_cache, 26286 KM_SLEEP); 26287 mutex_enter(SD_MUTEX(un)); 26288 /* 26289 * we released the mutex so recheck and go to 26290 * check list state. 26291 */ 26292 state = SD_WM_CHK_LIST; 26293 } else { 26294 /* 26295 * We exit out of state machine since we 26296 * have the wmap. Do the housekeeping first. 26297 * place the wmap on the wmap list if it is not 26298 * on it already and then set the state to done. 26299 */ 26300 wmp->wm_start = startb; 26301 wmp->wm_end = endb; 26302 wmp->wm_flags = typ | SD_WM_BUSY; 26303 if (typ & SD_WTYPE_RMW) { 26304 un->un_rmw_count++; 26305 } 26306 /* 26307 * If not already on the list then link 26308 */ 26309 if (!ONLIST(un, wmp)) { 26310 wmp->wm_next = un->un_wm; 26311 wmp->wm_prev = NULL; 26312 if (wmp->wm_next) 26313 wmp->wm_next->wm_prev = wmp; 26314 un->un_wm = wmp; 26315 } 26316 state = SD_WM_DONE; 26317 } 26318 break; 26319 26320 case SD_WM_WAIT_MAP: 26321 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 26322 /* 26323 * Wait is done on sl_wmp, which is set in the 26324 * check_list state. 26325 */ 26326 sl_wmp->wm_wanted_count++; 26327 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 26328 sl_wmp->wm_wanted_count--; 26329 /* 26330 * We can reuse the memory from the completed sl_wmp 26331 * lock range for our new lock, but only if noone is 26332 * waiting for it. 26333 */ 26334 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 26335 if (sl_wmp->wm_wanted_count == 0) { 26336 if (wmp != NULL) 26337 CHK_N_FREEWMP(un, wmp); 26338 wmp = sl_wmp; 26339 } 26340 sl_wmp = NULL; 26341 /* 26342 * After waking up, need to recheck for availability of 26343 * range. 26344 */ 26345 state = SD_WM_CHK_LIST; 26346 break; 26347 26348 default: 26349 panic("sd_range_lock: " 26350 "Unknown state %d in sd_range_lock", state); 26351 /*NOTREACHED*/ 26352 } /* switch(state) */ 26353 26354 } /* while(state != SD_WM_DONE) */ 26355 26356 mutex_exit(SD_MUTEX(un)); 26357 26358 ASSERT(wmp != NULL); 26359 26360 return (wmp); 26361 } 26362 26363 26364 /* 26365 * Function: sd_get_range() 26366 * 26367 * Description: Find if there any overlapping I/O to this one 26368 * Returns the write-map of 1st such I/O, NULL otherwise. 26369 * 26370 * Arguments: un - sd_lun structure for the device. 26371 * startb - The starting block number 26372 * endb - The end block number 26373 * 26374 * Return Code: wm - pointer to the wmap structure. 26375 */ 26376 26377 static struct sd_w_map * 26378 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 26379 { 26380 struct sd_w_map *wmp; 26381 26382 ASSERT(un != NULL); 26383 26384 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 26385 if (!(wmp->wm_flags & SD_WM_BUSY)) { 26386 continue; 26387 } 26388 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 26389 break; 26390 } 26391 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 26392 break; 26393 } 26394 } 26395 26396 return (wmp); 26397 } 26398 26399 26400 /* 26401 * Function: sd_free_inlist_wmap() 26402 * 26403 * Description: Unlink and free a write map struct. 26404 * 26405 * Arguments: un - sd_lun structure for the device. 26406 * wmp - sd_w_map which needs to be unlinked. 26407 */ 26408 26409 static void 26410 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 26411 { 26412 ASSERT(un != NULL); 26413 26414 if (un->un_wm == wmp) { 26415 un->un_wm = wmp->wm_next; 26416 } else { 26417 wmp->wm_prev->wm_next = wmp->wm_next; 26418 } 26419 26420 if (wmp->wm_next) { 26421 wmp->wm_next->wm_prev = wmp->wm_prev; 26422 } 26423 26424 wmp->wm_next = wmp->wm_prev = NULL; 26425 26426 kmem_cache_free(un->un_wm_cache, wmp); 26427 } 26428 26429 26430 /* 26431 * Function: sd_range_unlock() 26432 * 26433 * Description: Unlock the range locked by wm. 26434 * Free write map if nobody else is waiting on it. 26435 * 26436 * Arguments: un - sd_lun structure for the device. 26437 * wmp - sd_w_map which needs to be unlinked. 26438 */ 26439 26440 static void 26441 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 26442 { 26443 ASSERT(un != NULL); 26444 ASSERT(wm != NULL); 26445 ASSERT(!mutex_owned(SD_MUTEX(un))); 26446 26447 mutex_enter(SD_MUTEX(un)); 26448 26449 if (wm->wm_flags & SD_WTYPE_RMW) { 26450 un->un_rmw_count--; 26451 } 26452 26453 if (wm->wm_wanted_count) { 26454 wm->wm_flags = 0; 26455 /* 26456 * Broadcast that the wmap is available now. 26457 */ 26458 cv_broadcast(&wm->wm_avail); 26459 } else { 26460 /* 26461 * If no one is waiting on the map, it should be free'ed. 26462 */ 26463 sd_free_inlist_wmap(un, wm); 26464 } 26465 26466 mutex_exit(SD_MUTEX(un)); 26467 } 26468 26469 26470 /* 26471 * Function: sd_read_modify_write_task 26472 * 26473 * Description: Called from a taskq thread to initiate the write phase of 26474 * a read-modify-write request. This is used for targets where 26475 * un->un_sys_blocksize != un->un_tgt_blocksize. 26476 * 26477 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 26478 * 26479 * Context: Called under taskq thread context. 26480 */ 26481 26482 static void 26483 sd_read_modify_write_task(void *arg) 26484 { 26485 struct sd_mapblocksize_info *bsp; 26486 struct buf *bp; 26487 struct sd_xbuf *xp; 26488 struct sd_lun *un; 26489 26490 bp = arg; /* The bp is given in arg */ 26491 ASSERT(bp != NULL); 26492 26493 /* Get the pointer to the layer-private data struct */ 26494 xp = SD_GET_XBUF(bp); 26495 ASSERT(xp != NULL); 26496 bsp = xp->xb_private; 26497 ASSERT(bsp != NULL); 26498 26499 un = SD_GET_UN(bp); 26500 ASSERT(un != NULL); 26501 ASSERT(!mutex_owned(SD_MUTEX(un))); 26502 26503 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26504 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 26505 26506 /* 26507 * This is the write phase of a read-modify-write request, called 26508 * under the context of a taskq thread in response to the completion 26509 * of the read portion of the rmw request completing under interrupt 26510 * context. The write request must be sent from here down the iostart 26511 * chain as if it were being sent from sd_mapblocksize_iostart(), so 26512 * we use the layer index saved in the layer-private data area. 26513 */ 26514 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 26515 26516 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 26517 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 26518 } 26519 26520 26521 /* 26522 * Function: sddump_do_read_of_rmw() 26523 * 26524 * Description: This routine will be called from sddump, If sddump is called 26525 * with an I/O which not aligned on device blocksize boundary 26526 * then the write has to be converted to read-modify-write. 26527 * Do the read part here in order to keep sddump simple. 26528 * Note - That the sd_mutex is held across the call to this 26529 * routine. 26530 * 26531 * Arguments: un - sd_lun 26532 * blkno - block number in terms of media block size. 26533 * nblk - number of blocks. 26534 * bpp - pointer to pointer to the buf structure. On return 26535 * from this function, *bpp points to the valid buffer 26536 * to which the write has to be done. 26537 * 26538 * Return Code: 0 for success or errno-type return code 26539 */ 26540 26541 static int 26542 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 26543 struct buf **bpp) 26544 { 26545 int err; 26546 int i; 26547 int rval; 26548 struct buf *bp; 26549 struct scsi_pkt *pkt = NULL; 26550 uint32_t target_blocksize; 26551 26552 ASSERT(un != NULL); 26553 ASSERT(mutex_owned(SD_MUTEX(un))); 26554 26555 target_blocksize = un->un_tgt_blocksize; 26556 26557 mutex_exit(SD_MUTEX(un)); 26558 26559 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 26560 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 26561 if (bp == NULL) { 26562 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26563 "no resources for dumping; giving up"); 26564 err = ENOMEM; 26565 goto done; 26566 } 26567 26568 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 26569 blkno, nblk); 26570 if (rval != 0) { 26571 scsi_free_consistent_buf(bp); 26572 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26573 "no resources for dumping; giving up"); 26574 err = ENOMEM; 26575 goto done; 26576 } 26577 26578 pkt->pkt_flags |= FLAG_NOINTR; 26579 26580 err = EIO; 26581 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26582 26583 /* 26584 * Scsi_poll returns 0 (success) if the command completes and 26585 * the status block is STATUS_GOOD. We should only check 26586 * errors if this condition is not true. Even then we should 26587 * send our own request sense packet only if we have a check 26588 * condition and auto request sense has not been performed by 26589 * the hba. 26590 */ 26591 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 26592 26593 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 26594 err = 0; 26595 break; 26596 } 26597 26598 /* 26599 * Check CMD_DEV_GONE 1st, give up if device is gone, 26600 * no need to read RQS data. 26601 */ 26602 if (pkt->pkt_reason == CMD_DEV_GONE) { 26603 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 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