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 searches the next to last 121 * block for legacy backup EFI label. If fails, it will 122 * turn to the last block for backup EFI label; 123 * 2) Clear backup EFI label: sd first search the last block 124 * for backup EFI label, and will search the next to last 125 * block only if failed for the last block. 126 * 3) Calculate geometry: refer to sd_convert_geometry(), If 127 * capacity increasing by 1 causes disks' capacity to cross 128 * over the limits in table CHS_values, geometry info will 129 * change. This will raise an issue: In case that primary 130 * VTOC label is destroyed, format commandline can restore 131 * it via backup VTOC labels. And format locates backup VTOC 132 * labels by use of geometry from sd driver. So changing 133 * geometry will prevent format from finding backup VTOC 134 * labels. To eliminate this side effect for compatibility, 135 * sd uses (capacity -1) to calculate geometry; 136 * 4) 1TB disks: some important data structures use 32-bit 137 * signed long/int (for example, daddr_t), so that sd doesn't 138 * support a disk with capacity larger than 1TB on 32-bit 139 * platform. However, for exactly 1TB disk, it was treated as 140 * (1T - 512)B in the past, and could have valid solaris 141 * partitions. To workaround this, if an exactly 1TB disk has 142 * solaris fdisk partition, it will be allowed to work with sd. 143 */ 144 #if (defined(__fibre)) 145 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE 146 #else 147 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL 148 #endif 149 150 /* 151 * The name of the driver, established from the module name in _init. 152 */ 153 static char *sd_label = NULL; 154 155 /* 156 * Driver name is unfortunately prefixed on some driver.conf properties. 157 */ 158 #if (defined(__fibre)) 159 #define sd_max_xfer_size ssd_max_xfer_size 160 #define sd_config_list ssd_config_list 161 static char *sd_max_xfer_size = "ssd_max_xfer_size"; 162 static char *sd_config_list = "ssd-config-list"; 163 #else 164 static char *sd_max_xfer_size = "sd_max_xfer_size"; 165 static char *sd_config_list = "sd-config-list"; 166 #endif 167 168 /* 169 * Driver global variables 170 */ 171 172 #if (defined(__fibre)) 173 /* 174 * These #defines are to avoid namespace collisions that occur because this 175 * code is currently used to compile two seperate driver modules: sd and ssd. 176 * All global variables need to be treated this way (even if declared static) 177 * in order to allow the debugger to resolve the names properly. 178 * It is anticipated that in the near future the ssd module will be obsoleted, 179 * at which time this namespace issue should go away. 180 */ 181 #define sd_state ssd_state 182 #define sd_io_time ssd_io_time 183 #define sd_failfast_enable ssd_failfast_enable 184 #define sd_ua_retry_count ssd_ua_retry_count 185 #define sd_report_pfa ssd_report_pfa 186 #define sd_max_throttle ssd_max_throttle 187 #define sd_min_throttle ssd_min_throttle 188 #define sd_rot_delay ssd_rot_delay 189 190 #define sd_retry_on_reservation_conflict \ 191 ssd_retry_on_reservation_conflict 192 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay 193 #define sd_resv_conflict_name ssd_resv_conflict_name 194 195 #define sd_component_mask ssd_component_mask 196 #define sd_level_mask ssd_level_mask 197 #define sd_debug_un ssd_debug_un 198 #define sd_error_level ssd_error_level 199 200 #define sd_xbuf_active_limit ssd_xbuf_active_limit 201 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit 202 203 #define sd_tr ssd_tr 204 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout 205 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout 206 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable 207 #define sd_check_media_time ssd_check_media_time 208 #define sd_wait_cmds_complete ssd_wait_cmds_complete 209 #define sd_label_mutex ssd_label_mutex 210 #define sd_detach_mutex ssd_detach_mutex 211 #define sd_log_buf ssd_log_buf 212 #define sd_log_mutex ssd_log_mutex 213 214 #define sd_disk_table ssd_disk_table 215 #define sd_disk_table_size ssd_disk_table_size 216 #define sd_sense_mutex ssd_sense_mutex 217 #define sd_cdbtab ssd_cdbtab 218 219 #define sd_cb_ops ssd_cb_ops 220 #define sd_ops ssd_ops 221 #define sd_additional_codes ssd_additional_codes 222 223 #define sd_minor_data ssd_minor_data 224 #define sd_minor_data_efi ssd_minor_data_efi 225 226 #define sd_tq ssd_tq 227 #define sd_wmr_tq ssd_wmr_tq 228 #define sd_taskq_name ssd_taskq_name 229 #define sd_wmr_taskq_name ssd_wmr_taskq_name 230 #define sd_taskq_minalloc ssd_taskq_minalloc 231 #define sd_taskq_maxalloc ssd_taskq_maxalloc 232 233 #define sd_dump_format_string ssd_dump_format_string 234 235 #define sd_iostart_chain ssd_iostart_chain 236 #define sd_iodone_chain ssd_iodone_chain 237 238 #define sd_pm_idletime ssd_pm_idletime 239 240 #define sd_force_pm_supported ssd_force_pm_supported 241 242 #define sd_dtype_optical_bind ssd_dtype_optical_bind 243 244 #endif 245 246 247 #ifdef SDDEBUG 248 int sd_force_pm_supported = 0; 249 #endif /* SDDEBUG */ 250 251 void *sd_state = NULL; 252 int sd_io_time = SD_IO_TIME; 253 int sd_failfast_enable = 1; 254 int sd_ua_retry_count = SD_UA_RETRY_COUNT; 255 int sd_report_pfa = 1; 256 int sd_max_throttle = SD_MAX_THROTTLE; 257 int sd_min_throttle = SD_MIN_THROTTLE; 258 int sd_rot_delay = 4; /* Default 4ms Rotation delay */ 259 int sd_qfull_throttle_enable = TRUE; 260 261 int sd_retry_on_reservation_conflict = 1; 262 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 263 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay)) 264 265 static int sd_dtype_optical_bind = -1; 266 267 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */ 268 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict"; 269 270 /* 271 * Global data for debug logging. To enable debug printing, sd_component_mask 272 * and sd_level_mask should be set to the desired bit patterns as outlined in 273 * sddef.h. 274 */ 275 uint_t sd_component_mask = 0x0; 276 uint_t sd_level_mask = 0x0; 277 struct sd_lun *sd_debug_un = NULL; 278 uint_t sd_error_level = SCSI_ERR_RETRYABLE; 279 280 /* Note: these may go away in the future... */ 281 static uint32_t sd_xbuf_active_limit = 512; 282 static uint32_t sd_xbuf_reserve_limit = 16; 283 284 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 }; 285 286 /* 287 * Timer value used to reset the throttle after it has been reduced 288 * (typically in response to TRAN_BUSY or STATUS_QFULL) 289 */ 290 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT; 291 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT; 292 293 /* 294 * Interval value associated with the media change scsi watch. 295 */ 296 static int sd_check_media_time = 3000000; 297 298 /* 299 * Wait value used for in progress operations during a DDI_SUSPEND 300 */ 301 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE; 302 303 /* 304 * sd_label_mutex protects a static buffer used in the disk label 305 * component of the driver 306 */ 307 static kmutex_t sd_label_mutex; 308 309 /* 310 * sd_detach_mutex protects un_layer_count, un_detach_count, and 311 * un_opens_in_progress in the sd_lun structure. 312 */ 313 static kmutex_t sd_detach_mutex; 314 315 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex, 316 sd_lun::{un_layer_count un_detach_count un_opens_in_progress})) 317 318 /* 319 * Global buffer and mutex for debug logging 320 */ 321 static char sd_log_buf[1024]; 322 static kmutex_t sd_log_mutex; 323 324 /* 325 * Structs and globals for recording attached lun information. 326 * This maintains a chain. Each node in the chain represents a SCSI controller. 327 * The structure records the number of luns attached to each target connected 328 * with the controller. 329 * For parallel scsi device only. 330 */ 331 struct sd_scsi_hba_tgt_lun { 332 struct sd_scsi_hba_tgt_lun *next; 333 dev_info_t *pdip; 334 int nlun[NTARGETS_WIDE]; 335 }; 336 337 /* 338 * Flag to indicate the lun is attached or detached 339 */ 340 #define SD_SCSI_LUN_ATTACH 0 341 #define SD_SCSI_LUN_DETACH 1 342 343 static kmutex_t sd_scsi_target_lun_mutex; 344 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL; 345 346 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 347 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip)) 348 349 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex, 350 sd_scsi_target_lun_head)) 351 352 /* 353 * "Smart" Probe Caching structs, globals, #defines, etc. 354 * For parallel scsi and non-self-identify device only. 355 */ 356 357 /* 358 * The following resources and routines are implemented to support 359 * "smart" probing, which caches the scsi_probe() results in an array, 360 * in order to help avoid long probe times. 361 */ 362 struct sd_scsi_probe_cache { 363 struct sd_scsi_probe_cache *next; 364 dev_info_t *pdip; 365 int cache[NTARGETS_WIDE]; 366 }; 367 368 static kmutex_t sd_scsi_probe_cache_mutex; 369 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL; 370 371 /* 372 * Really we only need protection on the head of the linked list, but 373 * better safe than sorry. 374 */ 375 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 376 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip)) 377 378 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex, 379 sd_scsi_probe_cache_head)) 380 381 382 /* 383 * Vendor specific data name property declarations 384 */ 385 386 #if defined(__fibre) || defined(__i386) ||defined(__amd64) 387 388 static sd_tunables seagate_properties = { 389 SEAGATE_THROTTLE_VALUE, 390 0, 391 0, 392 0, 393 0, 394 0, 395 0, 396 0, 397 0 398 }; 399 400 401 static sd_tunables fujitsu_properties = { 402 FUJITSU_THROTTLE_VALUE, 403 0, 404 0, 405 0, 406 0, 407 0, 408 0, 409 0, 410 0 411 }; 412 413 static sd_tunables ibm_properties = { 414 IBM_THROTTLE_VALUE, 415 0, 416 0, 417 0, 418 0, 419 0, 420 0, 421 0, 422 0 423 }; 424 425 static sd_tunables purple_properties = { 426 PURPLE_THROTTLE_VALUE, 427 0, 428 0, 429 PURPLE_BUSY_RETRIES, 430 PURPLE_RESET_RETRY_COUNT, 431 PURPLE_RESERVE_RELEASE_TIME, 432 0, 433 0, 434 0 435 }; 436 437 static sd_tunables sve_properties = { 438 SVE_THROTTLE_VALUE, 439 0, 440 0, 441 SVE_BUSY_RETRIES, 442 SVE_RESET_RETRY_COUNT, 443 SVE_RESERVE_RELEASE_TIME, 444 SVE_MIN_THROTTLE_VALUE, 445 SVE_DISKSORT_DISABLED_FLAG, 446 0 447 }; 448 449 static sd_tunables maserati_properties = { 450 0, 451 0, 452 0, 453 0, 454 0, 455 0, 456 0, 457 MASERATI_DISKSORT_DISABLED_FLAG, 458 MASERATI_LUN_RESET_ENABLED_FLAG 459 }; 460 461 static sd_tunables pirus_properties = { 462 PIRUS_THROTTLE_VALUE, 463 0, 464 PIRUS_NRR_COUNT, 465 PIRUS_BUSY_RETRIES, 466 PIRUS_RESET_RETRY_COUNT, 467 0, 468 PIRUS_MIN_THROTTLE_VALUE, 469 PIRUS_DISKSORT_DISABLED_FLAG, 470 PIRUS_LUN_RESET_ENABLED_FLAG 471 }; 472 473 #endif 474 475 #if (defined(__sparc) && !defined(__fibre)) || \ 476 (defined(__i386) || defined(__amd64)) 477 478 479 static sd_tunables elite_properties = { 480 ELITE_THROTTLE_VALUE, 481 0, 482 0, 483 0, 484 0, 485 0, 486 0, 487 0, 488 0 489 }; 490 491 static sd_tunables st31200n_properties = { 492 ST31200N_THROTTLE_VALUE, 493 0, 494 0, 495 0, 496 0, 497 0, 498 0, 499 0, 500 0 501 }; 502 503 #endif /* Fibre or not */ 504 505 static sd_tunables lsi_properties_scsi = { 506 LSI_THROTTLE_VALUE, 507 0, 508 LSI_NOTREADY_RETRIES, 509 0, 510 0, 511 0, 512 0, 513 0, 514 0 515 }; 516 517 static sd_tunables symbios_properties = { 518 SYMBIOS_THROTTLE_VALUE, 519 0, 520 SYMBIOS_NOTREADY_RETRIES, 521 0, 522 0, 523 0, 524 0, 525 0, 526 0 527 }; 528 529 static sd_tunables lsi_properties = { 530 0, 531 0, 532 LSI_NOTREADY_RETRIES, 533 0, 534 0, 535 0, 536 0, 537 0, 538 0 539 }; 540 541 static sd_tunables lsi_oem_properties = { 542 0, 543 0, 544 LSI_OEM_NOTREADY_RETRIES, 545 0, 546 0, 547 0, 548 0, 549 0, 550 0 551 }; 552 553 554 555 #if (defined(SD_PROP_TST)) 556 557 #define SD_TST_CTYPE_VAL CTYPE_CDROM 558 #define SD_TST_THROTTLE_VAL 16 559 #define SD_TST_NOTREADY_VAL 12 560 #define SD_TST_BUSY_VAL 60 561 #define SD_TST_RST_RETRY_VAL 36 562 #define SD_TST_RSV_REL_TIME 60 563 564 static sd_tunables tst_properties = { 565 SD_TST_THROTTLE_VAL, 566 SD_TST_CTYPE_VAL, 567 SD_TST_NOTREADY_VAL, 568 SD_TST_BUSY_VAL, 569 SD_TST_RST_RETRY_VAL, 570 SD_TST_RSV_REL_TIME, 571 0, 572 0, 573 0 574 }; 575 #endif 576 577 /* This is similiar to the ANSI toupper implementation */ 578 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C)) 579 580 /* 581 * Static Driver Configuration Table 582 * 583 * This is the table of disks which need throttle adjustment (or, perhaps 584 * something else as defined by the flags at a future time.) device_id 585 * is a string consisting of concatenated vid (vendor), pid (product/model) 586 * and revision strings as defined in the scsi_inquiry structure. Offsets of 587 * the parts of the string are as defined by the sizes in the scsi_inquiry 588 * structure. Device type is searched as far as the device_id string is 589 * defined. Flags defines which values are to be set in the driver from the 590 * properties list. 591 * 592 * Entries below which begin and end with a "*" are a special case. 593 * These do not have a specific vendor, and the string which follows 594 * can appear anywhere in the 16 byte PID portion of the inquiry data. 595 * 596 * Entries below which begin and end with a " " (blank) are a special 597 * case. The comparison function will treat multiple consecutive blanks 598 * as equivalent to a single blank. For example, this causes a 599 * sd_disk_table entry of " NEC CDROM " to match a device's id string 600 * of "NEC CDROM". 601 * 602 * Note: The MD21 controller type has been obsoleted. 603 * ST318202F is a Legacy device 604 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been 605 * made with an FC connection. The entries here are a legacy. 606 */ 607 static sd_disk_config_t sd_disk_table[] = { 608 #if defined(__fibre) || defined(__i386) || defined(__amd64) 609 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 610 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 611 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 612 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties }, 613 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 614 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 615 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 616 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 617 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 618 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 619 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 620 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 621 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 622 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties }, 623 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 624 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 625 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 626 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 627 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 628 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 629 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 630 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 631 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties }, 632 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties }, 633 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties }, 634 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties }, 635 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties }, 636 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 637 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 638 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 639 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 640 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 641 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 642 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 643 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 644 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 645 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 648 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 649 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 650 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 652 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 653 { "SUN T3", SD_CONF_BSET_THROTTLE | 654 SD_CONF_BSET_BSY_RETRY_COUNT| 655 SD_CONF_BSET_RST_RETRIES| 656 SD_CONF_BSET_RSV_REL_TIME, 657 &purple_properties }, 658 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 659 SD_CONF_BSET_BSY_RETRY_COUNT| 660 SD_CONF_BSET_RST_RETRIES| 661 SD_CONF_BSET_RSV_REL_TIME| 662 SD_CONF_BSET_MIN_THROTTLE| 663 SD_CONF_BSET_DISKSORT_DISABLED, 664 &sve_properties }, 665 { "SUN T4", SD_CONF_BSET_THROTTLE | 666 SD_CONF_BSET_BSY_RETRY_COUNT| 667 SD_CONF_BSET_RST_RETRIES| 668 SD_CONF_BSET_RSV_REL_TIME, 669 &purple_properties }, 670 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 671 SD_CONF_BSET_LUN_RESET_ENABLED, 672 &maserati_properties }, 673 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 674 SD_CONF_BSET_NRR_COUNT| 675 SD_CONF_BSET_BSY_RETRY_COUNT| 676 SD_CONF_BSET_RST_RETRIES| 677 SD_CONF_BSET_MIN_THROTTLE| 678 SD_CONF_BSET_DISKSORT_DISABLED| 679 SD_CONF_BSET_LUN_RESET_ENABLED, 680 &pirus_properties }, 681 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 682 SD_CONF_BSET_NRR_COUNT| 683 SD_CONF_BSET_BSY_RETRY_COUNT| 684 SD_CONF_BSET_RST_RETRIES| 685 SD_CONF_BSET_MIN_THROTTLE| 686 SD_CONF_BSET_DISKSORT_DISABLED| 687 SD_CONF_BSET_LUN_RESET_ENABLED, 688 &pirus_properties }, 689 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 690 SD_CONF_BSET_NRR_COUNT| 691 SD_CONF_BSET_BSY_RETRY_COUNT| 692 SD_CONF_BSET_RST_RETRIES| 693 SD_CONF_BSET_MIN_THROTTLE| 694 SD_CONF_BSET_DISKSORT_DISABLED| 695 SD_CONF_BSET_LUN_RESET_ENABLED, 696 &pirus_properties }, 697 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 698 SD_CONF_BSET_NRR_COUNT| 699 SD_CONF_BSET_BSY_RETRY_COUNT| 700 SD_CONF_BSET_RST_RETRIES| 701 SD_CONF_BSET_MIN_THROTTLE| 702 SD_CONF_BSET_DISKSORT_DISABLED| 703 SD_CONF_BSET_LUN_RESET_ENABLED, 704 &pirus_properties }, 705 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 706 SD_CONF_BSET_NRR_COUNT| 707 SD_CONF_BSET_BSY_RETRY_COUNT| 708 SD_CONF_BSET_RST_RETRIES| 709 SD_CONF_BSET_MIN_THROTTLE| 710 SD_CONF_BSET_DISKSORT_DISABLED| 711 SD_CONF_BSET_LUN_RESET_ENABLED, 712 &pirus_properties }, 713 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 714 SD_CONF_BSET_NRR_COUNT| 715 SD_CONF_BSET_BSY_RETRY_COUNT| 716 SD_CONF_BSET_RST_RETRIES| 717 SD_CONF_BSET_MIN_THROTTLE| 718 SD_CONF_BSET_DISKSORT_DISABLED| 719 SD_CONF_BSET_LUN_RESET_ENABLED, 720 &pirus_properties }, 721 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 722 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 723 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 724 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 725 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 726 #endif /* fibre or NON-sparc platforms */ 727 #if ((defined(__sparc) && !defined(__fibre)) ||\ 728 (defined(__i386) || defined(__amd64))) 729 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 730 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 731 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 732 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 733 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 734 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 735 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 736 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 737 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 738 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 739 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 740 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 741 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 742 &symbios_properties }, 743 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 744 &lsi_properties_scsi }, 745 #if defined(__i386) || defined(__amd64) 746 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 747 | SD_CONF_BSET_READSUB_BCD 748 | SD_CONF_BSET_READ_TOC_ADDR_BCD 749 | SD_CONF_BSET_NO_READ_HEADER 750 | SD_CONF_BSET_READ_CD_XD4), NULL }, 751 752 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 753 | SD_CONF_BSET_READSUB_BCD 754 | SD_CONF_BSET_READ_TOC_ADDR_BCD 755 | SD_CONF_BSET_NO_READ_HEADER 756 | SD_CONF_BSET_READ_CD_XD4), NULL }, 757 #endif /* __i386 || __amd64 */ 758 #endif /* sparc NON-fibre or NON-sparc platforms */ 759 760 #if (defined(SD_PROP_TST)) 761 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 762 | SD_CONF_BSET_CTYPE 763 | SD_CONF_BSET_NRR_COUNT 764 | SD_CONF_BSET_FAB_DEVID 765 | SD_CONF_BSET_NOCACHE 766 | SD_CONF_BSET_BSY_RETRY_COUNT 767 | SD_CONF_BSET_PLAYMSF_BCD 768 | SD_CONF_BSET_READSUB_BCD 769 | SD_CONF_BSET_READ_TOC_TRK_BCD 770 | SD_CONF_BSET_READ_TOC_ADDR_BCD 771 | SD_CONF_BSET_NO_READ_HEADER 772 | SD_CONF_BSET_READ_CD_XD4 773 | SD_CONF_BSET_RST_RETRIES 774 | SD_CONF_BSET_RSV_REL_TIME 775 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 776 #endif 777 }; 778 779 static const int sd_disk_table_size = 780 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 781 782 783 /* 784 * Return codes of sd_uselabel(). 785 */ 786 #define SD_LABEL_IS_VALID 0 787 #define SD_LABEL_IS_INVALID 1 788 789 #define SD_INTERCONNECT_PARALLEL 0 790 #define SD_INTERCONNECT_FABRIC 1 791 #define SD_INTERCONNECT_FIBRE 2 792 #define SD_INTERCONNECT_SSA 3 793 #define SD_INTERCONNECT_SATA 4 794 #define SD_IS_PARALLEL_SCSI(un) \ 795 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 796 #define SD_IS_SERIAL(un) \ 797 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 798 799 /* 800 * Definitions used by device id registration routines 801 */ 802 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 803 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 804 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 805 #define WD_NODE 7 /* the whole disk minor */ 806 807 static kmutex_t sd_sense_mutex = {0}; 808 809 /* 810 * Macros for updates of the driver state 811 */ 812 #define New_state(un, s) \ 813 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 814 #define Restore_state(un) \ 815 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 816 817 static struct sd_cdbinfo sd_cdbtab[] = { 818 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 819 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 820 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 821 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 822 }; 823 824 /* 825 * Specifies the number of seconds that must have elapsed since the last 826 * cmd. has completed for a device to be declared idle to the PM framework. 827 */ 828 static int sd_pm_idletime = 1; 829 830 /* 831 * Internal function prototypes 832 */ 833 834 #if (defined(__fibre)) 835 /* 836 * These #defines are to avoid namespace collisions that occur because this 837 * code is currently used to compile two seperate driver modules: sd and ssd. 838 * All function names need to be treated this way (even if declared static) 839 * in order to allow the debugger to resolve the names properly. 840 * It is anticipated that in the near future the ssd module will be obsoleted, 841 * at which time this ugliness should go away. 842 */ 843 #define sd_log_trace ssd_log_trace 844 #define sd_log_info ssd_log_info 845 #define sd_log_err ssd_log_err 846 #define sdprobe ssdprobe 847 #define sdinfo ssdinfo 848 #define sd_prop_op ssd_prop_op 849 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 850 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 851 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 852 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 853 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 854 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 855 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 856 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 857 #define sd_spin_up_unit ssd_spin_up_unit 858 #define sd_enable_descr_sense ssd_enable_descr_sense 859 #define sd_reenable_dsense_task ssd_reenable_dsense_task 860 #define sd_set_mmc_caps ssd_set_mmc_caps 861 #define sd_read_unit_properties ssd_read_unit_properties 862 #define sd_process_sdconf_file ssd_process_sdconf_file 863 #define sd_process_sdconf_table ssd_process_sdconf_table 864 #define sd_sdconf_id_match ssd_sdconf_id_match 865 #define sd_blank_cmp ssd_blank_cmp 866 #define sd_chk_vers1_data ssd_chk_vers1_data 867 #define sd_set_vers1_properties ssd_set_vers1_properties 868 #define sd_validate_geometry ssd_validate_geometry 869 870 #if defined(_SUNOS_VTOC_16) 871 #define sd_convert_geometry ssd_convert_geometry 872 #endif 873 874 #define sd_resync_geom_caches ssd_resync_geom_caches 875 #define sd_read_fdisk ssd_read_fdisk 876 #define sd_get_physical_geometry ssd_get_physical_geometry 877 #define sd_get_virtual_geometry ssd_get_virtual_geometry 878 #define sd_update_block_info ssd_update_block_info 879 #define sd_swap_efi_gpt ssd_swap_efi_gpt 880 #define sd_swap_efi_gpe ssd_swap_efi_gpe 881 #define sd_validate_efi ssd_validate_efi 882 #define sd_use_efi ssd_use_efi 883 #define sd_uselabel ssd_uselabel 884 #define sd_build_default_label ssd_build_default_label 885 #define sd_has_max_chs_vals ssd_has_max_chs_vals 886 #define sd_inq_fill ssd_inq_fill 887 #define sd_register_devid ssd_register_devid 888 #define sd_get_devid_block ssd_get_devid_block 889 #define sd_get_devid ssd_get_devid 890 #define sd_create_devid ssd_create_devid 891 #define sd_write_deviceid ssd_write_deviceid 892 #define sd_check_vpd_page_support ssd_check_vpd_page_support 893 #define sd_setup_pm ssd_setup_pm 894 #define sd_create_pm_components ssd_create_pm_components 895 #define sd_ddi_suspend ssd_ddi_suspend 896 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 897 #define sd_ddi_resume ssd_ddi_resume 898 #define sd_ddi_pm_resume ssd_ddi_pm_resume 899 #define sdpower ssdpower 900 #define sdattach ssdattach 901 #define sddetach ssddetach 902 #define sd_unit_attach ssd_unit_attach 903 #define sd_unit_detach ssd_unit_detach 904 #define sd_set_unit_attributes ssd_set_unit_attributes 905 #define sd_create_minor_nodes ssd_create_minor_nodes 906 #define sd_create_errstats ssd_create_errstats 907 #define sd_set_errstats ssd_set_errstats 908 #define sd_set_pstats ssd_set_pstats 909 #define sddump ssddump 910 #define sd_scsi_poll ssd_scsi_poll 911 #define sd_send_polled_RQS ssd_send_polled_RQS 912 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 913 #define sd_init_event_callbacks ssd_init_event_callbacks 914 #define sd_event_callback ssd_event_callback 915 #define sd_cache_control ssd_cache_control 916 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 917 #define sd_make_device ssd_make_device 918 #define sdopen ssdopen 919 #define sdclose ssdclose 920 #define sd_ready_and_valid ssd_ready_and_valid 921 #define sdmin ssdmin 922 #define sdread ssdread 923 #define sdwrite ssdwrite 924 #define sdaread ssdaread 925 #define sdawrite ssdawrite 926 #define sdstrategy ssdstrategy 927 #define sdioctl ssdioctl 928 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 929 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 930 #define sd_checksum_iostart ssd_checksum_iostart 931 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 932 #define sd_pm_iostart ssd_pm_iostart 933 #define sd_core_iostart ssd_core_iostart 934 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 935 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 936 #define sd_checksum_iodone ssd_checksum_iodone 937 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 938 #define sd_pm_iodone ssd_pm_iodone 939 #define sd_initpkt_for_buf ssd_initpkt_for_buf 940 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 941 #define sd_setup_rw_pkt ssd_setup_rw_pkt 942 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 943 #define sd_buf_iodone ssd_buf_iodone 944 #define sd_uscsi_strategy ssd_uscsi_strategy 945 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 946 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 947 #define sd_uscsi_iodone ssd_uscsi_iodone 948 #define sd_xbuf_strategy ssd_xbuf_strategy 949 #define sd_xbuf_init ssd_xbuf_init 950 #define sd_pm_entry ssd_pm_entry 951 #define sd_pm_exit ssd_pm_exit 952 953 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 954 #define sd_pm_timeout_handler ssd_pm_timeout_handler 955 956 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 957 #define sdintr ssdintr 958 #define sd_start_cmds ssd_start_cmds 959 #define sd_send_scsi_cmd ssd_send_scsi_cmd 960 #define sd_bioclone_alloc ssd_bioclone_alloc 961 #define sd_bioclone_free ssd_bioclone_free 962 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 963 #define sd_shadow_buf_free ssd_shadow_buf_free 964 #define sd_print_transport_rejected_message \ 965 ssd_print_transport_rejected_message 966 #define sd_retry_command ssd_retry_command 967 #define sd_set_retry_bp ssd_set_retry_bp 968 #define sd_send_request_sense_command ssd_send_request_sense_command 969 #define sd_start_retry_command ssd_start_retry_command 970 #define sd_start_direct_priority_command \ 971 ssd_start_direct_priority_command 972 #define sd_return_failed_command ssd_return_failed_command 973 #define sd_return_failed_command_no_restart \ 974 ssd_return_failed_command_no_restart 975 #define sd_return_command ssd_return_command 976 #define sd_sync_with_callback ssd_sync_with_callback 977 #define sdrunout ssdrunout 978 #define sd_mark_rqs_busy ssd_mark_rqs_busy 979 #define sd_mark_rqs_idle ssd_mark_rqs_idle 980 #define sd_reduce_throttle ssd_reduce_throttle 981 #define sd_restore_throttle ssd_restore_throttle 982 #define sd_print_incomplete_msg ssd_print_incomplete_msg 983 #define sd_init_cdb_limits ssd_init_cdb_limits 984 #define sd_pkt_status_good ssd_pkt_status_good 985 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 986 #define sd_pkt_status_busy ssd_pkt_status_busy 987 #define sd_pkt_status_reservation_conflict \ 988 ssd_pkt_status_reservation_conflict 989 #define sd_pkt_status_qfull ssd_pkt_status_qfull 990 #define sd_handle_request_sense ssd_handle_request_sense 991 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 992 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 993 #define sd_validate_sense_data ssd_validate_sense_data 994 #define sd_decode_sense ssd_decode_sense 995 #define sd_print_sense_msg ssd_print_sense_msg 996 #define sd_sense_key_no_sense ssd_sense_key_no_sense 997 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 998 #define sd_sense_key_not_ready ssd_sense_key_not_ready 999 #define sd_sense_key_medium_or_hardware_error \ 1000 ssd_sense_key_medium_or_hardware_error 1001 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 1002 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 1003 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1004 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1005 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1006 #define sd_sense_key_default ssd_sense_key_default 1007 #define sd_print_retry_msg ssd_print_retry_msg 1008 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1009 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1010 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1011 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1012 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1013 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1014 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1015 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1016 #define sd_pkt_reason_default ssd_pkt_reason_default 1017 #define sd_reset_target ssd_reset_target 1018 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1019 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1020 #define sd_taskq_create ssd_taskq_create 1021 #define sd_taskq_delete ssd_taskq_delete 1022 #define sd_media_change_task ssd_media_change_task 1023 #define sd_handle_mchange ssd_handle_mchange 1024 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1025 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1026 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1027 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1028 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1029 sd_send_scsi_feature_GET_CONFIGURATION 1030 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1031 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1032 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1033 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1034 ssd_send_scsi_PERSISTENT_RESERVE_IN 1035 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1036 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1037 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1038 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1039 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1040 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1041 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1042 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1043 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1044 #define sd_alloc_rqs ssd_alloc_rqs 1045 #define sd_free_rqs ssd_free_rqs 1046 #define sd_dump_memory ssd_dump_memory 1047 #define sd_uscsi_ioctl ssd_uscsi_ioctl 1048 #define sd_get_media_info ssd_get_media_info 1049 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1050 #define sd_dkio_get_geometry ssd_dkio_get_geometry 1051 #define sd_dkio_set_geometry ssd_dkio_set_geometry 1052 #define sd_dkio_get_partition ssd_dkio_get_partition 1053 #define sd_dkio_set_partition ssd_dkio_set_partition 1054 #define sd_dkio_partition ssd_dkio_partition 1055 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 1056 #define sd_dkio_get_efi ssd_dkio_get_efi 1057 #define sd_build_user_vtoc ssd_build_user_vtoc 1058 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 1059 #define sd_dkio_set_efi ssd_dkio_set_efi 1060 #define sd_build_label_vtoc ssd_build_label_vtoc 1061 #define sd_write_label ssd_write_label 1062 #define sd_clear_vtoc ssd_clear_vtoc 1063 #define sd_clear_efi ssd_clear_efi 1064 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1065 #define sd_setup_next_xfer ssd_setup_next_xfer 1066 #define sd_dkio_get_temp ssd_dkio_get_temp 1067 #define sd_dkio_get_mboot ssd_dkio_get_mboot 1068 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1069 #define sd_setup_default_geometry ssd_setup_default_geometry 1070 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1071 #define sd_check_mhd ssd_check_mhd 1072 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1073 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1074 #define sd_sname ssd_sname 1075 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1076 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1077 #define sd_take_ownership ssd_take_ownership 1078 #define sd_reserve_release ssd_reserve_release 1079 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1080 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1081 #define sd_persistent_reservation_in_read_keys \ 1082 ssd_persistent_reservation_in_read_keys 1083 #define sd_persistent_reservation_in_read_resv \ 1084 ssd_persistent_reservation_in_read_resv 1085 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1086 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1087 #define sd_mhdioc_release ssd_mhdioc_release 1088 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1089 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1090 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1091 #define sr_change_blkmode ssr_change_blkmode 1092 #define sr_change_speed ssr_change_speed 1093 #define sr_atapi_change_speed ssr_atapi_change_speed 1094 #define sr_pause_resume ssr_pause_resume 1095 #define sr_play_msf ssr_play_msf 1096 #define sr_play_trkind ssr_play_trkind 1097 #define sr_read_all_subcodes ssr_read_all_subcodes 1098 #define sr_read_subchannel ssr_read_subchannel 1099 #define sr_read_tocentry ssr_read_tocentry 1100 #define sr_read_tochdr ssr_read_tochdr 1101 #define sr_read_cdda ssr_read_cdda 1102 #define sr_read_cdxa ssr_read_cdxa 1103 #define sr_read_mode1 ssr_read_mode1 1104 #define sr_read_mode2 ssr_read_mode2 1105 #define sr_read_cd_mode2 ssr_read_cd_mode2 1106 #define sr_sector_mode ssr_sector_mode 1107 #define sr_eject ssr_eject 1108 #define sr_ejected ssr_ejected 1109 #define sr_check_wp ssr_check_wp 1110 #define sd_check_media ssd_check_media 1111 #define sd_media_watch_cb ssd_media_watch_cb 1112 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1113 #define sr_volume_ctrl ssr_volume_ctrl 1114 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1115 #define sd_log_page_supported ssd_log_page_supported 1116 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1117 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1118 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1119 #define sd_range_lock ssd_range_lock 1120 #define sd_get_range ssd_get_range 1121 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1122 #define sd_range_unlock ssd_range_unlock 1123 #define sd_read_modify_write_task ssd_read_modify_write_task 1124 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1125 1126 #define sd_iostart_chain ssd_iostart_chain 1127 #define sd_iodone_chain ssd_iodone_chain 1128 #define sd_initpkt_map ssd_initpkt_map 1129 #define sd_destroypkt_map ssd_destroypkt_map 1130 #define sd_chain_type_map ssd_chain_type_map 1131 #define sd_chain_index_map ssd_chain_index_map 1132 1133 #define sd_failfast_flushctl ssd_failfast_flushctl 1134 #define sd_failfast_flushq ssd_failfast_flushq 1135 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1136 1137 #define sd_is_lsi ssd_is_lsi 1138 1139 #endif /* #if (defined(__fibre)) */ 1140 1141 1142 int _init(void); 1143 int _fini(void); 1144 int _info(struct modinfo *modinfop); 1145 1146 /*PRINTFLIKE3*/ 1147 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1148 /*PRINTFLIKE3*/ 1149 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1150 /*PRINTFLIKE3*/ 1151 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1152 1153 static int sdprobe(dev_info_t *devi); 1154 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1155 void **result); 1156 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1157 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1158 1159 /* 1160 * Smart probe for parallel scsi 1161 */ 1162 static void sd_scsi_probe_cache_init(void); 1163 static void sd_scsi_probe_cache_fini(void); 1164 static void sd_scsi_clear_probe_cache(void); 1165 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1166 1167 /* 1168 * Attached luns on target for parallel scsi 1169 */ 1170 static void sd_scsi_target_lun_init(void); 1171 static void sd_scsi_target_lun_fini(void); 1172 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1173 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1174 1175 static int sd_spin_up_unit(struct sd_lun *un); 1176 #ifdef _LP64 1177 static void sd_enable_descr_sense(struct sd_lun *un); 1178 static void sd_reenable_dsense_task(void *arg); 1179 #endif /* _LP64 */ 1180 1181 static void sd_set_mmc_caps(struct sd_lun *un); 1182 1183 static void sd_read_unit_properties(struct sd_lun *un); 1184 static int sd_process_sdconf_file(struct sd_lun *un); 1185 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1186 int *data_list, sd_tunables *values); 1187 static void sd_process_sdconf_table(struct sd_lun *un); 1188 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1189 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1190 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1191 int list_len, char *dataname_ptr); 1192 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1193 sd_tunables *prop_list); 1194 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1195 1196 #if defined(_SUNOS_VTOC_16) 1197 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1198 #endif 1199 1200 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1201 int path_flag); 1202 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1203 int path_flag); 1204 static void sd_get_physical_geometry(struct sd_lun *un, 1205 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1206 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1207 int lbasize); 1208 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1209 static void sd_swap_efi_gpt(efi_gpt_t *); 1210 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1211 static int sd_validate_efi(efi_gpt_t *); 1212 static int sd_use_efi(struct sd_lun *, int); 1213 static void sd_build_default_label(struct sd_lun *un); 1214 1215 #if defined(_FIRMWARE_NEEDS_FDISK) 1216 static int sd_has_max_chs_vals(struct ipart *fdp); 1217 #endif 1218 static void sd_inq_fill(char *p, int l, char *s); 1219 1220 1221 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1222 int reservation_flag); 1223 static daddr_t sd_get_devid_block(struct sd_lun *un); 1224 static int sd_get_devid(struct sd_lun *un); 1225 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1226 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1227 static int sd_write_deviceid(struct sd_lun *un); 1228 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1229 static int sd_check_vpd_page_support(struct sd_lun *un); 1230 1231 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1232 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1233 1234 static int sd_ddi_suspend(dev_info_t *devi); 1235 static int sd_ddi_pm_suspend(struct sd_lun *un); 1236 static int sd_ddi_resume(dev_info_t *devi); 1237 static int sd_ddi_pm_resume(struct sd_lun *un); 1238 static int sdpower(dev_info_t *devi, int component, int level); 1239 1240 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1241 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1242 static int sd_unit_attach(dev_info_t *devi); 1243 static int sd_unit_detach(dev_info_t *devi); 1244 1245 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1246 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1247 static void sd_create_errstats(struct sd_lun *un, int instance); 1248 static void sd_set_errstats(struct sd_lun *un); 1249 static void sd_set_pstats(struct sd_lun *un); 1250 1251 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1252 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1253 static int sd_send_polled_RQS(struct sd_lun *un); 1254 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1255 1256 #if (defined(__fibre)) 1257 /* 1258 * Event callbacks (photon) 1259 */ 1260 static void sd_init_event_callbacks(struct sd_lun *un); 1261 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1262 #endif 1263 1264 /* 1265 * Defines for sd_cache_control 1266 */ 1267 1268 #define SD_CACHE_ENABLE 1 1269 #define SD_CACHE_DISABLE 0 1270 #define SD_CACHE_NOCHANGE -1 1271 1272 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1273 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1274 static dev_t sd_make_device(dev_info_t *devi); 1275 1276 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1277 uint64_t capacity); 1278 1279 /* 1280 * Driver entry point functions. 1281 */ 1282 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1283 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1284 static int sd_ready_and_valid(struct sd_lun *un); 1285 1286 static void sdmin(struct buf *bp); 1287 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1288 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1289 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1290 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1291 1292 static int sdstrategy(struct buf *bp); 1293 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1294 1295 /* 1296 * Function prototypes for layering functions in the iostart chain. 1297 */ 1298 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1299 struct buf *bp); 1300 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1301 struct buf *bp); 1302 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1303 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1304 struct buf *bp); 1305 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1306 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1307 1308 /* 1309 * Function prototypes for layering functions in the iodone chain. 1310 */ 1311 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1312 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1313 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1314 struct buf *bp); 1315 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1316 struct buf *bp); 1317 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1318 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1319 struct buf *bp); 1320 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1321 1322 /* 1323 * Prototypes for functions to support buf(9S) based IO. 1324 */ 1325 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1326 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1327 static void sd_destroypkt_for_buf(struct buf *); 1328 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1329 struct buf *bp, int flags, 1330 int (*callback)(caddr_t), caddr_t callback_arg, 1331 diskaddr_t lba, uint32_t blockcount); 1332 #if defined(__i386) || defined(__amd64) 1333 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1334 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1335 #endif /* defined(__i386) || defined(__amd64) */ 1336 1337 /* 1338 * Prototypes for functions to support USCSI IO. 1339 */ 1340 static int sd_uscsi_strategy(struct buf *bp); 1341 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1342 static void sd_destroypkt_for_uscsi(struct buf *); 1343 1344 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1345 uchar_t chain_type, void *pktinfop); 1346 1347 static int sd_pm_entry(struct sd_lun *un); 1348 static void sd_pm_exit(struct sd_lun *un); 1349 1350 static void sd_pm_idletimeout_handler(void *arg); 1351 1352 /* 1353 * sd_core internal functions (used at the sd_core_io layer). 1354 */ 1355 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1356 static void sdintr(struct scsi_pkt *pktp); 1357 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1358 1359 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1360 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1361 int path_flag); 1362 1363 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1364 daddr_t blkno, int (*func)(struct buf *)); 1365 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1366 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1367 static void sd_bioclone_free(struct buf *bp); 1368 static void sd_shadow_buf_free(struct buf *bp); 1369 1370 static void sd_print_transport_rejected_message(struct sd_lun *un, 1371 struct sd_xbuf *xp, int code); 1372 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1373 void *arg, int code); 1374 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1375 void *arg, int code); 1376 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1377 void *arg, int code); 1378 1379 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1380 int retry_check_flag, 1381 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1382 int c), 1383 void *user_arg, int failure_code, clock_t retry_delay, 1384 void (*statp)(kstat_io_t *)); 1385 1386 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1387 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1388 1389 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1390 struct scsi_pkt *pktp); 1391 static void sd_start_retry_command(void *arg); 1392 static void sd_start_direct_priority_command(void *arg); 1393 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1394 int errcode); 1395 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1396 struct buf *bp, int errcode); 1397 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1398 static void sd_sync_with_callback(struct sd_lun *un); 1399 static int sdrunout(caddr_t arg); 1400 1401 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1402 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1403 1404 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1405 static void sd_restore_throttle(void *arg); 1406 1407 static void sd_init_cdb_limits(struct sd_lun *un); 1408 1409 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1410 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1411 1412 /* 1413 * Error handling functions 1414 */ 1415 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1416 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1417 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1418 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1419 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1420 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1421 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1422 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1423 1424 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1425 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1426 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1427 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1428 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1429 struct sd_xbuf *xp); 1430 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1431 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1432 1433 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1434 void *arg, int code); 1435 1436 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1437 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1438 static void sd_sense_key_recoverable_error(struct sd_lun *un, 1439 uint8_t *sense_datap, 1440 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1441 static void sd_sense_key_not_ready(struct sd_lun *un, 1442 uint8_t *sense_datap, 1443 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1444 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 1445 uint8_t *sense_datap, 1446 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1447 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1448 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1449 static void sd_sense_key_unit_attention(struct sd_lun *un, 1450 uint8_t *sense_datap, 1451 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1452 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1453 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1454 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1455 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1456 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1457 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1458 static void sd_sense_key_default(struct sd_lun *un, 1459 uint8_t *sense_datap, 1460 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1461 1462 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1463 void *arg, int flag); 1464 1465 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1466 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1467 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1468 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1469 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1470 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1471 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1472 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1473 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1474 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1475 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1476 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1477 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1478 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1479 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1480 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1481 1482 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1483 1484 static void sd_start_stop_unit_callback(void *arg); 1485 static void sd_start_stop_unit_task(void *arg); 1486 1487 static void sd_taskq_create(void); 1488 static void sd_taskq_delete(void); 1489 static void sd_media_change_task(void *arg); 1490 1491 static int sd_handle_mchange(struct sd_lun *un); 1492 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1493 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1494 uint32_t *lbap, int path_flag); 1495 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1496 uint32_t *lbap, int path_flag); 1497 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1498 int path_flag); 1499 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1500 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1501 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1502 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1503 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1504 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1505 uchar_t usr_cmd, uchar_t *usr_bufp); 1506 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1507 struct dk_callback *dkc); 1508 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1509 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1510 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1511 uchar_t *bufaddr, uint_t buflen); 1512 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1513 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1514 uchar_t *bufaddr, uint_t buflen, char feature); 1515 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1516 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1517 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1518 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1519 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1520 size_t buflen, daddr_t start_block, int path_flag); 1521 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1522 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1523 path_flag) 1524 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1525 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1526 path_flag) 1527 1528 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1529 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1530 uint16_t param_ptr, int path_flag); 1531 1532 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1533 static void sd_free_rqs(struct sd_lun *un); 1534 1535 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1536 uchar_t *data, int len, int fmt); 1537 static void sd_panic_for_res_conflict(struct sd_lun *un); 1538 1539 /* 1540 * Disk Ioctl Function Prototypes 1541 */ 1542 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1543 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1544 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1545 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1546 int geom_validated); 1547 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1548 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1549 int geom_validated); 1550 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1551 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1552 int geom_validated); 1553 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1554 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1555 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1556 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1557 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1558 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1559 static int sd_write_label(dev_t dev); 1560 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1561 static void sd_clear_vtoc(struct sd_lun *un); 1562 static void sd_clear_efi(struct sd_lun *un); 1563 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1564 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1565 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1566 static void sd_setup_default_geometry(struct sd_lun *un); 1567 #if defined(__i386) || defined(__amd64) 1568 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1569 #endif 1570 1571 /* 1572 * Multi-host Ioctl Prototypes 1573 */ 1574 static int sd_check_mhd(dev_t dev, int interval); 1575 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1576 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1577 static char *sd_sname(uchar_t status); 1578 static void sd_mhd_resvd_recover(void *arg); 1579 static void sd_resv_reclaim_thread(); 1580 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1581 static int sd_reserve_release(dev_t dev, int cmd); 1582 static void sd_rmv_resv_reclaim_req(dev_t dev); 1583 static void sd_mhd_reset_notify_cb(caddr_t arg); 1584 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1585 mhioc_inkeys_t *usrp, int flag); 1586 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1587 mhioc_inresvs_t *usrp, int flag); 1588 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1589 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1590 static int sd_mhdioc_release(dev_t dev); 1591 static int sd_mhdioc_register_devid(dev_t dev); 1592 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1593 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1594 1595 /* 1596 * SCSI removable prototypes 1597 */ 1598 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1599 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1600 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1601 static int sr_pause_resume(dev_t dev, int mode); 1602 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1603 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1604 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1605 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1606 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1607 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1609 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1610 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1611 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1612 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1613 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1614 static int sr_eject(dev_t dev); 1615 static void sr_ejected(register struct sd_lun *un); 1616 static int sr_check_wp(dev_t dev); 1617 static int sd_check_media(dev_t dev, enum dkio_state state); 1618 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1619 static void sd_delayed_cv_broadcast(void *arg); 1620 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1621 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1622 1623 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1624 1625 /* 1626 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1627 */ 1628 static void sd_check_for_writable_cd(struct sd_lun *un); 1629 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1630 static void sd_wm_cache_destructor(void *wm, void *un); 1631 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1632 daddr_t endb, ushort_t typ); 1633 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1634 daddr_t endb); 1635 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1636 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1637 static void sd_read_modify_write_task(void * arg); 1638 static int 1639 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1640 struct buf **bpp); 1641 1642 1643 /* 1644 * Function prototypes for failfast support. 1645 */ 1646 static void sd_failfast_flushq(struct sd_lun *un); 1647 static int sd_failfast_flushq_callback(struct buf *bp); 1648 1649 /* 1650 * Function prototypes to check for lsi devices 1651 */ 1652 static void sd_is_lsi(struct sd_lun *un); 1653 1654 /* 1655 * Function prototypes for x86 support 1656 */ 1657 #if defined(__i386) || defined(__amd64) 1658 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1659 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1660 #endif 1661 1662 /* 1663 * Constants for failfast support: 1664 * 1665 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1666 * failfast processing being performed. 1667 * 1668 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1669 * failfast processing on all bufs with B_FAILFAST set. 1670 */ 1671 1672 #define SD_FAILFAST_INACTIVE 0 1673 #define SD_FAILFAST_ACTIVE 1 1674 1675 /* 1676 * Bitmask to control behavior of buf(9S) flushes when a transition to 1677 * the failfast state occurs. Optional bits include: 1678 * 1679 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1680 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1681 * be flushed. 1682 * 1683 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1684 * driver, in addition to the regular wait queue. This includes the xbuf 1685 * queues. When clear, only the driver's wait queue will be flushed. 1686 */ 1687 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1688 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1689 1690 /* 1691 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1692 * to flush all queues within the driver. 1693 */ 1694 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1695 1696 1697 /* 1698 * SD Testing Fault Injection 1699 */ 1700 #ifdef SD_FAULT_INJECTION 1701 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1702 static void sd_faultinjection(struct scsi_pkt *pktp); 1703 static void sd_injection_log(char *buf, struct sd_lun *un); 1704 #endif 1705 1706 /* 1707 * Device driver ops vector 1708 */ 1709 static struct cb_ops sd_cb_ops = { 1710 sdopen, /* open */ 1711 sdclose, /* close */ 1712 sdstrategy, /* strategy */ 1713 nodev, /* print */ 1714 sddump, /* dump */ 1715 sdread, /* read */ 1716 sdwrite, /* write */ 1717 sdioctl, /* ioctl */ 1718 nodev, /* devmap */ 1719 nodev, /* mmap */ 1720 nodev, /* segmap */ 1721 nochpoll, /* poll */ 1722 sd_prop_op, /* cb_prop_op */ 1723 0, /* streamtab */ 1724 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1725 CB_REV, /* cb_rev */ 1726 sdaread, /* async I/O read entry point */ 1727 sdawrite /* async I/O write entry point */ 1728 }; 1729 1730 static struct dev_ops sd_ops = { 1731 DEVO_REV, /* devo_rev, */ 1732 0, /* refcnt */ 1733 sdinfo, /* info */ 1734 nulldev, /* identify */ 1735 sdprobe, /* probe */ 1736 sdattach, /* attach */ 1737 sddetach, /* detach */ 1738 nodev, /* reset */ 1739 &sd_cb_ops, /* driver operations */ 1740 NULL, /* bus operations */ 1741 sdpower /* power */ 1742 }; 1743 1744 1745 /* 1746 * This is the loadable module wrapper. 1747 */ 1748 #include <sys/modctl.h> 1749 1750 static struct modldrv modldrv = { 1751 &mod_driverops, /* Type of module. This one is a driver */ 1752 SD_MODULE_NAME, /* Module name. */ 1753 &sd_ops /* driver ops */ 1754 }; 1755 1756 1757 static struct modlinkage modlinkage = { 1758 MODREV_1, 1759 &modldrv, 1760 NULL 1761 }; 1762 1763 1764 static struct scsi_asq_key_strings sd_additional_codes[] = { 1765 0x81, 0, "Logical Unit is Reserved", 1766 0x85, 0, "Audio Address Not Valid", 1767 0xb6, 0, "Media Load Mechanism Failed", 1768 0xB9, 0, "Audio Play Operation Aborted", 1769 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1770 0x53, 2, "Medium removal prevented", 1771 0x6f, 0, "Authentication failed during key exchange", 1772 0x6f, 1, "Key not present", 1773 0x6f, 2, "Key not established", 1774 0x6f, 3, "Read without proper authentication", 1775 0x6f, 4, "Mismatched region to this logical unit", 1776 0x6f, 5, "Region reset count error", 1777 0xffff, 0x0, NULL 1778 }; 1779 1780 1781 /* 1782 * Struct for passing printing information for sense data messages 1783 */ 1784 struct sd_sense_info { 1785 int ssi_severity; 1786 int ssi_pfa_flag; 1787 }; 1788 1789 /* 1790 * Table of function pointers for iostart-side routines. Seperate "chains" 1791 * of layered function calls are formed by placing the function pointers 1792 * sequentially in the desired order. Functions are called according to an 1793 * incrementing table index ordering. The last function in each chain must 1794 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1795 * in the sd_iodone_chain[] array. 1796 * 1797 * Note: It may seem more natural to organize both the iostart and iodone 1798 * functions together, into an array of structures (or some similar 1799 * organization) with a common index, rather than two seperate arrays which 1800 * must be maintained in synchronization. The purpose of this division is 1801 * to achiece improved performance: individual arrays allows for more 1802 * effective cache line utilization on certain platforms. 1803 */ 1804 1805 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1806 1807 1808 static sd_chain_t sd_iostart_chain[] = { 1809 1810 /* Chain for buf IO for disk drive targets (PM enabled) */ 1811 sd_mapblockaddr_iostart, /* Index: 0 */ 1812 sd_pm_iostart, /* Index: 1 */ 1813 sd_core_iostart, /* Index: 2 */ 1814 1815 /* Chain for buf IO for disk drive targets (PM disabled) */ 1816 sd_mapblockaddr_iostart, /* Index: 3 */ 1817 sd_core_iostart, /* Index: 4 */ 1818 1819 /* Chain for buf IO for removable-media targets (PM enabled) */ 1820 sd_mapblockaddr_iostart, /* Index: 5 */ 1821 sd_mapblocksize_iostart, /* Index: 6 */ 1822 sd_pm_iostart, /* Index: 7 */ 1823 sd_core_iostart, /* Index: 8 */ 1824 1825 /* Chain for buf IO for removable-media targets (PM disabled) */ 1826 sd_mapblockaddr_iostart, /* Index: 9 */ 1827 sd_mapblocksize_iostart, /* Index: 10 */ 1828 sd_core_iostart, /* Index: 11 */ 1829 1830 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1831 sd_mapblockaddr_iostart, /* Index: 12 */ 1832 sd_checksum_iostart, /* Index: 13 */ 1833 sd_pm_iostart, /* Index: 14 */ 1834 sd_core_iostart, /* Index: 15 */ 1835 1836 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1837 sd_mapblockaddr_iostart, /* Index: 16 */ 1838 sd_checksum_iostart, /* Index: 17 */ 1839 sd_core_iostart, /* Index: 18 */ 1840 1841 /* Chain for USCSI commands (all targets) */ 1842 sd_pm_iostart, /* Index: 19 */ 1843 sd_core_iostart, /* Index: 20 */ 1844 1845 /* Chain for checksumming USCSI commands (all targets) */ 1846 sd_checksum_uscsi_iostart, /* Index: 21 */ 1847 sd_pm_iostart, /* Index: 22 */ 1848 sd_core_iostart, /* Index: 23 */ 1849 1850 /* Chain for "direct" USCSI commands (all targets) */ 1851 sd_core_iostart, /* Index: 24 */ 1852 1853 /* Chain for "direct priority" USCSI commands (all targets) */ 1854 sd_core_iostart, /* Index: 25 */ 1855 }; 1856 1857 /* 1858 * Macros to locate the first function of each iostart chain in the 1859 * sd_iostart_chain[] array. These are located by the index in the array. 1860 */ 1861 #define SD_CHAIN_DISK_IOSTART 0 1862 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1863 #define SD_CHAIN_RMMEDIA_IOSTART 5 1864 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1865 #define SD_CHAIN_CHKSUM_IOSTART 12 1866 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1867 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1868 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1869 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1870 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1871 1872 1873 /* 1874 * Table of function pointers for the iodone-side routines for the driver- 1875 * internal layering mechanism. The calling sequence for iodone routines 1876 * uses a decrementing table index, so the last routine called in a chain 1877 * must be at the lowest array index location for that chain. The last 1878 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1879 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1880 * of the functions in an iodone side chain must correspond to the ordering 1881 * of the iostart routines for that chain. Note that there is no iodone 1882 * side routine that corresponds to sd_core_iostart(), so there is no 1883 * entry in the table for this. 1884 */ 1885 1886 static sd_chain_t sd_iodone_chain[] = { 1887 1888 /* Chain for buf IO for disk drive targets (PM enabled) */ 1889 sd_buf_iodone, /* Index: 0 */ 1890 sd_mapblockaddr_iodone, /* Index: 1 */ 1891 sd_pm_iodone, /* Index: 2 */ 1892 1893 /* Chain for buf IO for disk drive targets (PM disabled) */ 1894 sd_buf_iodone, /* Index: 3 */ 1895 sd_mapblockaddr_iodone, /* Index: 4 */ 1896 1897 /* Chain for buf IO for removable-media targets (PM enabled) */ 1898 sd_buf_iodone, /* Index: 5 */ 1899 sd_mapblockaddr_iodone, /* Index: 6 */ 1900 sd_mapblocksize_iodone, /* Index: 7 */ 1901 sd_pm_iodone, /* Index: 8 */ 1902 1903 /* Chain for buf IO for removable-media targets (PM disabled) */ 1904 sd_buf_iodone, /* Index: 9 */ 1905 sd_mapblockaddr_iodone, /* Index: 10 */ 1906 sd_mapblocksize_iodone, /* Index: 11 */ 1907 1908 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1909 sd_buf_iodone, /* Index: 12 */ 1910 sd_mapblockaddr_iodone, /* Index: 13 */ 1911 sd_checksum_iodone, /* Index: 14 */ 1912 sd_pm_iodone, /* Index: 15 */ 1913 1914 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1915 sd_buf_iodone, /* Index: 16 */ 1916 sd_mapblockaddr_iodone, /* Index: 17 */ 1917 sd_checksum_iodone, /* Index: 18 */ 1918 1919 /* Chain for USCSI commands (non-checksum targets) */ 1920 sd_uscsi_iodone, /* Index: 19 */ 1921 sd_pm_iodone, /* Index: 20 */ 1922 1923 /* Chain for USCSI commands (checksum targets) */ 1924 sd_uscsi_iodone, /* Index: 21 */ 1925 sd_checksum_uscsi_iodone, /* Index: 22 */ 1926 sd_pm_iodone, /* Index: 22 */ 1927 1928 /* Chain for "direct" USCSI commands (all targets) */ 1929 sd_uscsi_iodone, /* Index: 24 */ 1930 1931 /* Chain for "direct priority" USCSI commands (all targets) */ 1932 sd_uscsi_iodone, /* Index: 25 */ 1933 }; 1934 1935 1936 /* 1937 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1938 * each iodone-side chain. These are located by the array index, but as the 1939 * iodone side functions are called in a decrementing-index order, the 1940 * highest index number in each chain must be specified (as these correspond 1941 * to the first function in the iodone chain that will be called by the core 1942 * at IO completion time). 1943 */ 1944 1945 #define SD_CHAIN_DISK_IODONE 2 1946 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1947 #define SD_CHAIN_RMMEDIA_IODONE 8 1948 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1949 #define SD_CHAIN_CHKSUM_IODONE 15 1950 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1951 #define SD_CHAIN_USCSI_CMD_IODONE 20 1952 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1953 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1954 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1955 1956 1957 1958 1959 /* 1960 * Array to map a layering chain index to the appropriate initpkt routine. 1961 * The redundant entries are present so that the index used for accessing 1962 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1963 * with this table as well. 1964 */ 1965 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1966 1967 static sd_initpkt_t sd_initpkt_map[] = { 1968 1969 /* Chain for buf IO for disk drive targets (PM enabled) */ 1970 sd_initpkt_for_buf, /* Index: 0 */ 1971 sd_initpkt_for_buf, /* Index: 1 */ 1972 sd_initpkt_for_buf, /* Index: 2 */ 1973 1974 /* Chain for buf IO for disk drive targets (PM disabled) */ 1975 sd_initpkt_for_buf, /* Index: 3 */ 1976 sd_initpkt_for_buf, /* Index: 4 */ 1977 1978 /* Chain for buf IO for removable-media targets (PM enabled) */ 1979 sd_initpkt_for_buf, /* Index: 5 */ 1980 sd_initpkt_for_buf, /* Index: 6 */ 1981 sd_initpkt_for_buf, /* Index: 7 */ 1982 sd_initpkt_for_buf, /* Index: 8 */ 1983 1984 /* Chain for buf IO for removable-media targets (PM disabled) */ 1985 sd_initpkt_for_buf, /* Index: 9 */ 1986 sd_initpkt_for_buf, /* Index: 10 */ 1987 sd_initpkt_for_buf, /* Index: 11 */ 1988 1989 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1990 sd_initpkt_for_buf, /* Index: 12 */ 1991 sd_initpkt_for_buf, /* Index: 13 */ 1992 sd_initpkt_for_buf, /* Index: 14 */ 1993 sd_initpkt_for_buf, /* Index: 15 */ 1994 1995 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1996 sd_initpkt_for_buf, /* Index: 16 */ 1997 sd_initpkt_for_buf, /* Index: 17 */ 1998 sd_initpkt_for_buf, /* Index: 18 */ 1999 2000 /* Chain for USCSI commands (non-checksum targets) */ 2001 sd_initpkt_for_uscsi, /* Index: 19 */ 2002 sd_initpkt_for_uscsi, /* Index: 20 */ 2003 2004 /* Chain for USCSI commands (checksum targets) */ 2005 sd_initpkt_for_uscsi, /* Index: 21 */ 2006 sd_initpkt_for_uscsi, /* Index: 22 */ 2007 sd_initpkt_for_uscsi, /* Index: 22 */ 2008 2009 /* Chain for "direct" USCSI commands (all targets) */ 2010 sd_initpkt_for_uscsi, /* Index: 24 */ 2011 2012 /* Chain for "direct priority" USCSI commands (all targets) */ 2013 sd_initpkt_for_uscsi, /* Index: 25 */ 2014 2015 }; 2016 2017 2018 /* 2019 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2020 * The redundant entries are present so that the index used for accessing 2021 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2022 * with this table as well. 2023 */ 2024 typedef void (*sd_destroypkt_t)(struct buf *); 2025 2026 static sd_destroypkt_t sd_destroypkt_map[] = { 2027 2028 /* Chain for buf IO for disk drive targets (PM enabled) */ 2029 sd_destroypkt_for_buf, /* Index: 0 */ 2030 sd_destroypkt_for_buf, /* Index: 1 */ 2031 sd_destroypkt_for_buf, /* Index: 2 */ 2032 2033 /* Chain for buf IO for disk drive targets (PM disabled) */ 2034 sd_destroypkt_for_buf, /* Index: 3 */ 2035 sd_destroypkt_for_buf, /* Index: 4 */ 2036 2037 /* Chain for buf IO for removable-media targets (PM enabled) */ 2038 sd_destroypkt_for_buf, /* Index: 5 */ 2039 sd_destroypkt_for_buf, /* Index: 6 */ 2040 sd_destroypkt_for_buf, /* Index: 7 */ 2041 sd_destroypkt_for_buf, /* Index: 8 */ 2042 2043 /* Chain for buf IO for removable-media targets (PM disabled) */ 2044 sd_destroypkt_for_buf, /* Index: 9 */ 2045 sd_destroypkt_for_buf, /* Index: 10 */ 2046 sd_destroypkt_for_buf, /* Index: 11 */ 2047 2048 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2049 sd_destroypkt_for_buf, /* Index: 12 */ 2050 sd_destroypkt_for_buf, /* Index: 13 */ 2051 sd_destroypkt_for_buf, /* Index: 14 */ 2052 sd_destroypkt_for_buf, /* Index: 15 */ 2053 2054 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2055 sd_destroypkt_for_buf, /* Index: 16 */ 2056 sd_destroypkt_for_buf, /* Index: 17 */ 2057 sd_destroypkt_for_buf, /* Index: 18 */ 2058 2059 /* Chain for USCSI commands (non-checksum targets) */ 2060 sd_destroypkt_for_uscsi, /* Index: 19 */ 2061 sd_destroypkt_for_uscsi, /* Index: 20 */ 2062 2063 /* Chain for USCSI commands (checksum targets) */ 2064 sd_destroypkt_for_uscsi, /* Index: 21 */ 2065 sd_destroypkt_for_uscsi, /* Index: 22 */ 2066 sd_destroypkt_for_uscsi, /* Index: 22 */ 2067 2068 /* Chain for "direct" USCSI commands (all targets) */ 2069 sd_destroypkt_for_uscsi, /* Index: 24 */ 2070 2071 /* Chain for "direct priority" USCSI commands (all targets) */ 2072 sd_destroypkt_for_uscsi, /* Index: 25 */ 2073 2074 }; 2075 2076 2077 2078 /* 2079 * Array to map a layering chain index to the appropriate chain "type". 2080 * The chain type indicates a specific property/usage of the chain. 2081 * The redundant entries are present so that the index used for accessing 2082 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2083 * with this table as well. 2084 */ 2085 2086 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2087 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2088 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2089 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2090 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2091 /* (for error recovery) */ 2092 2093 static int sd_chain_type_map[] = { 2094 2095 /* Chain for buf IO for disk drive targets (PM enabled) */ 2096 SD_CHAIN_BUFIO, /* Index: 0 */ 2097 SD_CHAIN_BUFIO, /* Index: 1 */ 2098 SD_CHAIN_BUFIO, /* Index: 2 */ 2099 2100 /* Chain for buf IO for disk drive targets (PM disabled) */ 2101 SD_CHAIN_BUFIO, /* Index: 3 */ 2102 SD_CHAIN_BUFIO, /* Index: 4 */ 2103 2104 /* Chain for buf IO for removable-media targets (PM enabled) */ 2105 SD_CHAIN_BUFIO, /* Index: 5 */ 2106 SD_CHAIN_BUFIO, /* Index: 6 */ 2107 SD_CHAIN_BUFIO, /* Index: 7 */ 2108 SD_CHAIN_BUFIO, /* Index: 8 */ 2109 2110 /* Chain for buf IO for removable-media targets (PM disabled) */ 2111 SD_CHAIN_BUFIO, /* Index: 9 */ 2112 SD_CHAIN_BUFIO, /* Index: 10 */ 2113 SD_CHAIN_BUFIO, /* Index: 11 */ 2114 2115 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2116 SD_CHAIN_BUFIO, /* Index: 12 */ 2117 SD_CHAIN_BUFIO, /* Index: 13 */ 2118 SD_CHAIN_BUFIO, /* Index: 14 */ 2119 SD_CHAIN_BUFIO, /* Index: 15 */ 2120 2121 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2122 SD_CHAIN_BUFIO, /* Index: 16 */ 2123 SD_CHAIN_BUFIO, /* Index: 17 */ 2124 SD_CHAIN_BUFIO, /* Index: 18 */ 2125 2126 /* Chain for USCSI commands (non-checksum targets) */ 2127 SD_CHAIN_USCSI, /* Index: 19 */ 2128 SD_CHAIN_USCSI, /* Index: 20 */ 2129 2130 /* Chain for USCSI commands (checksum targets) */ 2131 SD_CHAIN_USCSI, /* Index: 21 */ 2132 SD_CHAIN_USCSI, /* Index: 22 */ 2133 SD_CHAIN_USCSI, /* Index: 22 */ 2134 2135 /* Chain for "direct" USCSI commands (all targets) */ 2136 SD_CHAIN_DIRECT, /* Index: 24 */ 2137 2138 /* Chain for "direct priority" USCSI commands (all targets) */ 2139 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2140 }; 2141 2142 2143 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2144 #define SD_IS_BUFIO(xp) \ 2145 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2146 2147 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2148 #define SD_IS_DIRECT_PRIORITY(xp) \ 2149 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2150 2151 2152 2153 /* 2154 * Struct, array, and macros to map a specific chain to the appropriate 2155 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2156 * 2157 * The sd_chain_index_map[] array is used at attach time to set the various 2158 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2159 * chain to be used with the instance. This allows different instances to use 2160 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2161 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2162 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2163 * dynamically & without the use of locking; and (2) a layer may update the 2164 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2165 * to allow for deferred processing of an IO within the same chain from a 2166 * different execution context. 2167 */ 2168 2169 struct sd_chain_index { 2170 int sci_iostart_index; 2171 int sci_iodone_index; 2172 }; 2173 2174 static struct sd_chain_index sd_chain_index_map[] = { 2175 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2176 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2177 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2178 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2179 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2180 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2181 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2182 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2183 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2184 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2185 }; 2186 2187 2188 /* 2189 * The following are indexes into the sd_chain_index_map[] array. 2190 */ 2191 2192 /* un->un_buf_chain_type must be set to one of these */ 2193 #define SD_CHAIN_INFO_DISK 0 2194 #define SD_CHAIN_INFO_DISK_NO_PM 1 2195 #define SD_CHAIN_INFO_RMMEDIA 2 2196 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2197 #define SD_CHAIN_INFO_CHKSUM 4 2198 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2199 2200 /* un->un_uscsi_chain_type must be set to one of these */ 2201 #define SD_CHAIN_INFO_USCSI_CMD 6 2202 /* USCSI with PM disabled is the same as DIRECT */ 2203 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2204 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2205 2206 /* un->un_direct_chain_type must be set to one of these */ 2207 #define SD_CHAIN_INFO_DIRECT_CMD 8 2208 2209 /* un->un_priority_chain_type must be set to one of these */ 2210 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2211 2212 /* size for devid inquiries */ 2213 #define MAX_INQUIRY_SIZE 0xF0 2214 2215 /* 2216 * Macros used by functions to pass a given buf(9S) struct along to the 2217 * next function in the layering chain for further processing. 2218 * 2219 * In the following macros, passing more than three arguments to the called 2220 * routines causes the optimizer for the SPARC compiler to stop doing tail 2221 * call elimination which results in significant performance degradation. 2222 */ 2223 #define SD_BEGIN_IOSTART(index, un, bp) \ 2224 ((*(sd_iostart_chain[index]))(index, un, bp)) 2225 2226 #define SD_BEGIN_IODONE(index, un, bp) \ 2227 ((*(sd_iodone_chain[index]))(index, un, bp)) 2228 2229 #define SD_NEXT_IOSTART(index, un, bp) \ 2230 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2231 2232 #define SD_NEXT_IODONE(index, un, bp) \ 2233 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2234 2235 /* 2236 * Function: _init 2237 * 2238 * Description: This is the driver _init(9E) entry point. 2239 * 2240 * Return Code: Returns the value from mod_install(9F) or 2241 * ddi_soft_state_init(9F) as appropriate. 2242 * 2243 * Context: Called when driver module loaded. 2244 */ 2245 2246 int 2247 _init(void) 2248 { 2249 int err; 2250 2251 /* establish driver name from module name */ 2252 sd_label = mod_modname(&modlinkage); 2253 2254 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2255 SD_MAXUNIT); 2256 2257 if (err != 0) { 2258 return (err); 2259 } 2260 2261 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2262 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2263 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2264 2265 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2266 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2267 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2268 2269 /* 2270 * it's ok to init here even for fibre device 2271 */ 2272 sd_scsi_probe_cache_init(); 2273 2274 sd_scsi_target_lun_init(); 2275 2276 /* 2277 * Creating taskq before mod_install ensures that all callers (threads) 2278 * that enter the module after a successfull mod_install encounter 2279 * a valid taskq. 2280 */ 2281 sd_taskq_create(); 2282 2283 err = mod_install(&modlinkage); 2284 if (err != 0) { 2285 /* delete taskq if install fails */ 2286 sd_taskq_delete(); 2287 2288 mutex_destroy(&sd_detach_mutex); 2289 mutex_destroy(&sd_log_mutex); 2290 mutex_destroy(&sd_label_mutex); 2291 2292 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2293 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2294 cv_destroy(&sd_tr.srq_inprocess_cv); 2295 2296 sd_scsi_probe_cache_fini(); 2297 2298 sd_scsi_target_lun_fini(); 2299 2300 ddi_soft_state_fini(&sd_state); 2301 return (err); 2302 } 2303 2304 return (err); 2305 } 2306 2307 2308 /* 2309 * Function: _fini 2310 * 2311 * Description: This is the driver _fini(9E) entry point. 2312 * 2313 * Return Code: Returns the value from mod_remove(9F) 2314 * 2315 * Context: Called when driver module is unloaded. 2316 */ 2317 2318 int 2319 _fini(void) 2320 { 2321 int err; 2322 2323 if ((err = mod_remove(&modlinkage)) != 0) { 2324 return (err); 2325 } 2326 2327 sd_taskq_delete(); 2328 2329 mutex_destroy(&sd_detach_mutex); 2330 mutex_destroy(&sd_log_mutex); 2331 mutex_destroy(&sd_label_mutex); 2332 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2333 2334 sd_scsi_probe_cache_fini(); 2335 2336 sd_scsi_target_lun_fini(); 2337 2338 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2339 cv_destroy(&sd_tr.srq_inprocess_cv); 2340 2341 ddi_soft_state_fini(&sd_state); 2342 2343 return (err); 2344 } 2345 2346 2347 /* 2348 * Function: _info 2349 * 2350 * Description: This is the driver _info(9E) entry point. 2351 * 2352 * Arguments: modinfop - pointer to the driver modinfo structure 2353 * 2354 * Return Code: Returns the value from mod_info(9F). 2355 * 2356 * Context: Kernel thread context 2357 */ 2358 2359 int 2360 _info(struct modinfo *modinfop) 2361 { 2362 return (mod_info(&modlinkage, modinfop)); 2363 } 2364 2365 2366 /* 2367 * The following routines implement the driver message logging facility. 2368 * They provide component- and level- based debug output filtering. 2369 * Output may also be restricted to messages for a single instance by 2370 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2371 * to NULL, then messages for all instances are printed. 2372 * 2373 * These routines have been cloned from each other due to the language 2374 * constraints of macros and variable argument list processing. 2375 */ 2376 2377 2378 /* 2379 * Function: sd_log_err 2380 * 2381 * Description: This routine is called by the SD_ERROR macro for debug 2382 * logging of error conditions. 2383 * 2384 * Arguments: comp - driver component being logged 2385 * dev - pointer to driver info structure 2386 * fmt - error string and format to be logged 2387 */ 2388 2389 static void 2390 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2391 { 2392 va_list ap; 2393 dev_info_t *dev; 2394 2395 ASSERT(un != NULL); 2396 dev = SD_DEVINFO(un); 2397 ASSERT(dev != NULL); 2398 2399 /* 2400 * Filter messages based on the global component and level masks. 2401 * Also print if un matches the value of sd_debug_un, or if 2402 * sd_debug_un is set to NULL. 2403 */ 2404 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2405 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2406 mutex_enter(&sd_log_mutex); 2407 va_start(ap, fmt); 2408 (void) vsprintf(sd_log_buf, fmt, ap); 2409 va_end(ap); 2410 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2411 mutex_exit(&sd_log_mutex); 2412 } 2413 #ifdef SD_FAULT_INJECTION 2414 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2415 if (un->sd_injection_mask & comp) { 2416 mutex_enter(&sd_log_mutex); 2417 va_start(ap, fmt); 2418 (void) vsprintf(sd_log_buf, fmt, ap); 2419 va_end(ap); 2420 sd_injection_log(sd_log_buf, un); 2421 mutex_exit(&sd_log_mutex); 2422 } 2423 #endif 2424 } 2425 2426 2427 /* 2428 * Function: sd_log_info 2429 * 2430 * Description: This routine is called by the SD_INFO macro for debug 2431 * logging of general purpose informational conditions. 2432 * 2433 * Arguments: comp - driver component being logged 2434 * dev - pointer to driver info structure 2435 * fmt - info string and format to be logged 2436 */ 2437 2438 static void 2439 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2440 { 2441 va_list ap; 2442 dev_info_t *dev; 2443 2444 ASSERT(un != NULL); 2445 dev = SD_DEVINFO(un); 2446 ASSERT(dev != NULL); 2447 2448 /* 2449 * Filter messages based on the global component and level masks. 2450 * Also print if un matches the value of sd_debug_un, or if 2451 * sd_debug_un is set to NULL. 2452 */ 2453 if ((sd_component_mask & component) && 2454 (sd_level_mask & SD_LOGMASK_INFO) && 2455 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2456 mutex_enter(&sd_log_mutex); 2457 va_start(ap, fmt); 2458 (void) vsprintf(sd_log_buf, fmt, ap); 2459 va_end(ap); 2460 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2461 mutex_exit(&sd_log_mutex); 2462 } 2463 #ifdef SD_FAULT_INJECTION 2464 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2465 if (un->sd_injection_mask & component) { 2466 mutex_enter(&sd_log_mutex); 2467 va_start(ap, fmt); 2468 (void) vsprintf(sd_log_buf, fmt, ap); 2469 va_end(ap); 2470 sd_injection_log(sd_log_buf, un); 2471 mutex_exit(&sd_log_mutex); 2472 } 2473 #endif 2474 } 2475 2476 2477 /* 2478 * Function: sd_log_trace 2479 * 2480 * Description: This routine is called by the SD_TRACE macro for debug 2481 * logging of trace conditions (i.e. function entry/exit). 2482 * 2483 * Arguments: comp - driver component being logged 2484 * dev - pointer to driver info structure 2485 * fmt - trace string and format to be logged 2486 */ 2487 2488 static void 2489 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2490 { 2491 va_list ap; 2492 dev_info_t *dev; 2493 2494 ASSERT(un != NULL); 2495 dev = SD_DEVINFO(un); 2496 ASSERT(dev != NULL); 2497 2498 /* 2499 * Filter messages based on the global component and level masks. 2500 * Also print if un matches the value of sd_debug_un, or if 2501 * sd_debug_un is set to NULL. 2502 */ 2503 if ((sd_component_mask & component) && 2504 (sd_level_mask & SD_LOGMASK_TRACE) && 2505 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2506 mutex_enter(&sd_log_mutex); 2507 va_start(ap, fmt); 2508 (void) vsprintf(sd_log_buf, fmt, ap); 2509 va_end(ap); 2510 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2511 mutex_exit(&sd_log_mutex); 2512 } 2513 #ifdef SD_FAULT_INJECTION 2514 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2515 if (un->sd_injection_mask & component) { 2516 mutex_enter(&sd_log_mutex); 2517 va_start(ap, fmt); 2518 (void) vsprintf(sd_log_buf, fmt, ap); 2519 va_end(ap); 2520 sd_injection_log(sd_log_buf, un); 2521 mutex_exit(&sd_log_mutex); 2522 } 2523 #endif 2524 } 2525 2526 2527 /* 2528 * Function: sdprobe 2529 * 2530 * Description: This is the driver probe(9e) entry point function. 2531 * 2532 * Arguments: devi - opaque device info handle 2533 * 2534 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2535 * DDI_PROBE_FAILURE: If the probe failed. 2536 * DDI_PROBE_PARTIAL: If the instance is not present now, 2537 * but may be present in the future. 2538 */ 2539 2540 static int 2541 sdprobe(dev_info_t *devi) 2542 { 2543 struct scsi_device *devp; 2544 int rval; 2545 int instance; 2546 2547 /* 2548 * if it wasn't for pln, sdprobe could actually be nulldev 2549 * in the "__fibre" case. 2550 */ 2551 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2552 return (DDI_PROBE_DONTCARE); 2553 } 2554 2555 devp = ddi_get_driver_private(devi); 2556 2557 if (devp == NULL) { 2558 /* Ooops... nexus driver is mis-configured... */ 2559 return (DDI_PROBE_FAILURE); 2560 } 2561 2562 instance = ddi_get_instance(devi); 2563 2564 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2565 return (DDI_PROBE_PARTIAL); 2566 } 2567 2568 /* 2569 * Call the SCSA utility probe routine to see if we actually 2570 * have a target at this SCSI nexus. 2571 */ 2572 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2573 case SCSIPROBE_EXISTS: 2574 switch (devp->sd_inq->inq_dtype) { 2575 case DTYPE_DIRECT: 2576 rval = DDI_PROBE_SUCCESS; 2577 break; 2578 case DTYPE_RODIRECT: 2579 /* CDs etc. Can be removable media */ 2580 rval = DDI_PROBE_SUCCESS; 2581 break; 2582 case DTYPE_OPTICAL: 2583 /* 2584 * Rewritable optical driver HP115AA 2585 * Can also be removable media 2586 */ 2587 2588 /* 2589 * Do not attempt to bind to DTYPE_OPTICAL if 2590 * pre solaris 9 sparc sd behavior is required 2591 * 2592 * If first time through and sd_dtype_optical_bind 2593 * has not been set in /etc/system check properties 2594 */ 2595 2596 if (sd_dtype_optical_bind < 0) { 2597 sd_dtype_optical_bind = ddi_prop_get_int 2598 (DDI_DEV_T_ANY, devi, 0, 2599 "optical-device-bind", 1); 2600 } 2601 2602 if (sd_dtype_optical_bind == 0) { 2603 rval = DDI_PROBE_FAILURE; 2604 } else { 2605 rval = DDI_PROBE_SUCCESS; 2606 } 2607 break; 2608 2609 case DTYPE_NOTPRESENT: 2610 default: 2611 rval = DDI_PROBE_FAILURE; 2612 break; 2613 } 2614 break; 2615 default: 2616 rval = DDI_PROBE_PARTIAL; 2617 break; 2618 } 2619 2620 /* 2621 * This routine checks for resource allocation prior to freeing, 2622 * so it will take care of the "smart probing" case where a 2623 * scsi_probe() may or may not have been issued and will *not* 2624 * free previously-freed resources. 2625 */ 2626 scsi_unprobe(devp); 2627 return (rval); 2628 } 2629 2630 2631 /* 2632 * Function: sdinfo 2633 * 2634 * Description: This is the driver getinfo(9e) entry point function. 2635 * Given the device number, return the devinfo pointer from 2636 * the scsi_device structure or the instance number 2637 * associated with the dev_t. 2638 * 2639 * Arguments: dip - pointer to device info structure 2640 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2641 * DDI_INFO_DEVT2INSTANCE) 2642 * arg - driver dev_t 2643 * resultp - user buffer for request response 2644 * 2645 * Return Code: DDI_SUCCESS 2646 * DDI_FAILURE 2647 */ 2648 /* ARGSUSED */ 2649 static int 2650 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2651 { 2652 struct sd_lun *un; 2653 dev_t dev; 2654 int instance; 2655 int error; 2656 2657 switch (infocmd) { 2658 case DDI_INFO_DEVT2DEVINFO: 2659 dev = (dev_t)arg; 2660 instance = SDUNIT(dev); 2661 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2662 return (DDI_FAILURE); 2663 } 2664 *result = (void *) SD_DEVINFO(un); 2665 error = DDI_SUCCESS; 2666 break; 2667 case DDI_INFO_DEVT2INSTANCE: 2668 dev = (dev_t)arg; 2669 instance = SDUNIT(dev); 2670 *result = (void *)(uintptr_t)instance; 2671 error = DDI_SUCCESS; 2672 break; 2673 default: 2674 error = DDI_FAILURE; 2675 } 2676 return (error); 2677 } 2678 2679 /* 2680 * Function: sd_prop_op 2681 * 2682 * Description: This is the driver prop_op(9e) entry point function. 2683 * Return the number of blocks for the partition in question 2684 * or forward the request to the property facilities. 2685 * 2686 * Arguments: dev - device number 2687 * dip - pointer to device info structure 2688 * prop_op - property operator 2689 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2690 * name - pointer to property name 2691 * valuep - pointer or address of the user buffer 2692 * lengthp - property length 2693 * 2694 * Return Code: DDI_PROP_SUCCESS 2695 * DDI_PROP_NOT_FOUND 2696 * DDI_PROP_UNDEFINED 2697 * DDI_PROP_NO_MEMORY 2698 * DDI_PROP_BUF_TOO_SMALL 2699 */ 2700 2701 static int 2702 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2703 char *name, caddr_t valuep, int *lengthp) 2704 { 2705 int instance = ddi_get_instance(dip); 2706 struct sd_lun *un; 2707 uint64_t nblocks64; 2708 2709 /* 2710 * Our dynamic properties are all device specific and size oriented. 2711 * Requests issued under conditions where size is valid are passed 2712 * to ddi_prop_op_nblocks with the size information, otherwise the 2713 * request is passed to ddi_prop_op. Size depends on valid geometry. 2714 */ 2715 un = ddi_get_soft_state(sd_state, instance); 2716 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2717 (un->un_f_geometry_is_valid == FALSE)) { 2718 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2719 name, valuep, lengthp)); 2720 } else { 2721 /* get nblocks value */ 2722 ASSERT(!mutex_owned(SD_MUTEX(un))); 2723 mutex_enter(SD_MUTEX(un)); 2724 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2725 mutex_exit(SD_MUTEX(un)); 2726 2727 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2728 name, valuep, lengthp, nblocks64)); 2729 } 2730 } 2731 2732 /* 2733 * The following functions are for smart probing: 2734 * sd_scsi_probe_cache_init() 2735 * sd_scsi_probe_cache_fini() 2736 * sd_scsi_clear_probe_cache() 2737 * sd_scsi_probe_with_cache() 2738 */ 2739 2740 /* 2741 * Function: sd_scsi_probe_cache_init 2742 * 2743 * Description: Initializes the probe response cache mutex and head pointer. 2744 * 2745 * Context: Kernel thread context 2746 */ 2747 2748 static void 2749 sd_scsi_probe_cache_init(void) 2750 { 2751 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2752 sd_scsi_probe_cache_head = NULL; 2753 } 2754 2755 2756 /* 2757 * Function: sd_scsi_probe_cache_fini 2758 * 2759 * Description: Frees all resources associated with the probe response cache. 2760 * 2761 * Context: Kernel thread context 2762 */ 2763 2764 static void 2765 sd_scsi_probe_cache_fini(void) 2766 { 2767 struct sd_scsi_probe_cache *cp; 2768 struct sd_scsi_probe_cache *ncp; 2769 2770 /* Clean up our smart probing linked list */ 2771 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2772 ncp = cp->next; 2773 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2774 } 2775 sd_scsi_probe_cache_head = NULL; 2776 mutex_destroy(&sd_scsi_probe_cache_mutex); 2777 } 2778 2779 2780 /* 2781 * Function: sd_scsi_clear_probe_cache 2782 * 2783 * Description: This routine clears the probe response cache. This is 2784 * done when open() returns ENXIO so that when deferred 2785 * attach is attempted (possibly after a device has been 2786 * turned on) we will retry the probe. Since we don't know 2787 * which target we failed to open, we just clear the 2788 * entire cache. 2789 * 2790 * Context: Kernel thread context 2791 */ 2792 2793 static void 2794 sd_scsi_clear_probe_cache(void) 2795 { 2796 struct sd_scsi_probe_cache *cp; 2797 int i; 2798 2799 mutex_enter(&sd_scsi_probe_cache_mutex); 2800 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2801 /* 2802 * Reset all entries to SCSIPROBE_EXISTS. This will 2803 * force probing to be performed the next time 2804 * sd_scsi_probe_with_cache is called. 2805 */ 2806 for (i = 0; i < NTARGETS_WIDE; i++) { 2807 cp->cache[i] = SCSIPROBE_EXISTS; 2808 } 2809 } 2810 mutex_exit(&sd_scsi_probe_cache_mutex); 2811 } 2812 2813 2814 /* 2815 * Function: sd_scsi_probe_with_cache 2816 * 2817 * Description: This routine implements support for a scsi device probe 2818 * with cache. The driver maintains a cache of the target 2819 * responses to scsi probes. If we get no response from a 2820 * target during a probe inquiry, we remember that, and we 2821 * avoid additional calls to scsi_probe on non-zero LUNs 2822 * on the same target until the cache is cleared. By doing 2823 * so we avoid the 1/4 sec selection timeout for nonzero 2824 * LUNs. lun0 of a target is always probed. 2825 * 2826 * Arguments: devp - Pointer to a scsi_device(9S) structure 2827 * waitfunc - indicates what the allocator routines should 2828 * do when resources are not available. This value 2829 * is passed on to scsi_probe() when that routine 2830 * is called. 2831 * 2832 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2833 * otherwise the value returned by scsi_probe(9F). 2834 * 2835 * Context: Kernel thread context 2836 */ 2837 2838 static int 2839 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2840 { 2841 struct sd_scsi_probe_cache *cp; 2842 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2843 int lun, tgt; 2844 2845 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2846 SCSI_ADDR_PROP_LUN, 0); 2847 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2848 SCSI_ADDR_PROP_TARGET, -1); 2849 2850 /* Make sure caching enabled and target in range */ 2851 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2852 /* do it the old way (no cache) */ 2853 return (scsi_probe(devp, waitfn)); 2854 } 2855 2856 mutex_enter(&sd_scsi_probe_cache_mutex); 2857 2858 /* Find the cache for this scsi bus instance */ 2859 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2860 if (cp->pdip == pdip) { 2861 break; 2862 } 2863 } 2864 2865 /* If we can't find a cache for this pdip, create one */ 2866 if (cp == NULL) { 2867 int i; 2868 2869 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2870 KM_SLEEP); 2871 cp->pdip = pdip; 2872 cp->next = sd_scsi_probe_cache_head; 2873 sd_scsi_probe_cache_head = cp; 2874 for (i = 0; i < NTARGETS_WIDE; i++) { 2875 cp->cache[i] = SCSIPROBE_EXISTS; 2876 } 2877 } 2878 2879 mutex_exit(&sd_scsi_probe_cache_mutex); 2880 2881 /* Recompute the cache for this target if LUN zero */ 2882 if (lun == 0) { 2883 cp->cache[tgt] = SCSIPROBE_EXISTS; 2884 } 2885 2886 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2887 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2888 return (SCSIPROBE_NORESP); 2889 } 2890 2891 /* Do the actual probe; save & return the result */ 2892 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2893 } 2894 2895 2896 /* 2897 * Function: sd_scsi_target_lun_init 2898 * 2899 * Description: Initializes the attached lun chain mutex and head pointer. 2900 * 2901 * Context: Kernel thread context 2902 */ 2903 2904 static void 2905 sd_scsi_target_lun_init(void) 2906 { 2907 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2908 sd_scsi_target_lun_head = NULL; 2909 } 2910 2911 2912 /* 2913 * Function: sd_scsi_target_lun_fini 2914 * 2915 * Description: Frees all resources associated with the attached lun 2916 * chain 2917 * 2918 * Context: Kernel thread context 2919 */ 2920 2921 static void 2922 sd_scsi_target_lun_fini(void) 2923 { 2924 struct sd_scsi_hba_tgt_lun *cp; 2925 struct sd_scsi_hba_tgt_lun *ncp; 2926 2927 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2928 ncp = cp->next; 2929 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2930 } 2931 sd_scsi_target_lun_head = NULL; 2932 mutex_destroy(&sd_scsi_target_lun_mutex); 2933 } 2934 2935 2936 /* 2937 * Function: sd_scsi_get_target_lun_count 2938 * 2939 * Description: This routine will check in the attached lun chain to see 2940 * how many luns are attached on the required SCSI controller 2941 * and target. Currently, some capabilities like tagged queue 2942 * are supported per target based by HBA. So all luns in a 2943 * target have the same capabilities. Based on this assumption, 2944 * sd should only set these capabilities once per target. This 2945 * function is called when sd needs to decide how many luns 2946 * already attached on a target. 2947 * 2948 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2949 * controller device. 2950 * target - The target ID on the controller's SCSI bus. 2951 * 2952 * Return Code: The number of luns attached on the required target and 2953 * controller. 2954 * -1 if target ID is not in parallel SCSI scope or the given 2955 * dip is not in the chain. 2956 * 2957 * Context: Kernel thread context 2958 */ 2959 2960 static int 2961 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2962 { 2963 struct sd_scsi_hba_tgt_lun *cp; 2964 2965 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2966 return (-1); 2967 } 2968 2969 mutex_enter(&sd_scsi_target_lun_mutex); 2970 2971 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2972 if (cp->pdip == dip) { 2973 break; 2974 } 2975 } 2976 2977 mutex_exit(&sd_scsi_target_lun_mutex); 2978 2979 if (cp == NULL) { 2980 return (-1); 2981 } 2982 2983 return (cp->nlun[target]); 2984 } 2985 2986 2987 /* 2988 * Function: sd_scsi_update_lun_on_target 2989 * 2990 * Description: This routine is used to update the attached lun chain when a 2991 * lun is attached or detached on a target. 2992 * 2993 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2994 * controller device. 2995 * target - The target ID on the controller's SCSI bus. 2996 * flag - Indicate the lun is attached or detached. 2997 * 2998 * Context: Kernel thread context 2999 */ 3000 3001 static void 3002 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 3003 { 3004 struct sd_scsi_hba_tgt_lun *cp; 3005 3006 mutex_enter(&sd_scsi_target_lun_mutex); 3007 3008 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3009 if (cp->pdip == dip) { 3010 break; 3011 } 3012 } 3013 3014 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3015 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3016 KM_SLEEP); 3017 cp->pdip = dip; 3018 cp->next = sd_scsi_target_lun_head; 3019 sd_scsi_target_lun_head = cp; 3020 } 3021 3022 mutex_exit(&sd_scsi_target_lun_mutex); 3023 3024 if (cp != NULL) { 3025 if (flag == SD_SCSI_LUN_ATTACH) { 3026 cp->nlun[target] ++; 3027 } else { 3028 cp->nlun[target] --; 3029 } 3030 } 3031 } 3032 3033 3034 /* 3035 * Function: sd_spin_up_unit 3036 * 3037 * Description: Issues the following commands to spin-up the device: 3038 * START STOP UNIT, and INQUIRY. 3039 * 3040 * Arguments: un - driver soft state (unit) structure 3041 * 3042 * Return Code: 0 - success 3043 * EIO - failure 3044 * EACCES - reservation conflict 3045 * 3046 * Context: Kernel thread context 3047 */ 3048 3049 static int 3050 sd_spin_up_unit(struct sd_lun *un) 3051 { 3052 size_t resid = 0; 3053 int has_conflict = FALSE; 3054 uchar_t *bufaddr; 3055 3056 ASSERT(un != NULL); 3057 3058 /* 3059 * Send a throwaway START UNIT command. 3060 * 3061 * If we fail on this, we don't care presently what precisely 3062 * is wrong. EMC's arrays will also fail this with a check 3063 * condition (0x2/0x4/0x3) if the device is "inactive," but 3064 * we don't want to fail the attach because it may become 3065 * "active" later. 3066 */ 3067 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 3068 == EACCES) 3069 has_conflict = TRUE; 3070 3071 /* 3072 * Send another INQUIRY command to the target. This is necessary for 3073 * non-removable media direct access devices because their INQUIRY data 3074 * may not be fully qualified until they are spun up (perhaps via the 3075 * START command above). Note: This seems to be needed for some 3076 * legacy devices only.) The INQUIRY command should succeed even if a 3077 * Reservation Conflict is present. 3078 */ 3079 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3080 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 3081 kmem_free(bufaddr, SUN_INQSIZE); 3082 return (EIO); 3083 } 3084 3085 /* 3086 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3087 * Note that this routine does not return a failure here even if the 3088 * INQUIRY command did not return any data. This is a legacy behavior. 3089 */ 3090 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3091 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3092 } 3093 3094 kmem_free(bufaddr, SUN_INQSIZE); 3095 3096 /* If we hit a reservation conflict above, tell the caller. */ 3097 if (has_conflict == TRUE) { 3098 return (EACCES); 3099 } 3100 3101 return (0); 3102 } 3103 3104 #ifdef _LP64 3105 /* 3106 * Function: sd_enable_descr_sense 3107 * 3108 * Description: This routine attempts to select descriptor sense format 3109 * using the Control mode page. Devices that support 64 bit 3110 * LBAs (for >2TB luns) should also implement descriptor 3111 * sense data so we will call this function whenever we see 3112 * a lun larger than 2TB. If for some reason the device 3113 * supports 64 bit LBAs but doesn't support descriptor sense 3114 * presumably the mode select will fail. Everything will 3115 * continue to work normally except that we will not get 3116 * complete sense data for commands that fail with an LBA 3117 * larger than 32 bits. 3118 * 3119 * Arguments: un - driver soft state (unit) structure 3120 * 3121 * Context: Kernel thread context only 3122 */ 3123 3124 static void 3125 sd_enable_descr_sense(struct sd_lun *un) 3126 { 3127 uchar_t *header; 3128 struct mode_control_scsi3 *ctrl_bufp; 3129 size_t buflen; 3130 size_t bd_len; 3131 3132 /* 3133 * Read MODE SENSE page 0xA, Control Mode Page 3134 */ 3135 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3136 sizeof (struct mode_control_scsi3); 3137 header = kmem_zalloc(buflen, KM_SLEEP); 3138 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3139 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3140 SD_ERROR(SD_LOG_COMMON, un, 3141 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3142 goto eds_exit; 3143 } 3144 3145 /* 3146 * Determine size of Block Descriptors in order to locate 3147 * the mode page data. ATAPI devices return 0, SCSI devices 3148 * should return MODE_BLK_DESC_LENGTH. 3149 */ 3150 bd_len = ((struct mode_header *)header)->bdesc_length; 3151 3152 ctrl_bufp = (struct mode_control_scsi3 *) 3153 (header + MODE_HEADER_LENGTH + bd_len); 3154 3155 /* 3156 * Clear PS bit for MODE SELECT 3157 */ 3158 ctrl_bufp->mode_page.ps = 0; 3159 3160 /* 3161 * Set D_SENSE to enable descriptor sense format. 3162 */ 3163 ctrl_bufp->d_sense = 1; 3164 3165 /* 3166 * Use MODE SELECT to commit the change to the D_SENSE bit 3167 */ 3168 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3169 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3170 SD_INFO(SD_LOG_COMMON, un, 3171 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3172 goto eds_exit; 3173 } 3174 3175 eds_exit: 3176 kmem_free(header, buflen); 3177 } 3178 3179 /* 3180 * Function: sd_reenable_dsense_task 3181 * 3182 * Description: Re-enable descriptor sense after device or bus reset 3183 * 3184 * Context: Executes in a taskq() thread context 3185 */ 3186 static void 3187 sd_reenable_dsense_task(void *arg) 3188 { 3189 struct sd_lun *un = arg; 3190 3191 ASSERT(un != NULL); 3192 sd_enable_descr_sense(un); 3193 } 3194 #endif /* _LP64 */ 3195 3196 /* 3197 * Function: sd_set_mmc_caps 3198 * 3199 * Description: This routine determines if the device is MMC compliant and if 3200 * the device supports CDDA via a mode sense of the CDVD 3201 * capabilities mode page. Also checks if the device is a 3202 * dvdram writable device. 3203 * 3204 * Arguments: un - driver soft state (unit) structure 3205 * 3206 * Context: Kernel thread context only 3207 */ 3208 3209 static void 3210 sd_set_mmc_caps(struct sd_lun *un) 3211 { 3212 struct mode_header_grp2 *sense_mhp; 3213 uchar_t *sense_page; 3214 caddr_t buf; 3215 int bd_len; 3216 int status; 3217 struct uscsi_cmd com; 3218 int rtn; 3219 uchar_t *out_data_rw, *out_data_hd; 3220 uchar_t *rqbuf_rw, *rqbuf_hd; 3221 3222 ASSERT(un != NULL); 3223 3224 /* 3225 * The flags which will be set in this function are - mmc compliant, 3226 * dvdram writable device, cdda support. Initialize them to FALSE 3227 * and if a capability is detected - it will be set to TRUE. 3228 */ 3229 un->un_f_mmc_cap = FALSE; 3230 un->un_f_dvdram_writable_device = FALSE; 3231 un->un_f_cfg_cdda = FALSE; 3232 3233 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3234 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3235 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3236 3237 if (status != 0) { 3238 /* command failed; just return */ 3239 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3240 return; 3241 } 3242 /* 3243 * If the mode sense request for the CDROM CAPABILITIES 3244 * page (0x2A) succeeds the device is assumed to be MMC. 3245 */ 3246 un->un_f_mmc_cap = TRUE; 3247 3248 /* Get to the page data */ 3249 sense_mhp = (struct mode_header_grp2 *)buf; 3250 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3251 sense_mhp->bdesc_length_lo; 3252 if (bd_len > MODE_BLK_DESC_LENGTH) { 3253 /* 3254 * We did not get back the expected block descriptor 3255 * length so we cannot determine if the device supports 3256 * CDDA. However, we still indicate the device is MMC 3257 * according to the successful response to the page 3258 * 0x2A mode sense request. 3259 */ 3260 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3261 "sd_set_mmc_caps: Mode Sense returned " 3262 "invalid block descriptor length\n"); 3263 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3264 return; 3265 } 3266 3267 /* See if read CDDA is supported */ 3268 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3269 bd_len); 3270 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3271 3272 /* See if writing DVD RAM is supported. */ 3273 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3274 if (un->un_f_dvdram_writable_device == TRUE) { 3275 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3276 return; 3277 } 3278 3279 /* 3280 * If the device presents DVD or CD capabilities in the mode 3281 * page, we can return here since a RRD will not have 3282 * these capabilities. 3283 */ 3284 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3285 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3286 return; 3287 } 3288 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3289 3290 /* 3291 * If un->un_f_dvdram_writable_device is still FALSE, 3292 * check for a Removable Rigid Disk (RRD). A RRD 3293 * device is identified by the features RANDOM_WRITABLE and 3294 * HARDWARE_DEFECT_MANAGEMENT. 3295 */ 3296 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3297 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3298 3299 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3300 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3301 RANDOM_WRITABLE); 3302 if (rtn != 0) { 3303 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3304 kmem_free(rqbuf_rw, SENSE_LENGTH); 3305 return; 3306 } 3307 3308 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3309 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3310 3311 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3312 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3313 HARDWARE_DEFECT_MANAGEMENT); 3314 if (rtn == 0) { 3315 /* 3316 * We have good information, check for random writable 3317 * and hardware defect features. 3318 */ 3319 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3320 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3321 un->un_f_dvdram_writable_device = TRUE; 3322 } 3323 } 3324 3325 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3326 kmem_free(rqbuf_rw, SENSE_LENGTH); 3327 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3328 kmem_free(rqbuf_hd, SENSE_LENGTH); 3329 } 3330 3331 /* 3332 * Function: sd_check_for_writable_cd 3333 * 3334 * Description: This routine determines if the media in the device is 3335 * writable or not. It uses the get configuration command (0x46) 3336 * to determine if the media is writable 3337 * 3338 * Arguments: un - driver soft state (unit) structure 3339 * 3340 * Context: Never called at interrupt context. 3341 */ 3342 3343 static void 3344 sd_check_for_writable_cd(struct sd_lun *un) 3345 { 3346 struct uscsi_cmd com; 3347 uchar_t *out_data; 3348 uchar_t *rqbuf; 3349 int rtn; 3350 uchar_t *out_data_rw, *out_data_hd; 3351 uchar_t *rqbuf_rw, *rqbuf_hd; 3352 struct mode_header_grp2 *sense_mhp; 3353 uchar_t *sense_page; 3354 caddr_t buf; 3355 int bd_len; 3356 int status; 3357 3358 ASSERT(un != NULL); 3359 ASSERT(mutex_owned(SD_MUTEX(un))); 3360 3361 /* 3362 * Initialize the writable media to false, if configuration info. 3363 * tells us otherwise then only we will set it. 3364 */ 3365 un->un_f_mmc_writable_media = FALSE; 3366 mutex_exit(SD_MUTEX(un)); 3367 3368 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3369 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3370 3371 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3372 out_data, SD_PROFILE_HEADER_LEN); 3373 3374 mutex_enter(SD_MUTEX(un)); 3375 if (rtn == 0) { 3376 /* 3377 * We have good information, check for writable DVD. 3378 */ 3379 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3380 un->un_f_mmc_writable_media = TRUE; 3381 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3382 kmem_free(rqbuf, SENSE_LENGTH); 3383 return; 3384 } 3385 } 3386 3387 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3388 kmem_free(rqbuf, SENSE_LENGTH); 3389 3390 /* 3391 * Determine if this is a RRD type device. 3392 */ 3393 mutex_exit(SD_MUTEX(un)); 3394 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3395 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3396 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3397 mutex_enter(SD_MUTEX(un)); 3398 if (status != 0) { 3399 /* command failed; just return */ 3400 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3401 return; 3402 } 3403 3404 /* Get to the page data */ 3405 sense_mhp = (struct mode_header_grp2 *)buf; 3406 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3407 if (bd_len > MODE_BLK_DESC_LENGTH) { 3408 /* 3409 * We did not get back the expected block descriptor length so 3410 * we cannot check the mode page. 3411 */ 3412 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3413 "sd_check_for_writable_cd: Mode Sense returned " 3414 "invalid block descriptor length\n"); 3415 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3416 return; 3417 } 3418 3419 /* 3420 * If the device presents DVD or CD capabilities in the mode 3421 * page, we can return here since a RRD device will not have 3422 * these capabilities. 3423 */ 3424 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3425 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3426 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3427 return; 3428 } 3429 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3430 3431 /* 3432 * If un->un_f_mmc_writable_media is still FALSE, 3433 * check for RRD type media. A RRD device is identified 3434 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3435 */ 3436 mutex_exit(SD_MUTEX(un)); 3437 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3438 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3439 3440 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3441 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3442 RANDOM_WRITABLE); 3443 if (rtn != 0) { 3444 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3445 kmem_free(rqbuf_rw, SENSE_LENGTH); 3446 mutex_enter(SD_MUTEX(un)); 3447 return; 3448 } 3449 3450 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3451 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3452 3453 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3454 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3455 HARDWARE_DEFECT_MANAGEMENT); 3456 mutex_enter(SD_MUTEX(un)); 3457 if (rtn == 0) { 3458 /* 3459 * We have good information, check for random writable 3460 * and hardware defect features as current. 3461 */ 3462 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3463 (out_data_rw[10] & 0x1) && 3464 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3465 (out_data_hd[10] & 0x1)) { 3466 un->un_f_mmc_writable_media = TRUE; 3467 } 3468 } 3469 3470 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3471 kmem_free(rqbuf_rw, SENSE_LENGTH); 3472 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3473 kmem_free(rqbuf_hd, SENSE_LENGTH); 3474 } 3475 3476 /* 3477 * Function: sd_read_unit_properties 3478 * 3479 * Description: The following implements a property lookup mechanism. 3480 * Properties for particular disks (keyed on vendor, model 3481 * and rev numbers) are sought in the sd.conf file via 3482 * sd_process_sdconf_file(), and if not found there, are 3483 * looked for in a list hardcoded in this driver via 3484 * sd_process_sdconf_table() Once located the properties 3485 * are used to update the driver unit structure. 3486 * 3487 * Arguments: un - driver soft state (unit) structure 3488 */ 3489 3490 static void 3491 sd_read_unit_properties(struct sd_lun *un) 3492 { 3493 /* 3494 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3495 * the "sd-config-list" property (from the sd.conf file) or if 3496 * there was not a match for the inquiry vid/pid. If this event 3497 * occurs the static driver configuration table is searched for 3498 * a match. 3499 */ 3500 ASSERT(un != NULL); 3501 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3502 sd_process_sdconf_table(un); 3503 } 3504 3505 /* check for LSI device */ 3506 sd_is_lsi(un); 3507 3508 3509 } 3510 3511 3512 /* 3513 * Function: sd_process_sdconf_file 3514 * 3515 * Description: Use ddi_getlongprop to obtain the properties from the 3516 * driver's config file (ie, sd.conf) and update the driver 3517 * soft state structure accordingly. 3518 * 3519 * Arguments: un - driver soft state (unit) structure 3520 * 3521 * Return Code: SD_SUCCESS - The properties were successfully set according 3522 * to the driver configuration file. 3523 * SD_FAILURE - The driver config list was not obtained or 3524 * there was no vid/pid match. This indicates that 3525 * the static config table should be used. 3526 * 3527 * The config file has a property, "sd-config-list", which consists of 3528 * one or more duplets as follows: 3529 * 3530 * sd-config-list= 3531 * <duplet>, 3532 * [<duplet>,] 3533 * [<duplet>]; 3534 * 3535 * The structure of each duplet is as follows: 3536 * 3537 * <duplet>:= <vid+pid>,<data-property-name_list> 3538 * 3539 * The first entry of the duplet is the device ID string (the concatenated 3540 * vid & pid; not to be confused with a device_id). This is defined in 3541 * the same way as in the sd_disk_table. 3542 * 3543 * The second part of the duplet is a string that identifies a 3544 * data-property-name-list. The data-property-name-list is defined as 3545 * follows: 3546 * 3547 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3548 * 3549 * The syntax of <data-property-name> depends on the <version> field. 3550 * 3551 * If version = SD_CONF_VERSION_1 we have the following syntax: 3552 * 3553 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3554 * 3555 * where the prop0 value will be used to set prop0 if bit0 set in the 3556 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3557 * 3558 */ 3559 3560 static int 3561 sd_process_sdconf_file(struct sd_lun *un) 3562 { 3563 char *config_list = NULL; 3564 int config_list_len; 3565 int len; 3566 int dupletlen = 0; 3567 char *vidptr; 3568 int vidlen; 3569 char *dnlist_ptr; 3570 char *dataname_ptr; 3571 int dnlist_len; 3572 int dataname_len; 3573 int *data_list; 3574 int data_list_len; 3575 int rval = SD_FAILURE; 3576 int i; 3577 3578 ASSERT(un != NULL); 3579 3580 /* Obtain the configuration list associated with the .conf file */ 3581 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3582 sd_config_list, (caddr_t)&config_list, &config_list_len) 3583 != DDI_PROP_SUCCESS) { 3584 return (SD_FAILURE); 3585 } 3586 3587 /* 3588 * Compare vids in each duplet to the inquiry vid - if a match is 3589 * made, get the data value and update the soft state structure 3590 * accordingly. 3591 * 3592 * Note: This algorithm is complex and difficult to maintain. It should 3593 * be replaced with a more robust implementation. 3594 */ 3595 for (len = config_list_len, vidptr = config_list; len > 0; 3596 vidptr += dupletlen, len -= dupletlen) { 3597 /* 3598 * Note: The assumption here is that each vid entry is on 3599 * a unique line from its associated duplet. 3600 */ 3601 vidlen = dupletlen = (int)strlen(vidptr); 3602 if ((vidlen == 0) || 3603 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3604 dupletlen++; 3605 continue; 3606 } 3607 3608 /* 3609 * dnlist contains 1 or more blank separated 3610 * data-property-name entries 3611 */ 3612 dnlist_ptr = vidptr + vidlen + 1; 3613 dnlist_len = (int)strlen(dnlist_ptr); 3614 dupletlen += dnlist_len + 2; 3615 3616 /* 3617 * Set a pointer for the first data-property-name 3618 * entry in the list 3619 */ 3620 dataname_ptr = dnlist_ptr; 3621 dataname_len = 0; 3622 3623 /* 3624 * Loop through all data-property-name entries in the 3625 * data-property-name-list setting the properties for each. 3626 */ 3627 while (dataname_len < dnlist_len) { 3628 int version; 3629 3630 /* 3631 * Determine the length of the current 3632 * data-property-name entry by indexing until a 3633 * blank or NULL is encountered. When the space is 3634 * encountered reset it to a NULL for compliance 3635 * with ddi_getlongprop(). 3636 */ 3637 for (i = 0; ((dataname_ptr[i] != ' ') && 3638 (dataname_ptr[i] != '\0')); i++) { 3639 ; 3640 } 3641 3642 dataname_len += i; 3643 /* If not null terminated, Make it so */ 3644 if (dataname_ptr[i] == ' ') { 3645 dataname_ptr[i] = '\0'; 3646 } 3647 dataname_len++; 3648 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3649 "sd_process_sdconf_file: disk:%s, data:%s\n", 3650 vidptr, dataname_ptr); 3651 3652 /* Get the data list */ 3653 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3654 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3655 != DDI_PROP_SUCCESS) { 3656 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3657 "sd_process_sdconf_file: data property (%s)" 3658 " has no value\n", dataname_ptr); 3659 dataname_ptr = dnlist_ptr + dataname_len; 3660 continue; 3661 } 3662 3663 version = data_list[0]; 3664 3665 if (version == SD_CONF_VERSION_1) { 3666 sd_tunables values; 3667 3668 /* Set the properties */ 3669 if (sd_chk_vers1_data(un, data_list[1], 3670 &data_list[2], data_list_len, dataname_ptr) 3671 == SD_SUCCESS) { 3672 sd_get_tunables_from_conf(un, 3673 data_list[1], &data_list[2], 3674 &values); 3675 sd_set_vers1_properties(un, 3676 data_list[1], &values); 3677 rval = SD_SUCCESS; 3678 } else { 3679 rval = SD_FAILURE; 3680 } 3681 } else { 3682 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3683 "data property %s version 0x%x is invalid.", 3684 dataname_ptr, version); 3685 rval = SD_FAILURE; 3686 } 3687 kmem_free(data_list, data_list_len); 3688 dataname_ptr = dnlist_ptr + dataname_len; 3689 } 3690 } 3691 3692 /* free up the memory allocated by ddi_getlongprop */ 3693 if (config_list) { 3694 kmem_free(config_list, config_list_len); 3695 } 3696 3697 return (rval); 3698 } 3699 3700 /* 3701 * Function: sd_get_tunables_from_conf() 3702 * 3703 * 3704 * This function reads the data list from the sd.conf file and pulls 3705 * the values that can have numeric values as arguments and places 3706 * the values in the apropriate sd_tunables member. 3707 * Since the order of the data list members varies across platforms 3708 * This function reads them from the data list in a platform specific 3709 * order and places them into the correct sd_tunable member that is 3710 * a consistant across all platforms. 3711 */ 3712 static void 3713 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3714 sd_tunables *values) 3715 { 3716 int i; 3717 int mask; 3718 3719 bzero(values, sizeof (sd_tunables)); 3720 3721 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3722 3723 mask = 1 << i; 3724 if (mask > flags) { 3725 break; 3726 } 3727 3728 switch (mask & flags) { 3729 case 0: /* This mask bit not set in flags */ 3730 continue; 3731 case SD_CONF_BSET_THROTTLE: 3732 values->sdt_throttle = data_list[i]; 3733 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3734 "sd_get_tunables_from_conf: throttle = %d\n", 3735 values->sdt_throttle); 3736 break; 3737 case SD_CONF_BSET_CTYPE: 3738 values->sdt_ctype = data_list[i]; 3739 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3740 "sd_get_tunables_from_conf: ctype = %d\n", 3741 values->sdt_ctype); 3742 break; 3743 case SD_CONF_BSET_NRR_COUNT: 3744 values->sdt_not_rdy_retries = data_list[i]; 3745 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3746 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3747 values->sdt_not_rdy_retries); 3748 break; 3749 case SD_CONF_BSET_BSY_RETRY_COUNT: 3750 values->sdt_busy_retries = data_list[i]; 3751 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3752 "sd_get_tunables_from_conf: busy_retries = %d\n", 3753 values->sdt_busy_retries); 3754 break; 3755 case SD_CONF_BSET_RST_RETRIES: 3756 values->sdt_reset_retries = data_list[i]; 3757 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3758 "sd_get_tunables_from_conf: reset_retries = %d\n", 3759 values->sdt_reset_retries); 3760 break; 3761 case SD_CONF_BSET_RSV_REL_TIME: 3762 values->sdt_reserv_rel_time = data_list[i]; 3763 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3764 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3765 values->sdt_reserv_rel_time); 3766 break; 3767 case SD_CONF_BSET_MIN_THROTTLE: 3768 values->sdt_min_throttle = data_list[i]; 3769 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3770 "sd_get_tunables_from_conf: min_throttle = %d\n", 3771 values->sdt_min_throttle); 3772 break; 3773 case SD_CONF_BSET_DISKSORT_DISABLED: 3774 values->sdt_disk_sort_dis = data_list[i]; 3775 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3776 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3777 values->sdt_disk_sort_dis); 3778 break; 3779 case SD_CONF_BSET_LUN_RESET_ENABLED: 3780 values->sdt_lun_reset_enable = data_list[i]; 3781 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3782 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3783 "\n", values->sdt_lun_reset_enable); 3784 break; 3785 } 3786 } 3787 } 3788 3789 /* 3790 * Function: sd_process_sdconf_table 3791 * 3792 * Description: Search the static configuration table for a match on the 3793 * inquiry vid/pid and update the driver soft state structure 3794 * according to the table property values for the device. 3795 * 3796 * The form of a configuration table entry is: 3797 * <vid+pid>,<flags>,<property-data> 3798 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3799 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3800 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3801 * 3802 * Arguments: un - driver soft state (unit) structure 3803 */ 3804 3805 static void 3806 sd_process_sdconf_table(struct sd_lun *un) 3807 { 3808 char *id = NULL; 3809 int table_index; 3810 int idlen; 3811 3812 ASSERT(un != NULL); 3813 for (table_index = 0; table_index < sd_disk_table_size; 3814 table_index++) { 3815 id = sd_disk_table[table_index].device_id; 3816 idlen = strlen(id); 3817 if (idlen == 0) { 3818 continue; 3819 } 3820 3821 /* 3822 * The static configuration table currently does not 3823 * implement version 10 properties. Additionally, 3824 * multiple data-property-name entries are not 3825 * implemented in the static configuration table. 3826 */ 3827 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3828 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3829 "sd_process_sdconf_table: disk %s\n", id); 3830 sd_set_vers1_properties(un, 3831 sd_disk_table[table_index].flags, 3832 sd_disk_table[table_index].properties); 3833 break; 3834 } 3835 } 3836 } 3837 3838 3839 /* 3840 * Function: sd_sdconf_id_match 3841 * 3842 * Description: This local function implements a case sensitive vid/pid 3843 * comparison as well as the boundary cases of wild card and 3844 * multiple blanks. 3845 * 3846 * Note: An implicit assumption made here is that the scsi 3847 * inquiry structure will always keep the vid, pid and 3848 * revision strings in consecutive sequence, so they can be 3849 * read as a single string. If this assumption is not the 3850 * case, a separate string, to be used for the check, needs 3851 * to be built with these strings concatenated. 3852 * 3853 * Arguments: un - driver soft state (unit) structure 3854 * id - table or config file vid/pid 3855 * idlen - length of the vid/pid (bytes) 3856 * 3857 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3858 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3859 */ 3860 3861 static int 3862 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3863 { 3864 struct scsi_inquiry *sd_inq; 3865 int rval = SD_SUCCESS; 3866 3867 ASSERT(un != NULL); 3868 sd_inq = un->un_sd->sd_inq; 3869 ASSERT(id != NULL); 3870 3871 /* 3872 * We use the inq_vid as a pointer to a buffer containing the 3873 * vid and pid and use the entire vid/pid length of the table 3874 * entry for the comparison. This works because the inq_pid 3875 * data member follows inq_vid in the scsi_inquiry structure. 3876 */ 3877 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3878 /* 3879 * The user id string is compared to the inquiry vid/pid 3880 * using a case insensitive comparison and ignoring 3881 * multiple spaces. 3882 */ 3883 rval = sd_blank_cmp(un, id, idlen); 3884 if (rval != SD_SUCCESS) { 3885 /* 3886 * User id strings that start and end with a "*" 3887 * are a special case. These do not have a 3888 * specific vendor, and the product string can 3889 * appear anywhere in the 16 byte PID portion of 3890 * the inquiry data. This is a simple strstr() 3891 * type search for the user id in the inquiry data. 3892 */ 3893 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3894 char *pidptr = &id[1]; 3895 int i; 3896 int j; 3897 int pidstrlen = idlen - 2; 3898 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3899 pidstrlen; 3900 3901 if (j < 0) { 3902 return (SD_FAILURE); 3903 } 3904 for (i = 0; i < j; i++) { 3905 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3906 pidptr, pidstrlen) == 0) { 3907 rval = SD_SUCCESS; 3908 break; 3909 } 3910 } 3911 } 3912 } 3913 } 3914 return (rval); 3915 } 3916 3917 3918 /* 3919 * Function: sd_blank_cmp 3920 * 3921 * Description: If the id string starts and ends with a space, treat 3922 * multiple consecutive spaces as equivalent to a single 3923 * space. For example, this causes a sd_disk_table entry 3924 * of " NEC CDROM " to match a device's id string of 3925 * "NEC CDROM". 3926 * 3927 * Note: The success exit condition for this routine is if 3928 * the pointer to the table entry is '\0' and the cnt of 3929 * the inquiry length is zero. This will happen if the inquiry 3930 * string returned by the device is padded with spaces to be 3931 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3932 * SCSI spec states that the inquiry string is to be padded with 3933 * spaces. 3934 * 3935 * Arguments: un - driver soft state (unit) structure 3936 * id - table or config file vid/pid 3937 * idlen - length of the vid/pid (bytes) 3938 * 3939 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3940 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3941 */ 3942 3943 static int 3944 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3945 { 3946 char *p1; 3947 char *p2; 3948 int cnt; 3949 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3950 sizeof (SD_INQUIRY(un)->inq_pid); 3951 3952 ASSERT(un != NULL); 3953 p2 = un->un_sd->sd_inq->inq_vid; 3954 ASSERT(id != NULL); 3955 p1 = id; 3956 3957 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3958 /* 3959 * Note: string p1 is terminated by a NUL but string p2 3960 * isn't. The end of p2 is determined by cnt. 3961 */ 3962 for (;;) { 3963 /* skip over any extra blanks in both strings */ 3964 while ((*p1 != '\0') && (*p1 == ' ')) { 3965 p1++; 3966 } 3967 while ((cnt != 0) && (*p2 == ' ')) { 3968 p2++; 3969 cnt--; 3970 } 3971 3972 /* compare the two strings */ 3973 if ((cnt == 0) || 3974 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3975 break; 3976 } 3977 while ((cnt > 0) && 3978 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3979 p1++; 3980 p2++; 3981 cnt--; 3982 } 3983 } 3984 } 3985 3986 /* return SD_SUCCESS if both strings match */ 3987 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3988 } 3989 3990 3991 /* 3992 * Function: sd_chk_vers1_data 3993 * 3994 * Description: Verify the version 1 device properties provided by the 3995 * user via the configuration file 3996 * 3997 * Arguments: un - driver soft state (unit) structure 3998 * flags - integer mask indicating properties to be set 3999 * prop_list - integer list of property values 4000 * list_len - length of user provided data 4001 * 4002 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4003 * SD_FAILURE - Indicates the user provided data is invalid 4004 */ 4005 4006 static int 4007 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4008 int list_len, char *dataname_ptr) 4009 { 4010 int i; 4011 int mask = 1; 4012 int index = 0; 4013 4014 ASSERT(un != NULL); 4015 4016 /* Check for a NULL property name and list */ 4017 if (dataname_ptr == NULL) { 4018 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4019 "sd_chk_vers1_data: NULL data property name."); 4020 return (SD_FAILURE); 4021 } 4022 if (prop_list == NULL) { 4023 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4024 "sd_chk_vers1_data: %s NULL data property list.", 4025 dataname_ptr); 4026 return (SD_FAILURE); 4027 } 4028 4029 /* Display a warning if undefined bits are set in the flags */ 4030 if (flags & ~SD_CONF_BIT_MASK) { 4031 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4032 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4033 "Properties not set.", 4034 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4035 return (SD_FAILURE); 4036 } 4037 4038 /* 4039 * Verify the length of the list by identifying the highest bit set 4040 * in the flags and validating that the property list has a length 4041 * up to the index of this bit. 4042 */ 4043 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4044 if (flags & mask) { 4045 index++; 4046 } 4047 mask = 1 << i; 4048 } 4049 if ((list_len / sizeof (int)) < (index + 2)) { 4050 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4051 "sd_chk_vers1_data: " 4052 "Data property list %s size is incorrect. " 4053 "Properties not set.", dataname_ptr); 4054 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4055 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4056 return (SD_FAILURE); 4057 } 4058 return (SD_SUCCESS); 4059 } 4060 4061 4062 /* 4063 * Function: sd_set_vers1_properties 4064 * 4065 * Description: Set version 1 device properties based on a property list 4066 * retrieved from the driver configuration file or static 4067 * configuration table. Version 1 properties have the format: 4068 * 4069 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4070 * 4071 * where the prop0 value will be used to set prop0 if bit0 4072 * is set in the flags 4073 * 4074 * Arguments: un - driver soft state (unit) structure 4075 * flags - integer mask indicating properties to be set 4076 * prop_list - integer list of property values 4077 */ 4078 4079 static void 4080 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4081 { 4082 ASSERT(un != NULL); 4083 4084 /* 4085 * Set the flag to indicate cache is to be disabled. An attempt 4086 * to disable the cache via sd_cache_control() will be made 4087 * later during attach once the basic initialization is complete. 4088 */ 4089 if (flags & SD_CONF_BSET_NOCACHE) { 4090 un->un_f_opt_disable_cache = TRUE; 4091 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4092 "sd_set_vers1_properties: caching disabled flag set\n"); 4093 } 4094 4095 /* CD-specific configuration parameters */ 4096 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4097 un->un_f_cfg_playmsf_bcd = TRUE; 4098 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4099 "sd_set_vers1_properties: playmsf_bcd set\n"); 4100 } 4101 if (flags & SD_CONF_BSET_READSUB_BCD) { 4102 un->un_f_cfg_readsub_bcd = TRUE; 4103 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4104 "sd_set_vers1_properties: readsub_bcd set\n"); 4105 } 4106 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4107 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4108 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4109 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4110 } 4111 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4112 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4113 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4114 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4115 } 4116 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4117 un->un_f_cfg_no_read_header = TRUE; 4118 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4119 "sd_set_vers1_properties: no_read_header set\n"); 4120 } 4121 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4122 un->un_f_cfg_read_cd_xd4 = TRUE; 4123 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4124 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4125 } 4126 4127 /* Support for devices which do not have valid/unique serial numbers */ 4128 if (flags & SD_CONF_BSET_FAB_DEVID) { 4129 un->un_f_opt_fab_devid = TRUE; 4130 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4131 "sd_set_vers1_properties: fab_devid bit set\n"); 4132 } 4133 4134 /* Support for user throttle configuration */ 4135 if (flags & SD_CONF_BSET_THROTTLE) { 4136 ASSERT(prop_list != NULL); 4137 un->un_saved_throttle = un->un_throttle = 4138 prop_list->sdt_throttle; 4139 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4140 "sd_set_vers1_properties: throttle set to %d\n", 4141 prop_list->sdt_throttle); 4142 } 4143 4144 /* Set the per disk retry count according to the conf file or table. */ 4145 if (flags & SD_CONF_BSET_NRR_COUNT) { 4146 ASSERT(prop_list != NULL); 4147 if (prop_list->sdt_not_rdy_retries) { 4148 un->un_notready_retry_count = 4149 prop_list->sdt_not_rdy_retries; 4150 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4151 "sd_set_vers1_properties: not ready retry count" 4152 " set to %d\n", un->un_notready_retry_count); 4153 } 4154 } 4155 4156 /* The controller type is reported for generic disk driver ioctls */ 4157 if (flags & SD_CONF_BSET_CTYPE) { 4158 ASSERT(prop_list != NULL); 4159 switch (prop_list->sdt_ctype) { 4160 case CTYPE_CDROM: 4161 un->un_ctype = prop_list->sdt_ctype; 4162 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4163 "sd_set_vers1_properties: ctype set to " 4164 "CTYPE_CDROM\n"); 4165 break; 4166 case CTYPE_CCS: 4167 un->un_ctype = prop_list->sdt_ctype; 4168 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4169 "sd_set_vers1_properties: ctype set to " 4170 "CTYPE_CCS\n"); 4171 break; 4172 case CTYPE_ROD: /* RW optical */ 4173 un->un_ctype = prop_list->sdt_ctype; 4174 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4175 "sd_set_vers1_properties: ctype set to " 4176 "CTYPE_ROD\n"); 4177 break; 4178 default: 4179 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4180 "sd_set_vers1_properties: Could not set " 4181 "invalid ctype value (%d)", 4182 prop_list->sdt_ctype); 4183 } 4184 } 4185 4186 /* Purple failover timeout */ 4187 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4188 ASSERT(prop_list != NULL); 4189 un->un_busy_retry_count = 4190 prop_list->sdt_busy_retries; 4191 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4192 "sd_set_vers1_properties: " 4193 "busy retry count set to %d\n", 4194 un->un_busy_retry_count); 4195 } 4196 4197 /* Purple reset retry count */ 4198 if (flags & SD_CONF_BSET_RST_RETRIES) { 4199 ASSERT(prop_list != NULL); 4200 un->un_reset_retry_count = 4201 prop_list->sdt_reset_retries; 4202 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4203 "sd_set_vers1_properties: " 4204 "reset retry count set to %d\n", 4205 un->un_reset_retry_count); 4206 } 4207 4208 /* Purple reservation release timeout */ 4209 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4210 ASSERT(prop_list != NULL); 4211 un->un_reserve_release_time = 4212 prop_list->sdt_reserv_rel_time; 4213 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4214 "sd_set_vers1_properties: " 4215 "reservation release timeout set to %d\n", 4216 un->un_reserve_release_time); 4217 } 4218 4219 /* 4220 * Driver flag telling the driver to verify that no commands are pending 4221 * for a device before issuing a Test Unit Ready. This is a workaround 4222 * for a firmware bug in some Seagate eliteI drives. 4223 */ 4224 if (flags & SD_CONF_BSET_TUR_CHECK) { 4225 un->un_f_cfg_tur_check = TRUE; 4226 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4227 "sd_set_vers1_properties: tur queue check set\n"); 4228 } 4229 4230 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4231 un->un_min_throttle = prop_list->sdt_min_throttle; 4232 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4233 "sd_set_vers1_properties: min throttle set to %d\n", 4234 un->un_min_throttle); 4235 } 4236 4237 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4238 un->un_f_disksort_disabled = 4239 (prop_list->sdt_disk_sort_dis != 0) ? 4240 TRUE : FALSE; 4241 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4242 "sd_set_vers1_properties: disksort disabled " 4243 "flag set to %d\n", 4244 prop_list->sdt_disk_sort_dis); 4245 } 4246 4247 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4248 un->un_f_lun_reset_enabled = 4249 (prop_list->sdt_lun_reset_enable != 0) ? 4250 TRUE : FALSE; 4251 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4252 "sd_set_vers1_properties: lun reset enabled " 4253 "flag set to %d\n", 4254 prop_list->sdt_lun_reset_enable); 4255 } 4256 4257 /* 4258 * Validate the throttle values. 4259 * If any of the numbers are invalid, set everything to defaults. 4260 */ 4261 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4262 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4263 (un->un_min_throttle > un->un_throttle)) { 4264 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4265 un->un_min_throttle = sd_min_throttle; 4266 } 4267 } 4268 4269 /* 4270 * Function: sd_is_lsi() 4271 * 4272 * Description: Check for lsi devices, step throught the static device 4273 * table to match vid/pid. 4274 * 4275 * Args: un - ptr to sd_lun 4276 * 4277 * Notes: When creating new LSI property, need to add the new LSI property 4278 * to this function. 4279 */ 4280 static void 4281 sd_is_lsi(struct sd_lun *un) 4282 { 4283 char *id = NULL; 4284 int table_index; 4285 int idlen; 4286 void *prop; 4287 4288 ASSERT(un != NULL); 4289 for (table_index = 0; table_index < sd_disk_table_size; 4290 table_index++) { 4291 id = sd_disk_table[table_index].device_id; 4292 idlen = strlen(id); 4293 if (idlen == 0) { 4294 continue; 4295 } 4296 4297 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4298 prop = sd_disk_table[table_index].properties; 4299 if (prop == &lsi_properties || 4300 prop == &lsi_oem_properties || 4301 prop == &lsi_properties_scsi || 4302 prop == &symbios_properties) { 4303 un->un_f_cfg_is_lsi = TRUE; 4304 } 4305 break; 4306 } 4307 } 4308 } 4309 4310 4311 /* 4312 * The following routines support reading and interpretation of disk labels, 4313 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4314 * fdisk tables. 4315 */ 4316 4317 /* 4318 * Function: sd_validate_geometry 4319 * 4320 * Description: Read the label from the disk (if present). Update the unit's 4321 * geometry and vtoc information from the data in the label. 4322 * Verify that the label is valid. 4323 * 4324 * Arguments: un - driver soft state (unit) structure 4325 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4326 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4327 * to use the USCSI "direct" chain and bypass the normal 4328 * command waitq. 4329 * 4330 * Return Code: 0 - Successful completion 4331 * EINVAL - Invalid value in un->un_tgt_blocksize or 4332 * un->un_blockcount; or label on disk is corrupted 4333 * or unreadable. 4334 * EACCES - Reservation conflict at the device. 4335 * ENOMEM - Resource allocation error 4336 * ENOTSUP - geometry not applicable 4337 * 4338 * Context: Kernel thread only (can sleep). 4339 */ 4340 4341 static int 4342 sd_validate_geometry(struct sd_lun *un, int path_flag) 4343 { 4344 static char labelstring[128]; 4345 static char buf[256]; 4346 char *label = NULL; 4347 int label_error = 0; 4348 int gvalid = un->un_f_geometry_is_valid; 4349 int lbasize; 4350 uint_t capacity; 4351 int count; 4352 #if defined(__i386) || defined(__amd64) 4353 int forced_under_1t = 0; 4354 #endif 4355 4356 ASSERT(un != NULL); 4357 ASSERT(mutex_owned(SD_MUTEX(un))); 4358 4359 /* 4360 * If the required values are not valid, then try getting them 4361 * once via read capacity. If that fails, then fail this call. 4362 * This is necessary with the new mpxio failover behavior in 4363 * the T300 where we can get an attach for the inactive path 4364 * before the active path. The inactive path fails commands with 4365 * sense data of 02,04,88 which happens to the read capacity 4366 * before mpxio has had sufficient knowledge to know if it should 4367 * force a fail over or not. (Which it won't do at attach anyhow). 4368 * If the read capacity at attach time fails, un_tgt_blocksize and 4369 * un_blockcount won't be valid. 4370 */ 4371 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4372 (un->un_f_blockcount_is_valid != TRUE)) { 4373 uint64_t cap; 4374 uint32_t lbasz; 4375 int rval; 4376 4377 mutex_exit(SD_MUTEX(un)); 4378 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4379 &lbasz, SD_PATH_DIRECT); 4380 mutex_enter(SD_MUTEX(un)); 4381 if (rval == 0) { 4382 /* 4383 * The following relies on 4384 * sd_send_scsi_READ_CAPACITY never 4385 * returning 0 for capacity and/or lbasize. 4386 */ 4387 sd_update_block_info(un, lbasz, cap); 4388 } 4389 4390 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4391 (un->un_f_blockcount_is_valid != TRUE)) { 4392 return (EINVAL); 4393 } 4394 } 4395 4396 /* 4397 * Copy the lbasize and capacity so that if they're reset while we're 4398 * not holding the SD_MUTEX, we will continue to use valid values 4399 * after the SD_MUTEX is reacquired. (4119659) 4400 */ 4401 lbasize = un->un_tgt_blocksize; 4402 capacity = un->un_blockcount; 4403 4404 #if defined(_SUNOS_VTOC_16) 4405 /* 4406 * Set up the "whole disk" fdisk partition; this should always 4407 * exist, regardless of whether the disk contains an fdisk table 4408 * or vtoc. 4409 */ 4410 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4411 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4412 #endif 4413 4414 /* 4415 * Refresh the logical and physical geometry caches. 4416 * (data from MODE SENSE format/rigid disk geometry pages, 4417 * and scsi_ifgetcap("geometry"). 4418 */ 4419 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4420 4421 label_error = sd_use_efi(un, path_flag); 4422 if (label_error == 0) { 4423 /* found a valid EFI label */ 4424 SD_TRACE(SD_LOG_IO_PARTITION, un, 4425 "sd_validate_geometry: found EFI label\n"); 4426 un->un_solaris_offset = 0; 4427 un->un_solaris_size = capacity; 4428 return (ENOTSUP); 4429 } 4430 if (un->un_blockcount > DK_MAX_BLOCKS) { 4431 if (label_error == ESRCH) { 4432 /* 4433 * they've configured a LUN over 1TB, but used 4434 * format.dat to restrict format's view of the 4435 * capacity to be under 1TB 4436 */ 4437 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4438 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4439 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4440 "size to be < 1TB or relabel the disk with an EFI label"); 4441 #if defined(__i386) || defined(__amd64) 4442 forced_under_1t = 1; 4443 #endif 4444 } else { 4445 /* unlabeled disk over 1TB */ 4446 #if defined(__i386) || defined(__amd64) 4447 /* 4448 * Refer to comments on off-by-1 at the head of the file 4449 * A 1TB disk was treated as (1T - 512)B in the past, 4450 * thus, it might have valid solaris partition. We 4451 * will return ENOTSUP later only if this disk has no 4452 * valid solaris partition. 4453 */ 4454 if ((un->un_tgt_blocksize != un->un_sys_blocksize) || 4455 (un->un_blockcount - 1 > DK_MAX_BLOCKS) || 4456 un->un_f_has_removable_media || 4457 un->un_f_is_hotpluggable) 4458 #endif 4459 return (ENOTSUP); 4460 } 4461 } 4462 label_error = 0; 4463 4464 /* 4465 * at this point it is either labeled with a VTOC or it is 4466 * under 1TB (<= 1TB actually for off-by-1) 4467 */ 4468 if (un->un_f_vtoc_label_supported) { 4469 struct dk_label *dkl; 4470 offset_t dkl1; 4471 offset_t label_addr, real_addr; 4472 int rval; 4473 size_t buffer_size; 4474 4475 /* 4476 * Note: This will set up un->un_solaris_size and 4477 * un->un_solaris_offset. 4478 */ 4479 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4480 case SD_CMD_RESERVATION_CONFLICT: 4481 ASSERT(mutex_owned(SD_MUTEX(un))); 4482 return (EACCES); 4483 case SD_CMD_FAILURE: 4484 ASSERT(mutex_owned(SD_MUTEX(un))); 4485 return (ENOMEM); 4486 } 4487 4488 if (un->un_solaris_size <= DK_LABEL_LOC) { 4489 4490 #if defined(__i386) || defined(__amd64) 4491 /* 4492 * Refer to comments on off-by-1 at the head of the file 4493 * This is for 1TB disk only. Since that there is no 4494 * solaris partitions, return ENOTSUP as we do for 4495 * >1TB disk. 4496 */ 4497 if (un->un_blockcount > DK_MAX_BLOCKS) 4498 return (ENOTSUP); 4499 #endif 4500 /* 4501 * Found fdisk table but no Solaris partition entry, 4502 * so don't call sd_uselabel() and don't create 4503 * a default label. 4504 */ 4505 label_error = 0; 4506 un->un_f_geometry_is_valid = TRUE; 4507 goto no_solaris_partition; 4508 } 4509 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4510 4511 #if defined(__i386) || defined(__amd64) 4512 /* 4513 * Refer to comments on off-by-1 at the head of the file 4514 * Now, this 1TB disk has valid solaris partition. It 4515 * must be created by previous sd driver, we have to 4516 * treat it as (1T-512)B. 4517 */ 4518 if ((un->un_blockcount > DK_MAX_BLOCKS) && 4519 (forced_under_1t != 1)) { 4520 un->un_f_capacity_adjusted = 1; 4521 un->un_blockcount = DK_MAX_BLOCKS; 4522 un->un_map[P0_RAW_DISK].dkl_nblk = DK_MAX_BLOCKS; 4523 4524 /* 4525 * Refer to sd_read_fdisk, when there is no 4526 * fdisk partition table, un_solaris_size is 4527 * set to disk's capacity. In this case, we 4528 * need to adjust it 4529 */ 4530 if (un->un_solaris_size > DK_MAX_BLOCKS) 4531 un->un_solaris_size = DK_MAX_BLOCKS; 4532 sd_resync_geom_caches(un, DK_MAX_BLOCKS, 4533 lbasize, path_flag); 4534 } 4535 #endif 4536 4537 /* 4538 * sys_blocksize != tgt_blocksize, need to re-adjust 4539 * blkno and save the index to beginning of dk_label 4540 */ 4541 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4542 buffer_size = SD_REQBYTES2TGTBYTES(un, 4543 sizeof (struct dk_label)); 4544 4545 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4546 "label_addr: 0x%x allocation size: 0x%x\n", 4547 label_addr, buffer_size); 4548 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4549 if (dkl == NULL) { 4550 return (ENOMEM); 4551 } 4552 4553 mutex_exit(SD_MUTEX(un)); 4554 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4555 path_flag); 4556 mutex_enter(SD_MUTEX(un)); 4557 4558 switch (rval) { 4559 case 0: 4560 /* 4561 * sd_uselabel will establish that the geometry 4562 * is valid. 4563 * For sys_blocksize != tgt_blocksize, need 4564 * to index into the beginning of dk_label 4565 */ 4566 dkl1 = (daddr_t)dkl 4567 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4568 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4569 path_flag) != SD_LABEL_IS_VALID) { 4570 label_error = EINVAL; 4571 } 4572 break; 4573 case EACCES: 4574 label_error = EACCES; 4575 break; 4576 default: 4577 label_error = EINVAL; 4578 break; 4579 } 4580 4581 kmem_free(dkl, buffer_size); 4582 4583 #if defined(_SUNOS_VTOC_8) 4584 label = (char *)un->un_asciilabel; 4585 #elif defined(_SUNOS_VTOC_16) 4586 label = (char *)un->un_vtoc.v_asciilabel; 4587 #else 4588 #error "No VTOC format defined." 4589 #endif 4590 } 4591 4592 /* 4593 * If a valid label was not found, AND if no reservation conflict 4594 * was detected, then go ahead and create a default label (4069506). 4595 */ 4596 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4597 if (un->un_f_geometry_is_valid == FALSE) { 4598 sd_build_default_label(un); 4599 } 4600 label_error = 0; 4601 } 4602 4603 no_solaris_partition: 4604 if ((!un->un_f_has_removable_media || 4605 (un->un_f_has_removable_media && 4606 un->un_mediastate == DKIO_EJECTED)) && 4607 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4608 /* 4609 * Print out a message indicating who and what we are. 4610 * We do this only when we happen to really validate the 4611 * geometry. We may call sd_validate_geometry() at other 4612 * times, e.g., ioctl()'s like Get VTOC in which case we 4613 * don't want to print the label. 4614 * If the geometry is valid, print the label string, 4615 * else print vendor and product info, if available 4616 */ 4617 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4618 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4619 } else { 4620 mutex_enter(&sd_label_mutex); 4621 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4622 labelstring); 4623 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4624 &labelstring[64]); 4625 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4626 labelstring, &labelstring[64]); 4627 if (un->un_f_blockcount_is_valid == TRUE) { 4628 (void) sprintf(&buf[strlen(buf)], 4629 ", %llu %u byte blocks\n", 4630 (longlong_t)un->un_blockcount, 4631 un->un_tgt_blocksize); 4632 } else { 4633 (void) sprintf(&buf[strlen(buf)], 4634 ", (unknown capacity)\n"); 4635 } 4636 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4637 mutex_exit(&sd_label_mutex); 4638 } 4639 } 4640 4641 #if defined(_SUNOS_VTOC_16) 4642 /* 4643 * If we have valid geometry, set up the remaining fdisk partitions. 4644 * Note that dkl_cylno is not used for the fdisk map entries, so 4645 * we set it to an entirely bogus value. 4646 */ 4647 for (count = 0; count < FD_NUMPART; count++) { 4648 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4649 un->un_map[FDISK_P1 + count].dkl_nblk = 4650 un->un_fmap[count].fmap_nblk; 4651 4652 un->un_offset[FDISK_P1 + count] = 4653 un->un_fmap[count].fmap_start; 4654 } 4655 #endif 4656 4657 for (count = 0; count < NDKMAP; count++) { 4658 #if defined(_SUNOS_VTOC_8) 4659 struct dk_map *lp = &un->un_map[count]; 4660 un->un_offset[count] = 4661 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4662 #elif defined(_SUNOS_VTOC_16) 4663 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4664 4665 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4666 #else 4667 #error "No VTOC format defined." 4668 #endif 4669 } 4670 4671 /* 4672 * For VTOC labeled disk, create and set the partition stats 4673 * at attach time, update the stats according to dynamic 4674 * partition changes during running time. 4675 */ 4676 if (label_error == 0 && un->un_f_pkstats_enabled) { 4677 sd_set_pstats(un); 4678 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4679 "un:0x%p pstats created and set, or updated\n", un); 4680 } 4681 4682 return (label_error); 4683 } 4684 4685 4686 #if defined(_SUNOS_VTOC_16) 4687 /* 4688 * Macro: MAX_BLKS 4689 * 4690 * This macro is used for table entries where we need to have the largest 4691 * possible sector value for that head & SPT (sectors per track) 4692 * combination. Other entries for some smaller disk sizes are set by 4693 * convention to match those used by X86 BIOS usage. 4694 */ 4695 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4696 4697 /* 4698 * Function: sd_convert_geometry 4699 * 4700 * Description: Convert physical geometry into a dk_geom structure. In 4701 * other words, make sure we don't wrap 16-bit values. 4702 * e.g. converting from geom_cache to dk_geom 4703 * 4704 * Context: Kernel thread only 4705 */ 4706 static void 4707 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4708 { 4709 int i; 4710 static const struct chs_values { 4711 uint_t max_cap; /* Max Capacity for this HS. */ 4712 uint_t nhead; /* Heads to use. */ 4713 uint_t nsect; /* SPT to use. */ 4714 } CHS_values[] = { 4715 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4716 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4717 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4718 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4719 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4720 }; 4721 4722 /* Unlabeled SCSI floppy device */ 4723 if (capacity <= 0x1000) { 4724 un_g->dkg_nhead = 2; 4725 un_g->dkg_ncyl = 80; 4726 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4727 return; 4728 } 4729 4730 /* 4731 * For all devices we calculate cylinders using the 4732 * heads and sectors we assign based on capacity of the 4733 * device. The table is designed to be compatible with the 4734 * way other operating systems lay out fdisk tables for X86 4735 * and to insure that the cylinders never exceed 65535 to 4736 * prevent problems with X86 ioctls that report geometry. 4737 * We use SPT that are multiples of 63, since other OSes that 4738 * are not limited to 16-bits for cylinders stop at 63 SPT 4739 * we make do by using multiples of 63 SPT. 4740 * 4741 * Note than capacities greater than or equal to 1TB will simply 4742 * get the largest geometry from the table. This should be okay 4743 * since disks this large shouldn't be using CHS values anyway. 4744 */ 4745 for (i = 0; CHS_values[i].max_cap < capacity && 4746 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4747 ; 4748 4749 un_g->dkg_nhead = CHS_values[i].nhead; 4750 un_g->dkg_nsect = CHS_values[i].nsect; 4751 } 4752 #endif 4753 4754 4755 /* 4756 * Function: sd_resync_geom_caches 4757 * 4758 * Description: (Re)initialize both geometry caches: the virtual geometry 4759 * information is extracted from the HBA (the "geometry" 4760 * capability), and the physical geometry cache data is 4761 * generated by issuing MODE SENSE commands. 4762 * 4763 * Arguments: un - driver soft state (unit) structure 4764 * capacity - disk capacity in #blocks 4765 * lbasize - disk block size in bytes 4766 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4767 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4768 * to use the USCSI "direct" chain and bypass the normal 4769 * command waitq. 4770 * 4771 * Context: Kernel thread only (can sleep). 4772 */ 4773 4774 static void 4775 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4776 int path_flag) 4777 { 4778 struct geom_cache pgeom; 4779 struct geom_cache *pgeom_p = &pgeom; 4780 int spc; 4781 unsigned short nhead; 4782 unsigned short nsect; 4783 4784 ASSERT(un != NULL); 4785 ASSERT(mutex_owned(SD_MUTEX(un))); 4786 4787 /* 4788 * Ask the controller for its logical geometry. 4789 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4790 * then the lgeom cache will be invalid. 4791 */ 4792 sd_get_virtual_geometry(un, capacity, lbasize); 4793 4794 /* 4795 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4796 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4797 */ 4798 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4799 /* 4800 * Note: Perhaps this needs to be more adaptive? The rationale 4801 * is that, if there's no HBA geometry from the HBA driver, any 4802 * guess is good, since this is the physical geometry. If MODE 4803 * SENSE fails this gives a max cylinder size for non-LBA access 4804 */ 4805 nhead = 255; 4806 nsect = 63; 4807 } else { 4808 nhead = un->un_lgeom.g_nhead; 4809 nsect = un->un_lgeom.g_nsect; 4810 } 4811 4812 if (ISCD(un)) { 4813 pgeom_p->g_nhead = 1; 4814 pgeom_p->g_nsect = nsect * nhead; 4815 } else { 4816 pgeom_p->g_nhead = nhead; 4817 pgeom_p->g_nsect = nsect; 4818 } 4819 4820 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4821 pgeom_p->g_capacity = capacity; 4822 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4823 pgeom_p->g_acyl = 0; 4824 4825 /* 4826 * Retrieve fresh geometry data from the hardware, stash it 4827 * here temporarily before we rebuild the incore label. 4828 * 4829 * We want to use the MODE SENSE commands to derive the 4830 * physical geometry of the device, but if either command 4831 * fails, the logical geometry is used as the fallback for 4832 * disk label geometry. 4833 */ 4834 mutex_exit(SD_MUTEX(un)); 4835 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4836 mutex_enter(SD_MUTEX(un)); 4837 4838 /* 4839 * Now update the real copy while holding the mutex. This 4840 * way the global copy is never in an inconsistent state. 4841 */ 4842 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4843 4844 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4845 "(cached from lgeom)\n"); 4846 SD_INFO(SD_LOG_COMMON, un, 4847 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4848 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4849 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4850 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4851 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4852 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4853 un->un_pgeom.g_rpm); 4854 } 4855 4856 4857 /* 4858 * Function: sd_read_fdisk 4859 * 4860 * Description: utility routine to read the fdisk table. 4861 * 4862 * Arguments: un - driver soft state (unit) structure 4863 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4864 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4865 * to use the USCSI "direct" chain and bypass the normal 4866 * command waitq. 4867 * 4868 * Return Code: SD_CMD_SUCCESS 4869 * SD_CMD_FAILURE 4870 * 4871 * Context: Kernel thread only (can sleep). 4872 */ 4873 /* ARGSUSED */ 4874 static int 4875 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4876 { 4877 #if defined(_NO_FDISK_PRESENT) 4878 4879 un->un_solaris_offset = 0; 4880 un->un_solaris_size = capacity; 4881 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4882 return (SD_CMD_SUCCESS); 4883 4884 #elif defined(_FIRMWARE_NEEDS_FDISK) 4885 4886 struct ipart *fdp; 4887 struct mboot *mbp; 4888 struct ipart fdisk[FD_NUMPART]; 4889 int i; 4890 char sigbuf[2]; 4891 caddr_t bufp; 4892 int uidx; 4893 int rval; 4894 int lba = 0; 4895 uint_t solaris_offset; /* offset to solaris part. */ 4896 daddr_t solaris_size; /* size of solaris partition */ 4897 uint32_t blocksize; 4898 4899 ASSERT(un != NULL); 4900 ASSERT(mutex_owned(SD_MUTEX(un))); 4901 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4902 4903 blocksize = un->un_tgt_blocksize; 4904 4905 /* 4906 * Start off assuming no fdisk table 4907 */ 4908 solaris_offset = 0; 4909 solaris_size = capacity; 4910 4911 mutex_exit(SD_MUTEX(un)); 4912 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4913 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4914 mutex_enter(SD_MUTEX(un)); 4915 4916 if (rval != 0) { 4917 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4918 "sd_read_fdisk: fdisk read err\n"); 4919 kmem_free(bufp, blocksize); 4920 return (SD_CMD_FAILURE); 4921 } 4922 4923 mbp = (struct mboot *)bufp; 4924 4925 /* 4926 * The fdisk table does not begin on a 4-byte boundary within the 4927 * master boot record, so we copy it to an aligned structure to avoid 4928 * alignment exceptions on some processors. 4929 */ 4930 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4931 4932 /* 4933 * Check for lba support before verifying sig; sig might not be 4934 * there, say on a blank disk, but the max_chs mark may still 4935 * be present. 4936 * 4937 * Note: LBA support and BEFs are an x86-only concept but this 4938 * code should work OK on SPARC as well. 4939 */ 4940 4941 /* 4942 * First, check for lba-access-ok on root node (or prom root node) 4943 * if present there, don't need to search fdisk table. 4944 */ 4945 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4946 "lba-access-ok", 0) != 0) { 4947 /* All drives do LBA; don't search fdisk table */ 4948 lba = 1; 4949 } else { 4950 /* Okay, look for mark in fdisk table */ 4951 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4952 /* accumulate "lba" value from all partitions */ 4953 lba = (lba || sd_has_max_chs_vals(fdp)); 4954 } 4955 } 4956 4957 if (lba != 0) { 4958 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4959 4960 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4961 "lba-access-ok", 0) == 0) { 4962 /* not found; create it */ 4963 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4964 "lba-access-ok", (caddr_t)NULL, 0) != 4965 DDI_PROP_SUCCESS) { 4966 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4967 "sd_read_fdisk: Can't create lba property " 4968 "for instance %d\n", 4969 ddi_get_instance(SD_DEVINFO(un))); 4970 } 4971 } 4972 } 4973 4974 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4975 4976 /* 4977 * Endian-independent signature check 4978 */ 4979 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4980 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4981 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4982 "sd_read_fdisk: no fdisk\n"); 4983 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4984 rval = SD_CMD_SUCCESS; 4985 goto done; 4986 } 4987 4988 #ifdef SDDEBUG 4989 if (sd_level_mask & SD_LOGMASK_INFO) { 4990 fdp = fdisk; 4991 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4992 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4993 "numsect sysid bootid\n"); 4994 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4995 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4996 " %d: %8d %8d 0x%08x 0x%08x\n", 4997 i, fdp->relsect, fdp->numsect, 4998 fdp->systid, fdp->bootid); 4999 } 5000 } 5001 #endif 5002 5003 /* 5004 * Try to find the unix partition 5005 */ 5006 uidx = -1; 5007 solaris_offset = 0; 5008 solaris_size = 0; 5009 5010 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 5011 int relsect; 5012 int numsect; 5013 5014 if (fdp->numsect == 0) { 5015 un->un_fmap[i].fmap_start = 0; 5016 un->un_fmap[i].fmap_nblk = 0; 5017 continue; 5018 } 5019 5020 /* 5021 * Data in the fdisk table is little-endian. 5022 */ 5023 relsect = LE_32(fdp->relsect); 5024 numsect = LE_32(fdp->numsect); 5025 5026 un->un_fmap[i].fmap_start = relsect; 5027 un->un_fmap[i].fmap_nblk = numsect; 5028 5029 if (fdp->systid != SUNIXOS && 5030 fdp->systid != SUNIXOS2 && 5031 fdp->systid != EFI_PMBR) { 5032 continue; 5033 } 5034 5035 /* 5036 * use the last active solaris partition id found 5037 * (there should only be 1 active partition id) 5038 * 5039 * if there are no active solaris partition id 5040 * then use the first inactive solaris partition id 5041 */ 5042 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 5043 uidx = i; 5044 solaris_offset = relsect; 5045 solaris_size = numsect; 5046 } 5047 } 5048 5049 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 5050 un->un_solaris_offset, un->un_solaris_size); 5051 5052 rval = SD_CMD_SUCCESS; 5053 5054 done: 5055 5056 /* 5057 * Clear the VTOC info, only if the Solaris partition entry 5058 * has moved, changed size, been deleted, or if the size of 5059 * the partition is too small to even fit the label sector. 5060 */ 5061 if ((un->un_solaris_offset != solaris_offset) || 5062 (un->un_solaris_size != solaris_size) || 5063 solaris_size <= DK_LABEL_LOC) { 5064 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 5065 solaris_offset, solaris_size); 5066 bzero(&un->un_g, sizeof (struct dk_geom)); 5067 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5068 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5069 un->un_f_geometry_is_valid = FALSE; 5070 } 5071 un->un_solaris_offset = solaris_offset; 5072 un->un_solaris_size = solaris_size; 5073 kmem_free(bufp, blocksize); 5074 return (rval); 5075 5076 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 5077 #error "fdisk table presence undetermined for this platform." 5078 #endif /* #if defined(_NO_FDISK_PRESENT) */ 5079 } 5080 5081 5082 /* 5083 * Function: sd_get_physical_geometry 5084 * 5085 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 5086 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 5087 * target, and use this information to initialize the physical 5088 * geometry cache specified by pgeom_p. 5089 * 5090 * MODE SENSE is an optional command, so failure in this case 5091 * does not necessarily denote an error. We want to use the 5092 * MODE SENSE commands to derive the physical geometry of the 5093 * device, but if either command fails, the logical geometry is 5094 * used as the fallback for disk label geometry. 5095 * 5096 * This requires that un->un_blockcount and un->un_tgt_blocksize 5097 * have already been initialized for the current target and 5098 * that the current values be passed as args so that we don't 5099 * end up ever trying to use -1 as a valid value. This could 5100 * happen if either value is reset while we're not holding 5101 * the mutex. 5102 * 5103 * Arguments: un - driver soft state (unit) structure 5104 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5105 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5106 * to use the USCSI "direct" chain and bypass the normal 5107 * command waitq. 5108 * 5109 * Context: Kernel thread only (can sleep). 5110 */ 5111 5112 static void 5113 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 5114 int capacity, int lbasize, int path_flag) 5115 { 5116 struct mode_format *page3p; 5117 struct mode_geometry *page4p; 5118 struct mode_header *headerp; 5119 int sector_size; 5120 int nsect; 5121 int nhead; 5122 int ncyl; 5123 int intrlv; 5124 int spc; 5125 int modesense_capacity; 5126 int rpm; 5127 int bd_len; 5128 int mode_header_length; 5129 uchar_t *p3bufp; 5130 uchar_t *p4bufp; 5131 int cdbsize; 5132 5133 ASSERT(un != NULL); 5134 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 5135 5136 if (un->un_f_blockcount_is_valid != TRUE) { 5137 return; 5138 } 5139 5140 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 5141 return; 5142 } 5143 5144 if (lbasize == 0) { 5145 if (ISCD(un)) { 5146 lbasize = 2048; 5147 } else { 5148 lbasize = un->un_sys_blocksize; 5149 } 5150 } 5151 pgeom_p->g_secsize = (unsigned short)lbasize; 5152 5153 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 5154 5155 /* 5156 * Retrieve MODE SENSE page 3 - Format Device Page 5157 */ 5158 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 5159 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 5160 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 5161 != 0) { 5162 SD_ERROR(SD_LOG_COMMON, un, 5163 "sd_get_physical_geometry: mode sense page 3 failed\n"); 5164 goto page3_exit; 5165 } 5166 5167 /* 5168 * Determine size of Block Descriptors in order to locate the mode 5169 * page data. ATAPI devices return 0, SCSI devices should return 5170 * MODE_BLK_DESC_LENGTH. 5171 */ 5172 headerp = (struct mode_header *)p3bufp; 5173 if (un->un_f_cfg_is_atapi == TRUE) { 5174 struct mode_header_grp2 *mhp = 5175 (struct mode_header_grp2 *)headerp; 5176 mode_header_length = MODE_HEADER_LENGTH_GRP2; 5177 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5178 } else { 5179 mode_header_length = MODE_HEADER_LENGTH; 5180 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5181 } 5182 5183 if (bd_len > MODE_BLK_DESC_LENGTH) { 5184 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5185 "received unexpected bd_len of %d, page3\n", bd_len); 5186 goto page3_exit; 5187 } 5188 5189 page3p = (struct mode_format *) 5190 ((caddr_t)headerp + mode_header_length + bd_len); 5191 5192 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5193 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5194 "mode sense pg3 code mismatch %d\n", 5195 page3p->mode_page.code); 5196 goto page3_exit; 5197 } 5198 5199 /* 5200 * Use this physical geometry data only if BOTH MODE SENSE commands 5201 * complete successfully; otherwise, revert to the logical geometry. 5202 * So, we need to save everything in temporary variables. 5203 */ 5204 sector_size = BE_16(page3p->data_bytes_sect); 5205 5206 /* 5207 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5208 */ 5209 if (sector_size == 0) { 5210 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 5211 } else { 5212 sector_size &= ~(un->un_sys_blocksize - 1); 5213 } 5214 5215 nsect = BE_16(page3p->sect_track); 5216 intrlv = BE_16(page3p->interleave); 5217 5218 SD_INFO(SD_LOG_COMMON, un, 5219 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5220 SD_INFO(SD_LOG_COMMON, un, 5221 " mode page: %d; nsect: %d; sector size: %d;\n", 5222 page3p->mode_page.code, nsect, sector_size); 5223 SD_INFO(SD_LOG_COMMON, un, 5224 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5225 BE_16(page3p->track_skew), 5226 BE_16(page3p->cylinder_skew)); 5227 5228 5229 /* 5230 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5231 */ 5232 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5233 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 5234 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 5235 != 0) { 5236 SD_ERROR(SD_LOG_COMMON, un, 5237 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5238 goto page4_exit; 5239 } 5240 5241 /* 5242 * Determine size of Block Descriptors in order to locate the mode 5243 * page data. ATAPI devices return 0, SCSI devices should return 5244 * MODE_BLK_DESC_LENGTH. 5245 */ 5246 headerp = (struct mode_header *)p4bufp; 5247 if (un->un_f_cfg_is_atapi == TRUE) { 5248 struct mode_header_grp2 *mhp = 5249 (struct mode_header_grp2 *)headerp; 5250 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5251 } else { 5252 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5253 } 5254 5255 if (bd_len > MODE_BLK_DESC_LENGTH) { 5256 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5257 "received unexpected bd_len of %d, page4\n", bd_len); 5258 goto page4_exit; 5259 } 5260 5261 page4p = (struct mode_geometry *) 5262 ((caddr_t)headerp + mode_header_length + bd_len); 5263 5264 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5265 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5266 "mode sense pg4 code mismatch %d\n", 5267 page4p->mode_page.code); 5268 goto page4_exit; 5269 } 5270 5271 /* 5272 * Stash the data now, after we know that both commands completed. 5273 */ 5274 5275 mutex_enter(SD_MUTEX(un)); 5276 5277 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5278 spc = nhead * nsect; 5279 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5280 rpm = BE_16(page4p->rpm); 5281 5282 modesense_capacity = spc * ncyl; 5283 5284 SD_INFO(SD_LOG_COMMON, un, 5285 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5286 SD_INFO(SD_LOG_COMMON, un, 5287 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5288 SD_INFO(SD_LOG_COMMON, un, 5289 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5290 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5291 (void *)pgeom_p, capacity); 5292 5293 /* 5294 * Compensate if the drive's geometry is not rectangular, i.e., 5295 * the product of C * H * S returned by MODE SENSE >= that returned 5296 * by read capacity. This is an idiosyncrasy of the original x86 5297 * disk subsystem. 5298 */ 5299 if (modesense_capacity >= capacity) { 5300 SD_INFO(SD_LOG_COMMON, un, 5301 "sd_get_physical_geometry: adjusting acyl; " 5302 "old: %d; new: %d\n", pgeom_p->g_acyl, 5303 (modesense_capacity - capacity + spc - 1) / spc); 5304 if (sector_size != 0) { 5305 /* 1243403: NEC D38x7 drives don't support sec size */ 5306 pgeom_p->g_secsize = (unsigned short)sector_size; 5307 } 5308 pgeom_p->g_nsect = (unsigned short)nsect; 5309 pgeom_p->g_nhead = (unsigned short)nhead; 5310 pgeom_p->g_capacity = capacity; 5311 pgeom_p->g_acyl = 5312 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5313 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5314 } 5315 5316 pgeom_p->g_rpm = (unsigned short)rpm; 5317 pgeom_p->g_intrlv = (unsigned short)intrlv; 5318 5319 SD_INFO(SD_LOG_COMMON, un, 5320 "sd_get_physical_geometry: mode sense geometry:\n"); 5321 SD_INFO(SD_LOG_COMMON, un, 5322 " nsect: %d; sector size: %d; interlv: %d\n", 5323 nsect, sector_size, intrlv); 5324 SD_INFO(SD_LOG_COMMON, un, 5325 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5326 nhead, ncyl, rpm, modesense_capacity); 5327 SD_INFO(SD_LOG_COMMON, un, 5328 "sd_get_physical_geometry: (cached)\n"); 5329 SD_INFO(SD_LOG_COMMON, un, 5330 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5331 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5332 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5333 SD_INFO(SD_LOG_COMMON, un, 5334 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5335 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5336 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5337 5338 mutex_exit(SD_MUTEX(un)); 5339 5340 page4_exit: 5341 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5342 page3_exit: 5343 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5344 } 5345 5346 5347 /* 5348 * Function: sd_get_virtual_geometry 5349 * 5350 * Description: Ask the controller to tell us about the target device. 5351 * 5352 * Arguments: un - pointer to softstate 5353 * capacity - disk capacity in #blocks 5354 * lbasize - disk block size in bytes 5355 * 5356 * Context: Kernel thread only 5357 */ 5358 5359 static void 5360 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5361 { 5362 struct geom_cache *lgeom_p = &un->un_lgeom; 5363 uint_t geombuf; 5364 int spc; 5365 5366 ASSERT(un != NULL); 5367 ASSERT(mutex_owned(SD_MUTEX(un))); 5368 5369 mutex_exit(SD_MUTEX(un)); 5370 5371 /* Set sector size, and total number of sectors */ 5372 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5373 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5374 5375 /* Let the HBA tell us its geometry */ 5376 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5377 5378 mutex_enter(SD_MUTEX(un)); 5379 5380 /* A value of -1 indicates an undefined "geometry" property */ 5381 if (geombuf == (-1)) { 5382 return; 5383 } 5384 5385 /* Initialize the logical geometry cache. */ 5386 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5387 lgeom_p->g_nsect = geombuf & 0xffff; 5388 lgeom_p->g_secsize = un->un_sys_blocksize; 5389 5390 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5391 5392 /* 5393 * Note: The driver originally converted the capacity value from 5394 * target blocks to system blocks. However, the capacity value passed 5395 * to this routine is already in terms of system blocks (this scaling 5396 * is done when the READ CAPACITY command is issued and processed). 5397 * This 'error' may have gone undetected because the usage of g_ncyl 5398 * (which is based upon g_capacity) is very limited within the driver 5399 */ 5400 lgeom_p->g_capacity = capacity; 5401 5402 /* 5403 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5404 * hba may return zero values if the device has been removed. 5405 */ 5406 if (spc == 0) { 5407 lgeom_p->g_ncyl = 0; 5408 } else { 5409 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5410 } 5411 lgeom_p->g_acyl = 0; 5412 5413 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5414 SD_INFO(SD_LOG_COMMON, un, 5415 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5416 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5417 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5418 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5419 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5420 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5421 } 5422 5423 5424 /* 5425 * Function: sd_update_block_info 5426 * 5427 * Description: Calculate a byte count to sector count bitshift value 5428 * from sector size. 5429 * 5430 * Arguments: un: unit struct. 5431 * lbasize: new target sector size 5432 * capacity: new target capacity, ie. block count 5433 * 5434 * Context: Kernel thread context 5435 */ 5436 5437 static void 5438 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5439 { 5440 if (lbasize != 0) { 5441 un->un_tgt_blocksize = lbasize; 5442 un->un_f_tgt_blocksize_is_valid = TRUE; 5443 } 5444 5445 if (capacity != 0) { 5446 un->un_blockcount = capacity; 5447 un->un_f_blockcount_is_valid = TRUE; 5448 } 5449 } 5450 5451 5452 static void 5453 sd_swap_efi_gpt(efi_gpt_t *e) 5454 { 5455 _NOTE(ASSUMING_PROTECTED(*e)) 5456 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5457 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5458 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5459 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5460 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5461 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5462 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5463 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5464 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5465 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5466 e->efi_gpt_NumberOfPartitionEntries = 5467 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5468 e->efi_gpt_SizeOfPartitionEntry = 5469 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5470 e->efi_gpt_PartitionEntryArrayCRC32 = 5471 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5472 } 5473 5474 static void 5475 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5476 { 5477 int i; 5478 5479 _NOTE(ASSUMING_PROTECTED(*p)) 5480 for (i = 0; i < nparts; i++) { 5481 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5482 p[i].efi_gpe_PartitionTypeGUID); 5483 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5484 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5485 /* PartitionAttrs */ 5486 } 5487 } 5488 5489 static int 5490 sd_validate_efi(efi_gpt_t *labp) 5491 { 5492 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5493 return (EINVAL); 5494 /* at least 96 bytes in this version of the spec. */ 5495 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5496 labp->efi_gpt_HeaderSize) 5497 return (EINVAL); 5498 /* this should be 128 bytes */ 5499 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5500 return (EINVAL); 5501 return (0); 5502 } 5503 5504 static int 5505 sd_use_efi(struct sd_lun *un, int path_flag) 5506 { 5507 int i; 5508 int rval = 0; 5509 efi_gpe_t *partitions; 5510 uchar_t *buf; 5511 uint_t lbasize; 5512 uint64_t cap = 0; 5513 uint_t nparts; 5514 diskaddr_t gpe_lba; 5515 struct uuid uuid_type_reserved = EFI_RESERVED; 5516 5517 ASSERT(mutex_owned(SD_MUTEX(un))); 5518 lbasize = un->un_tgt_blocksize; 5519 un->un_reserved = -1; 5520 5521 mutex_exit(SD_MUTEX(un)); 5522 5523 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5524 5525 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5526 rval = EINVAL; 5527 goto done_err; 5528 } 5529 5530 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5531 if (rval) { 5532 goto done_err; 5533 } 5534 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5535 /* not ours */ 5536 rval = ESRCH; 5537 goto done_err; 5538 } 5539 5540 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5541 if (rval) { 5542 goto done_err; 5543 } 5544 sd_swap_efi_gpt((efi_gpt_t *)buf); 5545 5546 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5547 /* 5548 * Couldn't read the primary, try the backup. Our 5549 * capacity at this point could be based on CHS, so 5550 * check what the device reports. 5551 */ 5552 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5553 path_flag); 5554 if (rval) { 5555 goto done_err; 5556 } 5557 5558 /* 5559 * The MMC standard allows READ CAPACITY to be 5560 * inaccurate by a bounded amount (in the interest of 5561 * response latency). As a result, failed READs are 5562 * commonplace (due to the reading of metadata and not 5563 * data). Depending on the per-Vendor/drive Sense data, 5564 * the failed READ can cause many (unnecessary) retries. 5565 */ 5566 5567 /* 5568 * Refer to comments related to off-by-1 at the 5569 * header of this file. Search the next to last 5570 * block for backup EFI label. 5571 */ 5572 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5573 cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5574 path_flag)) != 0) { 5575 goto done_err; 5576 } 5577 5578 sd_swap_efi_gpt((efi_gpt_t *)buf); 5579 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5580 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5581 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5582 path_flag)) != 0) { 5583 goto done_err; 5584 } 5585 sd_swap_efi_gpt((efi_gpt_t *)buf); 5586 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5587 goto done_err; 5588 } 5589 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5590 "primary label corrupt; using backup\n"); 5591 } 5592 5593 if (cap == 0) 5594 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5595 path_flag); 5596 5597 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5598 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5599 5600 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5601 path_flag); 5602 if (rval) { 5603 goto done_err; 5604 } 5605 partitions = (efi_gpe_t *)buf; 5606 5607 if (nparts > MAXPART) { 5608 nparts = MAXPART; 5609 } 5610 sd_swap_efi_gpe(nparts, partitions); 5611 5612 mutex_enter(SD_MUTEX(un)); 5613 5614 /* Fill in partition table. */ 5615 for (i = 0; i < nparts; i++) { 5616 if (partitions->efi_gpe_StartingLBA != 0 || 5617 partitions->efi_gpe_EndingLBA != 0) { 5618 un->un_map[i].dkl_cylno = 5619 partitions->efi_gpe_StartingLBA; 5620 un->un_map[i].dkl_nblk = 5621 partitions->efi_gpe_EndingLBA - 5622 partitions->efi_gpe_StartingLBA + 1; 5623 un->un_offset[i] = 5624 partitions->efi_gpe_StartingLBA; 5625 } 5626 if (un->un_reserved == -1) { 5627 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID, 5628 &uuid_type_reserved, sizeof (struct uuid)) == 0) { 5629 un->un_reserved = i; 5630 } 5631 } 5632 if (i == WD_NODE) { 5633 /* 5634 * minor number 7 corresponds to the whole disk 5635 */ 5636 un->un_map[i].dkl_cylno = 0; 5637 un->un_map[i].dkl_nblk = un->un_blockcount; 5638 un->un_offset[i] = 0; 5639 } 5640 partitions++; 5641 } 5642 un->un_solaris_offset = 0; 5643 un->un_solaris_size = cap; 5644 un->un_f_geometry_is_valid = TRUE; 5645 5646 /* clear the vtoc label */ 5647 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5648 5649 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5650 5651 /* 5652 * For EFI labeled disk, create and set the partition stats 5653 * at attach time, update the stats according to dynamic 5654 * partition changes during running time. 5655 */ 5656 if (un->un_f_pkstats_enabled) { 5657 sd_set_pstats(un); 5658 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_use_efi: " 5659 "un:0x%p pstats created and set, or updated\n", un); 5660 } 5661 return (0); 5662 5663 done_err: 5664 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5665 mutex_enter(SD_MUTEX(un)); 5666 /* 5667 * if we didn't find something that could look like a VTOC 5668 * and the disk is over 1TB, we know there isn't a valid label. 5669 * Otherwise let sd_uselabel decide what to do. We only 5670 * want to invalidate this if we're certain the label isn't 5671 * valid because sd_prop_op will now fail, which in turn 5672 * causes things like opens and stats on the partition to fail. 5673 */ 5674 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5675 un->un_f_geometry_is_valid = FALSE; 5676 } 5677 return (rval); 5678 } 5679 5680 5681 /* 5682 * Function: sd_uselabel 5683 * 5684 * Description: Validate the disk label and update the relevant data (geometry, 5685 * partition, vtoc, and capacity data) in the sd_lun struct. 5686 * Marks the geometry of the unit as being valid. 5687 * 5688 * Arguments: un: unit struct. 5689 * dk_label: disk label 5690 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5691 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5692 * to use the USCSI "direct" chain and bypass the normal 5693 * command waitq. 5694 * 5695 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5696 * partition, vtoc, and capacity data are good. 5697 * 5698 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5699 * label; or computed capacity does not jibe with capacity 5700 * reported from the READ CAPACITY command. 5701 * 5702 * Context: Kernel thread only (can sleep). 5703 */ 5704 5705 static int 5706 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5707 { 5708 short *sp; 5709 short sum; 5710 short count; 5711 int label_error = SD_LABEL_IS_VALID; 5712 int i; 5713 int capacity; 5714 int part_end; 5715 int track_capacity; 5716 int err; 5717 #if defined(_SUNOS_VTOC_16) 5718 struct dkl_partition *vpartp; 5719 #endif 5720 ASSERT(un != NULL); 5721 ASSERT(mutex_owned(SD_MUTEX(un))); 5722 5723 /* Validate the magic number of the label. */ 5724 if (labp->dkl_magic != DKL_MAGIC) { 5725 #if defined(__sparc) 5726 if ((un->un_state == SD_STATE_NORMAL) && 5727 un->un_f_vtoc_errlog_supported) { 5728 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5729 "Corrupt label; wrong magic number\n"); 5730 } 5731 #endif 5732 return (SD_LABEL_IS_INVALID); 5733 } 5734 5735 /* Validate the checksum of the label. */ 5736 sp = (short *)labp; 5737 sum = 0; 5738 count = sizeof (struct dk_label) / sizeof (short); 5739 while (count--) { 5740 sum ^= *sp++; 5741 } 5742 5743 if (sum != 0) { 5744 #if defined(_SUNOS_VTOC_16) 5745 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5746 #elif defined(_SUNOS_VTOC_8) 5747 if ((un->un_state == SD_STATE_NORMAL) && 5748 un->un_f_vtoc_errlog_supported) { 5749 #endif 5750 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5751 "Corrupt label - label checksum failed\n"); 5752 } 5753 return (SD_LABEL_IS_INVALID); 5754 } 5755 5756 5757 /* 5758 * Fill in geometry structure with data from label. 5759 */ 5760 bzero(&un->un_g, sizeof (struct dk_geom)); 5761 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5762 un->un_g.dkg_acyl = labp->dkl_acyl; 5763 un->un_g.dkg_bcyl = 0; 5764 un->un_g.dkg_nhead = labp->dkl_nhead; 5765 un->un_g.dkg_nsect = labp->dkl_nsect; 5766 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5767 5768 #if defined(_SUNOS_VTOC_8) 5769 un->un_g.dkg_gap1 = labp->dkl_gap1; 5770 un->un_g.dkg_gap2 = labp->dkl_gap2; 5771 un->un_g.dkg_bhead = labp->dkl_bhead; 5772 #endif 5773 #if defined(_SUNOS_VTOC_16) 5774 un->un_dkg_skew = labp->dkl_skew; 5775 #endif 5776 5777 #if defined(__i386) || defined(__amd64) 5778 un->un_g.dkg_apc = labp->dkl_apc; 5779 #endif 5780 5781 /* 5782 * Currently we rely on the values in the label being accurate. If 5783 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5784 * 5785 * Note: In the future a MODE SENSE may be used to retrieve this data, 5786 * although this command is optional in SCSI-2. 5787 */ 5788 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5789 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5790 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5791 5792 /* 5793 * The Read and Write reinstruct values may not be valid 5794 * for older disks. 5795 */ 5796 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5797 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5798 5799 /* Fill in partition table. */ 5800 #if defined(_SUNOS_VTOC_8) 5801 for (i = 0; i < NDKMAP; i++) { 5802 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5803 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5804 } 5805 #endif 5806 #if defined(_SUNOS_VTOC_16) 5807 vpartp = labp->dkl_vtoc.v_part; 5808 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5809 5810 /* Prevent divide by zero */ 5811 if (track_capacity == 0) { 5812 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5813 "Corrupt label - zero nhead or nsect value\n"); 5814 5815 return (SD_LABEL_IS_INVALID); 5816 } 5817 5818 for (i = 0; i < NDKMAP; i++, vpartp++) { 5819 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5820 un->un_map[i].dkl_nblk = vpartp->p_size; 5821 } 5822 #endif 5823 5824 /* Fill in VTOC Structure. */ 5825 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5826 #if defined(_SUNOS_VTOC_8) 5827 /* 5828 * The 8-slice vtoc does not include the ascii label; save it into 5829 * the device's soft state structure here. 5830 */ 5831 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5832 #endif 5833 5834 /* Now look for a valid capacity. */ 5835 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5836 capacity = (un->un_g.dkg_ncyl * track_capacity); 5837 5838 if (un->un_g.dkg_acyl) { 5839 #if defined(__i386) || defined(__amd64) 5840 /* we may have > 1 alts cylinder */ 5841 capacity += (track_capacity * un->un_g.dkg_acyl); 5842 #else 5843 capacity += track_capacity; 5844 #endif 5845 } 5846 5847 /* 5848 * Force check here to ensure the computed capacity is valid. 5849 * If capacity is zero, it indicates an invalid label and 5850 * we should abort updating the relevant data then. 5851 */ 5852 if (capacity == 0) { 5853 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5854 "Corrupt label - no valid capacity could be retrieved\n"); 5855 5856 return (SD_LABEL_IS_INVALID); 5857 } 5858 5859 /* Mark the geometry as valid. */ 5860 un->un_f_geometry_is_valid = TRUE; 5861 5862 /* 5863 * At this point, un->un_blockcount should contain valid data from 5864 * the READ CAPACITY command. 5865 */ 5866 if (un->un_f_blockcount_is_valid != TRUE) { 5867 /* 5868 * We have a situation where the target didn't give us a good 5869 * READ CAPACITY value, yet there appears to be a valid label. 5870 * In this case, we'll fake the capacity. 5871 */ 5872 un->un_blockcount = capacity; 5873 un->un_f_blockcount_is_valid = TRUE; 5874 goto done; 5875 } 5876 5877 5878 if ((capacity <= un->un_blockcount) || 5879 (un->un_state != SD_STATE_NORMAL)) { 5880 #if defined(_SUNOS_VTOC_8) 5881 /* 5882 * We can't let this happen on drives that are subdivided 5883 * into logical disks (i.e., that have an fdisk table). 5884 * The un_blockcount field should always hold the full media 5885 * size in sectors, period. This code would overwrite 5886 * un_blockcount with the size of the Solaris fdisk partition. 5887 */ 5888 SD_ERROR(SD_LOG_COMMON, un, 5889 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5890 capacity, un->un_blockcount); 5891 un->un_blockcount = capacity; 5892 un->un_f_blockcount_is_valid = TRUE; 5893 #endif /* defined(_SUNOS_VTOC_8) */ 5894 goto done; 5895 } 5896 5897 if (ISCD(un)) { 5898 /* For CDROMs, we trust that the data in the label is OK. */ 5899 #if defined(_SUNOS_VTOC_8) 5900 for (i = 0; i < NDKMAP; i++) { 5901 part_end = labp->dkl_nhead * labp->dkl_nsect * 5902 labp->dkl_map[i].dkl_cylno + 5903 labp->dkl_map[i].dkl_nblk - 1; 5904 5905 if ((labp->dkl_map[i].dkl_nblk) && 5906 (part_end > un->un_blockcount)) { 5907 un->un_f_geometry_is_valid = FALSE; 5908 break; 5909 } 5910 } 5911 #endif 5912 #if defined(_SUNOS_VTOC_16) 5913 vpartp = &(labp->dkl_vtoc.v_part[0]); 5914 for (i = 0; i < NDKMAP; i++, vpartp++) { 5915 part_end = vpartp->p_start + vpartp->p_size; 5916 if ((vpartp->p_size > 0) && 5917 (part_end > un->un_blockcount)) { 5918 un->un_f_geometry_is_valid = FALSE; 5919 break; 5920 } 5921 } 5922 #endif 5923 } else { 5924 uint64_t t_capacity; 5925 uint32_t t_lbasize; 5926 5927 mutex_exit(SD_MUTEX(un)); 5928 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5929 path_flag); 5930 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5931 mutex_enter(SD_MUTEX(un)); 5932 5933 if (err == 0) { 5934 sd_update_block_info(un, t_lbasize, t_capacity); 5935 } 5936 5937 if (capacity > un->un_blockcount) { 5938 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5939 "Corrupt label - bad geometry\n"); 5940 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5941 "Label says %u blocks; Drive says %llu blocks\n", 5942 capacity, (unsigned long long)un->un_blockcount); 5943 un->un_f_geometry_is_valid = FALSE; 5944 label_error = SD_LABEL_IS_INVALID; 5945 } 5946 } 5947 5948 done: 5949 5950 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5951 SD_INFO(SD_LOG_COMMON, un, 5952 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5953 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5954 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5955 SD_INFO(SD_LOG_COMMON, un, 5956 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5957 un->un_tgt_blocksize, un->un_blockcount, 5958 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5959 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5960 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5961 5962 ASSERT(mutex_owned(SD_MUTEX(un))); 5963 5964 return (label_error); 5965 } 5966 5967 5968 /* 5969 * Function: sd_build_default_label 5970 * 5971 * Description: Generate a default label for those devices that do not have 5972 * one, e.g., new media, removable cartridges, etc.. 5973 * 5974 * Context: Kernel thread only 5975 */ 5976 5977 static void 5978 sd_build_default_label(struct sd_lun *un) 5979 { 5980 #if defined(_SUNOS_VTOC_16) 5981 uint_t phys_spc; 5982 uint_t disksize; 5983 struct dk_geom un_g; 5984 uint64_t capacity; 5985 #endif 5986 5987 ASSERT(un != NULL); 5988 ASSERT(mutex_owned(SD_MUTEX(un))); 5989 5990 #if defined(_SUNOS_VTOC_8) 5991 /* 5992 * Note: This is a legacy check for non-removable devices on VTOC_8 5993 * only. This may be a valid check for VTOC_16 as well. 5994 * Once we understand why there is this difference between SPARC and 5995 * x86 platform, we could remove this legacy check. 5996 */ 5997 ASSERT(un->un_f_default_vtoc_supported); 5998 #endif 5999 6000 bzero(&un->un_g, sizeof (struct dk_geom)); 6001 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 6002 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 6003 6004 #if defined(_SUNOS_VTOC_8) 6005 6006 /* 6007 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 6008 * But it is still necessary to set up various geometry information, 6009 * and we are doing this here. 6010 */ 6011 6012 /* 6013 * For the rpm, we use the minimum for the disk. For the head, cyl, 6014 * and number of sector per track, if the capacity <= 1GB, head = 64, 6015 * sect = 32. else head = 255, sect 63 Note: the capacity should be 6016 * equal to C*H*S values. This will cause some truncation of size due 6017 * to round off errors. For CD-ROMs, this truncation can have adverse 6018 * side effects, so returning ncyl and nhead as 1. The nsect will 6019 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 6020 */ 6021 if (ISCD(un)) { 6022 /* 6023 * Preserve the old behavior for non-writable 6024 * medias. Since dkg_nsect is a ushort, it 6025 * will lose bits as cdroms have more than 6026 * 65536 sectors. So if we recalculate 6027 * capacity, it will become much shorter. 6028 * But the dkg_* information is not 6029 * used for CDROMs so it is OK. But for 6030 * Writable CDs we need this information 6031 * to be valid (for newfs say). So we 6032 * make nsect and nhead > 1 that way 6033 * nsect can still stay within ushort limit 6034 * without losing any bits. 6035 */ 6036 if (un->un_f_mmc_writable_media == TRUE) { 6037 un->un_g.dkg_nhead = 64; 6038 un->un_g.dkg_nsect = 32; 6039 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6040 un->un_blockcount = un->un_g.dkg_ncyl * 6041 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6042 } else { 6043 un->un_g.dkg_ncyl = 1; 6044 un->un_g.dkg_nhead = 1; 6045 un->un_g.dkg_nsect = un->un_blockcount; 6046 } 6047 } else { 6048 if (un->un_blockcount <= 0x1000) { 6049 /* unlabeled SCSI floppy device */ 6050 un->un_g.dkg_nhead = 2; 6051 un->un_g.dkg_ncyl = 80; 6052 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 6053 } else if (un->un_blockcount <= 0x200000) { 6054 un->un_g.dkg_nhead = 64; 6055 un->un_g.dkg_nsect = 32; 6056 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6057 } else { 6058 un->un_g.dkg_nhead = 255; 6059 un->un_g.dkg_nsect = 63; 6060 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 6061 } 6062 un->un_blockcount = 6063 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6064 } 6065 6066 un->un_g.dkg_acyl = 0; 6067 un->un_g.dkg_bcyl = 0; 6068 un->un_g.dkg_rpm = 200; 6069 un->un_asciilabel[0] = '\0'; 6070 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 6071 6072 un->un_map[0].dkl_cylno = 0; 6073 un->un_map[0].dkl_nblk = un->un_blockcount; 6074 un->un_map[2].dkl_cylno = 0; 6075 un->un_map[2].dkl_nblk = un->un_blockcount; 6076 6077 #elif defined(_SUNOS_VTOC_16) 6078 6079 if (un->un_solaris_size == 0) { 6080 /* 6081 * Got fdisk table but no solaris entry therefore 6082 * don't create a default label 6083 */ 6084 un->un_f_geometry_is_valid = TRUE; 6085 return; 6086 } 6087 6088 /* 6089 * For CDs we continue to use the physical geometry to calculate 6090 * number of cylinders. All other devices must convert the 6091 * physical geometry (geom_cache) to values that will fit 6092 * in a dk_geom structure. 6093 */ 6094 if (ISCD(un)) { 6095 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 6096 } else { 6097 /* Convert physical geometry to disk geometry */ 6098 bzero(&un_g, sizeof (struct dk_geom)); 6099 6100 /* 6101 * Refer to comments related to off-by-1 at the 6102 * header of this file. 6103 * Before caculating geometry, capacity should be 6104 * decreased by 1. That un_f_capacity_adjusted is 6105 * TRUE means that we are treating a 1TB disk as 6106 * (1T - 512)B. And the capacity of disks is already 6107 * decreased by 1. 6108 */ 6109 if (!un->un_f_capacity_adjusted && 6110 !un->un_f_has_removable_media && 6111 !un->un_f_is_hotpluggable && 6112 un->un_tgt_blocksize == un->un_sys_blocksize) 6113 capacity = un->un_blockcount - 1; 6114 else 6115 capacity = un->un_blockcount; 6116 6117 sd_convert_geometry(capacity, &un_g); 6118 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 6119 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6120 } 6121 6122 ASSERT(phys_spc != 0); 6123 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 6124 un->un_g.dkg_acyl = DK_ACYL; 6125 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 6126 disksize = un->un_g.dkg_ncyl * phys_spc; 6127 6128 if (ISCD(un)) { 6129 /* 6130 * CD's don't use the "heads * sectors * cyls"-type of 6131 * geometry, but instead use the entire capacity of the media. 6132 */ 6133 disksize = un->un_solaris_size; 6134 un->un_g.dkg_nhead = 1; 6135 un->un_g.dkg_nsect = 1; 6136 un->un_g.dkg_rpm = 6137 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 6138 6139 un->un_vtoc.v_part[0].p_start = 0; 6140 un->un_vtoc.v_part[0].p_size = disksize; 6141 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 6142 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 6143 6144 un->un_map[0].dkl_cylno = 0; 6145 un->un_map[0].dkl_nblk = disksize; 6146 un->un_offset[0] = 0; 6147 6148 } else { 6149 /* 6150 * Hard disks and removable media cartridges 6151 */ 6152 un->un_g.dkg_rpm = 6153 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 6154 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 6155 6156 /* Add boot slice */ 6157 un->un_vtoc.v_part[8].p_start = 0; 6158 un->un_vtoc.v_part[8].p_size = phys_spc; 6159 un->un_vtoc.v_part[8].p_tag = V_BOOT; 6160 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 6161 6162 un->un_map[8].dkl_cylno = 0; 6163 un->un_map[8].dkl_nblk = phys_spc; 6164 un->un_offset[8] = 0; 6165 } 6166 6167 un->un_g.dkg_apc = 0; 6168 un->un_vtoc.v_nparts = V_NUMPAR; 6169 un->un_vtoc.v_version = V_VERSION; 6170 6171 /* Add backup slice */ 6172 un->un_vtoc.v_part[2].p_start = 0; 6173 un->un_vtoc.v_part[2].p_size = disksize; 6174 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 6175 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 6176 6177 un->un_map[2].dkl_cylno = 0; 6178 un->un_map[2].dkl_nblk = disksize; 6179 un->un_offset[2] = 0; 6180 6181 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 6182 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 6183 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 6184 6185 #else 6186 #error "No VTOC format defined." 6187 #endif 6188 6189 un->un_g.dkg_read_reinstruct = 0; 6190 un->un_g.dkg_write_reinstruct = 0; 6191 6192 un->un_g.dkg_intrlv = 1; 6193 6194 un->un_vtoc.v_sanity = VTOC_SANE; 6195 6196 un->un_f_geometry_is_valid = TRUE; 6197 6198 SD_INFO(SD_LOG_COMMON, un, 6199 "sd_build_default_label: Default label created: " 6200 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 6201 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 6202 un->un_g.dkg_nsect, un->un_blockcount); 6203 } 6204 6205 6206 #if defined(_FIRMWARE_NEEDS_FDISK) 6207 /* 6208 * Max CHS values, as they are encoded into bytes, for 1022/254/63 6209 */ 6210 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 6211 #define LBA_MAX_CYL (1022 & 0xFF) 6212 #define LBA_MAX_HEAD (254) 6213 6214 6215 /* 6216 * Function: sd_has_max_chs_vals 6217 * 6218 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 6219 * 6220 * Arguments: fdp - ptr to CHS info 6221 * 6222 * Return Code: True or false 6223 * 6224 * Context: Any. 6225 */ 6226 6227 static int 6228 sd_has_max_chs_vals(struct ipart *fdp) 6229 { 6230 return ((fdp->begcyl == LBA_MAX_CYL) && 6231 (fdp->beghead == LBA_MAX_HEAD) && 6232 (fdp->begsect == LBA_MAX_SECT) && 6233 (fdp->endcyl == LBA_MAX_CYL) && 6234 (fdp->endhead == LBA_MAX_HEAD) && 6235 (fdp->endsect == LBA_MAX_SECT)); 6236 } 6237 #endif 6238 6239 6240 /* 6241 * Function: sd_inq_fill 6242 * 6243 * Description: Print a piece of inquiry data, cleaned up for non-printable 6244 * characters and stopping at the first space character after 6245 * the beginning of the passed string; 6246 * 6247 * Arguments: p - source string 6248 * l - maximum length to copy 6249 * s - destination string 6250 * 6251 * Context: Any. 6252 */ 6253 6254 static void 6255 sd_inq_fill(char *p, int l, char *s) 6256 { 6257 unsigned i = 0; 6258 char c; 6259 6260 while (i++ < l) { 6261 if ((c = *p++) < ' ' || c >= 0x7F) { 6262 c = '*'; 6263 } else if (i != 1 && c == ' ') { 6264 break; 6265 } 6266 *s++ = c; 6267 } 6268 *s++ = 0; 6269 } 6270 6271 6272 /* 6273 * Function: sd_register_devid 6274 * 6275 * Description: This routine will obtain the device id information from the 6276 * target, obtain the serial number, and register the device 6277 * id with the ddi framework. 6278 * 6279 * Arguments: devi - the system's dev_info_t for the device. 6280 * un - driver soft state (unit) structure 6281 * reservation_flag - indicates if a reservation conflict 6282 * occurred during attach 6283 * 6284 * Context: Kernel Thread 6285 */ 6286 static void 6287 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 6288 { 6289 int rval = 0; 6290 uchar_t *inq80 = NULL; 6291 size_t inq80_len = MAX_INQUIRY_SIZE; 6292 size_t inq80_resid = 0; 6293 uchar_t *inq83 = NULL; 6294 size_t inq83_len = MAX_INQUIRY_SIZE; 6295 size_t inq83_resid = 0; 6296 6297 ASSERT(un != NULL); 6298 ASSERT(mutex_owned(SD_MUTEX(un))); 6299 ASSERT((SD_DEVINFO(un)) == devi); 6300 6301 /* 6302 * This is the case of antiquated Sun disk drives that have the 6303 * FAB_DEVID property set in the disk_table. These drives 6304 * manage the devid's by storing them in last 2 available sectors 6305 * on the drive and have them fabricated by the ddi layer by calling 6306 * ddi_devid_init and passing the DEVID_FAB flag. 6307 */ 6308 if (un->un_f_opt_fab_devid == TRUE) { 6309 /* 6310 * Depending on EINVAL isn't reliable, since a reserved disk 6311 * may result in invalid geometry, so check to make sure a 6312 * reservation conflict did not occur during attach. 6313 */ 6314 if ((sd_get_devid(un) == EINVAL) && 6315 (reservation_flag != SD_TARGET_IS_RESERVED)) { 6316 /* 6317 * The devid is invalid AND there is no reservation 6318 * conflict. Fabricate a new devid. 6319 */ 6320 (void) sd_create_devid(un); 6321 } 6322 6323 /* Register the devid if it exists */ 6324 if (un->un_devid != NULL) { 6325 (void) ddi_devid_register(SD_DEVINFO(un), 6326 un->un_devid); 6327 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6328 "sd_register_devid: Devid Fabricated\n"); 6329 } 6330 return; 6331 } 6332 6333 /* 6334 * We check the availibility of the World Wide Name (0x83) and Unit 6335 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 6336 * un_vpd_page_mask from them, we decide which way to get the WWN. If 6337 * 0x83 is availible, that is the best choice. Our next choice is 6338 * 0x80. If neither are availible, we munge the devid from the device 6339 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 6340 * to fabricate a devid for non-Sun qualified disks. 6341 */ 6342 if (sd_check_vpd_page_support(un) == 0) { 6343 /* collect page 80 data if available */ 6344 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 6345 6346 mutex_exit(SD_MUTEX(un)); 6347 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 6348 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 6349 0x01, 0x80, &inq80_resid); 6350 6351 if (rval != 0) { 6352 kmem_free(inq80, inq80_len); 6353 inq80 = NULL; 6354 inq80_len = 0; 6355 } 6356 mutex_enter(SD_MUTEX(un)); 6357 } 6358 6359 /* collect page 83 data if available */ 6360 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 6361 mutex_exit(SD_MUTEX(un)); 6362 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6363 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6364 0x01, 0x83, &inq83_resid); 6365 6366 if (rval != 0) { 6367 kmem_free(inq83, inq83_len); 6368 inq83 = NULL; 6369 inq83_len = 0; 6370 } 6371 mutex_enter(SD_MUTEX(un)); 6372 } 6373 } 6374 6375 /* encode best devid possible based on data available */ 6376 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6377 (char *)ddi_driver_name(SD_DEVINFO(un)), 6378 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6379 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6380 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6381 6382 /* devid successfully encoded, register devid */ 6383 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6384 6385 } else { 6386 /* 6387 * Unable to encode a devid based on data available. 6388 * This is not a Sun qualified disk. Older Sun disk 6389 * drives that have the SD_FAB_DEVID property 6390 * set in the disk_table and non Sun qualified 6391 * disks are treated in the same manner. These 6392 * drives manage the devid's by storing them in 6393 * last 2 available sectors on the drive and 6394 * have them fabricated by the ddi layer by 6395 * calling ddi_devid_init and passing the 6396 * DEVID_FAB flag. 6397 * Create a fabricate devid only if there's no 6398 * fabricate devid existed. 6399 */ 6400 if (sd_get_devid(un) == EINVAL) { 6401 (void) sd_create_devid(un); 6402 } 6403 un->un_f_opt_fab_devid = TRUE; 6404 6405 /* Register the devid if it exists */ 6406 if (un->un_devid != NULL) { 6407 (void) ddi_devid_register(SD_DEVINFO(un), 6408 un->un_devid); 6409 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6410 "sd_register_devid: devid fabricated using " 6411 "ddi framework\n"); 6412 } 6413 } 6414 6415 /* clean up resources */ 6416 if (inq80 != NULL) { 6417 kmem_free(inq80, inq80_len); 6418 } 6419 if (inq83 != NULL) { 6420 kmem_free(inq83, inq83_len); 6421 } 6422 } 6423 6424 static daddr_t 6425 sd_get_devid_block(struct sd_lun *un) 6426 { 6427 daddr_t spc, blk, head, cyl; 6428 6429 if ((un->un_f_geometry_is_valid == FALSE) || 6430 (un->un_solaris_size < DK_LABEL_LOC)) 6431 return (-1); 6432 6433 if (un->un_vtoc.v_sanity != VTOC_SANE) { 6434 /* EFI labeled */ 6435 if (un->un_reserved != -1) { 6436 blk = un->un_map[un->un_reserved].dkl_cylno; 6437 } else { 6438 return (-1); 6439 } 6440 } else { 6441 /* SMI labeled */ 6442 /* this geometry doesn't allow us to write a devid */ 6443 if (un->un_g.dkg_acyl < 2) { 6444 return (-1); 6445 } 6446 6447 /* 6448 * Subtract 2 guarantees that the next to last cylinder 6449 * is used 6450 */ 6451 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6452 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6453 head = un->un_g.dkg_nhead - 1; 6454 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6455 (head * un->un_g.dkg_nsect) + 1; 6456 } 6457 return (blk); 6458 } 6459 6460 /* 6461 * Function: sd_get_devid 6462 * 6463 * Description: This routine will return 0 if a valid device id has been 6464 * obtained from the target and stored in the soft state. If a 6465 * valid device id has not been previously read and stored, a 6466 * read attempt will be made. 6467 * 6468 * Arguments: un - driver soft state (unit) structure 6469 * 6470 * Return Code: 0 if we successfully get the device id 6471 * 6472 * Context: Kernel Thread 6473 */ 6474 6475 static int 6476 sd_get_devid(struct sd_lun *un) 6477 { 6478 struct dk_devid *dkdevid; 6479 ddi_devid_t tmpid; 6480 uint_t *ip; 6481 size_t sz; 6482 daddr_t blk; 6483 int status; 6484 int chksum; 6485 int i; 6486 size_t buffer_size; 6487 6488 ASSERT(un != NULL); 6489 ASSERT(mutex_owned(SD_MUTEX(un))); 6490 6491 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6492 un); 6493 6494 if (un->un_devid != NULL) { 6495 return (0); 6496 } 6497 6498 blk = sd_get_devid_block(un); 6499 if (blk < 0) 6500 return (EINVAL); 6501 6502 /* 6503 * Read and verify device id, stored in the reserved cylinders at the 6504 * end of the disk. Backup label is on the odd sectors of the last 6505 * track of the last cylinder. Device id will be on track of the next 6506 * to last cylinder. 6507 */ 6508 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6509 mutex_exit(SD_MUTEX(un)); 6510 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6511 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6512 SD_PATH_DIRECT); 6513 if (status != 0) { 6514 goto error; 6515 } 6516 6517 /* Validate the revision */ 6518 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6519 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6520 status = EINVAL; 6521 goto error; 6522 } 6523 6524 /* Calculate the checksum */ 6525 chksum = 0; 6526 ip = (uint_t *)dkdevid; 6527 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6528 i++) { 6529 chksum ^= ip[i]; 6530 } 6531 6532 /* Compare the checksums */ 6533 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6534 status = EINVAL; 6535 goto error; 6536 } 6537 6538 /* Validate the device id */ 6539 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6540 status = EINVAL; 6541 goto error; 6542 } 6543 6544 /* 6545 * Store the device id in the driver soft state 6546 */ 6547 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6548 tmpid = kmem_alloc(sz, KM_SLEEP); 6549 6550 mutex_enter(SD_MUTEX(un)); 6551 6552 un->un_devid = tmpid; 6553 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6554 6555 kmem_free(dkdevid, buffer_size); 6556 6557 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6558 6559 return (status); 6560 error: 6561 mutex_enter(SD_MUTEX(un)); 6562 kmem_free(dkdevid, buffer_size); 6563 return (status); 6564 } 6565 6566 6567 /* 6568 * Function: sd_create_devid 6569 * 6570 * Description: This routine will fabricate the device id and write it 6571 * to the disk. 6572 * 6573 * Arguments: un - driver soft state (unit) structure 6574 * 6575 * Return Code: value of the fabricated device id 6576 * 6577 * Context: Kernel Thread 6578 */ 6579 6580 static ddi_devid_t 6581 sd_create_devid(struct sd_lun *un) 6582 { 6583 ASSERT(un != NULL); 6584 6585 /* Fabricate the devid */ 6586 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6587 == DDI_FAILURE) { 6588 return (NULL); 6589 } 6590 6591 /* Write the devid to disk */ 6592 if (sd_write_deviceid(un) != 0) { 6593 ddi_devid_free(un->un_devid); 6594 un->un_devid = NULL; 6595 } 6596 6597 return (un->un_devid); 6598 } 6599 6600 6601 /* 6602 * Function: sd_write_deviceid 6603 * 6604 * Description: This routine will write the device id to the disk 6605 * reserved sector. 6606 * 6607 * Arguments: un - driver soft state (unit) structure 6608 * 6609 * Return Code: EINVAL 6610 * value returned by sd_send_scsi_cmd 6611 * 6612 * Context: Kernel Thread 6613 */ 6614 6615 static int 6616 sd_write_deviceid(struct sd_lun *un) 6617 { 6618 struct dk_devid *dkdevid; 6619 daddr_t blk; 6620 uint_t *ip, chksum; 6621 int status; 6622 int i; 6623 6624 ASSERT(mutex_owned(SD_MUTEX(un))); 6625 6626 blk = sd_get_devid_block(un); 6627 if (blk < 0) 6628 return (-1); 6629 mutex_exit(SD_MUTEX(un)); 6630 6631 /* Allocate the buffer */ 6632 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6633 6634 /* Fill in the revision */ 6635 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6636 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6637 6638 /* Copy in the device id */ 6639 mutex_enter(SD_MUTEX(un)); 6640 bcopy(un->un_devid, &dkdevid->dkd_devid, 6641 ddi_devid_sizeof(un->un_devid)); 6642 mutex_exit(SD_MUTEX(un)); 6643 6644 /* Calculate the checksum */ 6645 chksum = 0; 6646 ip = (uint_t *)dkdevid; 6647 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6648 i++) { 6649 chksum ^= ip[i]; 6650 } 6651 6652 /* Fill-in checksum */ 6653 DKD_FORMCHKSUM(chksum, dkdevid); 6654 6655 /* Write the reserved sector */ 6656 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6657 SD_PATH_DIRECT); 6658 6659 kmem_free(dkdevid, un->un_sys_blocksize); 6660 6661 mutex_enter(SD_MUTEX(un)); 6662 return (status); 6663 } 6664 6665 6666 /* 6667 * Function: sd_check_vpd_page_support 6668 * 6669 * Description: This routine sends an inquiry command with the EVPD bit set and 6670 * a page code of 0x00 to the device. It is used to determine which 6671 * vital product pages are availible to find the devid. We are 6672 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6673 * device does not support that command. 6674 * 6675 * Arguments: un - driver soft state (unit) structure 6676 * 6677 * Return Code: 0 - success 6678 * 1 - check condition 6679 * 6680 * Context: This routine can sleep. 6681 */ 6682 6683 static int 6684 sd_check_vpd_page_support(struct sd_lun *un) 6685 { 6686 uchar_t *page_list = NULL; 6687 uchar_t page_length = 0xff; /* Use max possible length */ 6688 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6689 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6690 int rval = 0; 6691 int counter; 6692 6693 ASSERT(un != NULL); 6694 ASSERT(mutex_owned(SD_MUTEX(un))); 6695 6696 mutex_exit(SD_MUTEX(un)); 6697 6698 /* 6699 * We'll set the page length to the maximum to save figuring it out 6700 * with an additional call. 6701 */ 6702 page_list = kmem_zalloc(page_length, KM_SLEEP); 6703 6704 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6705 page_code, NULL); 6706 6707 mutex_enter(SD_MUTEX(un)); 6708 6709 /* 6710 * Now we must validate that the device accepted the command, as some 6711 * drives do not support it. If the drive does support it, we will 6712 * return 0, and the supported pages will be in un_vpd_page_mask. If 6713 * not, we return -1. 6714 */ 6715 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6716 /* Loop to find one of the 2 pages we need */ 6717 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6718 6719 /* 6720 * Pages are returned in ascending order, and 0x83 is what we 6721 * are hoping for. 6722 */ 6723 while ((page_list[counter] <= 0x83) && 6724 (counter <= (page_list[VPD_PAGE_LENGTH] + 6725 VPD_HEAD_OFFSET))) { 6726 /* 6727 * Add 3 because page_list[3] is the number of 6728 * pages minus 3 6729 */ 6730 6731 switch (page_list[counter]) { 6732 case 0x00: 6733 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6734 break; 6735 case 0x80: 6736 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6737 break; 6738 case 0x81: 6739 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6740 break; 6741 case 0x82: 6742 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6743 break; 6744 case 0x83: 6745 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6746 break; 6747 } 6748 counter++; 6749 } 6750 6751 } else { 6752 rval = -1; 6753 6754 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6755 "sd_check_vpd_page_support: This drive does not implement " 6756 "VPD pages.\n"); 6757 } 6758 6759 kmem_free(page_list, page_length); 6760 6761 return (rval); 6762 } 6763 6764 6765 /* 6766 * Function: sd_setup_pm 6767 * 6768 * Description: Initialize Power Management on the device 6769 * 6770 * Context: Kernel Thread 6771 */ 6772 6773 static void 6774 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6775 { 6776 uint_t log_page_size; 6777 uchar_t *log_page_data; 6778 int rval; 6779 6780 /* 6781 * Since we are called from attach, holding a mutex for 6782 * un is unnecessary. Because some of the routines called 6783 * from here require SD_MUTEX to not be held, assert this 6784 * right up front. 6785 */ 6786 ASSERT(!mutex_owned(SD_MUTEX(un))); 6787 /* 6788 * Since the sd device does not have the 'reg' property, 6789 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6790 * The following code is to tell cpr that this device 6791 * DOES need to be suspended and resumed. 6792 */ 6793 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6794 "pm-hardware-state", "needs-suspend-resume"); 6795 6796 /* 6797 * This complies with the new power management framework 6798 * for certain desktop machines. Create the pm_components 6799 * property as a string array property. 6800 */ 6801 if (un->un_f_pm_supported) { 6802 /* 6803 * not all devices have a motor, try it first. 6804 * some devices may return ILLEGAL REQUEST, some 6805 * will hang 6806 * The following START_STOP_UNIT is used to check if target 6807 * device has a motor. 6808 */ 6809 un->un_f_start_stop_supported = TRUE; 6810 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6811 SD_PATH_DIRECT) != 0) { 6812 un->un_f_start_stop_supported = FALSE; 6813 } 6814 6815 /* 6816 * create pm properties anyways otherwise the parent can't 6817 * go to sleep 6818 */ 6819 (void) sd_create_pm_components(devi, un); 6820 un->un_f_pm_is_enabled = TRUE; 6821 return; 6822 } 6823 6824 if (!un->un_f_log_sense_supported) { 6825 un->un_power_level = SD_SPINDLE_ON; 6826 un->un_f_pm_is_enabled = FALSE; 6827 return; 6828 } 6829 6830 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6831 6832 #ifdef SDDEBUG 6833 if (sd_force_pm_supported) { 6834 /* Force a successful result */ 6835 rval = 1; 6836 } 6837 #endif 6838 6839 /* 6840 * If the start-stop cycle counter log page is not supported 6841 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6842 * then we should not create the pm_components property. 6843 */ 6844 if (rval == -1) { 6845 /* 6846 * Error. 6847 * Reading log sense failed, most likely this is 6848 * an older drive that does not support log sense. 6849 * If this fails auto-pm is not supported. 6850 */ 6851 un->un_power_level = SD_SPINDLE_ON; 6852 un->un_f_pm_is_enabled = FALSE; 6853 6854 } else if (rval == 0) { 6855 /* 6856 * Page not found. 6857 * The start stop cycle counter is implemented as page 6858 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6859 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6860 */ 6861 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6862 /* 6863 * Page found, use this one. 6864 */ 6865 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6866 un->un_f_pm_is_enabled = TRUE; 6867 } else { 6868 /* 6869 * Error or page not found. 6870 * auto-pm is not supported for this device. 6871 */ 6872 un->un_power_level = SD_SPINDLE_ON; 6873 un->un_f_pm_is_enabled = FALSE; 6874 } 6875 } else { 6876 /* 6877 * Page found, use it. 6878 */ 6879 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6880 un->un_f_pm_is_enabled = TRUE; 6881 } 6882 6883 6884 if (un->un_f_pm_is_enabled == TRUE) { 6885 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6886 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6887 6888 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6889 log_page_size, un->un_start_stop_cycle_page, 6890 0x01, 0, SD_PATH_DIRECT); 6891 #ifdef SDDEBUG 6892 if (sd_force_pm_supported) { 6893 /* Force a successful result */ 6894 rval = 0; 6895 } 6896 #endif 6897 6898 /* 6899 * If the Log sense for Page( Start/stop cycle counter page) 6900 * succeeds, then power managment is supported and we can 6901 * enable auto-pm. 6902 */ 6903 if (rval == 0) { 6904 (void) sd_create_pm_components(devi, un); 6905 } else { 6906 un->un_power_level = SD_SPINDLE_ON; 6907 un->un_f_pm_is_enabled = FALSE; 6908 } 6909 6910 kmem_free(log_page_data, log_page_size); 6911 } 6912 } 6913 6914 6915 /* 6916 * Function: sd_create_pm_components 6917 * 6918 * Description: Initialize PM property. 6919 * 6920 * Context: Kernel thread context 6921 */ 6922 6923 static void 6924 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6925 { 6926 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6927 6928 ASSERT(!mutex_owned(SD_MUTEX(un))); 6929 6930 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6931 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6932 /* 6933 * When components are initially created they are idle, 6934 * power up any non-removables. 6935 * Note: the return value of pm_raise_power can't be used 6936 * for determining if PM should be enabled for this device. 6937 * Even if you check the return values and remove this 6938 * property created above, the PM framework will not honor the 6939 * change after the first call to pm_raise_power. Hence, 6940 * removal of that property does not help if pm_raise_power 6941 * fails. In the case of removable media, the start/stop 6942 * will fail if the media is not present. 6943 */ 6944 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6945 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6946 mutex_enter(SD_MUTEX(un)); 6947 un->un_power_level = SD_SPINDLE_ON; 6948 mutex_enter(&un->un_pm_mutex); 6949 /* Set to on and not busy. */ 6950 un->un_pm_count = 0; 6951 } else { 6952 mutex_enter(SD_MUTEX(un)); 6953 un->un_power_level = SD_SPINDLE_OFF; 6954 mutex_enter(&un->un_pm_mutex); 6955 /* Set to off. */ 6956 un->un_pm_count = -1; 6957 } 6958 mutex_exit(&un->un_pm_mutex); 6959 mutex_exit(SD_MUTEX(un)); 6960 } else { 6961 un->un_power_level = SD_SPINDLE_ON; 6962 un->un_f_pm_is_enabled = FALSE; 6963 } 6964 } 6965 6966 6967 /* 6968 * Function: sd_ddi_suspend 6969 * 6970 * Description: Performs system power-down operations. This includes 6971 * setting the drive state to indicate its suspended so 6972 * that no new commands will be accepted. Also, wait for 6973 * all commands that are in transport or queued to a timer 6974 * for retry to complete. All timeout threads are cancelled. 6975 * 6976 * Return Code: DDI_FAILURE or DDI_SUCCESS 6977 * 6978 * Context: Kernel thread context 6979 */ 6980 6981 static int 6982 sd_ddi_suspend(dev_info_t *devi) 6983 { 6984 struct sd_lun *un; 6985 clock_t wait_cmds_complete; 6986 6987 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6988 if (un == NULL) { 6989 return (DDI_FAILURE); 6990 } 6991 6992 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6993 6994 mutex_enter(SD_MUTEX(un)); 6995 6996 /* Return success if the device is already suspended. */ 6997 if (un->un_state == SD_STATE_SUSPENDED) { 6998 mutex_exit(SD_MUTEX(un)); 6999 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7000 "device already suspended, exiting\n"); 7001 return (DDI_SUCCESS); 7002 } 7003 7004 /* Return failure if the device is being used by HA */ 7005 if (un->un_resvd_status & 7006 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 7007 mutex_exit(SD_MUTEX(un)); 7008 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7009 "device in use by HA, exiting\n"); 7010 return (DDI_FAILURE); 7011 } 7012 7013 /* 7014 * Return failure if the device is in a resource wait 7015 * or power changing state. 7016 */ 7017 if ((un->un_state == SD_STATE_RWAIT) || 7018 (un->un_state == SD_STATE_PM_CHANGING)) { 7019 mutex_exit(SD_MUTEX(un)); 7020 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7021 "device in resource wait state, exiting\n"); 7022 return (DDI_FAILURE); 7023 } 7024 7025 7026 un->un_save_state = un->un_last_state; 7027 New_state(un, SD_STATE_SUSPENDED); 7028 7029 /* 7030 * Wait for all commands that are in transport or queued to a timer 7031 * for retry to complete. 7032 * 7033 * While waiting, no new commands will be accepted or sent because of 7034 * the new state we set above. 7035 * 7036 * Wait till current operation has completed. If we are in the resource 7037 * wait state (with an intr outstanding) then we need to wait till the 7038 * intr completes and starts the next cmd. We want to wait for 7039 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 7040 */ 7041 wait_cmds_complete = ddi_get_lbolt() + 7042 (sd_wait_cmds_complete * drv_usectohz(1000000)); 7043 7044 while (un->un_ncmds_in_transport != 0) { 7045 /* 7046 * Fail if commands do not finish in the specified time. 7047 */ 7048 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 7049 wait_cmds_complete) == -1) { 7050 /* 7051 * Undo the state changes made above. Everything 7052 * must go back to it's original value. 7053 */ 7054 Restore_state(un); 7055 un->un_last_state = un->un_save_state; 7056 /* Wake up any threads that might be waiting. */ 7057 cv_broadcast(&un->un_suspend_cv); 7058 mutex_exit(SD_MUTEX(un)); 7059 SD_ERROR(SD_LOG_IO_PM, un, 7060 "sd_ddi_suspend: failed due to outstanding cmds\n"); 7061 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 7062 return (DDI_FAILURE); 7063 } 7064 } 7065 7066 /* 7067 * Cancel SCSI watch thread and timeouts, if any are active 7068 */ 7069 7070 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 7071 opaque_t temp_token = un->un_swr_token; 7072 mutex_exit(SD_MUTEX(un)); 7073 scsi_watch_suspend(temp_token); 7074 mutex_enter(SD_MUTEX(un)); 7075 } 7076 7077 if (un->un_reset_throttle_timeid != NULL) { 7078 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7079 un->un_reset_throttle_timeid = NULL; 7080 mutex_exit(SD_MUTEX(un)); 7081 (void) untimeout(temp_id); 7082 mutex_enter(SD_MUTEX(un)); 7083 } 7084 7085 if (un->un_dcvb_timeid != NULL) { 7086 timeout_id_t temp_id = un->un_dcvb_timeid; 7087 un->un_dcvb_timeid = NULL; 7088 mutex_exit(SD_MUTEX(un)); 7089 (void) untimeout(temp_id); 7090 mutex_enter(SD_MUTEX(un)); 7091 } 7092 7093 mutex_enter(&un->un_pm_mutex); 7094 if (un->un_pm_timeid != NULL) { 7095 timeout_id_t temp_id = un->un_pm_timeid; 7096 un->un_pm_timeid = NULL; 7097 mutex_exit(&un->un_pm_mutex); 7098 mutex_exit(SD_MUTEX(un)); 7099 (void) untimeout(temp_id); 7100 mutex_enter(SD_MUTEX(un)); 7101 } else { 7102 mutex_exit(&un->un_pm_mutex); 7103 } 7104 7105 if (un->un_retry_timeid != NULL) { 7106 timeout_id_t temp_id = un->un_retry_timeid; 7107 un->un_retry_timeid = NULL; 7108 mutex_exit(SD_MUTEX(un)); 7109 (void) untimeout(temp_id); 7110 mutex_enter(SD_MUTEX(un)); 7111 } 7112 7113 if (un->un_direct_priority_timeid != NULL) { 7114 timeout_id_t temp_id = un->un_direct_priority_timeid; 7115 un->un_direct_priority_timeid = NULL; 7116 mutex_exit(SD_MUTEX(un)); 7117 (void) untimeout(temp_id); 7118 mutex_enter(SD_MUTEX(un)); 7119 } 7120 7121 if (un->un_f_is_fibre == TRUE) { 7122 /* 7123 * Remove callbacks for insert and remove events 7124 */ 7125 if (un->un_insert_event != NULL) { 7126 mutex_exit(SD_MUTEX(un)); 7127 (void) ddi_remove_event_handler(un->un_insert_cb_id); 7128 mutex_enter(SD_MUTEX(un)); 7129 un->un_insert_event = NULL; 7130 } 7131 7132 if (un->un_remove_event != NULL) { 7133 mutex_exit(SD_MUTEX(un)); 7134 (void) ddi_remove_event_handler(un->un_remove_cb_id); 7135 mutex_enter(SD_MUTEX(un)); 7136 un->un_remove_event = NULL; 7137 } 7138 } 7139 7140 mutex_exit(SD_MUTEX(un)); 7141 7142 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 7143 7144 return (DDI_SUCCESS); 7145 } 7146 7147 7148 /* 7149 * Function: sd_ddi_pm_suspend 7150 * 7151 * Description: Set the drive state to low power. 7152 * Someone else is required to actually change the drive 7153 * power level. 7154 * 7155 * Arguments: un - driver soft state (unit) structure 7156 * 7157 * Return Code: DDI_FAILURE or DDI_SUCCESS 7158 * 7159 * Context: Kernel thread context 7160 */ 7161 7162 static int 7163 sd_ddi_pm_suspend(struct sd_lun *un) 7164 { 7165 ASSERT(un != NULL); 7166 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 7167 7168 ASSERT(!mutex_owned(SD_MUTEX(un))); 7169 mutex_enter(SD_MUTEX(un)); 7170 7171 /* 7172 * Exit if power management is not enabled for this device, or if 7173 * the device is being used by HA. 7174 */ 7175 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 7176 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 7177 mutex_exit(SD_MUTEX(un)); 7178 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 7179 return (DDI_SUCCESS); 7180 } 7181 7182 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 7183 un->un_ncmds_in_driver); 7184 7185 /* 7186 * See if the device is not busy, ie.: 7187 * - we have no commands in the driver for this device 7188 * - not waiting for resources 7189 */ 7190 if ((un->un_ncmds_in_driver == 0) && 7191 (un->un_state != SD_STATE_RWAIT)) { 7192 /* 7193 * The device is not busy, so it is OK to go to low power state. 7194 * Indicate low power, but rely on someone else to actually 7195 * change it. 7196 */ 7197 mutex_enter(&un->un_pm_mutex); 7198 un->un_pm_count = -1; 7199 mutex_exit(&un->un_pm_mutex); 7200 un->un_power_level = SD_SPINDLE_OFF; 7201 } 7202 7203 mutex_exit(SD_MUTEX(un)); 7204 7205 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 7206 7207 return (DDI_SUCCESS); 7208 } 7209 7210 7211 /* 7212 * Function: sd_ddi_resume 7213 * 7214 * Description: Performs system power-up operations.. 7215 * 7216 * Return Code: DDI_SUCCESS 7217 * DDI_FAILURE 7218 * 7219 * Context: Kernel thread context 7220 */ 7221 7222 static int 7223 sd_ddi_resume(dev_info_t *devi) 7224 { 7225 struct sd_lun *un; 7226 7227 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 7228 if (un == NULL) { 7229 return (DDI_FAILURE); 7230 } 7231 7232 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 7233 7234 mutex_enter(SD_MUTEX(un)); 7235 Restore_state(un); 7236 7237 /* 7238 * Restore the state which was saved to give the 7239 * the right state in un_last_state 7240 */ 7241 un->un_last_state = un->un_save_state; 7242 /* 7243 * Note: throttle comes back at full. 7244 * Also note: this MUST be done before calling pm_raise_power 7245 * otherwise the system can get hung in biowait. The scenario where 7246 * this'll happen is under cpr suspend. Writing of the system 7247 * state goes through sddump, which writes 0 to un_throttle. If 7248 * writing the system state then fails, example if the partition is 7249 * too small, then cpr attempts a resume. If throttle isn't restored 7250 * from the saved value until after calling pm_raise_power then 7251 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 7252 * in biowait. 7253 */ 7254 un->un_throttle = un->un_saved_throttle; 7255 7256 /* 7257 * The chance of failure is very rare as the only command done in power 7258 * entry point is START command when you transition from 0->1 or 7259 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 7260 * which suspend was done. Ignore the return value as the resume should 7261 * not be failed. In the case of removable media the media need not be 7262 * inserted and hence there is a chance that raise power will fail with 7263 * media not present. 7264 */ 7265 if (un->un_f_attach_spinup) { 7266 mutex_exit(SD_MUTEX(un)); 7267 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 7268 mutex_enter(SD_MUTEX(un)); 7269 } 7270 7271 /* 7272 * Don't broadcast to the suspend cv and therefore possibly 7273 * start I/O until after power has been restored. 7274 */ 7275 cv_broadcast(&un->un_suspend_cv); 7276 cv_broadcast(&un->un_state_cv); 7277 7278 /* restart thread */ 7279 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 7280 scsi_watch_resume(un->un_swr_token); 7281 } 7282 7283 #if (defined(__fibre)) 7284 if (un->un_f_is_fibre == TRUE) { 7285 /* 7286 * Add callbacks for insert and remove events 7287 */ 7288 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7289 sd_init_event_callbacks(un); 7290 } 7291 } 7292 #endif 7293 7294 /* 7295 * Transport any pending commands to the target. 7296 * 7297 * If this is a low-activity device commands in queue will have to wait 7298 * until new commands come in, which may take awhile. Also, we 7299 * specifically don't check un_ncmds_in_transport because we know that 7300 * there really are no commands in progress after the unit was 7301 * suspended and we could have reached the throttle level, been 7302 * suspended, and have no new commands coming in for awhile. Highly 7303 * unlikely, but so is the low-activity disk scenario. 7304 */ 7305 ddi_xbuf_dispatch(un->un_xbuf_attr); 7306 7307 sd_start_cmds(un, NULL); 7308 mutex_exit(SD_MUTEX(un)); 7309 7310 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 7311 7312 return (DDI_SUCCESS); 7313 } 7314 7315 7316 /* 7317 * Function: sd_ddi_pm_resume 7318 * 7319 * Description: Set the drive state to powered on. 7320 * Someone else is required to actually change the drive 7321 * power level. 7322 * 7323 * Arguments: un - driver soft state (unit) structure 7324 * 7325 * Return Code: DDI_SUCCESS 7326 * 7327 * Context: Kernel thread context 7328 */ 7329 7330 static int 7331 sd_ddi_pm_resume(struct sd_lun *un) 7332 { 7333 ASSERT(un != NULL); 7334 7335 ASSERT(!mutex_owned(SD_MUTEX(un))); 7336 mutex_enter(SD_MUTEX(un)); 7337 un->un_power_level = SD_SPINDLE_ON; 7338 7339 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7340 mutex_enter(&un->un_pm_mutex); 7341 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7342 un->un_pm_count++; 7343 ASSERT(un->un_pm_count == 0); 7344 /* 7345 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7346 * un_suspend_cv is for a system resume, not a power management 7347 * device resume. (4297749) 7348 * cv_broadcast(&un->un_suspend_cv); 7349 */ 7350 } 7351 mutex_exit(&un->un_pm_mutex); 7352 mutex_exit(SD_MUTEX(un)); 7353 7354 return (DDI_SUCCESS); 7355 } 7356 7357 7358 /* 7359 * Function: sd_pm_idletimeout_handler 7360 * 7361 * Description: A timer routine that's active only while a device is busy. 7362 * The purpose is to extend slightly the pm framework's busy 7363 * view of the device to prevent busy/idle thrashing for 7364 * back-to-back commands. Do this by comparing the current time 7365 * to the time at which the last command completed and when the 7366 * difference is greater than sd_pm_idletime, call 7367 * pm_idle_component. In addition to indicating idle to the pm 7368 * framework, update the chain type to again use the internal pm 7369 * layers of the driver. 7370 * 7371 * Arguments: arg - driver soft state (unit) structure 7372 * 7373 * Context: Executes in a timeout(9F) thread context 7374 */ 7375 7376 static void 7377 sd_pm_idletimeout_handler(void *arg) 7378 { 7379 struct sd_lun *un = arg; 7380 7381 time_t now; 7382 7383 mutex_enter(&sd_detach_mutex); 7384 if (un->un_detach_count != 0) { 7385 /* Abort if the instance is detaching */ 7386 mutex_exit(&sd_detach_mutex); 7387 return; 7388 } 7389 mutex_exit(&sd_detach_mutex); 7390 7391 now = ddi_get_time(); 7392 /* 7393 * Grab both mutexes, in the proper order, since we're accessing 7394 * both PM and softstate variables. 7395 */ 7396 mutex_enter(SD_MUTEX(un)); 7397 mutex_enter(&un->un_pm_mutex); 7398 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7399 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7400 /* 7401 * Update the chain types. 7402 * This takes affect on the next new command received. 7403 */ 7404 if (un->un_f_non_devbsize_supported) { 7405 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7406 } else { 7407 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7408 } 7409 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7410 7411 SD_TRACE(SD_LOG_IO_PM, un, 7412 "sd_pm_idletimeout_handler: idling device\n"); 7413 (void) pm_idle_component(SD_DEVINFO(un), 0); 7414 un->un_pm_idle_timeid = NULL; 7415 } else { 7416 un->un_pm_idle_timeid = 7417 timeout(sd_pm_idletimeout_handler, un, 7418 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7419 } 7420 mutex_exit(&un->un_pm_mutex); 7421 mutex_exit(SD_MUTEX(un)); 7422 } 7423 7424 7425 /* 7426 * Function: sd_pm_timeout_handler 7427 * 7428 * Description: Callback to tell framework we are idle. 7429 * 7430 * Context: timeout(9f) thread context. 7431 */ 7432 7433 static void 7434 sd_pm_timeout_handler(void *arg) 7435 { 7436 struct sd_lun *un = arg; 7437 7438 (void) pm_idle_component(SD_DEVINFO(un), 0); 7439 mutex_enter(&un->un_pm_mutex); 7440 un->un_pm_timeid = NULL; 7441 mutex_exit(&un->un_pm_mutex); 7442 } 7443 7444 7445 /* 7446 * Function: sdpower 7447 * 7448 * Description: PM entry point. 7449 * 7450 * Return Code: DDI_SUCCESS 7451 * DDI_FAILURE 7452 * 7453 * Context: Kernel thread context 7454 */ 7455 7456 static int 7457 sdpower(dev_info_t *devi, int component, int level) 7458 { 7459 struct sd_lun *un; 7460 int instance; 7461 int rval = DDI_SUCCESS; 7462 uint_t i, log_page_size, maxcycles, ncycles; 7463 uchar_t *log_page_data; 7464 int log_sense_page; 7465 int medium_present; 7466 time_t intvlp; 7467 dev_t dev; 7468 struct pm_trans_data sd_pm_tran_data; 7469 uchar_t save_state; 7470 int sval; 7471 uchar_t state_before_pm; 7472 int got_semaphore_here; 7473 7474 instance = ddi_get_instance(devi); 7475 7476 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7477 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7478 component != 0) { 7479 return (DDI_FAILURE); 7480 } 7481 7482 dev = sd_make_device(SD_DEVINFO(un)); 7483 7484 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7485 7486 /* 7487 * Must synchronize power down with close. 7488 * Attempt to decrement/acquire the open/close semaphore, 7489 * but do NOT wait on it. If it's not greater than zero, 7490 * ie. it can't be decremented without waiting, then 7491 * someone else, either open or close, already has it 7492 * and the try returns 0. Use that knowledge here to determine 7493 * if it's OK to change the device power level. 7494 * Also, only increment it on exit if it was decremented, ie. gotten, 7495 * here. 7496 */ 7497 got_semaphore_here = sema_tryp(&un->un_semoclose); 7498 7499 mutex_enter(SD_MUTEX(un)); 7500 7501 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7502 un->un_ncmds_in_driver); 7503 7504 /* 7505 * If un_ncmds_in_driver is non-zero it indicates commands are 7506 * already being processed in the driver, or if the semaphore was 7507 * not gotten here it indicates an open or close is being processed. 7508 * At the same time somebody is requesting to go low power which 7509 * can't happen, therefore we need to return failure. 7510 */ 7511 if ((level == SD_SPINDLE_OFF) && 7512 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7513 mutex_exit(SD_MUTEX(un)); 7514 7515 if (got_semaphore_here != 0) { 7516 sema_v(&un->un_semoclose); 7517 } 7518 SD_TRACE(SD_LOG_IO_PM, un, 7519 "sdpower: exit, device has queued cmds.\n"); 7520 return (DDI_FAILURE); 7521 } 7522 7523 /* 7524 * if it is OFFLINE that means the disk is completely dead 7525 * in our case we have to put the disk in on or off by sending commands 7526 * Of course that will fail anyway so return back here. 7527 * 7528 * Power changes to a device that's OFFLINE or SUSPENDED 7529 * are not allowed. 7530 */ 7531 if ((un->un_state == SD_STATE_OFFLINE) || 7532 (un->un_state == SD_STATE_SUSPENDED)) { 7533 mutex_exit(SD_MUTEX(un)); 7534 7535 if (got_semaphore_here != 0) { 7536 sema_v(&un->un_semoclose); 7537 } 7538 SD_TRACE(SD_LOG_IO_PM, un, 7539 "sdpower: exit, device is off-line.\n"); 7540 return (DDI_FAILURE); 7541 } 7542 7543 /* 7544 * Change the device's state to indicate it's power level 7545 * is being changed. Do this to prevent a power off in the 7546 * middle of commands, which is especially bad on devices 7547 * that are really powered off instead of just spun down. 7548 */ 7549 state_before_pm = un->un_state; 7550 un->un_state = SD_STATE_PM_CHANGING; 7551 7552 mutex_exit(SD_MUTEX(un)); 7553 7554 /* 7555 * If "pm-capable" property is set to TRUE by HBA drivers, 7556 * bypass the following checking, otherwise, check the log 7557 * sense information for this device 7558 */ 7559 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7560 /* 7561 * Get the log sense information to understand whether the 7562 * the powercycle counts have gone beyond the threshhold. 7563 */ 7564 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7565 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7566 7567 mutex_enter(SD_MUTEX(un)); 7568 log_sense_page = un->un_start_stop_cycle_page; 7569 mutex_exit(SD_MUTEX(un)); 7570 7571 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7572 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7573 #ifdef SDDEBUG 7574 if (sd_force_pm_supported) { 7575 /* Force a successful result */ 7576 rval = 0; 7577 } 7578 #endif 7579 if (rval != 0) { 7580 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7581 "Log Sense Failed\n"); 7582 kmem_free(log_page_data, log_page_size); 7583 /* Cannot support power management on those drives */ 7584 7585 if (got_semaphore_here != 0) { 7586 sema_v(&un->un_semoclose); 7587 } 7588 /* 7589 * On exit put the state back to it's original value 7590 * and broadcast to anyone waiting for the power 7591 * change completion. 7592 */ 7593 mutex_enter(SD_MUTEX(un)); 7594 un->un_state = state_before_pm; 7595 cv_broadcast(&un->un_suspend_cv); 7596 mutex_exit(SD_MUTEX(un)); 7597 SD_TRACE(SD_LOG_IO_PM, un, 7598 "sdpower: exit, Log Sense Failed.\n"); 7599 return (DDI_FAILURE); 7600 } 7601 7602 /* 7603 * From the page data - Convert the essential information to 7604 * pm_trans_data 7605 */ 7606 maxcycles = 7607 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7608 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7609 7610 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7611 7612 ncycles = 7613 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7614 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7615 7616 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7617 7618 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7619 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7620 log_page_data[8+i]; 7621 } 7622 7623 kmem_free(log_page_data, log_page_size); 7624 7625 /* 7626 * Call pm_trans_check routine to get the Ok from 7627 * the global policy 7628 */ 7629 7630 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7631 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7632 7633 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7634 #ifdef SDDEBUG 7635 if (sd_force_pm_supported) { 7636 /* Force a successful result */ 7637 rval = 1; 7638 } 7639 #endif 7640 switch (rval) { 7641 case 0: 7642 /* 7643 * Not Ok to Power cycle or error in parameters passed 7644 * Would have given the advised time to consider power 7645 * cycle. Based on the new intvlp parameter we are 7646 * supposed to pretend we are busy so that pm framework 7647 * will never call our power entry point. Because of 7648 * that install a timeout handler and wait for the 7649 * recommended time to elapse so that power management 7650 * can be effective again. 7651 * 7652 * To effect this behavior, call pm_busy_component to 7653 * indicate to the framework this device is busy. 7654 * By not adjusting un_pm_count the rest of PM in 7655 * the driver will function normally, and independant 7656 * of this but because the framework is told the device 7657 * is busy it won't attempt powering down until it gets 7658 * a matching idle. The timeout handler sends this. 7659 * Note: sd_pm_entry can't be called here to do this 7660 * because sdpower may have been called as a result 7661 * of a call to pm_raise_power from within sd_pm_entry. 7662 * 7663 * If a timeout handler is already active then 7664 * don't install another. 7665 */ 7666 mutex_enter(&un->un_pm_mutex); 7667 if (un->un_pm_timeid == NULL) { 7668 un->un_pm_timeid = 7669 timeout(sd_pm_timeout_handler, 7670 un, intvlp * drv_usectohz(1000000)); 7671 mutex_exit(&un->un_pm_mutex); 7672 (void) pm_busy_component(SD_DEVINFO(un), 0); 7673 } else { 7674 mutex_exit(&un->un_pm_mutex); 7675 } 7676 if (got_semaphore_here != 0) { 7677 sema_v(&un->un_semoclose); 7678 } 7679 /* 7680 * On exit put the state back to it's original value 7681 * and broadcast to anyone waiting for the power 7682 * change completion. 7683 */ 7684 mutex_enter(SD_MUTEX(un)); 7685 un->un_state = state_before_pm; 7686 cv_broadcast(&un->un_suspend_cv); 7687 mutex_exit(SD_MUTEX(un)); 7688 7689 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7690 "trans check Failed, not ok to power cycle.\n"); 7691 return (DDI_FAILURE); 7692 7693 case -1: 7694 if (got_semaphore_here != 0) { 7695 sema_v(&un->un_semoclose); 7696 } 7697 /* 7698 * On exit put the state back to it's original value 7699 * and broadcast to anyone waiting for the power 7700 * change completion. 7701 */ 7702 mutex_enter(SD_MUTEX(un)); 7703 un->un_state = state_before_pm; 7704 cv_broadcast(&un->un_suspend_cv); 7705 mutex_exit(SD_MUTEX(un)); 7706 SD_TRACE(SD_LOG_IO_PM, un, 7707 "sdpower: exit, trans check command Failed.\n"); 7708 return (DDI_FAILURE); 7709 } 7710 } 7711 7712 if (level == SD_SPINDLE_OFF) { 7713 /* 7714 * Save the last state... if the STOP FAILS we need it 7715 * for restoring 7716 */ 7717 mutex_enter(SD_MUTEX(un)); 7718 save_state = un->un_last_state; 7719 /* 7720 * There must not be any cmds. getting processed 7721 * in the driver when we get here. Power to the 7722 * device is potentially going off. 7723 */ 7724 ASSERT(un->un_ncmds_in_driver == 0); 7725 mutex_exit(SD_MUTEX(un)); 7726 7727 /* 7728 * For now suspend the device completely before spindle is 7729 * turned off 7730 */ 7731 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7732 if (got_semaphore_here != 0) { 7733 sema_v(&un->un_semoclose); 7734 } 7735 /* 7736 * On exit put the state back to it's original value 7737 * and broadcast to anyone waiting for the power 7738 * change completion. 7739 */ 7740 mutex_enter(SD_MUTEX(un)); 7741 un->un_state = state_before_pm; 7742 cv_broadcast(&un->un_suspend_cv); 7743 mutex_exit(SD_MUTEX(un)); 7744 SD_TRACE(SD_LOG_IO_PM, un, 7745 "sdpower: exit, PM suspend Failed.\n"); 7746 return (DDI_FAILURE); 7747 } 7748 } 7749 7750 /* 7751 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7752 * close, or strategy. Dump no long uses this routine, it uses it's 7753 * own code so it can be done in polled mode. 7754 */ 7755 7756 medium_present = TRUE; 7757 7758 /* 7759 * When powering up, issue a TUR in case the device is at unit 7760 * attention. Don't do retries. Bypass the PM layer, otherwise 7761 * a deadlock on un_pm_busy_cv will occur. 7762 */ 7763 if (level == SD_SPINDLE_ON) { 7764 (void) sd_send_scsi_TEST_UNIT_READY(un, 7765 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7766 } 7767 7768 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7769 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7770 7771 sval = sd_send_scsi_START_STOP_UNIT(un, 7772 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7773 SD_PATH_DIRECT); 7774 /* Command failed, check for media present. */ 7775 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7776 medium_present = FALSE; 7777 } 7778 7779 /* 7780 * The conditions of interest here are: 7781 * if a spindle off with media present fails, 7782 * then restore the state and return an error. 7783 * else if a spindle on fails, 7784 * then return an error (there's no state to restore). 7785 * In all other cases we setup for the new state 7786 * and return success. 7787 */ 7788 switch (level) { 7789 case SD_SPINDLE_OFF: 7790 if ((medium_present == TRUE) && (sval != 0)) { 7791 /* The stop command from above failed */ 7792 rval = DDI_FAILURE; 7793 /* 7794 * The stop command failed, and we have media 7795 * present. Put the level back by calling the 7796 * sd_pm_resume() and set the state back to 7797 * it's previous value. 7798 */ 7799 (void) sd_ddi_pm_resume(un); 7800 mutex_enter(SD_MUTEX(un)); 7801 un->un_last_state = save_state; 7802 mutex_exit(SD_MUTEX(un)); 7803 break; 7804 } 7805 /* 7806 * The stop command from above succeeded. 7807 */ 7808 if (un->un_f_monitor_media_state) { 7809 /* 7810 * Terminate watch thread in case of removable media 7811 * devices going into low power state. This is as per 7812 * the requirements of pm framework, otherwise commands 7813 * will be generated for the device (through watch 7814 * thread), even when the device is in low power state. 7815 */ 7816 mutex_enter(SD_MUTEX(un)); 7817 un->un_f_watcht_stopped = FALSE; 7818 if (un->un_swr_token != NULL) { 7819 opaque_t temp_token = un->un_swr_token; 7820 un->un_f_watcht_stopped = TRUE; 7821 un->un_swr_token = NULL; 7822 mutex_exit(SD_MUTEX(un)); 7823 (void) scsi_watch_request_terminate(temp_token, 7824 SCSI_WATCH_TERMINATE_WAIT); 7825 } else { 7826 mutex_exit(SD_MUTEX(un)); 7827 } 7828 } 7829 break; 7830 7831 default: /* The level requested is spindle on... */ 7832 /* 7833 * Legacy behavior: return success on a failed spinup 7834 * if there is no media in the drive. 7835 * Do this by looking at medium_present here. 7836 */ 7837 if ((sval != 0) && medium_present) { 7838 /* The start command from above failed */ 7839 rval = DDI_FAILURE; 7840 break; 7841 } 7842 /* 7843 * The start command from above succeeded 7844 * Resume the devices now that we have 7845 * started the disks 7846 */ 7847 (void) sd_ddi_pm_resume(un); 7848 7849 /* 7850 * Resume the watch thread since it was suspended 7851 * when the device went into low power mode. 7852 */ 7853 if (un->un_f_monitor_media_state) { 7854 mutex_enter(SD_MUTEX(un)); 7855 if (un->un_f_watcht_stopped == TRUE) { 7856 opaque_t temp_token; 7857 7858 un->un_f_watcht_stopped = FALSE; 7859 mutex_exit(SD_MUTEX(un)); 7860 temp_token = scsi_watch_request_submit( 7861 SD_SCSI_DEVP(un), 7862 sd_check_media_time, 7863 SENSE_LENGTH, sd_media_watch_cb, 7864 (caddr_t)dev); 7865 mutex_enter(SD_MUTEX(un)); 7866 un->un_swr_token = temp_token; 7867 } 7868 mutex_exit(SD_MUTEX(un)); 7869 } 7870 } 7871 if (got_semaphore_here != 0) { 7872 sema_v(&un->un_semoclose); 7873 } 7874 /* 7875 * On exit put the state back to it's original value 7876 * and broadcast to anyone waiting for the power 7877 * change completion. 7878 */ 7879 mutex_enter(SD_MUTEX(un)); 7880 un->un_state = state_before_pm; 7881 cv_broadcast(&un->un_suspend_cv); 7882 mutex_exit(SD_MUTEX(un)); 7883 7884 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7885 7886 return (rval); 7887 } 7888 7889 7890 7891 /* 7892 * Function: sdattach 7893 * 7894 * Description: Driver's attach(9e) entry point function. 7895 * 7896 * Arguments: devi - opaque device info handle 7897 * cmd - attach type 7898 * 7899 * Return Code: DDI_SUCCESS 7900 * DDI_FAILURE 7901 * 7902 * Context: Kernel thread context 7903 */ 7904 7905 static int 7906 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7907 { 7908 switch (cmd) { 7909 case DDI_ATTACH: 7910 return (sd_unit_attach(devi)); 7911 case DDI_RESUME: 7912 return (sd_ddi_resume(devi)); 7913 default: 7914 break; 7915 } 7916 return (DDI_FAILURE); 7917 } 7918 7919 7920 /* 7921 * Function: sddetach 7922 * 7923 * Description: Driver's detach(9E) entry point function. 7924 * 7925 * Arguments: devi - opaque device info handle 7926 * cmd - detach type 7927 * 7928 * Return Code: DDI_SUCCESS 7929 * DDI_FAILURE 7930 * 7931 * Context: Kernel thread context 7932 */ 7933 7934 static int 7935 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7936 { 7937 switch (cmd) { 7938 case DDI_DETACH: 7939 return (sd_unit_detach(devi)); 7940 case DDI_SUSPEND: 7941 return (sd_ddi_suspend(devi)); 7942 default: 7943 break; 7944 } 7945 return (DDI_FAILURE); 7946 } 7947 7948 7949 /* 7950 * Function: sd_sync_with_callback 7951 * 7952 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7953 * state while the callback routine is active. 7954 * 7955 * Arguments: un: softstate structure for the instance 7956 * 7957 * Context: Kernel thread context 7958 */ 7959 7960 static void 7961 sd_sync_with_callback(struct sd_lun *un) 7962 { 7963 ASSERT(un != NULL); 7964 7965 mutex_enter(SD_MUTEX(un)); 7966 7967 ASSERT(un->un_in_callback >= 0); 7968 7969 while (un->un_in_callback > 0) { 7970 mutex_exit(SD_MUTEX(un)); 7971 delay(2); 7972 mutex_enter(SD_MUTEX(un)); 7973 } 7974 7975 mutex_exit(SD_MUTEX(un)); 7976 } 7977 7978 /* 7979 * Function: sd_unit_attach 7980 * 7981 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7982 * the soft state structure for the device and performs 7983 * all necessary structure and device initializations. 7984 * 7985 * Arguments: devi: the system's dev_info_t for the device. 7986 * 7987 * Return Code: DDI_SUCCESS if attach is successful. 7988 * DDI_FAILURE if any part of the attach fails. 7989 * 7990 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7991 * Kernel thread context only. Can sleep. 7992 */ 7993 7994 static int 7995 sd_unit_attach(dev_info_t *devi) 7996 { 7997 struct scsi_device *devp; 7998 struct sd_lun *un; 7999 char *variantp; 8000 int reservation_flag = SD_TARGET_IS_UNRESERVED; 8001 int instance; 8002 int rval; 8003 int wc_enabled; 8004 int tgt; 8005 uint64_t capacity; 8006 uint_t lbasize; 8007 dev_info_t *pdip = ddi_get_parent(devi); 8008 8009 /* 8010 * Retrieve the target driver's private data area. This was set 8011 * up by the HBA. 8012 */ 8013 devp = ddi_get_driver_private(devi); 8014 8015 /* 8016 * Retrieve the target ID of the device. 8017 */ 8018 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8019 SCSI_ADDR_PROP_TARGET, -1); 8020 8021 /* 8022 * Since we have no idea what state things were left in by the last 8023 * user of the device, set up some 'default' settings, ie. turn 'em 8024 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 8025 * Do this before the scsi_probe, which sends an inquiry. 8026 * This is a fix for bug (4430280). 8027 * Of special importance is wide-xfer. The drive could have been left 8028 * in wide transfer mode by the last driver to communicate with it, 8029 * this includes us. If that's the case, and if the following is not 8030 * setup properly or we don't re-negotiate with the drive prior to 8031 * transferring data to/from the drive, it causes bus parity errors, 8032 * data overruns, and unexpected interrupts. This first occurred when 8033 * the fix for bug (4378686) was made. 8034 */ 8035 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 8036 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 8037 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 8038 8039 /* 8040 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 8041 * on a target. Setting it per lun instance actually sets the 8042 * capability of this target, which affects those luns already 8043 * attached on the same target. So during attach, we can only disable 8044 * this capability only when no other lun has been attached on this 8045 * target. By doing this, we assume a target has the same tagged-qing 8046 * capability for every lun. The condition can be removed when HBA 8047 * is changed to support per lun based tagged-qing capability. 8048 */ 8049 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8050 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 8051 } 8052 8053 /* 8054 * Use scsi_probe() to issue an INQUIRY command to the device. 8055 * This call will allocate and fill in the scsi_inquiry structure 8056 * and point the sd_inq member of the scsi_device structure to it. 8057 * If the attach succeeds, then this memory will not be de-allocated 8058 * (via scsi_unprobe()) until the instance is detached. 8059 */ 8060 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 8061 goto probe_failed; 8062 } 8063 8064 /* 8065 * Check the device type as specified in the inquiry data and 8066 * claim it if it is of a type that we support. 8067 */ 8068 switch (devp->sd_inq->inq_dtype) { 8069 case DTYPE_DIRECT: 8070 break; 8071 case DTYPE_RODIRECT: 8072 break; 8073 case DTYPE_OPTICAL: 8074 break; 8075 case DTYPE_NOTPRESENT: 8076 default: 8077 /* Unsupported device type; fail the attach. */ 8078 goto probe_failed; 8079 } 8080 8081 /* 8082 * Allocate the soft state structure for this unit. 8083 * 8084 * We rely upon this memory being set to all zeroes by 8085 * ddi_soft_state_zalloc(). We assume that any member of the 8086 * soft state structure that is not explicitly initialized by 8087 * this routine will have a value of zero. 8088 */ 8089 instance = ddi_get_instance(devp->sd_dev); 8090 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 8091 goto probe_failed; 8092 } 8093 8094 /* 8095 * Retrieve a pointer to the newly-allocated soft state. 8096 * 8097 * This should NEVER fail if the ddi_soft_state_zalloc() call above 8098 * was successful, unless something has gone horribly wrong and the 8099 * ddi's soft state internals are corrupt (in which case it is 8100 * probably better to halt here than just fail the attach....) 8101 */ 8102 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 8103 panic("sd_unit_attach: NULL soft state on instance:0x%x", 8104 instance); 8105 /*NOTREACHED*/ 8106 } 8107 8108 /* 8109 * Link the back ptr of the driver soft state to the scsi_device 8110 * struct for this lun. 8111 * Save a pointer to the softstate in the driver-private area of 8112 * the scsi_device struct. 8113 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 8114 * we first set un->un_sd below. 8115 */ 8116 un->un_sd = devp; 8117 devp->sd_private = (opaque_t)un; 8118 8119 /* 8120 * The following must be after devp is stored in the soft state struct. 8121 */ 8122 #ifdef SDDEBUG 8123 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8124 "%s_unit_attach: un:0x%p instance:%d\n", 8125 ddi_driver_name(devi), un, instance); 8126 #endif 8127 8128 /* 8129 * Set up the device type and node type (for the minor nodes). 8130 * By default we assume that the device can at least support the 8131 * Common Command Set. Call it a CD-ROM if it reports itself 8132 * as a RODIRECT device. 8133 */ 8134 switch (devp->sd_inq->inq_dtype) { 8135 case DTYPE_RODIRECT: 8136 un->un_node_type = DDI_NT_CD_CHAN; 8137 un->un_ctype = CTYPE_CDROM; 8138 break; 8139 case DTYPE_OPTICAL: 8140 un->un_node_type = DDI_NT_BLOCK_CHAN; 8141 un->un_ctype = CTYPE_ROD; 8142 break; 8143 default: 8144 un->un_node_type = DDI_NT_BLOCK_CHAN; 8145 un->un_ctype = CTYPE_CCS; 8146 break; 8147 } 8148 8149 /* 8150 * Try to read the interconnect type from the HBA. 8151 * 8152 * Note: This driver is currently compiled as two binaries, a parallel 8153 * scsi version (sd) and a fibre channel version (ssd). All functional 8154 * differences are determined at compile time. In the future a single 8155 * binary will be provided and the inteconnect type will be used to 8156 * differentiate between fibre and parallel scsi behaviors. At that time 8157 * it will be necessary for all fibre channel HBAs to support this 8158 * property. 8159 * 8160 * set un_f_is_fiber to TRUE ( default fiber ) 8161 */ 8162 un->un_f_is_fibre = TRUE; 8163 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 8164 case INTERCONNECT_SSA: 8165 un->un_interconnect_type = SD_INTERCONNECT_SSA; 8166 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8167 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 8168 break; 8169 case INTERCONNECT_PARALLEL: 8170 un->un_f_is_fibre = FALSE; 8171 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8172 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8173 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 8174 break; 8175 case INTERCONNECT_SATA: 8176 un->un_f_is_fibre = FALSE; 8177 un->un_interconnect_type = SD_INTERCONNECT_SATA; 8178 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8179 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 8180 break; 8181 case INTERCONNECT_FIBRE: 8182 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 8183 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8184 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 8185 break; 8186 case INTERCONNECT_FABRIC: 8187 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 8188 un->un_node_type = DDI_NT_BLOCK_FABRIC; 8189 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8190 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 8191 break; 8192 default: 8193 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 8194 /* 8195 * The HBA does not support the "interconnect-type" property 8196 * (or did not provide a recognized type). 8197 * 8198 * Note: This will be obsoleted when a single fibre channel 8199 * and parallel scsi driver is delivered. In the meantime the 8200 * interconnect type will be set to the platform default.If that 8201 * type is not parallel SCSI, it means that we should be 8202 * assuming "ssd" semantics. However, here this also means that 8203 * the FC HBA is not supporting the "interconnect-type" property 8204 * like we expect it to, so log this occurrence. 8205 */ 8206 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 8207 if (!SD_IS_PARALLEL_SCSI(un)) { 8208 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8209 "sd_unit_attach: un:0x%p Assuming " 8210 "INTERCONNECT_FIBRE\n", un); 8211 } else { 8212 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8213 "sd_unit_attach: un:0x%p Assuming " 8214 "INTERCONNECT_PARALLEL\n", un); 8215 un->un_f_is_fibre = FALSE; 8216 } 8217 #else 8218 /* 8219 * Note: This source will be implemented when a single fibre 8220 * channel and parallel scsi driver is delivered. The default 8221 * will be to assume that if a device does not support the 8222 * "interconnect-type" property it is a parallel SCSI HBA and 8223 * we will set the interconnect type for parallel scsi. 8224 */ 8225 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8226 un->un_f_is_fibre = FALSE; 8227 #endif 8228 break; 8229 } 8230 8231 if (un->un_f_is_fibre == TRUE) { 8232 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 8233 SCSI_VERSION_3) { 8234 switch (un->un_interconnect_type) { 8235 case SD_INTERCONNECT_FIBRE: 8236 case SD_INTERCONNECT_SSA: 8237 un->un_node_type = DDI_NT_BLOCK_WWN; 8238 break; 8239 default: 8240 break; 8241 } 8242 } 8243 } 8244 8245 /* 8246 * Initialize the Request Sense command for the target 8247 */ 8248 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 8249 goto alloc_rqs_failed; 8250 } 8251 8252 /* 8253 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 8254 * with seperate binary for sd and ssd. 8255 * 8256 * x86 has 1 binary, un_retry_count is set base on connection type. 8257 * The hardcoded values will go away when Sparc uses 1 binary 8258 * for sd and ssd. This hardcoded values need to match 8259 * SD_RETRY_COUNT in sddef.h 8260 * The value used is base on interconnect type. 8261 * fibre = 3, parallel = 5 8262 */ 8263 #if defined(__i386) || defined(__amd64) 8264 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 8265 #else 8266 un->un_retry_count = SD_RETRY_COUNT; 8267 #endif 8268 8269 /* 8270 * Set the per disk retry count to the default number of retries 8271 * for disks and CDROMs. This value can be overridden by the 8272 * disk property list or an entry in sd.conf. 8273 */ 8274 un->un_notready_retry_count = 8275 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 8276 : DISK_NOT_READY_RETRY_COUNT(un); 8277 8278 /* 8279 * Set the busy retry count to the default value of un_retry_count. 8280 * This can be overridden by entries in sd.conf or the device 8281 * config table. 8282 */ 8283 un->un_busy_retry_count = un->un_retry_count; 8284 8285 /* 8286 * Init the reset threshold for retries. This number determines 8287 * how many retries must be performed before a reset can be issued 8288 * (for certain error conditions). This can be overridden by entries 8289 * in sd.conf or the device config table. 8290 */ 8291 un->un_reset_retry_count = (un->un_retry_count / 2); 8292 8293 /* 8294 * Set the victim_retry_count to the default un_retry_count 8295 */ 8296 un->un_victim_retry_count = (2 * un->un_retry_count); 8297 8298 /* 8299 * Set the reservation release timeout to the default value of 8300 * 5 seconds. This can be overridden by entries in ssd.conf or the 8301 * device config table. 8302 */ 8303 un->un_reserve_release_time = 5; 8304 8305 /* 8306 * Set up the default maximum transfer size. Note that this may 8307 * get updated later in the attach, when setting up default wide 8308 * operations for disks. 8309 */ 8310 #if defined(__i386) || defined(__amd64) 8311 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 8312 #else 8313 un->un_max_xfer_size = (uint_t)maxphys; 8314 #endif 8315 8316 /* 8317 * Get "allow bus device reset" property (defaults to "enabled" if 8318 * the property was not defined). This is to disable bus resets for 8319 * certain kinds of error recovery. Note: In the future when a run-time 8320 * fibre check is available the soft state flag should default to 8321 * enabled. 8322 */ 8323 if (un->un_f_is_fibre == TRUE) { 8324 un->un_f_allow_bus_device_reset = TRUE; 8325 } else { 8326 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8327 "allow-bus-device-reset", 1) != 0) { 8328 un->un_f_allow_bus_device_reset = TRUE; 8329 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8330 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 8331 un); 8332 } else { 8333 un->un_f_allow_bus_device_reset = FALSE; 8334 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8335 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 8336 un); 8337 } 8338 } 8339 8340 /* 8341 * Check if this is an ATAPI device. ATAPI devices use Group 1 8342 * Read/Write commands and Group 2 Mode Sense/Select commands. 8343 * 8344 * Note: The "obsolete" way of doing this is to check for the "atapi" 8345 * property. The new "variant" property with a value of "atapi" has been 8346 * introduced so that future 'variants' of standard SCSI behavior (like 8347 * atapi) could be specified by the underlying HBA drivers by supplying 8348 * a new value for the "variant" property, instead of having to define a 8349 * new property. 8350 */ 8351 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 8352 un->un_f_cfg_is_atapi = TRUE; 8353 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8354 "sd_unit_attach: un:0x%p Atapi device\n", un); 8355 } 8356 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8357 &variantp) == DDI_PROP_SUCCESS) { 8358 if (strcmp(variantp, "atapi") == 0) { 8359 un->un_f_cfg_is_atapi = TRUE; 8360 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8361 "sd_unit_attach: un:0x%p Atapi device\n", un); 8362 } 8363 ddi_prop_free(variantp); 8364 } 8365 8366 un->un_cmd_timeout = SD_IO_TIME; 8367 8368 /* Info on current states, statuses, etc. (Updated frequently) */ 8369 un->un_state = SD_STATE_NORMAL; 8370 un->un_last_state = SD_STATE_NORMAL; 8371 8372 /* Control & status info for command throttling */ 8373 un->un_throttle = sd_max_throttle; 8374 un->un_saved_throttle = sd_max_throttle; 8375 un->un_min_throttle = sd_min_throttle; 8376 8377 if (un->un_f_is_fibre == TRUE) { 8378 un->un_f_use_adaptive_throttle = TRUE; 8379 } else { 8380 un->un_f_use_adaptive_throttle = FALSE; 8381 } 8382 8383 /* Removable media support. */ 8384 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8385 un->un_mediastate = DKIO_NONE; 8386 un->un_specified_mediastate = DKIO_NONE; 8387 8388 /* CVs for suspend/resume (PM or DR) */ 8389 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8390 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8391 8392 /* Power management support. */ 8393 un->un_power_level = SD_SPINDLE_UNINIT; 8394 8395 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8396 un->un_f_wcc_inprog = 0; 8397 8398 /* 8399 * The open/close semaphore is used to serialize threads executing 8400 * in the driver's open & close entry point routines for a given 8401 * instance. 8402 */ 8403 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8404 8405 /* 8406 * The conf file entry and softstate variable is a forceful override, 8407 * meaning a non-zero value must be entered to change the default. 8408 */ 8409 un->un_f_disksort_disabled = FALSE; 8410 8411 /* 8412 * Retrieve the properties from the static driver table or the driver 8413 * configuration file (.conf) for this unit and update the soft state 8414 * for the device as needed for the indicated properties. 8415 * Note: the property configuration needs to occur here as some of the 8416 * following routines may have dependancies on soft state flags set 8417 * as part of the driver property configuration. 8418 */ 8419 sd_read_unit_properties(un); 8420 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8421 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8422 8423 /* 8424 * Only if a device has "hotpluggable" property, it is 8425 * treated as hotpluggable device. Otherwise, it is 8426 * regarded as non-hotpluggable one. 8427 */ 8428 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8429 -1) != -1) { 8430 un->un_f_is_hotpluggable = TRUE; 8431 } 8432 8433 /* 8434 * set unit's attributes(flags) according to "hotpluggable" and 8435 * RMB bit in INQUIRY data. 8436 */ 8437 sd_set_unit_attributes(un, devi); 8438 8439 /* 8440 * By default, we mark the capacity, lbasize, and geometry 8441 * as invalid. Only if we successfully read a valid capacity 8442 * will we update the un_blockcount and un_tgt_blocksize with the 8443 * valid values (the geometry will be validated later). 8444 */ 8445 un->un_f_blockcount_is_valid = FALSE; 8446 un->un_f_tgt_blocksize_is_valid = FALSE; 8447 un->un_f_geometry_is_valid = FALSE; 8448 8449 /* 8450 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8451 * otherwise. 8452 */ 8453 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8454 un->un_blockcount = 0; 8455 8456 /* 8457 * Set up the per-instance info needed to determine the correct 8458 * CDBs and other info for issuing commands to the target. 8459 */ 8460 sd_init_cdb_limits(un); 8461 8462 /* 8463 * Set up the IO chains to use, based upon the target type. 8464 */ 8465 if (un->un_f_non_devbsize_supported) { 8466 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8467 } else { 8468 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8469 } 8470 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8471 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8472 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8473 8474 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8475 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8476 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8477 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8478 8479 8480 if (ISCD(un)) { 8481 un->un_additional_codes = sd_additional_codes; 8482 } else { 8483 un->un_additional_codes = NULL; 8484 } 8485 8486 /* 8487 * Create the kstats here so they can be available for attach-time 8488 * routines that send commands to the unit (either polled or via 8489 * sd_send_scsi_cmd). 8490 * 8491 * Note: This is a critical sequence that needs to be maintained: 8492 * 1) Instantiate the kstats here, before any routines using the 8493 * iopath (i.e. sd_send_scsi_cmd). 8494 * 2) Instantiate and initialize the partition stats 8495 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 8496 * see detailed comments there. 8497 * 3) Initialize the error stats (sd_set_errstats), following 8498 * sd_validate_geometry(),sd_register_devid(), 8499 * and sd_cache_control(). 8500 */ 8501 8502 un->un_stats = kstat_create(sd_label, instance, 8503 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8504 if (un->un_stats != NULL) { 8505 un->un_stats->ks_lock = SD_MUTEX(un); 8506 kstat_install(un->un_stats); 8507 } 8508 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8509 "sd_unit_attach: un:0x%p un_stats created\n", un); 8510 8511 sd_create_errstats(un, instance); 8512 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8513 "sd_unit_attach: un:0x%p errstats created\n", un); 8514 8515 /* 8516 * The following if/else code was relocated here from below as part 8517 * of the fix for bug (4430280). However with the default setup added 8518 * on entry to this routine, it's no longer absolutely necessary for 8519 * this to be before the call to sd_spin_up_unit. 8520 */ 8521 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 8522 /* 8523 * If SCSI-2 tagged queueing is supported by the target 8524 * and by the host adapter then we will enable it. 8525 */ 8526 un->un_tagflags = 0; 8527 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8528 (devp->sd_inq->inq_cmdque) && 8529 (un->un_f_arq_enabled == TRUE)) { 8530 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8531 1, 1) == 1) { 8532 un->un_tagflags = FLAG_STAG; 8533 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8534 "sd_unit_attach: un:0x%p tag queueing " 8535 "enabled\n", un); 8536 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8537 "untagged-qing", 0) == 1) { 8538 un->un_f_opt_queueing = TRUE; 8539 un->un_saved_throttle = un->un_throttle = 8540 min(un->un_throttle, 3); 8541 } else { 8542 un->un_f_opt_queueing = FALSE; 8543 un->un_saved_throttle = un->un_throttle = 1; 8544 } 8545 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8546 == 1) && (un->un_f_arq_enabled == TRUE)) { 8547 /* The Host Adapter supports internal queueing. */ 8548 un->un_f_opt_queueing = TRUE; 8549 un->un_saved_throttle = un->un_throttle = 8550 min(un->un_throttle, 3); 8551 } else { 8552 un->un_f_opt_queueing = FALSE; 8553 un->un_saved_throttle = un->un_throttle = 1; 8554 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8555 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8556 } 8557 8558 /* 8559 * Enable large transfers for SATA/SAS drives 8560 */ 8561 if (SD_IS_SERIAL(un)) { 8562 un->un_max_xfer_size = 8563 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8564 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8565 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8566 "sd_unit_attach: un:0x%p max transfer " 8567 "size=0x%x\n", un, un->un_max_xfer_size); 8568 8569 } 8570 8571 /* Setup or tear down default wide operations for disks */ 8572 8573 /* 8574 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8575 * and "ssd_max_xfer_size" to exist simultaneously on the same 8576 * system and be set to different values. In the future this 8577 * code may need to be updated when the ssd module is 8578 * obsoleted and removed from the system. (4299588) 8579 */ 8580 if (SD_IS_PARALLEL_SCSI(un) && 8581 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 8582 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8583 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8584 1, 1) == 1) { 8585 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8586 "sd_unit_attach: un:0x%p Wide Transfer " 8587 "enabled\n", un); 8588 } 8589 8590 /* 8591 * If tagged queuing has also been enabled, then 8592 * enable large xfers 8593 */ 8594 if (un->un_saved_throttle == sd_max_throttle) { 8595 un->un_max_xfer_size = 8596 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8597 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8598 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8599 "sd_unit_attach: un:0x%p max transfer " 8600 "size=0x%x\n", un, un->un_max_xfer_size); 8601 } 8602 } else { 8603 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8604 0, 1) == 1) { 8605 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8606 "sd_unit_attach: un:0x%p " 8607 "Wide Transfer disabled\n", un); 8608 } 8609 } 8610 } else { 8611 un->un_tagflags = FLAG_STAG; 8612 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8613 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8614 } 8615 8616 /* 8617 * If this target supports LUN reset, try to enable it. 8618 */ 8619 if (un->un_f_lun_reset_enabled) { 8620 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8621 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8622 "un:0x%p lun_reset capability set\n", un); 8623 } else { 8624 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8625 "un:0x%p lun-reset capability not set\n", un); 8626 } 8627 } 8628 8629 /* 8630 * At this point in the attach, we have enough info in the 8631 * soft state to be able to issue commands to the target. 8632 * 8633 * All command paths used below MUST issue their commands as 8634 * SD_PATH_DIRECT. This is important as intermediate layers 8635 * are not all initialized yet (such as PM). 8636 */ 8637 8638 /* 8639 * Send a TEST UNIT READY command to the device. This should clear 8640 * any outstanding UNIT ATTENTION that may be present. 8641 * 8642 * Note: Don't check for success, just track if there is a reservation, 8643 * this is a throw away command to clear any unit attentions. 8644 * 8645 * Note: This MUST be the first command issued to the target during 8646 * attach to ensure power on UNIT ATTENTIONS are cleared. 8647 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8648 * with attempts at spinning up a device with no media. 8649 */ 8650 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8651 reservation_flag = SD_TARGET_IS_RESERVED; 8652 } 8653 8654 /* 8655 * If the device is NOT a removable media device, attempt to spin 8656 * it up (using the START_STOP_UNIT command) and read its capacity 8657 * (using the READ CAPACITY command). Note, however, that either 8658 * of these could fail and in some cases we would continue with 8659 * the attach despite the failure (see below). 8660 */ 8661 if (un->un_f_descr_format_supported) { 8662 switch (sd_spin_up_unit(un)) { 8663 case 0: 8664 /* 8665 * Spin-up was successful; now try to read the 8666 * capacity. If successful then save the results 8667 * and mark the capacity & lbasize as valid. 8668 */ 8669 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8670 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8671 8672 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8673 &lbasize, SD_PATH_DIRECT)) { 8674 case 0: { 8675 if (capacity > DK_MAX_BLOCKS) { 8676 #ifdef _LP64 8677 if (capacity + 1 > 8678 SD_GROUP1_MAX_ADDRESS) { 8679 /* 8680 * Enable descriptor format 8681 * sense data so that we can 8682 * get 64 bit sense data 8683 * fields. 8684 */ 8685 sd_enable_descr_sense(un); 8686 } 8687 #else 8688 /* 32-bit kernels can't handle this */ 8689 scsi_log(SD_DEVINFO(un), 8690 sd_label, CE_WARN, 8691 "disk has %llu blocks, which " 8692 "is too large for a 32-bit " 8693 "kernel", capacity); 8694 8695 #if defined(__i386) || defined(__amd64) 8696 /* 8697 * Refer to comments related to off-by-1 8698 * at the header of this file. 8699 * 1TB disk was treated as (1T - 512)B 8700 * in the past, so that it might has 8701 * valid VTOC and solaris partitions, 8702 * we have to allow it to continue to 8703 * work. 8704 */ 8705 if (capacity -1 > DK_MAX_BLOCKS) 8706 #endif 8707 goto spinup_failed; 8708 #endif 8709 } 8710 8711 /* 8712 * Here it's not necessary to check the case: 8713 * the capacity of the device is bigger than 8714 * what the max hba cdb can support. Because 8715 * sd_send_scsi_READ_CAPACITY will retrieve 8716 * the capacity by sending USCSI command, which 8717 * is constrained by the max hba cdb. Actually, 8718 * sd_send_scsi_READ_CAPACITY will return 8719 * EINVAL when using bigger cdb than required 8720 * cdb length. Will handle this case in 8721 * "case EINVAL". 8722 */ 8723 8724 /* 8725 * The following relies on 8726 * sd_send_scsi_READ_CAPACITY never 8727 * returning 0 for capacity and/or lbasize. 8728 */ 8729 sd_update_block_info(un, lbasize, capacity); 8730 8731 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8732 "sd_unit_attach: un:0x%p capacity = %ld " 8733 "blocks; lbasize= %ld.\n", un, 8734 un->un_blockcount, un->un_tgt_blocksize); 8735 8736 break; 8737 } 8738 case EINVAL: 8739 /* 8740 * In the case where the max-cdb-length property 8741 * is smaller than the required CDB length for 8742 * a SCSI device, a target driver can fail to 8743 * attach to that device. 8744 */ 8745 scsi_log(SD_DEVINFO(un), 8746 sd_label, CE_WARN, 8747 "disk capacity is too large " 8748 "for current cdb length"); 8749 goto spinup_failed; 8750 case EACCES: 8751 /* 8752 * Should never get here if the spin-up 8753 * succeeded, but code it in anyway. 8754 * From here, just continue with the attach... 8755 */ 8756 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8757 "sd_unit_attach: un:0x%p " 8758 "sd_send_scsi_READ_CAPACITY " 8759 "returned reservation conflict\n", un); 8760 reservation_flag = SD_TARGET_IS_RESERVED; 8761 break; 8762 default: 8763 /* 8764 * Likewise, should never get here if the 8765 * spin-up succeeded. Just continue with 8766 * the attach... 8767 */ 8768 break; 8769 } 8770 break; 8771 case EACCES: 8772 /* 8773 * Device is reserved by another host. In this case 8774 * we could not spin it up or read the capacity, but 8775 * we continue with the attach anyway. 8776 */ 8777 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8778 "sd_unit_attach: un:0x%p spin-up reservation " 8779 "conflict.\n", un); 8780 reservation_flag = SD_TARGET_IS_RESERVED; 8781 break; 8782 default: 8783 /* Fail the attach if the spin-up failed. */ 8784 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8785 "sd_unit_attach: un:0x%p spin-up failed.", un); 8786 goto spinup_failed; 8787 } 8788 } 8789 8790 /* 8791 * Check to see if this is a MMC drive 8792 */ 8793 if (ISCD(un)) { 8794 sd_set_mmc_caps(un); 8795 } 8796 8797 /* 8798 * Create the minor nodes for the device. 8799 * Note: If we want to support fdisk on both sparc and intel, this will 8800 * have to separate out the notion that VTOC8 is always sparc, and 8801 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8802 * type will have to be determined at run-time, and the fdisk 8803 * partitioning will have to have been read & set up before we 8804 * create the minor nodes. (any other inits (such as kstats) that 8805 * also ought to be done before creating the minor nodes?) (Doesn't 8806 * setting up the minor nodes kind of imply that we're ready to 8807 * handle an open from userland?) 8808 */ 8809 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8810 goto create_minor_nodes_failed; 8811 } 8812 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8813 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8814 8815 /* 8816 * Add a zero-length attribute to tell the world we support 8817 * kernel ioctls (for layered drivers) 8818 */ 8819 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8820 DDI_KERNEL_IOCTL, NULL, 0); 8821 8822 /* 8823 * Add a boolean property to tell the world we support 8824 * the B_FAILFAST flag (for layered drivers) 8825 */ 8826 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8827 "ddi-failfast-supported", NULL, 0); 8828 8829 /* 8830 * Initialize power management 8831 */ 8832 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8833 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8834 sd_setup_pm(un, devi); 8835 if (un->un_f_pm_is_enabled == FALSE) { 8836 /* 8837 * For performance, point to a jump table that does 8838 * not include pm. 8839 * The direct and priority chains don't change with PM. 8840 * 8841 * Note: this is currently done based on individual device 8842 * capabilities. When an interface for determining system 8843 * power enabled state becomes available, or when additional 8844 * layers are added to the command chain, these values will 8845 * have to be re-evaluated for correctness. 8846 */ 8847 if (un->un_f_non_devbsize_supported) { 8848 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8849 } else { 8850 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8851 } 8852 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8853 } 8854 8855 /* 8856 * This property is set to 0 by HA software to avoid retries 8857 * on a reserved disk. (The preferred property name is 8858 * "retry-on-reservation-conflict") (1189689) 8859 * 8860 * Note: The use of a global here can have unintended consequences. A 8861 * per instance variable is preferrable to match the capabilities of 8862 * different underlying hba's (4402600) 8863 */ 8864 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8865 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8866 sd_retry_on_reservation_conflict); 8867 if (sd_retry_on_reservation_conflict != 0) { 8868 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8869 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8870 sd_retry_on_reservation_conflict); 8871 } 8872 8873 /* Set up options for QFULL handling. */ 8874 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8875 "qfull-retries", -1)) != -1) { 8876 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8877 rval, 1); 8878 } 8879 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8880 "qfull-retry-interval", -1)) != -1) { 8881 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8882 rval, 1); 8883 } 8884 8885 /* 8886 * This just prints a message that announces the existence of the 8887 * device. The message is always printed in the system logfile, but 8888 * only appears on the console if the system is booted with the 8889 * -v (verbose) argument. 8890 */ 8891 ddi_report_dev(devi); 8892 8893 /* 8894 * The framework calls driver attach routines single-threaded 8895 * for a given instance. However we still acquire SD_MUTEX here 8896 * because this required for calling the sd_validate_geometry() 8897 * and sd_register_devid() functions. 8898 */ 8899 mutex_enter(SD_MUTEX(un)); 8900 un->un_f_geometry_is_valid = FALSE; 8901 un->un_mediastate = DKIO_NONE; 8902 un->un_reserved = -1; 8903 8904 /* 8905 * Read and validate the device's geometry (ie, disk label) 8906 * A new unformatted drive will not have a valid geometry, but 8907 * the driver needs to successfully attach to this device so 8908 * the drive can be formatted via ioctls. 8909 */ 8910 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8911 ENOTSUP)) && 8912 (un->un_blockcount < DK_MAX_BLOCKS)) { 8913 /* 8914 * We found a small disk with an EFI label on it; 8915 * we need to fix up the minor nodes accordingly. 8916 */ 8917 ddi_remove_minor_node(devi, "h"); 8918 ddi_remove_minor_node(devi, "h,raw"); 8919 (void) ddi_create_minor_node(devi, "wd", 8920 S_IFBLK, 8921 (instance << SDUNIT_SHIFT) | WD_NODE, 8922 un->un_node_type, NULL); 8923 (void) ddi_create_minor_node(devi, "wd,raw", 8924 S_IFCHR, 8925 (instance << SDUNIT_SHIFT) | WD_NODE, 8926 un->un_node_type, NULL); 8927 } 8928 #if defined(__i386) || defined(__amd64) 8929 else if (un->un_f_capacity_adjusted == 1) { 8930 /* 8931 * Refer to comments related to off-by-1 at the 8932 * header of this file. 8933 * Adjust minor node for 1TB disk. 8934 */ 8935 ddi_remove_minor_node(devi, "wd"); 8936 ddi_remove_minor_node(devi, "wd,raw"); 8937 (void) ddi_create_minor_node(devi, "h", 8938 S_IFBLK, 8939 (instance << SDUNIT_SHIFT) | WD_NODE, 8940 un->un_node_type, NULL); 8941 (void) ddi_create_minor_node(devi, "h,raw", 8942 S_IFCHR, 8943 (instance << SDUNIT_SHIFT) | WD_NODE, 8944 un->un_node_type, NULL); 8945 } 8946 #endif 8947 /* 8948 * Read and initialize the devid for the unit. 8949 */ 8950 ASSERT(un->un_errstats != NULL); 8951 if (un->un_f_devid_supported) { 8952 sd_register_devid(un, devi, reservation_flag); 8953 } 8954 mutex_exit(SD_MUTEX(un)); 8955 8956 #if (defined(__fibre)) 8957 /* 8958 * Register callbacks for fibre only. You can't do this soley 8959 * on the basis of the devid_type because this is hba specific. 8960 * We need to query our hba capabilities to find out whether to 8961 * register or not. 8962 */ 8963 if (un->un_f_is_fibre) { 8964 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8965 sd_init_event_callbacks(un); 8966 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8967 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8968 } 8969 } 8970 #endif 8971 8972 if (un->un_f_opt_disable_cache == TRUE) { 8973 /* 8974 * Disable both read cache and write cache. This is 8975 * the historic behavior of the keywords in the config file. 8976 */ 8977 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8978 0) { 8979 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8980 "sd_unit_attach: un:0x%p Could not disable " 8981 "caching", un); 8982 goto devid_failed; 8983 } 8984 } 8985 8986 /* 8987 * Check the value of the WCE bit now and 8988 * set un_f_write_cache_enabled accordingly. 8989 */ 8990 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8991 mutex_enter(SD_MUTEX(un)); 8992 un->un_f_write_cache_enabled = (wc_enabled != 0); 8993 mutex_exit(SD_MUTEX(un)); 8994 8995 /* 8996 * Set the pstat and error stat values here, so data obtained during the 8997 * previous attach-time routines is available. 8998 * 8999 * Note: This is a critical sequence that needs to be maintained: 9000 * 1) Instantiate the kstats before any routines using the iopath 9001 * (i.e. sd_send_scsi_cmd). 9002 * 2) Instantiate and initialize the partition stats 9003 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 9004 * see detailed comments there. 9005 * 3) Initialize the error stats (sd_set_errstats), following 9006 * sd_validate_geometry(),sd_register_devid(), 9007 * and sd_cache_control(). 9008 */ 9009 sd_set_errstats(un); 9010 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9011 "sd_unit_attach: un:0x%p errstats set\n", un); 9012 9013 /* 9014 * Find out what type of reservation this disk supports. 9015 */ 9016 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 9017 case 0: 9018 /* 9019 * SCSI-3 reservations are supported. 9020 */ 9021 un->un_reservation_type = SD_SCSI3_RESERVATION; 9022 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9023 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 9024 break; 9025 case ENOTSUP: 9026 /* 9027 * The PERSISTENT RESERVE IN command would not be recognized by 9028 * a SCSI-2 device, so assume the reservation type is SCSI-2. 9029 */ 9030 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9031 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 9032 un->un_reservation_type = SD_SCSI2_RESERVATION; 9033 break; 9034 default: 9035 /* 9036 * default to SCSI-3 reservations 9037 */ 9038 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9039 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 9040 un->un_reservation_type = SD_SCSI3_RESERVATION; 9041 break; 9042 } 9043 9044 /* 9045 * After successfully attaching an instance, we record the information 9046 * of how many luns have been attached on the relative target and 9047 * controller for parallel SCSI. This information is used when sd tries 9048 * to set the tagged queuing capability in HBA. 9049 */ 9050 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9051 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 9052 } 9053 9054 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9055 "sd_unit_attach: un:0x%p exit success\n", un); 9056 9057 return (DDI_SUCCESS); 9058 9059 /* 9060 * An error occurred during the attach; clean up & return failure. 9061 */ 9062 9063 devid_failed: 9064 9065 setup_pm_failed: 9066 ddi_remove_minor_node(devi, NULL); 9067 9068 create_minor_nodes_failed: 9069 /* 9070 * Cleanup from the scsi_ifsetcap() calls (437868) 9071 */ 9072 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9073 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9074 9075 /* 9076 * Refer to the comments of setting tagged-qing in the beginning of 9077 * sd_unit_attach. We can only disable tagged queuing when there is 9078 * no lun attached on the target. 9079 */ 9080 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 9081 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9082 } 9083 9084 if (un->un_f_is_fibre == FALSE) { 9085 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9086 } 9087 9088 spinup_failed: 9089 9090 mutex_enter(SD_MUTEX(un)); 9091 9092 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 9093 if (un->un_direct_priority_timeid != NULL) { 9094 timeout_id_t temp_id = un->un_direct_priority_timeid; 9095 un->un_direct_priority_timeid = NULL; 9096 mutex_exit(SD_MUTEX(un)); 9097 (void) untimeout(temp_id); 9098 mutex_enter(SD_MUTEX(un)); 9099 } 9100 9101 /* Cancel any pending start/stop timeouts */ 9102 if (un->un_startstop_timeid != NULL) { 9103 timeout_id_t temp_id = un->un_startstop_timeid; 9104 un->un_startstop_timeid = NULL; 9105 mutex_exit(SD_MUTEX(un)); 9106 (void) untimeout(temp_id); 9107 mutex_enter(SD_MUTEX(un)); 9108 } 9109 9110 /* Cancel any pending reset-throttle timeouts */ 9111 if (un->un_reset_throttle_timeid != NULL) { 9112 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9113 un->un_reset_throttle_timeid = NULL; 9114 mutex_exit(SD_MUTEX(un)); 9115 (void) untimeout(temp_id); 9116 mutex_enter(SD_MUTEX(un)); 9117 } 9118 9119 /* Cancel any pending retry timeouts */ 9120 if (un->un_retry_timeid != NULL) { 9121 timeout_id_t temp_id = un->un_retry_timeid; 9122 un->un_retry_timeid = NULL; 9123 mutex_exit(SD_MUTEX(un)); 9124 (void) untimeout(temp_id); 9125 mutex_enter(SD_MUTEX(un)); 9126 } 9127 9128 /* Cancel any pending delayed cv broadcast timeouts */ 9129 if (un->un_dcvb_timeid != NULL) { 9130 timeout_id_t temp_id = un->un_dcvb_timeid; 9131 un->un_dcvb_timeid = NULL; 9132 mutex_exit(SD_MUTEX(un)); 9133 (void) untimeout(temp_id); 9134 mutex_enter(SD_MUTEX(un)); 9135 } 9136 9137 mutex_exit(SD_MUTEX(un)); 9138 9139 /* There should not be any in-progress I/O so ASSERT this check */ 9140 ASSERT(un->un_ncmds_in_transport == 0); 9141 ASSERT(un->un_ncmds_in_driver == 0); 9142 9143 /* Do not free the softstate if the callback routine is active */ 9144 sd_sync_with_callback(un); 9145 9146 /* 9147 * Partition stats apparently are not used with removables. These would 9148 * not have been created during attach, so no need to clean them up... 9149 */ 9150 if (un->un_stats != NULL) { 9151 kstat_delete(un->un_stats); 9152 un->un_stats = NULL; 9153 } 9154 if (un->un_errstats != NULL) { 9155 kstat_delete(un->un_errstats); 9156 un->un_errstats = NULL; 9157 } 9158 9159 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9160 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9161 9162 ddi_prop_remove_all(devi); 9163 sema_destroy(&un->un_semoclose); 9164 cv_destroy(&un->un_state_cv); 9165 9166 getrbuf_failed: 9167 9168 sd_free_rqs(un); 9169 9170 alloc_rqs_failed: 9171 9172 devp->sd_private = NULL; 9173 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 9174 9175 get_softstate_failed: 9176 /* 9177 * Note: the man pages are unclear as to whether or not doing a 9178 * ddi_soft_state_free(sd_state, instance) is the right way to 9179 * clean up after the ddi_soft_state_zalloc() if the subsequent 9180 * ddi_get_soft_state() fails. The implication seems to be 9181 * that the get_soft_state cannot fail if the zalloc succeeds. 9182 */ 9183 ddi_soft_state_free(sd_state, instance); 9184 9185 probe_failed: 9186 scsi_unprobe(devp); 9187 #ifdef SDDEBUG 9188 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 9189 (sd_level_mask & SD_LOGMASK_TRACE)) { 9190 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 9191 (void *)un); 9192 } 9193 #endif 9194 return (DDI_FAILURE); 9195 } 9196 9197 9198 /* 9199 * Function: sd_unit_detach 9200 * 9201 * Description: Performs DDI_DETACH processing for sddetach(). 9202 * 9203 * Return Code: DDI_SUCCESS 9204 * DDI_FAILURE 9205 * 9206 * Context: Kernel thread context 9207 */ 9208 9209 static int 9210 sd_unit_detach(dev_info_t *devi) 9211 { 9212 struct scsi_device *devp; 9213 struct sd_lun *un; 9214 int i; 9215 int tgt; 9216 dev_t dev; 9217 dev_info_t *pdip = ddi_get_parent(devi); 9218 int instance = ddi_get_instance(devi); 9219 9220 mutex_enter(&sd_detach_mutex); 9221 9222 /* 9223 * Fail the detach for any of the following: 9224 * - Unable to get the sd_lun struct for the instance 9225 * - A layered driver has an outstanding open on the instance 9226 * - Another thread is already detaching this instance 9227 * - Another thread is currently performing an open 9228 */ 9229 devp = ddi_get_driver_private(devi); 9230 if ((devp == NULL) || 9231 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 9232 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 9233 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 9234 mutex_exit(&sd_detach_mutex); 9235 return (DDI_FAILURE); 9236 } 9237 9238 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 9239 9240 /* 9241 * Mark this instance as currently in a detach, to inhibit any 9242 * opens from a layered driver. 9243 */ 9244 un->un_detach_count++; 9245 mutex_exit(&sd_detach_mutex); 9246 9247 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 9248 SCSI_ADDR_PROP_TARGET, -1); 9249 9250 dev = sd_make_device(SD_DEVINFO(un)); 9251 9252 #ifndef lint 9253 _NOTE(COMPETING_THREADS_NOW); 9254 #endif 9255 9256 mutex_enter(SD_MUTEX(un)); 9257 9258 /* 9259 * Fail the detach if there are any outstanding layered 9260 * opens on this device. 9261 */ 9262 for (i = 0; i < NDKMAP; i++) { 9263 if (un->un_ocmap.lyropen[i] != 0) { 9264 goto err_notclosed; 9265 } 9266 } 9267 9268 /* 9269 * Verify there are NO outstanding commands issued to this device. 9270 * ie, un_ncmds_in_transport == 0. 9271 * It's possible to have outstanding commands through the physio 9272 * code path, even though everything's closed. 9273 */ 9274 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 9275 (un->un_direct_priority_timeid != NULL) || 9276 (un->un_state == SD_STATE_RWAIT)) { 9277 mutex_exit(SD_MUTEX(un)); 9278 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9279 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 9280 goto err_stillbusy; 9281 } 9282 9283 /* 9284 * If we have the device reserved, release the reservation. 9285 */ 9286 if ((un->un_resvd_status & SD_RESERVE) && 9287 !(un->un_resvd_status & SD_LOST_RESERVE)) { 9288 mutex_exit(SD_MUTEX(un)); 9289 /* 9290 * Note: sd_reserve_release sends a command to the device 9291 * via the sd_ioctlcmd() path, and can sleep. 9292 */ 9293 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 9294 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9295 "sd_dr_detach: Cannot release reservation \n"); 9296 } 9297 } else { 9298 mutex_exit(SD_MUTEX(un)); 9299 } 9300 9301 /* 9302 * Untimeout any reserve recover, throttle reset, restart unit 9303 * and delayed broadcast timeout threads. Protect the timeout pointer 9304 * from getting nulled by their callback functions. 9305 */ 9306 mutex_enter(SD_MUTEX(un)); 9307 if (un->un_resvd_timeid != NULL) { 9308 timeout_id_t temp_id = un->un_resvd_timeid; 9309 un->un_resvd_timeid = NULL; 9310 mutex_exit(SD_MUTEX(un)); 9311 (void) untimeout(temp_id); 9312 mutex_enter(SD_MUTEX(un)); 9313 } 9314 9315 if (un->un_reset_throttle_timeid != NULL) { 9316 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9317 un->un_reset_throttle_timeid = NULL; 9318 mutex_exit(SD_MUTEX(un)); 9319 (void) untimeout(temp_id); 9320 mutex_enter(SD_MUTEX(un)); 9321 } 9322 9323 if (un->un_startstop_timeid != NULL) { 9324 timeout_id_t temp_id = un->un_startstop_timeid; 9325 un->un_startstop_timeid = NULL; 9326 mutex_exit(SD_MUTEX(un)); 9327 (void) untimeout(temp_id); 9328 mutex_enter(SD_MUTEX(un)); 9329 } 9330 9331 if (un->un_dcvb_timeid != NULL) { 9332 timeout_id_t temp_id = un->un_dcvb_timeid; 9333 un->un_dcvb_timeid = NULL; 9334 mutex_exit(SD_MUTEX(un)); 9335 (void) untimeout(temp_id); 9336 } else { 9337 mutex_exit(SD_MUTEX(un)); 9338 } 9339 9340 /* Remove any pending reservation reclaim requests for this device */ 9341 sd_rmv_resv_reclaim_req(dev); 9342 9343 mutex_enter(SD_MUTEX(un)); 9344 9345 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 9346 if (un->un_direct_priority_timeid != NULL) { 9347 timeout_id_t temp_id = un->un_direct_priority_timeid; 9348 un->un_direct_priority_timeid = NULL; 9349 mutex_exit(SD_MUTEX(un)); 9350 (void) untimeout(temp_id); 9351 mutex_enter(SD_MUTEX(un)); 9352 } 9353 9354 /* Cancel any active multi-host disk watch thread requests */ 9355 if (un->un_mhd_token != NULL) { 9356 mutex_exit(SD_MUTEX(un)); 9357 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 9358 if (scsi_watch_request_terminate(un->un_mhd_token, 9359 SCSI_WATCH_TERMINATE_NOWAIT)) { 9360 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9361 "sd_dr_detach: Cannot cancel mhd watch request\n"); 9362 /* 9363 * Note: We are returning here after having removed 9364 * some driver timeouts above. This is consistent with 9365 * the legacy implementation but perhaps the watch 9366 * terminate call should be made with the wait flag set. 9367 */ 9368 goto err_stillbusy; 9369 } 9370 mutex_enter(SD_MUTEX(un)); 9371 un->un_mhd_token = NULL; 9372 } 9373 9374 if (un->un_swr_token != NULL) { 9375 mutex_exit(SD_MUTEX(un)); 9376 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 9377 if (scsi_watch_request_terminate(un->un_swr_token, 9378 SCSI_WATCH_TERMINATE_NOWAIT)) { 9379 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9380 "sd_dr_detach: Cannot cancel swr watch request\n"); 9381 /* 9382 * Note: We are returning here after having removed 9383 * some driver timeouts above. This is consistent with 9384 * the legacy implementation but perhaps the watch 9385 * terminate call should be made with the wait flag set. 9386 */ 9387 goto err_stillbusy; 9388 } 9389 mutex_enter(SD_MUTEX(un)); 9390 un->un_swr_token = NULL; 9391 } 9392 9393 mutex_exit(SD_MUTEX(un)); 9394 9395 /* 9396 * Clear any scsi_reset_notifies. We clear the reset notifies 9397 * if we have not registered one. 9398 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 9399 */ 9400 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 9401 sd_mhd_reset_notify_cb, (caddr_t)un); 9402 9403 /* 9404 * protect the timeout pointers from getting nulled by 9405 * their callback functions during the cancellation process. 9406 * In such a scenario untimeout can be invoked with a null value. 9407 */ 9408 _NOTE(NO_COMPETING_THREADS_NOW); 9409 9410 mutex_enter(&un->un_pm_mutex); 9411 if (un->un_pm_idle_timeid != NULL) { 9412 timeout_id_t temp_id = un->un_pm_idle_timeid; 9413 un->un_pm_idle_timeid = NULL; 9414 mutex_exit(&un->un_pm_mutex); 9415 9416 /* 9417 * Timeout is active; cancel it. 9418 * Note that it'll never be active on a device 9419 * that does not support PM therefore we don't 9420 * have to check before calling pm_idle_component. 9421 */ 9422 (void) untimeout(temp_id); 9423 (void) pm_idle_component(SD_DEVINFO(un), 0); 9424 mutex_enter(&un->un_pm_mutex); 9425 } 9426 9427 /* 9428 * Check whether there is already a timeout scheduled for power 9429 * management. If yes then don't lower the power here, that's. 9430 * the timeout handler's job. 9431 */ 9432 if (un->un_pm_timeid != NULL) { 9433 timeout_id_t temp_id = un->un_pm_timeid; 9434 un->un_pm_timeid = NULL; 9435 mutex_exit(&un->un_pm_mutex); 9436 /* 9437 * Timeout is active; cancel it. 9438 * Note that it'll never be active on a device 9439 * that does not support PM therefore we don't 9440 * have to check before calling pm_idle_component. 9441 */ 9442 (void) untimeout(temp_id); 9443 (void) pm_idle_component(SD_DEVINFO(un), 0); 9444 9445 } else { 9446 mutex_exit(&un->un_pm_mutex); 9447 if ((un->un_f_pm_is_enabled == TRUE) && 9448 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 9449 DDI_SUCCESS)) { 9450 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9451 "sd_dr_detach: Lower power request failed, ignoring.\n"); 9452 /* 9453 * Fix for bug: 4297749, item # 13 9454 * The above test now includes a check to see if PM is 9455 * supported by this device before call 9456 * pm_lower_power(). 9457 * Note, the following is not dead code. The call to 9458 * pm_lower_power above will generate a call back into 9459 * our sdpower routine which might result in a timeout 9460 * handler getting activated. Therefore the following 9461 * code is valid and necessary. 9462 */ 9463 mutex_enter(&un->un_pm_mutex); 9464 if (un->un_pm_timeid != NULL) { 9465 timeout_id_t temp_id = un->un_pm_timeid; 9466 un->un_pm_timeid = NULL; 9467 mutex_exit(&un->un_pm_mutex); 9468 (void) untimeout(temp_id); 9469 (void) pm_idle_component(SD_DEVINFO(un), 0); 9470 } else { 9471 mutex_exit(&un->un_pm_mutex); 9472 } 9473 } 9474 } 9475 9476 /* 9477 * Cleanup from the scsi_ifsetcap() calls (437868) 9478 * Relocated here from above to be after the call to 9479 * pm_lower_power, which was getting errors. 9480 */ 9481 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9482 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9483 9484 /* 9485 * Currently, tagged queuing is supported per target based by HBA. 9486 * Setting this per lun instance actually sets the capability of this 9487 * target in HBA, which affects those luns already attached on the 9488 * same target. So during detach, we can only disable this capability 9489 * only when this is the only lun left on this target. By doing 9490 * this, we assume a target has the same tagged queuing capability 9491 * for every lun. The condition can be removed when HBA is changed to 9492 * support per lun based tagged queuing capability. 9493 */ 9494 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 9495 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9496 } 9497 9498 if (un->un_f_is_fibre == FALSE) { 9499 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9500 } 9501 9502 /* 9503 * Remove any event callbacks, fibre only 9504 */ 9505 if (un->un_f_is_fibre == TRUE) { 9506 if ((un->un_insert_event != NULL) && 9507 (ddi_remove_event_handler(un->un_insert_cb_id) != 9508 DDI_SUCCESS)) { 9509 /* 9510 * Note: We are returning here after having done 9511 * substantial cleanup above. This is consistent 9512 * with the legacy implementation but this may not 9513 * be the right thing to do. 9514 */ 9515 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9516 "sd_dr_detach: Cannot cancel insert event\n"); 9517 goto err_remove_event; 9518 } 9519 un->un_insert_event = NULL; 9520 9521 if ((un->un_remove_event != NULL) && 9522 (ddi_remove_event_handler(un->un_remove_cb_id) != 9523 DDI_SUCCESS)) { 9524 /* 9525 * Note: We are returning here after having done 9526 * substantial cleanup above. This is consistent 9527 * with the legacy implementation but this may not 9528 * be the right thing to do. 9529 */ 9530 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9531 "sd_dr_detach: Cannot cancel remove event\n"); 9532 goto err_remove_event; 9533 } 9534 un->un_remove_event = NULL; 9535 } 9536 9537 /* Do not free the softstate if the callback routine is active */ 9538 sd_sync_with_callback(un); 9539 9540 /* 9541 * Hold the detach mutex here, to make sure that no other threads ever 9542 * can access a (partially) freed soft state structure. 9543 */ 9544 mutex_enter(&sd_detach_mutex); 9545 9546 /* 9547 * Clean up the soft state struct. 9548 * Cleanup is done in reverse order of allocs/inits. 9549 * At this point there should be no competing threads anymore. 9550 */ 9551 9552 /* Unregister and free device id. */ 9553 ddi_devid_unregister(devi); 9554 if (un->un_devid) { 9555 ddi_devid_free(un->un_devid); 9556 un->un_devid = NULL; 9557 } 9558 9559 /* 9560 * Destroy wmap cache if it exists. 9561 */ 9562 if (un->un_wm_cache != NULL) { 9563 kmem_cache_destroy(un->un_wm_cache); 9564 un->un_wm_cache = NULL; 9565 } 9566 9567 /* Remove minor nodes */ 9568 ddi_remove_minor_node(devi, NULL); 9569 9570 /* 9571 * kstat cleanup is done in detach for all device types (4363169). 9572 * We do not want to fail detach if the device kstats are not deleted 9573 * since there is a confusion about the devo_refcnt for the device. 9574 * We just delete the kstats and let detach complete successfully. 9575 */ 9576 if (un->un_stats != NULL) { 9577 kstat_delete(un->un_stats); 9578 un->un_stats = NULL; 9579 } 9580 if (un->un_errstats != NULL) { 9581 kstat_delete(un->un_errstats); 9582 un->un_errstats = NULL; 9583 } 9584 9585 /* Remove partition stats */ 9586 if (un->un_f_pkstats_enabled) { 9587 for (i = 0; i < NSDMAP; i++) { 9588 if (un->un_pstats[i] != NULL) { 9589 kstat_delete(un->un_pstats[i]); 9590 un->un_pstats[i] = NULL; 9591 } 9592 } 9593 } 9594 9595 /* Remove xbuf registration */ 9596 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9597 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9598 9599 /* Remove driver properties */ 9600 ddi_prop_remove_all(devi); 9601 9602 mutex_destroy(&un->un_pm_mutex); 9603 cv_destroy(&un->un_pm_busy_cv); 9604 9605 cv_destroy(&un->un_wcc_cv); 9606 9607 /* Open/close semaphore */ 9608 sema_destroy(&un->un_semoclose); 9609 9610 /* Removable media condvar. */ 9611 cv_destroy(&un->un_state_cv); 9612 9613 /* Suspend/resume condvar. */ 9614 cv_destroy(&un->un_suspend_cv); 9615 cv_destroy(&un->un_disk_busy_cv); 9616 9617 sd_free_rqs(un); 9618 9619 /* Free up soft state */ 9620 devp->sd_private = NULL; 9621 bzero(un, sizeof (struct sd_lun)); 9622 ddi_soft_state_free(sd_state, instance); 9623 9624 mutex_exit(&sd_detach_mutex); 9625 9626 /* This frees up the INQUIRY data associated with the device. */ 9627 scsi_unprobe(devp); 9628 9629 /* 9630 * After successfully detaching an instance, we update the information 9631 * of how many luns have been attached in the relative target and 9632 * controller for parallel SCSI. This information is used when sd tries 9633 * to set the tagged queuing capability in HBA. 9634 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 9635 * check if the device is parallel SCSI. However, we don't need to 9636 * check here because we've already checked during attach. No device 9637 * that is not parallel SCSI is in the chain. 9638 */ 9639 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9640 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9641 } 9642 9643 return (DDI_SUCCESS); 9644 9645 err_notclosed: 9646 mutex_exit(SD_MUTEX(un)); 9647 9648 err_stillbusy: 9649 _NOTE(NO_COMPETING_THREADS_NOW); 9650 9651 err_remove_event: 9652 mutex_enter(&sd_detach_mutex); 9653 un->un_detach_count--; 9654 mutex_exit(&sd_detach_mutex); 9655 9656 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9657 return (DDI_FAILURE); 9658 } 9659 9660 9661 /* 9662 * Driver minor node structure and data table 9663 */ 9664 struct driver_minor_data { 9665 char *name; 9666 minor_t minor; 9667 int type; 9668 }; 9669 9670 static struct driver_minor_data sd_minor_data[] = { 9671 {"a", 0, S_IFBLK}, 9672 {"b", 1, S_IFBLK}, 9673 {"c", 2, S_IFBLK}, 9674 {"d", 3, S_IFBLK}, 9675 {"e", 4, S_IFBLK}, 9676 {"f", 5, S_IFBLK}, 9677 {"g", 6, S_IFBLK}, 9678 {"h", 7, S_IFBLK}, 9679 #if defined(_SUNOS_VTOC_16) 9680 {"i", 8, S_IFBLK}, 9681 {"j", 9, S_IFBLK}, 9682 {"k", 10, S_IFBLK}, 9683 {"l", 11, S_IFBLK}, 9684 {"m", 12, S_IFBLK}, 9685 {"n", 13, S_IFBLK}, 9686 {"o", 14, S_IFBLK}, 9687 {"p", 15, S_IFBLK}, 9688 #endif /* defined(_SUNOS_VTOC_16) */ 9689 #if defined(_FIRMWARE_NEEDS_FDISK) 9690 {"q", 16, S_IFBLK}, 9691 {"r", 17, S_IFBLK}, 9692 {"s", 18, S_IFBLK}, 9693 {"t", 19, S_IFBLK}, 9694 {"u", 20, S_IFBLK}, 9695 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9696 {"a,raw", 0, S_IFCHR}, 9697 {"b,raw", 1, S_IFCHR}, 9698 {"c,raw", 2, S_IFCHR}, 9699 {"d,raw", 3, S_IFCHR}, 9700 {"e,raw", 4, S_IFCHR}, 9701 {"f,raw", 5, S_IFCHR}, 9702 {"g,raw", 6, S_IFCHR}, 9703 {"h,raw", 7, S_IFCHR}, 9704 #if defined(_SUNOS_VTOC_16) 9705 {"i,raw", 8, S_IFCHR}, 9706 {"j,raw", 9, S_IFCHR}, 9707 {"k,raw", 10, S_IFCHR}, 9708 {"l,raw", 11, S_IFCHR}, 9709 {"m,raw", 12, S_IFCHR}, 9710 {"n,raw", 13, S_IFCHR}, 9711 {"o,raw", 14, S_IFCHR}, 9712 {"p,raw", 15, S_IFCHR}, 9713 #endif /* defined(_SUNOS_VTOC_16) */ 9714 #if defined(_FIRMWARE_NEEDS_FDISK) 9715 {"q,raw", 16, S_IFCHR}, 9716 {"r,raw", 17, S_IFCHR}, 9717 {"s,raw", 18, S_IFCHR}, 9718 {"t,raw", 19, S_IFCHR}, 9719 {"u,raw", 20, S_IFCHR}, 9720 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9721 {0} 9722 }; 9723 9724 static struct driver_minor_data sd_minor_data_efi[] = { 9725 {"a", 0, S_IFBLK}, 9726 {"b", 1, S_IFBLK}, 9727 {"c", 2, S_IFBLK}, 9728 {"d", 3, S_IFBLK}, 9729 {"e", 4, S_IFBLK}, 9730 {"f", 5, S_IFBLK}, 9731 {"g", 6, S_IFBLK}, 9732 {"wd", 7, S_IFBLK}, 9733 #if defined(_FIRMWARE_NEEDS_FDISK) 9734 {"q", 16, S_IFBLK}, 9735 {"r", 17, S_IFBLK}, 9736 {"s", 18, S_IFBLK}, 9737 {"t", 19, S_IFBLK}, 9738 {"u", 20, S_IFBLK}, 9739 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9740 {"a,raw", 0, S_IFCHR}, 9741 {"b,raw", 1, S_IFCHR}, 9742 {"c,raw", 2, S_IFCHR}, 9743 {"d,raw", 3, S_IFCHR}, 9744 {"e,raw", 4, S_IFCHR}, 9745 {"f,raw", 5, S_IFCHR}, 9746 {"g,raw", 6, S_IFCHR}, 9747 {"wd,raw", 7, S_IFCHR}, 9748 #if defined(_FIRMWARE_NEEDS_FDISK) 9749 {"q,raw", 16, S_IFCHR}, 9750 {"r,raw", 17, S_IFCHR}, 9751 {"s,raw", 18, S_IFCHR}, 9752 {"t,raw", 19, S_IFCHR}, 9753 {"u,raw", 20, S_IFCHR}, 9754 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9755 {0} 9756 }; 9757 9758 9759 /* 9760 * Function: sd_create_minor_nodes 9761 * 9762 * Description: Create the minor device nodes for the instance. 9763 * 9764 * Arguments: un - driver soft state (unit) structure 9765 * devi - pointer to device info structure 9766 * 9767 * Return Code: DDI_SUCCESS 9768 * DDI_FAILURE 9769 * 9770 * Context: Kernel thread context 9771 */ 9772 9773 static int 9774 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9775 { 9776 struct driver_minor_data *dmdp; 9777 struct scsi_device *devp; 9778 int instance; 9779 char name[48]; 9780 9781 ASSERT(un != NULL); 9782 devp = ddi_get_driver_private(devi); 9783 instance = ddi_get_instance(devp->sd_dev); 9784 9785 /* 9786 * Create all the minor nodes for this target. 9787 */ 9788 if (un->un_blockcount > DK_MAX_BLOCKS) 9789 dmdp = sd_minor_data_efi; 9790 else 9791 dmdp = sd_minor_data; 9792 while (dmdp->name != NULL) { 9793 9794 (void) sprintf(name, "%s", dmdp->name); 9795 9796 if (ddi_create_minor_node(devi, name, dmdp->type, 9797 (instance << SDUNIT_SHIFT) | dmdp->minor, 9798 un->un_node_type, NULL) == DDI_FAILURE) { 9799 /* 9800 * Clean up any nodes that may have been created, in 9801 * case this fails in the middle of the loop. 9802 */ 9803 ddi_remove_minor_node(devi, NULL); 9804 return (DDI_FAILURE); 9805 } 9806 dmdp++; 9807 } 9808 9809 return (DDI_SUCCESS); 9810 } 9811 9812 9813 /* 9814 * Function: sd_create_errstats 9815 * 9816 * Description: This routine instantiates the device error stats. 9817 * 9818 * Note: During attach the stats are instantiated first so they are 9819 * available for attach-time routines that utilize the driver 9820 * iopath to send commands to the device. The stats are initialized 9821 * separately so data obtained during some attach-time routines is 9822 * available. (4362483) 9823 * 9824 * Arguments: un - driver soft state (unit) structure 9825 * instance - driver instance 9826 * 9827 * Context: Kernel thread context 9828 */ 9829 9830 static void 9831 sd_create_errstats(struct sd_lun *un, int instance) 9832 { 9833 struct sd_errstats *stp; 9834 char kstatmodule_err[KSTAT_STRLEN]; 9835 char kstatname[KSTAT_STRLEN]; 9836 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9837 9838 ASSERT(un != NULL); 9839 9840 if (un->un_errstats != NULL) { 9841 return; 9842 } 9843 9844 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9845 "%serr", sd_label); 9846 (void) snprintf(kstatname, sizeof (kstatname), 9847 "%s%d,err", sd_label, instance); 9848 9849 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9850 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9851 9852 if (un->un_errstats == NULL) { 9853 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9854 "sd_create_errstats: Failed kstat_create\n"); 9855 return; 9856 } 9857 9858 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9859 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9860 KSTAT_DATA_UINT32); 9861 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9862 KSTAT_DATA_UINT32); 9863 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9864 KSTAT_DATA_UINT32); 9865 kstat_named_init(&stp->sd_vid, "Vendor", 9866 KSTAT_DATA_CHAR); 9867 kstat_named_init(&stp->sd_pid, "Product", 9868 KSTAT_DATA_CHAR); 9869 kstat_named_init(&stp->sd_revision, "Revision", 9870 KSTAT_DATA_CHAR); 9871 kstat_named_init(&stp->sd_serial, "Serial No", 9872 KSTAT_DATA_CHAR); 9873 kstat_named_init(&stp->sd_capacity, "Size", 9874 KSTAT_DATA_ULONGLONG); 9875 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9876 KSTAT_DATA_UINT32); 9877 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9878 KSTAT_DATA_UINT32); 9879 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9880 KSTAT_DATA_UINT32); 9881 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9882 KSTAT_DATA_UINT32); 9883 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9884 KSTAT_DATA_UINT32); 9885 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9886 KSTAT_DATA_UINT32); 9887 9888 un->un_errstats->ks_private = un; 9889 un->un_errstats->ks_update = nulldev; 9890 9891 kstat_install(un->un_errstats); 9892 } 9893 9894 9895 /* 9896 * Function: sd_set_errstats 9897 * 9898 * Description: This routine sets the value of the vendor id, product id, 9899 * revision, serial number, and capacity device error stats. 9900 * 9901 * Note: During attach the stats are instantiated first so they are 9902 * available for attach-time routines that utilize the driver 9903 * iopath to send commands to the device. The stats are initialized 9904 * separately so data obtained during some attach-time routines is 9905 * available. (4362483) 9906 * 9907 * Arguments: un - driver soft state (unit) structure 9908 * 9909 * Context: Kernel thread context 9910 */ 9911 9912 static void 9913 sd_set_errstats(struct sd_lun *un) 9914 { 9915 struct sd_errstats *stp; 9916 9917 ASSERT(un != NULL); 9918 ASSERT(un->un_errstats != NULL); 9919 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9920 ASSERT(stp != NULL); 9921 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9922 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9923 (void) strncpy(stp->sd_revision.value.c, 9924 un->un_sd->sd_inq->inq_revision, 4); 9925 9926 /* 9927 * All the errstats are persistent across detach/attach, 9928 * so reset all the errstats here in case of the hot 9929 * replacement of disk drives, except for not changed 9930 * Sun qualified drives. 9931 */ 9932 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9933 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9934 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9935 stp->sd_softerrs.value.ui32 = 0; 9936 stp->sd_harderrs.value.ui32 = 0; 9937 stp->sd_transerrs.value.ui32 = 0; 9938 stp->sd_rq_media_err.value.ui32 = 0; 9939 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9940 stp->sd_rq_nodev_err.value.ui32 = 0; 9941 stp->sd_rq_recov_err.value.ui32 = 0; 9942 stp->sd_rq_illrq_err.value.ui32 = 0; 9943 stp->sd_rq_pfa_err.value.ui32 = 0; 9944 } 9945 9946 /* 9947 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9948 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9949 * (4376302)) 9950 */ 9951 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9952 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9953 sizeof (SD_INQUIRY(un)->inq_serial)); 9954 } 9955 9956 if (un->un_f_blockcount_is_valid != TRUE) { 9957 /* 9958 * Set capacity error stat to 0 for no media. This ensures 9959 * a valid capacity is displayed in response to 'iostat -E' 9960 * when no media is present in the device. 9961 */ 9962 stp->sd_capacity.value.ui64 = 0; 9963 } else { 9964 /* 9965 * Multiply un_blockcount by un->un_sys_blocksize to get 9966 * capacity. 9967 * 9968 * Note: for non-512 blocksize devices "un_blockcount" has been 9969 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9970 * (un_tgt_blocksize / un->un_sys_blocksize). 9971 */ 9972 stp->sd_capacity.value.ui64 = (uint64_t) 9973 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9974 } 9975 } 9976 9977 9978 /* 9979 * Function: sd_set_pstats 9980 * 9981 * Description: This routine instantiates and initializes the partition 9982 * stats for each partition with more than zero blocks. 9983 * (4363169) 9984 * 9985 * Arguments: un - driver soft state (unit) structure 9986 * 9987 * Context: Kernel thread context 9988 */ 9989 9990 static void 9991 sd_set_pstats(struct sd_lun *un) 9992 { 9993 char kstatname[KSTAT_STRLEN]; 9994 int instance; 9995 int i; 9996 9997 ASSERT(un != NULL); 9998 9999 instance = ddi_get_instance(SD_DEVINFO(un)); 10000 10001 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 10002 for (i = 0; i < NSDMAP; i++) { 10003 if ((un->un_pstats[i] == NULL) && 10004 (un->un_map[i].dkl_nblk != 0)) { 10005 (void) snprintf(kstatname, sizeof (kstatname), 10006 "%s%d,%s", sd_label, instance, 10007 sd_minor_data[i].name); 10008 un->un_pstats[i] = kstat_create(sd_label, 10009 instance, kstatname, "partition", KSTAT_TYPE_IO, 10010 1, KSTAT_FLAG_PERSISTENT); 10011 if (un->un_pstats[i] != NULL) { 10012 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 10013 kstat_install(un->un_pstats[i]); 10014 } 10015 } 10016 } 10017 } 10018 10019 10020 #if (defined(__fibre)) 10021 /* 10022 * Function: sd_init_event_callbacks 10023 * 10024 * Description: This routine initializes the insertion and removal event 10025 * callbacks. (fibre only) 10026 * 10027 * Arguments: un - driver soft state (unit) structure 10028 * 10029 * Context: Kernel thread context 10030 */ 10031 10032 static void 10033 sd_init_event_callbacks(struct sd_lun *un) 10034 { 10035 ASSERT(un != NULL); 10036 10037 if ((un->un_insert_event == NULL) && 10038 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 10039 &un->un_insert_event) == DDI_SUCCESS)) { 10040 /* 10041 * Add the callback for an insertion event 10042 */ 10043 (void) ddi_add_event_handler(SD_DEVINFO(un), 10044 un->un_insert_event, sd_event_callback, (void *)un, 10045 &(un->un_insert_cb_id)); 10046 } 10047 10048 if ((un->un_remove_event == NULL) && 10049 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 10050 &un->un_remove_event) == DDI_SUCCESS)) { 10051 /* 10052 * Add the callback for a removal event 10053 */ 10054 (void) ddi_add_event_handler(SD_DEVINFO(un), 10055 un->un_remove_event, sd_event_callback, (void *)un, 10056 &(un->un_remove_cb_id)); 10057 } 10058 } 10059 10060 10061 /* 10062 * Function: sd_event_callback 10063 * 10064 * Description: This routine handles insert/remove events (photon). The 10065 * state is changed to OFFLINE which can be used to supress 10066 * error msgs. (fibre only) 10067 * 10068 * Arguments: un - driver soft state (unit) structure 10069 * 10070 * Context: Callout thread context 10071 */ 10072 /* ARGSUSED */ 10073 static void 10074 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 10075 void *bus_impldata) 10076 { 10077 struct sd_lun *un = (struct sd_lun *)arg; 10078 10079 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 10080 if (event == un->un_insert_event) { 10081 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 10082 mutex_enter(SD_MUTEX(un)); 10083 if (un->un_state == SD_STATE_OFFLINE) { 10084 if (un->un_last_state != SD_STATE_SUSPENDED) { 10085 un->un_state = un->un_last_state; 10086 } else { 10087 /* 10088 * We have gone through SUSPEND/RESUME while 10089 * we were offline. Restore the last state 10090 */ 10091 un->un_state = un->un_save_state; 10092 } 10093 } 10094 mutex_exit(SD_MUTEX(un)); 10095 10096 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 10097 } else if (event == un->un_remove_event) { 10098 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 10099 mutex_enter(SD_MUTEX(un)); 10100 /* 10101 * We need to handle an event callback that occurs during 10102 * the suspend operation, since we don't prevent it. 10103 */ 10104 if (un->un_state != SD_STATE_OFFLINE) { 10105 if (un->un_state != SD_STATE_SUSPENDED) { 10106 New_state(un, SD_STATE_OFFLINE); 10107 } else { 10108 un->un_last_state = SD_STATE_OFFLINE; 10109 } 10110 } 10111 mutex_exit(SD_MUTEX(un)); 10112 } else { 10113 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 10114 "!Unknown event\n"); 10115 } 10116 10117 } 10118 #endif 10119 10120 /* 10121 * Function: sd_cache_control() 10122 * 10123 * Description: This routine is the driver entry point for setting 10124 * read and write caching by modifying the WCE (write cache 10125 * enable) and RCD (read cache disable) bits of mode 10126 * page 8 (MODEPAGE_CACHING). 10127 * 10128 * Arguments: un - driver soft state (unit) structure 10129 * rcd_flag - flag for controlling the read cache 10130 * wce_flag - flag for controlling the write cache 10131 * 10132 * Return Code: EIO 10133 * code returned by sd_send_scsi_MODE_SENSE and 10134 * sd_send_scsi_MODE_SELECT 10135 * 10136 * Context: Kernel Thread 10137 */ 10138 10139 static int 10140 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 10141 { 10142 struct mode_caching *mode_caching_page; 10143 uchar_t *header; 10144 size_t buflen; 10145 int hdrlen; 10146 int bd_len; 10147 int rval = 0; 10148 struct mode_header_grp2 *mhp; 10149 10150 ASSERT(un != NULL); 10151 10152 /* 10153 * Do a test unit ready, otherwise a mode sense may not work if this 10154 * is the first command sent to the device after boot. 10155 */ 10156 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10157 10158 if (un->un_f_cfg_is_atapi == TRUE) { 10159 hdrlen = MODE_HEADER_LENGTH_GRP2; 10160 } else { 10161 hdrlen = MODE_HEADER_LENGTH; 10162 } 10163 10164 /* 10165 * Allocate memory for the retrieved mode page and its headers. Set 10166 * a pointer to the page itself. Use mode_cache_scsi3 to insure 10167 * we get all of the mode sense data otherwise, the mode select 10168 * will fail. mode_cache_scsi3 is a superset of mode_caching. 10169 */ 10170 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 10171 sizeof (struct mode_cache_scsi3); 10172 10173 header = kmem_zalloc(buflen, KM_SLEEP); 10174 10175 /* Get the information from the device. */ 10176 if (un->un_f_cfg_is_atapi == TRUE) { 10177 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10178 MODEPAGE_CACHING, SD_PATH_DIRECT); 10179 } else { 10180 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10181 MODEPAGE_CACHING, SD_PATH_DIRECT); 10182 } 10183 if (rval != 0) { 10184 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10185 "sd_cache_control: Mode Sense Failed\n"); 10186 kmem_free(header, buflen); 10187 return (rval); 10188 } 10189 10190 /* 10191 * Determine size of Block Descriptors in order to locate 10192 * the mode page data. ATAPI devices return 0, SCSI devices 10193 * should return MODE_BLK_DESC_LENGTH. 10194 */ 10195 if (un->un_f_cfg_is_atapi == TRUE) { 10196 mhp = (struct mode_header_grp2 *)header; 10197 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10198 } else { 10199 bd_len = ((struct mode_header *)header)->bdesc_length; 10200 } 10201 10202 if (bd_len > MODE_BLK_DESC_LENGTH) { 10203 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10204 "sd_cache_control: Mode Sense returned invalid " 10205 "block descriptor length\n"); 10206 kmem_free(header, buflen); 10207 return (EIO); 10208 } 10209 10210 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10211 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10212 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10213 " caching page code mismatch %d\n", 10214 mode_caching_page->mode_page.code); 10215 kmem_free(header, buflen); 10216 return (EIO); 10217 } 10218 10219 /* Check the relevant bits on successful mode sense. */ 10220 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 10221 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 10222 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 10223 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 10224 10225 size_t sbuflen; 10226 uchar_t save_pg; 10227 10228 /* 10229 * Construct select buffer length based on the 10230 * length of the sense data returned. 10231 */ 10232 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 10233 sizeof (struct mode_page) + 10234 (int)mode_caching_page->mode_page.length; 10235 10236 /* 10237 * Set the caching bits as requested. 10238 */ 10239 if (rcd_flag == SD_CACHE_ENABLE) 10240 mode_caching_page->rcd = 0; 10241 else if (rcd_flag == SD_CACHE_DISABLE) 10242 mode_caching_page->rcd = 1; 10243 10244 if (wce_flag == SD_CACHE_ENABLE) 10245 mode_caching_page->wce = 1; 10246 else if (wce_flag == SD_CACHE_DISABLE) 10247 mode_caching_page->wce = 0; 10248 10249 /* 10250 * Save the page if the mode sense says the 10251 * drive supports it. 10252 */ 10253 save_pg = mode_caching_page->mode_page.ps ? 10254 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 10255 10256 /* Clear reserved bits before mode select. */ 10257 mode_caching_page->mode_page.ps = 0; 10258 10259 /* 10260 * Clear out mode header for mode select. 10261 * The rest of the retrieved page will be reused. 10262 */ 10263 bzero(header, hdrlen); 10264 10265 if (un->un_f_cfg_is_atapi == TRUE) { 10266 mhp = (struct mode_header_grp2 *)header; 10267 mhp->bdesc_length_hi = bd_len >> 8; 10268 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 10269 } else { 10270 ((struct mode_header *)header)->bdesc_length = bd_len; 10271 } 10272 10273 /* Issue mode select to change the cache settings */ 10274 if (un->un_f_cfg_is_atapi == TRUE) { 10275 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 10276 sbuflen, save_pg, SD_PATH_DIRECT); 10277 } else { 10278 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 10279 sbuflen, save_pg, SD_PATH_DIRECT); 10280 } 10281 } 10282 10283 kmem_free(header, buflen); 10284 return (rval); 10285 } 10286 10287 10288 /* 10289 * Function: sd_get_write_cache_enabled() 10290 * 10291 * Description: This routine is the driver entry point for determining if 10292 * write caching is enabled. It examines the WCE (write cache 10293 * enable) bits of mode page 8 (MODEPAGE_CACHING). 10294 * 10295 * Arguments: un - driver soft state (unit) structure 10296 * is_enabled - pointer to int where write cache enabled state 10297 * is returned (non-zero -> write cache enabled) 10298 * 10299 * 10300 * Return Code: EIO 10301 * code returned by sd_send_scsi_MODE_SENSE 10302 * 10303 * Context: Kernel Thread 10304 * 10305 * NOTE: If ioctl is added to disable write cache, this sequence should 10306 * be followed so that no locking is required for accesses to 10307 * un->un_f_write_cache_enabled: 10308 * do mode select to clear wce 10309 * do synchronize cache to flush cache 10310 * set un->un_f_write_cache_enabled = FALSE 10311 * 10312 * Conversely, an ioctl to enable the write cache should be done 10313 * in this order: 10314 * set un->un_f_write_cache_enabled = TRUE 10315 * do mode select to set wce 10316 */ 10317 10318 static int 10319 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 10320 { 10321 struct mode_caching *mode_caching_page; 10322 uchar_t *header; 10323 size_t buflen; 10324 int hdrlen; 10325 int bd_len; 10326 int rval = 0; 10327 10328 ASSERT(un != NULL); 10329 ASSERT(is_enabled != NULL); 10330 10331 /* in case of error, flag as enabled */ 10332 *is_enabled = TRUE; 10333 10334 /* 10335 * Do a test unit ready, otherwise a mode sense may not work if this 10336 * is the first command sent to the device after boot. 10337 */ 10338 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10339 10340 if (un->un_f_cfg_is_atapi == TRUE) { 10341 hdrlen = MODE_HEADER_LENGTH_GRP2; 10342 } else { 10343 hdrlen = MODE_HEADER_LENGTH; 10344 } 10345 10346 /* 10347 * Allocate memory for the retrieved mode page and its headers. Set 10348 * a pointer to the page itself. 10349 */ 10350 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 10351 header = kmem_zalloc(buflen, KM_SLEEP); 10352 10353 /* Get the information from the device. */ 10354 if (un->un_f_cfg_is_atapi == TRUE) { 10355 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10356 MODEPAGE_CACHING, SD_PATH_DIRECT); 10357 } else { 10358 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10359 MODEPAGE_CACHING, SD_PATH_DIRECT); 10360 } 10361 if (rval != 0) { 10362 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10363 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 10364 kmem_free(header, buflen); 10365 return (rval); 10366 } 10367 10368 /* 10369 * Determine size of Block Descriptors in order to locate 10370 * the mode page data. ATAPI devices return 0, SCSI devices 10371 * should return MODE_BLK_DESC_LENGTH. 10372 */ 10373 if (un->un_f_cfg_is_atapi == TRUE) { 10374 struct mode_header_grp2 *mhp; 10375 mhp = (struct mode_header_grp2 *)header; 10376 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10377 } else { 10378 bd_len = ((struct mode_header *)header)->bdesc_length; 10379 } 10380 10381 if (bd_len > MODE_BLK_DESC_LENGTH) { 10382 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10383 "sd_get_write_cache_enabled: Mode Sense returned invalid " 10384 "block descriptor length\n"); 10385 kmem_free(header, buflen); 10386 return (EIO); 10387 } 10388 10389 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10390 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10391 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10392 " caching page code mismatch %d\n", 10393 mode_caching_page->mode_page.code); 10394 kmem_free(header, buflen); 10395 return (EIO); 10396 } 10397 *is_enabled = mode_caching_page->wce; 10398 10399 kmem_free(header, buflen); 10400 return (0); 10401 } 10402 10403 10404 /* 10405 * Function: sd_make_device 10406 * 10407 * Description: Utility routine to return the Solaris device number from 10408 * the data in the device's dev_info structure. 10409 * 10410 * Return Code: The Solaris device number 10411 * 10412 * Context: Any 10413 */ 10414 10415 static dev_t 10416 sd_make_device(dev_info_t *devi) 10417 { 10418 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 10419 ddi_get_instance(devi) << SDUNIT_SHIFT)); 10420 } 10421 10422 10423 /* 10424 * Function: sd_pm_entry 10425 * 10426 * Description: Called at the start of a new command to manage power 10427 * and busy status of a device. This includes determining whether 10428 * the current power state of the device is sufficient for 10429 * performing the command or whether it must be changed. 10430 * The PM framework is notified appropriately. 10431 * Only with a return status of DDI_SUCCESS will the 10432 * component be busy to the framework. 10433 * 10434 * All callers of sd_pm_entry must check the return status 10435 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 10436 * of DDI_FAILURE indicates the device failed to power up. 10437 * In this case un_pm_count has been adjusted so the result 10438 * on exit is still powered down, ie. count is less than 0. 10439 * Calling sd_pm_exit with this count value hits an ASSERT. 10440 * 10441 * Return Code: DDI_SUCCESS or DDI_FAILURE 10442 * 10443 * Context: Kernel thread context. 10444 */ 10445 10446 static int 10447 sd_pm_entry(struct sd_lun *un) 10448 { 10449 int return_status = DDI_SUCCESS; 10450 10451 ASSERT(!mutex_owned(SD_MUTEX(un))); 10452 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10453 10454 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 10455 10456 if (un->un_f_pm_is_enabled == FALSE) { 10457 SD_TRACE(SD_LOG_IO_PM, un, 10458 "sd_pm_entry: exiting, PM not enabled\n"); 10459 return (return_status); 10460 } 10461 10462 /* 10463 * Just increment a counter if PM is enabled. On the transition from 10464 * 0 ==> 1, mark the device as busy. The iodone side will decrement 10465 * the count with each IO and mark the device as idle when the count 10466 * hits 0. 10467 * 10468 * If the count is less than 0 the device is powered down. If a powered 10469 * down device is successfully powered up then the count must be 10470 * incremented to reflect the power up. Note that it'll get incremented 10471 * a second time to become busy. 10472 * 10473 * Because the following has the potential to change the device state 10474 * and must release the un_pm_mutex to do so, only one thread can be 10475 * allowed through at a time. 10476 */ 10477 10478 mutex_enter(&un->un_pm_mutex); 10479 while (un->un_pm_busy == TRUE) { 10480 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 10481 } 10482 un->un_pm_busy = TRUE; 10483 10484 if (un->un_pm_count < 1) { 10485 10486 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 10487 10488 /* 10489 * Indicate we are now busy so the framework won't attempt to 10490 * power down the device. This call will only fail if either 10491 * we passed a bad component number or the device has no 10492 * components. Neither of these should ever happen. 10493 */ 10494 mutex_exit(&un->un_pm_mutex); 10495 return_status = pm_busy_component(SD_DEVINFO(un), 0); 10496 ASSERT(return_status == DDI_SUCCESS); 10497 10498 mutex_enter(&un->un_pm_mutex); 10499 10500 if (un->un_pm_count < 0) { 10501 mutex_exit(&un->un_pm_mutex); 10502 10503 SD_TRACE(SD_LOG_IO_PM, un, 10504 "sd_pm_entry: power up component\n"); 10505 10506 /* 10507 * pm_raise_power will cause sdpower to be called 10508 * which brings the device power level to the 10509 * desired state, ON in this case. If successful, 10510 * un_pm_count and un_power_level will be updated 10511 * appropriately. 10512 */ 10513 return_status = pm_raise_power(SD_DEVINFO(un), 0, 10514 SD_SPINDLE_ON); 10515 10516 mutex_enter(&un->un_pm_mutex); 10517 10518 if (return_status != DDI_SUCCESS) { 10519 /* 10520 * Power up failed. 10521 * Idle the device and adjust the count 10522 * so the result on exit is that we're 10523 * still powered down, ie. count is less than 0. 10524 */ 10525 SD_TRACE(SD_LOG_IO_PM, un, 10526 "sd_pm_entry: power up failed," 10527 " idle the component\n"); 10528 10529 (void) pm_idle_component(SD_DEVINFO(un), 0); 10530 un->un_pm_count--; 10531 } else { 10532 /* 10533 * Device is powered up, verify the 10534 * count is non-negative. 10535 * This is debug only. 10536 */ 10537 ASSERT(un->un_pm_count == 0); 10538 } 10539 } 10540 10541 if (return_status == DDI_SUCCESS) { 10542 /* 10543 * For performance, now that the device has been tagged 10544 * as busy, and it's known to be powered up, update the 10545 * chain types to use jump tables that do not include 10546 * pm. This significantly lowers the overhead and 10547 * therefore improves performance. 10548 */ 10549 10550 mutex_exit(&un->un_pm_mutex); 10551 mutex_enter(SD_MUTEX(un)); 10552 SD_TRACE(SD_LOG_IO_PM, un, 10553 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10554 un->un_uscsi_chain_type); 10555 10556 if (un->un_f_non_devbsize_supported) { 10557 un->un_buf_chain_type = 10558 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10559 } else { 10560 un->un_buf_chain_type = 10561 SD_CHAIN_INFO_DISK_NO_PM; 10562 } 10563 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10564 10565 SD_TRACE(SD_LOG_IO_PM, un, 10566 " changed uscsi_chain_type to %d\n", 10567 un->un_uscsi_chain_type); 10568 mutex_exit(SD_MUTEX(un)); 10569 mutex_enter(&un->un_pm_mutex); 10570 10571 if (un->un_pm_idle_timeid == NULL) { 10572 /* 300 ms. */ 10573 un->un_pm_idle_timeid = 10574 timeout(sd_pm_idletimeout_handler, un, 10575 (drv_usectohz((clock_t)300000))); 10576 /* 10577 * Include an extra call to busy which keeps the 10578 * device busy with-respect-to the PM layer 10579 * until the timer fires, at which time it'll 10580 * get the extra idle call. 10581 */ 10582 (void) pm_busy_component(SD_DEVINFO(un), 0); 10583 } 10584 } 10585 } 10586 un->un_pm_busy = FALSE; 10587 /* Next... */ 10588 cv_signal(&un->un_pm_busy_cv); 10589 10590 un->un_pm_count++; 10591 10592 SD_TRACE(SD_LOG_IO_PM, un, 10593 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10594 10595 mutex_exit(&un->un_pm_mutex); 10596 10597 return (return_status); 10598 } 10599 10600 10601 /* 10602 * Function: sd_pm_exit 10603 * 10604 * Description: Called at the completion of a command to manage busy 10605 * status for the device. If the device becomes idle the 10606 * PM framework is notified. 10607 * 10608 * Context: Kernel thread context 10609 */ 10610 10611 static void 10612 sd_pm_exit(struct sd_lun *un) 10613 { 10614 ASSERT(!mutex_owned(SD_MUTEX(un))); 10615 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10616 10617 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10618 10619 /* 10620 * After attach the following flag is only read, so don't 10621 * take the penalty of acquiring a mutex for it. 10622 */ 10623 if (un->un_f_pm_is_enabled == TRUE) { 10624 10625 mutex_enter(&un->un_pm_mutex); 10626 un->un_pm_count--; 10627 10628 SD_TRACE(SD_LOG_IO_PM, un, 10629 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10630 10631 ASSERT(un->un_pm_count >= 0); 10632 if (un->un_pm_count == 0) { 10633 mutex_exit(&un->un_pm_mutex); 10634 10635 SD_TRACE(SD_LOG_IO_PM, un, 10636 "sd_pm_exit: idle component\n"); 10637 10638 (void) pm_idle_component(SD_DEVINFO(un), 0); 10639 10640 } else { 10641 mutex_exit(&un->un_pm_mutex); 10642 } 10643 } 10644 10645 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10646 } 10647 10648 10649 /* 10650 * Function: sdopen 10651 * 10652 * Description: Driver's open(9e) entry point function. 10653 * 10654 * Arguments: dev_i - pointer to device number 10655 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10656 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10657 * cred_p - user credential pointer 10658 * 10659 * Return Code: EINVAL 10660 * ENXIO 10661 * EIO 10662 * EROFS 10663 * EBUSY 10664 * 10665 * Context: Kernel thread context 10666 */ 10667 /* ARGSUSED */ 10668 static int 10669 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10670 { 10671 struct sd_lun *un; 10672 int nodelay; 10673 int part; 10674 uint64_t partmask; 10675 int instance; 10676 dev_t dev; 10677 int rval = EIO; 10678 10679 /* Validate the open type */ 10680 if (otyp >= OTYPCNT) { 10681 return (EINVAL); 10682 } 10683 10684 dev = *dev_p; 10685 instance = SDUNIT(dev); 10686 mutex_enter(&sd_detach_mutex); 10687 10688 /* 10689 * Fail the open if there is no softstate for the instance, or 10690 * if another thread somewhere is trying to detach the instance. 10691 */ 10692 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10693 (un->un_detach_count != 0)) { 10694 mutex_exit(&sd_detach_mutex); 10695 /* 10696 * The probe cache only needs to be cleared when open (9e) fails 10697 * with ENXIO (4238046). 10698 */ 10699 /* 10700 * un-conditionally clearing probe cache is ok with 10701 * separate sd/ssd binaries 10702 * x86 platform can be an issue with both parallel 10703 * and fibre in 1 binary 10704 */ 10705 sd_scsi_clear_probe_cache(); 10706 return (ENXIO); 10707 } 10708 10709 /* 10710 * The un_layer_count is to prevent another thread in specfs from 10711 * trying to detach the instance, which can happen when we are 10712 * called from a higher-layer driver instead of thru specfs. 10713 * This will not be needed when DDI provides a layered driver 10714 * interface that allows specfs to know that an instance is in 10715 * use by a layered driver & should not be detached. 10716 * 10717 * Note: the semantics for layered driver opens are exactly one 10718 * close for every open. 10719 */ 10720 if (otyp == OTYP_LYR) { 10721 un->un_layer_count++; 10722 } 10723 10724 /* 10725 * Keep a count of the current # of opens in progress. This is because 10726 * some layered drivers try to call us as a regular open. This can 10727 * cause problems that we cannot prevent, however by keeping this count 10728 * we can at least keep our open and detach routines from racing against 10729 * each other under such conditions. 10730 */ 10731 un->un_opens_in_progress++; 10732 mutex_exit(&sd_detach_mutex); 10733 10734 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10735 part = SDPART(dev); 10736 partmask = 1 << part; 10737 10738 /* 10739 * We use a semaphore here in order to serialize 10740 * open and close requests on the device. 10741 */ 10742 sema_p(&un->un_semoclose); 10743 10744 mutex_enter(SD_MUTEX(un)); 10745 10746 /* 10747 * All device accesses go thru sdstrategy() where we check 10748 * on suspend status but there could be a scsi_poll command, 10749 * which bypasses sdstrategy(), so we need to check pm 10750 * status. 10751 */ 10752 10753 if (!nodelay) { 10754 while ((un->un_state == SD_STATE_SUSPENDED) || 10755 (un->un_state == SD_STATE_PM_CHANGING)) { 10756 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10757 } 10758 10759 mutex_exit(SD_MUTEX(un)); 10760 if (sd_pm_entry(un) != DDI_SUCCESS) { 10761 rval = EIO; 10762 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10763 "sdopen: sd_pm_entry failed\n"); 10764 goto open_failed_with_pm; 10765 } 10766 mutex_enter(SD_MUTEX(un)); 10767 } 10768 10769 /* check for previous exclusive open */ 10770 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10771 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10772 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10773 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10774 10775 if (un->un_exclopen & (partmask)) { 10776 goto excl_open_fail; 10777 } 10778 10779 if (flag & FEXCL) { 10780 int i; 10781 if (un->un_ocmap.lyropen[part]) { 10782 goto excl_open_fail; 10783 } 10784 for (i = 0; i < (OTYPCNT - 1); i++) { 10785 if (un->un_ocmap.regopen[i] & (partmask)) { 10786 goto excl_open_fail; 10787 } 10788 } 10789 } 10790 10791 /* 10792 * Check the write permission if this is a removable media device, 10793 * NDELAY has not been set, and writable permission is requested. 10794 * 10795 * Note: If NDELAY was set and this is write-protected media the WRITE 10796 * attempt will fail with EIO as part of the I/O processing. This is a 10797 * more permissive implementation that allows the open to succeed and 10798 * WRITE attempts to fail when appropriate. 10799 */ 10800 if (un->un_f_chk_wp_open) { 10801 if ((flag & FWRITE) && (!nodelay)) { 10802 mutex_exit(SD_MUTEX(un)); 10803 /* 10804 * Defer the check for write permission on writable 10805 * DVD drive till sdstrategy and will not fail open even 10806 * if FWRITE is set as the device can be writable 10807 * depending upon the media and the media can change 10808 * after the call to open(). 10809 */ 10810 if (un->un_f_dvdram_writable_device == FALSE) { 10811 if (ISCD(un) || sr_check_wp(dev)) { 10812 rval = EROFS; 10813 mutex_enter(SD_MUTEX(un)); 10814 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10815 "write to cd or write protected media\n"); 10816 goto open_fail; 10817 } 10818 } 10819 mutex_enter(SD_MUTEX(un)); 10820 } 10821 } 10822 10823 /* 10824 * If opening in NDELAY/NONBLOCK mode, just return. 10825 * Check if disk is ready and has a valid geometry later. 10826 */ 10827 if (!nodelay) { 10828 mutex_exit(SD_MUTEX(un)); 10829 rval = sd_ready_and_valid(un); 10830 mutex_enter(SD_MUTEX(un)); 10831 /* 10832 * Fail if device is not ready or if the number of disk 10833 * blocks is zero or negative for non CD devices. 10834 */ 10835 if ((rval != SD_READY_VALID) || 10836 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10837 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10838 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10839 "device not ready or invalid disk block value\n"); 10840 goto open_fail; 10841 } 10842 #if defined(__i386) || defined(__amd64) 10843 } else { 10844 uchar_t *cp; 10845 /* 10846 * x86 requires special nodelay handling, so that p0 is 10847 * always defined and accessible. 10848 * Invalidate geometry only if device is not already open. 10849 */ 10850 cp = &un->un_ocmap.chkd[0]; 10851 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10852 if (*cp != (uchar_t)0) { 10853 break; 10854 } 10855 cp++; 10856 } 10857 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10858 un->un_f_geometry_is_valid = FALSE; 10859 } 10860 10861 #endif 10862 } 10863 10864 if (otyp == OTYP_LYR) { 10865 un->un_ocmap.lyropen[part]++; 10866 } else { 10867 un->un_ocmap.regopen[otyp] |= partmask; 10868 } 10869 10870 /* Set up open and exclusive open flags */ 10871 if (flag & FEXCL) { 10872 un->un_exclopen |= (partmask); 10873 } 10874 10875 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10876 "open of part %d type %d\n", part, otyp); 10877 10878 mutex_exit(SD_MUTEX(un)); 10879 if (!nodelay) { 10880 sd_pm_exit(un); 10881 } 10882 10883 sema_v(&un->un_semoclose); 10884 10885 mutex_enter(&sd_detach_mutex); 10886 un->un_opens_in_progress--; 10887 mutex_exit(&sd_detach_mutex); 10888 10889 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10890 return (DDI_SUCCESS); 10891 10892 excl_open_fail: 10893 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10894 rval = EBUSY; 10895 10896 open_fail: 10897 mutex_exit(SD_MUTEX(un)); 10898 10899 /* 10900 * On a failed open we must exit the pm management. 10901 */ 10902 if (!nodelay) { 10903 sd_pm_exit(un); 10904 } 10905 open_failed_with_pm: 10906 sema_v(&un->un_semoclose); 10907 10908 mutex_enter(&sd_detach_mutex); 10909 un->un_opens_in_progress--; 10910 if (otyp == OTYP_LYR) { 10911 un->un_layer_count--; 10912 } 10913 mutex_exit(&sd_detach_mutex); 10914 10915 return (rval); 10916 } 10917 10918 10919 /* 10920 * Function: sdclose 10921 * 10922 * Description: Driver's close(9e) entry point function. 10923 * 10924 * Arguments: dev - device number 10925 * flag - file status flag, informational only 10926 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10927 * cred_p - user credential pointer 10928 * 10929 * Return Code: ENXIO 10930 * 10931 * Context: Kernel thread context 10932 */ 10933 /* ARGSUSED */ 10934 static int 10935 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10936 { 10937 struct sd_lun *un; 10938 uchar_t *cp; 10939 int part; 10940 int nodelay; 10941 int rval = 0; 10942 10943 /* Validate the open type */ 10944 if (otyp >= OTYPCNT) { 10945 return (ENXIO); 10946 } 10947 10948 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10949 return (ENXIO); 10950 } 10951 10952 part = SDPART(dev); 10953 nodelay = flag & (FNDELAY | FNONBLOCK); 10954 10955 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10956 "sdclose: close of part %d type %d\n", part, otyp); 10957 10958 /* 10959 * We use a semaphore here in order to serialize 10960 * open and close requests on the device. 10961 */ 10962 sema_p(&un->un_semoclose); 10963 10964 mutex_enter(SD_MUTEX(un)); 10965 10966 /* Don't proceed if power is being changed. */ 10967 while (un->un_state == SD_STATE_PM_CHANGING) { 10968 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10969 } 10970 10971 if (un->un_exclopen & (1 << part)) { 10972 un->un_exclopen &= ~(1 << part); 10973 } 10974 10975 /* Update the open partition map */ 10976 if (otyp == OTYP_LYR) { 10977 un->un_ocmap.lyropen[part] -= 1; 10978 } else { 10979 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10980 } 10981 10982 cp = &un->un_ocmap.chkd[0]; 10983 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10984 if (*cp != NULL) { 10985 break; 10986 } 10987 cp++; 10988 } 10989 10990 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10991 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10992 10993 /* 10994 * We avoid persistance upon the last close, and set 10995 * the throttle back to the maximum. 10996 */ 10997 un->un_throttle = un->un_saved_throttle; 10998 10999 if (un->un_state == SD_STATE_OFFLINE) { 11000 if (un->un_f_is_fibre == FALSE) { 11001 scsi_log(SD_DEVINFO(un), sd_label, 11002 CE_WARN, "offline\n"); 11003 } 11004 un->un_f_geometry_is_valid = FALSE; 11005 11006 } else { 11007 /* 11008 * Flush any outstanding writes in NVRAM cache. 11009 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 11010 * cmd, it may not work for non-Pluto devices. 11011 * SYNCHRONIZE CACHE is not required for removables, 11012 * except DVD-RAM drives. 11013 * 11014 * Also note: because SYNCHRONIZE CACHE is currently 11015 * the only command issued here that requires the 11016 * drive be powered up, only do the power up before 11017 * sending the Sync Cache command. If additional 11018 * commands are added which require a powered up 11019 * drive, the following sequence may have to change. 11020 * 11021 * And finally, note that parallel SCSI on SPARC 11022 * only issues a Sync Cache to DVD-RAM, a newly 11023 * supported device. 11024 */ 11025 #if defined(__i386) || defined(__amd64) 11026 if (un->un_f_sync_cache_supported || 11027 un->un_f_dvdram_writable_device == TRUE) { 11028 #else 11029 if (un->un_f_dvdram_writable_device == TRUE) { 11030 #endif 11031 mutex_exit(SD_MUTEX(un)); 11032 if (sd_pm_entry(un) == DDI_SUCCESS) { 11033 rval = 11034 sd_send_scsi_SYNCHRONIZE_CACHE(un, 11035 NULL); 11036 /* ignore error if not supported */ 11037 if (rval == ENOTSUP) { 11038 rval = 0; 11039 } else if (rval != 0) { 11040 rval = EIO; 11041 } 11042 sd_pm_exit(un); 11043 } else { 11044 rval = EIO; 11045 } 11046 mutex_enter(SD_MUTEX(un)); 11047 } 11048 11049 /* 11050 * For devices which supports DOOR_LOCK, send an ALLOW 11051 * MEDIA REMOVAL command, but don't get upset if it 11052 * fails. We need to raise the power of the drive before 11053 * we can call sd_send_scsi_DOORLOCK() 11054 */ 11055 if (un->un_f_doorlock_supported) { 11056 mutex_exit(SD_MUTEX(un)); 11057 if (sd_pm_entry(un) == DDI_SUCCESS) { 11058 rval = sd_send_scsi_DOORLOCK(un, 11059 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 11060 11061 sd_pm_exit(un); 11062 if (ISCD(un) && (rval != 0) && 11063 (nodelay != 0)) { 11064 rval = ENXIO; 11065 } 11066 } else { 11067 rval = EIO; 11068 } 11069 mutex_enter(SD_MUTEX(un)); 11070 } 11071 11072 /* 11073 * If a device has removable media, invalidate all 11074 * parameters related to media, such as geometry, 11075 * blocksize, and blockcount. 11076 */ 11077 if (un->un_f_has_removable_media) { 11078 sr_ejected(un); 11079 } 11080 11081 /* 11082 * Destroy the cache (if it exists) which was 11083 * allocated for the write maps since this is 11084 * the last close for this media. 11085 */ 11086 if (un->un_wm_cache) { 11087 /* 11088 * Check if there are pending commands. 11089 * and if there are give a warning and 11090 * do not destroy the cache. 11091 */ 11092 if (un->un_ncmds_in_driver > 0) { 11093 scsi_log(SD_DEVINFO(un), 11094 sd_label, CE_WARN, 11095 "Unable to clean up memory " 11096 "because of pending I/O\n"); 11097 } else { 11098 kmem_cache_destroy( 11099 un->un_wm_cache); 11100 un->un_wm_cache = NULL; 11101 } 11102 } 11103 } 11104 } 11105 11106 mutex_exit(SD_MUTEX(un)); 11107 sema_v(&un->un_semoclose); 11108 11109 if (otyp == OTYP_LYR) { 11110 mutex_enter(&sd_detach_mutex); 11111 /* 11112 * The detach routine may run when the layer count 11113 * drops to zero. 11114 */ 11115 un->un_layer_count--; 11116 mutex_exit(&sd_detach_mutex); 11117 } 11118 11119 return (rval); 11120 } 11121 11122 11123 /* 11124 * Function: sd_ready_and_valid 11125 * 11126 * Description: Test if device is ready and has a valid geometry. 11127 * 11128 * Arguments: dev - device number 11129 * un - driver soft state (unit) structure 11130 * 11131 * Return Code: SD_READY_VALID ready and valid label 11132 * SD_READY_NOT_VALID ready, geom ops never applicable 11133 * SD_NOT_READY_VALID not ready, no label 11134 * SD_RESERVED_BY_OTHERS reservation conflict 11135 * 11136 * Context: Never called at interrupt context. 11137 */ 11138 11139 static int 11140 sd_ready_and_valid(struct sd_lun *un) 11141 { 11142 struct sd_errstats *stp; 11143 uint64_t capacity; 11144 uint_t lbasize; 11145 int rval = SD_READY_VALID; 11146 char name_str[48]; 11147 11148 ASSERT(un != NULL); 11149 ASSERT(!mutex_owned(SD_MUTEX(un))); 11150 11151 mutex_enter(SD_MUTEX(un)); 11152 /* 11153 * If a device has removable media, we must check if media is 11154 * ready when checking if this device is ready and valid. 11155 */ 11156 if (un->un_f_has_removable_media) { 11157 mutex_exit(SD_MUTEX(un)); 11158 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 11159 rval = SD_NOT_READY_VALID; 11160 mutex_enter(SD_MUTEX(un)); 11161 goto done; 11162 } 11163 11164 mutex_enter(SD_MUTEX(un)); 11165 if ((un->un_f_geometry_is_valid == FALSE) || 11166 (un->un_f_blockcount_is_valid == FALSE) || 11167 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 11168 11169 /* capacity has to be read every open. */ 11170 mutex_exit(SD_MUTEX(un)); 11171 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 11172 &lbasize, SD_PATH_DIRECT) != 0) { 11173 mutex_enter(SD_MUTEX(un)); 11174 un->un_f_geometry_is_valid = FALSE; 11175 rval = SD_NOT_READY_VALID; 11176 goto done; 11177 } else { 11178 mutex_enter(SD_MUTEX(un)); 11179 sd_update_block_info(un, lbasize, capacity); 11180 } 11181 } 11182 11183 /* 11184 * Check if the media in the device is writable or not. 11185 */ 11186 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 11187 sd_check_for_writable_cd(un); 11188 } 11189 11190 } else { 11191 /* 11192 * Do a test unit ready to clear any unit attention from non-cd 11193 * devices. 11194 */ 11195 mutex_exit(SD_MUTEX(un)); 11196 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 11197 mutex_enter(SD_MUTEX(un)); 11198 } 11199 11200 11201 /* 11202 * If this is a non 512 block device, allocate space for 11203 * the wmap cache. This is being done here since every time 11204 * a media is changed this routine will be called and the 11205 * block size is a function of media rather than device. 11206 */ 11207 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 11208 if (!(un->un_wm_cache)) { 11209 (void) snprintf(name_str, sizeof (name_str), 11210 "%s%d_cache", 11211 ddi_driver_name(SD_DEVINFO(un)), 11212 ddi_get_instance(SD_DEVINFO(un))); 11213 un->un_wm_cache = kmem_cache_create( 11214 name_str, sizeof (struct sd_w_map), 11215 8, sd_wm_cache_constructor, 11216 sd_wm_cache_destructor, NULL, 11217 (void *)un, NULL, 0); 11218 if (!(un->un_wm_cache)) { 11219 rval = ENOMEM; 11220 goto done; 11221 } 11222 } 11223 } 11224 11225 if (un->un_state == SD_STATE_NORMAL) { 11226 /* 11227 * If the target is not yet ready here (defined by a TUR 11228 * failure), invalidate the geometry and print an 'offline' 11229 * message. This is a legacy message, as the state of the 11230 * target is not actually changed to SD_STATE_OFFLINE. 11231 * 11232 * If the TUR fails for EACCES (Reservation Conflict), 11233 * SD_RESERVED_BY_OTHERS will be returned to indicate 11234 * reservation conflict. If the TUR fails for other 11235 * reasons, SD_NOT_READY_VALID will be returned. 11236 */ 11237 int err; 11238 11239 mutex_exit(SD_MUTEX(un)); 11240 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 11241 mutex_enter(SD_MUTEX(un)); 11242 11243 if (err != 0) { 11244 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11245 "offline or reservation conflict\n"); 11246 un->un_f_geometry_is_valid = FALSE; 11247 if (err == EACCES) { 11248 rval = SD_RESERVED_BY_OTHERS; 11249 } else { 11250 rval = SD_NOT_READY_VALID; 11251 } 11252 goto done; 11253 } 11254 } 11255 11256 if (un->un_f_format_in_progress == FALSE) { 11257 /* 11258 * Note: sd_validate_geometry may return TRUE, but that does 11259 * not necessarily mean un_f_geometry_is_valid == TRUE! 11260 */ 11261 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 11262 if (rval == ENOTSUP) { 11263 if (un->un_f_geometry_is_valid == TRUE) 11264 rval = 0; 11265 else { 11266 rval = SD_READY_NOT_VALID; 11267 goto done; 11268 } 11269 } 11270 if (rval != 0) { 11271 /* 11272 * We don't check the validity of geometry for 11273 * CDROMs. Also we assume we have a good label 11274 * even if sd_validate_geometry returned ENOMEM. 11275 */ 11276 if (!ISCD(un) && rval != ENOMEM) { 11277 rval = SD_NOT_READY_VALID; 11278 goto done; 11279 } 11280 } 11281 } 11282 11283 /* 11284 * If this device supports DOOR_LOCK command, try and send 11285 * this command to PREVENT MEDIA REMOVAL, but don't get upset 11286 * if it fails. For a CD, however, it is an error 11287 */ 11288 if (un->un_f_doorlock_supported) { 11289 mutex_exit(SD_MUTEX(un)); 11290 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 11291 SD_PATH_DIRECT) != 0) && ISCD(un)) { 11292 rval = SD_NOT_READY_VALID; 11293 mutex_enter(SD_MUTEX(un)); 11294 goto done; 11295 } 11296 mutex_enter(SD_MUTEX(un)); 11297 } 11298 11299 /* The state has changed, inform the media watch routines */ 11300 un->un_mediastate = DKIO_INSERTED; 11301 cv_broadcast(&un->un_state_cv); 11302 rval = SD_READY_VALID; 11303 11304 done: 11305 11306 /* 11307 * Initialize the capacity kstat value, if no media previously 11308 * (capacity kstat is 0) and a media has been inserted 11309 * (un_blockcount > 0). 11310 */ 11311 if (un->un_errstats != NULL) { 11312 stp = (struct sd_errstats *)un->un_errstats->ks_data; 11313 if ((stp->sd_capacity.value.ui64 == 0) && 11314 (un->un_f_blockcount_is_valid == TRUE)) { 11315 stp->sd_capacity.value.ui64 = 11316 (uint64_t)((uint64_t)un->un_blockcount * 11317 un->un_sys_blocksize); 11318 } 11319 } 11320 11321 mutex_exit(SD_MUTEX(un)); 11322 return (rval); 11323 } 11324 11325 11326 /* 11327 * Function: sdmin 11328 * 11329 * Description: Routine to limit the size of a data transfer. Used in 11330 * conjunction with physio(9F). 11331 * 11332 * Arguments: bp - pointer to the indicated buf(9S) struct. 11333 * 11334 * Context: Kernel thread context. 11335 */ 11336 11337 static void 11338 sdmin(struct buf *bp) 11339 { 11340 struct sd_lun *un; 11341 int instance; 11342 11343 instance = SDUNIT(bp->b_edev); 11344 11345 un = ddi_get_soft_state(sd_state, instance); 11346 ASSERT(un != NULL); 11347 11348 if (bp->b_bcount > un->un_max_xfer_size) { 11349 bp->b_bcount = un->un_max_xfer_size; 11350 } 11351 } 11352 11353 11354 /* 11355 * Function: sdread 11356 * 11357 * Description: Driver's read(9e) entry point function. 11358 * 11359 * Arguments: dev - device number 11360 * uio - structure pointer describing where data is to be stored 11361 * in user's space 11362 * cred_p - user credential pointer 11363 * 11364 * Return Code: ENXIO 11365 * EIO 11366 * EINVAL 11367 * value returned by physio 11368 * 11369 * Context: Kernel thread context. 11370 */ 11371 /* ARGSUSED */ 11372 static int 11373 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 11374 { 11375 struct sd_lun *un = NULL; 11376 int secmask; 11377 int err; 11378 11379 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11380 return (ENXIO); 11381 } 11382 11383 ASSERT(!mutex_owned(SD_MUTEX(un))); 11384 11385 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11386 mutex_enter(SD_MUTEX(un)); 11387 /* 11388 * Because the call to sd_ready_and_valid will issue I/O we 11389 * must wait here if either the device is suspended or 11390 * if it's power level is changing. 11391 */ 11392 while ((un->un_state == SD_STATE_SUSPENDED) || 11393 (un->un_state == SD_STATE_PM_CHANGING)) { 11394 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11395 } 11396 un->un_ncmds_in_driver++; 11397 mutex_exit(SD_MUTEX(un)); 11398 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11399 mutex_enter(SD_MUTEX(un)); 11400 un->un_ncmds_in_driver--; 11401 ASSERT(un->un_ncmds_in_driver >= 0); 11402 mutex_exit(SD_MUTEX(un)); 11403 return (EIO); 11404 } 11405 mutex_enter(SD_MUTEX(un)); 11406 un->un_ncmds_in_driver--; 11407 ASSERT(un->un_ncmds_in_driver >= 0); 11408 mutex_exit(SD_MUTEX(un)); 11409 } 11410 11411 /* 11412 * Read requests are restricted to multiples of the system block size. 11413 */ 11414 secmask = un->un_sys_blocksize - 1; 11415 11416 if (uio->uio_loffset & ((offset_t)(secmask))) { 11417 SD_ERROR(SD_LOG_READ_WRITE, un, 11418 "sdread: file offset not modulo %d\n", 11419 un->un_sys_blocksize); 11420 err = EINVAL; 11421 } else if (uio->uio_iov->iov_len & (secmask)) { 11422 SD_ERROR(SD_LOG_READ_WRITE, un, 11423 "sdread: transfer length not modulo %d\n", 11424 un->un_sys_blocksize); 11425 err = EINVAL; 11426 } else { 11427 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 11428 } 11429 return (err); 11430 } 11431 11432 11433 /* 11434 * Function: sdwrite 11435 * 11436 * Description: Driver's write(9e) entry point function. 11437 * 11438 * Arguments: dev - device number 11439 * uio - structure pointer describing where data is stored in 11440 * user's space 11441 * cred_p - user credential pointer 11442 * 11443 * Return Code: ENXIO 11444 * EIO 11445 * EINVAL 11446 * value returned by physio 11447 * 11448 * Context: Kernel thread context. 11449 */ 11450 /* ARGSUSED */ 11451 static int 11452 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11453 { 11454 struct sd_lun *un = NULL; 11455 int secmask; 11456 int err; 11457 11458 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11459 return (ENXIO); 11460 } 11461 11462 ASSERT(!mutex_owned(SD_MUTEX(un))); 11463 11464 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11465 mutex_enter(SD_MUTEX(un)); 11466 /* 11467 * Because the call to sd_ready_and_valid will issue I/O we 11468 * must wait here if either the device is suspended or 11469 * if it's power level is changing. 11470 */ 11471 while ((un->un_state == SD_STATE_SUSPENDED) || 11472 (un->un_state == SD_STATE_PM_CHANGING)) { 11473 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11474 } 11475 un->un_ncmds_in_driver++; 11476 mutex_exit(SD_MUTEX(un)); 11477 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11478 mutex_enter(SD_MUTEX(un)); 11479 un->un_ncmds_in_driver--; 11480 ASSERT(un->un_ncmds_in_driver >= 0); 11481 mutex_exit(SD_MUTEX(un)); 11482 return (EIO); 11483 } 11484 mutex_enter(SD_MUTEX(un)); 11485 un->un_ncmds_in_driver--; 11486 ASSERT(un->un_ncmds_in_driver >= 0); 11487 mutex_exit(SD_MUTEX(un)); 11488 } 11489 11490 /* 11491 * Write requests are restricted to multiples of the system block size. 11492 */ 11493 secmask = un->un_sys_blocksize - 1; 11494 11495 if (uio->uio_loffset & ((offset_t)(secmask))) { 11496 SD_ERROR(SD_LOG_READ_WRITE, un, 11497 "sdwrite: file offset not modulo %d\n", 11498 un->un_sys_blocksize); 11499 err = EINVAL; 11500 } else if (uio->uio_iov->iov_len & (secmask)) { 11501 SD_ERROR(SD_LOG_READ_WRITE, un, 11502 "sdwrite: transfer length not modulo %d\n", 11503 un->un_sys_blocksize); 11504 err = EINVAL; 11505 } else { 11506 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11507 } 11508 return (err); 11509 } 11510 11511 11512 /* 11513 * Function: sdaread 11514 * 11515 * Description: Driver's aread(9e) entry point function. 11516 * 11517 * Arguments: dev - device number 11518 * aio - structure pointer describing where data is to be stored 11519 * cred_p - user credential pointer 11520 * 11521 * Return Code: ENXIO 11522 * EIO 11523 * EINVAL 11524 * value returned by aphysio 11525 * 11526 * Context: Kernel thread context. 11527 */ 11528 /* ARGSUSED */ 11529 static int 11530 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11531 { 11532 struct sd_lun *un = NULL; 11533 struct uio *uio = aio->aio_uio; 11534 int secmask; 11535 int err; 11536 11537 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11538 return (ENXIO); 11539 } 11540 11541 ASSERT(!mutex_owned(SD_MUTEX(un))); 11542 11543 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11544 mutex_enter(SD_MUTEX(un)); 11545 /* 11546 * Because the call to sd_ready_and_valid will issue I/O we 11547 * must wait here if either the device is suspended or 11548 * if it's power level is changing. 11549 */ 11550 while ((un->un_state == SD_STATE_SUSPENDED) || 11551 (un->un_state == SD_STATE_PM_CHANGING)) { 11552 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11553 } 11554 un->un_ncmds_in_driver++; 11555 mutex_exit(SD_MUTEX(un)); 11556 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11557 mutex_enter(SD_MUTEX(un)); 11558 un->un_ncmds_in_driver--; 11559 ASSERT(un->un_ncmds_in_driver >= 0); 11560 mutex_exit(SD_MUTEX(un)); 11561 return (EIO); 11562 } 11563 mutex_enter(SD_MUTEX(un)); 11564 un->un_ncmds_in_driver--; 11565 ASSERT(un->un_ncmds_in_driver >= 0); 11566 mutex_exit(SD_MUTEX(un)); 11567 } 11568 11569 /* 11570 * Read requests are restricted to multiples of the system block size. 11571 */ 11572 secmask = un->un_sys_blocksize - 1; 11573 11574 if (uio->uio_loffset & ((offset_t)(secmask))) { 11575 SD_ERROR(SD_LOG_READ_WRITE, un, 11576 "sdaread: file offset not modulo %d\n", 11577 un->un_sys_blocksize); 11578 err = EINVAL; 11579 } else if (uio->uio_iov->iov_len & (secmask)) { 11580 SD_ERROR(SD_LOG_READ_WRITE, un, 11581 "sdaread: transfer length not modulo %d\n", 11582 un->un_sys_blocksize); 11583 err = EINVAL; 11584 } else { 11585 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11586 } 11587 return (err); 11588 } 11589 11590 11591 /* 11592 * Function: sdawrite 11593 * 11594 * Description: Driver's awrite(9e) entry point function. 11595 * 11596 * Arguments: dev - device number 11597 * aio - structure pointer describing where data is stored 11598 * cred_p - user credential pointer 11599 * 11600 * Return Code: ENXIO 11601 * EIO 11602 * EINVAL 11603 * value returned by aphysio 11604 * 11605 * Context: Kernel thread context. 11606 */ 11607 /* ARGSUSED */ 11608 static int 11609 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11610 { 11611 struct sd_lun *un = NULL; 11612 struct uio *uio = aio->aio_uio; 11613 int secmask; 11614 int err; 11615 11616 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11617 return (ENXIO); 11618 } 11619 11620 ASSERT(!mutex_owned(SD_MUTEX(un))); 11621 11622 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11623 mutex_enter(SD_MUTEX(un)); 11624 /* 11625 * Because the call to sd_ready_and_valid will issue I/O we 11626 * must wait here if either the device is suspended or 11627 * if it's power level is changing. 11628 */ 11629 while ((un->un_state == SD_STATE_SUSPENDED) || 11630 (un->un_state == SD_STATE_PM_CHANGING)) { 11631 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11632 } 11633 un->un_ncmds_in_driver++; 11634 mutex_exit(SD_MUTEX(un)); 11635 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11636 mutex_enter(SD_MUTEX(un)); 11637 un->un_ncmds_in_driver--; 11638 ASSERT(un->un_ncmds_in_driver >= 0); 11639 mutex_exit(SD_MUTEX(un)); 11640 return (EIO); 11641 } 11642 mutex_enter(SD_MUTEX(un)); 11643 un->un_ncmds_in_driver--; 11644 ASSERT(un->un_ncmds_in_driver >= 0); 11645 mutex_exit(SD_MUTEX(un)); 11646 } 11647 11648 /* 11649 * Write requests are restricted to multiples of the system block size. 11650 */ 11651 secmask = un->un_sys_blocksize - 1; 11652 11653 if (uio->uio_loffset & ((offset_t)(secmask))) { 11654 SD_ERROR(SD_LOG_READ_WRITE, un, 11655 "sdawrite: file offset not modulo %d\n", 11656 un->un_sys_blocksize); 11657 err = EINVAL; 11658 } else if (uio->uio_iov->iov_len & (secmask)) { 11659 SD_ERROR(SD_LOG_READ_WRITE, un, 11660 "sdawrite: transfer length not modulo %d\n", 11661 un->un_sys_blocksize); 11662 err = EINVAL; 11663 } else { 11664 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11665 } 11666 return (err); 11667 } 11668 11669 11670 11671 11672 11673 /* 11674 * Driver IO processing follows the following sequence: 11675 * 11676 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11677 * | | ^ 11678 * v v | 11679 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11680 * | | | | 11681 * v | | | 11682 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11683 * | | ^ ^ 11684 * v v | | 11685 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11686 * | | | | 11687 * +---+ | +------------+ +-------+ 11688 * | | | | 11689 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11690 * | v | | 11691 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11692 * | | ^ | 11693 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11694 * | v | | 11695 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11696 * | | ^ | 11697 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11698 * | v | | 11699 * | sd_checksum_iostart() sd_checksum_iodone() | 11700 * | | ^ | 11701 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11702 * | v | | 11703 * | sd_pm_iostart() sd_pm_iodone() | 11704 * | | ^ | 11705 * | | | | 11706 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11707 * | ^ 11708 * v | 11709 * sd_core_iostart() | 11710 * | | 11711 * | +------>(*destroypkt)() 11712 * +-> sd_start_cmds() <-+ | | 11713 * | | | v 11714 * | | | scsi_destroy_pkt(9F) 11715 * | | | 11716 * +->(*initpkt)() +- sdintr() 11717 * | | | | 11718 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11719 * | +-> scsi_setup_cdb(9F) | 11720 * | | 11721 * +--> scsi_transport(9F) | 11722 * | | 11723 * +----> SCSA ---->+ 11724 * 11725 * 11726 * This code is based upon the following presumtions: 11727 * 11728 * - iostart and iodone functions operate on buf(9S) structures. These 11729 * functions perform the necessary operations on the buf(9S) and pass 11730 * them along to the next function in the chain by using the macros 11731 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11732 * (for iodone side functions). 11733 * 11734 * - The iostart side functions may sleep. The iodone side functions 11735 * are called under interrupt context and may NOT sleep. Therefore 11736 * iodone side functions also may not call iostart side functions. 11737 * (NOTE: iostart side functions should NOT sleep for memory, as 11738 * this could result in deadlock.) 11739 * 11740 * - An iostart side function may call its corresponding iodone side 11741 * function directly (if necessary). 11742 * 11743 * - In the event of an error, an iostart side function can return a buf(9S) 11744 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11745 * b_error in the usual way of course). 11746 * 11747 * - The taskq mechanism may be used by the iodone side functions to dispatch 11748 * requests to the iostart side functions. The iostart side functions in 11749 * this case would be called under the context of a taskq thread, so it's 11750 * OK for them to block/sleep/spin in this case. 11751 * 11752 * - iostart side functions may allocate "shadow" buf(9S) structs and 11753 * pass them along to the next function in the chain. The corresponding 11754 * iodone side functions must coalesce the "shadow" bufs and return 11755 * the "original" buf to the next higher layer. 11756 * 11757 * - The b_private field of the buf(9S) struct holds a pointer to 11758 * an sd_xbuf struct, which contains information needed to 11759 * construct the scsi_pkt for the command. 11760 * 11761 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11762 * layer must acquire & release the SD_MUTEX(un) as needed. 11763 */ 11764 11765 11766 /* 11767 * Create taskq for all targets in the system. This is created at 11768 * _init(9E) and destroyed at _fini(9E). 11769 * 11770 * Note: here we set the minalloc to a reasonably high number to ensure that 11771 * we will have an adequate supply of task entries available at interrupt time. 11772 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11773 * sd_create_taskq(). Since we do not want to sleep for allocations at 11774 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11775 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11776 * requests any one instant in time. 11777 */ 11778 #define SD_TASKQ_NUMTHREADS 8 11779 #define SD_TASKQ_MINALLOC 256 11780 #define SD_TASKQ_MAXALLOC 256 11781 11782 static taskq_t *sd_tq = NULL; 11783 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11784 11785 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11786 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11787 11788 /* 11789 * The following task queue is being created for the write part of 11790 * read-modify-write of non-512 block size devices. 11791 * Limit the number of threads to 1 for now. This number has been choosen 11792 * considering the fact that it applies only to dvd ram drives/MO drives 11793 * currently. Performance for which is not main criteria at this stage. 11794 * Note: It needs to be explored if we can use a single taskq in future 11795 */ 11796 #define SD_WMR_TASKQ_NUMTHREADS 1 11797 static taskq_t *sd_wmr_tq = NULL; 11798 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11799 11800 /* 11801 * Function: sd_taskq_create 11802 * 11803 * Description: Create taskq thread(s) and preallocate task entries 11804 * 11805 * Return Code: Returns a pointer to the allocated taskq_t. 11806 * 11807 * Context: Can sleep. Requires blockable context. 11808 * 11809 * Notes: - The taskq() facility currently is NOT part of the DDI. 11810 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11811 * - taskq_create() will block for memory, also it will panic 11812 * if it cannot create the requested number of threads. 11813 * - Currently taskq_create() creates threads that cannot be 11814 * swapped. 11815 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11816 * supply of taskq entries at interrupt time (ie, so that we 11817 * do not have to sleep for memory) 11818 */ 11819 11820 static void 11821 sd_taskq_create(void) 11822 { 11823 char taskq_name[TASKQ_NAMELEN]; 11824 11825 ASSERT(sd_tq == NULL); 11826 ASSERT(sd_wmr_tq == NULL); 11827 11828 (void) snprintf(taskq_name, sizeof (taskq_name), 11829 "%s_drv_taskq", sd_label); 11830 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11831 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11832 TASKQ_PREPOPULATE)); 11833 11834 (void) snprintf(taskq_name, sizeof (taskq_name), 11835 "%s_rmw_taskq", sd_label); 11836 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11837 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11838 TASKQ_PREPOPULATE)); 11839 } 11840 11841 11842 /* 11843 * Function: sd_taskq_delete 11844 * 11845 * Description: Complementary cleanup routine for sd_taskq_create(). 11846 * 11847 * Context: Kernel thread context. 11848 */ 11849 11850 static void 11851 sd_taskq_delete(void) 11852 { 11853 ASSERT(sd_tq != NULL); 11854 ASSERT(sd_wmr_tq != NULL); 11855 taskq_destroy(sd_tq); 11856 taskq_destroy(sd_wmr_tq); 11857 sd_tq = NULL; 11858 sd_wmr_tq = NULL; 11859 } 11860 11861 11862 /* 11863 * Function: sdstrategy 11864 * 11865 * Description: Driver's strategy (9E) entry point function. 11866 * 11867 * Arguments: bp - pointer to buf(9S) 11868 * 11869 * Return Code: Always returns zero 11870 * 11871 * Context: Kernel thread context. 11872 */ 11873 11874 static int 11875 sdstrategy(struct buf *bp) 11876 { 11877 struct sd_lun *un; 11878 11879 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11880 if (un == NULL) { 11881 bioerror(bp, EIO); 11882 bp->b_resid = bp->b_bcount; 11883 biodone(bp); 11884 return (0); 11885 } 11886 /* As was done in the past, fail new cmds. if state is dumping. */ 11887 if (un->un_state == SD_STATE_DUMPING) { 11888 bioerror(bp, ENXIO); 11889 bp->b_resid = bp->b_bcount; 11890 biodone(bp); 11891 return (0); 11892 } 11893 11894 ASSERT(!mutex_owned(SD_MUTEX(un))); 11895 11896 /* 11897 * Commands may sneak in while we released the mutex in 11898 * DDI_SUSPEND, we should block new commands. However, old 11899 * commands that are still in the driver at this point should 11900 * still be allowed to drain. 11901 */ 11902 mutex_enter(SD_MUTEX(un)); 11903 /* 11904 * Must wait here if either the device is suspended or 11905 * if it's power level is changing. 11906 */ 11907 while ((un->un_state == SD_STATE_SUSPENDED) || 11908 (un->un_state == SD_STATE_PM_CHANGING)) { 11909 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11910 } 11911 11912 un->un_ncmds_in_driver++; 11913 11914 /* 11915 * atapi: Since we are running the CD for now in PIO mode we need to 11916 * call bp_mapin here to avoid bp_mapin called interrupt context under 11917 * the HBA's init_pkt routine. 11918 */ 11919 if (un->un_f_cfg_is_atapi == TRUE) { 11920 mutex_exit(SD_MUTEX(un)); 11921 bp_mapin(bp); 11922 mutex_enter(SD_MUTEX(un)); 11923 } 11924 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11925 un->un_ncmds_in_driver); 11926 11927 mutex_exit(SD_MUTEX(un)); 11928 11929 /* 11930 * This will (eventually) allocate the sd_xbuf area and 11931 * call sd_xbuf_strategy(). We just want to return the 11932 * result of ddi_xbuf_qstrategy so that we have an opt- 11933 * imized tail call which saves us a stack frame. 11934 */ 11935 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11936 } 11937 11938 11939 /* 11940 * Function: sd_xbuf_strategy 11941 * 11942 * Description: Function for initiating IO operations via the 11943 * ddi_xbuf_qstrategy() mechanism. 11944 * 11945 * Context: Kernel thread context. 11946 */ 11947 11948 static void 11949 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11950 { 11951 struct sd_lun *un = arg; 11952 11953 ASSERT(bp != NULL); 11954 ASSERT(xp != NULL); 11955 ASSERT(un != NULL); 11956 ASSERT(!mutex_owned(SD_MUTEX(un))); 11957 11958 /* 11959 * Initialize the fields in the xbuf and save a pointer to the 11960 * xbuf in bp->b_private. 11961 */ 11962 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11963 11964 /* Send the buf down the iostart chain */ 11965 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11966 } 11967 11968 11969 /* 11970 * Function: sd_xbuf_init 11971 * 11972 * Description: Prepare the given sd_xbuf struct for use. 11973 * 11974 * Arguments: un - ptr to softstate 11975 * bp - ptr to associated buf(9S) 11976 * xp - ptr to associated sd_xbuf 11977 * chain_type - IO chain type to use: 11978 * SD_CHAIN_NULL 11979 * SD_CHAIN_BUFIO 11980 * SD_CHAIN_USCSI 11981 * SD_CHAIN_DIRECT 11982 * SD_CHAIN_DIRECT_PRIORITY 11983 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11984 * initialization; may be NULL if none. 11985 * 11986 * Context: Kernel thread context 11987 */ 11988 11989 static void 11990 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11991 uchar_t chain_type, void *pktinfop) 11992 { 11993 int index; 11994 11995 ASSERT(un != NULL); 11996 ASSERT(bp != NULL); 11997 ASSERT(xp != NULL); 11998 11999 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 12000 bp, chain_type); 12001 12002 xp->xb_un = un; 12003 xp->xb_pktp = NULL; 12004 xp->xb_pktinfo = pktinfop; 12005 xp->xb_private = bp->b_private; 12006 xp->xb_blkno = (daddr_t)bp->b_blkno; 12007 12008 /* 12009 * Set up the iostart and iodone chain indexes in the xbuf, based 12010 * upon the specified chain type to use. 12011 */ 12012 switch (chain_type) { 12013 case SD_CHAIN_NULL: 12014 /* 12015 * Fall thru to just use the values for the buf type, even 12016 * tho for the NULL chain these values will never be used. 12017 */ 12018 /* FALLTHRU */ 12019 case SD_CHAIN_BUFIO: 12020 index = un->un_buf_chain_type; 12021 break; 12022 case SD_CHAIN_USCSI: 12023 index = un->un_uscsi_chain_type; 12024 break; 12025 case SD_CHAIN_DIRECT: 12026 index = un->un_direct_chain_type; 12027 break; 12028 case SD_CHAIN_DIRECT_PRIORITY: 12029 index = un->un_priority_chain_type; 12030 break; 12031 default: 12032 /* We're really broken if we ever get here... */ 12033 panic("sd_xbuf_init: illegal chain type!"); 12034 /*NOTREACHED*/ 12035 } 12036 12037 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 12038 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 12039 12040 /* 12041 * It might be a bit easier to simply bzero the entire xbuf above, 12042 * but it turns out that since we init a fair number of members anyway, 12043 * we save a fair number cycles by doing explicit assignment of zero. 12044 */ 12045 xp->xb_pkt_flags = 0; 12046 xp->xb_dma_resid = 0; 12047 xp->xb_retry_count = 0; 12048 xp->xb_victim_retry_count = 0; 12049 xp->xb_ua_retry_count = 0; 12050 xp->xb_sense_bp = NULL; 12051 xp->xb_sense_status = 0; 12052 xp->xb_sense_state = 0; 12053 xp->xb_sense_resid = 0; 12054 12055 bp->b_private = xp; 12056 bp->b_flags &= ~(B_DONE | B_ERROR); 12057 bp->b_resid = 0; 12058 bp->av_forw = NULL; 12059 bp->av_back = NULL; 12060 bioerror(bp, 0); 12061 12062 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 12063 } 12064 12065 12066 /* 12067 * Function: sd_uscsi_strategy 12068 * 12069 * Description: Wrapper for calling into the USCSI chain via physio(9F) 12070 * 12071 * Arguments: bp - buf struct ptr 12072 * 12073 * Return Code: Always returns 0 12074 * 12075 * Context: Kernel thread context 12076 */ 12077 12078 static int 12079 sd_uscsi_strategy(struct buf *bp) 12080 { 12081 struct sd_lun *un; 12082 struct sd_uscsi_info *uip; 12083 struct sd_xbuf *xp; 12084 uchar_t chain_type; 12085 12086 ASSERT(bp != NULL); 12087 12088 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 12089 if (un == NULL) { 12090 bioerror(bp, EIO); 12091 bp->b_resid = bp->b_bcount; 12092 biodone(bp); 12093 return (0); 12094 } 12095 12096 ASSERT(!mutex_owned(SD_MUTEX(un))); 12097 12098 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 12099 12100 mutex_enter(SD_MUTEX(un)); 12101 /* 12102 * atapi: Since we are running the CD for now in PIO mode we need to 12103 * call bp_mapin here to avoid bp_mapin called interrupt context under 12104 * the HBA's init_pkt routine. 12105 */ 12106 if (un->un_f_cfg_is_atapi == TRUE) { 12107 mutex_exit(SD_MUTEX(un)); 12108 bp_mapin(bp); 12109 mutex_enter(SD_MUTEX(un)); 12110 } 12111 un->un_ncmds_in_driver++; 12112 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 12113 un->un_ncmds_in_driver); 12114 mutex_exit(SD_MUTEX(un)); 12115 12116 /* 12117 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 12118 */ 12119 ASSERT(bp->b_private != NULL); 12120 uip = (struct sd_uscsi_info *)bp->b_private; 12121 12122 switch (uip->ui_flags) { 12123 case SD_PATH_DIRECT: 12124 chain_type = SD_CHAIN_DIRECT; 12125 break; 12126 case SD_PATH_DIRECT_PRIORITY: 12127 chain_type = SD_CHAIN_DIRECT_PRIORITY; 12128 break; 12129 default: 12130 chain_type = SD_CHAIN_USCSI; 12131 break; 12132 } 12133 12134 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12135 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 12136 12137 /* Use the index obtained within xbuf_init */ 12138 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 12139 12140 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 12141 12142 return (0); 12143 } 12144 12145 12146 /* 12147 * These routines perform raw i/o operations. 12148 */ 12149 /*ARGSUSED*/ 12150 static void 12151 sduscsimin(struct buf *bp) 12152 { 12153 /* 12154 * do not break up because the CDB count would then 12155 * be incorrect and data underruns would result (incomplete 12156 * read/writes which would be retried and then failed, see 12157 * sdintr(). 12158 */ 12159 } 12160 12161 12162 12163 /* 12164 * Function: sd_send_scsi_cmd 12165 * 12166 * Description: Runs a USCSI command for user (when called thru sdioctl), 12167 * or for the driver 12168 * 12169 * Arguments: dev - the dev_t for the device 12170 * incmd - ptr to a valid uscsi_cmd struct 12171 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 12172 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 12173 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 12174 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 12175 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 12176 * to use the USCSI "direct" chain and bypass the normal 12177 * command waitq. 12178 * 12179 * Return Code: 0 - successful completion of the given command 12180 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 12181 * ENXIO - soft state not found for specified dev 12182 * EINVAL 12183 * EFAULT - copyin/copyout error 12184 * return code of biowait(9F) or physio(9F): 12185 * EIO - IO error, caller may check incmd->uscsi_status 12186 * ENXIO 12187 * EACCES - reservation conflict 12188 * 12189 * Context: Waits for command to complete. Can sleep. 12190 */ 12191 12192 static int 12193 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 12194 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 12195 int path_flag) 12196 { 12197 struct sd_uscsi_info *uip; 12198 struct uscsi_cmd *uscmd; 12199 struct sd_lun *un; 12200 struct buf *bp; 12201 int rval; 12202 int flags; 12203 12204 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 12205 if (un == NULL) { 12206 return (ENXIO); 12207 } 12208 12209 ASSERT(!mutex_owned(SD_MUTEX(un))); 12210 12211 #ifdef SDDEBUG 12212 switch (dataspace) { 12213 case UIO_USERSPACE: 12214 SD_TRACE(SD_LOG_IO, un, 12215 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 12216 break; 12217 case UIO_SYSSPACE: 12218 SD_TRACE(SD_LOG_IO, un, 12219 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 12220 break; 12221 default: 12222 SD_TRACE(SD_LOG_IO, un, 12223 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 12224 break; 12225 } 12226 #endif 12227 12228 /* 12229 * Perform resets directly; no need to generate a command to do it. 12230 */ 12231 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 12232 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 12233 RESET_ALL : RESET_TARGET; 12234 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 12235 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 12236 /* Reset attempt was unsuccessful */ 12237 SD_TRACE(SD_LOG_IO, un, 12238 "sd_send_scsi_cmd: reset: failure\n"); 12239 return (EIO); 12240 } 12241 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 12242 return (0); 12243 } 12244 12245 /* Perfunctory sanity check... */ 12246 if (incmd->uscsi_cdblen <= 0) { 12247 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12248 "invalid uscsi_cdblen, returning EINVAL\n"); 12249 return (EINVAL); 12250 } else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) { 12251 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12252 "unsupported uscsi_cdblen, returning EINVAL\n"); 12253 return (EINVAL); 12254 } 12255 12256 /* 12257 * In order to not worry about where the uscsi structure came from 12258 * (or where the cdb it points to came from) we're going to make 12259 * kmem_alloc'd copies of them here. This will also allow reference 12260 * to the data they contain long after this process has gone to 12261 * sleep and its kernel stack has been unmapped, etc. 12262 * 12263 * First get some memory for the uscsi_cmd struct and copy the 12264 * contents of the given uscsi_cmd struct into it. 12265 */ 12266 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12267 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 12268 12269 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 12270 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 12271 12272 /* 12273 * Now get some space for the CDB, and copy the given CDB into 12274 * it. Use ddi_copyin() in case the data is in user space. 12275 */ 12276 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 12277 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 12278 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 12279 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 12280 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 12281 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12282 return (EFAULT); 12283 } 12284 12285 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 12286 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 12287 12288 bp = getrbuf(KM_SLEEP); 12289 12290 /* 12291 * Allocate an sd_uscsi_info struct and fill it with the info 12292 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12293 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12294 * since we allocate the buf here in this function, we do not 12295 * need to preserve the prior contents of b_private. 12296 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12297 */ 12298 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 12299 uip->ui_flags = path_flag; 12300 uip->ui_cmdp = uscmd; 12301 bp->b_private = uip; 12302 12303 /* 12304 * Initialize Request Sense buffering, if requested. 12305 */ 12306 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12307 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12308 /* 12309 * Here uscmd->uscsi_rqbuf currently points to the caller's 12310 * buffer, but we replace this with a kernel buffer that 12311 * we allocate to use with the sense data. The sense data 12312 * (if present) gets copied into this new buffer before the 12313 * command is completed. Then we copy the sense data from 12314 * our allocated buf into the caller's buffer below. Note 12315 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 12316 * below to perform the copy back to the caller's buf. 12317 */ 12318 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 12319 if (rqbufspace == UIO_USERSPACE) { 12320 uscmd->uscsi_rqlen = SENSE_LENGTH; 12321 uscmd->uscsi_rqresid = SENSE_LENGTH; 12322 } else { 12323 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 12324 uscmd->uscsi_rqlen = rlen; 12325 uscmd->uscsi_rqresid = rlen; 12326 } 12327 } else { 12328 uscmd->uscsi_rqbuf = NULL; 12329 uscmd->uscsi_rqlen = 0; 12330 uscmd->uscsi_rqresid = 0; 12331 } 12332 12333 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 12334 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 12335 12336 if (un->un_f_is_fibre == FALSE) { 12337 /* 12338 * Force asynchronous mode, if necessary. Doing this here 12339 * has the unfortunate effect of running other queued 12340 * commands async also, but since the main purpose of this 12341 * capability is downloading new drive firmware, we can 12342 * probably live with it. 12343 */ 12344 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 12345 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12346 == 1) { 12347 if (scsi_ifsetcap(SD_ADDRESS(un), 12348 "synchronous", 0, 1) == 1) { 12349 SD_TRACE(SD_LOG_IO, un, 12350 "sd_send_scsi_cmd: forced async ok\n"); 12351 } else { 12352 SD_TRACE(SD_LOG_IO, un, 12353 "sd_send_scsi_cmd:\ 12354 forced async failed\n"); 12355 rval = EINVAL; 12356 goto done; 12357 } 12358 } 12359 } 12360 12361 /* 12362 * Re-enable synchronous mode, if requested 12363 */ 12364 if (uscmd->uscsi_flags & USCSI_SYNC) { 12365 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12366 == 0) { 12367 int i = scsi_ifsetcap(SD_ADDRESS(un), 12368 "synchronous", 1, 1); 12369 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12370 "re-enabled sync %s\n", 12371 (i == 1) ? "ok" : "failed"); 12372 } 12373 } 12374 } 12375 12376 /* 12377 * Commands sent with priority are intended for error recovery 12378 * situations, and do not have retries performed. 12379 */ 12380 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12381 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12382 } 12383 12384 /* 12385 * If we're going to do actual I/O, let physio do all the right things 12386 */ 12387 if (uscmd->uscsi_buflen != 0) { 12388 struct iovec aiov; 12389 struct uio auio; 12390 struct uio *uio = &auio; 12391 12392 bzero(&auio, sizeof (struct uio)); 12393 bzero(&aiov, sizeof (struct iovec)); 12394 aiov.iov_base = uscmd->uscsi_bufaddr; 12395 aiov.iov_len = uscmd->uscsi_buflen; 12396 uio->uio_iov = &aiov; 12397 12398 uio->uio_iovcnt = 1; 12399 uio->uio_resid = uscmd->uscsi_buflen; 12400 uio->uio_segflg = dataspace; 12401 12402 /* 12403 * physio() will block here until the command completes.... 12404 */ 12405 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 12406 12407 rval = physio(sd_uscsi_strategy, bp, dev, 12408 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 12409 sduscsimin, uio); 12410 12411 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12412 "returned from physio with 0x%x\n", rval); 12413 12414 } else { 12415 /* 12416 * We have to mimic what physio would do here! Argh! 12417 */ 12418 bp->b_flags = B_BUSY | 12419 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 12420 bp->b_edev = dev; 12421 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 12422 bp->b_bcount = 0; 12423 bp->b_blkno = 0; 12424 12425 SD_TRACE(SD_LOG_IO, un, 12426 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 12427 12428 (void) sd_uscsi_strategy(bp); 12429 12430 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 12431 12432 rval = biowait(bp); 12433 12434 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12435 "returned from biowait with 0x%x\n", rval); 12436 } 12437 12438 done: 12439 12440 #ifdef SDDEBUG 12441 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12442 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12443 uscmd->uscsi_status, uscmd->uscsi_resid); 12444 if (uscmd->uscsi_bufaddr != NULL) { 12445 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12446 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12447 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12448 if (dataspace == UIO_SYSSPACE) { 12449 SD_DUMP_MEMORY(un, SD_LOG_IO, 12450 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12451 uscmd->uscsi_buflen, SD_LOG_HEX); 12452 } 12453 } 12454 #endif 12455 12456 /* 12457 * Get the status and residual to return to the caller. 12458 */ 12459 incmd->uscsi_status = uscmd->uscsi_status; 12460 incmd->uscsi_resid = uscmd->uscsi_resid; 12461 12462 /* 12463 * If the caller wants sense data, copy back whatever sense data 12464 * we may have gotten, and update the relevant rqsense info. 12465 */ 12466 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12467 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12468 12469 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 12470 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 12471 12472 /* Update the Request Sense status and resid */ 12473 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 12474 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 12475 12476 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12477 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 12478 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 12479 12480 /* Copy out the sense data for user processes */ 12481 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 12482 int flags = 12483 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 12484 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 12485 rqlen, flags) != 0) { 12486 rval = EFAULT; 12487 } 12488 /* 12489 * Note: Can't touch incmd->uscsi_rqbuf so use 12490 * uscmd->uscsi_rqbuf instead. They're the same. 12491 */ 12492 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12493 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 12494 incmd->uscsi_rqbuf, rqlen); 12495 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 12496 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 12497 } 12498 } 12499 12500 /* 12501 * Free allocated resources and return; mapout the buf in case it was 12502 * mapped in by a lower layer. 12503 */ 12504 bp_mapout(bp); 12505 freerbuf(bp); 12506 kmem_free(uip, sizeof (struct sd_uscsi_info)); 12507 if (uscmd->uscsi_rqbuf != NULL) { 12508 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 12509 } 12510 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 12511 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12512 12513 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 12514 12515 return (rval); 12516 } 12517 12518 12519 /* 12520 * Function: sd_buf_iodone 12521 * 12522 * Description: Frees the sd_xbuf & returns the buf to its originator. 12523 * 12524 * Context: May be called from interrupt context. 12525 */ 12526 /* ARGSUSED */ 12527 static void 12528 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12529 { 12530 struct sd_xbuf *xp; 12531 12532 ASSERT(un != NULL); 12533 ASSERT(bp != NULL); 12534 ASSERT(!mutex_owned(SD_MUTEX(un))); 12535 12536 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12537 12538 xp = SD_GET_XBUF(bp); 12539 ASSERT(xp != NULL); 12540 12541 mutex_enter(SD_MUTEX(un)); 12542 12543 /* 12544 * Grab time when the cmd completed. 12545 * This is used for determining if the system has been 12546 * idle long enough to make it idle to the PM framework. 12547 * This is for lowering the overhead, and therefore improving 12548 * performance per I/O operation. 12549 */ 12550 un->un_pm_idle_time = ddi_get_time(); 12551 12552 un->un_ncmds_in_driver--; 12553 ASSERT(un->un_ncmds_in_driver >= 0); 12554 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12555 un->un_ncmds_in_driver); 12556 12557 mutex_exit(SD_MUTEX(un)); 12558 12559 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12560 biodone(bp); /* bp is gone after this */ 12561 12562 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12563 } 12564 12565 12566 /* 12567 * Function: sd_uscsi_iodone 12568 * 12569 * Description: Frees the sd_xbuf & returns the buf to its originator. 12570 * 12571 * Context: May be called from interrupt context. 12572 */ 12573 /* ARGSUSED */ 12574 static void 12575 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12576 { 12577 struct sd_xbuf *xp; 12578 12579 ASSERT(un != NULL); 12580 ASSERT(bp != NULL); 12581 12582 xp = SD_GET_XBUF(bp); 12583 ASSERT(xp != NULL); 12584 ASSERT(!mutex_owned(SD_MUTEX(un))); 12585 12586 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12587 12588 bp->b_private = xp->xb_private; 12589 12590 mutex_enter(SD_MUTEX(un)); 12591 12592 /* 12593 * Grab time when the cmd completed. 12594 * This is used for determining if the system has been 12595 * idle long enough to make it idle to the PM framework. 12596 * This is for lowering the overhead, and therefore improving 12597 * performance per I/O operation. 12598 */ 12599 un->un_pm_idle_time = ddi_get_time(); 12600 12601 un->un_ncmds_in_driver--; 12602 ASSERT(un->un_ncmds_in_driver >= 0); 12603 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12604 un->un_ncmds_in_driver); 12605 12606 mutex_exit(SD_MUTEX(un)); 12607 12608 kmem_free(xp, sizeof (struct sd_xbuf)); 12609 biodone(bp); 12610 12611 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12612 } 12613 12614 12615 /* 12616 * Function: sd_mapblockaddr_iostart 12617 * 12618 * Description: Verify request lies withing the partition limits for 12619 * the indicated minor device. Issue "overrun" buf if 12620 * request would exceed partition range. Converts 12621 * partition-relative block address to absolute. 12622 * 12623 * Context: Can sleep 12624 * 12625 * Issues: This follows what the old code did, in terms of accessing 12626 * some of the partition info in the unit struct without holding 12627 * the mutext. This is a general issue, if the partition info 12628 * can be altered while IO is in progress... as soon as we send 12629 * a buf, its partitioning can be invalid before it gets to the 12630 * device. Probably the right fix is to move partitioning out 12631 * of the driver entirely. 12632 */ 12633 12634 static void 12635 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12636 { 12637 daddr_t nblocks; /* #blocks in the given partition */ 12638 daddr_t blocknum; /* Block number specified by the buf */ 12639 size_t requested_nblocks; 12640 size_t available_nblocks; 12641 int partition; 12642 diskaddr_t partition_offset; 12643 struct sd_xbuf *xp; 12644 12645 12646 ASSERT(un != NULL); 12647 ASSERT(bp != NULL); 12648 ASSERT(!mutex_owned(SD_MUTEX(un))); 12649 12650 SD_TRACE(SD_LOG_IO_PARTITION, un, 12651 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12652 12653 xp = SD_GET_XBUF(bp); 12654 ASSERT(xp != NULL); 12655 12656 /* 12657 * If the geometry is not indicated as valid, attempt to access 12658 * the unit & verify the geometry/label. This can be the case for 12659 * removable-media devices, of if the device was opened in 12660 * NDELAY/NONBLOCK mode. 12661 */ 12662 if ((un->un_f_geometry_is_valid != TRUE) && 12663 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12664 /* 12665 * For removable devices it is possible to start an I/O 12666 * without a media by opening the device in nodelay mode. 12667 * Also for writable CDs there can be many scenarios where 12668 * there is no geometry yet but volume manager is trying to 12669 * issue a read() just because it can see TOC on the CD. So 12670 * do not print a message for removables. 12671 */ 12672 if (!un->un_f_has_removable_media) { 12673 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12674 "i/o to invalid geometry\n"); 12675 } 12676 bioerror(bp, EIO); 12677 bp->b_resid = bp->b_bcount; 12678 SD_BEGIN_IODONE(index, un, bp); 12679 return; 12680 } 12681 12682 partition = SDPART(bp->b_edev); 12683 12684 /* #blocks in partition */ 12685 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12686 12687 /* Use of a local variable potentially improves performance slightly */ 12688 partition_offset = un->un_offset[partition]; 12689 12690 /* 12691 * blocknum is the starting block number of the request. At this 12692 * point it is still relative to the start of the minor device. 12693 */ 12694 blocknum = xp->xb_blkno; 12695 12696 /* 12697 * Legacy: If the starting block number is one past the last block 12698 * in the partition, do not set B_ERROR in the buf. 12699 */ 12700 if (blocknum == nblocks) { 12701 goto error_exit; 12702 } 12703 12704 /* 12705 * Confirm that the first block of the request lies within the 12706 * partition limits. Also the requested number of bytes must be 12707 * a multiple of the system block size. 12708 */ 12709 if ((blocknum < 0) || (blocknum >= nblocks) || 12710 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12711 bp->b_flags |= B_ERROR; 12712 goto error_exit; 12713 } 12714 12715 /* 12716 * If the requsted # blocks exceeds the available # blocks, that 12717 * is an overrun of the partition. 12718 */ 12719 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12720 available_nblocks = (size_t)(nblocks - blocknum); 12721 ASSERT(nblocks >= blocknum); 12722 12723 if (requested_nblocks > available_nblocks) { 12724 /* 12725 * Allocate an "overrun" buf to allow the request to proceed 12726 * for the amount of space available in the partition. The 12727 * amount not transferred will be added into the b_resid 12728 * when the operation is complete. The overrun buf 12729 * replaces the original buf here, and the original buf 12730 * is saved inside the overrun buf, for later use. 12731 */ 12732 size_t resid = SD_SYSBLOCKS2BYTES(un, 12733 (offset_t)(requested_nblocks - available_nblocks)); 12734 size_t count = bp->b_bcount - resid; 12735 /* 12736 * Note: count is an unsigned entity thus it'll NEVER 12737 * be less than 0 so ASSERT the original values are 12738 * correct. 12739 */ 12740 ASSERT(bp->b_bcount >= resid); 12741 12742 bp = sd_bioclone_alloc(bp, count, blocknum, 12743 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12744 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12745 ASSERT(xp != NULL); 12746 } 12747 12748 /* At this point there should be no residual for this buf. */ 12749 ASSERT(bp->b_resid == 0); 12750 12751 /* Convert the block number to an absolute address. */ 12752 xp->xb_blkno += partition_offset; 12753 12754 SD_NEXT_IOSTART(index, un, bp); 12755 12756 SD_TRACE(SD_LOG_IO_PARTITION, un, 12757 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12758 12759 return; 12760 12761 error_exit: 12762 bp->b_resid = bp->b_bcount; 12763 SD_BEGIN_IODONE(index, un, bp); 12764 SD_TRACE(SD_LOG_IO_PARTITION, un, 12765 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12766 } 12767 12768 12769 /* 12770 * Function: sd_mapblockaddr_iodone 12771 * 12772 * Description: Completion-side processing for partition management. 12773 * 12774 * Context: May be called under interrupt context 12775 */ 12776 12777 static void 12778 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12779 { 12780 /* int partition; */ /* Not used, see below. */ 12781 ASSERT(un != NULL); 12782 ASSERT(bp != NULL); 12783 ASSERT(!mutex_owned(SD_MUTEX(un))); 12784 12785 SD_TRACE(SD_LOG_IO_PARTITION, un, 12786 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12787 12788 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12789 /* 12790 * We have an "overrun" buf to deal with... 12791 */ 12792 struct sd_xbuf *xp; 12793 struct buf *obp; /* ptr to the original buf */ 12794 12795 xp = SD_GET_XBUF(bp); 12796 ASSERT(xp != NULL); 12797 12798 /* Retrieve the pointer to the original buf */ 12799 obp = (struct buf *)xp->xb_private; 12800 ASSERT(obp != NULL); 12801 12802 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12803 bioerror(obp, bp->b_error); 12804 12805 sd_bioclone_free(bp); 12806 12807 /* 12808 * Get back the original buf. 12809 * Note that since the restoration of xb_blkno below 12810 * was removed, the sd_xbuf is not needed. 12811 */ 12812 bp = obp; 12813 /* 12814 * xp = SD_GET_XBUF(bp); 12815 * ASSERT(xp != NULL); 12816 */ 12817 } 12818 12819 /* 12820 * Convert sd->xb_blkno back to a minor-device relative value. 12821 * Note: this has been commented out, as it is not needed in the 12822 * current implementation of the driver (ie, since this function 12823 * is at the top of the layering chains, so the info will be 12824 * discarded) and it is in the "hot" IO path. 12825 * 12826 * partition = getminor(bp->b_edev) & SDPART_MASK; 12827 * xp->xb_blkno -= un->un_offset[partition]; 12828 */ 12829 12830 SD_NEXT_IODONE(index, un, bp); 12831 12832 SD_TRACE(SD_LOG_IO_PARTITION, un, 12833 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12834 } 12835 12836 12837 /* 12838 * Function: sd_mapblocksize_iostart 12839 * 12840 * Description: Convert between system block size (un->un_sys_blocksize) 12841 * and target block size (un->un_tgt_blocksize). 12842 * 12843 * Context: Can sleep to allocate resources. 12844 * 12845 * Assumptions: A higher layer has already performed any partition validation, 12846 * and converted the xp->xb_blkno to an absolute value relative 12847 * to the start of the device. 12848 * 12849 * It is also assumed that the higher layer has implemented 12850 * an "overrun" mechanism for the case where the request would 12851 * read/write beyond the end of a partition. In this case we 12852 * assume (and ASSERT) that bp->b_resid == 0. 12853 * 12854 * Note: The implementation for this routine assumes the target 12855 * block size remains constant between allocation and transport. 12856 */ 12857 12858 static void 12859 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12860 { 12861 struct sd_mapblocksize_info *bsp; 12862 struct sd_xbuf *xp; 12863 offset_t first_byte; 12864 daddr_t start_block, end_block; 12865 daddr_t request_bytes; 12866 ushort_t is_aligned = FALSE; 12867 12868 ASSERT(un != NULL); 12869 ASSERT(bp != NULL); 12870 ASSERT(!mutex_owned(SD_MUTEX(un))); 12871 ASSERT(bp->b_resid == 0); 12872 12873 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12874 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12875 12876 /* 12877 * For a non-writable CD, a write request is an error 12878 */ 12879 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12880 (un->un_f_mmc_writable_media == FALSE)) { 12881 bioerror(bp, EIO); 12882 bp->b_resid = bp->b_bcount; 12883 SD_BEGIN_IODONE(index, un, bp); 12884 return; 12885 } 12886 12887 /* 12888 * We do not need a shadow buf if the device is using 12889 * un->un_sys_blocksize as its block size or if bcount == 0. 12890 * In this case there is no layer-private data block allocated. 12891 */ 12892 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12893 (bp->b_bcount == 0)) { 12894 goto done; 12895 } 12896 12897 #if defined(__i386) || defined(__amd64) 12898 /* We do not support non-block-aligned transfers for ROD devices */ 12899 ASSERT(!ISROD(un)); 12900 #endif 12901 12902 xp = SD_GET_XBUF(bp); 12903 ASSERT(xp != NULL); 12904 12905 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12906 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12907 un->un_tgt_blocksize, un->un_sys_blocksize); 12908 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12909 "request start block:0x%x\n", xp->xb_blkno); 12910 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12911 "request len:0x%x\n", bp->b_bcount); 12912 12913 /* 12914 * Allocate the layer-private data area for the mapblocksize layer. 12915 * Layers are allowed to use the xp_private member of the sd_xbuf 12916 * struct to store the pointer to their layer-private data block, but 12917 * each layer also has the responsibility of restoring the prior 12918 * contents of xb_private before returning the buf/xbuf to the 12919 * higher layer that sent it. 12920 * 12921 * Here we save the prior contents of xp->xb_private into the 12922 * bsp->mbs_oprivate field of our layer-private data area. This value 12923 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12924 * the layer-private area and returning the buf/xbuf to the layer 12925 * that sent it. 12926 * 12927 * Note that here we use kmem_zalloc for the allocation as there are 12928 * parts of the mapblocksize code that expect certain fields to be 12929 * zero unless explicitly set to a required value. 12930 */ 12931 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12932 bsp->mbs_oprivate = xp->xb_private; 12933 xp->xb_private = bsp; 12934 12935 /* 12936 * This treats the data on the disk (target) as an array of bytes. 12937 * first_byte is the byte offset, from the beginning of the device, 12938 * to the location of the request. This is converted from a 12939 * un->un_sys_blocksize block address to a byte offset, and then back 12940 * to a block address based upon a un->un_tgt_blocksize block size. 12941 * 12942 * xp->xb_blkno should be absolute upon entry into this function, 12943 * but, but it is based upon partitions that use the "system" 12944 * block size. It must be adjusted to reflect the block size of 12945 * the target. 12946 * 12947 * Note that end_block is actually the block that follows the last 12948 * block of the request, but that's what is needed for the computation. 12949 */ 12950 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12951 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12952 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12953 un->un_tgt_blocksize; 12954 12955 /* request_bytes is rounded up to a multiple of the target block size */ 12956 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12957 12958 /* 12959 * See if the starting address of the request and the request 12960 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12961 * then we do not need to allocate a shadow buf to handle the request. 12962 */ 12963 if (((first_byte % un->un_tgt_blocksize) == 0) && 12964 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12965 is_aligned = TRUE; 12966 } 12967 12968 if ((bp->b_flags & B_READ) == 0) { 12969 /* 12970 * Lock the range for a write operation. An aligned request is 12971 * considered a simple write; otherwise the request must be a 12972 * read-modify-write. 12973 */ 12974 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12975 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12976 } 12977 12978 /* 12979 * Alloc a shadow buf if the request is not aligned. Also, this is 12980 * where the READ command is generated for a read-modify-write. (The 12981 * write phase is deferred until after the read completes.) 12982 */ 12983 if (is_aligned == FALSE) { 12984 12985 struct sd_mapblocksize_info *shadow_bsp; 12986 struct sd_xbuf *shadow_xp; 12987 struct buf *shadow_bp; 12988 12989 /* 12990 * Allocate the shadow buf and it associated xbuf. Note that 12991 * after this call the xb_blkno value in both the original 12992 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12993 * same: absolute relative to the start of the device, and 12994 * adjusted for the target block size. The b_blkno in the 12995 * shadow buf will also be set to this value. We should never 12996 * change b_blkno in the original bp however. 12997 * 12998 * Note also that the shadow buf will always need to be a 12999 * READ command, regardless of whether the incoming command 13000 * is a READ or a WRITE. 13001 */ 13002 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 13003 xp->xb_blkno, 13004 (int (*)(struct buf *)) sd_mapblocksize_iodone); 13005 13006 shadow_xp = SD_GET_XBUF(shadow_bp); 13007 13008 /* 13009 * Allocate the layer-private data for the shadow buf. 13010 * (No need to preserve xb_private in the shadow xbuf.) 13011 */ 13012 shadow_xp->xb_private = shadow_bsp = 13013 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 13014 13015 /* 13016 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 13017 * to figure out where the start of the user data is (based upon 13018 * the system block size) in the data returned by the READ 13019 * command (which will be based upon the target blocksize). Note 13020 * that this is only really used if the request is unaligned. 13021 */ 13022 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13023 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13024 ASSERT((bsp->mbs_copy_offset >= 0) && 13025 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13026 13027 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13028 13029 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13030 13031 /* Transfer the wmap (if any) to the shadow buf */ 13032 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13033 bsp->mbs_wmp = NULL; 13034 13035 /* 13036 * The shadow buf goes on from here in place of the 13037 * original buf. 13038 */ 13039 shadow_bsp->mbs_orig_bp = bp; 13040 bp = shadow_bp; 13041 } 13042 13043 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13044 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13045 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13046 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13047 request_bytes); 13048 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13049 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13050 13051 done: 13052 SD_NEXT_IOSTART(index, un, bp); 13053 13054 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13055 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13056 } 13057 13058 13059 /* 13060 * Function: sd_mapblocksize_iodone 13061 * 13062 * Description: Completion side processing for block-size mapping. 13063 * 13064 * Context: May be called under interrupt context 13065 */ 13066 13067 static void 13068 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13069 { 13070 struct sd_mapblocksize_info *bsp; 13071 struct sd_xbuf *xp; 13072 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13073 struct buf *orig_bp; /* ptr to the original buf */ 13074 offset_t shadow_end; 13075 offset_t request_end; 13076 offset_t shadow_start; 13077 ssize_t copy_offset; 13078 size_t copy_length; 13079 size_t shortfall; 13080 uint_t is_write; /* TRUE if this bp is a WRITE */ 13081 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13082 13083 ASSERT(un != NULL); 13084 ASSERT(bp != NULL); 13085 13086 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13087 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13088 13089 /* 13090 * There is no shadow buf or layer-private data if the target is 13091 * using un->un_sys_blocksize as its block size or if bcount == 0. 13092 */ 13093 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 13094 (bp->b_bcount == 0)) { 13095 goto exit; 13096 } 13097 13098 xp = SD_GET_XBUF(bp); 13099 ASSERT(xp != NULL); 13100 13101 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13102 bsp = xp->xb_private; 13103 13104 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13105 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13106 13107 if (is_write) { 13108 /* 13109 * For a WRITE request we must free up the block range that 13110 * we have locked up. This holds regardless of whether this is 13111 * an aligned write request or a read-modify-write request. 13112 */ 13113 sd_range_unlock(un, bsp->mbs_wmp); 13114 bsp->mbs_wmp = NULL; 13115 } 13116 13117 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13118 /* 13119 * An aligned read or write command will have no shadow buf; 13120 * there is not much else to do with it. 13121 */ 13122 goto done; 13123 } 13124 13125 orig_bp = bsp->mbs_orig_bp; 13126 ASSERT(orig_bp != NULL); 13127 orig_xp = SD_GET_XBUF(orig_bp); 13128 ASSERT(orig_xp != NULL); 13129 ASSERT(!mutex_owned(SD_MUTEX(un))); 13130 13131 if (!is_write && has_wmap) { 13132 /* 13133 * A READ with a wmap means this is the READ phase of a 13134 * read-modify-write. If an error occurred on the READ then 13135 * we do not proceed with the WRITE phase or copy any data. 13136 * Just release the write maps and return with an error. 13137 */ 13138 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13139 orig_bp->b_resid = orig_bp->b_bcount; 13140 bioerror(orig_bp, bp->b_error); 13141 sd_range_unlock(un, bsp->mbs_wmp); 13142 goto freebuf_done; 13143 } 13144 } 13145 13146 /* 13147 * Here is where we set up to copy the data from the shadow buf 13148 * into the space associated with the original buf. 13149 * 13150 * To deal with the conversion between block sizes, these 13151 * computations treat the data as an array of bytes, with the 13152 * first byte (byte 0) corresponding to the first byte in the 13153 * first block on the disk. 13154 */ 13155 13156 /* 13157 * shadow_start and shadow_len indicate the location and size of 13158 * the data returned with the shadow IO request. 13159 */ 13160 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13161 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13162 13163 /* 13164 * copy_offset gives the offset (in bytes) from the start of the first 13165 * block of the READ request to the beginning of the data. We retrieve 13166 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13167 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13168 * data to be copied (in bytes). 13169 */ 13170 copy_offset = bsp->mbs_copy_offset; 13171 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 13172 copy_length = orig_bp->b_bcount; 13173 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13174 13175 /* 13176 * Set up the resid and error fields of orig_bp as appropriate. 13177 */ 13178 if (shadow_end >= request_end) { 13179 /* We got all the requested data; set resid to zero */ 13180 orig_bp->b_resid = 0; 13181 } else { 13182 /* 13183 * We failed to get enough data to fully satisfy the original 13184 * request. Just copy back whatever data we got and set 13185 * up the residual and error code as required. 13186 * 13187 * 'shortfall' is the amount by which the data received with the 13188 * shadow buf has "fallen short" of the requested amount. 13189 */ 13190 shortfall = (size_t)(request_end - shadow_end); 13191 13192 if (shortfall > orig_bp->b_bcount) { 13193 /* 13194 * We did not get enough data to even partially 13195 * fulfill the original request. The residual is 13196 * equal to the amount requested. 13197 */ 13198 orig_bp->b_resid = orig_bp->b_bcount; 13199 } else { 13200 /* 13201 * We did not get all the data that we requested 13202 * from the device, but we will try to return what 13203 * portion we did get. 13204 */ 13205 orig_bp->b_resid = shortfall; 13206 } 13207 ASSERT(copy_length >= orig_bp->b_resid); 13208 copy_length -= orig_bp->b_resid; 13209 } 13210 13211 /* Propagate the error code from the shadow buf to the original buf */ 13212 bioerror(orig_bp, bp->b_error); 13213 13214 if (is_write) { 13215 goto freebuf_done; /* No data copying for a WRITE */ 13216 } 13217 13218 if (has_wmap) { 13219 /* 13220 * This is a READ command from the READ phase of a 13221 * read-modify-write request. We have to copy the data given 13222 * by the user OVER the data returned by the READ command, 13223 * then convert the command from a READ to a WRITE and send 13224 * it back to the target. 13225 */ 13226 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13227 copy_length); 13228 13229 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13230 13231 /* 13232 * Dispatch the WRITE command to the taskq thread, which 13233 * will in turn send the command to the target. When the 13234 * WRITE command completes, we (sd_mapblocksize_iodone()) 13235 * will get called again as part of the iodone chain 13236 * processing for it. Note that we will still be dealing 13237 * with the shadow buf at that point. 13238 */ 13239 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13240 KM_NOSLEEP) != 0) { 13241 /* 13242 * Dispatch was successful so we are done. Return 13243 * without going any higher up the iodone chain. Do 13244 * not free up any layer-private data until after the 13245 * WRITE completes. 13246 */ 13247 return; 13248 } 13249 13250 /* 13251 * Dispatch of the WRITE command failed; set up the error 13252 * condition and send this IO back up the iodone chain. 13253 */ 13254 bioerror(orig_bp, EIO); 13255 orig_bp->b_resid = orig_bp->b_bcount; 13256 13257 } else { 13258 /* 13259 * This is a regular READ request (ie, not a RMW). Copy the 13260 * data from the shadow buf into the original buf. The 13261 * copy_offset compensates for any "misalignment" between the 13262 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13263 * original buf (with its un->un_sys_blocksize blocks). 13264 */ 13265 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13266 copy_length); 13267 } 13268 13269 freebuf_done: 13270 13271 /* 13272 * At this point we still have both the shadow buf AND the original 13273 * buf to deal with, as well as the layer-private data area in each. 13274 * Local variables are as follows: 13275 * 13276 * bp -- points to shadow buf 13277 * xp -- points to xbuf of shadow buf 13278 * bsp -- points to layer-private data area of shadow buf 13279 * orig_bp -- points to original buf 13280 * 13281 * First free the shadow buf and its associated xbuf, then free the 13282 * layer-private data area from the shadow buf. There is no need to 13283 * restore xb_private in the shadow xbuf. 13284 */ 13285 sd_shadow_buf_free(bp); 13286 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13287 13288 /* 13289 * Now update the local variables to point to the original buf, xbuf, 13290 * and layer-private area. 13291 */ 13292 bp = orig_bp; 13293 xp = SD_GET_XBUF(bp); 13294 ASSERT(xp != NULL); 13295 ASSERT(xp == orig_xp); 13296 bsp = xp->xb_private; 13297 ASSERT(bsp != NULL); 13298 13299 done: 13300 /* 13301 * Restore xb_private to whatever it was set to by the next higher 13302 * layer in the chain, then free the layer-private data area. 13303 */ 13304 xp->xb_private = bsp->mbs_oprivate; 13305 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13306 13307 exit: 13308 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13309 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13310 13311 SD_NEXT_IODONE(index, un, bp); 13312 } 13313 13314 13315 /* 13316 * Function: sd_checksum_iostart 13317 * 13318 * Description: A stub function for a layer that's currently not used. 13319 * For now just a placeholder. 13320 * 13321 * Context: Kernel thread context 13322 */ 13323 13324 static void 13325 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13326 { 13327 ASSERT(un != NULL); 13328 ASSERT(bp != NULL); 13329 ASSERT(!mutex_owned(SD_MUTEX(un))); 13330 SD_NEXT_IOSTART(index, un, bp); 13331 } 13332 13333 13334 /* 13335 * Function: sd_checksum_iodone 13336 * 13337 * Description: A stub function for a layer that's currently not used. 13338 * For now just a placeholder. 13339 * 13340 * Context: May be called under interrupt context 13341 */ 13342 13343 static void 13344 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13345 { 13346 ASSERT(un != NULL); 13347 ASSERT(bp != NULL); 13348 ASSERT(!mutex_owned(SD_MUTEX(un))); 13349 SD_NEXT_IODONE(index, un, bp); 13350 } 13351 13352 13353 /* 13354 * Function: sd_checksum_uscsi_iostart 13355 * 13356 * Description: A stub function for a layer that's currently not used. 13357 * For now just a placeholder. 13358 * 13359 * Context: Kernel thread context 13360 */ 13361 13362 static void 13363 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13364 { 13365 ASSERT(un != NULL); 13366 ASSERT(bp != NULL); 13367 ASSERT(!mutex_owned(SD_MUTEX(un))); 13368 SD_NEXT_IOSTART(index, un, bp); 13369 } 13370 13371 13372 /* 13373 * Function: sd_checksum_uscsi_iodone 13374 * 13375 * Description: A stub function for a layer that's currently not used. 13376 * For now just a placeholder. 13377 * 13378 * Context: May be called under interrupt context 13379 */ 13380 13381 static void 13382 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13383 { 13384 ASSERT(un != NULL); 13385 ASSERT(bp != NULL); 13386 ASSERT(!mutex_owned(SD_MUTEX(un))); 13387 SD_NEXT_IODONE(index, un, bp); 13388 } 13389 13390 13391 /* 13392 * Function: sd_pm_iostart 13393 * 13394 * Description: iostart-side routine for Power mangement. 13395 * 13396 * Context: Kernel thread context 13397 */ 13398 13399 static void 13400 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13401 { 13402 ASSERT(un != NULL); 13403 ASSERT(bp != NULL); 13404 ASSERT(!mutex_owned(SD_MUTEX(un))); 13405 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13406 13407 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13408 13409 if (sd_pm_entry(un) != DDI_SUCCESS) { 13410 /* 13411 * Set up to return the failed buf back up the 'iodone' 13412 * side of the calling chain. 13413 */ 13414 bioerror(bp, EIO); 13415 bp->b_resid = bp->b_bcount; 13416 13417 SD_BEGIN_IODONE(index, un, bp); 13418 13419 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13420 return; 13421 } 13422 13423 SD_NEXT_IOSTART(index, un, bp); 13424 13425 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13426 } 13427 13428 13429 /* 13430 * Function: sd_pm_iodone 13431 * 13432 * Description: iodone-side routine for power mangement. 13433 * 13434 * Context: may be called from interrupt context 13435 */ 13436 13437 static void 13438 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13439 { 13440 ASSERT(un != NULL); 13441 ASSERT(bp != NULL); 13442 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13443 13444 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13445 13446 /* 13447 * After attach the following flag is only read, so don't 13448 * take the penalty of acquiring a mutex for it. 13449 */ 13450 if (un->un_f_pm_is_enabled == TRUE) { 13451 sd_pm_exit(un); 13452 } 13453 13454 SD_NEXT_IODONE(index, un, bp); 13455 13456 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13457 } 13458 13459 13460 /* 13461 * Function: sd_core_iostart 13462 * 13463 * Description: Primary driver function for enqueuing buf(9S) structs from 13464 * the system and initiating IO to the target device 13465 * 13466 * Context: Kernel thread context. Can sleep. 13467 * 13468 * Assumptions: - The given xp->xb_blkno is absolute 13469 * (ie, relative to the start of the device). 13470 * - The IO is to be done using the native blocksize of 13471 * the device, as specified in un->un_tgt_blocksize. 13472 */ 13473 /* ARGSUSED */ 13474 static void 13475 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13476 { 13477 struct sd_xbuf *xp; 13478 13479 ASSERT(un != NULL); 13480 ASSERT(bp != NULL); 13481 ASSERT(!mutex_owned(SD_MUTEX(un))); 13482 ASSERT(bp->b_resid == 0); 13483 13484 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13485 13486 xp = SD_GET_XBUF(bp); 13487 ASSERT(xp != NULL); 13488 13489 mutex_enter(SD_MUTEX(un)); 13490 13491 /* 13492 * If we are currently in the failfast state, fail any new IO 13493 * that has B_FAILFAST set, then return. 13494 */ 13495 if ((bp->b_flags & B_FAILFAST) && 13496 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13497 mutex_exit(SD_MUTEX(un)); 13498 bioerror(bp, EIO); 13499 bp->b_resid = bp->b_bcount; 13500 SD_BEGIN_IODONE(index, un, bp); 13501 return; 13502 } 13503 13504 if (SD_IS_DIRECT_PRIORITY(xp)) { 13505 /* 13506 * Priority command -- transport it immediately. 13507 * 13508 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13509 * because all direct priority commands should be associated 13510 * with error recovery actions which we don't want to retry. 13511 */ 13512 sd_start_cmds(un, bp); 13513 } else { 13514 /* 13515 * Normal command -- add it to the wait queue, then start 13516 * transporting commands from the wait queue. 13517 */ 13518 sd_add_buf_to_waitq(un, bp); 13519 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13520 sd_start_cmds(un, NULL); 13521 } 13522 13523 mutex_exit(SD_MUTEX(un)); 13524 13525 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13526 } 13527 13528 13529 /* 13530 * Function: sd_init_cdb_limits 13531 * 13532 * Description: This is to handle scsi_pkt initialization differences 13533 * between the driver platforms. 13534 * 13535 * Legacy behaviors: 13536 * 13537 * If the block number or the sector count exceeds the 13538 * capabilities of a Group 0 command, shift over to a 13539 * Group 1 command. We don't blindly use Group 1 13540 * commands because a) some drives (CDC Wren IVs) get a 13541 * bit confused, and b) there is probably a fair amount 13542 * of speed difference for a target to receive and decode 13543 * a 10 byte command instead of a 6 byte command. 13544 * 13545 * The xfer time difference of 6 vs 10 byte CDBs is 13546 * still significant so this code is still worthwhile. 13547 * 10 byte CDBs are very inefficient with the fas HBA driver 13548 * and older disks. Each CDB byte took 1 usec with some 13549 * popular disks. 13550 * 13551 * Context: Must be called at attach time 13552 */ 13553 13554 static void 13555 sd_init_cdb_limits(struct sd_lun *un) 13556 { 13557 int hba_cdb_limit; 13558 13559 /* 13560 * Use CDB_GROUP1 commands for most devices except for 13561 * parallel SCSI fixed drives in which case we get better 13562 * performance using CDB_GROUP0 commands (where applicable). 13563 */ 13564 un->un_mincdb = SD_CDB_GROUP1; 13565 #if !defined(__fibre) 13566 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13567 !un->un_f_has_removable_media) { 13568 un->un_mincdb = SD_CDB_GROUP0; 13569 } 13570 #endif 13571 13572 /* 13573 * Try to read the max-cdb-length supported by HBA. 13574 */ 13575 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13576 if (0 >= un->un_max_hba_cdb) { 13577 un->un_max_hba_cdb = CDB_GROUP4; 13578 hba_cdb_limit = SD_CDB_GROUP4; 13579 } else if (0 < un->un_max_hba_cdb && 13580 un->un_max_hba_cdb < CDB_GROUP1) { 13581 hba_cdb_limit = SD_CDB_GROUP0; 13582 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13583 un->un_max_hba_cdb < CDB_GROUP5) { 13584 hba_cdb_limit = SD_CDB_GROUP1; 13585 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13586 un->un_max_hba_cdb < CDB_GROUP4) { 13587 hba_cdb_limit = SD_CDB_GROUP5; 13588 } else { 13589 hba_cdb_limit = SD_CDB_GROUP4; 13590 } 13591 13592 /* 13593 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13594 * commands for fixed disks unless we are building for a 32 bit 13595 * kernel. 13596 */ 13597 #ifdef _LP64 13598 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13599 min(hba_cdb_limit, SD_CDB_GROUP4); 13600 #else 13601 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13602 min(hba_cdb_limit, SD_CDB_GROUP1); 13603 #endif 13604 13605 /* 13606 * x86 systems require the PKT_DMA_PARTIAL flag 13607 */ 13608 #if defined(__x86) 13609 un->un_pkt_flags = PKT_DMA_PARTIAL; 13610 #else 13611 un->un_pkt_flags = 0; 13612 #endif 13613 13614 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13615 ? sizeof (struct scsi_arq_status) : 1); 13616 un->un_cmd_timeout = (ushort_t)sd_io_time; 13617 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13618 } 13619 13620 13621 /* 13622 * Function: sd_initpkt_for_buf 13623 * 13624 * Description: Allocate and initialize for transport a scsi_pkt struct, 13625 * based upon the info specified in the given buf struct. 13626 * 13627 * Assumes the xb_blkno in the request is absolute (ie, 13628 * relative to the start of the device (NOT partition!). 13629 * Also assumes that the request is using the native block 13630 * size of the device (as returned by the READ CAPACITY 13631 * command). 13632 * 13633 * Return Code: SD_PKT_ALLOC_SUCCESS 13634 * SD_PKT_ALLOC_FAILURE 13635 * SD_PKT_ALLOC_FAILURE_NO_DMA 13636 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13637 * 13638 * Context: Kernel thread and may be called from software interrupt context 13639 * as part of a sdrunout callback. This function may not block or 13640 * call routines that block 13641 */ 13642 13643 static int 13644 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13645 { 13646 struct sd_xbuf *xp; 13647 struct scsi_pkt *pktp = NULL; 13648 struct sd_lun *un; 13649 size_t blockcount; 13650 daddr_t startblock; 13651 int rval; 13652 int cmd_flags; 13653 13654 ASSERT(bp != NULL); 13655 ASSERT(pktpp != NULL); 13656 xp = SD_GET_XBUF(bp); 13657 ASSERT(xp != NULL); 13658 un = SD_GET_UN(bp); 13659 ASSERT(un != NULL); 13660 ASSERT(mutex_owned(SD_MUTEX(un))); 13661 ASSERT(bp->b_resid == 0); 13662 13663 SD_TRACE(SD_LOG_IO_CORE, un, 13664 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13665 13666 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13667 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13668 /* 13669 * Already have a scsi_pkt -- just need DMA resources. 13670 * We must recompute the CDB in case the mapping returns 13671 * a nonzero pkt_resid. 13672 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13673 * that is being retried, the unmap/remap of the DMA resouces 13674 * will result in the entire transfer starting over again 13675 * from the very first block. 13676 */ 13677 ASSERT(xp->xb_pktp != NULL); 13678 pktp = xp->xb_pktp; 13679 } else { 13680 pktp = NULL; 13681 } 13682 #endif /* __i386 || __amd64 */ 13683 13684 startblock = xp->xb_blkno; /* Absolute block num. */ 13685 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13686 13687 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13688 13689 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13690 13691 #else 13692 13693 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13694 13695 #endif 13696 13697 /* 13698 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13699 * call scsi_init_pkt, and build the CDB. 13700 */ 13701 rval = sd_setup_rw_pkt(un, &pktp, bp, 13702 cmd_flags, sdrunout, (caddr_t)un, 13703 startblock, blockcount); 13704 13705 if (rval == 0) { 13706 /* 13707 * Success. 13708 * 13709 * If partial DMA is being used and required for this transfer. 13710 * set it up here. 13711 */ 13712 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13713 (pktp->pkt_resid != 0)) { 13714 13715 /* 13716 * Save the CDB length and pkt_resid for the 13717 * next xfer 13718 */ 13719 xp->xb_dma_resid = pktp->pkt_resid; 13720 13721 /* rezero resid */ 13722 pktp->pkt_resid = 0; 13723 13724 } else { 13725 xp->xb_dma_resid = 0; 13726 } 13727 13728 pktp->pkt_flags = un->un_tagflags; 13729 pktp->pkt_time = un->un_cmd_timeout; 13730 pktp->pkt_comp = sdintr; 13731 13732 pktp->pkt_private = bp; 13733 *pktpp = pktp; 13734 13735 SD_TRACE(SD_LOG_IO_CORE, un, 13736 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13737 13738 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13739 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13740 #endif 13741 13742 return (SD_PKT_ALLOC_SUCCESS); 13743 13744 } 13745 13746 /* 13747 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13748 * from sd_setup_rw_pkt. 13749 */ 13750 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13751 13752 if (rval == SD_PKT_ALLOC_FAILURE) { 13753 *pktpp = NULL; 13754 /* 13755 * Set the driver state to RWAIT to indicate the driver 13756 * is waiting on resource allocations. The driver will not 13757 * suspend, pm_suspend, or detatch while the state is RWAIT. 13758 */ 13759 New_state(un, SD_STATE_RWAIT); 13760 13761 SD_ERROR(SD_LOG_IO_CORE, un, 13762 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13763 13764 if ((bp->b_flags & B_ERROR) != 0) { 13765 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13766 } 13767 return (SD_PKT_ALLOC_FAILURE); 13768 } else { 13769 /* 13770 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13771 * 13772 * This should never happen. Maybe someone messed with the 13773 * kernel's minphys? 13774 */ 13775 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13776 "Request rejected: too large for CDB: " 13777 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13778 SD_ERROR(SD_LOG_IO_CORE, un, 13779 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13780 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13781 13782 } 13783 } 13784 13785 13786 /* 13787 * Function: sd_destroypkt_for_buf 13788 * 13789 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13790 * 13791 * Context: Kernel thread or interrupt context 13792 */ 13793 13794 static void 13795 sd_destroypkt_for_buf(struct buf *bp) 13796 { 13797 ASSERT(bp != NULL); 13798 ASSERT(SD_GET_UN(bp) != NULL); 13799 13800 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13801 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13802 13803 ASSERT(SD_GET_PKTP(bp) != NULL); 13804 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13805 13806 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13807 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13808 } 13809 13810 /* 13811 * Function: sd_setup_rw_pkt 13812 * 13813 * Description: Determines appropriate CDB group for the requested LBA 13814 * and transfer length, calls scsi_init_pkt, and builds 13815 * the CDB. Do not use for partial DMA transfers except 13816 * for the initial transfer since the CDB size must 13817 * remain constant. 13818 * 13819 * Context: Kernel thread and may be called from software interrupt 13820 * context as part of a sdrunout callback. This function may not 13821 * block or call routines that block 13822 */ 13823 13824 13825 int 13826 sd_setup_rw_pkt(struct sd_lun *un, 13827 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13828 int (*callback)(caddr_t), caddr_t callback_arg, 13829 diskaddr_t lba, uint32_t blockcount) 13830 { 13831 struct scsi_pkt *return_pktp; 13832 union scsi_cdb *cdbp; 13833 struct sd_cdbinfo *cp = NULL; 13834 int i; 13835 13836 /* 13837 * See which size CDB to use, based upon the request. 13838 */ 13839 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13840 13841 /* 13842 * Check lba and block count against sd_cdbtab limits. 13843 * In the partial DMA case, we have to use the same size 13844 * CDB for all the transfers. Check lba + blockcount 13845 * against the max LBA so we know that segment of the 13846 * transfer can use the CDB we select. 13847 */ 13848 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13849 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13850 13851 /* 13852 * The command will fit into the CDB type 13853 * specified by sd_cdbtab[i]. 13854 */ 13855 cp = sd_cdbtab + i; 13856 13857 /* 13858 * Call scsi_init_pkt so we can fill in the 13859 * CDB. 13860 */ 13861 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13862 bp, cp->sc_grpcode, un->un_status_len, 0, 13863 flags, callback, callback_arg); 13864 13865 if (return_pktp != NULL) { 13866 13867 /* 13868 * Return new value of pkt 13869 */ 13870 *pktpp = return_pktp; 13871 13872 /* 13873 * To be safe, zero the CDB insuring there is 13874 * no leftover data from a previous command. 13875 */ 13876 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13877 13878 /* 13879 * Handle partial DMA mapping 13880 */ 13881 if (return_pktp->pkt_resid != 0) { 13882 13883 /* 13884 * Not going to xfer as many blocks as 13885 * originally expected 13886 */ 13887 blockcount -= 13888 SD_BYTES2TGTBLOCKS(un, 13889 return_pktp->pkt_resid); 13890 } 13891 13892 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13893 13894 /* 13895 * Set command byte based on the CDB 13896 * type we matched. 13897 */ 13898 cdbp->scc_cmd = cp->sc_grpmask | 13899 ((bp->b_flags & B_READ) ? 13900 SCMD_READ : SCMD_WRITE); 13901 13902 SD_FILL_SCSI1_LUN(un, return_pktp); 13903 13904 /* 13905 * Fill in LBA and length 13906 */ 13907 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13908 (cp->sc_grpcode == CDB_GROUP4) || 13909 (cp->sc_grpcode == CDB_GROUP0) || 13910 (cp->sc_grpcode == CDB_GROUP5)); 13911 13912 if (cp->sc_grpcode == CDB_GROUP1) { 13913 FORMG1ADDR(cdbp, lba); 13914 FORMG1COUNT(cdbp, blockcount); 13915 return (0); 13916 } else if (cp->sc_grpcode == CDB_GROUP4) { 13917 FORMG4LONGADDR(cdbp, lba); 13918 FORMG4COUNT(cdbp, blockcount); 13919 return (0); 13920 } else if (cp->sc_grpcode == CDB_GROUP0) { 13921 FORMG0ADDR(cdbp, lba); 13922 FORMG0COUNT(cdbp, blockcount); 13923 return (0); 13924 } else if (cp->sc_grpcode == CDB_GROUP5) { 13925 FORMG5ADDR(cdbp, lba); 13926 FORMG5COUNT(cdbp, blockcount); 13927 return (0); 13928 } 13929 13930 /* 13931 * It should be impossible to not match one 13932 * of the CDB types above, so we should never 13933 * reach this point. Set the CDB command byte 13934 * to test-unit-ready to avoid writing 13935 * to somewhere we don't intend. 13936 */ 13937 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13938 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13939 } else { 13940 /* 13941 * Couldn't get scsi_pkt 13942 */ 13943 return (SD_PKT_ALLOC_FAILURE); 13944 } 13945 } 13946 } 13947 13948 /* 13949 * None of the available CDB types were suitable. This really 13950 * should never happen: on a 64 bit system we support 13951 * READ16/WRITE16 which will hold an entire 64 bit disk address 13952 * and on a 32 bit system we will refuse to bind to a device 13953 * larger than 2TB so addresses will never be larger than 32 bits. 13954 */ 13955 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13956 } 13957 13958 #if defined(__i386) || defined(__amd64) 13959 /* 13960 * Function: sd_setup_next_rw_pkt 13961 * 13962 * Description: Setup packet for partial DMA transfers, except for the 13963 * initial transfer. sd_setup_rw_pkt should be used for 13964 * the initial transfer. 13965 * 13966 * Context: Kernel thread and may be called from interrupt context. 13967 */ 13968 13969 int 13970 sd_setup_next_rw_pkt(struct sd_lun *un, 13971 struct scsi_pkt *pktp, struct buf *bp, 13972 diskaddr_t lba, uint32_t blockcount) 13973 { 13974 uchar_t com; 13975 union scsi_cdb *cdbp; 13976 uchar_t cdb_group_id; 13977 13978 ASSERT(pktp != NULL); 13979 ASSERT(pktp->pkt_cdbp != NULL); 13980 13981 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13982 com = cdbp->scc_cmd; 13983 cdb_group_id = CDB_GROUPID(com); 13984 13985 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13986 (cdb_group_id == CDB_GROUPID_1) || 13987 (cdb_group_id == CDB_GROUPID_4) || 13988 (cdb_group_id == CDB_GROUPID_5)); 13989 13990 /* 13991 * Move pkt to the next portion of the xfer. 13992 * func is NULL_FUNC so we do not have to release 13993 * the disk mutex here. 13994 */ 13995 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13996 NULL_FUNC, NULL) == pktp) { 13997 /* Success. Handle partial DMA */ 13998 if (pktp->pkt_resid != 0) { 13999 blockcount -= 14000 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 14001 } 14002 14003 cdbp->scc_cmd = com; 14004 SD_FILL_SCSI1_LUN(un, pktp); 14005 if (cdb_group_id == CDB_GROUPID_1) { 14006 FORMG1ADDR(cdbp, lba); 14007 FORMG1COUNT(cdbp, blockcount); 14008 return (0); 14009 } else if (cdb_group_id == CDB_GROUPID_4) { 14010 FORMG4LONGADDR(cdbp, lba); 14011 FORMG4COUNT(cdbp, blockcount); 14012 return (0); 14013 } else if (cdb_group_id == CDB_GROUPID_0) { 14014 FORMG0ADDR(cdbp, lba); 14015 FORMG0COUNT(cdbp, blockcount); 14016 return (0); 14017 } else if (cdb_group_id == CDB_GROUPID_5) { 14018 FORMG5ADDR(cdbp, lba); 14019 FORMG5COUNT(cdbp, blockcount); 14020 return (0); 14021 } 14022 14023 /* Unreachable */ 14024 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14025 } 14026 14027 /* 14028 * Error setting up next portion of cmd transfer. 14029 * Something is definitely very wrong and this 14030 * should not happen. 14031 */ 14032 return (SD_PKT_ALLOC_FAILURE); 14033 } 14034 #endif /* defined(__i386) || defined(__amd64) */ 14035 14036 /* 14037 * Function: sd_initpkt_for_uscsi 14038 * 14039 * Description: Allocate and initialize for transport a scsi_pkt struct, 14040 * based upon the info specified in the given uscsi_cmd struct. 14041 * 14042 * Return Code: SD_PKT_ALLOC_SUCCESS 14043 * SD_PKT_ALLOC_FAILURE 14044 * SD_PKT_ALLOC_FAILURE_NO_DMA 14045 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14046 * 14047 * Context: Kernel thread and may be called from software interrupt context 14048 * as part of a sdrunout callback. This function may not block or 14049 * call routines that block 14050 */ 14051 14052 static int 14053 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14054 { 14055 struct uscsi_cmd *uscmd; 14056 struct sd_xbuf *xp; 14057 struct scsi_pkt *pktp; 14058 struct sd_lun *un; 14059 uint32_t flags = 0; 14060 14061 ASSERT(bp != NULL); 14062 ASSERT(pktpp != NULL); 14063 xp = SD_GET_XBUF(bp); 14064 ASSERT(xp != NULL); 14065 un = SD_GET_UN(bp); 14066 ASSERT(un != NULL); 14067 ASSERT(mutex_owned(SD_MUTEX(un))); 14068 14069 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14070 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14071 ASSERT(uscmd != NULL); 14072 14073 SD_TRACE(SD_LOG_IO_CORE, un, 14074 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14075 14076 /* 14077 * Allocate the scsi_pkt for the command. 14078 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14079 * during scsi_init_pkt time and will continue to use the 14080 * same path as long as the same scsi_pkt is used without 14081 * intervening scsi_dma_free(). Since uscsi command does 14082 * not call scsi_dmafree() before retry failed command, it 14083 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14084 * set such that scsi_vhci can use other available path for 14085 * retry. Besides, ucsci command does not allow DMA breakup, 14086 * so there is no need to set PKT_DMA_PARTIAL flag. 14087 */ 14088 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14089 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14090 sizeof (struct scsi_arq_status), 0, 14091 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14092 sdrunout, (caddr_t)un); 14093 14094 if (pktp == NULL) { 14095 *pktpp = NULL; 14096 /* 14097 * Set the driver state to RWAIT to indicate the driver 14098 * is waiting on resource allocations. The driver will not 14099 * suspend, pm_suspend, or detatch while the state is RWAIT. 14100 */ 14101 New_state(un, SD_STATE_RWAIT); 14102 14103 SD_ERROR(SD_LOG_IO_CORE, un, 14104 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14105 14106 if ((bp->b_flags & B_ERROR) != 0) { 14107 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14108 } 14109 return (SD_PKT_ALLOC_FAILURE); 14110 } 14111 14112 /* 14113 * We do not do DMA breakup for USCSI commands, so return failure 14114 * here if all the needed DMA resources were not allocated. 14115 */ 14116 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14117 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14118 scsi_destroy_pkt(pktp); 14119 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14120 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14121 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14122 } 14123 14124 /* Init the cdb from the given uscsi struct */ 14125 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14126 uscmd->uscsi_cdb[0], 0, 0, 0); 14127 14128 SD_FILL_SCSI1_LUN(un, pktp); 14129 14130 /* 14131 * Set up the optional USCSI flags. See the uscsi (7I) man page 14132 * for listing of the supported flags. 14133 */ 14134 14135 if (uscmd->uscsi_flags & USCSI_SILENT) { 14136 flags |= FLAG_SILENT; 14137 } 14138 14139 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14140 flags |= FLAG_DIAGNOSE; 14141 } 14142 14143 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14144 flags |= FLAG_ISOLATE; 14145 } 14146 14147 if (un->un_f_is_fibre == FALSE) { 14148 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14149 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14150 } 14151 } 14152 14153 /* 14154 * Set the pkt flags here so we save time later. 14155 * Note: These flags are NOT in the uscsi man page!!! 14156 */ 14157 if (uscmd->uscsi_flags & USCSI_HEAD) { 14158 flags |= FLAG_HEAD; 14159 } 14160 14161 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14162 flags |= FLAG_NOINTR; 14163 } 14164 14165 /* 14166 * For tagged queueing, things get a bit complicated. 14167 * Check first for head of queue and last for ordered queue. 14168 * If neither head nor order, use the default driver tag flags. 14169 */ 14170 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14171 if (uscmd->uscsi_flags & USCSI_HTAG) { 14172 flags |= FLAG_HTAG; 14173 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14174 flags |= FLAG_OTAG; 14175 } else { 14176 flags |= un->un_tagflags & FLAG_TAGMASK; 14177 } 14178 } 14179 14180 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14181 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14182 } 14183 14184 pktp->pkt_flags = flags; 14185 14186 /* Copy the caller's CDB into the pkt... */ 14187 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14188 14189 if (uscmd->uscsi_timeout == 0) { 14190 pktp->pkt_time = un->un_uscsi_timeout; 14191 } else { 14192 pktp->pkt_time = uscmd->uscsi_timeout; 14193 } 14194 14195 /* need it later to identify USCSI request in sdintr */ 14196 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14197 14198 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14199 14200 pktp->pkt_private = bp; 14201 pktp->pkt_comp = sdintr; 14202 *pktpp = pktp; 14203 14204 SD_TRACE(SD_LOG_IO_CORE, un, 14205 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14206 14207 return (SD_PKT_ALLOC_SUCCESS); 14208 } 14209 14210 14211 /* 14212 * Function: sd_destroypkt_for_uscsi 14213 * 14214 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14215 * IOs.. Also saves relevant info into the associated uscsi_cmd 14216 * struct. 14217 * 14218 * Context: May be called under interrupt context 14219 */ 14220 14221 static void 14222 sd_destroypkt_for_uscsi(struct buf *bp) 14223 { 14224 struct uscsi_cmd *uscmd; 14225 struct sd_xbuf *xp; 14226 struct scsi_pkt *pktp; 14227 struct sd_lun *un; 14228 14229 ASSERT(bp != NULL); 14230 xp = SD_GET_XBUF(bp); 14231 ASSERT(xp != NULL); 14232 un = SD_GET_UN(bp); 14233 ASSERT(un != NULL); 14234 ASSERT(!mutex_owned(SD_MUTEX(un))); 14235 pktp = SD_GET_PKTP(bp); 14236 ASSERT(pktp != NULL); 14237 14238 SD_TRACE(SD_LOG_IO_CORE, un, 14239 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14240 14241 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14242 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14243 ASSERT(uscmd != NULL); 14244 14245 /* Save the status and the residual into the uscsi_cmd struct */ 14246 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14247 uscmd->uscsi_resid = bp->b_resid; 14248 14249 /* 14250 * If enabled, copy any saved sense data into the area specified 14251 * by the uscsi command. 14252 */ 14253 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14254 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14255 /* 14256 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14257 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14258 */ 14259 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14260 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14261 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 14262 } 14263 14264 /* We are done with the scsi_pkt; free it now */ 14265 ASSERT(SD_GET_PKTP(bp) != NULL); 14266 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14267 14268 SD_TRACE(SD_LOG_IO_CORE, un, 14269 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14270 } 14271 14272 14273 /* 14274 * Function: sd_bioclone_alloc 14275 * 14276 * Description: Allocate a buf(9S) and init it as per the given buf 14277 * and the various arguments. The associated sd_xbuf 14278 * struct is (nearly) duplicated. The struct buf *bp 14279 * argument is saved in new_xp->xb_private. 14280 * 14281 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14282 * datalen - size of data area for the shadow bp 14283 * blkno - starting LBA 14284 * func - function pointer for b_iodone in the shadow buf. (May 14285 * be NULL if none.) 14286 * 14287 * Return Code: Pointer to allocates buf(9S) struct 14288 * 14289 * Context: Can sleep. 14290 */ 14291 14292 static struct buf * 14293 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14294 daddr_t blkno, int (*func)(struct buf *)) 14295 { 14296 struct sd_lun *un; 14297 struct sd_xbuf *xp; 14298 struct sd_xbuf *new_xp; 14299 struct buf *new_bp; 14300 14301 ASSERT(bp != NULL); 14302 xp = SD_GET_XBUF(bp); 14303 ASSERT(xp != NULL); 14304 un = SD_GET_UN(bp); 14305 ASSERT(un != NULL); 14306 ASSERT(!mutex_owned(SD_MUTEX(un))); 14307 14308 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14309 NULL, KM_SLEEP); 14310 14311 new_bp->b_lblkno = blkno; 14312 14313 /* 14314 * Allocate an xbuf for the shadow bp and copy the contents of the 14315 * original xbuf into it. 14316 */ 14317 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14318 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14319 14320 /* 14321 * The given bp is automatically saved in the xb_private member 14322 * of the new xbuf. Callers are allowed to depend on this. 14323 */ 14324 new_xp->xb_private = bp; 14325 14326 new_bp->b_private = new_xp; 14327 14328 return (new_bp); 14329 } 14330 14331 /* 14332 * Function: sd_shadow_buf_alloc 14333 * 14334 * Description: Allocate a buf(9S) and init it as per the given buf 14335 * and the various arguments. The associated sd_xbuf 14336 * struct is (nearly) duplicated. The struct buf *bp 14337 * argument is saved in new_xp->xb_private. 14338 * 14339 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14340 * datalen - size of data area for the shadow bp 14341 * bflags - B_READ or B_WRITE (pseudo flag) 14342 * blkno - starting LBA 14343 * func - function pointer for b_iodone in the shadow buf. (May 14344 * be NULL if none.) 14345 * 14346 * Return Code: Pointer to allocates buf(9S) struct 14347 * 14348 * Context: Can sleep. 14349 */ 14350 14351 static struct buf * 14352 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14353 daddr_t blkno, int (*func)(struct buf *)) 14354 { 14355 struct sd_lun *un; 14356 struct sd_xbuf *xp; 14357 struct sd_xbuf *new_xp; 14358 struct buf *new_bp; 14359 14360 ASSERT(bp != NULL); 14361 xp = SD_GET_XBUF(bp); 14362 ASSERT(xp != NULL); 14363 un = SD_GET_UN(bp); 14364 ASSERT(un != NULL); 14365 ASSERT(!mutex_owned(SD_MUTEX(un))); 14366 14367 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14368 bp_mapin(bp); 14369 } 14370 14371 bflags &= (B_READ | B_WRITE); 14372 #if defined(__i386) || defined(__amd64) 14373 new_bp = getrbuf(KM_SLEEP); 14374 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14375 new_bp->b_bcount = datalen; 14376 new_bp->b_flags = bflags | 14377 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14378 #else 14379 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14380 datalen, bflags, SLEEP_FUNC, NULL); 14381 #endif 14382 new_bp->av_forw = NULL; 14383 new_bp->av_back = NULL; 14384 new_bp->b_dev = bp->b_dev; 14385 new_bp->b_blkno = blkno; 14386 new_bp->b_iodone = func; 14387 new_bp->b_edev = bp->b_edev; 14388 new_bp->b_resid = 0; 14389 14390 /* We need to preserve the B_FAILFAST flag */ 14391 if (bp->b_flags & B_FAILFAST) { 14392 new_bp->b_flags |= B_FAILFAST; 14393 } 14394 14395 /* 14396 * Allocate an xbuf for the shadow bp and copy the contents of the 14397 * original xbuf into it. 14398 */ 14399 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14400 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14401 14402 /* Need later to copy data between the shadow buf & original buf! */ 14403 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14404 14405 /* 14406 * The given bp is automatically saved in the xb_private member 14407 * of the new xbuf. Callers are allowed to depend on this. 14408 */ 14409 new_xp->xb_private = bp; 14410 14411 new_bp->b_private = new_xp; 14412 14413 return (new_bp); 14414 } 14415 14416 /* 14417 * Function: sd_bioclone_free 14418 * 14419 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14420 * in the larger than partition operation. 14421 * 14422 * Context: May be called under interrupt context 14423 */ 14424 14425 static void 14426 sd_bioclone_free(struct buf *bp) 14427 { 14428 struct sd_xbuf *xp; 14429 14430 ASSERT(bp != NULL); 14431 xp = SD_GET_XBUF(bp); 14432 ASSERT(xp != NULL); 14433 14434 /* 14435 * Call bp_mapout() before freeing the buf, in case a lower 14436 * layer or HBA had done a bp_mapin(). we must do this here 14437 * as we are the "originator" of the shadow buf. 14438 */ 14439 bp_mapout(bp); 14440 14441 /* 14442 * Null out b_iodone before freeing the bp, to ensure that the driver 14443 * never gets confused by a stale value in this field. (Just a little 14444 * extra defensiveness here.) 14445 */ 14446 bp->b_iodone = NULL; 14447 14448 freerbuf(bp); 14449 14450 kmem_free(xp, sizeof (struct sd_xbuf)); 14451 } 14452 14453 /* 14454 * Function: sd_shadow_buf_free 14455 * 14456 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14457 * 14458 * Context: May be called under interrupt context 14459 */ 14460 14461 static void 14462 sd_shadow_buf_free(struct buf *bp) 14463 { 14464 struct sd_xbuf *xp; 14465 14466 ASSERT(bp != NULL); 14467 xp = SD_GET_XBUF(bp); 14468 ASSERT(xp != NULL); 14469 14470 #if defined(__sparc) 14471 /* 14472 * Call bp_mapout() before freeing the buf, in case a lower 14473 * layer or HBA had done a bp_mapin(). we must do this here 14474 * as we are the "originator" of the shadow buf. 14475 */ 14476 bp_mapout(bp); 14477 #endif 14478 14479 /* 14480 * Null out b_iodone before freeing the bp, to ensure that the driver 14481 * never gets confused by a stale value in this field. (Just a little 14482 * extra defensiveness here.) 14483 */ 14484 bp->b_iodone = NULL; 14485 14486 #if defined(__i386) || defined(__amd64) 14487 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14488 freerbuf(bp); 14489 #else 14490 scsi_free_consistent_buf(bp); 14491 #endif 14492 14493 kmem_free(xp, sizeof (struct sd_xbuf)); 14494 } 14495 14496 14497 /* 14498 * Function: sd_print_transport_rejected_message 14499 * 14500 * Description: This implements the ludicrously complex rules for printing 14501 * a "transport rejected" message. This is to address the 14502 * specific problem of having a flood of this error message 14503 * produced when a failover occurs. 14504 * 14505 * Context: Any. 14506 */ 14507 14508 static void 14509 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14510 int code) 14511 { 14512 ASSERT(un != NULL); 14513 ASSERT(mutex_owned(SD_MUTEX(un))); 14514 ASSERT(xp != NULL); 14515 14516 /* 14517 * Print the "transport rejected" message under the following 14518 * conditions: 14519 * 14520 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14521 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14522 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14523 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14524 * scsi_transport(9F) (which indicates that the target might have 14525 * gone off-line). This uses the un->un_tran_fatal_count 14526 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14527 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14528 * from scsi_transport(). 14529 * 14530 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14531 * the preceeding cases in order for the message to be printed. 14532 */ 14533 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14534 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14535 (code != TRAN_FATAL_ERROR) || 14536 (un->un_tran_fatal_count == 1)) { 14537 switch (code) { 14538 case TRAN_BADPKT: 14539 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14540 "transport rejected bad packet\n"); 14541 break; 14542 case TRAN_FATAL_ERROR: 14543 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14544 "transport rejected fatal error\n"); 14545 break; 14546 default: 14547 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14548 "transport rejected (%d)\n", code); 14549 break; 14550 } 14551 } 14552 } 14553 } 14554 14555 14556 /* 14557 * Function: sd_add_buf_to_waitq 14558 * 14559 * Description: Add the given buf(9S) struct to the wait queue for the 14560 * instance. If sorting is enabled, then the buf is added 14561 * to the queue via an elevator sort algorithm (a la 14562 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14563 * If sorting is not enabled, then the buf is just added 14564 * to the end of the wait queue. 14565 * 14566 * Return Code: void 14567 * 14568 * Context: Does not sleep/block, therefore technically can be called 14569 * from any context. However if sorting is enabled then the 14570 * execution time is indeterminate, and may take long if 14571 * the wait queue grows large. 14572 */ 14573 14574 static void 14575 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14576 { 14577 struct buf *ap; 14578 14579 ASSERT(bp != NULL); 14580 ASSERT(un != NULL); 14581 ASSERT(mutex_owned(SD_MUTEX(un))); 14582 14583 /* If the queue is empty, add the buf as the only entry & return. */ 14584 if (un->un_waitq_headp == NULL) { 14585 ASSERT(un->un_waitq_tailp == NULL); 14586 un->un_waitq_headp = un->un_waitq_tailp = bp; 14587 bp->av_forw = NULL; 14588 return; 14589 } 14590 14591 ASSERT(un->un_waitq_tailp != NULL); 14592 14593 /* 14594 * If sorting is disabled, just add the buf to the tail end of 14595 * the wait queue and return. 14596 */ 14597 if (un->un_f_disksort_disabled) { 14598 un->un_waitq_tailp->av_forw = bp; 14599 un->un_waitq_tailp = bp; 14600 bp->av_forw = NULL; 14601 return; 14602 } 14603 14604 /* 14605 * Sort thru the list of requests currently on the wait queue 14606 * and add the new buf request at the appropriate position. 14607 * 14608 * The un->un_waitq_headp is an activity chain pointer on which 14609 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14610 * first queue holds those requests which are positioned after 14611 * the current SD_GET_BLKNO() (in the first request); the second holds 14612 * requests which came in after their SD_GET_BLKNO() number was passed. 14613 * Thus we implement a one way scan, retracting after reaching 14614 * the end of the drive to the first request on the second 14615 * queue, at which time it becomes the first queue. 14616 * A one-way scan is natural because of the way UNIX read-ahead 14617 * blocks are allocated. 14618 * 14619 * If we lie after the first request, then we must locate the 14620 * second request list and add ourselves to it. 14621 */ 14622 ap = un->un_waitq_headp; 14623 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14624 while (ap->av_forw != NULL) { 14625 /* 14626 * Look for an "inversion" in the (normally 14627 * ascending) block numbers. This indicates 14628 * the start of the second request list. 14629 */ 14630 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14631 /* 14632 * Search the second request list for the 14633 * first request at a larger block number. 14634 * We go before that; however if there is 14635 * no such request, we go at the end. 14636 */ 14637 do { 14638 if (SD_GET_BLKNO(bp) < 14639 SD_GET_BLKNO(ap->av_forw)) { 14640 goto insert; 14641 } 14642 ap = ap->av_forw; 14643 } while (ap->av_forw != NULL); 14644 goto insert; /* after last */ 14645 } 14646 ap = ap->av_forw; 14647 } 14648 14649 /* 14650 * No inversions... we will go after the last, and 14651 * be the first request in the second request list. 14652 */ 14653 goto insert; 14654 } 14655 14656 /* 14657 * Request is at/after the current request... 14658 * sort in the first request list. 14659 */ 14660 while (ap->av_forw != NULL) { 14661 /* 14662 * We want to go after the current request (1) if 14663 * there is an inversion after it (i.e. it is the end 14664 * of the first request list), or (2) if the next 14665 * request is a larger block no. than our request. 14666 */ 14667 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14668 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14669 goto insert; 14670 } 14671 ap = ap->av_forw; 14672 } 14673 14674 /* 14675 * Neither a second list nor a larger request, therefore 14676 * we go at the end of the first list (which is the same 14677 * as the end of the whole schebang). 14678 */ 14679 insert: 14680 bp->av_forw = ap->av_forw; 14681 ap->av_forw = bp; 14682 14683 /* 14684 * If we inserted onto the tail end of the waitq, make sure the 14685 * tail pointer is updated. 14686 */ 14687 if (ap == un->un_waitq_tailp) { 14688 un->un_waitq_tailp = bp; 14689 } 14690 } 14691 14692 14693 /* 14694 * Function: sd_start_cmds 14695 * 14696 * Description: Remove and transport cmds from the driver queues. 14697 * 14698 * Arguments: un - pointer to the unit (soft state) struct for the target. 14699 * 14700 * immed_bp - ptr to a buf to be transported immediately. Only 14701 * the immed_bp is transported; bufs on the waitq are not 14702 * processed and the un_retry_bp is not checked. If immed_bp is 14703 * NULL, then normal queue processing is performed. 14704 * 14705 * Context: May be called from kernel thread context, interrupt context, 14706 * or runout callback context. This function may not block or 14707 * call routines that block. 14708 */ 14709 14710 static void 14711 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14712 { 14713 struct sd_xbuf *xp; 14714 struct buf *bp; 14715 void (*statp)(kstat_io_t *); 14716 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14717 void (*saved_statp)(kstat_io_t *); 14718 #endif 14719 int rval; 14720 14721 ASSERT(un != NULL); 14722 ASSERT(mutex_owned(SD_MUTEX(un))); 14723 ASSERT(un->un_ncmds_in_transport >= 0); 14724 ASSERT(un->un_throttle >= 0); 14725 14726 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14727 14728 do { 14729 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14730 saved_statp = NULL; 14731 #endif 14732 14733 /* 14734 * If we are syncing or dumping, fail the command to 14735 * avoid recursively calling back into scsi_transport(). 14736 * The dump I/O itself uses a separate code path so this 14737 * only prevents non-dump I/O from being sent while dumping. 14738 * File system sync takes place before dumping begins. 14739 * During panic, filesystem I/O is allowed provided 14740 * un_in_callback is <= 1. This is to prevent recursion 14741 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14742 * sd_start_cmds and so on. See panic.c for more information 14743 * about the states the system can be in during panic. 14744 */ 14745 if ((un->un_state == SD_STATE_DUMPING) || 14746 (ddi_in_panic() && (un->un_in_callback > 1))) { 14747 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14748 "sd_start_cmds: panicking\n"); 14749 goto exit; 14750 } 14751 14752 if ((bp = immed_bp) != NULL) { 14753 /* 14754 * We have a bp that must be transported immediately. 14755 * It's OK to transport the immed_bp here without doing 14756 * the throttle limit check because the immed_bp is 14757 * always used in a retry/recovery case. This means 14758 * that we know we are not at the throttle limit by 14759 * virtue of the fact that to get here we must have 14760 * already gotten a command back via sdintr(). This also 14761 * relies on (1) the command on un_retry_bp preventing 14762 * further commands from the waitq from being issued; 14763 * and (2) the code in sd_retry_command checking the 14764 * throttle limit before issuing a delayed or immediate 14765 * retry. This holds even if the throttle limit is 14766 * currently ratcheted down from its maximum value. 14767 */ 14768 statp = kstat_runq_enter; 14769 if (bp == un->un_retry_bp) { 14770 ASSERT((un->un_retry_statp == NULL) || 14771 (un->un_retry_statp == kstat_waitq_enter) || 14772 (un->un_retry_statp == 14773 kstat_runq_back_to_waitq)); 14774 /* 14775 * If the waitq kstat was incremented when 14776 * sd_set_retry_bp() queued this bp for a retry, 14777 * then we must set up statp so that the waitq 14778 * count will get decremented correctly below. 14779 * Also we must clear un->un_retry_statp to 14780 * ensure that we do not act on a stale value 14781 * in this field. 14782 */ 14783 if ((un->un_retry_statp == kstat_waitq_enter) || 14784 (un->un_retry_statp == 14785 kstat_runq_back_to_waitq)) { 14786 statp = kstat_waitq_to_runq; 14787 } 14788 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14789 saved_statp = un->un_retry_statp; 14790 #endif 14791 un->un_retry_statp = NULL; 14792 14793 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14794 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14795 "un_throttle:%d un_ncmds_in_transport:%d\n", 14796 un, un->un_retry_bp, un->un_throttle, 14797 un->un_ncmds_in_transport); 14798 } else { 14799 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14800 "processing priority bp:0x%p\n", bp); 14801 } 14802 14803 } else if ((bp = un->un_waitq_headp) != NULL) { 14804 /* 14805 * A command on the waitq is ready to go, but do not 14806 * send it if: 14807 * 14808 * (1) the throttle limit has been reached, or 14809 * (2) a retry is pending, or 14810 * (3) a START_STOP_UNIT callback pending, or 14811 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14812 * command is pending. 14813 * 14814 * For all of these conditions, IO processing will 14815 * restart after the condition is cleared. 14816 */ 14817 if (un->un_ncmds_in_transport >= un->un_throttle) { 14818 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14819 "sd_start_cmds: exiting, " 14820 "throttle limit reached!\n"); 14821 goto exit; 14822 } 14823 if (un->un_retry_bp != NULL) { 14824 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14825 "sd_start_cmds: exiting, retry pending!\n"); 14826 goto exit; 14827 } 14828 if (un->un_startstop_timeid != NULL) { 14829 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14830 "sd_start_cmds: exiting, " 14831 "START_STOP pending!\n"); 14832 goto exit; 14833 } 14834 if (un->un_direct_priority_timeid != NULL) { 14835 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14836 "sd_start_cmds: exiting, " 14837 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14838 goto exit; 14839 } 14840 14841 /* Dequeue the command */ 14842 un->un_waitq_headp = bp->av_forw; 14843 if (un->un_waitq_headp == NULL) { 14844 un->un_waitq_tailp = NULL; 14845 } 14846 bp->av_forw = NULL; 14847 statp = kstat_waitq_to_runq; 14848 SD_TRACE(SD_LOG_IO_CORE, un, 14849 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14850 14851 } else { 14852 /* No work to do so bail out now */ 14853 SD_TRACE(SD_LOG_IO_CORE, un, 14854 "sd_start_cmds: no more work, exiting!\n"); 14855 goto exit; 14856 } 14857 14858 /* 14859 * Reset the state to normal. This is the mechanism by which 14860 * the state transitions from either SD_STATE_RWAIT or 14861 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14862 * If state is SD_STATE_PM_CHANGING then this command is 14863 * part of the device power control and the state must 14864 * not be put back to normal. Doing so would would 14865 * allow new commands to proceed when they shouldn't, 14866 * the device may be going off. 14867 */ 14868 if ((un->un_state != SD_STATE_SUSPENDED) && 14869 (un->un_state != SD_STATE_PM_CHANGING)) { 14870 New_state(un, SD_STATE_NORMAL); 14871 } 14872 14873 xp = SD_GET_XBUF(bp); 14874 ASSERT(xp != NULL); 14875 14876 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14877 /* 14878 * Allocate the scsi_pkt if we need one, or attach DMA 14879 * resources if we have a scsi_pkt that needs them. The 14880 * latter should only occur for commands that are being 14881 * retried. 14882 */ 14883 if ((xp->xb_pktp == NULL) || 14884 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14885 #else 14886 if (xp->xb_pktp == NULL) { 14887 #endif 14888 /* 14889 * There is no scsi_pkt allocated for this buf. Call 14890 * the initpkt function to allocate & init one. 14891 * 14892 * The scsi_init_pkt runout callback functionality is 14893 * implemented as follows: 14894 * 14895 * 1) The initpkt function always calls 14896 * scsi_init_pkt(9F) with sdrunout specified as the 14897 * callback routine. 14898 * 2) A successful packet allocation is initialized and 14899 * the I/O is transported. 14900 * 3) The I/O associated with an allocation resource 14901 * failure is left on its queue to be retried via 14902 * runout or the next I/O. 14903 * 4) The I/O associated with a DMA error is removed 14904 * from the queue and failed with EIO. Processing of 14905 * the transport queues is also halted to be 14906 * restarted via runout or the next I/O. 14907 * 5) The I/O associated with a CDB size or packet 14908 * size error is removed from the queue and failed 14909 * with EIO. Processing of the transport queues is 14910 * continued. 14911 * 14912 * Note: there is no interface for canceling a runout 14913 * callback. To prevent the driver from detaching or 14914 * suspending while a runout is pending the driver 14915 * state is set to SD_STATE_RWAIT 14916 * 14917 * Note: using the scsi_init_pkt callback facility can 14918 * result in an I/O request persisting at the head of 14919 * the list which cannot be satisfied even after 14920 * multiple retries. In the future the driver may 14921 * implement some kind of maximum runout count before 14922 * failing an I/O. 14923 * 14924 * Note: the use of funcp below may seem superfluous, 14925 * but it helps warlock figure out the correct 14926 * initpkt function calls (see [s]sd.wlcmd). 14927 */ 14928 struct scsi_pkt *pktp; 14929 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14930 14931 ASSERT(bp != un->un_rqs_bp); 14932 14933 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14934 switch ((*funcp)(bp, &pktp)) { 14935 case SD_PKT_ALLOC_SUCCESS: 14936 xp->xb_pktp = pktp; 14937 SD_TRACE(SD_LOG_IO_CORE, un, 14938 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14939 pktp); 14940 goto got_pkt; 14941 14942 case SD_PKT_ALLOC_FAILURE: 14943 /* 14944 * Temporary (hopefully) resource depletion. 14945 * Since retries and RQS commands always have a 14946 * scsi_pkt allocated, these cases should never 14947 * get here. So the only cases this needs to 14948 * handle is a bp from the waitq (which we put 14949 * back onto the waitq for sdrunout), or a bp 14950 * sent as an immed_bp (which we just fail). 14951 */ 14952 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14953 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14954 14955 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14956 14957 if (bp == immed_bp) { 14958 /* 14959 * If SD_XB_DMA_FREED is clear, then 14960 * this is a failure to allocate a 14961 * scsi_pkt, and we must fail the 14962 * command. 14963 */ 14964 if ((xp->xb_pkt_flags & 14965 SD_XB_DMA_FREED) == 0) { 14966 break; 14967 } 14968 14969 /* 14970 * If this immediate command is NOT our 14971 * un_retry_bp, then we must fail it. 14972 */ 14973 if (bp != un->un_retry_bp) { 14974 break; 14975 } 14976 14977 /* 14978 * We get here if this cmd is our 14979 * un_retry_bp that was DMAFREED, but 14980 * scsi_init_pkt() failed to reallocate 14981 * DMA resources when we attempted to 14982 * retry it. This can happen when an 14983 * mpxio failover is in progress, but 14984 * we don't want to just fail the 14985 * command in this case. 14986 * 14987 * Use timeout(9F) to restart it after 14988 * a 100ms delay. We don't want to 14989 * let sdrunout() restart it, because 14990 * sdrunout() is just supposed to start 14991 * commands that are sitting on the 14992 * wait queue. The un_retry_bp stays 14993 * set until the command completes, but 14994 * sdrunout can be called many times 14995 * before that happens. Since sdrunout 14996 * cannot tell if the un_retry_bp is 14997 * already in the transport, it could 14998 * end up calling scsi_transport() for 14999 * the un_retry_bp multiple times. 15000 * 15001 * Also: don't schedule the callback 15002 * if some other callback is already 15003 * pending. 15004 */ 15005 if (un->un_retry_statp == NULL) { 15006 /* 15007 * restore the kstat pointer to 15008 * keep kstat counts coherent 15009 * when we do retry the command. 15010 */ 15011 un->un_retry_statp = 15012 saved_statp; 15013 } 15014 15015 if ((un->un_startstop_timeid == NULL) && 15016 (un->un_retry_timeid == NULL) && 15017 (un->un_direct_priority_timeid == 15018 NULL)) { 15019 15020 un->un_retry_timeid = 15021 timeout( 15022 sd_start_retry_command, 15023 un, SD_RESTART_TIMEOUT); 15024 } 15025 goto exit; 15026 } 15027 15028 #else 15029 if (bp == immed_bp) { 15030 break; /* Just fail the command */ 15031 } 15032 #endif 15033 15034 /* Add the buf back to the head of the waitq */ 15035 bp->av_forw = un->un_waitq_headp; 15036 un->un_waitq_headp = bp; 15037 if (un->un_waitq_tailp == NULL) { 15038 un->un_waitq_tailp = bp; 15039 } 15040 goto exit; 15041 15042 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15043 /* 15044 * HBA DMA resource failure. Fail the command 15045 * and continue processing of the queues. 15046 */ 15047 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15048 "sd_start_cmds: " 15049 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15050 break; 15051 15052 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15053 /* 15054 * Note:x86: Partial DMA mapping not supported 15055 * for USCSI commands, and all the needed DMA 15056 * resources were not allocated. 15057 */ 15058 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15059 "sd_start_cmds: " 15060 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15061 break; 15062 15063 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15064 /* 15065 * Note:x86: Request cannot fit into CDB based 15066 * on lba and len. 15067 */ 15068 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15069 "sd_start_cmds: " 15070 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15071 break; 15072 15073 default: 15074 /* Should NEVER get here! */ 15075 panic("scsi_initpkt error"); 15076 /*NOTREACHED*/ 15077 } 15078 15079 /* 15080 * Fatal error in allocating a scsi_pkt for this buf. 15081 * Update kstats & return the buf with an error code. 15082 * We must use sd_return_failed_command_no_restart() to 15083 * avoid a recursive call back into sd_start_cmds(). 15084 * However this also means that we must keep processing 15085 * the waitq here in order to avoid stalling. 15086 */ 15087 if (statp == kstat_waitq_to_runq) { 15088 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15089 } 15090 sd_return_failed_command_no_restart(un, bp, EIO); 15091 if (bp == immed_bp) { 15092 /* immed_bp is gone by now, so clear this */ 15093 immed_bp = NULL; 15094 } 15095 continue; 15096 } 15097 got_pkt: 15098 if (bp == immed_bp) { 15099 /* goto the head of the class.... */ 15100 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15101 } 15102 15103 un->un_ncmds_in_transport++; 15104 SD_UPDATE_KSTATS(un, statp, bp); 15105 15106 /* 15107 * Call scsi_transport() to send the command to the target. 15108 * According to SCSA architecture, we must drop the mutex here 15109 * before calling scsi_transport() in order to avoid deadlock. 15110 * Note that the scsi_pkt's completion routine can be executed 15111 * (from interrupt context) even before the call to 15112 * scsi_transport() returns. 15113 */ 15114 SD_TRACE(SD_LOG_IO_CORE, un, 15115 "sd_start_cmds: calling scsi_transport()\n"); 15116 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15117 15118 mutex_exit(SD_MUTEX(un)); 15119 rval = scsi_transport(xp->xb_pktp); 15120 mutex_enter(SD_MUTEX(un)); 15121 15122 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15123 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15124 15125 switch (rval) { 15126 case TRAN_ACCEPT: 15127 /* Clear this with every pkt accepted by the HBA */ 15128 un->un_tran_fatal_count = 0; 15129 break; /* Success; try the next cmd (if any) */ 15130 15131 case TRAN_BUSY: 15132 un->un_ncmds_in_transport--; 15133 ASSERT(un->un_ncmds_in_transport >= 0); 15134 15135 /* 15136 * Don't retry request sense, the sense data 15137 * is lost when another request is sent. 15138 * Free up the rqs buf and retry 15139 * the original failed cmd. Update kstat. 15140 */ 15141 if (bp == un->un_rqs_bp) { 15142 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15143 bp = sd_mark_rqs_idle(un, xp); 15144 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15145 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 15146 kstat_waitq_enter); 15147 goto exit; 15148 } 15149 15150 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15151 /* 15152 * Free the DMA resources for the scsi_pkt. This will 15153 * allow mpxio to select another path the next time 15154 * we call scsi_transport() with this scsi_pkt. 15155 * See sdintr() for the rationalization behind this. 15156 */ 15157 if ((un->un_f_is_fibre == TRUE) && 15158 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15159 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15160 scsi_dmafree(xp->xb_pktp); 15161 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15162 } 15163 #endif 15164 15165 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15166 /* 15167 * Commands that are SD_PATH_DIRECT_PRIORITY 15168 * are for error recovery situations. These do 15169 * not use the normal command waitq, so if they 15170 * get a TRAN_BUSY we cannot put them back onto 15171 * the waitq for later retry. One possible 15172 * problem is that there could already be some 15173 * other command on un_retry_bp that is waiting 15174 * for this one to complete, so we would be 15175 * deadlocked if we put this command back onto 15176 * the waitq for later retry (since un_retry_bp 15177 * must complete before the driver gets back to 15178 * commands on the waitq). 15179 * 15180 * To avoid deadlock we must schedule a callback 15181 * that will restart this command after a set 15182 * interval. This should keep retrying for as 15183 * long as the underlying transport keeps 15184 * returning TRAN_BUSY (just like for other 15185 * commands). Use the same timeout interval as 15186 * for the ordinary TRAN_BUSY retry. 15187 */ 15188 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15189 "sd_start_cmds: scsi_transport() returned " 15190 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15191 15192 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15193 un->un_direct_priority_timeid = 15194 timeout(sd_start_direct_priority_command, 15195 bp, SD_BSY_TIMEOUT / 500); 15196 15197 goto exit; 15198 } 15199 15200 /* 15201 * For TRAN_BUSY, we want to reduce the throttle value, 15202 * unless we are retrying a command. 15203 */ 15204 if (bp != un->un_retry_bp) { 15205 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15206 } 15207 15208 /* 15209 * Set up the bp to be tried again 10 ms later. 15210 * Note:x86: Is there a timeout value in the sd_lun 15211 * for this condition? 15212 */ 15213 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 15214 kstat_runq_back_to_waitq); 15215 goto exit; 15216 15217 case TRAN_FATAL_ERROR: 15218 un->un_tran_fatal_count++; 15219 /* FALLTHRU */ 15220 15221 case TRAN_BADPKT: 15222 default: 15223 un->un_ncmds_in_transport--; 15224 ASSERT(un->un_ncmds_in_transport >= 0); 15225 15226 /* 15227 * If this is our REQUEST SENSE command with a 15228 * transport error, we must get back the pointers 15229 * to the original buf, and mark the REQUEST 15230 * SENSE command as "available". 15231 */ 15232 if (bp == un->un_rqs_bp) { 15233 bp = sd_mark_rqs_idle(un, xp); 15234 xp = SD_GET_XBUF(bp); 15235 } else { 15236 /* 15237 * Legacy behavior: do not update transport 15238 * error count for request sense commands. 15239 */ 15240 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15241 } 15242 15243 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15244 sd_print_transport_rejected_message(un, xp, rval); 15245 15246 /* 15247 * We must use sd_return_failed_command_no_restart() to 15248 * avoid a recursive call back into sd_start_cmds(). 15249 * However this also means that we must keep processing 15250 * the waitq here in order to avoid stalling. 15251 */ 15252 sd_return_failed_command_no_restart(un, bp, EIO); 15253 15254 /* 15255 * Notify any threads waiting in sd_ddi_suspend() that 15256 * a command completion has occurred. 15257 */ 15258 if (un->un_state == SD_STATE_SUSPENDED) { 15259 cv_broadcast(&un->un_disk_busy_cv); 15260 } 15261 15262 if (bp == immed_bp) { 15263 /* immed_bp is gone by now, so clear this */ 15264 immed_bp = NULL; 15265 } 15266 break; 15267 } 15268 15269 } while (immed_bp == NULL); 15270 15271 exit: 15272 ASSERT(mutex_owned(SD_MUTEX(un))); 15273 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15274 } 15275 15276 15277 /* 15278 * Function: sd_return_command 15279 * 15280 * Description: Returns a command to its originator (with or without an 15281 * error). Also starts commands waiting to be transported 15282 * to the target. 15283 * 15284 * Context: May be called from interrupt, kernel, or timeout context 15285 */ 15286 15287 static void 15288 sd_return_command(struct sd_lun *un, struct buf *bp) 15289 { 15290 struct sd_xbuf *xp; 15291 #if defined(__i386) || defined(__amd64) 15292 struct scsi_pkt *pktp; 15293 #endif 15294 15295 ASSERT(bp != NULL); 15296 ASSERT(un != NULL); 15297 ASSERT(mutex_owned(SD_MUTEX(un))); 15298 ASSERT(bp != un->un_rqs_bp); 15299 xp = SD_GET_XBUF(bp); 15300 ASSERT(xp != NULL); 15301 15302 #if defined(__i386) || defined(__amd64) 15303 pktp = SD_GET_PKTP(bp); 15304 #endif 15305 15306 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15307 15308 #if defined(__i386) || defined(__amd64) 15309 /* 15310 * Note:x86: check for the "sdrestart failed" case. 15311 */ 15312 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15313 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15314 (xp->xb_pktp->pkt_resid == 0)) { 15315 15316 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15317 /* 15318 * Successfully set up next portion of cmd 15319 * transfer, try sending it 15320 */ 15321 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15322 NULL, NULL, 0, (clock_t)0, NULL); 15323 sd_start_cmds(un, NULL); 15324 return; /* Note:x86: need a return here? */ 15325 } 15326 } 15327 #endif 15328 15329 /* 15330 * If this is the failfast bp, clear it from un_failfast_bp. This 15331 * can happen if upon being re-tried the failfast bp either 15332 * succeeded or encountered another error (possibly even a different 15333 * error than the one that precipitated the failfast state, but in 15334 * that case it would have had to exhaust retries as well). Regardless, 15335 * this should not occur whenever the instance is in the active 15336 * failfast state. 15337 */ 15338 if (bp == un->un_failfast_bp) { 15339 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15340 un->un_failfast_bp = NULL; 15341 } 15342 15343 /* 15344 * Clear the failfast state upon successful completion of ANY cmd. 15345 */ 15346 if (bp->b_error == 0) { 15347 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15348 } 15349 15350 /* 15351 * This is used if the command was retried one or more times. Show that 15352 * we are done with it, and allow processing of the waitq to resume. 15353 */ 15354 if (bp == un->un_retry_bp) { 15355 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15356 "sd_return_command: un:0x%p: " 15357 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15358 un->un_retry_bp = NULL; 15359 un->un_retry_statp = NULL; 15360 } 15361 15362 SD_UPDATE_RDWR_STATS(un, bp); 15363 SD_UPDATE_PARTITION_STATS(un, bp); 15364 15365 switch (un->un_state) { 15366 case SD_STATE_SUSPENDED: 15367 /* 15368 * Notify any threads waiting in sd_ddi_suspend() that 15369 * a command completion has occurred. 15370 */ 15371 cv_broadcast(&un->un_disk_busy_cv); 15372 break; 15373 default: 15374 sd_start_cmds(un, NULL); 15375 break; 15376 } 15377 15378 /* Return this command up the iodone chain to its originator. */ 15379 mutex_exit(SD_MUTEX(un)); 15380 15381 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15382 xp->xb_pktp = NULL; 15383 15384 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15385 15386 ASSERT(!mutex_owned(SD_MUTEX(un))); 15387 mutex_enter(SD_MUTEX(un)); 15388 15389 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15390 } 15391 15392 15393 /* 15394 * Function: sd_return_failed_command 15395 * 15396 * Description: Command completion when an error occurred. 15397 * 15398 * Context: May be called from interrupt context 15399 */ 15400 15401 static void 15402 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15403 { 15404 ASSERT(bp != NULL); 15405 ASSERT(un != NULL); 15406 ASSERT(mutex_owned(SD_MUTEX(un))); 15407 15408 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15409 "sd_return_failed_command: entry\n"); 15410 15411 /* 15412 * b_resid could already be nonzero due to a partial data 15413 * transfer, so do not change it here. 15414 */ 15415 SD_BIOERROR(bp, errcode); 15416 15417 sd_return_command(un, bp); 15418 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15419 "sd_return_failed_command: exit\n"); 15420 } 15421 15422 15423 /* 15424 * Function: sd_return_failed_command_no_restart 15425 * 15426 * Description: Same as sd_return_failed_command, but ensures that no 15427 * call back into sd_start_cmds will be issued. 15428 * 15429 * Context: May be called from interrupt context 15430 */ 15431 15432 static void 15433 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15434 int errcode) 15435 { 15436 struct sd_xbuf *xp; 15437 15438 ASSERT(bp != NULL); 15439 ASSERT(un != NULL); 15440 ASSERT(mutex_owned(SD_MUTEX(un))); 15441 xp = SD_GET_XBUF(bp); 15442 ASSERT(xp != NULL); 15443 ASSERT(errcode != 0); 15444 15445 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15446 "sd_return_failed_command_no_restart: entry\n"); 15447 15448 /* 15449 * b_resid could already be nonzero due to a partial data 15450 * transfer, so do not change it here. 15451 */ 15452 SD_BIOERROR(bp, errcode); 15453 15454 /* 15455 * If this is the failfast bp, clear it. This can happen if the 15456 * failfast bp encounterd a fatal error when we attempted to 15457 * re-try it (such as a scsi_transport(9F) failure). However 15458 * we should NOT be in an active failfast state if the failfast 15459 * bp is not NULL. 15460 */ 15461 if (bp == un->un_failfast_bp) { 15462 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15463 un->un_failfast_bp = NULL; 15464 } 15465 15466 if (bp == un->un_retry_bp) { 15467 /* 15468 * This command was retried one or more times. Show that we are 15469 * done with it, and allow processing of the waitq to resume. 15470 */ 15471 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15472 "sd_return_failed_command_no_restart: " 15473 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15474 un->un_retry_bp = NULL; 15475 un->un_retry_statp = NULL; 15476 } 15477 15478 SD_UPDATE_RDWR_STATS(un, bp); 15479 SD_UPDATE_PARTITION_STATS(un, bp); 15480 15481 mutex_exit(SD_MUTEX(un)); 15482 15483 if (xp->xb_pktp != NULL) { 15484 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15485 xp->xb_pktp = NULL; 15486 } 15487 15488 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15489 15490 mutex_enter(SD_MUTEX(un)); 15491 15492 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15493 "sd_return_failed_command_no_restart: exit\n"); 15494 } 15495 15496 15497 /* 15498 * Function: sd_retry_command 15499 * 15500 * Description: queue up a command for retry, or (optionally) fail it 15501 * if retry counts are exhausted. 15502 * 15503 * Arguments: un - Pointer to the sd_lun struct for the target. 15504 * 15505 * bp - Pointer to the buf for the command to be retried. 15506 * 15507 * retry_check_flag - Flag to see which (if any) of the retry 15508 * counts should be decremented/checked. If the indicated 15509 * retry count is exhausted, then the command will not be 15510 * retried; it will be failed instead. This should use a 15511 * value equal to one of the following: 15512 * 15513 * SD_RETRIES_NOCHECK 15514 * SD_RESD_RETRIES_STANDARD 15515 * SD_RETRIES_VICTIM 15516 * 15517 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15518 * if the check should be made to see of FLAG_ISOLATE is set 15519 * in the pkt. If FLAG_ISOLATE is set, then the command is 15520 * not retried, it is simply failed. 15521 * 15522 * user_funcp - Ptr to function to call before dispatching the 15523 * command. May be NULL if no action needs to be performed. 15524 * (Primarily intended for printing messages.) 15525 * 15526 * user_arg - Optional argument to be passed along to 15527 * the user_funcp call. 15528 * 15529 * failure_code - errno return code to set in the bp if the 15530 * command is going to be failed. 15531 * 15532 * retry_delay - Retry delay interval in (clock_t) units. May 15533 * be zero which indicates that the retry should be retried 15534 * immediately (ie, without an intervening delay). 15535 * 15536 * statp - Ptr to kstat function to be updated if the command 15537 * is queued for a delayed retry. May be NULL if no kstat 15538 * update is desired. 15539 * 15540 * Context: May be called from interupt context. 15541 */ 15542 15543 static void 15544 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15545 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15546 code), void *user_arg, int failure_code, clock_t retry_delay, 15547 void (*statp)(kstat_io_t *)) 15548 { 15549 struct sd_xbuf *xp; 15550 struct scsi_pkt *pktp; 15551 15552 ASSERT(un != NULL); 15553 ASSERT(mutex_owned(SD_MUTEX(un))); 15554 ASSERT(bp != NULL); 15555 xp = SD_GET_XBUF(bp); 15556 ASSERT(xp != NULL); 15557 pktp = SD_GET_PKTP(bp); 15558 ASSERT(pktp != NULL); 15559 15560 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15561 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15562 15563 /* 15564 * If we are syncing or dumping, fail the command to avoid 15565 * recursively calling back into scsi_transport(). 15566 */ 15567 if (ddi_in_panic()) { 15568 goto fail_command_no_log; 15569 } 15570 15571 /* 15572 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15573 * log an error and fail the command. 15574 */ 15575 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15576 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15577 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15578 sd_dump_memory(un, SD_LOG_IO, "CDB", 15579 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15580 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15581 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15582 goto fail_command; 15583 } 15584 15585 /* 15586 * If we are suspended, then put the command onto head of the 15587 * wait queue since we don't want to start more commands. 15588 */ 15589 switch (un->un_state) { 15590 case SD_STATE_SUSPENDED: 15591 case SD_STATE_DUMPING: 15592 bp->av_forw = un->un_waitq_headp; 15593 un->un_waitq_headp = bp; 15594 if (un->un_waitq_tailp == NULL) { 15595 un->un_waitq_tailp = bp; 15596 } 15597 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15598 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15599 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15600 return; 15601 default: 15602 break; 15603 } 15604 15605 /* 15606 * If the caller wants us to check FLAG_ISOLATE, then see if that 15607 * is set; if it is then we do not want to retry the command. 15608 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15609 */ 15610 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15611 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15612 goto fail_command; 15613 } 15614 } 15615 15616 15617 /* 15618 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15619 * command timeout or a selection timeout has occurred. This means 15620 * that we were unable to establish an kind of communication with 15621 * the target, and subsequent retries and/or commands are likely 15622 * to encounter similar results and take a long time to complete. 15623 * 15624 * If this is a failfast error condition, we need to update the 15625 * failfast state, even if this bp does not have B_FAILFAST set. 15626 */ 15627 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15628 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15629 ASSERT(un->un_failfast_bp == NULL); 15630 /* 15631 * If we are already in the active failfast state, and 15632 * another failfast error condition has been detected, 15633 * then fail this command if it has B_FAILFAST set. 15634 * If B_FAILFAST is clear, then maintain the legacy 15635 * behavior of retrying heroically, even tho this will 15636 * take a lot more time to fail the command. 15637 */ 15638 if (bp->b_flags & B_FAILFAST) { 15639 goto fail_command; 15640 } 15641 } else { 15642 /* 15643 * We're not in the active failfast state, but we 15644 * have a failfast error condition, so we must begin 15645 * transition to the next state. We do this regardless 15646 * of whether or not this bp has B_FAILFAST set. 15647 */ 15648 if (un->un_failfast_bp == NULL) { 15649 /* 15650 * This is the first bp to meet a failfast 15651 * condition so save it on un_failfast_bp & 15652 * do normal retry processing. Do not enter 15653 * active failfast state yet. This marks 15654 * entry into the "failfast pending" state. 15655 */ 15656 un->un_failfast_bp = bp; 15657 15658 } else if (un->un_failfast_bp == bp) { 15659 /* 15660 * This is the second time *this* bp has 15661 * encountered a failfast error condition, 15662 * so enter active failfast state & flush 15663 * queues as appropriate. 15664 */ 15665 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15666 un->un_failfast_bp = NULL; 15667 sd_failfast_flushq(un); 15668 15669 /* 15670 * Fail this bp now if B_FAILFAST set; 15671 * otherwise continue with retries. (It would 15672 * be pretty ironic if this bp succeeded on a 15673 * subsequent retry after we just flushed all 15674 * the queues). 15675 */ 15676 if (bp->b_flags & B_FAILFAST) { 15677 goto fail_command; 15678 } 15679 15680 #if !defined(lint) && !defined(__lint) 15681 } else { 15682 /* 15683 * If neither of the preceeding conditionals 15684 * was true, it means that there is some 15685 * *other* bp that has met an inital failfast 15686 * condition and is currently either being 15687 * retried or is waiting to be retried. In 15688 * that case we should perform normal retry 15689 * processing on *this* bp, since there is a 15690 * chance that the current failfast condition 15691 * is transient and recoverable. If that does 15692 * not turn out to be the case, then retries 15693 * will be cleared when the wait queue is 15694 * flushed anyway. 15695 */ 15696 #endif 15697 } 15698 } 15699 } else { 15700 /* 15701 * SD_RETRIES_FAILFAST is clear, which indicates that we 15702 * likely were able to at least establish some level of 15703 * communication with the target and subsequent commands 15704 * and/or retries are likely to get through to the target, 15705 * In this case we want to be aggressive about clearing 15706 * the failfast state. Note that this does not affect 15707 * the "failfast pending" condition. 15708 */ 15709 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15710 } 15711 15712 15713 /* 15714 * Check the specified retry count to see if we can still do 15715 * any retries with this pkt before we should fail it. 15716 */ 15717 switch (retry_check_flag & SD_RETRIES_MASK) { 15718 case SD_RETRIES_VICTIM: 15719 /* 15720 * Check the victim retry count. If exhausted, then fall 15721 * thru & check against the standard retry count. 15722 */ 15723 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15724 /* Increment count & proceed with the retry */ 15725 xp->xb_victim_retry_count++; 15726 break; 15727 } 15728 /* Victim retries exhausted, fall back to std. retries... */ 15729 /* FALLTHRU */ 15730 15731 case SD_RETRIES_STANDARD: 15732 if (xp->xb_retry_count >= un->un_retry_count) { 15733 /* Retries exhausted, fail the command */ 15734 SD_TRACE(SD_LOG_IO_CORE, un, 15735 "sd_retry_command: retries exhausted!\n"); 15736 /* 15737 * update b_resid for failed SCMD_READ & SCMD_WRITE 15738 * commands with nonzero pkt_resid. 15739 */ 15740 if ((pktp->pkt_reason == CMD_CMPLT) && 15741 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15742 (pktp->pkt_resid != 0)) { 15743 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15744 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15745 SD_UPDATE_B_RESID(bp, pktp); 15746 } 15747 } 15748 goto fail_command; 15749 } 15750 xp->xb_retry_count++; 15751 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15752 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15753 break; 15754 15755 case SD_RETRIES_UA: 15756 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15757 /* Retries exhausted, fail the command */ 15758 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15759 "Unit Attention retries exhausted. " 15760 "Check the target.\n"); 15761 goto fail_command; 15762 } 15763 xp->xb_ua_retry_count++; 15764 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15765 "sd_retry_command: retry count:%d\n", 15766 xp->xb_ua_retry_count); 15767 break; 15768 15769 case SD_RETRIES_BUSY: 15770 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15771 /* Retries exhausted, fail the command */ 15772 SD_TRACE(SD_LOG_IO_CORE, un, 15773 "sd_retry_command: retries exhausted!\n"); 15774 goto fail_command; 15775 } 15776 xp->xb_retry_count++; 15777 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15778 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15779 break; 15780 15781 case SD_RETRIES_NOCHECK: 15782 default: 15783 /* No retry count to check. Just proceed with the retry */ 15784 break; 15785 } 15786 15787 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15788 15789 /* 15790 * If we were given a zero timeout, we must attempt to retry the 15791 * command immediately (ie, without a delay). 15792 */ 15793 if (retry_delay == 0) { 15794 /* 15795 * Check some limiting conditions to see if we can actually 15796 * do the immediate retry. If we cannot, then we must 15797 * fall back to queueing up a delayed retry. 15798 */ 15799 if (un->un_ncmds_in_transport >= un->un_throttle) { 15800 /* 15801 * We are at the throttle limit for the target, 15802 * fall back to delayed retry. 15803 */ 15804 retry_delay = SD_BSY_TIMEOUT; 15805 statp = kstat_waitq_enter; 15806 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15807 "sd_retry_command: immed. retry hit " 15808 "throttle!\n"); 15809 } else { 15810 /* 15811 * We're clear to proceed with the immediate retry. 15812 * First call the user-provided function (if any) 15813 */ 15814 if (user_funcp != NULL) { 15815 (*user_funcp)(un, bp, user_arg, 15816 SD_IMMEDIATE_RETRY_ISSUED); 15817 #ifdef __lock_lint 15818 sd_print_incomplete_msg(un, bp, user_arg, 15819 SD_IMMEDIATE_RETRY_ISSUED); 15820 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15821 SD_IMMEDIATE_RETRY_ISSUED); 15822 sd_print_sense_failed_msg(un, bp, user_arg, 15823 SD_IMMEDIATE_RETRY_ISSUED); 15824 #endif 15825 } 15826 15827 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15828 "sd_retry_command: issuing immediate retry\n"); 15829 15830 /* 15831 * Call sd_start_cmds() to transport the command to 15832 * the target. 15833 */ 15834 sd_start_cmds(un, bp); 15835 15836 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15837 "sd_retry_command exit\n"); 15838 return; 15839 } 15840 } 15841 15842 /* 15843 * Set up to retry the command after a delay. 15844 * First call the user-provided function (if any) 15845 */ 15846 if (user_funcp != NULL) { 15847 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15848 } 15849 15850 sd_set_retry_bp(un, bp, retry_delay, statp); 15851 15852 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15853 return; 15854 15855 fail_command: 15856 15857 if (user_funcp != NULL) { 15858 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15859 } 15860 15861 fail_command_no_log: 15862 15863 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15864 "sd_retry_command: returning failed command\n"); 15865 15866 sd_return_failed_command(un, bp, failure_code); 15867 15868 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15869 } 15870 15871 15872 /* 15873 * Function: sd_set_retry_bp 15874 * 15875 * Description: Set up the given bp for retry. 15876 * 15877 * Arguments: un - ptr to associated softstate 15878 * bp - ptr to buf(9S) for the command 15879 * retry_delay - time interval before issuing retry (may be 0) 15880 * statp - optional pointer to kstat function 15881 * 15882 * Context: May be called under interrupt context 15883 */ 15884 15885 static void 15886 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15887 void (*statp)(kstat_io_t *)) 15888 { 15889 ASSERT(un != NULL); 15890 ASSERT(mutex_owned(SD_MUTEX(un))); 15891 ASSERT(bp != NULL); 15892 15893 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15894 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15895 15896 /* 15897 * Indicate that the command is being retried. This will not allow any 15898 * other commands on the wait queue to be transported to the target 15899 * until this command has been completed (success or failure). The 15900 * "retry command" is not transported to the target until the given 15901 * time delay expires, unless the user specified a 0 retry_delay. 15902 * 15903 * Note: the timeout(9F) callback routine is what actually calls 15904 * sd_start_cmds() to transport the command, with the exception of a 15905 * zero retry_delay. The only current implementor of a zero retry delay 15906 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15907 */ 15908 if (un->un_retry_bp == NULL) { 15909 ASSERT(un->un_retry_statp == NULL); 15910 un->un_retry_bp = bp; 15911 15912 /* 15913 * If the user has not specified a delay the command should 15914 * be queued and no timeout should be scheduled. 15915 */ 15916 if (retry_delay == 0) { 15917 /* 15918 * Save the kstat pointer that will be used in the 15919 * call to SD_UPDATE_KSTATS() below, so that 15920 * sd_start_cmds() can correctly decrement the waitq 15921 * count when it is time to transport this command. 15922 */ 15923 un->un_retry_statp = statp; 15924 goto done; 15925 } 15926 } 15927 15928 if (un->un_retry_bp == bp) { 15929 /* 15930 * Save the kstat pointer that will be used in the call to 15931 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15932 * correctly decrement the waitq count when it is time to 15933 * transport this command. 15934 */ 15935 un->un_retry_statp = statp; 15936 15937 /* 15938 * Schedule a timeout if: 15939 * 1) The user has specified a delay. 15940 * 2) There is not a START_STOP_UNIT callback pending. 15941 * 15942 * If no delay has been specified, then it is up to the caller 15943 * to ensure that IO processing continues without stalling. 15944 * Effectively, this means that the caller will issue the 15945 * required call to sd_start_cmds(). The START_STOP_UNIT 15946 * callback does this after the START STOP UNIT command has 15947 * completed. In either of these cases we should not schedule 15948 * a timeout callback here. Also don't schedule the timeout if 15949 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15950 */ 15951 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15952 (un->un_direct_priority_timeid == NULL)) { 15953 un->un_retry_timeid = 15954 timeout(sd_start_retry_command, un, retry_delay); 15955 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15956 "sd_set_retry_bp: setting timeout: un: 0x%p" 15957 " bp:0x%p un_retry_timeid:0x%p\n", 15958 un, bp, un->un_retry_timeid); 15959 } 15960 } else { 15961 /* 15962 * We only get in here if there is already another command 15963 * waiting to be retried. In this case, we just put the 15964 * given command onto the wait queue, so it can be transported 15965 * after the current retry command has completed. 15966 * 15967 * Also we have to make sure that if the command at the head 15968 * of the wait queue is the un_failfast_bp, that we do not 15969 * put ahead of it any other commands that are to be retried. 15970 */ 15971 if ((un->un_failfast_bp != NULL) && 15972 (un->un_failfast_bp == un->un_waitq_headp)) { 15973 /* 15974 * Enqueue this command AFTER the first command on 15975 * the wait queue (which is also un_failfast_bp). 15976 */ 15977 bp->av_forw = un->un_waitq_headp->av_forw; 15978 un->un_waitq_headp->av_forw = bp; 15979 if (un->un_waitq_headp == un->un_waitq_tailp) { 15980 un->un_waitq_tailp = bp; 15981 } 15982 } else { 15983 /* Enqueue this command at the head of the waitq. */ 15984 bp->av_forw = un->un_waitq_headp; 15985 un->un_waitq_headp = bp; 15986 if (un->un_waitq_tailp == NULL) { 15987 un->un_waitq_tailp = bp; 15988 } 15989 } 15990 15991 if (statp == NULL) { 15992 statp = kstat_waitq_enter; 15993 } 15994 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15995 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15996 } 15997 15998 done: 15999 if (statp != NULL) { 16000 SD_UPDATE_KSTATS(un, statp, bp); 16001 } 16002 16003 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16004 "sd_set_retry_bp: exit un:0x%p\n", un); 16005 } 16006 16007 16008 /* 16009 * Function: sd_start_retry_command 16010 * 16011 * Description: Start the command that has been waiting on the target's 16012 * retry queue. Called from timeout(9F) context after the 16013 * retry delay interval has expired. 16014 * 16015 * Arguments: arg - pointer to associated softstate for the device. 16016 * 16017 * Context: timeout(9F) thread context. May not sleep. 16018 */ 16019 16020 static void 16021 sd_start_retry_command(void *arg) 16022 { 16023 struct sd_lun *un = arg; 16024 16025 ASSERT(un != NULL); 16026 ASSERT(!mutex_owned(SD_MUTEX(un))); 16027 16028 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16029 "sd_start_retry_command: entry\n"); 16030 16031 mutex_enter(SD_MUTEX(un)); 16032 16033 un->un_retry_timeid = NULL; 16034 16035 if (un->un_retry_bp != NULL) { 16036 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16037 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16038 un, un->un_retry_bp); 16039 sd_start_cmds(un, un->un_retry_bp); 16040 } 16041 16042 mutex_exit(SD_MUTEX(un)); 16043 16044 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16045 "sd_start_retry_command: exit\n"); 16046 } 16047 16048 16049 /* 16050 * Function: sd_start_direct_priority_command 16051 * 16052 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16053 * received TRAN_BUSY when we called scsi_transport() to send it 16054 * to the underlying HBA. This function is called from timeout(9F) 16055 * context after the delay interval has expired. 16056 * 16057 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16058 * 16059 * Context: timeout(9F) thread context. May not sleep. 16060 */ 16061 16062 static void 16063 sd_start_direct_priority_command(void *arg) 16064 { 16065 struct buf *priority_bp = arg; 16066 struct sd_lun *un; 16067 16068 ASSERT(priority_bp != NULL); 16069 un = SD_GET_UN(priority_bp); 16070 ASSERT(un != NULL); 16071 ASSERT(!mutex_owned(SD_MUTEX(un))); 16072 16073 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16074 "sd_start_direct_priority_command: entry\n"); 16075 16076 mutex_enter(SD_MUTEX(un)); 16077 un->un_direct_priority_timeid = NULL; 16078 sd_start_cmds(un, priority_bp); 16079 mutex_exit(SD_MUTEX(un)); 16080 16081 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16082 "sd_start_direct_priority_command: exit\n"); 16083 } 16084 16085 16086 /* 16087 * Function: sd_send_request_sense_command 16088 * 16089 * Description: Sends a REQUEST SENSE command to the target 16090 * 16091 * Context: May be called from interrupt context. 16092 */ 16093 16094 static void 16095 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16096 struct scsi_pkt *pktp) 16097 { 16098 ASSERT(bp != NULL); 16099 ASSERT(un != NULL); 16100 ASSERT(mutex_owned(SD_MUTEX(un))); 16101 16102 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16103 "entry: buf:0x%p\n", bp); 16104 16105 /* 16106 * If we are syncing or dumping, then fail the command to avoid a 16107 * recursive callback into scsi_transport(). Also fail the command 16108 * if we are suspended (legacy behavior). 16109 */ 16110 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16111 (un->un_state == SD_STATE_DUMPING)) { 16112 sd_return_failed_command(un, bp, EIO); 16113 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16114 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16115 return; 16116 } 16117 16118 /* 16119 * Retry the failed command and don't issue the request sense if: 16120 * 1) the sense buf is busy 16121 * 2) we have 1 or more outstanding commands on the target 16122 * (the sense data will be cleared or invalidated any way) 16123 * 16124 * Note: There could be an issue with not checking a retry limit here, 16125 * the problem is determining which retry limit to check. 16126 */ 16127 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16128 /* Don't retry if the command is flagged as non-retryable */ 16129 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16130 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16131 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 16132 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16133 "sd_send_request_sense_command: " 16134 "at full throttle, retrying exit\n"); 16135 } else { 16136 sd_return_failed_command(un, bp, EIO); 16137 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16138 "sd_send_request_sense_command: " 16139 "at full throttle, non-retryable exit\n"); 16140 } 16141 return; 16142 } 16143 16144 sd_mark_rqs_busy(un, bp); 16145 sd_start_cmds(un, un->un_rqs_bp); 16146 16147 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16148 "sd_send_request_sense_command: exit\n"); 16149 } 16150 16151 16152 /* 16153 * Function: sd_mark_rqs_busy 16154 * 16155 * Description: Indicate that the request sense bp for this instance is 16156 * in use. 16157 * 16158 * Context: May be called under interrupt context 16159 */ 16160 16161 static void 16162 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16163 { 16164 struct sd_xbuf *sense_xp; 16165 16166 ASSERT(un != NULL); 16167 ASSERT(bp != NULL); 16168 ASSERT(mutex_owned(SD_MUTEX(un))); 16169 ASSERT(un->un_sense_isbusy == 0); 16170 16171 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16172 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16173 16174 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16175 ASSERT(sense_xp != NULL); 16176 16177 SD_INFO(SD_LOG_IO, un, 16178 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16179 16180 ASSERT(sense_xp->xb_pktp != NULL); 16181 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16182 == (FLAG_SENSING | FLAG_HEAD)); 16183 16184 un->un_sense_isbusy = 1; 16185 un->un_rqs_bp->b_resid = 0; 16186 sense_xp->xb_pktp->pkt_resid = 0; 16187 sense_xp->xb_pktp->pkt_reason = 0; 16188 16189 /* So we can get back the bp at interrupt time! */ 16190 sense_xp->xb_sense_bp = bp; 16191 16192 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16193 16194 /* 16195 * Mark this buf as awaiting sense data. (This is already set in 16196 * the pkt_flags for the RQS packet.) 16197 */ 16198 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16199 16200 sense_xp->xb_retry_count = 0; 16201 sense_xp->xb_victim_retry_count = 0; 16202 sense_xp->xb_ua_retry_count = 0; 16203 sense_xp->xb_dma_resid = 0; 16204 16205 /* Clean up the fields for auto-request sense */ 16206 sense_xp->xb_sense_status = 0; 16207 sense_xp->xb_sense_state = 0; 16208 sense_xp->xb_sense_resid = 0; 16209 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16210 16211 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16212 } 16213 16214 16215 /* 16216 * Function: sd_mark_rqs_idle 16217 * 16218 * Description: SD_MUTEX must be held continuously through this routine 16219 * to prevent reuse of the rqs struct before the caller can 16220 * complete it's processing. 16221 * 16222 * Return Code: Pointer to the RQS buf 16223 * 16224 * Context: May be called under interrupt context 16225 */ 16226 16227 static struct buf * 16228 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16229 { 16230 struct buf *bp; 16231 ASSERT(un != NULL); 16232 ASSERT(sense_xp != NULL); 16233 ASSERT(mutex_owned(SD_MUTEX(un))); 16234 ASSERT(un->un_sense_isbusy != 0); 16235 16236 un->un_sense_isbusy = 0; 16237 bp = sense_xp->xb_sense_bp; 16238 sense_xp->xb_sense_bp = NULL; 16239 16240 /* This pkt is no longer interested in getting sense data */ 16241 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16242 16243 return (bp); 16244 } 16245 16246 16247 16248 /* 16249 * Function: sd_alloc_rqs 16250 * 16251 * Description: Set up the unit to receive auto request sense data 16252 * 16253 * Return Code: DDI_SUCCESS or DDI_FAILURE 16254 * 16255 * Context: Called under attach(9E) context 16256 */ 16257 16258 static int 16259 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16260 { 16261 struct sd_xbuf *xp; 16262 16263 ASSERT(un != NULL); 16264 ASSERT(!mutex_owned(SD_MUTEX(un))); 16265 ASSERT(un->un_rqs_bp == NULL); 16266 ASSERT(un->un_rqs_pktp == NULL); 16267 16268 /* 16269 * First allocate the required buf and scsi_pkt structs, then set up 16270 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16271 */ 16272 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16273 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16274 if (un->un_rqs_bp == NULL) { 16275 return (DDI_FAILURE); 16276 } 16277 16278 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16279 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16280 16281 if (un->un_rqs_pktp == NULL) { 16282 sd_free_rqs(un); 16283 return (DDI_FAILURE); 16284 } 16285 16286 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16287 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16288 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 16289 16290 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16291 16292 /* Set up the other needed members in the ARQ scsi_pkt. */ 16293 un->un_rqs_pktp->pkt_comp = sdintr; 16294 un->un_rqs_pktp->pkt_time = sd_io_time; 16295 un->un_rqs_pktp->pkt_flags |= 16296 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16297 16298 /* 16299 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16300 * provide any intpkt, destroypkt routines as we take care of 16301 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16302 */ 16303 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16304 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16305 xp->xb_pktp = un->un_rqs_pktp; 16306 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16307 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16308 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16309 16310 /* 16311 * Save the pointer to the request sense private bp so it can 16312 * be retrieved in sdintr. 16313 */ 16314 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16315 ASSERT(un->un_rqs_bp->b_private == xp); 16316 16317 /* 16318 * See if the HBA supports auto-request sense for the specified 16319 * target/lun. If it does, then try to enable it (if not already 16320 * enabled). 16321 * 16322 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16323 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16324 * return success. However, in both of these cases ARQ is always 16325 * enabled and scsi_ifgetcap will always return true. The best approach 16326 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16327 * 16328 * The 3rd case is the HBA (adp) always return enabled on 16329 * scsi_ifgetgetcap even when it's not enable, the best approach 16330 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16331 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16332 */ 16333 16334 if (un->un_f_is_fibre == TRUE) { 16335 un->un_f_arq_enabled = TRUE; 16336 } else { 16337 #if defined(__i386) || defined(__amd64) 16338 /* 16339 * Circumvent the Adaptec bug, remove this code when 16340 * the bug is fixed 16341 */ 16342 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16343 #endif 16344 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16345 case 0: 16346 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16347 "sd_alloc_rqs: HBA supports ARQ\n"); 16348 /* 16349 * ARQ is supported by this HBA but currently is not 16350 * enabled. Attempt to enable it and if successful then 16351 * mark this instance as ARQ enabled. 16352 */ 16353 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16354 == 1) { 16355 /* Successfully enabled ARQ in the HBA */ 16356 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16357 "sd_alloc_rqs: ARQ enabled\n"); 16358 un->un_f_arq_enabled = TRUE; 16359 } else { 16360 /* Could not enable ARQ in the HBA */ 16361 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16362 "sd_alloc_rqs: failed ARQ enable\n"); 16363 un->un_f_arq_enabled = FALSE; 16364 } 16365 break; 16366 case 1: 16367 /* 16368 * ARQ is supported by this HBA and is already enabled. 16369 * Just mark ARQ as enabled for this instance. 16370 */ 16371 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16372 "sd_alloc_rqs: ARQ already enabled\n"); 16373 un->un_f_arq_enabled = TRUE; 16374 break; 16375 default: 16376 /* 16377 * ARQ is not supported by this HBA; disable it for this 16378 * instance. 16379 */ 16380 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16381 "sd_alloc_rqs: HBA does not support ARQ\n"); 16382 un->un_f_arq_enabled = FALSE; 16383 break; 16384 } 16385 } 16386 16387 return (DDI_SUCCESS); 16388 } 16389 16390 16391 /* 16392 * Function: sd_free_rqs 16393 * 16394 * Description: Cleanup for the pre-instance RQS command. 16395 * 16396 * Context: Kernel thread context 16397 */ 16398 16399 static void 16400 sd_free_rqs(struct sd_lun *un) 16401 { 16402 ASSERT(un != NULL); 16403 16404 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16405 16406 /* 16407 * If consistent memory is bound to a scsi_pkt, the pkt 16408 * has to be destroyed *before* freeing the consistent memory. 16409 * Don't change the sequence of this operations. 16410 * scsi_destroy_pkt() might access memory, which isn't allowed, 16411 * after it was freed in scsi_free_consistent_buf(). 16412 */ 16413 if (un->un_rqs_pktp != NULL) { 16414 scsi_destroy_pkt(un->un_rqs_pktp); 16415 un->un_rqs_pktp = NULL; 16416 } 16417 16418 if (un->un_rqs_bp != NULL) { 16419 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 16420 scsi_free_consistent_buf(un->un_rqs_bp); 16421 un->un_rqs_bp = NULL; 16422 } 16423 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16424 } 16425 16426 16427 16428 /* 16429 * Function: sd_reduce_throttle 16430 * 16431 * Description: Reduces the maximun # of outstanding commands on a 16432 * target to the current number of outstanding commands. 16433 * Queues a tiemout(9F) callback to restore the limit 16434 * after a specified interval has elapsed. 16435 * Typically used when we get a TRAN_BUSY return code 16436 * back from scsi_transport(). 16437 * 16438 * Arguments: un - ptr to the sd_lun softstate struct 16439 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16440 * 16441 * Context: May be called from interrupt context 16442 */ 16443 16444 static void 16445 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16446 { 16447 ASSERT(un != NULL); 16448 ASSERT(mutex_owned(SD_MUTEX(un))); 16449 ASSERT(un->un_ncmds_in_transport >= 0); 16450 16451 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16452 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16453 un, un->un_throttle, un->un_ncmds_in_transport); 16454 16455 if (un->un_throttle > 1) { 16456 if (un->un_f_use_adaptive_throttle == TRUE) { 16457 switch (throttle_type) { 16458 case SD_THROTTLE_TRAN_BUSY: 16459 if (un->un_busy_throttle == 0) { 16460 un->un_busy_throttle = un->un_throttle; 16461 } 16462 break; 16463 case SD_THROTTLE_QFULL: 16464 un->un_busy_throttle = 0; 16465 break; 16466 default: 16467 ASSERT(FALSE); 16468 } 16469 16470 if (un->un_ncmds_in_transport > 0) { 16471 un->un_throttle = un->un_ncmds_in_transport; 16472 } 16473 16474 } else { 16475 if (un->un_ncmds_in_transport == 0) { 16476 un->un_throttle = 1; 16477 } else { 16478 un->un_throttle = un->un_ncmds_in_transport; 16479 } 16480 } 16481 } 16482 16483 /* Reschedule the timeout if none is currently active */ 16484 if (un->un_reset_throttle_timeid == NULL) { 16485 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16486 un, SD_THROTTLE_RESET_INTERVAL); 16487 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16488 "sd_reduce_throttle: timeout scheduled!\n"); 16489 } 16490 16491 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16492 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16493 } 16494 16495 16496 16497 /* 16498 * Function: sd_restore_throttle 16499 * 16500 * Description: Callback function for timeout(9F). Resets the current 16501 * value of un->un_throttle to its default. 16502 * 16503 * Arguments: arg - pointer to associated softstate for the device. 16504 * 16505 * Context: May be called from interrupt context 16506 */ 16507 16508 static void 16509 sd_restore_throttle(void *arg) 16510 { 16511 struct sd_lun *un = arg; 16512 16513 ASSERT(un != NULL); 16514 ASSERT(!mutex_owned(SD_MUTEX(un))); 16515 16516 mutex_enter(SD_MUTEX(un)); 16517 16518 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16519 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16520 16521 un->un_reset_throttle_timeid = NULL; 16522 16523 if (un->un_f_use_adaptive_throttle == TRUE) { 16524 /* 16525 * If un_busy_throttle is nonzero, then it contains the 16526 * value that un_throttle was when we got a TRAN_BUSY back 16527 * from scsi_transport(). We want to revert back to this 16528 * value. 16529 * 16530 * In the QFULL case, the throttle limit will incrementally 16531 * increase until it reaches max throttle. 16532 */ 16533 if (un->un_busy_throttle > 0) { 16534 un->un_throttle = un->un_busy_throttle; 16535 un->un_busy_throttle = 0; 16536 } else { 16537 /* 16538 * increase throttle by 10% open gate slowly, schedule 16539 * another restore if saved throttle has not been 16540 * reached 16541 */ 16542 short throttle; 16543 if (sd_qfull_throttle_enable) { 16544 throttle = un->un_throttle + 16545 max((un->un_throttle / 10), 1); 16546 un->un_throttle = 16547 (throttle < un->un_saved_throttle) ? 16548 throttle : un->un_saved_throttle; 16549 if (un->un_throttle < un->un_saved_throttle) { 16550 un->un_reset_throttle_timeid = 16551 timeout(sd_restore_throttle, 16552 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16553 } 16554 } 16555 } 16556 16557 /* 16558 * If un_throttle has fallen below the low-water mark, we 16559 * restore the maximum value here (and allow it to ratchet 16560 * down again if necessary). 16561 */ 16562 if (un->un_throttle < un->un_min_throttle) { 16563 un->un_throttle = un->un_saved_throttle; 16564 } 16565 } else { 16566 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16567 "restoring limit from 0x%x to 0x%x\n", 16568 un->un_throttle, un->un_saved_throttle); 16569 un->un_throttle = un->un_saved_throttle; 16570 } 16571 16572 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16573 "sd_restore_throttle: calling sd_start_cmds!\n"); 16574 16575 sd_start_cmds(un, NULL); 16576 16577 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16578 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16579 un, un->un_throttle); 16580 16581 mutex_exit(SD_MUTEX(un)); 16582 16583 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16584 } 16585 16586 /* 16587 * Function: sdrunout 16588 * 16589 * Description: Callback routine for scsi_init_pkt when a resource allocation 16590 * fails. 16591 * 16592 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16593 * soft state instance. 16594 * 16595 * Return Code: The scsi_init_pkt routine allows for the callback function to 16596 * return a 0 indicating the callback should be rescheduled or a 1 16597 * indicating not to reschedule. This routine always returns 1 16598 * because the driver always provides a callback function to 16599 * scsi_init_pkt. This results in a callback always being scheduled 16600 * (via the scsi_init_pkt callback implementation) if a resource 16601 * failure occurs. 16602 * 16603 * Context: This callback function may not block or call routines that block 16604 * 16605 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16606 * request persisting at the head of the list which cannot be 16607 * satisfied even after multiple retries. In the future the driver 16608 * may implement some time of maximum runout count before failing 16609 * an I/O. 16610 */ 16611 16612 static int 16613 sdrunout(caddr_t arg) 16614 { 16615 struct sd_lun *un = (struct sd_lun *)arg; 16616 16617 ASSERT(un != NULL); 16618 ASSERT(!mutex_owned(SD_MUTEX(un))); 16619 16620 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16621 16622 mutex_enter(SD_MUTEX(un)); 16623 sd_start_cmds(un, NULL); 16624 mutex_exit(SD_MUTEX(un)); 16625 /* 16626 * This callback routine always returns 1 (i.e. do not reschedule) 16627 * because we always specify sdrunout as the callback handler for 16628 * scsi_init_pkt inside the call to sd_start_cmds. 16629 */ 16630 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16631 return (1); 16632 } 16633 16634 16635 /* 16636 * Function: sdintr 16637 * 16638 * Description: Completion callback routine for scsi_pkt(9S) structs 16639 * sent to the HBA driver via scsi_transport(9F). 16640 * 16641 * Context: Interrupt context 16642 */ 16643 16644 static void 16645 sdintr(struct scsi_pkt *pktp) 16646 { 16647 struct buf *bp; 16648 struct sd_xbuf *xp; 16649 struct sd_lun *un; 16650 16651 ASSERT(pktp != NULL); 16652 bp = (struct buf *)pktp->pkt_private; 16653 ASSERT(bp != NULL); 16654 xp = SD_GET_XBUF(bp); 16655 ASSERT(xp != NULL); 16656 ASSERT(xp->xb_pktp != NULL); 16657 un = SD_GET_UN(bp); 16658 ASSERT(un != NULL); 16659 ASSERT(!mutex_owned(SD_MUTEX(un))); 16660 16661 #ifdef SD_FAULT_INJECTION 16662 16663 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16664 /* SD FaultInjection */ 16665 sd_faultinjection(pktp); 16666 16667 #endif /* SD_FAULT_INJECTION */ 16668 16669 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16670 " xp:0x%p, un:0x%p\n", bp, xp, un); 16671 16672 mutex_enter(SD_MUTEX(un)); 16673 16674 /* Reduce the count of the #commands currently in transport */ 16675 un->un_ncmds_in_transport--; 16676 ASSERT(un->un_ncmds_in_transport >= 0); 16677 16678 /* Increment counter to indicate that the callback routine is active */ 16679 un->un_in_callback++; 16680 16681 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16682 16683 #ifdef SDDEBUG 16684 if (bp == un->un_retry_bp) { 16685 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16686 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16687 un, un->un_retry_bp, un->un_ncmds_in_transport); 16688 } 16689 #endif 16690 16691 /* 16692 * If pkt_reason is CMD_DEV_GONE, just fail the command 16693 */ 16694 if (pktp->pkt_reason == CMD_DEV_GONE) { 16695 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16696 "Device is gone\n"); 16697 sd_return_failed_command(un, bp, EIO); 16698 goto exit; 16699 } 16700 16701 /* 16702 * First see if the pkt has auto-request sense data with it.... 16703 * Look at the packet state first so we don't take a performance 16704 * hit looking at the arq enabled flag unless absolutely necessary. 16705 */ 16706 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16707 (un->un_f_arq_enabled == TRUE)) { 16708 /* 16709 * The HBA did an auto request sense for this command so check 16710 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16711 * driver command that should not be retried. 16712 */ 16713 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16714 /* 16715 * Save the relevant sense info into the xp for the 16716 * original cmd. 16717 */ 16718 struct scsi_arq_status *asp; 16719 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16720 xp->xb_sense_status = 16721 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16722 xp->xb_sense_state = asp->sts_rqpkt_state; 16723 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16724 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16725 min(sizeof (struct scsi_extended_sense), 16726 SENSE_LENGTH)); 16727 16728 /* fail the command */ 16729 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16730 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16731 sd_return_failed_command(un, bp, EIO); 16732 goto exit; 16733 } 16734 16735 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16736 /* 16737 * We want to either retry or fail this command, so free 16738 * the DMA resources here. If we retry the command then 16739 * the DMA resources will be reallocated in sd_start_cmds(). 16740 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16741 * causes the *entire* transfer to start over again from the 16742 * beginning of the request, even for PARTIAL chunks that 16743 * have already transferred successfully. 16744 */ 16745 if ((un->un_f_is_fibre == TRUE) && 16746 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16747 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16748 scsi_dmafree(pktp); 16749 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16750 } 16751 #endif 16752 16753 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16754 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16755 16756 sd_handle_auto_request_sense(un, bp, xp, pktp); 16757 goto exit; 16758 } 16759 16760 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16761 if (pktp->pkt_flags & FLAG_SENSING) { 16762 /* This pktp is from the unit's REQUEST_SENSE command */ 16763 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16764 "sdintr: sd_handle_request_sense\n"); 16765 sd_handle_request_sense(un, bp, xp, pktp); 16766 goto exit; 16767 } 16768 16769 /* 16770 * Check to see if the command successfully completed as requested; 16771 * this is the most common case (and also the hot performance path). 16772 * 16773 * Requirements for successful completion are: 16774 * pkt_reason is CMD_CMPLT and packet status is status good. 16775 * In addition: 16776 * - A residual of zero indicates successful completion no matter what 16777 * the command is. 16778 * - If the residual is not zero and the command is not a read or 16779 * write, then it's still defined as successful completion. In other 16780 * words, if the command is a read or write the residual must be 16781 * zero for successful completion. 16782 * - If the residual is not zero and the command is a read or 16783 * write, and it's a USCSICMD, then it's still defined as 16784 * successful completion. 16785 */ 16786 if ((pktp->pkt_reason == CMD_CMPLT) && 16787 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16788 16789 /* 16790 * Since this command is returned with a good status, we 16791 * can reset the count for Sonoma failover. 16792 */ 16793 un->un_sonoma_failure_count = 0; 16794 16795 /* 16796 * Return all USCSI commands on good status 16797 */ 16798 if (pktp->pkt_resid == 0) { 16799 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16800 "sdintr: returning command for resid == 0\n"); 16801 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16802 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16803 SD_UPDATE_B_RESID(bp, pktp); 16804 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16805 "sdintr: returning command for resid != 0\n"); 16806 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16807 SD_UPDATE_B_RESID(bp, pktp); 16808 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16809 "sdintr: returning uscsi command\n"); 16810 } else { 16811 goto not_successful; 16812 } 16813 sd_return_command(un, bp); 16814 16815 /* 16816 * Decrement counter to indicate that the callback routine 16817 * is done. 16818 */ 16819 un->un_in_callback--; 16820 ASSERT(un->un_in_callback >= 0); 16821 mutex_exit(SD_MUTEX(un)); 16822 16823 return; 16824 } 16825 16826 not_successful: 16827 16828 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16829 /* 16830 * The following is based upon knowledge of the underlying transport 16831 * and its use of DMA resources. This code should be removed when 16832 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16833 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16834 * and sd_start_cmds(). 16835 * 16836 * Free any DMA resources associated with this command if there 16837 * is a chance it could be retried or enqueued for later retry. 16838 * If we keep the DMA binding then mpxio cannot reissue the 16839 * command on another path whenever a path failure occurs. 16840 * 16841 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16842 * causes the *entire* transfer to start over again from the 16843 * beginning of the request, even for PARTIAL chunks that 16844 * have already transferred successfully. 16845 * 16846 * This is only done for non-uscsi commands (and also skipped for the 16847 * driver's internal RQS command). Also just do this for Fibre Channel 16848 * devices as these are the only ones that support mpxio. 16849 */ 16850 if ((un->un_f_is_fibre == TRUE) && 16851 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16852 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16853 scsi_dmafree(pktp); 16854 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16855 } 16856 #endif 16857 16858 /* 16859 * The command did not successfully complete as requested so check 16860 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16861 * driver command that should not be retried so just return. If 16862 * FLAG_DIAGNOSE is not set the error will be processed below. 16863 */ 16864 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16865 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16866 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16867 /* 16868 * Issue a request sense if a check condition caused the error 16869 * (we handle the auto request sense case above), otherwise 16870 * just fail the command. 16871 */ 16872 if ((pktp->pkt_reason == CMD_CMPLT) && 16873 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16874 sd_send_request_sense_command(un, bp, pktp); 16875 } else { 16876 sd_return_failed_command(un, bp, EIO); 16877 } 16878 goto exit; 16879 } 16880 16881 /* 16882 * The command did not successfully complete as requested so process 16883 * the error, retry, and/or attempt recovery. 16884 */ 16885 switch (pktp->pkt_reason) { 16886 case CMD_CMPLT: 16887 switch (SD_GET_PKT_STATUS(pktp)) { 16888 case STATUS_GOOD: 16889 /* 16890 * The command completed successfully with a non-zero 16891 * residual 16892 */ 16893 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16894 "sdintr: STATUS_GOOD \n"); 16895 sd_pkt_status_good(un, bp, xp, pktp); 16896 break; 16897 16898 case STATUS_CHECK: 16899 case STATUS_TERMINATED: 16900 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16901 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16902 sd_pkt_status_check_condition(un, bp, xp, pktp); 16903 break; 16904 16905 case STATUS_BUSY: 16906 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16907 "sdintr: STATUS_BUSY\n"); 16908 sd_pkt_status_busy(un, bp, xp, pktp); 16909 break; 16910 16911 case STATUS_RESERVATION_CONFLICT: 16912 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16913 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16914 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16915 break; 16916 16917 case STATUS_QFULL: 16918 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16919 "sdintr: STATUS_QFULL\n"); 16920 sd_pkt_status_qfull(un, bp, xp, pktp); 16921 break; 16922 16923 case STATUS_MET: 16924 case STATUS_INTERMEDIATE: 16925 case STATUS_SCSI2: 16926 case STATUS_INTERMEDIATE_MET: 16927 case STATUS_ACA_ACTIVE: 16928 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16929 "Unexpected SCSI status received: 0x%x\n", 16930 SD_GET_PKT_STATUS(pktp)); 16931 sd_return_failed_command(un, bp, EIO); 16932 break; 16933 16934 default: 16935 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16936 "Invalid SCSI status received: 0x%x\n", 16937 SD_GET_PKT_STATUS(pktp)); 16938 sd_return_failed_command(un, bp, EIO); 16939 break; 16940 16941 } 16942 break; 16943 16944 case CMD_INCOMPLETE: 16945 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16946 "sdintr: CMD_INCOMPLETE\n"); 16947 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16948 break; 16949 case CMD_TRAN_ERR: 16950 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16951 "sdintr: CMD_TRAN_ERR\n"); 16952 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16953 break; 16954 case CMD_RESET: 16955 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16956 "sdintr: CMD_RESET \n"); 16957 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16958 break; 16959 case CMD_ABORTED: 16960 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16961 "sdintr: CMD_ABORTED \n"); 16962 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16963 break; 16964 case CMD_TIMEOUT: 16965 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16966 "sdintr: CMD_TIMEOUT\n"); 16967 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16968 break; 16969 case CMD_UNX_BUS_FREE: 16970 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16971 "sdintr: CMD_UNX_BUS_FREE \n"); 16972 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16973 break; 16974 case CMD_TAG_REJECT: 16975 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16976 "sdintr: CMD_TAG_REJECT\n"); 16977 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16978 break; 16979 default: 16980 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16981 "sdintr: default\n"); 16982 sd_pkt_reason_default(un, bp, xp, pktp); 16983 break; 16984 } 16985 16986 exit: 16987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16988 16989 /* Decrement counter to indicate that the callback routine is done. */ 16990 un->un_in_callback--; 16991 ASSERT(un->un_in_callback >= 0); 16992 16993 /* 16994 * At this point, the pkt has been dispatched, ie, it is either 16995 * being re-tried or has been returned to its caller and should 16996 * not be referenced. 16997 */ 16998 16999 mutex_exit(SD_MUTEX(un)); 17000 } 17001 17002 17003 /* 17004 * Function: sd_print_incomplete_msg 17005 * 17006 * Description: Prints the error message for a CMD_INCOMPLETE error. 17007 * 17008 * Arguments: un - ptr to associated softstate for the device. 17009 * bp - ptr to the buf(9S) for the command. 17010 * arg - message string ptr 17011 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17012 * or SD_NO_RETRY_ISSUED. 17013 * 17014 * Context: May be called under interrupt context 17015 */ 17016 17017 static void 17018 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17019 { 17020 struct scsi_pkt *pktp; 17021 char *msgp; 17022 char *cmdp = arg; 17023 17024 ASSERT(un != NULL); 17025 ASSERT(mutex_owned(SD_MUTEX(un))); 17026 ASSERT(bp != NULL); 17027 ASSERT(arg != NULL); 17028 pktp = SD_GET_PKTP(bp); 17029 ASSERT(pktp != NULL); 17030 17031 switch (code) { 17032 case SD_DELAYED_RETRY_ISSUED: 17033 case SD_IMMEDIATE_RETRY_ISSUED: 17034 msgp = "retrying"; 17035 break; 17036 case SD_NO_RETRY_ISSUED: 17037 default: 17038 msgp = "giving up"; 17039 break; 17040 } 17041 17042 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17043 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17044 "incomplete %s- %s\n", cmdp, msgp); 17045 } 17046 } 17047 17048 17049 17050 /* 17051 * Function: sd_pkt_status_good 17052 * 17053 * Description: Processing for a STATUS_GOOD code in pkt_status. 17054 * 17055 * Context: May be called under interrupt context 17056 */ 17057 17058 static void 17059 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17060 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17061 { 17062 char *cmdp; 17063 17064 ASSERT(un != NULL); 17065 ASSERT(mutex_owned(SD_MUTEX(un))); 17066 ASSERT(bp != NULL); 17067 ASSERT(xp != NULL); 17068 ASSERT(pktp != NULL); 17069 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17070 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17071 ASSERT(pktp->pkt_resid != 0); 17072 17073 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17074 17075 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17076 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17077 case SCMD_READ: 17078 cmdp = "read"; 17079 break; 17080 case SCMD_WRITE: 17081 cmdp = "write"; 17082 break; 17083 default: 17084 SD_UPDATE_B_RESID(bp, pktp); 17085 sd_return_command(un, bp); 17086 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17087 return; 17088 } 17089 17090 /* 17091 * See if we can retry the read/write, preferrably immediately. 17092 * If retries are exhaused, then sd_retry_command() will update 17093 * the b_resid count. 17094 */ 17095 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17096 cmdp, EIO, (clock_t)0, NULL); 17097 17098 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17099 } 17100 17101 17102 17103 17104 17105 /* 17106 * Function: sd_handle_request_sense 17107 * 17108 * Description: Processing for non-auto Request Sense command. 17109 * 17110 * Arguments: un - ptr to associated softstate 17111 * sense_bp - ptr to buf(9S) for the RQS command 17112 * sense_xp - ptr to the sd_xbuf for the RQS command 17113 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17114 * 17115 * Context: May be called under interrupt context 17116 */ 17117 17118 static void 17119 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17120 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17121 { 17122 struct buf *cmd_bp; /* buf for the original command */ 17123 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17124 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17125 17126 ASSERT(un != NULL); 17127 ASSERT(mutex_owned(SD_MUTEX(un))); 17128 ASSERT(sense_bp != NULL); 17129 ASSERT(sense_xp != NULL); 17130 ASSERT(sense_pktp != NULL); 17131 17132 /* 17133 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17134 * RQS command and not the original command. 17135 */ 17136 ASSERT(sense_pktp == un->un_rqs_pktp); 17137 ASSERT(sense_bp == un->un_rqs_bp); 17138 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17139 (FLAG_SENSING | FLAG_HEAD)); 17140 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17141 FLAG_SENSING) == FLAG_SENSING); 17142 17143 /* These are the bp, xp, and pktp for the original command */ 17144 cmd_bp = sense_xp->xb_sense_bp; 17145 cmd_xp = SD_GET_XBUF(cmd_bp); 17146 cmd_pktp = SD_GET_PKTP(cmd_bp); 17147 17148 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17149 /* 17150 * The REQUEST SENSE command failed. Release the REQUEST 17151 * SENSE command for re-use, get back the bp for the original 17152 * command, and attempt to re-try the original command if 17153 * FLAG_DIAGNOSE is not set in the original packet. 17154 */ 17155 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17156 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17157 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17158 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17159 NULL, NULL, EIO, (clock_t)0, NULL); 17160 return; 17161 } 17162 } 17163 17164 /* 17165 * Save the relevant sense info into the xp for the original cmd. 17166 * 17167 * Note: if the request sense failed the state info will be zero 17168 * as set in sd_mark_rqs_busy() 17169 */ 17170 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17171 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17172 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17173 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 17174 17175 /* 17176 * Free up the RQS command.... 17177 * NOTE: 17178 * Must do this BEFORE calling sd_validate_sense_data! 17179 * sd_validate_sense_data may return the original command in 17180 * which case the pkt will be freed and the flags can no 17181 * longer be touched. 17182 * SD_MUTEX is held through this process until the command 17183 * is dispatched based upon the sense data, so there are 17184 * no race conditions. 17185 */ 17186 (void) sd_mark_rqs_idle(un, sense_xp); 17187 17188 /* 17189 * For a retryable command see if we have valid sense data, if so then 17190 * turn it over to sd_decode_sense() to figure out the right course of 17191 * action. Just fail a non-retryable command. 17192 */ 17193 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17194 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 17195 SD_SENSE_DATA_IS_VALID) { 17196 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17197 } 17198 } else { 17199 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17200 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17201 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17202 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17203 sd_return_failed_command(un, cmd_bp, EIO); 17204 } 17205 } 17206 17207 17208 17209 17210 /* 17211 * Function: sd_handle_auto_request_sense 17212 * 17213 * Description: Processing for auto-request sense information. 17214 * 17215 * Arguments: un - ptr to associated softstate 17216 * bp - ptr to buf(9S) for the command 17217 * xp - ptr to the sd_xbuf for the command 17218 * pktp - ptr to the scsi_pkt(9S) for the command 17219 * 17220 * Context: May be called under interrupt context 17221 */ 17222 17223 static void 17224 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17225 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17226 { 17227 struct scsi_arq_status *asp; 17228 17229 ASSERT(un != NULL); 17230 ASSERT(mutex_owned(SD_MUTEX(un))); 17231 ASSERT(bp != NULL); 17232 ASSERT(xp != NULL); 17233 ASSERT(pktp != NULL); 17234 ASSERT(pktp != un->un_rqs_pktp); 17235 ASSERT(bp != un->un_rqs_bp); 17236 17237 /* 17238 * For auto-request sense, we get a scsi_arq_status back from 17239 * the HBA, with the sense data in the sts_sensedata member. 17240 * The pkt_scbp of the packet points to this scsi_arq_status. 17241 */ 17242 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17243 17244 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17245 /* 17246 * The auto REQUEST SENSE failed; see if we can re-try 17247 * the original command. 17248 */ 17249 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17250 "auto request sense failed (reason=%s)\n", 17251 scsi_rname(asp->sts_rqpkt_reason)); 17252 17253 sd_reset_target(un, pktp); 17254 17255 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17256 NULL, NULL, EIO, (clock_t)0, NULL); 17257 return; 17258 } 17259 17260 /* Save the relevant sense info into the xp for the original cmd. */ 17261 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17262 xp->xb_sense_state = asp->sts_rqpkt_state; 17263 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17264 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17265 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 17266 17267 /* 17268 * See if we have valid sense data, if so then turn it over to 17269 * sd_decode_sense() to figure out the right course of action. 17270 */ 17271 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 17272 sd_decode_sense(un, bp, xp, pktp); 17273 } 17274 } 17275 17276 17277 /* 17278 * Function: sd_print_sense_failed_msg 17279 * 17280 * Description: Print log message when RQS has failed. 17281 * 17282 * Arguments: un - ptr to associated softstate 17283 * bp - ptr to buf(9S) for the command 17284 * arg - generic message string ptr 17285 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17286 * or SD_NO_RETRY_ISSUED 17287 * 17288 * Context: May be called from interrupt context 17289 */ 17290 17291 static void 17292 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17293 int code) 17294 { 17295 char *msgp = arg; 17296 17297 ASSERT(un != NULL); 17298 ASSERT(mutex_owned(SD_MUTEX(un))); 17299 ASSERT(bp != NULL); 17300 17301 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17302 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17303 } 17304 } 17305 17306 17307 /* 17308 * Function: sd_validate_sense_data 17309 * 17310 * Description: Check the given sense data for validity. 17311 * If the sense data is not valid, the command will 17312 * be either failed or retried! 17313 * 17314 * Return Code: SD_SENSE_DATA_IS_INVALID 17315 * SD_SENSE_DATA_IS_VALID 17316 * 17317 * Context: May be called from interrupt context 17318 */ 17319 17320 static int 17321 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 17322 { 17323 struct scsi_extended_sense *esp; 17324 struct scsi_pkt *pktp; 17325 size_t actual_len; 17326 char *msgp = NULL; 17327 17328 ASSERT(un != NULL); 17329 ASSERT(mutex_owned(SD_MUTEX(un))); 17330 ASSERT(bp != NULL); 17331 ASSERT(bp != un->un_rqs_bp); 17332 ASSERT(xp != NULL); 17333 17334 pktp = SD_GET_PKTP(bp); 17335 ASSERT(pktp != NULL); 17336 17337 /* 17338 * Check the status of the RQS command (auto or manual). 17339 */ 17340 switch (xp->xb_sense_status & STATUS_MASK) { 17341 case STATUS_GOOD: 17342 break; 17343 17344 case STATUS_RESERVATION_CONFLICT: 17345 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17346 return (SD_SENSE_DATA_IS_INVALID); 17347 17348 case STATUS_BUSY: 17349 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17350 "Busy Status on REQUEST SENSE\n"); 17351 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17352 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17353 return (SD_SENSE_DATA_IS_INVALID); 17354 17355 case STATUS_QFULL: 17356 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17357 "QFULL Status on REQUEST SENSE\n"); 17358 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17359 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17360 return (SD_SENSE_DATA_IS_INVALID); 17361 17362 case STATUS_CHECK: 17363 case STATUS_TERMINATED: 17364 msgp = "Check Condition on REQUEST SENSE\n"; 17365 goto sense_failed; 17366 17367 default: 17368 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17369 goto sense_failed; 17370 } 17371 17372 /* 17373 * See if we got the minimum required amount of sense data. 17374 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17375 * or less. 17376 */ 17377 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 17378 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17379 (actual_len == 0)) { 17380 msgp = "Request Sense couldn't get sense data\n"; 17381 goto sense_failed; 17382 } 17383 17384 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17385 msgp = "Not enough sense information\n"; 17386 goto sense_failed; 17387 } 17388 17389 /* 17390 * We require the extended sense data 17391 */ 17392 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17393 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17394 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17395 static char tmp[8]; 17396 static char buf[148]; 17397 char *p = (char *)(xp->xb_sense_data); 17398 int i; 17399 17400 mutex_enter(&sd_sense_mutex); 17401 (void) strcpy(buf, "undecodable sense information:"); 17402 for (i = 0; i < actual_len; i++) { 17403 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17404 (void) strcpy(&buf[strlen(buf)], tmp); 17405 } 17406 i = strlen(buf); 17407 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17408 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 17409 mutex_exit(&sd_sense_mutex); 17410 } 17411 /* Note: Legacy behavior, fail the command with no retry */ 17412 sd_return_failed_command(un, bp, EIO); 17413 return (SD_SENSE_DATA_IS_INVALID); 17414 } 17415 17416 /* 17417 * Check that es_code is valid (es_class concatenated with es_code 17418 * make up the "response code" field. es_class will always be 7, so 17419 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17420 * format. 17421 */ 17422 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17423 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17424 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17425 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17426 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17427 goto sense_failed; 17428 } 17429 17430 return (SD_SENSE_DATA_IS_VALID); 17431 17432 sense_failed: 17433 /* 17434 * If the request sense failed (for whatever reason), attempt 17435 * to retry the original command. 17436 */ 17437 #if defined(__i386) || defined(__amd64) 17438 /* 17439 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17440 * sddef.h for Sparc platform, and x86 uses 1 binary 17441 * for both SCSI/FC. 17442 * The SD_RETRY_DELAY value need to be adjusted here 17443 * when SD_RETRY_DELAY change in sddef.h 17444 */ 17445 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17446 sd_print_sense_failed_msg, msgp, EIO, 17447 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17448 #else 17449 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17450 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17451 #endif 17452 17453 return (SD_SENSE_DATA_IS_INVALID); 17454 } 17455 17456 17457 17458 /* 17459 * Function: sd_decode_sense 17460 * 17461 * Description: Take recovery action(s) when SCSI Sense Data is received. 17462 * 17463 * Context: Interrupt context. 17464 */ 17465 17466 static void 17467 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17468 struct scsi_pkt *pktp) 17469 { 17470 uint8_t sense_key; 17471 17472 ASSERT(un != NULL); 17473 ASSERT(mutex_owned(SD_MUTEX(un))); 17474 ASSERT(bp != NULL); 17475 ASSERT(bp != un->un_rqs_bp); 17476 ASSERT(xp != NULL); 17477 ASSERT(pktp != NULL); 17478 17479 sense_key = scsi_sense_key(xp->xb_sense_data); 17480 17481 switch (sense_key) { 17482 case KEY_NO_SENSE: 17483 sd_sense_key_no_sense(un, bp, xp, pktp); 17484 break; 17485 case KEY_RECOVERABLE_ERROR: 17486 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17487 bp, xp, pktp); 17488 break; 17489 case KEY_NOT_READY: 17490 sd_sense_key_not_ready(un, xp->xb_sense_data, 17491 bp, xp, pktp); 17492 break; 17493 case KEY_MEDIUM_ERROR: 17494 case KEY_HARDWARE_ERROR: 17495 sd_sense_key_medium_or_hardware_error(un, 17496 xp->xb_sense_data, bp, xp, pktp); 17497 break; 17498 case KEY_ILLEGAL_REQUEST: 17499 sd_sense_key_illegal_request(un, bp, xp, pktp); 17500 break; 17501 case KEY_UNIT_ATTENTION: 17502 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17503 bp, xp, pktp); 17504 break; 17505 case KEY_WRITE_PROTECT: 17506 case KEY_VOLUME_OVERFLOW: 17507 case KEY_MISCOMPARE: 17508 sd_sense_key_fail_command(un, bp, xp, pktp); 17509 break; 17510 case KEY_BLANK_CHECK: 17511 sd_sense_key_blank_check(un, bp, xp, pktp); 17512 break; 17513 case KEY_ABORTED_COMMAND: 17514 sd_sense_key_aborted_command(un, bp, xp, pktp); 17515 break; 17516 case KEY_VENDOR_UNIQUE: 17517 case KEY_COPY_ABORTED: 17518 case KEY_EQUAL: 17519 case KEY_RESERVED: 17520 default: 17521 sd_sense_key_default(un, xp->xb_sense_data, 17522 bp, xp, pktp); 17523 break; 17524 } 17525 } 17526 17527 17528 /* 17529 * Function: sd_dump_memory 17530 * 17531 * Description: Debug logging routine to print the contents of a user provided 17532 * buffer. The output of the buffer is broken up into 256 byte 17533 * segments due to a size constraint of the scsi_log. 17534 * implementation. 17535 * 17536 * Arguments: un - ptr to softstate 17537 * comp - component mask 17538 * title - "title" string to preceed data when printed 17539 * data - ptr to data block to be printed 17540 * len - size of data block to be printed 17541 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17542 * 17543 * Context: May be called from interrupt context 17544 */ 17545 17546 #define SD_DUMP_MEMORY_BUF_SIZE 256 17547 17548 static char *sd_dump_format_string[] = { 17549 " 0x%02x", 17550 " %c" 17551 }; 17552 17553 static void 17554 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17555 int len, int fmt) 17556 { 17557 int i, j; 17558 int avail_count; 17559 int start_offset; 17560 int end_offset; 17561 size_t entry_len; 17562 char *bufp; 17563 char *local_buf; 17564 char *format_string; 17565 17566 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17567 17568 /* 17569 * In the debug version of the driver, this function is called from a 17570 * number of places which are NOPs in the release driver. 17571 * The debug driver therefore has additional methods of filtering 17572 * debug output. 17573 */ 17574 #ifdef SDDEBUG 17575 /* 17576 * In the debug version of the driver we can reduce the amount of debug 17577 * messages by setting sd_error_level to something other than 17578 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17579 * sd_component_mask. 17580 */ 17581 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17582 (sd_error_level != SCSI_ERR_ALL)) { 17583 return; 17584 } 17585 if (((sd_component_mask & comp) == 0) || 17586 (sd_error_level != SCSI_ERR_ALL)) { 17587 return; 17588 } 17589 #else 17590 if (sd_error_level != SCSI_ERR_ALL) { 17591 return; 17592 } 17593 #endif 17594 17595 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17596 bufp = local_buf; 17597 /* 17598 * Available length is the length of local_buf[], minus the 17599 * length of the title string, minus one for the ":", minus 17600 * one for the newline, minus one for the NULL terminator. 17601 * This gives the #bytes available for holding the printed 17602 * values from the given data buffer. 17603 */ 17604 if (fmt == SD_LOG_HEX) { 17605 format_string = sd_dump_format_string[0]; 17606 } else /* SD_LOG_CHAR */ { 17607 format_string = sd_dump_format_string[1]; 17608 } 17609 /* 17610 * Available count is the number of elements from the given 17611 * data buffer that we can fit into the available length. 17612 * This is based upon the size of the format string used. 17613 * Make one entry and find it's size. 17614 */ 17615 (void) sprintf(bufp, format_string, data[0]); 17616 entry_len = strlen(bufp); 17617 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17618 17619 j = 0; 17620 while (j < len) { 17621 bufp = local_buf; 17622 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17623 start_offset = j; 17624 17625 end_offset = start_offset + avail_count; 17626 17627 (void) sprintf(bufp, "%s:", title); 17628 bufp += strlen(bufp); 17629 for (i = start_offset; ((i < end_offset) && (j < len)); 17630 i++, j++) { 17631 (void) sprintf(bufp, format_string, data[i]); 17632 bufp += entry_len; 17633 } 17634 (void) sprintf(bufp, "\n"); 17635 17636 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17637 } 17638 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17639 } 17640 17641 /* 17642 * Function: sd_print_sense_msg 17643 * 17644 * Description: Log a message based upon the given sense data. 17645 * 17646 * Arguments: un - ptr to associated softstate 17647 * bp - ptr to buf(9S) for the command 17648 * arg - ptr to associate sd_sense_info struct 17649 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17650 * or SD_NO_RETRY_ISSUED 17651 * 17652 * Context: May be called from interrupt context 17653 */ 17654 17655 static void 17656 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17657 { 17658 struct sd_xbuf *xp; 17659 struct scsi_pkt *pktp; 17660 uint8_t *sensep; 17661 daddr_t request_blkno; 17662 diskaddr_t err_blkno; 17663 int severity; 17664 int pfa_flag; 17665 extern struct scsi_key_strings scsi_cmds[]; 17666 17667 ASSERT(un != NULL); 17668 ASSERT(mutex_owned(SD_MUTEX(un))); 17669 ASSERT(bp != NULL); 17670 xp = SD_GET_XBUF(bp); 17671 ASSERT(xp != NULL); 17672 pktp = SD_GET_PKTP(bp); 17673 ASSERT(pktp != NULL); 17674 ASSERT(arg != NULL); 17675 17676 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17677 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17678 17679 if ((code == SD_DELAYED_RETRY_ISSUED) || 17680 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17681 severity = SCSI_ERR_RETRYABLE; 17682 } 17683 17684 /* Use absolute block number for the request block number */ 17685 request_blkno = xp->xb_blkno; 17686 17687 /* 17688 * Now try to get the error block number from the sense data 17689 */ 17690 sensep = xp->xb_sense_data; 17691 17692 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17693 (uint64_t *)&err_blkno)) { 17694 /* 17695 * We retrieved the error block number from the information 17696 * portion of the sense data. 17697 * 17698 * For USCSI commands we are better off using the error 17699 * block no. as the requested block no. (This is the best 17700 * we can estimate.) 17701 */ 17702 if ((SD_IS_BUFIO(xp) == FALSE) && 17703 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17704 request_blkno = err_blkno; 17705 } 17706 } else { 17707 /* 17708 * Without the es_valid bit set (for fixed format) or an 17709 * information descriptor (for descriptor format) we cannot 17710 * be certain of the error blkno, so just use the 17711 * request_blkno. 17712 */ 17713 err_blkno = (diskaddr_t)request_blkno; 17714 } 17715 17716 /* 17717 * The following will log the buffer contents for the release driver 17718 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17719 * level is set to verbose. 17720 */ 17721 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17722 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17723 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17724 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17725 17726 if (pfa_flag == FALSE) { 17727 /* This is normally only set for USCSI */ 17728 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17729 return; 17730 } 17731 17732 if ((SD_IS_BUFIO(xp) == TRUE) && 17733 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17734 (severity < sd_error_level))) { 17735 return; 17736 } 17737 } 17738 17739 /* 17740 * Check for Sonoma Failover and keep a count of how many failed I/O's 17741 */ 17742 if ((SD_IS_LSI(un)) && 17743 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17744 (scsi_sense_asc(sensep) == 0x94) && 17745 (scsi_sense_ascq(sensep) == 0x01)) { 17746 un->un_sonoma_failure_count++; 17747 if (un->un_sonoma_failure_count > 1) { 17748 return; 17749 } 17750 } 17751 17752 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17753 request_blkno, err_blkno, scsi_cmds, 17754 (struct scsi_extended_sense *)sensep, 17755 un->un_additional_codes, NULL); 17756 } 17757 17758 /* 17759 * Function: sd_sense_key_no_sense 17760 * 17761 * Description: Recovery action when sense data was not received. 17762 * 17763 * Context: May be called from interrupt context 17764 */ 17765 17766 static void 17767 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17768 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17769 { 17770 struct sd_sense_info si; 17771 17772 ASSERT(un != NULL); 17773 ASSERT(mutex_owned(SD_MUTEX(un))); 17774 ASSERT(bp != NULL); 17775 ASSERT(xp != NULL); 17776 ASSERT(pktp != NULL); 17777 17778 si.ssi_severity = SCSI_ERR_FATAL; 17779 si.ssi_pfa_flag = FALSE; 17780 17781 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17782 17783 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17784 &si, EIO, (clock_t)0, NULL); 17785 } 17786 17787 17788 /* 17789 * Function: sd_sense_key_recoverable_error 17790 * 17791 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17792 * 17793 * Context: May be called from interrupt context 17794 */ 17795 17796 static void 17797 sd_sense_key_recoverable_error(struct sd_lun *un, 17798 uint8_t *sense_datap, 17799 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17800 { 17801 struct sd_sense_info si; 17802 uint8_t asc = scsi_sense_asc(sense_datap); 17803 17804 ASSERT(un != NULL); 17805 ASSERT(mutex_owned(SD_MUTEX(un))); 17806 ASSERT(bp != NULL); 17807 ASSERT(xp != NULL); 17808 ASSERT(pktp != NULL); 17809 17810 /* 17811 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17812 */ 17813 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17814 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17815 si.ssi_severity = SCSI_ERR_INFO; 17816 si.ssi_pfa_flag = TRUE; 17817 } else { 17818 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17819 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17820 si.ssi_severity = SCSI_ERR_RECOVERED; 17821 si.ssi_pfa_flag = FALSE; 17822 } 17823 17824 if (pktp->pkt_resid == 0) { 17825 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17826 sd_return_command(un, bp); 17827 return; 17828 } 17829 17830 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17831 &si, EIO, (clock_t)0, NULL); 17832 } 17833 17834 17835 17836 17837 /* 17838 * Function: sd_sense_key_not_ready 17839 * 17840 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17841 * 17842 * Context: May be called from interrupt context 17843 */ 17844 17845 static void 17846 sd_sense_key_not_ready(struct sd_lun *un, 17847 uint8_t *sense_datap, 17848 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17849 { 17850 struct sd_sense_info si; 17851 uint8_t asc = scsi_sense_asc(sense_datap); 17852 uint8_t ascq = scsi_sense_ascq(sense_datap); 17853 17854 ASSERT(un != NULL); 17855 ASSERT(mutex_owned(SD_MUTEX(un))); 17856 ASSERT(bp != NULL); 17857 ASSERT(xp != NULL); 17858 ASSERT(pktp != NULL); 17859 17860 si.ssi_severity = SCSI_ERR_FATAL; 17861 si.ssi_pfa_flag = FALSE; 17862 17863 /* 17864 * Update error stats after first NOT READY error. Disks may have 17865 * been powered down and may need to be restarted. For CDROMs, 17866 * report NOT READY errors only if media is present. 17867 */ 17868 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17869 (xp->xb_retry_count > 0)) { 17870 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17871 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17872 } 17873 17874 /* 17875 * Just fail if the "not ready" retry limit has been reached. 17876 */ 17877 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17878 /* Special check for error message printing for removables. */ 17879 if (un->un_f_has_removable_media && (asc == 0x04) && 17880 (ascq >= 0x04)) { 17881 si.ssi_severity = SCSI_ERR_ALL; 17882 } 17883 goto fail_command; 17884 } 17885 17886 /* 17887 * Check the ASC and ASCQ in the sense data as needed, to determine 17888 * what to do. 17889 */ 17890 switch (asc) { 17891 case 0x04: /* LOGICAL UNIT NOT READY */ 17892 /* 17893 * disk drives that don't spin up result in a very long delay 17894 * in format without warning messages. We will log a message 17895 * if the error level is set to verbose. 17896 */ 17897 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17898 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17899 "logical unit not ready, resetting disk\n"); 17900 } 17901 17902 /* 17903 * There are different requirements for CDROMs and disks for 17904 * the number of retries. If a CD-ROM is giving this, it is 17905 * probably reading TOC and is in the process of getting 17906 * ready, so we should keep on trying for a long time to make 17907 * sure that all types of media are taken in account (for 17908 * some media the drive takes a long time to read TOC). For 17909 * disks we do not want to retry this too many times as this 17910 * can cause a long hang in format when the drive refuses to 17911 * spin up (a very common failure). 17912 */ 17913 switch (ascq) { 17914 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17915 /* 17916 * Disk drives frequently refuse to spin up which 17917 * results in a very long hang in format without 17918 * warning messages. 17919 * 17920 * Note: This code preserves the legacy behavior of 17921 * comparing xb_retry_count against zero for fibre 17922 * channel targets instead of comparing against the 17923 * un_reset_retry_count value. The reason for this 17924 * discrepancy has been so utterly lost beneath the 17925 * Sands of Time that even Indiana Jones could not 17926 * find it. 17927 */ 17928 if (un->un_f_is_fibre == TRUE) { 17929 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17930 (xp->xb_retry_count > 0)) && 17931 (un->un_startstop_timeid == NULL)) { 17932 scsi_log(SD_DEVINFO(un), sd_label, 17933 CE_WARN, "logical unit not ready, " 17934 "resetting disk\n"); 17935 sd_reset_target(un, pktp); 17936 } 17937 } else { 17938 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17939 (xp->xb_retry_count > 17940 un->un_reset_retry_count)) && 17941 (un->un_startstop_timeid == NULL)) { 17942 scsi_log(SD_DEVINFO(un), sd_label, 17943 CE_WARN, "logical unit not ready, " 17944 "resetting disk\n"); 17945 sd_reset_target(un, pktp); 17946 } 17947 } 17948 break; 17949 17950 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17951 /* 17952 * If the target is in the process of becoming 17953 * ready, just proceed with the retry. This can 17954 * happen with CD-ROMs that take a long time to 17955 * read TOC after a power cycle or reset. 17956 */ 17957 goto do_retry; 17958 17959 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17960 break; 17961 17962 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17963 /* 17964 * Retries cannot help here so just fail right away. 17965 */ 17966 goto fail_command; 17967 17968 case 0x88: 17969 /* 17970 * Vendor-unique code for T3/T4: it indicates a 17971 * path problem in a mutipathed config, but as far as 17972 * the target driver is concerned it equates to a fatal 17973 * error, so we should just fail the command right away 17974 * (without printing anything to the console). If this 17975 * is not a T3/T4, fall thru to the default recovery 17976 * action. 17977 * T3/T4 is FC only, don't need to check is_fibre 17978 */ 17979 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17980 sd_return_failed_command(un, bp, EIO); 17981 return; 17982 } 17983 /* FALLTHRU */ 17984 17985 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17986 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17987 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17988 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17989 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17990 default: /* Possible future codes in SCSI spec? */ 17991 /* 17992 * For removable-media devices, do not retry if 17993 * ASCQ > 2 as these result mostly from USCSI commands 17994 * on MMC devices issued to check status of an 17995 * operation initiated in immediate mode. Also for 17996 * ASCQ >= 4 do not print console messages as these 17997 * mainly represent a user-initiated operation 17998 * instead of a system failure. 17999 */ 18000 if (un->un_f_has_removable_media) { 18001 si.ssi_severity = SCSI_ERR_ALL; 18002 goto fail_command; 18003 } 18004 break; 18005 } 18006 18007 /* 18008 * As part of our recovery attempt for the NOT READY 18009 * condition, we issue a START STOP UNIT command. However 18010 * we want to wait for a short delay before attempting this 18011 * as there may still be more commands coming back from the 18012 * target with the check condition. To do this we use 18013 * timeout(9F) to call sd_start_stop_unit_callback() after 18014 * the delay interval expires. (sd_start_stop_unit_callback() 18015 * dispatches sd_start_stop_unit_task(), which will issue 18016 * the actual START STOP UNIT command. The delay interval 18017 * is one-half of the delay that we will use to retry the 18018 * command that generated the NOT READY condition. 18019 * 18020 * Note that we could just dispatch sd_start_stop_unit_task() 18021 * from here and allow it to sleep for the delay interval, 18022 * but then we would be tying up the taskq thread 18023 * uncesessarily for the duration of the delay. 18024 * 18025 * Do not issue the START STOP UNIT if the current command 18026 * is already a START STOP UNIT. 18027 */ 18028 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18029 break; 18030 } 18031 18032 /* 18033 * Do not schedule the timeout if one is already pending. 18034 */ 18035 if (un->un_startstop_timeid != NULL) { 18036 SD_INFO(SD_LOG_ERROR, un, 18037 "sd_sense_key_not_ready: restart already issued to" 18038 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18039 ddi_get_instance(SD_DEVINFO(un))); 18040 break; 18041 } 18042 18043 /* 18044 * Schedule the START STOP UNIT command, then queue the command 18045 * for a retry. 18046 * 18047 * Note: A timeout is not scheduled for this retry because we 18048 * want the retry to be serial with the START_STOP_UNIT. The 18049 * retry will be started when the START_STOP_UNIT is completed 18050 * in sd_start_stop_unit_task. 18051 */ 18052 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18053 un, SD_BSY_TIMEOUT / 2); 18054 xp->xb_retry_count++; 18055 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18056 return; 18057 18058 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18059 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18060 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18061 "unit does not respond to selection\n"); 18062 } 18063 break; 18064 18065 case 0x3A: /* MEDIUM NOT PRESENT */ 18066 if (sd_error_level >= SCSI_ERR_FATAL) { 18067 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18068 "Caddy not inserted in drive\n"); 18069 } 18070 18071 sr_ejected(un); 18072 un->un_mediastate = DKIO_EJECTED; 18073 /* The state has changed, inform the media watch routines */ 18074 cv_broadcast(&un->un_state_cv); 18075 /* Just fail if no media is present in the drive. */ 18076 goto fail_command; 18077 18078 default: 18079 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18080 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18081 "Unit not Ready. Additional sense code 0x%x\n", 18082 asc); 18083 } 18084 break; 18085 } 18086 18087 do_retry: 18088 18089 /* 18090 * Retry the command, as some targets may report NOT READY for 18091 * several seconds after being reset. 18092 */ 18093 xp->xb_retry_count++; 18094 si.ssi_severity = SCSI_ERR_RETRYABLE; 18095 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18096 &si, EIO, SD_BSY_TIMEOUT, NULL); 18097 18098 return; 18099 18100 fail_command: 18101 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18102 sd_return_failed_command(un, bp, EIO); 18103 } 18104 18105 18106 18107 /* 18108 * Function: sd_sense_key_medium_or_hardware_error 18109 * 18110 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18111 * sense key. 18112 * 18113 * Context: May be called from interrupt context 18114 */ 18115 18116 static void 18117 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18118 uint8_t *sense_datap, 18119 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18120 { 18121 struct sd_sense_info si; 18122 uint8_t sense_key = scsi_sense_key(sense_datap); 18123 uint8_t asc = scsi_sense_asc(sense_datap); 18124 18125 ASSERT(un != NULL); 18126 ASSERT(mutex_owned(SD_MUTEX(un))); 18127 ASSERT(bp != NULL); 18128 ASSERT(xp != NULL); 18129 ASSERT(pktp != NULL); 18130 18131 si.ssi_severity = SCSI_ERR_FATAL; 18132 si.ssi_pfa_flag = FALSE; 18133 18134 if (sense_key == KEY_MEDIUM_ERROR) { 18135 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18136 } 18137 18138 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18139 18140 if ((un->un_reset_retry_count != 0) && 18141 (xp->xb_retry_count == un->un_reset_retry_count)) { 18142 mutex_exit(SD_MUTEX(un)); 18143 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18144 if (un->un_f_allow_bus_device_reset == TRUE) { 18145 18146 boolean_t try_resetting_target = B_TRUE; 18147 18148 /* 18149 * We need to be able to handle specific ASC when we are 18150 * handling a KEY_HARDWARE_ERROR. In particular 18151 * taking the default action of resetting the target may 18152 * not be the appropriate way to attempt recovery. 18153 * Resetting a target because of a single LUN failure 18154 * victimizes all LUNs on that target. 18155 * 18156 * This is true for the LSI arrays, if an LSI 18157 * array controller returns an ASC of 0x84 (LUN Dead) we 18158 * should trust it. 18159 */ 18160 18161 if (sense_key == KEY_HARDWARE_ERROR) { 18162 switch (asc) { 18163 case 0x84: 18164 if (SD_IS_LSI(un)) { 18165 try_resetting_target = B_FALSE; 18166 } 18167 break; 18168 default: 18169 break; 18170 } 18171 } 18172 18173 if (try_resetting_target == B_TRUE) { 18174 int reset_retval = 0; 18175 if (un->un_f_lun_reset_enabled == TRUE) { 18176 SD_TRACE(SD_LOG_IO_CORE, un, 18177 "sd_sense_key_medium_or_hardware_" 18178 "error: issuing RESET_LUN\n"); 18179 reset_retval = 18180 scsi_reset(SD_ADDRESS(un), 18181 RESET_LUN); 18182 } 18183 if (reset_retval == 0) { 18184 SD_TRACE(SD_LOG_IO_CORE, un, 18185 "sd_sense_key_medium_or_hardware_" 18186 "error: issuing RESET_TARGET\n"); 18187 (void) scsi_reset(SD_ADDRESS(un), 18188 RESET_TARGET); 18189 } 18190 } 18191 } 18192 mutex_enter(SD_MUTEX(un)); 18193 } 18194 18195 /* 18196 * This really ought to be a fatal error, but we will retry anyway 18197 * as some drives report this as a spurious error. 18198 */ 18199 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18200 &si, EIO, (clock_t)0, NULL); 18201 } 18202 18203 18204 18205 /* 18206 * Function: sd_sense_key_illegal_request 18207 * 18208 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18209 * 18210 * Context: May be called from interrupt context 18211 */ 18212 18213 static void 18214 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18215 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18216 { 18217 struct sd_sense_info si; 18218 18219 ASSERT(un != NULL); 18220 ASSERT(mutex_owned(SD_MUTEX(un))); 18221 ASSERT(bp != NULL); 18222 ASSERT(xp != NULL); 18223 ASSERT(pktp != NULL); 18224 18225 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18226 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18227 18228 si.ssi_severity = SCSI_ERR_INFO; 18229 si.ssi_pfa_flag = FALSE; 18230 18231 /* Pointless to retry if the target thinks it's an illegal request */ 18232 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18233 sd_return_failed_command(un, bp, EIO); 18234 } 18235 18236 18237 18238 18239 /* 18240 * Function: sd_sense_key_unit_attention 18241 * 18242 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18243 * 18244 * Context: May be called from interrupt context 18245 */ 18246 18247 static void 18248 sd_sense_key_unit_attention(struct sd_lun *un, 18249 uint8_t *sense_datap, 18250 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18251 { 18252 /* 18253 * For UNIT ATTENTION we allow retries for one minute. Devices 18254 * like Sonoma can return UNIT ATTENTION close to a minute 18255 * under certain conditions. 18256 */ 18257 int retry_check_flag = SD_RETRIES_UA; 18258 boolean_t kstat_updated = B_FALSE; 18259 struct sd_sense_info si; 18260 uint8_t asc = scsi_sense_asc(sense_datap); 18261 18262 ASSERT(un != NULL); 18263 ASSERT(mutex_owned(SD_MUTEX(un))); 18264 ASSERT(bp != NULL); 18265 ASSERT(xp != NULL); 18266 ASSERT(pktp != NULL); 18267 18268 si.ssi_severity = SCSI_ERR_INFO; 18269 si.ssi_pfa_flag = FALSE; 18270 18271 18272 switch (asc) { 18273 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18274 if (sd_report_pfa != 0) { 18275 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18276 si.ssi_pfa_flag = TRUE; 18277 retry_check_flag = SD_RETRIES_STANDARD; 18278 goto do_retry; 18279 } 18280 18281 break; 18282 18283 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18284 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18285 un->un_resvd_status |= 18286 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18287 } 18288 #ifdef _LP64 18289 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18290 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18291 un, KM_NOSLEEP) == 0) { 18292 /* 18293 * If we can't dispatch the task we'll just 18294 * live without descriptor sense. We can 18295 * try again on the next "unit attention" 18296 */ 18297 SD_ERROR(SD_LOG_ERROR, un, 18298 "sd_sense_key_unit_attention: " 18299 "Could not dispatch " 18300 "sd_reenable_dsense_task\n"); 18301 } 18302 } 18303 #endif /* _LP64 */ 18304 /* FALLTHRU */ 18305 18306 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18307 if (!un->un_f_has_removable_media) { 18308 break; 18309 } 18310 18311 /* 18312 * When we get a unit attention from a removable-media device, 18313 * it may be in a state that will take a long time to recover 18314 * (e.g., from a reset). Since we are executing in interrupt 18315 * context here, we cannot wait around for the device to come 18316 * back. So hand this command off to sd_media_change_task() 18317 * for deferred processing under taskq thread context. (Note 18318 * that the command still may be failed if a problem is 18319 * encountered at a later time.) 18320 */ 18321 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18322 KM_NOSLEEP) == 0) { 18323 /* 18324 * Cannot dispatch the request so fail the command. 18325 */ 18326 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18327 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18328 si.ssi_severity = SCSI_ERR_FATAL; 18329 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18330 sd_return_failed_command(un, bp, EIO); 18331 } 18332 18333 /* 18334 * If failed to dispatch sd_media_change_task(), we already 18335 * updated kstat. If succeed to dispatch sd_media_change_task(), 18336 * we should update kstat later if it encounters an error. So, 18337 * we update kstat_updated flag here. 18338 */ 18339 kstat_updated = B_TRUE; 18340 18341 /* 18342 * Either the command has been successfully dispatched to a 18343 * task Q for retrying, or the dispatch failed. In either case 18344 * do NOT retry again by calling sd_retry_command. This sets up 18345 * two retries of the same command and when one completes and 18346 * frees the resources the other will access freed memory, 18347 * a bad thing. 18348 */ 18349 return; 18350 18351 default: 18352 break; 18353 } 18354 18355 /* 18356 * Update kstat if we haven't done that. 18357 */ 18358 if (!kstat_updated) { 18359 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18360 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18361 } 18362 18363 do_retry: 18364 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18365 EIO, SD_UA_RETRY_DELAY, NULL); 18366 } 18367 18368 18369 18370 /* 18371 * Function: sd_sense_key_fail_command 18372 * 18373 * Description: Use to fail a command when we don't like the sense key that 18374 * was returned. 18375 * 18376 * Context: May be called from interrupt context 18377 */ 18378 18379 static void 18380 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18381 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18382 { 18383 struct sd_sense_info si; 18384 18385 ASSERT(un != NULL); 18386 ASSERT(mutex_owned(SD_MUTEX(un))); 18387 ASSERT(bp != NULL); 18388 ASSERT(xp != NULL); 18389 ASSERT(pktp != NULL); 18390 18391 si.ssi_severity = SCSI_ERR_FATAL; 18392 si.ssi_pfa_flag = FALSE; 18393 18394 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18395 sd_return_failed_command(un, bp, EIO); 18396 } 18397 18398 18399 18400 /* 18401 * Function: sd_sense_key_blank_check 18402 * 18403 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18404 * Has no monetary connotation. 18405 * 18406 * Context: May be called from interrupt context 18407 */ 18408 18409 static void 18410 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18411 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18412 { 18413 struct sd_sense_info si; 18414 18415 ASSERT(un != NULL); 18416 ASSERT(mutex_owned(SD_MUTEX(un))); 18417 ASSERT(bp != NULL); 18418 ASSERT(xp != NULL); 18419 ASSERT(pktp != NULL); 18420 18421 /* 18422 * Blank check is not fatal for removable devices, therefore 18423 * it does not require a console message. 18424 */ 18425 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18426 SCSI_ERR_FATAL; 18427 si.ssi_pfa_flag = FALSE; 18428 18429 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18430 sd_return_failed_command(un, bp, EIO); 18431 } 18432 18433 18434 18435 18436 /* 18437 * Function: sd_sense_key_aborted_command 18438 * 18439 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18440 * 18441 * Context: May be called from interrupt context 18442 */ 18443 18444 static void 18445 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18446 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18447 { 18448 struct sd_sense_info si; 18449 18450 ASSERT(un != NULL); 18451 ASSERT(mutex_owned(SD_MUTEX(un))); 18452 ASSERT(bp != NULL); 18453 ASSERT(xp != NULL); 18454 ASSERT(pktp != NULL); 18455 18456 si.ssi_severity = SCSI_ERR_FATAL; 18457 si.ssi_pfa_flag = FALSE; 18458 18459 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18460 18461 /* 18462 * This really ought to be a fatal error, but we will retry anyway 18463 * as some drives report this as a spurious error. 18464 */ 18465 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18466 &si, EIO, (clock_t)0, NULL); 18467 } 18468 18469 18470 18471 /* 18472 * Function: sd_sense_key_default 18473 * 18474 * Description: Default recovery action for several SCSI sense keys (basically 18475 * attempts a retry). 18476 * 18477 * Context: May be called from interrupt context 18478 */ 18479 18480 static void 18481 sd_sense_key_default(struct sd_lun *un, 18482 uint8_t *sense_datap, 18483 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18484 { 18485 struct sd_sense_info si; 18486 uint8_t sense_key = scsi_sense_key(sense_datap); 18487 18488 ASSERT(un != NULL); 18489 ASSERT(mutex_owned(SD_MUTEX(un))); 18490 ASSERT(bp != NULL); 18491 ASSERT(xp != NULL); 18492 ASSERT(pktp != NULL); 18493 18494 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18495 18496 /* 18497 * Undecoded sense key. Attempt retries and hope that will fix 18498 * the problem. Otherwise, we're dead. 18499 */ 18500 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18501 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18502 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18503 } 18504 18505 si.ssi_severity = SCSI_ERR_FATAL; 18506 si.ssi_pfa_flag = FALSE; 18507 18508 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18509 &si, EIO, (clock_t)0, NULL); 18510 } 18511 18512 18513 18514 /* 18515 * Function: sd_print_retry_msg 18516 * 18517 * Description: Print a message indicating the retry action being taken. 18518 * 18519 * Arguments: un - ptr to associated softstate 18520 * bp - ptr to buf(9S) for the command 18521 * arg - not used. 18522 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18523 * or SD_NO_RETRY_ISSUED 18524 * 18525 * Context: May be called from interrupt context 18526 */ 18527 /* ARGSUSED */ 18528 static void 18529 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18530 { 18531 struct sd_xbuf *xp; 18532 struct scsi_pkt *pktp; 18533 char *reasonp; 18534 char *msgp; 18535 18536 ASSERT(un != NULL); 18537 ASSERT(mutex_owned(SD_MUTEX(un))); 18538 ASSERT(bp != NULL); 18539 pktp = SD_GET_PKTP(bp); 18540 ASSERT(pktp != NULL); 18541 xp = SD_GET_XBUF(bp); 18542 ASSERT(xp != NULL); 18543 18544 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18545 mutex_enter(&un->un_pm_mutex); 18546 if ((un->un_state == SD_STATE_SUSPENDED) || 18547 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18548 (pktp->pkt_flags & FLAG_SILENT)) { 18549 mutex_exit(&un->un_pm_mutex); 18550 goto update_pkt_reason; 18551 } 18552 mutex_exit(&un->un_pm_mutex); 18553 18554 /* 18555 * Suppress messages if they are all the same pkt_reason; with 18556 * TQ, many (up to 256) are returned with the same pkt_reason. 18557 * If we are in panic, then suppress the retry messages. 18558 */ 18559 switch (flag) { 18560 case SD_NO_RETRY_ISSUED: 18561 msgp = "giving up"; 18562 break; 18563 case SD_IMMEDIATE_RETRY_ISSUED: 18564 case SD_DELAYED_RETRY_ISSUED: 18565 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18566 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18567 (sd_error_level != SCSI_ERR_ALL))) { 18568 return; 18569 } 18570 msgp = "retrying command"; 18571 break; 18572 default: 18573 goto update_pkt_reason; 18574 } 18575 18576 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18577 scsi_rname(pktp->pkt_reason)); 18578 18579 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18580 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18581 18582 update_pkt_reason: 18583 /* 18584 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18585 * This is to prevent multiple console messages for the same failure 18586 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18587 * when the command is retried successfully because there still may be 18588 * more commands coming back with the same value of pktp->pkt_reason. 18589 */ 18590 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18591 un->un_last_pkt_reason = pktp->pkt_reason; 18592 } 18593 } 18594 18595 18596 /* 18597 * Function: sd_print_cmd_incomplete_msg 18598 * 18599 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18600 * 18601 * Arguments: un - ptr to associated softstate 18602 * bp - ptr to buf(9S) for the command 18603 * arg - passed to sd_print_retry_msg() 18604 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18605 * or SD_NO_RETRY_ISSUED 18606 * 18607 * Context: May be called from interrupt context 18608 */ 18609 18610 static void 18611 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18612 int code) 18613 { 18614 dev_info_t *dip; 18615 18616 ASSERT(un != NULL); 18617 ASSERT(mutex_owned(SD_MUTEX(un))); 18618 ASSERT(bp != NULL); 18619 18620 switch (code) { 18621 case SD_NO_RETRY_ISSUED: 18622 /* Command was failed. Someone turned off this target? */ 18623 if (un->un_state != SD_STATE_OFFLINE) { 18624 /* 18625 * Suppress message if we are detaching and 18626 * device has been disconnected 18627 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18628 * private interface and not part of the DDI 18629 */ 18630 dip = un->un_sd->sd_dev; 18631 if (!(DEVI_IS_DETACHING(dip) && 18632 DEVI_IS_DEVICE_REMOVED(dip))) { 18633 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18634 "disk not responding to selection\n"); 18635 } 18636 New_state(un, SD_STATE_OFFLINE); 18637 } 18638 break; 18639 18640 case SD_DELAYED_RETRY_ISSUED: 18641 case SD_IMMEDIATE_RETRY_ISSUED: 18642 default: 18643 /* Command was successfully queued for retry */ 18644 sd_print_retry_msg(un, bp, arg, code); 18645 break; 18646 } 18647 } 18648 18649 18650 /* 18651 * Function: sd_pkt_reason_cmd_incomplete 18652 * 18653 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18654 * 18655 * Context: May be called from interrupt context 18656 */ 18657 18658 static void 18659 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18660 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18661 { 18662 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18663 18664 ASSERT(un != NULL); 18665 ASSERT(mutex_owned(SD_MUTEX(un))); 18666 ASSERT(bp != NULL); 18667 ASSERT(xp != NULL); 18668 ASSERT(pktp != NULL); 18669 18670 /* Do not do a reset if selection did not complete */ 18671 /* Note: Should this not just check the bit? */ 18672 if (pktp->pkt_state != STATE_GOT_BUS) { 18673 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18674 sd_reset_target(un, pktp); 18675 } 18676 18677 /* 18678 * If the target was not successfully selected, then set 18679 * SD_RETRIES_FAILFAST to indicate that we lost communication 18680 * with the target, and further retries and/or commands are 18681 * likely to take a long time. 18682 */ 18683 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18684 flag |= SD_RETRIES_FAILFAST; 18685 } 18686 18687 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18688 18689 sd_retry_command(un, bp, flag, 18690 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18691 } 18692 18693 18694 18695 /* 18696 * Function: sd_pkt_reason_cmd_tran_err 18697 * 18698 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18699 * 18700 * Context: May be called from interrupt context 18701 */ 18702 18703 static void 18704 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18705 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18706 { 18707 ASSERT(un != NULL); 18708 ASSERT(mutex_owned(SD_MUTEX(un))); 18709 ASSERT(bp != NULL); 18710 ASSERT(xp != NULL); 18711 ASSERT(pktp != NULL); 18712 18713 /* 18714 * Do not reset if we got a parity error, or if 18715 * selection did not complete. 18716 */ 18717 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18718 /* Note: Should this not just check the bit for pkt_state? */ 18719 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18720 (pktp->pkt_state != STATE_GOT_BUS)) { 18721 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18722 sd_reset_target(un, pktp); 18723 } 18724 18725 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18726 18727 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18728 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18729 } 18730 18731 18732 18733 /* 18734 * Function: sd_pkt_reason_cmd_reset 18735 * 18736 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18737 * 18738 * Context: May be called from interrupt context 18739 */ 18740 18741 static void 18742 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18743 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18744 { 18745 ASSERT(un != NULL); 18746 ASSERT(mutex_owned(SD_MUTEX(un))); 18747 ASSERT(bp != NULL); 18748 ASSERT(xp != NULL); 18749 ASSERT(pktp != NULL); 18750 18751 /* The target may still be running the command, so try to reset. */ 18752 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18753 sd_reset_target(un, pktp); 18754 18755 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18756 18757 /* 18758 * If pkt_reason is CMD_RESET chances are that this pkt got 18759 * reset because another target on this bus caused it. The target 18760 * that caused it should get CMD_TIMEOUT with pkt_statistics 18761 * of STAT_TIMEOUT/STAT_DEV_RESET. 18762 */ 18763 18764 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18765 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18766 } 18767 18768 18769 18770 18771 /* 18772 * Function: sd_pkt_reason_cmd_aborted 18773 * 18774 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18775 * 18776 * Context: May be called from interrupt context 18777 */ 18778 18779 static void 18780 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18781 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18782 { 18783 ASSERT(un != NULL); 18784 ASSERT(mutex_owned(SD_MUTEX(un))); 18785 ASSERT(bp != NULL); 18786 ASSERT(xp != NULL); 18787 ASSERT(pktp != NULL); 18788 18789 /* The target may still be running the command, so try to reset. */ 18790 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18791 sd_reset_target(un, pktp); 18792 18793 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18794 18795 /* 18796 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18797 * aborted because another target on this bus caused it. The target 18798 * that caused it should get CMD_TIMEOUT with pkt_statistics 18799 * of STAT_TIMEOUT/STAT_DEV_RESET. 18800 */ 18801 18802 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18803 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18804 } 18805 18806 18807 18808 /* 18809 * Function: sd_pkt_reason_cmd_timeout 18810 * 18811 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18812 * 18813 * Context: May be called from interrupt context 18814 */ 18815 18816 static void 18817 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18818 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18819 { 18820 ASSERT(un != NULL); 18821 ASSERT(mutex_owned(SD_MUTEX(un))); 18822 ASSERT(bp != NULL); 18823 ASSERT(xp != NULL); 18824 ASSERT(pktp != NULL); 18825 18826 18827 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18828 sd_reset_target(un, pktp); 18829 18830 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18831 18832 /* 18833 * A command timeout indicates that we could not establish 18834 * communication with the target, so set SD_RETRIES_FAILFAST 18835 * as further retries/commands are likely to take a long time. 18836 */ 18837 sd_retry_command(un, bp, 18838 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18839 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18840 } 18841 18842 18843 18844 /* 18845 * Function: sd_pkt_reason_cmd_unx_bus_free 18846 * 18847 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18848 * 18849 * Context: May be called from interrupt context 18850 */ 18851 18852 static void 18853 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18854 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18855 { 18856 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18857 18858 ASSERT(un != NULL); 18859 ASSERT(mutex_owned(SD_MUTEX(un))); 18860 ASSERT(bp != NULL); 18861 ASSERT(xp != NULL); 18862 ASSERT(pktp != NULL); 18863 18864 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18865 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18866 18867 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18868 sd_print_retry_msg : NULL; 18869 18870 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18871 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18872 } 18873 18874 18875 /* 18876 * Function: sd_pkt_reason_cmd_tag_reject 18877 * 18878 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18879 * 18880 * Context: May be called from interrupt context 18881 */ 18882 18883 static void 18884 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18885 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18886 { 18887 ASSERT(un != NULL); 18888 ASSERT(mutex_owned(SD_MUTEX(un))); 18889 ASSERT(bp != NULL); 18890 ASSERT(xp != NULL); 18891 ASSERT(pktp != NULL); 18892 18893 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18894 pktp->pkt_flags = 0; 18895 un->un_tagflags = 0; 18896 if (un->un_f_opt_queueing == TRUE) { 18897 un->un_throttle = min(un->un_throttle, 3); 18898 } else { 18899 un->un_throttle = 1; 18900 } 18901 mutex_exit(SD_MUTEX(un)); 18902 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18903 mutex_enter(SD_MUTEX(un)); 18904 18905 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18906 18907 /* Legacy behavior not to check retry counts here. */ 18908 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18909 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18910 } 18911 18912 18913 /* 18914 * Function: sd_pkt_reason_default 18915 * 18916 * Description: Default recovery actions for SCSA pkt_reason values that 18917 * do not have more explicit recovery actions. 18918 * 18919 * Context: May be called from interrupt context 18920 */ 18921 18922 static void 18923 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18924 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18925 { 18926 ASSERT(un != NULL); 18927 ASSERT(mutex_owned(SD_MUTEX(un))); 18928 ASSERT(bp != NULL); 18929 ASSERT(xp != NULL); 18930 ASSERT(pktp != NULL); 18931 18932 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18933 sd_reset_target(un, pktp); 18934 18935 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18936 18937 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18938 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18939 } 18940 18941 18942 18943 /* 18944 * Function: sd_pkt_status_check_condition 18945 * 18946 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18947 * 18948 * Context: May be called from interrupt context 18949 */ 18950 18951 static void 18952 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18953 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18954 { 18955 ASSERT(un != NULL); 18956 ASSERT(mutex_owned(SD_MUTEX(un))); 18957 ASSERT(bp != NULL); 18958 ASSERT(xp != NULL); 18959 ASSERT(pktp != NULL); 18960 18961 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18962 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18963 18964 /* 18965 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18966 * command will be retried after the request sense). Otherwise, retry 18967 * the command. Note: we are issuing the request sense even though the 18968 * retry limit may have been reached for the failed command. 18969 */ 18970 if (un->un_f_arq_enabled == FALSE) { 18971 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18972 "no ARQ, sending request sense command\n"); 18973 sd_send_request_sense_command(un, bp, pktp); 18974 } else { 18975 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18976 "ARQ,retrying request sense command\n"); 18977 #if defined(__i386) || defined(__amd64) 18978 /* 18979 * The SD_RETRY_DELAY value need to be adjusted here 18980 * when SD_RETRY_DELAY change in sddef.h 18981 */ 18982 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18983 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18984 NULL); 18985 #else 18986 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18987 EIO, SD_RETRY_DELAY, NULL); 18988 #endif 18989 } 18990 18991 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18992 } 18993 18994 18995 /* 18996 * Function: sd_pkt_status_busy 18997 * 18998 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 18999 * 19000 * Context: May be called from interrupt context 19001 */ 19002 19003 static void 19004 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 19005 struct scsi_pkt *pktp) 19006 { 19007 ASSERT(un != NULL); 19008 ASSERT(mutex_owned(SD_MUTEX(un))); 19009 ASSERT(bp != NULL); 19010 ASSERT(xp != NULL); 19011 ASSERT(pktp != NULL); 19012 19013 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19014 "sd_pkt_status_busy: entry\n"); 19015 19016 /* If retries are exhausted, just fail the command. */ 19017 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19018 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19019 "device busy too long\n"); 19020 sd_return_failed_command(un, bp, EIO); 19021 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19022 "sd_pkt_status_busy: exit\n"); 19023 return; 19024 } 19025 xp->xb_retry_count++; 19026 19027 /* 19028 * Try to reset the target. However, we do not want to perform 19029 * more than one reset if the device continues to fail. The reset 19030 * will be performed when the retry count reaches the reset 19031 * threshold. This threshold should be set such that at least 19032 * one retry is issued before the reset is performed. 19033 */ 19034 if (xp->xb_retry_count == 19035 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19036 int rval = 0; 19037 mutex_exit(SD_MUTEX(un)); 19038 if (un->un_f_allow_bus_device_reset == TRUE) { 19039 /* 19040 * First try to reset the LUN; if we cannot then 19041 * try to reset the target. 19042 */ 19043 if (un->un_f_lun_reset_enabled == TRUE) { 19044 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19045 "sd_pkt_status_busy: RESET_LUN\n"); 19046 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19047 } 19048 if (rval == 0) { 19049 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19050 "sd_pkt_status_busy: RESET_TARGET\n"); 19051 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19052 } 19053 } 19054 if (rval == 0) { 19055 /* 19056 * If the RESET_LUN and/or RESET_TARGET failed, 19057 * try RESET_ALL 19058 */ 19059 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19060 "sd_pkt_status_busy: RESET_ALL\n"); 19061 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19062 } 19063 mutex_enter(SD_MUTEX(un)); 19064 if (rval == 0) { 19065 /* 19066 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19067 * At this point we give up & fail the command. 19068 */ 19069 sd_return_failed_command(un, bp, EIO); 19070 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19071 "sd_pkt_status_busy: exit (failed cmd)\n"); 19072 return; 19073 } 19074 } 19075 19076 /* 19077 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19078 * we have already checked the retry counts above. 19079 */ 19080 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19081 EIO, SD_BSY_TIMEOUT, NULL); 19082 19083 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19084 "sd_pkt_status_busy: exit\n"); 19085 } 19086 19087 19088 /* 19089 * Function: sd_pkt_status_reservation_conflict 19090 * 19091 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19092 * command status. 19093 * 19094 * Context: May be called from interrupt context 19095 */ 19096 19097 static void 19098 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19099 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19100 { 19101 ASSERT(un != NULL); 19102 ASSERT(mutex_owned(SD_MUTEX(un))); 19103 ASSERT(bp != NULL); 19104 ASSERT(xp != NULL); 19105 ASSERT(pktp != NULL); 19106 19107 /* 19108 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19109 * conflict could be due to various reasons like incorrect keys, not 19110 * registered or not reserved etc. So, we return EACCES to the caller. 19111 */ 19112 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19113 int cmd = SD_GET_PKT_OPCODE(pktp); 19114 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19115 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19116 sd_return_failed_command(un, bp, EACCES); 19117 return; 19118 } 19119 } 19120 19121 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19122 19123 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19124 if (sd_failfast_enable != 0) { 19125 /* By definition, we must panic here.... */ 19126 sd_panic_for_res_conflict(un); 19127 /*NOTREACHED*/ 19128 } 19129 SD_ERROR(SD_LOG_IO, un, 19130 "sd_handle_resv_conflict: Disk Reserved\n"); 19131 sd_return_failed_command(un, bp, EACCES); 19132 return; 19133 } 19134 19135 /* 19136 * 1147670: retry only if sd_retry_on_reservation_conflict 19137 * property is set (default is 1). Retries will not succeed 19138 * on a disk reserved by another initiator. HA systems 19139 * may reset this via sd.conf to avoid these retries. 19140 * 19141 * Note: The legacy return code for this failure is EIO, however EACCES 19142 * seems more appropriate for a reservation conflict. 19143 */ 19144 if (sd_retry_on_reservation_conflict == 0) { 19145 SD_ERROR(SD_LOG_IO, un, 19146 "sd_handle_resv_conflict: Device Reserved\n"); 19147 sd_return_failed_command(un, bp, EIO); 19148 return; 19149 } 19150 19151 /* 19152 * Retry the command if we can. 19153 * 19154 * Note: The legacy return code for this failure is EIO, however EACCES 19155 * seems more appropriate for a reservation conflict. 19156 */ 19157 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19158 (clock_t)2, NULL); 19159 } 19160 19161 19162 19163 /* 19164 * Function: sd_pkt_status_qfull 19165 * 19166 * Description: Handle a QUEUE FULL condition from the target. This can 19167 * occur if the HBA does not handle the queue full condition. 19168 * (Basically this means third-party HBAs as Sun HBAs will 19169 * handle the queue full condition.) Note that if there are 19170 * some commands already in the transport, then the queue full 19171 * has occurred because the queue for this nexus is actually 19172 * full. If there are no commands in the transport, then the 19173 * queue full is resulting from some other initiator or lun 19174 * consuming all the resources at the target. 19175 * 19176 * Context: May be called from interrupt context 19177 */ 19178 19179 static void 19180 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19181 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19182 { 19183 ASSERT(un != NULL); 19184 ASSERT(mutex_owned(SD_MUTEX(un))); 19185 ASSERT(bp != NULL); 19186 ASSERT(xp != NULL); 19187 ASSERT(pktp != NULL); 19188 19189 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19190 "sd_pkt_status_qfull: entry\n"); 19191 19192 /* 19193 * Just lower the QFULL throttle and retry the command. Note that 19194 * we do not limit the number of retries here. 19195 */ 19196 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19197 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19198 SD_RESTART_TIMEOUT, NULL); 19199 19200 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19201 "sd_pkt_status_qfull: exit\n"); 19202 } 19203 19204 19205 /* 19206 * Function: sd_reset_target 19207 * 19208 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19209 * RESET_TARGET, or RESET_ALL. 19210 * 19211 * Context: May be called under interrupt context. 19212 */ 19213 19214 static void 19215 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19216 { 19217 int rval = 0; 19218 19219 ASSERT(un != NULL); 19220 ASSERT(mutex_owned(SD_MUTEX(un))); 19221 ASSERT(pktp != NULL); 19222 19223 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19224 19225 /* 19226 * No need to reset if the transport layer has already done so. 19227 */ 19228 if ((pktp->pkt_statistics & 19229 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19230 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19231 "sd_reset_target: no reset\n"); 19232 return; 19233 } 19234 19235 mutex_exit(SD_MUTEX(un)); 19236 19237 if (un->un_f_allow_bus_device_reset == TRUE) { 19238 if (un->un_f_lun_reset_enabled == TRUE) { 19239 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19240 "sd_reset_target: RESET_LUN\n"); 19241 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19242 } 19243 if (rval == 0) { 19244 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19245 "sd_reset_target: RESET_TARGET\n"); 19246 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19247 } 19248 } 19249 19250 if (rval == 0) { 19251 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19252 "sd_reset_target: RESET_ALL\n"); 19253 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19254 } 19255 19256 mutex_enter(SD_MUTEX(un)); 19257 19258 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19259 } 19260 19261 19262 /* 19263 * Function: sd_media_change_task 19264 * 19265 * Description: Recovery action for CDROM to become available. 19266 * 19267 * Context: Executes in a taskq() thread context 19268 */ 19269 19270 static void 19271 sd_media_change_task(void *arg) 19272 { 19273 struct scsi_pkt *pktp = arg; 19274 struct sd_lun *un; 19275 struct buf *bp; 19276 struct sd_xbuf *xp; 19277 int err = 0; 19278 int retry_count = 0; 19279 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19280 struct sd_sense_info si; 19281 19282 ASSERT(pktp != NULL); 19283 bp = (struct buf *)pktp->pkt_private; 19284 ASSERT(bp != NULL); 19285 xp = SD_GET_XBUF(bp); 19286 ASSERT(xp != NULL); 19287 un = SD_GET_UN(bp); 19288 ASSERT(un != NULL); 19289 ASSERT(!mutex_owned(SD_MUTEX(un))); 19290 ASSERT(un->un_f_monitor_media_state); 19291 19292 si.ssi_severity = SCSI_ERR_INFO; 19293 si.ssi_pfa_flag = FALSE; 19294 19295 /* 19296 * When a reset is issued on a CDROM, it takes a long time to 19297 * recover. First few attempts to read capacity and other things 19298 * related to handling unit attention fail (with a ASC 0x4 and 19299 * ASCQ 0x1). In that case we want to do enough retries and we want 19300 * to limit the retries in other cases of genuine failures like 19301 * no media in drive. 19302 */ 19303 while (retry_count++ < retry_limit) { 19304 if ((err = sd_handle_mchange(un)) == 0) { 19305 break; 19306 } 19307 if (err == EAGAIN) { 19308 retry_limit = SD_UNIT_ATTENTION_RETRY; 19309 } 19310 /* Sleep for 0.5 sec. & try again */ 19311 delay(drv_usectohz(500000)); 19312 } 19313 19314 /* 19315 * Dispatch (retry or fail) the original command here, 19316 * along with appropriate console messages.... 19317 * 19318 * Must grab the mutex before calling sd_retry_command, 19319 * sd_print_sense_msg and sd_return_failed_command. 19320 */ 19321 mutex_enter(SD_MUTEX(un)); 19322 if (err != SD_CMD_SUCCESS) { 19323 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19324 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19325 si.ssi_severity = SCSI_ERR_FATAL; 19326 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19327 sd_return_failed_command(un, bp, EIO); 19328 } else { 19329 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19330 &si, EIO, (clock_t)0, NULL); 19331 } 19332 mutex_exit(SD_MUTEX(un)); 19333 } 19334 19335 19336 19337 /* 19338 * Function: sd_handle_mchange 19339 * 19340 * Description: Perform geometry validation & other recovery when CDROM 19341 * has been removed from drive. 19342 * 19343 * Return Code: 0 for success 19344 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19345 * sd_send_scsi_READ_CAPACITY() 19346 * 19347 * Context: Executes in a taskq() thread context 19348 */ 19349 19350 static int 19351 sd_handle_mchange(struct sd_lun *un) 19352 { 19353 uint64_t capacity; 19354 uint32_t lbasize; 19355 int rval; 19356 19357 ASSERT(!mutex_owned(SD_MUTEX(un))); 19358 ASSERT(un->un_f_monitor_media_state); 19359 19360 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 19361 SD_PATH_DIRECT_PRIORITY)) != 0) { 19362 return (rval); 19363 } 19364 19365 mutex_enter(SD_MUTEX(un)); 19366 sd_update_block_info(un, lbasize, capacity); 19367 19368 if (un->un_errstats != NULL) { 19369 struct sd_errstats *stp = 19370 (struct sd_errstats *)un->un_errstats->ks_data; 19371 stp->sd_capacity.value.ui64 = (uint64_t) 19372 ((uint64_t)un->un_blockcount * 19373 (uint64_t)un->un_tgt_blocksize); 19374 } 19375 19376 /* 19377 * Note: Maybe let the strategy/partitioning chain worry about getting 19378 * valid geometry. 19379 */ 19380 un->un_f_geometry_is_valid = FALSE; 19381 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 19382 if (un->un_f_geometry_is_valid == FALSE) { 19383 mutex_exit(SD_MUTEX(un)); 19384 return (EIO); 19385 } 19386 19387 mutex_exit(SD_MUTEX(un)); 19388 19389 /* 19390 * Try to lock the door 19391 */ 19392 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19393 SD_PATH_DIRECT_PRIORITY)); 19394 } 19395 19396 19397 /* 19398 * Function: sd_send_scsi_DOORLOCK 19399 * 19400 * Description: Issue the scsi DOOR LOCK command 19401 * 19402 * Arguments: un - pointer to driver soft state (unit) structure for 19403 * this target. 19404 * flag - SD_REMOVAL_ALLOW 19405 * SD_REMOVAL_PREVENT 19406 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19407 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19408 * to use the USCSI "direct" chain and bypass the normal 19409 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19410 * command is issued as part of an error recovery action. 19411 * 19412 * Return Code: 0 - Success 19413 * errno return code from sd_send_scsi_cmd() 19414 * 19415 * Context: Can sleep. 19416 */ 19417 19418 static int 19419 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19420 { 19421 union scsi_cdb cdb; 19422 struct uscsi_cmd ucmd_buf; 19423 struct scsi_extended_sense sense_buf; 19424 int status; 19425 19426 ASSERT(un != NULL); 19427 ASSERT(!mutex_owned(SD_MUTEX(un))); 19428 19429 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19430 19431 /* already determined doorlock is not supported, fake success */ 19432 if (un->un_f_doorlock_supported == FALSE) { 19433 return (0); 19434 } 19435 19436 bzero(&cdb, sizeof (cdb)); 19437 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19438 19439 cdb.scc_cmd = SCMD_DOORLOCK; 19440 cdb.cdb_opaque[4] = (uchar_t)flag; 19441 19442 ucmd_buf.uscsi_cdb = (char *)&cdb; 19443 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19444 ucmd_buf.uscsi_bufaddr = NULL; 19445 ucmd_buf.uscsi_buflen = 0; 19446 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19447 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19448 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19449 ucmd_buf.uscsi_timeout = 15; 19450 19451 SD_TRACE(SD_LOG_IO, un, 19452 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19453 19454 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19455 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19456 19457 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19458 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19459 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19460 /* fake success and skip subsequent doorlock commands */ 19461 un->un_f_doorlock_supported = FALSE; 19462 return (0); 19463 } 19464 19465 return (status); 19466 } 19467 19468 /* 19469 * Function: sd_send_scsi_READ_CAPACITY 19470 * 19471 * Description: This routine uses the scsi READ CAPACITY command to determine 19472 * the device capacity in number of blocks and the device native 19473 * block size. If this function returns a failure, then the 19474 * values in *capp and *lbap are undefined. If the capacity 19475 * returned is 0xffffffff then the lun is too large for a 19476 * normal READ CAPACITY command and the results of a 19477 * READ CAPACITY 16 will be used instead. 19478 * 19479 * Arguments: un - ptr to soft state struct for the target 19480 * capp - ptr to unsigned 64-bit variable to receive the 19481 * capacity value from the command. 19482 * lbap - ptr to unsigned 32-bit varaible to receive the 19483 * block size value from the command 19484 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19485 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19486 * to use the USCSI "direct" chain and bypass the normal 19487 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19488 * command is issued as part of an error recovery action. 19489 * 19490 * Return Code: 0 - Success 19491 * EIO - IO error 19492 * EACCES - Reservation conflict detected 19493 * EAGAIN - Device is becoming ready 19494 * errno return code from sd_send_scsi_cmd() 19495 * 19496 * Context: Can sleep. Blocks until command completes. 19497 */ 19498 19499 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19500 19501 static int 19502 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19503 int path_flag) 19504 { 19505 struct scsi_extended_sense sense_buf; 19506 struct uscsi_cmd ucmd_buf; 19507 union scsi_cdb cdb; 19508 uint32_t *capacity_buf; 19509 uint64_t capacity; 19510 uint32_t lbasize; 19511 int status; 19512 19513 ASSERT(un != NULL); 19514 ASSERT(!mutex_owned(SD_MUTEX(un))); 19515 ASSERT(capp != NULL); 19516 ASSERT(lbap != NULL); 19517 19518 SD_TRACE(SD_LOG_IO, un, 19519 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19520 19521 /* 19522 * First send a READ_CAPACITY command to the target. 19523 * (This command is mandatory under SCSI-2.) 19524 * 19525 * Set up the CDB for the READ_CAPACITY command. The Partial 19526 * Medium Indicator bit is cleared. The address field must be 19527 * zero if the PMI bit is zero. 19528 */ 19529 bzero(&cdb, sizeof (cdb)); 19530 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19531 19532 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19533 19534 cdb.scc_cmd = SCMD_READ_CAPACITY; 19535 19536 ucmd_buf.uscsi_cdb = (char *)&cdb; 19537 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19538 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19539 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19540 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19541 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19542 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19543 ucmd_buf.uscsi_timeout = 60; 19544 19545 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19546 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19547 19548 switch (status) { 19549 case 0: 19550 /* Return failure if we did not get valid capacity data. */ 19551 if (ucmd_buf.uscsi_resid != 0) { 19552 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19553 return (EIO); 19554 } 19555 19556 /* 19557 * Read capacity and block size from the READ CAPACITY 10 data. 19558 * This data may be adjusted later due to device specific 19559 * issues. 19560 * 19561 * According to the SCSI spec, the READ CAPACITY 10 19562 * command returns the following: 19563 * 19564 * bytes 0-3: Maximum logical block address available. 19565 * (MSB in byte:0 & LSB in byte:3) 19566 * 19567 * bytes 4-7: Block length in bytes 19568 * (MSB in byte:4 & LSB in byte:7) 19569 * 19570 */ 19571 capacity = BE_32(capacity_buf[0]); 19572 lbasize = BE_32(capacity_buf[1]); 19573 19574 /* 19575 * Done with capacity_buf 19576 */ 19577 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19578 19579 /* 19580 * if the reported capacity is set to all 0xf's, then 19581 * this disk is too large and requires SBC-2 commands. 19582 * Reissue the request using READ CAPACITY 16. 19583 */ 19584 if (capacity == 0xffffffff) { 19585 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19586 &lbasize, path_flag); 19587 if (status != 0) { 19588 return (status); 19589 } 19590 } 19591 break; /* Success! */ 19592 case EIO: 19593 switch (ucmd_buf.uscsi_status) { 19594 case STATUS_RESERVATION_CONFLICT: 19595 status = EACCES; 19596 break; 19597 case STATUS_CHECK: 19598 /* 19599 * Check condition; look for ASC/ASCQ of 0x04/0x01 19600 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19601 */ 19602 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19603 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19604 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19605 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19606 return (EAGAIN); 19607 } 19608 break; 19609 default: 19610 break; 19611 } 19612 /* FALLTHRU */ 19613 default: 19614 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19615 return (status); 19616 } 19617 19618 /* 19619 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19620 * (2352 and 0 are common) so for these devices always force the value 19621 * to 2048 as required by the ATAPI specs. 19622 */ 19623 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19624 lbasize = 2048; 19625 } 19626 19627 /* 19628 * Get the maximum LBA value from the READ CAPACITY data. 19629 * Here we assume that the Partial Medium Indicator (PMI) bit 19630 * was cleared when issuing the command. This means that the LBA 19631 * returned from the device is the LBA of the last logical block 19632 * on the logical unit. The actual logical block count will be 19633 * this value plus one. 19634 * 19635 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19636 * so scale the capacity value to reflect this. 19637 */ 19638 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19639 19640 #if defined(__i386) || defined(__amd64) 19641 /* 19642 * Refer to comments related to off-by-1 at the 19643 * header of this file. 19644 * Treat 1TB disk as (1T - 512)B. 19645 */ 19646 if (un->un_f_capacity_adjusted == 1) 19647 capacity = DK_MAX_BLOCKS; 19648 #endif 19649 19650 /* 19651 * Copy the values from the READ CAPACITY command into the space 19652 * provided by the caller. 19653 */ 19654 *capp = capacity; 19655 *lbap = lbasize; 19656 19657 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19658 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19659 19660 /* 19661 * Both the lbasize and capacity from the device must be nonzero, 19662 * otherwise we assume that the values are not valid and return 19663 * failure to the caller. (4203735) 19664 */ 19665 if ((capacity == 0) || (lbasize == 0)) { 19666 return (EIO); 19667 } 19668 19669 return (0); 19670 } 19671 19672 /* 19673 * Function: sd_send_scsi_READ_CAPACITY_16 19674 * 19675 * Description: This routine uses the scsi READ CAPACITY 16 command to 19676 * determine the device capacity in number of blocks and the 19677 * device native block size. If this function returns a failure, 19678 * then the values in *capp and *lbap are undefined. 19679 * This routine should always be called by 19680 * sd_send_scsi_READ_CAPACITY which will appy any device 19681 * specific adjustments to capacity and lbasize. 19682 * 19683 * Arguments: un - ptr to soft state struct for the target 19684 * capp - ptr to unsigned 64-bit variable to receive the 19685 * capacity value from the command. 19686 * lbap - ptr to unsigned 32-bit varaible to receive the 19687 * block size value from the command 19688 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19689 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19690 * to use the USCSI "direct" chain and bypass the normal 19691 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19692 * this command is issued as part of an error recovery 19693 * action. 19694 * 19695 * Return Code: 0 - Success 19696 * EIO - IO error 19697 * EACCES - Reservation conflict detected 19698 * EAGAIN - Device is becoming ready 19699 * errno return code from sd_send_scsi_cmd() 19700 * 19701 * Context: Can sleep. Blocks until command completes. 19702 */ 19703 19704 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19705 19706 static int 19707 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19708 uint32_t *lbap, int path_flag) 19709 { 19710 struct scsi_extended_sense sense_buf; 19711 struct uscsi_cmd ucmd_buf; 19712 union scsi_cdb cdb; 19713 uint64_t *capacity16_buf; 19714 uint64_t capacity; 19715 uint32_t lbasize; 19716 int status; 19717 19718 ASSERT(un != NULL); 19719 ASSERT(!mutex_owned(SD_MUTEX(un))); 19720 ASSERT(capp != NULL); 19721 ASSERT(lbap != NULL); 19722 19723 SD_TRACE(SD_LOG_IO, un, 19724 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19725 19726 /* 19727 * First send a READ_CAPACITY_16 command to the target. 19728 * 19729 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19730 * Medium Indicator bit is cleared. The address field must be 19731 * zero if the PMI bit is zero. 19732 */ 19733 bzero(&cdb, sizeof (cdb)); 19734 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19735 19736 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19737 19738 ucmd_buf.uscsi_cdb = (char *)&cdb; 19739 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19740 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19741 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19742 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19743 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19744 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19745 ucmd_buf.uscsi_timeout = 60; 19746 19747 /* 19748 * Read Capacity (16) is a Service Action In command. One 19749 * command byte (0x9E) is overloaded for multiple operations, 19750 * with the second CDB byte specifying the desired operation 19751 */ 19752 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19753 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19754 19755 /* 19756 * Fill in allocation length field 19757 */ 19758 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19759 19760 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19761 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19762 19763 switch (status) { 19764 case 0: 19765 /* Return failure if we did not get valid capacity data. */ 19766 if (ucmd_buf.uscsi_resid > 20) { 19767 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19768 return (EIO); 19769 } 19770 19771 /* 19772 * Read capacity and block size from the READ CAPACITY 10 data. 19773 * This data may be adjusted later due to device specific 19774 * issues. 19775 * 19776 * According to the SCSI spec, the READ CAPACITY 10 19777 * command returns the following: 19778 * 19779 * bytes 0-7: Maximum logical block address available. 19780 * (MSB in byte:0 & LSB in byte:7) 19781 * 19782 * bytes 8-11: Block length in bytes 19783 * (MSB in byte:8 & LSB in byte:11) 19784 * 19785 */ 19786 capacity = BE_64(capacity16_buf[0]); 19787 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19788 19789 /* 19790 * Done with capacity16_buf 19791 */ 19792 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19793 19794 /* 19795 * if the reported capacity is set to all 0xf's, then 19796 * this disk is too large. This could only happen with 19797 * a device that supports LBAs larger than 64 bits which 19798 * are not defined by any current T10 standards. 19799 */ 19800 if (capacity == 0xffffffffffffffff) { 19801 return (EIO); 19802 } 19803 break; /* Success! */ 19804 case EIO: 19805 switch (ucmd_buf.uscsi_status) { 19806 case STATUS_RESERVATION_CONFLICT: 19807 status = EACCES; 19808 break; 19809 case STATUS_CHECK: 19810 /* 19811 * Check condition; look for ASC/ASCQ of 0x04/0x01 19812 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19813 */ 19814 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19815 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19816 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19817 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19818 return (EAGAIN); 19819 } 19820 break; 19821 default: 19822 break; 19823 } 19824 /* FALLTHRU */ 19825 default: 19826 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19827 return (status); 19828 } 19829 19830 *capp = capacity; 19831 *lbap = lbasize; 19832 19833 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19834 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19835 19836 return (0); 19837 } 19838 19839 19840 /* 19841 * Function: sd_send_scsi_START_STOP_UNIT 19842 * 19843 * Description: Issue a scsi START STOP UNIT command to the target. 19844 * 19845 * Arguments: un - pointer to driver soft state (unit) structure for 19846 * this target. 19847 * flag - SD_TARGET_START 19848 * SD_TARGET_STOP 19849 * SD_TARGET_EJECT 19850 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19851 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19852 * to use the USCSI "direct" chain and bypass the normal 19853 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19854 * command is issued as part of an error recovery action. 19855 * 19856 * Return Code: 0 - Success 19857 * EIO - IO error 19858 * EACCES - Reservation conflict detected 19859 * ENXIO - Not Ready, medium not present 19860 * errno return code from sd_send_scsi_cmd() 19861 * 19862 * Context: Can sleep. 19863 */ 19864 19865 static int 19866 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19867 { 19868 struct scsi_extended_sense sense_buf; 19869 union scsi_cdb cdb; 19870 struct uscsi_cmd ucmd_buf; 19871 int status; 19872 19873 ASSERT(un != NULL); 19874 ASSERT(!mutex_owned(SD_MUTEX(un))); 19875 19876 SD_TRACE(SD_LOG_IO, un, 19877 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19878 19879 if (un->un_f_check_start_stop && 19880 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19881 (un->un_f_start_stop_supported != TRUE)) { 19882 return (0); 19883 } 19884 19885 bzero(&cdb, sizeof (cdb)); 19886 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19887 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19888 19889 cdb.scc_cmd = SCMD_START_STOP; 19890 cdb.cdb_opaque[4] = (uchar_t)flag; 19891 19892 ucmd_buf.uscsi_cdb = (char *)&cdb; 19893 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19894 ucmd_buf.uscsi_bufaddr = NULL; 19895 ucmd_buf.uscsi_buflen = 0; 19896 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19897 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19898 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19899 ucmd_buf.uscsi_timeout = 200; 19900 19901 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19902 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19903 19904 switch (status) { 19905 case 0: 19906 break; /* Success! */ 19907 case EIO: 19908 switch (ucmd_buf.uscsi_status) { 19909 case STATUS_RESERVATION_CONFLICT: 19910 status = EACCES; 19911 break; 19912 case STATUS_CHECK: 19913 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19914 switch (scsi_sense_key( 19915 (uint8_t *)&sense_buf)) { 19916 case KEY_ILLEGAL_REQUEST: 19917 status = ENOTSUP; 19918 break; 19919 case KEY_NOT_READY: 19920 if (scsi_sense_asc( 19921 (uint8_t *)&sense_buf) 19922 == 0x3A) { 19923 status = ENXIO; 19924 } 19925 break; 19926 default: 19927 break; 19928 } 19929 } 19930 break; 19931 default: 19932 break; 19933 } 19934 break; 19935 default: 19936 break; 19937 } 19938 19939 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19940 19941 return (status); 19942 } 19943 19944 19945 /* 19946 * Function: sd_start_stop_unit_callback 19947 * 19948 * Description: timeout(9F) callback to begin recovery process for a 19949 * device that has spun down. 19950 * 19951 * Arguments: arg - pointer to associated softstate struct. 19952 * 19953 * Context: Executes in a timeout(9F) thread context 19954 */ 19955 19956 static void 19957 sd_start_stop_unit_callback(void *arg) 19958 { 19959 struct sd_lun *un = arg; 19960 ASSERT(un != NULL); 19961 ASSERT(!mutex_owned(SD_MUTEX(un))); 19962 19963 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19964 19965 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19966 } 19967 19968 19969 /* 19970 * Function: sd_start_stop_unit_task 19971 * 19972 * Description: Recovery procedure when a drive is spun down. 19973 * 19974 * Arguments: arg - pointer to associated softstate struct. 19975 * 19976 * Context: Executes in a taskq() thread context 19977 */ 19978 19979 static void 19980 sd_start_stop_unit_task(void *arg) 19981 { 19982 struct sd_lun *un = arg; 19983 19984 ASSERT(un != NULL); 19985 ASSERT(!mutex_owned(SD_MUTEX(un))); 19986 19987 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19988 19989 /* 19990 * Some unformatted drives report not ready error, no need to 19991 * restart if format has been initiated. 19992 */ 19993 mutex_enter(SD_MUTEX(un)); 19994 if (un->un_f_format_in_progress == TRUE) { 19995 mutex_exit(SD_MUTEX(un)); 19996 return; 19997 } 19998 mutex_exit(SD_MUTEX(un)); 19999 20000 /* 20001 * When a START STOP command is issued from here, it is part of a 20002 * failure recovery operation and must be issued before any other 20003 * commands, including any pending retries. Thus it must be sent 20004 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 20005 * succeeds or not, we will start I/O after the attempt. 20006 */ 20007 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 20008 SD_PATH_DIRECT_PRIORITY); 20009 20010 /* 20011 * The above call blocks until the START_STOP_UNIT command completes. 20012 * Now that it has completed, we must re-try the original IO that 20013 * received the NOT READY condition in the first place. There are 20014 * three possible conditions here: 20015 * 20016 * (1) The original IO is on un_retry_bp. 20017 * (2) The original IO is on the regular wait queue, and un_retry_bp 20018 * is NULL. 20019 * (3) The original IO is on the regular wait queue, and un_retry_bp 20020 * points to some other, unrelated bp. 20021 * 20022 * For each case, we must call sd_start_cmds() with un_retry_bp 20023 * as the argument. If un_retry_bp is NULL, this will initiate 20024 * processing of the regular wait queue. If un_retry_bp is not NULL, 20025 * then this will process the bp on un_retry_bp. That may or may not 20026 * be the original IO, but that does not matter: the important thing 20027 * is to keep the IO processing going at this point. 20028 * 20029 * Note: This is a very specific error recovery sequence associated 20030 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20031 * serialize the I/O with completion of the spin-up. 20032 */ 20033 mutex_enter(SD_MUTEX(un)); 20034 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20035 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20036 un, un->un_retry_bp); 20037 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20038 sd_start_cmds(un, un->un_retry_bp); 20039 mutex_exit(SD_MUTEX(un)); 20040 20041 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20042 } 20043 20044 20045 /* 20046 * Function: sd_send_scsi_INQUIRY 20047 * 20048 * Description: Issue the scsi INQUIRY command. 20049 * 20050 * Arguments: un 20051 * bufaddr 20052 * buflen 20053 * evpd 20054 * page_code 20055 * page_length 20056 * 20057 * Return Code: 0 - Success 20058 * errno return code from sd_send_scsi_cmd() 20059 * 20060 * Context: Can sleep. Does not return until command is completed. 20061 */ 20062 20063 static int 20064 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 20065 uchar_t evpd, uchar_t page_code, size_t *residp) 20066 { 20067 union scsi_cdb cdb; 20068 struct uscsi_cmd ucmd_buf; 20069 int status; 20070 20071 ASSERT(un != NULL); 20072 ASSERT(!mutex_owned(SD_MUTEX(un))); 20073 ASSERT(bufaddr != NULL); 20074 20075 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20076 20077 bzero(&cdb, sizeof (cdb)); 20078 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20079 bzero(bufaddr, buflen); 20080 20081 cdb.scc_cmd = SCMD_INQUIRY; 20082 cdb.cdb_opaque[1] = evpd; 20083 cdb.cdb_opaque[2] = page_code; 20084 FORMG0COUNT(&cdb, buflen); 20085 20086 ucmd_buf.uscsi_cdb = (char *)&cdb; 20087 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20088 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20089 ucmd_buf.uscsi_buflen = buflen; 20090 ucmd_buf.uscsi_rqbuf = NULL; 20091 ucmd_buf.uscsi_rqlen = 0; 20092 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20093 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20094 20095 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20096 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 20097 20098 if ((status == 0) && (residp != NULL)) { 20099 *residp = ucmd_buf.uscsi_resid; 20100 } 20101 20102 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20103 20104 return (status); 20105 } 20106 20107 20108 /* 20109 * Function: sd_send_scsi_TEST_UNIT_READY 20110 * 20111 * Description: Issue the scsi TEST UNIT READY command. 20112 * This routine can be told to set the flag USCSI_DIAGNOSE to 20113 * prevent retrying failed commands. Use this when the intent 20114 * is either to check for device readiness, to clear a Unit 20115 * Attention, or to clear any outstanding sense data. 20116 * However under specific conditions the expected behavior 20117 * is for retries to bring a device ready, so use the flag 20118 * with caution. 20119 * 20120 * Arguments: un 20121 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20122 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20123 * 0: dont check for media present, do retries on cmd. 20124 * 20125 * Return Code: 0 - Success 20126 * EIO - IO error 20127 * EACCES - Reservation conflict detected 20128 * ENXIO - Not Ready, medium not present 20129 * errno return code from sd_send_scsi_cmd() 20130 * 20131 * Context: Can sleep. Does not return until command is completed. 20132 */ 20133 20134 static int 20135 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 20136 { 20137 struct scsi_extended_sense sense_buf; 20138 union scsi_cdb cdb; 20139 struct uscsi_cmd ucmd_buf; 20140 int status; 20141 20142 ASSERT(un != NULL); 20143 ASSERT(!mutex_owned(SD_MUTEX(un))); 20144 20145 SD_TRACE(SD_LOG_IO, un, 20146 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20147 20148 /* 20149 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20150 * timeouts when they receive a TUR and the queue is not empty. Check 20151 * the configuration flag set during attach (indicating the drive has 20152 * this firmware bug) and un_ncmds_in_transport before issuing the 20153 * TUR. If there are 20154 * pending commands return success, this is a bit arbitrary but is ok 20155 * for non-removables (i.e. the eliteI disks) and non-clustering 20156 * configurations. 20157 */ 20158 if (un->un_f_cfg_tur_check == TRUE) { 20159 mutex_enter(SD_MUTEX(un)); 20160 if (un->un_ncmds_in_transport != 0) { 20161 mutex_exit(SD_MUTEX(un)); 20162 return (0); 20163 } 20164 mutex_exit(SD_MUTEX(un)); 20165 } 20166 20167 bzero(&cdb, sizeof (cdb)); 20168 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20169 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20170 20171 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20172 20173 ucmd_buf.uscsi_cdb = (char *)&cdb; 20174 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20175 ucmd_buf.uscsi_bufaddr = NULL; 20176 ucmd_buf.uscsi_buflen = 0; 20177 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20178 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20179 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20180 20181 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20182 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20183 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20184 } 20185 ucmd_buf.uscsi_timeout = 60; 20186 20187 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20188 UIO_SYSSPACE, UIO_SYSSPACE, 20189 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 20190 20191 switch (status) { 20192 case 0: 20193 break; /* Success! */ 20194 case EIO: 20195 switch (ucmd_buf.uscsi_status) { 20196 case STATUS_RESERVATION_CONFLICT: 20197 status = EACCES; 20198 break; 20199 case STATUS_CHECK: 20200 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20201 break; 20202 } 20203 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20204 (scsi_sense_key((uint8_t *)&sense_buf) == 20205 KEY_NOT_READY) && 20206 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20207 status = ENXIO; 20208 } 20209 break; 20210 default: 20211 break; 20212 } 20213 break; 20214 default: 20215 break; 20216 } 20217 20218 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20219 20220 return (status); 20221 } 20222 20223 20224 /* 20225 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20226 * 20227 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20228 * 20229 * Arguments: un 20230 * 20231 * Return Code: 0 - Success 20232 * EACCES 20233 * ENOTSUP 20234 * errno return code from sd_send_scsi_cmd() 20235 * 20236 * Context: Can sleep. Does not return until command is completed. 20237 */ 20238 20239 static int 20240 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 20241 uint16_t data_len, uchar_t *data_bufp) 20242 { 20243 struct scsi_extended_sense sense_buf; 20244 union scsi_cdb cdb; 20245 struct uscsi_cmd ucmd_buf; 20246 int status; 20247 int no_caller_buf = FALSE; 20248 20249 ASSERT(un != NULL); 20250 ASSERT(!mutex_owned(SD_MUTEX(un))); 20251 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20252 20253 SD_TRACE(SD_LOG_IO, un, 20254 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20255 20256 bzero(&cdb, sizeof (cdb)); 20257 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20258 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20259 if (data_bufp == NULL) { 20260 /* Allocate a default buf if the caller did not give one */ 20261 ASSERT(data_len == 0); 20262 data_len = MHIOC_RESV_KEY_SIZE; 20263 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20264 no_caller_buf = TRUE; 20265 } 20266 20267 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20268 cdb.cdb_opaque[1] = usr_cmd; 20269 FORMG1COUNT(&cdb, data_len); 20270 20271 ucmd_buf.uscsi_cdb = (char *)&cdb; 20272 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20273 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20274 ucmd_buf.uscsi_buflen = data_len; 20275 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20276 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20277 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20278 ucmd_buf.uscsi_timeout = 60; 20279 20280 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20281 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20282 20283 switch (status) { 20284 case 0: 20285 break; /* Success! */ 20286 case EIO: 20287 switch (ucmd_buf.uscsi_status) { 20288 case STATUS_RESERVATION_CONFLICT: 20289 status = EACCES; 20290 break; 20291 case STATUS_CHECK: 20292 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20293 (scsi_sense_key((uint8_t *)&sense_buf) == 20294 KEY_ILLEGAL_REQUEST)) { 20295 status = ENOTSUP; 20296 } 20297 break; 20298 default: 20299 break; 20300 } 20301 break; 20302 default: 20303 break; 20304 } 20305 20306 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20307 20308 if (no_caller_buf == TRUE) { 20309 kmem_free(data_bufp, data_len); 20310 } 20311 20312 return (status); 20313 } 20314 20315 20316 /* 20317 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20318 * 20319 * Description: This routine is the driver entry point for handling CD-ROM 20320 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20321 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20322 * device. 20323 * 20324 * Arguments: un - Pointer to soft state struct for the target. 20325 * usr_cmd SCSI-3 reservation facility command (one of 20326 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20327 * SD_SCSI3_PREEMPTANDABORT) 20328 * usr_bufp - user provided pointer register, reserve descriptor or 20329 * preempt and abort structure (mhioc_register_t, 20330 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20331 * 20332 * Return Code: 0 - Success 20333 * EACCES 20334 * ENOTSUP 20335 * errno return code from sd_send_scsi_cmd() 20336 * 20337 * Context: Can sleep. Does not return until command is completed. 20338 */ 20339 20340 static int 20341 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 20342 uchar_t *usr_bufp) 20343 { 20344 struct scsi_extended_sense sense_buf; 20345 union scsi_cdb cdb; 20346 struct uscsi_cmd ucmd_buf; 20347 int status; 20348 uchar_t data_len = sizeof (sd_prout_t); 20349 sd_prout_t *prp; 20350 20351 ASSERT(un != NULL); 20352 ASSERT(!mutex_owned(SD_MUTEX(un))); 20353 ASSERT(data_len == 24); /* required by scsi spec */ 20354 20355 SD_TRACE(SD_LOG_IO, un, 20356 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20357 20358 if (usr_bufp == NULL) { 20359 return (EINVAL); 20360 } 20361 20362 bzero(&cdb, sizeof (cdb)); 20363 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20364 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20365 prp = kmem_zalloc(data_len, KM_SLEEP); 20366 20367 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20368 cdb.cdb_opaque[1] = usr_cmd; 20369 FORMG1COUNT(&cdb, data_len); 20370 20371 ucmd_buf.uscsi_cdb = (char *)&cdb; 20372 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20373 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20374 ucmd_buf.uscsi_buflen = data_len; 20375 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20376 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20377 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20378 ucmd_buf.uscsi_timeout = 60; 20379 20380 switch (usr_cmd) { 20381 case SD_SCSI3_REGISTER: { 20382 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20383 20384 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20385 bcopy(ptr->newkey.key, prp->service_key, 20386 MHIOC_RESV_KEY_SIZE); 20387 prp->aptpl = ptr->aptpl; 20388 break; 20389 } 20390 case SD_SCSI3_RESERVE: 20391 case SD_SCSI3_RELEASE: { 20392 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20393 20394 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20395 prp->scope_address = BE_32(ptr->scope_specific_addr); 20396 cdb.cdb_opaque[2] = ptr->type; 20397 break; 20398 } 20399 case SD_SCSI3_PREEMPTANDABORT: { 20400 mhioc_preemptandabort_t *ptr = 20401 (mhioc_preemptandabort_t *)usr_bufp; 20402 20403 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20404 bcopy(ptr->victim_key.key, prp->service_key, 20405 MHIOC_RESV_KEY_SIZE); 20406 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20407 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20408 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20409 break; 20410 } 20411 case SD_SCSI3_REGISTERANDIGNOREKEY: 20412 { 20413 mhioc_registerandignorekey_t *ptr; 20414 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20415 bcopy(ptr->newkey.key, 20416 prp->service_key, MHIOC_RESV_KEY_SIZE); 20417 prp->aptpl = ptr->aptpl; 20418 break; 20419 } 20420 default: 20421 ASSERT(FALSE); 20422 break; 20423 } 20424 20425 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20426 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20427 20428 switch (status) { 20429 case 0: 20430 break; /* Success! */ 20431 case EIO: 20432 switch (ucmd_buf.uscsi_status) { 20433 case STATUS_RESERVATION_CONFLICT: 20434 status = EACCES; 20435 break; 20436 case STATUS_CHECK: 20437 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20438 (scsi_sense_key((uint8_t *)&sense_buf) == 20439 KEY_ILLEGAL_REQUEST)) { 20440 status = ENOTSUP; 20441 } 20442 break; 20443 default: 20444 break; 20445 } 20446 break; 20447 default: 20448 break; 20449 } 20450 20451 kmem_free(prp, data_len); 20452 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20453 return (status); 20454 } 20455 20456 20457 /* 20458 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20459 * 20460 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20461 * 20462 * Arguments: un - pointer to the target's soft state struct 20463 * 20464 * Return Code: 0 - success 20465 * errno-type error code 20466 * 20467 * Context: kernel thread context only. 20468 */ 20469 20470 static int 20471 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20472 { 20473 struct sd_uscsi_info *uip; 20474 struct uscsi_cmd *uscmd; 20475 union scsi_cdb *cdb; 20476 struct buf *bp; 20477 int rval = 0; 20478 20479 SD_TRACE(SD_LOG_IO, un, 20480 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20481 20482 ASSERT(un != NULL); 20483 ASSERT(!mutex_owned(SD_MUTEX(un))); 20484 20485 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20486 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20487 20488 /* 20489 * First get some memory for the uscsi_cmd struct and cdb 20490 * and initialize for SYNCHRONIZE_CACHE cmd. 20491 */ 20492 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20493 uscmd->uscsi_cdblen = CDB_GROUP1; 20494 uscmd->uscsi_cdb = (caddr_t)cdb; 20495 uscmd->uscsi_bufaddr = NULL; 20496 uscmd->uscsi_buflen = 0; 20497 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20498 uscmd->uscsi_rqlen = SENSE_LENGTH; 20499 uscmd->uscsi_rqresid = SENSE_LENGTH; 20500 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20501 uscmd->uscsi_timeout = sd_io_time; 20502 20503 /* 20504 * Allocate an sd_uscsi_info struct and fill it with the info 20505 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20506 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20507 * since we allocate the buf here in this function, we do not 20508 * need to preserve the prior contents of b_private. 20509 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20510 */ 20511 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20512 uip->ui_flags = SD_PATH_DIRECT; 20513 uip->ui_cmdp = uscmd; 20514 20515 bp = getrbuf(KM_SLEEP); 20516 bp->b_private = uip; 20517 20518 /* 20519 * Setup buffer to carry uscsi request. 20520 */ 20521 bp->b_flags = B_BUSY; 20522 bp->b_bcount = 0; 20523 bp->b_blkno = 0; 20524 20525 if (dkc != NULL) { 20526 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20527 uip->ui_dkc = *dkc; 20528 } 20529 20530 bp->b_edev = SD_GET_DEV(un); 20531 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20532 20533 (void) sd_uscsi_strategy(bp); 20534 20535 /* 20536 * If synchronous request, wait for completion 20537 * If async just return and let b_iodone callback 20538 * cleanup. 20539 * NOTE: On return, u_ncmds_in_driver will be decremented, 20540 * but it was also incremented in sd_uscsi_strategy(), so 20541 * we should be ok. 20542 */ 20543 if (dkc == NULL) { 20544 (void) biowait(bp); 20545 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20546 } 20547 20548 return (rval); 20549 } 20550 20551 20552 static int 20553 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20554 { 20555 struct sd_uscsi_info *uip; 20556 struct uscsi_cmd *uscmd; 20557 uint8_t *sense_buf; 20558 struct sd_lun *un; 20559 int status; 20560 20561 uip = (struct sd_uscsi_info *)(bp->b_private); 20562 ASSERT(uip != NULL); 20563 20564 uscmd = uip->ui_cmdp; 20565 ASSERT(uscmd != NULL); 20566 20567 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20568 ASSERT(sense_buf != NULL); 20569 20570 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20571 ASSERT(un != NULL); 20572 20573 status = geterror(bp); 20574 switch (status) { 20575 case 0: 20576 break; /* Success! */ 20577 case EIO: 20578 switch (uscmd->uscsi_status) { 20579 case STATUS_RESERVATION_CONFLICT: 20580 /* Ignore reservation conflict */ 20581 status = 0; 20582 goto done; 20583 20584 case STATUS_CHECK: 20585 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20586 (scsi_sense_key(sense_buf) == 20587 KEY_ILLEGAL_REQUEST)) { 20588 /* Ignore Illegal Request error */ 20589 mutex_enter(SD_MUTEX(un)); 20590 un->un_f_sync_cache_supported = FALSE; 20591 mutex_exit(SD_MUTEX(un)); 20592 status = ENOTSUP; 20593 goto done; 20594 } 20595 break; 20596 default: 20597 break; 20598 } 20599 /* FALLTHRU */ 20600 default: 20601 /* 20602 * Don't log an error message if this device 20603 * has removable media. 20604 */ 20605 if (!un->un_f_has_removable_media) { 20606 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20607 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20608 } 20609 break; 20610 } 20611 20612 done: 20613 if (uip->ui_dkc.dkc_callback != NULL) { 20614 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20615 } 20616 20617 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20618 freerbuf(bp); 20619 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20620 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20621 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20622 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20623 20624 return (status); 20625 } 20626 20627 20628 /* 20629 * Function: sd_send_scsi_GET_CONFIGURATION 20630 * 20631 * Description: Issues the get configuration command to the device. 20632 * Called from sd_check_for_writable_cd & sd_get_media_info 20633 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20634 * Arguments: un 20635 * ucmdbuf 20636 * rqbuf 20637 * rqbuflen 20638 * bufaddr 20639 * buflen 20640 * 20641 * Return Code: 0 - Success 20642 * errno return code from sd_send_scsi_cmd() 20643 * 20644 * Context: Can sleep. Does not return until command is completed. 20645 * 20646 */ 20647 20648 static int 20649 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20650 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20651 { 20652 char cdb[CDB_GROUP1]; 20653 int status; 20654 20655 ASSERT(un != NULL); 20656 ASSERT(!mutex_owned(SD_MUTEX(un))); 20657 ASSERT(bufaddr != NULL); 20658 ASSERT(ucmdbuf != NULL); 20659 ASSERT(rqbuf != NULL); 20660 20661 SD_TRACE(SD_LOG_IO, un, 20662 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20663 20664 bzero(cdb, sizeof (cdb)); 20665 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20666 bzero(rqbuf, rqbuflen); 20667 bzero(bufaddr, buflen); 20668 20669 /* 20670 * Set up cdb field for the get configuration command. 20671 */ 20672 cdb[0] = SCMD_GET_CONFIGURATION; 20673 cdb[1] = 0x02; /* Requested Type */ 20674 cdb[8] = SD_PROFILE_HEADER_LEN; 20675 ucmdbuf->uscsi_cdb = cdb; 20676 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20677 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20678 ucmdbuf->uscsi_buflen = buflen; 20679 ucmdbuf->uscsi_timeout = sd_io_time; 20680 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20681 ucmdbuf->uscsi_rqlen = rqbuflen; 20682 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20683 20684 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20685 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20686 20687 switch (status) { 20688 case 0: 20689 break; /* Success! */ 20690 case EIO: 20691 switch (ucmdbuf->uscsi_status) { 20692 case STATUS_RESERVATION_CONFLICT: 20693 status = EACCES; 20694 break; 20695 default: 20696 break; 20697 } 20698 break; 20699 default: 20700 break; 20701 } 20702 20703 if (status == 0) { 20704 SD_DUMP_MEMORY(un, SD_LOG_IO, 20705 "sd_send_scsi_GET_CONFIGURATION: data", 20706 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20707 } 20708 20709 SD_TRACE(SD_LOG_IO, un, 20710 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20711 20712 return (status); 20713 } 20714 20715 /* 20716 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20717 * 20718 * Description: Issues the get configuration command to the device to 20719 * retrieve a specfic feature. Called from 20720 * sd_check_for_writable_cd & sd_set_mmc_caps. 20721 * Arguments: un 20722 * ucmdbuf 20723 * rqbuf 20724 * rqbuflen 20725 * bufaddr 20726 * buflen 20727 * feature 20728 * 20729 * Return Code: 0 - Success 20730 * errno return code from sd_send_scsi_cmd() 20731 * 20732 * Context: Can sleep. Does not return until command is completed. 20733 * 20734 */ 20735 static int 20736 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20737 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20738 uchar_t *bufaddr, uint_t buflen, char feature) 20739 { 20740 char cdb[CDB_GROUP1]; 20741 int status; 20742 20743 ASSERT(un != NULL); 20744 ASSERT(!mutex_owned(SD_MUTEX(un))); 20745 ASSERT(bufaddr != NULL); 20746 ASSERT(ucmdbuf != NULL); 20747 ASSERT(rqbuf != NULL); 20748 20749 SD_TRACE(SD_LOG_IO, un, 20750 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20751 20752 bzero(cdb, sizeof (cdb)); 20753 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20754 bzero(rqbuf, rqbuflen); 20755 bzero(bufaddr, buflen); 20756 20757 /* 20758 * Set up cdb field for the get configuration command. 20759 */ 20760 cdb[0] = SCMD_GET_CONFIGURATION; 20761 cdb[1] = 0x02; /* Requested Type */ 20762 cdb[3] = feature; 20763 cdb[8] = buflen; 20764 ucmdbuf->uscsi_cdb = cdb; 20765 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20766 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20767 ucmdbuf->uscsi_buflen = buflen; 20768 ucmdbuf->uscsi_timeout = sd_io_time; 20769 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20770 ucmdbuf->uscsi_rqlen = rqbuflen; 20771 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20772 20773 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20774 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20775 20776 switch (status) { 20777 case 0: 20778 break; /* Success! */ 20779 case EIO: 20780 switch (ucmdbuf->uscsi_status) { 20781 case STATUS_RESERVATION_CONFLICT: 20782 status = EACCES; 20783 break; 20784 default: 20785 break; 20786 } 20787 break; 20788 default: 20789 break; 20790 } 20791 20792 if (status == 0) { 20793 SD_DUMP_MEMORY(un, SD_LOG_IO, 20794 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20795 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20796 } 20797 20798 SD_TRACE(SD_LOG_IO, un, 20799 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20800 20801 return (status); 20802 } 20803 20804 20805 /* 20806 * Function: sd_send_scsi_MODE_SENSE 20807 * 20808 * Description: Utility function for issuing a scsi MODE SENSE command. 20809 * Note: This routine uses a consistent implementation for Group0, 20810 * Group1, and Group2 commands across all platforms. ATAPI devices 20811 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20812 * 20813 * Arguments: un - pointer to the softstate struct for the target. 20814 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20815 * CDB_GROUP[1|2] (10 byte). 20816 * bufaddr - buffer for page data retrieved from the target. 20817 * buflen - size of page to be retrieved. 20818 * page_code - page code of data to be retrieved from the target. 20819 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20820 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20821 * to use the USCSI "direct" chain and bypass the normal 20822 * command waitq. 20823 * 20824 * Return Code: 0 - Success 20825 * errno return code from sd_send_scsi_cmd() 20826 * 20827 * Context: Can sleep. Does not return until command is completed. 20828 */ 20829 20830 static int 20831 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20832 size_t buflen, uchar_t page_code, int path_flag) 20833 { 20834 struct scsi_extended_sense sense_buf; 20835 union scsi_cdb cdb; 20836 struct uscsi_cmd ucmd_buf; 20837 int status; 20838 int headlen; 20839 20840 ASSERT(un != NULL); 20841 ASSERT(!mutex_owned(SD_MUTEX(un))); 20842 ASSERT(bufaddr != NULL); 20843 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20844 (cdbsize == CDB_GROUP2)); 20845 20846 SD_TRACE(SD_LOG_IO, un, 20847 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20848 20849 bzero(&cdb, sizeof (cdb)); 20850 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20851 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20852 bzero(bufaddr, buflen); 20853 20854 if (cdbsize == CDB_GROUP0) { 20855 cdb.scc_cmd = SCMD_MODE_SENSE; 20856 cdb.cdb_opaque[2] = page_code; 20857 FORMG0COUNT(&cdb, buflen); 20858 headlen = MODE_HEADER_LENGTH; 20859 } else { 20860 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20861 cdb.cdb_opaque[2] = page_code; 20862 FORMG1COUNT(&cdb, buflen); 20863 headlen = MODE_HEADER_LENGTH_GRP2; 20864 } 20865 20866 ASSERT(headlen <= buflen); 20867 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20868 20869 ucmd_buf.uscsi_cdb = (char *)&cdb; 20870 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20871 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20872 ucmd_buf.uscsi_buflen = buflen; 20873 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20874 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20875 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20876 ucmd_buf.uscsi_timeout = 60; 20877 20878 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20879 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20880 20881 switch (status) { 20882 case 0: 20883 /* 20884 * sr_check_wp() uses 0x3f page code and check the header of 20885 * mode page to determine if target device is write-protected. 20886 * But some USB devices return 0 bytes for 0x3f page code. For 20887 * this case, make sure that mode page header is returned at 20888 * least. 20889 */ 20890 if (buflen - ucmd_buf.uscsi_resid < headlen) 20891 status = EIO; 20892 break; /* Success! */ 20893 case EIO: 20894 switch (ucmd_buf.uscsi_status) { 20895 case STATUS_RESERVATION_CONFLICT: 20896 status = EACCES; 20897 break; 20898 default: 20899 break; 20900 } 20901 break; 20902 default: 20903 break; 20904 } 20905 20906 if (status == 0) { 20907 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20908 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20909 } 20910 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20911 20912 return (status); 20913 } 20914 20915 20916 /* 20917 * Function: sd_send_scsi_MODE_SELECT 20918 * 20919 * Description: Utility function for issuing a scsi MODE SELECT command. 20920 * Note: This routine uses a consistent implementation for Group0, 20921 * Group1, and Group2 commands across all platforms. ATAPI devices 20922 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20923 * 20924 * Arguments: un - pointer to the softstate struct for the target. 20925 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20926 * CDB_GROUP[1|2] (10 byte). 20927 * bufaddr - buffer for page data retrieved from the target. 20928 * buflen - size of page to be retrieved. 20929 * save_page - boolean to determin if SP bit should be set. 20930 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20931 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20932 * to use the USCSI "direct" chain and bypass the normal 20933 * command waitq. 20934 * 20935 * Return Code: 0 - Success 20936 * errno return code from sd_send_scsi_cmd() 20937 * 20938 * Context: Can sleep. Does not return until command is completed. 20939 */ 20940 20941 static int 20942 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20943 size_t buflen, uchar_t save_page, int path_flag) 20944 { 20945 struct scsi_extended_sense sense_buf; 20946 union scsi_cdb cdb; 20947 struct uscsi_cmd ucmd_buf; 20948 int status; 20949 20950 ASSERT(un != NULL); 20951 ASSERT(!mutex_owned(SD_MUTEX(un))); 20952 ASSERT(bufaddr != NULL); 20953 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20954 (cdbsize == CDB_GROUP2)); 20955 20956 SD_TRACE(SD_LOG_IO, un, 20957 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20958 20959 bzero(&cdb, sizeof (cdb)); 20960 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20961 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20962 20963 /* Set the PF bit for many third party drives */ 20964 cdb.cdb_opaque[1] = 0x10; 20965 20966 /* Set the savepage(SP) bit if given */ 20967 if (save_page == SD_SAVE_PAGE) { 20968 cdb.cdb_opaque[1] |= 0x01; 20969 } 20970 20971 if (cdbsize == CDB_GROUP0) { 20972 cdb.scc_cmd = SCMD_MODE_SELECT; 20973 FORMG0COUNT(&cdb, buflen); 20974 } else { 20975 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20976 FORMG1COUNT(&cdb, buflen); 20977 } 20978 20979 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20980 20981 ucmd_buf.uscsi_cdb = (char *)&cdb; 20982 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20983 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20984 ucmd_buf.uscsi_buflen = buflen; 20985 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20986 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20987 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20988 ucmd_buf.uscsi_timeout = 60; 20989 20990 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20991 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20992 20993 switch (status) { 20994 case 0: 20995 break; /* Success! */ 20996 case EIO: 20997 switch (ucmd_buf.uscsi_status) { 20998 case STATUS_RESERVATION_CONFLICT: 20999 status = EACCES; 21000 break; 21001 default: 21002 break; 21003 } 21004 break; 21005 default: 21006 break; 21007 } 21008 21009 if (status == 0) { 21010 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21011 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21012 } 21013 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21014 21015 return (status); 21016 } 21017 21018 21019 /* 21020 * Function: sd_send_scsi_RDWR 21021 * 21022 * Description: Issue a scsi READ or WRITE command with the given parameters. 21023 * 21024 * Arguments: un: Pointer to the sd_lun struct for the target. 21025 * cmd: SCMD_READ or SCMD_WRITE 21026 * bufaddr: Address of caller's buffer to receive the RDWR data 21027 * buflen: Length of caller's buffer receive the RDWR data. 21028 * start_block: Block number for the start of the RDWR operation. 21029 * (Assumes target-native block size.) 21030 * residp: Pointer to variable to receive the redisual of the 21031 * RDWR operation (may be NULL of no residual requested). 21032 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21033 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21034 * to use the USCSI "direct" chain and bypass the normal 21035 * command waitq. 21036 * 21037 * Return Code: 0 - Success 21038 * errno return code from sd_send_scsi_cmd() 21039 * 21040 * Context: Can sleep. Does not return until command is completed. 21041 */ 21042 21043 static int 21044 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 21045 size_t buflen, daddr_t start_block, int path_flag) 21046 { 21047 struct scsi_extended_sense sense_buf; 21048 union scsi_cdb cdb; 21049 struct uscsi_cmd ucmd_buf; 21050 uint32_t block_count; 21051 int status; 21052 int cdbsize; 21053 uchar_t flag; 21054 21055 ASSERT(un != NULL); 21056 ASSERT(!mutex_owned(SD_MUTEX(un))); 21057 ASSERT(bufaddr != NULL); 21058 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21059 21060 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21061 21062 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21063 return (EINVAL); 21064 } 21065 21066 mutex_enter(SD_MUTEX(un)); 21067 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21068 mutex_exit(SD_MUTEX(un)); 21069 21070 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21071 21072 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21073 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21074 bufaddr, buflen, start_block, block_count); 21075 21076 bzero(&cdb, sizeof (cdb)); 21077 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21078 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21079 21080 /* Compute CDB size to use */ 21081 if (start_block > 0xffffffff) 21082 cdbsize = CDB_GROUP4; 21083 else if ((start_block & 0xFFE00000) || 21084 (un->un_f_cfg_is_atapi == TRUE)) 21085 cdbsize = CDB_GROUP1; 21086 else 21087 cdbsize = CDB_GROUP0; 21088 21089 switch (cdbsize) { 21090 case CDB_GROUP0: /* 6-byte CDBs */ 21091 cdb.scc_cmd = cmd; 21092 FORMG0ADDR(&cdb, start_block); 21093 FORMG0COUNT(&cdb, block_count); 21094 break; 21095 case CDB_GROUP1: /* 10-byte CDBs */ 21096 cdb.scc_cmd = cmd | SCMD_GROUP1; 21097 FORMG1ADDR(&cdb, start_block); 21098 FORMG1COUNT(&cdb, block_count); 21099 break; 21100 case CDB_GROUP4: /* 16-byte CDBs */ 21101 cdb.scc_cmd = cmd | SCMD_GROUP4; 21102 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21103 FORMG4COUNT(&cdb, block_count); 21104 break; 21105 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21106 default: 21107 /* All others reserved */ 21108 return (EINVAL); 21109 } 21110 21111 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21112 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21113 21114 ucmd_buf.uscsi_cdb = (char *)&cdb; 21115 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21116 ucmd_buf.uscsi_bufaddr = bufaddr; 21117 ucmd_buf.uscsi_buflen = buflen; 21118 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21119 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21120 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21121 ucmd_buf.uscsi_timeout = 60; 21122 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21123 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21124 switch (status) { 21125 case 0: 21126 break; /* Success! */ 21127 case EIO: 21128 switch (ucmd_buf.uscsi_status) { 21129 case STATUS_RESERVATION_CONFLICT: 21130 status = EACCES; 21131 break; 21132 default: 21133 break; 21134 } 21135 break; 21136 default: 21137 break; 21138 } 21139 21140 if (status == 0) { 21141 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21142 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21143 } 21144 21145 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21146 21147 return (status); 21148 } 21149 21150 21151 /* 21152 * Function: sd_send_scsi_LOG_SENSE 21153 * 21154 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21155 * 21156 * Arguments: un: Pointer to the sd_lun struct for the target. 21157 * 21158 * Return Code: 0 - Success 21159 * errno return code from sd_send_scsi_cmd() 21160 * 21161 * Context: Can sleep. Does not return until command is completed. 21162 */ 21163 21164 static int 21165 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 21166 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21167 int path_flag) 21168 21169 { 21170 struct scsi_extended_sense sense_buf; 21171 union scsi_cdb cdb; 21172 struct uscsi_cmd ucmd_buf; 21173 int status; 21174 21175 ASSERT(un != NULL); 21176 ASSERT(!mutex_owned(SD_MUTEX(un))); 21177 21178 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21179 21180 bzero(&cdb, sizeof (cdb)); 21181 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21182 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21183 21184 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21185 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21186 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21187 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21188 FORMG1COUNT(&cdb, buflen); 21189 21190 ucmd_buf.uscsi_cdb = (char *)&cdb; 21191 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21192 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21193 ucmd_buf.uscsi_buflen = buflen; 21194 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21195 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21196 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21197 ucmd_buf.uscsi_timeout = 60; 21198 21199 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21200 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21201 21202 switch (status) { 21203 case 0: 21204 break; 21205 case EIO: 21206 switch (ucmd_buf.uscsi_status) { 21207 case STATUS_RESERVATION_CONFLICT: 21208 status = EACCES; 21209 break; 21210 case STATUS_CHECK: 21211 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21212 (scsi_sense_key((uint8_t *)&sense_buf) == 21213 KEY_ILLEGAL_REQUEST) && 21214 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21215 /* 21216 * ASC 0x24: INVALID FIELD IN CDB 21217 */ 21218 switch (page_code) { 21219 case START_STOP_CYCLE_PAGE: 21220 /* 21221 * The start stop cycle counter is 21222 * implemented as page 0x31 in earlier 21223 * generation disks. In new generation 21224 * disks the start stop cycle counter is 21225 * implemented as page 0xE. To properly 21226 * handle this case if an attempt for 21227 * log page 0xE is made and fails we 21228 * will try again using page 0x31. 21229 * 21230 * Network storage BU committed to 21231 * maintain the page 0x31 for this 21232 * purpose and will not have any other 21233 * page implemented with page code 0x31 21234 * until all disks transition to the 21235 * standard page. 21236 */ 21237 mutex_enter(SD_MUTEX(un)); 21238 un->un_start_stop_cycle_page = 21239 START_STOP_CYCLE_VU_PAGE; 21240 cdb.cdb_opaque[2] = 21241 (char)(page_control << 6) | 21242 un->un_start_stop_cycle_page; 21243 mutex_exit(SD_MUTEX(un)); 21244 status = sd_send_scsi_cmd( 21245 SD_GET_DEV(un), &ucmd_buf, 21246 UIO_SYSSPACE, UIO_SYSSPACE, 21247 UIO_SYSSPACE, path_flag); 21248 21249 break; 21250 case TEMPERATURE_PAGE: 21251 status = ENOTTY; 21252 break; 21253 default: 21254 break; 21255 } 21256 } 21257 break; 21258 default: 21259 break; 21260 } 21261 break; 21262 default: 21263 break; 21264 } 21265 21266 if (status == 0) { 21267 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21268 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21269 } 21270 21271 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21272 21273 return (status); 21274 } 21275 21276 21277 /* 21278 * Function: sdioctl 21279 * 21280 * Description: Driver's ioctl(9e) entry point function. 21281 * 21282 * Arguments: dev - device number 21283 * cmd - ioctl operation to be performed 21284 * arg - user argument, contains data to be set or reference 21285 * parameter for get 21286 * flag - bit flag, indicating open settings, 32/64 bit type 21287 * cred_p - user credential pointer 21288 * rval_p - calling process return value (OPT) 21289 * 21290 * Return Code: EINVAL 21291 * ENOTTY 21292 * ENXIO 21293 * EIO 21294 * EFAULT 21295 * ENOTSUP 21296 * EPERM 21297 * 21298 * Context: Called from the device switch at normal priority. 21299 */ 21300 21301 static int 21302 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 21303 { 21304 struct sd_lun *un = NULL; 21305 int geom_validated = FALSE; 21306 int err = 0; 21307 int i = 0; 21308 cred_t *cr; 21309 21310 /* 21311 * All device accesses go thru sdstrategy where we check on suspend 21312 * status 21313 */ 21314 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21315 return (ENXIO); 21316 } 21317 21318 ASSERT(!mutex_owned(SD_MUTEX(un))); 21319 21320 /* 21321 * Moved this wait from sd_uscsi_strategy to here for 21322 * reasons of deadlock prevention. Internal driver commands, 21323 * specifically those to change a devices power level, result 21324 * in a call to sd_uscsi_strategy. 21325 */ 21326 mutex_enter(SD_MUTEX(un)); 21327 while ((un->un_state == SD_STATE_SUSPENDED) || 21328 (un->un_state == SD_STATE_PM_CHANGING)) { 21329 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 21330 } 21331 /* 21332 * Twiddling the counter here protects commands from now 21333 * through to the top of sd_uscsi_strategy. Without the 21334 * counter inc. a power down, for example, could get in 21335 * after the above check for state is made and before 21336 * execution gets to the top of sd_uscsi_strategy. 21337 * That would cause problems. 21338 */ 21339 un->un_ncmds_in_driver++; 21340 21341 if ((un->un_f_geometry_is_valid == FALSE) && 21342 (flag & (FNDELAY | FNONBLOCK))) { 21343 switch (cmd) { 21344 case CDROMPAUSE: 21345 case CDROMRESUME: 21346 case CDROMPLAYMSF: 21347 case CDROMPLAYTRKIND: 21348 case CDROMREADTOCHDR: 21349 case CDROMREADTOCENTRY: 21350 case CDROMSTOP: 21351 case CDROMSTART: 21352 case CDROMVOLCTRL: 21353 case CDROMSUBCHNL: 21354 case CDROMREADMODE2: 21355 case CDROMREADMODE1: 21356 case CDROMREADOFFSET: 21357 case CDROMSBLKMODE: 21358 case CDROMGBLKMODE: 21359 case CDROMGDRVSPEED: 21360 case CDROMSDRVSPEED: 21361 case CDROMCDDA: 21362 case CDROMCDXA: 21363 case CDROMSUBCODE: 21364 if (!ISCD(un)) { 21365 un->un_ncmds_in_driver--; 21366 ASSERT(un->un_ncmds_in_driver >= 0); 21367 mutex_exit(SD_MUTEX(un)); 21368 return (ENOTTY); 21369 } 21370 break; 21371 case FDEJECT: 21372 case DKIOCEJECT: 21373 case CDROMEJECT: 21374 if (!un->un_f_eject_media_supported) { 21375 un->un_ncmds_in_driver--; 21376 ASSERT(un->un_ncmds_in_driver >= 0); 21377 mutex_exit(SD_MUTEX(un)); 21378 return (ENOTTY); 21379 } 21380 break; 21381 case DKIOCSVTOC: 21382 case DKIOCSETEFI: 21383 case DKIOCSMBOOT: 21384 case DKIOCFLUSHWRITECACHE: 21385 mutex_exit(SD_MUTEX(un)); 21386 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 21387 if (err != 0) { 21388 mutex_enter(SD_MUTEX(un)); 21389 un->un_ncmds_in_driver--; 21390 ASSERT(un->un_ncmds_in_driver >= 0); 21391 mutex_exit(SD_MUTEX(un)); 21392 return (EIO); 21393 } 21394 mutex_enter(SD_MUTEX(un)); 21395 /* FALLTHROUGH */ 21396 case DKIOCREMOVABLE: 21397 case DKIOCHOTPLUGGABLE: 21398 case DKIOCINFO: 21399 case DKIOCGMEDIAINFO: 21400 case MHIOCENFAILFAST: 21401 case MHIOCSTATUS: 21402 case MHIOCTKOWN: 21403 case MHIOCRELEASE: 21404 case MHIOCGRP_INKEYS: 21405 case MHIOCGRP_INRESV: 21406 case MHIOCGRP_REGISTER: 21407 case MHIOCGRP_RESERVE: 21408 case MHIOCGRP_PREEMPTANDABORT: 21409 case MHIOCGRP_REGISTERANDIGNOREKEY: 21410 case CDROMCLOSETRAY: 21411 case USCSICMD: 21412 goto skip_ready_valid; 21413 default: 21414 break; 21415 } 21416 21417 mutex_exit(SD_MUTEX(un)); 21418 err = sd_ready_and_valid(un); 21419 mutex_enter(SD_MUTEX(un)); 21420 if (err == SD_READY_NOT_VALID) { 21421 switch (cmd) { 21422 case DKIOCGAPART: 21423 case DKIOCGGEOM: 21424 case DKIOCSGEOM: 21425 case DKIOCGVTOC: 21426 case DKIOCSVTOC: 21427 case DKIOCSAPART: 21428 case DKIOCG_PHYGEOM: 21429 case DKIOCG_VIRTGEOM: 21430 err = ENOTSUP; 21431 un->un_ncmds_in_driver--; 21432 ASSERT(un->un_ncmds_in_driver >= 0); 21433 mutex_exit(SD_MUTEX(un)); 21434 return (err); 21435 } 21436 } 21437 if (err != SD_READY_VALID) { 21438 switch (cmd) { 21439 case DKIOCSTATE: 21440 case CDROMGDRVSPEED: 21441 case CDROMSDRVSPEED: 21442 case FDEJECT: /* for eject command */ 21443 case DKIOCEJECT: 21444 case CDROMEJECT: 21445 case DKIOCGETEFI: 21446 case DKIOCSGEOM: 21447 case DKIOCREMOVABLE: 21448 case DKIOCHOTPLUGGABLE: 21449 case DKIOCSAPART: 21450 case DKIOCSETEFI: 21451 break; 21452 default: 21453 if (un->un_f_has_removable_media) { 21454 err = ENXIO; 21455 } else { 21456 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 21457 if (err == SD_RESERVED_BY_OTHERS) { 21458 err = EACCES; 21459 } else { 21460 err = EIO; 21461 } 21462 } 21463 un->un_ncmds_in_driver--; 21464 ASSERT(un->un_ncmds_in_driver >= 0); 21465 mutex_exit(SD_MUTEX(un)); 21466 return (err); 21467 } 21468 } 21469 geom_validated = TRUE; 21470 } 21471 if ((un->un_f_geometry_is_valid == TRUE) && 21472 (un->un_solaris_size > 0)) { 21473 /* 21474 * the "geometry_is_valid" flag could be true if we 21475 * have an fdisk table but no Solaris partition 21476 */ 21477 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21478 /* it is EFI, so return ENOTSUP for these */ 21479 switch (cmd) { 21480 case DKIOCGAPART: 21481 case DKIOCGGEOM: 21482 case DKIOCGVTOC: 21483 case DKIOCSVTOC: 21484 case DKIOCSAPART: 21485 err = ENOTSUP; 21486 un->un_ncmds_in_driver--; 21487 ASSERT(un->un_ncmds_in_driver >= 0); 21488 mutex_exit(SD_MUTEX(un)); 21489 return (err); 21490 } 21491 } 21492 } 21493 21494 skip_ready_valid: 21495 mutex_exit(SD_MUTEX(un)); 21496 21497 switch (cmd) { 21498 case DKIOCINFO: 21499 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21500 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21501 break; 21502 21503 case DKIOCGMEDIAINFO: 21504 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21505 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21506 break; 21507 21508 case DKIOCGGEOM: 21509 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21510 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21511 geom_validated); 21512 break; 21513 21514 case DKIOCSGEOM: 21515 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21516 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21517 break; 21518 21519 case DKIOCGAPART: 21520 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21521 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21522 geom_validated); 21523 break; 21524 21525 case DKIOCSAPART: 21526 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21527 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21528 break; 21529 21530 case DKIOCGVTOC: 21531 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21532 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21533 geom_validated); 21534 break; 21535 21536 case DKIOCGETEFI: 21537 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21538 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21539 break; 21540 21541 case DKIOCPARTITION: 21542 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21543 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21544 break; 21545 21546 case DKIOCSVTOC: 21547 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21548 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21549 break; 21550 21551 case DKIOCSETEFI: 21552 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21553 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21554 break; 21555 21556 case DKIOCGMBOOT: 21557 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21558 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21559 break; 21560 21561 case DKIOCSMBOOT: 21562 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21563 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21564 break; 21565 21566 case DKIOCLOCK: 21567 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21568 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21569 SD_PATH_STANDARD); 21570 break; 21571 21572 case DKIOCUNLOCK: 21573 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21574 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21575 SD_PATH_STANDARD); 21576 break; 21577 21578 case DKIOCSTATE: { 21579 enum dkio_state state; 21580 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21581 21582 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21583 err = EFAULT; 21584 } else { 21585 err = sd_check_media(dev, state); 21586 if (err == 0) { 21587 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21588 sizeof (int), flag) != 0) 21589 err = EFAULT; 21590 } 21591 } 21592 break; 21593 } 21594 21595 case DKIOCREMOVABLE: 21596 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21597 i = un->un_f_has_removable_media ? 1 : 0; 21598 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21599 err = EFAULT; 21600 } else { 21601 err = 0; 21602 } 21603 break; 21604 21605 case DKIOCHOTPLUGGABLE: 21606 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21607 i = un->un_f_is_hotpluggable ? 1 : 0; 21608 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21609 err = EFAULT; 21610 } else { 21611 err = 0; 21612 } 21613 break; 21614 21615 case DKIOCGTEMPERATURE: 21616 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21617 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21618 break; 21619 21620 case MHIOCENFAILFAST: 21621 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21622 if ((err = drv_priv(cred_p)) == 0) { 21623 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21624 } 21625 break; 21626 21627 case MHIOCTKOWN: 21628 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21629 if ((err = drv_priv(cred_p)) == 0) { 21630 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21631 } 21632 break; 21633 21634 case MHIOCRELEASE: 21635 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21636 if ((err = drv_priv(cred_p)) == 0) { 21637 err = sd_mhdioc_release(dev); 21638 } 21639 break; 21640 21641 case MHIOCSTATUS: 21642 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21643 if ((err = drv_priv(cred_p)) == 0) { 21644 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21645 case 0: 21646 err = 0; 21647 break; 21648 case EACCES: 21649 *rval_p = 1; 21650 err = 0; 21651 break; 21652 default: 21653 err = EIO; 21654 break; 21655 } 21656 } 21657 break; 21658 21659 case MHIOCQRESERVE: 21660 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21661 if ((err = drv_priv(cred_p)) == 0) { 21662 err = sd_reserve_release(dev, SD_RESERVE); 21663 } 21664 break; 21665 21666 case MHIOCREREGISTERDEVID: 21667 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21668 if (drv_priv(cred_p) == EPERM) { 21669 err = EPERM; 21670 } else if (!un->un_f_devid_supported) { 21671 err = ENOTTY; 21672 } else { 21673 err = sd_mhdioc_register_devid(dev); 21674 } 21675 break; 21676 21677 case MHIOCGRP_INKEYS: 21678 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21679 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21680 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21681 err = ENOTSUP; 21682 } else { 21683 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21684 flag); 21685 } 21686 } 21687 break; 21688 21689 case MHIOCGRP_INRESV: 21690 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21691 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21692 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21693 err = ENOTSUP; 21694 } else { 21695 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21696 } 21697 } 21698 break; 21699 21700 case MHIOCGRP_REGISTER: 21701 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21702 if ((err = drv_priv(cred_p)) != EPERM) { 21703 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21704 err = ENOTSUP; 21705 } else if (arg != NULL) { 21706 mhioc_register_t reg; 21707 if (ddi_copyin((void *)arg, ®, 21708 sizeof (mhioc_register_t), flag) != 0) { 21709 err = EFAULT; 21710 } else { 21711 err = 21712 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21713 un, SD_SCSI3_REGISTER, 21714 (uchar_t *)®); 21715 } 21716 } 21717 } 21718 break; 21719 21720 case MHIOCGRP_RESERVE: 21721 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21722 if ((err = drv_priv(cred_p)) != EPERM) { 21723 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21724 err = ENOTSUP; 21725 } else if (arg != NULL) { 21726 mhioc_resv_desc_t resv_desc; 21727 if (ddi_copyin((void *)arg, &resv_desc, 21728 sizeof (mhioc_resv_desc_t), flag) != 0) { 21729 err = EFAULT; 21730 } else { 21731 err = 21732 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21733 un, SD_SCSI3_RESERVE, 21734 (uchar_t *)&resv_desc); 21735 } 21736 } 21737 } 21738 break; 21739 21740 case MHIOCGRP_PREEMPTANDABORT: 21741 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21742 if ((err = drv_priv(cred_p)) != EPERM) { 21743 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21744 err = ENOTSUP; 21745 } else if (arg != NULL) { 21746 mhioc_preemptandabort_t preempt_abort; 21747 if (ddi_copyin((void *)arg, &preempt_abort, 21748 sizeof (mhioc_preemptandabort_t), 21749 flag) != 0) { 21750 err = EFAULT; 21751 } else { 21752 err = 21753 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21754 un, SD_SCSI3_PREEMPTANDABORT, 21755 (uchar_t *)&preempt_abort); 21756 } 21757 } 21758 } 21759 break; 21760 21761 case MHIOCGRP_REGISTERANDIGNOREKEY: 21762 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21763 if ((err = drv_priv(cred_p)) != EPERM) { 21764 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21765 err = ENOTSUP; 21766 } else if (arg != NULL) { 21767 mhioc_registerandignorekey_t r_and_i; 21768 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21769 sizeof (mhioc_registerandignorekey_t), 21770 flag) != 0) { 21771 err = EFAULT; 21772 } else { 21773 err = 21774 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21775 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21776 (uchar_t *)&r_and_i); 21777 } 21778 } 21779 } 21780 break; 21781 21782 case USCSICMD: 21783 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21784 cr = ddi_get_cred(); 21785 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21786 err = EPERM; 21787 } else { 21788 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21789 } 21790 break; 21791 21792 case CDROMPAUSE: 21793 case CDROMRESUME: 21794 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21795 if (!ISCD(un)) { 21796 err = ENOTTY; 21797 } else { 21798 err = sr_pause_resume(dev, cmd); 21799 } 21800 break; 21801 21802 case CDROMPLAYMSF: 21803 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21804 if (!ISCD(un)) { 21805 err = ENOTTY; 21806 } else { 21807 err = sr_play_msf(dev, (caddr_t)arg, flag); 21808 } 21809 break; 21810 21811 case CDROMPLAYTRKIND: 21812 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21813 #if defined(__i386) || defined(__amd64) 21814 /* 21815 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21816 */ 21817 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21818 #else 21819 if (!ISCD(un)) { 21820 #endif 21821 err = ENOTTY; 21822 } else { 21823 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21824 } 21825 break; 21826 21827 case CDROMREADTOCHDR: 21828 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21829 if (!ISCD(un)) { 21830 err = ENOTTY; 21831 } else { 21832 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21833 } 21834 break; 21835 21836 case CDROMREADTOCENTRY: 21837 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21838 if (!ISCD(un)) { 21839 err = ENOTTY; 21840 } else { 21841 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21842 } 21843 break; 21844 21845 case CDROMSTOP: 21846 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21847 if (!ISCD(un)) { 21848 err = ENOTTY; 21849 } else { 21850 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21851 SD_PATH_STANDARD); 21852 } 21853 break; 21854 21855 case CDROMSTART: 21856 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21857 if (!ISCD(un)) { 21858 err = ENOTTY; 21859 } else { 21860 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21861 SD_PATH_STANDARD); 21862 } 21863 break; 21864 21865 case CDROMCLOSETRAY: 21866 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21867 if (!ISCD(un)) { 21868 err = ENOTTY; 21869 } else { 21870 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21871 SD_PATH_STANDARD); 21872 } 21873 break; 21874 21875 case FDEJECT: /* for eject command */ 21876 case DKIOCEJECT: 21877 case CDROMEJECT: 21878 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21879 if (!un->un_f_eject_media_supported) { 21880 err = ENOTTY; 21881 } else { 21882 err = sr_eject(dev); 21883 } 21884 break; 21885 21886 case CDROMVOLCTRL: 21887 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21888 if (!ISCD(un)) { 21889 err = ENOTTY; 21890 } else { 21891 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21892 } 21893 break; 21894 21895 case CDROMSUBCHNL: 21896 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21897 if (!ISCD(un)) { 21898 err = ENOTTY; 21899 } else { 21900 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21901 } 21902 break; 21903 21904 case CDROMREADMODE2: 21905 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21906 if (!ISCD(un)) { 21907 err = ENOTTY; 21908 } else if (un->un_f_cfg_is_atapi == TRUE) { 21909 /* 21910 * If the drive supports READ CD, use that instead of 21911 * switching the LBA size via a MODE SELECT 21912 * Block Descriptor 21913 */ 21914 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21915 } else { 21916 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21917 } 21918 break; 21919 21920 case CDROMREADMODE1: 21921 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21922 if (!ISCD(un)) { 21923 err = ENOTTY; 21924 } else { 21925 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21926 } 21927 break; 21928 21929 case CDROMREADOFFSET: 21930 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21931 if (!ISCD(un)) { 21932 err = ENOTTY; 21933 } else { 21934 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21935 flag); 21936 } 21937 break; 21938 21939 case CDROMSBLKMODE: 21940 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21941 /* 21942 * There is no means of changing block size in case of atapi 21943 * drives, thus return ENOTTY if drive type is atapi 21944 */ 21945 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21946 err = ENOTTY; 21947 } else if (un->un_f_mmc_cap == TRUE) { 21948 21949 /* 21950 * MMC Devices do not support changing the 21951 * logical block size 21952 * 21953 * Note: EINVAL is being returned instead of ENOTTY to 21954 * maintain consistancy with the original mmc 21955 * driver update. 21956 */ 21957 err = EINVAL; 21958 } else { 21959 mutex_enter(SD_MUTEX(un)); 21960 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21961 (un->un_ncmds_in_transport > 0)) { 21962 mutex_exit(SD_MUTEX(un)); 21963 err = EINVAL; 21964 } else { 21965 mutex_exit(SD_MUTEX(un)); 21966 err = sr_change_blkmode(dev, cmd, arg, flag); 21967 } 21968 } 21969 break; 21970 21971 case CDROMGBLKMODE: 21972 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21973 if (!ISCD(un)) { 21974 err = ENOTTY; 21975 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21976 (un->un_f_blockcount_is_valid != FALSE)) { 21977 /* 21978 * Drive is an ATAPI drive so return target block 21979 * size for ATAPI drives since we cannot change the 21980 * blocksize on ATAPI drives. Used primarily to detect 21981 * if an ATAPI cdrom is present. 21982 */ 21983 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21984 sizeof (int), flag) != 0) { 21985 err = EFAULT; 21986 } else { 21987 err = 0; 21988 } 21989 21990 } else { 21991 /* 21992 * Drive supports changing block sizes via a Mode 21993 * Select. 21994 */ 21995 err = sr_change_blkmode(dev, cmd, arg, flag); 21996 } 21997 break; 21998 21999 case CDROMGDRVSPEED: 22000 case CDROMSDRVSPEED: 22001 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 22002 if (!ISCD(un)) { 22003 err = ENOTTY; 22004 } else if (un->un_f_mmc_cap == TRUE) { 22005 /* 22006 * Note: In the future the driver implementation 22007 * for getting and 22008 * setting cd speed should entail: 22009 * 1) If non-mmc try the Toshiba mode page 22010 * (sr_change_speed) 22011 * 2) If mmc but no support for Real Time Streaming try 22012 * the SET CD SPEED (0xBB) command 22013 * (sr_atapi_change_speed) 22014 * 3) If mmc and support for Real Time Streaming 22015 * try the GET PERFORMANCE and SET STREAMING 22016 * commands (not yet implemented, 4380808) 22017 */ 22018 /* 22019 * As per recent MMC spec, CD-ROM speed is variable 22020 * and changes with LBA. Since there is no such 22021 * things as drive speed now, fail this ioctl. 22022 * 22023 * Note: EINVAL is returned for consistancy of original 22024 * implementation which included support for getting 22025 * the drive speed of mmc devices but not setting 22026 * the drive speed. Thus EINVAL would be returned 22027 * if a set request was made for an mmc device. 22028 * We no longer support get or set speed for 22029 * mmc but need to remain consistant with regard 22030 * to the error code returned. 22031 */ 22032 err = EINVAL; 22033 } else if (un->un_f_cfg_is_atapi == TRUE) { 22034 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22035 } else { 22036 err = sr_change_speed(dev, cmd, arg, flag); 22037 } 22038 break; 22039 22040 case CDROMCDDA: 22041 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22042 if (!ISCD(un)) { 22043 err = ENOTTY; 22044 } else { 22045 err = sr_read_cdda(dev, (void *)arg, flag); 22046 } 22047 break; 22048 22049 case CDROMCDXA: 22050 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22051 if (!ISCD(un)) { 22052 err = ENOTTY; 22053 } else { 22054 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22055 } 22056 break; 22057 22058 case CDROMSUBCODE: 22059 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22060 if (!ISCD(un)) { 22061 err = ENOTTY; 22062 } else { 22063 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22064 } 22065 break; 22066 22067 case DKIOCPARTINFO: { 22068 /* 22069 * Return parameters describing the selected disk slice. 22070 * Note: this ioctl is for the intel platform only 22071 */ 22072 #if defined(__i386) || defined(__amd64) 22073 int part; 22074 22075 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22076 part = SDPART(dev); 22077 22078 /* don't check un_solaris_size for pN */ 22079 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 22080 err = EIO; 22081 } else { 22082 struct part_info p; 22083 22084 p.p_start = (daddr_t)un->un_offset[part]; 22085 p.p_length = (int)un->un_map[part].dkl_nblk; 22086 #ifdef _MULTI_DATAMODEL 22087 switch (ddi_model_convert_from(flag & FMODELS)) { 22088 case DDI_MODEL_ILP32: 22089 { 22090 struct part_info32 p32; 22091 22092 p32.p_start = (daddr32_t)p.p_start; 22093 p32.p_length = p.p_length; 22094 if (ddi_copyout(&p32, (void *)arg, 22095 sizeof (p32), flag)) 22096 err = EFAULT; 22097 break; 22098 } 22099 22100 case DDI_MODEL_NONE: 22101 { 22102 if (ddi_copyout(&p, (void *)arg, sizeof (p), 22103 flag)) 22104 err = EFAULT; 22105 break; 22106 } 22107 } 22108 #else /* ! _MULTI_DATAMODEL */ 22109 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 22110 err = EFAULT; 22111 #endif /* _MULTI_DATAMODEL */ 22112 } 22113 #else 22114 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22115 err = ENOTTY; 22116 #endif 22117 break; 22118 } 22119 22120 case DKIOCG_PHYGEOM: { 22121 /* Return the driver's notion of the media physical geometry */ 22122 #if defined(__i386) || defined(__amd64) 22123 uint64_t capacity; 22124 struct dk_geom disk_geom; 22125 struct dk_geom *dkgp = &disk_geom; 22126 22127 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22128 mutex_enter(SD_MUTEX(un)); 22129 22130 if (un->un_g.dkg_nhead != 0 && 22131 un->un_g.dkg_nsect != 0) { 22132 /* 22133 * We succeeded in getting a geometry, but 22134 * right now it is being reported as just the 22135 * Solaris fdisk partition, just like for 22136 * DKIOCGGEOM. We need to change that to be 22137 * correct for the entire disk now. 22138 */ 22139 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 22140 dkgp->dkg_acyl = 0; 22141 dkgp->dkg_ncyl = un->un_blockcount / 22142 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22143 } else { 22144 bzero(dkgp, sizeof (struct dk_geom)); 22145 /* 22146 * This disk does not have a Solaris VTOC 22147 * so we must present a physical geometry 22148 * that will remain consistent regardless 22149 * of how the disk is used. This will ensure 22150 * that the geometry does not change regardless 22151 * of the fdisk partition type (ie. EFI, FAT32, 22152 * Solaris, etc). 22153 */ 22154 if (ISCD(un)) { 22155 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 22156 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 22157 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 22158 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 22159 } else { 22160 /* 22161 * Invalid un_blockcount can generate invalid 22162 * dk_geom and may result in division by zero 22163 * system failure. Should make sure blockcount 22164 * is valid before using it here. 22165 */ 22166 if (un->un_f_blockcount_is_valid == FALSE) { 22167 mutex_exit(SD_MUTEX(un)); 22168 err = EIO; 22169 22170 break; 22171 } 22172 22173 /* 22174 * Refer to comments related to off-by-1 at the 22175 * header of this file 22176 */ 22177 if (!un->un_f_capacity_adjusted && 22178 !un->un_f_has_removable_media && 22179 !un->un_f_is_hotpluggable && 22180 (un->un_tgt_blocksize == 22181 un->un_sys_blocksize)) 22182 capacity = un->un_blockcount - 1; 22183 else 22184 capacity = un->un_blockcount; 22185 22186 sd_convert_geometry(capacity, dkgp); 22187 dkgp->dkg_acyl = 0; 22188 dkgp->dkg_ncyl = capacity / 22189 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22190 } 22191 } 22192 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22193 22194 if (ddi_copyout(dkgp, (void *)arg, 22195 sizeof (struct dk_geom), flag)) { 22196 mutex_exit(SD_MUTEX(un)); 22197 err = EFAULT; 22198 } else { 22199 mutex_exit(SD_MUTEX(un)); 22200 err = 0; 22201 } 22202 #else 22203 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22204 err = ENOTTY; 22205 #endif 22206 break; 22207 } 22208 22209 case DKIOCG_VIRTGEOM: { 22210 /* Return the driver's notion of the media's logical geometry */ 22211 #if defined(__i386) || defined(__amd64) 22212 struct dk_geom disk_geom; 22213 struct dk_geom *dkgp = &disk_geom; 22214 22215 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22216 mutex_enter(SD_MUTEX(un)); 22217 /* 22218 * If there is no HBA geometry available, or 22219 * if the HBA returned us something that doesn't 22220 * really fit into an Int 13/function 8 geometry 22221 * result, just fail the ioctl. See PSARC 1998/313. 22222 */ 22223 if (un->un_lgeom.g_nhead == 0 || 22224 un->un_lgeom.g_nsect == 0 || 22225 un->un_lgeom.g_ncyl > 1024) { 22226 mutex_exit(SD_MUTEX(un)); 22227 err = EINVAL; 22228 } else { 22229 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 22230 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 22231 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22232 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 22233 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 22234 22235 if (ddi_copyout(dkgp, (void *)arg, 22236 sizeof (struct dk_geom), flag)) { 22237 mutex_exit(SD_MUTEX(un)); 22238 err = EFAULT; 22239 } else { 22240 mutex_exit(SD_MUTEX(un)); 22241 err = 0; 22242 } 22243 } 22244 #else 22245 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22246 err = ENOTTY; 22247 #endif 22248 break; 22249 } 22250 #ifdef SDDEBUG 22251 /* RESET/ABORTS testing ioctls */ 22252 case DKIOCRESET: { 22253 int reset_level; 22254 22255 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22256 err = EFAULT; 22257 } else { 22258 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22259 "reset_level = 0x%lx\n", reset_level); 22260 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22261 err = 0; 22262 } else { 22263 err = EIO; 22264 } 22265 } 22266 break; 22267 } 22268 22269 case DKIOCABORT: 22270 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22271 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22272 err = 0; 22273 } else { 22274 err = EIO; 22275 } 22276 break; 22277 #endif 22278 22279 #ifdef SD_FAULT_INJECTION 22280 /* SDIOC FaultInjection testing ioctls */ 22281 case SDIOCSTART: 22282 case SDIOCSTOP: 22283 case SDIOCINSERTPKT: 22284 case SDIOCINSERTXB: 22285 case SDIOCINSERTUN: 22286 case SDIOCINSERTARQ: 22287 case SDIOCPUSH: 22288 case SDIOCRETRIEVE: 22289 case SDIOCRUN: 22290 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22291 "SDIOC detected cmd:0x%X:\n", cmd); 22292 /* call error generator */ 22293 sd_faultinjection_ioctl(cmd, arg, un); 22294 err = 0; 22295 break; 22296 22297 #endif /* SD_FAULT_INJECTION */ 22298 22299 case DKIOCFLUSHWRITECACHE: 22300 { 22301 struct dk_callback *dkc = (struct dk_callback *)arg; 22302 22303 mutex_enter(SD_MUTEX(un)); 22304 if (!un->un_f_sync_cache_supported || 22305 !un->un_f_write_cache_enabled) { 22306 err = un->un_f_sync_cache_supported ? 22307 0 : ENOTSUP; 22308 mutex_exit(SD_MUTEX(un)); 22309 if ((flag & FKIOCTL) && dkc != NULL && 22310 dkc->dkc_callback != NULL) { 22311 (*dkc->dkc_callback)(dkc->dkc_cookie, 22312 err); 22313 /* 22314 * Did callback and reported error. 22315 * Since we did a callback, ioctl 22316 * should return 0. 22317 */ 22318 err = 0; 22319 } 22320 break; 22321 } 22322 mutex_exit(SD_MUTEX(un)); 22323 22324 if ((flag & FKIOCTL) && dkc != NULL && 22325 dkc->dkc_callback != NULL) { 22326 /* async SYNC CACHE request */ 22327 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 22328 } else { 22329 /* synchronous SYNC CACHE request */ 22330 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22331 } 22332 } 22333 break; 22334 22335 case DKIOCGETWCE: { 22336 22337 int wce; 22338 22339 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 22340 break; 22341 } 22342 22343 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 22344 err = EFAULT; 22345 } 22346 break; 22347 } 22348 22349 case DKIOCSETWCE: { 22350 22351 int wce, sync_supported; 22352 22353 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 22354 err = EFAULT; 22355 break; 22356 } 22357 22358 /* 22359 * Synchronize multiple threads trying to enable 22360 * or disable the cache via the un_f_wcc_cv 22361 * condition variable. 22362 */ 22363 mutex_enter(SD_MUTEX(un)); 22364 22365 /* 22366 * Don't allow the cache to be enabled if the 22367 * config file has it disabled. 22368 */ 22369 if (un->un_f_opt_disable_cache && wce) { 22370 mutex_exit(SD_MUTEX(un)); 22371 err = EINVAL; 22372 break; 22373 } 22374 22375 /* 22376 * Wait for write cache change in progress 22377 * bit to be clear before proceeding. 22378 */ 22379 while (un->un_f_wcc_inprog) 22380 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22381 22382 un->un_f_wcc_inprog = 1; 22383 22384 if (un->un_f_write_cache_enabled && wce == 0) { 22385 /* 22386 * Disable the write cache. Don't clear 22387 * un_f_write_cache_enabled until after 22388 * the mode select and flush are complete. 22389 */ 22390 sync_supported = un->un_f_sync_cache_supported; 22391 mutex_exit(SD_MUTEX(un)); 22392 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22393 SD_CACHE_DISABLE)) == 0 && sync_supported) { 22394 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22395 } 22396 22397 mutex_enter(SD_MUTEX(un)); 22398 if (err == 0) { 22399 un->un_f_write_cache_enabled = 0; 22400 } 22401 22402 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22403 /* 22404 * Set un_f_write_cache_enabled first, so there is 22405 * no window where the cache is enabled, but the 22406 * bit says it isn't. 22407 */ 22408 un->un_f_write_cache_enabled = 1; 22409 mutex_exit(SD_MUTEX(un)); 22410 22411 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22412 SD_CACHE_ENABLE); 22413 22414 mutex_enter(SD_MUTEX(un)); 22415 22416 if (err) { 22417 un->un_f_write_cache_enabled = 0; 22418 } 22419 } 22420 22421 un->un_f_wcc_inprog = 0; 22422 cv_broadcast(&un->un_wcc_cv); 22423 mutex_exit(SD_MUTEX(un)); 22424 break; 22425 } 22426 22427 default: 22428 err = ENOTTY; 22429 break; 22430 } 22431 mutex_enter(SD_MUTEX(un)); 22432 un->un_ncmds_in_driver--; 22433 ASSERT(un->un_ncmds_in_driver >= 0); 22434 mutex_exit(SD_MUTEX(un)); 22435 22436 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22437 return (err); 22438 } 22439 22440 22441 /* 22442 * Function: sd_uscsi_ioctl 22443 * 22444 * Description: This routine is the driver entry point for handling USCSI ioctl 22445 * requests (USCSICMD). 22446 * 22447 * Arguments: dev - the device number 22448 * arg - user provided scsi command 22449 * flag - this argument is a pass through to ddi_copyxxx() 22450 * directly from the mode argument of ioctl(). 22451 * 22452 * Return Code: code returned by sd_send_scsi_cmd 22453 * ENXIO 22454 * EFAULT 22455 * EAGAIN 22456 */ 22457 22458 static int 22459 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 22460 { 22461 #ifdef _MULTI_DATAMODEL 22462 /* 22463 * For use when a 32 bit app makes a call into a 22464 * 64 bit ioctl 22465 */ 22466 struct uscsi_cmd32 uscsi_cmd_32_for_64; 22467 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 22468 model_t model; 22469 #endif /* _MULTI_DATAMODEL */ 22470 struct uscsi_cmd *scmd = NULL; 22471 struct sd_lun *un = NULL; 22472 enum uio_seg uioseg; 22473 char cdb[CDB_GROUP0]; 22474 int rval = 0; 22475 22476 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22477 return (ENXIO); 22478 } 22479 22480 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 22481 22482 scmd = (struct uscsi_cmd *) 22483 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 22484 22485 #ifdef _MULTI_DATAMODEL 22486 switch (model = ddi_model_convert_from(flag & FMODELS)) { 22487 case DDI_MODEL_ILP32: 22488 { 22489 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 22490 rval = EFAULT; 22491 goto done; 22492 } 22493 /* 22494 * Convert the ILP32 uscsi data from the 22495 * application to LP64 for internal use. 22496 */ 22497 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 22498 break; 22499 } 22500 case DDI_MODEL_NONE: 22501 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22502 rval = EFAULT; 22503 goto done; 22504 } 22505 break; 22506 } 22507 #else /* ! _MULTI_DATAMODEL */ 22508 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22509 rval = EFAULT; 22510 goto done; 22511 } 22512 #endif /* _MULTI_DATAMODEL */ 22513 22514 scmd->uscsi_flags &= ~USCSI_NOINTR; 22515 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22516 if (un->un_f_format_in_progress == TRUE) { 22517 rval = EAGAIN; 22518 goto done; 22519 } 22520 22521 /* 22522 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22523 * we will have a valid cdb[0] to test. 22524 */ 22525 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22526 (cdb[0] == SCMD_FORMAT)) { 22527 SD_TRACE(SD_LOG_IOCTL, un, 22528 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22529 mutex_enter(SD_MUTEX(un)); 22530 un->un_f_format_in_progress = TRUE; 22531 mutex_exit(SD_MUTEX(un)); 22532 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22533 SD_PATH_STANDARD); 22534 mutex_enter(SD_MUTEX(un)); 22535 un->un_f_format_in_progress = FALSE; 22536 mutex_exit(SD_MUTEX(un)); 22537 } else { 22538 SD_TRACE(SD_LOG_IOCTL, un, 22539 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22540 /* 22541 * It's OK to fall into here even if the ddi_copyin() 22542 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22543 * does this same copyin and will return the EFAULT 22544 * if it fails. 22545 */ 22546 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22547 SD_PATH_STANDARD); 22548 } 22549 #ifdef _MULTI_DATAMODEL 22550 switch (model) { 22551 case DDI_MODEL_ILP32: 22552 /* 22553 * Convert back to ILP32 before copyout to the 22554 * application 22555 */ 22556 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22557 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22558 if (rval != 0) { 22559 rval = EFAULT; 22560 } 22561 } 22562 break; 22563 case DDI_MODEL_NONE: 22564 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22565 if (rval != 0) { 22566 rval = EFAULT; 22567 } 22568 } 22569 break; 22570 } 22571 #else /* ! _MULTI_DATAMODE */ 22572 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22573 if (rval != 0) { 22574 rval = EFAULT; 22575 } 22576 } 22577 #endif /* _MULTI_DATAMODE */ 22578 done: 22579 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22580 22581 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22582 22583 return (rval); 22584 } 22585 22586 22587 /* 22588 * Function: sd_dkio_ctrl_info 22589 * 22590 * Description: This routine is the driver entry point for handling controller 22591 * information ioctl requests (DKIOCINFO). 22592 * 22593 * Arguments: dev - the device number 22594 * arg - pointer to user provided dk_cinfo structure 22595 * specifying the controller type and attributes. 22596 * flag - this argument is a pass through to ddi_copyxxx() 22597 * directly from the mode argument of ioctl(). 22598 * 22599 * Return Code: 0 22600 * EFAULT 22601 * ENXIO 22602 */ 22603 22604 static int 22605 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22606 { 22607 struct sd_lun *un = NULL; 22608 struct dk_cinfo *info; 22609 dev_info_t *pdip; 22610 int lun, tgt; 22611 22612 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22613 return (ENXIO); 22614 } 22615 22616 info = (struct dk_cinfo *) 22617 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22618 22619 switch (un->un_ctype) { 22620 case CTYPE_CDROM: 22621 info->dki_ctype = DKC_CDROM; 22622 break; 22623 default: 22624 info->dki_ctype = DKC_SCSI_CCS; 22625 break; 22626 } 22627 pdip = ddi_get_parent(SD_DEVINFO(un)); 22628 info->dki_cnum = ddi_get_instance(pdip); 22629 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22630 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22631 } else { 22632 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22633 DK_DEVLEN - 1); 22634 } 22635 22636 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22637 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22638 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22639 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22640 22641 /* Unit Information */ 22642 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22643 info->dki_slave = ((tgt << 3) | lun); 22644 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22645 DK_DEVLEN - 1); 22646 info->dki_flags = DKI_FMTVOL; 22647 info->dki_partition = SDPART(dev); 22648 22649 /* Max Transfer size of this device in blocks */ 22650 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22651 info->dki_addr = 0; 22652 info->dki_space = 0; 22653 info->dki_prio = 0; 22654 info->dki_vec = 0; 22655 22656 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22657 kmem_free(info, sizeof (struct dk_cinfo)); 22658 return (EFAULT); 22659 } else { 22660 kmem_free(info, sizeof (struct dk_cinfo)); 22661 return (0); 22662 } 22663 } 22664 22665 22666 /* 22667 * Function: sd_get_media_info 22668 * 22669 * Description: This routine is the driver entry point for handling ioctl 22670 * requests for the media type or command set profile used by the 22671 * drive to operate on the media (DKIOCGMEDIAINFO). 22672 * 22673 * Arguments: dev - the device number 22674 * arg - pointer to user provided dk_minfo structure 22675 * specifying the media type, logical block size and 22676 * drive capacity. 22677 * flag - this argument is a pass through to ddi_copyxxx() 22678 * directly from the mode argument of ioctl(). 22679 * 22680 * Return Code: 0 22681 * EACCESS 22682 * EFAULT 22683 * ENXIO 22684 * EIO 22685 */ 22686 22687 static int 22688 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22689 { 22690 struct sd_lun *un = NULL; 22691 struct uscsi_cmd com; 22692 struct scsi_inquiry *sinq; 22693 struct dk_minfo media_info; 22694 u_longlong_t media_capacity; 22695 uint64_t capacity; 22696 uint_t lbasize; 22697 uchar_t *out_data; 22698 uchar_t *rqbuf; 22699 int rval = 0; 22700 int rtn; 22701 22702 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22703 (un->un_state == SD_STATE_OFFLINE)) { 22704 return (ENXIO); 22705 } 22706 22707 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22708 22709 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22710 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22711 22712 /* Issue a TUR to determine if the drive is ready with media present */ 22713 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22714 if (rval == ENXIO) { 22715 goto done; 22716 } 22717 22718 /* Now get configuration data */ 22719 if (ISCD(un)) { 22720 media_info.dki_media_type = DK_CDROM; 22721 22722 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22723 if (un->un_f_mmc_cap == TRUE) { 22724 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22725 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22726 22727 if (rtn) { 22728 /* 22729 * Failed for other than an illegal request 22730 * or command not supported 22731 */ 22732 if ((com.uscsi_status == STATUS_CHECK) && 22733 (com.uscsi_rqstatus == STATUS_GOOD)) { 22734 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22735 (rqbuf[12] != 0x20)) { 22736 rval = EIO; 22737 goto done; 22738 } 22739 } 22740 } else { 22741 /* 22742 * The GET CONFIGURATION command succeeded 22743 * so set the media type according to the 22744 * returned data 22745 */ 22746 media_info.dki_media_type = out_data[6]; 22747 media_info.dki_media_type <<= 8; 22748 media_info.dki_media_type |= out_data[7]; 22749 } 22750 } 22751 } else { 22752 /* 22753 * The profile list is not available, so we attempt to identify 22754 * the media type based on the inquiry data 22755 */ 22756 sinq = un->un_sd->sd_inq; 22757 if (sinq->inq_qual == 0) { 22758 /* This is a direct access device */ 22759 media_info.dki_media_type = DK_FIXED_DISK; 22760 22761 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22762 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22763 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22764 media_info.dki_media_type = DK_ZIP; 22765 } else if ( 22766 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22767 media_info.dki_media_type = DK_JAZ; 22768 } 22769 } 22770 } else { 22771 /* Not a CD or direct access so return unknown media */ 22772 media_info.dki_media_type = DK_UNKNOWN; 22773 } 22774 } 22775 22776 /* Now read the capacity so we can provide the lbasize and capacity */ 22777 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22778 SD_PATH_DIRECT)) { 22779 case 0: 22780 break; 22781 case EACCES: 22782 rval = EACCES; 22783 goto done; 22784 default: 22785 rval = EIO; 22786 goto done; 22787 } 22788 22789 media_info.dki_lbsize = lbasize; 22790 media_capacity = capacity; 22791 22792 /* 22793 * sd_send_scsi_READ_CAPACITY() reports capacity in 22794 * un->un_sys_blocksize chunks. So we need to convert it into 22795 * cap.lbasize chunks. 22796 */ 22797 media_capacity *= un->un_sys_blocksize; 22798 media_capacity /= lbasize; 22799 media_info.dki_capacity = media_capacity; 22800 22801 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22802 rval = EFAULT; 22803 /* Put goto. Anybody might add some code below in future */ 22804 goto done; 22805 } 22806 done: 22807 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22808 kmem_free(rqbuf, SENSE_LENGTH); 22809 return (rval); 22810 } 22811 22812 22813 /* 22814 * Function: sd_dkio_get_geometry 22815 * 22816 * Description: This routine is the driver entry point for handling user 22817 * requests to get the device geometry (DKIOCGGEOM). 22818 * 22819 * Arguments: dev - the device number 22820 * arg - pointer to user provided dk_geom structure specifying 22821 * the controller's notion of the current geometry. 22822 * flag - this argument is a pass through to ddi_copyxxx() 22823 * directly from the mode argument of ioctl(). 22824 * geom_validated - flag indicating if the device geometry has been 22825 * previously validated in the sdioctl routine. 22826 * 22827 * Return Code: 0 22828 * EFAULT 22829 * ENXIO 22830 * EIO 22831 */ 22832 22833 static int 22834 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22835 { 22836 struct sd_lun *un = NULL; 22837 struct dk_geom *tmp_geom = NULL; 22838 int rval = 0; 22839 22840 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22841 return (ENXIO); 22842 } 22843 22844 if (geom_validated == FALSE) { 22845 /* 22846 * sd_validate_geometry does not spin a disk up 22847 * if it was spun down. We need to make sure it 22848 * is ready. 22849 */ 22850 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22851 return (rval); 22852 } 22853 mutex_enter(SD_MUTEX(un)); 22854 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22855 mutex_exit(SD_MUTEX(un)); 22856 } 22857 if (rval) 22858 return (rval); 22859 22860 /* 22861 * It is possible that un_solaris_size is 0(uninitialized) 22862 * after sd_unit_attach. Reservation conflict may cause the 22863 * above situation. Thus, the zero check of un_solaris_size 22864 * should occur after the sd_validate_geometry() call. 22865 */ 22866 #if defined(__i386) || defined(__amd64) 22867 if (un->un_solaris_size == 0) { 22868 return (EIO); 22869 } 22870 #endif 22871 22872 /* 22873 * Make a local copy of the soft state geometry to avoid some potential 22874 * race conditions associated with holding the mutex and updating the 22875 * write_reinstruct value 22876 */ 22877 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22878 mutex_enter(SD_MUTEX(un)); 22879 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22880 mutex_exit(SD_MUTEX(un)); 22881 22882 if (tmp_geom->dkg_write_reinstruct == 0) { 22883 tmp_geom->dkg_write_reinstruct = 22884 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22885 sd_rot_delay) / (int)60000); 22886 } 22887 22888 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22889 flag); 22890 if (rval != 0) { 22891 rval = EFAULT; 22892 } 22893 22894 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22895 return (rval); 22896 22897 } 22898 22899 22900 /* 22901 * Function: sd_dkio_set_geometry 22902 * 22903 * Description: This routine is the driver entry point for handling user 22904 * requests to set the device geometry (DKIOCSGEOM). The actual 22905 * device geometry is not updated, just the driver "notion" of it. 22906 * 22907 * Arguments: dev - the device number 22908 * arg - pointer to user provided dk_geom structure used to set 22909 * the controller's notion of the current geometry. 22910 * flag - this argument is a pass through to ddi_copyxxx() 22911 * directly from the mode argument of ioctl(). 22912 * 22913 * Return Code: 0 22914 * EFAULT 22915 * ENXIO 22916 * EIO 22917 */ 22918 22919 static int 22920 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22921 { 22922 struct sd_lun *un = NULL; 22923 struct dk_geom *tmp_geom; 22924 struct dk_map *lp; 22925 int rval = 0; 22926 int i; 22927 22928 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22929 return (ENXIO); 22930 } 22931 22932 /* 22933 * Make sure there is no reservation conflict on the lun. 22934 */ 22935 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22936 return (EACCES); 22937 } 22938 22939 #if defined(__i386) || defined(__amd64) 22940 if (un->un_solaris_size == 0) { 22941 return (EIO); 22942 } 22943 #endif 22944 22945 /* 22946 * We need to copy the user specified geometry into local 22947 * storage and then update the softstate. We don't want to hold 22948 * the mutex and copyin directly from the user to the soft state 22949 */ 22950 tmp_geom = (struct dk_geom *) 22951 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22952 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22953 if (rval != 0) { 22954 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22955 return (EFAULT); 22956 } 22957 22958 mutex_enter(SD_MUTEX(un)); 22959 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22960 for (i = 0; i < NDKMAP; i++) { 22961 lp = &un->un_map[i]; 22962 un->un_offset[i] = 22963 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22964 #if defined(__i386) || defined(__amd64) 22965 un->un_offset[i] += un->un_solaris_offset; 22966 #endif 22967 } 22968 un->un_f_geometry_is_valid = FALSE; 22969 mutex_exit(SD_MUTEX(un)); 22970 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22971 22972 return (rval); 22973 } 22974 22975 22976 /* 22977 * Function: sd_dkio_get_partition 22978 * 22979 * Description: This routine is the driver entry point for handling user 22980 * requests to get the partition table (DKIOCGAPART). 22981 * 22982 * Arguments: dev - the device number 22983 * arg - pointer to user provided dk_allmap structure specifying 22984 * the controller's notion of the current partition table. 22985 * flag - this argument is a pass through to ddi_copyxxx() 22986 * directly from the mode argument of ioctl(). 22987 * geom_validated - flag indicating if the device geometry has been 22988 * previously validated in the sdioctl routine. 22989 * 22990 * Return Code: 0 22991 * EFAULT 22992 * ENXIO 22993 * EIO 22994 */ 22995 22996 static int 22997 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 22998 { 22999 struct sd_lun *un = NULL; 23000 int rval = 0; 23001 int size; 23002 23003 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23004 return (ENXIO); 23005 } 23006 23007 /* 23008 * Make sure the geometry is valid before getting the partition 23009 * information. 23010 */ 23011 mutex_enter(SD_MUTEX(un)); 23012 if (geom_validated == FALSE) { 23013 /* 23014 * sd_validate_geometry does not spin a disk up 23015 * if it was spun down. We need to make sure it 23016 * is ready before validating the geometry. 23017 */ 23018 mutex_exit(SD_MUTEX(un)); 23019 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23020 return (rval); 23021 } 23022 mutex_enter(SD_MUTEX(un)); 23023 23024 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23025 mutex_exit(SD_MUTEX(un)); 23026 return (rval); 23027 } 23028 } 23029 mutex_exit(SD_MUTEX(un)); 23030 23031 /* 23032 * It is possible that un_solaris_size is 0(uninitialized) 23033 * after sd_unit_attach. Reservation conflict may cause the 23034 * above situation. Thus, the zero check of un_solaris_size 23035 * should occur after the sd_validate_geometry() call. 23036 */ 23037 #if defined(__i386) || defined(__amd64) 23038 if (un->un_solaris_size == 0) { 23039 return (EIO); 23040 } 23041 #endif 23042 23043 #ifdef _MULTI_DATAMODEL 23044 switch (ddi_model_convert_from(flag & FMODELS)) { 23045 case DDI_MODEL_ILP32: { 23046 struct dk_map32 dk_map32[NDKMAP]; 23047 int i; 23048 23049 for (i = 0; i < NDKMAP; i++) { 23050 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 23051 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 23052 } 23053 size = NDKMAP * sizeof (struct dk_map32); 23054 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 23055 if (rval != 0) { 23056 rval = EFAULT; 23057 } 23058 break; 23059 } 23060 case DDI_MODEL_NONE: 23061 size = NDKMAP * sizeof (struct dk_map); 23062 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23063 if (rval != 0) { 23064 rval = EFAULT; 23065 } 23066 break; 23067 } 23068 #else /* ! _MULTI_DATAMODEL */ 23069 size = NDKMAP * sizeof (struct dk_map); 23070 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23071 if (rval != 0) { 23072 rval = EFAULT; 23073 } 23074 #endif /* _MULTI_DATAMODEL */ 23075 return (rval); 23076 } 23077 23078 23079 /* 23080 * Function: sd_dkio_set_partition 23081 * 23082 * Description: This routine is the driver entry point for handling user 23083 * requests to set the partition table (DKIOCSAPART). The actual 23084 * device partition is not updated. 23085 * 23086 * Arguments: dev - the device number 23087 * arg - pointer to user provided dk_allmap structure used to set 23088 * the controller's notion of the partition table. 23089 * flag - this argument is a pass through to ddi_copyxxx() 23090 * directly from the mode argument of ioctl(). 23091 * 23092 * Return Code: 0 23093 * EINVAL 23094 * EFAULT 23095 * ENXIO 23096 * EIO 23097 */ 23098 23099 static int 23100 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 23101 { 23102 struct sd_lun *un = NULL; 23103 struct dk_map dk_map[NDKMAP]; 23104 struct dk_map *lp; 23105 int rval = 0; 23106 int size; 23107 int i; 23108 #if defined(_SUNOS_VTOC_16) 23109 struct dkl_partition *vp; 23110 #endif 23111 23112 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23113 return (ENXIO); 23114 } 23115 23116 /* 23117 * Set the map for all logical partitions. We lock 23118 * the priority just to make sure an interrupt doesn't 23119 * come in while the map is half updated. 23120 */ 23121 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 23122 mutex_enter(SD_MUTEX(un)); 23123 if (un->un_blockcount > DK_MAX_BLOCKS) { 23124 mutex_exit(SD_MUTEX(un)); 23125 return (ENOTSUP); 23126 } 23127 mutex_exit(SD_MUTEX(un)); 23128 23129 /* 23130 * Make sure there is no reservation conflict on the lun. 23131 */ 23132 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 23133 return (EACCES); 23134 } 23135 23136 #if defined(__i386) || defined(__amd64) 23137 if (un->un_solaris_size == 0) { 23138 return (EIO); 23139 } 23140 #endif 23141 23142 #ifdef _MULTI_DATAMODEL 23143 switch (ddi_model_convert_from(flag & FMODELS)) { 23144 case DDI_MODEL_ILP32: { 23145 struct dk_map32 dk_map32[NDKMAP]; 23146 23147 size = NDKMAP * sizeof (struct dk_map32); 23148 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 23149 if (rval != 0) { 23150 return (EFAULT); 23151 } 23152 for (i = 0; i < NDKMAP; i++) { 23153 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 23154 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 23155 } 23156 break; 23157 } 23158 case DDI_MODEL_NONE: 23159 size = NDKMAP * sizeof (struct dk_map); 23160 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23161 if (rval != 0) { 23162 return (EFAULT); 23163 } 23164 break; 23165 } 23166 #else /* ! _MULTI_DATAMODEL */ 23167 size = NDKMAP * sizeof (struct dk_map); 23168 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23169 if (rval != 0) { 23170 return (EFAULT); 23171 } 23172 #endif /* _MULTI_DATAMODEL */ 23173 23174 mutex_enter(SD_MUTEX(un)); 23175 /* Note: The size used in this bcopy is set based upon the data model */ 23176 bcopy(dk_map, un->un_map, size); 23177 #if defined(_SUNOS_VTOC_16) 23178 vp = (struct dkl_partition *)&(un->un_vtoc); 23179 #endif /* defined(_SUNOS_VTOC_16) */ 23180 for (i = 0; i < NDKMAP; i++) { 23181 lp = &un->un_map[i]; 23182 un->un_offset[i] = 23183 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23184 #if defined(_SUNOS_VTOC_16) 23185 vp->p_start = un->un_offset[i]; 23186 vp->p_size = lp->dkl_nblk; 23187 vp++; 23188 #endif /* defined(_SUNOS_VTOC_16) */ 23189 #if defined(__i386) || defined(__amd64) 23190 un->un_offset[i] += un->un_solaris_offset; 23191 #endif 23192 } 23193 mutex_exit(SD_MUTEX(un)); 23194 return (rval); 23195 } 23196 23197 23198 /* 23199 * Function: sd_dkio_get_vtoc 23200 * 23201 * Description: This routine is the driver entry point for handling user 23202 * requests to get the current volume table of contents 23203 * (DKIOCGVTOC). 23204 * 23205 * Arguments: dev - the device number 23206 * arg - pointer to user provided vtoc structure specifying 23207 * the current vtoc. 23208 * flag - this argument is a pass through to ddi_copyxxx() 23209 * directly from the mode argument of ioctl(). 23210 * geom_validated - flag indicating if the device geometry has been 23211 * previously validated in the sdioctl routine. 23212 * 23213 * Return Code: 0 23214 * EFAULT 23215 * ENXIO 23216 * EIO 23217 */ 23218 23219 static int 23220 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 23221 { 23222 struct sd_lun *un = NULL; 23223 #if defined(_SUNOS_VTOC_8) 23224 struct vtoc user_vtoc; 23225 #endif /* defined(_SUNOS_VTOC_8) */ 23226 int rval = 0; 23227 23228 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23229 return (ENXIO); 23230 } 23231 23232 mutex_enter(SD_MUTEX(un)); 23233 if (geom_validated == FALSE) { 23234 /* 23235 * sd_validate_geometry does not spin a disk up 23236 * if it was spun down. We need to make sure it 23237 * is ready. 23238 */ 23239 mutex_exit(SD_MUTEX(un)); 23240 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23241 return (rval); 23242 } 23243 mutex_enter(SD_MUTEX(un)); 23244 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23245 mutex_exit(SD_MUTEX(un)); 23246 return (rval); 23247 } 23248 } 23249 23250 #if defined(_SUNOS_VTOC_8) 23251 sd_build_user_vtoc(un, &user_vtoc); 23252 mutex_exit(SD_MUTEX(un)); 23253 23254 #ifdef _MULTI_DATAMODEL 23255 switch (ddi_model_convert_from(flag & FMODELS)) { 23256 case DDI_MODEL_ILP32: { 23257 struct vtoc32 user_vtoc32; 23258 23259 vtoctovtoc32(user_vtoc, user_vtoc32); 23260 if (ddi_copyout(&user_vtoc32, (void *)arg, 23261 sizeof (struct vtoc32), flag)) { 23262 return (EFAULT); 23263 } 23264 break; 23265 } 23266 23267 case DDI_MODEL_NONE: 23268 if (ddi_copyout(&user_vtoc, (void *)arg, 23269 sizeof (struct vtoc), flag)) { 23270 return (EFAULT); 23271 } 23272 break; 23273 } 23274 #else /* ! _MULTI_DATAMODEL */ 23275 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 23276 return (EFAULT); 23277 } 23278 #endif /* _MULTI_DATAMODEL */ 23279 23280 #elif defined(_SUNOS_VTOC_16) 23281 mutex_exit(SD_MUTEX(un)); 23282 23283 #ifdef _MULTI_DATAMODEL 23284 /* 23285 * The un_vtoc structure is a "struct dk_vtoc" which is always 23286 * 32-bit to maintain compatibility with existing on-disk 23287 * structures. Thus, we need to convert the structure when copying 23288 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 23289 * program. If the target is a 32-bit program, then no conversion 23290 * is necessary. 23291 */ 23292 /* LINTED: logical expression always true: op "||" */ 23293 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 23294 switch (ddi_model_convert_from(flag & FMODELS)) { 23295 case DDI_MODEL_ILP32: 23296 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 23297 sizeof (un->un_vtoc), flag)) { 23298 return (EFAULT); 23299 } 23300 break; 23301 23302 case DDI_MODEL_NONE: { 23303 struct vtoc user_vtoc; 23304 23305 vtoc32tovtoc(un->un_vtoc, user_vtoc); 23306 if (ddi_copyout(&user_vtoc, (void *)arg, 23307 sizeof (struct vtoc), flag)) { 23308 return (EFAULT); 23309 } 23310 break; 23311 } 23312 } 23313 #else /* ! _MULTI_DATAMODEL */ 23314 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 23315 flag)) { 23316 return (EFAULT); 23317 } 23318 #endif /* _MULTI_DATAMODEL */ 23319 #else 23320 #error "No VTOC format defined." 23321 #endif 23322 23323 return (rval); 23324 } 23325 23326 static int 23327 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 23328 { 23329 struct sd_lun *un = NULL; 23330 dk_efi_t user_efi; 23331 int rval = 0; 23332 void *buffer; 23333 23334 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23335 return (ENXIO); 23336 23337 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23338 return (EFAULT); 23339 23340 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23341 23342 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23343 (user_efi.dki_length > un->un_max_xfer_size)) 23344 return (EINVAL); 23345 23346 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23347 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 23348 user_efi.dki_lba, SD_PATH_DIRECT); 23349 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 23350 user_efi.dki_length, flag) != 0) 23351 rval = EFAULT; 23352 23353 kmem_free(buffer, user_efi.dki_length); 23354 return (rval); 23355 } 23356 23357 /* 23358 * Function: sd_build_user_vtoc 23359 * 23360 * Description: This routine populates a pass by reference variable with the 23361 * current volume table of contents. 23362 * 23363 * Arguments: un - driver soft state (unit) structure 23364 * user_vtoc - pointer to vtoc structure to be populated 23365 */ 23366 23367 static void 23368 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23369 { 23370 struct dk_map2 *lpart; 23371 struct dk_map *lmap; 23372 struct partition *vpart; 23373 int nblks; 23374 int i; 23375 23376 ASSERT(mutex_owned(SD_MUTEX(un))); 23377 23378 /* 23379 * Return vtoc structure fields in the provided VTOC area, addressed 23380 * by *vtoc. 23381 */ 23382 bzero(user_vtoc, sizeof (struct vtoc)); 23383 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 23384 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 23385 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 23386 user_vtoc->v_sanity = VTOC_SANE; 23387 user_vtoc->v_version = un->un_vtoc.v_version; 23388 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 23389 user_vtoc->v_sectorsz = un->un_sys_blocksize; 23390 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 23391 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 23392 sizeof (un->un_vtoc.v_reserved)); 23393 /* 23394 * Convert partitioning information. 23395 * 23396 * Note the conversion from starting cylinder number 23397 * to starting sector number. 23398 */ 23399 lmap = un->un_map; 23400 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 23401 vpart = user_vtoc->v_part; 23402 23403 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23404 23405 for (i = 0; i < V_NUMPAR; i++) { 23406 vpart->p_tag = lpart->p_tag; 23407 vpart->p_flag = lpart->p_flag; 23408 vpart->p_start = lmap->dkl_cylno * nblks; 23409 vpart->p_size = lmap->dkl_nblk; 23410 lmap++; 23411 lpart++; 23412 vpart++; 23413 23414 /* (4364927) */ 23415 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 23416 } 23417 23418 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 23419 } 23420 23421 static int 23422 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 23423 { 23424 struct sd_lun *un = NULL; 23425 struct partition64 p64; 23426 int rval = 0; 23427 uint_t nparts; 23428 efi_gpe_t *partitions; 23429 efi_gpt_t *buffer; 23430 diskaddr_t gpe_lba; 23431 23432 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23433 return (ENXIO); 23434 } 23435 23436 if (ddi_copyin((const void *)arg, &p64, 23437 sizeof (struct partition64), flag)) { 23438 return (EFAULT); 23439 } 23440 23441 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23442 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23443 1, SD_PATH_DIRECT); 23444 if (rval != 0) 23445 goto done_error; 23446 23447 sd_swap_efi_gpt(buffer); 23448 23449 if ((rval = sd_validate_efi(buffer)) != 0) 23450 goto done_error; 23451 23452 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23453 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23454 if (p64.p_partno > nparts) { 23455 /* couldn't find it */ 23456 rval = ESRCH; 23457 goto done_error; 23458 } 23459 /* 23460 * if we're dealing with a partition that's out of the normal 23461 * 16K block, adjust accordingly 23462 */ 23463 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23464 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23465 gpe_lba, SD_PATH_DIRECT); 23466 if (rval) { 23467 goto done_error; 23468 } 23469 partitions = (efi_gpe_t *)buffer; 23470 23471 sd_swap_efi_gpe(nparts, partitions); 23472 23473 partitions += p64.p_partno; 23474 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23475 sizeof (struct uuid)); 23476 p64.p_start = partitions->efi_gpe_StartingLBA; 23477 p64.p_size = partitions->efi_gpe_EndingLBA - 23478 p64.p_start + 1; 23479 23480 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23481 rval = EFAULT; 23482 23483 done_error: 23484 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23485 return (rval); 23486 } 23487 23488 23489 /* 23490 * Function: sd_dkio_set_vtoc 23491 * 23492 * Description: This routine is the driver entry point for handling user 23493 * requests to set the current volume table of contents 23494 * (DKIOCSVTOC). 23495 * 23496 * Arguments: dev - the device number 23497 * arg - pointer to user provided vtoc structure used to set the 23498 * current vtoc. 23499 * flag - this argument is a pass through to ddi_copyxxx() 23500 * directly from the mode argument of ioctl(). 23501 * 23502 * Return Code: 0 23503 * EFAULT 23504 * ENXIO 23505 * EINVAL 23506 * ENOTSUP 23507 */ 23508 23509 static int 23510 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23511 { 23512 struct sd_lun *un = NULL; 23513 struct vtoc user_vtoc; 23514 int rval = 0; 23515 23516 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23517 return (ENXIO); 23518 } 23519 23520 #if defined(__i386) || defined(__amd64) 23521 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23522 return (EINVAL); 23523 } 23524 #endif 23525 23526 #ifdef _MULTI_DATAMODEL 23527 switch (ddi_model_convert_from(flag & FMODELS)) { 23528 case DDI_MODEL_ILP32: { 23529 struct vtoc32 user_vtoc32; 23530 23531 if (ddi_copyin((const void *)arg, &user_vtoc32, 23532 sizeof (struct vtoc32), flag)) { 23533 return (EFAULT); 23534 } 23535 vtoc32tovtoc(user_vtoc32, user_vtoc); 23536 break; 23537 } 23538 23539 case DDI_MODEL_NONE: 23540 if (ddi_copyin((const void *)arg, &user_vtoc, 23541 sizeof (struct vtoc), flag)) { 23542 return (EFAULT); 23543 } 23544 break; 23545 } 23546 #else /* ! _MULTI_DATAMODEL */ 23547 if (ddi_copyin((const void *)arg, &user_vtoc, 23548 sizeof (struct vtoc), flag)) { 23549 return (EFAULT); 23550 } 23551 #endif /* _MULTI_DATAMODEL */ 23552 23553 mutex_enter(SD_MUTEX(un)); 23554 if (un->un_blockcount > DK_MAX_BLOCKS) { 23555 mutex_exit(SD_MUTEX(un)); 23556 return (ENOTSUP); 23557 } 23558 if (un->un_g.dkg_ncyl == 0) { 23559 mutex_exit(SD_MUTEX(un)); 23560 return (EINVAL); 23561 } 23562 23563 mutex_exit(SD_MUTEX(un)); 23564 sd_clear_efi(un); 23565 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23566 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23567 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23568 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23569 un->un_node_type, NULL); 23570 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23571 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23572 un->un_node_type, NULL); 23573 mutex_enter(SD_MUTEX(un)); 23574 23575 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23576 if ((rval = sd_write_label(dev)) == 0) { 23577 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23578 != 0) { 23579 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23580 "sd_dkio_set_vtoc: " 23581 "Failed validate geometry\n"); 23582 } 23583 } 23584 } 23585 23586 /* 23587 * If sd_build_label_vtoc, or sd_write_label failed above write the 23588 * devid anyway, what can it hurt? Also preserve the device id by 23589 * writing to the disk acyl for the case where a devid has been 23590 * fabricated. 23591 */ 23592 if (un->un_f_devid_supported && 23593 (un->un_f_opt_fab_devid == TRUE)) { 23594 if (un->un_devid == NULL) { 23595 sd_register_devid(un, SD_DEVINFO(un), 23596 SD_TARGET_IS_UNRESERVED); 23597 } else { 23598 /* 23599 * The device id for this disk has been 23600 * fabricated. Fabricated device id's are 23601 * managed by storing them in the last 2 23602 * available sectors on the drive. The device 23603 * id must be preserved by writing it back out 23604 * to this location. 23605 */ 23606 if (sd_write_deviceid(un) != 0) { 23607 ddi_devid_free(un->un_devid); 23608 un->un_devid = NULL; 23609 } 23610 } 23611 } 23612 mutex_exit(SD_MUTEX(un)); 23613 return (rval); 23614 } 23615 23616 23617 /* 23618 * Function: sd_build_label_vtoc 23619 * 23620 * Description: This routine updates the driver soft state current volume table 23621 * of contents based on a user specified vtoc. 23622 * 23623 * Arguments: un - driver soft state (unit) structure 23624 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23625 * to update the driver soft state. 23626 * 23627 * Return Code: 0 23628 * EINVAL 23629 */ 23630 23631 static int 23632 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23633 { 23634 struct dk_map *lmap; 23635 struct partition *vpart; 23636 int nblks; 23637 #if defined(_SUNOS_VTOC_8) 23638 int ncyl; 23639 struct dk_map2 *lpart; 23640 #endif /* defined(_SUNOS_VTOC_8) */ 23641 int i; 23642 23643 ASSERT(mutex_owned(SD_MUTEX(un))); 23644 23645 /* Sanity-check the vtoc */ 23646 if (user_vtoc->v_sanity != VTOC_SANE || 23647 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23648 user_vtoc->v_nparts != V_NUMPAR) { 23649 return (EINVAL); 23650 } 23651 23652 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23653 if (nblks == 0) { 23654 return (EINVAL); 23655 } 23656 23657 #if defined(_SUNOS_VTOC_8) 23658 vpart = user_vtoc->v_part; 23659 for (i = 0; i < V_NUMPAR; i++) { 23660 if ((vpart->p_start % nblks) != 0) { 23661 return (EINVAL); 23662 } 23663 ncyl = vpart->p_start / nblks; 23664 ncyl += vpart->p_size / nblks; 23665 if ((vpart->p_size % nblks) != 0) { 23666 ncyl++; 23667 } 23668 if (ncyl > (int)un->un_g.dkg_ncyl) { 23669 return (EINVAL); 23670 } 23671 vpart++; 23672 } 23673 #endif /* defined(_SUNOS_VTOC_8) */ 23674 23675 /* Put appropriate vtoc structure fields into the disk label */ 23676 #if defined(_SUNOS_VTOC_16) 23677 /* 23678 * The vtoc is always a 32bit data structure to maintain the 23679 * on-disk format. Convert "in place" instead of bcopying it. 23680 */ 23681 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23682 23683 /* 23684 * in the 16-slice vtoc, starting sectors are expressed in 23685 * numbers *relative* to the start of the Solaris fdisk partition. 23686 */ 23687 lmap = un->un_map; 23688 vpart = user_vtoc->v_part; 23689 23690 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23691 lmap->dkl_cylno = vpart->p_start / nblks; 23692 lmap->dkl_nblk = vpart->p_size; 23693 } 23694 23695 #elif defined(_SUNOS_VTOC_8) 23696 23697 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23698 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23699 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23700 23701 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23702 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23703 23704 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23705 23706 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23707 23708 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23709 sizeof (un->un_vtoc.v_reserved)); 23710 23711 /* 23712 * Note the conversion from starting sector number 23713 * to starting cylinder number. 23714 * Return error if division results in a remainder. 23715 */ 23716 lmap = un->un_map; 23717 lpart = un->un_vtoc.v_part; 23718 vpart = user_vtoc->v_part; 23719 23720 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23721 lpart->p_tag = vpart->p_tag; 23722 lpart->p_flag = vpart->p_flag; 23723 lmap->dkl_cylno = vpart->p_start / nblks; 23724 lmap->dkl_nblk = vpart->p_size; 23725 23726 lmap++; 23727 lpart++; 23728 vpart++; 23729 23730 /* (4387723) */ 23731 #ifdef _LP64 23732 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23733 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23734 } else { 23735 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23736 } 23737 #else 23738 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23739 #endif 23740 } 23741 23742 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23743 #else 23744 #error "No VTOC format defined." 23745 #endif 23746 return (0); 23747 } 23748 23749 /* 23750 * Function: sd_clear_efi 23751 * 23752 * Description: This routine clears all EFI labels. 23753 * 23754 * Arguments: un - driver soft state (unit) structure 23755 * 23756 * Return Code: void 23757 */ 23758 23759 static void 23760 sd_clear_efi(struct sd_lun *un) 23761 { 23762 efi_gpt_t *gpt; 23763 uint_t lbasize; 23764 uint64_t cap; 23765 int rval; 23766 23767 ASSERT(!mutex_owned(SD_MUTEX(un))); 23768 23769 mutex_enter(SD_MUTEX(un)); 23770 un->un_reserved = -1; 23771 mutex_exit(SD_MUTEX(un)); 23772 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23773 23774 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23775 goto done; 23776 } 23777 23778 sd_swap_efi_gpt(gpt); 23779 rval = sd_validate_efi(gpt); 23780 if (rval == 0) { 23781 /* clear primary */ 23782 bzero(gpt, sizeof (efi_gpt_t)); 23783 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23784 SD_PATH_DIRECT))) { 23785 SD_INFO(SD_LOG_IO_PARTITION, un, 23786 "sd_clear_efi: clear primary label failed\n"); 23787 } 23788 } 23789 /* the backup */ 23790 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23791 SD_PATH_DIRECT); 23792 if (rval) { 23793 goto done; 23794 } 23795 /* 23796 * The MMC standard allows READ CAPACITY to be 23797 * inaccurate by a bounded amount (in the interest of 23798 * response latency). As a result, failed READs are 23799 * commonplace (due to the reading of metadata and not 23800 * data). Depending on the per-Vendor/drive Sense data, 23801 * the failed READ can cause many (unnecessary) retries. 23802 */ 23803 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23804 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23805 SD_PATH_DIRECT)) != 0) { 23806 goto done; 23807 } 23808 sd_swap_efi_gpt(gpt); 23809 rval = sd_validate_efi(gpt); 23810 if (rval == 0) { 23811 /* clear backup */ 23812 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23813 cap-1); 23814 bzero(gpt, sizeof (efi_gpt_t)); 23815 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23816 cap-1, SD_PATH_DIRECT))) { 23817 SD_INFO(SD_LOG_IO_PARTITION, un, 23818 "sd_clear_efi: clear backup label failed\n"); 23819 } 23820 } else { 23821 /* 23822 * Refer to comments related to off-by-1 at the 23823 * header of this file 23824 */ 23825 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23826 cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23827 SD_PATH_DIRECT)) != 0) { 23828 goto done; 23829 } 23830 sd_swap_efi_gpt(gpt); 23831 rval = sd_validate_efi(gpt); 23832 if (rval == 0) { 23833 /* clear legacy backup EFI label */ 23834 SD_TRACE(SD_LOG_IOCTL, un, 23835 "sd_clear_efi clear backup@%lu\n", cap-2); 23836 bzero(gpt, sizeof (efi_gpt_t)); 23837 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23838 cap-2, SD_PATH_DIRECT))) { 23839 SD_INFO(SD_LOG_IO_PARTITION, 23840 un, "sd_clear_efi: " 23841 " clear legacy backup label failed\n"); 23842 } 23843 } 23844 } 23845 23846 done: 23847 kmem_free(gpt, sizeof (efi_gpt_t)); 23848 } 23849 23850 /* 23851 * Function: sd_set_vtoc 23852 * 23853 * Description: This routine writes data to the appropriate positions 23854 * 23855 * Arguments: un - driver soft state (unit) structure 23856 * dkl - the data to be written 23857 * 23858 * Return: void 23859 */ 23860 23861 static int 23862 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23863 { 23864 void *shadow_buf; 23865 uint_t label_addr; 23866 int sec; 23867 int blk; 23868 int head; 23869 int cyl; 23870 int rval; 23871 23872 #if defined(__i386) || defined(__amd64) 23873 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23874 #else 23875 /* Write the primary label at block 0 of the solaris partition. */ 23876 label_addr = 0; 23877 #endif 23878 23879 if (NOT_DEVBSIZE(un)) { 23880 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23881 /* 23882 * Read the target's first block. 23883 */ 23884 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23885 un->un_tgt_blocksize, label_addr, 23886 SD_PATH_STANDARD)) != 0) { 23887 goto exit; 23888 } 23889 /* 23890 * Copy the contents of the label into the shadow buffer 23891 * which is of the size of target block size. 23892 */ 23893 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23894 } 23895 23896 /* Write the primary label */ 23897 if (NOT_DEVBSIZE(un)) { 23898 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23899 label_addr, SD_PATH_STANDARD); 23900 } else { 23901 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23902 label_addr, SD_PATH_STANDARD); 23903 } 23904 if (rval != 0) { 23905 return (rval); 23906 } 23907 23908 /* 23909 * Calculate where the backup labels go. They are always on 23910 * the last alternate cylinder, but some older drives put them 23911 * on head 2 instead of the last head. They are always on the 23912 * first 5 odd sectors of the appropriate track. 23913 * 23914 * We have no choice at this point, but to believe that the 23915 * disk label is valid. Use the geometry of the disk 23916 * as described in the label. 23917 */ 23918 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23919 head = dkl->dkl_nhead - 1; 23920 23921 /* 23922 * Write and verify the backup labels. Make sure we don't try to 23923 * write past the last cylinder. 23924 */ 23925 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23926 blk = (daddr_t)( 23927 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23928 (head * dkl->dkl_nsect) + sec); 23929 #if defined(__i386) || defined(__amd64) 23930 blk += un->un_solaris_offset; 23931 #endif 23932 if (NOT_DEVBSIZE(un)) { 23933 uint64_t tblk; 23934 /* 23935 * Need to read the block first for read modify write. 23936 */ 23937 tblk = (uint64_t)blk; 23938 blk = (int)((tblk * un->un_sys_blocksize) / 23939 un->un_tgt_blocksize); 23940 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23941 un->un_tgt_blocksize, blk, 23942 SD_PATH_STANDARD)) != 0) { 23943 goto exit; 23944 } 23945 /* 23946 * Modify the shadow buffer with the label. 23947 */ 23948 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23949 rval = sd_send_scsi_WRITE(un, shadow_buf, 23950 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23951 } else { 23952 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23953 blk, SD_PATH_STANDARD); 23954 SD_INFO(SD_LOG_IO_PARTITION, un, 23955 "sd_set_vtoc: wrote backup label %d\n", blk); 23956 } 23957 if (rval != 0) { 23958 goto exit; 23959 } 23960 } 23961 exit: 23962 if (NOT_DEVBSIZE(un)) { 23963 kmem_free(shadow_buf, un->un_tgt_blocksize); 23964 } 23965 return (rval); 23966 } 23967 23968 /* 23969 * Function: sd_clear_vtoc 23970 * 23971 * Description: This routine clears out the VTOC labels. 23972 * 23973 * Arguments: un - driver soft state (unit) structure 23974 * 23975 * Return: void 23976 */ 23977 23978 static void 23979 sd_clear_vtoc(struct sd_lun *un) 23980 { 23981 struct dk_label *dkl; 23982 23983 mutex_exit(SD_MUTEX(un)); 23984 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23985 mutex_enter(SD_MUTEX(un)); 23986 /* 23987 * sd_set_vtoc uses these fields in order to figure out 23988 * where to overwrite the backup labels 23989 */ 23990 dkl->dkl_apc = un->un_g.dkg_apc; 23991 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23992 dkl->dkl_acyl = un->un_g.dkg_acyl; 23993 dkl->dkl_nhead = un->un_g.dkg_nhead; 23994 dkl->dkl_nsect = un->un_g.dkg_nsect; 23995 mutex_exit(SD_MUTEX(un)); 23996 (void) sd_set_vtoc(un, dkl); 23997 kmem_free(dkl, sizeof (struct dk_label)); 23998 23999 mutex_enter(SD_MUTEX(un)); 24000 } 24001 24002 /* 24003 * Function: sd_write_label 24004 * 24005 * Description: This routine will validate and write the driver soft state vtoc 24006 * contents to the device. 24007 * 24008 * Arguments: dev - the device number 24009 * 24010 * Return Code: the code returned by sd_send_scsi_cmd() 24011 * 0 24012 * EINVAL 24013 * ENXIO 24014 * ENOMEM 24015 */ 24016 24017 static int 24018 sd_write_label(dev_t dev) 24019 { 24020 struct sd_lun *un; 24021 struct dk_label *dkl; 24022 short sum; 24023 short *sp; 24024 int i; 24025 int rval; 24026 24027 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24028 (un->un_state == SD_STATE_OFFLINE)) { 24029 return (ENXIO); 24030 } 24031 ASSERT(mutex_owned(SD_MUTEX(un))); 24032 mutex_exit(SD_MUTEX(un)); 24033 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 24034 mutex_enter(SD_MUTEX(un)); 24035 24036 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 24037 dkl->dkl_rpm = un->un_g.dkg_rpm; 24038 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 24039 dkl->dkl_apc = un->un_g.dkg_apc; 24040 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 24041 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 24042 dkl->dkl_acyl = un->un_g.dkg_acyl; 24043 dkl->dkl_nhead = un->un_g.dkg_nhead; 24044 dkl->dkl_nsect = un->un_g.dkg_nsect; 24045 24046 #if defined(_SUNOS_VTOC_8) 24047 dkl->dkl_obs1 = un->un_g.dkg_obs1; 24048 dkl->dkl_obs2 = un->un_g.dkg_obs2; 24049 dkl->dkl_obs3 = un->un_g.dkg_obs3; 24050 for (i = 0; i < NDKMAP; i++) { 24051 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 24052 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 24053 } 24054 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 24055 #elif defined(_SUNOS_VTOC_16) 24056 dkl->dkl_skew = un->un_dkg_skew; 24057 #else 24058 #error "No VTOC format defined." 24059 #endif 24060 24061 dkl->dkl_magic = DKL_MAGIC; 24062 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 24063 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 24064 24065 /* Construct checksum for the new disk label */ 24066 sum = 0; 24067 sp = (short *)dkl; 24068 i = sizeof (struct dk_label) / sizeof (short); 24069 while (i--) { 24070 sum ^= *sp++; 24071 } 24072 dkl->dkl_cksum = sum; 24073 24074 mutex_exit(SD_MUTEX(un)); 24075 24076 rval = sd_set_vtoc(un, dkl); 24077 exit: 24078 kmem_free(dkl, sizeof (struct dk_label)); 24079 mutex_enter(SD_MUTEX(un)); 24080 return (rval); 24081 } 24082 24083 static int 24084 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 24085 { 24086 struct sd_lun *un = NULL; 24087 dk_efi_t user_efi; 24088 int rval = 0; 24089 void *buffer; 24090 int valid_efi; 24091 24092 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 24093 return (ENXIO); 24094 24095 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 24096 return (EFAULT); 24097 24098 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 24099 24100 if ((user_efi.dki_length % un->un_tgt_blocksize) || 24101 (user_efi.dki_length > un->un_max_xfer_size)) 24102 return (EINVAL); 24103 24104 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 24105 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 24106 rval = EFAULT; 24107 } else { 24108 /* 24109 * let's clear the vtoc labels and clear the softstate 24110 * vtoc. 24111 */ 24112 mutex_enter(SD_MUTEX(un)); 24113 if (un->un_vtoc.v_sanity == VTOC_SANE) { 24114 SD_TRACE(SD_LOG_IO_PARTITION, un, 24115 "sd_dkio_set_efi: CLEAR VTOC\n"); 24116 sd_clear_vtoc(un); 24117 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24118 mutex_exit(SD_MUTEX(un)); 24119 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 24120 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 24121 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 24122 S_IFBLK, 24123 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24124 un->un_node_type, NULL); 24125 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 24126 S_IFCHR, 24127 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24128 un->un_node_type, NULL); 24129 } else 24130 mutex_exit(SD_MUTEX(un)); 24131 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 24132 user_efi.dki_lba, SD_PATH_DIRECT); 24133 if (rval == 0) { 24134 mutex_enter(SD_MUTEX(un)); 24135 24136 /* 24137 * Set the un_reserved for valid efi label. 24138 * Function clear_efi in fdisk and efi_write in 24139 * libefi both change efi label on disk in 3 steps 24140 * 1. Change primary gpt and gpe 24141 * 2. Change backup gpe 24142 * 3. Change backup gpt, which is one block 24143 * We only reread the efi label after the 3rd step, 24144 * or there will be warning "primary label corrupt". 24145 */ 24146 if (user_efi.dki_length == un->un_tgt_blocksize) { 24147 un->un_f_geometry_is_valid = FALSE; 24148 valid_efi = sd_use_efi(un, SD_PATH_DIRECT); 24149 if ((valid_efi == 0) && 24150 un->un_f_devid_supported && 24151 (un->un_f_opt_fab_devid == TRUE)) { 24152 if (un->un_devid == NULL) { 24153 sd_register_devid(un, 24154 SD_DEVINFO(un), 24155 SD_TARGET_IS_UNRESERVED); 24156 } else { 24157 /* 24158 * The device id for this disk 24159 * has been fabricated. The 24160 * device id must be preserved 24161 * by writing it back out to 24162 * disk. 24163 */ 24164 if (sd_write_deviceid(un) 24165 != 0) { 24166 ddi_devid_free( 24167 un->un_devid); 24168 un->un_devid = NULL; 24169 } 24170 } 24171 } 24172 } 24173 24174 mutex_exit(SD_MUTEX(un)); 24175 } 24176 } 24177 kmem_free(buffer, user_efi.dki_length); 24178 return (rval); 24179 } 24180 24181 /* 24182 * Function: sd_dkio_get_mboot 24183 * 24184 * Description: This routine is the driver entry point for handling user 24185 * requests to get the current device mboot (DKIOCGMBOOT) 24186 * 24187 * Arguments: dev - the device number 24188 * arg - pointer to user provided mboot structure specifying 24189 * the current mboot. 24190 * flag - this argument is a pass through to ddi_copyxxx() 24191 * directly from the mode argument of ioctl(). 24192 * 24193 * Return Code: 0 24194 * EINVAL 24195 * EFAULT 24196 * ENXIO 24197 */ 24198 24199 static int 24200 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 24201 { 24202 struct sd_lun *un; 24203 struct mboot *mboot; 24204 int rval; 24205 size_t buffer_size; 24206 24207 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24208 (un->un_state == SD_STATE_OFFLINE)) { 24209 return (ENXIO); 24210 } 24211 24212 if (!un->un_f_mboot_supported || arg == NULL) { 24213 return (EINVAL); 24214 } 24215 24216 /* 24217 * Read the mboot block, located at absolute block 0 on the target. 24218 */ 24219 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 24220 24221 SD_TRACE(SD_LOG_IO_PARTITION, un, 24222 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 24223 24224 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 24225 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 24226 SD_PATH_STANDARD)) == 0) { 24227 if (ddi_copyout(mboot, (void *)arg, 24228 sizeof (struct mboot), flag) != 0) { 24229 rval = EFAULT; 24230 } 24231 } 24232 kmem_free(mboot, buffer_size); 24233 return (rval); 24234 } 24235 24236 24237 /* 24238 * Function: sd_dkio_set_mboot 24239 * 24240 * Description: This routine is the driver entry point for handling user 24241 * requests to validate and set the device master boot 24242 * (DKIOCSMBOOT). 24243 * 24244 * Arguments: dev - the device number 24245 * arg - pointer to user provided mboot structure used to set the 24246 * master boot. 24247 * flag - this argument is a pass through to ddi_copyxxx() 24248 * directly from the mode argument of ioctl(). 24249 * 24250 * Return Code: 0 24251 * EINVAL 24252 * EFAULT 24253 * ENXIO 24254 */ 24255 24256 static int 24257 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 24258 { 24259 struct sd_lun *un = NULL; 24260 struct mboot *mboot = NULL; 24261 int rval; 24262 ushort_t magic; 24263 24264 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24265 return (ENXIO); 24266 } 24267 24268 ASSERT(!mutex_owned(SD_MUTEX(un))); 24269 24270 if (!un->un_f_mboot_supported) { 24271 return (EINVAL); 24272 } 24273 24274 if (arg == NULL) { 24275 return (EINVAL); 24276 } 24277 24278 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 24279 24280 if (ddi_copyin((const void *)arg, mboot, 24281 sizeof (struct mboot), flag) != 0) { 24282 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24283 return (EFAULT); 24284 } 24285 24286 /* Is this really a master boot record? */ 24287 magic = LE_16(mboot->signature); 24288 if (magic != MBB_MAGIC) { 24289 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24290 return (EINVAL); 24291 } 24292 24293 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 24294 SD_PATH_STANDARD); 24295 24296 mutex_enter(SD_MUTEX(un)); 24297 #if defined(__i386) || defined(__amd64) 24298 if (rval == 0) { 24299 /* 24300 * mboot has been written successfully. 24301 * update the fdisk and vtoc tables in memory 24302 */ 24303 rval = sd_update_fdisk_and_vtoc(un); 24304 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 24305 mutex_exit(SD_MUTEX(un)); 24306 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24307 return (rval); 24308 } 24309 } 24310 24311 #ifdef __lock_lint 24312 sd_setup_default_geometry(un); 24313 #endif 24314 24315 #else 24316 if (rval == 0) { 24317 /* 24318 * mboot has been written successfully. 24319 * set up the default geometry and VTOC 24320 */ 24321 if (un->un_blockcount <= DK_MAX_BLOCKS) 24322 sd_setup_default_geometry(un); 24323 } 24324 #endif 24325 mutex_exit(SD_MUTEX(un)); 24326 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24327 return (rval); 24328 } 24329 24330 24331 /* 24332 * Function: sd_setup_default_geometry 24333 * 24334 * Description: This local utility routine sets the default geometry as part of 24335 * setting the device mboot. 24336 * 24337 * Arguments: un - driver soft state (unit) structure 24338 * 24339 * Note: This may be redundant with sd_build_default_label. 24340 */ 24341 24342 static void 24343 sd_setup_default_geometry(struct sd_lun *un) 24344 { 24345 /* zero out the soft state geometry and partition table. */ 24346 bzero(&un->un_g, sizeof (struct dk_geom)); 24347 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24348 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 24349 un->un_asciilabel[0] = '\0'; 24350 24351 /* 24352 * For the rpm, we use the minimum for the disk. 24353 * For the head, cyl and number of sector per track, 24354 * if the capacity <= 1GB, head = 64, sect = 32. 24355 * else head = 255, sect 63 24356 * Note: the capacity should be equal to C*H*S values. 24357 * This will cause some truncation of size due to 24358 * round off errors. For CD-ROMs, this truncation can 24359 * have adverse side effects, so returning ncyl and 24360 * nhead as 1. The nsect will overflow for most of 24361 * CD-ROMs as nsect is of type ushort. 24362 */ 24363 if (ISCD(un)) { 24364 un->un_g.dkg_ncyl = 1; 24365 un->un_g.dkg_nhead = 1; 24366 un->un_g.dkg_nsect = un->un_blockcount; 24367 } else { 24368 if (un->un_blockcount <= 0x1000) { 24369 /* Needed for unlabeled SCSI floppies. */ 24370 un->un_g.dkg_nhead = 2; 24371 un->un_g.dkg_ncyl = 80; 24372 un->un_g.dkg_pcyl = 80; 24373 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 24374 } else if (un->un_blockcount <= 0x200000) { 24375 un->un_g.dkg_nhead = 64; 24376 un->un_g.dkg_nsect = 32; 24377 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 24378 } else { 24379 un->un_g.dkg_nhead = 255; 24380 un->un_g.dkg_nsect = 63; 24381 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 24382 } 24383 un->un_blockcount = un->un_g.dkg_ncyl * 24384 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 24385 } 24386 un->un_g.dkg_acyl = 0; 24387 un->un_g.dkg_bcyl = 0; 24388 un->un_g.dkg_intrlv = 1; 24389 un->un_g.dkg_rpm = 200; 24390 un->un_g.dkg_read_reinstruct = 0; 24391 un->un_g.dkg_write_reinstruct = 0; 24392 if (un->un_g.dkg_pcyl == 0) { 24393 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 24394 } 24395 24396 un->un_map['a'-'a'].dkl_cylno = 0; 24397 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 24398 un->un_map['c'-'a'].dkl_cylno = 0; 24399 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 24400 un->un_f_geometry_is_valid = FALSE; 24401 } 24402 24403 24404 #if defined(__i386) || defined(__amd64) 24405 /* 24406 * Function: sd_update_fdisk_and_vtoc 24407 * 24408 * Description: This local utility routine updates the device fdisk and vtoc 24409 * as part of setting the device mboot. 24410 * 24411 * Arguments: un - driver soft state (unit) structure 24412 * 24413 * Return Code: 0 for success or errno-type return code. 24414 * 24415 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 24416 * these did exist seperately in x86 sd.c!!! 24417 */ 24418 24419 static int 24420 sd_update_fdisk_and_vtoc(struct sd_lun *un) 24421 { 24422 static char labelstring[128]; 24423 static char buf[256]; 24424 char *label = 0; 24425 int count; 24426 int label_rc = 0; 24427 int gvalid = un->un_f_geometry_is_valid; 24428 int fdisk_rval; 24429 int lbasize; 24430 int capacity; 24431 24432 ASSERT(mutex_owned(SD_MUTEX(un))); 24433 24434 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 24435 return (EINVAL); 24436 } 24437 24438 if (un->un_f_blockcount_is_valid == FALSE) { 24439 return (EINVAL); 24440 } 24441 24442 #if defined(_SUNOS_VTOC_16) 24443 /* 24444 * Set up the "whole disk" fdisk partition; this should always 24445 * exist, regardless of whether the disk contains an fdisk table 24446 * or vtoc. 24447 */ 24448 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 24449 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 24450 #endif /* defined(_SUNOS_VTOC_16) */ 24451 24452 /* 24453 * copy the lbasize and capacity so that if they're 24454 * reset while we're not holding the SD_MUTEX(un), we will 24455 * continue to use valid values after the SD_MUTEX(un) is 24456 * reacquired. 24457 */ 24458 lbasize = un->un_tgt_blocksize; 24459 capacity = un->un_blockcount; 24460 24461 /* 24462 * refresh the logical and physical geometry caches. 24463 * (data from mode sense format/rigid disk geometry pages, 24464 * and scsi_ifgetcap("geometry"). 24465 */ 24466 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 24467 24468 /* 24469 * Only DIRECT ACCESS devices will have Sun labels. 24470 * CD's supposedly have a Sun label, too 24471 */ 24472 if (un->un_f_vtoc_label_supported) { 24473 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 24474 SD_PATH_DIRECT); 24475 if (fdisk_rval == SD_CMD_FAILURE) { 24476 ASSERT(mutex_owned(SD_MUTEX(un))); 24477 return (EIO); 24478 } 24479 24480 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 24481 ASSERT(mutex_owned(SD_MUTEX(un))); 24482 return (EACCES); 24483 } 24484 24485 if (un->un_solaris_size <= DK_LABEL_LOC) { 24486 /* 24487 * Found fdisk table but no Solaris partition entry, 24488 * so don't call sd_uselabel() and don't create 24489 * a default label. 24490 */ 24491 label_rc = 0; 24492 un->un_f_geometry_is_valid = TRUE; 24493 goto no_solaris_partition; 24494 } 24495 24496 #if defined(_SUNOS_VTOC_8) 24497 label = (char *)un->un_asciilabel; 24498 #elif defined(_SUNOS_VTOC_16) 24499 label = (char *)un->un_vtoc.v_asciilabel; 24500 #else 24501 #error "No VTOC format defined." 24502 #endif 24503 } else if (capacity < 0) { 24504 ASSERT(mutex_owned(SD_MUTEX(un))); 24505 return (EINVAL); 24506 } 24507 24508 /* 24509 * For Removable media We reach here if we have found a 24510 * SOLARIS PARTITION. 24511 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24512 * PARTITION has changed from the previous one, hence we will setup a 24513 * default VTOC in this case. 24514 */ 24515 if (un->un_f_geometry_is_valid == FALSE) { 24516 sd_build_default_label(un); 24517 label_rc = 0; 24518 } 24519 24520 no_solaris_partition: 24521 if ((!un->un_f_has_removable_media || 24522 (un->un_f_has_removable_media && 24523 un->un_mediastate == DKIO_EJECTED)) && 24524 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24525 /* 24526 * Print out a message indicating who and what we are. 24527 * We do this only when we happen to really validate the 24528 * geometry. We may call sd_validate_geometry() at other 24529 * times, ioctl()'s like Get VTOC in which case we 24530 * don't want to print the label. 24531 * If the geometry is valid, print the label string, 24532 * else print vendor and product info, if available 24533 */ 24534 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24535 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24536 } else { 24537 mutex_enter(&sd_label_mutex); 24538 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24539 labelstring); 24540 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24541 &labelstring[64]); 24542 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24543 labelstring, &labelstring[64]); 24544 if (un->un_f_blockcount_is_valid == TRUE) { 24545 (void) sprintf(&buf[strlen(buf)], 24546 ", %" PRIu64 " %u byte blocks\n", 24547 un->un_blockcount, 24548 un->un_tgt_blocksize); 24549 } else { 24550 (void) sprintf(&buf[strlen(buf)], 24551 ", (unknown capacity)\n"); 24552 } 24553 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24554 mutex_exit(&sd_label_mutex); 24555 } 24556 } 24557 24558 #if defined(_SUNOS_VTOC_16) 24559 /* 24560 * If we have valid geometry, set up the remaining fdisk partitions. 24561 * Note that dkl_cylno is not used for the fdisk map entries, so 24562 * we set it to an entirely bogus value. 24563 */ 24564 for (count = 0; count < FD_NUMPART; count++) { 24565 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24566 un->un_map[FDISK_P1 + count].dkl_nblk = 24567 un->un_fmap[count].fmap_nblk; 24568 un->un_offset[FDISK_P1 + count] = 24569 un->un_fmap[count].fmap_start; 24570 } 24571 #endif 24572 24573 for (count = 0; count < NDKMAP; count++) { 24574 #if defined(_SUNOS_VTOC_8) 24575 struct dk_map *lp = &un->un_map[count]; 24576 un->un_offset[count] = 24577 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24578 #elif defined(_SUNOS_VTOC_16) 24579 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24580 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24581 #else 24582 #error "No VTOC format defined." 24583 #endif 24584 } 24585 24586 ASSERT(mutex_owned(SD_MUTEX(un))); 24587 return (label_rc); 24588 } 24589 #endif 24590 24591 24592 /* 24593 * Function: sd_check_media 24594 * 24595 * Description: This utility routine implements the functionality for the 24596 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24597 * driver state changes from that specified by the user 24598 * (inserted or ejected). For example, if the user specifies 24599 * DKIO_EJECTED and the current media state is inserted this 24600 * routine will immediately return DKIO_INSERTED. However, if the 24601 * current media state is not inserted the user thread will be 24602 * blocked until the drive state changes. If DKIO_NONE is specified 24603 * the user thread will block until a drive state change occurs. 24604 * 24605 * Arguments: dev - the device number 24606 * state - user pointer to a dkio_state, updated with the current 24607 * drive state at return. 24608 * 24609 * Return Code: ENXIO 24610 * EIO 24611 * EAGAIN 24612 * EINTR 24613 */ 24614 24615 static int 24616 sd_check_media(dev_t dev, enum dkio_state state) 24617 { 24618 struct sd_lun *un = NULL; 24619 enum dkio_state prev_state; 24620 opaque_t token = NULL; 24621 int rval = 0; 24622 24623 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24624 return (ENXIO); 24625 } 24626 24627 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24628 24629 mutex_enter(SD_MUTEX(un)); 24630 24631 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24632 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24633 24634 prev_state = un->un_mediastate; 24635 24636 /* is there anything to do? */ 24637 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24638 /* 24639 * submit the request to the scsi_watch service; 24640 * scsi_media_watch_cb() does the real work 24641 */ 24642 mutex_exit(SD_MUTEX(un)); 24643 24644 /* 24645 * This change handles the case where a scsi watch request is 24646 * added to a device that is powered down. To accomplish this 24647 * we power up the device before adding the scsi watch request, 24648 * since the scsi watch sends a TUR directly to the device 24649 * which the device cannot handle if it is powered down. 24650 */ 24651 if (sd_pm_entry(un) != DDI_SUCCESS) { 24652 mutex_enter(SD_MUTEX(un)); 24653 goto done; 24654 } 24655 24656 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24657 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24658 (caddr_t)dev); 24659 24660 sd_pm_exit(un); 24661 24662 mutex_enter(SD_MUTEX(un)); 24663 if (token == NULL) { 24664 rval = EAGAIN; 24665 goto done; 24666 } 24667 24668 /* 24669 * This is a special case IOCTL that doesn't return 24670 * until the media state changes. Routine sdpower 24671 * knows about and handles this so don't count it 24672 * as an active cmd in the driver, which would 24673 * keep the device busy to the pm framework. 24674 * If the count isn't decremented the device can't 24675 * be powered down. 24676 */ 24677 un->un_ncmds_in_driver--; 24678 ASSERT(un->un_ncmds_in_driver >= 0); 24679 24680 /* 24681 * if a prior request had been made, this will be the same 24682 * token, as scsi_watch was designed that way. 24683 */ 24684 un->un_swr_token = token; 24685 un->un_specified_mediastate = state; 24686 24687 /* 24688 * now wait for media change 24689 * we will not be signalled unless mediastate == state but it is 24690 * still better to test for this condition, since there is a 24691 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24692 */ 24693 SD_TRACE(SD_LOG_COMMON, un, 24694 "sd_check_media: waiting for media state change\n"); 24695 while (un->un_mediastate == state) { 24696 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24697 SD_TRACE(SD_LOG_COMMON, un, 24698 "sd_check_media: waiting for media state " 24699 "was interrupted\n"); 24700 un->un_ncmds_in_driver++; 24701 rval = EINTR; 24702 goto done; 24703 } 24704 SD_TRACE(SD_LOG_COMMON, un, 24705 "sd_check_media: received signal, state=%x\n", 24706 un->un_mediastate); 24707 } 24708 /* 24709 * Inc the counter to indicate the device once again 24710 * has an active outstanding cmd. 24711 */ 24712 un->un_ncmds_in_driver++; 24713 } 24714 24715 /* invalidate geometry */ 24716 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24717 sr_ejected(un); 24718 } 24719 24720 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24721 uint64_t capacity; 24722 uint_t lbasize; 24723 24724 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24725 mutex_exit(SD_MUTEX(un)); 24726 /* 24727 * Since the following routines use SD_PATH_DIRECT, we must 24728 * call PM directly before the upcoming disk accesses. This 24729 * may cause the disk to be power/spin up. 24730 */ 24731 24732 if (sd_pm_entry(un) == DDI_SUCCESS) { 24733 rval = sd_send_scsi_READ_CAPACITY(un, 24734 &capacity, 24735 &lbasize, SD_PATH_DIRECT); 24736 if (rval != 0) { 24737 sd_pm_exit(un); 24738 mutex_enter(SD_MUTEX(un)); 24739 goto done; 24740 } 24741 } else { 24742 rval = EIO; 24743 mutex_enter(SD_MUTEX(un)); 24744 goto done; 24745 } 24746 mutex_enter(SD_MUTEX(un)); 24747 24748 sd_update_block_info(un, lbasize, capacity); 24749 24750 un->un_f_geometry_is_valid = FALSE; 24751 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24752 24753 mutex_exit(SD_MUTEX(un)); 24754 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24755 SD_PATH_DIRECT); 24756 sd_pm_exit(un); 24757 24758 mutex_enter(SD_MUTEX(un)); 24759 } 24760 done: 24761 un->un_f_watcht_stopped = FALSE; 24762 if (un->un_swr_token) { 24763 /* 24764 * Use of this local token and the mutex ensures that we avoid 24765 * some race conditions associated with terminating the 24766 * scsi watch. 24767 */ 24768 token = un->un_swr_token; 24769 un->un_swr_token = (opaque_t)NULL; 24770 mutex_exit(SD_MUTEX(un)); 24771 (void) scsi_watch_request_terminate(token, 24772 SCSI_WATCH_TERMINATE_WAIT); 24773 mutex_enter(SD_MUTEX(un)); 24774 } 24775 24776 /* 24777 * Update the capacity kstat value, if no media previously 24778 * (capacity kstat is 0) and a media has been inserted 24779 * (un_f_blockcount_is_valid == TRUE) 24780 */ 24781 if (un->un_errstats) { 24782 struct sd_errstats *stp = NULL; 24783 24784 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24785 if ((stp->sd_capacity.value.ui64 == 0) && 24786 (un->un_f_blockcount_is_valid == TRUE)) { 24787 stp->sd_capacity.value.ui64 = 24788 (uint64_t)((uint64_t)un->un_blockcount * 24789 un->un_sys_blocksize); 24790 } 24791 } 24792 mutex_exit(SD_MUTEX(un)); 24793 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24794 return (rval); 24795 } 24796 24797 24798 /* 24799 * Function: sd_delayed_cv_broadcast 24800 * 24801 * Description: Delayed cv_broadcast to allow for target to recover from media 24802 * insertion. 24803 * 24804 * Arguments: arg - driver soft state (unit) structure 24805 */ 24806 24807 static void 24808 sd_delayed_cv_broadcast(void *arg) 24809 { 24810 struct sd_lun *un = arg; 24811 24812 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24813 24814 mutex_enter(SD_MUTEX(un)); 24815 un->un_dcvb_timeid = NULL; 24816 cv_broadcast(&un->un_state_cv); 24817 mutex_exit(SD_MUTEX(un)); 24818 } 24819 24820 24821 /* 24822 * Function: sd_media_watch_cb 24823 * 24824 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24825 * routine processes the TUR sense data and updates the driver 24826 * state if a transition has occurred. The user thread 24827 * (sd_check_media) is then signalled. 24828 * 24829 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24830 * among multiple watches that share this callback function 24831 * resultp - scsi watch facility result packet containing scsi 24832 * packet, status byte and sense data 24833 * 24834 * Return Code: 0 for success, -1 for failure 24835 */ 24836 24837 static int 24838 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24839 { 24840 struct sd_lun *un; 24841 struct scsi_status *statusp = resultp->statusp; 24842 uint8_t *sensep = (uint8_t *)resultp->sensep; 24843 enum dkio_state state = DKIO_NONE; 24844 dev_t dev = (dev_t)arg; 24845 uchar_t actual_sense_length; 24846 uint8_t skey, asc, ascq; 24847 24848 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24849 return (-1); 24850 } 24851 actual_sense_length = resultp->actual_sense_length; 24852 24853 mutex_enter(SD_MUTEX(un)); 24854 SD_TRACE(SD_LOG_COMMON, un, 24855 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24856 *((char *)statusp), (void *)sensep, actual_sense_length); 24857 24858 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24859 un->un_mediastate = DKIO_DEV_GONE; 24860 cv_broadcast(&un->un_state_cv); 24861 mutex_exit(SD_MUTEX(un)); 24862 24863 return (0); 24864 } 24865 24866 /* 24867 * If there was a check condition then sensep points to valid sense data 24868 * If status was not a check condition but a reservation or busy status 24869 * then the new state is DKIO_NONE 24870 */ 24871 if (sensep != NULL) { 24872 skey = scsi_sense_key(sensep); 24873 asc = scsi_sense_asc(sensep); 24874 ascq = scsi_sense_ascq(sensep); 24875 24876 SD_INFO(SD_LOG_COMMON, un, 24877 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24878 skey, asc, ascq); 24879 /* This routine only uses up to 13 bytes of sense data. */ 24880 if (actual_sense_length >= 13) { 24881 if (skey == KEY_UNIT_ATTENTION) { 24882 if (asc == 0x28) { 24883 state = DKIO_INSERTED; 24884 } 24885 } else { 24886 /* 24887 * if 02/04/02 means that the host 24888 * should send start command. Explicitly 24889 * leave the media state as is 24890 * (inserted) as the media is inserted 24891 * and host has stopped device for PM 24892 * reasons. Upon next true read/write 24893 * to this media will bring the 24894 * device to the right state good for 24895 * media access. 24896 */ 24897 if ((skey == KEY_NOT_READY) && 24898 (asc == 0x3a)) { 24899 state = DKIO_EJECTED; 24900 } 24901 24902 /* 24903 * If the drivge is busy with an operation 24904 * or long write, keep the media in an 24905 * inserted state. 24906 */ 24907 24908 if ((skey == KEY_NOT_READY) && 24909 (asc == 0x04) && 24910 ((ascq == 0x02) || 24911 (ascq == 0x07) || 24912 (ascq == 0x08))) { 24913 state = DKIO_INSERTED; 24914 } 24915 } 24916 } 24917 } else if ((*((char *)statusp) == STATUS_GOOD) && 24918 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24919 state = DKIO_INSERTED; 24920 } 24921 24922 SD_TRACE(SD_LOG_COMMON, un, 24923 "sd_media_watch_cb: state=%x, specified=%x\n", 24924 state, un->un_specified_mediastate); 24925 24926 /* 24927 * now signal the waiting thread if this is *not* the specified state; 24928 * delay the signal if the state is DKIO_INSERTED to allow the target 24929 * to recover 24930 */ 24931 if (state != un->un_specified_mediastate) { 24932 un->un_mediastate = state; 24933 if (state == DKIO_INSERTED) { 24934 /* 24935 * delay the signal to give the drive a chance 24936 * to do what it apparently needs to do 24937 */ 24938 SD_TRACE(SD_LOG_COMMON, un, 24939 "sd_media_watch_cb: delayed cv_broadcast\n"); 24940 if (un->un_dcvb_timeid == NULL) { 24941 un->un_dcvb_timeid = 24942 timeout(sd_delayed_cv_broadcast, un, 24943 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24944 } 24945 } else { 24946 SD_TRACE(SD_LOG_COMMON, un, 24947 "sd_media_watch_cb: immediate cv_broadcast\n"); 24948 cv_broadcast(&un->un_state_cv); 24949 } 24950 } 24951 mutex_exit(SD_MUTEX(un)); 24952 return (0); 24953 } 24954 24955 24956 /* 24957 * Function: sd_dkio_get_temp 24958 * 24959 * Description: This routine is the driver entry point for handling ioctl 24960 * requests to get the disk temperature. 24961 * 24962 * Arguments: dev - the device number 24963 * arg - pointer to user provided dk_temperature structure. 24964 * flag - this argument is a pass through to ddi_copyxxx() 24965 * directly from the mode argument of ioctl(). 24966 * 24967 * Return Code: 0 24968 * EFAULT 24969 * ENXIO 24970 * EAGAIN 24971 */ 24972 24973 static int 24974 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24975 { 24976 struct sd_lun *un = NULL; 24977 struct dk_temperature *dktemp = NULL; 24978 uchar_t *temperature_page; 24979 int rval = 0; 24980 int path_flag = SD_PATH_STANDARD; 24981 24982 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24983 return (ENXIO); 24984 } 24985 24986 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24987 24988 /* copyin the disk temp argument to get the user flags */ 24989 if (ddi_copyin((void *)arg, dktemp, 24990 sizeof (struct dk_temperature), flag) != 0) { 24991 rval = EFAULT; 24992 goto done; 24993 } 24994 24995 /* Initialize the temperature to invalid. */ 24996 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 24997 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 24998 24999 /* 25000 * Note: Investigate removing the "bypass pm" semantic. 25001 * Can we just bypass PM always? 25002 */ 25003 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 25004 path_flag = SD_PATH_DIRECT; 25005 ASSERT(!mutex_owned(&un->un_pm_mutex)); 25006 mutex_enter(&un->un_pm_mutex); 25007 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25008 /* 25009 * If DKT_BYPASS_PM is set, and the drive happens to be 25010 * in low power mode, we can not wake it up, Need to 25011 * return EAGAIN. 25012 */ 25013 mutex_exit(&un->un_pm_mutex); 25014 rval = EAGAIN; 25015 goto done; 25016 } else { 25017 /* 25018 * Indicate to PM the device is busy. This is required 25019 * to avoid a race - i.e. the ioctl is issuing a 25020 * command and the pm framework brings down the device 25021 * to low power mode (possible power cut-off on some 25022 * platforms). 25023 */ 25024 mutex_exit(&un->un_pm_mutex); 25025 if (sd_pm_entry(un) != DDI_SUCCESS) { 25026 rval = EAGAIN; 25027 goto done; 25028 } 25029 } 25030 } 25031 25032 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 25033 25034 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 25035 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 25036 goto done2; 25037 } 25038 25039 /* 25040 * For the current temperature verify that the parameter length is 0x02 25041 * and the parameter code is 0x00 25042 */ 25043 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 25044 (temperature_page[5] == 0x00)) { 25045 if (temperature_page[9] == 0xFF) { 25046 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 25047 } else { 25048 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 25049 } 25050 } 25051 25052 /* 25053 * For the reference temperature verify that the parameter 25054 * length is 0x02 and the parameter code is 0x01 25055 */ 25056 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 25057 (temperature_page[11] == 0x01)) { 25058 if (temperature_page[15] == 0xFF) { 25059 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 25060 } else { 25061 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 25062 } 25063 } 25064 25065 /* Do the copyout regardless of the temperature commands status. */ 25066 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 25067 flag) != 0) { 25068 rval = EFAULT; 25069 } 25070 25071 done2: 25072 if (path_flag == SD_PATH_DIRECT) { 25073 sd_pm_exit(un); 25074 } 25075 25076 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 25077 done: 25078 if (dktemp != NULL) { 25079 kmem_free(dktemp, sizeof (struct dk_temperature)); 25080 } 25081 25082 return (rval); 25083 } 25084 25085 25086 /* 25087 * Function: sd_log_page_supported 25088 * 25089 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 25090 * supported log pages. 25091 * 25092 * Arguments: un - 25093 * log_page - 25094 * 25095 * Return Code: -1 - on error (log sense is optional and may not be supported). 25096 * 0 - log page not found. 25097 * 1 - log page found. 25098 */ 25099 25100 static int 25101 sd_log_page_supported(struct sd_lun *un, int log_page) 25102 { 25103 uchar_t *log_page_data; 25104 int i; 25105 int match = 0; 25106 int log_size; 25107 25108 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 25109 25110 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 25111 SD_PATH_DIRECT) != 0) { 25112 SD_ERROR(SD_LOG_COMMON, un, 25113 "sd_log_page_supported: failed log page retrieval\n"); 25114 kmem_free(log_page_data, 0xFF); 25115 return (-1); 25116 } 25117 log_size = log_page_data[3]; 25118 25119 /* 25120 * The list of supported log pages start from the fourth byte. Check 25121 * until we run out of log pages or a match is found. 25122 */ 25123 for (i = 4; (i < (log_size + 4)) && !match; i++) { 25124 if (log_page_data[i] == log_page) { 25125 match++; 25126 } 25127 } 25128 kmem_free(log_page_data, 0xFF); 25129 return (match); 25130 } 25131 25132 25133 /* 25134 * Function: sd_mhdioc_failfast 25135 * 25136 * Description: This routine is the driver entry point for handling ioctl 25137 * requests to enable/disable the multihost failfast option. 25138 * (MHIOCENFAILFAST) 25139 * 25140 * Arguments: dev - the device number 25141 * arg - user specified probing interval. 25142 * flag - this argument is a pass through to ddi_copyxxx() 25143 * directly from the mode argument of ioctl(). 25144 * 25145 * Return Code: 0 25146 * EFAULT 25147 * ENXIO 25148 */ 25149 25150 static int 25151 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 25152 { 25153 struct sd_lun *un = NULL; 25154 int mh_time; 25155 int rval = 0; 25156 25157 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25158 return (ENXIO); 25159 } 25160 25161 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 25162 return (EFAULT); 25163 25164 if (mh_time) { 25165 mutex_enter(SD_MUTEX(un)); 25166 un->un_resvd_status |= SD_FAILFAST; 25167 mutex_exit(SD_MUTEX(un)); 25168 /* 25169 * If mh_time is INT_MAX, then this ioctl is being used for 25170 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 25171 */ 25172 if (mh_time != INT_MAX) { 25173 rval = sd_check_mhd(dev, mh_time); 25174 } 25175 } else { 25176 (void) sd_check_mhd(dev, 0); 25177 mutex_enter(SD_MUTEX(un)); 25178 un->un_resvd_status &= ~SD_FAILFAST; 25179 mutex_exit(SD_MUTEX(un)); 25180 } 25181 return (rval); 25182 } 25183 25184 25185 /* 25186 * Function: sd_mhdioc_takeown 25187 * 25188 * Description: This routine is the driver entry point for handling ioctl 25189 * requests to forcefully acquire exclusive access rights to the 25190 * multihost disk (MHIOCTKOWN). 25191 * 25192 * Arguments: dev - the device number 25193 * arg - user provided structure specifying the delay 25194 * parameters in milliseconds 25195 * flag - this argument is a pass through to ddi_copyxxx() 25196 * directly from the mode argument of ioctl(). 25197 * 25198 * Return Code: 0 25199 * EFAULT 25200 * ENXIO 25201 */ 25202 25203 static int 25204 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 25205 { 25206 struct sd_lun *un = NULL; 25207 struct mhioctkown *tkown = NULL; 25208 int rval = 0; 25209 25210 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25211 return (ENXIO); 25212 } 25213 25214 if (arg != NULL) { 25215 tkown = (struct mhioctkown *) 25216 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 25217 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 25218 if (rval != 0) { 25219 rval = EFAULT; 25220 goto error; 25221 } 25222 } 25223 25224 rval = sd_take_ownership(dev, tkown); 25225 mutex_enter(SD_MUTEX(un)); 25226 if (rval == 0) { 25227 un->un_resvd_status |= SD_RESERVE; 25228 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 25229 sd_reinstate_resv_delay = 25230 tkown->reinstate_resv_delay * 1000; 25231 } else { 25232 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 25233 } 25234 /* 25235 * Give the scsi_watch routine interval set by 25236 * the MHIOCENFAILFAST ioctl precedence here. 25237 */ 25238 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 25239 mutex_exit(SD_MUTEX(un)); 25240 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 25241 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25242 "sd_mhdioc_takeown : %d\n", 25243 sd_reinstate_resv_delay); 25244 } else { 25245 mutex_exit(SD_MUTEX(un)); 25246 } 25247 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 25248 sd_mhd_reset_notify_cb, (caddr_t)un); 25249 } else { 25250 un->un_resvd_status &= ~SD_RESERVE; 25251 mutex_exit(SD_MUTEX(un)); 25252 } 25253 25254 error: 25255 if (tkown != NULL) { 25256 kmem_free(tkown, sizeof (struct mhioctkown)); 25257 } 25258 return (rval); 25259 } 25260 25261 25262 /* 25263 * Function: sd_mhdioc_release 25264 * 25265 * Description: This routine is the driver entry point for handling ioctl 25266 * requests to release exclusive access rights to the multihost 25267 * disk (MHIOCRELEASE). 25268 * 25269 * Arguments: dev - the device number 25270 * 25271 * Return Code: 0 25272 * ENXIO 25273 */ 25274 25275 static int 25276 sd_mhdioc_release(dev_t dev) 25277 { 25278 struct sd_lun *un = NULL; 25279 timeout_id_t resvd_timeid_save; 25280 int resvd_status_save; 25281 int rval = 0; 25282 25283 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25284 return (ENXIO); 25285 } 25286 25287 mutex_enter(SD_MUTEX(un)); 25288 resvd_status_save = un->un_resvd_status; 25289 un->un_resvd_status &= 25290 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 25291 if (un->un_resvd_timeid) { 25292 resvd_timeid_save = un->un_resvd_timeid; 25293 un->un_resvd_timeid = NULL; 25294 mutex_exit(SD_MUTEX(un)); 25295 (void) untimeout(resvd_timeid_save); 25296 } else { 25297 mutex_exit(SD_MUTEX(un)); 25298 } 25299 25300 /* 25301 * destroy any pending timeout thread that may be attempting to 25302 * reinstate reservation on this device. 25303 */ 25304 sd_rmv_resv_reclaim_req(dev); 25305 25306 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 25307 mutex_enter(SD_MUTEX(un)); 25308 if ((un->un_mhd_token) && 25309 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 25310 mutex_exit(SD_MUTEX(un)); 25311 (void) sd_check_mhd(dev, 0); 25312 } else { 25313 mutex_exit(SD_MUTEX(un)); 25314 } 25315 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 25316 sd_mhd_reset_notify_cb, (caddr_t)un); 25317 } else { 25318 /* 25319 * sd_mhd_watch_cb will restart the resvd recover timeout thread 25320 */ 25321 mutex_enter(SD_MUTEX(un)); 25322 un->un_resvd_status = resvd_status_save; 25323 mutex_exit(SD_MUTEX(un)); 25324 } 25325 return (rval); 25326 } 25327 25328 25329 /* 25330 * Function: sd_mhdioc_register_devid 25331 * 25332 * Description: This routine is the driver entry point for handling ioctl 25333 * requests to register the device id (MHIOCREREGISTERDEVID). 25334 * 25335 * Note: The implementation for this ioctl has been updated to 25336 * be consistent with the original PSARC case (1999/357) 25337 * (4375899, 4241671, 4220005) 25338 * 25339 * Arguments: dev - the device number 25340 * 25341 * Return Code: 0 25342 * ENXIO 25343 */ 25344 25345 static int 25346 sd_mhdioc_register_devid(dev_t dev) 25347 { 25348 struct sd_lun *un = NULL; 25349 int rval = 0; 25350 25351 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25352 return (ENXIO); 25353 } 25354 25355 ASSERT(!mutex_owned(SD_MUTEX(un))); 25356 25357 mutex_enter(SD_MUTEX(un)); 25358 25359 /* If a devid already exists, de-register it */ 25360 if (un->un_devid != NULL) { 25361 ddi_devid_unregister(SD_DEVINFO(un)); 25362 /* 25363 * After unregister devid, needs to free devid memory 25364 */ 25365 ddi_devid_free(un->un_devid); 25366 un->un_devid = NULL; 25367 } 25368 25369 /* Check for reservation conflict */ 25370 mutex_exit(SD_MUTEX(un)); 25371 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 25372 mutex_enter(SD_MUTEX(un)); 25373 25374 switch (rval) { 25375 case 0: 25376 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 25377 break; 25378 case EACCES: 25379 break; 25380 default: 25381 rval = EIO; 25382 } 25383 25384 mutex_exit(SD_MUTEX(un)); 25385 return (rval); 25386 } 25387 25388 25389 /* 25390 * Function: sd_mhdioc_inkeys 25391 * 25392 * Description: This routine is the driver entry point for handling ioctl 25393 * requests to issue the SCSI-3 Persistent In Read Keys command 25394 * to the device (MHIOCGRP_INKEYS). 25395 * 25396 * Arguments: dev - the device number 25397 * arg - user provided in_keys structure 25398 * flag - this argument is a pass through to ddi_copyxxx() 25399 * directly from the mode argument of ioctl(). 25400 * 25401 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 25402 * ENXIO 25403 * EFAULT 25404 */ 25405 25406 static int 25407 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 25408 { 25409 struct sd_lun *un; 25410 mhioc_inkeys_t inkeys; 25411 int rval = 0; 25412 25413 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25414 return (ENXIO); 25415 } 25416 25417 #ifdef _MULTI_DATAMODEL 25418 switch (ddi_model_convert_from(flag & FMODELS)) { 25419 case DDI_MODEL_ILP32: { 25420 struct mhioc_inkeys32 inkeys32; 25421 25422 if (ddi_copyin(arg, &inkeys32, 25423 sizeof (struct mhioc_inkeys32), flag) != 0) { 25424 return (EFAULT); 25425 } 25426 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 25427 if ((rval = sd_persistent_reservation_in_read_keys(un, 25428 &inkeys, flag)) != 0) { 25429 return (rval); 25430 } 25431 inkeys32.generation = inkeys.generation; 25432 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 25433 flag) != 0) { 25434 return (EFAULT); 25435 } 25436 break; 25437 } 25438 case DDI_MODEL_NONE: 25439 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 25440 flag) != 0) { 25441 return (EFAULT); 25442 } 25443 if ((rval = sd_persistent_reservation_in_read_keys(un, 25444 &inkeys, flag)) != 0) { 25445 return (rval); 25446 } 25447 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 25448 flag) != 0) { 25449 return (EFAULT); 25450 } 25451 break; 25452 } 25453 25454 #else /* ! _MULTI_DATAMODEL */ 25455 25456 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 25457 return (EFAULT); 25458 } 25459 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 25460 if (rval != 0) { 25461 return (rval); 25462 } 25463 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 25464 return (EFAULT); 25465 } 25466 25467 #endif /* _MULTI_DATAMODEL */ 25468 25469 return (rval); 25470 } 25471 25472 25473 /* 25474 * Function: sd_mhdioc_inresv 25475 * 25476 * Description: This routine is the driver entry point for handling ioctl 25477 * requests to issue the SCSI-3 Persistent In Read Reservations 25478 * command to the device (MHIOCGRP_INKEYS). 25479 * 25480 * Arguments: dev - the device number 25481 * arg - user provided in_resv structure 25482 * flag - this argument is a pass through to ddi_copyxxx() 25483 * directly from the mode argument of ioctl(). 25484 * 25485 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 25486 * ENXIO 25487 * EFAULT 25488 */ 25489 25490 static int 25491 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25492 { 25493 struct sd_lun *un; 25494 mhioc_inresvs_t inresvs; 25495 int rval = 0; 25496 25497 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25498 return (ENXIO); 25499 } 25500 25501 #ifdef _MULTI_DATAMODEL 25502 25503 switch (ddi_model_convert_from(flag & FMODELS)) { 25504 case DDI_MODEL_ILP32: { 25505 struct mhioc_inresvs32 inresvs32; 25506 25507 if (ddi_copyin(arg, &inresvs32, 25508 sizeof (struct mhioc_inresvs32), flag) != 0) { 25509 return (EFAULT); 25510 } 25511 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25512 if ((rval = sd_persistent_reservation_in_read_resv(un, 25513 &inresvs, flag)) != 0) { 25514 return (rval); 25515 } 25516 inresvs32.generation = inresvs.generation; 25517 if (ddi_copyout(&inresvs32, arg, 25518 sizeof (struct mhioc_inresvs32), flag) != 0) { 25519 return (EFAULT); 25520 } 25521 break; 25522 } 25523 case DDI_MODEL_NONE: 25524 if (ddi_copyin(arg, &inresvs, 25525 sizeof (mhioc_inresvs_t), flag) != 0) { 25526 return (EFAULT); 25527 } 25528 if ((rval = sd_persistent_reservation_in_read_resv(un, 25529 &inresvs, flag)) != 0) { 25530 return (rval); 25531 } 25532 if (ddi_copyout(&inresvs, arg, 25533 sizeof (mhioc_inresvs_t), flag) != 0) { 25534 return (EFAULT); 25535 } 25536 break; 25537 } 25538 25539 #else /* ! _MULTI_DATAMODEL */ 25540 25541 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25542 return (EFAULT); 25543 } 25544 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25545 if (rval != 0) { 25546 return (rval); 25547 } 25548 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25549 return (EFAULT); 25550 } 25551 25552 #endif /* ! _MULTI_DATAMODEL */ 25553 25554 return (rval); 25555 } 25556 25557 25558 /* 25559 * The following routines support the clustering functionality described below 25560 * and implement lost reservation reclaim functionality. 25561 * 25562 * Clustering 25563 * ---------- 25564 * The clustering code uses two different, independent forms of SCSI 25565 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25566 * Persistent Group Reservations. For any particular disk, it will use either 25567 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25568 * 25569 * SCSI-2 25570 * The cluster software takes ownership of a multi-hosted disk by issuing the 25571 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25572 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25573 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25574 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25575 * meaning of failfast is that if the driver (on this host) ever encounters the 25576 * scsi error return code RESERVATION_CONFLICT from the device, it should 25577 * immediately panic the host. The motivation for this ioctl is that if this 25578 * host does encounter reservation conflict, the underlying cause is that some 25579 * other host of the cluster has decided that this host is no longer in the 25580 * cluster and has seized control of the disks for itself. Since this host is no 25581 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25582 * does two things: 25583 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25584 * error to panic the host 25585 * (b) it sets up a periodic timer to test whether this host still has 25586 * "access" (in that no other host has reserved the device): if the 25587 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25588 * purpose of that periodic timer is to handle scenarios where the host is 25589 * otherwise temporarily quiescent, temporarily doing no real i/o. 25590 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25591 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25592 * the device itself. 25593 * 25594 * SCSI-3 PGR 25595 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25596 * facility is supported through the shared multihost disk ioctls 25597 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25598 * MHIOCGRP_PREEMPTANDABORT) 25599 * 25600 * Reservation Reclaim: 25601 * -------------------- 25602 * To support the lost reservation reclaim operations this driver creates a 25603 * single thread to handle reinstating reservations on all devices that have 25604 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25605 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25606 * and the reservation reclaim thread loops through the requests to regain the 25607 * lost reservations. 25608 */ 25609 25610 /* 25611 * Function: sd_check_mhd() 25612 * 25613 * Description: This function sets up and submits a scsi watch request or 25614 * terminates an existing watch request. This routine is used in 25615 * support of reservation reclaim. 25616 * 25617 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25618 * among multiple watches that share the callback function 25619 * interval - the number of microseconds specifying the watch 25620 * interval for issuing TEST UNIT READY commands. If 25621 * set to 0 the watch should be terminated. If the 25622 * interval is set to 0 and if the device is required 25623 * to hold reservation while disabling failfast, the 25624 * watch is restarted with an interval of 25625 * reinstate_resv_delay. 25626 * 25627 * Return Code: 0 - Successful submit/terminate of scsi watch request 25628 * ENXIO - Indicates an invalid device was specified 25629 * EAGAIN - Unable to submit the scsi watch request 25630 */ 25631 25632 static int 25633 sd_check_mhd(dev_t dev, int interval) 25634 { 25635 struct sd_lun *un; 25636 opaque_t token; 25637 25638 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25639 return (ENXIO); 25640 } 25641 25642 /* is this a watch termination request? */ 25643 if (interval == 0) { 25644 mutex_enter(SD_MUTEX(un)); 25645 /* if there is an existing watch task then terminate it */ 25646 if (un->un_mhd_token) { 25647 token = un->un_mhd_token; 25648 un->un_mhd_token = NULL; 25649 mutex_exit(SD_MUTEX(un)); 25650 (void) scsi_watch_request_terminate(token, 25651 SCSI_WATCH_TERMINATE_WAIT); 25652 mutex_enter(SD_MUTEX(un)); 25653 } else { 25654 mutex_exit(SD_MUTEX(un)); 25655 /* 25656 * Note: If we return here we don't check for the 25657 * failfast case. This is the original legacy 25658 * implementation but perhaps we should be checking 25659 * the failfast case. 25660 */ 25661 return (0); 25662 } 25663 /* 25664 * If the device is required to hold reservation while 25665 * disabling failfast, we need to restart the scsi_watch 25666 * routine with an interval of reinstate_resv_delay. 25667 */ 25668 if (un->un_resvd_status & SD_RESERVE) { 25669 interval = sd_reinstate_resv_delay/1000; 25670 } else { 25671 /* no failfast so bail */ 25672 mutex_exit(SD_MUTEX(un)); 25673 return (0); 25674 } 25675 mutex_exit(SD_MUTEX(un)); 25676 } 25677 25678 /* 25679 * adjust minimum time interval to 1 second, 25680 * and convert from msecs to usecs 25681 */ 25682 if (interval > 0 && interval < 1000) { 25683 interval = 1000; 25684 } 25685 interval *= 1000; 25686 25687 /* 25688 * submit the request to the scsi_watch service 25689 */ 25690 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25691 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25692 if (token == NULL) { 25693 return (EAGAIN); 25694 } 25695 25696 /* 25697 * save token for termination later on 25698 */ 25699 mutex_enter(SD_MUTEX(un)); 25700 un->un_mhd_token = token; 25701 mutex_exit(SD_MUTEX(un)); 25702 return (0); 25703 } 25704 25705 25706 /* 25707 * Function: sd_mhd_watch_cb() 25708 * 25709 * Description: This function is the call back function used by the scsi watch 25710 * facility. The scsi watch facility sends the "Test Unit Ready" 25711 * and processes the status. If applicable (i.e. a "Unit Attention" 25712 * status and automatic "Request Sense" not used) the scsi watch 25713 * facility will send a "Request Sense" and retrieve the sense data 25714 * to be passed to this callback function. In either case the 25715 * automatic "Request Sense" or the facility submitting one, this 25716 * callback is passed the status and sense data. 25717 * 25718 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25719 * among multiple watches that share this callback function 25720 * resultp - scsi watch facility result packet containing scsi 25721 * packet, status byte and sense data 25722 * 25723 * Return Code: 0 - continue the watch task 25724 * non-zero - terminate the watch task 25725 */ 25726 25727 static int 25728 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25729 { 25730 struct sd_lun *un; 25731 struct scsi_status *statusp; 25732 uint8_t *sensep; 25733 struct scsi_pkt *pkt; 25734 uchar_t actual_sense_length; 25735 dev_t dev = (dev_t)arg; 25736 25737 ASSERT(resultp != NULL); 25738 statusp = resultp->statusp; 25739 sensep = (uint8_t *)resultp->sensep; 25740 pkt = resultp->pkt; 25741 actual_sense_length = resultp->actual_sense_length; 25742 25743 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25744 return (ENXIO); 25745 } 25746 25747 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25748 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25749 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25750 25751 /* Begin processing of the status and/or sense data */ 25752 if (pkt->pkt_reason != CMD_CMPLT) { 25753 /* Handle the incomplete packet */ 25754 sd_mhd_watch_incomplete(un, pkt); 25755 return (0); 25756 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25757 if (*((unsigned char *)statusp) 25758 == STATUS_RESERVATION_CONFLICT) { 25759 /* 25760 * Handle a reservation conflict by panicking if 25761 * configured for failfast or by logging the conflict 25762 * and updating the reservation status 25763 */ 25764 mutex_enter(SD_MUTEX(un)); 25765 if ((un->un_resvd_status & SD_FAILFAST) && 25766 (sd_failfast_enable)) { 25767 sd_panic_for_res_conflict(un); 25768 /*NOTREACHED*/ 25769 } 25770 SD_INFO(SD_LOG_IOCTL_MHD, un, 25771 "sd_mhd_watch_cb: Reservation Conflict\n"); 25772 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25773 mutex_exit(SD_MUTEX(un)); 25774 } 25775 } 25776 25777 if (sensep != NULL) { 25778 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25779 mutex_enter(SD_MUTEX(un)); 25780 if ((scsi_sense_asc(sensep) == 25781 SD_SCSI_RESET_SENSE_CODE) && 25782 (un->un_resvd_status & SD_RESERVE)) { 25783 /* 25784 * The additional sense code indicates a power 25785 * on or bus device reset has occurred; update 25786 * the reservation status. 25787 */ 25788 un->un_resvd_status |= 25789 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25790 SD_INFO(SD_LOG_IOCTL_MHD, un, 25791 "sd_mhd_watch_cb: Lost Reservation\n"); 25792 } 25793 } else { 25794 return (0); 25795 } 25796 } else { 25797 mutex_enter(SD_MUTEX(un)); 25798 } 25799 25800 if ((un->un_resvd_status & SD_RESERVE) && 25801 (un->un_resvd_status & SD_LOST_RESERVE)) { 25802 if (un->un_resvd_status & SD_WANT_RESERVE) { 25803 /* 25804 * A reset occurred in between the last probe and this 25805 * one so if a timeout is pending cancel it. 25806 */ 25807 if (un->un_resvd_timeid) { 25808 timeout_id_t temp_id = un->un_resvd_timeid; 25809 un->un_resvd_timeid = NULL; 25810 mutex_exit(SD_MUTEX(un)); 25811 (void) untimeout(temp_id); 25812 mutex_enter(SD_MUTEX(un)); 25813 } 25814 un->un_resvd_status &= ~SD_WANT_RESERVE; 25815 } 25816 if (un->un_resvd_timeid == 0) { 25817 /* Schedule a timeout to handle the lost reservation */ 25818 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25819 (void *)dev, 25820 drv_usectohz(sd_reinstate_resv_delay)); 25821 } 25822 } 25823 mutex_exit(SD_MUTEX(un)); 25824 return (0); 25825 } 25826 25827 25828 /* 25829 * Function: sd_mhd_watch_incomplete() 25830 * 25831 * Description: This function is used to find out why a scsi pkt sent by the 25832 * scsi watch facility was not completed. Under some scenarios this 25833 * routine will return. Otherwise it will send a bus reset to see 25834 * if the drive is still online. 25835 * 25836 * Arguments: un - driver soft state (unit) structure 25837 * pkt - incomplete scsi pkt 25838 */ 25839 25840 static void 25841 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25842 { 25843 int be_chatty; 25844 int perr; 25845 25846 ASSERT(pkt != NULL); 25847 ASSERT(un != NULL); 25848 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25849 perr = (pkt->pkt_statistics & STAT_PERR); 25850 25851 mutex_enter(SD_MUTEX(un)); 25852 if (un->un_state == SD_STATE_DUMPING) { 25853 mutex_exit(SD_MUTEX(un)); 25854 return; 25855 } 25856 25857 switch (pkt->pkt_reason) { 25858 case CMD_UNX_BUS_FREE: 25859 /* 25860 * If we had a parity error that caused the target to drop BSY*, 25861 * don't be chatty about it. 25862 */ 25863 if (perr && be_chatty) { 25864 be_chatty = 0; 25865 } 25866 break; 25867 case CMD_TAG_REJECT: 25868 /* 25869 * The SCSI-2 spec states that a tag reject will be sent by the 25870 * target if tagged queuing is not supported. A tag reject may 25871 * also be sent during certain initialization periods or to 25872 * control internal resources. For the latter case the target 25873 * may also return Queue Full. 25874 * 25875 * If this driver receives a tag reject from a target that is 25876 * going through an init period or controlling internal 25877 * resources tagged queuing will be disabled. This is a less 25878 * than optimal behavior but the driver is unable to determine 25879 * the target state and assumes tagged queueing is not supported 25880 */ 25881 pkt->pkt_flags = 0; 25882 un->un_tagflags = 0; 25883 25884 if (un->un_f_opt_queueing == TRUE) { 25885 un->un_throttle = min(un->un_throttle, 3); 25886 } else { 25887 un->un_throttle = 1; 25888 } 25889 mutex_exit(SD_MUTEX(un)); 25890 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25891 mutex_enter(SD_MUTEX(un)); 25892 break; 25893 case CMD_INCOMPLETE: 25894 /* 25895 * The transport stopped with an abnormal state, fallthrough and 25896 * reset the target and/or bus unless selection did not complete 25897 * (indicated by STATE_GOT_BUS) in which case we don't want to 25898 * go through a target/bus reset 25899 */ 25900 if (pkt->pkt_state == STATE_GOT_BUS) { 25901 break; 25902 } 25903 /*FALLTHROUGH*/ 25904 25905 case CMD_TIMEOUT: 25906 default: 25907 /* 25908 * The lun may still be running the command, so a lun reset 25909 * should be attempted. If the lun reset fails or cannot be 25910 * issued, than try a target reset. Lastly try a bus reset. 25911 */ 25912 if ((pkt->pkt_statistics & 25913 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25914 int reset_retval = 0; 25915 mutex_exit(SD_MUTEX(un)); 25916 if (un->un_f_allow_bus_device_reset == TRUE) { 25917 if (un->un_f_lun_reset_enabled == TRUE) { 25918 reset_retval = 25919 scsi_reset(SD_ADDRESS(un), 25920 RESET_LUN); 25921 } 25922 if (reset_retval == 0) { 25923 reset_retval = 25924 scsi_reset(SD_ADDRESS(un), 25925 RESET_TARGET); 25926 } 25927 } 25928 if (reset_retval == 0) { 25929 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25930 } 25931 mutex_enter(SD_MUTEX(un)); 25932 } 25933 break; 25934 } 25935 25936 /* A device/bus reset has occurred; update the reservation status. */ 25937 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25938 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25939 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25940 un->un_resvd_status |= 25941 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25942 SD_INFO(SD_LOG_IOCTL_MHD, un, 25943 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25944 } 25945 } 25946 25947 /* 25948 * The disk has been turned off; Update the device state. 25949 * 25950 * Note: Should we be offlining the disk here? 25951 */ 25952 if (pkt->pkt_state == STATE_GOT_BUS) { 25953 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25954 "Disk not responding to selection\n"); 25955 if (un->un_state != SD_STATE_OFFLINE) { 25956 New_state(un, SD_STATE_OFFLINE); 25957 } 25958 } else if (be_chatty) { 25959 /* 25960 * suppress messages if they are all the same pkt reason; 25961 * with TQ, many (up to 256) are returned with the same 25962 * pkt_reason 25963 */ 25964 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25965 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25966 "sd_mhd_watch_incomplete: " 25967 "SCSI transport failed: reason '%s'\n", 25968 scsi_rname(pkt->pkt_reason)); 25969 } 25970 } 25971 un->un_last_pkt_reason = pkt->pkt_reason; 25972 mutex_exit(SD_MUTEX(un)); 25973 } 25974 25975 25976 /* 25977 * Function: sd_sname() 25978 * 25979 * Description: This is a simple little routine to return a string containing 25980 * a printable description of command status byte for use in 25981 * logging. 25982 * 25983 * Arguments: status - pointer to a status byte 25984 * 25985 * Return Code: char * - string containing status description. 25986 */ 25987 25988 static char * 25989 sd_sname(uchar_t status) 25990 { 25991 switch (status & STATUS_MASK) { 25992 case STATUS_GOOD: 25993 return ("good status"); 25994 case STATUS_CHECK: 25995 return ("check condition"); 25996 case STATUS_MET: 25997 return ("condition met"); 25998 case STATUS_BUSY: 25999 return ("busy"); 26000 case STATUS_INTERMEDIATE: 26001 return ("intermediate"); 26002 case STATUS_INTERMEDIATE_MET: 26003 return ("intermediate - condition met"); 26004 case STATUS_RESERVATION_CONFLICT: 26005 return ("reservation_conflict"); 26006 case STATUS_TERMINATED: 26007 return ("command terminated"); 26008 case STATUS_QFULL: 26009 return ("queue full"); 26010 default: 26011 return ("<unknown status>"); 26012 } 26013 } 26014 26015 26016 /* 26017 * Function: sd_mhd_resvd_recover() 26018 * 26019 * Description: This function adds a reservation entry to the 26020 * sd_resv_reclaim_request list and signals the reservation 26021 * reclaim thread that there is work pending. If the reservation 26022 * reclaim thread has not been previously created this function 26023 * will kick it off. 26024 * 26025 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26026 * among multiple watches that share this callback function 26027 * 26028 * Context: This routine is called by timeout() and is run in interrupt 26029 * context. It must not sleep or call other functions which may 26030 * sleep. 26031 */ 26032 26033 static void 26034 sd_mhd_resvd_recover(void *arg) 26035 { 26036 dev_t dev = (dev_t)arg; 26037 struct sd_lun *un; 26038 struct sd_thr_request *sd_treq = NULL; 26039 struct sd_thr_request *sd_cur = NULL; 26040 struct sd_thr_request *sd_prev = NULL; 26041 int already_there = 0; 26042 26043 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26044 return; 26045 } 26046 26047 mutex_enter(SD_MUTEX(un)); 26048 un->un_resvd_timeid = NULL; 26049 if (un->un_resvd_status & SD_WANT_RESERVE) { 26050 /* 26051 * There was a reset so don't issue the reserve, allow the 26052 * sd_mhd_watch_cb callback function to notice this and 26053 * reschedule the timeout for reservation. 26054 */ 26055 mutex_exit(SD_MUTEX(un)); 26056 return; 26057 } 26058 mutex_exit(SD_MUTEX(un)); 26059 26060 /* 26061 * Add this device to the sd_resv_reclaim_request list and the 26062 * sd_resv_reclaim_thread should take care of the rest. 26063 * 26064 * Note: We can't sleep in this context so if the memory allocation 26065 * fails allow the sd_mhd_watch_cb callback function to notice this and 26066 * reschedule the timeout for reservation. (4378460) 26067 */ 26068 sd_treq = (struct sd_thr_request *) 26069 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 26070 if (sd_treq == NULL) { 26071 return; 26072 } 26073 26074 sd_treq->sd_thr_req_next = NULL; 26075 sd_treq->dev = dev; 26076 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26077 if (sd_tr.srq_thr_req_head == NULL) { 26078 sd_tr.srq_thr_req_head = sd_treq; 26079 } else { 26080 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 26081 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 26082 if (sd_cur->dev == dev) { 26083 /* 26084 * already in Queue so don't log 26085 * another request for the device 26086 */ 26087 already_there = 1; 26088 break; 26089 } 26090 sd_prev = sd_cur; 26091 } 26092 if (!already_there) { 26093 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 26094 "logging request for %lx\n", dev); 26095 sd_prev->sd_thr_req_next = sd_treq; 26096 } else { 26097 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 26098 } 26099 } 26100 26101 /* 26102 * Create a kernel thread to do the reservation reclaim and free up this 26103 * thread. We cannot block this thread while we go away to do the 26104 * reservation reclaim 26105 */ 26106 if (sd_tr.srq_resv_reclaim_thread == NULL) 26107 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 26108 sd_resv_reclaim_thread, NULL, 26109 0, &p0, TS_RUN, v.v_maxsyspri - 2); 26110 26111 /* Tell the reservation reclaim thread that it has work to do */ 26112 cv_signal(&sd_tr.srq_resv_reclaim_cv); 26113 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26114 } 26115 26116 /* 26117 * Function: sd_resv_reclaim_thread() 26118 * 26119 * Description: This function implements the reservation reclaim operations 26120 * 26121 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26122 * among multiple watches that share this callback function 26123 */ 26124 26125 static void 26126 sd_resv_reclaim_thread() 26127 { 26128 struct sd_lun *un; 26129 struct sd_thr_request *sd_mhreq; 26130 26131 /* Wait for work */ 26132 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26133 if (sd_tr.srq_thr_req_head == NULL) { 26134 cv_wait(&sd_tr.srq_resv_reclaim_cv, 26135 &sd_tr.srq_resv_reclaim_mutex); 26136 } 26137 26138 /* Loop while we have work */ 26139 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 26140 un = ddi_get_soft_state(sd_state, 26141 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 26142 if (un == NULL) { 26143 /* 26144 * softstate structure is NULL so just 26145 * dequeue the request and continue 26146 */ 26147 sd_tr.srq_thr_req_head = 26148 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26149 kmem_free(sd_tr.srq_thr_cur_req, 26150 sizeof (struct sd_thr_request)); 26151 continue; 26152 } 26153 26154 /* dequeue the request */ 26155 sd_mhreq = sd_tr.srq_thr_cur_req; 26156 sd_tr.srq_thr_req_head = 26157 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26158 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26159 26160 /* 26161 * Reclaim reservation only if SD_RESERVE is still set. There 26162 * may have been a call to MHIOCRELEASE before we got here. 26163 */ 26164 mutex_enter(SD_MUTEX(un)); 26165 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26166 /* 26167 * Note: The SD_LOST_RESERVE flag is cleared before 26168 * reclaiming the reservation. If this is done after the 26169 * call to sd_reserve_release a reservation loss in the 26170 * window between pkt completion of reserve cmd and 26171 * mutex_enter below may not be recognized 26172 */ 26173 un->un_resvd_status &= ~SD_LOST_RESERVE; 26174 mutex_exit(SD_MUTEX(un)); 26175 26176 if (sd_reserve_release(sd_mhreq->dev, 26177 SD_RESERVE) == 0) { 26178 mutex_enter(SD_MUTEX(un)); 26179 un->un_resvd_status |= SD_RESERVE; 26180 mutex_exit(SD_MUTEX(un)); 26181 SD_INFO(SD_LOG_IOCTL_MHD, un, 26182 "sd_resv_reclaim_thread: " 26183 "Reservation Recovered\n"); 26184 } else { 26185 mutex_enter(SD_MUTEX(un)); 26186 un->un_resvd_status |= SD_LOST_RESERVE; 26187 mutex_exit(SD_MUTEX(un)); 26188 SD_INFO(SD_LOG_IOCTL_MHD, un, 26189 "sd_resv_reclaim_thread: Failed " 26190 "Reservation Recovery\n"); 26191 } 26192 } else { 26193 mutex_exit(SD_MUTEX(un)); 26194 } 26195 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26196 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 26197 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26198 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 26199 /* 26200 * wakeup the destroy thread if anyone is waiting on 26201 * us to complete. 26202 */ 26203 cv_signal(&sd_tr.srq_inprocess_cv); 26204 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26205 "sd_resv_reclaim_thread: cv_signalling current request \n"); 26206 } 26207 26208 /* 26209 * cleanup the sd_tr structure now that this thread will not exist 26210 */ 26211 ASSERT(sd_tr.srq_thr_req_head == NULL); 26212 ASSERT(sd_tr.srq_thr_cur_req == NULL); 26213 sd_tr.srq_resv_reclaim_thread = NULL; 26214 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26215 thread_exit(); 26216 } 26217 26218 26219 /* 26220 * Function: sd_rmv_resv_reclaim_req() 26221 * 26222 * Description: This function removes any pending reservation reclaim requests 26223 * for the specified device. 26224 * 26225 * Arguments: dev - the device 'dev_t' 26226 */ 26227 26228 static void 26229 sd_rmv_resv_reclaim_req(dev_t dev) 26230 { 26231 struct sd_thr_request *sd_mhreq; 26232 struct sd_thr_request *sd_prev; 26233 26234 /* Remove a reservation reclaim request from the list */ 26235 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26236 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 26237 /* 26238 * We are attempting to reinstate reservation for 26239 * this device. We wait for sd_reserve_release() 26240 * to return before we return. 26241 */ 26242 cv_wait(&sd_tr.srq_inprocess_cv, 26243 &sd_tr.srq_resv_reclaim_mutex); 26244 } else { 26245 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 26246 if (sd_mhreq && sd_mhreq->dev == dev) { 26247 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 26248 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26249 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26250 return; 26251 } 26252 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 26253 if (sd_mhreq && sd_mhreq->dev == dev) { 26254 break; 26255 } 26256 sd_prev = sd_mhreq; 26257 } 26258 if (sd_mhreq != NULL) { 26259 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 26260 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26261 } 26262 } 26263 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26264 } 26265 26266 26267 /* 26268 * Function: sd_mhd_reset_notify_cb() 26269 * 26270 * Description: This is a call back function for scsi_reset_notify. This 26271 * function updates the softstate reserved status and logs the 26272 * reset. The driver scsi watch facility callback function 26273 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 26274 * will reclaim the reservation. 26275 * 26276 * Arguments: arg - driver soft state (unit) structure 26277 */ 26278 26279 static void 26280 sd_mhd_reset_notify_cb(caddr_t arg) 26281 { 26282 struct sd_lun *un = (struct sd_lun *)arg; 26283 26284 mutex_enter(SD_MUTEX(un)); 26285 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26286 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 26287 SD_INFO(SD_LOG_IOCTL_MHD, un, 26288 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 26289 } 26290 mutex_exit(SD_MUTEX(un)); 26291 } 26292 26293 26294 /* 26295 * Function: sd_take_ownership() 26296 * 26297 * Description: This routine implements an algorithm to achieve a stable 26298 * reservation on disks which don't implement priority reserve, 26299 * and makes sure that other host lose re-reservation attempts. 26300 * This algorithm contains of a loop that keeps issuing the RESERVE 26301 * for some period of time (min_ownership_delay, default 6 seconds) 26302 * During that loop, it looks to see if there has been a bus device 26303 * reset or bus reset (both of which cause an existing reservation 26304 * to be lost). If the reservation is lost issue RESERVE until a 26305 * period of min_ownership_delay with no resets has gone by, or 26306 * until max_ownership_delay has expired. This loop ensures that 26307 * the host really did manage to reserve the device, in spite of 26308 * resets. The looping for min_ownership_delay (default six 26309 * seconds) is important to early generation clustering products, 26310 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 26311 * MHIOCENFAILFAST periodic timer of two seconds. By having 26312 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 26313 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 26314 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 26315 * have already noticed, via the MHIOCENFAILFAST polling, that it 26316 * no longer "owns" the disk and will have panicked itself. Thus, 26317 * the host issuing the MHIOCTKOWN is assured (with timing 26318 * dependencies) that by the time it actually starts to use the 26319 * disk for real work, the old owner is no longer accessing it. 26320 * 26321 * min_ownership_delay is the minimum amount of time for which the 26322 * disk must be reserved continuously devoid of resets before the 26323 * MHIOCTKOWN ioctl will return success. 26324 * 26325 * max_ownership_delay indicates the amount of time by which the 26326 * take ownership should succeed or timeout with an error. 26327 * 26328 * Arguments: dev - the device 'dev_t' 26329 * *p - struct containing timing info. 26330 * 26331 * Return Code: 0 for success or error code 26332 */ 26333 26334 static int 26335 sd_take_ownership(dev_t dev, struct mhioctkown *p) 26336 { 26337 struct sd_lun *un; 26338 int rval; 26339 int err; 26340 int reservation_count = 0; 26341 int min_ownership_delay = 6000000; /* in usec */ 26342 int max_ownership_delay = 30000000; /* in usec */ 26343 clock_t start_time; /* starting time of this algorithm */ 26344 clock_t end_time; /* time limit for giving up */ 26345 clock_t ownership_time; /* time limit for stable ownership */ 26346 clock_t current_time; 26347 clock_t previous_current_time; 26348 26349 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26350 return (ENXIO); 26351 } 26352 26353 /* 26354 * Attempt a device reservation. A priority reservation is requested. 26355 */ 26356 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 26357 != SD_SUCCESS) { 26358 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26359 "sd_take_ownership: return(1)=%d\n", rval); 26360 return (rval); 26361 } 26362 26363 /* Update the softstate reserved status to indicate the reservation */ 26364 mutex_enter(SD_MUTEX(un)); 26365 un->un_resvd_status |= SD_RESERVE; 26366 un->un_resvd_status &= 26367 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 26368 mutex_exit(SD_MUTEX(un)); 26369 26370 if (p != NULL) { 26371 if (p->min_ownership_delay != 0) { 26372 min_ownership_delay = p->min_ownership_delay * 1000; 26373 } 26374 if (p->max_ownership_delay != 0) { 26375 max_ownership_delay = p->max_ownership_delay * 1000; 26376 } 26377 } 26378 SD_INFO(SD_LOG_IOCTL_MHD, un, 26379 "sd_take_ownership: min, max delays: %d, %d\n", 26380 min_ownership_delay, max_ownership_delay); 26381 26382 start_time = ddi_get_lbolt(); 26383 current_time = start_time; 26384 ownership_time = current_time + drv_usectohz(min_ownership_delay); 26385 end_time = start_time + drv_usectohz(max_ownership_delay); 26386 26387 while (current_time - end_time < 0) { 26388 delay(drv_usectohz(500000)); 26389 26390 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 26391 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 26392 mutex_enter(SD_MUTEX(un)); 26393 rval = (un->un_resvd_status & 26394 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 26395 mutex_exit(SD_MUTEX(un)); 26396 break; 26397 } 26398 } 26399 previous_current_time = current_time; 26400 current_time = ddi_get_lbolt(); 26401 mutex_enter(SD_MUTEX(un)); 26402 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 26403 ownership_time = ddi_get_lbolt() + 26404 drv_usectohz(min_ownership_delay); 26405 reservation_count = 0; 26406 } else { 26407 reservation_count++; 26408 } 26409 un->un_resvd_status |= SD_RESERVE; 26410 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 26411 mutex_exit(SD_MUTEX(un)); 26412 26413 SD_INFO(SD_LOG_IOCTL_MHD, un, 26414 "sd_take_ownership: ticks for loop iteration=%ld, " 26415 "reservation=%s\n", (current_time - previous_current_time), 26416 reservation_count ? "ok" : "reclaimed"); 26417 26418 if (current_time - ownership_time >= 0 && 26419 reservation_count >= 4) { 26420 rval = 0; /* Achieved a stable ownership */ 26421 break; 26422 } 26423 if (current_time - end_time >= 0) { 26424 rval = EACCES; /* No ownership in max possible time */ 26425 break; 26426 } 26427 } 26428 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26429 "sd_take_ownership: return(2)=%d\n", rval); 26430 return (rval); 26431 } 26432 26433 26434 /* 26435 * Function: sd_reserve_release() 26436 * 26437 * Description: This function builds and sends scsi RESERVE, RELEASE, and 26438 * PRIORITY RESERVE commands based on a user specified command type 26439 * 26440 * Arguments: dev - the device 'dev_t' 26441 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 26442 * SD_RESERVE, SD_RELEASE 26443 * 26444 * Return Code: 0 or Error Code 26445 */ 26446 26447 static int 26448 sd_reserve_release(dev_t dev, int cmd) 26449 { 26450 struct uscsi_cmd *com = NULL; 26451 struct sd_lun *un = NULL; 26452 char cdb[CDB_GROUP0]; 26453 int rval; 26454 26455 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 26456 (cmd == SD_PRIORITY_RESERVE)); 26457 26458 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26459 return (ENXIO); 26460 } 26461 26462 /* instantiate and initialize the command and cdb */ 26463 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26464 bzero(cdb, CDB_GROUP0); 26465 com->uscsi_flags = USCSI_SILENT; 26466 com->uscsi_timeout = un->un_reserve_release_time; 26467 com->uscsi_cdblen = CDB_GROUP0; 26468 com->uscsi_cdb = cdb; 26469 if (cmd == SD_RELEASE) { 26470 cdb[0] = SCMD_RELEASE; 26471 } else { 26472 cdb[0] = SCMD_RESERVE; 26473 } 26474 26475 /* Send the command. */ 26476 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26477 UIO_SYSSPACE, SD_PATH_STANDARD); 26478 26479 /* 26480 * "break" a reservation that is held by another host, by issuing a 26481 * reset if priority reserve is desired, and we could not get the 26482 * device. 26483 */ 26484 if ((cmd == SD_PRIORITY_RESERVE) && 26485 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26486 /* 26487 * First try to reset the LUN. If we cannot, then try a target 26488 * reset, followed by a bus reset if the target reset fails. 26489 */ 26490 int reset_retval = 0; 26491 if (un->un_f_lun_reset_enabled == TRUE) { 26492 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26493 } 26494 if (reset_retval == 0) { 26495 /* The LUN reset either failed or was not issued */ 26496 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26497 } 26498 if ((reset_retval == 0) && 26499 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26500 rval = EIO; 26501 kmem_free(com, sizeof (*com)); 26502 return (rval); 26503 } 26504 26505 bzero(com, sizeof (struct uscsi_cmd)); 26506 com->uscsi_flags = USCSI_SILENT; 26507 com->uscsi_cdb = cdb; 26508 com->uscsi_cdblen = CDB_GROUP0; 26509 com->uscsi_timeout = 5; 26510 26511 /* 26512 * Reissue the last reserve command, this time without request 26513 * sense. Assume that it is just a regular reserve command. 26514 */ 26515 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26516 UIO_SYSSPACE, SD_PATH_STANDARD); 26517 } 26518 26519 /* Return an error if still getting a reservation conflict. */ 26520 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26521 rval = EACCES; 26522 } 26523 26524 kmem_free(com, sizeof (*com)); 26525 return (rval); 26526 } 26527 26528 26529 #define SD_NDUMP_RETRIES 12 26530 /* 26531 * System Crash Dump routine 26532 */ 26533 26534 static int 26535 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26536 { 26537 int instance; 26538 int partition; 26539 int i; 26540 int err; 26541 struct sd_lun *un; 26542 struct dk_map *lp; 26543 struct scsi_pkt *wr_pktp; 26544 struct buf *wr_bp; 26545 struct buf wr_buf; 26546 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26547 daddr_t tgt_blkno; /* rmw - blkno for target */ 26548 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26549 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26550 size_t io_start_offset; 26551 int doing_rmw = FALSE; 26552 int rval; 26553 #if defined(__i386) || defined(__amd64) 26554 ssize_t dma_resid; 26555 daddr_t oblkno; 26556 #endif 26557 26558 instance = SDUNIT(dev); 26559 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26560 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26561 return (ENXIO); 26562 } 26563 26564 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26565 26566 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26567 26568 partition = SDPART(dev); 26569 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26570 26571 /* Validate blocks to dump at against partition size. */ 26572 lp = &un->un_map[partition]; 26573 if ((blkno + nblk) > lp->dkl_nblk) { 26574 SD_TRACE(SD_LOG_DUMP, un, 26575 "sddump: dump range larger than partition: " 26576 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26577 blkno, nblk, lp->dkl_nblk); 26578 return (EINVAL); 26579 } 26580 26581 mutex_enter(&un->un_pm_mutex); 26582 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26583 struct scsi_pkt *start_pktp; 26584 26585 mutex_exit(&un->un_pm_mutex); 26586 26587 /* 26588 * use pm framework to power on HBA 1st 26589 */ 26590 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26591 26592 /* 26593 * Dump no long uses sdpower to power on a device, it's 26594 * in-line here so it can be done in polled mode. 26595 */ 26596 26597 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26598 26599 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26600 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26601 26602 if (start_pktp == NULL) { 26603 /* We were not given a SCSI packet, fail. */ 26604 return (EIO); 26605 } 26606 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26607 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26608 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26609 start_pktp->pkt_flags = FLAG_NOINTR; 26610 26611 mutex_enter(SD_MUTEX(un)); 26612 SD_FILL_SCSI1_LUN(un, start_pktp); 26613 mutex_exit(SD_MUTEX(un)); 26614 /* 26615 * Scsi_poll returns 0 (success) if the command completes and 26616 * the status block is STATUS_GOOD. 26617 */ 26618 if (sd_scsi_poll(un, start_pktp) != 0) { 26619 scsi_destroy_pkt(start_pktp); 26620 return (EIO); 26621 } 26622 scsi_destroy_pkt(start_pktp); 26623 (void) sd_ddi_pm_resume(un); 26624 } else { 26625 mutex_exit(&un->un_pm_mutex); 26626 } 26627 26628 mutex_enter(SD_MUTEX(un)); 26629 un->un_throttle = 0; 26630 26631 /* 26632 * The first time through, reset the specific target device. 26633 * However, when cpr calls sddump we know that sd is in a 26634 * a good state so no bus reset is required. 26635 * Clear sense data via Request Sense cmd. 26636 * In sddump we don't care about allow_bus_device_reset anymore 26637 */ 26638 26639 if ((un->un_state != SD_STATE_SUSPENDED) && 26640 (un->un_state != SD_STATE_DUMPING)) { 26641 26642 New_state(un, SD_STATE_DUMPING); 26643 26644 if (un->un_f_is_fibre == FALSE) { 26645 mutex_exit(SD_MUTEX(un)); 26646 /* 26647 * Attempt a bus reset for parallel scsi. 26648 * 26649 * Note: A bus reset is required because on some host 26650 * systems (i.e. E420R) a bus device reset is 26651 * insufficient to reset the state of the target. 26652 * 26653 * Note: Don't issue the reset for fibre-channel, 26654 * because this tends to hang the bus (loop) for 26655 * too long while everyone is logging out and in 26656 * and the deadman timer for dumping will fire 26657 * before the dump is complete. 26658 */ 26659 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26660 mutex_enter(SD_MUTEX(un)); 26661 Restore_state(un); 26662 mutex_exit(SD_MUTEX(un)); 26663 return (EIO); 26664 } 26665 26666 /* Delay to give the device some recovery time. */ 26667 drv_usecwait(10000); 26668 26669 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26670 SD_INFO(SD_LOG_DUMP, un, 26671 "sddump: sd_send_polled_RQS failed\n"); 26672 } 26673 mutex_enter(SD_MUTEX(un)); 26674 } 26675 } 26676 26677 /* 26678 * Convert the partition-relative block number to a 26679 * disk physical block number. 26680 */ 26681 blkno += un->un_offset[partition]; 26682 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26683 26684 26685 /* 26686 * Check if the device has a non-512 block size. 26687 */ 26688 wr_bp = NULL; 26689 if (NOT_DEVBSIZE(un)) { 26690 tgt_byte_offset = blkno * un->un_sys_blocksize; 26691 tgt_byte_count = nblk * un->un_sys_blocksize; 26692 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26693 (tgt_byte_count % un->un_tgt_blocksize)) { 26694 doing_rmw = TRUE; 26695 /* 26696 * Calculate the block number and number of block 26697 * in terms of the media block size. 26698 */ 26699 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26700 tgt_nblk = 26701 ((tgt_byte_offset + tgt_byte_count + 26702 (un->un_tgt_blocksize - 1)) / 26703 un->un_tgt_blocksize) - tgt_blkno; 26704 26705 /* 26706 * Invoke the routine which is going to do read part 26707 * of read-modify-write. 26708 * Note that this routine returns a pointer to 26709 * a valid bp in wr_bp. 26710 */ 26711 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26712 &wr_bp); 26713 if (err) { 26714 mutex_exit(SD_MUTEX(un)); 26715 return (err); 26716 } 26717 /* 26718 * Offset is being calculated as - 26719 * (original block # * system block size) - 26720 * (new block # * target block size) 26721 */ 26722 io_start_offset = 26723 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26724 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26725 26726 ASSERT((io_start_offset >= 0) && 26727 (io_start_offset < un->un_tgt_blocksize)); 26728 /* 26729 * Do the modify portion of read modify write. 26730 */ 26731 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26732 (size_t)nblk * un->un_sys_blocksize); 26733 } else { 26734 doing_rmw = FALSE; 26735 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26736 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26737 } 26738 26739 /* Convert blkno and nblk to target blocks */ 26740 blkno = tgt_blkno; 26741 nblk = tgt_nblk; 26742 } else { 26743 wr_bp = &wr_buf; 26744 bzero(wr_bp, sizeof (struct buf)); 26745 wr_bp->b_flags = B_BUSY; 26746 wr_bp->b_un.b_addr = addr; 26747 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26748 wr_bp->b_resid = 0; 26749 } 26750 26751 mutex_exit(SD_MUTEX(un)); 26752 26753 /* 26754 * Obtain a SCSI packet for the write command. 26755 * It should be safe to call the allocator here without 26756 * worrying about being locked for DVMA mapping because 26757 * the address we're passed is already a DVMA mapping 26758 * 26759 * We are also not going to worry about semaphore ownership 26760 * in the dump buffer. Dumping is single threaded at present. 26761 */ 26762 26763 wr_pktp = NULL; 26764 26765 #if defined(__i386) || defined(__amd64) 26766 dma_resid = wr_bp->b_bcount; 26767 oblkno = blkno; 26768 while (dma_resid != 0) { 26769 #endif 26770 26771 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26772 wr_bp->b_flags &= ~B_ERROR; 26773 26774 #if defined(__i386) || defined(__amd64) 26775 blkno = oblkno + 26776 ((wr_bp->b_bcount - dma_resid) / 26777 un->un_tgt_blocksize); 26778 nblk = dma_resid / un->un_tgt_blocksize; 26779 26780 if (wr_pktp) { 26781 /* Partial DMA transfers after initial transfer */ 26782 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26783 blkno, nblk); 26784 } else { 26785 /* Initial transfer */ 26786 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26787 un->un_pkt_flags, NULL_FUNC, NULL, 26788 blkno, nblk); 26789 } 26790 #else 26791 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26792 0, NULL_FUNC, NULL, blkno, nblk); 26793 #endif 26794 26795 if (rval == 0) { 26796 /* We were given a SCSI packet, continue. */ 26797 break; 26798 } 26799 26800 if (i == 0) { 26801 if (wr_bp->b_flags & B_ERROR) { 26802 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26803 "no resources for dumping; " 26804 "error code: 0x%x, retrying", 26805 geterror(wr_bp)); 26806 } else { 26807 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26808 "no resources for dumping; retrying"); 26809 } 26810 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26811 if (wr_bp->b_flags & B_ERROR) { 26812 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26813 "no resources for dumping; error code: " 26814 "0x%x, retrying\n", geterror(wr_bp)); 26815 } 26816 } else { 26817 if (wr_bp->b_flags & B_ERROR) { 26818 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26819 "no resources for dumping; " 26820 "error code: 0x%x, retries failed, " 26821 "giving up.\n", geterror(wr_bp)); 26822 } else { 26823 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26824 "no resources for dumping; " 26825 "retries failed, giving up.\n"); 26826 } 26827 mutex_enter(SD_MUTEX(un)); 26828 Restore_state(un); 26829 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26830 mutex_exit(SD_MUTEX(un)); 26831 scsi_free_consistent_buf(wr_bp); 26832 } else { 26833 mutex_exit(SD_MUTEX(un)); 26834 } 26835 return (EIO); 26836 } 26837 drv_usecwait(10000); 26838 } 26839 26840 #if defined(__i386) || defined(__amd64) 26841 /* 26842 * save the resid from PARTIAL_DMA 26843 */ 26844 dma_resid = wr_pktp->pkt_resid; 26845 if (dma_resid != 0) 26846 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26847 wr_pktp->pkt_resid = 0; 26848 #endif 26849 26850 /* SunBug 1222170 */ 26851 wr_pktp->pkt_flags = FLAG_NOINTR; 26852 26853 err = EIO; 26854 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26855 26856 /* 26857 * Scsi_poll returns 0 (success) if the command completes and 26858 * the status block is STATUS_GOOD. We should only check 26859 * errors if this condition is not true. Even then we should 26860 * send our own request sense packet only if we have a check 26861 * condition and auto request sense has not been performed by 26862 * the hba. 26863 */ 26864 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26865 26866 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26867 (wr_pktp->pkt_resid == 0)) { 26868 err = SD_SUCCESS; 26869 break; 26870 } 26871 26872 /* 26873 * Check CMD_DEV_GONE 1st, give up if device is gone. 26874 */ 26875 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26876 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26877 "Device is gone\n"); 26878 break; 26879 } 26880 26881 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26882 SD_INFO(SD_LOG_DUMP, un, 26883 "sddump: write failed with CHECK, try # %d\n", i); 26884 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26885 (void) sd_send_polled_RQS(un); 26886 } 26887 26888 continue; 26889 } 26890 26891 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26892 int reset_retval = 0; 26893 26894 SD_INFO(SD_LOG_DUMP, un, 26895 "sddump: write failed with BUSY, try # %d\n", i); 26896 26897 if (un->un_f_lun_reset_enabled == TRUE) { 26898 reset_retval = scsi_reset(SD_ADDRESS(un), 26899 RESET_LUN); 26900 } 26901 if (reset_retval == 0) { 26902 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26903 } 26904 (void) sd_send_polled_RQS(un); 26905 26906 } else { 26907 SD_INFO(SD_LOG_DUMP, un, 26908 "sddump: write failed with 0x%x, try # %d\n", 26909 SD_GET_PKT_STATUS(wr_pktp), i); 26910 mutex_enter(SD_MUTEX(un)); 26911 sd_reset_target(un, wr_pktp); 26912 mutex_exit(SD_MUTEX(un)); 26913 } 26914 26915 /* 26916 * If we are not getting anywhere with lun/target resets, 26917 * let's reset the bus. 26918 */ 26919 if (i == SD_NDUMP_RETRIES/2) { 26920 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26921 (void) sd_send_polled_RQS(un); 26922 } 26923 26924 } 26925 #if defined(__i386) || defined(__amd64) 26926 } /* dma_resid */ 26927 #endif 26928 26929 scsi_destroy_pkt(wr_pktp); 26930 mutex_enter(SD_MUTEX(un)); 26931 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26932 mutex_exit(SD_MUTEX(un)); 26933 scsi_free_consistent_buf(wr_bp); 26934 } else { 26935 mutex_exit(SD_MUTEX(un)); 26936 } 26937 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26938 return (err); 26939 } 26940 26941 /* 26942 * Function: sd_scsi_poll() 26943 * 26944 * Description: This is a wrapper for the scsi_poll call. 26945 * 26946 * Arguments: sd_lun - The unit structure 26947 * scsi_pkt - The scsi packet being sent to the device. 26948 * 26949 * Return Code: 0 - Command completed successfully with good status 26950 * -1 - Command failed. This could indicate a check condition 26951 * or other status value requiring recovery action. 26952 * 26953 */ 26954 26955 static int 26956 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26957 { 26958 int status; 26959 26960 ASSERT(un != NULL); 26961 ASSERT(!mutex_owned(SD_MUTEX(un))); 26962 ASSERT(pktp != NULL); 26963 26964 status = SD_SUCCESS; 26965 26966 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26967 pktp->pkt_flags |= un->un_tagflags; 26968 pktp->pkt_flags &= ~FLAG_NODISCON; 26969 } 26970 26971 status = sd_ddi_scsi_poll(pktp); 26972 /* 26973 * Scsi_poll returns 0 (success) if the command completes and the 26974 * status block is STATUS_GOOD. We should only check errors if this 26975 * condition is not true. Even then we should send our own request 26976 * sense packet only if we have a check condition and auto 26977 * request sense has not been performed by the hba. 26978 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26979 */ 26980 if ((status != SD_SUCCESS) && 26981 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26982 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26983 (pktp->pkt_reason != CMD_DEV_GONE)) 26984 (void) sd_send_polled_RQS(un); 26985 26986 return (status); 26987 } 26988 26989 /* 26990 * Function: sd_send_polled_RQS() 26991 * 26992 * Description: This sends the request sense command to a device. 26993 * 26994 * Arguments: sd_lun - The unit structure 26995 * 26996 * Return Code: 0 - Command completed successfully with good status 26997 * -1 - Command failed. 26998 * 26999 */ 27000 27001 static int 27002 sd_send_polled_RQS(struct sd_lun *un) 27003 { 27004 int ret_val; 27005 struct scsi_pkt *rqs_pktp; 27006 struct buf *rqs_bp; 27007 27008 ASSERT(un != NULL); 27009 ASSERT(!mutex_owned(SD_MUTEX(un))); 27010 27011 ret_val = SD_SUCCESS; 27012 27013 rqs_pktp = un->un_rqs_pktp; 27014 rqs_bp = un->un_rqs_bp; 27015 27016 mutex_enter(SD_MUTEX(un)); 27017 27018 if (un->un_sense_isbusy) { 27019 ret_val = SD_FAILURE; 27020 mutex_exit(SD_MUTEX(un)); 27021 return (ret_val); 27022 } 27023 27024 /* 27025 * If the request sense buffer (and packet) is not in use, 27026 * let's set the un_sense_isbusy and send our packet 27027 */ 27028 un->un_sense_isbusy = 1; 27029 rqs_pktp->pkt_resid = 0; 27030 rqs_pktp->pkt_reason = 0; 27031 rqs_pktp->pkt_flags |= FLAG_NOINTR; 27032 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 27033 27034 mutex_exit(SD_MUTEX(un)); 27035 27036 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 27037 " 0x%p\n", rqs_bp->b_un.b_addr); 27038 27039 /* 27040 * Can't send this to sd_scsi_poll, we wrap ourselves around the 27041 * axle - it has a call into us! 27042 */ 27043 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 27044 SD_INFO(SD_LOG_COMMON, un, 27045 "sd_send_polled_RQS: RQS failed\n"); 27046 } 27047 27048 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 27049 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 27050 27051 mutex_enter(SD_MUTEX(un)); 27052 un->un_sense_isbusy = 0; 27053 mutex_exit(SD_MUTEX(un)); 27054 27055 return (ret_val); 27056 } 27057 27058 /* 27059 * Defines needed for localized version of the scsi_poll routine. 27060 */ 27061 #define SD_CSEC 10000 /* usecs */ 27062 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 27063 27064 27065 /* 27066 * Function: sd_ddi_scsi_poll() 27067 * 27068 * Description: Localized version of the scsi_poll routine. The purpose is to 27069 * send a scsi_pkt to a device as a polled command. This version 27070 * is to ensure more robust handling of transport errors. 27071 * Specifically this routine cures not ready, coming ready 27072 * transition for power up and reset of sonoma's. This can take 27073 * up to 45 seconds for power-on and 20 seconds for reset of a 27074 * sonoma lun. 27075 * 27076 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 27077 * 27078 * Return Code: 0 - Command completed successfully with good status 27079 * -1 - Command failed. 27080 * 27081 */ 27082 27083 static int 27084 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 27085 { 27086 int busy_count; 27087 int timeout; 27088 int rval = SD_FAILURE; 27089 int savef; 27090 uint8_t *sensep; 27091 long savet; 27092 void (*savec)(); 27093 /* 27094 * The following is defined in machdep.c and is used in determining if 27095 * the scsi transport system will do polled I/O instead of interrupt 27096 * I/O when called from xx_dump(). 27097 */ 27098 extern int do_polled_io; 27099 27100 /* 27101 * save old flags in pkt, to restore at end 27102 */ 27103 savef = pkt->pkt_flags; 27104 savec = pkt->pkt_comp; 27105 savet = pkt->pkt_time; 27106 27107 pkt->pkt_flags |= FLAG_NOINTR; 27108 27109 /* 27110 * XXX there is nothing in the SCSA spec that states that we should not 27111 * do a callback for polled cmds; however, removing this will break sd 27112 * and probably other target drivers 27113 */ 27114 pkt->pkt_comp = NULL; 27115 27116 /* 27117 * we don't like a polled command without timeout. 27118 * 60 seconds seems long enough. 27119 */ 27120 if (pkt->pkt_time == 0) { 27121 pkt->pkt_time = SCSI_POLL_TIMEOUT; 27122 } 27123 27124 /* 27125 * Send polled cmd. 27126 * 27127 * We do some error recovery for various errors. Tran_busy, 27128 * queue full, and non-dispatched commands are retried every 10 msec. 27129 * as they are typically transient failures. Busy status and Not 27130 * Ready are retried every second as this status takes a while to 27131 * change. Unit attention is retried for pkt_time (60) times 27132 * with no delay. 27133 */ 27134 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 27135 27136 for (busy_count = 0; busy_count < timeout; busy_count++) { 27137 int rc; 27138 int poll_delay; 27139 27140 /* 27141 * Initialize pkt status variables. 27142 */ 27143 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 27144 27145 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 27146 if (rc != TRAN_BUSY) { 27147 /* Transport failed - give up. */ 27148 break; 27149 } else { 27150 /* Transport busy - try again. */ 27151 poll_delay = 1 * SD_CSEC; /* 10 msec */ 27152 } 27153 } else { 27154 /* 27155 * Transport accepted - check pkt status. 27156 */ 27157 rc = (*pkt->pkt_scbp) & STATUS_MASK; 27158 if (pkt->pkt_reason == CMD_CMPLT && 27159 rc == STATUS_CHECK && 27160 pkt->pkt_state & STATE_ARQ_DONE) { 27161 struct scsi_arq_status *arqstat = 27162 (struct scsi_arq_status *)(pkt->pkt_scbp); 27163 27164 sensep = (uint8_t *)&arqstat->sts_sensedata; 27165 } else { 27166 sensep = NULL; 27167 } 27168 27169 if ((pkt->pkt_reason == CMD_CMPLT) && 27170 (rc == STATUS_GOOD)) { 27171 /* No error - we're done */ 27172 rval = SD_SUCCESS; 27173 break; 27174 27175 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 27176 /* Lost connection - give up */ 27177 break; 27178 27179 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 27180 (pkt->pkt_state == 0)) { 27181 /* Pkt not dispatched - try again. */ 27182 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27183 27184 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27185 (rc == STATUS_QFULL)) { 27186 /* Queue full - try again. */ 27187 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27188 27189 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27190 (rc == STATUS_BUSY)) { 27191 /* Busy - try again. */ 27192 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27193 busy_count += (SD_SEC_TO_CSEC - 1); 27194 27195 } else if ((sensep != NULL) && 27196 (scsi_sense_key(sensep) == 27197 KEY_UNIT_ATTENTION)) { 27198 /* Unit Attention - try again */ 27199 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 27200 continue; 27201 27202 } else if ((sensep != NULL) && 27203 (scsi_sense_key(sensep) == KEY_NOT_READY) && 27204 (scsi_sense_asc(sensep) == 0x04) && 27205 (scsi_sense_ascq(sensep) == 0x01)) { 27206 /* Not ready -> ready - try again. */ 27207 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27208 busy_count += (SD_SEC_TO_CSEC - 1); 27209 27210 } else { 27211 /* BAD status - give up. */ 27212 break; 27213 } 27214 } 27215 27216 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 27217 !do_polled_io) { 27218 delay(drv_usectohz(poll_delay)); 27219 } else { 27220 /* we busy wait during cpr_dump or interrupt threads */ 27221 drv_usecwait(poll_delay); 27222 } 27223 } 27224 27225 pkt->pkt_flags = savef; 27226 pkt->pkt_comp = savec; 27227 pkt->pkt_time = savet; 27228 return (rval); 27229 } 27230 27231 27232 /* 27233 * Function: sd_persistent_reservation_in_read_keys 27234 * 27235 * Description: This routine is the driver entry point for handling CD-ROM 27236 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 27237 * by sending the SCSI-3 PRIN commands to the device. 27238 * Processes the read keys command response by copying the 27239 * reservation key information into the user provided buffer. 27240 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 27241 * 27242 * Arguments: un - Pointer to soft state struct for the target. 27243 * usrp - user provided pointer to multihost Persistent In Read 27244 * Keys structure (mhioc_inkeys_t) 27245 * flag - this argument is a pass through to ddi_copyxxx() 27246 * directly from the mode argument of ioctl(). 27247 * 27248 * Return Code: 0 - Success 27249 * EACCES 27250 * ENOTSUP 27251 * errno return code from sd_send_scsi_cmd() 27252 * 27253 * Context: Can sleep. Does not return until command is completed. 27254 */ 27255 27256 static int 27257 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 27258 mhioc_inkeys_t *usrp, int flag) 27259 { 27260 #ifdef _MULTI_DATAMODEL 27261 struct mhioc_key_list32 li32; 27262 #endif 27263 sd_prin_readkeys_t *in; 27264 mhioc_inkeys_t *ptr; 27265 mhioc_key_list_t li; 27266 uchar_t *data_bufp; 27267 int data_len; 27268 int rval; 27269 size_t copysz; 27270 27271 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 27272 return (EINVAL); 27273 } 27274 bzero(&li, sizeof (mhioc_key_list_t)); 27275 27276 /* 27277 * Get the listsize from user 27278 */ 27279 #ifdef _MULTI_DATAMODEL 27280 27281 switch (ddi_model_convert_from(flag & FMODELS)) { 27282 case DDI_MODEL_ILP32: 27283 copysz = sizeof (struct mhioc_key_list32); 27284 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 27285 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27286 "sd_persistent_reservation_in_read_keys: " 27287 "failed ddi_copyin: mhioc_key_list32_t\n"); 27288 rval = EFAULT; 27289 goto done; 27290 } 27291 li.listsize = li32.listsize; 27292 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 27293 break; 27294 27295 case DDI_MODEL_NONE: 27296 copysz = sizeof (mhioc_key_list_t); 27297 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27298 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27299 "sd_persistent_reservation_in_read_keys: " 27300 "failed ddi_copyin: mhioc_key_list_t\n"); 27301 rval = EFAULT; 27302 goto done; 27303 } 27304 break; 27305 } 27306 27307 #else /* ! _MULTI_DATAMODEL */ 27308 copysz = sizeof (mhioc_key_list_t); 27309 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27310 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27311 "sd_persistent_reservation_in_read_keys: " 27312 "failed ddi_copyin: mhioc_key_list_t\n"); 27313 rval = EFAULT; 27314 goto done; 27315 } 27316 #endif 27317 27318 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 27319 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 27320 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27321 27322 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 27323 data_len, data_bufp)) != 0) { 27324 goto done; 27325 } 27326 in = (sd_prin_readkeys_t *)data_bufp; 27327 ptr->generation = BE_32(in->generation); 27328 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 27329 27330 /* 27331 * Return the min(listsize, listlen) keys 27332 */ 27333 #ifdef _MULTI_DATAMODEL 27334 27335 switch (ddi_model_convert_from(flag & FMODELS)) { 27336 case DDI_MODEL_ILP32: 27337 li32.listlen = li.listlen; 27338 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 27339 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27340 "sd_persistent_reservation_in_read_keys: " 27341 "failed ddi_copyout: mhioc_key_list32_t\n"); 27342 rval = EFAULT; 27343 goto done; 27344 } 27345 break; 27346 27347 case DDI_MODEL_NONE: 27348 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27349 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27350 "sd_persistent_reservation_in_read_keys: " 27351 "failed ddi_copyout: mhioc_key_list_t\n"); 27352 rval = EFAULT; 27353 goto done; 27354 } 27355 break; 27356 } 27357 27358 #else /* ! _MULTI_DATAMODEL */ 27359 27360 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27361 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27362 "sd_persistent_reservation_in_read_keys: " 27363 "failed ddi_copyout: mhioc_key_list_t\n"); 27364 rval = EFAULT; 27365 goto done; 27366 } 27367 27368 #endif /* _MULTI_DATAMODEL */ 27369 27370 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 27371 li.listsize * MHIOC_RESV_KEY_SIZE); 27372 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 27373 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27374 "sd_persistent_reservation_in_read_keys: " 27375 "failed ddi_copyout: keylist\n"); 27376 rval = EFAULT; 27377 } 27378 done: 27379 kmem_free(data_bufp, data_len); 27380 return (rval); 27381 } 27382 27383 27384 /* 27385 * Function: sd_persistent_reservation_in_read_resv 27386 * 27387 * Description: This routine is the driver entry point for handling CD-ROM 27388 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 27389 * by sending the SCSI-3 PRIN commands to the device. 27390 * Process the read persistent reservations command response by 27391 * copying the reservation information into the user provided 27392 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 27393 * 27394 * Arguments: un - Pointer to soft state struct for the target. 27395 * usrp - user provided pointer to multihost Persistent In Read 27396 * Keys structure (mhioc_inkeys_t) 27397 * flag - this argument is a pass through to ddi_copyxxx() 27398 * directly from the mode argument of ioctl(). 27399 * 27400 * Return Code: 0 - Success 27401 * EACCES 27402 * ENOTSUP 27403 * errno return code from sd_send_scsi_cmd() 27404 * 27405 * Context: Can sleep. Does not return until command is completed. 27406 */ 27407 27408 static int 27409 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 27410 mhioc_inresvs_t *usrp, int flag) 27411 { 27412 #ifdef _MULTI_DATAMODEL 27413 struct mhioc_resv_desc_list32 resvlist32; 27414 #endif 27415 sd_prin_readresv_t *in; 27416 mhioc_inresvs_t *ptr; 27417 sd_readresv_desc_t *readresv_ptr; 27418 mhioc_resv_desc_list_t resvlist; 27419 mhioc_resv_desc_t resvdesc; 27420 uchar_t *data_bufp; 27421 int data_len; 27422 int rval; 27423 int i; 27424 size_t copysz; 27425 mhioc_resv_desc_t *bufp; 27426 27427 if ((ptr = usrp) == NULL) { 27428 return (EINVAL); 27429 } 27430 27431 /* 27432 * Get the listsize from user 27433 */ 27434 #ifdef _MULTI_DATAMODEL 27435 switch (ddi_model_convert_from(flag & FMODELS)) { 27436 case DDI_MODEL_ILP32: 27437 copysz = sizeof (struct mhioc_resv_desc_list32); 27438 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 27439 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27440 "sd_persistent_reservation_in_read_resv: " 27441 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27442 rval = EFAULT; 27443 goto done; 27444 } 27445 resvlist.listsize = resvlist32.listsize; 27446 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 27447 break; 27448 27449 case DDI_MODEL_NONE: 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 break; 27459 } 27460 #else /* ! _MULTI_DATAMODEL */ 27461 copysz = sizeof (mhioc_resv_desc_list_t); 27462 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27463 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27464 "sd_persistent_reservation_in_read_resv: " 27465 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27466 rval = EFAULT; 27467 goto done; 27468 } 27469 #endif /* ! _MULTI_DATAMODEL */ 27470 27471 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 27472 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 27473 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27474 27475 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 27476 data_len, data_bufp)) != 0) { 27477 goto done; 27478 } 27479 in = (sd_prin_readresv_t *)data_bufp; 27480 ptr->generation = BE_32(in->generation); 27481 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 27482 27483 /* 27484 * Return the min(listsize, listlen( keys 27485 */ 27486 #ifdef _MULTI_DATAMODEL 27487 27488 switch (ddi_model_convert_from(flag & FMODELS)) { 27489 case DDI_MODEL_ILP32: 27490 resvlist32.listlen = resvlist.listlen; 27491 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27492 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27493 "sd_persistent_reservation_in_read_resv: " 27494 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27495 rval = EFAULT; 27496 goto done; 27497 } 27498 break; 27499 27500 case DDI_MODEL_NONE: 27501 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27502 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27503 "sd_persistent_reservation_in_read_resv: " 27504 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27505 rval = EFAULT; 27506 goto done; 27507 } 27508 break; 27509 } 27510 27511 #else /* ! _MULTI_DATAMODEL */ 27512 27513 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27514 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27515 "sd_persistent_reservation_in_read_resv: " 27516 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27517 rval = EFAULT; 27518 goto done; 27519 } 27520 27521 #endif /* ! _MULTI_DATAMODEL */ 27522 27523 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27524 bufp = resvlist.list; 27525 copysz = sizeof (mhioc_resv_desc_t); 27526 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27527 i++, readresv_ptr++, bufp++) { 27528 27529 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27530 MHIOC_RESV_KEY_SIZE); 27531 resvdesc.type = readresv_ptr->type; 27532 resvdesc.scope = readresv_ptr->scope; 27533 resvdesc.scope_specific_addr = 27534 BE_32(readresv_ptr->scope_specific_addr); 27535 27536 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27537 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27538 "sd_persistent_reservation_in_read_resv: " 27539 "failed ddi_copyout: resvlist\n"); 27540 rval = EFAULT; 27541 goto done; 27542 } 27543 } 27544 done: 27545 kmem_free(data_bufp, data_len); 27546 return (rval); 27547 } 27548 27549 27550 /* 27551 * Function: sr_change_blkmode() 27552 * 27553 * Description: This routine is the driver entry point for handling CD-ROM 27554 * block mode ioctl requests. Support for returning and changing 27555 * the current block size in use by the device is implemented. The 27556 * LBA size is changed via a MODE SELECT Block Descriptor. 27557 * 27558 * This routine issues a mode sense with an allocation length of 27559 * 12 bytes for the mode page header and a single block descriptor. 27560 * 27561 * Arguments: dev - the device 'dev_t' 27562 * cmd - the request type; one of CDROMGBLKMODE (get) or 27563 * CDROMSBLKMODE (set) 27564 * data - current block size or requested block size 27565 * flag - this argument is a pass through to ddi_copyxxx() directly 27566 * from the mode argument of ioctl(). 27567 * 27568 * Return Code: the code returned by sd_send_scsi_cmd() 27569 * EINVAL if invalid arguments are provided 27570 * EFAULT if ddi_copyxxx() fails 27571 * ENXIO if fail ddi_get_soft_state 27572 * EIO if invalid mode sense block descriptor length 27573 * 27574 */ 27575 27576 static int 27577 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27578 { 27579 struct sd_lun *un = NULL; 27580 struct mode_header *sense_mhp, *select_mhp; 27581 struct block_descriptor *sense_desc, *select_desc; 27582 int current_bsize; 27583 int rval = EINVAL; 27584 uchar_t *sense = NULL; 27585 uchar_t *select = NULL; 27586 27587 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27588 27589 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27590 return (ENXIO); 27591 } 27592 27593 /* 27594 * The block length is changed via the Mode Select block descriptor, the 27595 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27596 * required as part of this routine. Therefore the mode sense allocation 27597 * length is specified to be the length of a mode page header and a 27598 * block descriptor. 27599 */ 27600 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27601 27602 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27603 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27605 "sr_change_blkmode: Mode Sense Failed\n"); 27606 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27607 return (rval); 27608 } 27609 27610 /* Check the block descriptor len to handle only 1 block descriptor */ 27611 sense_mhp = (struct mode_header *)sense; 27612 if ((sense_mhp->bdesc_length == 0) || 27613 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27614 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27615 "sr_change_blkmode: Mode Sense returned invalid block" 27616 " descriptor length\n"); 27617 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27618 return (EIO); 27619 } 27620 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27621 current_bsize = ((sense_desc->blksize_hi << 16) | 27622 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27623 27624 /* Process command */ 27625 switch (cmd) { 27626 case CDROMGBLKMODE: 27627 /* Return the block size obtained during the mode sense */ 27628 if (ddi_copyout(¤t_bsize, (void *)data, 27629 sizeof (int), flag) != 0) 27630 rval = EFAULT; 27631 break; 27632 case CDROMSBLKMODE: 27633 /* Validate the requested block size */ 27634 switch (data) { 27635 case CDROM_BLK_512: 27636 case CDROM_BLK_1024: 27637 case CDROM_BLK_2048: 27638 case CDROM_BLK_2056: 27639 case CDROM_BLK_2336: 27640 case CDROM_BLK_2340: 27641 case CDROM_BLK_2352: 27642 case CDROM_BLK_2368: 27643 case CDROM_BLK_2448: 27644 case CDROM_BLK_2646: 27645 case CDROM_BLK_2647: 27646 break; 27647 default: 27648 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27649 "sr_change_blkmode: " 27650 "Block Size '%ld' Not Supported\n", data); 27651 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27652 return (EINVAL); 27653 } 27654 27655 /* 27656 * The current block size matches the requested block size so 27657 * there is no need to send the mode select to change the size 27658 */ 27659 if (current_bsize == data) { 27660 break; 27661 } 27662 27663 /* Build the select data for the requested block size */ 27664 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27665 select_mhp = (struct mode_header *)select; 27666 select_desc = 27667 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27668 /* 27669 * The LBA size is changed via the block descriptor, so the 27670 * descriptor is built according to the user data 27671 */ 27672 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27673 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27674 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27675 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27676 27677 /* Send the mode select for the requested block size */ 27678 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27679 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27680 SD_PATH_STANDARD)) != 0) { 27681 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27682 "sr_change_blkmode: Mode Select Failed\n"); 27683 /* 27684 * The mode select failed for the requested block size, 27685 * so reset the data for the original block size and 27686 * send it to the target. The error is indicated by the 27687 * return value for the failed mode select. 27688 */ 27689 select_desc->blksize_hi = sense_desc->blksize_hi; 27690 select_desc->blksize_mid = sense_desc->blksize_mid; 27691 select_desc->blksize_lo = sense_desc->blksize_lo; 27692 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27693 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27694 SD_PATH_STANDARD); 27695 } else { 27696 ASSERT(!mutex_owned(SD_MUTEX(un))); 27697 mutex_enter(SD_MUTEX(un)); 27698 sd_update_block_info(un, (uint32_t)data, 0); 27699 27700 mutex_exit(SD_MUTEX(un)); 27701 } 27702 break; 27703 default: 27704 /* should not reach here, but check anyway */ 27705 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27706 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27707 rval = EINVAL; 27708 break; 27709 } 27710 27711 if (select) { 27712 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27713 } 27714 if (sense) { 27715 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27716 } 27717 return (rval); 27718 } 27719 27720 27721 /* 27722 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27723 * implement driver support for getting and setting the CD speed. The command 27724 * set used will be based on the device type. If the device has not been 27725 * identified as MMC the Toshiba vendor specific mode page will be used. If 27726 * the device is MMC but does not support the Real Time Streaming feature 27727 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27728 * be used to read the speed. 27729 */ 27730 27731 /* 27732 * Function: sr_change_speed() 27733 * 27734 * Description: This routine is the driver entry point for handling CD-ROM 27735 * drive speed ioctl requests for devices supporting the Toshiba 27736 * vendor specific drive speed mode page. Support for returning 27737 * and changing the current drive speed in use by the device is 27738 * implemented. 27739 * 27740 * Arguments: dev - the device 'dev_t' 27741 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27742 * CDROMSDRVSPEED (set) 27743 * data - current drive speed or requested drive speed 27744 * flag - this argument is a pass through to ddi_copyxxx() directly 27745 * from the mode argument of ioctl(). 27746 * 27747 * Return Code: the code returned by sd_send_scsi_cmd() 27748 * EINVAL if invalid arguments are provided 27749 * EFAULT if ddi_copyxxx() fails 27750 * ENXIO if fail ddi_get_soft_state 27751 * EIO if invalid mode sense block descriptor length 27752 */ 27753 27754 static int 27755 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27756 { 27757 struct sd_lun *un = NULL; 27758 struct mode_header *sense_mhp, *select_mhp; 27759 struct mode_speed *sense_page, *select_page; 27760 int current_speed; 27761 int rval = EINVAL; 27762 int bd_len; 27763 uchar_t *sense = NULL; 27764 uchar_t *select = NULL; 27765 27766 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27767 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27768 return (ENXIO); 27769 } 27770 27771 /* 27772 * Note: The drive speed is being modified here according to a Toshiba 27773 * vendor specific mode page (0x31). 27774 */ 27775 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27776 27777 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27778 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27779 SD_PATH_STANDARD)) != 0) { 27780 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27781 "sr_change_speed: Mode Sense Failed\n"); 27782 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27783 return (rval); 27784 } 27785 sense_mhp = (struct mode_header *)sense; 27786 27787 /* Check the block descriptor len to handle only 1 block descriptor */ 27788 bd_len = sense_mhp->bdesc_length; 27789 if (bd_len > MODE_BLK_DESC_LENGTH) { 27790 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27791 "sr_change_speed: Mode Sense returned invalid block " 27792 "descriptor length\n"); 27793 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27794 return (EIO); 27795 } 27796 27797 sense_page = (struct mode_speed *) 27798 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27799 current_speed = sense_page->speed; 27800 27801 /* Process command */ 27802 switch (cmd) { 27803 case CDROMGDRVSPEED: 27804 /* Return the drive speed obtained during the mode sense */ 27805 if (current_speed == 0x2) { 27806 current_speed = CDROM_TWELVE_SPEED; 27807 } 27808 if (ddi_copyout(¤t_speed, (void *)data, 27809 sizeof (int), flag) != 0) { 27810 rval = EFAULT; 27811 } 27812 break; 27813 case CDROMSDRVSPEED: 27814 /* Validate the requested drive speed */ 27815 switch ((uchar_t)data) { 27816 case CDROM_TWELVE_SPEED: 27817 data = 0x2; 27818 /*FALLTHROUGH*/ 27819 case CDROM_NORMAL_SPEED: 27820 case CDROM_DOUBLE_SPEED: 27821 case CDROM_QUAD_SPEED: 27822 case CDROM_MAXIMUM_SPEED: 27823 break; 27824 default: 27825 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27826 "sr_change_speed: " 27827 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27828 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27829 return (EINVAL); 27830 } 27831 27832 /* 27833 * The current drive speed matches the requested drive speed so 27834 * there is no need to send the mode select to change the speed 27835 */ 27836 if (current_speed == data) { 27837 break; 27838 } 27839 27840 /* Build the select data for the requested drive speed */ 27841 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27842 select_mhp = (struct mode_header *)select; 27843 select_mhp->bdesc_length = 0; 27844 select_page = 27845 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27846 select_page = 27847 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27848 select_page->mode_page.code = CDROM_MODE_SPEED; 27849 select_page->mode_page.length = 2; 27850 select_page->speed = (uchar_t)data; 27851 27852 /* Send the mode select for the requested block size */ 27853 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27854 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27855 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27856 /* 27857 * The mode select failed for the requested drive speed, 27858 * so reset the data for the original drive speed and 27859 * send it to the target. The error is indicated by the 27860 * return value for the failed mode select. 27861 */ 27862 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27863 "sr_drive_speed: Mode Select Failed\n"); 27864 select_page->speed = sense_page->speed; 27865 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27866 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27867 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27868 } 27869 break; 27870 default: 27871 /* should not reach here, but check anyway */ 27872 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27873 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27874 rval = EINVAL; 27875 break; 27876 } 27877 27878 if (select) { 27879 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27880 } 27881 if (sense) { 27882 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27883 } 27884 27885 return (rval); 27886 } 27887 27888 27889 /* 27890 * Function: sr_atapi_change_speed() 27891 * 27892 * Description: This routine is the driver entry point for handling CD-ROM 27893 * drive speed ioctl requests for MMC devices that do not support 27894 * the Real Time Streaming feature (0x107). 27895 * 27896 * Note: This routine will use the SET SPEED command which may not 27897 * be supported by all devices. 27898 * 27899 * Arguments: dev- the device 'dev_t' 27900 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27901 * CDROMSDRVSPEED (set) 27902 * data- current drive speed or requested drive speed 27903 * flag- this argument is a pass through to ddi_copyxxx() directly 27904 * from the mode argument of ioctl(). 27905 * 27906 * Return Code: the code returned by sd_send_scsi_cmd() 27907 * EINVAL if invalid arguments are provided 27908 * EFAULT if ddi_copyxxx() fails 27909 * ENXIO if fail ddi_get_soft_state 27910 * EIO if invalid mode sense block descriptor length 27911 */ 27912 27913 static int 27914 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27915 { 27916 struct sd_lun *un; 27917 struct uscsi_cmd *com = NULL; 27918 struct mode_header_grp2 *sense_mhp; 27919 uchar_t *sense_page; 27920 uchar_t *sense = NULL; 27921 char cdb[CDB_GROUP5]; 27922 int bd_len; 27923 int current_speed = 0; 27924 int max_speed = 0; 27925 int rval; 27926 27927 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27928 27929 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27930 return (ENXIO); 27931 } 27932 27933 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27934 27935 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27936 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27937 SD_PATH_STANDARD)) != 0) { 27938 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27939 "sr_atapi_change_speed: Mode Sense Failed\n"); 27940 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27941 return (rval); 27942 } 27943 27944 /* Check the block descriptor len to handle only 1 block descriptor */ 27945 sense_mhp = (struct mode_header_grp2 *)sense; 27946 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27947 if (bd_len > MODE_BLK_DESC_LENGTH) { 27948 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27949 "sr_atapi_change_speed: Mode Sense returned invalid " 27950 "block descriptor length\n"); 27951 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27952 return (EIO); 27953 } 27954 27955 /* Calculate the current and maximum drive speeds */ 27956 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27957 current_speed = (sense_page[14] << 8) | sense_page[15]; 27958 max_speed = (sense_page[8] << 8) | sense_page[9]; 27959 27960 /* Process the command */ 27961 switch (cmd) { 27962 case CDROMGDRVSPEED: 27963 current_speed /= SD_SPEED_1X; 27964 if (ddi_copyout(¤t_speed, (void *)data, 27965 sizeof (int), flag) != 0) 27966 rval = EFAULT; 27967 break; 27968 case CDROMSDRVSPEED: 27969 /* Convert the speed code to KB/sec */ 27970 switch ((uchar_t)data) { 27971 case CDROM_NORMAL_SPEED: 27972 current_speed = SD_SPEED_1X; 27973 break; 27974 case CDROM_DOUBLE_SPEED: 27975 current_speed = 2 * SD_SPEED_1X; 27976 break; 27977 case CDROM_QUAD_SPEED: 27978 current_speed = 4 * SD_SPEED_1X; 27979 break; 27980 case CDROM_TWELVE_SPEED: 27981 current_speed = 12 * SD_SPEED_1X; 27982 break; 27983 case CDROM_MAXIMUM_SPEED: 27984 current_speed = 0xffff; 27985 break; 27986 default: 27987 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27988 "sr_atapi_change_speed: invalid drive speed %d\n", 27989 (uchar_t)data); 27990 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27991 return (EINVAL); 27992 } 27993 27994 /* Check the request against the drive's max speed. */ 27995 if (current_speed != 0xffff) { 27996 if (current_speed > max_speed) { 27997 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27998 return (EINVAL); 27999 } 28000 } 28001 28002 /* 28003 * Build and send the SET SPEED command 28004 * 28005 * Note: The SET SPEED (0xBB) command used in this routine is 28006 * obsolete per the SCSI MMC spec but still supported in the 28007 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28008 * therefore the command is still implemented in this routine. 28009 */ 28010 bzero(cdb, sizeof (cdb)); 28011 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 28012 cdb[2] = (uchar_t)(current_speed >> 8); 28013 cdb[3] = (uchar_t)current_speed; 28014 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28015 com->uscsi_cdb = (caddr_t)cdb; 28016 com->uscsi_cdblen = CDB_GROUP5; 28017 com->uscsi_bufaddr = NULL; 28018 com->uscsi_buflen = 0; 28019 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28020 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 28021 UIO_SYSSPACE, SD_PATH_STANDARD); 28022 break; 28023 default: 28024 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28025 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 28026 rval = EINVAL; 28027 } 28028 28029 if (sense) { 28030 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 28031 } 28032 if (com) { 28033 kmem_free(com, sizeof (*com)); 28034 } 28035 return (rval); 28036 } 28037 28038 28039 /* 28040 * Function: sr_pause_resume() 28041 * 28042 * Description: This routine is the driver entry point for handling CD-ROM 28043 * pause/resume ioctl requests. This only affects the audio play 28044 * operation. 28045 * 28046 * Arguments: dev - the device 'dev_t' 28047 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 28048 * for setting the resume bit of the cdb. 28049 * 28050 * Return Code: the code returned by sd_send_scsi_cmd() 28051 * EINVAL if invalid mode specified 28052 * 28053 */ 28054 28055 static int 28056 sr_pause_resume(dev_t dev, int cmd) 28057 { 28058 struct sd_lun *un; 28059 struct uscsi_cmd *com; 28060 char cdb[CDB_GROUP1]; 28061 int rval; 28062 28063 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28064 return (ENXIO); 28065 } 28066 28067 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28068 bzero(cdb, CDB_GROUP1); 28069 cdb[0] = SCMD_PAUSE_RESUME; 28070 switch (cmd) { 28071 case CDROMRESUME: 28072 cdb[8] = 1; 28073 break; 28074 case CDROMPAUSE: 28075 cdb[8] = 0; 28076 break; 28077 default: 28078 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 28079 " Command '%x' Not Supported\n", cmd); 28080 rval = EINVAL; 28081 goto done; 28082 } 28083 28084 com->uscsi_cdb = cdb; 28085 com->uscsi_cdblen = CDB_GROUP1; 28086 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28087 28088 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28089 UIO_SYSSPACE, SD_PATH_STANDARD); 28090 28091 done: 28092 kmem_free(com, sizeof (*com)); 28093 return (rval); 28094 } 28095 28096 28097 /* 28098 * Function: sr_play_msf() 28099 * 28100 * Description: This routine is the driver entry point for handling CD-ROM 28101 * ioctl requests to output the audio signals at the specified 28102 * starting address and continue the audio play until the specified 28103 * ending address (CDROMPLAYMSF) The address is in Minute Second 28104 * Frame (MSF) format. 28105 * 28106 * Arguments: dev - the device 'dev_t' 28107 * data - pointer to user provided audio msf structure, 28108 * specifying start/end addresses. 28109 * flag - this argument is a pass through to ddi_copyxxx() 28110 * directly from the mode argument of ioctl(). 28111 * 28112 * Return Code: the code returned by sd_send_scsi_cmd() 28113 * EFAULT if ddi_copyxxx() fails 28114 * ENXIO if fail ddi_get_soft_state 28115 * EINVAL if data pointer is NULL 28116 */ 28117 28118 static int 28119 sr_play_msf(dev_t dev, caddr_t data, int flag) 28120 { 28121 struct sd_lun *un; 28122 struct uscsi_cmd *com; 28123 struct cdrom_msf msf_struct; 28124 struct cdrom_msf *msf = &msf_struct; 28125 char cdb[CDB_GROUP1]; 28126 int rval; 28127 28128 if (data == NULL) { 28129 return (EINVAL); 28130 } 28131 28132 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28133 return (ENXIO); 28134 } 28135 28136 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 28137 return (EFAULT); 28138 } 28139 28140 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28141 bzero(cdb, CDB_GROUP1); 28142 cdb[0] = SCMD_PLAYAUDIO_MSF; 28143 if (un->un_f_cfg_playmsf_bcd == TRUE) { 28144 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 28145 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 28146 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 28147 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 28148 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 28149 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 28150 } else { 28151 cdb[3] = msf->cdmsf_min0; 28152 cdb[4] = msf->cdmsf_sec0; 28153 cdb[5] = msf->cdmsf_frame0; 28154 cdb[6] = msf->cdmsf_min1; 28155 cdb[7] = msf->cdmsf_sec1; 28156 cdb[8] = msf->cdmsf_frame1; 28157 } 28158 com->uscsi_cdb = cdb; 28159 com->uscsi_cdblen = CDB_GROUP1; 28160 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28161 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28162 UIO_SYSSPACE, SD_PATH_STANDARD); 28163 kmem_free(com, sizeof (*com)); 28164 return (rval); 28165 } 28166 28167 28168 /* 28169 * Function: sr_play_trkind() 28170 * 28171 * Description: This routine is the driver entry point for handling CD-ROM 28172 * ioctl requests to output the audio signals at the specified 28173 * starting address and continue the audio play until the specified 28174 * ending address (CDROMPLAYTRKIND). The address is in Track Index 28175 * format. 28176 * 28177 * Arguments: dev - the device 'dev_t' 28178 * data - pointer to user provided audio track/index structure, 28179 * specifying start/end addresses. 28180 * flag - this argument is a pass through to ddi_copyxxx() 28181 * directly from the mode argument of ioctl(). 28182 * 28183 * Return Code: the code returned by sd_send_scsi_cmd() 28184 * EFAULT if ddi_copyxxx() fails 28185 * ENXIO if fail ddi_get_soft_state 28186 * EINVAL if data pointer is NULL 28187 */ 28188 28189 static int 28190 sr_play_trkind(dev_t dev, caddr_t data, int flag) 28191 { 28192 struct cdrom_ti ti_struct; 28193 struct cdrom_ti *ti = &ti_struct; 28194 struct uscsi_cmd *com = NULL; 28195 char cdb[CDB_GROUP1]; 28196 int rval; 28197 28198 if (data == NULL) { 28199 return (EINVAL); 28200 } 28201 28202 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 28203 return (EFAULT); 28204 } 28205 28206 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28207 bzero(cdb, CDB_GROUP1); 28208 cdb[0] = SCMD_PLAYAUDIO_TI; 28209 cdb[4] = ti->cdti_trk0; 28210 cdb[5] = ti->cdti_ind0; 28211 cdb[7] = ti->cdti_trk1; 28212 cdb[8] = ti->cdti_ind1; 28213 com->uscsi_cdb = cdb; 28214 com->uscsi_cdblen = CDB_GROUP1; 28215 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28216 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28217 UIO_SYSSPACE, SD_PATH_STANDARD); 28218 kmem_free(com, sizeof (*com)); 28219 return (rval); 28220 } 28221 28222 28223 /* 28224 * Function: sr_read_all_subcodes() 28225 * 28226 * Description: This routine is the driver entry point for handling CD-ROM 28227 * ioctl requests to return raw subcode data while the target is 28228 * playing audio (CDROMSUBCODE). 28229 * 28230 * Arguments: dev - the device 'dev_t' 28231 * data - pointer to user provided cdrom subcode structure, 28232 * specifying the transfer length and address. 28233 * flag - this argument is a pass through to ddi_copyxxx() 28234 * directly from the mode argument of ioctl(). 28235 * 28236 * Return Code: the code returned by sd_send_scsi_cmd() 28237 * EFAULT if ddi_copyxxx() fails 28238 * ENXIO if fail ddi_get_soft_state 28239 * EINVAL if data pointer is NULL 28240 */ 28241 28242 static int 28243 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 28244 { 28245 struct sd_lun *un = NULL; 28246 struct uscsi_cmd *com = NULL; 28247 struct cdrom_subcode *subcode = NULL; 28248 int rval; 28249 size_t buflen; 28250 char cdb[CDB_GROUP5]; 28251 28252 #ifdef _MULTI_DATAMODEL 28253 /* To support ILP32 applications in an LP64 world */ 28254 struct cdrom_subcode32 cdrom_subcode32; 28255 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 28256 #endif 28257 if (data == NULL) { 28258 return (EINVAL); 28259 } 28260 28261 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28262 return (ENXIO); 28263 } 28264 28265 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 28266 28267 #ifdef _MULTI_DATAMODEL 28268 switch (ddi_model_convert_from(flag & FMODELS)) { 28269 case DDI_MODEL_ILP32: 28270 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), 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 /* Convert the ILP32 uscsi data from the application to LP64 */ 28277 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 28278 break; 28279 case DDI_MODEL_NONE: 28280 if (ddi_copyin(data, subcode, 28281 sizeof (struct cdrom_subcode), flag)) { 28282 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28283 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28284 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28285 return (EFAULT); 28286 } 28287 break; 28288 } 28289 #else /* ! _MULTI_DATAMODEL */ 28290 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 28291 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28292 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28293 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28294 return (EFAULT); 28295 } 28296 #endif /* _MULTI_DATAMODEL */ 28297 28298 /* 28299 * Since MMC-2 expects max 3 bytes for length, check if the 28300 * length input is greater than 3 bytes 28301 */ 28302 if ((subcode->cdsc_length & 0xFF000000) != 0) { 28303 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28304 "sr_read_all_subcodes: " 28305 "cdrom transfer length too large: %d (limit %d)\n", 28306 subcode->cdsc_length, 0xFFFFFF); 28307 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28308 return (EINVAL); 28309 } 28310 28311 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 28312 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28313 bzero(cdb, CDB_GROUP5); 28314 28315 if (un->un_f_mmc_cap == TRUE) { 28316 cdb[0] = (char)SCMD_READ_CD; 28317 cdb[2] = (char)0xff; 28318 cdb[3] = (char)0xff; 28319 cdb[4] = (char)0xff; 28320 cdb[5] = (char)0xff; 28321 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28322 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28323 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 28324 cdb[10] = 1; 28325 } else { 28326 /* 28327 * Note: A vendor specific command (0xDF) is being used her to 28328 * request a read of all subcodes. 28329 */ 28330 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 28331 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 28332 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28333 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28334 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 28335 } 28336 com->uscsi_cdb = cdb; 28337 com->uscsi_cdblen = CDB_GROUP5; 28338 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 28339 com->uscsi_buflen = buflen; 28340 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28341 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28342 UIO_SYSSPACE, SD_PATH_STANDARD); 28343 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28344 kmem_free(com, sizeof (*com)); 28345 return (rval); 28346 } 28347 28348 28349 /* 28350 * Function: sr_read_subchannel() 28351 * 28352 * Description: This routine is the driver entry point for handling CD-ROM 28353 * ioctl requests to return the Q sub-channel data of the CD 28354 * current position block. (CDROMSUBCHNL) The data includes the 28355 * track number, index number, absolute CD-ROM address (LBA or MSF 28356 * format per the user) , track relative CD-ROM address (LBA or MSF 28357 * format per the user), control data and audio status. 28358 * 28359 * Arguments: dev - the device 'dev_t' 28360 * data - pointer to user provided cdrom sub-channel structure 28361 * flag - this argument is a pass through to ddi_copyxxx() 28362 * directly from the mode argument of ioctl(). 28363 * 28364 * Return Code: the code returned by sd_send_scsi_cmd() 28365 * EFAULT if ddi_copyxxx() fails 28366 * ENXIO if fail ddi_get_soft_state 28367 * EINVAL if data pointer is NULL 28368 */ 28369 28370 static int 28371 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 28372 { 28373 struct sd_lun *un; 28374 struct uscsi_cmd *com; 28375 struct cdrom_subchnl subchanel; 28376 struct cdrom_subchnl *subchnl = &subchanel; 28377 char cdb[CDB_GROUP1]; 28378 caddr_t buffer; 28379 int rval; 28380 28381 if (data == NULL) { 28382 return (EINVAL); 28383 } 28384 28385 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28386 (un->un_state == SD_STATE_OFFLINE)) { 28387 return (ENXIO); 28388 } 28389 28390 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 28391 return (EFAULT); 28392 } 28393 28394 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 28395 bzero(cdb, CDB_GROUP1); 28396 cdb[0] = SCMD_READ_SUBCHANNEL; 28397 /* Set the MSF bit based on the user requested address format */ 28398 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 28399 /* 28400 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 28401 * returned 28402 */ 28403 cdb[2] = 0x40; 28404 /* 28405 * Set byte 3 to specify the return data format. A value of 0x01 28406 * indicates that the CD-ROM current position should be returned. 28407 */ 28408 cdb[3] = 0x01; 28409 cdb[8] = 0x10; 28410 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28411 com->uscsi_cdb = cdb; 28412 com->uscsi_cdblen = CDB_GROUP1; 28413 com->uscsi_bufaddr = buffer; 28414 com->uscsi_buflen = 16; 28415 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28416 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28417 UIO_SYSSPACE, SD_PATH_STANDARD); 28418 if (rval != 0) { 28419 kmem_free(buffer, 16); 28420 kmem_free(com, sizeof (*com)); 28421 return (rval); 28422 } 28423 28424 /* Process the returned Q sub-channel data */ 28425 subchnl->cdsc_audiostatus = buffer[1]; 28426 subchnl->cdsc_adr = (buffer[5] & 0xF0); 28427 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 28428 subchnl->cdsc_trk = buffer[6]; 28429 subchnl->cdsc_ind = buffer[7]; 28430 if (subchnl->cdsc_format & CDROM_LBA) { 28431 subchnl->cdsc_absaddr.lba = 28432 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28433 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28434 subchnl->cdsc_reladdr.lba = 28435 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 28436 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 28437 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 28438 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 28439 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 28440 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 28441 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 28442 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 28443 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 28444 } else { 28445 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 28446 subchnl->cdsc_absaddr.msf.second = buffer[10]; 28447 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 28448 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 28449 subchnl->cdsc_reladdr.msf.second = buffer[14]; 28450 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 28451 } 28452 kmem_free(buffer, 16); 28453 kmem_free(com, sizeof (*com)); 28454 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 28455 != 0) { 28456 return (EFAULT); 28457 } 28458 return (rval); 28459 } 28460 28461 28462 /* 28463 * Function: sr_read_tocentry() 28464 * 28465 * Description: This routine is the driver entry point for handling CD-ROM 28466 * ioctl requests to read from the Table of Contents (TOC) 28467 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 28468 * fields, the starting address (LBA or MSF format per the user) 28469 * and the data mode if the user specified track is a data track. 28470 * 28471 * Note: The READ HEADER (0x44) command used in this routine is 28472 * obsolete per the SCSI MMC spec but still supported in the 28473 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28474 * therefore the command is still implemented in this routine. 28475 * 28476 * Arguments: dev - the device 'dev_t' 28477 * data - pointer to user provided toc entry structure, 28478 * specifying the track # and the address format 28479 * (LBA or MSF). 28480 * flag - this argument is a pass through to ddi_copyxxx() 28481 * directly from the mode argument of ioctl(). 28482 * 28483 * Return Code: the code returned by sd_send_scsi_cmd() 28484 * EFAULT if ddi_copyxxx() fails 28485 * ENXIO if fail ddi_get_soft_state 28486 * EINVAL if data pointer is NULL 28487 */ 28488 28489 static int 28490 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28491 { 28492 struct sd_lun *un = NULL; 28493 struct uscsi_cmd *com; 28494 struct cdrom_tocentry toc_entry; 28495 struct cdrom_tocentry *entry = &toc_entry; 28496 caddr_t buffer; 28497 int rval; 28498 char cdb[CDB_GROUP1]; 28499 28500 if (data == NULL) { 28501 return (EINVAL); 28502 } 28503 28504 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28505 (un->un_state == SD_STATE_OFFLINE)) { 28506 return (ENXIO); 28507 } 28508 28509 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28510 return (EFAULT); 28511 } 28512 28513 /* Validate the requested track and address format */ 28514 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28515 return (EINVAL); 28516 } 28517 28518 if (entry->cdte_track == 0) { 28519 return (EINVAL); 28520 } 28521 28522 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28523 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28524 bzero(cdb, CDB_GROUP1); 28525 28526 cdb[0] = SCMD_READ_TOC; 28527 /* Set the MSF bit based on the user requested address format */ 28528 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28529 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28530 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28531 } else { 28532 cdb[6] = entry->cdte_track; 28533 } 28534 28535 /* 28536 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28537 * (4 byte TOC response header + 8 byte track descriptor) 28538 */ 28539 cdb[8] = 12; 28540 com->uscsi_cdb = cdb; 28541 com->uscsi_cdblen = CDB_GROUP1; 28542 com->uscsi_bufaddr = buffer; 28543 com->uscsi_buflen = 0x0C; 28544 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28545 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28546 UIO_SYSSPACE, SD_PATH_STANDARD); 28547 if (rval != 0) { 28548 kmem_free(buffer, 12); 28549 kmem_free(com, sizeof (*com)); 28550 return (rval); 28551 } 28552 28553 /* Process the toc entry */ 28554 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28555 entry->cdte_ctrl = (buffer[5] & 0x0F); 28556 if (entry->cdte_format & CDROM_LBA) { 28557 entry->cdte_addr.lba = 28558 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28559 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28560 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28561 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28562 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28563 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28564 /* 28565 * Send a READ TOC command using the LBA address format to get 28566 * the LBA for the track requested so it can be used in the 28567 * READ HEADER request 28568 * 28569 * Note: The MSF bit of the READ HEADER command specifies the 28570 * output format. The block address specified in that command 28571 * must be in LBA format. 28572 */ 28573 cdb[1] = 0; 28574 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28575 UIO_SYSSPACE, SD_PATH_STANDARD); 28576 if (rval != 0) { 28577 kmem_free(buffer, 12); 28578 kmem_free(com, sizeof (*com)); 28579 return (rval); 28580 } 28581 } else { 28582 entry->cdte_addr.msf.minute = buffer[9]; 28583 entry->cdte_addr.msf.second = buffer[10]; 28584 entry->cdte_addr.msf.frame = buffer[11]; 28585 /* 28586 * Send a READ TOC command using the LBA address format to get 28587 * the LBA for the track requested so it can be used in the 28588 * READ HEADER request 28589 * 28590 * Note: The MSF bit of the READ HEADER command specifies the 28591 * output format. The block address specified in that command 28592 * must be in LBA format. 28593 */ 28594 cdb[1] = 0; 28595 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28596 UIO_SYSSPACE, SD_PATH_STANDARD); 28597 if (rval != 0) { 28598 kmem_free(buffer, 12); 28599 kmem_free(com, sizeof (*com)); 28600 return (rval); 28601 } 28602 } 28603 28604 /* 28605 * Build and send the READ HEADER command to determine the data mode of 28606 * the user specified track. 28607 */ 28608 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28609 (entry->cdte_track != CDROM_LEADOUT)) { 28610 bzero(cdb, CDB_GROUP1); 28611 cdb[0] = SCMD_READ_HEADER; 28612 cdb[2] = buffer[8]; 28613 cdb[3] = buffer[9]; 28614 cdb[4] = buffer[10]; 28615 cdb[5] = buffer[11]; 28616 cdb[8] = 0x08; 28617 com->uscsi_buflen = 0x08; 28618 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28619 UIO_SYSSPACE, SD_PATH_STANDARD); 28620 if (rval == 0) { 28621 entry->cdte_datamode = buffer[0]; 28622 } else { 28623 /* 28624 * READ HEADER command failed, since this is 28625 * obsoleted in one spec, its better to return 28626 * -1 for an invlid track so that we can still 28627 * recieve the rest of the TOC data. 28628 */ 28629 entry->cdte_datamode = (uchar_t)-1; 28630 } 28631 } else { 28632 entry->cdte_datamode = (uchar_t)-1; 28633 } 28634 28635 kmem_free(buffer, 12); 28636 kmem_free(com, sizeof (*com)); 28637 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28638 return (EFAULT); 28639 28640 return (rval); 28641 } 28642 28643 28644 /* 28645 * Function: sr_read_tochdr() 28646 * 28647 * Description: This routine is the driver entry point for handling CD-ROM 28648 * ioctl requests to read the Table of Contents (TOC) header 28649 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28650 * and ending track numbers 28651 * 28652 * Arguments: dev - the device 'dev_t' 28653 * data - pointer to user provided toc header structure, 28654 * specifying the starting and ending track numbers. 28655 * flag - this argument is a pass through to ddi_copyxxx() 28656 * directly from the mode argument of ioctl(). 28657 * 28658 * Return Code: the code returned by sd_send_scsi_cmd() 28659 * EFAULT if ddi_copyxxx() fails 28660 * ENXIO if fail ddi_get_soft_state 28661 * EINVAL if data pointer is NULL 28662 */ 28663 28664 static int 28665 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28666 { 28667 struct sd_lun *un; 28668 struct uscsi_cmd *com; 28669 struct cdrom_tochdr toc_header; 28670 struct cdrom_tochdr *hdr = &toc_header; 28671 char cdb[CDB_GROUP1]; 28672 int rval; 28673 caddr_t buffer; 28674 28675 if (data == NULL) { 28676 return (EINVAL); 28677 } 28678 28679 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28680 (un->un_state == SD_STATE_OFFLINE)) { 28681 return (ENXIO); 28682 } 28683 28684 buffer = kmem_zalloc(4, KM_SLEEP); 28685 bzero(cdb, CDB_GROUP1); 28686 cdb[0] = SCMD_READ_TOC; 28687 /* 28688 * Specifying a track number of 0x00 in the READ TOC command indicates 28689 * that the TOC header should be returned 28690 */ 28691 cdb[6] = 0x00; 28692 /* 28693 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28694 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28695 */ 28696 cdb[8] = 0x04; 28697 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28698 com->uscsi_cdb = cdb; 28699 com->uscsi_cdblen = CDB_GROUP1; 28700 com->uscsi_bufaddr = buffer; 28701 com->uscsi_buflen = 0x04; 28702 com->uscsi_timeout = 300; 28703 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28704 28705 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28706 UIO_SYSSPACE, SD_PATH_STANDARD); 28707 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28708 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28709 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28710 } else { 28711 hdr->cdth_trk0 = buffer[2]; 28712 hdr->cdth_trk1 = buffer[3]; 28713 } 28714 kmem_free(buffer, 4); 28715 kmem_free(com, sizeof (*com)); 28716 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28717 return (EFAULT); 28718 } 28719 return (rval); 28720 } 28721 28722 28723 /* 28724 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28725 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28726 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28727 * digital audio and extended architecture digital audio. These modes are 28728 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28729 * MMC specs. 28730 * 28731 * In addition to support for the various data formats these routines also 28732 * include support for devices that implement only the direct access READ 28733 * commands (0x08, 0x28), devices that implement the READ_CD commands 28734 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28735 * READ CDXA commands (0xD8, 0xDB) 28736 */ 28737 28738 /* 28739 * Function: sr_read_mode1() 28740 * 28741 * Description: This routine is the driver entry point for handling CD-ROM 28742 * ioctl read mode1 requests (CDROMREADMODE1). 28743 * 28744 * Arguments: dev - the device 'dev_t' 28745 * data - pointer to user provided cd read structure specifying 28746 * the lba buffer address and length. 28747 * flag - this argument is a pass through to ddi_copyxxx() 28748 * directly from the mode argument of ioctl(). 28749 * 28750 * Return Code: the code returned by sd_send_scsi_cmd() 28751 * EFAULT if ddi_copyxxx() fails 28752 * ENXIO if fail ddi_get_soft_state 28753 * EINVAL if data pointer is NULL 28754 */ 28755 28756 static int 28757 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28758 { 28759 struct sd_lun *un; 28760 struct cdrom_read mode1_struct; 28761 struct cdrom_read *mode1 = &mode1_struct; 28762 int rval; 28763 #ifdef _MULTI_DATAMODEL 28764 /* To support ILP32 applications in an LP64 world */ 28765 struct cdrom_read32 cdrom_read32; 28766 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28767 #endif /* _MULTI_DATAMODEL */ 28768 28769 if (data == NULL) { 28770 return (EINVAL); 28771 } 28772 28773 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28774 (un->un_state == SD_STATE_OFFLINE)) { 28775 return (ENXIO); 28776 } 28777 28778 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28779 "sd_read_mode1: entry: un:0x%p\n", un); 28780 28781 #ifdef _MULTI_DATAMODEL 28782 switch (ddi_model_convert_from(flag & FMODELS)) { 28783 case DDI_MODEL_ILP32: 28784 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28785 return (EFAULT); 28786 } 28787 /* Convert the ILP32 uscsi data from the application to LP64 */ 28788 cdrom_read32tocdrom_read(cdrd32, mode1); 28789 break; 28790 case DDI_MODEL_NONE: 28791 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28792 return (EFAULT); 28793 } 28794 } 28795 #else /* ! _MULTI_DATAMODEL */ 28796 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28797 return (EFAULT); 28798 } 28799 #endif /* _MULTI_DATAMODEL */ 28800 28801 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28802 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28803 28804 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28805 "sd_read_mode1: exit: un:0x%p\n", un); 28806 28807 return (rval); 28808 } 28809 28810 28811 /* 28812 * Function: sr_read_cd_mode2() 28813 * 28814 * Description: This routine is the driver entry point for handling CD-ROM 28815 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28816 * support the READ CD (0xBE) command or the 1st generation 28817 * READ CD (0xD4) command. 28818 * 28819 * Arguments: dev - the device 'dev_t' 28820 * data - pointer to user provided cd read structure specifying 28821 * the lba buffer address and length. 28822 * flag - this argument is a pass through to ddi_copyxxx() 28823 * directly from the mode argument of ioctl(). 28824 * 28825 * Return Code: the code returned by sd_send_scsi_cmd() 28826 * EFAULT if ddi_copyxxx() fails 28827 * ENXIO if fail ddi_get_soft_state 28828 * EINVAL if data pointer is NULL 28829 */ 28830 28831 static int 28832 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28833 { 28834 struct sd_lun *un; 28835 struct uscsi_cmd *com; 28836 struct cdrom_read mode2_struct; 28837 struct cdrom_read *mode2 = &mode2_struct; 28838 uchar_t cdb[CDB_GROUP5]; 28839 int nblocks; 28840 int rval; 28841 #ifdef _MULTI_DATAMODEL 28842 /* To support ILP32 applications in an LP64 world */ 28843 struct cdrom_read32 cdrom_read32; 28844 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28845 #endif /* _MULTI_DATAMODEL */ 28846 28847 if (data == NULL) { 28848 return (EINVAL); 28849 } 28850 28851 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28852 (un->un_state == SD_STATE_OFFLINE)) { 28853 return (ENXIO); 28854 } 28855 28856 #ifdef _MULTI_DATAMODEL 28857 switch (ddi_model_convert_from(flag & FMODELS)) { 28858 case DDI_MODEL_ILP32: 28859 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28860 return (EFAULT); 28861 } 28862 /* Convert the ILP32 uscsi data from the application to LP64 */ 28863 cdrom_read32tocdrom_read(cdrd32, mode2); 28864 break; 28865 case DDI_MODEL_NONE: 28866 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28867 return (EFAULT); 28868 } 28869 break; 28870 } 28871 28872 #else /* ! _MULTI_DATAMODEL */ 28873 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28874 return (EFAULT); 28875 } 28876 #endif /* _MULTI_DATAMODEL */ 28877 28878 bzero(cdb, sizeof (cdb)); 28879 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28880 /* Read command supported by 1st generation atapi drives */ 28881 cdb[0] = SCMD_READ_CDD4; 28882 } else { 28883 /* Universal CD Access Command */ 28884 cdb[0] = SCMD_READ_CD; 28885 } 28886 28887 /* 28888 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28889 */ 28890 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28891 28892 /* set the start address */ 28893 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28894 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28895 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28896 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28897 28898 /* set the transfer length */ 28899 nblocks = mode2->cdread_buflen / 2336; 28900 cdb[6] = (uchar_t)(nblocks >> 16); 28901 cdb[7] = (uchar_t)(nblocks >> 8); 28902 cdb[8] = (uchar_t)nblocks; 28903 28904 /* set the filter bits */ 28905 cdb[9] = CDROM_READ_CD_USERDATA; 28906 28907 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28908 com->uscsi_cdb = (caddr_t)cdb; 28909 com->uscsi_cdblen = sizeof (cdb); 28910 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28911 com->uscsi_buflen = mode2->cdread_buflen; 28912 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28913 28914 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28915 UIO_SYSSPACE, SD_PATH_STANDARD); 28916 kmem_free(com, sizeof (*com)); 28917 return (rval); 28918 } 28919 28920 28921 /* 28922 * Function: sr_read_mode2() 28923 * 28924 * Description: This routine is the driver entry point for handling CD-ROM 28925 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28926 * do not support the READ CD (0xBE) command. 28927 * 28928 * Arguments: dev - the device 'dev_t' 28929 * data - pointer to user provided cd read structure specifying 28930 * the lba buffer address and length. 28931 * flag - this argument is a pass through to ddi_copyxxx() 28932 * directly from the mode argument of ioctl(). 28933 * 28934 * Return Code: the code returned by sd_send_scsi_cmd() 28935 * EFAULT if ddi_copyxxx() fails 28936 * ENXIO if fail ddi_get_soft_state 28937 * EINVAL if data pointer is NULL 28938 * EIO if fail to reset block size 28939 * EAGAIN if commands are in progress in the driver 28940 */ 28941 28942 static int 28943 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28944 { 28945 struct sd_lun *un; 28946 struct cdrom_read mode2_struct; 28947 struct cdrom_read *mode2 = &mode2_struct; 28948 int rval; 28949 uint32_t restore_blksize; 28950 struct uscsi_cmd *com; 28951 uchar_t cdb[CDB_GROUP0]; 28952 int nblocks; 28953 28954 #ifdef _MULTI_DATAMODEL 28955 /* To support ILP32 applications in an LP64 world */ 28956 struct cdrom_read32 cdrom_read32; 28957 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28958 #endif /* _MULTI_DATAMODEL */ 28959 28960 if (data == NULL) { 28961 return (EINVAL); 28962 } 28963 28964 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28965 (un->un_state == SD_STATE_OFFLINE)) { 28966 return (ENXIO); 28967 } 28968 28969 /* 28970 * Because this routine will update the device and driver block size 28971 * being used we want to make sure there are no commands in progress. 28972 * If commands are in progress the user will have to try again. 28973 * 28974 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28975 * in sdioctl to protect commands from sdioctl through to the top of 28976 * sd_uscsi_strategy. See sdioctl for details. 28977 */ 28978 mutex_enter(SD_MUTEX(un)); 28979 if (un->un_ncmds_in_driver != 1) { 28980 mutex_exit(SD_MUTEX(un)); 28981 return (EAGAIN); 28982 } 28983 mutex_exit(SD_MUTEX(un)); 28984 28985 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28986 "sd_read_mode2: entry: un:0x%p\n", un); 28987 28988 #ifdef _MULTI_DATAMODEL 28989 switch (ddi_model_convert_from(flag & FMODELS)) { 28990 case DDI_MODEL_ILP32: 28991 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28992 return (EFAULT); 28993 } 28994 /* Convert the ILP32 uscsi data from the application to LP64 */ 28995 cdrom_read32tocdrom_read(cdrd32, mode2); 28996 break; 28997 case DDI_MODEL_NONE: 28998 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28999 return (EFAULT); 29000 } 29001 break; 29002 } 29003 #else /* ! _MULTI_DATAMODEL */ 29004 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 29005 return (EFAULT); 29006 } 29007 #endif /* _MULTI_DATAMODEL */ 29008 29009 /* Store the current target block size for restoration later */ 29010 restore_blksize = un->un_tgt_blocksize; 29011 29012 /* Change the device and soft state target block size to 2336 */ 29013 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 29014 rval = EIO; 29015 goto done; 29016 } 29017 29018 29019 bzero(cdb, sizeof (cdb)); 29020 29021 /* set READ operation */ 29022 cdb[0] = SCMD_READ; 29023 29024 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 29025 mode2->cdread_lba >>= 2; 29026 29027 /* set the start address */ 29028 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 29029 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 29030 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 29031 29032 /* set the transfer length */ 29033 nblocks = mode2->cdread_buflen / 2336; 29034 cdb[4] = (uchar_t)nblocks & 0xFF; 29035 29036 /* build command */ 29037 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29038 com->uscsi_cdb = (caddr_t)cdb; 29039 com->uscsi_cdblen = sizeof (cdb); 29040 com->uscsi_bufaddr = mode2->cdread_bufaddr; 29041 com->uscsi_buflen = mode2->cdread_buflen; 29042 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29043 29044 /* 29045 * Issue SCSI command with user space address for read buffer. 29046 * 29047 * This sends the command through main channel in the driver. 29048 * 29049 * Since this is accessed via an IOCTL call, we go through the 29050 * standard path, so that if the device was powered down, then 29051 * it would be 'awakened' to handle the command. 29052 */ 29053 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29054 UIO_SYSSPACE, SD_PATH_STANDARD); 29055 29056 kmem_free(com, sizeof (*com)); 29057 29058 /* Restore the device and soft state target block size */ 29059 if (sr_sector_mode(dev, restore_blksize) != 0) { 29060 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29061 "can't do switch back to mode 1\n"); 29062 /* 29063 * If sd_send_scsi_READ succeeded we still need to report 29064 * an error because we failed to reset the block size 29065 */ 29066 if (rval == 0) { 29067 rval = EIO; 29068 } 29069 } 29070 29071 done: 29072 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 29073 "sd_read_mode2: exit: un:0x%p\n", un); 29074 29075 return (rval); 29076 } 29077 29078 29079 /* 29080 * Function: sr_sector_mode() 29081 * 29082 * Description: This utility function is used by sr_read_mode2 to set the target 29083 * block size based on the user specified size. This is a legacy 29084 * implementation based upon a vendor specific mode page 29085 * 29086 * Arguments: dev - the device 'dev_t' 29087 * data - flag indicating if block size is being set to 2336 or 29088 * 512. 29089 * 29090 * Return Code: the code returned by sd_send_scsi_cmd() 29091 * EFAULT if ddi_copyxxx() fails 29092 * ENXIO if fail ddi_get_soft_state 29093 * EINVAL if data pointer is NULL 29094 */ 29095 29096 static int 29097 sr_sector_mode(dev_t dev, uint32_t blksize) 29098 { 29099 struct sd_lun *un; 29100 uchar_t *sense; 29101 uchar_t *select; 29102 int rval; 29103 29104 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29105 (un->un_state == SD_STATE_OFFLINE)) { 29106 return (ENXIO); 29107 } 29108 29109 sense = kmem_zalloc(20, KM_SLEEP); 29110 29111 /* Note: This is a vendor specific mode page (0x81) */ 29112 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 29113 SD_PATH_STANDARD)) != 0) { 29114 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29115 "sr_sector_mode: Mode Sense failed\n"); 29116 kmem_free(sense, 20); 29117 return (rval); 29118 } 29119 select = kmem_zalloc(20, KM_SLEEP); 29120 select[3] = 0x08; 29121 select[10] = ((blksize >> 8) & 0xff); 29122 select[11] = (blksize & 0xff); 29123 select[12] = 0x01; 29124 select[13] = 0x06; 29125 select[14] = sense[14]; 29126 select[15] = sense[15]; 29127 if (blksize == SD_MODE2_BLKSIZE) { 29128 select[14] |= 0x01; 29129 } 29130 29131 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 29132 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 29133 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29134 "sr_sector_mode: Mode Select failed\n"); 29135 } else { 29136 /* 29137 * Only update the softstate block size if we successfully 29138 * changed the device block mode. 29139 */ 29140 mutex_enter(SD_MUTEX(un)); 29141 sd_update_block_info(un, blksize, 0); 29142 mutex_exit(SD_MUTEX(un)); 29143 } 29144 kmem_free(sense, 20); 29145 kmem_free(select, 20); 29146 return (rval); 29147 } 29148 29149 29150 /* 29151 * Function: sr_read_cdda() 29152 * 29153 * Description: This routine is the driver entry point for handling CD-ROM 29154 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 29155 * the target supports CDDA these requests are handled via a vendor 29156 * specific command (0xD8) If the target does not support CDDA 29157 * these requests are handled via the READ CD command (0xBE). 29158 * 29159 * Arguments: dev - the device 'dev_t' 29160 * data - pointer to user provided CD-DA structure specifying 29161 * the track starting address, transfer length, and 29162 * subcode options. 29163 * flag - this argument is a pass through to ddi_copyxxx() 29164 * directly from the mode argument of ioctl(). 29165 * 29166 * Return Code: the code returned by sd_send_scsi_cmd() 29167 * EFAULT if ddi_copyxxx() fails 29168 * ENXIO if fail ddi_get_soft_state 29169 * EINVAL if invalid arguments are provided 29170 * ENOTTY 29171 */ 29172 29173 static int 29174 sr_read_cdda(dev_t dev, caddr_t data, int flag) 29175 { 29176 struct sd_lun *un; 29177 struct uscsi_cmd *com; 29178 struct cdrom_cdda *cdda; 29179 int rval; 29180 size_t buflen; 29181 char cdb[CDB_GROUP5]; 29182 29183 #ifdef _MULTI_DATAMODEL 29184 /* To support ILP32 applications in an LP64 world */ 29185 struct cdrom_cdda32 cdrom_cdda32; 29186 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 29187 #endif /* _MULTI_DATAMODEL */ 29188 29189 if (data == NULL) { 29190 return (EINVAL); 29191 } 29192 29193 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29194 return (ENXIO); 29195 } 29196 29197 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 29198 29199 #ifdef _MULTI_DATAMODEL 29200 switch (ddi_model_convert_from(flag & FMODELS)) { 29201 case DDI_MODEL_ILP32: 29202 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 29203 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29204 "sr_read_cdda: ddi_copyin Failed\n"); 29205 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29206 return (EFAULT); 29207 } 29208 /* Convert the ILP32 uscsi data from the application to LP64 */ 29209 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 29210 break; 29211 case DDI_MODEL_NONE: 29212 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29213 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29214 "sr_read_cdda: ddi_copyin Failed\n"); 29215 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29216 return (EFAULT); 29217 } 29218 break; 29219 } 29220 #else /* ! _MULTI_DATAMODEL */ 29221 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29222 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29223 "sr_read_cdda: ddi_copyin Failed\n"); 29224 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29225 return (EFAULT); 29226 } 29227 #endif /* _MULTI_DATAMODEL */ 29228 29229 /* 29230 * Since MMC-2 expects max 3 bytes for length, check if the 29231 * length input is greater than 3 bytes 29232 */ 29233 if ((cdda->cdda_length & 0xFF000000) != 0) { 29234 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 29235 "cdrom transfer length too large: %d (limit %d)\n", 29236 cdda->cdda_length, 0xFFFFFF); 29237 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29238 return (EINVAL); 29239 } 29240 29241 switch (cdda->cdda_subcode) { 29242 case CDROM_DA_NO_SUBCODE: 29243 buflen = CDROM_BLK_2352 * cdda->cdda_length; 29244 break; 29245 case CDROM_DA_SUBQ: 29246 buflen = CDROM_BLK_2368 * cdda->cdda_length; 29247 break; 29248 case CDROM_DA_ALL_SUBCODE: 29249 buflen = CDROM_BLK_2448 * cdda->cdda_length; 29250 break; 29251 case CDROM_DA_SUBCODE_ONLY: 29252 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 29253 break; 29254 default: 29255 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29256 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 29257 cdda->cdda_subcode); 29258 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29259 return (EINVAL); 29260 } 29261 29262 /* Build and send the command */ 29263 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29264 bzero(cdb, CDB_GROUP5); 29265 29266 if (un->un_f_cfg_cdda == TRUE) { 29267 cdb[0] = (char)SCMD_READ_CD; 29268 cdb[1] = 0x04; 29269 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29270 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29271 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29272 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29273 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29274 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29275 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 29276 cdb[9] = 0x10; 29277 switch (cdda->cdda_subcode) { 29278 case CDROM_DA_NO_SUBCODE : 29279 cdb[10] = 0x0; 29280 break; 29281 case CDROM_DA_SUBQ : 29282 cdb[10] = 0x2; 29283 break; 29284 case CDROM_DA_ALL_SUBCODE : 29285 cdb[10] = 0x1; 29286 break; 29287 case CDROM_DA_SUBCODE_ONLY : 29288 /* FALLTHROUGH */ 29289 default : 29290 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29291 kmem_free(com, sizeof (*com)); 29292 return (ENOTTY); 29293 } 29294 } else { 29295 cdb[0] = (char)SCMD_READ_CDDA; 29296 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29297 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29298 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29299 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29300 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 29301 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29302 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29303 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 29304 cdb[10] = cdda->cdda_subcode; 29305 } 29306 29307 com->uscsi_cdb = cdb; 29308 com->uscsi_cdblen = CDB_GROUP5; 29309 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 29310 com->uscsi_buflen = buflen; 29311 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29312 29313 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29314 UIO_SYSSPACE, SD_PATH_STANDARD); 29315 29316 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29317 kmem_free(com, sizeof (*com)); 29318 return (rval); 29319 } 29320 29321 29322 /* 29323 * Function: sr_read_cdxa() 29324 * 29325 * Description: This routine is the driver entry point for handling CD-ROM 29326 * ioctl requests to return CD-XA (Extended Architecture) data. 29327 * (CDROMCDXA). 29328 * 29329 * Arguments: dev - the device 'dev_t' 29330 * data - pointer to user provided CD-XA structure specifying 29331 * the data starting address, transfer length, and format 29332 * flag - this argument is a pass through to ddi_copyxxx() 29333 * directly from the mode argument of ioctl(). 29334 * 29335 * Return Code: the code returned by sd_send_scsi_cmd() 29336 * EFAULT if ddi_copyxxx() fails 29337 * ENXIO if fail ddi_get_soft_state 29338 * EINVAL if data pointer is NULL 29339 */ 29340 29341 static int 29342 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 29343 { 29344 struct sd_lun *un; 29345 struct uscsi_cmd *com; 29346 struct cdrom_cdxa *cdxa; 29347 int rval; 29348 size_t buflen; 29349 char cdb[CDB_GROUP5]; 29350 uchar_t read_flags; 29351 29352 #ifdef _MULTI_DATAMODEL 29353 /* To support ILP32 applications in an LP64 world */ 29354 struct cdrom_cdxa32 cdrom_cdxa32; 29355 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 29356 #endif /* _MULTI_DATAMODEL */ 29357 29358 if (data == NULL) { 29359 return (EINVAL); 29360 } 29361 29362 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29363 return (ENXIO); 29364 } 29365 29366 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 29367 29368 #ifdef _MULTI_DATAMODEL 29369 switch (ddi_model_convert_from(flag & FMODELS)) { 29370 case DDI_MODEL_ILP32: 29371 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 29372 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29373 return (EFAULT); 29374 } 29375 /* 29376 * Convert the ILP32 uscsi data from the 29377 * application to LP64 for internal use. 29378 */ 29379 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 29380 break; 29381 case DDI_MODEL_NONE: 29382 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29383 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29384 return (EFAULT); 29385 } 29386 break; 29387 } 29388 #else /* ! _MULTI_DATAMODEL */ 29389 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29390 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29391 return (EFAULT); 29392 } 29393 #endif /* _MULTI_DATAMODEL */ 29394 29395 /* 29396 * Since MMC-2 expects max 3 bytes for length, check if the 29397 * length input is greater than 3 bytes 29398 */ 29399 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 29400 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 29401 "cdrom transfer length too large: %d (limit %d)\n", 29402 cdxa->cdxa_length, 0xFFFFFF); 29403 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29404 return (EINVAL); 29405 } 29406 29407 switch (cdxa->cdxa_format) { 29408 case CDROM_XA_DATA: 29409 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 29410 read_flags = 0x10; 29411 break; 29412 case CDROM_XA_SECTOR_DATA: 29413 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 29414 read_flags = 0xf8; 29415 break; 29416 case CDROM_XA_DATA_W_ERROR: 29417 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 29418 read_flags = 0xfc; 29419 break; 29420 default: 29421 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29422 "sr_read_cdxa: Format '0x%x' Not Supported\n", 29423 cdxa->cdxa_format); 29424 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29425 return (EINVAL); 29426 } 29427 29428 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29429 bzero(cdb, CDB_GROUP5); 29430 if (un->un_f_mmc_cap == TRUE) { 29431 cdb[0] = (char)SCMD_READ_CD; 29432 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29433 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29434 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29435 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29436 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29437 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29438 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 29439 cdb[9] = (char)read_flags; 29440 } else { 29441 /* 29442 * Note: A vendor specific command (0xDB) is being used her to 29443 * request a read of all subcodes. 29444 */ 29445 cdb[0] = (char)SCMD_READ_CDXA; 29446 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29447 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29448 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29449 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29450 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 29451 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29452 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29453 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 29454 cdb[10] = cdxa->cdxa_format; 29455 } 29456 com->uscsi_cdb = cdb; 29457 com->uscsi_cdblen = CDB_GROUP5; 29458 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 29459 com->uscsi_buflen = buflen; 29460 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29461 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29462 UIO_SYSSPACE, SD_PATH_STANDARD); 29463 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29464 kmem_free(com, sizeof (*com)); 29465 return (rval); 29466 } 29467 29468 29469 /* 29470 * Function: sr_eject() 29471 * 29472 * Description: This routine is the driver entry point for handling CD-ROM 29473 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 29474 * 29475 * Arguments: dev - the device 'dev_t' 29476 * 29477 * Return Code: the code returned by sd_send_scsi_cmd() 29478 */ 29479 29480 static int 29481 sr_eject(dev_t dev) 29482 { 29483 struct sd_lun *un; 29484 int rval; 29485 29486 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29487 (un->un_state == SD_STATE_OFFLINE)) { 29488 return (ENXIO); 29489 } 29490 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29491 SD_PATH_STANDARD)) != 0) { 29492 return (rval); 29493 } 29494 29495 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29496 SD_PATH_STANDARD); 29497 29498 if (rval == 0) { 29499 mutex_enter(SD_MUTEX(un)); 29500 sr_ejected(un); 29501 un->un_mediastate = DKIO_EJECTED; 29502 cv_broadcast(&un->un_state_cv); 29503 mutex_exit(SD_MUTEX(un)); 29504 } 29505 return (rval); 29506 } 29507 29508 29509 /* 29510 * Function: sr_ejected() 29511 * 29512 * Description: This routine updates the soft state structure to invalidate the 29513 * geometry information after the media has been ejected or a 29514 * media eject has been detected. 29515 * 29516 * Arguments: un - driver soft state (unit) structure 29517 */ 29518 29519 static void 29520 sr_ejected(struct sd_lun *un) 29521 { 29522 struct sd_errstats *stp; 29523 29524 ASSERT(un != NULL); 29525 ASSERT(mutex_owned(SD_MUTEX(un))); 29526 29527 un->un_f_blockcount_is_valid = FALSE; 29528 un->un_f_tgt_blocksize_is_valid = FALSE; 29529 un->un_f_geometry_is_valid = FALSE; 29530 29531 if (un->un_errstats != NULL) { 29532 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29533 stp->sd_capacity.value.ui64 = 0; 29534 } 29535 } 29536 29537 29538 /* 29539 * Function: sr_check_wp() 29540 * 29541 * Description: This routine checks the write protection of a removable 29542 * media disk and hotpluggable devices via the write protect bit of 29543 * the Mode Page Header device specific field. Some devices choke 29544 * on unsupported mode page. In order to workaround this issue, 29545 * this routine has been implemented to use 0x3f mode page(request 29546 * for all pages) for all device types. 29547 * 29548 * Arguments: dev - the device 'dev_t' 29549 * 29550 * Return Code: int indicating if the device is write protected (1) or not (0) 29551 * 29552 * Context: Kernel thread. 29553 * 29554 */ 29555 29556 static int 29557 sr_check_wp(dev_t dev) 29558 { 29559 struct sd_lun *un; 29560 uchar_t device_specific; 29561 uchar_t *sense; 29562 int hdrlen; 29563 int rval = FALSE; 29564 29565 /* 29566 * Note: The return codes for this routine should be reworked to 29567 * properly handle the case of a NULL softstate. 29568 */ 29569 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29570 return (FALSE); 29571 } 29572 29573 if (un->un_f_cfg_is_atapi == TRUE) { 29574 /* 29575 * The mode page contents are not required; set the allocation 29576 * length for the mode page header only 29577 */ 29578 hdrlen = MODE_HEADER_LENGTH_GRP2; 29579 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29580 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29581 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29582 goto err_exit; 29583 device_specific = 29584 ((struct mode_header_grp2 *)sense)->device_specific; 29585 } else { 29586 hdrlen = MODE_HEADER_LENGTH; 29587 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29588 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29589 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29590 goto err_exit; 29591 device_specific = 29592 ((struct mode_header *)sense)->device_specific; 29593 } 29594 29595 /* 29596 * Write protect mode sense failed; not all disks 29597 * understand this query. Return FALSE assuming that 29598 * these devices are not writable. 29599 */ 29600 if (device_specific & WRITE_PROTECT) { 29601 rval = TRUE; 29602 } 29603 29604 err_exit: 29605 kmem_free(sense, hdrlen); 29606 return (rval); 29607 } 29608 29609 /* 29610 * Function: sr_volume_ctrl() 29611 * 29612 * Description: This routine is the driver entry point for handling CD-ROM 29613 * audio output volume ioctl requests. (CDROMVOLCTRL) 29614 * 29615 * Arguments: dev - the device 'dev_t' 29616 * data - pointer to user audio volume control structure 29617 * flag - this argument is a pass through to ddi_copyxxx() 29618 * directly from the mode argument of ioctl(). 29619 * 29620 * Return Code: the code returned by sd_send_scsi_cmd() 29621 * EFAULT if ddi_copyxxx() fails 29622 * ENXIO if fail ddi_get_soft_state 29623 * EINVAL if data pointer is NULL 29624 * 29625 */ 29626 29627 static int 29628 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29629 { 29630 struct sd_lun *un; 29631 struct cdrom_volctrl volume; 29632 struct cdrom_volctrl *vol = &volume; 29633 uchar_t *sense_page; 29634 uchar_t *select_page; 29635 uchar_t *sense; 29636 uchar_t *select; 29637 int sense_buflen; 29638 int select_buflen; 29639 int rval; 29640 29641 if (data == NULL) { 29642 return (EINVAL); 29643 } 29644 29645 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29646 (un->un_state == SD_STATE_OFFLINE)) { 29647 return (ENXIO); 29648 } 29649 29650 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29651 return (EFAULT); 29652 } 29653 29654 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29655 struct mode_header_grp2 *sense_mhp; 29656 struct mode_header_grp2 *select_mhp; 29657 int bd_len; 29658 29659 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29660 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29661 MODEPAGE_AUDIO_CTRL_LEN; 29662 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29663 select = kmem_zalloc(select_buflen, KM_SLEEP); 29664 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29665 sense_buflen, MODEPAGE_AUDIO_CTRL, 29666 SD_PATH_STANDARD)) != 0) { 29667 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29668 "sr_volume_ctrl: Mode Sense Failed\n"); 29669 kmem_free(sense, sense_buflen); 29670 kmem_free(select, select_buflen); 29671 return (rval); 29672 } 29673 sense_mhp = (struct mode_header_grp2 *)sense; 29674 select_mhp = (struct mode_header_grp2 *)select; 29675 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29676 sense_mhp->bdesc_length_lo; 29677 if (bd_len > MODE_BLK_DESC_LENGTH) { 29678 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29679 "sr_volume_ctrl: Mode Sense returned invalid " 29680 "block descriptor length\n"); 29681 kmem_free(sense, sense_buflen); 29682 kmem_free(select, select_buflen); 29683 return (EIO); 29684 } 29685 sense_page = (uchar_t *) 29686 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29687 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29688 select_mhp->length_msb = 0; 29689 select_mhp->length_lsb = 0; 29690 select_mhp->bdesc_length_hi = 0; 29691 select_mhp->bdesc_length_lo = 0; 29692 } else { 29693 struct mode_header *sense_mhp, *select_mhp; 29694 29695 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29696 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29697 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29698 select = kmem_zalloc(select_buflen, KM_SLEEP); 29699 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29700 sense_buflen, MODEPAGE_AUDIO_CTRL, 29701 SD_PATH_STANDARD)) != 0) { 29702 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29703 "sr_volume_ctrl: Mode Sense Failed\n"); 29704 kmem_free(sense, sense_buflen); 29705 kmem_free(select, select_buflen); 29706 return (rval); 29707 } 29708 sense_mhp = (struct mode_header *)sense; 29709 select_mhp = (struct mode_header *)select; 29710 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29711 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29712 "sr_volume_ctrl: Mode Sense returned invalid " 29713 "block descriptor length\n"); 29714 kmem_free(sense, sense_buflen); 29715 kmem_free(select, select_buflen); 29716 return (EIO); 29717 } 29718 sense_page = (uchar_t *) 29719 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29720 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29721 select_mhp->length = 0; 29722 select_mhp->bdesc_length = 0; 29723 } 29724 /* 29725 * Note: An audio control data structure could be created and overlayed 29726 * on the following in place of the array indexing method implemented. 29727 */ 29728 29729 /* Build the select data for the user volume data */ 29730 select_page[0] = MODEPAGE_AUDIO_CTRL; 29731 select_page[1] = 0xE; 29732 /* Set the immediate bit */ 29733 select_page[2] = 0x04; 29734 /* Zero out reserved fields */ 29735 select_page[3] = 0x00; 29736 select_page[4] = 0x00; 29737 /* Return sense data for fields not to be modified */ 29738 select_page[5] = sense_page[5]; 29739 select_page[6] = sense_page[6]; 29740 select_page[7] = sense_page[7]; 29741 /* Set the user specified volume levels for channel 0 and 1 */ 29742 select_page[8] = 0x01; 29743 select_page[9] = vol->channel0; 29744 select_page[10] = 0x02; 29745 select_page[11] = vol->channel1; 29746 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29747 select_page[12] = sense_page[12]; 29748 select_page[13] = sense_page[13]; 29749 select_page[14] = sense_page[14]; 29750 select_page[15] = sense_page[15]; 29751 29752 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29753 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29754 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29755 } else { 29756 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29757 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29758 } 29759 29760 kmem_free(sense, sense_buflen); 29761 kmem_free(select, select_buflen); 29762 return (rval); 29763 } 29764 29765 29766 /* 29767 * Function: sr_read_sony_session_offset() 29768 * 29769 * Description: This routine is the driver entry point for handling CD-ROM 29770 * ioctl requests for session offset information. (CDROMREADOFFSET) 29771 * The address of the first track in the last session of a 29772 * multi-session CD-ROM is returned 29773 * 29774 * Note: This routine uses a vendor specific key value in the 29775 * command control field without implementing any vendor check here 29776 * or in the ioctl routine. 29777 * 29778 * Arguments: dev - the device 'dev_t' 29779 * data - pointer to an int to hold the requested address 29780 * flag - this argument is a pass through to ddi_copyxxx() 29781 * directly from the mode argument of ioctl(). 29782 * 29783 * Return Code: the code returned by sd_send_scsi_cmd() 29784 * EFAULT if ddi_copyxxx() fails 29785 * ENXIO if fail ddi_get_soft_state 29786 * EINVAL if data pointer is NULL 29787 */ 29788 29789 static int 29790 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29791 { 29792 struct sd_lun *un; 29793 struct uscsi_cmd *com; 29794 caddr_t buffer; 29795 char cdb[CDB_GROUP1]; 29796 int session_offset = 0; 29797 int rval; 29798 29799 if (data == NULL) { 29800 return (EINVAL); 29801 } 29802 29803 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29804 (un->un_state == SD_STATE_OFFLINE)) { 29805 return (ENXIO); 29806 } 29807 29808 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29809 bzero(cdb, CDB_GROUP1); 29810 cdb[0] = SCMD_READ_TOC; 29811 /* 29812 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29813 * (4 byte TOC response header + 8 byte response data) 29814 */ 29815 cdb[8] = SONY_SESSION_OFFSET_LEN; 29816 /* Byte 9 is the control byte. A vendor specific value is used */ 29817 cdb[9] = SONY_SESSION_OFFSET_KEY; 29818 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29819 com->uscsi_cdb = cdb; 29820 com->uscsi_cdblen = CDB_GROUP1; 29821 com->uscsi_bufaddr = buffer; 29822 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29823 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29824 29825 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29826 UIO_SYSSPACE, SD_PATH_STANDARD); 29827 if (rval != 0) { 29828 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29829 kmem_free(com, sizeof (*com)); 29830 return (rval); 29831 } 29832 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29833 session_offset = 29834 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29835 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29836 /* 29837 * Offset returned offset in current lbasize block's. Convert to 29838 * 2k block's to return to the user 29839 */ 29840 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29841 session_offset >>= 2; 29842 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29843 session_offset >>= 1; 29844 } 29845 } 29846 29847 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29848 rval = EFAULT; 29849 } 29850 29851 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29852 kmem_free(com, sizeof (*com)); 29853 return (rval); 29854 } 29855 29856 29857 /* 29858 * Function: sd_wm_cache_constructor() 29859 * 29860 * Description: Cache Constructor for the wmap cache for the read/modify/write 29861 * devices. 29862 * 29863 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29864 * un - sd_lun structure for the device. 29865 * flag - the km flags passed to constructor 29866 * 29867 * Return Code: 0 on success. 29868 * -1 on failure. 29869 */ 29870 29871 /*ARGSUSED*/ 29872 static int 29873 sd_wm_cache_constructor(void *wm, void *un, int flags) 29874 { 29875 bzero(wm, sizeof (struct sd_w_map)); 29876 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29877 return (0); 29878 } 29879 29880 29881 /* 29882 * Function: sd_wm_cache_destructor() 29883 * 29884 * Description: Cache destructor for the wmap cache for the read/modify/write 29885 * devices. 29886 * 29887 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29888 * un - sd_lun structure for the device. 29889 */ 29890 /*ARGSUSED*/ 29891 static void 29892 sd_wm_cache_destructor(void *wm, void *un) 29893 { 29894 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29895 } 29896 29897 29898 /* 29899 * Function: sd_range_lock() 29900 * 29901 * Description: Lock the range of blocks specified as parameter to ensure 29902 * that read, modify write is atomic and no other i/o writes 29903 * to the same location. The range is specified in terms 29904 * of start and end blocks. Block numbers are the actual 29905 * media block numbers and not system. 29906 * 29907 * Arguments: un - sd_lun structure for the device. 29908 * startb - The starting block number 29909 * endb - The end block number 29910 * typ - type of i/o - simple/read_modify_write 29911 * 29912 * Return Code: wm - pointer to the wmap structure. 29913 * 29914 * Context: This routine can sleep. 29915 */ 29916 29917 static struct sd_w_map * 29918 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29919 { 29920 struct sd_w_map *wmp = NULL; 29921 struct sd_w_map *sl_wmp = NULL; 29922 struct sd_w_map *tmp_wmp; 29923 wm_state state = SD_WM_CHK_LIST; 29924 29925 29926 ASSERT(un != NULL); 29927 ASSERT(!mutex_owned(SD_MUTEX(un))); 29928 29929 mutex_enter(SD_MUTEX(un)); 29930 29931 while (state != SD_WM_DONE) { 29932 29933 switch (state) { 29934 case SD_WM_CHK_LIST: 29935 /* 29936 * This is the starting state. Check the wmap list 29937 * to see if the range is currently available. 29938 */ 29939 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29940 /* 29941 * If this is a simple write and no rmw 29942 * i/o is pending then try to lock the 29943 * range as the range should be available. 29944 */ 29945 state = SD_WM_LOCK_RANGE; 29946 } else { 29947 tmp_wmp = sd_get_range(un, startb, endb); 29948 if (tmp_wmp != NULL) { 29949 if ((wmp != NULL) && ONLIST(un, wmp)) { 29950 /* 29951 * Should not keep onlist wmps 29952 * while waiting this macro 29953 * will also do wmp = NULL; 29954 */ 29955 FREE_ONLIST_WMAP(un, wmp); 29956 } 29957 /* 29958 * sl_wmp is the wmap on which wait 29959 * is done, since the tmp_wmp points 29960 * to the inuse wmap, set sl_wmp to 29961 * tmp_wmp and change the state to sleep 29962 */ 29963 sl_wmp = tmp_wmp; 29964 state = SD_WM_WAIT_MAP; 29965 } else { 29966 state = SD_WM_LOCK_RANGE; 29967 } 29968 29969 } 29970 break; 29971 29972 case SD_WM_LOCK_RANGE: 29973 ASSERT(un->un_wm_cache); 29974 /* 29975 * The range need to be locked, try to get a wmap. 29976 * First attempt it with NO_SLEEP, want to avoid a sleep 29977 * if possible as we will have to release the sd mutex 29978 * if we have to sleep. 29979 */ 29980 if (wmp == NULL) 29981 wmp = kmem_cache_alloc(un->un_wm_cache, 29982 KM_NOSLEEP); 29983 if (wmp == NULL) { 29984 mutex_exit(SD_MUTEX(un)); 29985 _NOTE(DATA_READABLE_WITHOUT_LOCK 29986 (sd_lun::un_wm_cache)) 29987 wmp = kmem_cache_alloc(un->un_wm_cache, 29988 KM_SLEEP); 29989 mutex_enter(SD_MUTEX(un)); 29990 /* 29991 * we released the mutex so recheck and go to 29992 * check list state. 29993 */ 29994 state = SD_WM_CHK_LIST; 29995 } else { 29996 /* 29997 * We exit out of state machine since we 29998 * have the wmap. Do the housekeeping first. 29999 * place the wmap on the wmap list if it is not 30000 * on it already and then set the state to done. 30001 */ 30002 wmp->wm_start = startb; 30003 wmp->wm_end = endb; 30004 wmp->wm_flags = typ | SD_WM_BUSY; 30005 if (typ & SD_WTYPE_RMW) { 30006 un->un_rmw_count++; 30007 } 30008 /* 30009 * If not already on the list then link 30010 */ 30011 if (!ONLIST(un, wmp)) { 30012 wmp->wm_next = un->un_wm; 30013 wmp->wm_prev = NULL; 30014 if (wmp->wm_next) 30015 wmp->wm_next->wm_prev = wmp; 30016 un->un_wm = wmp; 30017 } 30018 state = SD_WM_DONE; 30019 } 30020 break; 30021 30022 case SD_WM_WAIT_MAP: 30023 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 30024 /* 30025 * Wait is done on sl_wmp, which is set in the 30026 * check_list state. 30027 */ 30028 sl_wmp->wm_wanted_count++; 30029 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 30030 sl_wmp->wm_wanted_count--; 30031 /* 30032 * We can reuse the memory from the completed sl_wmp 30033 * lock range for our new lock, but only if noone is 30034 * waiting for it. 30035 */ 30036 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 30037 if (sl_wmp->wm_wanted_count == 0) { 30038 if (wmp != NULL) 30039 CHK_N_FREEWMP(un, wmp); 30040 wmp = sl_wmp; 30041 } 30042 sl_wmp = NULL; 30043 /* 30044 * After waking up, need to recheck for availability of 30045 * range. 30046 */ 30047 state = SD_WM_CHK_LIST; 30048 break; 30049 30050 default: 30051 panic("sd_range_lock: " 30052 "Unknown state %d in sd_range_lock", state); 30053 /*NOTREACHED*/ 30054 } /* switch(state) */ 30055 30056 } /* while(state != SD_WM_DONE) */ 30057 30058 mutex_exit(SD_MUTEX(un)); 30059 30060 ASSERT(wmp != NULL); 30061 30062 return (wmp); 30063 } 30064 30065 30066 /* 30067 * Function: sd_get_range() 30068 * 30069 * Description: Find if there any overlapping I/O to this one 30070 * Returns the write-map of 1st such I/O, NULL otherwise. 30071 * 30072 * Arguments: un - sd_lun structure for the device. 30073 * startb - The starting block number 30074 * endb - The end block number 30075 * 30076 * Return Code: wm - pointer to the wmap structure. 30077 */ 30078 30079 static struct sd_w_map * 30080 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 30081 { 30082 struct sd_w_map *wmp; 30083 30084 ASSERT(un != NULL); 30085 30086 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 30087 if (!(wmp->wm_flags & SD_WM_BUSY)) { 30088 continue; 30089 } 30090 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 30091 break; 30092 } 30093 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 30094 break; 30095 } 30096 } 30097 30098 return (wmp); 30099 } 30100 30101 30102 /* 30103 * Function: sd_free_inlist_wmap() 30104 * 30105 * Description: Unlink and free a write map struct. 30106 * 30107 * Arguments: un - sd_lun structure for the device. 30108 * wmp - sd_w_map which needs to be unlinked. 30109 */ 30110 30111 static void 30112 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 30113 { 30114 ASSERT(un != NULL); 30115 30116 if (un->un_wm == wmp) { 30117 un->un_wm = wmp->wm_next; 30118 } else { 30119 wmp->wm_prev->wm_next = wmp->wm_next; 30120 } 30121 30122 if (wmp->wm_next) { 30123 wmp->wm_next->wm_prev = wmp->wm_prev; 30124 } 30125 30126 wmp->wm_next = wmp->wm_prev = NULL; 30127 30128 kmem_cache_free(un->un_wm_cache, wmp); 30129 } 30130 30131 30132 /* 30133 * Function: sd_range_unlock() 30134 * 30135 * Description: Unlock the range locked by wm. 30136 * Free write map if nobody else is waiting on it. 30137 * 30138 * Arguments: un - sd_lun structure for the device. 30139 * wmp - sd_w_map which needs to be unlinked. 30140 */ 30141 30142 static void 30143 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 30144 { 30145 ASSERT(un != NULL); 30146 ASSERT(wm != NULL); 30147 ASSERT(!mutex_owned(SD_MUTEX(un))); 30148 30149 mutex_enter(SD_MUTEX(un)); 30150 30151 if (wm->wm_flags & SD_WTYPE_RMW) { 30152 un->un_rmw_count--; 30153 } 30154 30155 if (wm->wm_wanted_count) { 30156 wm->wm_flags = 0; 30157 /* 30158 * Broadcast that the wmap is available now. 30159 */ 30160 cv_broadcast(&wm->wm_avail); 30161 } else { 30162 /* 30163 * If no one is waiting on the map, it should be free'ed. 30164 */ 30165 sd_free_inlist_wmap(un, wm); 30166 } 30167 30168 mutex_exit(SD_MUTEX(un)); 30169 } 30170 30171 30172 /* 30173 * Function: sd_read_modify_write_task 30174 * 30175 * Description: Called from a taskq thread to initiate the write phase of 30176 * a read-modify-write request. This is used for targets where 30177 * un->un_sys_blocksize != un->un_tgt_blocksize. 30178 * 30179 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 30180 * 30181 * Context: Called under taskq thread context. 30182 */ 30183 30184 static void 30185 sd_read_modify_write_task(void *arg) 30186 { 30187 struct sd_mapblocksize_info *bsp; 30188 struct buf *bp; 30189 struct sd_xbuf *xp; 30190 struct sd_lun *un; 30191 30192 bp = arg; /* The bp is given in arg */ 30193 ASSERT(bp != NULL); 30194 30195 /* Get the pointer to the layer-private data struct */ 30196 xp = SD_GET_XBUF(bp); 30197 ASSERT(xp != NULL); 30198 bsp = xp->xb_private; 30199 ASSERT(bsp != NULL); 30200 30201 un = SD_GET_UN(bp); 30202 ASSERT(un != NULL); 30203 ASSERT(!mutex_owned(SD_MUTEX(un))); 30204 30205 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30206 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 30207 30208 /* 30209 * This is the write phase of a read-modify-write request, called 30210 * under the context of a taskq thread in response to the completion 30211 * of the read portion of the rmw request completing under interrupt 30212 * context. The write request must be sent from here down the iostart 30213 * chain as if it were being sent from sd_mapblocksize_iostart(), so 30214 * we use the layer index saved in the layer-private data area. 30215 */ 30216 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 30217 30218 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30219 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 30220 } 30221 30222 30223 /* 30224 * Function: sddump_do_read_of_rmw() 30225 * 30226 * Description: This routine will be called from sddump, If sddump is called 30227 * with an I/O which not aligned on device blocksize boundary 30228 * then the write has to be converted to read-modify-write. 30229 * Do the read part here in order to keep sddump simple. 30230 * Note - That the sd_mutex is held across the call to this 30231 * routine. 30232 * 30233 * Arguments: un - sd_lun 30234 * blkno - block number in terms of media block size. 30235 * nblk - number of blocks. 30236 * bpp - pointer to pointer to the buf structure. On return 30237 * from this function, *bpp points to the valid buffer 30238 * to which the write has to be done. 30239 * 30240 * Return Code: 0 for success or errno-type return code 30241 */ 30242 30243 static int 30244 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 30245 struct buf **bpp) 30246 { 30247 int err; 30248 int i; 30249 int rval; 30250 struct buf *bp; 30251 struct scsi_pkt *pkt = NULL; 30252 uint32_t target_blocksize; 30253 30254 ASSERT(un != NULL); 30255 ASSERT(mutex_owned(SD_MUTEX(un))); 30256 30257 target_blocksize = un->un_tgt_blocksize; 30258 30259 mutex_exit(SD_MUTEX(un)); 30260 30261 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 30262 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 30263 if (bp == NULL) { 30264 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30265 "no resources for dumping; giving up"); 30266 err = ENOMEM; 30267 goto done; 30268 } 30269 30270 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 30271 blkno, nblk); 30272 if (rval != 0) { 30273 scsi_free_consistent_buf(bp); 30274 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30275 "no resources for dumping; giving up"); 30276 err = ENOMEM; 30277 goto done; 30278 } 30279 30280 pkt->pkt_flags |= FLAG_NOINTR; 30281 30282 err = EIO; 30283 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 30284 30285 /* 30286 * Scsi_poll returns 0 (success) if the command completes and 30287 * the status block is STATUS_GOOD. We should only check 30288 * errors if this condition is not true. Even then we should 30289 * send our own request sense packet only if we have a check 30290 * condition and auto request sense has not been performed by 30291 * the hba. 30292 */ 30293 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 30294 30295 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 30296 err = 0; 30297 break; 30298 } 30299 30300 /* 30301 * Check CMD_DEV_GONE 1st, give up if device is gone, 30302 * no need to read RQS data. 30303 */ 30304 if (pkt->pkt_reason == CMD_DEV_GONE) { 30305 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30306 "Device is gone\n"); 30307 break; 30308 } 30309 30310 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 30311 SD_INFO(SD_LOG_DUMP, un, 30312 "sddump: read failed with CHECK, try # %d\n", i); 30313 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 30314 (void) sd_send_polled_RQS(un); 30315 } 30316 30317 continue; 30318 } 30319 30320 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 30321 int reset_retval = 0; 30322 30323 SD_INFO(SD_LOG_DUMP, un, 30324 "sddump: read failed with BUSY, try # %d\n", i); 30325 30326 if (un->un_f_lun_reset_enabled == TRUE) { 30327 reset_retval = scsi_reset(SD_ADDRESS(un), 30328 RESET_LUN); 30329 } 30330 if (reset_retval == 0) { 30331 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 30332 } 30333 (void) sd_send_polled_RQS(un); 30334 30335 } else { 30336 SD_INFO(SD_LOG_DUMP, un, 30337 "sddump: read failed with 0x%x, try # %d\n", 30338 SD_GET_PKT_STATUS(pkt), i); 30339 mutex_enter(SD_MUTEX(un)); 30340 sd_reset_target(un, pkt); 30341 mutex_exit(SD_MUTEX(un)); 30342 } 30343 30344 /* 30345 * If we are not getting anywhere with lun/target resets, 30346 * let's reset the bus. 30347 */ 30348 if (i > SD_NDUMP_RETRIES/2) { 30349 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 30350 (void) sd_send_polled_RQS(un); 30351 } 30352 30353 } 30354 scsi_destroy_pkt(pkt); 30355 30356 if (err != 0) { 30357 scsi_free_consistent_buf(bp); 30358 *bpp = NULL; 30359 } else { 30360 *bpp = bp; 30361 } 30362 30363 done: 30364 mutex_enter(SD_MUTEX(un)); 30365 return (err); 30366 } 30367 30368 30369 /* 30370 * Function: sd_failfast_flushq 30371 * 30372 * Description: Take all bp's on the wait queue that have B_FAILFAST set 30373 * in b_flags and move them onto the failfast queue, then kick 30374 * off a thread to return all bp's on the failfast queue to 30375 * their owners with an error set. 30376 * 30377 * Arguments: un - pointer to the soft state struct for the instance. 30378 * 30379 * Context: may execute in interrupt context. 30380 */ 30381 30382 static void 30383 sd_failfast_flushq(struct sd_lun *un) 30384 { 30385 struct buf *bp; 30386 struct buf *next_waitq_bp; 30387 struct buf *prev_waitq_bp = NULL; 30388 30389 ASSERT(un != NULL); 30390 ASSERT(mutex_owned(SD_MUTEX(un))); 30391 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 30392 ASSERT(un->un_failfast_bp == NULL); 30393 30394 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30395 "sd_failfast_flushq: entry: un:0x%p\n", un); 30396 30397 /* 30398 * Check if we should flush all bufs when entering failfast state, or 30399 * just those with B_FAILFAST set. 30400 */ 30401 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 30402 /* 30403 * Move *all* bp's on the wait queue to the failfast flush 30404 * queue, including those that do NOT have B_FAILFAST set. 30405 */ 30406 if (un->un_failfast_headp == NULL) { 30407 ASSERT(un->un_failfast_tailp == NULL); 30408 un->un_failfast_headp = un->un_waitq_headp; 30409 } else { 30410 ASSERT(un->un_failfast_tailp != NULL); 30411 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 30412 } 30413 30414 un->un_failfast_tailp = un->un_waitq_tailp; 30415 30416 /* update kstat for each bp moved out of the waitq */ 30417 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 30418 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30419 } 30420 30421 /* empty the waitq */ 30422 un->un_waitq_headp = un->un_waitq_tailp = NULL; 30423 30424 } else { 30425 /* 30426 * Go thru the wait queue, pick off all entries with 30427 * B_FAILFAST set, and move these onto the failfast queue. 30428 */ 30429 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 30430 /* 30431 * Save the pointer to the next bp on the wait queue, 30432 * so we get to it on the next iteration of this loop. 30433 */ 30434 next_waitq_bp = bp->av_forw; 30435 30436 /* 30437 * If this bp from the wait queue does NOT have 30438 * B_FAILFAST set, just move on to the next element 30439 * in the wait queue. Note, this is the only place 30440 * where it is correct to set prev_waitq_bp. 30441 */ 30442 if ((bp->b_flags & B_FAILFAST) == 0) { 30443 prev_waitq_bp = bp; 30444 continue; 30445 } 30446 30447 /* 30448 * Remove the bp from the wait queue. 30449 */ 30450 if (bp == un->un_waitq_headp) { 30451 /* The bp is the first element of the waitq. */ 30452 un->un_waitq_headp = next_waitq_bp; 30453 if (un->un_waitq_headp == NULL) { 30454 /* The wait queue is now empty */ 30455 un->un_waitq_tailp = NULL; 30456 } 30457 } else { 30458 /* 30459 * The bp is either somewhere in the middle 30460 * or at the end of the wait queue. 30461 */ 30462 ASSERT(un->un_waitq_headp != NULL); 30463 ASSERT(prev_waitq_bp != NULL); 30464 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 30465 == 0); 30466 if (bp == un->un_waitq_tailp) { 30467 /* bp is the last entry on the waitq. */ 30468 ASSERT(next_waitq_bp == NULL); 30469 un->un_waitq_tailp = prev_waitq_bp; 30470 } 30471 prev_waitq_bp->av_forw = next_waitq_bp; 30472 } 30473 bp->av_forw = NULL; 30474 30475 /* 30476 * update kstat since the bp is moved out of 30477 * the waitq 30478 */ 30479 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30480 30481 /* 30482 * Now put the bp onto the failfast queue. 30483 */ 30484 if (un->un_failfast_headp == NULL) { 30485 /* failfast queue is currently empty */ 30486 ASSERT(un->un_failfast_tailp == NULL); 30487 un->un_failfast_headp = 30488 un->un_failfast_tailp = bp; 30489 } else { 30490 /* Add the bp to the end of the failfast q */ 30491 ASSERT(un->un_failfast_tailp != NULL); 30492 ASSERT(un->un_failfast_tailp->b_flags & 30493 B_FAILFAST); 30494 un->un_failfast_tailp->av_forw = bp; 30495 un->un_failfast_tailp = bp; 30496 } 30497 } 30498 } 30499 30500 /* 30501 * Now return all bp's on the failfast queue to their owners. 30502 */ 30503 while ((bp = un->un_failfast_headp) != NULL) { 30504 30505 un->un_failfast_headp = bp->av_forw; 30506 if (un->un_failfast_headp == NULL) { 30507 un->un_failfast_tailp = NULL; 30508 } 30509 30510 /* 30511 * We want to return the bp with a failure error code, but 30512 * we do not want a call to sd_start_cmds() to occur here, 30513 * so use sd_return_failed_command_no_restart() instead of 30514 * sd_return_failed_command(). 30515 */ 30516 sd_return_failed_command_no_restart(un, bp, EIO); 30517 } 30518 30519 /* Flush the xbuf queues if required. */ 30520 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30521 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30522 } 30523 30524 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30525 "sd_failfast_flushq: exit: un:0x%p\n", un); 30526 } 30527 30528 30529 /* 30530 * Function: sd_failfast_flushq_callback 30531 * 30532 * Description: Return TRUE if the given bp meets the criteria for failfast 30533 * flushing. Used with ddi_xbuf_flushq(9F). 30534 * 30535 * Arguments: bp - ptr to buf struct to be examined. 30536 * 30537 * Context: Any 30538 */ 30539 30540 static int 30541 sd_failfast_flushq_callback(struct buf *bp) 30542 { 30543 /* 30544 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30545 * state is entered; OR (2) the given bp has B_FAILFAST set. 30546 */ 30547 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30548 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30549 } 30550 30551 30552 30553 #if defined(__i386) || defined(__amd64) 30554 /* 30555 * Function: sd_setup_next_xfer 30556 * 30557 * Description: Prepare next I/O operation using DMA_PARTIAL 30558 * 30559 */ 30560 30561 static int 30562 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30563 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30564 { 30565 ssize_t num_blks_not_xfered; 30566 daddr_t strt_blk_num; 30567 ssize_t bytes_not_xfered; 30568 int rval; 30569 30570 ASSERT(pkt->pkt_resid == 0); 30571 30572 /* 30573 * Calculate next block number and amount to be transferred. 30574 * 30575 * How much data NOT transfered to the HBA yet. 30576 */ 30577 bytes_not_xfered = xp->xb_dma_resid; 30578 30579 /* 30580 * figure how many blocks NOT transfered to the HBA yet. 30581 */ 30582 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30583 30584 /* 30585 * set starting block number to the end of what WAS transfered. 30586 */ 30587 strt_blk_num = xp->xb_blkno + 30588 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30589 30590 /* 30591 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30592 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30593 * the disk mutex here. 30594 */ 30595 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30596 strt_blk_num, num_blks_not_xfered); 30597 30598 if (rval == 0) { 30599 30600 /* 30601 * Success. 30602 * 30603 * Adjust things if there are still more blocks to be 30604 * transfered. 30605 */ 30606 xp->xb_dma_resid = pkt->pkt_resid; 30607 pkt->pkt_resid = 0; 30608 30609 return (1); 30610 } 30611 30612 /* 30613 * There's really only one possible return value from 30614 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30615 * returns NULL. 30616 */ 30617 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30618 30619 bp->b_resid = bp->b_bcount; 30620 bp->b_flags |= B_ERROR; 30621 30622 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30623 "Error setting up next portion of DMA transfer\n"); 30624 30625 return (0); 30626 } 30627 #endif 30628 30629 /* 30630 * Function: sd_panic_for_res_conflict 30631 * 30632 * Description: Call panic with a string formated with "Reservation Conflict" 30633 * and a human readable identifier indicating the SD instance 30634 * that experienced the reservation conflict. 30635 * 30636 * Arguments: un - pointer to the soft state struct for the instance. 30637 * 30638 * Context: may execute in interrupt context. 30639 */ 30640 30641 #define SD_RESV_CONFLICT_FMT_LEN 40 30642 void 30643 sd_panic_for_res_conflict(struct sd_lun *un) 30644 { 30645 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30646 char path_str[MAXPATHLEN]; 30647 30648 (void) snprintf(panic_str, sizeof (panic_str), 30649 "Reservation Conflict\nDisk: %s", 30650 ddi_pathname(SD_DEVINFO(un), path_str)); 30651 30652 panic(panic_str); 30653 } 30654 30655 /* 30656 * Note: The following sd_faultinjection_ioctl( ) routines implement 30657 * driver support for handling fault injection for error analysis 30658 * causing faults in multiple layers of the driver. 30659 * 30660 */ 30661 30662 #ifdef SD_FAULT_INJECTION 30663 static uint_t sd_fault_injection_on = 0; 30664 30665 /* 30666 * Function: sd_faultinjection_ioctl() 30667 * 30668 * Description: This routine is the driver entry point for handling 30669 * faultinjection ioctls to inject errors into the 30670 * layer model 30671 * 30672 * Arguments: cmd - the ioctl cmd recieved 30673 * arg - the arguments from user and returns 30674 */ 30675 30676 static void 30677 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30678 30679 uint_t i; 30680 uint_t rval; 30681 30682 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30683 30684 mutex_enter(SD_MUTEX(un)); 30685 30686 switch (cmd) { 30687 case SDIOCRUN: 30688 /* Allow pushed faults to be injected */ 30689 SD_INFO(SD_LOG_SDTEST, un, 30690 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30691 30692 sd_fault_injection_on = 1; 30693 30694 SD_INFO(SD_LOG_IOERR, un, 30695 "sd_faultinjection_ioctl: run finished\n"); 30696 break; 30697 30698 case SDIOCSTART: 30699 /* Start Injection Session */ 30700 SD_INFO(SD_LOG_SDTEST, un, 30701 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30702 30703 sd_fault_injection_on = 0; 30704 un->sd_injection_mask = 0xFFFFFFFF; 30705 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30706 un->sd_fi_fifo_pkt[i] = NULL; 30707 un->sd_fi_fifo_xb[i] = NULL; 30708 un->sd_fi_fifo_un[i] = NULL; 30709 un->sd_fi_fifo_arq[i] = NULL; 30710 } 30711 un->sd_fi_fifo_start = 0; 30712 un->sd_fi_fifo_end = 0; 30713 30714 mutex_enter(&(un->un_fi_mutex)); 30715 un->sd_fi_log[0] = '\0'; 30716 un->sd_fi_buf_len = 0; 30717 mutex_exit(&(un->un_fi_mutex)); 30718 30719 SD_INFO(SD_LOG_IOERR, un, 30720 "sd_faultinjection_ioctl: start finished\n"); 30721 break; 30722 30723 case SDIOCSTOP: 30724 /* Stop Injection Session */ 30725 SD_INFO(SD_LOG_SDTEST, un, 30726 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30727 sd_fault_injection_on = 0; 30728 un->sd_injection_mask = 0x0; 30729 30730 /* Empty stray or unuseds structs from fifo */ 30731 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30732 if (un->sd_fi_fifo_pkt[i] != NULL) { 30733 kmem_free(un->sd_fi_fifo_pkt[i], 30734 sizeof (struct sd_fi_pkt)); 30735 } 30736 if (un->sd_fi_fifo_xb[i] != NULL) { 30737 kmem_free(un->sd_fi_fifo_xb[i], 30738 sizeof (struct sd_fi_xb)); 30739 } 30740 if (un->sd_fi_fifo_un[i] != NULL) { 30741 kmem_free(un->sd_fi_fifo_un[i], 30742 sizeof (struct sd_fi_un)); 30743 } 30744 if (un->sd_fi_fifo_arq[i] != NULL) { 30745 kmem_free(un->sd_fi_fifo_arq[i], 30746 sizeof (struct sd_fi_arq)); 30747 } 30748 un->sd_fi_fifo_pkt[i] = NULL; 30749 un->sd_fi_fifo_un[i] = NULL; 30750 un->sd_fi_fifo_xb[i] = NULL; 30751 un->sd_fi_fifo_arq[i] = NULL; 30752 } 30753 un->sd_fi_fifo_start = 0; 30754 un->sd_fi_fifo_end = 0; 30755 30756 SD_INFO(SD_LOG_IOERR, un, 30757 "sd_faultinjection_ioctl: stop finished\n"); 30758 break; 30759 30760 case SDIOCINSERTPKT: 30761 /* Store a packet struct to be pushed onto fifo */ 30762 SD_INFO(SD_LOG_SDTEST, un, 30763 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30764 30765 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30766 30767 sd_fault_injection_on = 0; 30768 30769 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30770 if (un->sd_fi_fifo_pkt[i] != NULL) { 30771 kmem_free(un->sd_fi_fifo_pkt[i], 30772 sizeof (struct sd_fi_pkt)); 30773 } 30774 if (arg != NULL) { 30775 un->sd_fi_fifo_pkt[i] = 30776 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30777 if (un->sd_fi_fifo_pkt[i] == NULL) { 30778 /* Alloc failed don't store anything */ 30779 break; 30780 } 30781 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30782 sizeof (struct sd_fi_pkt), 0); 30783 if (rval == -1) { 30784 kmem_free(un->sd_fi_fifo_pkt[i], 30785 sizeof (struct sd_fi_pkt)); 30786 un->sd_fi_fifo_pkt[i] = NULL; 30787 } 30788 } else { 30789 SD_INFO(SD_LOG_IOERR, un, 30790 "sd_faultinjection_ioctl: pkt null\n"); 30791 } 30792 break; 30793 30794 case SDIOCINSERTXB: 30795 /* Store a xb struct to be pushed onto fifo */ 30796 SD_INFO(SD_LOG_SDTEST, un, 30797 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30798 30799 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30800 30801 sd_fault_injection_on = 0; 30802 30803 if (un->sd_fi_fifo_xb[i] != NULL) { 30804 kmem_free(un->sd_fi_fifo_xb[i], 30805 sizeof (struct sd_fi_xb)); 30806 un->sd_fi_fifo_xb[i] = NULL; 30807 } 30808 if (arg != NULL) { 30809 un->sd_fi_fifo_xb[i] = 30810 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30811 if (un->sd_fi_fifo_xb[i] == NULL) { 30812 /* Alloc failed don't store anything */ 30813 break; 30814 } 30815 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30816 sizeof (struct sd_fi_xb), 0); 30817 30818 if (rval == -1) { 30819 kmem_free(un->sd_fi_fifo_xb[i], 30820 sizeof (struct sd_fi_xb)); 30821 un->sd_fi_fifo_xb[i] = NULL; 30822 } 30823 } else { 30824 SD_INFO(SD_LOG_IOERR, un, 30825 "sd_faultinjection_ioctl: xb null\n"); 30826 } 30827 break; 30828 30829 case SDIOCINSERTUN: 30830 /* Store a un struct to be pushed onto fifo */ 30831 SD_INFO(SD_LOG_SDTEST, un, 30832 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30833 30834 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30835 30836 sd_fault_injection_on = 0; 30837 30838 if (un->sd_fi_fifo_un[i] != NULL) { 30839 kmem_free(un->sd_fi_fifo_un[i], 30840 sizeof (struct sd_fi_un)); 30841 un->sd_fi_fifo_un[i] = NULL; 30842 } 30843 if (arg != NULL) { 30844 un->sd_fi_fifo_un[i] = 30845 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30846 if (un->sd_fi_fifo_un[i] == NULL) { 30847 /* Alloc failed don't store anything */ 30848 break; 30849 } 30850 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30851 sizeof (struct sd_fi_un), 0); 30852 if (rval == -1) { 30853 kmem_free(un->sd_fi_fifo_un[i], 30854 sizeof (struct sd_fi_un)); 30855 un->sd_fi_fifo_un[i] = NULL; 30856 } 30857 30858 } else { 30859 SD_INFO(SD_LOG_IOERR, un, 30860 "sd_faultinjection_ioctl: un null\n"); 30861 } 30862 30863 break; 30864 30865 case SDIOCINSERTARQ: 30866 /* Store a arq struct to be pushed onto fifo */ 30867 SD_INFO(SD_LOG_SDTEST, un, 30868 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30869 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30870 30871 sd_fault_injection_on = 0; 30872 30873 if (un->sd_fi_fifo_arq[i] != NULL) { 30874 kmem_free(un->sd_fi_fifo_arq[i], 30875 sizeof (struct sd_fi_arq)); 30876 un->sd_fi_fifo_arq[i] = NULL; 30877 } 30878 if (arg != NULL) { 30879 un->sd_fi_fifo_arq[i] = 30880 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30881 if (un->sd_fi_fifo_arq[i] == NULL) { 30882 /* Alloc failed don't store anything */ 30883 break; 30884 } 30885 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30886 sizeof (struct sd_fi_arq), 0); 30887 if (rval == -1) { 30888 kmem_free(un->sd_fi_fifo_arq[i], 30889 sizeof (struct sd_fi_arq)); 30890 un->sd_fi_fifo_arq[i] = NULL; 30891 } 30892 30893 } else { 30894 SD_INFO(SD_LOG_IOERR, un, 30895 "sd_faultinjection_ioctl: arq null\n"); 30896 } 30897 30898 break; 30899 30900 case SDIOCPUSH: 30901 /* Push stored xb, pkt, un, and arq onto fifo */ 30902 sd_fault_injection_on = 0; 30903 30904 if (arg != NULL) { 30905 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30906 if (rval != -1 && 30907 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30908 un->sd_fi_fifo_end += i; 30909 } 30910 } else { 30911 SD_INFO(SD_LOG_IOERR, un, 30912 "sd_faultinjection_ioctl: push arg null\n"); 30913 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30914 un->sd_fi_fifo_end++; 30915 } 30916 } 30917 SD_INFO(SD_LOG_IOERR, un, 30918 "sd_faultinjection_ioctl: push to end=%d\n", 30919 un->sd_fi_fifo_end); 30920 break; 30921 30922 case SDIOCRETRIEVE: 30923 /* Return buffer of log from Injection session */ 30924 SD_INFO(SD_LOG_SDTEST, un, 30925 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30926 30927 sd_fault_injection_on = 0; 30928 30929 mutex_enter(&(un->un_fi_mutex)); 30930 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30931 un->sd_fi_buf_len+1, 0); 30932 mutex_exit(&(un->un_fi_mutex)); 30933 30934 if (rval == -1) { 30935 /* 30936 * arg is possibly invalid setting 30937 * it to NULL for return 30938 */ 30939 arg = NULL; 30940 } 30941 break; 30942 } 30943 30944 mutex_exit(SD_MUTEX(un)); 30945 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30946 " exit\n"); 30947 } 30948 30949 30950 /* 30951 * Function: sd_injection_log() 30952 * 30953 * Description: This routine adds buff to the already existing injection log 30954 * for retrieval via faultinjection_ioctl for use in fault 30955 * detection and recovery 30956 * 30957 * Arguments: buf - the string to add to the log 30958 */ 30959 30960 static void 30961 sd_injection_log(char *buf, struct sd_lun *un) 30962 { 30963 uint_t len; 30964 30965 ASSERT(un != NULL); 30966 ASSERT(buf != NULL); 30967 30968 mutex_enter(&(un->un_fi_mutex)); 30969 30970 len = min(strlen(buf), 255); 30971 /* Add logged value to Injection log to be returned later */ 30972 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30973 uint_t offset = strlen((char *)un->sd_fi_log); 30974 char *destp = (char *)un->sd_fi_log + offset; 30975 int i; 30976 for (i = 0; i < len; i++) { 30977 *destp++ = *buf++; 30978 } 30979 un->sd_fi_buf_len += len; 30980 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30981 } 30982 30983 mutex_exit(&(un->un_fi_mutex)); 30984 } 30985 30986 30987 /* 30988 * Function: sd_faultinjection() 30989 * 30990 * Description: This routine takes the pkt and changes its 30991 * content based on error injection scenerio. 30992 * 30993 * Arguments: pktp - packet to be changed 30994 */ 30995 30996 static void 30997 sd_faultinjection(struct scsi_pkt *pktp) 30998 { 30999 uint_t i; 31000 struct sd_fi_pkt *fi_pkt; 31001 struct sd_fi_xb *fi_xb; 31002 struct sd_fi_un *fi_un; 31003 struct sd_fi_arq *fi_arq; 31004 struct buf *bp; 31005 struct sd_xbuf *xb; 31006 struct sd_lun *un; 31007 31008 ASSERT(pktp != NULL); 31009 31010 /* pull bp xb and un from pktp */ 31011 bp = (struct buf *)pktp->pkt_private; 31012 xb = SD_GET_XBUF(bp); 31013 un = SD_GET_UN(bp); 31014 31015 ASSERT(un != NULL); 31016 31017 mutex_enter(SD_MUTEX(un)); 31018 31019 SD_TRACE(SD_LOG_SDTEST, un, 31020 "sd_faultinjection: entry Injection from sdintr\n"); 31021 31022 /* if injection is off return */ 31023 if (sd_fault_injection_on == 0 || 31024 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 31025 mutex_exit(SD_MUTEX(un)); 31026 return; 31027 } 31028 31029 31030 /* take next set off fifo */ 31031 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 31032 31033 fi_pkt = un->sd_fi_fifo_pkt[i]; 31034 fi_xb = un->sd_fi_fifo_xb[i]; 31035 fi_un = un->sd_fi_fifo_un[i]; 31036 fi_arq = un->sd_fi_fifo_arq[i]; 31037 31038 31039 /* set variables accordingly */ 31040 /* set pkt if it was on fifo */ 31041 if (fi_pkt != NULL) { 31042 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 31043 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 31044 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 31045 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 31046 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 31047 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 31048 31049 } 31050 31051 /* set xb if it was on fifo */ 31052 if (fi_xb != NULL) { 31053 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 31054 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 31055 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 31056 SD_CONDSET(xb, xb, xb_victim_retry_count, 31057 "xb_victim_retry_count"); 31058 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 31059 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 31060 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 31061 31062 /* copy in block data from sense */ 31063 if (fi_xb->xb_sense_data[0] != -1) { 31064 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 31065 SENSE_LENGTH); 31066 } 31067 31068 /* copy in extended sense codes */ 31069 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 31070 "es_code"); 31071 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 31072 "es_key"); 31073 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 31074 "es_add_code"); 31075 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 31076 es_qual_code, "es_qual_code"); 31077 } 31078 31079 /* set un if it was on fifo */ 31080 if (fi_un != NULL) { 31081 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 31082 SD_CONDSET(un, un, un_ctype, "un_ctype"); 31083 SD_CONDSET(un, un, un_reset_retry_count, 31084 "un_reset_retry_count"); 31085 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 31086 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 31087 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 31088 SD_CONDSET(un, un, un_f_geometry_is_valid, 31089 "un_f_geometry_is_valid"); 31090 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 31091 "un_f_allow_bus_device_reset"); 31092 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 31093 31094 } 31095 31096 /* copy in auto request sense if it was on fifo */ 31097 if (fi_arq != NULL) { 31098 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 31099 } 31100 31101 /* free structs */ 31102 if (un->sd_fi_fifo_pkt[i] != NULL) { 31103 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 31104 } 31105 if (un->sd_fi_fifo_xb[i] != NULL) { 31106 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 31107 } 31108 if (un->sd_fi_fifo_un[i] != NULL) { 31109 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 31110 } 31111 if (un->sd_fi_fifo_arq[i] != NULL) { 31112 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 31113 } 31114 31115 /* 31116 * kmem_free does not gurantee to set to NULL 31117 * since we uses these to determine if we set 31118 * values or not lets confirm they are always 31119 * NULL after free 31120 */ 31121 un->sd_fi_fifo_pkt[i] = NULL; 31122 un->sd_fi_fifo_un[i] = NULL; 31123 un->sd_fi_fifo_xb[i] = NULL; 31124 un->sd_fi_fifo_arq[i] = NULL; 31125 31126 un->sd_fi_fifo_start++; 31127 31128 mutex_exit(SD_MUTEX(un)); 31129 31130 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 31131 } 31132 31133 #endif /* SD_FAULT_INJECTION */ 31134 31135 /* 31136 * This routine is invoked in sd_unit_attach(). Before calling it, the 31137 * properties in conf file should be processed already, and "hotpluggable" 31138 * property was processed also. 31139 * 31140 * The sd driver distinguishes 3 different type of devices: removable media, 31141 * non-removable media, and hotpluggable. Below the differences are defined: 31142 * 31143 * 1. Device ID 31144 * 31145 * The device ID of a device is used to identify this device. Refer to 31146 * ddi_devid_register(9F). 31147 * 31148 * For a non-removable media disk device which can provide 0x80 or 0x83 31149 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 31150 * device ID is created to identify this device. For other non-removable 31151 * media devices, a default device ID is created only if this device has 31152 * at least 2 alter cylinders. Otherwise, this device has no devid. 31153 * 31154 * ------------------------------------------------------- 31155 * removable media hotpluggable | Can Have Device ID 31156 * ------------------------------------------------------- 31157 * false false | Yes 31158 * false true | Yes 31159 * true x | No 31160 * ------------------------------------------------------ 31161 * 31162 * 31163 * 2. SCSI group 4 commands 31164 * 31165 * In SCSI specs, only some commands in group 4 command set can use 31166 * 8-byte addresses that can be used to access >2TB storage spaces. 31167 * Other commands have no such capability. Without supporting group4, 31168 * it is impossible to make full use of storage spaces of a disk with 31169 * capacity larger than 2TB. 31170 * 31171 * ----------------------------------------------- 31172 * removable media hotpluggable LP64 | Group 31173 * ----------------------------------------------- 31174 * false false false | 1 31175 * false false true | 4 31176 * false true false | 1 31177 * false true true | 4 31178 * true x x | 5 31179 * ----------------------------------------------- 31180 * 31181 * 31182 * 3. Check for VTOC Label 31183 * 31184 * If a direct-access disk has no EFI label, sd will check if it has a 31185 * valid VTOC label. Now, sd also does that check for removable media 31186 * and hotpluggable devices. 31187 * 31188 * -------------------------------------------------------------- 31189 * Direct-Access removable media hotpluggable | Check Label 31190 * ------------------------------------------------------------- 31191 * false false false | No 31192 * false false true | No 31193 * false true false | Yes 31194 * false true true | Yes 31195 * true x x | Yes 31196 * -------------------------------------------------------------- 31197 * 31198 * 31199 * 4. Building default VTOC label 31200 * 31201 * As section 3 says, sd checks if some kinds of devices have VTOC label. 31202 * If those devices have no valid VTOC label, sd(7d) will attempt to 31203 * create default VTOC for them. Currently sd creates default VTOC label 31204 * for all devices on x86 platform (VTOC_16), but only for removable 31205 * media devices on SPARC (VTOC_8). 31206 * 31207 * ----------------------------------------------------------- 31208 * removable media hotpluggable platform | Default Label 31209 * ----------------------------------------------------------- 31210 * false false sparc | No 31211 * false true x86 | Yes 31212 * false true sparc | Yes 31213 * true x x | Yes 31214 * ---------------------------------------------------------- 31215 * 31216 * 31217 * 5. Supported blocksizes of target devices 31218 * 31219 * Sd supports non-512-byte blocksize for removable media devices only. 31220 * For other devices, only 512-byte blocksize is supported. This may be 31221 * changed in near future because some RAID devices require non-512-byte 31222 * blocksize 31223 * 31224 * ----------------------------------------------------------- 31225 * removable media hotpluggable | non-512-byte blocksize 31226 * ----------------------------------------------------------- 31227 * false false | No 31228 * false true | No 31229 * true x | Yes 31230 * ----------------------------------------------------------- 31231 * 31232 * 31233 * 6. Automatic mount & unmount 31234 * 31235 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 31236 * if a device is removable media device. It return 1 for removable media 31237 * devices, and 0 for others. 31238 * 31239 * The automatic mounting subsystem should distinguish between the types 31240 * of devices and apply automounting policies to each. 31241 * 31242 * 31243 * 7. fdisk partition management 31244 * 31245 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 31246 * just supports fdisk partitions on x86 platform. On sparc platform, sd 31247 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 31248 * fdisk partitions on both x86 and SPARC platform. 31249 * 31250 * ----------------------------------------------------------- 31251 * platform removable media USB/1394 | fdisk supported 31252 * ----------------------------------------------------------- 31253 * x86 X X | true 31254 * ------------------------------------------------------------ 31255 * sparc X X | false 31256 * ------------------------------------------------------------ 31257 * 31258 * 31259 * 8. MBOOT/MBR 31260 * 31261 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 31262 * read/write mboot for removable media devices on sparc platform. 31263 * 31264 * ----------------------------------------------------------- 31265 * platform removable media USB/1394 | mboot supported 31266 * ----------------------------------------------------------- 31267 * x86 X X | true 31268 * ------------------------------------------------------------ 31269 * sparc false false | false 31270 * sparc false true | true 31271 * sparc true false | true 31272 * sparc true true | true 31273 * ------------------------------------------------------------ 31274 * 31275 * 31276 * 9. error handling during opening device 31277 * 31278 * If failed to open a disk device, an errno is returned. For some kinds 31279 * of errors, different errno is returned depending on if this device is 31280 * a removable media device. This brings USB/1394 hard disks in line with 31281 * expected hard disk behavior. It is not expected that this breaks any 31282 * application. 31283 * 31284 * ------------------------------------------------------ 31285 * removable media hotpluggable | errno 31286 * ------------------------------------------------------ 31287 * false false | EIO 31288 * false true | EIO 31289 * true x | ENXIO 31290 * ------------------------------------------------------ 31291 * 31292 * 31293 * 11. ioctls: DKIOCEJECT, CDROMEJECT 31294 * 31295 * These IOCTLs are applicable only to removable media devices. 31296 * 31297 * ----------------------------------------------------------- 31298 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 31299 * ----------------------------------------------------------- 31300 * false false | No 31301 * false true | No 31302 * true x | Yes 31303 * ----------------------------------------------------------- 31304 * 31305 * 31306 * 12. Kstats for partitions 31307 * 31308 * sd creates partition kstat for non-removable media devices. USB and 31309 * Firewire hard disks now have partition kstats 31310 * 31311 * ------------------------------------------------------ 31312 * removable media hotplugable | kstat 31313 * ------------------------------------------------------ 31314 * false false | Yes 31315 * false true | Yes 31316 * true x | No 31317 * ------------------------------------------------------ 31318 * 31319 * 31320 * 13. Removable media & hotpluggable properties 31321 * 31322 * Sd driver creates a "removable-media" property for removable media 31323 * devices. Parent nexus drivers create a "hotpluggable" property if 31324 * it supports hotplugging. 31325 * 31326 * --------------------------------------------------------------------- 31327 * removable media hotpluggable | "removable-media" " hotpluggable" 31328 * --------------------------------------------------------------------- 31329 * false false | No No 31330 * false true | No Yes 31331 * true false | Yes No 31332 * true true | Yes Yes 31333 * --------------------------------------------------------------------- 31334 * 31335 * 31336 * 14. Power Management 31337 * 31338 * sd only power manages removable media devices or devices that support 31339 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 31340 * 31341 * A parent nexus that supports hotplugging can also set "pm-capable" 31342 * if the disk can be power managed. 31343 * 31344 * ------------------------------------------------------------ 31345 * removable media hotpluggable pm-capable | power manage 31346 * ------------------------------------------------------------ 31347 * false false false | No 31348 * false false true | Yes 31349 * false true false | No 31350 * false true true | Yes 31351 * true x x | Yes 31352 * ------------------------------------------------------------ 31353 * 31354 * USB and firewire hard disks can now be power managed independently 31355 * of the framebuffer 31356 * 31357 * 31358 * 15. Support for USB disks with capacity larger than 1TB 31359 * 31360 * Currently, sd doesn't permit a fixed disk device with capacity 31361 * larger than 1TB to be used in a 32-bit operating system environment. 31362 * However, sd doesn't do that for removable media devices. Instead, it 31363 * assumes that removable media devices cannot have a capacity larger 31364 * than 1TB. Therefore, using those devices on 32-bit system is partially 31365 * supported, which can cause some unexpected results. 31366 * 31367 * --------------------------------------------------------------------- 31368 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 31369 * --------------------------------------------------------------------- 31370 * false false | true | no 31371 * false true | true | no 31372 * true false | true | Yes 31373 * true true | true | Yes 31374 * --------------------------------------------------------------------- 31375 * 31376 * 31377 * 16. Check write-protection at open time 31378 * 31379 * When a removable media device is being opened for writing without NDELAY 31380 * flag, sd will check if this device is writable. If attempting to open 31381 * without NDELAY flag a write-protected device, this operation will abort. 31382 * 31383 * ------------------------------------------------------------ 31384 * removable media USB/1394 | WP Check 31385 * ------------------------------------------------------------ 31386 * false false | No 31387 * false true | No 31388 * true false | Yes 31389 * true true | Yes 31390 * ------------------------------------------------------------ 31391 * 31392 * 31393 * 17. syslog when corrupted VTOC is encountered 31394 * 31395 * Currently, if an invalid VTOC is encountered, sd only print syslog 31396 * for fixed SCSI disks. 31397 * ------------------------------------------------------------ 31398 * removable media USB/1394 | print syslog 31399 * ------------------------------------------------------------ 31400 * false false | Yes 31401 * false true | No 31402 * true false | No 31403 * true true | No 31404 * ------------------------------------------------------------ 31405 */ 31406 static void 31407 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 31408 { 31409 int pm_capable_prop; 31410 31411 ASSERT(un->un_sd); 31412 ASSERT(un->un_sd->sd_inq); 31413 31414 #if defined(_SUNOS_VTOC_16) 31415 /* 31416 * For VTOC_16 devices, the default label will be created for all 31417 * devices. (see sd_build_default_label) 31418 */ 31419 un->un_f_default_vtoc_supported = TRUE; 31420 #endif 31421 31422 if (un->un_sd->sd_inq->inq_rmb) { 31423 /* 31424 * The media of this device is removable. And for this kind 31425 * of devices, it is possible to change medium after opening 31426 * devices. Thus we should support this operation. 31427 */ 31428 un->un_f_has_removable_media = TRUE; 31429 31430 #if defined(_SUNOS_VTOC_8) 31431 /* 31432 * Note: currently, for VTOC_8 devices, default label is 31433 * created for removable and hotpluggable devices only. 31434 */ 31435 un->un_f_default_vtoc_supported = TRUE; 31436 #endif 31437 /* 31438 * support non-512-byte blocksize of removable media devices 31439 */ 31440 un->un_f_non_devbsize_supported = TRUE; 31441 31442 /* 31443 * Assume that all removable media devices support DOOR_LOCK 31444 */ 31445 un->un_f_doorlock_supported = TRUE; 31446 31447 /* 31448 * For a removable media device, it is possible to be opened 31449 * with NDELAY flag when there is no media in drive, in this 31450 * case we don't care if device is writable. But if without 31451 * NDELAY flag, we need to check if media is write-protected. 31452 */ 31453 un->un_f_chk_wp_open = TRUE; 31454 31455 /* 31456 * need to start a SCSI watch thread to monitor media state, 31457 * when media is being inserted or ejected, notify syseventd. 31458 */ 31459 un->un_f_monitor_media_state = TRUE; 31460 31461 /* 31462 * Some devices don't support START_STOP_UNIT command. 31463 * Therefore, we'd better check if a device supports it 31464 * before sending it. 31465 */ 31466 un->un_f_check_start_stop = TRUE; 31467 31468 /* 31469 * support eject media ioctl: 31470 * FDEJECT, DKIOCEJECT, CDROMEJECT 31471 */ 31472 un->un_f_eject_media_supported = TRUE; 31473 31474 /* 31475 * Because many removable-media devices don't support 31476 * LOG_SENSE, we couldn't use this command to check if 31477 * a removable media device support power-management. 31478 * We assume that they support power-management via 31479 * START_STOP_UNIT command and can be spun up and down 31480 * without limitations. 31481 */ 31482 un->un_f_pm_supported = TRUE; 31483 31484 /* 31485 * Need to create a zero length (Boolean) property 31486 * removable-media for the removable media devices. 31487 * Note that the return value of the property is not being 31488 * checked, since if unable to create the property 31489 * then do not want the attach to fail altogether. Consistent 31490 * with other property creation in attach. 31491 */ 31492 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31493 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31494 31495 } else { 31496 /* 31497 * create device ID for device 31498 */ 31499 un->un_f_devid_supported = TRUE; 31500 31501 /* 31502 * Spin up non-removable-media devices once it is attached 31503 */ 31504 un->un_f_attach_spinup = TRUE; 31505 31506 /* 31507 * According to SCSI specification, Sense data has two kinds of 31508 * format: fixed format, and descriptor format. At present, we 31509 * don't support descriptor format sense data for removable 31510 * media. 31511 */ 31512 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31513 un->un_f_descr_format_supported = TRUE; 31514 } 31515 31516 /* 31517 * kstats are created only for non-removable media devices. 31518 * 31519 * Set this in sd.conf to 0 in order to disable kstats. The 31520 * default is 1, so they are enabled by default. 31521 */ 31522 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31523 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31524 "enable-partition-kstats", 1)); 31525 31526 /* 31527 * Check if HBA has set the "pm-capable" property. 31528 * If "pm-capable" exists and is non-zero then we can 31529 * power manage the device without checking the start/stop 31530 * cycle count log sense page. 31531 * 31532 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31533 * then we should not power manage the device. 31534 * 31535 * If "pm-capable" doesn't exist then pm_capable_prop will 31536 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31537 * sd will check the start/stop cycle count log sense page 31538 * and power manage the device if the cycle count limit has 31539 * not been exceeded. 31540 */ 31541 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31542 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31543 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31544 un->un_f_log_sense_supported = TRUE; 31545 } else { 31546 /* 31547 * pm-capable property exists. 31548 * 31549 * Convert "TRUE" values for pm_capable_prop to 31550 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31551 * later. "TRUE" values are any values except 31552 * SD_PM_CAPABLE_FALSE (0) and 31553 * SD_PM_CAPABLE_UNDEFINED (-1) 31554 */ 31555 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31556 un->un_f_log_sense_supported = FALSE; 31557 } else { 31558 un->un_f_pm_supported = TRUE; 31559 } 31560 31561 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31562 "sd_unit_attach: un:0x%p pm-capable " 31563 "property set to %d.\n", un, un->un_f_pm_supported); 31564 } 31565 } 31566 31567 if (un->un_f_is_hotpluggable) { 31568 #if defined(_SUNOS_VTOC_8) 31569 /* 31570 * Note: currently, for VTOC_8 devices, default label is 31571 * created for removable and hotpluggable devices only. 31572 */ 31573 un->un_f_default_vtoc_supported = TRUE; 31574 #endif 31575 31576 /* 31577 * Have to watch hotpluggable devices as well, since 31578 * that's the only way for userland applications to 31579 * detect hot removal while device is busy/mounted. 31580 */ 31581 un->un_f_monitor_media_state = TRUE; 31582 31583 un->un_f_check_start_stop = TRUE; 31584 31585 } 31586 31587 /* 31588 * By default, only DIRECT ACCESS devices and CDs will have Sun 31589 * labels. 31590 */ 31591 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31592 (un->un_sd->sd_inq->inq_rmb)) { 31593 /* 31594 * Direct access devices have disk label 31595 */ 31596 un->un_f_vtoc_label_supported = TRUE; 31597 } 31598 31599 /* 31600 * Fdisk partitions are supported for all direct access devices on 31601 * x86 platform, and just for removable media and hotpluggable 31602 * devices on SPARC platform. Later, we will set the following flag 31603 * to FALSE if current device is not removable media or hotpluggable 31604 * device and if sd works on SAPRC platform. 31605 */ 31606 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31607 un->un_f_mboot_supported = TRUE; 31608 } 31609 31610 if (!un->un_f_is_hotpluggable && 31611 !un->un_sd->sd_inq->inq_rmb) { 31612 31613 #if defined(_SUNOS_VTOC_8) 31614 /* 31615 * Don't support fdisk on fixed disk 31616 */ 31617 un->un_f_mboot_supported = FALSE; 31618 #endif 31619 31620 /* 31621 * Fixed disk support SYNC CACHE 31622 */ 31623 un->un_f_sync_cache_supported = TRUE; 31624 31625 /* 31626 * For fixed disk, if its VTOC is not valid, we will write 31627 * errlog into system log 31628 */ 31629 if (un->un_f_vtoc_label_supported) 31630 un->un_f_vtoc_errlog_supported = TRUE; 31631 } 31632 } 31633