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 /* 4661 * For VTOC labeled disk, create and set the partition stats 4662 * at attach time, update the stats according to dynamic 4663 * partition changes during running time. 4664 */ 4665 if (label_error == 0 && un->un_f_pkstats_enabled) { 4666 sd_set_pstats(un); 4667 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4668 "un:0x%p pstats created and set, or updated\n", un); 4669 } 4670 4671 return (label_error); 4672 } 4673 4674 4675 #if defined(_SUNOS_VTOC_16) 4676 /* 4677 * Macro: MAX_BLKS 4678 * 4679 * This macro is used for table entries where we need to have the largest 4680 * possible sector value for that head & SPT (sectors per track) 4681 * combination. Other entries for some smaller disk sizes are set by 4682 * convention to match those used by X86 BIOS usage. 4683 */ 4684 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4685 4686 /* 4687 * Function: sd_convert_geometry 4688 * 4689 * Description: Convert physical geometry into a dk_geom structure. In 4690 * other words, make sure we don't wrap 16-bit values. 4691 * e.g. converting from geom_cache to dk_geom 4692 * 4693 * Context: Kernel thread only 4694 */ 4695 static void 4696 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4697 { 4698 int i; 4699 static const struct chs_values { 4700 uint_t max_cap; /* Max Capacity for this HS. */ 4701 uint_t nhead; /* Heads to use. */ 4702 uint_t nsect; /* SPT to use. */ 4703 } CHS_values[] = { 4704 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4705 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4706 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4707 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4708 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4709 }; 4710 4711 /* Unlabeled SCSI floppy device */ 4712 if (capacity <= 0x1000) { 4713 un_g->dkg_nhead = 2; 4714 un_g->dkg_ncyl = 80; 4715 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4716 return; 4717 } 4718 4719 /* 4720 * For all devices we calculate cylinders using the 4721 * heads and sectors we assign based on capacity of the 4722 * device. The table is designed to be compatible with the 4723 * way other operating systems lay out fdisk tables for X86 4724 * and to insure that the cylinders never exceed 65535 to 4725 * prevent problems with X86 ioctls that report geometry. 4726 * We use SPT that are multiples of 63, since other OSes that 4727 * are not limited to 16-bits for cylinders stop at 63 SPT 4728 * we make do by using multiples of 63 SPT. 4729 * 4730 * Note than capacities greater than or equal to 1TB will simply 4731 * get the largest geometry from the table. This should be okay 4732 * since disks this large shouldn't be using CHS values anyway. 4733 */ 4734 for (i = 0; CHS_values[i].max_cap < capacity && 4735 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4736 ; 4737 4738 un_g->dkg_nhead = CHS_values[i].nhead; 4739 un_g->dkg_nsect = CHS_values[i].nsect; 4740 } 4741 #endif 4742 4743 4744 /* 4745 * Function: sd_resync_geom_caches 4746 * 4747 * Description: (Re)initialize both geometry caches: the virtual geometry 4748 * information is extracted from the HBA (the "geometry" 4749 * capability), and the physical geometry cache data is 4750 * generated by issuing MODE SENSE commands. 4751 * 4752 * Arguments: un - driver soft state (unit) structure 4753 * capacity - disk capacity in #blocks 4754 * lbasize - disk block size in bytes 4755 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4756 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4757 * to use the USCSI "direct" chain and bypass the normal 4758 * command waitq. 4759 * 4760 * Context: Kernel thread only (can sleep). 4761 */ 4762 4763 static void 4764 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4765 int path_flag) 4766 { 4767 struct geom_cache pgeom; 4768 struct geom_cache *pgeom_p = &pgeom; 4769 int spc; 4770 unsigned short nhead; 4771 unsigned short nsect; 4772 4773 ASSERT(un != NULL); 4774 ASSERT(mutex_owned(SD_MUTEX(un))); 4775 4776 /* 4777 * Ask the controller for its logical geometry. 4778 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4779 * then the lgeom cache will be invalid. 4780 */ 4781 sd_get_virtual_geometry(un, capacity, lbasize); 4782 4783 /* 4784 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4785 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4786 */ 4787 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4788 /* 4789 * Note: Perhaps this needs to be more adaptive? The rationale 4790 * is that, if there's no HBA geometry from the HBA driver, any 4791 * guess is good, since this is the physical geometry. If MODE 4792 * SENSE fails this gives a max cylinder size for non-LBA access 4793 */ 4794 nhead = 255; 4795 nsect = 63; 4796 } else { 4797 nhead = un->un_lgeom.g_nhead; 4798 nsect = un->un_lgeom.g_nsect; 4799 } 4800 4801 if (ISCD(un)) { 4802 pgeom_p->g_nhead = 1; 4803 pgeom_p->g_nsect = nsect * nhead; 4804 } else { 4805 pgeom_p->g_nhead = nhead; 4806 pgeom_p->g_nsect = nsect; 4807 } 4808 4809 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4810 pgeom_p->g_capacity = capacity; 4811 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4812 pgeom_p->g_acyl = 0; 4813 4814 /* 4815 * Retrieve fresh geometry data from the hardware, stash it 4816 * here temporarily before we rebuild the incore label. 4817 * 4818 * We want to use the MODE SENSE commands to derive the 4819 * physical geometry of the device, but if either command 4820 * fails, the logical geometry is used as the fallback for 4821 * disk label geometry. 4822 */ 4823 mutex_exit(SD_MUTEX(un)); 4824 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4825 mutex_enter(SD_MUTEX(un)); 4826 4827 /* 4828 * Now update the real copy while holding the mutex. This 4829 * way the global copy is never in an inconsistent state. 4830 */ 4831 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4832 4833 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4834 "(cached from lgeom)\n"); 4835 SD_INFO(SD_LOG_COMMON, un, 4836 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4837 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4838 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4839 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4840 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4841 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4842 un->un_pgeom.g_rpm); 4843 } 4844 4845 4846 /* 4847 * Function: sd_read_fdisk 4848 * 4849 * Description: utility routine to read the fdisk table. 4850 * 4851 * Arguments: un - driver soft state (unit) structure 4852 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4853 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4854 * to use the USCSI "direct" chain and bypass the normal 4855 * command waitq. 4856 * 4857 * Return Code: SD_CMD_SUCCESS 4858 * SD_CMD_FAILURE 4859 * 4860 * Context: Kernel thread only (can sleep). 4861 */ 4862 /* ARGSUSED */ 4863 static int 4864 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4865 { 4866 #if defined(_NO_FDISK_PRESENT) 4867 4868 un->un_solaris_offset = 0; 4869 un->un_solaris_size = capacity; 4870 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4871 return (SD_CMD_SUCCESS); 4872 4873 #elif defined(_FIRMWARE_NEEDS_FDISK) 4874 4875 struct ipart *fdp; 4876 struct mboot *mbp; 4877 struct ipart fdisk[FD_NUMPART]; 4878 int i; 4879 char sigbuf[2]; 4880 caddr_t bufp; 4881 int uidx; 4882 int rval; 4883 int lba = 0; 4884 uint_t solaris_offset; /* offset to solaris part. */ 4885 daddr_t solaris_size; /* size of solaris partition */ 4886 uint32_t blocksize; 4887 4888 ASSERT(un != NULL); 4889 ASSERT(mutex_owned(SD_MUTEX(un))); 4890 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4891 4892 blocksize = un->un_tgt_blocksize; 4893 4894 /* 4895 * Start off assuming no fdisk table 4896 */ 4897 solaris_offset = 0; 4898 solaris_size = capacity; 4899 4900 mutex_exit(SD_MUTEX(un)); 4901 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4902 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4903 mutex_enter(SD_MUTEX(un)); 4904 4905 if (rval != 0) { 4906 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4907 "sd_read_fdisk: fdisk read err\n"); 4908 kmem_free(bufp, blocksize); 4909 return (SD_CMD_FAILURE); 4910 } 4911 4912 mbp = (struct mboot *)bufp; 4913 4914 /* 4915 * The fdisk table does not begin on a 4-byte boundary within the 4916 * master boot record, so we copy it to an aligned structure to avoid 4917 * alignment exceptions on some processors. 4918 */ 4919 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4920 4921 /* 4922 * Check for lba support before verifying sig; sig might not be 4923 * there, say on a blank disk, but the max_chs mark may still 4924 * be present. 4925 * 4926 * Note: LBA support and BEFs are an x86-only concept but this 4927 * code should work OK on SPARC as well. 4928 */ 4929 4930 /* 4931 * First, check for lba-access-ok on root node (or prom root node) 4932 * if present there, don't need to search fdisk table. 4933 */ 4934 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4935 "lba-access-ok", 0) != 0) { 4936 /* All drives do LBA; don't search fdisk table */ 4937 lba = 1; 4938 } else { 4939 /* Okay, look for mark in fdisk table */ 4940 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4941 /* accumulate "lba" value from all partitions */ 4942 lba = (lba || sd_has_max_chs_vals(fdp)); 4943 } 4944 } 4945 4946 if (lba != 0) { 4947 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4948 4949 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4950 "lba-access-ok", 0) == 0) { 4951 /* not found; create it */ 4952 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4953 "lba-access-ok", (caddr_t)NULL, 0) != 4954 DDI_PROP_SUCCESS) { 4955 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4956 "sd_read_fdisk: Can't create lba property " 4957 "for instance %d\n", 4958 ddi_get_instance(SD_DEVINFO(un))); 4959 } 4960 } 4961 } 4962 4963 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4964 4965 /* 4966 * Endian-independent signature check 4967 */ 4968 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4969 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4970 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4971 "sd_read_fdisk: no fdisk\n"); 4972 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4973 rval = SD_CMD_SUCCESS; 4974 goto done; 4975 } 4976 4977 #ifdef SDDEBUG 4978 if (sd_level_mask & SD_LOGMASK_INFO) { 4979 fdp = fdisk; 4980 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4981 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4982 "numsect sysid bootid\n"); 4983 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4984 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4985 " %d: %8d %8d 0x%08x 0x%08x\n", 4986 i, fdp->relsect, fdp->numsect, 4987 fdp->systid, fdp->bootid); 4988 } 4989 } 4990 #endif 4991 4992 /* 4993 * Try to find the unix partition 4994 */ 4995 uidx = -1; 4996 solaris_offset = 0; 4997 solaris_size = 0; 4998 4999 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 5000 int relsect; 5001 int numsect; 5002 5003 if (fdp->numsect == 0) { 5004 un->un_fmap[i].fmap_start = 0; 5005 un->un_fmap[i].fmap_nblk = 0; 5006 continue; 5007 } 5008 5009 /* 5010 * Data in the fdisk table is little-endian. 5011 */ 5012 relsect = LE_32(fdp->relsect); 5013 numsect = LE_32(fdp->numsect); 5014 5015 un->un_fmap[i].fmap_start = relsect; 5016 un->un_fmap[i].fmap_nblk = numsect; 5017 5018 if (fdp->systid != SUNIXOS && 5019 fdp->systid != SUNIXOS2 && 5020 fdp->systid != EFI_PMBR) { 5021 continue; 5022 } 5023 5024 /* 5025 * use the last active solaris partition id found 5026 * (there should only be 1 active partition id) 5027 * 5028 * if there are no active solaris partition id 5029 * then use the first inactive solaris partition id 5030 */ 5031 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 5032 uidx = i; 5033 solaris_offset = relsect; 5034 solaris_size = numsect; 5035 } 5036 } 5037 5038 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 5039 un->un_solaris_offset, un->un_solaris_size); 5040 5041 rval = SD_CMD_SUCCESS; 5042 5043 done: 5044 5045 /* 5046 * Clear the VTOC info, only if the Solaris partition entry 5047 * has moved, changed size, been deleted, or if the size of 5048 * the partition is too small to even fit the label sector. 5049 */ 5050 if ((un->un_solaris_offset != solaris_offset) || 5051 (un->un_solaris_size != solaris_size) || 5052 solaris_size <= DK_LABEL_LOC) { 5053 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 5054 solaris_offset, solaris_size); 5055 bzero(&un->un_g, sizeof (struct dk_geom)); 5056 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5057 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5058 un->un_f_geometry_is_valid = FALSE; 5059 } 5060 un->un_solaris_offset = solaris_offset; 5061 un->un_solaris_size = solaris_size; 5062 kmem_free(bufp, blocksize); 5063 return (rval); 5064 5065 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 5066 #error "fdisk table presence undetermined for this platform." 5067 #endif /* #if defined(_NO_FDISK_PRESENT) */ 5068 } 5069 5070 5071 /* 5072 * Function: sd_get_physical_geometry 5073 * 5074 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 5075 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 5076 * target, and use this information to initialize the physical 5077 * geometry cache specified by pgeom_p. 5078 * 5079 * MODE SENSE is an optional command, so failure in this case 5080 * does not necessarily denote an error. We want to use the 5081 * MODE SENSE commands to derive the physical geometry of the 5082 * device, but if either command fails, the logical geometry is 5083 * used as the fallback for disk label geometry. 5084 * 5085 * This requires that un->un_blockcount and un->un_tgt_blocksize 5086 * have already been initialized for the current target and 5087 * that the current values be passed as args so that we don't 5088 * end up ever trying to use -1 as a valid value. This could 5089 * happen if either value is reset while we're not holding 5090 * the mutex. 5091 * 5092 * Arguments: un - driver soft state (unit) structure 5093 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5094 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5095 * to use the USCSI "direct" chain and bypass the normal 5096 * command waitq. 5097 * 5098 * Context: Kernel thread only (can sleep). 5099 */ 5100 5101 static void 5102 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 5103 int capacity, int lbasize, int path_flag) 5104 { 5105 struct mode_format *page3p; 5106 struct mode_geometry *page4p; 5107 struct mode_header *headerp; 5108 int sector_size; 5109 int nsect; 5110 int nhead; 5111 int ncyl; 5112 int intrlv; 5113 int spc; 5114 int modesense_capacity; 5115 int rpm; 5116 int bd_len; 5117 int mode_header_length; 5118 uchar_t *p3bufp; 5119 uchar_t *p4bufp; 5120 int cdbsize; 5121 5122 ASSERT(un != NULL); 5123 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 5124 5125 if (un->un_f_blockcount_is_valid != TRUE) { 5126 return; 5127 } 5128 5129 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 5130 return; 5131 } 5132 5133 if (lbasize == 0) { 5134 if (ISCD(un)) { 5135 lbasize = 2048; 5136 } else { 5137 lbasize = un->un_sys_blocksize; 5138 } 5139 } 5140 pgeom_p->g_secsize = (unsigned short)lbasize; 5141 5142 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 5143 5144 /* 5145 * Retrieve MODE SENSE page 3 - Format Device Page 5146 */ 5147 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 5148 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 5149 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 5150 != 0) { 5151 SD_ERROR(SD_LOG_COMMON, un, 5152 "sd_get_physical_geometry: mode sense page 3 failed\n"); 5153 goto page3_exit; 5154 } 5155 5156 /* 5157 * Determine size of Block Descriptors in order to locate the mode 5158 * page data. ATAPI devices return 0, SCSI devices should return 5159 * MODE_BLK_DESC_LENGTH. 5160 */ 5161 headerp = (struct mode_header *)p3bufp; 5162 if (un->un_f_cfg_is_atapi == TRUE) { 5163 struct mode_header_grp2 *mhp = 5164 (struct mode_header_grp2 *)headerp; 5165 mode_header_length = MODE_HEADER_LENGTH_GRP2; 5166 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5167 } else { 5168 mode_header_length = MODE_HEADER_LENGTH; 5169 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5170 } 5171 5172 if (bd_len > MODE_BLK_DESC_LENGTH) { 5173 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5174 "received unexpected bd_len of %d, page3\n", bd_len); 5175 goto page3_exit; 5176 } 5177 5178 page3p = (struct mode_format *) 5179 ((caddr_t)headerp + mode_header_length + bd_len); 5180 5181 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5182 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5183 "mode sense pg3 code mismatch %d\n", 5184 page3p->mode_page.code); 5185 goto page3_exit; 5186 } 5187 5188 /* 5189 * Use this physical geometry data only if BOTH MODE SENSE commands 5190 * complete successfully; otherwise, revert to the logical geometry. 5191 * So, we need to save everything in temporary variables. 5192 */ 5193 sector_size = BE_16(page3p->data_bytes_sect); 5194 5195 /* 5196 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5197 */ 5198 if (sector_size == 0) { 5199 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 5200 } else { 5201 sector_size &= ~(un->un_sys_blocksize - 1); 5202 } 5203 5204 nsect = BE_16(page3p->sect_track); 5205 intrlv = BE_16(page3p->interleave); 5206 5207 SD_INFO(SD_LOG_COMMON, un, 5208 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5209 SD_INFO(SD_LOG_COMMON, un, 5210 " mode page: %d; nsect: %d; sector size: %d;\n", 5211 page3p->mode_page.code, nsect, sector_size); 5212 SD_INFO(SD_LOG_COMMON, un, 5213 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5214 BE_16(page3p->track_skew), 5215 BE_16(page3p->cylinder_skew)); 5216 5217 5218 /* 5219 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5220 */ 5221 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5222 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 5223 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 5224 != 0) { 5225 SD_ERROR(SD_LOG_COMMON, un, 5226 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5227 goto page4_exit; 5228 } 5229 5230 /* 5231 * Determine size of Block Descriptors in order to locate the mode 5232 * page data. ATAPI devices return 0, SCSI devices should return 5233 * MODE_BLK_DESC_LENGTH. 5234 */ 5235 headerp = (struct mode_header *)p4bufp; 5236 if (un->un_f_cfg_is_atapi == TRUE) { 5237 struct mode_header_grp2 *mhp = 5238 (struct mode_header_grp2 *)headerp; 5239 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5240 } else { 5241 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5242 } 5243 5244 if (bd_len > MODE_BLK_DESC_LENGTH) { 5245 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5246 "received unexpected bd_len of %d, page4\n", bd_len); 5247 goto page4_exit; 5248 } 5249 5250 page4p = (struct mode_geometry *) 5251 ((caddr_t)headerp + mode_header_length + bd_len); 5252 5253 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5254 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5255 "mode sense pg4 code mismatch %d\n", 5256 page4p->mode_page.code); 5257 goto page4_exit; 5258 } 5259 5260 /* 5261 * Stash the data now, after we know that both commands completed. 5262 */ 5263 5264 mutex_enter(SD_MUTEX(un)); 5265 5266 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5267 spc = nhead * nsect; 5268 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5269 rpm = BE_16(page4p->rpm); 5270 5271 modesense_capacity = spc * ncyl; 5272 5273 SD_INFO(SD_LOG_COMMON, un, 5274 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5275 SD_INFO(SD_LOG_COMMON, un, 5276 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5277 SD_INFO(SD_LOG_COMMON, un, 5278 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5279 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5280 (void *)pgeom_p, capacity); 5281 5282 /* 5283 * Compensate if the drive's geometry is not rectangular, i.e., 5284 * the product of C * H * S returned by MODE SENSE >= that returned 5285 * by read capacity. This is an idiosyncrasy of the original x86 5286 * disk subsystem. 5287 */ 5288 if (modesense_capacity >= capacity) { 5289 SD_INFO(SD_LOG_COMMON, un, 5290 "sd_get_physical_geometry: adjusting acyl; " 5291 "old: %d; new: %d\n", pgeom_p->g_acyl, 5292 (modesense_capacity - capacity + spc - 1) / spc); 5293 if (sector_size != 0) { 5294 /* 1243403: NEC D38x7 drives don't support sec size */ 5295 pgeom_p->g_secsize = (unsigned short)sector_size; 5296 } 5297 pgeom_p->g_nsect = (unsigned short)nsect; 5298 pgeom_p->g_nhead = (unsigned short)nhead; 5299 pgeom_p->g_capacity = capacity; 5300 pgeom_p->g_acyl = 5301 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5302 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5303 } 5304 5305 pgeom_p->g_rpm = (unsigned short)rpm; 5306 pgeom_p->g_intrlv = (unsigned short)intrlv; 5307 5308 SD_INFO(SD_LOG_COMMON, un, 5309 "sd_get_physical_geometry: mode sense geometry:\n"); 5310 SD_INFO(SD_LOG_COMMON, un, 5311 " nsect: %d; sector size: %d; interlv: %d\n", 5312 nsect, sector_size, intrlv); 5313 SD_INFO(SD_LOG_COMMON, un, 5314 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5315 nhead, ncyl, rpm, modesense_capacity); 5316 SD_INFO(SD_LOG_COMMON, un, 5317 "sd_get_physical_geometry: (cached)\n"); 5318 SD_INFO(SD_LOG_COMMON, un, 5319 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5320 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5321 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5322 SD_INFO(SD_LOG_COMMON, un, 5323 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5324 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5325 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5326 5327 mutex_exit(SD_MUTEX(un)); 5328 5329 page4_exit: 5330 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5331 page3_exit: 5332 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5333 } 5334 5335 5336 /* 5337 * Function: sd_get_virtual_geometry 5338 * 5339 * Description: Ask the controller to tell us about the target device. 5340 * 5341 * Arguments: un - pointer to softstate 5342 * capacity - disk capacity in #blocks 5343 * lbasize - disk block size in bytes 5344 * 5345 * Context: Kernel thread only 5346 */ 5347 5348 static void 5349 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5350 { 5351 struct geom_cache *lgeom_p = &un->un_lgeom; 5352 uint_t geombuf; 5353 int spc; 5354 5355 ASSERT(un != NULL); 5356 ASSERT(mutex_owned(SD_MUTEX(un))); 5357 5358 mutex_exit(SD_MUTEX(un)); 5359 5360 /* Set sector size, and total number of sectors */ 5361 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5362 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5363 5364 /* Let the HBA tell us its geometry */ 5365 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5366 5367 mutex_enter(SD_MUTEX(un)); 5368 5369 /* A value of -1 indicates an undefined "geometry" property */ 5370 if (geombuf == (-1)) { 5371 return; 5372 } 5373 5374 /* Initialize the logical geometry cache. */ 5375 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5376 lgeom_p->g_nsect = geombuf & 0xffff; 5377 lgeom_p->g_secsize = un->un_sys_blocksize; 5378 5379 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5380 5381 /* 5382 * Note: The driver originally converted the capacity value from 5383 * target blocks to system blocks. However, the capacity value passed 5384 * to this routine is already in terms of system blocks (this scaling 5385 * is done when the READ CAPACITY command is issued and processed). 5386 * This 'error' may have gone undetected because the usage of g_ncyl 5387 * (which is based upon g_capacity) is very limited within the driver 5388 */ 5389 lgeom_p->g_capacity = capacity; 5390 5391 /* 5392 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5393 * hba may return zero values if the device has been removed. 5394 */ 5395 if (spc == 0) { 5396 lgeom_p->g_ncyl = 0; 5397 } else { 5398 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5399 } 5400 lgeom_p->g_acyl = 0; 5401 5402 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5403 SD_INFO(SD_LOG_COMMON, un, 5404 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5405 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5406 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5407 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5408 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5409 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5410 } 5411 5412 5413 /* 5414 * Function: sd_update_block_info 5415 * 5416 * Description: Calculate a byte count to sector count bitshift value 5417 * from sector size. 5418 * 5419 * Arguments: un: unit struct. 5420 * lbasize: new target sector size 5421 * capacity: new target capacity, ie. block count 5422 * 5423 * Context: Kernel thread context 5424 */ 5425 5426 static void 5427 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5428 { 5429 if (lbasize != 0) { 5430 un->un_tgt_blocksize = lbasize; 5431 un->un_f_tgt_blocksize_is_valid = TRUE; 5432 } 5433 5434 if (capacity != 0) { 5435 un->un_blockcount = capacity; 5436 un->un_f_blockcount_is_valid = TRUE; 5437 } 5438 } 5439 5440 5441 static void 5442 sd_swap_efi_gpt(efi_gpt_t *e) 5443 { 5444 _NOTE(ASSUMING_PROTECTED(*e)) 5445 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5446 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5447 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5448 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5449 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5450 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5451 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5452 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5453 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5454 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5455 e->efi_gpt_NumberOfPartitionEntries = 5456 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5457 e->efi_gpt_SizeOfPartitionEntry = 5458 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5459 e->efi_gpt_PartitionEntryArrayCRC32 = 5460 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5461 } 5462 5463 static void 5464 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5465 { 5466 int i; 5467 5468 _NOTE(ASSUMING_PROTECTED(*p)) 5469 for (i = 0; i < nparts; i++) { 5470 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5471 p[i].efi_gpe_PartitionTypeGUID); 5472 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5473 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5474 /* PartitionAttrs */ 5475 } 5476 } 5477 5478 static int 5479 sd_validate_efi(efi_gpt_t *labp) 5480 { 5481 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5482 return (EINVAL); 5483 /* at least 96 bytes in this version of the spec. */ 5484 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5485 labp->efi_gpt_HeaderSize) 5486 return (EINVAL); 5487 /* this should be 128 bytes */ 5488 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5489 return (EINVAL); 5490 return (0); 5491 } 5492 5493 static int 5494 sd_use_efi(struct sd_lun *un, int path_flag) 5495 { 5496 int i; 5497 int rval = 0; 5498 efi_gpe_t *partitions; 5499 uchar_t *buf; 5500 uint_t lbasize; 5501 uint64_t cap = 0; 5502 uint_t nparts; 5503 diskaddr_t gpe_lba; 5504 struct uuid uuid_type_reserved = EFI_RESERVED; 5505 5506 ASSERT(mutex_owned(SD_MUTEX(un))); 5507 lbasize = un->un_tgt_blocksize; 5508 un->un_reserved = -1; 5509 5510 mutex_exit(SD_MUTEX(un)); 5511 5512 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5513 5514 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5515 rval = EINVAL; 5516 goto done_err; 5517 } 5518 5519 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5520 if (rval) { 5521 goto done_err; 5522 } 5523 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5524 /* not ours */ 5525 rval = ESRCH; 5526 goto done_err; 5527 } 5528 5529 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5530 if (rval) { 5531 goto done_err; 5532 } 5533 sd_swap_efi_gpt((efi_gpt_t *)buf); 5534 5535 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5536 /* 5537 * Couldn't read the primary, try the backup. Our 5538 * capacity at this point could be based on CHS, so 5539 * check what the device reports. 5540 */ 5541 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5542 path_flag); 5543 if (rval) { 5544 goto done_err; 5545 } 5546 5547 /* 5548 * The MMC standard allows READ CAPACITY to be 5549 * inaccurate by a bounded amount (in the interest of 5550 * response latency). As a result, failed READs are 5551 * commonplace (due to the reading of metadata and not 5552 * data). Depending on the per-Vendor/drive Sense data, 5553 * the failed READ can cause many (unnecessary) retries. 5554 */ 5555 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5556 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5557 path_flag)) != 0) { 5558 goto done_err; 5559 } 5560 5561 sd_swap_efi_gpt((efi_gpt_t *)buf); 5562 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5563 5564 /* 5565 * Refer to comments related to off-by-1 at the 5566 * header of this file. Search the next to last 5567 * block for backup EFI label. 5568 */ 5569 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5570 cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5571 path_flag)) != 0) { 5572 goto done_err; 5573 } 5574 sd_swap_efi_gpt((efi_gpt_t *)buf); 5575 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5576 goto done_err; 5577 } 5578 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5579 "primary label corrupt; using backup\n"); 5580 } 5581 5582 if (cap == 0) 5583 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5584 path_flag); 5585 5586 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5587 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5588 5589 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5590 path_flag); 5591 if (rval) { 5592 goto done_err; 5593 } 5594 partitions = (efi_gpe_t *)buf; 5595 5596 if (nparts > MAXPART) { 5597 nparts = MAXPART; 5598 } 5599 sd_swap_efi_gpe(nparts, partitions); 5600 5601 mutex_enter(SD_MUTEX(un)); 5602 5603 /* Fill in partition table. */ 5604 for (i = 0; i < nparts; i++) { 5605 if (partitions->efi_gpe_StartingLBA != 0 || 5606 partitions->efi_gpe_EndingLBA != 0) { 5607 un->un_map[i].dkl_cylno = 5608 partitions->efi_gpe_StartingLBA; 5609 un->un_map[i].dkl_nblk = 5610 partitions->efi_gpe_EndingLBA - 5611 partitions->efi_gpe_StartingLBA + 1; 5612 un->un_offset[i] = 5613 partitions->efi_gpe_StartingLBA; 5614 } 5615 if (un->un_reserved == -1) { 5616 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID, 5617 &uuid_type_reserved, sizeof (struct uuid)) == 0) { 5618 un->un_reserved = i; 5619 } 5620 } 5621 if (i == WD_NODE) { 5622 /* 5623 * minor number 7 corresponds to the whole disk 5624 */ 5625 un->un_map[i].dkl_cylno = 0; 5626 un->un_map[i].dkl_nblk = un->un_blockcount; 5627 un->un_offset[i] = 0; 5628 } 5629 partitions++; 5630 } 5631 un->un_solaris_offset = 0; 5632 un->un_solaris_size = cap; 5633 un->un_f_geometry_is_valid = TRUE; 5634 5635 /* clear the vtoc label */ 5636 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5637 5638 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5639 5640 /* 5641 * For EFI labeled disk, create and set the partition stats 5642 * at attach time, update the stats according to dynamic 5643 * partition changes during running time. 5644 */ 5645 if (un->un_f_pkstats_enabled) { 5646 sd_set_pstats(un); 5647 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_use_efi: " 5648 "un:0x%p pstats created and set, or updated\n", un); 5649 } 5650 return (0); 5651 5652 done_err: 5653 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5654 mutex_enter(SD_MUTEX(un)); 5655 /* 5656 * if we didn't find something that could look like a VTOC 5657 * and the disk is over 1TB, we know there isn't a valid label. 5658 * Otherwise let sd_uselabel decide what to do. We only 5659 * want to invalidate this if we're certain the label isn't 5660 * valid because sd_prop_op will now fail, which in turn 5661 * causes things like opens and stats on the partition to fail. 5662 */ 5663 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5664 un->un_f_geometry_is_valid = FALSE; 5665 } 5666 return (rval); 5667 } 5668 5669 5670 /* 5671 * Function: sd_uselabel 5672 * 5673 * Description: Validate the disk label and update the relevant data (geometry, 5674 * partition, vtoc, and capacity data) in the sd_lun struct. 5675 * Marks the geometry of the unit as being valid. 5676 * 5677 * Arguments: un: unit struct. 5678 * dk_label: disk label 5679 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5680 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5681 * to use the USCSI "direct" chain and bypass the normal 5682 * command waitq. 5683 * 5684 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5685 * partition, vtoc, and capacity data are good. 5686 * 5687 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5688 * label; or computed capacity does not jibe with capacity 5689 * reported from the READ CAPACITY command. 5690 * 5691 * Context: Kernel thread only (can sleep). 5692 */ 5693 5694 static int 5695 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5696 { 5697 short *sp; 5698 short sum; 5699 short count; 5700 int label_error = SD_LABEL_IS_VALID; 5701 int i; 5702 int capacity; 5703 int part_end; 5704 int track_capacity; 5705 int err; 5706 #if defined(_SUNOS_VTOC_16) 5707 struct dkl_partition *vpartp; 5708 #endif 5709 ASSERT(un != NULL); 5710 ASSERT(mutex_owned(SD_MUTEX(un))); 5711 5712 /* Validate the magic number of the label. */ 5713 if (labp->dkl_magic != DKL_MAGIC) { 5714 #if defined(__sparc) 5715 if ((un->un_state == SD_STATE_NORMAL) && 5716 un->un_f_vtoc_errlog_supported) { 5717 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5718 "Corrupt label; wrong magic number\n"); 5719 } 5720 #endif 5721 return (SD_LABEL_IS_INVALID); 5722 } 5723 5724 /* Validate the checksum of the label. */ 5725 sp = (short *)labp; 5726 sum = 0; 5727 count = sizeof (struct dk_label) / sizeof (short); 5728 while (count--) { 5729 sum ^= *sp++; 5730 } 5731 5732 if (sum != 0) { 5733 #if defined(_SUNOS_VTOC_16) 5734 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5735 #elif defined(_SUNOS_VTOC_8) 5736 if ((un->un_state == SD_STATE_NORMAL) && 5737 un->un_f_vtoc_errlog_supported) { 5738 #endif 5739 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5740 "Corrupt label - label checksum failed\n"); 5741 } 5742 return (SD_LABEL_IS_INVALID); 5743 } 5744 5745 5746 /* 5747 * Fill in geometry structure with data from label. 5748 */ 5749 bzero(&un->un_g, sizeof (struct dk_geom)); 5750 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5751 un->un_g.dkg_acyl = labp->dkl_acyl; 5752 un->un_g.dkg_bcyl = 0; 5753 un->un_g.dkg_nhead = labp->dkl_nhead; 5754 un->un_g.dkg_nsect = labp->dkl_nsect; 5755 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5756 5757 #if defined(_SUNOS_VTOC_8) 5758 un->un_g.dkg_gap1 = labp->dkl_gap1; 5759 un->un_g.dkg_gap2 = labp->dkl_gap2; 5760 un->un_g.dkg_bhead = labp->dkl_bhead; 5761 #endif 5762 #if defined(_SUNOS_VTOC_16) 5763 un->un_dkg_skew = labp->dkl_skew; 5764 #endif 5765 5766 #if defined(__i386) || defined(__amd64) 5767 un->un_g.dkg_apc = labp->dkl_apc; 5768 #endif 5769 5770 /* 5771 * Currently we rely on the values in the label being accurate. If 5772 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5773 * 5774 * Note: In the future a MODE SENSE may be used to retrieve this data, 5775 * although this command is optional in SCSI-2. 5776 */ 5777 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5778 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5779 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5780 5781 /* 5782 * The Read and Write reinstruct values may not be valid 5783 * for older disks. 5784 */ 5785 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5786 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5787 5788 /* Fill in partition table. */ 5789 #if defined(_SUNOS_VTOC_8) 5790 for (i = 0; i < NDKMAP; i++) { 5791 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5792 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5793 } 5794 #endif 5795 #if defined(_SUNOS_VTOC_16) 5796 vpartp = labp->dkl_vtoc.v_part; 5797 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5798 5799 /* Prevent divide by zero */ 5800 if (track_capacity == 0) { 5801 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5802 "Corrupt label - zero nhead or nsect value\n"); 5803 5804 return (SD_LABEL_IS_INVALID); 5805 } 5806 5807 for (i = 0; i < NDKMAP; i++, vpartp++) { 5808 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5809 un->un_map[i].dkl_nblk = vpartp->p_size; 5810 } 5811 #endif 5812 5813 /* Fill in VTOC Structure. */ 5814 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5815 #if defined(_SUNOS_VTOC_8) 5816 /* 5817 * The 8-slice vtoc does not include the ascii label; save it into 5818 * the device's soft state structure here. 5819 */ 5820 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5821 #endif 5822 5823 /* Now look for a valid capacity. */ 5824 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5825 capacity = (un->un_g.dkg_ncyl * track_capacity); 5826 5827 if (un->un_g.dkg_acyl) { 5828 #if defined(__i386) || defined(__amd64) 5829 /* we may have > 1 alts cylinder */ 5830 capacity += (track_capacity * un->un_g.dkg_acyl); 5831 #else 5832 capacity += track_capacity; 5833 #endif 5834 } 5835 5836 /* 5837 * Force check here to ensure the computed capacity is valid. 5838 * If capacity is zero, it indicates an invalid label and 5839 * we should abort updating the relevant data then. 5840 */ 5841 if (capacity == 0) { 5842 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5843 "Corrupt label - no valid capacity could be retrieved\n"); 5844 5845 return (SD_LABEL_IS_INVALID); 5846 } 5847 5848 /* Mark the geometry as valid. */ 5849 un->un_f_geometry_is_valid = TRUE; 5850 5851 /* 5852 * At this point, un->un_blockcount should contain valid data from 5853 * the READ CAPACITY command. 5854 */ 5855 if (un->un_f_blockcount_is_valid != TRUE) { 5856 /* 5857 * We have a situation where the target didn't give us a good 5858 * READ CAPACITY value, yet there appears to be a valid label. 5859 * In this case, we'll fake the capacity. 5860 */ 5861 un->un_blockcount = capacity; 5862 un->un_f_blockcount_is_valid = TRUE; 5863 goto done; 5864 } 5865 5866 5867 if ((capacity <= un->un_blockcount) || 5868 (un->un_state != SD_STATE_NORMAL)) { 5869 #if defined(_SUNOS_VTOC_8) 5870 /* 5871 * We can't let this happen on drives that are subdivided 5872 * into logical disks (i.e., that have an fdisk table). 5873 * The un_blockcount field should always hold the full media 5874 * size in sectors, period. This code would overwrite 5875 * un_blockcount with the size of the Solaris fdisk partition. 5876 */ 5877 SD_ERROR(SD_LOG_COMMON, un, 5878 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5879 capacity, un->un_blockcount); 5880 un->un_blockcount = capacity; 5881 un->un_f_blockcount_is_valid = TRUE; 5882 #endif /* defined(_SUNOS_VTOC_8) */ 5883 goto done; 5884 } 5885 5886 if (ISCD(un)) { 5887 /* For CDROMs, we trust that the data in the label is OK. */ 5888 #if defined(_SUNOS_VTOC_8) 5889 for (i = 0; i < NDKMAP; i++) { 5890 part_end = labp->dkl_nhead * labp->dkl_nsect * 5891 labp->dkl_map[i].dkl_cylno + 5892 labp->dkl_map[i].dkl_nblk - 1; 5893 5894 if ((labp->dkl_map[i].dkl_nblk) && 5895 (part_end > un->un_blockcount)) { 5896 un->un_f_geometry_is_valid = FALSE; 5897 break; 5898 } 5899 } 5900 #endif 5901 #if defined(_SUNOS_VTOC_16) 5902 vpartp = &(labp->dkl_vtoc.v_part[0]); 5903 for (i = 0; i < NDKMAP; i++, vpartp++) { 5904 part_end = vpartp->p_start + vpartp->p_size; 5905 if ((vpartp->p_size > 0) && 5906 (part_end > un->un_blockcount)) { 5907 un->un_f_geometry_is_valid = FALSE; 5908 break; 5909 } 5910 } 5911 #endif 5912 } else { 5913 uint64_t t_capacity; 5914 uint32_t t_lbasize; 5915 5916 mutex_exit(SD_MUTEX(un)); 5917 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5918 path_flag); 5919 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5920 mutex_enter(SD_MUTEX(un)); 5921 5922 if (err == 0) { 5923 sd_update_block_info(un, t_lbasize, t_capacity); 5924 } 5925 5926 if (capacity > un->un_blockcount) { 5927 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5928 "Corrupt label - bad geometry\n"); 5929 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5930 "Label says %u blocks; Drive says %llu blocks\n", 5931 capacity, (unsigned long long)un->un_blockcount); 5932 un->un_f_geometry_is_valid = FALSE; 5933 label_error = SD_LABEL_IS_INVALID; 5934 } 5935 } 5936 5937 done: 5938 5939 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5940 SD_INFO(SD_LOG_COMMON, un, 5941 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5942 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5943 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5944 SD_INFO(SD_LOG_COMMON, un, 5945 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5946 un->un_tgt_blocksize, un->un_blockcount, 5947 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5948 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5949 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5950 5951 ASSERT(mutex_owned(SD_MUTEX(un))); 5952 5953 return (label_error); 5954 } 5955 5956 5957 /* 5958 * Function: sd_build_default_label 5959 * 5960 * Description: Generate a default label for those devices that do not have 5961 * one, e.g., new media, removable cartridges, etc.. 5962 * 5963 * Context: Kernel thread only 5964 */ 5965 5966 static void 5967 sd_build_default_label(struct sd_lun *un) 5968 { 5969 #if defined(_SUNOS_VTOC_16) 5970 uint_t phys_spc; 5971 uint_t disksize; 5972 struct dk_geom un_g; 5973 uint64_t capacity; 5974 #endif 5975 5976 ASSERT(un != NULL); 5977 ASSERT(mutex_owned(SD_MUTEX(un))); 5978 5979 #if defined(_SUNOS_VTOC_8) 5980 /* 5981 * Note: This is a legacy check for non-removable devices on VTOC_8 5982 * only. This may be a valid check for VTOC_16 as well. 5983 * Once we understand why there is this difference between SPARC and 5984 * x86 platform, we could remove this legacy check. 5985 */ 5986 ASSERT(un->un_f_default_vtoc_supported); 5987 #endif 5988 5989 bzero(&un->un_g, sizeof (struct dk_geom)); 5990 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5991 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5992 5993 #if defined(_SUNOS_VTOC_8) 5994 5995 /* 5996 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 5997 * But it is still necessary to set up various geometry information, 5998 * and we are doing this here. 5999 */ 6000 6001 /* 6002 * For the rpm, we use the minimum for the disk. For the head, cyl, 6003 * and number of sector per track, if the capacity <= 1GB, head = 64, 6004 * sect = 32. else head = 255, sect 63 Note: the capacity should be 6005 * equal to C*H*S values. This will cause some truncation of size due 6006 * to round off errors. For CD-ROMs, this truncation can have adverse 6007 * side effects, so returning ncyl and nhead as 1. The nsect will 6008 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 6009 */ 6010 if (ISCD(un)) { 6011 /* 6012 * Preserve the old behavior for non-writable 6013 * medias. Since dkg_nsect is a ushort, it 6014 * will lose bits as cdroms have more than 6015 * 65536 sectors. So if we recalculate 6016 * capacity, it will become much shorter. 6017 * But the dkg_* information is not 6018 * used for CDROMs so it is OK. But for 6019 * Writable CDs we need this information 6020 * to be valid (for newfs say). So we 6021 * make nsect and nhead > 1 that way 6022 * nsect can still stay within ushort limit 6023 * without losing any bits. 6024 */ 6025 if (un->un_f_mmc_writable_media == TRUE) { 6026 un->un_g.dkg_nhead = 64; 6027 un->un_g.dkg_nsect = 32; 6028 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6029 un->un_blockcount = un->un_g.dkg_ncyl * 6030 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6031 } else { 6032 un->un_g.dkg_ncyl = 1; 6033 un->un_g.dkg_nhead = 1; 6034 un->un_g.dkg_nsect = un->un_blockcount; 6035 } 6036 } else { 6037 if (un->un_blockcount <= 0x1000) { 6038 /* unlabeled SCSI floppy device */ 6039 un->un_g.dkg_nhead = 2; 6040 un->un_g.dkg_ncyl = 80; 6041 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 6042 } else if (un->un_blockcount <= 0x200000) { 6043 un->un_g.dkg_nhead = 64; 6044 un->un_g.dkg_nsect = 32; 6045 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6046 } else { 6047 un->un_g.dkg_nhead = 255; 6048 un->un_g.dkg_nsect = 63; 6049 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 6050 } 6051 un->un_blockcount = 6052 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6053 } 6054 6055 un->un_g.dkg_acyl = 0; 6056 un->un_g.dkg_bcyl = 0; 6057 un->un_g.dkg_rpm = 200; 6058 un->un_asciilabel[0] = '\0'; 6059 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 6060 6061 un->un_map[0].dkl_cylno = 0; 6062 un->un_map[0].dkl_nblk = un->un_blockcount; 6063 un->un_map[2].dkl_cylno = 0; 6064 un->un_map[2].dkl_nblk = un->un_blockcount; 6065 6066 #elif defined(_SUNOS_VTOC_16) 6067 6068 if (un->un_solaris_size == 0) { 6069 /* 6070 * Got fdisk table but no solaris entry therefore 6071 * don't create a default label 6072 */ 6073 un->un_f_geometry_is_valid = TRUE; 6074 return; 6075 } 6076 6077 /* 6078 * For CDs we continue to use the physical geometry to calculate 6079 * number of cylinders. All other devices must convert the 6080 * physical geometry (geom_cache) to values that will fit 6081 * in a dk_geom structure. 6082 */ 6083 if (ISCD(un)) { 6084 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 6085 } else { 6086 /* Convert physical geometry to disk geometry */ 6087 bzero(&un_g, sizeof (struct dk_geom)); 6088 6089 /* 6090 * Refer to comments related to off-by-1 at the 6091 * header of this file. 6092 * Before caculating geometry, capacity should be 6093 * decreased by 1. That un_f_capacity_adjusted is 6094 * TRUE means that we are treating a 1TB disk as 6095 * (1T - 512)B. And the capacity of disks is already 6096 * decreased by 1. 6097 */ 6098 if (!un->un_f_capacity_adjusted && 6099 !un->un_f_has_removable_media && 6100 !un->un_f_is_hotpluggable && 6101 un->un_tgt_blocksize == un->un_sys_blocksize) 6102 capacity = un->un_blockcount - 1; 6103 else 6104 capacity = un->un_blockcount; 6105 6106 sd_convert_geometry(capacity, &un_g); 6107 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 6108 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6109 } 6110 6111 ASSERT(phys_spc != 0); 6112 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 6113 un->un_g.dkg_acyl = DK_ACYL; 6114 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 6115 disksize = un->un_g.dkg_ncyl * phys_spc; 6116 6117 if (ISCD(un)) { 6118 /* 6119 * CD's don't use the "heads * sectors * cyls"-type of 6120 * geometry, but instead use the entire capacity of the media. 6121 */ 6122 disksize = un->un_solaris_size; 6123 un->un_g.dkg_nhead = 1; 6124 un->un_g.dkg_nsect = 1; 6125 un->un_g.dkg_rpm = 6126 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 6127 6128 un->un_vtoc.v_part[0].p_start = 0; 6129 un->un_vtoc.v_part[0].p_size = disksize; 6130 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 6131 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 6132 6133 un->un_map[0].dkl_cylno = 0; 6134 un->un_map[0].dkl_nblk = disksize; 6135 un->un_offset[0] = 0; 6136 6137 } else { 6138 /* 6139 * Hard disks and removable media cartridges 6140 */ 6141 un->un_g.dkg_rpm = 6142 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 6143 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 6144 6145 /* Add boot slice */ 6146 un->un_vtoc.v_part[8].p_start = 0; 6147 un->un_vtoc.v_part[8].p_size = phys_spc; 6148 un->un_vtoc.v_part[8].p_tag = V_BOOT; 6149 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 6150 6151 un->un_map[8].dkl_cylno = 0; 6152 un->un_map[8].dkl_nblk = phys_spc; 6153 un->un_offset[8] = 0; 6154 } 6155 6156 un->un_g.dkg_apc = 0; 6157 un->un_vtoc.v_nparts = V_NUMPAR; 6158 un->un_vtoc.v_version = V_VERSION; 6159 6160 /* Add backup slice */ 6161 un->un_vtoc.v_part[2].p_start = 0; 6162 un->un_vtoc.v_part[2].p_size = disksize; 6163 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 6164 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 6165 6166 un->un_map[2].dkl_cylno = 0; 6167 un->un_map[2].dkl_nblk = disksize; 6168 un->un_offset[2] = 0; 6169 6170 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 6171 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 6172 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 6173 6174 #else 6175 #error "No VTOC format defined." 6176 #endif 6177 6178 un->un_g.dkg_read_reinstruct = 0; 6179 un->un_g.dkg_write_reinstruct = 0; 6180 6181 un->un_g.dkg_intrlv = 1; 6182 6183 un->un_vtoc.v_sanity = VTOC_SANE; 6184 6185 un->un_f_geometry_is_valid = TRUE; 6186 6187 SD_INFO(SD_LOG_COMMON, un, 6188 "sd_build_default_label: Default label created: " 6189 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 6190 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 6191 un->un_g.dkg_nsect, un->un_blockcount); 6192 } 6193 6194 6195 #if defined(_FIRMWARE_NEEDS_FDISK) 6196 /* 6197 * Max CHS values, as they are encoded into bytes, for 1022/254/63 6198 */ 6199 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 6200 #define LBA_MAX_CYL (1022 & 0xFF) 6201 #define LBA_MAX_HEAD (254) 6202 6203 6204 /* 6205 * Function: sd_has_max_chs_vals 6206 * 6207 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 6208 * 6209 * Arguments: fdp - ptr to CHS info 6210 * 6211 * Return Code: True or false 6212 * 6213 * Context: Any. 6214 */ 6215 6216 static int 6217 sd_has_max_chs_vals(struct ipart *fdp) 6218 { 6219 return ((fdp->begcyl == LBA_MAX_CYL) && 6220 (fdp->beghead == LBA_MAX_HEAD) && 6221 (fdp->begsect == LBA_MAX_SECT) && 6222 (fdp->endcyl == LBA_MAX_CYL) && 6223 (fdp->endhead == LBA_MAX_HEAD) && 6224 (fdp->endsect == LBA_MAX_SECT)); 6225 } 6226 #endif 6227 6228 6229 /* 6230 * Function: sd_inq_fill 6231 * 6232 * Description: Print a piece of inquiry data, cleaned up for non-printable 6233 * characters and stopping at the first space character after 6234 * the beginning of the passed string; 6235 * 6236 * Arguments: p - source string 6237 * l - maximum length to copy 6238 * s - destination string 6239 * 6240 * Context: Any. 6241 */ 6242 6243 static void 6244 sd_inq_fill(char *p, int l, char *s) 6245 { 6246 unsigned i = 0; 6247 char c; 6248 6249 while (i++ < l) { 6250 if ((c = *p++) < ' ' || c >= 0x7F) { 6251 c = '*'; 6252 } else if (i != 1 && c == ' ') { 6253 break; 6254 } 6255 *s++ = c; 6256 } 6257 *s++ = 0; 6258 } 6259 6260 6261 /* 6262 * Function: sd_register_devid 6263 * 6264 * Description: This routine will obtain the device id information from the 6265 * target, obtain the serial number, and register the device 6266 * id with the ddi framework. 6267 * 6268 * Arguments: devi - the system's dev_info_t for the device. 6269 * un - driver soft state (unit) structure 6270 * reservation_flag - indicates if a reservation conflict 6271 * occurred during attach 6272 * 6273 * Context: Kernel Thread 6274 */ 6275 static void 6276 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 6277 { 6278 int rval = 0; 6279 uchar_t *inq80 = NULL; 6280 size_t inq80_len = MAX_INQUIRY_SIZE; 6281 size_t inq80_resid = 0; 6282 uchar_t *inq83 = NULL; 6283 size_t inq83_len = MAX_INQUIRY_SIZE; 6284 size_t inq83_resid = 0; 6285 6286 ASSERT(un != NULL); 6287 ASSERT(mutex_owned(SD_MUTEX(un))); 6288 ASSERT((SD_DEVINFO(un)) == devi); 6289 6290 /* 6291 * This is the case of antiquated Sun disk drives that have the 6292 * FAB_DEVID property set in the disk_table. These drives 6293 * manage the devid's by storing them in last 2 available sectors 6294 * on the drive and have them fabricated by the ddi layer by calling 6295 * ddi_devid_init and passing the DEVID_FAB flag. 6296 */ 6297 if (un->un_f_opt_fab_devid == TRUE) { 6298 /* 6299 * Depending on EINVAL isn't reliable, since a reserved disk 6300 * may result in invalid geometry, so check to make sure a 6301 * reservation conflict did not occur during attach. 6302 */ 6303 if ((sd_get_devid(un) == EINVAL) && 6304 (reservation_flag != SD_TARGET_IS_RESERVED)) { 6305 /* 6306 * The devid is invalid AND there is no reservation 6307 * conflict. Fabricate a new devid. 6308 */ 6309 (void) sd_create_devid(un); 6310 } 6311 6312 /* Register the devid if it exists */ 6313 if (un->un_devid != NULL) { 6314 (void) ddi_devid_register(SD_DEVINFO(un), 6315 un->un_devid); 6316 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6317 "sd_register_devid: Devid Fabricated\n"); 6318 } 6319 return; 6320 } 6321 6322 /* 6323 * We check the availibility of the World Wide Name (0x83) and Unit 6324 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 6325 * un_vpd_page_mask from them, we decide which way to get the WWN. If 6326 * 0x83 is availible, that is the best choice. Our next choice is 6327 * 0x80. If neither are availible, we munge the devid from the device 6328 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 6329 * to fabricate a devid for non-Sun qualified disks. 6330 */ 6331 if (sd_check_vpd_page_support(un) == 0) { 6332 /* collect page 80 data if available */ 6333 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 6334 6335 mutex_exit(SD_MUTEX(un)); 6336 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 6337 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 6338 0x01, 0x80, &inq80_resid); 6339 6340 if (rval != 0) { 6341 kmem_free(inq80, inq80_len); 6342 inq80 = NULL; 6343 inq80_len = 0; 6344 } 6345 mutex_enter(SD_MUTEX(un)); 6346 } 6347 6348 /* collect page 83 data if available */ 6349 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 6350 mutex_exit(SD_MUTEX(un)); 6351 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6352 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6353 0x01, 0x83, &inq83_resid); 6354 6355 if (rval != 0) { 6356 kmem_free(inq83, inq83_len); 6357 inq83 = NULL; 6358 inq83_len = 0; 6359 } 6360 mutex_enter(SD_MUTEX(un)); 6361 } 6362 } 6363 6364 /* encode best devid possible based on data available */ 6365 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6366 (char *)ddi_driver_name(SD_DEVINFO(un)), 6367 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6368 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6369 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6370 6371 /* devid successfully encoded, register devid */ 6372 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6373 6374 } else { 6375 /* 6376 * Unable to encode a devid based on data available. 6377 * This is not a Sun qualified disk. Older Sun disk 6378 * drives that have the SD_FAB_DEVID property 6379 * set in the disk_table and non Sun qualified 6380 * disks are treated in the same manner. These 6381 * drives manage the devid's by storing them in 6382 * last 2 available sectors on the drive and 6383 * have them fabricated by the ddi layer by 6384 * calling ddi_devid_init and passing the 6385 * DEVID_FAB flag. 6386 * Create a fabricate devid only if there's no 6387 * fabricate devid existed. 6388 */ 6389 if (sd_get_devid(un) == EINVAL) { 6390 (void) sd_create_devid(un); 6391 } 6392 un->un_f_opt_fab_devid = TRUE; 6393 6394 /* Register the devid if it exists */ 6395 if (un->un_devid != NULL) { 6396 (void) ddi_devid_register(SD_DEVINFO(un), 6397 un->un_devid); 6398 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6399 "sd_register_devid: devid fabricated using " 6400 "ddi framework\n"); 6401 } 6402 } 6403 6404 /* clean up resources */ 6405 if (inq80 != NULL) { 6406 kmem_free(inq80, inq80_len); 6407 } 6408 if (inq83 != NULL) { 6409 kmem_free(inq83, inq83_len); 6410 } 6411 } 6412 6413 static daddr_t 6414 sd_get_devid_block(struct sd_lun *un) 6415 { 6416 daddr_t spc, blk, head, cyl; 6417 6418 if ((un->un_f_geometry_is_valid == FALSE) || 6419 (un->un_solaris_size < DK_LABEL_LOC)) 6420 return (-1); 6421 6422 if (un->un_vtoc.v_sanity != VTOC_SANE) { 6423 /* EFI labeled */ 6424 if (un->un_reserved != -1) { 6425 blk = un->un_map[un->un_reserved].dkl_cylno; 6426 } else { 6427 return (-1); 6428 } 6429 } else { 6430 /* SMI labeled */ 6431 /* this geometry doesn't allow us to write a devid */ 6432 if (un->un_g.dkg_acyl < 2) { 6433 return (-1); 6434 } 6435 6436 /* 6437 * Subtract 2 guarantees that the next to last cylinder 6438 * is used 6439 */ 6440 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6441 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6442 head = un->un_g.dkg_nhead - 1; 6443 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6444 (head * un->un_g.dkg_nsect) + 1; 6445 } 6446 return (blk); 6447 } 6448 6449 /* 6450 * Function: sd_get_devid 6451 * 6452 * Description: This routine will return 0 if a valid device id has been 6453 * obtained from the target and stored in the soft state. If a 6454 * valid device id has not been previously read and stored, a 6455 * read attempt will be made. 6456 * 6457 * Arguments: un - driver soft state (unit) structure 6458 * 6459 * Return Code: 0 if we successfully get the device id 6460 * 6461 * Context: Kernel Thread 6462 */ 6463 6464 static int 6465 sd_get_devid(struct sd_lun *un) 6466 { 6467 struct dk_devid *dkdevid; 6468 ddi_devid_t tmpid; 6469 uint_t *ip; 6470 size_t sz; 6471 daddr_t blk; 6472 int status; 6473 int chksum; 6474 int i; 6475 size_t buffer_size; 6476 6477 ASSERT(un != NULL); 6478 ASSERT(mutex_owned(SD_MUTEX(un))); 6479 6480 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6481 un); 6482 6483 if (un->un_devid != NULL) { 6484 return (0); 6485 } 6486 6487 blk = sd_get_devid_block(un); 6488 if (blk < 0) 6489 return (EINVAL); 6490 6491 /* 6492 * Read and verify device id, stored in the reserved cylinders at the 6493 * end of the disk. Backup label is on the odd sectors of the last 6494 * track of the last cylinder. Device id will be on track of the next 6495 * to last cylinder. 6496 */ 6497 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6498 mutex_exit(SD_MUTEX(un)); 6499 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6500 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6501 SD_PATH_DIRECT); 6502 if (status != 0) { 6503 goto error; 6504 } 6505 6506 /* Validate the revision */ 6507 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6508 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6509 status = EINVAL; 6510 goto error; 6511 } 6512 6513 /* Calculate the checksum */ 6514 chksum = 0; 6515 ip = (uint_t *)dkdevid; 6516 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6517 i++) { 6518 chksum ^= ip[i]; 6519 } 6520 6521 /* Compare the checksums */ 6522 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6523 status = EINVAL; 6524 goto error; 6525 } 6526 6527 /* Validate the device id */ 6528 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6529 status = EINVAL; 6530 goto error; 6531 } 6532 6533 /* 6534 * Store the device id in the driver soft state 6535 */ 6536 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6537 tmpid = kmem_alloc(sz, KM_SLEEP); 6538 6539 mutex_enter(SD_MUTEX(un)); 6540 6541 un->un_devid = tmpid; 6542 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6543 6544 kmem_free(dkdevid, buffer_size); 6545 6546 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6547 6548 return (status); 6549 error: 6550 mutex_enter(SD_MUTEX(un)); 6551 kmem_free(dkdevid, buffer_size); 6552 return (status); 6553 } 6554 6555 6556 /* 6557 * Function: sd_create_devid 6558 * 6559 * Description: This routine will fabricate the device id and write it 6560 * to the disk. 6561 * 6562 * Arguments: un - driver soft state (unit) structure 6563 * 6564 * Return Code: value of the fabricated device id 6565 * 6566 * Context: Kernel Thread 6567 */ 6568 6569 static ddi_devid_t 6570 sd_create_devid(struct sd_lun *un) 6571 { 6572 ASSERT(un != NULL); 6573 6574 /* Fabricate the devid */ 6575 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6576 == DDI_FAILURE) { 6577 return (NULL); 6578 } 6579 6580 /* Write the devid to disk */ 6581 if (sd_write_deviceid(un) != 0) { 6582 ddi_devid_free(un->un_devid); 6583 un->un_devid = NULL; 6584 } 6585 6586 return (un->un_devid); 6587 } 6588 6589 6590 /* 6591 * Function: sd_write_deviceid 6592 * 6593 * Description: This routine will write the device id to the disk 6594 * reserved sector. 6595 * 6596 * Arguments: un - driver soft state (unit) structure 6597 * 6598 * Return Code: EINVAL 6599 * value returned by sd_send_scsi_cmd 6600 * 6601 * Context: Kernel Thread 6602 */ 6603 6604 static int 6605 sd_write_deviceid(struct sd_lun *un) 6606 { 6607 struct dk_devid *dkdevid; 6608 daddr_t blk; 6609 uint_t *ip, chksum; 6610 int status; 6611 int i; 6612 6613 ASSERT(mutex_owned(SD_MUTEX(un))); 6614 6615 blk = sd_get_devid_block(un); 6616 if (blk < 0) 6617 return (-1); 6618 mutex_exit(SD_MUTEX(un)); 6619 6620 /* Allocate the buffer */ 6621 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6622 6623 /* Fill in the revision */ 6624 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6625 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6626 6627 /* Copy in the device id */ 6628 mutex_enter(SD_MUTEX(un)); 6629 bcopy(un->un_devid, &dkdevid->dkd_devid, 6630 ddi_devid_sizeof(un->un_devid)); 6631 mutex_exit(SD_MUTEX(un)); 6632 6633 /* Calculate the checksum */ 6634 chksum = 0; 6635 ip = (uint_t *)dkdevid; 6636 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6637 i++) { 6638 chksum ^= ip[i]; 6639 } 6640 6641 /* Fill-in checksum */ 6642 DKD_FORMCHKSUM(chksum, dkdevid); 6643 6644 /* Write the reserved sector */ 6645 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6646 SD_PATH_DIRECT); 6647 6648 kmem_free(dkdevid, un->un_sys_blocksize); 6649 6650 mutex_enter(SD_MUTEX(un)); 6651 return (status); 6652 } 6653 6654 6655 /* 6656 * Function: sd_check_vpd_page_support 6657 * 6658 * Description: This routine sends an inquiry command with the EVPD bit set and 6659 * a page code of 0x00 to the device. It is used to determine which 6660 * vital product pages are availible to find the devid. We are 6661 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6662 * device does not support that command. 6663 * 6664 * Arguments: un - driver soft state (unit) structure 6665 * 6666 * Return Code: 0 - success 6667 * 1 - check condition 6668 * 6669 * Context: This routine can sleep. 6670 */ 6671 6672 static int 6673 sd_check_vpd_page_support(struct sd_lun *un) 6674 { 6675 uchar_t *page_list = NULL; 6676 uchar_t page_length = 0xff; /* Use max possible length */ 6677 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6678 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6679 int rval = 0; 6680 int counter; 6681 6682 ASSERT(un != NULL); 6683 ASSERT(mutex_owned(SD_MUTEX(un))); 6684 6685 mutex_exit(SD_MUTEX(un)); 6686 6687 /* 6688 * We'll set the page length to the maximum to save figuring it out 6689 * with an additional call. 6690 */ 6691 page_list = kmem_zalloc(page_length, KM_SLEEP); 6692 6693 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6694 page_code, NULL); 6695 6696 mutex_enter(SD_MUTEX(un)); 6697 6698 /* 6699 * Now we must validate that the device accepted the command, as some 6700 * drives do not support it. If the drive does support it, we will 6701 * return 0, and the supported pages will be in un_vpd_page_mask. If 6702 * not, we return -1. 6703 */ 6704 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6705 /* Loop to find one of the 2 pages we need */ 6706 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6707 6708 /* 6709 * Pages are returned in ascending order, and 0x83 is what we 6710 * are hoping for. 6711 */ 6712 while ((page_list[counter] <= 0x83) && 6713 (counter <= (page_list[VPD_PAGE_LENGTH] + 6714 VPD_HEAD_OFFSET))) { 6715 /* 6716 * Add 3 because page_list[3] is the number of 6717 * pages minus 3 6718 */ 6719 6720 switch (page_list[counter]) { 6721 case 0x00: 6722 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6723 break; 6724 case 0x80: 6725 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6726 break; 6727 case 0x81: 6728 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6729 break; 6730 case 0x82: 6731 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6732 break; 6733 case 0x83: 6734 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6735 break; 6736 } 6737 counter++; 6738 } 6739 6740 } else { 6741 rval = -1; 6742 6743 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6744 "sd_check_vpd_page_support: This drive does not implement " 6745 "VPD pages.\n"); 6746 } 6747 6748 kmem_free(page_list, page_length); 6749 6750 return (rval); 6751 } 6752 6753 6754 /* 6755 * Function: sd_setup_pm 6756 * 6757 * Description: Initialize Power Management on the device 6758 * 6759 * Context: Kernel Thread 6760 */ 6761 6762 static void 6763 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6764 { 6765 uint_t log_page_size; 6766 uchar_t *log_page_data; 6767 int rval; 6768 6769 /* 6770 * Since we are called from attach, holding a mutex for 6771 * un is unnecessary. Because some of the routines called 6772 * from here require SD_MUTEX to not be held, assert this 6773 * right up front. 6774 */ 6775 ASSERT(!mutex_owned(SD_MUTEX(un))); 6776 /* 6777 * Since the sd device does not have the 'reg' property, 6778 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6779 * The following code is to tell cpr that this device 6780 * DOES need to be suspended and resumed. 6781 */ 6782 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6783 "pm-hardware-state", "needs-suspend-resume"); 6784 6785 /* 6786 * This complies with the new power management framework 6787 * for certain desktop machines. Create the pm_components 6788 * property as a string array property. 6789 */ 6790 if (un->un_f_pm_supported) { 6791 /* 6792 * not all devices have a motor, try it first. 6793 * some devices may return ILLEGAL REQUEST, some 6794 * will hang 6795 * The following START_STOP_UNIT is used to check if target 6796 * device has a motor. 6797 */ 6798 un->un_f_start_stop_supported = TRUE; 6799 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6800 SD_PATH_DIRECT) != 0) { 6801 un->un_f_start_stop_supported = FALSE; 6802 } 6803 6804 /* 6805 * create pm properties anyways otherwise the parent can't 6806 * go to sleep 6807 */ 6808 (void) sd_create_pm_components(devi, un); 6809 un->un_f_pm_is_enabled = TRUE; 6810 return; 6811 } 6812 6813 if (!un->un_f_log_sense_supported) { 6814 un->un_power_level = SD_SPINDLE_ON; 6815 un->un_f_pm_is_enabled = FALSE; 6816 return; 6817 } 6818 6819 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6820 6821 #ifdef SDDEBUG 6822 if (sd_force_pm_supported) { 6823 /* Force a successful result */ 6824 rval = 1; 6825 } 6826 #endif 6827 6828 /* 6829 * If the start-stop cycle counter log page is not supported 6830 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6831 * then we should not create the pm_components property. 6832 */ 6833 if (rval == -1) { 6834 /* 6835 * Error. 6836 * Reading log sense failed, most likely this is 6837 * an older drive that does not support log sense. 6838 * If this fails auto-pm is not supported. 6839 */ 6840 un->un_power_level = SD_SPINDLE_ON; 6841 un->un_f_pm_is_enabled = FALSE; 6842 6843 } else if (rval == 0) { 6844 /* 6845 * Page not found. 6846 * The start stop cycle counter is implemented as page 6847 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6848 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6849 */ 6850 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6851 /* 6852 * Page found, use this one. 6853 */ 6854 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6855 un->un_f_pm_is_enabled = TRUE; 6856 } else { 6857 /* 6858 * Error or page not found. 6859 * auto-pm is not supported for this device. 6860 */ 6861 un->un_power_level = SD_SPINDLE_ON; 6862 un->un_f_pm_is_enabled = FALSE; 6863 } 6864 } else { 6865 /* 6866 * Page found, use it. 6867 */ 6868 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6869 un->un_f_pm_is_enabled = TRUE; 6870 } 6871 6872 6873 if (un->un_f_pm_is_enabled == TRUE) { 6874 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6875 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6876 6877 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6878 log_page_size, un->un_start_stop_cycle_page, 6879 0x01, 0, SD_PATH_DIRECT); 6880 #ifdef SDDEBUG 6881 if (sd_force_pm_supported) { 6882 /* Force a successful result */ 6883 rval = 0; 6884 } 6885 #endif 6886 6887 /* 6888 * If the Log sense for Page( Start/stop cycle counter page) 6889 * succeeds, then power managment is supported and we can 6890 * enable auto-pm. 6891 */ 6892 if (rval == 0) { 6893 (void) sd_create_pm_components(devi, un); 6894 } else { 6895 un->un_power_level = SD_SPINDLE_ON; 6896 un->un_f_pm_is_enabled = FALSE; 6897 } 6898 6899 kmem_free(log_page_data, log_page_size); 6900 } 6901 } 6902 6903 6904 /* 6905 * Function: sd_create_pm_components 6906 * 6907 * Description: Initialize PM property. 6908 * 6909 * Context: Kernel thread context 6910 */ 6911 6912 static void 6913 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6914 { 6915 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6916 6917 ASSERT(!mutex_owned(SD_MUTEX(un))); 6918 6919 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6920 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6921 /* 6922 * When components are initially created they are idle, 6923 * power up any non-removables. 6924 * Note: the return value of pm_raise_power can't be used 6925 * for determining if PM should be enabled for this device. 6926 * Even if you check the return values and remove this 6927 * property created above, the PM framework will not honor the 6928 * change after the first call to pm_raise_power. Hence, 6929 * removal of that property does not help if pm_raise_power 6930 * fails. In the case of removable media, the start/stop 6931 * will fail if the media is not present. 6932 */ 6933 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6934 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6935 mutex_enter(SD_MUTEX(un)); 6936 un->un_power_level = SD_SPINDLE_ON; 6937 mutex_enter(&un->un_pm_mutex); 6938 /* Set to on and not busy. */ 6939 un->un_pm_count = 0; 6940 } else { 6941 mutex_enter(SD_MUTEX(un)); 6942 un->un_power_level = SD_SPINDLE_OFF; 6943 mutex_enter(&un->un_pm_mutex); 6944 /* Set to off. */ 6945 un->un_pm_count = -1; 6946 } 6947 mutex_exit(&un->un_pm_mutex); 6948 mutex_exit(SD_MUTEX(un)); 6949 } else { 6950 un->un_power_level = SD_SPINDLE_ON; 6951 un->un_f_pm_is_enabled = FALSE; 6952 } 6953 } 6954 6955 6956 /* 6957 * Function: sd_ddi_suspend 6958 * 6959 * Description: Performs system power-down operations. This includes 6960 * setting the drive state to indicate its suspended so 6961 * that no new commands will be accepted. Also, wait for 6962 * all commands that are in transport or queued to a timer 6963 * for retry to complete. All timeout threads are cancelled. 6964 * 6965 * Return Code: DDI_FAILURE or DDI_SUCCESS 6966 * 6967 * Context: Kernel thread context 6968 */ 6969 6970 static int 6971 sd_ddi_suspend(dev_info_t *devi) 6972 { 6973 struct sd_lun *un; 6974 clock_t wait_cmds_complete; 6975 6976 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6977 if (un == NULL) { 6978 return (DDI_FAILURE); 6979 } 6980 6981 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6982 6983 mutex_enter(SD_MUTEX(un)); 6984 6985 /* Return success if the device is already suspended. */ 6986 if (un->un_state == SD_STATE_SUSPENDED) { 6987 mutex_exit(SD_MUTEX(un)); 6988 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6989 "device already suspended, exiting\n"); 6990 return (DDI_SUCCESS); 6991 } 6992 6993 /* Return failure if the device is being used by HA */ 6994 if (un->un_resvd_status & 6995 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 6996 mutex_exit(SD_MUTEX(un)); 6997 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 6998 "device in use by HA, exiting\n"); 6999 return (DDI_FAILURE); 7000 } 7001 7002 /* 7003 * Return failure if the device is in a resource wait 7004 * or power changing state. 7005 */ 7006 if ((un->un_state == SD_STATE_RWAIT) || 7007 (un->un_state == SD_STATE_PM_CHANGING)) { 7008 mutex_exit(SD_MUTEX(un)); 7009 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7010 "device in resource wait state, exiting\n"); 7011 return (DDI_FAILURE); 7012 } 7013 7014 7015 un->un_save_state = un->un_last_state; 7016 New_state(un, SD_STATE_SUSPENDED); 7017 7018 /* 7019 * Wait for all commands that are in transport or queued to a timer 7020 * for retry to complete. 7021 * 7022 * While waiting, no new commands will be accepted or sent because of 7023 * the new state we set above. 7024 * 7025 * Wait till current operation has completed. If we are in the resource 7026 * wait state (with an intr outstanding) then we need to wait till the 7027 * intr completes and starts the next cmd. We want to wait for 7028 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 7029 */ 7030 wait_cmds_complete = ddi_get_lbolt() + 7031 (sd_wait_cmds_complete * drv_usectohz(1000000)); 7032 7033 while (un->un_ncmds_in_transport != 0) { 7034 /* 7035 * Fail if commands do not finish in the specified time. 7036 */ 7037 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 7038 wait_cmds_complete) == -1) { 7039 /* 7040 * Undo the state changes made above. Everything 7041 * must go back to it's original value. 7042 */ 7043 Restore_state(un); 7044 un->un_last_state = un->un_save_state; 7045 /* Wake up any threads that might be waiting. */ 7046 cv_broadcast(&un->un_suspend_cv); 7047 mutex_exit(SD_MUTEX(un)); 7048 SD_ERROR(SD_LOG_IO_PM, un, 7049 "sd_ddi_suspend: failed due to outstanding cmds\n"); 7050 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 7051 return (DDI_FAILURE); 7052 } 7053 } 7054 7055 /* 7056 * Cancel SCSI watch thread and timeouts, if any are active 7057 */ 7058 7059 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 7060 opaque_t temp_token = un->un_swr_token; 7061 mutex_exit(SD_MUTEX(un)); 7062 scsi_watch_suspend(temp_token); 7063 mutex_enter(SD_MUTEX(un)); 7064 } 7065 7066 if (un->un_reset_throttle_timeid != NULL) { 7067 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7068 un->un_reset_throttle_timeid = NULL; 7069 mutex_exit(SD_MUTEX(un)); 7070 (void) untimeout(temp_id); 7071 mutex_enter(SD_MUTEX(un)); 7072 } 7073 7074 if (un->un_dcvb_timeid != NULL) { 7075 timeout_id_t temp_id = un->un_dcvb_timeid; 7076 un->un_dcvb_timeid = NULL; 7077 mutex_exit(SD_MUTEX(un)); 7078 (void) untimeout(temp_id); 7079 mutex_enter(SD_MUTEX(un)); 7080 } 7081 7082 mutex_enter(&un->un_pm_mutex); 7083 if (un->un_pm_timeid != NULL) { 7084 timeout_id_t temp_id = un->un_pm_timeid; 7085 un->un_pm_timeid = NULL; 7086 mutex_exit(&un->un_pm_mutex); 7087 mutex_exit(SD_MUTEX(un)); 7088 (void) untimeout(temp_id); 7089 mutex_enter(SD_MUTEX(un)); 7090 } else { 7091 mutex_exit(&un->un_pm_mutex); 7092 } 7093 7094 if (un->un_retry_timeid != NULL) { 7095 timeout_id_t temp_id = un->un_retry_timeid; 7096 un->un_retry_timeid = NULL; 7097 mutex_exit(SD_MUTEX(un)); 7098 (void) untimeout(temp_id); 7099 mutex_enter(SD_MUTEX(un)); 7100 } 7101 7102 if (un->un_direct_priority_timeid != NULL) { 7103 timeout_id_t temp_id = un->un_direct_priority_timeid; 7104 un->un_direct_priority_timeid = NULL; 7105 mutex_exit(SD_MUTEX(un)); 7106 (void) untimeout(temp_id); 7107 mutex_enter(SD_MUTEX(un)); 7108 } 7109 7110 if (un->un_f_is_fibre == TRUE) { 7111 /* 7112 * Remove callbacks for insert and remove events 7113 */ 7114 if (un->un_insert_event != NULL) { 7115 mutex_exit(SD_MUTEX(un)); 7116 (void) ddi_remove_event_handler(un->un_insert_cb_id); 7117 mutex_enter(SD_MUTEX(un)); 7118 un->un_insert_event = NULL; 7119 } 7120 7121 if (un->un_remove_event != NULL) { 7122 mutex_exit(SD_MUTEX(un)); 7123 (void) ddi_remove_event_handler(un->un_remove_cb_id); 7124 mutex_enter(SD_MUTEX(un)); 7125 un->un_remove_event = NULL; 7126 } 7127 } 7128 7129 mutex_exit(SD_MUTEX(un)); 7130 7131 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 7132 7133 return (DDI_SUCCESS); 7134 } 7135 7136 7137 /* 7138 * Function: sd_ddi_pm_suspend 7139 * 7140 * Description: Set the drive state to low power. 7141 * Someone else is required to actually change the drive 7142 * power level. 7143 * 7144 * Arguments: un - driver soft state (unit) structure 7145 * 7146 * Return Code: DDI_FAILURE or DDI_SUCCESS 7147 * 7148 * Context: Kernel thread context 7149 */ 7150 7151 static int 7152 sd_ddi_pm_suspend(struct sd_lun *un) 7153 { 7154 ASSERT(un != NULL); 7155 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 7156 7157 ASSERT(!mutex_owned(SD_MUTEX(un))); 7158 mutex_enter(SD_MUTEX(un)); 7159 7160 /* 7161 * Exit if power management is not enabled for this device, or if 7162 * the device is being used by HA. 7163 */ 7164 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 7165 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 7166 mutex_exit(SD_MUTEX(un)); 7167 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 7168 return (DDI_SUCCESS); 7169 } 7170 7171 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 7172 un->un_ncmds_in_driver); 7173 7174 /* 7175 * See if the device is not busy, ie.: 7176 * - we have no commands in the driver for this device 7177 * - not waiting for resources 7178 */ 7179 if ((un->un_ncmds_in_driver == 0) && 7180 (un->un_state != SD_STATE_RWAIT)) { 7181 /* 7182 * The device is not busy, so it is OK to go to low power state. 7183 * Indicate low power, but rely on someone else to actually 7184 * change it. 7185 */ 7186 mutex_enter(&un->un_pm_mutex); 7187 un->un_pm_count = -1; 7188 mutex_exit(&un->un_pm_mutex); 7189 un->un_power_level = SD_SPINDLE_OFF; 7190 } 7191 7192 mutex_exit(SD_MUTEX(un)); 7193 7194 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 7195 7196 return (DDI_SUCCESS); 7197 } 7198 7199 7200 /* 7201 * Function: sd_ddi_resume 7202 * 7203 * Description: Performs system power-up operations.. 7204 * 7205 * Return Code: DDI_SUCCESS 7206 * DDI_FAILURE 7207 * 7208 * Context: Kernel thread context 7209 */ 7210 7211 static int 7212 sd_ddi_resume(dev_info_t *devi) 7213 { 7214 struct sd_lun *un; 7215 7216 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 7217 if (un == NULL) { 7218 return (DDI_FAILURE); 7219 } 7220 7221 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 7222 7223 mutex_enter(SD_MUTEX(un)); 7224 Restore_state(un); 7225 7226 /* 7227 * Restore the state which was saved to give the 7228 * the right state in un_last_state 7229 */ 7230 un->un_last_state = un->un_save_state; 7231 /* 7232 * Note: throttle comes back at full. 7233 * Also note: this MUST be done before calling pm_raise_power 7234 * otherwise the system can get hung in biowait. The scenario where 7235 * this'll happen is under cpr suspend. Writing of the system 7236 * state goes through sddump, which writes 0 to un_throttle. If 7237 * writing the system state then fails, example if the partition is 7238 * too small, then cpr attempts a resume. If throttle isn't restored 7239 * from the saved value until after calling pm_raise_power then 7240 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 7241 * in biowait. 7242 */ 7243 un->un_throttle = un->un_saved_throttle; 7244 7245 /* 7246 * The chance of failure is very rare as the only command done in power 7247 * entry point is START command when you transition from 0->1 or 7248 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 7249 * which suspend was done. Ignore the return value as the resume should 7250 * not be failed. In the case of removable media the media need not be 7251 * inserted and hence there is a chance that raise power will fail with 7252 * media not present. 7253 */ 7254 if (un->un_f_attach_spinup) { 7255 mutex_exit(SD_MUTEX(un)); 7256 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 7257 mutex_enter(SD_MUTEX(un)); 7258 } 7259 7260 /* 7261 * Don't broadcast to the suspend cv and therefore possibly 7262 * start I/O until after power has been restored. 7263 */ 7264 cv_broadcast(&un->un_suspend_cv); 7265 cv_broadcast(&un->un_state_cv); 7266 7267 /* restart thread */ 7268 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 7269 scsi_watch_resume(un->un_swr_token); 7270 } 7271 7272 #if (defined(__fibre)) 7273 if (un->un_f_is_fibre == TRUE) { 7274 /* 7275 * Add callbacks for insert and remove events 7276 */ 7277 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7278 sd_init_event_callbacks(un); 7279 } 7280 } 7281 #endif 7282 7283 /* 7284 * Transport any pending commands to the target. 7285 * 7286 * If this is a low-activity device commands in queue will have to wait 7287 * until new commands come in, which may take awhile. Also, we 7288 * specifically don't check un_ncmds_in_transport because we know that 7289 * there really are no commands in progress after the unit was 7290 * suspended and we could have reached the throttle level, been 7291 * suspended, and have no new commands coming in for awhile. Highly 7292 * unlikely, but so is the low-activity disk scenario. 7293 */ 7294 ddi_xbuf_dispatch(un->un_xbuf_attr); 7295 7296 sd_start_cmds(un, NULL); 7297 mutex_exit(SD_MUTEX(un)); 7298 7299 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 7300 7301 return (DDI_SUCCESS); 7302 } 7303 7304 7305 /* 7306 * Function: sd_ddi_pm_resume 7307 * 7308 * Description: Set the drive state to powered on. 7309 * Someone else is required to actually change the drive 7310 * power level. 7311 * 7312 * Arguments: un - driver soft state (unit) structure 7313 * 7314 * Return Code: DDI_SUCCESS 7315 * 7316 * Context: Kernel thread context 7317 */ 7318 7319 static int 7320 sd_ddi_pm_resume(struct sd_lun *un) 7321 { 7322 ASSERT(un != NULL); 7323 7324 ASSERT(!mutex_owned(SD_MUTEX(un))); 7325 mutex_enter(SD_MUTEX(un)); 7326 un->un_power_level = SD_SPINDLE_ON; 7327 7328 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7329 mutex_enter(&un->un_pm_mutex); 7330 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7331 un->un_pm_count++; 7332 ASSERT(un->un_pm_count == 0); 7333 /* 7334 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7335 * un_suspend_cv is for a system resume, not a power management 7336 * device resume. (4297749) 7337 * cv_broadcast(&un->un_suspend_cv); 7338 */ 7339 } 7340 mutex_exit(&un->un_pm_mutex); 7341 mutex_exit(SD_MUTEX(un)); 7342 7343 return (DDI_SUCCESS); 7344 } 7345 7346 7347 /* 7348 * Function: sd_pm_idletimeout_handler 7349 * 7350 * Description: A timer routine that's active only while a device is busy. 7351 * The purpose is to extend slightly the pm framework's busy 7352 * view of the device to prevent busy/idle thrashing for 7353 * back-to-back commands. Do this by comparing the current time 7354 * to the time at which the last command completed and when the 7355 * difference is greater than sd_pm_idletime, call 7356 * pm_idle_component. In addition to indicating idle to the pm 7357 * framework, update the chain type to again use the internal pm 7358 * layers of the driver. 7359 * 7360 * Arguments: arg - driver soft state (unit) structure 7361 * 7362 * Context: Executes in a timeout(9F) thread context 7363 */ 7364 7365 static void 7366 sd_pm_idletimeout_handler(void *arg) 7367 { 7368 struct sd_lun *un = arg; 7369 7370 time_t now; 7371 7372 mutex_enter(&sd_detach_mutex); 7373 if (un->un_detach_count != 0) { 7374 /* Abort if the instance is detaching */ 7375 mutex_exit(&sd_detach_mutex); 7376 return; 7377 } 7378 mutex_exit(&sd_detach_mutex); 7379 7380 now = ddi_get_time(); 7381 /* 7382 * Grab both mutexes, in the proper order, since we're accessing 7383 * both PM and softstate variables. 7384 */ 7385 mutex_enter(SD_MUTEX(un)); 7386 mutex_enter(&un->un_pm_mutex); 7387 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7388 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7389 /* 7390 * Update the chain types. 7391 * This takes affect on the next new command received. 7392 */ 7393 if (un->un_f_non_devbsize_supported) { 7394 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7395 } else { 7396 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7397 } 7398 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7399 7400 SD_TRACE(SD_LOG_IO_PM, un, 7401 "sd_pm_idletimeout_handler: idling device\n"); 7402 (void) pm_idle_component(SD_DEVINFO(un), 0); 7403 un->un_pm_idle_timeid = NULL; 7404 } else { 7405 un->un_pm_idle_timeid = 7406 timeout(sd_pm_idletimeout_handler, un, 7407 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7408 } 7409 mutex_exit(&un->un_pm_mutex); 7410 mutex_exit(SD_MUTEX(un)); 7411 } 7412 7413 7414 /* 7415 * Function: sd_pm_timeout_handler 7416 * 7417 * Description: Callback to tell framework we are idle. 7418 * 7419 * Context: timeout(9f) thread context. 7420 */ 7421 7422 static void 7423 sd_pm_timeout_handler(void *arg) 7424 { 7425 struct sd_lun *un = arg; 7426 7427 (void) pm_idle_component(SD_DEVINFO(un), 0); 7428 mutex_enter(&un->un_pm_mutex); 7429 un->un_pm_timeid = NULL; 7430 mutex_exit(&un->un_pm_mutex); 7431 } 7432 7433 7434 /* 7435 * Function: sdpower 7436 * 7437 * Description: PM entry point. 7438 * 7439 * Return Code: DDI_SUCCESS 7440 * DDI_FAILURE 7441 * 7442 * Context: Kernel thread context 7443 */ 7444 7445 static int 7446 sdpower(dev_info_t *devi, int component, int level) 7447 { 7448 struct sd_lun *un; 7449 int instance; 7450 int rval = DDI_SUCCESS; 7451 uint_t i, log_page_size, maxcycles, ncycles; 7452 uchar_t *log_page_data; 7453 int log_sense_page; 7454 int medium_present; 7455 time_t intvlp; 7456 dev_t dev; 7457 struct pm_trans_data sd_pm_tran_data; 7458 uchar_t save_state; 7459 int sval; 7460 uchar_t state_before_pm; 7461 int got_semaphore_here; 7462 7463 instance = ddi_get_instance(devi); 7464 7465 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7466 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7467 component != 0) { 7468 return (DDI_FAILURE); 7469 } 7470 7471 dev = sd_make_device(SD_DEVINFO(un)); 7472 7473 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7474 7475 /* 7476 * Must synchronize power down with close. 7477 * Attempt to decrement/acquire the open/close semaphore, 7478 * but do NOT wait on it. If it's not greater than zero, 7479 * ie. it can't be decremented without waiting, then 7480 * someone else, either open or close, already has it 7481 * and the try returns 0. Use that knowledge here to determine 7482 * if it's OK to change the device power level. 7483 * Also, only increment it on exit if it was decremented, ie. gotten, 7484 * here. 7485 */ 7486 got_semaphore_here = sema_tryp(&un->un_semoclose); 7487 7488 mutex_enter(SD_MUTEX(un)); 7489 7490 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7491 un->un_ncmds_in_driver); 7492 7493 /* 7494 * If un_ncmds_in_driver is non-zero it indicates commands are 7495 * already being processed in the driver, or if the semaphore was 7496 * not gotten here it indicates an open or close is being processed. 7497 * At the same time somebody is requesting to go low power which 7498 * can't happen, therefore we need to return failure. 7499 */ 7500 if ((level == SD_SPINDLE_OFF) && 7501 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7502 mutex_exit(SD_MUTEX(un)); 7503 7504 if (got_semaphore_here != 0) { 7505 sema_v(&un->un_semoclose); 7506 } 7507 SD_TRACE(SD_LOG_IO_PM, un, 7508 "sdpower: exit, device has queued cmds.\n"); 7509 return (DDI_FAILURE); 7510 } 7511 7512 /* 7513 * if it is OFFLINE that means the disk is completely dead 7514 * in our case we have to put the disk in on or off by sending commands 7515 * Of course that will fail anyway so return back here. 7516 * 7517 * Power changes to a device that's OFFLINE or SUSPENDED 7518 * are not allowed. 7519 */ 7520 if ((un->un_state == SD_STATE_OFFLINE) || 7521 (un->un_state == SD_STATE_SUSPENDED)) { 7522 mutex_exit(SD_MUTEX(un)); 7523 7524 if (got_semaphore_here != 0) { 7525 sema_v(&un->un_semoclose); 7526 } 7527 SD_TRACE(SD_LOG_IO_PM, un, 7528 "sdpower: exit, device is off-line.\n"); 7529 return (DDI_FAILURE); 7530 } 7531 7532 /* 7533 * Change the device's state to indicate it's power level 7534 * is being changed. Do this to prevent a power off in the 7535 * middle of commands, which is especially bad on devices 7536 * that are really powered off instead of just spun down. 7537 */ 7538 state_before_pm = un->un_state; 7539 un->un_state = SD_STATE_PM_CHANGING; 7540 7541 mutex_exit(SD_MUTEX(un)); 7542 7543 /* 7544 * If "pm-capable" property is set to TRUE by HBA drivers, 7545 * bypass the following checking, otherwise, check the log 7546 * sense information for this device 7547 */ 7548 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7549 /* 7550 * Get the log sense information to understand whether the 7551 * the powercycle counts have gone beyond the threshhold. 7552 */ 7553 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7554 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7555 7556 mutex_enter(SD_MUTEX(un)); 7557 log_sense_page = un->un_start_stop_cycle_page; 7558 mutex_exit(SD_MUTEX(un)); 7559 7560 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7561 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7562 #ifdef SDDEBUG 7563 if (sd_force_pm_supported) { 7564 /* Force a successful result */ 7565 rval = 0; 7566 } 7567 #endif 7568 if (rval != 0) { 7569 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7570 "Log Sense Failed\n"); 7571 kmem_free(log_page_data, log_page_size); 7572 /* Cannot support power management on those drives */ 7573 7574 if (got_semaphore_here != 0) { 7575 sema_v(&un->un_semoclose); 7576 } 7577 /* 7578 * On exit put the state back to it's original value 7579 * and broadcast to anyone waiting for the power 7580 * change completion. 7581 */ 7582 mutex_enter(SD_MUTEX(un)); 7583 un->un_state = state_before_pm; 7584 cv_broadcast(&un->un_suspend_cv); 7585 mutex_exit(SD_MUTEX(un)); 7586 SD_TRACE(SD_LOG_IO_PM, un, 7587 "sdpower: exit, Log Sense Failed.\n"); 7588 return (DDI_FAILURE); 7589 } 7590 7591 /* 7592 * From the page data - Convert the essential information to 7593 * pm_trans_data 7594 */ 7595 maxcycles = 7596 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7597 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7598 7599 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7600 7601 ncycles = 7602 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7603 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7604 7605 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7606 7607 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7608 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7609 log_page_data[8+i]; 7610 } 7611 7612 kmem_free(log_page_data, log_page_size); 7613 7614 /* 7615 * Call pm_trans_check routine to get the Ok from 7616 * the global policy 7617 */ 7618 7619 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7620 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7621 7622 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7623 #ifdef SDDEBUG 7624 if (sd_force_pm_supported) { 7625 /* Force a successful result */ 7626 rval = 1; 7627 } 7628 #endif 7629 switch (rval) { 7630 case 0: 7631 /* 7632 * Not Ok to Power cycle or error in parameters passed 7633 * Would have given the advised time to consider power 7634 * cycle. Based on the new intvlp parameter we are 7635 * supposed to pretend we are busy so that pm framework 7636 * will never call our power entry point. Because of 7637 * that install a timeout handler and wait for the 7638 * recommended time to elapse so that power management 7639 * can be effective again. 7640 * 7641 * To effect this behavior, call pm_busy_component to 7642 * indicate to the framework this device is busy. 7643 * By not adjusting un_pm_count the rest of PM in 7644 * the driver will function normally, and independant 7645 * of this but because the framework is told the device 7646 * is busy it won't attempt powering down until it gets 7647 * a matching idle. The timeout handler sends this. 7648 * Note: sd_pm_entry can't be called here to do this 7649 * because sdpower may have been called as a result 7650 * of a call to pm_raise_power from within sd_pm_entry. 7651 * 7652 * If a timeout handler is already active then 7653 * don't install another. 7654 */ 7655 mutex_enter(&un->un_pm_mutex); 7656 if (un->un_pm_timeid == NULL) { 7657 un->un_pm_timeid = 7658 timeout(sd_pm_timeout_handler, 7659 un, intvlp * drv_usectohz(1000000)); 7660 mutex_exit(&un->un_pm_mutex); 7661 (void) pm_busy_component(SD_DEVINFO(un), 0); 7662 } else { 7663 mutex_exit(&un->un_pm_mutex); 7664 } 7665 if (got_semaphore_here != 0) { 7666 sema_v(&un->un_semoclose); 7667 } 7668 /* 7669 * On exit put the state back to it's original value 7670 * and broadcast to anyone waiting for the power 7671 * change completion. 7672 */ 7673 mutex_enter(SD_MUTEX(un)); 7674 un->un_state = state_before_pm; 7675 cv_broadcast(&un->un_suspend_cv); 7676 mutex_exit(SD_MUTEX(un)); 7677 7678 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7679 "trans check Failed, not ok to power cycle.\n"); 7680 return (DDI_FAILURE); 7681 7682 case -1: 7683 if (got_semaphore_here != 0) { 7684 sema_v(&un->un_semoclose); 7685 } 7686 /* 7687 * On exit put the state back to it's original value 7688 * and broadcast to anyone waiting for the power 7689 * change completion. 7690 */ 7691 mutex_enter(SD_MUTEX(un)); 7692 un->un_state = state_before_pm; 7693 cv_broadcast(&un->un_suspend_cv); 7694 mutex_exit(SD_MUTEX(un)); 7695 SD_TRACE(SD_LOG_IO_PM, un, 7696 "sdpower: exit, trans check command Failed.\n"); 7697 return (DDI_FAILURE); 7698 } 7699 } 7700 7701 if (level == SD_SPINDLE_OFF) { 7702 /* 7703 * Save the last state... if the STOP FAILS we need it 7704 * for restoring 7705 */ 7706 mutex_enter(SD_MUTEX(un)); 7707 save_state = un->un_last_state; 7708 /* 7709 * There must not be any cmds. getting processed 7710 * in the driver when we get here. Power to the 7711 * device is potentially going off. 7712 */ 7713 ASSERT(un->un_ncmds_in_driver == 0); 7714 mutex_exit(SD_MUTEX(un)); 7715 7716 /* 7717 * For now suspend the device completely before spindle is 7718 * turned off 7719 */ 7720 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7721 if (got_semaphore_here != 0) { 7722 sema_v(&un->un_semoclose); 7723 } 7724 /* 7725 * On exit put the state back to it's original value 7726 * and broadcast to anyone waiting for the power 7727 * change completion. 7728 */ 7729 mutex_enter(SD_MUTEX(un)); 7730 un->un_state = state_before_pm; 7731 cv_broadcast(&un->un_suspend_cv); 7732 mutex_exit(SD_MUTEX(un)); 7733 SD_TRACE(SD_LOG_IO_PM, un, 7734 "sdpower: exit, PM suspend Failed.\n"); 7735 return (DDI_FAILURE); 7736 } 7737 } 7738 7739 /* 7740 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7741 * close, or strategy. Dump no long uses this routine, it uses it's 7742 * own code so it can be done in polled mode. 7743 */ 7744 7745 medium_present = TRUE; 7746 7747 /* 7748 * When powering up, issue a TUR in case the device is at unit 7749 * attention. Don't do retries. Bypass the PM layer, otherwise 7750 * a deadlock on un_pm_busy_cv will occur. 7751 */ 7752 if (level == SD_SPINDLE_ON) { 7753 (void) sd_send_scsi_TEST_UNIT_READY(un, 7754 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7755 } 7756 7757 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7758 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7759 7760 sval = sd_send_scsi_START_STOP_UNIT(un, 7761 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7762 SD_PATH_DIRECT); 7763 /* Command failed, check for media present. */ 7764 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7765 medium_present = FALSE; 7766 } 7767 7768 /* 7769 * The conditions of interest here are: 7770 * if a spindle off with media present fails, 7771 * then restore the state and return an error. 7772 * else if a spindle on fails, 7773 * then return an error (there's no state to restore). 7774 * In all other cases we setup for the new state 7775 * and return success. 7776 */ 7777 switch (level) { 7778 case SD_SPINDLE_OFF: 7779 if ((medium_present == TRUE) && (sval != 0)) { 7780 /* The stop command from above failed */ 7781 rval = DDI_FAILURE; 7782 /* 7783 * The stop command failed, and we have media 7784 * present. Put the level back by calling the 7785 * sd_pm_resume() and set the state back to 7786 * it's previous value. 7787 */ 7788 (void) sd_ddi_pm_resume(un); 7789 mutex_enter(SD_MUTEX(un)); 7790 un->un_last_state = save_state; 7791 mutex_exit(SD_MUTEX(un)); 7792 break; 7793 } 7794 /* 7795 * The stop command from above succeeded. 7796 */ 7797 if (un->un_f_monitor_media_state) { 7798 /* 7799 * Terminate watch thread in case of removable media 7800 * devices going into low power state. This is as per 7801 * the requirements of pm framework, otherwise commands 7802 * will be generated for the device (through watch 7803 * thread), even when the device is in low power state. 7804 */ 7805 mutex_enter(SD_MUTEX(un)); 7806 un->un_f_watcht_stopped = FALSE; 7807 if (un->un_swr_token != NULL) { 7808 opaque_t temp_token = un->un_swr_token; 7809 un->un_f_watcht_stopped = TRUE; 7810 un->un_swr_token = NULL; 7811 mutex_exit(SD_MUTEX(un)); 7812 (void) scsi_watch_request_terminate(temp_token, 7813 SCSI_WATCH_TERMINATE_WAIT); 7814 } else { 7815 mutex_exit(SD_MUTEX(un)); 7816 } 7817 } 7818 break; 7819 7820 default: /* The level requested is spindle on... */ 7821 /* 7822 * Legacy behavior: return success on a failed spinup 7823 * if there is no media in the drive. 7824 * Do this by looking at medium_present here. 7825 */ 7826 if ((sval != 0) && medium_present) { 7827 /* The start command from above failed */ 7828 rval = DDI_FAILURE; 7829 break; 7830 } 7831 /* 7832 * The start command from above succeeded 7833 * Resume the devices now that we have 7834 * started the disks 7835 */ 7836 (void) sd_ddi_pm_resume(un); 7837 7838 /* 7839 * Resume the watch thread since it was suspended 7840 * when the device went into low power mode. 7841 */ 7842 if (un->un_f_monitor_media_state) { 7843 mutex_enter(SD_MUTEX(un)); 7844 if (un->un_f_watcht_stopped == TRUE) { 7845 opaque_t temp_token; 7846 7847 un->un_f_watcht_stopped = FALSE; 7848 mutex_exit(SD_MUTEX(un)); 7849 temp_token = scsi_watch_request_submit( 7850 SD_SCSI_DEVP(un), 7851 sd_check_media_time, 7852 SENSE_LENGTH, sd_media_watch_cb, 7853 (caddr_t)dev); 7854 mutex_enter(SD_MUTEX(un)); 7855 un->un_swr_token = temp_token; 7856 } 7857 mutex_exit(SD_MUTEX(un)); 7858 } 7859 } 7860 if (got_semaphore_here != 0) { 7861 sema_v(&un->un_semoclose); 7862 } 7863 /* 7864 * On exit put the state back to it's original value 7865 * and broadcast to anyone waiting for the power 7866 * change completion. 7867 */ 7868 mutex_enter(SD_MUTEX(un)); 7869 un->un_state = state_before_pm; 7870 cv_broadcast(&un->un_suspend_cv); 7871 mutex_exit(SD_MUTEX(un)); 7872 7873 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7874 7875 return (rval); 7876 } 7877 7878 7879 7880 /* 7881 * Function: sdattach 7882 * 7883 * Description: Driver's attach(9e) entry point function. 7884 * 7885 * Arguments: devi - opaque device info handle 7886 * cmd - attach type 7887 * 7888 * Return Code: DDI_SUCCESS 7889 * DDI_FAILURE 7890 * 7891 * Context: Kernel thread context 7892 */ 7893 7894 static int 7895 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7896 { 7897 switch (cmd) { 7898 case DDI_ATTACH: 7899 return (sd_unit_attach(devi)); 7900 case DDI_RESUME: 7901 return (sd_ddi_resume(devi)); 7902 default: 7903 break; 7904 } 7905 return (DDI_FAILURE); 7906 } 7907 7908 7909 /* 7910 * Function: sddetach 7911 * 7912 * Description: Driver's detach(9E) entry point function. 7913 * 7914 * Arguments: devi - opaque device info handle 7915 * cmd - detach type 7916 * 7917 * Return Code: DDI_SUCCESS 7918 * DDI_FAILURE 7919 * 7920 * Context: Kernel thread context 7921 */ 7922 7923 static int 7924 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7925 { 7926 switch (cmd) { 7927 case DDI_DETACH: 7928 return (sd_unit_detach(devi)); 7929 case DDI_SUSPEND: 7930 return (sd_ddi_suspend(devi)); 7931 default: 7932 break; 7933 } 7934 return (DDI_FAILURE); 7935 } 7936 7937 7938 /* 7939 * Function: sd_sync_with_callback 7940 * 7941 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7942 * state while the callback routine is active. 7943 * 7944 * Arguments: un: softstate structure for the instance 7945 * 7946 * Context: Kernel thread context 7947 */ 7948 7949 static void 7950 sd_sync_with_callback(struct sd_lun *un) 7951 { 7952 ASSERT(un != NULL); 7953 7954 mutex_enter(SD_MUTEX(un)); 7955 7956 ASSERT(un->un_in_callback >= 0); 7957 7958 while (un->un_in_callback > 0) { 7959 mutex_exit(SD_MUTEX(un)); 7960 delay(2); 7961 mutex_enter(SD_MUTEX(un)); 7962 } 7963 7964 mutex_exit(SD_MUTEX(un)); 7965 } 7966 7967 /* 7968 * Function: sd_unit_attach 7969 * 7970 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7971 * the soft state structure for the device and performs 7972 * all necessary structure and device initializations. 7973 * 7974 * Arguments: devi: the system's dev_info_t for the device. 7975 * 7976 * Return Code: DDI_SUCCESS if attach is successful. 7977 * DDI_FAILURE if any part of the attach fails. 7978 * 7979 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7980 * Kernel thread context only. Can sleep. 7981 */ 7982 7983 static int 7984 sd_unit_attach(dev_info_t *devi) 7985 { 7986 struct scsi_device *devp; 7987 struct sd_lun *un; 7988 char *variantp; 7989 int reservation_flag = SD_TARGET_IS_UNRESERVED; 7990 int instance; 7991 int rval; 7992 int wc_enabled; 7993 int tgt; 7994 uint64_t capacity; 7995 uint_t lbasize; 7996 dev_info_t *pdip = ddi_get_parent(devi); 7997 7998 /* 7999 * Retrieve the target driver's private data area. This was set 8000 * up by the HBA. 8001 */ 8002 devp = ddi_get_driver_private(devi); 8003 8004 /* 8005 * Retrieve the target ID of the device. 8006 */ 8007 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8008 SCSI_ADDR_PROP_TARGET, -1); 8009 8010 /* 8011 * Since we have no idea what state things were left in by the last 8012 * user of the device, set up some 'default' settings, ie. turn 'em 8013 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 8014 * Do this before the scsi_probe, which sends an inquiry. 8015 * This is a fix for bug (4430280). 8016 * Of special importance is wide-xfer. The drive could have been left 8017 * in wide transfer mode by the last driver to communicate with it, 8018 * this includes us. If that's the case, and if the following is not 8019 * setup properly or we don't re-negotiate with the drive prior to 8020 * transferring data to/from the drive, it causes bus parity errors, 8021 * data overruns, and unexpected interrupts. This first occurred when 8022 * the fix for bug (4378686) was made. 8023 */ 8024 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 8025 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 8026 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 8027 8028 /* 8029 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 8030 * on a target. Setting it per lun instance actually sets the 8031 * capability of this target, which affects those luns already 8032 * attached on the same target. So during attach, we can only disable 8033 * this capability only when no other lun has been attached on this 8034 * target. By doing this, we assume a target has the same tagged-qing 8035 * capability for every lun. The condition can be removed when HBA 8036 * is changed to support per lun based tagged-qing capability. 8037 */ 8038 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8039 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 8040 } 8041 8042 /* 8043 * Use scsi_probe() to issue an INQUIRY command to the device. 8044 * This call will allocate and fill in the scsi_inquiry structure 8045 * and point the sd_inq member of the scsi_device structure to it. 8046 * If the attach succeeds, then this memory will not be de-allocated 8047 * (via scsi_unprobe()) until the instance is detached. 8048 */ 8049 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 8050 goto probe_failed; 8051 } 8052 8053 /* 8054 * Check the device type as specified in the inquiry data and 8055 * claim it if it is of a type that we support. 8056 */ 8057 switch (devp->sd_inq->inq_dtype) { 8058 case DTYPE_DIRECT: 8059 break; 8060 case DTYPE_RODIRECT: 8061 break; 8062 case DTYPE_OPTICAL: 8063 break; 8064 case DTYPE_NOTPRESENT: 8065 default: 8066 /* Unsupported device type; fail the attach. */ 8067 goto probe_failed; 8068 } 8069 8070 /* 8071 * Allocate the soft state structure for this unit. 8072 * 8073 * We rely upon this memory being set to all zeroes by 8074 * ddi_soft_state_zalloc(). We assume that any member of the 8075 * soft state structure that is not explicitly initialized by 8076 * this routine will have a value of zero. 8077 */ 8078 instance = ddi_get_instance(devp->sd_dev); 8079 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 8080 goto probe_failed; 8081 } 8082 8083 /* 8084 * Retrieve a pointer to the newly-allocated soft state. 8085 * 8086 * This should NEVER fail if the ddi_soft_state_zalloc() call above 8087 * was successful, unless something has gone horribly wrong and the 8088 * ddi's soft state internals are corrupt (in which case it is 8089 * probably better to halt here than just fail the attach....) 8090 */ 8091 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 8092 panic("sd_unit_attach: NULL soft state on instance:0x%x", 8093 instance); 8094 /*NOTREACHED*/ 8095 } 8096 8097 /* 8098 * Link the back ptr of the driver soft state to the scsi_device 8099 * struct for this lun. 8100 * Save a pointer to the softstate in the driver-private area of 8101 * the scsi_device struct. 8102 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 8103 * we first set un->un_sd below. 8104 */ 8105 un->un_sd = devp; 8106 devp->sd_private = (opaque_t)un; 8107 8108 /* 8109 * The following must be after devp is stored in the soft state struct. 8110 */ 8111 #ifdef SDDEBUG 8112 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8113 "%s_unit_attach: un:0x%p instance:%d\n", 8114 ddi_driver_name(devi), un, instance); 8115 #endif 8116 8117 /* 8118 * Set up the device type and node type (for the minor nodes). 8119 * By default we assume that the device can at least support the 8120 * Common Command Set. Call it a CD-ROM if it reports itself 8121 * as a RODIRECT device. 8122 */ 8123 switch (devp->sd_inq->inq_dtype) { 8124 case DTYPE_RODIRECT: 8125 un->un_node_type = DDI_NT_CD_CHAN; 8126 un->un_ctype = CTYPE_CDROM; 8127 break; 8128 case DTYPE_OPTICAL: 8129 un->un_node_type = DDI_NT_BLOCK_CHAN; 8130 un->un_ctype = CTYPE_ROD; 8131 break; 8132 default: 8133 un->un_node_type = DDI_NT_BLOCK_CHAN; 8134 un->un_ctype = CTYPE_CCS; 8135 break; 8136 } 8137 8138 /* 8139 * Try to read the interconnect type from the HBA. 8140 * 8141 * Note: This driver is currently compiled as two binaries, a parallel 8142 * scsi version (sd) and a fibre channel version (ssd). All functional 8143 * differences are determined at compile time. In the future a single 8144 * binary will be provided and the inteconnect type will be used to 8145 * differentiate between fibre and parallel scsi behaviors. At that time 8146 * it will be necessary for all fibre channel HBAs to support this 8147 * property. 8148 * 8149 * set un_f_is_fiber to TRUE ( default fiber ) 8150 */ 8151 un->un_f_is_fibre = TRUE; 8152 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 8153 case INTERCONNECT_SSA: 8154 un->un_interconnect_type = SD_INTERCONNECT_SSA; 8155 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8156 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 8157 break; 8158 case INTERCONNECT_PARALLEL: 8159 un->un_f_is_fibre = FALSE; 8160 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8161 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8162 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 8163 break; 8164 case INTERCONNECT_SATA: 8165 un->un_f_is_fibre = FALSE; 8166 un->un_interconnect_type = SD_INTERCONNECT_SATA; 8167 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8168 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 8169 break; 8170 case INTERCONNECT_FIBRE: 8171 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 8172 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8173 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 8174 break; 8175 case INTERCONNECT_FABRIC: 8176 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 8177 un->un_node_type = DDI_NT_BLOCK_FABRIC; 8178 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8179 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 8180 break; 8181 default: 8182 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 8183 /* 8184 * The HBA does not support the "interconnect-type" property 8185 * (or did not provide a recognized type). 8186 * 8187 * Note: This will be obsoleted when a single fibre channel 8188 * and parallel scsi driver is delivered. In the meantime the 8189 * interconnect type will be set to the platform default.If that 8190 * type is not parallel SCSI, it means that we should be 8191 * assuming "ssd" semantics. However, here this also means that 8192 * the FC HBA is not supporting the "interconnect-type" property 8193 * like we expect it to, so log this occurrence. 8194 */ 8195 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 8196 if (!SD_IS_PARALLEL_SCSI(un)) { 8197 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8198 "sd_unit_attach: un:0x%p Assuming " 8199 "INTERCONNECT_FIBRE\n", un); 8200 } else { 8201 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8202 "sd_unit_attach: un:0x%p Assuming " 8203 "INTERCONNECT_PARALLEL\n", un); 8204 un->un_f_is_fibre = FALSE; 8205 } 8206 #else 8207 /* 8208 * Note: This source will be implemented when a single fibre 8209 * channel and parallel scsi driver is delivered. The default 8210 * will be to assume that if a device does not support the 8211 * "interconnect-type" property it is a parallel SCSI HBA and 8212 * we will set the interconnect type for parallel scsi. 8213 */ 8214 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8215 un->un_f_is_fibre = FALSE; 8216 #endif 8217 break; 8218 } 8219 8220 if (un->un_f_is_fibre == TRUE) { 8221 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 8222 SCSI_VERSION_3) { 8223 switch (un->un_interconnect_type) { 8224 case SD_INTERCONNECT_FIBRE: 8225 case SD_INTERCONNECT_SSA: 8226 un->un_node_type = DDI_NT_BLOCK_WWN; 8227 break; 8228 default: 8229 break; 8230 } 8231 } 8232 } 8233 8234 /* 8235 * Initialize the Request Sense command for the target 8236 */ 8237 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 8238 goto alloc_rqs_failed; 8239 } 8240 8241 /* 8242 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 8243 * with seperate binary for sd and ssd. 8244 * 8245 * x86 has 1 binary, un_retry_count is set base on connection type. 8246 * The hardcoded values will go away when Sparc uses 1 binary 8247 * for sd and ssd. This hardcoded values need to match 8248 * SD_RETRY_COUNT in sddef.h 8249 * The value used is base on interconnect type. 8250 * fibre = 3, parallel = 5 8251 */ 8252 #if defined(__i386) || defined(__amd64) 8253 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 8254 #else 8255 un->un_retry_count = SD_RETRY_COUNT; 8256 #endif 8257 8258 /* 8259 * Set the per disk retry count to the default number of retries 8260 * for disks and CDROMs. This value can be overridden by the 8261 * disk property list or an entry in sd.conf. 8262 */ 8263 un->un_notready_retry_count = 8264 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 8265 : DISK_NOT_READY_RETRY_COUNT(un); 8266 8267 /* 8268 * Set the busy retry count to the default value of un_retry_count. 8269 * This can be overridden by entries in sd.conf or the device 8270 * config table. 8271 */ 8272 un->un_busy_retry_count = un->un_retry_count; 8273 8274 /* 8275 * Init the reset threshold for retries. This number determines 8276 * how many retries must be performed before a reset can be issued 8277 * (for certain error conditions). This can be overridden by entries 8278 * in sd.conf or the device config table. 8279 */ 8280 un->un_reset_retry_count = (un->un_retry_count / 2); 8281 8282 /* 8283 * Set the victim_retry_count to the default un_retry_count 8284 */ 8285 un->un_victim_retry_count = (2 * un->un_retry_count); 8286 8287 /* 8288 * Set the reservation release timeout to the default value of 8289 * 5 seconds. This can be overridden by entries in ssd.conf or the 8290 * device config table. 8291 */ 8292 un->un_reserve_release_time = 5; 8293 8294 /* 8295 * Set up the default maximum transfer size. Note that this may 8296 * get updated later in the attach, when setting up default wide 8297 * operations for disks. 8298 */ 8299 #if defined(__i386) || defined(__amd64) 8300 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 8301 #else 8302 un->un_max_xfer_size = (uint_t)maxphys; 8303 #endif 8304 8305 /* 8306 * Get "allow bus device reset" property (defaults to "enabled" if 8307 * the property was not defined). This is to disable bus resets for 8308 * certain kinds of error recovery. Note: In the future when a run-time 8309 * fibre check is available the soft state flag should default to 8310 * enabled. 8311 */ 8312 if (un->un_f_is_fibre == TRUE) { 8313 un->un_f_allow_bus_device_reset = TRUE; 8314 } else { 8315 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8316 "allow-bus-device-reset", 1) != 0) { 8317 un->un_f_allow_bus_device_reset = TRUE; 8318 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8319 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 8320 un); 8321 } else { 8322 un->un_f_allow_bus_device_reset = FALSE; 8323 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8324 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 8325 un); 8326 } 8327 } 8328 8329 /* 8330 * Check if this is an ATAPI device. ATAPI devices use Group 1 8331 * Read/Write commands and Group 2 Mode Sense/Select commands. 8332 * 8333 * Note: The "obsolete" way of doing this is to check for the "atapi" 8334 * property. The new "variant" property with a value of "atapi" has been 8335 * introduced so that future 'variants' of standard SCSI behavior (like 8336 * atapi) could be specified by the underlying HBA drivers by supplying 8337 * a new value for the "variant" property, instead of having to define a 8338 * new property. 8339 */ 8340 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 8341 un->un_f_cfg_is_atapi = TRUE; 8342 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8343 "sd_unit_attach: un:0x%p Atapi device\n", un); 8344 } 8345 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8346 &variantp) == DDI_PROP_SUCCESS) { 8347 if (strcmp(variantp, "atapi") == 0) { 8348 un->un_f_cfg_is_atapi = TRUE; 8349 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8350 "sd_unit_attach: un:0x%p Atapi device\n", un); 8351 } 8352 ddi_prop_free(variantp); 8353 } 8354 8355 un->un_cmd_timeout = SD_IO_TIME; 8356 8357 /* Info on current states, statuses, etc. (Updated frequently) */ 8358 un->un_state = SD_STATE_NORMAL; 8359 un->un_last_state = SD_STATE_NORMAL; 8360 8361 /* Control & status info for command throttling */ 8362 un->un_throttle = sd_max_throttle; 8363 un->un_saved_throttle = sd_max_throttle; 8364 un->un_min_throttle = sd_min_throttle; 8365 8366 if (un->un_f_is_fibre == TRUE) { 8367 un->un_f_use_adaptive_throttle = TRUE; 8368 } else { 8369 un->un_f_use_adaptive_throttle = FALSE; 8370 } 8371 8372 /* Removable media support. */ 8373 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8374 un->un_mediastate = DKIO_NONE; 8375 un->un_specified_mediastate = DKIO_NONE; 8376 8377 /* CVs for suspend/resume (PM or DR) */ 8378 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8379 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8380 8381 /* Power management support. */ 8382 un->un_power_level = SD_SPINDLE_UNINIT; 8383 8384 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8385 un->un_f_wcc_inprog = 0; 8386 8387 /* 8388 * The open/close semaphore is used to serialize threads executing 8389 * in the driver's open & close entry point routines for a given 8390 * instance. 8391 */ 8392 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8393 8394 /* 8395 * The conf file entry and softstate variable is a forceful override, 8396 * meaning a non-zero value must be entered to change the default. 8397 */ 8398 un->un_f_disksort_disabled = FALSE; 8399 8400 /* 8401 * Retrieve the properties from the static driver table or the driver 8402 * configuration file (.conf) for this unit and update the soft state 8403 * for the device as needed for the indicated properties. 8404 * Note: the property configuration needs to occur here as some of the 8405 * following routines may have dependancies on soft state flags set 8406 * as part of the driver property configuration. 8407 */ 8408 sd_read_unit_properties(un); 8409 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8410 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8411 8412 /* 8413 * Only if a device has "hotpluggable" property, it is 8414 * treated as hotpluggable device. Otherwise, it is 8415 * regarded as non-hotpluggable one. 8416 */ 8417 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8418 -1) != -1) { 8419 un->un_f_is_hotpluggable = TRUE; 8420 } 8421 8422 /* 8423 * set unit's attributes(flags) according to "hotpluggable" and 8424 * RMB bit in INQUIRY data. 8425 */ 8426 sd_set_unit_attributes(un, devi); 8427 8428 /* 8429 * By default, we mark the capacity, lbasize, and geometry 8430 * as invalid. Only if we successfully read a valid capacity 8431 * will we update the un_blockcount and un_tgt_blocksize with the 8432 * valid values (the geometry will be validated later). 8433 */ 8434 un->un_f_blockcount_is_valid = FALSE; 8435 un->un_f_tgt_blocksize_is_valid = FALSE; 8436 un->un_f_geometry_is_valid = FALSE; 8437 8438 /* 8439 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8440 * otherwise. 8441 */ 8442 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8443 un->un_blockcount = 0; 8444 8445 /* 8446 * Set up the per-instance info needed to determine the correct 8447 * CDBs and other info for issuing commands to the target. 8448 */ 8449 sd_init_cdb_limits(un); 8450 8451 /* 8452 * Set up the IO chains to use, based upon the target type. 8453 */ 8454 if (un->un_f_non_devbsize_supported) { 8455 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8456 } else { 8457 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8458 } 8459 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8460 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8461 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8462 8463 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8464 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8465 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8466 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8467 8468 8469 if (ISCD(un)) { 8470 un->un_additional_codes = sd_additional_codes; 8471 } else { 8472 un->un_additional_codes = NULL; 8473 } 8474 8475 /* 8476 * Create the kstats here so they can be available for attach-time 8477 * routines that send commands to the unit (either polled or via 8478 * sd_send_scsi_cmd). 8479 * 8480 * Note: This is a critical sequence that needs to be maintained: 8481 * 1) Instantiate the kstats here, before any routines using the 8482 * iopath (i.e. sd_send_scsi_cmd). 8483 * 2) Instantiate and initialize the partition stats 8484 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 8485 * see detailed comments there. 8486 * 3) Initialize the error stats (sd_set_errstats), following 8487 * sd_validate_geometry(),sd_register_devid(), 8488 * and sd_cache_control(). 8489 */ 8490 8491 un->un_stats = kstat_create(sd_label, instance, 8492 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8493 if (un->un_stats != NULL) { 8494 un->un_stats->ks_lock = SD_MUTEX(un); 8495 kstat_install(un->un_stats); 8496 } 8497 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8498 "sd_unit_attach: un:0x%p un_stats created\n", un); 8499 8500 sd_create_errstats(un, instance); 8501 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8502 "sd_unit_attach: un:0x%p errstats created\n", un); 8503 8504 /* 8505 * The following if/else code was relocated here from below as part 8506 * of the fix for bug (4430280). However with the default setup added 8507 * on entry to this routine, it's no longer absolutely necessary for 8508 * this to be before the call to sd_spin_up_unit. 8509 */ 8510 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 8511 /* 8512 * If SCSI-2 tagged queueing is supported by the target 8513 * and by the host adapter then we will enable it. 8514 */ 8515 un->un_tagflags = 0; 8516 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8517 (devp->sd_inq->inq_cmdque) && 8518 (un->un_f_arq_enabled == TRUE)) { 8519 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8520 1, 1) == 1) { 8521 un->un_tagflags = FLAG_STAG; 8522 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8523 "sd_unit_attach: un:0x%p tag queueing " 8524 "enabled\n", un); 8525 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8526 "untagged-qing", 0) == 1) { 8527 un->un_f_opt_queueing = TRUE; 8528 un->un_saved_throttle = un->un_throttle = 8529 min(un->un_throttle, 3); 8530 } else { 8531 un->un_f_opt_queueing = FALSE; 8532 un->un_saved_throttle = un->un_throttle = 1; 8533 } 8534 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8535 == 1) && (un->un_f_arq_enabled == TRUE)) { 8536 /* The Host Adapter supports internal queueing. */ 8537 un->un_f_opt_queueing = TRUE; 8538 un->un_saved_throttle = un->un_throttle = 8539 min(un->un_throttle, 3); 8540 } else { 8541 un->un_f_opt_queueing = FALSE; 8542 un->un_saved_throttle = un->un_throttle = 1; 8543 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8544 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8545 } 8546 8547 /* 8548 * Enable large transfers for SATA/SAS drives 8549 */ 8550 if (SD_IS_SERIAL(un)) { 8551 un->un_max_xfer_size = 8552 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8553 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8554 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8555 "sd_unit_attach: un:0x%p max transfer " 8556 "size=0x%x\n", un, un->un_max_xfer_size); 8557 8558 } 8559 8560 /* Setup or tear down default wide operations for disks */ 8561 8562 /* 8563 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8564 * and "ssd_max_xfer_size" to exist simultaneously on the same 8565 * system and be set to different values. In the future this 8566 * code may need to be updated when the ssd module is 8567 * obsoleted and removed from the system. (4299588) 8568 */ 8569 if (SD_IS_PARALLEL_SCSI(un) && 8570 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 8571 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8572 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8573 1, 1) == 1) { 8574 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8575 "sd_unit_attach: un:0x%p Wide Transfer " 8576 "enabled\n", un); 8577 } 8578 8579 /* 8580 * If tagged queuing has also been enabled, then 8581 * enable large xfers 8582 */ 8583 if (un->un_saved_throttle == sd_max_throttle) { 8584 un->un_max_xfer_size = 8585 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8586 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8587 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8588 "sd_unit_attach: un:0x%p max transfer " 8589 "size=0x%x\n", un, un->un_max_xfer_size); 8590 } 8591 } else { 8592 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8593 0, 1) == 1) { 8594 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8595 "sd_unit_attach: un:0x%p " 8596 "Wide Transfer disabled\n", un); 8597 } 8598 } 8599 } else { 8600 un->un_tagflags = FLAG_STAG; 8601 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8602 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8603 } 8604 8605 /* 8606 * If this target supports LUN reset, try to enable it. 8607 */ 8608 if (un->un_f_lun_reset_enabled) { 8609 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8610 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8611 "un:0x%p lun_reset capability set\n", un); 8612 } else { 8613 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8614 "un:0x%p lun-reset capability not set\n", un); 8615 } 8616 } 8617 8618 /* 8619 * At this point in the attach, we have enough info in the 8620 * soft state to be able to issue commands to the target. 8621 * 8622 * All command paths used below MUST issue their commands as 8623 * SD_PATH_DIRECT. This is important as intermediate layers 8624 * are not all initialized yet (such as PM). 8625 */ 8626 8627 /* 8628 * Send a TEST UNIT READY command to the device. This should clear 8629 * any outstanding UNIT ATTENTION that may be present. 8630 * 8631 * Note: Don't check for success, just track if there is a reservation, 8632 * this is a throw away command to clear any unit attentions. 8633 * 8634 * Note: This MUST be the first command issued to the target during 8635 * attach to ensure power on UNIT ATTENTIONS are cleared. 8636 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8637 * with attempts at spinning up a device with no media. 8638 */ 8639 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8640 reservation_flag = SD_TARGET_IS_RESERVED; 8641 } 8642 8643 /* 8644 * If the device is NOT a removable media device, attempt to spin 8645 * it up (using the START_STOP_UNIT command) and read its capacity 8646 * (using the READ CAPACITY command). Note, however, that either 8647 * of these could fail and in some cases we would continue with 8648 * the attach despite the failure (see below). 8649 */ 8650 if (un->un_f_descr_format_supported) { 8651 switch (sd_spin_up_unit(un)) { 8652 case 0: 8653 /* 8654 * Spin-up was successful; now try to read the 8655 * capacity. If successful then save the results 8656 * and mark the capacity & lbasize as valid. 8657 */ 8658 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8659 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8660 8661 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8662 &lbasize, SD_PATH_DIRECT)) { 8663 case 0: { 8664 if (capacity > DK_MAX_BLOCKS) { 8665 #ifdef _LP64 8666 if (capacity + 1 > 8667 SD_GROUP1_MAX_ADDRESS) { 8668 /* 8669 * Enable descriptor format 8670 * sense data so that we can 8671 * get 64 bit sense data 8672 * fields. 8673 */ 8674 sd_enable_descr_sense(un); 8675 } 8676 #else 8677 /* 32-bit kernels can't handle this */ 8678 scsi_log(SD_DEVINFO(un), 8679 sd_label, CE_WARN, 8680 "disk has %llu blocks, which " 8681 "is too large for a 32-bit " 8682 "kernel", capacity); 8683 8684 #if defined(__i386) || defined(__amd64) 8685 /* 8686 * Refer to comments related to off-by-1 8687 * at the header of this file. 8688 * 1TB disk was treated as (1T - 512)B 8689 * in the past, so that it might has 8690 * valid VTOC and solaris partitions, 8691 * we have to allow it to continue to 8692 * work. 8693 */ 8694 if (capacity -1 > DK_MAX_BLOCKS) 8695 #endif 8696 goto spinup_failed; 8697 #endif 8698 } 8699 8700 /* 8701 * Here it's not necessary to check the case: 8702 * the capacity of the device is bigger than 8703 * what the max hba cdb can support. Because 8704 * sd_send_scsi_READ_CAPACITY will retrieve 8705 * the capacity by sending USCSI command, which 8706 * is constrained by the max hba cdb. Actually, 8707 * sd_send_scsi_READ_CAPACITY will return 8708 * EINVAL when using bigger cdb than required 8709 * cdb length. Will handle this case in 8710 * "case EINVAL". 8711 */ 8712 8713 /* 8714 * The following relies on 8715 * sd_send_scsi_READ_CAPACITY never 8716 * returning 0 for capacity and/or lbasize. 8717 */ 8718 sd_update_block_info(un, lbasize, capacity); 8719 8720 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8721 "sd_unit_attach: un:0x%p capacity = %ld " 8722 "blocks; lbasize= %ld.\n", un, 8723 un->un_blockcount, un->un_tgt_blocksize); 8724 8725 break; 8726 } 8727 case EINVAL: 8728 /* 8729 * In the case where the max-cdb-length property 8730 * is smaller than the required CDB length for 8731 * a SCSI device, a target driver can fail to 8732 * attach to that device. 8733 */ 8734 scsi_log(SD_DEVINFO(un), 8735 sd_label, CE_WARN, 8736 "disk capacity is too large " 8737 "for current cdb length"); 8738 goto spinup_failed; 8739 case EACCES: 8740 /* 8741 * Should never get here if the spin-up 8742 * succeeded, but code it in anyway. 8743 * From here, just continue with the attach... 8744 */ 8745 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8746 "sd_unit_attach: un:0x%p " 8747 "sd_send_scsi_READ_CAPACITY " 8748 "returned reservation conflict\n", un); 8749 reservation_flag = SD_TARGET_IS_RESERVED; 8750 break; 8751 default: 8752 /* 8753 * Likewise, should never get here if the 8754 * spin-up succeeded. Just continue with 8755 * the attach... 8756 */ 8757 break; 8758 } 8759 break; 8760 case EACCES: 8761 /* 8762 * Device is reserved by another host. In this case 8763 * we could not spin it up or read the capacity, but 8764 * we continue with the attach anyway. 8765 */ 8766 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8767 "sd_unit_attach: un:0x%p spin-up reservation " 8768 "conflict.\n", un); 8769 reservation_flag = SD_TARGET_IS_RESERVED; 8770 break; 8771 default: 8772 /* Fail the attach if the spin-up failed. */ 8773 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8774 "sd_unit_attach: un:0x%p spin-up failed.", un); 8775 goto spinup_failed; 8776 } 8777 } 8778 8779 /* 8780 * Check to see if this is a MMC drive 8781 */ 8782 if (ISCD(un)) { 8783 sd_set_mmc_caps(un); 8784 } 8785 8786 /* 8787 * Create the minor nodes for the device. 8788 * Note: If we want to support fdisk on both sparc and intel, this will 8789 * have to separate out the notion that VTOC8 is always sparc, and 8790 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8791 * type will have to be determined at run-time, and the fdisk 8792 * partitioning will have to have been read & set up before we 8793 * create the minor nodes. (any other inits (such as kstats) that 8794 * also ought to be done before creating the minor nodes?) (Doesn't 8795 * setting up the minor nodes kind of imply that we're ready to 8796 * handle an open from userland?) 8797 */ 8798 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8799 goto create_minor_nodes_failed; 8800 } 8801 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8802 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8803 8804 /* 8805 * Add a zero-length attribute to tell the world we support 8806 * kernel ioctls (for layered drivers) 8807 */ 8808 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8809 DDI_KERNEL_IOCTL, NULL, 0); 8810 8811 /* 8812 * Add a boolean property to tell the world we support 8813 * the B_FAILFAST flag (for layered drivers) 8814 */ 8815 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8816 "ddi-failfast-supported", NULL, 0); 8817 8818 /* 8819 * Initialize power management 8820 */ 8821 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8822 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8823 sd_setup_pm(un, devi); 8824 if (un->un_f_pm_is_enabled == FALSE) { 8825 /* 8826 * For performance, point to a jump table that does 8827 * not include pm. 8828 * The direct and priority chains don't change with PM. 8829 * 8830 * Note: this is currently done based on individual device 8831 * capabilities. When an interface for determining system 8832 * power enabled state becomes available, or when additional 8833 * layers are added to the command chain, these values will 8834 * have to be re-evaluated for correctness. 8835 */ 8836 if (un->un_f_non_devbsize_supported) { 8837 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8838 } else { 8839 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8840 } 8841 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8842 } 8843 8844 /* 8845 * This property is set to 0 by HA software to avoid retries 8846 * on a reserved disk. (The preferred property name is 8847 * "retry-on-reservation-conflict") (1189689) 8848 * 8849 * Note: The use of a global here can have unintended consequences. A 8850 * per instance variable is preferrable to match the capabilities of 8851 * different underlying hba's (4402600) 8852 */ 8853 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8854 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8855 sd_retry_on_reservation_conflict); 8856 if (sd_retry_on_reservation_conflict != 0) { 8857 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8858 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8859 sd_retry_on_reservation_conflict); 8860 } 8861 8862 /* Set up options for QFULL handling. */ 8863 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8864 "qfull-retries", -1)) != -1) { 8865 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8866 rval, 1); 8867 } 8868 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8869 "qfull-retry-interval", -1)) != -1) { 8870 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8871 rval, 1); 8872 } 8873 8874 /* 8875 * This just prints a message that announces the existence of the 8876 * device. The message is always printed in the system logfile, but 8877 * only appears on the console if the system is booted with the 8878 * -v (verbose) argument. 8879 */ 8880 ddi_report_dev(devi); 8881 8882 /* 8883 * The framework calls driver attach routines single-threaded 8884 * for a given instance. However we still acquire SD_MUTEX here 8885 * because this required for calling the sd_validate_geometry() 8886 * and sd_register_devid() functions. 8887 */ 8888 mutex_enter(SD_MUTEX(un)); 8889 un->un_f_geometry_is_valid = FALSE; 8890 un->un_mediastate = DKIO_NONE; 8891 un->un_reserved = -1; 8892 8893 /* 8894 * Read and validate the device's geometry (ie, disk label) 8895 * A new unformatted drive will not have a valid geometry, but 8896 * the driver needs to successfully attach to this device so 8897 * the drive can be formatted via ioctls. 8898 */ 8899 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8900 ENOTSUP)) && 8901 (un->un_blockcount < DK_MAX_BLOCKS)) { 8902 /* 8903 * We found a small disk with an EFI label on it; 8904 * we need to fix up the minor nodes accordingly. 8905 */ 8906 ddi_remove_minor_node(devi, "h"); 8907 ddi_remove_minor_node(devi, "h,raw"); 8908 (void) ddi_create_minor_node(devi, "wd", 8909 S_IFBLK, 8910 (instance << SDUNIT_SHIFT) | WD_NODE, 8911 un->un_node_type, NULL); 8912 (void) ddi_create_minor_node(devi, "wd,raw", 8913 S_IFCHR, 8914 (instance << SDUNIT_SHIFT) | WD_NODE, 8915 un->un_node_type, NULL); 8916 } 8917 #if defined(__i386) || defined(__amd64) 8918 else if (un->un_f_capacity_adjusted == 1) { 8919 /* 8920 * Refer to comments related to off-by-1 at the 8921 * header of this file. 8922 * Adjust minor node for 1TB disk. 8923 */ 8924 ddi_remove_minor_node(devi, "wd"); 8925 ddi_remove_minor_node(devi, "wd,raw"); 8926 (void) ddi_create_minor_node(devi, "h", 8927 S_IFBLK, 8928 (instance << SDUNIT_SHIFT) | WD_NODE, 8929 un->un_node_type, NULL); 8930 (void) ddi_create_minor_node(devi, "h,raw", 8931 S_IFCHR, 8932 (instance << SDUNIT_SHIFT) | WD_NODE, 8933 un->un_node_type, NULL); 8934 } 8935 #endif 8936 /* 8937 * Read and initialize the devid for the unit. 8938 */ 8939 ASSERT(un->un_errstats != NULL); 8940 if (un->un_f_devid_supported) { 8941 sd_register_devid(un, devi, reservation_flag); 8942 } 8943 mutex_exit(SD_MUTEX(un)); 8944 8945 #if (defined(__fibre)) 8946 /* 8947 * Register callbacks for fibre only. You can't do this soley 8948 * on the basis of the devid_type because this is hba specific. 8949 * We need to query our hba capabilities to find out whether to 8950 * register or not. 8951 */ 8952 if (un->un_f_is_fibre) { 8953 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8954 sd_init_event_callbacks(un); 8955 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8956 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8957 } 8958 } 8959 #endif 8960 8961 if (un->un_f_opt_disable_cache == TRUE) { 8962 /* 8963 * Disable both read cache and write cache. This is 8964 * the historic behavior of the keywords in the config file. 8965 */ 8966 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8967 0) { 8968 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8969 "sd_unit_attach: un:0x%p Could not disable " 8970 "caching", un); 8971 goto devid_failed; 8972 } 8973 } 8974 8975 /* 8976 * Check the value of the WCE bit now and 8977 * set un_f_write_cache_enabled accordingly. 8978 */ 8979 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8980 mutex_enter(SD_MUTEX(un)); 8981 un->un_f_write_cache_enabled = (wc_enabled != 0); 8982 mutex_exit(SD_MUTEX(un)); 8983 8984 /* 8985 * Set the pstat and error stat values here, so data obtained during the 8986 * previous attach-time routines is available. 8987 * 8988 * Note: This is a critical sequence that needs to be maintained: 8989 * 1) Instantiate the kstats before any routines using the iopath 8990 * (i.e. sd_send_scsi_cmd). 8991 * 2) Instantiate and initialize the partition stats 8992 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 8993 * see detailed comments there. 8994 * 3) Initialize the error stats (sd_set_errstats), following 8995 * sd_validate_geometry(),sd_register_devid(), 8996 * and sd_cache_control(). 8997 */ 8998 sd_set_errstats(un); 8999 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9000 "sd_unit_attach: un:0x%p errstats set\n", un); 9001 9002 /* 9003 * Find out what type of reservation this disk supports. 9004 */ 9005 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 9006 case 0: 9007 /* 9008 * SCSI-3 reservations are supported. 9009 */ 9010 un->un_reservation_type = SD_SCSI3_RESERVATION; 9011 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9012 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 9013 break; 9014 case ENOTSUP: 9015 /* 9016 * The PERSISTENT RESERVE IN command would not be recognized by 9017 * a SCSI-2 device, so assume the reservation type is SCSI-2. 9018 */ 9019 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9020 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 9021 un->un_reservation_type = SD_SCSI2_RESERVATION; 9022 break; 9023 default: 9024 /* 9025 * default to SCSI-3 reservations 9026 */ 9027 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9028 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 9029 un->un_reservation_type = SD_SCSI3_RESERVATION; 9030 break; 9031 } 9032 9033 /* 9034 * After successfully attaching an instance, we record the information 9035 * of how many luns have been attached on the relative target and 9036 * controller for parallel SCSI. This information is used when sd tries 9037 * to set the tagged queuing capability in HBA. 9038 */ 9039 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9040 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 9041 } 9042 9043 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9044 "sd_unit_attach: un:0x%p exit success\n", un); 9045 9046 return (DDI_SUCCESS); 9047 9048 /* 9049 * An error occurred during the attach; clean up & return failure. 9050 */ 9051 9052 devid_failed: 9053 9054 setup_pm_failed: 9055 ddi_remove_minor_node(devi, NULL); 9056 9057 create_minor_nodes_failed: 9058 /* 9059 * Cleanup from the scsi_ifsetcap() calls (437868) 9060 */ 9061 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9062 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9063 9064 /* 9065 * Refer to the comments of setting tagged-qing in the beginning of 9066 * sd_unit_attach. We can only disable tagged queuing when there is 9067 * no lun attached on the target. 9068 */ 9069 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 9070 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9071 } 9072 9073 if (un->un_f_is_fibre == FALSE) { 9074 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9075 } 9076 9077 spinup_failed: 9078 9079 mutex_enter(SD_MUTEX(un)); 9080 9081 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 9082 if (un->un_direct_priority_timeid != NULL) { 9083 timeout_id_t temp_id = un->un_direct_priority_timeid; 9084 un->un_direct_priority_timeid = NULL; 9085 mutex_exit(SD_MUTEX(un)); 9086 (void) untimeout(temp_id); 9087 mutex_enter(SD_MUTEX(un)); 9088 } 9089 9090 /* Cancel any pending start/stop timeouts */ 9091 if (un->un_startstop_timeid != NULL) { 9092 timeout_id_t temp_id = un->un_startstop_timeid; 9093 un->un_startstop_timeid = NULL; 9094 mutex_exit(SD_MUTEX(un)); 9095 (void) untimeout(temp_id); 9096 mutex_enter(SD_MUTEX(un)); 9097 } 9098 9099 /* Cancel any pending reset-throttle timeouts */ 9100 if (un->un_reset_throttle_timeid != NULL) { 9101 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9102 un->un_reset_throttle_timeid = NULL; 9103 mutex_exit(SD_MUTEX(un)); 9104 (void) untimeout(temp_id); 9105 mutex_enter(SD_MUTEX(un)); 9106 } 9107 9108 /* Cancel any pending retry timeouts */ 9109 if (un->un_retry_timeid != NULL) { 9110 timeout_id_t temp_id = un->un_retry_timeid; 9111 un->un_retry_timeid = NULL; 9112 mutex_exit(SD_MUTEX(un)); 9113 (void) untimeout(temp_id); 9114 mutex_enter(SD_MUTEX(un)); 9115 } 9116 9117 /* Cancel any pending delayed cv broadcast timeouts */ 9118 if (un->un_dcvb_timeid != NULL) { 9119 timeout_id_t temp_id = un->un_dcvb_timeid; 9120 un->un_dcvb_timeid = NULL; 9121 mutex_exit(SD_MUTEX(un)); 9122 (void) untimeout(temp_id); 9123 mutex_enter(SD_MUTEX(un)); 9124 } 9125 9126 mutex_exit(SD_MUTEX(un)); 9127 9128 /* There should not be any in-progress I/O so ASSERT this check */ 9129 ASSERT(un->un_ncmds_in_transport == 0); 9130 ASSERT(un->un_ncmds_in_driver == 0); 9131 9132 /* Do not free the softstate if the callback routine is active */ 9133 sd_sync_with_callback(un); 9134 9135 /* 9136 * Partition stats apparently are not used with removables. These would 9137 * not have been created during attach, so no need to clean them up... 9138 */ 9139 if (un->un_stats != NULL) { 9140 kstat_delete(un->un_stats); 9141 un->un_stats = NULL; 9142 } 9143 if (un->un_errstats != NULL) { 9144 kstat_delete(un->un_errstats); 9145 un->un_errstats = NULL; 9146 } 9147 9148 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9149 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9150 9151 ddi_prop_remove_all(devi); 9152 sema_destroy(&un->un_semoclose); 9153 cv_destroy(&un->un_state_cv); 9154 9155 getrbuf_failed: 9156 9157 sd_free_rqs(un); 9158 9159 alloc_rqs_failed: 9160 9161 devp->sd_private = NULL; 9162 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 9163 9164 get_softstate_failed: 9165 /* 9166 * Note: the man pages are unclear as to whether or not doing a 9167 * ddi_soft_state_free(sd_state, instance) is the right way to 9168 * clean up after the ddi_soft_state_zalloc() if the subsequent 9169 * ddi_get_soft_state() fails. The implication seems to be 9170 * that the get_soft_state cannot fail if the zalloc succeeds. 9171 */ 9172 ddi_soft_state_free(sd_state, instance); 9173 9174 probe_failed: 9175 scsi_unprobe(devp); 9176 #ifdef SDDEBUG 9177 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 9178 (sd_level_mask & SD_LOGMASK_TRACE)) { 9179 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 9180 (void *)un); 9181 } 9182 #endif 9183 return (DDI_FAILURE); 9184 } 9185 9186 9187 /* 9188 * Function: sd_unit_detach 9189 * 9190 * Description: Performs DDI_DETACH processing for sddetach(). 9191 * 9192 * Return Code: DDI_SUCCESS 9193 * DDI_FAILURE 9194 * 9195 * Context: Kernel thread context 9196 */ 9197 9198 static int 9199 sd_unit_detach(dev_info_t *devi) 9200 { 9201 struct scsi_device *devp; 9202 struct sd_lun *un; 9203 int i; 9204 int tgt; 9205 dev_t dev; 9206 dev_info_t *pdip = ddi_get_parent(devi); 9207 int instance = ddi_get_instance(devi); 9208 9209 mutex_enter(&sd_detach_mutex); 9210 9211 /* 9212 * Fail the detach for any of the following: 9213 * - Unable to get the sd_lun struct for the instance 9214 * - A layered driver has an outstanding open on the instance 9215 * - Another thread is already detaching this instance 9216 * - Another thread is currently performing an open 9217 */ 9218 devp = ddi_get_driver_private(devi); 9219 if ((devp == NULL) || 9220 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 9221 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 9222 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 9223 mutex_exit(&sd_detach_mutex); 9224 return (DDI_FAILURE); 9225 } 9226 9227 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 9228 9229 /* 9230 * Mark this instance as currently in a detach, to inhibit any 9231 * opens from a layered driver. 9232 */ 9233 un->un_detach_count++; 9234 mutex_exit(&sd_detach_mutex); 9235 9236 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 9237 SCSI_ADDR_PROP_TARGET, -1); 9238 9239 dev = sd_make_device(SD_DEVINFO(un)); 9240 9241 #ifndef lint 9242 _NOTE(COMPETING_THREADS_NOW); 9243 #endif 9244 9245 mutex_enter(SD_MUTEX(un)); 9246 9247 /* 9248 * Fail the detach if there are any outstanding layered 9249 * opens on this device. 9250 */ 9251 for (i = 0; i < NDKMAP; i++) { 9252 if (un->un_ocmap.lyropen[i] != 0) { 9253 goto err_notclosed; 9254 } 9255 } 9256 9257 /* 9258 * Verify there are NO outstanding commands issued to this device. 9259 * ie, un_ncmds_in_transport == 0. 9260 * It's possible to have outstanding commands through the physio 9261 * code path, even though everything's closed. 9262 */ 9263 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 9264 (un->un_direct_priority_timeid != NULL) || 9265 (un->un_state == SD_STATE_RWAIT)) { 9266 mutex_exit(SD_MUTEX(un)); 9267 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9268 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 9269 goto err_stillbusy; 9270 } 9271 9272 /* 9273 * If we have the device reserved, release the reservation. 9274 */ 9275 if ((un->un_resvd_status & SD_RESERVE) && 9276 !(un->un_resvd_status & SD_LOST_RESERVE)) { 9277 mutex_exit(SD_MUTEX(un)); 9278 /* 9279 * Note: sd_reserve_release sends a command to the device 9280 * via the sd_ioctlcmd() path, and can sleep. 9281 */ 9282 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 9283 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9284 "sd_dr_detach: Cannot release reservation \n"); 9285 } 9286 } else { 9287 mutex_exit(SD_MUTEX(un)); 9288 } 9289 9290 /* 9291 * Untimeout any reserve recover, throttle reset, restart unit 9292 * and delayed broadcast timeout threads. Protect the timeout pointer 9293 * from getting nulled by their callback functions. 9294 */ 9295 mutex_enter(SD_MUTEX(un)); 9296 if (un->un_resvd_timeid != NULL) { 9297 timeout_id_t temp_id = un->un_resvd_timeid; 9298 un->un_resvd_timeid = NULL; 9299 mutex_exit(SD_MUTEX(un)); 9300 (void) untimeout(temp_id); 9301 mutex_enter(SD_MUTEX(un)); 9302 } 9303 9304 if (un->un_reset_throttle_timeid != NULL) { 9305 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9306 un->un_reset_throttle_timeid = NULL; 9307 mutex_exit(SD_MUTEX(un)); 9308 (void) untimeout(temp_id); 9309 mutex_enter(SD_MUTEX(un)); 9310 } 9311 9312 if (un->un_startstop_timeid != NULL) { 9313 timeout_id_t temp_id = un->un_startstop_timeid; 9314 un->un_startstop_timeid = NULL; 9315 mutex_exit(SD_MUTEX(un)); 9316 (void) untimeout(temp_id); 9317 mutex_enter(SD_MUTEX(un)); 9318 } 9319 9320 if (un->un_dcvb_timeid != NULL) { 9321 timeout_id_t temp_id = un->un_dcvb_timeid; 9322 un->un_dcvb_timeid = NULL; 9323 mutex_exit(SD_MUTEX(un)); 9324 (void) untimeout(temp_id); 9325 } else { 9326 mutex_exit(SD_MUTEX(un)); 9327 } 9328 9329 /* Remove any pending reservation reclaim requests for this device */ 9330 sd_rmv_resv_reclaim_req(dev); 9331 9332 mutex_enter(SD_MUTEX(un)); 9333 9334 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 9335 if (un->un_direct_priority_timeid != NULL) { 9336 timeout_id_t temp_id = un->un_direct_priority_timeid; 9337 un->un_direct_priority_timeid = NULL; 9338 mutex_exit(SD_MUTEX(un)); 9339 (void) untimeout(temp_id); 9340 mutex_enter(SD_MUTEX(un)); 9341 } 9342 9343 /* Cancel any active multi-host disk watch thread requests */ 9344 if (un->un_mhd_token != NULL) { 9345 mutex_exit(SD_MUTEX(un)); 9346 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 9347 if (scsi_watch_request_terminate(un->un_mhd_token, 9348 SCSI_WATCH_TERMINATE_NOWAIT)) { 9349 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9350 "sd_dr_detach: Cannot cancel mhd watch request\n"); 9351 /* 9352 * Note: We are returning here after having removed 9353 * some driver timeouts above. This is consistent with 9354 * the legacy implementation but perhaps the watch 9355 * terminate call should be made with the wait flag set. 9356 */ 9357 goto err_stillbusy; 9358 } 9359 mutex_enter(SD_MUTEX(un)); 9360 un->un_mhd_token = NULL; 9361 } 9362 9363 if (un->un_swr_token != NULL) { 9364 mutex_exit(SD_MUTEX(un)); 9365 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 9366 if (scsi_watch_request_terminate(un->un_swr_token, 9367 SCSI_WATCH_TERMINATE_NOWAIT)) { 9368 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9369 "sd_dr_detach: Cannot cancel swr watch request\n"); 9370 /* 9371 * Note: We are returning here after having removed 9372 * some driver timeouts above. This is consistent with 9373 * the legacy implementation but perhaps the watch 9374 * terminate call should be made with the wait flag set. 9375 */ 9376 goto err_stillbusy; 9377 } 9378 mutex_enter(SD_MUTEX(un)); 9379 un->un_swr_token = NULL; 9380 } 9381 9382 mutex_exit(SD_MUTEX(un)); 9383 9384 /* 9385 * Clear any scsi_reset_notifies. We clear the reset notifies 9386 * if we have not registered one. 9387 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 9388 */ 9389 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 9390 sd_mhd_reset_notify_cb, (caddr_t)un); 9391 9392 /* 9393 * protect the timeout pointers from getting nulled by 9394 * their callback functions during the cancellation process. 9395 * In such a scenario untimeout can be invoked with a null value. 9396 */ 9397 _NOTE(NO_COMPETING_THREADS_NOW); 9398 9399 mutex_enter(&un->un_pm_mutex); 9400 if (un->un_pm_idle_timeid != NULL) { 9401 timeout_id_t temp_id = un->un_pm_idle_timeid; 9402 un->un_pm_idle_timeid = NULL; 9403 mutex_exit(&un->un_pm_mutex); 9404 9405 /* 9406 * Timeout is active; cancel it. 9407 * Note that it'll never be active on a device 9408 * that does not support PM therefore we don't 9409 * have to check before calling pm_idle_component. 9410 */ 9411 (void) untimeout(temp_id); 9412 (void) pm_idle_component(SD_DEVINFO(un), 0); 9413 mutex_enter(&un->un_pm_mutex); 9414 } 9415 9416 /* 9417 * Check whether there is already a timeout scheduled for power 9418 * management. If yes then don't lower the power here, that's. 9419 * the timeout handler's job. 9420 */ 9421 if (un->un_pm_timeid != NULL) { 9422 timeout_id_t temp_id = un->un_pm_timeid; 9423 un->un_pm_timeid = NULL; 9424 mutex_exit(&un->un_pm_mutex); 9425 /* 9426 * Timeout is active; cancel it. 9427 * Note that it'll never be active on a device 9428 * that does not support PM therefore we don't 9429 * have to check before calling pm_idle_component. 9430 */ 9431 (void) untimeout(temp_id); 9432 (void) pm_idle_component(SD_DEVINFO(un), 0); 9433 9434 } else { 9435 mutex_exit(&un->un_pm_mutex); 9436 if ((un->un_f_pm_is_enabled == TRUE) && 9437 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 9438 DDI_SUCCESS)) { 9439 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9440 "sd_dr_detach: Lower power request failed, ignoring.\n"); 9441 /* 9442 * Fix for bug: 4297749, item # 13 9443 * The above test now includes a check to see if PM is 9444 * supported by this device before call 9445 * pm_lower_power(). 9446 * Note, the following is not dead code. The call to 9447 * pm_lower_power above will generate a call back into 9448 * our sdpower routine which might result in a timeout 9449 * handler getting activated. Therefore the following 9450 * code is valid and necessary. 9451 */ 9452 mutex_enter(&un->un_pm_mutex); 9453 if (un->un_pm_timeid != NULL) { 9454 timeout_id_t temp_id = un->un_pm_timeid; 9455 un->un_pm_timeid = NULL; 9456 mutex_exit(&un->un_pm_mutex); 9457 (void) untimeout(temp_id); 9458 (void) pm_idle_component(SD_DEVINFO(un), 0); 9459 } else { 9460 mutex_exit(&un->un_pm_mutex); 9461 } 9462 } 9463 } 9464 9465 /* 9466 * Cleanup from the scsi_ifsetcap() calls (437868) 9467 * Relocated here from above to be after the call to 9468 * pm_lower_power, which was getting errors. 9469 */ 9470 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9471 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9472 9473 /* 9474 * Currently, tagged queuing is supported per target based by HBA. 9475 * Setting this per lun instance actually sets the capability of this 9476 * target in HBA, which affects those luns already attached on the 9477 * same target. So during detach, we can only disable this capability 9478 * only when this is the only lun left on this target. By doing 9479 * this, we assume a target has the same tagged queuing capability 9480 * for every lun. The condition can be removed when HBA is changed to 9481 * support per lun based tagged queuing capability. 9482 */ 9483 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 9484 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9485 } 9486 9487 if (un->un_f_is_fibre == FALSE) { 9488 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9489 } 9490 9491 /* 9492 * Remove any event callbacks, fibre only 9493 */ 9494 if (un->un_f_is_fibre == TRUE) { 9495 if ((un->un_insert_event != NULL) && 9496 (ddi_remove_event_handler(un->un_insert_cb_id) != 9497 DDI_SUCCESS)) { 9498 /* 9499 * Note: We are returning here after having done 9500 * substantial cleanup above. This is consistent 9501 * with the legacy implementation but this may not 9502 * be the right thing to do. 9503 */ 9504 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9505 "sd_dr_detach: Cannot cancel insert event\n"); 9506 goto err_remove_event; 9507 } 9508 un->un_insert_event = NULL; 9509 9510 if ((un->un_remove_event != NULL) && 9511 (ddi_remove_event_handler(un->un_remove_cb_id) != 9512 DDI_SUCCESS)) { 9513 /* 9514 * Note: We are returning here after having done 9515 * substantial cleanup above. This is consistent 9516 * with the legacy implementation but this may not 9517 * be the right thing to do. 9518 */ 9519 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9520 "sd_dr_detach: Cannot cancel remove event\n"); 9521 goto err_remove_event; 9522 } 9523 un->un_remove_event = NULL; 9524 } 9525 9526 /* Do not free the softstate if the callback routine is active */ 9527 sd_sync_with_callback(un); 9528 9529 /* 9530 * Hold the detach mutex here, to make sure that no other threads ever 9531 * can access a (partially) freed soft state structure. 9532 */ 9533 mutex_enter(&sd_detach_mutex); 9534 9535 /* 9536 * Clean up the soft state struct. 9537 * Cleanup is done in reverse order of allocs/inits. 9538 * At this point there should be no competing threads anymore. 9539 */ 9540 9541 /* Unregister and free device id. */ 9542 ddi_devid_unregister(devi); 9543 if (un->un_devid) { 9544 ddi_devid_free(un->un_devid); 9545 un->un_devid = NULL; 9546 } 9547 9548 /* 9549 * Destroy wmap cache if it exists. 9550 */ 9551 if (un->un_wm_cache != NULL) { 9552 kmem_cache_destroy(un->un_wm_cache); 9553 un->un_wm_cache = NULL; 9554 } 9555 9556 /* Remove minor nodes */ 9557 ddi_remove_minor_node(devi, NULL); 9558 9559 /* 9560 * kstat cleanup is done in detach for all device types (4363169). 9561 * We do not want to fail detach if the device kstats are not deleted 9562 * since there is a confusion about the devo_refcnt for the device. 9563 * We just delete the kstats and let detach complete successfully. 9564 */ 9565 if (un->un_stats != NULL) { 9566 kstat_delete(un->un_stats); 9567 un->un_stats = NULL; 9568 } 9569 if (un->un_errstats != NULL) { 9570 kstat_delete(un->un_errstats); 9571 un->un_errstats = NULL; 9572 } 9573 9574 /* Remove partition stats */ 9575 if (un->un_f_pkstats_enabled) { 9576 for (i = 0; i < NSDMAP; i++) { 9577 if (un->un_pstats[i] != NULL) { 9578 kstat_delete(un->un_pstats[i]); 9579 un->un_pstats[i] = NULL; 9580 } 9581 } 9582 } 9583 9584 /* Remove xbuf registration */ 9585 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9586 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9587 9588 /* Remove driver properties */ 9589 ddi_prop_remove_all(devi); 9590 9591 mutex_destroy(&un->un_pm_mutex); 9592 cv_destroy(&un->un_pm_busy_cv); 9593 9594 cv_destroy(&un->un_wcc_cv); 9595 9596 /* Open/close semaphore */ 9597 sema_destroy(&un->un_semoclose); 9598 9599 /* Removable media condvar. */ 9600 cv_destroy(&un->un_state_cv); 9601 9602 /* Suspend/resume condvar. */ 9603 cv_destroy(&un->un_suspend_cv); 9604 cv_destroy(&un->un_disk_busy_cv); 9605 9606 sd_free_rqs(un); 9607 9608 /* Free up soft state */ 9609 devp->sd_private = NULL; 9610 bzero(un, sizeof (struct sd_lun)); 9611 ddi_soft_state_free(sd_state, instance); 9612 9613 mutex_exit(&sd_detach_mutex); 9614 9615 /* This frees up the INQUIRY data associated with the device. */ 9616 scsi_unprobe(devp); 9617 9618 /* 9619 * After successfully detaching an instance, we update the information 9620 * of how many luns have been attached in the relative target and 9621 * controller for parallel SCSI. This information is used when sd tries 9622 * to set the tagged queuing capability in HBA. 9623 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 9624 * check if the device is parallel SCSI. However, we don't need to 9625 * check here because we've already checked during attach. No device 9626 * that is not parallel SCSI is in the chain. 9627 */ 9628 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9629 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9630 } 9631 9632 return (DDI_SUCCESS); 9633 9634 err_notclosed: 9635 mutex_exit(SD_MUTEX(un)); 9636 9637 err_stillbusy: 9638 _NOTE(NO_COMPETING_THREADS_NOW); 9639 9640 err_remove_event: 9641 mutex_enter(&sd_detach_mutex); 9642 un->un_detach_count--; 9643 mutex_exit(&sd_detach_mutex); 9644 9645 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9646 return (DDI_FAILURE); 9647 } 9648 9649 9650 /* 9651 * Driver minor node structure and data table 9652 */ 9653 struct driver_minor_data { 9654 char *name; 9655 minor_t minor; 9656 int type; 9657 }; 9658 9659 static struct driver_minor_data sd_minor_data[] = { 9660 {"a", 0, S_IFBLK}, 9661 {"b", 1, S_IFBLK}, 9662 {"c", 2, S_IFBLK}, 9663 {"d", 3, S_IFBLK}, 9664 {"e", 4, S_IFBLK}, 9665 {"f", 5, S_IFBLK}, 9666 {"g", 6, S_IFBLK}, 9667 {"h", 7, S_IFBLK}, 9668 #if defined(_SUNOS_VTOC_16) 9669 {"i", 8, S_IFBLK}, 9670 {"j", 9, S_IFBLK}, 9671 {"k", 10, S_IFBLK}, 9672 {"l", 11, S_IFBLK}, 9673 {"m", 12, S_IFBLK}, 9674 {"n", 13, S_IFBLK}, 9675 {"o", 14, S_IFBLK}, 9676 {"p", 15, S_IFBLK}, 9677 #endif /* defined(_SUNOS_VTOC_16) */ 9678 #if defined(_FIRMWARE_NEEDS_FDISK) 9679 {"q", 16, S_IFBLK}, 9680 {"r", 17, S_IFBLK}, 9681 {"s", 18, S_IFBLK}, 9682 {"t", 19, S_IFBLK}, 9683 {"u", 20, S_IFBLK}, 9684 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9685 {"a,raw", 0, S_IFCHR}, 9686 {"b,raw", 1, S_IFCHR}, 9687 {"c,raw", 2, S_IFCHR}, 9688 {"d,raw", 3, S_IFCHR}, 9689 {"e,raw", 4, S_IFCHR}, 9690 {"f,raw", 5, S_IFCHR}, 9691 {"g,raw", 6, S_IFCHR}, 9692 {"h,raw", 7, S_IFCHR}, 9693 #if defined(_SUNOS_VTOC_16) 9694 {"i,raw", 8, S_IFCHR}, 9695 {"j,raw", 9, S_IFCHR}, 9696 {"k,raw", 10, S_IFCHR}, 9697 {"l,raw", 11, S_IFCHR}, 9698 {"m,raw", 12, S_IFCHR}, 9699 {"n,raw", 13, S_IFCHR}, 9700 {"o,raw", 14, S_IFCHR}, 9701 {"p,raw", 15, S_IFCHR}, 9702 #endif /* defined(_SUNOS_VTOC_16) */ 9703 #if defined(_FIRMWARE_NEEDS_FDISK) 9704 {"q,raw", 16, S_IFCHR}, 9705 {"r,raw", 17, S_IFCHR}, 9706 {"s,raw", 18, S_IFCHR}, 9707 {"t,raw", 19, S_IFCHR}, 9708 {"u,raw", 20, S_IFCHR}, 9709 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9710 {0} 9711 }; 9712 9713 static struct driver_minor_data sd_minor_data_efi[] = { 9714 {"a", 0, S_IFBLK}, 9715 {"b", 1, S_IFBLK}, 9716 {"c", 2, S_IFBLK}, 9717 {"d", 3, S_IFBLK}, 9718 {"e", 4, S_IFBLK}, 9719 {"f", 5, S_IFBLK}, 9720 {"g", 6, S_IFBLK}, 9721 {"wd", 7, S_IFBLK}, 9722 #if defined(_FIRMWARE_NEEDS_FDISK) 9723 {"q", 16, S_IFBLK}, 9724 {"r", 17, S_IFBLK}, 9725 {"s", 18, S_IFBLK}, 9726 {"t", 19, S_IFBLK}, 9727 {"u", 20, S_IFBLK}, 9728 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9729 {"a,raw", 0, S_IFCHR}, 9730 {"b,raw", 1, S_IFCHR}, 9731 {"c,raw", 2, S_IFCHR}, 9732 {"d,raw", 3, S_IFCHR}, 9733 {"e,raw", 4, S_IFCHR}, 9734 {"f,raw", 5, S_IFCHR}, 9735 {"g,raw", 6, S_IFCHR}, 9736 {"wd,raw", 7, S_IFCHR}, 9737 #if defined(_FIRMWARE_NEEDS_FDISK) 9738 {"q,raw", 16, S_IFCHR}, 9739 {"r,raw", 17, S_IFCHR}, 9740 {"s,raw", 18, S_IFCHR}, 9741 {"t,raw", 19, S_IFCHR}, 9742 {"u,raw", 20, S_IFCHR}, 9743 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9744 {0} 9745 }; 9746 9747 9748 /* 9749 * Function: sd_create_minor_nodes 9750 * 9751 * Description: Create the minor device nodes for the instance. 9752 * 9753 * Arguments: un - driver soft state (unit) structure 9754 * devi - pointer to device info structure 9755 * 9756 * Return Code: DDI_SUCCESS 9757 * DDI_FAILURE 9758 * 9759 * Context: Kernel thread context 9760 */ 9761 9762 static int 9763 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9764 { 9765 struct driver_minor_data *dmdp; 9766 struct scsi_device *devp; 9767 int instance; 9768 char name[48]; 9769 9770 ASSERT(un != NULL); 9771 devp = ddi_get_driver_private(devi); 9772 instance = ddi_get_instance(devp->sd_dev); 9773 9774 /* 9775 * Create all the minor nodes for this target. 9776 */ 9777 if (un->un_blockcount > DK_MAX_BLOCKS) 9778 dmdp = sd_minor_data_efi; 9779 else 9780 dmdp = sd_minor_data; 9781 while (dmdp->name != NULL) { 9782 9783 (void) sprintf(name, "%s", dmdp->name); 9784 9785 if (ddi_create_minor_node(devi, name, dmdp->type, 9786 (instance << SDUNIT_SHIFT) | dmdp->minor, 9787 un->un_node_type, NULL) == DDI_FAILURE) { 9788 /* 9789 * Clean up any nodes that may have been created, in 9790 * case this fails in the middle of the loop. 9791 */ 9792 ddi_remove_minor_node(devi, NULL); 9793 return (DDI_FAILURE); 9794 } 9795 dmdp++; 9796 } 9797 9798 return (DDI_SUCCESS); 9799 } 9800 9801 9802 /* 9803 * Function: sd_create_errstats 9804 * 9805 * Description: This routine instantiates the device error stats. 9806 * 9807 * Note: During attach the stats are instantiated first so they are 9808 * available for attach-time routines that utilize the driver 9809 * iopath to send commands to the device. The stats are initialized 9810 * separately so data obtained during some attach-time routines is 9811 * available. (4362483) 9812 * 9813 * Arguments: un - driver soft state (unit) structure 9814 * instance - driver instance 9815 * 9816 * Context: Kernel thread context 9817 */ 9818 9819 static void 9820 sd_create_errstats(struct sd_lun *un, int instance) 9821 { 9822 struct sd_errstats *stp; 9823 char kstatmodule_err[KSTAT_STRLEN]; 9824 char kstatname[KSTAT_STRLEN]; 9825 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9826 9827 ASSERT(un != NULL); 9828 9829 if (un->un_errstats != NULL) { 9830 return; 9831 } 9832 9833 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9834 "%serr", sd_label); 9835 (void) snprintf(kstatname, sizeof (kstatname), 9836 "%s%d,err", sd_label, instance); 9837 9838 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9839 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9840 9841 if (un->un_errstats == NULL) { 9842 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9843 "sd_create_errstats: Failed kstat_create\n"); 9844 return; 9845 } 9846 9847 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9848 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9849 KSTAT_DATA_UINT32); 9850 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9851 KSTAT_DATA_UINT32); 9852 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9853 KSTAT_DATA_UINT32); 9854 kstat_named_init(&stp->sd_vid, "Vendor", 9855 KSTAT_DATA_CHAR); 9856 kstat_named_init(&stp->sd_pid, "Product", 9857 KSTAT_DATA_CHAR); 9858 kstat_named_init(&stp->sd_revision, "Revision", 9859 KSTAT_DATA_CHAR); 9860 kstat_named_init(&stp->sd_serial, "Serial No", 9861 KSTAT_DATA_CHAR); 9862 kstat_named_init(&stp->sd_capacity, "Size", 9863 KSTAT_DATA_ULONGLONG); 9864 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9865 KSTAT_DATA_UINT32); 9866 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9867 KSTAT_DATA_UINT32); 9868 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9869 KSTAT_DATA_UINT32); 9870 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9871 KSTAT_DATA_UINT32); 9872 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9873 KSTAT_DATA_UINT32); 9874 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9875 KSTAT_DATA_UINT32); 9876 9877 un->un_errstats->ks_private = un; 9878 un->un_errstats->ks_update = nulldev; 9879 9880 kstat_install(un->un_errstats); 9881 } 9882 9883 9884 /* 9885 * Function: sd_set_errstats 9886 * 9887 * Description: This routine sets the value of the vendor id, product id, 9888 * revision, serial number, and capacity device error stats. 9889 * 9890 * Note: During attach the stats are instantiated first so they are 9891 * available for attach-time routines that utilize the driver 9892 * iopath to send commands to the device. The stats are initialized 9893 * separately so data obtained during some attach-time routines is 9894 * available. (4362483) 9895 * 9896 * Arguments: un - driver soft state (unit) structure 9897 * 9898 * Context: Kernel thread context 9899 */ 9900 9901 static void 9902 sd_set_errstats(struct sd_lun *un) 9903 { 9904 struct sd_errstats *stp; 9905 9906 ASSERT(un != NULL); 9907 ASSERT(un->un_errstats != NULL); 9908 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9909 ASSERT(stp != NULL); 9910 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9911 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9912 (void) strncpy(stp->sd_revision.value.c, 9913 un->un_sd->sd_inq->inq_revision, 4); 9914 9915 /* 9916 * All the errstats are persistent across detach/attach, 9917 * so reset all the errstats here in case of the hot 9918 * replacement of disk drives, except for not changed 9919 * Sun qualified drives. 9920 */ 9921 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9922 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9923 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9924 stp->sd_softerrs.value.ui32 = 0; 9925 stp->sd_harderrs.value.ui32 = 0; 9926 stp->sd_transerrs.value.ui32 = 0; 9927 stp->sd_rq_media_err.value.ui32 = 0; 9928 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9929 stp->sd_rq_nodev_err.value.ui32 = 0; 9930 stp->sd_rq_recov_err.value.ui32 = 0; 9931 stp->sd_rq_illrq_err.value.ui32 = 0; 9932 stp->sd_rq_pfa_err.value.ui32 = 0; 9933 } 9934 9935 /* 9936 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9937 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9938 * (4376302)) 9939 */ 9940 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9941 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9942 sizeof (SD_INQUIRY(un)->inq_serial)); 9943 } 9944 9945 if (un->un_f_blockcount_is_valid != TRUE) { 9946 /* 9947 * Set capacity error stat to 0 for no media. This ensures 9948 * a valid capacity is displayed in response to 'iostat -E' 9949 * when no media is present in the device. 9950 */ 9951 stp->sd_capacity.value.ui64 = 0; 9952 } else { 9953 /* 9954 * Multiply un_blockcount by un->un_sys_blocksize to get 9955 * capacity. 9956 * 9957 * Note: for non-512 blocksize devices "un_blockcount" has been 9958 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9959 * (un_tgt_blocksize / un->un_sys_blocksize). 9960 */ 9961 stp->sd_capacity.value.ui64 = (uint64_t) 9962 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9963 } 9964 } 9965 9966 9967 /* 9968 * Function: sd_set_pstats 9969 * 9970 * Description: This routine instantiates and initializes the partition 9971 * stats for each partition with more than zero blocks. 9972 * (4363169) 9973 * 9974 * Arguments: un - driver soft state (unit) structure 9975 * 9976 * Context: Kernel thread context 9977 */ 9978 9979 static void 9980 sd_set_pstats(struct sd_lun *un) 9981 { 9982 char kstatname[KSTAT_STRLEN]; 9983 int instance; 9984 int i; 9985 9986 ASSERT(un != NULL); 9987 9988 instance = ddi_get_instance(SD_DEVINFO(un)); 9989 9990 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 9991 for (i = 0; i < NSDMAP; i++) { 9992 if ((un->un_pstats[i] == NULL) && 9993 (un->un_map[i].dkl_nblk != 0)) { 9994 (void) snprintf(kstatname, sizeof (kstatname), 9995 "%s%d,%s", sd_label, instance, 9996 sd_minor_data[i].name); 9997 un->un_pstats[i] = kstat_create(sd_label, 9998 instance, kstatname, "partition", KSTAT_TYPE_IO, 9999 1, KSTAT_FLAG_PERSISTENT); 10000 if (un->un_pstats[i] != NULL) { 10001 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 10002 kstat_install(un->un_pstats[i]); 10003 } 10004 } 10005 } 10006 } 10007 10008 10009 #if (defined(__fibre)) 10010 /* 10011 * Function: sd_init_event_callbacks 10012 * 10013 * Description: This routine initializes the insertion and removal event 10014 * callbacks. (fibre only) 10015 * 10016 * Arguments: un - driver soft state (unit) structure 10017 * 10018 * Context: Kernel thread context 10019 */ 10020 10021 static void 10022 sd_init_event_callbacks(struct sd_lun *un) 10023 { 10024 ASSERT(un != NULL); 10025 10026 if ((un->un_insert_event == NULL) && 10027 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 10028 &un->un_insert_event) == DDI_SUCCESS)) { 10029 /* 10030 * Add the callback for an insertion event 10031 */ 10032 (void) ddi_add_event_handler(SD_DEVINFO(un), 10033 un->un_insert_event, sd_event_callback, (void *)un, 10034 &(un->un_insert_cb_id)); 10035 } 10036 10037 if ((un->un_remove_event == NULL) && 10038 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 10039 &un->un_remove_event) == DDI_SUCCESS)) { 10040 /* 10041 * Add the callback for a removal event 10042 */ 10043 (void) ddi_add_event_handler(SD_DEVINFO(un), 10044 un->un_remove_event, sd_event_callback, (void *)un, 10045 &(un->un_remove_cb_id)); 10046 } 10047 } 10048 10049 10050 /* 10051 * Function: sd_event_callback 10052 * 10053 * Description: This routine handles insert/remove events (photon). The 10054 * state is changed to OFFLINE which can be used to supress 10055 * error msgs. (fibre only) 10056 * 10057 * Arguments: un - driver soft state (unit) structure 10058 * 10059 * Context: Callout thread context 10060 */ 10061 /* ARGSUSED */ 10062 static void 10063 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 10064 void *bus_impldata) 10065 { 10066 struct sd_lun *un = (struct sd_lun *)arg; 10067 10068 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 10069 if (event == un->un_insert_event) { 10070 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 10071 mutex_enter(SD_MUTEX(un)); 10072 if (un->un_state == SD_STATE_OFFLINE) { 10073 if (un->un_last_state != SD_STATE_SUSPENDED) { 10074 un->un_state = un->un_last_state; 10075 } else { 10076 /* 10077 * We have gone through SUSPEND/RESUME while 10078 * we were offline. Restore the last state 10079 */ 10080 un->un_state = un->un_save_state; 10081 } 10082 } 10083 mutex_exit(SD_MUTEX(un)); 10084 10085 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 10086 } else if (event == un->un_remove_event) { 10087 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 10088 mutex_enter(SD_MUTEX(un)); 10089 /* 10090 * We need to handle an event callback that occurs during 10091 * the suspend operation, since we don't prevent it. 10092 */ 10093 if (un->un_state != SD_STATE_OFFLINE) { 10094 if (un->un_state != SD_STATE_SUSPENDED) { 10095 New_state(un, SD_STATE_OFFLINE); 10096 } else { 10097 un->un_last_state = SD_STATE_OFFLINE; 10098 } 10099 } 10100 mutex_exit(SD_MUTEX(un)); 10101 } else { 10102 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 10103 "!Unknown event\n"); 10104 } 10105 10106 } 10107 #endif 10108 10109 /* 10110 * Function: sd_cache_control() 10111 * 10112 * Description: This routine is the driver entry point for setting 10113 * read and write caching by modifying the WCE (write cache 10114 * enable) and RCD (read cache disable) bits of mode 10115 * page 8 (MODEPAGE_CACHING). 10116 * 10117 * Arguments: un - driver soft state (unit) structure 10118 * rcd_flag - flag for controlling the read cache 10119 * wce_flag - flag for controlling the write cache 10120 * 10121 * Return Code: EIO 10122 * code returned by sd_send_scsi_MODE_SENSE and 10123 * sd_send_scsi_MODE_SELECT 10124 * 10125 * Context: Kernel Thread 10126 */ 10127 10128 static int 10129 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 10130 { 10131 struct mode_caching *mode_caching_page; 10132 uchar_t *header; 10133 size_t buflen; 10134 int hdrlen; 10135 int bd_len; 10136 int rval = 0; 10137 struct mode_header_grp2 *mhp; 10138 10139 ASSERT(un != NULL); 10140 10141 /* 10142 * Do a test unit ready, otherwise a mode sense may not work if this 10143 * is the first command sent to the device after boot. 10144 */ 10145 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10146 10147 if (un->un_f_cfg_is_atapi == TRUE) { 10148 hdrlen = MODE_HEADER_LENGTH_GRP2; 10149 } else { 10150 hdrlen = MODE_HEADER_LENGTH; 10151 } 10152 10153 /* 10154 * Allocate memory for the retrieved mode page and its headers. Set 10155 * a pointer to the page itself. Use mode_cache_scsi3 to insure 10156 * we get all of the mode sense data otherwise, the mode select 10157 * will fail. mode_cache_scsi3 is a superset of mode_caching. 10158 */ 10159 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 10160 sizeof (struct mode_cache_scsi3); 10161 10162 header = kmem_zalloc(buflen, KM_SLEEP); 10163 10164 /* Get the information from the device. */ 10165 if (un->un_f_cfg_is_atapi == TRUE) { 10166 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10167 MODEPAGE_CACHING, SD_PATH_DIRECT); 10168 } else { 10169 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10170 MODEPAGE_CACHING, SD_PATH_DIRECT); 10171 } 10172 if (rval != 0) { 10173 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10174 "sd_cache_control: Mode Sense Failed\n"); 10175 kmem_free(header, buflen); 10176 return (rval); 10177 } 10178 10179 /* 10180 * Determine size of Block Descriptors in order to locate 10181 * the mode page data. ATAPI devices return 0, SCSI devices 10182 * should return MODE_BLK_DESC_LENGTH. 10183 */ 10184 if (un->un_f_cfg_is_atapi == TRUE) { 10185 mhp = (struct mode_header_grp2 *)header; 10186 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10187 } else { 10188 bd_len = ((struct mode_header *)header)->bdesc_length; 10189 } 10190 10191 if (bd_len > MODE_BLK_DESC_LENGTH) { 10192 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10193 "sd_cache_control: Mode Sense returned invalid " 10194 "block descriptor length\n"); 10195 kmem_free(header, buflen); 10196 return (EIO); 10197 } 10198 10199 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10200 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10201 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10202 " caching page code mismatch %d\n", 10203 mode_caching_page->mode_page.code); 10204 kmem_free(header, buflen); 10205 return (EIO); 10206 } 10207 10208 /* Check the relevant bits on successful mode sense. */ 10209 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 10210 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 10211 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 10212 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 10213 10214 size_t sbuflen; 10215 uchar_t save_pg; 10216 10217 /* 10218 * Construct select buffer length based on the 10219 * length of the sense data returned. 10220 */ 10221 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 10222 sizeof (struct mode_page) + 10223 (int)mode_caching_page->mode_page.length; 10224 10225 /* 10226 * Set the caching bits as requested. 10227 */ 10228 if (rcd_flag == SD_CACHE_ENABLE) 10229 mode_caching_page->rcd = 0; 10230 else if (rcd_flag == SD_CACHE_DISABLE) 10231 mode_caching_page->rcd = 1; 10232 10233 if (wce_flag == SD_CACHE_ENABLE) 10234 mode_caching_page->wce = 1; 10235 else if (wce_flag == SD_CACHE_DISABLE) 10236 mode_caching_page->wce = 0; 10237 10238 /* 10239 * Save the page if the mode sense says the 10240 * drive supports it. 10241 */ 10242 save_pg = mode_caching_page->mode_page.ps ? 10243 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 10244 10245 /* Clear reserved bits before mode select. */ 10246 mode_caching_page->mode_page.ps = 0; 10247 10248 /* 10249 * Clear out mode header for mode select. 10250 * The rest of the retrieved page will be reused. 10251 */ 10252 bzero(header, hdrlen); 10253 10254 if (un->un_f_cfg_is_atapi == TRUE) { 10255 mhp = (struct mode_header_grp2 *)header; 10256 mhp->bdesc_length_hi = bd_len >> 8; 10257 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 10258 } else { 10259 ((struct mode_header *)header)->bdesc_length = bd_len; 10260 } 10261 10262 /* Issue mode select to change the cache settings */ 10263 if (un->un_f_cfg_is_atapi == TRUE) { 10264 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 10265 sbuflen, save_pg, SD_PATH_DIRECT); 10266 } else { 10267 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 10268 sbuflen, save_pg, SD_PATH_DIRECT); 10269 } 10270 } 10271 10272 kmem_free(header, buflen); 10273 return (rval); 10274 } 10275 10276 10277 /* 10278 * Function: sd_get_write_cache_enabled() 10279 * 10280 * Description: This routine is the driver entry point for determining if 10281 * write caching is enabled. It examines the WCE (write cache 10282 * enable) bits of mode page 8 (MODEPAGE_CACHING). 10283 * 10284 * Arguments: un - driver soft state (unit) structure 10285 * is_enabled - pointer to int where write cache enabled state 10286 * is returned (non-zero -> write cache enabled) 10287 * 10288 * 10289 * Return Code: EIO 10290 * code returned by sd_send_scsi_MODE_SENSE 10291 * 10292 * Context: Kernel Thread 10293 * 10294 * NOTE: If ioctl is added to disable write cache, this sequence should 10295 * be followed so that no locking is required for accesses to 10296 * un->un_f_write_cache_enabled: 10297 * do mode select to clear wce 10298 * do synchronize cache to flush cache 10299 * set un->un_f_write_cache_enabled = FALSE 10300 * 10301 * Conversely, an ioctl to enable the write cache should be done 10302 * in this order: 10303 * set un->un_f_write_cache_enabled = TRUE 10304 * do mode select to set wce 10305 */ 10306 10307 static int 10308 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 10309 { 10310 struct mode_caching *mode_caching_page; 10311 uchar_t *header; 10312 size_t buflen; 10313 int hdrlen; 10314 int bd_len; 10315 int rval = 0; 10316 10317 ASSERT(un != NULL); 10318 ASSERT(is_enabled != NULL); 10319 10320 /* in case of error, flag as enabled */ 10321 *is_enabled = TRUE; 10322 10323 /* 10324 * Do a test unit ready, otherwise a mode sense may not work if this 10325 * is the first command sent to the device after boot. 10326 */ 10327 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10328 10329 if (un->un_f_cfg_is_atapi == TRUE) { 10330 hdrlen = MODE_HEADER_LENGTH_GRP2; 10331 } else { 10332 hdrlen = MODE_HEADER_LENGTH; 10333 } 10334 10335 /* 10336 * Allocate memory for the retrieved mode page and its headers. Set 10337 * a pointer to the page itself. 10338 */ 10339 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 10340 header = kmem_zalloc(buflen, KM_SLEEP); 10341 10342 /* Get the information from the device. */ 10343 if (un->un_f_cfg_is_atapi == TRUE) { 10344 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10345 MODEPAGE_CACHING, SD_PATH_DIRECT); 10346 } else { 10347 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10348 MODEPAGE_CACHING, SD_PATH_DIRECT); 10349 } 10350 if (rval != 0) { 10351 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10352 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 10353 kmem_free(header, buflen); 10354 return (rval); 10355 } 10356 10357 /* 10358 * Determine size of Block Descriptors in order to locate 10359 * the mode page data. ATAPI devices return 0, SCSI devices 10360 * should return MODE_BLK_DESC_LENGTH. 10361 */ 10362 if (un->un_f_cfg_is_atapi == TRUE) { 10363 struct mode_header_grp2 *mhp; 10364 mhp = (struct mode_header_grp2 *)header; 10365 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10366 } else { 10367 bd_len = ((struct mode_header *)header)->bdesc_length; 10368 } 10369 10370 if (bd_len > MODE_BLK_DESC_LENGTH) { 10371 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10372 "sd_get_write_cache_enabled: Mode Sense returned invalid " 10373 "block descriptor length\n"); 10374 kmem_free(header, buflen); 10375 return (EIO); 10376 } 10377 10378 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10379 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10380 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10381 " caching page code mismatch %d\n", 10382 mode_caching_page->mode_page.code); 10383 kmem_free(header, buflen); 10384 return (EIO); 10385 } 10386 *is_enabled = mode_caching_page->wce; 10387 10388 kmem_free(header, buflen); 10389 return (0); 10390 } 10391 10392 10393 /* 10394 * Function: sd_make_device 10395 * 10396 * Description: Utility routine to return the Solaris device number from 10397 * the data in the device's dev_info structure. 10398 * 10399 * Return Code: The Solaris device number 10400 * 10401 * Context: Any 10402 */ 10403 10404 static dev_t 10405 sd_make_device(dev_info_t *devi) 10406 { 10407 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 10408 ddi_get_instance(devi) << SDUNIT_SHIFT)); 10409 } 10410 10411 10412 /* 10413 * Function: sd_pm_entry 10414 * 10415 * Description: Called at the start of a new command to manage power 10416 * and busy status of a device. This includes determining whether 10417 * the current power state of the device is sufficient for 10418 * performing the command or whether it must be changed. 10419 * The PM framework is notified appropriately. 10420 * Only with a return status of DDI_SUCCESS will the 10421 * component be busy to the framework. 10422 * 10423 * All callers of sd_pm_entry must check the return status 10424 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 10425 * of DDI_FAILURE indicates the device failed to power up. 10426 * In this case un_pm_count has been adjusted so the result 10427 * on exit is still powered down, ie. count is less than 0. 10428 * Calling sd_pm_exit with this count value hits an ASSERT. 10429 * 10430 * Return Code: DDI_SUCCESS or DDI_FAILURE 10431 * 10432 * Context: Kernel thread context. 10433 */ 10434 10435 static int 10436 sd_pm_entry(struct sd_lun *un) 10437 { 10438 int return_status = DDI_SUCCESS; 10439 10440 ASSERT(!mutex_owned(SD_MUTEX(un))); 10441 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10442 10443 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 10444 10445 if (un->un_f_pm_is_enabled == FALSE) { 10446 SD_TRACE(SD_LOG_IO_PM, un, 10447 "sd_pm_entry: exiting, PM not enabled\n"); 10448 return (return_status); 10449 } 10450 10451 /* 10452 * Just increment a counter if PM is enabled. On the transition from 10453 * 0 ==> 1, mark the device as busy. The iodone side will decrement 10454 * the count with each IO and mark the device as idle when the count 10455 * hits 0. 10456 * 10457 * If the count is less than 0 the device is powered down. If a powered 10458 * down device is successfully powered up then the count must be 10459 * incremented to reflect the power up. Note that it'll get incremented 10460 * a second time to become busy. 10461 * 10462 * Because the following has the potential to change the device state 10463 * and must release the un_pm_mutex to do so, only one thread can be 10464 * allowed through at a time. 10465 */ 10466 10467 mutex_enter(&un->un_pm_mutex); 10468 while (un->un_pm_busy == TRUE) { 10469 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 10470 } 10471 un->un_pm_busy = TRUE; 10472 10473 if (un->un_pm_count < 1) { 10474 10475 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 10476 10477 /* 10478 * Indicate we are now busy so the framework won't attempt to 10479 * power down the device. This call will only fail if either 10480 * we passed a bad component number or the device has no 10481 * components. Neither of these should ever happen. 10482 */ 10483 mutex_exit(&un->un_pm_mutex); 10484 return_status = pm_busy_component(SD_DEVINFO(un), 0); 10485 ASSERT(return_status == DDI_SUCCESS); 10486 10487 mutex_enter(&un->un_pm_mutex); 10488 10489 if (un->un_pm_count < 0) { 10490 mutex_exit(&un->un_pm_mutex); 10491 10492 SD_TRACE(SD_LOG_IO_PM, un, 10493 "sd_pm_entry: power up component\n"); 10494 10495 /* 10496 * pm_raise_power will cause sdpower to be called 10497 * which brings the device power level to the 10498 * desired state, ON in this case. If successful, 10499 * un_pm_count and un_power_level will be updated 10500 * appropriately. 10501 */ 10502 return_status = pm_raise_power(SD_DEVINFO(un), 0, 10503 SD_SPINDLE_ON); 10504 10505 mutex_enter(&un->un_pm_mutex); 10506 10507 if (return_status != DDI_SUCCESS) { 10508 /* 10509 * Power up failed. 10510 * Idle the device and adjust the count 10511 * so the result on exit is that we're 10512 * still powered down, ie. count is less than 0. 10513 */ 10514 SD_TRACE(SD_LOG_IO_PM, un, 10515 "sd_pm_entry: power up failed," 10516 " idle the component\n"); 10517 10518 (void) pm_idle_component(SD_DEVINFO(un), 0); 10519 un->un_pm_count--; 10520 } else { 10521 /* 10522 * Device is powered up, verify the 10523 * count is non-negative. 10524 * This is debug only. 10525 */ 10526 ASSERT(un->un_pm_count == 0); 10527 } 10528 } 10529 10530 if (return_status == DDI_SUCCESS) { 10531 /* 10532 * For performance, now that the device has been tagged 10533 * as busy, and it's known to be powered up, update the 10534 * chain types to use jump tables that do not include 10535 * pm. This significantly lowers the overhead and 10536 * therefore improves performance. 10537 */ 10538 10539 mutex_exit(&un->un_pm_mutex); 10540 mutex_enter(SD_MUTEX(un)); 10541 SD_TRACE(SD_LOG_IO_PM, un, 10542 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10543 un->un_uscsi_chain_type); 10544 10545 if (un->un_f_non_devbsize_supported) { 10546 un->un_buf_chain_type = 10547 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10548 } else { 10549 un->un_buf_chain_type = 10550 SD_CHAIN_INFO_DISK_NO_PM; 10551 } 10552 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10553 10554 SD_TRACE(SD_LOG_IO_PM, un, 10555 " changed uscsi_chain_type to %d\n", 10556 un->un_uscsi_chain_type); 10557 mutex_exit(SD_MUTEX(un)); 10558 mutex_enter(&un->un_pm_mutex); 10559 10560 if (un->un_pm_idle_timeid == NULL) { 10561 /* 300 ms. */ 10562 un->un_pm_idle_timeid = 10563 timeout(sd_pm_idletimeout_handler, un, 10564 (drv_usectohz((clock_t)300000))); 10565 /* 10566 * Include an extra call to busy which keeps the 10567 * device busy with-respect-to the PM layer 10568 * until the timer fires, at which time it'll 10569 * get the extra idle call. 10570 */ 10571 (void) pm_busy_component(SD_DEVINFO(un), 0); 10572 } 10573 } 10574 } 10575 un->un_pm_busy = FALSE; 10576 /* Next... */ 10577 cv_signal(&un->un_pm_busy_cv); 10578 10579 un->un_pm_count++; 10580 10581 SD_TRACE(SD_LOG_IO_PM, un, 10582 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10583 10584 mutex_exit(&un->un_pm_mutex); 10585 10586 return (return_status); 10587 } 10588 10589 10590 /* 10591 * Function: sd_pm_exit 10592 * 10593 * Description: Called at the completion of a command to manage busy 10594 * status for the device. If the device becomes idle the 10595 * PM framework is notified. 10596 * 10597 * Context: Kernel thread context 10598 */ 10599 10600 static void 10601 sd_pm_exit(struct sd_lun *un) 10602 { 10603 ASSERT(!mutex_owned(SD_MUTEX(un))); 10604 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10605 10606 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10607 10608 /* 10609 * After attach the following flag is only read, so don't 10610 * take the penalty of acquiring a mutex for it. 10611 */ 10612 if (un->un_f_pm_is_enabled == TRUE) { 10613 10614 mutex_enter(&un->un_pm_mutex); 10615 un->un_pm_count--; 10616 10617 SD_TRACE(SD_LOG_IO_PM, un, 10618 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10619 10620 ASSERT(un->un_pm_count >= 0); 10621 if (un->un_pm_count == 0) { 10622 mutex_exit(&un->un_pm_mutex); 10623 10624 SD_TRACE(SD_LOG_IO_PM, un, 10625 "sd_pm_exit: idle component\n"); 10626 10627 (void) pm_idle_component(SD_DEVINFO(un), 0); 10628 10629 } else { 10630 mutex_exit(&un->un_pm_mutex); 10631 } 10632 } 10633 10634 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10635 } 10636 10637 10638 /* 10639 * Function: sdopen 10640 * 10641 * Description: Driver's open(9e) entry point function. 10642 * 10643 * Arguments: dev_i - pointer to device number 10644 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10645 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10646 * cred_p - user credential pointer 10647 * 10648 * Return Code: EINVAL 10649 * ENXIO 10650 * EIO 10651 * EROFS 10652 * EBUSY 10653 * 10654 * Context: Kernel thread context 10655 */ 10656 /* ARGSUSED */ 10657 static int 10658 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10659 { 10660 struct sd_lun *un; 10661 int nodelay; 10662 int part; 10663 uint64_t partmask; 10664 int instance; 10665 dev_t dev; 10666 int rval = EIO; 10667 10668 /* Validate the open type */ 10669 if (otyp >= OTYPCNT) { 10670 return (EINVAL); 10671 } 10672 10673 dev = *dev_p; 10674 instance = SDUNIT(dev); 10675 mutex_enter(&sd_detach_mutex); 10676 10677 /* 10678 * Fail the open if there is no softstate for the instance, or 10679 * if another thread somewhere is trying to detach the instance. 10680 */ 10681 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10682 (un->un_detach_count != 0)) { 10683 mutex_exit(&sd_detach_mutex); 10684 /* 10685 * The probe cache only needs to be cleared when open (9e) fails 10686 * with ENXIO (4238046). 10687 */ 10688 /* 10689 * un-conditionally clearing probe cache is ok with 10690 * separate sd/ssd binaries 10691 * x86 platform can be an issue with both parallel 10692 * and fibre in 1 binary 10693 */ 10694 sd_scsi_clear_probe_cache(); 10695 return (ENXIO); 10696 } 10697 10698 /* 10699 * The un_layer_count is to prevent another thread in specfs from 10700 * trying to detach the instance, which can happen when we are 10701 * called from a higher-layer driver instead of thru specfs. 10702 * This will not be needed when DDI provides a layered driver 10703 * interface that allows specfs to know that an instance is in 10704 * use by a layered driver & should not be detached. 10705 * 10706 * Note: the semantics for layered driver opens are exactly one 10707 * close for every open. 10708 */ 10709 if (otyp == OTYP_LYR) { 10710 un->un_layer_count++; 10711 } 10712 10713 /* 10714 * Keep a count of the current # of opens in progress. This is because 10715 * some layered drivers try to call us as a regular open. This can 10716 * cause problems that we cannot prevent, however by keeping this count 10717 * we can at least keep our open and detach routines from racing against 10718 * each other under such conditions. 10719 */ 10720 un->un_opens_in_progress++; 10721 mutex_exit(&sd_detach_mutex); 10722 10723 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10724 part = SDPART(dev); 10725 partmask = 1 << part; 10726 10727 /* 10728 * We use a semaphore here in order to serialize 10729 * open and close requests on the device. 10730 */ 10731 sema_p(&un->un_semoclose); 10732 10733 mutex_enter(SD_MUTEX(un)); 10734 10735 /* 10736 * All device accesses go thru sdstrategy() where we check 10737 * on suspend status but there could be a scsi_poll command, 10738 * which bypasses sdstrategy(), so we need to check pm 10739 * status. 10740 */ 10741 10742 if (!nodelay) { 10743 while ((un->un_state == SD_STATE_SUSPENDED) || 10744 (un->un_state == SD_STATE_PM_CHANGING)) { 10745 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10746 } 10747 10748 mutex_exit(SD_MUTEX(un)); 10749 if (sd_pm_entry(un) != DDI_SUCCESS) { 10750 rval = EIO; 10751 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10752 "sdopen: sd_pm_entry failed\n"); 10753 goto open_failed_with_pm; 10754 } 10755 mutex_enter(SD_MUTEX(un)); 10756 } 10757 10758 /* check for previous exclusive open */ 10759 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10760 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10761 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10762 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10763 10764 if (un->un_exclopen & (partmask)) { 10765 goto excl_open_fail; 10766 } 10767 10768 if (flag & FEXCL) { 10769 int i; 10770 if (un->un_ocmap.lyropen[part]) { 10771 goto excl_open_fail; 10772 } 10773 for (i = 0; i < (OTYPCNT - 1); i++) { 10774 if (un->un_ocmap.regopen[i] & (partmask)) { 10775 goto excl_open_fail; 10776 } 10777 } 10778 } 10779 10780 /* 10781 * Check the write permission if this is a removable media device, 10782 * NDELAY has not been set, and writable permission is requested. 10783 * 10784 * Note: If NDELAY was set and this is write-protected media the WRITE 10785 * attempt will fail with EIO as part of the I/O processing. This is a 10786 * more permissive implementation that allows the open to succeed and 10787 * WRITE attempts to fail when appropriate. 10788 */ 10789 if (un->un_f_chk_wp_open) { 10790 if ((flag & FWRITE) && (!nodelay)) { 10791 mutex_exit(SD_MUTEX(un)); 10792 /* 10793 * Defer the check for write permission on writable 10794 * DVD drive till sdstrategy and will not fail open even 10795 * if FWRITE is set as the device can be writable 10796 * depending upon the media and the media can change 10797 * after the call to open(). 10798 */ 10799 if (un->un_f_dvdram_writable_device == FALSE) { 10800 if (ISCD(un) || sr_check_wp(dev)) { 10801 rval = EROFS; 10802 mutex_enter(SD_MUTEX(un)); 10803 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10804 "write to cd or write protected media\n"); 10805 goto open_fail; 10806 } 10807 } 10808 mutex_enter(SD_MUTEX(un)); 10809 } 10810 } 10811 10812 /* 10813 * If opening in NDELAY/NONBLOCK mode, just return. 10814 * Check if disk is ready and has a valid geometry later. 10815 */ 10816 if (!nodelay) { 10817 mutex_exit(SD_MUTEX(un)); 10818 rval = sd_ready_and_valid(un); 10819 mutex_enter(SD_MUTEX(un)); 10820 /* 10821 * Fail if device is not ready or if the number of disk 10822 * blocks is zero or negative for non CD devices. 10823 */ 10824 if ((rval != SD_READY_VALID) || 10825 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10826 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10827 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10828 "device not ready or invalid disk block value\n"); 10829 goto open_fail; 10830 } 10831 #if defined(__i386) || defined(__amd64) 10832 } else { 10833 uchar_t *cp; 10834 /* 10835 * x86 requires special nodelay handling, so that p0 is 10836 * always defined and accessible. 10837 * Invalidate geometry only if device is not already open. 10838 */ 10839 cp = &un->un_ocmap.chkd[0]; 10840 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10841 if (*cp != (uchar_t)0) { 10842 break; 10843 } 10844 cp++; 10845 } 10846 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10847 un->un_f_geometry_is_valid = FALSE; 10848 } 10849 10850 #endif 10851 } 10852 10853 if (otyp == OTYP_LYR) { 10854 un->un_ocmap.lyropen[part]++; 10855 } else { 10856 un->un_ocmap.regopen[otyp] |= partmask; 10857 } 10858 10859 /* Set up open and exclusive open flags */ 10860 if (flag & FEXCL) { 10861 un->un_exclopen |= (partmask); 10862 } 10863 10864 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10865 "open of part %d type %d\n", part, otyp); 10866 10867 mutex_exit(SD_MUTEX(un)); 10868 if (!nodelay) { 10869 sd_pm_exit(un); 10870 } 10871 10872 sema_v(&un->un_semoclose); 10873 10874 mutex_enter(&sd_detach_mutex); 10875 un->un_opens_in_progress--; 10876 mutex_exit(&sd_detach_mutex); 10877 10878 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10879 return (DDI_SUCCESS); 10880 10881 excl_open_fail: 10882 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10883 rval = EBUSY; 10884 10885 open_fail: 10886 mutex_exit(SD_MUTEX(un)); 10887 10888 /* 10889 * On a failed open we must exit the pm management. 10890 */ 10891 if (!nodelay) { 10892 sd_pm_exit(un); 10893 } 10894 open_failed_with_pm: 10895 sema_v(&un->un_semoclose); 10896 10897 mutex_enter(&sd_detach_mutex); 10898 un->un_opens_in_progress--; 10899 if (otyp == OTYP_LYR) { 10900 un->un_layer_count--; 10901 } 10902 mutex_exit(&sd_detach_mutex); 10903 10904 return (rval); 10905 } 10906 10907 10908 /* 10909 * Function: sdclose 10910 * 10911 * Description: Driver's close(9e) entry point function. 10912 * 10913 * Arguments: dev - device number 10914 * flag - file status flag, informational only 10915 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10916 * cred_p - user credential pointer 10917 * 10918 * Return Code: ENXIO 10919 * 10920 * Context: Kernel thread context 10921 */ 10922 /* ARGSUSED */ 10923 static int 10924 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10925 { 10926 struct sd_lun *un; 10927 uchar_t *cp; 10928 int part; 10929 int nodelay; 10930 int rval = 0; 10931 10932 /* Validate the open type */ 10933 if (otyp >= OTYPCNT) { 10934 return (ENXIO); 10935 } 10936 10937 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10938 return (ENXIO); 10939 } 10940 10941 part = SDPART(dev); 10942 nodelay = flag & (FNDELAY | FNONBLOCK); 10943 10944 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10945 "sdclose: close of part %d type %d\n", part, otyp); 10946 10947 /* 10948 * We use a semaphore here in order to serialize 10949 * open and close requests on the device. 10950 */ 10951 sema_p(&un->un_semoclose); 10952 10953 mutex_enter(SD_MUTEX(un)); 10954 10955 /* Don't proceed if power is being changed. */ 10956 while (un->un_state == SD_STATE_PM_CHANGING) { 10957 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10958 } 10959 10960 if (un->un_exclopen & (1 << part)) { 10961 un->un_exclopen &= ~(1 << part); 10962 } 10963 10964 /* Update the open partition map */ 10965 if (otyp == OTYP_LYR) { 10966 un->un_ocmap.lyropen[part] -= 1; 10967 } else { 10968 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10969 } 10970 10971 cp = &un->un_ocmap.chkd[0]; 10972 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10973 if (*cp != NULL) { 10974 break; 10975 } 10976 cp++; 10977 } 10978 10979 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10980 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10981 10982 /* 10983 * We avoid persistance upon the last close, and set 10984 * the throttle back to the maximum. 10985 */ 10986 un->un_throttle = un->un_saved_throttle; 10987 10988 if (un->un_state == SD_STATE_OFFLINE) { 10989 if (un->un_f_is_fibre == FALSE) { 10990 scsi_log(SD_DEVINFO(un), sd_label, 10991 CE_WARN, "offline\n"); 10992 } 10993 un->un_f_geometry_is_valid = FALSE; 10994 10995 } else { 10996 /* 10997 * Flush any outstanding writes in NVRAM cache. 10998 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 10999 * cmd, it may not work for non-Pluto devices. 11000 * SYNCHRONIZE CACHE is not required for removables, 11001 * except DVD-RAM drives. 11002 * 11003 * Also note: because SYNCHRONIZE CACHE is currently 11004 * the only command issued here that requires the 11005 * drive be powered up, only do the power up before 11006 * sending the Sync Cache command. If additional 11007 * commands are added which require a powered up 11008 * drive, the following sequence may have to change. 11009 * 11010 * And finally, note that parallel SCSI on SPARC 11011 * only issues a Sync Cache to DVD-RAM, a newly 11012 * supported device. 11013 */ 11014 #if defined(__i386) || defined(__amd64) 11015 if (un->un_f_sync_cache_supported || 11016 un->un_f_dvdram_writable_device == TRUE) { 11017 #else 11018 if (un->un_f_dvdram_writable_device == TRUE) { 11019 #endif 11020 mutex_exit(SD_MUTEX(un)); 11021 if (sd_pm_entry(un) == DDI_SUCCESS) { 11022 rval = 11023 sd_send_scsi_SYNCHRONIZE_CACHE(un, 11024 NULL); 11025 /* ignore error if not supported */ 11026 if (rval == ENOTSUP) { 11027 rval = 0; 11028 } else if (rval != 0) { 11029 rval = EIO; 11030 } 11031 sd_pm_exit(un); 11032 } else { 11033 rval = EIO; 11034 } 11035 mutex_enter(SD_MUTEX(un)); 11036 } 11037 11038 /* 11039 * For devices which supports DOOR_LOCK, send an ALLOW 11040 * MEDIA REMOVAL command, but don't get upset if it 11041 * fails. We need to raise the power of the drive before 11042 * we can call sd_send_scsi_DOORLOCK() 11043 */ 11044 if (un->un_f_doorlock_supported) { 11045 mutex_exit(SD_MUTEX(un)); 11046 if (sd_pm_entry(un) == DDI_SUCCESS) { 11047 rval = sd_send_scsi_DOORLOCK(un, 11048 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 11049 11050 sd_pm_exit(un); 11051 if (ISCD(un) && (rval != 0) && 11052 (nodelay != 0)) { 11053 rval = ENXIO; 11054 } 11055 } else { 11056 rval = EIO; 11057 } 11058 mutex_enter(SD_MUTEX(un)); 11059 } 11060 11061 /* 11062 * If a device has removable media, invalidate all 11063 * parameters related to media, such as geometry, 11064 * blocksize, and blockcount. 11065 */ 11066 if (un->un_f_has_removable_media) { 11067 sr_ejected(un); 11068 } 11069 11070 /* 11071 * Destroy the cache (if it exists) which was 11072 * allocated for the write maps since this is 11073 * the last close for this media. 11074 */ 11075 if (un->un_wm_cache) { 11076 /* 11077 * Check if there are pending commands. 11078 * and if there are give a warning and 11079 * do not destroy the cache. 11080 */ 11081 if (un->un_ncmds_in_driver > 0) { 11082 scsi_log(SD_DEVINFO(un), 11083 sd_label, CE_WARN, 11084 "Unable to clean up memory " 11085 "because of pending I/O\n"); 11086 } else { 11087 kmem_cache_destroy( 11088 un->un_wm_cache); 11089 un->un_wm_cache = NULL; 11090 } 11091 } 11092 } 11093 } 11094 11095 mutex_exit(SD_MUTEX(un)); 11096 sema_v(&un->un_semoclose); 11097 11098 if (otyp == OTYP_LYR) { 11099 mutex_enter(&sd_detach_mutex); 11100 /* 11101 * The detach routine may run when the layer count 11102 * drops to zero. 11103 */ 11104 un->un_layer_count--; 11105 mutex_exit(&sd_detach_mutex); 11106 } 11107 11108 return (rval); 11109 } 11110 11111 11112 /* 11113 * Function: sd_ready_and_valid 11114 * 11115 * Description: Test if device is ready and has a valid geometry. 11116 * 11117 * Arguments: dev - device number 11118 * un - driver soft state (unit) structure 11119 * 11120 * Return Code: SD_READY_VALID ready and valid label 11121 * SD_READY_NOT_VALID ready, geom ops never applicable 11122 * SD_NOT_READY_VALID not ready, no label 11123 * SD_RESERVED_BY_OTHERS reservation conflict 11124 * 11125 * Context: Never called at interrupt context. 11126 */ 11127 11128 static int 11129 sd_ready_and_valid(struct sd_lun *un) 11130 { 11131 struct sd_errstats *stp; 11132 uint64_t capacity; 11133 uint_t lbasize; 11134 int rval = SD_READY_VALID; 11135 char name_str[48]; 11136 11137 ASSERT(un != NULL); 11138 ASSERT(!mutex_owned(SD_MUTEX(un))); 11139 11140 mutex_enter(SD_MUTEX(un)); 11141 /* 11142 * If a device has removable media, we must check if media is 11143 * ready when checking if this device is ready and valid. 11144 */ 11145 if (un->un_f_has_removable_media) { 11146 mutex_exit(SD_MUTEX(un)); 11147 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 11148 rval = SD_NOT_READY_VALID; 11149 mutex_enter(SD_MUTEX(un)); 11150 goto done; 11151 } 11152 11153 mutex_enter(SD_MUTEX(un)); 11154 if ((un->un_f_geometry_is_valid == FALSE) || 11155 (un->un_f_blockcount_is_valid == FALSE) || 11156 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 11157 11158 /* capacity has to be read every open. */ 11159 mutex_exit(SD_MUTEX(un)); 11160 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 11161 &lbasize, SD_PATH_DIRECT) != 0) { 11162 mutex_enter(SD_MUTEX(un)); 11163 un->un_f_geometry_is_valid = FALSE; 11164 rval = SD_NOT_READY_VALID; 11165 goto done; 11166 } else { 11167 mutex_enter(SD_MUTEX(un)); 11168 sd_update_block_info(un, lbasize, capacity); 11169 } 11170 } 11171 11172 /* 11173 * Check if the media in the device is writable or not. 11174 */ 11175 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 11176 sd_check_for_writable_cd(un); 11177 } 11178 11179 } else { 11180 /* 11181 * Do a test unit ready to clear any unit attention from non-cd 11182 * devices. 11183 */ 11184 mutex_exit(SD_MUTEX(un)); 11185 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 11186 mutex_enter(SD_MUTEX(un)); 11187 } 11188 11189 11190 /* 11191 * If this is a non 512 block device, allocate space for 11192 * the wmap cache. This is being done here since every time 11193 * a media is changed this routine will be called and the 11194 * block size is a function of media rather than device. 11195 */ 11196 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 11197 if (!(un->un_wm_cache)) { 11198 (void) snprintf(name_str, sizeof (name_str), 11199 "%s%d_cache", 11200 ddi_driver_name(SD_DEVINFO(un)), 11201 ddi_get_instance(SD_DEVINFO(un))); 11202 un->un_wm_cache = kmem_cache_create( 11203 name_str, sizeof (struct sd_w_map), 11204 8, sd_wm_cache_constructor, 11205 sd_wm_cache_destructor, NULL, 11206 (void *)un, NULL, 0); 11207 if (!(un->un_wm_cache)) { 11208 rval = ENOMEM; 11209 goto done; 11210 } 11211 } 11212 } 11213 11214 if (un->un_state == SD_STATE_NORMAL) { 11215 /* 11216 * If the target is not yet ready here (defined by a TUR 11217 * failure), invalidate the geometry and print an 'offline' 11218 * message. This is a legacy message, as the state of the 11219 * target is not actually changed to SD_STATE_OFFLINE. 11220 * 11221 * If the TUR fails for EACCES (Reservation Conflict), 11222 * SD_RESERVED_BY_OTHERS will be returned to indicate 11223 * reservation conflict. If the TUR fails for other 11224 * reasons, SD_NOT_READY_VALID will be returned. 11225 */ 11226 int err; 11227 11228 mutex_exit(SD_MUTEX(un)); 11229 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 11230 mutex_enter(SD_MUTEX(un)); 11231 11232 if (err != 0) { 11233 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11234 "offline or reservation conflict\n"); 11235 un->un_f_geometry_is_valid = FALSE; 11236 if (err == EACCES) { 11237 rval = SD_RESERVED_BY_OTHERS; 11238 } else { 11239 rval = SD_NOT_READY_VALID; 11240 } 11241 goto done; 11242 } 11243 } 11244 11245 if (un->un_f_format_in_progress == FALSE) { 11246 /* 11247 * Note: sd_validate_geometry may return TRUE, but that does 11248 * not necessarily mean un_f_geometry_is_valid == TRUE! 11249 */ 11250 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 11251 if (rval == ENOTSUP) { 11252 if (un->un_f_geometry_is_valid == TRUE) 11253 rval = 0; 11254 else { 11255 rval = SD_READY_NOT_VALID; 11256 goto done; 11257 } 11258 } 11259 if (rval != 0) { 11260 /* 11261 * We don't check the validity of geometry for 11262 * CDROMs. Also we assume we have a good label 11263 * even if sd_validate_geometry returned ENOMEM. 11264 */ 11265 if (!ISCD(un) && rval != ENOMEM) { 11266 rval = SD_NOT_READY_VALID; 11267 goto done; 11268 } 11269 } 11270 } 11271 11272 /* 11273 * If this device supports DOOR_LOCK command, try and send 11274 * this command to PREVENT MEDIA REMOVAL, but don't get upset 11275 * if it fails. For a CD, however, it is an error 11276 */ 11277 if (un->un_f_doorlock_supported) { 11278 mutex_exit(SD_MUTEX(un)); 11279 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 11280 SD_PATH_DIRECT) != 0) && ISCD(un)) { 11281 rval = SD_NOT_READY_VALID; 11282 mutex_enter(SD_MUTEX(un)); 11283 goto done; 11284 } 11285 mutex_enter(SD_MUTEX(un)); 11286 } 11287 11288 /* The state has changed, inform the media watch routines */ 11289 un->un_mediastate = DKIO_INSERTED; 11290 cv_broadcast(&un->un_state_cv); 11291 rval = SD_READY_VALID; 11292 11293 done: 11294 11295 /* 11296 * Initialize the capacity kstat value, if no media previously 11297 * (capacity kstat is 0) and a media has been inserted 11298 * (un_blockcount > 0). 11299 */ 11300 if (un->un_errstats != NULL) { 11301 stp = (struct sd_errstats *)un->un_errstats->ks_data; 11302 if ((stp->sd_capacity.value.ui64 == 0) && 11303 (un->un_f_blockcount_is_valid == TRUE)) { 11304 stp->sd_capacity.value.ui64 = 11305 (uint64_t)((uint64_t)un->un_blockcount * 11306 un->un_sys_blocksize); 11307 } 11308 } 11309 11310 mutex_exit(SD_MUTEX(un)); 11311 return (rval); 11312 } 11313 11314 11315 /* 11316 * Function: sdmin 11317 * 11318 * Description: Routine to limit the size of a data transfer. Used in 11319 * conjunction with physio(9F). 11320 * 11321 * Arguments: bp - pointer to the indicated buf(9S) struct. 11322 * 11323 * Context: Kernel thread context. 11324 */ 11325 11326 static void 11327 sdmin(struct buf *bp) 11328 { 11329 struct sd_lun *un; 11330 int instance; 11331 11332 instance = SDUNIT(bp->b_edev); 11333 11334 un = ddi_get_soft_state(sd_state, instance); 11335 ASSERT(un != NULL); 11336 11337 if (bp->b_bcount > un->un_max_xfer_size) { 11338 bp->b_bcount = un->un_max_xfer_size; 11339 } 11340 } 11341 11342 11343 /* 11344 * Function: sdread 11345 * 11346 * Description: Driver's read(9e) entry point function. 11347 * 11348 * Arguments: dev - device number 11349 * uio - structure pointer describing where data is to be stored 11350 * in user's space 11351 * cred_p - user credential pointer 11352 * 11353 * Return Code: ENXIO 11354 * EIO 11355 * EINVAL 11356 * value returned by physio 11357 * 11358 * Context: Kernel thread context. 11359 */ 11360 /* ARGSUSED */ 11361 static int 11362 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 11363 { 11364 struct sd_lun *un = NULL; 11365 int secmask; 11366 int err; 11367 11368 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11369 return (ENXIO); 11370 } 11371 11372 ASSERT(!mutex_owned(SD_MUTEX(un))); 11373 11374 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11375 mutex_enter(SD_MUTEX(un)); 11376 /* 11377 * Because the call to sd_ready_and_valid will issue I/O we 11378 * must wait here if either the device is suspended or 11379 * if it's power level is changing. 11380 */ 11381 while ((un->un_state == SD_STATE_SUSPENDED) || 11382 (un->un_state == SD_STATE_PM_CHANGING)) { 11383 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11384 } 11385 un->un_ncmds_in_driver++; 11386 mutex_exit(SD_MUTEX(un)); 11387 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11388 mutex_enter(SD_MUTEX(un)); 11389 un->un_ncmds_in_driver--; 11390 ASSERT(un->un_ncmds_in_driver >= 0); 11391 mutex_exit(SD_MUTEX(un)); 11392 return (EIO); 11393 } 11394 mutex_enter(SD_MUTEX(un)); 11395 un->un_ncmds_in_driver--; 11396 ASSERT(un->un_ncmds_in_driver >= 0); 11397 mutex_exit(SD_MUTEX(un)); 11398 } 11399 11400 /* 11401 * Read requests are restricted to multiples of the system block size. 11402 */ 11403 secmask = un->un_sys_blocksize - 1; 11404 11405 if (uio->uio_loffset & ((offset_t)(secmask))) { 11406 SD_ERROR(SD_LOG_READ_WRITE, un, 11407 "sdread: file offset not modulo %d\n", 11408 un->un_sys_blocksize); 11409 err = EINVAL; 11410 } else if (uio->uio_iov->iov_len & (secmask)) { 11411 SD_ERROR(SD_LOG_READ_WRITE, un, 11412 "sdread: transfer length not modulo %d\n", 11413 un->un_sys_blocksize); 11414 err = EINVAL; 11415 } else { 11416 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 11417 } 11418 return (err); 11419 } 11420 11421 11422 /* 11423 * Function: sdwrite 11424 * 11425 * Description: Driver's write(9e) entry point function. 11426 * 11427 * Arguments: dev - device number 11428 * uio - structure pointer describing where data is stored in 11429 * user's space 11430 * cred_p - user credential pointer 11431 * 11432 * Return Code: ENXIO 11433 * EIO 11434 * EINVAL 11435 * value returned by physio 11436 * 11437 * Context: Kernel thread context. 11438 */ 11439 /* ARGSUSED */ 11440 static int 11441 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11442 { 11443 struct sd_lun *un = NULL; 11444 int secmask; 11445 int err; 11446 11447 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11448 return (ENXIO); 11449 } 11450 11451 ASSERT(!mutex_owned(SD_MUTEX(un))); 11452 11453 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11454 mutex_enter(SD_MUTEX(un)); 11455 /* 11456 * Because the call to sd_ready_and_valid will issue I/O we 11457 * must wait here if either the device is suspended or 11458 * if it's power level is changing. 11459 */ 11460 while ((un->un_state == SD_STATE_SUSPENDED) || 11461 (un->un_state == SD_STATE_PM_CHANGING)) { 11462 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11463 } 11464 un->un_ncmds_in_driver++; 11465 mutex_exit(SD_MUTEX(un)); 11466 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11467 mutex_enter(SD_MUTEX(un)); 11468 un->un_ncmds_in_driver--; 11469 ASSERT(un->un_ncmds_in_driver >= 0); 11470 mutex_exit(SD_MUTEX(un)); 11471 return (EIO); 11472 } 11473 mutex_enter(SD_MUTEX(un)); 11474 un->un_ncmds_in_driver--; 11475 ASSERT(un->un_ncmds_in_driver >= 0); 11476 mutex_exit(SD_MUTEX(un)); 11477 } 11478 11479 /* 11480 * Write requests are restricted to multiples of the system block size. 11481 */ 11482 secmask = un->un_sys_blocksize - 1; 11483 11484 if (uio->uio_loffset & ((offset_t)(secmask))) { 11485 SD_ERROR(SD_LOG_READ_WRITE, un, 11486 "sdwrite: file offset not modulo %d\n", 11487 un->un_sys_blocksize); 11488 err = EINVAL; 11489 } else if (uio->uio_iov->iov_len & (secmask)) { 11490 SD_ERROR(SD_LOG_READ_WRITE, un, 11491 "sdwrite: transfer length not modulo %d\n", 11492 un->un_sys_blocksize); 11493 err = EINVAL; 11494 } else { 11495 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11496 } 11497 return (err); 11498 } 11499 11500 11501 /* 11502 * Function: sdaread 11503 * 11504 * Description: Driver's aread(9e) entry point function. 11505 * 11506 * Arguments: dev - device number 11507 * aio - structure pointer describing where data is to be stored 11508 * cred_p - user credential pointer 11509 * 11510 * Return Code: ENXIO 11511 * EIO 11512 * EINVAL 11513 * value returned by aphysio 11514 * 11515 * Context: Kernel thread context. 11516 */ 11517 /* ARGSUSED */ 11518 static int 11519 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11520 { 11521 struct sd_lun *un = NULL; 11522 struct uio *uio = aio->aio_uio; 11523 int secmask; 11524 int err; 11525 11526 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11527 return (ENXIO); 11528 } 11529 11530 ASSERT(!mutex_owned(SD_MUTEX(un))); 11531 11532 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11533 mutex_enter(SD_MUTEX(un)); 11534 /* 11535 * Because the call to sd_ready_and_valid will issue I/O we 11536 * must wait here if either the device is suspended or 11537 * if it's power level is changing. 11538 */ 11539 while ((un->un_state == SD_STATE_SUSPENDED) || 11540 (un->un_state == SD_STATE_PM_CHANGING)) { 11541 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11542 } 11543 un->un_ncmds_in_driver++; 11544 mutex_exit(SD_MUTEX(un)); 11545 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11546 mutex_enter(SD_MUTEX(un)); 11547 un->un_ncmds_in_driver--; 11548 ASSERT(un->un_ncmds_in_driver >= 0); 11549 mutex_exit(SD_MUTEX(un)); 11550 return (EIO); 11551 } 11552 mutex_enter(SD_MUTEX(un)); 11553 un->un_ncmds_in_driver--; 11554 ASSERT(un->un_ncmds_in_driver >= 0); 11555 mutex_exit(SD_MUTEX(un)); 11556 } 11557 11558 /* 11559 * Read requests are restricted to multiples of the system block size. 11560 */ 11561 secmask = un->un_sys_blocksize - 1; 11562 11563 if (uio->uio_loffset & ((offset_t)(secmask))) { 11564 SD_ERROR(SD_LOG_READ_WRITE, un, 11565 "sdaread: file offset not modulo %d\n", 11566 un->un_sys_blocksize); 11567 err = EINVAL; 11568 } else if (uio->uio_iov->iov_len & (secmask)) { 11569 SD_ERROR(SD_LOG_READ_WRITE, un, 11570 "sdaread: transfer length not modulo %d\n", 11571 un->un_sys_blocksize); 11572 err = EINVAL; 11573 } else { 11574 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11575 } 11576 return (err); 11577 } 11578 11579 11580 /* 11581 * Function: sdawrite 11582 * 11583 * Description: Driver's awrite(9e) entry point function. 11584 * 11585 * Arguments: dev - device number 11586 * aio - structure pointer describing where data is stored 11587 * cred_p - user credential pointer 11588 * 11589 * Return Code: ENXIO 11590 * EIO 11591 * EINVAL 11592 * value returned by aphysio 11593 * 11594 * Context: Kernel thread context. 11595 */ 11596 /* ARGSUSED */ 11597 static int 11598 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11599 { 11600 struct sd_lun *un = NULL; 11601 struct uio *uio = aio->aio_uio; 11602 int secmask; 11603 int err; 11604 11605 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11606 return (ENXIO); 11607 } 11608 11609 ASSERT(!mutex_owned(SD_MUTEX(un))); 11610 11611 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11612 mutex_enter(SD_MUTEX(un)); 11613 /* 11614 * Because the call to sd_ready_and_valid will issue I/O we 11615 * must wait here if either the device is suspended or 11616 * if it's power level is changing. 11617 */ 11618 while ((un->un_state == SD_STATE_SUSPENDED) || 11619 (un->un_state == SD_STATE_PM_CHANGING)) { 11620 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11621 } 11622 un->un_ncmds_in_driver++; 11623 mutex_exit(SD_MUTEX(un)); 11624 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11625 mutex_enter(SD_MUTEX(un)); 11626 un->un_ncmds_in_driver--; 11627 ASSERT(un->un_ncmds_in_driver >= 0); 11628 mutex_exit(SD_MUTEX(un)); 11629 return (EIO); 11630 } 11631 mutex_enter(SD_MUTEX(un)); 11632 un->un_ncmds_in_driver--; 11633 ASSERT(un->un_ncmds_in_driver >= 0); 11634 mutex_exit(SD_MUTEX(un)); 11635 } 11636 11637 /* 11638 * Write requests are restricted to multiples of the system block size. 11639 */ 11640 secmask = un->un_sys_blocksize - 1; 11641 11642 if (uio->uio_loffset & ((offset_t)(secmask))) { 11643 SD_ERROR(SD_LOG_READ_WRITE, un, 11644 "sdawrite: file offset not modulo %d\n", 11645 un->un_sys_blocksize); 11646 err = EINVAL; 11647 } else if (uio->uio_iov->iov_len & (secmask)) { 11648 SD_ERROR(SD_LOG_READ_WRITE, un, 11649 "sdawrite: transfer length not modulo %d\n", 11650 un->un_sys_blocksize); 11651 err = EINVAL; 11652 } else { 11653 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11654 } 11655 return (err); 11656 } 11657 11658 11659 11660 11661 11662 /* 11663 * Driver IO processing follows the following sequence: 11664 * 11665 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11666 * | | ^ 11667 * v v | 11668 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11669 * | | | | 11670 * v | | | 11671 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11672 * | | ^ ^ 11673 * v v | | 11674 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11675 * | | | | 11676 * +---+ | +------------+ +-------+ 11677 * | | | | 11678 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11679 * | v | | 11680 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11681 * | | ^ | 11682 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11683 * | v | | 11684 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11685 * | | ^ | 11686 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11687 * | v | | 11688 * | sd_checksum_iostart() sd_checksum_iodone() | 11689 * | | ^ | 11690 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11691 * | v | | 11692 * | sd_pm_iostart() sd_pm_iodone() | 11693 * | | ^ | 11694 * | | | | 11695 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11696 * | ^ 11697 * v | 11698 * sd_core_iostart() | 11699 * | | 11700 * | +------>(*destroypkt)() 11701 * +-> sd_start_cmds() <-+ | | 11702 * | | | v 11703 * | | | scsi_destroy_pkt(9F) 11704 * | | | 11705 * +->(*initpkt)() +- sdintr() 11706 * | | | | 11707 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11708 * | +-> scsi_setup_cdb(9F) | 11709 * | | 11710 * +--> scsi_transport(9F) | 11711 * | | 11712 * +----> SCSA ---->+ 11713 * 11714 * 11715 * This code is based upon the following presumtions: 11716 * 11717 * - iostart and iodone functions operate on buf(9S) structures. These 11718 * functions perform the necessary operations on the buf(9S) and pass 11719 * them along to the next function in the chain by using the macros 11720 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11721 * (for iodone side functions). 11722 * 11723 * - The iostart side functions may sleep. The iodone side functions 11724 * are called under interrupt context and may NOT sleep. Therefore 11725 * iodone side functions also may not call iostart side functions. 11726 * (NOTE: iostart side functions should NOT sleep for memory, as 11727 * this could result in deadlock.) 11728 * 11729 * - An iostart side function may call its corresponding iodone side 11730 * function directly (if necessary). 11731 * 11732 * - In the event of an error, an iostart side function can return a buf(9S) 11733 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11734 * b_error in the usual way of course). 11735 * 11736 * - The taskq mechanism may be used by the iodone side functions to dispatch 11737 * requests to the iostart side functions. The iostart side functions in 11738 * this case would be called under the context of a taskq thread, so it's 11739 * OK for them to block/sleep/spin in this case. 11740 * 11741 * - iostart side functions may allocate "shadow" buf(9S) structs and 11742 * pass them along to the next function in the chain. The corresponding 11743 * iodone side functions must coalesce the "shadow" bufs and return 11744 * the "original" buf to the next higher layer. 11745 * 11746 * - The b_private field of the buf(9S) struct holds a pointer to 11747 * an sd_xbuf struct, which contains information needed to 11748 * construct the scsi_pkt for the command. 11749 * 11750 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11751 * layer must acquire & release the SD_MUTEX(un) as needed. 11752 */ 11753 11754 11755 /* 11756 * Create taskq for all targets in the system. This is created at 11757 * _init(9E) and destroyed at _fini(9E). 11758 * 11759 * Note: here we set the minalloc to a reasonably high number to ensure that 11760 * we will have an adequate supply of task entries available at interrupt time. 11761 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11762 * sd_create_taskq(). Since we do not want to sleep for allocations at 11763 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11764 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11765 * requests any one instant in time. 11766 */ 11767 #define SD_TASKQ_NUMTHREADS 8 11768 #define SD_TASKQ_MINALLOC 256 11769 #define SD_TASKQ_MAXALLOC 256 11770 11771 static taskq_t *sd_tq = NULL; 11772 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11773 11774 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11775 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11776 11777 /* 11778 * The following task queue is being created for the write part of 11779 * read-modify-write of non-512 block size devices. 11780 * Limit the number of threads to 1 for now. This number has been choosen 11781 * considering the fact that it applies only to dvd ram drives/MO drives 11782 * currently. Performance for which is not main criteria at this stage. 11783 * Note: It needs to be explored if we can use a single taskq in future 11784 */ 11785 #define SD_WMR_TASKQ_NUMTHREADS 1 11786 static taskq_t *sd_wmr_tq = NULL; 11787 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11788 11789 /* 11790 * Function: sd_taskq_create 11791 * 11792 * Description: Create taskq thread(s) and preallocate task entries 11793 * 11794 * Return Code: Returns a pointer to the allocated taskq_t. 11795 * 11796 * Context: Can sleep. Requires blockable context. 11797 * 11798 * Notes: - The taskq() facility currently is NOT part of the DDI. 11799 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11800 * - taskq_create() will block for memory, also it will panic 11801 * if it cannot create the requested number of threads. 11802 * - Currently taskq_create() creates threads that cannot be 11803 * swapped. 11804 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11805 * supply of taskq entries at interrupt time (ie, so that we 11806 * do not have to sleep for memory) 11807 */ 11808 11809 static void 11810 sd_taskq_create(void) 11811 { 11812 char taskq_name[TASKQ_NAMELEN]; 11813 11814 ASSERT(sd_tq == NULL); 11815 ASSERT(sd_wmr_tq == NULL); 11816 11817 (void) snprintf(taskq_name, sizeof (taskq_name), 11818 "%s_drv_taskq", sd_label); 11819 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11820 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11821 TASKQ_PREPOPULATE)); 11822 11823 (void) snprintf(taskq_name, sizeof (taskq_name), 11824 "%s_rmw_taskq", sd_label); 11825 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11826 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11827 TASKQ_PREPOPULATE)); 11828 } 11829 11830 11831 /* 11832 * Function: sd_taskq_delete 11833 * 11834 * Description: Complementary cleanup routine for sd_taskq_create(). 11835 * 11836 * Context: Kernel thread context. 11837 */ 11838 11839 static void 11840 sd_taskq_delete(void) 11841 { 11842 ASSERT(sd_tq != NULL); 11843 ASSERT(sd_wmr_tq != NULL); 11844 taskq_destroy(sd_tq); 11845 taskq_destroy(sd_wmr_tq); 11846 sd_tq = NULL; 11847 sd_wmr_tq = NULL; 11848 } 11849 11850 11851 /* 11852 * Function: sdstrategy 11853 * 11854 * Description: Driver's strategy (9E) entry point function. 11855 * 11856 * Arguments: bp - pointer to buf(9S) 11857 * 11858 * Return Code: Always returns zero 11859 * 11860 * Context: Kernel thread context. 11861 */ 11862 11863 static int 11864 sdstrategy(struct buf *bp) 11865 { 11866 struct sd_lun *un; 11867 11868 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11869 if (un == NULL) { 11870 bioerror(bp, EIO); 11871 bp->b_resid = bp->b_bcount; 11872 biodone(bp); 11873 return (0); 11874 } 11875 /* As was done in the past, fail new cmds. if state is dumping. */ 11876 if (un->un_state == SD_STATE_DUMPING) { 11877 bioerror(bp, ENXIO); 11878 bp->b_resid = bp->b_bcount; 11879 biodone(bp); 11880 return (0); 11881 } 11882 11883 ASSERT(!mutex_owned(SD_MUTEX(un))); 11884 11885 /* 11886 * Commands may sneak in while we released the mutex in 11887 * DDI_SUSPEND, we should block new commands. However, old 11888 * commands that are still in the driver at this point should 11889 * still be allowed to drain. 11890 */ 11891 mutex_enter(SD_MUTEX(un)); 11892 /* 11893 * Must wait here if either the device is suspended or 11894 * if it's power level is changing. 11895 */ 11896 while ((un->un_state == SD_STATE_SUSPENDED) || 11897 (un->un_state == SD_STATE_PM_CHANGING)) { 11898 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11899 } 11900 11901 un->un_ncmds_in_driver++; 11902 11903 /* 11904 * atapi: Since we are running the CD for now in PIO mode we need to 11905 * call bp_mapin here to avoid bp_mapin called interrupt context under 11906 * the HBA's init_pkt routine. 11907 */ 11908 if (un->un_f_cfg_is_atapi == TRUE) { 11909 mutex_exit(SD_MUTEX(un)); 11910 bp_mapin(bp); 11911 mutex_enter(SD_MUTEX(un)); 11912 } 11913 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11914 un->un_ncmds_in_driver); 11915 11916 mutex_exit(SD_MUTEX(un)); 11917 11918 /* 11919 * This will (eventually) allocate the sd_xbuf area and 11920 * call sd_xbuf_strategy(). We just want to return the 11921 * result of ddi_xbuf_qstrategy so that we have an opt- 11922 * imized tail call which saves us a stack frame. 11923 */ 11924 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11925 } 11926 11927 11928 /* 11929 * Function: sd_xbuf_strategy 11930 * 11931 * Description: Function for initiating IO operations via the 11932 * ddi_xbuf_qstrategy() mechanism. 11933 * 11934 * Context: Kernel thread context. 11935 */ 11936 11937 static void 11938 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11939 { 11940 struct sd_lun *un = arg; 11941 11942 ASSERT(bp != NULL); 11943 ASSERT(xp != NULL); 11944 ASSERT(un != NULL); 11945 ASSERT(!mutex_owned(SD_MUTEX(un))); 11946 11947 /* 11948 * Initialize the fields in the xbuf and save a pointer to the 11949 * xbuf in bp->b_private. 11950 */ 11951 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11952 11953 /* Send the buf down the iostart chain */ 11954 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11955 } 11956 11957 11958 /* 11959 * Function: sd_xbuf_init 11960 * 11961 * Description: Prepare the given sd_xbuf struct for use. 11962 * 11963 * Arguments: un - ptr to softstate 11964 * bp - ptr to associated buf(9S) 11965 * xp - ptr to associated sd_xbuf 11966 * chain_type - IO chain type to use: 11967 * SD_CHAIN_NULL 11968 * SD_CHAIN_BUFIO 11969 * SD_CHAIN_USCSI 11970 * SD_CHAIN_DIRECT 11971 * SD_CHAIN_DIRECT_PRIORITY 11972 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11973 * initialization; may be NULL if none. 11974 * 11975 * Context: Kernel thread context 11976 */ 11977 11978 static void 11979 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11980 uchar_t chain_type, void *pktinfop) 11981 { 11982 int index; 11983 11984 ASSERT(un != NULL); 11985 ASSERT(bp != NULL); 11986 ASSERT(xp != NULL); 11987 11988 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 11989 bp, chain_type); 11990 11991 xp->xb_un = un; 11992 xp->xb_pktp = NULL; 11993 xp->xb_pktinfo = pktinfop; 11994 xp->xb_private = bp->b_private; 11995 xp->xb_blkno = (daddr_t)bp->b_blkno; 11996 11997 /* 11998 * Set up the iostart and iodone chain indexes in the xbuf, based 11999 * upon the specified chain type to use. 12000 */ 12001 switch (chain_type) { 12002 case SD_CHAIN_NULL: 12003 /* 12004 * Fall thru to just use the values for the buf type, even 12005 * tho for the NULL chain these values will never be used. 12006 */ 12007 /* FALLTHRU */ 12008 case SD_CHAIN_BUFIO: 12009 index = un->un_buf_chain_type; 12010 break; 12011 case SD_CHAIN_USCSI: 12012 index = un->un_uscsi_chain_type; 12013 break; 12014 case SD_CHAIN_DIRECT: 12015 index = un->un_direct_chain_type; 12016 break; 12017 case SD_CHAIN_DIRECT_PRIORITY: 12018 index = un->un_priority_chain_type; 12019 break; 12020 default: 12021 /* We're really broken if we ever get here... */ 12022 panic("sd_xbuf_init: illegal chain type!"); 12023 /*NOTREACHED*/ 12024 } 12025 12026 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 12027 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 12028 12029 /* 12030 * It might be a bit easier to simply bzero the entire xbuf above, 12031 * but it turns out that since we init a fair number of members anyway, 12032 * we save a fair number cycles by doing explicit assignment of zero. 12033 */ 12034 xp->xb_pkt_flags = 0; 12035 xp->xb_dma_resid = 0; 12036 xp->xb_retry_count = 0; 12037 xp->xb_victim_retry_count = 0; 12038 xp->xb_ua_retry_count = 0; 12039 xp->xb_sense_bp = NULL; 12040 xp->xb_sense_status = 0; 12041 xp->xb_sense_state = 0; 12042 xp->xb_sense_resid = 0; 12043 12044 bp->b_private = xp; 12045 bp->b_flags &= ~(B_DONE | B_ERROR); 12046 bp->b_resid = 0; 12047 bp->av_forw = NULL; 12048 bp->av_back = NULL; 12049 bioerror(bp, 0); 12050 12051 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 12052 } 12053 12054 12055 /* 12056 * Function: sd_uscsi_strategy 12057 * 12058 * Description: Wrapper for calling into the USCSI chain via physio(9F) 12059 * 12060 * Arguments: bp - buf struct ptr 12061 * 12062 * Return Code: Always returns 0 12063 * 12064 * Context: Kernel thread context 12065 */ 12066 12067 static int 12068 sd_uscsi_strategy(struct buf *bp) 12069 { 12070 struct sd_lun *un; 12071 struct sd_uscsi_info *uip; 12072 struct sd_xbuf *xp; 12073 uchar_t chain_type; 12074 12075 ASSERT(bp != NULL); 12076 12077 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 12078 if (un == NULL) { 12079 bioerror(bp, EIO); 12080 bp->b_resid = bp->b_bcount; 12081 biodone(bp); 12082 return (0); 12083 } 12084 12085 ASSERT(!mutex_owned(SD_MUTEX(un))); 12086 12087 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 12088 12089 mutex_enter(SD_MUTEX(un)); 12090 /* 12091 * atapi: Since we are running the CD for now in PIO mode we need to 12092 * call bp_mapin here to avoid bp_mapin called interrupt context under 12093 * the HBA's init_pkt routine. 12094 */ 12095 if (un->un_f_cfg_is_atapi == TRUE) { 12096 mutex_exit(SD_MUTEX(un)); 12097 bp_mapin(bp); 12098 mutex_enter(SD_MUTEX(un)); 12099 } 12100 un->un_ncmds_in_driver++; 12101 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 12102 un->un_ncmds_in_driver); 12103 mutex_exit(SD_MUTEX(un)); 12104 12105 /* 12106 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 12107 */ 12108 ASSERT(bp->b_private != NULL); 12109 uip = (struct sd_uscsi_info *)bp->b_private; 12110 12111 switch (uip->ui_flags) { 12112 case SD_PATH_DIRECT: 12113 chain_type = SD_CHAIN_DIRECT; 12114 break; 12115 case SD_PATH_DIRECT_PRIORITY: 12116 chain_type = SD_CHAIN_DIRECT_PRIORITY; 12117 break; 12118 default: 12119 chain_type = SD_CHAIN_USCSI; 12120 break; 12121 } 12122 12123 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12124 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 12125 12126 /* Use the index obtained within xbuf_init */ 12127 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 12128 12129 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 12130 12131 return (0); 12132 } 12133 12134 12135 /* 12136 * These routines perform raw i/o operations. 12137 */ 12138 /*ARGSUSED*/ 12139 static void 12140 sduscsimin(struct buf *bp) 12141 { 12142 /* 12143 * do not break up because the CDB count would then 12144 * be incorrect and data underruns would result (incomplete 12145 * read/writes which would be retried and then failed, see 12146 * sdintr(). 12147 */ 12148 } 12149 12150 12151 12152 /* 12153 * Function: sd_send_scsi_cmd 12154 * 12155 * Description: Runs a USCSI command for user (when called thru sdioctl), 12156 * or for the driver 12157 * 12158 * Arguments: dev - the dev_t for the device 12159 * incmd - ptr to a valid uscsi_cmd struct 12160 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 12161 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 12162 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 12163 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 12164 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 12165 * to use the USCSI "direct" chain and bypass the normal 12166 * command waitq. 12167 * 12168 * Return Code: 0 - successful completion of the given command 12169 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 12170 * ENXIO - soft state not found for specified dev 12171 * EINVAL 12172 * EFAULT - copyin/copyout error 12173 * return code of biowait(9F) or physio(9F): 12174 * EIO - IO error, caller may check incmd->uscsi_status 12175 * ENXIO 12176 * EACCES - reservation conflict 12177 * 12178 * Context: Waits for command to complete. Can sleep. 12179 */ 12180 12181 static int 12182 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 12183 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 12184 int path_flag) 12185 { 12186 struct sd_uscsi_info *uip; 12187 struct uscsi_cmd *uscmd; 12188 struct sd_lun *un; 12189 struct buf *bp; 12190 int rval; 12191 int flags; 12192 12193 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 12194 if (un == NULL) { 12195 return (ENXIO); 12196 } 12197 12198 ASSERT(!mutex_owned(SD_MUTEX(un))); 12199 12200 #ifdef SDDEBUG 12201 switch (dataspace) { 12202 case UIO_USERSPACE: 12203 SD_TRACE(SD_LOG_IO, un, 12204 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 12205 break; 12206 case UIO_SYSSPACE: 12207 SD_TRACE(SD_LOG_IO, un, 12208 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 12209 break; 12210 default: 12211 SD_TRACE(SD_LOG_IO, un, 12212 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 12213 break; 12214 } 12215 #endif 12216 12217 /* 12218 * Perform resets directly; no need to generate a command to do it. 12219 */ 12220 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 12221 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 12222 RESET_ALL : RESET_TARGET; 12223 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 12224 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 12225 /* Reset attempt was unsuccessful */ 12226 SD_TRACE(SD_LOG_IO, un, 12227 "sd_send_scsi_cmd: reset: failure\n"); 12228 return (EIO); 12229 } 12230 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 12231 return (0); 12232 } 12233 12234 /* Perfunctory sanity check... */ 12235 if (incmd->uscsi_cdblen <= 0) { 12236 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12237 "invalid uscsi_cdblen, returning EINVAL\n"); 12238 return (EINVAL); 12239 } else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) { 12240 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12241 "unsupported uscsi_cdblen, returning EINVAL\n"); 12242 return (EINVAL); 12243 } 12244 12245 /* 12246 * In order to not worry about where the uscsi structure came from 12247 * (or where the cdb it points to came from) we're going to make 12248 * kmem_alloc'd copies of them here. This will also allow reference 12249 * to the data they contain long after this process has gone to 12250 * sleep and its kernel stack has been unmapped, etc. 12251 * 12252 * First get some memory for the uscsi_cmd struct and copy the 12253 * contents of the given uscsi_cmd struct into it. 12254 */ 12255 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12256 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 12257 12258 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 12259 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 12260 12261 /* 12262 * Now get some space for the CDB, and copy the given CDB into 12263 * it. Use ddi_copyin() in case the data is in user space. 12264 */ 12265 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 12266 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 12267 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 12268 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 12269 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 12270 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12271 return (EFAULT); 12272 } 12273 12274 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 12275 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 12276 12277 bp = getrbuf(KM_SLEEP); 12278 12279 /* 12280 * Allocate an sd_uscsi_info struct and fill it with the info 12281 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12282 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12283 * since we allocate the buf here in this function, we do not 12284 * need to preserve the prior contents of b_private. 12285 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12286 */ 12287 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 12288 uip->ui_flags = path_flag; 12289 uip->ui_cmdp = uscmd; 12290 bp->b_private = uip; 12291 12292 /* 12293 * Initialize Request Sense buffering, if requested. 12294 */ 12295 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12296 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12297 /* 12298 * Here uscmd->uscsi_rqbuf currently points to the caller's 12299 * buffer, but we replace this with a kernel buffer that 12300 * we allocate to use with the sense data. The sense data 12301 * (if present) gets copied into this new buffer before the 12302 * command is completed. Then we copy the sense data from 12303 * our allocated buf into the caller's buffer below. Note 12304 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 12305 * below to perform the copy back to the caller's buf. 12306 */ 12307 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 12308 if (rqbufspace == UIO_USERSPACE) { 12309 uscmd->uscsi_rqlen = SENSE_LENGTH; 12310 uscmd->uscsi_rqresid = SENSE_LENGTH; 12311 } else { 12312 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 12313 uscmd->uscsi_rqlen = rlen; 12314 uscmd->uscsi_rqresid = rlen; 12315 } 12316 } else { 12317 uscmd->uscsi_rqbuf = NULL; 12318 uscmd->uscsi_rqlen = 0; 12319 uscmd->uscsi_rqresid = 0; 12320 } 12321 12322 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 12323 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 12324 12325 if (un->un_f_is_fibre == FALSE) { 12326 /* 12327 * Force asynchronous mode, if necessary. Doing this here 12328 * has the unfortunate effect of running other queued 12329 * commands async also, but since the main purpose of this 12330 * capability is downloading new drive firmware, we can 12331 * probably live with it. 12332 */ 12333 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 12334 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12335 == 1) { 12336 if (scsi_ifsetcap(SD_ADDRESS(un), 12337 "synchronous", 0, 1) == 1) { 12338 SD_TRACE(SD_LOG_IO, un, 12339 "sd_send_scsi_cmd: forced async ok\n"); 12340 } else { 12341 SD_TRACE(SD_LOG_IO, un, 12342 "sd_send_scsi_cmd:\ 12343 forced async failed\n"); 12344 rval = EINVAL; 12345 goto done; 12346 } 12347 } 12348 } 12349 12350 /* 12351 * Re-enable synchronous mode, if requested 12352 */ 12353 if (uscmd->uscsi_flags & USCSI_SYNC) { 12354 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12355 == 0) { 12356 int i = scsi_ifsetcap(SD_ADDRESS(un), 12357 "synchronous", 1, 1); 12358 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12359 "re-enabled sync %s\n", 12360 (i == 1) ? "ok" : "failed"); 12361 } 12362 } 12363 } 12364 12365 /* 12366 * Commands sent with priority are intended for error recovery 12367 * situations, and do not have retries performed. 12368 */ 12369 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12370 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12371 } 12372 12373 /* 12374 * If we're going to do actual I/O, let physio do all the right things 12375 */ 12376 if (uscmd->uscsi_buflen != 0) { 12377 struct iovec aiov; 12378 struct uio auio; 12379 struct uio *uio = &auio; 12380 12381 bzero(&auio, sizeof (struct uio)); 12382 bzero(&aiov, sizeof (struct iovec)); 12383 aiov.iov_base = uscmd->uscsi_bufaddr; 12384 aiov.iov_len = uscmd->uscsi_buflen; 12385 uio->uio_iov = &aiov; 12386 12387 uio->uio_iovcnt = 1; 12388 uio->uio_resid = uscmd->uscsi_buflen; 12389 uio->uio_segflg = dataspace; 12390 12391 /* 12392 * physio() will block here until the command completes.... 12393 */ 12394 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 12395 12396 rval = physio(sd_uscsi_strategy, bp, dev, 12397 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 12398 sduscsimin, uio); 12399 12400 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12401 "returned from physio with 0x%x\n", rval); 12402 12403 } else { 12404 /* 12405 * We have to mimic what physio would do here! Argh! 12406 */ 12407 bp->b_flags = B_BUSY | 12408 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 12409 bp->b_edev = dev; 12410 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 12411 bp->b_bcount = 0; 12412 bp->b_blkno = 0; 12413 12414 SD_TRACE(SD_LOG_IO, un, 12415 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 12416 12417 (void) sd_uscsi_strategy(bp); 12418 12419 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 12420 12421 rval = biowait(bp); 12422 12423 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12424 "returned from biowait with 0x%x\n", rval); 12425 } 12426 12427 done: 12428 12429 #ifdef SDDEBUG 12430 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12431 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12432 uscmd->uscsi_status, uscmd->uscsi_resid); 12433 if (uscmd->uscsi_bufaddr != NULL) { 12434 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12435 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12436 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12437 if (dataspace == UIO_SYSSPACE) { 12438 SD_DUMP_MEMORY(un, SD_LOG_IO, 12439 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12440 uscmd->uscsi_buflen, SD_LOG_HEX); 12441 } 12442 } 12443 #endif 12444 12445 /* 12446 * Get the status and residual to return to the caller. 12447 */ 12448 incmd->uscsi_status = uscmd->uscsi_status; 12449 incmd->uscsi_resid = uscmd->uscsi_resid; 12450 12451 /* 12452 * If the caller wants sense data, copy back whatever sense data 12453 * we may have gotten, and update the relevant rqsense info. 12454 */ 12455 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12456 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12457 12458 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 12459 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 12460 12461 /* Update the Request Sense status and resid */ 12462 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 12463 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 12464 12465 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12466 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 12467 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 12468 12469 /* Copy out the sense data for user processes */ 12470 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 12471 int flags = 12472 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 12473 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 12474 rqlen, flags) != 0) { 12475 rval = EFAULT; 12476 } 12477 /* 12478 * Note: Can't touch incmd->uscsi_rqbuf so use 12479 * uscmd->uscsi_rqbuf instead. They're the same. 12480 */ 12481 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12482 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 12483 incmd->uscsi_rqbuf, rqlen); 12484 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 12485 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 12486 } 12487 } 12488 12489 /* 12490 * Free allocated resources and return; mapout the buf in case it was 12491 * mapped in by a lower layer. 12492 */ 12493 bp_mapout(bp); 12494 freerbuf(bp); 12495 kmem_free(uip, sizeof (struct sd_uscsi_info)); 12496 if (uscmd->uscsi_rqbuf != NULL) { 12497 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 12498 } 12499 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 12500 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12501 12502 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 12503 12504 return (rval); 12505 } 12506 12507 12508 /* 12509 * Function: sd_buf_iodone 12510 * 12511 * Description: Frees the sd_xbuf & returns the buf to its originator. 12512 * 12513 * Context: May be called from interrupt context. 12514 */ 12515 /* ARGSUSED */ 12516 static void 12517 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12518 { 12519 struct sd_xbuf *xp; 12520 12521 ASSERT(un != NULL); 12522 ASSERT(bp != NULL); 12523 ASSERT(!mutex_owned(SD_MUTEX(un))); 12524 12525 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12526 12527 xp = SD_GET_XBUF(bp); 12528 ASSERT(xp != NULL); 12529 12530 mutex_enter(SD_MUTEX(un)); 12531 12532 /* 12533 * Grab time when the cmd completed. 12534 * This is used for determining if the system has been 12535 * idle long enough to make it idle to the PM framework. 12536 * This is for lowering the overhead, and therefore improving 12537 * performance per I/O operation. 12538 */ 12539 un->un_pm_idle_time = ddi_get_time(); 12540 12541 un->un_ncmds_in_driver--; 12542 ASSERT(un->un_ncmds_in_driver >= 0); 12543 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12544 un->un_ncmds_in_driver); 12545 12546 mutex_exit(SD_MUTEX(un)); 12547 12548 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12549 biodone(bp); /* bp is gone after this */ 12550 12551 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12552 } 12553 12554 12555 /* 12556 * Function: sd_uscsi_iodone 12557 * 12558 * Description: Frees the sd_xbuf & returns the buf to its originator. 12559 * 12560 * Context: May be called from interrupt context. 12561 */ 12562 /* ARGSUSED */ 12563 static void 12564 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12565 { 12566 struct sd_xbuf *xp; 12567 12568 ASSERT(un != NULL); 12569 ASSERT(bp != NULL); 12570 12571 xp = SD_GET_XBUF(bp); 12572 ASSERT(xp != NULL); 12573 ASSERT(!mutex_owned(SD_MUTEX(un))); 12574 12575 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12576 12577 bp->b_private = xp->xb_private; 12578 12579 mutex_enter(SD_MUTEX(un)); 12580 12581 /* 12582 * Grab time when the cmd completed. 12583 * This is used for determining if the system has been 12584 * idle long enough to make it idle to the PM framework. 12585 * This is for lowering the overhead, and therefore improving 12586 * performance per I/O operation. 12587 */ 12588 un->un_pm_idle_time = ddi_get_time(); 12589 12590 un->un_ncmds_in_driver--; 12591 ASSERT(un->un_ncmds_in_driver >= 0); 12592 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12593 un->un_ncmds_in_driver); 12594 12595 mutex_exit(SD_MUTEX(un)); 12596 12597 kmem_free(xp, sizeof (struct sd_xbuf)); 12598 biodone(bp); 12599 12600 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12601 } 12602 12603 12604 /* 12605 * Function: sd_mapblockaddr_iostart 12606 * 12607 * Description: Verify request lies withing the partition limits for 12608 * the indicated minor device. Issue "overrun" buf if 12609 * request would exceed partition range. Converts 12610 * partition-relative block address to absolute. 12611 * 12612 * Context: Can sleep 12613 * 12614 * Issues: This follows what the old code did, in terms of accessing 12615 * some of the partition info in the unit struct without holding 12616 * the mutext. This is a general issue, if the partition info 12617 * can be altered while IO is in progress... as soon as we send 12618 * a buf, its partitioning can be invalid before it gets to the 12619 * device. Probably the right fix is to move partitioning out 12620 * of the driver entirely. 12621 */ 12622 12623 static void 12624 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12625 { 12626 daddr_t nblocks; /* #blocks in the given partition */ 12627 daddr_t blocknum; /* Block number specified by the buf */ 12628 size_t requested_nblocks; 12629 size_t available_nblocks; 12630 int partition; 12631 diskaddr_t partition_offset; 12632 struct sd_xbuf *xp; 12633 12634 12635 ASSERT(un != NULL); 12636 ASSERT(bp != NULL); 12637 ASSERT(!mutex_owned(SD_MUTEX(un))); 12638 12639 SD_TRACE(SD_LOG_IO_PARTITION, un, 12640 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12641 12642 xp = SD_GET_XBUF(bp); 12643 ASSERT(xp != NULL); 12644 12645 /* 12646 * If the geometry is not indicated as valid, attempt to access 12647 * the unit & verify the geometry/label. This can be the case for 12648 * removable-media devices, of if the device was opened in 12649 * NDELAY/NONBLOCK mode. 12650 */ 12651 if ((un->un_f_geometry_is_valid != TRUE) && 12652 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12653 /* 12654 * For removable devices it is possible to start an I/O 12655 * without a media by opening the device in nodelay mode. 12656 * Also for writable CDs there can be many scenarios where 12657 * there is no geometry yet but volume manager is trying to 12658 * issue a read() just because it can see TOC on the CD. So 12659 * do not print a message for removables. 12660 */ 12661 if (!un->un_f_has_removable_media) { 12662 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12663 "i/o to invalid geometry\n"); 12664 } 12665 bioerror(bp, EIO); 12666 bp->b_resid = bp->b_bcount; 12667 SD_BEGIN_IODONE(index, un, bp); 12668 return; 12669 } 12670 12671 partition = SDPART(bp->b_edev); 12672 12673 /* #blocks in partition */ 12674 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12675 12676 /* Use of a local variable potentially improves performance slightly */ 12677 partition_offset = un->un_offset[partition]; 12678 12679 /* 12680 * blocknum is the starting block number of the request. At this 12681 * point it is still relative to the start of the minor device. 12682 */ 12683 blocknum = xp->xb_blkno; 12684 12685 /* 12686 * Legacy: If the starting block number is one past the last block 12687 * in the partition, do not set B_ERROR in the buf. 12688 */ 12689 if (blocknum == nblocks) { 12690 goto error_exit; 12691 } 12692 12693 /* 12694 * Confirm that the first block of the request lies within the 12695 * partition limits. Also the requested number of bytes must be 12696 * a multiple of the system block size. 12697 */ 12698 if ((blocknum < 0) || (blocknum >= nblocks) || 12699 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12700 bp->b_flags |= B_ERROR; 12701 goto error_exit; 12702 } 12703 12704 /* 12705 * If the requsted # blocks exceeds the available # blocks, that 12706 * is an overrun of the partition. 12707 */ 12708 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12709 available_nblocks = (size_t)(nblocks - blocknum); 12710 ASSERT(nblocks >= blocknum); 12711 12712 if (requested_nblocks > available_nblocks) { 12713 /* 12714 * Allocate an "overrun" buf to allow the request to proceed 12715 * for the amount of space available in the partition. The 12716 * amount not transferred will be added into the b_resid 12717 * when the operation is complete. The overrun buf 12718 * replaces the original buf here, and the original buf 12719 * is saved inside the overrun buf, for later use. 12720 */ 12721 size_t resid = SD_SYSBLOCKS2BYTES(un, 12722 (offset_t)(requested_nblocks - available_nblocks)); 12723 size_t count = bp->b_bcount - resid; 12724 /* 12725 * Note: count is an unsigned entity thus it'll NEVER 12726 * be less than 0 so ASSERT the original values are 12727 * correct. 12728 */ 12729 ASSERT(bp->b_bcount >= resid); 12730 12731 bp = sd_bioclone_alloc(bp, count, blocknum, 12732 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12733 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12734 ASSERT(xp != NULL); 12735 } 12736 12737 /* At this point there should be no residual for this buf. */ 12738 ASSERT(bp->b_resid == 0); 12739 12740 /* Convert the block number to an absolute address. */ 12741 xp->xb_blkno += partition_offset; 12742 12743 SD_NEXT_IOSTART(index, un, bp); 12744 12745 SD_TRACE(SD_LOG_IO_PARTITION, un, 12746 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12747 12748 return; 12749 12750 error_exit: 12751 bp->b_resid = bp->b_bcount; 12752 SD_BEGIN_IODONE(index, un, bp); 12753 SD_TRACE(SD_LOG_IO_PARTITION, un, 12754 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12755 } 12756 12757 12758 /* 12759 * Function: sd_mapblockaddr_iodone 12760 * 12761 * Description: Completion-side processing for partition management. 12762 * 12763 * Context: May be called under interrupt context 12764 */ 12765 12766 static void 12767 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12768 { 12769 /* int partition; */ /* Not used, see below. */ 12770 ASSERT(un != NULL); 12771 ASSERT(bp != NULL); 12772 ASSERT(!mutex_owned(SD_MUTEX(un))); 12773 12774 SD_TRACE(SD_LOG_IO_PARTITION, un, 12775 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12776 12777 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12778 /* 12779 * We have an "overrun" buf to deal with... 12780 */ 12781 struct sd_xbuf *xp; 12782 struct buf *obp; /* ptr to the original buf */ 12783 12784 xp = SD_GET_XBUF(bp); 12785 ASSERT(xp != NULL); 12786 12787 /* Retrieve the pointer to the original buf */ 12788 obp = (struct buf *)xp->xb_private; 12789 ASSERT(obp != NULL); 12790 12791 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12792 bioerror(obp, bp->b_error); 12793 12794 sd_bioclone_free(bp); 12795 12796 /* 12797 * Get back the original buf. 12798 * Note that since the restoration of xb_blkno below 12799 * was removed, the sd_xbuf is not needed. 12800 */ 12801 bp = obp; 12802 /* 12803 * xp = SD_GET_XBUF(bp); 12804 * ASSERT(xp != NULL); 12805 */ 12806 } 12807 12808 /* 12809 * Convert sd->xb_blkno back to a minor-device relative value. 12810 * Note: this has been commented out, as it is not needed in the 12811 * current implementation of the driver (ie, since this function 12812 * is at the top of the layering chains, so the info will be 12813 * discarded) and it is in the "hot" IO path. 12814 * 12815 * partition = getminor(bp->b_edev) & SDPART_MASK; 12816 * xp->xb_blkno -= un->un_offset[partition]; 12817 */ 12818 12819 SD_NEXT_IODONE(index, un, bp); 12820 12821 SD_TRACE(SD_LOG_IO_PARTITION, un, 12822 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12823 } 12824 12825 12826 /* 12827 * Function: sd_mapblocksize_iostart 12828 * 12829 * Description: Convert between system block size (un->un_sys_blocksize) 12830 * and target block size (un->un_tgt_blocksize). 12831 * 12832 * Context: Can sleep to allocate resources. 12833 * 12834 * Assumptions: A higher layer has already performed any partition validation, 12835 * and converted the xp->xb_blkno to an absolute value relative 12836 * to the start of the device. 12837 * 12838 * It is also assumed that the higher layer has implemented 12839 * an "overrun" mechanism for the case where the request would 12840 * read/write beyond the end of a partition. In this case we 12841 * assume (and ASSERT) that bp->b_resid == 0. 12842 * 12843 * Note: The implementation for this routine assumes the target 12844 * block size remains constant between allocation and transport. 12845 */ 12846 12847 static void 12848 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12849 { 12850 struct sd_mapblocksize_info *bsp; 12851 struct sd_xbuf *xp; 12852 offset_t first_byte; 12853 daddr_t start_block, end_block; 12854 daddr_t request_bytes; 12855 ushort_t is_aligned = FALSE; 12856 12857 ASSERT(un != NULL); 12858 ASSERT(bp != NULL); 12859 ASSERT(!mutex_owned(SD_MUTEX(un))); 12860 ASSERT(bp->b_resid == 0); 12861 12862 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12863 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12864 12865 /* 12866 * For a non-writable CD, a write request is an error 12867 */ 12868 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12869 (un->un_f_mmc_writable_media == FALSE)) { 12870 bioerror(bp, EIO); 12871 bp->b_resid = bp->b_bcount; 12872 SD_BEGIN_IODONE(index, un, bp); 12873 return; 12874 } 12875 12876 /* 12877 * We do not need a shadow buf if the device is using 12878 * un->un_sys_blocksize as its block size or if bcount == 0. 12879 * In this case there is no layer-private data block allocated. 12880 */ 12881 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12882 (bp->b_bcount == 0)) { 12883 goto done; 12884 } 12885 12886 #if defined(__i386) || defined(__amd64) 12887 /* We do not support non-block-aligned transfers for ROD devices */ 12888 ASSERT(!ISROD(un)); 12889 #endif 12890 12891 xp = SD_GET_XBUF(bp); 12892 ASSERT(xp != NULL); 12893 12894 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12895 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12896 un->un_tgt_blocksize, un->un_sys_blocksize); 12897 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12898 "request start block:0x%x\n", xp->xb_blkno); 12899 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12900 "request len:0x%x\n", bp->b_bcount); 12901 12902 /* 12903 * Allocate the layer-private data area for the mapblocksize layer. 12904 * Layers are allowed to use the xp_private member of the sd_xbuf 12905 * struct to store the pointer to their layer-private data block, but 12906 * each layer also has the responsibility of restoring the prior 12907 * contents of xb_private before returning the buf/xbuf to the 12908 * higher layer that sent it. 12909 * 12910 * Here we save the prior contents of xp->xb_private into the 12911 * bsp->mbs_oprivate field of our layer-private data area. This value 12912 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12913 * the layer-private area and returning the buf/xbuf to the layer 12914 * that sent it. 12915 * 12916 * Note that here we use kmem_zalloc for the allocation as there are 12917 * parts of the mapblocksize code that expect certain fields to be 12918 * zero unless explicitly set to a required value. 12919 */ 12920 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12921 bsp->mbs_oprivate = xp->xb_private; 12922 xp->xb_private = bsp; 12923 12924 /* 12925 * This treats the data on the disk (target) as an array of bytes. 12926 * first_byte is the byte offset, from the beginning of the device, 12927 * to the location of the request. This is converted from a 12928 * un->un_sys_blocksize block address to a byte offset, and then back 12929 * to a block address based upon a un->un_tgt_blocksize block size. 12930 * 12931 * xp->xb_blkno should be absolute upon entry into this function, 12932 * but, but it is based upon partitions that use the "system" 12933 * block size. It must be adjusted to reflect the block size of 12934 * the target. 12935 * 12936 * Note that end_block is actually the block that follows the last 12937 * block of the request, but that's what is needed for the computation. 12938 */ 12939 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12940 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12941 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12942 un->un_tgt_blocksize; 12943 12944 /* request_bytes is rounded up to a multiple of the target block size */ 12945 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12946 12947 /* 12948 * See if the starting address of the request and the request 12949 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12950 * then we do not need to allocate a shadow buf to handle the request. 12951 */ 12952 if (((first_byte % un->un_tgt_blocksize) == 0) && 12953 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12954 is_aligned = TRUE; 12955 } 12956 12957 if ((bp->b_flags & B_READ) == 0) { 12958 /* 12959 * Lock the range for a write operation. An aligned request is 12960 * considered a simple write; otherwise the request must be a 12961 * read-modify-write. 12962 */ 12963 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12964 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12965 } 12966 12967 /* 12968 * Alloc a shadow buf if the request is not aligned. Also, this is 12969 * where the READ command is generated for a read-modify-write. (The 12970 * write phase is deferred until after the read completes.) 12971 */ 12972 if (is_aligned == FALSE) { 12973 12974 struct sd_mapblocksize_info *shadow_bsp; 12975 struct sd_xbuf *shadow_xp; 12976 struct buf *shadow_bp; 12977 12978 /* 12979 * Allocate the shadow buf and it associated xbuf. Note that 12980 * after this call the xb_blkno value in both the original 12981 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12982 * same: absolute relative to the start of the device, and 12983 * adjusted for the target block size. The b_blkno in the 12984 * shadow buf will also be set to this value. We should never 12985 * change b_blkno in the original bp however. 12986 * 12987 * Note also that the shadow buf will always need to be a 12988 * READ command, regardless of whether the incoming command 12989 * is a READ or a WRITE. 12990 */ 12991 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 12992 xp->xb_blkno, 12993 (int (*)(struct buf *)) sd_mapblocksize_iodone); 12994 12995 shadow_xp = SD_GET_XBUF(shadow_bp); 12996 12997 /* 12998 * Allocate the layer-private data for the shadow buf. 12999 * (No need to preserve xb_private in the shadow xbuf.) 13000 */ 13001 shadow_xp->xb_private = shadow_bsp = 13002 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 13003 13004 /* 13005 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 13006 * to figure out where the start of the user data is (based upon 13007 * the system block size) in the data returned by the READ 13008 * command (which will be based upon the target blocksize). Note 13009 * that this is only really used if the request is unaligned. 13010 */ 13011 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13012 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13013 ASSERT((bsp->mbs_copy_offset >= 0) && 13014 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13015 13016 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13017 13018 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13019 13020 /* Transfer the wmap (if any) to the shadow buf */ 13021 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13022 bsp->mbs_wmp = NULL; 13023 13024 /* 13025 * The shadow buf goes on from here in place of the 13026 * original buf. 13027 */ 13028 shadow_bsp->mbs_orig_bp = bp; 13029 bp = shadow_bp; 13030 } 13031 13032 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13033 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13034 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13035 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13036 request_bytes); 13037 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13038 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13039 13040 done: 13041 SD_NEXT_IOSTART(index, un, bp); 13042 13043 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13044 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13045 } 13046 13047 13048 /* 13049 * Function: sd_mapblocksize_iodone 13050 * 13051 * Description: Completion side processing for block-size mapping. 13052 * 13053 * Context: May be called under interrupt context 13054 */ 13055 13056 static void 13057 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13058 { 13059 struct sd_mapblocksize_info *bsp; 13060 struct sd_xbuf *xp; 13061 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13062 struct buf *orig_bp; /* ptr to the original buf */ 13063 offset_t shadow_end; 13064 offset_t request_end; 13065 offset_t shadow_start; 13066 ssize_t copy_offset; 13067 size_t copy_length; 13068 size_t shortfall; 13069 uint_t is_write; /* TRUE if this bp is a WRITE */ 13070 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13071 13072 ASSERT(un != NULL); 13073 ASSERT(bp != NULL); 13074 13075 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13076 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13077 13078 /* 13079 * There is no shadow buf or layer-private data if the target is 13080 * using un->un_sys_blocksize as its block size or if bcount == 0. 13081 */ 13082 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 13083 (bp->b_bcount == 0)) { 13084 goto exit; 13085 } 13086 13087 xp = SD_GET_XBUF(bp); 13088 ASSERT(xp != NULL); 13089 13090 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13091 bsp = xp->xb_private; 13092 13093 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13094 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13095 13096 if (is_write) { 13097 /* 13098 * For a WRITE request we must free up the block range that 13099 * we have locked up. This holds regardless of whether this is 13100 * an aligned write request or a read-modify-write request. 13101 */ 13102 sd_range_unlock(un, bsp->mbs_wmp); 13103 bsp->mbs_wmp = NULL; 13104 } 13105 13106 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13107 /* 13108 * An aligned read or write command will have no shadow buf; 13109 * there is not much else to do with it. 13110 */ 13111 goto done; 13112 } 13113 13114 orig_bp = bsp->mbs_orig_bp; 13115 ASSERT(orig_bp != NULL); 13116 orig_xp = SD_GET_XBUF(orig_bp); 13117 ASSERT(orig_xp != NULL); 13118 ASSERT(!mutex_owned(SD_MUTEX(un))); 13119 13120 if (!is_write && has_wmap) { 13121 /* 13122 * A READ with a wmap means this is the READ phase of a 13123 * read-modify-write. If an error occurred on the READ then 13124 * we do not proceed with the WRITE phase or copy any data. 13125 * Just release the write maps and return with an error. 13126 */ 13127 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13128 orig_bp->b_resid = orig_bp->b_bcount; 13129 bioerror(orig_bp, bp->b_error); 13130 sd_range_unlock(un, bsp->mbs_wmp); 13131 goto freebuf_done; 13132 } 13133 } 13134 13135 /* 13136 * Here is where we set up to copy the data from the shadow buf 13137 * into the space associated with the original buf. 13138 * 13139 * To deal with the conversion between block sizes, these 13140 * computations treat the data as an array of bytes, with the 13141 * first byte (byte 0) corresponding to the first byte in the 13142 * first block on the disk. 13143 */ 13144 13145 /* 13146 * shadow_start and shadow_len indicate the location and size of 13147 * the data returned with the shadow IO request. 13148 */ 13149 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13150 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13151 13152 /* 13153 * copy_offset gives the offset (in bytes) from the start of the first 13154 * block of the READ request to the beginning of the data. We retrieve 13155 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13156 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13157 * data to be copied (in bytes). 13158 */ 13159 copy_offset = bsp->mbs_copy_offset; 13160 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 13161 copy_length = orig_bp->b_bcount; 13162 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13163 13164 /* 13165 * Set up the resid and error fields of orig_bp as appropriate. 13166 */ 13167 if (shadow_end >= request_end) { 13168 /* We got all the requested data; set resid to zero */ 13169 orig_bp->b_resid = 0; 13170 } else { 13171 /* 13172 * We failed to get enough data to fully satisfy the original 13173 * request. Just copy back whatever data we got and set 13174 * up the residual and error code as required. 13175 * 13176 * 'shortfall' is the amount by which the data received with the 13177 * shadow buf has "fallen short" of the requested amount. 13178 */ 13179 shortfall = (size_t)(request_end - shadow_end); 13180 13181 if (shortfall > orig_bp->b_bcount) { 13182 /* 13183 * We did not get enough data to even partially 13184 * fulfill the original request. The residual is 13185 * equal to the amount requested. 13186 */ 13187 orig_bp->b_resid = orig_bp->b_bcount; 13188 } else { 13189 /* 13190 * We did not get all the data that we requested 13191 * from the device, but we will try to return what 13192 * portion we did get. 13193 */ 13194 orig_bp->b_resid = shortfall; 13195 } 13196 ASSERT(copy_length >= orig_bp->b_resid); 13197 copy_length -= orig_bp->b_resid; 13198 } 13199 13200 /* Propagate the error code from the shadow buf to the original buf */ 13201 bioerror(orig_bp, bp->b_error); 13202 13203 if (is_write) { 13204 goto freebuf_done; /* No data copying for a WRITE */ 13205 } 13206 13207 if (has_wmap) { 13208 /* 13209 * This is a READ command from the READ phase of a 13210 * read-modify-write request. We have to copy the data given 13211 * by the user OVER the data returned by the READ command, 13212 * then convert the command from a READ to a WRITE and send 13213 * it back to the target. 13214 */ 13215 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13216 copy_length); 13217 13218 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13219 13220 /* 13221 * Dispatch the WRITE command to the taskq thread, which 13222 * will in turn send the command to the target. When the 13223 * WRITE command completes, we (sd_mapblocksize_iodone()) 13224 * will get called again as part of the iodone chain 13225 * processing for it. Note that we will still be dealing 13226 * with the shadow buf at that point. 13227 */ 13228 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13229 KM_NOSLEEP) != 0) { 13230 /* 13231 * Dispatch was successful so we are done. Return 13232 * without going any higher up the iodone chain. Do 13233 * not free up any layer-private data until after the 13234 * WRITE completes. 13235 */ 13236 return; 13237 } 13238 13239 /* 13240 * Dispatch of the WRITE command failed; set up the error 13241 * condition and send this IO back up the iodone chain. 13242 */ 13243 bioerror(orig_bp, EIO); 13244 orig_bp->b_resid = orig_bp->b_bcount; 13245 13246 } else { 13247 /* 13248 * This is a regular READ request (ie, not a RMW). Copy the 13249 * data from the shadow buf into the original buf. The 13250 * copy_offset compensates for any "misalignment" between the 13251 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13252 * original buf (with its un->un_sys_blocksize blocks). 13253 */ 13254 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13255 copy_length); 13256 } 13257 13258 freebuf_done: 13259 13260 /* 13261 * At this point we still have both the shadow buf AND the original 13262 * buf to deal with, as well as the layer-private data area in each. 13263 * Local variables are as follows: 13264 * 13265 * bp -- points to shadow buf 13266 * xp -- points to xbuf of shadow buf 13267 * bsp -- points to layer-private data area of shadow buf 13268 * orig_bp -- points to original buf 13269 * 13270 * First free the shadow buf and its associated xbuf, then free the 13271 * layer-private data area from the shadow buf. There is no need to 13272 * restore xb_private in the shadow xbuf. 13273 */ 13274 sd_shadow_buf_free(bp); 13275 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13276 13277 /* 13278 * Now update the local variables to point to the original buf, xbuf, 13279 * and layer-private area. 13280 */ 13281 bp = orig_bp; 13282 xp = SD_GET_XBUF(bp); 13283 ASSERT(xp != NULL); 13284 ASSERT(xp == orig_xp); 13285 bsp = xp->xb_private; 13286 ASSERT(bsp != NULL); 13287 13288 done: 13289 /* 13290 * Restore xb_private to whatever it was set to by the next higher 13291 * layer in the chain, then free the layer-private data area. 13292 */ 13293 xp->xb_private = bsp->mbs_oprivate; 13294 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13295 13296 exit: 13297 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13298 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13299 13300 SD_NEXT_IODONE(index, un, bp); 13301 } 13302 13303 13304 /* 13305 * Function: sd_checksum_iostart 13306 * 13307 * Description: A stub function for a layer that's currently not used. 13308 * For now just a placeholder. 13309 * 13310 * Context: Kernel thread context 13311 */ 13312 13313 static void 13314 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13315 { 13316 ASSERT(un != NULL); 13317 ASSERT(bp != NULL); 13318 ASSERT(!mutex_owned(SD_MUTEX(un))); 13319 SD_NEXT_IOSTART(index, un, bp); 13320 } 13321 13322 13323 /* 13324 * Function: sd_checksum_iodone 13325 * 13326 * Description: A stub function for a layer that's currently not used. 13327 * For now just a placeholder. 13328 * 13329 * Context: May be called under interrupt context 13330 */ 13331 13332 static void 13333 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13334 { 13335 ASSERT(un != NULL); 13336 ASSERT(bp != NULL); 13337 ASSERT(!mutex_owned(SD_MUTEX(un))); 13338 SD_NEXT_IODONE(index, un, bp); 13339 } 13340 13341 13342 /* 13343 * Function: sd_checksum_uscsi_iostart 13344 * 13345 * Description: A stub function for a layer that's currently not used. 13346 * For now just a placeholder. 13347 * 13348 * Context: Kernel thread context 13349 */ 13350 13351 static void 13352 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13353 { 13354 ASSERT(un != NULL); 13355 ASSERT(bp != NULL); 13356 ASSERT(!mutex_owned(SD_MUTEX(un))); 13357 SD_NEXT_IOSTART(index, un, bp); 13358 } 13359 13360 13361 /* 13362 * Function: sd_checksum_uscsi_iodone 13363 * 13364 * Description: A stub function for a layer that's currently not used. 13365 * For now just a placeholder. 13366 * 13367 * Context: May be called under interrupt context 13368 */ 13369 13370 static void 13371 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13372 { 13373 ASSERT(un != NULL); 13374 ASSERT(bp != NULL); 13375 ASSERT(!mutex_owned(SD_MUTEX(un))); 13376 SD_NEXT_IODONE(index, un, bp); 13377 } 13378 13379 13380 /* 13381 * Function: sd_pm_iostart 13382 * 13383 * Description: iostart-side routine for Power mangement. 13384 * 13385 * Context: Kernel thread context 13386 */ 13387 13388 static void 13389 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13390 { 13391 ASSERT(un != NULL); 13392 ASSERT(bp != NULL); 13393 ASSERT(!mutex_owned(SD_MUTEX(un))); 13394 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13395 13396 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13397 13398 if (sd_pm_entry(un) != DDI_SUCCESS) { 13399 /* 13400 * Set up to return the failed buf back up the 'iodone' 13401 * side of the calling chain. 13402 */ 13403 bioerror(bp, EIO); 13404 bp->b_resid = bp->b_bcount; 13405 13406 SD_BEGIN_IODONE(index, un, bp); 13407 13408 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13409 return; 13410 } 13411 13412 SD_NEXT_IOSTART(index, un, bp); 13413 13414 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13415 } 13416 13417 13418 /* 13419 * Function: sd_pm_iodone 13420 * 13421 * Description: iodone-side routine for power mangement. 13422 * 13423 * Context: may be called from interrupt context 13424 */ 13425 13426 static void 13427 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13428 { 13429 ASSERT(un != NULL); 13430 ASSERT(bp != NULL); 13431 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13432 13433 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13434 13435 /* 13436 * After attach the following flag is only read, so don't 13437 * take the penalty of acquiring a mutex for it. 13438 */ 13439 if (un->un_f_pm_is_enabled == TRUE) { 13440 sd_pm_exit(un); 13441 } 13442 13443 SD_NEXT_IODONE(index, un, bp); 13444 13445 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13446 } 13447 13448 13449 /* 13450 * Function: sd_core_iostart 13451 * 13452 * Description: Primary driver function for enqueuing buf(9S) structs from 13453 * the system and initiating IO to the target device 13454 * 13455 * Context: Kernel thread context. Can sleep. 13456 * 13457 * Assumptions: - The given xp->xb_blkno is absolute 13458 * (ie, relative to the start of the device). 13459 * - The IO is to be done using the native blocksize of 13460 * the device, as specified in un->un_tgt_blocksize. 13461 */ 13462 /* ARGSUSED */ 13463 static void 13464 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13465 { 13466 struct sd_xbuf *xp; 13467 13468 ASSERT(un != NULL); 13469 ASSERT(bp != NULL); 13470 ASSERT(!mutex_owned(SD_MUTEX(un))); 13471 ASSERT(bp->b_resid == 0); 13472 13473 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13474 13475 xp = SD_GET_XBUF(bp); 13476 ASSERT(xp != NULL); 13477 13478 mutex_enter(SD_MUTEX(un)); 13479 13480 /* 13481 * If we are currently in the failfast state, fail any new IO 13482 * that has B_FAILFAST set, then return. 13483 */ 13484 if ((bp->b_flags & B_FAILFAST) && 13485 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13486 mutex_exit(SD_MUTEX(un)); 13487 bioerror(bp, EIO); 13488 bp->b_resid = bp->b_bcount; 13489 SD_BEGIN_IODONE(index, un, bp); 13490 return; 13491 } 13492 13493 if (SD_IS_DIRECT_PRIORITY(xp)) { 13494 /* 13495 * Priority command -- transport it immediately. 13496 * 13497 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13498 * because all direct priority commands should be associated 13499 * with error recovery actions which we don't want to retry. 13500 */ 13501 sd_start_cmds(un, bp); 13502 } else { 13503 /* 13504 * Normal command -- add it to the wait queue, then start 13505 * transporting commands from the wait queue. 13506 */ 13507 sd_add_buf_to_waitq(un, bp); 13508 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13509 sd_start_cmds(un, NULL); 13510 } 13511 13512 mutex_exit(SD_MUTEX(un)); 13513 13514 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13515 } 13516 13517 13518 /* 13519 * Function: sd_init_cdb_limits 13520 * 13521 * Description: This is to handle scsi_pkt initialization differences 13522 * between the driver platforms. 13523 * 13524 * Legacy behaviors: 13525 * 13526 * If the block number or the sector count exceeds the 13527 * capabilities of a Group 0 command, shift over to a 13528 * Group 1 command. We don't blindly use Group 1 13529 * commands because a) some drives (CDC Wren IVs) get a 13530 * bit confused, and b) there is probably a fair amount 13531 * of speed difference for a target to receive and decode 13532 * a 10 byte command instead of a 6 byte command. 13533 * 13534 * The xfer time difference of 6 vs 10 byte CDBs is 13535 * still significant so this code is still worthwhile. 13536 * 10 byte CDBs are very inefficient with the fas HBA driver 13537 * and older disks. Each CDB byte took 1 usec with some 13538 * popular disks. 13539 * 13540 * Context: Must be called at attach time 13541 */ 13542 13543 static void 13544 sd_init_cdb_limits(struct sd_lun *un) 13545 { 13546 int hba_cdb_limit; 13547 13548 /* 13549 * Use CDB_GROUP1 commands for most devices except for 13550 * parallel SCSI fixed drives in which case we get better 13551 * performance using CDB_GROUP0 commands (where applicable). 13552 */ 13553 un->un_mincdb = SD_CDB_GROUP1; 13554 #if !defined(__fibre) 13555 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13556 !un->un_f_has_removable_media) { 13557 un->un_mincdb = SD_CDB_GROUP0; 13558 } 13559 #endif 13560 13561 /* 13562 * Try to read the max-cdb-length supported by HBA. 13563 */ 13564 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13565 if (0 >= un->un_max_hba_cdb) { 13566 un->un_max_hba_cdb = CDB_GROUP4; 13567 hba_cdb_limit = SD_CDB_GROUP4; 13568 } else if (0 < un->un_max_hba_cdb && 13569 un->un_max_hba_cdb < CDB_GROUP1) { 13570 hba_cdb_limit = SD_CDB_GROUP0; 13571 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13572 un->un_max_hba_cdb < CDB_GROUP5) { 13573 hba_cdb_limit = SD_CDB_GROUP1; 13574 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13575 un->un_max_hba_cdb < CDB_GROUP4) { 13576 hba_cdb_limit = SD_CDB_GROUP5; 13577 } else { 13578 hba_cdb_limit = SD_CDB_GROUP4; 13579 } 13580 13581 /* 13582 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13583 * commands for fixed disks unless we are building for a 32 bit 13584 * kernel. 13585 */ 13586 #ifdef _LP64 13587 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13588 min(hba_cdb_limit, SD_CDB_GROUP4); 13589 #else 13590 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13591 min(hba_cdb_limit, SD_CDB_GROUP1); 13592 #endif 13593 13594 /* 13595 * x86 systems require the PKT_DMA_PARTIAL flag 13596 */ 13597 #if defined(__x86) 13598 un->un_pkt_flags = PKT_DMA_PARTIAL; 13599 #else 13600 un->un_pkt_flags = 0; 13601 #endif 13602 13603 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13604 ? sizeof (struct scsi_arq_status) : 1); 13605 un->un_cmd_timeout = (ushort_t)sd_io_time; 13606 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13607 } 13608 13609 13610 /* 13611 * Function: sd_initpkt_for_buf 13612 * 13613 * Description: Allocate and initialize for transport a scsi_pkt struct, 13614 * based upon the info specified in the given buf struct. 13615 * 13616 * Assumes the xb_blkno in the request is absolute (ie, 13617 * relative to the start of the device (NOT partition!). 13618 * Also assumes that the request is using the native block 13619 * size of the device (as returned by the READ CAPACITY 13620 * command). 13621 * 13622 * Return Code: SD_PKT_ALLOC_SUCCESS 13623 * SD_PKT_ALLOC_FAILURE 13624 * SD_PKT_ALLOC_FAILURE_NO_DMA 13625 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13626 * 13627 * Context: Kernel thread and may be called from software interrupt context 13628 * as part of a sdrunout callback. This function may not block or 13629 * call routines that block 13630 */ 13631 13632 static int 13633 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13634 { 13635 struct sd_xbuf *xp; 13636 struct scsi_pkt *pktp = NULL; 13637 struct sd_lun *un; 13638 size_t blockcount; 13639 daddr_t startblock; 13640 int rval; 13641 int cmd_flags; 13642 13643 ASSERT(bp != NULL); 13644 ASSERT(pktpp != NULL); 13645 xp = SD_GET_XBUF(bp); 13646 ASSERT(xp != NULL); 13647 un = SD_GET_UN(bp); 13648 ASSERT(un != NULL); 13649 ASSERT(mutex_owned(SD_MUTEX(un))); 13650 ASSERT(bp->b_resid == 0); 13651 13652 SD_TRACE(SD_LOG_IO_CORE, un, 13653 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13654 13655 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13656 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13657 /* 13658 * Already have a scsi_pkt -- just need DMA resources. 13659 * We must recompute the CDB in case the mapping returns 13660 * a nonzero pkt_resid. 13661 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13662 * that is being retried, the unmap/remap of the DMA resouces 13663 * will result in the entire transfer starting over again 13664 * from the very first block. 13665 */ 13666 ASSERT(xp->xb_pktp != NULL); 13667 pktp = xp->xb_pktp; 13668 } else { 13669 pktp = NULL; 13670 } 13671 #endif /* __i386 || __amd64 */ 13672 13673 startblock = xp->xb_blkno; /* Absolute block num. */ 13674 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13675 13676 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13677 13678 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13679 13680 #else 13681 13682 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13683 13684 #endif 13685 13686 /* 13687 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13688 * call scsi_init_pkt, and build the CDB. 13689 */ 13690 rval = sd_setup_rw_pkt(un, &pktp, bp, 13691 cmd_flags, sdrunout, (caddr_t)un, 13692 startblock, blockcount); 13693 13694 if (rval == 0) { 13695 /* 13696 * Success. 13697 * 13698 * If partial DMA is being used and required for this transfer. 13699 * set it up here. 13700 */ 13701 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13702 (pktp->pkt_resid != 0)) { 13703 13704 /* 13705 * Save the CDB length and pkt_resid for the 13706 * next xfer 13707 */ 13708 xp->xb_dma_resid = pktp->pkt_resid; 13709 13710 /* rezero resid */ 13711 pktp->pkt_resid = 0; 13712 13713 } else { 13714 xp->xb_dma_resid = 0; 13715 } 13716 13717 pktp->pkt_flags = un->un_tagflags; 13718 pktp->pkt_time = un->un_cmd_timeout; 13719 pktp->pkt_comp = sdintr; 13720 13721 pktp->pkt_private = bp; 13722 *pktpp = pktp; 13723 13724 SD_TRACE(SD_LOG_IO_CORE, un, 13725 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13726 13727 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13728 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13729 #endif 13730 13731 return (SD_PKT_ALLOC_SUCCESS); 13732 13733 } 13734 13735 /* 13736 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13737 * from sd_setup_rw_pkt. 13738 */ 13739 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13740 13741 if (rval == SD_PKT_ALLOC_FAILURE) { 13742 *pktpp = NULL; 13743 /* 13744 * Set the driver state to RWAIT to indicate the driver 13745 * is waiting on resource allocations. The driver will not 13746 * suspend, pm_suspend, or detatch while the state is RWAIT. 13747 */ 13748 New_state(un, SD_STATE_RWAIT); 13749 13750 SD_ERROR(SD_LOG_IO_CORE, un, 13751 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13752 13753 if ((bp->b_flags & B_ERROR) != 0) { 13754 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13755 } 13756 return (SD_PKT_ALLOC_FAILURE); 13757 } else { 13758 /* 13759 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13760 * 13761 * This should never happen. Maybe someone messed with the 13762 * kernel's minphys? 13763 */ 13764 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13765 "Request rejected: too large for CDB: " 13766 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13767 SD_ERROR(SD_LOG_IO_CORE, un, 13768 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13769 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13770 13771 } 13772 } 13773 13774 13775 /* 13776 * Function: sd_destroypkt_for_buf 13777 * 13778 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13779 * 13780 * Context: Kernel thread or interrupt context 13781 */ 13782 13783 static void 13784 sd_destroypkt_for_buf(struct buf *bp) 13785 { 13786 ASSERT(bp != NULL); 13787 ASSERT(SD_GET_UN(bp) != NULL); 13788 13789 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13790 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13791 13792 ASSERT(SD_GET_PKTP(bp) != NULL); 13793 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13794 13795 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13796 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13797 } 13798 13799 /* 13800 * Function: sd_setup_rw_pkt 13801 * 13802 * Description: Determines appropriate CDB group for the requested LBA 13803 * and transfer length, calls scsi_init_pkt, and builds 13804 * the CDB. Do not use for partial DMA transfers except 13805 * for the initial transfer since the CDB size must 13806 * remain constant. 13807 * 13808 * Context: Kernel thread and may be called from software interrupt 13809 * context as part of a sdrunout callback. This function may not 13810 * block or call routines that block 13811 */ 13812 13813 13814 int 13815 sd_setup_rw_pkt(struct sd_lun *un, 13816 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13817 int (*callback)(caddr_t), caddr_t callback_arg, 13818 diskaddr_t lba, uint32_t blockcount) 13819 { 13820 struct scsi_pkt *return_pktp; 13821 union scsi_cdb *cdbp; 13822 struct sd_cdbinfo *cp = NULL; 13823 int i; 13824 13825 /* 13826 * See which size CDB to use, based upon the request. 13827 */ 13828 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13829 13830 /* 13831 * Check lba and block count against sd_cdbtab limits. 13832 * In the partial DMA case, we have to use the same size 13833 * CDB for all the transfers. Check lba + blockcount 13834 * against the max LBA so we know that segment of the 13835 * transfer can use the CDB we select. 13836 */ 13837 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13838 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13839 13840 /* 13841 * The command will fit into the CDB type 13842 * specified by sd_cdbtab[i]. 13843 */ 13844 cp = sd_cdbtab + i; 13845 13846 /* 13847 * Call scsi_init_pkt so we can fill in the 13848 * CDB. 13849 */ 13850 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13851 bp, cp->sc_grpcode, un->un_status_len, 0, 13852 flags, callback, callback_arg); 13853 13854 if (return_pktp != NULL) { 13855 13856 /* 13857 * Return new value of pkt 13858 */ 13859 *pktpp = return_pktp; 13860 13861 /* 13862 * To be safe, zero the CDB insuring there is 13863 * no leftover data from a previous command. 13864 */ 13865 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13866 13867 /* 13868 * Handle partial DMA mapping 13869 */ 13870 if (return_pktp->pkt_resid != 0) { 13871 13872 /* 13873 * Not going to xfer as many blocks as 13874 * originally expected 13875 */ 13876 blockcount -= 13877 SD_BYTES2TGTBLOCKS(un, 13878 return_pktp->pkt_resid); 13879 } 13880 13881 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13882 13883 /* 13884 * Set command byte based on the CDB 13885 * type we matched. 13886 */ 13887 cdbp->scc_cmd = cp->sc_grpmask | 13888 ((bp->b_flags & B_READ) ? 13889 SCMD_READ : SCMD_WRITE); 13890 13891 SD_FILL_SCSI1_LUN(un, return_pktp); 13892 13893 /* 13894 * Fill in LBA and length 13895 */ 13896 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13897 (cp->sc_grpcode == CDB_GROUP4) || 13898 (cp->sc_grpcode == CDB_GROUP0) || 13899 (cp->sc_grpcode == CDB_GROUP5)); 13900 13901 if (cp->sc_grpcode == CDB_GROUP1) { 13902 FORMG1ADDR(cdbp, lba); 13903 FORMG1COUNT(cdbp, blockcount); 13904 return (0); 13905 } else if (cp->sc_grpcode == CDB_GROUP4) { 13906 FORMG4LONGADDR(cdbp, lba); 13907 FORMG4COUNT(cdbp, blockcount); 13908 return (0); 13909 } else if (cp->sc_grpcode == CDB_GROUP0) { 13910 FORMG0ADDR(cdbp, lba); 13911 FORMG0COUNT(cdbp, blockcount); 13912 return (0); 13913 } else if (cp->sc_grpcode == CDB_GROUP5) { 13914 FORMG5ADDR(cdbp, lba); 13915 FORMG5COUNT(cdbp, blockcount); 13916 return (0); 13917 } 13918 13919 /* 13920 * It should be impossible to not match one 13921 * of the CDB types above, so we should never 13922 * reach this point. Set the CDB command byte 13923 * to test-unit-ready to avoid writing 13924 * to somewhere we don't intend. 13925 */ 13926 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13927 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13928 } else { 13929 /* 13930 * Couldn't get scsi_pkt 13931 */ 13932 return (SD_PKT_ALLOC_FAILURE); 13933 } 13934 } 13935 } 13936 13937 /* 13938 * None of the available CDB types were suitable. This really 13939 * should never happen: on a 64 bit system we support 13940 * READ16/WRITE16 which will hold an entire 64 bit disk address 13941 * and on a 32 bit system we will refuse to bind to a device 13942 * larger than 2TB so addresses will never be larger than 32 bits. 13943 */ 13944 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13945 } 13946 13947 #if defined(__i386) || defined(__amd64) 13948 /* 13949 * Function: sd_setup_next_rw_pkt 13950 * 13951 * Description: Setup packet for partial DMA transfers, except for the 13952 * initial transfer. sd_setup_rw_pkt should be used for 13953 * the initial transfer. 13954 * 13955 * Context: Kernel thread and may be called from interrupt context. 13956 */ 13957 13958 int 13959 sd_setup_next_rw_pkt(struct sd_lun *un, 13960 struct scsi_pkt *pktp, struct buf *bp, 13961 diskaddr_t lba, uint32_t blockcount) 13962 { 13963 uchar_t com; 13964 union scsi_cdb *cdbp; 13965 uchar_t cdb_group_id; 13966 13967 ASSERT(pktp != NULL); 13968 ASSERT(pktp->pkt_cdbp != NULL); 13969 13970 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13971 com = cdbp->scc_cmd; 13972 cdb_group_id = CDB_GROUPID(com); 13973 13974 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13975 (cdb_group_id == CDB_GROUPID_1) || 13976 (cdb_group_id == CDB_GROUPID_4) || 13977 (cdb_group_id == CDB_GROUPID_5)); 13978 13979 /* 13980 * Move pkt to the next portion of the xfer. 13981 * func is NULL_FUNC so we do not have to release 13982 * the disk mutex here. 13983 */ 13984 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13985 NULL_FUNC, NULL) == pktp) { 13986 /* Success. Handle partial DMA */ 13987 if (pktp->pkt_resid != 0) { 13988 blockcount -= 13989 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 13990 } 13991 13992 cdbp->scc_cmd = com; 13993 SD_FILL_SCSI1_LUN(un, pktp); 13994 if (cdb_group_id == CDB_GROUPID_1) { 13995 FORMG1ADDR(cdbp, lba); 13996 FORMG1COUNT(cdbp, blockcount); 13997 return (0); 13998 } else if (cdb_group_id == CDB_GROUPID_4) { 13999 FORMG4LONGADDR(cdbp, lba); 14000 FORMG4COUNT(cdbp, blockcount); 14001 return (0); 14002 } else if (cdb_group_id == CDB_GROUPID_0) { 14003 FORMG0ADDR(cdbp, lba); 14004 FORMG0COUNT(cdbp, blockcount); 14005 return (0); 14006 } else if (cdb_group_id == CDB_GROUPID_5) { 14007 FORMG5ADDR(cdbp, lba); 14008 FORMG5COUNT(cdbp, blockcount); 14009 return (0); 14010 } 14011 14012 /* Unreachable */ 14013 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14014 } 14015 14016 /* 14017 * Error setting up next portion of cmd transfer. 14018 * Something is definitely very wrong and this 14019 * should not happen. 14020 */ 14021 return (SD_PKT_ALLOC_FAILURE); 14022 } 14023 #endif /* defined(__i386) || defined(__amd64) */ 14024 14025 /* 14026 * Function: sd_initpkt_for_uscsi 14027 * 14028 * Description: Allocate and initialize for transport a scsi_pkt struct, 14029 * based upon the info specified in the given uscsi_cmd struct. 14030 * 14031 * Return Code: SD_PKT_ALLOC_SUCCESS 14032 * SD_PKT_ALLOC_FAILURE 14033 * SD_PKT_ALLOC_FAILURE_NO_DMA 14034 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14035 * 14036 * Context: Kernel thread and may be called from software interrupt context 14037 * as part of a sdrunout callback. This function may not block or 14038 * call routines that block 14039 */ 14040 14041 static int 14042 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14043 { 14044 struct uscsi_cmd *uscmd; 14045 struct sd_xbuf *xp; 14046 struct scsi_pkt *pktp; 14047 struct sd_lun *un; 14048 uint32_t flags = 0; 14049 14050 ASSERT(bp != NULL); 14051 ASSERT(pktpp != NULL); 14052 xp = SD_GET_XBUF(bp); 14053 ASSERT(xp != NULL); 14054 un = SD_GET_UN(bp); 14055 ASSERT(un != NULL); 14056 ASSERT(mutex_owned(SD_MUTEX(un))); 14057 14058 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14059 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14060 ASSERT(uscmd != NULL); 14061 14062 SD_TRACE(SD_LOG_IO_CORE, un, 14063 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14064 14065 /* 14066 * Allocate the scsi_pkt for the command. 14067 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14068 * during scsi_init_pkt time and will continue to use the 14069 * same path as long as the same scsi_pkt is used without 14070 * intervening scsi_dma_free(). Since uscsi command does 14071 * not call scsi_dmafree() before retry failed command, it 14072 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14073 * set such that scsi_vhci can use other available path for 14074 * retry. Besides, ucsci command does not allow DMA breakup, 14075 * so there is no need to set PKT_DMA_PARTIAL flag. 14076 */ 14077 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14078 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14079 sizeof (struct scsi_arq_status), 0, 14080 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14081 sdrunout, (caddr_t)un); 14082 14083 if (pktp == NULL) { 14084 *pktpp = NULL; 14085 /* 14086 * Set the driver state to RWAIT to indicate the driver 14087 * is waiting on resource allocations. The driver will not 14088 * suspend, pm_suspend, or detatch while the state is RWAIT. 14089 */ 14090 New_state(un, SD_STATE_RWAIT); 14091 14092 SD_ERROR(SD_LOG_IO_CORE, un, 14093 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14094 14095 if ((bp->b_flags & B_ERROR) != 0) { 14096 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14097 } 14098 return (SD_PKT_ALLOC_FAILURE); 14099 } 14100 14101 /* 14102 * We do not do DMA breakup for USCSI commands, so return failure 14103 * here if all the needed DMA resources were not allocated. 14104 */ 14105 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14106 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14107 scsi_destroy_pkt(pktp); 14108 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14109 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14110 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14111 } 14112 14113 /* Init the cdb from the given uscsi struct */ 14114 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14115 uscmd->uscsi_cdb[0], 0, 0, 0); 14116 14117 SD_FILL_SCSI1_LUN(un, pktp); 14118 14119 /* 14120 * Set up the optional USCSI flags. See the uscsi (7I) man page 14121 * for listing of the supported flags. 14122 */ 14123 14124 if (uscmd->uscsi_flags & USCSI_SILENT) { 14125 flags |= FLAG_SILENT; 14126 } 14127 14128 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14129 flags |= FLAG_DIAGNOSE; 14130 } 14131 14132 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14133 flags |= FLAG_ISOLATE; 14134 } 14135 14136 if (un->un_f_is_fibre == FALSE) { 14137 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14138 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14139 } 14140 } 14141 14142 /* 14143 * Set the pkt flags here so we save time later. 14144 * Note: These flags are NOT in the uscsi man page!!! 14145 */ 14146 if (uscmd->uscsi_flags & USCSI_HEAD) { 14147 flags |= FLAG_HEAD; 14148 } 14149 14150 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14151 flags |= FLAG_NOINTR; 14152 } 14153 14154 /* 14155 * For tagged queueing, things get a bit complicated. 14156 * Check first for head of queue and last for ordered queue. 14157 * If neither head nor order, use the default driver tag flags. 14158 */ 14159 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14160 if (uscmd->uscsi_flags & USCSI_HTAG) { 14161 flags |= FLAG_HTAG; 14162 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14163 flags |= FLAG_OTAG; 14164 } else { 14165 flags |= un->un_tagflags & FLAG_TAGMASK; 14166 } 14167 } 14168 14169 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14170 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14171 } 14172 14173 pktp->pkt_flags = flags; 14174 14175 /* Copy the caller's CDB into the pkt... */ 14176 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14177 14178 if (uscmd->uscsi_timeout == 0) { 14179 pktp->pkt_time = un->un_uscsi_timeout; 14180 } else { 14181 pktp->pkt_time = uscmd->uscsi_timeout; 14182 } 14183 14184 /* need it later to identify USCSI request in sdintr */ 14185 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14186 14187 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14188 14189 pktp->pkt_private = bp; 14190 pktp->pkt_comp = sdintr; 14191 *pktpp = pktp; 14192 14193 SD_TRACE(SD_LOG_IO_CORE, un, 14194 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14195 14196 return (SD_PKT_ALLOC_SUCCESS); 14197 } 14198 14199 14200 /* 14201 * Function: sd_destroypkt_for_uscsi 14202 * 14203 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14204 * IOs.. Also saves relevant info into the associated uscsi_cmd 14205 * struct. 14206 * 14207 * Context: May be called under interrupt context 14208 */ 14209 14210 static void 14211 sd_destroypkt_for_uscsi(struct buf *bp) 14212 { 14213 struct uscsi_cmd *uscmd; 14214 struct sd_xbuf *xp; 14215 struct scsi_pkt *pktp; 14216 struct sd_lun *un; 14217 14218 ASSERT(bp != NULL); 14219 xp = SD_GET_XBUF(bp); 14220 ASSERT(xp != NULL); 14221 un = SD_GET_UN(bp); 14222 ASSERT(un != NULL); 14223 ASSERT(!mutex_owned(SD_MUTEX(un))); 14224 pktp = SD_GET_PKTP(bp); 14225 ASSERT(pktp != NULL); 14226 14227 SD_TRACE(SD_LOG_IO_CORE, un, 14228 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14229 14230 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14231 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14232 ASSERT(uscmd != NULL); 14233 14234 /* Save the status and the residual into the uscsi_cmd struct */ 14235 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14236 uscmd->uscsi_resid = bp->b_resid; 14237 14238 /* 14239 * If enabled, copy any saved sense data into the area specified 14240 * by the uscsi command. 14241 */ 14242 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14243 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14244 /* 14245 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14246 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14247 */ 14248 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14249 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14250 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 14251 } 14252 14253 /* We are done with the scsi_pkt; free it now */ 14254 ASSERT(SD_GET_PKTP(bp) != NULL); 14255 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14256 14257 SD_TRACE(SD_LOG_IO_CORE, un, 14258 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14259 } 14260 14261 14262 /* 14263 * Function: sd_bioclone_alloc 14264 * 14265 * Description: Allocate a buf(9S) and init it as per the given buf 14266 * and the various arguments. The associated sd_xbuf 14267 * struct is (nearly) duplicated. The struct buf *bp 14268 * argument is saved in new_xp->xb_private. 14269 * 14270 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14271 * datalen - size of data area for the shadow bp 14272 * blkno - starting LBA 14273 * func - function pointer for b_iodone in the shadow buf. (May 14274 * be NULL if none.) 14275 * 14276 * Return Code: Pointer to allocates buf(9S) struct 14277 * 14278 * Context: Can sleep. 14279 */ 14280 14281 static struct buf * 14282 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14283 daddr_t blkno, int (*func)(struct buf *)) 14284 { 14285 struct sd_lun *un; 14286 struct sd_xbuf *xp; 14287 struct sd_xbuf *new_xp; 14288 struct buf *new_bp; 14289 14290 ASSERT(bp != NULL); 14291 xp = SD_GET_XBUF(bp); 14292 ASSERT(xp != NULL); 14293 un = SD_GET_UN(bp); 14294 ASSERT(un != NULL); 14295 ASSERT(!mutex_owned(SD_MUTEX(un))); 14296 14297 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14298 NULL, KM_SLEEP); 14299 14300 new_bp->b_lblkno = blkno; 14301 14302 /* 14303 * Allocate an xbuf for the shadow bp and copy the contents of the 14304 * original xbuf into it. 14305 */ 14306 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14307 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14308 14309 /* 14310 * The given bp is automatically saved in the xb_private member 14311 * of the new xbuf. Callers are allowed to depend on this. 14312 */ 14313 new_xp->xb_private = bp; 14314 14315 new_bp->b_private = new_xp; 14316 14317 return (new_bp); 14318 } 14319 14320 /* 14321 * Function: sd_shadow_buf_alloc 14322 * 14323 * Description: Allocate a buf(9S) and init it as per the given buf 14324 * and the various arguments. The associated sd_xbuf 14325 * struct is (nearly) duplicated. The struct buf *bp 14326 * argument is saved in new_xp->xb_private. 14327 * 14328 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14329 * datalen - size of data area for the shadow bp 14330 * bflags - B_READ or B_WRITE (pseudo flag) 14331 * blkno - starting LBA 14332 * func - function pointer for b_iodone in the shadow buf. (May 14333 * be NULL if none.) 14334 * 14335 * Return Code: Pointer to allocates buf(9S) struct 14336 * 14337 * Context: Can sleep. 14338 */ 14339 14340 static struct buf * 14341 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14342 daddr_t blkno, int (*func)(struct buf *)) 14343 { 14344 struct sd_lun *un; 14345 struct sd_xbuf *xp; 14346 struct sd_xbuf *new_xp; 14347 struct buf *new_bp; 14348 14349 ASSERT(bp != NULL); 14350 xp = SD_GET_XBUF(bp); 14351 ASSERT(xp != NULL); 14352 un = SD_GET_UN(bp); 14353 ASSERT(un != NULL); 14354 ASSERT(!mutex_owned(SD_MUTEX(un))); 14355 14356 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14357 bp_mapin(bp); 14358 } 14359 14360 bflags &= (B_READ | B_WRITE); 14361 #if defined(__i386) || defined(__amd64) 14362 new_bp = getrbuf(KM_SLEEP); 14363 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14364 new_bp->b_bcount = datalen; 14365 new_bp->b_flags = bflags | 14366 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14367 #else 14368 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14369 datalen, bflags, SLEEP_FUNC, NULL); 14370 #endif 14371 new_bp->av_forw = NULL; 14372 new_bp->av_back = NULL; 14373 new_bp->b_dev = bp->b_dev; 14374 new_bp->b_blkno = blkno; 14375 new_bp->b_iodone = func; 14376 new_bp->b_edev = bp->b_edev; 14377 new_bp->b_resid = 0; 14378 14379 /* We need to preserve the B_FAILFAST flag */ 14380 if (bp->b_flags & B_FAILFAST) { 14381 new_bp->b_flags |= B_FAILFAST; 14382 } 14383 14384 /* 14385 * Allocate an xbuf for the shadow bp and copy the contents of the 14386 * original xbuf into it. 14387 */ 14388 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14389 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14390 14391 /* Need later to copy data between the shadow buf & original buf! */ 14392 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14393 14394 /* 14395 * The given bp is automatically saved in the xb_private member 14396 * of the new xbuf. Callers are allowed to depend on this. 14397 */ 14398 new_xp->xb_private = bp; 14399 14400 new_bp->b_private = new_xp; 14401 14402 return (new_bp); 14403 } 14404 14405 /* 14406 * Function: sd_bioclone_free 14407 * 14408 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14409 * in the larger than partition operation. 14410 * 14411 * Context: May be called under interrupt context 14412 */ 14413 14414 static void 14415 sd_bioclone_free(struct buf *bp) 14416 { 14417 struct sd_xbuf *xp; 14418 14419 ASSERT(bp != NULL); 14420 xp = SD_GET_XBUF(bp); 14421 ASSERT(xp != NULL); 14422 14423 /* 14424 * Call bp_mapout() before freeing the buf, in case a lower 14425 * layer or HBA had done a bp_mapin(). we must do this here 14426 * as we are the "originator" of the shadow buf. 14427 */ 14428 bp_mapout(bp); 14429 14430 /* 14431 * Null out b_iodone before freeing the bp, to ensure that the driver 14432 * never gets confused by a stale value in this field. (Just a little 14433 * extra defensiveness here.) 14434 */ 14435 bp->b_iodone = NULL; 14436 14437 freerbuf(bp); 14438 14439 kmem_free(xp, sizeof (struct sd_xbuf)); 14440 } 14441 14442 /* 14443 * Function: sd_shadow_buf_free 14444 * 14445 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14446 * 14447 * Context: May be called under interrupt context 14448 */ 14449 14450 static void 14451 sd_shadow_buf_free(struct buf *bp) 14452 { 14453 struct sd_xbuf *xp; 14454 14455 ASSERT(bp != NULL); 14456 xp = SD_GET_XBUF(bp); 14457 ASSERT(xp != NULL); 14458 14459 #if defined(__sparc) 14460 /* 14461 * Call bp_mapout() before freeing the buf, in case a lower 14462 * layer or HBA had done a bp_mapin(). we must do this here 14463 * as we are the "originator" of the shadow buf. 14464 */ 14465 bp_mapout(bp); 14466 #endif 14467 14468 /* 14469 * Null out b_iodone before freeing the bp, to ensure that the driver 14470 * never gets confused by a stale value in this field. (Just a little 14471 * extra defensiveness here.) 14472 */ 14473 bp->b_iodone = NULL; 14474 14475 #if defined(__i386) || defined(__amd64) 14476 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14477 freerbuf(bp); 14478 #else 14479 scsi_free_consistent_buf(bp); 14480 #endif 14481 14482 kmem_free(xp, sizeof (struct sd_xbuf)); 14483 } 14484 14485 14486 /* 14487 * Function: sd_print_transport_rejected_message 14488 * 14489 * Description: This implements the ludicrously complex rules for printing 14490 * a "transport rejected" message. This is to address the 14491 * specific problem of having a flood of this error message 14492 * produced when a failover occurs. 14493 * 14494 * Context: Any. 14495 */ 14496 14497 static void 14498 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14499 int code) 14500 { 14501 ASSERT(un != NULL); 14502 ASSERT(mutex_owned(SD_MUTEX(un))); 14503 ASSERT(xp != NULL); 14504 14505 /* 14506 * Print the "transport rejected" message under the following 14507 * conditions: 14508 * 14509 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14510 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14511 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14512 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14513 * scsi_transport(9F) (which indicates that the target might have 14514 * gone off-line). This uses the un->un_tran_fatal_count 14515 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14516 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14517 * from scsi_transport(). 14518 * 14519 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14520 * the preceeding cases in order for the message to be printed. 14521 */ 14522 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14523 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14524 (code != TRAN_FATAL_ERROR) || 14525 (un->un_tran_fatal_count == 1)) { 14526 switch (code) { 14527 case TRAN_BADPKT: 14528 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14529 "transport rejected bad packet\n"); 14530 break; 14531 case TRAN_FATAL_ERROR: 14532 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14533 "transport rejected fatal error\n"); 14534 break; 14535 default: 14536 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14537 "transport rejected (%d)\n", code); 14538 break; 14539 } 14540 } 14541 } 14542 } 14543 14544 14545 /* 14546 * Function: sd_add_buf_to_waitq 14547 * 14548 * Description: Add the given buf(9S) struct to the wait queue for the 14549 * instance. If sorting is enabled, then the buf is added 14550 * to the queue via an elevator sort algorithm (a la 14551 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14552 * If sorting is not enabled, then the buf is just added 14553 * to the end of the wait queue. 14554 * 14555 * Return Code: void 14556 * 14557 * Context: Does not sleep/block, therefore technically can be called 14558 * from any context. However if sorting is enabled then the 14559 * execution time is indeterminate, and may take long if 14560 * the wait queue grows large. 14561 */ 14562 14563 static void 14564 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14565 { 14566 struct buf *ap; 14567 14568 ASSERT(bp != NULL); 14569 ASSERT(un != NULL); 14570 ASSERT(mutex_owned(SD_MUTEX(un))); 14571 14572 /* If the queue is empty, add the buf as the only entry & return. */ 14573 if (un->un_waitq_headp == NULL) { 14574 ASSERT(un->un_waitq_tailp == NULL); 14575 un->un_waitq_headp = un->un_waitq_tailp = bp; 14576 bp->av_forw = NULL; 14577 return; 14578 } 14579 14580 ASSERT(un->un_waitq_tailp != NULL); 14581 14582 /* 14583 * If sorting is disabled, just add the buf to the tail end of 14584 * the wait queue and return. 14585 */ 14586 if (un->un_f_disksort_disabled) { 14587 un->un_waitq_tailp->av_forw = bp; 14588 un->un_waitq_tailp = bp; 14589 bp->av_forw = NULL; 14590 return; 14591 } 14592 14593 /* 14594 * Sort thru the list of requests currently on the wait queue 14595 * and add the new buf request at the appropriate position. 14596 * 14597 * The un->un_waitq_headp is an activity chain pointer on which 14598 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14599 * first queue holds those requests which are positioned after 14600 * the current SD_GET_BLKNO() (in the first request); the second holds 14601 * requests which came in after their SD_GET_BLKNO() number was passed. 14602 * Thus we implement a one way scan, retracting after reaching 14603 * the end of the drive to the first request on the second 14604 * queue, at which time it becomes the first queue. 14605 * A one-way scan is natural because of the way UNIX read-ahead 14606 * blocks are allocated. 14607 * 14608 * If we lie after the first request, then we must locate the 14609 * second request list and add ourselves to it. 14610 */ 14611 ap = un->un_waitq_headp; 14612 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14613 while (ap->av_forw != NULL) { 14614 /* 14615 * Look for an "inversion" in the (normally 14616 * ascending) block numbers. This indicates 14617 * the start of the second request list. 14618 */ 14619 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14620 /* 14621 * Search the second request list for the 14622 * first request at a larger block number. 14623 * We go before that; however if there is 14624 * no such request, we go at the end. 14625 */ 14626 do { 14627 if (SD_GET_BLKNO(bp) < 14628 SD_GET_BLKNO(ap->av_forw)) { 14629 goto insert; 14630 } 14631 ap = ap->av_forw; 14632 } while (ap->av_forw != NULL); 14633 goto insert; /* after last */ 14634 } 14635 ap = ap->av_forw; 14636 } 14637 14638 /* 14639 * No inversions... we will go after the last, and 14640 * be the first request in the second request list. 14641 */ 14642 goto insert; 14643 } 14644 14645 /* 14646 * Request is at/after the current request... 14647 * sort in the first request list. 14648 */ 14649 while (ap->av_forw != NULL) { 14650 /* 14651 * We want to go after the current request (1) if 14652 * there is an inversion after it (i.e. it is the end 14653 * of the first request list), or (2) if the next 14654 * request is a larger block no. than our request. 14655 */ 14656 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14657 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14658 goto insert; 14659 } 14660 ap = ap->av_forw; 14661 } 14662 14663 /* 14664 * Neither a second list nor a larger request, therefore 14665 * we go at the end of the first list (which is the same 14666 * as the end of the whole schebang). 14667 */ 14668 insert: 14669 bp->av_forw = ap->av_forw; 14670 ap->av_forw = bp; 14671 14672 /* 14673 * If we inserted onto the tail end of the waitq, make sure the 14674 * tail pointer is updated. 14675 */ 14676 if (ap == un->un_waitq_tailp) { 14677 un->un_waitq_tailp = bp; 14678 } 14679 } 14680 14681 14682 /* 14683 * Function: sd_start_cmds 14684 * 14685 * Description: Remove and transport cmds from the driver queues. 14686 * 14687 * Arguments: un - pointer to the unit (soft state) struct for the target. 14688 * 14689 * immed_bp - ptr to a buf to be transported immediately. Only 14690 * the immed_bp is transported; bufs on the waitq are not 14691 * processed and the un_retry_bp is not checked. If immed_bp is 14692 * NULL, then normal queue processing is performed. 14693 * 14694 * Context: May be called from kernel thread context, interrupt context, 14695 * or runout callback context. This function may not block or 14696 * call routines that block. 14697 */ 14698 14699 static void 14700 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14701 { 14702 struct sd_xbuf *xp; 14703 struct buf *bp; 14704 void (*statp)(kstat_io_t *); 14705 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14706 void (*saved_statp)(kstat_io_t *); 14707 #endif 14708 int rval; 14709 14710 ASSERT(un != NULL); 14711 ASSERT(mutex_owned(SD_MUTEX(un))); 14712 ASSERT(un->un_ncmds_in_transport >= 0); 14713 ASSERT(un->un_throttle >= 0); 14714 14715 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14716 14717 do { 14718 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14719 saved_statp = NULL; 14720 #endif 14721 14722 /* 14723 * If we are syncing or dumping, fail the command to 14724 * avoid recursively calling back into scsi_transport(). 14725 * The dump I/O itself uses a separate code path so this 14726 * only prevents non-dump I/O from being sent while dumping. 14727 * File system sync takes place before dumping begins. 14728 * During panic, filesystem I/O is allowed provided 14729 * un_in_callback is <= 1. This is to prevent recursion 14730 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14731 * sd_start_cmds and so on. See panic.c for more information 14732 * about the states the system can be in during panic. 14733 */ 14734 if ((un->un_state == SD_STATE_DUMPING) || 14735 (ddi_in_panic() && (un->un_in_callback > 1))) { 14736 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14737 "sd_start_cmds: panicking\n"); 14738 goto exit; 14739 } 14740 14741 if ((bp = immed_bp) != NULL) { 14742 /* 14743 * We have a bp that must be transported immediately. 14744 * It's OK to transport the immed_bp here without doing 14745 * the throttle limit check because the immed_bp is 14746 * always used in a retry/recovery case. This means 14747 * that we know we are not at the throttle limit by 14748 * virtue of the fact that to get here we must have 14749 * already gotten a command back via sdintr(). This also 14750 * relies on (1) the command on un_retry_bp preventing 14751 * further commands from the waitq from being issued; 14752 * and (2) the code in sd_retry_command checking the 14753 * throttle limit before issuing a delayed or immediate 14754 * retry. This holds even if the throttle limit is 14755 * currently ratcheted down from its maximum value. 14756 */ 14757 statp = kstat_runq_enter; 14758 if (bp == un->un_retry_bp) { 14759 ASSERT((un->un_retry_statp == NULL) || 14760 (un->un_retry_statp == kstat_waitq_enter) || 14761 (un->un_retry_statp == 14762 kstat_runq_back_to_waitq)); 14763 /* 14764 * If the waitq kstat was incremented when 14765 * sd_set_retry_bp() queued this bp for a retry, 14766 * then we must set up statp so that the waitq 14767 * count will get decremented correctly below. 14768 * Also we must clear un->un_retry_statp to 14769 * ensure that we do not act on a stale value 14770 * in this field. 14771 */ 14772 if ((un->un_retry_statp == kstat_waitq_enter) || 14773 (un->un_retry_statp == 14774 kstat_runq_back_to_waitq)) { 14775 statp = kstat_waitq_to_runq; 14776 } 14777 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14778 saved_statp = un->un_retry_statp; 14779 #endif 14780 un->un_retry_statp = NULL; 14781 14782 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14783 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14784 "un_throttle:%d un_ncmds_in_transport:%d\n", 14785 un, un->un_retry_bp, un->un_throttle, 14786 un->un_ncmds_in_transport); 14787 } else { 14788 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14789 "processing priority bp:0x%p\n", bp); 14790 } 14791 14792 } else if ((bp = un->un_waitq_headp) != NULL) { 14793 /* 14794 * A command on the waitq is ready to go, but do not 14795 * send it if: 14796 * 14797 * (1) the throttle limit has been reached, or 14798 * (2) a retry is pending, or 14799 * (3) a START_STOP_UNIT callback pending, or 14800 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14801 * command is pending. 14802 * 14803 * For all of these conditions, IO processing will 14804 * restart after the condition is cleared. 14805 */ 14806 if (un->un_ncmds_in_transport >= un->un_throttle) { 14807 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14808 "sd_start_cmds: exiting, " 14809 "throttle limit reached!\n"); 14810 goto exit; 14811 } 14812 if (un->un_retry_bp != NULL) { 14813 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14814 "sd_start_cmds: exiting, retry pending!\n"); 14815 goto exit; 14816 } 14817 if (un->un_startstop_timeid != NULL) { 14818 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14819 "sd_start_cmds: exiting, " 14820 "START_STOP pending!\n"); 14821 goto exit; 14822 } 14823 if (un->un_direct_priority_timeid != NULL) { 14824 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14825 "sd_start_cmds: exiting, " 14826 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14827 goto exit; 14828 } 14829 14830 /* Dequeue the command */ 14831 un->un_waitq_headp = bp->av_forw; 14832 if (un->un_waitq_headp == NULL) { 14833 un->un_waitq_tailp = NULL; 14834 } 14835 bp->av_forw = NULL; 14836 statp = kstat_waitq_to_runq; 14837 SD_TRACE(SD_LOG_IO_CORE, un, 14838 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14839 14840 } else { 14841 /* No work to do so bail out now */ 14842 SD_TRACE(SD_LOG_IO_CORE, un, 14843 "sd_start_cmds: no more work, exiting!\n"); 14844 goto exit; 14845 } 14846 14847 /* 14848 * Reset the state to normal. This is the mechanism by which 14849 * the state transitions from either SD_STATE_RWAIT or 14850 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14851 * If state is SD_STATE_PM_CHANGING then this command is 14852 * part of the device power control and the state must 14853 * not be put back to normal. Doing so would would 14854 * allow new commands to proceed when they shouldn't, 14855 * the device may be going off. 14856 */ 14857 if ((un->un_state != SD_STATE_SUSPENDED) && 14858 (un->un_state != SD_STATE_PM_CHANGING)) { 14859 New_state(un, SD_STATE_NORMAL); 14860 } 14861 14862 xp = SD_GET_XBUF(bp); 14863 ASSERT(xp != NULL); 14864 14865 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14866 /* 14867 * Allocate the scsi_pkt if we need one, or attach DMA 14868 * resources if we have a scsi_pkt that needs them. The 14869 * latter should only occur for commands that are being 14870 * retried. 14871 */ 14872 if ((xp->xb_pktp == NULL) || 14873 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14874 #else 14875 if (xp->xb_pktp == NULL) { 14876 #endif 14877 /* 14878 * There is no scsi_pkt allocated for this buf. Call 14879 * the initpkt function to allocate & init one. 14880 * 14881 * The scsi_init_pkt runout callback functionality is 14882 * implemented as follows: 14883 * 14884 * 1) The initpkt function always calls 14885 * scsi_init_pkt(9F) with sdrunout specified as the 14886 * callback routine. 14887 * 2) A successful packet allocation is initialized and 14888 * the I/O is transported. 14889 * 3) The I/O associated with an allocation resource 14890 * failure is left on its queue to be retried via 14891 * runout or the next I/O. 14892 * 4) The I/O associated with a DMA error is removed 14893 * from the queue and failed with EIO. Processing of 14894 * the transport queues is also halted to be 14895 * restarted via runout or the next I/O. 14896 * 5) The I/O associated with a CDB size or packet 14897 * size error is removed from the queue and failed 14898 * with EIO. Processing of the transport queues is 14899 * continued. 14900 * 14901 * Note: there is no interface for canceling a runout 14902 * callback. To prevent the driver from detaching or 14903 * suspending while a runout is pending the driver 14904 * state is set to SD_STATE_RWAIT 14905 * 14906 * Note: using the scsi_init_pkt callback facility can 14907 * result in an I/O request persisting at the head of 14908 * the list which cannot be satisfied even after 14909 * multiple retries. In the future the driver may 14910 * implement some kind of maximum runout count before 14911 * failing an I/O. 14912 * 14913 * Note: the use of funcp below may seem superfluous, 14914 * but it helps warlock figure out the correct 14915 * initpkt function calls (see [s]sd.wlcmd). 14916 */ 14917 struct scsi_pkt *pktp; 14918 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14919 14920 ASSERT(bp != un->un_rqs_bp); 14921 14922 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14923 switch ((*funcp)(bp, &pktp)) { 14924 case SD_PKT_ALLOC_SUCCESS: 14925 xp->xb_pktp = pktp; 14926 SD_TRACE(SD_LOG_IO_CORE, un, 14927 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14928 pktp); 14929 goto got_pkt; 14930 14931 case SD_PKT_ALLOC_FAILURE: 14932 /* 14933 * Temporary (hopefully) resource depletion. 14934 * Since retries and RQS commands always have a 14935 * scsi_pkt allocated, these cases should never 14936 * get here. So the only cases this needs to 14937 * handle is a bp from the waitq (which we put 14938 * back onto the waitq for sdrunout), or a bp 14939 * sent as an immed_bp (which we just fail). 14940 */ 14941 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14942 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14943 14944 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14945 14946 if (bp == immed_bp) { 14947 /* 14948 * If SD_XB_DMA_FREED is clear, then 14949 * this is a failure to allocate a 14950 * scsi_pkt, and we must fail the 14951 * command. 14952 */ 14953 if ((xp->xb_pkt_flags & 14954 SD_XB_DMA_FREED) == 0) { 14955 break; 14956 } 14957 14958 /* 14959 * If this immediate command is NOT our 14960 * un_retry_bp, then we must fail it. 14961 */ 14962 if (bp != un->un_retry_bp) { 14963 break; 14964 } 14965 14966 /* 14967 * We get here if this cmd is our 14968 * un_retry_bp that was DMAFREED, but 14969 * scsi_init_pkt() failed to reallocate 14970 * DMA resources when we attempted to 14971 * retry it. This can happen when an 14972 * mpxio failover is in progress, but 14973 * we don't want to just fail the 14974 * command in this case. 14975 * 14976 * Use timeout(9F) to restart it after 14977 * a 100ms delay. We don't want to 14978 * let sdrunout() restart it, because 14979 * sdrunout() is just supposed to start 14980 * commands that are sitting on the 14981 * wait queue. The un_retry_bp stays 14982 * set until the command completes, but 14983 * sdrunout can be called many times 14984 * before that happens. Since sdrunout 14985 * cannot tell if the un_retry_bp is 14986 * already in the transport, it could 14987 * end up calling scsi_transport() for 14988 * the un_retry_bp multiple times. 14989 * 14990 * Also: don't schedule the callback 14991 * if some other callback is already 14992 * pending. 14993 */ 14994 if (un->un_retry_statp == NULL) { 14995 /* 14996 * restore the kstat pointer to 14997 * keep kstat counts coherent 14998 * when we do retry the command. 14999 */ 15000 un->un_retry_statp = 15001 saved_statp; 15002 } 15003 15004 if ((un->un_startstop_timeid == NULL) && 15005 (un->un_retry_timeid == NULL) && 15006 (un->un_direct_priority_timeid == 15007 NULL)) { 15008 15009 un->un_retry_timeid = 15010 timeout( 15011 sd_start_retry_command, 15012 un, SD_RESTART_TIMEOUT); 15013 } 15014 goto exit; 15015 } 15016 15017 #else 15018 if (bp == immed_bp) { 15019 break; /* Just fail the command */ 15020 } 15021 #endif 15022 15023 /* Add the buf back to the head of the waitq */ 15024 bp->av_forw = un->un_waitq_headp; 15025 un->un_waitq_headp = bp; 15026 if (un->un_waitq_tailp == NULL) { 15027 un->un_waitq_tailp = bp; 15028 } 15029 goto exit; 15030 15031 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15032 /* 15033 * HBA DMA resource failure. Fail the command 15034 * and continue processing of the queues. 15035 */ 15036 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15037 "sd_start_cmds: " 15038 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15039 break; 15040 15041 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15042 /* 15043 * Note:x86: Partial DMA mapping not supported 15044 * for USCSI commands, and all the needed DMA 15045 * resources were not allocated. 15046 */ 15047 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15048 "sd_start_cmds: " 15049 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15050 break; 15051 15052 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15053 /* 15054 * Note:x86: Request cannot fit into CDB based 15055 * on lba and len. 15056 */ 15057 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15058 "sd_start_cmds: " 15059 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15060 break; 15061 15062 default: 15063 /* Should NEVER get here! */ 15064 panic("scsi_initpkt error"); 15065 /*NOTREACHED*/ 15066 } 15067 15068 /* 15069 * Fatal error in allocating a scsi_pkt for this buf. 15070 * Update kstats & return the buf with an error code. 15071 * We must use sd_return_failed_command_no_restart() to 15072 * avoid a recursive call back into sd_start_cmds(). 15073 * However this also means that we must keep processing 15074 * the waitq here in order to avoid stalling. 15075 */ 15076 if (statp == kstat_waitq_to_runq) { 15077 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15078 } 15079 sd_return_failed_command_no_restart(un, bp, EIO); 15080 if (bp == immed_bp) { 15081 /* immed_bp is gone by now, so clear this */ 15082 immed_bp = NULL; 15083 } 15084 continue; 15085 } 15086 got_pkt: 15087 if (bp == immed_bp) { 15088 /* goto the head of the class.... */ 15089 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15090 } 15091 15092 un->un_ncmds_in_transport++; 15093 SD_UPDATE_KSTATS(un, statp, bp); 15094 15095 /* 15096 * Call scsi_transport() to send the command to the target. 15097 * According to SCSA architecture, we must drop the mutex here 15098 * before calling scsi_transport() in order to avoid deadlock. 15099 * Note that the scsi_pkt's completion routine can be executed 15100 * (from interrupt context) even before the call to 15101 * scsi_transport() returns. 15102 */ 15103 SD_TRACE(SD_LOG_IO_CORE, un, 15104 "sd_start_cmds: calling scsi_transport()\n"); 15105 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15106 15107 mutex_exit(SD_MUTEX(un)); 15108 rval = scsi_transport(xp->xb_pktp); 15109 mutex_enter(SD_MUTEX(un)); 15110 15111 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15112 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15113 15114 switch (rval) { 15115 case TRAN_ACCEPT: 15116 /* Clear this with every pkt accepted by the HBA */ 15117 un->un_tran_fatal_count = 0; 15118 break; /* Success; try the next cmd (if any) */ 15119 15120 case TRAN_BUSY: 15121 un->un_ncmds_in_transport--; 15122 ASSERT(un->un_ncmds_in_transport >= 0); 15123 15124 /* 15125 * Don't retry request sense, the sense data 15126 * is lost when another request is sent. 15127 * Free up the rqs buf and retry 15128 * the original failed cmd. Update kstat. 15129 */ 15130 if (bp == un->un_rqs_bp) { 15131 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15132 bp = sd_mark_rqs_idle(un, xp); 15133 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15134 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 15135 kstat_waitq_enter); 15136 goto exit; 15137 } 15138 15139 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15140 /* 15141 * Free the DMA resources for the scsi_pkt. This will 15142 * allow mpxio to select another path the next time 15143 * we call scsi_transport() with this scsi_pkt. 15144 * See sdintr() for the rationalization behind this. 15145 */ 15146 if ((un->un_f_is_fibre == TRUE) && 15147 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15148 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15149 scsi_dmafree(xp->xb_pktp); 15150 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15151 } 15152 #endif 15153 15154 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15155 /* 15156 * Commands that are SD_PATH_DIRECT_PRIORITY 15157 * are for error recovery situations. These do 15158 * not use the normal command waitq, so if they 15159 * get a TRAN_BUSY we cannot put them back onto 15160 * the waitq for later retry. One possible 15161 * problem is that there could already be some 15162 * other command on un_retry_bp that is waiting 15163 * for this one to complete, so we would be 15164 * deadlocked if we put this command back onto 15165 * the waitq for later retry (since un_retry_bp 15166 * must complete before the driver gets back to 15167 * commands on the waitq). 15168 * 15169 * To avoid deadlock we must schedule a callback 15170 * that will restart this command after a set 15171 * interval. This should keep retrying for as 15172 * long as the underlying transport keeps 15173 * returning TRAN_BUSY (just like for other 15174 * commands). Use the same timeout interval as 15175 * for the ordinary TRAN_BUSY retry. 15176 */ 15177 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15178 "sd_start_cmds: scsi_transport() returned " 15179 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15180 15181 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15182 un->un_direct_priority_timeid = 15183 timeout(sd_start_direct_priority_command, 15184 bp, SD_BSY_TIMEOUT / 500); 15185 15186 goto exit; 15187 } 15188 15189 /* 15190 * For TRAN_BUSY, we want to reduce the throttle value, 15191 * unless we are retrying a command. 15192 */ 15193 if (bp != un->un_retry_bp) { 15194 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15195 } 15196 15197 /* 15198 * Set up the bp to be tried again 10 ms later. 15199 * Note:x86: Is there a timeout value in the sd_lun 15200 * for this condition? 15201 */ 15202 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 15203 kstat_runq_back_to_waitq); 15204 goto exit; 15205 15206 case TRAN_FATAL_ERROR: 15207 un->un_tran_fatal_count++; 15208 /* FALLTHRU */ 15209 15210 case TRAN_BADPKT: 15211 default: 15212 un->un_ncmds_in_transport--; 15213 ASSERT(un->un_ncmds_in_transport >= 0); 15214 15215 /* 15216 * If this is our REQUEST SENSE command with a 15217 * transport error, we must get back the pointers 15218 * to the original buf, and mark the REQUEST 15219 * SENSE command as "available". 15220 */ 15221 if (bp == un->un_rqs_bp) { 15222 bp = sd_mark_rqs_idle(un, xp); 15223 xp = SD_GET_XBUF(bp); 15224 } else { 15225 /* 15226 * Legacy behavior: do not update transport 15227 * error count for request sense commands. 15228 */ 15229 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15230 } 15231 15232 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15233 sd_print_transport_rejected_message(un, xp, rval); 15234 15235 /* 15236 * We must use sd_return_failed_command_no_restart() to 15237 * avoid a recursive call back into sd_start_cmds(). 15238 * However this also means that we must keep processing 15239 * the waitq here in order to avoid stalling. 15240 */ 15241 sd_return_failed_command_no_restart(un, bp, EIO); 15242 15243 /* 15244 * Notify any threads waiting in sd_ddi_suspend() that 15245 * a command completion has occurred. 15246 */ 15247 if (un->un_state == SD_STATE_SUSPENDED) { 15248 cv_broadcast(&un->un_disk_busy_cv); 15249 } 15250 15251 if (bp == immed_bp) { 15252 /* immed_bp is gone by now, so clear this */ 15253 immed_bp = NULL; 15254 } 15255 break; 15256 } 15257 15258 } while (immed_bp == NULL); 15259 15260 exit: 15261 ASSERT(mutex_owned(SD_MUTEX(un))); 15262 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15263 } 15264 15265 15266 /* 15267 * Function: sd_return_command 15268 * 15269 * Description: Returns a command to its originator (with or without an 15270 * error). Also starts commands waiting to be transported 15271 * to the target. 15272 * 15273 * Context: May be called from interrupt, kernel, or timeout context 15274 */ 15275 15276 static void 15277 sd_return_command(struct sd_lun *un, struct buf *bp) 15278 { 15279 struct sd_xbuf *xp; 15280 #if defined(__i386) || defined(__amd64) 15281 struct scsi_pkt *pktp; 15282 #endif 15283 15284 ASSERT(bp != NULL); 15285 ASSERT(un != NULL); 15286 ASSERT(mutex_owned(SD_MUTEX(un))); 15287 ASSERT(bp != un->un_rqs_bp); 15288 xp = SD_GET_XBUF(bp); 15289 ASSERT(xp != NULL); 15290 15291 #if defined(__i386) || defined(__amd64) 15292 pktp = SD_GET_PKTP(bp); 15293 #endif 15294 15295 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15296 15297 #if defined(__i386) || defined(__amd64) 15298 /* 15299 * Note:x86: check for the "sdrestart failed" case. 15300 */ 15301 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15302 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15303 (xp->xb_pktp->pkt_resid == 0)) { 15304 15305 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15306 /* 15307 * Successfully set up next portion of cmd 15308 * transfer, try sending it 15309 */ 15310 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15311 NULL, NULL, 0, (clock_t)0, NULL); 15312 sd_start_cmds(un, NULL); 15313 return; /* Note:x86: need a return here? */ 15314 } 15315 } 15316 #endif 15317 15318 /* 15319 * If this is the failfast bp, clear it from un_failfast_bp. This 15320 * can happen if upon being re-tried the failfast bp either 15321 * succeeded or encountered another error (possibly even a different 15322 * error than the one that precipitated the failfast state, but in 15323 * that case it would have had to exhaust retries as well). Regardless, 15324 * this should not occur whenever the instance is in the active 15325 * failfast state. 15326 */ 15327 if (bp == un->un_failfast_bp) { 15328 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15329 un->un_failfast_bp = NULL; 15330 } 15331 15332 /* 15333 * Clear the failfast state upon successful completion of ANY cmd. 15334 */ 15335 if (bp->b_error == 0) { 15336 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15337 } 15338 15339 /* 15340 * This is used if the command was retried one or more times. Show that 15341 * we are done with it, and allow processing of the waitq to resume. 15342 */ 15343 if (bp == un->un_retry_bp) { 15344 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15345 "sd_return_command: un:0x%p: " 15346 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15347 un->un_retry_bp = NULL; 15348 un->un_retry_statp = NULL; 15349 } 15350 15351 SD_UPDATE_RDWR_STATS(un, bp); 15352 SD_UPDATE_PARTITION_STATS(un, bp); 15353 15354 switch (un->un_state) { 15355 case SD_STATE_SUSPENDED: 15356 /* 15357 * Notify any threads waiting in sd_ddi_suspend() that 15358 * a command completion has occurred. 15359 */ 15360 cv_broadcast(&un->un_disk_busy_cv); 15361 break; 15362 default: 15363 sd_start_cmds(un, NULL); 15364 break; 15365 } 15366 15367 /* Return this command up the iodone chain to its originator. */ 15368 mutex_exit(SD_MUTEX(un)); 15369 15370 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15371 xp->xb_pktp = NULL; 15372 15373 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15374 15375 ASSERT(!mutex_owned(SD_MUTEX(un))); 15376 mutex_enter(SD_MUTEX(un)); 15377 15378 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15379 } 15380 15381 15382 /* 15383 * Function: sd_return_failed_command 15384 * 15385 * Description: Command completion when an error occurred. 15386 * 15387 * Context: May be called from interrupt context 15388 */ 15389 15390 static void 15391 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15392 { 15393 ASSERT(bp != NULL); 15394 ASSERT(un != NULL); 15395 ASSERT(mutex_owned(SD_MUTEX(un))); 15396 15397 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15398 "sd_return_failed_command: entry\n"); 15399 15400 /* 15401 * b_resid could already be nonzero due to a partial data 15402 * transfer, so do not change it here. 15403 */ 15404 SD_BIOERROR(bp, errcode); 15405 15406 sd_return_command(un, bp); 15407 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15408 "sd_return_failed_command: exit\n"); 15409 } 15410 15411 15412 /* 15413 * Function: sd_return_failed_command_no_restart 15414 * 15415 * Description: Same as sd_return_failed_command, but ensures that no 15416 * call back into sd_start_cmds will be issued. 15417 * 15418 * Context: May be called from interrupt context 15419 */ 15420 15421 static void 15422 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15423 int errcode) 15424 { 15425 struct sd_xbuf *xp; 15426 15427 ASSERT(bp != NULL); 15428 ASSERT(un != NULL); 15429 ASSERT(mutex_owned(SD_MUTEX(un))); 15430 xp = SD_GET_XBUF(bp); 15431 ASSERT(xp != NULL); 15432 ASSERT(errcode != 0); 15433 15434 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15435 "sd_return_failed_command_no_restart: entry\n"); 15436 15437 /* 15438 * b_resid could already be nonzero due to a partial data 15439 * transfer, so do not change it here. 15440 */ 15441 SD_BIOERROR(bp, errcode); 15442 15443 /* 15444 * If this is the failfast bp, clear it. This can happen if the 15445 * failfast bp encounterd a fatal error when we attempted to 15446 * re-try it (such as a scsi_transport(9F) failure). However 15447 * we should NOT be in an active failfast state if the failfast 15448 * bp is not NULL. 15449 */ 15450 if (bp == un->un_failfast_bp) { 15451 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15452 un->un_failfast_bp = NULL; 15453 } 15454 15455 if (bp == un->un_retry_bp) { 15456 /* 15457 * This command was retried one or more times. Show that we are 15458 * done with it, and allow processing of the waitq to resume. 15459 */ 15460 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15461 "sd_return_failed_command_no_restart: " 15462 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15463 un->un_retry_bp = NULL; 15464 un->un_retry_statp = NULL; 15465 } 15466 15467 SD_UPDATE_RDWR_STATS(un, bp); 15468 SD_UPDATE_PARTITION_STATS(un, bp); 15469 15470 mutex_exit(SD_MUTEX(un)); 15471 15472 if (xp->xb_pktp != NULL) { 15473 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15474 xp->xb_pktp = NULL; 15475 } 15476 15477 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15478 15479 mutex_enter(SD_MUTEX(un)); 15480 15481 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15482 "sd_return_failed_command_no_restart: exit\n"); 15483 } 15484 15485 15486 /* 15487 * Function: sd_retry_command 15488 * 15489 * Description: queue up a command for retry, or (optionally) fail it 15490 * if retry counts are exhausted. 15491 * 15492 * Arguments: un - Pointer to the sd_lun struct for the target. 15493 * 15494 * bp - Pointer to the buf for the command to be retried. 15495 * 15496 * retry_check_flag - Flag to see which (if any) of the retry 15497 * counts should be decremented/checked. If the indicated 15498 * retry count is exhausted, then the command will not be 15499 * retried; it will be failed instead. This should use a 15500 * value equal to one of the following: 15501 * 15502 * SD_RETRIES_NOCHECK 15503 * SD_RESD_RETRIES_STANDARD 15504 * SD_RETRIES_VICTIM 15505 * 15506 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15507 * if the check should be made to see of FLAG_ISOLATE is set 15508 * in the pkt. If FLAG_ISOLATE is set, then the command is 15509 * not retried, it is simply failed. 15510 * 15511 * user_funcp - Ptr to function to call before dispatching the 15512 * command. May be NULL if no action needs to be performed. 15513 * (Primarily intended for printing messages.) 15514 * 15515 * user_arg - Optional argument to be passed along to 15516 * the user_funcp call. 15517 * 15518 * failure_code - errno return code to set in the bp if the 15519 * command is going to be failed. 15520 * 15521 * retry_delay - Retry delay interval in (clock_t) units. May 15522 * be zero which indicates that the retry should be retried 15523 * immediately (ie, without an intervening delay). 15524 * 15525 * statp - Ptr to kstat function to be updated if the command 15526 * is queued for a delayed retry. May be NULL if no kstat 15527 * update is desired. 15528 * 15529 * Context: May be called from interupt context. 15530 */ 15531 15532 static void 15533 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15534 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15535 code), void *user_arg, int failure_code, clock_t retry_delay, 15536 void (*statp)(kstat_io_t *)) 15537 { 15538 struct sd_xbuf *xp; 15539 struct scsi_pkt *pktp; 15540 15541 ASSERT(un != NULL); 15542 ASSERT(mutex_owned(SD_MUTEX(un))); 15543 ASSERT(bp != NULL); 15544 xp = SD_GET_XBUF(bp); 15545 ASSERT(xp != NULL); 15546 pktp = SD_GET_PKTP(bp); 15547 ASSERT(pktp != NULL); 15548 15549 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15550 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15551 15552 /* 15553 * If we are syncing or dumping, fail the command to avoid 15554 * recursively calling back into scsi_transport(). 15555 */ 15556 if (ddi_in_panic()) { 15557 goto fail_command_no_log; 15558 } 15559 15560 /* 15561 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15562 * log an error and fail the command. 15563 */ 15564 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15565 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15566 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15567 sd_dump_memory(un, SD_LOG_IO, "CDB", 15568 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15569 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15570 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15571 goto fail_command; 15572 } 15573 15574 /* 15575 * If we are suspended, then put the command onto head of the 15576 * wait queue since we don't want to start more commands. 15577 */ 15578 switch (un->un_state) { 15579 case SD_STATE_SUSPENDED: 15580 case SD_STATE_DUMPING: 15581 bp->av_forw = un->un_waitq_headp; 15582 un->un_waitq_headp = bp; 15583 if (un->un_waitq_tailp == NULL) { 15584 un->un_waitq_tailp = bp; 15585 } 15586 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15587 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15588 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15589 return; 15590 default: 15591 break; 15592 } 15593 15594 /* 15595 * If the caller wants us to check FLAG_ISOLATE, then see if that 15596 * is set; if it is then we do not want to retry the command. 15597 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15598 */ 15599 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15600 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15601 goto fail_command; 15602 } 15603 } 15604 15605 15606 /* 15607 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15608 * command timeout or a selection timeout has occurred. This means 15609 * that we were unable to establish an kind of communication with 15610 * the target, and subsequent retries and/or commands are likely 15611 * to encounter similar results and take a long time to complete. 15612 * 15613 * If this is a failfast error condition, we need to update the 15614 * failfast state, even if this bp does not have B_FAILFAST set. 15615 */ 15616 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15617 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15618 ASSERT(un->un_failfast_bp == NULL); 15619 /* 15620 * If we are already in the active failfast state, and 15621 * another failfast error condition has been detected, 15622 * then fail this command if it has B_FAILFAST set. 15623 * If B_FAILFAST is clear, then maintain the legacy 15624 * behavior of retrying heroically, even tho this will 15625 * take a lot more time to fail the command. 15626 */ 15627 if (bp->b_flags & B_FAILFAST) { 15628 goto fail_command; 15629 } 15630 } else { 15631 /* 15632 * We're not in the active failfast state, but we 15633 * have a failfast error condition, so we must begin 15634 * transition to the next state. We do this regardless 15635 * of whether or not this bp has B_FAILFAST set. 15636 */ 15637 if (un->un_failfast_bp == NULL) { 15638 /* 15639 * This is the first bp to meet a failfast 15640 * condition so save it on un_failfast_bp & 15641 * do normal retry processing. Do not enter 15642 * active failfast state yet. This marks 15643 * entry into the "failfast pending" state. 15644 */ 15645 un->un_failfast_bp = bp; 15646 15647 } else if (un->un_failfast_bp == bp) { 15648 /* 15649 * This is the second time *this* bp has 15650 * encountered a failfast error condition, 15651 * so enter active failfast state & flush 15652 * queues as appropriate. 15653 */ 15654 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15655 un->un_failfast_bp = NULL; 15656 sd_failfast_flushq(un); 15657 15658 /* 15659 * Fail this bp now if B_FAILFAST set; 15660 * otherwise continue with retries. (It would 15661 * be pretty ironic if this bp succeeded on a 15662 * subsequent retry after we just flushed all 15663 * the queues). 15664 */ 15665 if (bp->b_flags & B_FAILFAST) { 15666 goto fail_command; 15667 } 15668 15669 #if !defined(lint) && !defined(__lint) 15670 } else { 15671 /* 15672 * If neither of the preceeding conditionals 15673 * was true, it means that there is some 15674 * *other* bp that has met an inital failfast 15675 * condition and is currently either being 15676 * retried or is waiting to be retried. In 15677 * that case we should perform normal retry 15678 * processing on *this* bp, since there is a 15679 * chance that the current failfast condition 15680 * is transient and recoverable. If that does 15681 * not turn out to be the case, then retries 15682 * will be cleared when the wait queue is 15683 * flushed anyway. 15684 */ 15685 #endif 15686 } 15687 } 15688 } else { 15689 /* 15690 * SD_RETRIES_FAILFAST is clear, which indicates that we 15691 * likely were able to at least establish some level of 15692 * communication with the target and subsequent commands 15693 * and/or retries are likely to get through to the target, 15694 * In this case we want to be aggressive about clearing 15695 * the failfast state. Note that this does not affect 15696 * the "failfast pending" condition. 15697 */ 15698 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15699 } 15700 15701 15702 /* 15703 * Check the specified retry count to see if we can still do 15704 * any retries with this pkt before we should fail it. 15705 */ 15706 switch (retry_check_flag & SD_RETRIES_MASK) { 15707 case SD_RETRIES_VICTIM: 15708 /* 15709 * Check the victim retry count. If exhausted, then fall 15710 * thru & check against the standard retry count. 15711 */ 15712 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15713 /* Increment count & proceed with the retry */ 15714 xp->xb_victim_retry_count++; 15715 break; 15716 } 15717 /* Victim retries exhausted, fall back to std. retries... */ 15718 /* FALLTHRU */ 15719 15720 case SD_RETRIES_STANDARD: 15721 if (xp->xb_retry_count >= un->un_retry_count) { 15722 /* Retries exhausted, fail the command */ 15723 SD_TRACE(SD_LOG_IO_CORE, un, 15724 "sd_retry_command: retries exhausted!\n"); 15725 /* 15726 * update b_resid for failed SCMD_READ & SCMD_WRITE 15727 * commands with nonzero pkt_resid. 15728 */ 15729 if ((pktp->pkt_reason == CMD_CMPLT) && 15730 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15731 (pktp->pkt_resid != 0)) { 15732 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15733 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15734 SD_UPDATE_B_RESID(bp, pktp); 15735 } 15736 } 15737 goto fail_command; 15738 } 15739 xp->xb_retry_count++; 15740 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15741 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15742 break; 15743 15744 case SD_RETRIES_UA: 15745 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15746 /* Retries exhausted, fail the command */ 15747 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15748 "Unit Attention retries exhausted. " 15749 "Check the target.\n"); 15750 goto fail_command; 15751 } 15752 xp->xb_ua_retry_count++; 15753 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15754 "sd_retry_command: retry count:%d\n", 15755 xp->xb_ua_retry_count); 15756 break; 15757 15758 case SD_RETRIES_BUSY: 15759 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15760 /* Retries exhausted, fail the command */ 15761 SD_TRACE(SD_LOG_IO_CORE, un, 15762 "sd_retry_command: retries exhausted!\n"); 15763 goto fail_command; 15764 } 15765 xp->xb_retry_count++; 15766 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15767 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15768 break; 15769 15770 case SD_RETRIES_NOCHECK: 15771 default: 15772 /* No retry count to check. Just proceed with the retry */ 15773 break; 15774 } 15775 15776 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15777 15778 /* 15779 * If we were given a zero timeout, we must attempt to retry the 15780 * command immediately (ie, without a delay). 15781 */ 15782 if (retry_delay == 0) { 15783 /* 15784 * Check some limiting conditions to see if we can actually 15785 * do the immediate retry. If we cannot, then we must 15786 * fall back to queueing up a delayed retry. 15787 */ 15788 if (un->un_ncmds_in_transport >= un->un_throttle) { 15789 /* 15790 * We are at the throttle limit for the target, 15791 * fall back to delayed retry. 15792 */ 15793 retry_delay = SD_BSY_TIMEOUT; 15794 statp = kstat_waitq_enter; 15795 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15796 "sd_retry_command: immed. retry hit " 15797 "throttle!\n"); 15798 } else { 15799 /* 15800 * We're clear to proceed with the immediate retry. 15801 * First call the user-provided function (if any) 15802 */ 15803 if (user_funcp != NULL) { 15804 (*user_funcp)(un, bp, user_arg, 15805 SD_IMMEDIATE_RETRY_ISSUED); 15806 #ifdef __lock_lint 15807 sd_print_incomplete_msg(un, bp, user_arg, 15808 SD_IMMEDIATE_RETRY_ISSUED); 15809 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15810 SD_IMMEDIATE_RETRY_ISSUED); 15811 sd_print_sense_failed_msg(un, bp, user_arg, 15812 SD_IMMEDIATE_RETRY_ISSUED); 15813 #endif 15814 } 15815 15816 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15817 "sd_retry_command: issuing immediate retry\n"); 15818 15819 /* 15820 * Call sd_start_cmds() to transport the command to 15821 * the target. 15822 */ 15823 sd_start_cmds(un, bp); 15824 15825 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15826 "sd_retry_command exit\n"); 15827 return; 15828 } 15829 } 15830 15831 /* 15832 * Set up to retry the command after a delay. 15833 * First call the user-provided function (if any) 15834 */ 15835 if (user_funcp != NULL) { 15836 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15837 } 15838 15839 sd_set_retry_bp(un, bp, retry_delay, statp); 15840 15841 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15842 return; 15843 15844 fail_command: 15845 15846 if (user_funcp != NULL) { 15847 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15848 } 15849 15850 fail_command_no_log: 15851 15852 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15853 "sd_retry_command: returning failed command\n"); 15854 15855 sd_return_failed_command(un, bp, failure_code); 15856 15857 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15858 } 15859 15860 15861 /* 15862 * Function: sd_set_retry_bp 15863 * 15864 * Description: Set up the given bp for retry. 15865 * 15866 * Arguments: un - ptr to associated softstate 15867 * bp - ptr to buf(9S) for the command 15868 * retry_delay - time interval before issuing retry (may be 0) 15869 * statp - optional pointer to kstat function 15870 * 15871 * Context: May be called under interrupt context 15872 */ 15873 15874 static void 15875 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15876 void (*statp)(kstat_io_t *)) 15877 { 15878 ASSERT(un != NULL); 15879 ASSERT(mutex_owned(SD_MUTEX(un))); 15880 ASSERT(bp != NULL); 15881 15882 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15883 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15884 15885 /* 15886 * Indicate that the command is being retried. This will not allow any 15887 * other commands on the wait queue to be transported to the target 15888 * until this command has been completed (success or failure). The 15889 * "retry command" is not transported to the target until the given 15890 * time delay expires, unless the user specified a 0 retry_delay. 15891 * 15892 * Note: the timeout(9F) callback routine is what actually calls 15893 * sd_start_cmds() to transport the command, with the exception of a 15894 * zero retry_delay. The only current implementor of a zero retry delay 15895 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15896 */ 15897 if (un->un_retry_bp == NULL) { 15898 ASSERT(un->un_retry_statp == NULL); 15899 un->un_retry_bp = bp; 15900 15901 /* 15902 * If the user has not specified a delay the command should 15903 * be queued and no timeout should be scheduled. 15904 */ 15905 if (retry_delay == 0) { 15906 /* 15907 * Save the kstat pointer that will be used in the 15908 * call to SD_UPDATE_KSTATS() below, so that 15909 * sd_start_cmds() can correctly decrement the waitq 15910 * count when it is time to transport this command. 15911 */ 15912 un->un_retry_statp = statp; 15913 goto done; 15914 } 15915 } 15916 15917 if (un->un_retry_bp == bp) { 15918 /* 15919 * Save the kstat pointer that will be used in the call to 15920 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15921 * correctly decrement the waitq count when it is time to 15922 * transport this command. 15923 */ 15924 un->un_retry_statp = statp; 15925 15926 /* 15927 * Schedule a timeout if: 15928 * 1) The user has specified a delay. 15929 * 2) There is not a START_STOP_UNIT callback pending. 15930 * 15931 * If no delay has been specified, then it is up to the caller 15932 * to ensure that IO processing continues without stalling. 15933 * Effectively, this means that the caller will issue the 15934 * required call to sd_start_cmds(). The START_STOP_UNIT 15935 * callback does this after the START STOP UNIT command has 15936 * completed. In either of these cases we should not schedule 15937 * a timeout callback here. Also don't schedule the timeout if 15938 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15939 */ 15940 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15941 (un->un_direct_priority_timeid == NULL)) { 15942 un->un_retry_timeid = 15943 timeout(sd_start_retry_command, un, retry_delay); 15944 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15945 "sd_set_retry_bp: setting timeout: un: 0x%p" 15946 " bp:0x%p un_retry_timeid:0x%p\n", 15947 un, bp, un->un_retry_timeid); 15948 } 15949 } else { 15950 /* 15951 * We only get in here if there is already another command 15952 * waiting to be retried. In this case, we just put the 15953 * given command onto the wait queue, so it can be transported 15954 * after the current retry command has completed. 15955 * 15956 * Also we have to make sure that if the command at the head 15957 * of the wait queue is the un_failfast_bp, that we do not 15958 * put ahead of it any other commands that are to be retried. 15959 */ 15960 if ((un->un_failfast_bp != NULL) && 15961 (un->un_failfast_bp == un->un_waitq_headp)) { 15962 /* 15963 * Enqueue this command AFTER the first command on 15964 * the wait queue (which is also un_failfast_bp). 15965 */ 15966 bp->av_forw = un->un_waitq_headp->av_forw; 15967 un->un_waitq_headp->av_forw = bp; 15968 if (un->un_waitq_headp == un->un_waitq_tailp) { 15969 un->un_waitq_tailp = bp; 15970 } 15971 } else { 15972 /* Enqueue this command at the head of the waitq. */ 15973 bp->av_forw = un->un_waitq_headp; 15974 un->un_waitq_headp = bp; 15975 if (un->un_waitq_tailp == NULL) { 15976 un->un_waitq_tailp = bp; 15977 } 15978 } 15979 15980 if (statp == NULL) { 15981 statp = kstat_waitq_enter; 15982 } 15983 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15984 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15985 } 15986 15987 done: 15988 if (statp != NULL) { 15989 SD_UPDATE_KSTATS(un, statp, bp); 15990 } 15991 15992 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15993 "sd_set_retry_bp: exit un:0x%p\n", un); 15994 } 15995 15996 15997 /* 15998 * Function: sd_start_retry_command 15999 * 16000 * Description: Start the command that has been waiting on the target's 16001 * retry queue. Called from timeout(9F) context after the 16002 * retry delay interval has expired. 16003 * 16004 * Arguments: arg - pointer to associated softstate for the device. 16005 * 16006 * Context: timeout(9F) thread context. May not sleep. 16007 */ 16008 16009 static void 16010 sd_start_retry_command(void *arg) 16011 { 16012 struct sd_lun *un = arg; 16013 16014 ASSERT(un != NULL); 16015 ASSERT(!mutex_owned(SD_MUTEX(un))); 16016 16017 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16018 "sd_start_retry_command: entry\n"); 16019 16020 mutex_enter(SD_MUTEX(un)); 16021 16022 un->un_retry_timeid = NULL; 16023 16024 if (un->un_retry_bp != NULL) { 16025 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16026 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16027 un, un->un_retry_bp); 16028 sd_start_cmds(un, un->un_retry_bp); 16029 } 16030 16031 mutex_exit(SD_MUTEX(un)); 16032 16033 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16034 "sd_start_retry_command: exit\n"); 16035 } 16036 16037 16038 /* 16039 * Function: sd_start_direct_priority_command 16040 * 16041 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16042 * received TRAN_BUSY when we called scsi_transport() to send it 16043 * to the underlying HBA. This function is called from timeout(9F) 16044 * context after the delay interval has expired. 16045 * 16046 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16047 * 16048 * Context: timeout(9F) thread context. May not sleep. 16049 */ 16050 16051 static void 16052 sd_start_direct_priority_command(void *arg) 16053 { 16054 struct buf *priority_bp = arg; 16055 struct sd_lun *un; 16056 16057 ASSERT(priority_bp != NULL); 16058 un = SD_GET_UN(priority_bp); 16059 ASSERT(un != NULL); 16060 ASSERT(!mutex_owned(SD_MUTEX(un))); 16061 16062 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16063 "sd_start_direct_priority_command: entry\n"); 16064 16065 mutex_enter(SD_MUTEX(un)); 16066 un->un_direct_priority_timeid = NULL; 16067 sd_start_cmds(un, priority_bp); 16068 mutex_exit(SD_MUTEX(un)); 16069 16070 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16071 "sd_start_direct_priority_command: exit\n"); 16072 } 16073 16074 16075 /* 16076 * Function: sd_send_request_sense_command 16077 * 16078 * Description: Sends a REQUEST SENSE command to the target 16079 * 16080 * Context: May be called from interrupt context. 16081 */ 16082 16083 static void 16084 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16085 struct scsi_pkt *pktp) 16086 { 16087 ASSERT(bp != NULL); 16088 ASSERT(un != NULL); 16089 ASSERT(mutex_owned(SD_MUTEX(un))); 16090 16091 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16092 "entry: buf:0x%p\n", bp); 16093 16094 /* 16095 * If we are syncing or dumping, then fail the command to avoid a 16096 * recursive callback into scsi_transport(). Also fail the command 16097 * if we are suspended (legacy behavior). 16098 */ 16099 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16100 (un->un_state == SD_STATE_DUMPING)) { 16101 sd_return_failed_command(un, bp, EIO); 16102 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16103 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16104 return; 16105 } 16106 16107 /* 16108 * Retry the failed command and don't issue the request sense if: 16109 * 1) the sense buf is busy 16110 * 2) we have 1 or more outstanding commands on the target 16111 * (the sense data will be cleared or invalidated any way) 16112 * 16113 * Note: There could be an issue with not checking a retry limit here, 16114 * the problem is determining which retry limit to check. 16115 */ 16116 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16117 /* Don't retry if the command is flagged as non-retryable */ 16118 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16119 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16120 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 16121 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16122 "sd_send_request_sense_command: " 16123 "at full throttle, retrying exit\n"); 16124 } else { 16125 sd_return_failed_command(un, bp, EIO); 16126 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16127 "sd_send_request_sense_command: " 16128 "at full throttle, non-retryable exit\n"); 16129 } 16130 return; 16131 } 16132 16133 sd_mark_rqs_busy(un, bp); 16134 sd_start_cmds(un, un->un_rqs_bp); 16135 16136 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16137 "sd_send_request_sense_command: exit\n"); 16138 } 16139 16140 16141 /* 16142 * Function: sd_mark_rqs_busy 16143 * 16144 * Description: Indicate that the request sense bp for this instance is 16145 * in use. 16146 * 16147 * Context: May be called under interrupt context 16148 */ 16149 16150 static void 16151 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16152 { 16153 struct sd_xbuf *sense_xp; 16154 16155 ASSERT(un != NULL); 16156 ASSERT(bp != NULL); 16157 ASSERT(mutex_owned(SD_MUTEX(un))); 16158 ASSERT(un->un_sense_isbusy == 0); 16159 16160 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16161 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16162 16163 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16164 ASSERT(sense_xp != NULL); 16165 16166 SD_INFO(SD_LOG_IO, un, 16167 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16168 16169 ASSERT(sense_xp->xb_pktp != NULL); 16170 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16171 == (FLAG_SENSING | FLAG_HEAD)); 16172 16173 un->un_sense_isbusy = 1; 16174 un->un_rqs_bp->b_resid = 0; 16175 sense_xp->xb_pktp->pkt_resid = 0; 16176 sense_xp->xb_pktp->pkt_reason = 0; 16177 16178 /* So we can get back the bp at interrupt time! */ 16179 sense_xp->xb_sense_bp = bp; 16180 16181 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16182 16183 /* 16184 * Mark this buf as awaiting sense data. (This is already set in 16185 * the pkt_flags for the RQS packet.) 16186 */ 16187 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16188 16189 sense_xp->xb_retry_count = 0; 16190 sense_xp->xb_victim_retry_count = 0; 16191 sense_xp->xb_ua_retry_count = 0; 16192 sense_xp->xb_dma_resid = 0; 16193 16194 /* Clean up the fields for auto-request sense */ 16195 sense_xp->xb_sense_status = 0; 16196 sense_xp->xb_sense_state = 0; 16197 sense_xp->xb_sense_resid = 0; 16198 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16199 16200 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16201 } 16202 16203 16204 /* 16205 * Function: sd_mark_rqs_idle 16206 * 16207 * Description: SD_MUTEX must be held continuously through this routine 16208 * to prevent reuse of the rqs struct before the caller can 16209 * complete it's processing. 16210 * 16211 * Return Code: Pointer to the RQS buf 16212 * 16213 * Context: May be called under interrupt context 16214 */ 16215 16216 static struct buf * 16217 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16218 { 16219 struct buf *bp; 16220 ASSERT(un != NULL); 16221 ASSERT(sense_xp != NULL); 16222 ASSERT(mutex_owned(SD_MUTEX(un))); 16223 ASSERT(un->un_sense_isbusy != 0); 16224 16225 un->un_sense_isbusy = 0; 16226 bp = sense_xp->xb_sense_bp; 16227 sense_xp->xb_sense_bp = NULL; 16228 16229 /* This pkt is no longer interested in getting sense data */ 16230 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16231 16232 return (bp); 16233 } 16234 16235 16236 16237 /* 16238 * Function: sd_alloc_rqs 16239 * 16240 * Description: Set up the unit to receive auto request sense data 16241 * 16242 * Return Code: DDI_SUCCESS or DDI_FAILURE 16243 * 16244 * Context: Called under attach(9E) context 16245 */ 16246 16247 static int 16248 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16249 { 16250 struct sd_xbuf *xp; 16251 16252 ASSERT(un != NULL); 16253 ASSERT(!mutex_owned(SD_MUTEX(un))); 16254 ASSERT(un->un_rqs_bp == NULL); 16255 ASSERT(un->un_rqs_pktp == NULL); 16256 16257 /* 16258 * First allocate the required buf and scsi_pkt structs, then set up 16259 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16260 */ 16261 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16262 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16263 if (un->un_rqs_bp == NULL) { 16264 return (DDI_FAILURE); 16265 } 16266 16267 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16268 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16269 16270 if (un->un_rqs_pktp == NULL) { 16271 sd_free_rqs(un); 16272 return (DDI_FAILURE); 16273 } 16274 16275 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16276 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16277 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 16278 16279 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16280 16281 /* Set up the other needed members in the ARQ scsi_pkt. */ 16282 un->un_rqs_pktp->pkt_comp = sdintr; 16283 un->un_rqs_pktp->pkt_time = sd_io_time; 16284 un->un_rqs_pktp->pkt_flags |= 16285 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16286 16287 /* 16288 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16289 * provide any intpkt, destroypkt routines as we take care of 16290 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16291 */ 16292 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16293 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16294 xp->xb_pktp = un->un_rqs_pktp; 16295 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16296 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16297 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16298 16299 /* 16300 * Save the pointer to the request sense private bp so it can 16301 * be retrieved in sdintr. 16302 */ 16303 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16304 ASSERT(un->un_rqs_bp->b_private == xp); 16305 16306 /* 16307 * See if the HBA supports auto-request sense for the specified 16308 * target/lun. If it does, then try to enable it (if not already 16309 * enabled). 16310 * 16311 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16312 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16313 * return success. However, in both of these cases ARQ is always 16314 * enabled and scsi_ifgetcap will always return true. The best approach 16315 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16316 * 16317 * The 3rd case is the HBA (adp) always return enabled on 16318 * scsi_ifgetgetcap even when it's not enable, the best approach 16319 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16320 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16321 */ 16322 16323 if (un->un_f_is_fibre == TRUE) { 16324 un->un_f_arq_enabled = TRUE; 16325 } else { 16326 #if defined(__i386) || defined(__amd64) 16327 /* 16328 * Circumvent the Adaptec bug, remove this code when 16329 * the bug is fixed 16330 */ 16331 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16332 #endif 16333 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16334 case 0: 16335 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16336 "sd_alloc_rqs: HBA supports ARQ\n"); 16337 /* 16338 * ARQ is supported by this HBA but currently is not 16339 * enabled. Attempt to enable it and if successful then 16340 * mark this instance as ARQ enabled. 16341 */ 16342 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16343 == 1) { 16344 /* Successfully enabled ARQ in the HBA */ 16345 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16346 "sd_alloc_rqs: ARQ enabled\n"); 16347 un->un_f_arq_enabled = TRUE; 16348 } else { 16349 /* Could not enable ARQ in the HBA */ 16350 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16351 "sd_alloc_rqs: failed ARQ enable\n"); 16352 un->un_f_arq_enabled = FALSE; 16353 } 16354 break; 16355 case 1: 16356 /* 16357 * ARQ is supported by this HBA and is already enabled. 16358 * Just mark ARQ as enabled for this instance. 16359 */ 16360 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16361 "sd_alloc_rqs: ARQ already enabled\n"); 16362 un->un_f_arq_enabled = TRUE; 16363 break; 16364 default: 16365 /* 16366 * ARQ is not supported by this HBA; disable it for this 16367 * instance. 16368 */ 16369 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16370 "sd_alloc_rqs: HBA does not support ARQ\n"); 16371 un->un_f_arq_enabled = FALSE; 16372 break; 16373 } 16374 } 16375 16376 return (DDI_SUCCESS); 16377 } 16378 16379 16380 /* 16381 * Function: sd_free_rqs 16382 * 16383 * Description: Cleanup for the pre-instance RQS command. 16384 * 16385 * Context: Kernel thread context 16386 */ 16387 16388 static void 16389 sd_free_rqs(struct sd_lun *un) 16390 { 16391 ASSERT(un != NULL); 16392 16393 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16394 16395 /* 16396 * If consistent memory is bound to a scsi_pkt, the pkt 16397 * has to be destroyed *before* freeing the consistent memory. 16398 * Don't change the sequence of this operations. 16399 * scsi_destroy_pkt() might access memory, which isn't allowed, 16400 * after it was freed in scsi_free_consistent_buf(). 16401 */ 16402 if (un->un_rqs_pktp != NULL) { 16403 scsi_destroy_pkt(un->un_rqs_pktp); 16404 un->un_rqs_pktp = NULL; 16405 } 16406 16407 if (un->un_rqs_bp != NULL) { 16408 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 16409 scsi_free_consistent_buf(un->un_rqs_bp); 16410 un->un_rqs_bp = NULL; 16411 } 16412 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16413 } 16414 16415 16416 16417 /* 16418 * Function: sd_reduce_throttle 16419 * 16420 * Description: Reduces the maximun # of outstanding commands on a 16421 * target to the current number of outstanding commands. 16422 * Queues a tiemout(9F) callback to restore the limit 16423 * after a specified interval has elapsed. 16424 * Typically used when we get a TRAN_BUSY return code 16425 * back from scsi_transport(). 16426 * 16427 * Arguments: un - ptr to the sd_lun softstate struct 16428 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16429 * 16430 * Context: May be called from interrupt context 16431 */ 16432 16433 static void 16434 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16435 { 16436 ASSERT(un != NULL); 16437 ASSERT(mutex_owned(SD_MUTEX(un))); 16438 ASSERT(un->un_ncmds_in_transport >= 0); 16439 16440 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16441 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16442 un, un->un_throttle, un->un_ncmds_in_transport); 16443 16444 if (un->un_throttle > 1) { 16445 if (un->un_f_use_adaptive_throttle == TRUE) { 16446 switch (throttle_type) { 16447 case SD_THROTTLE_TRAN_BUSY: 16448 if (un->un_busy_throttle == 0) { 16449 un->un_busy_throttle = un->un_throttle; 16450 } 16451 break; 16452 case SD_THROTTLE_QFULL: 16453 un->un_busy_throttle = 0; 16454 break; 16455 default: 16456 ASSERT(FALSE); 16457 } 16458 16459 if (un->un_ncmds_in_transport > 0) { 16460 un->un_throttle = un->un_ncmds_in_transport; 16461 } 16462 16463 } else { 16464 if (un->un_ncmds_in_transport == 0) { 16465 un->un_throttle = 1; 16466 } else { 16467 un->un_throttle = un->un_ncmds_in_transport; 16468 } 16469 } 16470 } 16471 16472 /* Reschedule the timeout if none is currently active */ 16473 if (un->un_reset_throttle_timeid == NULL) { 16474 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16475 un, SD_THROTTLE_RESET_INTERVAL); 16476 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16477 "sd_reduce_throttle: timeout scheduled!\n"); 16478 } 16479 16480 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16481 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16482 } 16483 16484 16485 16486 /* 16487 * Function: sd_restore_throttle 16488 * 16489 * Description: Callback function for timeout(9F). Resets the current 16490 * value of un->un_throttle to its default. 16491 * 16492 * Arguments: arg - pointer to associated softstate for the device. 16493 * 16494 * Context: May be called from interrupt context 16495 */ 16496 16497 static void 16498 sd_restore_throttle(void *arg) 16499 { 16500 struct sd_lun *un = arg; 16501 16502 ASSERT(un != NULL); 16503 ASSERT(!mutex_owned(SD_MUTEX(un))); 16504 16505 mutex_enter(SD_MUTEX(un)); 16506 16507 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16508 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16509 16510 un->un_reset_throttle_timeid = NULL; 16511 16512 if (un->un_f_use_adaptive_throttle == TRUE) { 16513 /* 16514 * If un_busy_throttle is nonzero, then it contains the 16515 * value that un_throttle was when we got a TRAN_BUSY back 16516 * from scsi_transport(). We want to revert back to this 16517 * value. 16518 * 16519 * In the QFULL case, the throttle limit will incrementally 16520 * increase until it reaches max throttle. 16521 */ 16522 if (un->un_busy_throttle > 0) { 16523 un->un_throttle = un->un_busy_throttle; 16524 un->un_busy_throttle = 0; 16525 } else { 16526 /* 16527 * increase throttle by 10% open gate slowly, schedule 16528 * another restore if saved throttle has not been 16529 * reached 16530 */ 16531 short throttle; 16532 if (sd_qfull_throttle_enable) { 16533 throttle = un->un_throttle + 16534 max((un->un_throttle / 10), 1); 16535 un->un_throttle = 16536 (throttle < un->un_saved_throttle) ? 16537 throttle : un->un_saved_throttle; 16538 if (un->un_throttle < un->un_saved_throttle) { 16539 un->un_reset_throttle_timeid = 16540 timeout(sd_restore_throttle, 16541 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16542 } 16543 } 16544 } 16545 16546 /* 16547 * If un_throttle has fallen below the low-water mark, we 16548 * restore the maximum value here (and allow it to ratchet 16549 * down again if necessary). 16550 */ 16551 if (un->un_throttle < un->un_min_throttle) { 16552 un->un_throttle = un->un_saved_throttle; 16553 } 16554 } else { 16555 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16556 "restoring limit from 0x%x to 0x%x\n", 16557 un->un_throttle, un->un_saved_throttle); 16558 un->un_throttle = un->un_saved_throttle; 16559 } 16560 16561 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16562 "sd_restore_throttle: calling sd_start_cmds!\n"); 16563 16564 sd_start_cmds(un, NULL); 16565 16566 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16567 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16568 un, un->un_throttle); 16569 16570 mutex_exit(SD_MUTEX(un)); 16571 16572 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16573 } 16574 16575 /* 16576 * Function: sdrunout 16577 * 16578 * Description: Callback routine for scsi_init_pkt when a resource allocation 16579 * fails. 16580 * 16581 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16582 * soft state instance. 16583 * 16584 * Return Code: The scsi_init_pkt routine allows for the callback function to 16585 * return a 0 indicating the callback should be rescheduled or a 1 16586 * indicating not to reschedule. This routine always returns 1 16587 * because the driver always provides a callback function to 16588 * scsi_init_pkt. This results in a callback always being scheduled 16589 * (via the scsi_init_pkt callback implementation) if a resource 16590 * failure occurs. 16591 * 16592 * Context: This callback function may not block or call routines that block 16593 * 16594 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16595 * request persisting at the head of the list which cannot be 16596 * satisfied even after multiple retries. In the future the driver 16597 * may implement some time of maximum runout count before failing 16598 * an I/O. 16599 */ 16600 16601 static int 16602 sdrunout(caddr_t arg) 16603 { 16604 struct sd_lun *un = (struct sd_lun *)arg; 16605 16606 ASSERT(un != NULL); 16607 ASSERT(!mutex_owned(SD_MUTEX(un))); 16608 16609 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16610 16611 mutex_enter(SD_MUTEX(un)); 16612 sd_start_cmds(un, NULL); 16613 mutex_exit(SD_MUTEX(un)); 16614 /* 16615 * This callback routine always returns 1 (i.e. do not reschedule) 16616 * because we always specify sdrunout as the callback handler for 16617 * scsi_init_pkt inside the call to sd_start_cmds. 16618 */ 16619 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16620 return (1); 16621 } 16622 16623 16624 /* 16625 * Function: sdintr 16626 * 16627 * Description: Completion callback routine for scsi_pkt(9S) structs 16628 * sent to the HBA driver via scsi_transport(9F). 16629 * 16630 * Context: Interrupt context 16631 */ 16632 16633 static void 16634 sdintr(struct scsi_pkt *pktp) 16635 { 16636 struct buf *bp; 16637 struct sd_xbuf *xp; 16638 struct sd_lun *un; 16639 16640 ASSERT(pktp != NULL); 16641 bp = (struct buf *)pktp->pkt_private; 16642 ASSERT(bp != NULL); 16643 xp = SD_GET_XBUF(bp); 16644 ASSERT(xp != NULL); 16645 ASSERT(xp->xb_pktp != NULL); 16646 un = SD_GET_UN(bp); 16647 ASSERT(un != NULL); 16648 ASSERT(!mutex_owned(SD_MUTEX(un))); 16649 16650 #ifdef SD_FAULT_INJECTION 16651 16652 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16653 /* SD FaultInjection */ 16654 sd_faultinjection(pktp); 16655 16656 #endif /* SD_FAULT_INJECTION */ 16657 16658 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16659 " xp:0x%p, un:0x%p\n", bp, xp, un); 16660 16661 mutex_enter(SD_MUTEX(un)); 16662 16663 /* Reduce the count of the #commands currently in transport */ 16664 un->un_ncmds_in_transport--; 16665 ASSERT(un->un_ncmds_in_transport >= 0); 16666 16667 /* Increment counter to indicate that the callback routine is active */ 16668 un->un_in_callback++; 16669 16670 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16671 16672 #ifdef SDDEBUG 16673 if (bp == un->un_retry_bp) { 16674 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16675 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16676 un, un->un_retry_bp, un->un_ncmds_in_transport); 16677 } 16678 #endif 16679 16680 /* 16681 * If pkt_reason is CMD_DEV_GONE, just fail the command 16682 */ 16683 if (pktp->pkt_reason == CMD_DEV_GONE) { 16684 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16685 "Device is gone\n"); 16686 sd_return_failed_command(un, bp, EIO); 16687 goto exit; 16688 } 16689 16690 /* 16691 * First see if the pkt has auto-request sense data with it.... 16692 * Look at the packet state first so we don't take a performance 16693 * hit looking at the arq enabled flag unless absolutely necessary. 16694 */ 16695 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16696 (un->un_f_arq_enabled == TRUE)) { 16697 /* 16698 * The HBA did an auto request sense for this command so check 16699 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16700 * driver command that should not be retried. 16701 */ 16702 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16703 /* 16704 * Save the relevant sense info into the xp for the 16705 * original cmd. 16706 */ 16707 struct scsi_arq_status *asp; 16708 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16709 xp->xb_sense_status = 16710 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16711 xp->xb_sense_state = asp->sts_rqpkt_state; 16712 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16713 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16714 min(sizeof (struct scsi_extended_sense), 16715 SENSE_LENGTH)); 16716 16717 /* fail the command */ 16718 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16719 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16720 sd_return_failed_command(un, bp, EIO); 16721 goto exit; 16722 } 16723 16724 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16725 /* 16726 * We want to either retry or fail this command, so free 16727 * the DMA resources here. If we retry the command then 16728 * the DMA resources will be reallocated in sd_start_cmds(). 16729 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16730 * causes the *entire* transfer to start over again from the 16731 * beginning of the request, even for PARTIAL chunks that 16732 * have already transferred successfully. 16733 */ 16734 if ((un->un_f_is_fibre == TRUE) && 16735 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16736 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16737 scsi_dmafree(pktp); 16738 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16739 } 16740 #endif 16741 16742 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16743 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16744 16745 sd_handle_auto_request_sense(un, bp, xp, pktp); 16746 goto exit; 16747 } 16748 16749 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16750 if (pktp->pkt_flags & FLAG_SENSING) { 16751 /* This pktp is from the unit's REQUEST_SENSE command */ 16752 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16753 "sdintr: sd_handle_request_sense\n"); 16754 sd_handle_request_sense(un, bp, xp, pktp); 16755 goto exit; 16756 } 16757 16758 /* 16759 * Check to see if the command successfully completed as requested; 16760 * this is the most common case (and also the hot performance path). 16761 * 16762 * Requirements for successful completion are: 16763 * pkt_reason is CMD_CMPLT and packet status is status good. 16764 * In addition: 16765 * - A residual of zero indicates successful completion no matter what 16766 * the command is. 16767 * - If the residual is not zero and the command is not a read or 16768 * write, then it's still defined as successful completion. In other 16769 * words, if the command is a read or write the residual must be 16770 * zero for successful completion. 16771 * - If the residual is not zero and the command is a read or 16772 * write, and it's a USCSICMD, then it's still defined as 16773 * successful completion. 16774 */ 16775 if ((pktp->pkt_reason == CMD_CMPLT) && 16776 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16777 16778 /* 16779 * Since this command is returned with a good status, we 16780 * can reset the count for Sonoma failover. 16781 */ 16782 un->un_sonoma_failure_count = 0; 16783 16784 /* 16785 * Return all USCSI commands on good status 16786 */ 16787 if (pktp->pkt_resid == 0) { 16788 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16789 "sdintr: returning command for resid == 0\n"); 16790 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16791 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16792 SD_UPDATE_B_RESID(bp, pktp); 16793 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16794 "sdintr: returning command for resid != 0\n"); 16795 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16796 SD_UPDATE_B_RESID(bp, pktp); 16797 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16798 "sdintr: returning uscsi command\n"); 16799 } else { 16800 goto not_successful; 16801 } 16802 sd_return_command(un, bp); 16803 16804 /* 16805 * Decrement counter to indicate that the callback routine 16806 * is done. 16807 */ 16808 un->un_in_callback--; 16809 ASSERT(un->un_in_callback >= 0); 16810 mutex_exit(SD_MUTEX(un)); 16811 16812 return; 16813 } 16814 16815 not_successful: 16816 16817 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16818 /* 16819 * The following is based upon knowledge of the underlying transport 16820 * and its use of DMA resources. This code should be removed when 16821 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16822 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16823 * and sd_start_cmds(). 16824 * 16825 * Free any DMA resources associated with this command if there 16826 * is a chance it could be retried or enqueued for later retry. 16827 * If we keep the DMA binding then mpxio cannot reissue the 16828 * command on another path whenever a path failure occurs. 16829 * 16830 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16831 * causes the *entire* transfer to start over again from the 16832 * beginning of the request, even for PARTIAL chunks that 16833 * have already transferred successfully. 16834 * 16835 * This is only done for non-uscsi commands (and also skipped for the 16836 * driver's internal RQS command). Also just do this for Fibre Channel 16837 * devices as these are the only ones that support mpxio. 16838 */ 16839 if ((un->un_f_is_fibre == TRUE) && 16840 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16841 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16842 scsi_dmafree(pktp); 16843 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16844 } 16845 #endif 16846 16847 /* 16848 * The command did not successfully complete as requested so check 16849 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16850 * driver command that should not be retried so just return. If 16851 * FLAG_DIAGNOSE is not set the error will be processed below. 16852 */ 16853 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16854 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16855 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16856 /* 16857 * Issue a request sense if a check condition caused the error 16858 * (we handle the auto request sense case above), otherwise 16859 * just fail the command. 16860 */ 16861 if ((pktp->pkt_reason == CMD_CMPLT) && 16862 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16863 sd_send_request_sense_command(un, bp, pktp); 16864 } else { 16865 sd_return_failed_command(un, bp, EIO); 16866 } 16867 goto exit; 16868 } 16869 16870 /* 16871 * The command did not successfully complete as requested so process 16872 * the error, retry, and/or attempt recovery. 16873 */ 16874 switch (pktp->pkt_reason) { 16875 case CMD_CMPLT: 16876 switch (SD_GET_PKT_STATUS(pktp)) { 16877 case STATUS_GOOD: 16878 /* 16879 * The command completed successfully with a non-zero 16880 * residual 16881 */ 16882 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16883 "sdintr: STATUS_GOOD \n"); 16884 sd_pkt_status_good(un, bp, xp, pktp); 16885 break; 16886 16887 case STATUS_CHECK: 16888 case STATUS_TERMINATED: 16889 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16890 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16891 sd_pkt_status_check_condition(un, bp, xp, pktp); 16892 break; 16893 16894 case STATUS_BUSY: 16895 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16896 "sdintr: STATUS_BUSY\n"); 16897 sd_pkt_status_busy(un, bp, xp, pktp); 16898 break; 16899 16900 case STATUS_RESERVATION_CONFLICT: 16901 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16902 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16903 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16904 break; 16905 16906 case STATUS_QFULL: 16907 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16908 "sdintr: STATUS_QFULL\n"); 16909 sd_pkt_status_qfull(un, bp, xp, pktp); 16910 break; 16911 16912 case STATUS_MET: 16913 case STATUS_INTERMEDIATE: 16914 case STATUS_SCSI2: 16915 case STATUS_INTERMEDIATE_MET: 16916 case STATUS_ACA_ACTIVE: 16917 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16918 "Unexpected SCSI status received: 0x%x\n", 16919 SD_GET_PKT_STATUS(pktp)); 16920 sd_return_failed_command(un, bp, EIO); 16921 break; 16922 16923 default: 16924 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16925 "Invalid SCSI status received: 0x%x\n", 16926 SD_GET_PKT_STATUS(pktp)); 16927 sd_return_failed_command(un, bp, EIO); 16928 break; 16929 16930 } 16931 break; 16932 16933 case CMD_INCOMPLETE: 16934 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16935 "sdintr: CMD_INCOMPLETE\n"); 16936 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16937 break; 16938 case CMD_TRAN_ERR: 16939 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16940 "sdintr: CMD_TRAN_ERR\n"); 16941 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16942 break; 16943 case CMD_RESET: 16944 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16945 "sdintr: CMD_RESET \n"); 16946 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16947 break; 16948 case CMD_ABORTED: 16949 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16950 "sdintr: CMD_ABORTED \n"); 16951 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16952 break; 16953 case CMD_TIMEOUT: 16954 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16955 "sdintr: CMD_TIMEOUT\n"); 16956 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16957 break; 16958 case CMD_UNX_BUS_FREE: 16959 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16960 "sdintr: CMD_UNX_BUS_FREE \n"); 16961 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16962 break; 16963 case CMD_TAG_REJECT: 16964 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16965 "sdintr: CMD_TAG_REJECT\n"); 16966 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16967 break; 16968 default: 16969 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16970 "sdintr: default\n"); 16971 sd_pkt_reason_default(un, bp, xp, pktp); 16972 break; 16973 } 16974 16975 exit: 16976 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16977 16978 /* Decrement counter to indicate that the callback routine is done. */ 16979 un->un_in_callback--; 16980 ASSERT(un->un_in_callback >= 0); 16981 16982 /* 16983 * At this point, the pkt has been dispatched, ie, it is either 16984 * being re-tried or has been returned to its caller and should 16985 * not be referenced. 16986 */ 16987 16988 mutex_exit(SD_MUTEX(un)); 16989 } 16990 16991 16992 /* 16993 * Function: sd_print_incomplete_msg 16994 * 16995 * Description: Prints the error message for a CMD_INCOMPLETE error. 16996 * 16997 * Arguments: un - ptr to associated softstate for the device. 16998 * bp - ptr to the buf(9S) for the command. 16999 * arg - message string ptr 17000 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17001 * or SD_NO_RETRY_ISSUED. 17002 * 17003 * Context: May be called under interrupt context 17004 */ 17005 17006 static void 17007 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17008 { 17009 struct scsi_pkt *pktp; 17010 char *msgp; 17011 char *cmdp = arg; 17012 17013 ASSERT(un != NULL); 17014 ASSERT(mutex_owned(SD_MUTEX(un))); 17015 ASSERT(bp != NULL); 17016 ASSERT(arg != NULL); 17017 pktp = SD_GET_PKTP(bp); 17018 ASSERT(pktp != NULL); 17019 17020 switch (code) { 17021 case SD_DELAYED_RETRY_ISSUED: 17022 case SD_IMMEDIATE_RETRY_ISSUED: 17023 msgp = "retrying"; 17024 break; 17025 case SD_NO_RETRY_ISSUED: 17026 default: 17027 msgp = "giving up"; 17028 break; 17029 } 17030 17031 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17032 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17033 "incomplete %s- %s\n", cmdp, msgp); 17034 } 17035 } 17036 17037 17038 17039 /* 17040 * Function: sd_pkt_status_good 17041 * 17042 * Description: Processing for a STATUS_GOOD code in pkt_status. 17043 * 17044 * Context: May be called under interrupt context 17045 */ 17046 17047 static void 17048 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17049 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17050 { 17051 char *cmdp; 17052 17053 ASSERT(un != NULL); 17054 ASSERT(mutex_owned(SD_MUTEX(un))); 17055 ASSERT(bp != NULL); 17056 ASSERT(xp != NULL); 17057 ASSERT(pktp != NULL); 17058 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17059 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17060 ASSERT(pktp->pkt_resid != 0); 17061 17062 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17063 17064 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17065 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17066 case SCMD_READ: 17067 cmdp = "read"; 17068 break; 17069 case SCMD_WRITE: 17070 cmdp = "write"; 17071 break; 17072 default: 17073 SD_UPDATE_B_RESID(bp, pktp); 17074 sd_return_command(un, bp); 17075 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17076 return; 17077 } 17078 17079 /* 17080 * See if we can retry the read/write, preferrably immediately. 17081 * If retries are exhaused, then sd_retry_command() will update 17082 * the b_resid count. 17083 */ 17084 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17085 cmdp, EIO, (clock_t)0, NULL); 17086 17087 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17088 } 17089 17090 17091 17092 17093 17094 /* 17095 * Function: sd_handle_request_sense 17096 * 17097 * Description: Processing for non-auto Request Sense command. 17098 * 17099 * Arguments: un - ptr to associated softstate 17100 * sense_bp - ptr to buf(9S) for the RQS command 17101 * sense_xp - ptr to the sd_xbuf for the RQS command 17102 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17103 * 17104 * Context: May be called under interrupt context 17105 */ 17106 17107 static void 17108 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17109 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17110 { 17111 struct buf *cmd_bp; /* buf for the original command */ 17112 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17113 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17114 17115 ASSERT(un != NULL); 17116 ASSERT(mutex_owned(SD_MUTEX(un))); 17117 ASSERT(sense_bp != NULL); 17118 ASSERT(sense_xp != NULL); 17119 ASSERT(sense_pktp != NULL); 17120 17121 /* 17122 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17123 * RQS command and not the original command. 17124 */ 17125 ASSERT(sense_pktp == un->un_rqs_pktp); 17126 ASSERT(sense_bp == un->un_rqs_bp); 17127 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17128 (FLAG_SENSING | FLAG_HEAD)); 17129 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17130 FLAG_SENSING) == FLAG_SENSING); 17131 17132 /* These are the bp, xp, and pktp for the original command */ 17133 cmd_bp = sense_xp->xb_sense_bp; 17134 cmd_xp = SD_GET_XBUF(cmd_bp); 17135 cmd_pktp = SD_GET_PKTP(cmd_bp); 17136 17137 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17138 /* 17139 * The REQUEST SENSE command failed. Release the REQUEST 17140 * SENSE command for re-use, get back the bp for the original 17141 * command, and attempt to re-try the original command if 17142 * FLAG_DIAGNOSE is not set in the original packet. 17143 */ 17144 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17145 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17146 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17147 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17148 NULL, NULL, EIO, (clock_t)0, NULL); 17149 return; 17150 } 17151 } 17152 17153 /* 17154 * Save the relevant sense info into the xp for the original cmd. 17155 * 17156 * Note: if the request sense failed the state info will be zero 17157 * as set in sd_mark_rqs_busy() 17158 */ 17159 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17160 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17161 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17162 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 17163 17164 /* 17165 * Free up the RQS command.... 17166 * NOTE: 17167 * Must do this BEFORE calling sd_validate_sense_data! 17168 * sd_validate_sense_data may return the original command in 17169 * which case the pkt will be freed and the flags can no 17170 * longer be touched. 17171 * SD_MUTEX is held through this process until the command 17172 * is dispatched based upon the sense data, so there are 17173 * no race conditions. 17174 */ 17175 (void) sd_mark_rqs_idle(un, sense_xp); 17176 17177 /* 17178 * For a retryable command see if we have valid sense data, if so then 17179 * turn it over to sd_decode_sense() to figure out the right course of 17180 * action. Just fail a non-retryable command. 17181 */ 17182 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17183 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 17184 SD_SENSE_DATA_IS_VALID) { 17185 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17186 } 17187 } else { 17188 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17189 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17190 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17191 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17192 sd_return_failed_command(un, cmd_bp, EIO); 17193 } 17194 } 17195 17196 17197 17198 17199 /* 17200 * Function: sd_handle_auto_request_sense 17201 * 17202 * Description: Processing for auto-request sense information. 17203 * 17204 * Arguments: un - ptr to associated softstate 17205 * bp - ptr to buf(9S) for the command 17206 * xp - ptr to the sd_xbuf for the command 17207 * pktp - ptr to the scsi_pkt(9S) for the command 17208 * 17209 * Context: May be called under interrupt context 17210 */ 17211 17212 static void 17213 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17214 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17215 { 17216 struct scsi_arq_status *asp; 17217 17218 ASSERT(un != NULL); 17219 ASSERT(mutex_owned(SD_MUTEX(un))); 17220 ASSERT(bp != NULL); 17221 ASSERT(xp != NULL); 17222 ASSERT(pktp != NULL); 17223 ASSERT(pktp != un->un_rqs_pktp); 17224 ASSERT(bp != un->un_rqs_bp); 17225 17226 /* 17227 * For auto-request sense, we get a scsi_arq_status back from 17228 * the HBA, with the sense data in the sts_sensedata member. 17229 * The pkt_scbp of the packet points to this scsi_arq_status. 17230 */ 17231 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17232 17233 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17234 /* 17235 * The auto REQUEST SENSE failed; see if we can re-try 17236 * the original command. 17237 */ 17238 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17239 "auto request sense failed (reason=%s)\n", 17240 scsi_rname(asp->sts_rqpkt_reason)); 17241 17242 sd_reset_target(un, pktp); 17243 17244 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17245 NULL, NULL, EIO, (clock_t)0, NULL); 17246 return; 17247 } 17248 17249 /* Save the relevant sense info into the xp for the original cmd. */ 17250 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17251 xp->xb_sense_state = asp->sts_rqpkt_state; 17252 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17253 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17254 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 17255 17256 /* 17257 * See if we have valid sense data, if so then turn it over to 17258 * sd_decode_sense() to figure out the right course of action. 17259 */ 17260 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 17261 sd_decode_sense(un, bp, xp, pktp); 17262 } 17263 } 17264 17265 17266 /* 17267 * Function: sd_print_sense_failed_msg 17268 * 17269 * Description: Print log message when RQS has failed. 17270 * 17271 * Arguments: un - ptr to associated softstate 17272 * bp - ptr to buf(9S) for the command 17273 * arg - generic message string ptr 17274 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17275 * or SD_NO_RETRY_ISSUED 17276 * 17277 * Context: May be called from interrupt context 17278 */ 17279 17280 static void 17281 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17282 int code) 17283 { 17284 char *msgp = arg; 17285 17286 ASSERT(un != NULL); 17287 ASSERT(mutex_owned(SD_MUTEX(un))); 17288 ASSERT(bp != NULL); 17289 17290 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17291 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17292 } 17293 } 17294 17295 17296 /* 17297 * Function: sd_validate_sense_data 17298 * 17299 * Description: Check the given sense data for validity. 17300 * If the sense data is not valid, the command will 17301 * be either failed or retried! 17302 * 17303 * Return Code: SD_SENSE_DATA_IS_INVALID 17304 * SD_SENSE_DATA_IS_VALID 17305 * 17306 * Context: May be called from interrupt context 17307 */ 17308 17309 static int 17310 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 17311 { 17312 struct scsi_extended_sense *esp; 17313 struct scsi_pkt *pktp; 17314 size_t actual_len; 17315 char *msgp = NULL; 17316 17317 ASSERT(un != NULL); 17318 ASSERT(mutex_owned(SD_MUTEX(un))); 17319 ASSERT(bp != NULL); 17320 ASSERT(bp != un->un_rqs_bp); 17321 ASSERT(xp != NULL); 17322 17323 pktp = SD_GET_PKTP(bp); 17324 ASSERT(pktp != NULL); 17325 17326 /* 17327 * Check the status of the RQS command (auto or manual). 17328 */ 17329 switch (xp->xb_sense_status & STATUS_MASK) { 17330 case STATUS_GOOD: 17331 break; 17332 17333 case STATUS_RESERVATION_CONFLICT: 17334 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17335 return (SD_SENSE_DATA_IS_INVALID); 17336 17337 case STATUS_BUSY: 17338 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17339 "Busy Status on REQUEST SENSE\n"); 17340 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17341 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17342 return (SD_SENSE_DATA_IS_INVALID); 17343 17344 case STATUS_QFULL: 17345 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17346 "QFULL Status on REQUEST SENSE\n"); 17347 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17348 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17349 return (SD_SENSE_DATA_IS_INVALID); 17350 17351 case STATUS_CHECK: 17352 case STATUS_TERMINATED: 17353 msgp = "Check Condition on REQUEST SENSE\n"; 17354 goto sense_failed; 17355 17356 default: 17357 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17358 goto sense_failed; 17359 } 17360 17361 /* 17362 * See if we got the minimum required amount of sense data. 17363 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17364 * or less. 17365 */ 17366 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 17367 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17368 (actual_len == 0)) { 17369 msgp = "Request Sense couldn't get sense data\n"; 17370 goto sense_failed; 17371 } 17372 17373 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17374 msgp = "Not enough sense information\n"; 17375 goto sense_failed; 17376 } 17377 17378 /* 17379 * We require the extended sense data 17380 */ 17381 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17382 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17383 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17384 static char tmp[8]; 17385 static char buf[148]; 17386 char *p = (char *)(xp->xb_sense_data); 17387 int i; 17388 17389 mutex_enter(&sd_sense_mutex); 17390 (void) strcpy(buf, "undecodable sense information:"); 17391 for (i = 0; i < actual_len; i++) { 17392 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17393 (void) strcpy(&buf[strlen(buf)], tmp); 17394 } 17395 i = strlen(buf); 17396 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17397 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 17398 mutex_exit(&sd_sense_mutex); 17399 } 17400 /* Note: Legacy behavior, fail the command with no retry */ 17401 sd_return_failed_command(un, bp, EIO); 17402 return (SD_SENSE_DATA_IS_INVALID); 17403 } 17404 17405 /* 17406 * Check that es_code is valid (es_class concatenated with es_code 17407 * make up the "response code" field. es_class will always be 7, so 17408 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17409 * format. 17410 */ 17411 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17412 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17413 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17414 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17415 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17416 goto sense_failed; 17417 } 17418 17419 return (SD_SENSE_DATA_IS_VALID); 17420 17421 sense_failed: 17422 /* 17423 * If the request sense failed (for whatever reason), attempt 17424 * to retry the original command. 17425 */ 17426 #if defined(__i386) || defined(__amd64) 17427 /* 17428 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17429 * sddef.h for Sparc platform, and x86 uses 1 binary 17430 * for both SCSI/FC. 17431 * The SD_RETRY_DELAY value need to be adjusted here 17432 * when SD_RETRY_DELAY change in sddef.h 17433 */ 17434 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17435 sd_print_sense_failed_msg, msgp, EIO, 17436 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17437 #else 17438 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17439 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17440 #endif 17441 17442 return (SD_SENSE_DATA_IS_INVALID); 17443 } 17444 17445 17446 17447 /* 17448 * Function: sd_decode_sense 17449 * 17450 * Description: Take recovery action(s) when SCSI Sense Data is received. 17451 * 17452 * Context: Interrupt context. 17453 */ 17454 17455 static void 17456 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17457 struct scsi_pkt *pktp) 17458 { 17459 uint8_t sense_key; 17460 17461 ASSERT(un != NULL); 17462 ASSERT(mutex_owned(SD_MUTEX(un))); 17463 ASSERT(bp != NULL); 17464 ASSERT(bp != un->un_rqs_bp); 17465 ASSERT(xp != NULL); 17466 ASSERT(pktp != NULL); 17467 17468 sense_key = scsi_sense_key(xp->xb_sense_data); 17469 17470 switch (sense_key) { 17471 case KEY_NO_SENSE: 17472 sd_sense_key_no_sense(un, bp, xp, pktp); 17473 break; 17474 case KEY_RECOVERABLE_ERROR: 17475 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17476 bp, xp, pktp); 17477 break; 17478 case KEY_NOT_READY: 17479 sd_sense_key_not_ready(un, xp->xb_sense_data, 17480 bp, xp, pktp); 17481 break; 17482 case KEY_MEDIUM_ERROR: 17483 case KEY_HARDWARE_ERROR: 17484 sd_sense_key_medium_or_hardware_error(un, 17485 xp->xb_sense_data, bp, xp, pktp); 17486 break; 17487 case KEY_ILLEGAL_REQUEST: 17488 sd_sense_key_illegal_request(un, bp, xp, pktp); 17489 break; 17490 case KEY_UNIT_ATTENTION: 17491 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17492 bp, xp, pktp); 17493 break; 17494 case KEY_WRITE_PROTECT: 17495 case KEY_VOLUME_OVERFLOW: 17496 case KEY_MISCOMPARE: 17497 sd_sense_key_fail_command(un, bp, xp, pktp); 17498 break; 17499 case KEY_BLANK_CHECK: 17500 sd_sense_key_blank_check(un, bp, xp, pktp); 17501 break; 17502 case KEY_ABORTED_COMMAND: 17503 sd_sense_key_aborted_command(un, bp, xp, pktp); 17504 break; 17505 case KEY_VENDOR_UNIQUE: 17506 case KEY_COPY_ABORTED: 17507 case KEY_EQUAL: 17508 case KEY_RESERVED: 17509 default: 17510 sd_sense_key_default(un, xp->xb_sense_data, 17511 bp, xp, pktp); 17512 break; 17513 } 17514 } 17515 17516 17517 /* 17518 * Function: sd_dump_memory 17519 * 17520 * Description: Debug logging routine to print the contents of a user provided 17521 * buffer. The output of the buffer is broken up into 256 byte 17522 * segments due to a size constraint of the scsi_log. 17523 * implementation. 17524 * 17525 * Arguments: un - ptr to softstate 17526 * comp - component mask 17527 * title - "title" string to preceed data when printed 17528 * data - ptr to data block to be printed 17529 * len - size of data block to be printed 17530 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17531 * 17532 * Context: May be called from interrupt context 17533 */ 17534 17535 #define SD_DUMP_MEMORY_BUF_SIZE 256 17536 17537 static char *sd_dump_format_string[] = { 17538 " 0x%02x", 17539 " %c" 17540 }; 17541 17542 static void 17543 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17544 int len, int fmt) 17545 { 17546 int i, j; 17547 int avail_count; 17548 int start_offset; 17549 int end_offset; 17550 size_t entry_len; 17551 char *bufp; 17552 char *local_buf; 17553 char *format_string; 17554 17555 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17556 17557 /* 17558 * In the debug version of the driver, this function is called from a 17559 * number of places which are NOPs in the release driver. 17560 * The debug driver therefore has additional methods of filtering 17561 * debug output. 17562 */ 17563 #ifdef SDDEBUG 17564 /* 17565 * In the debug version of the driver we can reduce the amount of debug 17566 * messages by setting sd_error_level to something other than 17567 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17568 * sd_component_mask. 17569 */ 17570 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17571 (sd_error_level != SCSI_ERR_ALL)) { 17572 return; 17573 } 17574 if (((sd_component_mask & comp) == 0) || 17575 (sd_error_level != SCSI_ERR_ALL)) { 17576 return; 17577 } 17578 #else 17579 if (sd_error_level != SCSI_ERR_ALL) { 17580 return; 17581 } 17582 #endif 17583 17584 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17585 bufp = local_buf; 17586 /* 17587 * Available length is the length of local_buf[], minus the 17588 * length of the title string, minus one for the ":", minus 17589 * one for the newline, minus one for the NULL terminator. 17590 * This gives the #bytes available for holding the printed 17591 * values from the given data buffer. 17592 */ 17593 if (fmt == SD_LOG_HEX) { 17594 format_string = sd_dump_format_string[0]; 17595 } else /* SD_LOG_CHAR */ { 17596 format_string = sd_dump_format_string[1]; 17597 } 17598 /* 17599 * Available count is the number of elements from the given 17600 * data buffer that we can fit into the available length. 17601 * This is based upon the size of the format string used. 17602 * Make one entry and find it's size. 17603 */ 17604 (void) sprintf(bufp, format_string, data[0]); 17605 entry_len = strlen(bufp); 17606 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17607 17608 j = 0; 17609 while (j < len) { 17610 bufp = local_buf; 17611 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17612 start_offset = j; 17613 17614 end_offset = start_offset + avail_count; 17615 17616 (void) sprintf(bufp, "%s:", title); 17617 bufp += strlen(bufp); 17618 for (i = start_offset; ((i < end_offset) && (j < len)); 17619 i++, j++) { 17620 (void) sprintf(bufp, format_string, data[i]); 17621 bufp += entry_len; 17622 } 17623 (void) sprintf(bufp, "\n"); 17624 17625 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17626 } 17627 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17628 } 17629 17630 /* 17631 * Function: sd_print_sense_msg 17632 * 17633 * Description: Log a message based upon the given sense data. 17634 * 17635 * Arguments: un - ptr to associated softstate 17636 * bp - ptr to buf(9S) for the command 17637 * arg - ptr to associate sd_sense_info struct 17638 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17639 * or SD_NO_RETRY_ISSUED 17640 * 17641 * Context: May be called from interrupt context 17642 */ 17643 17644 static void 17645 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17646 { 17647 struct sd_xbuf *xp; 17648 struct scsi_pkt *pktp; 17649 uint8_t *sensep; 17650 daddr_t request_blkno; 17651 diskaddr_t err_blkno; 17652 int severity; 17653 int pfa_flag; 17654 extern struct scsi_key_strings scsi_cmds[]; 17655 17656 ASSERT(un != NULL); 17657 ASSERT(mutex_owned(SD_MUTEX(un))); 17658 ASSERT(bp != NULL); 17659 xp = SD_GET_XBUF(bp); 17660 ASSERT(xp != NULL); 17661 pktp = SD_GET_PKTP(bp); 17662 ASSERT(pktp != NULL); 17663 ASSERT(arg != NULL); 17664 17665 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17666 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17667 17668 if ((code == SD_DELAYED_RETRY_ISSUED) || 17669 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17670 severity = SCSI_ERR_RETRYABLE; 17671 } 17672 17673 /* Use absolute block number for the request block number */ 17674 request_blkno = xp->xb_blkno; 17675 17676 /* 17677 * Now try to get the error block number from the sense data 17678 */ 17679 sensep = xp->xb_sense_data; 17680 17681 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17682 (uint64_t *)&err_blkno)) { 17683 /* 17684 * We retrieved the error block number from the information 17685 * portion of the sense data. 17686 * 17687 * For USCSI commands we are better off using the error 17688 * block no. as the requested block no. (This is the best 17689 * we can estimate.) 17690 */ 17691 if ((SD_IS_BUFIO(xp) == FALSE) && 17692 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17693 request_blkno = err_blkno; 17694 } 17695 } else { 17696 /* 17697 * Without the es_valid bit set (for fixed format) or an 17698 * information descriptor (for descriptor format) we cannot 17699 * be certain of the error blkno, so just use the 17700 * request_blkno. 17701 */ 17702 err_blkno = (diskaddr_t)request_blkno; 17703 } 17704 17705 /* 17706 * The following will log the buffer contents for the release driver 17707 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17708 * level is set to verbose. 17709 */ 17710 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17711 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17712 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17713 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17714 17715 if (pfa_flag == FALSE) { 17716 /* This is normally only set for USCSI */ 17717 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17718 return; 17719 } 17720 17721 if ((SD_IS_BUFIO(xp) == TRUE) && 17722 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17723 (severity < sd_error_level))) { 17724 return; 17725 } 17726 } 17727 17728 /* 17729 * Check for Sonoma Failover and keep a count of how many failed I/O's 17730 */ 17731 if ((SD_IS_LSI(un)) && 17732 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17733 (scsi_sense_asc(sensep) == 0x94) && 17734 (scsi_sense_ascq(sensep) == 0x01)) { 17735 un->un_sonoma_failure_count++; 17736 if (un->un_sonoma_failure_count > 1) { 17737 return; 17738 } 17739 } 17740 17741 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17742 request_blkno, err_blkno, scsi_cmds, 17743 (struct scsi_extended_sense *)sensep, 17744 un->un_additional_codes, NULL); 17745 } 17746 17747 /* 17748 * Function: sd_sense_key_no_sense 17749 * 17750 * Description: Recovery action when sense data was not received. 17751 * 17752 * Context: May be called from interrupt context 17753 */ 17754 17755 static void 17756 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17757 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17758 { 17759 struct sd_sense_info si; 17760 17761 ASSERT(un != NULL); 17762 ASSERT(mutex_owned(SD_MUTEX(un))); 17763 ASSERT(bp != NULL); 17764 ASSERT(xp != NULL); 17765 ASSERT(pktp != NULL); 17766 17767 si.ssi_severity = SCSI_ERR_FATAL; 17768 si.ssi_pfa_flag = FALSE; 17769 17770 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17771 17772 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17773 &si, EIO, (clock_t)0, NULL); 17774 } 17775 17776 17777 /* 17778 * Function: sd_sense_key_recoverable_error 17779 * 17780 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17781 * 17782 * Context: May be called from interrupt context 17783 */ 17784 17785 static void 17786 sd_sense_key_recoverable_error(struct sd_lun *un, 17787 uint8_t *sense_datap, 17788 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17789 { 17790 struct sd_sense_info si; 17791 uint8_t asc = scsi_sense_asc(sense_datap); 17792 17793 ASSERT(un != NULL); 17794 ASSERT(mutex_owned(SD_MUTEX(un))); 17795 ASSERT(bp != NULL); 17796 ASSERT(xp != NULL); 17797 ASSERT(pktp != NULL); 17798 17799 /* 17800 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17801 */ 17802 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17803 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17804 si.ssi_severity = SCSI_ERR_INFO; 17805 si.ssi_pfa_flag = TRUE; 17806 } else { 17807 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17808 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17809 si.ssi_severity = SCSI_ERR_RECOVERED; 17810 si.ssi_pfa_flag = FALSE; 17811 } 17812 17813 if (pktp->pkt_resid == 0) { 17814 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17815 sd_return_command(un, bp); 17816 return; 17817 } 17818 17819 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17820 &si, EIO, (clock_t)0, NULL); 17821 } 17822 17823 17824 17825 17826 /* 17827 * Function: sd_sense_key_not_ready 17828 * 17829 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17830 * 17831 * Context: May be called from interrupt context 17832 */ 17833 17834 static void 17835 sd_sense_key_not_ready(struct sd_lun *un, 17836 uint8_t *sense_datap, 17837 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17838 { 17839 struct sd_sense_info si; 17840 uint8_t asc = scsi_sense_asc(sense_datap); 17841 uint8_t ascq = scsi_sense_ascq(sense_datap); 17842 17843 ASSERT(un != NULL); 17844 ASSERT(mutex_owned(SD_MUTEX(un))); 17845 ASSERT(bp != NULL); 17846 ASSERT(xp != NULL); 17847 ASSERT(pktp != NULL); 17848 17849 si.ssi_severity = SCSI_ERR_FATAL; 17850 si.ssi_pfa_flag = FALSE; 17851 17852 /* 17853 * Update error stats after first NOT READY error. Disks may have 17854 * been powered down and may need to be restarted. For CDROMs, 17855 * report NOT READY errors only if media is present. 17856 */ 17857 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17858 (xp->xb_retry_count > 0)) { 17859 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17860 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17861 } 17862 17863 /* 17864 * Just fail if the "not ready" retry limit has been reached. 17865 */ 17866 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17867 /* Special check for error message printing for removables. */ 17868 if (un->un_f_has_removable_media && (asc == 0x04) && 17869 (ascq >= 0x04)) { 17870 si.ssi_severity = SCSI_ERR_ALL; 17871 } 17872 goto fail_command; 17873 } 17874 17875 /* 17876 * Check the ASC and ASCQ in the sense data as needed, to determine 17877 * what to do. 17878 */ 17879 switch (asc) { 17880 case 0x04: /* LOGICAL UNIT NOT READY */ 17881 /* 17882 * disk drives that don't spin up result in a very long delay 17883 * in format without warning messages. We will log a message 17884 * if the error level is set to verbose. 17885 */ 17886 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17887 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17888 "logical unit not ready, resetting disk\n"); 17889 } 17890 17891 /* 17892 * There are different requirements for CDROMs and disks for 17893 * the number of retries. If a CD-ROM is giving this, it is 17894 * probably reading TOC and is in the process of getting 17895 * ready, so we should keep on trying for a long time to make 17896 * sure that all types of media are taken in account (for 17897 * some media the drive takes a long time to read TOC). For 17898 * disks we do not want to retry this too many times as this 17899 * can cause a long hang in format when the drive refuses to 17900 * spin up (a very common failure). 17901 */ 17902 switch (ascq) { 17903 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17904 /* 17905 * Disk drives frequently refuse to spin up which 17906 * results in a very long hang in format without 17907 * warning messages. 17908 * 17909 * Note: This code preserves the legacy behavior of 17910 * comparing xb_retry_count against zero for fibre 17911 * channel targets instead of comparing against the 17912 * un_reset_retry_count value. The reason for this 17913 * discrepancy has been so utterly lost beneath the 17914 * Sands of Time that even Indiana Jones could not 17915 * find it. 17916 */ 17917 if (un->un_f_is_fibre == TRUE) { 17918 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17919 (xp->xb_retry_count > 0)) && 17920 (un->un_startstop_timeid == NULL)) { 17921 scsi_log(SD_DEVINFO(un), sd_label, 17922 CE_WARN, "logical unit not ready, " 17923 "resetting disk\n"); 17924 sd_reset_target(un, pktp); 17925 } 17926 } else { 17927 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17928 (xp->xb_retry_count > 17929 un->un_reset_retry_count)) && 17930 (un->un_startstop_timeid == NULL)) { 17931 scsi_log(SD_DEVINFO(un), sd_label, 17932 CE_WARN, "logical unit not ready, " 17933 "resetting disk\n"); 17934 sd_reset_target(un, pktp); 17935 } 17936 } 17937 break; 17938 17939 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17940 /* 17941 * If the target is in the process of becoming 17942 * ready, just proceed with the retry. This can 17943 * happen with CD-ROMs that take a long time to 17944 * read TOC after a power cycle or reset. 17945 */ 17946 goto do_retry; 17947 17948 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17949 break; 17950 17951 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17952 /* 17953 * Retries cannot help here so just fail right away. 17954 */ 17955 goto fail_command; 17956 17957 case 0x88: 17958 /* 17959 * Vendor-unique code for T3/T4: it indicates a 17960 * path problem in a mutipathed config, but as far as 17961 * the target driver is concerned it equates to a fatal 17962 * error, so we should just fail the command right away 17963 * (without printing anything to the console). If this 17964 * is not a T3/T4, fall thru to the default recovery 17965 * action. 17966 * T3/T4 is FC only, don't need to check is_fibre 17967 */ 17968 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17969 sd_return_failed_command(un, bp, EIO); 17970 return; 17971 } 17972 /* FALLTHRU */ 17973 17974 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17975 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17976 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17977 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17978 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17979 default: /* Possible future codes in SCSI spec? */ 17980 /* 17981 * For removable-media devices, do not retry if 17982 * ASCQ > 2 as these result mostly from USCSI commands 17983 * on MMC devices issued to check status of an 17984 * operation initiated in immediate mode. Also for 17985 * ASCQ >= 4 do not print console messages as these 17986 * mainly represent a user-initiated operation 17987 * instead of a system failure. 17988 */ 17989 if (un->un_f_has_removable_media) { 17990 si.ssi_severity = SCSI_ERR_ALL; 17991 goto fail_command; 17992 } 17993 break; 17994 } 17995 17996 /* 17997 * As part of our recovery attempt for the NOT READY 17998 * condition, we issue a START STOP UNIT command. However 17999 * we want to wait for a short delay before attempting this 18000 * as there may still be more commands coming back from the 18001 * target with the check condition. To do this we use 18002 * timeout(9F) to call sd_start_stop_unit_callback() after 18003 * the delay interval expires. (sd_start_stop_unit_callback() 18004 * dispatches sd_start_stop_unit_task(), which will issue 18005 * the actual START STOP UNIT command. The delay interval 18006 * is one-half of the delay that we will use to retry the 18007 * command that generated the NOT READY condition. 18008 * 18009 * Note that we could just dispatch sd_start_stop_unit_task() 18010 * from here and allow it to sleep for the delay interval, 18011 * but then we would be tying up the taskq thread 18012 * uncesessarily for the duration of the delay. 18013 * 18014 * Do not issue the START STOP UNIT if the current command 18015 * is already a START STOP UNIT. 18016 */ 18017 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18018 break; 18019 } 18020 18021 /* 18022 * Do not schedule the timeout if one is already pending. 18023 */ 18024 if (un->un_startstop_timeid != NULL) { 18025 SD_INFO(SD_LOG_ERROR, un, 18026 "sd_sense_key_not_ready: restart already issued to" 18027 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18028 ddi_get_instance(SD_DEVINFO(un))); 18029 break; 18030 } 18031 18032 /* 18033 * Schedule the START STOP UNIT command, then queue the command 18034 * for a retry. 18035 * 18036 * Note: A timeout is not scheduled for this retry because we 18037 * want the retry to be serial with the START_STOP_UNIT. The 18038 * retry will be started when the START_STOP_UNIT is completed 18039 * in sd_start_stop_unit_task. 18040 */ 18041 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18042 un, SD_BSY_TIMEOUT / 2); 18043 xp->xb_retry_count++; 18044 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18045 return; 18046 18047 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18048 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18049 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18050 "unit does not respond to selection\n"); 18051 } 18052 break; 18053 18054 case 0x3A: /* MEDIUM NOT PRESENT */ 18055 if (sd_error_level >= SCSI_ERR_FATAL) { 18056 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18057 "Caddy not inserted in drive\n"); 18058 } 18059 18060 sr_ejected(un); 18061 un->un_mediastate = DKIO_EJECTED; 18062 /* The state has changed, inform the media watch routines */ 18063 cv_broadcast(&un->un_state_cv); 18064 /* Just fail if no media is present in the drive. */ 18065 goto fail_command; 18066 18067 default: 18068 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18069 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18070 "Unit not Ready. Additional sense code 0x%x\n", 18071 asc); 18072 } 18073 break; 18074 } 18075 18076 do_retry: 18077 18078 /* 18079 * Retry the command, as some targets may report NOT READY for 18080 * several seconds after being reset. 18081 */ 18082 xp->xb_retry_count++; 18083 si.ssi_severity = SCSI_ERR_RETRYABLE; 18084 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18085 &si, EIO, SD_BSY_TIMEOUT, NULL); 18086 18087 return; 18088 18089 fail_command: 18090 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18091 sd_return_failed_command(un, bp, EIO); 18092 } 18093 18094 18095 18096 /* 18097 * Function: sd_sense_key_medium_or_hardware_error 18098 * 18099 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18100 * sense key. 18101 * 18102 * Context: May be called from interrupt context 18103 */ 18104 18105 static void 18106 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18107 uint8_t *sense_datap, 18108 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18109 { 18110 struct sd_sense_info si; 18111 uint8_t sense_key = scsi_sense_key(sense_datap); 18112 uint8_t asc = scsi_sense_asc(sense_datap); 18113 18114 ASSERT(un != NULL); 18115 ASSERT(mutex_owned(SD_MUTEX(un))); 18116 ASSERT(bp != NULL); 18117 ASSERT(xp != NULL); 18118 ASSERT(pktp != NULL); 18119 18120 si.ssi_severity = SCSI_ERR_FATAL; 18121 si.ssi_pfa_flag = FALSE; 18122 18123 if (sense_key == KEY_MEDIUM_ERROR) { 18124 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18125 } 18126 18127 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18128 18129 if ((un->un_reset_retry_count != 0) && 18130 (xp->xb_retry_count == un->un_reset_retry_count)) { 18131 mutex_exit(SD_MUTEX(un)); 18132 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18133 if (un->un_f_allow_bus_device_reset == TRUE) { 18134 18135 boolean_t try_resetting_target = B_TRUE; 18136 18137 /* 18138 * We need to be able to handle specific ASC when we are 18139 * handling a KEY_HARDWARE_ERROR. In particular 18140 * taking the default action of resetting the target may 18141 * not be the appropriate way to attempt recovery. 18142 * Resetting a target because of a single LUN failure 18143 * victimizes all LUNs on that target. 18144 * 18145 * This is true for the LSI arrays, if an LSI 18146 * array controller returns an ASC of 0x84 (LUN Dead) we 18147 * should trust it. 18148 */ 18149 18150 if (sense_key == KEY_HARDWARE_ERROR) { 18151 switch (asc) { 18152 case 0x84: 18153 if (SD_IS_LSI(un)) { 18154 try_resetting_target = B_FALSE; 18155 } 18156 break; 18157 default: 18158 break; 18159 } 18160 } 18161 18162 if (try_resetting_target == B_TRUE) { 18163 int reset_retval = 0; 18164 if (un->un_f_lun_reset_enabled == TRUE) { 18165 SD_TRACE(SD_LOG_IO_CORE, un, 18166 "sd_sense_key_medium_or_hardware_" 18167 "error: issuing RESET_LUN\n"); 18168 reset_retval = 18169 scsi_reset(SD_ADDRESS(un), 18170 RESET_LUN); 18171 } 18172 if (reset_retval == 0) { 18173 SD_TRACE(SD_LOG_IO_CORE, un, 18174 "sd_sense_key_medium_or_hardware_" 18175 "error: issuing RESET_TARGET\n"); 18176 (void) scsi_reset(SD_ADDRESS(un), 18177 RESET_TARGET); 18178 } 18179 } 18180 } 18181 mutex_enter(SD_MUTEX(un)); 18182 } 18183 18184 /* 18185 * This really ought to be a fatal error, but we will retry anyway 18186 * as some drives report this as a spurious error. 18187 */ 18188 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18189 &si, EIO, (clock_t)0, NULL); 18190 } 18191 18192 18193 18194 /* 18195 * Function: sd_sense_key_illegal_request 18196 * 18197 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18198 * 18199 * Context: May be called from interrupt context 18200 */ 18201 18202 static void 18203 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18204 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18205 { 18206 struct sd_sense_info si; 18207 18208 ASSERT(un != NULL); 18209 ASSERT(mutex_owned(SD_MUTEX(un))); 18210 ASSERT(bp != NULL); 18211 ASSERT(xp != NULL); 18212 ASSERT(pktp != NULL); 18213 18214 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18215 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18216 18217 si.ssi_severity = SCSI_ERR_INFO; 18218 si.ssi_pfa_flag = FALSE; 18219 18220 /* Pointless to retry if the target thinks it's an illegal request */ 18221 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18222 sd_return_failed_command(un, bp, EIO); 18223 } 18224 18225 18226 18227 18228 /* 18229 * Function: sd_sense_key_unit_attention 18230 * 18231 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18232 * 18233 * Context: May be called from interrupt context 18234 */ 18235 18236 static void 18237 sd_sense_key_unit_attention(struct sd_lun *un, 18238 uint8_t *sense_datap, 18239 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18240 { 18241 /* 18242 * For UNIT ATTENTION we allow retries for one minute. Devices 18243 * like Sonoma can return UNIT ATTENTION close to a minute 18244 * under certain conditions. 18245 */ 18246 int retry_check_flag = SD_RETRIES_UA; 18247 boolean_t kstat_updated = B_FALSE; 18248 struct sd_sense_info si; 18249 uint8_t asc = scsi_sense_asc(sense_datap); 18250 18251 ASSERT(un != NULL); 18252 ASSERT(mutex_owned(SD_MUTEX(un))); 18253 ASSERT(bp != NULL); 18254 ASSERT(xp != NULL); 18255 ASSERT(pktp != NULL); 18256 18257 si.ssi_severity = SCSI_ERR_INFO; 18258 si.ssi_pfa_flag = FALSE; 18259 18260 18261 switch (asc) { 18262 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18263 if (sd_report_pfa != 0) { 18264 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18265 si.ssi_pfa_flag = TRUE; 18266 retry_check_flag = SD_RETRIES_STANDARD; 18267 goto do_retry; 18268 } 18269 18270 break; 18271 18272 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18273 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18274 un->un_resvd_status |= 18275 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18276 } 18277 #ifdef _LP64 18278 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18279 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18280 un, KM_NOSLEEP) == 0) { 18281 /* 18282 * If we can't dispatch the task we'll just 18283 * live without descriptor sense. We can 18284 * try again on the next "unit attention" 18285 */ 18286 SD_ERROR(SD_LOG_ERROR, un, 18287 "sd_sense_key_unit_attention: " 18288 "Could not dispatch " 18289 "sd_reenable_dsense_task\n"); 18290 } 18291 } 18292 #endif /* _LP64 */ 18293 /* FALLTHRU */ 18294 18295 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18296 if (!un->un_f_has_removable_media) { 18297 break; 18298 } 18299 18300 /* 18301 * When we get a unit attention from a removable-media device, 18302 * it may be in a state that will take a long time to recover 18303 * (e.g., from a reset). Since we are executing in interrupt 18304 * context here, we cannot wait around for the device to come 18305 * back. So hand this command off to sd_media_change_task() 18306 * for deferred processing under taskq thread context. (Note 18307 * that the command still may be failed if a problem is 18308 * encountered at a later time.) 18309 */ 18310 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18311 KM_NOSLEEP) == 0) { 18312 /* 18313 * Cannot dispatch the request so fail the command. 18314 */ 18315 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18316 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18317 si.ssi_severity = SCSI_ERR_FATAL; 18318 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18319 sd_return_failed_command(un, bp, EIO); 18320 } 18321 18322 /* 18323 * If failed to dispatch sd_media_change_task(), we already 18324 * updated kstat. If succeed to dispatch sd_media_change_task(), 18325 * we should update kstat later if it encounters an error. So, 18326 * we update kstat_updated flag here. 18327 */ 18328 kstat_updated = B_TRUE; 18329 18330 /* 18331 * Either the command has been successfully dispatched to a 18332 * task Q for retrying, or the dispatch failed. In either case 18333 * do NOT retry again by calling sd_retry_command. This sets up 18334 * two retries of the same command and when one completes and 18335 * frees the resources the other will access freed memory, 18336 * a bad thing. 18337 */ 18338 return; 18339 18340 default: 18341 break; 18342 } 18343 18344 /* 18345 * Update kstat if we haven't done that. 18346 */ 18347 if (!kstat_updated) { 18348 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18349 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18350 } 18351 18352 do_retry: 18353 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18354 EIO, SD_UA_RETRY_DELAY, NULL); 18355 } 18356 18357 18358 18359 /* 18360 * Function: sd_sense_key_fail_command 18361 * 18362 * Description: Use to fail a command when we don't like the sense key that 18363 * was returned. 18364 * 18365 * Context: May be called from interrupt context 18366 */ 18367 18368 static void 18369 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18370 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18371 { 18372 struct sd_sense_info si; 18373 18374 ASSERT(un != NULL); 18375 ASSERT(mutex_owned(SD_MUTEX(un))); 18376 ASSERT(bp != NULL); 18377 ASSERT(xp != NULL); 18378 ASSERT(pktp != NULL); 18379 18380 si.ssi_severity = SCSI_ERR_FATAL; 18381 si.ssi_pfa_flag = FALSE; 18382 18383 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18384 sd_return_failed_command(un, bp, EIO); 18385 } 18386 18387 18388 18389 /* 18390 * Function: sd_sense_key_blank_check 18391 * 18392 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18393 * Has no monetary connotation. 18394 * 18395 * Context: May be called from interrupt context 18396 */ 18397 18398 static void 18399 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18400 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18401 { 18402 struct sd_sense_info si; 18403 18404 ASSERT(un != NULL); 18405 ASSERT(mutex_owned(SD_MUTEX(un))); 18406 ASSERT(bp != NULL); 18407 ASSERT(xp != NULL); 18408 ASSERT(pktp != NULL); 18409 18410 /* 18411 * Blank check is not fatal for removable devices, therefore 18412 * it does not require a console message. 18413 */ 18414 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18415 SCSI_ERR_FATAL; 18416 si.ssi_pfa_flag = FALSE; 18417 18418 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18419 sd_return_failed_command(un, bp, EIO); 18420 } 18421 18422 18423 18424 18425 /* 18426 * Function: sd_sense_key_aborted_command 18427 * 18428 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18429 * 18430 * Context: May be called from interrupt context 18431 */ 18432 18433 static void 18434 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18435 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18436 { 18437 struct sd_sense_info si; 18438 18439 ASSERT(un != NULL); 18440 ASSERT(mutex_owned(SD_MUTEX(un))); 18441 ASSERT(bp != NULL); 18442 ASSERT(xp != NULL); 18443 ASSERT(pktp != NULL); 18444 18445 si.ssi_severity = SCSI_ERR_FATAL; 18446 si.ssi_pfa_flag = FALSE; 18447 18448 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18449 18450 /* 18451 * This really ought to be a fatal error, but we will retry anyway 18452 * as some drives report this as a spurious error. 18453 */ 18454 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18455 &si, EIO, (clock_t)0, NULL); 18456 } 18457 18458 18459 18460 /* 18461 * Function: sd_sense_key_default 18462 * 18463 * Description: Default recovery action for several SCSI sense keys (basically 18464 * attempts a retry). 18465 * 18466 * Context: May be called from interrupt context 18467 */ 18468 18469 static void 18470 sd_sense_key_default(struct sd_lun *un, 18471 uint8_t *sense_datap, 18472 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18473 { 18474 struct sd_sense_info si; 18475 uint8_t sense_key = scsi_sense_key(sense_datap); 18476 18477 ASSERT(un != NULL); 18478 ASSERT(mutex_owned(SD_MUTEX(un))); 18479 ASSERT(bp != NULL); 18480 ASSERT(xp != NULL); 18481 ASSERT(pktp != NULL); 18482 18483 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18484 18485 /* 18486 * Undecoded sense key. Attempt retries and hope that will fix 18487 * the problem. Otherwise, we're dead. 18488 */ 18489 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18490 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18491 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18492 } 18493 18494 si.ssi_severity = SCSI_ERR_FATAL; 18495 si.ssi_pfa_flag = FALSE; 18496 18497 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18498 &si, EIO, (clock_t)0, NULL); 18499 } 18500 18501 18502 18503 /* 18504 * Function: sd_print_retry_msg 18505 * 18506 * Description: Print a message indicating the retry action being taken. 18507 * 18508 * Arguments: un - ptr to associated softstate 18509 * bp - ptr to buf(9S) for the command 18510 * arg - not used. 18511 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18512 * or SD_NO_RETRY_ISSUED 18513 * 18514 * Context: May be called from interrupt context 18515 */ 18516 /* ARGSUSED */ 18517 static void 18518 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18519 { 18520 struct sd_xbuf *xp; 18521 struct scsi_pkt *pktp; 18522 char *reasonp; 18523 char *msgp; 18524 18525 ASSERT(un != NULL); 18526 ASSERT(mutex_owned(SD_MUTEX(un))); 18527 ASSERT(bp != NULL); 18528 pktp = SD_GET_PKTP(bp); 18529 ASSERT(pktp != NULL); 18530 xp = SD_GET_XBUF(bp); 18531 ASSERT(xp != NULL); 18532 18533 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18534 mutex_enter(&un->un_pm_mutex); 18535 if ((un->un_state == SD_STATE_SUSPENDED) || 18536 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18537 (pktp->pkt_flags & FLAG_SILENT)) { 18538 mutex_exit(&un->un_pm_mutex); 18539 goto update_pkt_reason; 18540 } 18541 mutex_exit(&un->un_pm_mutex); 18542 18543 /* 18544 * Suppress messages if they are all the same pkt_reason; with 18545 * TQ, many (up to 256) are returned with the same pkt_reason. 18546 * If we are in panic, then suppress the retry messages. 18547 */ 18548 switch (flag) { 18549 case SD_NO_RETRY_ISSUED: 18550 msgp = "giving up"; 18551 break; 18552 case SD_IMMEDIATE_RETRY_ISSUED: 18553 case SD_DELAYED_RETRY_ISSUED: 18554 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18555 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18556 (sd_error_level != SCSI_ERR_ALL))) { 18557 return; 18558 } 18559 msgp = "retrying command"; 18560 break; 18561 default: 18562 goto update_pkt_reason; 18563 } 18564 18565 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18566 scsi_rname(pktp->pkt_reason)); 18567 18568 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18569 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18570 18571 update_pkt_reason: 18572 /* 18573 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18574 * This is to prevent multiple console messages for the same failure 18575 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18576 * when the command is retried successfully because there still may be 18577 * more commands coming back with the same value of pktp->pkt_reason. 18578 */ 18579 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18580 un->un_last_pkt_reason = pktp->pkt_reason; 18581 } 18582 } 18583 18584 18585 /* 18586 * Function: sd_print_cmd_incomplete_msg 18587 * 18588 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18589 * 18590 * Arguments: un - ptr to associated softstate 18591 * bp - ptr to buf(9S) for the command 18592 * arg - passed to sd_print_retry_msg() 18593 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18594 * or SD_NO_RETRY_ISSUED 18595 * 18596 * Context: May be called from interrupt context 18597 */ 18598 18599 static void 18600 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18601 int code) 18602 { 18603 dev_info_t *dip; 18604 18605 ASSERT(un != NULL); 18606 ASSERT(mutex_owned(SD_MUTEX(un))); 18607 ASSERT(bp != NULL); 18608 18609 switch (code) { 18610 case SD_NO_RETRY_ISSUED: 18611 /* Command was failed. Someone turned off this target? */ 18612 if (un->un_state != SD_STATE_OFFLINE) { 18613 /* 18614 * Suppress message if we are detaching and 18615 * device has been disconnected 18616 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18617 * private interface and not part of the DDI 18618 */ 18619 dip = un->un_sd->sd_dev; 18620 if (!(DEVI_IS_DETACHING(dip) && 18621 DEVI_IS_DEVICE_REMOVED(dip))) { 18622 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18623 "disk not responding to selection\n"); 18624 } 18625 New_state(un, SD_STATE_OFFLINE); 18626 } 18627 break; 18628 18629 case SD_DELAYED_RETRY_ISSUED: 18630 case SD_IMMEDIATE_RETRY_ISSUED: 18631 default: 18632 /* Command was successfully queued for retry */ 18633 sd_print_retry_msg(un, bp, arg, code); 18634 break; 18635 } 18636 } 18637 18638 18639 /* 18640 * Function: sd_pkt_reason_cmd_incomplete 18641 * 18642 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18643 * 18644 * Context: May be called from interrupt context 18645 */ 18646 18647 static void 18648 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18649 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18650 { 18651 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18652 18653 ASSERT(un != NULL); 18654 ASSERT(mutex_owned(SD_MUTEX(un))); 18655 ASSERT(bp != NULL); 18656 ASSERT(xp != NULL); 18657 ASSERT(pktp != NULL); 18658 18659 /* Do not do a reset if selection did not complete */ 18660 /* Note: Should this not just check the bit? */ 18661 if (pktp->pkt_state != STATE_GOT_BUS) { 18662 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18663 sd_reset_target(un, pktp); 18664 } 18665 18666 /* 18667 * If the target was not successfully selected, then set 18668 * SD_RETRIES_FAILFAST to indicate that we lost communication 18669 * with the target, and further retries and/or commands are 18670 * likely to take a long time. 18671 */ 18672 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18673 flag |= SD_RETRIES_FAILFAST; 18674 } 18675 18676 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18677 18678 sd_retry_command(un, bp, flag, 18679 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18680 } 18681 18682 18683 18684 /* 18685 * Function: sd_pkt_reason_cmd_tran_err 18686 * 18687 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18688 * 18689 * Context: May be called from interrupt context 18690 */ 18691 18692 static void 18693 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18694 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18695 { 18696 ASSERT(un != NULL); 18697 ASSERT(mutex_owned(SD_MUTEX(un))); 18698 ASSERT(bp != NULL); 18699 ASSERT(xp != NULL); 18700 ASSERT(pktp != NULL); 18701 18702 /* 18703 * Do not reset if we got a parity error, or if 18704 * selection did not complete. 18705 */ 18706 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18707 /* Note: Should this not just check the bit for pkt_state? */ 18708 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18709 (pktp->pkt_state != STATE_GOT_BUS)) { 18710 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18711 sd_reset_target(un, pktp); 18712 } 18713 18714 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18715 18716 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18717 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18718 } 18719 18720 18721 18722 /* 18723 * Function: sd_pkt_reason_cmd_reset 18724 * 18725 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18726 * 18727 * Context: May be called from interrupt context 18728 */ 18729 18730 static void 18731 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18732 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18733 { 18734 ASSERT(un != NULL); 18735 ASSERT(mutex_owned(SD_MUTEX(un))); 18736 ASSERT(bp != NULL); 18737 ASSERT(xp != NULL); 18738 ASSERT(pktp != NULL); 18739 18740 /* The target may still be running the command, so try to reset. */ 18741 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18742 sd_reset_target(un, pktp); 18743 18744 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18745 18746 /* 18747 * If pkt_reason is CMD_RESET chances are that this pkt got 18748 * reset because another target on this bus caused it. The target 18749 * that caused it should get CMD_TIMEOUT with pkt_statistics 18750 * of STAT_TIMEOUT/STAT_DEV_RESET. 18751 */ 18752 18753 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18754 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18755 } 18756 18757 18758 18759 18760 /* 18761 * Function: sd_pkt_reason_cmd_aborted 18762 * 18763 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18764 * 18765 * Context: May be called from interrupt context 18766 */ 18767 18768 static void 18769 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18770 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18771 { 18772 ASSERT(un != NULL); 18773 ASSERT(mutex_owned(SD_MUTEX(un))); 18774 ASSERT(bp != NULL); 18775 ASSERT(xp != NULL); 18776 ASSERT(pktp != NULL); 18777 18778 /* The target may still be running the command, so try to reset. */ 18779 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18780 sd_reset_target(un, pktp); 18781 18782 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18783 18784 /* 18785 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18786 * aborted because another target on this bus caused it. The target 18787 * that caused it should get CMD_TIMEOUT with pkt_statistics 18788 * of STAT_TIMEOUT/STAT_DEV_RESET. 18789 */ 18790 18791 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18792 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18793 } 18794 18795 18796 18797 /* 18798 * Function: sd_pkt_reason_cmd_timeout 18799 * 18800 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18801 * 18802 * Context: May be called from interrupt context 18803 */ 18804 18805 static void 18806 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18807 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18808 { 18809 ASSERT(un != NULL); 18810 ASSERT(mutex_owned(SD_MUTEX(un))); 18811 ASSERT(bp != NULL); 18812 ASSERT(xp != NULL); 18813 ASSERT(pktp != NULL); 18814 18815 18816 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18817 sd_reset_target(un, pktp); 18818 18819 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18820 18821 /* 18822 * A command timeout indicates that we could not establish 18823 * communication with the target, so set SD_RETRIES_FAILFAST 18824 * as further retries/commands are likely to take a long time. 18825 */ 18826 sd_retry_command(un, bp, 18827 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18828 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18829 } 18830 18831 18832 18833 /* 18834 * Function: sd_pkt_reason_cmd_unx_bus_free 18835 * 18836 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18837 * 18838 * Context: May be called from interrupt context 18839 */ 18840 18841 static void 18842 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18843 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18844 { 18845 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18846 18847 ASSERT(un != NULL); 18848 ASSERT(mutex_owned(SD_MUTEX(un))); 18849 ASSERT(bp != NULL); 18850 ASSERT(xp != NULL); 18851 ASSERT(pktp != NULL); 18852 18853 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18854 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18855 18856 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18857 sd_print_retry_msg : NULL; 18858 18859 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18860 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18861 } 18862 18863 18864 /* 18865 * Function: sd_pkt_reason_cmd_tag_reject 18866 * 18867 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18868 * 18869 * Context: May be called from interrupt context 18870 */ 18871 18872 static void 18873 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18874 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18875 { 18876 ASSERT(un != NULL); 18877 ASSERT(mutex_owned(SD_MUTEX(un))); 18878 ASSERT(bp != NULL); 18879 ASSERT(xp != NULL); 18880 ASSERT(pktp != NULL); 18881 18882 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18883 pktp->pkt_flags = 0; 18884 un->un_tagflags = 0; 18885 if (un->un_f_opt_queueing == TRUE) { 18886 un->un_throttle = min(un->un_throttle, 3); 18887 } else { 18888 un->un_throttle = 1; 18889 } 18890 mutex_exit(SD_MUTEX(un)); 18891 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18892 mutex_enter(SD_MUTEX(un)); 18893 18894 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18895 18896 /* Legacy behavior not to check retry counts here. */ 18897 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18898 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18899 } 18900 18901 18902 /* 18903 * Function: sd_pkt_reason_default 18904 * 18905 * Description: Default recovery actions for SCSA pkt_reason values that 18906 * do not have more explicit recovery actions. 18907 * 18908 * Context: May be called from interrupt context 18909 */ 18910 18911 static void 18912 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18913 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18914 { 18915 ASSERT(un != NULL); 18916 ASSERT(mutex_owned(SD_MUTEX(un))); 18917 ASSERT(bp != NULL); 18918 ASSERT(xp != NULL); 18919 ASSERT(pktp != NULL); 18920 18921 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18922 sd_reset_target(un, pktp); 18923 18924 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18925 18926 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18927 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18928 } 18929 18930 18931 18932 /* 18933 * Function: sd_pkt_status_check_condition 18934 * 18935 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18936 * 18937 * Context: May be called from interrupt context 18938 */ 18939 18940 static void 18941 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18942 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18943 { 18944 ASSERT(un != NULL); 18945 ASSERT(mutex_owned(SD_MUTEX(un))); 18946 ASSERT(bp != NULL); 18947 ASSERT(xp != NULL); 18948 ASSERT(pktp != NULL); 18949 18950 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18951 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18952 18953 /* 18954 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18955 * command will be retried after the request sense). Otherwise, retry 18956 * the command. Note: we are issuing the request sense even though the 18957 * retry limit may have been reached for the failed command. 18958 */ 18959 if (un->un_f_arq_enabled == FALSE) { 18960 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18961 "no ARQ, sending request sense command\n"); 18962 sd_send_request_sense_command(un, bp, pktp); 18963 } else { 18964 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18965 "ARQ,retrying request sense command\n"); 18966 #if defined(__i386) || defined(__amd64) 18967 /* 18968 * The SD_RETRY_DELAY value need to be adjusted here 18969 * when SD_RETRY_DELAY change in sddef.h 18970 */ 18971 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18972 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18973 NULL); 18974 #else 18975 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18976 EIO, SD_RETRY_DELAY, NULL); 18977 #endif 18978 } 18979 18980 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18981 } 18982 18983 18984 /* 18985 * Function: sd_pkt_status_busy 18986 * 18987 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18988 * 18989 * Context: May be called from interrupt context 18990 */ 18991 18992 static void 18993 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 18994 struct scsi_pkt *pktp) 18995 { 18996 ASSERT(un != NULL); 18997 ASSERT(mutex_owned(SD_MUTEX(un))); 18998 ASSERT(bp != NULL); 18999 ASSERT(xp != NULL); 19000 ASSERT(pktp != NULL); 19001 19002 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19003 "sd_pkt_status_busy: entry\n"); 19004 19005 /* If retries are exhausted, just fail the command. */ 19006 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19007 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19008 "device busy too long\n"); 19009 sd_return_failed_command(un, bp, EIO); 19010 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19011 "sd_pkt_status_busy: exit\n"); 19012 return; 19013 } 19014 xp->xb_retry_count++; 19015 19016 /* 19017 * Try to reset the target. However, we do not want to perform 19018 * more than one reset if the device continues to fail. The reset 19019 * will be performed when the retry count reaches the reset 19020 * threshold. This threshold should be set such that at least 19021 * one retry is issued before the reset is performed. 19022 */ 19023 if (xp->xb_retry_count == 19024 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19025 int rval = 0; 19026 mutex_exit(SD_MUTEX(un)); 19027 if (un->un_f_allow_bus_device_reset == TRUE) { 19028 /* 19029 * First try to reset the LUN; if we cannot then 19030 * try to reset the target. 19031 */ 19032 if (un->un_f_lun_reset_enabled == TRUE) { 19033 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19034 "sd_pkt_status_busy: RESET_LUN\n"); 19035 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19036 } 19037 if (rval == 0) { 19038 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19039 "sd_pkt_status_busy: RESET_TARGET\n"); 19040 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19041 } 19042 } 19043 if (rval == 0) { 19044 /* 19045 * If the RESET_LUN and/or RESET_TARGET failed, 19046 * try RESET_ALL 19047 */ 19048 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19049 "sd_pkt_status_busy: RESET_ALL\n"); 19050 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19051 } 19052 mutex_enter(SD_MUTEX(un)); 19053 if (rval == 0) { 19054 /* 19055 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19056 * At this point we give up & fail the command. 19057 */ 19058 sd_return_failed_command(un, bp, EIO); 19059 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19060 "sd_pkt_status_busy: exit (failed cmd)\n"); 19061 return; 19062 } 19063 } 19064 19065 /* 19066 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19067 * we have already checked the retry counts above. 19068 */ 19069 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19070 EIO, SD_BSY_TIMEOUT, NULL); 19071 19072 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19073 "sd_pkt_status_busy: exit\n"); 19074 } 19075 19076 19077 /* 19078 * Function: sd_pkt_status_reservation_conflict 19079 * 19080 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19081 * command status. 19082 * 19083 * Context: May be called from interrupt context 19084 */ 19085 19086 static void 19087 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19088 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19089 { 19090 ASSERT(un != NULL); 19091 ASSERT(mutex_owned(SD_MUTEX(un))); 19092 ASSERT(bp != NULL); 19093 ASSERT(xp != NULL); 19094 ASSERT(pktp != NULL); 19095 19096 /* 19097 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19098 * conflict could be due to various reasons like incorrect keys, not 19099 * registered or not reserved etc. So, we return EACCES to the caller. 19100 */ 19101 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19102 int cmd = SD_GET_PKT_OPCODE(pktp); 19103 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19104 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19105 sd_return_failed_command(un, bp, EACCES); 19106 return; 19107 } 19108 } 19109 19110 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19111 19112 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19113 if (sd_failfast_enable != 0) { 19114 /* By definition, we must panic here.... */ 19115 sd_panic_for_res_conflict(un); 19116 /*NOTREACHED*/ 19117 } 19118 SD_ERROR(SD_LOG_IO, un, 19119 "sd_handle_resv_conflict: Disk Reserved\n"); 19120 sd_return_failed_command(un, bp, EACCES); 19121 return; 19122 } 19123 19124 /* 19125 * 1147670: retry only if sd_retry_on_reservation_conflict 19126 * property is set (default is 1). Retries will not succeed 19127 * on a disk reserved by another initiator. HA systems 19128 * may reset this via sd.conf to avoid these retries. 19129 * 19130 * Note: The legacy return code for this failure is EIO, however EACCES 19131 * seems more appropriate for a reservation conflict. 19132 */ 19133 if (sd_retry_on_reservation_conflict == 0) { 19134 SD_ERROR(SD_LOG_IO, un, 19135 "sd_handle_resv_conflict: Device Reserved\n"); 19136 sd_return_failed_command(un, bp, EIO); 19137 return; 19138 } 19139 19140 /* 19141 * Retry the command if we can. 19142 * 19143 * Note: The legacy return code for this failure is EIO, however EACCES 19144 * seems more appropriate for a reservation conflict. 19145 */ 19146 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19147 (clock_t)2, NULL); 19148 } 19149 19150 19151 19152 /* 19153 * Function: sd_pkt_status_qfull 19154 * 19155 * Description: Handle a QUEUE FULL condition from the target. This can 19156 * occur if the HBA does not handle the queue full condition. 19157 * (Basically this means third-party HBAs as Sun HBAs will 19158 * handle the queue full condition.) Note that if there are 19159 * some commands already in the transport, then the queue full 19160 * has occurred because the queue for this nexus is actually 19161 * full. If there are no commands in the transport, then the 19162 * queue full is resulting from some other initiator or lun 19163 * consuming all the resources at the target. 19164 * 19165 * Context: May be called from interrupt context 19166 */ 19167 19168 static void 19169 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19170 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19171 { 19172 ASSERT(un != NULL); 19173 ASSERT(mutex_owned(SD_MUTEX(un))); 19174 ASSERT(bp != NULL); 19175 ASSERT(xp != NULL); 19176 ASSERT(pktp != NULL); 19177 19178 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19179 "sd_pkt_status_qfull: entry\n"); 19180 19181 /* 19182 * Just lower the QFULL throttle and retry the command. Note that 19183 * we do not limit the number of retries here. 19184 */ 19185 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19186 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19187 SD_RESTART_TIMEOUT, NULL); 19188 19189 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19190 "sd_pkt_status_qfull: exit\n"); 19191 } 19192 19193 19194 /* 19195 * Function: sd_reset_target 19196 * 19197 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19198 * RESET_TARGET, or RESET_ALL. 19199 * 19200 * Context: May be called under interrupt context. 19201 */ 19202 19203 static void 19204 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19205 { 19206 int rval = 0; 19207 19208 ASSERT(un != NULL); 19209 ASSERT(mutex_owned(SD_MUTEX(un))); 19210 ASSERT(pktp != NULL); 19211 19212 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19213 19214 /* 19215 * No need to reset if the transport layer has already done so. 19216 */ 19217 if ((pktp->pkt_statistics & 19218 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19219 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19220 "sd_reset_target: no reset\n"); 19221 return; 19222 } 19223 19224 mutex_exit(SD_MUTEX(un)); 19225 19226 if (un->un_f_allow_bus_device_reset == TRUE) { 19227 if (un->un_f_lun_reset_enabled == TRUE) { 19228 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19229 "sd_reset_target: RESET_LUN\n"); 19230 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19231 } 19232 if (rval == 0) { 19233 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19234 "sd_reset_target: RESET_TARGET\n"); 19235 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19236 } 19237 } 19238 19239 if (rval == 0) { 19240 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19241 "sd_reset_target: RESET_ALL\n"); 19242 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19243 } 19244 19245 mutex_enter(SD_MUTEX(un)); 19246 19247 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19248 } 19249 19250 19251 /* 19252 * Function: sd_media_change_task 19253 * 19254 * Description: Recovery action for CDROM to become available. 19255 * 19256 * Context: Executes in a taskq() thread context 19257 */ 19258 19259 static void 19260 sd_media_change_task(void *arg) 19261 { 19262 struct scsi_pkt *pktp = arg; 19263 struct sd_lun *un; 19264 struct buf *bp; 19265 struct sd_xbuf *xp; 19266 int err = 0; 19267 int retry_count = 0; 19268 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19269 struct sd_sense_info si; 19270 19271 ASSERT(pktp != NULL); 19272 bp = (struct buf *)pktp->pkt_private; 19273 ASSERT(bp != NULL); 19274 xp = SD_GET_XBUF(bp); 19275 ASSERT(xp != NULL); 19276 un = SD_GET_UN(bp); 19277 ASSERT(un != NULL); 19278 ASSERT(!mutex_owned(SD_MUTEX(un))); 19279 ASSERT(un->un_f_monitor_media_state); 19280 19281 si.ssi_severity = SCSI_ERR_INFO; 19282 si.ssi_pfa_flag = FALSE; 19283 19284 /* 19285 * When a reset is issued on a CDROM, it takes a long time to 19286 * recover. First few attempts to read capacity and other things 19287 * related to handling unit attention fail (with a ASC 0x4 and 19288 * ASCQ 0x1). In that case we want to do enough retries and we want 19289 * to limit the retries in other cases of genuine failures like 19290 * no media in drive. 19291 */ 19292 while (retry_count++ < retry_limit) { 19293 if ((err = sd_handle_mchange(un)) == 0) { 19294 break; 19295 } 19296 if (err == EAGAIN) { 19297 retry_limit = SD_UNIT_ATTENTION_RETRY; 19298 } 19299 /* Sleep for 0.5 sec. & try again */ 19300 delay(drv_usectohz(500000)); 19301 } 19302 19303 /* 19304 * Dispatch (retry or fail) the original command here, 19305 * along with appropriate console messages.... 19306 * 19307 * Must grab the mutex before calling sd_retry_command, 19308 * sd_print_sense_msg and sd_return_failed_command. 19309 */ 19310 mutex_enter(SD_MUTEX(un)); 19311 if (err != SD_CMD_SUCCESS) { 19312 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19313 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19314 si.ssi_severity = SCSI_ERR_FATAL; 19315 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19316 sd_return_failed_command(un, bp, EIO); 19317 } else { 19318 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19319 &si, EIO, (clock_t)0, NULL); 19320 } 19321 mutex_exit(SD_MUTEX(un)); 19322 } 19323 19324 19325 19326 /* 19327 * Function: sd_handle_mchange 19328 * 19329 * Description: Perform geometry validation & other recovery when CDROM 19330 * has been removed from drive. 19331 * 19332 * Return Code: 0 for success 19333 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19334 * sd_send_scsi_READ_CAPACITY() 19335 * 19336 * Context: Executes in a taskq() thread context 19337 */ 19338 19339 static int 19340 sd_handle_mchange(struct sd_lun *un) 19341 { 19342 uint64_t capacity; 19343 uint32_t lbasize; 19344 int rval; 19345 19346 ASSERT(!mutex_owned(SD_MUTEX(un))); 19347 ASSERT(un->un_f_monitor_media_state); 19348 19349 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 19350 SD_PATH_DIRECT_PRIORITY)) != 0) { 19351 return (rval); 19352 } 19353 19354 mutex_enter(SD_MUTEX(un)); 19355 sd_update_block_info(un, lbasize, capacity); 19356 19357 if (un->un_errstats != NULL) { 19358 struct sd_errstats *stp = 19359 (struct sd_errstats *)un->un_errstats->ks_data; 19360 stp->sd_capacity.value.ui64 = (uint64_t) 19361 ((uint64_t)un->un_blockcount * 19362 (uint64_t)un->un_tgt_blocksize); 19363 } 19364 19365 /* 19366 * Note: Maybe let the strategy/partitioning chain worry about getting 19367 * valid geometry. 19368 */ 19369 un->un_f_geometry_is_valid = FALSE; 19370 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 19371 if (un->un_f_geometry_is_valid == FALSE) { 19372 mutex_exit(SD_MUTEX(un)); 19373 return (EIO); 19374 } 19375 19376 mutex_exit(SD_MUTEX(un)); 19377 19378 /* 19379 * Try to lock the door 19380 */ 19381 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19382 SD_PATH_DIRECT_PRIORITY)); 19383 } 19384 19385 19386 /* 19387 * Function: sd_send_scsi_DOORLOCK 19388 * 19389 * Description: Issue the scsi DOOR LOCK command 19390 * 19391 * Arguments: un - pointer to driver soft state (unit) structure for 19392 * this target. 19393 * flag - SD_REMOVAL_ALLOW 19394 * SD_REMOVAL_PREVENT 19395 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19396 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19397 * to use the USCSI "direct" chain and bypass the normal 19398 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19399 * command is issued as part of an error recovery action. 19400 * 19401 * Return Code: 0 - Success 19402 * errno return code from sd_send_scsi_cmd() 19403 * 19404 * Context: Can sleep. 19405 */ 19406 19407 static int 19408 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19409 { 19410 union scsi_cdb cdb; 19411 struct uscsi_cmd ucmd_buf; 19412 struct scsi_extended_sense sense_buf; 19413 int status; 19414 19415 ASSERT(un != NULL); 19416 ASSERT(!mutex_owned(SD_MUTEX(un))); 19417 19418 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19419 19420 /* already determined doorlock is not supported, fake success */ 19421 if (un->un_f_doorlock_supported == FALSE) { 19422 return (0); 19423 } 19424 19425 bzero(&cdb, sizeof (cdb)); 19426 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19427 19428 cdb.scc_cmd = SCMD_DOORLOCK; 19429 cdb.cdb_opaque[4] = (uchar_t)flag; 19430 19431 ucmd_buf.uscsi_cdb = (char *)&cdb; 19432 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19433 ucmd_buf.uscsi_bufaddr = NULL; 19434 ucmd_buf.uscsi_buflen = 0; 19435 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19436 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19437 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19438 ucmd_buf.uscsi_timeout = 15; 19439 19440 SD_TRACE(SD_LOG_IO, un, 19441 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19442 19443 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19444 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19445 19446 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19447 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19448 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19449 /* fake success and skip subsequent doorlock commands */ 19450 un->un_f_doorlock_supported = FALSE; 19451 return (0); 19452 } 19453 19454 return (status); 19455 } 19456 19457 /* 19458 * Function: sd_send_scsi_READ_CAPACITY 19459 * 19460 * Description: This routine uses the scsi READ CAPACITY command to determine 19461 * the device capacity in number of blocks and the device native 19462 * block size. If this function returns a failure, then the 19463 * values in *capp and *lbap are undefined. If the capacity 19464 * returned is 0xffffffff then the lun is too large for a 19465 * normal READ CAPACITY command and the results of a 19466 * READ CAPACITY 16 will be used instead. 19467 * 19468 * Arguments: un - ptr to soft state struct for the target 19469 * capp - ptr to unsigned 64-bit variable to receive the 19470 * capacity value from the command. 19471 * lbap - ptr to unsigned 32-bit varaible to receive the 19472 * block size value from the command 19473 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19474 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19475 * to use the USCSI "direct" chain and bypass the normal 19476 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19477 * command is issued as part of an error recovery action. 19478 * 19479 * Return Code: 0 - Success 19480 * EIO - IO error 19481 * EACCES - Reservation conflict detected 19482 * EAGAIN - Device is becoming ready 19483 * errno return code from sd_send_scsi_cmd() 19484 * 19485 * Context: Can sleep. Blocks until command completes. 19486 */ 19487 19488 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19489 19490 static int 19491 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19492 int path_flag) 19493 { 19494 struct scsi_extended_sense sense_buf; 19495 struct uscsi_cmd ucmd_buf; 19496 union scsi_cdb cdb; 19497 uint32_t *capacity_buf; 19498 uint64_t capacity; 19499 uint32_t lbasize; 19500 int status; 19501 19502 ASSERT(un != NULL); 19503 ASSERT(!mutex_owned(SD_MUTEX(un))); 19504 ASSERT(capp != NULL); 19505 ASSERT(lbap != NULL); 19506 19507 SD_TRACE(SD_LOG_IO, un, 19508 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19509 19510 /* 19511 * First send a READ_CAPACITY command to the target. 19512 * (This command is mandatory under SCSI-2.) 19513 * 19514 * Set up the CDB for the READ_CAPACITY command. The Partial 19515 * Medium Indicator bit is cleared. The address field must be 19516 * zero if the PMI bit is zero. 19517 */ 19518 bzero(&cdb, sizeof (cdb)); 19519 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19520 19521 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19522 19523 cdb.scc_cmd = SCMD_READ_CAPACITY; 19524 19525 ucmd_buf.uscsi_cdb = (char *)&cdb; 19526 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19527 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19528 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19529 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19530 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19531 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19532 ucmd_buf.uscsi_timeout = 60; 19533 19534 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19535 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19536 19537 switch (status) { 19538 case 0: 19539 /* Return failure if we did not get valid capacity data. */ 19540 if (ucmd_buf.uscsi_resid != 0) { 19541 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19542 return (EIO); 19543 } 19544 19545 /* 19546 * Read capacity and block size from the READ CAPACITY 10 data. 19547 * This data may be adjusted later due to device specific 19548 * issues. 19549 * 19550 * According to the SCSI spec, the READ CAPACITY 10 19551 * command returns the following: 19552 * 19553 * bytes 0-3: Maximum logical block address available. 19554 * (MSB in byte:0 & LSB in byte:3) 19555 * 19556 * bytes 4-7: Block length in bytes 19557 * (MSB in byte:4 & LSB in byte:7) 19558 * 19559 */ 19560 capacity = BE_32(capacity_buf[0]); 19561 lbasize = BE_32(capacity_buf[1]); 19562 19563 /* 19564 * Done with capacity_buf 19565 */ 19566 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19567 19568 /* 19569 * if the reported capacity is set to all 0xf's, then 19570 * this disk is too large and requires SBC-2 commands. 19571 * Reissue the request using READ CAPACITY 16. 19572 */ 19573 if (capacity == 0xffffffff) { 19574 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19575 &lbasize, path_flag); 19576 if (status != 0) { 19577 return (status); 19578 } 19579 } 19580 break; /* Success! */ 19581 case EIO: 19582 switch (ucmd_buf.uscsi_status) { 19583 case STATUS_RESERVATION_CONFLICT: 19584 status = EACCES; 19585 break; 19586 case STATUS_CHECK: 19587 /* 19588 * Check condition; look for ASC/ASCQ of 0x04/0x01 19589 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19590 */ 19591 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19592 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19593 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19594 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19595 return (EAGAIN); 19596 } 19597 break; 19598 default: 19599 break; 19600 } 19601 /* FALLTHRU */ 19602 default: 19603 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19604 return (status); 19605 } 19606 19607 /* 19608 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19609 * (2352 and 0 are common) so for these devices always force the value 19610 * to 2048 as required by the ATAPI specs. 19611 */ 19612 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19613 lbasize = 2048; 19614 } 19615 19616 /* 19617 * Get the maximum LBA value from the READ CAPACITY data. 19618 * Here we assume that the Partial Medium Indicator (PMI) bit 19619 * was cleared when issuing the command. This means that the LBA 19620 * returned from the device is the LBA of the last logical block 19621 * on the logical unit. The actual logical block count will be 19622 * this value plus one. 19623 * 19624 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19625 * so scale the capacity value to reflect this. 19626 */ 19627 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19628 19629 #if defined(__i386) || defined(__amd64) 19630 /* 19631 * Refer to comments related to off-by-1 at the 19632 * header of this file. 19633 * Treat 1TB disk as (1T - 512)B. 19634 */ 19635 if (un->un_f_capacity_adjusted == 1) 19636 capacity = DK_MAX_BLOCKS; 19637 #endif 19638 19639 /* 19640 * Copy the values from the READ CAPACITY command into the space 19641 * provided by the caller. 19642 */ 19643 *capp = capacity; 19644 *lbap = lbasize; 19645 19646 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19647 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19648 19649 /* 19650 * Both the lbasize and capacity from the device must be nonzero, 19651 * otherwise we assume that the values are not valid and return 19652 * failure to the caller. (4203735) 19653 */ 19654 if ((capacity == 0) || (lbasize == 0)) { 19655 return (EIO); 19656 } 19657 19658 return (0); 19659 } 19660 19661 /* 19662 * Function: sd_send_scsi_READ_CAPACITY_16 19663 * 19664 * Description: This routine uses the scsi READ CAPACITY 16 command to 19665 * determine the device capacity in number of blocks and the 19666 * device native block size. If this function returns a failure, 19667 * then the values in *capp and *lbap are undefined. 19668 * This routine should always be called by 19669 * sd_send_scsi_READ_CAPACITY which will appy any device 19670 * specific adjustments to capacity and lbasize. 19671 * 19672 * Arguments: un - ptr to soft state struct for the target 19673 * capp - ptr to unsigned 64-bit variable to receive the 19674 * capacity value from the command. 19675 * lbap - ptr to unsigned 32-bit varaible to receive the 19676 * block size value from the command 19677 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19678 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19679 * to use the USCSI "direct" chain and bypass the normal 19680 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19681 * this command is issued as part of an error recovery 19682 * action. 19683 * 19684 * Return Code: 0 - Success 19685 * EIO - IO error 19686 * EACCES - Reservation conflict detected 19687 * EAGAIN - Device is becoming ready 19688 * errno return code from sd_send_scsi_cmd() 19689 * 19690 * Context: Can sleep. Blocks until command completes. 19691 */ 19692 19693 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19694 19695 static int 19696 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19697 uint32_t *lbap, int path_flag) 19698 { 19699 struct scsi_extended_sense sense_buf; 19700 struct uscsi_cmd ucmd_buf; 19701 union scsi_cdb cdb; 19702 uint64_t *capacity16_buf; 19703 uint64_t capacity; 19704 uint32_t lbasize; 19705 int status; 19706 19707 ASSERT(un != NULL); 19708 ASSERT(!mutex_owned(SD_MUTEX(un))); 19709 ASSERT(capp != NULL); 19710 ASSERT(lbap != NULL); 19711 19712 SD_TRACE(SD_LOG_IO, un, 19713 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19714 19715 /* 19716 * First send a READ_CAPACITY_16 command to the target. 19717 * 19718 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19719 * Medium Indicator bit is cleared. The address field must be 19720 * zero if the PMI bit is zero. 19721 */ 19722 bzero(&cdb, sizeof (cdb)); 19723 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19724 19725 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19726 19727 ucmd_buf.uscsi_cdb = (char *)&cdb; 19728 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19729 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19730 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19731 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19732 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19733 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19734 ucmd_buf.uscsi_timeout = 60; 19735 19736 /* 19737 * Read Capacity (16) is a Service Action In command. One 19738 * command byte (0x9E) is overloaded for multiple operations, 19739 * with the second CDB byte specifying the desired operation 19740 */ 19741 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19742 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19743 19744 /* 19745 * Fill in allocation length field 19746 */ 19747 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19748 19749 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19750 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19751 19752 switch (status) { 19753 case 0: 19754 /* Return failure if we did not get valid capacity data. */ 19755 if (ucmd_buf.uscsi_resid > 20) { 19756 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19757 return (EIO); 19758 } 19759 19760 /* 19761 * Read capacity and block size from the READ CAPACITY 10 data. 19762 * This data may be adjusted later due to device specific 19763 * issues. 19764 * 19765 * According to the SCSI spec, the READ CAPACITY 10 19766 * command returns the following: 19767 * 19768 * bytes 0-7: Maximum logical block address available. 19769 * (MSB in byte:0 & LSB in byte:7) 19770 * 19771 * bytes 8-11: Block length in bytes 19772 * (MSB in byte:8 & LSB in byte:11) 19773 * 19774 */ 19775 capacity = BE_64(capacity16_buf[0]); 19776 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19777 19778 /* 19779 * Done with capacity16_buf 19780 */ 19781 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19782 19783 /* 19784 * if the reported capacity is set to all 0xf's, then 19785 * this disk is too large. This could only happen with 19786 * a device that supports LBAs larger than 64 bits which 19787 * are not defined by any current T10 standards. 19788 */ 19789 if (capacity == 0xffffffffffffffff) { 19790 return (EIO); 19791 } 19792 break; /* Success! */ 19793 case EIO: 19794 switch (ucmd_buf.uscsi_status) { 19795 case STATUS_RESERVATION_CONFLICT: 19796 status = EACCES; 19797 break; 19798 case STATUS_CHECK: 19799 /* 19800 * Check condition; look for ASC/ASCQ of 0x04/0x01 19801 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19802 */ 19803 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19804 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19805 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19806 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19807 return (EAGAIN); 19808 } 19809 break; 19810 default: 19811 break; 19812 } 19813 /* FALLTHRU */ 19814 default: 19815 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19816 return (status); 19817 } 19818 19819 *capp = capacity; 19820 *lbap = lbasize; 19821 19822 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19823 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19824 19825 return (0); 19826 } 19827 19828 19829 /* 19830 * Function: sd_send_scsi_START_STOP_UNIT 19831 * 19832 * Description: Issue a scsi START STOP UNIT command to the target. 19833 * 19834 * Arguments: un - pointer to driver soft state (unit) structure for 19835 * this target. 19836 * flag - SD_TARGET_START 19837 * SD_TARGET_STOP 19838 * SD_TARGET_EJECT 19839 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19840 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19841 * to use the USCSI "direct" chain and bypass the normal 19842 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19843 * command is issued as part of an error recovery action. 19844 * 19845 * Return Code: 0 - Success 19846 * EIO - IO error 19847 * EACCES - Reservation conflict detected 19848 * ENXIO - Not Ready, medium not present 19849 * errno return code from sd_send_scsi_cmd() 19850 * 19851 * Context: Can sleep. 19852 */ 19853 19854 static int 19855 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19856 { 19857 struct scsi_extended_sense sense_buf; 19858 union scsi_cdb cdb; 19859 struct uscsi_cmd ucmd_buf; 19860 int status; 19861 19862 ASSERT(un != NULL); 19863 ASSERT(!mutex_owned(SD_MUTEX(un))); 19864 19865 SD_TRACE(SD_LOG_IO, un, 19866 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19867 19868 if (un->un_f_check_start_stop && 19869 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19870 (un->un_f_start_stop_supported != TRUE)) { 19871 return (0); 19872 } 19873 19874 bzero(&cdb, sizeof (cdb)); 19875 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19876 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19877 19878 cdb.scc_cmd = SCMD_START_STOP; 19879 cdb.cdb_opaque[4] = (uchar_t)flag; 19880 19881 ucmd_buf.uscsi_cdb = (char *)&cdb; 19882 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19883 ucmd_buf.uscsi_bufaddr = NULL; 19884 ucmd_buf.uscsi_buflen = 0; 19885 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19886 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19887 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19888 ucmd_buf.uscsi_timeout = 200; 19889 19890 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19891 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19892 19893 switch (status) { 19894 case 0: 19895 break; /* Success! */ 19896 case EIO: 19897 switch (ucmd_buf.uscsi_status) { 19898 case STATUS_RESERVATION_CONFLICT: 19899 status = EACCES; 19900 break; 19901 case STATUS_CHECK: 19902 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19903 switch (scsi_sense_key( 19904 (uint8_t *)&sense_buf)) { 19905 case KEY_ILLEGAL_REQUEST: 19906 status = ENOTSUP; 19907 break; 19908 case KEY_NOT_READY: 19909 if (scsi_sense_asc( 19910 (uint8_t *)&sense_buf) 19911 == 0x3A) { 19912 status = ENXIO; 19913 } 19914 break; 19915 default: 19916 break; 19917 } 19918 } 19919 break; 19920 default: 19921 break; 19922 } 19923 break; 19924 default: 19925 break; 19926 } 19927 19928 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19929 19930 return (status); 19931 } 19932 19933 19934 /* 19935 * Function: sd_start_stop_unit_callback 19936 * 19937 * Description: timeout(9F) callback to begin recovery process for a 19938 * device that has spun down. 19939 * 19940 * Arguments: arg - pointer to associated softstate struct. 19941 * 19942 * Context: Executes in a timeout(9F) thread context 19943 */ 19944 19945 static void 19946 sd_start_stop_unit_callback(void *arg) 19947 { 19948 struct sd_lun *un = arg; 19949 ASSERT(un != NULL); 19950 ASSERT(!mutex_owned(SD_MUTEX(un))); 19951 19952 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19953 19954 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19955 } 19956 19957 19958 /* 19959 * Function: sd_start_stop_unit_task 19960 * 19961 * Description: Recovery procedure when a drive is spun down. 19962 * 19963 * Arguments: arg - pointer to associated softstate struct. 19964 * 19965 * Context: Executes in a taskq() thread context 19966 */ 19967 19968 static void 19969 sd_start_stop_unit_task(void *arg) 19970 { 19971 struct sd_lun *un = arg; 19972 19973 ASSERT(un != NULL); 19974 ASSERT(!mutex_owned(SD_MUTEX(un))); 19975 19976 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19977 19978 /* 19979 * Some unformatted drives report not ready error, no need to 19980 * restart if format has been initiated. 19981 */ 19982 mutex_enter(SD_MUTEX(un)); 19983 if (un->un_f_format_in_progress == TRUE) { 19984 mutex_exit(SD_MUTEX(un)); 19985 return; 19986 } 19987 mutex_exit(SD_MUTEX(un)); 19988 19989 /* 19990 * When a START STOP command is issued from here, it is part of a 19991 * failure recovery operation and must be issued before any other 19992 * commands, including any pending retries. Thus it must be sent 19993 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 19994 * succeeds or not, we will start I/O after the attempt. 19995 */ 19996 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 19997 SD_PATH_DIRECT_PRIORITY); 19998 19999 /* 20000 * The above call blocks until the START_STOP_UNIT command completes. 20001 * Now that it has completed, we must re-try the original IO that 20002 * received the NOT READY condition in the first place. There are 20003 * three possible conditions here: 20004 * 20005 * (1) The original IO is on un_retry_bp. 20006 * (2) The original IO is on the regular wait queue, and un_retry_bp 20007 * is NULL. 20008 * (3) The original IO is on the regular wait queue, and un_retry_bp 20009 * points to some other, unrelated bp. 20010 * 20011 * For each case, we must call sd_start_cmds() with un_retry_bp 20012 * as the argument. If un_retry_bp is NULL, this will initiate 20013 * processing of the regular wait queue. If un_retry_bp is not NULL, 20014 * then this will process the bp on un_retry_bp. That may or may not 20015 * be the original IO, but that does not matter: the important thing 20016 * is to keep the IO processing going at this point. 20017 * 20018 * Note: This is a very specific error recovery sequence associated 20019 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20020 * serialize the I/O with completion of the spin-up. 20021 */ 20022 mutex_enter(SD_MUTEX(un)); 20023 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20024 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20025 un, un->un_retry_bp); 20026 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20027 sd_start_cmds(un, un->un_retry_bp); 20028 mutex_exit(SD_MUTEX(un)); 20029 20030 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20031 } 20032 20033 20034 /* 20035 * Function: sd_send_scsi_INQUIRY 20036 * 20037 * Description: Issue the scsi INQUIRY command. 20038 * 20039 * Arguments: un 20040 * bufaddr 20041 * buflen 20042 * evpd 20043 * page_code 20044 * page_length 20045 * 20046 * Return Code: 0 - Success 20047 * errno return code from sd_send_scsi_cmd() 20048 * 20049 * Context: Can sleep. Does not return until command is completed. 20050 */ 20051 20052 static int 20053 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 20054 uchar_t evpd, uchar_t page_code, size_t *residp) 20055 { 20056 union scsi_cdb cdb; 20057 struct uscsi_cmd ucmd_buf; 20058 int status; 20059 20060 ASSERT(un != NULL); 20061 ASSERT(!mutex_owned(SD_MUTEX(un))); 20062 ASSERT(bufaddr != NULL); 20063 20064 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20065 20066 bzero(&cdb, sizeof (cdb)); 20067 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20068 bzero(bufaddr, buflen); 20069 20070 cdb.scc_cmd = SCMD_INQUIRY; 20071 cdb.cdb_opaque[1] = evpd; 20072 cdb.cdb_opaque[2] = page_code; 20073 FORMG0COUNT(&cdb, buflen); 20074 20075 ucmd_buf.uscsi_cdb = (char *)&cdb; 20076 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20077 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20078 ucmd_buf.uscsi_buflen = buflen; 20079 ucmd_buf.uscsi_rqbuf = NULL; 20080 ucmd_buf.uscsi_rqlen = 0; 20081 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20082 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20083 20084 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20085 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 20086 20087 if ((status == 0) && (residp != NULL)) { 20088 *residp = ucmd_buf.uscsi_resid; 20089 } 20090 20091 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20092 20093 return (status); 20094 } 20095 20096 20097 /* 20098 * Function: sd_send_scsi_TEST_UNIT_READY 20099 * 20100 * Description: Issue the scsi TEST UNIT READY command. 20101 * This routine can be told to set the flag USCSI_DIAGNOSE to 20102 * prevent retrying failed commands. Use this when the intent 20103 * is either to check for device readiness, to clear a Unit 20104 * Attention, or to clear any outstanding sense data. 20105 * However under specific conditions the expected behavior 20106 * is for retries to bring a device ready, so use the flag 20107 * with caution. 20108 * 20109 * Arguments: un 20110 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20111 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20112 * 0: dont check for media present, do retries on cmd. 20113 * 20114 * Return Code: 0 - Success 20115 * EIO - IO error 20116 * EACCES - Reservation conflict detected 20117 * ENXIO - Not Ready, medium not present 20118 * errno return code from sd_send_scsi_cmd() 20119 * 20120 * Context: Can sleep. Does not return until command is completed. 20121 */ 20122 20123 static int 20124 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 20125 { 20126 struct scsi_extended_sense sense_buf; 20127 union scsi_cdb cdb; 20128 struct uscsi_cmd ucmd_buf; 20129 int status; 20130 20131 ASSERT(un != NULL); 20132 ASSERT(!mutex_owned(SD_MUTEX(un))); 20133 20134 SD_TRACE(SD_LOG_IO, un, 20135 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20136 20137 /* 20138 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20139 * timeouts when they receive a TUR and the queue is not empty. Check 20140 * the configuration flag set during attach (indicating the drive has 20141 * this firmware bug) and un_ncmds_in_transport before issuing the 20142 * TUR. If there are 20143 * pending commands return success, this is a bit arbitrary but is ok 20144 * for non-removables (i.e. the eliteI disks) and non-clustering 20145 * configurations. 20146 */ 20147 if (un->un_f_cfg_tur_check == TRUE) { 20148 mutex_enter(SD_MUTEX(un)); 20149 if (un->un_ncmds_in_transport != 0) { 20150 mutex_exit(SD_MUTEX(un)); 20151 return (0); 20152 } 20153 mutex_exit(SD_MUTEX(un)); 20154 } 20155 20156 bzero(&cdb, sizeof (cdb)); 20157 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20158 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20159 20160 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20161 20162 ucmd_buf.uscsi_cdb = (char *)&cdb; 20163 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20164 ucmd_buf.uscsi_bufaddr = NULL; 20165 ucmd_buf.uscsi_buflen = 0; 20166 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20167 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20168 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20169 20170 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20171 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20172 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20173 } 20174 ucmd_buf.uscsi_timeout = 60; 20175 20176 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20177 UIO_SYSSPACE, UIO_SYSSPACE, 20178 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 20179 20180 switch (status) { 20181 case 0: 20182 break; /* Success! */ 20183 case EIO: 20184 switch (ucmd_buf.uscsi_status) { 20185 case STATUS_RESERVATION_CONFLICT: 20186 status = EACCES; 20187 break; 20188 case STATUS_CHECK: 20189 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20190 break; 20191 } 20192 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20193 (scsi_sense_key((uint8_t *)&sense_buf) == 20194 KEY_NOT_READY) && 20195 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20196 status = ENXIO; 20197 } 20198 break; 20199 default: 20200 break; 20201 } 20202 break; 20203 default: 20204 break; 20205 } 20206 20207 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20208 20209 return (status); 20210 } 20211 20212 20213 /* 20214 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20215 * 20216 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20217 * 20218 * Arguments: un 20219 * 20220 * Return Code: 0 - Success 20221 * EACCES 20222 * ENOTSUP 20223 * errno return code from sd_send_scsi_cmd() 20224 * 20225 * Context: Can sleep. Does not return until command is completed. 20226 */ 20227 20228 static int 20229 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 20230 uint16_t data_len, uchar_t *data_bufp) 20231 { 20232 struct scsi_extended_sense sense_buf; 20233 union scsi_cdb cdb; 20234 struct uscsi_cmd ucmd_buf; 20235 int status; 20236 int no_caller_buf = FALSE; 20237 20238 ASSERT(un != NULL); 20239 ASSERT(!mutex_owned(SD_MUTEX(un))); 20240 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20241 20242 SD_TRACE(SD_LOG_IO, un, 20243 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20244 20245 bzero(&cdb, sizeof (cdb)); 20246 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20247 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20248 if (data_bufp == NULL) { 20249 /* Allocate a default buf if the caller did not give one */ 20250 ASSERT(data_len == 0); 20251 data_len = MHIOC_RESV_KEY_SIZE; 20252 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20253 no_caller_buf = TRUE; 20254 } 20255 20256 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20257 cdb.cdb_opaque[1] = usr_cmd; 20258 FORMG1COUNT(&cdb, data_len); 20259 20260 ucmd_buf.uscsi_cdb = (char *)&cdb; 20261 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20262 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20263 ucmd_buf.uscsi_buflen = data_len; 20264 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20265 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20266 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20267 ucmd_buf.uscsi_timeout = 60; 20268 20269 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20270 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20271 20272 switch (status) { 20273 case 0: 20274 break; /* Success! */ 20275 case EIO: 20276 switch (ucmd_buf.uscsi_status) { 20277 case STATUS_RESERVATION_CONFLICT: 20278 status = EACCES; 20279 break; 20280 case STATUS_CHECK: 20281 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20282 (scsi_sense_key((uint8_t *)&sense_buf) == 20283 KEY_ILLEGAL_REQUEST)) { 20284 status = ENOTSUP; 20285 } 20286 break; 20287 default: 20288 break; 20289 } 20290 break; 20291 default: 20292 break; 20293 } 20294 20295 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20296 20297 if (no_caller_buf == TRUE) { 20298 kmem_free(data_bufp, data_len); 20299 } 20300 20301 return (status); 20302 } 20303 20304 20305 /* 20306 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20307 * 20308 * Description: This routine is the driver entry point for handling CD-ROM 20309 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20310 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20311 * device. 20312 * 20313 * Arguments: un - Pointer to soft state struct for the target. 20314 * usr_cmd SCSI-3 reservation facility command (one of 20315 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20316 * SD_SCSI3_PREEMPTANDABORT) 20317 * usr_bufp - user provided pointer register, reserve descriptor or 20318 * preempt and abort structure (mhioc_register_t, 20319 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20320 * 20321 * Return Code: 0 - Success 20322 * EACCES 20323 * ENOTSUP 20324 * errno return code from sd_send_scsi_cmd() 20325 * 20326 * Context: Can sleep. Does not return until command is completed. 20327 */ 20328 20329 static int 20330 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 20331 uchar_t *usr_bufp) 20332 { 20333 struct scsi_extended_sense sense_buf; 20334 union scsi_cdb cdb; 20335 struct uscsi_cmd ucmd_buf; 20336 int status; 20337 uchar_t data_len = sizeof (sd_prout_t); 20338 sd_prout_t *prp; 20339 20340 ASSERT(un != NULL); 20341 ASSERT(!mutex_owned(SD_MUTEX(un))); 20342 ASSERT(data_len == 24); /* required by scsi spec */ 20343 20344 SD_TRACE(SD_LOG_IO, un, 20345 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20346 20347 if (usr_bufp == NULL) { 20348 return (EINVAL); 20349 } 20350 20351 bzero(&cdb, sizeof (cdb)); 20352 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20353 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20354 prp = kmem_zalloc(data_len, KM_SLEEP); 20355 20356 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20357 cdb.cdb_opaque[1] = usr_cmd; 20358 FORMG1COUNT(&cdb, data_len); 20359 20360 ucmd_buf.uscsi_cdb = (char *)&cdb; 20361 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20362 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20363 ucmd_buf.uscsi_buflen = data_len; 20364 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20365 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20366 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20367 ucmd_buf.uscsi_timeout = 60; 20368 20369 switch (usr_cmd) { 20370 case SD_SCSI3_REGISTER: { 20371 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20372 20373 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20374 bcopy(ptr->newkey.key, prp->service_key, 20375 MHIOC_RESV_KEY_SIZE); 20376 prp->aptpl = ptr->aptpl; 20377 break; 20378 } 20379 case SD_SCSI3_RESERVE: 20380 case SD_SCSI3_RELEASE: { 20381 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20382 20383 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20384 prp->scope_address = BE_32(ptr->scope_specific_addr); 20385 cdb.cdb_opaque[2] = ptr->type; 20386 break; 20387 } 20388 case SD_SCSI3_PREEMPTANDABORT: { 20389 mhioc_preemptandabort_t *ptr = 20390 (mhioc_preemptandabort_t *)usr_bufp; 20391 20392 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20393 bcopy(ptr->victim_key.key, prp->service_key, 20394 MHIOC_RESV_KEY_SIZE); 20395 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20396 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20397 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20398 break; 20399 } 20400 case SD_SCSI3_REGISTERANDIGNOREKEY: 20401 { 20402 mhioc_registerandignorekey_t *ptr; 20403 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20404 bcopy(ptr->newkey.key, 20405 prp->service_key, MHIOC_RESV_KEY_SIZE); 20406 prp->aptpl = ptr->aptpl; 20407 break; 20408 } 20409 default: 20410 ASSERT(FALSE); 20411 break; 20412 } 20413 20414 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20415 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20416 20417 switch (status) { 20418 case 0: 20419 break; /* Success! */ 20420 case EIO: 20421 switch (ucmd_buf.uscsi_status) { 20422 case STATUS_RESERVATION_CONFLICT: 20423 status = EACCES; 20424 break; 20425 case STATUS_CHECK: 20426 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20427 (scsi_sense_key((uint8_t *)&sense_buf) == 20428 KEY_ILLEGAL_REQUEST)) { 20429 status = ENOTSUP; 20430 } 20431 break; 20432 default: 20433 break; 20434 } 20435 break; 20436 default: 20437 break; 20438 } 20439 20440 kmem_free(prp, data_len); 20441 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20442 return (status); 20443 } 20444 20445 20446 /* 20447 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20448 * 20449 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20450 * 20451 * Arguments: un - pointer to the target's soft state struct 20452 * 20453 * Return Code: 0 - success 20454 * errno-type error code 20455 * 20456 * Context: kernel thread context only. 20457 */ 20458 20459 static int 20460 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20461 { 20462 struct sd_uscsi_info *uip; 20463 struct uscsi_cmd *uscmd; 20464 union scsi_cdb *cdb; 20465 struct buf *bp; 20466 int rval = 0; 20467 20468 SD_TRACE(SD_LOG_IO, un, 20469 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20470 20471 ASSERT(un != NULL); 20472 ASSERT(!mutex_owned(SD_MUTEX(un))); 20473 20474 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20475 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20476 20477 /* 20478 * First get some memory for the uscsi_cmd struct and cdb 20479 * and initialize for SYNCHRONIZE_CACHE cmd. 20480 */ 20481 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20482 uscmd->uscsi_cdblen = CDB_GROUP1; 20483 uscmd->uscsi_cdb = (caddr_t)cdb; 20484 uscmd->uscsi_bufaddr = NULL; 20485 uscmd->uscsi_buflen = 0; 20486 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20487 uscmd->uscsi_rqlen = SENSE_LENGTH; 20488 uscmd->uscsi_rqresid = SENSE_LENGTH; 20489 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20490 uscmd->uscsi_timeout = sd_io_time; 20491 20492 /* 20493 * Allocate an sd_uscsi_info struct and fill it with the info 20494 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20495 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20496 * since we allocate the buf here in this function, we do not 20497 * need to preserve the prior contents of b_private. 20498 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20499 */ 20500 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20501 uip->ui_flags = SD_PATH_DIRECT; 20502 uip->ui_cmdp = uscmd; 20503 20504 bp = getrbuf(KM_SLEEP); 20505 bp->b_private = uip; 20506 20507 /* 20508 * Setup buffer to carry uscsi request. 20509 */ 20510 bp->b_flags = B_BUSY; 20511 bp->b_bcount = 0; 20512 bp->b_blkno = 0; 20513 20514 if (dkc != NULL) { 20515 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20516 uip->ui_dkc = *dkc; 20517 } 20518 20519 bp->b_edev = SD_GET_DEV(un); 20520 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20521 20522 (void) sd_uscsi_strategy(bp); 20523 20524 /* 20525 * If synchronous request, wait for completion 20526 * If async just return and let b_iodone callback 20527 * cleanup. 20528 * NOTE: On return, u_ncmds_in_driver will be decremented, 20529 * but it was also incremented in sd_uscsi_strategy(), so 20530 * we should be ok. 20531 */ 20532 if (dkc == NULL) { 20533 (void) biowait(bp); 20534 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20535 } 20536 20537 return (rval); 20538 } 20539 20540 20541 static int 20542 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20543 { 20544 struct sd_uscsi_info *uip; 20545 struct uscsi_cmd *uscmd; 20546 uint8_t *sense_buf; 20547 struct sd_lun *un; 20548 int status; 20549 20550 uip = (struct sd_uscsi_info *)(bp->b_private); 20551 ASSERT(uip != NULL); 20552 20553 uscmd = uip->ui_cmdp; 20554 ASSERT(uscmd != NULL); 20555 20556 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20557 ASSERT(sense_buf != NULL); 20558 20559 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20560 ASSERT(un != NULL); 20561 20562 status = geterror(bp); 20563 switch (status) { 20564 case 0: 20565 break; /* Success! */ 20566 case EIO: 20567 switch (uscmd->uscsi_status) { 20568 case STATUS_RESERVATION_CONFLICT: 20569 /* Ignore reservation conflict */ 20570 status = 0; 20571 goto done; 20572 20573 case STATUS_CHECK: 20574 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20575 (scsi_sense_key(sense_buf) == 20576 KEY_ILLEGAL_REQUEST)) { 20577 /* Ignore Illegal Request error */ 20578 mutex_enter(SD_MUTEX(un)); 20579 un->un_f_sync_cache_supported = FALSE; 20580 mutex_exit(SD_MUTEX(un)); 20581 status = ENOTSUP; 20582 goto done; 20583 } 20584 break; 20585 default: 20586 break; 20587 } 20588 /* FALLTHRU */ 20589 default: 20590 /* 20591 * Don't log an error message if this device 20592 * has removable media. 20593 */ 20594 if (!un->un_f_has_removable_media) { 20595 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20596 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20597 } 20598 break; 20599 } 20600 20601 done: 20602 if (uip->ui_dkc.dkc_callback != NULL) { 20603 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20604 } 20605 20606 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20607 freerbuf(bp); 20608 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20609 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20610 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20611 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20612 20613 return (status); 20614 } 20615 20616 20617 /* 20618 * Function: sd_send_scsi_GET_CONFIGURATION 20619 * 20620 * Description: Issues the get configuration command to the device. 20621 * Called from sd_check_for_writable_cd & sd_get_media_info 20622 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20623 * Arguments: un 20624 * ucmdbuf 20625 * rqbuf 20626 * rqbuflen 20627 * bufaddr 20628 * buflen 20629 * 20630 * Return Code: 0 - Success 20631 * errno return code from sd_send_scsi_cmd() 20632 * 20633 * Context: Can sleep. Does not return until command is completed. 20634 * 20635 */ 20636 20637 static int 20638 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20639 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20640 { 20641 char cdb[CDB_GROUP1]; 20642 int status; 20643 20644 ASSERT(un != NULL); 20645 ASSERT(!mutex_owned(SD_MUTEX(un))); 20646 ASSERT(bufaddr != NULL); 20647 ASSERT(ucmdbuf != NULL); 20648 ASSERT(rqbuf != NULL); 20649 20650 SD_TRACE(SD_LOG_IO, un, 20651 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20652 20653 bzero(cdb, sizeof (cdb)); 20654 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20655 bzero(rqbuf, rqbuflen); 20656 bzero(bufaddr, buflen); 20657 20658 /* 20659 * Set up cdb field for the get configuration command. 20660 */ 20661 cdb[0] = SCMD_GET_CONFIGURATION; 20662 cdb[1] = 0x02; /* Requested Type */ 20663 cdb[8] = SD_PROFILE_HEADER_LEN; 20664 ucmdbuf->uscsi_cdb = cdb; 20665 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20666 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20667 ucmdbuf->uscsi_buflen = buflen; 20668 ucmdbuf->uscsi_timeout = sd_io_time; 20669 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20670 ucmdbuf->uscsi_rqlen = rqbuflen; 20671 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20672 20673 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20674 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20675 20676 switch (status) { 20677 case 0: 20678 break; /* Success! */ 20679 case EIO: 20680 switch (ucmdbuf->uscsi_status) { 20681 case STATUS_RESERVATION_CONFLICT: 20682 status = EACCES; 20683 break; 20684 default: 20685 break; 20686 } 20687 break; 20688 default: 20689 break; 20690 } 20691 20692 if (status == 0) { 20693 SD_DUMP_MEMORY(un, SD_LOG_IO, 20694 "sd_send_scsi_GET_CONFIGURATION: data", 20695 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20696 } 20697 20698 SD_TRACE(SD_LOG_IO, un, 20699 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20700 20701 return (status); 20702 } 20703 20704 /* 20705 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20706 * 20707 * Description: Issues the get configuration command to the device to 20708 * retrieve a specfic feature. Called from 20709 * sd_check_for_writable_cd & sd_set_mmc_caps. 20710 * Arguments: un 20711 * ucmdbuf 20712 * rqbuf 20713 * rqbuflen 20714 * bufaddr 20715 * buflen 20716 * feature 20717 * 20718 * Return Code: 0 - Success 20719 * errno return code from sd_send_scsi_cmd() 20720 * 20721 * Context: Can sleep. Does not return until command is completed. 20722 * 20723 */ 20724 static int 20725 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20726 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20727 uchar_t *bufaddr, uint_t buflen, char feature) 20728 { 20729 char cdb[CDB_GROUP1]; 20730 int status; 20731 20732 ASSERT(un != NULL); 20733 ASSERT(!mutex_owned(SD_MUTEX(un))); 20734 ASSERT(bufaddr != NULL); 20735 ASSERT(ucmdbuf != NULL); 20736 ASSERT(rqbuf != NULL); 20737 20738 SD_TRACE(SD_LOG_IO, un, 20739 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20740 20741 bzero(cdb, sizeof (cdb)); 20742 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20743 bzero(rqbuf, rqbuflen); 20744 bzero(bufaddr, buflen); 20745 20746 /* 20747 * Set up cdb field for the get configuration command. 20748 */ 20749 cdb[0] = SCMD_GET_CONFIGURATION; 20750 cdb[1] = 0x02; /* Requested Type */ 20751 cdb[3] = feature; 20752 cdb[8] = buflen; 20753 ucmdbuf->uscsi_cdb = cdb; 20754 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20755 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20756 ucmdbuf->uscsi_buflen = buflen; 20757 ucmdbuf->uscsi_timeout = sd_io_time; 20758 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20759 ucmdbuf->uscsi_rqlen = rqbuflen; 20760 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20761 20762 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20763 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20764 20765 switch (status) { 20766 case 0: 20767 break; /* Success! */ 20768 case EIO: 20769 switch (ucmdbuf->uscsi_status) { 20770 case STATUS_RESERVATION_CONFLICT: 20771 status = EACCES; 20772 break; 20773 default: 20774 break; 20775 } 20776 break; 20777 default: 20778 break; 20779 } 20780 20781 if (status == 0) { 20782 SD_DUMP_MEMORY(un, SD_LOG_IO, 20783 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20784 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20785 } 20786 20787 SD_TRACE(SD_LOG_IO, un, 20788 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20789 20790 return (status); 20791 } 20792 20793 20794 /* 20795 * Function: sd_send_scsi_MODE_SENSE 20796 * 20797 * Description: Utility function for issuing a scsi MODE SENSE command. 20798 * Note: This routine uses a consistent implementation for Group0, 20799 * Group1, and Group2 commands across all platforms. ATAPI devices 20800 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20801 * 20802 * Arguments: un - pointer to the softstate struct for the target. 20803 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20804 * CDB_GROUP[1|2] (10 byte). 20805 * bufaddr - buffer for page data retrieved from the target. 20806 * buflen - size of page to be retrieved. 20807 * page_code - page code of data to be retrieved from the target. 20808 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20809 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20810 * to use the USCSI "direct" chain and bypass the normal 20811 * command waitq. 20812 * 20813 * Return Code: 0 - Success 20814 * errno return code from sd_send_scsi_cmd() 20815 * 20816 * Context: Can sleep. Does not return until command is completed. 20817 */ 20818 20819 static int 20820 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20821 size_t buflen, uchar_t page_code, int path_flag) 20822 { 20823 struct scsi_extended_sense sense_buf; 20824 union scsi_cdb cdb; 20825 struct uscsi_cmd ucmd_buf; 20826 int status; 20827 int headlen; 20828 20829 ASSERT(un != NULL); 20830 ASSERT(!mutex_owned(SD_MUTEX(un))); 20831 ASSERT(bufaddr != NULL); 20832 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20833 (cdbsize == CDB_GROUP2)); 20834 20835 SD_TRACE(SD_LOG_IO, un, 20836 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20837 20838 bzero(&cdb, sizeof (cdb)); 20839 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20840 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20841 bzero(bufaddr, buflen); 20842 20843 if (cdbsize == CDB_GROUP0) { 20844 cdb.scc_cmd = SCMD_MODE_SENSE; 20845 cdb.cdb_opaque[2] = page_code; 20846 FORMG0COUNT(&cdb, buflen); 20847 headlen = MODE_HEADER_LENGTH; 20848 } else { 20849 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20850 cdb.cdb_opaque[2] = page_code; 20851 FORMG1COUNT(&cdb, buflen); 20852 headlen = MODE_HEADER_LENGTH_GRP2; 20853 } 20854 20855 ASSERT(headlen <= buflen); 20856 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20857 20858 ucmd_buf.uscsi_cdb = (char *)&cdb; 20859 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20860 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20861 ucmd_buf.uscsi_buflen = buflen; 20862 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20863 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20864 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20865 ucmd_buf.uscsi_timeout = 60; 20866 20867 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20868 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20869 20870 switch (status) { 20871 case 0: 20872 /* 20873 * sr_check_wp() uses 0x3f page code and check the header of 20874 * mode page to determine if target device is write-protected. 20875 * But some USB devices return 0 bytes for 0x3f page code. For 20876 * this case, make sure that mode page header is returned at 20877 * least. 20878 */ 20879 if (buflen - ucmd_buf.uscsi_resid < headlen) 20880 status = EIO; 20881 break; /* Success! */ 20882 case EIO: 20883 switch (ucmd_buf.uscsi_status) { 20884 case STATUS_RESERVATION_CONFLICT: 20885 status = EACCES; 20886 break; 20887 default: 20888 break; 20889 } 20890 break; 20891 default: 20892 break; 20893 } 20894 20895 if (status == 0) { 20896 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20897 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20898 } 20899 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20900 20901 return (status); 20902 } 20903 20904 20905 /* 20906 * Function: sd_send_scsi_MODE_SELECT 20907 * 20908 * Description: Utility function for issuing a scsi MODE SELECT command. 20909 * Note: This routine uses a consistent implementation for Group0, 20910 * Group1, and Group2 commands across all platforms. ATAPI devices 20911 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20912 * 20913 * Arguments: un - pointer to the softstate struct for the target. 20914 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20915 * CDB_GROUP[1|2] (10 byte). 20916 * bufaddr - buffer for page data retrieved from the target. 20917 * buflen - size of page to be retrieved. 20918 * save_page - boolean to determin if SP bit should be set. 20919 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20920 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20921 * to use the USCSI "direct" chain and bypass the normal 20922 * command waitq. 20923 * 20924 * Return Code: 0 - Success 20925 * errno return code from sd_send_scsi_cmd() 20926 * 20927 * Context: Can sleep. Does not return until command is completed. 20928 */ 20929 20930 static int 20931 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20932 size_t buflen, uchar_t save_page, int path_flag) 20933 { 20934 struct scsi_extended_sense sense_buf; 20935 union scsi_cdb cdb; 20936 struct uscsi_cmd ucmd_buf; 20937 int status; 20938 20939 ASSERT(un != NULL); 20940 ASSERT(!mutex_owned(SD_MUTEX(un))); 20941 ASSERT(bufaddr != NULL); 20942 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20943 (cdbsize == CDB_GROUP2)); 20944 20945 SD_TRACE(SD_LOG_IO, un, 20946 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20947 20948 bzero(&cdb, sizeof (cdb)); 20949 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20950 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20951 20952 /* Set the PF bit for many third party drives */ 20953 cdb.cdb_opaque[1] = 0x10; 20954 20955 /* Set the savepage(SP) bit if given */ 20956 if (save_page == SD_SAVE_PAGE) { 20957 cdb.cdb_opaque[1] |= 0x01; 20958 } 20959 20960 if (cdbsize == CDB_GROUP0) { 20961 cdb.scc_cmd = SCMD_MODE_SELECT; 20962 FORMG0COUNT(&cdb, buflen); 20963 } else { 20964 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20965 FORMG1COUNT(&cdb, buflen); 20966 } 20967 20968 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20969 20970 ucmd_buf.uscsi_cdb = (char *)&cdb; 20971 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20972 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20973 ucmd_buf.uscsi_buflen = buflen; 20974 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20975 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20976 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20977 ucmd_buf.uscsi_timeout = 60; 20978 20979 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20980 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20981 20982 switch (status) { 20983 case 0: 20984 break; /* Success! */ 20985 case EIO: 20986 switch (ucmd_buf.uscsi_status) { 20987 case STATUS_RESERVATION_CONFLICT: 20988 status = EACCES; 20989 break; 20990 default: 20991 break; 20992 } 20993 break; 20994 default: 20995 break; 20996 } 20997 20998 if (status == 0) { 20999 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21000 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21001 } 21002 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21003 21004 return (status); 21005 } 21006 21007 21008 /* 21009 * Function: sd_send_scsi_RDWR 21010 * 21011 * Description: Issue a scsi READ or WRITE command with the given parameters. 21012 * 21013 * Arguments: un: Pointer to the sd_lun struct for the target. 21014 * cmd: SCMD_READ or SCMD_WRITE 21015 * bufaddr: Address of caller's buffer to receive the RDWR data 21016 * buflen: Length of caller's buffer receive the RDWR data. 21017 * start_block: Block number for the start of the RDWR operation. 21018 * (Assumes target-native block size.) 21019 * residp: Pointer to variable to receive the redisual of the 21020 * RDWR operation (may be NULL of no residual requested). 21021 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21022 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21023 * to use the USCSI "direct" chain and bypass the normal 21024 * command waitq. 21025 * 21026 * Return Code: 0 - Success 21027 * errno return code from sd_send_scsi_cmd() 21028 * 21029 * Context: Can sleep. Does not return until command is completed. 21030 */ 21031 21032 static int 21033 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 21034 size_t buflen, daddr_t start_block, int path_flag) 21035 { 21036 struct scsi_extended_sense sense_buf; 21037 union scsi_cdb cdb; 21038 struct uscsi_cmd ucmd_buf; 21039 uint32_t block_count; 21040 int status; 21041 int cdbsize; 21042 uchar_t flag; 21043 21044 ASSERT(un != NULL); 21045 ASSERT(!mutex_owned(SD_MUTEX(un))); 21046 ASSERT(bufaddr != NULL); 21047 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21048 21049 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21050 21051 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21052 return (EINVAL); 21053 } 21054 21055 mutex_enter(SD_MUTEX(un)); 21056 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21057 mutex_exit(SD_MUTEX(un)); 21058 21059 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21060 21061 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21062 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21063 bufaddr, buflen, start_block, block_count); 21064 21065 bzero(&cdb, sizeof (cdb)); 21066 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21067 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21068 21069 /* Compute CDB size to use */ 21070 if (start_block > 0xffffffff) 21071 cdbsize = CDB_GROUP4; 21072 else if ((start_block & 0xFFE00000) || 21073 (un->un_f_cfg_is_atapi == TRUE)) 21074 cdbsize = CDB_GROUP1; 21075 else 21076 cdbsize = CDB_GROUP0; 21077 21078 switch (cdbsize) { 21079 case CDB_GROUP0: /* 6-byte CDBs */ 21080 cdb.scc_cmd = cmd; 21081 FORMG0ADDR(&cdb, start_block); 21082 FORMG0COUNT(&cdb, block_count); 21083 break; 21084 case CDB_GROUP1: /* 10-byte CDBs */ 21085 cdb.scc_cmd = cmd | SCMD_GROUP1; 21086 FORMG1ADDR(&cdb, start_block); 21087 FORMG1COUNT(&cdb, block_count); 21088 break; 21089 case CDB_GROUP4: /* 16-byte CDBs */ 21090 cdb.scc_cmd = cmd | SCMD_GROUP4; 21091 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21092 FORMG4COUNT(&cdb, block_count); 21093 break; 21094 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21095 default: 21096 /* All others reserved */ 21097 return (EINVAL); 21098 } 21099 21100 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21101 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21102 21103 ucmd_buf.uscsi_cdb = (char *)&cdb; 21104 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21105 ucmd_buf.uscsi_bufaddr = bufaddr; 21106 ucmd_buf.uscsi_buflen = buflen; 21107 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21108 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21109 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21110 ucmd_buf.uscsi_timeout = 60; 21111 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21112 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21113 switch (status) { 21114 case 0: 21115 break; /* Success! */ 21116 case EIO: 21117 switch (ucmd_buf.uscsi_status) { 21118 case STATUS_RESERVATION_CONFLICT: 21119 status = EACCES; 21120 break; 21121 default: 21122 break; 21123 } 21124 break; 21125 default: 21126 break; 21127 } 21128 21129 if (status == 0) { 21130 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21131 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21132 } 21133 21134 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21135 21136 return (status); 21137 } 21138 21139 21140 /* 21141 * Function: sd_send_scsi_LOG_SENSE 21142 * 21143 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21144 * 21145 * Arguments: un: Pointer to the sd_lun struct for the target. 21146 * 21147 * Return Code: 0 - Success 21148 * errno return code from sd_send_scsi_cmd() 21149 * 21150 * Context: Can sleep. Does not return until command is completed. 21151 */ 21152 21153 static int 21154 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 21155 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21156 int path_flag) 21157 21158 { 21159 struct scsi_extended_sense sense_buf; 21160 union scsi_cdb cdb; 21161 struct uscsi_cmd ucmd_buf; 21162 int status; 21163 21164 ASSERT(un != NULL); 21165 ASSERT(!mutex_owned(SD_MUTEX(un))); 21166 21167 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21168 21169 bzero(&cdb, sizeof (cdb)); 21170 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21171 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21172 21173 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21174 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21175 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21176 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21177 FORMG1COUNT(&cdb, buflen); 21178 21179 ucmd_buf.uscsi_cdb = (char *)&cdb; 21180 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21181 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21182 ucmd_buf.uscsi_buflen = buflen; 21183 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21184 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21185 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21186 ucmd_buf.uscsi_timeout = 60; 21187 21188 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21189 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21190 21191 switch (status) { 21192 case 0: 21193 break; 21194 case EIO: 21195 switch (ucmd_buf.uscsi_status) { 21196 case STATUS_RESERVATION_CONFLICT: 21197 status = EACCES; 21198 break; 21199 case STATUS_CHECK: 21200 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21201 (scsi_sense_key((uint8_t *)&sense_buf) == 21202 KEY_ILLEGAL_REQUEST) && 21203 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21204 /* 21205 * ASC 0x24: INVALID FIELD IN CDB 21206 */ 21207 switch (page_code) { 21208 case START_STOP_CYCLE_PAGE: 21209 /* 21210 * The start stop cycle counter is 21211 * implemented as page 0x31 in earlier 21212 * generation disks. In new generation 21213 * disks the start stop cycle counter is 21214 * implemented as page 0xE. To properly 21215 * handle this case if an attempt for 21216 * log page 0xE is made and fails we 21217 * will try again using page 0x31. 21218 * 21219 * Network storage BU committed to 21220 * maintain the page 0x31 for this 21221 * purpose and will not have any other 21222 * page implemented with page code 0x31 21223 * until all disks transition to the 21224 * standard page. 21225 */ 21226 mutex_enter(SD_MUTEX(un)); 21227 un->un_start_stop_cycle_page = 21228 START_STOP_CYCLE_VU_PAGE; 21229 cdb.cdb_opaque[2] = 21230 (char)(page_control << 6) | 21231 un->un_start_stop_cycle_page; 21232 mutex_exit(SD_MUTEX(un)); 21233 status = sd_send_scsi_cmd( 21234 SD_GET_DEV(un), &ucmd_buf, 21235 UIO_SYSSPACE, UIO_SYSSPACE, 21236 UIO_SYSSPACE, path_flag); 21237 21238 break; 21239 case TEMPERATURE_PAGE: 21240 status = ENOTTY; 21241 break; 21242 default: 21243 break; 21244 } 21245 } 21246 break; 21247 default: 21248 break; 21249 } 21250 break; 21251 default: 21252 break; 21253 } 21254 21255 if (status == 0) { 21256 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21257 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21258 } 21259 21260 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21261 21262 return (status); 21263 } 21264 21265 21266 /* 21267 * Function: sdioctl 21268 * 21269 * Description: Driver's ioctl(9e) entry point function. 21270 * 21271 * Arguments: dev - device number 21272 * cmd - ioctl operation to be performed 21273 * arg - user argument, contains data to be set or reference 21274 * parameter for get 21275 * flag - bit flag, indicating open settings, 32/64 bit type 21276 * cred_p - user credential pointer 21277 * rval_p - calling process return value (OPT) 21278 * 21279 * Return Code: EINVAL 21280 * ENOTTY 21281 * ENXIO 21282 * EIO 21283 * EFAULT 21284 * ENOTSUP 21285 * EPERM 21286 * 21287 * Context: Called from the device switch at normal priority. 21288 */ 21289 21290 static int 21291 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 21292 { 21293 struct sd_lun *un = NULL; 21294 int geom_validated = FALSE; 21295 int err = 0; 21296 int i = 0; 21297 cred_t *cr; 21298 21299 /* 21300 * All device accesses go thru sdstrategy where we check on suspend 21301 * status 21302 */ 21303 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21304 return (ENXIO); 21305 } 21306 21307 ASSERT(!mutex_owned(SD_MUTEX(un))); 21308 21309 /* 21310 * Moved this wait from sd_uscsi_strategy to here for 21311 * reasons of deadlock prevention. Internal driver commands, 21312 * specifically those to change a devices power level, result 21313 * in a call to sd_uscsi_strategy. 21314 */ 21315 mutex_enter(SD_MUTEX(un)); 21316 while ((un->un_state == SD_STATE_SUSPENDED) || 21317 (un->un_state == SD_STATE_PM_CHANGING)) { 21318 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 21319 } 21320 /* 21321 * Twiddling the counter here protects commands from now 21322 * through to the top of sd_uscsi_strategy. Without the 21323 * counter inc. a power down, for example, could get in 21324 * after the above check for state is made and before 21325 * execution gets to the top of sd_uscsi_strategy. 21326 * That would cause problems. 21327 */ 21328 un->un_ncmds_in_driver++; 21329 21330 if ((un->un_f_geometry_is_valid == FALSE) && 21331 (flag & (FNDELAY | FNONBLOCK))) { 21332 switch (cmd) { 21333 case CDROMPAUSE: 21334 case CDROMRESUME: 21335 case CDROMPLAYMSF: 21336 case CDROMPLAYTRKIND: 21337 case CDROMREADTOCHDR: 21338 case CDROMREADTOCENTRY: 21339 case CDROMSTOP: 21340 case CDROMSTART: 21341 case CDROMVOLCTRL: 21342 case CDROMSUBCHNL: 21343 case CDROMREADMODE2: 21344 case CDROMREADMODE1: 21345 case CDROMREADOFFSET: 21346 case CDROMSBLKMODE: 21347 case CDROMGBLKMODE: 21348 case CDROMGDRVSPEED: 21349 case CDROMSDRVSPEED: 21350 case CDROMCDDA: 21351 case CDROMCDXA: 21352 case CDROMSUBCODE: 21353 if (!ISCD(un)) { 21354 un->un_ncmds_in_driver--; 21355 ASSERT(un->un_ncmds_in_driver >= 0); 21356 mutex_exit(SD_MUTEX(un)); 21357 return (ENOTTY); 21358 } 21359 break; 21360 case FDEJECT: 21361 case DKIOCEJECT: 21362 case CDROMEJECT: 21363 if (!un->un_f_eject_media_supported) { 21364 un->un_ncmds_in_driver--; 21365 ASSERT(un->un_ncmds_in_driver >= 0); 21366 mutex_exit(SD_MUTEX(un)); 21367 return (ENOTTY); 21368 } 21369 break; 21370 case DKIOCSVTOC: 21371 case DKIOCSETEFI: 21372 case DKIOCSMBOOT: 21373 case DKIOCFLUSHWRITECACHE: 21374 mutex_exit(SD_MUTEX(un)); 21375 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 21376 if (err != 0) { 21377 mutex_enter(SD_MUTEX(un)); 21378 un->un_ncmds_in_driver--; 21379 ASSERT(un->un_ncmds_in_driver >= 0); 21380 mutex_exit(SD_MUTEX(un)); 21381 return (EIO); 21382 } 21383 mutex_enter(SD_MUTEX(un)); 21384 /* FALLTHROUGH */ 21385 case DKIOCREMOVABLE: 21386 case DKIOCHOTPLUGGABLE: 21387 case DKIOCINFO: 21388 case DKIOCGMEDIAINFO: 21389 case MHIOCENFAILFAST: 21390 case MHIOCSTATUS: 21391 case MHIOCTKOWN: 21392 case MHIOCRELEASE: 21393 case MHIOCGRP_INKEYS: 21394 case MHIOCGRP_INRESV: 21395 case MHIOCGRP_REGISTER: 21396 case MHIOCGRP_RESERVE: 21397 case MHIOCGRP_PREEMPTANDABORT: 21398 case MHIOCGRP_REGISTERANDIGNOREKEY: 21399 case CDROMCLOSETRAY: 21400 case USCSICMD: 21401 goto skip_ready_valid; 21402 default: 21403 break; 21404 } 21405 21406 mutex_exit(SD_MUTEX(un)); 21407 err = sd_ready_and_valid(un); 21408 mutex_enter(SD_MUTEX(un)); 21409 if (err == SD_READY_NOT_VALID) { 21410 switch (cmd) { 21411 case DKIOCGAPART: 21412 case DKIOCGGEOM: 21413 case DKIOCSGEOM: 21414 case DKIOCGVTOC: 21415 case DKIOCSVTOC: 21416 case DKIOCSAPART: 21417 case DKIOCG_PHYGEOM: 21418 case DKIOCG_VIRTGEOM: 21419 err = ENOTSUP; 21420 un->un_ncmds_in_driver--; 21421 ASSERT(un->un_ncmds_in_driver >= 0); 21422 mutex_exit(SD_MUTEX(un)); 21423 return (err); 21424 } 21425 } 21426 if (err != SD_READY_VALID) { 21427 switch (cmd) { 21428 case DKIOCSTATE: 21429 case CDROMGDRVSPEED: 21430 case CDROMSDRVSPEED: 21431 case FDEJECT: /* for eject command */ 21432 case DKIOCEJECT: 21433 case CDROMEJECT: 21434 case DKIOCGETEFI: 21435 case DKIOCSGEOM: 21436 case DKIOCREMOVABLE: 21437 case DKIOCHOTPLUGGABLE: 21438 case DKIOCSAPART: 21439 case DKIOCSETEFI: 21440 break; 21441 default: 21442 if (un->un_f_has_removable_media) { 21443 err = ENXIO; 21444 } else { 21445 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 21446 if (err == SD_RESERVED_BY_OTHERS) { 21447 err = EACCES; 21448 } else { 21449 err = EIO; 21450 } 21451 } 21452 un->un_ncmds_in_driver--; 21453 ASSERT(un->un_ncmds_in_driver >= 0); 21454 mutex_exit(SD_MUTEX(un)); 21455 return (err); 21456 } 21457 } 21458 geom_validated = TRUE; 21459 } 21460 if ((un->un_f_geometry_is_valid == TRUE) && 21461 (un->un_solaris_size > 0)) { 21462 /* 21463 * the "geometry_is_valid" flag could be true if we 21464 * have an fdisk table but no Solaris partition 21465 */ 21466 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21467 /* it is EFI, so return ENOTSUP for these */ 21468 switch (cmd) { 21469 case DKIOCGAPART: 21470 case DKIOCGGEOM: 21471 case DKIOCGVTOC: 21472 case DKIOCSVTOC: 21473 case DKIOCSAPART: 21474 err = ENOTSUP; 21475 un->un_ncmds_in_driver--; 21476 ASSERT(un->un_ncmds_in_driver >= 0); 21477 mutex_exit(SD_MUTEX(un)); 21478 return (err); 21479 } 21480 } 21481 } 21482 21483 skip_ready_valid: 21484 mutex_exit(SD_MUTEX(un)); 21485 21486 switch (cmd) { 21487 case DKIOCINFO: 21488 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21489 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21490 break; 21491 21492 case DKIOCGMEDIAINFO: 21493 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21494 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21495 break; 21496 21497 case DKIOCGGEOM: 21498 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21499 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21500 geom_validated); 21501 break; 21502 21503 case DKIOCSGEOM: 21504 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21505 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21506 break; 21507 21508 case DKIOCGAPART: 21509 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21510 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21511 geom_validated); 21512 break; 21513 21514 case DKIOCSAPART: 21515 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21516 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21517 break; 21518 21519 case DKIOCGVTOC: 21520 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21521 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21522 geom_validated); 21523 break; 21524 21525 case DKIOCGETEFI: 21526 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21527 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21528 break; 21529 21530 case DKIOCPARTITION: 21531 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21532 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21533 break; 21534 21535 case DKIOCSVTOC: 21536 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21537 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21538 break; 21539 21540 case DKIOCSETEFI: 21541 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21542 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21543 break; 21544 21545 case DKIOCGMBOOT: 21546 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21547 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21548 break; 21549 21550 case DKIOCSMBOOT: 21551 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21552 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21553 break; 21554 21555 case DKIOCLOCK: 21556 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21557 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21558 SD_PATH_STANDARD); 21559 break; 21560 21561 case DKIOCUNLOCK: 21562 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21563 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21564 SD_PATH_STANDARD); 21565 break; 21566 21567 case DKIOCSTATE: { 21568 enum dkio_state state; 21569 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21570 21571 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21572 err = EFAULT; 21573 } else { 21574 err = sd_check_media(dev, state); 21575 if (err == 0) { 21576 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21577 sizeof (int), flag) != 0) 21578 err = EFAULT; 21579 } 21580 } 21581 break; 21582 } 21583 21584 case DKIOCREMOVABLE: 21585 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21586 i = un->un_f_has_removable_media ? 1 : 0; 21587 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21588 err = EFAULT; 21589 } else { 21590 err = 0; 21591 } 21592 break; 21593 21594 case DKIOCHOTPLUGGABLE: 21595 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21596 i = un->un_f_is_hotpluggable ? 1 : 0; 21597 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21598 err = EFAULT; 21599 } else { 21600 err = 0; 21601 } 21602 break; 21603 21604 case DKIOCGTEMPERATURE: 21605 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21606 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21607 break; 21608 21609 case MHIOCENFAILFAST: 21610 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21611 if ((err = drv_priv(cred_p)) == 0) { 21612 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21613 } 21614 break; 21615 21616 case MHIOCTKOWN: 21617 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21618 if ((err = drv_priv(cred_p)) == 0) { 21619 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21620 } 21621 break; 21622 21623 case MHIOCRELEASE: 21624 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21625 if ((err = drv_priv(cred_p)) == 0) { 21626 err = sd_mhdioc_release(dev); 21627 } 21628 break; 21629 21630 case MHIOCSTATUS: 21631 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21632 if ((err = drv_priv(cred_p)) == 0) { 21633 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21634 case 0: 21635 err = 0; 21636 break; 21637 case EACCES: 21638 *rval_p = 1; 21639 err = 0; 21640 break; 21641 default: 21642 err = EIO; 21643 break; 21644 } 21645 } 21646 break; 21647 21648 case MHIOCQRESERVE: 21649 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21650 if ((err = drv_priv(cred_p)) == 0) { 21651 err = sd_reserve_release(dev, SD_RESERVE); 21652 } 21653 break; 21654 21655 case MHIOCREREGISTERDEVID: 21656 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21657 if (drv_priv(cred_p) == EPERM) { 21658 err = EPERM; 21659 } else if (!un->un_f_devid_supported) { 21660 err = ENOTTY; 21661 } else { 21662 err = sd_mhdioc_register_devid(dev); 21663 } 21664 break; 21665 21666 case MHIOCGRP_INKEYS: 21667 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21668 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21669 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21670 err = ENOTSUP; 21671 } else { 21672 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21673 flag); 21674 } 21675 } 21676 break; 21677 21678 case MHIOCGRP_INRESV: 21679 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21680 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21681 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21682 err = ENOTSUP; 21683 } else { 21684 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21685 } 21686 } 21687 break; 21688 21689 case MHIOCGRP_REGISTER: 21690 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21691 if ((err = drv_priv(cred_p)) != EPERM) { 21692 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21693 err = ENOTSUP; 21694 } else if (arg != NULL) { 21695 mhioc_register_t reg; 21696 if (ddi_copyin((void *)arg, ®, 21697 sizeof (mhioc_register_t), flag) != 0) { 21698 err = EFAULT; 21699 } else { 21700 err = 21701 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21702 un, SD_SCSI3_REGISTER, 21703 (uchar_t *)®); 21704 } 21705 } 21706 } 21707 break; 21708 21709 case MHIOCGRP_RESERVE: 21710 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21711 if ((err = drv_priv(cred_p)) != EPERM) { 21712 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21713 err = ENOTSUP; 21714 } else if (arg != NULL) { 21715 mhioc_resv_desc_t resv_desc; 21716 if (ddi_copyin((void *)arg, &resv_desc, 21717 sizeof (mhioc_resv_desc_t), flag) != 0) { 21718 err = EFAULT; 21719 } else { 21720 err = 21721 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21722 un, SD_SCSI3_RESERVE, 21723 (uchar_t *)&resv_desc); 21724 } 21725 } 21726 } 21727 break; 21728 21729 case MHIOCGRP_PREEMPTANDABORT: 21730 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21731 if ((err = drv_priv(cred_p)) != EPERM) { 21732 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21733 err = ENOTSUP; 21734 } else if (arg != NULL) { 21735 mhioc_preemptandabort_t preempt_abort; 21736 if (ddi_copyin((void *)arg, &preempt_abort, 21737 sizeof (mhioc_preemptandabort_t), 21738 flag) != 0) { 21739 err = EFAULT; 21740 } else { 21741 err = 21742 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21743 un, SD_SCSI3_PREEMPTANDABORT, 21744 (uchar_t *)&preempt_abort); 21745 } 21746 } 21747 } 21748 break; 21749 21750 case MHIOCGRP_REGISTERANDIGNOREKEY: 21751 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21752 if ((err = drv_priv(cred_p)) != EPERM) { 21753 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21754 err = ENOTSUP; 21755 } else if (arg != NULL) { 21756 mhioc_registerandignorekey_t r_and_i; 21757 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21758 sizeof (mhioc_registerandignorekey_t), 21759 flag) != 0) { 21760 err = EFAULT; 21761 } else { 21762 err = 21763 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21764 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21765 (uchar_t *)&r_and_i); 21766 } 21767 } 21768 } 21769 break; 21770 21771 case USCSICMD: 21772 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21773 cr = ddi_get_cred(); 21774 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21775 err = EPERM; 21776 } else { 21777 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21778 } 21779 break; 21780 21781 case CDROMPAUSE: 21782 case CDROMRESUME: 21783 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21784 if (!ISCD(un)) { 21785 err = ENOTTY; 21786 } else { 21787 err = sr_pause_resume(dev, cmd); 21788 } 21789 break; 21790 21791 case CDROMPLAYMSF: 21792 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21793 if (!ISCD(un)) { 21794 err = ENOTTY; 21795 } else { 21796 err = sr_play_msf(dev, (caddr_t)arg, flag); 21797 } 21798 break; 21799 21800 case CDROMPLAYTRKIND: 21801 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21802 #if defined(__i386) || defined(__amd64) 21803 /* 21804 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21805 */ 21806 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21807 #else 21808 if (!ISCD(un)) { 21809 #endif 21810 err = ENOTTY; 21811 } else { 21812 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21813 } 21814 break; 21815 21816 case CDROMREADTOCHDR: 21817 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21818 if (!ISCD(un)) { 21819 err = ENOTTY; 21820 } else { 21821 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21822 } 21823 break; 21824 21825 case CDROMREADTOCENTRY: 21826 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21827 if (!ISCD(un)) { 21828 err = ENOTTY; 21829 } else { 21830 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21831 } 21832 break; 21833 21834 case CDROMSTOP: 21835 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21836 if (!ISCD(un)) { 21837 err = ENOTTY; 21838 } else { 21839 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21840 SD_PATH_STANDARD); 21841 } 21842 break; 21843 21844 case CDROMSTART: 21845 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21846 if (!ISCD(un)) { 21847 err = ENOTTY; 21848 } else { 21849 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21850 SD_PATH_STANDARD); 21851 } 21852 break; 21853 21854 case CDROMCLOSETRAY: 21855 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21856 if (!ISCD(un)) { 21857 err = ENOTTY; 21858 } else { 21859 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21860 SD_PATH_STANDARD); 21861 } 21862 break; 21863 21864 case FDEJECT: /* for eject command */ 21865 case DKIOCEJECT: 21866 case CDROMEJECT: 21867 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21868 if (!un->un_f_eject_media_supported) { 21869 err = ENOTTY; 21870 } else { 21871 err = sr_eject(dev); 21872 } 21873 break; 21874 21875 case CDROMVOLCTRL: 21876 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21877 if (!ISCD(un)) { 21878 err = ENOTTY; 21879 } else { 21880 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21881 } 21882 break; 21883 21884 case CDROMSUBCHNL: 21885 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21886 if (!ISCD(un)) { 21887 err = ENOTTY; 21888 } else { 21889 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21890 } 21891 break; 21892 21893 case CDROMREADMODE2: 21894 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21895 if (!ISCD(un)) { 21896 err = ENOTTY; 21897 } else if (un->un_f_cfg_is_atapi == TRUE) { 21898 /* 21899 * If the drive supports READ CD, use that instead of 21900 * switching the LBA size via a MODE SELECT 21901 * Block Descriptor 21902 */ 21903 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21904 } else { 21905 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21906 } 21907 break; 21908 21909 case CDROMREADMODE1: 21910 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21911 if (!ISCD(un)) { 21912 err = ENOTTY; 21913 } else { 21914 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21915 } 21916 break; 21917 21918 case CDROMREADOFFSET: 21919 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21920 if (!ISCD(un)) { 21921 err = ENOTTY; 21922 } else { 21923 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21924 flag); 21925 } 21926 break; 21927 21928 case CDROMSBLKMODE: 21929 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21930 /* 21931 * There is no means of changing block size in case of atapi 21932 * drives, thus return ENOTTY if drive type is atapi 21933 */ 21934 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21935 err = ENOTTY; 21936 } else if (un->un_f_mmc_cap == TRUE) { 21937 21938 /* 21939 * MMC Devices do not support changing the 21940 * logical block size 21941 * 21942 * Note: EINVAL is being returned instead of ENOTTY to 21943 * maintain consistancy with the original mmc 21944 * driver update. 21945 */ 21946 err = EINVAL; 21947 } else { 21948 mutex_enter(SD_MUTEX(un)); 21949 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21950 (un->un_ncmds_in_transport > 0)) { 21951 mutex_exit(SD_MUTEX(un)); 21952 err = EINVAL; 21953 } else { 21954 mutex_exit(SD_MUTEX(un)); 21955 err = sr_change_blkmode(dev, cmd, arg, flag); 21956 } 21957 } 21958 break; 21959 21960 case CDROMGBLKMODE: 21961 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21962 if (!ISCD(un)) { 21963 err = ENOTTY; 21964 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21965 (un->un_f_blockcount_is_valid != FALSE)) { 21966 /* 21967 * Drive is an ATAPI drive so return target block 21968 * size for ATAPI drives since we cannot change the 21969 * blocksize on ATAPI drives. Used primarily to detect 21970 * if an ATAPI cdrom is present. 21971 */ 21972 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21973 sizeof (int), flag) != 0) { 21974 err = EFAULT; 21975 } else { 21976 err = 0; 21977 } 21978 21979 } else { 21980 /* 21981 * Drive supports changing block sizes via a Mode 21982 * Select. 21983 */ 21984 err = sr_change_blkmode(dev, cmd, arg, flag); 21985 } 21986 break; 21987 21988 case CDROMGDRVSPEED: 21989 case CDROMSDRVSPEED: 21990 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 21991 if (!ISCD(un)) { 21992 err = ENOTTY; 21993 } else if (un->un_f_mmc_cap == TRUE) { 21994 /* 21995 * Note: In the future the driver implementation 21996 * for getting and 21997 * setting cd speed should entail: 21998 * 1) If non-mmc try the Toshiba mode page 21999 * (sr_change_speed) 22000 * 2) If mmc but no support for Real Time Streaming try 22001 * the SET CD SPEED (0xBB) command 22002 * (sr_atapi_change_speed) 22003 * 3) If mmc and support for Real Time Streaming 22004 * try the GET PERFORMANCE and SET STREAMING 22005 * commands (not yet implemented, 4380808) 22006 */ 22007 /* 22008 * As per recent MMC spec, CD-ROM speed is variable 22009 * and changes with LBA. Since there is no such 22010 * things as drive speed now, fail this ioctl. 22011 * 22012 * Note: EINVAL is returned for consistancy of original 22013 * implementation which included support for getting 22014 * the drive speed of mmc devices but not setting 22015 * the drive speed. Thus EINVAL would be returned 22016 * if a set request was made for an mmc device. 22017 * We no longer support get or set speed for 22018 * mmc but need to remain consistant with regard 22019 * to the error code returned. 22020 */ 22021 err = EINVAL; 22022 } else if (un->un_f_cfg_is_atapi == TRUE) { 22023 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22024 } else { 22025 err = sr_change_speed(dev, cmd, arg, flag); 22026 } 22027 break; 22028 22029 case CDROMCDDA: 22030 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22031 if (!ISCD(un)) { 22032 err = ENOTTY; 22033 } else { 22034 err = sr_read_cdda(dev, (void *)arg, flag); 22035 } 22036 break; 22037 22038 case CDROMCDXA: 22039 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22040 if (!ISCD(un)) { 22041 err = ENOTTY; 22042 } else { 22043 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22044 } 22045 break; 22046 22047 case CDROMSUBCODE: 22048 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22049 if (!ISCD(un)) { 22050 err = ENOTTY; 22051 } else { 22052 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22053 } 22054 break; 22055 22056 case DKIOCPARTINFO: { 22057 /* 22058 * Return parameters describing the selected disk slice. 22059 * Note: this ioctl is for the intel platform only 22060 */ 22061 #if defined(__i386) || defined(__amd64) 22062 int part; 22063 22064 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22065 part = SDPART(dev); 22066 22067 /* don't check un_solaris_size for pN */ 22068 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 22069 err = EIO; 22070 } else { 22071 struct part_info p; 22072 22073 p.p_start = (daddr_t)un->un_offset[part]; 22074 p.p_length = (int)un->un_map[part].dkl_nblk; 22075 #ifdef _MULTI_DATAMODEL 22076 switch (ddi_model_convert_from(flag & FMODELS)) { 22077 case DDI_MODEL_ILP32: 22078 { 22079 struct part_info32 p32; 22080 22081 p32.p_start = (daddr32_t)p.p_start; 22082 p32.p_length = p.p_length; 22083 if (ddi_copyout(&p32, (void *)arg, 22084 sizeof (p32), flag)) 22085 err = EFAULT; 22086 break; 22087 } 22088 22089 case DDI_MODEL_NONE: 22090 { 22091 if (ddi_copyout(&p, (void *)arg, sizeof (p), 22092 flag)) 22093 err = EFAULT; 22094 break; 22095 } 22096 } 22097 #else /* ! _MULTI_DATAMODEL */ 22098 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 22099 err = EFAULT; 22100 #endif /* _MULTI_DATAMODEL */ 22101 } 22102 #else 22103 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22104 err = ENOTTY; 22105 #endif 22106 break; 22107 } 22108 22109 case DKIOCG_PHYGEOM: { 22110 /* Return the driver's notion of the media physical geometry */ 22111 #if defined(__i386) || defined(__amd64) 22112 uint64_t capacity; 22113 struct dk_geom disk_geom; 22114 struct dk_geom *dkgp = &disk_geom; 22115 22116 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22117 mutex_enter(SD_MUTEX(un)); 22118 22119 if (un->un_g.dkg_nhead != 0 && 22120 un->un_g.dkg_nsect != 0) { 22121 /* 22122 * We succeeded in getting a geometry, but 22123 * right now it is being reported as just the 22124 * Solaris fdisk partition, just like for 22125 * DKIOCGGEOM. We need to change that to be 22126 * correct for the entire disk now. 22127 */ 22128 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 22129 dkgp->dkg_acyl = 0; 22130 dkgp->dkg_ncyl = un->un_blockcount / 22131 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22132 } else { 22133 bzero(dkgp, sizeof (struct dk_geom)); 22134 /* 22135 * This disk does not have a Solaris VTOC 22136 * so we must present a physical geometry 22137 * that will remain consistent regardless 22138 * of how the disk is used. This will ensure 22139 * that the geometry does not change regardless 22140 * of the fdisk partition type (ie. EFI, FAT32, 22141 * Solaris, etc). 22142 */ 22143 if (ISCD(un)) { 22144 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 22145 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 22146 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 22147 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 22148 } else { 22149 /* 22150 * Invalid un_blockcount can generate invalid 22151 * dk_geom and may result in division by zero 22152 * system failure. Should make sure blockcount 22153 * is valid before using it here. 22154 */ 22155 if (un->un_f_blockcount_is_valid == FALSE) { 22156 mutex_exit(SD_MUTEX(un)); 22157 err = EIO; 22158 22159 break; 22160 } 22161 22162 /* 22163 * Refer to comments related to off-by-1 at the 22164 * header of this file 22165 */ 22166 if (!un->un_f_capacity_adjusted && 22167 !un->un_f_has_removable_media && 22168 !un->un_f_is_hotpluggable && 22169 (un->un_tgt_blocksize == 22170 un->un_sys_blocksize)) 22171 capacity = un->un_blockcount - 1; 22172 else 22173 capacity = un->un_blockcount; 22174 22175 sd_convert_geometry(capacity, dkgp); 22176 dkgp->dkg_acyl = 0; 22177 dkgp->dkg_ncyl = capacity / 22178 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22179 } 22180 } 22181 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22182 22183 if (ddi_copyout(dkgp, (void *)arg, 22184 sizeof (struct dk_geom), flag)) { 22185 mutex_exit(SD_MUTEX(un)); 22186 err = EFAULT; 22187 } else { 22188 mutex_exit(SD_MUTEX(un)); 22189 err = 0; 22190 } 22191 #else 22192 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22193 err = ENOTTY; 22194 #endif 22195 break; 22196 } 22197 22198 case DKIOCG_VIRTGEOM: { 22199 /* Return the driver's notion of the media's logical geometry */ 22200 #if defined(__i386) || defined(__amd64) 22201 struct dk_geom disk_geom; 22202 struct dk_geom *dkgp = &disk_geom; 22203 22204 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22205 mutex_enter(SD_MUTEX(un)); 22206 /* 22207 * If there is no HBA geometry available, or 22208 * if the HBA returned us something that doesn't 22209 * really fit into an Int 13/function 8 geometry 22210 * result, just fail the ioctl. See PSARC 1998/313. 22211 */ 22212 if (un->un_lgeom.g_nhead == 0 || 22213 un->un_lgeom.g_nsect == 0 || 22214 un->un_lgeom.g_ncyl > 1024) { 22215 mutex_exit(SD_MUTEX(un)); 22216 err = EINVAL; 22217 } else { 22218 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 22219 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 22220 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22221 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 22222 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 22223 22224 if (ddi_copyout(dkgp, (void *)arg, 22225 sizeof (struct dk_geom), flag)) { 22226 mutex_exit(SD_MUTEX(un)); 22227 err = EFAULT; 22228 } else { 22229 mutex_exit(SD_MUTEX(un)); 22230 err = 0; 22231 } 22232 } 22233 #else 22234 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22235 err = ENOTTY; 22236 #endif 22237 break; 22238 } 22239 #ifdef SDDEBUG 22240 /* RESET/ABORTS testing ioctls */ 22241 case DKIOCRESET: { 22242 int reset_level; 22243 22244 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22245 err = EFAULT; 22246 } else { 22247 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22248 "reset_level = 0x%lx\n", reset_level); 22249 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22250 err = 0; 22251 } else { 22252 err = EIO; 22253 } 22254 } 22255 break; 22256 } 22257 22258 case DKIOCABORT: 22259 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22260 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22261 err = 0; 22262 } else { 22263 err = EIO; 22264 } 22265 break; 22266 #endif 22267 22268 #ifdef SD_FAULT_INJECTION 22269 /* SDIOC FaultInjection testing ioctls */ 22270 case SDIOCSTART: 22271 case SDIOCSTOP: 22272 case SDIOCINSERTPKT: 22273 case SDIOCINSERTXB: 22274 case SDIOCINSERTUN: 22275 case SDIOCINSERTARQ: 22276 case SDIOCPUSH: 22277 case SDIOCRETRIEVE: 22278 case SDIOCRUN: 22279 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22280 "SDIOC detected cmd:0x%X:\n", cmd); 22281 /* call error generator */ 22282 sd_faultinjection_ioctl(cmd, arg, un); 22283 err = 0; 22284 break; 22285 22286 #endif /* SD_FAULT_INJECTION */ 22287 22288 case DKIOCFLUSHWRITECACHE: 22289 { 22290 struct dk_callback *dkc = (struct dk_callback *)arg; 22291 22292 mutex_enter(SD_MUTEX(un)); 22293 if (!un->un_f_sync_cache_supported || 22294 !un->un_f_write_cache_enabled) { 22295 err = un->un_f_sync_cache_supported ? 22296 0 : ENOTSUP; 22297 mutex_exit(SD_MUTEX(un)); 22298 if ((flag & FKIOCTL) && dkc != NULL && 22299 dkc->dkc_callback != NULL) { 22300 (*dkc->dkc_callback)(dkc->dkc_cookie, 22301 err); 22302 /* 22303 * Did callback and reported error. 22304 * Since we did a callback, ioctl 22305 * should return 0. 22306 */ 22307 err = 0; 22308 } 22309 break; 22310 } 22311 mutex_exit(SD_MUTEX(un)); 22312 22313 if ((flag & FKIOCTL) && dkc != NULL && 22314 dkc->dkc_callback != NULL) { 22315 /* async SYNC CACHE request */ 22316 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 22317 } else { 22318 /* synchronous SYNC CACHE request */ 22319 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22320 } 22321 } 22322 break; 22323 22324 case DKIOCGETWCE: { 22325 22326 int wce; 22327 22328 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 22329 break; 22330 } 22331 22332 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 22333 err = EFAULT; 22334 } 22335 break; 22336 } 22337 22338 case DKIOCSETWCE: { 22339 22340 int wce, sync_supported; 22341 22342 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 22343 err = EFAULT; 22344 break; 22345 } 22346 22347 /* 22348 * Synchronize multiple threads trying to enable 22349 * or disable the cache via the un_f_wcc_cv 22350 * condition variable. 22351 */ 22352 mutex_enter(SD_MUTEX(un)); 22353 22354 /* 22355 * Don't allow the cache to be enabled if the 22356 * config file has it disabled. 22357 */ 22358 if (un->un_f_opt_disable_cache && wce) { 22359 mutex_exit(SD_MUTEX(un)); 22360 err = EINVAL; 22361 break; 22362 } 22363 22364 /* 22365 * Wait for write cache change in progress 22366 * bit to be clear before proceeding. 22367 */ 22368 while (un->un_f_wcc_inprog) 22369 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22370 22371 un->un_f_wcc_inprog = 1; 22372 22373 if (un->un_f_write_cache_enabled && wce == 0) { 22374 /* 22375 * Disable the write cache. Don't clear 22376 * un_f_write_cache_enabled until after 22377 * the mode select and flush are complete. 22378 */ 22379 sync_supported = un->un_f_sync_cache_supported; 22380 mutex_exit(SD_MUTEX(un)); 22381 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22382 SD_CACHE_DISABLE)) == 0 && sync_supported) { 22383 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22384 } 22385 22386 mutex_enter(SD_MUTEX(un)); 22387 if (err == 0) { 22388 un->un_f_write_cache_enabled = 0; 22389 } 22390 22391 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22392 /* 22393 * Set un_f_write_cache_enabled first, so there is 22394 * no window where the cache is enabled, but the 22395 * bit says it isn't. 22396 */ 22397 un->un_f_write_cache_enabled = 1; 22398 mutex_exit(SD_MUTEX(un)); 22399 22400 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22401 SD_CACHE_ENABLE); 22402 22403 mutex_enter(SD_MUTEX(un)); 22404 22405 if (err) { 22406 un->un_f_write_cache_enabled = 0; 22407 } 22408 } 22409 22410 un->un_f_wcc_inprog = 0; 22411 cv_broadcast(&un->un_wcc_cv); 22412 mutex_exit(SD_MUTEX(un)); 22413 break; 22414 } 22415 22416 default: 22417 err = ENOTTY; 22418 break; 22419 } 22420 mutex_enter(SD_MUTEX(un)); 22421 un->un_ncmds_in_driver--; 22422 ASSERT(un->un_ncmds_in_driver >= 0); 22423 mutex_exit(SD_MUTEX(un)); 22424 22425 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22426 return (err); 22427 } 22428 22429 22430 /* 22431 * Function: sd_uscsi_ioctl 22432 * 22433 * Description: This routine is the driver entry point for handling USCSI ioctl 22434 * requests (USCSICMD). 22435 * 22436 * Arguments: dev - the device number 22437 * arg - user provided scsi command 22438 * flag - this argument is a pass through to ddi_copyxxx() 22439 * directly from the mode argument of ioctl(). 22440 * 22441 * Return Code: code returned by sd_send_scsi_cmd 22442 * ENXIO 22443 * EFAULT 22444 * EAGAIN 22445 */ 22446 22447 static int 22448 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 22449 { 22450 #ifdef _MULTI_DATAMODEL 22451 /* 22452 * For use when a 32 bit app makes a call into a 22453 * 64 bit ioctl 22454 */ 22455 struct uscsi_cmd32 uscsi_cmd_32_for_64; 22456 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 22457 model_t model; 22458 #endif /* _MULTI_DATAMODEL */ 22459 struct uscsi_cmd *scmd = NULL; 22460 struct sd_lun *un = NULL; 22461 enum uio_seg uioseg; 22462 char cdb[CDB_GROUP0]; 22463 int rval = 0; 22464 22465 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22466 return (ENXIO); 22467 } 22468 22469 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 22470 22471 scmd = (struct uscsi_cmd *) 22472 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 22473 22474 #ifdef _MULTI_DATAMODEL 22475 switch (model = ddi_model_convert_from(flag & FMODELS)) { 22476 case DDI_MODEL_ILP32: 22477 { 22478 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 22479 rval = EFAULT; 22480 goto done; 22481 } 22482 /* 22483 * Convert the ILP32 uscsi data from the 22484 * application to LP64 for internal use. 22485 */ 22486 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 22487 break; 22488 } 22489 case DDI_MODEL_NONE: 22490 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22491 rval = EFAULT; 22492 goto done; 22493 } 22494 break; 22495 } 22496 #else /* ! _MULTI_DATAMODEL */ 22497 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22498 rval = EFAULT; 22499 goto done; 22500 } 22501 #endif /* _MULTI_DATAMODEL */ 22502 22503 scmd->uscsi_flags &= ~USCSI_NOINTR; 22504 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22505 if (un->un_f_format_in_progress == TRUE) { 22506 rval = EAGAIN; 22507 goto done; 22508 } 22509 22510 /* 22511 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22512 * we will have a valid cdb[0] to test. 22513 */ 22514 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22515 (cdb[0] == SCMD_FORMAT)) { 22516 SD_TRACE(SD_LOG_IOCTL, un, 22517 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22518 mutex_enter(SD_MUTEX(un)); 22519 un->un_f_format_in_progress = TRUE; 22520 mutex_exit(SD_MUTEX(un)); 22521 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22522 SD_PATH_STANDARD); 22523 mutex_enter(SD_MUTEX(un)); 22524 un->un_f_format_in_progress = FALSE; 22525 mutex_exit(SD_MUTEX(un)); 22526 } else { 22527 SD_TRACE(SD_LOG_IOCTL, un, 22528 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22529 /* 22530 * It's OK to fall into here even if the ddi_copyin() 22531 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22532 * does this same copyin and will return the EFAULT 22533 * if it fails. 22534 */ 22535 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22536 SD_PATH_STANDARD); 22537 } 22538 #ifdef _MULTI_DATAMODEL 22539 switch (model) { 22540 case DDI_MODEL_ILP32: 22541 /* 22542 * Convert back to ILP32 before copyout to the 22543 * application 22544 */ 22545 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22546 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22547 if (rval != 0) { 22548 rval = EFAULT; 22549 } 22550 } 22551 break; 22552 case DDI_MODEL_NONE: 22553 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22554 if (rval != 0) { 22555 rval = EFAULT; 22556 } 22557 } 22558 break; 22559 } 22560 #else /* ! _MULTI_DATAMODE */ 22561 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22562 if (rval != 0) { 22563 rval = EFAULT; 22564 } 22565 } 22566 #endif /* _MULTI_DATAMODE */ 22567 done: 22568 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22569 22570 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22571 22572 return (rval); 22573 } 22574 22575 22576 /* 22577 * Function: sd_dkio_ctrl_info 22578 * 22579 * Description: This routine is the driver entry point for handling controller 22580 * information ioctl requests (DKIOCINFO). 22581 * 22582 * Arguments: dev - the device number 22583 * arg - pointer to user provided dk_cinfo structure 22584 * specifying the controller type and attributes. 22585 * flag - this argument is a pass through to ddi_copyxxx() 22586 * directly from the mode argument of ioctl(). 22587 * 22588 * Return Code: 0 22589 * EFAULT 22590 * ENXIO 22591 */ 22592 22593 static int 22594 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22595 { 22596 struct sd_lun *un = NULL; 22597 struct dk_cinfo *info; 22598 dev_info_t *pdip; 22599 int lun, tgt; 22600 22601 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22602 return (ENXIO); 22603 } 22604 22605 info = (struct dk_cinfo *) 22606 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22607 22608 switch (un->un_ctype) { 22609 case CTYPE_CDROM: 22610 info->dki_ctype = DKC_CDROM; 22611 break; 22612 default: 22613 info->dki_ctype = DKC_SCSI_CCS; 22614 break; 22615 } 22616 pdip = ddi_get_parent(SD_DEVINFO(un)); 22617 info->dki_cnum = ddi_get_instance(pdip); 22618 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22619 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22620 } else { 22621 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22622 DK_DEVLEN - 1); 22623 } 22624 22625 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22626 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22627 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22628 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22629 22630 /* Unit Information */ 22631 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22632 info->dki_slave = ((tgt << 3) | lun); 22633 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22634 DK_DEVLEN - 1); 22635 info->dki_flags = DKI_FMTVOL; 22636 info->dki_partition = SDPART(dev); 22637 22638 /* Max Transfer size of this device in blocks */ 22639 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22640 info->dki_addr = 0; 22641 info->dki_space = 0; 22642 info->dki_prio = 0; 22643 info->dki_vec = 0; 22644 22645 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22646 kmem_free(info, sizeof (struct dk_cinfo)); 22647 return (EFAULT); 22648 } else { 22649 kmem_free(info, sizeof (struct dk_cinfo)); 22650 return (0); 22651 } 22652 } 22653 22654 22655 /* 22656 * Function: sd_get_media_info 22657 * 22658 * Description: This routine is the driver entry point for handling ioctl 22659 * requests for the media type or command set profile used by the 22660 * drive to operate on the media (DKIOCGMEDIAINFO). 22661 * 22662 * Arguments: dev - the device number 22663 * arg - pointer to user provided dk_minfo structure 22664 * specifying the media type, logical block size and 22665 * drive capacity. 22666 * flag - this argument is a pass through to ddi_copyxxx() 22667 * directly from the mode argument of ioctl(). 22668 * 22669 * Return Code: 0 22670 * EACCESS 22671 * EFAULT 22672 * ENXIO 22673 * EIO 22674 */ 22675 22676 static int 22677 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22678 { 22679 struct sd_lun *un = NULL; 22680 struct uscsi_cmd com; 22681 struct scsi_inquiry *sinq; 22682 struct dk_minfo media_info; 22683 u_longlong_t media_capacity; 22684 uint64_t capacity; 22685 uint_t lbasize; 22686 uchar_t *out_data; 22687 uchar_t *rqbuf; 22688 int rval = 0; 22689 int rtn; 22690 22691 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22692 (un->un_state == SD_STATE_OFFLINE)) { 22693 return (ENXIO); 22694 } 22695 22696 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22697 22698 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22699 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22700 22701 /* Issue a TUR to determine if the drive is ready with media present */ 22702 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22703 if (rval == ENXIO) { 22704 goto done; 22705 } 22706 22707 /* Now get configuration data */ 22708 if (ISCD(un)) { 22709 media_info.dki_media_type = DK_CDROM; 22710 22711 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22712 if (un->un_f_mmc_cap == TRUE) { 22713 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22714 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22715 22716 if (rtn) { 22717 /* 22718 * Failed for other than an illegal request 22719 * or command not supported 22720 */ 22721 if ((com.uscsi_status == STATUS_CHECK) && 22722 (com.uscsi_rqstatus == STATUS_GOOD)) { 22723 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22724 (rqbuf[12] != 0x20)) { 22725 rval = EIO; 22726 goto done; 22727 } 22728 } 22729 } else { 22730 /* 22731 * The GET CONFIGURATION command succeeded 22732 * so set the media type according to the 22733 * returned data 22734 */ 22735 media_info.dki_media_type = out_data[6]; 22736 media_info.dki_media_type <<= 8; 22737 media_info.dki_media_type |= out_data[7]; 22738 } 22739 } 22740 } else { 22741 /* 22742 * The profile list is not available, so we attempt to identify 22743 * the media type based on the inquiry data 22744 */ 22745 sinq = un->un_sd->sd_inq; 22746 if (sinq->inq_qual == 0) { 22747 /* This is a direct access device */ 22748 media_info.dki_media_type = DK_FIXED_DISK; 22749 22750 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22751 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22752 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22753 media_info.dki_media_type = DK_ZIP; 22754 } else if ( 22755 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22756 media_info.dki_media_type = DK_JAZ; 22757 } 22758 } 22759 } else { 22760 /* Not a CD or direct access so return unknown media */ 22761 media_info.dki_media_type = DK_UNKNOWN; 22762 } 22763 } 22764 22765 /* Now read the capacity so we can provide the lbasize and capacity */ 22766 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22767 SD_PATH_DIRECT)) { 22768 case 0: 22769 break; 22770 case EACCES: 22771 rval = EACCES; 22772 goto done; 22773 default: 22774 rval = EIO; 22775 goto done; 22776 } 22777 22778 media_info.dki_lbsize = lbasize; 22779 media_capacity = capacity; 22780 22781 /* 22782 * sd_send_scsi_READ_CAPACITY() reports capacity in 22783 * un->un_sys_blocksize chunks. So we need to convert it into 22784 * cap.lbasize chunks. 22785 */ 22786 media_capacity *= un->un_sys_blocksize; 22787 media_capacity /= lbasize; 22788 media_info.dki_capacity = media_capacity; 22789 22790 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22791 rval = EFAULT; 22792 /* Put goto. Anybody might add some code below in future */ 22793 goto done; 22794 } 22795 done: 22796 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22797 kmem_free(rqbuf, SENSE_LENGTH); 22798 return (rval); 22799 } 22800 22801 22802 /* 22803 * Function: sd_dkio_get_geometry 22804 * 22805 * Description: This routine is the driver entry point for handling user 22806 * requests to get the device geometry (DKIOCGGEOM). 22807 * 22808 * Arguments: dev - the device number 22809 * arg - pointer to user provided dk_geom structure specifying 22810 * the controller's notion of the current geometry. 22811 * flag - this argument is a pass through to ddi_copyxxx() 22812 * directly from the mode argument of ioctl(). 22813 * geom_validated - flag indicating if the device geometry has been 22814 * previously validated in the sdioctl routine. 22815 * 22816 * Return Code: 0 22817 * EFAULT 22818 * ENXIO 22819 * EIO 22820 */ 22821 22822 static int 22823 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22824 { 22825 struct sd_lun *un = NULL; 22826 struct dk_geom *tmp_geom = NULL; 22827 int rval = 0; 22828 22829 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22830 return (ENXIO); 22831 } 22832 22833 if (geom_validated == FALSE) { 22834 /* 22835 * sd_validate_geometry does not spin a disk up 22836 * if it was spun down. We need to make sure it 22837 * is ready. 22838 */ 22839 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22840 return (rval); 22841 } 22842 mutex_enter(SD_MUTEX(un)); 22843 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22844 mutex_exit(SD_MUTEX(un)); 22845 } 22846 if (rval) 22847 return (rval); 22848 22849 /* 22850 * It is possible that un_solaris_size is 0(uninitialized) 22851 * after sd_unit_attach. Reservation conflict may cause the 22852 * above situation. Thus, the zero check of un_solaris_size 22853 * should occur after the sd_validate_geometry() call. 22854 */ 22855 #if defined(__i386) || defined(__amd64) 22856 if (un->un_solaris_size == 0) { 22857 return (EIO); 22858 } 22859 #endif 22860 22861 /* 22862 * Make a local copy of the soft state geometry to avoid some potential 22863 * race conditions associated with holding the mutex and updating the 22864 * write_reinstruct value 22865 */ 22866 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22867 mutex_enter(SD_MUTEX(un)); 22868 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22869 mutex_exit(SD_MUTEX(un)); 22870 22871 if (tmp_geom->dkg_write_reinstruct == 0) { 22872 tmp_geom->dkg_write_reinstruct = 22873 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22874 sd_rot_delay) / (int)60000); 22875 } 22876 22877 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22878 flag); 22879 if (rval != 0) { 22880 rval = EFAULT; 22881 } 22882 22883 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22884 return (rval); 22885 22886 } 22887 22888 22889 /* 22890 * Function: sd_dkio_set_geometry 22891 * 22892 * Description: This routine is the driver entry point for handling user 22893 * requests to set the device geometry (DKIOCSGEOM). The actual 22894 * device geometry is not updated, just the driver "notion" of it. 22895 * 22896 * Arguments: dev - the device number 22897 * arg - pointer to user provided dk_geom structure used to set 22898 * the controller's notion of the current geometry. 22899 * flag - this argument is a pass through to ddi_copyxxx() 22900 * directly from the mode argument of ioctl(). 22901 * 22902 * Return Code: 0 22903 * EFAULT 22904 * ENXIO 22905 * EIO 22906 */ 22907 22908 static int 22909 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22910 { 22911 struct sd_lun *un = NULL; 22912 struct dk_geom *tmp_geom; 22913 struct dk_map *lp; 22914 int rval = 0; 22915 int i; 22916 22917 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22918 return (ENXIO); 22919 } 22920 22921 /* 22922 * Make sure there is no reservation conflict on the lun. 22923 */ 22924 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22925 return (EACCES); 22926 } 22927 22928 #if defined(__i386) || defined(__amd64) 22929 if (un->un_solaris_size == 0) { 22930 return (EIO); 22931 } 22932 #endif 22933 22934 /* 22935 * We need to copy the user specified geometry into local 22936 * storage and then update the softstate. We don't want to hold 22937 * the mutex and copyin directly from the user to the soft state 22938 */ 22939 tmp_geom = (struct dk_geom *) 22940 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22941 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22942 if (rval != 0) { 22943 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22944 return (EFAULT); 22945 } 22946 22947 mutex_enter(SD_MUTEX(un)); 22948 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22949 for (i = 0; i < NDKMAP; i++) { 22950 lp = &un->un_map[i]; 22951 un->un_offset[i] = 22952 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22953 #if defined(__i386) || defined(__amd64) 22954 un->un_offset[i] += un->un_solaris_offset; 22955 #endif 22956 } 22957 un->un_f_geometry_is_valid = FALSE; 22958 mutex_exit(SD_MUTEX(un)); 22959 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22960 22961 return (rval); 22962 } 22963 22964 22965 /* 22966 * Function: sd_dkio_get_partition 22967 * 22968 * Description: This routine is the driver entry point for handling user 22969 * requests to get the partition table (DKIOCGAPART). 22970 * 22971 * Arguments: dev - the device number 22972 * arg - pointer to user provided dk_allmap structure specifying 22973 * the controller's notion of the current partition table. 22974 * flag - this argument is a pass through to ddi_copyxxx() 22975 * directly from the mode argument of ioctl(). 22976 * geom_validated - flag indicating if the device geometry has been 22977 * previously validated in the sdioctl routine. 22978 * 22979 * Return Code: 0 22980 * EFAULT 22981 * ENXIO 22982 * EIO 22983 */ 22984 22985 static int 22986 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22987 { 22988 struct sd_lun *un = NULL; 22989 int rval = 0; 22990 int size; 22991 22992 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22993 return (ENXIO); 22994 } 22995 22996 /* 22997 * Make sure the geometry is valid before getting the partition 22998 * information. 22999 */ 23000 mutex_enter(SD_MUTEX(un)); 23001 if (geom_validated == FALSE) { 23002 /* 23003 * sd_validate_geometry does not spin a disk up 23004 * if it was spun down. We need to make sure it 23005 * is ready before validating the geometry. 23006 */ 23007 mutex_exit(SD_MUTEX(un)); 23008 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23009 return (rval); 23010 } 23011 mutex_enter(SD_MUTEX(un)); 23012 23013 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23014 mutex_exit(SD_MUTEX(un)); 23015 return (rval); 23016 } 23017 } 23018 mutex_exit(SD_MUTEX(un)); 23019 23020 /* 23021 * It is possible that un_solaris_size is 0(uninitialized) 23022 * after sd_unit_attach. Reservation conflict may cause the 23023 * above situation. Thus, the zero check of un_solaris_size 23024 * should occur after the sd_validate_geometry() call. 23025 */ 23026 #if defined(__i386) || defined(__amd64) 23027 if (un->un_solaris_size == 0) { 23028 return (EIO); 23029 } 23030 #endif 23031 23032 #ifdef _MULTI_DATAMODEL 23033 switch (ddi_model_convert_from(flag & FMODELS)) { 23034 case DDI_MODEL_ILP32: { 23035 struct dk_map32 dk_map32[NDKMAP]; 23036 int i; 23037 23038 for (i = 0; i < NDKMAP; i++) { 23039 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 23040 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 23041 } 23042 size = NDKMAP * sizeof (struct dk_map32); 23043 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 23044 if (rval != 0) { 23045 rval = EFAULT; 23046 } 23047 break; 23048 } 23049 case DDI_MODEL_NONE: 23050 size = NDKMAP * sizeof (struct dk_map); 23051 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23052 if (rval != 0) { 23053 rval = EFAULT; 23054 } 23055 break; 23056 } 23057 #else /* ! _MULTI_DATAMODEL */ 23058 size = NDKMAP * sizeof (struct dk_map); 23059 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23060 if (rval != 0) { 23061 rval = EFAULT; 23062 } 23063 #endif /* _MULTI_DATAMODEL */ 23064 return (rval); 23065 } 23066 23067 23068 /* 23069 * Function: sd_dkio_set_partition 23070 * 23071 * Description: This routine is the driver entry point for handling user 23072 * requests to set the partition table (DKIOCSAPART). The actual 23073 * device partition is not updated. 23074 * 23075 * Arguments: dev - the device number 23076 * arg - pointer to user provided dk_allmap structure used to set 23077 * the controller's notion of the partition table. 23078 * flag - this argument is a pass through to ddi_copyxxx() 23079 * directly from the mode argument of ioctl(). 23080 * 23081 * Return Code: 0 23082 * EINVAL 23083 * EFAULT 23084 * ENXIO 23085 * EIO 23086 */ 23087 23088 static int 23089 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 23090 { 23091 struct sd_lun *un = NULL; 23092 struct dk_map dk_map[NDKMAP]; 23093 struct dk_map *lp; 23094 int rval = 0; 23095 int size; 23096 int i; 23097 #if defined(_SUNOS_VTOC_16) 23098 struct dkl_partition *vp; 23099 #endif 23100 23101 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23102 return (ENXIO); 23103 } 23104 23105 /* 23106 * Set the map for all logical partitions. We lock 23107 * the priority just to make sure an interrupt doesn't 23108 * come in while the map is half updated. 23109 */ 23110 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 23111 mutex_enter(SD_MUTEX(un)); 23112 if (un->un_blockcount > DK_MAX_BLOCKS) { 23113 mutex_exit(SD_MUTEX(un)); 23114 return (ENOTSUP); 23115 } 23116 mutex_exit(SD_MUTEX(un)); 23117 23118 /* 23119 * Make sure there is no reservation conflict on the lun. 23120 */ 23121 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 23122 return (EACCES); 23123 } 23124 23125 #if defined(__i386) || defined(__amd64) 23126 if (un->un_solaris_size == 0) { 23127 return (EIO); 23128 } 23129 #endif 23130 23131 #ifdef _MULTI_DATAMODEL 23132 switch (ddi_model_convert_from(flag & FMODELS)) { 23133 case DDI_MODEL_ILP32: { 23134 struct dk_map32 dk_map32[NDKMAP]; 23135 23136 size = NDKMAP * sizeof (struct dk_map32); 23137 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 23138 if (rval != 0) { 23139 return (EFAULT); 23140 } 23141 for (i = 0; i < NDKMAP; i++) { 23142 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 23143 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 23144 } 23145 break; 23146 } 23147 case DDI_MODEL_NONE: 23148 size = NDKMAP * sizeof (struct dk_map); 23149 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23150 if (rval != 0) { 23151 return (EFAULT); 23152 } 23153 break; 23154 } 23155 #else /* ! _MULTI_DATAMODEL */ 23156 size = NDKMAP * sizeof (struct dk_map); 23157 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23158 if (rval != 0) { 23159 return (EFAULT); 23160 } 23161 #endif /* _MULTI_DATAMODEL */ 23162 23163 mutex_enter(SD_MUTEX(un)); 23164 /* Note: The size used in this bcopy is set based upon the data model */ 23165 bcopy(dk_map, un->un_map, size); 23166 #if defined(_SUNOS_VTOC_16) 23167 vp = (struct dkl_partition *)&(un->un_vtoc); 23168 #endif /* defined(_SUNOS_VTOC_16) */ 23169 for (i = 0; i < NDKMAP; i++) { 23170 lp = &un->un_map[i]; 23171 un->un_offset[i] = 23172 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23173 #if defined(_SUNOS_VTOC_16) 23174 vp->p_start = un->un_offset[i]; 23175 vp->p_size = lp->dkl_nblk; 23176 vp++; 23177 #endif /* defined(_SUNOS_VTOC_16) */ 23178 #if defined(__i386) || defined(__amd64) 23179 un->un_offset[i] += un->un_solaris_offset; 23180 #endif 23181 } 23182 mutex_exit(SD_MUTEX(un)); 23183 return (rval); 23184 } 23185 23186 23187 /* 23188 * Function: sd_dkio_get_vtoc 23189 * 23190 * Description: This routine is the driver entry point for handling user 23191 * requests to get the current volume table of contents 23192 * (DKIOCGVTOC). 23193 * 23194 * Arguments: dev - the device number 23195 * arg - pointer to user provided vtoc structure specifying 23196 * the current vtoc. 23197 * flag - this argument is a pass through to ddi_copyxxx() 23198 * directly from the mode argument of ioctl(). 23199 * geom_validated - flag indicating if the device geometry has been 23200 * previously validated in the sdioctl routine. 23201 * 23202 * Return Code: 0 23203 * EFAULT 23204 * ENXIO 23205 * EIO 23206 */ 23207 23208 static int 23209 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 23210 { 23211 struct sd_lun *un = NULL; 23212 #if defined(_SUNOS_VTOC_8) 23213 struct vtoc user_vtoc; 23214 #endif /* defined(_SUNOS_VTOC_8) */ 23215 int rval = 0; 23216 23217 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23218 return (ENXIO); 23219 } 23220 23221 mutex_enter(SD_MUTEX(un)); 23222 if (geom_validated == FALSE) { 23223 /* 23224 * sd_validate_geometry does not spin a disk up 23225 * if it was spun down. We need to make sure it 23226 * is ready. 23227 */ 23228 mutex_exit(SD_MUTEX(un)); 23229 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23230 return (rval); 23231 } 23232 mutex_enter(SD_MUTEX(un)); 23233 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23234 mutex_exit(SD_MUTEX(un)); 23235 return (rval); 23236 } 23237 } 23238 23239 #if defined(_SUNOS_VTOC_8) 23240 sd_build_user_vtoc(un, &user_vtoc); 23241 mutex_exit(SD_MUTEX(un)); 23242 23243 #ifdef _MULTI_DATAMODEL 23244 switch (ddi_model_convert_from(flag & FMODELS)) { 23245 case DDI_MODEL_ILP32: { 23246 struct vtoc32 user_vtoc32; 23247 23248 vtoctovtoc32(user_vtoc, user_vtoc32); 23249 if (ddi_copyout(&user_vtoc32, (void *)arg, 23250 sizeof (struct vtoc32), flag)) { 23251 return (EFAULT); 23252 } 23253 break; 23254 } 23255 23256 case DDI_MODEL_NONE: 23257 if (ddi_copyout(&user_vtoc, (void *)arg, 23258 sizeof (struct vtoc), flag)) { 23259 return (EFAULT); 23260 } 23261 break; 23262 } 23263 #else /* ! _MULTI_DATAMODEL */ 23264 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 23265 return (EFAULT); 23266 } 23267 #endif /* _MULTI_DATAMODEL */ 23268 23269 #elif defined(_SUNOS_VTOC_16) 23270 mutex_exit(SD_MUTEX(un)); 23271 23272 #ifdef _MULTI_DATAMODEL 23273 /* 23274 * The un_vtoc structure is a "struct dk_vtoc" which is always 23275 * 32-bit to maintain compatibility with existing on-disk 23276 * structures. Thus, we need to convert the structure when copying 23277 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 23278 * program. If the target is a 32-bit program, then no conversion 23279 * is necessary. 23280 */ 23281 /* LINTED: logical expression always true: op "||" */ 23282 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 23283 switch (ddi_model_convert_from(flag & FMODELS)) { 23284 case DDI_MODEL_ILP32: 23285 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 23286 sizeof (un->un_vtoc), flag)) { 23287 return (EFAULT); 23288 } 23289 break; 23290 23291 case DDI_MODEL_NONE: { 23292 struct vtoc user_vtoc; 23293 23294 vtoc32tovtoc(un->un_vtoc, user_vtoc); 23295 if (ddi_copyout(&user_vtoc, (void *)arg, 23296 sizeof (struct vtoc), flag)) { 23297 return (EFAULT); 23298 } 23299 break; 23300 } 23301 } 23302 #else /* ! _MULTI_DATAMODEL */ 23303 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 23304 flag)) { 23305 return (EFAULT); 23306 } 23307 #endif /* _MULTI_DATAMODEL */ 23308 #else 23309 #error "No VTOC format defined." 23310 #endif 23311 23312 return (rval); 23313 } 23314 23315 static int 23316 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 23317 { 23318 struct sd_lun *un = NULL; 23319 dk_efi_t user_efi; 23320 int rval = 0; 23321 void *buffer; 23322 23323 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23324 return (ENXIO); 23325 23326 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23327 return (EFAULT); 23328 23329 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23330 23331 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23332 (user_efi.dki_length > un->un_max_xfer_size)) 23333 return (EINVAL); 23334 23335 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23336 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 23337 user_efi.dki_lba, SD_PATH_DIRECT); 23338 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 23339 user_efi.dki_length, flag) != 0) 23340 rval = EFAULT; 23341 23342 kmem_free(buffer, user_efi.dki_length); 23343 return (rval); 23344 } 23345 23346 /* 23347 * Function: sd_build_user_vtoc 23348 * 23349 * Description: This routine populates a pass by reference variable with the 23350 * current volume table of contents. 23351 * 23352 * Arguments: un - driver soft state (unit) structure 23353 * user_vtoc - pointer to vtoc structure to be populated 23354 */ 23355 23356 static void 23357 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23358 { 23359 struct dk_map2 *lpart; 23360 struct dk_map *lmap; 23361 struct partition *vpart; 23362 int nblks; 23363 int i; 23364 23365 ASSERT(mutex_owned(SD_MUTEX(un))); 23366 23367 /* 23368 * Return vtoc structure fields in the provided VTOC area, addressed 23369 * by *vtoc. 23370 */ 23371 bzero(user_vtoc, sizeof (struct vtoc)); 23372 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 23373 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 23374 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 23375 user_vtoc->v_sanity = VTOC_SANE; 23376 user_vtoc->v_version = un->un_vtoc.v_version; 23377 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 23378 user_vtoc->v_sectorsz = un->un_sys_blocksize; 23379 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 23380 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 23381 sizeof (un->un_vtoc.v_reserved)); 23382 /* 23383 * Convert partitioning information. 23384 * 23385 * Note the conversion from starting cylinder number 23386 * to starting sector number. 23387 */ 23388 lmap = un->un_map; 23389 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 23390 vpart = user_vtoc->v_part; 23391 23392 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23393 23394 for (i = 0; i < V_NUMPAR; i++) { 23395 vpart->p_tag = lpart->p_tag; 23396 vpart->p_flag = lpart->p_flag; 23397 vpart->p_start = lmap->dkl_cylno * nblks; 23398 vpart->p_size = lmap->dkl_nblk; 23399 lmap++; 23400 lpart++; 23401 vpart++; 23402 23403 /* (4364927) */ 23404 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 23405 } 23406 23407 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 23408 } 23409 23410 static int 23411 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 23412 { 23413 struct sd_lun *un = NULL; 23414 struct partition64 p64; 23415 int rval = 0; 23416 uint_t nparts; 23417 efi_gpe_t *partitions; 23418 efi_gpt_t *buffer; 23419 diskaddr_t gpe_lba; 23420 23421 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23422 return (ENXIO); 23423 } 23424 23425 if (ddi_copyin((const void *)arg, &p64, 23426 sizeof (struct partition64), flag)) { 23427 return (EFAULT); 23428 } 23429 23430 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23431 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23432 1, SD_PATH_DIRECT); 23433 if (rval != 0) 23434 goto done_error; 23435 23436 sd_swap_efi_gpt(buffer); 23437 23438 if ((rval = sd_validate_efi(buffer)) != 0) 23439 goto done_error; 23440 23441 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23442 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23443 if (p64.p_partno > nparts) { 23444 /* couldn't find it */ 23445 rval = ESRCH; 23446 goto done_error; 23447 } 23448 /* 23449 * if we're dealing with a partition that's out of the normal 23450 * 16K block, adjust accordingly 23451 */ 23452 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23453 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23454 gpe_lba, SD_PATH_DIRECT); 23455 if (rval) { 23456 goto done_error; 23457 } 23458 partitions = (efi_gpe_t *)buffer; 23459 23460 sd_swap_efi_gpe(nparts, partitions); 23461 23462 partitions += p64.p_partno; 23463 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23464 sizeof (struct uuid)); 23465 p64.p_start = partitions->efi_gpe_StartingLBA; 23466 p64.p_size = partitions->efi_gpe_EndingLBA - 23467 p64.p_start + 1; 23468 23469 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23470 rval = EFAULT; 23471 23472 done_error: 23473 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23474 return (rval); 23475 } 23476 23477 23478 /* 23479 * Function: sd_dkio_set_vtoc 23480 * 23481 * Description: This routine is the driver entry point for handling user 23482 * requests to set the current volume table of contents 23483 * (DKIOCSVTOC). 23484 * 23485 * Arguments: dev - the device number 23486 * arg - pointer to user provided vtoc structure used to set the 23487 * current vtoc. 23488 * flag - this argument is a pass through to ddi_copyxxx() 23489 * directly from the mode argument of ioctl(). 23490 * 23491 * Return Code: 0 23492 * EFAULT 23493 * ENXIO 23494 * EINVAL 23495 * ENOTSUP 23496 */ 23497 23498 static int 23499 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23500 { 23501 struct sd_lun *un = NULL; 23502 struct vtoc user_vtoc; 23503 int rval = 0; 23504 23505 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23506 return (ENXIO); 23507 } 23508 23509 #if defined(__i386) || defined(__amd64) 23510 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23511 return (EINVAL); 23512 } 23513 #endif 23514 23515 #ifdef _MULTI_DATAMODEL 23516 switch (ddi_model_convert_from(flag & FMODELS)) { 23517 case DDI_MODEL_ILP32: { 23518 struct vtoc32 user_vtoc32; 23519 23520 if (ddi_copyin((const void *)arg, &user_vtoc32, 23521 sizeof (struct vtoc32), flag)) { 23522 return (EFAULT); 23523 } 23524 vtoc32tovtoc(user_vtoc32, user_vtoc); 23525 break; 23526 } 23527 23528 case DDI_MODEL_NONE: 23529 if (ddi_copyin((const void *)arg, &user_vtoc, 23530 sizeof (struct vtoc), flag)) { 23531 return (EFAULT); 23532 } 23533 break; 23534 } 23535 #else /* ! _MULTI_DATAMODEL */ 23536 if (ddi_copyin((const void *)arg, &user_vtoc, 23537 sizeof (struct vtoc), flag)) { 23538 return (EFAULT); 23539 } 23540 #endif /* _MULTI_DATAMODEL */ 23541 23542 mutex_enter(SD_MUTEX(un)); 23543 if (un->un_blockcount > DK_MAX_BLOCKS) { 23544 mutex_exit(SD_MUTEX(un)); 23545 return (ENOTSUP); 23546 } 23547 if (un->un_g.dkg_ncyl == 0) { 23548 mutex_exit(SD_MUTEX(un)); 23549 return (EINVAL); 23550 } 23551 23552 mutex_exit(SD_MUTEX(un)); 23553 sd_clear_efi(un); 23554 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23555 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23556 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23557 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23558 un->un_node_type, NULL); 23559 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23560 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23561 un->un_node_type, NULL); 23562 mutex_enter(SD_MUTEX(un)); 23563 23564 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23565 if ((rval = sd_write_label(dev)) == 0) { 23566 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23567 != 0) { 23568 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23569 "sd_dkio_set_vtoc: " 23570 "Failed validate geometry\n"); 23571 } 23572 } 23573 } 23574 23575 /* 23576 * If sd_build_label_vtoc, or sd_write_label failed above write the 23577 * devid anyway, what can it hurt? Also preserve the device id by 23578 * writing to the disk acyl for the case where a devid has been 23579 * fabricated. 23580 */ 23581 if (un->un_f_devid_supported && 23582 (un->un_f_opt_fab_devid == TRUE)) { 23583 if (un->un_devid == NULL) { 23584 sd_register_devid(un, SD_DEVINFO(un), 23585 SD_TARGET_IS_UNRESERVED); 23586 } else { 23587 /* 23588 * The device id for this disk has been 23589 * fabricated. Fabricated device id's are 23590 * managed by storing them in the last 2 23591 * available sectors on the drive. The device 23592 * id must be preserved by writing it back out 23593 * to this location. 23594 */ 23595 if (sd_write_deviceid(un) != 0) { 23596 ddi_devid_free(un->un_devid); 23597 un->un_devid = NULL; 23598 } 23599 } 23600 } 23601 mutex_exit(SD_MUTEX(un)); 23602 return (rval); 23603 } 23604 23605 23606 /* 23607 * Function: sd_build_label_vtoc 23608 * 23609 * Description: This routine updates the driver soft state current volume table 23610 * of contents based on a user specified vtoc. 23611 * 23612 * Arguments: un - driver soft state (unit) structure 23613 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23614 * to update the driver soft state. 23615 * 23616 * Return Code: 0 23617 * EINVAL 23618 */ 23619 23620 static int 23621 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23622 { 23623 struct dk_map *lmap; 23624 struct partition *vpart; 23625 int nblks; 23626 #if defined(_SUNOS_VTOC_8) 23627 int ncyl; 23628 struct dk_map2 *lpart; 23629 #endif /* defined(_SUNOS_VTOC_8) */ 23630 int i; 23631 23632 ASSERT(mutex_owned(SD_MUTEX(un))); 23633 23634 /* Sanity-check the vtoc */ 23635 if (user_vtoc->v_sanity != VTOC_SANE || 23636 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23637 user_vtoc->v_nparts != V_NUMPAR) { 23638 return (EINVAL); 23639 } 23640 23641 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23642 if (nblks == 0) { 23643 return (EINVAL); 23644 } 23645 23646 #if defined(_SUNOS_VTOC_8) 23647 vpart = user_vtoc->v_part; 23648 for (i = 0; i < V_NUMPAR; i++) { 23649 if ((vpart->p_start % nblks) != 0) { 23650 return (EINVAL); 23651 } 23652 ncyl = vpart->p_start / nblks; 23653 ncyl += vpart->p_size / nblks; 23654 if ((vpart->p_size % nblks) != 0) { 23655 ncyl++; 23656 } 23657 if (ncyl > (int)un->un_g.dkg_ncyl) { 23658 return (EINVAL); 23659 } 23660 vpart++; 23661 } 23662 #endif /* defined(_SUNOS_VTOC_8) */ 23663 23664 /* Put appropriate vtoc structure fields into the disk label */ 23665 #if defined(_SUNOS_VTOC_16) 23666 /* 23667 * The vtoc is always a 32bit data structure to maintain the 23668 * on-disk format. Convert "in place" instead of bcopying it. 23669 */ 23670 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23671 23672 /* 23673 * in the 16-slice vtoc, starting sectors are expressed in 23674 * numbers *relative* to the start of the Solaris fdisk partition. 23675 */ 23676 lmap = un->un_map; 23677 vpart = user_vtoc->v_part; 23678 23679 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23680 lmap->dkl_cylno = vpart->p_start / nblks; 23681 lmap->dkl_nblk = vpart->p_size; 23682 } 23683 23684 #elif defined(_SUNOS_VTOC_8) 23685 23686 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23687 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23688 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23689 23690 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23691 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23692 23693 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23694 23695 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23696 23697 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23698 sizeof (un->un_vtoc.v_reserved)); 23699 23700 /* 23701 * Note the conversion from starting sector number 23702 * to starting cylinder number. 23703 * Return error if division results in a remainder. 23704 */ 23705 lmap = un->un_map; 23706 lpart = un->un_vtoc.v_part; 23707 vpart = user_vtoc->v_part; 23708 23709 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23710 lpart->p_tag = vpart->p_tag; 23711 lpart->p_flag = vpart->p_flag; 23712 lmap->dkl_cylno = vpart->p_start / nblks; 23713 lmap->dkl_nblk = vpart->p_size; 23714 23715 lmap++; 23716 lpart++; 23717 vpart++; 23718 23719 /* (4387723) */ 23720 #ifdef _LP64 23721 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23722 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23723 } else { 23724 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23725 } 23726 #else 23727 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23728 #endif 23729 } 23730 23731 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23732 #else 23733 #error "No VTOC format defined." 23734 #endif 23735 return (0); 23736 } 23737 23738 /* 23739 * Function: sd_clear_efi 23740 * 23741 * Description: This routine clears all EFI labels. 23742 * 23743 * Arguments: un - driver soft state (unit) structure 23744 * 23745 * Return Code: void 23746 */ 23747 23748 static void 23749 sd_clear_efi(struct sd_lun *un) 23750 { 23751 efi_gpt_t *gpt; 23752 uint_t lbasize; 23753 uint64_t cap; 23754 int rval; 23755 23756 ASSERT(!mutex_owned(SD_MUTEX(un))); 23757 23758 mutex_enter(SD_MUTEX(un)); 23759 un->un_reserved = -1; 23760 mutex_exit(SD_MUTEX(un)); 23761 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23762 23763 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23764 goto done; 23765 } 23766 23767 sd_swap_efi_gpt(gpt); 23768 rval = sd_validate_efi(gpt); 23769 if (rval == 0) { 23770 /* clear primary */ 23771 bzero(gpt, sizeof (efi_gpt_t)); 23772 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23773 SD_PATH_DIRECT))) { 23774 SD_INFO(SD_LOG_IO_PARTITION, un, 23775 "sd_clear_efi: clear primary label failed\n"); 23776 } 23777 } 23778 /* the backup */ 23779 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23780 SD_PATH_DIRECT); 23781 if (rval) { 23782 goto done; 23783 } 23784 /* 23785 * The MMC standard allows READ CAPACITY to be 23786 * inaccurate by a bounded amount (in the interest of 23787 * response latency). As a result, failed READs are 23788 * commonplace (due to the reading of metadata and not 23789 * data). Depending on the per-Vendor/drive Sense data, 23790 * the failed READ can cause many (unnecessary) retries. 23791 */ 23792 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23793 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23794 SD_PATH_DIRECT)) != 0) { 23795 goto done; 23796 } 23797 sd_swap_efi_gpt(gpt); 23798 rval = sd_validate_efi(gpt); 23799 if (rval == 0) { 23800 /* clear backup */ 23801 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23802 cap-1); 23803 bzero(gpt, sizeof (efi_gpt_t)); 23804 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23805 cap-1, SD_PATH_DIRECT))) { 23806 SD_INFO(SD_LOG_IO_PARTITION, un, 23807 "sd_clear_efi: clear backup label failed\n"); 23808 } 23809 } else { 23810 /* 23811 * Refer to comments related to off-by-1 at the 23812 * header of this file 23813 */ 23814 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23815 cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23816 SD_PATH_DIRECT)) != 0) { 23817 goto done; 23818 } 23819 sd_swap_efi_gpt(gpt); 23820 rval = sd_validate_efi(gpt); 23821 if (rval == 0) { 23822 /* clear legacy backup EFI label */ 23823 SD_TRACE(SD_LOG_IOCTL, un, 23824 "sd_clear_efi clear backup@%lu\n", cap-2); 23825 bzero(gpt, sizeof (efi_gpt_t)); 23826 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23827 cap-2, SD_PATH_DIRECT))) { 23828 SD_INFO(SD_LOG_IO_PARTITION, 23829 un, "sd_clear_efi: " 23830 " clear legacy backup label failed\n"); 23831 } 23832 } 23833 } 23834 23835 done: 23836 kmem_free(gpt, sizeof (efi_gpt_t)); 23837 } 23838 23839 /* 23840 * Function: sd_set_vtoc 23841 * 23842 * Description: This routine writes data to the appropriate positions 23843 * 23844 * Arguments: un - driver soft state (unit) structure 23845 * dkl - the data to be written 23846 * 23847 * Return: void 23848 */ 23849 23850 static int 23851 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23852 { 23853 void *shadow_buf; 23854 uint_t label_addr; 23855 int sec; 23856 int blk; 23857 int head; 23858 int cyl; 23859 int rval; 23860 23861 #if defined(__i386) || defined(__amd64) 23862 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23863 #else 23864 /* Write the primary label at block 0 of the solaris partition. */ 23865 label_addr = 0; 23866 #endif 23867 23868 if (NOT_DEVBSIZE(un)) { 23869 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23870 /* 23871 * Read the target's first block. 23872 */ 23873 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23874 un->un_tgt_blocksize, label_addr, 23875 SD_PATH_STANDARD)) != 0) { 23876 goto exit; 23877 } 23878 /* 23879 * Copy the contents of the label into the shadow buffer 23880 * which is of the size of target block size. 23881 */ 23882 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23883 } 23884 23885 /* Write the primary label */ 23886 if (NOT_DEVBSIZE(un)) { 23887 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23888 label_addr, SD_PATH_STANDARD); 23889 } else { 23890 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23891 label_addr, SD_PATH_STANDARD); 23892 } 23893 if (rval != 0) { 23894 return (rval); 23895 } 23896 23897 /* 23898 * Calculate where the backup labels go. They are always on 23899 * the last alternate cylinder, but some older drives put them 23900 * on head 2 instead of the last head. They are always on the 23901 * first 5 odd sectors of the appropriate track. 23902 * 23903 * We have no choice at this point, but to believe that the 23904 * disk label is valid. Use the geometry of the disk 23905 * as described in the label. 23906 */ 23907 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23908 head = dkl->dkl_nhead - 1; 23909 23910 /* 23911 * Write and verify the backup labels. Make sure we don't try to 23912 * write past the last cylinder. 23913 */ 23914 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23915 blk = (daddr_t)( 23916 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23917 (head * dkl->dkl_nsect) + sec); 23918 #if defined(__i386) || defined(__amd64) 23919 blk += un->un_solaris_offset; 23920 #endif 23921 if (NOT_DEVBSIZE(un)) { 23922 uint64_t tblk; 23923 /* 23924 * Need to read the block first for read modify write. 23925 */ 23926 tblk = (uint64_t)blk; 23927 blk = (int)((tblk * un->un_sys_blocksize) / 23928 un->un_tgt_blocksize); 23929 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23930 un->un_tgt_blocksize, blk, 23931 SD_PATH_STANDARD)) != 0) { 23932 goto exit; 23933 } 23934 /* 23935 * Modify the shadow buffer with the label. 23936 */ 23937 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23938 rval = sd_send_scsi_WRITE(un, shadow_buf, 23939 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23940 } else { 23941 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23942 blk, SD_PATH_STANDARD); 23943 SD_INFO(SD_LOG_IO_PARTITION, un, 23944 "sd_set_vtoc: wrote backup label %d\n", blk); 23945 } 23946 if (rval != 0) { 23947 goto exit; 23948 } 23949 } 23950 exit: 23951 if (NOT_DEVBSIZE(un)) { 23952 kmem_free(shadow_buf, un->un_tgt_blocksize); 23953 } 23954 return (rval); 23955 } 23956 23957 /* 23958 * Function: sd_clear_vtoc 23959 * 23960 * Description: This routine clears out the VTOC labels. 23961 * 23962 * Arguments: un - driver soft state (unit) structure 23963 * 23964 * Return: void 23965 */ 23966 23967 static void 23968 sd_clear_vtoc(struct sd_lun *un) 23969 { 23970 struct dk_label *dkl; 23971 23972 mutex_exit(SD_MUTEX(un)); 23973 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23974 mutex_enter(SD_MUTEX(un)); 23975 /* 23976 * sd_set_vtoc uses these fields in order to figure out 23977 * where to overwrite the backup labels 23978 */ 23979 dkl->dkl_apc = un->un_g.dkg_apc; 23980 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23981 dkl->dkl_acyl = un->un_g.dkg_acyl; 23982 dkl->dkl_nhead = un->un_g.dkg_nhead; 23983 dkl->dkl_nsect = un->un_g.dkg_nsect; 23984 mutex_exit(SD_MUTEX(un)); 23985 (void) sd_set_vtoc(un, dkl); 23986 kmem_free(dkl, sizeof (struct dk_label)); 23987 23988 mutex_enter(SD_MUTEX(un)); 23989 } 23990 23991 /* 23992 * Function: sd_write_label 23993 * 23994 * Description: This routine will validate and write the driver soft state vtoc 23995 * contents to the device. 23996 * 23997 * Arguments: dev - the device number 23998 * 23999 * Return Code: the code returned by sd_send_scsi_cmd() 24000 * 0 24001 * EINVAL 24002 * ENXIO 24003 * ENOMEM 24004 */ 24005 24006 static int 24007 sd_write_label(dev_t dev) 24008 { 24009 struct sd_lun *un; 24010 struct dk_label *dkl; 24011 short sum; 24012 short *sp; 24013 int i; 24014 int rval; 24015 24016 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24017 (un->un_state == SD_STATE_OFFLINE)) { 24018 return (ENXIO); 24019 } 24020 ASSERT(mutex_owned(SD_MUTEX(un))); 24021 mutex_exit(SD_MUTEX(un)); 24022 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 24023 mutex_enter(SD_MUTEX(un)); 24024 24025 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 24026 dkl->dkl_rpm = un->un_g.dkg_rpm; 24027 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 24028 dkl->dkl_apc = un->un_g.dkg_apc; 24029 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 24030 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 24031 dkl->dkl_acyl = un->un_g.dkg_acyl; 24032 dkl->dkl_nhead = un->un_g.dkg_nhead; 24033 dkl->dkl_nsect = un->un_g.dkg_nsect; 24034 24035 #if defined(_SUNOS_VTOC_8) 24036 dkl->dkl_obs1 = un->un_g.dkg_obs1; 24037 dkl->dkl_obs2 = un->un_g.dkg_obs2; 24038 dkl->dkl_obs3 = un->un_g.dkg_obs3; 24039 for (i = 0; i < NDKMAP; i++) { 24040 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 24041 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 24042 } 24043 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 24044 #elif defined(_SUNOS_VTOC_16) 24045 dkl->dkl_skew = un->un_dkg_skew; 24046 #else 24047 #error "No VTOC format defined." 24048 #endif 24049 24050 dkl->dkl_magic = DKL_MAGIC; 24051 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 24052 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 24053 24054 /* Construct checksum for the new disk label */ 24055 sum = 0; 24056 sp = (short *)dkl; 24057 i = sizeof (struct dk_label) / sizeof (short); 24058 while (i--) { 24059 sum ^= *sp++; 24060 } 24061 dkl->dkl_cksum = sum; 24062 24063 mutex_exit(SD_MUTEX(un)); 24064 24065 rval = sd_set_vtoc(un, dkl); 24066 exit: 24067 kmem_free(dkl, sizeof (struct dk_label)); 24068 mutex_enter(SD_MUTEX(un)); 24069 return (rval); 24070 } 24071 24072 static int 24073 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 24074 { 24075 struct sd_lun *un = NULL; 24076 dk_efi_t user_efi; 24077 int rval = 0; 24078 void *buffer; 24079 int valid_efi; 24080 24081 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 24082 return (ENXIO); 24083 24084 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 24085 return (EFAULT); 24086 24087 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 24088 24089 if ((user_efi.dki_length % un->un_tgt_blocksize) || 24090 (user_efi.dki_length > un->un_max_xfer_size)) 24091 return (EINVAL); 24092 24093 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 24094 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 24095 rval = EFAULT; 24096 } else { 24097 /* 24098 * let's clear the vtoc labels and clear the softstate 24099 * vtoc. 24100 */ 24101 mutex_enter(SD_MUTEX(un)); 24102 if (un->un_vtoc.v_sanity == VTOC_SANE) { 24103 SD_TRACE(SD_LOG_IO_PARTITION, un, 24104 "sd_dkio_set_efi: CLEAR VTOC\n"); 24105 sd_clear_vtoc(un); 24106 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24107 mutex_exit(SD_MUTEX(un)); 24108 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 24109 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 24110 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 24111 S_IFBLK, 24112 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24113 un->un_node_type, NULL); 24114 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 24115 S_IFCHR, 24116 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24117 un->un_node_type, NULL); 24118 } else 24119 mutex_exit(SD_MUTEX(un)); 24120 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 24121 user_efi.dki_lba, SD_PATH_DIRECT); 24122 if (rval == 0) { 24123 mutex_enter(SD_MUTEX(un)); 24124 24125 /* 24126 * Set the un_reserved for valid efi label. 24127 * Function clear_efi in fdisk and efi_write in 24128 * libefi both change efi label on disk in 3 steps 24129 * 1. Change primary gpt and gpe 24130 * 2. Change backup gpe 24131 * 3. Change backup gpt, which is one block 24132 * We only reread the efi label after the 3rd step, 24133 * or there will be warning "primary label corrupt". 24134 */ 24135 if (user_efi.dki_length == un->un_tgt_blocksize) { 24136 un->un_f_geometry_is_valid = FALSE; 24137 valid_efi = sd_use_efi(un, SD_PATH_DIRECT); 24138 if ((valid_efi == 0) && 24139 un->un_f_devid_supported && 24140 (un->un_f_opt_fab_devid == TRUE)) { 24141 if (un->un_devid == NULL) { 24142 sd_register_devid(un, 24143 SD_DEVINFO(un), 24144 SD_TARGET_IS_UNRESERVED); 24145 } else { 24146 /* 24147 * The device id for this disk 24148 * has been fabricated. The 24149 * device id must be preserved 24150 * by writing it back out to 24151 * disk. 24152 */ 24153 if (sd_write_deviceid(un) 24154 != 0) { 24155 ddi_devid_free( 24156 un->un_devid); 24157 un->un_devid = NULL; 24158 } 24159 } 24160 } 24161 } 24162 24163 mutex_exit(SD_MUTEX(un)); 24164 } 24165 } 24166 kmem_free(buffer, user_efi.dki_length); 24167 return (rval); 24168 } 24169 24170 /* 24171 * Function: sd_dkio_get_mboot 24172 * 24173 * Description: This routine is the driver entry point for handling user 24174 * requests to get the current device mboot (DKIOCGMBOOT) 24175 * 24176 * Arguments: dev - the device number 24177 * arg - pointer to user provided mboot structure specifying 24178 * the current mboot. 24179 * flag - this argument is a pass through to ddi_copyxxx() 24180 * directly from the mode argument of ioctl(). 24181 * 24182 * Return Code: 0 24183 * EINVAL 24184 * EFAULT 24185 * ENXIO 24186 */ 24187 24188 static int 24189 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 24190 { 24191 struct sd_lun *un; 24192 struct mboot *mboot; 24193 int rval; 24194 size_t buffer_size; 24195 24196 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24197 (un->un_state == SD_STATE_OFFLINE)) { 24198 return (ENXIO); 24199 } 24200 24201 if (!un->un_f_mboot_supported || arg == NULL) { 24202 return (EINVAL); 24203 } 24204 24205 /* 24206 * Read the mboot block, located at absolute block 0 on the target. 24207 */ 24208 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 24209 24210 SD_TRACE(SD_LOG_IO_PARTITION, un, 24211 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 24212 24213 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 24214 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 24215 SD_PATH_STANDARD)) == 0) { 24216 if (ddi_copyout(mboot, (void *)arg, 24217 sizeof (struct mboot), flag) != 0) { 24218 rval = EFAULT; 24219 } 24220 } 24221 kmem_free(mboot, buffer_size); 24222 return (rval); 24223 } 24224 24225 24226 /* 24227 * Function: sd_dkio_set_mboot 24228 * 24229 * Description: This routine is the driver entry point for handling user 24230 * requests to validate and set the device master boot 24231 * (DKIOCSMBOOT). 24232 * 24233 * Arguments: dev - the device number 24234 * arg - pointer to user provided mboot structure used to set the 24235 * master boot. 24236 * flag - this argument is a pass through to ddi_copyxxx() 24237 * directly from the mode argument of ioctl(). 24238 * 24239 * Return Code: 0 24240 * EINVAL 24241 * EFAULT 24242 * ENXIO 24243 */ 24244 24245 static int 24246 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 24247 { 24248 struct sd_lun *un = NULL; 24249 struct mboot *mboot = NULL; 24250 int rval; 24251 ushort_t magic; 24252 24253 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24254 return (ENXIO); 24255 } 24256 24257 ASSERT(!mutex_owned(SD_MUTEX(un))); 24258 24259 if (!un->un_f_mboot_supported) { 24260 return (EINVAL); 24261 } 24262 24263 if (arg == NULL) { 24264 return (EINVAL); 24265 } 24266 24267 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 24268 24269 if (ddi_copyin((const void *)arg, mboot, 24270 sizeof (struct mboot), flag) != 0) { 24271 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24272 return (EFAULT); 24273 } 24274 24275 /* Is this really a master boot record? */ 24276 magic = LE_16(mboot->signature); 24277 if (magic != MBB_MAGIC) { 24278 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24279 return (EINVAL); 24280 } 24281 24282 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 24283 SD_PATH_STANDARD); 24284 24285 mutex_enter(SD_MUTEX(un)); 24286 #if defined(__i386) || defined(__amd64) 24287 if (rval == 0) { 24288 /* 24289 * mboot has been written successfully. 24290 * update the fdisk and vtoc tables in memory 24291 */ 24292 rval = sd_update_fdisk_and_vtoc(un); 24293 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 24294 mutex_exit(SD_MUTEX(un)); 24295 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24296 return (rval); 24297 } 24298 } 24299 24300 #ifdef __lock_lint 24301 sd_setup_default_geometry(un); 24302 #endif 24303 24304 #else 24305 if (rval == 0) { 24306 /* 24307 * mboot has been written successfully. 24308 * set up the default geometry and VTOC 24309 */ 24310 if (un->un_blockcount <= DK_MAX_BLOCKS) 24311 sd_setup_default_geometry(un); 24312 } 24313 #endif 24314 mutex_exit(SD_MUTEX(un)); 24315 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24316 return (rval); 24317 } 24318 24319 24320 /* 24321 * Function: sd_setup_default_geometry 24322 * 24323 * Description: This local utility routine sets the default geometry as part of 24324 * setting the device mboot. 24325 * 24326 * Arguments: un - driver soft state (unit) structure 24327 * 24328 * Note: This may be redundant with sd_build_default_label. 24329 */ 24330 24331 static void 24332 sd_setup_default_geometry(struct sd_lun *un) 24333 { 24334 /* zero out the soft state geometry and partition table. */ 24335 bzero(&un->un_g, sizeof (struct dk_geom)); 24336 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24337 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 24338 un->un_asciilabel[0] = '\0'; 24339 24340 /* 24341 * For the rpm, we use the minimum for the disk. 24342 * For the head, cyl and number of sector per track, 24343 * if the capacity <= 1GB, head = 64, sect = 32. 24344 * else head = 255, sect 63 24345 * Note: the capacity should be equal to C*H*S values. 24346 * This will cause some truncation of size due to 24347 * round off errors. For CD-ROMs, this truncation can 24348 * have adverse side effects, so returning ncyl and 24349 * nhead as 1. The nsect will overflow for most of 24350 * CD-ROMs as nsect is of type ushort. 24351 */ 24352 if (ISCD(un)) { 24353 un->un_g.dkg_ncyl = 1; 24354 un->un_g.dkg_nhead = 1; 24355 un->un_g.dkg_nsect = un->un_blockcount; 24356 } else { 24357 if (un->un_blockcount <= 0x1000) { 24358 /* Needed for unlabeled SCSI floppies. */ 24359 un->un_g.dkg_nhead = 2; 24360 un->un_g.dkg_ncyl = 80; 24361 un->un_g.dkg_pcyl = 80; 24362 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 24363 } else if (un->un_blockcount <= 0x200000) { 24364 un->un_g.dkg_nhead = 64; 24365 un->un_g.dkg_nsect = 32; 24366 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 24367 } else { 24368 un->un_g.dkg_nhead = 255; 24369 un->un_g.dkg_nsect = 63; 24370 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 24371 } 24372 un->un_blockcount = un->un_g.dkg_ncyl * 24373 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 24374 } 24375 un->un_g.dkg_acyl = 0; 24376 un->un_g.dkg_bcyl = 0; 24377 un->un_g.dkg_intrlv = 1; 24378 un->un_g.dkg_rpm = 200; 24379 un->un_g.dkg_read_reinstruct = 0; 24380 un->un_g.dkg_write_reinstruct = 0; 24381 if (un->un_g.dkg_pcyl == 0) { 24382 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 24383 } 24384 24385 un->un_map['a'-'a'].dkl_cylno = 0; 24386 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 24387 un->un_map['c'-'a'].dkl_cylno = 0; 24388 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 24389 un->un_f_geometry_is_valid = FALSE; 24390 } 24391 24392 24393 #if defined(__i386) || defined(__amd64) 24394 /* 24395 * Function: sd_update_fdisk_and_vtoc 24396 * 24397 * Description: This local utility routine updates the device fdisk and vtoc 24398 * as part of setting the device mboot. 24399 * 24400 * Arguments: un - driver soft state (unit) structure 24401 * 24402 * Return Code: 0 for success or errno-type return code. 24403 * 24404 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 24405 * these did exist seperately in x86 sd.c!!! 24406 */ 24407 24408 static int 24409 sd_update_fdisk_and_vtoc(struct sd_lun *un) 24410 { 24411 static char labelstring[128]; 24412 static char buf[256]; 24413 char *label = 0; 24414 int count; 24415 int label_rc = 0; 24416 int gvalid = un->un_f_geometry_is_valid; 24417 int fdisk_rval; 24418 int lbasize; 24419 int capacity; 24420 24421 ASSERT(mutex_owned(SD_MUTEX(un))); 24422 24423 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 24424 return (EINVAL); 24425 } 24426 24427 if (un->un_f_blockcount_is_valid == FALSE) { 24428 return (EINVAL); 24429 } 24430 24431 #if defined(_SUNOS_VTOC_16) 24432 /* 24433 * Set up the "whole disk" fdisk partition; this should always 24434 * exist, regardless of whether the disk contains an fdisk table 24435 * or vtoc. 24436 */ 24437 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 24438 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 24439 #endif /* defined(_SUNOS_VTOC_16) */ 24440 24441 /* 24442 * copy the lbasize and capacity so that if they're 24443 * reset while we're not holding the SD_MUTEX(un), we will 24444 * continue to use valid values after the SD_MUTEX(un) is 24445 * reacquired. 24446 */ 24447 lbasize = un->un_tgt_blocksize; 24448 capacity = un->un_blockcount; 24449 24450 /* 24451 * refresh the logical and physical geometry caches. 24452 * (data from mode sense format/rigid disk geometry pages, 24453 * and scsi_ifgetcap("geometry"). 24454 */ 24455 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 24456 24457 /* 24458 * Only DIRECT ACCESS devices will have Sun labels. 24459 * CD's supposedly have a Sun label, too 24460 */ 24461 if (un->un_f_vtoc_label_supported) { 24462 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 24463 SD_PATH_DIRECT); 24464 if (fdisk_rval == SD_CMD_FAILURE) { 24465 ASSERT(mutex_owned(SD_MUTEX(un))); 24466 return (EIO); 24467 } 24468 24469 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 24470 ASSERT(mutex_owned(SD_MUTEX(un))); 24471 return (EACCES); 24472 } 24473 24474 if (un->un_solaris_size <= DK_LABEL_LOC) { 24475 /* 24476 * Found fdisk table but no Solaris partition entry, 24477 * so don't call sd_uselabel() and don't create 24478 * a default label. 24479 */ 24480 label_rc = 0; 24481 un->un_f_geometry_is_valid = TRUE; 24482 goto no_solaris_partition; 24483 } 24484 24485 #if defined(_SUNOS_VTOC_8) 24486 label = (char *)un->un_asciilabel; 24487 #elif defined(_SUNOS_VTOC_16) 24488 label = (char *)un->un_vtoc.v_asciilabel; 24489 #else 24490 #error "No VTOC format defined." 24491 #endif 24492 } else if (capacity < 0) { 24493 ASSERT(mutex_owned(SD_MUTEX(un))); 24494 return (EINVAL); 24495 } 24496 24497 /* 24498 * For Removable media We reach here if we have found a 24499 * SOLARIS PARTITION. 24500 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24501 * PARTITION has changed from the previous one, hence we will setup a 24502 * default VTOC in this case. 24503 */ 24504 if (un->un_f_geometry_is_valid == FALSE) { 24505 sd_build_default_label(un); 24506 label_rc = 0; 24507 } 24508 24509 no_solaris_partition: 24510 if ((!un->un_f_has_removable_media || 24511 (un->un_f_has_removable_media && 24512 un->un_mediastate == DKIO_EJECTED)) && 24513 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24514 /* 24515 * Print out a message indicating who and what we are. 24516 * We do this only when we happen to really validate the 24517 * geometry. We may call sd_validate_geometry() at other 24518 * times, ioctl()'s like Get VTOC in which case we 24519 * don't want to print the label. 24520 * If the geometry is valid, print the label string, 24521 * else print vendor and product info, if available 24522 */ 24523 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24524 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24525 } else { 24526 mutex_enter(&sd_label_mutex); 24527 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24528 labelstring); 24529 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24530 &labelstring[64]); 24531 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24532 labelstring, &labelstring[64]); 24533 if (un->un_f_blockcount_is_valid == TRUE) { 24534 (void) sprintf(&buf[strlen(buf)], 24535 ", %" PRIu64 " %u byte blocks\n", 24536 un->un_blockcount, 24537 un->un_tgt_blocksize); 24538 } else { 24539 (void) sprintf(&buf[strlen(buf)], 24540 ", (unknown capacity)\n"); 24541 } 24542 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24543 mutex_exit(&sd_label_mutex); 24544 } 24545 } 24546 24547 #if defined(_SUNOS_VTOC_16) 24548 /* 24549 * If we have valid geometry, set up the remaining fdisk partitions. 24550 * Note that dkl_cylno is not used for the fdisk map entries, so 24551 * we set it to an entirely bogus value. 24552 */ 24553 for (count = 0; count < FD_NUMPART; count++) { 24554 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24555 un->un_map[FDISK_P1 + count].dkl_nblk = 24556 un->un_fmap[count].fmap_nblk; 24557 un->un_offset[FDISK_P1 + count] = 24558 un->un_fmap[count].fmap_start; 24559 } 24560 #endif 24561 24562 for (count = 0; count < NDKMAP; count++) { 24563 #if defined(_SUNOS_VTOC_8) 24564 struct dk_map *lp = &un->un_map[count]; 24565 un->un_offset[count] = 24566 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24567 #elif defined(_SUNOS_VTOC_16) 24568 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24569 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24570 #else 24571 #error "No VTOC format defined." 24572 #endif 24573 } 24574 24575 ASSERT(mutex_owned(SD_MUTEX(un))); 24576 return (label_rc); 24577 } 24578 #endif 24579 24580 24581 /* 24582 * Function: sd_check_media 24583 * 24584 * Description: This utility routine implements the functionality for the 24585 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24586 * driver state changes from that specified by the user 24587 * (inserted or ejected). For example, if the user specifies 24588 * DKIO_EJECTED and the current media state is inserted this 24589 * routine will immediately return DKIO_INSERTED. However, if the 24590 * current media state is not inserted the user thread will be 24591 * blocked until the drive state changes. If DKIO_NONE is specified 24592 * the user thread will block until a drive state change occurs. 24593 * 24594 * Arguments: dev - the device number 24595 * state - user pointer to a dkio_state, updated with the current 24596 * drive state at return. 24597 * 24598 * Return Code: ENXIO 24599 * EIO 24600 * EAGAIN 24601 * EINTR 24602 */ 24603 24604 static int 24605 sd_check_media(dev_t dev, enum dkio_state state) 24606 { 24607 struct sd_lun *un = NULL; 24608 enum dkio_state prev_state; 24609 opaque_t token = NULL; 24610 int rval = 0; 24611 24612 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24613 return (ENXIO); 24614 } 24615 24616 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24617 24618 mutex_enter(SD_MUTEX(un)); 24619 24620 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24621 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24622 24623 prev_state = un->un_mediastate; 24624 24625 /* is there anything to do? */ 24626 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24627 /* 24628 * submit the request to the scsi_watch service; 24629 * scsi_media_watch_cb() does the real work 24630 */ 24631 mutex_exit(SD_MUTEX(un)); 24632 24633 /* 24634 * This change handles the case where a scsi watch request is 24635 * added to a device that is powered down. To accomplish this 24636 * we power up the device before adding the scsi watch request, 24637 * since the scsi watch sends a TUR directly to the device 24638 * which the device cannot handle if it is powered down. 24639 */ 24640 if (sd_pm_entry(un) != DDI_SUCCESS) { 24641 mutex_enter(SD_MUTEX(un)); 24642 goto done; 24643 } 24644 24645 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24646 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24647 (caddr_t)dev); 24648 24649 sd_pm_exit(un); 24650 24651 mutex_enter(SD_MUTEX(un)); 24652 if (token == NULL) { 24653 rval = EAGAIN; 24654 goto done; 24655 } 24656 24657 /* 24658 * This is a special case IOCTL that doesn't return 24659 * until the media state changes. Routine sdpower 24660 * knows about and handles this so don't count it 24661 * as an active cmd in the driver, which would 24662 * keep the device busy to the pm framework. 24663 * If the count isn't decremented the device can't 24664 * be powered down. 24665 */ 24666 un->un_ncmds_in_driver--; 24667 ASSERT(un->un_ncmds_in_driver >= 0); 24668 24669 /* 24670 * if a prior request had been made, this will be the same 24671 * token, as scsi_watch was designed that way. 24672 */ 24673 un->un_swr_token = token; 24674 un->un_specified_mediastate = state; 24675 24676 /* 24677 * now wait for media change 24678 * we will not be signalled unless mediastate == state but it is 24679 * still better to test for this condition, since there is a 24680 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24681 */ 24682 SD_TRACE(SD_LOG_COMMON, un, 24683 "sd_check_media: waiting for media state change\n"); 24684 while (un->un_mediastate == state) { 24685 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24686 SD_TRACE(SD_LOG_COMMON, un, 24687 "sd_check_media: waiting for media state " 24688 "was interrupted\n"); 24689 un->un_ncmds_in_driver++; 24690 rval = EINTR; 24691 goto done; 24692 } 24693 SD_TRACE(SD_LOG_COMMON, un, 24694 "sd_check_media: received signal, state=%x\n", 24695 un->un_mediastate); 24696 } 24697 /* 24698 * Inc the counter to indicate the device once again 24699 * has an active outstanding cmd. 24700 */ 24701 un->un_ncmds_in_driver++; 24702 } 24703 24704 /* invalidate geometry */ 24705 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24706 sr_ejected(un); 24707 } 24708 24709 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24710 uint64_t capacity; 24711 uint_t lbasize; 24712 24713 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24714 mutex_exit(SD_MUTEX(un)); 24715 /* 24716 * Since the following routines use SD_PATH_DIRECT, we must 24717 * call PM directly before the upcoming disk accesses. This 24718 * may cause the disk to be power/spin up. 24719 */ 24720 24721 if (sd_pm_entry(un) == DDI_SUCCESS) { 24722 rval = sd_send_scsi_READ_CAPACITY(un, 24723 &capacity, 24724 &lbasize, SD_PATH_DIRECT); 24725 if (rval != 0) { 24726 sd_pm_exit(un); 24727 mutex_enter(SD_MUTEX(un)); 24728 goto done; 24729 } 24730 } else { 24731 rval = EIO; 24732 mutex_enter(SD_MUTEX(un)); 24733 goto done; 24734 } 24735 mutex_enter(SD_MUTEX(un)); 24736 24737 sd_update_block_info(un, lbasize, capacity); 24738 24739 un->un_f_geometry_is_valid = FALSE; 24740 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24741 24742 mutex_exit(SD_MUTEX(un)); 24743 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24744 SD_PATH_DIRECT); 24745 sd_pm_exit(un); 24746 24747 mutex_enter(SD_MUTEX(un)); 24748 } 24749 done: 24750 un->un_f_watcht_stopped = FALSE; 24751 if (un->un_swr_token) { 24752 /* 24753 * Use of this local token and the mutex ensures that we avoid 24754 * some race conditions associated with terminating the 24755 * scsi watch. 24756 */ 24757 token = un->un_swr_token; 24758 un->un_swr_token = (opaque_t)NULL; 24759 mutex_exit(SD_MUTEX(un)); 24760 (void) scsi_watch_request_terminate(token, 24761 SCSI_WATCH_TERMINATE_WAIT); 24762 mutex_enter(SD_MUTEX(un)); 24763 } 24764 24765 /* 24766 * Update the capacity kstat value, if no media previously 24767 * (capacity kstat is 0) and a media has been inserted 24768 * (un_f_blockcount_is_valid == TRUE) 24769 */ 24770 if (un->un_errstats) { 24771 struct sd_errstats *stp = NULL; 24772 24773 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24774 if ((stp->sd_capacity.value.ui64 == 0) && 24775 (un->un_f_blockcount_is_valid == TRUE)) { 24776 stp->sd_capacity.value.ui64 = 24777 (uint64_t)((uint64_t)un->un_blockcount * 24778 un->un_sys_blocksize); 24779 } 24780 } 24781 mutex_exit(SD_MUTEX(un)); 24782 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24783 return (rval); 24784 } 24785 24786 24787 /* 24788 * Function: sd_delayed_cv_broadcast 24789 * 24790 * Description: Delayed cv_broadcast to allow for target to recover from media 24791 * insertion. 24792 * 24793 * Arguments: arg - driver soft state (unit) structure 24794 */ 24795 24796 static void 24797 sd_delayed_cv_broadcast(void *arg) 24798 { 24799 struct sd_lun *un = arg; 24800 24801 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24802 24803 mutex_enter(SD_MUTEX(un)); 24804 un->un_dcvb_timeid = NULL; 24805 cv_broadcast(&un->un_state_cv); 24806 mutex_exit(SD_MUTEX(un)); 24807 } 24808 24809 24810 /* 24811 * Function: sd_media_watch_cb 24812 * 24813 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24814 * routine processes the TUR sense data and updates the driver 24815 * state if a transition has occurred. The user thread 24816 * (sd_check_media) is then signalled. 24817 * 24818 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24819 * among multiple watches that share this callback function 24820 * resultp - scsi watch facility result packet containing scsi 24821 * packet, status byte and sense data 24822 * 24823 * Return Code: 0 for success, -1 for failure 24824 */ 24825 24826 static int 24827 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24828 { 24829 struct sd_lun *un; 24830 struct scsi_status *statusp = resultp->statusp; 24831 uint8_t *sensep = (uint8_t *)resultp->sensep; 24832 enum dkio_state state = DKIO_NONE; 24833 dev_t dev = (dev_t)arg; 24834 uchar_t actual_sense_length; 24835 uint8_t skey, asc, ascq; 24836 24837 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24838 return (-1); 24839 } 24840 actual_sense_length = resultp->actual_sense_length; 24841 24842 mutex_enter(SD_MUTEX(un)); 24843 SD_TRACE(SD_LOG_COMMON, un, 24844 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24845 *((char *)statusp), (void *)sensep, actual_sense_length); 24846 24847 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24848 un->un_mediastate = DKIO_DEV_GONE; 24849 cv_broadcast(&un->un_state_cv); 24850 mutex_exit(SD_MUTEX(un)); 24851 24852 return (0); 24853 } 24854 24855 /* 24856 * If there was a check condition then sensep points to valid sense data 24857 * If status was not a check condition but a reservation or busy status 24858 * then the new state is DKIO_NONE 24859 */ 24860 if (sensep != NULL) { 24861 skey = scsi_sense_key(sensep); 24862 asc = scsi_sense_asc(sensep); 24863 ascq = scsi_sense_ascq(sensep); 24864 24865 SD_INFO(SD_LOG_COMMON, un, 24866 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24867 skey, asc, ascq); 24868 /* This routine only uses up to 13 bytes of sense data. */ 24869 if (actual_sense_length >= 13) { 24870 if (skey == KEY_UNIT_ATTENTION) { 24871 if (asc == 0x28) { 24872 state = DKIO_INSERTED; 24873 } 24874 } else { 24875 /* 24876 * if 02/04/02 means that the host 24877 * should send start command. Explicitly 24878 * leave the media state as is 24879 * (inserted) as the media is inserted 24880 * and host has stopped device for PM 24881 * reasons. Upon next true read/write 24882 * to this media will bring the 24883 * device to the right state good for 24884 * media access. 24885 */ 24886 if ((skey == KEY_NOT_READY) && 24887 (asc == 0x3a)) { 24888 state = DKIO_EJECTED; 24889 } 24890 24891 /* 24892 * If the drivge is busy with an operation 24893 * or long write, keep the media in an 24894 * inserted state. 24895 */ 24896 24897 if ((skey == KEY_NOT_READY) && 24898 (asc == 0x04) && 24899 ((ascq == 0x02) || 24900 (ascq == 0x07) || 24901 (ascq == 0x08))) { 24902 state = DKIO_INSERTED; 24903 } 24904 } 24905 } 24906 } else if ((*((char *)statusp) == STATUS_GOOD) && 24907 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24908 state = DKIO_INSERTED; 24909 } 24910 24911 SD_TRACE(SD_LOG_COMMON, un, 24912 "sd_media_watch_cb: state=%x, specified=%x\n", 24913 state, un->un_specified_mediastate); 24914 24915 /* 24916 * now signal the waiting thread if this is *not* the specified state; 24917 * delay the signal if the state is DKIO_INSERTED to allow the target 24918 * to recover 24919 */ 24920 if (state != un->un_specified_mediastate) { 24921 un->un_mediastate = state; 24922 if (state == DKIO_INSERTED) { 24923 /* 24924 * delay the signal to give the drive a chance 24925 * to do what it apparently needs to do 24926 */ 24927 SD_TRACE(SD_LOG_COMMON, un, 24928 "sd_media_watch_cb: delayed cv_broadcast\n"); 24929 if (un->un_dcvb_timeid == NULL) { 24930 un->un_dcvb_timeid = 24931 timeout(sd_delayed_cv_broadcast, un, 24932 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24933 } 24934 } else { 24935 SD_TRACE(SD_LOG_COMMON, un, 24936 "sd_media_watch_cb: immediate cv_broadcast\n"); 24937 cv_broadcast(&un->un_state_cv); 24938 } 24939 } 24940 mutex_exit(SD_MUTEX(un)); 24941 return (0); 24942 } 24943 24944 24945 /* 24946 * Function: sd_dkio_get_temp 24947 * 24948 * Description: This routine is the driver entry point for handling ioctl 24949 * requests to get the disk temperature. 24950 * 24951 * Arguments: dev - the device number 24952 * arg - pointer to user provided dk_temperature structure. 24953 * flag - this argument is a pass through to ddi_copyxxx() 24954 * directly from the mode argument of ioctl(). 24955 * 24956 * Return Code: 0 24957 * EFAULT 24958 * ENXIO 24959 * EAGAIN 24960 */ 24961 24962 static int 24963 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24964 { 24965 struct sd_lun *un = NULL; 24966 struct dk_temperature *dktemp = NULL; 24967 uchar_t *temperature_page; 24968 int rval = 0; 24969 int path_flag = SD_PATH_STANDARD; 24970 24971 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24972 return (ENXIO); 24973 } 24974 24975 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24976 24977 /* copyin the disk temp argument to get the user flags */ 24978 if (ddi_copyin((void *)arg, dktemp, 24979 sizeof (struct dk_temperature), flag) != 0) { 24980 rval = EFAULT; 24981 goto done; 24982 } 24983 24984 /* Initialize the temperature to invalid. */ 24985 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24986 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24987 24988 /* 24989 * Note: Investigate removing the "bypass pm" semantic. 24990 * Can we just bypass PM always? 24991 */ 24992 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 24993 path_flag = SD_PATH_DIRECT; 24994 ASSERT(!mutex_owned(&un->un_pm_mutex)); 24995 mutex_enter(&un->un_pm_mutex); 24996 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 24997 /* 24998 * If DKT_BYPASS_PM is set, and the drive happens to be 24999 * in low power mode, we can not wake it up, Need to 25000 * return EAGAIN. 25001 */ 25002 mutex_exit(&un->un_pm_mutex); 25003 rval = EAGAIN; 25004 goto done; 25005 } else { 25006 /* 25007 * Indicate to PM the device is busy. This is required 25008 * to avoid a race - i.e. the ioctl is issuing a 25009 * command and the pm framework brings down the device 25010 * to low power mode (possible power cut-off on some 25011 * platforms). 25012 */ 25013 mutex_exit(&un->un_pm_mutex); 25014 if (sd_pm_entry(un) != DDI_SUCCESS) { 25015 rval = EAGAIN; 25016 goto done; 25017 } 25018 } 25019 } 25020 25021 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 25022 25023 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 25024 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 25025 goto done2; 25026 } 25027 25028 /* 25029 * For the current temperature verify that the parameter length is 0x02 25030 * and the parameter code is 0x00 25031 */ 25032 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 25033 (temperature_page[5] == 0x00)) { 25034 if (temperature_page[9] == 0xFF) { 25035 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 25036 } else { 25037 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 25038 } 25039 } 25040 25041 /* 25042 * For the reference temperature verify that the parameter 25043 * length is 0x02 and the parameter code is 0x01 25044 */ 25045 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 25046 (temperature_page[11] == 0x01)) { 25047 if (temperature_page[15] == 0xFF) { 25048 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 25049 } else { 25050 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 25051 } 25052 } 25053 25054 /* Do the copyout regardless of the temperature commands status. */ 25055 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 25056 flag) != 0) { 25057 rval = EFAULT; 25058 } 25059 25060 done2: 25061 if (path_flag == SD_PATH_DIRECT) { 25062 sd_pm_exit(un); 25063 } 25064 25065 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 25066 done: 25067 if (dktemp != NULL) { 25068 kmem_free(dktemp, sizeof (struct dk_temperature)); 25069 } 25070 25071 return (rval); 25072 } 25073 25074 25075 /* 25076 * Function: sd_log_page_supported 25077 * 25078 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 25079 * supported log pages. 25080 * 25081 * Arguments: un - 25082 * log_page - 25083 * 25084 * Return Code: -1 - on error (log sense is optional and may not be supported). 25085 * 0 - log page not found. 25086 * 1 - log page found. 25087 */ 25088 25089 static int 25090 sd_log_page_supported(struct sd_lun *un, int log_page) 25091 { 25092 uchar_t *log_page_data; 25093 int i; 25094 int match = 0; 25095 int log_size; 25096 25097 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 25098 25099 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 25100 SD_PATH_DIRECT) != 0) { 25101 SD_ERROR(SD_LOG_COMMON, un, 25102 "sd_log_page_supported: failed log page retrieval\n"); 25103 kmem_free(log_page_data, 0xFF); 25104 return (-1); 25105 } 25106 log_size = log_page_data[3]; 25107 25108 /* 25109 * The list of supported log pages start from the fourth byte. Check 25110 * until we run out of log pages or a match is found. 25111 */ 25112 for (i = 4; (i < (log_size + 4)) && !match; i++) { 25113 if (log_page_data[i] == log_page) { 25114 match++; 25115 } 25116 } 25117 kmem_free(log_page_data, 0xFF); 25118 return (match); 25119 } 25120 25121 25122 /* 25123 * Function: sd_mhdioc_failfast 25124 * 25125 * Description: This routine is the driver entry point for handling ioctl 25126 * requests to enable/disable the multihost failfast option. 25127 * (MHIOCENFAILFAST) 25128 * 25129 * Arguments: dev - the device number 25130 * arg - user specified probing interval. 25131 * flag - this argument is a pass through to ddi_copyxxx() 25132 * directly from the mode argument of ioctl(). 25133 * 25134 * Return Code: 0 25135 * EFAULT 25136 * ENXIO 25137 */ 25138 25139 static int 25140 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 25141 { 25142 struct sd_lun *un = NULL; 25143 int mh_time; 25144 int rval = 0; 25145 25146 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25147 return (ENXIO); 25148 } 25149 25150 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 25151 return (EFAULT); 25152 25153 if (mh_time) { 25154 mutex_enter(SD_MUTEX(un)); 25155 un->un_resvd_status |= SD_FAILFAST; 25156 mutex_exit(SD_MUTEX(un)); 25157 /* 25158 * If mh_time is INT_MAX, then this ioctl is being used for 25159 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 25160 */ 25161 if (mh_time != INT_MAX) { 25162 rval = sd_check_mhd(dev, mh_time); 25163 } 25164 } else { 25165 (void) sd_check_mhd(dev, 0); 25166 mutex_enter(SD_MUTEX(un)); 25167 un->un_resvd_status &= ~SD_FAILFAST; 25168 mutex_exit(SD_MUTEX(un)); 25169 } 25170 return (rval); 25171 } 25172 25173 25174 /* 25175 * Function: sd_mhdioc_takeown 25176 * 25177 * Description: This routine is the driver entry point for handling ioctl 25178 * requests to forcefully acquire exclusive access rights to the 25179 * multihost disk (MHIOCTKOWN). 25180 * 25181 * Arguments: dev - the device number 25182 * arg - user provided structure specifying the delay 25183 * parameters in milliseconds 25184 * flag - this argument is a pass through to ddi_copyxxx() 25185 * directly from the mode argument of ioctl(). 25186 * 25187 * Return Code: 0 25188 * EFAULT 25189 * ENXIO 25190 */ 25191 25192 static int 25193 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 25194 { 25195 struct sd_lun *un = NULL; 25196 struct mhioctkown *tkown = NULL; 25197 int rval = 0; 25198 25199 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25200 return (ENXIO); 25201 } 25202 25203 if (arg != NULL) { 25204 tkown = (struct mhioctkown *) 25205 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 25206 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 25207 if (rval != 0) { 25208 rval = EFAULT; 25209 goto error; 25210 } 25211 } 25212 25213 rval = sd_take_ownership(dev, tkown); 25214 mutex_enter(SD_MUTEX(un)); 25215 if (rval == 0) { 25216 un->un_resvd_status |= SD_RESERVE; 25217 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 25218 sd_reinstate_resv_delay = 25219 tkown->reinstate_resv_delay * 1000; 25220 } else { 25221 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 25222 } 25223 /* 25224 * Give the scsi_watch routine interval set by 25225 * the MHIOCENFAILFAST ioctl precedence here. 25226 */ 25227 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 25228 mutex_exit(SD_MUTEX(un)); 25229 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 25230 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25231 "sd_mhdioc_takeown : %d\n", 25232 sd_reinstate_resv_delay); 25233 } else { 25234 mutex_exit(SD_MUTEX(un)); 25235 } 25236 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 25237 sd_mhd_reset_notify_cb, (caddr_t)un); 25238 } else { 25239 un->un_resvd_status &= ~SD_RESERVE; 25240 mutex_exit(SD_MUTEX(un)); 25241 } 25242 25243 error: 25244 if (tkown != NULL) { 25245 kmem_free(tkown, sizeof (struct mhioctkown)); 25246 } 25247 return (rval); 25248 } 25249 25250 25251 /* 25252 * Function: sd_mhdioc_release 25253 * 25254 * Description: This routine is the driver entry point for handling ioctl 25255 * requests to release exclusive access rights to the multihost 25256 * disk (MHIOCRELEASE). 25257 * 25258 * Arguments: dev - the device number 25259 * 25260 * Return Code: 0 25261 * ENXIO 25262 */ 25263 25264 static int 25265 sd_mhdioc_release(dev_t dev) 25266 { 25267 struct sd_lun *un = NULL; 25268 timeout_id_t resvd_timeid_save; 25269 int resvd_status_save; 25270 int rval = 0; 25271 25272 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25273 return (ENXIO); 25274 } 25275 25276 mutex_enter(SD_MUTEX(un)); 25277 resvd_status_save = un->un_resvd_status; 25278 un->un_resvd_status &= 25279 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 25280 if (un->un_resvd_timeid) { 25281 resvd_timeid_save = un->un_resvd_timeid; 25282 un->un_resvd_timeid = NULL; 25283 mutex_exit(SD_MUTEX(un)); 25284 (void) untimeout(resvd_timeid_save); 25285 } else { 25286 mutex_exit(SD_MUTEX(un)); 25287 } 25288 25289 /* 25290 * destroy any pending timeout thread that may be attempting to 25291 * reinstate reservation on this device. 25292 */ 25293 sd_rmv_resv_reclaim_req(dev); 25294 25295 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 25296 mutex_enter(SD_MUTEX(un)); 25297 if ((un->un_mhd_token) && 25298 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 25299 mutex_exit(SD_MUTEX(un)); 25300 (void) sd_check_mhd(dev, 0); 25301 } else { 25302 mutex_exit(SD_MUTEX(un)); 25303 } 25304 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 25305 sd_mhd_reset_notify_cb, (caddr_t)un); 25306 } else { 25307 /* 25308 * sd_mhd_watch_cb will restart the resvd recover timeout thread 25309 */ 25310 mutex_enter(SD_MUTEX(un)); 25311 un->un_resvd_status = resvd_status_save; 25312 mutex_exit(SD_MUTEX(un)); 25313 } 25314 return (rval); 25315 } 25316 25317 25318 /* 25319 * Function: sd_mhdioc_register_devid 25320 * 25321 * Description: This routine is the driver entry point for handling ioctl 25322 * requests to register the device id (MHIOCREREGISTERDEVID). 25323 * 25324 * Note: The implementation for this ioctl has been updated to 25325 * be consistent with the original PSARC case (1999/357) 25326 * (4375899, 4241671, 4220005) 25327 * 25328 * Arguments: dev - the device number 25329 * 25330 * Return Code: 0 25331 * ENXIO 25332 */ 25333 25334 static int 25335 sd_mhdioc_register_devid(dev_t dev) 25336 { 25337 struct sd_lun *un = NULL; 25338 int rval = 0; 25339 25340 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25341 return (ENXIO); 25342 } 25343 25344 ASSERT(!mutex_owned(SD_MUTEX(un))); 25345 25346 mutex_enter(SD_MUTEX(un)); 25347 25348 /* If a devid already exists, de-register it */ 25349 if (un->un_devid != NULL) { 25350 ddi_devid_unregister(SD_DEVINFO(un)); 25351 /* 25352 * After unregister devid, needs to free devid memory 25353 */ 25354 ddi_devid_free(un->un_devid); 25355 un->un_devid = NULL; 25356 } 25357 25358 /* Check for reservation conflict */ 25359 mutex_exit(SD_MUTEX(un)); 25360 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 25361 mutex_enter(SD_MUTEX(un)); 25362 25363 switch (rval) { 25364 case 0: 25365 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 25366 break; 25367 case EACCES: 25368 break; 25369 default: 25370 rval = EIO; 25371 } 25372 25373 mutex_exit(SD_MUTEX(un)); 25374 return (rval); 25375 } 25376 25377 25378 /* 25379 * Function: sd_mhdioc_inkeys 25380 * 25381 * Description: This routine is the driver entry point for handling ioctl 25382 * requests to issue the SCSI-3 Persistent In Read Keys command 25383 * to the device (MHIOCGRP_INKEYS). 25384 * 25385 * Arguments: dev - the device number 25386 * arg - user provided in_keys structure 25387 * flag - this argument is a pass through to ddi_copyxxx() 25388 * directly from the mode argument of ioctl(). 25389 * 25390 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 25391 * ENXIO 25392 * EFAULT 25393 */ 25394 25395 static int 25396 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 25397 { 25398 struct sd_lun *un; 25399 mhioc_inkeys_t inkeys; 25400 int rval = 0; 25401 25402 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25403 return (ENXIO); 25404 } 25405 25406 #ifdef _MULTI_DATAMODEL 25407 switch (ddi_model_convert_from(flag & FMODELS)) { 25408 case DDI_MODEL_ILP32: { 25409 struct mhioc_inkeys32 inkeys32; 25410 25411 if (ddi_copyin(arg, &inkeys32, 25412 sizeof (struct mhioc_inkeys32), flag) != 0) { 25413 return (EFAULT); 25414 } 25415 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 25416 if ((rval = sd_persistent_reservation_in_read_keys(un, 25417 &inkeys, flag)) != 0) { 25418 return (rval); 25419 } 25420 inkeys32.generation = inkeys.generation; 25421 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 25422 flag) != 0) { 25423 return (EFAULT); 25424 } 25425 break; 25426 } 25427 case DDI_MODEL_NONE: 25428 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 25429 flag) != 0) { 25430 return (EFAULT); 25431 } 25432 if ((rval = sd_persistent_reservation_in_read_keys(un, 25433 &inkeys, flag)) != 0) { 25434 return (rval); 25435 } 25436 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 25437 flag) != 0) { 25438 return (EFAULT); 25439 } 25440 break; 25441 } 25442 25443 #else /* ! _MULTI_DATAMODEL */ 25444 25445 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 25446 return (EFAULT); 25447 } 25448 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 25449 if (rval != 0) { 25450 return (rval); 25451 } 25452 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 25453 return (EFAULT); 25454 } 25455 25456 #endif /* _MULTI_DATAMODEL */ 25457 25458 return (rval); 25459 } 25460 25461 25462 /* 25463 * Function: sd_mhdioc_inresv 25464 * 25465 * Description: This routine is the driver entry point for handling ioctl 25466 * requests to issue the SCSI-3 Persistent In Read Reservations 25467 * command to the device (MHIOCGRP_INKEYS). 25468 * 25469 * Arguments: dev - the device number 25470 * arg - user provided in_resv structure 25471 * flag - this argument is a pass through to ddi_copyxxx() 25472 * directly from the mode argument of ioctl(). 25473 * 25474 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 25475 * ENXIO 25476 * EFAULT 25477 */ 25478 25479 static int 25480 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25481 { 25482 struct sd_lun *un; 25483 mhioc_inresvs_t inresvs; 25484 int rval = 0; 25485 25486 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25487 return (ENXIO); 25488 } 25489 25490 #ifdef _MULTI_DATAMODEL 25491 25492 switch (ddi_model_convert_from(flag & FMODELS)) { 25493 case DDI_MODEL_ILP32: { 25494 struct mhioc_inresvs32 inresvs32; 25495 25496 if (ddi_copyin(arg, &inresvs32, 25497 sizeof (struct mhioc_inresvs32), flag) != 0) { 25498 return (EFAULT); 25499 } 25500 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25501 if ((rval = sd_persistent_reservation_in_read_resv(un, 25502 &inresvs, flag)) != 0) { 25503 return (rval); 25504 } 25505 inresvs32.generation = inresvs.generation; 25506 if (ddi_copyout(&inresvs32, arg, 25507 sizeof (struct mhioc_inresvs32), flag) != 0) { 25508 return (EFAULT); 25509 } 25510 break; 25511 } 25512 case DDI_MODEL_NONE: 25513 if (ddi_copyin(arg, &inresvs, 25514 sizeof (mhioc_inresvs_t), flag) != 0) { 25515 return (EFAULT); 25516 } 25517 if ((rval = sd_persistent_reservation_in_read_resv(un, 25518 &inresvs, flag)) != 0) { 25519 return (rval); 25520 } 25521 if (ddi_copyout(&inresvs, arg, 25522 sizeof (mhioc_inresvs_t), flag) != 0) { 25523 return (EFAULT); 25524 } 25525 break; 25526 } 25527 25528 #else /* ! _MULTI_DATAMODEL */ 25529 25530 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25531 return (EFAULT); 25532 } 25533 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25534 if (rval != 0) { 25535 return (rval); 25536 } 25537 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25538 return (EFAULT); 25539 } 25540 25541 #endif /* ! _MULTI_DATAMODEL */ 25542 25543 return (rval); 25544 } 25545 25546 25547 /* 25548 * The following routines support the clustering functionality described below 25549 * and implement lost reservation reclaim functionality. 25550 * 25551 * Clustering 25552 * ---------- 25553 * The clustering code uses two different, independent forms of SCSI 25554 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25555 * Persistent Group Reservations. For any particular disk, it will use either 25556 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25557 * 25558 * SCSI-2 25559 * The cluster software takes ownership of a multi-hosted disk by issuing the 25560 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25561 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25562 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25563 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25564 * meaning of failfast is that if the driver (on this host) ever encounters the 25565 * scsi error return code RESERVATION_CONFLICT from the device, it should 25566 * immediately panic the host. The motivation for this ioctl is that if this 25567 * host does encounter reservation conflict, the underlying cause is that some 25568 * other host of the cluster has decided that this host is no longer in the 25569 * cluster and has seized control of the disks for itself. Since this host is no 25570 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25571 * does two things: 25572 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25573 * error to panic the host 25574 * (b) it sets up a periodic timer to test whether this host still has 25575 * "access" (in that no other host has reserved the device): if the 25576 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25577 * purpose of that periodic timer is to handle scenarios where the host is 25578 * otherwise temporarily quiescent, temporarily doing no real i/o. 25579 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25580 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25581 * the device itself. 25582 * 25583 * SCSI-3 PGR 25584 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25585 * facility is supported through the shared multihost disk ioctls 25586 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25587 * MHIOCGRP_PREEMPTANDABORT) 25588 * 25589 * Reservation Reclaim: 25590 * -------------------- 25591 * To support the lost reservation reclaim operations this driver creates a 25592 * single thread to handle reinstating reservations on all devices that have 25593 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25594 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25595 * and the reservation reclaim thread loops through the requests to regain the 25596 * lost reservations. 25597 */ 25598 25599 /* 25600 * Function: sd_check_mhd() 25601 * 25602 * Description: This function sets up and submits a scsi watch request or 25603 * terminates an existing watch request. This routine is used in 25604 * support of reservation reclaim. 25605 * 25606 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25607 * among multiple watches that share the callback function 25608 * interval - the number of microseconds specifying the watch 25609 * interval for issuing TEST UNIT READY commands. If 25610 * set to 0 the watch should be terminated. If the 25611 * interval is set to 0 and if the device is required 25612 * to hold reservation while disabling failfast, the 25613 * watch is restarted with an interval of 25614 * reinstate_resv_delay. 25615 * 25616 * Return Code: 0 - Successful submit/terminate of scsi watch request 25617 * ENXIO - Indicates an invalid device was specified 25618 * EAGAIN - Unable to submit the scsi watch request 25619 */ 25620 25621 static int 25622 sd_check_mhd(dev_t dev, int interval) 25623 { 25624 struct sd_lun *un; 25625 opaque_t token; 25626 25627 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25628 return (ENXIO); 25629 } 25630 25631 /* is this a watch termination request? */ 25632 if (interval == 0) { 25633 mutex_enter(SD_MUTEX(un)); 25634 /* if there is an existing watch task then terminate it */ 25635 if (un->un_mhd_token) { 25636 token = un->un_mhd_token; 25637 un->un_mhd_token = NULL; 25638 mutex_exit(SD_MUTEX(un)); 25639 (void) scsi_watch_request_terminate(token, 25640 SCSI_WATCH_TERMINATE_WAIT); 25641 mutex_enter(SD_MUTEX(un)); 25642 } else { 25643 mutex_exit(SD_MUTEX(un)); 25644 /* 25645 * Note: If we return here we don't check for the 25646 * failfast case. This is the original legacy 25647 * implementation but perhaps we should be checking 25648 * the failfast case. 25649 */ 25650 return (0); 25651 } 25652 /* 25653 * If the device is required to hold reservation while 25654 * disabling failfast, we need to restart the scsi_watch 25655 * routine with an interval of reinstate_resv_delay. 25656 */ 25657 if (un->un_resvd_status & SD_RESERVE) { 25658 interval = sd_reinstate_resv_delay/1000; 25659 } else { 25660 /* no failfast so bail */ 25661 mutex_exit(SD_MUTEX(un)); 25662 return (0); 25663 } 25664 mutex_exit(SD_MUTEX(un)); 25665 } 25666 25667 /* 25668 * adjust minimum time interval to 1 second, 25669 * and convert from msecs to usecs 25670 */ 25671 if (interval > 0 && interval < 1000) { 25672 interval = 1000; 25673 } 25674 interval *= 1000; 25675 25676 /* 25677 * submit the request to the scsi_watch service 25678 */ 25679 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25680 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25681 if (token == NULL) { 25682 return (EAGAIN); 25683 } 25684 25685 /* 25686 * save token for termination later on 25687 */ 25688 mutex_enter(SD_MUTEX(un)); 25689 un->un_mhd_token = token; 25690 mutex_exit(SD_MUTEX(un)); 25691 return (0); 25692 } 25693 25694 25695 /* 25696 * Function: sd_mhd_watch_cb() 25697 * 25698 * Description: This function is the call back function used by the scsi watch 25699 * facility. The scsi watch facility sends the "Test Unit Ready" 25700 * and processes the status. If applicable (i.e. a "Unit Attention" 25701 * status and automatic "Request Sense" not used) the scsi watch 25702 * facility will send a "Request Sense" and retrieve the sense data 25703 * to be passed to this callback function. In either case the 25704 * automatic "Request Sense" or the facility submitting one, this 25705 * callback is passed the status and sense data. 25706 * 25707 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25708 * among multiple watches that share this callback function 25709 * resultp - scsi watch facility result packet containing scsi 25710 * packet, status byte and sense data 25711 * 25712 * Return Code: 0 - continue the watch task 25713 * non-zero - terminate the watch task 25714 */ 25715 25716 static int 25717 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25718 { 25719 struct sd_lun *un; 25720 struct scsi_status *statusp; 25721 uint8_t *sensep; 25722 struct scsi_pkt *pkt; 25723 uchar_t actual_sense_length; 25724 dev_t dev = (dev_t)arg; 25725 25726 ASSERT(resultp != NULL); 25727 statusp = resultp->statusp; 25728 sensep = (uint8_t *)resultp->sensep; 25729 pkt = resultp->pkt; 25730 actual_sense_length = resultp->actual_sense_length; 25731 25732 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25733 return (ENXIO); 25734 } 25735 25736 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25737 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25738 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25739 25740 /* Begin processing of the status and/or sense data */ 25741 if (pkt->pkt_reason != CMD_CMPLT) { 25742 /* Handle the incomplete packet */ 25743 sd_mhd_watch_incomplete(un, pkt); 25744 return (0); 25745 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25746 if (*((unsigned char *)statusp) 25747 == STATUS_RESERVATION_CONFLICT) { 25748 /* 25749 * Handle a reservation conflict by panicking if 25750 * configured for failfast or by logging the conflict 25751 * and updating the reservation status 25752 */ 25753 mutex_enter(SD_MUTEX(un)); 25754 if ((un->un_resvd_status & SD_FAILFAST) && 25755 (sd_failfast_enable)) { 25756 sd_panic_for_res_conflict(un); 25757 /*NOTREACHED*/ 25758 } 25759 SD_INFO(SD_LOG_IOCTL_MHD, un, 25760 "sd_mhd_watch_cb: Reservation Conflict\n"); 25761 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25762 mutex_exit(SD_MUTEX(un)); 25763 } 25764 } 25765 25766 if (sensep != NULL) { 25767 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25768 mutex_enter(SD_MUTEX(un)); 25769 if ((scsi_sense_asc(sensep) == 25770 SD_SCSI_RESET_SENSE_CODE) && 25771 (un->un_resvd_status & SD_RESERVE)) { 25772 /* 25773 * The additional sense code indicates a power 25774 * on or bus device reset has occurred; update 25775 * the reservation status. 25776 */ 25777 un->un_resvd_status |= 25778 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25779 SD_INFO(SD_LOG_IOCTL_MHD, un, 25780 "sd_mhd_watch_cb: Lost Reservation\n"); 25781 } 25782 } else { 25783 return (0); 25784 } 25785 } else { 25786 mutex_enter(SD_MUTEX(un)); 25787 } 25788 25789 if ((un->un_resvd_status & SD_RESERVE) && 25790 (un->un_resvd_status & SD_LOST_RESERVE)) { 25791 if (un->un_resvd_status & SD_WANT_RESERVE) { 25792 /* 25793 * A reset occurred in between the last probe and this 25794 * one so if a timeout is pending cancel it. 25795 */ 25796 if (un->un_resvd_timeid) { 25797 timeout_id_t temp_id = un->un_resvd_timeid; 25798 un->un_resvd_timeid = NULL; 25799 mutex_exit(SD_MUTEX(un)); 25800 (void) untimeout(temp_id); 25801 mutex_enter(SD_MUTEX(un)); 25802 } 25803 un->un_resvd_status &= ~SD_WANT_RESERVE; 25804 } 25805 if (un->un_resvd_timeid == 0) { 25806 /* Schedule a timeout to handle the lost reservation */ 25807 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25808 (void *)dev, 25809 drv_usectohz(sd_reinstate_resv_delay)); 25810 } 25811 } 25812 mutex_exit(SD_MUTEX(un)); 25813 return (0); 25814 } 25815 25816 25817 /* 25818 * Function: sd_mhd_watch_incomplete() 25819 * 25820 * Description: This function is used to find out why a scsi pkt sent by the 25821 * scsi watch facility was not completed. Under some scenarios this 25822 * routine will return. Otherwise it will send a bus reset to see 25823 * if the drive is still online. 25824 * 25825 * Arguments: un - driver soft state (unit) structure 25826 * pkt - incomplete scsi pkt 25827 */ 25828 25829 static void 25830 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25831 { 25832 int be_chatty; 25833 int perr; 25834 25835 ASSERT(pkt != NULL); 25836 ASSERT(un != NULL); 25837 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25838 perr = (pkt->pkt_statistics & STAT_PERR); 25839 25840 mutex_enter(SD_MUTEX(un)); 25841 if (un->un_state == SD_STATE_DUMPING) { 25842 mutex_exit(SD_MUTEX(un)); 25843 return; 25844 } 25845 25846 switch (pkt->pkt_reason) { 25847 case CMD_UNX_BUS_FREE: 25848 /* 25849 * If we had a parity error that caused the target to drop BSY*, 25850 * don't be chatty about it. 25851 */ 25852 if (perr && be_chatty) { 25853 be_chatty = 0; 25854 } 25855 break; 25856 case CMD_TAG_REJECT: 25857 /* 25858 * The SCSI-2 spec states that a tag reject will be sent by the 25859 * target if tagged queuing is not supported. A tag reject may 25860 * also be sent during certain initialization periods or to 25861 * control internal resources. For the latter case the target 25862 * may also return Queue Full. 25863 * 25864 * If this driver receives a tag reject from a target that is 25865 * going through an init period or controlling internal 25866 * resources tagged queuing will be disabled. This is a less 25867 * than optimal behavior but the driver is unable to determine 25868 * the target state and assumes tagged queueing is not supported 25869 */ 25870 pkt->pkt_flags = 0; 25871 un->un_tagflags = 0; 25872 25873 if (un->un_f_opt_queueing == TRUE) { 25874 un->un_throttle = min(un->un_throttle, 3); 25875 } else { 25876 un->un_throttle = 1; 25877 } 25878 mutex_exit(SD_MUTEX(un)); 25879 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25880 mutex_enter(SD_MUTEX(un)); 25881 break; 25882 case CMD_INCOMPLETE: 25883 /* 25884 * The transport stopped with an abnormal state, fallthrough and 25885 * reset the target and/or bus unless selection did not complete 25886 * (indicated by STATE_GOT_BUS) in which case we don't want to 25887 * go through a target/bus reset 25888 */ 25889 if (pkt->pkt_state == STATE_GOT_BUS) { 25890 break; 25891 } 25892 /*FALLTHROUGH*/ 25893 25894 case CMD_TIMEOUT: 25895 default: 25896 /* 25897 * The lun may still be running the command, so a lun reset 25898 * should be attempted. If the lun reset fails or cannot be 25899 * issued, than try a target reset. Lastly try a bus reset. 25900 */ 25901 if ((pkt->pkt_statistics & 25902 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25903 int reset_retval = 0; 25904 mutex_exit(SD_MUTEX(un)); 25905 if (un->un_f_allow_bus_device_reset == TRUE) { 25906 if (un->un_f_lun_reset_enabled == TRUE) { 25907 reset_retval = 25908 scsi_reset(SD_ADDRESS(un), 25909 RESET_LUN); 25910 } 25911 if (reset_retval == 0) { 25912 reset_retval = 25913 scsi_reset(SD_ADDRESS(un), 25914 RESET_TARGET); 25915 } 25916 } 25917 if (reset_retval == 0) { 25918 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25919 } 25920 mutex_enter(SD_MUTEX(un)); 25921 } 25922 break; 25923 } 25924 25925 /* A device/bus reset has occurred; update the reservation status. */ 25926 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25927 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25928 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25929 un->un_resvd_status |= 25930 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25931 SD_INFO(SD_LOG_IOCTL_MHD, un, 25932 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25933 } 25934 } 25935 25936 /* 25937 * The disk has been turned off; Update the device state. 25938 * 25939 * Note: Should we be offlining the disk here? 25940 */ 25941 if (pkt->pkt_state == STATE_GOT_BUS) { 25942 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25943 "Disk not responding to selection\n"); 25944 if (un->un_state != SD_STATE_OFFLINE) { 25945 New_state(un, SD_STATE_OFFLINE); 25946 } 25947 } else if (be_chatty) { 25948 /* 25949 * suppress messages if they are all the same pkt reason; 25950 * with TQ, many (up to 256) are returned with the same 25951 * pkt_reason 25952 */ 25953 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25954 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25955 "sd_mhd_watch_incomplete: " 25956 "SCSI transport failed: reason '%s'\n", 25957 scsi_rname(pkt->pkt_reason)); 25958 } 25959 } 25960 un->un_last_pkt_reason = pkt->pkt_reason; 25961 mutex_exit(SD_MUTEX(un)); 25962 } 25963 25964 25965 /* 25966 * Function: sd_sname() 25967 * 25968 * Description: This is a simple little routine to return a string containing 25969 * a printable description of command status byte for use in 25970 * logging. 25971 * 25972 * Arguments: status - pointer to a status byte 25973 * 25974 * Return Code: char * - string containing status description. 25975 */ 25976 25977 static char * 25978 sd_sname(uchar_t status) 25979 { 25980 switch (status & STATUS_MASK) { 25981 case STATUS_GOOD: 25982 return ("good status"); 25983 case STATUS_CHECK: 25984 return ("check condition"); 25985 case STATUS_MET: 25986 return ("condition met"); 25987 case STATUS_BUSY: 25988 return ("busy"); 25989 case STATUS_INTERMEDIATE: 25990 return ("intermediate"); 25991 case STATUS_INTERMEDIATE_MET: 25992 return ("intermediate - condition met"); 25993 case STATUS_RESERVATION_CONFLICT: 25994 return ("reservation_conflict"); 25995 case STATUS_TERMINATED: 25996 return ("command terminated"); 25997 case STATUS_QFULL: 25998 return ("queue full"); 25999 default: 26000 return ("<unknown status>"); 26001 } 26002 } 26003 26004 26005 /* 26006 * Function: sd_mhd_resvd_recover() 26007 * 26008 * Description: This function adds a reservation entry to the 26009 * sd_resv_reclaim_request list and signals the reservation 26010 * reclaim thread that there is work pending. If the reservation 26011 * reclaim thread has not been previously created this function 26012 * will kick it off. 26013 * 26014 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26015 * among multiple watches that share this callback function 26016 * 26017 * Context: This routine is called by timeout() and is run in interrupt 26018 * context. It must not sleep or call other functions which may 26019 * sleep. 26020 */ 26021 26022 static void 26023 sd_mhd_resvd_recover(void *arg) 26024 { 26025 dev_t dev = (dev_t)arg; 26026 struct sd_lun *un; 26027 struct sd_thr_request *sd_treq = NULL; 26028 struct sd_thr_request *sd_cur = NULL; 26029 struct sd_thr_request *sd_prev = NULL; 26030 int already_there = 0; 26031 26032 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26033 return; 26034 } 26035 26036 mutex_enter(SD_MUTEX(un)); 26037 un->un_resvd_timeid = NULL; 26038 if (un->un_resvd_status & SD_WANT_RESERVE) { 26039 /* 26040 * There was a reset so don't issue the reserve, allow the 26041 * sd_mhd_watch_cb callback function to notice this and 26042 * reschedule the timeout for reservation. 26043 */ 26044 mutex_exit(SD_MUTEX(un)); 26045 return; 26046 } 26047 mutex_exit(SD_MUTEX(un)); 26048 26049 /* 26050 * Add this device to the sd_resv_reclaim_request list and the 26051 * sd_resv_reclaim_thread should take care of the rest. 26052 * 26053 * Note: We can't sleep in this context so if the memory allocation 26054 * fails allow the sd_mhd_watch_cb callback function to notice this and 26055 * reschedule the timeout for reservation. (4378460) 26056 */ 26057 sd_treq = (struct sd_thr_request *) 26058 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 26059 if (sd_treq == NULL) { 26060 return; 26061 } 26062 26063 sd_treq->sd_thr_req_next = NULL; 26064 sd_treq->dev = dev; 26065 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26066 if (sd_tr.srq_thr_req_head == NULL) { 26067 sd_tr.srq_thr_req_head = sd_treq; 26068 } else { 26069 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 26070 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 26071 if (sd_cur->dev == dev) { 26072 /* 26073 * already in Queue so don't log 26074 * another request for the device 26075 */ 26076 already_there = 1; 26077 break; 26078 } 26079 sd_prev = sd_cur; 26080 } 26081 if (!already_there) { 26082 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 26083 "logging request for %lx\n", dev); 26084 sd_prev->sd_thr_req_next = sd_treq; 26085 } else { 26086 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 26087 } 26088 } 26089 26090 /* 26091 * Create a kernel thread to do the reservation reclaim and free up this 26092 * thread. We cannot block this thread while we go away to do the 26093 * reservation reclaim 26094 */ 26095 if (sd_tr.srq_resv_reclaim_thread == NULL) 26096 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 26097 sd_resv_reclaim_thread, NULL, 26098 0, &p0, TS_RUN, v.v_maxsyspri - 2); 26099 26100 /* Tell the reservation reclaim thread that it has work to do */ 26101 cv_signal(&sd_tr.srq_resv_reclaim_cv); 26102 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26103 } 26104 26105 /* 26106 * Function: sd_resv_reclaim_thread() 26107 * 26108 * Description: This function implements the reservation reclaim operations 26109 * 26110 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26111 * among multiple watches that share this callback function 26112 */ 26113 26114 static void 26115 sd_resv_reclaim_thread() 26116 { 26117 struct sd_lun *un; 26118 struct sd_thr_request *sd_mhreq; 26119 26120 /* Wait for work */ 26121 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26122 if (sd_tr.srq_thr_req_head == NULL) { 26123 cv_wait(&sd_tr.srq_resv_reclaim_cv, 26124 &sd_tr.srq_resv_reclaim_mutex); 26125 } 26126 26127 /* Loop while we have work */ 26128 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 26129 un = ddi_get_soft_state(sd_state, 26130 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 26131 if (un == NULL) { 26132 /* 26133 * softstate structure is NULL so just 26134 * dequeue the request and continue 26135 */ 26136 sd_tr.srq_thr_req_head = 26137 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26138 kmem_free(sd_tr.srq_thr_cur_req, 26139 sizeof (struct sd_thr_request)); 26140 continue; 26141 } 26142 26143 /* dequeue the request */ 26144 sd_mhreq = sd_tr.srq_thr_cur_req; 26145 sd_tr.srq_thr_req_head = 26146 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26147 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26148 26149 /* 26150 * Reclaim reservation only if SD_RESERVE is still set. There 26151 * may have been a call to MHIOCRELEASE before we got here. 26152 */ 26153 mutex_enter(SD_MUTEX(un)); 26154 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26155 /* 26156 * Note: The SD_LOST_RESERVE flag is cleared before 26157 * reclaiming the reservation. If this is done after the 26158 * call to sd_reserve_release a reservation loss in the 26159 * window between pkt completion of reserve cmd and 26160 * mutex_enter below may not be recognized 26161 */ 26162 un->un_resvd_status &= ~SD_LOST_RESERVE; 26163 mutex_exit(SD_MUTEX(un)); 26164 26165 if (sd_reserve_release(sd_mhreq->dev, 26166 SD_RESERVE) == 0) { 26167 mutex_enter(SD_MUTEX(un)); 26168 un->un_resvd_status |= SD_RESERVE; 26169 mutex_exit(SD_MUTEX(un)); 26170 SD_INFO(SD_LOG_IOCTL_MHD, un, 26171 "sd_resv_reclaim_thread: " 26172 "Reservation Recovered\n"); 26173 } else { 26174 mutex_enter(SD_MUTEX(un)); 26175 un->un_resvd_status |= SD_LOST_RESERVE; 26176 mutex_exit(SD_MUTEX(un)); 26177 SD_INFO(SD_LOG_IOCTL_MHD, un, 26178 "sd_resv_reclaim_thread: Failed " 26179 "Reservation Recovery\n"); 26180 } 26181 } else { 26182 mutex_exit(SD_MUTEX(un)); 26183 } 26184 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26185 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 26186 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26187 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 26188 /* 26189 * wakeup the destroy thread if anyone is waiting on 26190 * us to complete. 26191 */ 26192 cv_signal(&sd_tr.srq_inprocess_cv); 26193 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26194 "sd_resv_reclaim_thread: cv_signalling current request \n"); 26195 } 26196 26197 /* 26198 * cleanup the sd_tr structure now that this thread will not exist 26199 */ 26200 ASSERT(sd_tr.srq_thr_req_head == NULL); 26201 ASSERT(sd_tr.srq_thr_cur_req == NULL); 26202 sd_tr.srq_resv_reclaim_thread = NULL; 26203 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26204 thread_exit(); 26205 } 26206 26207 26208 /* 26209 * Function: sd_rmv_resv_reclaim_req() 26210 * 26211 * Description: This function removes any pending reservation reclaim requests 26212 * for the specified device. 26213 * 26214 * Arguments: dev - the device 'dev_t' 26215 */ 26216 26217 static void 26218 sd_rmv_resv_reclaim_req(dev_t dev) 26219 { 26220 struct sd_thr_request *sd_mhreq; 26221 struct sd_thr_request *sd_prev; 26222 26223 /* Remove a reservation reclaim request from the list */ 26224 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26225 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 26226 /* 26227 * We are attempting to reinstate reservation for 26228 * this device. We wait for sd_reserve_release() 26229 * to return before we return. 26230 */ 26231 cv_wait(&sd_tr.srq_inprocess_cv, 26232 &sd_tr.srq_resv_reclaim_mutex); 26233 } else { 26234 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 26235 if (sd_mhreq && sd_mhreq->dev == dev) { 26236 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 26237 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26238 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26239 return; 26240 } 26241 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 26242 if (sd_mhreq && sd_mhreq->dev == dev) { 26243 break; 26244 } 26245 sd_prev = sd_mhreq; 26246 } 26247 if (sd_mhreq != NULL) { 26248 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 26249 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26250 } 26251 } 26252 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26253 } 26254 26255 26256 /* 26257 * Function: sd_mhd_reset_notify_cb() 26258 * 26259 * Description: This is a call back function for scsi_reset_notify. This 26260 * function updates the softstate reserved status and logs the 26261 * reset. The driver scsi watch facility callback function 26262 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 26263 * will reclaim the reservation. 26264 * 26265 * Arguments: arg - driver soft state (unit) structure 26266 */ 26267 26268 static void 26269 sd_mhd_reset_notify_cb(caddr_t arg) 26270 { 26271 struct sd_lun *un = (struct sd_lun *)arg; 26272 26273 mutex_enter(SD_MUTEX(un)); 26274 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26275 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 26276 SD_INFO(SD_LOG_IOCTL_MHD, un, 26277 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 26278 } 26279 mutex_exit(SD_MUTEX(un)); 26280 } 26281 26282 26283 /* 26284 * Function: sd_take_ownership() 26285 * 26286 * Description: This routine implements an algorithm to achieve a stable 26287 * reservation on disks which don't implement priority reserve, 26288 * and makes sure that other host lose re-reservation attempts. 26289 * This algorithm contains of a loop that keeps issuing the RESERVE 26290 * for some period of time (min_ownership_delay, default 6 seconds) 26291 * During that loop, it looks to see if there has been a bus device 26292 * reset or bus reset (both of which cause an existing reservation 26293 * to be lost). If the reservation is lost issue RESERVE until a 26294 * period of min_ownership_delay with no resets has gone by, or 26295 * until max_ownership_delay has expired. This loop ensures that 26296 * the host really did manage to reserve the device, in spite of 26297 * resets. The looping for min_ownership_delay (default six 26298 * seconds) is important to early generation clustering products, 26299 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 26300 * MHIOCENFAILFAST periodic timer of two seconds. By having 26301 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 26302 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 26303 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 26304 * have already noticed, via the MHIOCENFAILFAST polling, that it 26305 * no longer "owns" the disk and will have panicked itself. Thus, 26306 * the host issuing the MHIOCTKOWN is assured (with timing 26307 * dependencies) that by the time it actually starts to use the 26308 * disk for real work, the old owner is no longer accessing it. 26309 * 26310 * min_ownership_delay is the minimum amount of time for which the 26311 * disk must be reserved continuously devoid of resets before the 26312 * MHIOCTKOWN ioctl will return success. 26313 * 26314 * max_ownership_delay indicates the amount of time by which the 26315 * take ownership should succeed or timeout with an error. 26316 * 26317 * Arguments: dev - the device 'dev_t' 26318 * *p - struct containing timing info. 26319 * 26320 * Return Code: 0 for success or error code 26321 */ 26322 26323 static int 26324 sd_take_ownership(dev_t dev, struct mhioctkown *p) 26325 { 26326 struct sd_lun *un; 26327 int rval; 26328 int err; 26329 int reservation_count = 0; 26330 int min_ownership_delay = 6000000; /* in usec */ 26331 int max_ownership_delay = 30000000; /* in usec */ 26332 clock_t start_time; /* starting time of this algorithm */ 26333 clock_t end_time; /* time limit for giving up */ 26334 clock_t ownership_time; /* time limit for stable ownership */ 26335 clock_t current_time; 26336 clock_t previous_current_time; 26337 26338 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26339 return (ENXIO); 26340 } 26341 26342 /* 26343 * Attempt a device reservation. A priority reservation is requested. 26344 */ 26345 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 26346 != SD_SUCCESS) { 26347 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26348 "sd_take_ownership: return(1)=%d\n", rval); 26349 return (rval); 26350 } 26351 26352 /* Update the softstate reserved status to indicate the reservation */ 26353 mutex_enter(SD_MUTEX(un)); 26354 un->un_resvd_status |= SD_RESERVE; 26355 un->un_resvd_status &= 26356 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 26357 mutex_exit(SD_MUTEX(un)); 26358 26359 if (p != NULL) { 26360 if (p->min_ownership_delay != 0) { 26361 min_ownership_delay = p->min_ownership_delay * 1000; 26362 } 26363 if (p->max_ownership_delay != 0) { 26364 max_ownership_delay = p->max_ownership_delay * 1000; 26365 } 26366 } 26367 SD_INFO(SD_LOG_IOCTL_MHD, un, 26368 "sd_take_ownership: min, max delays: %d, %d\n", 26369 min_ownership_delay, max_ownership_delay); 26370 26371 start_time = ddi_get_lbolt(); 26372 current_time = start_time; 26373 ownership_time = current_time + drv_usectohz(min_ownership_delay); 26374 end_time = start_time + drv_usectohz(max_ownership_delay); 26375 26376 while (current_time - end_time < 0) { 26377 delay(drv_usectohz(500000)); 26378 26379 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 26380 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 26381 mutex_enter(SD_MUTEX(un)); 26382 rval = (un->un_resvd_status & 26383 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 26384 mutex_exit(SD_MUTEX(un)); 26385 break; 26386 } 26387 } 26388 previous_current_time = current_time; 26389 current_time = ddi_get_lbolt(); 26390 mutex_enter(SD_MUTEX(un)); 26391 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 26392 ownership_time = ddi_get_lbolt() + 26393 drv_usectohz(min_ownership_delay); 26394 reservation_count = 0; 26395 } else { 26396 reservation_count++; 26397 } 26398 un->un_resvd_status |= SD_RESERVE; 26399 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 26400 mutex_exit(SD_MUTEX(un)); 26401 26402 SD_INFO(SD_LOG_IOCTL_MHD, un, 26403 "sd_take_ownership: ticks for loop iteration=%ld, " 26404 "reservation=%s\n", (current_time - previous_current_time), 26405 reservation_count ? "ok" : "reclaimed"); 26406 26407 if (current_time - ownership_time >= 0 && 26408 reservation_count >= 4) { 26409 rval = 0; /* Achieved a stable ownership */ 26410 break; 26411 } 26412 if (current_time - end_time >= 0) { 26413 rval = EACCES; /* No ownership in max possible time */ 26414 break; 26415 } 26416 } 26417 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26418 "sd_take_ownership: return(2)=%d\n", rval); 26419 return (rval); 26420 } 26421 26422 26423 /* 26424 * Function: sd_reserve_release() 26425 * 26426 * Description: This function builds and sends scsi RESERVE, RELEASE, and 26427 * PRIORITY RESERVE commands based on a user specified command type 26428 * 26429 * Arguments: dev - the device 'dev_t' 26430 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 26431 * SD_RESERVE, SD_RELEASE 26432 * 26433 * Return Code: 0 or Error Code 26434 */ 26435 26436 static int 26437 sd_reserve_release(dev_t dev, int cmd) 26438 { 26439 struct uscsi_cmd *com = NULL; 26440 struct sd_lun *un = NULL; 26441 char cdb[CDB_GROUP0]; 26442 int rval; 26443 26444 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 26445 (cmd == SD_PRIORITY_RESERVE)); 26446 26447 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26448 return (ENXIO); 26449 } 26450 26451 /* instantiate and initialize the command and cdb */ 26452 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26453 bzero(cdb, CDB_GROUP0); 26454 com->uscsi_flags = USCSI_SILENT; 26455 com->uscsi_timeout = un->un_reserve_release_time; 26456 com->uscsi_cdblen = CDB_GROUP0; 26457 com->uscsi_cdb = cdb; 26458 if (cmd == SD_RELEASE) { 26459 cdb[0] = SCMD_RELEASE; 26460 } else { 26461 cdb[0] = SCMD_RESERVE; 26462 } 26463 26464 /* Send the command. */ 26465 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26466 UIO_SYSSPACE, SD_PATH_STANDARD); 26467 26468 /* 26469 * "break" a reservation that is held by another host, by issuing a 26470 * reset if priority reserve is desired, and we could not get the 26471 * device. 26472 */ 26473 if ((cmd == SD_PRIORITY_RESERVE) && 26474 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26475 /* 26476 * First try to reset the LUN. If we cannot, then try a target 26477 * reset, followed by a bus reset if the target reset fails. 26478 */ 26479 int reset_retval = 0; 26480 if (un->un_f_lun_reset_enabled == TRUE) { 26481 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26482 } 26483 if (reset_retval == 0) { 26484 /* The LUN reset either failed or was not issued */ 26485 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26486 } 26487 if ((reset_retval == 0) && 26488 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26489 rval = EIO; 26490 kmem_free(com, sizeof (*com)); 26491 return (rval); 26492 } 26493 26494 bzero(com, sizeof (struct uscsi_cmd)); 26495 com->uscsi_flags = USCSI_SILENT; 26496 com->uscsi_cdb = cdb; 26497 com->uscsi_cdblen = CDB_GROUP0; 26498 com->uscsi_timeout = 5; 26499 26500 /* 26501 * Reissue the last reserve command, this time without request 26502 * sense. Assume that it is just a regular reserve command. 26503 */ 26504 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26505 UIO_SYSSPACE, SD_PATH_STANDARD); 26506 } 26507 26508 /* Return an error if still getting a reservation conflict. */ 26509 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26510 rval = EACCES; 26511 } 26512 26513 kmem_free(com, sizeof (*com)); 26514 return (rval); 26515 } 26516 26517 26518 #define SD_NDUMP_RETRIES 12 26519 /* 26520 * System Crash Dump routine 26521 */ 26522 26523 static int 26524 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26525 { 26526 int instance; 26527 int partition; 26528 int i; 26529 int err; 26530 struct sd_lun *un; 26531 struct dk_map *lp; 26532 struct scsi_pkt *wr_pktp; 26533 struct buf *wr_bp; 26534 struct buf wr_buf; 26535 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26536 daddr_t tgt_blkno; /* rmw - blkno for target */ 26537 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26538 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26539 size_t io_start_offset; 26540 int doing_rmw = FALSE; 26541 int rval; 26542 #if defined(__i386) || defined(__amd64) 26543 ssize_t dma_resid; 26544 daddr_t oblkno; 26545 #endif 26546 26547 instance = SDUNIT(dev); 26548 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26549 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26550 return (ENXIO); 26551 } 26552 26553 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26554 26555 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26556 26557 partition = SDPART(dev); 26558 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26559 26560 /* Validate blocks to dump at against partition size. */ 26561 lp = &un->un_map[partition]; 26562 if ((blkno + nblk) > lp->dkl_nblk) { 26563 SD_TRACE(SD_LOG_DUMP, un, 26564 "sddump: dump range larger than partition: " 26565 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26566 blkno, nblk, lp->dkl_nblk); 26567 return (EINVAL); 26568 } 26569 26570 mutex_enter(&un->un_pm_mutex); 26571 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26572 struct scsi_pkt *start_pktp; 26573 26574 mutex_exit(&un->un_pm_mutex); 26575 26576 /* 26577 * use pm framework to power on HBA 1st 26578 */ 26579 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26580 26581 /* 26582 * Dump no long uses sdpower to power on a device, it's 26583 * in-line here so it can be done in polled mode. 26584 */ 26585 26586 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26587 26588 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26589 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26590 26591 if (start_pktp == NULL) { 26592 /* We were not given a SCSI packet, fail. */ 26593 return (EIO); 26594 } 26595 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26596 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26597 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26598 start_pktp->pkt_flags = FLAG_NOINTR; 26599 26600 mutex_enter(SD_MUTEX(un)); 26601 SD_FILL_SCSI1_LUN(un, start_pktp); 26602 mutex_exit(SD_MUTEX(un)); 26603 /* 26604 * Scsi_poll returns 0 (success) if the command completes and 26605 * the status block is STATUS_GOOD. 26606 */ 26607 if (sd_scsi_poll(un, start_pktp) != 0) { 26608 scsi_destroy_pkt(start_pktp); 26609 return (EIO); 26610 } 26611 scsi_destroy_pkt(start_pktp); 26612 (void) sd_ddi_pm_resume(un); 26613 } else { 26614 mutex_exit(&un->un_pm_mutex); 26615 } 26616 26617 mutex_enter(SD_MUTEX(un)); 26618 un->un_throttle = 0; 26619 26620 /* 26621 * The first time through, reset the specific target device. 26622 * However, when cpr calls sddump we know that sd is in a 26623 * a good state so no bus reset is required. 26624 * Clear sense data via Request Sense cmd. 26625 * In sddump we don't care about allow_bus_device_reset anymore 26626 */ 26627 26628 if ((un->un_state != SD_STATE_SUSPENDED) && 26629 (un->un_state != SD_STATE_DUMPING)) { 26630 26631 New_state(un, SD_STATE_DUMPING); 26632 26633 if (un->un_f_is_fibre == FALSE) { 26634 mutex_exit(SD_MUTEX(un)); 26635 /* 26636 * Attempt a bus reset for parallel scsi. 26637 * 26638 * Note: A bus reset is required because on some host 26639 * systems (i.e. E420R) a bus device reset is 26640 * insufficient to reset the state of the target. 26641 * 26642 * Note: Don't issue the reset for fibre-channel, 26643 * because this tends to hang the bus (loop) for 26644 * too long while everyone is logging out and in 26645 * and the deadman timer for dumping will fire 26646 * before the dump is complete. 26647 */ 26648 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26649 mutex_enter(SD_MUTEX(un)); 26650 Restore_state(un); 26651 mutex_exit(SD_MUTEX(un)); 26652 return (EIO); 26653 } 26654 26655 /* Delay to give the device some recovery time. */ 26656 drv_usecwait(10000); 26657 26658 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26659 SD_INFO(SD_LOG_DUMP, un, 26660 "sddump: sd_send_polled_RQS failed\n"); 26661 } 26662 mutex_enter(SD_MUTEX(un)); 26663 } 26664 } 26665 26666 /* 26667 * Convert the partition-relative block number to a 26668 * disk physical block number. 26669 */ 26670 blkno += un->un_offset[partition]; 26671 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26672 26673 26674 /* 26675 * Check if the device has a non-512 block size. 26676 */ 26677 wr_bp = NULL; 26678 if (NOT_DEVBSIZE(un)) { 26679 tgt_byte_offset = blkno * un->un_sys_blocksize; 26680 tgt_byte_count = nblk * un->un_sys_blocksize; 26681 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26682 (tgt_byte_count % un->un_tgt_blocksize)) { 26683 doing_rmw = TRUE; 26684 /* 26685 * Calculate the block number and number of block 26686 * in terms of the media block size. 26687 */ 26688 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26689 tgt_nblk = 26690 ((tgt_byte_offset + tgt_byte_count + 26691 (un->un_tgt_blocksize - 1)) / 26692 un->un_tgt_blocksize) - tgt_blkno; 26693 26694 /* 26695 * Invoke the routine which is going to do read part 26696 * of read-modify-write. 26697 * Note that this routine returns a pointer to 26698 * a valid bp in wr_bp. 26699 */ 26700 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26701 &wr_bp); 26702 if (err) { 26703 mutex_exit(SD_MUTEX(un)); 26704 return (err); 26705 } 26706 /* 26707 * Offset is being calculated as - 26708 * (original block # * system block size) - 26709 * (new block # * target block size) 26710 */ 26711 io_start_offset = 26712 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26713 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26714 26715 ASSERT((io_start_offset >= 0) && 26716 (io_start_offset < un->un_tgt_blocksize)); 26717 /* 26718 * Do the modify portion of read modify write. 26719 */ 26720 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26721 (size_t)nblk * un->un_sys_blocksize); 26722 } else { 26723 doing_rmw = FALSE; 26724 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26725 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26726 } 26727 26728 /* Convert blkno and nblk to target blocks */ 26729 blkno = tgt_blkno; 26730 nblk = tgt_nblk; 26731 } else { 26732 wr_bp = &wr_buf; 26733 bzero(wr_bp, sizeof (struct buf)); 26734 wr_bp->b_flags = B_BUSY; 26735 wr_bp->b_un.b_addr = addr; 26736 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26737 wr_bp->b_resid = 0; 26738 } 26739 26740 mutex_exit(SD_MUTEX(un)); 26741 26742 /* 26743 * Obtain a SCSI packet for the write command. 26744 * It should be safe to call the allocator here without 26745 * worrying about being locked for DVMA mapping because 26746 * the address we're passed is already a DVMA mapping 26747 * 26748 * We are also not going to worry about semaphore ownership 26749 * in the dump buffer. Dumping is single threaded at present. 26750 */ 26751 26752 wr_pktp = NULL; 26753 26754 #if defined(__i386) || defined(__amd64) 26755 dma_resid = wr_bp->b_bcount; 26756 oblkno = blkno; 26757 while (dma_resid != 0) { 26758 #endif 26759 26760 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26761 wr_bp->b_flags &= ~B_ERROR; 26762 26763 #if defined(__i386) || defined(__amd64) 26764 blkno = oblkno + 26765 ((wr_bp->b_bcount - dma_resid) / 26766 un->un_tgt_blocksize); 26767 nblk = dma_resid / un->un_tgt_blocksize; 26768 26769 if (wr_pktp) { 26770 /* Partial DMA transfers after initial transfer */ 26771 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26772 blkno, nblk); 26773 } else { 26774 /* Initial transfer */ 26775 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26776 un->un_pkt_flags, NULL_FUNC, NULL, 26777 blkno, nblk); 26778 } 26779 #else 26780 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26781 0, NULL_FUNC, NULL, blkno, nblk); 26782 #endif 26783 26784 if (rval == 0) { 26785 /* We were given a SCSI packet, continue. */ 26786 break; 26787 } 26788 26789 if (i == 0) { 26790 if (wr_bp->b_flags & B_ERROR) { 26791 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26792 "no resources for dumping; " 26793 "error code: 0x%x, retrying", 26794 geterror(wr_bp)); 26795 } else { 26796 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26797 "no resources for dumping; retrying"); 26798 } 26799 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26800 if (wr_bp->b_flags & B_ERROR) { 26801 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26802 "no resources for dumping; error code: " 26803 "0x%x, retrying\n", geterror(wr_bp)); 26804 } 26805 } else { 26806 if (wr_bp->b_flags & B_ERROR) { 26807 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26808 "no resources for dumping; " 26809 "error code: 0x%x, retries failed, " 26810 "giving up.\n", geterror(wr_bp)); 26811 } else { 26812 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26813 "no resources for dumping; " 26814 "retries failed, giving up.\n"); 26815 } 26816 mutex_enter(SD_MUTEX(un)); 26817 Restore_state(un); 26818 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26819 mutex_exit(SD_MUTEX(un)); 26820 scsi_free_consistent_buf(wr_bp); 26821 } else { 26822 mutex_exit(SD_MUTEX(un)); 26823 } 26824 return (EIO); 26825 } 26826 drv_usecwait(10000); 26827 } 26828 26829 #if defined(__i386) || defined(__amd64) 26830 /* 26831 * save the resid from PARTIAL_DMA 26832 */ 26833 dma_resid = wr_pktp->pkt_resid; 26834 if (dma_resid != 0) 26835 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26836 wr_pktp->pkt_resid = 0; 26837 #endif 26838 26839 /* SunBug 1222170 */ 26840 wr_pktp->pkt_flags = FLAG_NOINTR; 26841 26842 err = EIO; 26843 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26844 26845 /* 26846 * Scsi_poll returns 0 (success) if the command completes and 26847 * the status block is STATUS_GOOD. We should only check 26848 * errors if this condition is not true. Even then we should 26849 * send our own request sense packet only if we have a check 26850 * condition and auto request sense has not been performed by 26851 * the hba. 26852 */ 26853 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26854 26855 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26856 (wr_pktp->pkt_resid == 0)) { 26857 err = SD_SUCCESS; 26858 break; 26859 } 26860 26861 /* 26862 * Check CMD_DEV_GONE 1st, give up if device is gone. 26863 */ 26864 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26865 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26866 "Device is gone\n"); 26867 break; 26868 } 26869 26870 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26871 SD_INFO(SD_LOG_DUMP, un, 26872 "sddump: write failed with CHECK, try # %d\n", i); 26873 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26874 (void) sd_send_polled_RQS(un); 26875 } 26876 26877 continue; 26878 } 26879 26880 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26881 int reset_retval = 0; 26882 26883 SD_INFO(SD_LOG_DUMP, un, 26884 "sddump: write failed with BUSY, try # %d\n", i); 26885 26886 if (un->un_f_lun_reset_enabled == TRUE) { 26887 reset_retval = scsi_reset(SD_ADDRESS(un), 26888 RESET_LUN); 26889 } 26890 if (reset_retval == 0) { 26891 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26892 } 26893 (void) sd_send_polled_RQS(un); 26894 26895 } else { 26896 SD_INFO(SD_LOG_DUMP, un, 26897 "sddump: write failed with 0x%x, try # %d\n", 26898 SD_GET_PKT_STATUS(wr_pktp), i); 26899 mutex_enter(SD_MUTEX(un)); 26900 sd_reset_target(un, wr_pktp); 26901 mutex_exit(SD_MUTEX(un)); 26902 } 26903 26904 /* 26905 * If we are not getting anywhere with lun/target resets, 26906 * let's reset the bus. 26907 */ 26908 if (i == SD_NDUMP_RETRIES/2) { 26909 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26910 (void) sd_send_polled_RQS(un); 26911 } 26912 26913 } 26914 #if defined(__i386) || defined(__amd64) 26915 } /* dma_resid */ 26916 #endif 26917 26918 scsi_destroy_pkt(wr_pktp); 26919 mutex_enter(SD_MUTEX(un)); 26920 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26921 mutex_exit(SD_MUTEX(un)); 26922 scsi_free_consistent_buf(wr_bp); 26923 } else { 26924 mutex_exit(SD_MUTEX(un)); 26925 } 26926 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26927 return (err); 26928 } 26929 26930 /* 26931 * Function: sd_scsi_poll() 26932 * 26933 * Description: This is a wrapper for the scsi_poll call. 26934 * 26935 * Arguments: sd_lun - The unit structure 26936 * scsi_pkt - The scsi packet being sent to the device. 26937 * 26938 * Return Code: 0 - Command completed successfully with good status 26939 * -1 - Command failed. This could indicate a check condition 26940 * or other status value requiring recovery action. 26941 * 26942 */ 26943 26944 static int 26945 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26946 { 26947 int status; 26948 26949 ASSERT(un != NULL); 26950 ASSERT(!mutex_owned(SD_MUTEX(un))); 26951 ASSERT(pktp != NULL); 26952 26953 status = SD_SUCCESS; 26954 26955 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26956 pktp->pkt_flags |= un->un_tagflags; 26957 pktp->pkt_flags &= ~FLAG_NODISCON; 26958 } 26959 26960 status = sd_ddi_scsi_poll(pktp); 26961 /* 26962 * Scsi_poll returns 0 (success) if the command completes and the 26963 * status block is STATUS_GOOD. We should only check errors if this 26964 * condition is not true. Even then we should send our own request 26965 * sense packet only if we have a check condition and auto 26966 * request sense has not been performed by the hba. 26967 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26968 */ 26969 if ((status != SD_SUCCESS) && 26970 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26971 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26972 (pktp->pkt_reason != CMD_DEV_GONE)) 26973 (void) sd_send_polled_RQS(un); 26974 26975 return (status); 26976 } 26977 26978 /* 26979 * Function: sd_send_polled_RQS() 26980 * 26981 * Description: This sends the request sense command to a device. 26982 * 26983 * Arguments: sd_lun - The unit structure 26984 * 26985 * Return Code: 0 - Command completed successfully with good status 26986 * -1 - Command failed. 26987 * 26988 */ 26989 26990 static int 26991 sd_send_polled_RQS(struct sd_lun *un) 26992 { 26993 int ret_val; 26994 struct scsi_pkt *rqs_pktp; 26995 struct buf *rqs_bp; 26996 26997 ASSERT(un != NULL); 26998 ASSERT(!mutex_owned(SD_MUTEX(un))); 26999 27000 ret_val = SD_SUCCESS; 27001 27002 rqs_pktp = un->un_rqs_pktp; 27003 rqs_bp = un->un_rqs_bp; 27004 27005 mutex_enter(SD_MUTEX(un)); 27006 27007 if (un->un_sense_isbusy) { 27008 ret_val = SD_FAILURE; 27009 mutex_exit(SD_MUTEX(un)); 27010 return (ret_val); 27011 } 27012 27013 /* 27014 * If the request sense buffer (and packet) is not in use, 27015 * let's set the un_sense_isbusy and send our packet 27016 */ 27017 un->un_sense_isbusy = 1; 27018 rqs_pktp->pkt_resid = 0; 27019 rqs_pktp->pkt_reason = 0; 27020 rqs_pktp->pkt_flags |= FLAG_NOINTR; 27021 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 27022 27023 mutex_exit(SD_MUTEX(un)); 27024 27025 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 27026 " 0x%p\n", rqs_bp->b_un.b_addr); 27027 27028 /* 27029 * Can't send this to sd_scsi_poll, we wrap ourselves around the 27030 * axle - it has a call into us! 27031 */ 27032 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 27033 SD_INFO(SD_LOG_COMMON, un, 27034 "sd_send_polled_RQS: RQS failed\n"); 27035 } 27036 27037 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 27038 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 27039 27040 mutex_enter(SD_MUTEX(un)); 27041 un->un_sense_isbusy = 0; 27042 mutex_exit(SD_MUTEX(un)); 27043 27044 return (ret_val); 27045 } 27046 27047 /* 27048 * Defines needed for localized version of the scsi_poll routine. 27049 */ 27050 #define SD_CSEC 10000 /* usecs */ 27051 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 27052 27053 27054 /* 27055 * Function: sd_ddi_scsi_poll() 27056 * 27057 * Description: Localized version of the scsi_poll routine. The purpose is to 27058 * send a scsi_pkt to a device as a polled command. This version 27059 * is to ensure more robust handling of transport errors. 27060 * Specifically this routine cures not ready, coming ready 27061 * transition for power up and reset of sonoma's. This can take 27062 * up to 45 seconds for power-on and 20 seconds for reset of a 27063 * sonoma lun. 27064 * 27065 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 27066 * 27067 * Return Code: 0 - Command completed successfully with good status 27068 * -1 - Command failed. 27069 * 27070 */ 27071 27072 static int 27073 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 27074 { 27075 int busy_count; 27076 int timeout; 27077 int rval = SD_FAILURE; 27078 int savef; 27079 uint8_t *sensep; 27080 long savet; 27081 void (*savec)(); 27082 /* 27083 * The following is defined in machdep.c and is used in determining if 27084 * the scsi transport system will do polled I/O instead of interrupt 27085 * I/O when called from xx_dump(). 27086 */ 27087 extern int do_polled_io; 27088 27089 /* 27090 * save old flags in pkt, to restore at end 27091 */ 27092 savef = pkt->pkt_flags; 27093 savec = pkt->pkt_comp; 27094 savet = pkt->pkt_time; 27095 27096 pkt->pkt_flags |= FLAG_NOINTR; 27097 27098 /* 27099 * XXX there is nothing in the SCSA spec that states that we should not 27100 * do a callback for polled cmds; however, removing this will break sd 27101 * and probably other target drivers 27102 */ 27103 pkt->pkt_comp = NULL; 27104 27105 /* 27106 * we don't like a polled command without timeout. 27107 * 60 seconds seems long enough. 27108 */ 27109 if (pkt->pkt_time == 0) { 27110 pkt->pkt_time = SCSI_POLL_TIMEOUT; 27111 } 27112 27113 /* 27114 * Send polled cmd. 27115 * 27116 * We do some error recovery for various errors. Tran_busy, 27117 * queue full, and non-dispatched commands are retried every 10 msec. 27118 * as they are typically transient failures. Busy status and Not 27119 * Ready are retried every second as this status takes a while to 27120 * change. Unit attention is retried for pkt_time (60) times 27121 * with no delay. 27122 */ 27123 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 27124 27125 for (busy_count = 0; busy_count < timeout; busy_count++) { 27126 int rc; 27127 int poll_delay; 27128 27129 /* 27130 * Initialize pkt status variables. 27131 */ 27132 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 27133 27134 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 27135 if (rc != TRAN_BUSY) { 27136 /* Transport failed - give up. */ 27137 break; 27138 } else { 27139 /* Transport busy - try again. */ 27140 poll_delay = 1 * SD_CSEC; /* 10 msec */ 27141 } 27142 } else { 27143 /* 27144 * Transport accepted - check pkt status. 27145 */ 27146 rc = (*pkt->pkt_scbp) & STATUS_MASK; 27147 if (pkt->pkt_reason == CMD_CMPLT && 27148 rc == STATUS_CHECK && 27149 pkt->pkt_state & STATE_ARQ_DONE) { 27150 struct scsi_arq_status *arqstat = 27151 (struct scsi_arq_status *)(pkt->pkt_scbp); 27152 27153 sensep = (uint8_t *)&arqstat->sts_sensedata; 27154 } else { 27155 sensep = NULL; 27156 } 27157 27158 if ((pkt->pkt_reason == CMD_CMPLT) && 27159 (rc == STATUS_GOOD)) { 27160 /* No error - we're done */ 27161 rval = SD_SUCCESS; 27162 break; 27163 27164 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 27165 /* Lost connection - give up */ 27166 break; 27167 27168 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 27169 (pkt->pkt_state == 0)) { 27170 /* Pkt not dispatched - try again. */ 27171 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27172 27173 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27174 (rc == STATUS_QFULL)) { 27175 /* Queue full - try again. */ 27176 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27177 27178 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27179 (rc == STATUS_BUSY)) { 27180 /* Busy - try again. */ 27181 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27182 busy_count += (SD_SEC_TO_CSEC - 1); 27183 27184 } else if ((sensep != NULL) && 27185 (scsi_sense_key(sensep) == 27186 KEY_UNIT_ATTENTION)) { 27187 /* Unit Attention - try again */ 27188 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 27189 continue; 27190 27191 } else if ((sensep != NULL) && 27192 (scsi_sense_key(sensep) == KEY_NOT_READY) && 27193 (scsi_sense_asc(sensep) == 0x04) && 27194 (scsi_sense_ascq(sensep) == 0x01)) { 27195 /* Not ready -> ready - try again. */ 27196 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27197 busy_count += (SD_SEC_TO_CSEC - 1); 27198 27199 } else { 27200 /* BAD status - give up. */ 27201 break; 27202 } 27203 } 27204 27205 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 27206 !do_polled_io) { 27207 delay(drv_usectohz(poll_delay)); 27208 } else { 27209 /* we busy wait during cpr_dump or interrupt threads */ 27210 drv_usecwait(poll_delay); 27211 } 27212 } 27213 27214 pkt->pkt_flags = savef; 27215 pkt->pkt_comp = savec; 27216 pkt->pkt_time = savet; 27217 return (rval); 27218 } 27219 27220 27221 /* 27222 * Function: sd_persistent_reservation_in_read_keys 27223 * 27224 * Description: This routine is the driver entry point for handling CD-ROM 27225 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 27226 * by sending the SCSI-3 PRIN commands to the device. 27227 * Processes the read keys command response by copying the 27228 * reservation key information into the user provided buffer. 27229 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 27230 * 27231 * Arguments: un - Pointer to soft state struct for the target. 27232 * usrp - user provided pointer to multihost Persistent In Read 27233 * Keys structure (mhioc_inkeys_t) 27234 * flag - this argument is a pass through to ddi_copyxxx() 27235 * directly from the mode argument of ioctl(). 27236 * 27237 * Return Code: 0 - Success 27238 * EACCES 27239 * ENOTSUP 27240 * errno return code from sd_send_scsi_cmd() 27241 * 27242 * Context: Can sleep. Does not return until command is completed. 27243 */ 27244 27245 static int 27246 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 27247 mhioc_inkeys_t *usrp, int flag) 27248 { 27249 #ifdef _MULTI_DATAMODEL 27250 struct mhioc_key_list32 li32; 27251 #endif 27252 sd_prin_readkeys_t *in; 27253 mhioc_inkeys_t *ptr; 27254 mhioc_key_list_t li; 27255 uchar_t *data_bufp; 27256 int data_len; 27257 int rval; 27258 size_t copysz; 27259 27260 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 27261 return (EINVAL); 27262 } 27263 bzero(&li, sizeof (mhioc_key_list_t)); 27264 27265 /* 27266 * Get the listsize from user 27267 */ 27268 #ifdef _MULTI_DATAMODEL 27269 27270 switch (ddi_model_convert_from(flag & FMODELS)) { 27271 case DDI_MODEL_ILP32: 27272 copysz = sizeof (struct mhioc_key_list32); 27273 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 27274 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27275 "sd_persistent_reservation_in_read_keys: " 27276 "failed ddi_copyin: mhioc_key_list32_t\n"); 27277 rval = EFAULT; 27278 goto done; 27279 } 27280 li.listsize = li32.listsize; 27281 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 27282 break; 27283 27284 case DDI_MODEL_NONE: 27285 copysz = sizeof (mhioc_key_list_t); 27286 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27287 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27288 "sd_persistent_reservation_in_read_keys: " 27289 "failed ddi_copyin: mhioc_key_list_t\n"); 27290 rval = EFAULT; 27291 goto done; 27292 } 27293 break; 27294 } 27295 27296 #else /* ! _MULTI_DATAMODEL */ 27297 copysz = sizeof (mhioc_key_list_t); 27298 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27299 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27300 "sd_persistent_reservation_in_read_keys: " 27301 "failed ddi_copyin: mhioc_key_list_t\n"); 27302 rval = EFAULT; 27303 goto done; 27304 } 27305 #endif 27306 27307 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 27308 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 27309 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27310 27311 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 27312 data_len, data_bufp)) != 0) { 27313 goto done; 27314 } 27315 in = (sd_prin_readkeys_t *)data_bufp; 27316 ptr->generation = BE_32(in->generation); 27317 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 27318 27319 /* 27320 * Return the min(listsize, listlen) keys 27321 */ 27322 #ifdef _MULTI_DATAMODEL 27323 27324 switch (ddi_model_convert_from(flag & FMODELS)) { 27325 case DDI_MODEL_ILP32: 27326 li32.listlen = li.listlen; 27327 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 27328 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27329 "sd_persistent_reservation_in_read_keys: " 27330 "failed ddi_copyout: mhioc_key_list32_t\n"); 27331 rval = EFAULT; 27332 goto done; 27333 } 27334 break; 27335 27336 case DDI_MODEL_NONE: 27337 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27338 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27339 "sd_persistent_reservation_in_read_keys: " 27340 "failed ddi_copyout: mhioc_key_list_t\n"); 27341 rval = EFAULT; 27342 goto done; 27343 } 27344 break; 27345 } 27346 27347 #else /* ! _MULTI_DATAMODEL */ 27348 27349 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27350 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27351 "sd_persistent_reservation_in_read_keys: " 27352 "failed ddi_copyout: mhioc_key_list_t\n"); 27353 rval = EFAULT; 27354 goto done; 27355 } 27356 27357 #endif /* _MULTI_DATAMODEL */ 27358 27359 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 27360 li.listsize * MHIOC_RESV_KEY_SIZE); 27361 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 27362 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27363 "sd_persistent_reservation_in_read_keys: " 27364 "failed ddi_copyout: keylist\n"); 27365 rval = EFAULT; 27366 } 27367 done: 27368 kmem_free(data_bufp, data_len); 27369 return (rval); 27370 } 27371 27372 27373 /* 27374 * Function: sd_persistent_reservation_in_read_resv 27375 * 27376 * Description: This routine is the driver entry point for handling CD-ROM 27377 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 27378 * by sending the SCSI-3 PRIN commands to the device. 27379 * Process the read persistent reservations command response by 27380 * copying the reservation information into the user provided 27381 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 27382 * 27383 * Arguments: un - Pointer to soft state struct for the target. 27384 * usrp - user provided pointer to multihost Persistent In Read 27385 * Keys structure (mhioc_inkeys_t) 27386 * flag - this argument is a pass through to ddi_copyxxx() 27387 * directly from the mode argument of ioctl(). 27388 * 27389 * Return Code: 0 - Success 27390 * EACCES 27391 * ENOTSUP 27392 * errno return code from sd_send_scsi_cmd() 27393 * 27394 * Context: Can sleep. Does not return until command is completed. 27395 */ 27396 27397 static int 27398 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 27399 mhioc_inresvs_t *usrp, int flag) 27400 { 27401 #ifdef _MULTI_DATAMODEL 27402 struct mhioc_resv_desc_list32 resvlist32; 27403 #endif 27404 sd_prin_readresv_t *in; 27405 mhioc_inresvs_t *ptr; 27406 sd_readresv_desc_t *readresv_ptr; 27407 mhioc_resv_desc_list_t resvlist; 27408 mhioc_resv_desc_t resvdesc; 27409 uchar_t *data_bufp; 27410 int data_len; 27411 int rval; 27412 int i; 27413 size_t copysz; 27414 mhioc_resv_desc_t *bufp; 27415 27416 if ((ptr = usrp) == NULL) { 27417 return (EINVAL); 27418 } 27419 27420 /* 27421 * Get the listsize from user 27422 */ 27423 #ifdef _MULTI_DATAMODEL 27424 switch (ddi_model_convert_from(flag & FMODELS)) { 27425 case DDI_MODEL_ILP32: 27426 copysz = sizeof (struct mhioc_resv_desc_list32); 27427 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 27428 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27429 "sd_persistent_reservation_in_read_resv: " 27430 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27431 rval = EFAULT; 27432 goto done; 27433 } 27434 resvlist.listsize = resvlist32.listsize; 27435 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 27436 break; 27437 27438 case DDI_MODEL_NONE: 27439 copysz = sizeof (mhioc_resv_desc_list_t); 27440 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27441 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27442 "sd_persistent_reservation_in_read_resv: " 27443 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27444 rval = EFAULT; 27445 goto done; 27446 } 27447 break; 27448 } 27449 #else /* ! _MULTI_DATAMODEL */ 27450 copysz = sizeof (mhioc_resv_desc_list_t); 27451 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27452 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27453 "sd_persistent_reservation_in_read_resv: " 27454 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27455 rval = EFAULT; 27456 goto done; 27457 } 27458 #endif /* ! _MULTI_DATAMODEL */ 27459 27460 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 27461 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 27462 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27463 27464 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 27465 data_len, data_bufp)) != 0) { 27466 goto done; 27467 } 27468 in = (sd_prin_readresv_t *)data_bufp; 27469 ptr->generation = BE_32(in->generation); 27470 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 27471 27472 /* 27473 * Return the min(listsize, listlen( keys 27474 */ 27475 #ifdef _MULTI_DATAMODEL 27476 27477 switch (ddi_model_convert_from(flag & FMODELS)) { 27478 case DDI_MODEL_ILP32: 27479 resvlist32.listlen = resvlist.listlen; 27480 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27481 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27482 "sd_persistent_reservation_in_read_resv: " 27483 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27484 rval = EFAULT; 27485 goto done; 27486 } 27487 break; 27488 27489 case DDI_MODEL_NONE: 27490 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27491 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27492 "sd_persistent_reservation_in_read_resv: " 27493 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27494 rval = EFAULT; 27495 goto done; 27496 } 27497 break; 27498 } 27499 27500 #else /* ! _MULTI_DATAMODEL */ 27501 27502 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27503 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27504 "sd_persistent_reservation_in_read_resv: " 27505 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27506 rval = EFAULT; 27507 goto done; 27508 } 27509 27510 #endif /* ! _MULTI_DATAMODEL */ 27511 27512 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27513 bufp = resvlist.list; 27514 copysz = sizeof (mhioc_resv_desc_t); 27515 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27516 i++, readresv_ptr++, bufp++) { 27517 27518 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27519 MHIOC_RESV_KEY_SIZE); 27520 resvdesc.type = readresv_ptr->type; 27521 resvdesc.scope = readresv_ptr->scope; 27522 resvdesc.scope_specific_addr = 27523 BE_32(readresv_ptr->scope_specific_addr); 27524 27525 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27526 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27527 "sd_persistent_reservation_in_read_resv: " 27528 "failed ddi_copyout: resvlist\n"); 27529 rval = EFAULT; 27530 goto done; 27531 } 27532 } 27533 done: 27534 kmem_free(data_bufp, data_len); 27535 return (rval); 27536 } 27537 27538 27539 /* 27540 * Function: sr_change_blkmode() 27541 * 27542 * Description: This routine is the driver entry point for handling CD-ROM 27543 * block mode ioctl requests. Support for returning and changing 27544 * the current block size in use by the device is implemented. The 27545 * LBA size is changed via a MODE SELECT Block Descriptor. 27546 * 27547 * This routine issues a mode sense with an allocation length of 27548 * 12 bytes for the mode page header and a single block descriptor. 27549 * 27550 * Arguments: dev - the device 'dev_t' 27551 * cmd - the request type; one of CDROMGBLKMODE (get) or 27552 * CDROMSBLKMODE (set) 27553 * data - current block size or requested block size 27554 * flag - this argument is a pass through to ddi_copyxxx() directly 27555 * from the mode argument of ioctl(). 27556 * 27557 * Return Code: the code returned by sd_send_scsi_cmd() 27558 * EINVAL if invalid arguments are provided 27559 * EFAULT if ddi_copyxxx() fails 27560 * ENXIO if fail ddi_get_soft_state 27561 * EIO if invalid mode sense block descriptor length 27562 * 27563 */ 27564 27565 static int 27566 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27567 { 27568 struct sd_lun *un = NULL; 27569 struct mode_header *sense_mhp, *select_mhp; 27570 struct block_descriptor *sense_desc, *select_desc; 27571 int current_bsize; 27572 int rval = EINVAL; 27573 uchar_t *sense = NULL; 27574 uchar_t *select = NULL; 27575 27576 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27577 27578 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27579 return (ENXIO); 27580 } 27581 27582 /* 27583 * The block length is changed via the Mode Select block descriptor, the 27584 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27585 * required as part of this routine. Therefore the mode sense allocation 27586 * length is specified to be the length of a mode page header and a 27587 * block descriptor. 27588 */ 27589 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27590 27591 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27592 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27593 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27594 "sr_change_blkmode: Mode Sense Failed\n"); 27595 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27596 return (rval); 27597 } 27598 27599 /* Check the block descriptor len to handle only 1 block descriptor */ 27600 sense_mhp = (struct mode_header *)sense; 27601 if ((sense_mhp->bdesc_length == 0) || 27602 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27603 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27604 "sr_change_blkmode: Mode Sense returned invalid block" 27605 " descriptor length\n"); 27606 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27607 return (EIO); 27608 } 27609 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27610 current_bsize = ((sense_desc->blksize_hi << 16) | 27611 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27612 27613 /* Process command */ 27614 switch (cmd) { 27615 case CDROMGBLKMODE: 27616 /* Return the block size obtained during the mode sense */ 27617 if (ddi_copyout(¤t_bsize, (void *)data, 27618 sizeof (int), flag) != 0) 27619 rval = EFAULT; 27620 break; 27621 case CDROMSBLKMODE: 27622 /* Validate the requested block size */ 27623 switch (data) { 27624 case CDROM_BLK_512: 27625 case CDROM_BLK_1024: 27626 case CDROM_BLK_2048: 27627 case CDROM_BLK_2056: 27628 case CDROM_BLK_2336: 27629 case CDROM_BLK_2340: 27630 case CDROM_BLK_2352: 27631 case CDROM_BLK_2368: 27632 case CDROM_BLK_2448: 27633 case CDROM_BLK_2646: 27634 case CDROM_BLK_2647: 27635 break; 27636 default: 27637 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27638 "sr_change_blkmode: " 27639 "Block Size '%ld' Not Supported\n", data); 27640 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27641 return (EINVAL); 27642 } 27643 27644 /* 27645 * The current block size matches the requested block size so 27646 * there is no need to send the mode select to change the size 27647 */ 27648 if (current_bsize == data) { 27649 break; 27650 } 27651 27652 /* Build the select data for the requested block size */ 27653 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27654 select_mhp = (struct mode_header *)select; 27655 select_desc = 27656 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27657 /* 27658 * The LBA size is changed via the block descriptor, so the 27659 * descriptor is built according to the user data 27660 */ 27661 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27662 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27663 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27664 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27665 27666 /* Send the mode select for the requested block size */ 27667 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27668 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27669 SD_PATH_STANDARD)) != 0) { 27670 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27671 "sr_change_blkmode: Mode Select Failed\n"); 27672 /* 27673 * The mode select failed for the requested block size, 27674 * so reset the data for the original block size and 27675 * send it to the target. The error is indicated by the 27676 * return value for the failed mode select. 27677 */ 27678 select_desc->blksize_hi = sense_desc->blksize_hi; 27679 select_desc->blksize_mid = sense_desc->blksize_mid; 27680 select_desc->blksize_lo = sense_desc->blksize_lo; 27681 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27682 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27683 SD_PATH_STANDARD); 27684 } else { 27685 ASSERT(!mutex_owned(SD_MUTEX(un))); 27686 mutex_enter(SD_MUTEX(un)); 27687 sd_update_block_info(un, (uint32_t)data, 0); 27688 27689 mutex_exit(SD_MUTEX(un)); 27690 } 27691 break; 27692 default: 27693 /* should not reach here, but check anyway */ 27694 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27695 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27696 rval = EINVAL; 27697 break; 27698 } 27699 27700 if (select) { 27701 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27702 } 27703 if (sense) { 27704 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27705 } 27706 return (rval); 27707 } 27708 27709 27710 /* 27711 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27712 * implement driver support for getting and setting the CD speed. The command 27713 * set used will be based on the device type. If the device has not been 27714 * identified as MMC the Toshiba vendor specific mode page will be used. If 27715 * the device is MMC but does not support the Real Time Streaming feature 27716 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27717 * be used to read the speed. 27718 */ 27719 27720 /* 27721 * Function: sr_change_speed() 27722 * 27723 * Description: This routine is the driver entry point for handling CD-ROM 27724 * drive speed ioctl requests for devices supporting the Toshiba 27725 * vendor specific drive speed mode page. Support for returning 27726 * and changing the current drive speed in use by the device is 27727 * implemented. 27728 * 27729 * Arguments: dev - the device 'dev_t' 27730 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27731 * CDROMSDRVSPEED (set) 27732 * data - current drive speed or requested drive speed 27733 * flag - this argument is a pass through to ddi_copyxxx() directly 27734 * from the mode argument of ioctl(). 27735 * 27736 * Return Code: the code returned by sd_send_scsi_cmd() 27737 * EINVAL if invalid arguments are provided 27738 * EFAULT if ddi_copyxxx() fails 27739 * ENXIO if fail ddi_get_soft_state 27740 * EIO if invalid mode sense block descriptor length 27741 */ 27742 27743 static int 27744 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27745 { 27746 struct sd_lun *un = NULL; 27747 struct mode_header *sense_mhp, *select_mhp; 27748 struct mode_speed *sense_page, *select_page; 27749 int current_speed; 27750 int rval = EINVAL; 27751 int bd_len; 27752 uchar_t *sense = NULL; 27753 uchar_t *select = NULL; 27754 27755 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27756 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27757 return (ENXIO); 27758 } 27759 27760 /* 27761 * Note: The drive speed is being modified here according to a Toshiba 27762 * vendor specific mode page (0x31). 27763 */ 27764 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27765 27766 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27767 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27768 SD_PATH_STANDARD)) != 0) { 27769 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27770 "sr_change_speed: Mode Sense Failed\n"); 27771 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27772 return (rval); 27773 } 27774 sense_mhp = (struct mode_header *)sense; 27775 27776 /* Check the block descriptor len to handle only 1 block descriptor */ 27777 bd_len = sense_mhp->bdesc_length; 27778 if (bd_len > MODE_BLK_DESC_LENGTH) { 27779 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27780 "sr_change_speed: Mode Sense returned invalid block " 27781 "descriptor length\n"); 27782 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27783 return (EIO); 27784 } 27785 27786 sense_page = (struct mode_speed *) 27787 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27788 current_speed = sense_page->speed; 27789 27790 /* Process command */ 27791 switch (cmd) { 27792 case CDROMGDRVSPEED: 27793 /* Return the drive speed obtained during the mode sense */ 27794 if (current_speed == 0x2) { 27795 current_speed = CDROM_TWELVE_SPEED; 27796 } 27797 if (ddi_copyout(¤t_speed, (void *)data, 27798 sizeof (int), flag) != 0) { 27799 rval = EFAULT; 27800 } 27801 break; 27802 case CDROMSDRVSPEED: 27803 /* Validate the requested drive speed */ 27804 switch ((uchar_t)data) { 27805 case CDROM_TWELVE_SPEED: 27806 data = 0x2; 27807 /*FALLTHROUGH*/ 27808 case CDROM_NORMAL_SPEED: 27809 case CDROM_DOUBLE_SPEED: 27810 case CDROM_QUAD_SPEED: 27811 case CDROM_MAXIMUM_SPEED: 27812 break; 27813 default: 27814 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27815 "sr_change_speed: " 27816 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27817 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27818 return (EINVAL); 27819 } 27820 27821 /* 27822 * The current drive speed matches the requested drive speed so 27823 * there is no need to send the mode select to change the speed 27824 */ 27825 if (current_speed == data) { 27826 break; 27827 } 27828 27829 /* Build the select data for the requested drive speed */ 27830 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27831 select_mhp = (struct mode_header *)select; 27832 select_mhp->bdesc_length = 0; 27833 select_page = 27834 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27835 select_page = 27836 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27837 select_page->mode_page.code = CDROM_MODE_SPEED; 27838 select_page->mode_page.length = 2; 27839 select_page->speed = (uchar_t)data; 27840 27841 /* Send the mode select for the requested block size */ 27842 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27843 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27844 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27845 /* 27846 * The mode select failed for the requested drive speed, 27847 * so reset the data for the original drive speed and 27848 * send it to the target. The error is indicated by the 27849 * return value for the failed mode select. 27850 */ 27851 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27852 "sr_drive_speed: Mode Select Failed\n"); 27853 select_page->speed = sense_page->speed; 27854 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27855 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27856 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27857 } 27858 break; 27859 default: 27860 /* should not reach here, but check anyway */ 27861 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27862 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27863 rval = EINVAL; 27864 break; 27865 } 27866 27867 if (select) { 27868 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27869 } 27870 if (sense) { 27871 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27872 } 27873 27874 return (rval); 27875 } 27876 27877 27878 /* 27879 * Function: sr_atapi_change_speed() 27880 * 27881 * Description: This routine is the driver entry point for handling CD-ROM 27882 * drive speed ioctl requests for MMC devices that do not support 27883 * the Real Time Streaming feature (0x107). 27884 * 27885 * Note: This routine will use the SET SPEED command which may not 27886 * be supported by all devices. 27887 * 27888 * Arguments: dev- the device 'dev_t' 27889 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27890 * CDROMSDRVSPEED (set) 27891 * data- current drive speed or requested drive speed 27892 * flag- this argument is a pass through to ddi_copyxxx() directly 27893 * from the mode argument of ioctl(). 27894 * 27895 * Return Code: the code returned by sd_send_scsi_cmd() 27896 * EINVAL if invalid arguments are provided 27897 * EFAULT if ddi_copyxxx() fails 27898 * ENXIO if fail ddi_get_soft_state 27899 * EIO if invalid mode sense block descriptor length 27900 */ 27901 27902 static int 27903 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27904 { 27905 struct sd_lun *un; 27906 struct uscsi_cmd *com = NULL; 27907 struct mode_header_grp2 *sense_mhp; 27908 uchar_t *sense_page; 27909 uchar_t *sense = NULL; 27910 char cdb[CDB_GROUP5]; 27911 int bd_len; 27912 int current_speed = 0; 27913 int max_speed = 0; 27914 int rval; 27915 27916 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27917 27918 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27919 return (ENXIO); 27920 } 27921 27922 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27923 27924 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27925 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27926 SD_PATH_STANDARD)) != 0) { 27927 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27928 "sr_atapi_change_speed: Mode Sense Failed\n"); 27929 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27930 return (rval); 27931 } 27932 27933 /* Check the block descriptor len to handle only 1 block descriptor */ 27934 sense_mhp = (struct mode_header_grp2 *)sense; 27935 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27936 if (bd_len > MODE_BLK_DESC_LENGTH) { 27937 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27938 "sr_atapi_change_speed: Mode Sense returned invalid " 27939 "block descriptor length\n"); 27940 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27941 return (EIO); 27942 } 27943 27944 /* Calculate the current and maximum drive speeds */ 27945 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27946 current_speed = (sense_page[14] << 8) | sense_page[15]; 27947 max_speed = (sense_page[8] << 8) | sense_page[9]; 27948 27949 /* Process the command */ 27950 switch (cmd) { 27951 case CDROMGDRVSPEED: 27952 current_speed /= SD_SPEED_1X; 27953 if (ddi_copyout(¤t_speed, (void *)data, 27954 sizeof (int), flag) != 0) 27955 rval = EFAULT; 27956 break; 27957 case CDROMSDRVSPEED: 27958 /* Convert the speed code to KB/sec */ 27959 switch ((uchar_t)data) { 27960 case CDROM_NORMAL_SPEED: 27961 current_speed = SD_SPEED_1X; 27962 break; 27963 case CDROM_DOUBLE_SPEED: 27964 current_speed = 2 * SD_SPEED_1X; 27965 break; 27966 case CDROM_QUAD_SPEED: 27967 current_speed = 4 * SD_SPEED_1X; 27968 break; 27969 case CDROM_TWELVE_SPEED: 27970 current_speed = 12 * SD_SPEED_1X; 27971 break; 27972 case CDROM_MAXIMUM_SPEED: 27973 current_speed = 0xffff; 27974 break; 27975 default: 27976 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27977 "sr_atapi_change_speed: invalid drive speed %d\n", 27978 (uchar_t)data); 27979 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27980 return (EINVAL); 27981 } 27982 27983 /* Check the request against the drive's max speed. */ 27984 if (current_speed != 0xffff) { 27985 if (current_speed > max_speed) { 27986 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27987 return (EINVAL); 27988 } 27989 } 27990 27991 /* 27992 * Build and send the SET SPEED command 27993 * 27994 * Note: The SET SPEED (0xBB) command used in this routine is 27995 * obsolete per the SCSI MMC spec but still supported in the 27996 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 27997 * therefore the command is still implemented in this routine. 27998 */ 27999 bzero(cdb, sizeof (cdb)); 28000 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 28001 cdb[2] = (uchar_t)(current_speed >> 8); 28002 cdb[3] = (uchar_t)current_speed; 28003 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28004 com->uscsi_cdb = (caddr_t)cdb; 28005 com->uscsi_cdblen = CDB_GROUP5; 28006 com->uscsi_bufaddr = NULL; 28007 com->uscsi_buflen = 0; 28008 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28009 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 28010 UIO_SYSSPACE, SD_PATH_STANDARD); 28011 break; 28012 default: 28013 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28014 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 28015 rval = EINVAL; 28016 } 28017 28018 if (sense) { 28019 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 28020 } 28021 if (com) { 28022 kmem_free(com, sizeof (*com)); 28023 } 28024 return (rval); 28025 } 28026 28027 28028 /* 28029 * Function: sr_pause_resume() 28030 * 28031 * Description: This routine is the driver entry point for handling CD-ROM 28032 * pause/resume ioctl requests. This only affects the audio play 28033 * operation. 28034 * 28035 * Arguments: dev - the device 'dev_t' 28036 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 28037 * for setting the resume bit of the cdb. 28038 * 28039 * Return Code: the code returned by sd_send_scsi_cmd() 28040 * EINVAL if invalid mode specified 28041 * 28042 */ 28043 28044 static int 28045 sr_pause_resume(dev_t dev, int cmd) 28046 { 28047 struct sd_lun *un; 28048 struct uscsi_cmd *com; 28049 char cdb[CDB_GROUP1]; 28050 int rval; 28051 28052 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28053 return (ENXIO); 28054 } 28055 28056 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28057 bzero(cdb, CDB_GROUP1); 28058 cdb[0] = SCMD_PAUSE_RESUME; 28059 switch (cmd) { 28060 case CDROMRESUME: 28061 cdb[8] = 1; 28062 break; 28063 case CDROMPAUSE: 28064 cdb[8] = 0; 28065 break; 28066 default: 28067 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 28068 " Command '%x' Not Supported\n", cmd); 28069 rval = EINVAL; 28070 goto done; 28071 } 28072 28073 com->uscsi_cdb = cdb; 28074 com->uscsi_cdblen = CDB_GROUP1; 28075 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28076 28077 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28078 UIO_SYSSPACE, SD_PATH_STANDARD); 28079 28080 done: 28081 kmem_free(com, sizeof (*com)); 28082 return (rval); 28083 } 28084 28085 28086 /* 28087 * Function: sr_play_msf() 28088 * 28089 * Description: This routine is the driver entry point for handling CD-ROM 28090 * ioctl requests to output the audio signals at the specified 28091 * starting address and continue the audio play until the specified 28092 * ending address (CDROMPLAYMSF) The address is in Minute Second 28093 * Frame (MSF) format. 28094 * 28095 * Arguments: dev - the device 'dev_t' 28096 * data - pointer to user provided audio msf structure, 28097 * specifying start/end addresses. 28098 * flag - this argument is a pass through to ddi_copyxxx() 28099 * directly from the mode argument of ioctl(). 28100 * 28101 * Return Code: the code returned by sd_send_scsi_cmd() 28102 * EFAULT if ddi_copyxxx() fails 28103 * ENXIO if fail ddi_get_soft_state 28104 * EINVAL if data pointer is NULL 28105 */ 28106 28107 static int 28108 sr_play_msf(dev_t dev, caddr_t data, int flag) 28109 { 28110 struct sd_lun *un; 28111 struct uscsi_cmd *com; 28112 struct cdrom_msf msf_struct; 28113 struct cdrom_msf *msf = &msf_struct; 28114 char cdb[CDB_GROUP1]; 28115 int rval; 28116 28117 if (data == NULL) { 28118 return (EINVAL); 28119 } 28120 28121 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28122 return (ENXIO); 28123 } 28124 28125 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 28126 return (EFAULT); 28127 } 28128 28129 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28130 bzero(cdb, CDB_GROUP1); 28131 cdb[0] = SCMD_PLAYAUDIO_MSF; 28132 if (un->un_f_cfg_playmsf_bcd == TRUE) { 28133 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 28134 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 28135 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 28136 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 28137 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 28138 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 28139 } else { 28140 cdb[3] = msf->cdmsf_min0; 28141 cdb[4] = msf->cdmsf_sec0; 28142 cdb[5] = msf->cdmsf_frame0; 28143 cdb[6] = msf->cdmsf_min1; 28144 cdb[7] = msf->cdmsf_sec1; 28145 cdb[8] = msf->cdmsf_frame1; 28146 } 28147 com->uscsi_cdb = cdb; 28148 com->uscsi_cdblen = CDB_GROUP1; 28149 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28150 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28151 UIO_SYSSPACE, SD_PATH_STANDARD); 28152 kmem_free(com, sizeof (*com)); 28153 return (rval); 28154 } 28155 28156 28157 /* 28158 * Function: sr_play_trkind() 28159 * 28160 * Description: This routine is the driver entry point for handling CD-ROM 28161 * ioctl requests to output the audio signals at the specified 28162 * starting address and continue the audio play until the specified 28163 * ending address (CDROMPLAYTRKIND). The address is in Track Index 28164 * format. 28165 * 28166 * Arguments: dev - the device 'dev_t' 28167 * data - pointer to user provided audio track/index structure, 28168 * specifying start/end addresses. 28169 * flag - this argument is a pass through to ddi_copyxxx() 28170 * directly from the mode argument of ioctl(). 28171 * 28172 * Return Code: the code returned by sd_send_scsi_cmd() 28173 * EFAULT if ddi_copyxxx() fails 28174 * ENXIO if fail ddi_get_soft_state 28175 * EINVAL if data pointer is NULL 28176 */ 28177 28178 static int 28179 sr_play_trkind(dev_t dev, caddr_t data, int flag) 28180 { 28181 struct cdrom_ti ti_struct; 28182 struct cdrom_ti *ti = &ti_struct; 28183 struct uscsi_cmd *com = NULL; 28184 char cdb[CDB_GROUP1]; 28185 int rval; 28186 28187 if (data == NULL) { 28188 return (EINVAL); 28189 } 28190 28191 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 28192 return (EFAULT); 28193 } 28194 28195 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28196 bzero(cdb, CDB_GROUP1); 28197 cdb[0] = SCMD_PLAYAUDIO_TI; 28198 cdb[4] = ti->cdti_trk0; 28199 cdb[5] = ti->cdti_ind0; 28200 cdb[7] = ti->cdti_trk1; 28201 cdb[8] = ti->cdti_ind1; 28202 com->uscsi_cdb = cdb; 28203 com->uscsi_cdblen = CDB_GROUP1; 28204 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28205 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28206 UIO_SYSSPACE, SD_PATH_STANDARD); 28207 kmem_free(com, sizeof (*com)); 28208 return (rval); 28209 } 28210 28211 28212 /* 28213 * Function: sr_read_all_subcodes() 28214 * 28215 * Description: This routine is the driver entry point for handling CD-ROM 28216 * ioctl requests to return raw subcode data while the target is 28217 * playing audio (CDROMSUBCODE). 28218 * 28219 * Arguments: dev - the device 'dev_t' 28220 * data - pointer to user provided cdrom subcode structure, 28221 * specifying the transfer length and address. 28222 * flag - this argument is a pass through to ddi_copyxxx() 28223 * directly from the mode argument of ioctl(). 28224 * 28225 * Return Code: the code returned by sd_send_scsi_cmd() 28226 * EFAULT if ddi_copyxxx() fails 28227 * ENXIO if fail ddi_get_soft_state 28228 * EINVAL if data pointer is NULL 28229 */ 28230 28231 static int 28232 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 28233 { 28234 struct sd_lun *un = NULL; 28235 struct uscsi_cmd *com = NULL; 28236 struct cdrom_subcode *subcode = NULL; 28237 int rval; 28238 size_t buflen; 28239 char cdb[CDB_GROUP5]; 28240 28241 #ifdef _MULTI_DATAMODEL 28242 /* To support ILP32 applications in an LP64 world */ 28243 struct cdrom_subcode32 cdrom_subcode32; 28244 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 28245 #endif 28246 if (data == NULL) { 28247 return (EINVAL); 28248 } 28249 28250 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28251 return (ENXIO); 28252 } 28253 28254 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 28255 28256 #ifdef _MULTI_DATAMODEL 28257 switch (ddi_model_convert_from(flag & FMODELS)) { 28258 case DDI_MODEL_ILP32: 28259 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 28260 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28261 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28262 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28263 return (EFAULT); 28264 } 28265 /* Convert the ILP32 uscsi data from the application to LP64 */ 28266 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 28267 break; 28268 case DDI_MODEL_NONE: 28269 if (ddi_copyin(data, subcode, 28270 sizeof (struct cdrom_subcode), flag)) { 28271 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28272 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28273 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28274 return (EFAULT); 28275 } 28276 break; 28277 } 28278 #else /* ! _MULTI_DATAMODEL */ 28279 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 28280 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28281 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28282 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28283 return (EFAULT); 28284 } 28285 #endif /* _MULTI_DATAMODEL */ 28286 28287 /* 28288 * Since MMC-2 expects max 3 bytes for length, check if the 28289 * length input is greater than 3 bytes 28290 */ 28291 if ((subcode->cdsc_length & 0xFF000000) != 0) { 28292 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28293 "sr_read_all_subcodes: " 28294 "cdrom transfer length too large: %d (limit %d)\n", 28295 subcode->cdsc_length, 0xFFFFFF); 28296 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28297 return (EINVAL); 28298 } 28299 28300 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 28301 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28302 bzero(cdb, CDB_GROUP5); 28303 28304 if (un->un_f_mmc_cap == TRUE) { 28305 cdb[0] = (char)SCMD_READ_CD; 28306 cdb[2] = (char)0xff; 28307 cdb[3] = (char)0xff; 28308 cdb[4] = (char)0xff; 28309 cdb[5] = (char)0xff; 28310 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28311 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28312 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 28313 cdb[10] = 1; 28314 } else { 28315 /* 28316 * Note: A vendor specific command (0xDF) is being used her to 28317 * request a read of all subcodes. 28318 */ 28319 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 28320 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 28321 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28322 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28323 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 28324 } 28325 com->uscsi_cdb = cdb; 28326 com->uscsi_cdblen = CDB_GROUP5; 28327 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 28328 com->uscsi_buflen = buflen; 28329 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28330 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28331 UIO_SYSSPACE, SD_PATH_STANDARD); 28332 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28333 kmem_free(com, sizeof (*com)); 28334 return (rval); 28335 } 28336 28337 28338 /* 28339 * Function: sr_read_subchannel() 28340 * 28341 * Description: This routine is the driver entry point for handling CD-ROM 28342 * ioctl requests to return the Q sub-channel data of the CD 28343 * current position block. (CDROMSUBCHNL) The data includes the 28344 * track number, index number, absolute CD-ROM address (LBA or MSF 28345 * format per the user) , track relative CD-ROM address (LBA or MSF 28346 * format per the user), control data and audio status. 28347 * 28348 * Arguments: dev - the device 'dev_t' 28349 * data - pointer to user provided cdrom sub-channel structure 28350 * flag - this argument is a pass through to ddi_copyxxx() 28351 * directly from the mode argument of ioctl(). 28352 * 28353 * Return Code: the code returned by sd_send_scsi_cmd() 28354 * EFAULT if ddi_copyxxx() fails 28355 * ENXIO if fail ddi_get_soft_state 28356 * EINVAL if data pointer is NULL 28357 */ 28358 28359 static int 28360 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 28361 { 28362 struct sd_lun *un; 28363 struct uscsi_cmd *com; 28364 struct cdrom_subchnl subchanel; 28365 struct cdrom_subchnl *subchnl = &subchanel; 28366 char cdb[CDB_GROUP1]; 28367 caddr_t buffer; 28368 int rval; 28369 28370 if (data == NULL) { 28371 return (EINVAL); 28372 } 28373 28374 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28375 (un->un_state == SD_STATE_OFFLINE)) { 28376 return (ENXIO); 28377 } 28378 28379 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 28380 return (EFAULT); 28381 } 28382 28383 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 28384 bzero(cdb, CDB_GROUP1); 28385 cdb[0] = SCMD_READ_SUBCHANNEL; 28386 /* Set the MSF bit based on the user requested address format */ 28387 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 28388 /* 28389 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 28390 * returned 28391 */ 28392 cdb[2] = 0x40; 28393 /* 28394 * Set byte 3 to specify the return data format. A value of 0x01 28395 * indicates that the CD-ROM current position should be returned. 28396 */ 28397 cdb[3] = 0x01; 28398 cdb[8] = 0x10; 28399 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28400 com->uscsi_cdb = cdb; 28401 com->uscsi_cdblen = CDB_GROUP1; 28402 com->uscsi_bufaddr = buffer; 28403 com->uscsi_buflen = 16; 28404 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28405 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28406 UIO_SYSSPACE, SD_PATH_STANDARD); 28407 if (rval != 0) { 28408 kmem_free(buffer, 16); 28409 kmem_free(com, sizeof (*com)); 28410 return (rval); 28411 } 28412 28413 /* Process the returned Q sub-channel data */ 28414 subchnl->cdsc_audiostatus = buffer[1]; 28415 subchnl->cdsc_adr = (buffer[5] & 0xF0); 28416 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 28417 subchnl->cdsc_trk = buffer[6]; 28418 subchnl->cdsc_ind = buffer[7]; 28419 if (subchnl->cdsc_format & CDROM_LBA) { 28420 subchnl->cdsc_absaddr.lba = 28421 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28422 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28423 subchnl->cdsc_reladdr.lba = 28424 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 28425 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 28426 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 28427 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 28428 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 28429 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 28430 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 28431 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 28432 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 28433 } else { 28434 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 28435 subchnl->cdsc_absaddr.msf.second = buffer[10]; 28436 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 28437 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 28438 subchnl->cdsc_reladdr.msf.second = buffer[14]; 28439 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 28440 } 28441 kmem_free(buffer, 16); 28442 kmem_free(com, sizeof (*com)); 28443 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 28444 != 0) { 28445 return (EFAULT); 28446 } 28447 return (rval); 28448 } 28449 28450 28451 /* 28452 * Function: sr_read_tocentry() 28453 * 28454 * Description: This routine is the driver entry point for handling CD-ROM 28455 * ioctl requests to read from the Table of Contents (TOC) 28456 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 28457 * fields, the starting address (LBA or MSF format per the user) 28458 * and the data mode if the user specified track is a data track. 28459 * 28460 * Note: The READ HEADER (0x44) command used in this routine is 28461 * obsolete per the SCSI MMC spec but still supported in the 28462 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28463 * therefore the command is still implemented in this routine. 28464 * 28465 * Arguments: dev - the device 'dev_t' 28466 * data - pointer to user provided toc entry structure, 28467 * specifying the track # and the address format 28468 * (LBA or MSF). 28469 * flag - this argument is a pass through to ddi_copyxxx() 28470 * directly from the mode argument of ioctl(). 28471 * 28472 * Return Code: the code returned by sd_send_scsi_cmd() 28473 * EFAULT if ddi_copyxxx() fails 28474 * ENXIO if fail ddi_get_soft_state 28475 * EINVAL if data pointer is NULL 28476 */ 28477 28478 static int 28479 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28480 { 28481 struct sd_lun *un = NULL; 28482 struct uscsi_cmd *com; 28483 struct cdrom_tocentry toc_entry; 28484 struct cdrom_tocentry *entry = &toc_entry; 28485 caddr_t buffer; 28486 int rval; 28487 char cdb[CDB_GROUP1]; 28488 28489 if (data == NULL) { 28490 return (EINVAL); 28491 } 28492 28493 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28494 (un->un_state == SD_STATE_OFFLINE)) { 28495 return (ENXIO); 28496 } 28497 28498 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28499 return (EFAULT); 28500 } 28501 28502 /* Validate the requested track and address format */ 28503 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28504 return (EINVAL); 28505 } 28506 28507 if (entry->cdte_track == 0) { 28508 return (EINVAL); 28509 } 28510 28511 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28512 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28513 bzero(cdb, CDB_GROUP1); 28514 28515 cdb[0] = SCMD_READ_TOC; 28516 /* Set the MSF bit based on the user requested address format */ 28517 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28518 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28519 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28520 } else { 28521 cdb[6] = entry->cdte_track; 28522 } 28523 28524 /* 28525 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28526 * (4 byte TOC response header + 8 byte track descriptor) 28527 */ 28528 cdb[8] = 12; 28529 com->uscsi_cdb = cdb; 28530 com->uscsi_cdblen = CDB_GROUP1; 28531 com->uscsi_bufaddr = buffer; 28532 com->uscsi_buflen = 0x0C; 28533 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28534 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28535 UIO_SYSSPACE, SD_PATH_STANDARD); 28536 if (rval != 0) { 28537 kmem_free(buffer, 12); 28538 kmem_free(com, sizeof (*com)); 28539 return (rval); 28540 } 28541 28542 /* Process the toc entry */ 28543 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28544 entry->cdte_ctrl = (buffer[5] & 0x0F); 28545 if (entry->cdte_format & CDROM_LBA) { 28546 entry->cdte_addr.lba = 28547 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28548 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28549 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28550 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28551 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28552 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28553 /* 28554 * Send a READ TOC command using the LBA address format to get 28555 * the LBA for the track requested so it can be used in the 28556 * READ HEADER request 28557 * 28558 * Note: The MSF bit of the READ HEADER command specifies the 28559 * output format. The block address specified in that command 28560 * must be in LBA format. 28561 */ 28562 cdb[1] = 0; 28563 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28564 UIO_SYSSPACE, SD_PATH_STANDARD); 28565 if (rval != 0) { 28566 kmem_free(buffer, 12); 28567 kmem_free(com, sizeof (*com)); 28568 return (rval); 28569 } 28570 } else { 28571 entry->cdte_addr.msf.minute = buffer[9]; 28572 entry->cdte_addr.msf.second = buffer[10]; 28573 entry->cdte_addr.msf.frame = buffer[11]; 28574 /* 28575 * Send a READ TOC command using the LBA address format to get 28576 * the LBA for the track requested so it can be used in the 28577 * READ HEADER request 28578 * 28579 * Note: The MSF bit of the READ HEADER command specifies the 28580 * output format. The block address specified in that command 28581 * must be in LBA format. 28582 */ 28583 cdb[1] = 0; 28584 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28585 UIO_SYSSPACE, SD_PATH_STANDARD); 28586 if (rval != 0) { 28587 kmem_free(buffer, 12); 28588 kmem_free(com, sizeof (*com)); 28589 return (rval); 28590 } 28591 } 28592 28593 /* 28594 * Build and send the READ HEADER command to determine the data mode of 28595 * the user specified track. 28596 */ 28597 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28598 (entry->cdte_track != CDROM_LEADOUT)) { 28599 bzero(cdb, CDB_GROUP1); 28600 cdb[0] = SCMD_READ_HEADER; 28601 cdb[2] = buffer[8]; 28602 cdb[3] = buffer[9]; 28603 cdb[4] = buffer[10]; 28604 cdb[5] = buffer[11]; 28605 cdb[8] = 0x08; 28606 com->uscsi_buflen = 0x08; 28607 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28608 UIO_SYSSPACE, SD_PATH_STANDARD); 28609 if (rval == 0) { 28610 entry->cdte_datamode = buffer[0]; 28611 } else { 28612 /* 28613 * READ HEADER command failed, since this is 28614 * obsoleted in one spec, its better to return 28615 * -1 for an invlid track so that we can still 28616 * recieve the rest of the TOC data. 28617 */ 28618 entry->cdte_datamode = (uchar_t)-1; 28619 } 28620 } else { 28621 entry->cdte_datamode = (uchar_t)-1; 28622 } 28623 28624 kmem_free(buffer, 12); 28625 kmem_free(com, sizeof (*com)); 28626 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28627 return (EFAULT); 28628 28629 return (rval); 28630 } 28631 28632 28633 /* 28634 * Function: sr_read_tochdr() 28635 * 28636 * Description: This routine is the driver entry point for handling CD-ROM 28637 * ioctl requests to read the Table of Contents (TOC) header 28638 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28639 * and ending track numbers 28640 * 28641 * Arguments: dev - the device 'dev_t' 28642 * data - pointer to user provided toc header structure, 28643 * specifying the starting and ending track numbers. 28644 * flag - this argument is a pass through to ddi_copyxxx() 28645 * directly from the mode argument of ioctl(). 28646 * 28647 * Return Code: the code returned by sd_send_scsi_cmd() 28648 * EFAULT if ddi_copyxxx() fails 28649 * ENXIO if fail ddi_get_soft_state 28650 * EINVAL if data pointer is NULL 28651 */ 28652 28653 static int 28654 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28655 { 28656 struct sd_lun *un; 28657 struct uscsi_cmd *com; 28658 struct cdrom_tochdr toc_header; 28659 struct cdrom_tochdr *hdr = &toc_header; 28660 char cdb[CDB_GROUP1]; 28661 int rval; 28662 caddr_t buffer; 28663 28664 if (data == NULL) { 28665 return (EINVAL); 28666 } 28667 28668 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28669 (un->un_state == SD_STATE_OFFLINE)) { 28670 return (ENXIO); 28671 } 28672 28673 buffer = kmem_zalloc(4, KM_SLEEP); 28674 bzero(cdb, CDB_GROUP1); 28675 cdb[0] = SCMD_READ_TOC; 28676 /* 28677 * Specifying a track number of 0x00 in the READ TOC command indicates 28678 * that the TOC header should be returned 28679 */ 28680 cdb[6] = 0x00; 28681 /* 28682 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28683 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28684 */ 28685 cdb[8] = 0x04; 28686 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28687 com->uscsi_cdb = cdb; 28688 com->uscsi_cdblen = CDB_GROUP1; 28689 com->uscsi_bufaddr = buffer; 28690 com->uscsi_buflen = 0x04; 28691 com->uscsi_timeout = 300; 28692 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28693 28694 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28695 UIO_SYSSPACE, SD_PATH_STANDARD); 28696 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28697 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28698 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28699 } else { 28700 hdr->cdth_trk0 = buffer[2]; 28701 hdr->cdth_trk1 = buffer[3]; 28702 } 28703 kmem_free(buffer, 4); 28704 kmem_free(com, sizeof (*com)); 28705 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28706 return (EFAULT); 28707 } 28708 return (rval); 28709 } 28710 28711 28712 /* 28713 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28714 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28715 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28716 * digital audio and extended architecture digital audio. These modes are 28717 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28718 * MMC specs. 28719 * 28720 * In addition to support for the various data formats these routines also 28721 * include support for devices that implement only the direct access READ 28722 * commands (0x08, 0x28), devices that implement the READ_CD commands 28723 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28724 * READ CDXA commands (0xD8, 0xDB) 28725 */ 28726 28727 /* 28728 * Function: sr_read_mode1() 28729 * 28730 * Description: This routine is the driver entry point for handling CD-ROM 28731 * ioctl read mode1 requests (CDROMREADMODE1). 28732 * 28733 * Arguments: dev - the device 'dev_t' 28734 * data - pointer to user provided cd read structure specifying 28735 * the lba buffer address and length. 28736 * flag - this argument is a pass through to ddi_copyxxx() 28737 * directly from the mode argument of ioctl(). 28738 * 28739 * Return Code: the code returned by sd_send_scsi_cmd() 28740 * EFAULT if ddi_copyxxx() fails 28741 * ENXIO if fail ddi_get_soft_state 28742 * EINVAL if data pointer is NULL 28743 */ 28744 28745 static int 28746 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28747 { 28748 struct sd_lun *un; 28749 struct cdrom_read mode1_struct; 28750 struct cdrom_read *mode1 = &mode1_struct; 28751 int rval; 28752 #ifdef _MULTI_DATAMODEL 28753 /* To support ILP32 applications in an LP64 world */ 28754 struct cdrom_read32 cdrom_read32; 28755 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28756 #endif /* _MULTI_DATAMODEL */ 28757 28758 if (data == NULL) { 28759 return (EINVAL); 28760 } 28761 28762 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28763 (un->un_state == SD_STATE_OFFLINE)) { 28764 return (ENXIO); 28765 } 28766 28767 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28768 "sd_read_mode1: entry: un:0x%p\n", un); 28769 28770 #ifdef _MULTI_DATAMODEL 28771 switch (ddi_model_convert_from(flag & FMODELS)) { 28772 case DDI_MODEL_ILP32: 28773 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28774 return (EFAULT); 28775 } 28776 /* Convert the ILP32 uscsi data from the application to LP64 */ 28777 cdrom_read32tocdrom_read(cdrd32, mode1); 28778 break; 28779 case DDI_MODEL_NONE: 28780 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28781 return (EFAULT); 28782 } 28783 } 28784 #else /* ! _MULTI_DATAMODEL */ 28785 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28786 return (EFAULT); 28787 } 28788 #endif /* _MULTI_DATAMODEL */ 28789 28790 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28791 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28792 28793 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28794 "sd_read_mode1: exit: un:0x%p\n", un); 28795 28796 return (rval); 28797 } 28798 28799 28800 /* 28801 * Function: sr_read_cd_mode2() 28802 * 28803 * Description: This routine is the driver entry point for handling CD-ROM 28804 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28805 * support the READ CD (0xBE) command or the 1st generation 28806 * READ CD (0xD4) command. 28807 * 28808 * Arguments: dev - the device 'dev_t' 28809 * data - pointer to user provided cd read structure specifying 28810 * the lba buffer address and length. 28811 * flag - this argument is a pass through to ddi_copyxxx() 28812 * directly from the mode argument of ioctl(). 28813 * 28814 * Return Code: the code returned by sd_send_scsi_cmd() 28815 * EFAULT if ddi_copyxxx() fails 28816 * ENXIO if fail ddi_get_soft_state 28817 * EINVAL if data pointer is NULL 28818 */ 28819 28820 static int 28821 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28822 { 28823 struct sd_lun *un; 28824 struct uscsi_cmd *com; 28825 struct cdrom_read mode2_struct; 28826 struct cdrom_read *mode2 = &mode2_struct; 28827 uchar_t cdb[CDB_GROUP5]; 28828 int nblocks; 28829 int rval; 28830 #ifdef _MULTI_DATAMODEL 28831 /* To support ILP32 applications in an LP64 world */ 28832 struct cdrom_read32 cdrom_read32; 28833 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28834 #endif /* _MULTI_DATAMODEL */ 28835 28836 if (data == NULL) { 28837 return (EINVAL); 28838 } 28839 28840 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28841 (un->un_state == SD_STATE_OFFLINE)) { 28842 return (ENXIO); 28843 } 28844 28845 #ifdef _MULTI_DATAMODEL 28846 switch (ddi_model_convert_from(flag & FMODELS)) { 28847 case DDI_MODEL_ILP32: 28848 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28849 return (EFAULT); 28850 } 28851 /* Convert the ILP32 uscsi data from the application to LP64 */ 28852 cdrom_read32tocdrom_read(cdrd32, mode2); 28853 break; 28854 case DDI_MODEL_NONE: 28855 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28856 return (EFAULT); 28857 } 28858 break; 28859 } 28860 28861 #else /* ! _MULTI_DATAMODEL */ 28862 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28863 return (EFAULT); 28864 } 28865 #endif /* _MULTI_DATAMODEL */ 28866 28867 bzero(cdb, sizeof (cdb)); 28868 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28869 /* Read command supported by 1st generation atapi drives */ 28870 cdb[0] = SCMD_READ_CDD4; 28871 } else { 28872 /* Universal CD Access Command */ 28873 cdb[0] = SCMD_READ_CD; 28874 } 28875 28876 /* 28877 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28878 */ 28879 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28880 28881 /* set the start address */ 28882 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28883 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28884 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28885 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28886 28887 /* set the transfer length */ 28888 nblocks = mode2->cdread_buflen / 2336; 28889 cdb[6] = (uchar_t)(nblocks >> 16); 28890 cdb[7] = (uchar_t)(nblocks >> 8); 28891 cdb[8] = (uchar_t)nblocks; 28892 28893 /* set the filter bits */ 28894 cdb[9] = CDROM_READ_CD_USERDATA; 28895 28896 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28897 com->uscsi_cdb = (caddr_t)cdb; 28898 com->uscsi_cdblen = sizeof (cdb); 28899 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28900 com->uscsi_buflen = mode2->cdread_buflen; 28901 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28902 28903 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28904 UIO_SYSSPACE, SD_PATH_STANDARD); 28905 kmem_free(com, sizeof (*com)); 28906 return (rval); 28907 } 28908 28909 28910 /* 28911 * Function: sr_read_mode2() 28912 * 28913 * Description: This routine is the driver entry point for handling CD-ROM 28914 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28915 * do not support the READ CD (0xBE) command. 28916 * 28917 * Arguments: dev - the device 'dev_t' 28918 * data - pointer to user provided cd read structure specifying 28919 * the lba buffer address and length. 28920 * flag - this argument is a pass through to ddi_copyxxx() 28921 * directly from the mode argument of ioctl(). 28922 * 28923 * Return Code: the code returned by sd_send_scsi_cmd() 28924 * EFAULT if ddi_copyxxx() fails 28925 * ENXIO if fail ddi_get_soft_state 28926 * EINVAL if data pointer is NULL 28927 * EIO if fail to reset block size 28928 * EAGAIN if commands are in progress in the driver 28929 */ 28930 28931 static int 28932 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28933 { 28934 struct sd_lun *un; 28935 struct cdrom_read mode2_struct; 28936 struct cdrom_read *mode2 = &mode2_struct; 28937 int rval; 28938 uint32_t restore_blksize; 28939 struct uscsi_cmd *com; 28940 uchar_t cdb[CDB_GROUP0]; 28941 int nblocks; 28942 28943 #ifdef _MULTI_DATAMODEL 28944 /* To support ILP32 applications in an LP64 world */ 28945 struct cdrom_read32 cdrom_read32; 28946 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28947 #endif /* _MULTI_DATAMODEL */ 28948 28949 if (data == NULL) { 28950 return (EINVAL); 28951 } 28952 28953 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28954 (un->un_state == SD_STATE_OFFLINE)) { 28955 return (ENXIO); 28956 } 28957 28958 /* 28959 * Because this routine will update the device and driver block size 28960 * being used we want to make sure there are no commands in progress. 28961 * If commands are in progress the user will have to try again. 28962 * 28963 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28964 * in sdioctl to protect commands from sdioctl through to the top of 28965 * sd_uscsi_strategy. See sdioctl for details. 28966 */ 28967 mutex_enter(SD_MUTEX(un)); 28968 if (un->un_ncmds_in_driver != 1) { 28969 mutex_exit(SD_MUTEX(un)); 28970 return (EAGAIN); 28971 } 28972 mutex_exit(SD_MUTEX(un)); 28973 28974 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28975 "sd_read_mode2: entry: un:0x%p\n", un); 28976 28977 #ifdef _MULTI_DATAMODEL 28978 switch (ddi_model_convert_from(flag & FMODELS)) { 28979 case DDI_MODEL_ILP32: 28980 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28981 return (EFAULT); 28982 } 28983 /* Convert the ILP32 uscsi data from the application to LP64 */ 28984 cdrom_read32tocdrom_read(cdrd32, mode2); 28985 break; 28986 case DDI_MODEL_NONE: 28987 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28988 return (EFAULT); 28989 } 28990 break; 28991 } 28992 #else /* ! _MULTI_DATAMODEL */ 28993 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 28994 return (EFAULT); 28995 } 28996 #endif /* _MULTI_DATAMODEL */ 28997 28998 /* Store the current target block size for restoration later */ 28999 restore_blksize = un->un_tgt_blocksize; 29000 29001 /* Change the device and soft state target block size to 2336 */ 29002 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 29003 rval = EIO; 29004 goto done; 29005 } 29006 29007 29008 bzero(cdb, sizeof (cdb)); 29009 29010 /* set READ operation */ 29011 cdb[0] = SCMD_READ; 29012 29013 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 29014 mode2->cdread_lba >>= 2; 29015 29016 /* set the start address */ 29017 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 29018 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 29019 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 29020 29021 /* set the transfer length */ 29022 nblocks = mode2->cdread_buflen / 2336; 29023 cdb[4] = (uchar_t)nblocks & 0xFF; 29024 29025 /* build command */ 29026 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29027 com->uscsi_cdb = (caddr_t)cdb; 29028 com->uscsi_cdblen = sizeof (cdb); 29029 com->uscsi_bufaddr = mode2->cdread_bufaddr; 29030 com->uscsi_buflen = mode2->cdread_buflen; 29031 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29032 29033 /* 29034 * Issue SCSI command with user space address for read buffer. 29035 * 29036 * This sends the command through main channel in the driver. 29037 * 29038 * Since this is accessed via an IOCTL call, we go through the 29039 * standard path, so that if the device was powered down, then 29040 * it would be 'awakened' to handle the command. 29041 */ 29042 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29043 UIO_SYSSPACE, SD_PATH_STANDARD); 29044 29045 kmem_free(com, sizeof (*com)); 29046 29047 /* Restore the device and soft state target block size */ 29048 if (sr_sector_mode(dev, restore_blksize) != 0) { 29049 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29050 "can't do switch back to mode 1\n"); 29051 /* 29052 * If sd_send_scsi_READ succeeded we still need to report 29053 * an error because we failed to reset the block size 29054 */ 29055 if (rval == 0) { 29056 rval = EIO; 29057 } 29058 } 29059 29060 done: 29061 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 29062 "sd_read_mode2: exit: un:0x%p\n", un); 29063 29064 return (rval); 29065 } 29066 29067 29068 /* 29069 * Function: sr_sector_mode() 29070 * 29071 * Description: This utility function is used by sr_read_mode2 to set the target 29072 * block size based on the user specified size. This is a legacy 29073 * implementation based upon a vendor specific mode page 29074 * 29075 * Arguments: dev - the device 'dev_t' 29076 * data - flag indicating if block size is being set to 2336 or 29077 * 512. 29078 * 29079 * Return Code: the code returned by sd_send_scsi_cmd() 29080 * EFAULT if ddi_copyxxx() fails 29081 * ENXIO if fail ddi_get_soft_state 29082 * EINVAL if data pointer is NULL 29083 */ 29084 29085 static int 29086 sr_sector_mode(dev_t dev, uint32_t blksize) 29087 { 29088 struct sd_lun *un; 29089 uchar_t *sense; 29090 uchar_t *select; 29091 int rval; 29092 29093 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29094 (un->un_state == SD_STATE_OFFLINE)) { 29095 return (ENXIO); 29096 } 29097 29098 sense = kmem_zalloc(20, KM_SLEEP); 29099 29100 /* Note: This is a vendor specific mode page (0x81) */ 29101 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 29102 SD_PATH_STANDARD)) != 0) { 29103 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29104 "sr_sector_mode: Mode Sense failed\n"); 29105 kmem_free(sense, 20); 29106 return (rval); 29107 } 29108 select = kmem_zalloc(20, KM_SLEEP); 29109 select[3] = 0x08; 29110 select[10] = ((blksize >> 8) & 0xff); 29111 select[11] = (blksize & 0xff); 29112 select[12] = 0x01; 29113 select[13] = 0x06; 29114 select[14] = sense[14]; 29115 select[15] = sense[15]; 29116 if (blksize == SD_MODE2_BLKSIZE) { 29117 select[14] |= 0x01; 29118 } 29119 29120 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 29121 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 29122 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29123 "sr_sector_mode: Mode Select failed\n"); 29124 } else { 29125 /* 29126 * Only update the softstate block size if we successfully 29127 * changed the device block mode. 29128 */ 29129 mutex_enter(SD_MUTEX(un)); 29130 sd_update_block_info(un, blksize, 0); 29131 mutex_exit(SD_MUTEX(un)); 29132 } 29133 kmem_free(sense, 20); 29134 kmem_free(select, 20); 29135 return (rval); 29136 } 29137 29138 29139 /* 29140 * Function: sr_read_cdda() 29141 * 29142 * Description: This routine is the driver entry point for handling CD-ROM 29143 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 29144 * the target supports CDDA these requests are handled via a vendor 29145 * specific command (0xD8) If the target does not support CDDA 29146 * these requests are handled via the READ CD command (0xBE). 29147 * 29148 * Arguments: dev - the device 'dev_t' 29149 * data - pointer to user provided CD-DA structure specifying 29150 * the track starting address, transfer length, and 29151 * subcode options. 29152 * flag - this argument is a pass through to ddi_copyxxx() 29153 * directly from the mode argument of ioctl(). 29154 * 29155 * Return Code: the code returned by sd_send_scsi_cmd() 29156 * EFAULT if ddi_copyxxx() fails 29157 * ENXIO if fail ddi_get_soft_state 29158 * EINVAL if invalid arguments are provided 29159 * ENOTTY 29160 */ 29161 29162 static int 29163 sr_read_cdda(dev_t dev, caddr_t data, int flag) 29164 { 29165 struct sd_lun *un; 29166 struct uscsi_cmd *com; 29167 struct cdrom_cdda *cdda; 29168 int rval; 29169 size_t buflen; 29170 char cdb[CDB_GROUP5]; 29171 29172 #ifdef _MULTI_DATAMODEL 29173 /* To support ILP32 applications in an LP64 world */ 29174 struct cdrom_cdda32 cdrom_cdda32; 29175 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 29176 #endif /* _MULTI_DATAMODEL */ 29177 29178 if (data == NULL) { 29179 return (EINVAL); 29180 } 29181 29182 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29183 return (ENXIO); 29184 } 29185 29186 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 29187 29188 #ifdef _MULTI_DATAMODEL 29189 switch (ddi_model_convert_from(flag & FMODELS)) { 29190 case DDI_MODEL_ILP32: 29191 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 29192 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29193 "sr_read_cdda: ddi_copyin Failed\n"); 29194 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29195 return (EFAULT); 29196 } 29197 /* Convert the ILP32 uscsi data from the application to LP64 */ 29198 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 29199 break; 29200 case DDI_MODEL_NONE: 29201 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29202 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29203 "sr_read_cdda: ddi_copyin Failed\n"); 29204 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29205 return (EFAULT); 29206 } 29207 break; 29208 } 29209 #else /* ! _MULTI_DATAMODEL */ 29210 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29211 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29212 "sr_read_cdda: ddi_copyin Failed\n"); 29213 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29214 return (EFAULT); 29215 } 29216 #endif /* _MULTI_DATAMODEL */ 29217 29218 /* 29219 * Since MMC-2 expects max 3 bytes for length, check if the 29220 * length input is greater than 3 bytes 29221 */ 29222 if ((cdda->cdda_length & 0xFF000000) != 0) { 29223 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 29224 "cdrom transfer length too large: %d (limit %d)\n", 29225 cdda->cdda_length, 0xFFFFFF); 29226 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29227 return (EINVAL); 29228 } 29229 29230 switch (cdda->cdda_subcode) { 29231 case CDROM_DA_NO_SUBCODE: 29232 buflen = CDROM_BLK_2352 * cdda->cdda_length; 29233 break; 29234 case CDROM_DA_SUBQ: 29235 buflen = CDROM_BLK_2368 * cdda->cdda_length; 29236 break; 29237 case CDROM_DA_ALL_SUBCODE: 29238 buflen = CDROM_BLK_2448 * cdda->cdda_length; 29239 break; 29240 case CDROM_DA_SUBCODE_ONLY: 29241 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 29242 break; 29243 default: 29244 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29245 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 29246 cdda->cdda_subcode); 29247 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29248 return (EINVAL); 29249 } 29250 29251 /* Build and send the command */ 29252 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29253 bzero(cdb, CDB_GROUP5); 29254 29255 if (un->un_f_cfg_cdda == TRUE) { 29256 cdb[0] = (char)SCMD_READ_CD; 29257 cdb[1] = 0x04; 29258 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29259 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29260 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29261 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29262 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29263 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29264 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 29265 cdb[9] = 0x10; 29266 switch (cdda->cdda_subcode) { 29267 case CDROM_DA_NO_SUBCODE : 29268 cdb[10] = 0x0; 29269 break; 29270 case CDROM_DA_SUBQ : 29271 cdb[10] = 0x2; 29272 break; 29273 case CDROM_DA_ALL_SUBCODE : 29274 cdb[10] = 0x1; 29275 break; 29276 case CDROM_DA_SUBCODE_ONLY : 29277 /* FALLTHROUGH */ 29278 default : 29279 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29280 kmem_free(com, sizeof (*com)); 29281 return (ENOTTY); 29282 } 29283 } else { 29284 cdb[0] = (char)SCMD_READ_CDDA; 29285 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29286 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29287 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29288 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29289 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 29290 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29291 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29292 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 29293 cdb[10] = cdda->cdda_subcode; 29294 } 29295 29296 com->uscsi_cdb = cdb; 29297 com->uscsi_cdblen = CDB_GROUP5; 29298 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 29299 com->uscsi_buflen = buflen; 29300 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29301 29302 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29303 UIO_SYSSPACE, SD_PATH_STANDARD); 29304 29305 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29306 kmem_free(com, sizeof (*com)); 29307 return (rval); 29308 } 29309 29310 29311 /* 29312 * Function: sr_read_cdxa() 29313 * 29314 * Description: This routine is the driver entry point for handling CD-ROM 29315 * ioctl requests to return CD-XA (Extended Architecture) data. 29316 * (CDROMCDXA). 29317 * 29318 * Arguments: dev - the device 'dev_t' 29319 * data - pointer to user provided CD-XA structure specifying 29320 * the data starting address, transfer length, and format 29321 * flag - this argument is a pass through to ddi_copyxxx() 29322 * directly from the mode argument of ioctl(). 29323 * 29324 * Return Code: the code returned by sd_send_scsi_cmd() 29325 * EFAULT if ddi_copyxxx() fails 29326 * ENXIO if fail ddi_get_soft_state 29327 * EINVAL if data pointer is NULL 29328 */ 29329 29330 static int 29331 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 29332 { 29333 struct sd_lun *un; 29334 struct uscsi_cmd *com; 29335 struct cdrom_cdxa *cdxa; 29336 int rval; 29337 size_t buflen; 29338 char cdb[CDB_GROUP5]; 29339 uchar_t read_flags; 29340 29341 #ifdef _MULTI_DATAMODEL 29342 /* To support ILP32 applications in an LP64 world */ 29343 struct cdrom_cdxa32 cdrom_cdxa32; 29344 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 29345 #endif /* _MULTI_DATAMODEL */ 29346 29347 if (data == NULL) { 29348 return (EINVAL); 29349 } 29350 29351 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29352 return (ENXIO); 29353 } 29354 29355 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 29356 29357 #ifdef _MULTI_DATAMODEL 29358 switch (ddi_model_convert_from(flag & FMODELS)) { 29359 case DDI_MODEL_ILP32: 29360 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 29361 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29362 return (EFAULT); 29363 } 29364 /* 29365 * Convert the ILP32 uscsi data from the 29366 * application to LP64 for internal use. 29367 */ 29368 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 29369 break; 29370 case DDI_MODEL_NONE: 29371 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29372 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29373 return (EFAULT); 29374 } 29375 break; 29376 } 29377 #else /* ! _MULTI_DATAMODEL */ 29378 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29379 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29380 return (EFAULT); 29381 } 29382 #endif /* _MULTI_DATAMODEL */ 29383 29384 /* 29385 * Since MMC-2 expects max 3 bytes for length, check if the 29386 * length input is greater than 3 bytes 29387 */ 29388 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 29389 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 29390 "cdrom transfer length too large: %d (limit %d)\n", 29391 cdxa->cdxa_length, 0xFFFFFF); 29392 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29393 return (EINVAL); 29394 } 29395 29396 switch (cdxa->cdxa_format) { 29397 case CDROM_XA_DATA: 29398 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 29399 read_flags = 0x10; 29400 break; 29401 case CDROM_XA_SECTOR_DATA: 29402 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 29403 read_flags = 0xf8; 29404 break; 29405 case CDROM_XA_DATA_W_ERROR: 29406 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 29407 read_flags = 0xfc; 29408 break; 29409 default: 29410 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29411 "sr_read_cdxa: Format '0x%x' Not Supported\n", 29412 cdxa->cdxa_format); 29413 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29414 return (EINVAL); 29415 } 29416 29417 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29418 bzero(cdb, CDB_GROUP5); 29419 if (un->un_f_mmc_cap == TRUE) { 29420 cdb[0] = (char)SCMD_READ_CD; 29421 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29422 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29423 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29424 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29425 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29426 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29427 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 29428 cdb[9] = (char)read_flags; 29429 } else { 29430 /* 29431 * Note: A vendor specific command (0xDB) is being used her to 29432 * request a read of all subcodes. 29433 */ 29434 cdb[0] = (char)SCMD_READ_CDXA; 29435 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29436 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29437 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29438 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29439 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 29440 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29441 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29442 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 29443 cdb[10] = cdxa->cdxa_format; 29444 } 29445 com->uscsi_cdb = cdb; 29446 com->uscsi_cdblen = CDB_GROUP5; 29447 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 29448 com->uscsi_buflen = buflen; 29449 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29450 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29451 UIO_SYSSPACE, SD_PATH_STANDARD); 29452 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29453 kmem_free(com, sizeof (*com)); 29454 return (rval); 29455 } 29456 29457 29458 /* 29459 * Function: sr_eject() 29460 * 29461 * Description: This routine is the driver entry point for handling CD-ROM 29462 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 29463 * 29464 * Arguments: dev - the device 'dev_t' 29465 * 29466 * Return Code: the code returned by sd_send_scsi_cmd() 29467 */ 29468 29469 static int 29470 sr_eject(dev_t dev) 29471 { 29472 struct sd_lun *un; 29473 int rval; 29474 29475 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29476 (un->un_state == SD_STATE_OFFLINE)) { 29477 return (ENXIO); 29478 } 29479 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29480 SD_PATH_STANDARD)) != 0) { 29481 return (rval); 29482 } 29483 29484 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29485 SD_PATH_STANDARD); 29486 29487 if (rval == 0) { 29488 mutex_enter(SD_MUTEX(un)); 29489 sr_ejected(un); 29490 un->un_mediastate = DKIO_EJECTED; 29491 cv_broadcast(&un->un_state_cv); 29492 mutex_exit(SD_MUTEX(un)); 29493 } 29494 return (rval); 29495 } 29496 29497 29498 /* 29499 * Function: sr_ejected() 29500 * 29501 * Description: This routine updates the soft state structure to invalidate the 29502 * geometry information after the media has been ejected or a 29503 * media eject has been detected. 29504 * 29505 * Arguments: un - driver soft state (unit) structure 29506 */ 29507 29508 static void 29509 sr_ejected(struct sd_lun *un) 29510 { 29511 struct sd_errstats *stp; 29512 29513 ASSERT(un != NULL); 29514 ASSERT(mutex_owned(SD_MUTEX(un))); 29515 29516 un->un_f_blockcount_is_valid = FALSE; 29517 un->un_f_tgt_blocksize_is_valid = FALSE; 29518 un->un_f_geometry_is_valid = FALSE; 29519 29520 if (un->un_errstats != NULL) { 29521 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29522 stp->sd_capacity.value.ui64 = 0; 29523 } 29524 } 29525 29526 29527 /* 29528 * Function: sr_check_wp() 29529 * 29530 * Description: This routine checks the write protection of a removable 29531 * media disk and hotpluggable devices via the write protect bit of 29532 * the Mode Page Header device specific field. Some devices choke 29533 * on unsupported mode page. In order to workaround this issue, 29534 * this routine has been implemented to use 0x3f mode page(request 29535 * for all pages) for all device types. 29536 * 29537 * Arguments: dev - the device 'dev_t' 29538 * 29539 * Return Code: int indicating if the device is write protected (1) or not (0) 29540 * 29541 * Context: Kernel thread. 29542 * 29543 */ 29544 29545 static int 29546 sr_check_wp(dev_t dev) 29547 { 29548 struct sd_lun *un; 29549 uchar_t device_specific; 29550 uchar_t *sense; 29551 int hdrlen; 29552 int rval = FALSE; 29553 29554 /* 29555 * Note: The return codes for this routine should be reworked to 29556 * properly handle the case of a NULL softstate. 29557 */ 29558 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29559 return (FALSE); 29560 } 29561 29562 if (un->un_f_cfg_is_atapi == TRUE) { 29563 /* 29564 * The mode page contents are not required; set the allocation 29565 * length for the mode page header only 29566 */ 29567 hdrlen = MODE_HEADER_LENGTH_GRP2; 29568 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29569 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29570 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29571 goto err_exit; 29572 device_specific = 29573 ((struct mode_header_grp2 *)sense)->device_specific; 29574 } else { 29575 hdrlen = MODE_HEADER_LENGTH; 29576 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29577 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29578 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29579 goto err_exit; 29580 device_specific = 29581 ((struct mode_header *)sense)->device_specific; 29582 } 29583 29584 /* 29585 * Write protect mode sense failed; not all disks 29586 * understand this query. Return FALSE assuming that 29587 * these devices are not writable. 29588 */ 29589 if (device_specific & WRITE_PROTECT) { 29590 rval = TRUE; 29591 } 29592 29593 err_exit: 29594 kmem_free(sense, hdrlen); 29595 return (rval); 29596 } 29597 29598 /* 29599 * Function: sr_volume_ctrl() 29600 * 29601 * Description: This routine is the driver entry point for handling CD-ROM 29602 * audio output volume ioctl requests. (CDROMVOLCTRL) 29603 * 29604 * Arguments: dev - the device 'dev_t' 29605 * data - pointer to user audio volume control structure 29606 * flag - this argument is a pass through to ddi_copyxxx() 29607 * directly from the mode argument of ioctl(). 29608 * 29609 * Return Code: the code returned by sd_send_scsi_cmd() 29610 * EFAULT if ddi_copyxxx() fails 29611 * ENXIO if fail ddi_get_soft_state 29612 * EINVAL if data pointer is NULL 29613 * 29614 */ 29615 29616 static int 29617 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29618 { 29619 struct sd_lun *un; 29620 struct cdrom_volctrl volume; 29621 struct cdrom_volctrl *vol = &volume; 29622 uchar_t *sense_page; 29623 uchar_t *select_page; 29624 uchar_t *sense; 29625 uchar_t *select; 29626 int sense_buflen; 29627 int select_buflen; 29628 int rval; 29629 29630 if (data == NULL) { 29631 return (EINVAL); 29632 } 29633 29634 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29635 (un->un_state == SD_STATE_OFFLINE)) { 29636 return (ENXIO); 29637 } 29638 29639 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29640 return (EFAULT); 29641 } 29642 29643 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29644 struct mode_header_grp2 *sense_mhp; 29645 struct mode_header_grp2 *select_mhp; 29646 int bd_len; 29647 29648 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29649 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29650 MODEPAGE_AUDIO_CTRL_LEN; 29651 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29652 select = kmem_zalloc(select_buflen, KM_SLEEP); 29653 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29654 sense_buflen, MODEPAGE_AUDIO_CTRL, 29655 SD_PATH_STANDARD)) != 0) { 29656 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29657 "sr_volume_ctrl: Mode Sense Failed\n"); 29658 kmem_free(sense, sense_buflen); 29659 kmem_free(select, select_buflen); 29660 return (rval); 29661 } 29662 sense_mhp = (struct mode_header_grp2 *)sense; 29663 select_mhp = (struct mode_header_grp2 *)select; 29664 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29665 sense_mhp->bdesc_length_lo; 29666 if (bd_len > MODE_BLK_DESC_LENGTH) { 29667 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29668 "sr_volume_ctrl: Mode Sense returned invalid " 29669 "block descriptor length\n"); 29670 kmem_free(sense, sense_buflen); 29671 kmem_free(select, select_buflen); 29672 return (EIO); 29673 } 29674 sense_page = (uchar_t *) 29675 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29676 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29677 select_mhp->length_msb = 0; 29678 select_mhp->length_lsb = 0; 29679 select_mhp->bdesc_length_hi = 0; 29680 select_mhp->bdesc_length_lo = 0; 29681 } else { 29682 struct mode_header *sense_mhp, *select_mhp; 29683 29684 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29685 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29686 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29687 select = kmem_zalloc(select_buflen, KM_SLEEP); 29688 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29689 sense_buflen, MODEPAGE_AUDIO_CTRL, 29690 SD_PATH_STANDARD)) != 0) { 29691 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29692 "sr_volume_ctrl: Mode Sense Failed\n"); 29693 kmem_free(sense, sense_buflen); 29694 kmem_free(select, select_buflen); 29695 return (rval); 29696 } 29697 sense_mhp = (struct mode_header *)sense; 29698 select_mhp = (struct mode_header *)select; 29699 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29700 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29701 "sr_volume_ctrl: Mode Sense returned invalid " 29702 "block descriptor length\n"); 29703 kmem_free(sense, sense_buflen); 29704 kmem_free(select, select_buflen); 29705 return (EIO); 29706 } 29707 sense_page = (uchar_t *) 29708 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29709 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29710 select_mhp->length = 0; 29711 select_mhp->bdesc_length = 0; 29712 } 29713 /* 29714 * Note: An audio control data structure could be created and overlayed 29715 * on the following in place of the array indexing method implemented. 29716 */ 29717 29718 /* Build the select data for the user volume data */ 29719 select_page[0] = MODEPAGE_AUDIO_CTRL; 29720 select_page[1] = 0xE; 29721 /* Set the immediate bit */ 29722 select_page[2] = 0x04; 29723 /* Zero out reserved fields */ 29724 select_page[3] = 0x00; 29725 select_page[4] = 0x00; 29726 /* Return sense data for fields not to be modified */ 29727 select_page[5] = sense_page[5]; 29728 select_page[6] = sense_page[6]; 29729 select_page[7] = sense_page[7]; 29730 /* Set the user specified volume levels for channel 0 and 1 */ 29731 select_page[8] = 0x01; 29732 select_page[9] = vol->channel0; 29733 select_page[10] = 0x02; 29734 select_page[11] = vol->channel1; 29735 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29736 select_page[12] = sense_page[12]; 29737 select_page[13] = sense_page[13]; 29738 select_page[14] = sense_page[14]; 29739 select_page[15] = sense_page[15]; 29740 29741 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29742 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29743 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29744 } else { 29745 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29746 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29747 } 29748 29749 kmem_free(sense, sense_buflen); 29750 kmem_free(select, select_buflen); 29751 return (rval); 29752 } 29753 29754 29755 /* 29756 * Function: sr_read_sony_session_offset() 29757 * 29758 * Description: This routine is the driver entry point for handling CD-ROM 29759 * ioctl requests for session offset information. (CDROMREADOFFSET) 29760 * The address of the first track in the last session of a 29761 * multi-session CD-ROM is returned 29762 * 29763 * Note: This routine uses a vendor specific key value in the 29764 * command control field without implementing any vendor check here 29765 * or in the ioctl routine. 29766 * 29767 * Arguments: dev - the device 'dev_t' 29768 * data - pointer to an int to hold the requested address 29769 * flag - this argument is a pass through to ddi_copyxxx() 29770 * directly from the mode argument of ioctl(). 29771 * 29772 * Return Code: the code returned by sd_send_scsi_cmd() 29773 * EFAULT if ddi_copyxxx() fails 29774 * ENXIO if fail ddi_get_soft_state 29775 * EINVAL if data pointer is NULL 29776 */ 29777 29778 static int 29779 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29780 { 29781 struct sd_lun *un; 29782 struct uscsi_cmd *com; 29783 caddr_t buffer; 29784 char cdb[CDB_GROUP1]; 29785 int session_offset = 0; 29786 int rval; 29787 29788 if (data == NULL) { 29789 return (EINVAL); 29790 } 29791 29792 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29793 (un->un_state == SD_STATE_OFFLINE)) { 29794 return (ENXIO); 29795 } 29796 29797 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29798 bzero(cdb, CDB_GROUP1); 29799 cdb[0] = SCMD_READ_TOC; 29800 /* 29801 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29802 * (4 byte TOC response header + 8 byte response data) 29803 */ 29804 cdb[8] = SONY_SESSION_OFFSET_LEN; 29805 /* Byte 9 is the control byte. A vendor specific value is used */ 29806 cdb[9] = SONY_SESSION_OFFSET_KEY; 29807 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29808 com->uscsi_cdb = cdb; 29809 com->uscsi_cdblen = CDB_GROUP1; 29810 com->uscsi_bufaddr = buffer; 29811 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29812 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29813 29814 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29815 UIO_SYSSPACE, SD_PATH_STANDARD); 29816 if (rval != 0) { 29817 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29818 kmem_free(com, sizeof (*com)); 29819 return (rval); 29820 } 29821 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29822 session_offset = 29823 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29824 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29825 /* 29826 * Offset returned offset in current lbasize block's. Convert to 29827 * 2k block's to return to the user 29828 */ 29829 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29830 session_offset >>= 2; 29831 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29832 session_offset >>= 1; 29833 } 29834 } 29835 29836 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29837 rval = EFAULT; 29838 } 29839 29840 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29841 kmem_free(com, sizeof (*com)); 29842 return (rval); 29843 } 29844 29845 29846 /* 29847 * Function: sd_wm_cache_constructor() 29848 * 29849 * Description: Cache Constructor for the wmap cache for the read/modify/write 29850 * devices. 29851 * 29852 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29853 * un - sd_lun structure for the device. 29854 * flag - the km flags passed to constructor 29855 * 29856 * Return Code: 0 on success. 29857 * -1 on failure. 29858 */ 29859 29860 /*ARGSUSED*/ 29861 static int 29862 sd_wm_cache_constructor(void *wm, void *un, int flags) 29863 { 29864 bzero(wm, sizeof (struct sd_w_map)); 29865 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29866 return (0); 29867 } 29868 29869 29870 /* 29871 * Function: sd_wm_cache_destructor() 29872 * 29873 * Description: Cache destructor for the wmap cache for the read/modify/write 29874 * devices. 29875 * 29876 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29877 * un - sd_lun structure for the device. 29878 */ 29879 /*ARGSUSED*/ 29880 static void 29881 sd_wm_cache_destructor(void *wm, void *un) 29882 { 29883 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29884 } 29885 29886 29887 /* 29888 * Function: sd_range_lock() 29889 * 29890 * Description: Lock the range of blocks specified as parameter to ensure 29891 * that read, modify write is atomic and no other i/o writes 29892 * to the same location. The range is specified in terms 29893 * of start and end blocks. Block numbers are the actual 29894 * media block numbers and not system. 29895 * 29896 * Arguments: un - sd_lun structure for the device. 29897 * startb - The starting block number 29898 * endb - The end block number 29899 * typ - type of i/o - simple/read_modify_write 29900 * 29901 * Return Code: wm - pointer to the wmap structure. 29902 * 29903 * Context: This routine can sleep. 29904 */ 29905 29906 static struct sd_w_map * 29907 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29908 { 29909 struct sd_w_map *wmp = NULL; 29910 struct sd_w_map *sl_wmp = NULL; 29911 struct sd_w_map *tmp_wmp; 29912 wm_state state = SD_WM_CHK_LIST; 29913 29914 29915 ASSERT(un != NULL); 29916 ASSERT(!mutex_owned(SD_MUTEX(un))); 29917 29918 mutex_enter(SD_MUTEX(un)); 29919 29920 while (state != SD_WM_DONE) { 29921 29922 switch (state) { 29923 case SD_WM_CHK_LIST: 29924 /* 29925 * This is the starting state. Check the wmap list 29926 * to see if the range is currently available. 29927 */ 29928 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29929 /* 29930 * If this is a simple write and no rmw 29931 * i/o is pending then try to lock the 29932 * range as the range should be available. 29933 */ 29934 state = SD_WM_LOCK_RANGE; 29935 } else { 29936 tmp_wmp = sd_get_range(un, startb, endb); 29937 if (tmp_wmp != NULL) { 29938 if ((wmp != NULL) && ONLIST(un, wmp)) { 29939 /* 29940 * Should not keep onlist wmps 29941 * while waiting this macro 29942 * will also do wmp = NULL; 29943 */ 29944 FREE_ONLIST_WMAP(un, wmp); 29945 } 29946 /* 29947 * sl_wmp is the wmap on which wait 29948 * is done, since the tmp_wmp points 29949 * to the inuse wmap, set sl_wmp to 29950 * tmp_wmp and change the state to sleep 29951 */ 29952 sl_wmp = tmp_wmp; 29953 state = SD_WM_WAIT_MAP; 29954 } else { 29955 state = SD_WM_LOCK_RANGE; 29956 } 29957 29958 } 29959 break; 29960 29961 case SD_WM_LOCK_RANGE: 29962 ASSERT(un->un_wm_cache); 29963 /* 29964 * The range need to be locked, try to get a wmap. 29965 * First attempt it with NO_SLEEP, want to avoid a sleep 29966 * if possible as we will have to release the sd mutex 29967 * if we have to sleep. 29968 */ 29969 if (wmp == NULL) 29970 wmp = kmem_cache_alloc(un->un_wm_cache, 29971 KM_NOSLEEP); 29972 if (wmp == NULL) { 29973 mutex_exit(SD_MUTEX(un)); 29974 _NOTE(DATA_READABLE_WITHOUT_LOCK 29975 (sd_lun::un_wm_cache)) 29976 wmp = kmem_cache_alloc(un->un_wm_cache, 29977 KM_SLEEP); 29978 mutex_enter(SD_MUTEX(un)); 29979 /* 29980 * we released the mutex so recheck and go to 29981 * check list state. 29982 */ 29983 state = SD_WM_CHK_LIST; 29984 } else { 29985 /* 29986 * We exit out of state machine since we 29987 * have the wmap. Do the housekeeping first. 29988 * place the wmap on the wmap list if it is not 29989 * on it already and then set the state to done. 29990 */ 29991 wmp->wm_start = startb; 29992 wmp->wm_end = endb; 29993 wmp->wm_flags = typ | SD_WM_BUSY; 29994 if (typ & SD_WTYPE_RMW) { 29995 un->un_rmw_count++; 29996 } 29997 /* 29998 * If not already on the list then link 29999 */ 30000 if (!ONLIST(un, wmp)) { 30001 wmp->wm_next = un->un_wm; 30002 wmp->wm_prev = NULL; 30003 if (wmp->wm_next) 30004 wmp->wm_next->wm_prev = wmp; 30005 un->un_wm = wmp; 30006 } 30007 state = SD_WM_DONE; 30008 } 30009 break; 30010 30011 case SD_WM_WAIT_MAP: 30012 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 30013 /* 30014 * Wait is done on sl_wmp, which is set in the 30015 * check_list state. 30016 */ 30017 sl_wmp->wm_wanted_count++; 30018 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 30019 sl_wmp->wm_wanted_count--; 30020 /* 30021 * We can reuse the memory from the completed sl_wmp 30022 * lock range for our new lock, but only if noone is 30023 * waiting for it. 30024 */ 30025 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 30026 if (sl_wmp->wm_wanted_count == 0) { 30027 if (wmp != NULL) 30028 CHK_N_FREEWMP(un, wmp); 30029 wmp = sl_wmp; 30030 } 30031 sl_wmp = NULL; 30032 /* 30033 * After waking up, need to recheck for availability of 30034 * range. 30035 */ 30036 state = SD_WM_CHK_LIST; 30037 break; 30038 30039 default: 30040 panic("sd_range_lock: " 30041 "Unknown state %d in sd_range_lock", state); 30042 /*NOTREACHED*/ 30043 } /* switch(state) */ 30044 30045 } /* while(state != SD_WM_DONE) */ 30046 30047 mutex_exit(SD_MUTEX(un)); 30048 30049 ASSERT(wmp != NULL); 30050 30051 return (wmp); 30052 } 30053 30054 30055 /* 30056 * Function: sd_get_range() 30057 * 30058 * Description: Find if there any overlapping I/O to this one 30059 * Returns the write-map of 1st such I/O, NULL otherwise. 30060 * 30061 * Arguments: un - sd_lun structure for the device. 30062 * startb - The starting block number 30063 * endb - The end block number 30064 * 30065 * Return Code: wm - pointer to the wmap structure. 30066 */ 30067 30068 static struct sd_w_map * 30069 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 30070 { 30071 struct sd_w_map *wmp; 30072 30073 ASSERT(un != NULL); 30074 30075 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 30076 if (!(wmp->wm_flags & SD_WM_BUSY)) { 30077 continue; 30078 } 30079 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 30080 break; 30081 } 30082 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 30083 break; 30084 } 30085 } 30086 30087 return (wmp); 30088 } 30089 30090 30091 /* 30092 * Function: sd_free_inlist_wmap() 30093 * 30094 * Description: Unlink and free a write map struct. 30095 * 30096 * Arguments: un - sd_lun structure for the device. 30097 * wmp - sd_w_map which needs to be unlinked. 30098 */ 30099 30100 static void 30101 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 30102 { 30103 ASSERT(un != NULL); 30104 30105 if (un->un_wm == wmp) { 30106 un->un_wm = wmp->wm_next; 30107 } else { 30108 wmp->wm_prev->wm_next = wmp->wm_next; 30109 } 30110 30111 if (wmp->wm_next) { 30112 wmp->wm_next->wm_prev = wmp->wm_prev; 30113 } 30114 30115 wmp->wm_next = wmp->wm_prev = NULL; 30116 30117 kmem_cache_free(un->un_wm_cache, wmp); 30118 } 30119 30120 30121 /* 30122 * Function: sd_range_unlock() 30123 * 30124 * Description: Unlock the range locked by wm. 30125 * Free write map if nobody else is waiting on it. 30126 * 30127 * Arguments: un - sd_lun structure for the device. 30128 * wmp - sd_w_map which needs to be unlinked. 30129 */ 30130 30131 static void 30132 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 30133 { 30134 ASSERT(un != NULL); 30135 ASSERT(wm != NULL); 30136 ASSERT(!mutex_owned(SD_MUTEX(un))); 30137 30138 mutex_enter(SD_MUTEX(un)); 30139 30140 if (wm->wm_flags & SD_WTYPE_RMW) { 30141 un->un_rmw_count--; 30142 } 30143 30144 if (wm->wm_wanted_count) { 30145 wm->wm_flags = 0; 30146 /* 30147 * Broadcast that the wmap is available now. 30148 */ 30149 cv_broadcast(&wm->wm_avail); 30150 } else { 30151 /* 30152 * If no one is waiting on the map, it should be free'ed. 30153 */ 30154 sd_free_inlist_wmap(un, wm); 30155 } 30156 30157 mutex_exit(SD_MUTEX(un)); 30158 } 30159 30160 30161 /* 30162 * Function: sd_read_modify_write_task 30163 * 30164 * Description: Called from a taskq thread to initiate the write phase of 30165 * a read-modify-write request. This is used for targets where 30166 * un->un_sys_blocksize != un->un_tgt_blocksize. 30167 * 30168 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 30169 * 30170 * Context: Called under taskq thread context. 30171 */ 30172 30173 static void 30174 sd_read_modify_write_task(void *arg) 30175 { 30176 struct sd_mapblocksize_info *bsp; 30177 struct buf *bp; 30178 struct sd_xbuf *xp; 30179 struct sd_lun *un; 30180 30181 bp = arg; /* The bp is given in arg */ 30182 ASSERT(bp != NULL); 30183 30184 /* Get the pointer to the layer-private data struct */ 30185 xp = SD_GET_XBUF(bp); 30186 ASSERT(xp != NULL); 30187 bsp = xp->xb_private; 30188 ASSERT(bsp != NULL); 30189 30190 un = SD_GET_UN(bp); 30191 ASSERT(un != NULL); 30192 ASSERT(!mutex_owned(SD_MUTEX(un))); 30193 30194 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30195 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 30196 30197 /* 30198 * This is the write phase of a read-modify-write request, called 30199 * under the context of a taskq thread in response to the completion 30200 * of the read portion of the rmw request completing under interrupt 30201 * context. The write request must be sent from here down the iostart 30202 * chain as if it were being sent from sd_mapblocksize_iostart(), so 30203 * we use the layer index saved in the layer-private data area. 30204 */ 30205 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 30206 30207 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30208 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 30209 } 30210 30211 30212 /* 30213 * Function: sddump_do_read_of_rmw() 30214 * 30215 * Description: This routine will be called from sddump, If sddump is called 30216 * with an I/O which not aligned on device blocksize boundary 30217 * then the write has to be converted to read-modify-write. 30218 * Do the read part here in order to keep sddump simple. 30219 * Note - That the sd_mutex is held across the call to this 30220 * routine. 30221 * 30222 * Arguments: un - sd_lun 30223 * blkno - block number in terms of media block size. 30224 * nblk - number of blocks. 30225 * bpp - pointer to pointer to the buf structure. On return 30226 * from this function, *bpp points to the valid buffer 30227 * to which the write has to be done. 30228 * 30229 * Return Code: 0 for success or errno-type return code 30230 */ 30231 30232 static int 30233 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 30234 struct buf **bpp) 30235 { 30236 int err; 30237 int i; 30238 int rval; 30239 struct buf *bp; 30240 struct scsi_pkt *pkt = NULL; 30241 uint32_t target_blocksize; 30242 30243 ASSERT(un != NULL); 30244 ASSERT(mutex_owned(SD_MUTEX(un))); 30245 30246 target_blocksize = un->un_tgt_blocksize; 30247 30248 mutex_exit(SD_MUTEX(un)); 30249 30250 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 30251 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 30252 if (bp == NULL) { 30253 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30254 "no resources for dumping; giving up"); 30255 err = ENOMEM; 30256 goto done; 30257 } 30258 30259 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 30260 blkno, nblk); 30261 if (rval != 0) { 30262 scsi_free_consistent_buf(bp); 30263 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30264 "no resources for dumping; giving up"); 30265 err = ENOMEM; 30266 goto done; 30267 } 30268 30269 pkt->pkt_flags |= FLAG_NOINTR; 30270 30271 err = EIO; 30272 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 30273 30274 /* 30275 * Scsi_poll returns 0 (success) if the command completes and 30276 * the status block is STATUS_GOOD. We should only check 30277 * errors if this condition is not true. Even then we should 30278 * send our own request sense packet only if we have a check 30279 * condition and auto request sense has not been performed by 30280 * the hba. 30281 */ 30282 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 30283 30284 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 30285 err = 0; 30286 break; 30287 } 30288 30289 /* 30290 * Check CMD_DEV_GONE 1st, give up if device is gone, 30291 * no need to read RQS data. 30292 */ 30293 if (pkt->pkt_reason == CMD_DEV_GONE) { 30294 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30295 "Device is gone\n"); 30296 break; 30297 } 30298 30299 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 30300 SD_INFO(SD_LOG_DUMP, un, 30301 "sddump: read failed with CHECK, try # %d\n", i); 30302 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 30303 (void) sd_send_polled_RQS(un); 30304 } 30305 30306 continue; 30307 } 30308 30309 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 30310 int reset_retval = 0; 30311 30312 SD_INFO(SD_LOG_DUMP, un, 30313 "sddump: read failed with BUSY, try # %d\n", i); 30314 30315 if (un->un_f_lun_reset_enabled == TRUE) { 30316 reset_retval = scsi_reset(SD_ADDRESS(un), 30317 RESET_LUN); 30318 } 30319 if (reset_retval == 0) { 30320 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 30321 } 30322 (void) sd_send_polled_RQS(un); 30323 30324 } else { 30325 SD_INFO(SD_LOG_DUMP, un, 30326 "sddump: read failed with 0x%x, try # %d\n", 30327 SD_GET_PKT_STATUS(pkt), i); 30328 mutex_enter(SD_MUTEX(un)); 30329 sd_reset_target(un, pkt); 30330 mutex_exit(SD_MUTEX(un)); 30331 } 30332 30333 /* 30334 * If we are not getting anywhere with lun/target resets, 30335 * let's reset the bus. 30336 */ 30337 if (i > SD_NDUMP_RETRIES/2) { 30338 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 30339 (void) sd_send_polled_RQS(un); 30340 } 30341 30342 } 30343 scsi_destroy_pkt(pkt); 30344 30345 if (err != 0) { 30346 scsi_free_consistent_buf(bp); 30347 *bpp = NULL; 30348 } else { 30349 *bpp = bp; 30350 } 30351 30352 done: 30353 mutex_enter(SD_MUTEX(un)); 30354 return (err); 30355 } 30356 30357 30358 /* 30359 * Function: sd_failfast_flushq 30360 * 30361 * Description: Take all bp's on the wait queue that have B_FAILFAST set 30362 * in b_flags and move them onto the failfast queue, then kick 30363 * off a thread to return all bp's on the failfast queue to 30364 * their owners with an error set. 30365 * 30366 * Arguments: un - pointer to the soft state struct for the instance. 30367 * 30368 * Context: may execute in interrupt context. 30369 */ 30370 30371 static void 30372 sd_failfast_flushq(struct sd_lun *un) 30373 { 30374 struct buf *bp; 30375 struct buf *next_waitq_bp; 30376 struct buf *prev_waitq_bp = NULL; 30377 30378 ASSERT(un != NULL); 30379 ASSERT(mutex_owned(SD_MUTEX(un))); 30380 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 30381 ASSERT(un->un_failfast_bp == NULL); 30382 30383 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30384 "sd_failfast_flushq: entry: un:0x%p\n", un); 30385 30386 /* 30387 * Check if we should flush all bufs when entering failfast state, or 30388 * just those with B_FAILFAST set. 30389 */ 30390 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 30391 /* 30392 * Move *all* bp's on the wait queue to the failfast flush 30393 * queue, including those that do NOT have B_FAILFAST set. 30394 */ 30395 if (un->un_failfast_headp == NULL) { 30396 ASSERT(un->un_failfast_tailp == NULL); 30397 un->un_failfast_headp = un->un_waitq_headp; 30398 } else { 30399 ASSERT(un->un_failfast_tailp != NULL); 30400 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 30401 } 30402 30403 un->un_failfast_tailp = un->un_waitq_tailp; 30404 30405 /* update kstat for each bp moved out of the waitq */ 30406 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 30407 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30408 } 30409 30410 /* empty the waitq */ 30411 un->un_waitq_headp = un->un_waitq_tailp = NULL; 30412 30413 } else { 30414 /* 30415 * Go thru the wait queue, pick off all entries with 30416 * B_FAILFAST set, and move these onto the failfast queue. 30417 */ 30418 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 30419 /* 30420 * Save the pointer to the next bp on the wait queue, 30421 * so we get to it on the next iteration of this loop. 30422 */ 30423 next_waitq_bp = bp->av_forw; 30424 30425 /* 30426 * If this bp from the wait queue does NOT have 30427 * B_FAILFAST set, just move on to the next element 30428 * in the wait queue. Note, this is the only place 30429 * where it is correct to set prev_waitq_bp. 30430 */ 30431 if ((bp->b_flags & B_FAILFAST) == 0) { 30432 prev_waitq_bp = bp; 30433 continue; 30434 } 30435 30436 /* 30437 * Remove the bp from the wait queue. 30438 */ 30439 if (bp == un->un_waitq_headp) { 30440 /* The bp is the first element of the waitq. */ 30441 un->un_waitq_headp = next_waitq_bp; 30442 if (un->un_waitq_headp == NULL) { 30443 /* The wait queue is now empty */ 30444 un->un_waitq_tailp = NULL; 30445 } 30446 } else { 30447 /* 30448 * The bp is either somewhere in the middle 30449 * or at the end of the wait queue. 30450 */ 30451 ASSERT(un->un_waitq_headp != NULL); 30452 ASSERT(prev_waitq_bp != NULL); 30453 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 30454 == 0); 30455 if (bp == un->un_waitq_tailp) { 30456 /* bp is the last entry on the waitq. */ 30457 ASSERT(next_waitq_bp == NULL); 30458 un->un_waitq_tailp = prev_waitq_bp; 30459 } 30460 prev_waitq_bp->av_forw = next_waitq_bp; 30461 } 30462 bp->av_forw = NULL; 30463 30464 /* 30465 * update kstat since the bp is moved out of 30466 * the waitq 30467 */ 30468 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30469 30470 /* 30471 * Now put the bp onto the failfast queue. 30472 */ 30473 if (un->un_failfast_headp == NULL) { 30474 /* failfast queue is currently empty */ 30475 ASSERT(un->un_failfast_tailp == NULL); 30476 un->un_failfast_headp = 30477 un->un_failfast_tailp = bp; 30478 } else { 30479 /* Add the bp to the end of the failfast q */ 30480 ASSERT(un->un_failfast_tailp != NULL); 30481 ASSERT(un->un_failfast_tailp->b_flags & 30482 B_FAILFAST); 30483 un->un_failfast_tailp->av_forw = bp; 30484 un->un_failfast_tailp = bp; 30485 } 30486 } 30487 } 30488 30489 /* 30490 * Now return all bp's on the failfast queue to their owners. 30491 */ 30492 while ((bp = un->un_failfast_headp) != NULL) { 30493 30494 un->un_failfast_headp = bp->av_forw; 30495 if (un->un_failfast_headp == NULL) { 30496 un->un_failfast_tailp = NULL; 30497 } 30498 30499 /* 30500 * We want to return the bp with a failure error code, but 30501 * we do not want a call to sd_start_cmds() to occur here, 30502 * so use sd_return_failed_command_no_restart() instead of 30503 * sd_return_failed_command(). 30504 */ 30505 sd_return_failed_command_no_restart(un, bp, EIO); 30506 } 30507 30508 /* Flush the xbuf queues if required. */ 30509 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30510 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30511 } 30512 30513 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30514 "sd_failfast_flushq: exit: un:0x%p\n", un); 30515 } 30516 30517 30518 /* 30519 * Function: sd_failfast_flushq_callback 30520 * 30521 * Description: Return TRUE if the given bp meets the criteria for failfast 30522 * flushing. Used with ddi_xbuf_flushq(9F). 30523 * 30524 * Arguments: bp - ptr to buf struct to be examined. 30525 * 30526 * Context: Any 30527 */ 30528 30529 static int 30530 sd_failfast_flushq_callback(struct buf *bp) 30531 { 30532 /* 30533 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30534 * state is entered; OR (2) the given bp has B_FAILFAST set. 30535 */ 30536 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30537 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30538 } 30539 30540 30541 30542 #if defined(__i386) || defined(__amd64) 30543 /* 30544 * Function: sd_setup_next_xfer 30545 * 30546 * Description: Prepare next I/O operation using DMA_PARTIAL 30547 * 30548 */ 30549 30550 static int 30551 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30552 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30553 { 30554 ssize_t num_blks_not_xfered; 30555 daddr_t strt_blk_num; 30556 ssize_t bytes_not_xfered; 30557 int rval; 30558 30559 ASSERT(pkt->pkt_resid == 0); 30560 30561 /* 30562 * Calculate next block number and amount to be transferred. 30563 * 30564 * How much data NOT transfered to the HBA yet. 30565 */ 30566 bytes_not_xfered = xp->xb_dma_resid; 30567 30568 /* 30569 * figure how many blocks NOT transfered to the HBA yet. 30570 */ 30571 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30572 30573 /* 30574 * set starting block number to the end of what WAS transfered. 30575 */ 30576 strt_blk_num = xp->xb_blkno + 30577 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30578 30579 /* 30580 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30581 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30582 * the disk mutex here. 30583 */ 30584 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30585 strt_blk_num, num_blks_not_xfered); 30586 30587 if (rval == 0) { 30588 30589 /* 30590 * Success. 30591 * 30592 * Adjust things if there are still more blocks to be 30593 * transfered. 30594 */ 30595 xp->xb_dma_resid = pkt->pkt_resid; 30596 pkt->pkt_resid = 0; 30597 30598 return (1); 30599 } 30600 30601 /* 30602 * There's really only one possible return value from 30603 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30604 * returns NULL. 30605 */ 30606 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30607 30608 bp->b_resid = bp->b_bcount; 30609 bp->b_flags |= B_ERROR; 30610 30611 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30612 "Error setting up next portion of DMA transfer\n"); 30613 30614 return (0); 30615 } 30616 #endif 30617 30618 /* 30619 * Function: sd_panic_for_res_conflict 30620 * 30621 * Description: Call panic with a string formated with "Reservation Conflict" 30622 * and a human readable identifier indicating the SD instance 30623 * that experienced the reservation conflict. 30624 * 30625 * Arguments: un - pointer to the soft state struct for the instance. 30626 * 30627 * Context: may execute in interrupt context. 30628 */ 30629 30630 #define SD_RESV_CONFLICT_FMT_LEN 40 30631 void 30632 sd_panic_for_res_conflict(struct sd_lun *un) 30633 { 30634 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30635 char path_str[MAXPATHLEN]; 30636 30637 (void) snprintf(panic_str, sizeof (panic_str), 30638 "Reservation Conflict\nDisk: %s", 30639 ddi_pathname(SD_DEVINFO(un), path_str)); 30640 30641 panic(panic_str); 30642 } 30643 30644 /* 30645 * Note: The following sd_faultinjection_ioctl( ) routines implement 30646 * driver support for handling fault injection for error analysis 30647 * causing faults in multiple layers of the driver. 30648 * 30649 */ 30650 30651 #ifdef SD_FAULT_INJECTION 30652 static uint_t sd_fault_injection_on = 0; 30653 30654 /* 30655 * Function: sd_faultinjection_ioctl() 30656 * 30657 * Description: This routine is the driver entry point for handling 30658 * faultinjection ioctls to inject errors into the 30659 * layer model 30660 * 30661 * Arguments: cmd - the ioctl cmd recieved 30662 * arg - the arguments from user and returns 30663 */ 30664 30665 static void 30666 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30667 30668 uint_t i; 30669 uint_t rval; 30670 30671 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30672 30673 mutex_enter(SD_MUTEX(un)); 30674 30675 switch (cmd) { 30676 case SDIOCRUN: 30677 /* Allow pushed faults to be injected */ 30678 SD_INFO(SD_LOG_SDTEST, un, 30679 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30680 30681 sd_fault_injection_on = 1; 30682 30683 SD_INFO(SD_LOG_IOERR, un, 30684 "sd_faultinjection_ioctl: run finished\n"); 30685 break; 30686 30687 case SDIOCSTART: 30688 /* Start Injection Session */ 30689 SD_INFO(SD_LOG_SDTEST, un, 30690 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30691 30692 sd_fault_injection_on = 0; 30693 un->sd_injection_mask = 0xFFFFFFFF; 30694 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30695 un->sd_fi_fifo_pkt[i] = NULL; 30696 un->sd_fi_fifo_xb[i] = NULL; 30697 un->sd_fi_fifo_un[i] = NULL; 30698 un->sd_fi_fifo_arq[i] = NULL; 30699 } 30700 un->sd_fi_fifo_start = 0; 30701 un->sd_fi_fifo_end = 0; 30702 30703 mutex_enter(&(un->un_fi_mutex)); 30704 un->sd_fi_log[0] = '\0'; 30705 un->sd_fi_buf_len = 0; 30706 mutex_exit(&(un->un_fi_mutex)); 30707 30708 SD_INFO(SD_LOG_IOERR, un, 30709 "sd_faultinjection_ioctl: start finished\n"); 30710 break; 30711 30712 case SDIOCSTOP: 30713 /* Stop Injection Session */ 30714 SD_INFO(SD_LOG_SDTEST, un, 30715 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30716 sd_fault_injection_on = 0; 30717 un->sd_injection_mask = 0x0; 30718 30719 /* Empty stray or unuseds structs from fifo */ 30720 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30721 if (un->sd_fi_fifo_pkt[i] != NULL) { 30722 kmem_free(un->sd_fi_fifo_pkt[i], 30723 sizeof (struct sd_fi_pkt)); 30724 } 30725 if (un->sd_fi_fifo_xb[i] != NULL) { 30726 kmem_free(un->sd_fi_fifo_xb[i], 30727 sizeof (struct sd_fi_xb)); 30728 } 30729 if (un->sd_fi_fifo_un[i] != NULL) { 30730 kmem_free(un->sd_fi_fifo_un[i], 30731 sizeof (struct sd_fi_un)); 30732 } 30733 if (un->sd_fi_fifo_arq[i] != NULL) { 30734 kmem_free(un->sd_fi_fifo_arq[i], 30735 sizeof (struct sd_fi_arq)); 30736 } 30737 un->sd_fi_fifo_pkt[i] = NULL; 30738 un->sd_fi_fifo_un[i] = NULL; 30739 un->sd_fi_fifo_xb[i] = NULL; 30740 un->sd_fi_fifo_arq[i] = NULL; 30741 } 30742 un->sd_fi_fifo_start = 0; 30743 un->sd_fi_fifo_end = 0; 30744 30745 SD_INFO(SD_LOG_IOERR, un, 30746 "sd_faultinjection_ioctl: stop finished\n"); 30747 break; 30748 30749 case SDIOCINSERTPKT: 30750 /* Store a packet struct to be pushed onto fifo */ 30751 SD_INFO(SD_LOG_SDTEST, un, 30752 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30753 30754 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30755 30756 sd_fault_injection_on = 0; 30757 30758 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30759 if (un->sd_fi_fifo_pkt[i] != NULL) { 30760 kmem_free(un->sd_fi_fifo_pkt[i], 30761 sizeof (struct sd_fi_pkt)); 30762 } 30763 if (arg != NULL) { 30764 un->sd_fi_fifo_pkt[i] = 30765 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30766 if (un->sd_fi_fifo_pkt[i] == NULL) { 30767 /* Alloc failed don't store anything */ 30768 break; 30769 } 30770 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30771 sizeof (struct sd_fi_pkt), 0); 30772 if (rval == -1) { 30773 kmem_free(un->sd_fi_fifo_pkt[i], 30774 sizeof (struct sd_fi_pkt)); 30775 un->sd_fi_fifo_pkt[i] = NULL; 30776 } 30777 } else { 30778 SD_INFO(SD_LOG_IOERR, un, 30779 "sd_faultinjection_ioctl: pkt null\n"); 30780 } 30781 break; 30782 30783 case SDIOCINSERTXB: 30784 /* Store a xb struct to be pushed onto fifo */ 30785 SD_INFO(SD_LOG_SDTEST, un, 30786 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30787 30788 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30789 30790 sd_fault_injection_on = 0; 30791 30792 if (un->sd_fi_fifo_xb[i] != NULL) { 30793 kmem_free(un->sd_fi_fifo_xb[i], 30794 sizeof (struct sd_fi_xb)); 30795 un->sd_fi_fifo_xb[i] = NULL; 30796 } 30797 if (arg != NULL) { 30798 un->sd_fi_fifo_xb[i] = 30799 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30800 if (un->sd_fi_fifo_xb[i] == NULL) { 30801 /* Alloc failed don't store anything */ 30802 break; 30803 } 30804 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30805 sizeof (struct sd_fi_xb), 0); 30806 30807 if (rval == -1) { 30808 kmem_free(un->sd_fi_fifo_xb[i], 30809 sizeof (struct sd_fi_xb)); 30810 un->sd_fi_fifo_xb[i] = NULL; 30811 } 30812 } else { 30813 SD_INFO(SD_LOG_IOERR, un, 30814 "sd_faultinjection_ioctl: xb null\n"); 30815 } 30816 break; 30817 30818 case SDIOCINSERTUN: 30819 /* Store a un struct to be pushed onto fifo */ 30820 SD_INFO(SD_LOG_SDTEST, un, 30821 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30822 30823 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30824 30825 sd_fault_injection_on = 0; 30826 30827 if (un->sd_fi_fifo_un[i] != NULL) { 30828 kmem_free(un->sd_fi_fifo_un[i], 30829 sizeof (struct sd_fi_un)); 30830 un->sd_fi_fifo_un[i] = NULL; 30831 } 30832 if (arg != NULL) { 30833 un->sd_fi_fifo_un[i] = 30834 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30835 if (un->sd_fi_fifo_un[i] == NULL) { 30836 /* Alloc failed don't store anything */ 30837 break; 30838 } 30839 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30840 sizeof (struct sd_fi_un), 0); 30841 if (rval == -1) { 30842 kmem_free(un->sd_fi_fifo_un[i], 30843 sizeof (struct sd_fi_un)); 30844 un->sd_fi_fifo_un[i] = NULL; 30845 } 30846 30847 } else { 30848 SD_INFO(SD_LOG_IOERR, un, 30849 "sd_faultinjection_ioctl: un null\n"); 30850 } 30851 30852 break; 30853 30854 case SDIOCINSERTARQ: 30855 /* Store a arq struct to be pushed onto fifo */ 30856 SD_INFO(SD_LOG_SDTEST, un, 30857 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30858 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30859 30860 sd_fault_injection_on = 0; 30861 30862 if (un->sd_fi_fifo_arq[i] != NULL) { 30863 kmem_free(un->sd_fi_fifo_arq[i], 30864 sizeof (struct sd_fi_arq)); 30865 un->sd_fi_fifo_arq[i] = NULL; 30866 } 30867 if (arg != NULL) { 30868 un->sd_fi_fifo_arq[i] = 30869 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30870 if (un->sd_fi_fifo_arq[i] == NULL) { 30871 /* Alloc failed don't store anything */ 30872 break; 30873 } 30874 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30875 sizeof (struct sd_fi_arq), 0); 30876 if (rval == -1) { 30877 kmem_free(un->sd_fi_fifo_arq[i], 30878 sizeof (struct sd_fi_arq)); 30879 un->sd_fi_fifo_arq[i] = NULL; 30880 } 30881 30882 } else { 30883 SD_INFO(SD_LOG_IOERR, un, 30884 "sd_faultinjection_ioctl: arq null\n"); 30885 } 30886 30887 break; 30888 30889 case SDIOCPUSH: 30890 /* Push stored xb, pkt, un, and arq onto fifo */ 30891 sd_fault_injection_on = 0; 30892 30893 if (arg != NULL) { 30894 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30895 if (rval != -1 && 30896 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30897 un->sd_fi_fifo_end += i; 30898 } 30899 } else { 30900 SD_INFO(SD_LOG_IOERR, un, 30901 "sd_faultinjection_ioctl: push arg null\n"); 30902 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30903 un->sd_fi_fifo_end++; 30904 } 30905 } 30906 SD_INFO(SD_LOG_IOERR, un, 30907 "sd_faultinjection_ioctl: push to end=%d\n", 30908 un->sd_fi_fifo_end); 30909 break; 30910 30911 case SDIOCRETRIEVE: 30912 /* Return buffer of log from Injection session */ 30913 SD_INFO(SD_LOG_SDTEST, un, 30914 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30915 30916 sd_fault_injection_on = 0; 30917 30918 mutex_enter(&(un->un_fi_mutex)); 30919 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30920 un->sd_fi_buf_len+1, 0); 30921 mutex_exit(&(un->un_fi_mutex)); 30922 30923 if (rval == -1) { 30924 /* 30925 * arg is possibly invalid setting 30926 * it to NULL for return 30927 */ 30928 arg = NULL; 30929 } 30930 break; 30931 } 30932 30933 mutex_exit(SD_MUTEX(un)); 30934 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30935 " exit\n"); 30936 } 30937 30938 30939 /* 30940 * Function: sd_injection_log() 30941 * 30942 * Description: This routine adds buff to the already existing injection log 30943 * for retrieval via faultinjection_ioctl for use in fault 30944 * detection and recovery 30945 * 30946 * Arguments: buf - the string to add to the log 30947 */ 30948 30949 static void 30950 sd_injection_log(char *buf, struct sd_lun *un) 30951 { 30952 uint_t len; 30953 30954 ASSERT(un != NULL); 30955 ASSERT(buf != NULL); 30956 30957 mutex_enter(&(un->un_fi_mutex)); 30958 30959 len = min(strlen(buf), 255); 30960 /* Add logged value to Injection log to be returned later */ 30961 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30962 uint_t offset = strlen((char *)un->sd_fi_log); 30963 char *destp = (char *)un->sd_fi_log + offset; 30964 int i; 30965 for (i = 0; i < len; i++) { 30966 *destp++ = *buf++; 30967 } 30968 un->sd_fi_buf_len += len; 30969 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30970 } 30971 30972 mutex_exit(&(un->un_fi_mutex)); 30973 } 30974 30975 30976 /* 30977 * Function: sd_faultinjection() 30978 * 30979 * Description: This routine takes the pkt and changes its 30980 * content based on error injection scenerio. 30981 * 30982 * Arguments: pktp - packet to be changed 30983 */ 30984 30985 static void 30986 sd_faultinjection(struct scsi_pkt *pktp) 30987 { 30988 uint_t i; 30989 struct sd_fi_pkt *fi_pkt; 30990 struct sd_fi_xb *fi_xb; 30991 struct sd_fi_un *fi_un; 30992 struct sd_fi_arq *fi_arq; 30993 struct buf *bp; 30994 struct sd_xbuf *xb; 30995 struct sd_lun *un; 30996 30997 ASSERT(pktp != NULL); 30998 30999 /* pull bp xb and un from pktp */ 31000 bp = (struct buf *)pktp->pkt_private; 31001 xb = SD_GET_XBUF(bp); 31002 un = SD_GET_UN(bp); 31003 31004 ASSERT(un != NULL); 31005 31006 mutex_enter(SD_MUTEX(un)); 31007 31008 SD_TRACE(SD_LOG_SDTEST, un, 31009 "sd_faultinjection: entry Injection from sdintr\n"); 31010 31011 /* if injection is off return */ 31012 if (sd_fault_injection_on == 0 || 31013 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 31014 mutex_exit(SD_MUTEX(un)); 31015 return; 31016 } 31017 31018 31019 /* take next set off fifo */ 31020 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 31021 31022 fi_pkt = un->sd_fi_fifo_pkt[i]; 31023 fi_xb = un->sd_fi_fifo_xb[i]; 31024 fi_un = un->sd_fi_fifo_un[i]; 31025 fi_arq = un->sd_fi_fifo_arq[i]; 31026 31027 31028 /* set variables accordingly */ 31029 /* set pkt if it was on fifo */ 31030 if (fi_pkt != NULL) { 31031 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 31032 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 31033 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 31034 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 31035 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 31036 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 31037 31038 } 31039 31040 /* set xb if it was on fifo */ 31041 if (fi_xb != NULL) { 31042 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 31043 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 31044 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 31045 SD_CONDSET(xb, xb, xb_victim_retry_count, 31046 "xb_victim_retry_count"); 31047 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 31048 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 31049 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 31050 31051 /* copy in block data from sense */ 31052 if (fi_xb->xb_sense_data[0] != -1) { 31053 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 31054 SENSE_LENGTH); 31055 } 31056 31057 /* copy in extended sense codes */ 31058 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 31059 "es_code"); 31060 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 31061 "es_key"); 31062 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 31063 "es_add_code"); 31064 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 31065 es_qual_code, "es_qual_code"); 31066 } 31067 31068 /* set un if it was on fifo */ 31069 if (fi_un != NULL) { 31070 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 31071 SD_CONDSET(un, un, un_ctype, "un_ctype"); 31072 SD_CONDSET(un, un, un_reset_retry_count, 31073 "un_reset_retry_count"); 31074 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 31075 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 31076 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 31077 SD_CONDSET(un, un, un_f_geometry_is_valid, 31078 "un_f_geometry_is_valid"); 31079 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 31080 "un_f_allow_bus_device_reset"); 31081 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 31082 31083 } 31084 31085 /* copy in auto request sense if it was on fifo */ 31086 if (fi_arq != NULL) { 31087 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 31088 } 31089 31090 /* free structs */ 31091 if (un->sd_fi_fifo_pkt[i] != NULL) { 31092 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 31093 } 31094 if (un->sd_fi_fifo_xb[i] != NULL) { 31095 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 31096 } 31097 if (un->sd_fi_fifo_un[i] != NULL) { 31098 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 31099 } 31100 if (un->sd_fi_fifo_arq[i] != NULL) { 31101 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 31102 } 31103 31104 /* 31105 * kmem_free does not gurantee to set to NULL 31106 * since we uses these to determine if we set 31107 * values or not lets confirm they are always 31108 * NULL after free 31109 */ 31110 un->sd_fi_fifo_pkt[i] = NULL; 31111 un->sd_fi_fifo_un[i] = NULL; 31112 un->sd_fi_fifo_xb[i] = NULL; 31113 un->sd_fi_fifo_arq[i] = NULL; 31114 31115 un->sd_fi_fifo_start++; 31116 31117 mutex_exit(SD_MUTEX(un)); 31118 31119 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 31120 } 31121 31122 #endif /* SD_FAULT_INJECTION */ 31123 31124 /* 31125 * This routine is invoked in sd_unit_attach(). Before calling it, the 31126 * properties in conf file should be processed already, and "hotpluggable" 31127 * property was processed also. 31128 * 31129 * The sd driver distinguishes 3 different type of devices: removable media, 31130 * non-removable media, and hotpluggable. Below the differences are defined: 31131 * 31132 * 1. Device ID 31133 * 31134 * The device ID of a device is used to identify this device. Refer to 31135 * ddi_devid_register(9F). 31136 * 31137 * For a non-removable media disk device which can provide 0x80 or 0x83 31138 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 31139 * device ID is created to identify this device. For other non-removable 31140 * media devices, a default device ID is created only if this device has 31141 * at least 2 alter cylinders. Otherwise, this device has no devid. 31142 * 31143 * ------------------------------------------------------- 31144 * removable media hotpluggable | Can Have Device ID 31145 * ------------------------------------------------------- 31146 * false false | Yes 31147 * false true | Yes 31148 * true x | No 31149 * ------------------------------------------------------ 31150 * 31151 * 31152 * 2. SCSI group 4 commands 31153 * 31154 * In SCSI specs, only some commands in group 4 command set can use 31155 * 8-byte addresses that can be used to access >2TB storage spaces. 31156 * Other commands have no such capability. Without supporting group4, 31157 * it is impossible to make full use of storage spaces of a disk with 31158 * capacity larger than 2TB. 31159 * 31160 * ----------------------------------------------- 31161 * removable media hotpluggable LP64 | Group 31162 * ----------------------------------------------- 31163 * false false false | 1 31164 * false false true | 4 31165 * false true false | 1 31166 * false true true | 4 31167 * true x x | 5 31168 * ----------------------------------------------- 31169 * 31170 * 31171 * 3. Check for VTOC Label 31172 * 31173 * If a direct-access disk has no EFI label, sd will check if it has a 31174 * valid VTOC label. Now, sd also does that check for removable media 31175 * and hotpluggable devices. 31176 * 31177 * -------------------------------------------------------------- 31178 * Direct-Access removable media hotpluggable | Check Label 31179 * ------------------------------------------------------------- 31180 * false false false | No 31181 * false false true | No 31182 * false true false | Yes 31183 * false true true | Yes 31184 * true x x | Yes 31185 * -------------------------------------------------------------- 31186 * 31187 * 31188 * 4. Building default VTOC label 31189 * 31190 * As section 3 says, sd checks if some kinds of devices have VTOC label. 31191 * If those devices have no valid VTOC label, sd(7d) will attempt to 31192 * create default VTOC for them. Currently sd creates default VTOC label 31193 * for all devices on x86 platform (VTOC_16), but only for removable 31194 * media devices on SPARC (VTOC_8). 31195 * 31196 * ----------------------------------------------------------- 31197 * removable media hotpluggable platform | Default Label 31198 * ----------------------------------------------------------- 31199 * false false sparc | No 31200 * false true x86 | Yes 31201 * false true sparc | Yes 31202 * true x x | Yes 31203 * ---------------------------------------------------------- 31204 * 31205 * 31206 * 5. Supported blocksizes of target devices 31207 * 31208 * Sd supports non-512-byte blocksize for removable media devices only. 31209 * For other devices, only 512-byte blocksize is supported. This may be 31210 * changed in near future because some RAID devices require non-512-byte 31211 * blocksize 31212 * 31213 * ----------------------------------------------------------- 31214 * removable media hotpluggable | non-512-byte blocksize 31215 * ----------------------------------------------------------- 31216 * false false | No 31217 * false true | No 31218 * true x | Yes 31219 * ----------------------------------------------------------- 31220 * 31221 * 31222 * 6. Automatic mount & unmount 31223 * 31224 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 31225 * if a device is removable media device. It return 1 for removable media 31226 * devices, and 0 for others. 31227 * 31228 * The automatic mounting subsystem should distinguish between the types 31229 * of devices and apply automounting policies to each. 31230 * 31231 * 31232 * 7. fdisk partition management 31233 * 31234 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 31235 * just supports fdisk partitions on x86 platform. On sparc platform, sd 31236 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 31237 * fdisk partitions on both x86 and SPARC platform. 31238 * 31239 * ----------------------------------------------------------- 31240 * platform removable media USB/1394 | fdisk supported 31241 * ----------------------------------------------------------- 31242 * x86 X X | true 31243 * ------------------------------------------------------------ 31244 * sparc X X | false 31245 * ------------------------------------------------------------ 31246 * 31247 * 31248 * 8. MBOOT/MBR 31249 * 31250 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 31251 * read/write mboot for removable media devices on sparc platform. 31252 * 31253 * ----------------------------------------------------------- 31254 * platform removable media USB/1394 | mboot supported 31255 * ----------------------------------------------------------- 31256 * x86 X X | true 31257 * ------------------------------------------------------------ 31258 * sparc false false | false 31259 * sparc false true | true 31260 * sparc true false | true 31261 * sparc true true | true 31262 * ------------------------------------------------------------ 31263 * 31264 * 31265 * 9. error handling during opening device 31266 * 31267 * If failed to open a disk device, an errno is returned. For some kinds 31268 * of errors, different errno is returned depending on if this device is 31269 * a removable media device. This brings USB/1394 hard disks in line with 31270 * expected hard disk behavior. It is not expected that this breaks any 31271 * application. 31272 * 31273 * ------------------------------------------------------ 31274 * removable media hotpluggable | errno 31275 * ------------------------------------------------------ 31276 * false false | EIO 31277 * false true | EIO 31278 * true x | ENXIO 31279 * ------------------------------------------------------ 31280 * 31281 * 31282 * 11. ioctls: DKIOCEJECT, CDROMEJECT 31283 * 31284 * These IOCTLs are applicable only to removable media devices. 31285 * 31286 * ----------------------------------------------------------- 31287 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 31288 * ----------------------------------------------------------- 31289 * false false | No 31290 * false true | No 31291 * true x | Yes 31292 * ----------------------------------------------------------- 31293 * 31294 * 31295 * 12. Kstats for partitions 31296 * 31297 * sd creates partition kstat for non-removable media devices. USB and 31298 * Firewire hard disks now have partition kstats 31299 * 31300 * ------------------------------------------------------ 31301 * removable media hotplugable | kstat 31302 * ------------------------------------------------------ 31303 * false false | Yes 31304 * false true | Yes 31305 * true x | No 31306 * ------------------------------------------------------ 31307 * 31308 * 31309 * 13. Removable media & hotpluggable properties 31310 * 31311 * Sd driver creates a "removable-media" property for removable media 31312 * devices. Parent nexus drivers create a "hotpluggable" property if 31313 * it supports hotplugging. 31314 * 31315 * --------------------------------------------------------------------- 31316 * removable media hotpluggable | "removable-media" " hotpluggable" 31317 * --------------------------------------------------------------------- 31318 * false false | No No 31319 * false true | No Yes 31320 * true false | Yes No 31321 * true true | Yes Yes 31322 * --------------------------------------------------------------------- 31323 * 31324 * 31325 * 14. Power Management 31326 * 31327 * sd only power manages removable media devices or devices that support 31328 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 31329 * 31330 * A parent nexus that supports hotplugging can also set "pm-capable" 31331 * if the disk can be power managed. 31332 * 31333 * ------------------------------------------------------------ 31334 * removable media hotpluggable pm-capable | power manage 31335 * ------------------------------------------------------------ 31336 * false false false | No 31337 * false false true | Yes 31338 * false true false | No 31339 * false true true | Yes 31340 * true x x | Yes 31341 * ------------------------------------------------------------ 31342 * 31343 * USB and firewire hard disks can now be power managed independently 31344 * of the framebuffer 31345 * 31346 * 31347 * 15. Support for USB disks with capacity larger than 1TB 31348 * 31349 * Currently, sd doesn't permit a fixed disk device with capacity 31350 * larger than 1TB to be used in a 32-bit operating system environment. 31351 * However, sd doesn't do that for removable media devices. Instead, it 31352 * assumes that removable media devices cannot have a capacity larger 31353 * than 1TB. Therefore, using those devices on 32-bit system is partially 31354 * supported, which can cause some unexpected results. 31355 * 31356 * --------------------------------------------------------------------- 31357 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 31358 * --------------------------------------------------------------------- 31359 * false false | true | no 31360 * false true | true | no 31361 * true false | true | Yes 31362 * true true | true | Yes 31363 * --------------------------------------------------------------------- 31364 * 31365 * 31366 * 16. Check write-protection at open time 31367 * 31368 * When a removable media device is being opened for writing without NDELAY 31369 * flag, sd will check if this device is writable. If attempting to open 31370 * without NDELAY flag a write-protected device, this operation will abort. 31371 * 31372 * ------------------------------------------------------------ 31373 * removable media USB/1394 | WP Check 31374 * ------------------------------------------------------------ 31375 * false false | No 31376 * false true | No 31377 * true false | Yes 31378 * true true | Yes 31379 * ------------------------------------------------------------ 31380 * 31381 * 31382 * 17. syslog when corrupted VTOC is encountered 31383 * 31384 * Currently, if an invalid VTOC is encountered, sd only print syslog 31385 * for fixed SCSI disks. 31386 * ------------------------------------------------------------ 31387 * removable media USB/1394 | print syslog 31388 * ------------------------------------------------------------ 31389 * false false | Yes 31390 * false true | No 31391 * true false | No 31392 * true true | No 31393 * ------------------------------------------------------------ 31394 */ 31395 static void 31396 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 31397 { 31398 int pm_capable_prop; 31399 31400 ASSERT(un->un_sd); 31401 ASSERT(un->un_sd->sd_inq); 31402 31403 #if defined(_SUNOS_VTOC_16) 31404 /* 31405 * For VTOC_16 devices, the default label will be created for all 31406 * devices. (see sd_build_default_label) 31407 */ 31408 un->un_f_default_vtoc_supported = TRUE; 31409 #endif 31410 31411 if (un->un_sd->sd_inq->inq_rmb) { 31412 /* 31413 * The media of this device is removable. And for this kind 31414 * of devices, it is possible to change medium after opening 31415 * devices. Thus we should support this operation. 31416 */ 31417 un->un_f_has_removable_media = TRUE; 31418 31419 #if defined(_SUNOS_VTOC_8) 31420 /* 31421 * Note: currently, for VTOC_8 devices, default label is 31422 * created for removable and hotpluggable devices only. 31423 */ 31424 un->un_f_default_vtoc_supported = TRUE; 31425 #endif 31426 /* 31427 * support non-512-byte blocksize of removable media devices 31428 */ 31429 un->un_f_non_devbsize_supported = TRUE; 31430 31431 /* 31432 * Assume that all removable media devices support DOOR_LOCK 31433 */ 31434 un->un_f_doorlock_supported = TRUE; 31435 31436 /* 31437 * For a removable media device, it is possible to be opened 31438 * with NDELAY flag when there is no media in drive, in this 31439 * case we don't care if device is writable. But if without 31440 * NDELAY flag, we need to check if media is write-protected. 31441 */ 31442 un->un_f_chk_wp_open = TRUE; 31443 31444 /* 31445 * need to start a SCSI watch thread to monitor media state, 31446 * when media is being inserted or ejected, notify syseventd. 31447 */ 31448 un->un_f_monitor_media_state = TRUE; 31449 31450 /* 31451 * Some devices don't support START_STOP_UNIT command. 31452 * Therefore, we'd better check if a device supports it 31453 * before sending it. 31454 */ 31455 un->un_f_check_start_stop = TRUE; 31456 31457 /* 31458 * support eject media ioctl: 31459 * FDEJECT, DKIOCEJECT, CDROMEJECT 31460 */ 31461 un->un_f_eject_media_supported = TRUE; 31462 31463 /* 31464 * Because many removable-media devices don't support 31465 * LOG_SENSE, we couldn't use this command to check if 31466 * a removable media device support power-management. 31467 * We assume that they support power-management via 31468 * START_STOP_UNIT command and can be spun up and down 31469 * without limitations. 31470 */ 31471 un->un_f_pm_supported = TRUE; 31472 31473 /* 31474 * Need to create a zero length (Boolean) property 31475 * removable-media for the removable media devices. 31476 * Note that the return value of the property is not being 31477 * checked, since if unable to create the property 31478 * then do not want the attach to fail altogether. Consistent 31479 * with other property creation in attach. 31480 */ 31481 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31482 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31483 31484 } else { 31485 /* 31486 * create device ID for device 31487 */ 31488 un->un_f_devid_supported = TRUE; 31489 31490 /* 31491 * Spin up non-removable-media devices once it is attached 31492 */ 31493 un->un_f_attach_spinup = TRUE; 31494 31495 /* 31496 * According to SCSI specification, Sense data has two kinds of 31497 * format: fixed format, and descriptor format. At present, we 31498 * don't support descriptor format sense data for removable 31499 * media. 31500 */ 31501 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31502 un->un_f_descr_format_supported = TRUE; 31503 } 31504 31505 /* 31506 * kstats are created only for non-removable media devices. 31507 * 31508 * Set this in sd.conf to 0 in order to disable kstats. The 31509 * default is 1, so they are enabled by default. 31510 */ 31511 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31512 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31513 "enable-partition-kstats", 1)); 31514 31515 /* 31516 * Check if HBA has set the "pm-capable" property. 31517 * If "pm-capable" exists and is non-zero then we can 31518 * power manage the device without checking the start/stop 31519 * cycle count log sense page. 31520 * 31521 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31522 * then we should not power manage the device. 31523 * 31524 * If "pm-capable" doesn't exist then pm_capable_prop will 31525 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31526 * sd will check the start/stop cycle count log sense page 31527 * and power manage the device if the cycle count limit has 31528 * not been exceeded. 31529 */ 31530 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31531 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31532 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31533 un->un_f_log_sense_supported = TRUE; 31534 } else { 31535 /* 31536 * pm-capable property exists. 31537 * 31538 * Convert "TRUE" values for pm_capable_prop to 31539 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31540 * later. "TRUE" values are any values except 31541 * SD_PM_CAPABLE_FALSE (0) and 31542 * SD_PM_CAPABLE_UNDEFINED (-1) 31543 */ 31544 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31545 un->un_f_log_sense_supported = FALSE; 31546 } else { 31547 un->un_f_pm_supported = TRUE; 31548 } 31549 31550 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31551 "sd_unit_attach: un:0x%p pm-capable " 31552 "property set to %d.\n", un, un->un_f_pm_supported); 31553 } 31554 } 31555 31556 if (un->un_f_is_hotpluggable) { 31557 #if defined(_SUNOS_VTOC_8) 31558 /* 31559 * Note: currently, for VTOC_8 devices, default label is 31560 * created for removable and hotpluggable devices only. 31561 */ 31562 un->un_f_default_vtoc_supported = TRUE; 31563 #endif 31564 31565 /* 31566 * Have to watch hotpluggable devices as well, since 31567 * that's the only way for userland applications to 31568 * detect hot removal while device is busy/mounted. 31569 */ 31570 un->un_f_monitor_media_state = TRUE; 31571 31572 un->un_f_check_start_stop = TRUE; 31573 31574 } 31575 31576 /* 31577 * By default, only DIRECT ACCESS devices and CDs will have Sun 31578 * labels. 31579 */ 31580 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31581 (un->un_sd->sd_inq->inq_rmb)) { 31582 /* 31583 * Direct access devices have disk label 31584 */ 31585 un->un_f_vtoc_label_supported = TRUE; 31586 } 31587 31588 /* 31589 * Fdisk partitions are supported for all direct access devices on 31590 * x86 platform, and just for removable media and hotpluggable 31591 * devices on SPARC platform. Later, we will set the following flag 31592 * to FALSE if current device is not removable media or hotpluggable 31593 * device and if sd works on SAPRC platform. 31594 */ 31595 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31596 un->un_f_mboot_supported = TRUE; 31597 } 31598 31599 if (!un->un_f_is_hotpluggable && 31600 !un->un_sd->sd_inq->inq_rmb) { 31601 31602 #if defined(_SUNOS_VTOC_8) 31603 /* 31604 * Don't support fdisk on fixed disk 31605 */ 31606 un->un_f_mboot_supported = FALSE; 31607 #endif 31608 31609 /* 31610 * Fixed disk support SYNC CACHE 31611 */ 31612 un->un_f_sync_cache_supported = TRUE; 31613 31614 /* 31615 * For fixed disk, if its VTOC is not valid, we will write 31616 * errlog into system log 31617 */ 31618 if (un->un_f_vtoc_label_supported) 31619 un->un_f_vtoc_errlog_supported = TRUE; 31620 } 31621 } 31622