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 2006 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/file.h> 41 #include <sys/stat.h> 42 #include <sys/kstat.h> 43 #include <sys/vtrace.h> 44 #include <sys/note.h> 45 #include <sys/thread.h> 46 #include <sys/proc.h> 47 #include <sys/efi_partition.h> 48 #include <sys/var.h> 49 #include <sys/aio_req.h> 50 51 #ifdef __lock_lint 52 #define _LP64 53 #define __amd64 54 #endif 55 56 #if (defined(__fibre)) 57 /* Note: is there a leadville version of the following? */ 58 #include <sys/fc4/fcal_linkapp.h> 59 #endif 60 #include <sys/taskq.h> 61 #include <sys/uuid.h> 62 #include <sys/byteorder.h> 63 #include <sys/sdt.h> 64 65 #include "sd_xbuf.h" 66 67 #include <sys/scsi/targets/sddef.h> 68 69 70 /* 71 * Loadable module info. 72 */ 73 #if (defined(__fibre)) 74 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver %I%" 75 char _depends_on[] = "misc/scsi drv/fcp"; 76 #else 77 #define SD_MODULE_NAME "SCSI Disk Driver %I%" 78 char _depends_on[] = "misc/scsi"; 79 #endif 80 81 /* 82 * Define the interconnect type, to allow the driver to distinguish 83 * between parallel SCSI (sd) and fibre channel (ssd) behaviors. 84 * 85 * This is really for backward compatability. In the future, the driver 86 * should actually check the "interconnect-type" property as reported by 87 * the HBA; however at present this property is not defined by all HBAs, 88 * so we will use this #define (1) to permit the driver to run in 89 * backward-compatability mode; and (2) to print a notification message 90 * if an FC HBA does not support the "interconnect-type" property. The 91 * behavior of the driver will be to assume parallel SCSI behaviors unless 92 * the "interconnect-type" property is defined by the HBA **AND** has a 93 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or 94 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre 95 * Channel behaviors (as per the old ssd). (Note that the 96 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and 97 * will result in the driver assuming parallel SCSI behaviors.) 98 * 99 * (see common/sys/scsi/impl/services.h) 100 * 101 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default 102 * since some FC HBAs may already support that, and there is some code in 103 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the 104 * default would confuse that code, and besides things should work fine 105 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the 106 * "interconnect_type" property. 107 * 108 * Notes for off-by-1 workaround: 109 * ----------------------------- 110 * 111 * SCSI READ_CAPACITY command returns the LBA number of the 112 * last logical block, but sd once treated this number as 113 * disks' capacity on x86 platform. And LBAs are addressed 114 * based 0. So the last block was lost on x86 platform. 115 * 116 * Now, we remove this workaround. In order for present sd 117 * driver to work with disks which are labeled/partitioned 118 * via previous sd, we add workaround as follows: 119 * 120 * 1) Locate backup EFI label: sd searchs the next to last 121 * block for backup EFI label if it can't find it on the 122 * last block; 123 * 2) Calculate geometry: refer to sd_convert_geometry(), If 124 * capacity increasing by 1 causes disks' capacity to cross 125 * over the limits in table CHS_values, geometry info will 126 * change. This will raise an issue: In case that primary 127 * VTOC label is destroyed, format commandline can restore 128 * it via backup VTOC labels. And format locates backup VTOC 129 * labels by use of geometry from sd driver. So changing 130 * geometry will prevent format from finding backup VTOC 131 * labels. To eliminate this side effect for compatibility, 132 * sd uses (capacity -1) to calculate geometry; 133 * 3) 1TB disks: VTOC uses 32-bit signed int, thus sd doesn't 134 * support VTOC for a disk which has more than DK_MAX_BLOCKS 135 * LBAs. However, for exactly 1TB disk, it was treated as 136 * (1T - 512)B in the past, and could have VTOC. To overcome 137 * this, if an exactly 1TB disk has solaris fdisk partition, 138 * it will be allowed to work with sd. 139 */ 140 #if (defined(__fibre)) 141 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 142 #else 143 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 144 #endif 145 146 /* 147 * The name of the driver, established from the module name in _init. 148 */ 149 static char *sd_label = NULL; 150 151 /* 152 * Driver name is unfortunately prefixed on some driver.conf properties. 153 */ 154 #if (defined(__fibre)) 155 #define sd_max_xfer_size ssd_max_xfer_size 156 #define sd_config_list ssd_config_list 157 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 158 static char *sd_config_list = "ssd-config-list"; 159 #else 160 static char *sd_max_xfer_size = "sd_max_xfer_size"; 161 static char *sd_config_list = "sd-config-list"; 162 #endif 163 164 /* 165 * Driver global variables 166 */ 167 168 #if (defined(__fibre)) 169 /* 170 * These #defines are to avoid namespace collisions that occur because this 171 * code is currently used to compile two seperate driver modules: sd and ssd. 172 * All global variables need to be treated this way (even if declared static) 173 * in order to allow the debugger to resolve the names properly. 174 * It is anticipated that in the near future the ssd module will be obsoleted, 175 * at which time this namespace issue should go away. 176 */ 177 #define sd_state ssd_state 178 #define sd_io_time ssd_io_time 179 #define sd_failfast_enable ssd_failfast_enable 180 #define sd_ua_retry_count ssd_ua_retry_count 181 #define sd_report_pfa ssd_report_pfa 182 #define sd_max_throttle ssd_max_throttle 183 #define sd_min_throttle ssd_min_throttle 184 #define sd_rot_delay ssd_rot_delay 185 186 #define sd_retry_on_reservation_conflict \ 187 ssd_retry_on_reservation_conflict 188 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 189 #define sd_resv_conflict_name ssd_resv_conflict_name 190 191 #define sd_component_mask ssd_component_mask 192 #define sd_level_mask ssd_level_mask 193 #define sd_debug_un ssd_debug_un 194 #define sd_error_level ssd_error_level 195 196 #define sd_xbuf_active_limit ssd_xbuf_active_limit 197 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 198 199 #define sd_tr ssd_tr 200 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 201 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 202 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 203 #define sd_check_media_time ssd_check_media_time 204 #define sd_wait_cmds_complete ssd_wait_cmds_complete 205 #define sd_label_mutex ssd_label_mutex 206 #define sd_detach_mutex ssd_detach_mutex 207 #define sd_log_buf ssd_log_buf 208 #define sd_log_mutex ssd_log_mutex 209 210 #define sd_disk_table ssd_disk_table 211 #define sd_disk_table_size ssd_disk_table_size 212 #define sd_sense_mutex ssd_sense_mutex 213 #define sd_cdbtab ssd_cdbtab 214 215 #define sd_cb_ops ssd_cb_ops 216 #define sd_ops ssd_ops 217 #define sd_additional_codes ssd_additional_codes 218 219 #define sd_minor_data ssd_minor_data 220 #define sd_minor_data_efi ssd_minor_data_efi 221 222 #define sd_tq ssd_tq 223 #define sd_wmr_tq ssd_wmr_tq 224 #define sd_taskq_name ssd_taskq_name 225 #define sd_wmr_taskq_name ssd_wmr_taskq_name 226 #define sd_taskq_minalloc ssd_taskq_minalloc 227 #define sd_taskq_maxalloc ssd_taskq_maxalloc 228 229 #define sd_dump_format_string ssd_dump_format_string 230 231 #define sd_iostart_chain ssd_iostart_chain 232 #define sd_iodone_chain ssd_iodone_chain 233 234 #define sd_pm_idletime ssd_pm_idletime 235 236 #define sd_force_pm_supported ssd_force_pm_supported 237 238 #define sd_dtype_optical_bind ssd_dtype_optical_bind 239 240 #endif 241 242 243 #ifdef SDDEBUG 244 int sd_force_pm_supported = 0; 245 #endif /* SDDEBUG */ 246 247 void *sd_state = NULL; 248 int sd_io_time = SD_IO_TIME; 249 int sd_failfast_enable = 1; 250 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 251 int sd_report_pfa = 1; 252 int sd_max_throttle = SD_MAX_THROTTLE; 253 int sd_min_throttle = SD_MIN_THROTTLE; 254 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 255 int sd_qfull_throttle_enable = TRUE; 256 257 int sd_retry_on_reservation_conflict = 1; 258 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 259 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 260 261 static int sd_dtype_optical_bind = -1; 262 263 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 264 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 265 266 /* 267 * Global data for debug logging. To enable debug printing, sd_component_mask 268 * and sd_level_mask should be set to the desired bit patterns as outlined in 269 * sddef.h. 270 */ 271 uint_t sd_component_mask = 0x0; 272 uint_t sd_level_mask = 0x0; 273 struct sd_lun *sd_debug_un = NULL; 274 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 275 276 /* Note: these may go away in the future... */ 277 static uint32_t sd_xbuf_active_limit = 512; 278 static uint32_t sd_xbuf_reserve_limit = 16; 279 280 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 281 282 /* 283 * Timer value used to reset the throttle after it has been reduced 284 * (typically in response to TRAN_BUSY or STATUS_QFULL) 285 */ 286 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 287 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 288 289 /* 290 * Interval value associated with the media change scsi watch. 291 */ 292 static int sd_check_media_time = 3000000; 293 294 /* 295 * Wait value used for in progress operations during a DDI_SUSPEND 296 */ 297 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 298 299 /* 300 * sd_label_mutex protects a static buffer used in the disk label 301 * component of the driver 302 */ 303 static kmutex_t sd_label_mutex; 304 305 /* 306 * sd_detach_mutex protects un_layer_count, un_detach_count, and 307 * un_opens_in_progress in the sd_lun structure. 308 */ 309 static kmutex_t sd_detach_mutex; 310 311 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 312 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 313 314 /* 315 * Global buffer and mutex for debug logging 316 */ 317 static char sd_log_buf[1024]; 318 static kmutex_t sd_log_mutex; 319 320 /* 321 * Structs and globals for recording attached lun information. 322 * This maintains a chain. Each node in the chain represents a SCSI controller. 323 * The structure records the number of luns attached to each target connected 324 * with the controller. 325 * For parallel scsi device only. 326 */ 327 struct sd_scsi_hba_tgt_lun { 328 struct sd_scsi_hba_tgt_lun *next; 329 dev_info_t *pdip; 330 int nlun[NTARGETS_WIDE]; 331 }; 332 333 /* 334 * Flag to indicate the lun is attached or detached 335 */ 336 #define SD_SCSI_LUN_ATTACH 0 337 #define SD_SCSI_LUN_DETACH 1 338 339 static kmutex_t sd_scsi_target_lun_mutex; 340 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 341 342 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 343 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 344 345 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 346 sd_scsi_target_lun_head)) 347 348 /* 349 * "Smart" Probe Caching structs, globals, #defines, etc. 350 * For parallel scsi and non-self-identify device only. 351 */ 352 353 /* 354 * The following resources and routines are implemented to support 355 * "smart" probing, which caches the scsi_probe() results in an array, 356 * in order to help avoid long probe times. 357 */ 358 struct sd_scsi_probe_cache { 359 struct sd_scsi_probe_cache *next; 360 dev_info_t *pdip; 361 int cache[NTARGETS_WIDE]; 362 }; 363 364 static kmutex_t sd_scsi_probe_cache_mutex; 365 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 366 367 /* 368 * Really we only need protection on the head of the linked list, but 369 * better safe than sorry. 370 */ 371 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 372 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 373 374 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 375 sd_scsi_probe_cache_head)) 376 377 378 /* 379 * Vendor specific data name property declarations 380 */ 381 382 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 383 384 static sd_tunables seagate_properties = { 385 SEAGATE_THROTTLE_VALUE, 386 0, 387 0, 388 0, 389 0, 390 0, 391 0, 392 0, 393 0 394 }; 395 396 397 static sd_tunables fujitsu_properties = { 398 FUJITSU_THROTTLE_VALUE, 399 0, 400 0, 401 0, 402 0, 403 0, 404 0, 405 0, 406 0 407 }; 408 409 static sd_tunables ibm_properties = { 410 IBM_THROTTLE_VALUE, 411 0, 412 0, 413 0, 414 0, 415 0, 416 0, 417 0, 418 0 419 }; 420 421 static sd_tunables purple_properties = { 422 PURPLE_THROTTLE_VALUE, 423 0, 424 0, 425 PURPLE_BUSY_RETRIES, 426 PURPLE_RESET_RETRY_COUNT, 427 PURPLE_RESERVE_RELEASE_TIME, 428 0, 429 0, 430 0 431 }; 432 433 static sd_tunables sve_properties = { 434 SVE_THROTTLE_VALUE, 435 0, 436 0, 437 SVE_BUSY_RETRIES, 438 SVE_RESET_RETRY_COUNT, 439 SVE_RESERVE_RELEASE_TIME, 440 SVE_MIN_THROTTLE_VALUE, 441 SVE_DISKSORT_DISABLED_FLAG, 442 0 443 }; 444 445 static sd_tunables maserati_properties = { 446 0, 447 0, 448 0, 449 0, 450 0, 451 0, 452 0, 453 MASERATI_DISKSORT_DISABLED_FLAG, 454 MASERATI_LUN_RESET_ENABLED_FLAG 455 }; 456 457 static sd_tunables pirus_properties = { 458 PIRUS_THROTTLE_VALUE, 459 0, 460 PIRUS_NRR_COUNT, 461 PIRUS_BUSY_RETRIES, 462 PIRUS_RESET_RETRY_COUNT, 463 0, 464 PIRUS_MIN_THROTTLE_VALUE, 465 PIRUS_DISKSORT_DISABLED_FLAG, 466 PIRUS_LUN_RESET_ENABLED_FLAG 467 }; 468 469 #endif 470 471 #if (defined(__sparc) && !defined(__fibre)) || \ 472 (defined(__i386) || defined(__amd64)) 473 474 475 static sd_tunables elite_properties = { 476 ELITE_THROTTLE_VALUE, 477 0, 478 0, 479 0, 480 0, 481 0, 482 0, 483 0, 484 0 485 }; 486 487 static sd_tunables st31200n_properties = { 488 ST31200N_THROTTLE_VALUE, 489 0, 490 0, 491 0, 492 0, 493 0, 494 0, 495 0, 496 0 497 }; 498 499 #endif /* Fibre or not */ 500 501 static sd_tunables lsi_properties_scsi = { 502 LSI_THROTTLE_VALUE, 503 0, 504 LSI_NOTREADY_RETRIES, 505 0, 506 0, 507 0, 508 0, 509 0, 510 0 511 }; 512 513 static sd_tunables symbios_properties = { 514 SYMBIOS_THROTTLE_VALUE, 515 0, 516 SYMBIOS_NOTREADY_RETRIES, 517 0, 518 0, 519 0, 520 0, 521 0, 522 0 523 }; 524 525 static sd_tunables lsi_properties = { 526 0, 527 0, 528 LSI_NOTREADY_RETRIES, 529 0, 530 0, 531 0, 532 0, 533 0, 534 0 535 }; 536 537 static sd_tunables lsi_oem_properties = { 538 0, 539 0, 540 LSI_OEM_NOTREADY_RETRIES, 541 0, 542 0, 543 0, 544 0, 545 0, 546 0 547 }; 548 549 550 551 #if (defined(SD_PROP_TST)) 552 553 #define SD_TST_CTYPE_VAL CTYPE_CDROM 554 #define SD_TST_THROTTLE_VAL 16 555 #define SD_TST_NOTREADY_VAL 12 556 #define SD_TST_BUSY_VAL 60 557 #define SD_TST_RST_RETRY_VAL 36 558 #define SD_TST_RSV_REL_TIME 60 559 560 static sd_tunables tst_properties = { 561 SD_TST_THROTTLE_VAL, 562 SD_TST_CTYPE_VAL, 563 SD_TST_NOTREADY_VAL, 564 SD_TST_BUSY_VAL, 565 SD_TST_RST_RETRY_VAL, 566 SD_TST_RSV_REL_TIME, 567 0, 568 0, 569 0 570 }; 571 #endif 572 573 /* This is similiar to the ANSI toupper implementation */ 574 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 575 576 /* 577 * Static Driver Configuration Table 578 * 579 * This is the table of disks which need throttle adjustment (or, perhaps 580 * something else as defined by the flags at a future time.) device_id 581 * is a string consisting of concatenated vid (vendor), pid (product/model) 582 * and revision strings as defined in the scsi_inquiry structure. Offsets of 583 * the parts of the string are as defined by the sizes in the scsi_inquiry 584 * structure. Device type is searched as far as the device_id string is 585 * defined. Flags defines which values are to be set in the driver from the 586 * properties list. 587 * 588 * Entries below which begin and end with a "*" are a special case. 589 * These do not have a specific vendor, and the string which follows 590 * can appear anywhere in the 16 byte PID portion of the inquiry data. 591 * 592 * Entries below which begin and end with a " " (blank) are a special 593 * case. The comparison function will treat multiple consecutive blanks 594 * as equivalent to a single blank. For example, this causes a 595 * sd_disk_table entry of " NEC CDROM " to match a device's id string 596 * of "NEC CDROM". 597 * 598 * Note: The MD21 controller type has been obsoleted. 599 * ST318202F is a Legacy device 600 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 601 * made with an FC connection. The entries here are a legacy. 602 */ 603 static sd_disk_config_t sd_disk_table[] = { 604 #if defined(__fibre) || defined(__i386) || defined(__amd64) 605 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 606 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 607 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 608 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 609 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 610 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 611 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 612 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 613 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 614 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 615 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 616 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 617 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 618 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 619 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 620 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 621 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 622 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 623 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 624 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 625 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 626 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 627 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 628 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 629 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 630 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 631 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 632 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 633 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 634 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 635 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 636 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 637 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 638 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 639 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 640 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 641 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 642 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 643 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 644 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 645 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 648 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 649 { "SUN T3", SD_CONF_BSET_THROTTLE | 650 SD_CONF_BSET_BSY_RETRY_COUNT| 651 SD_CONF_BSET_RST_RETRIES| 652 SD_CONF_BSET_RSV_REL_TIME, 653 &purple_properties }, 654 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 655 SD_CONF_BSET_BSY_RETRY_COUNT| 656 SD_CONF_BSET_RST_RETRIES| 657 SD_CONF_BSET_RSV_REL_TIME| 658 SD_CONF_BSET_MIN_THROTTLE| 659 SD_CONF_BSET_DISKSORT_DISABLED, 660 &sve_properties }, 661 { "SUN T4", SD_CONF_BSET_THROTTLE | 662 SD_CONF_BSET_BSY_RETRY_COUNT| 663 SD_CONF_BSET_RST_RETRIES| 664 SD_CONF_BSET_RSV_REL_TIME, 665 &purple_properties }, 666 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 667 SD_CONF_BSET_LUN_RESET_ENABLED, 668 &maserati_properties }, 669 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 670 SD_CONF_BSET_NRR_COUNT| 671 SD_CONF_BSET_BSY_RETRY_COUNT| 672 SD_CONF_BSET_RST_RETRIES| 673 SD_CONF_BSET_MIN_THROTTLE| 674 SD_CONF_BSET_DISKSORT_DISABLED| 675 SD_CONF_BSET_LUN_RESET_ENABLED, 676 &pirus_properties }, 677 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 678 SD_CONF_BSET_NRR_COUNT| 679 SD_CONF_BSET_BSY_RETRY_COUNT| 680 SD_CONF_BSET_RST_RETRIES| 681 SD_CONF_BSET_MIN_THROTTLE| 682 SD_CONF_BSET_DISKSORT_DISABLED| 683 SD_CONF_BSET_LUN_RESET_ENABLED, 684 &pirus_properties }, 685 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 686 SD_CONF_BSET_NRR_COUNT| 687 SD_CONF_BSET_BSY_RETRY_COUNT| 688 SD_CONF_BSET_RST_RETRIES| 689 SD_CONF_BSET_MIN_THROTTLE| 690 SD_CONF_BSET_DISKSORT_DISABLED| 691 SD_CONF_BSET_LUN_RESET_ENABLED, 692 &pirus_properties }, 693 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 694 SD_CONF_BSET_NRR_COUNT| 695 SD_CONF_BSET_BSY_RETRY_COUNT| 696 SD_CONF_BSET_RST_RETRIES| 697 SD_CONF_BSET_MIN_THROTTLE| 698 SD_CONF_BSET_DISKSORT_DISABLED| 699 SD_CONF_BSET_LUN_RESET_ENABLED, 700 &pirus_properties }, 701 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 702 SD_CONF_BSET_NRR_COUNT| 703 SD_CONF_BSET_BSY_RETRY_COUNT| 704 SD_CONF_BSET_RST_RETRIES| 705 SD_CONF_BSET_MIN_THROTTLE| 706 SD_CONF_BSET_DISKSORT_DISABLED| 707 SD_CONF_BSET_LUN_RESET_ENABLED, 708 &pirus_properties }, 709 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 710 SD_CONF_BSET_NRR_COUNT| 711 SD_CONF_BSET_BSY_RETRY_COUNT| 712 SD_CONF_BSET_RST_RETRIES| 713 SD_CONF_BSET_MIN_THROTTLE| 714 SD_CONF_BSET_DISKSORT_DISABLED| 715 SD_CONF_BSET_LUN_RESET_ENABLED, 716 &pirus_properties }, 717 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 718 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 719 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 720 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 721 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 722 #endif /* fibre or NON-sparc platforms */ 723 #if ((defined(__sparc) && !defined(__fibre)) ||\ 724 (defined(__i386) || defined(__amd64))) 725 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 726 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 727 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 728 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 729 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 730 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 731 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 732 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 733 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 734 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 735 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 736 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 737 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 738 &symbios_properties }, 739 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 740 &lsi_properties_scsi }, 741 #if defined(__i386) || defined(__amd64) 742 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 743 | SD_CONF_BSET_READSUB_BCD 744 | SD_CONF_BSET_READ_TOC_ADDR_BCD 745 | SD_CONF_BSET_NO_READ_HEADER 746 | SD_CONF_BSET_READ_CD_XD4), NULL }, 747 748 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 749 | SD_CONF_BSET_READSUB_BCD 750 | SD_CONF_BSET_READ_TOC_ADDR_BCD 751 | SD_CONF_BSET_NO_READ_HEADER 752 | SD_CONF_BSET_READ_CD_XD4), NULL }, 753 #endif /* __i386 || __amd64 */ 754 #endif /* sparc NON-fibre or NON-sparc platforms */ 755 756 #if (defined(SD_PROP_TST)) 757 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 758 | SD_CONF_BSET_CTYPE 759 | SD_CONF_BSET_NRR_COUNT 760 | SD_CONF_BSET_FAB_DEVID 761 | SD_CONF_BSET_NOCACHE 762 | SD_CONF_BSET_BSY_RETRY_COUNT 763 | SD_CONF_BSET_PLAYMSF_BCD 764 | SD_CONF_BSET_READSUB_BCD 765 | SD_CONF_BSET_READ_TOC_TRK_BCD 766 | SD_CONF_BSET_READ_TOC_ADDR_BCD 767 | SD_CONF_BSET_NO_READ_HEADER 768 | SD_CONF_BSET_READ_CD_XD4 769 | SD_CONF_BSET_RST_RETRIES 770 | SD_CONF_BSET_RSV_REL_TIME 771 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 772 #endif 773 }; 774 775 static const int sd_disk_table_size = 776 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 777 778 779 /* 780 * Return codes of sd_uselabel(). 781 */ 782 #define SD_LABEL_IS_VALID 0 783 #define SD_LABEL_IS_INVALID 1 784 785 #define SD_INTERCONNECT_PARALLEL 0 786 #define SD_INTERCONNECT_FABRIC 1 787 #define SD_INTERCONNECT_FIBRE 2 788 #define SD_INTERCONNECT_SSA 3 789 #define SD_INTERCONNECT_SATA 4 790 #define SD_IS_PARALLEL_SCSI(un) \ 791 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 792 #define SD_IS_SERIAL(un) \ 793 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 794 795 /* 796 * Definitions used by device id registration routines 797 */ 798 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 799 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 800 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 801 #define WD_NODE 7 /* the whole disk minor */ 802 803 static kmutex_t sd_sense_mutex = {0}; 804 805 /* 806 * Macros for updates of the driver state 807 */ 808 #define New_state(un, s) \ 809 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 810 #define Restore_state(un) \ 811 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 812 813 static struct sd_cdbinfo sd_cdbtab[] = { 814 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 815 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 816 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 817 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 818 }; 819 820 /* 821 * Specifies the number of seconds that must have elapsed since the last 822 * cmd. has completed for a device to be declared idle to the PM framework. 823 */ 824 static int sd_pm_idletime = 1; 825 826 /* 827 * Internal function prototypes 828 */ 829 830 #if (defined(__fibre)) 831 /* 832 * These #defines are to avoid namespace collisions that occur because this 833 * code is currently used to compile two seperate driver modules: sd and ssd. 834 * All function names need to be treated this way (even if declared static) 835 * in order to allow the debugger to resolve the names properly. 836 * It is anticipated that in the near future the ssd module will be obsoleted, 837 * at which time this ugliness should go away. 838 */ 839 #define sd_log_trace ssd_log_trace 840 #define sd_log_info ssd_log_info 841 #define sd_log_err ssd_log_err 842 #define sdprobe ssdprobe 843 #define sdinfo ssdinfo 844 #define sd_prop_op ssd_prop_op 845 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 846 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 847 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 848 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 849 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 850 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 851 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 852 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 853 #define sd_spin_up_unit ssd_spin_up_unit 854 #define sd_enable_descr_sense ssd_enable_descr_sense 855 #define sd_reenable_dsense_task ssd_reenable_dsense_task 856 #define sd_set_mmc_caps ssd_set_mmc_caps 857 #define sd_read_unit_properties ssd_read_unit_properties 858 #define sd_process_sdconf_file ssd_process_sdconf_file 859 #define sd_process_sdconf_table ssd_process_sdconf_table 860 #define sd_sdconf_id_match ssd_sdconf_id_match 861 #define sd_blank_cmp ssd_blank_cmp 862 #define sd_chk_vers1_data ssd_chk_vers1_data 863 #define sd_set_vers1_properties ssd_set_vers1_properties 864 #define sd_validate_geometry ssd_validate_geometry 865 866 #if defined(_SUNOS_VTOC_16) 867 #define sd_convert_geometry ssd_convert_geometry 868 #endif 869 870 #define sd_resync_geom_caches ssd_resync_geom_caches 871 #define sd_read_fdisk ssd_read_fdisk 872 #define sd_get_physical_geometry ssd_get_physical_geometry 873 #define sd_get_virtual_geometry ssd_get_virtual_geometry 874 #define sd_update_block_info ssd_update_block_info 875 #define sd_swap_efi_gpt ssd_swap_efi_gpt 876 #define sd_swap_efi_gpe ssd_swap_efi_gpe 877 #define sd_validate_efi ssd_validate_efi 878 #define sd_use_efi ssd_use_efi 879 #define sd_uselabel ssd_uselabel 880 #define sd_build_default_label ssd_build_default_label 881 #define sd_has_max_chs_vals ssd_has_max_chs_vals 882 #define sd_inq_fill ssd_inq_fill 883 #define sd_register_devid ssd_register_devid 884 #define sd_get_devid_block ssd_get_devid_block 885 #define sd_get_devid ssd_get_devid 886 #define sd_create_devid ssd_create_devid 887 #define sd_write_deviceid ssd_write_deviceid 888 #define sd_check_vpd_page_support ssd_check_vpd_page_support 889 #define sd_setup_pm ssd_setup_pm 890 #define sd_create_pm_components ssd_create_pm_components 891 #define sd_ddi_suspend ssd_ddi_suspend 892 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 893 #define sd_ddi_resume ssd_ddi_resume 894 #define sd_ddi_pm_resume ssd_ddi_pm_resume 895 #define sdpower ssdpower 896 #define sdattach ssdattach 897 #define sddetach ssddetach 898 #define sd_unit_attach ssd_unit_attach 899 #define sd_unit_detach ssd_unit_detach 900 #define sd_set_unit_attributes ssd_set_unit_attributes 901 #define sd_create_minor_nodes ssd_create_minor_nodes 902 #define sd_create_errstats ssd_create_errstats 903 #define sd_set_errstats ssd_set_errstats 904 #define sd_set_pstats ssd_set_pstats 905 #define sddump ssddump 906 #define sd_scsi_poll ssd_scsi_poll 907 #define sd_send_polled_RQS ssd_send_polled_RQS 908 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 909 #define sd_init_event_callbacks ssd_init_event_callbacks 910 #define sd_event_callback ssd_event_callback 911 #define sd_cache_control ssd_cache_control 912 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 913 #define sd_make_device ssd_make_device 914 #define sdopen ssdopen 915 #define sdclose ssdclose 916 #define sd_ready_and_valid ssd_ready_and_valid 917 #define sdmin ssdmin 918 #define sdread ssdread 919 #define sdwrite ssdwrite 920 #define sdaread ssdaread 921 #define sdawrite ssdawrite 922 #define sdstrategy ssdstrategy 923 #define sdioctl ssdioctl 924 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 925 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 926 #define sd_checksum_iostart ssd_checksum_iostart 927 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 928 #define sd_pm_iostart ssd_pm_iostart 929 #define sd_core_iostart ssd_core_iostart 930 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 931 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 932 #define sd_checksum_iodone ssd_checksum_iodone 933 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 934 #define sd_pm_iodone ssd_pm_iodone 935 #define sd_initpkt_for_buf ssd_initpkt_for_buf 936 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 937 #define sd_setup_rw_pkt ssd_setup_rw_pkt 938 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 939 #define sd_buf_iodone ssd_buf_iodone 940 #define sd_uscsi_strategy ssd_uscsi_strategy 941 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 942 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 943 #define sd_uscsi_iodone ssd_uscsi_iodone 944 #define sd_xbuf_strategy ssd_xbuf_strategy 945 #define sd_xbuf_init ssd_xbuf_init 946 #define sd_pm_entry ssd_pm_entry 947 #define sd_pm_exit ssd_pm_exit 948 949 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 950 #define sd_pm_timeout_handler ssd_pm_timeout_handler 951 952 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 953 #define sdintr ssdintr 954 #define sd_start_cmds ssd_start_cmds 955 #define sd_send_scsi_cmd ssd_send_scsi_cmd 956 #define sd_bioclone_alloc ssd_bioclone_alloc 957 #define sd_bioclone_free ssd_bioclone_free 958 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 959 #define sd_shadow_buf_free ssd_shadow_buf_free 960 #define sd_print_transport_rejected_message \ 961 ssd_print_transport_rejected_message 962 #define sd_retry_command ssd_retry_command 963 #define sd_set_retry_bp ssd_set_retry_bp 964 #define sd_send_request_sense_command ssd_send_request_sense_command 965 #define sd_start_retry_command ssd_start_retry_command 966 #define sd_start_direct_priority_command \ 967 ssd_start_direct_priority_command 968 #define sd_return_failed_command ssd_return_failed_command 969 #define sd_return_failed_command_no_restart \ 970 ssd_return_failed_command_no_restart 971 #define sd_return_command ssd_return_command 972 #define sd_sync_with_callback ssd_sync_with_callback 973 #define sdrunout ssdrunout 974 #define sd_mark_rqs_busy ssd_mark_rqs_busy 975 #define sd_mark_rqs_idle ssd_mark_rqs_idle 976 #define sd_reduce_throttle ssd_reduce_throttle 977 #define sd_restore_throttle ssd_restore_throttle 978 #define sd_print_incomplete_msg ssd_print_incomplete_msg 979 #define sd_init_cdb_limits ssd_init_cdb_limits 980 #define sd_pkt_status_good ssd_pkt_status_good 981 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 982 #define sd_pkt_status_busy ssd_pkt_status_busy 983 #define sd_pkt_status_reservation_conflict \ 984 ssd_pkt_status_reservation_conflict 985 #define sd_pkt_status_qfull ssd_pkt_status_qfull 986 #define sd_handle_request_sense ssd_handle_request_sense 987 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 988 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 989 #define sd_validate_sense_data ssd_validate_sense_data 990 #define sd_decode_sense ssd_decode_sense 991 #define sd_print_sense_msg ssd_print_sense_msg 992 #define sd_sense_key_no_sense ssd_sense_key_no_sense 993 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 994 #define sd_sense_key_not_ready ssd_sense_key_not_ready 995 #define sd_sense_key_medium_or_hardware_error \ 996 ssd_sense_key_medium_or_hardware_error 997 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 998 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 999 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1000 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1001 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1002 #define sd_sense_key_default ssd_sense_key_default 1003 #define sd_print_retry_msg ssd_print_retry_msg 1004 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1005 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1006 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1007 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1008 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1009 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1010 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1011 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1012 #define sd_pkt_reason_default ssd_pkt_reason_default 1013 #define sd_reset_target ssd_reset_target 1014 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1015 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1016 #define sd_taskq_create ssd_taskq_create 1017 #define sd_taskq_delete ssd_taskq_delete 1018 #define sd_media_change_task ssd_media_change_task 1019 #define sd_handle_mchange ssd_handle_mchange 1020 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1021 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1022 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1023 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1024 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1025 sd_send_scsi_feature_GET_CONFIGURATION 1026 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1027 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1028 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1029 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1030 ssd_send_scsi_PERSISTENT_RESERVE_IN 1031 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1032 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1033 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1034 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1035 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1036 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1037 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1038 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1039 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1040 #define sd_alloc_rqs ssd_alloc_rqs 1041 #define sd_free_rqs ssd_free_rqs 1042 #define sd_dump_memory ssd_dump_memory 1043 #define sd_uscsi_ioctl ssd_uscsi_ioctl 1044 #define sd_get_media_info ssd_get_media_info 1045 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1046 #define sd_dkio_get_geometry ssd_dkio_get_geometry 1047 #define sd_dkio_set_geometry ssd_dkio_set_geometry 1048 #define sd_dkio_get_partition ssd_dkio_get_partition 1049 #define sd_dkio_set_partition ssd_dkio_set_partition 1050 #define sd_dkio_partition ssd_dkio_partition 1051 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 1052 #define sd_dkio_get_efi ssd_dkio_get_efi 1053 #define sd_build_user_vtoc ssd_build_user_vtoc 1054 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 1055 #define sd_dkio_set_efi ssd_dkio_set_efi 1056 #define sd_build_label_vtoc ssd_build_label_vtoc 1057 #define sd_write_label ssd_write_label 1058 #define sd_clear_vtoc ssd_clear_vtoc 1059 #define sd_clear_efi ssd_clear_efi 1060 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1061 #define sd_setup_next_xfer ssd_setup_next_xfer 1062 #define sd_dkio_get_temp ssd_dkio_get_temp 1063 #define sd_dkio_get_mboot ssd_dkio_get_mboot 1064 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1065 #define sd_setup_default_geometry ssd_setup_default_geometry 1066 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1067 #define sd_check_mhd ssd_check_mhd 1068 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1069 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1070 #define sd_sname ssd_sname 1071 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1072 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1073 #define sd_take_ownership ssd_take_ownership 1074 #define sd_reserve_release ssd_reserve_release 1075 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1076 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1077 #define sd_persistent_reservation_in_read_keys \ 1078 ssd_persistent_reservation_in_read_keys 1079 #define sd_persistent_reservation_in_read_resv \ 1080 ssd_persistent_reservation_in_read_resv 1081 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1082 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1083 #define sd_mhdioc_release ssd_mhdioc_release 1084 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1085 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1086 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1087 #define sr_change_blkmode ssr_change_blkmode 1088 #define sr_change_speed ssr_change_speed 1089 #define sr_atapi_change_speed ssr_atapi_change_speed 1090 #define sr_pause_resume ssr_pause_resume 1091 #define sr_play_msf ssr_play_msf 1092 #define sr_play_trkind ssr_play_trkind 1093 #define sr_read_all_subcodes ssr_read_all_subcodes 1094 #define sr_read_subchannel ssr_read_subchannel 1095 #define sr_read_tocentry ssr_read_tocentry 1096 #define sr_read_tochdr ssr_read_tochdr 1097 #define sr_read_cdda ssr_read_cdda 1098 #define sr_read_cdxa ssr_read_cdxa 1099 #define sr_read_mode1 ssr_read_mode1 1100 #define sr_read_mode2 ssr_read_mode2 1101 #define sr_read_cd_mode2 ssr_read_cd_mode2 1102 #define sr_sector_mode ssr_sector_mode 1103 #define sr_eject ssr_eject 1104 #define sr_ejected ssr_ejected 1105 #define sr_check_wp ssr_check_wp 1106 #define sd_check_media ssd_check_media 1107 #define sd_media_watch_cb ssd_media_watch_cb 1108 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1109 #define sr_volume_ctrl ssr_volume_ctrl 1110 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1111 #define sd_log_page_supported ssd_log_page_supported 1112 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1113 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1114 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1115 #define sd_range_lock ssd_range_lock 1116 #define sd_get_range ssd_get_range 1117 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1118 #define sd_range_unlock ssd_range_unlock 1119 #define sd_read_modify_write_task ssd_read_modify_write_task 1120 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1121 1122 #define sd_iostart_chain ssd_iostart_chain 1123 #define sd_iodone_chain ssd_iodone_chain 1124 #define sd_initpkt_map ssd_initpkt_map 1125 #define sd_destroypkt_map ssd_destroypkt_map 1126 #define sd_chain_type_map ssd_chain_type_map 1127 #define sd_chain_index_map ssd_chain_index_map 1128 1129 #define sd_failfast_flushctl ssd_failfast_flushctl 1130 #define sd_failfast_flushq ssd_failfast_flushq 1131 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1132 1133 #define sd_is_lsi ssd_is_lsi 1134 1135 #endif /* #if (defined(__fibre)) */ 1136 1137 1138 int _init(void); 1139 int _fini(void); 1140 int _info(struct modinfo *modinfop); 1141 1142 /*PRINTFLIKE3*/ 1143 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1144 /*PRINTFLIKE3*/ 1145 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1146 /*PRINTFLIKE3*/ 1147 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1148 1149 static int sdprobe(dev_info_t *devi); 1150 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1151 void **result); 1152 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1153 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1154 1155 /* 1156 * Smart probe for parallel scsi 1157 */ 1158 static void sd_scsi_probe_cache_init(void); 1159 static void sd_scsi_probe_cache_fini(void); 1160 static void sd_scsi_clear_probe_cache(void); 1161 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1162 1163 /* 1164 * Attached luns on target for parallel scsi 1165 */ 1166 static void sd_scsi_target_lun_init(void); 1167 static void sd_scsi_target_lun_fini(void); 1168 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1169 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1170 1171 static int sd_spin_up_unit(struct sd_lun *un); 1172 #ifdef _LP64 1173 static void sd_enable_descr_sense(struct sd_lun *un); 1174 static void sd_reenable_dsense_task(void *arg); 1175 #endif /* _LP64 */ 1176 1177 static void sd_set_mmc_caps(struct sd_lun *un); 1178 1179 static void sd_read_unit_properties(struct sd_lun *un); 1180 static int sd_process_sdconf_file(struct sd_lun *un); 1181 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1182 int *data_list, sd_tunables *values); 1183 static void sd_process_sdconf_table(struct sd_lun *un); 1184 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1185 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1186 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1187 int list_len, char *dataname_ptr); 1188 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1189 sd_tunables *prop_list); 1190 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1191 1192 #if defined(_SUNOS_VTOC_16) 1193 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1194 #endif 1195 1196 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1197 int path_flag); 1198 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1199 int path_flag); 1200 static void sd_get_physical_geometry(struct sd_lun *un, 1201 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1202 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1203 int lbasize); 1204 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1205 static void sd_swap_efi_gpt(efi_gpt_t *); 1206 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1207 static int sd_validate_efi(efi_gpt_t *); 1208 static int sd_use_efi(struct sd_lun *, int); 1209 static void sd_build_default_label(struct sd_lun *un); 1210 1211 #if defined(_FIRMWARE_NEEDS_FDISK) 1212 static int sd_has_max_chs_vals(struct ipart *fdp); 1213 #endif 1214 static void sd_inq_fill(char *p, int l, char *s); 1215 1216 1217 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1218 int reservation_flag); 1219 static daddr_t sd_get_devid_block(struct sd_lun *un); 1220 static int sd_get_devid(struct sd_lun *un); 1221 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1222 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1223 static int sd_write_deviceid(struct sd_lun *un); 1224 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1225 static int sd_check_vpd_page_support(struct sd_lun *un); 1226 1227 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1228 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1229 1230 static int sd_ddi_suspend(dev_info_t *devi); 1231 static int sd_ddi_pm_suspend(struct sd_lun *un); 1232 static int sd_ddi_resume(dev_info_t *devi); 1233 static int sd_ddi_pm_resume(struct sd_lun *un); 1234 static int sdpower(dev_info_t *devi, int component, int level); 1235 1236 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1237 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1238 static int sd_unit_attach(dev_info_t *devi); 1239 static int sd_unit_detach(dev_info_t *devi); 1240 1241 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1242 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1243 static void sd_create_errstats(struct sd_lun *un, int instance); 1244 static void sd_set_errstats(struct sd_lun *un); 1245 static void sd_set_pstats(struct sd_lun *un); 1246 1247 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1248 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1249 static int sd_send_polled_RQS(struct sd_lun *un); 1250 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1251 1252 #if (defined(__fibre)) 1253 /* 1254 * Event callbacks (photon) 1255 */ 1256 static void sd_init_event_callbacks(struct sd_lun *un); 1257 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1258 #endif 1259 1260 /* 1261 * Defines for sd_cache_control 1262 */ 1263 1264 #define SD_CACHE_ENABLE 1 1265 #define SD_CACHE_DISABLE 0 1266 #define SD_CACHE_NOCHANGE -1 1267 1268 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1269 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1270 static dev_t sd_make_device(dev_info_t *devi); 1271 1272 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1273 uint64_t capacity); 1274 1275 /* 1276 * Driver entry point functions. 1277 */ 1278 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1279 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1280 static int sd_ready_and_valid(struct sd_lun *un); 1281 1282 static void sdmin(struct buf *bp); 1283 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1284 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1285 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1286 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1287 1288 static int sdstrategy(struct buf *bp); 1289 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1290 1291 /* 1292 * Function prototypes for layering functions in the iostart chain. 1293 */ 1294 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1295 struct buf *bp); 1296 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1297 struct buf *bp); 1298 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1299 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1300 struct buf *bp); 1301 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1302 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1303 1304 /* 1305 * Function prototypes for layering functions in the iodone chain. 1306 */ 1307 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1308 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1309 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1310 struct buf *bp); 1311 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1312 struct buf *bp); 1313 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1314 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1315 struct buf *bp); 1316 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1317 1318 /* 1319 * Prototypes for functions to support buf(9S) based IO. 1320 */ 1321 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1322 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1323 static void sd_destroypkt_for_buf(struct buf *); 1324 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1325 struct buf *bp, int flags, 1326 int (*callback)(caddr_t), caddr_t callback_arg, 1327 diskaddr_t lba, uint32_t blockcount); 1328 #if defined(__i386) || defined(__amd64) 1329 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1330 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1331 #endif /* defined(__i386) || defined(__amd64) */ 1332 1333 /* 1334 * Prototypes for functions to support USCSI IO. 1335 */ 1336 static int sd_uscsi_strategy(struct buf *bp); 1337 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1338 static void sd_destroypkt_for_uscsi(struct buf *); 1339 1340 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1341 uchar_t chain_type, void *pktinfop); 1342 1343 static int sd_pm_entry(struct sd_lun *un); 1344 static void sd_pm_exit(struct sd_lun *un); 1345 1346 static void sd_pm_idletimeout_handler(void *arg); 1347 1348 /* 1349 * sd_core internal functions (used at the sd_core_io layer). 1350 */ 1351 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1352 static void sdintr(struct scsi_pkt *pktp); 1353 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1354 1355 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1356 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1357 int path_flag); 1358 1359 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1360 daddr_t blkno, int (*func)(struct buf *)); 1361 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1362 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1363 static void sd_bioclone_free(struct buf *bp); 1364 static void sd_shadow_buf_free(struct buf *bp); 1365 1366 static void sd_print_transport_rejected_message(struct sd_lun *un, 1367 struct sd_xbuf *xp, int code); 1368 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1369 void *arg, int code); 1370 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1371 void *arg, int code); 1372 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1373 void *arg, int code); 1374 1375 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1376 int retry_check_flag, 1377 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1378 int c), 1379 void *user_arg, int failure_code, clock_t retry_delay, 1380 void (*statp)(kstat_io_t *)); 1381 1382 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1383 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1384 1385 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1386 struct scsi_pkt *pktp); 1387 static void sd_start_retry_command(void *arg); 1388 static void sd_start_direct_priority_command(void *arg); 1389 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1390 int errcode); 1391 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1392 struct buf *bp, int errcode); 1393 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1394 static void sd_sync_with_callback(struct sd_lun *un); 1395 static int sdrunout(caddr_t arg); 1396 1397 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1398 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1399 1400 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1401 static void sd_restore_throttle(void *arg); 1402 1403 static void sd_init_cdb_limits(struct sd_lun *un); 1404 1405 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1406 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1407 1408 /* 1409 * Error handling functions 1410 */ 1411 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1412 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1413 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1414 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1415 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1416 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1417 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1418 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1419 1420 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1421 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1422 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1423 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1424 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1425 struct sd_xbuf *xp); 1426 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1427 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1428 1429 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1430 void *arg, int code); 1431 1432 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1433 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1434 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1435 uint8_t *sense_datap, 1436 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1437 static void sd_sense_key_not_ready(struct sd_lun *un, 1438 uint8_t *sense_datap, 1439 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1440 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1441 uint8_t *sense_datap, 1442 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1443 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1444 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1445 static void sd_sense_key_unit_attention(struct sd_lun *un, 1446 uint8_t *sense_datap, 1447 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1448 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1449 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1450 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1451 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1452 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1453 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1454 static void sd_sense_key_default(struct sd_lun *un, 1455 uint8_t *sense_datap, 1456 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1457 1458 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1459 void *arg, int flag); 1460 1461 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1462 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1463 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1464 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1465 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1466 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1467 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1468 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1469 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1470 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1471 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1472 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1473 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1474 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1475 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1476 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1477 1478 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1479 1480 static void sd_start_stop_unit_callback(void *arg); 1481 static void sd_start_stop_unit_task(void *arg); 1482 1483 static void sd_taskq_create(void); 1484 static void sd_taskq_delete(void); 1485 static void sd_media_change_task(void *arg); 1486 1487 static int sd_handle_mchange(struct sd_lun *un); 1488 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1489 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1490 uint32_t *lbap, int path_flag); 1491 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1492 uint32_t *lbap, int path_flag); 1493 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1494 int path_flag); 1495 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1496 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1497 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1498 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1499 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1500 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1501 uchar_t usr_cmd, uchar_t *usr_bufp); 1502 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1503 struct dk_callback *dkc); 1504 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1505 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1506 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1507 uchar_t *bufaddr, uint_t buflen); 1508 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1509 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1510 uchar_t *bufaddr, uint_t buflen, char feature); 1511 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1512 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1513 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1514 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1515 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1516 size_t buflen, daddr_t start_block, int path_flag); 1517 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1518 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1519 path_flag) 1520 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1521 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1522 path_flag) 1523 1524 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1525 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1526 uint16_t param_ptr, int path_flag); 1527 1528 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1529 static void sd_free_rqs(struct sd_lun *un); 1530 1531 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1532 uchar_t *data, int len, int fmt); 1533 static void sd_panic_for_res_conflict(struct sd_lun *un); 1534 1535 /* 1536 * Disk Ioctl Function Prototypes 1537 */ 1538 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1539 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1540 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1541 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1542 int geom_validated); 1543 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1544 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1545 int geom_validated); 1546 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1547 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1548 int geom_validated); 1549 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1550 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1551 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1552 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1553 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1554 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1555 static int sd_write_label(dev_t dev); 1556 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1557 static void sd_clear_vtoc(struct sd_lun *un); 1558 static void sd_clear_efi(struct sd_lun *un); 1559 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1560 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1561 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1562 static void sd_setup_default_geometry(struct sd_lun *un); 1563 #if defined(__i386) || defined(__amd64) 1564 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1565 #endif 1566 1567 /* 1568 * Multi-host Ioctl Prototypes 1569 */ 1570 static int sd_check_mhd(dev_t dev, int interval); 1571 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1572 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1573 static char *sd_sname(uchar_t status); 1574 static void sd_mhd_resvd_recover(void *arg); 1575 static void sd_resv_reclaim_thread(); 1576 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1577 static int sd_reserve_release(dev_t dev, int cmd); 1578 static void sd_rmv_resv_reclaim_req(dev_t dev); 1579 static void sd_mhd_reset_notify_cb(caddr_t arg); 1580 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1581 mhioc_inkeys_t *usrp, int flag); 1582 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1583 mhioc_inresvs_t *usrp, int flag); 1584 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1585 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1586 static int sd_mhdioc_release(dev_t dev); 1587 static int sd_mhdioc_register_devid(dev_t dev); 1588 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1589 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1590 1591 /* 1592 * SCSI removable prototypes 1593 */ 1594 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1595 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1596 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1597 static int sr_pause_resume(dev_t dev, int mode); 1598 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1599 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1600 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1601 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1602 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1603 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1604 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1605 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1606 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1607 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1609 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1610 static int sr_eject(dev_t dev); 1611 static void sr_ejected(register struct sd_lun *un); 1612 static int sr_check_wp(dev_t dev); 1613 static int sd_check_media(dev_t dev, enum dkio_state state); 1614 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1615 static void sd_delayed_cv_broadcast(void *arg); 1616 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1617 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1618 1619 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1620 1621 /* 1622 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1623 */ 1624 static void sd_check_for_writable_cd(struct sd_lun *un); 1625 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1626 static void sd_wm_cache_destructor(void *wm, void *un); 1627 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1628 daddr_t endb, ushort_t typ); 1629 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1630 daddr_t endb); 1631 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1632 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1633 static void sd_read_modify_write_task(void * arg); 1634 static int 1635 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1636 struct buf **bpp); 1637 1638 1639 /* 1640 * Function prototypes for failfast support. 1641 */ 1642 static void sd_failfast_flushq(struct sd_lun *un); 1643 static int sd_failfast_flushq_callback(struct buf *bp); 1644 1645 /* 1646 * Function prototypes to check for lsi devices 1647 */ 1648 static void sd_is_lsi(struct sd_lun *un); 1649 1650 /* 1651 * Function prototypes for x86 support 1652 */ 1653 #if defined(__i386) || defined(__amd64) 1654 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1655 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1656 #endif 1657 1658 /* 1659 * Constants for failfast support: 1660 * 1661 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1662 * failfast processing being performed. 1663 * 1664 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1665 * failfast processing on all bufs with B_FAILFAST set. 1666 */ 1667 1668 #define SD_FAILFAST_INACTIVE 0 1669 #define SD_FAILFAST_ACTIVE 1 1670 1671 /* 1672 * Bitmask to control behavior of buf(9S) flushes when a transition to 1673 * the failfast state occurs. Optional bits include: 1674 * 1675 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1676 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1677 * be flushed. 1678 * 1679 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1680 * driver, in addition to the regular wait queue. This includes the xbuf 1681 * queues. When clear, only the driver's wait queue will be flushed. 1682 */ 1683 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1684 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1685 1686 /* 1687 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1688 * to flush all queues within the driver. 1689 */ 1690 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1691 1692 1693 /* 1694 * SD Testing Fault Injection 1695 */ 1696 #ifdef SD_FAULT_INJECTION 1697 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1698 static void sd_faultinjection(struct scsi_pkt *pktp); 1699 static void sd_injection_log(char *buf, struct sd_lun *un); 1700 #endif 1701 1702 /* 1703 * Device driver ops vector 1704 */ 1705 static struct cb_ops sd_cb_ops = { 1706 sdopen, /* open */ 1707 sdclose, /* close */ 1708 sdstrategy, /* strategy */ 1709 nodev, /* print */ 1710 sddump, /* dump */ 1711 sdread, /* read */ 1712 sdwrite, /* write */ 1713 sdioctl, /* ioctl */ 1714 nodev, /* devmap */ 1715 nodev, /* mmap */ 1716 nodev, /* segmap */ 1717 nochpoll, /* poll */ 1718 sd_prop_op, /* cb_prop_op */ 1719 0, /* streamtab */ 1720 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1721 CB_REV, /* cb_rev */ 1722 sdaread, /* async I/O read entry point */ 1723 sdawrite /* async I/O write entry point */ 1724 }; 1725 1726 static struct dev_ops sd_ops = { 1727 DEVO_REV, /* devo_rev, */ 1728 0, /* refcnt */ 1729 sdinfo, /* info */ 1730 nulldev, /* identify */ 1731 sdprobe, /* probe */ 1732 sdattach, /* attach */ 1733 sddetach, /* detach */ 1734 nodev, /* reset */ 1735 &sd_cb_ops, /* driver operations */ 1736 NULL, /* bus operations */ 1737 sdpower /* power */ 1738 }; 1739 1740 1741 /* 1742 * This is the loadable module wrapper. 1743 */ 1744 #include <sys/modctl.h> 1745 1746 static struct modldrv modldrv = { 1747 &mod_driverops, /* Type of module. This one is a driver */ 1748 SD_MODULE_NAME, /* Module name. */ 1749 &sd_ops /* driver ops */ 1750 }; 1751 1752 1753 static struct modlinkage modlinkage = { 1754 MODREV_1, 1755 &modldrv, 1756 NULL 1757 }; 1758 1759 1760 static struct scsi_asq_key_strings sd_additional_codes[] = { 1761 0x81, 0, "Logical Unit is Reserved", 1762 0x85, 0, "Audio Address Not Valid", 1763 0xb6, 0, "Media Load Mechanism Failed", 1764 0xB9, 0, "Audio Play Operation Aborted", 1765 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1766 0x53, 2, "Medium removal prevented", 1767 0x6f, 0, "Authentication failed during key exchange", 1768 0x6f, 1, "Key not present", 1769 0x6f, 2, "Key not established", 1770 0x6f, 3, "Read without proper authentication", 1771 0x6f, 4, "Mismatched region to this logical unit", 1772 0x6f, 5, "Region reset count error", 1773 0xffff, 0x0, NULL 1774 }; 1775 1776 1777 /* 1778 * Struct for passing printing information for sense data messages 1779 */ 1780 struct sd_sense_info { 1781 int ssi_severity; 1782 int ssi_pfa_flag; 1783 }; 1784 1785 /* 1786 * Table of function pointers for iostart-side routines. Seperate "chains" 1787 * of layered function calls are formed by placing the function pointers 1788 * sequentially in the desired order. Functions are called according to an 1789 * incrementing table index ordering. The last function in each chain must 1790 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1791 * in the sd_iodone_chain[] array. 1792 * 1793 * Note: It may seem more natural to organize both the iostart and iodone 1794 * functions together, into an array of structures (or some similar 1795 * organization) with a common index, rather than two seperate arrays which 1796 * must be maintained in synchronization. The purpose of this division is 1797 * to achiece improved performance: individual arrays allows for more 1798 * effective cache line utilization on certain platforms. 1799 */ 1800 1801 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1802 1803 1804 static sd_chain_t sd_iostart_chain[] = { 1805 1806 /* Chain for buf IO for disk drive targets (PM enabled) */ 1807 sd_mapblockaddr_iostart, /* Index: 0 */ 1808 sd_pm_iostart, /* Index: 1 */ 1809 sd_core_iostart, /* Index: 2 */ 1810 1811 /* Chain for buf IO for disk drive targets (PM disabled) */ 1812 sd_mapblockaddr_iostart, /* Index: 3 */ 1813 sd_core_iostart, /* Index: 4 */ 1814 1815 /* Chain for buf IO for removable-media targets (PM enabled) */ 1816 sd_mapblockaddr_iostart, /* Index: 5 */ 1817 sd_mapblocksize_iostart, /* Index: 6 */ 1818 sd_pm_iostart, /* Index: 7 */ 1819 sd_core_iostart, /* Index: 8 */ 1820 1821 /* Chain for buf IO for removable-media targets (PM disabled) */ 1822 sd_mapblockaddr_iostart, /* Index: 9 */ 1823 sd_mapblocksize_iostart, /* Index: 10 */ 1824 sd_core_iostart, /* Index: 11 */ 1825 1826 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1827 sd_mapblockaddr_iostart, /* Index: 12 */ 1828 sd_checksum_iostart, /* Index: 13 */ 1829 sd_pm_iostart, /* Index: 14 */ 1830 sd_core_iostart, /* Index: 15 */ 1831 1832 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1833 sd_mapblockaddr_iostart, /* Index: 16 */ 1834 sd_checksum_iostart, /* Index: 17 */ 1835 sd_core_iostart, /* Index: 18 */ 1836 1837 /* Chain for USCSI commands (all targets) */ 1838 sd_pm_iostart, /* Index: 19 */ 1839 sd_core_iostart, /* Index: 20 */ 1840 1841 /* Chain for checksumming USCSI commands (all targets) */ 1842 sd_checksum_uscsi_iostart, /* Index: 21 */ 1843 sd_pm_iostart, /* Index: 22 */ 1844 sd_core_iostart, /* Index: 23 */ 1845 1846 /* Chain for "direct" USCSI commands (all targets) */ 1847 sd_core_iostart, /* Index: 24 */ 1848 1849 /* Chain for "direct priority" USCSI commands (all targets) */ 1850 sd_core_iostart, /* Index: 25 */ 1851 }; 1852 1853 /* 1854 * Macros to locate the first function of each iostart chain in the 1855 * sd_iostart_chain[] array. These are located by the index in the array. 1856 */ 1857 #define SD_CHAIN_DISK_IOSTART 0 1858 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1859 #define SD_CHAIN_RMMEDIA_IOSTART 5 1860 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1861 #define SD_CHAIN_CHKSUM_IOSTART 12 1862 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1863 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1864 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1865 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1866 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1867 1868 1869 /* 1870 * Table of function pointers for the iodone-side routines for the driver- 1871 * internal layering mechanism. The calling sequence for iodone routines 1872 * uses a decrementing table index, so the last routine called in a chain 1873 * must be at the lowest array index location for that chain. The last 1874 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1875 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1876 * of the functions in an iodone side chain must correspond to the ordering 1877 * of the iostart routines for that chain. Note that there is no iodone 1878 * side routine that corresponds to sd_core_iostart(), so there is no 1879 * entry in the table for this. 1880 */ 1881 1882 static sd_chain_t sd_iodone_chain[] = { 1883 1884 /* Chain for buf IO for disk drive targets (PM enabled) */ 1885 sd_buf_iodone, /* Index: 0 */ 1886 sd_mapblockaddr_iodone, /* Index: 1 */ 1887 sd_pm_iodone, /* Index: 2 */ 1888 1889 /* Chain for buf IO for disk drive targets (PM disabled) */ 1890 sd_buf_iodone, /* Index: 3 */ 1891 sd_mapblockaddr_iodone, /* Index: 4 */ 1892 1893 /* Chain for buf IO for removable-media targets (PM enabled) */ 1894 sd_buf_iodone, /* Index: 5 */ 1895 sd_mapblockaddr_iodone, /* Index: 6 */ 1896 sd_mapblocksize_iodone, /* Index: 7 */ 1897 sd_pm_iodone, /* Index: 8 */ 1898 1899 /* Chain for buf IO for removable-media targets (PM disabled) */ 1900 sd_buf_iodone, /* Index: 9 */ 1901 sd_mapblockaddr_iodone, /* Index: 10 */ 1902 sd_mapblocksize_iodone, /* Index: 11 */ 1903 1904 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1905 sd_buf_iodone, /* Index: 12 */ 1906 sd_mapblockaddr_iodone, /* Index: 13 */ 1907 sd_checksum_iodone, /* Index: 14 */ 1908 sd_pm_iodone, /* Index: 15 */ 1909 1910 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1911 sd_buf_iodone, /* Index: 16 */ 1912 sd_mapblockaddr_iodone, /* Index: 17 */ 1913 sd_checksum_iodone, /* Index: 18 */ 1914 1915 /* Chain for USCSI commands (non-checksum targets) */ 1916 sd_uscsi_iodone, /* Index: 19 */ 1917 sd_pm_iodone, /* Index: 20 */ 1918 1919 /* Chain for USCSI commands (checksum targets) */ 1920 sd_uscsi_iodone, /* Index: 21 */ 1921 sd_checksum_uscsi_iodone, /* Index: 22 */ 1922 sd_pm_iodone, /* Index: 22 */ 1923 1924 /* Chain for "direct" USCSI commands (all targets) */ 1925 sd_uscsi_iodone, /* Index: 24 */ 1926 1927 /* Chain for "direct priority" USCSI commands (all targets) */ 1928 sd_uscsi_iodone, /* Index: 25 */ 1929 }; 1930 1931 1932 /* 1933 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1934 * each iodone-side chain. These are located by the array index, but as the 1935 * iodone side functions are called in a decrementing-index order, the 1936 * highest index number in each chain must be specified (as these correspond 1937 * to the first function in the iodone chain that will be called by the core 1938 * at IO completion time). 1939 */ 1940 1941 #define SD_CHAIN_DISK_IODONE 2 1942 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1943 #define SD_CHAIN_RMMEDIA_IODONE 8 1944 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1945 #define SD_CHAIN_CHKSUM_IODONE 15 1946 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1947 #define SD_CHAIN_USCSI_CMD_IODONE 20 1948 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1949 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1950 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1951 1952 1953 1954 1955 /* 1956 * Array to map a layering chain index to the appropriate initpkt routine. 1957 * The redundant entries are present so that the index used for accessing 1958 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1959 * with this table as well. 1960 */ 1961 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1962 1963 static sd_initpkt_t sd_initpkt_map[] = { 1964 1965 /* Chain for buf IO for disk drive targets (PM enabled) */ 1966 sd_initpkt_for_buf, /* Index: 0 */ 1967 sd_initpkt_for_buf, /* Index: 1 */ 1968 sd_initpkt_for_buf, /* Index: 2 */ 1969 1970 /* Chain for buf IO for disk drive targets (PM disabled) */ 1971 sd_initpkt_for_buf, /* Index: 3 */ 1972 sd_initpkt_for_buf, /* Index: 4 */ 1973 1974 /* Chain for buf IO for removable-media targets (PM enabled) */ 1975 sd_initpkt_for_buf, /* Index: 5 */ 1976 sd_initpkt_for_buf, /* Index: 6 */ 1977 sd_initpkt_for_buf, /* Index: 7 */ 1978 sd_initpkt_for_buf, /* Index: 8 */ 1979 1980 /* Chain for buf IO for removable-media targets (PM disabled) */ 1981 sd_initpkt_for_buf, /* Index: 9 */ 1982 sd_initpkt_for_buf, /* Index: 10 */ 1983 sd_initpkt_for_buf, /* Index: 11 */ 1984 1985 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1986 sd_initpkt_for_buf, /* Index: 12 */ 1987 sd_initpkt_for_buf, /* Index: 13 */ 1988 sd_initpkt_for_buf, /* Index: 14 */ 1989 sd_initpkt_for_buf, /* Index: 15 */ 1990 1991 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1992 sd_initpkt_for_buf, /* Index: 16 */ 1993 sd_initpkt_for_buf, /* Index: 17 */ 1994 sd_initpkt_for_buf, /* Index: 18 */ 1995 1996 /* Chain for USCSI commands (non-checksum targets) */ 1997 sd_initpkt_for_uscsi, /* Index: 19 */ 1998 sd_initpkt_for_uscsi, /* Index: 20 */ 1999 2000 /* Chain for USCSI commands (checksum targets) */ 2001 sd_initpkt_for_uscsi, /* Index: 21 */ 2002 sd_initpkt_for_uscsi, /* Index: 22 */ 2003 sd_initpkt_for_uscsi, /* Index: 22 */ 2004 2005 /* Chain for "direct" USCSI commands (all targets) */ 2006 sd_initpkt_for_uscsi, /* Index: 24 */ 2007 2008 /* Chain for "direct priority" USCSI commands (all targets) */ 2009 sd_initpkt_for_uscsi, /* Index: 25 */ 2010 2011 }; 2012 2013 2014 /* 2015 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2016 * The redundant entries are present so that the index used for accessing 2017 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2018 * with this table as well. 2019 */ 2020 typedef void (*sd_destroypkt_t)(struct buf *); 2021 2022 static sd_destroypkt_t sd_destroypkt_map[] = { 2023 2024 /* Chain for buf IO for disk drive targets (PM enabled) */ 2025 sd_destroypkt_for_buf, /* Index: 0 */ 2026 sd_destroypkt_for_buf, /* Index: 1 */ 2027 sd_destroypkt_for_buf, /* Index: 2 */ 2028 2029 /* Chain for buf IO for disk drive targets (PM disabled) */ 2030 sd_destroypkt_for_buf, /* Index: 3 */ 2031 sd_destroypkt_for_buf, /* Index: 4 */ 2032 2033 /* Chain for buf IO for removable-media targets (PM enabled) */ 2034 sd_destroypkt_for_buf, /* Index: 5 */ 2035 sd_destroypkt_for_buf, /* Index: 6 */ 2036 sd_destroypkt_for_buf, /* Index: 7 */ 2037 sd_destroypkt_for_buf, /* Index: 8 */ 2038 2039 /* Chain for buf IO for removable-media targets (PM disabled) */ 2040 sd_destroypkt_for_buf, /* Index: 9 */ 2041 sd_destroypkt_for_buf, /* Index: 10 */ 2042 sd_destroypkt_for_buf, /* Index: 11 */ 2043 2044 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2045 sd_destroypkt_for_buf, /* Index: 12 */ 2046 sd_destroypkt_for_buf, /* Index: 13 */ 2047 sd_destroypkt_for_buf, /* Index: 14 */ 2048 sd_destroypkt_for_buf, /* Index: 15 */ 2049 2050 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2051 sd_destroypkt_for_buf, /* Index: 16 */ 2052 sd_destroypkt_for_buf, /* Index: 17 */ 2053 sd_destroypkt_for_buf, /* Index: 18 */ 2054 2055 /* Chain for USCSI commands (non-checksum targets) */ 2056 sd_destroypkt_for_uscsi, /* Index: 19 */ 2057 sd_destroypkt_for_uscsi, /* Index: 20 */ 2058 2059 /* Chain for USCSI commands (checksum targets) */ 2060 sd_destroypkt_for_uscsi, /* Index: 21 */ 2061 sd_destroypkt_for_uscsi, /* Index: 22 */ 2062 sd_destroypkt_for_uscsi, /* Index: 22 */ 2063 2064 /* Chain for "direct" USCSI commands (all targets) */ 2065 sd_destroypkt_for_uscsi, /* Index: 24 */ 2066 2067 /* Chain for "direct priority" USCSI commands (all targets) */ 2068 sd_destroypkt_for_uscsi, /* Index: 25 */ 2069 2070 }; 2071 2072 2073 2074 /* 2075 * Array to map a layering chain index to the appropriate chain "type". 2076 * The chain type indicates a specific property/usage of the chain. 2077 * The redundant entries are present so that the index used for accessing 2078 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2079 * with this table as well. 2080 */ 2081 2082 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2083 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2084 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2085 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2086 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2087 /* (for error recovery) */ 2088 2089 static int sd_chain_type_map[] = { 2090 2091 /* Chain for buf IO for disk drive targets (PM enabled) */ 2092 SD_CHAIN_BUFIO, /* Index: 0 */ 2093 SD_CHAIN_BUFIO, /* Index: 1 */ 2094 SD_CHAIN_BUFIO, /* Index: 2 */ 2095 2096 /* Chain for buf IO for disk drive targets (PM disabled) */ 2097 SD_CHAIN_BUFIO, /* Index: 3 */ 2098 SD_CHAIN_BUFIO, /* Index: 4 */ 2099 2100 /* Chain for buf IO for removable-media targets (PM enabled) */ 2101 SD_CHAIN_BUFIO, /* Index: 5 */ 2102 SD_CHAIN_BUFIO, /* Index: 6 */ 2103 SD_CHAIN_BUFIO, /* Index: 7 */ 2104 SD_CHAIN_BUFIO, /* Index: 8 */ 2105 2106 /* Chain for buf IO for removable-media targets (PM disabled) */ 2107 SD_CHAIN_BUFIO, /* Index: 9 */ 2108 SD_CHAIN_BUFIO, /* Index: 10 */ 2109 SD_CHAIN_BUFIO, /* Index: 11 */ 2110 2111 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2112 SD_CHAIN_BUFIO, /* Index: 12 */ 2113 SD_CHAIN_BUFIO, /* Index: 13 */ 2114 SD_CHAIN_BUFIO, /* Index: 14 */ 2115 SD_CHAIN_BUFIO, /* Index: 15 */ 2116 2117 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2118 SD_CHAIN_BUFIO, /* Index: 16 */ 2119 SD_CHAIN_BUFIO, /* Index: 17 */ 2120 SD_CHAIN_BUFIO, /* Index: 18 */ 2121 2122 /* Chain for USCSI commands (non-checksum targets) */ 2123 SD_CHAIN_USCSI, /* Index: 19 */ 2124 SD_CHAIN_USCSI, /* Index: 20 */ 2125 2126 /* Chain for USCSI commands (checksum targets) */ 2127 SD_CHAIN_USCSI, /* Index: 21 */ 2128 SD_CHAIN_USCSI, /* Index: 22 */ 2129 SD_CHAIN_USCSI, /* Index: 22 */ 2130 2131 /* Chain for "direct" USCSI commands (all targets) */ 2132 SD_CHAIN_DIRECT, /* Index: 24 */ 2133 2134 /* Chain for "direct priority" USCSI commands (all targets) */ 2135 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2136 }; 2137 2138 2139 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2140 #define SD_IS_BUFIO(xp) \ 2141 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2142 2143 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2144 #define SD_IS_DIRECT_PRIORITY(xp) \ 2145 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2146 2147 2148 2149 /* 2150 * Struct, array, and macros to map a specific chain to the appropriate 2151 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2152 * 2153 * The sd_chain_index_map[] array is used at attach time to set the various 2154 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2155 * chain to be used with the instance. This allows different instances to use 2156 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2157 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2158 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2159 * dynamically & without the use of locking; and (2) a layer may update the 2160 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2161 * to allow for deferred processing of an IO within the same chain from a 2162 * different execution context. 2163 */ 2164 2165 struct sd_chain_index { 2166 int sci_iostart_index; 2167 int sci_iodone_index; 2168 }; 2169 2170 static struct sd_chain_index sd_chain_index_map[] = { 2171 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2172 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2173 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2174 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2175 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2176 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2177 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2178 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2179 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2180 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2181 }; 2182 2183 2184 /* 2185 * The following are indexes into the sd_chain_index_map[] array. 2186 */ 2187 2188 /* un->un_buf_chain_type must be set to one of these */ 2189 #define SD_CHAIN_INFO_DISK 0 2190 #define SD_CHAIN_INFO_DISK_NO_PM 1 2191 #define SD_CHAIN_INFO_RMMEDIA 2 2192 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2193 #define SD_CHAIN_INFO_CHKSUM 4 2194 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2195 2196 /* un->un_uscsi_chain_type must be set to one of these */ 2197 #define SD_CHAIN_INFO_USCSI_CMD 6 2198 /* USCSI with PM disabled is the same as DIRECT */ 2199 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2200 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2201 2202 /* un->un_direct_chain_type must be set to one of these */ 2203 #define SD_CHAIN_INFO_DIRECT_CMD 8 2204 2205 /* un->un_priority_chain_type must be set to one of these */ 2206 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2207 2208 /* size for devid inquiries */ 2209 #define MAX_INQUIRY_SIZE 0xF0 2210 2211 /* 2212 * Macros used by functions to pass a given buf(9S) struct along to the 2213 * next function in the layering chain for further processing. 2214 * 2215 * In the following macros, passing more than three arguments to the called 2216 * routines causes the optimizer for the SPARC compiler to stop doing tail 2217 * call elimination which results in significant performance degradation. 2218 */ 2219 #define SD_BEGIN_IOSTART(index, un, bp) \ 2220 ((*(sd_iostart_chain[index]))(index, un, bp)) 2221 2222 #define SD_BEGIN_IODONE(index, un, bp) \ 2223 ((*(sd_iodone_chain[index]))(index, un, bp)) 2224 2225 #define SD_NEXT_IOSTART(index, un, bp) \ 2226 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2227 2228 #define SD_NEXT_IODONE(index, un, bp) \ 2229 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2230 2231 /* 2232 * Function: _init 2233 * 2234 * Description: This is the driver _init(9E) entry point. 2235 * 2236 * Return Code: Returns the value from mod_install(9F) or 2237 * ddi_soft_state_init(9F) as appropriate. 2238 * 2239 * Context: Called when driver module loaded. 2240 */ 2241 2242 int 2243 _init(void) 2244 { 2245 int err; 2246 2247 /* establish driver name from module name */ 2248 sd_label = mod_modname(&modlinkage); 2249 2250 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2251 SD_MAXUNIT); 2252 2253 if (err != 0) { 2254 return (err); 2255 } 2256 2257 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2258 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2259 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2260 2261 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2262 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2263 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2264 2265 /* 2266 * it's ok to init here even for fibre device 2267 */ 2268 sd_scsi_probe_cache_init(); 2269 2270 sd_scsi_target_lun_init(); 2271 2272 /* 2273 * Creating taskq before mod_install ensures that all callers (threads) 2274 * that enter the module after a successfull mod_install encounter 2275 * a valid taskq. 2276 */ 2277 sd_taskq_create(); 2278 2279 err = mod_install(&modlinkage); 2280 if (err != 0) { 2281 /* delete taskq if install fails */ 2282 sd_taskq_delete(); 2283 2284 mutex_destroy(&sd_detach_mutex); 2285 mutex_destroy(&sd_log_mutex); 2286 mutex_destroy(&sd_label_mutex); 2287 2288 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2289 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2290 cv_destroy(&sd_tr.srq_inprocess_cv); 2291 2292 sd_scsi_probe_cache_fini(); 2293 2294 sd_scsi_target_lun_fini(); 2295 2296 ddi_soft_state_fini(&sd_state); 2297 return (err); 2298 } 2299 2300 return (err); 2301 } 2302 2303 2304 /* 2305 * Function: _fini 2306 * 2307 * Description: This is the driver _fini(9E) entry point. 2308 * 2309 * Return Code: Returns the value from mod_remove(9F) 2310 * 2311 * Context: Called when driver module is unloaded. 2312 */ 2313 2314 int 2315 _fini(void) 2316 { 2317 int err; 2318 2319 if ((err = mod_remove(&modlinkage)) != 0) { 2320 return (err); 2321 } 2322 2323 sd_taskq_delete(); 2324 2325 mutex_destroy(&sd_detach_mutex); 2326 mutex_destroy(&sd_log_mutex); 2327 mutex_destroy(&sd_label_mutex); 2328 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2329 2330 sd_scsi_probe_cache_fini(); 2331 2332 sd_scsi_target_lun_fini(); 2333 2334 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2335 cv_destroy(&sd_tr.srq_inprocess_cv); 2336 2337 ddi_soft_state_fini(&sd_state); 2338 2339 return (err); 2340 } 2341 2342 2343 /* 2344 * Function: _info 2345 * 2346 * Description: This is the driver _info(9E) entry point. 2347 * 2348 * Arguments: modinfop - pointer to the driver modinfo structure 2349 * 2350 * Return Code: Returns the value from mod_info(9F). 2351 * 2352 * Context: Kernel thread context 2353 */ 2354 2355 int 2356 _info(struct modinfo *modinfop) 2357 { 2358 return (mod_info(&modlinkage, modinfop)); 2359 } 2360 2361 2362 /* 2363 * The following routines implement the driver message logging facility. 2364 * They provide component- and level- based debug output filtering. 2365 * Output may also be restricted to messages for a single instance by 2366 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2367 * to NULL, then messages for all instances are printed. 2368 * 2369 * These routines have been cloned from each other due to the language 2370 * constraints of macros and variable argument list processing. 2371 */ 2372 2373 2374 /* 2375 * Function: sd_log_err 2376 * 2377 * Description: This routine is called by the SD_ERROR macro for debug 2378 * logging of error conditions. 2379 * 2380 * Arguments: comp - driver component being logged 2381 * dev - pointer to driver info structure 2382 * fmt - error string and format to be logged 2383 */ 2384 2385 static void 2386 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2387 { 2388 va_list ap; 2389 dev_info_t *dev; 2390 2391 ASSERT(un != NULL); 2392 dev = SD_DEVINFO(un); 2393 ASSERT(dev != NULL); 2394 2395 /* 2396 * Filter messages based on the global component and level masks. 2397 * Also print if un matches the value of sd_debug_un, or if 2398 * sd_debug_un is set to NULL. 2399 */ 2400 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2401 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2402 mutex_enter(&sd_log_mutex); 2403 va_start(ap, fmt); 2404 (void) vsprintf(sd_log_buf, fmt, ap); 2405 va_end(ap); 2406 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2407 mutex_exit(&sd_log_mutex); 2408 } 2409 #ifdef SD_FAULT_INJECTION 2410 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2411 if (un->sd_injection_mask & comp) { 2412 mutex_enter(&sd_log_mutex); 2413 va_start(ap, fmt); 2414 (void) vsprintf(sd_log_buf, fmt, ap); 2415 va_end(ap); 2416 sd_injection_log(sd_log_buf, un); 2417 mutex_exit(&sd_log_mutex); 2418 } 2419 #endif 2420 } 2421 2422 2423 /* 2424 * Function: sd_log_info 2425 * 2426 * Description: This routine is called by the SD_INFO macro for debug 2427 * logging of general purpose informational conditions. 2428 * 2429 * Arguments: comp - driver component being logged 2430 * dev - pointer to driver info structure 2431 * fmt - info string and format to be logged 2432 */ 2433 2434 static void 2435 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2436 { 2437 va_list ap; 2438 dev_info_t *dev; 2439 2440 ASSERT(un != NULL); 2441 dev = SD_DEVINFO(un); 2442 ASSERT(dev != NULL); 2443 2444 /* 2445 * Filter messages based on the global component and level masks. 2446 * Also print if un matches the value of sd_debug_un, or if 2447 * sd_debug_un is set to NULL. 2448 */ 2449 if ((sd_component_mask & component) && 2450 (sd_level_mask & SD_LOGMASK_INFO) && 2451 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2452 mutex_enter(&sd_log_mutex); 2453 va_start(ap, fmt); 2454 (void) vsprintf(sd_log_buf, fmt, ap); 2455 va_end(ap); 2456 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2457 mutex_exit(&sd_log_mutex); 2458 } 2459 #ifdef SD_FAULT_INJECTION 2460 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2461 if (un->sd_injection_mask & component) { 2462 mutex_enter(&sd_log_mutex); 2463 va_start(ap, fmt); 2464 (void) vsprintf(sd_log_buf, fmt, ap); 2465 va_end(ap); 2466 sd_injection_log(sd_log_buf, un); 2467 mutex_exit(&sd_log_mutex); 2468 } 2469 #endif 2470 } 2471 2472 2473 /* 2474 * Function: sd_log_trace 2475 * 2476 * Description: This routine is called by the SD_TRACE macro for debug 2477 * logging of trace conditions (i.e. function entry/exit). 2478 * 2479 * Arguments: comp - driver component being logged 2480 * dev - pointer to driver info structure 2481 * fmt - trace string and format to be logged 2482 */ 2483 2484 static void 2485 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2486 { 2487 va_list ap; 2488 dev_info_t *dev; 2489 2490 ASSERT(un != NULL); 2491 dev = SD_DEVINFO(un); 2492 ASSERT(dev != NULL); 2493 2494 /* 2495 * Filter messages based on the global component and level masks. 2496 * Also print if un matches the value of sd_debug_un, or if 2497 * sd_debug_un is set to NULL. 2498 */ 2499 if ((sd_component_mask & component) && 2500 (sd_level_mask & SD_LOGMASK_TRACE) && 2501 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2502 mutex_enter(&sd_log_mutex); 2503 va_start(ap, fmt); 2504 (void) vsprintf(sd_log_buf, fmt, ap); 2505 va_end(ap); 2506 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2507 mutex_exit(&sd_log_mutex); 2508 } 2509 #ifdef SD_FAULT_INJECTION 2510 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2511 if (un->sd_injection_mask & component) { 2512 mutex_enter(&sd_log_mutex); 2513 va_start(ap, fmt); 2514 (void) vsprintf(sd_log_buf, fmt, ap); 2515 va_end(ap); 2516 sd_injection_log(sd_log_buf, un); 2517 mutex_exit(&sd_log_mutex); 2518 } 2519 #endif 2520 } 2521 2522 2523 /* 2524 * Function: sdprobe 2525 * 2526 * Description: This is the driver probe(9e) entry point function. 2527 * 2528 * Arguments: devi - opaque device info handle 2529 * 2530 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2531 * DDI_PROBE_FAILURE: If the probe failed. 2532 * DDI_PROBE_PARTIAL: If the instance is not present now, 2533 * but may be present in the future. 2534 */ 2535 2536 static int 2537 sdprobe(dev_info_t *devi) 2538 { 2539 struct scsi_device *devp; 2540 int rval; 2541 int instance; 2542 2543 /* 2544 * if it wasn't for pln, sdprobe could actually be nulldev 2545 * in the "__fibre" case. 2546 */ 2547 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2548 return (DDI_PROBE_DONTCARE); 2549 } 2550 2551 devp = ddi_get_driver_private(devi); 2552 2553 if (devp == NULL) { 2554 /* Ooops... nexus driver is mis-configured... */ 2555 return (DDI_PROBE_FAILURE); 2556 } 2557 2558 instance = ddi_get_instance(devi); 2559 2560 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2561 return (DDI_PROBE_PARTIAL); 2562 } 2563 2564 /* 2565 * Call the SCSA utility probe routine to see if we actually 2566 * have a target at this SCSI nexus. 2567 */ 2568 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2569 case SCSIPROBE_EXISTS: 2570 switch (devp->sd_inq->inq_dtype) { 2571 case DTYPE_DIRECT: 2572 rval = DDI_PROBE_SUCCESS; 2573 break; 2574 case DTYPE_RODIRECT: 2575 /* CDs etc. Can be removable media */ 2576 rval = DDI_PROBE_SUCCESS; 2577 break; 2578 case DTYPE_OPTICAL: 2579 /* 2580 * Rewritable optical driver HP115AA 2581 * Can also be removable media 2582 */ 2583 2584 /* 2585 * Do not attempt to bind to DTYPE_OPTICAL if 2586 * pre solaris 9 sparc sd behavior is required 2587 * 2588 * If first time through and sd_dtype_optical_bind 2589 * has not been set in /etc/system check properties 2590 */ 2591 2592 if (sd_dtype_optical_bind < 0) { 2593 sd_dtype_optical_bind = ddi_prop_get_int 2594 (DDI_DEV_T_ANY, devi, 0, 2595 "optical-device-bind", 1); 2596 } 2597 2598 if (sd_dtype_optical_bind == 0) { 2599 rval = DDI_PROBE_FAILURE; 2600 } else { 2601 rval = DDI_PROBE_SUCCESS; 2602 } 2603 break; 2604 2605 case DTYPE_NOTPRESENT: 2606 default: 2607 rval = DDI_PROBE_FAILURE; 2608 break; 2609 } 2610 break; 2611 default: 2612 rval = DDI_PROBE_PARTIAL; 2613 break; 2614 } 2615 2616 /* 2617 * This routine checks for resource allocation prior to freeing, 2618 * so it will take care of the "smart probing" case where a 2619 * scsi_probe() may or may not have been issued and will *not* 2620 * free previously-freed resources. 2621 */ 2622 scsi_unprobe(devp); 2623 return (rval); 2624 } 2625 2626 2627 /* 2628 * Function: sdinfo 2629 * 2630 * Description: This is the driver getinfo(9e) entry point function. 2631 * Given the device number, return the devinfo pointer from 2632 * the scsi_device structure or the instance number 2633 * associated with the dev_t. 2634 * 2635 * Arguments: dip - pointer to device info structure 2636 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2637 * DDI_INFO_DEVT2INSTANCE) 2638 * arg - driver dev_t 2639 * resultp - user buffer for request response 2640 * 2641 * Return Code: DDI_SUCCESS 2642 * DDI_FAILURE 2643 */ 2644 /* ARGSUSED */ 2645 static int 2646 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2647 { 2648 struct sd_lun *un; 2649 dev_t dev; 2650 int instance; 2651 int error; 2652 2653 switch (infocmd) { 2654 case DDI_INFO_DEVT2DEVINFO: 2655 dev = (dev_t)arg; 2656 instance = SDUNIT(dev); 2657 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2658 return (DDI_FAILURE); 2659 } 2660 *result = (void *) SD_DEVINFO(un); 2661 error = DDI_SUCCESS; 2662 break; 2663 case DDI_INFO_DEVT2INSTANCE: 2664 dev = (dev_t)arg; 2665 instance = SDUNIT(dev); 2666 *result = (void *)(uintptr_t)instance; 2667 error = DDI_SUCCESS; 2668 break; 2669 default: 2670 error = DDI_FAILURE; 2671 } 2672 return (error); 2673 } 2674 2675 /* 2676 * Function: sd_prop_op 2677 * 2678 * Description: This is the driver prop_op(9e) entry point function. 2679 * Return the number of blocks for the partition in question 2680 * or forward the request to the property facilities. 2681 * 2682 * Arguments: dev - device number 2683 * dip - pointer to device info structure 2684 * prop_op - property operator 2685 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2686 * name - pointer to property name 2687 * valuep - pointer or address of the user buffer 2688 * lengthp - property length 2689 * 2690 * Return Code: DDI_PROP_SUCCESS 2691 * DDI_PROP_NOT_FOUND 2692 * DDI_PROP_UNDEFINED 2693 * DDI_PROP_NO_MEMORY 2694 * DDI_PROP_BUF_TOO_SMALL 2695 */ 2696 2697 static int 2698 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2699 char *name, caddr_t valuep, int *lengthp) 2700 { 2701 int instance = ddi_get_instance(dip); 2702 struct sd_lun *un; 2703 uint64_t nblocks64; 2704 2705 /* 2706 * Our dynamic properties are all device specific and size oriented. 2707 * Requests issued under conditions where size is valid are passed 2708 * to ddi_prop_op_nblocks with the size information, otherwise the 2709 * request is passed to ddi_prop_op. Size depends on valid geometry. 2710 */ 2711 un = ddi_get_soft_state(sd_state, instance); 2712 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2713 (un->un_f_geometry_is_valid == FALSE)) { 2714 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2715 name, valuep, lengthp)); 2716 } else { 2717 /* get nblocks value */ 2718 ASSERT(!mutex_owned(SD_MUTEX(un))); 2719 mutex_enter(SD_MUTEX(un)); 2720 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2721 mutex_exit(SD_MUTEX(un)); 2722 2723 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2724 name, valuep, lengthp, nblocks64)); 2725 } 2726 } 2727 2728 /* 2729 * The following functions are for smart probing: 2730 * sd_scsi_probe_cache_init() 2731 * sd_scsi_probe_cache_fini() 2732 * sd_scsi_clear_probe_cache() 2733 * sd_scsi_probe_with_cache() 2734 */ 2735 2736 /* 2737 * Function: sd_scsi_probe_cache_init 2738 * 2739 * Description: Initializes the probe response cache mutex and head pointer. 2740 * 2741 * Context: Kernel thread context 2742 */ 2743 2744 static void 2745 sd_scsi_probe_cache_init(void) 2746 { 2747 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2748 sd_scsi_probe_cache_head = NULL; 2749 } 2750 2751 2752 /* 2753 * Function: sd_scsi_probe_cache_fini 2754 * 2755 * Description: Frees all resources associated with the probe response cache. 2756 * 2757 * Context: Kernel thread context 2758 */ 2759 2760 static void 2761 sd_scsi_probe_cache_fini(void) 2762 { 2763 struct sd_scsi_probe_cache *cp; 2764 struct sd_scsi_probe_cache *ncp; 2765 2766 /* Clean up our smart probing linked list */ 2767 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2768 ncp = cp->next; 2769 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2770 } 2771 sd_scsi_probe_cache_head = NULL; 2772 mutex_destroy(&sd_scsi_probe_cache_mutex); 2773 } 2774 2775 2776 /* 2777 * Function: sd_scsi_clear_probe_cache 2778 * 2779 * Description: This routine clears the probe response cache. This is 2780 * done when open() returns ENXIO so that when deferred 2781 * attach is attempted (possibly after a device has been 2782 * turned on) we will retry the probe. Since we don't know 2783 * which target we failed to open, we just clear the 2784 * entire cache. 2785 * 2786 * Context: Kernel thread context 2787 */ 2788 2789 static void 2790 sd_scsi_clear_probe_cache(void) 2791 { 2792 struct sd_scsi_probe_cache *cp; 2793 int i; 2794 2795 mutex_enter(&sd_scsi_probe_cache_mutex); 2796 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2797 /* 2798 * Reset all entries to SCSIPROBE_EXISTS. This will 2799 * force probing to be performed the next time 2800 * sd_scsi_probe_with_cache is called. 2801 */ 2802 for (i = 0; i < NTARGETS_WIDE; i++) { 2803 cp->cache[i] = SCSIPROBE_EXISTS; 2804 } 2805 } 2806 mutex_exit(&sd_scsi_probe_cache_mutex); 2807 } 2808 2809 2810 /* 2811 * Function: sd_scsi_probe_with_cache 2812 * 2813 * Description: This routine implements support for a scsi device probe 2814 * with cache. The driver maintains a cache of the target 2815 * responses to scsi probes. If we get no response from a 2816 * target during a probe inquiry, we remember that, and we 2817 * avoid additional calls to scsi_probe on non-zero LUNs 2818 * on the same target until the cache is cleared. By doing 2819 * so we avoid the 1/4 sec selection timeout for nonzero 2820 * LUNs. lun0 of a target is always probed. 2821 * 2822 * Arguments: devp - Pointer to a scsi_device(9S) structure 2823 * waitfunc - indicates what the allocator routines should 2824 * do when resources are not available. This value 2825 * is passed on to scsi_probe() when that routine 2826 * is called. 2827 * 2828 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2829 * otherwise the value returned by scsi_probe(9F). 2830 * 2831 * Context: Kernel thread context 2832 */ 2833 2834 static int 2835 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2836 { 2837 struct sd_scsi_probe_cache *cp; 2838 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2839 int lun, tgt; 2840 2841 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2842 SCSI_ADDR_PROP_LUN, 0); 2843 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2844 SCSI_ADDR_PROP_TARGET, -1); 2845 2846 /* Make sure caching enabled and target in range */ 2847 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2848 /* do it the old way (no cache) */ 2849 return (scsi_probe(devp, waitfn)); 2850 } 2851 2852 mutex_enter(&sd_scsi_probe_cache_mutex); 2853 2854 /* Find the cache for this scsi bus instance */ 2855 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2856 if (cp->pdip == pdip) { 2857 break; 2858 } 2859 } 2860 2861 /* If we can't find a cache for this pdip, create one */ 2862 if (cp == NULL) { 2863 int i; 2864 2865 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2866 KM_SLEEP); 2867 cp->pdip = pdip; 2868 cp->next = sd_scsi_probe_cache_head; 2869 sd_scsi_probe_cache_head = cp; 2870 for (i = 0; i < NTARGETS_WIDE; i++) { 2871 cp->cache[i] = SCSIPROBE_EXISTS; 2872 } 2873 } 2874 2875 mutex_exit(&sd_scsi_probe_cache_mutex); 2876 2877 /* Recompute the cache for this target if LUN zero */ 2878 if (lun == 0) { 2879 cp->cache[tgt] = SCSIPROBE_EXISTS; 2880 } 2881 2882 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2883 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2884 return (SCSIPROBE_NORESP); 2885 } 2886 2887 /* Do the actual probe; save & return the result */ 2888 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2889 } 2890 2891 2892 /* 2893 * Function: sd_scsi_target_lun_init 2894 * 2895 * Description: Initializes the attached lun chain mutex and head pointer. 2896 * 2897 * Context: Kernel thread context 2898 */ 2899 2900 static void 2901 sd_scsi_target_lun_init(void) 2902 { 2903 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2904 sd_scsi_target_lun_head = NULL; 2905 } 2906 2907 2908 /* 2909 * Function: sd_scsi_target_lun_fini 2910 * 2911 * Description: Frees all resources associated with the attached lun 2912 * chain 2913 * 2914 * Context: Kernel thread context 2915 */ 2916 2917 static void 2918 sd_scsi_target_lun_fini(void) 2919 { 2920 struct sd_scsi_hba_tgt_lun *cp; 2921 struct sd_scsi_hba_tgt_lun *ncp; 2922 2923 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2924 ncp = cp->next; 2925 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2926 } 2927 sd_scsi_target_lun_head = NULL; 2928 mutex_destroy(&sd_scsi_target_lun_mutex); 2929 } 2930 2931 2932 /* 2933 * Function: sd_scsi_get_target_lun_count 2934 * 2935 * Description: This routine will check in the attached lun chain to see 2936 * how many luns are attached on the required SCSI controller 2937 * and target. Currently, some capabilities like tagged queue 2938 * are supported per target based by HBA. So all luns in a 2939 * target have the same capabilities. Based on this assumption, 2940 * sd should only set these capabilities once per target. This 2941 * function is called when sd needs to decide how many luns 2942 * already attached on a target. 2943 * 2944 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2945 * controller device. 2946 * target - The target ID on the controller's SCSI bus. 2947 * 2948 * Return Code: The number of luns attached on the required target and 2949 * controller. 2950 * -1 if target ID is not in parallel SCSI scope or the given 2951 * dip is not in the chain. 2952 * 2953 * Context: Kernel thread context 2954 */ 2955 2956 static int 2957 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2958 { 2959 struct sd_scsi_hba_tgt_lun *cp; 2960 2961 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2962 return (-1); 2963 } 2964 2965 mutex_enter(&sd_scsi_target_lun_mutex); 2966 2967 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2968 if (cp->pdip == dip) { 2969 break; 2970 } 2971 } 2972 2973 mutex_exit(&sd_scsi_target_lun_mutex); 2974 2975 if (cp == NULL) { 2976 return (-1); 2977 } 2978 2979 return (cp->nlun[target]); 2980 } 2981 2982 2983 /* 2984 * Function: sd_scsi_update_lun_on_target 2985 * 2986 * Description: This routine is used to update the attached lun chain when a 2987 * lun is attached or detached on a target. 2988 * 2989 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2990 * controller device. 2991 * target - The target ID on the controller's SCSI bus. 2992 * flag - Indicate the lun is attached or detached. 2993 * 2994 * Context: Kernel thread context 2995 */ 2996 2997 static void 2998 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 2999 { 3000 struct sd_scsi_hba_tgt_lun *cp; 3001 3002 mutex_enter(&sd_scsi_target_lun_mutex); 3003 3004 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3005 if (cp->pdip == dip) { 3006 break; 3007 } 3008 } 3009 3010 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3011 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3012 KM_SLEEP); 3013 cp->pdip = dip; 3014 cp->next = sd_scsi_target_lun_head; 3015 sd_scsi_target_lun_head = cp; 3016 } 3017 3018 mutex_exit(&sd_scsi_target_lun_mutex); 3019 3020 if (cp != NULL) { 3021 if (flag == SD_SCSI_LUN_ATTACH) { 3022 cp->nlun[target] ++; 3023 } else { 3024 cp->nlun[target] --; 3025 } 3026 } 3027 } 3028 3029 3030 /* 3031 * Function: sd_spin_up_unit 3032 * 3033 * Description: Issues the following commands to spin-up the device: 3034 * START STOP UNIT, and INQUIRY. 3035 * 3036 * Arguments: un - driver soft state (unit) structure 3037 * 3038 * Return Code: 0 - success 3039 * EIO - failure 3040 * EACCES - reservation conflict 3041 * 3042 * Context: Kernel thread context 3043 */ 3044 3045 static int 3046 sd_spin_up_unit(struct sd_lun *un) 3047 { 3048 size_t resid = 0; 3049 int has_conflict = FALSE; 3050 uchar_t *bufaddr; 3051 3052 ASSERT(un != NULL); 3053 3054 /* 3055 * Send a throwaway START UNIT command. 3056 * 3057 * If we fail on this, we don't care presently what precisely 3058 * is wrong. EMC's arrays will also fail this with a check 3059 * condition (0x2/0x4/0x3) if the device is "inactive," but 3060 * we don't want to fail the attach because it may become 3061 * "active" later. 3062 */ 3063 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 3064 == EACCES) 3065 has_conflict = TRUE; 3066 3067 /* 3068 * Send another INQUIRY command to the target. This is necessary for 3069 * non-removable media direct access devices because their INQUIRY data 3070 * may not be fully qualified until they are spun up (perhaps via the 3071 * START command above). Note: This seems to be needed for some 3072 * legacy devices only.) The INQUIRY command should succeed even if a 3073 * Reservation Conflict is present. 3074 */ 3075 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3076 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 3077 kmem_free(bufaddr, SUN_INQSIZE); 3078 return (EIO); 3079 } 3080 3081 /* 3082 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3083 * Note that this routine does not return a failure here even if the 3084 * INQUIRY command did not return any data. This is a legacy behavior. 3085 */ 3086 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3087 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3088 } 3089 3090 kmem_free(bufaddr, SUN_INQSIZE); 3091 3092 /* If we hit a reservation conflict above, tell the caller. */ 3093 if (has_conflict == TRUE) { 3094 return (EACCES); 3095 } 3096 3097 return (0); 3098 } 3099 3100 #ifdef _LP64 3101 /* 3102 * Function: sd_enable_descr_sense 3103 * 3104 * Description: This routine attempts to select descriptor sense format 3105 * using the Control mode page. Devices that support 64 bit 3106 * LBAs (for >2TB luns) should also implement descriptor 3107 * sense data so we will call this function whenever we see 3108 * a lun larger than 2TB. If for some reason the device 3109 * supports 64 bit LBAs but doesn't support descriptor sense 3110 * presumably the mode select will fail. Everything will 3111 * continue to work normally except that we will not get 3112 * complete sense data for commands that fail with an LBA 3113 * larger than 32 bits. 3114 * 3115 * Arguments: un - driver soft state (unit) structure 3116 * 3117 * Context: Kernel thread context only 3118 */ 3119 3120 static void 3121 sd_enable_descr_sense(struct sd_lun *un) 3122 { 3123 uchar_t *header; 3124 struct mode_control_scsi3 *ctrl_bufp; 3125 size_t buflen; 3126 size_t bd_len; 3127 3128 /* 3129 * Read MODE SENSE page 0xA, Control Mode Page 3130 */ 3131 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3132 sizeof (struct mode_control_scsi3); 3133 header = kmem_zalloc(buflen, KM_SLEEP); 3134 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3135 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3136 SD_ERROR(SD_LOG_COMMON, un, 3137 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3138 goto eds_exit; 3139 } 3140 3141 /* 3142 * Determine size of Block Descriptors in order to locate 3143 * the mode page data. ATAPI devices return 0, SCSI devices 3144 * should return MODE_BLK_DESC_LENGTH. 3145 */ 3146 bd_len = ((struct mode_header *)header)->bdesc_length; 3147 3148 ctrl_bufp = (struct mode_control_scsi3 *) 3149 (header + MODE_HEADER_LENGTH + bd_len); 3150 3151 /* 3152 * Clear PS bit for MODE SELECT 3153 */ 3154 ctrl_bufp->mode_page.ps = 0; 3155 3156 /* 3157 * Set D_SENSE to enable descriptor sense format. 3158 */ 3159 ctrl_bufp->d_sense = 1; 3160 3161 /* 3162 * Use MODE SELECT to commit the change to the D_SENSE bit 3163 */ 3164 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3165 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3166 SD_INFO(SD_LOG_COMMON, un, 3167 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3168 goto eds_exit; 3169 } 3170 3171 eds_exit: 3172 kmem_free(header, buflen); 3173 } 3174 3175 /* 3176 * Function: sd_reenable_dsense_task 3177 * 3178 * Description: Re-enable descriptor sense after device or bus reset 3179 * 3180 * Context: Executes in a taskq() thread context 3181 */ 3182 static void 3183 sd_reenable_dsense_task(void *arg) 3184 { 3185 struct sd_lun *un = arg; 3186 3187 ASSERT(un != NULL); 3188 sd_enable_descr_sense(un); 3189 } 3190 #endif /* _LP64 */ 3191 3192 /* 3193 * Function: sd_set_mmc_caps 3194 * 3195 * Description: This routine determines if the device is MMC compliant and if 3196 * the device supports CDDA via a mode sense of the CDVD 3197 * capabilities mode page. Also checks if the device is a 3198 * dvdram writable device. 3199 * 3200 * Arguments: un - driver soft state (unit) structure 3201 * 3202 * Context: Kernel thread context only 3203 */ 3204 3205 static void 3206 sd_set_mmc_caps(struct sd_lun *un) 3207 { 3208 struct mode_header_grp2 *sense_mhp; 3209 uchar_t *sense_page; 3210 caddr_t buf; 3211 int bd_len; 3212 int status; 3213 struct uscsi_cmd com; 3214 int rtn; 3215 uchar_t *out_data_rw, *out_data_hd; 3216 uchar_t *rqbuf_rw, *rqbuf_hd; 3217 3218 ASSERT(un != NULL); 3219 3220 /* 3221 * The flags which will be set in this function are - mmc compliant, 3222 * dvdram writable device, cdda support. Initialize them to FALSE 3223 * and if a capability is detected - it will be set to TRUE. 3224 */ 3225 un->un_f_mmc_cap = FALSE; 3226 un->un_f_dvdram_writable_device = FALSE; 3227 un->un_f_cfg_cdda = FALSE; 3228 3229 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3230 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3231 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3232 3233 if (status != 0) { 3234 /* command failed; just return */ 3235 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3236 return; 3237 } 3238 /* 3239 * If the mode sense request for the CDROM CAPABILITIES 3240 * page (0x2A) succeeds the device is assumed to be MMC. 3241 */ 3242 un->un_f_mmc_cap = TRUE; 3243 3244 /* Get to the page data */ 3245 sense_mhp = (struct mode_header_grp2 *)buf; 3246 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3247 sense_mhp->bdesc_length_lo; 3248 if (bd_len > MODE_BLK_DESC_LENGTH) { 3249 /* 3250 * We did not get back the expected block descriptor 3251 * length so we cannot determine if the device supports 3252 * CDDA. However, we still indicate the device is MMC 3253 * according to the successful response to the page 3254 * 0x2A mode sense request. 3255 */ 3256 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3257 "sd_set_mmc_caps: Mode Sense returned " 3258 "invalid block descriptor length\n"); 3259 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3260 return; 3261 } 3262 3263 /* See if read CDDA is supported */ 3264 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3265 bd_len); 3266 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3267 3268 /* See if writing DVD RAM is supported. */ 3269 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3270 if (un->un_f_dvdram_writable_device == TRUE) { 3271 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3272 return; 3273 } 3274 3275 /* 3276 * If the device presents DVD or CD capabilities in the mode 3277 * page, we can return here since a RRD will not have 3278 * these capabilities. 3279 */ 3280 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3281 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3282 return; 3283 } 3284 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3285 3286 /* 3287 * If un->un_f_dvdram_writable_device is still FALSE, 3288 * check for a Removable Rigid Disk (RRD). A RRD 3289 * device is identified by the features RANDOM_WRITABLE and 3290 * HARDWARE_DEFECT_MANAGEMENT. 3291 */ 3292 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3293 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3294 3295 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3296 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3297 RANDOM_WRITABLE); 3298 if (rtn != 0) { 3299 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3300 kmem_free(rqbuf_rw, SENSE_LENGTH); 3301 return; 3302 } 3303 3304 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3305 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3306 3307 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3308 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3309 HARDWARE_DEFECT_MANAGEMENT); 3310 if (rtn == 0) { 3311 /* 3312 * We have good information, check for random writable 3313 * and hardware defect features. 3314 */ 3315 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3316 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3317 un->un_f_dvdram_writable_device = TRUE; 3318 } 3319 } 3320 3321 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3322 kmem_free(rqbuf_rw, SENSE_LENGTH); 3323 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3324 kmem_free(rqbuf_hd, SENSE_LENGTH); 3325 } 3326 3327 /* 3328 * Function: sd_check_for_writable_cd 3329 * 3330 * Description: This routine determines if the media in the device is 3331 * writable or not. It uses the get configuration command (0x46) 3332 * to determine if the media is writable 3333 * 3334 * Arguments: un - driver soft state (unit) structure 3335 * 3336 * Context: Never called at interrupt context. 3337 */ 3338 3339 static void 3340 sd_check_for_writable_cd(struct sd_lun *un) 3341 { 3342 struct uscsi_cmd com; 3343 uchar_t *out_data; 3344 uchar_t *rqbuf; 3345 int rtn; 3346 uchar_t *out_data_rw, *out_data_hd; 3347 uchar_t *rqbuf_rw, *rqbuf_hd; 3348 struct mode_header_grp2 *sense_mhp; 3349 uchar_t *sense_page; 3350 caddr_t buf; 3351 int bd_len; 3352 int status; 3353 3354 ASSERT(un != NULL); 3355 ASSERT(mutex_owned(SD_MUTEX(un))); 3356 3357 /* 3358 * Initialize the writable media to false, if configuration info. 3359 * tells us otherwise then only we will set it. 3360 */ 3361 un->un_f_mmc_writable_media = FALSE; 3362 mutex_exit(SD_MUTEX(un)); 3363 3364 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3365 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3366 3367 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3368 out_data, SD_PROFILE_HEADER_LEN); 3369 3370 mutex_enter(SD_MUTEX(un)); 3371 if (rtn == 0) { 3372 /* 3373 * We have good information, check for writable DVD. 3374 */ 3375 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3376 un->un_f_mmc_writable_media = TRUE; 3377 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3378 kmem_free(rqbuf, SENSE_LENGTH); 3379 return; 3380 } 3381 } 3382 3383 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3384 kmem_free(rqbuf, SENSE_LENGTH); 3385 3386 /* 3387 * Determine if this is a RRD type device. 3388 */ 3389 mutex_exit(SD_MUTEX(un)); 3390 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3391 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3392 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3393 mutex_enter(SD_MUTEX(un)); 3394 if (status != 0) { 3395 /* command failed; just return */ 3396 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3397 return; 3398 } 3399 3400 /* Get to the page data */ 3401 sense_mhp = (struct mode_header_grp2 *)buf; 3402 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3403 if (bd_len > MODE_BLK_DESC_LENGTH) { 3404 /* 3405 * We did not get back the expected block descriptor length so 3406 * we cannot check the mode page. 3407 */ 3408 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3409 "sd_check_for_writable_cd: Mode Sense returned " 3410 "invalid block descriptor length\n"); 3411 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3412 return; 3413 } 3414 3415 /* 3416 * If the device presents DVD or CD capabilities in the mode 3417 * page, we can return here since a RRD device will not have 3418 * these capabilities. 3419 */ 3420 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3421 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3422 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3423 return; 3424 } 3425 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3426 3427 /* 3428 * If un->un_f_mmc_writable_media is still FALSE, 3429 * check for RRD type media. A RRD device is identified 3430 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3431 */ 3432 mutex_exit(SD_MUTEX(un)); 3433 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3434 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3435 3436 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3437 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3438 RANDOM_WRITABLE); 3439 if (rtn != 0) { 3440 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3441 kmem_free(rqbuf_rw, SENSE_LENGTH); 3442 mutex_enter(SD_MUTEX(un)); 3443 return; 3444 } 3445 3446 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3447 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3448 3449 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3450 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3451 HARDWARE_DEFECT_MANAGEMENT); 3452 mutex_enter(SD_MUTEX(un)); 3453 if (rtn == 0) { 3454 /* 3455 * We have good information, check for random writable 3456 * and hardware defect features as current. 3457 */ 3458 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3459 (out_data_rw[10] & 0x1) && 3460 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3461 (out_data_hd[10] & 0x1)) { 3462 un->un_f_mmc_writable_media = TRUE; 3463 } 3464 } 3465 3466 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3467 kmem_free(rqbuf_rw, SENSE_LENGTH); 3468 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3469 kmem_free(rqbuf_hd, SENSE_LENGTH); 3470 } 3471 3472 /* 3473 * Function: sd_read_unit_properties 3474 * 3475 * Description: The following implements a property lookup mechanism. 3476 * Properties for particular disks (keyed on vendor, model 3477 * and rev numbers) are sought in the sd.conf file via 3478 * sd_process_sdconf_file(), and if not found there, are 3479 * looked for in a list hardcoded in this driver via 3480 * sd_process_sdconf_table() Once located the properties 3481 * are used to update the driver unit structure. 3482 * 3483 * Arguments: un - driver soft state (unit) structure 3484 */ 3485 3486 static void 3487 sd_read_unit_properties(struct sd_lun *un) 3488 { 3489 /* 3490 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3491 * the "sd-config-list" property (from the sd.conf file) or if 3492 * there was not a match for the inquiry vid/pid. If this event 3493 * occurs the static driver configuration table is searched for 3494 * a match. 3495 */ 3496 ASSERT(un != NULL); 3497 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3498 sd_process_sdconf_table(un); 3499 } 3500 3501 /* check for LSI device */ 3502 sd_is_lsi(un); 3503 3504 3505 } 3506 3507 3508 /* 3509 * Function: sd_process_sdconf_file 3510 * 3511 * Description: Use ddi_getlongprop to obtain the properties from the 3512 * driver's config file (ie, sd.conf) and update the driver 3513 * soft state structure accordingly. 3514 * 3515 * Arguments: un - driver soft state (unit) structure 3516 * 3517 * Return Code: SD_SUCCESS - The properties were successfully set according 3518 * to the driver configuration file. 3519 * SD_FAILURE - The driver config list was not obtained or 3520 * there was no vid/pid match. This indicates that 3521 * the static config table should be used. 3522 * 3523 * The config file has a property, "sd-config-list", which consists of 3524 * one or more duplets as follows: 3525 * 3526 * sd-config-list= 3527 * <duplet>, 3528 * [<duplet>,] 3529 * [<duplet>]; 3530 * 3531 * The structure of each duplet is as follows: 3532 * 3533 * <duplet>:= <vid+pid>,<data-property-name_list> 3534 * 3535 * The first entry of the duplet is the device ID string (the concatenated 3536 * vid & pid; not to be confused with a device_id). This is defined in 3537 * the same way as in the sd_disk_table. 3538 * 3539 * The second part of the duplet is a string that identifies a 3540 * data-property-name-list. The data-property-name-list is defined as 3541 * follows: 3542 * 3543 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3544 * 3545 * The syntax of <data-property-name> depends on the <version> field. 3546 * 3547 * If version = SD_CONF_VERSION_1 we have the following syntax: 3548 * 3549 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3550 * 3551 * where the prop0 value will be used to set prop0 if bit0 set in the 3552 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3553 * 3554 */ 3555 3556 static int 3557 sd_process_sdconf_file(struct sd_lun *un) 3558 { 3559 char *config_list = NULL; 3560 int config_list_len; 3561 int len; 3562 int dupletlen = 0; 3563 char *vidptr; 3564 int vidlen; 3565 char *dnlist_ptr; 3566 char *dataname_ptr; 3567 int dnlist_len; 3568 int dataname_len; 3569 int *data_list; 3570 int data_list_len; 3571 int rval = SD_FAILURE; 3572 int i; 3573 3574 ASSERT(un != NULL); 3575 3576 /* Obtain the configuration list associated with the .conf file */ 3577 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3578 sd_config_list, (caddr_t)&config_list, &config_list_len) 3579 != DDI_PROP_SUCCESS) { 3580 return (SD_FAILURE); 3581 } 3582 3583 /* 3584 * Compare vids in each duplet to the inquiry vid - if a match is 3585 * made, get the data value and update the soft state structure 3586 * accordingly. 3587 * 3588 * Note: This algorithm is complex and difficult to maintain. It should 3589 * be replaced with a more robust implementation. 3590 */ 3591 for (len = config_list_len, vidptr = config_list; len > 0; 3592 vidptr += dupletlen, len -= dupletlen) { 3593 /* 3594 * Note: The assumption here is that each vid entry is on 3595 * a unique line from its associated duplet. 3596 */ 3597 vidlen = dupletlen = (int)strlen(vidptr); 3598 if ((vidlen == 0) || 3599 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3600 dupletlen++; 3601 continue; 3602 } 3603 3604 /* 3605 * dnlist contains 1 or more blank separated 3606 * data-property-name entries 3607 */ 3608 dnlist_ptr = vidptr + vidlen + 1; 3609 dnlist_len = (int)strlen(dnlist_ptr); 3610 dupletlen += dnlist_len + 2; 3611 3612 /* 3613 * Set a pointer for the first data-property-name 3614 * entry in the list 3615 */ 3616 dataname_ptr = dnlist_ptr; 3617 dataname_len = 0; 3618 3619 /* 3620 * Loop through all data-property-name entries in the 3621 * data-property-name-list setting the properties for each. 3622 */ 3623 while (dataname_len < dnlist_len) { 3624 int version; 3625 3626 /* 3627 * Determine the length of the current 3628 * data-property-name entry by indexing until a 3629 * blank or NULL is encountered. When the space is 3630 * encountered reset it to a NULL for compliance 3631 * with ddi_getlongprop(). 3632 */ 3633 for (i = 0; ((dataname_ptr[i] != ' ') && 3634 (dataname_ptr[i] != '\0')); i++) { 3635 ; 3636 } 3637 3638 dataname_len += i; 3639 /* If not null terminated, Make it so */ 3640 if (dataname_ptr[i] == ' ') { 3641 dataname_ptr[i] = '\0'; 3642 } 3643 dataname_len++; 3644 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3645 "sd_process_sdconf_file: disk:%s, data:%s\n", 3646 vidptr, dataname_ptr); 3647 3648 /* Get the data list */ 3649 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3650 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3651 != DDI_PROP_SUCCESS) { 3652 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3653 "sd_process_sdconf_file: data property (%s)" 3654 " has no value\n", dataname_ptr); 3655 dataname_ptr = dnlist_ptr + dataname_len; 3656 continue; 3657 } 3658 3659 version = data_list[0]; 3660 3661 if (version == SD_CONF_VERSION_1) { 3662 sd_tunables values; 3663 3664 /* Set the properties */ 3665 if (sd_chk_vers1_data(un, data_list[1], 3666 &data_list[2], data_list_len, dataname_ptr) 3667 == SD_SUCCESS) { 3668 sd_get_tunables_from_conf(un, 3669 data_list[1], &data_list[2], 3670 &values); 3671 sd_set_vers1_properties(un, 3672 data_list[1], &values); 3673 rval = SD_SUCCESS; 3674 } else { 3675 rval = SD_FAILURE; 3676 } 3677 } else { 3678 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3679 "data property %s version 0x%x is invalid.", 3680 dataname_ptr, version); 3681 rval = SD_FAILURE; 3682 } 3683 kmem_free(data_list, data_list_len); 3684 dataname_ptr = dnlist_ptr + dataname_len; 3685 } 3686 } 3687 3688 /* free up the memory allocated by ddi_getlongprop */ 3689 if (config_list) { 3690 kmem_free(config_list, config_list_len); 3691 } 3692 3693 return (rval); 3694 } 3695 3696 /* 3697 * Function: sd_get_tunables_from_conf() 3698 * 3699 * 3700 * This function reads the data list from the sd.conf file and pulls 3701 * the values that can have numeric values as arguments and places 3702 * the values in the apropriate sd_tunables member. 3703 * Since the order of the data list members varies across platforms 3704 * This function reads them from the data list in a platform specific 3705 * order and places them into the correct sd_tunable member that is 3706 * a consistant across all platforms. 3707 */ 3708 static void 3709 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3710 sd_tunables *values) 3711 { 3712 int i; 3713 int mask; 3714 3715 bzero(values, sizeof (sd_tunables)); 3716 3717 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3718 3719 mask = 1 << i; 3720 if (mask > flags) { 3721 break; 3722 } 3723 3724 switch (mask & flags) { 3725 case 0: /* This mask bit not set in flags */ 3726 continue; 3727 case SD_CONF_BSET_THROTTLE: 3728 values->sdt_throttle = data_list[i]; 3729 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3730 "sd_get_tunables_from_conf: throttle = %d\n", 3731 values->sdt_throttle); 3732 break; 3733 case SD_CONF_BSET_CTYPE: 3734 values->sdt_ctype = data_list[i]; 3735 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3736 "sd_get_tunables_from_conf: ctype = %d\n", 3737 values->sdt_ctype); 3738 break; 3739 case SD_CONF_BSET_NRR_COUNT: 3740 values->sdt_not_rdy_retries = data_list[i]; 3741 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3742 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3743 values->sdt_not_rdy_retries); 3744 break; 3745 case SD_CONF_BSET_BSY_RETRY_COUNT: 3746 values->sdt_busy_retries = data_list[i]; 3747 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3748 "sd_get_tunables_from_conf: busy_retries = %d\n", 3749 values->sdt_busy_retries); 3750 break; 3751 case SD_CONF_BSET_RST_RETRIES: 3752 values->sdt_reset_retries = data_list[i]; 3753 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3754 "sd_get_tunables_from_conf: reset_retries = %d\n", 3755 values->sdt_reset_retries); 3756 break; 3757 case SD_CONF_BSET_RSV_REL_TIME: 3758 values->sdt_reserv_rel_time = data_list[i]; 3759 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3760 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3761 values->sdt_reserv_rel_time); 3762 break; 3763 case SD_CONF_BSET_MIN_THROTTLE: 3764 values->sdt_min_throttle = data_list[i]; 3765 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3766 "sd_get_tunables_from_conf: min_throttle = %d\n", 3767 values->sdt_min_throttle); 3768 break; 3769 case SD_CONF_BSET_DISKSORT_DISABLED: 3770 values->sdt_disk_sort_dis = data_list[i]; 3771 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3772 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3773 values->sdt_disk_sort_dis); 3774 break; 3775 case SD_CONF_BSET_LUN_RESET_ENABLED: 3776 values->sdt_lun_reset_enable = data_list[i]; 3777 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3778 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3779 "\n", values->sdt_lun_reset_enable); 3780 break; 3781 } 3782 } 3783 } 3784 3785 /* 3786 * Function: sd_process_sdconf_table 3787 * 3788 * Description: Search the static configuration table for a match on the 3789 * inquiry vid/pid and update the driver soft state structure 3790 * according to the table property values for the device. 3791 * 3792 * The form of a configuration table entry is: 3793 * <vid+pid>,<flags>,<property-data> 3794 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3795 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3796 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3797 * 3798 * Arguments: un - driver soft state (unit) structure 3799 */ 3800 3801 static void 3802 sd_process_sdconf_table(struct sd_lun *un) 3803 { 3804 char *id = NULL; 3805 int table_index; 3806 int idlen; 3807 3808 ASSERT(un != NULL); 3809 for (table_index = 0; table_index < sd_disk_table_size; 3810 table_index++) { 3811 id = sd_disk_table[table_index].device_id; 3812 idlen = strlen(id); 3813 if (idlen == 0) { 3814 continue; 3815 } 3816 3817 /* 3818 * The static configuration table currently does not 3819 * implement version 10 properties. Additionally, 3820 * multiple data-property-name entries are not 3821 * implemented in the static configuration table. 3822 */ 3823 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3824 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3825 "sd_process_sdconf_table: disk %s\n", id); 3826 sd_set_vers1_properties(un, 3827 sd_disk_table[table_index].flags, 3828 sd_disk_table[table_index].properties); 3829 break; 3830 } 3831 } 3832 } 3833 3834 3835 /* 3836 * Function: sd_sdconf_id_match 3837 * 3838 * Description: This local function implements a case sensitive vid/pid 3839 * comparison as well as the boundary cases of wild card and 3840 * multiple blanks. 3841 * 3842 * Note: An implicit assumption made here is that the scsi 3843 * inquiry structure will always keep the vid, pid and 3844 * revision strings in consecutive sequence, so they can be 3845 * read as a single string. If this assumption is not the 3846 * case, a separate string, to be used for the check, needs 3847 * to be built with these strings concatenated. 3848 * 3849 * Arguments: un - driver soft state (unit) structure 3850 * id - table or config file vid/pid 3851 * idlen - length of the vid/pid (bytes) 3852 * 3853 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3854 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3855 */ 3856 3857 static int 3858 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3859 { 3860 struct scsi_inquiry *sd_inq; 3861 int rval = SD_SUCCESS; 3862 3863 ASSERT(un != NULL); 3864 sd_inq = un->un_sd->sd_inq; 3865 ASSERT(id != NULL); 3866 3867 /* 3868 * We use the inq_vid as a pointer to a buffer containing the 3869 * vid and pid and use the entire vid/pid length of the table 3870 * entry for the comparison. This works because the inq_pid 3871 * data member follows inq_vid in the scsi_inquiry structure. 3872 */ 3873 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3874 /* 3875 * The user id string is compared to the inquiry vid/pid 3876 * using a case insensitive comparison and ignoring 3877 * multiple spaces. 3878 */ 3879 rval = sd_blank_cmp(un, id, idlen); 3880 if (rval != SD_SUCCESS) { 3881 /* 3882 * User id strings that start and end with a "*" 3883 * are a special case. These do not have a 3884 * specific vendor, and the product string can 3885 * appear anywhere in the 16 byte PID portion of 3886 * the inquiry data. This is a simple strstr() 3887 * type search for the user id in the inquiry data. 3888 */ 3889 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3890 char *pidptr = &id[1]; 3891 int i; 3892 int j; 3893 int pidstrlen = idlen - 2; 3894 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3895 pidstrlen; 3896 3897 if (j < 0) { 3898 return (SD_FAILURE); 3899 } 3900 for (i = 0; i < j; i++) { 3901 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3902 pidptr, pidstrlen) == 0) { 3903 rval = SD_SUCCESS; 3904 break; 3905 } 3906 } 3907 } 3908 } 3909 } 3910 return (rval); 3911 } 3912 3913 3914 /* 3915 * Function: sd_blank_cmp 3916 * 3917 * Description: If the id string starts and ends with a space, treat 3918 * multiple consecutive spaces as equivalent to a single 3919 * space. For example, this causes a sd_disk_table entry 3920 * of " NEC CDROM " to match a device's id string of 3921 * "NEC CDROM". 3922 * 3923 * Note: The success exit condition for this routine is if 3924 * the pointer to the table entry is '\0' and the cnt of 3925 * the inquiry length is zero. This will happen if the inquiry 3926 * string returned by the device is padded with spaces to be 3927 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3928 * SCSI spec states that the inquiry string is to be padded with 3929 * spaces. 3930 * 3931 * Arguments: un - driver soft state (unit) structure 3932 * id - table or config file vid/pid 3933 * idlen - length of the vid/pid (bytes) 3934 * 3935 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3936 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3937 */ 3938 3939 static int 3940 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3941 { 3942 char *p1; 3943 char *p2; 3944 int cnt; 3945 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3946 sizeof (SD_INQUIRY(un)->inq_pid); 3947 3948 ASSERT(un != NULL); 3949 p2 = un->un_sd->sd_inq->inq_vid; 3950 ASSERT(id != NULL); 3951 p1 = id; 3952 3953 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3954 /* 3955 * Note: string p1 is terminated by a NUL but string p2 3956 * isn't. The end of p2 is determined by cnt. 3957 */ 3958 for (;;) { 3959 /* skip over any extra blanks in both strings */ 3960 while ((*p1 != '\0') && (*p1 == ' ')) { 3961 p1++; 3962 } 3963 while ((cnt != 0) && (*p2 == ' ')) { 3964 p2++; 3965 cnt--; 3966 } 3967 3968 /* compare the two strings */ 3969 if ((cnt == 0) || 3970 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3971 break; 3972 } 3973 while ((cnt > 0) && 3974 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3975 p1++; 3976 p2++; 3977 cnt--; 3978 } 3979 } 3980 } 3981 3982 /* return SD_SUCCESS if both strings match */ 3983 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3984 } 3985 3986 3987 /* 3988 * Function: sd_chk_vers1_data 3989 * 3990 * Description: Verify the version 1 device properties provided by the 3991 * user via the configuration file 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 * list_len - length of user provided data 3997 * 3998 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 3999 * SD_FAILURE - Indicates the user provided data is invalid 4000 */ 4001 4002 static int 4003 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4004 int list_len, char *dataname_ptr) 4005 { 4006 int i; 4007 int mask = 1; 4008 int index = 0; 4009 4010 ASSERT(un != NULL); 4011 4012 /* Check for a NULL property name and list */ 4013 if (dataname_ptr == NULL) { 4014 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4015 "sd_chk_vers1_data: NULL data property name."); 4016 return (SD_FAILURE); 4017 } 4018 if (prop_list == NULL) { 4019 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4020 "sd_chk_vers1_data: %s NULL data property list.", 4021 dataname_ptr); 4022 return (SD_FAILURE); 4023 } 4024 4025 /* Display a warning if undefined bits are set in the flags */ 4026 if (flags & ~SD_CONF_BIT_MASK) { 4027 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4028 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4029 "Properties not set.", 4030 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4031 return (SD_FAILURE); 4032 } 4033 4034 /* 4035 * Verify the length of the list by identifying the highest bit set 4036 * in the flags and validating that the property list has a length 4037 * up to the index of this bit. 4038 */ 4039 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4040 if (flags & mask) { 4041 index++; 4042 } 4043 mask = 1 << i; 4044 } 4045 if ((list_len / sizeof (int)) < (index + 2)) { 4046 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4047 "sd_chk_vers1_data: " 4048 "Data property list %s size is incorrect. " 4049 "Properties not set.", dataname_ptr); 4050 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4051 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4052 return (SD_FAILURE); 4053 } 4054 return (SD_SUCCESS); 4055 } 4056 4057 4058 /* 4059 * Function: sd_set_vers1_properties 4060 * 4061 * Description: Set version 1 device properties based on a property list 4062 * retrieved from the driver configuration file or static 4063 * configuration table. Version 1 properties have the format: 4064 * 4065 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4066 * 4067 * where the prop0 value will be used to set prop0 if bit0 4068 * is set in the flags 4069 * 4070 * Arguments: un - driver soft state (unit) structure 4071 * flags - integer mask indicating properties to be set 4072 * prop_list - integer list of property values 4073 */ 4074 4075 static void 4076 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4077 { 4078 ASSERT(un != NULL); 4079 4080 /* 4081 * Set the flag to indicate cache is to be disabled. An attempt 4082 * to disable the cache via sd_cache_control() will be made 4083 * later during attach once the basic initialization is complete. 4084 */ 4085 if (flags & SD_CONF_BSET_NOCACHE) { 4086 un->un_f_opt_disable_cache = TRUE; 4087 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4088 "sd_set_vers1_properties: caching disabled flag set\n"); 4089 } 4090 4091 /* CD-specific configuration parameters */ 4092 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4093 un->un_f_cfg_playmsf_bcd = TRUE; 4094 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4095 "sd_set_vers1_properties: playmsf_bcd set\n"); 4096 } 4097 if (flags & SD_CONF_BSET_READSUB_BCD) { 4098 un->un_f_cfg_readsub_bcd = TRUE; 4099 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4100 "sd_set_vers1_properties: readsub_bcd set\n"); 4101 } 4102 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4103 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4104 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4105 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4106 } 4107 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4108 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4109 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4110 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4111 } 4112 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4113 un->un_f_cfg_no_read_header = TRUE; 4114 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4115 "sd_set_vers1_properties: no_read_header set\n"); 4116 } 4117 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4118 un->un_f_cfg_read_cd_xd4 = TRUE; 4119 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4120 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4121 } 4122 4123 /* Support for devices which do not have valid/unique serial numbers */ 4124 if (flags & SD_CONF_BSET_FAB_DEVID) { 4125 un->un_f_opt_fab_devid = TRUE; 4126 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4127 "sd_set_vers1_properties: fab_devid bit set\n"); 4128 } 4129 4130 /* Support for user throttle configuration */ 4131 if (flags & SD_CONF_BSET_THROTTLE) { 4132 ASSERT(prop_list != NULL); 4133 un->un_saved_throttle = un->un_throttle = 4134 prop_list->sdt_throttle; 4135 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4136 "sd_set_vers1_properties: throttle set to %d\n", 4137 prop_list->sdt_throttle); 4138 } 4139 4140 /* Set the per disk retry count according to the conf file or table. */ 4141 if (flags & SD_CONF_BSET_NRR_COUNT) { 4142 ASSERT(prop_list != NULL); 4143 if (prop_list->sdt_not_rdy_retries) { 4144 un->un_notready_retry_count = 4145 prop_list->sdt_not_rdy_retries; 4146 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4147 "sd_set_vers1_properties: not ready retry count" 4148 " set to %d\n", un->un_notready_retry_count); 4149 } 4150 } 4151 4152 /* The controller type is reported for generic disk driver ioctls */ 4153 if (flags & SD_CONF_BSET_CTYPE) { 4154 ASSERT(prop_list != NULL); 4155 switch (prop_list->sdt_ctype) { 4156 case CTYPE_CDROM: 4157 un->un_ctype = prop_list->sdt_ctype; 4158 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4159 "sd_set_vers1_properties: ctype set to " 4160 "CTYPE_CDROM\n"); 4161 break; 4162 case CTYPE_CCS: 4163 un->un_ctype = prop_list->sdt_ctype; 4164 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4165 "sd_set_vers1_properties: ctype set to " 4166 "CTYPE_CCS\n"); 4167 break; 4168 case CTYPE_ROD: /* RW optical */ 4169 un->un_ctype = prop_list->sdt_ctype; 4170 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4171 "sd_set_vers1_properties: ctype set to " 4172 "CTYPE_ROD\n"); 4173 break; 4174 default: 4175 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4176 "sd_set_vers1_properties: Could not set " 4177 "invalid ctype value (%d)", 4178 prop_list->sdt_ctype); 4179 } 4180 } 4181 4182 /* Purple failover timeout */ 4183 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4184 ASSERT(prop_list != NULL); 4185 un->un_busy_retry_count = 4186 prop_list->sdt_busy_retries; 4187 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4188 "sd_set_vers1_properties: " 4189 "busy retry count set to %d\n", 4190 un->un_busy_retry_count); 4191 } 4192 4193 /* Purple reset retry count */ 4194 if (flags & SD_CONF_BSET_RST_RETRIES) { 4195 ASSERT(prop_list != NULL); 4196 un->un_reset_retry_count = 4197 prop_list->sdt_reset_retries; 4198 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4199 "sd_set_vers1_properties: " 4200 "reset retry count set to %d\n", 4201 un->un_reset_retry_count); 4202 } 4203 4204 /* Purple reservation release timeout */ 4205 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4206 ASSERT(prop_list != NULL); 4207 un->un_reserve_release_time = 4208 prop_list->sdt_reserv_rel_time; 4209 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4210 "sd_set_vers1_properties: " 4211 "reservation release timeout set to %d\n", 4212 un->un_reserve_release_time); 4213 } 4214 4215 /* 4216 * Driver flag telling the driver to verify that no commands are pending 4217 * for a device before issuing a Test Unit Ready. This is a workaround 4218 * for a firmware bug in some Seagate eliteI drives. 4219 */ 4220 if (flags & SD_CONF_BSET_TUR_CHECK) { 4221 un->un_f_cfg_tur_check = TRUE; 4222 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4223 "sd_set_vers1_properties: tur queue check set\n"); 4224 } 4225 4226 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4227 un->un_min_throttle = prop_list->sdt_min_throttle; 4228 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4229 "sd_set_vers1_properties: min throttle set to %d\n", 4230 un->un_min_throttle); 4231 } 4232 4233 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4234 un->un_f_disksort_disabled = 4235 (prop_list->sdt_disk_sort_dis != 0) ? 4236 TRUE : FALSE; 4237 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4238 "sd_set_vers1_properties: disksort disabled " 4239 "flag set to %d\n", 4240 prop_list->sdt_disk_sort_dis); 4241 } 4242 4243 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4244 un->un_f_lun_reset_enabled = 4245 (prop_list->sdt_lun_reset_enable != 0) ? 4246 TRUE : FALSE; 4247 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4248 "sd_set_vers1_properties: lun reset enabled " 4249 "flag set to %d\n", 4250 prop_list->sdt_lun_reset_enable); 4251 } 4252 4253 /* 4254 * Validate the throttle values. 4255 * If any of the numbers are invalid, set everything to defaults. 4256 */ 4257 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4258 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4259 (un->un_min_throttle > un->un_throttle)) { 4260 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4261 un->un_min_throttle = sd_min_throttle; 4262 } 4263 } 4264 4265 /* 4266 * Function: sd_is_lsi() 4267 * 4268 * Description: Check for lsi devices, step throught the static device 4269 * table to match vid/pid. 4270 * 4271 * Args: un - ptr to sd_lun 4272 * 4273 * Notes: When creating new LSI property, need to add the new LSI property 4274 * to this function. 4275 */ 4276 static void 4277 sd_is_lsi(struct sd_lun *un) 4278 { 4279 char *id = NULL; 4280 int table_index; 4281 int idlen; 4282 void *prop; 4283 4284 ASSERT(un != NULL); 4285 for (table_index = 0; table_index < sd_disk_table_size; 4286 table_index++) { 4287 id = sd_disk_table[table_index].device_id; 4288 idlen = strlen(id); 4289 if (idlen == 0) { 4290 continue; 4291 } 4292 4293 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4294 prop = sd_disk_table[table_index].properties; 4295 if (prop == &lsi_properties || 4296 prop == &lsi_oem_properties || 4297 prop == &lsi_properties_scsi || 4298 prop == &symbios_properties) { 4299 un->un_f_cfg_is_lsi = TRUE; 4300 } 4301 break; 4302 } 4303 } 4304 } 4305 4306 4307 /* 4308 * The following routines support reading and interpretation of disk labels, 4309 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4310 * fdisk tables. 4311 */ 4312 4313 /* 4314 * Function: sd_validate_geometry 4315 * 4316 * Description: Read the label from the disk (if present). Update the unit's 4317 * geometry and vtoc information from the data in the label. 4318 * Verify that the label is valid. 4319 * 4320 * Arguments: un - driver soft state (unit) structure 4321 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4322 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4323 * to use the USCSI "direct" chain and bypass the normal 4324 * command waitq. 4325 * 4326 * Return Code: 0 - Successful completion 4327 * EINVAL - Invalid value in un->un_tgt_blocksize or 4328 * un->un_blockcount; or label on disk is corrupted 4329 * or unreadable. 4330 * EACCES - Reservation conflict at the device. 4331 * ENOMEM - Resource allocation error 4332 * ENOTSUP - geometry not applicable 4333 * 4334 * Context: Kernel thread only (can sleep). 4335 */ 4336 4337 static int 4338 sd_validate_geometry(struct sd_lun *un, int path_flag) 4339 { 4340 static char labelstring[128]; 4341 static char buf[256]; 4342 char *label = NULL; 4343 int label_error = 0; 4344 int gvalid = un->un_f_geometry_is_valid; 4345 int lbasize; 4346 uint_t capacity; 4347 int count; 4348 4349 ASSERT(un != NULL); 4350 ASSERT(mutex_owned(SD_MUTEX(un))); 4351 4352 /* 4353 * If the required values are not valid, then try getting them 4354 * once via read capacity. If that fails, then fail this call. 4355 * This is necessary with the new mpxio failover behavior in 4356 * the T300 where we can get an attach for the inactive path 4357 * before the active path. The inactive path fails commands with 4358 * sense data of 02,04,88 which happens to the read capacity 4359 * before mpxio has had sufficient knowledge to know if it should 4360 * force a fail over or not. (Which it won't do at attach anyhow). 4361 * If the read capacity at attach time fails, un_tgt_blocksize and 4362 * un_blockcount won't be valid. 4363 */ 4364 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4365 (un->un_f_blockcount_is_valid != TRUE)) { 4366 uint64_t cap; 4367 uint32_t lbasz; 4368 int rval; 4369 4370 mutex_exit(SD_MUTEX(un)); 4371 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4372 &lbasz, SD_PATH_DIRECT); 4373 mutex_enter(SD_MUTEX(un)); 4374 if (rval == 0) { 4375 /* 4376 * The following relies on 4377 * sd_send_scsi_READ_CAPACITY never 4378 * returning 0 for capacity and/or lbasize. 4379 */ 4380 sd_update_block_info(un, lbasz, cap); 4381 } 4382 4383 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4384 (un->un_f_blockcount_is_valid != TRUE)) { 4385 return (EINVAL); 4386 } 4387 } 4388 4389 /* 4390 * Copy the lbasize and capacity so that if they're reset while we're 4391 * not holding the SD_MUTEX, we will continue to use valid values 4392 * after the SD_MUTEX is reacquired. (4119659) 4393 */ 4394 lbasize = un->un_tgt_blocksize; 4395 capacity = un->un_blockcount; 4396 4397 #if defined(_SUNOS_VTOC_16) 4398 /* 4399 * Set up the "whole disk" fdisk partition; this should always 4400 * exist, regardless of whether the disk contains an fdisk table 4401 * or vtoc. 4402 */ 4403 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4404 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4405 #endif 4406 4407 /* 4408 * Refresh the logical and physical geometry caches. 4409 * (data from MODE SENSE format/rigid disk geometry pages, 4410 * and scsi_ifgetcap("geometry"). 4411 */ 4412 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4413 4414 label_error = sd_use_efi(un, path_flag); 4415 if (label_error == 0) { 4416 /* found a valid EFI label */ 4417 SD_TRACE(SD_LOG_IO_PARTITION, un, 4418 "sd_validate_geometry: found EFI label\n"); 4419 un->un_solaris_offset = 0; 4420 un->un_solaris_size = capacity; 4421 return (ENOTSUP); 4422 } 4423 if (un->un_blockcount > DK_MAX_BLOCKS) { 4424 if (label_error == ESRCH) { 4425 /* 4426 * they've configured a LUN over 1TB, but used 4427 * format.dat to restrict format's view of the 4428 * capacity to be under 1TB 4429 */ 4430 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4431 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4432 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4433 "size to be < 1TB or relabel the disk with an EFI label"); 4434 } else { 4435 /* unlabeled disk over 1TB */ 4436 #if defined(__i386) || defined(__amd64) 4437 /* 4438 * Refer to comments on off-by-1 at the head of the file 4439 * A 1TB disk was treated as (1T - 512)B in the past, 4440 * thus, it might have valid solaris partition. We 4441 * will return ENOTSUP later only if this disk has no 4442 * valid solaris partition. 4443 */ 4444 if ((un->un_tgt_blocksize != un->un_sys_blocksize) || 4445 (un->un_blockcount - 1 > DK_MAX_BLOCKS) || 4446 un->un_f_has_removable_media || 4447 un->un_f_is_hotpluggable) 4448 #endif 4449 return (ENOTSUP); 4450 } 4451 } 4452 label_error = 0; 4453 4454 /* 4455 * at this point it is either labeled with a VTOC or it is 4456 * under 1TB (<= 1TB actually for off-by-1) 4457 */ 4458 if (un->un_f_vtoc_label_supported) { 4459 struct dk_label *dkl; 4460 offset_t dkl1; 4461 offset_t label_addr, real_addr; 4462 int rval; 4463 size_t buffer_size; 4464 4465 /* 4466 * Note: This will set up un->un_solaris_size and 4467 * un->un_solaris_offset. 4468 */ 4469 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4470 case SD_CMD_RESERVATION_CONFLICT: 4471 ASSERT(mutex_owned(SD_MUTEX(un))); 4472 return (EACCES); 4473 case SD_CMD_FAILURE: 4474 ASSERT(mutex_owned(SD_MUTEX(un))); 4475 return (ENOMEM); 4476 } 4477 4478 if (un->un_solaris_size <= DK_LABEL_LOC) { 4479 4480 #if defined(__i386) || defined(__amd64) 4481 /* 4482 * Refer to comments on off-by-1 at the head of the file 4483 * This is for 1TB disk only. Since that there is no 4484 * solaris partitions, return ENOTSUP as we do for 4485 * >1TB disk. 4486 */ 4487 if (un->un_blockcount > DK_MAX_BLOCKS) 4488 return (ENOTSUP); 4489 #endif 4490 /* 4491 * Found fdisk table but no Solaris partition entry, 4492 * so don't call sd_uselabel() and don't create 4493 * a default label. 4494 */ 4495 label_error = 0; 4496 un->un_f_geometry_is_valid = TRUE; 4497 goto no_solaris_partition; 4498 } 4499 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4500 4501 #if defined(__i386) || defined(__amd64) 4502 /* 4503 * Refer to comments on off-by-1 at the head of the file 4504 * Now, this 1TB disk has valid solaris partition. It 4505 * must be created by previous sd driver, we have to 4506 * treat it as (1T-512)B. 4507 */ 4508 if (un->un_blockcount > DK_MAX_BLOCKS) { 4509 un->un_f_capacity_adjusted = 1; 4510 un->un_blockcount = DK_MAX_BLOCKS; 4511 un->un_map[P0_RAW_DISK].dkl_nblk = DK_MAX_BLOCKS; 4512 4513 /* 4514 * Refer to sd_read_fdisk, when there is no 4515 * fdisk partition table, un_solaris_size is 4516 * set to disk's capacity. In this case, we 4517 * need to adjust it 4518 */ 4519 if (un->un_solaris_size > DK_MAX_BLOCKS) 4520 un->un_solaris_size = DK_MAX_BLOCKS; 4521 sd_resync_geom_caches(un, DK_MAX_BLOCKS, 4522 lbasize, path_flag); 4523 } 4524 #endif 4525 4526 /* 4527 * sys_blocksize != tgt_blocksize, need to re-adjust 4528 * blkno and save the index to beginning of dk_label 4529 */ 4530 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4531 buffer_size = SD_REQBYTES2TGTBYTES(un, 4532 sizeof (struct dk_label)); 4533 4534 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4535 "label_addr: 0x%x allocation size: 0x%x\n", 4536 label_addr, buffer_size); 4537 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4538 if (dkl == NULL) { 4539 return (ENOMEM); 4540 } 4541 4542 mutex_exit(SD_MUTEX(un)); 4543 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4544 path_flag); 4545 mutex_enter(SD_MUTEX(un)); 4546 4547 switch (rval) { 4548 case 0: 4549 /* 4550 * sd_uselabel will establish that the geometry 4551 * is valid. 4552 * For sys_blocksize != tgt_blocksize, need 4553 * to index into the beginning of dk_label 4554 */ 4555 dkl1 = (daddr_t)dkl 4556 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4557 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4558 path_flag) != SD_LABEL_IS_VALID) { 4559 label_error = EINVAL; 4560 } 4561 break; 4562 case EACCES: 4563 label_error = EACCES; 4564 break; 4565 default: 4566 label_error = EINVAL; 4567 break; 4568 } 4569 4570 kmem_free(dkl, buffer_size); 4571 4572 #if defined(_SUNOS_VTOC_8) 4573 label = (char *)un->un_asciilabel; 4574 #elif defined(_SUNOS_VTOC_16) 4575 label = (char *)un->un_vtoc.v_asciilabel; 4576 #else 4577 #error "No VTOC format defined." 4578 #endif 4579 } 4580 4581 /* 4582 * If a valid label was not found, AND if no reservation conflict 4583 * was detected, then go ahead and create a default label (4069506). 4584 */ 4585 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4586 if (un->un_f_geometry_is_valid == FALSE) { 4587 sd_build_default_label(un); 4588 } 4589 label_error = 0; 4590 } 4591 4592 no_solaris_partition: 4593 if ((!un->un_f_has_removable_media || 4594 (un->un_f_has_removable_media && 4595 un->un_mediastate == DKIO_EJECTED)) && 4596 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4597 /* 4598 * Print out a message indicating who and what we are. 4599 * We do this only when we happen to really validate the 4600 * geometry. We may call sd_validate_geometry() at other 4601 * times, e.g., ioctl()'s like Get VTOC in which case we 4602 * don't want to print the label. 4603 * If the geometry is valid, print the label string, 4604 * else print vendor and product info, if available 4605 */ 4606 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4607 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4608 } else { 4609 mutex_enter(&sd_label_mutex); 4610 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4611 labelstring); 4612 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4613 &labelstring[64]); 4614 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4615 labelstring, &labelstring[64]); 4616 if (un->un_f_blockcount_is_valid == TRUE) { 4617 (void) sprintf(&buf[strlen(buf)], 4618 ", %llu %u byte blocks\n", 4619 (longlong_t)un->un_blockcount, 4620 un->un_tgt_blocksize); 4621 } else { 4622 (void) sprintf(&buf[strlen(buf)], 4623 ", (unknown capacity)\n"); 4624 } 4625 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4626 mutex_exit(&sd_label_mutex); 4627 } 4628 } 4629 4630 #if defined(_SUNOS_VTOC_16) 4631 /* 4632 * If we have valid geometry, set up the remaining fdisk partitions. 4633 * Note that dkl_cylno is not used for the fdisk map entries, so 4634 * we set it to an entirely bogus value. 4635 */ 4636 for (count = 0; count < FD_NUMPART; count++) { 4637 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4638 un->un_map[FDISK_P1 + count].dkl_nblk = 4639 un->un_fmap[count].fmap_nblk; 4640 4641 un->un_offset[FDISK_P1 + count] = 4642 un->un_fmap[count].fmap_start; 4643 } 4644 #endif 4645 4646 for (count = 0; count < NDKMAP; count++) { 4647 #if defined(_SUNOS_VTOC_8) 4648 struct dk_map *lp = &un->un_map[count]; 4649 un->un_offset[count] = 4650 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4651 #elif defined(_SUNOS_VTOC_16) 4652 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4653 4654 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4655 #else 4656 #error "No VTOC format defined." 4657 #endif 4658 } 4659 4660 return (label_error); 4661 } 4662 4663 4664 #if defined(_SUNOS_VTOC_16) 4665 /* 4666 * Macro: MAX_BLKS 4667 * 4668 * This macro is used for table entries where we need to have the largest 4669 * possible sector value for that head & SPT (sectors per track) 4670 * combination. Other entries for some smaller disk sizes are set by 4671 * convention to match those used by X86 BIOS usage. 4672 */ 4673 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4674 4675 /* 4676 * Function: sd_convert_geometry 4677 * 4678 * Description: Convert physical geometry into a dk_geom structure. In 4679 * other words, make sure we don't wrap 16-bit values. 4680 * e.g. converting from geom_cache to dk_geom 4681 * 4682 * Context: Kernel thread only 4683 */ 4684 static void 4685 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4686 { 4687 int i; 4688 static const struct chs_values { 4689 uint_t max_cap; /* Max Capacity for this HS. */ 4690 uint_t nhead; /* Heads to use. */ 4691 uint_t nsect; /* SPT to use. */ 4692 } CHS_values[] = { 4693 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4694 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4695 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4696 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4697 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4698 }; 4699 4700 /* Unlabeled SCSI floppy device */ 4701 if (capacity <= 0x1000) { 4702 un_g->dkg_nhead = 2; 4703 un_g->dkg_ncyl = 80; 4704 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4705 return; 4706 } 4707 4708 /* 4709 * For all devices we calculate cylinders using the 4710 * heads and sectors we assign based on capacity of the 4711 * device. The table is designed to be compatible with the 4712 * way other operating systems lay out fdisk tables for X86 4713 * and to insure that the cylinders never exceed 65535 to 4714 * prevent problems with X86 ioctls that report geometry. 4715 * We use SPT that are multiples of 63, since other OSes that 4716 * are not limited to 16-bits for cylinders stop at 63 SPT 4717 * we make do by using multiples of 63 SPT. 4718 * 4719 * Note than capacities greater than or equal to 1TB will simply 4720 * get the largest geometry from the table. This should be okay 4721 * since disks this large shouldn't be using CHS values anyway. 4722 */ 4723 for (i = 0; CHS_values[i].max_cap < capacity && 4724 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4725 ; 4726 4727 un_g->dkg_nhead = CHS_values[i].nhead; 4728 un_g->dkg_nsect = CHS_values[i].nsect; 4729 } 4730 #endif 4731 4732 4733 /* 4734 * Function: sd_resync_geom_caches 4735 * 4736 * Description: (Re)initialize both geometry caches: the virtual geometry 4737 * information is extracted from the HBA (the "geometry" 4738 * capability), and the physical geometry cache data is 4739 * generated by issuing MODE SENSE commands. 4740 * 4741 * Arguments: un - driver soft state (unit) structure 4742 * capacity - disk capacity in #blocks 4743 * lbasize - disk block size in bytes 4744 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4745 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4746 * to use the USCSI "direct" chain and bypass the normal 4747 * command waitq. 4748 * 4749 * Context: Kernel thread only (can sleep). 4750 */ 4751 4752 static void 4753 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4754 int path_flag) 4755 { 4756 struct geom_cache pgeom; 4757 struct geom_cache *pgeom_p = &pgeom; 4758 int spc; 4759 unsigned short nhead; 4760 unsigned short nsect; 4761 4762 ASSERT(un != NULL); 4763 ASSERT(mutex_owned(SD_MUTEX(un))); 4764 4765 /* 4766 * Ask the controller for its logical geometry. 4767 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4768 * then the lgeom cache will be invalid. 4769 */ 4770 sd_get_virtual_geometry(un, capacity, lbasize); 4771 4772 /* 4773 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4774 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4775 */ 4776 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4777 /* 4778 * Note: Perhaps this needs to be more adaptive? The rationale 4779 * is that, if there's no HBA geometry from the HBA driver, any 4780 * guess is good, since this is the physical geometry. If MODE 4781 * SENSE fails this gives a max cylinder size for non-LBA access 4782 */ 4783 nhead = 255; 4784 nsect = 63; 4785 } else { 4786 nhead = un->un_lgeom.g_nhead; 4787 nsect = un->un_lgeom.g_nsect; 4788 } 4789 4790 if (ISCD(un)) { 4791 pgeom_p->g_nhead = 1; 4792 pgeom_p->g_nsect = nsect * nhead; 4793 } else { 4794 pgeom_p->g_nhead = nhead; 4795 pgeom_p->g_nsect = nsect; 4796 } 4797 4798 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4799 pgeom_p->g_capacity = capacity; 4800 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4801 pgeom_p->g_acyl = 0; 4802 4803 /* 4804 * Retrieve fresh geometry data from the hardware, stash it 4805 * here temporarily before we rebuild the incore label. 4806 * 4807 * We want to use the MODE SENSE commands to derive the 4808 * physical geometry of the device, but if either command 4809 * fails, the logical geometry is used as the fallback for 4810 * disk label geometry. 4811 */ 4812 mutex_exit(SD_MUTEX(un)); 4813 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4814 mutex_enter(SD_MUTEX(un)); 4815 4816 /* 4817 * Now update the real copy while holding the mutex. This 4818 * way the global copy is never in an inconsistent state. 4819 */ 4820 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4821 4822 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4823 "(cached from lgeom)\n"); 4824 SD_INFO(SD_LOG_COMMON, un, 4825 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4826 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4827 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4828 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4829 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4830 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4831 un->un_pgeom.g_rpm); 4832 } 4833 4834 4835 /* 4836 * Function: sd_read_fdisk 4837 * 4838 * Description: utility routine to read the fdisk table. 4839 * 4840 * Arguments: un - driver soft state (unit) structure 4841 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4842 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4843 * to use the USCSI "direct" chain and bypass the normal 4844 * command waitq. 4845 * 4846 * Return Code: SD_CMD_SUCCESS 4847 * SD_CMD_FAILURE 4848 * 4849 * Context: Kernel thread only (can sleep). 4850 */ 4851 /* ARGSUSED */ 4852 static int 4853 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4854 { 4855 #if defined(_NO_FDISK_PRESENT) 4856 4857 un->un_solaris_offset = 0; 4858 un->un_solaris_size = capacity; 4859 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4860 return (SD_CMD_SUCCESS); 4861 4862 #elif defined(_FIRMWARE_NEEDS_FDISK) 4863 4864 struct ipart *fdp; 4865 struct mboot *mbp; 4866 struct ipart fdisk[FD_NUMPART]; 4867 int i; 4868 char sigbuf[2]; 4869 caddr_t bufp; 4870 int uidx; 4871 int rval; 4872 int lba = 0; 4873 uint_t solaris_offset; /* offset to solaris part. */ 4874 daddr_t solaris_size; /* size of solaris partition */ 4875 uint32_t blocksize; 4876 4877 ASSERT(un != NULL); 4878 ASSERT(mutex_owned(SD_MUTEX(un))); 4879 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4880 4881 blocksize = un->un_tgt_blocksize; 4882 4883 /* 4884 * Start off assuming no fdisk table 4885 */ 4886 solaris_offset = 0; 4887 solaris_size = capacity; 4888 4889 mutex_exit(SD_MUTEX(un)); 4890 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4891 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4892 mutex_enter(SD_MUTEX(un)); 4893 4894 if (rval != 0) { 4895 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4896 "sd_read_fdisk: fdisk read err\n"); 4897 kmem_free(bufp, blocksize); 4898 return (SD_CMD_FAILURE); 4899 } 4900 4901 mbp = (struct mboot *)bufp; 4902 4903 /* 4904 * The fdisk table does not begin on a 4-byte boundary within the 4905 * master boot record, so we copy it to an aligned structure to avoid 4906 * alignment exceptions on some processors. 4907 */ 4908 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4909 4910 /* 4911 * Check for lba support before verifying sig; sig might not be 4912 * there, say on a blank disk, but the max_chs mark may still 4913 * be present. 4914 * 4915 * Note: LBA support and BEFs are an x86-only concept but this 4916 * code should work OK on SPARC as well. 4917 */ 4918 4919 /* 4920 * First, check for lba-access-ok on root node (or prom root node) 4921 * if present there, don't need to search fdisk table. 4922 */ 4923 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4924 "lba-access-ok", 0) != 0) { 4925 /* All drives do LBA; don't search fdisk table */ 4926 lba = 1; 4927 } else { 4928 /* Okay, look for mark in fdisk table */ 4929 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4930 /* accumulate "lba" value from all partitions */ 4931 lba = (lba || sd_has_max_chs_vals(fdp)); 4932 } 4933 } 4934 4935 if (lba != 0) { 4936 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4937 4938 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4939 "lba-access-ok", 0) == 0) { 4940 /* not found; create it */ 4941 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4942 "lba-access-ok", (caddr_t)NULL, 0) != 4943 DDI_PROP_SUCCESS) { 4944 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4945 "sd_read_fdisk: Can't create lba property " 4946 "for instance %d\n", 4947 ddi_get_instance(SD_DEVINFO(un))); 4948 } 4949 } 4950 } 4951 4952 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4953 4954 /* 4955 * Endian-independent signature check 4956 */ 4957 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4958 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4959 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4960 "sd_read_fdisk: no fdisk\n"); 4961 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4962 rval = SD_CMD_SUCCESS; 4963 goto done; 4964 } 4965 4966 #ifdef SDDEBUG 4967 if (sd_level_mask & SD_LOGMASK_INFO) { 4968 fdp = fdisk; 4969 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4970 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4971 "numsect sysid bootid\n"); 4972 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4973 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4974 " %d: %8d %8d 0x%08x 0x%08x\n", 4975 i, fdp->relsect, fdp->numsect, 4976 fdp->systid, fdp->bootid); 4977 } 4978 } 4979 #endif 4980 4981 /* 4982 * Try to find the unix partition 4983 */ 4984 uidx = -1; 4985 solaris_offset = 0; 4986 solaris_size = 0; 4987 4988 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4989 int relsect; 4990 int numsect; 4991 4992 if (fdp->numsect == 0) { 4993 un->un_fmap[i].fmap_start = 0; 4994 un->un_fmap[i].fmap_nblk = 0; 4995 continue; 4996 } 4997 4998 /* 4999 * Data in the fdisk table is little-endian. 5000 */ 5001 relsect = LE_32(fdp->relsect); 5002 numsect = LE_32(fdp->numsect); 5003 5004 un->un_fmap[i].fmap_start = relsect; 5005 un->un_fmap[i].fmap_nblk = numsect; 5006 5007 if (fdp->systid != SUNIXOS && 5008 fdp->systid != SUNIXOS2 && 5009 fdp->systid != EFI_PMBR) { 5010 continue; 5011 } 5012 5013 /* 5014 * use the last active solaris partition id found 5015 * (there should only be 1 active partition id) 5016 * 5017 * if there are no active solaris partition id 5018 * then use the first inactive solaris partition id 5019 */ 5020 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 5021 uidx = i; 5022 solaris_offset = relsect; 5023 solaris_size = numsect; 5024 } 5025 } 5026 5027 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 5028 un->un_solaris_offset, un->un_solaris_size); 5029 5030 rval = SD_CMD_SUCCESS; 5031 5032 done: 5033 5034 /* 5035 * Clear the VTOC info, only if the Solaris partition entry 5036 * has moved, changed size, been deleted, or if the size of 5037 * the partition is too small to even fit the label sector. 5038 */ 5039 if ((un->un_solaris_offset != solaris_offset) || 5040 (un->un_solaris_size != solaris_size) || 5041 solaris_size <= DK_LABEL_LOC) { 5042 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 5043 solaris_offset, solaris_size); 5044 bzero(&un->un_g, sizeof (struct dk_geom)); 5045 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5046 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5047 un->un_f_geometry_is_valid = FALSE; 5048 } 5049 un->un_solaris_offset = solaris_offset; 5050 un->un_solaris_size = solaris_size; 5051 kmem_free(bufp, blocksize); 5052 return (rval); 5053 5054 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 5055 #error "fdisk table presence undetermined for this platform." 5056 #endif /* #if defined(_NO_FDISK_PRESENT) */ 5057 } 5058 5059 5060 /* 5061 * Function: sd_get_physical_geometry 5062 * 5063 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 5064 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 5065 * target, and use this information to initialize the physical 5066 * geometry cache specified by pgeom_p. 5067 * 5068 * MODE SENSE is an optional command, so failure in this case 5069 * does not necessarily denote an error. We want to use the 5070 * MODE SENSE commands to derive the physical geometry of the 5071 * device, but if either command fails, the logical geometry is 5072 * used as the fallback for disk label geometry. 5073 * 5074 * This requires that un->un_blockcount and un->un_tgt_blocksize 5075 * have already been initialized for the current target and 5076 * that the current values be passed as args so that we don't 5077 * end up ever trying to use -1 as a valid value. This could 5078 * happen if either value is reset while we're not holding 5079 * the mutex. 5080 * 5081 * Arguments: un - driver soft state (unit) structure 5082 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5083 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5084 * to use the USCSI "direct" chain and bypass the normal 5085 * command waitq. 5086 * 5087 * Context: Kernel thread only (can sleep). 5088 */ 5089 5090 static void 5091 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 5092 int capacity, int lbasize, int path_flag) 5093 { 5094 struct mode_format *page3p; 5095 struct mode_geometry *page4p; 5096 struct mode_header *headerp; 5097 int sector_size; 5098 int nsect; 5099 int nhead; 5100 int ncyl; 5101 int intrlv; 5102 int spc; 5103 int modesense_capacity; 5104 int rpm; 5105 int bd_len; 5106 int mode_header_length; 5107 uchar_t *p3bufp; 5108 uchar_t *p4bufp; 5109 int cdbsize; 5110 5111 ASSERT(un != NULL); 5112 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 5113 5114 if (un->un_f_blockcount_is_valid != TRUE) { 5115 return; 5116 } 5117 5118 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 5119 return; 5120 } 5121 5122 if (lbasize == 0) { 5123 if (ISCD(un)) { 5124 lbasize = 2048; 5125 } else { 5126 lbasize = un->un_sys_blocksize; 5127 } 5128 } 5129 pgeom_p->g_secsize = (unsigned short)lbasize; 5130 5131 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 5132 5133 /* 5134 * Retrieve MODE SENSE page 3 - Format Device Page 5135 */ 5136 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 5137 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 5138 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 5139 != 0) { 5140 SD_ERROR(SD_LOG_COMMON, un, 5141 "sd_get_physical_geometry: mode sense page 3 failed\n"); 5142 goto page3_exit; 5143 } 5144 5145 /* 5146 * Determine size of Block Descriptors in order to locate the mode 5147 * page data. ATAPI devices return 0, SCSI devices should return 5148 * MODE_BLK_DESC_LENGTH. 5149 */ 5150 headerp = (struct mode_header *)p3bufp; 5151 if (un->un_f_cfg_is_atapi == TRUE) { 5152 struct mode_header_grp2 *mhp = 5153 (struct mode_header_grp2 *)headerp; 5154 mode_header_length = MODE_HEADER_LENGTH_GRP2; 5155 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5156 } else { 5157 mode_header_length = MODE_HEADER_LENGTH; 5158 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5159 } 5160 5161 if (bd_len > MODE_BLK_DESC_LENGTH) { 5162 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5163 "received unexpected bd_len of %d, page3\n", bd_len); 5164 goto page3_exit; 5165 } 5166 5167 page3p = (struct mode_format *) 5168 ((caddr_t)headerp + mode_header_length + bd_len); 5169 5170 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5171 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5172 "mode sense pg3 code mismatch %d\n", 5173 page3p->mode_page.code); 5174 goto page3_exit; 5175 } 5176 5177 /* 5178 * Use this physical geometry data only if BOTH MODE SENSE commands 5179 * complete successfully; otherwise, revert to the logical geometry. 5180 * So, we need to save everything in temporary variables. 5181 */ 5182 sector_size = BE_16(page3p->data_bytes_sect); 5183 5184 /* 5185 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5186 */ 5187 if (sector_size == 0) { 5188 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 5189 } else { 5190 sector_size &= ~(un->un_sys_blocksize - 1); 5191 } 5192 5193 nsect = BE_16(page3p->sect_track); 5194 intrlv = BE_16(page3p->interleave); 5195 5196 SD_INFO(SD_LOG_COMMON, un, 5197 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5198 SD_INFO(SD_LOG_COMMON, un, 5199 " mode page: %d; nsect: %d; sector size: %d;\n", 5200 page3p->mode_page.code, nsect, sector_size); 5201 SD_INFO(SD_LOG_COMMON, un, 5202 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5203 BE_16(page3p->track_skew), 5204 BE_16(page3p->cylinder_skew)); 5205 5206 5207 /* 5208 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5209 */ 5210 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5211 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 5212 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 5213 != 0) { 5214 SD_ERROR(SD_LOG_COMMON, un, 5215 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5216 goto page4_exit; 5217 } 5218 5219 /* 5220 * Determine size of Block Descriptors in order to locate the mode 5221 * page data. ATAPI devices return 0, SCSI devices should return 5222 * MODE_BLK_DESC_LENGTH. 5223 */ 5224 headerp = (struct mode_header *)p4bufp; 5225 if (un->un_f_cfg_is_atapi == TRUE) { 5226 struct mode_header_grp2 *mhp = 5227 (struct mode_header_grp2 *)headerp; 5228 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5229 } else { 5230 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5231 } 5232 5233 if (bd_len > MODE_BLK_DESC_LENGTH) { 5234 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5235 "received unexpected bd_len of %d, page4\n", bd_len); 5236 goto page4_exit; 5237 } 5238 5239 page4p = (struct mode_geometry *) 5240 ((caddr_t)headerp + mode_header_length + bd_len); 5241 5242 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5243 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5244 "mode sense pg4 code mismatch %d\n", 5245 page4p->mode_page.code); 5246 goto page4_exit; 5247 } 5248 5249 /* 5250 * Stash the data now, after we know that both commands completed. 5251 */ 5252 5253 mutex_enter(SD_MUTEX(un)); 5254 5255 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5256 spc = nhead * nsect; 5257 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5258 rpm = BE_16(page4p->rpm); 5259 5260 modesense_capacity = spc * ncyl; 5261 5262 SD_INFO(SD_LOG_COMMON, un, 5263 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5264 SD_INFO(SD_LOG_COMMON, un, 5265 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5266 SD_INFO(SD_LOG_COMMON, un, 5267 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5268 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5269 (void *)pgeom_p, capacity); 5270 5271 /* 5272 * Compensate if the drive's geometry is not rectangular, i.e., 5273 * the product of C * H * S returned by MODE SENSE >= that returned 5274 * by read capacity. This is an idiosyncrasy of the original x86 5275 * disk subsystem. 5276 */ 5277 if (modesense_capacity >= capacity) { 5278 SD_INFO(SD_LOG_COMMON, un, 5279 "sd_get_physical_geometry: adjusting acyl; " 5280 "old: %d; new: %d\n", pgeom_p->g_acyl, 5281 (modesense_capacity - capacity + spc - 1) / spc); 5282 if (sector_size != 0) { 5283 /* 1243403: NEC D38x7 drives don't support sec size */ 5284 pgeom_p->g_secsize = (unsigned short)sector_size; 5285 } 5286 pgeom_p->g_nsect = (unsigned short)nsect; 5287 pgeom_p->g_nhead = (unsigned short)nhead; 5288 pgeom_p->g_capacity = capacity; 5289 pgeom_p->g_acyl = 5290 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5291 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5292 } 5293 5294 pgeom_p->g_rpm = (unsigned short)rpm; 5295 pgeom_p->g_intrlv = (unsigned short)intrlv; 5296 5297 SD_INFO(SD_LOG_COMMON, un, 5298 "sd_get_physical_geometry: mode sense geometry:\n"); 5299 SD_INFO(SD_LOG_COMMON, un, 5300 " nsect: %d; sector size: %d; interlv: %d\n", 5301 nsect, sector_size, intrlv); 5302 SD_INFO(SD_LOG_COMMON, un, 5303 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5304 nhead, ncyl, rpm, modesense_capacity); 5305 SD_INFO(SD_LOG_COMMON, un, 5306 "sd_get_physical_geometry: (cached)\n"); 5307 SD_INFO(SD_LOG_COMMON, un, 5308 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5309 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5310 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5311 SD_INFO(SD_LOG_COMMON, un, 5312 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5313 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5314 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5315 5316 mutex_exit(SD_MUTEX(un)); 5317 5318 page4_exit: 5319 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5320 page3_exit: 5321 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5322 } 5323 5324 5325 /* 5326 * Function: sd_get_virtual_geometry 5327 * 5328 * Description: Ask the controller to tell us about the target device. 5329 * 5330 * Arguments: un - pointer to softstate 5331 * capacity - disk capacity in #blocks 5332 * lbasize - disk block size in bytes 5333 * 5334 * Context: Kernel thread only 5335 */ 5336 5337 static void 5338 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5339 { 5340 struct geom_cache *lgeom_p = &un->un_lgeom; 5341 uint_t geombuf; 5342 int spc; 5343 5344 ASSERT(un != NULL); 5345 ASSERT(mutex_owned(SD_MUTEX(un))); 5346 5347 mutex_exit(SD_MUTEX(un)); 5348 5349 /* Set sector size, and total number of sectors */ 5350 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5351 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5352 5353 /* Let the HBA tell us its geometry */ 5354 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5355 5356 mutex_enter(SD_MUTEX(un)); 5357 5358 /* A value of -1 indicates an undefined "geometry" property */ 5359 if (geombuf == (-1)) { 5360 return; 5361 } 5362 5363 /* Initialize the logical geometry cache. */ 5364 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5365 lgeom_p->g_nsect = geombuf & 0xffff; 5366 lgeom_p->g_secsize = un->un_sys_blocksize; 5367 5368 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5369 5370 /* 5371 * Note: The driver originally converted the capacity value from 5372 * target blocks to system blocks. However, the capacity value passed 5373 * to this routine is already in terms of system blocks (this scaling 5374 * is done when the READ CAPACITY command is issued and processed). 5375 * This 'error' may have gone undetected because the usage of g_ncyl 5376 * (which is based upon g_capacity) is very limited within the driver 5377 */ 5378 lgeom_p->g_capacity = capacity; 5379 5380 /* 5381 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5382 * hba may return zero values if the device has been removed. 5383 */ 5384 if (spc == 0) { 5385 lgeom_p->g_ncyl = 0; 5386 } else { 5387 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5388 } 5389 lgeom_p->g_acyl = 0; 5390 5391 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5392 SD_INFO(SD_LOG_COMMON, un, 5393 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5394 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5395 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5396 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5397 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5398 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5399 } 5400 5401 5402 /* 5403 * Function: sd_update_block_info 5404 * 5405 * Description: Calculate a byte count to sector count bitshift value 5406 * from sector size. 5407 * 5408 * Arguments: un: unit struct. 5409 * lbasize: new target sector size 5410 * capacity: new target capacity, ie. block count 5411 * 5412 * Context: Kernel thread context 5413 */ 5414 5415 static void 5416 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5417 { 5418 if (lbasize != 0) { 5419 un->un_tgt_blocksize = lbasize; 5420 un->un_f_tgt_blocksize_is_valid = TRUE; 5421 } 5422 5423 if (capacity != 0) { 5424 un->un_blockcount = capacity; 5425 un->un_f_blockcount_is_valid = TRUE; 5426 } 5427 } 5428 5429 5430 static void 5431 sd_swap_efi_gpt(efi_gpt_t *e) 5432 { 5433 _NOTE(ASSUMING_PROTECTED(*e)) 5434 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5435 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5436 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5437 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5438 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5439 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5440 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5441 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5442 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5443 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5444 e->efi_gpt_NumberOfPartitionEntries = 5445 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5446 e->efi_gpt_SizeOfPartitionEntry = 5447 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5448 e->efi_gpt_PartitionEntryArrayCRC32 = 5449 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5450 } 5451 5452 static void 5453 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5454 { 5455 int i; 5456 5457 _NOTE(ASSUMING_PROTECTED(*p)) 5458 for (i = 0; i < nparts; i++) { 5459 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5460 p[i].efi_gpe_PartitionTypeGUID); 5461 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5462 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5463 /* PartitionAttrs */ 5464 } 5465 } 5466 5467 static int 5468 sd_validate_efi(efi_gpt_t *labp) 5469 { 5470 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5471 return (EINVAL); 5472 /* at least 96 bytes in this version of the spec. */ 5473 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5474 labp->efi_gpt_HeaderSize) 5475 return (EINVAL); 5476 /* this should be 128 bytes */ 5477 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5478 return (EINVAL); 5479 return (0); 5480 } 5481 5482 static int 5483 sd_use_efi(struct sd_lun *un, int path_flag) 5484 { 5485 int i; 5486 int rval = 0; 5487 efi_gpe_t *partitions; 5488 uchar_t *buf; 5489 uint_t lbasize; 5490 uint64_t cap = 0; 5491 uint_t nparts; 5492 diskaddr_t gpe_lba; 5493 struct uuid uuid_type_reserved = EFI_RESERVED; 5494 5495 ASSERT(mutex_owned(SD_MUTEX(un))); 5496 lbasize = un->un_tgt_blocksize; 5497 un->un_reserved = -1; 5498 5499 mutex_exit(SD_MUTEX(un)); 5500 5501 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5502 5503 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5504 rval = EINVAL; 5505 goto done_err; 5506 } 5507 5508 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5509 if (rval) { 5510 goto done_err; 5511 } 5512 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5513 /* not ours */ 5514 rval = ESRCH; 5515 goto done_err; 5516 } 5517 5518 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5519 if (rval) { 5520 goto done_err; 5521 } 5522 sd_swap_efi_gpt((efi_gpt_t *)buf); 5523 5524 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5525 /* 5526 * Couldn't read the primary, try the backup. Our 5527 * capacity at this point could be based on CHS, so 5528 * check what the device reports. 5529 */ 5530 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5531 path_flag); 5532 if (rval) { 5533 goto done_err; 5534 } 5535 5536 /* 5537 * The MMC standard allows READ CAPACITY to be 5538 * inaccurate by a bounded amount (in the interest of 5539 * response latency). As a result, failed READs are 5540 * commonplace (due to the reading of metadata and not 5541 * data). Depending on the per-Vendor/drive Sense data, 5542 * the failed READ can cause many (unnecessary) retries. 5543 */ 5544 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5545 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5546 path_flag)) != 0) { 5547 goto done_err; 5548 } 5549 5550 sd_swap_efi_gpt((efi_gpt_t *)buf); 5551 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5552 5553 /* 5554 * Refer to comments related to off-by-1 at the 5555 * header of this file. Search the next to last 5556 * block for backup EFI label. 5557 */ 5558 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5559 cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5560 path_flag)) != 0) { 5561 goto done_err; 5562 } 5563 sd_swap_efi_gpt((efi_gpt_t *)buf); 5564 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5565 goto done_err; 5566 } 5567 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5568 "primary label corrupt; using backup\n"); 5569 } 5570 5571 if (cap == 0) 5572 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5573 path_flag); 5574 5575 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5576 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5577 5578 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5579 path_flag); 5580 if (rval) { 5581 goto done_err; 5582 } 5583 partitions = (efi_gpe_t *)buf; 5584 5585 if (nparts > MAXPART) { 5586 nparts = MAXPART; 5587 } 5588 sd_swap_efi_gpe(nparts, partitions); 5589 5590 mutex_enter(SD_MUTEX(un)); 5591 5592 /* Fill in partition table. */ 5593 for (i = 0; i < nparts; i++) { 5594 if (partitions->efi_gpe_StartingLBA != 0 || 5595 partitions->efi_gpe_EndingLBA != 0) { 5596 un->un_map[i].dkl_cylno = 5597 partitions->efi_gpe_StartingLBA; 5598 un->un_map[i].dkl_nblk = 5599 partitions->efi_gpe_EndingLBA - 5600 partitions->efi_gpe_StartingLBA + 1; 5601 un->un_offset[i] = 5602 partitions->efi_gpe_StartingLBA; 5603 } 5604 if (un->un_reserved == -1) { 5605 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID, 5606 &uuid_type_reserved, sizeof (struct uuid)) == 0) { 5607 un->un_reserved = i; 5608 } 5609 } 5610 if (i == WD_NODE) { 5611 /* 5612 * minor number 7 corresponds to the whole disk 5613 */ 5614 un->un_map[i].dkl_cylno = 0; 5615 un->un_map[i].dkl_nblk = un->un_blockcount; 5616 un->un_offset[i] = 0; 5617 } 5618 partitions++; 5619 } 5620 un->un_solaris_offset = 0; 5621 un->un_solaris_size = cap; 5622 un->un_f_geometry_is_valid = TRUE; 5623 5624 /* clear the vtoc label */ 5625 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5626 5627 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5628 return (0); 5629 5630 done_err: 5631 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5632 mutex_enter(SD_MUTEX(un)); 5633 /* 5634 * if we didn't find something that could look like a VTOC 5635 * and the disk is over 1TB, we know there isn't a valid label. 5636 * Otherwise let sd_uselabel decide what to do. We only 5637 * want to invalidate this if we're certain the label isn't 5638 * valid because sd_prop_op will now fail, which in turn 5639 * causes things like opens and stats on the partition to fail. 5640 */ 5641 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5642 un->un_f_geometry_is_valid = FALSE; 5643 } 5644 return (rval); 5645 } 5646 5647 5648 /* 5649 * Function: sd_uselabel 5650 * 5651 * Description: Validate the disk label and update the relevant data (geometry, 5652 * partition, vtoc, and capacity data) in the sd_lun struct. 5653 * Marks the geometry of the unit as being valid. 5654 * 5655 * Arguments: un: unit struct. 5656 * dk_label: disk label 5657 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5658 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5659 * to use the USCSI "direct" chain and bypass the normal 5660 * command waitq. 5661 * 5662 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5663 * partition, vtoc, and capacity data are good. 5664 * 5665 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5666 * label; or computed capacity does not jibe with capacity 5667 * reported from the READ CAPACITY command. 5668 * 5669 * Context: Kernel thread only (can sleep). 5670 */ 5671 5672 static int 5673 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5674 { 5675 short *sp; 5676 short sum; 5677 short count; 5678 int label_error = SD_LABEL_IS_VALID; 5679 int i; 5680 int capacity; 5681 int part_end; 5682 int track_capacity; 5683 int err; 5684 #if defined(_SUNOS_VTOC_16) 5685 struct dkl_partition *vpartp; 5686 #endif 5687 ASSERT(un != NULL); 5688 ASSERT(mutex_owned(SD_MUTEX(un))); 5689 5690 /* Validate the magic number of the label. */ 5691 if (labp->dkl_magic != DKL_MAGIC) { 5692 #if defined(__sparc) 5693 if ((un->un_state == SD_STATE_NORMAL) && 5694 un->un_f_vtoc_errlog_supported) { 5695 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5696 "Corrupt label; wrong magic number\n"); 5697 } 5698 #endif 5699 return (SD_LABEL_IS_INVALID); 5700 } 5701 5702 /* Validate the checksum of the label. */ 5703 sp = (short *)labp; 5704 sum = 0; 5705 count = sizeof (struct dk_label) / sizeof (short); 5706 while (count--) { 5707 sum ^= *sp++; 5708 } 5709 5710 if (sum != 0) { 5711 #if defined(_SUNOS_VTOC_16) 5712 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5713 #elif defined(_SUNOS_VTOC_8) 5714 if ((un->un_state == SD_STATE_NORMAL) && 5715 un->un_f_vtoc_errlog_supported) { 5716 #endif 5717 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5718 "Corrupt label - label checksum failed\n"); 5719 } 5720 return (SD_LABEL_IS_INVALID); 5721 } 5722 5723 5724 /* 5725 * Fill in geometry structure with data from label. 5726 */ 5727 bzero(&un->un_g, sizeof (struct dk_geom)); 5728 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5729 un->un_g.dkg_acyl = labp->dkl_acyl; 5730 un->un_g.dkg_bcyl = 0; 5731 un->un_g.dkg_nhead = labp->dkl_nhead; 5732 un->un_g.dkg_nsect = labp->dkl_nsect; 5733 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5734 5735 #if defined(_SUNOS_VTOC_8) 5736 un->un_g.dkg_gap1 = labp->dkl_gap1; 5737 un->un_g.dkg_gap2 = labp->dkl_gap2; 5738 un->un_g.dkg_bhead = labp->dkl_bhead; 5739 #endif 5740 #if defined(_SUNOS_VTOC_16) 5741 un->un_dkg_skew = labp->dkl_skew; 5742 #endif 5743 5744 #if defined(__i386) || defined(__amd64) 5745 un->un_g.dkg_apc = labp->dkl_apc; 5746 #endif 5747 5748 /* 5749 * Currently we rely on the values in the label being accurate. If 5750 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5751 * 5752 * Note: In the future a MODE SENSE may be used to retrieve this data, 5753 * although this command is optional in SCSI-2. 5754 */ 5755 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5756 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5757 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5758 5759 /* 5760 * The Read and Write reinstruct values may not be valid 5761 * for older disks. 5762 */ 5763 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5764 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5765 5766 /* Fill in partition table. */ 5767 #if defined(_SUNOS_VTOC_8) 5768 for (i = 0; i < NDKMAP; i++) { 5769 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5770 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5771 } 5772 #endif 5773 #if defined(_SUNOS_VTOC_16) 5774 vpartp = labp->dkl_vtoc.v_part; 5775 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5776 5777 /* Prevent divide by zero */ 5778 if (track_capacity == 0) { 5779 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5780 "Corrupt label - zero nhead or nsect value\n"); 5781 5782 return (SD_LABEL_IS_INVALID); 5783 } 5784 5785 for (i = 0; i < NDKMAP; i++, vpartp++) { 5786 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5787 un->un_map[i].dkl_nblk = vpartp->p_size; 5788 } 5789 #endif 5790 5791 /* Fill in VTOC Structure. */ 5792 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5793 #if defined(_SUNOS_VTOC_8) 5794 /* 5795 * The 8-slice vtoc does not include the ascii label; save it into 5796 * the device's soft state structure here. 5797 */ 5798 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5799 #endif 5800 5801 /* Now look for a valid capacity. */ 5802 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5803 capacity = (un->un_g.dkg_ncyl * track_capacity); 5804 5805 if (un->un_g.dkg_acyl) { 5806 #if defined(__i386) || defined(__amd64) 5807 /* we may have > 1 alts cylinder */ 5808 capacity += (track_capacity * un->un_g.dkg_acyl); 5809 #else 5810 capacity += track_capacity; 5811 #endif 5812 } 5813 5814 /* 5815 * Force check here to ensure the computed capacity is valid. 5816 * If capacity is zero, it indicates an invalid label and 5817 * we should abort updating the relevant data then. 5818 */ 5819 if (capacity == 0) { 5820 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5821 "Corrupt label - no valid capacity could be retrieved\n"); 5822 5823 return (SD_LABEL_IS_INVALID); 5824 } 5825 5826 /* Mark the geometry as valid. */ 5827 un->un_f_geometry_is_valid = TRUE; 5828 5829 /* 5830 * At this point, un->un_blockcount should contain valid data from 5831 * the READ CAPACITY command. 5832 */ 5833 if (un->un_f_blockcount_is_valid != TRUE) { 5834 /* 5835 * We have a situation where the target didn't give us a good 5836 * READ CAPACITY value, yet there appears to be a valid label. 5837 * In this case, we'll fake the capacity. 5838 */ 5839 un->un_blockcount = capacity; 5840 un->un_f_blockcount_is_valid = TRUE; 5841 goto done; 5842 } 5843 5844 5845 if ((capacity <= un->un_blockcount) || 5846 (un->un_state != SD_STATE_NORMAL)) { 5847 #if defined(_SUNOS_VTOC_8) 5848 /* 5849 * We can't let this happen on drives that are subdivided 5850 * into logical disks (i.e., that have an fdisk table). 5851 * The un_blockcount field should always hold the full media 5852 * size in sectors, period. This code would overwrite 5853 * un_blockcount with the size of the Solaris fdisk partition. 5854 */ 5855 SD_ERROR(SD_LOG_COMMON, un, 5856 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5857 capacity, un->un_blockcount); 5858 un->un_blockcount = capacity; 5859 un->un_f_blockcount_is_valid = TRUE; 5860 #endif /* defined(_SUNOS_VTOC_8) */ 5861 goto done; 5862 } 5863 5864 if (ISCD(un)) { 5865 /* For CDROMs, we trust that the data in the label is OK. */ 5866 #if defined(_SUNOS_VTOC_8) 5867 for (i = 0; i < NDKMAP; i++) { 5868 part_end = labp->dkl_nhead * labp->dkl_nsect * 5869 labp->dkl_map[i].dkl_cylno + 5870 labp->dkl_map[i].dkl_nblk - 1; 5871 5872 if ((labp->dkl_map[i].dkl_nblk) && 5873 (part_end > un->un_blockcount)) { 5874 un->un_f_geometry_is_valid = FALSE; 5875 break; 5876 } 5877 } 5878 #endif 5879 #if defined(_SUNOS_VTOC_16) 5880 vpartp = &(labp->dkl_vtoc.v_part[0]); 5881 for (i = 0; i < NDKMAP; i++, vpartp++) { 5882 part_end = vpartp->p_start + vpartp->p_size; 5883 if ((vpartp->p_size > 0) && 5884 (part_end > un->un_blockcount)) { 5885 un->un_f_geometry_is_valid = FALSE; 5886 break; 5887 } 5888 } 5889 #endif 5890 } else { 5891 uint64_t t_capacity; 5892 uint32_t t_lbasize; 5893 5894 mutex_exit(SD_MUTEX(un)); 5895 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5896 path_flag); 5897 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5898 mutex_enter(SD_MUTEX(un)); 5899 5900 if (err == 0) { 5901 sd_update_block_info(un, t_lbasize, t_capacity); 5902 } 5903 5904 if (capacity > un->un_blockcount) { 5905 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5906 "Corrupt label - bad geometry\n"); 5907 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5908 "Label says %u blocks; Drive says %llu blocks\n", 5909 capacity, (unsigned long long)un->un_blockcount); 5910 un->un_f_geometry_is_valid = FALSE; 5911 label_error = SD_LABEL_IS_INVALID; 5912 } 5913 } 5914 5915 done: 5916 5917 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5918 SD_INFO(SD_LOG_COMMON, un, 5919 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5920 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5921 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5922 SD_INFO(SD_LOG_COMMON, un, 5923 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5924 un->un_tgt_blocksize, un->un_blockcount, 5925 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5926 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5927 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5928 5929 ASSERT(mutex_owned(SD_MUTEX(un))); 5930 5931 return (label_error); 5932 } 5933 5934 5935 /* 5936 * Function: sd_build_default_label 5937 * 5938 * Description: Generate a default label for those devices that do not have 5939 * one, e.g., new media, removable cartridges, etc.. 5940 * 5941 * Context: Kernel thread only 5942 */ 5943 5944 static void 5945 sd_build_default_label(struct sd_lun *un) 5946 { 5947 #if defined(_SUNOS_VTOC_16) 5948 uint_t phys_spc; 5949 uint_t disksize; 5950 struct dk_geom un_g; 5951 uint64_t capacity; 5952 #endif 5953 5954 ASSERT(un != NULL); 5955 ASSERT(mutex_owned(SD_MUTEX(un))); 5956 5957 #if defined(_SUNOS_VTOC_8) 5958 /* 5959 * Note: This is a legacy check for non-removable devices on VTOC_8 5960 * only. This may be a valid check for VTOC_16 as well. 5961 * Once we understand why there is this difference between SPARC and 5962 * x86 platform, we could remove this legacy check. 5963 */ 5964 ASSERT(un->un_f_default_vtoc_supported); 5965 #endif 5966 5967 bzero(&un->un_g, sizeof (struct dk_geom)); 5968 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5969 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5970 5971 #if defined(_SUNOS_VTOC_8) 5972 5973 /* 5974 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5975 * But it is still necessary to set up various geometry information, 5976 * and we are doing this here. 5977 */ 5978 5979 /* 5980 * For the rpm, we use the minimum for the disk. For the head, cyl, 5981 * and number of sector per track, if the capacity <= 1GB, head = 64, 5982 * sect = 32. else head = 255, sect 63 Note: the capacity should be 5983 * equal to C*H*S values. This will cause some truncation of size due 5984 * to round off errors. For CD-ROMs, this truncation can have adverse 5985 * side effects, so returning ncyl and nhead as 1. The nsect will 5986 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 5987 */ 5988 if (ISCD(un)) { 5989 /* 5990 * Preserve the old behavior for non-writable 5991 * medias. Since dkg_nsect is a ushort, it 5992 * will lose bits as cdroms have more than 5993 * 65536 sectors. So if we recalculate 5994 * capacity, it will become much shorter. 5995 * But the dkg_* information is not 5996 * used for CDROMs so it is OK. But for 5997 * Writable CDs we need this information 5998 * to be valid (for newfs say). So we 5999 * make nsect and nhead > 1 that way 6000 * nsect can still stay within ushort limit 6001 * without losing any bits. 6002 */ 6003 if (un->un_f_mmc_writable_media == TRUE) { 6004 un->un_g.dkg_nhead = 64; 6005 un->un_g.dkg_nsect = 32; 6006 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6007 un->un_blockcount = un->un_g.dkg_ncyl * 6008 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6009 } else { 6010 un->un_g.dkg_ncyl = 1; 6011 un->un_g.dkg_nhead = 1; 6012 un->un_g.dkg_nsect = un->un_blockcount; 6013 } 6014 } else { 6015 if (un->un_blockcount <= 0x1000) { 6016 /* unlabeled SCSI floppy device */ 6017 un->un_g.dkg_nhead = 2; 6018 un->un_g.dkg_ncyl = 80; 6019 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 6020 } else if (un->un_blockcount <= 0x200000) { 6021 un->un_g.dkg_nhead = 64; 6022 un->un_g.dkg_nsect = 32; 6023 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6024 } else { 6025 un->un_g.dkg_nhead = 255; 6026 un->un_g.dkg_nsect = 63; 6027 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 6028 } 6029 un->un_blockcount = 6030 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6031 } 6032 6033 un->un_g.dkg_acyl = 0; 6034 un->un_g.dkg_bcyl = 0; 6035 un->un_g.dkg_rpm = 200; 6036 un->un_asciilabel[0] = '\0'; 6037 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 6038 6039 un->un_map[0].dkl_cylno = 0; 6040 un->un_map[0].dkl_nblk = un->un_blockcount; 6041 un->un_map[2].dkl_cylno = 0; 6042 un->un_map[2].dkl_nblk = un->un_blockcount; 6043 6044 #elif defined(_SUNOS_VTOC_16) 6045 6046 if (un->un_solaris_size == 0) { 6047 /* 6048 * Got fdisk table but no solaris entry therefore 6049 * don't create a default label 6050 */ 6051 un->un_f_geometry_is_valid = TRUE; 6052 return; 6053 } 6054 6055 /* 6056 * For CDs we continue to use the physical geometry to calculate 6057 * number of cylinders. All other devices must convert the 6058 * physical geometry (geom_cache) to values that will fit 6059 * in a dk_geom structure. 6060 */ 6061 if (ISCD(un)) { 6062 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 6063 } else { 6064 /* Convert physical geometry to disk geometry */ 6065 bzero(&un_g, sizeof (struct dk_geom)); 6066 6067 /* 6068 * Refer to comments related to off-by-1 at the 6069 * header of this file. 6070 * Before caculating geometry, capacity should be 6071 * decreased by 1. That un_f_capacity_adjusted is 6072 * TRUE means that we are treating a 1TB disk as 6073 * (1T - 512)B. And the capacity of disks is already 6074 * decreased by 1. 6075 */ 6076 if (!un->un_f_capacity_adjusted && 6077 !un->un_f_has_removable_media && 6078 !un->un_f_is_hotpluggable && 6079 un->un_tgt_blocksize == un->un_sys_blocksize) 6080 capacity = un->un_blockcount - 1; 6081 else 6082 capacity = un->un_blockcount; 6083 6084 sd_convert_geometry(capacity, &un_g); 6085 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 6086 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6087 } 6088 6089 ASSERT(phys_spc != 0); 6090 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 6091 un->un_g.dkg_acyl = DK_ACYL; 6092 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 6093 disksize = un->un_g.dkg_ncyl * phys_spc; 6094 6095 if (ISCD(un)) { 6096 /* 6097 * CD's don't use the "heads * sectors * cyls"-type of 6098 * geometry, but instead use the entire capacity of the media. 6099 */ 6100 disksize = un->un_solaris_size; 6101 un->un_g.dkg_nhead = 1; 6102 un->un_g.dkg_nsect = 1; 6103 un->un_g.dkg_rpm = 6104 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 6105 6106 un->un_vtoc.v_part[0].p_start = 0; 6107 un->un_vtoc.v_part[0].p_size = disksize; 6108 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 6109 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 6110 6111 un->un_map[0].dkl_cylno = 0; 6112 un->un_map[0].dkl_nblk = disksize; 6113 un->un_offset[0] = 0; 6114 6115 } else { 6116 /* 6117 * Hard disks and removable media cartridges 6118 */ 6119 un->un_g.dkg_rpm = 6120 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 6121 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 6122 6123 /* Add boot slice */ 6124 un->un_vtoc.v_part[8].p_start = 0; 6125 un->un_vtoc.v_part[8].p_size = phys_spc; 6126 un->un_vtoc.v_part[8].p_tag = V_BOOT; 6127 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 6128 6129 un->un_map[8].dkl_cylno = 0; 6130 un->un_map[8].dkl_nblk = phys_spc; 6131 un->un_offset[8] = 0; 6132 } 6133 6134 un->un_g.dkg_apc = 0; 6135 un->un_vtoc.v_nparts = V_NUMPAR; 6136 un->un_vtoc.v_version = V_VERSION; 6137 6138 /* Add backup slice */ 6139 un->un_vtoc.v_part[2].p_start = 0; 6140 un->un_vtoc.v_part[2].p_size = disksize; 6141 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 6142 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 6143 6144 un->un_map[2].dkl_cylno = 0; 6145 un->un_map[2].dkl_nblk = disksize; 6146 un->un_offset[2] = 0; 6147 6148 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 6149 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 6150 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 6151 6152 #else 6153 #error "No VTOC format defined." 6154 #endif 6155 6156 un->un_g.dkg_read_reinstruct = 0; 6157 un->un_g.dkg_write_reinstruct = 0; 6158 6159 un->un_g.dkg_intrlv = 1; 6160 6161 un->un_vtoc.v_sanity = VTOC_SANE; 6162 6163 un->un_f_geometry_is_valid = TRUE; 6164 6165 SD_INFO(SD_LOG_COMMON, un, 6166 "sd_build_default_label: Default label created: " 6167 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 6168 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 6169 un->un_g.dkg_nsect, un->un_blockcount); 6170 } 6171 6172 6173 #if defined(_FIRMWARE_NEEDS_FDISK) 6174 /* 6175 * Max CHS values, as they are encoded into bytes, for 1022/254/63 6176 */ 6177 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 6178 #define LBA_MAX_CYL (1022 & 0xFF) 6179 #define LBA_MAX_HEAD (254) 6180 6181 6182 /* 6183 * Function: sd_has_max_chs_vals 6184 * 6185 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 6186 * 6187 * Arguments: fdp - ptr to CHS info 6188 * 6189 * Return Code: True or false 6190 * 6191 * Context: Any. 6192 */ 6193 6194 static int 6195 sd_has_max_chs_vals(struct ipart *fdp) 6196 { 6197 return ((fdp->begcyl == LBA_MAX_CYL) && 6198 (fdp->beghead == LBA_MAX_HEAD) && 6199 (fdp->begsect == LBA_MAX_SECT) && 6200 (fdp->endcyl == LBA_MAX_CYL) && 6201 (fdp->endhead == LBA_MAX_HEAD) && 6202 (fdp->endsect == LBA_MAX_SECT)); 6203 } 6204 #endif 6205 6206 6207 /* 6208 * Function: sd_inq_fill 6209 * 6210 * Description: Print a piece of inquiry data, cleaned up for non-printable 6211 * characters and stopping at the first space character after 6212 * the beginning of the passed string; 6213 * 6214 * Arguments: p - source string 6215 * l - maximum length to copy 6216 * s - destination string 6217 * 6218 * Context: Any. 6219 */ 6220 6221 static void 6222 sd_inq_fill(char *p, int l, char *s) 6223 { 6224 unsigned i = 0; 6225 char c; 6226 6227 while (i++ < l) { 6228 if ((c = *p++) < ' ' || c >= 0x7F) { 6229 c = '*'; 6230 } else if (i != 1 && c == ' ') { 6231 break; 6232 } 6233 *s++ = c; 6234 } 6235 *s++ = 0; 6236 } 6237 6238 6239 /* 6240 * Function: sd_register_devid 6241 * 6242 * Description: This routine will obtain the device id information from the 6243 * target, obtain the serial number, and register the device 6244 * id with the ddi framework. 6245 * 6246 * Arguments: devi - the system's dev_info_t for the device. 6247 * un - driver soft state (unit) structure 6248 * reservation_flag - indicates if a reservation conflict 6249 * occurred during attach 6250 * 6251 * Context: Kernel Thread 6252 */ 6253 static void 6254 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 6255 { 6256 int rval = 0; 6257 uchar_t *inq80 = NULL; 6258 size_t inq80_len = MAX_INQUIRY_SIZE; 6259 size_t inq80_resid = 0; 6260 uchar_t *inq83 = NULL; 6261 size_t inq83_len = MAX_INQUIRY_SIZE; 6262 size_t inq83_resid = 0; 6263 6264 ASSERT(un != NULL); 6265 ASSERT(mutex_owned(SD_MUTEX(un))); 6266 ASSERT((SD_DEVINFO(un)) == devi); 6267 6268 /* 6269 * This is the case of antiquated Sun disk drives that have the 6270 * FAB_DEVID property set in the disk_table. These drives 6271 * manage the devid's by storing them in last 2 available sectors 6272 * on the drive and have them fabricated by the ddi layer by calling 6273 * ddi_devid_init and passing the DEVID_FAB flag. 6274 */ 6275 if (un->un_f_opt_fab_devid == TRUE) { 6276 /* 6277 * Depending on EINVAL isn't reliable, since a reserved disk 6278 * may result in invalid geometry, so check to make sure a 6279 * reservation conflict did not occur during attach. 6280 */ 6281 if ((sd_get_devid(un) == EINVAL) && 6282 (reservation_flag != SD_TARGET_IS_RESERVED)) { 6283 /* 6284 * The devid is invalid AND there is no reservation 6285 * conflict. Fabricate a new devid. 6286 */ 6287 (void) sd_create_devid(un); 6288 } 6289 6290 /* Register the devid if it exists */ 6291 if (un->un_devid != NULL) { 6292 (void) ddi_devid_register(SD_DEVINFO(un), 6293 un->un_devid); 6294 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6295 "sd_register_devid: Devid Fabricated\n"); 6296 } 6297 return; 6298 } 6299 6300 /* 6301 * We check the availibility of the World Wide Name (0x83) and Unit 6302 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 6303 * un_vpd_page_mask from them, we decide which way to get the WWN. If 6304 * 0x83 is availible, that is the best choice. Our next choice is 6305 * 0x80. If neither are availible, we munge the devid from the device 6306 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 6307 * to fabricate a devid for non-Sun qualified disks. 6308 */ 6309 if (sd_check_vpd_page_support(un) == 0) { 6310 /* collect page 80 data if available */ 6311 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 6312 6313 mutex_exit(SD_MUTEX(un)); 6314 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 6315 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 6316 0x01, 0x80, &inq80_resid); 6317 6318 if (rval != 0) { 6319 kmem_free(inq80, inq80_len); 6320 inq80 = NULL; 6321 inq80_len = 0; 6322 } 6323 mutex_enter(SD_MUTEX(un)); 6324 } 6325 6326 /* collect page 83 data if available */ 6327 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 6328 mutex_exit(SD_MUTEX(un)); 6329 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6330 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6331 0x01, 0x83, &inq83_resid); 6332 6333 if (rval != 0) { 6334 kmem_free(inq83, inq83_len); 6335 inq83 = NULL; 6336 inq83_len = 0; 6337 } 6338 mutex_enter(SD_MUTEX(un)); 6339 } 6340 } 6341 6342 /* encode best devid possible based on data available */ 6343 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6344 (char *)ddi_driver_name(SD_DEVINFO(un)), 6345 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6346 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6347 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6348 6349 /* devid successfully encoded, register devid */ 6350 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6351 6352 } else { 6353 /* 6354 * Unable to encode a devid based on data available. 6355 * This is not a Sun qualified disk. Older Sun disk 6356 * drives that have the SD_FAB_DEVID property 6357 * set in the disk_table and non Sun qualified 6358 * disks are treated in the same manner. These 6359 * drives manage the devid's by storing them in 6360 * last 2 available sectors on the drive and 6361 * have them fabricated by the ddi layer by 6362 * calling ddi_devid_init and passing the 6363 * DEVID_FAB flag. 6364 * Create a fabricate devid only if there's no 6365 * fabricate devid existed. 6366 */ 6367 if (sd_get_devid(un) == EINVAL) { 6368 (void) sd_create_devid(un); 6369 } 6370 un->un_f_opt_fab_devid = TRUE; 6371 6372 /* Register the devid if it exists */ 6373 if (un->un_devid != NULL) { 6374 (void) ddi_devid_register(SD_DEVINFO(un), 6375 un->un_devid); 6376 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6377 "sd_register_devid: devid fabricated using " 6378 "ddi framework\n"); 6379 } 6380 } 6381 6382 /* clean up resources */ 6383 if (inq80 != NULL) { 6384 kmem_free(inq80, inq80_len); 6385 } 6386 if (inq83 != NULL) { 6387 kmem_free(inq83, inq83_len); 6388 } 6389 } 6390 6391 static daddr_t 6392 sd_get_devid_block(struct sd_lun *un) 6393 { 6394 daddr_t spc, blk, head, cyl; 6395 6396 if ((un->un_f_geometry_is_valid == FALSE) || 6397 (un->un_solaris_size < DK_LABEL_LOC)) 6398 return (-1); 6399 6400 if (un->un_vtoc.v_sanity != VTOC_SANE) { 6401 /* EFI labeled */ 6402 if (un->un_reserved != -1) { 6403 blk = un->un_map[un->un_reserved].dkl_cylno; 6404 } else { 6405 return (-1); 6406 } 6407 } else { 6408 /* SMI labeled */ 6409 /* this geometry doesn't allow us to write a devid */ 6410 if (un->un_g.dkg_acyl < 2) { 6411 return (-1); 6412 } 6413 6414 /* 6415 * Subtract 2 guarantees that the next to last cylinder 6416 * is used 6417 */ 6418 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6419 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6420 head = un->un_g.dkg_nhead - 1; 6421 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6422 (head * un->un_g.dkg_nsect) + 1; 6423 } 6424 return (blk); 6425 } 6426 6427 /* 6428 * Function: sd_get_devid 6429 * 6430 * Description: This routine will return 0 if a valid device id has been 6431 * obtained from the target and stored in the soft state. If a 6432 * valid device id has not been previously read and stored, a 6433 * read attempt will be made. 6434 * 6435 * Arguments: un - driver soft state (unit) structure 6436 * 6437 * Return Code: 0 if we successfully get the device id 6438 * 6439 * Context: Kernel Thread 6440 */ 6441 6442 static int 6443 sd_get_devid(struct sd_lun *un) 6444 { 6445 struct dk_devid *dkdevid; 6446 ddi_devid_t tmpid; 6447 uint_t *ip; 6448 size_t sz; 6449 daddr_t blk; 6450 int status; 6451 int chksum; 6452 int i; 6453 size_t buffer_size; 6454 6455 ASSERT(un != NULL); 6456 ASSERT(mutex_owned(SD_MUTEX(un))); 6457 6458 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6459 un); 6460 6461 if (un->un_devid != NULL) { 6462 return (0); 6463 } 6464 6465 blk = sd_get_devid_block(un); 6466 if (blk < 0) 6467 return (EINVAL); 6468 6469 /* 6470 * Read and verify device id, stored in the reserved cylinders at the 6471 * end of the disk. Backup label is on the odd sectors of the last 6472 * track of the last cylinder. Device id will be on track of the next 6473 * to last cylinder. 6474 */ 6475 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6476 mutex_exit(SD_MUTEX(un)); 6477 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6478 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6479 SD_PATH_DIRECT); 6480 if (status != 0) { 6481 goto error; 6482 } 6483 6484 /* Validate the revision */ 6485 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6486 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6487 status = EINVAL; 6488 goto error; 6489 } 6490 6491 /* Calculate the checksum */ 6492 chksum = 0; 6493 ip = (uint_t *)dkdevid; 6494 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6495 i++) { 6496 chksum ^= ip[i]; 6497 } 6498 6499 /* Compare the checksums */ 6500 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6501 status = EINVAL; 6502 goto error; 6503 } 6504 6505 /* Validate the device id */ 6506 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6507 status = EINVAL; 6508 goto error; 6509 } 6510 6511 /* 6512 * Store the device id in the driver soft state 6513 */ 6514 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6515 tmpid = kmem_alloc(sz, KM_SLEEP); 6516 6517 mutex_enter(SD_MUTEX(un)); 6518 6519 un->un_devid = tmpid; 6520 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6521 6522 kmem_free(dkdevid, buffer_size); 6523 6524 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6525 6526 return (status); 6527 error: 6528 mutex_enter(SD_MUTEX(un)); 6529 kmem_free(dkdevid, buffer_size); 6530 return (status); 6531 } 6532 6533 6534 /* 6535 * Function: sd_create_devid 6536 * 6537 * Description: This routine will fabricate the device id and write it 6538 * to the disk. 6539 * 6540 * Arguments: un - driver soft state (unit) structure 6541 * 6542 * Return Code: value of the fabricated device id 6543 * 6544 * Context: Kernel Thread 6545 */ 6546 6547 static ddi_devid_t 6548 sd_create_devid(struct sd_lun *un) 6549 { 6550 ASSERT(un != NULL); 6551 6552 /* Fabricate the devid */ 6553 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6554 == DDI_FAILURE) { 6555 return (NULL); 6556 } 6557 6558 /* Write the devid to disk */ 6559 if (sd_write_deviceid(un) != 0) { 6560 ddi_devid_free(un->un_devid); 6561 un->un_devid = NULL; 6562 } 6563 6564 return (un->un_devid); 6565 } 6566 6567 6568 /* 6569 * Function: sd_write_deviceid 6570 * 6571 * Description: This routine will write the device id to the disk 6572 * reserved sector. 6573 * 6574 * Arguments: un - driver soft state (unit) structure 6575 * 6576 * Return Code: EINVAL 6577 * value returned by sd_send_scsi_cmd 6578 * 6579 * Context: Kernel Thread 6580 */ 6581 6582 static int 6583 sd_write_deviceid(struct sd_lun *un) 6584 { 6585 struct dk_devid *dkdevid; 6586 daddr_t blk; 6587 uint_t *ip, chksum; 6588 int status; 6589 int i; 6590 6591 ASSERT(mutex_owned(SD_MUTEX(un))); 6592 6593 blk = sd_get_devid_block(un); 6594 if (blk < 0) 6595 return (-1); 6596 mutex_exit(SD_MUTEX(un)); 6597 6598 /* Allocate the buffer */ 6599 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6600 6601 /* Fill in the revision */ 6602 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6603 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6604 6605 /* Copy in the device id */ 6606 mutex_enter(SD_MUTEX(un)); 6607 bcopy(un->un_devid, &dkdevid->dkd_devid, 6608 ddi_devid_sizeof(un->un_devid)); 6609 mutex_exit(SD_MUTEX(un)); 6610 6611 /* Calculate the checksum */ 6612 chksum = 0; 6613 ip = (uint_t *)dkdevid; 6614 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6615 i++) { 6616 chksum ^= ip[i]; 6617 } 6618 6619 /* Fill-in checksum */ 6620 DKD_FORMCHKSUM(chksum, dkdevid); 6621 6622 /* Write the reserved sector */ 6623 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6624 SD_PATH_DIRECT); 6625 6626 kmem_free(dkdevid, un->un_sys_blocksize); 6627 6628 mutex_enter(SD_MUTEX(un)); 6629 return (status); 6630 } 6631 6632 6633 /* 6634 * Function: sd_check_vpd_page_support 6635 * 6636 * Description: This routine sends an inquiry command with the EVPD bit set and 6637 * a page code of 0x00 to the device. It is used to determine which 6638 * vital product pages are availible to find the devid. We are 6639 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6640 * device does not support that command. 6641 * 6642 * Arguments: un - driver soft state (unit) structure 6643 * 6644 * Return Code: 0 - success 6645 * 1 - check condition 6646 * 6647 * Context: This routine can sleep. 6648 */ 6649 6650 static int 6651 sd_check_vpd_page_support(struct sd_lun *un) 6652 { 6653 uchar_t *page_list = NULL; 6654 uchar_t page_length = 0xff; /* Use max possible length */ 6655 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6656 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6657 int rval = 0; 6658 int counter; 6659 6660 ASSERT(un != NULL); 6661 ASSERT(mutex_owned(SD_MUTEX(un))); 6662 6663 mutex_exit(SD_MUTEX(un)); 6664 6665 /* 6666 * We'll set the page length to the maximum to save figuring it out 6667 * with an additional call. 6668 */ 6669 page_list = kmem_zalloc(page_length, KM_SLEEP); 6670 6671 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6672 page_code, NULL); 6673 6674 mutex_enter(SD_MUTEX(un)); 6675 6676 /* 6677 * Now we must validate that the device accepted the command, as some 6678 * drives do not support it. If the drive does support it, we will 6679 * return 0, and the supported pages will be in un_vpd_page_mask. If 6680 * not, we return -1. 6681 */ 6682 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6683 /* Loop to find one of the 2 pages we need */ 6684 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6685 6686 /* 6687 * Pages are returned in ascending order, and 0x83 is what we 6688 * are hoping for. 6689 */ 6690 while ((page_list[counter] <= 0x83) && 6691 (counter <= (page_list[VPD_PAGE_LENGTH] + 6692 VPD_HEAD_OFFSET))) { 6693 /* 6694 * Add 3 because page_list[3] is the number of 6695 * pages minus 3 6696 */ 6697 6698 switch (page_list[counter]) { 6699 case 0x00: 6700 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6701 break; 6702 case 0x80: 6703 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6704 break; 6705 case 0x81: 6706 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6707 break; 6708 case 0x82: 6709 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6710 break; 6711 case 0x83: 6712 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6713 break; 6714 } 6715 counter++; 6716 } 6717 6718 } else { 6719 rval = -1; 6720 6721 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6722 "sd_check_vpd_page_support: This drive does not implement " 6723 "VPD pages.\n"); 6724 } 6725 6726 kmem_free(page_list, page_length); 6727 6728 return (rval); 6729 } 6730 6731 6732 /* 6733 * Function: sd_setup_pm 6734 * 6735 * Description: Initialize Power Management on the device 6736 * 6737 * Context: Kernel Thread 6738 */ 6739 6740 static void 6741 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6742 { 6743 uint_t log_page_size; 6744 uchar_t *log_page_data; 6745 int rval; 6746 6747 /* 6748 * Since we are called from attach, holding a mutex for 6749 * un is unnecessary. Because some of the routines called 6750 * from here require SD_MUTEX to not be held, assert this 6751 * right up front. 6752 */ 6753 ASSERT(!mutex_owned(SD_MUTEX(un))); 6754 /* 6755 * Since the sd device does not have the 'reg' property, 6756 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6757 * The following code is to tell cpr that this device 6758 * DOES need to be suspended and resumed. 6759 */ 6760 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6761 "pm-hardware-state", "needs-suspend-resume"); 6762 6763 /* 6764 * This complies with the new power management framework 6765 * for certain desktop machines. Create the pm_components 6766 * property as a string array property. 6767 */ 6768 if (un->un_f_pm_supported) { 6769 /* 6770 * not all devices have a motor, try it first. 6771 * some devices may return ILLEGAL REQUEST, some 6772 * will hang 6773 * The following START_STOP_UNIT is used to check if target 6774 * device has a motor. 6775 */ 6776 un->un_f_start_stop_supported = TRUE; 6777 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6778 SD_PATH_DIRECT) != 0) { 6779 un->un_f_start_stop_supported = FALSE; 6780 } 6781 6782 /* 6783 * create pm properties anyways otherwise the parent can't 6784 * go to sleep 6785 */ 6786 (void) sd_create_pm_components(devi, un); 6787 un->un_f_pm_is_enabled = TRUE; 6788 return; 6789 } 6790 6791 if (!un->un_f_log_sense_supported) { 6792 un->un_power_level = SD_SPINDLE_ON; 6793 un->un_f_pm_is_enabled = FALSE; 6794 return; 6795 } 6796 6797 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6798 6799 #ifdef SDDEBUG 6800 if (sd_force_pm_supported) { 6801 /* Force a successful result */ 6802 rval = 1; 6803 } 6804 #endif 6805 6806 /* 6807 * If the start-stop cycle counter log page is not supported 6808 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6809 * then we should not create the pm_components property. 6810 */ 6811 if (rval == -1) { 6812 /* 6813 * Error. 6814 * Reading log sense failed, most likely this is 6815 * an older drive that does not support log sense. 6816 * If this fails auto-pm is not supported. 6817 */ 6818 un->un_power_level = SD_SPINDLE_ON; 6819 un->un_f_pm_is_enabled = FALSE; 6820 6821 } else if (rval == 0) { 6822 /* 6823 * Page not found. 6824 * The start stop cycle counter is implemented as page 6825 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6826 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6827 */ 6828 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6829 /* 6830 * Page found, use this one. 6831 */ 6832 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6833 un->un_f_pm_is_enabled = TRUE; 6834 } else { 6835 /* 6836 * Error or page not found. 6837 * auto-pm is not supported for this device. 6838 */ 6839 un->un_power_level = SD_SPINDLE_ON; 6840 un->un_f_pm_is_enabled = FALSE; 6841 } 6842 } else { 6843 /* 6844 * Page found, use it. 6845 */ 6846 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6847 un->un_f_pm_is_enabled = TRUE; 6848 } 6849 6850 6851 if (un->un_f_pm_is_enabled == TRUE) { 6852 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6853 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6854 6855 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6856 log_page_size, un->un_start_stop_cycle_page, 6857 0x01, 0, SD_PATH_DIRECT); 6858 #ifdef SDDEBUG 6859 if (sd_force_pm_supported) { 6860 /* Force a successful result */ 6861 rval = 0; 6862 } 6863 #endif 6864 6865 /* 6866 * If the Log sense for Page( Start/stop cycle counter page) 6867 * succeeds, then power managment is supported and we can 6868 * enable auto-pm. 6869 */ 6870 if (rval == 0) { 6871 (void) sd_create_pm_components(devi, un); 6872 } else { 6873 un->un_power_level = SD_SPINDLE_ON; 6874 un->un_f_pm_is_enabled = FALSE; 6875 } 6876 6877 kmem_free(log_page_data, log_page_size); 6878 } 6879 } 6880 6881 6882 /* 6883 * Function: sd_create_pm_components 6884 * 6885 * Description: Initialize PM property. 6886 * 6887 * Context: Kernel thread context 6888 */ 6889 6890 static void 6891 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6892 { 6893 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6894 6895 ASSERT(!mutex_owned(SD_MUTEX(un))); 6896 6897 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6898 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6899 /* 6900 * When components are initially created they are idle, 6901 * power up any non-removables. 6902 * Note: the return value of pm_raise_power can't be used 6903 * for determining if PM should be enabled for this device. 6904 * Even if you check the return values and remove this 6905 * property created above, the PM framework will not honor the 6906 * change after the first call to pm_raise_power. Hence, 6907 * removal of that property does not help if pm_raise_power 6908 * fails. In the case of removable media, the start/stop 6909 * will fail if the media is not present. 6910 */ 6911 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6912 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6913 mutex_enter(SD_MUTEX(un)); 6914 un->un_power_level = SD_SPINDLE_ON; 6915 mutex_enter(&un->un_pm_mutex); 6916 /* Set to on and not busy. */ 6917 un->un_pm_count = 0; 6918 } else { 6919 mutex_enter(SD_MUTEX(un)); 6920 un->un_power_level = SD_SPINDLE_OFF; 6921 mutex_enter(&un->un_pm_mutex); 6922 /* Set to off. */ 6923 un->un_pm_count = -1; 6924 } 6925 mutex_exit(&un->un_pm_mutex); 6926 mutex_exit(SD_MUTEX(un)); 6927 } else { 6928 un->un_power_level = SD_SPINDLE_ON; 6929 un->un_f_pm_is_enabled = FALSE; 6930 } 6931 } 6932 6933 6934 /* 6935 * Function: sd_ddi_suspend 6936 * 6937 * Description: Performs system power-down operations. This includes 6938 * setting the drive state to indicate its suspended so 6939 * that no new commands will be accepted. Also, wait for 6940 * all commands that are in transport or queued to a timer 6941 * for retry to complete. All timeout threads are cancelled. 6942 * 6943 * Return Code: DDI_FAILURE or DDI_SUCCESS 6944 * 6945 * Context: Kernel thread context 6946 */ 6947 6948 static int 6949 sd_ddi_suspend(dev_info_t *devi) 6950 { 6951 struct sd_lun *un; 6952 clock_t wait_cmds_complete; 6953 6954 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6955 if (un == NULL) { 6956 return (DDI_FAILURE); 6957 } 6958 6959 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6960 6961 mutex_enter(SD_MUTEX(un)); 6962 6963 /* Return success if the device is already suspended. */ 6964 if (un->un_state == SD_STATE_SUSPENDED) { 6965 mutex_exit(SD_MUTEX(un)); 6966 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6967 "device already suspended, exiting\n"); 6968 return (DDI_SUCCESS); 6969 } 6970 6971 /* Return failure if the device is being used by HA */ 6972 if (un->un_resvd_status & 6973 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6974 mutex_exit(SD_MUTEX(un)); 6975 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6976 "device in use by HA, exiting\n"); 6977 return (DDI_FAILURE); 6978 } 6979 6980 /* 6981 * Return failure if the device is in a resource wait 6982 * or power changing state. 6983 */ 6984 if ((un->un_state == SD_STATE_RWAIT) || 6985 (un->un_state == SD_STATE_PM_CHANGING)) { 6986 mutex_exit(SD_MUTEX(un)); 6987 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6988 "device in resource wait state, exiting\n"); 6989 return (DDI_FAILURE); 6990 } 6991 6992 6993 un->un_save_state = un->un_last_state; 6994 New_state(un, SD_STATE_SUSPENDED); 6995 6996 /* 6997 * Wait for all commands that are in transport or queued to a timer 6998 * for retry to complete. 6999 * 7000 * While waiting, no new commands will be accepted or sent because of 7001 * the new state we set above. 7002 * 7003 * Wait till current operation has completed. If we are in the resource 7004 * wait state (with an intr outstanding) then we need to wait till the 7005 * intr completes and starts the next cmd. We want to wait for 7006 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 7007 */ 7008 wait_cmds_complete = ddi_get_lbolt() + 7009 (sd_wait_cmds_complete * drv_usectohz(1000000)); 7010 7011 while (un->un_ncmds_in_transport != 0) { 7012 /* 7013 * Fail if commands do not finish in the specified time. 7014 */ 7015 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 7016 wait_cmds_complete) == -1) { 7017 /* 7018 * Undo the state changes made above. Everything 7019 * must go back to it's original value. 7020 */ 7021 Restore_state(un); 7022 un->un_last_state = un->un_save_state; 7023 /* Wake up any threads that might be waiting. */ 7024 cv_broadcast(&un->un_suspend_cv); 7025 mutex_exit(SD_MUTEX(un)); 7026 SD_ERROR(SD_LOG_IO_PM, un, 7027 "sd_ddi_suspend: failed due to outstanding cmds\n"); 7028 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 7029 return (DDI_FAILURE); 7030 } 7031 } 7032 7033 /* 7034 * Cancel SCSI watch thread and timeouts, if any are active 7035 */ 7036 7037 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 7038 opaque_t temp_token = un->un_swr_token; 7039 mutex_exit(SD_MUTEX(un)); 7040 scsi_watch_suspend(temp_token); 7041 mutex_enter(SD_MUTEX(un)); 7042 } 7043 7044 if (un->un_reset_throttle_timeid != NULL) { 7045 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7046 un->un_reset_throttle_timeid = NULL; 7047 mutex_exit(SD_MUTEX(un)); 7048 (void) untimeout(temp_id); 7049 mutex_enter(SD_MUTEX(un)); 7050 } 7051 7052 if (un->un_dcvb_timeid != NULL) { 7053 timeout_id_t temp_id = un->un_dcvb_timeid; 7054 un->un_dcvb_timeid = NULL; 7055 mutex_exit(SD_MUTEX(un)); 7056 (void) untimeout(temp_id); 7057 mutex_enter(SD_MUTEX(un)); 7058 } 7059 7060 mutex_enter(&un->un_pm_mutex); 7061 if (un->un_pm_timeid != NULL) { 7062 timeout_id_t temp_id = un->un_pm_timeid; 7063 un->un_pm_timeid = NULL; 7064 mutex_exit(&un->un_pm_mutex); 7065 mutex_exit(SD_MUTEX(un)); 7066 (void) untimeout(temp_id); 7067 mutex_enter(SD_MUTEX(un)); 7068 } else { 7069 mutex_exit(&un->un_pm_mutex); 7070 } 7071 7072 if (un->un_retry_timeid != NULL) { 7073 timeout_id_t temp_id = un->un_retry_timeid; 7074 un->un_retry_timeid = NULL; 7075 mutex_exit(SD_MUTEX(un)); 7076 (void) untimeout(temp_id); 7077 mutex_enter(SD_MUTEX(un)); 7078 } 7079 7080 if (un->un_direct_priority_timeid != NULL) { 7081 timeout_id_t temp_id = un->un_direct_priority_timeid; 7082 un->un_direct_priority_timeid = NULL; 7083 mutex_exit(SD_MUTEX(un)); 7084 (void) untimeout(temp_id); 7085 mutex_enter(SD_MUTEX(un)); 7086 } 7087 7088 if (un->un_f_is_fibre == TRUE) { 7089 /* 7090 * Remove callbacks for insert and remove events 7091 */ 7092 if (un->un_insert_event != NULL) { 7093 mutex_exit(SD_MUTEX(un)); 7094 (void) ddi_remove_event_handler(un->un_insert_cb_id); 7095 mutex_enter(SD_MUTEX(un)); 7096 un->un_insert_event = NULL; 7097 } 7098 7099 if (un->un_remove_event != NULL) { 7100 mutex_exit(SD_MUTEX(un)); 7101 (void) ddi_remove_event_handler(un->un_remove_cb_id); 7102 mutex_enter(SD_MUTEX(un)); 7103 un->un_remove_event = NULL; 7104 } 7105 } 7106 7107 mutex_exit(SD_MUTEX(un)); 7108 7109 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 7110 7111 return (DDI_SUCCESS); 7112 } 7113 7114 7115 /* 7116 * Function: sd_ddi_pm_suspend 7117 * 7118 * Description: Set the drive state to low power. 7119 * Someone else is required to actually change the drive 7120 * power level. 7121 * 7122 * Arguments: un - driver soft state (unit) structure 7123 * 7124 * Return Code: DDI_FAILURE or DDI_SUCCESS 7125 * 7126 * Context: Kernel thread context 7127 */ 7128 7129 static int 7130 sd_ddi_pm_suspend(struct sd_lun *un) 7131 { 7132 ASSERT(un != NULL); 7133 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 7134 7135 ASSERT(!mutex_owned(SD_MUTEX(un))); 7136 mutex_enter(SD_MUTEX(un)); 7137 7138 /* 7139 * Exit if power management is not enabled for this device, or if 7140 * the device is being used by HA. 7141 */ 7142 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 7143 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 7144 mutex_exit(SD_MUTEX(un)); 7145 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 7146 return (DDI_SUCCESS); 7147 } 7148 7149 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 7150 un->un_ncmds_in_driver); 7151 7152 /* 7153 * See if the device is not busy, ie.: 7154 * - we have no commands in the driver for this device 7155 * - not waiting for resources 7156 */ 7157 if ((un->un_ncmds_in_driver == 0) && 7158 (un->un_state != SD_STATE_RWAIT)) { 7159 /* 7160 * The device is not busy, so it is OK to go to low power state. 7161 * Indicate low power, but rely on someone else to actually 7162 * change it. 7163 */ 7164 mutex_enter(&un->un_pm_mutex); 7165 un->un_pm_count = -1; 7166 mutex_exit(&un->un_pm_mutex); 7167 un->un_power_level = SD_SPINDLE_OFF; 7168 } 7169 7170 mutex_exit(SD_MUTEX(un)); 7171 7172 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 7173 7174 return (DDI_SUCCESS); 7175 } 7176 7177 7178 /* 7179 * Function: sd_ddi_resume 7180 * 7181 * Description: Performs system power-up operations.. 7182 * 7183 * Return Code: DDI_SUCCESS 7184 * DDI_FAILURE 7185 * 7186 * Context: Kernel thread context 7187 */ 7188 7189 static int 7190 sd_ddi_resume(dev_info_t *devi) 7191 { 7192 struct sd_lun *un; 7193 7194 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 7195 if (un == NULL) { 7196 return (DDI_FAILURE); 7197 } 7198 7199 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 7200 7201 mutex_enter(SD_MUTEX(un)); 7202 Restore_state(un); 7203 7204 /* 7205 * Restore the state which was saved to give the 7206 * the right state in un_last_state 7207 */ 7208 un->un_last_state = un->un_save_state; 7209 /* 7210 * Note: throttle comes back at full. 7211 * Also note: this MUST be done before calling pm_raise_power 7212 * otherwise the system can get hung in biowait. The scenario where 7213 * this'll happen is under cpr suspend. Writing of the system 7214 * state goes through sddump, which writes 0 to un_throttle. If 7215 * writing the system state then fails, example if the partition is 7216 * too small, then cpr attempts a resume. If throttle isn't restored 7217 * from the saved value until after calling pm_raise_power then 7218 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 7219 * in biowait. 7220 */ 7221 un->un_throttle = un->un_saved_throttle; 7222 7223 /* 7224 * The chance of failure is very rare as the only command done in power 7225 * entry point is START command when you transition from 0->1 or 7226 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 7227 * which suspend was done. Ignore the return value as the resume should 7228 * not be failed. In the case of removable media the media need not be 7229 * inserted and hence there is a chance that raise power will fail with 7230 * media not present. 7231 */ 7232 if (un->un_f_attach_spinup) { 7233 mutex_exit(SD_MUTEX(un)); 7234 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 7235 mutex_enter(SD_MUTEX(un)); 7236 } 7237 7238 /* 7239 * Don't broadcast to the suspend cv and therefore possibly 7240 * start I/O until after power has been restored. 7241 */ 7242 cv_broadcast(&un->un_suspend_cv); 7243 cv_broadcast(&un->un_state_cv); 7244 7245 /* restart thread */ 7246 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 7247 scsi_watch_resume(un->un_swr_token); 7248 } 7249 7250 #if (defined(__fibre)) 7251 if (un->un_f_is_fibre == TRUE) { 7252 /* 7253 * Add callbacks for insert and remove events 7254 */ 7255 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7256 sd_init_event_callbacks(un); 7257 } 7258 } 7259 #endif 7260 7261 /* 7262 * Transport any pending commands to the target. 7263 * 7264 * If this is a low-activity device commands in queue will have to wait 7265 * until new commands come in, which may take awhile. Also, we 7266 * specifically don't check un_ncmds_in_transport because we know that 7267 * there really are no commands in progress after the unit was 7268 * suspended and we could have reached the throttle level, been 7269 * suspended, and have no new commands coming in for awhile. Highly 7270 * unlikely, but so is the low-activity disk scenario. 7271 */ 7272 ddi_xbuf_dispatch(un->un_xbuf_attr); 7273 7274 sd_start_cmds(un, NULL); 7275 mutex_exit(SD_MUTEX(un)); 7276 7277 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 7278 7279 return (DDI_SUCCESS); 7280 } 7281 7282 7283 /* 7284 * Function: sd_ddi_pm_resume 7285 * 7286 * Description: Set the drive state to powered on. 7287 * Someone else is required to actually change the drive 7288 * power level. 7289 * 7290 * Arguments: un - driver soft state (unit) structure 7291 * 7292 * Return Code: DDI_SUCCESS 7293 * 7294 * Context: Kernel thread context 7295 */ 7296 7297 static int 7298 sd_ddi_pm_resume(struct sd_lun *un) 7299 { 7300 ASSERT(un != NULL); 7301 7302 ASSERT(!mutex_owned(SD_MUTEX(un))); 7303 mutex_enter(SD_MUTEX(un)); 7304 un->un_power_level = SD_SPINDLE_ON; 7305 7306 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7307 mutex_enter(&un->un_pm_mutex); 7308 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7309 un->un_pm_count++; 7310 ASSERT(un->un_pm_count == 0); 7311 /* 7312 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7313 * un_suspend_cv is for a system resume, not a power management 7314 * device resume. (4297749) 7315 * cv_broadcast(&un->un_suspend_cv); 7316 */ 7317 } 7318 mutex_exit(&un->un_pm_mutex); 7319 mutex_exit(SD_MUTEX(un)); 7320 7321 return (DDI_SUCCESS); 7322 } 7323 7324 7325 /* 7326 * Function: sd_pm_idletimeout_handler 7327 * 7328 * Description: A timer routine that's active only while a device is busy. 7329 * The purpose is to extend slightly the pm framework's busy 7330 * view of the device to prevent busy/idle thrashing for 7331 * back-to-back commands. Do this by comparing the current time 7332 * to the time at which the last command completed and when the 7333 * difference is greater than sd_pm_idletime, call 7334 * pm_idle_component. In addition to indicating idle to the pm 7335 * framework, update the chain type to again use the internal pm 7336 * layers of the driver. 7337 * 7338 * Arguments: arg - driver soft state (unit) structure 7339 * 7340 * Context: Executes in a timeout(9F) thread context 7341 */ 7342 7343 static void 7344 sd_pm_idletimeout_handler(void *arg) 7345 { 7346 struct sd_lun *un = arg; 7347 7348 time_t now; 7349 7350 mutex_enter(&sd_detach_mutex); 7351 if (un->un_detach_count != 0) { 7352 /* Abort if the instance is detaching */ 7353 mutex_exit(&sd_detach_mutex); 7354 return; 7355 } 7356 mutex_exit(&sd_detach_mutex); 7357 7358 now = ddi_get_time(); 7359 /* 7360 * Grab both mutexes, in the proper order, since we're accessing 7361 * both PM and softstate variables. 7362 */ 7363 mutex_enter(SD_MUTEX(un)); 7364 mutex_enter(&un->un_pm_mutex); 7365 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7366 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7367 /* 7368 * Update the chain types. 7369 * This takes affect on the next new command received. 7370 */ 7371 if (un->un_f_non_devbsize_supported) { 7372 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7373 } else { 7374 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7375 } 7376 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7377 7378 SD_TRACE(SD_LOG_IO_PM, un, 7379 "sd_pm_idletimeout_handler: idling device\n"); 7380 (void) pm_idle_component(SD_DEVINFO(un), 0); 7381 un->un_pm_idle_timeid = NULL; 7382 } else { 7383 un->un_pm_idle_timeid = 7384 timeout(sd_pm_idletimeout_handler, un, 7385 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7386 } 7387 mutex_exit(&un->un_pm_mutex); 7388 mutex_exit(SD_MUTEX(un)); 7389 } 7390 7391 7392 /* 7393 * Function: sd_pm_timeout_handler 7394 * 7395 * Description: Callback to tell framework we are idle. 7396 * 7397 * Context: timeout(9f) thread context. 7398 */ 7399 7400 static void 7401 sd_pm_timeout_handler(void *arg) 7402 { 7403 struct sd_lun *un = arg; 7404 7405 (void) pm_idle_component(SD_DEVINFO(un), 0); 7406 mutex_enter(&un->un_pm_mutex); 7407 un->un_pm_timeid = NULL; 7408 mutex_exit(&un->un_pm_mutex); 7409 } 7410 7411 7412 /* 7413 * Function: sdpower 7414 * 7415 * Description: PM entry point. 7416 * 7417 * Return Code: DDI_SUCCESS 7418 * DDI_FAILURE 7419 * 7420 * Context: Kernel thread context 7421 */ 7422 7423 static int 7424 sdpower(dev_info_t *devi, int component, int level) 7425 { 7426 struct sd_lun *un; 7427 int instance; 7428 int rval = DDI_SUCCESS; 7429 uint_t i, log_page_size, maxcycles, ncycles; 7430 uchar_t *log_page_data; 7431 int log_sense_page; 7432 int medium_present; 7433 time_t intvlp; 7434 dev_t dev; 7435 struct pm_trans_data sd_pm_tran_data; 7436 uchar_t save_state; 7437 int sval; 7438 uchar_t state_before_pm; 7439 int got_semaphore_here; 7440 7441 instance = ddi_get_instance(devi); 7442 7443 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7444 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7445 component != 0) { 7446 return (DDI_FAILURE); 7447 } 7448 7449 dev = sd_make_device(SD_DEVINFO(un)); 7450 7451 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7452 7453 /* 7454 * Must synchronize power down with close. 7455 * Attempt to decrement/acquire the open/close semaphore, 7456 * but do NOT wait on it. If it's not greater than zero, 7457 * ie. it can't be decremented without waiting, then 7458 * someone else, either open or close, already has it 7459 * and the try returns 0. Use that knowledge here to determine 7460 * if it's OK to change the device power level. 7461 * Also, only increment it on exit if it was decremented, ie. gotten, 7462 * here. 7463 */ 7464 got_semaphore_here = sema_tryp(&un->un_semoclose); 7465 7466 mutex_enter(SD_MUTEX(un)); 7467 7468 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7469 un->un_ncmds_in_driver); 7470 7471 /* 7472 * If un_ncmds_in_driver is non-zero it indicates commands are 7473 * already being processed in the driver, or if the semaphore was 7474 * not gotten here it indicates an open or close is being processed. 7475 * At the same time somebody is requesting to go low power which 7476 * can't happen, therefore we need to return failure. 7477 */ 7478 if ((level == SD_SPINDLE_OFF) && 7479 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7480 mutex_exit(SD_MUTEX(un)); 7481 7482 if (got_semaphore_here != 0) { 7483 sema_v(&un->un_semoclose); 7484 } 7485 SD_TRACE(SD_LOG_IO_PM, un, 7486 "sdpower: exit, device has queued cmds.\n"); 7487 return (DDI_FAILURE); 7488 } 7489 7490 /* 7491 * if it is OFFLINE that means the disk is completely dead 7492 * in our case we have to put the disk in on or off by sending commands 7493 * Of course that will fail anyway so return back here. 7494 * 7495 * Power changes to a device that's OFFLINE or SUSPENDED 7496 * are not allowed. 7497 */ 7498 if ((un->un_state == SD_STATE_OFFLINE) || 7499 (un->un_state == SD_STATE_SUSPENDED)) { 7500 mutex_exit(SD_MUTEX(un)); 7501 7502 if (got_semaphore_here != 0) { 7503 sema_v(&un->un_semoclose); 7504 } 7505 SD_TRACE(SD_LOG_IO_PM, un, 7506 "sdpower: exit, device is off-line.\n"); 7507 return (DDI_FAILURE); 7508 } 7509 7510 /* 7511 * Change the device's state to indicate it's power level 7512 * is being changed. Do this to prevent a power off in the 7513 * middle of commands, which is especially bad on devices 7514 * that are really powered off instead of just spun down. 7515 */ 7516 state_before_pm = un->un_state; 7517 un->un_state = SD_STATE_PM_CHANGING; 7518 7519 mutex_exit(SD_MUTEX(un)); 7520 7521 /* 7522 * If "pm-capable" property is set to TRUE by HBA drivers, 7523 * bypass the following checking, otherwise, check the log 7524 * sense information for this device 7525 */ 7526 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7527 /* 7528 * Get the log sense information to understand whether the 7529 * the powercycle counts have gone beyond the threshhold. 7530 */ 7531 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7532 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7533 7534 mutex_enter(SD_MUTEX(un)); 7535 log_sense_page = un->un_start_stop_cycle_page; 7536 mutex_exit(SD_MUTEX(un)); 7537 7538 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7539 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7540 #ifdef SDDEBUG 7541 if (sd_force_pm_supported) { 7542 /* Force a successful result */ 7543 rval = 0; 7544 } 7545 #endif 7546 if (rval != 0) { 7547 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7548 "Log Sense Failed\n"); 7549 kmem_free(log_page_data, log_page_size); 7550 /* Cannot support power management on those drives */ 7551 7552 if (got_semaphore_here != 0) { 7553 sema_v(&un->un_semoclose); 7554 } 7555 /* 7556 * On exit put the state back to it's original value 7557 * and broadcast to anyone waiting for the power 7558 * change completion. 7559 */ 7560 mutex_enter(SD_MUTEX(un)); 7561 un->un_state = state_before_pm; 7562 cv_broadcast(&un->un_suspend_cv); 7563 mutex_exit(SD_MUTEX(un)); 7564 SD_TRACE(SD_LOG_IO_PM, un, 7565 "sdpower: exit, Log Sense Failed.\n"); 7566 return (DDI_FAILURE); 7567 } 7568 7569 /* 7570 * From the page data - Convert the essential information to 7571 * pm_trans_data 7572 */ 7573 maxcycles = 7574 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7575 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7576 7577 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7578 7579 ncycles = 7580 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7581 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7582 7583 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7584 7585 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7586 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7587 log_page_data[8+i]; 7588 } 7589 7590 kmem_free(log_page_data, log_page_size); 7591 7592 /* 7593 * Call pm_trans_check routine to get the Ok from 7594 * the global policy 7595 */ 7596 7597 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7598 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7599 7600 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7601 #ifdef SDDEBUG 7602 if (sd_force_pm_supported) { 7603 /* Force a successful result */ 7604 rval = 1; 7605 } 7606 #endif 7607 switch (rval) { 7608 case 0: 7609 /* 7610 * Not Ok to Power cycle or error in parameters passed 7611 * Would have given the advised time to consider power 7612 * cycle. Based on the new intvlp parameter we are 7613 * supposed to pretend we are busy so that pm framework 7614 * will never call our power entry point. Because of 7615 * that install a timeout handler and wait for the 7616 * recommended time to elapse so that power management 7617 * can be effective again. 7618 * 7619 * To effect this behavior, call pm_busy_component to 7620 * indicate to the framework this device is busy. 7621 * By not adjusting un_pm_count the rest of PM in 7622 * the driver will function normally, and independant 7623 * of this but because the framework is told the device 7624 * is busy it won't attempt powering down until it gets 7625 * a matching idle. The timeout handler sends this. 7626 * Note: sd_pm_entry can't be called here to do this 7627 * because sdpower may have been called as a result 7628 * of a call to pm_raise_power from within sd_pm_entry. 7629 * 7630 * If a timeout handler is already active then 7631 * don't install another. 7632 */ 7633 mutex_enter(&un->un_pm_mutex); 7634 if (un->un_pm_timeid == NULL) { 7635 un->un_pm_timeid = 7636 timeout(sd_pm_timeout_handler, 7637 un, intvlp * drv_usectohz(1000000)); 7638 mutex_exit(&un->un_pm_mutex); 7639 (void) pm_busy_component(SD_DEVINFO(un), 0); 7640 } else { 7641 mutex_exit(&un->un_pm_mutex); 7642 } 7643 if (got_semaphore_here != 0) { 7644 sema_v(&un->un_semoclose); 7645 } 7646 /* 7647 * On exit put the state back to it's original value 7648 * and broadcast to anyone waiting for the power 7649 * change completion. 7650 */ 7651 mutex_enter(SD_MUTEX(un)); 7652 un->un_state = state_before_pm; 7653 cv_broadcast(&un->un_suspend_cv); 7654 mutex_exit(SD_MUTEX(un)); 7655 7656 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7657 "trans check Failed, not ok to power cycle.\n"); 7658 return (DDI_FAILURE); 7659 7660 case -1: 7661 if (got_semaphore_here != 0) { 7662 sema_v(&un->un_semoclose); 7663 } 7664 /* 7665 * On exit put the state back to it's original value 7666 * and broadcast to anyone waiting for the power 7667 * change completion. 7668 */ 7669 mutex_enter(SD_MUTEX(un)); 7670 un->un_state = state_before_pm; 7671 cv_broadcast(&un->un_suspend_cv); 7672 mutex_exit(SD_MUTEX(un)); 7673 SD_TRACE(SD_LOG_IO_PM, un, 7674 "sdpower: exit, trans check command Failed.\n"); 7675 return (DDI_FAILURE); 7676 } 7677 } 7678 7679 if (level == SD_SPINDLE_OFF) { 7680 /* 7681 * Save the last state... if the STOP FAILS we need it 7682 * for restoring 7683 */ 7684 mutex_enter(SD_MUTEX(un)); 7685 save_state = un->un_last_state; 7686 /* 7687 * There must not be any cmds. getting processed 7688 * in the driver when we get here. Power to the 7689 * device is potentially going off. 7690 */ 7691 ASSERT(un->un_ncmds_in_driver == 0); 7692 mutex_exit(SD_MUTEX(un)); 7693 7694 /* 7695 * For now suspend the device completely before spindle is 7696 * turned off 7697 */ 7698 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7699 if (got_semaphore_here != 0) { 7700 sema_v(&un->un_semoclose); 7701 } 7702 /* 7703 * On exit put the state back to it's original value 7704 * and broadcast to anyone waiting for the power 7705 * change completion. 7706 */ 7707 mutex_enter(SD_MUTEX(un)); 7708 un->un_state = state_before_pm; 7709 cv_broadcast(&un->un_suspend_cv); 7710 mutex_exit(SD_MUTEX(un)); 7711 SD_TRACE(SD_LOG_IO_PM, un, 7712 "sdpower: exit, PM suspend Failed.\n"); 7713 return (DDI_FAILURE); 7714 } 7715 } 7716 7717 /* 7718 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7719 * close, or strategy. Dump no long uses this routine, it uses it's 7720 * own code so it can be done in polled mode. 7721 */ 7722 7723 medium_present = TRUE; 7724 7725 /* 7726 * When powering up, issue a TUR in case the device is at unit 7727 * attention. Don't do retries. Bypass the PM layer, otherwise 7728 * a deadlock on un_pm_busy_cv will occur. 7729 */ 7730 if (level == SD_SPINDLE_ON) { 7731 (void) sd_send_scsi_TEST_UNIT_READY(un, 7732 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7733 } 7734 7735 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7736 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7737 7738 sval = sd_send_scsi_START_STOP_UNIT(un, 7739 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7740 SD_PATH_DIRECT); 7741 /* Command failed, check for media present. */ 7742 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7743 medium_present = FALSE; 7744 } 7745 7746 /* 7747 * The conditions of interest here are: 7748 * if a spindle off with media present fails, 7749 * then restore the state and return an error. 7750 * else if a spindle on fails, 7751 * then return an error (there's no state to restore). 7752 * In all other cases we setup for the new state 7753 * and return success. 7754 */ 7755 switch (level) { 7756 case SD_SPINDLE_OFF: 7757 if ((medium_present == TRUE) && (sval != 0)) { 7758 /* The stop command from above failed */ 7759 rval = DDI_FAILURE; 7760 /* 7761 * The stop command failed, and we have media 7762 * present. Put the level back by calling the 7763 * sd_pm_resume() and set the state back to 7764 * it's previous value. 7765 */ 7766 (void) sd_ddi_pm_resume(un); 7767 mutex_enter(SD_MUTEX(un)); 7768 un->un_last_state = save_state; 7769 mutex_exit(SD_MUTEX(un)); 7770 break; 7771 } 7772 /* 7773 * The stop command from above succeeded. 7774 */ 7775 if (un->un_f_monitor_media_state) { 7776 /* 7777 * Terminate watch thread in case of removable media 7778 * devices going into low power state. This is as per 7779 * the requirements of pm framework, otherwise commands 7780 * will be generated for the device (through watch 7781 * thread), even when the device is in low power state. 7782 */ 7783 mutex_enter(SD_MUTEX(un)); 7784 un->un_f_watcht_stopped = FALSE; 7785 if (un->un_swr_token != NULL) { 7786 opaque_t temp_token = un->un_swr_token; 7787 un->un_f_watcht_stopped = TRUE; 7788 un->un_swr_token = NULL; 7789 mutex_exit(SD_MUTEX(un)); 7790 (void) scsi_watch_request_terminate(temp_token, 7791 SCSI_WATCH_TERMINATE_WAIT); 7792 } else { 7793 mutex_exit(SD_MUTEX(un)); 7794 } 7795 } 7796 break; 7797 7798 default: /* The level requested is spindle on... */ 7799 /* 7800 * Legacy behavior: return success on a failed spinup 7801 * if there is no media in the drive. 7802 * Do this by looking at medium_present here. 7803 */ 7804 if ((sval != 0) && medium_present) { 7805 /* The start command from above failed */ 7806 rval = DDI_FAILURE; 7807 break; 7808 } 7809 /* 7810 * The start command from above succeeded 7811 * Resume the devices now that we have 7812 * started the disks 7813 */ 7814 (void) sd_ddi_pm_resume(un); 7815 7816 /* 7817 * Resume the watch thread since it was suspended 7818 * when the device went into low power mode. 7819 */ 7820 if (un->un_f_monitor_media_state) { 7821 mutex_enter(SD_MUTEX(un)); 7822 if (un->un_f_watcht_stopped == TRUE) { 7823 opaque_t temp_token; 7824 7825 un->un_f_watcht_stopped = FALSE; 7826 mutex_exit(SD_MUTEX(un)); 7827 temp_token = scsi_watch_request_submit( 7828 SD_SCSI_DEVP(un), 7829 sd_check_media_time, 7830 SENSE_LENGTH, sd_media_watch_cb, 7831 (caddr_t)dev); 7832 mutex_enter(SD_MUTEX(un)); 7833 un->un_swr_token = temp_token; 7834 } 7835 mutex_exit(SD_MUTEX(un)); 7836 } 7837 } 7838 if (got_semaphore_here != 0) { 7839 sema_v(&un->un_semoclose); 7840 } 7841 /* 7842 * On exit put the state back to it's original value 7843 * and broadcast to anyone waiting for the power 7844 * change completion. 7845 */ 7846 mutex_enter(SD_MUTEX(un)); 7847 un->un_state = state_before_pm; 7848 cv_broadcast(&un->un_suspend_cv); 7849 mutex_exit(SD_MUTEX(un)); 7850 7851 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7852 7853 return (rval); 7854 } 7855 7856 7857 7858 /* 7859 * Function: sdattach 7860 * 7861 * Description: Driver's attach(9e) entry point function. 7862 * 7863 * Arguments: devi - opaque device info handle 7864 * cmd - attach type 7865 * 7866 * Return Code: DDI_SUCCESS 7867 * DDI_FAILURE 7868 * 7869 * Context: Kernel thread context 7870 */ 7871 7872 static int 7873 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7874 { 7875 switch (cmd) { 7876 case DDI_ATTACH: 7877 return (sd_unit_attach(devi)); 7878 case DDI_RESUME: 7879 return (sd_ddi_resume(devi)); 7880 default: 7881 break; 7882 } 7883 return (DDI_FAILURE); 7884 } 7885 7886 7887 /* 7888 * Function: sddetach 7889 * 7890 * Description: Driver's detach(9E) entry point function. 7891 * 7892 * Arguments: devi - opaque device info handle 7893 * cmd - detach type 7894 * 7895 * Return Code: DDI_SUCCESS 7896 * DDI_FAILURE 7897 * 7898 * Context: Kernel thread context 7899 */ 7900 7901 static int 7902 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7903 { 7904 switch (cmd) { 7905 case DDI_DETACH: 7906 return (sd_unit_detach(devi)); 7907 case DDI_SUSPEND: 7908 return (sd_ddi_suspend(devi)); 7909 default: 7910 break; 7911 } 7912 return (DDI_FAILURE); 7913 } 7914 7915 7916 /* 7917 * Function: sd_sync_with_callback 7918 * 7919 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7920 * state while the callback routine is active. 7921 * 7922 * Arguments: un: softstate structure for the instance 7923 * 7924 * Context: Kernel thread context 7925 */ 7926 7927 static void 7928 sd_sync_with_callback(struct sd_lun *un) 7929 { 7930 ASSERT(un != NULL); 7931 7932 mutex_enter(SD_MUTEX(un)); 7933 7934 ASSERT(un->un_in_callback >= 0); 7935 7936 while (un->un_in_callback > 0) { 7937 mutex_exit(SD_MUTEX(un)); 7938 delay(2); 7939 mutex_enter(SD_MUTEX(un)); 7940 } 7941 7942 mutex_exit(SD_MUTEX(un)); 7943 } 7944 7945 /* 7946 * Function: sd_unit_attach 7947 * 7948 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7949 * the soft state structure for the device and performs 7950 * all necessary structure and device initializations. 7951 * 7952 * Arguments: devi: the system's dev_info_t for the device. 7953 * 7954 * Return Code: DDI_SUCCESS if attach is successful. 7955 * DDI_FAILURE if any part of the attach fails. 7956 * 7957 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7958 * Kernel thread context only. Can sleep. 7959 */ 7960 7961 static int 7962 sd_unit_attach(dev_info_t *devi) 7963 { 7964 struct scsi_device *devp; 7965 struct sd_lun *un; 7966 char *variantp; 7967 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7968 int instance; 7969 int rval; 7970 int wc_enabled; 7971 int tgt; 7972 uint64_t capacity; 7973 uint_t lbasize; 7974 dev_info_t *pdip = ddi_get_parent(devi); 7975 7976 /* 7977 * Retrieve the target driver's private data area. This was set 7978 * up by the HBA. 7979 */ 7980 devp = ddi_get_driver_private(devi); 7981 7982 /* 7983 * Retrieve the target ID of the device. 7984 */ 7985 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 7986 SCSI_ADDR_PROP_TARGET, -1); 7987 7988 /* 7989 * Since we have no idea what state things were left in by the last 7990 * user of the device, set up some 'default' settings, ie. turn 'em 7991 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 7992 * Do this before the scsi_probe, which sends an inquiry. 7993 * This is a fix for bug (4430280). 7994 * Of special importance is wide-xfer. The drive could have been left 7995 * in wide transfer mode by the last driver to communicate with it, 7996 * this includes us. If that's the case, and if the following is not 7997 * setup properly or we don't re-negotiate with the drive prior to 7998 * transferring data to/from the drive, it causes bus parity errors, 7999 * data overruns, and unexpected interrupts. This first occurred when 8000 * the fix for bug (4378686) was made. 8001 */ 8002 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 8003 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 8004 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 8005 8006 /* 8007 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 8008 * on a target. Setting it per lun instance actually sets the 8009 * capability of this target, which affects those luns already 8010 * attached on the same target. So during attach, we can only disable 8011 * this capability only when no other lun has been attached on this 8012 * target. By doing this, we assume a target has the same tagged-qing 8013 * capability for every lun. The condition can be removed when HBA 8014 * is changed to support per lun based tagged-qing capability. 8015 */ 8016 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8017 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 8018 } 8019 8020 /* 8021 * Use scsi_probe() to issue an INQUIRY command to the device. 8022 * This call will allocate and fill in the scsi_inquiry structure 8023 * and point the sd_inq member of the scsi_device structure to it. 8024 * If the attach succeeds, then this memory will not be de-allocated 8025 * (via scsi_unprobe()) until the instance is detached. 8026 */ 8027 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 8028 goto probe_failed; 8029 } 8030 8031 /* 8032 * Check the device type as specified in the inquiry data and 8033 * claim it if it is of a type that we support. 8034 */ 8035 switch (devp->sd_inq->inq_dtype) { 8036 case DTYPE_DIRECT: 8037 break; 8038 case DTYPE_RODIRECT: 8039 break; 8040 case DTYPE_OPTICAL: 8041 break; 8042 case DTYPE_NOTPRESENT: 8043 default: 8044 /* Unsupported device type; fail the attach. */ 8045 goto probe_failed; 8046 } 8047 8048 /* 8049 * Allocate the soft state structure for this unit. 8050 * 8051 * We rely upon this memory being set to all zeroes by 8052 * ddi_soft_state_zalloc(). We assume that any member of the 8053 * soft state structure that is not explicitly initialized by 8054 * this routine will have a value of zero. 8055 */ 8056 instance = ddi_get_instance(devp->sd_dev); 8057 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 8058 goto probe_failed; 8059 } 8060 8061 /* 8062 * Retrieve a pointer to the newly-allocated soft state. 8063 * 8064 * This should NEVER fail if the ddi_soft_state_zalloc() call above 8065 * was successful, unless something has gone horribly wrong and the 8066 * ddi's soft state internals are corrupt (in which case it is 8067 * probably better to halt here than just fail the attach....) 8068 */ 8069 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 8070 panic("sd_unit_attach: NULL soft state on instance:0x%x", 8071 instance); 8072 /*NOTREACHED*/ 8073 } 8074 8075 /* 8076 * Link the back ptr of the driver soft state to the scsi_device 8077 * struct for this lun. 8078 * Save a pointer to the softstate in the driver-private area of 8079 * the scsi_device struct. 8080 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 8081 * we first set un->un_sd below. 8082 */ 8083 un->un_sd = devp; 8084 devp->sd_private = (opaque_t)un; 8085 8086 /* 8087 * The following must be after devp is stored in the soft state struct. 8088 */ 8089 #ifdef SDDEBUG 8090 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8091 "%s_unit_attach: un:0x%p instance:%d\n", 8092 ddi_driver_name(devi), un, instance); 8093 #endif 8094 8095 /* 8096 * Set up the device type and node type (for the minor nodes). 8097 * By default we assume that the device can at least support the 8098 * Common Command Set. Call it a CD-ROM if it reports itself 8099 * as a RODIRECT device. 8100 */ 8101 switch (devp->sd_inq->inq_dtype) { 8102 case DTYPE_RODIRECT: 8103 un->un_node_type = DDI_NT_CD_CHAN; 8104 un->un_ctype = CTYPE_CDROM; 8105 break; 8106 case DTYPE_OPTICAL: 8107 un->un_node_type = DDI_NT_BLOCK_CHAN; 8108 un->un_ctype = CTYPE_ROD; 8109 break; 8110 default: 8111 un->un_node_type = DDI_NT_BLOCK_CHAN; 8112 un->un_ctype = CTYPE_CCS; 8113 break; 8114 } 8115 8116 /* 8117 * Try to read the interconnect type from the HBA. 8118 * 8119 * Note: This driver is currently compiled as two binaries, a parallel 8120 * scsi version (sd) and a fibre channel version (ssd). All functional 8121 * differences are determined at compile time. In the future a single 8122 * binary will be provided and the inteconnect type will be used to 8123 * differentiate between fibre and parallel scsi behaviors. At that time 8124 * it will be necessary for all fibre channel HBAs to support this 8125 * property. 8126 * 8127 * set un_f_is_fiber to TRUE ( default fiber ) 8128 */ 8129 un->un_f_is_fibre = TRUE; 8130 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 8131 case INTERCONNECT_SSA: 8132 un->un_interconnect_type = SD_INTERCONNECT_SSA; 8133 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8134 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 8135 break; 8136 case INTERCONNECT_PARALLEL: 8137 un->un_f_is_fibre = FALSE; 8138 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8139 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8140 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 8141 break; 8142 case INTERCONNECT_SATA: 8143 un->un_f_is_fibre = FALSE; 8144 un->un_interconnect_type = SD_INTERCONNECT_SATA; 8145 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8146 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 8147 break; 8148 case INTERCONNECT_FIBRE: 8149 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 8150 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8151 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 8152 break; 8153 case INTERCONNECT_FABRIC: 8154 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 8155 un->un_node_type = DDI_NT_BLOCK_FABRIC; 8156 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8157 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 8158 break; 8159 default: 8160 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 8161 /* 8162 * The HBA does not support the "interconnect-type" property 8163 * (or did not provide a recognized type). 8164 * 8165 * Note: This will be obsoleted when a single fibre channel 8166 * and parallel scsi driver is delivered. In the meantime the 8167 * interconnect type will be set to the platform default.If that 8168 * type is not parallel SCSI, it means that we should be 8169 * assuming "ssd" semantics. However, here this also means that 8170 * the FC HBA is not supporting the "interconnect-type" property 8171 * like we expect it to, so log this occurrence. 8172 */ 8173 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 8174 if (!SD_IS_PARALLEL_SCSI(un)) { 8175 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8176 "sd_unit_attach: un:0x%p Assuming " 8177 "INTERCONNECT_FIBRE\n", un); 8178 } else { 8179 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8180 "sd_unit_attach: un:0x%p Assuming " 8181 "INTERCONNECT_PARALLEL\n", un); 8182 un->un_f_is_fibre = FALSE; 8183 } 8184 #else 8185 /* 8186 * Note: This source will be implemented when a single fibre 8187 * channel and parallel scsi driver is delivered. The default 8188 * will be to assume that if a device does not support the 8189 * "interconnect-type" property it is a parallel SCSI HBA and 8190 * we will set the interconnect type for parallel scsi. 8191 */ 8192 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8193 un->un_f_is_fibre = FALSE; 8194 #endif 8195 break; 8196 } 8197 8198 if (un->un_f_is_fibre == TRUE) { 8199 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 8200 SCSI_VERSION_3) { 8201 switch (un->un_interconnect_type) { 8202 case SD_INTERCONNECT_FIBRE: 8203 case SD_INTERCONNECT_SSA: 8204 un->un_node_type = DDI_NT_BLOCK_WWN; 8205 break; 8206 default: 8207 break; 8208 } 8209 } 8210 } 8211 8212 /* 8213 * Initialize the Request Sense command for the target 8214 */ 8215 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 8216 goto alloc_rqs_failed; 8217 } 8218 8219 /* 8220 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 8221 * with seperate binary for sd and ssd. 8222 * 8223 * x86 has 1 binary, un_retry_count is set base on connection type. 8224 * The hardcoded values will go away when Sparc uses 1 binary 8225 * for sd and ssd. This hardcoded values need to match 8226 * SD_RETRY_COUNT in sddef.h 8227 * The value used is base on interconnect type. 8228 * fibre = 3, parallel = 5 8229 */ 8230 #if defined(__i386) || defined(__amd64) 8231 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 8232 #else 8233 un->un_retry_count = SD_RETRY_COUNT; 8234 #endif 8235 8236 /* 8237 * Set the per disk retry count to the default number of retries 8238 * for disks and CDROMs. This value can be overridden by the 8239 * disk property list or an entry in sd.conf. 8240 */ 8241 un->un_notready_retry_count = 8242 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 8243 : DISK_NOT_READY_RETRY_COUNT(un); 8244 8245 /* 8246 * Set the busy retry count to the default value of un_retry_count. 8247 * This can be overridden by entries in sd.conf or the device 8248 * config table. 8249 */ 8250 un->un_busy_retry_count = un->un_retry_count; 8251 8252 /* 8253 * Init the reset threshold for retries. This number determines 8254 * how many retries must be performed before a reset can be issued 8255 * (for certain error conditions). This can be overridden by entries 8256 * in sd.conf or the device config table. 8257 */ 8258 un->un_reset_retry_count = (un->un_retry_count / 2); 8259 8260 /* 8261 * Set the victim_retry_count to the default un_retry_count 8262 */ 8263 un->un_victim_retry_count = (2 * un->un_retry_count); 8264 8265 /* 8266 * Set the reservation release timeout to the default value of 8267 * 5 seconds. This can be overridden by entries in ssd.conf or the 8268 * device config table. 8269 */ 8270 un->un_reserve_release_time = 5; 8271 8272 /* 8273 * Set up the default maximum transfer size. Note that this may 8274 * get updated later in the attach, when setting up default wide 8275 * operations for disks. 8276 */ 8277 #if defined(__i386) || defined(__amd64) 8278 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 8279 #else 8280 un->un_max_xfer_size = (uint_t)maxphys; 8281 #endif 8282 8283 /* 8284 * Get "allow bus device reset" property (defaults to "enabled" if 8285 * the property was not defined). This is to disable bus resets for 8286 * certain kinds of error recovery. Note: In the future when a run-time 8287 * fibre check is available the soft state flag should default to 8288 * enabled. 8289 */ 8290 if (un->un_f_is_fibre == TRUE) { 8291 un->un_f_allow_bus_device_reset = TRUE; 8292 } else { 8293 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8294 "allow-bus-device-reset", 1) != 0) { 8295 un->un_f_allow_bus_device_reset = TRUE; 8296 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8297 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 8298 un); 8299 } else { 8300 un->un_f_allow_bus_device_reset = FALSE; 8301 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8302 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 8303 un); 8304 } 8305 } 8306 8307 /* 8308 * Check if this is an ATAPI device. ATAPI devices use Group 1 8309 * Read/Write commands and Group 2 Mode Sense/Select commands. 8310 * 8311 * Note: The "obsolete" way of doing this is to check for the "atapi" 8312 * property. The new "variant" property with a value of "atapi" has been 8313 * introduced so that future 'variants' of standard SCSI behavior (like 8314 * atapi) could be specified by the underlying HBA drivers by supplying 8315 * a new value for the "variant" property, instead of having to define a 8316 * new property. 8317 */ 8318 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 8319 un->un_f_cfg_is_atapi = TRUE; 8320 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8321 "sd_unit_attach: un:0x%p Atapi device\n", un); 8322 } 8323 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8324 &variantp) == DDI_PROP_SUCCESS) { 8325 if (strcmp(variantp, "atapi") == 0) { 8326 un->un_f_cfg_is_atapi = TRUE; 8327 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8328 "sd_unit_attach: un:0x%p Atapi device\n", un); 8329 } 8330 ddi_prop_free(variantp); 8331 } 8332 8333 un->un_cmd_timeout = SD_IO_TIME; 8334 8335 /* Info on current states, statuses, etc. (Updated frequently) */ 8336 un->un_state = SD_STATE_NORMAL; 8337 un->un_last_state = SD_STATE_NORMAL; 8338 8339 /* Control & status info for command throttling */ 8340 un->un_throttle = sd_max_throttle; 8341 un->un_saved_throttle = sd_max_throttle; 8342 un->un_min_throttle = sd_min_throttle; 8343 8344 if (un->un_f_is_fibre == TRUE) { 8345 un->un_f_use_adaptive_throttle = TRUE; 8346 } else { 8347 un->un_f_use_adaptive_throttle = FALSE; 8348 } 8349 8350 /* Removable media support. */ 8351 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8352 un->un_mediastate = DKIO_NONE; 8353 un->un_specified_mediastate = DKIO_NONE; 8354 8355 /* CVs for suspend/resume (PM or DR) */ 8356 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8357 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8358 8359 /* Power management support. */ 8360 un->un_power_level = SD_SPINDLE_UNINIT; 8361 8362 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8363 un->un_f_wcc_inprog = 0; 8364 8365 /* 8366 * The open/close semaphore is used to serialize threads executing 8367 * in the driver's open & close entry point routines for a given 8368 * instance. 8369 */ 8370 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8371 8372 /* 8373 * The conf file entry and softstate variable is a forceful override, 8374 * meaning a non-zero value must be entered to change the default. 8375 */ 8376 un->un_f_disksort_disabled = FALSE; 8377 8378 /* 8379 * Retrieve the properties from the static driver table or the driver 8380 * configuration file (.conf) for this unit and update the soft state 8381 * for the device as needed for the indicated properties. 8382 * Note: the property configuration needs to occur here as some of the 8383 * following routines may have dependancies on soft state flags set 8384 * as part of the driver property configuration. 8385 */ 8386 sd_read_unit_properties(un); 8387 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8388 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8389 8390 /* 8391 * Only if a device has "hotpluggable" property, it is 8392 * treated as hotpluggable device. Otherwise, it is 8393 * regarded as non-hotpluggable one. 8394 */ 8395 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8396 -1) != -1) { 8397 un->un_f_is_hotpluggable = TRUE; 8398 } 8399 8400 /* 8401 * set unit's attributes(flags) according to "hotpluggable" and 8402 * RMB bit in INQUIRY data. 8403 */ 8404 sd_set_unit_attributes(un, devi); 8405 8406 /* 8407 * By default, we mark the capacity, lbasize, and geometry 8408 * as invalid. Only if we successfully read a valid capacity 8409 * will we update the un_blockcount and un_tgt_blocksize with the 8410 * valid values (the geometry will be validated later). 8411 */ 8412 un->un_f_blockcount_is_valid = FALSE; 8413 un->un_f_tgt_blocksize_is_valid = FALSE; 8414 un->un_f_geometry_is_valid = FALSE; 8415 8416 /* 8417 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8418 * otherwise. 8419 */ 8420 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8421 un->un_blockcount = 0; 8422 8423 /* 8424 * Set up the per-instance info needed to determine the correct 8425 * CDBs and other info for issuing commands to the target. 8426 */ 8427 sd_init_cdb_limits(un); 8428 8429 /* 8430 * Set up the IO chains to use, based upon the target type. 8431 */ 8432 if (un->un_f_non_devbsize_supported) { 8433 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8434 } else { 8435 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8436 } 8437 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8438 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8439 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8440 8441 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8442 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8443 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8444 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8445 8446 8447 if (ISCD(un)) { 8448 un->un_additional_codes = sd_additional_codes; 8449 } else { 8450 un->un_additional_codes = NULL; 8451 } 8452 8453 /* 8454 * Create the kstats here so they can be available for attach-time 8455 * routines that send commands to the unit (either polled or via 8456 * sd_send_scsi_cmd). 8457 * 8458 * Note: This is a critical sequence that needs to be maintained: 8459 * 1) Instantiate the kstats here, before any routines using the 8460 * iopath (i.e. sd_send_scsi_cmd). 8461 * 2) Initialize the error stats (sd_set_errstats) and partition 8462 * stats (sd_set_pstats), following sd_validate_geometry(), 8463 * sd_register_devid(), and sd_cache_control(). 8464 */ 8465 8466 un->un_stats = kstat_create(sd_label, instance, 8467 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8468 if (un->un_stats != NULL) { 8469 un->un_stats->ks_lock = SD_MUTEX(un); 8470 kstat_install(un->un_stats); 8471 } 8472 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8473 "sd_unit_attach: un:0x%p un_stats created\n", un); 8474 8475 sd_create_errstats(un, instance); 8476 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8477 "sd_unit_attach: un:0x%p errstats created\n", un); 8478 8479 /* 8480 * The following if/else code was relocated here from below as part 8481 * of the fix for bug (4430280). However with the default setup added 8482 * on entry to this routine, it's no longer absolutely necessary for 8483 * this to be before the call to sd_spin_up_unit. 8484 */ 8485 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 8486 /* 8487 * If SCSI-2 tagged queueing is supported by the target 8488 * and by the host adapter then we will enable it. 8489 */ 8490 un->un_tagflags = 0; 8491 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8492 (devp->sd_inq->inq_cmdque) && 8493 (un->un_f_arq_enabled == TRUE)) { 8494 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8495 1, 1) == 1) { 8496 un->un_tagflags = FLAG_STAG; 8497 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8498 "sd_unit_attach: un:0x%p tag queueing " 8499 "enabled\n", un); 8500 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8501 "untagged-qing", 0) == 1) { 8502 un->un_f_opt_queueing = TRUE; 8503 un->un_saved_throttle = un->un_throttle = 8504 min(un->un_throttle, 3); 8505 } else { 8506 un->un_f_opt_queueing = FALSE; 8507 un->un_saved_throttle = un->un_throttle = 1; 8508 } 8509 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8510 == 1) && (un->un_f_arq_enabled == TRUE)) { 8511 /* The Host Adapter supports internal queueing. */ 8512 un->un_f_opt_queueing = TRUE; 8513 un->un_saved_throttle = un->un_throttle = 8514 min(un->un_throttle, 3); 8515 } else { 8516 un->un_f_opt_queueing = FALSE; 8517 un->un_saved_throttle = un->un_throttle = 1; 8518 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8519 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8520 } 8521 8522 /* 8523 * Enable large transfers for SATA/SAS drives 8524 */ 8525 if (SD_IS_SERIAL(un)) { 8526 un->un_max_xfer_size = 8527 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8528 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8529 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8530 "sd_unit_attach: un:0x%p max transfer " 8531 "size=0x%x\n", un, un->un_max_xfer_size); 8532 8533 } 8534 8535 /* Setup or tear down default wide operations for disks */ 8536 8537 /* 8538 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8539 * and "ssd_max_xfer_size" to exist simultaneously on the same 8540 * system and be set to different values. In the future this 8541 * code may need to be updated when the ssd module is 8542 * obsoleted and removed from the system. (4299588) 8543 */ 8544 if (SD_IS_PARALLEL_SCSI(un) && 8545 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 8546 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8547 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8548 1, 1) == 1) { 8549 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8550 "sd_unit_attach: un:0x%p Wide Transfer " 8551 "enabled\n", un); 8552 } 8553 8554 /* 8555 * If tagged queuing has also been enabled, then 8556 * enable large xfers 8557 */ 8558 if (un->un_saved_throttle == sd_max_throttle) { 8559 un->un_max_xfer_size = 8560 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8561 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8562 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8563 "sd_unit_attach: un:0x%p max transfer " 8564 "size=0x%x\n", un, un->un_max_xfer_size); 8565 } 8566 } else { 8567 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8568 0, 1) == 1) { 8569 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8570 "sd_unit_attach: un:0x%p " 8571 "Wide Transfer disabled\n", un); 8572 } 8573 } 8574 } else { 8575 un->un_tagflags = FLAG_STAG; 8576 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8577 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8578 } 8579 8580 /* 8581 * If this target supports LUN reset, try to enable it. 8582 */ 8583 if (un->un_f_lun_reset_enabled) { 8584 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8585 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8586 "un:0x%p lun_reset capability set\n", un); 8587 } else { 8588 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8589 "un:0x%p lun-reset capability not set\n", un); 8590 } 8591 } 8592 8593 /* 8594 * At this point in the attach, we have enough info in the 8595 * soft state to be able to issue commands to the target. 8596 * 8597 * All command paths used below MUST issue their commands as 8598 * SD_PATH_DIRECT. This is important as intermediate layers 8599 * are not all initialized yet (such as PM). 8600 */ 8601 8602 /* 8603 * Send a TEST UNIT READY command to the device. This should clear 8604 * any outstanding UNIT ATTENTION that may be present. 8605 * 8606 * Note: Don't check for success, just track if there is a reservation, 8607 * this is a throw away command to clear any unit attentions. 8608 * 8609 * Note: This MUST be the first command issued to the target during 8610 * attach to ensure power on UNIT ATTENTIONS are cleared. 8611 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8612 * with attempts at spinning up a device with no media. 8613 */ 8614 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8615 reservation_flag = SD_TARGET_IS_RESERVED; 8616 } 8617 8618 /* 8619 * If the device is NOT a removable media device, attempt to spin 8620 * it up (using the START_STOP_UNIT command) and read its capacity 8621 * (using the READ CAPACITY command). Note, however, that either 8622 * of these could fail and in some cases we would continue with 8623 * the attach despite the failure (see below). 8624 */ 8625 if (un->un_f_descr_format_supported) { 8626 switch (sd_spin_up_unit(un)) { 8627 case 0: 8628 /* 8629 * Spin-up was successful; now try to read the 8630 * capacity. If successful then save the results 8631 * and mark the capacity & lbasize as valid. 8632 */ 8633 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8634 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8635 8636 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8637 &lbasize, SD_PATH_DIRECT)) { 8638 case 0: { 8639 if (capacity > DK_MAX_BLOCKS) { 8640 #ifdef _LP64 8641 if (capacity + 1 > 8642 SD_GROUP1_MAX_ADDRESS) { 8643 /* 8644 * Enable descriptor format 8645 * sense data so that we can 8646 * get 64 bit sense data 8647 * fields. 8648 */ 8649 sd_enable_descr_sense(un); 8650 } 8651 #else 8652 /* 32-bit kernels can't handle this */ 8653 scsi_log(SD_DEVINFO(un), 8654 sd_label, CE_WARN, 8655 "disk has %llu blocks, which " 8656 "is too large for a 32-bit " 8657 "kernel", capacity); 8658 8659 #if defined(__i386) || defined(__amd64) 8660 /* 8661 * Refer to comments related to off-by-1 8662 * at the header of this file. 8663 * 1TB disk was treated as (1T - 512)B 8664 * in the past, so that it might has 8665 * valid VTOC and solaris partitions, 8666 * we have to allow it to continue to 8667 * work. 8668 */ 8669 if (capacity -1 > DK_MAX_BLOCKS) 8670 #endif 8671 goto spinup_failed; 8672 #endif 8673 } 8674 8675 /* 8676 * Here it's not necessary to check the case: 8677 * the capacity of the device is bigger than 8678 * what the max hba cdb can support. Because 8679 * sd_send_scsi_READ_CAPACITY will retrieve 8680 * the capacity by sending USCSI command, which 8681 * is constrained by the max hba cdb. Actually, 8682 * sd_send_scsi_READ_CAPACITY will return 8683 * EINVAL when using bigger cdb than required 8684 * cdb length. Will handle this case in 8685 * "case EINVAL". 8686 */ 8687 8688 /* 8689 * The following relies on 8690 * sd_send_scsi_READ_CAPACITY never 8691 * returning 0 for capacity and/or lbasize. 8692 */ 8693 sd_update_block_info(un, lbasize, capacity); 8694 8695 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8696 "sd_unit_attach: un:0x%p capacity = %ld " 8697 "blocks; lbasize= %ld.\n", un, 8698 un->un_blockcount, un->un_tgt_blocksize); 8699 8700 break; 8701 } 8702 case EINVAL: 8703 /* 8704 * In the case where the max-cdb-length property 8705 * is smaller than the required CDB length for 8706 * a SCSI device, a target driver can fail to 8707 * attach to that device. 8708 */ 8709 scsi_log(SD_DEVINFO(un), 8710 sd_label, CE_WARN, 8711 "disk capacity is too large " 8712 "for current cdb length"); 8713 goto spinup_failed; 8714 case EACCES: 8715 /* 8716 * Should never get here if the spin-up 8717 * succeeded, but code it in anyway. 8718 * From here, just continue with the attach... 8719 */ 8720 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8721 "sd_unit_attach: un:0x%p " 8722 "sd_send_scsi_READ_CAPACITY " 8723 "returned reservation conflict\n", un); 8724 reservation_flag = SD_TARGET_IS_RESERVED; 8725 break; 8726 default: 8727 /* 8728 * Likewise, should never get here if the 8729 * spin-up succeeded. Just continue with 8730 * the attach... 8731 */ 8732 break; 8733 } 8734 break; 8735 case EACCES: 8736 /* 8737 * Device is reserved by another host. In this case 8738 * we could not spin it up or read the capacity, but 8739 * we continue with the attach anyway. 8740 */ 8741 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8742 "sd_unit_attach: un:0x%p spin-up reservation " 8743 "conflict.\n", un); 8744 reservation_flag = SD_TARGET_IS_RESERVED; 8745 break; 8746 default: 8747 /* Fail the attach if the spin-up failed. */ 8748 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8749 "sd_unit_attach: un:0x%p spin-up failed.", un); 8750 goto spinup_failed; 8751 } 8752 } 8753 8754 /* 8755 * Check to see if this is a MMC drive 8756 */ 8757 if (ISCD(un)) { 8758 sd_set_mmc_caps(un); 8759 } 8760 8761 /* 8762 * Create the minor nodes for the device. 8763 * Note: If we want to support fdisk on both sparc and intel, this will 8764 * have to separate out the notion that VTOC8 is always sparc, and 8765 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8766 * type will have to be determined at run-time, and the fdisk 8767 * partitioning will have to have been read & set up before we 8768 * create the minor nodes. (any other inits (such as kstats) that 8769 * also ought to be done before creating the minor nodes?) (Doesn't 8770 * setting up the minor nodes kind of imply that we're ready to 8771 * handle an open from userland?) 8772 */ 8773 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8774 goto create_minor_nodes_failed; 8775 } 8776 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8777 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8778 8779 /* 8780 * Add a zero-length attribute to tell the world we support 8781 * kernel ioctls (for layered drivers) 8782 */ 8783 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8784 DDI_KERNEL_IOCTL, NULL, 0); 8785 8786 /* 8787 * Add a boolean property to tell the world we support 8788 * the B_FAILFAST flag (for layered drivers) 8789 */ 8790 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8791 "ddi-failfast-supported", NULL, 0); 8792 8793 /* 8794 * Initialize power management 8795 */ 8796 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8797 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8798 sd_setup_pm(un, devi); 8799 if (un->un_f_pm_is_enabled == FALSE) { 8800 /* 8801 * For performance, point to a jump table that does 8802 * not include pm. 8803 * The direct and priority chains don't change with PM. 8804 * 8805 * Note: this is currently done based on individual device 8806 * capabilities. When an interface for determining system 8807 * power enabled state becomes available, or when additional 8808 * layers are added to the command chain, these values will 8809 * have to be re-evaluated for correctness. 8810 */ 8811 if (un->un_f_non_devbsize_supported) { 8812 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8813 } else { 8814 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8815 } 8816 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8817 } 8818 8819 /* 8820 * This property is set to 0 by HA software to avoid retries 8821 * on a reserved disk. (The preferred property name is 8822 * "retry-on-reservation-conflict") (1189689) 8823 * 8824 * Note: The use of a global here can have unintended consequences. A 8825 * per instance variable is preferrable to match the capabilities of 8826 * different underlying hba's (4402600) 8827 */ 8828 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8829 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8830 sd_retry_on_reservation_conflict); 8831 if (sd_retry_on_reservation_conflict != 0) { 8832 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8833 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8834 sd_retry_on_reservation_conflict); 8835 } 8836 8837 /* Set up options for QFULL handling. */ 8838 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8839 "qfull-retries", -1)) != -1) { 8840 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8841 rval, 1); 8842 } 8843 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8844 "qfull-retry-interval", -1)) != -1) { 8845 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8846 rval, 1); 8847 } 8848 8849 /* 8850 * This just prints a message that announces the existence of the 8851 * device. The message is always printed in the system logfile, but 8852 * only appears on the console if the system is booted with the 8853 * -v (verbose) argument. 8854 */ 8855 ddi_report_dev(devi); 8856 8857 /* 8858 * The framework calls driver attach routines single-threaded 8859 * for a given instance. However we still acquire SD_MUTEX here 8860 * because this required for calling the sd_validate_geometry() 8861 * and sd_register_devid() functions. 8862 */ 8863 mutex_enter(SD_MUTEX(un)); 8864 un->un_f_geometry_is_valid = FALSE; 8865 un->un_mediastate = DKIO_NONE; 8866 un->un_reserved = -1; 8867 8868 /* 8869 * Read and validate the device's geometry (ie, disk label) 8870 * A new unformatted drive will not have a valid geometry, but 8871 * the driver needs to successfully attach to this device so 8872 * the drive can be formatted via ioctls. 8873 */ 8874 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8875 ENOTSUP)) && 8876 (un->un_blockcount < DK_MAX_BLOCKS)) { 8877 /* 8878 * We found a small disk with an EFI label on it; 8879 * we need to fix up the minor nodes accordingly. 8880 */ 8881 ddi_remove_minor_node(devi, "h"); 8882 ddi_remove_minor_node(devi, "h,raw"); 8883 (void) ddi_create_minor_node(devi, "wd", 8884 S_IFBLK, 8885 (instance << SDUNIT_SHIFT) | WD_NODE, 8886 un->un_node_type, NULL); 8887 (void) ddi_create_minor_node(devi, "wd,raw", 8888 S_IFCHR, 8889 (instance << SDUNIT_SHIFT) | WD_NODE, 8890 un->un_node_type, NULL); 8891 } 8892 #if defined(__i386) || defined(__amd64) 8893 else if (un->un_f_capacity_adjusted == 1) { 8894 /* 8895 * Refer to comments related to off-by-1 at the 8896 * header of this file. 8897 * Adjust minor node for 1TB disk. 8898 */ 8899 ddi_remove_minor_node(devi, "wd"); 8900 ddi_remove_minor_node(devi, "wd,raw"); 8901 (void) ddi_create_minor_node(devi, "h", 8902 S_IFBLK, 8903 (instance << SDUNIT_SHIFT) | WD_NODE, 8904 un->un_node_type, NULL); 8905 (void) ddi_create_minor_node(devi, "h,raw", 8906 S_IFCHR, 8907 (instance << SDUNIT_SHIFT) | WD_NODE, 8908 un->un_node_type, NULL); 8909 } 8910 #endif 8911 /* 8912 * Read and initialize the devid for the unit. 8913 */ 8914 ASSERT(un->un_errstats != NULL); 8915 if (un->un_f_devid_supported) { 8916 sd_register_devid(un, devi, reservation_flag); 8917 } 8918 mutex_exit(SD_MUTEX(un)); 8919 8920 #if (defined(__fibre)) 8921 /* 8922 * Register callbacks for fibre only. You can't do this soley 8923 * on the basis of the devid_type because this is hba specific. 8924 * We need to query our hba capabilities to find out whether to 8925 * register or not. 8926 */ 8927 if (un->un_f_is_fibre) { 8928 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8929 sd_init_event_callbacks(un); 8930 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8931 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8932 } 8933 } 8934 #endif 8935 8936 if (un->un_f_opt_disable_cache == TRUE) { 8937 /* 8938 * Disable both read cache and write cache. This is 8939 * the historic behavior of the keywords in the config file. 8940 */ 8941 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8942 0) { 8943 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8944 "sd_unit_attach: un:0x%p Could not disable " 8945 "caching", un); 8946 goto devid_failed; 8947 } 8948 } 8949 8950 /* 8951 * Check the value of the WCE bit now and 8952 * set un_f_write_cache_enabled accordingly. 8953 */ 8954 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8955 mutex_enter(SD_MUTEX(un)); 8956 un->un_f_write_cache_enabled = (wc_enabled != 0); 8957 mutex_exit(SD_MUTEX(un)); 8958 8959 /* 8960 * Set the pstat and error stat values here, so data obtained during the 8961 * previous attach-time routines is available. 8962 * 8963 * Note: This is a critical sequence that needs to be maintained: 8964 * 1) Instantiate the kstats before any routines using the iopath 8965 * (i.e. sd_send_scsi_cmd). 8966 * 2) Initialize the error stats (sd_set_errstats) and partition 8967 * stats (sd_set_pstats)here, following sd_validate_geometry(), 8968 * sd_register_devid(), and sd_cache_control(). 8969 */ 8970 if (un->un_f_pkstats_enabled) { 8971 sd_set_pstats(un); 8972 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8973 "sd_unit_attach: un:0x%p pstats created and set\n", un); 8974 } 8975 8976 sd_set_errstats(un); 8977 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8978 "sd_unit_attach: un:0x%p errstats set\n", un); 8979 8980 /* 8981 * Find out what type of reservation this disk supports. 8982 */ 8983 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 8984 case 0: 8985 /* 8986 * SCSI-3 reservations are supported. 8987 */ 8988 un->un_reservation_type = SD_SCSI3_RESERVATION; 8989 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8990 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 8991 break; 8992 case ENOTSUP: 8993 /* 8994 * The PERSISTENT RESERVE IN command would not be recognized by 8995 * a SCSI-2 device, so assume the reservation type is SCSI-2. 8996 */ 8997 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8998 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 8999 un->un_reservation_type = SD_SCSI2_RESERVATION; 9000 break; 9001 default: 9002 /* 9003 * default to SCSI-3 reservations 9004 */ 9005 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9006 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 9007 un->un_reservation_type = SD_SCSI3_RESERVATION; 9008 break; 9009 } 9010 9011 /* 9012 * After successfully attaching an instance, we record the information 9013 * of how many luns have been attached on the relative target and 9014 * controller for parallel SCSI. This information is used when sd tries 9015 * to set the tagged queuing capability in HBA. 9016 */ 9017 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9018 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 9019 } 9020 9021 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9022 "sd_unit_attach: un:0x%p exit success\n", un); 9023 9024 return (DDI_SUCCESS); 9025 9026 /* 9027 * An error occurred during the attach; clean up & return failure. 9028 */ 9029 9030 devid_failed: 9031 9032 setup_pm_failed: 9033 ddi_remove_minor_node(devi, NULL); 9034 9035 create_minor_nodes_failed: 9036 /* 9037 * Cleanup from the scsi_ifsetcap() calls (437868) 9038 */ 9039 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9040 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9041 9042 /* 9043 * Refer to the comments of setting tagged-qing in the beginning of 9044 * sd_unit_attach. We can only disable tagged queuing when there is 9045 * no lun attached on the target. 9046 */ 9047 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 9048 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9049 } 9050 9051 if (un->un_f_is_fibre == FALSE) { 9052 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9053 } 9054 9055 spinup_failed: 9056 9057 mutex_enter(SD_MUTEX(un)); 9058 9059 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 9060 if (un->un_direct_priority_timeid != NULL) { 9061 timeout_id_t temp_id = un->un_direct_priority_timeid; 9062 un->un_direct_priority_timeid = NULL; 9063 mutex_exit(SD_MUTEX(un)); 9064 (void) untimeout(temp_id); 9065 mutex_enter(SD_MUTEX(un)); 9066 } 9067 9068 /* Cancel any pending start/stop timeouts */ 9069 if (un->un_startstop_timeid != NULL) { 9070 timeout_id_t temp_id = un->un_startstop_timeid; 9071 un->un_startstop_timeid = NULL; 9072 mutex_exit(SD_MUTEX(un)); 9073 (void) untimeout(temp_id); 9074 mutex_enter(SD_MUTEX(un)); 9075 } 9076 9077 /* Cancel any pending reset-throttle timeouts */ 9078 if (un->un_reset_throttle_timeid != NULL) { 9079 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9080 un->un_reset_throttle_timeid = NULL; 9081 mutex_exit(SD_MUTEX(un)); 9082 (void) untimeout(temp_id); 9083 mutex_enter(SD_MUTEX(un)); 9084 } 9085 9086 /* Cancel any pending retry timeouts */ 9087 if (un->un_retry_timeid != NULL) { 9088 timeout_id_t temp_id = un->un_retry_timeid; 9089 un->un_retry_timeid = NULL; 9090 mutex_exit(SD_MUTEX(un)); 9091 (void) untimeout(temp_id); 9092 mutex_enter(SD_MUTEX(un)); 9093 } 9094 9095 /* Cancel any pending delayed cv broadcast timeouts */ 9096 if (un->un_dcvb_timeid != NULL) { 9097 timeout_id_t temp_id = un->un_dcvb_timeid; 9098 un->un_dcvb_timeid = NULL; 9099 mutex_exit(SD_MUTEX(un)); 9100 (void) untimeout(temp_id); 9101 mutex_enter(SD_MUTEX(un)); 9102 } 9103 9104 mutex_exit(SD_MUTEX(un)); 9105 9106 /* There should not be any in-progress I/O so ASSERT this check */ 9107 ASSERT(un->un_ncmds_in_transport == 0); 9108 ASSERT(un->un_ncmds_in_driver == 0); 9109 9110 /* Do not free the softstate if the callback routine is active */ 9111 sd_sync_with_callback(un); 9112 9113 /* 9114 * Partition stats apparently are not used with removables. These would 9115 * not have been created during attach, so no need to clean them up... 9116 */ 9117 if (un->un_stats != NULL) { 9118 kstat_delete(un->un_stats); 9119 un->un_stats = NULL; 9120 } 9121 if (un->un_errstats != NULL) { 9122 kstat_delete(un->un_errstats); 9123 un->un_errstats = NULL; 9124 } 9125 9126 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9127 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9128 9129 ddi_prop_remove_all(devi); 9130 sema_destroy(&un->un_semoclose); 9131 cv_destroy(&un->un_state_cv); 9132 9133 getrbuf_failed: 9134 9135 sd_free_rqs(un); 9136 9137 alloc_rqs_failed: 9138 9139 devp->sd_private = NULL; 9140 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 9141 9142 get_softstate_failed: 9143 /* 9144 * Note: the man pages are unclear as to whether or not doing a 9145 * ddi_soft_state_free(sd_state, instance) is the right way to 9146 * clean up after the ddi_soft_state_zalloc() if the subsequent 9147 * ddi_get_soft_state() fails. The implication seems to be 9148 * that the get_soft_state cannot fail if the zalloc succeeds. 9149 */ 9150 ddi_soft_state_free(sd_state, instance); 9151 9152 probe_failed: 9153 scsi_unprobe(devp); 9154 #ifdef SDDEBUG 9155 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 9156 (sd_level_mask & SD_LOGMASK_TRACE)) { 9157 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 9158 (void *)un); 9159 } 9160 #endif 9161 return (DDI_FAILURE); 9162 } 9163 9164 9165 /* 9166 * Function: sd_unit_detach 9167 * 9168 * Description: Performs DDI_DETACH processing for sddetach(). 9169 * 9170 * Return Code: DDI_SUCCESS 9171 * DDI_FAILURE 9172 * 9173 * Context: Kernel thread context 9174 */ 9175 9176 static int 9177 sd_unit_detach(dev_info_t *devi) 9178 { 9179 struct scsi_device *devp; 9180 struct sd_lun *un; 9181 int i; 9182 int tgt; 9183 dev_t dev; 9184 dev_info_t *pdip = ddi_get_parent(devi); 9185 int instance = ddi_get_instance(devi); 9186 9187 mutex_enter(&sd_detach_mutex); 9188 9189 /* 9190 * Fail the detach for any of the following: 9191 * - Unable to get the sd_lun struct for the instance 9192 * - A layered driver has an outstanding open on the instance 9193 * - Another thread is already detaching this instance 9194 * - Another thread is currently performing an open 9195 */ 9196 devp = ddi_get_driver_private(devi); 9197 if ((devp == NULL) || 9198 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 9199 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 9200 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 9201 mutex_exit(&sd_detach_mutex); 9202 return (DDI_FAILURE); 9203 } 9204 9205 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 9206 9207 /* 9208 * Mark this instance as currently in a detach, to inhibit any 9209 * opens from a layered driver. 9210 */ 9211 un->un_detach_count++; 9212 mutex_exit(&sd_detach_mutex); 9213 9214 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 9215 SCSI_ADDR_PROP_TARGET, -1); 9216 9217 dev = sd_make_device(SD_DEVINFO(un)); 9218 9219 #ifndef lint 9220 _NOTE(COMPETING_THREADS_NOW); 9221 #endif 9222 9223 mutex_enter(SD_MUTEX(un)); 9224 9225 /* 9226 * Fail the detach if there are any outstanding layered 9227 * opens on this device. 9228 */ 9229 for (i = 0; i < NDKMAP; i++) { 9230 if (un->un_ocmap.lyropen[i] != 0) { 9231 goto err_notclosed; 9232 } 9233 } 9234 9235 /* 9236 * Verify there are NO outstanding commands issued to this device. 9237 * ie, un_ncmds_in_transport == 0. 9238 * It's possible to have outstanding commands through the physio 9239 * code path, even though everything's closed. 9240 */ 9241 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 9242 (un->un_direct_priority_timeid != NULL) || 9243 (un->un_state == SD_STATE_RWAIT)) { 9244 mutex_exit(SD_MUTEX(un)); 9245 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9246 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 9247 goto err_stillbusy; 9248 } 9249 9250 /* 9251 * If we have the device reserved, release the reservation. 9252 */ 9253 if ((un->un_resvd_status & SD_RESERVE) && 9254 !(un->un_resvd_status & SD_LOST_RESERVE)) { 9255 mutex_exit(SD_MUTEX(un)); 9256 /* 9257 * Note: sd_reserve_release sends a command to the device 9258 * via the sd_ioctlcmd() path, and can sleep. 9259 */ 9260 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 9261 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9262 "sd_dr_detach: Cannot release reservation \n"); 9263 } 9264 } else { 9265 mutex_exit(SD_MUTEX(un)); 9266 } 9267 9268 /* 9269 * Untimeout any reserve recover, throttle reset, restart unit 9270 * and delayed broadcast timeout threads. Protect the timeout pointer 9271 * from getting nulled by their callback functions. 9272 */ 9273 mutex_enter(SD_MUTEX(un)); 9274 if (un->un_resvd_timeid != NULL) { 9275 timeout_id_t temp_id = un->un_resvd_timeid; 9276 un->un_resvd_timeid = NULL; 9277 mutex_exit(SD_MUTEX(un)); 9278 (void) untimeout(temp_id); 9279 mutex_enter(SD_MUTEX(un)); 9280 } 9281 9282 if (un->un_reset_throttle_timeid != NULL) { 9283 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9284 un->un_reset_throttle_timeid = NULL; 9285 mutex_exit(SD_MUTEX(un)); 9286 (void) untimeout(temp_id); 9287 mutex_enter(SD_MUTEX(un)); 9288 } 9289 9290 if (un->un_startstop_timeid != NULL) { 9291 timeout_id_t temp_id = un->un_startstop_timeid; 9292 un->un_startstop_timeid = NULL; 9293 mutex_exit(SD_MUTEX(un)); 9294 (void) untimeout(temp_id); 9295 mutex_enter(SD_MUTEX(un)); 9296 } 9297 9298 if (un->un_dcvb_timeid != NULL) { 9299 timeout_id_t temp_id = un->un_dcvb_timeid; 9300 un->un_dcvb_timeid = NULL; 9301 mutex_exit(SD_MUTEX(un)); 9302 (void) untimeout(temp_id); 9303 } else { 9304 mutex_exit(SD_MUTEX(un)); 9305 } 9306 9307 /* Remove any pending reservation reclaim requests for this device */ 9308 sd_rmv_resv_reclaim_req(dev); 9309 9310 mutex_enter(SD_MUTEX(un)); 9311 9312 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 9313 if (un->un_direct_priority_timeid != NULL) { 9314 timeout_id_t temp_id = un->un_direct_priority_timeid; 9315 un->un_direct_priority_timeid = NULL; 9316 mutex_exit(SD_MUTEX(un)); 9317 (void) untimeout(temp_id); 9318 mutex_enter(SD_MUTEX(un)); 9319 } 9320 9321 /* Cancel any active multi-host disk watch thread requests */ 9322 if (un->un_mhd_token != NULL) { 9323 mutex_exit(SD_MUTEX(un)); 9324 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 9325 if (scsi_watch_request_terminate(un->un_mhd_token, 9326 SCSI_WATCH_TERMINATE_NOWAIT)) { 9327 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9328 "sd_dr_detach: Cannot cancel mhd watch request\n"); 9329 /* 9330 * Note: We are returning here after having removed 9331 * some driver timeouts above. This is consistent with 9332 * the legacy implementation but perhaps the watch 9333 * terminate call should be made with the wait flag set. 9334 */ 9335 goto err_stillbusy; 9336 } 9337 mutex_enter(SD_MUTEX(un)); 9338 un->un_mhd_token = NULL; 9339 } 9340 9341 if (un->un_swr_token != NULL) { 9342 mutex_exit(SD_MUTEX(un)); 9343 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 9344 if (scsi_watch_request_terminate(un->un_swr_token, 9345 SCSI_WATCH_TERMINATE_NOWAIT)) { 9346 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9347 "sd_dr_detach: Cannot cancel swr watch request\n"); 9348 /* 9349 * Note: We are returning here after having removed 9350 * some driver timeouts above. This is consistent with 9351 * the legacy implementation but perhaps the watch 9352 * terminate call should be made with the wait flag set. 9353 */ 9354 goto err_stillbusy; 9355 } 9356 mutex_enter(SD_MUTEX(un)); 9357 un->un_swr_token = NULL; 9358 } 9359 9360 mutex_exit(SD_MUTEX(un)); 9361 9362 /* 9363 * Clear any scsi_reset_notifies. We clear the reset notifies 9364 * if we have not registered one. 9365 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 9366 */ 9367 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 9368 sd_mhd_reset_notify_cb, (caddr_t)un); 9369 9370 /* 9371 * protect the timeout pointers from getting nulled by 9372 * their callback functions during the cancellation process. 9373 * In such a scenario untimeout can be invoked with a null value. 9374 */ 9375 _NOTE(NO_COMPETING_THREADS_NOW); 9376 9377 mutex_enter(&un->un_pm_mutex); 9378 if (un->un_pm_idle_timeid != NULL) { 9379 timeout_id_t temp_id = un->un_pm_idle_timeid; 9380 un->un_pm_idle_timeid = NULL; 9381 mutex_exit(&un->un_pm_mutex); 9382 9383 /* 9384 * Timeout is active; cancel it. 9385 * Note that it'll never be active on a device 9386 * that does not support PM therefore we don't 9387 * have to check before calling pm_idle_component. 9388 */ 9389 (void) untimeout(temp_id); 9390 (void) pm_idle_component(SD_DEVINFO(un), 0); 9391 mutex_enter(&un->un_pm_mutex); 9392 } 9393 9394 /* 9395 * Check whether there is already a timeout scheduled for power 9396 * management. If yes then don't lower the power here, that's. 9397 * the timeout handler's job. 9398 */ 9399 if (un->un_pm_timeid != NULL) { 9400 timeout_id_t temp_id = un->un_pm_timeid; 9401 un->un_pm_timeid = NULL; 9402 mutex_exit(&un->un_pm_mutex); 9403 /* 9404 * Timeout is active; cancel it. 9405 * Note that it'll never be active on a device 9406 * that does not support PM therefore we don't 9407 * have to check before calling pm_idle_component. 9408 */ 9409 (void) untimeout(temp_id); 9410 (void) pm_idle_component(SD_DEVINFO(un), 0); 9411 9412 } else { 9413 mutex_exit(&un->un_pm_mutex); 9414 if ((un->un_f_pm_is_enabled == TRUE) && 9415 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 9416 DDI_SUCCESS)) { 9417 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9418 "sd_dr_detach: Lower power request failed, ignoring.\n"); 9419 /* 9420 * Fix for bug: 4297749, item # 13 9421 * The above test now includes a check to see if PM is 9422 * supported by this device before call 9423 * pm_lower_power(). 9424 * Note, the following is not dead code. The call to 9425 * pm_lower_power above will generate a call back into 9426 * our sdpower routine which might result in a timeout 9427 * handler getting activated. Therefore the following 9428 * code is valid and necessary. 9429 */ 9430 mutex_enter(&un->un_pm_mutex); 9431 if (un->un_pm_timeid != NULL) { 9432 timeout_id_t temp_id = un->un_pm_timeid; 9433 un->un_pm_timeid = NULL; 9434 mutex_exit(&un->un_pm_mutex); 9435 (void) untimeout(temp_id); 9436 (void) pm_idle_component(SD_DEVINFO(un), 0); 9437 } else { 9438 mutex_exit(&un->un_pm_mutex); 9439 } 9440 } 9441 } 9442 9443 /* 9444 * Cleanup from the scsi_ifsetcap() calls (437868) 9445 * Relocated here from above to be after the call to 9446 * pm_lower_power, which was getting errors. 9447 */ 9448 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9449 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9450 9451 /* 9452 * Currently, tagged queuing is supported per target based by HBA. 9453 * Setting this per lun instance actually sets the capability of this 9454 * target in HBA, which affects those luns already attached on the 9455 * same target. So during detach, we can only disable this capability 9456 * only when this is the only lun left on this target. By doing 9457 * this, we assume a target has the same tagged queuing capability 9458 * for every lun. The condition can be removed when HBA is changed to 9459 * support per lun based tagged queuing capability. 9460 */ 9461 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 9462 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9463 } 9464 9465 if (un->un_f_is_fibre == FALSE) { 9466 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9467 } 9468 9469 /* 9470 * Remove any event callbacks, fibre only 9471 */ 9472 if (un->un_f_is_fibre == TRUE) { 9473 if ((un->un_insert_event != NULL) && 9474 (ddi_remove_event_handler(un->un_insert_cb_id) != 9475 DDI_SUCCESS)) { 9476 /* 9477 * Note: We are returning here after having done 9478 * substantial cleanup above. This is consistent 9479 * with the legacy implementation but this may not 9480 * be the right thing to do. 9481 */ 9482 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9483 "sd_dr_detach: Cannot cancel insert event\n"); 9484 goto err_remove_event; 9485 } 9486 un->un_insert_event = NULL; 9487 9488 if ((un->un_remove_event != NULL) && 9489 (ddi_remove_event_handler(un->un_remove_cb_id) != 9490 DDI_SUCCESS)) { 9491 /* 9492 * Note: We are returning here after having done 9493 * substantial cleanup above. This is consistent 9494 * with the legacy implementation but this may not 9495 * be the right thing to do. 9496 */ 9497 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9498 "sd_dr_detach: Cannot cancel remove event\n"); 9499 goto err_remove_event; 9500 } 9501 un->un_remove_event = NULL; 9502 } 9503 9504 /* Do not free the softstate if the callback routine is active */ 9505 sd_sync_with_callback(un); 9506 9507 /* 9508 * Hold the detach mutex here, to make sure that no other threads ever 9509 * can access a (partially) freed soft state structure. 9510 */ 9511 mutex_enter(&sd_detach_mutex); 9512 9513 /* 9514 * Clean up the soft state struct. 9515 * Cleanup is done in reverse order of allocs/inits. 9516 * At this point there should be no competing threads anymore. 9517 */ 9518 9519 /* Unregister and free device id. */ 9520 ddi_devid_unregister(devi); 9521 if (un->un_devid) { 9522 ddi_devid_free(un->un_devid); 9523 un->un_devid = NULL; 9524 } 9525 9526 /* 9527 * Destroy wmap cache if it exists. 9528 */ 9529 if (un->un_wm_cache != NULL) { 9530 kmem_cache_destroy(un->un_wm_cache); 9531 un->un_wm_cache = NULL; 9532 } 9533 9534 /* Remove minor nodes */ 9535 ddi_remove_minor_node(devi, NULL); 9536 9537 /* 9538 * kstat cleanup is done in detach for all device types (4363169). 9539 * We do not want to fail detach if the device kstats are not deleted 9540 * since there is a confusion about the devo_refcnt for the device. 9541 * We just delete the kstats and let detach complete successfully. 9542 */ 9543 if (un->un_stats != NULL) { 9544 kstat_delete(un->un_stats); 9545 un->un_stats = NULL; 9546 } 9547 if (un->un_errstats != NULL) { 9548 kstat_delete(un->un_errstats); 9549 un->un_errstats = NULL; 9550 } 9551 9552 /* Remove partition stats */ 9553 if (un->un_f_pkstats_enabled) { 9554 for (i = 0; i < NSDMAP; i++) { 9555 if (un->un_pstats[i] != NULL) { 9556 kstat_delete(un->un_pstats[i]); 9557 un->un_pstats[i] = NULL; 9558 } 9559 } 9560 } 9561 9562 /* Remove xbuf registration */ 9563 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9564 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9565 9566 /* Remove driver properties */ 9567 ddi_prop_remove_all(devi); 9568 9569 mutex_destroy(&un->un_pm_mutex); 9570 cv_destroy(&un->un_pm_busy_cv); 9571 9572 cv_destroy(&un->un_wcc_cv); 9573 9574 /* Open/close semaphore */ 9575 sema_destroy(&un->un_semoclose); 9576 9577 /* Removable media condvar. */ 9578 cv_destroy(&un->un_state_cv); 9579 9580 /* Suspend/resume condvar. */ 9581 cv_destroy(&un->un_suspend_cv); 9582 cv_destroy(&un->un_disk_busy_cv); 9583 9584 sd_free_rqs(un); 9585 9586 /* Free up soft state */ 9587 devp->sd_private = NULL; 9588 bzero(un, sizeof (struct sd_lun)); 9589 ddi_soft_state_free(sd_state, instance); 9590 9591 mutex_exit(&sd_detach_mutex); 9592 9593 /* This frees up the INQUIRY data associated with the device. */ 9594 scsi_unprobe(devp); 9595 9596 /* 9597 * After successfully detaching an instance, we update the information 9598 * of how many luns have been attached in the relative target and 9599 * controller for parallel SCSI. This information is used when sd tries 9600 * to set the tagged queuing capability in HBA. 9601 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 9602 * check if the device is parallel SCSI. However, we don't need to 9603 * check here because we've already checked during attach. No device 9604 * that is not parallel SCSI is in the chain. 9605 */ 9606 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9607 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9608 } 9609 9610 return (DDI_SUCCESS); 9611 9612 err_notclosed: 9613 mutex_exit(SD_MUTEX(un)); 9614 9615 err_stillbusy: 9616 _NOTE(NO_COMPETING_THREADS_NOW); 9617 9618 err_remove_event: 9619 mutex_enter(&sd_detach_mutex); 9620 un->un_detach_count--; 9621 mutex_exit(&sd_detach_mutex); 9622 9623 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9624 return (DDI_FAILURE); 9625 } 9626 9627 9628 /* 9629 * Driver minor node structure and data table 9630 */ 9631 struct driver_minor_data { 9632 char *name; 9633 minor_t minor; 9634 int type; 9635 }; 9636 9637 static struct driver_minor_data sd_minor_data[] = { 9638 {"a", 0, S_IFBLK}, 9639 {"b", 1, S_IFBLK}, 9640 {"c", 2, S_IFBLK}, 9641 {"d", 3, S_IFBLK}, 9642 {"e", 4, S_IFBLK}, 9643 {"f", 5, S_IFBLK}, 9644 {"g", 6, S_IFBLK}, 9645 {"h", 7, S_IFBLK}, 9646 #if defined(_SUNOS_VTOC_16) 9647 {"i", 8, S_IFBLK}, 9648 {"j", 9, S_IFBLK}, 9649 {"k", 10, S_IFBLK}, 9650 {"l", 11, S_IFBLK}, 9651 {"m", 12, S_IFBLK}, 9652 {"n", 13, S_IFBLK}, 9653 {"o", 14, S_IFBLK}, 9654 {"p", 15, S_IFBLK}, 9655 #endif /* defined(_SUNOS_VTOC_16) */ 9656 #if defined(_FIRMWARE_NEEDS_FDISK) 9657 {"q", 16, S_IFBLK}, 9658 {"r", 17, S_IFBLK}, 9659 {"s", 18, S_IFBLK}, 9660 {"t", 19, S_IFBLK}, 9661 {"u", 20, S_IFBLK}, 9662 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9663 {"a,raw", 0, S_IFCHR}, 9664 {"b,raw", 1, S_IFCHR}, 9665 {"c,raw", 2, S_IFCHR}, 9666 {"d,raw", 3, S_IFCHR}, 9667 {"e,raw", 4, S_IFCHR}, 9668 {"f,raw", 5, S_IFCHR}, 9669 {"g,raw", 6, S_IFCHR}, 9670 {"h,raw", 7, S_IFCHR}, 9671 #if defined(_SUNOS_VTOC_16) 9672 {"i,raw", 8, S_IFCHR}, 9673 {"j,raw", 9, S_IFCHR}, 9674 {"k,raw", 10, S_IFCHR}, 9675 {"l,raw", 11, S_IFCHR}, 9676 {"m,raw", 12, S_IFCHR}, 9677 {"n,raw", 13, S_IFCHR}, 9678 {"o,raw", 14, S_IFCHR}, 9679 {"p,raw", 15, S_IFCHR}, 9680 #endif /* defined(_SUNOS_VTOC_16) */ 9681 #if defined(_FIRMWARE_NEEDS_FDISK) 9682 {"q,raw", 16, S_IFCHR}, 9683 {"r,raw", 17, S_IFCHR}, 9684 {"s,raw", 18, S_IFCHR}, 9685 {"t,raw", 19, S_IFCHR}, 9686 {"u,raw", 20, S_IFCHR}, 9687 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9688 {0} 9689 }; 9690 9691 static struct driver_minor_data sd_minor_data_efi[] = { 9692 {"a", 0, S_IFBLK}, 9693 {"b", 1, S_IFBLK}, 9694 {"c", 2, S_IFBLK}, 9695 {"d", 3, S_IFBLK}, 9696 {"e", 4, S_IFBLK}, 9697 {"f", 5, S_IFBLK}, 9698 {"g", 6, S_IFBLK}, 9699 {"wd", 7, S_IFBLK}, 9700 #if defined(_FIRMWARE_NEEDS_FDISK) 9701 {"q", 16, S_IFBLK}, 9702 {"r", 17, S_IFBLK}, 9703 {"s", 18, S_IFBLK}, 9704 {"t", 19, S_IFBLK}, 9705 {"u", 20, S_IFBLK}, 9706 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9707 {"a,raw", 0, S_IFCHR}, 9708 {"b,raw", 1, S_IFCHR}, 9709 {"c,raw", 2, S_IFCHR}, 9710 {"d,raw", 3, S_IFCHR}, 9711 {"e,raw", 4, S_IFCHR}, 9712 {"f,raw", 5, S_IFCHR}, 9713 {"g,raw", 6, S_IFCHR}, 9714 {"wd,raw", 7, S_IFCHR}, 9715 #if defined(_FIRMWARE_NEEDS_FDISK) 9716 {"q,raw", 16, S_IFCHR}, 9717 {"r,raw", 17, S_IFCHR}, 9718 {"s,raw", 18, S_IFCHR}, 9719 {"t,raw", 19, S_IFCHR}, 9720 {"u,raw", 20, S_IFCHR}, 9721 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9722 {0} 9723 }; 9724 9725 9726 /* 9727 * Function: sd_create_minor_nodes 9728 * 9729 * Description: Create the minor device nodes for the instance. 9730 * 9731 * Arguments: un - driver soft state (unit) structure 9732 * devi - pointer to device info structure 9733 * 9734 * Return Code: DDI_SUCCESS 9735 * DDI_FAILURE 9736 * 9737 * Context: Kernel thread context 9738 */ 9739 9740 static int 9741 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9742 { 9743 struct driver_minor_data *dmdp; 9744 struct scsi_device *devp; 9745 int instance; 9746 char name[48]; 9747 9748 ASSERT(un != NULL); 9749 devp = ddi_get_driver_private(devi); 9750 instance = ddi_get_instance(devp->sd_dev); 9751 9752 /* 9753 * Create all the minor nodes for this target. 9754 */ 9755 if (un->un_blockcount > DK_MAX_BLOCKS) 9756 dmdp = sd_minor_data_efi; 9757 else 9758 dmdp = sd_minor_data; 9759 while (dmdp->name != NULL) { 9760 9761 (void) sprintf(name, "%s", dmdp->name); 9762 9763 if (ddi_create_minor_node(devi, name, dmdp->type, 9764 (instance << SDUNIT_SHIFT) | dmdp->minor, 9765 un->un_node_type, NULL) == DDI_FAILURE) { 9766 /* 9767 * Clean up any nodes that may have been created, in 9768 * case this fails in the middle of the loop. 9769 */ 9770 ddi_remove_minor_node(devi, NULL); 9771 return (DDI_FAILURE); 9772 } 9773 dmdp++; 9774 } 9775 9776 return (DDI_SUCCESS); 9777 } 9778 9779 9780 /* 9781 * Function: sd_create_errstats 9782 * 9783 * Description: This routine instantiates the device error stats. 9784 * 9785 * Note: During attach the stats are instantiated first so they are 9786 * available for attach-time routines that utilize the driver 9787 * iopath to send commands to the device. The stats are initialized 9788 * separately so data obtained during some attach-time routines is 9789 * available. (4362483) 9790 * 9791 * Arguments: un - driver soft state (unit) structure 9792 * instance - driver instance 9793 * 9794 * Context: Kernel thread context 9795 */ 9796 9797 static void 9798 sd_create_errstats(struct sd_lun *un, int instance) 9799 { 9800 struct sd_errstats *stp; 9801 char kstatmodule_err[KSTAT_STRLEN]; 9802 char kstatname[KSTAT_STRLEN]; 9803 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9804 9805 ASSERT(un != NULL); 9806 9807 if (un->un_errstats != NULL) { 9808 return; 9809 } 9810 9811 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9812 "%serr", sd_label); 9813 (void) snprintf(kstatname, sizeof (kstatname), 9814 "%s%d,err", sd_label, instance); 9815 9816 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9817 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9818 9819 if (un->un_errstats == NULL) { 9820 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9821 "sd_create_errstats: Failed kstat_create\n"); 9822 return; 9823 } 9824 9825 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9826 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9827 KSTAT_DATA_UINT32); 9828 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9829 KSTAT_DATA_UINT32); 9830 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9831 KSTAT_DATA_UINT32); 9832 kstat_named_init(&stp->sd_vid, "Vendor", 9833 KSTAT_DATA_CHAR); 9834 kstat_named_init(&stp->sd_pid, "Product", 9835 KSTAT_DATA_CHAR); 9836 kstat_named_init(&stp->sd_revision, "Revision", 9837 KSTAT_DATA_CHAR); 9838 kstat_named_init(&stp->sd_serial, "Serial No", 9839 KSTAT_DATA_CHAR); 9840 kstat_named_init(&stp->sd_capacity, "Size", 9841 KSTAT_DATA_ULONGLONG); 9842 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9843 KSTAT_DATA_UINT32); 9844 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9845 KSTAT_DATA_UINT32); 9846 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9847 KSTAT_DATA_UINT32); 9848 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9849 KSTAT_DATA_UINT32); 9850 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9851 KSTAT_DATA_UINT32); 9852 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9853 KSTAT_DATA_UINT32); 9854 9855 un->un_errstats->ks_private = un; 9856 un->un_errstats->ks_update = nulldev; 9857 9858 kstat_install(un->un_errstats); 9859 } 9860 9861 9862 /* 9863 * Function: sd_set_errstats 9864 * 9865 * Description: This routine sets the value of the vendor id, product id, 9866 * revision, serial number, and capacity device error stats. 9867 * 9868 * Note: During attach the stats are instantiated first so they are 9869 * available for attach-time routines that utilize the driver 9870 * iopath to send commands to the device. The stats are initialized 9871 * separately so data obtained during some attach-time routines is 9872 * available. (4362483) 9873 * 9874 * Arguments: un - driver soft state (unit) structure 9875 * 9876 * Context: Kernel thread context 9877 */ 9878 9879 static void 9880 sd_set_errstats(struct sd_lun *un) 9881 { 9882 struct sd_errstats *stp; 9883 9884 ASSERT(un != NULL); 9885 ASSERT(un->un_errstats != NULL); 9886 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9887 ASSERT(stp != NULL); 9888 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9889 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9890 (void) strncpy(stp->sd_revision.value.c, 9891 un->un_sd->sd_inq->inq_revision, 4); 9892 9893 /* 9894 * All the errstats are persistent across detach/attach, 9895 * so reset all the errstats here in case of the hot 9896 * replacement of disk drives, except for not changed 9897 * Sun qualified drives. 9898 */ 9899 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9900 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9901 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9902 stp->sd_softerrs.value.ui32 = 0; 9903 stp->sd_harderrs.value.ui32 = 0; 9904 stp->sd_transerrs.value.ui32 = 0; 9905 stp->sd_rq_media_err.value.ui32 = 0; 9906 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9907 stp->sd_rq_nodev_err.value.ui32 = 0; 9908 stp->sd_rq_recov_err.value.ui32 = 0; 9909 stp->sd_rq_illrq_err.value.ui32 = 0; 9910 stp->sd_rq_pfa_err.value.ui32 = 0; 9911 } 9912 9913 /* 9914 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9915 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9916 * (4376302)) 9917 */ 9918 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9919 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9920 sizeof (SD_INQUIRY(un)->inq_serial)); 9921 } 9922 9923 if (un->un_f_blockcount_is_valid != TRUE) { 9924 /* 9925 * Set capacity error stat to 0 for no media. This ensures 9926 * a valid capacity is displayed in response to 'iostat -E' 9927 * when no media is present in the device. 9928 */ 9929 stp->sd_capacity.value.ui64 = 0; 9930 } else { 9931 /* 9932 * Multiply un_blockcount by un->un_sys_blocksize to get 9933 * capacity. 9934 * 9935 * Note: for non-512 blocksize devices "un_blockcount" has been 9936 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9937 * (un_tgt_blocksize / un->un_sys_blocksize). 9938 */ 9939 stp->sd_capacity.value.ui64 = (uint64_t) 9940 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9941 } 9942 } 9943 9944 9945 /* 9946 * Function: sd_set_pstats 9947 * 9948 * Description: This routine instantiates and initializes the partition 9949 * stats for each partition with more than zero blocks. 9950 * (4363169) 9951 * 9952 * Arguments: un - driver soft state (unit) structure 9953 * 9954 * Context: Kernel thread context 9955 */ 9956 9957 static void 9958 sd_set_pstats(struct sd_lun *un) 9959 { 9960 char kstatname[KSTAT_STRLEN]; 9961 int instance; 9962 int i; 9963 9964 ASSERT(un != NULL); 9965 9966 instance = ddi_get_instance(SD_DEVINFO(un)); 9967 9968 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9969 for (i = 0; i < NSDMAP; i++) { 9970 if ((un->un_pstats[i] == NULL) && 9971 (un->un_map[i].dkl_nblk != 0)) { 9972 (void) snprintf(kstatname, sizeof (kstatname), 9973 "%s%d,%s", sd_label, instance, 9974 sd_minor_data[i].name); 9975 un->un_pstats[i] = kstat_create(sd_label, 9976 instance, kstatname, "partition", KSTAT_TYPE_IO, 9977 1, KSTAT_FLAG_PERSISTENT); 9978 if (un->un_pstats[i] != NULL) { 9979 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 9980 kstat_install(un->un_pstats[i]); 9981 } 9982 } 9983 } 9984 } 9985 9986 9987 #if (defined(__fibre)) 9988 /* 9989 * Function: sd_init_event_callbacks 9990 * 9991 * Description: This routine initializes the insertion and removal event 9992 * callbacks. (fibre only) 9993 * 9994 * Arguments: un - driver soft state (unit) structure 9995 * 9996 * Context: Kernel thread context 9997 */ 9998 9999 static void 10000 sd_init_event_callbacks(struct sd_lun *un) 10001 { 10002 ASSERT(un != NULL); 10003 10004 if ((un->un_insert_event == NULL) && 10005 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 10006 &un->un_insert_event) == DDI_SUCCESS)) { 10007 /* 10008 * Add the callback for an insertion event 10009 */ 10010 (void) ddi_add_event_handler(SD_DEVINFO(un), 10011 un->un_insert_event, sd_event_callback, (void *)un, 10012 &(un->un_insert_cb_id)); 10013 } 10014 10015 if ((un->un_remove_event == NULL) && 10016 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 10017 &un->un_remove_event) == DDI_SUCCESS)) { 10018 /* 10019 * Add the callback for a removal event 10020 */ 10021 (void) ddi_add_event_handler(SD_DEVINFO(un), 10022 un->un_remove_event, sd_event_callback, (void *)un, 10023 &(un->un_remove_cb_id)); 10024 } 10025 } 10026 10027 10028 /* 10029 * Function: sd_event_callback 10030 * 10031 * Description: This routine handles insert/remove events (photon). The 10032 * state is changed to OFFLINE which can be used to supress 10033 * error msgs. (fibre only) 10034 * 10035 * Arguments: un - driver soft state (unit) structure 10036 * 10037 * Context: Callout thread context 10038 */ 10039 /* ARGSUSED */ 10040 static void 10041 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 10042 void *bus_impldata) 10043 { 10044 struct sd_lun *un = (struct sd_lun *)arg; 10045 10046 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 10047 if (event == un->un_insert_event) { 10048 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 10049 mutex_enter(SD_MUTEX(un)); 10050 if (un->un_state == SD_STATE_OFFLINE) { 10051 if (un->un_last_state != SD_STATE_SUSPENDED) { 10052 un->un_state = un->un_last_state; 10053 } else { 10054 /* 10055 * We have gone through SUSPEND/RESUME while 10056 * we were offline. Restore the last state 10057 */ 10058 un->un_state = un->un_save_state; 10059 } 10060 } 10061 mutex_exit(SD_MUTEX(un)); 10062 10063 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 10064 } else if (event == un->un_remove_event) { 10065 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 10066 mutex_enter(SD_MUTEX(un)); 10067 /* 10068 * We need to handle an event callback that occurs during 10069 * the suspend operation, since we don't prevent it. 10070 */ 10071 if (un->un_state != SD_STATE_OFFLINE) { 10072 if (un->un_state != SD_STATE_SUSPENDED) { 10073 New_state(un, SD_STATE_OFFLINE); 10074 } else { 10075 un->un_last_state = SD_STATE_OFFLINE; 10076 } 10077 } 10078 mutex_exit(SD_MUTEX(un)); 10079 } else { 10080 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 10081 "!Unknown event\n"); 10082 } 10083 10084 } 10085 #endif 10086 10087 /* 10088 * Function: sd_cache_control() 10089 * 10090 * Description: This routine is the driver entry point for setting 10091 * read and write caching by modifying the WCE (write cache 10092 * enable) and RCD (read cache disable) bits of mode 10093 * page 8 (MODEPAGE_CACHING). 10094 * 10095 * Arguments: un - driver soft state (unit) structure 10096 * rcd_flag - flag for controlling the read cache 10097 * wce_flag - flag for controlling the write cache 10098 * 10099 * Return Code: EIO 10100 * code returned by sd_send_scsi_MODE_SENSE and 10101 * sd_send_scsi_MODE_SELECT 10102 * 10103 * Context: Kernel Thread 10104 */ 10105 10106 static int 10107 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 10108 { 10109 struct mode_caching *mode_caching_page; 10110 uchar_t *header; 10111 size_t buflen; 10112 int hdrlen; 10113 int bd_len; 10114 int rval = 0; 10115 struct mode_header_grp2 *mhp; 10116 10117 ASSERT(un != NULL); 10118 10119 /* 10120 * Do a test unit ready, otherwise a mode sense may not work if this 10121 * is the first command sent to the device after boot. 10122 */ 10123 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10124 10125 if (un->un_f_cfg_is_atapi == TRUE) { 10126 hdrlen = MODE_HEADER_LENGTH_GRP2; 10127 } else { 10128 hdrlen = MODE_HEADER_LENGTH; 10129 } 10130 10131 /* 10132 * Allocate memory for the retrieved mode page and its headers. Set 10133 * a pointer to the page itself. Use mode_cache_scsi3 to insure 10134 * we get all of the mode sense data otherwise, the mode select 10135 * will fail. mode_cache_scsi3 is a superset of mode_caching. 10136 */ 10137 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 10138 sizeof (struct mode_cache_scsi3); 10139 10140 header = kmem_zalloc(buflen, KM_SLEEP); 10141 10142 /* Get the information from the device. */ 10143 if (un->un_f_cfg_is_atapi == TRUE) { 10144 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10145 MODEPAGE_CACHING, SD_PATH_DIRECT); 10146 } else { 10147 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10148 MODEPAGE_CACHING, SD_PATH_DIRECT); 10149 } 10150 if (rval != 0) { 10151 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10152 "sd_cache_control: Mode Sense Failed\n"); 10153 kmem_free(header, buflen); 10154 return (rval); 10155 } 10156 10157 /* 10158 * Determine size of Block Descriptors in order to locate 10159 * the mode page data. ATAPI devices return 0, SCSI devices 10160 * should return MODE_BLK_DESC_LENGTH. 10161 */ 10162 if (un->un_f_cfg_is_atapi == TRUE) { 10163 mhp = (struct mode_header_grp2 *)header; 10164 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10165 } else { 10166 bd_len = ((struct mode_header *)header)->bdesc_length; 10167 } 10168 10169 if (bd_len > MODE_BLK_DESC_LENGTH) { 10170 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10171 "sd_cache_control: Mode Sense returned invalid " 10172 "block descriptor length\n"); 10173 kmem_free(header, buflen); 10174 return (EIO); 10175 } 10176 10177 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10178 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10179 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10180 " caching page code mismatch %d\n", 10181 mode_caching_page->mode_page.code); 10182 kmem_free(header, buflen); 10183 return (EIO); 10184 } 10185 10186 /* Check the relevant bits on successful mode sense. */ 10187 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 10188 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 10189 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 10190 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 10191 10192 size_t sbuflen; 10193 uchar_t save_pg; 10194 10195 /* 10196 * Construct select buffer length based on the 10197 * length of the sense data returned. 10198 */ 10199 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 10200 sizeof (struct mode_page) + 10201 (int)mode_caching_page->mode_page.length; 10202 10203 /* 10204 * Set the caching bits as requested. 10205 */ 10206 if (rcd_flag == SD_CACHE_ENABLE) 10207 mode_caching_page->rcd = 0; 10208 else if (rcd_flag == SD_CACHE_DISABLE) 10209 mode_caching_page->rcd = 1; 10210 10211 if (wce_flag == SD_CACHE_ENABLE) 10212 mode_caching_page->wce = 1; 10213 else if (wce_flag == SD_CACHE_DISABLE) 10214 mode_caching_page->wce = 0; 10215 10216 /* 10217 * Save the page if the mode sense says the 10218 * drive supports it. 10219 */ 10220 save_pg = mode_caching_page->mode_page.ps ? 10221 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 10222 10223 /* Clear reserved bits before mode select. */ 10224 mode_caching_page->mode_page.ps = 0; 10225 10226 /* 10227 * Clear out mode header for mode select. 10228 * The rest of the retrieved page will be reused. 10229 */ 10230 bzero(header, hdrlen); 10231 10232 if (un->un_f_cfg_is_atapi == TRUE) { 10233 mhp = (struct mode_header_grp2 *)header; 10234 mhp->bdesc_length_hi = bd_len >> 8; 10235 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 10236 } else { 10237 ((struct mode_header *)header)->bdesc_length = bd_len; 10238 } 10239 10240 /* Issue mode select to change the cache settings */ 10241 if (un->un_f_cfg_is_atapi == TRUE) { 10242 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 10243 sbuflen, save_pg, SD_PATH_DIRECT); 10244 } else { 10245 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 10246 sbuflen, save_pg, SD_PATH_DIRECT); 10247 } 10248 } 10249 10250 kmem_free(header, buflen); 10251 return (rval); 10252 } 10253 10254 10255 /* 10256 * Function: sd_get_write_cache_enabled() 10257 * 10258 * Description: This routine is the driver entry point for determining if 10259 * write caching is enabled. It examines the WCE (write cache 10260 * enable) bits of mode page 8 (MODEPAGE_CACHING). 10261 * 10262 * Arguments: un - driver soft state (unit) structure 10263 * is_enabled - pointer to int where write cache enabled state 10264 * is returned (non-zero -> write cache enabled) 10265 * 10266 * 10267 * Return Code: EIO 10268 * code returned by sd_send_scsi_MODE_SENSE 10269 * 10270 * Context: Kernel Thread 10271 * 10272 * NOTE: If ioctl is added to disable write cache, this sequence should 10273 * be followed so that no locking is required for accesses to 10274 * un->un_f_write_cache_enabled: 10275 * do mode select to clear wce 10276 * do synchronize cache to flush cache 10277 * set un->un_f_write_cache_enabled = FALSE 10278 * 10279 * Conversely, an ioctl to enable the write cache should be done 10280 * in this order: 10281 * set un->un_f_write_cache_enabled = TRUE 10282 * do mode select to set wce 10283 */ 10284 10285 static int 10286 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 10287 { 10288 struct mode_caching *mode_caching_page; 10289 uchar_t *header; 10290 size_t buflen; 10291 int hdrlen; 10292 int bd_len; 10293 int rval = 0; 10294 10295 ASSERT(un != NULL); 10296 ASSERT(is_enabled != NULL); 10297 10298 /* in case of error, flag as enabled */ 10299 *is_enabled = TRUE; 10300 10301 /* 10302 * Do a test unit ready, otherwise a mode sense may not work if this 10303 * is the first command sent to the device after boot. 10304 */ 10305 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10306 10307 if (un->un_f_cfg_is_atapi == TRUE) { 10308 hdrlen = MODE_HEADER_LENGTH_GRP2; 10309 } else { 10310 hdrlen = MODE_HEADER_LENGTH; 10311 } 10312 10313 /* 10314 * Allocate memory for the retrieved mode page and its headers. Set 10315 * a pointer to the page itself. 10316 */ 10317 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 10318 header = kmem_zalloc(buflen, KM_SLEEP); 10319 10320 /* Get the information from the device. */ 10321 if (un->un_f_cfg_is_atapi == TRUE) { 10322 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10323 MODEPAGE_CACHING, SD_PATH_DIRECT); 10324 } else { 10325 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10326 MODEPAGE_CACHING, SD_PATH_DIRECT); 10327 } 10328 if (rval != 0) { 10329 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10330 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 10331 kmem_free(header, buflen); 10332 return (rval); 10333 } 10334 10335 /* 10336 * Determine size of Block Descriptors in order to locate 10337 * the mode page data. ATAPI devices return 0, SCSI devices 10338 * should return MODE_BLK_DESC_LENGTH. 10339 */ 10340 if (un->un_f_cfg_is_atapi == TRUE) { 10341 struct mode_header_grp2 *mhp; 10342 mhp = (struct mode_header_grp2 *)header; 10343 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10344 } else { 10345 bd_len = ((struct mode_header *)header)->bdesc_length; 10346 } 10347 10348 if (bd_len > MODE_BLK_DESC_LENGTH) { 10349 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10350 "sd_get_write_cache_enabled: Mode Sense returned invalid " 10351 "block descriptor length\n"); 10352 kmem_free(header, buflen); 10353 return (EIO); 10354 } 10355 10356 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10357 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10358 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10359 " caching page code mismatch %d\n", 10360 mode_caching_page->mode_page.code); 10361 kmem_free(header, buflen); 10362 return (EIO); 10363 } 10364 *is_enabled = mode_caching_page->wce; 10365 10366 kmem_free(header, buflen); 10367 return (0); 10368 } 10369 10370 10371 /* 10372 * Function: sd_make_device 10373 * 10374 * Description: Utility routine to return the Solaris device number from 10375 * the data in the device's dev_info structure. 10376 * 10377 * Return Code: The Solaris device number 10378 * 10379 * Context: Any 10380 */ 10381 10382 static dev_t 10383 sd_make_device(dev_info_t *devi) 10384 { 10385 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 10386 ddi_get_instance(devi) << SDUNIT_SHIFT)); 10387 } 10388 10389 10390 /* 10391 * Function: sd_pm_entry 10392 * 10393 * Description: Called at the start of a new command to manage power 10394 * and busy status of a device. This includes determining whether 10395 * the current power state of the device is sufficient for 10396 * performing the command or whether it must be changed. 10397 * The PM framework is notified appropriately. 10398 * Only with a return status of DDI_SUCCESS will the 10399 * component be busy to the framework. 10400 * 10401 * All callers of sd_pm_entry must check the return status 10402 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 10403 * of DDI_FAILURE indicates the device failed to power up. 10404 * In this case un_pm_count has been adjusted so the result 10405 * on exit is still powered down, ie. count is less than 0. 10406 * Calling sd_pm_exit with this count value hits an ASSERT. 10407 * 10408 * Return Code: DDI_SUCCESS or DDI_FAILURE 10409 * 10410 * Context: Kernel thread context. 10411 */ 10412 10413 static int 10414 sd_pm_entry(struct sd_lun *un) 10415 { 10416 int return_status = DDI_SUCCESS; 10417 10418 ASSERT(!mutex_owned(SD_MUTEX(un))); 10419 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10420 10421 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 10422 10423 if (un->un_f_pm_is_enabled == FALSE) { 10424 SD_TRACE(SD_LOG_IO_PM, un, 10425 "sd_pm_entry: exiting, PM not enabled\n"); 10426 return (return_status); 10427 } 10428 10429 /* 10430 * Just increment a counter if PM is enabled. On the transition from 10431 * 0 ==> 1, mark the device as busy. The iodone side will decrement 10432 * the count with each IO and mark the device as idle when the count 10433 * hits 0. 10434 * 10435 * If the count is less than 0 the device is powered down. If a powered 10436 * down device is successfully powered up then the count must be 10437 * incremented to reflect the power up. Note that it'll get incremented 10438 * a second time to become busy. 10439 * 10440 * Because the following has the potential to change the device state 10441 * and must release the un_pm_mutex to do so, only one thread can be 10442 * allowed through at a time. 10443 */ 10444 10445 mutex_enter(&un->un_pm_mutex); 10446 while (un->un_pm_busy == TRUE) { 10447 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 10448 } 10449 un->un_pm_busy = TRUE; 10450 10451 if (un->un_pm_count < 1) { 10452 10453 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 10454 10455 /* 10456 * Indicate we are now busy so the framework won't attempt to 10457 * power down the device. This call will only fail if either 10458 * we passed a bad component number or the device has no 10459 * components. Neither of these should ever happen. 10460 */ 10461 mutex_exit(&un->un_pm_mutex); 10462 return_status = pm_busy_component(SD_DEVINFO(un), 0); 10463 ASSERT(return_status == DDI_SUCCESS); 10464 10465 mutex_enter(&un->un_pm_mutex); 10466 10467 if (un->un_pm_count < 0) { 10468 mutex_exit(&un->un_pm_mutex); 10469 10470 SD_TRACE(SD_LOG_IO_PM, un, 10471 "sd_pm_entry: power up component\n"); 10472 10473 /* 10474 * pm_raise_power will cause sdpower to be called 10475 * which brings the device power level to the 10476 * desired state, ON in this case. If successful, 10477 * un_pm_count and un_power_level will be updated 10478 * appropriately. 10479 */ 10480 return_status = pm_raise_power(SD_DEVINFO(un), 0, 10481 SD_SPINDLE_ON); 10482 10483 mutex_enter(&un->un_pm_mutex); 10484 10485 if (return_status != DDI_SUCCESS) { 10486 /* 10487 * Power up failed. 10488 * Idle the device and adjust the count 10489 * so the result on exit is that we're 10490 * still powered down, ie. count is less than 0. 10491 */ 10492 SD_TRACE(SD_LOG_IO_PM, un, 10493 "sd_pm_entry: power up failed," 10494 " idle the component\n"); 10495 10496 (void) pm_idle_component(SD_DEVINFO(un), 0); 10497 un->un_pm_count--; 10498 } else { 10499 /* 10500 * Device is powered up, verify the 10501 * count is non-negative. 10502 * This is debug only. 10503 */ 10504 ASSERT(un->un_pm_count == 0); 10505 } 10506 } 10507 10508 if (return_status == DDI_SUCCESS) { 10509 /* 10510 * For performance, now that the device has been tagged 10511 * as busy, and it's known to be powered up, update the 10512 * chain types to use jump tables that do not include 10513 * pm. This significantly lowers the overhead and 10514 * therefore improves performance. 10515 */ 10516 10517 mutex_exit(&un->un_pm_mutex); 10518 mutex_enter(SD_MUTEX(un)); 10519 SD_TRACE(SD_LOG_IO_PM, un, 10520 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10521 un->un_uscsi_chain_type); 10522 10523 if (un->un_f_non_devbsize_supported) { 10524 un->un_buf_chain_type = 10525 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10526 } else { 10527 un->un_buf_chain_type = 10528 SD_CHAIN_INFO_DISK_NO_PM; 10529 } 10530 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10531 10532 SD_TRACE(SD_LOG_IO_PM, un, 10533 " changed uscsi_chain_type to %d\n", 10534 un->un_uscsi_chain_type); 10535 mutex_exit(SD_MUTEX(un)); 10536 mutex_enter(&un->un_pm_mutex); 10537 10538 if (un->un_pm_idle_timeid == NULL) { 10539 /* 300 ms. */ 10540 un->un_pm_idle_timeid = 10541 timeout(sd_pm_idletimeout_handler, un, 10542 (drv_usectohz((clock_t)300000))); 10543 /* 10544 * Include an extra call to busy which keeps the 10545 * device busy with-respect-to the PM layer 10546 * until the timer fires, at which time it'll 10547 * get the extra idle call. 10548 */ 10549 (void) pm_busy_component(SD_DEVINFO(un), 0); 10550 } 10551 } 10552 } 10553 un->un_pm_busy = FALSE; 10554 /* Next... */ 10555 cv_signal(&un->un_pm_busy_cv); 10556 10557 un->un_pm_count++; 10558 10559 SD_TRACE(SD_LOG_IO_PM, un, 10560 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10561 10562 mutex_exit(&un->un_pm_mutex); 10563 10564 return (return_status); 10565 } 10566 10567 10568 /* 10569 * Function: sd_pm_exit 10570 * 10571 * Description: Called at the completion of a command to manage busy 10572 * status for the device. If the device becomes idle the 10573 * PM framework is notified. 10574 * 10575 * Context: Kernel thread context 10576 */ 10577 10578 static void 10579 sd_pm_exit(struct sd_lun *un) 10580 { 10581 ASSERT(!mutex_owned(SD_MUTEX(un))); 10582 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10583 10584 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10585 10586 /* 10587 * After attach the following flag is only read, so don't 10588 * take the penalty of acquiring a mutex for it. 10589 */ 10590 if (un->un_f_pm_is_enabled == TRUE) { 10591 10592 mutex_enter(&un->un_pm_mutex); 10593 un->un_pm_count--; 10594 10595 SD_TRACE(SD_LOG_IO_PM, un, 10596 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10597 10598 ASSERT(un->un_pm_count >= 0); 10599 if (un->un_pm_count == 0) { 10600 mutex_exit(&un->un_pm_mutex); 10601 10602 SD_TRACE(SD_LOG_IO_PM, un, 10603 "sd_pm_exit: idle component\n"); 10604 10605 (void) pm_idle_component(SD_DEVINFO(un), 0); 10606 10607 } else { 10608 mutex_exit(&un->un_pm_mutex); 10609 } 10610 } 10611 10612 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10613 } 10614 10615 10616 /* 10617 * Function: sdopen 10618 * 10619 * Description: Driver's open(9e) entry point function. 10620 * 10621 * Arguments: dev_i - pointer to device number 10622 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10623 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10624 * cred_p - user credential pointer 10625 * 10626 * Return Code: EINVAL 10627 * ENXIO 10628 * EIO 10629 * EROFS 10630 * EBUSY 10631 * 10632 * Context: Kernel thread context 10633 */ 10634 /* ARGSUSED */ 10635 static int 10636 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10637 { 10638 struct sd_lun *un; 10639 int nodelay; 10640 int part; 10641 uint64_t partmask; 10642 int instance; 10643 dev_t dev; 10644 int rval = EIO; 10645 10646 /* Validate the open type */ 10647 if (otyp >= OTYPCNT) { 10648 return (EINVAL); 10649 } 10650 10651 dev = *dev_p; 10652 instance = SDUNIT(dev); 10653 mutex_enter(&sd_detach_mutex); 10654 10655 /* 10656 * Fail the open if there is no softstate for the instance, or 10657 * if another thread somewhere is trying to detach the instance. 10658 */ 10659 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10660 (un->un_detach_count != 0)) { 10661 mutex_exit(&sd_detach_mutex); 10662 /* 10663 * The probe cache only needs to be cleared when open (9e) fails 10664 * with ENXIO (4238046). 10665 */ 10666 /* 10667 * un-conditionally clearing probe cache is ok with 10668 * separate sd/ssd binaries 10669 * x86 platform can be an issue with both parallel 10670 * and fibre in 1 binary 10671 */ 10672 sd_scsi_clear_probe_cache(); 10673 return (ENXIO); 10674 } 10675 10676 /* 10677 * The un_layer_count is to prevent another thread in specfs from 10678 * trying to detach the instance, which can happen when we are 10679 * called from a higher-layer driver instead of thru specfs. 10680 * This will not be needed when DDI provides a layered driver 10681 * interface that allows specfs to know that an instance is in 10682 * use by a layered driver & should not be detached. 10683 * 10684 * Note: the semantics for layered driver opens are exactly one 10685 * close for every open. 10686 */ 10687 if (otyp == OTYP_LYR) { 10688 un->un_layer_count++; 10689 } 10690 10691 /* 10692 * Keep a count of the current # of opens in progress. This is because 10693 * some layered drivers try to call us as a regular open. This can 10694 * cause problems that we cannot prevent, however by keeping this count 10695 * we can at least keep our open and detach routines from racing against 10696 * each other under such conditions. 10697 */ 10698 un->un_opens_in_progress++; 10699 mutex_exit(&sd_detach_mutex); 10700 10701 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10702 part = SDPART(dev); 10703 partmask = 1 << part; 10704 10705 /* 10706 * We use a semaphore here in order to serialize 10707 * open and close requests on the device. 10708 */ 10709 sema_p(&un->un_semoclose); 10710 10711 mutex_enter(SD_MUTEX(un)); 10712 10713 /* 10714 * All device accesses go thru sdstrategy() where we check 10715 * on suspend status but there could be a scsi_poll command, 10716 * which bypasses sdstrategy(), so we need to check pm 10717 * status. 10718 */ 10719 10720 if (!nodelay) { 10721 while ((un->un_state == SD_STATE_SUSPENDED) || 10722 (un->un_state == SD_STATE_PM_CHANGING)) { 10723 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10724 } 10725 10726 mutex_exit(SD_MUTEX(un)); 10727 if (sd_pm_entry(un) != DDI_SUCCESS) { 10728 rval = EIO; 10729 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10730 "sdopen: sd_pm_entry failed\n"); 10731 goto open_failed_with_pm; 10732 } 10733 mutex_enter(SD_MUTEX(un)); 10734 } 10735 10736 /* check for previous exclusive open */ 10737 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10738 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10739 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10740 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10741 10742 if (un->un_exclopen & (partmask)) { 10743 goto excl_open_fail; 10744 } 10745 10746 if (flag & FEXCL) { 10747 int i; 10748 if (un->un_ocmap.lyropen[part]) { 10749 goto excl_open_fail; 10750 } 10751 for (i = 0; i < (OTYPCNT - 1); i++) { 10752 if (un->un_ocmap.regopen[i] & (partmask)) { 10753 goto excl_open_fail; 10754 } 10755 } 10756 } 10757 10758 /* 10759 * Check the write permission if this is a removable media device, 10760 * NDELAY has not been set, and writable permission is requested. 10761 * 10762 * Note: If NDELAY was set and this is write-protected media the WRITE 10763 * attempt will fail with EIO as part of the I/O processing. This is a 10764 * more permissive implementation that allows the open to succeed and 10765 * WRITE attempts to fail when appropriate. 10766 */ 10767 if (un->un_f_chk_wp_open) { 10768 if ((flag & FWRITE) && (!nodelay)) { 10769 mutex_exit(SD_MUTEX(un)); 10770 /* 10771 * Defer the check for write permission on writable 10772 * DVD drive till sdstrategy and will not fail open even 10773 * if FWRITE is set as the device can be writable 10774 * depending upon the media and the media can change 10775 * after the call to open(). 10776 */ 10777 if (un->un_f_dvdram_writable_device == FALSE) { 10778 if (ISCD(un) || sr_check_wp(dev)) { 10779 rval = EROFS; 10780 mutex_enter(SD_MUTEX(un)); 10781 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10782 "write to cd or write protected media\n"); 10783 goto open_fail; 10784 } 10785 } 10786 mutex_enter(SD_MUTEX(un)); 10787 } 10788 } 10789 10790 /* 10791 * If opening in NDELAY/NONBLOCK mode, just return. 10792 * Check if disk is ready and has a valid geometry later. 10793 */ 10794 if (!nodelay) { 10795 mutex_exit(SD_MUTEX(un)); 10796 rval = sd_ready_and_valid(un); 10797 mutex_enter(SD_MUTEX(un)); 10798 /* 10799 * Fail if device is not ready or if the number of disk 10800 * blocks is zero or negative for non CD devices. 10801 */ 10802 if ((rval != SD_READY_VALID) || 10803 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10804 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10805 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10806 "device not ready or invalid disk block value\n"); 10807 goto open_fail; 10808 } 10809 #if defined(__i386) || defined(__amd64) 10810 } else { 10811 uchar_t *cp; 10812 /* 10813 * x86 requires special nodelay handling, so that p0 is 10814 * always defined and accessible. 10815 * Invalidate geometry only if device is not already open. 10816 */ 10817 cp = &un->un_ocmap.chkd[0]; 10818 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10819 if (*cp != (uchar_t)0) { 10820 break; 10821 } 10822 cp++; 10823 } 10824 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10825 un->un_f_geometry_is_valid = FALSE; 10826 } 10827 10828 #endif 10829 } 10830 10831 if (otyp == OTYP_LYR) { 10832 un->un_ocmap.lyropen[part]++; 10833 } else { 10834 un->un_ocmap.regopen[otyp] |= partmask; 10835 } 10836 10837 /* Set up open and exclusive open flags */ 10838 if (flag & FEXCL) { 10839 un->un_exclopen |= (partmask); 10840 } 10841 10842 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10843 "open of part %d type %d\n", part, otyp); 10844 10845 mutex_exit(SD_MUTEX(un)); 10846 if (!nodelay) { 10847 sd_pm_exit(un); 10848 } 10849 10850 sema_v(&un->un_semoclose); 10851 10852 mutex_enter(&sd_detach_mutex); 10853 un->un_opens_in_progress--; 10854 mutex_exit(&sd_detach_mutex); 10855 10856 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10857 return (DDI_SUCCESS); 10858 10859 excl_open_fail: 10860 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10861 rval = EBUSY; 10862 10863 open_fail: 10864 mutex_exit(SD_MUTEX(un)); 10865 10866 /* 10867 * On a failed open we must exit the pm management. 10868 */ 10869 if (!nodelay) { 10870 sd_pm_exit(un); 10871 } 10872 open_failed_with_pm: 10873 sema_v(&un->un_semoclose); 10874 10875 mutex_enter(&sd_detach_mutex); 10876 un->un_opens_in_progress--; 10877 if (otyp == OTYP_LYR) { 10878 un->un_layer_count--; 10879 } 10880 mutex_exit(&sd_detach_mutex); 10881 10882 return (rval); 10883 } 10884 10885 10886 /* 10887 * Function: sdclose 10888 * 10889 * Description: Driver's close(9e) entry point function. 10890 * 10891 * Arguments: dev - device number 10892 * flag - file status flag, informational only 10893 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10894 * cred_p - user credential pointer 10895 * 10896 * Return Code: ENXIO 10897 * 10898 * Context: Kernel thread context 10899 */ 10900 /* ARGSUSED */ 10901 static int 10902 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10903 { 10904 struct sd_lun *un; 10905 uchar_t *cp; 10906 int part; 10907 int nodelay; 10908 int rval = 0; 10909 10910 /* Validate the open type */ 10911 if (otyp >= OTYPCNT) { 10912 return (ENXIO); 10913 } 10914 10915 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10916 return (ENXIO); 10917 } 10918 10919 part = SDPART(dev); 10920 nodelay = flag & (FNDELAY | FNONBLOCK); 10921 10922 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10923 "sdclose: close of part %d type %d\n", part, otyp); 10924 10925 /* 10926 * We use a semaphore here in order to serialize 10927 * open and close requests on the device. 10928 */ 10929 sema_p(&un->un_semoclose); 10930 10931 mutex_enter(SD_MUTEX(un)); 10932 10933 /* Don't proceed if power is being changed. */ 10934 while (un->un_state == SD_STATE_PM_CHANGING) { 10935 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10936 } 10937 10938 if (un->un_exclopen & (1 << part)) { 10939 un->un_exclopen &= ~(1 << part); 10940 } 10941 10942 /* Update the open partition map */ 10943 if (otyp == OTYP_LYR) { 10944 un->un_ocmap.lyropen[part] -= 1; 10945 } else { 10946 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10947 } 10948 10949 cp = &un->un_ocmap.chkd[0]; 10950 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10951 if (*cp != NULL) { 10952 break; 10953 } 10954 cp++; 10955 } 10956 10957 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10958 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10959 10960 /* 10961 * We avoid persistance upon the last close, and set 10962 * the throttle back to the maximum. 10963 */ 10964 un->un_throttle = un->un_saved_throttle; 10965 10966 if (un->un_state == SD_STATE_OFFLINE) { 10967 if (un->un_f_is_fibre == FALSE) { 10968 scsi_log(SD_DEVINFO(un), sd_label, 10969 CE_WARN, "offline\n"); 10970 } 10971 un->un_f_geometry_is_valid = FALSE; 10972 10973 } else { 10974 /* 10975 * Flush any outstanding writes in NVRAM cache. 10976 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10977 * cmd, it may not work for non-Pluto devices. 10978 * SYNCHRONIZE CACHE is not required for removables, 10979 * except DVD-RAM drives. 10980 * 10981 * Also note: because SYNCHRONIZE CACHE is currently 10982 * the only command issued here that requires the 10983 * drive be powered up, only do the power up before 10984 * sending the Sync Cache command. If additional 10985 * commands are added which require a powered up 10986 * drive, the following sequence may have to change. 10987 * 10988 * And finally, note that parallel SCSI on SPARC 10989 * only issues a Sync Cache to DVD-RAM, a newly 10990 * supported device. 10991 */ 10992 #if defined(__i386) || defined(__amd64) 10993 if (un->un_f_sync_cache_supported || 10994 un->un_f_dvdram_writable_device == TRUE) { 10995 #else 10996 if (un->un_f_dvdram_writable_device == TRUE) { 10997 #endif 10998 mutex_exit(SD_MUTEX(un)); 10999 if (sd_pm_entry(un) == DDI_SUCCESS) { 11000 rval = 11001 sd_send_scsi_SYNCHRONIZE_CACHE(un, 11002 NULL); 11003 /* ignore error if not supported */ 11004 if (rval == ENOTSUP) { 11005 rval = 0; 11006 } else if (rval != 0) { 11007 rval = EIO; 11008 } 11009 sd_pm_exit(un); 11010 } else { 11011 rval = EIO; 11012 } 11013 mutex_enter(SD_MUTEX(un)); 11014 } 11015 11016 /* 11017 * For devices which supports DOOR_LOCK, send an ALLOW 11018 * MEDIA REMOVAL command, but don't get upset if it 11019 * fails. We need to raise the power of the drive before 11020 * we can call sd_send_scsi_DOORLOCK() 11021 */ 11022 if (un->un_f_doorlock_supported) { 11023 mutex_exit(SD_MUTEX(un)); 11024 if (sd_pm_entry(un) == DDI_SUCCESS) { 11025 rval = sd_send_scsi_DOORLOCK(un, 11026 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 11027 11028 sd_pm_exit(un); 11029 if (ISCD(un) && (rval != 0) && 11030 (nodelay != 0)) { 11031 rval = ENXIO; 11032 } 11033 } else { 11034 rval = EIO; 11035 } 11036 mutex_enter(SD_MUTEX(un)); 11037 } 11038 11039 /* 11040 * If a device has removable media, invalidate all 11041 * parameters related to media, such as geometry, 11042 * blocksize, and blockcount. 11043 */ 11044 if (un->un_f_has_removable_media) { 11045 sr_ejected(un); 11046 } 11047 11048 /* 11049 * Destroy the cache (if it exists) which was 11050 * allocated for the write maps since this is 11051 * the last close for this media. 11052 */ 11053 if (un->un_wm_cache) { 11054 /* 11055 * Check if there are pending commands. 11056 * and if there are give a warning and 11057 * do not destroy the cache. 11058 */ 11059 if (un->un_ncmds_in_driver > 0) { 11060 scsi_log(SD_DEVINFO(un), 11061 sd_label, CE_WARN, 11062 "Unable to clean up memory " 11063 "because of pending I/O\n"); 11064 } else { 11065 kmem_cache_destroy( 11066 un->un_wm_cache); 11067 un->un_wm_cache = NULL; 11068 } 11069 } 11070 } 11071 } 11072 11073 mutex_exit(SD_MUTEX(un)); 11074 sema_v(&un->un_semoclose); 11075 11076 if (otyp == OTYP_LYR) { 11077 mutex_enter(&sd_detach_mutex); 11078 /* 11079 * The detach routine may run when the layer count 11080 * drops to zero. 11081 */ 11082 un->un_layer_count--; 11083 mutex_exit(&sd_detach_mutex); 11084 } 11085 11086 return (rval); 11087 } 11088 11089 11090 /* 11091 * Function: sd_ready_and_valid 11092 * 11093 * Description: Test if device is ready and has a valid geometry. 11094 * 11095 * Arguments: dev - device number 11096 * un - driver soft state (unit) structure 11097 * 11098 * Return Code: SD_READY_VALID ready and valid label 11099 * SD_READY_NOT_VALID ready, geom ops never applicable 11100 * SD_NOT_READY_VALID not ready, no label 11101 * SD_RESERVED_BY_OTHERS reservation conflict 11102 * 11103 * Context: Never called at interrupt context. 11104 */ 11105 11106 static int 11107 sd_ready_and_valid(struct sd_lun *un) 11108 { 11109 struct sd_errstats *stp; 11110 uint64_t capacity; 11111 uint_t lbasize; 11112 int rval = SD_READY_VALID; 11113 char name_str[48]; 11114 11115 ASSERT(un != NULL); 11116 ASSERT(!mutex_owned(SD_MUTEX(un))); 11117 11118 mutex_enter(SD_MUTEX(un)); 11119 /* 11120 * If a device has removable media, we must check if media is 11121 * ready when checking if this device is ready and valid. 11122 */ 11123 if (un->un_f_has_removable_media) { 11124 mutex_exit(SD_MUTEX(un)); 11125 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 11126 rval = SD_NOT_READY_VALID; 11127 mutex_enter(SD_MUTEX(un)); 11128 goto done; 11129 } 11130 11131 mutex_enter(SD_MUTEX(un)); 11132 if ((un->un_f_geometry_is_valid == FALSE) || 11133 (un->un_f_blockcount_is_valid == FALSE) || 11134 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 11135 11136 /* capacity has to be read every open. */ 11137 mutex_exit(SD_MUTEX(un)); 11138 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 11139 &lbasize, SD_PATH_DIRECT) != 0) { 11140 mutex_enter(SD_MUTEX(un)); 11141 un->un_f_geometry_is_valid = FALSE; 11142 rval = SD_NOT_READY_VALID; 11143 goto done; 11144 } else { 11145 mutex_enter(SD_MUTEX(un)); 11146 sd_update_block_info(un, lbasize, capacity); 11147 } 11148 } 11149 11150 /* 11151 * Check if the media in the device is writable or not. 11152 */ 11153 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 11154 sd_check_for_writable_cd(un); 11155 } 11156 11157 } else { 11158 /* 11159 * Do a test unit ready to clear any unit attention from non-cd 11160 * devices. 11161 */ 11162 mutex_exit(SD_MUTEX(un)); 11163 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 11164 mutex_enter(SD_MUTEX(un)); 11165 } 11166 11167 11168 /* 11169 * If this is a non 512 block device, allocate space for 11170 * the wmap cache. This is being done here since every time 11171 * a media is changed this routine will be called and the 11172 * block size is a function of media rather than device. 11173 */ 11174 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 11175 if (!(un->un_wm_cache)) { 11176 (void) snprintf(name_str, sizeof (name_str), 11177 "%s%d_cache", 11178 ddi_driver_name(SD_DEVINFO(un)), 11179 ddi_get_instance(SD_DEVINFO(un))); 11180 un->un_wm_cache = kmem_cache_create( 11181 name_str, sizeof (struct sd_w_map), 11182 8, sd_wm_cache_constructor, 11183 sd_wm_cache_destructor, NULL, 11184 (void *)un, NULL, 0); 11185 if (!(un->un_wm_cache)) { 11186 rval = ENOMEM; 11187 goto done; 11188 } 11189 } 11190 } 11191 11192 if (un->un_state == SD_STATE_NORMAL) { 11193 /* 11194 * If the target is not yet ready here (defined by a TUR 11195 * failure), invalidate the geometry and print an 'offline' 11196 * message. This is a legacy message, as the state of the 11197 * target is not actually changed to SD_STATE_OFFLINE. 11198 * 11199 * If the TUR fails for EACCES (Reservation Conflict), 11200 * SD_RESERVED_BY_OTHERS will be returned to indicate 11201 * reservation conflict. If the TUR fails for other 11202 * reasons, SD_NOT_READY_VALID will be returned. 11203 */ 11204 int err; 11205 11206 mutex_exit(SD_MUTEX(un)); 11207 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 11208 mutex_enter(SD_MUTEX(un)); 11209 11210 if (err != 0) { 11211 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11212 "offline or reservation conflict\n"); 11213 un->un_f_geometry_is_valid = FALSE; 11214 if (err == EACCES) { 11215 rval = SD_RESERVED_BY_OTHERS; 11216 } else { 11217 rval = SD_NOT_READY_VALID; 11218 } 11219 goto done; 11220 } 11221 } 11222 11223 if (un->un_f_format_in_progress == FALSE) { 11224 /* 11225 * Note: sd_validate_geometry may return TRUE, but that does 11226 * not necessarily mean un_f_geometry_is_valid == TRUE! 11227 */ 11228 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 11229 if (rval == ENOTSUP) { 11230 if (un->un_f_geometry_is_valid == TRUE) 11231 rval = 0; 11232 else { 11233 rval = SD_READY_NOT_VALID; 11234 goto done; 11235 } 11236 } 11237 if (rval != 0) { 11238 /* 11239 * We don't check the validity of geometry for 11240 * CDROMs. Also we assume we have a good label 11241 * even if sd_validate_geometry returned ENOMEM. 11242 */ 11243 if (!ISCD(un) && rval != ENOMEM) { 11244 rval = SD_NOT_READY_VALID; 11245 goto done; 11246 } 11247 } 11248 } 11249 11250 /* 11251 * If this device supports DOOR_LOCK command, try and send 11252 * this command to PREVENT MEDIA REMOVAL, but don't get upset 11253 * if it fails. For a CD, however, it is an error 11254 */ 11255 if (un->un_f_doorlock_supported) { 11256 mutex_exit(SD_MUTEX(un)); 11257 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 11258 SD_PATH_DIRECT) != 0) && ISCD(un)) { 11259 rval = SD_NOT_READY_VALID; 11260 mutex_enter(SD_MUTEX(un)); 11261 goto done; 11262 } 11263 mutex_enter(SD_MUTEX(un)); 11264 } 11265 11266 /* The state has changed, inform the media watch routines */ 11267 un->un_mediastate = DKIO_INSERTED; 11268 cv_broadcast(&un->un_state_cv); 11269 rval = SD_READY_VALID; 11270 11271 done: 11272 11273 /* 11274 * Initialize the capacity kstat value, if no media previously 11275 * (capacity kstat is 0) and a media has been inserted 11276 * (un_blockcount > 0). 11277 */ 11278 if (un->un_errstats != NULL) { 11279 stp = (struct sd_errstats *)un->un_errstats->ks_data; 11280 if ((stp->sd_capacity.value.ui64 == 0) && 11281 (un->un_f_blockcount_is_valid == TRUE)) { 11282 stp->sd_capacity.value.ui64 = 11283 (uint64_t)((uint64_t)un->un_blockcount * 11284 un->un_sys_blocksize); 11285 } 11286 } 11287 11288 mutex_exit(SD_MUTEX(un)); 11289 return (rval); 11290 } 11291 11292 11293 /* 11294 * Function: sdmin 11295 * 11296 * Description: Routine to limit the size of a data transfer. Used in 11297 * conjunction with physio(9F). 11298 * 11299 * Arguments: bp - pointer to the indicated buf(9S) struct. 11300 * 11301 * Context: Kernel thread context. 11302 */ 11303 11304 static void 11305 sdmin(struct buf *bp) 11306 { 11307 struct sd_lun *un; 11308 int instance; 11309 11310 instance = SDUNIT(bp->b_edev); 11311 11312 un = ddi_get_soft_state(sd_state, instance); 11313 ASSERT(un != NULL); 11314 11315 if (bp->b_bcount > un->un_max_xfer_size) { 11316 bp->b_bcount = un->un_max_xfer_size; 11317 } 11318 } 11319 11320 11321 /* 11322 * Function: sdread 11323 * 11324 * Description: Driver's read(9e) entry point function. 11325 * 11326 * Arguments: dev - device number 11327 * uio - structure pointer describing where data is to be stored 11328 * in user's space 11329 * cred_p - user credential pointer 11330 * 11331 * Return Code: ENXIO 11332 * EIO 11333 * EINVAL 11334 * value returned by physio 11335 * 11336 * Context: Kernel thread context. 11337 */ 11338 /* ARGSUSED */ 11339 static int 11340 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 11341 { 11342 struct sd_lun *un = NULL; 11343 int secmask; 11344 int err; 11345 11346 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11347 return (ENXIO); 11348 } 11349 11350 ASSERT(!mutex_owned(SD_MUTEX(un))); 11351 11352 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11353 mutex_enter(SD_MUTEX(un)); 11354 /* 11355 * Because the call to sd_ready_and_valid will issue I/O we 11356 * must wait here if either the device is suspended or 11357 * if it's power level is changing. 11358 */ 11359 while ((un->un_state == SD_STATE_SUSPENDED) || 11360 (un->un_state == SD_STATE_PM_CHANGING)) { 11361 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11362 } 11363 un->un_ncmds_in_driver++; 11364 mutex_exit(SD_MUTEX(un)); 11365 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11366 mutex_enter(SD_MUTEX(un)); 11367 un->un_ncmds_in_driver--; 11368 ASSERT(un->un_ncmds_in_driver >= 0); 11369 mutex_exit(SD_MUTEX(un)); 11370 return (EIO); 11371 } 11372 mutex_enter(SD_MUTEX(un)); 11373 un->un_ncmds_in_driver--; 11374 ASSERT(un->un_ncmds_in_driver >= 0); 11375 mutex_exit(SD_MUTEX(un)); 11376 } 11377 11378 /* 11379 * Read requests are restricted to multiples of the system block size. 11380 */ 11381 secmask = un->un_sys_blocksize - 1; 11382 11383 if (uio->uio_loffset & ((offset_t)(secmask))) { 11384 SD_ERROR(SD_LOG_READ_WRITE, un, 11385 "sdread: file offset not modulo %d\n", 11386 un->un_sys_blocksize); 11387 err = EINVAL; 11388 } else if (uio->uio_iov->iov_len & (secmask)) { 11389 SD_ERROR(SD_LOG_READ_WRITE, un, 11390 "sdread: transfer length not modulo %d\n", 11391 un->un_sys_blocksize); 11392 err = EINVAL; 11393 } else { 11394 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 11395 } 11396 return (err); 11397 } 11398 11399 11400 /* 11401 * Function: sdwrite 11402 * 11403 * Description: Driver's write(9e) entry point function. 11404 * 11405 * Arguments: dev - device number 11406 * uio - structure pointer describing where data is stored in 11407 * user's space 11408 * cred_p - user credential pointer 11409 * 11410 * Return Code: ENXIO 11411 * EIO 11412 * EINVAL 11413 * value returned by physio 11414 * 11415 * Context: Kernel thread context. 11416 */ 11417 /* ARGSUSED */ 11418 static int 11419 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11420 { 11421 struct sd_lun *un = NULL; 11422 int secmask; 11423 int err; 11424 11425 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11426 return (ENXIO); 11427 } 11428 11429 ASSERT(!mutex_owned(SD_MUTEX(un))); 11430 11431 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11432 mutex_enter(SD_MUTEX(un)); 11433 /* 11434 * Because the call to sd_ready_and_valid will issue I/O we 11435 * must wait here if either the device is suspended or 11436 * if it's power level is changing. 11437 */ 11438 while ((un->un_state == SD_STATE_SUSPENDED) || 11439 (un->un_state == SD_STATE_PM_CHANGING)) { 11440 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11441 } 11442 un->un_ncmds_in_driver++; 11443 mutex_exit(SD_MUTEX(un)); 11444 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11445 mutex_enter(SD_MUTEX(un)); 11446 un->un_ncmds_in_driver--; 11447 ASSERT(un->un_ncmds_in_driver >= 0); 11448 mutex_exit(SD_MUTEX(un)); 11449 return (EIO); 11450 } 11451 mutex_enter(SD_MUTEX(un)); 11452 un->un_ncmds_in_driver--; 11453 ASSERT(un->un_ncmds_in_driver >= 0); 11454 mutex_exit(SD_MUTEX(un)); 11455 } 11456 11457 /* 11458 * Write requests are restricted to multiples of the system block size. 11459 */ 11460 secmask = un->un_sys_blocksize - 1; 11461 11462 if (uio->uio_loffset & ((offset_t)(secmask))) { 11463 SD_ERROR(SD_LOG_READ_WRITE, un, 11464 "sdwrite: file offset not modulo %d\n", 11465 un->un_sys_blocksize); 11466 err = EINVAL; 11467 } else if (uio->uio_iov->iov_len & (secmask)) { 11468 SD_ERROR(SD_LOG_READ_WRITE, un, 11469 "sdwrite: transfer length not modulo %d\n", 11470 un->un_sys_blocksize); 11471 err = EINVAL; 11472 } else { 11473 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11474 } 11475 return (err); 11476 } 11477 11478 11479 /* 11480 * Function: sdaread 11481 * 11482 * Description: Driver's aread(9e) entry point function. 11483 * 11484 * Arguments: dev - device number 11485 * aio - structure pointer describing where data is to be stored 11486 * cred_p - user credential pointer 11487 * 11488 * Return Code: ENXIO 11489 * EIO 11490 * EINVAL 11491 * value returned by aphysio 11492 * 11493 * Context: Kernel thread context. 11494 */ 11495 /* ARGSUSED */ 11496 static int 11497 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11498 { 11499 struct sd_lun *un = NULL; 11500 struct uio *uio = aio->aio_uio; 11501 int secmask; 11502 int err; 11503 11504 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11505 return (ENXIO); 11506 } 11507 11508 ASSERT(!mutex_owned(SD_MUTEX(un))); 11509 11510 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11511 mutex_enter(SD_MUTEX(un)); 11512 /* 11513 * Because the call to sd_ready_and_valid will issue I/O we 11514 * must wait here if either the device is suspended or 11515 * if it's power level is changing. 11516 */ 11517 while ((un->un_state == SD_STATE_SUSPENDED) || 11518 (un->un_state == SD_STATE_PM_CHANGING)) { 11519 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11520 } 11521 un->un_ncmds_in_driver++; 11522 mutex_exit(SD_MUTEX(un)); 11523 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11524 mutex_enter(SD_MUTEX(un)); 11525 un->un_ncmds_in_driver--; 11526 ASSERT(un->un_ncmds_in_driver >= 0); 11527 mutex_exit(SD_MUTEX(un)); 11528 return (EIO); 11529 } 11530 mutex_enter(SD_MUTEX(un)); 11531 un->un_ncmds_in_driver--; 11532 ASSERT(un->un_ncmds_in_driver >= 0); 11533 mutex_exit(SD_MUTEX(un)); 11534 } 11535 11536 /* 11537 * Read requests are restricted to multiples of the system block size. 11538 */ 11539 secmask = un->un_sys_blocksize - 1; 11540 11541 if (uio->uio_loffset & ((offset_t)(secmask))) { 11542 SD_ERROR(SD_LOG_READ_WRITE, un, 11543 "sdaread: file offset not modulo %d\n", 11544 un->un_sys_blocksize); 11545 err = EINVAL; 11546 } else if (uio->uio_iov->iov_len & (secmask)) { 11547 SD_ERROR(SD_LOG_READ_WRITE, un, 11548 "sdaread: transfer length not modulo %d\n", 11549 un->un_sys_blocksize); 11550 err = EINVAL; 11551 } else { 11552 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11553 } 11554 return (err); 11555 } 11556 11557 11558 /* 11559 * Function: sdawrite 11560 * 11561 * Description: Driver's awrite(9e) entry point function. 11562 * 11563 * Arguments: dev - device number 11564 * aio - structure pointer describing where data is stored 11565 * cred_p - user credential pointer 11566 * 11567 * Return Code: ENXIO 11568 * EIO 11569 * EINVAL 11570 * value returned by aphysio 11571 * 11572 * Context: Kernel thread context. 11573 */ 11574 /* ARGSUSED */ 11575 static int 11576 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11577 { 11578 struct sd_lun *un = NULL; 11579 struct uio *uio = aio->aio_uio; 11580 int secmask; 11581 int err; 11582 11583 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11584 return (ENXIO); 11585 } 11586 11587 ASSERT(!mutex_owned(SD_MUTEX(un))); 11588 11589 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11590 mutex_enter(SD_MUTEX(un)); 11591 /* 11592 * Because the call to sd_ready_and_valid will issue I/O we 11593 * must wait here if either the device is suspended or 11594 * if it's power level is changing. 11595 */ 11596 while ((un->un_state == SD_STATE_SUSPENDED) || 11597 (un->un_state == SD_STATE_PM_CHANGING)) { 11598 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11599 } 11600 un->un_ncmds_in_driver++; 11601 mutex_exit(SD_MUTEX(un)); 11602 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11603 mutex_enter(SD_MUTEX(un)); 11604 un->un_ncmds_in_driver--; 11605 ASSERT(un->un_ncmds_in_driver >= 0); 11606 mutex_exit(SD_MUTEX(un)); 11607 return (EIO); 11608 } 11609 mutex_enter(SD_MUTEX(un)); 11610 un->un_ncmds_in_driver--; 11611 ASSERT(un->un_ncmds_in_driver >= 0); 11612 mutex_exit(SD_MUTEX(un)); 11613 } 11614 11615 /* 11616 * Write requests are restricted to multiples of the system block size. 11617 */ 11618 secmask = un->un_sys_blocksize - 1; 11619 11620 if (uio->uio_loffset & ((offset_t)(secmask))) { 11621 SD_ERROR(SD_LOG_READ_WRITE, un, 11622 "sdawrite: file offset not modulo %d\n", 11623 un->un_sys_blocksize); 11624 err = EINVAL; 11625 } else if (uio->uio_iov->iov_len & (secmask)) { 11626 SD_ERROR(SD_LOG_READ_WRITE, un, 11627 "sdawrite: transfer length not modulo %d\n", 11628 un->un_sys_blocksize); 11629 err = EINVAL; 11630 } else { 11631 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11632 } 11633 return (err); 11634 } 11635 11636 11637 11638 11639 11640 /* 11641 * Driver IO processing follows the following sequence: 11642 * 11643 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11644 * | | ^ 11645 * v v | 11646 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11647 * | | | | 11648 * v | | | 11649 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11650 * | | ^ ^ 11651 * v v | | 11652 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11653 * | | | | 11654 * +---+ | +------------+ +-------+ 11655 * | | | | 11656 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11657 * | v | | 11658 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11659 * | | ^ | 11660 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11661 * | v | | 11662 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11663 * | | ^ | 11664 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11665 * | v | | 11666 * | sd_checksum_iostart() sd_checksum_iodone() | 11667 * | | ^ | 11668 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11669 * | v | | 11670 * | sd_pm_iostart() sd_pm_iodone() | 11671 * | | ^ | 11672 * | | | | 11673 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11674 * | ^ 11675 * v | 11676 * sd_core_iostart() | 11677 * | | 11678 * | +------>(*destroypkt)() 11679 * +-> sd_start_cmds() <-+ | | 11680 * | | | v 11681 * | | | scsi_destroy_pkt(9F) 11682 * | | | 11683 * +->(*initpkt)() +- sdintr() 11684 * | | | | 11685 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11686 * | +-> scsi_setup_cdb(9F) | 11687 * | | 11688 * +--> scsi_transport(9F) | 11689 * | | 11690 * +----> SCSA ---->+ 11691 * 11692 * 11693 * This code is based upon the following presumtions: 11694 * 11695 * - iostart and iodone functions operate on buf(9S) structures. These 11696 * functions perform the necessary operations on the buf(9S) and pass 11697 * them along to the next function in the chain by using the macros 11698 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11699 * (for iodone side functions). 11700 * 11701 * - The iostart side functions may sleep. The iodone side functions 11702 * are called under interrupt context and may NOT sleep. Therefore 11703 * iodone side functions also may not call iostart side functions. 11704 * (NOTE: iostart side functions should NOT sleep for memory, as 11705 * this could result in deadlock.) 11706 * 11707 * - An iostart side function may call its corresponding iodone side 11708 * function directly (if necessary). 11709 * 11710 * - In the event of an error, an iostart side function can return a buf(9S) 11711 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11712 * b_error in the usual way of course). 11713 * 11714 * - The taskq mechanism may be used by the iodone side functions to dispatch 11715 * requests to the iostart side functions. The iostart side functions in 11716 * this case would be called under the context of a taskq thread, so it's 11717 * OK for them to block/sleep/spin in this case. 11718 * 11719 * - iostart side functions may allocate "shadow" buf(9S) structs and 11720 * pass them along to the next function in the chain. The corresponding 11721 * iodone side functions must coalesce the "shadow" bufs and return 11722 * the "original" buf to the next higher layer. 11723 * 11724 * - The b_private field of the buf(9S) struct holds a pointer to 11725 * an sd_xbuf struct, which contains information needed to 11726 * construct the scsi_pkt for the command. 11727 * 11728 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11729 * layer must acquire & release the SD_MUTEX(un) as needed. 11730 */ 11731 11732 11733 /* 11734 * Create taskq for all targets in the system. This is created at 11735 * _init(9E) and destroyed at _fini(9E). 11736 * 11737 * Note: here we set the minalloc to a reasonably high number to ensure that 11738 * we will have an adequate supply of task entries available at interrupt time. 11739 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11740 * sd_create_taskq(). Since we do not want to sleep for allocations at 11741 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11742 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11743 * requests any one instant in time. 11744 */ 11745 #define SD_TASKQ_NUMTHREADS 8 11746 #define SD_TASKQ_MINALLOC 256 11747 #define SD_TASKQ_MAXALLOC 256 11748 11749 static taskq_t *sd_tq = NULL; 11750 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11751 11752 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11753 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11754 11755 /* 11756 * The following task queue is being created for the write part of 11757 * read-modify-write of non-512 block size devices. 11758 * Limit the number of threads to 1 for now. This number has been choosen 11759 * considering the fact that it applies only to dvd ram drives/MO drives 11760 * currently. Performance for which is not main criteria at this stage. 11761 * Note: It needs to be explored if we can use a single taskq in future 11762 */ 11763 #define SD_WMR_TASKQ_NUMTHREADS 1 11764 static taskq_t *sd_wmr_tq = NULL; 11765 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11766 11767 /* 11768 * Function: sd_taskq_create 11769 * 11770 * Description: Create taskq thread(s) and preallocate task entries 11771 * 11772 * Return Code: Returns a pointer to the allocated taskq_t. 11773 * 11774 * Context: Can sleep. Requires blockable context. 11775 * 11776 * Notes: - The taskq() facility currently is NOT part of the DDI. 11777 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11778 * - taskq_create() will block for memory, also it will panic 11779 * if it cannot create the requested number of threads. 11780 * - Currently taskq_create() creates threads that cannot be 11781 * swapped. 11782 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11783 * supply of taskq entries at interrupt time (ie, so that we 11784 * do not have to sleep for memory) 11785 */ 11786 11787 static void 11788 sd_taskq_create(void) 11789 { 11790 char taskq_name[TASKQ_NAMELEN]; 11791 11792 ASSERT(sd_tq == NULL); 11793 ASSERT(sd_wmr_tq == NULL); 11794 11795 (void) snprintf(taskq_name, sizeof (taskq_name), 11796 "%s_drv_taskq", sd_label); 11797 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11798 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11799 TASKQ_PREPOPULATE)); 11800 11801 (void) snprintf(taskq_name, sizeof (taskq_name), 11802 "%s_rmw_taskq", sd_label); 11803 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11804 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11805 TASKQ_PREPOPULATE)); 11806 } 11807 11808 11809 /* 11810 * Function: sd_taskq_delete 11811 * 11812 * Description: Complementary cleanup routine for sd_taskq_create(). 11813 * 11814 * Context: Kernel thread context. 11815 */ 11816 11817 static void 11818 sd_taskq_delete(void) 11819 { 11820 ASSERT(sd_tq != NULL); 11821 ASSERT(sd_wmr_tq != NULL); 11822 taskq_destroy(sd_tq); 11823 taskq_destroy(sd_wmr_tq); 11824 sd_tq = NULL; 11825 sd_wmr_tq = NULL; 11826 } 11827 11828 11829 /* 11830 * Function: sdstrategy 11831 * 11832 * Description: Driver's strategy (9E) entry point function. 11833 * 11834 * Arguments: bp - pointer to buf(9S) 11835 * 11836 * Return Code: Always returns zero 11837 * 11838 * Context: Kernel thread context. 11839 */ 11840 11841 static int 11842 sdstrategy(struct buf *bp) 11843 { 11844 struct sd_lun *un; 11845 11846 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11847 if (un == NULL) { 11848 bioerror(bp, EIO); 11849 bp->b_resid = bp->b_bcount; 11850 biodone(bp); 11851 return (0); 11852 } 11853 /* As was done in the past, fail new cmds. if state is dumping. */ 11854 if (un->un_state == SD_STATE_DUMPING) { 11855 bioerror(bp, ENXIO); 11856 bp->b_resid = bp->b_bcount; 11857 biodone(bp); 11858 return (0); 11859 } 11860 11861 ASSERT(!mutex_owned(SD_MUTEX(un))); 11862 11863 /* 11864 * Commands may sneak in while we released the mutex in 11865 * DDI_SUSPEND, we should block new commands. However, old 11866 * commands that are still in the driver at this point should 11867 * still be allowed to drain. 11868 */ 11869 mutex_enter(SD_MUTEX(un)); 11870 /* 11871 * Must wait here if either the device is suspended or 11872 * if it's power level is changing. 11873 */ 11874 while ((un->un_state == SD_STATE_SUSPENDED) || 11875 (un->un_state == SD_STATE_PM_CHANGING)) { 11876 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11877 } 11878 11879 un->un_ncmds_in_driver++; 11880 11881 /* 11882 * atapi: Since we are running the CD for now in PIO mode we need to 11883 * call bp_mapin here to avoid bp_mapin called interrupt context under 11884 * the HBA's init_pkt routine. 11885 */ 11886 if (un->un_f_cfg_is_atapi == TRUE) { 11887 mutex_exit(SD_MUTEX(un)); 11888 bp_mapin(bp); 11889 mutex_enter(SD_MUTEX(un)); 11890 } 11891 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11892 un->un_ncmds_in_driver); 11893 11894 mutex_exit(SD_MUTEX(un)); 11895 11896 /* 11897 * This will (eventually) allocate the sd_xbuf area and 11898 * call sd_xbuf_strategy(). We just want to return the 11899 * result of ddi_xbuf_qstrategy so that we have an opt- 11900 * imized tail call which saves us a stack frame. 11901 */ 11902 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11903 } 11904 11905 11906 /* 11907 * Function: sd_xbuf_strategy 11908 * 11909 * Description: Function for initiating IO operations via the 11910 * ddi_xbuf_qstrategy() mechanism. 11911 * 11912 * Context: Kernel thread context. 11913 */ 11914 11915 static void 11916 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11917 { 11918 struct sd_lun *un = arg; 11919 11920 ASSERT(bp != NULL); 11921 ASSERT(xp != NULL); 11922 ASSERT(un != NULL); 11923 ASSERT(!mutex_owned(SD_MUTEX(un))); 11924 11925 /* 11926 * Initialize the fields in the xbuf and save a pointer to the 11927 * xbuf in bp->b_private. 11928 */ 11929 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11930 11931 /* Send the buf down the iostart chain */ 11932 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11933 } 11934 11935 11936 /* 11937 * Function: sd_xbuf_init 11938 * 11939 * Description: Prepare the given sd_xbuf struct for use. 11940 * 11941 * Arguments: un - ptr to softstate 11942 * bp - ptr to associated buf(9S) 11943 * xp - ptr to associated sd_xbuf 11944 * chain_type - IO chain type to use: 11945 * SD_CHAIN_NULL 11946 * SD_CHAIN_BUFIO 11947 * SD_CHAIN_USCSI 11948 * SD_CHAIN_DIRECT 11949 * SD_CHAIN_DIRECT_PRIORITY 11950 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11951 * initialization; may be NULL if none. 11952 * 11953 * Context: Kernel thread context 11954 */ 11955 11956 static void 11957 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11958 uchar_t chain_type, void *pktinfop) 11959 { 11960 int index; 11961 11962 ASSERT(un != NULL); 11963 ASSERT(bp != NULL); 11964 ASSERT(xp != NULL); 11965 11966 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11967 bp, chain_type); 11968 11969 xp->xb_un = un; 11970 xp->xb_pktp = NULL; 11971 xp->xb_pktinfo = pktinfop; 11972 xp->xb_private = bp->b_private; 11973 xp->xb_blkno = (daddr_t)bp->b_blkno; 11974 11975 /* 11976 * Set up the iostart and iodone chain indexes in the xbuf, based 11977 * upon the specified chain type to use. 11978 */ 11979 switch (chain_type) { 11980 case SD_CHAIN_NULL: 11981 /* 11982 * Fall thru to just use the values for the buf type, even 11983 * tho for the NULL chain these values will never be used. 11984 */ 11985 /* FALLTHRU */ 11986 case SD_CHAIN_BUFIO: 11987 index = un->un_buf_chain_type; 11988 break; 11989 case SD_CHAIN_USCSI: 11990 index = un->un_uscsi_chain_type; 11991 break; 11992 case SD_CHAIN_DIRECT: 11993 index = un->un_direct_chain_type; 11994 break; 11995 case SD_CHAIN_DIRECT_PRIORITY: 11996 index = un->un_priority_chain_type; 11997 break; 11998 default: 11999 /* We're really broken if we ever get here... */ 12000 panic("sd_xbuf_init: illegal chain type!"); 12001 /*NOTREACHED*/ 12002 } 12003 12004 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 12005 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 12006 12007 /* 12008 * It might be a bit easier to simply bzero the entire xbuf above, 12009 * but it turns out that since we init a fair number of members anyway, 12010 * we save a fair number cycles by doing explicit assignment of zero. 12011 */ 12012 xp->xb_pkt_flags = 0; 12013 xp->xb_dma_resid = 0; 12014 xp->xb_retry_count = 0; 12015 xp->xb_victim_retry_count = 0; 12016 xp->xb_ua_retry_count = 0; 12017 xp->xb_sense_bp = NULL; 12018 xp->xb_sense_status = 0; 12019 xp->xb_sense_state = 0; 12020 xp->xb_sense_resid = 0; 12021 12022 bp->b_private = xp; 12023 bp->b_flags &= ~(B_DONE | B_ERROR); 12024 bp->b_resid = 0; 12025 bp->av_forw = NULL; 12026 bp->av_back = NULL; 12027 bioerror(bp, 0); 12028 12029 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 12030 } 12031 12032 12033 /* 12034 * Function: sd_uscsi_strategy 12035 * 12036 * Description: Wrapper for calling into the USCSI chain via physio(9F) 12037 * 12038 * Arguments: bp - buf struct ptr 12039 * 12040 * Return Code: Always returns 0 12041 * 12042 * Context: Kernel thread context 12043 */ 12044 12045 static int 12046 sd_uscsi_strategy(struct buf *bp) 12047 { 12048 struct sd_lun *un; 12049 struct sd_uscsi_info *uip; 12050 struct sd_xbuf *xp; 12051 uchar_t chain_type; 12052 12053 ASSERT(bp != NULL); 12054 12055 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 12056 if (un == NULL) { 12057 bioerror(bp, EIO); 12058 bp->b_resid = bp->b_bcount; 12059 biodone(bp); 12060 return (0); 12061 } 12062 12063 ASSERT(!mutex_owned(SD_MUTEX(un))); 12064 12065 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 12066 12067 mutex_enter(SD_MUTEX(un)); 12068 /* 12069 * atapi: Since we are running the CD for now in PIO mode we need to 12070 * call bp_mapin here to avoid bp_mapin called interrupt context under 12071 * the HBA's init_pkt routine. 12072 */ 12073 if (un->un_f_cfg_is_atapi == TRUE) { 12074 mutex_exit(SD_MUTEX(un)); 12075 bp_mapin(bp); 12076 mutex_enter(SD_MUTEX(un)); 12077 } 12078 un->un_ncmds_in_driver++; 12079 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 12080 un->un_ncmds_in_driver); 12081 mutex_exit(SD_MUTEX(un)); 12082 12083 /* 12084 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 12085 */ 12086 ASSERT(bp->b_private != NULL); 12087 uip = (struct sd_uscsi_info *)bp->b_private; 12088 12089 switch (uip->ui_flags) { 12090 case SD_PATH_DIRECT: 12091 chain_type = SD_CHAIN_DIRECT; 12092 break; 12093 case SD_PATH_DIRECT_PRIORITY: 12094 chain_type = SD_CHAIN_DIRECT_PRIORITY; 12095 break; 12096 default: 12097 chain_type = SD_CHAIN_USCSI; 12098 break; 12099 } 12100 12101 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12102 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 12103 12104 /* Use the index obtained within xbuf_init */ 12105 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 12106 12107 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 12108 12109 return (0); 12110 } 12111 12112 12113 /* 12114 * These routines perform raw i/o operations. 12115 */ 12116 /*ARGSUSED*/ 12117 static void 12118 sduscsimin(struct buf *bp) 12119 { 12120 /* 12121 * do not break up because the CDB count would then 12122 * be incorrect and data underruns would result (incomplete 12123 * read/writes which would be retried and then failed, see 12124 * sdintr(). 12125 */ 12126 } 12127 12128 12129 12130 /* 12131 * Function: sd_send_scsi_cmd 12132 * 12133 * Description: Runs a USCSI command for user (when called thru sdioctl), 12134 * or for the driver 12135 * 12136 * Arguments: dev - the dev_t for the device 12137 * incmd - ptr to a valid uscsi_cmd struct 12138 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 12139 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 12140 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 12141 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 12142 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 12143 * to use the USCSI "direct" chain and bypass the normal 12144 * command waitq. 12145 * 12146 * Return Code: 0 - successful completion of the given command 12147 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 12148 * ENXIO - soft state not found for specified dev 12149 * EINVAL 12150 * EFAULT - copyin/copyout error 12151 * return code of biowait(9F) or physio(9F): 12152 * EIO - IO error, caller may check incmd->uscsi_status 12153 * ENXIO 12154 * EACCES - reservation conflict 12155 * 12156 * Context: Waits for command to complete. Can sleep. 12157 */ 12158 12159 static int 12160 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 12161 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 12162 int path_flag) 12163 { 12164 struct sd_uscsi_info *uip; 12165 struct uscsi_cmd *uscmd; 12166 struct sd_lun *un; 12167 struct buf *bp; 12168 int rval; 12169 int flags; 12170 12171 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 12172 if (un == NULL) { 12173 return (ENXIO); 12174 } 12175 12176 ASSERT(!mutex_owned(SD_MUTEX(un))); 12177 12178 #ifdef SDDEBUG 12179 switch (dataspace) { 12180 case UIO_USERSPACE: 12181 SD_TRACE(SD_LOG_IO, un, 12182 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 12183 break; 12184 case UIO_SYSSPACE: 12185 SD_TRACE(SD_LOG_IO, un, 12186 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 12187 break; 12188 default: 12189 SD_TRACE(SD_LOG_IO, un, 12190 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 12191 break; 12192 } 12193 #endif 12194 12195 /* 12196 * Perform resets directly; no need to generate a command to do it. 12197 */ 12198 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 12199 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 12200 RESET_ALL : RESET_TARGET; 12201 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 12202 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 12203 /* Reset attempt was unsuccessful */ 12204 SD_TRACE(SD_LOG_IO, un, 12205 "sd_send_scsi_cmd: reset: failure\n"); 12206 return (EIO); 12207 } 12208 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 12209 return (0); 12210 } 12211 12212 /* Perfunctory sanity check... */ 12213 if (incmd->uscsi_cdblen <= 0) { 12214 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12215 "invalid uscsi_cdblen, returning EINVAL\n"); 12216 return (EINVAL); 12217 } else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) { 12218 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12219 "unsupported uscsi_cdblen, returning EINVAL\n"); 12220 return (EINVAL); 12221 } 12222 12223 /* 12224 * In order to not worry about where the uscsi structure came from 12225 * (or where the cdb it points to came from) we're going to make 12226 * kmem_alloc'd copies of them here. This will also allow reference 12227 * to the data they contain long after this process has gone to 12228 * sleep and its kernel stack has been unmapped, etc. 12229 * 12230 * First get some memory for the uscsi_cmd struct and copy the 12231 * contents of the given uscsi_cmd struct into it. 12232 */ 12233 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12234 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 12235 12236 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 12237 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 12238 12239 /* 12240 * Now get some space for the CDB, and copy the given CDB into 12241 * it. Use ddi_copyin() in case the data is in user space. 12242 */ 12243 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 12244 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 12245 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 12246 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 12247 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 12248 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12249 return (EFAULT); 12250 } 12251 12252 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 12253 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 12254 12255 bp = getrbuf(KM_SLEEP); 12256 12257 /* 12258 * Allocate an sd_uscsi_info struct and fill it with the info 12259 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12260 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12261 * since we allocate the buf here in this function, we do not 12262 * need to preserve the prior contents of b_private. 12263 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12264 */ 12265 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 12266 uip->ui_flags = path_flag; 12267 uip->ui_cmdp = uscmd; 12268 bp->b_private = uip; 12269 12270 /* 12271 * Initialize Request Sense buffering, if requested. 12272 */ 12273 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12274 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12275 /* 12276 * Here uscmd->uscsi_rqbuf currently points to the caller's 12277 * buffer, but we replace this with a kernel buffer that 12278 * we allocate to use with the sense data. The sense data 12279 * (if present) gets copied into this new buffer before the 12280 * command is completed. Then we copy the sense data from 12281 * our allocated buf into the caller's buffer below. Note 12282 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 12283 * below to perform the copy back to the caller's buf. 12284 */ 12285 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 12286 if (rqbufspace == UIO_USERSPACE) { 12287 uscmd->uscsi_rqlen = SENSE_LENGTH; 12288 uscmd->uscsi_rqresid = SENSE_LENGTH; 12289 } else { 12290 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 12291 uscmd->uscsi_rqlen = rlen; 12292 uscmd->uscsi_rqresid = rlen; 12293 } 12294 } else { 12295 uscmd->uscsi_rqbuf = NULL; 12296 uscmd->uscsi_rqlen = 0; 12297 uscmd->uscsi_rqresid = 0; 12298 } 12299 12300 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 12301 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 12302 12303 if (un->un_f_is_fibre == FALSE) { 12304 /* 12305 * Force asynchronous mode, if necessary. Doing this here 12306 * has the unfortunate effect of running other queued 12307 * commands async also, but since the main purpose of this 12308 * capability is downloading new drive firmware, we can 12309 * probably live with it. 12310 */ 12311 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 12312 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12313 == 1) { 12314 if (scsi_ifsetcap(SD_ADDRESS(un), 12315 "synchronous", 0, 1) == 1) { 12316 SD_TRACE(SD_LOG_IO, un, 12317 "sd_send_scsi_cmd: forced async ok\n"); 12318 } else { 12319 SD_TRACE(SD_LOG_IO, un, 12320 "sd_send_scsi_cmd:\ 12321 forced async failed\n"); 12322 rval = EINVAL; 12323 goto done; 12324 } 12325 } 12326 } 12327 12328 /* 12329 * Re-enable synchronous mode, if requested 12330 */ 12331 if (uscmd->uscsi_flags & USCSI_SYNC) { 12332 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12333 == 0) { 12334 int i = scsi_ifsetcap(SD_ADDRESS(un), 12335 "synchronous", 1, 1); 12336 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12337 "re-enabled sync %s\n", 12338 (i == 1) ? "ok" : "failed"); 12339 } 12340 } 12341 } 12342 12343 /* 12344 * Commands sent with priority are intended for error recovery 12345 * situations, and do not have retries performed. 12346 */ 12347 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12348 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12349 } 12350 12351 /* 12352 * If we're going to do actual I/O, let physio do all the right things 12353 */ 12354 if (uscmd->uscsi_buflen != 0) { 12355 struct iovec aiov; 12356 struct uio auio; 12357 struct uio *uio = &auio; 12358 12359 bzero(&auio, sizeof (struct uio)); 12360 bzero(&aiov, sizeof (struct iovec)); 12361 aiov.iov_base = uscmd->uscsi_bufaddr; 12362 aiov.iov_len = uscmd->uscsi_buflen; 12363 uio->uio_iov = &aiov; 12364 12365 uio->uio_iovcnt = 1; 12366 uio->uio_resid = uscmd->uscsi_buflen; 12367 uio->uio_segflg = dataspace; 12368 12369 /* 12370 * physio() will block here until the command completes.... 12371 */ 12372 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 12373 12374 rval = physio(sd_uscsi_strategy, bp, dev, 12375 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 12376 sduscsimin, uio); 12377 12378 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12379 "returned from physio with 0x%x\n", rval); 12380 12381 } else { 12382 /* 12383 * We have to mimic what physio would do here! Argh! 12384 */ 12385 bp->b_flags = B_BUSY | 12386 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 12387 bp->b_edev = dev; 12388 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 12389 bp->b_bcount = 0; 12390 bp->b_blkno = 0; 12391 12392 SD_TRACE(SD_LOG_IO, un, 12393 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 12394 12395 (void) sd_uscsi_strategy(bp); 12396 12397 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 12398 12399 rval = biowait(bp); 12400 12401 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12402 "returned from biowait with 0x%x\n", rval); 12403 } 12404 12405 done: 12406 12407 #ifdef SDDEBUG 12408 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12409 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12410 uscmd->uscsi_status, uscmd->uscsi_resid); 12411 if (uscmd->uscsi_bufaddr != NULL) { 12412 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12413 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12414 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12415 if (dataspace == UIO_SYSSPACE) { 12416 SD_DUMP_MEMORY(un, SD_LOG_IO, 12417 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12418 uscmd->uscsi_buflen, SD_LOG_HEX); 12419 } 12420 } 12421 #endif 12422 12423 /* 12424 * Get the status and residual to return to the caller. 12425 */ 12426 incmd->uscsi_status = uscmd->uscsi_status; 12427 incmd->uscsi_resid = uscmd->uscsi_resid; 12428 12429 /* 12430 * If the caller wants sense data, copy back whatever sense data 12431 * we may have gotten, and update the relevant rqsense info. 12432 */ 12433 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12434 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12435 12436 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 12437 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 12438 12439 /* Update the Request Sense status and resid */ 12440 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 12441 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 12442 12443 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12444 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 12445 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 12446 12447 /* Copy out the sense data for user processes */ 12448 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 12449 int flags = 12450 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 12451 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 12452 rqlen, flags) != 0) { 12453 rval = EFAULT; 12454 } 12455 /* 12456 * Note: Can't touch incmd->uscsi_rqbuf so use 12457 * uscmd->uscsi_rqbuf instead. They're the same. 12458 */ 12459 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12460 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 12461 incmd->uscsi_rqbuf, rqlen); 12462 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 12463 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 12464 } 12465 } 12466 12467 /* 12468 * Free allocated resources and return; mapout the buf in case it was 12469 * mapped in by a lower layer. 12470 */ 12471 bp_mapout(bp); 12472 freerbuf(bp); 12473 kmem_free(uip, sizeof (struct sd_uscsi_info)); 12474 if (uscmd->uscsi_rqbuf != NULL) { 12475 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 12476 } 12477 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 12478 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12479 12480 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 12481 12482 return (rval); 12483 } 12484 12485 12486 /* 12487 * Function: sd_buf_iodone 12488 * 12489 * Description: Frees the sd_xbuf & returns the buf to its originator. 12490 * 12491 * Context: May be called from interrupt context. 12492 */ 12493 /* ARGSUSED */ 12494 static void 12495 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12496 { 12497 struct sd_xbuf *xp; 12498 12499 ASSERT(un != NULL); 12500 ASSERT(bp != NULL); 12501 ASSERT(!mutex_owned(SD_MUTEX(un))); 12502 12503 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12504 12505 xp = SD_GET_XBUF(bp); 12506 ASSERT(xp != NULL); 12507 12508 mutex_enter(SD_MUTEX(un)); 12509 12510 /* 12511 * Grab time when the cmd completed. 12512 * This is used for determining if the system has been 12513 * idle long enough to make it idle to the PM framework. 12514 * This is for lowering the overhead, and therefore improving 12515 * performance per I/O operation. 12516 */ 12517 un->un_pm_idle_time = ddi_get_time(); 12518 12519 un->un_ncmds_in_driver--; 12520 ASSERT(un->un_ncmds_in_driver >= 0); 12521 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12522 un->un_ncmds_in_driver); 12523 12524 mutex_exit(SD_MUTEX(un)); 12525 12526 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12527 biodone(bp); /* bp is gone after this */ 12528 12529 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12530 } 12531 12532 12533 /* 12534 * Function: sd_uscsi_iodone 12535 * 12536 * Description: Frees the sd_xbuf & returns the buf to its originator. 12537 * 12538 * Context: May be called from interrupt context. 12539 */ 12540 /* ARGSUSED */ 12541 static void 12542 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12543 { 12544 struct sd_xbuf *xp; 12545 12546 ASSERT(un != NULL); 12547 ASSERT(bp != NULL); 12548 12549 xp = SD_GET_XBUF(bp); 12550 ASSERT(xp != NULL); 12551 ASSERT(!mutex_owned(SD_MUTEX(un))); 12552 12553 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12554 12555 bp->b_private = xp->xb_private; 12556 12557 mutex_enter(SD_MUTEX(un)); 12558 12559 /* 12560 * Grab time when the cmd completed. 12561 * This is used for determining if the system has been 12562 * idle long enough to make it idle to the PM framework. 12563 * This is for lowering the overhead, and therefore improving 12564 * performance per I/O operation. 12565 */ 12566 un->un_pm_idle_time = ddi_get_time(); 12567 12568 un->un_ncmds_in_driver--; 12569 ASSERT(un->un_ncmds_in_driver >= 0); 12570 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12571 un->un_ncmds_in_driver); 12572 12573 mutex_exit(SD_MUTEX(un)); 12574 12575 kmem_free(xp, sizeof (struct sd_xbuf)); 12576 biodone(bp); 12577 12578 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12579 } 12580 12581 12582 /* 12583 * Function: sd_mapblockaddr_iostart 12584 * 12585 * Description: Verify request lies withing the partition limits for 12586 * the indicated minor device. Issue "overrun" buf if 12587 * request would exceed partition range. Converts 12588 * partition-relative block address to absolute. 12589 * 12590 * Context: Can sleep 12591 * 12592 * Issues: This follows what the old code did, in terms of accessing 12593 * some of the partition info in the unit struct without holding 12594 * the mutext. This is a general issue, if the partition info 12595 * can be altered while IO is in progress... as soon as we send 12596 * a buf, its partitioning can be invalid before it gets to the 12597 * device. Probably the right fix is to move partitioning out 12598 * of the driver entirely. 12599 */ 12600 12601 static void 12602 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12603 { 12604 daddr_t nblocks; /* #blocks in the given partition */ 12605 daddr_t blocknum; /* Block number specified by the buf */ 12606 size_t requested_nblocks; 12607 size_t available_nblocks; 12608 int partition; 12609 diskaddr_t partition_offset; 12610 struct sd_xbuf *xp; 12611 12612 12613 ASSERT(un != NULL); 12614 ASSERT(bp != NULL); 12615 ASSERT(!mutex_owned(SD_MUTEX(un))); 12616 12617 SD_TRACE(SD_LOG_IO_PARTITION, un, 12618 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12619 12620 xp = SD_GET_XBUF(bp); 12621 ASSERT(xp != NULL); 12622 12623 /* 12624 * If the geometry is not indicated as valid, attempt to access 12625 * the unit & verify the geometry/label. This can be the case for 12626 * removable-media devices, of if the device was opened in 12627 * NDELAY/NONBLOCK mode. 12628 */ 12629 if ((un->un_f_geometry_is_valid != TRUE) && 12630 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12631 /* 12632 * For removable devices it is possible to start an I/O 12633 * without a media by opening the device in nodelay mode. 12634 * Also for writable CDs there can be many scenarios where 12635 * there is no geometry yet but volume manager is trying to 12636 * issue a read() just because it can see TOC on the CD. So 12637 * do not print a message for removables. 12638 */ 12639 if (!un->un_f_has_removable_media) { 12640 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12641 "i/o to invalid geometry\n"); 12642 } 12643 bioerror(bp, EIO); 12644 bp->b_resid = bp->b_bcount; 12645 SD_BEGIN_IODONE(index, un, bp); 12646 return; 12647 } 12648 12649 partition = SDPART(bp->b_edev); 12650 12651 /* #blocks in partition */ 12652 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12653 12654 /* Use of a local variable potentially improves performance slightly */ 12655 partition_offset = un->un_offset[partition]; 12656 12657 /* 12658 * blocknum is the starting block number of the request. At this 12659 * point it is still relative to the start of the minor device. 12660 */ 12661 blocknum = xp->xb_blkno; 12662 12663 /* 12664 * Legacy: If the starting block number is one past the last block 12665 * in the partition, do not set B_ERROR in the buf. 12666 */ 12667 if (blocknum == nblocks) { 12668 goto error_exit; 12669 } 12670 12671 /* 12672 * Confirm that the first block of the request lies within the 12673 * partition limits. Also the requested number of bytes must be 12674 * a multiple of the system block size. 12675 */ 12676 if ((blocknum < 0) || (blocknum >= nblocks) || 12677 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12678 bp->b_flags |= B_ERROR; 12679 goto error_exit; 12680 } 12681 12682 /* 12683 * If the requsted # blocks exceeds the available # blocks, that 12684 * is an overrun of the partition. 12685 */ 12686 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12687 available_nblocks = (size_t)(nblocks - blocknum); 12688 ASSERT(nblocks >= blocknum); 12689 12690 if (requested_nblocks > available_nblocks) { 12691 /* 12692 * Allocate an "overrun" buf to allow the request to proceed 12693 * for the amount of space available in the partition. The 12694 * amount not transferred will be added into the b_resid 12695 * when the operation is complete. The overrun buf 12696 * replaces the original buf here, and the original buf 12697 * is saved inside the overrun buf, for later use. 12698 */ 12699 size_t resid = SD_SYSBLOCKS2BYTES(un, 12700 (offset_t)(requested_nblocks - available_nblocks)); 12701 size_t count = bp->b_bcount - resid; 12702 /* 12703 * Note: count is an unsigned entity thus it'll NEVER 12704 * be less than 0 so ASSERT the original values are 12705 * correct. 12706 */ 12707 ASSERT(bp->b_bcount >= resid); 12708 12709 bp = sd_bioclone_alloc(bp, count, blocknum, 12710 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12711 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12712 ASSERT(xp != NULL); 12713 } 12714 12715 /* At this point there should be no residual for this buf. */ 12716 ASSERT(bp->b_resid == 0); 12717 12718 /* Convert the block number to an absolute address. */ 12719 xp->xb_blkno += partition_offset; 12720 12721 SD_NEXT_IOSTART(index, un, bp); 12722 12723 SD_TRACE(SD_LOG_IO_PARTITION, un, 12724 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12725 12726 return; 12727 12728 error_exit: 12729 bp->b_resid = bp->b_bcount; 12730 SD_BEGIN_IODONE(index, un, bp); 12731 SD_TRACE(SD_LOG_IO_PARTITION, un, 12732 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12733 } 12734 12735 12736 /* 12737 * Function: sd_mapblockaddr_iodone 12738 * 12739 * Description: Completion-side processing for partition management. 12740 * 12741 * Context: May be called under interrupt context 12742 */ 12743 12744 static void 12745 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12746 { 12747 /* int partition; */ /* Not used, see below. */ 12748 ASSERT(un != NULL); 12749 ASSERT(bp != NULL); 12750 ASSERT(!mutex_owned(SD_MUTEX(un))); 12751 12752 SD_TRACE(SD_LOG_IO_PARTITION, un, 12753 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12754 12755 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12756 /* 12757 * We have an "overrun" buf to deal with... 12758 */ 12759 struct sd_xbuf *xp; 12760 struct buf *obp; /* ptr to the original buf */ 12761 12762 xp = SD_GET_XBUF(bp); 12763 ASSERT(xp != NULL); 12764 12765 /* Retrieve the pointer to the original buf */ 12766 obp = (struct buf *)xp->xb_private; 12767 ASSERT(obp != NULL); 12768 12769 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12770 bioerror(obp, bp->b_error); 12771 12772 sd_bioclone_free(bp); 12773 12774 /* 12775 * Get back the original buf. 12776 * Note that since the restoration of xb_blkno below 12777 * was removed, the sd_xbuf is not needed. 12778 */ 12779 bp = obp; 12780 /* 12781 * xp = SD_GET_XBUF(bp); 12782 * ASSERT(xp != NULL); 12783 */ 12784 } 12785 12786 /* 12787 * Convert sd->xb_blkno back to a minor-device relative value. 12788 * Note: this has been commented out, as it is not needed in the 12789 * current implementation of the driver (ie, since this function 12790 * is at the top of the layering chains, so the info will be 12791 * discarded) and it is in the "hot" IO path. 12792 * 12793 * partition = getminor(bp->b_edev) & SDPART_MASK; 12794 * xp->xb_blkno -= un->un_offset[partition]; 12795 */ 12796 12797 SD_NEXT_IODONE(index, un, bp); 12798 12799 SD_TRACE(SD_LOG_IO_PARTITION, un, 12800 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12801 } 12802 12803 12804 /* 12805 * Function: sd_mapblocksize_iostart 12806 * 12807 * Description: Convert between system block size (un->un_sys_blocksize) 12808 * and target block size (un->un_tgt_blocksize). 12809 * 12810 * Context: Can sleep to allocate resources. 12811 * 12812 * Assumptions: A higher layer has already performed any partition validation, 12813 * and converted the xp->xb_blkno to an absolute value relative 12814 * to the start of the device. 12815 * 12816 * It is also assumed that the higher layer has implemented 12817 * an "overrun" mechanism for the case where the request would 12818 * read/write beyond the end of a partition. In this case we 12819 * assume (and ASSERT) that bp->b_resid == 0. 12820 * 12821 * Note: The implementation for this routine assumes the target 12822 * block size remains constant between allocation and transport. 12823 */ 12824 12825 static void 12826 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12827 { 12828 struct sd_mapblocksize_info *bsp; 12829 struct sd_xbuf *xp; 12830 offset_t first_byte; 12831 daddr_t start_block, end_block; 12832 daddr_t request_bytes; 12833 ushort_t is_aligned = FALSE; 12834 12835 ASSERT(un != NULL); 12836 ASSERT(bp != NULL); 12837 ASSERT(!mutex_owned(SD_MUTEX(un))); 12838 ASSERT(bp->b_resid == 0); 12839 12840 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12841 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12842 12843 /* 12844 * For a non-writable CD, a write request is an error 12845 */ 12846 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12847 (un->un_f_mmc_writable_media == FALSE)) { 12848 bioerror(bp, EIO); 12849 bp->b_resid = bp->b_bcount; 12850 SD_BEGIN_IODONE(index, un, bp); 12851 return; 12852 } 12853 12854 /* 12855 * We do not need a shadow buf if the device is using 12856 * un->un_sys_blocksize as its block size or if bcount == 0. 12857 * In this case there is no layer-private data block allocated. 12858 */ 12859 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12860 (bp->b_bcount == 0)) { 12861 goto done; 12862 } 12863 12864 #if defined(__i386) || defined(__amd64) 12865 /* We do not support non-block-aligned transfers for ROD devices */ 12866 ASSERT(!ISROD(un)); 12867 #endif 12868 12869 xp = SD_GET_XBUF(bp); 12870 ASSERT(xp != NULL); 12871 12872 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12873 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12874 un->un_tgt_blocksize, un->un_sys_blocksize); 12875 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12876 "request start block:0x%x\n", xp->xb_blkno); 12877 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12878 "request len:0x%x\n", bp->b_bcount); 12879 12880 /* 12881 * Allocate the layer-private data area for the mapblocksize layer. 12882 * Layers are allowed to use the xp_private member of the sd_xbuf 12883 * struct to store the pointer to their layer-private data block, but 12884 * each layer also has the responsibility of restoring the prior 12885 * contents of xb_private before returning the buf/xbuf to the 12886 * higher layer that sent it. 12887 * 12888 * Here we save the prior contents of xp->xb_private into the 12889 * bsp->mbs_oprivate field of our layer-private data area. This value 12890 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12891 * the layer-private area and returning the buf/xbuf to the layer 12892 * that sent it. 12893 * 12894 * Note that here we use kmem_zalloc for the allocation as there are 12895 * parts of the mapblocksize code that expect certain fields to be 12896 * zero unless explicitly set to a required value. 12897 */ 12898 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12899 bsp->mbs_oprivate = xp->xb_private; 12900 xp->xb_private = bsp; 12901 12902 /* 12903 * This treats the data on the disk (target) as an array of bytes. 12904 * first_byte is the byte offset, from the beginning of the device, 12905 * to the location of the request. This is converted from a 12906 * un->un_sys_blocksize block address to a byte offset, and then back 12907 * to a block address based upon a un->un_tgt_blocksize block size. 12908 * 12909 * xp->xb_blkno should be absolute upon entry into this function, 12910 * but, but it is based upon partitions that use the "system" 12911 * block size. It must be adjusted to reflect the block size of 12912 * the target. 12913 * 12914 * Note that end_block is actually the block that follows the last 12915 * block of the request, but that's what is needed for the computation. 12916 */ 12917 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12918 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12919 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12920 un->un_tgt_blocksize; 12921 12922 /* request_bytes is rounded up to a multiple of the target block size */ 12923 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12924 12925 /* 12926 * See if the starting address of the request and the request 12927 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12928 * then we do not need to allocate a shadow buf to handle the request. 12929 */ 12930 if (((first_byte % un->un_tgt_blocksize) == 0) && 12931 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12932 is_aligned = TRUE; 12933 } 12934 12935 if ((bp->b_flags & B_READ) == 0) { 12936 /* 12937 * Lock the range for a write operation. An aligned request is 12938 * considered a simple write; otherwise the request must be a 12939 * read-modify-write. 12940 */ 12941 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12942 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12943 } 12944 12945 /* 12946 * Alloc a shadow buf if the request is not aligned. Also, this is 12947 * where the READ command is generated for a read-modify-write. (The 12948 * write phase is deferred until after the read completes.) 12949 */ 12950 if (is_aligned == FALSE) { 12951 12952 struct sd_mapblocksize_info *shadow_bsp; 12953 struct sd_xbuf *shadow_xp; 12954 struct buf *shadow_bp; 12955 12956 /* 12957 * Allocate the shadow buf and it associated xbuf. Note that 12958 * after this call the xb_blkno value in both the original 12959 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12960 * same: absolute relative to the start of the device, and 12961 * adjusted for the target block size. The b_blkno in the 12962 * shadow buf will also be set to this value. We should never 12963 * change b_blkno in the original bp however. 12964 * 12965 * Note also that the shadow buf will always need to be a 12966 * READ command, regardless of whether the incoming command 12967 * is a READ or a WRITE. 12968 */ 12969 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12970 xp->xb_blkno, 12971 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12972 12973 shadow_xp = SD_GET_XBUF(shadow_bp); 12974 12975 /* 12976 * Allocate the layer-private data for the shadow buf. 12977 * (No need to preserve xb_private in the shadow xbuf.) 12978 */ 12979 shadow_xp->xb_private = shadow_bsp = 12980 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12981 12982 /* 12983 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 12984 * to figure out where the start of the user data is (based upon 12985 * the system block size) in the data returned by the READ 12986 * command (which will be based upon the target blocksize). Note 12987 * that this is only really used if the request is unaligned. 12988 */ 12989 bsp->mbs_copy_offset = (ssize_t)(first_byte - 12990 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 12991 ASSERT((bsp->mbs_copy_offset >= 0) && 12992 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 12993 12994 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 12995 12996 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 12997 12998 /* Transfer the wmap (if any) to the shadow buf */ 12999 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13000 bsp->mbs_wmp = NULL; 13001 13002 /* 13003 * The shadow buf goes on from here in place of the 13004 * original buf. 13005 */ 13006 shadow_bsp->mbs_orig_bp = bp; 13007 bp = shadow_bp; 13008 } 13009 13010 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13011 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13012 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13013 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13014 request_bytes); 13015 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13016 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13017 13018 done: 13019 SD_NEXT_IOSTART(index, un, bp); 13020 13021 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13022 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13023 } 13024 13025 13026 /* 13027 * Function: sd_mapblocksize_iodone 13028 * 13029 * Description: Completion side processing for block-size mapping. 13030 * 13031 * Context: May be called under interrupt context 13032 */ 13033 13034 static void 13035 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13036 { 13037 struct sd_mapblocksize_info *bsp; 13038 struct sd_xbuf *xp; 13039 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13040 struct buf *orig_bp; /* ptr to the original buf */ 13041 offset_t shadow_end; 13042 offset_t request_end; 13043 offset_t shadow_start; 13044 ssize_t copy_offset; 13045 size_t copy_length; 13046 size_t shortfall; 13047 uint_t is_write; /* TRUE if this bp is a WRITE */ 13048 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13049 13050 ASSERT(un != NULL); 13051 ASSERT(bp != NULL); 13052 13053 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13054 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13055 13056 /* 13057 * There is no shadow buf or layer-private data if the target is 13058 * using un->un_sys_blocksize as its block size or if bcount == 0. 13059 */ 13060 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 13061 (bp->b_bcount == 0)) { 13062 goto exit; 13063 } 13064 13065 xp = SD_GET_XBUF(bp); 13066 ASSERT(xp != NULL); 13067 13068 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13069 bsp = xp->xb_private; 13070 13071 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13072 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13073 13074 if (is_write) { 13075 /* 13076 * For a WRITE request we must free up the block range that 13077 * we have locked up. This holds regardless of whether this is 13078 * an aligned write request or a read-modify-write request. 13079 */ 13080 sd_range_unlock(un, bsp->mbs_wmp); 13081 bsp->mbs_wmp = NULL; 13082 } 13083 13084 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13085 /* 13086 * An aligned read or write command will have no shadow buf; 13087 * there is not much else to do with it. 13088 */ 13089 goto done; 13090 } 13091 13092 orig_bp = bsp->mbs_orig_bp; 13093 ASSERT(orig_bp != NULL); 13094 orig_xp = SD_GET_XBUF(orig_bp); 13095 ASSERT(orig_xp != NULL); 13096 ASSERT(!mutex_owned(SD_MUTEX(un))); 13097 13098 if (!is_write && has_wmap) { 13099 /* 13100 * A READ with a wmap means this is the READ phase of a 13101 * read-modify-write. If an error occurred on the READ then 13102 * we do not proceed with the WRITE phase or copy any data. 13103 * Just release the write maps and return with an error. 13104 */ 13105 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13106 orig_bp->b_resid = orig_bp->b_bcount; 13107 bioerror(orig_bp, bp->b_error); 13108 sd_range_unlock(un, bsp->mbs_wmp); 13109 goto freebuf_done; 13110 } 13111 } 13112 13113 /* 13114 * Here is where we set up to copy the data from the shadow buf 13115 * into the space associated with the original buf. 13116 * 13117 * To deal with the conversion between block sizes, these 13118 * computations treat the data as an array of bytes, with the 13119 * first byte (byte 0) corresponding to the first byte in the 13120 * first block on the disk. 13121 */ 13122 13123 /* 13124 * shadow_start and shadow_len indicate the location and size of 13125 * the data returned with the shadow IO request. 13126 */ 13127 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13128 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13129 13130 /* 13131 * copy_offset gives the offset (in bytes) from the start of the first 13132 * block of the READ request to the beginning of the data. We retrieve 13133 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13134 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13135 * data to be copied (in bytes). 13136 */ 13137 copy_offset = bsp->mbs_copy_offset; 13138 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 13139 copy_length = orig_bp->b_bcount; 13140 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13141 13142 /* 13143 * Set up the resid and error fields of orig_bp as appropriate. 13144 */ 13145 if (shadow_end >= request_end) { 13146 /* We got all the requested data; set resid to zero */ 13147 orig_bp->b_resid = 0; 13148 } else { 13149 /* 13150 * We failed to get enough data to fully satisfy the original 13151 * request. Just copy back whatever data we got and set 13152 * up the residual and error code as required. 13153 * 13154 * 'shortfall' is the amount by which the data received with the 13155 * shadow buf has "fallen short" of the requested amount. 13156 */ 13157 shortfall = (size_t)(request_end - shadow_end); 13158 13159 if (shortfall > orig_bp->b_bcount) { 13160 /* 13161 * We did not get enough data to even partially 13162 * fulfill the original request. The residual is 13163 * equal to the amount requested. 13164 */ 13165 orig_bp->b_resid = orig_bp->b_bcount; 13166 } else { 13167 /* 13168 * We did not get all the data that we requested 13169 * from the device, but we will try to return what 13170 * portion we did get. 13171 */ 13172 orig_bp->b_resid = shortfall; 13173 } 13174 ASSERT(copy_length >= orig_bp->b_resid); 13175 copy_length -= orig_bp->b_resid; 13176 } 13177 13178 /* Propagate the error code from the shadow buf to the original buf */ 13179 bioerror(orig_bp, bp->b_error); 13180 13181 if (is_write) { 13182 goto freebuf_done; /* No data copying for a WRITE */ 13183 } 13184 13185 if (has_wmap) { 13186 /* 13187 * This is a READ command from the READ phase of a 13188 * read-modify-write request. We have to copy the data given 13189 * by the user OVER the data returned by the READ command, 13190 * then convert the command from a READ to a WRITE and send 13191 * it back to the target. 13192 */ 13193 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13194 copy_length); 13195 13196 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13197 13198 /* 13199 * Dispatch the WRITE command to the taskq thread, which 13200 * will in turn send the command to the target. When the 13201 * WRITE command completes, we (sd_mapblocksize_iodone()) 13202 * will get called again as part of the iodone chain 13203 * processing for it. Note that we will still be dealing 13204 * with the shadow buf at that point. 13205 */ 13206 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13207 KM_NOSLEEP) != 0) { 13208 /* 13209 * Dispatch was successful so we are done. Return 13210 * without going any higher up the iodone chain. Do 13211 * not free up any layer-private data until after the 13212 * WRITE completes. 13213 */ 13214 return; 13215 } 13216 13217 /* 13218 * Dispatch of the WRITE command failed; set up the error 13219 * condition and send this IO back up the iodone chain. 13220 */ 13221 bioerror(orig_bp, EIO); 13222 orig_bp->b_resid = orig_bp->b_bcount; 13223 13224 } else { 13225 /* 13226 * This is a regular READ request (ie, not a RMW). Copy the 13227 * data from the shadow buf into the original buf. The 13228 * copy_offset compensates for any "misalignment" between the 13229 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13230 * original buf (with its un->un_sys_blocksize blocks). 13231 */ 13232 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13233 copy_length); 13234 } 13235 13236 freebuf_done: 13237 13238 /* 13239 * At this point we still have both the shadow buf AND the original 13240 * buf to deal with, as well as the layer-private data area in each. 13241 * Local variables are as follows: 13242 * 13243 * bp -- points to shadow buf 13244 * xp -- points to xbuf of shadow buf 13245 * bsp -- points to layer-private data area of shadow buf 13246 * orig_bp -- points to original buf 13247 * 13248 * First free the shadow buf and its associated xbuf, then free the 13249 * layer-private data area from the shadow buf. There is no need to 13250 * restore xb_private in the shadow xbuf. 13251 */ 13252 sd_shadow_buf_free(bp); 13253 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13254 13255 /* 13256 * Now update the local variables to point to the original buf, xbuf, 13257 * and layer-private area. 13258 */ 13259 bp = orig_bp; 13260 xp = SD_GET_XBUF(bp); 13261 ASSERT(xp != NULL); 13262 ASSERT(xp == orig_xp); 13263 bsp = xp->xb_private; 13264 ASSERT(bsp != NULL); 13265 13266 done: 13267 /* 13268 * Restore xb_private to whatever it was set to by the next higher 13269 * layer in the chain, then free the layer-private data area. 13270 */ 13271 xp->xb_private = bsp->mbs_oprivate; 13272 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13273 13274 exit: 13275 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13276 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13277 13278 SD_NEXT_IODONE(index, un, bp); 13279 } 13280 13281 13282 /* 13283 * Function: sd_checksum_iostart 13284 * 13285 * Description: A stub function for a layer that's currently not used. 13286 * For now just a placeholder. 13287 * 13288 * Context: Kernel thread context 13289 */ 13290 13291 static void 13292 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13293 { 13294 ASSERT(un != NULL); 13295 ASSERT(bp != NULL); 13296 ASSERT(!mutex_owned(SD_MUTEX(un))); 13297 SD_NEXT_IOSTART(index, un, bp); 13298 } 13299 13300 13301 /* 13302 * Function: sd_checksum_iodone 13303 * 13304 * Description: A stub function for a layer that's currently not used. 13305 * For now just a placeholder. 13306 * 13307 * Context: May be called under interrupt context 13308 */ 13309 13310 static void 13311 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13312 { 13313 ASSERT(un != NULL); 13314 ASSERT(bp != NULL); 13315 ASSERT(!mutex_owned(SD_MUTEX(un))); 13316 SD_NEXT_IODONE(index, un, bp); 13317 } 13318 13319 13320 /* 13321 * Function: sd_checksum_uscsi_iostart 13322 * 13323 * Description: A stub function for a layer that's currently not used. 13324 * For now just a placeholder. 13325 * 13326 * Context: Kernel thread context 13327 */ 13328 13329 static void 13330 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13331 { 13332 ASSERT(un != NULL); 13333 ASSERT(bp != NULL); 13334 ASSERT(!mutex_owned(SD_MUTEX(un))); 13335 SD_NEXT_IOSTART(index, un, bp); 13336 } 13337 13338 13339 /* 13340 * Function: sd_checksum_uscsi_iodone 13341 * 13342 * Description: A stub function for a layer that's currently not used. 13343 * For now just a placeholder. 13344 * 13345 * Context: May be called under interrupt context 13346 */ 13347 13348 static void 13349 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13350 { 13351 ASSERT(un != NULL); 13352 ASSERT(bp != NULL); 13353 ASSERT(!mutex_owned(SD_MUTEX(un))); 13354 SD_NEXT_IODONE(index, un, bp); 13355 } 13356 13357 13358 /* 13359 * Function: sd_pm_iostart 13360 * 13361 * Description: iostart-side routine for Power mangement. 13362 * 13363 * Context: Kernel thread context 13364 */ 13365 13366 static void 13367 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13368 { 13369 ASSERT(un != NULL); 13370 ASSERT(bp != NULL); 13371 ASSERT(!mutex_owned(SD_MUTEX(un))); 13372 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13373 13374 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13375 13376 if (sd_pm_entry(un) != DDI_SUCCESS) { 13377 /* 13378 * Set up to return the failed buf back up the 'iodone' 13379 * side of the calling chain. 13380 */ 13381 bioerror(bp, EIO); 13382 bp->b_resid = bp->b_bcount; 13383 13384 SD_BEGIN_IODONE(index, un, bp); 13385 13386 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13387 return; 13388 } 13389 13390 SD_NEXT_IOSTART(index, un, bp); 13391 13392 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13393 } 13394 13395 13396 /* 13397 * Function: sd_pm_iodone 13398 * 13399 * Description: iodone-side routine for power mangement. 13400 * 13401 * Context: may be called from interrupt context 13402 */ 13403 13404 static void 13405 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13406 { 13407 ASSERT(un != NULL); 13408 ASSERT(bp != NULL); 13409 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13410 13411 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13412 13413 /* 13414 * After attach the following flag is only read, so don't 13415 * take the penalty of acquiring a mutex for it. 13416 */ 13417 if (un->un_f_pm_is_enabled == TRUE) { 13418 sd_pm_exit(un); 13419 } 13420 13421 SD_NEXT_IODONE(index, un, bp); 13422 13423 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13424 } 13425 13426 13427 /* 13428 * Function: sd_core_iostart 13429 * 13430 * Description: Primary driver function for enqueuing buf(9S) structs from 13431 * the system and initiating IO to the target device 13432 * 13433 * Context: Kernel thread context. Can sleep. 13434 * 13435 * Assumptions: - The given xp->xb_blkno is absolute 13436 * (ie, relative to the start of the device). 13437 * - The IO is to be done using the native blocksize of 13438 * the device, as specified in un->un_tgt_blocksize. 13439 */ 13440 /* ARGSUSED */ 13441 static void 13442 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13443 { 13444 struct sd_xbuf *xp; 13445 13446 ASSERT(un != NULL); 13447 ASSERT(bp != NULL); 13448 ASSERT(!mutex_owned(SD_MUTEX(un))); 13449 ASSERT(bp->b_resid == 0); 13450 13451 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13452 13453 xp = SD_GET_XBUF(bp); 13454 ASSERT(xp != NULL); 13455 13456 mutex_enter(SD_MUTEX(un)); 13457 13458 /* 13459 * If we are currently in the failfast state, fail any new IO 13460 * that has B_FAILFAST set, then return. 13461 */ 13462 if ((bp->b_flags & B_FAILFAST) && 13463 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13464 mutex_exit(SD_MUTEX(un)); 13465 bioerror(bp, EIO); 13466 bp->b_resid = bp->b_bcount; 13467 SD_BEGIN_IODONE(index, un, bp); 13468 return; 13469 } 13470 13471 if (SD_IS_DIRECT_PRIORITY(xp)) { 13472 /* 13473 * Priority command -- transport it immediately. 13474 * 13475 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13476 * because all direct priority commands should be associated 13477 * with error recovery actions which we don't want to retry. 13478 */ 13479 sd_start_cmds(un, bp); 13480 } else { 13481 /* 13482 * Normal command -- add it to the wait queue, then start 13483 * transporting commands from the wait queue. 13484 */ 13485 sd_add_buf_to_waitq(un, bp); 13486 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13487 sd_start_cmds(un, NULL); 13488 } 13489 13490 mutex_exit(SD_MUTEX(un)); 13491 13492 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13493 } 13494 13495 13496 /* 13497 * Function: sd_init_cdb_limits 13498 * 13499 * Description: This is to handle scsi_pkt initialization differences 13500 * between the driver platforms. 13501 * 13502 * Legacy behaviors: 13503 * 13504 * If the block number or the sector count exceeds the 13505 * capabilities of a Group 0 command, shift over to a 13506 * Group 1 command. We don't blindly use Group 1 13507 * commands because a) some drives (CDC Wren IVs) get a 13508 * bit confused, and b) there is probably a fair amount 13509 * of speed difference for a target to receive and decode 13510 * a 10 byte command instead of a 6 byte command. 13511 * 13512 * The xfer time difference of 6 vs 10 byte CDBs is 13513 * still significant so this code is still worthwhile. 13514 * 10 byte CDBs are very inefficient with the fas HBA driver 13515 * and older disks. Each CDB byte took 1 usec with some 13516 * popular disks. 13517 * 13518 * Context: Must be called at attach time 13519 */ 13520 13521 static void 13522 sd_init_cdb_limits(struct sd_lun *un) 13523 { 13524 int hba_cdb_limit; 13525 13526 /* 13527 * Use CDB_GROUP1 commands for most devices except for 13528 * parallel SCSI fixed drives in which case we get better 13529 * performance using CDB_GROUP0 commands (where applicable). 13530 */ 13531 un->un_mincdb = SD_CDB_GROUP1; 13532 #if !defined(__fibre) 13533 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13534 !un->un_f_has_removable_media) { 13535 un->un_mincdb = SD_CDB_GROUP0; 13536 } 13537 #endif 13538 13539 /* 13540 * Try to read the max-cdb-length supported by HBA. 13541 */ 13542 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13543 if (0 >= un->un_max_hba_cdb) { 13544 un->un_max_hba_cdb = CDB_GROUP4; 13545 hba_cdb_limit = SD_CDB_GROUP4; 13546 } else if (0 < un->un_max_hba_cdb && 13547 un->un_max_hba_cdb < CDB_GROUP1) { 13548 hba_cdb_limit = SD_CDB_GROUP0; 13549 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13550 un->un_max_hba_cdb < CDB_GROUP5) { 13551 hba_cdb_limit = SD_CDB_GROUP1; 13552 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13553 un->un_max_hba_cdb < CDB_GROUP4) { 13554 hba_cdb_limit = SD_CDB_GROUP5; 13555 } else { 13556 hba_cdb_limit = SD_CDB_GROUP4; 13557 } 13558 13559 /* 13560 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13561 * commands for fixed disks unless we are building for a 32 bit 13562 * kernel. 13563 */ 13564 #ifdef _LP64 13565 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13566 min(hba_cdb_limit, SD_CDB_GROUP4); 13567 #else 13568 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13569 min(hba_cdb_limit, SD_CDB_GROUP1); 13570 #endif 13571 13572 /* 13573 * x86 systems require the PKT_DMA_PARTIAL flag 13574 */ 13575 #if defined(__x86) 13576 un->un_pkt_flags = PKT_DMA_PARTIAL; 13577 #else 13578 un->un_pkt_flags = 0; 13579 #endif 13580 13581 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13582 ? sizeof (struct scsi_arq_status) : 1); 13583 un->un_cmd_timeout = (ushort_t)sd_io_time; 13584 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13585 } 13586 13587 13588 /* 13589 * Function: sd_initpkt_for_buf 13590 * 13591 * Description: Allocate and initialize for transport a scsi_pkt struct, 13592 * based upon the info specified in the given buf struct. 13593 * 13594 * Assumes the xb_blkno in the request is absolute (ie, 13595 * relative to the start of the device (NOT partition!). 13596 * Also assumes that the request is using the native block 13597 * size of the device (as returned by the READ CAPACITY 13598 * command). 13599 * 13600 * Return Code: SD_PKT_ALLOC_SUCCESS 13601 * SD_PKT_ALLOC_FAILURE 13602 * SD_PKT_ALLOC_FAILURE_NO_DMA 13603 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13604 * 13605 * Context: Kernel thread and may be called from software interrupt context 13606 * as part of a sdrunout callback. This function may not block or 13607 * call routines that block 13608 */ 13609 13610 static int 13611 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13612 { 13613 struct sd_xbuf *xp; 13614 struct scsi_pkt *pktp = NULL; 13615 struct sd_lun *un; 13616 size_t blockcount; 13617 daddr_t startblock; 13618 int rval; 13619 int cmd_flags; 13620 13621 ASSERT(bp != NULL); 13622 ASSERT(pktpp != NULL); 13623 xp = SD_GET_XBUF(bp); 13624 ASSERT(xp != NULL); 13625 un = SD_GET_UN(bp); 13626 ASSERT(un != NULL); 13627 ASSERT(mutex_owned(SD_MUTEX(un))); 13628 ASSERT(bp->b_resid == 0); 13629 13630 SD_TRACE(SD_LOG_IO_CORE, un, 13631 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13632 13633 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13634 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13635 /* 13636 * Already have a scsi_pkt -- just need DMA resources. 13637 * We must recompute the CDB in case the mapping returns 13638 * a nonzero pkt_resid. 13639 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13640 * that is being retried, the unmap/remap of the DMA resouces 13641 * will result in the entire transfer starting over again 13642 * from the very first block. 13643 */ 13644 ASSERT(xp->xb_pktp != NULL); 13645 pktp = xp->xb_pktp; 13646 } else { 13647 pktp = NULL; 13648 } 13649 #endif /* __i386 || __amd64 */ 13650 13651 startblock = xp->xb_blkno; /* Absolute block num. */ 13652 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13653 13654 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13655 13656 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13657 13658 #else 13659 13660 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13661 13662 #endif 13663 13664 /* 13665 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13666 * call scsi_init_pkt, and build the CDB. 13667 */ 13668 rval = sd_setup_rw_pkt(un, &pktp, bp, 13669 cmd_flags, sdrunout, (caddr_t)un, 13670 startblock, blockcount); 13671 13672 if (rval == 0) { 13673 /* 13674 * Success. 13675 * 13676 * If partial DMA is being used and required for this transfer. 13677 * set it up here. 13678 */ 13679 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13680 (pktp->pkt_resid != 0)) { 13681 13682 /* 13683 * Save the CDB length and pkt_resid for the 13684 * next xfer 13685 */ 13686 xp->xb_dma_resid = pktp->pkt_resid; 13687 13688 /* rezero resid */ 13689 pktp->pkt_resid = 0; 13690 13691 } else { 13692 xp->xb_dma_resid = 0; 13693 } 13694 13695 pktp->pkt_flags = un->un_tagflags; 13696 pktp->pkt_time = un->un_cmd_timeout; 13697 pktp->pkt_comp = sdintr; 13698 13699 pktp->pkt_private = bp; 13700 *pktpp = pktp; 13701 13702 SD_TRACE(SD_LOG_IO_CORE, un, 13703 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13704 13705 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13706 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13707 #endif 13708 13709 return (SD_PKT_ALLOC_SUCCESS); 13710 13711 } 13712 13713 /* 13714 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13715 * from sd_setup_rw_pkt. 13716 */ 13717 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13718 13719 if (rval == SD_PKT_ALLOC_FAILURE) { 13720 *pktpp = NULL; 13721 /* 13722 * Set the driver state to RWAIT to indicate the driver 13723 * is waiting on resource allocations. The driver will not 13724 * suspend, pm_suspend, or detatch while the state is RWAIT. 13725 */ 13726 New_state(un, SD_STATE_RWAIT); 13727 13728 SD_ERROR(SD_LOG_IO_CORE, un, 13729 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13730 13731 if ((bp->b_flags & B_ERROR) != 0) { 13732 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13733 } 13734 return (SD_PKT_ALLOC_FAILURE); 13735 } else { 13736 /* 13737 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13738 * 13739 * This should never happen. Maybe someone messed with the 13740 * kernel's minphys? 13741 */ 13742 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13743 "Request rejected: too large for CDB: " 13744 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13745 SD_ERROR(SD_LOG_IO_CORE, un, 13746 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13747 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13748 13749 } 13750 } 13751 13752 13753 /* 13754 * Function: sd_destroypkt_for_buf 13755 * 13756 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13757 * 13758 * Context: Kernel thread or interrupt context 13759 */ 13760 13761 static void 13762 sd_destroypkt_for_buf(struct buf *bp) 13763 { 13764 ASSERT(bp != NULL); 13765 ASSERT(SD_GET_UN(bp) != NULL); 13766 13767 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13768 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13769 13770 ASSERT(SD_GET_PKTP(bp) != NULL); 13771 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13772 13773 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13774 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13775 } 13776 13777 /* 13778 * Function: sd_setup_rw_pkt 13779 * 13780 * Description: Determines appropriate CDB group for the requested LBA 13781 * and transfer length, calls scsi_init_pkt, and builds 13782 * the CDB. Do not use for partial DMA transfers except 13783 * for the initial transfer since the CDB size must 13784 * remain constant. 13785 * 13786 * Context: Kernel thread and may be called from software interrupt 13787 * context as part of a sdrunout callback. This function may not 13788 * block or call routines that block 13789 */ 13790 13791 13792 int 13793 sd_setup_rw_pkt(struct sd_lun *un, 13794 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13795 int (*callback)(caddr_t), caddr_t callback_arg, 13796 diskaddr_t lba, uint32_t blockcount) 13797 { 13798 struct scsi_pkt *return_pktp; 13799 union scsi_cdb *cdbp; 13800 struct sd_cdbinfo *cp = NULL; 13801 int i; 13802 13803 /* 13804 * See which size CDB to use, based upon the request. 13805 */ 13806 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13807 13808 /* 13809 * Check lba and block count against sd_cdbtab limits. 13810 * In the partial DMA case, we have to use the same size 13811 * CDB for all the transfers. Check lba + blockcount 13812 * against the max LBA so we know that segment of the 13813 * transfer can use the CDB we select. 13814 */ 13815 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13816 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13817 13818 /* 13819 * The command will fit into the CDB type 13820 * specified by sd_cdbtab[i]. 13821 */ 13822 cp = sd_cdbtab + i; 13823 13824 /* 13825 * Call scsi_init_pkt so we can fill in the 13826 * CDB. 13827 */ 13828 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13829 bp, cp->sc_grpcode, un->un_status_len, 0, 13830 flags, callback, callback_arg); 13831 13832 if (return_pktp != NULL) { 13833 13834 /* 13835 * Return new value of pkt 13836 */ 13837 *pktpp = return_pktp; 13838 13839 /* 13840 * To be safe, zero the CDB insuring there is 13841 * no leftover data from a previous command. 13842 */ 13843 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13844 13845 /* 13846 * Handle partial DMA mapping 13847 */ 13848 if (return_pktp->pkt_resid != 0) { 13849 13850 /* 13851 * Not going to xfer as many blocks as 13852 * originally expected 13853 */ 13854 blockcount -= 13855 SD_BYTES2TGTBLOCKS(un, 13856 return_pktp->pkt_resid); 13857 } 13858 13859 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13860 13861 /* 13862 * Set command byte based on the CDB 13863 * type we matched. 13864 */ 13865 cdbp->scc_cmd = cp->sc_grpmask | 13866 ((bp->b_flags & B_READ) ? 13867 SCMD_READ : SCMD_WRITE); 13868 13869 SD_FILL_SCSI1_LUN(un, return_pktp); 13870 13871 /* 13872 * Fill in LBA and length 13873 */ 13874 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13875 (cp->sc_grpcode == CDB_GROUP4) || 13876 (cp->sc_grpcode == CDB_GROUP0) || 13877 (cp->sc_grpcode == CDB_GROUP5)); 13878 13879 if (cp->sc_grpcode == CDB_GROUP1) { 13880 FORMG1ADDR(cdbp, lba); 13881 FORMG1COUNT(cdbp, blockcount); 13882 return (0); 13883 } else if (cp->sc_grpcode == CDB_GROUP4) { 13884 FORMG4LONGADDR(cdbp, lba); 13885 FORMG4COUNT(cdbp, blockcount); 13886 return (0); 13887 } else if (cp->sc_grpcode == CDB_GROUP0) { 13888 FORMG0ADDR(cdbp, lba); 13889 FORMG0COUNT(cdbp, blockcount); 13890 return (0); 13891 } else if (cp->sc_grpcode == CDB_GROUP5) { 13892 FORMG5ADDR(cdbp, lba); 13893 FORMG5COUNT(cdbp, blockcount); 13894 return (0); 13895 } 13896 13897 /* 13898 * It should be impossible to not match one 13899 * of the CDB types above, so we should never 13900 * reach this point. Set the CDB command byte 13901 * to test-unit-ready to avoid writing 13902 * to somewhere we don't intend. 13903 */ 13904 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13905 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13906 } else { 13907 /* 13908 * Couldn't get scsi_pkt 13909 */ 13910 return (SD_PKT_ALLOC_FAILURE); 13911 } 13912 } 13913 } 13914 13915 /* 13916 * None of the available CDB types were suitable. This really 13917 * should never happen: on a 64 bit system we support 13918 * READ16/WRITE16 which will hold an entire 64 bit disk address 13919 * and on a 32 bit system we will refuse to bind to a device 13920 * larger than 2TB so addresses will never be larger than 32 bits. 13921 */ 13922 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13923 } 13924 13925 #if defined(__i386) || defined(__amd64) 13926 /* 13927 * Function: sd_setup_next_rw_pkt 13928 * 13929 * Description: Setup packet for partial DMA transfers, except for the 13930 * initial transfer. sd_setup_rw_pkt should be used for 13931 * the initial transfer. 13932 * 13933 * Context: Kernel thread and may be called from interrupt context. 13934 */ 13935 13936 int 13937 sd_setup_next_rw_pkt(struct sd_lun *un, 13938 struct scsi_pkt *pktp, struct buf *bp, 13939 diskaddr_t lba, uint32_t blockcount) 13940 { 13941 uchar_t com; 13942 union scsi_cdb *cdbp; 13943 uchar_t cdb_group_id; 13944 13945 ASSERT(pktp != NULL); 13946 ASSERT(pktp->pkt_cdbp != NULL); 13947 13948 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13949 com = cdbp->scc_cmd; 13950 cdb_group_id = CDB_GROUPID(com); 13951 13952 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13953 (cdb_group_id == CDB_GROUPID_1) || 13954 (cdb_group_id == CDB_GROUPID_4) || 13955 (cdb_group_id == CDB_GROUPID_5)); 13956 13957 /* 13958 * Move pkt to the next portion of the xfer. 13959 * func is NULL_FUNC so we do not have to release 13960 * the disk mutex here. 13961 */ 13962 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13963 NULL_FUNC, NULL) == pktp) { 13964 /* Success. Handle partial DMA */ 13965 if (pktp->pkt_resid != 0) { 13966 blockcount -= 13967 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13968 } 13969 13970 cdbp->scc_cmd = com; 13971 SD_FILL_SCSI1_LUN(un, pktp); 13972 if (cdb_group_id == CDB_GROUPID_1) { 13973 FORMG1ADDR(cdbp, lba); 13974 FORMG1COUNT(cdbp, blockcount); 13975 return (0); 13976 } else if (cdb_group_id == CDB_GROUPID_4) { 13977 FORMG4LONGADDR(cdbp, lba); 13978 FORMG4COUNT(cdbp, blockcount); 13979 return (0); 13980 } else if (cdb_group_id == CDB_GROUPID_0) { 13981 FORMG0ADDR(cdbp, lba); 13982 FORMG0COUNT(cdbp, blockcount); 13983 return (0); 13984 } else if (cdb_group_id == CDB_GROUPID_5) { 13985 FORMG5ADDR(cdbp, lba); 13986 FORMG5COUNT(cdbp, blockcount); 13987 return (0); 13988 } 13989 13990 /* Unreachable */ 13991 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13992 } 13993 13994 /* 13995 * Error setting up next portion of cmd transfer. 13996 * Something is definitely very wrong and this 13997 * should not happen. 13998 */ 13999 return (SD_PKT_ALLOC_FAILURE); 14000 } 14001 #endif /* defined(__i386) || defined(__amd64) */ 14002 14003 /* 14004 * Function: sd_initpkt_for_uscsi 14005 * 14006 * Description: Allocate and initialize for transport a scsi_pkt struct, 14007 * based upon the info specified in the given uscsi_cmd struct. 14008 * 14009 * Return Code: SD_PKT_ALLOC_SUCCESS 14010 * SD_PKT_ALLOC_FAILURE 14011 * SD_PKT_ALLOC_FAILURE_NO_DMA 14012 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14013 * 14014 * Context: Kernel thread and may be called from software interrupt context 14015 * as part of a sdrunout callback. This function may not block or 14016 * call routines that block 14017 */ 14018 14019 static int 14020 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14021 { 14022 struct uscsi_cmd *uscmd; 14023 struct sd_xbuf *xp; 14024 struct scsi_pkt *pktp; 14025 struct sd_lun *un; 14026 uint32_t flags = 0; 14027 14028 ASSERT(bp != NULL); 14029 ASSERT(pktpp != NULL); 14030 xp = SD_GET_XBUF(bp); 14031 ASSERT(xp != NULL); 14032 un = SD_GET_UN(bp); 14033 ASSERT(un != NULL); 14034 ASSERT(mutex_owned(SD_MUTEX(un))); 14035 14036 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14037 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14038 ASSERT(uscmd != NULL); 14039 14040 SD_TRACE(SD_LOG_IO_CORE, un, 14041 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14042 14043 /* 14044 * Allocate the scsi_pkt for the command. 14045 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14046 * during scsi_init_pkt time and will continue to use the 14047 * same path as long as the same scsi_pkt is used without 14048 * intervening scsi_dma_free(). Since uscsi command does 14049 * not call scsi_dmafree() before retry failed command, it 14050 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14051 * set such that scsi_vhci can use other available path for 14052 * retry. Besides, ucsci command does not allow DMA breakup, 14053 * so there is no need to set PKT_DMA_PARTIAL flag. 14054 */ 14055 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14056 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14057 sizeof (struct scsi_arq_status), 0, 14058 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14059 sdrunout, (caddr_t)un); 14060 14061 if (pktp == NULL) { 14062 *pktpp = NULL; 14063 /* 14064 * Set the driver state to RWAIT to indicate the driver 14065 * is waiting on resource allocations. The driver will not 14066 * suspend, pm_suspend, or detatch while the state is RWAIT. 14067 */ 14068 New_state(un, SD_STATE_RWAIT); 14069 14070 SD_ERROR(SD_LOG_IO_CORE, un, 14071 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14072 14073 if ((bp->b_flags & B_ERROR) != 0) { 14074 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14075 } 14076 return (SD_PKT_ALLOC_FAILURE); 14077 } 14078 14079 /* 14080 * We do not do DMA breakup for USCSI commands, so return failure 14081 * here if all the needed DMA resources were not allocated. 14082 */ 14083 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14084 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14085 scsi_destroy_pkt(pktp); 14086 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14087 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14088 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14089 } 14090 14091 /* Init the cdb from the given uscsi struct */ 14092 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14093 uscmd->uscsi_cdb[0], 0, 0, 0); 14094 14095 SD_FILL_SCSI1_LUN(un, pktp); 14096 14097 /* 14098 * Set up the optional USCSI flags. See the uscsi (7I) man page 14099 * for listing of the supported flags. 14100 */ 14101 14102 if (uscmd->uscsi_flags & USCSI_SILENT) { 14103 flags |= FLAG_SILENT; 14104 } 14105 14106 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14107 flags |= FLAG_DIAGNOSE; 14108 } 14109 14110 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14111 flags |= FLAG_ISOLATE; 14112 } 14113 14114 if (un->un_f_is_fibre == FALSE) { 14115 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14116 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14117 } 14118 } 14119 14120 /* 14121 * Set the pkt flags here so we save time later. 14122 * Note: These flags are NOT in the uscsi man page!!! 14123 */ 14124 if (uscmd->uscsi_flags & USCSI_HEAD) { 14125 flags |= FLAG_HEAD; 14126 } 14127 14128 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14129 flags |= FLAG_NOINTR; 14130 } 14131 14132 /* 14133 * For tagged queueing, things get a bit complicated. 14134 * Check first for head of queue and last for ordered queue. 14135 * If neither head nor order, use the default driver tag flags. 14136 */ 14137 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14138 if (uscmd->uscsi_flags & USCSI_HTAG) { 14139 flags |= FLAG_HTAG; 14140 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14141 flags |= FLAG_OTAG; 14142 } else { 14143 flags |= un->un_tagflags & FLAG_TAGMASK; 14144 } 14145 } 14146 14147 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14148 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14149 } 14150 14151 pktp->pkt_flags = flags; 14152 14153 /* Copy the caller's CDB into the pkt... */ 14154 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14155 14156 if (uscmd->uscsi_timeout == 0) { 14157 pktp->pkt_time = un->un_uscsi_timeout; 14158 } else { 14159 pktp->pkt_time = uscmd->uscsi_timeout; 14160 } 14161 14162 /* need it later to identify USCSI request in sdintr */ 14163 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14164 14165 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14166 14167 pktp->pkt_private = bp; 14168 pktp->pkt_comp = sdintr; 14169 *pktpp = pktp; 14170 14171 SD_TRACE(SD_LOG_IO_CORE, un, 14172 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14173 14174 return (SD_PKT_ALLOC_SUCCESS); 14175 } 14176 14177 14178 /* 14179 * Function: sd_destroypkt_for_uscsi 14180 * 14181 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14182 * IOs.. Also saves relevant info into the associated uscsi_cmd 14183 * struct. 14184 * 14185 * Context: May be called under interrupt context 14186 */ 14187 14188 static void 14189 sd_destroypkt_for_uscsi(struct buf *bp) 14190 { 14191 struct uscsi_cmd *uscmd; 14192 struct sd_xbuf *xp; 14193 struct scsi_pkt *pktp; 14194 struct sd_lun *un; 14195 14196 ASSERT(bp != NULL); 14197 xp = SD_GET_XBUF(bp); 14198 ASSERT(xp != NULL); 14199 un = SD_GET_UN(bp); 14200 ASSERT(un != NULL); 14201 ASSERT(!mutex_owned(SD_MUTEX(un))); 14202 pktp = SD_GET_PKTP(bp); 14203 ASSERT(pktp != NULL); 14204 14205 SD_TRACE(SD_LOG_IO_CORE, un, 14206 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14207 14208 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14209 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14210 ASSERT(uscmd != NULL); 14211 14212 /* Save the status and the residual into the uscsi_cmd struct */ 14213 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14214 uscmd->uscsi_resid = bp->b_resid; 14215 14216 /* 14217 * If enabled, copy any saved sense data into the area specified 14218 * by the uscsi command. 14219 */ 14220 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14221 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14222 /* 14223 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14224 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14225 */ 14226 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14227 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14228 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 14229 } 14230 14231 /* We are done with the scsi_pkt; free it now */ 14232 ASSERT(SD_GET_PKTP(bp) != NULL); 14233 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14234 14235 SD_TRACE(SD_LOG_IO_CORE, un, 14236 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14237 } 14238 14239 14240 /* 14241 * Function: sd_bioclone_alloc 14242 * 14243 * Description: Allocate a buf(9S) and init it as per the given buf 14244 * and the various arguments. The associated sd_xbuf 14245 * struct is (nearly) duplicated. The struct buf *bp 14246 * argument is saved in new_xp->xb_private. 14247 * 14248 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14249 * datalen - size of data area for the shadow bp 14250 * blkno - starting LBA 14251 * func - function pointer for b_iodone in the shadow buf. (May 14252 * be NULL if none.) 14253 * 14254 * Return Code: Pointer to allocates buf(9S) struct 14255 * 14256 * Context: Can sleep. 14257 */ 14258 14259 static struct buf * 14260 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14261 daddr_t blkno, int (*func)(struct buf *)) 14262 { 14263 struct sd_lun *un; 14264 struct sd_xbuf *xp; 14265 struct sd_xbuf *new_xp; 14266 struct buf *new_bp; 14267 14268 ASSERT(bp != NULL); 14269 xp = SD_GET_XBUF(bp); 14270 ASSERT(xp != NULL); 14271 un = SD_GET_UN(bp); 14272 ASSERT(un != NULL); 14273 ASSERT(!mutex_owned(SD_MUTEX(un))); 14274 14275 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14276 NULL, KM_SLEEP); 14277 14278 new_bp->b_lblkno = blkno; 14279 14280 /* 14281 * Allocate an xbuf for the shadow bp and copy the contents of the 14282 * original xbuf into it. 14283 */ 14284 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14285 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14286 14287 /* 14288 * The given bp is automatically saved in the xb_private member 14289 * of the new xbuf. Callers are allowed to depend on this. 14290 */ 14291 new_xp->xb_private = bp; 14292 14293 new_bp->b_private = new_xp; 14294 14295 return (new_bp); 14296 } 14297 14298 /* 14299 * Function: sd_shadow_buf_alloc 14300 * 14301 * Description: Allocate a buf(9S) and init it as per the given buf 14302 * and the various arguments. The associated sd_xbuf 14303 * struct is (nearly) duplicated. The struct buf *bp 14304 * argument is saved in new_xp->xb_private. 14305 * 14306 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14307 * datalen - size of data area for the shadow bp 14308 * bflags - B_READ or B_WRITE (pseudo flag) 14309 * blkno - starting LBA 14310 * func - function pointer for b_iodone in the shadow buf. (May 14311 * be NULL if none.) 14312 * 14313 * Return Code: Pointer to allocates buf(9S) struct 14314 * 14315 * Context: Can sleep. 14316 */ 14317 14318 static struct buf * 14319 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14320 daddr_t blkno, int (*func)(struct buf *)) 14321 { 14322 struct sd_lun *un; 14323 struct sd_xbuf *xp; 14324 struct sd_xbuf *new_xp; 14325 struct buf *new_bp; 14326 14327 ASSERT(bp != NULL); 14328 xp = SD_GET_XBUF(bp); 14329 ASSERT(xp != NULL); 14330 un = SD_GET_UN(bp); 14331 ASSERT(un != NULL); 14332 ASSERT(!mutex_owned(SD_MUTEX(un))); 14333 14334 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14335 bp_mapin(bp); 14336 } 14337 14338 bflags &= (B_READ | B_WRITE); 14339 #if defined(__i386) || defined(__amd64) 14340 new_bp = getrbuf(KM_SLEEP); 14341 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14342 new_bp->b_bcount = datalen; 14343 new_bp->b_flags = bflags | 14344 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14345 #else 14346 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14347 datalen, bflags, SLEEP_FUNC, NULL); 14348 #endif 14349 new_bp->av_forw = NULL; 14350 new_bp->av_back = NULL; 14351 new_bp->b_dev = bp->b_dev; 14352 new_bp->b_blkno = blkno; 14353 new_bp->b_iodone = func; 14354 new_bp->b_edev = bp->b_edev; 14355 new_bp->b_resid = 0; 14356 14357 /* We need to preserve the B_FAILFAST flag */ 14358 if (bp->b_flags & B_FAILFAST) { 14359 new_bp->b_flags |= B_FAILFAST; 14360 } 14361 14362 /* 14363 * Allocate an xbuf for the shadow bp and copy the contents of the 14364 * original xbuf into it. 14365 */ 14366 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14367 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14368 14369 /* Need later to copy data between the shadow buf & original buf! */ 14370 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14371 14372 /* 14373 * The given bp is automatically saved in the xb_private member 14374 * of the new xbuf. Callers are allowed to depend on this. 14375 */ 14376 new_xp->xb_private = bp; 14377 14378 new_bp->b_private = new_xp; 14379 14380 return (new_bp); 14381 } 14382 14383 /* 14384 * Function: sd_bioclone_free 14385 * 14386 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14387 * in the larger than partition operation. 14388 * 14389 * Context: May be called under interrupt context 14390 */ 14391 14392 static void 14393 sd_bioclone_free(struct buf *bp) 14394 { 14395 struct sd_xbuf *xp; 14396 14397 ASSERT(bp != NULL); 14398 xp = SD_GET_XBUF(bp); 14399 ASSERT(xp != NULL); 14400 14401 /* 14402 * Call bp_mapout() before freeing the buf, in case a lower 14403 * layer or HBA had done a bp_mapin(). we must do this here 14404 * as we are the "originator" of the shadow buf. 14405 */ 14406 bp_mapout(bp); 14407 14408 /* 14409 * Null out b_iodone before freeing the bp, to ensure that the driver 14410 * never gets confused by a stale value in this field. (Just a little 14411 * extra defensiveness here.) 14412 */ 14413 bp->b_iodone = NULL; 14414 14415 freerbuf(bp); 14416 14417 kmem_free(xp, sizeof (struct sd_xbuf)); 14418 } 14419 14420 /* 14421 * Function: sd_shadow_buf_free 14422 * 14423 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14424 * 14425 * Context: May be called under interrupt context 14426 */ 14427 14428 static void 14429 sd_shadow_buf_free(struct buf *bp) 14430 { 14431 struct sd_xbuf *xp; 14432 14433 ASSERT(bp != NULL); 14434 xp = SD_GET_XBUF(bp); 14435 ASSERT(xp != NULL); 14436 14437 #if defined(__sparc) 14438 /* 14439 * Call bp_mapout() before freeing the buf, in case a lower 14440 * layer or HBA had done a bp_mapin(). we must do this here 14441 * as we are the "originator" of the shadow buf. 14442 */ 14443 bp_mapout(bp); 14444 #endif 14445 14446 /* 14447 * Null out b_iodone before freeing the bp, to ensure that the driver 14448 * never gets confused by a stale value in this field. (Just a little 14449 * extra defensiveness here.) 14450 */ 14451 bp->b_iodone = NULL; 14452 14453 #if defined(__i386) || defined(__amd64) 14454 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14455 freerbuf(bp); 14456 #else 14457 scsi_free_consistent_buf(bp); 14458 #endif 14459 14460 kmem_free(xp, sizeof (struct sd_xbuf)); 14461 } 14462 14463 14464 /* 14465 * Function: sd_print_transport_rejected_message 14466 * 14467 * Description: This implements the ludicrously complex rules for printing 14468 * a "transport rejected" message. This is to address the 14469 * specific problem of having a flood of this error message 14470 * produced when a failover occurs. 14471 * 14472 * Context: Any. 14473 */ 14474 14475 static void 14476 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14477 int code) 14478 { 14479 ASSERT(un != NULL); 14480 ASSERT(mutex_owned(SD_MUTEX(un))); 14481 ASSERT(xp != NULL); 14482 14483 /* 14484 * Print the "transport rejected" message under the following 14485 * conditions: 14486 * 14487 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14488 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14489 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14490 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14491 * scsi_transport(9F) (which indicates that the target might have 14492 * gone off-line). This uses the un->un_tran_fatal_count 14493 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14494 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14495 * from scsi_transport(). 14496 * 14497 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14498 * the preceeding cases in order for the message to be printed. 14499 */ 14500 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14501 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14502 (code != TRAN_FATAL_ERROR) || 14503 (un->un_tran_fatal_count == 1)) { 14504 switch (code) { 14505 case TRAN_BADPKT: 14506 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14507 "transport rejected bad packet\n"); 14508 break; 14509 case TRAN_FATAL_ERROR: 14510 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14511 "transport rejected fatal error\n"); 14512 break; 14513 default: 14514 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14515 "transport rejected (%d)\n", code); 14516 break; 14517 } 14518 } 14519 } 14520 } 14521 14522 14523 /* 14524 * Function: sd_add_buf_to_waitq 14525 * 14526 * Description: Add the given buf(9S) struct to the wait queue for the 14527 * instance. If sorting is enabled, then the buf is added 14528 * to the queue via an elevator sort algorithm (a la 14529 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14530 * If sorting is not enabled, then the buf is just added 14531 * to the end of the wait queue. 14532 * 14533 * Return Code: void 14534 * 14535 * Context: Does not sleep/block, therefore technically can be called 14536 * from any context. However if sorting is enabled then the 14537 * execution time is indeterminate, and may take long if 14538 * the wait queue grows large. 14539 */ 14540 14541 static void 14542 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14543 { 14544 struct buf *ap; 14545 14546 ASSERT(bp != NULL); 14547 ASSERT(un != NULL); 14548 ASSERT(mutex_owned(SD_MUTEX(un))); 14549 14550 /* If the queue is empty, add the buf as the only entry & return. */ 14551 if (un->un_waitq_headp == NULL) { 14552 ASSERT(un->un_waitq_tailp == NULL); 14553 un->un_waitq_headp = un->un_waitq_tailp = bp; 14554 bp->av_forw = NULL; 14555 return; 14556 } 14557 14558 ASSERT(un->un_waitq_tailp != NULL); 14559 14560 /* 14561 * If sorting is disabled, just add the buf to the tail end of 14562 * the wait queue and return. 14563 */ 14564 if (un->un_f_disksort_disabled) { 14565 un->un_waitq_tailp->av_forw = bp; 14566 un->un_waitq_tailp = bp; 14567 bp->av_forw = NULL; 14568 return; 14569 } 14570 14571 /* 14572 * Sort thru the list of requests currently on the wait queue 14573 * and add the new buf request at the appropriate position. 14574 * 14575 * The un->un_waitq_headp is an activity chain pointer on which 14576 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14577 * first queue holds those requests which are positioned after 14578 * the current SD_GET_BLKNO() (in the first request); the second holds 14579 * requests which came in after their SD_GET_BLKNO() number was passed. 14580 * Thus we implement a one way scan, retracting after reaching 14581 * the end of the drive to the first request on the second 14582 * queue, at which time it becomes the first queue. 14583 * A one-way scan is natural because of the way UNIX read-ahead 14584 * blocks are allocated. 14585 * 14586 * If we lie after the first request, then we must locate the 14587 * second request list and add ourselves to it. 14588 */ 14589 ap = un->un_waitq_headp; 14590 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14591 while (ap->av_forw != NULL) { 14592 /* 14593 * Look for an "inversion" in the (normally 14594 * ascending) block numbers. This indicates 14595 * the start of the second request list. 14596 */ 14597 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14598 /* 14599 * Search the second request list for the 14600 * first request at a larger block number. 14601 * We go before that; however if there is 14602 * no such request, we go at the end. 14603 */ 14604 do { 14605 if (SD_GET_BLKNO(bp) < 14606 SD_GET_BLKNO(ap->av_forw)) { 14607 goto insert; 14608 } 14609 ap = ap->av_forw; 14610 } while (ap->av_forw != NULL); 14611 goto insert; /* after last */ 14612 } 14613 ap = ap->av_forw; 14614 } 14615 14616 /* 14617 * No inversions... we will go after the last, and 14618 * be the first request in the second request list. 14619 */ 14620 goto insert; 14621 } 14622 14623 /* 14624 * Request is at/after the current request... 14625 * sort in the first request list. 14626 */ 14627 while (ap->av_forw != NULL) { 14628 /* 14629 * We want to go after the current request (1) if 14630 * there is an inversion after it (i.e. it is the end 14631 * of the first request list), or (2) if the next 14632 * request is a larger block no. than our request. 14633 */ 14634 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14635 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14636 goto insert; 14637 } 14638 ap = ap->av_forw; 14639 } 14640 14641 /* 14642 * Neither a second list nor a larger request, therefore 14643 * we go at the end of the first list (which is the same 14644 * as the end of the whole schebang). 14645 */ 14646 insert: 14647 bp->av_forw = ap->av_forw; 14648 ap->av_forw = bp; 14649 14650 /* 14651 * If we inserted onto the tail end of the waitq, make sure the 14652 * tail pointer is updated. 14653 */ 14654 if (ap == un->un_waitq_tailp) { 14655 un->un_waitq_tailp = bp; 14656 } 14657 } 14658 14659 14660 /* 14661 * Function: sd_start_cmds 14662 * 14663 * Description: Remove and transport cmds from the driver queues. 14664 * 14665 * Arguments: un - pointer to the unit (soft state) struct for the target. 14666 * 14667 * immed_bp - ptr to a buf to be transported immediately. Only 14668 * the immed_bp is transported; bufs on the waitq are not 14669 * processed and the un_retry_bp is not checked. If immed_bp is 14670 * NULL, then normal queue processing is performed. 14671 * 14672 * Context: May be called from kernel thread context, interrupt context, 14673 * or runout callback context. This function may not block or 14674 * call routines that block. 14675 */ 14676 14677 static void 14678 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14679 { 14680 struct sd_xbuf *xp; 14681 struct buf *bp; 14682 void (*statp)(kstat_io_t *); 14683 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14684 void (*saved_statp)(kstat_io_t *); 14685 #endif 14686 int rval; 14687 14688 ASSERT(un != NULL); 14689 ASSERT(mutex_owned(SD_MUTEX(un))); 14690 ASSERT(un->un_ncmds_in_transport >= 0); 14691 ASSERT(un->un_throttle >= 0); 14692 14693 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14694 14695 do { 14696 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14697 saved_statp = NULL; 14698 #endif 14699 14700 /* 14701 * If we are syncing or dumping, fail the command to 14702 * avoid recursively calling back into scsi_transport(). 14703 * The dump I/O itself uses a separate code path so this 14704 * only prevents non-dump I/O from being sent while dumping. 14705 * File system sync takes place before dumping begins. 14706 * During panic, filesystem I/O is allowed provided 14707 * un_in_callback is <= 1. This is to prevent recursion 14708 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14709 * sd_start_cmds and so on. See panic.c for more information 14710 * about the states the system can be in during panic. 14711 */ 14712 if ((un->un_state == SD_STATE_DUMPING) || 14713 (ddi_in_panic() && (un->un_in_callback > 1))) { 14714 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14715 "sd_start_cmds: panicking\n"); 14716 goto exit; 14717 } 14718 14719 if ((bp = immed_bp) != NULL) { 14720 /* 14721 * We have a bp that must be transported immediately. 14722 * It's OK to transport the immed_bp here without doing 14723 * the throttle limit check because the immed_bp is 14724 * always used in a retry/recovery case. This means 14725 * that we know we are not at the throttle limit by 14726 * virtue of the fact that to get here we must have 14727 * already gotten a command back via sdintr(). This also 14728 * relies on (1) the command on un_retry_bp preventing 14729 * further commands from the waitq from being issued; 14730 * and (2) the code in sd_retry_command checking the 14731 * throttle limit before issuing a delayed or immediate 14732 * retry. This holds even if the throttle limit is 14733 * currently ratcheted down from its maximum value. 14734 */ 14735 statp = kstat_runq_enter; 14736 if (bp == un->un_retry_bp) { 14737 ASSERT((un->un_retry_statp == NULL) || 14738 (un->un_retry_statp == kstat_waitq_enter) || 14739 (un->un_retry_statp == 14740 kstat_runq_back_to_waitq)); 14741 /* 14742 * If the waitq kstat was incremented when 14743 * sd_set_retry_bp() queued this bp for a retry, 14744 * then we must set up statp so that the waitq 14745 * count will get decremented correctly below. 14746 * Also we must clear un->un_retry_statp to 14747 * ensure that we do not act on a stale value 14748 * in this field. 14749 */ 14750 if ((un->un_retry_statp == kstat_waitq_enter) || 14751 (un->un_retry_statp == 14752 kstat_runq_back_to_waitq)) { 14753 statp = kstat_waitq_to_runq; 14754 } 14755 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14756 saved_statp = un->un_retry_statp; 14757 #endif 14758 un->un_retry_statp = NULL; 14759 14760 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14761 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14762 "un_throttle:%d un_ncmds_in_transport:%d\n", 14763 un, un->un_retry_bp, un->un_throttle, 14764 un->un_ncmds_in_transport); 14765 } else { 14766 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14767 "processing priority bp:0x%p\n", bp); 14768 } 14769 14770 } else if ((bp = un->un_waitq_headp) != NULL) { 14771 /* 14772 * A command on the waitq is ready to go, but do not 14773 * send it if: 14774 * 14775 * (1) the throttle limit has been reached, or 14776 * (2) a retry is pending, or 14777 * (3) a START_STOP_UNIT callback pending, or 14778 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14779 * command is pending. 14780 * 14781 * For all of these conditions, IO processing will 14782 * restart after the condition is cleared. 14783 */ 14784 if (un->un_ncmds_in_transport >= un->un_throttle) { 14785 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14786 "sd_start_cmds: exiting, " 14787 "throttle limit reached!\n"); 14788 goto exit; 14789 } 14790 if (un->un_retry_bp != NULL) { 14791 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14792 "sd_start_cmds: exiting, retry pending!\n"); 14793 goto exit; 14794 } 14795 if (un->un_startstop_timeid != NULL) { 14796 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14797 "sd_start_cmds: exiting, " 14798 "START_STOP pending!\n"); 14799 goto exit; 14800 } 14801 if (un->un_direct_priority_timeid != NULL) { 14802 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14803 "sd_start_cmds: exiting, " 14804 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14805 goto exit; 14806 } 14807 14808 /* Dequeue the command */ 14809 un->un_waitq_headp = bp->av_forw; 14810 if (un->un_waitq_headp == NULL) { 14811 un->un_waitq_tailp = NULL; 14812 } 14813 bp->av_forw = NULL; 14814 statp = kstat_waitq_to_runq; 14815 SD_TRACE(SD_LOG_IO_CORE, un, 14816 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14817 14818 } else { 14819 /* No work to do so bail out now */ 14820 SD_TRACE(SD_LOG_IO_CORE, un, 14821 "sd_start_cmds: no more work, exiting!\n"); 14822 goto exit; 14823 } 14824 14825 /* 14826 * Reset the state to normal. This is the mechanism by which 14827 * the state transitions from either SD_STATE_RWAIT or 14828 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14829 * If state is SD_STATE_PM_CHANGING then this command is 14830 * part of the device power control and the state must 14831 * not be put back to normal. Doing so would would 14832 * allow new commands to proceed when they shouldn't, 14833 * the device may be going off. 14834 */ 14835 if ((un->un_state != SD_STATE_SUSPENDED) && 14836 (un->un_state != SD_STATE_PM_CHANGING)) { 14837 New_state(un, SD_STATE_NORMAL); 14838 } 14839 14840 xp = SD_GET_XBUF(bp); 14841 ASSERT(xp != NULL); 14842 14843 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14844 /* 14845 * Allocate the scsi_pkt if we need one, or attach DMA 14846 * resources if we have a scsi_pkt that needs them. The 14847 * latter should only occur for commands that are being 14848 * retried. 14849 */ 14850 if ((xp->xb_pktp == NULL) || 14851 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14852 #else 14853 if (xp->xb_pktp == NULL) { 14854 #endif 14855 /* 14856 * There is no scsi_pkt allocated for this buf. Call 14857 * the initpkt function to allocate & init one. 14858 * 14859 * The scsi_init_pkt runout callback functionality is 14860 * implemented as follows: 14861 * 14862 * 1) The initpkt function always calls 14863 * scsi_init_pkt(9F) with sdrunout specified as the 14864 * callback routine. 14865 * 2) A successful packet allocation is initialized and 14866 * the I/O is transported. 14867 * 3) The I/O associated with an allocation resource 14868 * failure is left on its queue to be retried via 14869 * runout or the next I/O. 14870 * 4) The I/O associated with a DMA error is removed 14871 * from the queue and failed with EIO. Processing of 14872 * the transport queues is also halted to be 14873 * restarted via runout or the next I/O. 14874 * 5) The I/O associated with a CDB size or packet 14875 * size error is removed from the queue and failed 14876 * with EIO. Processing of the transport queues is 14877 * continued. 14878 * 14879 * Note: there is no interface for canceling a runout 14880 * callback. To prevent the driver from detaching or 14881 * suspending while a runout is pending the driver 14882 * state is set to SD_STATE_RWAIT 14883 * 14884 * Note: using the scsi_init_pkt callback facility can 14885 * result in an I/O request persisting at the head of 14886 * the list which cannot be satisfied even after 14887 * multiple retries. In the future the driver may 14888 * implement some kind of maximum runout count before 14889 * failing an I/O. 14890 * 14891 * Note: the use of funcp below may seem superfluous, 14892 * but it helps warlock figure out the correct 14893 * initpkt function calls (see [s]sd.wlcmd). 14894 */ 14895 struct scsi_pkt *pktp; 14896 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14897 14898 ASSERT(bp != un->un_rqs_bp); 14899 14900 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14901 switch ((*funcp)(bp, &pktp)) { 14902 case SD_PKT_ALLOC_SUCCESS: 14903 xp->xb_pktp = pktp; 14904 SD_TRACE(SD_LOG_IO_CORE, un, 14905 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14906 pktp); 14907 goto got_pkt; 14908 14909 case SD_PKT_ALLOC_FAILURE: 14910 /* 14911 * Temporary (hopefully) resource depletion. 14912 * Since retries and RQS commands always have a 14913 * scsi_pkt allocated, these cases should never 14914 * get here. So the only cases this needs to 14915 * handle is a bp from the waitq (which we put 14916 * back onto the waitq for sdrunout), or a bp 14917 * sent as an immed_bp (which we just fail). 14918 */ 14919 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14920 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14921 14922 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14923 14924 if (bp == immed_bp) { 14925 /* 14926 * If SD_XB_DMA_FREED is clear, then 14927 * this is a failure to allocate a 14928 * scsi_pkt, and we must fail the 14929 * command. 14930 */ 14931 if ((xp->xb_pkt_flags & 14932 SD_XB_DMA_FREED) == 0) { 14933 break; 14934 } 14935 14936 /* 14937 * If this immediate command is NOT our 14938 * un_retry_bp, then we must fail it. 14939 */ 14940 if (bp != un->un_retry_bp) { 14941 break; 14942 } 14943 14944 /* 14945 * We get here if this cmd is our 14946 * un_retry_bp that was DMAFREED, but 14947 * scsi_init_pkt() failed to reallocate 14948 * DMA resources when we attempted to 14949 * retry it. This can happen when an 14950 * mpxio failover is in progress, but 14951 * we don't want to just fail the 14952 * command in this case. 14953 * 14954 * Use timeout(9F) to restart it after 14955 * a 100ms delay. We don't want to 14956 * let sdrunout() restart it, because 14957 * sdrunout() is just supposed to start 14958 * commands that are sitting on the 14959 * wait queue. The un_retry_bp stays 14960 * set until the command completes, but 14961 * sdrunout can be called many times 14962 * before that happens. Since sdrunout 14963 * cannot tell if the un_retry_bp is 14964 * already in the transport, it could 14965 * end up calling scsi_transport() for 14966 * the un_retry_bp multiple times. 14967 * 14968 * Also: don't schedule the callback 14969 * if some other callback is already 14970 * pending. 14971 */ 14972 if (un->un_retry_statp == NULL) { 14973 /* 14974 * restore the kstat pointer to 14975 * keep kstat counts coherent 14976 * when we do retry the command. 14977 */ 14978 un->un_retry_statp = 14979 saved_statp; 14980 } 14981 14982 if ((un->un_startstop_timeid == NULL) && 14983 (un->un_retry_timeid == NULL) && 14984 (un->un_direct_priority_timeid == 14985 NULL)) { 14986 14987 un->un_retry_timeid = 14988 timeout( 14989 sd_start_retry_command, 14990 un, SD_RESTART_TIMEOUT); 14991 } 14992 goto exit; 14993 } 14994 14995 #else 14996 if (bp == immed_bp) { 14997 break; /* Just fail the command */ 14998 } 14999 #endif 15000 15001 /* Add the buf back to the head of the waitq */ 15002 bp->av_forw = un->un_waitq_headp; 15003 un->un_waitq_headp = bp; 15004 if (un->un_waitq_tailp == NULL) { 15005 un->un_waitq_tailp = bp; 15006 } 15007 goto exit; 15008 15009 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15010 /* 15011 * HBA DMA resource failure. Fail the command 15012 * and continue processing of the queues. 15013 */ 15014 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15015 "sd_start_cmds: " 15016 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15017 break; 15018 15019 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15020 /* 15021 * Note:x86: Partial DMA mapping not supported 15022 * for USCSI commands, and all the needed DMA 15023 * resources were not allocated. 15024 */ 15025 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15026 "sd_start_cmds: " 15027 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15028 break; 15029 15030 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15031 /* 15032 * Note:x86: Request cannot fit into CDB based 15033 * on lba and len. 15034 */ 15035 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15036 "sd_start_cmds: " 15037 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15038 break; 15039 15040 default: 15041 /* Should NEVER get here! */ 15042 panic("scsi_initpkt error"); 15043 /*NOTREACHED*/ 15044 } 15045 15046 /* 15047 * Fatal error in allocating a scsi_pkt for this buf. 15048 * Update kstats & return the buf with an error code. 15049 * We must use sd_return_failed_command_no_restart() to 15050 * avoid a recursive call back into sd_start_cmds(). 15051 * However this also means that we must keep processing 15052 * the waitq here in order to avoid stalling. 15053 */ 15054 if (statp == kstat_waitq_to_runq) { 15055 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15056 } 15057 sd_return_failed_command_no_restart(un, bp, EIO); 15058 if (bp == immed_bp) { 15059 /* immed_bp is gone by now, so clear this */ 15060 immed_bp = NULL; 15061 } 15062 continue; 15063 } 15064 got_pkt: 15065 if (bp == immed_bp) { 15066 /* goto the head of the class.... */ 15067 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15068 } 15069 15070 un->un_ncmds_in_transport++; 15071 SD_UPDATE_KSTATS(un, statp, bp); 15072 15073 /* 15074 * Call scsi_transport() to send the command to the target. 15075 * According to SCSA architecture, we must drop the mutex here 15076 * before calling scsi_transport() in order to avoid deadlock. 15077 * Note that the scsi_pkt's completion routine can be executed 15078 * (from interrupt context) even before the call to 15079 * scsi_transport() returns. 15080 */ 15081 SD_TRACE(SD_LOG_IO_CORE, un, 15082 "sd_start_cmds: calling scsi_transport()\n"); 15083 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15084 15085 mutex_exit(SD_MUTEX(un)); 15086 rval = scsi_transport(xp->xb_pktp); 15087 mutex_enter(SD_MUTEX(un)); 15088 15089 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15090 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15091 15092 switch (rval) { 15093 case TRAN_ACCEPT: 15094 /* Clear this with every pkt accepted by the HBA */ 15095 un->un_tran_fatal_count = 0; 15096 break; /* Success; try the next cmd (if any) */ 15097 15098 case TRAN_BUSY: 15099 un->un_ncmds_in_transport--; 15100 ASSERT(un->un_ncmds_in_transport >= 0); 15101 15102 /* 15103 * Don't retry request sense, the sense data 15104 * is lost when another request is sent. 15105 * Free up the rqs buf and retry 15106 * the original failed cmd. Update kstat. 15107 */ 15108 if (bp == un->un_rqs_bp) { 15109 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15110 bp = sd_mark_rqs_idle(un, xp); 15111 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15112 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 15113 kstat_waitq_enter); 15114 goto exit; 15115 } 15116 15117 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15118 /* 15119 * Free the DMA resources for the scsi_pkt. This will 15120 * allow mpxio to select another path the next time 15121 * we call scsi_transport() with this scsi_pkt. 15122 * See sdintr() for the rationalization behind this. 15123 */ 15124 if ((un->un_f_is_fibre == TRUE) && 15125 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15126 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15127 scsi_dmafree(xp->xb_pktp); 15128 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15129 } 15130 #endif 15131 15132 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15133 /* 15134 * Commands that are SD_PATH_DIRECT_PRIORITY 15135 * are for error recovery situations. These do 15136 * not use the normal command waitq, so if they 15137 * get a TRAN_BUSY we cannot put them back onto 15138 * the waitq for later retry. One possible 15139 * problem is that there could already be some 15140 * other command on un_retry_bp that is waiting 15141 * for this one to complete, so we would be 15142 * deadlocked if we put this command back onto 15143 * the waitq for later retry (since un_retry_bp 15144 * must complete before the driver gets back to 15145 * commands on the waitq). 15146 * 15147 * To avoid deadlock we must schedule a callback 15148 * that will restart this command after a set 15149 * interval. This should keep retrying for as 15150 * long as the underlying transport keeps 15151 * returning TRAN_BUSY (just like for other 15152 * commands). Use the same timeout interval as 15153 * for the ordinary TRAN_BUSY retry. 15154 */ 15155 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15156 "sd_start_cmds: scsi_transport() returned " 15157 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15158 15159 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15160 un->un_direct_priority_timeid = 15161 timeout(sd_start_direct_priority_command, 15162 bp, SD_BSY_TIMEOUT / 500); 15163 15164 goto exit; 15165 } 15166 15167 /* 15168 * For TRAN_BUSY, we want to reduce the throttle value, 15169 * unless we are retrying a command. 15170 */ 15171 if (bp != un->un_retry_bp) { 15172 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15173 } 15174 15175 /* 15176 * Set up the bp to be tried again 10 ms later. 15177 * Note:x86: Is there a timeout value in the sd_lun 15178 * for this condition? 15179 */ 15180 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 15181 kstat_runq_back_to_waitq); 15182 goto exit; 15183 15184 case TRAN_FATAL_ERROR: 15185 un->un_tran_fatal_count++; 15186 /* FALLTHRU */ 15187 15188 case TRAN_BADPKT: 15189 default: 15190 un->un_ncmds_in_transport--; 15191 ASSERT(un->un_ncmds_in_transport >= 0); 15192 15193 /* 15194 * If this is our REQUEST SENSE command with a 15195 * transport error, we must get back the pointers 15196 * to the original buf, and mark the REQUEST 15197 * SENSE command as "available". 15198 */ 15199 if (bp == un->un_rqs_bp) { 15200 bp = sd_mark_rqs_idle(un, xp); 15201 xp = SD_GET_XBUF(bp); 15202 } else { 15203 /* 15204 * Legacy behavior: do not update transport 15205 * error count for request sense commands. 15206 */ 15207 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15208 } 15209 15210 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15211 sd_print_transport_rejected_message(un, xp, rval); 15212 15213 /* 15214 * We must use sd_return_failed_command_no_restart() to 15215 * avoid a recursive call back into sd_start_cmds(). 15216 * However this also means that we must keep processing 15217 * the waitq here in order to avoid stalling. 15218 */ 15219 sd_return_failed_command_no_restart(un, bp, EIO); 15220 15221 /* 15222 * Notify any threads waiting in sd_ddi_suspend() that 15223 * a command completion has occurred. 15224 */ 15225 if (un->un_state == SD_STATE_SUSPENDED) { 15226 cv_broadcast(&un->un_disk_busy_cv); 15227 } 15228 15229 if (bp == immed_bp) { 15230 /* immed_bp is gone by now, so clear this */ 15231 immed_bp = NULL; 15232 } 15233 break; 15234 } 15235 15236 } while (immed_bp == NULL); 15237 15238 exit: 15239 ASSERT(mutex_owned(SD_MUTEX(un))); 15240 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15241 } 15242 15243 15244 /* 15245 * Function: sd_return_command 15246 * 15247 * Description: Returns a command to its originator (with or without an 15248 * error). Also starts commands waiting to be transported 15249 * to the target. 15250 * 15251 * Context: May be called from interrupt, kernel, or timeout context 15252 */ 15253 15254 static void 15255 sd_return_command(struct sd_lun *un, struct buf *bp) 15256 { 15257 struct sd_xbuf *xp; 15258 #if defined(__i386) || defined(__amd64) 15259 struct scsi_pkt *pktp; 15260 #endif 15261 15262 ASSERT(bp != NULL); 15263 ASSERT(un != NULL); 15264 ASSERT(mutex_owned(SD_MUTEX(un))); 15265 ASSERT(bp != un->un_rqs_bp); 15266 xp = SD_GET_XBUF(bp); 15267 ASSERT(xp != NULL); 15268 15269 #if defined(__i386) || defined(__amd64) 15270 pktp = SD_GET_PKTP(bp); 15271 #endif 15272 15273 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15274 15275 #if defined(__i386) || defined(__amd64) 15276 /* 15277 * Note:x86: check for the "sdrestart failed" case. 15278 */ 15279 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15280 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15281 (xp->xb_pktp->pkt_resid == 0)) { 15282 15283 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15284 /* 15285 * Successfully set up next portion of cmd 15286 * transfer, try sending it 15287 */ 15288 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15289 NULL, NULL, 0, (clock_t)0, NULL); 15290 sd_start_cmds(un, NULL); 15291 return; /* Note:x86: need a return here? */ 15292 } 15293 } 15294 #endif 15295 15296 /* 15297 * If this is the failfast bp, clear it from un_failfast_bp. This 15298 * can happen if upon being re-tried the failfast bp either 15299 * succeeded or encountered another error (possibly even a different 15300 * error than the one that precipitated the failfast state, but in 15301 * that case it would have had to exhaust retries as well). Regardless, 15302 * this should not occur whenever the instance is in the active 15303 * failfast state. 15304 */ 15305 if (bp == un->un_failfast_bp) { 15306 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15307 un->un_failfast_bp = NULL; 15308 } 15309 15310 /* 15311 * Clear the failfast state upon successful completion of ANY cmd. 15312 */ 15313 if (bp->b_error == 0) { 15314 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15315 } 15316 15317 /* 15318 * This is used if the command was retried one or more times. Show that 15319 * we are done with it, and allow processing of the waitq to resume. 15320 */ 15321 if (bp == un->un_retry_bp) { 15322 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15323 "sd_return_command: un:0x%p: " 15324 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15325 un->un_retry_bp = NULL; 15326 un->un_retry_statp = NULL; 15327 } 15328 15329 SD_UPDATE_RDWR_STATS(un, bp); 15330 SD_UPDATE_PARTITION_STATS(un, bp); 15331 15332 switch (un->un_state) { 15333 case SD_STATE_SUSPENDED: 15334 /* 15335 * Notify any threads waiting in sd_ddi_suspend() that 15336 * a command completion has occurred. 15337 */ 15338 cv_broadcast(&un->un_disk_busy_cv); 15339 break; 15340 default: 15341 sd_start_cmds(un, NULL); 15342 break; 15343 } 15344 15345 /* Return this command up the iodone chain to its originator. */ 15346 mutex_exit(SD_MUTEX(un)); 15347 15348 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15349 xp->xb_pktp = NULL; 15350 15351 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15352 15353 ASSERT(!mutex_owned(SD_MUTEX(un))); 15354 mutex_enter(SD_MUTEX(un)); 15355 15356 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15357 } 15358 15359 15360 /* 15361 * Function: sd_return_failed_command 15362 * 15363 * Description: Command completion when an error occurred. 15364 * 15365 * Context: May be called from interrupt context 15366 */ 15367 15368 static void 15369 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15370 { 15371 ASSERT(bp != NULL); 15372 ASSERT(un != NULL); 15373 ASSERT(mutex_owned(SD_MUTEX(un))); 15374 15375 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15376 "sd_return_failed_command: entry\n"); 15377 15378 /* 15379 * b_resid could already be nonzero due to a partial data 15380 * transfer, so do not change it here. 15381 */ 15382 SD_BIOERROR(bp, errcode); 15383 15384 sd_return_command(un, bp); 15385 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15386 "sd_return_failed_command: exit\n"); 15387 } 15388 15389 15390 /* 15391 * Function: sd_return_failed_command_no_restart 15392 * 15393 * Description: Same as sd_return_failed_command, but ensures that no 15394 * call back into sd_start_cmds will be issued. 15395 * 15396 * Context: May be called from interrupt context 15397 */ 15398 15399 static void 15400 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15401 int errcode) 15402 { 15403 struct sd_xbuf *xp; 15404 15405 ASSERT(bp != NULL); 15406 ASSERT(un != NULL); 15407 ASSERT(mutex_owned(SD_MUTEX(un))); 15408 xp = SD_GET_XBUF(bp); 15409 ASSERT(xp != NULL); 15410 ASSERT(errcode != 0); 15411 15412 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15413 "sd_return_failed_command_no_restart: entry\n"); 15414 15415 /* 15416 * b_resid could already be nonzero due to a partial data 15417 * transfer, so do not change it here. 15418 */ 15419 SD_BIOERROR(bp, errcode); 15420 15421 /* 15422 * If this is the failfast bp, clear it. This can happen if the 15423 * failfast bp encounterd a fatal error when we attempted to 15424 * re-try it (such as a scsi_transport(9F) failure). However 15425 * we should NOT be in an active failfast state if the failfast 15426 * bp is not NULL. 15427 */ 15428 if (bp == un->un_failfast_bp) { 15429 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15430 un->un_failfast_bp = NULL; 15431 } 15432 15433 if (bp == un->un_retry_bp) { 15434 /* 15435 * This command was retried one or more times. Show that we are 15436 * done with it, and allow processing of the waitq to resume. 15437 */ 15438 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15439 "sd_return_failed_command_no_restart: " 15440 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15441 un->un_retry_bp = NULL; 15442 un->un_retry_statp = NULL; 15443 } 15444 15445 SD_UPDATE_RDWR_STATS(un, bp); 15446 SD_UPDATE_PARTITION_STATS(un, bp); 15447 15448 mutex_exit(SD_MUTEX(un)); 15449 15450 if (xp->xb_pktp != NULL) { 15451 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15452 xp->xb_pktp = NULL; 15453 } 15454 15455 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15456 15457 mutex_enter(SD_MUTEX(un)); 15458 15459 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15460 "sd_return_failed_command_no_restart: exit\n"); 15461 } 15462 15463 15464 /* 15465 * Function: sd_retry_command 15466 * 15467 * Description: queue up a command for retry, or (optionally) fail it 15468 * if retry counts are exhausted. 15469 * 15470 * Arguments: un - Pointer to the sd_lun struct for the target. 15471 * 15472 * bp - Pointer to the buf for the command to be retried. 15473 * 15474 * retry_check_flag - Flag to see which (if any) of the retry 15475 * counts should be decremented/checked. If the indicated 15476 * retry count is exhausted, then the command will not be 15477 * retried; it will be failed instead. This should use a 15478 * value equal to one of the following: 15479 * 15480 * SD_RETRIES_NOCHECK 15481 * SD_RESD_RETRIES_STANDARD 15482 * SD_RETRIES_VICTIM 15483 * 15484 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15485 * if the check should be made to see of FLAG_ISOLATE is set 15486 * in the pkt. If FLAG_ISOLATE is set, then the command is 15487 * not retried, it is simply failed. 15488 * 15489 * user_funcp - Ptr to function to call before dispatching the 15490 * command. May be NULL if no action needs to be performed. 15491 * (Primarily intended for printing messages.) 15492 * 15493 * user_arg - Optional argument to be passed along to 15494 * the user_funcp call. 15495 * 15496 * failure_code - errno return code to set in the bp if the 15497 * command is going to be failed. 15498 * 15499 * retry_delay - Retry delay interval in (clock_t) units. May 15500 * be zero which indicates that the retry should be retried 15501 * immediately (ie, without an intervening delay). 15502 * 15503 * statp - Ptr to kstat function to be updated if the command 15504 * is queued for a delayed retry. May be NULL if no kstat 15505 * update is desired. 15506 * 15507 * Context: May be called from interupt context. 15508 */ 15509 15510 static void 15511 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15512 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15513 code), void *user_arg, int failure_code, clock_t retry_delay, 15514 void (*statp)(kstat_io_t *)) 15515 { 15516 struct sd_xbuf *xp; 15517 struct scsi_pkt *pktp; 15518 15519 ASSERT(un != NULL); 15520 ASSERT(mutex_owned(SD_MUTEX(un))); 15521 ASSERT(bp != NULL); 15522 xp = SD_GET_XBUF(bp); 15523 ASSERT(xp != NULL); 15524 pktp = SD_GET_PKTP(bp); 15525 ASSERT(pktp != NULL); 15526 15527 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15528 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15529 15530 /* 15531 * If we are syncing or dumping, fail the command to avoid 15532 * recursively calling back into scsi_transport(). 15533 */ 15534 if (ddi_in_panic()) { 15535 goto fail_command_no_log; 15536 } 15537 15538 /* 15539 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15540 * log an error and fail the command. 15541 */ 15542 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15543 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15544 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15545 sd_dump_memory(un, SD_LOG_IO, "CDB", 15546 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15547 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15548 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15549 goto fail_command; 15550 } 15551 15552 /* 15553 * If we are suspended, then put the command onto head of the 15554 * wait queue since we don't want to start more commands. 15555 */ 15556 switch (un->un_state) { 15557 case SD_STATE_SUSPENDED: 15558 case SD_STATE_DUMPING: 15559 bp->av_forw = un->un_waitq_headp; 15560 un->un_waitq_headp = bp; 15561 if (un->un_waitq_tailp == NULL) { 15562 un->un_waitq_tailp = bp; 15563 } 15564 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15565 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15566 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15567 return; 15568 default: 15569 break; 15570 } 15571 15572 /* 15573 * If the caller wants us to check FLAG_ISOLATE, then see if that 15574 * is set; if it is then we do not want to retry the command. 15575 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15576 */ 15577 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15578 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15579 goto fail_command; 15580 } 15581 } 15582 15583 15584 /* 15585 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15586 * command timeout or a selection timeout has occurred. This means 15587 * that we were unable to establish an kind of communication with 15588 * the target, and subsequent retries and/or commands are likely 15589 * to encounter similar results and take a long time to complete. 15590 * 15591 * If this is a failfast error condition, we need to update the 15592 * failfast state, even if this bp does not have B_FAILFAST set. 15593 */ 15594 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15595 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15596 ASSERT(un->un_failfast_bp == NULL); 15597 /* 15598 * If we are already in the active failfast state, and 15599 * another failfast error condition has been detected, 15600 * then fail this command if it has B_FAILFAST set. 15601 * If B_FAILFAST is clear, then maintain the legacy 15602 * behavior of retrying heroically, even tho this will 15603 * take a lot more time to fail the command. 15604 */ 15605 if (bp->b_flags & B_FAILFAST) { 15606 goto fail_command; 15607 } 15608 } else { 15609 /* 15610 * We're not in the active failfast state, but we 15611 * have a failfast error condition, so we must begin 15612 * transition to the next state. We do this regardless 15613 * of whether or not this bp has B_FAILFAST set. 15614 */ 15615 if (un->un_failfast_bp == NULL) { 15616 /* 15617 * This is the first bp to meet a failfast 15618 * condition so save it on un_failfast_bp & 15619 * do normal retry processing. Do not enter 15620 * active failfast state yet. This marks 15621 * entry into the "failfast pending" state. 15622 */ 15623 un->un_failfast_bp = bp; 15624 15625 } else if (un->un_failfast_bp == bp) { 15626 /* 15627 * This is the second time *this* bp has 15628 * encountered a failfast error condition, 15629 * so enter active failfast state & flush 15630 * queues as appropriate. 15631 */ 15632 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15633 un->un_failfast_bp = NULL; 15634 sd_failfast_flushq(un); 15635 15636 /* 15637 * Fail this bp now if B_FAILFAST set; 15638 * otherwise continue with retries. (It would 15639 * be pretty ironic if this bp succeeded on a 15640 * subsequent retry after we just flushed all 15641 * the queues). 15642 */ 15643 if (bp->b_flags & B_FAILFAST) { 15644 goto fail_command; 15645 } 15646 15647 #if !defined(lint) && !defined(__lint) 15648 } else { 15649 /* 15650 * If neither of the preceeding conditionals 15651 * was true, it means that there is some 15652 * *other* bp that has met an inital failfast 15653 * condition and is currently either being 15654 * retried or is waiting to be retried. In 15655 * that case we should perform normal retry 15656 * processing on *this* bp, since there is a 15657 * chance that the current failfast condition 15658 * is transient and recoverable. If that does 15659 * not turn out to be the case, then retries 15660 * will be cleared when the wait queue is 15661 * flushed anyway. 15662 */ 15663 #endif 15664 } 15665 } 15666 } else { 15667 /* 15668 * SD_RETRIES_FAILFAST is clear, which indicates that we 15669 * likely were able to at least establish some level of 15670 * communication with the target and subsequent commands 15671 * and/or retries are likely to get through to the target, 15672 * In this case we want to be aggressive about clearing 15673 * the failfast state. Note that this does not affect 15674 * the "failfast pending" condition. 15675 */ 15676 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15677 } 15678 15679 15680 /* 15681 * Check the specified retry count to see if we can still do 15682 * any retries with this pkt before we should fail it. 15683 */ 15684 switch (retry_check_flag & SD_RETRIES_MASK) { 15685 case SD_RETRIES_VICTIM: 15686 /* 15687 * Check the victim retry count. If exhausted, then fall 15688 * thru & check against the standard retry count. 15689 */ 15690 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15691 /* Increment count & proceed with the retry */ 15692 xp->xb_victim_retry_count++; 15693 break; 15694 } 15695 /* Victim retries exhausted, fall back to std. retries... */ 15696 /* FALLTHRU */ 15697 15698 case SD_RETRIES_STANDARD: 15699 if (xp->xb_retry_count >= un->un_retry_count) { 15700 /* Retries exhausted, fail the command */ 15701 SD_TRACE(SD_LOG_IO_CORE, un, 15702 "sd_retry_command: retries exhausted!\n"); 15703 /* 15704 * update b_resid for failed SCMD_READ & SCMD_WRITE 15705 * commands with nonzero pkt_resid. 15706 */ 15707 if ((pktp->pkt_reason == CMD_CMPLT) && 15708 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15709 (pktp->pkt_resid != 0)) { 15710 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15711 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15712 SD_UPDATE_B_RESID(bp, pktp); 15713 } 15714 } 15715 goto fail_command; 15716 } 15717 xp->xb_retry_count++; 15718 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15719 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15720 break; 15721 15722 case SD_RETRIES_UA: 15723 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15724 /* Retries exhausted, fail the command */ 15725 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15726 "Unit Attention retries exhausted. " 15727 "Check the target.\n"); 15728 goto fail_command; 15729 } 15730 xp->xb_ua_retry_count++; 15731 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15732 "sd_retry_command: retry count:%d\n", 15733 xp->xb_ua_retry_count); 15734 break; 15735 15736 case SD_RETRIES_BUSY: 15737 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15738 /* Retries exhausted, fail the command */ 15739 SD_TRACE(SD_LOG_IO_CORE, un, 15740 "sd_retry_command: retries exhausted!\n"); 15741 goto fail_command; 15742 } 15743 xp->xb_retry_count++; 15744 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15745 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15746 break; 15747 15748 case SD_RETRIES_NOCHECK: 15749 default: 15750 /* No retry count to check. Just proceed with the retry */ 15751 break; 15752 } 15753 15754 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15755 15756 /* 15757 * If we were given a zero timeout, we must attempt to retry the 15758 * command immediately (ie, without a delay). 15759 */ 15760 if (retry_delay == 0) { 15761 /* 15762 * Check some limiting conditions to see if we can actually 15763 * do the immediate retry. If we cannot, then we must 15764 * fall back to queueing up a delayed retry. 15765 */ 15766 if (un->un_ncmds_in_transport >= un->un_throttle) { 15767 /* 15768 * We are at the throttle limit for the target, 15769 * fall back to delayed retry. 15770 */ 15771 retry_delay = SD_BSY_TIMEOUT; 15772 statp = kstat_waitq_enter; 15773 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15774 "sd_retry_command: immed. retry hit " 15775 "throttle!\n"); 15776 } else { 15777 /* 15778 * We're clear to proceed with the immediate retry. 15779 * First call the user-provided function (if any) 15780 */ 15781 if (user_funcp != NULL) { 15782 (*user_funcp)(un, bp, user_arg, 15783 SD_IMMEDIATE_RETRY_ISSUED); 15784 #ifdef __lock_lint 15785 sd_print_incomplete_msg(un, bp, user_arg, 15786 SD_IMMEDIATE_RETRY_ISSUED); 15787 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15788 SD_IMMEDIATE_RETRY_ISSUED); 15789 sd_print_sense_failed_msg(un, bp, user_arg, 15790 SD_IMMEDIATE_RETRY_ISSUED); 15791 #endif 15792 } 15793 15794 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15795 "sd_retry_command: issuing immediate retry\n"); 15796 15797 /* 15798 * Call sd_start_cmds() to transport the command to 15799 * the target. 15800 */ 15801 sd_start_cmds(un, bp); 15802 15803 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15804 "sd_retry_command exit\n"); 15805 return; 15806 } 15807 } 15808 15809 /* 15810 * Set up to retry the command after a delay. 15811 * First call the user-provided function (if any) 15812 */ 15813 if (user_funcp != NULL) { 15814 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15815 } 15816 15817 sd_set_retry_bp(un, bp, retry_delay, statp); 15818 15819 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15820 return; 15821 15822 fail_command: 15823 15824 if (user_funcp != NULL) { 15825 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15826 } 15827 15828 fail_command_no_log: 15829 15830 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15831 "sd_retry_command: returning failed command\n"); 15832 15833 sd_return_failed_command(un, bp, failure_code); 15834 15835 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15836 } 15837 15838 15839 /* 15840 * Function: sd_set_retry_bp 15841 * 15842 * Description: Set up the given bp for retry. 15843 * 15844 * Arguments: un - ptr to associated softstate 15845 * bp - ptr to buf(9S) for the command 15846 * retry_delay - time interval before issuing retry (may be 0) 15847 * statp - optional pointer to kstat function 15848 * 15849 * Context: May be called under interrupt context 15850 */ 15851 15852 static void 15853 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15854 void (*statp)(kstat_io_t *)) 15855 { 15856 ASSERT(un != NULL); 15857 ASSERT(mutex_owned(SD_MUTEX(un))); 15858 ASSERT(bp != NULL); 15859 15860 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15861 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15862 15863 /* 15864 * Indicate that the command is being retried. This will not allow any 15865 * other commands on the wait queue to be transported to the target 15866 * until this command has been completed (success or failure). The 15867 * "retry command" is not transported to the target until the given 15868 * time delay expires, unless the user specified a 0 retry_delay. 15869 * 15870 * Note: the timeout(9F) callback routine is what actually calls 15871 * sd_start_cmds() to transport the command, with the exception of a 15872 * zero retry_delay. The only current implementor of a zero retry delay 15873 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15874 */ 15875 if (un->un_retry_bp == NULL) { 15876 ASSERT(un->un_retry_statp == NULL); 15877 un->un_retry_bp = bp; 15878 15879 /* 15880 * If the user has not specified a delay the command should 15881 * be queued and no timeout should be scheduled. 15882 */ 15883 if (retry_delay == 0) { 15884 /* 15885 * Save the kstat pointer that will be used in the 15886 * call to SD_UPDATE_KSTATS() below, so that 15887 * sd_start_cmds() can correctly decrement the waitq 15888 * count when it is time to transport this command. 15889 */ 15890 un->un_retry_statp = statp; 15891 goto done; 15892 } 15893 } 15894 15895 if (un->un_retry_bp == bp) { 15896 /* 15897 * Save the kstat pointer that will be used in the call to 15898 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15899 * correctly decrement the waitq count when it is time to 15900 * transport this command. 15901 */ 15902 un->un_retry_statp = statp; 15903 15904 /* 15905 * Schedule a timeout if: 15906 * 1) The user has specified a delay. 15907 * 2) There is not a START_STOP_UNIT callback pending. 15908 * 15909 * If no delay has been specified, then it is up to the caller 15910 * to ensure that IO processing continues without stalling. 15911 * Effectively, this means that the caller will issue the 15912 * required call to sd_start_cmds(). The START_STOP_UNIT 15913 * callback does this after the START STOP UNIT command has 15914 * completed. In either of these cases we should not schedule 15915 * a timeout callback here. Also don't schedule the timeout if 15916 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15917 */ 15918 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15919 (un->un_direct_priority_timeid == NULL)) { 15920 un->un_retry_timeid = 15921 timeout(sd_start_retry_command, un, retry_delay); 15922 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15923 "sd_set_retry_bp: setting timeout: un: 0x%p" 15924 " bp:0x%p un_retry_timeid:0x%p\n", 15925 un, bp, un->un_retry_timeid); 15926 } 15927 } else { 15928 /* 15929 * We only get in here if there is already another command 15930 * waiting to be retried. In this case, we just put the 15931 * given command onto the wait queue, so it can be transported 15932 * after the current retry command has completed. 15933 * 15934 * Also we have to make sure that if the command at the head 15935 * of the wait queue is the un_failfast_bp, that we do not 15936 * put ahead of it any other commands that are to be retried. 15937 */ 15938 if ((un->un_failfast_bp != NULL) && 15939 (un->un_failfast_bp == un->un_waitq_headp)) { 15940 /* 15941 * Enqueue this command AFTER the first command on 15942 * the wait queue (which is also un_failfast_bp). 15943 */ 15944 bp->av_forw = un->un_waitq_headp->av_forw; 15945 un->un_waitq_headp->av_forw = bp; 15946 if (un->un_waitq_headp == un->un_waitq_tailp) { 15947 un->un_waitq_tailp = bp; 15948 } 15949 } else { 15950 /* Enqueue this command at the head of the waitq. */ 15951 bp->av_forw = un->un_waitq_headp; 15952 un->un_waitq_headp = bp; 15953 if (un->un_waitq_tailp == NULL) { 15954 un->un_waitq_tailp = bp; 15955 } 15956 } 15957 15958 if (statp == NULL) { 15959 statp = kstat_waitq_enter; 15960 } 15961 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15962 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15963 } 15964 15965 done: 15966 if (statp != NULL) { 15967 SD_UPDATE_KSTATS(un, statp, bp); 15968 } 15969 15970 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15971 "sd_set_retry_bp: exit un:0x%p\n", un); 15972 } 15973 15974 15975 /* 15976 * Function: sd_start_retry_command 15977 * 15978 * Description: Start the command that has been waiting on the target's 15979 * retry queue. Called from timeout(9F) context after the 15980 * retry delay interval has expired. 15981 * 15982 * Arguments: arg - pointer to associated softstate for the device. 15983 * 15984 * Context: timeout(9F) thread context. May not sleep. 15985 */ 15986 15987 static void 15988 sd_start_retry_command(void *arg) 15989 { 15990 struct sd_lun *un = arg; 15991 15992 ASSERT(un != NULL); 15993 ASSERT(!mutex_owned(SD_MUTEX(un))); 15994 15995 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15996 "sd_start_retry_command: entry\n"); 15997 15998 mutex_enter(SD_MUTEX(un)); 15999 16000 un->un_retry_timeid = NULL; 16001 16002 if (un->un_retry_bp != NULL) { 16003 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16004 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16005 un, un->un_retry_bp); 16006 sd_start_cmds(un, un->un_retry_bp); 16007 } 16008 16009 mutex_exit(SD_MUTEX(un)); 16010 16011 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16012 "sd_start_retry_command: exit\n"); 16013 } 16014 16015 16016 /* 16017 * Function: sd_start_direct_priority_command 16018 * 16019 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16020 * received TRAN_BUSY when we called scsi_transport() to send it 16021 * to the underlying HBA. This function is called from timeout(9F) 16022 * context after the delay interval has expired. 16023 * 16024 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16025 * 16026 * Context: timeout(9F) thread context. May not sleep. 16027 */ 16028 16029 static void 16030 sd_start_direct_priority_command(void *arg) 16031 { 16032 struct buf *priority_bp = arg; 16033 struct sd_lun *un; 16034 16035 ASSERT(priority_bp != NULL); 16036 un = SD_GET_UN(priority_bp); 16037 ASSERT(un != NULL); 16038 ASSERT(!mutex_owned(SD_MUTEX(un))); 16039 16040 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16041 "sd_start_direct_priority_command: entry\n"); 16042 16043 mutex_enter(SD_MUTEX(un)); 16044 un->un_direct_priority_timeid = NULL; 16045 sd_start_cmds(un, priority_bp); 16046 mutex_exit(SD_MUTEX(un)); 16047 16048 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16049 "sd_start_direct_priority_command: exit\n"); 16050 } 16051 16052 16053 /* 16054 * Function: sd_send_request_sense_command 16055 * 16056 * Description: Sends a REQUEST SENSE command to the target 16057 * 16058 * Context: May be called from interrupt context. 16059 */ 16060 16061 static void 16062 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16063 struct scsi_pkt *pktp) 16064 { 16065 ASSERT(bp != NULL); 16066 ASSERT(un != NULL); 16067 ASSERT(mutex_owned(SD_MUTEX(un))); 16068 16069 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16070 "entry: buf:0x%p\n", bp); 16071 16072 /* 16073 * If we are syncing or dumping, then fail the command to avoid a 16074 * recursive callback into scsi_transport(). Also fail the command 16075 * if we are suspended (legacy behavior). 16076 */ 16077 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16078 (un->un_state == SD_STATE_DUMPING)) { 16079 sd_return_failed_command(un, bp, EIO); 16080 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16081 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16082 return; 16083 } 16084 16085 /* 16086 * Retry the failed command and don't issue the request sense if: 16087 * 1) the sense buf is busy 16088 * 2) we have 1 or more outstanding commands on the target 16089 * (the sense data will be cleared or invalidated any way) 16090 * 16091 * Note: There could be an issue with not checking a retry limit here, 16092 * the problem is determining which retry limit to check. 16093 */ 16094 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16095 /* Don't retry if the command is flagged as non-retryable */ 16096 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16097 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16098 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 16099 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16100 "sd_send_request_sense_command: " 16101 "at full throttle, retrying exit\n"); 16102 } else { 16103 sd_return_failed_command(un, bp, EIO); 16104 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16105 "sd_send_request_sense_command: " 16106 "at full throttle, non-retryable exit\n"); 16107 } 16108 return; 16109 } 16110 16111 sd_mark_rqs_busy(un, bp); 16112 sd_start_cmds(un, un->un_rqs_bp); 16113 16114 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16115 "sd_send_request_sense_command: exit\n"); 16116 } 16117 16118 16119 /* 16120 * Function: sd_mark_rqs_busy 16121 * 16122 * Description: Indicate that the request sense bp for this instance is 16123 * in use. 16124 * 16125 * Context: May be called under interrupt context 16126 */ 16127 16128 static void 16129 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16130 { 16131 struct sd_xbuf *sense_xp; 16132 16133 ASSERT(un != NULL); 16134 ASSERT(bp != NULL); 16135 ASSERT(mutex_owned(SD_MUTEX(un))); 16136 ASSERT(un->un_sense_isbusy == 0); 16137 16138 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16139 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16140 16141 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16142 ASSERT(sense_xp != NULL); 16143 16144 SD_INFO(SD_LOG_IO, un, 16145 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16146 16147 ASSERT(sense_xp->xb_pktp != NULL); 16148 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16149 == (FLAG_SENSING | FLAG_HEAD)); 16150 16151 un->un_sense_isbusy = 1; 16152 un->un_rqs_bp->b_resid = 0; 16153 sense_xp->xb_pktp->pkt_resid = 0; 16154 sense_xp->xb_pktp->pkt_reason = 0; 16155 16156 /* So we can get back the bp at interrupt time! */ 16157 sense_xp->xb_sense_bp = bp; 16158 16159 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16160 16161 /* 16162 * Mark this buf as awaiting sense data. (This is already set in 16163 * the pkt_flags for the RQS packet.) 16164 */ 16165 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16166 16167 sense_xp->xb_retry_count = 0; 16168 sense_xp->xb_victim_retry_count = 0; 16169 sense_xp->xb_ua_retry_count = 0; 16170 sense_xp->xb_dma_resid = 0; 16171 16172 /* Clean up the fields for auto-request sense */ 16173 sense_xp->xb_sense_status = 0; 16174 sense_xp->xb_sense_state = 0; 16175 sense_xp->xb_sense_resid = 0; 16176 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16177 16178 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16179 } 16180 16181 16182 /* 16183 * Function: sd_mark_rqs_idle 16184 * 16185 * Description: SD_MUTEX must be held continuously through this routine 16186 * to prevent reuse of the rqs struct before the caller can 16187 * complete it's processing. 16188 * 16189 * Return Code: Pointer to the RQS buf 16190 * 16191 * Context: May be called under interrupt context 16192 */ 16193 16194 static struct buf * 16195 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16196 { 16197 struct buf *bp; 16198 ASSERT(un != NULL); 16199 ASSERT(sense_xp != NULL); 16200 ASSERT(mutex_owned(SD_MUTEX(un))); 16201 ASSERT(un->un_sense_isbusy != 0); 16202 16203 un->un_sense_isbusy = 0; 16204 bp = sense_xp->xb_sense_bp; 16205 sense_xp->xb_sense_bp = NULL; 16206 16207 /* This pkt is no longer interested in getting sense data */ 16208 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16209 16210 return (bp); 16211 } 16212 16213 16214 16215 /* 16216 * Function: sd_alloc_rqs 16217 * 16218 * Description: Set up the unit to receive auto request sense data 16219 * 16220 * Return Code: DDI_SUCCESS or DDI_FAILURE 16221 * 16222 * Context: Called under attach(9E) context 16223 */ 16224 16225 static int 16226 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16227 { 16228 struct sd_xbuf *xp; 16229 16230 ASSERT(un != NULL); 16231 ASSERT(!mutex_owned(SD_MUTEX(un))); 16232 ASSERT(un->un_rqs_bp == NULL); 16233 ASSERT(un->un_rqs_pktp == NULL); 16234 16235 /* 16236 * First allocate the required buf and scsi_pkt structs, then set up 16237 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16238 */ 16239 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16240 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16241 if (un->un_rqs_bp == NULL) { 16242 return (DDI_FAILURE); 16243 } 16244 16245 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16246 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16247 16248 if (un->un_rqs_pktp == NULL) { 16249 sd_free_rqs(un); 16250 return (DDI_FAILURE); 16251 } 16252 16253 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16254 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16255 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 16256 16257 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16258 16259 /* Set up the other needed members in the ARQ scsi_pkt. */ 16260 un->un_rqs_pktp->pkt_comp = sdintr; 16261 un->un_rqs_pktp->pkt_time = sd_io_time; 16262 un->un_rqs_pktp->pkt_flags |= 16263 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16264 16265 /* 16266 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16267 * provide any intpkt, destroypkt routines as we take care of 16268 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16269 */ 16270 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16271 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16272 xp->xb_pktp = un->un_rqs_pktp; 16273 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16274 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16275 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16276 16277 /* 16278 * Save the pointer to the request sense private bp so it can 16279 * be retrieved in sdintr. 16280 */ 16281 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16282 ASSERT(un->un_rqs_bp->b_private == xp); 16283 16284 /* 16285 * See if the HBA supports auto-request sense for the specified 16286 * target/lun. If it does, then try to enable it (if not already 16287 * enabled). 16288 * 16289 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16290 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16291 * return success. However, in both of these cases ARQ is always 16292 * enabled and scsi_ifgetcap will always return true. The best approach 16293 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16294 * 16295 * The 3rd case is the HBA (adp) always return enabled on 16296 * scsi_ifgetgetcap even when it's not enable, the best approach 16297 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16298 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16299 */ 16300 16301 if (un->un_f_is_fibre == TRUE) { 16302 un->un_f_arq_enabled = TRUE; 16303 } else { 16304 #if defined(__i386) || defined(__amd64) 16305 /* 16306 * Circumvent the Adaptec bug, remove this code when 16307 * the bug is fixed 16308 */ 16309 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16310 #endif 16311 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16312 case 0: 16313 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16314 "sd_alloc_rqs: HBA supports ARQ\n"); 16315 /* 16316 * ARQ is supported by this HBA but currently is not 16317 * enabled. Attempt to enable it and if successful then 16318 * mark this instance as ARQ enabled. 16319 */ 16320 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16321 == 1) { 16322 /* Successfully enabled ARQ in the HBA */ 16323 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16324 "sd_alloc_rqs: ARQ enabled\n"); 16325 un->un_f_arq_enabled = TRUE; 16326 } else { 16327 /* Could not enable ARQ in the HBA */ 16328 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16329 "sd_alloc_rqs: failed ARQ enable\n"); 16330 un->un_f_arq_enabled = FALSE; 16331 } 16332 break; 16333 case 1: 16334 /* 16335 * ARQ is supported by this HBA and is already enabled. 16336 * Just mark ARQ as enabled for this instance. 16337 */ 16338 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16339 "sd_alloc_rqs: ARQ already enabled\n"); 16340 un->un_f_arq_enabled = TRUE; 16341 break; 16342 default: 16343 /* 16344 * ARQ is not supported by this HBA; disable it for this 16345 * instance. 16346 */ 16347 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16348 "sd_alloc_rqs: HBA does not support ARQ\n"); 16349 un->un_f_arq_enabled = FALSE; 16350 break; 16351 } 16352 } 16353 16354 return (DDI_SUCCESS); 16355 } 16356 16357 16358 /* 16359 * Function: sd_free_rqs 16360 * 16361 * Description: Cleanup for the pre-instance RQS command. 16362 * 16363 * Context: Kernel thread context 16364 */ 16365 16366 static void 16367 sd_free_rqs(struct sd_lun *un) 16368 { 16369 ASSERT(un != NULL); 16370 16371 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16372 16373 /* 16374 * If consistent memory is bound to a scsi_pkt, the pkt 16375 * has to be destroyed *before* freeing the consistent memory. 16376 * Don't change the sequence of this operations. 16377 * scsi_destroy_pkt() might access memory, which isn't allowed, 16378 * after it was freed in scsi_free_consistent_buf(). 16379 */ 16380 if (un->un_rqs_pktp != NULL) { 16381 scsi_destroy_pkt(un->un_rqs_pktp); 16382 un->un_rqs_pktp = NULL; 16383 } 16384 16385 if (un->un_rqs_bp != NULL) { 16386 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 16387 scsi_free_consistent_buf(un->un_rqs_bp); 16388 un->un_rqs_bp = NULL; 16389 } 16390 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16391 } 16392 16393 16394 16395 /* 16396 * Function: sd_reduce_throttle 16397 * 16398 * Description: Reduces the maximun # of outstanding commands on a 16399 * target to the current number of outstanding commands. 16400 * Queues a tiemout(9F) callback to restore the limit 16401 * after a specified interval has elapsed. 16402 * Typically used when we get a TRAN_BUSY return code 16403 * back from scsi_transport(). 16404 * 16405 * Arguments: un - ptr to the sd_lun softstate struct 16406 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16407 * 16408 * Context: May be called from interrupt context 16409 */ 16410 16411 static void 16412 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16413 { 16414 ASSERT(un != NULL); 16415 ASSERT(mutex_owned(SD_MUTEX(un))); 16416 ASSERT(un->un_ncmds_in_transport >= 0); 16417 16418 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16419 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16420 un, un->un_throttle, un->un_ncmds_in_transport); 16421 16422 if (un->un_throttle > 1) { 16423 if (un->un_f_use_adaptive_throttle == TRUE) { 16424 switch (throttle_type) { 16425 case SD_THROTTLE_TRAN_BUSY: 16426 if (un->un_busy_throttle == 0) { 16427 un->un_busy_throttle = un->un_throttle; 16428 } 16429 break; 16430 case SD_THROTTLE_QFULL: 16431 un->un_busy_throttle = 0; 16432 break; 16433 default: 16434 ASSERT(FALSE); 16435 } 16436 16437 if (un->un_ncmds_in_transport > 0) { 16438 un->un_throttle = un->un_ncmds_in_transport; 16439 } 16440 16441 } else { 16442 if (un->un_ncmds_in_transport == 0) { 16443 un->un_throttle = 1; 16444 } else { 16445 un->un_throttle = un->un_ncmds_in_transport; 16446 } 16447 } 16448 } 16449 16450 /* Reschedule the timeout if none is currently active */ 16451 if (un->un_reset_throttle_timeid == NULL) { 16452 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16453 un, SD_THROTTLE_RESET_INTERVAL); 16454 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16455 "sd_reduce_throttle: timeout scheduled!\n"); 16456 } 16457 16458 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16459 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16460 } 16461 16462 16463 16464 /* 16465 * Function: sd_restore_throttle 16466 * 16467 * Description: Callback function for timeout(9F). Resets the current 16468 * value of un->un_throttle to its default. 16469 * 16470 * Arguments: arg - pointer to associated softstate for the device. 16471 * 16472 * Context: May be called from interrupt context 16473 */ 16474 16475 static void 16476 sd_restore_throttle(void *arg) 16477 { 16478 struct sd_lun *un = arg; 16479 16480 ASSERT(un != NULL); 16481 ASSERT(!mutex_owned(SD_MUTEX(un))); 16482 16483 mutex_enter(SD_MUTEX(un)); 16484 16485 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16486 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16487 16488 un->un_reset_throttle_timeid = NULL; 16489 16490 if (un->un_f_use_adaptive_throttle == TRUE) { 16491 /* 16492 * If un_busy_throttle is nonzero, then it contains the 16493 * value that un_throttle was when we got a TRAN_BUSY back 16494 * from scsi_transport(). We want to revert back to this 16495 * value. 16496 * 16497 * In the QFULL case, the throttle limit will incrementally 16498 * increase until it reaches max throttle. 16499 */ 16500 if (un->un_busy_throttle > 0) { 16501 un->un_throttle = un->un_busy_throttle; 16502 un->un_busy_throttle = 0; 16503 } else { 16504 /* 16505 * increase throttle by 10% open gate slowly, schedule 16506 * another restore if saved throttle has not been 16507 * reached 16508 */ 16509 short throttle; 16510 if (sd_qfull_throttle_enable) { 16511 throttle = un->un_throttle + 16512 max((un->un_throttle / 10), 1); 16513 un->un_throttle = 16514 (throttle < un->un_saved_throttle) ? 16515 throttle : un->un_saved_throttle; 16516 if (un->un_throttle < un->un_saved_throttle) { 16517 un->un_reset_throttle_timeid = 16518 timeout(sd_restore_throttle, 16519 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16520 } 16521 } 16522 } 16523 16524 /* 16525 * If un_throttle has fallen below the low-water mark, we 16526 * restore the maximum value here (and allow it to ratchet 16527 * down again if necessary). 16528 */ 16529 if (un->un_throttle < un->un_min_throttle) { 16530 un->un_throttle = un->un_saved_throttle; 16531 } 16532 } else { 16533 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16534 "restoring limit from 0x%x to 0x%x\n", 16535 un->un_throttle, un->un_saved_throttle); 16536 un->un_throttle = un->un_saved_throttle; 16537 } 16538 16539 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16540 "sd_restore_throttle: calling sd_start_cmds!\n"); 16541 16542 sd_start_cmds(un, NULL); 16543 16544 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16545 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16546 un, un->un_throttle); 16547 16548 mutex_exit(SD_MUTEX(un)); 16549 16550 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16551 } 16552 16553 /* 16554 * Function: sdrunout 16555 * 16556 * Description: Callback routine for scsi_init_pkt when a resource allocation 16557 * fails. 16558 * 16559 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16560 * soft state instance. 16561 * 16562 * Return Code: The scsi_init_pkt routine allows for the callback function to 16563 * return a 0 indicating the callback should be rescheduled or a 1 16564 * indicating not to reschedule. This routine always returns 1 16565 * because the driver always provides a callback function to 16566 * scsi_init_pkt. This results in a callback always being scheduled 16567 * (via the scsi_init_pkt callback implementation) if a resource 16568 * failure occurs. 16569 * 16570 * Context: This callback function may not block or call routines that block 16571 * 16572 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16573 * request persisting at the head of the list which cannot be 16574 * satisfied even after multiple retries. In the future the driver 16575 * may implement some time of maximum runout count before failing 16576 * an I/O. 16577 */ 16578 16579 static int 16580 sdrunout(caddr_t arg) 16581 { 16582 struct sd_lun *un = (struct sd_lun *)arg; 16583 16584 ASSERT(un != NULL); 16585 ASSERT(!mutex_owned(SD_MUTEX(un))); 16586 16587 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16588 16589 mutex_enter(SD_MUTEX(un)); 16590 sd_start_cmds(un, NULL); 16591 mutex_exit(SD_MUTEX(un)); 16592 /* 16593 * This callback routine always returns 1 (i.e. do not reschedule) 16594 * because we always specify sdrunout as the callback handler for 16595 * scsi_init_pkt inside the call to sd_start_cmds. 16596 */ 16597 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16598 return (1); 16599 } 16600 16601 16602 /* 16603 * Function: sdintr 16604 * 16605 * Description: Completion callback routine for scsi_pkt(9S) structs 16606 * sent to the HBA driver via scsi_transport(9F). 16607 * 16608 * Context: Interrupt context 16609 */ 16610 16611 static void 16612 sdintr(struct scsi_pkt *pktp) 16613 { 16614 struct buf *bp; 16615 struct sd_xbuf *xp; 16616 struct sd_lun *un; 16617 16618 ASSERT(pktp != NULL); 16619 bp = (struct buf *)pktp->pkt_private; 16620 ASSERT(bp != NULL); 16621 xp = SD_GET_XBUF(bp); 16622 ASSERT(xp != NULL); 16623 ASSERT(xp->xb_pktp != NULL); 16624 un = SD_GET_UN(bp); 16625 ASSERT(un != NULL); 16626 ASSERT(!mutex_owned(SD_MUTEX(un))); 16627 16628 #ifdef SD_FAULT_INJECTION 16629 16630 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16631 /* SD FaultInjection */ 16632 sd_faultinjection(pktp); 16633 16634 #endif /* SD_FAULT_INJECTION */ 16635 16636 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16637 " xp:0x%p, un:0x%p\n", bp, xp, un); 16638 16639 mutex_enter(SD_MUTEX(un)); 16640 16641 /* Reduce the count of the #commands currently in transport */ 16642 un->un_ncmds_in_transport--; 16643 ASSERT(un->un_ncmds_in_transport >= 0); 16644 16645 /* Increment counter to indicate that the callback routine is active */ 16646 un->un_in_callback++; 16647 16648 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16649 16650 #ifdef SDDEBUG 16651 if (bp == un->un_retry_bp) { 16652 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16653 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16654 un, un->un_retry_bp, un->un_ncmds_in_transport); 16655 } 16656 #endif 16657 16658 /* 16659 * If pkt_reason is CMD_DEV_GONE, just fail the command 16660 */ 16661 if (pktp->pkt_reason == CMD_DEV_GONE) { 16662 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16663 "Device is gone\n"); 16664 sd_return_failed_command(un, bp, EIO); 16665 goto exit; 16666 } 16667 16668 /* 16669 * First see if the pkt has auto-request sense data with it.... 16670 * Look at the packet state first so we don't take a performance 16671 * hit looking at the arq enabled flag unless absolutely necessary. 16672 */ 16673 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16674 (un->un_f_arq_enabled == TRUE)) { 16675 /* 16676 * The HBA did an auto request sense for this command so check 16677 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16678 * driver command that should not be retried. 16679 */ 16680 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16681 /* 16682 * Save the relevant sense info into the xp for the 16683 * original cmd. 16684 */ 16685 struct scsi_arq_status *asp; 16686 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16687 xp->xb_sense_status = 16688 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16689 xp->xb_sense_state = asp->sts_rqpkt_state; 16690 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16691 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16692 min(sizeof (struct scsi_extended_sense), 16693 SENSE_LENGTH)); 16694 16695 /* fail the command */ 16696 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16697 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16698 sd_return_failed_command(un, bp, EIO); 16699 goto exit; 16700 } 16701 16702 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16703 /* 16704 * We want to either retry or fail this command, so free 16705 * the DMA resources here. If we retry the command then 16706 * the DMA resources will be reallocated in sd_start_cmds(). 16707 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16708 * causes the *entire* transfer to start over again from the 16709 * beginning of the request, even for PARTIAL chunks that 16710 * have already transferred successfully. 16711 */ 16712 if ((un->un_f_is_fibre == TRUE) && 16713 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16714 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16715 scsi_dmafree(pktp); 16716 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16717 } 16718 #endif 16719 16720 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16721 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16722 16723 sd_handle_auto_request_sense(un, bp, xp, pktp); 16724 goto exit; 16725 } 16726 16727 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16728 if (pktp->pkt_flags & FLAG_SENSING) { 16729 /* This pktp is from the unit's REQUEST_SENSE command */ 16730 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16731 "sdintr: sd_handle_request_sense\n"); 16732 sd_handle_request_sense(un, bp, xp, pktp); 16733 goto exit; 16734 } 16735 16736 /* 16737 * Check to see if the command successfully completed as requested; 16738 * this is the most common case (and also the hot performance path). 16739 * 16740 * Requirements for successful completion are: 16741 * pkt_reason is CMD_CMPLT and packet status is status good. 16742 * In addition: 16743 * - A residual of zero indicates successful completion no matter what 16744 * the command is. 16745 * - If the residual is not zero and the command is not a read or 16746 * write, then it's still defined as successful completion. In other 16747 * words, if the command is a read or write the residual must be 16748 * zero for successful completion. 16749 * - If the residual is not zero and the command is a read or 16750 * write, and it's a USCSICMD, then it's still defined as 16751 * successful completion. 16752 */ 16753 if ((pktp->pkt_reason == CMD_CMPLT) && 16754 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16755 16756 /* 16757 * Since this command is returned with a good status, we 16758 * can reset the count for Sonoma failover. 16759 */ 16760 un->un_sonoma_failure_count = 0; 16761 16762 /* 16763 * Return all USCSI commands on good status 16764 */ 16765 if (pktp->pkt_resid == 0) { 16766 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16767 "sdintr: returning command for resid == 0\n"); 16768 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16769 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16770 SD_UPDATE_B_RESID(bp, pktp); 16771 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16772 "sdintr: returning command for resid != 0\n"); 16773 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16774 SD_UPDATE_B_RESID(bp, pktp); 16775 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16776 "sdintr: returning uscsi command\n"); 16777 } else { 16778 goto not_successful; 16779 } 16780 sd_return_command(un, bp); 16781 16782 /* 16783 * Decrement counter to indicate that the callback routine 16784 * is done. 16785 */ 16786 un->un_in_callback--; 16787 ASSERT(un->un_in_callback >= 0); 16788 mutex_exit(SD_MUTEX(un)); 16789 16790 return; 16791 } 16792 16793 not_successful: 16794 16795 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16796 /* 16797 * The following is based upon knowledge of the underlying transport 16798 * and its use of DMA resources. This code should be removed when 16799 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16800 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16801 * and sd_start_cmds(). 16802 * 16803 * Free any DMA resources associated with this command if there 16804 * is a chance it could be retried or enqueued for later retry. 16805 * If we keep the DMA binding then mpxio cannot reissue the 16806 * command on another path whenever a path failure occurs. 16807 * 16808 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16809 * causes the *entire* transfer to start over again from the 16810 * beginning of the request, even for PARTIAL chunks that 16811 * have already transferred successfully. 16812 * 16813 * This is only done for non-uscsi commands (and also skipped for the 16814 * driver's internal RQS command). Also just do this for Fibre Channel 16815 * devices as these are the only ones that support mpxio. 16816 */ 16817 if ((un->un_f_is_fibre == TRUE) && 16818 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16819 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16820 scsi_dmafree(pktp); 16821 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16822 } 16823 #endif 16824 16825 /* 16826 * The command did not successfully complete as requested so check 16827 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16828 * driver command that should not be retried so just return. If 16829 * FLAG_DIAGNOSE is not set the error will be processed below. 16830 */ 16831 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16832 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16833 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16834 /* 16835 * Issue a request sense if a check condition caused the error 16836 * (we handle the auto request sense case above), otherwise 16837 * just fail the command. 16838 */ 16839 if ((pktp->pkt_reason == CMD_CMPLT) && 16840 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16841 sd_send_request_sense_command(un, bp, pktp); 16842 } else { 16843 sd_return_failed_command(un, bp, EIO); 16844 } 16845 goto exit; 16846 } 16847 16848 /* 16849 * The command did not successfully complete as requested so process 16850 * the error, retry, and/or attempt recovery. 16851 */ 16852 switch (pktp->pkt_reason) { 16853 case CMD_CMPLT: 16854 switch (SD_GET_PKT_STATUS(pktp)) { 16855 case STATUS_GOOD: 16856 /* 16857 * The command completed successfully with a non-zero 16858 * residual 16859 */ 16860 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16861 "sdintr: STATUS_GOOD \n"); 16862 sd_pkt_status_good(un, bp, xp, pktp); 16863 break; 16864 16865 case STATUS_CHECK: 16866 case STATUS_TERMINATED: 16867 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16868 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16869 sd_pkt_status_check_condition(un, bp, xp, pktp); 16870 break; 16871 16872 case STATUS_BUSY: 16873 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16874 "sdintr: STATUS_BUSY\n"); 16875 sd_pkt_status_busy(un, bp, xp, pktp); 16876 break; 16877 16878 case STATUS_RESERVATION_CONFLICT: 16879 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16880 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16881 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16882 break; 16883 16884 case STATUS_QFULL: 16885 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16886 "sdintr: STATUS_QFULL\n"); 16887 sd_pkt_status_qfull(un, bp, xp, pktp); 16888 break; 16889 16890 case STATUS_MET: 16891 case STATUS_INTERMEDIATE: 16892 case STATUS_SCSI2: 16893 case STATUS_INTERMEDIATE_MET: 16894 case STATUS_ACA_ACTIVE: 16895 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16896 "Unexpected SCSI status received: 0x%x\n", 16897 SD_GET_PKT_STATUS(pktp)); 16898 sd_return_failed_command(un, bp, EIO); 16899 break; 16900 16901 default: 16902 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16903 "Invalid SCSI status received: 0x%x\n", 16904 SD_GET_PKT_STATUS(pktp)); 16905 sd_return_failed_command(un, bp, EIO); 16906 break; 16907 16908 } 16909 break; 16910 16911 case CMD_INCOMPLETE: 16912 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16913 "sdintr: CMD_INCOMPLETE\n"); 16914 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16915 break; 16916 case CMD_TRAN_ERR: 16917 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16918 "sdintr: CMD_TRAN_ERR\n"); 16919 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16920 break; 16921 case CMD_RESET: 16922 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16923 "sdintr: CMD_RESET \n"); 16924 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16925 break; 16926 case CMD_ABORTED: 16927 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16928 "sdintr: CMD_ABORTED \n"); 16929 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16930 break; 16931 case CMD_TIMEOUT: 16932 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16933 "sdintr: CMD_TIMEOUT\n"); 16934 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16935 break; 16936 case CMD_UNX_BUS_FREE: 16937 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16938 "sdintr: CMD_UNX_BUS_FREE \n"); 16939 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16940 break; 16941 case CMD_TAG_REJECT: 16942 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16943 "sdintr: CMD_TAG_REJECT\n"); 16944 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16945 break; 16946 default: 16947 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16948 "sdintr: default\n"); 16949 sd_pkt_reason_default(un, bp, xp, pktp); 16950 break; 16951 } 16952 16953 exit: 16954 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16955 16956 /* Decrement counter to indicate that the callback routine is done. */ 16957 un->un_in_callback--; 16958 ASSERT(un->un_in_callback >= 0); 16959 16960 /* 16961 * At this point, the pkt has been dispatched, ie, it is either 16962 * being re-tried or has been returned to its caller and should 16963 * not be referenced. 16964 */ 16965 16966 mutex_exit(SD_MUTEX(un)); 16967 } 16968 16969 16970 /* 16971 * Function: sd_print_incomplete_msg 16972 * 16973 * Description: Prints the error message for a CMD_INCOMPLETE error. 16974 * 16975 * Arguments: un - ptr to associated softstate for the device. 16976 * bp - ptr to the buf(9S) for the command. 16977 * arg - message string ptr 16978 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 16979 * or SD_NO_RETRY_ISSUED. 16980 * 16981 * Context: May be called under interrupt context 16982 */ 16983 16984 static void 16985 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 16986 { 16987 struct scsi_pkt *pktp; 16988 char *msgp; 16989 char *cmdp = arg; 16990 16991 ASSERT(un != NULL); 16992 ASSERT(mutex_owned(SD_MUTEX(un))); 16993 ASSERT(bp != NULL); 16994 ASSERT(arg != NULL); 16995 pktp = SD_GET_PKTP(bp); 16996 ASSERT(pktp != NULL); 16997 16998 switch (code) { 16999 case SD_DELAYED_RETRY_ISSUED: 17000 case SD_IMMEDIATE_RETRY_ISSUED: 17001 msgp = "retrying"; 17002 break; 17003 case SD_NO_RETRY_ISSUED: 17004 default: 17005 msgp = "giving up"; 17006 break; 17007 } 17008 17009 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17010 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17011 "incomplete %s- %s\n", cmdp, msgp); 17012 } 17013 } 17014 17015 17016 17017 /* 17018 * Function: sd_pkt_status_good 17019 * 17020 * Description: Processing for a STATUS_GOOD code in pkt_status. 17021 * 17022 * Context: May be called under interrupt context 17023 */ 17024 17025 static void 17026 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17027 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17028 { 17029 char *cmdp; 17030 17031 ASSERT(un != NULL); 17032 ASSERT(mutex_owned(SD_MUTEX(un))); 17033 ASSERT(bp != NULL); 17034 ASSERT(xp != NULL); 17035 ASSERT(pktp != NULL); 17036 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17037 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17038 ASSERT(pktp->pkt_resid != 0); 17039 17040 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17041 17042 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17043 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17044 case SCMD_READ: 17045 cmdp = "read"; 17046 break; 17047 case SCMD_WRITE: 17048 cmdp = "write"; 17049 break; 17050 default: 17051 SD_UPDATE_B_RESID(bp, pktp); 17052 sd_return_command(un, bp); 17053 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17054 return; 17055 } 17056 17057 /* 17058 * See if we can retry the read/write, preferrably immediately. 17059 * If retries are exhaused, then sd_retry_command() will update 17060 * the b_resid count. 17061 */ 17062 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17063 cmdp, EIO, (clock_t)0, NULL); 17064 17065 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17066 } 17067 17068 17069 17070 17071 17072 /* 17073 * Function: sd_handle_request_sense 17074 * 17075 * Description: Processing for non-auto Request Sense command. 17076 * 17077 * Arguments: un - ptr to associated softstate 17078 * sense_bp - ptr to buf(9S) for the RQS command 17079 * sense_xp - ptr to the sd_xbuf for the RQS command 17080 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17081 * 17082 * Context: May be called under interrupt context 17083 */ 17084 17085 static void 17086 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17087 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17088 { 17089 struct buf *cmd_bp; /* buf for the original command */ 17090 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17091 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17092 17093 ASSERT(un != NULL); 17094 ASSERT(mutex_owned(SD_MUTEX(un))); 17095 ASSERT(sense_bp != NULL); 17096 ASSERT(sense_xp != NULL); 17097 ASSERT(sense_pktp != NULL); 17098 17099 /* 17100 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17101 * RQS command and not the original command. 17102 */ 17103 ASSERT(sense_pktp == un->un_rqs_pktp); 17104 ASSERT(sense_bp == un->un_rqs_bp); 17105 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17106 (FLAG_SENSING | FLAG_HEAD)); 17107 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17108 FLAG_SENSING) == FLAG_SENSING); 17109 17110 /* These are the bp, xp, and pktp for the original command */ 17111 cmd_bp = sense_xp->xb_sense_bp; 17112 cmd_xp = SD_GET_XBUF(cmd_bp); 17113 cmd_pktp = SD_GET_PKTP(cmd_bp); 17114 17115 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17116 /* 17117 * The REQUEST SENSE command failed. Release the REQUEST 17118 * SENSE command for re-use, get back the bp for the original 17119 * command, and attempt to re-try the original command if 17120 * FLAG_DIAGNOSE is not set in the original packet. 17121 */ 17122 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17123 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17124 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17125 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17126 NULL, NULL, EIO, (clock_t)0, NULL); 17127 return; 17128 } 17129 } 17130 17131 /* 17132 * Save the relevant sense info into the xp for the original cmd. 17133 * 17134 * Note: if the request sense failed the state info will be zero 17135 * as set in sd_mark_rqs_busy() 17136 */ 17137 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17138 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17139 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17140 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 17141 17142 /* 17143 * Free up the RQS command.... 17144 * NOTE: 17145 * Must do this BEFORE calling sd_validate_sense_data! 17146 * sd_validate_sense_data may return the original command in 17147 * which case the pkt will be freed and the flags can no 17148 * longer be touched. 17149 * SD_MUTEX is held through this process until the command 17150 * is dispatched based upon the sense data, so there are 17151 * no race conditions. 17152 */ 17153 (void) sd_mark_rqs_idle(un, sense_xp); 17154 17155 /* 17156 * For a retryable command see if we have valid sense data, if so then 17157 * turn it over to sd_decode_sense() to figure out the right course of 17158 * action. Just fail a non-retryable command. 17159 */ 17160 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17161 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 17162 SD_SENSE_DATA_IS_VALID) { 17163 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17164 } 17165 } else { 17166 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17167 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17168 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17169 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17170 sd_return_failed_command(un, cmd_bp, EIO); 17171 } 17172 } 17173 17174 17175 17176 17177 /* 17178 * Function: sd_handle_auto_request_sense 17179 * 17180 * Description: Processing for auto-request sense information. 17181 * 17182 * Arguments: un - ptr to associated softstate 17183 * bp - ptr to buf(9S) for the command 17184 * xp - ptr to the sd_xbuf for the command 17185 * pktp - ptr to the scsi_pkt(9S) for the command 17186 * 17187 * Context: May be called under interrupt context 17188 */ 17189 17190 static void 17191 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17192 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17193 { 17194 struct scsi_arq_status *asp; 17195 17196 ASSERT(un != NULL); 17197 ASSERT(mutex_owned(SD_MUTEX(un))); 17198 ASSERT(bp != NULL); 17199 ASSERT(xp != NULL); 17200 ASSERT(pktp != NULL); 17201 ASSERT(pktp != un->un_rqs_pktp); 17202 ASSERT(bp != un->un_rqs_bp); 17203 17204 /* 17205 * For auto-request sense, we get a scsi_arq_status back from 17206 * the HBA, with the sense data in the sts_sensedata member. 17207 * The pkt_scbp of the packet points to this scsi_arq_status. 17208 */ 17209 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17210 17211 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17212 /* 17213 * The auto REQUEST SENSE failed; see if we can re-try 17214 * the original command. 17215 */ 17216 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17217 "auto request sense failed (reason=%s)\n", 17218 scsi_rname(asp->sts_rqpkt_reason)); 17219 17220 sd_reset_target(un, pktp); 17221 17222 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17223 NULL, NULL, EIO, (clock_t)0, NULL); 17224 return; 17225 } 17226 17227 /* Save the relevant sense info into the xp for the original cmd. */ 17228 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17229 xp->xb_sense_state = asp->sts_rqpkt_state; 17230 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17231 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17232 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 17233 17234 /* 17235 * See if we have valid sense data, if so then turn it over to 17236 * sd_decode_sense() to figure out the right course of action. 17237 */ 17238 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 17239 sd_decode_sense(un, bp, xp, pktp); 17240 } 17241 } 17242 17243 17244 /* 17245 * Function: sd_print_sense_failed_msg 17246 * 17247 * Description: Print log message when RQS has failed. 17248 * 17249 * Arguments: un - ptr to associated softstate 17250 * bp - ptr to buf(9S) for the command 17251 * arg - generic message string ptr 17252 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17253 * or SD_NO_RETRY_ISSUED 17254 * 17255 * Context: May be called from interrupt context 17256 */ 17257 17258 static void 17259 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17260 int code) 17261 { 17262 char *msgp = arg; 17263 17264 ASSERT(un != NULL); 17265 ASSERT(mutex_owned(SD_MUTEX(un))); 17266 ASSERT(bp != NULL); 17267 17268 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17269 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17270 } 17271 } 17272 17273 17274 /* 17275 * Function: sd_validate_sense_data 17276 * 17277 * Description: Check the given sense data for validity. 17278 * If the sense data is not valid, the command will 17279 * be either failed or retried! 17280 * 17281 * Return Code: SD_SENSE_DATA_IS_INVALID 17282 * SD_SENSE_DATA_IS_VALID 17283 * 17284 * Context: May be called from interrupt context 17285 */ 17286 17287 static int 17288 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 17289 { 17290 struct scsi_extended_sense *esp; 17291 struct scsi_pkt *pktp; 17292 size_t actual_len; 17293 char *msgp = NULL; 17294 17295 ASSERT(un != NULL); 17296 ASSERT(mutex_owned(SD_MUTEX(un))); 17297 ASSERT(bp != NULL); 17298 ASSERT(bp != un->un_rqs_bp); 17299 ASSERT(xp != NULL); 17300 17301 pktp = SD_GET_PKTP(bp); 17302 ASSERT(pktp != NULL); 17303 17304 /* 17305 * Check the status of the RQS command (auto or manual). 17306 */ 17307 switch (xp->xb_sense_status & STATUS_MASK) { 17308 case STATUS_GOOD: 17309 break; 17310 17311 case STATUS_RESERVATION_CONFLICT: 17312 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17313 return (SD_SENSE_DATA_IS_INVALID); 17314 17315 case STATUS_BUSY: 17316 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17317 "Busy Status on REQUEST SENSE\n"); 17318 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17319 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17320 return (SD_SENSE_DATA_IS_INVALID); 17321 17322 case STATUS_QFULL: 17323 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17324 "QFULL Status on REQUEST SENSE\n"); 17325 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17326 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17327 return (SD_SENSE_DATA_IS_INVALID); 17328 17329 case STATUS_CHECK: 17330 case STATUS_TERMINATED: 17331 msgp = "Check Condition on REQUEST SENSE\n"; 17332 goto sense_failed; 17333 17334 default: 17335 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17336 goto sense_failed; 17337 } 17338 17339 /* 17340 * See if we got the minimum required amount of sense data. 17341 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17342 * or less. 17343 */ 17344 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 17345 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17346 (actual_len == 0)) { 17347 msgp = "Request Sense couldn't get sense data\n"; 17348 goto sense_failed; 17349 } 17350 17351 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17352 msgp = "Not enough sense information\n"; 17353 goto sense_failed; 17354 } 17355 17356 /* 17357 * We require the extended sense data 17358 */ 17359 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17360 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17361 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17362 static char tmp[8]; 17363 static char buf[148]; 17364 char *p = (char *)(xp->xb_sense_data); 17365 int i; 17366 17367 mutex_enter(&sd_sense_mutex); 17368 (void) strcpy(buf, "undecodable sense information:"); 17369 for (i = 0; i < actual_len; i++) { 17370 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17371 (void) strcpy(&buf[strlen(buf)], tmp); 17372 } 17373 i = strlen(buf); 17374 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17375 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 17376 mutex_exit(&sd_sense_mutex); 17377 } 17378 /* Note: Legacy behavior, fail the command with no retry */ 17379 sd_return_failed_command(un, bp, EIO); 17380 return (SD_SENSE_DATA_IS_INVALID); 17381 } 17382 17383 /* 17384 * Check that es_code is valid (es_class concatenated with es_code 17385 * make up the "response code" field. es_class will always be 7, so 17386 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17387 * format. 17388 */ 17389 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17390 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17391 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17392 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17393 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17394 goto sense_failed; 17395 } 17396 17397 return (SD_SENSE_DATA_IS_VALID); 17398 17399 sense_failed: 17400 /* 17401 * If the request sense failed (for whatever reason), attempt 17402 * to retry the original command. 17403 */ 17404 #if defined(__i386) || defined(__amd64) 17405 /* 17406 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17407 * sddef.h for Sparc platform, and x86 uses 1 binary 17408 * for both SCSI/FC. 17409 * The SD_RETRY_DELAY value need to be adjusted here 17410 * when SD_RETRY_DELAY change in sddef.h 17411 */ 17412 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17413 sd_print_sense_failed_msg, msgp, EIO, 17414 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17415 #else 17416 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17417 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17418 #endif 17419 17420 return (SD_SENSE_DATA_IS_INVALID); 17421 } 17422 17423 17424 17425 /* 17426 * Function: sd_decode_sense 17427 * 17428 * Description: Take recovery action(s) when SCSI Sense Data is received. 17429 * 17430 * Context: Interrupt context. 17431 */ 17432 17433 static void 17434 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17435 struct scsi_pkt *pktp) 17436 { 17437 uint8_t sense_key; 17438 17439 ASSERT(un != NULL); 17440 ASSERT(mutex_owned(SD_MUTEX(un))); 17441 ASSERT(bp != NULL); 17442 ASSERT(bp != un->un_rqs_bp); 17443 ASSERT(xp != NULL); 17444 ASSERT(pktp != NULL); 17445 17446 sense_key = scsi_sense_key(xp->xb_sense_data); 17447 17448 switch (sense_key) { 17449 case KEY_NO_SENSE: 17450 sd_sense_key_no_sense(un, bp, xp, pktp); 17451 break; 17452 case KEY_RECOVERABLE_ERROR: 17453 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17454 bp, xp, pktp); 17455 break; 17456 case KEY_NOT_READY: 17457 sd_sense_key_not_ready(un, xp->xb_sense_data, 17458 bp, xp, pktp); 17459 break; 17460 case KEY_MEDIUM_ERROR: 17461 case KEY_HARDWARE_ERROR: 17462 sd_sense_key_medium_or_hardware_error(un, 17463 xp->xb_sense_data, bp, xp, pktp); 17464 break; 17465 case KEY_ILLEGAL_REQUEST: 17466 sd_sense_key_illegal_request(un, bp, xp, pktp); 17467 break; 17468 case KEY_UNIT_ATTENTION: 17469 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17470 bp, xp, pktp); 17471 break; 17472 case KEY_WRITE_PROTECT: 17473 case KEY_VOLUME_OVERFLOW: 17474 case KEY_MISCOMPARE: 17475 sd_sense_key_fail_command(un, bp, xp, pktp); 17476 break; 17477 case KEY_BLANK_CHECK: 17478 sd_sense_key_blank_check(un, bp, xp, pktp); 17479 break; 17480 case KEY_ABORTED_COMMAND: 17481 sd_sense_key_aborted_command(un, bp, xp, pktp); 17482 break; 17483 case KEY_VENDOR_UNIQUE: 17484 case KEY_COPY_ABORTED: 17485 case KEY_EQUAL: 17486 case KEY_RESERVED: 17487 default: 17488 sd_sense_key_default(un, xp->xb_sense_data, 17489 bp, xp, pktp); 17490 break; 17491 } 17492 } 17493 17494 17495 /* 17496 * Function: sd_dump_memory 17497 * 17498 * Description: Debug logging routine to print the contents of a user provided 17499 * buffer. The output of the buffer is broken up into 256 byte 17500 * segments due to a size constraint of the scsi_log. 17501 * implementation. 17502 * 17503 * Arguments: un - ptr to softstate 17504 * comp - component mask 17505 * title - "title" string to preceed data when printed 17506 * data - ptr to data block to be printed 17507 * len - size of data block to be printed 17508 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17509 * 17510 * Context: May be called from interrupt context 17511 */ 17512 17513 #define SD_DUMP_MEMORY_BUF_SIZE 256 17514 17515 static char *sd_dump_format_string[] = { 17516 " 0x%02x", 17517 " %c" 17518 }; 17519 17520 static void 17521 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17522 int len, int fmt) 17523 { 17524 int i, j; 17525 int avail_count; 17526 int start_offset; 17527 int end_offset; 17528 size_t entry_len; 17529 char *bufp; 17530 char *local_buf; 17531 char *format_string; 17532 17533 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17534 17535 /* 17536 * In the debug version of the driver, this function is called from a 17537 * number of places which are NOPs in the release driver. 17538 * The debug driver therefore has additional methods of filtering 17539 * debug output. 17540 */ 17541 #ifdef SDDEBUG 17542 /* 17543 * In the debug version of the driver we can reduce the amount of debug 17544 * messages by setting sd_error_level to something other than 17545 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17546 * sd_component_mask. 17547 */ 17548 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17549 (sd_error_level != SCSI_ERR_ALL)) { 17550 return; 17551 } 17552 if (((sd_component_mask & comp) == 0) || 17553 (sd_error_level != SCSI_ERR_ALL)) { 17554 return; 17555 } 17556 #else 17557 if (sd_error_level != SCSI_ERR_ALL) { 17558 return; 17559 } 17560 #endif 17561 17562 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17563 bufp = local_buf; 17564 /* 17565 * Available length is the length of local_buf[], minus the 17566 * length of the title string, minus one for the ":", minus 17567 * one for the newline, minus one for the NULL terminator. 17568 * This gives the #bytes available for holding the printed 17569 * values from the given data buffer. 17570 */ 17571 if (fmt == SD_LOG_HEX) { 17572 format_string = sd_dump_format_string[0]; 17573 } else /* SD_LOG_CHAR */ { 17574 format_string = sd_dump_format_string[1]; 17575 } 17576 /* 17577 * Available count is the number of elements from the given 17578 * data buffer that we can fit into the available length. 17579 * This is based upon the size of the format string used. 17580 * Make one entry and find it's size. 17581 */ 17582 (void) sprintf(bufp, format_string, data[0]); 17583 entry_len = strlen(bufp); 17584 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17585 17586 j = 0; 17587 while (j < len) { 17588 bufp = local_buf; 17589 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17590 start_offset = j; 17591 17592 end_offset = start_offset + avail_count; 17593 17594 (void) sprintf(bufp, "%s:", title); 17595 bufp += strlen(bufp); 17596 for (i = start_offset; ((i < end_offset) && (j < len)); 17597 i++, j++) { 17598 (void) sprintf(bufp, format_string, data[i]); 17599 bufp += entry_len; 17600 } 17601 (void) sprintf(bufp, "\n"); 17602 17603 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17604 } 17605 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17606 } 17607 17608 /* 17609 * Function: sd_print_sense_msg 17610 * 17611 * Description: Log a message based upon the given sense data. 17612 * 17613 * Arguments: un - ptr to associated softstate 17614 * bp - ptr to buf(9S) for the command 17615 * arg - ptr to associate sd_sense_info struct 17616 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17617 * or SD_NO_RETRY_ISSUED 17618 * 17619 * Context: May be called from interrupt context 17620 */ 17621 17622 static void 17623 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17624 { 17625 struct sd_xbuf *xp; 17626 struct scsi_pkt *pktp; 17627 uint8_t *sensep; 17628 daddr_t request_blkno; 17629 diskaddr_t err_blkno; 17630 int severity; 17631 int pfa_flag; 17632 extern struct scsi_key_strings scsi_cmds[]; 17633 17634 ASSERT(un != NULL); 17635 ASSERT(mutex_owned(SD_MUTEX(un))); 17636 ASSERT(bp != NULL); 17637 xp = SD_GET_XBUF(bp); 17638 ASSERT(xp != NULL); 17639 pktp = SD_GET_PKTP(bp); 17640 ASSERT(pktp != NULL); 17641 ASSERT(arg != NULL); 17642 17643 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17644 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17645 17646 if ((code == SD_DELAYED_RETRY_ISSUED) || 17647 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17648 severity = SCSI_ERR_RETRYABLE; 17649 } 17650 17651 /* Use absolute block number for the request block number */ 17652 request_blkno = xp->xb_blkno; 17653 17654 /* 17655 * Now try to get the error block number from the sense data 17656 */ 17657 sensep = xp->xb_sense_data; 17658 17659 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17660 (uint64_t *)&err_blkno)) { 17661 /* 17662 * We retrieved the error block number from the information 17663 * portion of the sense data. 17664 * 17665 * For USCSI commands we are better off using the error 17666 * block no. as the requested block no. (This is the best 17667 * we can estimate.) 17668 */ 17669 if ((SD_IS_BUFIO(xp) == FALSE) && 17670 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17671 request_blkno = err_blkno; 17672 } 17673 } else { 17674 /* 17675 * Without the es_valid bit set (for fixed format) or an 17676 * information descriptor (for descriptor format) we cannot 17677 * be certain of the error blkno, so just use the 17678 * request_blkno. 17679 */ 17680 err_blkno = (diskaddr_t)request_blkno; 17681 } 17682 17683 /* 17684 * The following will log the buffer contents for the release driver 17685 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17686 * level is set to verbose. 17687 */ 17688 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17689 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17690 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17691 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17692 17693 if (pfa_flag == FALSE) { 17694 /* This is normally only set for USCSI */ 17695 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17696 return; 17697 } 17698 17699 if ((SD_IS_BUFIO(xp) == TRUE) && 17700 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17701 (severity < sd_error_level))) { 17702 return; 17703 } 17704 } 17705 17706 /* 17707 * Check for Sonoma Failover and keep a count of how many failed I/O's 17708 */ 17709 if ((SD_IS_LSI(un)) && 17710 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17711 (scsi_sense_asc(sensep) == 0x94) && 17712 (scsi_sense_ascq(sensep) == 0x01)) { 17713 un->un_sonoma_failure_count++; 17714 if (un->un_sonoma_failure_count > 1) { 17715 return; 17716 } 17717 } 17718 17719 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17720 request_blkno, err_blkno, scsi_cmds, 17721 (struct scsi_extended_sense *)sensep, 17722 un->un_additional_codes, NULL); 17723 } 17724 17725 /* 17726 * Function: sd_sense_key_no_sense 17727 * 17728 * Description: Recovery action when sense data was not received. 17729 * 17730 * Context: May be called from interrupt context 17731 */ 17732 17733 static void 17734 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17735 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17736 { 17737 struct sd_sense_info si; 17738 17739 ASSERT(un != NULL); 17740 ASSERT(mutex_owned(SD_MUTEX(un))); 17741 ASSERT(bp != NULL); 17742 ASSERT(xp != NULL); 17743 ASSERT(pktp != NULL); 17744 17745 si.ssi_severity = SCSI_ERR_FATAL; 17746 si.ssi_pfa_flag = FALSE; 17747 17748 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17749 17750 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17751 &si, EIO, (clock_t)0, NULL); 17752 } 17753 17754 17755 /* 17756 * Function: sd_sense_key_recoverable_error 17757 * 17758 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17759 * 17760 * Context: May be called from interrupt context 17761 */ 17762 17763 static void 17764 sd_sense_key_recoverable_error(struct sd_lun *un, 17765 uint8_t *sense_datap, 17766 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17767 { 17768 struct sd_sense_info si; 17769 uint8_t asc = scsi_sense_asc(sense_datap); 17770 17771 ASSERT(un != NULL); 17772 ASSERT(mutex_owned(SD_MUTEX(un))); 17773 ASSERT(bp != NULL); 17774 ASSERT(xp != NULL); 17775 ASSERT(pktp != NULL); 17776 17777 /* 17778 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17779 */ 17780 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17781 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17782 si.ssi_severity = SCSI_ERR_INFO; 17783 si.ssi_pfa_flag = TRUE; 17784 } else { 17785 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17786 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17787 si.ssi_severity = SCSI_ERR_RECOVERED; 17788 si.ssi_pfa_flag = FALSE; 17789 } 17790 17791 if (pktp->pkt_resid == 0) { 17792 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17793 sd_return_command(un, bp); 17794 return; 17795 } 17796 17797 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17798 &si, EIO, (clock_t)0, NULL); 17799 } 17800 17801 17802 17803 17804 /* 17805 * Function: sd_sense_key_not_ready 17806 * 17807 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17808 * 17809 * Context: May be called from interrupt context 17810 */ 17811 17812 static void 17813 sd_sense_key_not_ready(struct sd_lun *un, 17814 uint8_t *sense_datap, 17815 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17816 { 17817 struct sd_sense_info si; 17818 uint8_t asc = scsi_sense_asc(sense_datap); 17819 uint8_t ascq = scsi_sense_ascq(sense_datap); 17820 17821 ASSERT(un != NULL); 17822 ASSERT(mutex_owned(SD_MUTEX(un))); 17823 ASSERT(bp != NULL); 17824 ASSERT(xp != NULL); 17825 ASSERT(pktp != NULL); 17826 17827 si.ssi_severity = SCSI_ERR_FATAL; 17828 si.ssi_pfa_flag = FALSE; 17829 17830 /* 17831 * Update error stats after first NOT READY error. Disks may have 17832 * been powered down and may need to be restarted. For CDROMs, 17833 * report NOT READY errors only if media is present. 17834 */ 17835 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17836 (xp->xb_retry_count > 0)) { 17837 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17838 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17839 } 17840 17841 /* 17842 * Just fail if the "not ready" retry limit has been reached. 17843 */ 17844 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17845 /* Special check for error message printing for removables. */ 17846 if (un->un_f_has_removable_media && (asc == 0x04) && 17847 (ascq >= 0x04)) { 17848 si.ssi_severity = SCSI_ERR_ALL; 17849 } 17850 goto fail_command; 17851 } 17852 17853 /* 17854 * Check the ASC and ASCQ in the sense data as needed, to determine 17855 * what to do. 17856 */ 17857 switch (asc) { 17858 case 0x04: /* LOGICAL UNIT NOT READY */ 17859 /* 17860 * disk drives that don't spin up result in a very long delay 17861 * in format without warning messages. We will log a message 17862 * if the error level is set to verbose. 17863 */ 17864 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17865 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17866 "logical unit not ready, resetting disk\n"); 17867 } 17868 17869 /* 17870 * There are different requirements for CDROMs and disks for 17871 * the number of retries. If a CD-ROM is giving this, it is 17872 * probably reading TOC and is in the process of getting 17873 * ready, so we should keep on trying for a long time to make 17874 * sure that all types of media are taken in account (for 17875 * some media the drive takes a long time to read TOC). For 17876 * disks we do not want to retry this too many times as this 17877 * can cause a long hang in format when the drive refuses to 17878 * spin up (a very common failure). 17879 */ 17880 switch (ascq) { 17881 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17882 /* 17883 * Disk drives frequently refuse to spin up which 17884 * results in a very long hang in format without 17885 * warning messages. 17886 * 17887 * Note: This code preserves the legacy behavior of 17888 * comparing xb_retry_count against zero for fibre 17889 * channel targets instead of comparing against the 17890 * un_reset_retry_count value. The reason for this 17891 * discrepancy has been so utterly lost beneath the 17892 * Sands of Time that even Indiana Jones could not 17893 * find it. 17894 */ 17895 if (un->un_f_is_fibre == TRUE) { 17896 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17897 (xp->xb_retry_count > 0)) && 17898 (un->un_startstop_timeid == NULL)) { 17899 scsi_log(SD_DEVINFO(un), sd_label, 17900 CE_WARN, "logical unit not ready, " 17901 "resetting disk\n"); 17902 sd_reset_target(un, pktp); 17903 } 17904 } else { 17905 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17906 (xp->xb_retry_count > 17907 un->un_reset_retry_count)) && 17908 (un->un_startstop_timeid == NULL)) { 17909 scsi_log(SD_DEVINFO(un), sd_label, 17910 CE_WARN, "logical unit not ready, " 17911 "resetting disk\n"); 17912 sd_reset_target(un, pktp); 17913 } 17914 } 17915 break; 17916 17917 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17918 /* 17919 * If the target is in the process of becoming 17920 * ready, just proceed with the retry. This can 17921 * happen with CD-ROMs that take a long time to 17922 * read TOC after a power cycle or reset. 17923 */ 17924 goto do_retry; 17925 17926 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17927 break; 17928 17929 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17930 /* 17931 * Retries cannot help here so just fail right away. 17932 */ 17933 goto fail_command; 17934 17935 case 0x88: 17936 /* 17937 * Vendor-unique code for T3/T4: it indicates a 17938 * path problem in a mutipathed config, but as far as 17939 * the target driver is concerned it equates to a fatal 17940 * error, so we should just fail the command right away 17941 * (without printing anything to the console). If this 17942 * is not a T3/T4, fall thru to the default recovery 17943 * action. 17944 * T3/T4 is FC only, don't need to check is_fibre 17945 */ 17946 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17947 sd_return_failed_command(un, bp, EIO); 17948 return; 17949 } 17950 /* FALLTHRU */ 17951 17952 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17953 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17954 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17955 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17956 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17957 default: /* Possible future codes in SCSI spec? */ 17958 /* 17959 * For removable-media devices, do not retry if 17960 * ASCQ > 2 as these result mostly from USCSI commands 17961 * on MMC devices issued to check status of an 17962 * operation initiated in immediate mode. Also for 17963 * ASCQ >= 4 do not print console messages as these 17964 * mainly represent a user-initiated operation 17965 * instead of a system failure. 17966 */ 17967 if (un->un_f_has_removable_media) { 17968 si.ssi_severity = SCSI_ERR_ALL; 17969 goto fail_command; 17970 } 17971 break; 17972 } 17973 17974 /* 17975 * As part of our recovery attempt for the NOT READY 17976 * condition, we issue a START STOP UNIT command. However 17977 * we want to wait for a short delay before attempting this 17978 * as there may still be more commands coming back from the 17979 * target with the check condition. To do this we use 17980 * timeout(9F) to call sd_start_stop_unit_callback() after 17981 * the delay interval expires. (sd_start_stop_unit_callback() 17982 * dispatches sd_start_stop_unit_task(), which will issue 17983 * the actual START STOP UNIT command. The delay interval 17984 * is one-half of the delay that we will use to retry the 17985 * command that generated the NOT READY condition. 17986 * 17987 * Note that we could just dispatch sd_start_stop_unit_task() 17988 * from here and allow it to sleep for the delay interval, 17989 * but then we would be tying up the taskq thread 17990 * uncesessarily for the duration of the delay. 17991 * 17992 * Do not issue the START STOP UNIT if the current command 17993 * is already a START STOP UNIT. 17994 */ 17995 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 17996 break; 17997 } 17998 17999 /* 18000 * Do not schedule the timeout if one is already pending. 18001 */ 18002 if (un->un_startstop_timeid != NULL) { 18003 SD_INFO(SD_LOG_ERROR, un, 18004 "sd_sense_key_not_ready: restart already issued to" 18005 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18006 ddi_get_instance(SD_DEVINFO(un))); 18007 break; 18008 } 18009 18010 /* 18011 * Schedule the START STOP UNIT command, then queue the command 18012 * for a retry. 18013 * 18014 * Note: A timeout is not scheduled for this retry because we 18015 * want the retry to be serial with the START_STOP_UNIT. The 18016 * retry will be started when the START_STOP_UNIT is completed 18017 * in sd_start_stop_unit_task. 18018 */ 18019 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18020 un, SD_BSY_TIMEOUT / 2); 18021 xp->xb_retry_count++; 18022 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18023 return; 18024 18025 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18026 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18027 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18028 "unit does not respond to selection\n"); 18029 } 18030 break; 18031 18032 case 0x3A: /* MEDIUM NOT PRESENT */ 18033 if (sd_error_level >= SCSI_ERR_FATAL) { 18034 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18035 "Caddy not inserted in drive\n"); 18036 } 18037 18038 sr_ejected(un); 18039 un->un_mediastate = DKIO_EJECTED; 18040 /* The state has changed, inform the media watch routines */ 18041 cv_broadcast(&un->un_state_cv); 18042 /* Just fail if no media is present in the drive. */ 18043 goto fail_command; 18044 18045 default: 18046 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18047 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18048 "Unit not Ready. Additional sense code 0x%x\n", 18049 asc); 18050 } 18051 break; 18052 } 18053 18054 do_retry: 18055 18056 /* 18057 * Retry the command, as some targets may report NOT READY for 18058 * several seconds after being reset. 18059 */ 18060 xp->xb_retry_count++; 18061 si.ssi_severity = SCSI_ERR_RETRYABLE; 18062 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18063 &si, EIO, SD_BSY_TIMEOUT, NULL); 18064 18065 return; 18066 18067 fail_command: 18068 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18069 sd_return_failed_command(un, bp, EIO); 18070 } 18071 18072 18073 18074 /* 18075 * Function: sd_sense_key_medium_or_hardware_error 18076 * 18077 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18078 * sense key. 18079 * 18080 * Context: May be called from interrupt context 18081 */ 18082 18083 static void 18084 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18085 uint8_t *sense_datap, 18086 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18087 { 18088 struct sd_sense_info si; 18089 uint8_t sense_key = scsi_sense_key(sense_datap); 18090 uint8_t asc = scsi_sense_asc(sense_datap); 18091 18092 ASSERT(un != NULL); 18093 ASSERT(mutex_owned(SD_MUTEX(un))); 18094 ASSERT(bp != NULL); 18095 ASSERT(xp != NULL); 18096 ASSERT(pktp != NULL); 18097 18098 si.ssi_severity = SCSI_ERR_FATAL; 18099 si.ssi_pfa_flag = FALSE; 18100 18101 if (sense_key == KEY_MEDIUM_ERROR) { 18102 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18103 } 18104 18105 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18106 18107 if ((un->un_reset_retry_count != 0) && 18108 (xp->xb_retry_count == un->un_reset_retry_count)) { 18109 mutex_exit(SD_MUTEX(un)); 18110 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18111 if (un->un_f_allow_bus_device_reset == TRUE) { 18112 18113 boolean_t try_resetting_target = B_TRUE; 18114 18115 /* 18116 * We need to be able to handle specific ASC when we are 18117 * handling a KEY_HARDWARE_ERROR. In particular 18118 * taking the default action of resetting the target may 18119 * not be the appropriate way to attempt recovery. 18120 * Resetting a target because of a single LUN failure 18121 * victimizes all LUNs on that target. 18122 * 18123 * This is true for the LSI arrays, if an LSI 18124 * array controller returns an ASC of 0x84 (LUN Dead) we 18125 * should trust it. 18126 */ 18127 18128 if (sense_key == KEY_HARDWARE_ERROR) { 18129 switch (asc) { 18130 case 0x84: 18131 if (SD_IS_LSI(un)) { 18132 try_resetting_target = B_FALSE; 18133 } 18134 break; 18135 default: 18136 break; 18137 } 18138 } 18139 18140 if (try_resetting_target == B_TRUE) { 18141 int reset_retval = 0; 18142 if (un->un_f_lun_reset_enabled == TRUE) { 18143 SD_TRACE(SD_LOG_IO_CORE, un, 18144 "sd_sense_key_medium_or_hardware_" 18145 "error: issuing RESET_LUN\n"); 18146 reset_retval = 18147 scsi_reset(SD_ADDRESS(un), 18148 RESET_LUN); 18149 } 18150 if (reset_retval == 0) { 18151 SD_TRACE(SD_LOG_IO_CORE, un, 18152 "sd_sense_key_medium_or_hardware_" 18153 "error: issuing RESET_TARGET\n"); 18154 (void) scsi_reset(SD_ADDRESS(un), 18155 RESET_TARGET); 18156 } 18157 } 18158 } 18159 mutex_enter(SD_MUTEX(un)); 18160 } 18161 18162 /* 18163 * This really ought to be a fatal error, but we will retry anyway 18164 * as some drives report this as a spurious error. 18165 */ 18166 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18167 &si, EIO, (clock_t)0, NULL); 18168 } 18169 18170 18171 18172 /* 18173 * Function: sd_sense_key_illegal_request 18174 * 18175 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18176 * 18177 * Context: May be called from interrupt context 18178 */ 18179 18180 static void 18181 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18182 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18183 { 18184 struct sd_sense_info si; 18185 18186 ASSERT(un != NULL); 18187 ASSERT(mutex_owned(SD_MUTEX(un))); 18188 ASSERT(bp != NULL); 18189 ASSERT(xp != NULL); 18190 ASSERT(pktp != NULL); 18191 18192 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18193 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18194 18195 si.ssi_severity = SCSI_ERR_INFO; 18196 si.ssi_pfa_flag = FALSE; 18197 18198 /* Pointless to retry if the target thinks it's an illegal request */ 18199 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18200 sd_return_failed_command(un, bp, EIO); 18201 } 18202 18203 18204 18205 18206 /* 18207 * Function: sd_sense_key_unit_attention 18208 * 18209 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18210 * 18211 * Context: May be called from interrupt context 18212 */ 18213 18214 static void 18215 sd_sense_key_unit_attention(struct sd_lun *un, 18216 uint8_t *sense_datap, 18217 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18218 { 18219 /* 18220 * For UNIT ATTENTION we allow retries for one minute. Devices 18221 * like Sonoma can return UNIT ATTENTION close to a minute 18222 * under certain conditions. 18223 */ 18224 int retry_check_flag = SD_RETRIES_UA; 18225 boolean_t kstat_updated = B_FALSE; 18226 struct sd_sense_info si; 18227 uint8_t asc = scsi_sense_asc(sense_datap); 18228 18229 ASSERT(un != NULL); 18230 ASSERT(mutex_owned(SD_MUTEX(un))); 18231 ASSERT(bp != NULL); 18232 ASSERT(xp != NULL); 18233 ASSERT(pktp != NULL); 18234 18235 si.ssi_severity = SCSI_ERR_INFO; 18236 si.ssi_pfa_flag = FALSE; 18237 18238 18239 switch (asc) { 18240 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18241 if (sd_report_pfa != 0) { 18242 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18243 si.ssi_pfa_flag = TRUE; 18244 retry_check_flag = SD_RETRIES_STANDARD; 18245 goto do_retry; 18246 } 18247 18248 break; 18249 18250 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18251 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18252 un->un_resvd_status |= 18253 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18254 } 18255 #ifdef _LP64 18256 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18257 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18258 un, KM_NOSLEEP) == 0) { 18259 /* 18260 * If we can't dispatch the task we'll just 18261 * live without descriptor sense. We can 18262 * try again on the next "unit attention" 18263 */ 18264 SD_ERROR(SD_LOG_ERROR, un, 18265 "sd_sense_key_unit_attention: " 18266 "Could not dispatch " 18267 "sd_reenable_dsense_task\n"); 18268 } 18269 } 18270 #endif /* _LP64 */ 18271 /* FALLTHRU */ 18272 18273 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18274 if (!un->un_f_has_removable_media) { 18275 break; 18276 } 18277 18278 /* 18279 * When we get a unit attention from a removable-media device, 18280 * it may be in a state that will take a long time to recover 18281 * (e.g., from a reset). Since we are executing in interrupt 18282 * context here, we cannot wait around for the device to come 18283 * back. So hand this command off to sd_media_change_task() 18284 * for deferred processing under taskq thread context. (Note 18285 * that the command still may be failed if a problem is 18286 * encountered at a later time.) 18287 */ 18288 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18289 KM_NOSLEEP) == 0) { 18290 /* 18291 * Cannot dispatch the request so fail the command. 18292 */ 18293 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18294 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18295 si.ssi_severity = SCSI_ERR_FATAL; 18296 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18297 sd_return_failed_command(un, bp, EIO); 18298 } 18299 18300 /* 18301 * If failed to dispatch sd_media_change_task(), we already 18302 * updated kstat. If succeed to dispatch sd_media_change_task(), 18303 * we should update kstat later if it encounters an error. So, 18304 * we update kstat_updated flag here. 18305 */ 18306 kstat_updated = B_TRUE; 18307 18308 /* 18309 * Either the command has been successfully dispatched to a 18310 * task Q for retrying, or the dispatch failed. In either case 18311 * do NOT retry again by calling sd_retry_command. This sets up 18312 * two retries of the same command and when one completes and 18313 * frees the resources the other will access freed memory, 18314 * a bad thing. 18315 */ 18316 return; 18317 18318 default: 18319 break; 18320 } 18321 18322 /* 18323 * Update kstat if we haven't done that. 18324 */ 18325 if (!kstat_updated) { 18326 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18327 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18328 } 18329 18330 do_retry: 18331 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18332 EIO, SD_UA_RETRY_DELAY, NULL); 18333 } 18334 18335 18336 18337 /* 18338 * Function: sd_sense_key_fail_command 18339 * 18340 * Description: Use to fail a command when we don't like the sense key that 18341 * was returned. 18342 * 18343 * Context: May be called from interrupt context 18344 */ 18345 18346 static void 18347 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18348 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18349 { 18350 struct sd_sense_info si; 18351 18352 ASSERT(un != NULL); 18353 ASSERT(mutex_owned(SD_MUTEX(un))); 18354 ASSERT(bp != NULL); 18355 ASSERT(xp != NULL); 18356 ASSERT(pktp != NULL); 18357 18358 si.ssi_severity = SCSI_ERR_FATAL; 18359 si.ssi_pfa_flag = FALSE; 18360 18361 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18362 sd_return_failed_command(un, bp, EIO); 18363 } 18364 18365 18366 18367 /* 18368 * Function: sd_sense_key_blank_check 18369 * 18370 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18371 * Has no monetary connotation. 18372 * 18373 * Context: May be called from interrupt context 18374 */ 18375 18376 static void 18377 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18378 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18379 { 18380 struct sd_sense_info si; 18381 18382 ASSERT(un != NULL); 18383 ASSERT(mutex_owned(SD_MUTEX(un))); 18384 ASSERT(bp != NULL); 18385 ASSERT(xp != NULL); 18386 ASSERT(pktp != NULL); 18387 18388 /* 18389 * Blank check is not fatal for removable devices, therefore 18390 * it does not require a console message. 18391 */ 18392 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18393 SCSI_ERR_FATAL; 18394 si.ssi_pfa_flag = FALSE; 18395 18396 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18397 sd_return_failed_command(un, bp, EIO); 18398 } 18399 18400 18401 18402 18403 /* 18404 * Function: sd_sense_key_aborted_command 18405 * 18406 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18407 * 18408 * Context: May be called from interrupt context 18409 */ 18410 18411 static void 18412 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18413 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18414 { 18415 struct sd_sense_info si; 18416 18417 ASSERT(un != NULL); 18418 ASSERT(mutex_owned(SD_MUTEX(un))); 18419 ASSERT(bp != NULL); 18420 ASSERT(xp != NULL); 18421 ASSERT(pktp != NULL); 18422 18423 si.ssi_severity = SCSI_ERR_FATAL; 18424 si.ssi_pfa_flag = FALSE; 18425 18426 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18427 18428 /* 18429 * This really ought to be a fatal error, but we will retry anyway 18430 * as some drives report this as a spurious error. 18431 */ 18432 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18433 &si, EIO, (clock_t)0, NULL); 18434 } 18435 18436 18437 18438 /* 18439 * Function: sd_sense_key_default 18440 * 18441 * Description: Default recovery action for several SCSI sense keys (basically 18442 * attempts a retry). 18443 * 18444 * Context: May be called from interrupt context 18445 */ 18446 18447 static void 18448 sd_sense_key_default(struct sd_lun *un, 18449 uint8_t *sense_datap, 18450 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18451 { 18452 struct sd_sense_info si; 18453 uint8_t sense_key = scsi_sense_key(sense_datap); 18454 18455 ASSERT(un != NULL); 18456 ASSERT(mutex_owned(SD_MUTEX(un))); 18457 ASSERT(bp != NULL); 18458 ASSERT(xp != NULL); 18459 ASSERT(pktp != NULL); 18460 18461 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18462 18463 /* 18464 * Undecoded sense key. Attempt retries and hope that will fix 18465 * the problem. Otherwise, we're dead. 18466 */ 18467 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18468 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18469 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18470 } 18471 18472 si.ssi_severity = SCSI_ERR_FATAL; 18473 si.ssi_pfa_flag = FALSE; 18474 18475 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18476 &si, EIO, (clock_t)0, NULL); 18477 } 18478 18479 18480 18481 /* 18482 * Function: sd_print_retry_msg 18483 * 18484 * Description: Print a message indicating the retry action being taken. 18485 * 18486 * Arguments: un - ptr to associated softstate 18487 * bp - ptr to buf(9S) for the command 18488 * arg - not used. 18489 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18490 * or SD_NO_RETRY_ISSUED 18491 * 18492 * Context: May be called from interrupt context 18493 */ 18494 /* ARGSUSED */ 18495 static void 18496 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18497 { 18498 struct sd_xbuf *xp; 18499 struct scsi_pkt *pktp; 18500 char *reasonp; 18501 char *msgp; 18502 18503 ASSERT(un != NULL); 18504 ASSERT(mutex_owned(SD_MUTEX(un))); 18505 ASSERT(bp != NULL); 18506 pktp = SD_GET_PKTP(bp); 18507 ASSERT(pktp != NULL); 18508 xp = SD_GET_XBUF(bp); 18509 ASSERT(xp != NULL); 18510 18511 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18512 mutex_enter(&un->un_pm_mutex); 18513 if ((un->un_state == SD_STATE_SUSPENDED) || 18514 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18515 (pktp->pkt_flags & FLAG_SILENT)) { 18516 mutex_exit(&un->un_pm_mutex); 18517 goto update_pkt_reason; 18518 } 18519 mutex_exit(&un->un_pm_mutex); 18520 18521 /* 18522 * Suppress messages if they are all the same pkt_reason; with 18523 * TQ, many (up to 256) are returned with the same pkt_reason. 18524 * If we are in panic, then suppress the retry messages. 18525 */ 18526 switch (flag) { 18527 case SD_NO_RETRY_ISSUED: 18528 msgp = "giving up"; 18529 break; 18530 case SD_IMMEDIATE_RETRY_ISSUED: 18531 case SD_DELAYED_RETRY_ISSUED: 18532 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18533 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18534 (sd_error_level != SCSI_ERR_ALL))) { 18535 return; 18536 } 18537 msgp = "retrying command"; 18538 break; 18539 default: 18540 goto update_pkt_reason; 18541 } 18542 18543 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18544 scsi_rname(pktp->pkt_reason)); 18545 18546 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18547 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18548 18549 update_pkt_reason: 18550 /* 18551 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18552 * This is to prevent multiple console messages for the same failure 18553 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18554 * when the command is retried successfully because there still may be 18555 * more commands coming back with the same value of pktp->pkt_reason. 18556 */ 18557 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18558 un->un_last_pkt_reason = pktp->pkt_reason; 18559 } 18560 } 18561 18562 18563 /* 18564 * Function: sd_print_cmd_incomplete_msg 18565 * 18566 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18567 * 18568 * Arguments: un - ptr to associated softstate 18569 * bp - ptr to buf(9S) for the command 18570 * arg - passed to sd_print_retry_msg() 18571 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18572 * or SD_NO_RETRY_ISSUED 18573 * 18574 * Context: May be called from interrupt context 18575 */ 18576 18577 static void 18578 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18579 int code) 18580 { 18581 dev_info_t *dip; 18582 18583 ASSERT(un != NULL); 18584 ASSERT(mutex_owned(SD_MUTEX(un))); 18585 ASSERT(bp != NULL); 18586 18587 switch (code) { 18588 case SD_NO_RETRY_ISSUED: 18589 /* Command was failed. Someone turned off this target? */ 18590 if (un->un_state != SD_STATE_OFFLINE) { 18591 /* 18592 * Suppress message if we are detaching and 18593 * device has been disconnected 18594 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18595 * private interface and not part of the DDI 18596 */ 18597 dip = un->un_sd->sd_dev; 18598 if (!(DEVI_IS_DETACHING(dip) && 18599 DEVI_IS_DEVICE_REMOVED(dip))) { 18600 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18601 "disk not responding to selection\n"); 18602 } 18603 New_state(un, SD_STATE_OFFLINE); 18604 } 18605 break; 18606 18607 case SD_DELAYED_RETRY_ISSUED: 18608 case SD_IMMEDIATE_RETRY_ISSUED: 18609 default: 18610 /* Command was successfully queued for retry */ 18611 sd_print_retry_msg(un, bp, arg, code); 18612 break; 18613 } 18614 } 18615 18616 18617 /* 18618 * Function: sd_pkt_reason_cmd_incomplete 18619 * 18620 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18621 * 18622 * Context: May be called from interrupt context 18623 */ 18624 18625 static void 18626 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18627 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18628 { 18629 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18630 18631 ASSERT(un != NULL); 18632 ASSERT(mutex_owned(SD_MUTEX(un))); 18633 ASSERT(bp != NULL); 18634 ASSERT(xp != NULL); 18635 ASSERT(pktp != NULL); 18636 18637 /* Do not do a reset if selection did not complete */ 18638 /* Note: Should this not just check the bit? */ 18639 if (pktp->pkt_state != STATE_GOT_BUS) { 18640 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18641 sd_reset_target(un, pktp); 18642 } 18643 18644 /* 18645 * If the target was not successfully selected, then set 18646 * SD_RETRIES_FAILFAST to indicate that we lost communication 18647 * with the target, and further retries and/or commands are 18648 * likely to take a long time. 18649 */ 18650 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18651 flag |= SD_RETRIES_FAILFAST; 18652 } 18653 18654 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18655 18656 sd_retry_command(un, bp, flag, 18657 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18658 } 18659 18660 18661 18662 /* 18663 * Function: sd_pkt_reason_cmd_tran_err 18664 * 18665 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18666 * 18667 * Context: May be called from interrupt context 18668 */ 18669 18670 static void 18671 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18672 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18673 { 18674 ASSERT(un != NULL); 18675 ASSERT(mutex_owned(SD_MUTEX(un))); 18676 ASSERT(bp != NULL); 18677 ASSERT(xp != NULL); 18678 ASSERT(pktp != NULL); 18679 18680 /* 18681 * Do not reset if we got a parity error, or if 18682 * selection did not complete. 18683 */ 18684 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18685 /* Note: Should this not just check the bit for pkt_state? */ 18686 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18687 (pktp->pkt_state != STATE_GOT_BUS)) { 18688 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18689 sd_reset_target(un, pktp); 18690 } 18691 18692 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18693 18694 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18695 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18696 } 18697 18698 18699 18700 /* 18701 * Function: sd_pkt_reason_cmd_reset 18702 * 18703 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18704 * 18705 * Context: May be called from interrupt context 18706 */ 18707 18708 static void 18709 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18710 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18711 { 18712 ASSERT(un != NULL); 18713 ASSERT(mutex_owned(SD_MUTEX(un))); 18714 ASSERT(bp != NULL); 18715 ASSERT(xp != NULL); 18716 ASSERT(pktp != NULL); 18717 18718 /* The target may still be running the command, so try to reset. */ 18719 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18720 sd_reset_target(un, pktp); 18721 18722 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18723 18724 /* 18725 * If pkt_reason is CMD_RESET chances are that this pkt got 18726 * reset because another target on this bus caused it. The target 18727 * that caused it should get CMD_TIMEOUT with pkt_statistics 18728 * of STAT_TIMEOUT/STAT_DEV_RESET. 18729 */ 18730 18731 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18732 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18733 } 18734 18735 18736 18737 18738 /* 18739 * Function: sd_pkt_reason_cmd_aborted 18740 * 18741 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18742 * 18743 * Context: May be called from interrupt context 18744 */ 18745 18746 static void 18747 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18748 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18749 { 18750 ASSERT(un != NULL); 18751 ASSERT(mutex_owned(SD_MUTEX(un))); 18752 ASSERT(bp != NULL); 18753 ASSERT(xp != NULL); 18754 ASSERT(pktp != NULL); 18755 18756 /* The target may still be running the command, so try to reset. */ 18757 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18758 sd_reset_target(un, pktp); 18759 18760 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18761 18762 /* 18763 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18764 * aborted because another target on this bus caused it. The target 18765 * that caused it should get CMD_TIMEOUT with pkt_statistics 18766 * of STAT_TIMEOUT/STAT_DEV_RESET. 18767 */ 18768 18769 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18770 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18771 } 18772 18773 18774 18775 /* 18776 * Function: sd_pkt_reason_cmd_timeout 18777 * 18778 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18779 * 18780 * Context: May be called from interrupt context 18781 */ 18782 18783 static void 18784 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18785 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18786 { 18787 ASSERT(un != NULL); 18788 ASSERT(mutex_owned(SD_MUTEX(un))); 18789 ASSERT(bp != NULL); 18790 ASSERT(xp != NULL); 18791 ASSERT(pktp != NULL); 18792 18793 18794 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18795 sd_reset_target(un, pktp); 18796 18797 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18798 18799 /* 18800 * A command timeout indicates that we could not establish 18801 * communication with the target, so set SD_RETRIES_FAILFAST 18802 * as further retries/commands are likely to take a long time. 18803 */ 18804 sd_retry_command(un, bp, 18805 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18806 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18807 } 18808 18809 18810 18811 /* 18812 * Function: sd_pkt_reason_cmd_unx_bus_free 18813 * 18814 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18815 * 18816 * Context: May be called from interrupt context 18817 */ 18818 18819 static void 18820 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18821 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18822 { 18823 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18824 18825 ASSERT(un != NULL); 18826 ASSERT(mutex_owned(SD_MUTEX(un))); 18827 ASSERT(bp != NULL); 18828 ASSERT(xp != NULL); 18829 ASSERT(pktp != NULL); 18830 18831 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18832 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18833 18834 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18835 sd_print_retry_msg : NULL; 18836 18837 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18838 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18839 } 18840 18841 18842 /* 18843 * Function: sd_pkt_reason_cmd_tag_reject 18844 * 18845 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18846 * 18847 * Context: May be called from interrupt context 18848 */ 18849 18850 static void 18851 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18852 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18853 { 18854 ASSERT(un != NULL); 18855 ASSERT(mutex_owned(SD_MUTEX(un))); 18856 ASSERT(bp != NULL); 18857 ASSERT(xp != NULL); 18858 ASSERT(pktp != NULL); 18859 18860 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18861 pktp->pkt_flags = 0; 18862 un->un_tagflags = 0; 18863 if (un->un_f_opt_queueing == TRUE) { 18864 un->un_throttle = min(un->un_throttle, 3); 18865 } else { 18866 un->un_throttle = 1; 18867 } 18868 mutex_exit(SD_MUTEX(un)); 18869 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18870 mutex_enter(SD_MUTEX(un)); 18871 18872 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18873 18874 /* Legacy behavior not to check retry counts here. */ 18875 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18876 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18877 } 18878 18879 18880 /* 18881 * Function: sd_pkt_reason_default 18882 * 18883 * Description: Default recovery actions for SCSA pkt_reason values that 18884 * do not have more explicit recovery actions. 18885 * 18886 * Context: May be called from interrupt context 18887 */ 18888 18889 static void 18890 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18891 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18892 { 18893 ASSERT(un != NULL); 18894 ASSERT(mutex_owned(SD_MUTEX(un))); 18895 ASSERT(bp != NULL); 18896 ASSERT(xp != NULL); 18897 ASSERT(pktp != NULL); 18898 18899 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18900 sd_reset_target(un, pktp); 18901 18902 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18903 18904 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18905 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18906 } 18907 18908 18909 18910 /* 18911 * Function: sd_pkt_status_check_condition 18912 * 18913 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18914 * 18915 * Context: May be called from interrupt context 18916 */ 18917 18918 static void 18919 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18920 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18921 { 18922 ASSERT(un != NULL); 18923 ASSERT(mutex_owned(SD_MUTEX(un))); 18924 ASSERT(bp != NULL); 18925 ASSERT(xp != NULL); 18926 ASSERT(pktp != NULL); 18927 18928 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18929 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18930 18931 /* 18932 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18933 * command will be retried after the request sense). Otherwise, retry 18934 * the command. Note: we are issuing the request sense even though the 18935 * retry limit may have been reached for the failed command. 18936 */ 18937 if (un->un_f_arq_enabled == FALSE) { 18938 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18939 "no ARQ, sending request sense command\n"); 18940 sd_send_request_sense_command(un, bp, pktp); 18941 } else { 18942 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18943 "ARQ,retrying request sense command\n"); 18944 #if defined(__i386) || defined(__amd64) 18945 /* 18946 * The SD_RETRY_DELAY value need to be adjusted here 18947 * when SD_RETRY_DELAY change in sddef.h 18948 */ 18949 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18950 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18951 NULL); 18952 #else 18953 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18954 EIO, SD_RETRY_DELAY, NULL); 18955 #endif 18956 } 18957 18958 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18959 } 18960 18961 18962 /* 18963 * Function: sd_pkt_status_busy 18964 * 18965 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18966 * 18967 * Context: May be called from interrupt context 18968 */ 18969 18970 static void 18971 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18972 struct scsi_pkt *pktp) 18973 { 18974 ASSERT(un != NULL); 18975 ASSERT(mutex_owned(SD_MUTEX(un))); 18976 ASSERT(bp != NULL); 18977 ASSERT(xp != NULL); 18978 ASSERT(pktp != NULL); 18979 18980 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18981 "sd_pkt_status_busy: entry\n"); 18982 18983 /* If retries are exhausted, just fail the command. */ 18984 if (xp->xb_retry_count >= un->un_busy_retry_count) { 18985 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18986 "device busy too long\n"); 18987 sd_return_failed_command(un, bp, EIO); 18988 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 18989 "sd_pkt_status_busy: exit\n"); 18990 return; 18991 } 18992 xp->xb_retry_count++; 18993 18994 /* 18995 * Try to reset the target. However, we do not want to perform 18996 * more than one reset if the device continues to fail. The reset 18997 * will be performed when the retry count reaches the reset 18998 * threshold. This threshold should be set such that at least 18999 * one retry is issued before the reset is performed. 19000 */ 19001 if (xp->xb_retry_count == 19002 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19003 int rval = 0; 19004 mutex_exit(SD_MUTEX(un)); 19005 if (un->un_f_allow_bus_device_reset == TRUE) { 19006 /* 19007 * First try to reset the LUN; if we cannot then 19008 * try to reset the target. 19009 */ 19010 if (un->un_f_lun_reset_enabled == TRUE) { 19011 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19012 "sd_pkt_status_busy: RESET_LUN\n"); 19013 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19014 } 19015 if (rval == 0) { 19016 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19017 "sd_pkt_status_busy: RESET_TARGET\n"); 19018 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19019 } 19020 } 19021 if (rval == 0) { 19022 /* 19023 * If the RESET_LUN and/or RESET_TARGET failed, 19024 * try RESET_ALL 19025 */ 19026 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19027 "sd_pkt_status_busy: RESET_ALL\n"); 19028 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19029 } 19030 mutex_enter(SD_MUTEX(un)); 19031 if (rval == 0) { 19032 /* 19033 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19034 * At this point we give up & fail the command. 19035 */ 19036 sd_return_failed_command(un, bp, EIO); 19037 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19038 "sd_pkt_status_busy: exit (failed cmd)\n"); 19039 return; 19040 } 19041 } 19042 19043 /* 19044 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19045 * we have already checked the retry counts above. 19046 */ 19047 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19048 EIO, SD_BSY_TIMEOUT, NULL); 19049 19050 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19051 "sd_pkt_status_busy: exit\n"); 19052 } 19053 19054 19055 /* 19056 * Function: sd_pkt_status_reservation_conflict 19057 * 19058 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19059 * command status. 19060 * 19061 * Context: May be called from interrupt context 19062 */ 19063 19064 static void 19065 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19066 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19067 { 19068 ASSERT(un != NULL); 19069 ASSERT(mutex_owned(SD_MUTEX(un))); 19070 ASSERT(bp != NULL); 19071 ASSERT(xp != NULL); 19072 ASSERT(pktp != NULL); 19073 19074 /* 19075 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19076 * conflict could be due to various reasons like incorrect keys, not 19077 * registered or not reserved etc. So, we return EACCES to the caller. 19078 */ 19079 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19080 int cmd = SD_GET_PKT_OPCODE(pktp); 19081 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19082 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19083 sd_return_failed_command(un, bp, EACCES); 19084 return; 19085 } 19086 } 19087 19088 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19089 19090 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19091 if (sd_failfast_enable != 0) { 19092 /* By definition, we must panic here.... */ 19093 sd_panic_for_res_conflict(un); 19094 /*NOTREACHED*/ 19095 } 19096 SD_ERROR(SD_LOG_IO, un, 19097 "sd_handle_resv_conflict: Disk Reserved\n"); 19098 sd_return_failed_command(un, bp, EACCES); 19099 return; 19100 } 19101 19102 /* 19103 * 1147670: retry only if sd_retry_on_reservation_conflict 19104 * property is set (default is 1). Retries will not succeed 19105 * on a disk reserved by another initiator. HA systems 19106 * may reset this via sd.conf to avoid these retries. 19107 * 19108 * Note: The legacy return code for this failure is EIO, however EACCES 19109 * seems more appropriate for a reservation conflict. 19110 */ 19111 if (sd_retry_on_reservation_conflict == 0) { 19112 SD_ERROR(SD_LOG_IO, un, 19113 "sd_handle_resv_conflict: Device Reserved\n"); 19114 sd_return_failed_command(un, bp, EIO); 19115 return; 19116 } 19117 19118 /* 19119 * Retry the command if we can. 19120 * 19121 * Note: The legacy return code for this failure is EIO, however EACCES 19122 * seems more appropriate for a reservation conflict. 19123 */ 19124 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19125 (clock_t)2, NULL); 19126 } 19127 19128 19129 19130 /* 19131 * Function: sd_pkt_status_qfull 19132 * 19133 * Description: Handle a QUEUE FULL condition from the target. This can 19134 * occur if the HBA does not handle the queue full condition. 19135 * (Basically this means third-party HBAs as Sun HBAs will 19136 * handle the queue full condition.) Note that if there are 19137 * some commands already in the transport, then the queue full 19138 * has occurred because the queue for this nexus is actually 19139 * full. If there are no commands in the transport, then the 19140 * queue full is resulting from some other initiator or lun 19141 * consuming all the resources at the target. 19142 * 19143 * Context: May be called from interrupt context 19144 */ 19145 19146 static void 19147 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19148 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19149 { 19150 ASSERT(un != NULL); 19151 ASSERT(mutex_owned(SD_MUTEX(un))); 19152 ASSERT(bp != NULL); 19153 ASSERT(xp != NULL); 19154 ASSERT(pktp != NULL); 19155 19156 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19157 "sd_pkt_status_qfull: entry\n"); 19158 19159 /* 19160 * Just lower the QFULL throttle and retry the command. Note that 19161 * we do not limit the number of retries here. 19162 */ 19163 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19164 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19165 SD_RESTART_TIMEOUT, NULL); 19166 19167 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19168 "sd_pkt_status_qfull: exit\n"); 19169 } 19170 19171 19172 /* 19173 * Function: sd_reset_target 19174 * 19175 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19176 * RESET_TARGET, or RESET_ALL. 19177 * 19178 * Context: May be called under interrupt context. 19179 */ 19180 19181 static void 19182 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19183 { 19184 int rval = 0; 19185 19186 ASSERT(un != NULL); 19187 ASSERT(mutex_owned(SD_MUTEX(un))); 19188 ASSERT(pktp != NULL); 19189 19190 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19191 19192 /* 19193 * No need to reset if the transport layer has already done so. 19194 */ 19195 if ((pktp->pkt_statistics & 19196 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19197 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19198 "sd_reset_target: no reset\n"); 19199 return; 19200 } 19201 19202 mutex_exit(SD_MUTEX(un)); 19203 19204 if (un->un_f_allow_bus_device_reset == TRUE) { 19205 if (un->un_f_lun_reset_enabled == TRUE) { 19206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19207 "sd_reset_target: RESET_LUN\n"); 19208 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19209 } 19210 if (rval == 0) { 19211 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19212 "sd_reset_target: RESET_TARGET\n"); 19213 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19214 } 19215 } 19216 19217 if (rval == 0) { 19218 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19219 "sd_reset_target: RESET_ALL\n"); 19220 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19221 } 19222 19223 mutex_enter(SD_MUTEX(un)); 19224 19225 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19226 } 19227 19228 19229 /* 19230 * Function: sd_media_change_task 19231 * 19232 * Description: Recovery action for CDROM to become available. 19233 * 19234 * Context: Executes in a taskq() thread context 19235 */ 19236 19237 static void 19238 sd_media_change_task(void *arg) 19239 { 19240 struct scsi_pkt *pktp = arg; 19241 struct sd_lun *un; 19242 struct buf *bp; 19243 struct sd_xbuf *xp; 19244 int err = 0; 19245 int retry_count = 0; 19246 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19247 struct sd_sense_info si; 19248 19249 ASSERT(pktp != NULL); 19250 bp = (struct buf *)pktp->pkt_private; 19251 ASSERT(bp != NULL); 19252 xp = SD_GET_XBUF(bp); 19253 ASSERT(xp != NULL); 19254 un = SD_GET_UN(bp); 19255 ASSERT(un != NULL); 19256 ASSERT(!mutex_owned(SD_MUTEX(un))); 19257 ASSERT(un->un_f_monitor_media_state); 19258 19259 si.ssi_severity = SCSI_ERR_INFO; 19260 si.ssi_pfa_flag = FALSE; 19261 19262 /* 19263 * When a reset is issued on a CDROM, it takes a long time to 19264 * recover. First few attempts to read capacity and other things 19265 * related to handling unit attention fail (with a ASC 0x4 and 19266 * ASCQ 0x1). In that case we want to do enough retries and we want 19267 * to limit the retries in other cases of genuine failures like 19268 * no media in drive. 19269 */ 19270 while (retry_count++ < retry_limit) { 19271 if ((err = sd_handle_mchange(un)) == 0) { 19272 break; 19273 } 19274 if (err == EAGAIN) { 19275 retry_limit = SD_UNIT_ATTENTION_RETRY; 19276 } 19277 /* Sleep for 0.5 sec. & try again */ 19278 delay(drv_usectohz(500000)); 19279 } 19280 19281 /* 19282 * Dispatch (retry or fail) the original command here, 19283 * along with appropriate console messages.... 19284 * 19285 * Must grab the mutex before calling sd_retry_command, 19286 * sd_print_sense_msg and sd_return_failed_command. 19287 */ 19288 mutex_enter(SD_MUTEX(un)); 19289 if (err != SD_CMD_SUCCESS) { 19290 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19291 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19292 si.ssi_severity = SCSI_ERR_FATAL; 19293 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19294 sd_return_failed_command(un, bp, EIO); 19295 } else { 19296 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19297 &si, EIO, (clock_t)0, NULL); 19298 } 19299 mutex_exit(SD_MUTEX(un)); 19300 } 19301 19302 19303 19304 /* 19305 * Function: sd_handle_mchange 19306 * 19307 * Description: Perform geometry validation & other recovery when CDROM 19308 * has been removed from drive. 19309 * 19310 * Return Code: 0 for success 19311 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19312 * sd_send_scsi_READ_CAPACITY() 19313 * 19314 * Context: Executes in a taskq() thread context 19315 */ 19316 19317 static int 19318 sd_handle_mchange(struct sd_lun *un) 19319 { 19320 uint64_t capacity; 19321 uint32_t lbasize; 19322 int rval; 19323 19324 ASSERT(!mutex_owned(SD_MUTEX(un))); 19325 ASSERT(un->un_f_monitor_media_state); 19326 19327 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 19328 SD_PATH_DIRECT_PRIORITY)) != 0) { 19329 return (rval); 19330 } 19331 19332 mutex_enter(SD_MUTEX(un)); 19333 sd_update_block_info(un, lbasize, capacity); 19334 19335 if (un->un_errstats != NULL) { 19336 struct sd_errstats *stp = 19337 (struct sd_errstats *)un->un_errstats->ks_data; 19338 stp->sd_capacity.value.ui64 = (uint64_t) 19339 ((uint64_t)un->un_blockcount * 19340 (uint64_t)un->un_tgt_blocksize); 19341 } 19342 19343 /* 19344 * Note: Maybe let the strategy/partitioning chain worry about getting 19345 * valid geometry. 19346 */ 19347 un->un_f_geometry_is_valid = FALSE; 19348 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 19349 if (un->un_f_geometry_is_valid == FALSE) { 19350 mutex_exit(SD_MUTEX(un)); 19351 return (EIO); 19352 } 19353 19354 mutex_exit(SD_MUTEX(un)); 19355 19356 /* 19357 * Try to lock the door 19358 */ 19359 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19360 SD_PATH_DIRECT_PRIORITY)); 19361 } 19362 19363 19364 /* 19365 * Function: sd_send_scsi_DOORLOCK 19366 * 19367 * Description: Issue the scsi DOOR LOCK command 19368 * 19369 * Arguments: un - pointer to driver soft state (unit) structure for 19370 * this target. 19371 * flag - SD_REMOVAL_ALLOW 19372 * SD_REMOVAL_PREVENT 19373 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19374 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19375 * to use the USCSI "direct" chain and bypass the normal 19376 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19377 * command is issued as part of an error recovery action. 19378 * 19379 * Return Code: 0 - Success 19380 * errno return code from sd_send_scsi_cmd() 19381 * 19382 * Context: Can sleep. 19383 */ 19384 19385 static int 19386 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19387 { 19388 union scsi_cdb cdb; 19389 struct uscsi_cmd ucmd_buf; 19390 struct scsi_extended_sense sense_buf; 19391 int status; 19392 19393 ASSERT(un != NULL); 19394 ASSERT(!mutex_owned(SD_MUTEX(un))); 19395 19396 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19397 19398 /* already determined doorlock is not supported, fake success */ 19399 if (un->un_f_doorlock_supported == FALSE) { 19400 return (0); 19401 } 19402 19403 bzero(&cdb, sizeof (cdb)); 19404 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19405 19406 cdb.scc_cmd = SCMD_DOORLOCK; 19407 cdb.cdb_opaque[4] = (uchar_t)flag; 19408 19409 ucmd_buf.uscsi_cdb = (char *)&cdb; 19410 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19411 ucmd_buf.uscsi_bufaddr = NULL; 19412 ucmd_buf.uscsi_buflen = 0; 19413 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19414 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19415 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19416 ucmd_buf.uscsi_timeout = 15; 19417 19418 SD_TRACE(SD_LOG_IO, un, 19419 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19420 19421 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19422 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19423 19424 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19425 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19426 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19427 /* fake success and skip subsequent doorlock commands */ 19428 un->un_f_doorlock_supported = FALSE; 19429 return (0); 19430 } 19431 19432 return (status); 19433 } 19434 19435 /* 19436 * Function: sd_send_scsi_READ_CAPACITY 19437 * 19438 * Description: This routine uses the scsi READ CAPACITY command to determine 19439 * the device capacity in number of blocks and the device native 19440 * block size. If this function returns a failure, then the 19441 * values in *capp and *lbap are undefined. If the capacity 19442 * returned is 0xffffffff then the lun is too large for a 19443 * normal READ CAPACITY command and the results of a 19444 * READ CAPACITY 16 will be used instead. 19445 * 19446 * Arguments: un - ptr to soft state struct for the target 19447 * capp - ptr to unsigned 64-bit variable to receive the 19448 * capacity value from the command. 19449 * lbap - ptr to unsigned 32-bit varaible to receive the 19450 * block size value from the command 19451 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19452 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19453 * to use the USCSI "direct" chain and bypass the normal 19454 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19455 * command is issued as part of an error recovery action. 19456 * 19457 * Return Code: 0 - Success 19458 * EIO - IO error 19459 * EACCES - Reservation conflict detected 19460 * EAGAIN - Device is becoming ready 19461 * errno return code from sd_send_scsi_cmd() 19462 * 19463 * Context: Can sleep. Blocks until command completes. 19464 */ 19465 19466 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19467 19468 static int 19469 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19470 int path_flag) 19471 { 19472 struct scsi_extended_sense sense_buf; 19473 struct uscsi_cmd ucmd_buf; 19474 union scsi_cdb cdb; 19475 uint32_t *capacity_buf; 19476 uint64_t capacity; 19477 uint32_t lbasize; 19478 int status; 19479 19480 ASSERT(un != NULL); 19481 ASSERT(!mutex_owned(SD_MUTEX(un))); 19482 ASSERT(capp != NULL); 19483 ASSERT(lbap != NULL); 19484 19485 SD_TRACE(SD_LOG_IO, un, 19486 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19487 19488 /* 19489 * First send a READ_CAPACITY command to the target. 19490 * (This command is mandatory under SCSI-2.) 19491 * 19492 * Set up the CDB for the READ_CAPACITY command. The Partial 19493 * Medium Indicator bit is cleared. The address field must be 19494 * zero if the PMI bit is zero. 19495 */ 19496 bzero(&cdb, sizeof (cdb)); 19497 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19498 19499 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19500 19501 cdb.scc_cmd = SCMD_READ_CAPACITY; 19502 19503 ucmd_buf.uscsi_cdb = (char *)&cdb; 19504 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19505 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19506 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19507 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19508 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19509 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19510 ucmd_buf.uscsi_timeout = 60; 19511 19512 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19513 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19514 19515 switch (status) { 19516 case 0: 19517 /* Return failure if we did not get valid capacity data. */ 19518 if (ucmd_buf.uscsi_resid != 0) { 19519 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19520 return (EIO); 19521 } 19522 19523 /* 19524 * Read capacity and block size from the READ CAPACITY 10 data. 19525 * This data may be adjusted later due to device specific 19526 * issues. 19527 * 19528 * According to the SCSI spec, the READ CAPACITY 10 19529 * command returns the following: 19530 * 19531 * bytes 0-3: Maximum logical block address available. 19532 * (MSB in byte:0 & LSB in byte:3) 19533 * 19534 * bytes 4-7: Block length in bytes 19535 * (MSB in byte:4 & LSB in byte:7) 19536 * 19537 */ 19538 capacity = BE_32(capacity_buf[0]); 19539 lbasize = BE_32(capacity_buf[1]); 19540 19541 /* 19542 * Done with capacity_buf 19543 */ 19544 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19545 19546 /* 19547 * if the reported capacity is set to all 0xf's, then 19548 * this disk is too large and requires SBC-2 commands. 19549 * Reissue the request using READ CAPACITY 16. 19550 */ 19551 if (capacity == 0xffffffff) { 19552 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19553 &lbasize, path_flag); 19554 if (status != 0) { 19555 return (status); 19556 } 19557 } 19558 break; /* Success! */ 19559 case EIO: 19560 switch (ucmd_buf.uscsi_status) { 19561 case STATUS_RESERVATION_CONFLICT: 19562 status = EACCES; 19563 break; 19564 case STATUS_CHECK: 19565 /* 19566 * Check condition; look for ASC/ASCQ of 0x04/0x01 19567 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19568 */ 19569 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19570 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19571 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19572 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19573 return (EAGAIN); 19574 } 19575 break; 19576 default: 19577 break; 19578 } 19579 /* FALLTHRU */ 19580 default: 19581 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19582 return (status); 19583 } 19584 19585 /* 19586 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19587 * (2352 and 0 are common) so for these devices always force the value 19588 * to 2048 as required by the ATAPI specs. 19589 */ 19590 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19591 lbasize = 2048; 19592 } 19593 19594 /* 19595 * Get the maximum LBA value from the READ CAPACITY data. 19596 * Here we assume that the Partial Medium Indicator (PMI) bit 19597 * was cleared when issuing the command. This means that the LBA 19598 * returned from the device is the LBA of the last logical block 19599 * on the logical unit. The actual logical block count will be 19600 * this value plus one. 19601 * 19602 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19603 * so scale the capacity value to reflect this. 19604 */ 19605 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19606 19607 #if defined(__i386) || defined(__amd64) 19608 /* 19609 * Refer to comments related to off-by-1 at the 19610 * header of this file. 19611 * Treat 1TB disk as (1T - 512)B. 19612 */ 19613 if (un->un_f_capacity_adjusted == 1) 19614 capacity = DK_MAX_BLOCKS; 19615 #endif 19616 19617 /* 19618 * Copy the values from the READ CAPACITY command into the space 19619 * provided by the caller. 19620 */ 19621 *capp = capacity; 19622 *lbap = lbasize; 19623 19624 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19625 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19626 19627 /* 19628 * Both the lbasize and capacity from the device must be nonzero, 19629 * otherwise we assume that the values are not valid and return 19630 * failure to the caller. (4203735) 19631 */ 19632 if ((capacity == 0) || (lbasize == 0)) { 19633 return (EIO); 19634 } 19635 19636 return (0); 19637 } 19638 19639 /* 19640 * Function: sd_send_scsi_READ_CAPACITY_16 19641 * 19642 * Description: This routine uses the scsi READ CAPACITY 16 command to 19643 * determine the device capacity in number of blocks and the 19644 * device native block size. If this function returns a failure, 19645 * then the values in *capp and *lbap are undefined. 19646 * This routine should always be called by 19647 * sd_send_scsi_READ_CAPACITY which will appy any device 19648 * specific adjustments to capacity and lbasize. 19649 * 19650 * Arguments: un - ptr to soft state struct for the target 19651 * capp - ptr to unsigned 64-bit variable to receive the 19652 * capacity value from the command. 19653 * lbap - ptr to unsigned 32-bit varaible to receive the 19654 * block size value from the command 19655 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19656 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19657 * to use the USCSI "direct" chain and bypass the normal 19658 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19659 * this command is issued as part of an error recovery 19660 * action. 19661 * 19662 * Return Code: 0 - Success 19663 * EIO - IO error 19664 * EACCES - Reservation conflict detected 19665 * EAGAIN - Device is becoming ready 19666 * errno return code from sd_send_scsi_cmd() 19667 * 19668 * Context: Can sleep. Blocks until command completes. 19669 */ 19670 19671 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19672 19673 static int 19674 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19675 uint32_t *lbap, int path_flag) 19676 { 19677 struct scsi_extended_sense sense_buf; 19678 struct uscsi_cmd ucmd_buf; 19679 union scsi_cdb cdb; 19680 uint64_t *capacity16_buf; 19681 uint64_t capacity; 19682 uint32_t lbasize; 19683 int status; 19684 19685 ASSERT(un != NULL); 19686 ASSERT(!mutex_owned(SD_MUTEX(un))); 19687 ASSERT(capp != NULL); 19688 ASSERT(lbap != NULL); 19689 19690 SD_TRACE(SD_LOG_IO, un, 19691 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19692 19693 /* 19694 * First send a READ_CAPACITY_16 command to the target. 19695 * 19696 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19697 * Medium Indicator bit is cleared. The address field must be 19698 * zero if the PMI bit is zero. 19699 */ 19700 bzero(&cdb, sizeof (cdb)); 19701 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19702 19703 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19704 19705 ucmd_buf.uscsi_cdb = (char *)&cdb; 19706 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19707 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19708 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19709 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19710 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19711 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19712 ucmd_buf.uscsi_timeout = 60; 19713 19714 /* 19715 * Read Capacity (16) is a Service Action In command. One 19716 * command byte (0x9E) is overloaded for multiple operations, 19717 * with the second CDB byte specifying the desired operation 19718 */ 19719 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19720 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19721 19722 /* 19723 * Fill in allocation length field 19724 */ 19725 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19726 19727 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19728 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19729 19730 switch (status) { 19731 case 0: 19732 /* Return failure if we did not get valid capacity data. */ 19733 if (ucmd_buf.uscsi_resid > 20) { 19734 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19735 return (EIO); 19736 } 19737 19738 /* 19739 * Read capacity and block size from the READ CAPACITY 10 data. 19740 * This data may be adjusted later due to device specific 19741 * issues. 19742 * 19743 * According to the SCSI spec, the READ CAPACITY 10 19744 * command returns the following: 19745 * 19746 * bytes 0-7: Maximum logical block address available. 19747 * (MSB in byte:0 & LSB in byte:7) 19748 * 19749 * bytes 8-11: Block length in bytes 19750 * (MSB in byte:8 & LSB in byte:11) 19751 * 19752 */ 19753 capacity = BE_64(capacity16_buf[0]); 19754 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19755 19756 /* 19757 * Done with capacity16_buf 19758 */ 19759 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19760 19761 /* 19762 * if the reported capacity is set to all 0xf's, then 19763 * this disk is too large. This could only happen with 19764 * a device that supports LBAs larger than 64 bits which 19765 * are not defined by any current T10 standards. 19766 */ 19767 if (capacity == 0xffffffffffffffff) { 19768 return (EIO); 19769 } 19770 break; /* Success! */ 19771 case EIO: 19772 switch (ucmd_buf.uscsi_status) { 19773 case STATUS_RESERVATION_CONFLICT: 19774 status = EACCES; 19775 break; 19776 case STATUS_CHECK: 19777 /* 19778 * Check condition; look for ASC/ASCQ of 0x04/0x01 19779 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19780 */ 19781 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19782 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19783 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19784 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19785 return (EAGAIN); 19786 } 19787 break; 19788 default: 19789 break; 19790 } 19791 /* FALLTHRU */ 19792 default: 19793 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19794 return (status); 19795 } 19796 19797 *capp = capacity; 19798 *lbap = lbasize; 19799 19800 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19801 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19802 19803 return (0); 19804 } 19805 19806 19807 /* 19808 * Function: sd_send_scsi_START_STOP_UNIT 19809 * 19810 * Description: Issue a scsi START STOP UNIT command to the target. 19811 * 19812 * Arguments: un - pointer to driver soft state (unit) structure for 19813 * this target. 19814 * flag - SD_TARGET_START 19815 * SD_TARGET_STOP 19816 * SD_TARGET_EJECT 19817 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19818 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19819 * to use the USCSI "direct" chain and bypass the normal 19820 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19821 * command is issued as part of an error recovery action. 19822 * 19823 * Return Code: 0 - Success 19824 * EIO - IO error 19825 * EACCES - Reservation conflict detected 19826 * ENXIO - Not Ready, medium not present 19827 * errno return code from sd_send_scsi_cmd() 19828 * 19829 * Context: Can sleep. 19830 */ 19831 19832 static int 19833 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19834 { 19835 struct scsi_extended_sense sense_buf; 19836 union scsi_cdb cdb; 19837 struct uscsi_cmd ucmd_buf; 19838 int status; 19839 19840 ASSERT(un != NULL); 19841 ASSERT(!mutex_owned(SD_MUTEX(un))); 19842 19843 SD_TRACE(SD_LOG_IO, un, 19844 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19845 19846 if (un->un_f_check_start_stop && 19847 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19848 (un->un_f_start_stop_supported != TRUE)) { 19849 return (0); 19850 } 19851 19852 bzero(&cdb, sizeof (cdb)); 19853 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19854 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19855 19856 cdb.scc_cmd = SCMD_START_STOP; 19857 cdb.cdb_opaque[4] = (uchar_t)flag; 19858 19859 ucmd_buf.uscsi_cdb = (char *)&cdb; 19860 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19861 ucmd_buf.uscsi_bufaddr = NULL; 19862 ucmd_buf.uscsi_buflen = 0; 19863 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19864 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19865 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19866 ucmd_buf.uscsi_timeout = 200; 19867 19868 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19869 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19870 19871 switch (status) { 19872 case 0: 19873 break; /* Success! */ 19874 case EIO: 19875 switch (ucmd_buf.uscsi_status) { 19876 case STATUS_RESERVATION_CONFLICT: 19877 status = EACCES; 19878 break; 19879 case STATUS_CHECK: 19880 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19881 switch (scsi_sense_key( 19882 (uint8_t *)&sense_buf)) { 19883 case KEY_ILLEGAL_REQUEST: 19884 status = ENOTSUP; 19885 break; 19886 case KEY_NOT_READY: 19887 if (scsi_sense_asc( 19888 (uint8_t *)&sense_buf) 19889 == 0x3A) { 19890 status = ENXIO; 19891 } 19892 break; 19893 default: 19894 break; 19895 } 19896 } 19897 break; 19898 default: 19899 break; 19900 } 19901 break; 19902 default: 19903 break; 19904 } 19905 19906 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19907 19908 return (status); 19909 } 19910 19911 19912 /* 19913 * Function: sd_start_stop_unit_callback 19914 * 19915 * Description: timeout(9F) callback to begin recovery process for a 19916 * device that has spun down. 19917 * 19918 * Arguments: arg - pointer to associated softstate struct. 19919 * 19920 * Context: Executes in a timeout(9F) thread context 19921 */ 19922 19923 static void 19924 sd_start_stop_unit_callback(void *arg) 19925 { 19926 struct sd_lun *un = arg; 19927 ASSERT(un != NULL); 19928 ASSERT(!mutex_owned(SD_MUTEX(un))); 19929 19930 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19931 19932 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19933 } 19934 19935 19936 /* 19937 * Function: sd_start_stop_unit_task 19938 * 19939 * Description: Recovery procedure when a drive is spun down. 19940 * 19941 * Arguments: arg - pointer to associated softstate struct. 19942 * 19943 * Context: Executes in a taskq() thread context 19944 */ 19945 19946 static void 19947 sd_start_stop_unit_task(void *arg) 19948 { 19949 struct sd_lun *un = arg; 19950 19951 ASSERT(un != NULL); 19952 ASSERT(!mutex_owned(SD_MUTEX(un))); 19953 19954 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19955 19956 /* 19957 * Some unformatted drives report not ready error, no need to 19958 * restart if format has been initiated. 19959 */ 19960 mutex_enter(SD_MUTEX(un)); 19961 if (un->un_f_format_in_progress == TRUE) { 19962 mutex_exit(SD_MUTEX(un)); 19963 return; 19964 } 19965 mutex_exit(SD_MUTEX(un)); 19966 19967 /* 19968 * When a START STOP command is issued from here, it is part of a 19969 * failure recovery operation and must be issued before any other 19970 * commands, including any pending retries. Thus it must be sent 19971 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19972 * succeeds or not, we will start I/O after the attempt. 19973 */ 19974 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19975 SD_PATH_DIRECT_PRIORITY); 19976 19977 /* 19978 * The above call blocks until the START_STOP_UNIT command completes. 19979 * Now that it has completed, we must re-try the original IO that 19980 * received the NOT READY condition in the first place. There are 19981 * three possible conditions here: 19982 * 19983 * (1) The original IO is on un_retry_bp. 19984 * (2) The original IO is on the regular wait queue, and un_retry_bp 19985 * is NULL. 19986 * (3) The original IO is on the regular wait queue, and un_retry_bp 19987 * points to some other, unrelated bp. 19988 * 19989 * For each case, we must call sd_start_cmds() with un_retry_bp 19990 * as the argument. If un_retry_bp is NULL, this will initiate 19991 * processing of the regular wait queue. If un_retry_bp is not NULL, 19992 * then this will process the bp on un_retry_bp. That may or may not 19993 * be the original IO, but that does not matter: the important thing 19994 * is to keep the IO processing going at this point. 19995 * 19996 * Note: This is a very specific error recovery sequence associated 19997 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 19998 * serialize the I/O with completion of the spin-up. 19999 */ 20000 mutex_enter(SD_MUTEX(un)); 20001 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20002 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20003 un, un->un_retry_bp); 20004 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20005 sd_start_cmds(un, un->un_retry_bp); 20006 mutex_exit(SD_MUTEX(un)); 20007 20008 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20009 } 20010 20011 20012 /* 20013 * Function: sd_send_scsi_INQUIRY 20014 * 20015 * Description: Issue the scsi INQUIRY command. 20016 * 20017 * Arguments: un 20018 * bufaddr 20019 * buflen 20020 * evpd 20021 * page_code 20022 * page_length 20023 * 20024 * Return Code: 0 - Success 20025 * errno return code from sd_send_scsi_cmd() 20026 * 20027 * Context: Can sleep. Does not return until command is completed. 20028 */ 20029 20030 static int 20031 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 20032 uchar_t evpd, uchar_t page_code, size_t *residp) 20033 { 20034 union scsi_cdb cdb; 20035 struct uscsi_cmd ucmd_buf; 20036 int status; 20037 20038 ASSERT(un != NULL); 20039 ASSERT(!mutex_owned(SD_MUTEX(un))); 20040 ASSERT(bufaddr != NULL); 20041 20042 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20043 20044 bzero(&cdb, sizeof (cdb)); 20045 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20046 bzero(bufaddr, buflen); 20047 20048 cdb.scc_cmd = SCMD_INQUIRY; 20049 cdb.cdb_opaque[1] = evpd; 20050 cdb.cdb_opaque[2] = page_code; 20051 FORMG0COUNT(&cdb, buflen); 20052 20053 ucmd_buf.uscsi_cdb = (char *)&cdb; 20054 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20055 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20056 ucmd_buf.uscsi_buflen = buflen; 20057 ucmd_buf.uscsi_rqbuf = NULL; 20058 ucmd_buf.uscsi_rqlen = 0; 20059 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20060 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20061 20062 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20063 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 20064 20065 if ((status == 0) && (residp != NULL)) { 20066 *residp = ucmd_buf.uscsi_resid; 20067 } 20068 20069 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20070 20071 return (status); 20072 } 20073 20074 20075 /* 20076 * Function: sd_send_scsi_TEST_UNIT_READY 20077 * 20078 * Description: Issue the scsi TEST UNIT READY command. 20079 * This routine can be told to set the flag USCSI_DIAGNOSE to 20080 * prevent retrying failed commands. Use this when the intent 20081 * is either to check for device readiness, to clear a Unit 20082 * Attention, or to clear any outstanding sense data. 20083 * However under specific conditions the expected behavior 20084 * is for retries to bring a device ready, so use the flag 20085 * with caution. 20086 * 20087 * Arguments: un 20088 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20089 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20090 * 0: dont check for media present, do retries on cmd. 20091 * 20092 * Return Code: 0 - Success 20093 * EIO - IO error 20094 * EACCES - Reservation conflict detected 20095 * ENXIO - Not Ready, medium not present 20096 * errno return code from sd_send_scsi_cmd() 20097 * 20098 * Context: Can sleep. Does not return until command is completed. 20099 */ 20100 20101 static int 20102 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 20103 { 20104 struct scsi_extended_sense sense_buf; 20105 union scsi_cdb cdb; 20106 struct uscsi_cmd ucmd_buf; 20107 int status; 20108 20109 ASSERT(un != NULL); 20110 ASSERT(!mutex_owned(SD_MUTEX(un))); 20111 20112 SD_TRACE(SD_LOG_IO, un, 20113 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20114 20115 /* 20116 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20117 * timeouts when they receive a TUR and the queue is not empty. Check 20118 * the configuration flag set during attach (indicating the drive has 20119 * this firmware bug) and un_ncmds_in_transport before issuing the 20120 * TUR. If there are 20121 * pending commands return success, this is a bit arbitrary but is ok 20122 * for non-removables (i.e. the eliteI disks) and non-clustering 20123 * configurations. 20124 */ 20125 if (un->un_f_cfg_tur_check == TRUE) { 20126 mutex_enter(SD_MUTEX(un)); 20127 if (un->un_ncmds_in_transport != 0) { 20128 mutex_exit(SD_MUTEX(un)); 20129 return (0); 20130 } 20131 mutex_exit(SD_MUTEX(un)); 20132 } 20133 20134 bzero(&cdb, sizeof (cdb)); 20135 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20136 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20137 20138 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20139 20140 ucmd_buf.uscsi_cdb = (char *)&cdb; 20141 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20142 ucmd_buf.uscsi_bufaddr = NULL; 20143 ucmd_buf.uscsi_buflen = 0; 20144 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20145 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20146 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20147 20148 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20149 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20150 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20151 } 20152 ucmd_buf.uscsi_timeout = 60; 20153 20154 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20155 UIO_SYSSPACE, UIO_SYSSPACE, 20156 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 20157 20158 switch (status) { 20159 case 0: 20160 break; /* Success! */ 20161 case EIO: 20162 switch (ucmd_buf.uscsi_status) { 20163 case STATUS_RESERVATION_CONFLICT: 20164 status = EACCES; 20165 break; 20166 case STATUS_CHECK: 20167 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20168 break; 20169 } 20170 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20171 (scsi_sense_key((uint8_t *)&sense_buf) == 20172 KEY_NOT_READY) && 20173 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20174 status = ENXIO; 20175 } 20176 break; 20177 default: 20178 break; 20179 } 20180 break; 20181 default: 20182 break; 20183 } 20184 20185 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20186 20187 return (status); 20188 } 20189 20190 20191 /* 20192 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20193 * 20194 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20195 * 20196 * Arguments: un 20197 * 20198 * Return Code: 0 - Success 20199 * EACCES 20200 * ENOTSUP 20201 * errno return code from sd_send_scsi_cmd() 20202 * 20203 * Context: Can sleep. Does not return until command is completed. 20204 */ 20205 20206 static int 20207 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 20208 uint16_t data_len, uchar_t *data_bufp) 20209 { 20210 struct scsi_extended_sense sense_buf; 20211 union scsi_cdb cdb; 20212 struct uscsi_cmd ucmd_buf; 20213 int status; 20214 int no_caller_buf = FALSE; 20215 20216 ASSERT(un != NULL); 20217 ASSERT(!mutex_owned(SD_MUTEX(un))); 20218 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20219 20220 SD_TRACE(SD_LOG_IO, un, 20221 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20222 20223 bzero(&cdb, sizeof (cdb)); 20224 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20225 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20226 if (data_bufp == NULL) { 20227 /* Allocate a default buf if the caller did not give one */ 20228 ASSERT(data_len == 0); 20229 data_len = MHIOC_RESV_KEY_SIZE; 20230 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20231 no_caller_buf = TRUE; 20232 } 20233 20234 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20235 cdb.cdb_opaque[1] = usr_cmd; 20236 FORMG1COUNT(&cdb, data_len); 20237 20238 ucmd_buf.uscsi_cdb = (char *)&cdb; 20239 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20240 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20241 ucmd_buf.uscsi_buflen = data_len; 20242 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20243 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20244 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20245 ucmd_buf.uscsi_timeout = 60; 20246 20247 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20248 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20249 20250 switch (status) { 20251 case 0: 20252 break; /* Success! */ 20253 case EIO: 20254 switch (ucmd_buf.uscsi_status) { 20255 case STATUS_RESERVATION_CONFLICT: 20256 status = EACCES; 20257 break; 20258 case STATUS_CHECK: 20259 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20260 (scsi_sense_key((uint8_t *)&sense_buf) == 20261 KEY_ILLEGAL_REQUEST)) { 20262 status = ENOTSUP; 20263 } 20264 break; 20265 default: 20266 break; 20267 } 20268 break; 20269 default: 20270 break; 20271 } 20272 20273 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20274 20275 if (no_caller_buf == TRUE) { 20276 kmem_free(data_bufp, data_len); 20277 } 20278 20279 return (status); 20280 } 20281 20282 20283 /* 20284 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20285 * 20286 * Description: This routine is the driver entry point for handling CD-ROM 20287 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20288 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20289 * device. 20290 * 20291 * Arguments: un - Pointer to soft state struct for the target. 20292 * usr_cmd SCSI-3 reservation facility command (one of 20293 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20294 * SD_SCSI3_PREEMPTANDABORT) 20295 * usr_bufp - user provided pointer register, reserve descriptor or 20296 * preempt and abort structure (mhioc_register_t, 20297 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20298 * 20299 * Return Code: 0 - Success 20300 * EACCES 20301 * ENOTSUP 20302 * errno return code from sd_send_scsi_cmd() 20303 * 20304 * Context: Can sleep. Does not return until command is completed. 20305 */ 20306 20307 static int 20308 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 20309 uchar_t *usr_bufp) 20310 { 20311 struct scsi_extended_sense sense_buf; 20312 union scsi_cdb cdb; 20313 struct uscsi_cmd ucmd_buf; 20314 int status; 20315 uchar_t data_len = sizeof (sd_prout_t); 20316 sd_prout_t *prp; 20317 20318 ASSERT(un != NULL); 20319 ASSERT(!mutex_owned(SD_MUTEX(un))); 20320 ASSERT(data_len == 24); /* required by scsi spec */ 20321 20322 SD_TRACE(SD_LOG_IO, un, 20323 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20324 20325 if (usr_bufp == NULL) { 20326 return (EINVAL); 20327 } 20328 20329 bzero(&cdb, sizeof (cdb)); 20330 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20331 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20332 prp = kmem_zalloc(data_len, KM_SLEEP); 20333 20334 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20335 cdb.cdb_opaque[1] = usr_cmd; 20336 FORMG1COUNT(&cdb, data_len); 20337 20338 ucmd_buf.uscsi_cdb = (char *)&cdb; 20339 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20340 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20341 ucmd_buf.uscsi_buflen = data_len; 20342 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20343 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20344 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20345 ucmd_buf.uscsi_timeout = 60; 20346 20347 switch (usr_cmd) { 20348 case SD_SCSI3_REGISTER: { 20349 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20350 20351 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20352 bcopy(ptr->newkey.key, prp->service_key, 20353 MHIOC_RESV_KEY_SIZE); 20354 prp->aptpl = ptr->aptpl; 20355 break; 20356 } 20357 case SD_SCSI3_RESERVE: 20358 case SD_SCSI3_RELEASE: { 20359 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20360 20361 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20362 prp->scope_address = BE_32(ptr->scope_specific_addr); 20363 cdb.cdb_opaque[2] = ptr->type; 20364 break; 20365 } 20366 case SD_SCSI3_PREEMPTANDABORT: { 20367 mhioc_preemptandabort_t *ptr = 20368 (mhioc_preemptandabort_t *)usr_bufp; 20369 20370 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20371 bcopy(ptr->victim_key.key, prp->service_key, 20372 MHIOC_RESV_KEY_SIZE); 20373 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20374 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20375 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20376 break; 20377 } 20378 case SD_SCSI3_REGISTERANDIGNOREKEY: 20379 { 20380 mhioc_registerandignorekey_t *ptr; 20381 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20382 bcopy(ptr->newkey.key, 20383 prp->service_key, MHIOC_RESV_KEY_SIZE); 20384 prp->aptpl = ptr->aptpl; 20385 break; 20386 } 20387 default: 20388 ASSERT(FALSE); 20389 break; 20390 } 20391 20392 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20393 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20394 20395 switch (status) { 20396 case 0: 20397 break; /* Success! */ 20398 case EIO: 20399 switch (ucmd_buf.uscsi_status) { 20400 case STATUS_RESERVATION_CONFLICT: 20401 status = EACCES; 20402 break; 20403 case STATUS_CHECK: 20404 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20405 (scsi_sense_key((uint8_t *)&sense_buf) == 20406 KEY_ILLEGAL_REQUEST)) { 20407 status = ENOTSUP; 20408 } 20409 break; 20410 default: 20411 break; 20412 } 20413 break; 20414 default: 20415 break; 20416 } 20417 20418 kmem_free(prp, data_len); 20419 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20420 return (status); 20421 } 20422 20423 20424 /* 20425 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20426 * 20427 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20428 * 20429 * Arguments: un - pointer to the target's soft state struct 20430 * 20431 * Return Code: 0 - success 20432 * errno-type error code 20433 * 20434 * Context: kernel thread context only. 20435 */ 20436 20437 static int 20438 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20439 { 20440 struct sd_uscsi_info *uip; 20441 struct uscsi_cmd *uscmd; 20442 union scsi_cdb *cdb; 20443 struct buf *bp; 20444 int rval = 0; 20445 20446 SD_TRACE(SD_LOG_IO, un, 20447 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20448 20449 ASSERT(un != NULL); 20450 ASSERT(!mutex_owned(SD_MUTEX(un))); 20451 20452 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20453 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20454 20455 /* 20456 * First get some memory for the uscsi_cmd struct and cdb 20457 * and initialize for SYNCHRONIZE_CACHE cmd. 20458 */ 20459 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20460 uscmd->uscsi_cdblen = CDB_GROUP1; 20461 uscmd->uscsi_cdb = (caddr_t)cdb; 20462 uscmd->uscsi_bufaddr = NULL; 20463 uscmd->uscsi_buflen = 0; 20464 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20465 uscmd->uscsi_rqlen = SENSE_LENGTH; 20466 uscmd->uscsi_rqresid = SENSE_LENGTH; 20467 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20468 uscmd->uscsi_timeout = sd_io_time; 20469 20470 /* 20471 * Allocate an sd_uscsi_info struct and fill it with the info 20472 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20473 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20474 * since we allocate the buf here in this function, we do not 20475 * need to preserve the prior contents of b_private. 20476 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20477 */ 20478 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20479 uip->ui_flags = SD_PATH_DIRECT; 20480 uip->ui_cmdp = uscmd; 20481 20482 bp = getrbuf(KM_SLEEP); 20483 bp->b_private = uip; 20484 20485 /* 20486 * Setup buffer to carry uscsi request. 20487 */ 20488 bp->b_flags = B_BUSY; 20489 bp->b_bcount = 0; 20490 bp->b_blkno = 0; 20491 20492 if (dkc != NULL) { 20493 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20494 uip->ui_dkc = *dkc; 20495 } 20496 20497 bp->b_edev = SD_GET_DEV(un); 20498 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20499 20500 (void) sd_uscsi_strategy(bp); 20501 20502 /* 20503 * If synchronous request, wait for completion 20504 * If async just return and let b_iodone callback 20505 * cleanup. 20506 * NOTE: On return, u_ncmds_in_driver will be decremented, 20507 * but it was also incremented in sd_uscsi_strategy(), so 20508 * we should be ok. 20509 */ 20510 if (dkc == NULL) { 20511 (void) biowait(bp); 20512 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20513 } 20514 20515 return (rval); 20516 } 20517 20518 20519 static int 20520 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20521 { 20522 struct sd_uscsi_info *uip; 20523 struct uscsi_cmd *uscmd; 20524 uint8_t *sense_buf; 20525 struct sd_lun *un; 20526 int status; 20527 20528 uip = (struct sd_uscsi_info *)(bp->b_private); 20529 ASSERT(uip != NULL); 20530 20531 uscmd = uip->ui_cmdp; 20532 ASSERT(uscmd != NULL); 20533 20534 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20535 ASSERT(sense_buf != NULL); 20536 20537 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20538 ASSERT(un != NULL); 20539 20540 status = geterror(bp); 20541 switch (status) { 20542 case 0: 20543 break; /* Success! */ 20544 case EIO: 20545 switch (uscmd->uscsi_status) { 20546 case STATUS_RESERVATION_CONFLICT: 20547 /* Ignore reservation conflict */ 20548 status = 0; 20549 goto done; 20550 20551 case STATUS_CHECK: 20552 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20553 (scsi_sense_key(sense_buf) == 20554 KEY_ILLEGAL_REQUEST)) { 20555 /* Ignore Illegal Request error */ 20556 mutex_enter(SD_MUTEX(un)); 20557 un->un_f_sync_cache_supported = FALSE; 20558 mutex_exit(SD_MUTEX(un)); 20559 status = ENOTSUP; 20560 goto done; 20561 } 20562 break; 20563 default: 20564 break; 20565 } 20566 /* FALLTHRU */ 20567 default: 20568 /* 20569 * Don't log an error message if this device 20570 * has removable media. 20571 */ 20572 if (!un->un_f_has_removable_media) { 20573 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20574 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20575 } 20576 break; 20577 } 20578 20579 done: 20580 if (uip->ui_dkc.dkc_callback != NULL) { 20581 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20582 } 20583 20584 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20585 freerbuf(bp); 20586 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20587 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20588 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20589 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20590 20591 return (status); 20592 } 20593 20594 20595 /* 20596 * Function: sd_send_scsi_GET_CONFIGURATION 20597 * 20598 * Description: Issues the get configuration command to the device. 20599 * Called from sd_check_for_writable_cd & sd_get_media_info 20600 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20601 * Arguments: un 20602 * ucmdbuf 20603 * rqbuf 20604 * rqbuflen 20605 * bufaddr 20606 * buflen 20607 * 20608 * Return Code: 0 - Success 20609 * errno return code from sd_send_scsi_cmd() 20610 * 20611 * Context: Can sleep. Does not return until command is completed. 20612 * 20613 */ 20614 20615 static int 20616 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20617 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20618 { 20619 char cdb[CDB_GROUP1]; 20620 int status; 20621 20622 ASSERT(un != NULL); 20623 ASSERT(!mutex_owned(SD_MUTEX(un))); 20624 ASSERT(bufaddr != NULL); 20625 ASSERT(ucmdbuf != NULL); 20626 ASSERT(rqbuf != NULL); 20627 20628 SD_TRACE(SD_LOG_IO, un, 20629 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20630 20631 bzero(cdb, sizeof (cdb)); 20632 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20633 bzero(rqbuf, rqbuflen); 20634 bzero(bufaddr, buflen); 20635 20636 /* 20637 * Set up cdb field for the get configuration command. 20638 */ 20639 cdb[0] = SCMD_GET_CONFIGURATION; 20640 cdb[1] = 0x02; /* Requested Type */ 20641 cdb[8] = SD_PROFILE_HEADER_LEN; 20642 ucmdbuf->uscsi_cdb = cdb; 20643 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20644 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20645 ucmdbuf->uscsi_buflen = buflen; 20646 ucmdbuf->uscsi_timeout = sd_io_time; 20647 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20648 ucmdbuf->uscsi_rqlen = rqbuflen; 20649 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20650 20651 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20652 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20653 20654 switch (status) { 20655 case 0: 20656 break; /* Success! */ 20657 case EIO: 20658 switch (ucmdbuf->uscsi_status) { 20659 case STATUS_RESERVATION_CONFLICT: 20660 status = EACCES; 20661 break; 20662 default: 20663 break; 20664 } 20665 break; 20666 default: 20667 break; 20668 } 20669 20670 if (status == 0) { 20671 SD_DUMP_MEMORY(un, SD_LOG_IO, 20672 "sd_send_scsi_GET_CONFIGURATION: data", 20673 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20674 } 20675 20676 SD_TRACE(SD_LOG_IO, un, 20677 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20678 20679 return (status); 20680 } 20681 20682 /* 20683 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20684 * 20685 * Description: Issues the get configuration command to the device to 20686 * retrieve a specfic feature. Called from 20687 * sd_check_for_writable_cd & sd_set_mmc_caps. 20688 * Arguments: un 20689 * ucmdbuf 20690 * rqbuf 20691 * rqbuflen 20692 * bufaddr 20693 * buflen 20694 * feature 20695 * 20696 * Return Code: 0 - Success 20697 * errno return code from sd_send_scsi_cmd() 20698 * 20699 * Context: Can sleep. Does not return until command is completed. 20700 * 20701 */ 20702 static int 20703 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20704 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20705 uchar_t *bufaddr, uint_t buflen, char feature) 20706 { 20707 char cdb[CDB_GROUP1]; 20708 int status; 20709 20710 ASSERT(un != NULL); 20711 ASSERT(!mutex_owned(SD_MUTEX(un))); 20712 ASSERT(bufaddr != NULL); 20713 ASSERT(ucmdbuf != NULL); 20714 ASSERT(rqbuf != NULL); 20715 20716 SD_TRACE(SD_LOG_IO, un, 20717 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20718 20719 bzero(cdb, sizeof (cdb)); 20720 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20721 bzero(rqbuf, rqbuflen); 20722 bzero(bufaddr, buflen); 20723 20724 /* 20725 * Set up cdb field for the get configuration command. 20726 */ 20727 cdb[0] = SCMD_GET_CONFIGURATION; 20728 cdb[1] = 0x02; /* Requested Type */ 20729 cdb[3] = feature; 20730 cdb[8] = buflen; 20731 ucmdbuf->uscsi_cdb = cdb; 20732 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20733 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20734 ucmdbuf->uscsi_buflen = buflen; 20735 ucmdbuf->uscsi_timeout = sd_io_time; 20736 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20737 ucmdbuf->uscsi_rqlen = rqbuflen; 20738 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20739 20740 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20741 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20742 20743 switch (status) { 20744 case 0: 20745 break; /* Success! */ 20746 case EIO: 20747 switch (ucmdbuf->uscsi_status) { 20748 case STATUS_RESERVATION_CONFLICT: 20749 status = EACCES; 20750 break; 20751 default: 20752 break; 20753 } 20754 break; 20755 default: 20756 break; 20757 } 20758 20759 if (status == 0) { 20760 SD_DUMP_MEMORY(un, SD_LOG_IO, 20761 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20762 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20763 } 20764 20765 SD_TRACE(SD_LOG_IO, un, 20766 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20767 20768 return (status); 20769 } 20770 20771 20772 /* 20773 * Function: sd_send_scsi_MODE_SENSE 20774 * 20775 * Description: Utility function for issuing a scsi MODE SENSE command. 20776 * Note: This routine uses a consistent implementation for Group0, 20777 * Group1, and Group2 commands across all platforms. ATAPI devices 20778 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20779 * 20780 * Arguments: un - pointer to the softstate struct for the target. 20781 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20782 * CDB_GROUP[1|2] (10 byte). 20783 * bufaddr - buffer for page data retrieved from the target. 20784 * buflen - size of page to be retrieved. 20785 * page_code - page code of data to be retrieved from the target. 20786 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20787 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20788 * to use the USCSI "direct" chain and bypass the normal 20789 * command waitq. 20790 * 20791 * Return Code: 0 - Success 20792 * errno return code from sd_send_scsi_cmd() 20793 * 20794 * Context: Can sleep. Does not return until command is completed. 20795 */ 20796 20797 static int 20798 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20799 size_t buflen, uchar_t page_code, int path_flag) 20800 { 20801 struct scsi_extended_sense sense_buf; 20802 union scsi_cdb cdb; 20803 struct uscsi_cmd ucmd_buf; 20804 int status; 20805 int headlen; 20806 20807 ASSERT(un != NULL); 20808 ASSERT(!mutex_owned(SD_MUTEX(un))); 20809 ASSERT(bufaddr != NULL); 20810 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20811 (cdbsize == CDB_GROUP2)); 20812 20813 SD_TRACE(SD_LOG_IO, un, 20814 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20815 20816 bzero(&cdb, sizeof (cdb)); 20817 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20818 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20819 bzero(bufaddr, buflen); 20820 20821 if (cdbsize == CDB_GROUP0) { 20822 cdb.scc_cmd = SCMD_MODE_SENSE; 20823 cdb.cdb_opaque[2] = page_code; 20824 FORMG0COUNT(&cdb, buflen); 20825 headlen = MODE_HEADER_LENGTH; 20826 } else { 20827 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20828 cdb.cdb_opaque[2] = page_code; 20829 FORMG1COUNT(&cdb, buflen); 20830 headlen = MODE_HEADER_LENGTH_GRP2; 20831 } 20832 20833 ASSERT(headlen <= buflen); 20834 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20835 20836 ucmd_buf.uscsi_cdb = (char *)&cdb; 20837 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20838 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20839 ucmd_buf.uscsi_buflen = buflen; 20840 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20841 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20842 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20843 ucmd_buf.uscsi_timeout = 60; 20844 20845 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20846 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20847 20848 switch (status) { 20849 case 0: 20850 /* 20851 * sr_check_wp() uses 0x3f page code and check the header of 20852 * mode page to determine if target device is write-protected. 20853 * But some USB devices return 0 bytes for 0x3f page code. For 20854 * this case, make sure that mode page header is returned at 20855 * least. 20856 */ 20857 if (buflen - ucmd_buf.uscsi_resid < headlen) 20858 status = EIO; 20859 break; /* Success! */ 20860 case EIO: 20861 switch (ucmd_buf.uscsi_status) { 20862 case STATUS_RESERVATION_CONFLICT: 20863 status = EACCES; 20864 break; 20865 default: 20866 break; 20867 } 20868 break; 20869 default: 20870 break; 20871 } 20872 20873 if (status == 0) { 20874 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20875 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20876 } 20877 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20878 20879 return (status); 20880 } 20881 20882 20883 /* 20884 * Function: sd_send_scsi_MODE_SELECT 20885 * 20886 * Description: Utility function for issuing a scsi MODE SELECT command. 20887 * Note: This routine uses a consistent implementation for Group0, 20888 * Group1, and Group2 commands across all platforms. ATAPI devices 20889 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20890 * 20891 * Arguments: un - pointer to the softstate struct for the target. 20892 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20893 * CDB_GROUP[1|2] (10 byte). 20894 * bufaddr - buffer for page data retrieved from the target. 20895 * buflen - size of page to be retrieved. 20896 * save_page - boolean to determin if SP bit should be set. 20897 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20898 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20899 * to use the USCSI "direct" chain and bypass the normal 20900 * command waitq. 20901 * 20902 * Return Code: 0 - Success 20903 * errno return code from sd_send_scsi_cmd() 20904 * 20905 * Context: Can sleep. Does not return until command is completed. 20906 */ 20907 20908 static int 20909 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20910 size_t buflen, uchar_t save_page, int path_flag) 20911 { 20912 struct scsi_extended_sense sense_buf; 20913 union scsi_cdb cdb; 20914 struct uscsi_cmd ucmd_buf; 20915 int status; 20916 20917 ASSERT(un != NULL); 20918 ASSERT(!mutex_owned(SD_MUTEX(un))); 20919 ASSERT(bufaddr != NULL); 20920 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20921 (cdbsize == CDB_GROUP2)); 20922 20923 SD_TRACE(SD_LOG_IO, un, 20924 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20925 20926 bzero(&cdb, sizeof (cdb)); 20927 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20928 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20929 20930 /* Set the PF bit for many third party drives */ 20931 cdb.cdb_opaque[1] = 0x10; 20932 20933 /* Set the savepage(SP) bit if given */ 20934 if (save_page == SD_SAVE_PAGE) { 20935 cdb.cdb_opaque[1] |= 0x01; 20936 } 20937 20938 if (cdbsize == CDB_GROUP0) { 20939 cdb.scc_cmd = SCMD_MODE_SELECT; 20940 FORMG0COUNT(&cdb, buflen); 20941 } else { 20942 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20943 FORMG1COUNT(&cdb, buflen); 20944 } 20945 20946 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20947 20948 ucmd_buf.uscsi_cdb = (char *)&cdb; 20949 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20950 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20951 ucmd_buf.uscsi_buflen = buflen; 20952 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20953 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20954 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20955 ucmd_buf.uscsi_timeout = 60; 20956 20957 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20958 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20959 20960 switch (status) { 20961 case 0: 20962 break; /* Success! */ 20963 case EIO: 20964 switch (ucmd_buf.uscsi_status) { 20965 case STATUS_RESERVATION_CONFLICT: 20966 status = EACCES; 20967 break; 20968 default: 20969 break; 20970 } 20971 break; 20972 default: 20973 break; 20974 } 20975 20976 if (status == 0) { 20977 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 20978 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20979 } 20980 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 20981 20982 return (status); 20983 } 20984 20985 20986 /* 20987 * Function: sd_send_scsi_RDWR 20988 * 20989 * Description: Issue a scsi READ or WRITE command with the given parameters. 20990 * 20991 * Arguments: un: Pointer to the sd_lun struct for the target. 20992 * cmd: SCMD_READ or SCMD_WRITE 20993 * bufaddr: Address of caller's buffer to receive the RDWR data 20994 * buflen: Length of caller's buffer receive the RDWR data. 20995 * start_block: Block number for the start of the RDWR operation. 20996 * (Assumes target-native block size.) 20997 * residp: Pointer to variable to receive the redisual of the 20998 * RDWR operation (may be NULL of no residual requested). 20999 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21000 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21001 * to use the USCSI "direct" chain and bypass the normal 21002 * command waitq. 21003 * 21004 * Return Code: 0 - Success 21005 * errno return code from sd_send_scsi_cmd() 21006 * 21007 * Context: Can sleep. Does not return until command is completed. 21008 */ 21009 21010 static int 21011 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 21012 size_t buflen, daddr_t start_block, int path_flag) 21013 { 21014 struct scsi_extended_sense sense_buf; 21015 union scsi_cdb cdb; 21016 struct uscsi_cmd ucmd_buf; 21017 uint32_t block_count; 21018 int status; 21019 int cdbsize; 21020 uchar_t flag; 21021 21022 ASSERT(un != NULL); 21023 ASSERT(!mutex_owned(SD_MUTEX(un))); 21024 ASSERT(bufaddr != NULL); 21025 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21026 21027 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21028 21029 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21030 return (EINVAL); 21031 } 21032 21033 mutex_enter(SD_MUTEX(un)); 21034 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21035 mutex_exit(SD_MUTEX(un)); 21036 21037 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21038 21039 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21040 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21041 bufaddr, buflen, start_block, block_count); 21042 21043 bzero(&cdb, sizeof (cdb)); 21044 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21045 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21046 21047 /* Compute CDB size to use */ 21048 if (start_block > 0xffffffff) 21049 cdbsize = CDB_GROUP4; 21050 else if ((start_block & 0xFFE00000) || 21051 (un->un_f_cfg_is_atapi == TRUE)) 21052 cdbsize = CDB_GROUP1; 21053 else 21054 cdbsize = CDB_GROUP0; 21055 21056 switch (cdbsize) { 21057 case CDB_GROUP0: /* 6-byte CDBs */ 21058 cdb.scc_cmd = cmd; 21059 FORMG0ADDR(&cdb, start_block); 21060 FORMG0COUNT(&cdb, block_count); 21061 break; 21062 case CDB_GROUP1: /* 10-byte CDBs */ 21063 cdb.scc_cmd = cmd | SCMD_GROUP1; 21064 FORMG1ADDR(&cdb, start_block); 21065 FORMG1COUNT(&cdb, block_count); 21066 break; 21067 case CDB_GROUP4: /* 16-byte CDBs */ 21068 cdb.scc_cmd = cmd | SCMD_GROUP4; 21069 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21070 FORMG4COUNT(&cdb, block_count); 21071 break; 21072 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21073 default: 21074 /* All others reserved */ 21075 return (EINVAL); 21076 } 21077 21078 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21079 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21080 21081 ucmd_buf.uscsi_cdb = (char *)&cdb; 21082 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21083 ucmd_buf.uscsi_bufaddr = bufaddr; 21084 ucmd_buf.uscsi_buflen = buflen; 21085 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21086 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21087 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21088 ucmd_buf.uscsi_timeout = 60; 21089 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21090 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21091 switch (status) { 21092 case 0: 21093 break; /* Success! */ 21094 case EIO: 21095 switch (ucmd_buf.uscsi_status) { 21096 case STATUS_RESERVATION_CONFLICT: 21097 status = EACCES; 21098 break; 21099 default: 21100 break; 21101 } 21102 break; 21103 default: 21104 break; 21105 } 21106 21107 if (status == 0) { 21108 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21109 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21110 } 21111 21112 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21113 21114 return (status); 21115 } 21116 21117 21118 /* 21119 * Function: sd_send_scsi_LOG_SENSE 21120 * 21121 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21122 * 21123 * Arguments: un: Pointer to the sd_lun struct for the target. 21124 * 21125 * Return Code: 0 - Success 21126 * errno return code from sd_send_scsi_cmd() 21127 * 21128 * Context: Can sleep. Does not return until command is completed. 21129 */ 21130 21131 static int 21132 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 21133 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21134 int path_flag) 21135 21136 { 21137 struct scsi_extended_sense sense_buf; 21138 union scsi_cdb cdb; 21139 struct uscsi_cmd ucmd_buf; 21140 int status; 21141 21142 ASSERT(un != NULL); 21143 ASSERT(!mutex_owned(SD_MUTEX(un))); 21144 21145 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21146 21147 bzero(&cdb, sizeof (cdb)); 21148 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21149 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21150 21151 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21152 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21153 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21154 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21155 FORMG1COUNT(&cdb, buflen); 21156 21157 ucmd_buf.uscsi_cdb = (char *)&cdb; 21158 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21159 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21160 ucmd_buf.uscsi_buflen = buflen; 21161 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21162 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21163 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21164 ucmd_buf.uscsi_timeout = 60; 21165 21166 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21167 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21168 21169 switch (status) { 21170 case 0: 21171 break; 21172 case EIO: 21173 switch (ucmd_buf.uscsi_status) { 21174 case STATUS_RESERVATION_CONFLICT: 21175 status = EACCES; 21176 break; 21177 case STATUS_CHECK: 21178 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21179 (scsi_sense_key((uint8_t *)&sense_buf) == 21180 KEY_ILLEGAL_REQUEST) && 21181 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21182 /* 21183 * ASC 0x24: INVALID FIELD IN CDB 21184 */ 21185 switch (page_code) { 21186 case START_STOP_CYCLE_PAGE: 21187 /* 21188 * The start stop cycle counter is 21189 * implemented as page 0x31 in earlier 21190 * generation disks. In new generation 21191 * disks the start stop cycle counter is 21192 * implemented as page 0xE. To properly 21193 * handle this case if an attempt for 21194 * log page 0xE is made and fails we 21195 * will try again using page 0x31. 21196 * 21197 * Network storage BU committed to 21198 * maintain the page 0x31 for this 21199 * purpose and will not have any other 21200 * page implemented with page code 0x31 21201 * until all disks transition to the 21202 * standard page. 21203 */ 21204 mutex_enter(SD_MUTEX(un)); 21205 un->un_start_stop_cycle_page = 21206 START_STOP_CYCLE_VU_PAGE; 21207 cdb.cdb_opaque[2] = 21208 (char)(page_control << 6) | 21209 un->un_start_stop_cycle_page; 21210 mutex_exit(SD_MUTEX(un)); 21211 status = sd_send_scsi_cmd( 21212 SD_GET_DEV(un), &ucmd_buf, 21213 UIO_SYSSPACE, UIO_SYSSPACE, 21214 UIO_SYSSPACE, path_flag); 21215 21216 break; 21217 case TEMPERATURE_PAGE: 21218 status = ENOTTY; 21219 break; 21220 default: 21221 break; 21222 } 21223 } 21224 break; 21225 default: 21226 break; 21227 } 21228 break; 21229 default: 21230 break; 21231 } 21232 21233 if (status == 0) { 21234 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21235 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21236 } 21237 21238 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21239 21240 return (status); 21241 } 21242 21243 21244 /* 21245 * Function: sdioctl 21246 * 21247 * Description: Driver's ioctl(9e) entry point function. 21248 * 21249 * Arguments: dev - device number 21250 * cmd - ioctl operation to be performed 21251 * arg - user argument, contains data to be set or reference 21252 * parameter for get 21253 * flag - bit flag, indicating open settings, 32/64 bit type 21254 * cred_p - user credential pointer 21255 * rval_p - calling process return value (OPT) 21256 * 21257 * Return Code: EINVAL 21258 * ENOTTY 21259 * ENXIO 21260 * EIO 21261 * EFAULT 21262 * ENOTSUP 21263 * EPERM 21264 * 21265 * Context: Called from the device switch at normal priority. 21266 */ 21267 21268 static int 21269 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 21270 { 21271 struct sd_lun *un = NULL; 21272 int geom_validated = FALSE; 21273 int err = 0; 21274 int i = 0; 21275 cred_t *cr; 21276 21277 /* 21278 * All device accesses go thru sdstrategy where we check on suspend 21279 * status 21280 */ 21281 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21282 return (ENXIO); 21283 } 21284 21285 ASSERT(!mutex_owned(SD_MUTEX(un))); 21286 21287 /* 21288 * Moved this wait from sd_uscsi_strategy to here for 21289 * reasons of deadlock prevention. Internal driver commands, 21290 * specifically those to change a devices power level, result 21291 * in a call to sd_uscsi_strategy. 21292 */ 21293 mutex_enter(SD_MUTEX(un)); 21294 while ((un->un_state == SD_STATE_SUSPENDED) || 21295 (un->un_state == SD_STATE_PM_CHANGING)) { 21296 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 21297 } 21298 /* 21299 * Twiddling the counter here protects commands from now 21300 * through to the top of sd_uscsi_strategy. Without the 21301 * counter inc. a power down, for example, could get in 21302 * after the above check for state is made and before 21303 * execution gets to the top of sd_uscsi_strategy. 21304 * That would cause problems. 21305 */ 21306 un->un_ncmds_in_driver++; 21307 21308 if ((un->un_f_geometry_is_valid == FALSE) && 21309 (flag & (FNDELAY | FNONBLOCK))) { 21310 switch (cmd) { 21311 case CDROMPAUSE: 21312 case CDROMRESUME: 21313 case CDROMPLAYMSF: 21314 case CDROMPLAYTRKIND: 21315 case CDROMREADTOCHDR: 21316 case CDROMREADTOCENTRY: 21317 case CDROMSTOP: 21318 case CDROMSTART: 21319 case CDROMVOLCTRL: 21320 case CDROMSUBCHNL: 21321 case CDROMREADMODE2: 21322 case CDROMREADMODE1: 21323 case CDROMREADOFFSET: 21324 case CDROMSBLKMODE: 21325 case CDROMGBLKMODE: 21326 case CDROMGDRVSPEED: 21327 case CDROMSDRVSPEED: 21328 case CDROMCDDA: 21329 case CDROMCDXA: 21330 case CDROMSUBCODE: 21331 if (!ISCD(un)) { 21332 un->un_ncmds_in_driver--; 21333 ASSERT(un->un_ncmds_in_driver >= 0); 21334 mutex_exit(SD_MUTEX(un)); 21335 return (ENOTTY); 21336 } 21337 break; 21338 case FDEJECT: 21339 case DKIOCEJECT: 21340 case CDROMEJECT: 21341 if (!un->un_f_eject_media_supported) { 21342 un->un_ncmds_in_driver--; 21343 ASSERT(un->un_ncmds_in_driver >= 0); 21344 mutex_exit(SD_MUTEX(un)); 21345 return (ENOTTY); 21346 } 21347 break; 21348 case DKIOCSVTOC: 21349 case DKIOCSETEFI: 21350 case DKIOCSMBOOT: 21351 case DKIOCFLUSHWRITECACHE: 21352 mutex_exit(SD_MUTEX(un)); 21353 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 21354 if (err != 0) { 21355 mutex_enter(SD_MUTEX(un)); 21356 un->un_ncmds_in_driver--; 21357 ASSERT(un->un_ncmds_in_driver >= 0); 21358 mutex_exit(SD_MUTEX(un)); 21359 return (EIO); 21360 } 21361 mutex_enter(SD_MUTEX(un)); 21362 /* FALLTHROUGH */ 21363 case DKIOCREMOVABLE: 21364 case DKIOCHOTPLUGGABLE: 21365 case DKIOCINFO: 21366 case DKIOCGMEDIAINFO: 21367 case MHIOCENFAILFAST: 21368 case MHIOCSTATUS: 21369 case MHIOCTKOWN: 21370 case MHIOCRELEASE: 21371 case MHIOCGRP_INKEYS: 21372 case MHIOCGRP_INRESV: 21373 case MHIOCGRP_REGISTER: 21374 case MHIOCGRP_RESERVE: 21375 case MHIOCGRP_PREEMPTANDABORT: 21376 case MHIOCGRP_REGISTERANDIGNOREKEY: 21377 case CDROMCLOSETRAY: 21378 case USCSICMD: 21379 goto skip_ready_valid; 21380 default: 21381 break; 21382 } 21383 21384 mutex_exit(SD_MUTEX(un)); 21385 err = sd_ready_and_valid(un); 21386 mutex_enter(SD_MUTEX(un)); 21387 if (err == SD_READY_NOT_VALID) { 21388 switch (cmd) { 21389 case DKIOCGAPART: 21390 case DKIOCGGEOM: 21391 case DKIOCSGEOM: 21392 case DKIOCGVTOC: 21393 case DKIOCSVTOC: 21394 case DKIOCSAPART: 21395 case DKIOCG_PHYGEOM: 21396 case DKIOCG_VIRTGEOM: 21397 err = ENOTSUP; 21398 un->un_ncmds_in_driver--; 21399 ASSERT(un->un_ncmds_in_driver >= 0); 21400 mutex_exit(SD_MUTEX(un)); 21401 return (err); 21402 } 21403 } 21404 if (err != SD_READY_VALID) { 21405 switch (cmd) { 21406 case DKIOCSTATE: 21407 case CDROMGDRVSPEED: 21408 case CDROMSDRVSPEED: 21409 case FDEJECT: /* for eject command */ 21410 case DKIOCEJECT: 21411 case CDROMEJECT: 21412 case DKIOCGETEFI: 21413 case DKIOCSGEOM: 21414 case DKIOCREMOVABLE: 21415 case DKIOCHOTPLUGGABLE: 21416 case DKIOCSAPART: 21417 case DKIOCSETEFI: 21418 break; 21419 default: 21420 if (un->un_f_has_removable_media) { 21421 err = ENXIO; 21422 } else { 21423 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 21424 if (err == SD_RESERVED_BY_OTHERS) { 21425 err = EACCES; 21426 } else { 21427 err = EIO; 21428 } 21429 } 21430 un->un_ncmds_in_driver--; 21431 ASSERT(un->un_ncmds_in_driver >= 0); 21432 mutex_exit(SD_MUTEX(un)); 21433 return (err); 21434 } 21435 } 21436 geom_validated = TRUE; 21437 } 21438 if ((un->un_f_geometry_is_valid == TRUE) && 21439 (un->un_solaris_size > 0)) { 21440 /* 21441 * the "geometry_is_valid" flag could be true if we 21442 * have an fdisk table but no Solaris partition 21443 */ 21444 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21445 /* it is EFI, so return ENOTSUP for these */ 21446 switch (cmd) { 21447 case DKIOCGAPART: 21448 case DKIOCGGEOM: 21449 case DKIOCGVTOC: 21450 case DKIOCSVTOC: 21451 case DKIOCSAPART: 21452 err = ENOTSUP; 21453 un->un_ncmds_in_driver--; 21454 ASSERT(un->un_ncmds_in_driver >= 0); 21455 mutex_exit(SD_MUTEX(un)); 21456 return (err); 21457 } 21458 } 21459 } 21460 21461 skip_ready_valid: 21462 mutex_exit(SD_MUTEX(un)); 21463 21464 switch (cmd) { 21465 case DKIOCINFO: 21466 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21467 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21468 break; 21469 21470 case DKIOCGMEDIAINFO: 21471 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21472 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21473 break; 21474 21475 case DKIOCGGEOM: 21476 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21477 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21478 geom_validated); 21479 break; 21480 21481 case DKIOCSGEOM: 21482 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21483 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21484 break; 21485 21486 case DKIOCGAPART: 21487 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21488 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21489 geom_validated); 21490 break; 21491 21492 case DKIOCSAPART: 21493 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21494 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21495 break; 21496 21497 case DKIOCGVTOC: 21498 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21499 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21500 geom_validated); 21501 break; 21502 21503 case DKIOCGETEFI: 21504 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21505 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21506 break; 21507 21508 case DKIOCPARTITION: 21509 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21510 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21511 break; 21512 21513 case DKIOCSVTOC: 21514 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21515 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21516 break; 21517 21518 case DKIOCSETEFI: 21519 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21520 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21521 break; 21522 21523 case DKIOCGMBOOT: 21524 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21525 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21526 break; 21527 21528 case DKIOCSMBOOT: 21529 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21530 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21531 break; 21532 21533 case DKIOCLOCK: 21534 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21535 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21536 SD_PATH_STANDARD); 21537 break; 21538 21539 case DKIOCUNLOCK: 21540 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21541 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21542 SD_PATH_STANDARD); 21543 break; 21544 21545 case DKIOCSTATE: { 21546 enum dkio_state state; 21547 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21548 21549 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21550 err = EFAULT; 21551 } else { 21552 err = sd_check_media(dev, state); 21553 if (err == 0) { 21554 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21555 sizeof (int), flag) != 0) 21556 err = EFAULT; 21557 } 21558 } 21559 break; 21560 } 21561 21562 case DKIOCREMOVABLE: 21563 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21564 /* 21565 * At present, vold only does automount for removable-media 21566 * devices, in order not to break current applications, we 21567 * still let hopluggable devices pretend to be removable media 21568 * devices for vold. In the near future, once vold is EOL'ed, 21569 * we should remove this workaround. 21570 */ 21571 if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) { 21572 i = 1; 21573 } else { 21574 i = 0; 21575 } 21576 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21577 err = EFAULT; 21578 } else { 21579 err = 0; 21580 } 21581 break; 21582 21583 case DKIOCHOTPLUGGABLE: 21584 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21585 if (un->un_f_is_hotpluggable) { 21586 i = 1; 21587 } else { 21588 i = 0; 21589 } 21590 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21591 err = EFAULT; 21592 } else { 21593 err = 0; 21594 } 21595 break; 21596 21597 case DKIOCGTEMPERATURE: 21598 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21599 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21600 break; 21601 21602 case MHIOCENFAILFAST: 21603 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21604 if ((err = drv_priv(cred_p)) == 0) { 21605 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21606 } 21607 break; 21608 21609 case MHIOCTKOWN: 21610 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21611 if ((err = drv_priv(cred_p)) == 0) { 21612 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21613 } 21614 break; 21615 21616 case MHIOCRELEASE: 21617 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21618 if ((err = drv_priv(cred_p)) == 0) { 21619 err = sd_mhdioc_release(dev); 21620 } 21621 break; 21622 21623 case MHIOCSTATUS: 21624 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21625 if ((err = drv_priv(cred_p)) == 0) { 21626 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21627 case 0: 21628 err = 0; 21629 break; 21630 case EACCES: 21631 *rval_p = 1; 21632 err = 0; 21633 break; 21634 default: 21635 err = EIO; 21636 break; 21637 } 21638 } 21639 break; 21640 21641 case MHIOCQRESERVE: 21642 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21643 if ((err = drv_priv(cred_p)) == 0) { 21644 err = sd_reserve_release(dev, SD_RESERVE); 21645 } 21646 break; 21647 21648 case MHIOCREREGISTERDEVID: 21649 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21650 if (drv_priv(cred_p) == EPERM) { 21651 err = EPERM; 21652 } else if (!un->un_f_devid_supported) { 21653 err = ENOTTY; 21654 } else { 21655 err = sd_mhdioc_register_devid(dev); 21656 } 21657 break; 21658 21659 case MHIOCGRP_INKEYS: 21660 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21661 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21662 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21663 err = ENOTSUP; 21664 } else { 21665 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21666 flag); 21667 } 21668 } 21669 break; 21670 21671 case MHIOCGRP_INRESV: 21672 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21673 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21674 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21675 err = ENOTSUP; 21676 } else { 21677 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21678 } 21679 } 21680 break; 21681 21682 case MHIOCGRP_REGISTER: 21683 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21684 if ((err = drv_priv(cred_p)) != EPERM) { 21685 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21686 err = ENOTSUP; 21687 } else if (arg != NULL) { 21688 mhioc_register_t reg; 21689 if (ddi_copyin((void *)arg, ®, 21690 sizeof (mhioc_register_t), flag) != 0) { 21691 err = EFAULT; 21692 } else { 21693 err = 21694 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21695 un, SD_SCSI3_REGISTER, 21696 (uchar_t *)®); 21697 } 21698 } 21699 } 21700 break; 21701 21702 case MHIOCGRP_RESERVE: 21703 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21704 if ((err = drv_priv(cred_p)) != EPERM) { 21705 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21706 err = ENOTSUP; 21707 } else if (arg != NULL) { 21708 mhioc_resv_desc_t resv_desc; 21709 if (ddi_copyin((void *)arg, &resv_desc, 21710 sizeof (mhioc_resv_desc_t), flag) != 0) { 21711 err = EFAULT; 21712 } else { 21713 err = 21714 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21715 un, SD_SCSI3_RESERVE, 21716 (uchar_t *)&resv_desc); 21717 } 21718 } 21719 } 21720 break; 21721 21722 case MHIOCGRP_PREEMPTANDABORT: 21723 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21724 if ((err = drv_priv(cred_p)) != EPERM) { 21725 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21726 err = ENOTSUP; 21727 } else if (arg != NULL) { 21728 mhioc_preemptandabort_t preempt_abort; 21729 if (ddi_copyin((void *)arg, &preempt_abort, 21730 sizeof (mhioc_preemptandabort_t), 21731 flag) != 0) { 21732 err = EFAULT; 21733 } else { 21734 err = 21735 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21736 un, SD_SCSI3_PREEMPTANDABORT, 21737 (uchar_t *)&preempt_abort); 21738 } 21739 } 21740 } 21741 break; 21742 21743 case MHIOCGRP_REGISTERANDIGNOREKEY: 21744 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21745 if ((err = drv_priv(cred_p)) != EPERM) { 21746 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21747 err = ENOTSUP; 21748 } else if (arg != NULL) { 21749 mhioc_registerandignorekey_t r_and_i; 21750 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21751 sizeof (mhioc_registerandignorekey_t), 21752 flag) != 0) { 21753 err = EFAULT; 21754 } else { 21755 err = 21756 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21757 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21758 (uchar_t *)&r_and_i); 21759 } 21760 } 21761 } 21762 break; 21763 21764 case USCSICMD: 21765 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21766 cr = ddi_get_cred(); 21767 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21768 err = EPERM; 21769 } else { 21770 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21771 } 21772 break; 21773 21774 case CDROMPAUSE: 21775 case CDROMRESUME: 21776 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21777 if (!ISCD(un)) { 21778 err = ENOTTY; 21779 } else { 21780 err = sr_pause_resume(dev, cmd); 21781 } 21782 break; 21783 21784 case CDROMPLAYMSF: 21785 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21786 if (!ISCD(un)) { 21787 err = ENOTTY; 21788 } else { 21789 err = sr_play_msf(dev, (caddr_t)arg, flag); 21790 } 21791 break; 21792 21793 case CDROMPLAYTRKIND: 21794 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21795 #if defined(__i386) || defined(__amd64) 21796 /* 21797 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21798 */ 21799 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21800 #else 21801 if (!ISCD(un)) { 21802 #endif 21803 err = ENOTTY; 21804 } else { 21805 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21806 } 21807 break; 21808 21809 case CDROMREADTOCHDR: 21810 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21811 if (!ISCD(un)) { 21812 err = ENOTTY; 21813 } else { 21814 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21815 } 21816 break; 21817 21818 case CDROMREADTOCENTRY: 21819 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21820 if (!ISCD(un)) { 21821 err = ENOTTY; 21822 } else { 21823 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21824 } 21825 break; 21826 21827 case CDROMSTOP: 21828 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21829 if (!ISCD(un)) { 21830 err = ENOTTY; 21831 } else { 21832 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21833 SD_PATH_STANDARD); 21834 } 21835 break; 21836 21837 case CDROMSTART: 21838 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21839 if (!ISCD(un)) { 21840 err = ENOTTY; 21841 } else { 21842 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21843 SD_PATH_STANDARD); 21844 } 21845 break; 21846 21847 case CDROMCLOSETRAY: 21848 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21849 if (!ISCD(un)) { 21850 err = ENOTTY; 21851 } else { 21852 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21853 SD_PATH_STANDARD); 21854 } 21855 break; 21856 21857 case FDEJECT: /* for eject command */ 21858 case DKIOCEJECT: 21859 case CDROMEJECT: 21860 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21861 if (!un->un_f_eject_media_supported) { 21862 err = ENOTTY; 21863 } else { 21864 err = sr_eject(dev); 21865 } 21866 break; 21867 21868 case CDROMVOLCTRL: 21869 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21870 if (!ISCD(un)) { 21871 err = ENOTTY; 21872 } else { 21873 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21874 } 21875 break; 21876 21877 case CDROMSUBCHNL: 21878 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21879 if (!ISCD(un)) { 21880 err = ENOTTY; 21881 } else { 21882 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21883 } 21884 break; 21885 21886 case CDROMREADMODE2: 21887 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21888 if (!ISCD(un)) { 21889 err = ENOTTY; 21890 } else if (un->un_f_cfg_is_atapi == TRUE) { 21891 /* 21892 * If the drive supports READ CD, use that instead of 21893 * switching the LBA size via a MODE SELECT 21894 * Block Descriptor 21895 */ 21896 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21897 } else { 21898 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21899 } 21900 break; 21901 21902 case CDROMREADMODE1: 21903 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21904 if (!ISCD(un)) { 21905 err = ENOTTY; 21906 } else { 21907 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21908 } 21909 break; 21910 21911 case CDROMREADOFFSET: 21912 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21913 if (!ISCD(un)) { 21914 err = ENOTTY; 21915 } else { 21916 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21917 flag); 21918 } 21919 break; 21920 21921 case CDROMSBLKMODE: 21922 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21923 /* 21924 * There is no means of changing block size in case of atapi 21925 * drives, thus return ENOTTY if drive type is atapi 21926 */ 21927 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21928 err = ENOTTY; 21929 } else if (un->un_f_mmc_cap == TRUE) { 21930 21931 /* 21932 * MMC Devices do not support changing the 21933 * logical block size 21934 * 21935 * Note: EINVAL is being returned instead of ENOTTY to 21936 * maintain consistancy with the original mmc 21937 * driver update. 21938 */ 21939 err = EINVAL; 21940 } else { 21941 mutex_enter(SD_MUTEX(un)); 21942 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21943 (un->un_ncmds_in_transport > 0)) { 21944 mutex_exit(SD_MUTEX(un)); 21945 err = EINVAL; 21946 } else { 21947 mutex_exit(SD_MUTEX(un)); 21948 err = sr_change_blkmode(dev, cmd, arg, flag); 21949 } 21950 } 21951 break; 21952 21953 case CDROMGBLKMODE: 21954 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21955 if (!ISCD(un)) { 21956 err = ENOTTY; 21957 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21958 (un->un_f_blockcount_is_valid != FALSE)) { 21959 /* 21960 * Drive is an ATAPI drive so return target block 21961 * size for ATAPI drives since we cannot change the 21962 * blocksize on ATAPI drives. Used primarily to detect 21963 * if an ATAPI cdrom is present. 21964 */ 21965 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21966 sizeof (int), flag) != 0) { 21967 err = EFAULT; 21968 } else { 21969 err = 0; 21970 } 21971 21972 } else { 21973 /* 21974 * Drive supports changing block sizes via a Mode 21975 * Select. 21976 */ 21977 err = sr_change_blkmode(dev, cmd, arg, flag); 21978 } 21979 break; 21980 21981 case CDROMGDRVSPEED: 21982 case CDROMSDRVSPEED: 21983 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21984 if (!ISCD(un)) { 21985 err = ENOTTY; 21986 } else if (un->un_f_mmc_cap == TRUE) { 21987 /* 21988 * Note: In the future the driver implementation 21989 * for getting and 21990 * setting cd speed should entail: 21991 * 1) If non-mmc try the Toshiba mode page 21992 * (sr_change_speed) 21993 * 2) If mmc but no support for Real Time Streaming try 21994 * the SET CD SPEED (0xBB) command 21995 * (sr_atapi_change_speed) 21996 * 3) If mmc and support for Real Time Streaming 21997 * try the GET PERFORMANCE and SET STREAMING 21998 * commands (not yet implemented, 4380808) 21999 */ 22000 /* 22001 * As per recent MMC spec, CD-ROM speed is variable 22002 * and changes with LBA. Since there is no such 22003 * things as drive speed now, fail this ioctl. 22004 * 22005 * Note: EINVAL is returned for consistancy of original 22006 * implementation which included support for getting 22007 * the drive speed of mmc devices but not setting 22008 * the drive speed. Thus EINVAL would be returned 22009 * if a set request was made for an mmc device. 22010 * We no longer support get or set speed for 22011 * mmc but need to remain consistant with regard 22012 * to the error code returned. 22013 */ 22014 err = EINVAL; 22015 } else if (un->un_f_cfg_is_atapi == TRUE) { 22016 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22017 } else { 22018 err = sr_change_speed(dev, cmd, arg, flag); 22019 } 22020 break; 22021 22022 case CDROMCDDA: 22023 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22024 if (!ISCD(un)) { 22025 err = ENOTTY; 22026 } else { 22027 err = sr_read_cdda(dev, (void *)arg, flag); 22028 } 22029 break; 22030 22031 case CDROMCDXA: 22032 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22033 if (!ISCD(un)) { 22034 err = ENOTTY; 22035 } else { 22036 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22037 } 22038 break; 22039 22040 case CDROMSUBCODE: 22041 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22042 if (!ISCD(un)) { 22043 err = ENOTTY; 22044 } else { 22045 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22046 } 22047 break; 22048 22049 case DKIOCPARTINFO: { 22050 /* 22051 * Return parameters describing the selected disk slice. 22052 * Note: this ioctl is for the intel platform only 22053 */ 22054 #if defined(__i386) || defined(__amd64) 22055 int part; 22056 22057 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22058 part = SDPART(dev); 22059 22060 /* don't check un_solaris_size for pN */ 22061 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 22062 err = EIO; 22063 } else { 22064 struct part_info p; 22065 22066 p.p_start = (daddr_t)un->un_offset[part]; 22067 p.p_length = (int)un->un_map[part].dkl_nblk; 22068 #ifdef _MULTI_DATAMODEL 22069 switch (ddi_model_convert_from(flag & FMODELS)) { 22070 case DDI_MODEL_ILP32: 22071 { 22072 struct part_info32 p32; 22073 22074 p32.p_start = (daddr32_t)p.p_start; 22075 p32.p_length = p.p_length; 22076 if (ddi_copyout(&p32, (void *)arg, 22077 sizeof (p32), flag)) 22078 err = EFAULT; 22079 break; 22080 } 22081 22082 case DDI_MODEL_NONE: 22083 { 22084 if (ddi_copyout(&p, (void *)arg, sizeof (p), 22085 flag)) 22086 err = EFAULT; 22087 break; 22088 } 22089 } 22090 #else /* ! _MULTI_DATAMODEL */ 22091 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 22092 err = EFAULT; 22093 #endif /* _MULTI_DATAMODEL */ 22094 } 22095 #else 22096 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22097 err = ENOTTY; 22098 #endif 22099 break; 22100 } 22101 22102 case DKIOCG_PHYGEOM: { 22103 /* Return the driver's notion of the media physical geometry */ 22104 #if defined(__i386) || defined(__amd64) 22105 uint64_t capacity; 22106 struct dk_geom disk_geom; 22107 struct dk_geom *dkgp = &disk_geom; 22108 22109 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22110 mutex_enter(SD_MUTEX(un)); 22111 22112 if (un->un_g.dkg_nhead != 0 && 22113 un->un_g.dkg_nsect != 0) { 22114 /* 22115 * We succeeded in getting a geometry, but 22116 * right now it is being reported as just the 22117 * Solaris fdisk partition, just like for 22118 * DKIOCGGEOM. We need to change that to be 22119 * correct for the entire disk now. 22120 */ 22121 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 22122 dkgp->dkg_acyl = 0; 22123 dkgp->dkg_ncyl = un->un_blockcount / 22124 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22125 } else { 22126 bzero(dkgp, sizeof (struct dk_geom)); 22127 /* 22128 * This disk does not have a Solaris VTOC 22129 * so we must present a physical geometry 22130 * that will remain consistent regardless 22131 * of how the disk is used. This will ensure 22132 * that the geometry does not change regardless 22133 * of the fdisk partition type (ie. EFI, FAT32, 22134 * Solaris, etc). 22135 */ 22136 if (ISCD(un)) { 22137 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 22138 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 22139 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 22140 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 22141 } else { 22142 /* 22143 * Invalid un_blockcount can generate invalid 22144 * dk_geom and may result in division by zero 22145 * system failure. Should make sure blockcount 22146 * is valid before using it here. 22147 */ 22148 if (un->un_f_blockcount_is_valid == FALSE) { 22149 mutex_exit(SD_MUTEX(un)); 22150 err = EIO; 22151 22152 break; 22153 } 22154 22155 /* 22156 * Refer to comments related to off-by-1 at the 22157 * header of this file 22158 */ 22159 if (!un->un_f_capacity_adjusted && 22160 !un->un_f_has_removable_media && 22161 !un->un_f_is_hotpluggable && 22162 (un->un_tgt_blocksize == 22163 un->un_sys_blocksize)) 22164 capacity = un->un_blockcount - 1; 22165 else 22166 capacity = un->un_blockcount; 22167 22168 sd_convert_geometry(capacity, dkgp); 22169 dkgp->dkg_acyl = 0; 22170 dkgp->dkg_ncyl = capacity / 22171 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22172 } 22173 } 22174 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22175 22176 if (ddi_copyout(dkgp, (void *)arg, 22177 sizeof (struct dk_geom), flag)) { 22178 mutex_exit(SD_MUTEX(un)); 22179 err = EFAULT; 22180 } else { 22181 mutex_exit(SD_MUTEX(un)); 22182 err = 0; 22183 } 22184 #else 22185 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22186 err = ENOTTY; 22187 #endif 22188 break; 22189 } 22190 22191 case DKIOCG_VIRTGEOM: { 22192 /* Return the driver's notion of the media's logical geometry */ 22193 #if defined(__i386) || defined(__amd64) 22194 struct dk_geom disk_geom; 22195 struct dk_geom *dkgp = &disk_geom; 22196 22197 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22198 mutex_enter(SD_MUTEX(un)); 22199 /* 22200 * If there is no HBA geometry available, or 22201 * if the HBA returned us something that doesn't 22202 * really fit into an Int 13/function 8 geometry 22203 * result, just fail the ioctl. See PSARC 1998/313. 22204 */ 22205 if (un->un_lgeom.g_nhead == 0 || 22206 un->un_lgeom.g_nsect == 0 || 22207 un->un_lgeom.g_ncyl > 1024) { 22208 mutex_exit(SD_MUTEX(un)); 22209 err = EINVAL; 22210 } else { 22211 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 22212 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 22213 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22214 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 22215 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 22216 22217 if (ddi_copyout(dkgp, (void *)arg, 22218 sizeof (struct dk_geom), flag)) { 22219 mutex_exit(SD_MUTEX(un)); 22220 err = EFAULT; 22221 } else { 22222 mutex_exit(SD_MUTEX(un)); 22223 err = 0; 22224 } 22225 } 22226 #else 22227 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22228 err = ENOTTY; 22229 #endif 22230 break; 22231 } 22232 #ifdef SDDEBUG 22233 /* RESET/ABORTS testing ioctls */ 22234 case DKIOCRESET: { 22235 int reset_level; 22236 22237 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22238 err = EFAULT; 22239 } else { 22240 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22241 "reset_level = 0x%lx\n", reset_level); 22242 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22243 err = 0; 22244 } else { 22245 err = EIO; 22246 } 22247 } 22248 break; 22249 } 22250 22251 case DKIOCABORT: 22252 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22253 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22254 err = 0; 22255 } else { 22256 err = EIO; 22257 } 22258 break; 22259 #endif 22260 22261 #ifdef SD_FAULT_INJECTION 22262 /* SDIOC FaultInjection testing ioctls */ 22263 case SDIOCSTART: 22264 case SDIOCSTOP: 22265 case SDIOCINSERTPKT: 22266 case SDIOCINSERTXB: 22267 case SDIOCINSERTUN: 22268 case SDIOCINSERTARQ: 22269 case SDIOCPUSH: 22270 case SDIOCRETRIEVE: 22271 case SDIOCRUN: 22272 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22273 "SDIOC detected cmd:0x%X:\n", cmd); 22274 /* call error generator */ 22275 sd_faultinjection_ioctl(cmd, arg, un); 22276 err = 0; 22277 break; 22278 22279 #endif /* SD_FAULT_INJECTION */ 22280 22281 case DKIOCFLUSHWRITECACHE: 22282 { 22283 struct dk_callback *dkc = (struct dk_callback *)arg; 22284 22285 mutex_enter(SD_MUTEX(un)); 22286 if (!un->un_f_sync_cache_supported || 22287 !un->un_f_write_cache_enabled) { 22288 err = un->un_f_sync_cache_supported ? 22289 0 : ENOTSUP; 22290 mutex_exit(SD_MUTEX(un)); 22291 if ((flag & FKIOCTL) && dkc != NULL && 22292 dkc->dkc_callback != NULL) { 22293 (*dkc->dkc_callback)(dkc->dkc_cookie, 22294 err); 22295 /* 22296 * Did callback and reported error. 22297 * Since we did a callback, ioctl 22298 * should return 0. 22299 */ 22300 err = 0; 22301 } 22302 break; 22303 } 22304 mutex_exit(SD_MUTEX(un)); 22305 22306 if ((flag & FKIOCTL) && dkc != NULL && 22307 dkc->dkc_callback != NULL) { 22308 /* async SYNC CACHE request */ 22309 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 22310 } else { 22311 /* synchronous SYNC CACHE request */ 22312 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22313 } 22314 } 22315 break; 22316 22317 case DKIOCGETWCE: { 22318 22319 int wce; 22320 22321 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 22322 break; 22323 } 22324 22325 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 22326 err = EFAULT; 22327 } 22328 break; 22329 } 22330 22331 case DKIOCSETWCE: { 22332 22333 int wce, sync_supported; 22334 22335 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 22336 err = EFAULT; 22337 break; 22338 } 22339 22340 /* 22341 * Synchronize multiple threads trying to enable 22342 * or disable the cache via the un_f_wcc_cv 22343 * condition variable. 22344 */ 22345 mutex_enter(SD_MUTEX(un)); 22346 22347 /* 22348 * Don't allow the cache to be enabled if the 22349 * config file has it disabled. 22350 */ 22351 if (un->un_f_opt_disable_cache && wce) { 22352 mutex_exit(SD_MUTEX(un)); 22353 err = EINVAL; 22354 break; 22355 } 22356 22357 /* 22358 * Wait for write cache change in progress 22359 * bit to be clear before proceeding. 22360 */ 22361 while (un->un_f_wcc_inprog) 22362 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22363 22364 un->un_f_wcc_inprog = 1; 22365 22366 if (un->un_f_write_cache_enabled && wce == 0) { 22367 /* 22368 * Disable the write cache. Don't clear 22369 * un_f_write_cache_enabled until after 22370 * the mode select and flush are complete. 22371 */ 22372 sync_supported = un->un_f_sync_cache_supported; 22373 mutex_exit(SD_MUTEX(un)); 22374 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22375 SD_CACHE_DISABLE)) == 0 && sync_supported) { 22376 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22377 } 22378 22379 mutex_enter(SD_MUTEX(un)); 22380 if (err == 0) { 22381 un->un_f_write_cache_enabled = 0; 22382 } 22383 22384 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22385 /* 22386 * Set un_f_write_cache_enabled first, so there is 22387 * no window where the cache is enabled, but the 22388 * bit says it isn't. 22389 */ 22390 un->un_f_write_cache_enabled = 1; 22391 mutex_exit(SD_MUTEX(un)); 22392 22393 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22394 SD_CACHE_ENABLE); 22395 22396 mutex_enter(SD_MUTEX(un)); 22397 22398 if (err) { 22399 un->un_f_write_cache_enabled = 0; 22400 } 22401 } 22402 22403 un->un_f_wcc_inprog = 0; 22404 cv_broadcast(&un->un_wcc_cv); 22405 mutex_exit(SD_MUTEX(un)); 22406 break; 22407 } 22408 22409 default: 22410 err = ENOTTY; 22411 break; 22412 } 22413 mutex_enter(SD_MUTEX(un)); 22414 un->un_ncmds_in_driver--; 22415 ASSERT(un->un_ncmds_in_driver >= 0); 22416 mutex_exit(SD_MUTEX(un)); 22417 22418 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22419 return (err); 22420 } 22421 22422 22423 /* 22424 * Function: sd_uscsi_ioctl 22425 * 22426 * Description: This routine is the driver entry point for handling USCSI ioctl 22427 * requests (USCSICMD). 22428 * 22429 * Arguments: dev - the device number 22430 * arg - user provided scsi command 22431 * flag - this argument is a pass through to ddi_copyxxx() 22432 * directly from the mode argument of ioctl(). 22433 * 22434 * Return Code: code returned by sd_send_scsi_cmd 22435 * ENXIO 22436 * EFAULT 22437 * EAGAIN 22438 */ 22439 22440 static int 22441 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 22442 { 22443 #ifdef _MULTI_DATAMODEL 22444 /* 22445 * For use when a 32 bit app makes a call into a 22446 * 64 bit ioctl 22447 */ 22448 struct uscsi_cmd32 uscsi_cmd_32_for_64; 22449 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 22450 model_t model; 22451 #endif /* _MULTI_DATAMODEL */ 22452 struct uscsi_cmd *scmd = NULL; 22453 struct sd_lun *un = NULL; 22454 enum uio_seg uioseg; 22455 char cdb[CDB_GROUP0]; 22456 int rval = 0; 22457 22458 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22459 return (ENXIO); 22460 } 22461 22462 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 22463 22464 scmd = (struct uscsi_cmd *) 22465 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 22466 22467 #ifdef _MULTI_DATAMODEL 22468 switch (model = ddi_model_convert_from(flag & FMODELS)) { 22469 case DDI_MODEL_ILP32: 22470 { 22471 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 22472 rval = EFAULT; 22473 goto done; 22474 } 22475 /* 22476 * Convert the ILP32 uscsi data from the 22477 * application to LP64 for internal use. 22478 */ 22479 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 22480 break; 22481 } 22482 case DDI_MODEL_NONE: 22483 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22484 rval = EFAULT; 22485 goto done; 22486 } 22487 break; 22488 } 22489 #else /* ! _MULTI_DATAMODEL */ 22490 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22491 rval = EFAULT; 22492 goto done; 22493 } 22494 #endif /* _MULTI_DATAMODEL */ 22495 22496 scmd->uscsi_flags &= ~USCSI_NOINTR; 22497 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22498 if (un->un_f_format_in_progress == TRUE) { 22499 rval = EAGAIN; 22500 goto done; 22501 } 22502 22503 /* 22504 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22505 * we will have a valid cdb[0] to test. 22506 */ 22507 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22508 (cdb[0] == SCMD_FORMAT)) { 22509 SD_TRACE(SD_LOG_IOCTL, un, 22510 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22511 mutex_enter(SD_MUTEX(un)); 22512 un->un_f_format_in_progress = TRUE; 22513 mutex_exit(SD_MUTEX(un)); 22514 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22515 SD_PATH_STANDARD); 22516 mutex_enter(SD_MUTEX(un)); 22517 un->un_f_format_in_progress = FALSE; 22518 mutex_exit(SD_MUTEX(un)); 22519 } else { 22520 SD_TRACE(SD_LOG_IOCTL, un, 22521 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22522 /* 22523 * It's OK to fall into here even if the ddi_copyin() 22524 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22525 * does this same copyin and will return the EFAULT 22526 * if it fails. 22527 */ 22528 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22529 SD_PATH_STANDARD); 22530 } 22531 #ifdef _MULTI_DATAMODEL 22532 switch (model) { 22533 case DDI_MODEL_ILP32: 22534 /* 22535 * Convert back to ILP32 before copyout to the 22536 * application 22537 */ 22538 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22539 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22540 if (rval != 0) { 22541 rval = EFAULT; 22542 } 22543 } 22544 break; 22545 case DDI_MODEL_NONE: 22546 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22547 if (rval != 0) { 22548 rval = EFAULT; 22549 } 22550 } 22551 break; 22552 } 22553 #else /* ! _MULTI_DATAMODE */ 22554 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22555 if (rval != 0) { 22556 rval = EFAULT; 22557 } 22558 } 22559 #endif /* _MULTI_DATAMODE */ 22560 done: 22561 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22562 22563 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22564 22565 return (rval); 22566 } 22567 22568 22569 /* 22570 * Function: sd_dkio_ctrl_info 22571 * 22572 * Description: This routine is the driver entry point for handling controller 22573 * information ioctl requests (DKIOCINFO). 22574 * 22575 * Arguments: dev - the device number 22576 * arg - pointer to user provided dk_cinfo structure 22577 * specifying the controller type and attributes. 22578 * flag - this argument is a pass through to ddi_copyxxx() 22579 * directly from the mode argument of ioctl(). 22580 * 22581 * Return Code: 0 22582 * EFAULT 22583 * ENXIO 22584 */ 22585 22586 static int 22587 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22588 { 22589 struct sd_lun *un = NULL; 22590 struct dk_cinfo *info; 22591 dev_info_t *pdip; 22592 int lun, tgt; 22593 22594 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22595 return (ENXIO); 22596 } 22597 22598 info = (struct dk_cinfo *) 22599 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22600 22601 switch (un->un_ctype) { 22602 case CTYPE_CDROM: 22603 info->dki_ctype = DKC_CDROM; 22604 break; 22605 default: 22606 info->dki_ctype = DKC_SCSI_CCS; 22607 break; 22608 } 22609 pdip = ddi_get_parent(SD_DEVINFO(un)); 22610 info->dki_cnum = ddi_get_instance(pdip); 22611 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22612 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22613 } else { 22614 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22615 DK_DEVLEN - 1); 22616 } 22617 22618 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22619 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22620 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22621 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22622 22623 /* Unit Information */ 22624 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22625 info->dki_slave = ((tgt << 3) | lun); 22626 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22627 DK_DEVLEN - 1); 22628 info->dki_flags = DKI_FMTVOL; 22629 info->dki_partition = SDPART(dev); 22630 22631 /* Max Transfer size of this device in blocks */ 22632 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22633 info->dki_addr = 0; 22634 info->dki_space = 0; 22635 info->dki_prio = 0; 22636 info->dki_vec = 0; 22637 22638 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22639 kmem_free(info, sizeof (struct dk_cinfo)); 22640 return (EFAULT); 22641 } else { 22642 kmem_free(info, sizeof (struct dk_cinfo)); 22643 return (0); 22644 } 22645 } 22646 22647 22648 /* 22649 * Function: sd_get_media_info 22650 * 22651 * Description: This routine is the driver entry point for handling ioctl 22652 * requests for the media type or command set profile used by the 22653 * drive to operate on the media (DKIOCGMEDIAINFO). 22654 * 22655 * Arguments: dev - the device number 22656 * arg - pointer to user provided dk_minfo structure 22657 * specifying the media type, logical block size and 22658 * drive capacity. 22659 * flag - this argument is a pass through to ddi_copyxxx() 22660 * directly from the mode argument of ioctl(). 22661 * 22662 * Return Code: 0 22663 * EACCESS 22664 * EFAULT 22665 * ENXIO 22666 * EIO 22667 */ 22668 22669 static int 22670 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22671 { 22672 struct sd_lun *un = NULL; 22673 struct uscsi_cmd com; 22674 struct scsi_inquiry *sinq; 22675 struct dk_minfo media_info; 22676 u_longlong_t media_capacity; 22677 uint64_t capacity; 22678 uint_t lbasize; 22679 uchar_t *out_data; 22680 uchar_t *rqbuf; 22681 int rval = 0; 22682 int rtn; 22683 22684 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22685 (un->un_state == SD_STATE_OFFLINE)) { 22686 return (ENXIO); 22687 } 22688 22689 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22690 22691 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22692 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22693 22694 /* Issue a TUR to determine if the drive is ready with media present */ 22695 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22696 if (rval == ENXIO) { 22697 goto done; 22698 } 22699 22700 /* Now get configuration data */ 22701 if (ISCD(un)) { 22702 media_info.dki_media_type = DK_CDROM; 22703 22704 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22705 if (un->un_f_mmc_cap == TRUE) { 22706 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22707 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22708 22709 if (rtn) { 22710 /* 22711 * Failed for other than an illegal request 22712 * or command not supported 22713 */ 22714 if ((com.uscsi_status == STATUS_CHECK) && 22715 (com.uscsi_rqstatus == STATUS_GOOD)) { 22716 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22717 (rqbuf[12] != 0x20)) { 22718 rval = EIO; 22719 goto done; 22720 } 22721 } 22722 } else { 22723 /* 22724 * The GET CONFIGURATION command succeeded 22725 * so set the media type according to the 22726 * returned data 22727 */ 22728 media_info.dki_media_type = out_data[6]; 22729 media_info.dki_media_type <<= 8; 22730 media_info.dki_media_type |= out_data[7]; 22731 } 22732 } 22733 } else { 22734 /* 22735 * The profile list is not available, so we attempt to identify 22736 * the media type based on the inquiry data 22737 */ 22738 sinq = un->un_sd->sd_inq; 22739 if (sinq->inq_qual == 0) { 22740 /* This is a direct access device */ 22741 media_info.dki_media_type = DK_FIXED_DISK; 22742 22743 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22744 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22745 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22746 media_info.dki_media_type = DK_ZIP; 22747 } else if ( 22748 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22749 media_info.dki_media_type = DK_JAZ; 22750 } 22751 } 22752 } else { 22753 /* Not a CD or direct access so return unknown media */ 22754 media_info.dki_media_type = DK_UNKNOWN; 22755 } 22756 } 22757 22758 /* Now read the capacity so we can provide the lbasize and capacity */ 22759 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22760 SD_PATH_DIRECT)) { 22761 case 0: 22762 break; 22763 case EACCES: 22764 rval = EACCES; 22765 goto done; 22766 default: 22767 rval = EIO; 22768 goto done; 22769 } 22770 22771 media_info.dki_lbsize = lbasize; 22772 media_capacity = capacity; 22773 22774 /* 22775 * sd_send_scsi_READ_CAPACITY() reports capacity in 22776 * un->un_sys_blocksize chunks. So we need to convert it into 22777 * cap.lbasize chunks. 22778 */ 22779 media_capacity *= un->un_sys_blocksize; 22780 media_capacity /= lbasize; 22781 media_info.dki_capacity = media_capacity; 22782 22783 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22784 rval = EFAULT; 22785 /* Put goto. Anybody might add some code below in future */ 22786 goto done; 22787 } 22788 done: 22789 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22790 kmem_free(rqbuf, SENSE_LENGTH); 22791 return (rval); 22792 } 22793 22794 22795 /* 22796 * Function: sd_dkio_get_geometry 22797 * 22798 * Description: This routine is the driver entry point for handling user 22799 * requests to get the device geometry (DKIOCGGEOM). 22800 * 22801 * Arguments: dev - the device number 22802 * arg - pointer to user provided dk_geom structure specifying 22803 * the controller's notion of the current geometry. 22804 * flag - this argument is a pass through to ddi_copyxxx() 22805 * directly from the mode argument of ioctl(). 22806 * geom_validated - flag indicating if the device geometry has been 22807 * previously validated in the sdioctl routine. 22808 * 22809 * Return Code: 0 22810 * EFAULT 22811 * ENXIO 22812 * EIO 22813 */ 22814 22815 static int 22816 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22817 { 22818 struct sd_lun *un = NULL; 22819 struct dk_geom *tmp_geom = NULL; 22820 int rval = 0; 22821 22822 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22823 return (ENXIO); 22824 } 22825 22826 if (geom_validated == FALSE) { 22827 /* 22828 * sd_validate_geometry does not spin a disk up 22829 * if it was spun down. We need to make sure it 22830 * is ready. 22831 */ 22832 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22833 return (rval); 22834 } 22835 mutex_enter(SD_MUTEX(un)); 22836 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22837 mutex_exit(SD_MUTEX(un)); 22838 } 22839 if (rval) 22840 return (rval); 22841 22842 /* 22843 * It is possible that un_solaris_size is 0(uninitialized) 22844 * after sd_unit_attach. Reservation conflict may cause the 22845 * above situation. Thus, the zero check of un_solaris_size 22846 * should occur after the sd_validate_geometry() call. 22847 */ 22848 #if defined(__i386) || defined(__amd64) 22849 if (un->un_solaris_size == 0) { 22850 return (EIO); 22851 } 22852 #endif 22853 22854 /* 22855 * Make a local copy of the soft state geometry to avoid some potential 22856 * race conditions associated with holding the mutex and updating the 22857 * write_reinstruct value 22858 */ 22859 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22860 mutex_enter(SD_MUTEX(un)); 22861 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22862 mutex_exit(SD_MUTEX(un)); 22863 22864 if (tmp_geom->dkg_write_reinstruct == 0) { 22865 tmp_geom->dkg_write_reinstruct = 22866 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22867 sd_rot_delay) / (int)60000); 22868 } 22869 22870 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22871 flag); 22872 if (rval != 0) { 22873 rval = EFAULT; 22874 } 22875 22876 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22877 return (rval); 22878 22879 } 22880 22881 22882 /* 22883 * Function: sd_dkio_set_geometry 22884 * 22885 * Description: This routine is the driver entry point for handling user 22886 * requests to set the device geometry (DKIOCSGEOM). The actual 22887 * device geometry is not updated, just the driver "notion" of it. 22888 * 22889 * Arguments: dev - the device number 22890 * arg - pointer to user provided dk_geom structure used to set 22891 * the controller's notion of the current geometry. 22892 * flag - this argument is a pass through to ddi_copyxxx() 22893 * directly from the mode argument of ioctl(). 22894 * 22895 * Return Code: 0 22896 * EFAULT 22897 * ENXIO 22898 * EIO 22899 */ 22900 22901 static int 22902 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22903 { 22904 struct sd_lun *un = NULL; 22905 struct dk_geom *tmp_geom; 22906 struct dk_map *lp; 22907 int rval = 0; 22908 int i; 22909 22910 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22911 return (ENXIO); 22912 } 22913 22914 /* 22915 * Make sure there is no reservation conflict on the lun. 22916 */ 22917 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22918 return (EACCES); 22919 } 22920 22921 #if defined(__i386) || defined(__amd64) 22922 if (un->un_solaris_size == 0) { 22923 return (EIO); 22924 } 22925 #endif 22926 22927 /* 22928 * We need to copy the user specified geometry into local 22929 * storage and then update the softstate. We don't want to hold 22930 * the mutex and copyin directly from the user to the soft state 22931 */ 22932 tmp_geom = (struct dk_geom *) 22933 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22934 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22935 if (rval != 0) { 22936 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22937 return (EFAULT); 22938 } 22939 22940 mutex_enter(SD_MUTEX(un)); 22941 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22942 for (i = 0; i < NDKMAP; i++) { 22943 lp = &un->un_map[i]; 22944 un->un_offset[i] = 22945 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22946 #if defined(__i386) || defined(__amd64) 22947 un->un_offset[i] += un->un_solaris_offset; 22948 #endif 22949 } 22950 un->un_f_geometry_is_valid = FALSE; 22951 mutex_exit(SD_MUTEX(un)); 22952 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22953 22954 return (rval); 22955 } 22956 22957 22958 /* 22959 * Function: sd_dkio_get_partition 22960 * 22961 * Description: This routine is the driver entry point for handling user 22962 * requests to get the partition table (DKIOCGAPART). 22963 * 22964 * Arguments: dev - the device number 22965 * arg - pointer to user provided dk_allmap structure specifying 22966 * the controller's notion of the current partition table. 22967 * flag - this argument is a pass through to ddi_copyxxx() 22968 * directly from the mode argument of ioctl(). 22969 * geom_validated - flag indicating if the device geometry has been 22970 * previously validated in the sdioctl routine. 22971 * 22972 * Return Code: 0 22973 * EFAULT 22974 * ENXIO 22975 * EIO 22976 */ 22977 22978 static int 22979 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22980 { 22981 struct sd_lun *un = NULL; 22982 int rval = 0; 22983 int size; 22984 22985 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22986 return (ENXIO); 22987 } 22988 22989 /* 22990 * Make sure the geometry is valid before getting the partition 22991 * information. 22992 */ 22993 mutex_enter(SD_MUTEX(un)); 22994 if (geom_validated == FALSE) { 22995 /* 22996 * sd_validate_geometry does not spin a disk up 22997 * if it was spun down. We need to make sure it 22998 * is ready before validating the geometry. 22999 */ 23000 mutex_exit(SD_MUTEX(un)); 23001 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23002 return (rval); 23003 } 23004 mutex_enter(SD_MUTEX(un)); 23005 23006 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23007 mutex_exit(SD_MUTEX(un)); 23008 return (rval); 23009 } 23010 } 23011 mutex_exit(SD_MUTEX(un)); 23012 23013 /* 23014 * It is possible that un_solaris_size is 0(uninitialized) 23015 * after sd_unit_attach. Reservation conflict may cause the 23016 * above situation. Thus, the zero check of un_solaris_size 23017 * should occur after the sd_validate_geometry() call. 23018 */ 23019 #if defined(__i386) || defined(__amd64) 23020 if (un->un_solaris_size == 0) { 23021 return (EIO); 23022 } 23023 #endif 23024 23025 #ifdef _MULTI_DATAMODEL 23026 switch (ddi_model_convert_from(flag & FMODELS)) { 23027 case DDI_MODEL_ILP32: { 23028 struct dk_map32 dk_map32[NDKMAP]; 23029 int i; 23030 23031 for (i = 0; i < NDKMAP; i++) { 23032 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 23033 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 23034 } 23035 size = NDKMAP * sizeof (struct dk_map32); 23036 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 23037 if (rval != 0) { 23038 rval = EFAULT; 23039 } 23040 break; 23041 } 23042 case DDI_MODEL_NONE: 23043 size = NDKMAP * sizeof (struct dk_map); 23044 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23045 if (rval != 0) { 23046 rval = EFAULT; 23047 } 23048 break; 23049 } 23050 #else /* ! _MULTI_DATAMODEL */ 23051 size = NDKMAP * sizeof (struct dk_map); 23052 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23053 if (rval != 0) { 23054 rval = EFAULT; 23055 } 23056 #endif /* _MULTI_DATAMODEL */ 23057 return (rval); 23058 } 23059 23060 23061 /* 23062 * Function: sd_dkio_set_partition 23063 * 23064 * Description: This routine is the driver entry point for handling user 23065 * requests to set the partition table (DKIOCSAPART). The actual 23066 * device partition is not updated. 23067 * 23068 * Arguments: dev - the device number 23069 * arg - pointer to user provided dk_allmap structure used to set 23070 * the controller's notion of the partition table. 23071 * flag - this argument is a pass through to ddi_copyxxx() 23072 * directly from the mode argument of ioctl(). 23073 * 23074 * Return Code: 0 23075 * EINVAL 23076 * EFAULT 23077 * ENXIO 23078 * EIO 23079 */ 23080 23081 static int 23082 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 23083 { 23084 struct sd_lun *un = NULL; 23085 struct dk_map dk_map[NDKMAP]; 23086 struct dk_map *lp; 23087 int rval = 0; 23088 int size; 23089 int i; 23090 #if defined(_SUNOS_VTOC_16) 23091 struct dkl_partition *vp; 23092 #endif 23093 23094 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23095 return (ENXIO); 23096 } 23097 23098 /* 23099 * Set the map for all logical partitions. We lock 23100 * the priority just to make sure an interrupt doesn't 23101 * come in while the map is half updated. 23102 */ 23103 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 23104 mutex_enter(SD_MUTEX(un)); 23105 if (un->un_blockcount > DK_MAX_BLOCKS) { 23106 mutex_exit(SD_MUTEX(un)); 23107 return (ENOTSUP); 23108 } 23109 mutex_exit(SD_MUTEX(un)); 23110 23111 /* 23112 * Make sure there is no reservation conflict on the lun. 23113 */ 23114 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 23115 return (EACCES); 23116 } 23117 23118 #if defined(__i386) || defined(__amd64) 23119 if (un->un_solaris_size == 0) { 23120 return (EIO); 23121 } 23122 #endif 23123 23124 #ifdef _MULTI_DATAMODEL 23125 switch (ddi_model_convert_from(flag & FMODELS)) { 23126 case DDI_MODEL_ILP32: { 23127 struct dk_map32 dk_map32[NDKMAP]; 23128 23129 size = NDKMAP * sizeof (struct dk_map32); 23130 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 23131 if (rval != 0) { 23132 return (EFAULT); 23133 } 23134 for (i = 0; i < NDKMAP; i++) { 23135 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 23136 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 23137 } 23138 break; 23139 } 23140 case DDI_MODEL_NONE: 23141 size = NDKMAP * sizeof (struct dk_map); 23142 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23143 if (rval != 0) { 23144 return (EFAULT); 23145 } 23146 break; 23147 } 23148 #else /* ! _MULTI_DATAMODEL */ 23149 size = NDKMAP * sizeof (struct dk_map); 23150 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23151 if (rval != 0) { 23152 return (EFAULT); 23153 } 23154 #endif /* _MULTI_DATAMODEL */ 23155 23156 mutex_enter(SD_MUTEX(un)); 23157 /* Note: The size used in this bcopy is set based upon the data model */ 23158 bcopy(dk_map, un->un_map, size); 23159 #if defined(_SUNOS_VTOC_16) 23160 vp = (struct dkl_partition *)&(un->un_vtoc); 23161 #endif /* defined(_SUNOS_VTOC_16) */ 23162 for (i = 0; i < NDKMAP; i++) { 23163 lp = &un->un_map[i]; 23164 un->un_offset[i] = 23165 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23166 #if defined(_SUNOS_VTOC_16) 23167 vp->p_start = un->un_offset[i]; 23168 vp->p_size = lp->dkl_nblk; 23169 vp++; 23170 #endif /* defined(_SUNOS_VTOC_16) */ 23171 #if defined(__i386) || defined(__amd64) 23172 un->un_offset[i] += un->un_solaris_offset; 23173 #endif 23174 } 23175 mutex_exit(SD_MUTEX(un)); 23176 return (rval); 23177 } 23178 23179 23180 /* 23181 * Function: sd_dkio_get_vtoc 23182 * 23183 * Description: This routine is the driver entry point for handling user 23184 * requests to get the current volume table of contents 23185 * (DKIOCGVTOC). 23186 * 23187 * Arguments: dev - the device number 23188 * arg - pointer to user provided vtoc structure specifying 23189 * the current vtoc. 23190 * flag - this argument is a pass through to ddi_copyxxx() 23191 * directly from the mode argument of ioctl(). 23192 * geom_validated - flag indicating if the device geometry has been 23193 * previously validated in the sdioctl routine. 23194 * 23195 * Return Code: 0 23196 * EFAULT 23197 * ENXIO 23198 * EIO 23199 */ 23200 23201 static int 23202 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 23203 { 23204 struct sd_lun *un = NULL; 23205 #if defined(_SUNOS_VTOC_8) 23206 struct vtoc user_vtoc; 23207 #endif /* defined(_SUNOS_VTOC_8) */ 23208 int rval = 0; 23209 23210 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23211 return (ENXIO); 23212 } 23213 23214 mutex_enter(SD_MUTEX(un)); 23215 if (geom_validated == FALSE) { 23216 /* 23217 * sd_validate_geometry does not spin a disk up 23218 * if it was spun down. We need to make sure it 23219 * is ready. 23220 */ 23221 mutex_exit(SD_MUTEX(un)); 23222 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23223 return (rval); 23224 } 23225 mutex_enter(SD_MUTEX(un)); 23226 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23227 mutex_exit(SD_MUTEX(un)); 23228 return (rval); 23229 } 23230 } 23231 23232 #if defined(_SUNOS_VTOC_8) 23233 sd_build_user_vtoc(un, &user_vtoc); 23234 mutex_exit(SD_MUTEX(un)); 23235 23236 #ifdef _MULTI_DATAMODEL 23237 switch (ddi_model_convert_from(flag & FMODELS)) { 23238 case DDI_MODEL_ILP32: { 23239 struct vtoc32 user_vtoc32; 23240 23241 vtoctovtoc32(user_vtoc, user_vtoc32); 23242 if (ddi_copyout(&user_vtoc32, (void *)arg, 23243 sizeof (struct vtoc32), flag)) { 23244 return (EFAULT); 23245 } 23246 break; 23247 } 23248 23249 case DDI_MODEL_NONE: 23250 if (ddi_copyout(&user_vtoc, (void *)arg, 23251 sizeof (struct vtoc), flag)) { 23252 return (EFAULT); 23253 } 23254 break; 23255 } 23256 #else /* ! _MULTI_DATAMODEL */ 23257 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 23258 return (EFAULT); 23259 } 23260 #endif /* _MULTI_DATAMODEL */ 23261 23262 #elif defined(_SUNOS_VTOC_16) 23263 mutex_exit(SD_MUTEX(un)); 23264 23265 #ifdef _MULTI_DATAMODEL 23266 /* 23267 * The un_vtoc structure is a "struct dk_vtoc" which is always 23268 * 32-bit to maintain compatibility with existing on-disk 23269 * structures. Thus, we need to convert the structure when copying 23270 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 23271 * program. If the target is a 32-bit program, then no conversion 23272 * is necessary. 23273 */ 23274 /* LINTED: logical expression always true: op "||" */ 23275 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 23276 switch (ddi_model_convert_from(flag & FMODELS)) { 23277 case DDI_MODEL_ILP32: 23278 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 23279 sizeof (un->un_vtoc), flag)) { 23280 return (EFAULT); 23281 } 23282 break; 23283 23284 case DDI_MODEL_NONE: { 23285 struct vtoc user_vtoc; 23286 23287 vtoc32tovtoc(un->un_vtoc, user_vtoc); 23288 if (ddi_copyout(&user_vtoc, (void *)arg, 23289 sizeof (struct vtoc), flag)) { 23290 return (EFAULT); 23291 } 23292 break; 23293 } 23294 } 23295 #else /* ! _MULTI_DATAMODEL */ 23296 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 23297 flag)) { 23298 return (EFAULT); 23299 } 23300 #endif /* _MULTI_DATAMODEL */ 23301 #else 23302 #error "No VTOC format defined." 23303 #endif 23304 23305 return (rval); 23306 } 23307 23308 static int 23309 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 23310 { 23311 struct sd_lun *un = NULL; 23312 dk_efi_t user_efi; 23313 int rval = 0; 23314 void *buffer; 23315 23316 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23317 return (ENXIO); 23318 23319 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23320 return (EFAULT); 23321 23322 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23323 23324 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23325 (user_efi.dki_length > un->un_max_xfer_size)) 23326 return (EINVAL); 23327 23328 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23329 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 23330 user_efi.dki_lba, SD_PATH_DIRECT); 23331 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 23332 user_efi.dki_length, flag) != 0) 23333 rval = EFAULT; 23334 23335 kmem_free(buffer, user_efi.dki_length); 23336 return (rval); 23337 } 23338 23339 /* 23340 * Function: sd_build_user_vtoc 23341 * 23342 * Description: This routine populates a pass by reference variable with the 23343 * current volume table of contents. 23344 * 23345 * Arguments: un - driver soft state (unit) structure 23346 * user_vtoc - pointer to vtoc structure to be populated 23347 */ 23348 23349 static void 23350 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23351 { 23352 struct dk_map2 *lpart; 23353 struct dk_map *lmap; 23354 struct partition *vpart; 23355 int nblks; 23356 int i; 23357 23358 ASSERT(mutex_owned(SD_MUTEX(un))); 23359 23360 /* 23361 * Return vtoc structure fields in the provided VTOC area, addressed 23362 * by *vtoc. 23363 */ 23364 bzero(user_vtoc, sizeof (struct vtoc)); 23365 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 23366 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 23367 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 23368 user_vtoc->v_sanity = VTOC_SANE; 23369 user_vtoc->v_version = un->un_vtoc.v_version; 23370 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 23371 user_vtoc->v_sectorsz = un->un_sys_blocksize; 23372 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 23373 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 23374 sizeof (un->un_vtoc.v_reserved)); 23375 /* 23376 * Convert partitioning information. 23377 * 23378 * Note the conversion from starting cylinder number 23379 * to starting sector number. 23380 */ 23381 lmap = un->un_map; 23382 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 23383 vpart = user_vtoc->v_part; 23384 23385 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23386 23387 for (i = 0; i < V_NUMPAR; i++) { 23388 vpart->p_tag = lpart->p_tag; 23389 vpart->p_flag = lpart->p_flag; 23390 vpart->p_start = lmap->dkl_cylno * nblks; 23391 vpart->p_size = lmap->dkl_nblk; 23392 lmap++; 23393 lpart++; 23394 vpart++; 23395 23396 /* (4364927) */ 23397 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 23398 } 23399 23400 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 23401 } 23402 23403 static int 23404 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 23405 { 23406 struct sd_lun *un = NULL; 23407 struct partition64 p64; 23408 int rval = 0; 23409 uint_t nparts; 23410 efi_gpe_t *partitions; 23411 efi_gpt_t *buffer; 23412 diskaddr_t gpe_lba; 23413 23414 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23415 return (ENXIO); 23416 } 23417 23418 if (ddi_copyin((const void *)arg, &p64, 23419 sizeof (struct partition64), flag)) { 23420 return (EFAULT); 23421 } 23422 23423 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23424 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23425 1, SD_PATH_DIRECT); 23426 if (rval != 0) 23427 goto done_error; 23428 23429 sd_swap_efi_gpt(buffer); 23430 23431 if ((rval = sd_validate_efi(buffer)) != 0) 23432 goto done_error; 23433 23434 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23435 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23436 if (p64.p_partno > nparts) { 23437 /* couldn't find it */ 23438 rval = ESRCH; 23439 goto done_error; 23440 } 23441 /* 23442 * if we're dealing with a partition that's out of the normal 23443 * 16K block, adjust accordingly 23444 */ 23445 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23446 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23447 gpe_lba, SD_PATH_DIRECT); 23448 if (rval) { 23449 goto done_error; 23450 } 23451 partitions = (efi_gpe_t *)buffer; 23452 23453 sd_swap_efi_gpe(nparts, partitions); 23454 23455 partitions += p64.p_partno; 23456 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23457 sizeof (struct uuid)); 23458 p64.p_start = partitions->efi_gpe_StartingLBA; 23459 p64.p_size = partitions->efi_gpe_EndingLBA - 23460 p64.p_start + 1; 23461 23462 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23463 rval = EFAULT; 23464 23465 done_error: 23466 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23467 return (rval); 23468 } 23469 23470 23471 /* 23472 * Function: sd_dkio_set_vtoc 23473 * 23474 * Description: This routine is the driver entry point for handling user 23475 * requests to set the current volume table of contents 23476 * (DKIOCSVTOC). 23477 * 23478 * Arguments: dev - the device number 23479 * arg - pointer to user provided vtoc structure used to set the 23480 * current vtoc. 23481 * flag - this argument is a pass through to ddi_copyxxx() 23482 * directly from the mode argument of ioctl(). 23483 * 23484 * Return Code: 0 23485 * EFAULT 23486 * ENXIO 23487 * EINVAL 23488 * ENOTSUP 23489 */ 23490 23491 static int 23492 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23493 { 23494 struct sd_lun *un = NULL; 23495 struct vtoc user_vtoc; 23496 int rval = 0; 23497 23498 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23499 return (ENXIO); 23500 } 23501 23502 #if defined(__i386) || defined(__amd64) 23503 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23504 return (EINVAL); 23505 } 23506 #endif 23507 23508 #ifdef _MULTI_DATAMODEL 23509 switch (ddi_model_convert_from(flag & FMODELS)) { 23510 case DDI_MODEL_ILP32: { 23511 struct vtoc32 user_vtoc32; 23512 23513 if (ddi_copyin((const void *)arg, &user_vtoc32, 23514 sizeof (struct vtoc32), flag)) { 23515 return (EFAULT); 23516 } 23517 vtoc32tovtoc(user_vtoc32, user_vtoc); 23518 break; 23519 } 23520 23521 case DDI_MODEL_NONE: 23522 if (ddi_copyin((const void *)arg, &user_vtoc, 23523 sizeof (struct vtoc), flag)) { 23524 return (EFAULT); 23525 } 23526 break; 23527 } 23528 #else /* ! _MULTI_DATAMODEL */ 23529 if (ddi_copyin((const void *)arg, &user_vtoc, 23530 sizeof (struct vtoc), flag)) { 23531 return (EFAULT); 23532 } 23533 #endif /* _MULTI_DATAMODEL */ 23534 23535 mutex_enter(SD_MUTEX(un)); 23536 if (un->un_blockcount > DK_MAX_BLOCKS) { 23537 mutex_exit(SD_MUTEX(un)); 23538 return (ENOTSUP); 23539 } 23540 if (un->un_g.dkg_ncyl == 0) { 23541 mutex_exit(SD_MUTEX(un)); 23542 return (EINVAL); 23543 } 23544 23545 mutex_exit(SD_MUTEX(un)); 23546 sd_clear_efi(un); 23547 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23548 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23549 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23550 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23551 un->un_node_type, NULL); 23552 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23553 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23554 un->un_node_type, NULL); 23555 mutex_enter(SD_MUTEX(un)); 23556 23557 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23558 if ((rval = sd_write_label(dev)) == 0) { 23559 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23560 != 0) { 23561 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23562 "sd_dkio_set_vtoc: " 23563 "Failed validate geometry\n"); 23564 } 23565 } 23566 } 23567 23568 /* 23569 * If sd_build_label_vtoc, or sd_write_label failed above write the 23570 * devid anyway, what can it hurt? Also preserve the device id by 23571 * writing to the disk acyl for the case where a devid has been 23572 * fabricated. 23573 */ 23574 if (un->un_f_devid_supported && 23575 (un->un_f_opt_fab_devid == TRUE)) { 23576 if (un->un_devid == NULL) { 23577 sd_register_devid(un, SD_DEVINFO(un), 23578 SD_TARGET_IS_UNRESERVED); 23579 } else { 23580 /* 23581 * The device id for this disk has been 23582 * fabricated. Fabricated device id's are 23583 * managed by storing them in the last 2 23584 * available sectors on the drive. The device 23585 * id must be preserved by writing it back out 23586 * to this location. 23587 */ 23588 if (sd_write_deviceid(un) != 0) { 23589 ddi_devid_free(un->un_devid); 23590 un->un_devid = NULL; 23591 } 23592 } 23593 } 23594 mutex_exit(SD_MUTEX(un)); 23595 return (rval); 23596 } 23597 23598 23599 /* 23600 * Function: sd_build_label_vtoc 23601 * 23602 * Description: This routine updates the driver soft state current volume table 23603 * of contents based on a user specified vtoc. 23604 * 23605 * Arguments: un - driver soft state (unit) structure 23606 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23607 * to update the driver soft state. 23608 * 23609 * Return Code: 0 23610 * EINVAL 23611 */ 23612 23613 static int 23614 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23615 { 23616 struct dk_map *lmap; 23617 struct partition *vpart; 23618 int nblks; 23619 #if defined(_SUNOS_VTOC_8) 23620 int ncyl; 23621 struct dk_map2 *lpart; 23622 #endif /* defined(_SUNOS_VTOC_8) */ 23623 int i; 23624 23625 ASSERT(mutex_owned(SD_MUTEX(un))); 23626 23627 /* Sanity-check the vtoc */ 23628 if (user_vtoc->v_sanity != VTOC_SANE || 23629 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23630 user_vtoc->v_nparts != V_NUMPAR) { 23631 return (EINVAL); 23632 } 23633 23634 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23635 if (nblks == 0) { 23636 return (EINVAL); 23637 } 23638 23639 #if defined(_SUNOS_VTOC_8) 23640 vpart = user_vtoc->v_part; 23641 for (i = 0; i < V_NUMPAR; i++) { 23642 if ((vpart->p_start % nblks) != 0) { 23643 return (EINVAL); 23644 } 23645 ncyl = vpart->p_start / nblks; 23646 ncyl += vpart->p_size / nblks; 23647 if ((vpart->p_size % nblks) != 0) { 23648 ncyl++; 23649 } 23650 if (ncyl > (int)un->un_g.dkg_ncyl) { 23651 return (EINVAL); 23652 } 23653 vpart++; 23654 } 23655 #endif /* defined(_SUNOS_VTOC_8) */ 23656 23657 /* Put appropriate vtoc structure fields into the disk label */ 23658 #if defined(_SUNOS_VTOC_16) 23659 /* 23660 * The vtoc is always a 32bit data structure to maintain the 23661 * on-disk format. Convert "in place" instead of bcopying it. 23662 */ 23663 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23664 23665 /* 23666 * in the 16-slice vtoc, starting sectors are expressed in 23667 * numbers *relative* to the start of the Solaris fdisk partition. 23668 */ 23669 lmap = un->un_map; 23670 vpart = user_vtoc->v_part; 23671 23672 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23673 lmap->dkl_cylno = vpart->p_start / nblks; 23674 lmap->dkl_nblk = vpart->p_size; 23675 } 23676 23677 #elif defined(_SUNOS_VTOC_8) 23678 23679 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23680 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23681 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23682 23683 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23684 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23685 23686 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23687 23688 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23689 23690 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23691 sizeof (un->un_vtoc.v_reserved)); 23692 23693 /* 23694 * Note the conversion from starting sector number 23695 * to starting cylinder number. 23696 * Return error if division results in a remainder. 23697 */ 23698 lmap = un->un_map; 23699 lpart = un->un_vtoc.v_part; 23700 vpart = user_vtoc->v_part; 23701 23702 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23703 lpart->p_tag = vpart->p_tag; 23704 lpart->p_flag = vpart->p_flag; 23705 lmap->dkl_cylno = vpart->p_start / nblks; 23706 lmap->dkl_nblk = vpart->p_size; 23707 23708 lmap++; 23709 lpart++; 23710 vpart++; 23711 23712 /* (4387723) */ 23713 #ifdef _LP64 23714 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23715 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23716 } else { 23717 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23718 } 23719 #else 23720 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23721 #endif 23722 } 23723 23724 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23725 #else 23726 #error "No VTOC format defined." 23727 #endif 23728 return (0); 23729 } 23730 23731 /* 23732 * Function: sd_clear_efi 23733 * 23734 * Description: This routine clears all EFI labels. 23735 * 23736 * Arguments: un - driver soft state (unit) structure 23737 * 23738 * Return Code: void 23739 */ 23740 23741 static void 23742 sd_clear_efi(struct sd_lun *un) 23743 { 23744 efi_gpt_t *gpt; 23745 uint_t lbasize; 23746 uint64_t cap; 23747 int rval; 23748 23749 ASSERT(!mutex_owned(SD_MUTEX(un))); 23750 23751 mutex_enter(SD_MUTEX(un)); 23752 un->un_reserved = -1; 23753 mutex_exit(SD_MUTEX(un)); 23754 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23755 23756 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23757 goto done; 23758 } 23759 23760 sd_swap_efi_gpt(gpt); 23761 rval = sd_validate_efi(gpt); 23762 if (rval == 0) { 23763 /* clear primary */ 23764 bzero(gpt, sizeof (efi_gpt_t)); 23765 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23766 SD_PATH_DIRECT))) { 23767 SD_INFO(SD_LOG_IO_PARTITION, un, 23768 "sd_clear_efi: clear primary label failed\n"); 23769 } 23770 } 23771 /* the backup */ 23772 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23773 SD_PATH_DIRECT); 23774 if (rval) { 23775 goto done; 23776 } 23777 /* 23778 * The MMC standard allows READ CAPACITY to be 23779 * inaccurate by a bounded amount (in the interest of 23780 * response latency). As a result, failed READs are 23781 * commonplace (due to the reading of metadata and not 23782 * data). Depending on the per-Vendor/drive Sense data, 23783 * the failed READ can cause many (unnecessary) retries. 23784 */ 23785 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23786 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23787 SD_PATH_DIRECT)) != 0) { 23788 goto done; 23789 } 23790 sd_swap_efi_gpt(gpt); 23791 rval = sd_validate_efi(gpt); 23792 if (rval == 0) { 23793 /* clear backup */ 23794 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23795 cap-1); 23796 bzero(gpt, sizeof (efi_gpt_t)); 23797 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23798 cap-1, SD_PATH_DIRECT))) { 23799 SD_INFO(SD_LOG_IO_PARTITION, un, 23800 "sd_clear_efi: clear backup label failed\n"); 23801 } 23802 } else { 23803 /* 23804 * Refer to comments related to off-by-1 at the 23805 * header of this file 23806 */ 23807 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23808 cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23809 SD_PATH_DIRECT)) != 0) { 23810 goto done; 23811 } 23812 sd_swap_efi_gpt(gpt); 23813 rval = sd_validate_efi(gpt); 23814 if (rval == 0) { 23815 /* clear legacy backup EFI label */ 23816 SD_TRACE(SD_LOG_IOCTL, un, 23817 "sd_clear_efi clear backup@%lu\n", cap-2); 23818 bzero(gpt, sizeof (efi_gpt_t)); 23819 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23820 cap-2, SD_PATH_DIRECT))) { 23821 SD_INFO(SD_LOG_IO_PARTITION, 23822 un, "sd_clear_efi: " 23823 " clear legacy backup label failed\n"); 23824 } 23825 } 23826 } 23827 23828 done: 23829 kmem_free(gpt, sizeof (efi_gpt_t)); 23830 } 23831 23832 /* 23833 * Function: sd_set_vtoc 23834 * 23835 * Description: This routine writes data to the appropriate positions 23836 * 23837 * Arguments: un - driver soft state (unit) structure 23838 * dkl - the data to be written 23839 * 23840 * Return: void 23841 */ 23842 23843 static int 23844 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23845 { 23846 void *shadow_buf; 23847 uint_t label_addr; 23848 int sec; 23849 int blk; 23850 int head; 23851 int cyl; 23852 int rval; 23853 23854 #if defined(__i386) || defined(__amd64) 23855 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23856 #else 23857 /* Write the primary label at block 0 of the solaris partition. */ 23858 label_addr = 0; 23859 #endif 23860 23861 if (NOT_DEVBSIZE(un)) { 23862 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23863 /* 23864 * Read the target's first block. 23865 */ 23866 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23867 un->un_tgt_blocksize, label_addr, 23868 SD_PATH_STANDARD)) != 0) { 23869 goto exit; 23870 } 23871 /* 23872 * Copy the contents of the label into the shadow buffer 23873 * which is of the size of target block size. 23874 */ 23875 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23876 } 23877 23878 /* Write the primary label */ 23879 if (NOT_DEVBSIZE(un)) { 23880 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23881 label_addr, SD_PATH_STANDARD); 23882 } else { 23883 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23884 label_addr, SD_PATH_STANDARD); 23885 } 23886 if (rval != 0) { 23887 return (rval); 23888 } 23889 23890 /* 23891 * Calculate where the backup labels go. They are always on 23892 * the last alternate cylinder, but some older drives put them 23893 * on head 2 instead of the last head. They are always on the 23894 * first 5 odd sectors of the appropriate track. 23895 * 23896 * We have no choice at this point, but to believe that the 23897 * disk label is valid. Use the geometry of the disk 23898 * as described in the label. 23899 */ 23900 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23901 head = dkl->dkl_nhead - 1; 23902 23903 /* 23904 * Write and verify the backup labels. Make sure we don't try to 23905 * write past the last cylinder. 23906 */ 23907 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23908 blk = (daddr_t)( 23909 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23910 (head * dkl->dkl_nsect) + sec); 23911 #if defined(__i386) || defined(__amd64) 23912 blk += un->un_solaris_offset; 23913 #endif 23914 if (NOT_DEVBSIZE(un)) { 23915 uint64_t tblk; 23916 /* 23917 * Need to read the block first for read modify write. 23918 */ 23919 tblk = (uint64_t)blk; 23920 blk = (int)((tblk * un->un_sys_blocksize) / 23921 un->un_tgt_blocksize); 23922 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23923 un->un_tgt_blocksize, blk, 23924 SD_PATH_STANDARD)) != 0) { 23925 goto exit; 23926 } 23927 /* 23928 * Modify the shadow buffer with the label. 23929 */ 23930 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23931 rval = sd_send_scsi_WRITE(un, shadow_buf, 23932 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23933 } else { 23934 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23935 blk, SD_PATH_STANDARD); 23936 SD_INFO(SD_LOG_IO_PARTITION, un, 23937 "sd_set_vtoc: wrote backup label %d\n", blk); 23938 } 23939 if (rval != 0) { 23940 goto exit; 23941 } 23942 } 23943 exit: 23944 if (NOT_DEVBSIZE(un)) { 23945 kmem_free(shadow_buf, un->un_tgt_blocksize); 23946 } 23947 return (rval); 23948 } 23949 23950 /* 23951 * Function: sd_clear_vtoc 23952 * 23953 * Description: This routine clears out the VTOC labels. 23954 * 23955 * Arguments: un - driver soft state (unit) structure 23956 * 23957 * Return: void 23958 */ 23959 23960 static void 23961 sd_clear_vtoc(struct sd_lun *un) 23962 { 23963 struct dk_label *dkl; 23964 23965 mutex_exit(SD_MUTEX(un)); 23966 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23967 mutex_enter(SD_MUTEX(un)); 23968 /* 23969 * sd_set_vtoc uses these fields in order to figure out 23970 * where to overwrite the backup labels 23971 */ 23972 dkl->dkl_apc = un->un_g.dkg_apc; 23973 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23974 dkl->dkl_acyl = un->un_g.dkg_acyl; 23975 dkl->dkl_nhead = un->un_g.dkg_nhead; 23976 dkl->dkl_nsect = un->un_g.dkg_nsect; 23977 mutex_exit(SD_MUTEX(un)); 23978 (void) sd_set_vtoc(un, dkl); 23979 kmem_free(dkl, sizeof (struct dk_label)); 23980 23981 mutex_enter(SD_MUTEX(un)); 23982 } 23983 23984 /* 23985 * Function: sd_write_label 23986 * 23987 * Description: This routine will validate and write the driver soft state vtoc 23988 * contents to the device. 23989 * 23990 * Arguments: dev - the device number 23991 * 23992 * Return Code: the code returned by sd_send_scsi_cmd() 23993 * 0 23994 * EINVAL 23995 * ENXIO 23996 * ENOMEM 23997 */ 23998 23999 static int 24000 sd_write_label(dev_t dev) 24001 { 24002 struct sd_lun *un; 24003 struct dk_label *dkl; 24004 short sum; 24005 short *sp; 24006 int i; 24007 int rval; 24008 24009 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24010 (un->un_state == SD_STATE_OFFLINE)) { 24011 return (ENXIO); 24012 } 24013 ASSERT(mutex_owned(SD_MUTEX(un))); 24014 mutex_exit(SD_MUTEX(un)); 24015 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 24016 mutex_enter(SD_MUTEX(un)); 24017 24018 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 24019 dkl->dkl_rpm = un->un_g.dkg_rpm; 24020 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 24021 dkl->dkl_apc = un->un_g.dkg_apc; 24022 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 24023 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 24024 dkl->dkl_acyl = un->un_g.dkg_acyl; 24025 dkl->dkl_nhead = un->un_g.dkg_nhead; 24026 dkl->dkl_nsect = un->un_g.dkg_nsect; 24027 24028 #if defined(_SUNOS_VTOC_8) 24029 dkl->dkl_obs1 = un->un_g.dkg_obs1; 24030 dkl->dkl_obs2 = un->un_g.dkg_obs2; 24031 dkl->dkl_obs3 = un->un_g.dkg_obs3; 24032 for (i = 0; i < NDKMAP; i++) { 24033 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 24034 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 24035 } 24036 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 24037 #elif defined(_SUNOS_VTOC_16) 24038 dkl->dkl_skew = un->un_dkg_skew; 24039 #else 24040 #error "No VTOC format defined." 24041 #endif 24042 24043 dkl->dkl_magic = DKL_MAGIC; 24044 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 24045 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 24046 24047 /* Construct checksum for the new disk label */ 24048 sum = 0; 24049 sp = (short *)dkl; 24050 i = sizeof (struct dk_label) / sizeof (short); 24051 while (i--) { 24052 sum ^= *sp++; 24053 } 24054 dkl->dkl_cksum = sum; 24055 24056 mutex_exit(SD_MUTEX(un)); 24057 24058 rval = sd_set_vtoc(un, dkl); 24059 exit: 24060 kmem_free(dkl, sizeof (struct dk_label)); 24061 mutex_enter(SD_MUTEX(un)); 24062 return (rval); 24063 } 24064 24065 static int 24066 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 24067 { 24068 struct sd_lun *un = NULL; 24069 dk_efi_t user_efi; 24070 int rval = 0; 24071 void *buffer; 24072 int valid_efi; 24073 24074 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 24075 return (ENXIO); 24076 24077 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 24078 return (EFAULT); 24079 24080 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 24081 24082 if ((user_efi.dki_length % un->un_tgt_blocksize) || 24083 (user_efi.dki_length > un->un_max_xfer_size)) 24084 return (EINVAL); 24085 24086 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 24087 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 24088 rval = EFAULT; 24089 } else { 24090 /* 24091 * let's clear the vtoc labels and clear the softstate 24092 * vtoc. 24093 */ 24094 mutex_enter(SD_MUTEX(un)); 24095 if (un->un_vtoc.v_sanity == VTOC_SANE) { 24096 SD_TRACE(SD_LOG_IO_PARTITION, un, 24097 "sd_dkio_set_efi: CLEAR VTOC\n"); 24098 sd_clear_vtoc(un); 24099 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24100 mutex_exit(SD_MUTEX(un)); 24101 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 24102 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 24103 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 24104 S_IFBLK, 24105 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24106 un->un_node_type, NULL); 24107 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 24108 S_IFCHR, 24109 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24110 un->un_node_type, NULL); 24111 } else 24112 mutex_exit(SD_MUTEX(un)); 24113 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 24114 user_efi.dki_lba, SD_PATH_DIRECT); 24115 if (rval == 0) { 24116 mutex_enter(SD_MUTEX(un)); 24117 24118 /* 24119 * Set the un_reserved for valid efi label. 24120 * Function clear_efi in fdisk and efi_write in 24121 * libefi both change efi label on disk in 3 steps 24122 * 1. Change primary gpt and gpe 24123 * 2. Change backup gpe 24124 * 3. Change backup gpt, which is one block 24125 * We only reread the efi label after the 3rd step, 24126 * or there will be warning "primary label corrupt". 24127 */ 24128 if (user_efi.dki_length == un->un_tgt_blocksize) { 24129 un->un_f_geometry_is_valid = FALSE; 24130 valid_efi = sd_use_efi(un, SD_PATH_DIRECT); 24131 if ((valid_efi == 0) && 24132 un->un_f_devid_supported && 24133 (un->un_f_opt_fab_devid == TRUE)) { 24134 if (un->un_devid == NULL) { 24135 sd_register_devid(un, 24136 SD_DEVINFO(un), 24137 SD_TARGET_IS_UNRESERVED); 24138 } else { 24139 /* 24140 * The device id for this disk 24141 * has been fabricated. The 24142 * device id must be preserved 24143 * by writing it back out to 24144 * disk. 24145 */ 24146 if (sd_write_deviceid(un) 24147 != 0) { 24148 ddi_devid_free( 24149 un->un_devid); 24150 un->un_devid = NULL; 24151 } 24152 } 24153 } 24154 } 24155 24156 mutex_exit(SD_MUTEX(un)); 24157 } 24158 } 24159 kmem_free(buffer, user_efi.dki_length); 24160 return (rval); 24161 } 24162 24163 /* 24164 * Function: sd_dkio_get_mboot 24165 * 24166 * Description: This routine is the driver entry point for handling user 24167 * requests to get the current device mboot (DKIOCGMBOOT) 24168 * 24169 * Arguments: dev - the device number 24170 * arg - pointer to user provided mboot structure specifying 24171 * the current mboot. 24172 * flag - this argument is a pass through to ddi_copyxxx() 24173 * directly from the mode argument of ioctl(). 24174 * 24175 * Return Code: 0 24176 * EINVAL 24177 * EFAULT 24178 * ENXIO 24179 */ 24180 24181 static int 24182 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 24183 { 24184 struct sd_lun *un; 24185 struct mboot *mboot; 24186 int rval; 24187 size_t buffer_size; 24188 24189 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24190 (un->un_state == SD_STATE_OFFLINE)) { 24191 return (ENXIO); 24192 } 24193 24194 if (!un->un_f_mboot_supported || arg == NULL) { 24195 return (EINVAL); 24196 } 24197 24198 /* 24199 * Read the mboot block, located at absolute block 0 on the target. 24200 */ 24201 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 24202 24203 SD_TRACE(SD_LOG_IO_PARTITION, un, 24204 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 24205 24206 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 24207 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 24208 SD_PATH_STANDARD)) == 0) { 24209 if (ddi_copyout(mboot, (void *)arg, 24210 sizeof (struct mboot), flag) != 0) { 24211 rval = EFAULT; 24212 } 24213 } 24214 kmem_free(mboot, buffer_size); 24215 return (rval); 24216 } 24217 24218 24219 /* 24220 * Function: sd_dkio_set_mboot 24221 * 24222 * Description: This routine is the driver entry point for handling user 24223 * requests to validate and set the device master boot 24224 * (DKIOCSMBOOT). 24225 * 24226 * Arguments: dev - the device number 24227 * arg - pointer to user provided mboot structure used to set the 24228 * master boot. 24229 * flag - this argument is a pass through to ddi_copyxxx() 24230 * directly from the mode argument of ioctl(). 24231 * 24232 * Return Code: 0 24233 * EINVAL 24234 * EFAULT 24235 * ENXIO 24236 */ 24237 24238 static int 24239 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 24240 { 24241 struct sd_lun *un = NULL; 24242 struct mboot *mboot = NULL; 24243 int rval; 24244 ushort_t magic; 24245 24246 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24247 return (ENXIO); 24248 } 24249 24250 ASSERT(!mutex_owned(SD_MUTEX(un))); 24251 24252 if (!un->un_f_mboot_supported) { 24253 return (EINVAL); 24254 } 24255 24256 if (arg == NULL) { 24257 return (EINVAL); 24258 } 24259 24260 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 24261 24262 if (ddi_copyin((const void *)arg, mboot, 24263 sizeof (struct mboot), flag) != 0) { 24264 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24265 return (EFAULT); 24266 } 24267 24268 /* Is this really a master boot record? */ 24269 magic = LE_16(mboot->signature); 24270 if (magic != MBB_MAGIC) { 24271 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24272 return (EINVAL); 24273 } 24274 24275 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 24276 SD_PATH_STANDARD); 24277 24278 mutex_enter(SD_MUTEX(un)); 24279 #if defined(__i386) || defined(__amd64) 24280 if (rval == 0) { 24281 /* 24282 * mboot has been written successfully. 24283 * update the fdisk and vtoc tables in memory 24284 */ 24285 rval = sd_update_fdisk_and_vtoc(un); 24286 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 24287 mutex_exit(SD_MUTEX(un)); 24288 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24289 return (rval); 24290 } 24291 } 24292 24293 #ifdef __lock_lint 24294 sd_setup_default_geometry(un); 24295 #endif 24296 24297 #else 24298 if (rval == 0) { 24299 /* 24300 * mboot has been written successfully. 24301 * set up the default geometry and VTOC 24302 */ 24303 if (un->un_blockcount <= DK_MAX_BLOCKS) 24304 sd_setup_default_geometry(un); 24305 } 24306 #endif 24307 mutex_exit(SD_MUTEX(un)); 24308 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24309 return (rval); 24310 } 24311 24312 24313 /* 24314 * Function: sd_setup_default_geometry 24315 * 24316 * Description: This local utility routine sets the default geometry as part of 24317 * setting the device mboot. 24318 * 24319 * Arguments: un - driver soft state (unit) structure 24320 * 24321 * Note: This may be redundant with sd_build_default_label. 24322 */ 24323 24324 static void 24325 sd_setup_default_geometry(struct sd_lun *un) 24326 { 24327 /* zero out the soft state geometry and partition table. */ 24328 bzero(&un->un_g, sizeof (struct dk_geom)); 24329 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24330 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 24331 un->un_asciilabel[0] = '\0'; 24332 24333 /* 24334 * For the rpm, we use the minimum for the disk. 24335 * For the head, cyl and number of sector per track, 24336 * if the capacity <= 1GB, head = 64, sect = 32. 24337 * else head = 255, sect 63 24338 * Note: the capacity should be equal to C*H*S values. 24339 * This will cause some truncation of size due to 24340 * round off errors. For CD-ROMs, this truncation can 24341 * have adverse side effects, so returning ncyl and 24342 * nhead as 1. The nsect will overflow for most of 24343 * CD-ROMs as nsect is of type ushort. 24344 */ 24345 if (ISCD(un)) { 24346 un->un_g.dkg_ncyl = 1; 24347 un->un_g.dkg_nhead = 1; 24348 un->un_g.dkg_nsect = un->un_blockcount; 24349 } else { 24350 if (un->un_blockcount <= 0x1000) { 24351 /* Needed for unlabeled SCSI floppies. */ 24352 un->un_g.dkg_nhead = 2; 24353 un->un_g.dkg_ncyl = 80; 24354 un->un_g.dkg_pcyl = 80; 24355 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 24356 } else if (un->un_blockcount <= 0x200000) { 24357 un->un_g.dkg_nhead = 64; 24358 un->un_g.dkg_nsect = 32; 24359 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 24360 } else { 24361 un->un_g.dkg_nhead = 255; 24362 un->un_g.dkg_nsect = 63; 24363 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 24364 } 24365 un->un_blockcount = un->un_g.dkg_ncyl * 24366 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 24367 } 24368 un->un_g.dkg_acyl = 0; 24369 un->un_g.dkg_bcyl = 0; 24370 un->un_g.dkg_intrlv = 1; 24371 un->un_g.dkg_rpm = 200; 24372 un->un_g.dkg_read_reinstruct = 0; 24373 un->un_g.dkg_write_reinstruct = 0; 24374 if (un->un_g.dkg_pcyl == 0) { 24375 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 24376 } 24377 24378 un->un_map['a'-'a'].dkl_cylno = 0; 24379 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 24380 un->un_map['c'-'a'].dkl_cylno = 0; 24381 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 24382 un->un_f_geometry_is_valid = FALSE; 24383 } 24384 24385 24386 #if defined(__i386) || defined(__amd64) 24387 /* 24388 * Function: sd_update_fdisk_and_vtoc 24389 * 24390 * Description: This local utility routine updates the device fdisk and vtoc 24391 * as part of setting the device mboot. 24392 * 24393 * Arguments: un - driver soft state (unit) structure 24394 * 24395 * Return Code: 0 for success or errno-type return code. 24396 * 24397 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 24398 * these did exist seperately in x86 sd.c!!! 24399 */ 24400 24401 static int 24402 sd_update_fdisk_and_vtoc(struct sd_lun *un) 24403 { 24404 static char labelstring[128]; 24405 static char buf[256]; 24406 char *label = 0; 24407 int count; 24408 int label_rc = 0; 24409 int gvalid = un->un_f_geometry_is_valid; 24410 int fdisk_rval; 24411 int lbasize; 24412 int capacity; 24413 24414 ASSERT(mutex_owned(SD_MUTEX(un))); 24415 24416 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 24417 return (EINVAL); 24418 } 24419 24420 if (un->un_f_blockcount_is_valid == FALSE) { 24421 return (EINVAL); 24422 } 24423 24424 #if defined(_SUNOS_VTOC_16) 24425 /* 24426 * Set up the "whole disk" fdisk partition; this should always 24427 * exist, regardless of whether the disk contains an fdisk table 24428 * or vtoc. 24429 */ 24430 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 24431 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 24432 #endif /* defined(_SUNOS_VTOC_16) */ 24433 24434 /* 24435 * copy the lbasize and capacity so that if they're 24436 * reset while we're not holding the SD_MUTEX(un), we will 24437 * continue to use valid values after the SD_MUTEX(un) is 24438 * reacquired. 24439 */ 24440 lbasize = un->un_tgt_blocksize; 24441 capacity = un->un_blockcount; 24442 24443 /* 24444 * refresh the logical and physical geometry caches. 24445 * (data from mode sense format/rigid disk geometry pages, 24446 * and scsi_ifgetcap("geometry"). 24447 */ 24448 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 24449 24450 /* 24451 * Only DIRECT ACCESS devices will have Sun labels. 24452 * CD's supposedly have a Sun label, too 24453 */ 24454 if (un->un_f_vtoc_label_supported) { 24455 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 24456 SD_PATH_DIRECT); 24457 if (fdisk_rval == SD_CMD_FAILURE) { 24458 ASSERT(mutex_owned(SD_MUTEX(un))); 24459 return (EIO); 24460 } 24461 24462 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 24463 ASSERT(mutex_owned(SD_MUTEX(un))); 24464 return (EACCES); 24465 } 24466 24467 if (un->un_solaris_size <= DK_LABEL_LOC) { 24468 /* 24469 * Found fdisk table but no Solaris partition entry, 24470 * so don't call sd_uselabel() and don't create 24471 * a default label. 24472 */ 24473 label_rc = 0; 24474 un->un_f_geometry_is_valid = TRUE; 24475 goto no_solaris_partition; 24476 } 24477 24478 #if defined(_SUNOS_VTOC_8) 24479 label = (char *)un->un_asciilabel; 24480 #elif defined(_SUNOS_VTOC_16) 24481 label = (char *)un->un_vtoc.v_asciilabel; 24482 #else 24483 #error "No VTOC format defined." 24484 #endif 24485 } else if (capacity < 0) { 24486 ASSERT(mutex_owned(SD_MUTEX(un))); 24487 return (EINVAL); 24488 } 24489 24490 /* 24491 * For Removable media We reach here if we have found a 24492 * SOLARIS PARTITION. 24493 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24494 * PARTITION has changed from the previous one, hence we will setup a 24495 * default VTOC in this case. 24496 */ 24497 if (un->un_f_geometry_is_valid == FALSE) { 24498 sd_build_default_label(un); 24499 label_rc = 0; 24500 } 24501 24502 no_solaris_partition: 24503 if ((!un->un_f_has_removable_media || 24504 (un->un_f_has_removable_media && 24505 un->un_mediastate == DKIO_EJECTED)) && 24506 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24507 /* 24508 * Print out a message indicating who and what we are. 24509 * We do this only when we happen to really validate the 24510 * geometry. We may call sd_validate_geometry() at other 24511 * times, ioctl()'s like Get VTOC in which case we 24512 * don't want to print the label. 24513 * If the geometry is valid, print the label string, 24514 * else print vendor and product info, if available 24515 */ 24516 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24517 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24518 } else { 24519 mutex_enter(&sd_label_mutex); 24520 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24521 labelstring); 24522 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24523 &labelstring[64]); 24524 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24525 labelstring, &labelstring[64]); 24526 if (un->un_f_blockcount_is_valid == TRUE) { 24527 (void) sprintf(&buf[strlen(buf)], 24528 ", %" PRIu64 " %u byte blocks\n", 24529 un->un_blockcount, 24530 un->un_tgt_blocksize); 24531 } else { 24532 (void) sprintf(&buf[strlen(buf)], 24533 ", (unknown capacity)\n"); 24534 } 24535 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24536 mutex_exit(&sd_label_mutex); 24537 } 24538 } 24539 24540 #if defined(_SUNOS_VTOC_16) 24541 /* 24542 * If we have valid geometry, set up the remaining fdisk partitions. 24543 * Note that dkl_cylno is not used for the fdisk map entries, so 24544 * we set it to an entirely bogus value. 24545 */ 24546 for (count = 0; count < FD_NUMPART; count++) { 24547 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24548 un->un_map[FDISK_P1 + count].dkl_nblk = 24549 un->un_fmap[count].fmap_nblk; 24550 un->un_offset[FDISK_P1 + count] = 24551 un->un_fmap[count].fmap_start; 24552 } 24553 #endif 24554 24555 for (count = 0; count < NDKMAP; count++) { 24556 #if defined(_SUNOS_VTOC_8) 24557 struct dk_map *lp = &un->un_map[count]; 24558 un->un_offset[count] = 24559 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24560 #elif defined(_SUNOS_VTOC_16) 24561 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24562 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24563 #else 24564 #error "No VTOC format defined." 24565 #endif 24566 } 24567 24568 ASSERT(mutex_owned(SD_MUTEX(un))); 24569 return (label_rc); 24570 } 24571 #endif 24572 24573 24574 /* 24575 * Function: sd_check_media 24576 * 24577 * Description: This utility routine implements the functionality for the 24578 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24579 * driver state changes from that specified by the user 24580 * (inserted or ejected). For example, if the user specifies 24581 * DKIO_EJECTED and the current media state is inserted this 24582 * routine will immediately return DKIO_INSERTED. However, if the 24583 * current media state is not inserted the user thread will be 24584 * blocked until the drive state changes. If DKIO_NONE is specified 24585 * the user thread will block until a drive state change occurs. 24586 * 24587 * Arguments: dev - the device number 24588 * state - user pointer to a dkio_state, updated with the current 24589 * drive state at return. 24590 * 24591 * Return Code: ENXIO 24592 * EIO 24593 * EAGAIN 24594 * EINTR 24595 */ 24596 24597 static int 24598 sd_check_media(dev_t dev, enum dkio_state state) 24599 { 24600 struct sd_lun *un = NULL; 24601 enum dkio_state prev_state; 24602 opaque_t token = NULL; 24603 int rval = 0; 24604 24605 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24606 return (ENXIO); 24607 } 24608 24609 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24610 24611 mutex_enter(SD_MUTEX(un)); 24612 24613 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24614 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24615 24616 prev_state = un->un_mediastate; 24617 24618 /* is there anything to do? */ 24619 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24620 /* 24621 * submit the request to the scsi_watch service; 24622 * scsi_media_watch_cb() does the real work 24623 */ 24624 mutex_exit(SD_MUTEX(un)); 24625 24626 /* 24627 * This change handles the case where a scsi watch request is 24628 * added to a device that is powered down. To accomplish this 24629 * we power up the device before adding the scsi watch request, 24630 * since the scsi watch sends a TUR directly to the device 24631 * which the device cannot handle if it is powered down. 24632 */ 24633 if (sd_pm_entry(un) != DDI_SUCCESS) { 24634 mutex_enter(SD_MUTEX(un)); 24635 goto done; 24636 } 24637 24638 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24639 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24640 (caddr_t)dev); 24641 24642 sd_pm_exit(un); 24643 24644 mutex_enter(SD_MUTEX(un)); 24645 if (token == NULL) { 24646 rval = EAGAIN; 24647 goto done; 24648 } 24649 24650 /* 24651 * This is a special case IOCTL that doesn't return 24652 * until the media state changes. Routine sdpower 24653 * knows about and handles this so don't count it 24654 * as an active cmd in the driver, which would 24655 * keep the device busy to the pm framework. 24656 * If the count isn't decremented the device can't 24657 * be powered down. 24658 */ 24659 un->un_ncmds_in_driver--; 24660 ASSERT(un->un_ncmds_in_driver >= 0); 24661 24662 /* 24663 * if a prior request had been made, this will be the same 24664 * token, as scsi_watch was designed that way. 24665 */ 24666 un->un_swr_token = token; 24667 un->un_specified_mediastate = state; 24668 24669 /* 24670 * now wait for media change 24671 * we will not be signalled unless mediastate == state but it is 24672 * still better to test for this condition, since there is a 24673 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24674 */ 24675 SD_TRACE(SD_LOG_COMMON, un, 24676 "sd_check_media: waiting for media state change\n"); 24677 while (un->un_mediastate == state) { 24678 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24679 SD_TRACE(SD_LOG_COMMON, un, 24680 "sd_check_media: waiting for media state " 24681 "was interrupted\n"); 24682 un->un_ncmds_in_driver++; 24683 rval = EINTR; 24684 goto done; 24685 } 24686 SD_TRACE(SD_LOG_COMMON, un, 24687 "sd_check_media: received signal, state=%x\n", 24688 un->un_mediastate); 24689 } 24690 /* 24691 * Inc the counter to indicate the device once again 24692 * has an active outstanding cmd. 24693 */ 24694 un->un_ncmds_in_driver++; 24695 } 24696 24697 /* invalidate geometry */ 24698 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24699 sr_ejected(un); 24700 } 24701 24702 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24703 uint64_t capacity; 24704 uint_t lbasize; 24705 24706 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24707 mutex_exit(SD_MUTEX(un)); 24708 /* 24709 * Since the following routines use SD_PATH_DIRECT, we must 24710 * call PM directly before the upcoming disk accesses. This 24711 * may cause the disk to be power/spin up. 24712 */ 24713 24714 if (sd_pm_entry(un) == DDI_SUCCESS) { 24715 rval = sd_send_scsi_READ_CAPACITY(un, 24716 &capacity, 24717 &lbasize, SD_PATH_DIRECT); 24718 if (rval != 0) { 24719 sd_pm_exit(un); 24720 mutex_enter(SD_MUTEX(un)); 24721 goto done; 24722 } 24723 } else { 24724 rval = EIO; 24725 mutex_enter(SD_MUTEX(un)); 24726 goto done; 24727 } 24728 mutex_enter(SD_MUTEX(un)); 24729 24730 sd_update_block_info(un, lbasize, capacity); 24731 24732 un->un_f_geometry_is_valid = FALSE; 24733 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24734 24735 mutex_exit(SD_MUTEX(un)); 24736 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24737 SD_PATH_DIRECT); 24738 sd_pm_exit(un); 24739 24740 mutex_enter(SD_MUTEX(un)); 24741 } 24742 done: 24743 un->un_f_watcht_stopped = FALSE; 24744 if (un->un_swr_token) { 24745 /* 24746 * Use of this local token and the mutex ensures that we avoid 24747 * some race conditions associated with terminating the 24748 * scsi watch. 24749 */ 24750 token = un->un_swr_token; 24751 un->un_swr_token = (opaque_t)NULL; 24752 mutex_exit(SD_MUTEX(un)); 24753 (void) scsi_watch_request_terminate(token, 24754 SCSI_WATCH_TERMINATE_WAIT); 24755 mutex_enter(SD_MUTEX(un)); 24756 } 24757 24758 /* 24759 * Update the capacity kstat value, if no media previously 24760 * (capacity kstat is 0) and a media has been inserted 24761 * (un_f_blockcount_is_valid == TRUE) 24762 */ 24763 if (un->un_errstats) { 24764 struct sd_errstats *stp = NULL; 24765 24766 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24767 if ((stp->sd_capacity.value.ui64 == 0) && 24768 (un->un_f_blockcount_is_valid == TRUE)) { 24769 stp->sd_capacity.value.ui64 = 24770 (uint64_t)((uint64_t)un->un_blockcount * 24771 un->un_sys_blocksize); 24772 } 24773 } 24774 mutex_exit(SD_MUTEX(un)); 24775 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24776 return (rval); 24777 } 24778 24779 24780 /* 24781 * Function: sd_delayed_cv_broadcast 24782 * 24783 * Description: Delayed cv_broadcast to allow for target to recover from media 24784 * insertion. 24785 * 24786 * Arguments: arg - driver soft state (unit) structure 24787 */ 24788 24789 static void 24790 sd_delayed_cv_broadcast(void *arg) 24791 { 24792 struct sd_lun *un = arg; 24793 24794 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24795 24796 mutex_enter(SD_MUTEX(un)); 24797 un->un_dcvb_timeid = NULL; 24798 cv_broadcast(&un->un_state_cv); 24799 mutex_exit(SD_MUTEX(un)); 24800 } 24801 24802 24803 /* 24804 * Function: sd_media_watch_cb 24805 * 24806 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24807 * routine processes the TUR sense data and updates the driver 24808 * state if a transition has occurred. The user thread 24809 * (sd_check_media) is then signalled. 24810 * 24811 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24812 * among multiple watches that share this callback function 24813 * resultp - scsi watch facility result packet containing scsi 24814 * packet, status byte and sense data 24815 * 24816 * Return Code: 0 for success, -1 for failure 24817 */ 24818 24819 static int 24820 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24821 { 24822 struct sd_lun *un; 24823 struct scsi_status *statusp = resultp->statusp; 24824 uint8_t *sensep = (uint8_t *)resultp->sensep; 24825 enum dkio_state state = DKIO_NONE; 24826 dev_t dev = (dev_t)arg; 24827 uchar_t actual_sense_length; 24828 uint8_t skey, asc, ascq; 24829 24830 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24831 return (-1); 24832 } 24833 actual_sense_length = resultp->actual_sense_length; 24834 24835 mutex_enter(SD_MUTEX(un)); 24836 SD_TRACE(SD_LOG_COMMON, un, 24837 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24838 *((char *)statusp), (void *)sensep, actual_sense_length); 24839 24840 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24841 un->un_mediastate = DKIO_DEV_GONE; 24842 cv_broadcast(&un->un_state_cv); 24843 mutex_exit(SD_MUTEX(un)); 24844 24845 return (0); 24846 } 24847 24848 /* 24849 * If there was a check condition then sensep points to valid sense data 24850 * If status was not a check condition but a reservation or busy status 24851 * then the new state is DKIO_NONE 24852 */ 24853 if (sensep != NULL) { 24854 skey = scsi_sense_key(sensep); 24855 asc = scsi_sense_asc(sensep); 24856 ascq = scsi_sense_ascq(sensep); 24857 24858 SD_INFO(SD_LOG_COMMON, un, 24859 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24860 skey, asc, ascq); 24861 /* This routine only uses up to 13 bytes of sense data. */ 24862 if (actual_sense_length >= 13) { 24863 if (skey == KEY_UNIT_ATTENTION) { 24864 if (asc == 0x28) { 24865 state = DKIO_INSERTED; 24866 } 24867 } else { 24868 /* 24869 * if 02/04/02 means that the host 24870 * should send start command. Explicitly 24871 * leave the media state as is 24872 * (inserted) as the media is inserted 24873 * and host has stopped device for PM 24874 * reasons. Upon next true read/write 24875 * to this media will bring the 24876 * device to the right state good for 24877 * media access. 24878 */ 24879 if ((skey == KEY_NOT_READY) && 24880 (asc == 0x3a)) { 24881 state = DKIO_EJECTED; 24882 } 24883 24884 /* 24885 * If the drivge is busy with an operation 24886 * or long write, keep the media in an 24887 * inserted state. 24888 */ 24889 24890 if ((skey == KEY_NOT_READY) && 24891 (asc == 0x04) && 24892 ((ascq == 0x02) || 24893 (ascq == 0x07) || 24894 (ascq == 0x08))) { 24895 state = DKIO_INSERTED; 24896 } 24897 } 24898 } 24899 } else if ((*((char *)statusp) == STATUS_GOOD) && 24900 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24901 state = DKIO_INSERTED; 24902 } 24903 24904 SD_TRACE(SD_LOG_COMMON, un, 24905 "sd_media_watch_cb: state=%x, specified=%x\n", 24906 state, un->un_specified_mediastate); 24907 24908 /* 24909 * now signal the waiting thread if this is *not* the specified state; 24910 * delay the signal if the state is DKIO_INSERTED to allow the target 24911 * to recover 24912 */ 24913 if (state != un->un_specified_mediastate) { 24914 un->un_mediastate = state; 24915 if (state == DKIO_INSERTED) { 24916 /* 24917 * delay the signal to give the drive a chance 24918 * to do what it apparently needs to do 24919 */ 24920 SD_TRACE(SD_LOG_COMMON, un, 24921 "sd_media_watch_cb: delayed cv_broadcast\n"); 24922 if (un->un_dcvb_timeid == NULL) { 24923 un->un_dcvb_timeid = 24924 timeout(sd_delayed_cv_broadcast, un, 24925 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24926 } 24927 } else { 24928 SD_TRACE(SD_LOG_COMMON, un, 24929 "sd_media_watch_cb: immediate cv_broadcast\n"); 24930 cv_broadcast(&un->un_state_cv); 24931 } 24932 } 24933 mutex_exit(SD_MUTEX(un)); 24934 return (0); 24935 } 24936 24937 24938 /* 24939 * Function: sd_dkio_get_temp 24940 * 24941 * Description: This routine is the driver entry point for handling ioctl 24942 * requests to get the disk temperature. 24943 * 24944 * Arguments: dev - the device number 24945 * arg - pointer to user provided dk_temperature structure. 24946 * flag - this argument is a pass through to ddi_copyxxx() 24947 * directly from the mode argument of ioctl(). 24948 * 24949 * Return Code: 0 24950 * EFAULT 24951 * ENXIO 24952 * EAGAIN 24953 */ 24954 24955 static int 24956 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24957 { 24958 struct sd_lun *un = NULL; 24959 struct dk_temperature *dktemp = NULL; 24960 uchar_t *temperature_page; 24961 int rval = 0; 24962 int path_flag = SD_PATH_STANDARD; 24963 24964 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24965 return (ENXIO); 24966 } 24967 24968 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24969 24970 /* copyin the disk temp argument to get the user flags */ 24971 if (ddi_copyin((void *)arg, dktemp, 24972 sizeof (struct dk_temperature), flag) != 0) { 24973 rval = EFAULT; 24974 goto done; 24975 } 24976 24977 /* Initialize the temperature to invalid. */ 24978 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24979 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24980 24981 /* 24982 * Note: Investigate removing the "bypass pm" semantic. 24983 * Can we just bypass PM always? 24984 */ 24985 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24986 path_flag = SD_PATH_DIRECT; 24987 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24988 mutex_enter(&un->un_pm_mutex); 24989 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24990 /* 24991 * If DKT_BYPASS_PM is set, and the drive happens to be 24992 * in low power mode, we can not wake it up, Need to 24993 * return EAGAIN. 24994 */ 24995 mutex_exit(&un->un_pm_mutex); 24996 rval = EAGAIN; 24997 goto done; 24998 } else { 24999 /* 25000 * Indicate to PM the device is busy. This is required 25001 * to avoid a race - i.e. the ioctl is issuing a 25002 * command and the pm framework brings down the device 25003 * to low power mode (possible power cut-off on some 25004 * platforms). 25005 */ 25006 mutex_exit(&un->un_pm_mutex); 25007 if (sd_pm_entry(un) != DDI_SUCCESS) { 25008 rval = EAGAIN; 25009 goto done; 25010 } 25011 } 25012 } 25013 25014 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 25015 25016 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 25017 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 25018 goto done2; 25019 } 25020 25021 /* 25022 * For the current temperature verify that the parameter length is 0x02 25023 * and the parameter code is 0x00 25024 */ 25025 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 25026 (temperature_page[5] == 0x00)) { 25027 if (temperature_page[9] == 0xFF) { 25028 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 25029 } else { 25030 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 25031 } 25032 } 25033 25034 /* 25035 * For the reference temperature verify that the parameter 25036 * length is 0x02 and the parameter code is 0x01 25037 */ 25038 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 25039 (temperature_page[11] == 0x01)) { 25040 if (temperature_page[15] == 0xFF) { 25041 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 25042 } else { 25043 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 25044 } 25045 } 25046 25047 /* Do the copyout regardless of the temperature commands status. */ 25048 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 25049 flag) != 0) { 25050 rval = EFAULT; 25051 } 25052 25053 done2: 25054 if (path_flag == SD_PATH_DIRECT) { 25055 sd_pm_exit(un); 25056 } 25057 25058 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 25059 done: 25060 if (dktemp != NULL) { 25061 kmem_free(dktemp, sizeof (struct dk_temperature)); 25062 } 25063 25064 return (rval); 25065 } 25066 25067 25068 /* 25069 * Function: sd_log_page_supported 25070 * 25071 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 25072 * supported log pages. 25073 * 25074 * Arguments: un - 25075 * log_page - 25076 * 25077 * Return Code: -1 - on error (log sense is optional and may not be supported). 25078 * 0 - log page not found. 25079 * 1 - log page found. 25080 */ 25081 25082 static int 25083 sd_log_page_supported(struct sd_lun *un, int log_page) 25084 { 25085 uchar_t *log_page_data; 25086 int i; 25087 int match = 0; 25088 int log_size; 25089 25090 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 25091 25092 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 25093 SD_PATH_DIRECT) != 0) { 25094 SD_ERROR(SD_LOG_COMMON, un, 25095 "sd_log_page_supported: failed log page retrieval\n"); 25096 kmem_free(log_page_data, 0xFF); 25097 return (-1); 25098 } 25099 log_size = log_page_data[3]; 25100 25101 /* 25102 * The list of supported log pages start from the fourth byte. Check 25103 * until we run out of log pages or a match is found. 25104 */ 25105 for (i = 4; (i < (log_size + 4)) && !match; i++) { 25106 if (log_page_data[i] == log_page) { 25107 match++; 25108 } 25109 } 25110 kmem_free(log_page_data, 0xFF); 25111 return (match); 25112 } 25113 25114 25115 /* 25116 * Function: sd_mhdioc_failfast 25117 * 25118 * Description: This routine is the driver entry point for handling ioctl 25119 * requests to enable/disable the multihost failfast option. 25120 * (MHIOCENFAILFAST) 25121 * 25122 * Arguments: dev - the device number 25123 * arg - user specified probing interval. 25124 * flag - this argument is a pass through to ddi_copyxxx() 25125 * directly from the mode argument of ioctl(). 25126 * 25127 * Return Code: 0 25128 * EFAULT 25129 * ENXIO 25130 */ 25131 25132 static int 25133 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 25134 { 25135 struct sd_lun *un = NULL; 25136 int mh_time; 25137 int rval = 0; 25138 25139 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25140 return (ENXIO); 25141 } 25142 25143 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 25144 return (EFAULT); 25145 25146 if (mh_time) { 25147 mutex_enter(SD_MUTEX(un)); 25148 un->un_resvd_status |= SD_FAILFAST; 25149 mutex_exit(SD_MUTEX(un)); 25150 /* 25151 * If mh_time is INT_MAX, then this ioctl is being used for 25152 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 25153 */ 25154 if (mh_time != INT_MAX) { 25155 rval = sd_check_mhd(dev, mh_time); 25156 } 25157 } else { 25158 (void) sd_check_mhd(dev, 0); 25159 mutex_enter(SD_MUTEX(un)); 25160 un->un_resvd_status &= ~SD_FAILFAST; 25161 mutex_exit(SD_MUTEX(un)); 25162 } 25163 return (rval); 25164 } 25165 25166 25167 /* 25168 * Function: sd_mhdioc_takeown 25169 * 25170 * Description: This routine is the driver entry point for handling ioctl 25171 * requests to forcefully acquire exclusive access rights to the 25172 * multihost disk (MHIOCTKOWN). 25173 * 25174 * Arguments: dev - the device number 25175 * arg - user provided structure specifying the delay 25176 * parameters in milliseconds 25177 * flag - this argument is a pass through to ddi_copyxxx() 25178 * directly from the mode argument of ioctl(). 25179 * 25180 * Return Code: 0 25181 * EFAULT 25182 * ENXIO 25183 */ 25184 25185 static int 25186 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 25187 { 25188 struct sd_lun *un = NULL; 25189 struct mhioctkown *tkown = NULL; 25190 int rval = 0; 25191 25192 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25193 return (ENXIO); 25194 } 25195 25196 if (arg != NULL) { 25197 tkown = (struct mhioctkown *) 25198 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 25199 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 25200 if (rval != 0) { 25201 rval = EFAULT; 25202 goto error; 25203 } 25204 } 25205 25206 rval = sd_take_ownership(dev, tkown); 25207 mutex_enter(SD_MUTEX(un)); 25208 if (rval == 0) { 25209 un->un_resvd_status |= SD_RESERVE; 25210 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 25211 sd_reinstate_resv_delay = 25212 tkown->reinstate_resv_delay * 1000; 25213 } else { 25214 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 25215 } 25216 /* 25217 * Give the scsi_watch routine interval set by 25218 * the MHIOCENFAILFAST ioctl precedence here. 25219 */ 25220 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 25221 mutex_exit(SD_MUTEX(un)); 25222 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 25223 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25224 "sd_mhdioc_takeown : %d\n", 25225 sd_reinstate_resv_delay); 25226 } else { 25227 mutex_exit(SD_MUTEX(un)); 25228 } 25229 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 25230 sd_mhd_reset_notify_cb, (caddr_t)un); 25231 } else { 25232 un->un_resvd_status &= ~SD_RESERVE; 25233 mutex_exit(SD_MUTEX(un)); 25234 } 25235 25236 error: 25237 if (tkown != NULL) { 25238 kmem_free(tkown, sizeof (struct mhioctkown)); 25239 } 25240 return (rval); 25241 } 25242 25243 25244 /* 25245 * Function: sd_mhdioc_release 25246 * 25247 * Description: This routine is the driver entry point for handling ioctl 25248 * requests to release exclusive access rights to the multihost 25249 * disk (MHIOCRELEASE). 25250 * 25251 * Arguments: dev - the device number 25252 * 25253 * Return Code: 0 25254 * ENXIO 25255 */ 25256 25257 static int 25258 sd_mhdioc_release(dev_t dev) 25259 { 25260 struct sd_lun *un = NULL; 25261 timeout_id_t resvd_timeid_save; 25262 int resvd_status_save; 25263 int rval = 0; 25264 25265 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25266 return (ENXIO); 25267 } 25268 25269 mutex_enter(SD_MUTEX(un)); 25270 resvd_status_save = un->un_resvd_status; 25271 un->un_resvd_status &= 25272 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 25273 if (un->un_resvd_timeid) { 25274 resvd_timeid_save = un->un_resvd_timeid; 25275 un->un_resvd_timeid = NULL; 25276 mutex_exit(SD_MUTEX(un)); 25277 (void) untimeout(resvd_timeid_save); 25278 } else { 25279 mutex_exit(SD_MUTEX(un)); 25280 } 25281 25282 /* 25283 * destroy any pending timeout thread that may be attempting to 25284 * reinstate reservation on this device. 25285 */ 25286 sd_rmv_resv_reclaim_req(dev); 25287 25288 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 25289 mutex_enter(SD_MUTEX(un)); 25290 if ((un->un_mhd_token) && 25291 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 25292 mutex_exit(SD_MUTEX(un)); 25293 (void) sd_check_mhd(dev, 0); 25294 } else { 25295 mutex_exit(SD_MUTEX(un)); 25296 } 25297 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 25298 sd_mhd_reset_notify_cb, (caddr_t)un); 25299 } else { 25300 /* 25301 * sd_mhd_watch_cb will restart the resvd recover timeout thread 25302 */ 25303 mutex_enter(SD_MUTEX(un)); 25304 un->un_resvd_status = resvd_status_save; 25305 mutex_exit(SD_MUTEX(un)); 25306 } 25307 return (rval); 25308 } 25309 25310 25311 /* 25312 * Function: sd_mhdioc_register_devid 25313 * 25314 * Description: This routine is the driver entry point for handling ioctl 25315 * requests to register the device id (MHIOCREREGISTERDEVID). 25316 * 25317 * Note: The implementation for this ioctl has been updated to 25318 * be consistent with the original PSARC case (1999/357) 25319 * (4375899, 4241671, 4220005) 25320 * 25321 * Arguments: dev - the device number 25322 * 25323 * Return Code: 0 25324 * ENXIO 25325 */ 25326 25327 static int 25328 sd_mhdioc_register_devid(dev_t dev) 25329 { 25330 struct sd_lun *un = NULL; 25331 int rval = 0; 25332 25333 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25334 return (ENXIO); 25335 } 25336 25337 ASSERT(!mutex_owned(SD_MUTEX(un))); 25338 25339 mutex_enter(SD_MUTEX(un)); 25340 25341 /* If a devid already exists, de-register it */ 25342 if (un->un_devid != NULL) { 25343 ddi_devid_unregister(SD_DEVINFO(un)); 25344 /* 25345 * After unregister devid, needs to free devid memory 25346 */ 25347 ddi_devid_free(un->un_devid); 25348 un->un_devid = NULL; 25349 } 25350 25351 /* Check for reservation conflict */ 25352 mutex_exit(SD_MUTEX(un)); 25353 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 25354 mutex_enter(SD_MUTEX(un)); 25355 25356 switch (rval) { 25357 case 0: 25358 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 25359 break; 25360 case EACCES: 25361 break; 25362 default: 25363 rval = EIO; 25364 } 25365 25366 mutex_exit(SD_MUTEX(un)); 25367 return (rval); 25368 } 25369 25370 25371 /* 25372 * Function: sd_mhdioc_inkeys 25373 * 25374 * Description: This routine is the driver entry point for handling ioctl 25375 * requests to issue the SCSI-3 Persistent In Read Keys command 25376 * to the device (MHIOCGRP_INKEYS). 25377 * 25378 * Arguments: dev - the device number 25379 * arg - user provided in_keys structure 25380 * flag - this argument is a pass through to ddi_copyxxx() 25381 * directly from the mode argument of ioctl(). 25382 * 25383 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 25384 * ENXIO 25385 * EFAULT 25386 */ 25387 25388 static int 25389 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 25390 { 25391 struct sd_lun *un; 25392 mhioc_inkeys_t inkeys; 25393 int rval = 0; 25394 25395 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25396 return (ENXIO); 25397 } 25398 25399 #ifdef _MULTI_DATAMODEL 25400 switch (ddi_model_convert_from(flag & FMODELS)) { 25401 case DDI_MODEL_ILP32: { 25402 struct mhioc_inkeys32 inkeys32; 25403 25404 if (ddi_copyin(arg, &inkeys32, 25405 sizeof (struct mhioc_inkeys32), flag) != 0) { 25406 return (EFAULT); 25407 } 25408 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 25409 if ((rval = sd_persistent_reservation_in_read_keys(un, 25410 &inkeys, flag)) != 0) { 25411 return (rval); 25412 } 25413 inkeys32.generation = inkeys.generation; 25414 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 25415 flag) != 0) { 25416 return (EFAULT); 25417 } 25418 break; 25419 } 25420 case DDI_MODEL_NONE: 25421 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 25422 flag) != 0) { 25423 return (EFAULT); 25424 } 25425 if ((rval = sd_persistent_reservation_in_read_keys(un, 25426 &inkeys, flag)) != 0) { 25427 return (rval); 25428 } 25429 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 25430 flag) != 0) { 25431 return (EFAULT); 25432 } 25433 break; 25434 } 25435 25436 #else /* ! _MULTI_DATAMODEL */ 25437 25438 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 25439 return (EFAULT); 25440 } 25441 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 25442 if (rval != 0) { 25443 return (rval); 25444 } 25445 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 25446 return (EFAULT); 25447 } 25448 25449 #endif /* _MULTI_DATAMODEL */ 25450 25451 return (rval); 25452 } 25453 25454 25455 /* 25456 * Function: sd_mhdioc_inresv 25457 * 25458 * Description: This routine is the driver entry point for handling ioctl 25459 * requests to issue the SCSI-3 Persistent In Read Reservations 25460 * command to the device (MHIOCGRP_INKEYS). 25461 * 25462 * Arguments: dev - the device number 25463 * arg - user provided in_resv structure 25464 * flag - this argument is a pass through to ddi_copyxxx() 25465 * directly from the mode argument of ioctl(). 25466 * 25467 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 25468 * ENXIO 25469 * EFAULT 25470 */ 25471 25472 static int 25473 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25474 { 25475 struct sd_lun *un; 25476 mhioc_inresvs_t inresvs; 25477 int rval = 0; 25478 25479 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25480 return (ENXIO); 25481 } 25482 25483 #ifdef _MULTI_DATAMODEL 25484 25485 switch (ddi_model_convert_from(flag & FMODELS)) { 25486 case DDI_MODEL_ILP32: { 25487 struct mhioc_inresvs32 inresvs32; 25488 25489 if (ddi_copyin(arg, &inresvs32, 25490 sizeof (struct mhioc_inresvs32), flag) != 0) { 25491 return (EFAULT); 25492 } 25493 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25494 if ((rval = sd_persistent_reservation_in_read_resv(un, 25495 &inresvs, flag)) != 0) { 25496 return (rval); 25497 } 25498 inresvs32.generation = inresvs.generation; 25499 if (ddi_copyout(&inresvs32, arg, 25500 sizeof (struct mhioc_inresvs32), flag) != 0) { 25501 return (EFAULT); 25502 } 25503 break; 25504 } 25505 case DDI_MODEL_NONE: 25506 if (ddi_copyin(arg, &inresvs, 25507 sizeof (mhioc_inresvs_t), flag) != 0) { 25508 return (EFAULT); 25509 } 25510 if ((rval = sd_persistent_reservation_in_read_resv(un, 25511 &inresvs, flag)) != 0) { 25512 return (rval); 25513 } 25514 if (ddi_copyout(&inresvs, arg, 25515 sizeof (mhioc_inresvs_t), flag) != 0) { 25516 return (EFAULT); 25517 } 25518 break; 25519 } 25520 25521 #else /* ! _MULTI_DATAMODEL */ 25522 25523 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25524 return (EFAULT); 25525 } 25526 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25527 if (rval != 0) { 25528 return (rval); 25529 } 25530 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25531 return (EFAULT); 25532 } 25533 25534 #endif /* ! _MULTI_DATAMODEL */ 25535 25536 return (rval); 25537 } 25538 25539 25540 /* 25541 * The following routines support the clustering functionality described below 25542 * and implement lost reservation reclaim functionality. 25543 * 25544 * Clustering 25545 * ---------- 25546 * The clustering code uses two different, independent forms of SCSI 25547 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25548 * Persistent Group Reservations. For any particular disk, it will use either 25549 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25550 * 25551 * SCSI-2 25552 * The cluster software takes ownership of a multi-hosted disk by issuing the 25553 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25554 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25555 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25556 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25557 * meaning of failfast is that if the driver (on this host) ever encounters the 25558 * scsi error return code RESERVATION_CONFLICT from the device, it should 25559 * immediately panic the host. The motivation for this ioctl is that if this 25560 * host does encounter reservation conflict, the underlying cause is that some 25561 * other host of the cluster has decided that this host is no longer in the 25562 * cluster and has seized control of the disks for itself. Since this host is no 25563 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25564 * does two things: 25565 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25566 * error to panic the host 25567 * (b) it sets up a periodic timer to test whether this host still has 25568 * "access" (in that no other host has reserved the device): if the 25569 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25570 * purpose of that periodic timer is to handle scenarios where the host is 25571 * otherwise temporarily quiescent, temporarily doing no real i/o. 25572 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25573 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25574 * the device itself. 25575 * 25576 * SCSI-3 PGR 25577 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25578 * facility is supported through the shared multihost disk ioctls 25579 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25580 * MHIOCGRP_PREEMPTANDABORT) 25581 * 25582 * Reservation Reclaim: 25583 * -------------------- 25584 * To support the lost reservation reclaim operations this driver creates a 25585 * single thread to handle reinstating reservations on all devices that have 25586 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25587 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25588 * and the reservation reclaim thread loops through the requests to regain the 25589 * lost reservations. 25590 */ 25591 25592 /* 25593 * Function: sd_check_mhd() 25594 * 25595 * Description: This function sets up and submits a scsi watch request or 25596 * terminates an existing watch request. This routine is used in 25597 * support of reservation reclaim. 25598 * 25599 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25600 * among multiple watches that share the callback function 25601 * interval - the number of microseconds specifying the watch 25602 * interval for issuing TEST UNIT READY commands. If 25603 * set to 0 the watch should be terminated. If the 25604 * interval is set to 0 and if the device is required 25605 * to hold reservation while disabling failfast, the 25606 * watch is restarted with an interval of 25607 * reinstate_resv_delay. 25608 * 25609 * Return Code: 0 - Successful submit/terminate of scsi watch request 25610 * ENXIO - Indicates an invalid device was specified 25611 * EAGAIN - Unable to submit the scsi watch request 25612 */ 25613 25614 static int 25615 sd_check_mhd(dev_t dev, int interval) 25616 { 25617 struct sd_lun *un; 25618 opaque_t token; 25619 25620 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25621 return (ENXIO); 25622 } 25623 25624 /* is this a watch termination request? */ 25625 if (interval == 0) { 25626 mutex_enter(SD_MUTEX(un)); 25627 /* if there is an existing watch task then terminate it */ 25628 if (un->un_mhd_token) { 25629 token = un->un_mhd_token; 25630 un->un_mhd_token = NULL; 25631 mutex_exit(SD_MUTEX(un)); 25632 (void) scsi_watch_request_terminate(token, 25633 SCSI_WATCH_TERMINATE_WAIT); 25634 mutex_enter(SD_MUTEX(un)); 25635 } else { 25636 mutex_exit(SD_MUTEX(un)); 25637 /* 25638 * Note: If we return here we don't check for the 25639 * failfast case. This is the original legacy 25640 * implementation but perhaps we should be checking 25641 * the failfast case. 25642 */ 25643 return (0); 25644 } 25645 /* 25646 * If the device is required to hold reservation while 25647 * disabling failfast, we need to restart the scsi_watch 25648 * routine with an interval of reinstate_resv_delay. 25649 */ 25650 if (un->un_resvd_status & SD_RESERVE) { 25651 interval = sd_reinstate_resv_delay/1000; 25652 } else { 25653 /* no failfast so bail */ 25654 mutex_exit(SD_MUTEX(un)); 25655 return (0); 25656 } 25657 mutex_exit(SD_MUTEX(un)); 25658 } 25659 25660 /* 25661 * adjust minimum time interval to 1 second, 25662 * and convert from msecs to usecs 25663 */ 25664 if (interval > 0 && interval < 1000) { 25665 interval = 1000; 25666 } 25667 interval *= 1000; 25668 25669 /* 25670 * submit the request to the scsi_watch service 25671 */ 25672 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25673 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25674 if (token == NULL) { 25675 return (EAGAIN); 25676 } 25677 25678 /* 25679 * save token for termination later on 25680 */ 25681 mutex_enter(SD_MUTEX(un)); 25682 un->un_mhd_token = token; 25683 mutex_exit(SD_MUTEX(un)); 25684 return (0); 25685 } 25686 25687 25688 /* 25689 * Function: sd_mhd_watch_cb() 25690 * 25691 * Description: This function is the call back function used by the scsi watch 25692 * facility. The scsi watch facility sends the "Test Unit Ready" 25693 * and processes the status. If applicable (i.e. a "Unit Attention" 25694 * status and automatic "Request Sense" not used) the scsi watch 25695 * facility will send a "Request Sense" and retrieve the sense data 25696 * to be passed to this callback function. In either case the 25697 * automatic "Request Sense" or the facility submitting one, this 25698 * callback is passed the status and sense data. 25699 * 25700 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25701 * among multiple watches that share this callback function 25702 * resultp - scsi watch facility result packet containing scsi 25703 * packet, status byte and sense data 25704 * 25705 * Return Code: 0 - continue the watch task 25706 * non-zero - terminate the watch task 25707 */ 25708 25709 static int 25710 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25711 { 25712 struct sd_lun *un; 25713 struct scsi_status *statusp; 25714 uint8_t *sensep; 25715 struct scsi_pkt *pkt; 25716 uchar_t actual_sense_length; 25717 dev_t dev = (dev_t)arg; 25718 25719 ASSERT(resultp != NULL); 25720 statusp = resultp->statusp; 25721 sensep = (uint8_t *)resultp->sensep; 25722 pkt = resultp->pkt; 25723 actual_sense_length = resultp->actual_sense_length; 25724 25725 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25726 return (ENXIO); 25727 } 25728 25729 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25730 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25731 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25732 25733 /* Begin processing of the status and/or sense data */ 25734 if (pkt->pkt_reason != CMD_CMPLT) { 25735 /* Handle the incomplete packet */ 25736 sd_mhd_watch_incomplete(un, pkt); 25737 return (0); 25738 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25739 if (*((unsigned char *)statusp) 25740 == STATUS_RESERVATION_CONFLICT) { 25741 /* 25742 * Handle a reservation conflict by panicking if 25743 * configured for failfast or by logging the conflict 25744 * and updating the reservation status 25745 */ 25746 mutex_enter(SD_MUTEX(un)); 25747 if ((un->un_resvd_status & SD_FAILFAST) && 25748 (sd_failfast_enable)) { 25749 sd_panic_for_res_conflict(un); 25750 /*NOTREACHED*/ 25751 } 25752 SD_INFO(SD_LOG_IOCTL_MHD, un, 25753 "sd_mhd_watch_cb: Reservation Conflict\n"); 25754 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25755 mutex_exit(SD_MUTEX(un)); 25756 } 25757 } 25758 25759 if (sensep != NULL) { 25760 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25761 mutex_enter(SD_MUTEX(un)); 25762 if ((scsi_sense_asc(sensep) == 25763 SD_SCSI_RESET_SENSE_CODE) && 25764 (un->un_resvd_status & SD_RESERVE)) { 25765 /* 25766 * The additional sense code indicates a power 25767 * on or bus device reset has occurred; update 25768 * the reservation status. 25769 */ 25770 un->un_resvd_status |= 25771 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25772 SD_INFO(SD_LOG_IOCTL_MHD, un, 25773 "sd_mhd_watch_cb: Lost Reservation\n"); 25774 } 25775 } else { 25776 return (0); 25777 } 25778 } else { 25779 mutex_enter(SD_MUTEX(un)); 25780 } 25781 25782 if ((un->un_resvd_status & SD_RESERVE) && 25783 (un->un_resvd_status & SD_LOST_RESERVE)) { 25784 if (un->un_resvd_status & SD_WANT_RESERVE) { 25785 /* 25786 * A reset occurred in between the last probe and this 25787 * one so if a timeout is pending cancel it. 25788 */ 25789 if (un->un_resvd_timeid) { 25790 timeout_id_t temp_id = un->un_resvd_timeid; 25791 un->un_resvd_timeid = NULL; 25792 mutex_exit(SD_MUTEX(un)); 25793 (void) untimeout(temp_id); 25794 mutex_enter(SD_MUTEX(un)); 25795 } 25796 un->un_resvd_status &= ~SD_WANT_RESERVE; 25797 } 25798 if (un->un_resvd_timeid == 0) { 25799 /* Schedule a timeout to handle the lost reservation */ 25800 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25801 (void *)dev, 25802 drv_usectohz(sd_reinstate_resv_delay)); 25803 } 25804 } 25805 mutex_exit(SD_MUTEX(un)); 25806 return (0); 25807 } 25808 25809 25810 /* 25811 * Function: sd_mhd_watch_incomplete() 25812 * 25813 * Description: This function is used to find out why a scsi pkt sent by the 25814 * scsi watch facility was not completed. Under some scenarios this 25815 * routine will return. Otherwise it will send a bus reset to see 25816 * if the drive is still online. 25817 * 25818 * Arguments: un - driver soft state (unit) structure 25819 * pkt - incomplete scsi pkt 25820 */ 25821 25822 static void 25823 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25824 { 25825 int be_chatty; 25826 int perr; 25827 25828 ASSERT(pkt != NULL); 25829 ASSERT(un != NULL); 25830 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25831 perr = (pkt->pkt_statistics & STAT_PERR); 25832 25833 mutex_enter(SD_MUTEX(un)); 25834 if (un->un_state == SD_STATE_DUMPING) { 25835 mutex_exit(SD_MUTEX(un)); 25836 return; 25837 } 25838 25839 switch (pkt->pkt_reason) { 25840 case CMD_UNX_BUS_FREE: 25841 /* 25842 * If we had a parity error that caused the target to drop BSY*, 25843 * don't be chatty about it. 25844 */ 25845 if (perr && be_chatty) { 25846 be_chatty = 0; 25847 } 25848 break; 25849 case CMD_TAG_REJECT: 25850 /* 25851 * The SCSI-2 spec states that a tag reject will be sent by the 25852 * target if tagged queuing is not supported. A tag reject may 25853 * also be sent during certain initialization periods or to 25854 * control internal resources. For the latter case the target 25855 * may also return Queue Full. 25856 * 25857 * If this driver receives a tag reject from a target that is 25858 * going through an init period or controlling internal 25859 * resources tagged queuing will be disabled. This is a less 25860 * than optimal behavior but the driver is unable to determine 25861 * the target state and assumes tagged queueing is not supported 25862 */ 25863 pkt->pkt_flags = 0; 25864 un->un_tagflags = 0; 25865 25866 if (un->un_f_opt_queueing == TRUE) { 25867 un->un_throttle = min(un->un_throttle, 3); 25868 } else { 25869 un->un_throttle = 1; 25870 } 25871 mutex_exit(SD_MUTEX(un)); 25872 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25873 mutex_enter(SD_MUTEX(un)); 25874 break; 25875 case CMD_INCOMPLETE: 25876 /* 25877 * The transport stopped with an abnormal state, fallthrough and 25878 * reset the target and/or bus unless selection did not complete 25879 * (indicated by STATE_GOT_BUS) in which case we don't want to 25880 * go through a target/bus reset 25881 */ 25882 if (pkt->pkt_state == STATE_GOT_BUS) { 25883 break; 25884 } 25885 /*FALLTHROUGH*/ 25886 25887 case CMD_TIMEOUT: 25888 default: 25889 /* 25890 * The lun may still be running the command, so a lun reset 25891 * should be attempted. If the lun reset fails or cannot be 25892 * issued, than try a target reset. Lastly try a bus reset. 25893 */ 25894 if ((pkt->pkt_statistics & 25895 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25896 int reset_retval = 0; 25897 mutex_exit(SD_MUTEX(un)); 25898 if (un->un_f_allow_bus_device_reset == TRUE) { 25899 if (un->un_f_lun_reset_enabled == TRUE) { 25900 reset_retval = 25901 scsi_reset(SD_ADDRESS(un), 25902 RESET_LUN); 25903 } 25904 if (reset_retval == 0) { 25905 reset_retval = 25906 scsi_reset(SD_ADDRESS(un), 25907 RESET_TARGET); 25908 } 25909 } 25910 if (reset_retval == 0) { 25911 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25912 } 25913 mutex_enter(SD_MUTEX(un)); 25914 } 25915 break; 25916 } 25917 25918 /* A device/bus reset has occurred; update the reservation status. */ 25919 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25920 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25921 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25922 un->un_resvd_status |= 25923 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25924 SD_INFO(SD_LOG_IOCTL_MHD, un, 25925 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25926 } 25927 } 25928 25929 /* 25930 * The disk has been turned off; Update the device state. 25931 * 25932 * Note: Should we be offlining the disk here? 25933 */ 25934 if (pkt->pkt_state == STATE_GOT_BUS) { 25935 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25936 "Disk not responding to selection\n"); 25937 if (un->un_state != SD_STATE_OFFLINE) { 25938 New_state(un, SD_STATE_OFFLINE); 25939 } 25940 } else if (be_chatty) { 25941 /* 25942 * suppress messages if they are all the same pkt reason; 25943 * with TQ, many (up to 256) are returned with the same 25944 * pkt_reason 25945 */ 25946 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25947 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25948 "sd_mhd_watch_incomplete: " 25949 "SCSI transport failed: reason '%s'\n", 25950 scsi_rname(pkt->pkt_reason)); 25951 } 25952 } 25953 un->un_last_pkt_reason = pkt->pkt_reason; 25954 mutex_exit(SD_MUTEX(un)); 25955 } 25956 25957 25958 /* 25959 * Function: sd_sname() 25960 * 25961 * Description: This is a simple little routine to return a string containing 25962 * a printable description of command status byte for use in 25963 * logging. 25964 * 25965 * Arguments: status - pointer to a status byte 25966 * 25967 * Return Code: char * - string containing status description. 25968 */ 25969 25970 static char * 25971 sd_sname(uchar_t status) 25972 { 25973 switch (status & STATUS_MASK) { 25974 case STATUS_GOOD: 25975 return ("good status"); 25976 case STATUS_CHECK: 25977 return ("check condition"); 25978 case STATUS_MET: 25979 return ("condition met"); 25980 case STATUS_BUSY: 25981 return ("busy"); 25982 case STATUS_INTERMEDIATE: 25983 return ("intermediate"); 25984 case STATUS_INTERMEDIATE_MET: 25985 return ("intermediate - condition met"); 25986 case STATUS_RESERVATION_CONFLICT: 25987 return ("reservation_conflict"); 25988 case STATUS_TERMINATED: 25989 return ("command terminated"); 25990 case STATUS_QFULL: 25991 return ("queue full"); 25992 default: 25993 return ("<unknown status>"); 25994 } 25995 } 25996 25997 25998 /* 25999 * Function: sd_mhd_resvd_recover() 26000 * 26001 * Description: This function adds a reservation entry to the 26002 * sd_resv_reclaim_request list and signals the reservation 26003 * reclaim thread that there is work pending. If the reservation 26004 * reclaim thread has not been previously created this function 26005 * will kick it off. 26006 * 26007 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26008 * among multiple watches that share this callback function 26009 * 26010 * Context: This routine is called by timeout() and is run in interrupt 26011 * context. It must not sleep or call other functions which may 26012 * sleep. 26013 */ 26014 26015 static void 26016 sd_mhd_resvd_recover(void *arg) 26017 { 26018 dev_t dev = (dev_t)arg; 26019 struct sd_lun *un; 26020 struct sd_thr_request *sd_treq = NULL; 26021 struct sd_thr_request *sd_cur = NULL; 26022 struct sd_thr_request *sd_prev = NULL; 26023 int already_there = 0; 26024 26025 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26026 return; 26027 } 26028 26029 mutex_enter(SD_MUTEX(un)); 26030 un->un_resvd_timeid = NULL; 26031 if (un->un_resvd_status & SD_WANT_RESERVE) { 26032 /* 26033 * There was a reset so don't issue the reserve, allow the 26034 * sd_mhd_watch_cb callback function to notice this and 26035 * reschedule the timeout for reservation. 26036 */ 26037 mutex_exit(SD_MUTEX(un)); 26038 return; 26039 } 26040 mutex_exit(SD_MUTEX(un)); 26041 26042 /* 26043 * Add this device to the sd_resv_reclaim_request list and the 26044 * sd_resv_reclaim_thread should take care of the rest. 26045 * 26046 * Note: We can't sleep in this context so if the memory allocation 26047 * fails allow the sd_mhd_watch_cb callback function to notice this and 26048 * reschedule the timeout for reservation. (4378460) 26049 */ 26050 sd_treq = (struct sd_thr_request *) 26051 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 26052 if (sd_treq == NULL) { 26053 return; 26054 } 26055 26056 sd_treq->sd_thr_req_next = NULL; 26057 sd_treq->dev = dev; 26058 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26059 if (sd_tr.srq_thr_req_head == NULL) { 26060 sd_tr.srq_thr_req_head = sd_treq; 26061 } else { 26062 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 26063 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 26064 if (sd_cur->dev == dev) { 26065 /* 26066 * already in Queue so don't log 26067 * another request for the device 26068 */ 26069 already_there = 1; 26070 break; 26071 } 26072 sd_prev = sd_cur; 26073 } 26074 if (!already_there) { 26075 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 26076 "logging request for %lx\n", dev); 26077 sd_prev->sd_thr_req_next = sd_treq; 26078 } else { 26079 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 26080 } 26081 } 26082 26083 /* 26084 * Create a kernel thread to do the reservation reclaim and free up this 26085 * thread. We cannot block this thread while we go away to do the 26086 * reservation reclaim 26087 */ 26088 if (sd_tr.srq_resv_reclaim_thread == NULL) 26089 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 26090 sd_resv_reclaim_thread, NULL, 26091 0, &p0, TS_RUN, v.v_maxsyspri - 2); 26092 26093 /* Tell the reservation reclaim thread that it has work to do */ 26094 cv_signal(&sd_tr.srq_resv_reclaim_cv); 26095 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26096 } 26097 26098 /* 26099 * Function: sd_resv_reclaim_thread() 26100 * 26101 * Description: This function implements the reservation reclaim operations 26102 * 26103 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26104 * among multiple watches that share this callback function 26105 */ 26106 26107 static void 26108 sd_resv_reclaim_thread() 26109 { 26110 struct sd_lun *un; 26111 struct sd_thr_request *sd_mhreq; 26112 26113 /* Wait for work */ 26114 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26115 if (sd_tr.srq_thr_req_head == NULL) { 26116 cv_wait(&sd_tr.srq_resv_reclaim_cv, 26117 &sd_tr.srq_resv_reclaim_mutex); 26118 } 26119 26120 /* Loop while we have work */ 26121 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 26122 un = ddi_get_soft_state(sd_state, 26123 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 26124 if (un == NULL) { 26125 /* 26126 * softstate structure is NULL so just 26127 * dequeue the request and continue 26128 */ 26129 sd_tr.srq_thr_req_head = 26130 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26131 kmem_free(sd_tr.srq_thr_cur_req, 26132 sizeof (struct sd_thr_request)); 26133 continue; 26134 } 26135 26136 /* dequeue the request */ 26137 sd_mhreq = sd_tr.srq_thr_cur_req; 26138 sd_tr.srq_thr_req_head = 26139 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26140 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26141 26142 /* 26143 * Reclaim reservation only if SD_RESERVE is still set. There 26144 * may have been a call to MHIOCRELEASE before we got here. 26145 */ 26146 mutex_enter(SD_MUTEX(un)); 26147 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26148 /* 26149 * Note: The SD_LOST_RESERVE flag is cleared before 26150 * reclaiming the reservation. If this is done after the 26151 * call to sd_reserve_release a reservation loss in the 26152 * window between pkt completion of reserve cmd and 26153 * mutex_enter below may not be recognized 26154 */ 26155 un->un_resvd_status &= ~SD_LOST_RESERVE; 26156 mutex_exit(SD_MUTEX(un)); 26157 26158 if (sd_reserve_release(sd_mhreq->dev, 26159 SD_RESERVE) == 0) { 26160 mutex_enter(SD_MUTEX(un)); 26161 un->un_resvd_status |= SD_RESERVE; 26162 mutex_exit(SD_MUTEX(un)); 26163 SD_INFO(SD_LOG_IOCTL_MHD, un, 26164 "sd_resv_reclaim_thread: " 26165 "Reservation Recovered\n"); 26166 } else { 26167 mutex_enter(SD_MUTEX(un)); 26168 un->un_resvd_status |= SD_LOST_RESERVE; 26169 mutex_exit(SD_MUTEX(un)); 26170 SD_INFO(SD_LOG_IOCTL_MHD, un, 26171 "sd_resv_reclaim_thread: Failed " 26172 "Reservation Recovery\n"); 26173 } 26174 } else { 26175 mutex_exit(SD_MUTEX(un)); 26176 } 26177 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26178 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 26179 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26180 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 26181 /* 26182 * wakeup the destroy thread if anyone is waiting on 26183 * us to complete. 26184 */ 26185 cv_signal(&sd_tr.srq_inprocess_cv); 26186 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26187 "sd_resv_reclaim_thread: cv_signalling current request \n"); 26188 } 26189 26190 /* 26191 * cleanup the sd_tr structure now that this thread will not exist 26192 */ 26193 ASSERT(sd_tr.srq_thr_req_head == NULL); 26194 ASSERT(sd_tr.srq_thr_cur_req == NULL); 26195 sd_tr.srq_resv_reclaim_thread = NULL; 26196 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26197 thread_exit(); 26198 } 26199 26200 26201 /* 26202 * Function: sd_rmv_resv_reclaim_req() 26203 * 26204 * Description: This function removes any pending reservation reclaim requests 26205 * for the specified device. 26206 * 26207 * Arguments: dev - the device 'dev_t' 26208 */ 26209 26210 static void 26211 sd_rmv_resv_reclaim_req(dev_t dev) 26212 { 26213 struct sd_thr_request *sd_mhreq; 26214 struct sd_thr_request *sd_prev; 26215 26216 /* Remove a reservation reclaim request from the list */ 26217 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26218 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 26219 /* 26220 * We are attempting to reinstate reservation for 26221 * this device. We wait for sd_reserve_release() 26222 * to return before we return. 26223 */ 26224 cv_wait(&sd_tr.srq_inprocess_cv, 26225 &sd_tr.srq_resv_reclaim_mutex); 26226 } else { 26227 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 26228 if (sd_mhreq && sd_mhreq->dev == dev) { 26229 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 26230 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26231 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26232 return; 26233 } 26234 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 26235 if (sd_mhreq && sd_mhreq->dev == dev) { 26236 break; 26237 } 26238 sd_prev = sd_mhreq; 26239 } 26240 if (sd_mhreq != NULL) { 26241 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 26242 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26243 } 26244 } 26245 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26246 } 26247 26248 26249 /* 26250 * Function: sd_mhd_reset_notify_cb() 26251 * 26252 * Description: This is a call back function for scsi_reset_notify. This 26253 * function updates the softstate reserved status and logs the 26254 * reset. The driver scsi watch facility callback function 26255 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 26256 * will reclaim the reservation. 26257 * 26258 * Arguments: arg - driver soft state (unit) structure 26259 */ 26260 26261 static void 26262 sd_mhd_reset_notify_cb(caddr_t arg) 26263 { 26264 struct sd_lun *un = (struct sd_lun *)arg; 26265 26266 mutex_enter(SD_MUTEX(un)); 26267 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26268 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 26269 SD_INFO(SD_LOG_IOCTL_MHD, un, 26270 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 26271 } 26272 mutex_exit(SD_MUTEX(un)); 26273 } 26274 26275 26276 /* 26277 * Function: sd_take_ownership() 26278 * 26279 * Description: This routine implements an algorithm to achieve a stable 26280 * reservation on disks which don't implement priority reserve, 26281 * and makes sure that other host lose re-reservation attempts. 26282 * This algorithm contains of a loop that keeps issuing the RESERVE 26283 * for some period of time (min_ownership_delay, default 6 seconds) 26284 * During that loop, it looks to see if there has been a bus device 26285 * reset or bus reset (both of which cause an existing reservation 26286 * to be lost). If the reservation is lost issue RESERVE until a 26287 * period of min_ownership_delay with no resets has gone by, or 26288 * until max_ownership_delay has expired. This loop ensures that 26289 * the host really did manage to reserve the device, in spite of 26290 * resets. The looping for min_ownership_delay (default six 26291 * seconds) is important to early generation clustering products, 26292 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 26293 * MHIOCENFAILFAST periodic timer of two seconds. By having 26294 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 26295 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 26296 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 26297 * have already noticed, via the MHIOCENFAILFAST polling, that it 26298 * no longer "owns" the disk and will have panicked itself. Thus, 26299 * the host issuing the MHIOCTKOWN is assured (with timing 26300 * dependencies) that by the time it actually starts to use the 26301 * disk for real work, the old owner is no longer accessing it. 26302 * 26303 * min_ownership_delay is the minimum amount of time for which the 26304 * disk must be reserved continuously devoid of resets before the 26305 * MHIOCTKOWN ioctl will return success. 26306 * 26307 * max_ownership_delay indicates the amount of time by which the 26308 * take ownership should succeed or timeout with an error. 26309 * 26310 * Arguments: dev - the device 'dev_t' 26311 * *p - struct containing timing info. 26312 * 26313 * Return Code: 0 for success or error code 26314 */ 26315 26316 static int 26317 sd_take_ownership(dev_t dev, struct mhioctkown *p) 26318 { 26319 struct sd_lun *un; 26320 int rval; 26321 int err; 26322 int reservation_count = 0; 26323 int min_ownership_delay = 6000000; /* in usec */ 26324 int max_ownership_delay = 30000000; /* in usec */ 26325 clock_t start_time; /* starting time of this algorithm */ 26326 clock_t end_time; /* time limit for giving up */ 26327 clock_t ownership_time; /* time limit for stable ownership */ 26328 clock_t current_time; 26329 clock_t previous_current_time; 26330 26331 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26332 return (ENXIO); 26333 } 26334 26335 /* 26336 * Attempt a device reservation. A priority reservation is requested. 26337 */ 26338 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 26339 != SD_SUCCESS) { 26340 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26341 "sd_take_ownership: return(1)=%d\n", rval); 26342 return (rval); 26343 } 26344 26345 /* Update the softstate reserved status to indicate the reservation */ 26346 mutex_enter(SD_MUTEX(un)); 26347 un->un_resvd_status |= SD_RESERVE; 26348 un->un_resvd_status &= 26349 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 26350 mutex_exit(SD_MUTEX(un)); 26351 26352 if (p != NULL) { 26353 if (p->min_ownership_delay != 0) { 26354 min_ownership_delay = p->min_ownership_delay * 1000; 26355 } 26356 if (p->max_ownership_delay != 0) { 26357 max_ownership_delay = p->max_ownership_delay * 1000; 26358 } 26359 } 26360 SD_INFO(SD_LOG_IOCTL_MHD, un, 26361 "sd_take_ownership: min, max delays: %d, %d\n", 26362 min_ownership_delay, max_ownership_delay); 26363 26364 start_time = ddi_get_lbolt(); 26365 current_time = start_time; 26366 ownership_time = current_time + drv_usectohz(min_ownership_delay); 26367 end_time = start_time + drv_usectohz(max_ownership_delay); 26368 26369 while (current_time - end_time < 0) { 26370 delay(drv_usectohz(500000)); 26371 26372 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 26373 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 26374 mutex_enter(SD_MUTEX(un)); 26375 rval = (un->un_resvd_status & 26376 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 26377 mutex_exit(SD_MUTEX(un)); 26378 break; 26379 } 26380 } 26381 previous_current_time = current_time; 26382 current_time = ddi_get_lbolt(); 26383 mutex_enter(SD_MUTEX(un)); 26384 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 26385 ownership_time = ddi_get_lbolt() + 26386 drv_usectohz(min_ownership_delay); 26387 reservation_count = 0; 26388 } else { 26389 reservation_count++; 26390 } 26391 un->un_resvd_status |= SD_RESERVE; 26392 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 26393 mutex_exit(SD_MUTEX(un)); 26394 26395 SD_INFO(SD_LOG_IOCTL_MHD, un, 26396 "sd_take_ownership: ticks for loop iteration=%ld, " 26397 "reservation=%s\n", (current_time - previous_current_time), 26398 reservation_count ? "ok" : "reclaimed"); 26399 26400 if (current_time - ownership_time >= 0 && 26401 reservation_count >= 4) { 26402 rval = 0; /* Achieved a stable ownership */ 26403 break; 26404 } 26405 if (current_time - end_time >= 0) { 26406 rval = EACCES; /* No ownership in max possible time */ 26407 break; 26408 } 26409 } 26410 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26411 "sd_take_ownership: return(2)=%d\n", rval); 26412 return (rval); 26413 } 26414 26415 26416 /* 26417 * Function: sd_reserve_release() 26418 * 26419 * Description: This function builds and sends scsi RESERVE, RELEASE, and 26420 * PRIORITY RESERVE commands based on a user specified command type 26421 * 26422 * Arguments: dev - the device 'dev_t' 26423 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 26424 * SD_RESERVE, SD_RELEASE 26425 * 26426 * Return Code: 0 or Error Code 26427 */ 26428 26429 static int 26430 sd_reserve_release(dev_t dev, int cmd) 26431 { 26432 struct uscsi_cmd *com = NULL; 26433 struct sd_lun *un = NULL; 26434 char cdb[CDB_GROUP0]; 26435 int rval; 26436 26437 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 26438 (cmd == SD_PRIORITY_RESERVE)); 26439 26440 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26441 return (ENXIO); 26442 } 26443 26444 /* instantiate and initialize the command and cdb */ 26445 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26446 bzero(cdb, CDB_GROUP0); 26447 com->uscsi_flags = USCSI_SILENT; 26448 com->uscsi_timeout = un->un_reserve_release_time; 26449 com->uscsi_cdblen = CDB_GROUP0; 26450 com->uscsi_cdb = cdb; 26451 if (cmd == SD_RELEASE) { 26452 cdb[0] = SCMD_RELEASE; 26453 } else { 26454 cdb[0] = SCMD_RESERVE; 26455 } 26456 26457 /* Send the command. */ 26458 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26459 UIO_SYSSPACE, SD_PATH_STANDARD); 26460 26461 /* 26462 * "break" a reservation that is held by another host, by issuing a 26463 * reset if priority reserve is desired, and we could not get the 26464 * device. 26465 */ 26466 if ((cmd == SD_PRIORITY_RESERVE) && 26467 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26468 /* 26469 * First try to reset the LUN. If we cannot, then try a target 26470 * reset, followed by a bus reset if the target reset fails. 26471 */ 26472 int reset_retval = 0; 26473 if (un->un_f_lun_reset_enabled == TRUE) { 26474 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26475 } 26476 if (reset_retval == 0) { 26477 /* The LUN reset either failed or was not issued */ 26478 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26479 } 26480 if ((reset_retval == 0) && 26481 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26482 rval = EIO; 26483 kmem_free(com, sizeof (*com)); 26484 return (rval); 26485 } 26486 26487 bzero(com, sizeof (struct uscsi_cmd)); 26488 com->uscsi_flags = USCSI_SILENT; 26489 com->uscsi_cdb = cdb; 26490 com->uscsi_cdblen = CDB_GROUP0; 26491 com->uscsi_timeout = 5; 26492 26493 /* 26494 * Reissue the last reserve command, this time without request 26495 * sense. Assume that it is just a regular reserve command. 26496 */ 26497 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26498 UIO_SYSSPACE, SD_PATH_STANDARD); 26499 } 26500 26501 /* Return an error if still getting a reservation conflict. */ 26502 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26503 rval = EACCES; 26504 } 26505 26506 kmem_free(com, sizeof (*com)); 26507 return (rval); 26508 } 26509 26510 26511 #define SD_NDUMP_RETRIES 12 26512 /* 26513 * System Crash Dump routine 26514 */ 26515 26516 static int 26517 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26518 { 26519 int instance; 26520 int partition; 26521 int i; 26522 int err; 26523 struct sd_lun *un; 26524 struct dk_map *lp; 26525 struct scsi_pkt *wr_pktp; 26526 struct buf *wr_bp; 26527 struct buf wr_buf; 26528 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26529 daddr_t tgt_blkno; /* rmw - blkno for target */ 26530 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26531 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26532 size_t io_start_offset; 26533 int doing_rmw = FALSE; 26534 int rval; 26535 #if defined(__i386) || defined(__amd64) 26536 ssize_t dma_resid; 26537 daddr_t oblkno; 26538 #endif 26539 26540 instance = SDUNIT(dev); 26541 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26542 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26543 return (ENXIO); 26544 } 26545 26546 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26547 26548 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26549 26550 partition = SDPART(dev); 26551 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26552 26553 /* Validate blocks to dump at against partition size. */ 26554 lp = &un->un_map[partition]; 26555 if ((blkno + nblk) > lp->dkl_nblk) { 26556 SD_TRACE(SD_LOG_DUMP, un, 26557 "sddump: dump range larger than partition: " 26558 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26559 blkno, nblk, lp->dkl_nblk); 26560 return (EINVAL); 26561 } 26562 26563 mutex_enter(&un->un_pm_mutex); 26564 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26565 struct scsi_pkt *start_pktp; 26566 26567 mutex_exit(&un->un_pm_mutex); 26568 26569 /* 26570 * use pm framework to power on HBA 1st 26571 */ 26572 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26573 26574 /* 26575 * Dump no long uses sdpower to power on a device, it's 26576 * in-line here so it can be done in polled mode. 26577 */ 26578 26579 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26580 26581 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26582 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26583 26584 if (start_pktp == NULL) { 26585 /* We were not given a SCSI packet, fail. */ 26586 return (EIO); 26587 } 26588 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26589 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26590 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26591 start_pktp->pkt_flags = FLAG_NOINTR; 26592 26593 mutex_enter(SD_MUTEX(un)); 26594 SD_FILL_SCSI1_LUN(un, start_pktp); 26595 mutex_exit(SD_MUTEX(un)); 26596 /* 26597 * Scsi_poll returns 0 (success) if the command completes and 26598 * the status block is STATUS_GOOD. 26599 */ 26600 if (sd_scsi_poll(un, start_pktp) != 0) { 26601 scsi_destroy_pkt(start_pktp); 26602 return (EIO); 26603 } 26604 scsi_destroy_pkt(start_pktp); 26605 (void) sd_ddi_pm_resume(un); 26606 } else { 26607 mutex_exit(&un->un_pm_mutex); 26608 } 26609 26610 mutex_enter(SD_MUTEX(un)); 26611 un->un_throttle = 0; 26612 26613 /* 26614 * The first time through, reset the specific target device. 26615 * However, when cpr calls sddump we know that sd is in a 26616 * a good state so no bus reset is required. 26617 * Clear sense data via Request Sense cmd. 26618 * In sddump we don't care about allow_bus_device_reset anymore 26619 */ 26620 26621 if ((un->un_state != SD_STATE_SUSPENDED) && 26622 (un->un_state != SD_STATE_DUMPING)) { 26623 26624 New_state(un, SD_STATE_DUMPING); 26625 26626 if (un->un_f_is_fibre == FALSE) { 26627 mutex_exit(SD_MUTEX(un)); 26628 /* 26629 * Attempt a bus reset for parallel scsi. 26630 * 26631 * Note: A bus reset is required because on some host 26632 * systems (i.e. E420R) a bus device reset is 26633 * insufficient to reset the state of the target. 26634 * 26635 * Note: Don't issue the reset for fibre-channel, 26636 * because this tends to hang the bus (loop) for 26637 * too long while everyone is logging out and in 26638 * and the deadman timer for dumping will fire 26639 * before the dump is complete. 26640 */ 26641 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26642 mutex_enter(SD_MUTEX(un)); 26643 Restore_state(un); 26644 mutex_exit(SD_MUTEX(un)); 26645 return (EIO); 26646 } 26647 26648 /* Delay to give the device some recovery time. */ 26649 drv_usecwait(10000); 26650 26651 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26652 SD_INFO(SD_LOG_DUMP, un, 26653 "sddump: sd_send_polled_RQS failed\n"); 26654 } 26655 mutex_enter(SD_MUTEX(un)); 26656 } 26657 } 26658 26659 /* 26660 * Convert the partition-relative block number to a 26661 * disk physical block number. 26662 */ 26663 blkno += un->un_offset[partition]; 26664 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26665 26666 26667 /* 26668 * Check if the device has a non-512 block size. 26669 */ 26670 wr_bp = NULL; 26671 if (NOT_DEVBSIZE(un)) { 26672 tgt_byte_offset = blkno * un->un_sys_blocksize; 26673 tgt_byte_count = nblk * un->un_sys_blocksize; 26674 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26675 (tgt_byte_count % un->un_tgt_blocksize)) { 26676 doing_rmw = TRUE; 26677 /* 26678 * Calculate the block number and number of block 26679 * in terms of the media block size. 26680 */ 26681 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26682 tgt_nblk = 26683 ((tgt_byte_offset + tgt_byte_count + 26684 (un->un_tgt_blocksize - 1)) / 26685 un->un_tgt_blocksize) - tgt_blkno; 26686 26687 /* 26688 * Invoke the routine which is going to do read part 26689 * of read-modify-write. 26690 * Note that this routine returns a pointer to 26691 * a valid bp in wr_bp. 26692 */ 26693 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26694 &wr_bp); 26695 if (err) { 26696 mutex_exit(SD_MUTEX(un)); 26697 return (err); 26698 } 26699 /* 26700 * Offset is being calculated as - 26701 * (original block # * system block size) - 26702 * (new block # * target block size) 26703 */ 26704 io_start_offset = 26705 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26706 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26707 26708 ASSERT((io_start_offset >= 0) && 26709 (io_start_offset < un->un_tgt_blocksize)); 26710 /* 26711 * Do the modify portion of read modify write. 26712 */ 26713 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26714 (size_t)nblk * un->un_sys_blocksize); 26715 } else { 26716 doing_rmw = FALSE; 26717 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26718 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26719 } 26720 26721 /* Convert blkno and nblk to target blocks */ 26722 blkno = tgt_blkno; 26723 nblk = tgt_nblk; 26724 } else { 26725 wr_bp = &wr_buf; 26726 bzero(wr_bp, sizeof (struct buf)); 26727 wr_bp->b_flags = B_BUSY; 26728 wr_bp->b_un.b_addr = addr; 26729 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26730 wr_bp->b_resid = 0; 26731 } 26732 26733 mutex_exit(SD_MUTEX(un)); 26734 26735 /* 26736 * Obtain a SCSI packet for the write command. 26737 * It should be safe to call the allocator here without 26738 * worrying about being locked for DVMA mapping because 26739 * the address we're passed is already a DVMA mapping 26740 * 26741 * We are also not going to worry about semaphore ownership 26742 * in the dump buffer. Dumping is single threaded at present. 26743 */ 26744 26745 wr_pktp = NULL; 26746 26747 #if defined(__i386) || defined(__amd64) 26748 dma_resid = wr_bp->b_bcount; 26749 oblkno = blkno; 26750 while (dma_resid != 0) { 26751 #endif 26752 26753 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26754 wr_bp->b_flags &= ~B_ERROR; 26755 26756 #if defined(__i386) || defined(__amd64) 26757 blkno = oblkno + 26758 ((wr_bp->b_bcount - dma_resid) / 26759 un->un_tgt_blocksize); 26760 nblk = dma_resid / un->un_tgt_blocksize; 26761 26762 if (wr_pktp) { 26763 /* Partial DMA transfers after initial transfer */ 26764 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26765 blkno, nblk); 26766 } else { 26767 /* Initial transfer */ 26768 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26769 un->un_pkt_flags, NULL_FUNC, NULL, 26770 blkno, nblk); 26771 } 26772 #else 26773 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26774 0, NULL_FUNC, NULL, blkno, nblk); 26775 #endif 26776 26777 if (rval == 0) { 26778 /* We were given a SCSI packet, continue. */ 26779 break; 26780 } 26781 26782 if (i == 0) { 26783 if (wr_bp->b_flags & B_ERROR) { 26784 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26785 "no resources for dumping; " 26786 "error code: 0x%x, retrying", 26787 geterror(wr_bp)); 26788 } else { 26789 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26790 "no resources for dumping; retrying"); 26791 } 26792 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26793 if (wr_bp->b_flags & B_ERROR) { 26794 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26795 "no resources for dumping; error code: " 26796 "0x%x, retrying\n", geterror(wr_bp)); 26797 } 26798 } else { 26799 if (wr_bp->b_flags & B_ERROR) { 26800 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26801 "no resources for dumping; " 26802 "error code: 0x%x, retries failed, " 26803 "giving up.\n", geterror(wr_bp)); 26804 } else { 26805 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26806 "no resources for dumping; " 26807 "retries failed, giving up.\n"); 26808 } 26809 mutex_enter(SD_MUTEX(un)); 26810 Restore_state(un); 26811 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26812 mutex_exit(SD_MUTEX(un)); 26813 scsi_free_consistent_buf(wr_bp); 26814 } else { 26815 mutex_exit(SD_MUTEX(un)); 26816 } 26817 return (EIO); 26818 } 26819 drv_usecwait(10000); 26820 } 26821 26822 #if defined(__i386) || defined(__amd64) 26823 /* 26824 * save the resid from PARTIAL_DMA 26825 */ 26826 dma_resid = wr_pktp->pkt_resid; 26827 if (dma_resid != 0) 26828 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26829 wr_pktp->pkt_resid = 0; 26830 #endif 26831 26832 /* SunBug 1222170 */ 26833 wr_pktp->pkt_flags = FLAG_NOINTR; 26834 26835 err = EIO; 26836 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26837 26838 /* 26839 * Scsi_poll returns 0 (success) if the command completes and 26840 * the status block is STATUS_GOOD. We should only check 26841 * errors if this condition is not true. Even then we should 26842 * send our own request sense packet only if we have a check 26843 * condition and auto request sense has not been performed by 26844 * the hba. 26845 */ 26846 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26847 26848 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26849 (wr_pktp->pkt_resid == 0)) { 26850 err = SD_SUCCESS; 26851 break; 26852 } 26853 26854 /* 26855 * Check CMD_DEV_GONE 1st, give up if device is gone. 26856 */ 26857 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26858 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26859 "Device is gone\n"); 26860 break; 26861 } 26862 26863 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26864 SD_INFO(SD_LOG_DUMP, un, 26865 "sddump: write failed with CHECK, try # %d\n", i); 26866 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26867 (void) sd_send_polled_RQS(un); 26868 } 26869 26870 continue; 26871 } 26872 26873 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26874 int reset_retval = 0; 26875 26876 SD_INFO(SD_LOG_DUMP, un, 26877 "sddump: write failed with BUSY, try # %d\n", i); 26878 26879 if (un->un_f_lun_reset_enabled == TRUE) { 26880 reset_retval = scsi_reset(SD_ADDRESS(un), 26881 RESET_LUN); 26882 } 26883 if (reset_retval == 0) { 26884 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26885 } 26886 (void) sd_send_polled_RQS(un); 26887 26888 } else { 26889 SD_INFO(SD_LOG_DUMP, un, 26890 "sddump: write failed with 0x%x, try # %d\n", 26891 SD_GET_PKT_STATUS(wr_pktp), i); 26892 mutex_enter(SD_MUTEX(un)); 26893 sd_reset_target(un, wr_pktp); 26894 mutex_exit(SD_MUTEX(un)); 26895 } 26896 26897 /* 26898 * If we are not getting anywhere with lun/target resets, 26899 * let's reset the bus. 26900 */ 26901 if (i == SD_NDUMP_RETRIES/2) { 26902 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26903 (void) sd_send_polled_RQS(un); 26904 } 26905 26906 } 26907 #if defined(__i386) || defined(__amd64) 26908 } /* dma_resid */ 26909 #endif 26910 26911 scsi_destroy_pkt(wr_pktp); 26912 mutex_enter(SD_MUTEX(un)); 26913 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26914 mutex_exit(SD_MUTEX(un)); 26915 scsi_free_consistent_buf(wr_bp); 26916 } else { 26917 mutex_exit(SD_MUTEX(un)); 26918 } 26919 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26920 return (err); 26921 } 26922 26923 /* 26924 * Function: sd_scsi_poll() 26925 * 26926 * Description: This is a wrapper for the scsi_poll call. 26927 * 26928 * Arguments: sd_lun - The unit structure 26929 * scsi_pkt - The scsi packet being sent to the device. 26930 * 26931 * Return Code: 0 - Command completed successfully with good status 26932 * -1 - Command failed. This could indicate a check condition 26933 * or other status value requiring recovery action. 26934 * 26935 */ 26936 26937 static int 26938 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26939 { 26940 int status; 26941 26942 ASSERT(un != NULL); 26943 ASSERT(!mutex_owned(SD_MUTEX(un))); 26944 ASSERT(pktp != NULL); 26945 26946 status = SD_SUCCESS; 26947 26948 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26949 pktp->pkt_flags |= un->un_tagflags; 26950 pktp->pkt_flags &= ~FLAG_NODISCON; 26951 } 26952 26953 status = sd_ddi_scsi_poll(pktp); 26954 /* 26955 * Scsi_poll returns 0 (success) if the command completes and the 26956 * status block is STATUS_GOOD. We should only check errors if this 26957 * condition is not true. Even then we should send our own request 26958 * sense packet only if we have a check condition and auto 26959 * request sense has not been performed by the hba. 26960 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26961 */ 26962 if ((status != SD_SUCCESS) && 26963 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26964 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26965 (pktp->pkt_reason != CMD_DEV_GONE)) 26966 (void) sd_send_polled_RQS(un); 26967 26968 return (status); 26969 } 26970 26971 /* 26972 * Function: sd_send_polled_RQS() 26973 * 26974 * Description: This sends the request sense command to a device. 26975 * 26976 * Arguments: sd_lun - The unit structure 26977 * 26978 * Return Code: 0 - Command completed successfully with good status 26979 * -1 - Command failed. 26980 * 26981 */ 26982 26983 static int 26984 sd_send_polled_RQS(struct sd_lun *un) 26985 { 26986 int ret_val; 26987 struct scsi_pkt *rqs_pktp; 26988 struct buf *rqs_bp; 26989 26990 ASSERT(un != NULL); 26991 ASSERT(!mutex_owned(SD_MUTEX(un))); 26992 26993 ret_val = SD_SUCCESS; 26994 26995 rqs_pktp = un->un_rqs_pktp; 26996 rqs_bp = un->un_rqs_bp; 26997 26998 mutex_enter(SD_MUTEX(un)); 26999 27000 if (un->un_sense_isbusy) { 27001 ret_val = SD_FAILURE; 27002 mutex_exit(SD_MUTEX(un)); 27003 return (ret_val); 27004 } 27005 27006 /* 27007 * If the request sense buffer (and packet) is not in use, 27008 * let's set the un_sense_isbusy and send our packet 27009 */ 27010 un->un_sense_isbusy = 1; 27011 rqs_pktp->pkt_resid = 0; 27012 rqs_pktp->pkt_reason = 0; 27013 rqs_pktp->pkt_flags |= FLAG_NOINTR; 27014 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 27015 27016 mutex_exit(SD_MUTEX(un)); 27017 27018 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 27019 " 0x%p\n", rqs_bp->b_un.b_addr); 27020 27021 /* 27022 * Can't send this to sd_scsi_poll, we wrap ourselves around the 27023 * axle - it has a call into us! 27024 */ 27025 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 27026 SD_INFO(SD_LOG_COMMON, un, 27027 "sd_send_polled_RQS: RQS failed\n"); 27028 } 27029 27030 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 27031 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 27032 27033 mutex_enter(SD_MUTEX(un)); 27034 un->un_sense_isbusy = 0; 27035 mutex_exit(SD_MUTEX(un)); 27036 27037 return (ret_val); 27038 } 27039 27040 /* 27041 * Defines needed for localized version of the scsi_poll routine. 27042 */ 27043 #define SD_CSEC 10000 /* usecs */ 27044 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 27045 27046 27047 /* 27048 * Function: sd_ddi_scsi_poll() 27049 * 27050 * Description: Localized version of the scsi_poll routine. The purpose is to 27051 * send a scsi_pkt to a device as a polled command. This version 27052 * is to ensure more robust handling of transport errors. 27053 * Specifically this routine cures not ready, coming ready 27054 * transition for power up and reset of sonoma's. This can take 27055 * up to 45 seconds for power-on and 20 seconds for reset of a 27056 * sonoma lun. 27057 * 27058 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 27059 * 27060 * Return Code: 0 - Command completed successfully with good status 27061 * -1 - Command failed. 27062 * 27063 */ 27064 27065 static int 27066 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 27067 { 27068 int busy_count; 27069 int timeout; 27070 int rval = SD_FAILURE; 27071 int savef; 27072 uint8_t *sensep; 27073 long savet; 27074 void (*savec)(); 27075 /* 27076 * The following is defined in machdep.c and is used in determining if 27077 * the scsi transport system will do polled I/O instead of interrupt 27078 * I/O when called from xx_dump(). 27079 */ 27080 extern int do_polled_io; 27081 27082 /* 27083 * save old flags in pkt, to restore at end 27084 */ 27085 savef = pkt->pkt_flags; 27086 savec = pkt->pkt_comp; 27087 savet = pkt->pkt_time; 27088 27089 pkt->pkt_flags |= FLAG_NOINTR; 27090 27091 /* 27092 * XXX there is nothing in the SCSA spec that states that we should not 27093 * do a callback for polled cmds; however, removing this will break sd 27094 * and probably other target drivers 27095 */ 27096 pkt->pkt_comp = NULL; 27097 27098 /* 27099 * we don't like a polled command without timeout. 27100 * 60 seconds seems long enough. 27101 */ 27102 if (pkt->pkt_time == 0) { 27103 pkt->pkt_time = SCSI_POLL_TIMEOUT; 27104 } 27105 27106 /* 27107 * Send polled cmd. 27108 * 27109 * We do some error recovery for various errors. Tran_busy, 27110 * queue full, and non-dispatched commands are retried every 10 msec. 27111 * as they are typically transient failures. Busy status and Not 27112 * Ready are retried every second as this status takes a while to 27113 * change. Unit attention is retried for pkt_time (60) times 27114 * with no delay. 27115 */ 27116 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 27117 27118 for (busy_count = 0; busy_count < timeout; busy_count++) { 27119 int rc; 27120 int poll_delay; 27121 27122 /* 27123 * Initialize pkt status variables. 27124 */ 27125 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 27126 27127 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 27128 if (rc != TRAN_BUSY) { 27129 /* Transport failed - give up. */ 27130 break; 27131 } else { 27132 /* Transport busy - try again. */ 27133 poll_delay = 1 * SD_CSEC; /* 10 msec */ 27134 } 27135 } else { 27136 /* 27137 * Transport accepted - check pkt status. 27138 */ 27139 rc = (*pkt->pkt_scbp) & STATUS_MASK; 27140 if (pkt->pkt_reason == CMD_CMPLT && 27141 rc == STATUS_CHECK && 27142 pkt->pkt_state & STATE_ARQ_DONE) { 27143 struct scsi_arq_status *arqstat = 27144 (struct scsi_arq_status *)(pkt->pkt_scbp); 27145 27146 sensep = (uint8_t *)&arqstat->sts_sensedata; 27147 } else { 27148 sensep = NULL; 27149 } 27150 27151 if ((pkt->pkt_reason == CMD_CMPLT) && 27152 (rc == STATUS_GOOD)) { 27153 /* No error - we're done */ 27154 rval = SD_SUCCESS; 27155 break; 27156 27157 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 27158 /* Lost connection - give up */ 27159 break; 27160 27161 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 27162 (pkt->pkt_state == 0)) { 27163 /* Pkt not dispatched - try again. */ 27164 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27165 27166 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27167 (rc == STATUS_QFULL)) { 27168 /* Queue full - try again. */ 27169 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27170 27171 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27172 (rc == STATUS_BUSY)) { 27173 /* Busy - try again. */ 27174 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27175 busy_count += (SD_SEC_TO_CSEC - 1); 27176 27177 } else if ((sensep != NULL) && 27178 (scsi_sense_key(sensep) == 27179 KEY_UNIT_ATTENTION)) { 27180 /* Unit Attention - try again */ 27181 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 27182 continue; 27183 27184 } else if ((sensep != NULL) && 27185 (scsi_sense_key(sensep) == KEY_NOT_READY) && 27186 (scsi_sense_asc(sensep) == 0x04) && 27187 (scsi_sense_ascq(sensep) == 0x01)) { 27188 /* Not ready -> ready - try again. */ 27189 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27190 busy_count += (SD_SEC_TO_CSEC - 1); 27191 27192 } else { 27193 /* BAD status - give up. */ 27194 break; 27195 } 27196 } 27197 27198 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 27199 !do_polled_io) { 27200 delay(drv_usectohz(poll_delay)); 27201 } else { 27202 /* we busy wait during cpr_dump or interrupt threads */ 27203 drv_usecwait(poll_delay); 27204 } 27205 } 27206 27207 pkt->pkt_flags = savef; 27208 pkt->pkt_comp = savec; 27209 pkt->pkt_time = savet; 27210 return (rval); 27211 } 27212 27213 27214 /* 27215 * Function: sd_persistent_reservation_in_read_keys 27216 * 27217 * Description: This routine is the driver entry point for handling CD-ROM 27218 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 27219 * by sending the SCSI-3 PRIN commands to the device. 27220 * Processes the read keys command response by copying the 27221 * reservation key information into the user provided buffer. 27222 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 27223 * 27224 * Arguments: un - Pointer to soft state struct for the target. 27225 * usrp - user provided pointer to multihost Persistent In Read 27226 * Keys structure (mhioc_inkeys_t) 27227 * flag - this argument is a pass through to ddi_copyxxx() 27228 * directly from the mode argument of ioctl(). 27229 * 27230 * Return Code: 0 - Success 27231 * EACCES 27232 * ENOTSUP 27233 * errno return code from sd_send_scsi_cmd() 27234 * 27235 * Context: Can sleep. Does not return until command is completed. 27236 */ 27237 27238 static int 27239 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 27240 mhioc_inkeys_t *usrp, int flag) 27241 { 27242 #ifdef _MULTI_DATAMODEL 27243 struct mhioc_key_list32 li32; 27244 #endif 27245 sd_prin_readkeys_t *in; 27246 mhioc_inkeys_t *ptr; 27247 mhioc_key_list_t li; 27248 uchar_t *data_bufp; 27249 int data_len; 27250 int rval; 27251 size_t copysz; 27252 27253 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 27254 return (EINVAL); 27255 } 27256 bzero(&li, sizeof (mhioc_key_list_t)); 27257 27258 /* 27259 * Get the listsize from user 27260 */ 27261 #ifdef _MULTI_DATAMODEL 27262 27263 switch (ddi_model_convert_from(flag & FMODELS)) { 27264 case DDI_MODEL_ILP32: 27265 copysz = sizeof (struct mhioc_key_list32); 27266 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 27267 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27268 "sd_persistent_reservation_in_read_keys: " 27269 "failed ddi_copyin: mhioc_key_list32_t\n"); 27270 rval = EFAULT; 27271 goto done; 27272 } 27273 li.listsize = li32.listsize; 27274 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 27275 break; 27276 27277 case DDI_MODEL_NONE: 27278 copysz = sizeof (mhioc_key_list_t); 27279 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27280 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27281 "sd_persistent_reservation_in_read_keys: " 27282 "failed ddi_copyin: mhioc_key_list_t\n"); 27283 rval = EFAULT; 27284 goto done; 27285 } 27286 break; 27287 } 27288 27289 #else /* ! _MULTI_DATAMODEL */ 27290 copysz = sizeof (mhioc_key_list_t); 27291 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27292 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27293 "sd_persistent_reservation_in_read_keys: " 27294 "failed ddi_copyin: mhioc_key_list_t\n"); 27295 rval = EFAULT; 27296 goto done; 27297 } 27298 #endif 27299 27300 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 27301 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 27302 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27303 27304 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 27305 data_len, data_bufp)) != 0) { 27306 goto done; 27307 } 27308 in = (sd_prin_readkeys_t *)data_bufp; 27309 ptr->generation = BE_32(in->generation); 27310 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 27311 27312 /* 27313 * Return the min(listsize, listlen) keys 27314 */ 27315 #ifdef _MULTI_DATAMODEL 27316 27317 switch (ddi_model_convert_from(flag & FMODELS)) { 27318 case DDI_MODEL_ILP32: 27319 li32.listlen = li.listlen; 27320 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 27321 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27322 "sd_persistent_reservation_in_read_keys: " 27323 "failed ddi_copyout: mhioc_key_list32_t\n"); 27324 rval = EFAULT; 27325 goto done; 27326 } 27327 break; 27328 27329 case DDI_MODEL_NONE: 27330 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27331 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27332 "sd_persistent_reservation_in_read_keys: " 27333 "failed ddi_copyout: mhioc_key_list_t\n"); 27334 rval = EFAULT; 27335 goto done; 27336 } 27337 break; 27338 } 27339 27340 #else /* ! _MULTI_DATAMODEL */ 27341 27342 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27343 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27344 "sd_persistent_reservation_in_read_keys: " 27345 "failed ddi_copyout: mhioc_key_list_t\n"); 27346 rval = EFAULT; 27347 goto done; 27348 } 27349 27350 #endif /* _MULTI_DATAMODEL */ 27351 27352 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 27353 li.listsize * MHIOC_RESV_KEY_SIZE); 27354 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 27355 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27356 "sd_persistent_reservation_in_read_keys: " 27357 "failed ddi_copyout: keylist\n"); 27358 rval = EFAULT; 27359 } 27360 done: 27361 kmem_free(data_bufp, data_len); 27362 return (rval); 27363 } 27364 27365 27366 /* 27367 * Function: sd_persistent_reservation_in_read_resv 27368 * 27369 * Description: This routine is the driver entry point for handling CD-ROM 27370 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 27371 * by sending the SCSI-3 PRIN commands to the device. 27372 * Process the read persistent reservations command response by 27373 * copying the reservation information into the user provided 27374 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 27375 * 27376 * Arguments: un - Pointer to soft state struct for the target. 27377 * usrp - user provided pointer to multihost Persistent In Read 27378 * Keys structure (mhioc_inkeys_t) 27379 * flag - this argument is a pass through to ddi_copyxxx() 27380 * directly from the mode argument of ioctl(). 27381 * 27382 * Return Code: 0 - Success 27383 * EACCES 27384 * ENOTSUP 27385 * errno return code from sd_send_scsi_cmd() 27386 * 27387 * Context: Can sleep. Does not return until command is completed. 27388 */ 27389 27390 static int 27391 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 27392 mhioc_inresvs_t *usrp, int flag) 27393 { 27394 #ifdef _MULTI_DATAMODEL 27395 struct mhioc_resv_desc_list32 resvlist32; 27396 #endif 27397 sd_prin_readresv_t *in; 27398 mhioc_inresvs_t *ptr; 27399 sd_readresv_desc_t *readresv_ptr; 27400 mhioc_resv_desc_list_t resvlist; 27401 mhioc_resv_desc_t resvdesc; 27402 uchar_t *data_bufp; 27403 int data_len; 27404 int rval; 27405 int i; 27406 size_t copysz; 27407 mhioc_resv_desc_t *bufp; 27408 27409 if ((ptr = usrp) == NULL) { 27410 return (EINVAL); 27411 } 27412 27413 /* 27414 * Get the listsize from user 27415 */ 27416 #ifdef _MULTI_DATAMODEL 27417 switch (ddi_model_convert_from(flag & FMODELS)) { 27418 case DDI_MODEL_ILP32: 27419 copysz = sizeof (struct mhioc_resv_desc_list32); 27420 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 27421 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27422 "sd_persistent_reservation_in_read_resv: " 27423 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27424 rval = EFAULT; 27425 goto done; 27426 } 27427 resvlist.listsize = resvlist32.listsize; 27428 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 27429 break; 27430 27431 case DDI_MODEL_NONE: 27432 copysz = sizeof (mhioc_resv_desc_list_t); 27433 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27434 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27435 "sd_persistent_reservation_in_read_resv: " 27436 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27437 rval = EFAULT; 27438 goto done; 27439 } 27440 break; 27441 } 27442 #else /* ! _MULTI_DATAMODEL */ 27443 copysz = sizeof (mhioc_resv_desc_list_t); 27444 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27445 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27446 "sd_persistent_reservation_in_read_resv: " 27447 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27448 rval = EFAULT; 27449 goto done; 27450 } 27451 #endif /* ! _MULTI_DATAMODEL */ 27452 27453 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 27454 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 27455 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27456 27457 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 27458 data_len, data_bufp)) != 0) { 27459 goto done; 27460 } 27461 in = (sd_prin_readresv_t *)data_bufp; 27462 ptr->generation = BE_32(in->generation); 27463 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 27464 27465 /* 27466 * Return the min(listsize, listlen( keys 27467 */ 27468 #ifdef _MULTI_DATAMODEL 27469 27470 switch (ddi_model_convert_from(flag & FMODELS)) { 27471 case DDI_MODEL_ILP32: 27472 resvlist32.listlen = resvlist.listlen; 27473 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27474 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27475 "sd_persistent_reservation_in_read_resv: " 27476 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27477 rval = EFAULT; 27478 goto done; 27479 } 27480 break; 27481 27482 case DDI_MODEL_NONE: 27483 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27484 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27485 "sd_persistent_reservation_in_read_resv: " 27486 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27487 rval = EFAULT; 27488 goto done; 27489 } 27490 break; 27491 } 27492 27493 #else /* ! _MULTI_DATAMODEL */ 27494 27495 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27496 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27497 "sd_persistent_reservation_in_read_resv: " 27498 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27499 rval = EFAULT; 27500 goto done; 27501 } 27502 27503 #endif /* ! _MULTI_DATAMODEL */ 27504 27505 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27506 bufp = resvlist.list; 27507 copysz = sizeof (mhioc_resv_desc_t); 27508 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27509 i++, readresv_ptr++, bufp++) { 27510 27511 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27512 MHIOC_RESV_KEY_SIZE); 27513 resvdesc.type = readresv_ptr->type; 27514 resvdesc.scope = readresv_ptr->scope; 27515 resvdesc.scope_specific_addr = 27516 BE_32(readresv_ptr->scope_specific_addr); 27517 27518 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27519 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27520 "sd_persistent_reservation_in_read_resv: " 27521 "failed ddi_copyout: resvlist\n"); 27522 rval = EFAULT; 27523 goto done; 27524 } 27525 } 27526 done: 27527 kmem_free(data_bufp, data_len); 27528 return (rval); 27529 } 27530 27531 27532 /* 27533 * Function: sr_change_blkmode() 27534 * 27535 * Description: This routine is the driver entry point for handling CD-ROM 27536 * block mode ioctl requests. Support for returning and changing 27537 * the current block size in use by the device is implemented. The 27538 * LBA size is changed via a MODE SELECT Block Descriptor. 27539 * 27540 * This routine issues a mode sense with an allocation length of 27541 * 12 bytes for the mode page header and a single block descriptor. 27542 * 27543 * Arguments: dev - the device 'dev_t' 27544 * cmd - the request type; one of CDROMGBLKMODE (get) or 27545 * CDROMSBLKMODE (set) 27546 * data - current block size or requested block size 27547 * flag - this argument is a pass through to ddi_copyxxx() directly 27548 * from the mode argument of ioctl(). 27549 * 27550 * Return Code: the code returned by sd_send_scsi_cmd() 27551 * EINVAL if invalid arguments are provided 27552 * EFAULT if ddi_copyxxx() fails 27553 * ENXIO if fail ddi_get_soft_state 27554 * EIO if invalid mode sense block descriptor length 27555 * 27556 */ 27557 27558 static int 27559 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27560 { 27561 struct sd_lun *un = NULL; 27562 struct mode_header *sense_mhp, *select_mhp; 27563 struct block_descriptor *sense_desc, *select_desc; 27564 int current_bsize; 27565 int rval = EINVAL; 27566 uchar_t *sense = NULL; 27567 uchar_t *select = NULL; 27568 27569 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27570 27571 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27572 return (ENXIO); 27573 } 27574 27575 /* 27576 * The block length is changed via the Mode Select block descriptor, the 27577 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27578 * required as part of this routine. Therefore the mode sense allocation 27579 * length is specified to be the length of a mode page header and a 27580 * block descriptor. 27581 */ 27582 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27583 27584 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27585 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27586 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27587 "sr_change_blkmode: Mode Sense Failed\n"); 27588 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27589 return (rval); 27590 } 27591 27592 /* Check the block descriptor len to handle only 1 block descriptor */ 27593 sense_mhp = (struct mode_header *)sense; 27594 if ((sense_mhp->bdesc_length == 0) || 27595 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27596 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27597 "sr_change_blkmode: Mode Sense returned invalid block" 27598 " descriptor length\n"); 27599 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27600 return (EIO); 27601 } 27602 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27603 current_bsize = ((sense_desc->blksize_hi << 16) | 27604 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27605 27606 /* Process command */ 27607 switch (cmd) { 27608 case CDROMGBLKMODE: 27609 /* Return the block size obtained during the mode sense */ 27610 if (ddi_copyout(¤t_bsize, (void *)data, 27611 sizeof (int), flag) != 0) 27612 rval = EFAULT; 27613 break; 27614 case CDROMSBLKMODE: 27615 /* Validate the requested block size */ 27616 switch (data) { 27617 case CDROM_BLK_512: 27618 case CDROM_BLK_1024: 27619 case CDROM_BLK_2048: 27620 case CDROM_BLK_2056: 27621 case CDROM_BLK_2336: 27622 case CDROM_BLK_2340: 27623 case CDROM_BLK_2352: 27624 case CDROM_BLK_2368: 27625 case CDROM_BLK_2448: 27626 case CDROM_BLK_2646: 27627 case CDROM_BLK_2647: 27628 break; 27629 default: 27630 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27631 "sr_change_blkmode: " 27632 "Block Size '%ld' Not Supported\n", data); 27633 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27634 return (EINVAL); 27635 } 27636 27637 /* 27638 * The current block size matches the requested block size so 27639 * there is no need to send the mode select to change the size 27640 */ 27641 if (current_bsize == data) { 27642 break; 27643 } 27644 27645 /* Build the select data for the requested block size */ 27646 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27647 select_mhp = (struct mode_header *)select; 27648 select_desc = 27649 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27650 /* 27651 * The LBA size is changed via the block descriptor, so the 27652 * descriptor is built according to the user data 27653 */ 27654 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27655 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27656 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27657 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27658 27659 /* Send the mode select for the requested block size */ 27660 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27661 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27662 SD_PATH_STANDARD)) != 0) { 27663 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27664 "sr_change_blkmode: Mode Select Failed\n"); 27665 /* 27666 * The mode select failed for the requested block size, 27667 * so reset the data for the original block size and 27668 * send it to the target. The error is indicated by the 27669 * return value for the failed mode select. 27670 */ 27671 select_desc->blksize_hi = sense_desc->blksize_hi; 27672 select_desc->blksize_mid = sense_desc->blksize_mid; 27673 select_desc->blksize_lo = sense_desc->blksize_lo; 27674 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27675 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27676 SD_PATH_STANDARD); 27677 } else { 27678 ASSERT(!mutex_owned(SD_MUTEX(un))); 27679 mutex_enter(SD_MUTEX(un)); 27680 sd_update_block_info(un, (uint32_t)data, 0); 27681 27682 mutex_exit(SD_MUTEX(un)); 27683 } 27684 break; 27685 default: 27686 /* should not reach here, but check anyway */ 27687 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27688 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27689 rval = EINVAL; 27690 break; 27691 } 27692 27693 if (select) { 27694 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27695 } 27696 if (sense) { 27697 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27698 } 27699 return (rval); 27700 } 27701 27702 27703 /* 27704 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27705 * implement driver support for getting and setting the CD speed. The command 27706 * set used will be based on the device type. If the device has not been 27707 * identified as MMC the Toshiba vendor specific mode page will be used. If 27708 * the device is MMC but does not support the Real Time Streaming feature 27709 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27710 * be used to read the speed. 27711 */ 27712 27713 /* 27714 * Function: sr_change_speed() 27715 * 27716 * Description: This routine is the driver entry point for handling CD-ROM 27717 * drive speed ioctl requests for devices supporting the Toshiba 27718 * vendor specific drive speed mode page. Support for returning 27719 * and changing the current drive speed in use by the device is 27720 * implemented. 27721 * 27722 * Arguments: dev - the device 'dev_t' 27723 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27724 * CDROMSDRVSPEED (set) 27725 * data - current drive speed or requested drive speed 27726 * flag - this argument is a pass through to ddi_copyxxx() directly 27727 * from the mode argument of ioctl(). 27728 * 27729 * Return Code: the code returned by sd_send_scsi_cmd() 27730 * EINVAL if invalid arguments are provided 27731 * EFAULT if ddi_copyxxx() fails 27732 * ENXIO if fail ddi_get_soft_state 27733 * EIO if invalid mode sense block descriptor length 27734 */ 27735 27736 static int 27737 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27738 { 27739 struct sd_lun *un = NULL; 27740 struct mode_header *sense_mhp, *select_mhp; 27741 struct mode_speed *sense_page, *select_page; 27742 int current_speed; 27743 int rval = EINVAL; 27744 int bd_len; 27745 uchar_t *sense = NULL; 27746 uchar_t *select = NULL; 27747 27748 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27749 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27750 return (ENXIO); 27751 } 27752 27753 /* 27754 * Note: The drive speed is being modified here according to a Toshiba 27755 * vendor specific mode page (0x31). 27756 */ 27757 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27758 27759 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27760 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27761 SD_PATH_STANDARD)) != 0) { 27762 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27763 "sr_change_speed: Mode Sense Failed\n"); 27764 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27765 return (rval); 27766 } 27767 sense_mhp = (struct mode_header *)sense; 27768 27769 /* Check the block descriptor len to handle only 1 block descriptor */ 27770 bd_len = sense_mhp->bdesc_length; 27771 if (bd_len > MODE_BLK_DESC_LENGTH) { 27772 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27773 "sr_change_speed: Mode Sense returned invalid block " 27774 "descriptor length\n"); 27775 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27776 return (EIO); 27777 } 27778 27779 sense_page = (struct mode_speed *) 27780 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27781 current_speed = sense_page->speed; 27782 27783 /* Process command */ 27784 switch (cmd) { 27785 case CDROMGDRVSPEED: 27786 /* Return the drive speed obtained during the mode sense */ 27787 if (current_speed == 0x2) { 27788 current_speed = CDROM_TWELVE_SPEED; 27789 } 27790 if (ddi_copyout(¤t_speed, (void *)data, 27791 sizeof (int), flag) != 0) { 27792 rval = EFAULT; 27793 } 27794 break; 27795 case CDROMSDRVSPEED: 27796 /* Validate the requested drive speed */ 27797 switch ((uchar_t)data) { 27798 case CDROM_TWELVE_SPEED: 27799 data = 0x2; 27800 /*FALLTHROUGH*/ 27801 case CDROM_NORMAL_SPEED: 27802 case CDROM_DOUBLE_SPEED: 27803 case CDROM_QUAD_SPEED: 27804 case CDROM_MAXIMUM_SPEED: 27805 break; 27806 default: 27807 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27808 "sr_change_speed: " 27809 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27810 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27811 return (EINVAL); 27812 } 27813 27814 /* 27815 * The current drive speed matches the requested drive speed so 27816 * there is no need to send the mode select to change the speed 27817 */ 27818 if (current_speed == data) { 27819 break; 27820 } 27821 27822 /* Build the select data for the requested drive speed */ 27823 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27824 select_mhp = (struct mode_header *)select; 27825 select_mhp->bdesc_length = 0; 27826 select_page = 27827 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27828 select_page = 27829 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27830 select_page->mode_page.code = CDROM_MODE_SPEED; 27831 select_page->mode_page.length = 2; 27832 select_page->speed = (uchar_t)data; 27833 27834 /* Send the mode select for the requested block size */ 27835 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27836 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27837 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27838 /* 27839 * The mode select failed for the requested drive speed, 27840 * so reset the data for the original drive speed and 27841 * send it to the target. The error is indicated by the 27842 * return value for the failed mode select. 27843 */ 27844 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27845 "sr_drive_speed: Mode Select Failed\n"); 27846 select_page->speed = sense_page->speed; 27847 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27848 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27849 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27850 } 27851 break; 27852 default: 27853 /* should not reach here, but check anyway */ 27854 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27855 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27856 rval = EINVAL; 27857 break; 27858 } 27859 27860 if (select) { 27861 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27862 } 27863 if (sense) { 27864 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27865 } 27866 27867 return (rval); 27868 } 27869 27870 27871 /* 27872 * Function: sr_atapi_change_speed() 27873 * 27874 * Description: This routine is the driver entry point for handling CD-ROM 27875 * drive speed ioctl requests for MMC devices that do not support 27876 * the Real Time Streaming feature (0x107). 27877 * 27878 * Note: This routine will use the SET SPEED command which may not 27879 * be supported by all devices. 27880 * 27881 * Arguments: dev- the device 'dev_t' 27882 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27883 * CDROMSDRVSPEED (set) 27884 * data- current drive speed or requested drive speed 27885 * flag- this argument is a pass through to ddi_copyxxx() directly 27886 * from the mode argument of ioctl(). 27887 * 27888 * Return Code: the code returned by sd_send_scsi_cmd() 27889 * EINVAL if invalid arguments are provided 27890 * EFAULT if ddi_copyxxx() fails 27891 * ENXIO if fail ddi_get_soft_state 27892 * EIO if invalid mode sense block descriptor length 27893 */ 27894 27895 static int 27896 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27897 { 27898 struct sd_lun *un; 27899 struct uscsi_cmd *com = NULL; 27900 struct mode_header_grp2 *sense_mhp; 27901 uchar_t *sense_page; 27902 uchar_t *sense = NULL; 27903 char cdb[CDB_GROUP5]; 27904 int bd_len; 27905 int current_speed = 0; 27906 int max_speed = 0; 27907 int rval; 27908 27909 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27910 27911 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27912 return (ENXIO); 27913 } 27914 27915 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27916 27917 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27918 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27919 SD_PATH_STANDARD)) != 0) { 27920 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27921 "sr_atapi_change_speed: Mode Sense Failed\n"); 27922 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27923 return (rval); 27924 } 27925 27926 /* Check the block descriptor len to handle only 1 block descriptor */ 27927 sense_mhp = (struct mode_header_grp2 *)sense; 27928 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27929 if (bd_len > MODE_BLK_DESC_LENGTH) { 27930 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27931 "sr_atapi_change_speed: Mode Sense returned invalid " 27932 "block descriptor length\n"); 27933 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27934 return (EIO); 27935 } 27936 27937 /* Calculate the current and maximum drive speeds */ 27938 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27939 current_speed = (sense_page[14] << 8) | sense_page[15]; 27940 max_speed = (sense_page[8] << 8) | sense_page[9]; 27941 27942 /* Process the command */ 27943 switch (cmd) { 27944 case CDROMGDRVSPEED: 27945 current_speed /= SD_SPEED_1X; 27946 if (ddi_copyout(¤t_speed, (void *)data, 27947 sizeof (int), flag) != 0) 27948 rval = EFAULT; 27949 break; 27950 case CDROMSDRVSPEED: 27951 /* Convert the speed code to KB/sec */ 27952 switch ((uchar_t)data) { 27953 case CDROM_NORMAL_SPEED: 27954 current_speed = SD_SPEED_1X; 27955 break; 27956 case CDROM_DOUBLE_SPEED: 27957 current_speed = 2 * SD_SPEED_1X; 27958 break; 27959 case CDROM_QUAD_SPEED: 27960 current_speed = 4 * SD_SPEED_1X; 27961 break; 27962 case CDROM_TWELVE_SPEED: 27963 current_speed = 12 * SD_SPEED_1X; 27964 break; 27965 case CDROM_MAXIMUM_SPEED: 27966 current_speed = 0xffff; 27967 break; 27968 default: 27969 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27970 "sr_atapi_change_speed: invalid drive speed %d\n", 27971 (uchar_t)data); 27972 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27973 return (EINVAL); 27974 } 27975 27976 /* Check the request against the drive's max speed. */ 27977 if (current_speed != 0xffff) { 27978 if (current_speed > max_speed) { 27979 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27980 return (EINVAL); 27981 } 27982 } 27983 27984 /* 27985 * Build and send the SET SPEED command 27986 * 27987 * Note: The SET SPEED (0xBB) command used in this routine is 27988 * obsolete per the SCSI MMC spec but still supported in the 27989 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27990 * therefore the command is still implemented in this routine. 27991 */ 27992 bzero(cdb, sizeof (cdb)); 27993 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 27994 cdb[2] = (uchar_t)(current_speed >> 8); 27995 cdb[3] = (uchar_t)current_speed; 27996 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 27997 com->uscsi_cdb = (caddr_t)cdb; 27998 com->uscsi_cdblen = CDB_GROUP5; 27999 com->uscsi_bufaddr = NULL; 28000 com->uscsi_buflen = 0; 28001 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28002 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 28003 UIO_SYSSPACE, SD_PATH_STANDARD); 28004 break; 28005 default: 28006 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28007 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 28008 rval = EINVAL; 28009 } 28010 28011 if (sense) { 28012 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 28013 } 28014 if (com) { 28015 kmem_free(com, sizeof (*com)); 28016 } 28017 return (rval); 28018 } 28019 28020 28021 /* 28022 * Function: sr_pause_resume() 28023 * 28024 * Description: This routine is the driver entry point for handling CD-ROM 28025 * pause/resume ioctl requests. This only affects the audio play 28026 * operation. 28027 * 28028 * Arguments: dev - the device 'dev_t' 28029 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 28030 * for setting the resume bit of the cdb. 28031 * 28032 * Return Code: the code returned by sd_send_scsi_cmd() 28033 * EINVAL if invalid mode specified 28034 * 28035 */ 28036 28037 static int 28038 sr_pause_resume(dev_t dev, int cmd) 28039 { 28040 struct sd_lun *un; 28041 struct uscsi_cmd *com; 28042 char cdb[CDB_GROUP1]; 28043 int rval; 28044 28045 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28046 return (ENXIO); 28047 } 28048 28049 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28050 bzero(cdb, CDB_GROUP1); 28051 cdb[0] = SCMD_PAUSE_RESUME; 28052 switch (cmd) { 28053 case CDROMRESUME: 28054 cdb[8] = 1; 28055 break; 28056 case CDROMPAUSE: 28057 cdb[8] = 0; 28058 break; 28059 default: 28060 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 28061 " Command '%x' Not Supported\n", cmd); 28062 rval = EINVAL; 28063 goto done; 28064 } 28065 28066 com->uscsi_cdb = cdb; 28067 com->uscsi_cdblen = CDB_GROUP1; 28068 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28069 28070 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28071 UIO_SYSSPACE, SD_PATH_STANDARD); 28072 28073 done: 28074 kmem_free(com, sizeof (*com)); 28075 return (rval); 28076 } 28077 28078 28079 /* 28080 * Function: sr_play_msf() 28081 * 28082 * Description: This routine is the driver entry point for handling CD-ROM 28083 * ioctl requests to output the audio signals at the specified 28084 * starting address and continue the audio play until the specified 28085 * ending address (CDROMPLAYMSF) The address is in Minute Second 28086 * Frame (MSF) format. 28087 * 28088 * Arguments: dev - the device 'dev_t' 28089 * data - pointer to user provided audio msf structure, 28090 * specifying start/end addresses. 28091 * flag - this argument is a pass through to ddi_copyxxx() 28092 * directly from the mode argument of ioctl(). 28093 * 28094 * Return Code: the code returned by sd_send_scsi_cmd() 28095 * EFAULT if ddi_copyxxx() fails 28096 * ENXIO if fail ddi_get_soft_state 28097 * EINVAL if data pointer is NULL 28098 */ 28099 28100 static int 28101 sr_play_msf(dev_t dev, caddr_t data, int flag) 28102 { 28103 struct sd_lun *un; 28104 struct uscsi_cmd *com; 28105 struct cdrom_msf msf_struct; 28106 struct cdrom_msf *msf = &msf_struct; 28107 char cdb[CDB_GROUP1]; 28108 int rval; 28109 28110 if (data == NULL) { 28111 return (EINVAL); 28112 } 28113 28114 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28115 return (ENXIO); 28116 } 28117 28118 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 28119 return (EFAULT); 28120 } 28121 28122 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28123 bzero(cdb, CDB_GROUP1); 28124 cdb[0] = SCMD_PLAYAUDIO_MSF; 28125 if (un->un_f_cfg_playmsf_bcd == TRUE) { 28126 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 28127 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 28128 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 28129 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 28130 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 28131 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 28132 } else { 28133 cdb[3] = msf->cdmsf_min0; 28134 cdb[4] = msf->cdmsf_sec0; 28135 cdb[5] = msf->cdmsf_frame0; 28136 cdb[6] = msf->cdmsf_min1; 28137 cdb[7] = msf->cdmsf_sec1; 28138 cdb[8] = msf->cdmsf_frame1; 28139 } 28140 com->uscsi_cdb = cdb; 28141 com->uscsi_cdblen = CDB_GROUP1; 28142 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28143 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28144 UIO_SYSSPACE, SD_PATH_STANDARD); 28145 kmem_free(com, sizeof (*com)); 28146 return (rval); 28147 } 28148 28149 28150 /* 28151 * Function: sr_play_trkind() 28152 * 28153 * Description: This routine is the driver entry point for handling CD-ROM 28154 * ioctl requests to output the audio signals at the specified 28155 * starting address and continue the audio play until the specified 28156 * ending address (CDROMPLAYTRKIND). The address is in Track Index 28157 * format. 28158 * 28159 * Arguments: dev - the device 'dev_t' 28160 * data - pointer to user provided audio track/index structure, 28161 * specifying start/end addresses. 28162 * flag - this argument is a pass through to ddi_copyxxx() 28163 * directly from the mode argument of ioctl(). 28164 * 28165 * Return Code: the code returned by sd_send_scsi_cmd() 28166 * EFAULT if ddi_copyxxx() fails 28167 * ENXIO if fail ddi_get_soft_state 28168 * EINVAL if data pointer is NULL 28169 */ 28170 28171 static int 28172 sr_play_trkind(dev_t dev, caddr_t data, int flag) 28173 { 28174 struct cdrom_ti ti_struct; 28175 struct cdrom_ti *ti = &ti_struct; 28176 struct uscsi_cmd *com = NULL; 28177 char cdb[CDB_GROUP1]; 28178 int rval; 28179 28180 if (data == NULL) { 28181 return (EINVAL); 28182 } 28183 28184 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 28185 return (EFAULT); 28186 } 28187 28188 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28189 bzero(cdb, CDB_GROUP1); 28190 cdb[0] = SCMD_PLAYAUDIO_TI; 28191 cdb[4] = ti->cdti_trk0; 28192 cdb[5] = ti->cdti_ind0; 28193 cdb[7] = ti->cdti_trk1; 28194 cdb[8] = ti->cdti_ind1; 28195 com->uscsi_cdb = cdb; 28196 com->uscsi_cdblen = CDB_GROUP1; 28197 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28198 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28199 UIO_SYSSPACE, SD_PATH_STANDARD); 28200 kmem_free(com, sizeof (*com)); 28201 return (rval); 28202 } 28203 28204 28205 /* 28206 * Function: sr_read_all_subcodes() 28207 * 28208 * Description: This routine is the driver entry point for handling CD-ROM 28209 * ioctl requests to return raw subcode data while the target is 28210 * playing audio (CDROMSUBCODE). 28211 * 28212 * Arguments: dev - the device 'dev_t' 28213 * data - pointer to user provided cdrom subcode structure, 28214 * specifying the transfer length and address. 28215 * flag - this argument is a pass through to ddi_copyxxx() 28216 * directly from the mode argument of ioctl(). 28217 * 28218 * Return Code: the code returned by sd_send_scsi_cmd() 28219 * EFAULT if ddi_copyxxx() fails 28220 * ENXIO if fail ddi_get_soft_state 28221 * EINVAL if data pointer is NULL 28222 */ 28223 28224 static int 28225 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 28226 { 28227 struct sd_lun *un = NULL; 28228 struct uscsi_cmd *com = NULL; 28229 struct cdrom_subcode *subcode = NULL; 28230 int rval; 28231 size_t buflen; 28232 char cdb[CDB_GROUP5]; 28233 28234 #ifdef _MULTI_DATAMODEL 28235 /* To support ILP32 applications in an LP64 world */ 28236 struct cdrom_subcode32 cdrom_subcode32; 28237 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 28238 #endif 28239 if (data == NULL) { 28240 return (EINVAL); 28241 } 28242 28243 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28244 return (ENXIO); 28245 } 28246 28247 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 28248 28249 #ifdef _MULTI_DATAMODEL 28250 switch (ddi_model_convert_from(flag & FMODELS)) { 28251 case DDI_MODEL_ILP32: 28252 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 28253 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28254 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28255 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28256 return (EFAULT); 28257 } 28258 /* Convert the ILP32 uscsi data from the application to LP64 */ 28259 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 28260 break; 28261 case DDI_MODEL_NONE: 28262 if (ddi_copyin(data, subcode, 28263 sizeof (struct cdrom_subcode), flag)) { 28264 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28265 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28266 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28267 return (EFAULT); 28268 } 28269 break; 28270 } 28271 #else /* ! _MULTI_DATAMODEL */ 28272 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 28273 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28274 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28275 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28276 return (EFAULT); 28277 } 28278 #endif /* _MULTI_DATAMODEL */ 28279 28280 /* 28281 * Since MMC-2 expects max 3 bytes for length, check if the 28282 * length input is greater than 3 bytes 28283 */ 28284 if ((subcode->cdsc_length & 0xFF000000) != 0) { 28285 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28286 "sr_read_all_subcodes: " 28287 "cdrom transfer length too large: %d (limit %d)\n", 28288 subcode->cdsc_length, 0xFFFFFF); 28289 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28290 return (EINVAL); 28291 } 28292 28293 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 28294 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28295 bzero(cdb, CDB_GROUP5); 28296 28297 if (un->un_f_mmc_cap == TRUE) { 28298 cdb[0] = (char)SCMD_READ_CD; 28299 cdb[2] = (char)0xff; 28300 cdb[3] = (char)0xff; 28301 cdb[4] = (char)0xff; 28302 cdb[5] = (char)0xff; 28303 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28304 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28305 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 28306 cdb[10] = 1; 28307 } else { 28308 /* 28309 * Note: A vendor specific command (0xDF) is being used her to 28310 * request a read of all subcodes. 28311 */ 28312 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 28313 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 28314 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28315 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28316 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 28317 } 28318 com->uscsi_cdb = cdb; 28319 com->uscsi_cdblen = CDB_GROUP5; 28320 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 28321 com->uscsi_buflen = buflen; 28322 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28323 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28324 UIO_SYSSPACE, SD_PATH_STANDARD); 28325 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28326 kmem_free(com, sizeof (*com)); 28327 return (rval); 28328 } 28329 28330 28331 /* 28332 * Function: sr_read_subchannel() 28333 * 28334 * Description: This routine is the driver entry point for handling CD-ROM 28335 * ioctl requests to return the Q sub-channel data of the CD 28336 * current position block. (CDROMSUBCHNL) The data includes the 28337 * track number, index number, absolute CD-ROM address (LBA or MSF 28338 * format per the user) , track relative CD-ROM address (LBA or MSF 28339 * format per the user), control data and audio status. 28340 * 28341 * Arguments: dev - the device 'dev_t' 28342 * data - pointer to user provided cdrom sub-channel structure 28343 * flag - this argument is a pass through to ddi_copyxxx() 28344 * directly from the mode argument of ioctl(). 28345 * 28346 * Return Code: the code returned by sd_send_scsi_cmd() 28347 * EFAULT if ddi_copyxxx() fails 28348 * ENXIO if fail ddi_get_soft_state 28349 * EINVAL if data pointer is NULL 28350 */ 28351 28352 static int 28353 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 28354 { 28355 struct sd_lun *un; 28356 struct uscsi_cmd *com; 28357 struct cdrom_subchnl subchanel; 28358 struct cdrom_subchnl *subchnl = &subchanel; 28359 char cdb[CDB_GROUP1]; 28360 caddr_t buffer; 28361 int rval; 28362 28363 if (data == NULL) { 28364 return (EINVAL); 28365 } 28366 28367 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28368 (un->un_state == SD_STATE_OFFLINE)) { 28369 return (ENXIO); 28370 } 28371 28372 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 28373 return (EFAULT); 28374 } 28375 28376 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 28377 bzero(cdb, CDB_GROUP1); 28378 cdb[0] = SCMD_READ_SUBCHANNEL; 28379 /* Set the MSF bit based on the user requested address format */ 28380 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 28381 /* 28382 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 28383 * returned 28384 */ 28385 cdb[2] = 0x40; 28386 /* 28387 * Set byte 3 to specify the return data format. A value of 0x01 28388 * indicates that the CD-ROM current position should be returned. 28389 */ 28390 cdb[3] = 0x01; 28391 cdb[8] = 0x10; 28392 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28393 com->uscsi_cdb = cdb; 28394 com->uscsi_cdblen = CDB_GROUP1; 28395 com->uscsi_bufaddr = buffer; 28396 com->uscsi_buflen = 16; 28397 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28398 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28399 UIO_SYSSPACE, SD_PATH_STANDARD); 28400 if (rval != 0) { 28401 kmem_free(buffer, 16); 28402 kmem_free(com, sizeof (*com)); 28403 return (rval); 28404 } 28405 28406 /* Process the returned Q sub-channel data */ 28407 subchnl->cdsc_audiostatus = buffer[1]; 28408 subchnl->cdsc_adr = (buffer[5] & 0xF0); 28409 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 28410 subchnl->cdsc_trk = buffer[6]; 28411 subchnl->cdsc_ind = buffer[7]; 28412 if (subchnl->cdsc_format & CDROM_LBA) { 28413 subchnl->cdsc_absaddr.lba = 28414 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28415 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28416 subchnl->cdsc_reladdr.lba = 28417 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 28418 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 28419 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 28420 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 28421 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 28422 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 28423 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 28424 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 28425 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 28426 } else { 28427 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 28428 subchnl->cdsc_absaddr.msf.second = buffer[10]; 28429 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 28430 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 28431 subchnl->cdsc_reladdr.msf.second = buffer[14]; 28432 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 28433 } 28434 kmem_free(buffer, 16); 28435 kmem_free(com, sizeof (*com)); 28436 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 28437 != 0) { 28438 return (EFAULT); 28439 } 28440 return (rval); 28441 } 28442 28443 28444 /* 28445 * Function: sr_read_tocentry() 28446 * 28447 * Description: This routine is the driver entry point for handling CD-ROM 28448 * ioctl requests to read from the Table of Contents (TOC) 28449 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 28450 * fields, the starting address (LBA or MSF format per the user) 28451 * and the data mode if the user specified track is a data track. 28452 * 28453 * Note: The READ HEADER (0x44) command used in this routine is 28454 * obsolete per the SCSI MMC spec but still supported in the 28455 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28456 * therefore the command is still implemented in this routine. 28457 * 28458 * Arguments: dev - the device 'dev_t' 28459 * data - pointer to user provided toc entry structure, 28460 * specifying the track # and the address format 28461 * (LBA or MSF). 28462 * flag - this argument is a pass through to ddi_copyxxx() 28463 * directly from the mode argument of ioctl(). 28464 * 28465 * Return Code: the code returned by sd_send_scsi_cmd() 28466 * EFAULT if ddi_copyxxx() fails 28467 * ENXIO if fail ddi_get_soft_state 28468 * EINVAL if data pointer is NULL 28469 */ 28470 28471 static int 28472 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28473 { 28474 struct sd_lun *un = NULL; 28475 struct uscsi_cmd *com; 28476 struct cdrom_tocentry toc_entry; 28477 struct cdrom_tocentry *entry = &toc_entry; 28478 caddr_t buffer; 28479 int rval; 28480 char cdb[CDB_GROUP1]; 28481 28482 if (data == NULL) { 28483 return (EINVAL); 28484 } 28485 28486 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28487 (un->un_state == SD_STATE_OFFLINE)) { 28488 return (ENXIO); 28489 } 28490 28491 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28492 return (EFAULT); 28493 } 28494 28495 /* Validate the requested track and address format */ 28496 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28497 return (EINVAL); 28498 } 28499 28500 if (entry->cdte_track == 0) { 28501 return (EINVAL); 28502 } 28503 28504 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28505 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28506 bzero(cdb, CDB_GROUP1); 28507 28508 cdb[0] = SCMD_READ_TOC; 28509 /* Set the MSF bit based on the user requested address format */ 28510 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28511 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28512 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28513 } else { 28514 cdb[6] = entry->cdte_track; 28515 } 28516 28517 /* 28518 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28519 * (4 byte TOC response header + 8 byte track descriptor) 28520 */ 28521 cdb[8] = 12; 28522 com->uscsi_cdb = cdb; 28523 com->uscsi_cdblen = CDB_GROUP1; 28524 com->uscsi_bufaddr = buffer; 28525 com->uscsi_buflen = 0x0C; 28526 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28527 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28528 UIO_SYSSPACE, SD_PATH_STANDARD); 28529 if (rval != 0) { 28530 kmem_free(buffer, 12); 28531 kmem_free(com, sizeof (*com)); 28532 return (rval); 28533 } 28534 28535 /* Process the toc entry */ 28536 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28537 entry->cdte_ctrl = (buffer[5] & 0x0F); 28538 if (entry->cdte_format & CDROM_LBA) { 28539 entry->cdte_addr.lba = 28540 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28541 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28542 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28543 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28544 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28545 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28546 /* 28547 * Send a READ TOC command using the LBA address format to get 28548 * the LBA for the track requested so it can be used in the 28549 * READ HEADER request 28550 * 28551 * Note: The MSF bit of the READ HEADER command specifies the 28552 * output format. The block address specified in that command 28553 * must be in LBA format. 28554 */ 28555 cdb[1] = 0; 28556 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28557 UIO_SYSSPACE, SD_PATH_STANDARD); 28558 if (rval != 0) { 28559 kmem_free(buffer, 12); 28560 kmem_free(com, sizeof (*com)); 28561 return (rval); 28562 } 28563 } else { 28564 entry->cdte_addr.msf.minute = buffer[9]; 28565 entry->cdte_addr.msf.second = buffer[10]; 28566 entry->cdte_addr.msf.frame = buffer[11]; 28567 /* 28568 * Send a READ TOC command using the LBA address format to get 28569 * the LBA for the track requested so it can be used in the 28570 * READ HEADER request 28571 * 28572 * Note: The MSF bit of the READ HEADER command specifies the 28573 * output format. The block address specified in that command 28574 * must be in LBA format. 28575 */ 28576 cdb[1] = 0; 28577 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28578 UIO_SYSSPACE, SD_PATH_STANDARD); 28579 if (rval != 0) { 28580 kmem_free(buffer, 12); 28581 kmem_free(com, sizeof (*com)); 28582 return (rval); 28583 } 28584 } 28585 28586 /* 28587 * Build and send the READ HEADER command to determine the data mode of 28588 * the user specified track. 28589 */ 28590 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28591 (entry->cdte_track != CDROM_LEADOUT)) { 28592 bzero(cdb, CDB_GROUP1); 28593 cdb[0] = SCMD_READ_HEADER; 28594 cdb[2] = buffer[8]; 28595 cdb[3] = buffer[9]; 28596 cdb[4] = buffer[10]; 28597 cdb[5] = buffer[11]; 28598 cdb[8] = 0x08; 28599 com->uscsi_buflen = 0x08; 28600 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28601 UIO_SYSSPACE, SD_PATH_STANDARD); 28602 if (rval == 0) { 28603 entry->cdte_datamode = buffer[0]; 28604 } else { 28605 /* 28606 * READ HEADER command failed, since this is 28607 * obsoleted in one spec, its better to return 28608 * -1 for an invlid track so that we can still 28609 * recieve the rest of the TOC data. 28610 */ 28611 entry->cdte_datamode = (uchar_t)-1; 28612 } 28613 } else { 28614 entry->cdte_datamode = (uchar_t)-1; 28615 } 28616 28617 kmem_free(buffer, 12); 28618 kmem_free(com, sizeof (*com)); 28619 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28620 return (EFAULT); 28621 28622 return (rval); 28623 } 28624 28625 28626 /* 28627 * Function: sr_read_tochdr() 28628 * 28629 * Description: This routine is the driver entry point for handling CD-ROM 28630 * ioctl requests to read the Table of Contents (TOC) header 28631 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28632 * and ending track numbers 28633 * 28634 * Arguments: dev - the device 'dev_t' 28635 * data - pointer to user provided toc header structure, 28636 * specifying the starting and ending track numbers. 28637 * flag - this argument is a pass through to ddi_copyxxx() 28638 * directly from the mode argument of ioctl(). 28639 * 28640 * Return Code: the code returned by sd_send_scsi_cmd() 28641 * EFAULT if ddi_copyxxx() fails 28642 * ENXIO if fail ddi_get_soft_state 28643 * EINVAL if data pointer is NULL 28644 */ 28645 28646 static int 28647 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28648 { 28649 struct sd_lun *un; 28650 struct uscsi_cmd *com; 28651 struct cdrom_tochdr toc_header; 28652 struct cdrom_tochdr *hdr = &toc_header; 28653 char cdb[CDB_GROUP1]; 28654 int rval; 28655 caddr_t buffer; 28656 28657 if (data == NULL) { 28658 return (EINVAL); 28659 } 28660 28661 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28662 (un->un_state == SD_STATE_OFFLINE)) { 28663 return (ENXIO); 28664 } 28665 28666 buffer = kmem_zalloc(4, KM_SLEEP); 28667 bzero(cdb, CDB_GROUP1); 28668 cdb[0] = SCMD_READ_TOC; 28669 /* 28670 * Specifying a track number of 0x00 in the READ TOC command indicates 28671 * that the TOC header should be returned 28672 */ 28673 cdb[6] = 0x00; 28674 /* 28675 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28676 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28677 */ 28678 cdb[8] = 0x04; 28679 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28680 com->uscsi_cdb = cdb; 28681 com->uscsi_cdblen = CDB_GROUP1; 28682 com->uscsi_bufaddr = buffer; 28683 com->uscsi_buflen = 0x04; 28684 com->uscsi_timeout = 300; 28685 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28686 28687 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28688 UIO_SYSSPACE, SD_PATH_STANDARD); 28689 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28690 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28691 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28692 } else { 28693 hdr->cdth_trk0 = buffer[2]; 28694 hdr->cdth_trk1 = buffer[3]; 28695 } 28696 kmem_free(buffer, 4); 28697 kmem_free(com, sizeof (*com)); 28698 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28699 return (EFAULT); 28700 } 28701 return (rval); 28702 } 28703 28704 28705 /* 28706 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28707 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28708 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28709 * digital audio and extended architecture digital audio. These modes are 28710 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28711 * MMC specs. 28712 * 28713 * In addition to support for the various data formats these routines also 28714 * include support for devices that implement only the direct access READ 28715 * commands (0x08, 0x28), devices that implement the READ_CD commands 28716 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28717 * READ CDXA commands (0xD8, 0xDB) 28718 */ 28719 28720 /* 28721 * Function: sr_read_mode1() 28722 * 28723 * Description: This routine is the driver entry point for handling CD-ROM 28724 * ioctl read mode1 requests (CDROMREADMODE1). 28725 * 28726 * Arguments: dev - the device 'dev_t' 28727 * data - pointer to user provided cd read structure specifying 28728 * the lba buffer address and length. 28729 * flag - this argument is a pass through to ddi_copyxxx() 28730 * directly from the mode argument of ioctl(). 28731 * 28732 * Return Code: the code returned by sd_send_scsi_cmd() 28733 * EFAULT if ddi_copyxxx() fails 28734 * ENXIO if fail ddi_get_soft_state 28735 * EINVAL if data pointer is NULL 28736 */ 28737 28738 static int 28739 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28740 { 28741 struct sd_lun *un; 28742 struct cdrom_read mode1_struct; 28743 struct cdrom_read *mode1 = &mode1_struct; 28744 int rval; 28745 #ifdef _MULTI_DATAMODEL 28746 /* To support ILP32 applications in an LP64 world */ 28747 struct cdrom_read32 cdrom_read32; 28748 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28749 #endif /* _MULTI_DATAMODEL */ 28750 28751 if (data == NULL) { 28752 return (EINVAL); 28753 } 28754 28755 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28756 (un->un_state == SD_STATE_OFFLINE)) { 28757 return (ENXIO); 28758 } 28759 28760 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28761 "sd_read_mode1: entry: un:0x%p\n", un); 28762 28763 #ifdef _MULTI_DATAMODEL 28764 switch (ddi_model_convert_from(flag & FMODELS)) { 28765 case DDI_MODEL_ILP32: 28766 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28767 return (EFAULT); 28768 } 28769 /* Convert the ILP32 uscsi data from the application to LP64 */ 28770 cdrom_read32tocdrom_read(cdrd32, mode1); 28771 break; 28772 case DDI_MODEL_NONE: 28773 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28774 return (EFAULT); 28775 } 28776 } 28777 #else /* ! _MULTI_DATAMODEL */ 28778 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28779 return (EFAULT); 28780 } 28781 #endif /* _MULTI_DATAMODEL */ 28782 28783 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28784 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28785 28786 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28787 "sd_read_mode1: exit: un:0x%p\n", un); 28788 28789 return (rval); 28790 } 28791 28792 28793 /* 28794 * Function: sr_read_cd_mode2() 28795 * 28796 * Description: This routine is the driver entry point for handling CD-ROM 28797 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28798 * support the READ CD (0xBE) command or the 1st generation 28799 * READ CD (0xD4) command. 28800 * 28801 * Arguments: dev - the device 'dev_t' 28802 * data - pointer to user provided cd read structure specifying 28803 * the lba buffer address and length. 28804 * flag - this argument is a pass through to ddi_copyxxx() 28805 * directly from the mode argument of ioctl(). 28806 * 28807 * Return Code: the code returned by sd_send_scsi_cmd() 28808 * EFAULT if ddi_copyxxx() fails 28809 * ENXIO if fail ddi_get_soft_state 28810 * EINVAL if data pointer is NULL 28811 */ 28812 28813 static int 28814 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28815 { 28816 struct sd_lun *un; 28817 struct uscsi_cmd *com; 28818 struct cdrom_read mode2_struct; 28819 struct cdrom_read *mode2 = &mode2_struct; 28820 uchar_t cdb[CDB_GROUP5]; 28821 int nblocks; 28822 int rval; 28823 #ifdef _MULTI_DATAMODEL 28824 /* To support ILP32 applications in an LP64 world */ 28825 struct cdrom_read32 cdrom_read32; 28826 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28827 #endif /* _MULTI_DATAMODEL */ 28828 28829 if (data == NULL) { 28830 return (EINVAL); 28831 } 28832 28833 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28834 (un->un_state == SD_STATE_OFFLINE)) { 28835 return (ENXIO); 28836 } 28837 28838 #ifdef _MULTI_DATAMODEL 28839 switch (ddi_model_convert_from(flag & FMODELS)) { 28840 case DDI_MODEL_ILP32: 28841 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28842 return (EFAULT); 28843 } 28844 /* Convert the ILP32 uscsi data from the application to LP64 */ 28845 cdrom_read32tocdrom_read(cdrd32, mode2); 28846 break; 28847 case DDI_MODEL_NONE: 28848 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28849 return (EFAULT); 28850 } 28851 break; 28852 } 28853 28854 #else /* ! _MULTI_DATAMODEL */ 28855 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28856 return (EFAULT); 28857 } 28858 #endif /* _MULTI_DATAMODEL */ 28859 28860 bzero(cdb, sizeof (cdb)); 28861 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28862 /* Read command supported by 1st generation atapi drives */ 28863 cdb[0] = SCMD_READ_CDD4; 28864 } else { 28865 /* Universal CD Access Command */ 28866 cdb[0] = SCMD_READ_CD; 28867 } 28868 28869 /* 28870 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28871 */ 28872 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28873 28874 /* set the start address */ 28875 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28876 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28877 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28878 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28879 28880 /* set the transfer length */ 28881 nblocks = mode2->cdread_buflen / 2336; 28882 cdb[6] = (uchar_t)(nblocks >> 16); 28883 cdb[7] = (uchar_t)(nblocks >> 8); 28884 cdb[8] = (uchar_t)nblocks; 28885 28886 /* set the filter bits */ 28887 cdb[9] = CDROM_READ_CD_USERDATA; 28888 28889 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28890 com->uscsi_cdb = (caddr_t)cdb; 28891 com->uscsi_cdblen = sizeof (cdb); 28892 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28893 com->uscsi_buflen = mode2->cdread_buflen; 28894 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28895 28896 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28897 UIO_SYSSPACE, SD_PATH_STANDARD); 28898 kmem_free(com, sizeof (*com)); 28899 return (rval); 28900 } 28901 28902 28903 /* 28904 * Function: sr_read_mode2() 28905 * 28906 * Description: This routine is the driver entry point for handling CD-ROM 28907 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28908 * do not support the READ CD (0xBE) command. 28909 * 28910 * Arguments: dev - the device 'dev_t' 28911 * data - pointer to user provided cd read structure specifying 28912 * the lba buffer address and length. 28913 * flag - this argument is a pass through to ddi_copyxxx() 28914 * directly from the mode argument of ioctl(). 28915 * 28916 * Return Code: the code returned by sd_send_scsi_cmd() 28917 * EFAULT if ddi_copyxxx() fails 28918 * ENXIO if fail ddi_get_soft_state 28919 * EINVAL if data pointer is NULL 28920 * EIO if fail to reset block size 28921 * EAGAIN if commands are in progress in the driver 28922 */ 28923 28924 static int 28925 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28926 { 28927 struct sd_lun *un; 28928 struct cdrom_read mode2_struct; 28929 struct cdrom_read *mode2 = &mode2_struct; 28930 int rval; 28931 uint32_t restore_blksize; 28932 struct uscsi_cmd *com; 28933 uchar_t cdb[CDB_GROUP0]; 28934 int nblocks; 28935 28936 #ifdef _MULTI_DATAMODEL 28937 /* To support ILP32 applications in an LP64 world */ 28938 struct cdrom_read32 cdrom_read32; 28939 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28940 #endif /* _MULTI_DATAMODEL */ 28941 28942 if (data == NULL) { 28943 return (EINVAL); 28944 } 28945 28946 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28947 (un->un_state == SD_STATE_OFFLINE)) { 28948 return (ENXIO); 28949 } 28950 28951 /* 28952 * Because this routine will update the device and driver block size 28953 * being used we want to make sure there are no commands in progress. 28954 * If commands are in progress the user will have to try again. 28955 * 28956 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28957 * in sdioctl to protect commands from sdioctl through to the top of 28958 * sd_uscsi_strategy. See sdioctl for details. 28959 */ 28960 mutex_enter(SD_MUTEX(un)); 28961 if (un->un_ncmds_in_driver != 1) { 28962 mutex_exit(SD_MUTEX(un)); 28963 return (EAGAIN); 28964 } 28965 mutex_exit(SD_MUTEX(un)); 28966 28967 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28968 "sd_read_mode2: entry: un:0x%p\n", un); 28969 28970 #ifdef _MULTI_DATAMODEL 28971 switch (ddi_model_convert_from(flag & FMODELS)) { 28972 case DDI_MODEL_ILP32: 28973 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28974 return (EFAULT); 28975 } 28976 /* Convert the ILP32 uscsi data from the application to LP64 */ 28977 cdrom_read32tocdrom_read(cdrd32, mode2); 28978 break; 28979 case DDI_MODEL_NONE: 28980 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28981 return (EFAULT); 28982 } 28983 break; 28984 } 28985 #else /* ! _MULTI_DATAMODEL */ 28986 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28987 return (EFAULT); 28988 } 28989 #endif /* _MULTI_DATAMODEL */ 28990 28991 /* Store the current target block size for restoration later */ 28992 restore_blksize = un->un_tgt_blocksize; 28993 28994 /* Change the device and soft state target block size to 2336 */ 28995 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 28996 rval = EIO; 28997 goto done; 28998 } 28999 29000 29001 bzero(cdb, sizeof (cdb)); 29002 29003 /* set READ operation */ 29004 cdb[0] = SCMD_READ; 29005 29006 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 29007 mode2->cdread_lba >>= 2; 29008 29009 /* set the start address */ 29010 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 29011 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 29012 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 29013 29014 /* set the transfer length */ 29015 nblocks = mode2->cdread_buflen / 2336; 29016 cdb[4] = (uchar_t)nblocks & 0xFF; 29017 29018 /* build command */ 29019 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29020 com->uscsi_cdb = (caddr_t)cdb; 29021 com->uscsi_cdblen = sizeof (cdb); 29022 com->uscsi_bufaddr = mode2->cdread_bufaddr; 29023 com->uscsi_buflen = mode2->cdread_buflen; 29024 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29025 29026 /* 29027 * Issue SCSI command with user space address for read buffer. 29028 * 29029 * This sends the command through main channel in the driver. 29030 * 29031 * Since this is accessed via an IOCTL call, we go through the 29032 * standard path, so that if the device was powered down, then 29033 * it would be 'awakened' to handle the command. 29034 */ 29035 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29036 UIO_SYSSPACE, SD_PATH_STANDARD); 29037 29038 kmem_free(com, sizeof (*com)); 29039 29040 /* Restore the device and soft state target block size */ 29041 if (sr_sector_mode(dev, restore_blksize) != 0) { 29042 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29043 "can't do switch back to mode 1\n"); 29044 /* 29045 * If sd_send_scsi_READ succeeded we still need to report 29046 * an error because we failed to reset the block size 29047 */ 29048 if (rval == 0) { 29049 rval = EIO; 29050 } 29051 } 29052 29053 done: 29054 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 29055 "sd_read_mode2: exit: un:0x%p\n", un); 29056 29057 return (rval); 29058 } 29059 29060 29061 /* 29062 * Function: sr_sector_mode() 29063 * 29064 * Description: This utility function is used by sr_read_mode2 to set the target 29065 * block size based on the user specified size. This is a legacy 29066 * implementation based upon a vendor specific mode page 29067 * 29068 * Arguments: dev - the device 'dev_t' 29069 * data - flag indicating if block size is being set to 2336 or 29070 * 512. 29071 * 29072 * Return Code: the code returned by sd_send_scsi_cmd() 29073 * EFAULT if ddi_copyxxx() fails 29074 * ENXIO if fail ddi_get_soft_state 29075 * EINVAL if data pointer is NULL 29076 */ 29077 29078 static int 29079 sr_sector_mode(dev_t dev, uint32_t blksize) 29080 { 29081 struct sd_lun *un; 29082 uchar_t *sense; 29083 uchar_t *select; 29084 int rval; 29085 29086 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29087 (un->un_state == SD_STATE_OFFLINE)) { 29088 return (ENXIO); 29089 } 29090 29091 sense = kmem_zalloc(20, KM_SLEEP); 29092 29093 /* Note: This is a vendor specific mode page (0x81) */ 29094 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 29095 SD_PATH_STANDARD)) != 0) { 29096 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29097 "sr_sector_mode: Mode Sense failed\n"); 29098 kmem_free(sense, 20); 29099 return (rval); 29100 } 29101 select = kmem_zalloc(20, KM_SLEEP); 29102 select[3] = 0x08; 29103 select[10] = ((blksize >> 8) & 0xff); 29104 select[11] = (blksize & 0xff); 29105 select[12] = 0x01; 29106 select[13] = 0x06; 29107 select[14] = sense[14]; 29108 select[15] = sense[15]; 29109 if (blksize == SD_MODE2_BLKSIZE) { 29110 select[14] |= 0x01; 29111 } 29112 29113 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 29114 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 29115 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29116 "sr_sector_mode: Mode Select failed\n"); 29117 } else { 29118 /* 29119 * Only update the softstate block size if we successfully 29120 * changed the device block mode. 29121 */ 29122 mutex_enter(SD_MUTEX(un)); 29123 sd_update_block_info(un, blksize, 0); 29124 mutex_exit(SD_MUTEX(un)); 29125 } 29126 kmem_free(sense, 20); 29127 kmem_free(select, 20); 29128 return (rval); 29129 } 29130 29131 29132 /* 29133 * Function: sr_read_cdda() 29134 * 29135 * Description: This routine is the driver entry point for handling CD-ROM 29136 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 29137 * the target supports CDDA these requests are handled via a vendor 29138 * specific command (0xD8) If the target does not support CDDA 29139 * these requests are handled via the READ CD command (0xBE). 29140 * 29141 * Arguments: dev - the device 'dev_t' 29142 * data - pointer to user provided CD-DA structure specifying 29143 * the track starting address, transfer length, and 29144 * subcode options. 29145 * flag - this argument is a pass through to ddi_copyxxx() 29146 * directly from the mode argument of ioctl(). 29147 * 29148 * Return Code: the code returned by sd_send_scsi_cmd() 29149 * EFAULT if ddi_copyxxx() fails 29150 * ENXIO if fail ddi_get_soft_state 29151 * EINVAL if invalid arguments are provided 29152 * ENOTTY 29153 */ 29154 29155 static int 29156 sr_read_cdda(dev_t dev, caddr_t data, int flag) 29157 { 29158 struct sd_lun *un; 29159 struct uscsi_cmd *com; 29160 struct cdrom_cdda *cdda; 29161 int rval; 29162 size_t buflen; 29163 char cdb[CDB_GROUP5]; 29164 29165 #ifdef _MULTI_DATAMODEL 29166 /* To support ILP32 applications in an LP64 world */ 29167 struct cdrom_cdda32 cdrom_cdda32; 29168 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 29169 #endif /* _MULTI_DATAMODEL */ 29170 29171 if (data == NULL) { 29172 return (EINVAL); 29173 } 29174 29175 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29176 return (ENXIO); 29177 } 29178 29179 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 29180 29181 #ifdef _MULTI_DATAMODEL 29182 switch (ddi_model_convert_from(flag & FMODELS)) { 29183 case DDI_MODEL_ILP32: 29184 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 29185 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29186 "sr_read_cdda: ddi_copyin Failed\n"); 29187 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29188 return (EFAULT); 29189 } 29190 /* Convert the ILP32 uscsi data from the application to LP64 */ 29191 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 29192 break; 29193 case DDI_MODEL_NONE: 29194 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29195 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29196 "sr_read_cdda: ddi_copyin Failed\n"); 29197 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29198 return (EFAULT); 29199 } 29200 break; 29201 } 29202 #else /* ! _MULTI_DATAMODEL */ 29203 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29204 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29205 "sr_read_cdda: ddi_copyin Failed\n"); 29206 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29207 return (EFAULT); 29208 } 29209 #endif /* _MULTI_DATAMODEL */ 29210 29211 /* 29212 * Since MMC-2 expects max 3 bytes for length, check if the 29213 * length input is greater than 3 bytes 29214 */ 29215 if ((cdda->cdda_length & 0xFF000000) != 0) { 29216 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 29217 "cdrom transfer length too large: %d (limit %d)\n", 29218 cdda->cdda_length, 0xFFFFFF); 29219 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29220 return (EINVAL); 29221 } 29222 29223 switch (cdda->cdda_subcode) { 29224 case CDROM_DA_NO_SUBCODE: 29225 buflen = CDROM_BLK_2352 * cdda->cdda_length; 29226 break; 29227 case CDROM_DA_SUBQ: 29228 buflen = CDROM_BLK_2368 * cdda->cdda_length; 29229 break; 29230 case CDROM_DA_ALL_SUBCODE: 29231 buflen = CDROM_BLK_2448 * cdda->cdda_length; 29232 break; 29233 case CDROM_DA_SUBCODE_ONLY: 29234 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 29235 break; 29236 default: 29237 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29238 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 29239 cdda->cdda_subcode); 29240 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29241 return (EINVAL); 29242 } 29243 29244 /* Build and send the command */ 29245 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29246 bzero(cdb, CDB_GROUP5); 29247 29248 if (un->un_f_cfg_cdda == TRUE) { 29249 cdb[0] = (char)SCMD_READ_CD; 29250 cdb[1] = 0x04; 29251 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29252 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29253 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29254 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29255 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29256 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29257 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 29258 cdb[9] = 0x10; 29259 switch (cdda->cdda_subcode) { 29260 case CDROM_DA_NO_SUBCODE : 29261 cdb[10] = 0x0; 29262 break; 29263 case CDROM_DA_SUBQ : 29264 cdb[10] = 0x2; 29265 break; 29266 case CDROM_DA_ALL_SUBCODE : 29267 cdb[10] = 0x1; 29268 break; 29269 case CDROM_DA_SUBCODE_ONLY : 29270 /* FALLTHROUGH */ 29271 default : 29272 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29273 kmem_free(com, sizeof (*com)); 29274 return (ENOTTY); 29275 } 29276 } else { 29277 cdb[0] = (char)SCMD_READ_CDDA; 29278 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29279 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29280 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29281 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29282 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 29283 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29284 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29285 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 29286 cdb[10] = cdda->cdda_subcode; 29287 } 29288 29289 com->uscsi_cdb = cdb; 29290 com->uscsi_cdblen = CDB_GROUP5; 29291 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 29292 com->uscsi_buflen = buflen; 29293 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29294 29295 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29296 UIO_SYSSPACE, SD_PATH_STANDARD); 29297 29298 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29299 kmem_free(com, sizeof (*com)); 29300 return (rval); 29301 } 29302 29303 29304 /* 29305 * Function: sr_read_cdxa() 29306 * 29307 * Description: This routine is the driver entry point for handling CD-ROM 29308 * ioctl requests to return CD-XA (Extended Architecture) data. 29309 * (CDROMCDXA). 29310 * 29311 * Arguments: dev - the device 'dev_t' 29312 * data - pointer to user provided CD-XA structure specifying 29313 * the data starting address, transfer length, and format 29314 * flag - this argument is a pass through to ddi_copyxxx() 29315 * directly from the mode argument of ioctl(). 29316 * 29317 * Return Code: the code returned by sd_send_scsi_cmd() 29318 * EFAULT if ddi_copyxxx() fails 29319 * ENXIO if fail ddi_get_soft_state 29320 * EINVAL if data pointer is NULL 29321 */ 29322 29323 static int 29324 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 29325 { 29326 struct sd_lun *un; 29327 struct uscsi_cmd *com; 29328 struct cdrom_cdxa *cdxa; 29329 int rval; 29330 size_t buflen; 29331 char cdb[CDB_GROUP5]; 29332 uchar_t read_flags; 29333 29334 #ifdef _MULTI_DATAMODEL 29335 /* To support ILP32 applications in an LP64 world */ 29336 struct cdrom_cdxa32 cdrom_cdxa32; 29337 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 29338 #endif /* _MULTI_DATAMODEL */ 29339 29340 if (data == NULL) { 29341 return (EINVAL); 29342 } 29343 29344 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29345 return (ENXIO); 29346 } 29347 29348 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 29349 29350 #ifdef _MULTI_DATAMODEL 29351 switch (ddi_model_convert_from(flag & FMODELS)) { 29352 case DDI_MODEL_ILP32: 29353 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 29354 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29355 return (EFAULT); 29356 } 29357 /* 29358 * Convert the ILP32 uscsi data from the 29359 * application to LP64 for internal use. 29360 */ 29361 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 29362 break; 29363 case DDI_MODEL_NONE: 29364 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29365 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29366 return (EFAULT); 29367 } 29368 break; 29369 } 29370 #else /* ! _MULTI_DATAMODEL */ 29371 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29372 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29373 return (EFAULT); 29374 } 29375 #endif /* _MULTI_DATAMODEL */ 29376 29377 /* 29378 * Since MMC-2 expects max 3 bytes for length, check if the 29379 * length input is greater than 3 bytes 29380 */ 29381 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 29382 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 29383 "cdrom transfer length too large: %d (limit %d)\n", 29384 cdxa->cdxa_length, 0xFFFFFF); 29385 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29386 return (EINVAL); 29387 } 29388 29389 switch (cdxa->cdxa_format) { 29390 case CDROM_XA_DATA: 29391 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 29392 read_flags = 0x10; 29393 break; 29394 case CDROM_XA_SECTOR_DATA: 29395 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 29396 read_flags = 0xf8; 29397 break; 29398 case CDROM_XA_DATA_W_ERROR: 29399 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 29400 read_flags = 0xfc; 29401 break; 29402 default: 29403 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29404 "sr_read_cdxa: Format '0x%x' Not Supported\n", 29405 cdxa->cdxa_format); 29406 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29407 return (EINVAL); 29408 } 29409 29410 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29411 bzero(cdb, CDB_GROUP5); 29412 if (un->un_f_mmc_cap == TRUE) { 29413 cdb[0] = (char)SCMD_READ_CD; 29414 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29415 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29416 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29417 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29418 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29419 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29420 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 29421 cdb[9] = (char)read_flags; 29422 } else { 29423 /* 29424 * Note: A vendor specific command (0xDB) is being used her to 29425 * request a read of all subcodes. 29426 */ 29427 cdb[0] = (char)SCMD_READ_CDXA; 29428 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29429 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29430 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29431 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29432 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 29433 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29434 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29435 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 29436 cdb[10] = cdxa->cdxa_format; 29437 } 29438 com->uscsi_cdb = cdb; 29439 com->uscsi_cdblen = CDB_GROUP5; 29440 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 29441 com->uscsi_buflen = buflen; 29442 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29443 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29444 UIO_SYSSPACE, SD_PATH_STANDARD); 29445 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29446 kmem_free(com, sizeof (*com)); 29447 return (rval); 29448 } 29449 29450 29451 /* 29452 * Function: sr_eject() 29453 * 29454 * Description: This routine is the driver entry point for handling CD-ROM 29455 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 29456 * 29457 * Arguments: dev - the device 'dev_t' 29458 * 29459 * Return Code: the code returned by sd_send_scsi_cmd() 29460 */ 29461 29462 static int 29463 sr_eject(dev_t dev) 29464 { 29465 struct sd_lun *un; 29466 int rval; 29467 29468 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29469 (un->un_state == SD_STATE_OFFLINE)) { 29470 return (ENXIO); 29471 } 29472 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29473 SD_PATH_STANDARD)) != 0) { 29474 return (rval); 29475 } 29476 29477 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29478 SD_PATH_STANDARD); 29479 29480 if (rval == 0) { 29481 mutex_enter(SD_MUTEX(un)); 29482 sr_ejected(un); 29483 un->un_mediastate = DKIO_EJECTED; 29484 cv_broadcast(&un->un_state_cv); 29485 mutex_exit(SD_MUTEX(un)); 29486 } 29487 return (rval); 29488 } 29489 29490 29491 /* 29492 * Function: sr_ejected() 29493 * 29494 * Description: This routine updates the soft state structure to invalidate the 29495 * geometry information after the media has been ejected or a 29496 * media eject has been detected. 29497 * 29498 * Arguments: un - driver soft state (unit) structure 29499 */ 29500 29501 static void 29502 sr_ejected(struct sd_lun *un) 29503 { 29504 struct sd_errstats *stp; 29505 29506 ASSERT(un != NULL); 29507 ASSERT(mutex_owned(SD_MUTEX(un))); 29508 29509 un->un_f_blockcount_is_valid = FALSE; 29510 un->un_f_tgt_blocksize_is_valid = FALSE; 29511 un->un_f_geometry_is_valid = FALSE; 29512 29513 if (un->un_errstats != NULL) { 29514 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29515 stp->sd_capacity.value.ui64 = 0; 29516 } 29517 } 29518 29519 29520 /* 29521 * Function: sr_check_wp() 29522 * 29523 * Description: This routine checks the write protection of a removable 29524 * media disk and hotpluggable devices via the write protect bit of 29525 * the Mode Page Header device specific field. Some devices choke 29526 * on unsupported mode page. In order to workaround this issue, 29527 * this routine has been implemented to use 0x3f mode page(request 29528 * for all pages) for all device types. 29529 * 29530 * Arguments: dev - the device 'dev_t' 29531 * 29532 * Return Code: int indicating if the device is write protected (1) or not (0) 29533 * 29534 * Context: Kernel thread. 29535 * 29536 */ 29537 29538 static int 29539 sr_check_wp(dev_t dev) 29540 { 29541 struct sd_lun *un; 29542 uchar_t device_specific; 29543 uchar_t *sense; 29544 int hdrlen; 29545 int rval = FALSE; 29546 29547 /* 29548 * Note: The return codes for this routine should be reworked to 29549 * properly handle the case of a NULL softstate. 29550 */ 29551 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29552 return (FALSE); 29553 } 29554 29555 if (un->un_f_cfg_is_atapi == TRUE) { 29556 /* 29557 * The mode page contents are not required; set the allocation 29558 * length for the mode page header only 29559 */ 29560 hdrlen = MODE_HEADER_LENGTH_GRP2; 29561 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29562 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29563 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29564 goto err_exit; 29565 device_specific = 29566 ((struct mode_header_grp2 *)sense)->device_specific; 29567 } else { 29568 hdrlen = MODE_HEADER_LENGTH; 29569 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29570 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29571 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29572 goto err_exit; 29573 device_specific = 29574 ((struct mode_header *)sense)->device_specific; 29575 } 29576 29577 /* 29578 * Write protect mode sense failed; not all disks 29579 * understand this query. Return FALSE assuming that 29580 * these devices are not writable. 29581 */ 29582 if (device_specific & WRITE_PROTECT) { 29583 rval = TRUE; 29584 } 29585 29586 err_exit: 29587 kmem_free(sense, hdrlen); 29588 return (rval); 29589 } 29590 29591 /* 29592 * Function: sr_volume_ctrl() 29593 * 29594 * Description: This routine is the driver entry point for handling CD-ROM 29595 * audio output volume ioctl requests. (CDROMVOLCTRL) 29596 * 29597 * Arguments: dev - the device 'dev_t' 29598 * data - pointer to user audio volume control structure 29599 * flag - this argument is a pass through to ddi_copyxxx() 29600 * directly from the mode argument of ioctl(). 29601 * 29602 * Return Code: the code returned by sd_send_scsi_cmd() 29603 * EFAULT if ddi_copyxxx() fails 29604 * ENXIO if fail ddi_get_soft_state 29605 * EINVAL if data pointer is NULL 29606 * 29607 */ 29608 29609 static int 29610 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29611 { 29612 struct sd_lun *un; 29613 struct cdrom_volctrl volume; 29614 struct cdrom_volctrl *vol = &volume; 29615 uchar_t *sense_page; 29616 uchar_t *select_page; 29617 uchar_t *sense; 29618 uchar_t *select; 29619 int sense_buflen; 29620 int select_buflen; 29621 int rval; 29622 29623 if (data == NULL) { 29624 return (EINVAL); 29625 } 29626 29627 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29628 (un->un_state == SD_STATE_OFFLINE)) { 29629 return (ENXIO); 29630 } 29631 29632 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29633 return (EFAULT); 29634 } 29635 29636 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29637 struct mode_header_grp2 *sense_mhp; 29638 struct mode_header_grp2 *select_mhp; 29639 int bd_len; 29640 29641 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29642 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29643 MODEPAGE_AUDIO_CTRL_LEN; 29644 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29645 select = kmem_zalloc(select_buflen, KM_SLEEP); 29646 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29647 sense_buflen, MODEPAGE_AUDIO_CTRL, 29648 SD_PATH_STANDARD)) != 0) { 29649 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29650 "sr_volume_ctrl: Mode Sense Failed\n"); 29651 kmem_free(sense, sense_buflen); 29652 kmem_free(select, select_buflen); 29653 return (rval); 29654 } 29655 sense_mhp = (struct mode_header_grp2 *)sense; 29656 select_mhp = (struct mode_header_grp2 *)select; 29657 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29658 sense_mhp->bdesc_length_lo; 29659 if (bd_len > MODE_BLK_DESC_LENGTH) { 29660 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29661 "sr_volume_ctrl: Mode Sense returned invalid " 29662 "block descriptor length\n"); 29663 kmem_free(sense, sense_buflen); 29664 kmem_free(select, select_buflen); 29665 return (EIO); 29666 } 29667 sense_page = (uchar_t *) 29668 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29669 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29670 select_mhp->length_msb = 0; 29671 select_mhp->length_lsb = 0; 29672 select_mhp->bdesc_length_hi = 0; 29673 select_mhp->bdesc_length_lo = 0; 29674 } else { 29675 struct mode_header *sense_mhp, *select_mhp; 29676 29677 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29678 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29679 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29680 select = kmem_zalloc(select_buflen, KM_SLEEP); 29681 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29682 sense_buflen, MODEPAGE_AUDIO_CTRL, 29683 SD_PATH_STANDARD)) != 0) { 29684 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29685 "sr_volume_ctrl: Mode Sense Failed\n"); 29686 kmem_free(sense, sense_buflen); 29687 kmem_free(select, select_buflen); 29688 return (rval); 29689 } 29690 sense_mhp = (struct mode_header *)sense; 29691 select_mhp = (struct mode_header *)select; 29692 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29693 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29694 "sr_volume_ctrl: Mode Sense returned invalid " 29695 "block descriptor length\n"); 29696 kmem_free(sense, sense_buflen); 29697 kmem_free(select, select_buflen); 29698 return (EIO); 29699 } 29700 sense_page = (uchar_t *) 29701 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29702 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29703 select_mhp->length = 0; 29704 select_mhp->bdesc_length = 0; 29705 } 29706 /* 29707 * Note: An audio control data structure could be created and overlayed 29708 * on the following in place of the array indexing method implemented. 29709 */ 29710 29711 /* Build the select data for the user volume data */ 29712 select_page[0] = MODEPAGE_AUDIO_CTRL; 29713 select_page[1] = 0xE; 29714 /* Set the immediate bit */ 29715 select_page[2] = 0x04; 29716 /* Zero out reserved fields */ 29717 select_page[3] = 0x00; 29718 select_page[4] = 0x00; 29719 /* Return sense data for fields not to be modified */ 29720 select_page[5] = sense_page[5]; 29721 select_page[6] = sense_page[6]; 29722 select_page[7] = sense_page[7]; 29723 /* Set the user specified volume levels for channel 0 and 1 */ 29724 select_page[8] = 0x01; 29725 select_page[9] = vol->channel0; 29726 select_page[10] = 0x02; 29727 select_page[11] = vol->channel1; 29728 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29729 select_page[12] = sense_page[12]; 29730 select_page[13] = sense_page[13]; 29731 select_page[14] = sense_page[14]; 29732 select_page[15] = sense_page[15]; 29733 29734 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29735 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29736 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29737 } else { 29738 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29739 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29740 } 29741 29742 kmem_free(sense, sense_buflen); 29743 kmem_free(select, select_buflen); 29744 return (rval); 29745 } 29746 29747 29748 /* 29749 * Function: sr_read_sony_session_offset() 29750 * 29751 * Description: This routine is the driver entry point for handling CD-ROM 29752 * ioctl requests for session offset information. (CDROMREADOFFSET) 29753 * The address of the first track in the last session of a 29754 * multi-session CD-ROM is returned 29755 * 29756 * Note: This routine uses a vendor specific key value in the 29757 * command control field without implementing any vendor check here 29758 * or in the ioctl routine. 29759 * 29760 * Arguments: dev - the device 'dev_t' 29761 * data - pointer to an int to hold the requested address 29762 * flag - this argument is a pass through to ddi_copyxxx() 29763 * directly from the mode argument of ioctl(). 29764 * 29765 * Return Code: the code returned by sd_send_scsi_cmd() 29766 * EFAULT if ddi_copyxxx() fails 29767 * ENXIO if fail ddi_get_soft_state 29768 * EINVAL if data pointer is NULL 29769 */ 29770 29771 static int 29772 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29773 { 29774 struct sd_lun *un; 29775 struct uscsi_cmd *com; 29776 caddr_t buffer; 29777 char cdb[CDB_GROUP1]; 29778 int session_offset = 0; 29779 int rval; 29780 29781 if (data == NULL) { 29782 return (EINVAL); 29783 } 29784 29785 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29786 (un->un_state == SD_STATE_OFFLINE)) { 29787 return (ENXIO); 29788 } 29789 29790 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29791 bzero(cdb, CDB_GROUP1); 29792 cdb[0] = SCMD_READ_TOC; 29793 /* 29794 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29795 * (4 byte TOC response header + 8 byte response data) 29796 */ 29797 cdb[8] = SONY_SESSION_OFFSET_LEN; 29798 /* Byte 9 is the control byte. A vendor specific value is used */ 29799 cdb[9] = SONY_SESSION_OFFSET_KEY; 29800 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29801 com->uscsi_cdb = cdb; 29802 com->uscsi_cdblen = CDB_GROUP1; 29803 com->uscsi_bufaddr = buffer; 29804 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29805 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29806 29807 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29808 UIO_SYSSPACE, SD_PATH_STANDARD); 29809 if (rval != 0) { 29810 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29811 kmem_free(com, sizeof (*com)); 29812 return (rval); 29813 } 29814 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29815 session_offset = 29816 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29817 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29818 /* 29819 * Offset returned offset in current lbasize block's. Convert to 29820 * 2k block's to return to the user 29821 */ 29822 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29823 session_offset >>= 2; 29824 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29825 session_offset >>= 1; 29826 } 29827 } 29828 29829 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29830 rval = EFAULT; 29831 } 29832 29833 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29834 kmem_free(com, sizeof (*com)); 29835 return (rval); 29836 } 29837 29838 29839 /* 29840 * Function: sd_wm_cache_constructor() 29841 * 29842 * Description: Cache Constructor for the wmap cache for the read/modify/write 29843 * devices. 29844 * 29845 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29846 * un - sd_lun structure for the device. 29847 * flag - the km flags passed to constructor 29848 * 29849 * Return Code: 0 on success. 29850 * -1 on failure. 29851 */ 29852 29853 /*ARGSUSED*/ 29854 static int 29855 sd_wm_cache_constructor(void *wm, void *un, int flags) 29856 { 29857 bzero(wm, sizeof (struct sd_w_map)); 29858 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29859 return (0); 29860 } 29861 29862 29863 /* 29864 * Function: sd_wm_cache_destructor() 29865 * 29866 * Description: Cache destructor for the wmap cache for the read/modify/write 29867 * devices. 29868 * 29869 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29870 * un - sd_lun structure for the device. 29871 */ 29872 /*ARGSUSED*/ 29873 static void 29874 sd_wm_cache_destructor(void *wm, void *un) 29875 { 29876 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29877 } 29878 29879 29880 /* 29881 * Function: sd_range_lock() 29882 * 29883 * Description: Lock the range of blocks specified as parameter to ensure 29884 * that read, modify write is atomic and no other i/o writes 29885 * to the same location. The range is specified in terms 29886 * of start and end blocks. Block numbers are the actual 29887 * media block numbers and not system. 29888 * 29889 * Arguments: un - sd_lun structure for the device. 29890 * startb - The starting block number 29891 * endb - The end block number 29892 * typ - type of i/o - simple/read_modify_write 29893 * 29894 * Return Code: wm - pointer to the wmap structure. 29895 * 29896 * Context: This routine can sleep. 29897 */ 29898 29899 static struct sd_w_map * 29900 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29901 { 29902 struct sd_w_map *wmp = NULL; 29903 struct sd_w_map *sl_wmp = NULL; 29904 struct sd_w_map *tmp_wmp; 29905 wm_state state = SD_WM_CHK_LIST; 29906 29907 29908 ASSERT(un != NULL); 29909 ASSERT(!mutex_owned(SD_MUTEX(un))); 29910 29911 mutex_enter(SD_MUTEX(un)); 29912 29913 while (state != SD_WM_DONE) { 29914 29915 switch (state) { 29916 case SD_WM_CHK_LIST: 29917 /* 29918 * This is the starting state. Check the wmap list 29919 * to see if the range is currently available. 29920 */ 29921 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29922 /* 29923 * If this is a simple write and no rmw 29924 * i/o is pending then try to lock the 29925 * range as the range should be available. 29926 */ 29927 state = SD_WM_LOCK_RANGE; 29928 } else { 29929 tmp_wmp = sd_get_range(un, startb, endb); 29930 if (tmp_wmp != NULL) { 29931 if ((wmp != NULL) && ONLIST(un, wmp)) { 29932 /* 29933 * Should not keep onlist wmps 29934 * while waiting this macro 29935 * will also do wmp = NULL; 29936 */ 29937 FREE_ONLIST_WMAP(un, wmp); 29938 } 29939 /* 29940 * sl_wmp is the wmap on which wait 29941 * is done, since the tmp_wmp points 29942 * to the inuse wmap, set sl_wmp to 29943 * tmp_wmp and change the state to sleep 29944 */ 29945 sl_wmp = tmp_wmp; 29946 state = SD_WM_WAIT_MAP; 29947 } else { 29948 state = SD_WM_LOCK_RANGE; 29949 } 29950 29951 } 29952 break; 29953 29954 case SD_WM_LOCK_RANGE: 29955 ASSERT(un->un_wm_cache); 29956 /* 29957 * The range need to be locked, try to get a wmap. 29958 * First attempt it with NO_SLEEP, want to avoid a sleep 29959 * if possible as we will have to release the sd mutex 29960 * if we have to sleep. 29961 */ 29962 if (wmp == NULL) 29963 wmp = kmem_cache_alloc(un->un_wm_cache, 29964 KM_NOSLEEP); 29965 if (wmp == NULL) { 29966 mutex_exit(SD_MUTEX(un)); 29967 _NOTE(DATA_READABLE_WITHOUT_LOCK 29968 (sd_lun::un_wm_cache)) 29969 wmp = kmem_cache_alloc(un->un_wm_cache, 29970 KM_SLEEP); 29971 mutex_enter(SD_MUTEX(un)); 29972 /* 29973 * we released the mutex so recheck and go to 29974 * check list state. 29975 */ 29976 state = SD_WM_CHK_LIST; 29977 } else { 29978 /* 29979 * We exit out of state machine since we 29980 * have the wmap. Do the housekeeping first. 29981 * place the wmap on the wmap list if it is not 29982 * on it already and then set the state to done. 29983 */ 29984 wmp->wm_start = startb; 29985 wmp->wm_end = endb; 29986 wmp->wm_flags = typ | SD_WM_BUSY; 29987 if (typ & SD_WTYPE_RMW) { 29988 un->un_rmw_count++; 29989 } 29990 /* 29991 * If not already on the list then link 29992 */ 29993 if (!ONLIST(un, wmp)) { 29994 wmp->wm_next = un->un_wm; 29995 wmp->wm_prev = NULL; 29996 if (wmp->wm_next) 29997 wmp->wm_next->wm_prev = wmp; 29998 un->un_wm = wmp; 29999 } 30000 state = SD_WM_DONE; 30001 } 30002 break; 30003 30004 case SD_WM_WAIT_MAP: 30005 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 30006 /* 30007 * Wait is done on sl_wmp, which is set in the 30008 * check_list state. 30009 */ 30010 sl_wmp->wm_wanted_count++; 30011 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 30012 sl_wmp->wm_wanted_count--; 30013 /* 30014 * We can reuse the memory from the completed sl_wmp 30015 * lock range for our new lock, but only if noone is 30016 * waiting for it. 30017 */ 30018 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 30019 if (sl_wmp->wm_wanted_count == 0) { 30020 if (wmp != NULL) 30021 CHK_N_FREEWMP(un, wmp); 30022 wmp = sl_wmp; 30023 } 30024 sl_wmp = NULL; 30025 /* 30026 * After waking up, need to recheck for availability of 30027 * range. 30028 */ 30029 state = SD_WM_CHK_LIST; 30030 break; 30031 30032 default: 30033 panic("sd_range_lock: " 30034 "Unknown state %d in sd_range_lock", state); 30035 /*NOTREACHED*/ 30036 } /* switch(state) */ 30037 30038 } /* while(state != SD_WM_DONE) */ 30039 30040 mutex_exit(SD_MUTEX(un)); 30041 30042 ASSERT(wmp != NULL); 30043 30044 return (wmp); 30045 } 30046 30047 30048 /* 30049 * Function: sd_get_range() 30050 * 30051 * Description: Find if there any overlapping I/O to this one 30052 * Returns the write-map of 1st such I/O, NULL otherwise. 30053 * 30054 * Arguments: un - sd_lun structure for the device. 30055 * startb - The starting block number 30056 * endb - The end block number 30057 * 30058 * Return Code: wm - pointer to the wmap structure. 30059 */ 30060 30061 static struct sd_w_map * 30062 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 30063 { 30064 struct sd_w_map *wmp; 30065 30066 ASSERT(un != NULL); 30067 30068 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 30069 if (!(wmp->wm_flags & SD_WM_BUSY)) { 30070 continue; 30071 } 30072 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 30073 break; 30074 } 30075 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 30076 break; 30077 } 30078 } 30079 30080 return (wmp); 30081 } 30082 30083 30084 /* 30085 * Function: sd_free_inlist_wmap() 30086 * 30087 * Description: Unlink and free a write map struct. 30088 * 30089 * Arguments: un - sd_lun structure for the device. 30090 * wmp - sd_w_map which needs to be unlinked. 30091 */ 30092 30093 static void 30094 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 30095 { 30096 ASSERT(un != NULL); 30097 30098 if (un->un_wm == wmp) { 30099 un->un_wm = wmp->wm_next; 30100 } else { 30101 wmp->wm_prev->wm_next = wmp->wm_next; 30102 } 30103 30104 if (wmp->wm_next) { 30105 wmp->wm_next->wm_prev = wmp->wm_prev; 30106 } 30107 30108 wmp->wm_next = wmp->wm_prev = NULL; 30109 30110 kmem_cache_free(un->un_wm_cache, wmp); 30111 } 30112 30113 30114 /* 30115 * Function: sd_range_unlock() 30116 * 30117 * Description: Unlock the range locked by wm. 30118 * Free write map if nobody else is waiting on it. 30119 * 30120 * Arguments: un - sd_lun structure for the device. 30121 * wmp - sd_w_map which needs to be unlinked. 30122 */ 30123 30124 static void 30125 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 30126 { 30127 ASSERT(un != NULL); 30128 ASSERT(wm != NULL); 30129 ASSERT(!mutex_owned(SD_MUTEX(un))); 30130 30131 mutex_enter(SD_MUTEX(un)); 30132 30133 if (wm->wm_flags & SD_WTYPE_RMW) { 30134 un->un_rmw_count--; 30135 } 30136 30137 if (wm->wm_wanted_count) { 30138 wm->wm_flags = 0; 30139 /* 30140 * Broadcast that the wmap is available now. 30141 */ 30142 cv_broadcast(&wm->wm_avail); 30143 } else { 30144 /* 30145 * If no one is waiting on the map, it should be free'ed. 30146 */ 30147 sd_free_inlist_wmap(un, wm); 30148 } 30149 30150 mutex_exit(SD_MUTEX(un)); 30151 } 30152 30153 30154 /* 30155 * Function: sd_read_modify_write_task 30156 * 30157 * Description: Called from a taskq thread to initiate the write phase of 30158 * a read-modify-write request. This is used for targets where 30159 * un->un_sys_blocksize != un->un_tgt_blocksize. 30160 * 30161 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 30162 * 30163 * Context: Called under taskq thread context. 30164 */ 30165 30166 static void 30167 sd_read_modify_write_task(void *arg) 30168 { 30169 struct sd_mapblocksize_info *bsp; 30170 struct buf *bp; 30171 struct sd_xbuf *xp; 30172 struct sd_lun *un; 30173 30174 bp = arg; /* The bp is given in arg */ 30175 ASSERT(bp != NULL); 30176 30177 /* Get the pointer to the layer-private data struct */ 30178 xp = SD_GET_XBUF(bp); 30179 ASSERT(xp != NULL); 30180 bsp = xp->xb_private; 30181 ASSERT(bsp != NULL); 30182 30183 un = SD_GET_UN(bp); 30184 ASSERT(un != NULL); 30185 ASSERT(!mutex_owned(SD_MUTEX(un))); 30186 30187 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30188 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 30189 30190 /* 30191 * This is the write phase of a read-modify-write request, called 30192 * under the context of a taskq thread in response to the completion 30193 * of the read portion of the rmw request completing under interrupt 30194 * context. The write request must be sent from here down the iostart 30195 * chain as if it were being sent from sd_mapblocksize_iostart(), so 30196 * we use the layer index saved in the layer-private data area. 30197 */ 30198 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 30199 30200 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30201 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 30202 } 30203 30204 30205 /* 30206 * Function: sddump_do_read_of_rmw() 30207 * 30208 * Description: This routine will be called from sddump, If sddump is called 30209 * with an I/O which not aligned on device blocksize boundary 30210 * then the write has to be converted to read-modify-write. 30211 * Do the read part here in order to keep sddump simple. 30212 * Note - That the sd_mutex is held across the call to this 30213 * routine. 30214 * 30215 * Arguments: un - sd_lun 30216 * blkno - block number in terms of media block size. 30217 * nblk - number of blocks. 30218 * bpp - pointer to pointer to the buf structure. On return 30219 * from this function, *bpp points to the valid buffer 30220 * to which the write has to be done. 30221 * 30222 * Return Code: 0 for success or errno-type return code 30223 */ 30224 30225 static int 30226 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 30227 struct buf **bpp) 30228 { 30229 int err; 30230 int i; 30231 int rval; 30232 struct buf *bp; 30233 struct scsi_pkt *pkt = NULL; 30234 uint32_t target_blocksize; 30235 30236 ASSERT(un != NULL); 30237 ASSERT(mutex_owned(SD_MUTEX(un))); 30238 30239 target_blocksize = un->un_tgt_blocksize; 30240 30241 mutex_exit(SD_MUTEX(un)); 30242 30243 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 30244 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 30245 if (bp == NULL) { 30246 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30247 "no resources for dumping; giving up"); 30248 err = ENOMEM; 30249 goto done; 30250 } 30251 30252 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 30253 blkno, nblk); 30254 if (rval != 0) { 30255 scsi_free_consistent_buf(bp); 30256 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30257 "no resources for dumping; giving up"); 30258 err = ENOMEM; 30259 goto done; 30260 } 30261 30262 pkt->pkt_flags |= FLAG_NOINTR; 30263 30264 err = EIO; 30265 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 30266 30267 /* 30268 * Scsi_poll returns 0 (success) if the command completes and 30269 * the status block is STATUS_GOOD. We should only check 30270 * errors if this condition is not true. Even then we should 30271 * send our own request sense packet only if we have a check 30272 * condition and auto request sense has not been performed by 30273 * the hba. 30274 */ 30275 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 30276 30277 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 30278 err = 0; 30279 break; 30280 } 30281 30282 /* 30283 * Check CMD_DEV_GONE 1st, give up if device is gone, 30284 * no need to read RQS data. 30285 */ 30286 if (pkt->pkt_reason == CMD_DEV_GONE) { 30287 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30288 "Device is gone\n"); 30289 break; 30290 } 30291 30292 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 30293 SD_INFO(SD_LOG_DUMP, un, 30294 "sddump: read failed with CHECK, try # %d\n", i); 30295 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 30296 (void) sd_send_polled_RQS(un); 30297 } 30298 30299 continue; 30300 } 30301 30302 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 30303 int reset_retval = 0; 30304 30305 SD_INFO(SD_LOG_DUMP, un, 30306 "sddump: read failed with BUSY, try # %d\n", i); 30307 30308 if (un->un_f_lun_reset_enabled == TRUE) { 30309 reset_retval = scsi_reset(SD_ADDRESS(un), 30310 RESET_LUN); 30311 } 30312 if (reset_retval == 0) { 30313 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 30314 } 30315 (void) sd_send_polled_RQS(un); 30316 30317 } else { 30318 SD_INFO(SD_LOG_DUMP, un, 30319 "sddump: read failed with 0x%x, try # %d\n", 30320 SD_GET_PKT_STATUS(pkt), i); 30321 mutex_enter(SD_MUTEX(un)); 30322 sd_reset_target(un, pkt); 30323 mutex_exit(SD_MUTEX(un)); 30324 } 30325 30326 /* 30327 * If we are not getting anywhere with lun/target resets, 30328 * let's reset the bus. 30329 */ 30330 if (i > SD_NDUMP_RETRIES/2) { 30331 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 30332 (void) sd_send_polled_RQS(un); 30333 } 30334 30335 } 30336 scsi_destroy_pkt(pkt); 30337 30338 if (err != 0) { 30339 scsi_free_consistent_buf(bp); 30340 *bpp = NULL; 30341 } else { 30342 *bpp = bp; 30343 } 30344 30345 done: 30346 mutex_enter(SD_MUTEX(un)); 30347 return (err); 30348 } 30349 30350 30351 /* 30352 * Function: sd_failfast_flushq 30353 * 30354 * Description: Take all bp's on the wait queue that have B_FAILFAST set 30355 * in b_flags and move them onto the failfast queue, then kick 30356 * off a thread to return all bp's on the failfast queue to 30357 * their owners with an error set. 30358 * 30359 * Arguments: un - pointer to the soft state struct for the instance. 30360 * 30361 * Context: may execute in interrupt context. 30362 */ 30363 30364 static void 30365 sd_failfast_flushq(struct sd_lun *un) 30366 { 30367 struct buf *bp; 30368 struct buf *next_waitq_bp; 30369 struct buf *prev_waitq_bp = NULL; 30370 30371 ASSERT(un != NULL); 30372 ASSERT(mutex_owned(SD_MUTEX(un))); 30373 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 30374 ASSERT(un->un_failfast_bp == NULL); 30375 30376 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30377 "sd_failfast_flushq: entry: un:0x%p\n", un); 30378 30379 /* 30380 * Check if we should flush all bufs when entering failfast state, or 30381 * just those with B_FAILFAST set. 30382 */ 30383 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 30384 /* 30385 * Move *all* bp's on the wait queue to the failfast flush 30386 * queue, including those that do NOT have B_FAILFAST set. 30387 */ 30388 if (un->un_failfast_headp == NULL) { 30389 ASSERT(un->un_failfast_tailp == NULL); 30390 un->un_failfast_headp = un->un_waitq_headp; 30391 } else { 30392 ASSERT(un->un_failfast_tailp != NULL); 30393 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 30394 } 30395 30396 un->un_failfast_tailp = un->un_waitq_tailp; 30397 30398 /* update kstat for each bp moved out of the waitq */ 30399 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 30400 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30401 } 30402 30403 /* empty the waitq */ 30404 un->un_waitq_headp = un->un_waitq_tailp = NULL; 30405 30406 } else { 30407 /* 30408 * Go thru the wait queue, pick off all entries with 30409 * B_FAILFAST set, and move these onto the failfast queue. 30410 */ 30411 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 30412 /* 30413 * Save the pointer to the next bp on the wait queue, 30414 * so we get to it on the next iteration of this loop. 30415 */ 30416 next_waitq_bp = bp->av_forw; 30417 30418 /* 30419 * If this bp from the wait queue does NOT have 30420 * B_FAILFAST set, just move on to the next element 30421 * in the wait queue. Note, this is the only place 30422 * where it is correct to set prev_waitq_bp. 30423 */ 30424 if ((bp->b_flags & B_FAILFAST) == 0) { 30425 prev_waitq_bp = bp; 30426 continue; 30427 } 30428 30429 /* 30430 * Remove the bp from the wait queue. 30431 */ 30432 if (bp == un->un_waitq_headp) { 30433 /* The bp is the first element of the waitq. */ 30434 un->un_waitq_headp = next_waitq_bp; 30435 if (un->un_waitq_headp == NULL) { 30436 /* The wait queue is now empty */ 30437 un->un_waitq_tailp = NULL; 30438 } 30439 } else { 30440 /* 30441 * The bp is either somewhere in the middle 30442 * or at the end of the wait queue. 30443 */ 30444 ASSERT(un->un_waitq_headp != NULL); 30445 ASSERT(prev_waitq_bp != NULL); 30446 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 30447 == 0); 30448 if (bp == un->un_waitq_tailp) { 30449 /* bp is the last entry on the waitq. */ 30450 ASSERT(next_waitq_bp == NULL); 30451 un->un_waitq_tailp = prev_waitq_bp; 30452 } 30453 prev_waitq_bp->av_forw = next_waitq_bp; 30454 } 30455 bp->av_forw = NULL; 30456 30457 /* 30458 * update kstat since the bp is moved out of 30459 * the waitq 30460 */ 30461 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30462 30463 /* 30464 * Now put the bp onto the failfast queue. 30465 */ 30466 if (un->un_failfast_headp == NULL) { 30467 /* failfast queue is currently empty */ 30468 ASSERT(un->un_failfast_tailp == NULL); 30469 un->un_failfast_headp = 30470 un->un_failfast_tailp = bp; 30471 } else { 30472 /* Add the bp to the end of the failfast q */ 30473 ASSERT(un->un_failfast_tailp != NULL); 30474 ASSERT(un->un_failfast_tailp->b_flags & 30475 B_FAILFAST); 30476 un->un_failfast_tailp->av_forw = bp; 30477 un->un_failfast_tailp = bp; 30478 } 30479 } 30480 } 30481 30482 /* 30483 * Now return all bp's on the failfast queue to their owners. 30484 */ 30485 while ((bp = un->un_failfast_headp) != NULL) { 30486 30487 un->un_failfast_headp = bp->av_forw; 30488 if (un->un_failfast_headp == NULL) { 30489 un->un_failfast_tailp = NULL; 30490 } 30491 30492 /* 30493 * We want to return the bp with a failure error code, but 30494 * we do not want a call to sd_start_cmds() to occur here, 30495 * so use sd_return_failed_command_no_restart() instead of 30496 * sd_return_failed_command(). 30497 */ 30498 sd_return_failed_command_no_restart(un, bp, EIO); 30499 } 30500 30501 /* Flush the xbuf queues if required. */ 30502 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30503 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30504 } 30505 30506 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30507 "sd_failfast_flushq: exit: un:0x%p\n", un); 30508 } 30509 30510 30511 /* 30512 * Function: sd_failfast_flushq_callback 30513 * 30514 * Description: Return TRUE if the given bp meets the criteria for failfast 30515 * flushing. Used with ddi_xbuf_flushq(9F). 30516 * 30517 * Arguments: bp - ptr to buf struct to be examined. 30518 * 30519 * Context: Any 30520 */ 30521 30522 static int 30523 sd_failfast_flushq_callback(struct buf *bp) 30524 { 30525 /* 30526 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30527 * state is entered; OR (2) the given bp has B_FAILFAST set. 30528 */ 30529 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30530 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30531 } 30532 30533 30534 30535 #if defined(__i386) || defined(__amd64) 30536 /* 30537 * Function: sd_setup_next_xfer 30538 * 30539 * Description: Prepare next I/O operation using DMA_PARTIAL 30540 * 30541 */ 30542 30543 static int 30544 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30545 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30546 { 30547 ssize_t num_blks_not_xfered; 30548 daddr_t strt_blk_num; 30549 ssize_t bytes_not_xfered; 30550 int rval; 30551 30552 ASSERT(pkt->pkt_resid == 0); 30553 30554 /* 30555 * Calculate next block number and amount to be transferred. 30556 * 30557 * How much data NOT transfered to the HBA yet. 30558 */ 30559 bytes_not_xfered = xp->xb_dma_resid; 30560 30561 /* 30562 * figure how many blocks NOT transfered to the HBA yet. 30563 */ 30564 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30565 30566 /* 30567 * set starting block number to the end of what WAS transfered. 30568 */ 30569 strt_blk_num = xp->xb_blkno + 30570 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30571 30572 /* 30573 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30574 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30575 * the disk mutex here. 30576 */ 30577 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30578 strt_blk_num, num_blks_not_xfered); 30579 30580 if (rval == 0) { 30581 30582 /* 30583 * Success. 30584 * 30585 * Adjust things if there are still more blocks to be 30586 * transfered. 30587 */ 30588 xp->xb_dma_resid = pkt->pkt_resid; 30589 pkt->pkt_resid = 0; 30590 30591 return (1); 30592 } 30593 30594 /* 30595 * There's really only one possible return value from 30596 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30597 * returns NULL. 30598 */ 30599 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30600 30601 bp->b_resid = bp->b_bcount; 30602 bp->b_flags |= B_ERROR; 30603 30604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30605 "Error setting up next portion of DMA transfer\n"); 30606 30607 return (0); 30608 } 30609 #endif 30610 30611 /* 30612 * Function: sd_panic_for_res_conflict 30613 * 30614 * Description: Call panic with a string formated with "Reservation Conflict" 30615 * and a human readable identifier indicating the SD instance 30616 * that experienced the reservation conflict. 30617 * 30618 * Arguments: un - pointer to the soft state struct for the instance. 30619 * 30620 * Context: may execute in interrupt context. 30621 */ 30622 30623 #define SD_RESV_CONFLICT_FMT_LEN 40 30624 void 30625 sd_panic_for_res_conflict(struct sd_lun *un) 30626 { 30627 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30628 char path_str[MAXPATHLEN]; 30629 30630 (void) snprintf(panic_str, sizeof (panic_str), 30631 "Reservation Conflict\nDisk: %s", 30632 ddi_pathname(SD_DEVINFO(un), path_str)); 30633 30634 panic(panic_str); 30635 } 30636 30637 /* 30638 * Note: The following sd_faultinjection_ioctl( ) routines implement 30639 * driver support for handling fault injection for error analysis 30640 * causing faults in multiple layers of the driver. 30641 * 30642 */ 30643 30644 #ifdef SD_FAULT_INJECTION 30645 static uint_t sd_fault_injection_on = 0; 30646 30647 /* 30648 * Function: sd_faultinjection_ioctl() 30649 * 30650 * Description: This routine is the driver entry point for handling 30651 * faultinjection ioctls to inject errors into the 30652 * layer model 30653 * 30654 * Arguments: cmd - the ioctl cmd recieved 30655 * arg - the arguments from user and returns 30656 */ 30657 30658 static void 30659 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30660 30661 uint_t i; 30662 uint_t rval; 30663 30664 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30665 30666 mutex_enter(SD_MUTEX(un)); 30667 30668 switch (cmd) { 30669 case SDIOCRUN: 30670 /* Allow pushed faults to be injected */ 30671 SD_INFO(SD_LOG_SDTEST, un, 30672 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30673 30674 sd_fault_injection_on = 1; 30675 30676 SD_INFO(SD_LOG_IOERR, un, 30677 "sd_faultinjection_ioctl: run finished\n"); 30678 break; 30679 30680 case SDIOCSTART: 30681 /* Start Injection Session */ 30682 SD_INFO(SD_LOG_SDTEST, un, 30683 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30684 30685 sd_fault_injection_on = 0; 30686 un->sd_injection_mask = 0xFFFFFFFF; 30687 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30688 un->sd_fi_fifo_pkt[i] = NULL; 30689 un->sd_fi_fifo_xb[i] = NULL; 30690 un->sd_fi_fifo_un[i] = NULL; 30691 un->sd_fi_fifo_arq[i] = NULL; 30692 } 30693 un->sd_fi_fifo_start = 0; 30694 un->sd_fi_fifo_end = 0; 30695 30696 mutex_enter(&(un->un_fi_mutex)); 30697 un->sd_fi_log[0] = '\0'; 30698 un->sd_fi_buf_len = 0; 30699 mutex_exit(&(un->un_fi_mutex)); 30700 30701 SD_INFO(SD_LOG_IOERR, un, 30702 "sd_faultinjection_ioctl: start finished\n"); 30703 break; 30704 30705 case SDIOCSTOP: 30706 /* Stop Injection Session */ 30707 SD_INFO(SD_LOG_SDTEST, un, 30708 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30709 sd_fault_injection_on = 0; 30710 un->sd_injection_mask = 0x0; 30711 30712 /* Empty stray or unuseds structs from fifo */ 30713 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30714 if (un->sd_fi_fifo_pkt[i] != NULL) { 30715 kmem_free(un->sd_fi_fifo_pkt[i], 30716 sizeof (struct sd_fi_pkt)); 30717 } 30718 if (un->sd_fi_fifo_xb[i] != NULL) { 30719 kmem_free(un->sd_fi_fifo_xb[i], 30720 sizeof (struct sd_fi_xb)); 30721 } 30722 if (un->sd_fi_fifo_un[i] != NULL) { 30723 kmem_free(un->sd_fi_fifo_un[i], 30724 sizeof (struct sd_fi_un)); 30725 } 30726 if (un->sd_fi_fifo_arq[i] != NULL) { 30727 kmem_free(un->sd_fi_fifo_arq[i], 30728 sizeof (struct sd_fi_arq)); 30729 } 30730 un->sd_fi_fifo_pkt[i] = NULL; 30731 un->sd_fi_fifo_un[i] = NULL; 30732 un->sd_fi_fifo_xb[i] = NULL; 30733 un->sd_fi_fifo_arq[i] = NULL; 30734 } 30735 un->sd_fi_fifo_start = 0; 30736 un->sd_fi_fifo_end = 0; 30737 30738 SD_INFO(SD_LOG_IOERR, un, 30739 "sd_faultinjection_ioctl: stop finished\n"); 30740 break; 30741 30742 case SDIOCINSERTPKT: 30743 /* Store a packet struct to be pushed onto fifo */ 30744 SD_INFO(SD_LOG_SDTEST, un, 30745 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30746 30747 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30748 30749 sd_fault_injection_on = 0; 30750 30751 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30752 if (un->sd_fi_fifo_pkt[i] != NULL) { 30753 kmem_free(un->sd_fi_fifo_pkt[i], 30754 sizeof (struct sd_fi_pkt)); 30755 } 30756 if (arg != NULL) { 30757 un->sd_fi_fifo_pkt[i] = 30758 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30759 if (un->sd_fi_fifo_pkt[i] == NULL) { 30760 /* Alloc failed don't store anything */ 30761 break; 30762 } 30763 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30764 sizeof (struct sd_fi_pkt), 0); 30765 if (rval == -1) { 30766 kmem_free(un->sd_fi_fifo_pkt[i], 30767 sizeof (struct sd_fi_pkt)); 30768 un->sd_fi_fifo_pkt[i] = NULL; 30769 } 30770 } else { 30771 SD_INFO(SD_LOG_IOERR, un, 30772 "sd_faultinjection_ioctl: pkt null\n"); 30773 } 30774 break; 30775 30776 case SDIOCINSERTXB: 30777 /* Store a xb struct to be pushed onto fifo */ 30778 SD_INFO(SD_LOG_SDTEST, un, 30779 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30780 30781 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30782 30783 sd_fault_injection_on = 0; 30784 30785 if (un->sd_fi_fifo_xb[i] != NULL) { 30786 kmem_free(un->sd_fi_fifo_xb[i], 30787 sizeof (struct sd_fi_xb)); 30788 un->sd_fi_fifo_xb[i] = NULL; 30789 } 30790 if (arg != NULL) { 30791 un->sd_fi_fifo_xb[i] = 30792 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30793 if (un->sd_fi_fifo_xb[i] == NULL) { 30794 /* Alloc failed don't store anything */ 30795 break; 30796 } 30797 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30798 sizeof (struct sd_fi_xb), 0); 30799 30800 if (rval == -1) { 30801 kmem_free(un->sd_fi_fifo_xb[i], 30802 sizeof (struct sd_fi_xb)); 30803 un->sd_fi_fifo_xb[i] = NULL; 30804 } 30805 } else { 30806 SD_INFO(SD_LOG_IOERR, un, 30807 "sd_faultinjection_ioctl: xb null\n"); 30808 } 30809 break; 30810 30811 case SDIOCINSERTUN: 30812 /* Store a un struct to be pushed onto fifo */ 30813 SD_INFO(SD_LOG_SDTEST, un, 30814 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30815 30816 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30817 30818 sd_fault_injection_on = 0; 30819 30820 if (un->sd_fi_fifo_un[i] != NULL) { 30821 kmem_free(un->sd_fi_fifo_un[i], 30822 sizeof (struct sd_fi_un)); 30823 un->sd_fi_fifo_un[i] = NULL; 30824 } 30825 if (arg != NULL) { 30826 un->sd_fi_fifo_un[i] = 30827 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30828 if (un->sd_fi_fifo_un[i] == NULL) { 30829 /* Alloc failed don't store anything */ 30830 break; 30831 } 30832 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30833 sizeof (struct sd_fi_un), 0); 30834 if (rval == -1) { 30835 kmem_free(un->sd_fi_fifo_un[i], 30836 sizeof (struct sd_fi_un)); 30837 un->sd_fi_fifo_un[i] = NULL; 30838 } 30839 30840 } else { 30841 SD_INFO(SD_LOG_IOERR, un, 30842 "sd_faultinjection_ioctl: un null\n"); 30843 } 30844 30845 break; 30846 30847 case SDIOCINSERTARQ: 30848 /* Store a arq struct to be pushed onto fifo */ 30849 SD_INFO(SD_LOG_SDTEST, un, 30850 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30851 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30852 30853 sd_fault_injection_on = 0; 30854 30855 if (un->sd_fi_fifo_arq[i] != NULL) { 30856 kmem_free(un->sd_fi_fifo_arq[i], 30857 sizeof (struct sd_fi_arq)); 30858 un->sd_fi_fifo_arq[i] = NULL; 30859 } 30860 if (arg != NULL) { 30861 un->sd_fi_fifo_arq[i] = 30862 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30863 if (un->sd_fi_fifo_arq[i] == NULL) { 30864 /* Alloc failed don't store anything */ 30865 break; 30866 } 30867 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30868 sizeof (struct sd_fi_arq), 0); 30869 if (rval == -1) { 30870 kmem_free(un->sd_fi_fifo_arq[i], 30871 sizeof (struct sd_fi_arq)); 30872 un->sd_fi_fifo_arq[i] = NULL; 30873 } 30874 30875 } else { 30876 SD_INFO(SD_LOG_IOERR, un, 30877 "sd_faultinjection_ioctl: arq null\n"); 30878 } 30879 30880 break; 30881 30882 case SDIOCPUSH: 30883 /* Push stored xb, pkt, un, and arq onto fifo */ 30884 sd_fault_injection_on = 0; 30885 30886 if (arg != NULL) { 30887 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30888 if (rval != -1 && 30889 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30890 un->sd_fi_fifo_end += i; 30891 } 30892 } else { 30893 SD_INFO(SD_LOG_IOERR, un, 30894 "sd_faultinjection_ioctl: push arg null\n"); 30895 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30896 un->sd_fi_fifo_end++; 30897 } 30898 } 30899 SD_INFO(SD_LOG_IOERR, un, 30900 "sd_faultinjection_ioctl: push to end=%d\n", 30901 un->sd_fi_fifo_end); 30902 break; 30903 30904 case SDIOCRETRIEVE: 30905 /* Return buffer of log from Injection session */ 30906 SD_INFO(SD_LOG_SDTEST, un, 30907 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30908 30909 sd_fault_injection_on = 0; 30910 30911 mutex_enter(&(un->un_fi_mutex)); 30912 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30913 un->sd_fi_buf_len+1, 0); 30914 mutex_exit(&(un->un_fi_mutex)); 30915 30916 if (rval == -1) { 30917 /* 30918 * arg is possibly invalid setting 30919 * it to NULL for return 30920 */ 30921 arg = NULL; 30922 } 30923 break; 30924 } 30925 30926 mutex_exit(SD_MUTEX(un)); 30927 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30928 " exit\n"); 30929 } 30930 30931 30932 /* 30933 * Function: sd_injection_log() 30934 * 30935 * Description: This routine adds buff to the already existing injection log 30936 * for retrieval via faultinjection_ioctl for use in fault 30937 * detection and recovery 30938 * 30939 * Arguments: buf - the string to add to the log 30940 */ 30941 30942 static void 30943 sd_injection_log(char *buf, struct sd_lun *un) 30944 { 30945 uint_t len; 30946 30947 ASSERT(un != NULL); 30948 ASSERT(buf != NULL); 30949 30950 mutex_enter(&(un->un_fi_mutex)); 30951 30952 len = min(strlen(buf), 255); 30953 /* Add logged value to Injection log to be returned later */ 30954 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30955 uint_t offset = strlen((char *)un->sd_fi_log); 30956 char *destp = (char *)un->sd_fi_log + offset; 30957 int i; 30958 for (i = 0; i < len; i++) { 30959 *destp++ = *buf++; 30960 } 30961 un->sd_fi_buf_len += len; 30962 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30963 } 30964 30965 mutex_exit(&(un->un_fi_mutex)); 30966 } 30967 30968 30969 /* 30970 * Function: sd_faultinjection() 30971 * 30972 * Description: This routine takes the pkt and changes its 30973 * content based on error injection scenerio. 30974 * 30975 * Arguments: pktp - packet to be changed 30976 */ 30977 30978 static void 30979 sd_faultinjection(struct scsi_pkt *pktp) 30980 { 30981 uint_t i; 30982 struct sd_fi_pkt *fi_pkt; 30983 struct sd_fi_xb *fi_xb; 30984 struct sd_fi_un *fi_un; 30985 struct sd_fi_arq *fi_arq; 30986 struct buf *bp; 30987 struct sd_xbuf *xb; 30988 struct sd_lun *un; 30989 30990 ASSERT(pktp != NULL); 30991 30992 /* pull bp xb and un from pktp */ 30993 bp = (struct buf *)pktp->pkt_private; 30994 xb = SD_GET_XBUF(bp); 30995 un = SD_GET_UN(bp); 30996 30997 ASSERT(un != NULL); 30998 30999 mutex_enter(SD_MUTEX(un)); 31000 31001 SD_TRACE(SD_LOG_SDTEST, un, 31002 "sd_faultinjection: entry Injection from sdintr\n"); 31003 31004 /* if injection is off return */ 31005 if (sd_fault_injection_on == 0 || 31006 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 31007 mutex_exit(SD_MUTEX(un)); 31008 return; 31009 } 31010 31011 31012 /* take next set off fifo */ 31013 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 31014 31015 fi_pkt = un->sd_fi_fifo_pkt[i]; 31016 fi_xb = un->sd_fi_fifo_xb[i]; 31017 fi_un = un->sd_fi_fifo_un[i]; 31018 fi_arq = un->sd_fi_fifo_arq[i]; 31019 31020 31021 /* set variables accordingly */ 31022 /* set pkt if it was on fifo */ 31023 if (fi_pkt != NULL) { 31024 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 31025 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 31026 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 31027 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 31028 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 31029 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 31030 31031 } 31032 31033 /* set xb if it was on fifo */ 31034 if (fi_xb != NULL) { 31035 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 31036 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 31037 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 31038 SD_CONDSET(xb, xb, xb_victim_retry_count, 31039 "xb_victim_retry_count"); 31040 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 31041 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 31042 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 31043 31044 /* copy in block data from sense */ 31045 if (fi_xb->xb_sense_data[0] != -1) { 31046 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 31047 SENSE_LENGTH); 31048 } 31049 31050 /* copy in extended sense codes */ 31051 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 31052 "es_code"); 31053 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 31054 "es_key"); 31055 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 31056 "es_add_code"); 31057 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 31058 es_qual_code, "es_qual_code"); 31059 } 31060 31061 /* set un if it was on fifo */ 31062 if (fi_un != NULL) { 31063 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 31064 SD_CONDSET(un, un, un_ctype, "un_ctype"); 31065 SD_CONDSET(un, un, un_reset_retry_count, 31066 "un_reset_retry_count"); 31067 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 31068 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 31069 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 31070 SD_CONDSET(un, un, un_f_geometry_is_valid, 31071 "un_f_geometry_is_valid"); 31072 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 31073 "un_f_allow_bus_device_reset"); 31074 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 31075 31076 } 31077 31078 /* copy in auto request sense if it was on fifo */ 31079 if (fi_arq != NULL) { 31080 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 31081 } 31082 31083 /* free structs */ 31084 if (un->sd_fi_fifo_pkt[i] != NULL) { 31085 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 31086 } 31087 if (un->sd_fi_fifo_xb[i] != NULL) { 31088 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 31089 } 31090 if (un->sd_fi_fifo_un[i] != NULL) { 31091 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 31092 } 31093 if (un->sd_fi_fifo_arq[i] != NULL) { 31094 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 31095 } 31096 31097 /* 31098 * kmem_free does not gurantee to set to NULL 31099 * since we uses these to determine if we set 31100 * values or not lets confirm they are always 31101 * NULL after free 31102 */ 31103 un->sd_fi_fifo_pkt[i] = NULL; 31104 un->sd_fi_fifo_un[i] = NULL; 31105 un->sd_fi_fifo_xb[i] = NULL; 31106 un->sd_fi_fifo_arq[i] = NULL; 31107 31108 un->sd_fi_fifo_start++; 31109 31110 mutex_exit(SD_MUTEX(un)); 31111 31112 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 31113 } 31114 31115 #endif /* SD_FAULT_INJECTION */ 31116 31117 /* 31118 * This routine is invoked in sd_unit_attach(). Before calling it, the 31119 * properties in conf file should be processed already, and "hotpluggable" 31120 * property was processed also. 31121 * 31122 * The sd driver distinguishes 3 different type of devices: removable media, 31123 * non-removable media, and hotpluggable. Below the differences are defined: 31124 * 31125 * 1. Device ID 31126 * 31127 * The device ID of a device is used to identify this device. Refer to 31128 * ddi_devid_register(9F). 31129 * 31130 * For a non-removable media disk device which can provide 0x80 or 0x83 31131 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 31132 * device ID is created to identify this device. For other non-removable 31133 * media devices, a default device ID is created only if this device has 31134 * at least 2 alter cylinders. Otherwise, this device has no devid. 31135 * 31136 * ------------------------------------------------------- 31137 * removable media hotpluggable | Can Have Device ID 31138 * ------------------------------------------------------- 31139 * false false | Yes 31140 * false true | Yes 31141 * true x | No 31142 * ------------------------------------------------------ 31143 * 31144 * 31145 * 2. SCSI group 4 commands 31146 * 31147 * In SCSI specs, only some commands in group 4 command set can use 31148 * 8-byte addresses that can be used to access >2TB storage spaces. 31149 * Other commands have no such capability. Without supporting group4, 31150 * it is impossible to make full use of storage spaces of a disk with 31151 * capacity larger than 2TB. 31152 * 31153 * ----------------------------------------------- 31154 * removable media hotpluggable LP64 | Group 31155 * ----------------------------------------------- 31156 * false false false | 1 31157 * false false true | 4 31158 * false true false | 1 31159 * false true true | 4 31160 * true x x | 5 31161 * ----------------------------------------------- 31162 * 31163 * 31164 * 3. Check for VTOC Label 31165 * 31166 * If a direct-access disk has no EFI label, sd will check if it has a 31167 * valid VTOC label. Now, sd also does that check for removable media 31168 * and hotpluggable devices. 31169 * 31170 * -------------------------------------------------------------- 31171 * Direct-Access removable media hotpluggable | Check Label 31172 * ------------------------------------------------------------- 31173 * false false false | No 31174 * false false true | No 31175 * false true false | Yes 31176 * false true true | Yes 31177 * true x x | Yes 31178 * -------------------------------------------------------------- 31179 * 31180 * 31181 * 4. Building default VTOC label 31182 * 31183 * As section 3 says, sd checks if some kinds of devices have VTOC label. 31184 * If those devices have no valid VTOC label, sd(7d) will attempt to 31185 * create default VTOC for them. Currently sd creates default VTOC label 31186 * for all devices on x86 platform (VTOC_16), but only for removable 31187 * media devices on SPARC (VTOC_8). 31188 * 31189 * ----------------------------------------------------------- 31190 * removable media hotpluggable platform | Default Label 31191 * ----------------------------------------------------------- 31192 * false false sparc | No 31193 * false true x86 | Yes 31194 * false true sparc | Yes 31195 * true x x | Yes 31196 * ---------------------------------------------------------- 31197 * 31198 * 31199 * 5. Supported blocksizes of target devices 31200 * 31201 * Sd supports non-512-byte blocksize for removable media devices only. 31202 * For other devices, only 512-byte blocksize is supported. This may be 31203 * changed in near future because some RAID devices require non-512-byte 31204 * blocksize 31205 * 31206 * ----------------------------------------------------------- 31207 * removable media hotpluggable | non-512-byte blocksize 31208 * ----------------------------------------------------------- 31209 * false false | No 31210 * false true | No 31211 * true x | Yes 31212 * ----------------------------------------------------------- 31213 * 31214 * 31215 * 6. Automatic mount & unmount (i.e. vold) 31216 * 31217 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 31218 * if a device is removable media device. It return 1 for removable media 31219 * devices, and 0 for others. 31220 * 31221 * Vold treats a device as removable one only if DKIOREMOVABLE returns 1. 31222 * And it does automounting only for removable media devices. In order to 31223 * preserve users' experience and let vold continue to do automounting for 31224 * USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk 31225 * devices. 31226 * 31227 * ------------------------------------------------------ 31228 * removable media hotpluggable | automatic mount 31229 * ------------------------------------------------------ 31230 * false false | No 31231 * false true | Yes 31232 * true x | Yes 31233 * ------------------------------------------------------ 31234 * 31235 * 31236 * 7. fdisk partition management 31237 * 31238 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 31239 * just supports fdisk partitions on x86 platform. On sparc platform, sd 31240 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 31241 * fdisk partitions on both x86 and SPARC platform. 31242 * 31243 * ----------------------------------------------------------- 31244 * platform removable media USB/1394 | fdisk supported 31245 * ----------------------------------------------------------- 31246 * x86 X X | true 31247 * ------------------------------------------------------------ 31248 * sparc X X | false 31249 * ------------------------------------------------------------ 31250 * 31251 * 31252 * 8. MBOOT/MBR 31253 * 31254 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 31255 * read/write mboot for removable media devices on sparc platform. 31256 * 31257 * ----------------------------------------------------------- 31258 * platform removable media USB/1394 | mboot supported 31259 * ----------------------------------------------------------- 31260 * x86 X X | true 31261 * ------------------------------------------------------------ 31262 * sparc false false | false 31263 * sparc false true | true 31264 * sparc true false | true 31265 * sparc true true | true 31266 * ------------------------------------------------------------ 31267 * 31268 * 31269 * 9. error handling during opening device 31270 * 31271 * If failed to open a disk device, an errno is returned. For some kinds 31272 * of errors, different errno is returned depending on if this device is 31273 * a removable media device. This brings USB/1394 hard disks in line with 31274 * expected hard disk behavior. It is not expected that this breaks any 31275 * application. 31276 * 31277 * ------------------------------------------------------ 31278 * removable media hotpluggable | errno 31279 * ------------------------------------------------------ 31280 * false false | EIO 31281 * false true | EIO 31282 * true x | ENXIO 31283 * ------------------------------------------------------ 31284 * 31285 * 31286 * 11. ioctls: DKIOCEJECT, CDROMEJECT 31287 * 31288 * These IOCTLs are applicable only to removable media devices. 31289 * 31290 * ----------------------------------------------------------- 31291 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 31292 * ----------------------------------------------------------- 31293 * false false | No 31294 * false true | No 31295 * true x | Yes 31296 * ----------------------------------------------------------- 31297 * 31298 * 31299 * 12. Kstats for partitions 31300 * 31301 * sd creates partition kstat for non-removable media devices. USB and 31302 * Firewire hard disks now have partition kstats 31303 * 31304 * ------------------------------------------------------ 31305 * removable media hotplugable | kstat 31306 * ------------------------------------------------------ 31307 * false false | Yes 31308 * false true | Yes 31309 * true x | No 31310 * ------------------------------------------------------ 31311 * 31312 * 31313 * 13. Removable media & hotpluggable properties 31314 * 31315 * Sd driver creates a "removable-media" property for removable media 31316 * devices. Parent nexus drivers create a "hotpluggable" property if 31317 * it supports hotplugging. 31318 * 31319 * --------------------------------------------------------------------- 31320 * removable media hotpluggable | "removable-media" " hotpluggable" 31321 * --------------------------------------------------------------------- 31322 * false false | No No 31323 * false true | No Yes 31324 * true false | Yes No 31325 * true true | Yes Yes 31326 * --------------------------------------------------------------------- 31327 * 31328 * 31329 * 14. Power Management 31330 * 31331 * sd only power manages removable media devices or devices that support 31332 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 31333 * 31334 * A parent nexus that supports hotplugging can also set "pm-capable" 31335 * if the disk can be power managed. 31336 * 31337 * ------------------------------------------------------------ 31338 * removable media hotpluggable pm-capable | power manage 31339 * ------------------------------------------------------------ 31340 * false false false | No 31341 * false false true | Yes 31342 * false true false | No 31343 * false true true | Yes 31344 * true x x | Yes 31345 * ------------------------------------------------------------ 31346 * 31347 * USB and firewire hard disks can now be power managed independently 31348 * of the framebuffer 31349 * 31350 * 31351 * 15. Support for USB disks with capacity larger than 1TB 31352 * 31353 * Currently, sd doesn't permit a fixed disk device with capacity 31354 * larger than 1TB to be used in a 32-bit operating system environment. 31355 * However, sd doesn't do that for removable media devices. Instead, it 31356 * assumes that removable media devices cannot have a capacity larger 31357 * than 1TB. Therefore, using those devices on 32-bit system is partially 31358 * supported, which can cause some unexpected results. 31359 * 31360 * --------------------------------------------------------------------- 31361 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 31362 * --------------------------------------------------------------------- 31363 * false false | true | no 31364 * false true | true | no 31365 * true false | true | Yes 31366 * true true | true | Yes 31367 * --------------------------------------------------------------------- 31368 * 31369 * 31370 * 16. Check write-protection at open time 31371 * 31372 * When a removable media device is being opened for writing without NDELAY 31373 * flag, sd will check if this device is writable. If attempting to open 31374 * without NDELAY flag a write-protected device, this operation will abort. 31375 * 31376 * ------------------------------------------------------------ 31377 * removable media USB/1394 | WP Check 31378 * ------------------------------------------------------------ 31379 * false false | No 31380 * false true | No 31381 * true false | Yes 31382 * true true | Yes 31383 * ------------------------------------------------------------ 31384 * 31385 * 31386 * 17. syslog when corrupted VTOC is encountered 31387 * 31388 * Currently, if an invalid VTOC is encountered, sd only print syslog 31389 * for fixed SCSI disks. 31390 * ------------------------------------------------------------ 31391 * removable media USB/1394 | print syslog 31392 * ------------------------------------------------------------ 31393 * false false | Yes 31394 * false true | No 31395 * true false | No 31396 * true true | No 31397 * ------------------------------------------------------------ 31398 */ 31399 static void 31400 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 31401 { 31402 int pm_capable_prop; 31403 31404 ASSERT(un->un_sd); 31405 ASSERT(un->un_sd->sd_inq); 31406 31407 #if defined(_SUNOS_VTOC_16) 31408 /* 31409 * For VTOC_16 devices, the default label will be created for all 31410 * devices. (see sd_build_default_label) 31411 */ 31412 un->un_f_default_vtoc_supported = TRUE; 31413 #endif 31414 31415 if (un->un_sd->sd_inq->inq_rmb) { 31416 /* 31417 * The media of this device is removable. And for this kind 31418 * of devices, it is possible to change medium after openning 31419 * devices. Thus we should support this operation. 31420 */ 31421 un->un_f_has_removable_media = TRUE; 31422 31423 #if defined(_SUNOS_VTOC_8) 31424 /* 31425 * Note: currently, for VTOC_8 devices, default label is 31426 * created for removable and hotpluggable devices only. 31427 */ 31428 un->un_f_default_vtoc_supported = TRUE; 31429 #endif 31430 /* 31431 * support non-512-byte blocksize of removable media devices 31432 */ 31433 un->un_f_non_devbsize_supported = TRUE; 31434 31435 /* 31436 * Assume that all removable media devices support DOOR_LOCK 31437 */ 31438 un->un_f_doorlock_supported = TRUE; 31439 31440 /* 31441 * For a removable media device, it is possible to be opened 31442 * with NDELAY flag when there is no media in drive, in this 31443 * case we don't care if device is writable. But if without 31444 * NDELAY flag, we need to check if media is write-protected. 31445 */ 31446 un->un_f_chk_wp_open = TRUE; 31447 31448 /* 31449 * need to start a SCSI watch thread to monitor media state, 31450 * when media is being inserted or ejected, notify syseventd. 31451 */ 31452 un->un_f_monitor_media_state = TRUE; 31453 31454 /* 31455 * Some devices don't support START_STOP_UNIT command. 31456 * Therefore, we'd better check if a device supports it 31457 * before sending it. 31458 */ 31459 un->un_f_check_start_stop = TRUE; 31460 31461 /* 31462 * support eject media ioctl: 31463 * FDEJECT, DKIOCEJECT, CDROMEJECT 31464 */ 31465 un->un_f_eject_media_supported = TRUE; 31466 31467 /* 31468 * Because many removable-media devices don't support 31469 * LOG_SENSE, we couldn't use this command to check if 31470 * a removable media device support power-management. 31471 * We assume that they support power-management via 31472 * START_STOP_UNIT command and can be spun up and down 31473 * without limitations. 31474 */ 31475 un->un_f_pm_supported = TRUE; 31476 31477 /* 31478 * Need to create a zero length (Boolean) property 31479 * removable-media for the removable media devices. 31480 * Note that the return value of the property is not being 31481 * checked, since if unable to create the property 31482 * then do not want the attach to fail altogether. Consistent 31483 * with other property creation in attach. 31484 */ 31485 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31486 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31487 31488 } else { 31489 /* 31490 * create device ID for device 31491 */ 31492 un->un_f_devid_supported = TRUE; 31493 31494 /* 31495 * Spin up non-removable-media devices once it is attached 31496 */ 31497 un->un_f_attach_spinup = TRUE; 31498 31499 /* 31500 * According to SCSI specification, Sense data has two kinds of 31501 * format: fixed format, and descriptor format. At present, we 31502 * don't support descriptor format sense data for removable 31503 * media. 31504 */ 31505 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31506 un->un_f_descr_format_supported = TRUE; 31507 } 31508 31509 /* 31510 * kstats are created only for non-removable media devices. 31511 * 31512 * Set this in sd.conf to 0 in order to disable kstats. The 31513 * default is 1, so they are enabled by default. 31514 */ 31515 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31516 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31517 "enable-partition-kstats", 1)); 31518 31519 /* 31520 * Check if HBA has set the "pm-capable" property. 31521 * If "pm-capable" exists and is non-zero then we can 31522 * power manage the device without checking the start/stop 31523 * cycle count log sense page. 31524 * 31525 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31526 * then we should not power manage the device. 31527 * 31528 * If "pm-capable" doesn't exist then pm_capable_prop will 31529 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31530 * sd will check the start/stop cycle count log sense page 31531 * and power manage the device if the cycle count limit has 31532 * not been exceeded. 31533 */ 31534 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31535 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31536 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31537 un->un_f_log_sense_supported = TRUE; 31538 } else { 31539 /* 31540 * pm-capable property exists. 31541 * 31542 * Convert "TRUE" values for pm_capable_prop to 31543 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31544 * later. "TRUE" values are any values except 31545 * SD_PM_CAPABLE_FALSE (0) and 31546 * SD_PM_CAPABLE_UNDEFINED (-1) 31547 */ 31548 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31549 un->un_f_log_sense_supported = FALSE; 31550 } else { 31551 un->un_f_pm_supported = TRUE; 31552 } 31553 31554 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31555 "sd_unit_attach: un:0x%p pm-capable " 31556 "property set to %d.\n", un, un->un_f_pm_supported); 31557 } 31558 } 31559 31560 if (un->un_f_is_hotpluggable) { 31561 #if defined(_SUNOS_VTOC_8) 31562 /* 31563 * Note: currently, for VTOC_8 devices, default label is 31564 * created for removable and hotpluggable devices only. 31565 */ 31566 un->un_f_default_vtoc_supported = TRUE; 31567 #endif 31568 31569 /* 31570 * Temporarily, let hotpluggable devices pretend to be 31571 * removable-media devices for vold. 31572 */ 31573 un->un_f_monitor_media_state = TRUE; 31574 31575 un->un_f_check_start_stop = TRUE; 31576 31577 } 31578 31579 /* 31580 * By default, only DIRECT ACCESS devices and CDs will have Sun 31581 * labels. 31582 */ 31583 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31584 (un->un_sd->sd_inq->inq_rmb)) { 31585 /* 31586 * Direct access devices have disk label 31587 */ 31588 un->un_f_vtoc_label_supported = TRUE; 31589 } 31590 31591 /* 31592 * Fdisk partitions are supported for all direct access devices on 31593 * x86 platform, and just for removable media and hotpluggable 31594 * devices on SPARC platform. Later, we will set the following flag 31595 * to FALSE if current device is not removable media or hotpluggable 31596 * device and if sd works on SAPRC platform. 31597 */ 31598 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31599 un->un_f_mboot_supported = TRUE; 31600 } 31601 31602 if (!un->un_f_is_hotpluggable && 31603 !un->un_sd->sd_inq->inq_rmb) { 31604 31605 #if defined(_SUNOS_VTOC_8) 31606 /* 31607 * Don't support fdisk on fixed disk 31608 */ 31609 un->un_f_mboot_supported = FALSE; 31610 #endif 31611 31612 /* 31613 * Fixed disk support SYNC CACHE 31614 */ 31615 un->un_f_sync_cache_supported = TRUE; 31616 31617 /* 31618 * For fixed disk, if its VTOC is not valid, we will write 31619 * errlog into system log 31620 */ 31621 if (un->un_f_vtoc_label_supported) 31622 un->un_f_vtoc_errlog_supported = TRUE; 31623 } 31624 } 31625