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 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 637 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 638 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 639 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 640 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 641 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 642 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 643 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 644 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 645 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 646 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 647 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 648 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 649 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 650 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 651 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 652 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 653 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 654 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties }, 655 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties }, 656 { "SUN T3", SD_CONF_BSET_THROTTLE | 657 SD_CONF_BSET_BSY_RETRY_COUNT| 658 SD_CONF_BSET_RST_RETRIES| 659 SD_CONF_BSET_RSV_REL_TIME, 660 &purple_properties }, 661 { "SUN SESS01", SD_CONF_BSET_THROTTLE | 662 SD_CONF_BSET_BSY_RETRY_COUNT| 663 SD_CONF_BSET_RST_RETRIES| 664 SD_CONF_BSET_RSV_REL_TIME| 665 SD_CONF_BSET_MIN_THROTTLE| 666 SD_CONF_BSET_DISKSORT_DISABLED, 667 &sve_properties }, 668 { "SUN T4", SD_CONF_BSET_THROTTLE | 669 SD_CONF_BSET_BSY_RETRY_COUNT| 670 SD_CONF_BSET_RST_RETRIES| 671 SD_CONF_BSET_RSV_REL_TIME, 672 &purple_properties }, 673 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED | 674 SD_CONF_BSET_LUN_RESET_ENABLED, 675 &maserati_properties }, 676 { "SUN SE6920", SD_CONF_BSET_THROTTLE | 677 SD_CONF_BSET_NRR_COUNT| 678 SD_CONF_BSET_BSY_RETRY_COUNT| 679 SD_CONF_BSET_RST_RETRIES| 680 SD_CONF_BSET_MIN_THROTTLE| 681 SD_CONF_BSET_DISKSORT_DISABLED| 682 SD_CONF_BSET_LUN_RESET_ENABLED, 683 &pirus_properties }, 684 { "SUN SE6940", SD_CONF_BSET_THROTTLE | 685 SD_CONF_BSET_NRR_COUNT| 686 SD_CONF_BSET_BSY_RETRY_COUNT| 687 SD_CONF_BSET_RST_RETRIES| 688 SD_CONF_BSET_MIN_THROTTLE| 689 SD_CONF_BSET_DISKSORT_DISABLED| 690 SD_CONF_BSET_LUN_RESET_ENABLED, 691 &pirus_properties }, 692 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE | 693 SD_CONF_BSET_NRR_COUNT| 694 SD_CONF_BSET_BSY_RETRY_COUNT| 695 SD_CONF_BSET_RST_RETRIES| 696 SD_CONF_BSET_MIN_THROTTLE| 697 SD_CONF_BSET_DISKSORT_DISABLED| 698 SD_CONF_BSET_LUN_RESET_ENABLED, 699 &pirus_properties }, 700 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE | 701 SD_CONF_BSET_NRR_COUNT| 702 SD_CONF_BSET_BSY_RETRY_COUNT| 703 SD_CONF_BSET_RST_RETRIES| 704 SD_CONF_BSET_MIN_THROTTLE| 705 SD_CONF_BSET_DISKSORT_DISABLED| 706 SD_CONF_BSET_LUN_RESET_ENABLED, 707 &pirus_properties }, 708 { "SUN PSX1000", SD_CONF_BSET_THROTTLE | 709 SD_CONF_BSET_NRR_COUNT| 710 SD_CONF_BSET_BSY_RETRY_COUNT| 711 SD_CONF_BSET_RST_RETRIES| 712 SD_CONF_BSET_MIN_THROTTLE| 713 SD_CONF_BSET_DISKSORT_DISABLED| 714 SD_CONF_BSET_LUN_RESET_ENABLED, 715 &pirus_properties }, 716 { "SUN SE6330", SD_CONF_BSET_THROTTLE | 717 SD_CONF_BSET_NRR_COUNT| 718 SD_CONF_BSET_BSY_RETRY_COUNT| 719 SD_CONF_BSET_RST_RETRIES| 720 SD_CONF_BSET_MIN_THROTTLE| 721 SD_CONF_BSET_DISKSORT_DISABLED| 722 SD_CONF_BSET_LUN_RESET_ENABLED, 723 &pirus_properties }, 724 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 725 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 726 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 727 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties }, 728 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties }, 729 #endif /* fibre or NON-sparc platforms */ 730 #if ((defined(__sparc) && !defined(__fibre)) ||\ 731 (defined(__i386) || defined(__amd64))) 732 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties }, 733 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties }, 734 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL }, 735 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL }, 736 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL }, 737 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL }, 738 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL }, 739 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL }, 740 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL }, 741 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL }, 742 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL }, 743 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL }, 744 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT, 745 &symbios_properties }, 746 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT, 747 &lsi_properties_scsi }, 748 #if defined(__i386) || defined(__amd64) 749 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD 750 | SD_CONF_BSET_READSUB_BCD 751 | SD_CONF_BSET_READ_TOC_ADDR_BCD 752 | SD_CONF_BSET_NO_READ_HEADER 753 | SD_CONF_BSET_READ_CD_XD4), NULL }, 754 755 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD 756 | SD_CONF_BSET_READSUB_BCD 757 | SD_CONF_BSET_READ_TOC_ADDR_BCD 758 | SD_CONF_BSET_NO_READ_HEADER 759 | SD_CONF_BSET_READ_CD_XD4), NULL }, 760 #endif /* __i386 || __amd64 */ 761 #endif /* sparc NON-fibre or NON-sparc platforms */ 762 763 #if (defined(SD_PROP_TST)) 764 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE 765 | SD_CONF_BSET_CTYPE 766 | SD_CONF_BSET_NRR_COUNT 767 | SD_CONF_BSET_FAB_DEVID 768 | SD_CONF_BSET_NOCACHE 769 | SD_CONF_BSET_BSY_RETRY_COUNT 770 | SD_CONF_BSET_PLAYMSF_BCD 771 | SD_CONF_BSET_READSUB_BCD 772 | SD_CONF_BSET_READ_TOC_TRK_BCD 773 | SD_CONF_BSET_READ_TOC_ADDR_BCD 774 | SD_CONF_BSET_NO_READ_HEADER 775 | SD_CONF_BSET_READ_CD_XD4 776 | SD_CONF_BSET_RST_RETRIES 777 | SD_CONF_BSET_RSV_REL_TIME 778 | SD_CONF_BSET_TUR_CHECK), &tst_properties}, 779 #endif 780 }; 781 782 static const int sd_disk_table_size = 783 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t); 784 785 786 /* 787 * Return codes of sd_uselabel(). 788 */ 789 #define SD_LABEL_IS_VALID 0 790 #define SD_LABEL_IS_INVALID 1 791 792 #define SD_INTERCONNECT_PARALLEL 0 793 #define SD_INTERCONNECT_FABRIC 1 794 #define SD_INTERCONNECT_FIBRE 2 795 #define SD_INTERCONNECT_SSA 3 796 #define SD_INTERCONNECT_SATA 4 797 #define SD_IS_PARALLEL_SCSI(un) \ 798 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL) 799 #define SD_IS_SERIAL(un) \ 800 ((un)->un_interconnect_type == SD_INTERCONNECT_SATA) 801 802 /* 803 * Definitions used by device id registration routines 804 */ 805 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */ 806 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */ 807 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */ 808 #define WD_NODE 7 /* the whole disk minor */ 809 810 static kmutex_t sd_sense_mutex = {0}; 811 812 /* 813 * Macros for updates of the driver state 814 */ 815 #define New_state(un, s) \ 816 (un)->un_last_state = (un)->un_state, (un)->un_state = (s) 817 #define Restore_state(un) \ 818 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); } 819 820 static struct sd_cdbinfo sd_cdbtab[] = { 821 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, }, 822 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, }, 823 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, }, 824 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, }, 825 }; 826 827 /* 828 * Specifies the number of seconds that must have elapsed since the last 829 * cmd. has completed for a device to be declared idle to the PM framework. 830 */ 831 static int sd_pm_idletime = 1; 832 833 /* 834 * Internal function prototypes 835 */ 836 837 #if (defined(__fibre)) 838 /* 839 * These #defines are to avoid namespace collisions that occur because this 840 * code is currently used to compile two seperate driver modules: sd and ssd. 841 * All function names need to be treated this way (even if declared static) 842 * in order to allow the debugger to resolve the names properly. 843 * It is anticipated that in the near future the ssd module will be obsoleted, 844 * at which time this ugliness should go away. 845 */ 846 #define sd_log_trace ssd_log_trace 847 #define sd_log_info ssd_log_info 848 #define sd_log_err ssd_log_err 849 #define sdprobe ssdprobe 850 #define sdinfo ssdinfo 851 #define sd_prop_op ssd_prop_op 852 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init 853 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini 854 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache 855 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache 856 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init 857 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini 858 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count 859 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target 860 #define sd_spin_up_unit ssd_spin_up_unit 861 #define sd_enable_descr_sense ssd_enable_descr_sense 862 #define sd_reenable_dsense_task ssd_reenable_dsense_task 863 #define sd_set_mmc_caps ssd_set_mmc_caps 864 #define sd_read_unit_properties ssd_read_unit_properties 865 #define sd_process_sdconf_file ssd_process_sdconf_file 866 #define sd_process_sdconf_table ssd_process_sdconf_table 867 #define sd_sdconf_id_match ssd_sdconf_id_match 868 #define sd_blank_cmp ssd_blank_cmp 869 #define sd_chk_vers1_data ssd_chk_vers1_data 870 #define sd_set_vers1_properties ssd_set_vers1_properties 871 #define sd_validate_geometry ssd_validate_geometry 872 873 #if defined(_SUNOS_VTOC_16) 874 #define sd_convert_geometry ssd_convert_geometry 875 #endif 876 877 #define sd_resync_geom_caches ssd_resync_geom_caches 878 #define sd_read_fdisk ssd_read_fdisk 879 #define sd_get_physical_geometry ssd_get_physical_geometry 880 #define sd_get_virtual_geometry ssd_get_virtual_geometry 881 #define sd_update_block_info ssd_update_block_info 882 #define sd_swap_efi_gpt ssd_swap_efi_gpt 883 #define sd_swap_efi_gpe ssd_swap_efi_gpe 884 #define sd_validate_efi ssd_validate_efi 885 #define sd_use_efi ssd_use_efi 886 #define sd_uselabel ssd_uselabel 887 #define sd_build_default_label ssd_build_default_label 888 #define sd_has_max_chs_vals ssd_has_max_chs_vals 889 #define sd_inq_fill ssd_inq_fill 890 #define sd_register_devid ssd_register_devid 891 #define sd_get_devid_block ssd_get_devid_block 892 #define sd_get_devid ssd_get_devid 893 #define sd_create_devid ssd_create_devid 894 #define sd_write_deviceid ssd_write_deviceid 895 #define sd_check_vpd_page_support ssd_check_vpd_page_support 896 #define sd_setup_pm ssd_setup_pm 897 #define sd_create_pm_components ssd_create_pm_components 898 #define sd_ddi_suspend ssd_ddi_suspend 899 #define sd_ddi_pm_suspend ssd_ddi_pm_suspend 900 #define sd_ddi_resume ssd_ddi_resume 901 #define sd_ddi_pm_resume ssd_ddi_pm_resume 902 #define sdpower ssdpower 903 #define sdattach ssdattach 904 #define sddetach ssddetach 905 #define sd_unit_attach ssd_unit_attach 906 #define sd_unit_detach ssd_unit_detach 907 #define sd_set_unit_attributes ssd_set_unit_attributes 908 #define sd_create_minor_nodes ssd_create_minor_nodes 909 #define sd_create_errstats ssd_create_errstats 910 #define sd_set_errstats ssd_set_errstats 911 #define sd_set_pstats ssd_set_pstats 912 #define sddump ssddump 913 #define sd_scsi_poll ssd_scsi_poll 914 #define sd_send_polled_RQS ssd_send_polled_RQS 915 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll 916 #define sd_init_event_callbacks ssd_init_event_callbacks 917 #define sd_event_callback ssd_event_callback 918 #define sd_cache_control ssd_cache_control 919 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled 920 #define sd_make_device ssd_make_device 921 #define sdopen ssdopen 922 #define sdclose ssdclose 923 #define sd_ready_and_valid ssd_ready_and_valid 924 #define sdmin ssdmin 925 #define sdread ssdread 926 #define sdwrite ssdwrite 927 #define sdaread ssdaread 928 #define sdawrite ssdawrite 929 #define sdstrategy ssdstrategy 930 #define sdioctl ssdioctl 931 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart 932 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart 933 #define sd_checksum_iostart ssd_checksum_iostart 934 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart 935 #define sd_pm_iostart ssd_pm_iostart 936 #define sd_core_iostart ssd_core_iostart 937 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone 938 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone 939 #define sd_checksum_iodone ssd_checksum_iodone 940 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone 941 #define sd_pm_iodone ssd_pm_iodone 942 #define sd_initpkt_for_buf ssd_initpkt_for_buf 943 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf 944 #define sd_setup_rw_pkt ssd_setup_rw_pkt 945 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt 946 #define sd_buf_iodone ssd_buf_iodone 947 #define sd_uscsi_strategy ssd_uscsi_strategy 948 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi 949 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi 950 #define sd_uscsi_iodone ssd_uscsi_iodone 951 #define sd_xbuf_strategy ssd_xbuf_strategy 952 #define sd_xbuf_init ssd_xbuf_init 953 #define sd_pm_entry ssd_pm_entry 954 #define sd_pm_exit ssd_pm_exit 955 956 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler 957 #define sd_pm_timeout_handler ssd_pm_timeout_handler 958 959 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq 960 #define sdintr ssdintr 961 #define sd_start_cmds ssd_start_cmds 962 #define sd_send_scsi_cmd ssd_send_scsi_cmd 963 #define sd_bioclone_alloc ssd_bioclone_alloc 964 #define sd_bioclone_free ssd_bioclone_free 965 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc 966 #define sd_shadow_buf_free ssd_shadow_buf_free 967 #define sd_print_transport_rejected_message \ 968 ssd_print_transport_rejected_message 969 #define sd_retry_command ssd_retry_command 970 #define sd_set_retry_bp ssd_set_retry_bp 971 #define sd_send_request_sense_command ssd_send_request_sense_command 972 #define sd_start_retry_command ssd_start_retry_command 973 #define sd_start_direct_priority_command \ 974 ssd_start_direct_priority_command 975 #define sd_return_failed_command ssd_return_failed_command 976 #define sd_return_failed_command_no_restart \ 977 ssd_return_failed_command_no_restart 978 #define sd_return_command ssd_return_command 979 #define sd_sync_with_callback ssd_sync_with_callback 980 #define sdrunout ssdrunout 981 #define sd_mark_rqs_busy ssd_mark_rqs_busy 982 #define sd_mark_rqs_idle ssd_mark_rqs_idle 983 #define sd_reduce_throttle ssd_reduce_throttle 984 #define sd_restore_throttle ssd_restore_throttle 985 #define sd_print_incomplete_msg ssd_print_incomplete_msg 986 #define sd_init_cdb_limits ssd_init_cdb_limits 987 #define sd_pkt_status_good ssd_pkt_status_good 988 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition 989 #define sd_pkt_status_busy ssd_pkt_status_busy 990 #define sd_pkt_status_reservation_conflict \ 991 ssd_pkt_status_reservation_conflict 992 #define sd_pkt_status_qfull ssd_pkt_status_qfull 993 #define sd_handle_request_sense ssd_handle_request_sense 994 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense 995 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg 996 #define sd_validate_sense_data ssd_validate_sense_data 997 #define sd_decode_sense ssd_decode_sense 998 #define sd_print_sense_msg ssd_print_sense_msg 999 #define sd_sense_key_no_sense ssd_sense_key_no_sense 1000 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error 1001 #define sd_sense_key_not_ready ssd_sense_key_not_ready 1002 #define sd_sense_key_medium_or_hardware_error \ 1003 ssd_sense_key_medium_or_hardware_error 1004 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request 1005 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention 1006 #define sd_sense_key_fail_command ssd_sense_key_fail_command 1007 #define sd_sense_key_blank_check ssd_sense_key_blank_check 1008 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command 1009 #define sd_sense_key_default ssd_sense_key_default 1010 #define sd_print_retry_msg ssd_print_retry_msg 1011 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg 1012 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete 1013 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err 1014 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset 1015 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted 1016 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout 1017 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free 1018 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject 1019 #define sd_pkt_reason_default ssd_pkt_reason_default 1020 #define sd_reset_target ssd_reset_target 1021 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback 1022 #define sd_start_stop_unit_task ssd_start_stop_unit_task 1023 #define sd_taskq_create ssd_taskq_create 1024 #define sd_taskq_delete ssd_taskq_delete 1025 #define sd_media_change_task ssd_media_change_task 1026 #define sd_handle_mchange ssd_handle_mchange 1027 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK 1028 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY 1029 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16 1030 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION 1031 #define sd_send_scsi_feature_GET_CONFIGURATION \ 1032 sd_send_scsi_feature_GET_CONFIGURATION 1033 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT 1034 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY 1035 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY 1036 #define sd_send_scsi_PERSISTENT_RESERVE_IN \ 1037 ssd_send_scsi_PERSISTENT_RESERVE_IN 1038 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \ 1039 ssd_send_scsi_PERSISTENT_RESERVE_OUT 1040 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE 1041 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \ 1042 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone 1043 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE 1044 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT 1045 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR 1046 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE 1047 #define sd_alloc_rqs ssd_alloc_rqs 1048 #define sd_free_rqs ssd_free_rqs 1049 #define sd_dump_memory ssd_dump_memory 1050 #define sd_uscsi_ioctl ssd_uscsi_ioctl 1051 #define sd_get_media_info ssd_get_media_info 1052 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info 1053 #define sd_dkio_get_geometry ssd_dkio_get_geometry 1054 #define sd_dkio_set_geometry ssd_dkio_set_geometry 1055 #define sd_dkio_get_partition ssd_dkio_get_partition 1056 #define sd_dkio_set_partition ssd_dkio_set_partition 1057 #define sd_dkio_partition ssd_dkio_partition 1058 #define sd_dkio_get_vtoc ssd_dkio_get_vtoc 1059 #define sd_dkio_get_efi ssd_dkio_get_efi 1060 #define sd_build_user_vtoc ssd_build_user_vtoc 1061 #define sd_dkio_set_vtoc ssd_dkio_set_vtoc 1062 #define sd_dkio_set_efi ssd_dkio_set_efi 1063 #define sd_build_label_vtoc ssd_build_label_vtoc 1064 #define sd_write_label ssd_write_label 1065 #define sd_clear_vtoc ssd_clear_vtoc 1066 #define sd_clear_efi ssd_clear_efi 1067 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf 1068 #define sd_setup_next_xfer ssd_setup_next_xfer 1069 #define sd_dkio_get_temp ssd_dkio_get_temp 1070 #define sd_dkio_get_mboot ssd_dkio_get_mboot 1071 #define sd_dkio_set_mboot ssd_dkio_set_mboot 1072 #define sd_setup_default_geometry ssd_setup_default_geometry 1073 #define sd_update_fdisk_and_vtoc ssd_update_fdisk_and_vtoc 1074 #define sd_check_mhd ssd_check_mhd 1075 #define sd_mhd_watch_cb ssd_mhd_watch_cb 1076 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete 1077 #define sd_sname ssd_sname 1078 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover 1079 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread 1080 #define sd_take_ownership ssd_take_ownership 1081 #define sd_reserve_release ssd_reserve_release 1082 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req 1083 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb 1084 #define sd_persistent_reservation_in_read_keys \ 1085 ssd_persistent_reservation_in_read_keys 1086 #define sd_persistent_reservation_in_read_resv \ 1087 ssd_persistent_reservation_in_read_resv 1088 #define sd_mhdioc_takeown ssd_mhdioc_takeown 1089 #define sd_mhdioc_failfast ssd_mhdioc_failfast 1090 #define sd_mhdioc_release ssd_mhdioc_release 1091 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid 1092 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys 1093 #define sd_mhdioc_inresv ssd_mhdioc_inresv 1094 #define sr_change_blkmode ssr_change_blkmode 1095 #define sr_change_speed ssr_change_speed 1096 #define sr_atapi_change_speed ssr_atapi_change_speed 1097 #define sr_pause_resume ssr_pause_resume 1098 #define sr_play_msf ssr_play_msf 1099 #define sr_play_trkind ssr_play_trkind 1100 #define sr_read_all_subcodes ssr_read_all_subcodes 1101 #define sr_read_subchannel ssr_read_subchannel 1102 #define sr_read_tocentry ssr_read_tocentry 1103 #define sr_read_tochdr ssr_read_tochdr 1104 #define sr_read_cdda ssr_read_cdda 1105 #define sr_read_cdxa ssr_read_cdxa 1106 #define sr_read_mode1 ssr_read_mode1 1107 #define sr_read_mode2 ssr_read_mode2 1108 #define sr_read_cd_mode2 ssr_read_cd_mode2 1109 #define sr_sector_mode ssr_sector_mode 1110 #define sr_eject ssr_eject 1111 #define sr_ejected ssr_ejected 1112 #define sr_check_wp ssr_check_wp 1113 #define sd_check_media ssd_check_media 1114 #define sd_media_watch_cb ssd_media_watch_cb 1115 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast 1116 #define sr_volume_ctrl ssr_volume_ctrl 1117 #define sr_read_sony_session_offset ssr_read_sony_session_offset 1118 #define sd_log_page_supported ssd_log_page_supported 1119 #define sd_check_for_writable_cd ssd_check_for_writable_cd 1120 #define sd_wm_cache_constructor ssd_wm_cache_constructor 1121 #define sd_wm_cache_destructor ssd_wm_cache_destructor 1122 #define sd_range_lock ssd_range_lock 1123 #define sd_get_range ssd_get_range 1124 #define sd_free_inlist_wmap ssd_free_inlist_wmap 1125 #define sd_range_unlock ssd_range_unlock 1126 #define sd_read_modify_write_task ssd_read_modify_write_task 1127 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw 1128 1129 #define sd_iostart_chain ssd_iostart_chain 1130 #define sd_iodone_chain ssd_iodone_chain 1131 #define sd_initpkt_map ssd_initpkt_map 1132 #define sd_destroypkt_map ssd_destroypkt_map 1133 #define sd_chain_type_map ssd_chain_type_map 1134 #define sd_chain_index_map ssd_chain_index_map 1135 1136 #define sd_failfast_flushctl ssd_failfast_flushctl 1137 #define sd_failfast_flushq ssd_failfast_flushq 1138 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback 1139 1140 #define sd_is_lsi ssd_is_lsi 1141 1142 #endif /* #if (defined(__fibre)) */ 1143 1144 1145 int _init(void); 1146 int _fini(void); 1147 int _info(struct modinfo *modinfop); 1148 1149 /*PRINTFLIKE3*/ 1150 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1151 /*PRINTFLIKE3*/ 1152 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1153 /*PRINTFLIKE3*/ 1154 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...); 1155 1156 static int sdprobe(dev_info_t *devi); 1157 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 1158 void **result); 1159 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1160 int mod_flags, char *name, caddr_t valuep, int *lengthp); 1161 1162 /* 1163 * Smart probe for parallel scsi 1164 */ 1165 static void sd_scsi_probe_cache_init(void); 1166 static void sd_scsi_probe_cache_fini(void); 1167 static void sd_scsi_clear_probe_cache(void); 1168 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)()); 1169 1170 /* 1171 * Attached luns on target for parallel scsi 1172 */ 1173 static void sd_scsi_target_lun_init(void); 1174 static void sd_scsi_target_lun_fini(void); 1175 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target); 1176 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag); 1177 1178 static int sd_spin_up_unit(struct sd_lun *un); 1179 #ifdef _LP64 1180 static void sd_enable_descr_sense(struct sd_lun *un); 1181 static void sd_reenable_dsense_task(void *arg); 1182 #endif /* _LP64 */ 1183 1184 static void sd_set_mmc_caps(struct sd_lun *un); 1185 1186 static void sd_read_unit_properties(struct sd_lun *un); 1187 static int sd_process_sdconf_file(struct sd_lun *un); 1188 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, 1189 int *data_list, sd_tunables *values); 1190 static void sd_process_sdconf_table(struct sd_lun *un); 1191 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen); 1192 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen); 1193 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 1194 int list_len, char *dataname_ptr); 1195 static void sd_set_vers1_properties(struct sd_lun *un, int flags, 1196 sd_tunables *prop_list); 1197 static int sd_validate_geometry(struct sd_lun *un, int path_flag); 1198 1199 #if defined(_SUNOS_VTOC_16) 1200 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g); 1201 #endif 1202 1203 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 1204 int path_flag); 1205 static int sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, 1206 int path_flag); 1207 static void sd_get_physical_geometry(struct sd_lun *un, 1208 struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag); 1209 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity, 1210 int lbasize); 1211 static int sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag); 1212 static void sd_swap_efi_gpt(efi_gpt_t *); 1213 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *); 1214 static int sd_validate_efi(efi_gpt_t *); 1215 static int sd_use_efi(struct sd_lun *, int); 1216 static void sd_build_default_label(struct sd_lun *un); 1217 1218 #if defined(_FIRMWARE_NEEDS_FDISK) 1219 static int sd_has_max_chs_vals(struct ipart *fdp); 1220 #endif 1221 static void sd_inq_fill(char *p, int l, char *s); 1222 1223 1224 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi, 1225 int reservation_flag); 1226 static daddr_t sd_get_devid_block(struct sd_lun *un); 1227 static int sd_get_devid(struct sd_lun *un); 1228 static int sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len); 1229 static ddi_devid_t sd_create_devid(struct sd_lun *un); 1230 static int sd_write_deviceid(struct sd_lun *un); 1231 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len); 1232 static int sd_check_vpd_page_support(struct sd_lun *un); 1233 1234 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi); 1235 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un); 1236 1237 static int sd_ddi_suspend(dev_info_t *devi); 1238 static int sd_ddi_pm_suspend(struct sd_lun *un); 1239 static int sd_ddi_resume(dev_info_t *devi); 1240 static int sd_ddi_pm_resume(struct sd_lun *un); 1241 static int sdpower(dev_info_t *devi, int component, int level); 1242 1243 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1244 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1245 static int sd_unit_attach(dev_info_t *devi); 1246 static int sd_unit_detach(dev_info_t *devi); 1247 1248 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi); 1249 static int sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi); 1250 static void sd_create_errstats(struct sd_lun *un, int instance); 1251 static void sd_set_errstats(struct sd_lun *un); 1252 static void sd_set_pstats(struct sd_lun *un); 1253 1254 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk); 1255 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt); 1256 static int sd_send_polled_RQS(struct sd_lun *un); 1257 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt); 1258 1259 #if (defined(__fibre)) 1260 /* 1261 * Event callbacks (photon) 1262 */ 1263 static void sd_init_event_callbacks(struct sd_lun *un); 1264 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *); 1265 #endif 1266 1267 /* 1268 * Defines for sd_cache_control 1269 */ 1270 1271 #define SD_CACHE_ENABLE 1 1272 #define SD_CACHE_DISABLE 0 1273 #define SD_CACHE_NOCHANGE -1 1274 1275 static int sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag); 1276 static int sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled); 1277 static dev_t sd_make_device(dev_info_t *devi); 1278 1279 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize, 1280 uint64_t capacity); 1281 1282 /* 1283 * Driver entry point functions. 1284 */ 1285 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p); 1286 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p); 1287 static int sd_ready_and_valid(struct sd_lun *un); 1288 1289 static void sdmin(struct buf *bp); 1290 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p); 1291 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p); 1292 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1293 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 1294 1295 static int sdstrategy(struct buf *bp); 1296 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *); 1297 1298 /* 1299 * Function prototypes for layering functions in the iostart chain. 1300 */ 1301 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un, 1302 struct buf *bp); 1303 static void sd_mapblocksize_iostart(int index, struct sd_lun *un, 1304 struct buf *bp); 1305 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp); 1306 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un, 1307 struct buf *bp); 1308 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp); 1309 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp); 1310 1311 /* 1312 * Function prototypes for layering functions in the iodone chain. 1313 */ 1314 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp); 1315 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp); 1316 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un, 1317 struct buf *bp); 1318 static void sd_mapblocksize_iodone(int index, struct sd_lun *un, 1319 struct buf *bp); 1320 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp); 1321 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un, 1322 struct buf *bp); 1323 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp); 1324 1325 /* 1326 * Prototypes for functions to support buf(9S) based IO. 1327 */ 1328 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg); 1329 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **); 1330 static void sd_destroypkt_for_buf(struct buf *); 1331 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp, 1332 struct buf *bp, int flags, 1333 int (*callback)(caddr_t), caddr_t callback_arg, 1334 diskaddr_t lba, uint32_t blockcount); 1335 #if defined(__i386) || defined(__amd64) 1336 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp, 1337 struct buf *bp, diskaddr_t lba, uint32_t blockcount); 1338 #endif /* defined(__i386) || defined(__amd64) */ 1339 1340 /* 1341 * Prototypes for functions to support USCSI IO. 1342 */ 1343 static int sd_uscsi_strategy(struct buf *bp); 1344 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **); 1345 static void sd_destroypkt_for_uscsi(struct buf *); 1346 1347 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 1348 uchar_t chain_type, void *pktinfop); 1349 1350 static int sd_pm_entry(struct sd_lun *un); 1351 static void sd_pm_exit(struct sd_lun *un); 1352 1353 static void sd_pm_idletimeout_handler(void *arg); 1354 1355 /* 1356 * sd_core internal functions (used at the sd_core_io layer). 1357 */ 1358 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp); 1359 static void sdintr(struct scsi_pkt *pktp); 1360 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp); 1361 1362 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 1363 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 1364 int path_flag); 1365 1366 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen, 1367 daddr_t blkno, int (*func)(struct buf *)); 1368 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen, 1369 uint_t bflags, daddr_t blkno, int (*func)(struct buf *)); 1370 static void sd_bioclone_free(struct buf *bp); 1371 static void sd_shadow_buf_free(struct buf *bp); 1372 1373 static void sd_print_transport_rejected_message(struct sd_lun *un, 1374 struct sd_xbuf *xp, int code); 1375 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, 1376 void *arg, int code); 1377 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, 1378 void *arg, int code); 1379 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, 1380 void *arg, int code); 1381 1382 static void sd_retry_command(struct sd_lun *un, struct buf *bp, 1383 int retry_check_flag, 1384 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, 1385 int c), 1386 void *user_arg, int failure_code, clock_t retry_delay, 1387 void (*statp)(kstat_io_t *)); 1388 1389 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp, 1390 clock_t retry_delay, void (*statp)(kstat_io_t *)); 1391 1392 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 1393 struct scsi_pkt *pktp); 1394 static void sd_start_retry_command(void *arg); 1395 static void sd_start_direct_priority_command(void *arg); 1396 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp, 1397 int errcode); 1398 static void sd_return_failed_command_no_restart(struct sd_lun *un, 1399 struct buf *bp, int errcode); 1400 static void sd_return_command(struct sd_lun *un, struct buf *bp); 1401 static void sd_sync_with_callback(struct sd_lun *un); 1402 static int sdrunout(caddr_t arg); 1403 1404 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp); 1405 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp); 1406 1407 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type); 1408 static void sd_restore_throttle(void *arg); 1409 1410 static void sd_init_cdb_limits(struct sd_lun *un); 1411 1412 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 1413 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1414 1415 /* 1416 * Error handling functions 1417 */ 1418 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 1419 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1420 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, 1421 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1422 static void sd_pkt_status_reservation_conflict(struct sd_lun *un, 1423 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1424 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 1425 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1426 1427 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp, 1428 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1429 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 1430 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1431 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp, 1432 struct sd_xbuf *xp); 1433 static void sd_decode_sense(struct sd_lun *un, struct buf *bp, 1434 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1435 1436 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp, 1437 void *arg, int code); 1438 1439 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 1440 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1441 static void sd_sense_key_recoverable_error(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_not_ready(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_medium_or_hardware_error(struct sd_lun *un, 1448 uint8_t *sense_datap, 1449 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1450 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 1451 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1452 static void sd_sense_key_unit_attention(struct sd_lun *un, 1453 uint8_t *sense_datap, 1454 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1455 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 1456 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1457 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 1458 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1459 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 1460 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1461 static void sd_sense_key_default(struct sd_lun *un, 1462 uint8_t *sense_datap, 1463 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp); 1464 1465 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp, 1466 void *arg, int flag); 1467 1468 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 1469 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1470 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 1471 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1472 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 1473 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1474 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 1475 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1476 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 1477 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1478 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 1479 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1480 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 1481 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1482 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 1483 struct sd_xbuf *xp, struct scsi_pkt *pktp); 1484 1485 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp); 1486 1487 static void sd_start_stop_unit_callback(void *arg); 1488 static void sd_start_stop_unit_task(void *arg); 1489 1490 static void sd_taskq_create(void); 1491 static void sd_taskq_delete(void); 1492 static void sd_media_change_task(void *arg); 1493 1494 static int sd_handle_mchange(struct sd_lun *un); 1495 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag); 1496 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, 1497 uint32_t *lbap, int path_flag); 1498 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 1499 uint32_t *lbap, int path_flag); 1500 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, 1501 int path_flag); 1502 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, 1503 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp); 1504 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag); 1505 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, 1506 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp); 1507 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, 1508 uchar_t usr_cmd, uchar_t *usr_bufp); 1509 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, 1510 struct dk_callback *dkc); 1511 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp); 1512 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, 1513 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1514 uchar_t *bufaddr, uint_t buflen); 1515 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 1516 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 1517 uchar_t *bufaddr, uint_t buflen, char feature); 1518 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, 1519 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag); 1520 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, 1521 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag); 1522 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 1523 size_t buflen, daddr_t start_block, int path_flag); 1524 #define sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag) \ 1525 sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \ 1526 path_flag) 1527 #define sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag) \ 1528 sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\ 1529 path_flag) 1530 1531 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, 1532 uint16_t buflen, uchar_t page_code, uchar_t page_control, 1533 uint16_t param_ptr, int path_flag); 1534 1535 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un); 1536 static void sd_free_rqs(struct sd_lun *un); 1537 1538 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, 1539 uchar_t *data, int len, int fmt); 1540 static void sd_panic_for_res_conflict(struct sd_lun *un); 1541 1542 /* 1543 * Disk Ioctl Function Prototypes 1544 */ 1545 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag); 1546 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag); 1547 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag); 1548 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, 1549 int geom_validated); 1550 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag); 1551 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, 1552 int geom_validated); 1553 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag); 1554 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, 1555 int geom_validated); 1556 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag); 1557 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag); 1558 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1559 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag); 1560 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag); 1561 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc); 1562 static int sd_write_label(dev_t dev); 1563 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl); 1564 static void sd_clear_vtoc(struct sd_lun *un); 1565 static void sd_clear_efi(struct sd_lun *un); 1566 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag); 1567 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag); 1568 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag); 1569 static void sd_setup_default_geometry(struct sd_lun *un); 1570 #if defined(__i386) || defined(__amd64) 1571 static int sd_update_fdisk_and_vtoc(struct sd_lun *un); 1572 #endif 1573 1574 /* 1575 * Multi-host Ioctl Prototypes 1576 */ 1577 static int sd_check_mhd(dev_t dev, int interval); 1578 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1579 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt); 1580 static char *sd_sname(uchar_t status); 1581 static void sd_mhd_resvd_recover(void *arg); 1582 static void sd_resv_reclaim_thread(); 1583 static int sd_take_ownership(dev_t dev, struct mhioctkown *p); 1584 static int sd_reserve_release(dev_t dev, int cmd); 1585 static void sd_rmv_resv_reclaim_req(dev_t dev); 1586 static void sd_mhd_reset_notify_cb(caddr_t arg); 1587 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un, 1588 mhioc_inkeys_t *usrp, int flag); 1589 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un, 1590 mhioc_inresvs_t *usrp, int flag); 1591 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag); 1592 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag); 1593 static int sd_mhdioc_release(dev_t dev); 1594 static int sd_mhdioc_register_devid(dev_t dev); 1595 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag); 1596 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag); 1597 1598 /* 1599 * SCSI removable prototypes 1600 */ 1601 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag); 1602 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1603 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag); 1604 static int sr_pause_resume(dev_t dev, int mode); 1605 static int sr_play_msf(dev_t dev, caddr_t data, int flag); 1606 static int sr_play_trkind(dev_t dev, caddr_t data, int flag); 1607 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag); 1608 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag); 1609 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag); 1610 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag); 1611 static int sr_read_cdda(dev_t dev, caddr_t data, int flag); 1612 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag); 1613 static int sr_read_mode1(dev_t dev, caddr_t data, int flag); 1614 static int sr_read_mode2(dev_t dev, caddr_t data, int flag); 1615 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag); 1616 static int sr_sector_mode(dev_t dev, uint32_t blksize); 1617 static int sr_eject(dev_t dev); 1618 static void sr_ejected(register struct sd_lun *un); 1619 static int sr_check_wp(dev_t dev); 1620 static int sd_check_media(dev_t dev, enum dkio_state state); 1621 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 1622 static void sd_delayed_cv_broadcast(void *arg); 1623 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag); 1624 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag); 1625 1626 static int sd_log_page_supported(struct sd_lun *un, int log_page); 1627 1628 /* 1629 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions. 1630 */ 1631 static void sd_check_for_writable_cd(struct sd_lun *un); 1632 static int sd_wm_cache_constructor(void *wm, void *un, int flags); 1633 static void sd_wm_cache_destructor(void *wm, void *un); 1634 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb, 1635 daddr_t endb, ushort_t typ); 1636 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb, 1637 daddr_t endb); 1638 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp); 1639 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm); 1640 static void sd_read_modify_write_task(void * arg); 1641 static int 1642 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 1643 struct buf **bpp); 1644 1645 1646 /* 1647 * Function prototypes for failfast support. 1648 */ 1649 static void sd_failfast_flushq(struct sd_lun *un); 1650 static int sd_failfast_flushq_callback(struct buf *bp); 1651 1652 /* 1653 * Function prototypes to check for lsi devices 1654 */ 1655 static void sd_is_lsi(struct sd_lun *un); 1656 1657 /* 1658 * Function prototypes for x86 support 1659 */ 1660 #if defined(__i386) || defined(__amd64) 1661 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 1662 struct scsi_pkt *pkt, struct sd_xbuf *xp); 1663 #endif 1664 1665 /* 1666 * Constants for failfast support: 1667 * 1668 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO 1669 * failfast processing being performed. 1670 * 1671 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing 1672 * failfast processing on all bufs with B_FAILFAST set. 1673 */ 1674 1675 #define SD_FAILFAST_INACTIVE 0 1676 #define SD_FAILFAST_ACTIVE 1 1677 1678 /* 1679 * Bitmask to control behavior of buf(9S) flushes when a transition to 1680 * the failfast state occurs. Optional bits include: 1681 * 1682 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that 1683 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will 1684 * be flushed. 1685 * 1686 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the 1687 * driver, in addition to the regular wait queue. This includes the xbuf 1688 * queues. When clear, only the driver's wait queue will be flushed. 1689 */ 1690 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01 1691 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02 1692 1693 /* 1694 * The default behavior is to only flush bufs that have B_FAILFAST set, but 1695 * to flush all queues within the driver. 1696 */ 1697 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES; 1698 1699 1700 /* 1701 * SD Testing Fault Injection 1702 */ 1703 #ifdef SD_FAULT_INJECTION 1704 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un); 1705 static void sd_faultinjection(struct scsi_pkt *pktp); 1706 static void sd_injection_log(char *buf, struct sd_lun *un); 1707 #endif 1708 1709 /* 1710 * Device driver ops vector 1711 */ 1712 static struct cb_ops sd_cb_ops = { 1713 sdopen, /* open */ 1714 sdclose, /* close */ 1715 sdstrategy, /* strategy */ 1716 nodev, /* print */ 1717 sddump, /* dump */ 1718 sdread, /* read */ 1719 sdwrite, /* write */ 1720 sdioctl, /* ioctl */ 1721 nodev, /* devmap */ 1722 nodev, /* mmap */ 1723 nodev, /* segmap */ 1724 nochpoll, /* poll */ 1725 sd_prop_op, /* cb_prop_op */ 1726 0, /* streamtab */ 1727 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */ 1728 CB_REV, /* cb_rev */ 1729 sdaread, /* async I/O read entry point */ 1730 sdawrite /* async I/O write entry point */ 1731 }; 1732 1733 static struct dev_ops sd_ops = { 1734 DEVO_REV, /* devo_rev, */ 1735 0, /* refcnt */ 1736 sdinfo, /* info */ 1737 nulldev, /* identify */ 1738 sdprobe, /* probe */ 1739 sdattach, /* attach */ 1740 sddetach, /* detach */ 1741 nodev, /* reset */ 1742 &sd_cb_ops, /* driver operations */ 1743 NULL, /* bus operations */ 1744 sdpower /* power */ 1745 }; 1746 1747 1748 /* 1749 * This is the loadable module wrapper. 1750 */ 1751 #include <sys/modctl.h> 1752 1753 static struct modldrv modldrv = { 1754 &mod_driverops, /* Type of module. This one is a driver */ 1755 SD_MODULE_NAME, /* Module name. */ 1756 &sd_ops /* driver ops */ 1757 }; 1758 1759 1760 static struct modlinkage modlinkage = { 1761 MODREV_1, 1762 &modldrv, 1763 NULL 1764 }; 1765 1766 1767 static struct scsi_asq_key_strings sd_additional_codes[] = { 1768 0x81, 0, "Logical Unit is Reserved", 1769 0x85, 0, "Audio Address Not Valid", 1770 0xb6, 0, "Media Load Mechanism Failed", 1771 0xB9, 0, "Audio Play Operation Aborted", 1772 0xbf, 0, "Buffer Overflow for Read All Subcodes Command", 1773 0x53, 2, "Medium removal prevented", 1774 0x6f, 0, "Authentication failed during key exchange", 1775 0x6f, 1, "Key not present", 1776 0x6f, 2, "Key not established", 1777 0x6f, 3, "Read without proper authentication", 1778 0x6f, 4, "Mismatched region to this logical unit", 1779 0x6f, 5, "Region reset count error", 1780 0xffff, 0x0, NULL 1781 }; 1782 1783 1784 /* 1785 * Struct for passing printing information for sense data messages 1786 */ 1787 struct sd_sense_info { 1788 int ssi_severity; 1789 int ssi_pfa_flag; 1790 }; 1791 1792 /* 1793 * Table of function pointers for iostart-side routines. Seperate "chains" 1794 * of layered function calls are formed by placing the function pointers 1795 * sequentially in the desired order. Functions are called according to an 1796 * incrementing table index ordering. The last function in each chain must 1797 * be sd_core_iostart(). The corresponding iodone-side routines are expected 1798 * in the sd_iodone_chain[] array. 1799 * 1800 * Note: It may seem more natural to organize both the iostart and iodone 1801 * functions together, into an array of structures (or some similar 1802 * organization) with a common index, rather than two seperate arrays which 1803 * must be maintained in synchronization. The purpose of this division is 1804 * to achiece improved performance: individual arrays allows for more 1805 * effective cache line utilization on certain platforms. 1806 */ 1807 1808 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp); 1809 1810 1811 static sd_chain_t sd_iostart_chain[] = { 1812 1813 /* Chain for buf IO for disk drive targets (PM enabled) */ 1814 sd_mapblockaddr_iostart, /* Index: 0 */ 1815 sd_pm_iostart, /* Index: 1 */ 1816 sd_core_iostart, /* Index: 2 */ 1817 1818 /* Chain for buf IO for disk drive targets (PM disabled) */ 1819 sd_mapblockaddr_iostart, /* Index: 3 */ 1820 sd_core_iostart, /* Index: 4 */ 1821 1822 /* Chain for buf IO for removable-media targets (PM enabled) */ 1823 sd_mapblockaddr_iostart, /* Index: 5 */ 1824 sd_mapblocksize_iostart, /* Index: 6 */ 1825 sd_pm_iostart, /* Index: 7 */ 1826 sd_core_iostart, /* Index: 8 */ 1827 1828 /* Chain for buf IO for removable-media targets (PM disabled) */ 1829 sd_mapblockaddr_iostart, /* Index: 9 */ 1830 sd_mapblocksize_iostart, /* Index: 10 */ 1831 sd_core_iostart, /* Index: 11 */ 1832 1833 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1834 sd_mapblockaddr_iostart, /* Index: 12 */ 1835 sd_checksum_iostart, /* Index: 13 */ 1836 sd_pm_iostart, /* Index: 14 */ 1837 sd_core_iostart, /* Index: 15 */ 1838 1839 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1840 sd_mapblockaddr_iostart, /* Index: 16 */ 1841 sd_checksum_iostart, /* Index: 17 */ 1842 sd_core_iostart, /* Index: 18 */ 1843 1844 /* Chain for USCSI commands (all targets) */ 1845 sd_pm_iostart, /* Index: 19 */ 1846 sd_core_iostart, /* Index: 20 */ 1847 1848 /* Chain for checksumming USCSI commands (all targets) */ 1849 sd_checksum_uscsi_iostart, /* Index: 21 */ 1850 sd_pm_iostart, /* Index: 22 */ 1851 sd_core_iostart, /* Index: 23 */ 1852 1853 /* Chain for "direct" USCSI commands (all targets) */ 1854 sd_core_iostart, /* Index: 24 */ 1855 1856 /* Chain for "direct priority" USCSI commands (all targets) */ 1857 sd_core_iostart, /* Index: 25 */ 1858 }; 1859 1860 /* 1861 * Macros to locate the first function of each iostart chain in the 1862 * sd_iostart_chain[] array. These are located by the index in the array. 1863 */ 1864 #define SD_CHAIN_DISK_IOSTART 0 1865 #define SD_CHAIN_DISK_IOSTART_NO_PM 3 1866 #define SD_CHAIN_RMMEDIA_IOSTART 5 1867 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9 1868 #define SD_CHAIN_CHKSUM_IOSTART 12 1869 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16 1870 #define SD_CHAIN_USCSI_CMD_IOSTART 19 1871 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21 1872 #define SD_CHAIN_DIRECT_CMD_IOSTART 24 1873 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25 1874 1875 1876 /* 1877 * Table of function pointers for the iodone-side routines for the driver- 1878 * internal layering mechanism. The calling sequence for iodone routines 1879 * uses a decrementing table index, so the last routine called in a chain 1880 * must be at the lowest array index location for that chain. The last 1881 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs) 1882 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering 1883 * of the functions in an iodone side chain must correspond to the ordering 1884 * of the iostart routines for that chain. Note that there is no iodone 1885 * side routine that corresponds to sd_core_iostart(), so there is no 1886 * entry in the table for this. 1887 */ 1888 1889 static sd_chain_t sd_iodone_chain[] = { 1890 1891 /* Chain for buf IO for disk drive targets (PM enabled) */ 1892 sd_buf_iodone, /* Index: 0 */ 1893 sd_mapblockaddr_iodone, /* Index: 1 */ 1894 sd_pm_iodone, /* Index: 2 */ 1895 1896 /* Chain for buf IO for disk drive targets (PM disabled) */ 1897 sd_buf_iodone, /* Index: 3 */ 1898 sd_mapblockaddr_iodone, /* Index: 4 */ 1899 1900 /* Chain for buf IO for removable-media targets (PM enabled) */ 1901 sd_buf_iodone, /* Index: 5 */ 1902 sd_mapblockaddr_iodone, /* Index: 6 */ 1903 sd_mapblocksize_iodone, /* Index: 7 */ 1904 sd_pm_iodone, /* Index: 8 */ 1905 1906 /* Chain for buf IO for removable-media targets (PM disabled) */ 1907 sd_buf_iodone, /* Index: 9 */ 1908 sd_mapblockaddr_iodone, /* Index: 10 */ 1909 sd_mapblocksize_iodone, /* Index: 11 */ 1910 1911 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1912 sd_buf_iodone, /* Index: 12 */ 1913 sd_mapblockaddr_iodone, /* Index: 13 */ 1914 sd_checksum_iodone, /* Index: 14 */ 1915 sd_pm_iodone, /* Index: 15 */ 1916 1917 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1918 sd_buf_iodone, /* Index: 16 */ 1919 sd_mapblockaddr_iodone, /* Index: 17 */ 1920 sd_checksum_iodone, /* Index: 18 */ 1921 1922 /* Chain for USCSI commands (non-checksum targets) */ 1923 sd_uscsi_iodone, /* Index: 19 */ 1924 sd_pm_iodone, /* Index: 20 */ 1925 1926 /* Chain for USCSI commands (checksum targets) */ 1927 sd_uscsi_iodone, /* Index: 21 */ 1928 sd_checksum_uscsi_iodone, /* Index: 22 */ 1929 sd_pm_iodone, /* Index: 22 */ 1930 1931 /* Chain for "direct" USCSI commands (all targets) */ 1932 sd_uscsi_iodone, /* Index: 24 */ 1933 1934 /* Chain for "direct priority" USCSI commands (all targets) */ 1935 sd_uscsi_iodone, /* Index: 25 */ 1936 }; 1937 1938 1939 /* 1940 * Macros to locate the "first" function in the sd_iodone_chain[] array for 1941 * each iodone-side chain. These are located by the array index, but as the 1942 * iodone side functions are called in a decrementing-index order, the 1943 * highest index number in each chain must be specified (as these correspond 1944 * to the first function in the iodone chain that will be called by the core 1945 * at IO completion time). 1946 */ 1947 1948 #define SD_CHAIN_DISK_IODONE 2 1949 #define SD_CHAIN_DISK_IODONE_NO_PM 4 1950 #define SD_CHAIN_RMMEDIA_IODONE 8 1951 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11 1952 #define SD_CHAIN_CHKSUM_IODONE 15 1953 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18 1954 #define SD_CHAIN_USCSI_CMD_IODONE 20 1955 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22 1956 #define SD_CHAIN_DIRECT_CMD_IODONE 24 1957 #define SD_CHAIN_PRIORITY_CMD_IODONE 25 1958 1959 1960 1961 1962 /* 1963 * Array to map a layering chain index to the appropriate initpkt routine. 1964 * The redundant entries are present so that the index used for accessing 1965 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 1966 * with this table as well. 1967 */ 1968 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **); 1969 1970 static sd_initpkt_t sd_initpkt_map[] = { 1971 1972 /* Chain for buf IO for disk drive targets (PM enabled) */ 1973 sd_initpkt_for_buf, /* Index: 0 */ 1974 sd_initpkt_for_buf, /* Index: 1 */ 1975 sd_initpkt_for_buf, /* Index: 2 */ 1976 1977 /* Chain for buf IO for disk drive targets (PM disabled) */ 1978 sd_initpkt_for_buf, /* Index: 3 */ 1979 sd_initpkt_for_buf, /* Index: 4 */ 1980 1981 /* Chain for buf IO for removable-media targets (PM enabled) */ 1982 sd_initpkt_for_buf, /* Index: 5 */ 1983 sd_initpkt_for_buf, /* Index: 6 */ 1984 sd_initpkt_for_buf, /* Index: 7 */ 1985 sd_initpkt_for_buf, /* Index: 8 */ 1986 1987 /* Chain for buf IO for removable-media targets (PM disabled) */ 1988 sd_initpkt_for_buf, /* Index: 9 */ 1989 sd_initpkt_for_buf, /* Index: 10 */ 1990 sd_initpkt_for_buf, /* Index: 11 */ 1991 1992 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 1993 sd_initpkt_for_buf, /* Index: 12 */ 1994 sd_initpkt_for_buf, /* Index: 13 */ 1995 sd_initpkt_for_buf, /* Index: 14 */ 1996 sd_initpkt_for_buf, /* Index: 15 */ 1997 1998 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 1999 sd_initpkt_for_buf, /* Index: 16 */ 2000 sd_initpkt_for_buf, /* Index: 17 */ 2001 sd_initpkt_for_buf, /* Index: 18 */ 2002 2003 /* Chain for USCSI commands (non-checksum targets) */ 2004 sd_initpkt_for_uscsi, /* Index: 19 */ 2005 sd_initpkt_for_uscsi, /* Index: 20 */ 2006 2007 /* Chain for USCSI commands (checksum targets) */ 2008 sd_initpkt_for_uscsi, /* Index: 21 */ 2009 sd_initpkt_for_uscsi, /* Index: 22 */ 2010 sd_initpkt_for_uscsi, /* Index: 22 */ 2011 2012 /* Chain for "direct" USCSI commands (all targets) */ 2013 sd_initpkt_for_uscsi, /* Index: 24 */ 2014 2015 /* Chain for "direct priority" USCSI commands (all targets) */ 2016 sd_initpkt_for_uscsi, /* Index: 25 */ 2017 2018 }; 2019 2020 2021 /* 2022 * Array to map a layering chain index to the appropriate destroypktpkt routine. 2023 * The redundant entries are present so that the index used for accessing 2024 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2025 * with this table as well. 2026 */ 2027 typedef void (*sd_destroypkt_t)(struct buf *); 2028 2029 static sd_destroypkt_t sd_destroypkt_map[] = { 2030 2031 /* Chain for buf IO for disk drive targets (PM enabled) */ 2032 sd_destroypkt_for_buf, /* Index: 0 */ 2033 sd_destroypkt_for_buf, /* Index: 1 */ 2034 sd_destroypkt_for_buf, /* Index: 2 */ 2035 2036 /* Chain for buf IO for disk drive targets (PM disabled) */ 2037 sd_destroypkt_for_buf, /* Index: 3 */ 2038 sd_destroypkt_for_buf, /* Index: 4 */ 2039 2040 /* Chain for buf IO for removable-media targets (PM enabled) */ 2041 sd_destroypkt_for_buf, /* Index: 5 */ 2042 sd_destroypkt_for_buf, /* Index: 6 */ 2043 sd_destroypkt_for_buf, /* Index: 7 */ 2044 sd_destroypkt_for_buf, /* Index: 8 */ 2045 2046 /* Chain for buf IO for removable-media targets (PM disabled) */ 2047 sd_destroypkt_for_buf, /* Index: 9 */ 2048 sd_destroypkt_for_buf, /* Index: 10 */ 2049 sd_destroypkt_for_buf, /* Index: 11 */ 2050 2051 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2052 sd_destroypkt_for_buf, /* Index: 12 */ 2053 sd_destroypkt_for_buf, /* Index: 13 */ 2054 sd_destroypkt_for_buf, /* Index: 14 */ 2055 sd_destroypkt_for_buf, /* Index: 15 */ 2056 2057 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2058 sd_destroypkt_for_buf, /* Index: 16 */ 2059 sd_destroypkt_for_buf, /* Index: 17 */ 2060 sd_destroypkt_for_buf, /* Index: 18 */ 2061 2062 /* Chain for USCSI commands (non-checksum targets) */ 2063 sd_destroypkt_for_uscsi, /* Index: 19 */ 2064 sd_destroypkt_for_uscsi, /* Index: 20 */ 2065 2066 /* Chain for USCSI commands (checksum targets) */ 2067 sd_destroypkt_for_uscsi, /* Index: 21 */ 2068 sd_destroypkt_for_uscsi, /* Index: 22 */ 2069 sd_destroypkt_for_uscsi, /* Index: 22 */ 2070 2071 /* Chain for "direct" USCSI commands (all targets) */ 2072 sd_destroypkt_for_uscsi, /* Index: 24 */ 2073 2074 /* Chain for "direct priority" USCSI commands (all targets) */ 2075 sd_destroypkt_for_uscsi, /* Index: 25 */ 2076 2077 }; 2078 2079 2080 2081 /* 2082 * Array to map a layering chain index to the appropriate chain "type". 2083 * The chain type indicates a specific property/usage of the chain. 2084 * The redundant entries are present so that the index used for accessing 2085 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly 2086 * with this table as well. 2087 */ 2088 2089 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */ 2090 #define SD_CHAIN_BUFIO 1 /* regular buf IO */ 2091 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */ 2092 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */ 2093 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */ 2094 /* (for error recovery) */ 2095 2096 static int sd_chain_type_map[] = { 2097 2098 /* Chain for buf IO for disk drive targets (PM enabled) */ 2099 SD_CHAIN_BUFIO, /* Index: 0 */ 2100 SD_CHAIN_BUFIO, /* Index: 1 */ 2101 SD_CHAIN_BUFIO, /* Index: 2 */ 2102 2103 /* Chain for buf IO for disk drive targets (PM disabled) */ 2104 SD_CHAIN_BUFIO, /* Index: 3 */ 2105 SD_CHAIN_BUFIO, /* Index: 4 */ 2106 2107 /* Chain for buf IO for removable-media targets (PM enabled) */ 2108 SD_CHAIN_BUFIO, /* Index: 5 */ 2109 SD_CHAIN_BUFIO, /* Index: 6 */ 2110 SD_CHAIN_BUFIO, /* Index: 7 */ 2111 SD_CHAIN_BUFIO, /* Index: 8 */ 2112 2113 /* Chain for buf IO for removable-media targets (PM disabled) */ 2114 SD_CHAIN_BUFIO, /* Index: 9 */ 2115 SD_CHAIN_BUFIO, /* Index: 10 */ 2116 SD_CHAIN_BUFIO, /* Index: 11 */ 2117 2118 /* Chain for buf IO for disk drives with checksumming (PM enabled) */ 2119 SD_CHAIN_BUFIO, /* Index: 12 */ 2120 SD_CHAIN_BUFIO, /* Index: 13 */ 2121 SD_CHAIN_BUFIO, /* Index: 14 */ 2122 SD_CHAIN_BUFIO, /* Index: 15 */ 2123 2124 /* Chain for buf IO for disk drives with checksumming (PM disabled) */ 2125 SD_CHAIN_BUFIO, /* Index: 16 */ 2126 SD_CHAIN_BUFIO, /* Index: 17 */ 2127 SD_CHAIN_BUFIO, /* Index: 18 */ 2128 2129 /* Chain for USCSI commands (non-checksum targets) */ 2130 SD_CHAIN_USCSI, /* Index: 19 */ 2131 SD_CHAIN_USCSI, /* Index: 20 */ 2132 2133 /* Chain for USCSI commands (checksum targets) */ 2134 SD_CHAIN_USCSI, /* Index: 21 */ 2135 SD_CHAIN_USCSI, /* Index: 22 */ 2136 SD_CHAIN_USCSI, /* Index: 22 */ 2137 2138 /* Chain for "direct" USCSI commands (all targets) */ 2139 SD_CHAIN_DIRECT, /* Index: 24 */ 2140 2141 /* Chain for "direct priority" USCSI commands (all targets) */ 2142 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */ 2143 }; 2144 2145 2146 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */ 2147 #define SD_IS_BUFIO(xp) \ 2148 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO) 2149 2150 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */ 2151 #define SD_IS_DIRECT_PRIORITY(xp) \ 2152 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY) 2153 2154 2155 2156 /* 2157 * Struct, array, and macros to map a specific chain to the appropriate 2158 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays. 2159 * 2160 * The sd_chain_index_map[] array is used at attach time to set the various 2161 * un_xxx_chain type members of the sd_lun softstate to the specific layering 2162 * chain to be used with the instance. This allows different instances to use 2163 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart 2164 * and xb_chain_iodone index values in the sd_xbuf are initialized to these 2165 * values at sd_xbuf init time, this allows (1) layering chains may be changed 2166 * dynamically & without the use of locking; and (2) a layer may update the 2167 * xb_chain_io[start|done] member in a given xbuf with its current index value, 2168 * to allow for deferred processing of an IO within the same chain from a 2169 * different execution context. 2170 */ 2171 2172 struct sd_chain_index { 2173 int sci_iostart_index; 2174 int sci_iodone_index; 2175 }; 2176 2177 static struct sd_chain_index sd_chain_index_map[] = { 2178 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE }, 2179 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM }, 2180 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE }, 2181 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM }, 2182 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE }, 2183 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM }, 2184 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE }, 2185 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE }, 2186 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE }, 2187 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE }, 2188 }; 2189 2190 2191 /* 2192 * The following are indexes into the sd_chain_index_map[] array. 2193 */ 2194 2195 /* un->un_buf_chain_type must be set to one of these */ 2196 #define SD_CHAIN_INFO_DISK 0 2197 #define SD_CHAIN_INFO_DISK_NO_PM 1 2198 #define SD_CHAIN_INFO_RMMEDIA 2 2199 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3 2200 #define SD_CHAIN_INFO_CHKSUM 4 2201 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5 2202 2203 /* un->un_uscsi_chain_type must be set to one of these */ 2204 #define SD_CHAIN_INFO_USCSI_CMD 6 2205 /* USCSI with PM disabled is the same as DIRECT */ 2206 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8 2207 #define SD_CHAIN_INFO_USCSI_CHKSUM 7 2208 2209 /* un->un_direct_chain_type must be set to one of these */ 2210 #define SD_CHAIN_INFO_DIRECT_CMD 8 2211 2212 /* un->un_priority_chain_type must be set to one of these */ 2213 #define SD_CHAIN_INFO_PRIORITY_CMD 9 2214 2215 /* size for devid inquiries */ 2216 #define MAX_INQUIRY_SIZE 0xF0 2217 2218 /* 2219 * Macros used by functions to pass a given buf(9S) struct along to the 2220 * next function in the layering chain for further processing. 2221 * 2222 * In the following macros, passing more than three arguments to the called 2223 * routines causes the optimizer for the SPARC compiler to stop doing tail 2224 * call elimination which results in significant performance degradation. 2225 */ 2226 #define SD_BEGIN_IOSTART(index, un, bp) \ 2227 ((*(sd_iostart_chain[index]))(index, un, bp)) 2228 2229 #define SD_BEGIN_IODONE(index, un, bp) \ 2230 ((*(sd_iodone_chain[index]))(index, un, bp)) 2231 2232 #define SD_NEXT_IOSTART(index, un, bp) \ 2233 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp)) 2234 2235 #define SD_NEXT_IODONE(index, un, bp) \ 2236 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp)) 2237 2238 /* 2239 * Function: _init 2240 * 2241 * Description: This is the driver _init(9E) entry point. 2242 * 2243 * Return Code: Returns the value from mod_install(9F) or 2244 * ddi_soft_state_init(9F) as appropriate. 2245 * 2246 * Context: Called when driver module loaded. 2247 */ 2248 2249 int 2250 _init(void) 2251 { 2252 int err; 2253 2254 /* establish driver name from module name */ 2255 sd_label = mod_modname(&modlinkage); 2256 2257 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun), 2258 SD_MAXUNIT); 2259 2260 if (err != 0) { 2261 return (err); 2262 } 2263 2264 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL); 2265 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL); 2266 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL); 2267 2268 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL); 2269 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL); 2270 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL); 2271 2272 /* 2273 * it's ok to init here even for fibre device 2274 */ 2275 sd_scsi_probe_cache_init(); 2276 2277 sd_scsi_target_lun_init(); 2278 2279 /* 2280 * Creating taskq before mod_install ensures that all callers (threads) 2281 * that enter the module after a successfull mod_install encounter 2282 * a valid taskq. 2283 */ 2284 sd_taskq_create(); 2285 2286 err = mod_install(&modlinkage); 2287 if (err != 0) { 2288 /* delete taskq if install fails */ 2289 sd_taskq_delete(); 2290 2291 mutex_destroy(&sd_detach_mutex); 2292 mutex_destroy(&sd_log_mutex); 2293 mutex_destroy(&sd_label_mutex); 2294 2295 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2296 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2297 cv_destroy(&sd_tr.srq_inprocess_cv); 2298 2299 sd_scsi_probe_cache_fini(); 2300 2301 sd_scsi_target_lun_fini(); 2302 2303 ddi_soft_state_fini(&sd_state); 2304 return (err); 2305 } 2306 2307 return (err); 2308 } 2309 2310 2311 /* 2312 * Function: _fini 2313 * 2314 * Description: This is the driver _fini(9E) entry point. 2315 * 2316 * Return Code: Returns the value from mod_remove(9F) 2317 * 2318 * Context: Called when driver module is unloaded. 2319 */ 2320 2321 int 2322 _fini(void) 2323 { 2324 int err; 2325 2326 if ((err = mod_remove(&modlinkage)) != 0) { 2327 return (err); 2328 } 2329 2330 sd_taskq_delete(); 2331 2332 mutex_destroy(&sd_detach_mutex); 2333 mutex_destroy(&sd_log_mutex); 2334 mutex_destroy(&sd_label_mutex); 2335 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex); 2336 2337 sd_scsi_probe_cache_fini(); 2338 2339 sd_scsi_target_lun_fini(); 2340 2341 cv_destroy(&sd_tr.srq_resv_reclaim_cv); 2342 cv_destroy(&sd_tr.srq_inprocess_cv); 2343 2344 ddi_soft_state_fini(&sd_state); 2345 2346 return (err); 2347 } 2348 2349 2350 /* 2351 * Function: _info 2352 * 2353 * Description: This is the driver _info(9E) entry point. 2354 * 2355 * Arguments: modinfop - pointer to the driver modinfo structure 2356 * 2357 * Return Code: Returns the value from mod_info(9F). 2358 * 2359 * Context: Kernel thread context 2360 */ 2361 2362 int 2363 _info(struct modinfo *modinfop) 2364 { 2365 return (mod_info(&modlinkage, modinfop)); 2366 } 2367 2368 2369 /* 2370 * The following routines implement the driver message logging facility. 2371 * They provide component- and level- based debug output filtering. 2372 * Output may also be restricted to messages for a single instance by 2373 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set 2374 * to NULL, then messages for all instances are printed. 2375 * 2376 * These routines have been cloned from each other due to the language 2377 * constraints of macros and variable argument list processing. 2378 */ 2379 2380 2381 /* 2382 * Function: sd_log_err 2383 * 2384 * Description: This routine is called by the SD_ERROR macro for debug 2385 * logging of error conditions. 2386 * 2387 * Arguments: comp - driver component being logged 2388 * dev - pointer to driver info structure 2389 * fmt - error string and format to be logged 2390 */ 2391 2392 static void 2393 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...) 2394 { 2395 va_list ap; 2396 dev_info_t *dev; 2397 2398 ASSERT(un != NULL); 2399 dev = SD_DEVINFO(un); 2400 ASSERT(dev != NULL); 2401 2402 /* 2403 * Filter messages based on the global component and level masks. 2404 * Also print if un matches the value of sd_debug_un, or if 2405 * sd_debug_un is set to NULL. 2406 */ 2407 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) && 2408 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2409 mutex_enter(&sd_log_mutex); 2410 va_start(ap, fmt); 2411 (void) vsprintf(sd_log_buf, fmt, ap); 2412 va_end(ap); 2413 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2414 mutex_exit(&sd_log_mutex); 2415 } 2416 #ifdef SD_FAULT_INJECTION 2417 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2418 if (un->sd_injection_mask & comp) { 2419 mutex_enter(&sd_log_mutex); 2420 va_start(ap, fmt); 2421 (void) vsprintf(sd_log_buf, fmt, ap); 2422 va_end(ap); 2423 sd_injection_log(sd_log_buf, un); 2424 mutex_exit(&sd_log_mutex); 2425 } 2426 #endif 2427 } 2428 2429 2430 /* 2431 * Function: sd_log_info 2432 * 2433 * Description: This routine is called by the SD_INFO macro for debug 2434 * logging of general purpose informational conditions. 2435 * 2436 * Arguments: comp - driver component being logged 2437 * dev - pointer to driver info structure 2438 * fmt - info string and format to be logged 2439 */ 2440 2441 static void 2442 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...) 2443 { 2444 va_list ap; 2445 dev_info_t *dev; 2446 2447 ASSERT(un != NULL); 2448 dev = SD_DEVINFO(un); 2449 ASSERT(dev != NULL); 2450 2451 /* 2452 * Filter messages based on the global component and level masks. 2453 * Also print if un matches the value of sd_debug_un, or if 2454 * sd_debug_un is set to NULL. 2455 */ 2456 if ((sd_component_mask & component) && 2457 (sd_level_mask & SD_LOGMASK_INFO) && 2458 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2459 mutex_enter(&sd_log_mutex); 2460 va_start(ap, fmt); 2461 (void) vsprintf(sd_log_buf, fmt, ap); 2462 va_end(ap); 2463 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2464 mutex_exit(&sd_log_mutex); 2465 } 2466 #ifdef SD_FAULT_INJECTION 2467 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2468 if (un->sd_injection_mask & component) { 2469 mutex_enter(&sd_log_mutex); 2470 va_start(ap, fmt); 2471 (void) vsprintf(sd_log_buf, fmt, ap); 2472 va_end(ap); 2473 sd_injection_log(sd_log_buf, un); 2474 mutex_exit(&sd_log_mutex); 2475 } 2476 #endif 2477 } 2478 2479 2480 /* 2481 * Function: sd_log_trace 2482 * 2483 * Description: This routine is called by the SD_TRACE macro for debug 2484 * logging of trace conditions (i.e. function entry/exit). 2485 * 2486 * Arguments: comp - driver component being logged 2487 * dev - pointer to driver info structure 2488 * fmt - trace string and format to be logged 2489 */ 2490 2491 static void 2492 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...) 2493 { 2494 va_list ap; 2495 dev_info_t *dev; 2496 2497 ASSERT(un != NULL); 2498 dev = SD_DEVINFO(un); 2499 ASSERT(dev != NULL); 2500 2501 /* 2502 * Filter messages based on the global component and level masks. 2503 * Also print if un matches the value of sd_debug_un, or if 2504 * sd_debug_un is set to NULL. 2505 */ 2506 if ((sd_component_mask & component) && 2507 (sd_level_mask & SD_LOGMASK_TRACE) && 2508 ((sd_debug_un == NULL) || (sd_debug_un == un))) { 2509 mutex_enter(&sd_log_mutex); 2510 va_start(ap, fmt); 2511 (void) vsprintf(sd_log_buf, fmt, ap); 2512 va_end(ap); 2513 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf); 2514 mutex_exit(&sd_log_mutex); 2515 } 2516 #ifdef SD_FAULT_INJECTION 2517 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask)); 2518 if (un->sd_injection_mask & component) { 2519 mutex_enter(&sd_log_mutex); 2520 va_start(ap, fmt); 2521 (void) vsprintf(sd_log_buf, fmt, ap); 2522 va_end(ap); 2523 sd_injection_log(sd_log_buf, un); 2524 mutex_exit(&sd_log_mutex); 2525 } 2526 #endif 2527 } 2528 2529 2530 /* 2531 * Function: sdprobe 2532 * 2533 * Description: This is the driver probe(9e) entry point function. 2534 * 2535 * Arguments: devi - opaque device info handle 2536 * 2537 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful. 2538 * DDI_PROBE_FAILURE: If the probe failed. 2539 * DDI_PROBE_PARTIAL: If the instance is not present now, 2540 * but may be present in the future. 2541 */ 2542 2543 static int 2544 sdprobe(dev_info_t *devi) 2545 { 2546 struct scsi_device *devp; 2547 int rval; 2548 int instance; 2549 2550 /* 2551 * if it wasn't for pln, sdprobe could actually be nulldev 2552 * in the "__fibre" case. 2553 */ 2554 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 2555 return (DDI_PROBE_DONTCARE); 2556 } 2557 2558 devp = ddi_get_driver_private(devi); 2559 2560 if (devp == NULL) { 2561 /* Ooops... nexus driver is mis-configured... */ 2562 return (DDI_PROBE_FAILURE); 2563 } 2564 2565 instance = ddi_get_instance(devi); 2566 2567 if (ddi_get_soft_state(sd_state, instance) != NULL) { 2568 return (DDI_PROBE_PARTIAL); 2569 } 2570 2571 /* 2572 * Call the SCSA utility probe routine to see if we actually 2573 * have a target at this SCSI nexus. 2574 */ 2575 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) { 2576 case SCSIPROBE_EXISTS: 2577 switch (devp->sd_inq->inq_dtype) { 2578 case DTYPE_DIRECT: 2579 rval = DDI_PROBE_SUCCESS; 2580 break; 2581 case DTYPE_RODIRECT: 2582 /* CDs etc. Can be removable media */ 2583 rval = DDI_PROBE_SUCCESS; 2584 break; 2585 case DTYPE_OPTICAL: 2586 /* 2587 * Rewritable optical driver HP115AA 2588 * Can also be removable media 2589 */ 2590 2591 /* 2592 * Do not attempt to bind to DTYPE_OPTICAL if 2593 * pre solaris 9 sparc sd behavior is required 2594 * 2595 * If first time through and sd_dtype_optical_bind 2596 * has not been set in /etc/system check properties 2597 */ 2598 2599 if (sd_dtype_optical_bind < 0) { 2600 sd_dtype_optical_bind = ddi_prop_get_int 2601 (DDI_DEV_T_ANY, devi, 0, 2602 "optical-device-bind", 1); 2603 } 2604 2605 if (sd_dtype_optical_bind == 0) { 2606 rval = DDI_PROBE_FAILURE; 2607 } else { 2608 rval = DDI_PROBE_SUCCESS; 2609 } 2610 break; 2611 2612 case DTYPE_NOTPRESENT: 2613 default: 2614 rval = DDI_PROBE_FAILURE; 2615 break; 2616 } 2617 break; 2618 default: 2619 rval = DDI_PROBE_PARTIAL; 2620 break; 2621 } 2622 2623 /* 2624 * This routine checks for resource allocation prior to freeing, 2625 * so it will take care of the "smart probing" case where a 2626 * scsi_probe() may or may not have been issued and will *not* 2627 * free previously-freed resources. 2628 */ 2629 scsi_unprobe(devp); 2630 return (rval); 2631 } 2632 2633 2634 /* 2635 * Function: sdinfo 2636 * 2637 * Description: This is the driver getinfo(9e) entry point function. 2638 * Given the device number, return the devinfo pointer from 2639 * the scsi_device structure or the instance number 2640 * associated with the dev_t. 2641 * 2642 * Arguments: dip - pointer to device info structure 2643 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO, 2644 * DDI_INFO_DEVT2INSTANCE) 2645 * arg - driver dev_t 2646 * resultp - user buffer for request response 2647 * 2648 * Return Code: DDI_SUCCESS 2649 * DDI_FAILURE 2650 */ 2651 /* ARGSUSED */ 2652 static int 2653 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2654 { 2655 struct sd_lun *un; 2656 dev_t dev; 2657 int instance; 2658 int error; 2659 2660 switch (infocmd) { 2661 case DDI_INFO_DEVT2DEVINFO: 2662 dev = (dev_t)arg; 2663 instance = SDUNIT(dev); 2664 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 2665 return (DDI_FAILURE); 2666 } 2667 *result = (void *) SD_DEVINFO(un); 2668 error = DDI_SUCCESS; 2669 break; 2670 case DDI_INFO_DEVT2INSTANCE: 2671 dev = (dev_t)arg; 2672 instance = SDUNIT(dev); 2673 *result = (void *)(uintptr_t)instance; 2674 error = DDI_SUCCESS; 2675 break; 2676 default: 2677 error = DDI_FAILURE; 2678 } 2679 return (error); 2680 } 2681 2682 /* 2683 * Function: sd_prop_op 2684 * 2685 * Description: This is the driver prop_op(9e) entry point function. 2686 * Return the number of blocks for the partition in question 2687 * or forward the request to the property facilities. 2688 * 2689 * Arguments: dev - device number 2690 * dip - pointer to device info structure 2691 * prop_op - property operator 2692 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent 2693 * name - pointer to property name 2694 * valuep - pointer or address of the user buffer 2695 * lengthp - property length 2696 * 2697 * Return Code: DDI_PROP_SUCCESS 2698 * DDI_PROP_NOT_FOUND 2699 * DDI_PROP_UNDEFINED 2700 * DDI_PROP_NO_MEMORY 2701 * DDI_PROP_BUF_TOO_SMALL 2702 */ 2703 2704 static int 2705 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 2706 char *name, caddr_t valuep, int *lengthp) 2707 { 2708 int instance = ddi_get_instance(dip); 2709 struct sd_lun *un; 2710 uint64_t nblocks64; 2711 2712 /* 2713 * Our dynamic properties are all device specific and size oriented. 2714 * Requests issued under conditions where size is valid are passed 2715 * to ddi_prop_op_nblocks with the size information, otherwise the 2716 * request is passed to ddi_prop_op. Size depends on valid geometry. 2717 */ 2718 un = ddi_get_soft_state(sd_state, instance); 2719 if ((dev == DDI_DEV_T_ANY) || (un == NULL) || 2720 (un->un_f_geometry_is_valid == FALSE)) { 2721 return (ddi_prop_op(dev, dip, prop_op, mod_flags, 2722 name, valuep, lengthp)); 2723 } else { 2724 /* get nblocks value */ 2725 ASSERT(!mutex_owned(SD_MUTEX(un))); 2726 mutex_enter(SD_MUTEX(un)); 2727 nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk; 2728 mutex_exit(SD_MUTEX(un)); 2729 2730 return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags, 2731 name, valuep, lengthp, nblocks64)); 2732 } 2733 } 2734 2735 /* 2736 * The following functions are for smart probing: 2737 * sd_scsi_probe_cache_init() 2738 * sd_scsi_probe_cache_fini() 2739 * sd_scsi_clear_probe_cache() 2740 * sd_scsi_probe_with_cache() 2741 */ 2742 2743 /* 2744 * Function: sd_scsi_probe_cache_init 2745 * 2746 * Description: Initializes the probe response cache mutex and head pointer. 2747 * 2748 * Context: Kernel thread context 2749 */ 2750 2751 static void 2752 sd_scsi_probe_cache_init(void) 2753 { 2754 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL); 2755 sd_scsi_probe_cache_head = NULL; 2756 } 2757 2758 2759 /* 2760 * Function: sd_scsi_probe_cache_fini 2761 * 2762 * Description: Frees all resources associated with the probe response cache. 2763 * 2764 * Context: Kernel thread context 2765 */ 2766 2767 static void 2768 sd_scsi_probe_cache_fini(void) 2769 { 2770 struct sd_scsi_probe_cache *cp; 2771 struct sd_scsi_probe_cache *ncp; 2772 2773 /* Clean up our smart probing linked list */ 2774 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) { 2775 ncp = cp->next; 2776 kmem_free(cp, sizeof (struct sd_scsi_probe_cache)); 2777 } 2778 sd_scsi_probe_cache_head = NULL; 2779 mutex_destroy(&sd_scsi_probe_cache_mutex); 2780 } 2781 2782 2783 /* 2784 * Function: sd_scsi_clear_probe_cache 2785 * 2786 * Description: This routine clears the probe response cache. This is 2787 * done when open() returns ENXIO so that when deferred 2788 * attach is attempted (possibly after a device has been 2789 * turned on) we will retry the probe. Since we don't know 2790 * which target we failed to open, we just clear the 2791 * entire cache. 2792 * 2793 * Context: Kernel thread context 2794 */ 2795 2796 static void 2797 sd_scsi_clear_probe_cache(void) 2798 { 2799 struct sd_scsi_probe_cache *cp; 2800 int i; 2801 2802 mutex_enter(&sd_scsi_probe_cache_mutex); 2803 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2804 /* 2805 * Reset all entries to SCSIPROBE_EXISTS. This will 2806 * force probing to be performed the next time 2807 * sd_scsi_probe_with_cache is called. 2808 */ 2809 for (i = 0; i < NTARGETS_WIDE; i++) { 2810 cp->cache[i] = SCSIPROBE_EXISTS; 2811 } 2812 } 2813 mutex_exit(&sd_scsi_probe_cache_mutex); 2814 } 2815 2816 2817 /* 2818 * Function: sd_scsi_probe_with_cache 2819 * 2820 * Description: This routine implements support for a scsi device probe 2821 * with cache. The driver maintains a cache of the target 2822 * responses to scsi probes. If we get no response from a 2823 * target during a probe inquiry, we remember that, and we 2824 * avoid additional calls to scsi_probe on non-zero LUNs 2825 * on the same target until the cache is cleared. By doing 2826 * so we avoid the 1/4 sec selection timeout for nonzero 2827 * LUNs. lun0 of a target is always probed. 2828 * 2829 * Arguments: devp - Pointer to a scsi_device(9S) structure 2830 * waitfunc - indicates what the allocator routines should 2831 * do when resources are not available. This value 2832 * is passed on to scsi_probe() when that routine 2833 * is called. 2834 * 2835 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache; 2836 * otherwise the value returned by scsi_probe(9F). 2837 * 2838 * Context: Kernel thread context 2839 */ 2840 2841 static int 2842 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)()) 2843 { 2844 struct sd_scsi_probe_cache *cp; 2845 dev_info_t *pdip = ddi_get_parent(devp->sd_dev); 2846 int lun, tgt; 2847 2848 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2849 SCSI_ADDR_PROP_LUN, 0); 2850 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS, 2851 SCSI_ADDR_PROP_TARGET, -1); 2852 2853 /* Make sure caching enabled and target in range */ 2854 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) { 2855 /* do it the old way (no cache) */ 2856 return (scsi_probe(devp, waitfn)); 2857 } 2858 2859 mutex_enter(&sd_scsi_probe_cache_mutex); 2860 2861 /* Find the cache for this scsi bus instance */ 2862 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) { 2863 if (cp->pdip == pdip) { 2864 break; 2865 } 2866 } 2867 2868 /* If we can't find a cache for this pdip, create one */ 2869 if (cp == NULL) { 2870 int i; 2871 2872 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache), 2873 KM_SLEEP); 2874 cp->pdip = pdip; 2875 cp->next = sd_scsi_probe_cache_head; 2876 sd_scsi_probe_cache_head = cp; 2877 for (i = 0; i < NTARGETS_WIDE; i++) { 2878 cp->cache[i] = SCSIPROBE_EXISTS; 2879 } 2880 } 2881 2882 mutex_exit(&sd_scsi_probe_cache_mutex); 2883 2884 /* Recompute the cache for this target if LUN zero */ 2885 if (lun == 0) { 2886 cp->cache[tgt] = SCSIPROBE_EXISTS; 2887 } 2888 2889 /* Don't probe if cache remembers a NORESP from a previous LUN. */ 2890 if (cp->cache[tgt] != SCSIPROBE_EXISTS) { 2891 return (SCSIPROBE_NORESP); 2892 } 2893 2894 /* Do the actual probe; save & return the result */ 2895 return (cp->cache[tgt] = scsi_probe(devp, waitfn)); 2896 } 2897 2898 2899 /* 2900 * Function: sd_scsi_target_lun_init 2901 * 2902 * Description: Initializes the attached lun chain mutex and head pointer. 2903 * 2904 * Context: Kernel thread context 2905 */ 2906 2907 static void 2908 sd_scsi_target_lun_init(void) 2909 { 2910 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL); 2911 sd_scsi_target_lun_head = NULL; 2912 } 2913 2914 2915 /* 2916 * Function: sd_scsi_target_lun_fini 2917 * 2918 * Description: Frees all resources associated with the attached lun 2919 * chain 2920 * 2921 * Context: Kernel thread context 2922 */ 2923 2924 static void 2925 sd_scsi_target_lun_fini(void) 2926 { 2927 struct sd_scsi_hba_tgt_lun *cp; 2928 struct sd_scsi_hba_tgt_lun *ncp; 2929 2930 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) { 2931 ncp = cp->next; 2932 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun)); 2933 } 2934 sd_scsi_target_lun_head = NULL; 2935 mutex_destroy(&sd_scsi_target_lun_mutex); 2936 } 2937 2938 2939 /* 2940 * Function: sd_scsi_get_target_lun_count 2941 * 2942 * Description: This routine will check in the attached lun chain to see 2943 * how many luns are attached on the required SCSI controller 2944 * and target. Currently, some capabilities like tagged queue 2945 * are supported per target based by HBA. So all luns in a 2946 * target have the same capabilities. Based on this assumption, 2947 * sd should only set these capabilities once per target. This 2948 * function is called when sd needs to decide how many luns 2949 * already attached on a target. 2950 * 2951 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2952 * controller device. 2953 * target - The target ID on the controller's SCSI bus. 2954 * 2955 * Return Code: The number of luns attached on the required target and 2956 * controller. 2957 * -1 if target ID is not in parallel SCSI scope or the given 2958 * dip is not in the chain. 2959 * 2960 * Context: Kernel thread context 2961 */ 2962 2963 static int 2964 sd_scsi_get_target_lun_count(dev_info_t *dip, int target) 2965 { 2966 struct sd_scsi_hba_tgt_lun *cp; 2967 2968 if ((target < 0) || (target >= NTARGETS_WIDE)) { 2969 return (-1); 2970 } 2971 2972 mutex_enter(&sd_scsi_target_lun_mutex); 2973 2974 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 2975 if (cp->pdip == dip) { 2976 break; 2977 } 2978 } 2979 2980 mutex_exit(&sd_scsi_target_lun_mutex); 2981 2982 if (cp == NULL) { 2983 return (-1); 2984 } 2985 2986 return (cp->nlun[target]); 2987 } 2988 2989 2990 /* 2991 * Function: sd_scsi_update_lun_on_target 2992 * 2993 * Description: This routine is used to update the attached lun chain when a 2994 * lun is attached or detached on a target. 2995 * 2996 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI 2997 * controller device. 2998 * target - The target ID on the controller's SCSI bus. 2999 * flag - Indicate the lun is attached or detached. 3000 * 3001 * Context: Kernel thread context 3002 */ 3003 3004 static void 3005 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag) 3006 { 3007 struct sd_scsi_hba_tgt_lun *cp; 3008 3009 mutex_enter(&sd_scsi_target_lun_mutex); 3010 3011 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) { 3012 if (cp->pdip == dip) { 3013 break; 3014 } 3015 } 3016 3017 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) { 3018 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun), 3019 KM_SLEEP); 3020 cp->pdip = dip; 3021 cp->next = sd_scsi_target_lun_head; 3022 sd_scsi_target_lun_head = cp; 3023 } 3024 3025 mutex_exit(&sd_scsi_target_lun_mutex); 3026 3027 if (cp != NULL) { 3028 if (flag == SD_SCSI_LUN_ATTACH) { 3029 cp->nlun[target] ++; 3030 } else { 3031 cp->nlun[target] --; 3032 } 3033 } 3034 } 3035 3036 3037 /* 3038 * Function: sd_spin_up_unit 3039 * 3040 * Description: Issues the following commands to spin-up the device: 3041 * START STOP UNIT, and INQUIRY. 3042 * 3043 * Arguments: un - driver soft state (unit) structure 3044 * 3045 * Return Code: 0 - success 3046 * EIO - failure 3047 * EACCES - reservation conflict 3048 * 3049 * Context: Kernel thread context 3050 */ 3051 3052 static int 3053 sd_spin_up_unit(struct sd_lun *un) 3054 { 3055 size_t resid = 0; 3056 int has_conflict = FALSE; 3057 uchar_t *bufaddr; 3058 3059 ASSERT(un != NULL); 3060 3061 /* 3062 * Send a throwaway START UNIT command. 3063 * 3064 * If we fail on this, we don't care presently what precisely 3065 * is wrong. EMC's arrays will also fail this with a check 3066 * condition (0x2/0x4/0x3) if the device is "inactive," but 3067 * we don't want to fail the attach because it may become 3068 * "active" later. 3069 */ 3070 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT) 3071 == EACCES) 3072 has_conflict = TRUE; 3073 3074 /* 3075 * Send another INQUIRY command to the target. This is necessary for 3076 * non-removable media direct access devices because their INQUIRY data 3077 * may not be fully qualified until they are spun up (perhaps via the 3078 * START command above). Note: This seems to be needed for some 3079 * legacy devices only.) The INQUIRY command should succeed even if a 3080 * Reservation Conflict is present. 3081 */ 3082 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP); 3083 if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) { 3084 kmem_free(bufaddr, SUN_INQSIZE); 3085 return (EIO); 3086 } 3087 3088 /* 3089 * If we got enough INQUIRY data, copy it over the old INQUIRY data. 3090 * Note that this routine does not return a failure here even if the 3091 * INQUIRY command did not return any data. This is a legacy behavior. 3092 */ 3093 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) { 3094 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE); 3095 } 3096 3097 kmem_free(bufaddr, SUN_INQSIZE); 3098 3099 /* If we hit a reservation conflict above, tell the caller. */ 3100 if (has_conflict == TRUE) { 3101 return (EACCES); 3102 } 3103 3104 return (0); 3105 } 3106 3107 #ifdef _LP64 3108 /* 3109 * Function: sd_enable_descr_sense 3110 * 3111 * Description: This routine attempts to select descriptor sense format 3112 * using the Control mode page. Devices that support 64 bit 3113 * LBAs (for >2TB luns) should also implement descriptor 3114 * sense data so we will call this function whenever we see 3115 * a lun larger than 2TB. If for some reason the device 3116 * supports 64 bit LBAs but doesn't support descriptor sense 3117 * presumably the mode select will fail. Everything will 3118 * continue to work normally except that we will not get 3119 * complete sense data for commands that fail with an LBA 3120 * larger than 32 bits. 3121 * 3122 * Arguments: un - driver soft state (unit) structure 3123 * 3124 * Context: Kernel thread context only 3125 */ 3126 3127 static void 3128 sd_enable_descr_sense(struct sd_lun *un) 3129 { 3130 uchar_t *header; 3131 struct mode_control_scsi3 *ctrl_bufp; 3132 size_t buflen; 3133 size_t bd_len; 3134 3135 /* 3136 * Read MODE SENSE page 0xA, Control Mode Page 3137 */ 3138 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH + 3139 sizeof (struct mode_control_scsi3); 3140 header = kmem_zalloc(buflen, KM_SLEEP); 3141 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 3142 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) { 3143 SD_ERROR(SD_LOG_COMMON, un, 3144 "sd_enable_descr_sense: mode sense ctrl page failed\n"); 3145 goto eds_exit; 3146 } 3147 3148 /* 3149 * Determine size of Block Descriptors in order to locate 3150 * the mode page data. ATAPI devices return 0, SCSI devices 3151 * should return MODE_BLK_DESC_LENGTH. 3152 */ 3153 bd_len = ((struct mode_header *)header)->bdesc_length; 3154 3155 ctrl_bufp = (struct mode_control_scsi3 *) 3156 (header + MODE_HEADER_LENGTH + bd_len); 3157 3158 /* 3159 * Clear PS bit for MODE SELECT 3160 */ 3161 ctrl_bufp->mode_page.ps = 0; 3162 3163 /* 3164 * Set D_SENSE to enable descriptor sense format. 3165 */ 3166 ctrl_bufp->d_sense = 1; 3167 3168 /* 3169 * Use MODE SELECT to commit the change to the D_SENSE bit 3170 */ 3171 if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 3172 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) { 3173 SD_INFO(SD_LOG_COMMON, un, 3174 "sd_enable_descr_sense: mode select ctrl page failed\n"); 3175 goto eds_exit; 3176 } 3177 3178 eds_exit: 3179 kmem_free(header, buflen); 3180 } 3181 3182 /* 3183 * Function: sd_reenable_dsense_task 3184 * 3185 * Description: Re-enable descriptor sense after device or bus reset 3186 * 3187 * Context: Executes in a taskq() thread context 3188 */ 3189 static void 3190 sd_reenable_dsense_task(void *arg) 3191 { 3192 struct sd_lun *un = arg; 3193 3194 ASSERT(un != NULL); 3195 sd_enable_descr_sense(un); 3196 } 3197 #endif /* _LP64 */ 3198 3199 /* 3200 * Function: sd_set_mmc_caps 3201 * 3202 * Description: This routine determines if the device is MMC compliant and if 3203 * the device supports CDDA via a mode sense of the CDVD 3204 * capabilities mode page. Also checks if the device is a 3205 * dvdram writable device. 3206 * 3207 * Arguments: un - driver soft state (unit) structure 3208 * 3209 * Context: Kernel thread context only 3210 */ 3211 3212 static void 3213 sd_set_mmc_caps(struct sd_lun *un) 3214 { 3215 struct mode_header_grp2 *sense_mhp; 3216 uchar_t *sense_page; 3217 caddr_t buf; 3218 int bd_len; 3219 int status; 3220 struct uscsi_cmd com; 3221 int rtn; 3222 uchar_t *out_data_rw, *out_data_hd; 3223 uchar_t *rqbuf_rw, *rqbuf_hd; 3224 3225 ASSERT(un != NULL); 3226 3227 /* 3228 * The flags which will be set in this function are - mmc compliant, 3229 * dvdram writable device, cdda support. Initialize them to FALSE 3230 * and if a capability is detected - it will be set to TRUE. 3231 */ 3232 un->un_f_mmc_cap = FALSE; 3233 un->un_f_dvdram_writable_device = FALSE; 3234 un->un_f_cfg_cdda = FALSE; 3235 3236 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3237 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3238 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3239 3240 if (status != 0) { 3241 /* command failed; just return */ 3242 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3243 return; 3244 } 3245 /* 3246 * If the mode sense request for the CDROM CAPABILITIES 3247 * page (0x2A) succeeds the device is assumed to be MMC. 3248 */ 3249 un->un_f_mmc_cap = TRUE; 3250 3251 /* Get to the page data */ 3252 sense_mhp = (struct mode_header_grp2 *)buf; 3253 bd_len = (sense_mhp->bdesc_length_hi << 8) | 3254 sense_mhp->bdesc_length_lo; 3255 if (bd_len > MODE_BLK_DESC_LENGTH) { 3256 /* 3257 * We did not get back the expected block descriptor 3258 * length so we cannot determine if the device supports 3259 * CDDA. However, we still indicate the device is MMC 3260 * according to the successful response to the page 3261 * 0x2A mode sense request. 3262 */ 3263 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3264 "sd_set_mmc_caps: Mode Sense returned " 3265 "invalid block descriptor length\n"); 3266 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3267 return; 3268 } 3269 3270 /* See if read CDDA is supported */ 3271 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + 3272 bd_len); 3273 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE; 3274 3275 /* See if writing DVD RAM is supported. */ 3276 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE; 3277 if (un->un_f_dvdram_writable_device == TRUE) { 3278 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3279 return; 3280 } 3281 3282 /* 3283 * If the device presents DVD or CD capabilities in the mode 3284 * page, we can return here since a RRD will not have 3285 * these capabilities. 3286 */ 3287 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3288 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3289 return; 3290 } 3291 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3292 3293 /* 3294 * If un->un_f_dvdram_writable_device is still FALSE, 3295 * check for a Removable Rigid Disk (RRD). A RRD 3296 * device is identified by the features RANDOM_WRITABLE and 3297 * HARDWARE_DEFECT_MANAGEMENT. 3298 */ 3299 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3300 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3301 3302 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3303 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3304 RANDOM_WRITABLE); 3305 if (rtn != 0) { 3306 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3307 kmem_free(rqbuf_rw, SENSE_LENGTH); 3308 return; 3309 } 3310 3311 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3312 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3313 3314 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3315 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3316 HARDWARE_DEFECT_MANAGEMENT); 3317 if (rtn == 0) { 3318 /* 3319 * We have good information, check for random writable 3320 * and hardware defect features. 3321 */ 3322 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3323 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) { 3324 un->un_f_dvdram_writable_device = TRUE; 3325 } 3326 } 3327 3328 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3329 kmem_free(rqbuf_rw, SENSE_LENGTH); 3330 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3331 kmem_free(rqbuf_hd, SENSE_LENGTH); 3332 } 3333 3334 /* 3335 * Function: sd_check_for_writable_cd 3336 * 3337 * Description: This routine determines if the media in the device is 3338 * writable or not. It uses the get configuration command (0x46) 3339 * to determine if the media is writable 3340 * 3341 * Arguments: un - driver soft state (unit) structure 3342 * 3343 * Context: Never called at interrupt context. 3344 */ 3345 3346 static void 3347 sd_check_for_writable_cd(struct sd_lun *un) 3348 { 3349 struct uscsi_cmd com; 3350 uchar_t *out_data; 3351 uchar_t *rqbuf; 3352 int rtn; 3353 uchar_t *out_data_rw, *out_data_hd; 3354 uchar_t *rqbuf_rw, *rqbuf_hd; 3355 struct mode_header_grp2 *sense_mhp; 3356 uchar_t *sense_page; 3357 caddr_t buf; 3358 int bd_len; 3359 int status; 3360 3361 ASSERT(un != NULL); 3362 ASSERT(mutex_owned(SD_MUTEX(un))); 3363 3364 /* 3365 * Initialize the writable media to false, if configuration info. 3366 * tells us otherwise then only we will set it. 3367 */ 3368 un->un_f_mmc_writable_media = FALSE; 3369 mutex_exit(SD_MUTEX(un)); 3370 3371 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 3372 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3373 3374 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH, 3375 out_data, SD_PROFILE_HEADER_LEN); 3376 3377 mutex_enter(SD_MUTEX(un)); 3378 if (rtn == 0) { 3379 /* 3380 * We have good information, check for writable DVD. 3381 */ 3382 if ((out_data[6] == 0) && (out_data[7] == 0x12)) { 3383 un->un_f_mmc_writable_media = TRUE; 3384 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3385 kmem_free(rqbuf, SENSE_LENGTH); 3386 return; 3387 } 3388 } 3389 3390 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 3391 kmem_free(rqbuf, SENSE_LENGTH); 3392 3393 /* 3394 * Determine if this is a RRD type device. 3395 */ 3396 mutex_exit(SD_MUTEX(un)); 3397 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 3398 status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf, 3399 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT); 3400 mutex_enter(SD_MUTEX(un)); 3401 if (status != 0) { 3402 /* command failed; just return */ 3403 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3404 return; 3405 } 3406 3407 /* Get to the page data */ 3408 sense_mhp = (struct mode_header_grp2 *)buf; 3409 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 3410 if (bd_len > MODE_BLK_DESC_LENGTH) { 3411 /* 3412 * We did not get back the expected block descriptor length so 3413 * we cannot check the mode page. 3414 */ 3415 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3416 "sd_check_for_writable_cd: Mode Sense returned " 3417 "invalid block descriptor length\n"); 3418 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3419 return; 3420 } 3421 3422 /* 3423 * If the device presents DVD or CD capabilities in the mode 3424 * page, we can return here since a RRD device will not have 3425 * these capabilities. 3426 */ 3427 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len); 3428 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) { 3429 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3430 return; 3431 } 3432 kmem_free(buf, BUFLEN_MODE_CDROM_CAP); 3433 3434 /* 3435 * If un->un_f_mmc_writable_media is still FALSE, 3436 * check for RRD type media. A RRD device is identified 3437 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT. 3438 */ 3439 mutex_exit(SD_MUTEX(un)); 3440 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3441 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3442 3443 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw, 3444 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN, 3445 RANDOM_WRITABLE); 3446 if (rtn != 0) { 3447 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3448 kmem_free(rqbuf_rw, SENSE_LENGTH); 3449 mutex_enter(SD_MUTEX(un)); 3450 return; 3451 } 3452 3453 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP); 3454 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 3455 3456 rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd, 3457 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN, 3458 HARDWARE_DEFECT_MANAGEMENT); 3459 mutex_enter(SD_MUTEX(un)); 3460 if (rtn == 0) { 3461 /* 3462 * We have good information, check for random writable 3463 * and hardware defect features as current. 3464 */ 3465 if ((out_data_rw[9] & RANDOM_WRITABLE) && 3466 (out_data_rw[10] & 0x1) && 3467 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) && 3468 (out_data_hd[10] & 0x1)) { 3469 un->un_f_mmc_writable_media = TRUE; 3470 } 3471 } 3472 3473 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN); 3474 kmem_free(rqbuf_rw, SENSE_LENGTH); 3475 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN); 3476 kmem_free(rqbuf_hd, SENSE_LENGTH); 3477 } 3478 3479 /* 3480 * Function: sd_read_unit_properties 3481 * 3482 * Description: The following implements a property lookup mechanism. 3483 * Properties for particular disks (keyed on vendor, model 3484 * and rev numbers) are sought in the sd.conf file via 3485 * sd_process_sdconf_file(), and if not found there, are 3486 * looked for in a list hardcoded in this driver via 3487 * sd_process_sdconf_table() Once located the properties 3488 * are used to update the driver unit structure. 3489 * 3490 * Arguments: un - driver soft state (unit) structure 3491 */ 3492 3493 static void 3494 sd_read_unit_properties(struct sd_lun *un) 3495 { 3496 /* 3497 * sd_process_sdconf_file returns SD_FAILURE if it cannot find 3498 * the "sd-config-list" property (from the sd.conf file) or if 3499 * there was not a match for the inquiry vid/pid. If this event 3500 * occurs the static driver configuration table is searched for 3501 * a match. 3502 */ 3503 ASSERT(un != NULL); 3504 if (sd_process_sdconf_file(un) == SD_FAILURE) { 3505 sd_process_sdconf_table(un); 3506 } 3507 3508 /* check for LSI device */ 3509 sd_is_lsi(un); 3510 3511 3512 } 3513 3514 3515 /* 3516 * Function: sd_process_sdconf_file 3517 * 3518 * Description: Use ddi_getlongprop to obtain the properties from the 3519 * driver's config file (ie, sd.conf) and update the driver 3520 * soft state structure accordingly. 3521 * 3522 * Arguments: un - driver soft state (unit) structure 3523 * 3524 * Return Code: SD_SUCCESS - The properties were successfully set according 3525 * to the driver configuration file. 3526 * SD_FAILURE - The driver config list was not obtained or 3527 * there was no vid/pid match. This indicates that 3528 * the static config table should be used. 3529 * 3530 * The config file has a property, "sd-config-list", which consists of 3531 * one or more duplets as follows: 3532 * 3533 * sd-config-list= 3534 * <duplet>, 3535 * [<duplet>,] 3536 * [<duplet>]; 3537 * 3538 * The structure of each duplet is as follows: 3539 * 3540 * <duplet>:= <vid+pid>,<data-property-name_list> 3541 * 3542 * The first entry of the duplet is the device ID string (the concatenated 3543 * vid & pid; not to be confused with a device_id). This is defined in 3544 * the same way as in the sd_disk_table. 3545 * 3546 * The second part of the duplet is a string that identifies a 3547 * data-property-name-list. The data-property-name-list is defined as 3548 * follows: 3549 * 3550 * <data-property-name-list>:=<data-property-name> [<data-property-name>] 3551 * 3552 * The syntax of <data-property-name> depends on the <version> field. 3553 * 3554 * If version = SD_CONF_VERSION_1 we have the following syntax: 3555 * 3556 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 3557 * 3558 * where the prop0 value will be used to set prop0 if bit0 set in the 3559 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1 3560 * 3561 */ 3562 3563 static int 3564 sd_process_sdconf_file(struct sd_lun *un) 3565 { 3566 char *config_list = NULL; 3567 int config_list_len; 3568 int len; 3569 int dupletlen = 0; 3570 char *vidptr; 3571 int vidlen; 3572 char *dnlist_ptr; 3573 char *dataname_ptr; 3574 int dnlist_len; 3575 int dataname_len; 3576 int *data_list; 3577 int data_list_len; 3578 int rval = SD_FAILURE; 3579 int i; 3580 3581 ASSERT(un != NULL); 3582 3583 /* Obtain the configuration list associated with the .conf file */ 3584 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS, 3585 sd_config_list, (caddr_t)&config_list, &config_list_len) 3586 != DDI_PROP_SUCCESS) { 3587 return (SD_FAILURE); 3588 } 3589 3590 /* 3591 * Compare vids in each duplet to the inquiry vid - if a match is 3592 * made, get the data value and update the soft state structure 3593 * accordingly. 3594 * 3595 * Note: This algorithm is complex and difficult to maintain. It should 3596 * be replaced with a more robust implementation. 3597 */ 3598 for (len = config_list_len, vidptr = config_list; len > 0; 3599 vidptr += dupletlen, len -= dupletlen) { 3600 /* 3601 * Note: The assumption here is that each vid entry is on 3602 * a unique line from its associated duplet. 3603 */ 3604 vidlen = dupletlen = (int)strlen(vidptr); 3605 if ((vidlen == 0) || 3606 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) { 3607 dupletlen++; 3608 continue; 3609 } 3610 3611 /* 3612 * dnlist contains 1 or more blank separated 3613 * data-property-name entries 3614 */ 3615 dnlist_ptr = vidptr + vidlen + 1; 3616 dnlist_len = (int)strlen(dnlist_ptr); 3617 dupletlen += dnlist_len + 2; 3618 3619 /* 3620 * Set a pointer for the first data-property-name 3621 * entry in the list 3622 */ 3623 dataname_ptr = dnlist_ptr; 3624 dataname_len = 0; 3625 3626 /* 3627 * Loop through all data-property-name entries in the 3628 * data-property-name-list setting the properties for each. 3629 */ 3630 while (dataname_len < dnlist_len) { 3631 int version; 3632 3633 /* 3634 * Determine the length of the current 3635 * data-property-name entry by indexing until a 3636 * blank or NULL is encountered. When the space is 3637 * encountered reset it to a NULL for compliance 3638 * with ddi_getlongprop(). 3639 */ 3640 for (i = 0; ((dataname_ptr[i] != ' ') && 3641 (dataname_ptr[i] != '\0')); i++) { 3642 ; 3643 } 3644 3645 dataname_len += i; 3646 /* If not null terminated, Make it so */ 3647 if (dataname_ptr[i] == ' ') { 3648 dataname_ptr[i] = '\0'; 3649 } 3650 dataname_len++; 3651 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3652 "sd_process_sdconf_file: disk:%s, data:%s\n", 3653 vidptr, dataname_ptr); 3654 3655 /* Get the data list */ 3656 if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0, 3657 dataname_ptr, (caddr_t)&data_list, &data_list_len) 3658 != DDI_PROP_SUCCESS) { 3659 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3660 "sd_process_sdconf_file: data property (%s)" 3661 " has no value\n", dataname_ptr); 3662 dataname_ptr = dnlist_ptr + dataname_len; 3663 continue; 3664 } 3665 3666 version = data_list[0]; 3667 3668 if (version == SD_CONF_VERSION_1) { 3669 sd_tunables values; 3670 3671 /* Set the properties */ 3672 if (sd_chk_vers1_data(un, data_list[1], 3673 &data_list[2], data_list_len, dataname_ptr) 3674 == SD_SUCCESS) { 3675 sd_get_tunables_from_conf(un, 3676 data_list[1], &data_list[2], 3677 &values); 3678 sd_set_vers1_properties(un, 3679 data_list[1], &values); 3680 rval = SD_SUCCESS; 3681 } else { 3682 rval = SD_FAILURE; 3683 } 3684 } else { 3685 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 3686 "data property %s version 0x%x is invalid.", 3687 dataname_ptr, version); 3688 rval = SD_FAILURE; 3689 } 3690 kmem_free(data_list, data_list_len); 3691 dataname_ptr = dnlist_ptr + dataname_len; 3692 } 3693 } 3694 3695 /* free up the memory allocated by ddi_getlongprop */ 3696 if (config_list) { 3697 kmem_free(config_list, config_list_len); 3698 } 3699 3700 return (rval); 3701 } 3702 3703 /* 3704 * Function: sd_get_tunables_from_conf() 3705 * 3706 * 3707 * This function reads the data list from the sd.conf file and pulls 3708 * the values that can have numeric values as arguments and places 3709 * the values in the apropriate sd_tunables member. 3710 * Since the order of the data list members varies across platforms 3711 * This function reads them from the data list in a platform specific 3712 * order and places them into the correct sd_tunable member that is 3713 * a consistant across all platforms. 3714 */ 3715 static void 3716 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, 3717 sd_tunables *values) 3718 { 3719 int i; 3720 int mask; 3721 3722 bzero(values, sizeof (sd_tunables)); 3723 3724 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 3725 3726 mask = 1 << i; 3727 if (mask > flags) { 3728 break; 3729 } 3730 3731 switch (mask & flags) { 3732 case 0: /* This mask bit not set in flags */ 3733 continue; 3734 case SD_CONF_BSET_THROTTLE: 3735 values->sdt_throttle = data_list[i]; 3736 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3737 "sd_get_tunables_from_conf: throttle = %d\n", 3738 values->sdt_throttle); 3739 break; 3740 case SD_CONF_BSET_CTYPE: 3741 values->sdt_ctype = data_list[i]; 3742 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3743 "sd_get_tunables_from_conf: ctype = %d\n", 3744 values->sdt_ctype); 3745 break; 3746 case SD_CONF_BSET_NRR_COUNT: 3747 values->sdt_not_rdy_retries = data_list[i]; 3748 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3749 "sd_get_tunables_from_conf: not_rdy_retries = %d\n", 3750 values->sdt_not_rdy_retries); 3751 break; 3752 case SD_CONF_BSET_BSY_RETRY_COUNT: 3753 values->sdt_busy_retries = data_list[i]; 3754 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3755 "sd_get_tunables_from_conf: busy_retries = %d\n", 3756 values->sdt_busy_retries); 3757 break; 3758 case SD_CONF_BSET_RST_RETRIES: 3759 values->sdt_reset_retries = data_list[i]; 3760 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3761 "sd_get_tunables_from_conf: reset_retries = %d\n", 3762 values->sdt_reset_retries); 3763 break; 3764 case SD_CONF_BSET_RSV_REL_TIME: 3765 values->sdt_reserv_rel_time = data_list[i]; 3766 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3767 "sd_get_tunables_from_conf: reserv_rel_time = %d\n", 3768 values->sdt_reserv_rel_time); 3769 break; 3770 case SD_CONF_BSET_MIN_THROTTLE: 3771 values->sdt_min_throttle = data_list[i]; 3772 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3773 "sd_get_tunables_from_conf: min_throttle = %d\n", 3774 values->sdt_min_throttle); 3775 break; 3776 case SD_CONF_BSET_DISKSORT_DISABLED: 3777 values->sdt_disk_sort_dis = data_list[i]; 3778 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3779 "sd_get_tunables_from_conf: disk_sort_dis = %d\n", 3780 values->sdt_disk_sort_dis); 3781 break; 3782 case SD_CONF_BSET_LUN_RESET_ENABLED: 3783 values->sdt_lun_reset_enable = data_list[i]; 3784 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3785 "sd_get_tunables_from_conf: lun_reset_enable = %d" 3786 "\n", values->sdt_lun_reset_enable); 3787 break; 3788 } 3789 } 3790 } 3791 3792 /* 3793 * Function: sd_process_sdconf_table 3794 * 3795 * Description: Search the static configuration table for a match on the 3796 * inquiry vid/pid and update the driver soft state structure 3797 * according to the table property values for the device. 3798 * 3799 * The form of a configuration table entry is: 3800 * <vid+pid>,<flags>,<property-data> 3801 * "SEAGATE ST42400N",1,63,0,0 (Fibre) 3802 * "SEAGATE ST42400N",1,63,0,0,0,0 (Sparc) 3803 * "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0 (Intel) 3804 * 3805 * Arguments: un - driver soft state (unit) structure 3806 */ 3807 3808 static void 3809 sd_process_sdconf_table(struct sd_lun *un) 3810 { 3811 char *id = NULL; 3812 int table_index; 3813 int idlen; 3814 3815 ASSERT(un != NULL); 3816 for (table_index = 0; table_index < sd_disk_table_size; 3817 table_index++) { 3818 id = sd_disk_table[table_index].device_id; 3819 idlen = strlen(id); 3820 if (idlen == 0) { 3821 continue; 3822 } 3823 3824 /* 3825 * The static configuration table currently does not 3826 * implement version 10 properties. Additionally, 3827 * multiple data-property-name entries are not 3828 * implemented in the static configuration table. 3829 */ 3830 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 3831 SD_INFO(SD_LOG_ATTACH_DETACH, un, 3832 "sd_process_sdconf_table: disk %s\n", id); 3833 sd_set_vers1_properties(un, 3834 sd_disk_table[table_index].flags, 3835 sd_disk_table[table_index].properties); 3836 break; 3837 } 3838 } 3839 } 3840 3841 3842 /* 3843 * Function: sd_sdconf_id_match 3844 * 3845 * Description: This local function implements a case sensitive vid/pid 3846 * comparison as well as the boundary cases of wild card and 3847 * multiple blanks. 3848 * 3849 * Note: An implicit assumption made here is that the scsi 3850 * inquiry structure will always keep the vid, pid and 3851 * revision strings in consecutive sequence, so they can be 3852 * read as a single string. If this assumption is not the 3853 * case, a separate string, to be used for the check, needs 3854 * to be built with these strings concatenated. 3855 * 3856 * Arguments: un - driver soft state (unit) structure 3857 * id - table or config file vid/pid 3858 * idlen - length of the vid/pid (bytes) 3859 * 3860 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3861 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3862 */ 3863 3864 static int 3865 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen) 3866 { 3867 struct scsi_inquiry *sd_inq; 3868 int rval = SD_SUCCESS; 3869 3870 ASSERT(un != NULL); 3871 sd_inq = un->un_sd->sd_inq; 3872 ASSERT(id != NULL); 3873 3874 /* 3875 * We use the inq_vid as a pointer to a buffer containing the 3876 * vid and pid and use the entire vid/pid length of the table 3877 * entry for the comparison. This works because the inq_pid 3878 * data member follows inq_vid in the scsi_inquiry structure. 3879 */ 3880 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) { 3881 /* 3882 * The user id string is compared to the inquiry vid/pid 3883 * using a case insensitive comparison and ignoring 3884 * multiple spaces. 3885 */ 3886 rval = sd_blank_cmp(un, id, idlen); 3887 if (rval != SD_SUCCESS) { 3888 /* 3889 * User id strings that start and end with a "*" 3890 * are a special case. These do not have a 3891 * specific vendor, and the product string can 3892 * appear anywhere in the 16 byte PID portion of 3893 * the inquiry data. This is a simple strstr() 3894 * type search for the user id in the inquiry data. 3895 */ 3896 if ((id[0] == '*') && (id[idlen - 1] == '*')) { 3897 char *pidptr = &id[1]; 3898 int i; 3899 int j; 3900 int pidstrlen = idlen - 2; 3901 j = sizeof (SD_INQUIRY(un)->inq_pid) - 3902 pidstrlen; 3903 3904 if (j < 0) { 3905 return (SD_FAILURE); 3906 } 3907 for (i = 0; i < j; i++) { 3908 if (bcmp(&SD_INQUIRY(un)->inq_pid[i], 3909 pidptr, pidstrlen) == 0) { 3910 rval = SD_SUCCESS; 3911 break; 3912 } 3913 } 3914 } 3915 } 3916 } 3917 return (rval); 3918 } 3919 3920 3921 /* 3922 * Function: sd_blank_cmp 3923 * 3924 * Description: If the id string starts and ends with a space, treat 3925 * multiple consecutive spaces as equivalent to a single 3926 * space. For example, this causes a sd_disk_table entry 3927 * of " NEC CDROM " to match a device's id string of 3928 * "NEC CDROM". 3929 * 3930 * Note: The success exit condition for this routine is if 3931 * the pointer to the table entry is '\0' and the cnt of 3932 * the inquiry length is zero. This will happen if the inquiry 3933 * string returned by the device is padded with spaces to be 3934 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The 3935 * SCSI spec states that the inquiry string is to be padded with 3936 * spaces. 3937 * 3938 * Arguments: un - driver soft state (unit) structure 3939 * id - table or config file vid/pid 3940 * idlen - length of the vid/pid (bytes) 3941 * 3942 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid 3943 * SD_FAILURE - Indicates no match with the inquiry vid/pid 3944 */ 3945 3946 static int 3947 sd_blank_cmp(struct sd_lun *un, char *id, int idlen) 3948 { 3949 char *p1; 3950 char *p2; 3951 int cnt; 3952 cnt = sizeof (SD_INQUIRY(un)->inq_vid) + 3953 sizeof (SD_INQUIRY(un)->inq_pid); 3954 3955 ASSERT(un != NULL); 3956 p2 = un->un_sd->sd_inq->inq_vid; 3957 ASSERT(id != NULL); 3958 p1 = id; 3959 3960 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) { 3961 /* 3962 * Note: string p1 is terminated by a NUL but string p2 3963 * isn't. The end of p2 is determined by cnt. 3964 */ 3965 for (;;) { 3966 /* skip over any extra blanks in both strings */ 3967 while ((*p1 != '\0') && (*p1 == ' ')) { 3968 p1++; 3969 } 3970 while ((cnt != 0) && (*p2 == ' ')) { 3971 p2++; 3972 cnt--; 3973 } 3974 3975 /* compare the two strings */ 3976 if ((cnt == 0) || 3977 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) { 3978 break; 3979 } 3980 while ((cnt > 0) && 3981 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) { 3982 p1++; 3983 p2++; 3984 cnt--; 3985 } 3986 } 3987 } 3988 3989 /* return SD_SUCCESS if both strings match */ 3990 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE); 3991 } 3992 3993 3994 /* 3995 * Function: sd_chk_vers1_data 3996 * 3997 * Description: Verify the version 1 device properties provided by the 3998 * user via the configuration file 3999 * 4000 * Arguments: un - driver soft state (unit) structure 4001 * flags - integer mask indicating properties to be set 4002 * prop_list - integer list of property values 4003 * list_len - length of user provided data 4004 * 4005 * Return Code: SD_SUCCESS - Indicates the user provided data is valid 4006 * SD_FAILURE - Indicates the user provided data is invalid 4007 */ 4008 4009 static int 4010 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list, 4011 int list_len, char *dataname_ptr) 4012 { 4013 int i; 4014 int mask = 1; 4015 int index = 0; 4016 4017 ASSERT(un != NULL); 4018 4019 /* Check for a NULL property name and list */ 4020 if (dataname_ptr == NULL) { 4021 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4022 "sd_chk_vers1_data: NULL data property name."); 4023 return (SD_FAILURE); 4024 } 4025 if (prop_list == NULL) { 4026 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4027 "sd_chk_vers1_data: %s NULL data property list.", 4028 dataname_ptr); 4029 return (SD_FAILURE); 4030 } 4031 4032 /* Display a warning if undefined bits are set in the flags */ 4033 if (flags & ~SD_CONF_BIT_MASK) { 4034 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4035 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. " 4036 "Properties not set.", 4037 (flags & ~SD_CONF_BIT_MASK), dataname_ptr); 4038 return (SD_FAILURE); 4039 } 4040 4041 /* 4042 * Verify the length of the list by identifying the highest bit set 4043 * in the flags and validating that the property list has a length 4044 * up to the index of this bit. 4045 */ 4046 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) { 4047 if (flags & mask) { 4048 index++; 4049 } 4050 mask = 1 << i; 4051 } 4052 if ((list_len / sizeof (int)) < (index + 2)) { 4053 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4054 "sd_chk_vers1_data: " 4055 "Data property list %s size is incorrect. " 4056 "Properties not set.", dataname_ptr); 4057 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: " 4058 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS); 4059 return (SD_FAILURE); 4060 } 4061 return (SD_SUCCESS); 4062 } 4063 4064 4065 /* 4066 * Function: sd_set_vers1_properties 4067 * 4068 * Description: Set version 1 device properties based on a property list 4069 * retrieved from the driver configuration file or static 4070 * configuration table. Version 1 properties have the format: 4071 * 4072 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN> 4073 * 4074 * where the prop0 value will be used to set prop0 if bit0 4075 * is set in the flags 4076 * 4077 * Arguments: un - driver soft state (unit) structure 4078 * flags - integer mask indicating properties to be set 4079 * prop_list - integer list of property values 4080 */ 4081 4082 static void 4083 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list) 4084 { 4085 ASSERT(un != NULL); 4086 4087 /* 4088 * Set the flag to indicate cache is to be disabled. An attempt 4089 * to disable the cache via sd_cache_control() will be made 4090 * later during attach once the basic initialization is complete. 4091 */ 4092 if (flags & SD_CONF_BSET_NOCACHE) { 4093 un->un_f_opt_disable_cache = TRUE; 4094 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4095 "sd_set_vers1_properties: caching disabled flag set\n"); 4096 } 4097 4098 /* CD-specific configuration parameters */ 4099 if (flags & SD_CONF_BSET_PLAYMSF_BCD) { 4100 un->un_f_cfg_playmsf_bcd = TRUE; 4101 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4102 "sd_set_vers1_properties: playmsf_bcd set\n"); 4103 } 4104 if (flags & SD_CONF_BSET_READSUB_BCD) { 4105 un->un_f_cfg_readsub_bcd = TRUE; 4106 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4107 "sd_set_vers1_properties: readsub_bcd set\n"); 4108 } 4109 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) { 4110 un->un_f_cfg_read_toc_trk_bcd = TRUE; 4111 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4112 "sd_set_vers1_properties: read_toc_trk_bcd set\n"); 4113 } 4114 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) { 4115 un->un_f_cfg_read_toc_addr_bcd = TRUE; 4116 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4117 "sd_set_vers1_properties: read_toc_addr_bcd set\n"); 4118 } 4119 if (flags & SD_CONF_BSET_NO_READ_HEADER) { 4120 un->un_f_cfg_no_read_header = TRUE; 4121 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4122 "sd_set_vers1_properties: no_read_header set\n"); 4123 } 4124 if (flags & SD_CONF_BSET_READ_CD_XD4) { 4125 un->un_f_cfg_read_cd_xd4 = TRUE; 4126 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4127 "sd_set_vers1_properties: read_cd_xd4 set\n"); 4128 } 4129 4130 /* Support for devices which do not have valid/unique serial numbers */ 4131 if (flags & SD_CONF_BSET_FAB_DEVID) { 4132 un->un_f_opt_fab_devid = TRUE; 4133 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4134 "sd_set_vers1_properties: fab_devid bit set\n"); 4135 } 4136 4137 /* Support for user throttle configuration */ 4138 if (flags & SD_CONF_BSET_THROTTLE) { 4139 ASSERT(prop_list != NULL); 4140 un->un_saved_throttle = un->un_throttle = 4141 prop_list->sdt_throttle; 4142 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4143 "sd_set_vers1_properties: throttle set to %d\n", 4144 prop_list->sdt_throttle); 4145 } 4146 4147 /* Set the per disk retry count according to the conf file or table. */ 4148 if (flags & SD_CONF_BSET_NRR_COUNT) { 4149 ASSERT(prop_list != NULL); 4150 if (prop_list->sdt_not_rdy_retries) { 4151 un->un_notready_retry_count = 4152 prop_list->sdt_not_rdy_retries; 4153 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4154 "sd_set_vers1_properties: not ready retry count" 4155 " set to %d\n", un->un_notready_retry_count); 4156 } 4157 } 4158 4159 /* The controller type is reported for generic disk driver ioctls */ 4160 if (flags & SD_CONF_BSET_CTYPE) { 4161 ASSERT(prop_list != NULL); 4162 switch (prop_list->sdt_ctype) { 4163 case CTYPE_CDROM: 4164 un->un_ctype = prop_list->sdt_ctype; 4165 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4166 "sd_set_vers1_properties: ctype set to " 4167 "CTYPE_CDROM\n"); 4168 break; 4169 case CTYPE_CCS: 4170 un->un_ctype = prop_list->sdt_ctype; 4171 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4172 "sd_set_vers1_properties: ctype set to " 4173 "CTYPE_CCS\n"); 4174 break; 4175 case CTYPE_ROD: /* RW optical */ 4176 un->un_ctype = prop_list->sdt_ctype; 4177 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4178 "sd_set_vers1_properties: ctype set to " 4179 "CTYPE_ROD\n"); 4180 break; 4181 default: 4182 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4183 "sd_set_vers1_properties: Could not set " 4184 "invalid ctype value (%d)", 4185 prop_list->sdt_ctype); 4186 } 4187 } 4188 4189 /* Purple failover timeout */ 4190 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) { 4191 ASSERT(prop_list != NULL); 4192 un->un_busy_retry_count = 4193 prop_list->sdt_busy_retries; 4194 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4195 "sd_set_vers1_properties: " 4196 "busy retry count set to %d\n", 4197 un->un_busy_retry_count); 4198 } 4199 4200 /* Purple reset retry count */ 4201 if (flags & SD_CONF_BSET_RST_RETRIES) { 4202 ASSERT(prop_list != NULL); 4203 un->un_reset_retry_count = 4204 prop_list->sdt_reset_retries; 4205 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4206 "sd_set_vers1_properties: " 4207 "reset retry count set to %d\n", 4208 un->un_reset_retry_count); 4209 } 4210 4211 /* Purple reservation release timeout */ 4212 if (flags & SD_CONF_BSET_RSV_REL_TIME) { 4213 ASSERT(prop_list != NULL); 4214 un->un_reserve_release_time = 4215 prop_list->sdt_reserv_rel_time; 4216 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4217 "sd_set_vers1_properties: " 4218 "reservation release timeout set to %d\n", 4219 un->un_reserve_release_time); 4220 } 4221 4222 /* 4223 * Driver flag telling the driver to verify that no commands are pending 4224 * for a device before issuing a Test Unit Ready. This is a workaround 4225 * for a firmware bug in some Seagate eliteI drives. 4226 */ 4227 if (flags & SD_CONF_BSET_TUR_CHECK) { 4228 un->un_f_cfg_tur_check = TRUE; 4229 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4230 "sd_set_vers1_properties: tur queue check set\n"); 4231 } 4232 4233 if (flags & SD_CONF_BSET_MIN_THROTTLE) { 4234 un->un_min_throttle = prop_list->sdt_min_throttle; 4235 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4236 "sd_set_vers1_properties: min throttle set to %d\n", 4237 un->un_min_throttle); 4238 } 4239 4240 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) { 4241 un->un_f_disksort_disabled = 4242 (prop_list->sdt_disk_sort_dis != 0) ? 4243 TRUE : FALSE; 4244 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4245 "sd_set_vers1_properties: disksort disabled " 4246 "flag set to %d\n", 4247 prop_list->sdt_disk_sort_dis); 4248 } 4249 4250 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) { 4251 un->un_f_lun_reset_enabled = 4252 (prop_list->sdt_lun_reset_enable != 0) ? 4253 TRUE : FALSE; 4254 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4255 "sd_set_vers1_properties: lun reset enabled " 4256 "flag set to %d\n", 4257 prop_list->sdt_lun_reset_enable); 4258 } 4259 4260 /* 4261 * Validate the throttle values. 4262 * If any of the numbers are invalid, set everything to defaults. 4263 */ 4264 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) || 4265 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) || 4266 (un->un_min_throttle > un->un_throttle)) { 4267 un->un_saved_throttle = un->un_throttle = sd_max_throttle; 4268 un->un_min_throttle = sd_min_throttle; 4269 } 4270 } 4271 4272 /* 4273 * Function: sd_is_lsi() 4274 * 4275 * Description: Check for lsi devices, step throught the static device 4276 * table to match vid/pid. 4277 * 4278 * Args: un - ptr to sd_lun 4279 * 4280 * Notes: When creating new LSI property, need to add the new LSI property 4281 * to this function. 4282 */ 4283 static void 4284 sd_is_lsi(struct sd_lun *un) 4285 { 4286 char *id = NULL; 4287 int table_index; 4288 int idlen; 4289 void *prop; 4290 4291 ASSERT(un != NULL); 4292 for (table_index = 0; table_index < sd_disk_table_size; 4293 table_index++) { 4294 id = sd_disk_table[table_index].device_id; 4295 idlen = strlen(id); 4296 if (idlen == 0) { 4297 continue; 4298 } 4299 4300 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) { 4301 prop = sd_disk_table[table_index].properties; 4302 if (prop == &lsi_properties || 4303 prop == &lsi_oem_properties || 4304 prop == &lsi_properties_scsi || 4305 prop == &symbios_properties) { 4306 un->un_f_cfg_is_lsi = TRUE; 4307 } 4308 break; 4309 } 4310 } 4311 } 4312 4313 4314 /* 4315 * The following routines support reading and interpretation of disk labels, 4316 * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and 4317 * fdisk tables. 4318 */ 4319 4320 /* 4321 * Function: sd_validate_geometry 4322 * 4323 * Description: Read the label from the disk (if present). Update the unit's 4324 * geometry and vtoc information from the data in the label. 4325 * Verify that the label is valid. 4326 * 4327 * Arguments: un - driver soft state (unit) structure 4328 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4329 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4330 * to use the USCSI "direct" chain and bypass the normal 4331 * command waitq. 4332 * 4333 * Return Code: 0 - Successful completion 4334 * EINVAL - Invalid value in un->un_tgt_blocksize or 4335 * un->un_blockcount; or label on disk is corrupted 4336 * or unreadable. 4337 * EACCES - Reservation conflict at the device. 4338 * ENOMEM - Resource allocation error 4339 * ENOTSUP - geometry not applicable 4340 * 4341 * Context: Kernel thread only (can sleep). 4342 */ 4343 4344 static int 4345 sd_validate_geometry(struct sd_lun *un, int path_flag) 4346 { 4347 static char labelstring[128]; 4348 static char buf[256]; 4349 char *label = NULL; 4350 int label_error = 0; 4351 int gvalid = un->un_f_geometry_is_valid; 4352 int lbasize; 4353 uint_t capacity; 4354 int count; 4355 #if defined(__i386) || defined(__amd64) 4356 int forced_under_1t = 0; 4357 #endif 4358 4359 ASSERT(un != NULL); 4360 ASSERT(mutex_owned(SD_MUTEX(un))); 4361 4362 /* 4363 * If the required values are not valid, then try getting them 4364 * once via read capacity. If that fails, then fail this call. 4365 * This is necessary with the new mpxio failover behavior in 4366 * the T300 where we can get an attach for the inactive path 4367 * before the active path. The inactive path fails commands with 4368 * sense data of 02,04,88 which happens to the read capacity 4369 * before mpxio has had sufficient knowledge to know if it should 4370 * force a fail over or not. (Which it won't do at attach anyhow). 4371 * If the read capacity at attach time fails, un_tgt_blocksize and 4372 * un_blockcount won't be valid. 4373 */ 4374 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4375 (un->un_f_blockcount_is_valid != TRUE)) { 4376 uint64_t cap; 4377 uint32_t lbasz; 4378 int rval; 4379 4380 mutex_exit(SD_MUTEX(un)); 4381 rval = sd_send_scsi_READ_CAPACITY(un, &cap, 4382 &lbasz, SD_PATH_DIRECT); 4383 mutex_enter(SD_MUTEX(un)); 4384 if (rval == 0) { 4385 /* 4386 * The following relies on 4387 * sd_send_scsi_READ_CAPACITY never 4388 * returning 0 for capacity and/or lbasize. 4389 */ 4390 sd_update_block_info(un, lbasz, cap); 4391 } 4392 4393 if ((un->un_f_tgt_blocksize_is_valid != TRUE) || 4394 (un->un_f_blockcount_is_valid != TRUE)) { 4395 return (EINVAL); 4396 } 4397 } 4398 4399 /* 4400 * Copy the lbasize and capacity so that if they're reset while we're 4401 * not holding the SD_MUTEX, we will continue to use valid values 4402 * after the SD_MUTEX is reacquired. (4119659) 4403 */ 4404 lbasize = un->un_tgt_blocksize; 4405 capacity = un->un_blockcount; 4406 4407 #if defined(_SUNOS_VTOC_16) 4408 /* 4409 * Set up the "whole disk" fdisk partition; this should always 4410 * exist, regardless of whether the disk contains an fdisk table 4411 * or vtoc. 4412 */ 4413 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 4414 un->un_map[P0_RAW_DISK].dkl_nblk = capacity; 4415 #endif 4416 4417 /* 4418 * Refresh the logical and physical geometry caches. 4419 * (data from MODE SENSE format/rigid disk geometry pages, 4420 * and scsi_ifgetcap("geometry"). 4421 */ 4422 sd_resync_geom_caches(un, capacity, lbasize, path_flag); 4423 4424 label_error = sd_use_efi(un, path_flag); 4425 if (label_error == 0) { 4426 /* found a valid EFI label */ 4427 SD_TRACE(SD_LOG_IO_PARTITION, un, 4428 "sd_validate_geometry: found EFI label\n"); 4429 un->un_solaris_offset = 0; 4430 un->un_solaris_size = capacity; 4431 return (ENOTSUP); 4432 } 4433 if (un->un_blockcount > DK_MAX_BLOCKS) { 4434 if (label_error == ESRCH) { 4435 /* 4436 * they've configured a LUN over 1TB, but used 4437 * format.dat to restrict format's view of the 4438 * capacity to be under 1TB 4439 */ 4440 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 4441 "is >1TB and has a VTOC label: use format(1M) to either decrease the"); 4442 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 4443 "size to be < 1TB or relabel the disk with an EFI label"); 4444 #if defined(__i386) || defined(__amd64) 4445 forced_under_1t = 1; 4446 #endif 4447 } else { 4448 /* unlabeled disk over 1TB */ 4449 #if defined(__i386) || defined(__amd64) 4450 /* 4451 * Refer to comments on off-by-1 at the head of the file 4452 * A 1TB disk was treated as (1T - 512)B in the past, 4453 * thus, it might have valid solaris partition. We 4454 * will return ENOTSUP later only if this disk has no 4455 * valid solaris partition. 4456 */ 4457 if ((un->un_tgt_blocksize != un->un_sys_blocksize) || 4458 (un->un_blockcount - 1 > DK_MAX_BLOCKS) || 4459 un->un_f_has_removable_media || 4460 un->un_f_is_hotpluggable) 4461 #endif 4462 return (ENOTSUP); 4463 } 4464 } 4465 label_error = 0; 4466 4467 /* 4468 * at this point it is either labeled with a VTOC or it is 4469 * under 1TB (<= 1TB actually for off-by-1) 4470 */ 4471 if (un->un_f_vtoc_label_supported) { 4472 struct dk_label *dkl; 4473 offset_t dkl1; 4474 offset_t label_addr, real_addr; 4475 int rval; 4476 size_t buffer_size; 4477 4478 /* 4479 * Note: This will set up un->un_solaris_size and 4480 * un->un_solaris_offset. 4481 */ 4482 switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) { 4483 case SD_CMD_RESERVATION_CONFLICT: 4484 ASSERT(mutex_owned(SD_MUTEX(un))); 4485 return (EACCES); 4486 case SD_CMD_FAILURE: 4487 ASSERT(mutex_owned(SD_MUTEX(un))); 4488 return (ENOMEM); 4489 } 4490 4491 if (un->un_solaris_size <= DK_LABEL_LOC) { 4492 4493 #if defined(__i386) || defined(__amd64) 4494 /* 4495 * Refer to comments on off-by-1 at the head of the file 4496 * This is for 1TB disk only. Since that there is no 4497 * solaris partitions, return ENOTSUP as we do for 4498 * >1TB disk. 4499 */ 4500 if (un->un_blockcount > DK_MAX_BLOCKS) 4501 return (ENOTSUP); 4502 #endif 4503 /* 4504 * Found fdisk table but no Solaris partition entry, 4505 * so don't call sd_uselabel() and don't create 4506 * a default label. 4507 */ 4508 label_error = 0; 4509 un->un_f_geometry_is_valid = TRUE; 4510 goto no_solaris_partition; 4511 } 4512 label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC); 4513 4514 #if defined(__i386) || defined(__amd64) 4515 /* 4516 * Refer to comments on off-by-1 at the head of the file 4517 * Now, this 1TB disk has valid solaris partition. It 4518 * must be created by previous sd driver, we have to 4519 * treat it as (1T-512)B. 4520 */ 4521 if ((un->un_blockcount > DK_MAX_BLOCKS) && 4522 (forced_under_1t != 1)) { 4523 un->un_f_capacity_adjusted = 1; 4524 un->un_blockcount = DK_MAX_BLOCKS; 4525 un->un_map[P0_RAW_DISK].dkl_nblk = DK_MAX_BLOCKS; 4526 4527 /* 4528 * Refer to sd_read_fdisk, when there is no 4529 * fdisk partition table, un_solaris_size is 4530 * set to disk's capacity. In this case, we 4531 * need to adjust it 4532 */ 4533 if (un->un_solaris_size > DK_MAX_BLOCKS) 4534 un->un_solaris_size = DK_MAX_BLOCKS; 4535 sd_resync_geom_caches(un, DK_MAX_BLOCKS, 4536 lbasize, path_flag); 4537 } 4538 #endif 4539 4540 /* 4541 * sys_blocksize != tgt_blocksize, need to re-adjust 4542 * blkno and save the index to beginning of dk_label 4543 */ 4544 real_addr = SD_SYS2TGTBLOCK(un, label_addr); 4545 buffer_size = SD_REQBYTES2TGTBYTES(un, 4546 sizeof (struct dk_label)); 4547 4548 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4549 "label_addr: 0x%x allocation size: 0x%x\n", 4550 label_addr, buffer_size); 4551 dkl = kmem_zalloc(buffer_size, KM_NOSLEEP); 4552 if (dkl == NULL) { 4553 return (ENOMEM); 4554 } 4555 4556 mutex_exit(SD_MUTEX(un)); 4557 rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr, 4558 path_flag); 4559 mutex_enter(SD_MUTEX(un)); 4560 4561 switch (rval) { 4562 case 0: 4563 /* 4564 * sd_uselabel will establish that the geometry 4565 * is valid. 4566 * For sys_blocksize != tgt_blocksize, need 4567 * to index into the beginning of dk_label 4568 */ 4569 dkl1 = (daddr_t)dkl 4570 + SD_TGTBYTEOFFSET(un, label_addr, real_addr); 4571 if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1, 4572 path_flag) != SD_LABEL_IS_VALID) { 4573 label_error = EINVAL; 4574 } 4575 break; 4576 case EACCES: 4577 label_error = EACCES; 4578 break; 4579 default: 4580 label_error = EINVAL; 4581 break; 4582 } 4583 4584 kmem_free(dkl, buffer_size); 4585 4586 #if defined(_SUNOS_VTOC_8) 4587 label = (char *)un->un_asciilabel; 4588 #elif defined(_SUNOS_VTOC_16) 4589 label = (char *)un->un_vtoc.v_asciilabel; 4590 #else 4591 #error "No VTOC format defined." 4592 #endif 4593 } 4594 4595 /* 4596 * If a valid label was not found, AND if no reservation conflict 4597 * was detected, then go ahead and create a default label (4069506). 4598 */ 4599 if (un->un_f_default_vtoc_supported && (label_error != EACCES)) { 4600 if (un->un_f_geometry_is_valid == FALSE) { 4601 sd_build_default_label(un); 4602 } 4603 label_error = 0; 4604 } 4605 4606 no_solaris_partition: 4607 if ((!un->un_f_has_removable_media || 4608 (un->un_f_has_removable_media && 4609 un->un_mediastate == DKIO_EJECTED)) && 4610 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 4611 /* 4612 * Print out a message indicating who and what we are. 4613 * We do this only when we happen to really validate the 4614 * geometry. We may call sd_validate_geometry() at other 4615 * times, e.g., ioctl()'s like Get VTOC in which case we 4616 * don't want to print the label. 4617 * If the geometry is valid, print the label string, 4618 * else print vendor and product info, if available 4619 */ 4620 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 4621 SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label); 4622 } else { 4623 mutex_enter(&sd_label_mutex); 4624 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 4625 labelstring); 4626 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 4627 &labelstring[64]); 4628 (void) sprintf(buf, "?Vendor '%s', product '%s'", 4629 labelstring, &labelstring[64]); 4630 if (un->un_f_blockcount_is_valid == TRUE) { 4631 (void) sprintf(&buf[strlen(buf)], 4632 ", %llu %u byte blocks\n", 4633 (longlong_t)un->un_blockcount, 4634 un->un_tgt_blocksize); 4635 } else { 4636 (void) sprintf(&buf[strlen(buf)], 4637 ", (unknown capacity)\n"); 4638 } 4639 SD_INFO(SD_LOG_ATTACH_DETACH, un, buf); 4640 mutex_exit(&sd_label_mutex); 4641 } 4642 } 4643 4644 #if defined(_SUNOS_VTOC_16) 4645 /* 4646 * If we have valid geometry, set up the remaining fdisk partitions. 4647 * Note that dkl_cylno is not used for the fdisk map entries, so 4648 * we set it to an entirely bogus value. 4649 */ 4650 for (count = 0; count < FD_NUMPART; count++) { 4651 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 4652 un->un_map[FDISK_P1 + count].dkl_nblk = 4653 un->un_fmap[count].fmap_nblk; 4654 4655 un->un_offset[FDISK_P1 + count] = 4656 un->un_fmap[count].fmap_start; 4657 } 4658 #endif 4659 4660 for (count = 0; count < NDKMAP; count++) { 4661 #if defined(_SUNOS_VTOC_8) 4662 struct dk_map *lp = &un->un_map[count]; 4663 un->un_offset[count] = 4664 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 4665 #elif defined(_SUNOS_VTOC_16) 4666 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 4667 4668 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 4669 #else 4670 #error "No VTOC format defined." 4671 #endif 4672 } 4673 4674 /* 4675 * For VTOC labeled disk, create and set the partition stats 4676 * at attach time, update the stats according to dynamic 4677 * partition changes during running time. 4678 */ 4679 if (label_error == 0 && un->un_f_pkstats_enabled) { 4680 sd_set_pstats(un); 4681 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: " 4682 "un:0x%p pstats created and set, or updated\n", un); 4683 } 4684 4685 return (label_error); 4686 } 4687 4688 4689 #if defined(_SUNOS_VTOC_16) 4690 /* 4691 * Macro: MAX_BLKS 4692 * 4693 * This macro is used for table entries where we need to have the largest 4694 * possible sector value for that head & SPT (sectors per track) 4695 * combination. Other entries for some smaller disk sizes are set by 4696 * convention to match those used by X86 BIOS usage. 4697 */ 4698 #define MAX_BLKS(heads, spt) UINT16_MAX * heads * spt, heads, spt 4699 4700 /* 4701 * Function: sd_convert_geometry 4702 * 4703 * Description: Convert physical geometry into a dk_geom structure. In 4704 * other words, make sure we don't wrap 16-bit values. 4705 * e.g. converting from geom_cache to dk_geom 4706 * 4707 * Context: Kernel thread only 4708 */ 4709 static void 4710 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g) 4711 { 4712 int i; 4713 static const struct chs_values { 4714 uint_t max_cap; /* Max Capacity for this HS. */ 4715 uint_t nhead; /* Heads to use. */ 4716 uint_t nsect; /* SPT to use. */ 4717 } CHS_values[] = { 4718 {0x00200000, 64, 32}, /* 1GB or smaller disk. */ 4719 {0x01000000, 128, 32}, /* 8GB or smaller disk. */ 4720 {MAX_BLKS(255, 63)}, /* 502.02GB or smaller disk. */ 4721 {MAX_BLKS(255, 126)}, /* .98TB or smaller disk. */ 4722 {DK_MAX_BLOCKS, 255, 189} /* Max size is just under 1TB */ 4723 }; 4724 4725 /* Unlabeled SCSI floppy device */ 4726 if (capacity <= 0x1000) { 4727 un_g->dkg_nhead = 2; 4728 un_g->dkg_ncyl = 80; 4729 un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl); 4730 return; 4731 } 4732 4733 /* 4734 * For all devices we calculate cylinders using the 4735 * heads and sectors we assign based on capacity of the 4736 * device. The table is designed to be compatible with the 4737 * way other operating systems lay out fdisk tables for X86 4738 * and to insure that the cylinders never exceed 65535 to 4739 * prevent problems with X86 ioctls that report geometry. 4740 * We use SPT that are multiples of 63, since other OSes that 4741 * are not limited to 16-bits for cylinders stop at 63 SPT 4742 * we make do by using multiples of 63 SPT. 4743 * 4744 * Note than capacities greater than or equal to 1TB will simply 4745 * get the largest geometry from the table. This should be okay 4746 * since disks this large shouldn't be using CHS values anyway. 4747 */ 4748 for (i = 0; CHS_values[i].max_cap < capacity && 4749 CHS_values[i].max_cap != DK_MAX_BLOCKS; i++) 4750 ; 4751 4752 un_g->dkg_nhead = CHS_values[i].nhead; 4753 un_g->dkg_nsect = CHS_values[i].nsect; 4754 } 4755 #endif 4756 4757 4758 /* 4759 * Function: sd_resync_geom_caches 4760 * 4761 * Description: (Re)initialize both geometry caches: the virtual geometry 4762 * information is extracted from the HBA (the "geometry" 4763 * capability), and the physical geometry cache data is 4764 * generated by issuing MODE SENSE commands. 4765 * 4766 * Arguments: un - driver soft state (unit) structure 4767 * capacity - disk capacity in #blocks 4768 * lbasize - disk block size in bytes 4769 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4770 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4771 * to use the USCSI "direct" chain and bypass the normal 4772 * command waitq. 4773 * 4774 * Context: Kernel thread only (can sleep). 4775 */ 4776 4777 static void 4778 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize, 4779 int path_flag) 4780 { 4781 struct geom_cache pgeom; 4782 struct geom_cache *pgeom_p = &pgeom; 4783 int spc; 4784 unsigned short nhead; 4785 unsigned short nsect; 4786 4787 ASSERT(un != NULL); 4788 ASSERT(mutex_owned(SD_MUTEX(un))); 4789 4790 /* 4791 * Ask the controller for its logical geometry. 4792 * Note: if the HBA does not support scsi_ifgetcap("geometry"), 4793 * then the lgeom cache will be invalid. 4794 */ 4795 sd_get_virtual_geometry(un, capacity, lbasize); 4796 4797 /* 4798 * Initialize the pgeom cache from lgeom, so that if MODE SENSE 4799 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values. 4800 */ 4801 if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) { 4802 /* 4803 * Note: Perhaps this needs to be more adaptive? The rationale 4804 * is that, if there's no HBA geometry from the HBA driver, any 4805 * guess is good, since this is the physical geometry. If MODE 4806 * SENSE fails this gives a max cylinder size for non-LBA access 4807 */ 4808 nhead = 255; 4809 nsect = 63; 4810 } else { 4811 nhead = un->un_lgeom.g_nhead; 4812 nsect = un->un_lgeom.g_nsect; 4813 } 4814 4815 if (ISCD(un)) { 4816 pgeom_p->g_nhead = 1; 4817 pgeom_p->g_nsect = nsect * nhead; 4818 } else { 4819 pgeom_p->g_nhead = nhead; 4820 pgeom_p->g_nsect = nsect; 4821 } 4822 4823 spc = pgeom_p->g_nhead * pgeom_p->g_nsect; 4824 pgeom_p->g_capacity = capacity; 4825 pgeom_p->g_ncyl = pgeom_p->g_capacity / spc; 4826 pgeom_p->g_acyl = 0; 4827 4828 /* 4829 * Retrieve fresh geometry data from the hardware, stash it 4830 * here temporarily before we rebuild the incore label. 4831 * 4832 * We want to use the MODE SENSE commands to derive the 4833 * physical geometry of the device, but if either command 4834 * fails, the logical geometry is used as the fallback for 4835 * disk label geometry. 4836 */ 4837 mutex_exit(SD_MUTEX(un)); 4838 sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag); 4839 mutex_enter(SD_MUTEX(un)); 4840 4841 /* 4842 * Now update the real copy while holding the mutex. This 4843 * way the global copy is never in an inconsistent state. 4844 */ 4845 bcopy(pgeom_p, &un->un_pgeom, sizeof (un->un_pgeom)); 4846 4847 SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: " 4848 "(cached from lgeom)\n"); 4849 SD_INFO(SD_LOG_COMMON, un, 4850 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 4851 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 4852 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 4853 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 4854 "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize, 4855 un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv, 4856 un->un_pgeom.g_rpm); 4857 } 4858 4859 4860 /* 4861 * Function: sd_read_fdisk 4862 * 4863 * Description: utility routine to read the fdisk table. 4864 * 4865 * Arguments: un - driver soft state (unit) structure 4866 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 4867 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 4868 * to use the USCSI "direct" chain and bypass the normal 4869 * command waitq. 4870 * 4871 * Return Code: SD_CMD_SUCCESS 4872 * SD_CMD_FAILURE 4873 * 4874 * Context: Kernel thread only (can sleep). 4875 */ 4876 /* ARGSUSED */ 4877 static int 4878 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag) 4879 { 4880 #if defined(_NO_FDISK_PRESENT) 4881 4882 un->un_solaris_offset = 0; 4883 un->un_solaris_size = capacity; 4884 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4885 return (SD_CMD_SUCCESS); 4886 4887 #elif defined(_FIRMWARE_NEEDS_FDISK) 4888 4889 struct ipart *fdp; 4890 struct mboot *mbp; 4891 struct ipart fdisk[FD_NUMPART]; 4892 int i; 4893 char sigbuf[2]; 4894 caddr_t bufp; 4895 int uidx; 4896 int rval; 4897 int lba = 0; 4898 uint_t solaris_offset; /* offset to solaris part. */ 4899 daddr_t solaris_size; /* size of solaris partition */ 4900 uint32_t blocksize; 4901 4902 ASSERT(un != NULL); 4903 ASSERT(mutex_owned(SD_MUTEX(un))); 4904 ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE); 4905 4906 blocksize = un->un_tgt_blocksize; 4907 4908 /* 4909 * Start off assuming no fdisk table 4910 */ 4911 solaris_offset = 0; 4912 solaris_size = capacity; 4913 4914 mutex_exit(SD_MUTEX(un)); 4915 bufp = kmem_zalloc(blocksize, KM_SLEEP); 4916 rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag); 4917 mutex_enter(SD_MUTEX(un)); 4918 4919 if (rval != 0) { 4920 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4921 "sd_read_fdisk: fdisk read err\n"); 4922 kmem_free(bufp, blocksize); 4923 return (SD_CMD_FAILURE); 4924 } 4925 4926 mbp = (struct mboot *)bufp; 4927 4928 /* 4929 * The fdisk table does not begin on a 4-byte boundary within the 4930 * master boot record, so we copy it to an aligned structure to avoid 4931 * alignment exceptions on some processors. 4932 */ 4933 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk)); 4934 4935 /* 4936 * Check for lba support before verifying sig; sig might not be 4937 * there, say on a blank disk, but the max_chs mark may still 4938 * be present. 4939 * 4940 * Note: LBA support and BEFs are an x86-only concept but this 4941 * code should work OK on SPARC as well. 4942 */ 4943 4944 /* 4945 * First, check for lba-access-ok on root node (or prom root node) 4946 * if present there, don't need to search fdisk table. 4947 */ 4948 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 4949 "lba-access-ok", 0) != 0) { 4950 /* All drives do LBA; don't search fdisk table */ 4951 lba = 1; 4952 } else { 4953 /* Okay, look for mark in fdisk table */ 4954 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 4955 /* accumulate "lba" value from all partitions */ 4956 lba = (lba || sd_has_max_chs_vals(fdp)); 4957 } 4958 } 4959 4960 if (lba != 0) { 4961 dev_t dev = sd_make_device(SD_DEVINFO(un)); 4962 4963 if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS, 4964 "lba-access-ok", 0) == 0) { 4965 /* not found; create it */ 4966 if (ddi_prop_create(dev, SD_DEVINFO(un), 0, 4967 "lba-access-ok", (caddr_t)NULL, 0) != 4968 DDI_PROP_SUCCESS) { 4969 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4970 "sd_read_fdisk: Can't create lba property " 4971 "for instance %d\n", 4972 ddi_get_instance(SD_DEVINFO(un))); 4973 } 4974 } 4975 } 4976 4977 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf)); 4978 4979 /* 4980 * Endian-independent signature check 4981 */ 4982 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) || 4983 (sigbuf[0] != (MBB_MAGIC & 0xFF))) { 4984 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 4985 "sd_read_fdisk: no fdisk\n"); 4986 bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART); 4987 rval = SD_CMD_SUCCESS; 4988 goto done; 4989 } 4990 4991 #ifdef SDDEBUG 4992 if (sd_level_mask & SD_LOGMASK_INFO) { 4993 fdp = fdisk; 4994 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n"); 4995 SD_INFO(SD_LOG_ATTACH_DETACH, un, " relsect " 4996 "numsect sysid bootid\n"); 4997 for (i = 0; i < FD_NUMPART; i++, fdp++) { 4998 SD_INFO(SD_LOG_ATTACH_DETACH, un, 4999 " %d: %8d %8d 0x%08x 0x%08x\n", 5000 i, fdp->relsect, fdp->numsect, 5001 fdp->systid, fdp->bootid); 5002 } 5003 } 5004 #endif 5005 5006 /* 5007 * Try to find the unix partition 5008 */ 5009 uidx = -1; 5010 solaris_offset = 0; 5011 solaris_size = 0; 5012 5013 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) { 5014 int relsect; 5015 int numsect; 5016 5017 if (fdp->numsect == 0) { 5018 un->un_fmap[i].fmap_start = 0; 5019 un->un_fmap[i].fmap_nblk = 0; 5020 continue; 5021 } 5022 5023 /* 5024 * Data in the fdisk table is little-endian. 5025 */ 5026 relsect = LE_32(fdp->relsect); 5027 numsect = LE_32(fdp->numsect); 5028 5029 un->un_fmap[i].fmap_start = relsect; 5030 un->un_fmap[i].fmap_nblk = numsect; 5031 5032 if (fdp->systid != SUNIXOS && 5033 fdp->systid != SUNIXOS2 && 5034 fdp->systid != EFI_PMBR) { 5035 continue; 5036 } 5037 5038 /* 5039 * use the last active solaris partition id found 5040 * (there should only be 1 active partition id) 5041 * 5042 * if there are no active solaris partition id 5043 * then use the first inactive solaris partition id 5044 */ 5045 if ((uidx == -1) || (fdp->bootid == ACTIVE)) { 5046 uidx = i; 5047 solaris_offset = relsect; 5048 solaris_size = numsect; 5049 } 5050 } 5051 5052 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx", 5053 un->un_solaris_offset, un->un_solaris_size); 5054 5055 rval = SD_CMD_SUCCESS; 5056 5057 done: 5058 5059 /* 5060 * Clear the VTOC info, only if the Solaris partition entry 5061 * has moved, changed size, been deleted, or if the size of 5062 * the partition is too small to even fit the label sector. 5063 */ 5064 if ((un->un_solaris_offset != solaris_offset) || 5065 (un->un_solaris_size != solaris_size) || 5066 solaris_size <= DK_LABEL_LOC) { 5067 SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx", 5068 solaris_offset, solaris_size); 5069 bzero(&un->un_g, sizeof (struct dk_geom)); 5070 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5071 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 5072 un->un_f_geometry_is_valid = FALSE; 5073 } 5074 un->un_solaris_offset = solaris_offset; 5075 un->un_solaris_size = solaris_size; 5076 kmem_free(bufp, blocksize); 5077 return (rval); 5078 5079 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */ 5080 #error "fdisk table presence undetermined for this platform." 5081 #endif /* #if defined(_NO_FDISK_PRESENT) */ 5082 } 5083 5084 5085 /* 5086 * Function: sd_get_physical_geometry 5087 * 5088 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and 5089 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the 5090 * target, and use this information to initialize the physical 5091 * geometry cache specified by pgeom_p. 5092 * 5093 * MODE SENSE is an optional command, so failure in this case 5094 * does not necessarily denote an error. We want to use the 5095 * MODE SENSE commands to derive the physical geometry of the 5096 * device, but if either command fails, the logical geometry is 5097 * used as the fallback for disk label geometry. 5098 * 5099 * This requires that un->un_blockcount and un->un_tgt_blocksize 5100 * have already been initialized for the current target and 5101 * that the current values be passed as args so that we don't 5102 * end up ever trying to use -1 as a valid value. This could 5103 * happen if either value is reset while we're not holding 5104 * the mutex. 5105 * 5106 * Arguments: un - driver soft state (unit) structure 5107 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5108 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5109 * to use the USCSI "direct" chain and bypass the normal 5110 * command waitq. 5111 * 5112 * Context: Kernel thread only (can sleep). 5113 */ 5114 5115 static void 5116 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p, 5117 int capacity, int lbasize, int path_flag) 5118 { 5119 struct mode_format *page3p; 5120 struct mode_geometry *page4p; 5121 struct mode_header *headerp; 5122 int sector_size; 5123 int nsect; 5124 int nhead; 5125 int ncyl; 5126 int intrlv; 5127 int spc; 5128 int modesense_capacity; 5129 int rpm; 5130 int bd_len; 5131 int mode_header_length; 5132 uchar_t *p3bufp; 5133 uchar_t *p4bufp; 5134 int cdbsize; 5135 5136 ASSERT(un != NULL); 5137 ASSERT(!(mutex_owned(SD_MUTEX(un)))); 5138 5139 if (un->un_f_blockcount_is_valid != TRUE) { 5140 return; 5141 } 5142 5143 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 5144 return; 5145 } 5146 5147 if (lbasize == 0) { 5148 if (ISCD(un)) { 5149 lbasize = 2048; 5150 } else { 5151 lbasize = un->un_sys_blocksize; 5152 } 5153 } 5154 pgeom_p->g_secsize = (unsigned short)lbasize; 5155 5156 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0; 5157 5158 /* 5159 * Retrieve MODE SENSE page 3 - Format Device Page 5160 */ 5161 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP); 5162 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp, 5163 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag) 5164 != 0) { 5165 SD_ERROR(SD_LOG_COMMON, un, 5166 "sd_get_physical_geometry: mode sense page 3 failed\n"); 5167 goto page3_exit; 5168 } 5169 5170 /* 5171 * Determine size of Block Descriptors in order to locate the mode 5172 * page data. ATAPI devices return 0, SCSI devices should return 5173 * MODE_BLK_DESC_LENGTH. 5174 */ 5175 headerp = (struct mode_header *)p3bufp; 5176 if (un->un_f_cfg_is_atapi == TRUE) { 5177 struct mode_header_grp2 *mhp = 5178 (struct mode_header_grp2 *)headerp; 5179 mode_header_length = MODE_HEADER_LENGTH_GRP2; 5180 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5181 } else { 5182 mode_header_length = MODE_HEADER_LENGTH; 5183 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5184 } 5185 5186 if (bd_len > MODE_BLK_DESC_LENGTH) { 5187 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5188 "received unexpected bd_len of %d, page3\n", bd_len); 5189 goto page3_exit; 5190 } 5191 5192 page3p = (struct mode_format *) 5193 ((caddr_t)headerp + mode_header_length + bd_len); 5194 5195 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) { 5196 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5197 "mode sense pg3 code mismatch %d\n", 5198 page3p->mode_page.code); 5199 goto page3_exit; 5200 } 5201 5202 /* 5203 * Use this physical geometry data only if BOTH MODE SENSE commands 5204 * complete successfully; otherwise, revert to the logical geometry. 5205 * So, we need to save everything in temporary variables. 5206 */ 5207 sector_size = BE_16(page3p->data_bytes_sect); 5208 5209 /* 5210 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size 5211 */ 5212 if (sector_size == 0) { 5213 sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize; 5214 } else { 5215 sector_size &= ~(un->un_sys_blocksize - 1); 5216 } 5217 5218 nsect = BE_16(page3p->sect_track); 5219 intrlv = BE_16(page3p->interleave); 5220 5221 SD_INFO(SD_LOG_COMMON, un, 5222 "sd_get_physical_geometry: Format Parameters (page 3)\n"); 5223 SD_INFO(SD_LOG_COMMON, un, 5224 " mode page: %d; nsect: %d; sector size: %d;\n", 5225 page3p->mode_page.code, nsect, sector_size); 5226 SD_INFO(SD_LOG_COMMON, un, 5227 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv, 5228 BE_16(page3p->track_skew), 5229 BE_16(page3p->cylinder_skew)); 5230 5231 5232 /* 5233 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page 5234 */ 5235 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP); 5236 if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp, 5237 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag) 5238 != 0) { 5239 SD_ERROR(SD_LOG_COMMON, un, 5240 "sd_get_physical_geometry: mode sense page 4 failed\n"); 5241 goto page4_exit; 5242 } 5243 5244 /* 5245 * Determine size of Block Descriptors in order to locate the mode 5246 * page data. ATAPI devices return 0, SCSI devices should return 5247 * MODE_BLK_DESC_LENGTH. 5248 */ 5249 headerp = (struct mode_header *)p4bufp; 5250 if (un->un_f_cfg_is_atapi == TRUE) { 5251 struct mode_header_grp2 *mhp = 5252 (struct mode_header_grp2 *)headerp; 5253 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 5254 } else { 5255 bd_len = ((struct mode_header *)headerp)->bdesc_length; 5256 } 5257 5258 if (bd_len > MODE_BLK_DESC_LENGTH) { 5259 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5260 "received unexpected bd_len of %d, page4\n", bd_len); 5261 goto page4_exit; 5262 } 5263 5264 page4p = (struct mode_geometry *) 5265 ((caddr_t)headerp + mode_header_length + bd_len); 5266 5267 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) { 5268 SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: " 5269 "mode sense pg4 code mismatch %d\n", 5270 page4p->mode_page.code); 5271 goto page4_exit; 5272 } 5273 5274 /* 5275 * Stash the data now, after we know that both commands completed. 5276 */ 5277 5278 mutex_enter(SD_MUTEX(un)); 5279 5280 nhead = (int)page4p->heads; /* uchar, so no conversion needed */ 5281 spc = nhead * nsect; 5282 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb; 5283 rpm = BE_16(page4p->rpm); 5284 5285 modesense_capacity = spc * ncyl; 5286 5287 SD_INFO(SD_LOG_COMMON, un, 5288 "sd_get_physical_geometry: Geometry Parameters (page 4)\n"); 5289 SD_INFO(SD_LOG_COMMON, un, 5290 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm); 5291 SD_INFO(SD_LOG_COMMON, un, 5292 " computed capacity(h*s*c): %d;\n", modesense_capacity); 5293 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n", 5294 (void *)pgeom_p, capacity); 5295 5296 /* 5297 * Compensate if the drive's geometry is not rectangular, i.e., 5298 * the product of C * H * S returned by MODE SENSE >= that returned 5299 * by read capacity. This is an idiosyncrasy of the original x86 5300 * disk subsystem. 5301 */ 5302 if (modesense_capacity >= capacity) { 5303 SD_INFO(SD_LOG_COMMON, un, 5304 "sd_get_physical_geometry: adjusting acyl; " 5305 "old: %d; new: %d\n", pgeom_p->g_acyl, 5306 (modesense_capacity - capacity + spc - 1) / spc); 5307 if (sector_size != 0) { 5308 /* 1243403: NEC D38x7 drives don't support sec size */ 5309 pgeom_p->g_secsize = (unsigned short)sector_size; 5310 } 5311 pgeom_p->g_nsect = (unsigned short)nsect; 5312 pgeom_p->g_nhead = (unsigned short)nhead; 5313 pgeom_p->g_capacity = capacity; 5314 pgeom_p->g_acyl = 5315 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc; 5316 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl; 5317 } 5318 5319 pgeom_p->g_rpm = (unsigned short)rpm; 5320 pgeom_p->g_intrlv = (unsigned short)intrlv; 5321 5322 SD_INFO(SD_LOG_COMMON, un, 5323 "sd_get_physical_geometry: mode sense geometry:\n"); 5324 SD_INFO(SD_LOG_COMMON, un, 5325 " nsect: %d; sector size: %d; interlv: %d\n", 5326 nsect, sector_size, intrlv); 5327 SD_INFO(SD_LOG_COMMON, un, 5328 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n", 5329 nhead, ncyl, rpm, modesense_capacity); 5330 SD_INFO(SD_LOG_COMMON, un, 5331 "sd_get_physical_geometry: (cached)\n"); 5332 SD_INFO(SD_LOG_COMMON, un, 5333 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5334 un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl, 5335 un->un_pgeom.g_nhead, un->un_pgeom.g_nsect); 5336 SD_INFO(SD_LOG_COMMON, un, 5337 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n", 5338 un->un_pgeom.g_secsize, un->un_pgeom.g_capacity, 5339 un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm); 5340 5341 mutex_exit(SD_MUTEX(un)); 5342 5343 page4_exit: 5344 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH); 5345 page3_exit: 5346 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH); 5347 } 5348 5349 5350 /* 5351 * Function: sd_get_virtual_geometry 5352 * 5353 * Description: Ask the controller to tell us about the target device. 5354 * 5355 * Arguments: un - pointer to softstate 5356 * capacity - disk capacity in #blocks 5357 * lbasize - disk block size in bytes 5358 * 5359 * Context: Kernel thread only 5360 */ 5361 5362 static void 5363 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize) 5364 { 5365 struct geom_cache *lgeom_p = &un->un_lgeom; 5366 uint_t geombuf; 5367 int spc; 5368 5369 ASSERT(un != NULL); 5370 ASSERT(mutex_owned(SD_MUTEX(un))); 5371 5372 mutex_exit(SD_MUTEX(un)); 5373 5374 /* Set sector size, and total number of sectors */ 5375 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1); 5376 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1); 5377 5378 /* Let the HBA tell us its geometry */ 5379 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1); 5380 5381 mutex_enter(SD_MUTEX(un)); 5382 5383 /* A value of -1 indicates an undefined "geometry" property */ 5384 if (geombuf == (-1)) { 5385 return; 5386 } 5387 5388 /* Initialize the logical geometry cache. */ 5389 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff; 5390 lgeom_p->g_nsect = geombuf & 0xffff; 5391 lgeom_p->g_secsize = un->un_sys_blocksize; 5392 5393 spc = lgeom_p->g_nhead * lgeom_p->g_nsect; 5394 5395 /* 5396 * Note: The driver originally converted the capacity value from 5397 * target blocks to system blocks. However, the capacity value passed 5398 * to this routine is already in terms of system blocks (this scaling 5399 * is done when the READ CAPACITY command is issued and processed). 5400 * This 'error' may have gone undetected because the usage of g_ncyl 5401 * (which is based upon g_capacity) is very limited within the driver 5402 */ 5403 lgeom_p->g_capacity = capacity; 5404 5405 /* 5406 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The 5407 * hba may return zero values if the device has been removed. 5408 */ 5409 if (spc == 0) { 5410 lgeom_p->g_ncyl = 0; 5411 } else { 5412 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc; 5413 } 5414 lgeom_p->g_acyl = 0; 5415 5416 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n"); 5417 SD_INFO(SD_LOG_COMMON, un, 5418 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n", 5419 un->un_lgeom.g_ncyl, un->un_lgeom.g_acyl, 5420 un->un_lgeom.g_nhead, un->un_lgeom.g_nsect); 5421 SD_INFO(SD_LOG_COMMON, un, " lbasize: %d; capacity: %ld; " 5422 "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize, 5423 un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm); 5424 } 5425 5426 5427 /* 5428 * Function: sd_update_block_info 5429 * 5430 * Description: Calculate a byte count to sector count bitshift value 5431 * from sector size. 5432 * 5433 * Arguments: un: unit struct. 5434 * lbasize: new target sector size 5435 * capacity: new target capacity, ie. block count 5436 * 5437 * Context: Kernel thread context 5438 */ 5439 5440 static void 5441 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity) 5442 { 5443 if (lbasize != 0) { 5444 un->un_tgt_blocksize = lbasize; 5445 un->un_f_tgt_blocksize_is_valid = TRUE; 5446 } 5447 5448 if (capacity != 0) { 5449 un->un_blockcount = capacity; 5450 un->un_f_blockcount_is_valid = TRUE; 5451 } 5452 } 5453 5454 5455 static void 5456 sd_swap_efi_gpt(efi_gpt_t *e) 5457 { 5458 _NOTE(ASSUMING_PROTECTED(*e)) 5459 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature); 5460 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision); 5461 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize); 5462 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32); 5463 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA); 5464 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA); 5465 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA); 5466 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA); 5467 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID); 5468 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA); 5469 e->efi_gpt_NumberOfPartitionEntries = 5470 LE_32(e->efi_gpt_NumberOfPartitionEntries); 5471 e->efi_gpt_SizeOfPartitionEntry = 5472 LE_32(e->efi_gpt_SizeOfPartitionEntry); 5473 e->efi_gpt_PartitionEntryArrayCRC32 = 5474 LE_32(e->efi_gpt_PartitionEntryArrayCRC32); 5475 } 5476 5477 static void 5478 sd_swap_efi_gpe(int nparts, efi_gpe_t *p) 5479 { 5480 int i; 5481 5482 _NOTE(ASSUMING_PROTECTED(*p)) 5483 for (i = 0; i < nparts; i++) { 5484 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID, 5485 p[i].efi_gpe_PartitionTypeGUID); 5486 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA); 5487 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA); 5488 /* PartitionAttrs */ 5489 } 5490 } 5491 5492 static int 5493 sd_validate_efi(efi_gpt_t *labp) 5494 { 5495 if (labp->efi_gpt_Signature != EFI_SIGNATURE) 5496 return (EINVAL); 5497 /* at least 96 bytes in this version of the spec. */ 5498 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) > 5499 labp->efi_gpt_HeaderSize) 5500 return (EINVAL); 5501 /* this should be 128 bytes */ 5502 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t)) 5503 return (EINVAL); 5504 return (0); 5505 } 5506 5507 static int 5508 sd_use_efi(struct sd_lun *un, int path_flag) 5509 { 5510 int i; 5511 int rval = 0; 5512 efi_gpe_t *partitions; 5513 uchar_t *buf; 5514 uint_t lbasize; 5515 uint64_t cap = 0; 5516 uint_t nparts; 5517 diskaddr_t gpe_lba; 5518 struct uuid uuid_type_reserved = EFI_RESERVED; 5519 5520 ASSERT(mutex_owned(SD_MUTEX(un))); 5521 lbasize = un->un_tgt_blocksize; 5522 un->un_reserved = -1; 5523 5524 mutex_exit(SD_MUTEX(un)); 5525 5526 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 5527 5528 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 5529 rval = EINVAL; 5530 goto done_err; 5531 } 5532 5533 rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag); 5534 if (rval) { 5535 goto done_err; 5536 } 5537 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) { 5538 /* not ours */ 5539 rval = ESRCH; 5540 goto done_err; 5541 } 5542 5543 rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag); 5544 if (rval) { 5545 goto done_err; 5546 } 5547 sd_swap_efi_gpt((efi_gpt_t *)buf); 5548 5549 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5550 /* 5551 * Couldn't read the primary, try the backup. Our 5552 * capacity at this point could be based on CHS, so 5553 * check what the device reports. 5554 */ 5555 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5556 path_flag); 5557 if (rval) { 5558 goto done_err; 5559 } 5560 5561 /* 5562 * The MMC standard allows READ CAPACITY to be 5563 * inaccurate by a bounded amount (in the interest of 5564 * response latency). As a result, failed READs are 5565 * commonplace (due to the reading of metadata and not 5566 * data). Depending on the per-Vendor/drive Sense data, 5567 * the failed READ can cause many (unnecessary) retries. 5568 */ 5569 5570 /* 5571 * Refer to comments related to off-by-1 at the 5572 * header of this file. Search the next to last 5573 * block for backup EFI label. 5574 */ 5575 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5576 cap - 2, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5577 path_flag)) != 0) { 5578 goto done_err; 5579 } 5580 5581 sd_swap_efi_gpt((efi_gpt_t *)buf); 5582 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) { 5583 if ((rval = sd_send_scsi_READ(un, buf, lbasize, 5584 cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY : 5585 path_flag)) != 0) { 5586 goto done_err; 5587 } 5588 sd_swap_efi_gpt((efi_gpt_t *)buf); 5589 if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) 5590 goto done_err; 5591 } 5592 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5593 "primary label corrupt; using backup\n"); 5594 } 5595 5596 if (cap == 0) 5597 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 5598 path_flag); 5599 5600 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries; 5601 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA; 5602 5603 rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba, 5604 path_flag); 5605 if (rval) { 5606 goto done_err; 5607 } 5608 partitions = (efi_gpe_t *)buf; 5609 5610 if (nparts > MAXPART) { 5611 nparts = MAXPART; 5612 } 5613 sd_swap_efi_gpe(nparts, partitions); 5614 5615 mutex_enter(SD_MUTEX(un)); 5616 5617 /* Fill in partition table. */ 5618 for (i = 0; i < nparts; i++) { 5619 if (partitions->efi_gpe_StartingLBA != 0 || 5620 partitions->efi_gpe_EndingLBA != 0) { 5621 un->un_map[i].dkl_cylno = 5622 partitions->efi_gpe_StartingLBA; 5623 un->un_map[i].dkl_nblk = 5624 partitions->efi_gpe_EndingLBA - 5625 partitions->efi_gpe_StartingLBA + 1; 5626 un->un_offset[i] = 5627 partitions->efi_gpe_StartingLBA; 5628 } 5629 if (un->un_reserved == -1) { 5630 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID, 5631 &uuid_type_reserved, sizeof (struct uuid)) == 0) { 5632 un->un_reserved = i; 5633 } 5634 } 5635 if (i == WD_NODE) { 5636 /* 5637 * minor number 7 corresponds to the whole disk 5638 */ 5639 un->un_map[i].dkl_cylno = 0; 5640 un->un_map[i].dkl_nblk = un->un_blockcount; 5641 un->un_offset[i] = 0; 5642 } 5643 partitions++; 5644 } 5645 un->un_solaris_offset = 0; 5646 un->un_solaris_size = cap; 5647 un->un_f_geometry_is_valid = TRUE; 5648 5649 /* clear the vtoc label */ 5650 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 5651 5652 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5653 5654 /* 5655 * For EFI labeled disk, create and set the partition stats 5656 * at attach time, update the stats according to dynamic 5657 * partition changes during running time. 5658 */ 5659 if (un->un_f_pkstats_enabled) { 5660 sd_set_pstats(un); 5661 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_use_efi: " 5662 "un:0x%p pstats created and set, or updated\n", un); 5663 } 5664 return (0); 5665 5666 done_err: 5667 kmem_free(buf, EFI_MIN_ARRAY_SIZE); 5668 mutex_enter(SD_MUTEX(un)); 5669 /* 5670 * if we didn't find something that could look like a VTOC 5671 * and the disk is over 1TB, we know there isn't a valid label. 5672 * Otherwise let sd_uselabel decide what to do. We only 5673 * want to invalidate this if we're certain the label isn't 5674 * valid because sd_prop_op will now fail, which in turn 5675 * causes things like opens and stats on the partition to fail. 5676 */ 5677 if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) { 5678 un->un_f_geometry_is_valid = FALSE; 5679 } 5680 return (rval); 5681 } 5682 5683 5684 /* 5685 * Function: sd_uselabel 5686 * 5687 * Description: Validate the disk label and update the relevant data (geometry, 5688 * partition, vtoc, and capacity data) in the sd_lun struct. 5689 * Marks the geometry of the unit as being valid. 5690 * 5691 * Arguments: un: unit struct. 5692 * dk_label: disk label 5693 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 5694 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 5695 * to use the USCSI "direct" chain and bypass the normal 5696 * command waitq. 5697 * 5698 * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry, 5699 * partition, vtoc, and capacity data are good. 5700 * 5701 * SD_LABEL_IS_INVALID: Magic number or checksum error in the 5702 * label; or computed capacity does not jibe with capacity 5703 * reported from the READ CAPACITY command. 5704 * 5705 * Context: Kernel thread only (can sleep). 5706 */ 5707 5708 static int 5709 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag) 5710 { 5711 short *sp; 5712 short sum; 5713 short count; 5714 int label_error = SD_LABEL_IS_VALID; 5715 int i; 5716 int capacity; 5717 int part_end; 5718 int track_capacity; 5719 int err; 5720 #if defined(_SUNOS_VTOC_16) 5721 struct dkl_partition *vpartp; 5722 #endif 5723 ASSERT(un != NULL); 5724 ASSERT(mutex_owned(SD_MUTEX(un))); 5725 5726 /* Validate the magic number of the label. */ 5727 if (labp->dkl_magic != DKL_MAGIC) { 5728 #if defined(__sparc) 5729 if ((un->un_state == SD_STATE_NORMAL) && 5730 un->un_f_vtoc_errlog_supported) { 5731 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5732 "Corrupt label; wrong magic number\n"); 5733 } 5734 #endif 5735 return (SD_LABEL_IS_INVALID); 5736 } 5737 5738 /* Validate the checksum of the label. */ 5739 sp = (short *)labp; 5740 sum = 0; 5741 count = sizeof (struct dk_label) / sizeof (short); 5742 while (count--) { 5743 sum ^= *sp++; 5744 } 5745 5746 if (sum != 0) { 5747 #if defined(_SUNOS_VTOC_16) 5748 if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) { 5749 #elif defined(_SUNOS_VTOC_8) 5750 if ((un->un_state == SD_STATE_NORMAL) && 5751 un->un_f_vtoc_errlog_supported) { 5752 #endif 5753 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5754 "Corrupt label - label checksum failed\n"); 5755 } 5756 return (SD_LABEL_IS_INVALID); 5757 } 5758 5759 5760 /* 5761 * Fill in geometry structure with data from label. 5762 */ 5763 bzero(&un->un_g, sizeof (struct dk_geom)); 5764 un->un_g.dkg_ncyl = labp->dkl_ncyl; 5765 un->un_g.dkg_acyl = labp->dkl_acyl; 5766 un->un_g.dkg_bcyl = 0; 5767 un->un_g.dkg_nhead = labp->dkl_nhead; 5768 un->un_g.dkg_nsect = labp->dkl_nsect; 5769 un->un_g.dkg_intrlv = labp->dkl_intrlv; 5770 5771 #if defined(_SUNOS_VTOC_8) 5772 un->un_g.dkg_gap1 = labp->dkl_gap1; 5773 un->un_g.dkg_gap2 = labp->dkl_gap2; 5774 un->un_g.dkg_bhead = labp->dkl_bhead; 5775 #endif 5776 #if defined(_SUNOS_VTOC_16) 5777 un->un_dkg_skew = labp->dkl_skew; 5778 #endif 5779 5780 #if defined(__i386) || defined(__amd64) 5781 un->un_g.dkg_apc = labp->dkl_apc; 5782 #endif 5783 5784 /* 5785 * Currently we rely on the values in the label being accurate. If 5786 * dlk_rpm or dlk_pcly are zero in the label, use a default value. 5787 * 5788 * Note: In the future a MODE SENSE may be used to retrieve this data, 5789 * although this command is optional in SCSI-2. 5790 */ 5791 un->un_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600; 5792 un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl : 5793 (un->un_g.dkg_ncyl + un->un_g.dkg_acyl); 5794 5795 /* 5796 * The Read and Write reinstruct values may not be valid 5797 * for older disks. 5798 */ 5799 un->un_g.dkg_read_reinstruct = labp->dkl_read_reinstruct; 5800 un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct; 5801 5802 /* Fill in partition table. */ 5803 #if defined(_SUNOS_VTOC_8) 5804 for (i = 0; i < NDKMAP; i++) { 5805 un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno; 5806 un->un_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk; 5807 } 5808 #endif 5809 #if defined(_SUNOS_VTOC_16) 5810 vpartp = labp->dkl_vtoc.v_part; 5811 track_capacity = labp->dkl_nhead * labp->dkl_nsect; 5812 5813 /* Prevent divide by zero */ 5814 if (track_capacity == 0) { 5815 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5816 "Corrupt label - zero nhead or nsect value\n"); 5817 5818 return (SD_LABEL_IS_INVALID); 5819 } 5820 5821 for (i = 0; i < NDKMAP; i++, vpartp++) { 5822 un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity; 5823 un->un_map[i].dkl_nblk = vpartp->p_size; 5824 } 5825 #endif 5826 5827 /* Fill in VTOC Structure. */ 5828 bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc)); 5829 #if defined(_SUNOS_VTOC_8) 5830 /* 5831 * The 8-slice vtoc does not include the ascii label; save it into 5832 * the device's soft state structure here. 5833 */ 5834 bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 5835 #endif 5836 5837 /* Now look for a valid capacity. */ 5838 track_capacity = (un->un_g.dkg_nhead * un->un_g.dkg_nsect); 5839 capacity = (un->un_g.dkg_ncyl * track_capacity); 5840 5841 if (un->un_g.dkg_acyl) { 5842 #if defined(__i386) || defined(__amd64) 5843 /* we may have > 1 alts cylinder */ 5844 capacity += (track_capacity * un->un_g.dkg_acyl); 5845 #else 5846 capacity += track_capacity; 5847 #endif 5848 } 5849 5850 /* 5851 * Force check here to ensure the computed capacity is valid. 5852 * If capacity is zero, it indicates an invalid label and 5853 * we should abort updating the relevant data then. 5854 */ 5855 if (capacity == 0) { 5856 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5857 "Corrupt label - no valid capacity could be retrieved\n"); 5858 5859 return (SD_LABEL_IS_INVALID); 5860 } 5861 5862 /* Mark the geometry as valid. */ 5863 un->un_f_geometry_is_valid = TRUE; 5864 5865 /* 5866 * At this point, un->un_blockcount should contain valid data from 5867 * the READ CAPACITY command. 5868 */ 5869 if (un->un_f_blockcount_is_valid != TRUE) { 5870 /* 5871 * We have a situation where the target didn't give us a good 5872 * READ CAPACITY value, yet there appears to be a valid label. 5873 * In this case, we'll fake the capacity. 5874 */ 5875 un->un_blockcount = capacity; 5876 un->un_f_blockcount_is_valid = TRUE; 5877 goto done; 5878 } 5879 5880 5881 if ((capacity <= un->un_blockcount) || 5882 (un->un_state != SD_STATE_NORMAL)) { 5883 #if defined(_SUNOS_VTOC_8) 5884 /* 5885 * We can't let this happen on drives that are subdivided 5886 * into logical disks (i.e., that have an fdisk table). 5887 * The un_blockcount field should always hold the full media 5888 * size in sectors, period. This code would overwrite 5889 * un_blockcount with the size of the Solaris fdisk partition. 5890 */ 5891 SD_ERROR(SD_LOG_COMMON, un, 5892 "sd_uselabel: Label %d blocks; Drive %d blocks\n", 5893 capacity, un->un_blockcount); 5894 un->un_blockcount = capacity; 5895 un->un_f_blockcount_is_valid = TRUE; 5896 #endif /* defined(_SUNOS_VTOC_8) */ 5897 goto done; 5898 } 5899 5900 if (ISCD(un)) { 5901 /* For CDROMs, we trust that the data in the label is OK. */ 5902 #if defined(_SUNOS_VTOC_8) 5903 for (i = 0; i < NDKMAP; i++) { 5904 part_end = labp->dkl_nhead * labp->dkl_nsect * 5905 labp->dkl_map[i].dkl_cylno + 5906 labp->dkl_map[i].dkl_nblk - 1; 5907 5908 if ((labp->dkl_map[i].dkl_nblk) && 5909 (part_end > un->un_blockcount)) { 5910 un->un_f_geometry_is_valid = FALSE; 5911 break; 5912 } 5913 } 5914 #endif 5915 #if defined(_SUNOS_VTOC_16) 5916 vpartp = &(labp->dkl_vtoc.v_part[0]); 5917 for (i = 0; i < NDKMAP; i++, vpartp++) { 5918 part_end = vpartp->p_start + vpartp->p_size; 5919 if ((vpartp->p_size > 0) && 5920 (part_end > un->un_blockcount)) { 5921 un->un_f_geometry_is_valid = FALSE; 5922 break; 5923 } 5924 } 5925 #endif 5926 } else { 5927 uint64_t t_capacity; 5928 uint32_t t_lbasize; 5929 5930 mutex_exit(SD_MUTEX(un)); 5931 err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize, 5932 path_flag); 5933 ASSERT(t_capacity <= DK_MAX_BLOCKS); 5934 mutex_enter(SD_MUTEX(un)); 5935 5936 if (err == 0) { 5937 sd_update_block_info(un, t_lbasize, t_capacity); 5938 } 5939 5940 if (capacity > un->un_blockcount) { 5941 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 5942 "Corrupt label - bad geometry\n"); 5943 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 5944 "Label says %u blocks; Drive says %llu blocks\n", 5945 capacity, (unsigned long long)un->un_blockcount); 5946 un->un_f_geometry_is_valid = FALSE; 5947 label_error = SD_LABEL_IS_INVALID; 5948 } 5949 } 5950 5951 done: 5952 5953 SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n"); 5954 SD_INFO(SD_LOG_COMMON, un, 5955 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n", 5956 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 5957 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 5958 SD_INFO(SD_LOG_COMMON, un, 5959 " lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n", 5960 un->un_tgt_blocksize, un->un_blockcount, 5961 un->un_g.dkg_intrlv, un->un_g.dkg_rpm); 5962 SD_INFO(SD_LOG_COMMON, un, " wrt_reinstr: %d; rd_reinstr: %d\n", 5963 un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct); 5964 5965 ASSERT(mutex_owned(SD_MUTEX(un))); 5966 5967 return (label_error); 5968 } 5969 5970 5971 /* 5972 * Function: sd_build_default_label 5973 * 5974 * Description: Generate a default label for those devices that do not have 5975 * one, e.g., new media, removable cartridges, etc.. 5976 * 5977 * Context: Kernel thread only 5978 */ 5979 5980 static void 5981 sd_build_default_label(struct sd_lun *un) 5982 { 5983 #if defined(_SUNOS_VTOC_16) 5984 uint_t phys_spc; 5985 uint_t disksize; 5986 struct dk_geom un_g; 5987 uint64_t capacity; 5988 #endif 5989 5990 ASSERT(un != NULL); 5991 ASSERT(mutex_owned(SD_MUTEX(un))); 5992 5993 #if defined(_SUNOS_VTOC_8) 5994 /* 5995 * Note: This is a legacy check for non-removable devices on VTOC_8 5996 * only. This may be a valid check for VTOC_16 as well. 5997 * Once we understand why there is this difference between SPARC and 5998 * x86 platform, we could remove this legacy check. 5999 */ 6000 ASSERT(un->un_f_default_vtoc_supported); 6001 #endif 6002 6003 bzero(&un->un_g, sizeof (struct dk_geom)); 6004 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 6005 bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map))); 6006 6007 #if defined(_SUNOS_VTOC_8) 6008 6009 /* 6010 * It's a REMOVABLE media, therefore no label (on sparc, anyway). 6011 * But it is still necessary to set up various geometry information, 6012 * and we are doing this here. 6013 */ 6014 6015 /* 6016 * For the rpm, we use the minimum for the disk. For the head, cyl, 6017 * and number of sector per track, if the capacity <= 1GB, head = 64, 6018 * sect = 32. else head = 255, sect 63 Note: the capacity should be 6019 * equal to C*H*S values. This will cause some truncation of size due 6020 * to round off errors. For CD-ROMs, this truncation can have adverse 6021 * side effects, so returning ncyl and nhead as 1. The nsect will 6022 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569) 6023 */ 6024 if (ISCD(un)) { 6025 /* 6026 * Preserve the old behavior for non-writable 6027 * medias. Since dkg_nsect is a ushort, it 6028 * will lose bits as cdroms have more than 6029 * 65536 sectors. So if we recalculate 6030 * capacity, it will become much shorter. 6031 * But the dkg_* information is not 6032 * used for CDROMs so it is OK. But for 6033 * Writable CDs we need this information 6034 * to be valid (for newfs say). So we 6035 * make nsect and nhead > 1 that way 6036 * nsect can still stay within ushort limit 6037 * without losing any bits. 6038 */ 6039 if (un->un_f_mmc_writable_media == TRUE) { 6040 un->un_g.dkg_nhead = 64; 6041 un->un_g.dkg_nsect = 32; 6042 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6043 un->un_blockcount = un->un_g.dkg_ncyl * 6044 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6045 } else { 6046 un->un_g.dkg_ncyl = 1; 6047 un->un_g.dkg_nhead = 1; 6048 un->un_g.dkg_nsect = un->un_blockcount; 6049 } 6050 } else { 6051 if (un->un_blockcount <= 0x1000) { 6052 /* unlabeled SCSI floppy device */ 6053 un->un_g.dkg_nhead = 2; 6054 un->un_g.dkg_ncyl = 80; 6055 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 6056 } else if (un->un_blockcount <= 0x200000) { 6057 un->un_g.dkg_nhead = 64; 6058 un->un_g.dkg_nsect = 32; 6059 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 6060 } else { 6061 un->un_g.dkg_nhead = 255; 6062 un->un_g.dkg_nsect = 63; 6063 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 6064 } 6065 un->un_blockcount = 6066 un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6067 } 6068 6069 un->un_g.dkg_acyl = 0; 6070 un->un_g.dkg_bcyl = 0; 6071 un->un_g.dkg_rpm = 200; 6072 un->un_asciilabel[0] = '\0'; 6073 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl; 6074 6075 un->un_map[0].dkl_cylno = 0; 6076 un->un_map[0].dkl_nblk = un->un_blockcount; 6077 un->un_map[2].dkl_cylno = 0; 6078 un->un_map[2].dkl_nblk = un->un_blockcount; 6079 6080 #elif defined(_SUNOS_VTOC_16) 6081 6082 if (un->un_solaris_size == 0) { 6083 /* 6084 * Got fdisk table but no solaris entry therefore 6085 * don't create a default label 6086 */ 6087 un->un_f_geometry_is_valid = TRUE; 6088 return; 6089 } 6090 6091 /* 6092 * For CDs we continue to use the physical geometry to calculate 6093 * number of cylinders. All other devices must convert the 6094 * physical geometry (geom_cache) to values that will fit 6095 * in a dk_geom structure. 6096 */ 6097 if (ISCD(un)) { 6098 phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect; 6099 } else { 6100 /* Convert physical geometry to disk geometry */ 6101 bzero(&un_g, sizeof (struct dk_geom)); 6102 6103 /* 6104 * Refer to comments related to off-by-1 at the 6105 * header of this file. 6106 * Before caculating geometry, capacity should be 6107 * decreased by 1. That un_f_capacity_adjusted is 6108 * TRUE means that we are treating a 1TB disk as 6109 * (1T - 512)B. And the capacity of disks is already 6110 * decreased by 1. 6111 */ 6112 if (!un->un_f_capacity_adjusted && 6113 !un->un_f_has_removable_media && 6114 !un->un_f_is_hotpluggable && 6115 un->un_tgt_blocksize == un->un_sys_blocksize) 6116 capacity = un->un_blockcount - 1; 6117 else 6118 capacity = un->un_blockcount; 6119 6120 sd_convert_geometry(capacity, &un_g); 6121 bcopy(&un_g, &un->un_g, sizeof (un->un_g)); 6122 phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6123 } 6124 6125 ASSERT(phys_spc != 0); 6126 un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc; 6127 un->un_g.dkg_acyl = DK_ACYL; 6128 un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL; 6129 disksize = un->un_g.dkg_ncyl * phys_spc; 6130 6131 if (ISCD(un)) { 6132 /* 6133 * CD's don't use the "heads * sectors * cyls"-type of 6134 * geometry, but instead use the entire capacity of the media. 6135 */ 6136 disksize = un->un_solaris_size; 6137 un->un_g.dkg_nhead = 1; 6138 un->un_g.dkg_nsect = 1; 6139 un->un_g.dkg_rpm = 6140 (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm; 6141 6142 un->un_vtoc.v_part[0].p_start = 0; 6143 un->un_vtoc.v_part[0].p_size = disksize; 6144 un->un_vtoc.v_part[0].p_tag = V_BACKUP; 6145 un->un_vtoc.v_part[0].p_flag = V_UNMNT; 6146 6147 un->un_map[0].dkl_cylno = 0; 6148 un->un_map[0].dkl_nblk = disksize; 6149 un->un_offset[0] = 0; 6150 6151 } else { 6152 /* 6153 * Hard disks and removable media cartridges 6154 */ 6155 un->un_g.dkg_rpm = 6156 (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm; 6157 un->un_vtoc.v_sectorsz = un->un_sys_blocksize; 6158 6159 /* Add boot slice */ 6160 un->un_vtoc.v_part[8].p_start = 0; 6161 un->un_vtoc.v_part[8].p_size = phys_spc; 6162 un->un_vtoc.v_part[8].p_tag = V_BOOT; 6163 un->un_vtoc.v_part[8].p_flag = V_UNMNT; 6164 6165 un->un_map[8].dkl_cylno = 0; 6166 un->un_map[8].dkl_nblk = phys_spc; 6167 un->un_offset[8] = 0; 6168 } 6169 6170 un->un_g.dkg_apc = 0; 6171 un->un_vtoc.v_nparts = V_NUMPAR; 6172 un->un_vtoc.v_version = V_VERSION; 6173 6174 /* Add backup slice */ 6175 un->un_vtoc.v_part[2].p_start = 0; 6176 un->un_vtoc.v_part[2].p_size = disksize; 6177 un->un_vtoc.v_part[2].p_tag = V_BACKUP; 6178 un->un_vtoc.v_part[2].p_flag = V_UNMNT; 6179 6180 un->un_map[2].dkl_cylno = 0; 6181 un->un_map[2].dkl_nblk = disksize; 6182 un->un_offset[2] = 0; 6183 6184 (void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d" 6185 " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl, 6186 un->un_g.dkg_nhead, un->un_g.dkg_nsect); 6187 6188 #else 6189 #error "No VTOC format defined." 6190 #endif 6191 6192 un->un_g.dkg_read_reinstruct = 0; 6193 un->un_g.dkg_write_reinstruct = 0; 6194 6195 un->un_g.dkg_intrlv = 1; 6196 6197 un->un_vtoc.v_sanity = VTOC_SANE; 6198 6199 un->un_f_geometry_is_valid = TRUE; 6200 6201 SD_INFO(SD_LOG_COMMON, un, 6202 "sd_build_default_label: Default label created: " 6203 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n", 6204 un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead, 6205 un->un_g.dkg_nsect, un->un_blockcount); 6206 } 6207 6208 6209 #if defined(_FIRMWARE_NEEDS_FDISK) 6210 /* 6211 * Max CHS values, as they are encoded into bytes, for 1022/254/63 6212 */ 6213 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2)) 6214 #define LBA_MAX_CYL (1022 & 0xFF) 6215 #define LBA_MAX_HEAD (254) 6216 6217 6218 /* 6219 * Function: sd_has_max_chs_vals 6220 * 6221 * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum. 6222 * 6223 * Arguments: fdp - ptr to CHS info 6224 * 6225 * Return Code: True or false 6226 * 6227 * Context: Any. 6228 */ 6229 6230 static int 6231 sd_has_max_chs_vals(struct ipart *fdp) 6232 { 6233 return ((fdp->begcyl == LBA_MAX_CYL) && 6234 (fdp->beghead == LBA_MAX_HEAD) && 6235 (fdp->begsect == LBA_MAX_SECT) && 6236 (fdp->endcyl == LBA_MAX_CYL) && 6237 (fdp->endhead == LBA_MAX_HEAD) && 6238 (fdp->endsect == LBA_MAX_SECT)); 6239 } 6240 #endif 6241 6242 6243 /* 6244 * Function: sd_inq_fill 6245 * 6246 * Description: Print a piece of inquiry data, cleaned up for non-printable 6247 * characters and stopping at the first space character after 6248 * the beginning of the passed string; 6249 * 6250 * Arguments: p - source string 6251 * l - maximum length to copy 6252 * s - destination string 6253 * 6254 * Context: Any. 6255 */ 6256 6257 static void 6258 sd_inq_fill(char *p, int l, char *s) 6259 { 6260 unsigned i = 0; 6261 char c; 6262 6263 while (i++ < l) { 6264 if ((c = *p++) < ' ' || c >= 0x7F) { 6265 c = '*'; 6266 } else if (i != 1 && c == ' ') { 6267 break; 6268 } 6269 *s++ = c; 6270 } 6271 *s++ = 0; 6272 } 6273 6274 6275 /* 6276 * Function: sd_register_devid 6277 * 6278 * Description: This routine will obtain the device id information from the 6279 * target, obtain the serial number, and register the device 6280 * id with the ddi framework. 6281 * 6282 * Arguments: devi - the system's dev_info_t for the device. 6283 * un - driver soft state (unit) structure 6284 * reservation_flag - indicates if a reservation conflict 6285 * occurred during attach 6286 * 6287 * Context: Kernel Thread 6288 */ 6289 static void 6290 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag) 6291 { 6292 int rval = 0; 6293 uchar_t *inq80 = NULL; 6294 size_t inq80_len = MAX_INQUIRY_SIZE; 6295 size_t inq80_resid = 0; 6296 uchar_t *inq83 = NULL; 6297 size_t inq83_len = MAX_INQUIRY_SIZE; 6298 size_t inq83_resid = 0; 6299 6300 ASSERT(un != NULL); 6301 ASSERT(mutex_owned(SD_MUTEX(un))); 6302 ASSERT((SD_DEVINFO(un)) == devi); 6303 6304 /* 6305 * This is the case of antiquated Sun disk drives that have the 6306 * FAB_DEVID property set in the disk_table. These drives 6307 * manage the devid's by storing them in last 2 available sectors 6308 * on the drive and have them fabricated by the ddi layer by calling 6309 * ddi_devid_init and passing the DEVID_FAB flag. 6310 */ 6311 if (un->un_f_opt_fab_devid == TRUE) { 6312 /* 6313 * Depending on EINVAL isn't reliable, since a reserved disk 6314 * may result in invalid geometry, so check to make sure a 6315 * reservation conflict did not occur during attach. 6316 */ 6317 if ((sd_get_devid(un) == EINVAL) && 6318 (reservation_flag != SD_TARGET_IS_RESERVED)) { 6319 /* 6320 * The devid is invalid AND there is no reservation 6321 * conflict. Fabricate a new devid. 6322 */ 6323 (void) sd_create_devid(un); 6324 } 6325 6326 /* Register the devid if it exists */ 6327 if (un->un_devid != NULL) { 6328 (void) ddi_devid_register(SD_DEVINFO(un), 6329 un->un_devid); 6330 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6331 "sd_register_devid: Devid Fabricated\n"); 6332 } 6333 return; 6334 } 6335 6336 /* 6337 * We check the availibility of the World Wide Name (0x83) and Unit 6338 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using 6339 * un_vpd_page_mask from them, we decide which way to get the WWN. If 6340 * 0x83 is availible, that is the best choice. Our next choice is 6341 * 0x80. If neither are availible, we munge the devid from the device 6342 * vid/pid/serial # for Sun qualified disks, or use the ddi framework 6343 * to fabricate a devid for non-Sun qualified disks. 6344 */ 6345 if (sd_check_vpd_page_support(un) == 0) { 6346 /* collect page 80 data if available */ 6347 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) { 6348 6349 mutex_exit(SD_MUTEX(un)); 6350 inq80 = kmem_zalloc(inq80_len, KM_SLEEP); 6351 rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len, 6352 0x01, 0x80, &inq80_resid); 6353 6354 if (rval != 0) { 6355 kmem_free(inq80, inq80_len); 6356 inq80 = NULL; 6357 inq80_len = 0; 6358 } 6359 mutex_enter(SD_MUTEX(un)); 6360 } 6361 6362 /* collect page 83 data if available */ 6363 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) { 6364 mutex_exit(SD_MUTEX(un)); 6365 inq83 = kmem_zalloc(inq83_len, KM_SLEEP); 6366 rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len, 6367 0x01, 0x83, &inq83_resid); 6368 6369 if (rval != 0) { 6370 kmem_free(inq83, inq83_len); 6371 inq83 = NULL; 6372 inq83_len = 0; 6373 } 6374 mutex_enter(SD_MUTEX(un)); 6375 } 6376 } 6377 6378 /* encode best devid possible based on data available */ 6379 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, 6380 (char *)ddi_driver_name(SD_DEVINFO(un)), 6381 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)), 6382 inq80, inq80_len - inq80_resid, inq83, inq83_len - 6383 inq83_resid, &un->un_devid) == DDI_SUCCESS) { 6384 6385 /* devid successfully encoded, register devid */ 6386 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid); 6387 6388 } else { 6389 /* 6390 * Unable to encode a devid based on data available. 6391 * This is not a Sun qualified disk. Older Sun disk 6392 * drives that have the SD_FAB_DEVID property 6393 * set in the disk_table and non Sun qualified 6394 * disks are treated in the same manner. These 6395 * drives manage the devid's by storing them in 6396 * last 2 available sectors on the drive and 6397 * have them fabricated by the ddi layer by 6398 * calling ddi_devid_init and passing the 6399 * DEVID_FAB flag. 6400 * Create a fabricate devid only if there's no 6401 * fabricate devid existed. 6402 */ 6403 if (sd_get_devid(un) == EINVAL) { 6404 (void) sd_create_devid(un); 6405 } 6406 un->un_f_opt_fab_devid = TRUE; 6407 6408 /* Register the devid if it exists */ 6409 if (un->un_devid != NULL) { 6410 (void) ddi_devid_register(SD_DEVINFO(un), 6411 un->un_devid); 6412 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6413 "sd_register_devid: devid fabricated using " 6414 "ddi framework\n"); 6415 } 6416 } 6417 6418 /* clean up resources */ 6419 if (inq80 != NULL) { 6420 kmem_free(inq80, inq80_len); 6421 } 6422 if (inq83 != NULL) { 6423 kmem_free(inq83, inq83_len); 6424 } 6425 } 6426 6427 static daddr_t 6428 sd_get_devid_block(struct sd_lun *un) 6429 { 6430 daddr_t spc, blk, head, cyl; 6431 6432 if ((un->un_f_geometry_is_valid == FALSE) || 6433 (un->un_solaris_size < DK_LABEL_LOC)) 6434 return (-1); 6435 6436 if (un->un_vtoc.v_sanity != VTOC_SANE) { 6437 /* EFI labeled */ 6438 if (un->un_reserved != -1) { 6439 blk = un->un_map[un->un_reserved].dkl_cylno; 6440 } else { 6441 return (-1); 6442 } 6443 } else { 6444 /* SMI labeled */ 6445 /* this geometry doesn't allow us to write a devid */ 6446 if (un->un_g.dkg_acyl < 2) { 6447 return (-1); 6448 } 6449 6450 /* 6451 * Subtract 2 guarantees that the next to last cylinder 6452 * is used 6453 */ 6454 cyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl - 2; 6455 spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect; 6456 head = un->un_g.dkg_nhead - 1; 6457 blk = (cyl * (spc - un->un_g.dkg_apc)) + 6458 (head * un->un_g.dkg_nsect) + 1; 6459 } 6460 return (blk); 6461 } 6462 6463 /* 6464 * Function: sd_get_devid 6465 * 6466 * Description: This routine will return 0 if a valid device id has been 6467 * obtained from the target and stored in the soft state. If a 6468 * valid device id has not been previously read and stored, a 6469 * read attempt will be made. 6470 * 6471 * Arguments: un - driver soft state (unit) structure 6472 * 6473 * Return Code: 0 if we successfully get the device id 6474 * 6475 * Context: Kernel Thread 6476 */ 6477 6478 static int 6479 sd_get_devid(struct sd_lun *un) 6480 { 6481 struct dk_devid *dkdevid; 6482 ddi_devid_t tmpid; 6483 uint_t *ip; 6484 size_t sz; 6485 daddr_t blk; 6486 int status; 6487 int chksum; 6488 int i; 6489 size_t buffer_size; 6490 6491 ASSERT(un != NULL); 6492 ASSERT(mutex_owned(SD_MUTEX(un))); 6493 6494 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n", 6495 un); 6496 6497 if (un->un_devid != NULL) { 6498 return (0); 6499 } 6500 6501 blk = sd_get_devid_block(un); 6502 if (blk < 0) 6503 return (EINVAL); 6504 6505 /* 6506 * Read and verify device id, stored in the reserved cylinders at the 6507 * end of the disk. Backup label is on the odd sectors of the last 6508 * track of the last cylinder. Device id will be on track of the next 6509 * to last cylinder. 6510 */ 6511 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid)); 6512 mutex_exit(SD_MUTEX(un)); 6513 dkdevid = kmem_alloc(buffer_size, KM_SLEEP); 6514 status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk, 6515 SD_PATH_DIRECT); 6516 if (status != 0) { 6517 goto error; 6518 } 6519 6520 /* Validate the revision */ 6521 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 6522 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 6523 status = EINVAL; 6524 goto error; 6525 } 6526 6527 /* Calculate the checksum */ 6528 chksum = 0; 6529 ip = (uint_t *)dkdevid; 6530 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6531 i++) { 6532 chksum ^= ip[i]; 6533 } 6534 6535 /* Compare the checksums */ 6536 if (DKD_GETCHKSUM(dkdevid) != chksum) { 6537 status = EINVAL; 6538 goto error; 6539 } 6540 6541 /* Validate the device id */ 6542 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 6543 status = EINVAL; 6544 goto error; 6545 } 6546 6547 /* 6548 * Store the device id in the driver soft state 6549 */ 6550 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 6551 tmpid = kmem_alloc(sz, KM_SLEEP); 6552 6553 mutex_enter(SD_MUTEX(un)); 6554 6555 un->un_devid = tmpid; 6556 bcopy(&dkdevid->dkd_devid, un->un_devid, sz); 6557 6558 kmem_free(dkdevid, buffer_size); 6559 6560 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un); 6561 6562 return (status); 6563 error: 6564 mutex_enter(SD_MUTEX(un)); 6565 kmem_free(dkdevid, buffer_size); 6566 return (status); 6567 } 6568 6569 6570 /* 6571 * Function: sd_create_devid 6572 * 6573 * Description: This routine will fabricate the device id and write it 6574 * to the disk. 6575 * 6576 * Arguments: un - driver soft state (unit) structure 6577 * 6578 * Return Code: value of the fabricated device id 6579 * 6580 * Context: Kernel Thread 6581 */ 6582 6583 static ddi_devid_t 6584 sd_create_devid(struct sd_lun *un) 6585 { 6586 ASSERT(un != NULL); 6587 6588 /* Fabricate the devid */ 6589 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid) 6590 == DDI_FAILURE) { 6591 return (NULL); 6592 } 6593 6594 /* Write the devid to disk */ 6595 if (sd_write_deviceid(un) != 0) { 6596 ddi_devid_free(un->un_devid); 6597 un->un_devid = NULL; 6598 } 6599 6600 return (un->un_devid); 6601 } 6602 6603 6604 /* 6605 * Function: sd_write_deviceid 6606 * 6607 * Description: This routine will write the device id to the disk 6608 * reserved sector. 6609 * 6610 * Arguments: un - driver soft state (unit) structure 6611 * 6612 * Return Code: EINVAL 6613 * value returned by sd_send_scsi_cmd 6614 * 6615 * Context: Kernel Thread 6616 */ 6617 6618 static int 6619 sd_write_deviceid(struct sd_lun *un) 6620 { 6621 struct dk_devid *dkdevid; 6622 daddr_t blk; 6623 uint_t *ip, chksum; 6624 int status; 6625 int i; 6626 6627 ASSERT(mutex_owned(SD_MUTEX(un))); 6628 6629 blk = sd_get_devid_block(un); 6630 if (blk < 0) 6631 return (-1); 6632 mutex_exit(SD_MUTEX(un)); 6633 6634 /* Allocate the buffer */ 6635 dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP); 6636 6637 /* Fill in the revision */ 6638 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 6639 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 6640 6641 /* Copy in the device id */ 6642 mutex_enter(SD_MUTEX(un)); 6643 bcopy(un->un_devid, &dkdevid->dkd_devid, 6644 ddi_devid_sizeof(un->un_devid)); 6645 mutex_exit(SD_MUTEX(un)); 6646 6647 /* Calculate the checksum */ 6648 chksum = 0; 6649 ip = (uint_t *)dkdevid; 6650 for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int)); 6651 i++) { 6652 chksum ^= ip[i]; 6653 } 6654 6655 /* Fill-in checksum */ 6656 DKD_FORMCHKSUM(chksum, dkdevid); 6657 6658 /* Write the reserved sector */ 6659 status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk, 6660 SD_PATH_DIRECT); 6661 6662 kmem_free(dkdevid, un->un_sys_blocksize); 6663 6664 mutex_enter(SD_MUTEX(un)); 6665 return (status); 6666 } 6667 6668 6669 /* 6670 * Function: sd_check_vpd_page_support 6671 * 6672 * Description: This routine sends an inquiry command with the EVPD bit set and 6673 * a page code of 0x00 to the device. It is used to determine which 6674 * vital product pages are availible to find the devid. We are 6675 * looking for pages 0x83 or 0x80. If we return a negative 1, the 6676 * device does not support that command. 6677 * 6678 * Arguments: un - driver soft state (unit) structure 6679 * 6680 * Return Code: 0 - success 6681 * 1 - check condition 6682 * 6683 * Context: This routine can sleep. 6684 */ 6685 6686 static int 6687 sd_check_vpd_page_support(struct sd_lun *un) 6688 { 6689 uchar_t *page_list = NULL; 6690 uchar_t page_length = 0xff; /* Use max possible length */ 6691 uchar_t evpd = 0x01; /* Set the EVPD bit */ 6692 uchar_t page_code = 0x00; /* Supported VPD Pages */ 6693 int rval = 0; 6694 int counter; 6695 6696 ASSERT(un != NULL); 6697 ASSERT(mutex_owned(SD_MUTEX(un))); 6698 6699 mutex_exit(SD_MUTEX(un)); 6700 6701 /* 6702 * We'll set the page length to the maximum to save figuring it out 6703 * with an additional call. 6704 */ 6705 page_list = kmem_zalloc(page_length, KM_SLEEP); 6706 6707 rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd, 6708 page_code, NULL); 6709 6710 mutex_enter(SD_MUTEX(un)); 6711 6712 /* 6713 * Now we must validate that the device accepted the command, as some 6714 * drives do not support it. If the drive does support it, we will 6715 * return 0, and the supported pages will be in un_vpd_page_mask. If 6716 * not, we return -1. 6717 */ 6718 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) { 6719 /* Loop to find one of the 2 pages we need */ 6720 counter = 4; /* Supported pages start at byte 4, with 0x00 */ 6721 6722 /* 6723 * Pages are returned in ascending order, and 0x83 is what we 6724 * are hoping for. 6725 */ 6726 while ((page_list[counter] <= 0x83) && 6727 (counter <= (page_list[VPD_PAGE_LENGTH] + 6728 VPD_HEAD_OFFSET))) { 6729 /* 6730 * Add 3 because page_list[3] is the number of 6731 * pages minus 3 6732 */ 6733 6734 switch (page_list[counter]) { 6735 case 0x00: 6736 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG; 6737 break; 6738 case 0x80: 6739 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG; 6740 break; 6741 case 0x81: 6742 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG; 6743 break; 6744 case 0x82: 6745 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG; 6746 break; 6747 case 0x83: 6748 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG; 6749 break; 6750 } 6751 counter++; 6752 } 6753 6754 } else { 6755 rval = -1; 6756 6757 SD_INFO(SD_LOG_ATTACH_DETACH, un, 6758 "sd_check_vpd_page_support: This drive does not implement " 6759 "VPD pages.\n"); 6760 } 6761 6762 kmem_free(page_list, page_length); 6763 6764 return (rval); 6765 } 6766 6767 6768 /* 6769 * Function: sd_setup_pm 6770 * 6771 * Description: Initialize Power Management on the device 6772 * 6773 * Context: Kernel Thread 6774 */ 6775 6776 static void 6777 sd_setup_pm(struct sd_lun *un, dev_info_t *devi) 6778 { 6779 uint_t log_page_size; 6780 uchar_t *log_page_data; 6781 int rval; 6782 6783 /* 6784 * Since we are called from attach, holding a mutex for 6785 * un is unnecessary. Because some of the routines called 6786 * from here require SD_MUTEX to not be held, assert this 6787 * right up front. 6788 */ 6789 ASSERT(!mutex_owned(SD_MUTEX(un))); 6790 /* 6791 * Since the sd device does not have the 'reg' property, 6792 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries. 6793 * The following code is to tell cpr that this device 6794 * DOES need to be suspended and resumed. 6795 */ 6796 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 6797 "pm-hardware-state", "needs-suspend-resume"); 6798 6799 /* 6800 * This complies with the new power management framework 6801 * for certain desktop machines. Create the pm_components 6802 * property as a string array property. 6803 */ 6804 if (un->un_f_pm_supported) { 6805 /* 6806 * not all devices have a motor, try it first. 6807 * some devices may return ILLEGAL REQUEST, some 6808 * will hang 6809 * The following START_STOP_UNIT is used to check if target 6810 * device has a motor. 6811 */ 6812 un->un_f_start_stop_supported = TRUE; 6813 if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 6814 SD_PATH_DIRECT) != 0) { 6815 un->un_f_start_stop_supported = FALSE; 6816 } 6817 6818 /* 6819 * create pm properties anyways otherwise the parent can't 6820 * go to sleep 6821 */ 6822 (void) sd_create_pm_components(devi, un); 6823 un->un_f_pm_is_enabled = TRUE; 6824 return; 6825 } 6826 6827 if (!un->un_f_log_sense_supported) { 6828 un->un_power_level = SD_SPINDLE_ON; 6829 un->un_f_pm_is_enabled = FALSE; 6830 return; 6831 } 6832 6833 rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE); 6834 6835 #ifdef SDDEBUG 6836 if (sd_force_pm_supported) { 6837 /* Force a successful result */ 6838 rval = 1; 6839 } 6840 #endif 6841 6842 /* 6843 * If the start-stop cycle counter log page is not supported 6844 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0) 6845 * then we should not create the pm_components property. 6846 */ 6847 if (rval == -1) { 6848 /* 6849 * Error. 6850 * Reading log sense failed, most likely this is 6851 * an older drive that does not support log sense. 6852 * If this fails auto-pm is not supported. 6853 */ 6854 un->un_power_level = SD_SPINDLE_ON; 6855 un->un_f_pm_is_enabled = FALSE; 6856 6857 } else if (rval == 0) { 6858 /* 6859 * Page not found. 6860 * The start stop cycle counter is implemented as page 6861 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For 6862 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE). 6863 */ 6864 if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) { 6865 /* 6866 * Page found, use this one. 6867 */ 6868 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE; 6869 un->un_f_pm_is_enabled = TRUE; 6870 } else { 6871 /* 6872 * Error or page not found. 6873 * auto-pm is not supported for this device. 6874 */ 6875 un->un_power_level = SD_SPINDLE_ON; 6876 un->un_f_pm_is_enabled = FALSE; 6877 } 6878 } else { 6879 /* 6880 * Page found, use it. 6881 */ 6882 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE; 6883 un->un_f_pm_is_enabled = TRUE; 6884 } 6885 6886 6887 if (un->un_f_pm_is_enabled == TRUE) { 6888 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 6889 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 6890 6891 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 6892 log_page_size, un->un_start_stop_cycle_page, 6893 0x01, 0, SD_PATH_DIRECT); 6894 #ifdef SDDEBUG 6895 if (sd_force_pm_supported) { 6896 /* Force a successful result */ 6897 rval = 0; 6898 } 6899 #endif 6900 6901 /* 6902 * If the Log sense for Page( Start/stop cycle counter page) 6903 * succeeds, then power managment is supported and we can 6904 * enable auto-pm. 6905 */ 6906 if (rval == 0) { 6907 (void) sd_create_pm_components(devi, un); 6908 } else { 6909 un->un_power_level = SD_SPINDLE_ON; 6910 un->un_f_pm_is_enabled = FALSE; 6911 } 6912 6913 kmem_free(log_page_data, log_page_size); 6914 } 6915 } 6916 6917 6918 /* 6919 * Function: sd_create_pm_components 6920 * 6921 * Description: Initialize PM property. 6922 * 6923 * Context: Kernel thread context 6924 */ 6925 6926 static void 6927 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un) 6928 { 6929 char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL }; 6930 6931 ASSERT(!mutex_owned(SD_MUTEX(un))); 6932 6933 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi, 6934 "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) { 6935 /* 6936 * When components are initially created they are idle, 6937 * power up any non-removables. 6938 * Note: the return value of pm_raise_power can't be used 6939 * for determining if PM should be enabled for this device. 6940 * Even if you check the return values and remove this 6941 * property created above, the PM framework will not honor the 6942 * change after the first call to pm_raise_power. Hence, 6943 * removal of that property does not help if pm_raise_power 6944 * fails. In the case of removable media, the start/stop 6945 * will fail if the media is not present. 6946 */ 6947 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0, 6948 SD_SPINDLE_ON) == DDI_SUCCESS)) { 6949 mutex_enter(SD_MUTEX(un)); 6950 un->un_power_level = SD_SPINDLE_ON; 6951 mutex_enter(&un->un_pm_mutex); 6952 /* Set to on and not busy. */ 6953 un->un_pm_count = 0; 6954 } else { 6955 mutex_enter(SD_MUTEX(un)); 6956 un->un_power_level = SD_SPINDLE_OFF; 6957 mutex_enter(&un->un_pm_mutex); 6958 /* Set to off. */ 6959 un->un_pm_count = -1; 6960 } 6961 mutex_exit(&un->un_pm_mutex); 6962 mutex_exit(SD_MUTEX(un)); 6963 } else { 6964 un->un_power_level = SD_SPINDLE_ON; 6965 un->un_f_pm_is_enabled = FALSE; 6966 } 6967 } 6968 6969 6970 /* 6971 * Function: sd_ddi_suspend 6972 * 6973 * Description: Performs system power-down operations. This includes 6974 * setting the drive state to indicate its suspended so 6975 * that no new commands will be accepted. Also, wait for 6976 * all commands that are in transport or queued to a timer 6977 * for retry to complete. All timeout threads are cancelled. 6978 * 6979 * Return Code: DDI_FAILURE or DDI_SUCCESS 6980 * 6981 * Context: Kernel thread context 6982 */ 6983 6984 static int 6985 sd_ddi_suspend(dev_info_t *devi) 6986 { 6987 struct sd_lun *un; 6988 clock_t wait_cmds_complete; 6989 6990 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 6991 if (un == NULL) { 6992 return (DDI_FAILURE); 6993 } 6994 6995 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n"); 6996 6997 mutex_enter(SD_MUTEX(un)); 6998 6999 /* Return success if the device is already suspended. */ 7000 if (un->un_state == SD_STATE_SUSPENDED) { 7001 mutex_exit(SD_MUTEX(un)); 7002 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7003 "device already suspended, exiting\n"); 7004 return (DDI_SUCCESS); 7005 } 7006 7007 /* Return failure if the device is being used by HA */ 7008 if (un->un_resvd_status & 7009 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) { 7010 mutex_exit(SD_MUTEX(un)); 7011 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7012 "device in use by HA, exiting\n"); 7013 return (DDI_FAILURE); 7014 } 7015 7016 /* 7017 * Return failure if the device is in a resource wait 7018 * or power changing state. 7019 */ 7020 if ((un->un_state == SD_STATE_RWAIT) || 7021 (un->un_state == SD_STATE_PM_CHANGING)) { 7022 mutex_exit(SD_MUTEX(un)); 7023 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: " 7024 "device in resource wait state, exiting\n"); 7025 return (DDI_FAILURE); 7026 } 7027 7028 7029 un->un_save_state = un->un_last_state; 7030 New_state(un, SD_STATE_SUSPENDED); 7031 7032 /* 7033 * Wait for all commands that are in transport or queued to a timer 7034 * for retry to complete. 7035 * 7036 * While waiting, no new commands will be accepted or sent because of 7037 * the new state we set above. 7038 * 7039 * Wait till current operation has completed. If we are in the resource 7040 * wait state (with an intr outstanding) then we need to wait till the 7041 * intr completes and starts the next cmd. We want to wait for 7042 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND. 7043 */ 7044 wait_cmds_complete = ddi_get_lbolt() + 7045 (sd_wait_cmds_complete * drv_usectohz(1000000)); 7046 7047 while (un->un_ncmds_in_transport != 0) { 7048 /* 7049 * Fail if commands do not finish in the specified time. 7050 */ 7051 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un), 7052 wait_cmds_complete) == -1) { 7053 /* 7054 * Undo the state changes made above. Everything 7055 * must go back to it's original value. 7056 */ 7057 Restore_state(un); 7058 un->un_last_state = un->un_save_state; 7059 /* Wake up any threads that might be waiting. */ 7060 cv_broadcast(&un->un_suspend_cv); 7061 mutex_exit(SD_MUTEX(un)); 7062 SD_ERROR(SD_LOG_IO_PM, un, 7063 "sd_ddi_suspend: failed due to outstanding cmds\n"); 7064 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n"); 7065 return (DDI_FAILURE); 7066 } 7067 } 7068 7069 /* 7070 * Cancel SCSI watch thread and timeouts, if any are active 7071 */ 7072 7073 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) { 7074 opaque_t temp_token = un->un_swr_token; 7075 mutex_exit(SD_MUTEX(un)); 7076 scsi_watch_suspend(temp_token); 7077 mutex_enter(SD_MUTEX(un)); 7078 } 7079 7080 if (un->un_reset_throttle_timeid != NULL) { 7081 timeout_id_t temp_id = un->un_reset_throttle_timeid; 7082 un->un_reset_throttle_timeid = NULL; 7083 mutex_exit(SD_MUTEX(un)); 7084 (void) untimeout(temp_id); 7085 mutex_enter(SD_MUTEX(un)); 7086 } 7087 7088 if (un->un_dcvb_timeid != NULL) { 7089 timeout_id_t temp_id = un->un_dcvb_timeid; 7090 un->un_dcvb_timeid = NULL; 7091 mutex_exit(SD_MUTEX(un)); 7092 (void) untimeout(temp_id); 7093 mutex_enter(SD_MUTEX(un)); 7094 } 7095 7096 mutex_enter(&un->un_pm_mutex); 7097 if (un->un_pm_timeid != NULL) { 7098 timeout_id_t temp_id = un->un_pm_timeid; 7099 un->un_pm_timeid = NULL; 7100 mutex_exit(&un->un_pm_mutex); 7101 mutex_exit(SD_MUTEX(un)); 7102 (void) untimeout(temp_id); 7103 mutex_enter(SD_MUTEX(un)); 7104 } else { 7105 mutex_exit(&un->un_pm_mutex); 7106 } 7107 7108 if (un->un_retry_timeid != NULL) { 7109 timeout_id_t temp_id = un->un_retry_timeid; 7110 un->un_retry_timeid = NULL; 7111 mutex_exit(SD_MUTEX(un)); 7112 (void) untimeout(temp_id); 7113 mutex_enter(SD_MUTEX(un)); 7114 } 7115 7116 if (un->un_direct_priority_timeid != NULL) { 7117 timeout_id_t temp_id = un->un_direct_priority_timeid; 7118 un->un_direct_priority_timeid = NULL; 7119 mutex_exit(SD_MUTEX(un)); 7120 (void) untimeout(temp_id); 7121 mutex_enter(SD_MUTEX(un)); 7122 } 7123 7124 if (un->un_f_is_fibre == TRUE) { 7125 /* 7126 * Remove callbacks for insert and remove events 7127 */ 7128 if (un->un_insert_event != NULL) { 7129 mutex_exit(SD_MUTEX(un)); 7130 (void) ddi_remove_event_handler(un->un_insert_cb_id); 7131 mutex_enter(SD_MUTEX(un)); 7132 un->un_insert_event = NULL; 7133 } 7134 7135 if (un->un_remove_event != NULL) { 7136 mutex_exit(SD_MUTEX(un)); 7137 (void) ddi_remove_event_handler(un->un_remove_cb_id); 7138 mutex_enter(SD_MUTEX(un)); 7139 un->un_remove_event = NULL; 7140 } 7141 } 7142 7143 mutex_exit(SD_MUTEX(un)); 7144 7145 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n"); 7146 7147 return (DDI_SUCCESS); 7148 } 7149 7150 7151 /* 7152 * Function: sd_ddi_pm_suspend 7153 * 7154 * Description: Set the drive state to low power. 7155 * Someone else is required to actually change the drive 7156 * power level. 7157 * 7158 * Arguments: un - driver soft state (unit) structure 7159 * 7160 * Return Code: DDI_FAILURE or DDI_SUCCESS 7161 * 7162 * Context: Kernel thread context 7163 */ 7164 7165 static int 7166 sd_ddi_pm_suspend(struct sd_lun *un) 7167 { 7168 ASSERT(un != NULL); 7169 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n"); 7170 7171 ASSERT(!mutex_owned(SD_MUTEX(un))); 7172 mutex_enter(SD_MUTEX(un)); 7173 7174 /* 7175 * Exit if power management is not enabled for this device, or if 7176 * the device is being used by HA. 7177 */ 7178 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status & 7179 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) { 7180 mutex_exit(SD_MUTEX(un)); 7181 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n"); 7182 return (DDI_SUCCESS); 7183 } 7184 7185 SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n", 7186 un->un_ncmds_in_driver); 7187 7188 /* 7189 * See if the device is not busy, ie.: 7190 * - we have no commands in the driver for this device 7191 * - not waiting for resources 7192 */ 7193 if ((un->un_ncmds_in_driver == 0) && 7194 (un->un_state != SD_STATE_RWAIT)) { 7195 /* 7196 * The device is not busy, so it is OK to go to low power state. 7197 * Indicate low power, but rely on someone else to actually 7198 * change it. 7199 */ 7200 mutex_enter(&un->un_pm_mutex); 7201 un->un_pm_count = -1; 7202 mutex_exit(&un->un_pm_mutex); 7203 un->un_power_level = SD_SPINDLE_OFF; 7204 } 7205 7206 mutex_exit(SD_MUTEX(un)); 7207 7208 SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n"); 7209 7210 return (DDI_SUCCESS); 7211 } 7212 7213 7214 /* 7215 * Function: sd_ddi_resume 7216 * 7217 * Description: Performs system power-up operations.. 7218 * 7219 * Return Code: DDI_SUCCESS 7220 * DDI_FAILURE 7221 * 7222 * Context: Kernel thread context 7223 */ 7224 7225 static int 7226 sd_ddi_resume(dev_info_t *devi) 7227 { 7228 struct sd_lun *un; 7229 7230 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi)); 7231 if (un == NULL) { 7232 return (DDI_FAILURE); 7233 } 7234 7235 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n"); 7236 7237 mutex_enter(SD_MUTEX(un)); 7238 Restore_state(un); 7239 7240 /* 7241 * Restore the state which was saved to give the 7242 * the right state in un_last_state 7243 */ 7244 un->un_last_state = un->un_save_state; 7245 /* 7246 * Note: throttle comes back at full. 7247 * Also note: this MUST be done before calling pm_raise_power 7248 * otherwise the system can get hung in biowait. The scenario where 7249 * this'll happen is under cpr suspend. Writing of the system 7250 * state goes through sddump, which writes 0 to un_throttle. If 7251 * writing the system state then fails, example if the partition is 7252 * too small, then cpr attempts a resume. If throttle isn't restored 7253 * from the saved value until after calling pm_raise_power then 7254 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs 7255 * in biowait. 7256 */ 7257 un->un_throttle = un->un_saved_throttle; 7258 7259 /* 7260 * The chance of failure is very rare as the only command done in power 7261 * entry point is START command when you transition from 0->1 or 7262 * unknown->1. Put it to SPINDLE ON state irrespective of the state at 7263 * which suspend was done. Ignore the return value as the resume should 7264 * not be failed. In the case of removable media the media need not be 7265 * inserted and hence there is a chance that raise power will fail with 7266 * media not present. 7267 */ 7268 if (un->un_f_attach_spinup) { 7269 mutex_exit(SD_MUTEX(un)); 7270 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 7271 mutex_enter(SD_MUTEX(un)); 7272 } 7273 7274 /* 7275 * Don't broadcast to the suspend cv and therefore possibly 7276 * start I/O until after power has been restored. 7277 */ 7278 cv_broadcast(&un->un_suspend_cv); 7279 cv_broadcast(&un->un_state_cv); 7280 7281 /* restart thread */ 7282 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) { 7283 scsi_watch_resume(un->un_swr_token); 7284 } 7285 7286 #if (defined(__fibre)) 7287 if (un->un_f_is_fibre == TRUE) { 7288 /* 7289 * Add callbacks for insert and remove events 7290 */ 7291 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 7292 sd_init_event_callbacks(un); 7293 } 7294 } 7295 #endif 7296 7297 /* 7298 * Transport any pending commands to the target. 7299 * 7300 * If this is a low-activity device commands in queue will have to wait 7301 * until new commands come in, which may take awhile. Also, we 7302 * specifically don't check un_ncmds_in_transport because we know that 7303 * there really are no commands in progress after the unit was 7304 * suspended and we could have reached the throttle level, been 7305 * suspended, and have no new commands coming in for awhile. Highly 7306 * unlikely, but so is the low-activity disk scenario. 7307 */ 7308 ddi_xbuf_dispatch(un->un_xbuf_attr); 7309 7310 sd_start_cmds(un, NULL); 7311 mutex_exit(SD_MUTEX(un)); 7312 7313 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n"); 7314 7315 return (DDI_SUCCESS); 7316 } 7317 7318 7319 /* 7320 * Function: sd_ddi_pm_resume 7321 * 7322 * Description: Set the drive state to powered on. 7323 * Someone else is required to actually change the drive 7324 * power level. 7325 * 7326 * Arguments: un - driver soft state (unit) structure 7327 * 7328 * Return Code: DDI_SUCCESS 7329 * 7330 * Context: Kernel thread context 7331 */ 7332 7333 static int 7334 sd_ddi_pm_resume(struct sd_lun *un) 7335 { 7336 ASSERT(un != NULL); 7337 7338 ASSERT(!mutex_owned(SD_MUTEX(un))); 7339 mutex_enter(SD_MUTEX(un)); 7340 un->un_power_level = SD_SPINDLE_ON; 7341 7342 ASSERT(!mutex_owned(&un->un_pm_mutex)); 7343 mutex_enter(&un->un_pm_mutex); 7344 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 7345 un->un_pm_count++; 7346 ASSERT(un->un_pm_count == 0); 7347 /* 7348 * Note: no longer do the cv_broadcast on un_suspend_cv. The 7349 * un_suspend_cv is for a system resume, not a power management 7350 * device resume. (4297749) 7351 * cv_broadcast(&un->un_suspend_cv); 7352 */ 7353 } 7354 mutex_exit(&un->un_pm_mutex); 7355 mutex_exit(SD_MUTEX(un)); 7356 7357 return (DDI_SUCCESS); 7358 } 7359 7360 7361 /* 7362 * Function: sd_pm_idletimeout_handler 7363 * 7364 * Description: A timer routine that's active only while a device is busy. 7365 * The purpose is to extend slightly the pm framework's busy 7366 * view of the device to prevent busy/idle thrashing for 7367 * back-to-back commands. Do this by comparing the current time 7368 * to the time at which the last command completed and when the 7369 * difference is greater than sd_pm_idletime, call 7370 * pm_idle_component. In addition to indicating idle to the pm 7371 * framework, update the chain type to again use the internal pm 7372 * layers of the driver. 7373 * 7374 * Arguments: arg - driver soft state (unit) structure 7375 * 7376 * Context: Executes in a timeout(9F) thread context 7377 */ 7378 7379 static void 7380 sd_pm_idletimeout_handler(void *arg) 7381 { 7382 struct sd_lun *un = arg; 7383 7384 time_t now; 7385 7386 mutex_enter(&sd_detach_mutex); 7387 if (un->un_detach_count != 0) { 7388 /* Abort if the instance is detaching */ 7389 mutex_exit(&sd_detach_mutex); 7390 return; 7391 } 7392 mutex_exit(&sd_detach_mutex); 7393 7394 now = ddi_get_time(); 7395 /* 7396 * Grab both mutexes, in the proper order, since we're accessing 7397 * both PM and softstate variables. 7398 */ 7399 mutex_enter(SD_MUTEX(un)); 7400 mutex_enter(&un->un_pm_mutex); 7401 if (((now - un->un_pm_idle_time) > sd_pm_idletime) && 7402 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) { 7403 /* 7404 * Update the chain types. 7405 * This takes affect on the next new command received. 7406 */ 7407 if (un->un_f_non_devbsize_supported) { 7408 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 7409 } else { 7410 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 7411 } 7412 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 7413 7414 SD_TRACE(SD_LOG_IO_PM, un, 7415 "sd_pm_idletimeout_handler: idling device\n"); 7416 (void) pm_idle_component(SD_DEVINFO(un), 0); 7417 un->un_pm_idle_timeid = NULL; 7418 } else { 7419 un->un_pm_idle_timeid = 7420 timeout(sd_pm_idletimeout_handler, un, 7421 (drv_usectohz((clock_t)300000))); /* 300 ms. */ 7422 } 7423 mutex_exit(&un->un_pm_mutex); 7424 mutex_exit(SD_MUTEX(un)); 7425 } 7426 7427 7428 /* 7429 * Function: sd_pm_timeout_handler 7430 * 7431 * Description: Callback to tell framework we are idle. 7432 * 7433 * Context: timeout(9f) thread context. 7434 */ 7435 7436 static void 7437 sd_pm_timeout_handler(void *arg) 7438 { 7439 struct sd_lun *un = arg; 7440 7441 (void) pm_idle_component(SD_DEVINFO(un), 0); 7442 mutex_enter(&un->un_pm_mutex); 7443 un->un_pm_timeid = NULL; 7444 mutex_exit(&un->un_pm_mutex); 7445 } 7446 7447 7448 /* 7449 * Function: sdpower 7450 * 7451 * Description: PM entry point. 7452 * 7453 * Return Code: DDI_SUCCESS 7454 * DDI_FAILURE 7455 * 7456 * Context: Kernel thread context 7457 */ 7458 7459 static int 7460 sdpower(dev_info_t *devi, int component, int level) 7461 { 7462 struct sd_lun *un; 7463 int instance; 7464 int rval = DDI_SUCCESS; 7465 uint_t i, log_page_size, maxcycles, ncycles; 7466 uchar_t *log_page_data; 7467 int log_sense_page; 7468 int medium_present; 7469 time_t intvlp; 7470 dev_t dev; 7471 struct pm_trans_data sd_pm_tran_data; 7472 uchar_t save_state; 7473 int sval; 7474 uchar_t state_before_pm; 7475 int got_semaphore_here; 7476 7477 instance = ddi_get_instance(devi); 7478 7479 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 7480 (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) || 7481 component != 0) { 7482 return (DDI_FAILURE); 7483 } 7484 7485 dev = sd_make_device(SD_DEVINFO(un)); 7486 7487 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level); 7488 7489 /* 7490 * Must synchronize power down with close. 7491 * Attempt to decrement/acquire the open/close semaphore, 7492 * but do NOT wait on it. If it's not greater than zero, 7493 * ie. it can't be decremented without waiting, then 7494 * someone else, either open or close, already has it 7495 * and the try returns 0. Use that knowledge here to determine 7496 * if it's OK to change the device power level. 7497 * Also, only increment it on exit if it was decremented, ie. gotten, 7498 * here. 7499 */ 7500 got_semaphore_here = sema_tryp(&un->un_semoclose); 7501 7502 mutex_enter(SD_MUTEX(un)); 7503 7504 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n", 7505 un->un_ncmds_in_driver); 7506 7507 /* 7508 * If un_ncmds_in_driver is non-zero it indicates commands are 7509 * already being processed in the driver, or if the semaphore was 7510 * not gotten here it indicates an open or close is being processed. 7511 * At the same time somebody is requesting to go low power which 7512 * can't happen, therefore we need to return failure. 7513 */ 7514 if ((level == SD_SPINDLE_OFF) && 7515 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) { 7516 mutex_exit(SD_MUTEX(un)); 7517 7518 if (got_semaphore_here != 0) { 7519 sema_v(&un->un_semoclose); 7520 } 7521 SD_TRACE(SD_LOG_IO_PM, un, 7522 "sdpower: exit, device has queued cmds.\n"); 7523 return (DDI_FAILURE); 7524 } 7525 7526 /* 7527 * if it is OFFLINE that means the disk is completely dead 7528 * in our case we have to put the disk in on or off by sending commands 7529 * Of course that will fail anyway so return back here. 7530 * 7531 * Power changes to a device that's OFFLINE or SUSPENDED 7532 * are not allowed. 7533 */ 7534 if ((un->un_state == SD_STATE_OFFLINE) || 7535 (un->un_state == SD_STATE_SUSPENDED)) { 7536 mutex_exit(SD_MUTEX(un)); 7537 7538 if (got_semaphore_here != 0) { 7539 sema_v(&un->un_semoclose); 7540 } 7541 SD_TRACE(SD_LOG_IO_PM, un, 7542 "sdpower: exit, device is off-line.\n"); 7543 return (DDI_FAILURE); 7544 } 7545 7546 /* 7547 * Change the device's state to indicate it's power level 7548 * is being changed. Do this to prevent a power off in the 7549 * middle of commands, which is especially bad on devices 7550 * that are really powered off instead of just spun down. 7551 */ 7552 state_before_pm = un->un_state; 7553 un->un_state = SD_STATE_PM_CHANGING; 7554 7555 mutex_exit(SD_MUTEX(un)); 7556 7557 /* 7558 * If "pm-capable" property is set to TRUE by HBA drivers, 7559 * bypass the following checking, otherwise, check the log 7560 * sense information for this device 7561 */ 7562 if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) { 7563 /* 7564 * Get the log sense information to understand whether the 7565 * the powercycle counts have gone beyond the threshhold. 7566 */ 7567 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE; 7568 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP); 7569 7570 mutex_enter(SD_MUTEX(un)); 7571 log_sense_page = un->un_start_stop_cycle_page; 7572 mutex_exit(SD_MUTEX(un)); 7573 7574 rval = sd_send_scsi_LOG_SENSE(un, log_page_data, 7575 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT); 7576 #ifdef SDDEBUG 7577 if (sd_force_pm_supported) { 7578 /* Force a successful result */ 7579 rval = 0; 7580 } 7581 #endif 7582 if (rval != 0) { 7583 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 7584 "Log Sense Failed\n"); 7585 kmem_free(log_page_data, log_page_size); 7586 /* Cannot support power management on those drives */ 7587 7588 if (got_semaphore_here != 0) { 7589 sema_v(&un->un_semoclose); 7590 } 7591 /* 7592 * On exit put the state back to it's original value 7593 * and broadcast to anyone waiting for the power 7594 * change completion. 7595 */ 7596 mutex_enter(SD_MUTEX(un)); 7597 un->un_state = state_before_pm; 7598 cv_broadcast(&un->un_suspend_cv); 7599 mutex_exit(SD_MUTEX(un)); 7600 SD_TRACE(SD_LOG_IO_PM, un, 7601 "sdpower: exit, Log Sense Failed.\n"); 7602 return (DDI_FAILURE); 7603 } 7604 7605 /* 7606 * From the page data - Convert the essential information to 7607 * pm_trans_data 7608 */ 7609 maxcycles = 7610 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) | 7611 (log_page_data[0x1E] << 8) | log_page_data[0x1F]; 7612 7613 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles; 7614 7615 ncycles = 7616 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) | 7617 (log_page_data[0x26] << 8) | log_page_data[0x27]; 7618 7619 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles; 7620 7621 for (i = 0; i < DC_SCSI_MFR_LEN; i++) { 7622 sd_pm_tran_data.un.scsi_cycles.svc_date[i] = 7623 log_page_data[8+i]; 7624 } 7625 7626 kmem_free(log_page_data, log_page_size); 7627 7628 /* 7629 * Call pm_trans_check routine to get the Ok from 7630 * the global policy 7631 */ 7632 7633 sd_pm_tran_data.format = DC_SCSI_FORMAT; 7634 sd_pm_tran_data.un.scsi_cycles.flag = 0; 7635 7636 rval = pm_trans_check(&sd_pm_tran_data, &intvlp); 7637 #ifdef SDDEBUG 7638 if (sd_force_pm_supported) { 7639 /* Force a successful result */ 7640 rval = 1; 7641 } 7642 #endif 7643 switch (rval) { 7644 case 0: 7645 /* 7646 * Not Ok to Power cycle or error in parameters passed 7647 * Would have given the advised time to consider power 7648 * cycle. Based on the new intvlp parameter we are 7649 * supposed to pretend we are busy so that pm framework 7650 * will never call our power entry point. Because of 7651 * that install a timeout handler and wait for the 7652 * recommended time to elapse so that power management 7653 * can be effective again. 7654 * 7655 * To effect this behavior, call pm_busy_component to 7656 * indicate to the framework this device is busy. 7657 * By not adjusting un_pm_count the rest of PM in 7658 * the driver will function normally, and independant 7659 * of this but because the framework is told the device 7660 * is busy it won't attempt powering down until it gets 7661 * a matching idle. The timeout handler sends this. 7662 * Note: sd_pm_entry can't be called here to do this 7663 * because sdpower may have been called as a result 7664 * of a call to pm_raise_power from within sd_pm_entry. 7665 * 7666 * If a timeout handler is already active then 7667 * don't install another. 7668 */ 7669 mutex_enter(&un->un_pm_mutex); 7670 if (un->un_pm_timeid == NULL) { 7671 un->un_pm_timeid = 7672 timeout(sd_pm_timeout_handler, 7673 un, intvlp * drv_usectohz(1000000)); 7674 mutex_exit(&un->un_pm_mutex); 7675 (void) pm_busy_component(SD_DEVINFO(un), 0); 7676 } else { 7677 mutex_exit(&un->un_pm_mutex); 7678 } 7679 if (got_semaphore_here != 0) { 7680 sema_v(&un->un_semoclose); 7681 } 7682 /* 7683 * On exit put the state back to it's original value 7684 * and broadcast to anyone waiting for the power 7685 * change completion. 7686 */ 7687 mutex_enter(SD_MUTEX(un)); 7688 un->un_state = state_before_pm; 7689 cv_broadcast(&un->un_suspend_cv); 7690 mutex_exit(SD_MUTEX(un)); 7691 7692 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, " 7693 "trans check Failed, not ok to power cycle.\n"); 7694 return (DDI_FAILURE); 7695 7696 case -1: 7697 if (got_semaphore_here != 0) { 7698 sema_v(&un->un_semoclose); 7699 } 7700 /* 7701 * On exit put the state back to it's original value 7702 * and broadcast to anyone waiting for the power 7703 * change completion. 7704 */ 7705 mutex_enter(SD_MUTEX(un)); 7706 un->un_state = state_before_pm; 7707 cv_broadcast(&un->un_suspend_cv); 7708 mutex_exit(SD_MUTEX(un)); 7709 SD_TRACE(SD_LOG_IO_PM, un, 7710 "sdpower: exit, trans check command Failed.\n"); 7711 return (DDI_FAILURE); 7712 } 7713 } 7714 7715 if (level == SD_SPINDLE_OFF) { 7716 /* 7717 * Save the last state... if the STOP FAILS we need it 7718 * for restoring 7719 */ 7720 mutex_enter(SD_MUTEX(un)); 7721 save_state = un->un_last_state; 7722 /* 7723 * There must not be any cmds. getting processed 7724 * in the driver when we get here. Power to the 7725 * device is potentially going off. 7726 */ 7727 ASSERT(un->un_ncmds_in_driver == 0); 7728 mutex_exit(SD_MUTEX(un)); 7729 7730 /* 7731 * For now suspend the device completely before spindle is 7732 * turned off 7733 */ 7734 if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) { 7735 if (got_semaphore_here != 0) { 7736 sema_v(&un->un_semoclose); 7737 } 7738 /* 7739 * On exit put the state back to it's original value 7740 * and broadcast to anyone waiting for the power 7741 * change completion. 7742 */ 7743 mutex_enter(SD_MUTEX(un)); 7744 un->un_state = state_before_pm; 7745 cv_broadcast(&un->un_suspend_cv); 7746 mutex_exit(SD_MUTEX(un)); 7747 SD_TRACE(SD_LOG_IO_PM, un, 7748 "sdpower: exit, PM suspend Failed.\n"); 7749 return (DDI_FAILURE); 7750 } 7751 } 7752 7753 /* 7754 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open, 7755 * close, or strategy. Dump no long uses this routine, it uses it's 7756 * own code so it can be done in polled mode. 7757 */ 7758 7759 medium_present = TRUE; 7760 7761 /* 7762 * When powering up, issue a TUR in case the device is at unit 7763 * attention. Don't do retries. Bypass the PM layer, otherwise 7764 * a deadlock on un_pm_busy_cv will occur. 7765 */ 7766 if (level == SD_SPINDLE_ON) { 7767 (void) sd_send_scsi_TEST_UNIT_READY(un, 7768 SD_DONT_RETRY_TUR | SD_BYPASS_PM); 7769 } 7770 7771 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n", 7772 ((level == SD_SPINDLE_ON) ? "START" : "STOP")); 7773 7774 sval = sd_send_scsi_START_STOP_UNIT(un, 7775 ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP), 7776 SD_PATH_DIRECT); 7777 /* Command failed, check for media present. */ 7778 if ((sval == ENXIO) && un->un_f_has_removable_media) { 7779 medium_present = FALSE; 7780 } 7781 7782 /* 7783 * The conditions of interest here are: 7784 * if a spindle off with media present fails, 7785 * then restore the state and return an error. 7786 * else if a spindle on fails, 7787 * then return an error (there's no state to restore). 7788 * In all other cases we setup for the new state 7789 * and return success. 7790 */ 7791 switch (level) { 7792 case SD_SPINDLE_OFF: 7793 if ((medium_present == TRUE) && (sval != 0)) { 7794 /* The stop command from above failed */ 7795 rval = DDI_FAILURE; 7796 /* 7797 * The stop command failed, and we have media 7798 * present. Put the level back by calling the 7799 * sd_pm_resume() and set the state back to 7800 * it's previous value. 7801 */ 7802 (void) sd_ddi_pm_resume(un); 7803 mutex_enter(SD_MUTEX(un)); 7804 un->un_last_state = save_state; 7805 mutex_exit(SD_MUTEX(un)); 7806 break; 7807 } 7808 /* 7809 * The stop command from above succeeded. 7810 */ 7811 if (un->un_f_monitor_media_state) { 7812 /* 7813 * Terminate watch thread in case of removable media 7814 * devices going into low power state. This is as per 7815 * the requirements of pm framework, otherwise commands 7816 * will be generated for the device (through watch 7817 * thread), even when the device is in low power state. 7818 */ 7819 mutex_enter(SD_MUTEX(un)); 7820 un->un_f_watcht_stopped = FALSE; 7821 if (un->un_swr_token != NULL) { 7822 opaque_t temp_token = un->un_swr_token; 7823 un->un_f_watcht_stopped = TRUE; 7824 un->un_swr_token = NULL; 7825 mutex_exit(SD_MUTEX(un)); 7826 (void) scsi_watch_request_terminate(temp_token, 7827 SCSI_WATCH_TERMINATE_WAIT); 7828 } else { 7829 mutex_exit(SD_MUTEX(un)); 7830 } 7831 } 7832 break; 7833 7834 default: /* The level requested is spindle on... */ 7835 /* 7836 * Legacy behavior: return success on a failed spinup 7837 * if there is no media in the drive. 7838 * Do this by looking at medium_present here. 7839 */ 7840 if ((sval != 0) && medium_present) { 7841 /* The start command from above failed */ 7842 rval = DDI_FAILURE; 7843 break; 7844 } 7845 /* 7846 * The start command from above succeeded 7847 * Resume the devices now that we have 7848 * started the disks 7849 */ 7850 (void) sd_ddi_pm_resume(un); 7851 7852 /* 7853 * Resume the watch thread since it was suspended 7854 * when the device went into low power mode. 7855 */ 7856 if (un->un_f_monitor_media_state) { 7857 mutex_enter(SD_MUTEX(un)); 7858 if (un->un_f_watcht_stopped == TRUE) { 7859 opaque_t temp_token; 7860 7861 un->un_f_watcht_stopped = FALSE; 7862 mutex_exit(SD_MUTEX(un)); 7863 temp_token = scsi_watch_request_submit( 7864 SD_SCSI_DEVP(un), 7865 sd_check_media_time, 7866 SENSE_LENGTH, sd_media_watch_cb, 7867 (caddr_t)dev); 7868 mutex_enter(SD_MUTEX(un)); 7869 un->un_swr_token = temp_token; 7870 } 7871 mutex_exit(SD_MUTEX(un)); 7872 } 7873 } 7874 if (got_semaphore_here != 0) { 7875 sema_v(&un->un_semoclose); 7876 } 7877 /* 7878 * On exit put the state back to it's original value 7879 * and broadcast to anyone waiting for the power 7880 * change completion. 7881 */ 7882 mutex_enter(SD_MUTEX(un)); 7883 un->un_state = state_before_pm; 7884 cv_broadcast(&un->un_suspend_cv); 7885 mutex_exit(SD_MUTEX(un)); 7886 7887 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval); 7888 7889 return (rval); 7890 } 7891 7892 7893 7894 /* 7895 * Function: sdattach 7896 * 7897 * Description: Driver's attach(9e) entry point function. 7898 * 7899 * Arguments: devi - opaque device info handle 7900 * cmd - attach type 7901 * 7902 * Return Code: DDI_SUCCESS 7903 * DDI_FAILURE 7904 * 7905 * Context: Kernel thread context 7906 */ 7907 7908 static int 7909 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd) 7910 { 7911 switch (cmd) { 7912 case DDI_ATTACH: 7913 return (sd_unit_attach(devi)); 7914 case DDI_RESUME: 7915 return (sd_ddi_resume(devi)); 7916 default: 7917 break; 7918 } 7919 return (DDI_FAILURE); 7920 } 7921 7922 7923 /* 7924 * Function: sddetach 7925 * 7926 * Description: Driver's detach(9E) entry point function. 7927 * 7928 * Arguments: devi - opaque device info handle 7929 * cmd - detach type 7930 * 7931 * Return Code: DDI_SUCCESS 7932 * DDI_FAILURE 7933 * 7934 * Context: Kernel thread context 7935 */ 7936 7937 static int 7938 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7939 { 7940 switch (cmd) { 7941 case DDI_DETACH: 7942 return (sd_unit_detach(devi)); 7943 case DDI_SUSPEND: 7944 return (sd_ddi_suspend(devi)); 7945 default: 7946 break; 7947 } 7948 return (DDI_FAILURE); 7949 } 7950 7951 7952 /* 7953 * Function: sd_sync_with_callback 7954 * 7955 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft 7956 * state while the callback routine is active. 7957 * 7958 * Arguments: un: softstate structure for the instance 7959 * 7960 * Context: Kernel thread context 7961 */ 7962 7963 static void 7964 sd_sync_with_callback(struct sd_lun *un) 7965 { 7966 ASSERT(un != NULL); 7967 7968 mutex_enter(SD_MUTEX(un)); 7969 7970 ASSERT(un->un_in_callback >= 0); 7971 7972 while (un->un_in_callback > 0) { 7973 mutex_exit(SD_MUTEX(un)); 7974 delay(2); 7975 mutex_enter(SD_MUTEX(un)); 7976 } 7977 7978 mutex_exit(SD_MUTEX(un)); 7979 } 7980 7981 /* 7982 * Function: sd_unit_attach 7983 * 7984 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates 7985 * the soft state structure for the device and performs 7986 * all necessary structure and device initializations. 7987 * 7988 * Arguments: devi: the system's dev_info_t for the device. 7989 * 7990 * Return Code: DDI_SUCCESS if attach is successful. 7991 * DDI_FAILURE if any part of the attach fails. 7992 * 7993 * Context: Called at attach(9e) time for the DDI_ATTACH flag. 7994 * Kernel thread context only. Can sleep. 7995 */ 7996 7997 static int 7998 sd_unit_attach(dev_info_t *devi) 7999 { 8000 struct scsi_device *devp; 8001 struct sd_lun *un; 8002 char *variantp; 8003 int reservation_flag = SD_TARGET_IS_UNRESERVED; 8004 int instance; 8005 int rval; 8006 int wc_enabled; 8007 int tgt; 8008 uint64_t capacity; 8009 uint_t lbasize; 8010 dev_info_t *pdip = ddi_get_parent(devi); 8011 8012 /* 8013 * Retrieve the target driver's private data area. This was set 8014 * up by the HBA. 8015 */ 8016 devp = ddi_get_driver_private(devi); 8017 8018 /* 8019 * Retrieve the target ID of the device. 8020 */ 8021 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8022 SCSI_ADDR_PROP_TARGET, -1); 8023 8024 /* 8025 * Since we have no idea what state things were left in by the last 8026 * user of the device, set up some 'default' settings, ie. turn 'em 8027 * off. The scsi_ifsetcap calls force re-negotiations with the drive. 8028 * Do this before the scsi_probe, which sends an inquiry. 8029 * This is a fix for bug (4430280). 8030 * Of special importance is wide-xfer. The drive could have been left 8031 * in wide transfer mode by the last driver to communicate with it, 8032 * this includes us. If that's the case, and if the following is not 8033 * setup properly or we don't re-negotiate with the drive prior to 8034 * transferring data to/from the drive, it causes bus parity errors, 8035 * data overruns, and unexpected interrupts. This first occurred when 8036 * the fix for bug (4378686) was made. 8037 */ 8038 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1); 8039 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1); 8040 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1); 8041 8042 /* 8043 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs 8044 * on a target. Setting it per lun instance actually sets the 8045 * capability of this target, which affects those luns already 8046 * attached on the same target. So during attach, we can only disable 8047 * this capability only when no other lun has been attached on this 8048 * target. By doing this, we assume a target has the same tagged-qing 8049 * capability for every lun. The condition can be removed when HBA 8050 * is changed to support per lun based tagged-qing capability. 8051 */ 8052 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 8053 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1); 8054 } 8055 8056 /* 8057 * Use scsi_probe() to issue an INQUIRY command to the device. 8058 * This call will allocate and fill in the scsi_inquiry structure 8059 * and point the sd_inq member of the scsi_device structure to it. 8060 * If the attach succeeds, then this memory will not be de-allocated 8061 * (via scsi_unprobe()) until the instance is detached. 8062 */ 8063 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) { 8064 goto probe_failed; 8065 } 8066 8067 /* 8068 * Check the device type as specified in the inquiry data and 8069 * claim it if it is of a type that we support. 8070 */ 8071 switch (devp->sd_inq->inq_dtype) { 8072 case DTYPE_DIRECT: 8073 break; 8074 case DTYPE_RODIRECT: 8075 break; 8076 case DTYPE_OPTICAL: 8077 break; 8078 case DTYPE_NOTPRESENT: 8079 default: 8080 /* Unsupported device type; fail the attach. */ 8081 goto probe_failed; 8082 } 8083 8084 /* 8085 * Allocate the soft state structure for this unit. 8086 * 8087 * We rely upon this memory being set to all zeroes by 8088 * ddi_soft_state_zalloc(). We assume that any member of the 8089 * soft state structure that is not explicitly initialized by 8090 * this routine will have a value of zero. 8091 */ 8092 instance = ddi_get_instance(devp->sd_dev); 8093 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) { 8094 goto probe_failed; 8095 } 8096 8097 /* 8098 * Retrieve a pointer to the newly-allocated soft state. 8099 * 8100 * This should NEVER fail if the ddi_soft_state_zalloc() call above 8101 * was successful, unless something has gone horribly wrong and the 8102 * ddi's soft state internals are corrupt (in which case it is 8103 * probably better to halt here than just fail the attach....) 8104 */ 8105 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) { 8106 panic("sd_unit_attach: NULL soft state on instance:0x%x", 8107 instance); 8108 /*NOTREACHED*/ 8109 } 8110 8111 /* 8112 * Link the back ptr of the driver soft state to the scsi_device 8113 * struct for this lun. 8114 * Save a pointer to the softstate in the driver-private area of 8115 * the scsi_device struct. 8116 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until 8117 * we first set un->un_sd below. 8118 */ 8119 un->un_sd = devp; 8120 devp->sd_private = (opaque_t)un; 8121 8122 /* 8123 * The following must be after devp is stored in the soft state struct. 8124 */ 8125 #ifdef SDDEBUG 8126 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8127 "%s_unit_attach: un:0x%p instance:%d\n", 8128 ddi_driver_name(devi), un, instance); 8129 #endif 8130 8131 /* 8132 * Set up the device type and node type (for the minor nodes). 8133 * By default we assume that the device can at least support the 8134 * Common Command Set. Call it a CD-ROM if it reports itself 8135 * as a RODIRECT device. 8136 */ 8137 switch (devp->sd_inq->inq_dtype) { 8138 case DTYPE_RODIRECT: 8139 un->un_node_type = DDI_NT_CD_CHAN; 8140 un->un_ctype = CTYPE_CDROM; 8141 break; 8142 case DTYPE_OPTICAL: 8143 un->un_node_type = DDI_NT_BLOCK_CHAN; 8144 un->un_ctype = CTYPE_ROD; 8145 break; 8146 default: 8147 un->un_node_type = DDI_NT_BLOCK_CHAN; 8148 un->un_ctype = CTYPE_CCS; 8149 break; 8150 } 8151 8152 /* 8153 * Try to read the interconnect type from the HBA. 8154 * 8155 * Note: This driver is currently compiled as two binaries, a parallel 8156 * scsi version (sd) and a fibre channel version (ssd). All functional 8157 * differences are determined at compile time. In the future a single 8158 * binary will be provided and the inteconnect type will be used to 8159 * differentiate between fibre and parallel scsi behaviors. At that time 8160 * it will be necessary for all fibre channel HBAs to support this 8161 * property. 8162 * 8163 * set un_f_is_fiber to TRUE ( default fiber ) 8164 */ 8165 un->un_f_is_fibre = TRUE; 8166 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) { 8167 case INTERCONNECT_SSA: 8168 un->un_interconnect_type = SD_INTERCONNECT_SSA; 8169 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8170 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un); 8171 break; 8172 case INTERCONNECT_PARALLEL: 8173 un->un_f_is_fibre = FALSE; 8174 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8175 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8176 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un); 8177 break; 8178 case INTERCONNECT_SATA: 8179 un->un_f_is_fibre = FALSE; 8180 un->un_interconnect_type = SD_INTERCONNECT_SATA; 8181 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8182 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un); 8183 break; 8184 case INTERCONNECT_FIBRE: 8185 un->un_interconnect_type = SD_INTERCONNECT_FIBRE; 8186 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8187 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un); 8188 break; 8189 case INTERCONNECT_FABRIC: 8190 un->un_interconnect_type = SD_INTERCONNECT_FABRIC; 8191 un->un_node_type = DDI_NT_BLOCK_FABRIC; 8192 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8193 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un); 8194 break; 8195 default: 8196 #ifdef SD_DEFAULT_INTERCONNECT_TYPE 8197 /* 8198 * The HBA does not support the "interconnect-type" property 8199 * (or did not provide a recognized type). 8200 * 8201 * Note: This will be obsoleted when a single fibre channel 8202 * and parallel scsi driver is delivered. In the meantime the 8203 * interconnect type will be set to the platform default.If that 8204 * type is not parallel SCSI, it means that we should be 8205 * assuming "ssd" semantics. However, here this also means that 8206 * the FC HBA is not supporting the "interconnect-type" property 8207 * like we expect it to, so log this occurrence. 8208 */ 8209 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE; 8210 if (!SD_IS_PARALLEL_SCSI(un)) { 8211 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8212 "sd_unit_attach: un:0x%p Assuming " 8213 "INTERCONNECT_FIBRE\n", un); 8214 } else { 8215 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8216 "sd_unit_attach: un:0x%p Assuming " 8217 "INTERCONNECT_PARALLEL\n", un); 8218 un->un_f_is_fibre = FALSE; 8219 } 8220 #else 8221 /* 8222 * Note: This source will be implemented when a single fibre 8223 * channel and parallel scsi driver is delivered. The default 8224 * will be to assume that if a device does not support the 8225 * "interconnect-type" property it is a parallel SCSI HBA and 8226 * we will set the interconnect type for parallel scsi. 8227 */ 8228 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL; 8229 un->un_f_is_fibre = FALSE; 8230 #endif 8231 break; 8232 } 8233 8234 if (un->un_f_is_fibre == TRUE) { 8235 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) == 8236 SCSI_VERSION_3) { 8237 switch (un->un_interconnect_type) { 8238 case SD_INTERCONNECT_FIBRE: 8239 case SD_INTERCONNECT_SSA: 8240 un->un_node_type = DDI_NT_BLOCK_WWN; 8241 break; 8242 default: 8243 break; 8244 } 8245 } 8246 } 8247 8248 /* 8249 * Initialize the Request Sense command for the target 8250 */ 8251 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) { 8252 goto alloc_rqs_failed; 8253 } 8254 8255 /* 8256 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc 8257 * with seperate binary for sd and ssd. 8258 * 8259 * x86 has 1 binary, un_retry_count is set base on connection type. 8260 * The hardcoded values will go away when Sparc uses 1 binary 8261 * for sd and ssd. This hardcoded values need to match 8262 * SD_RETRY_COUNT in sddef.h 8263 * The value used is base on interconnect type. 8264 * fibre = 3, parallel = 5 8265 */ 8266 #if defined(__i386) || defined(__amd64) 8267 un->un_retry_count = un->un_f_is_fibre ? 3 : 5; 8268 #else 8269 un->un_retry_count = SD_RETRY_COUNT; 8270 #endif 8271 8272 /* 8273 * Set the per disk retry count to the default number of retries 8274 * for disks and CDROMs. This value can be overridden by the 8275 * disk property list or an entry in sd.conf. 8276 */ 8277 un->un_notready_retry_count = 8278 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un) 8279 : DISK_NOT_READY_RETRY_COUNT(un); 8280 8281 /* 8282 * Set the busy retry count to the default value of un_retry_count. 8283 * This can be overridden by entries in sd.conf or the device 8284 * config table. 8285 */ 8286 un->un_busy_retry_count = un->un_retry_count; 8287 8288 /* 8289 * Init the reset threshold for retries. This number determines 8290 * how many retries must be performed before a reset can be issued 8291 * (for certain error conditions). This can be overridden by entries 8292 * in sd.conf or the device config table. 8293 */ 8294 un->un_reset_retry_count = (un->un_retry_count / 2); 8295 8296 /* 8297 * Set the victim_retry_count to the default un_retry_count 8298 */ 8299 un->un_victim_retry_count = (2 * un->un_retry_count); 8300 8301 /* 8302 * Set the reservation release timeout to the default value of 8303 * 5 seconds. This can be overridden by entries in ssd.conf or the 8304 * device config table. 8305 */ 8306 un->un_reserve_release_time = 5; 8307 8308 /* 8309 * Set up the default maximum transfer size. Note that this may 8310 * get updated later in the attach, when setting up default wide 8311 * operations for disks. 8312 */ 8313 #if defined(__i386) || defined(__amd64) 8314 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE; 8315 #else 8316 un->un_max_xfer_size = (uint_t)maxphys; 8317 #endif 8318 8319 /* 8320 * Get "allow bus device reset" property (defaults to "enabled" if 8321 * the property was not defined). This is to disable bus resets for 8322 * certain kinds of error recovery. Note: In the future when a run-time 8323 * fibre check is available the soft state flag should default to 8324 * enabled. 8325 */ 8326 if (un->un_f_is_fibre == TRUE) { 8327 un->un_f_allow_bus_device_reset = TRUE; 8328 } else { 8329 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 8330 "allow-bus-device-reset", 1) != 0) { 8331 un->un_f_allow_bus_device_reset = TRUE; 8332 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8333 "sd_unit_attach: un:0x%p Bus device reset enabled\n", 8334 un); 8335 } else { 8336 un->un_f_allow_bus_device_reset = FALSE; 8337 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8338 "sd_unit_attach: un:0x%p Bus device reset disabled\n", 8339 un); 8340 } 8341 } 8342 8343 /* 8344 * Check if this is an ATAPI device. ATAPI devices use Group 1 8345 * Read/Write commands and Group 2 Mode Sense/Select commands. 8346 * 8347 * Note: The "obsolete" way of doing this is to check for the "atapi" 8348 * property. The new "variant" property with a value of "atapi" has been 8349 * introduced so that future 'variants' of standard SCSI behavior (like 8350 * atapi) could be specified by the underlying HBA drivers by supplying 8351 * a new value for the "variant" property, instead of having to define a 8352 * new property. 8353 */ 8354 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) { 8355 un->un_f_cfg_is_atapi = TRUE; 8356 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8357 "sd_unit_attach: un:0x%p Atapi device\n", un); 8358 } 8359 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant", 8360 &variantp) == DDI_PROP_SUCCESS) { 8361 if (strcmp(variantp, "atapi") == 0) { 8362 un->un_f_cfg_is_atapi = TRUE; 8363 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8364 "sd_unit_attach: un:0x%p Atapi device\n", un); 8365 } 8366 ddi_prop_free(variantp); 8367 } 8368 8369 un->un_cmd_timeout = SD_IO_TIME; 8370 8371 /* Info on current states, statuses, etc. (Updated frequently) */ 8372 un->un_state = SD_STATE_NORMAL; 8373 un->un_last_state = SD_STATE_NORMAL; 8374 8375 /* Control & status info for command throttling */ 8376 un->un_throttle = sd_max_throttle; 8377 un->un_saved_throttle = sd_max_throttle; 8378 un->un_min_throttle = sd_min_throttle; 8379 8380 if (un->un_f_is_fibre == TRUE) { 8381 un->un_f_use_adaptive_throttle = TRUE; 8382 } else { 8383 un->un_f_use_adaptive_throttle = FALSE; 8384 } 8385 8386 /* Removable media support. */ 8387 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 8388 un->un_mediastate = DKIO_NONE; 8389 un->un_specified_mediastate = DKIO_NONE; 8390 8391 /* CVs for suspend/resume (PM or DR) */ 8392 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 8393 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL); 8394 8395 /* Power management support. */ 8396 un->un_power_level = SD_SPINDLE_UNINIT; 8397 8398 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL); 8399 un->un_f_wcc_inprog = 0; 8400 8401 /* 8402 * The open/close semaphore is used to serialize threads executing 8403 * in the driver's open & close entry point routines for a given 8404 * instance. 8405 */ 8406 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL); 8407 8408 /* 8409 * The conf file entry and softstate variable is a forceful override, 8410 * meaning a non-zero value must be entered to change the default. 8411 */ 8412 un->un_f_disksort_disabled = FALSE; 8413 8414 /* 8415 * Retrieve the properties from the static driver table or the driver 8416 * configuration file (.conf) for this unit and update the soft state 8417 * for the device as needed for the indicated properties. 8418 * Note: the property configuration needs to occur here as some of the 8419 * following routines may have dependancies on soft state flags set 8420 * as part of the driver property configuration. 8421 */ 8422 sd_read_unit_properties(un); 8423 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8424 "sd_unit_attach: un:0x%p property configuration complete.\n", un); 8425 8426 /* 8427 * Only if a device has "hotpluggable" property, it is 8428 * treated as hotpluggable device. Otherwise, it is 8429 * regarded as non-hotpluggable one. 8430 */ 8431 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable", 8432 -1) != -1) { 8433 un->un_f_is_hotpluggable = TRUE; 8434 } 8435 8436 /* 8437 * set unit's attributes(flags) according to "hotpluggable" and 8438 * RMB bit in INQUIRY data. 8439 */ 8440 sd_set_unit_attributes(un, devi); 8441 8442 /* 8443 * By default, we mark the capacity, lbasize, and geometry 8444 * as invalid. Only if we successfully read a valid capacity 8445 * will we update the un_blockcount and un_tgt_blocksize with the 8446 * valid values (the geometry will be validated later). 8447 */ 8448 un->un_f_blockcount_is_valid = FALSE; 8449 un->un_f_tgt_blocksize_is_valid = FALSE; 8450 un->un_f_geometry_is_valid = FALSE; 8451 8452 /* 8453 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine 8454 * otherwise. 8455 */ 8456 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE; 8457 un->un_blockcount = 0; 8458 8459 /* 8460 * Set up the per-instance info needed to determine the correct 8461 * CDBs and other info for issuing commands to the target. 8462 */ 8463 sd_init_cdb_limits(un); 8464 8465 /* 8466 * Set up the IO chains to use, based upon the target type. 8467 */ 8468 if (un->un_f_non_devbsize_supported) { 8469 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA; 8470 } else { 8471 un->un_buf_chain_type = SD_CHAIN_INFO_DISK; 8472 } 8473 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD; 8474 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD; 8475 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD; 8476 8477 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf), 8478 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit, 8479 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER); 8480 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi); 8481 8482 8483 if (ISCD(un)) { 8484 un->un_additional_codes = sd_additional_codes; 8485 } else { 8486 un->un_additional_codes = NULL; 8487 } 8488 8489 /* 8490 * Create the kstats here so they can be available for attach-time 8491 * routines that send commands to the unit (either polled or via 8492 * sd_send_scsi_cmd). 8493 * 8494 * Note: This is a critical sequence that needs to be maintained: 8495 * 1) Instantiate the kstats here, before any routines using the 8496 * iopath (i.e. sd_send_scsi_cmd). 8497 * 2) Instantiate and initialize the partition stats 8498 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 8499 * see detailed comments there. 8500 * 3) Initialize the error stats (sd_set_errstats), following 8501 * sd_validate_geometry(),sd_register_devid(), 8502 * and sd_cache_control(). 8503 */ 8504 8505 un->un_stats = kstat_create(sd_label, instance, 8506 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 8507 if (un->un_stats != NULL) { 8508 un->un_stats->ks_lock = SD_MUTEX(un); 8509 kstat_install(un->un_stats); 8510 } 8511 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8512 "sd_unit_attach: un:0x%p un_stats created\n", un); 8513 8514 sd_create_errstats(un, instance); 8515 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8516 "sd_unit_attach: un:0x%p errstats created\n", un); 8517 8518 /* 8519 * The following if/else code was relocated here from below as part 8520 * of the fix for bug (4430280). However with the default setup added 8521 * on entry to this routine, it's no longer absolutely necessary for 8522 * this to be before the call to sd_spin_up_unit. 8523 */ 8524 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) { 8525 /* 8526 * If SCSI-2 tagged queueing is supported by the target 8527 * and by the host adapter then we will enable it. 8528 */ 8529 un->un_tagflags = 0; 8530 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 8531 (devp->sd_inq->inq_cmdque) && 8532 (un->un_f_arq_enabled == TRUE)) { 8533 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 8534 1, 1) == 1) { 8535 un->un_tagflags = FLAG_STAG; 8536 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8537 "sd_unit_attach: un:0x%p tag queueing " 8538 "enabled\n", un); 8539 } else if (scsi_ifgetcap(SD_ADDRESS(un), 8540 "untagged-qing", 0) == 1) { 8541 un->un_f_opt_queueing = TRUE; 8542 un->un_saved_throttle = un->un_throttle = 8543 min(un->un_throttle, 3); 8544 } else { 8545 un->un_f_opt_queueing = FALSE; 8546 un->un_saved_throttle = un->un_throttle = 1; 8547 } 8548 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0) 8549 == 1) && (un->un_f_arq_enabled == TRUE)) { 8550 /* The Host Adapter supports internal queueing. */ 8551 un->un_f_opt_queueing = TRUE; 8552 un->un_saved_throttle = un->un_throttle = 8553 min(un->un_throttle, 3); 8554 } else { 8555 un->un_f_opt_queueing = FALSE; 8556 un->un_saved_throttle = un->un_throttle = 1; 8557 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8558 "sd_unit_attach: un:0x%p no tag queueing\n", un); 8559 } 8560 8561 /* 8562 * Enable large transfers for SATA/SAS drives 8563 */ 8564 if (SD_IS_SERIAL(un)) { 8565 un->un_max_xfer_size = 8566 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8567 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8568 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8569 "sd_unit_attach: un:0x%p max transfer " 8570 "size=0x%x\n", un, un->un_max_xfer_size); 8571 8572 } 8573 8574 /* Setup or tear down default wide operations for disks */ 8575 8576 /* 8577 * Note: Legacy: it may be possible for both "sd_max_xfer_size" 8578 * and "ssd_max_xfer_size" to exist simultaneously on the same 8579 * system and be set to different values. In the future this 8580 * code may need to be updated when the ssd module is 8581 * obsoleted and removed from the system. (4299588) 8582 */ 8583 if (SD_IS_PARALLEL_SCSI(un) && 8584 (devp->sd_inq->inq_rdf == RDF_SCSI2) && 8585 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) { 8586 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8587 1, 1) == 1) { 8588 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8589 "sd_unit_attach: un:0x%p Wide Transfer " 8590 "enabled\n", un); 8591 } 8592 8593 /* 8594 * If tagged queuing has also been enabled, then 8595 * enable large xfers 8596 */ 8597 if (un->un_saved_throttle == sd_max_throttle) { 8598 un->un_max_xfer_size = 8599 ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8600 sd_max_xfer_size, SD_MAX_XFER_SIZE); 8601 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8602 "sd_unit_attach: un:0x%p max transfer " 8603 "size=0x%x\n", un, un->un_max_xfer_size); 8604 } 8605 } else { 8606 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 8607 0, 1) == 1) { 8608 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8609 "sd_unit_attach: un:0x%p " 8610 "Wide Transfer disabled\n", un); 8611 } 8612 } 8613 } else { 8614 un->un_tagflags = FLAG_STAG; 8615 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY, 8616 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE); 8617 } 8618 8619 /* 8620 * If this target supports LUN reset, try to enable it. 8621 */ 8622 if (un->un_f_lun_reset_enabled) { 8623 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) { 8624 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8625 "un:0x%p lun_reset capability set\n", un); 8626 } else { 8627 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: " 8628 "un:0x%p lun-reset capability not set\n", un); 8629 } 8630 } 8631 8632 /* 8633 * At this point in the attach, we have enough info in the 8634 * soft state to be able to issue commands to the target. 8635 * 8636 * All command paths used below MUST issue their commands as 8637 * SD_PATH_DIRECT. This is important as intermediate layers 8638 * are not all initialized yet (such as PM). 8639 */ 8640 8641 /* 8642 * Send a TEST UNIT READY command to the device. This should clear 8643 * any outstanding UNIT ATTENTION that may be present. 8644 * 8645 * Note: Don't check for success, just track if there is a reservation, 8646 * this is a throw away command to clear any unit attentions. 8647 * 8648 * Note: This MUST be the first command issued to the target during 8649 * attach to ensure power on UNIT ATTENTIONS are cleared. 8650 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated 8651 * with attempts at spinning up a device with no media. 8652 */ 8653 if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) { 8654 reservation_flag = SD_TARGET_IS_RESERVED; 8655 } 8656 8657 /* 8658 * If the device is NOT a removable media device, attempt to spin 8659 * it up (using the START_STOP_UNIT command) and read its capacity 8660 * (using the READ CAPACITY command). Note, however, that either 8661 * of these could fail and in some cases we would continue with 8662 * the attach despite the failure (see below). 8663 */ 8664 if (un->un_f_descr_format_supported) { 8665 switch (sd_spin_up_unit(un)) { 8666 case 0: 8667 /* 8668 * Spin-up was successful; now try to read the 8669 * capacity. If successful then save the results 8670 * and mark the capacity & lbasize as valid. 8671 */ 8672 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8673 "sd_unit_attach: un:0x%p spin-up successful\n", un); 8674 8675 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, 8676 &lbasize, SD_PATH_DIRECT)) { 8677 case 0: { 8678 if (capacity > DK_MAX_BLOCKS) { 8679 #ifdef _LP64 8680 if (capacity + 1 > 8681 SD_GROUP1_MAX_ADDRESS) { 8682 /* 8683 * Enable descriptor format 8684 * sense data so that we can 8685 * get 64 bit sense data 8686 * fields. 8687 */ 8688 sd_enable_descr_sense(un); 8689 } 8690 #else 8691 /* 32-bit kernels can't handle this */ 8692 scsi_log(SD_DEVINFO(un), 8693 sd_label, CE_WARN, 8694 "disk has %llu blocks, which " 8695 "is too large for a 32-bit " 8696 "kernel", capacity); 8697 8698 #if defined(__i386) || defined(__amd64) 8699 /* 8700 * Refer to comments related to off-by-1 8701 * at the header of this file. 8702 * 1TB disk was treated as (1T - 512)B 8703 * in the past, so that it might has 8704 * valid VTOC and solaris partitions, 8705 * we have to allow it to continue to 8706 * work. 8707 */ 8708 if (capacity -1 > DK_MAX_BLOCKS) 8709 #endif 8710 goto spinup_failed; 8711 #endif 8712 } 8713 8714 /* 8715 * Here it's not necessary to check the case: 8716 * the capacity of the device is bigger than 8717 * what the max hba cdb can support. Because 8718 * sd_send_scsi_READ_CAPACITY will retrieve 8719 * the capacity by sending USCSI command, which 8720 * is constrained by the max hba cdb. Actually, 8721 * sd_send_scsi_READ_CAPACITY will return 8722 * EINVAL when using bigger cdb than required 8723 * cdb length. Will handle this case in 8724 * "case EINVAL". 8725 */ 8726 8727 /* 8728 * The following relies on 8729 * sd_send_scsi_READ_CAPACITY never 8730 * returning 0 for capacity and/or lbasize. 8731 */ 8732 sd_update_block_info(un, lbasize, capacity); 8733 8734 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8735 "sd_unit_attach: un:0x%p capacity = %ld " 8736 "blocks; lbasize= %ld.\n", un, 8737 un->un_blockcount, un->un_tgt_blocksize); 8738 8739 break; 8740 } 8741 case EINVAL: 8742 /* 8743 * In the case where the max-cdb-length property 8744 * is smaller than the required CDB length for 8745 * a SCSI device, a target driver can fail to 8746 * attach to that device. 8747 */ 8748 scsi_log(SD_DEVINFO(un), 8749 sd_label, CE_WARN, 8750 "disk capacity is too large " 8751 "for current cdb length"); 8752 goto spinup_failed; 8753 case EACCES: 8754 /* 8755 * Should never get here if the spin-up 8756 * succeeded, but code it in anyway. 8757 * From here, just continue with the attach... 8758 */ 8759 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8760 "sd_unit_attach: un:0x%p " 8761 "sd_send_scsi_READ_CAPACITY " 8762 "returned reservation conflict\n", un); 8763 reservation_flag = SD_TARGET_IS_RESERVED; 8764 break; 8765 default: 8766 /* 8767 * Likewise, should never get here if the 8768 * spin-up succeeded. Just continue with 8769 * the attach... 8770 */ 8771 break; 8772 } 8773 break; 8774 case EACCES: 8775 /* 8776 * Device is reserved by another host. In this case 8777 * we could not spin it up or read the capacity, but 8778 * we continue with the attach anyway. 8779 */ 8780 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8781 "sd_unit_attach: un:0x%p spin-up reservation " 8782 "conflict.\n", un); 8783 reservation_flag = SD_TARGET_IS_RESERVED; 8784 break; 8785 default: 8786 /* Fail the attach if the spin-up failed. */ 8787 SD_INFO(SD_LOG_ATTACH_DETACH, un, 8788 "sd_unit_attach: un:0x%p spin-up failed.", un); 8789 goto spinup_failed; 8790 } 8791 } 8792 8793 /* 8794 * Check to see if this is a MMC drive 8795 */ 8796 if (ISCD(un)) { 8797 sd_set_mmc_caps(un); 8798 } 8799 8800 /* 8801 * Create the minor nodes for the device. 8802 * Note: If we want to support fdisk on both sparc and intel, this will 8803 * have to separate out the notion that VTOC8 is always sparc, and 8804 * VTOC16 is always intel (tho these can be the defaults). The vtoc 8805 * type will have to be determined at run-time, and the fdisk 8806 * partitioning will have to have been read & set up before we 8807 * create the minor nodes. (any other inits (such as kstats) that 8808 * also ought to be done before creating the minor nodes?) (Doesn't 8809 * setting up the minor nodes kind of imply that we're ready to 8810 * handle an open from userland?) 8811 */ 8812 if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) { 8813 goto create_minor_nodes_failed; 8814 } 8815 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8816 "sd_unit_attach: un:0x%p minor nodes created\n", un); 8817 8818 /* 8819 * Add a zero-length attribute to tell the world we support 8820 * kernel ioctls (for layered drivers) 8821 */ 8822 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8823 DDI_KERNEL_IOCTL, NULL, 0); 8824 8825 /* 8826 * Add a boolean property to tell the world we support 8827 * the B_FAILFAST flag (for layered drivers) 8828 */ 8829 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 8830 "ddi-failfast-supported", NULL, 0); 8831 8832 /* 8833 * Initialize power management 8834 */ 8835 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL); 8836 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL); 8837 sd_setup_pm(un, devi); 8838 if (un->un_f_pm_is_enabled == FALSE) { 8839 /* 8840 * For performance, point to a jump table that does 8841 * not include pm. 8842 * The direct and priority chains don't change with PM. 8843 * 8844 * Note: this is currently done based on individual device 8845 * capabilities. When an interface for determining system 8846 * power enabled state becomes available, or when additional 8847 * layers are added to the command chain, these values will 8848 * have to be re-evaluated for correctness. 8849 */ 8850 if (un->un_f_non_devbsize_supported) { 8851 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM; 8852 } else { 8853 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM; 8854 } 8855 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 8856 } 8857 8858 /* 8859 * This property is set to 0 by HA software to avoid retries 8860 * on a reserved disk. (The preferred property name is 8861 * "retry-on-reservation-conflict") (1189689) 8862 * 8863 * Note: The use of a global here can have unintended consequences. A 8864 * per instance variable is preferrable to match the capabilities of 8865 * different underlying hba's (4402600) 8866 */ 8867 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi, 8868 DDI_PROP_DONTPASS, "retry-on-reservation-conflict", 8869 sd_retry_on_reservation_conflict); 8870 if (sd_retry_on_reservation_conflict != 0) { 8871 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, 8872 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name, 8873 sd_retry_on_reservation_conflict); 8874 } 8875 8876 /* Set up options for QFULL handling. */ 8877 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8878 "qfull-retries", -1)) != -1) { 8879 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries", 8880 rval, 1); 8881 } 8882 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0, 8883 "qfull-retry-interval", -1)) != -1) { 8884 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval", 8885 rval, 1); 8886 } 8887 8888 /* 8889 * This just prints a message that announces the existence of the 8890 * device. The message is always printed in the system logfile, but 8891 * only appears on the console if the system is booted with the 8892 * -v (verbose) argument. 8893 */ 8894 ddi_report_dev(devi); 8895 8896 /* 8897 * The framework calls driver attach routines single-threaded 8898 * for a given instance. However we still acquire SD_MUTEX here 8899 * because this required for calling the sd_validate_geometry() 8900 * and sd_register_devid() functions. 8901 */ 8902 mutex_enter(SD_MUTEX(un)); 8903 un->un_f_geometry_is_valid = FALSE; 8904 un->un_mediastate = DKIO_NONE; 8905 un->un_reserved = -1; 8906 8907 /* 8908 * Read and validate the device's geometry (ie, disk label) 8909 * A new unformatted drive will not have a valid geometry, but 8910 * the driver needs to successfully attach to this device so 8911 * the drive can be formatted via ioctls. 8912 */ 8913 if (((sd_validate_geometry(un, SD_PATH_DIRECT) == 8914 ENOTSUP)) && 8915 (un->un_blockcount < DK_MAX_BLOCKS)) { 8916 /* 8917 * We found a small disk with an EFI label on it; 8918 * we need to fix up the minor nodes accordingly. 8919 */ 8920 ddi_remove_minor_node(devi, "h"); 8921 ddi_remove_minor_node(devi, "h,raw"); 8922 (void) ddi_create_minor_node(devi, "wd", 8923 S_IFBLK, 8924 (instance << SDUNIT_SHIFT) | WD_NODE, 8925 un->un_node_type, NULL); 8926 (void) ddi_create_minor_node(devi, "wd,raw", 8927 S_IFCHR, 8928 (instance << SDUNIT_SHIFT) | WD_NODE, 8929 un->un_node_type, NULL); 8930 } 8931 #if defined(__i386) || defined(__amd64) 8932 else if (un->un_f_capacity_adjusted == 1) { 8933 /* 8934 * Refer to comments related to off-by-1 at the 8935 * header of this file. 8936 * Adjust minor node for 1TB disk. 8937 */ 8938 ddi_remove_minor_node(devi, "wd"); 8939 ddi_remove_minor_node(devi, "wd,raw"); 8940 (void) ddi_create_minor_node(devi, "h", 8941 S_IFBLK, 8942 (instance << SDUNIT_SHIFT) | WD_NODE, 8943 un->un_node_type, NULL); 8944 (void) ddi_create_minor_node(devi, "h,raw", 8945 S_IFCHR, 8946 (instance << SDUNIT_SHIFT) | WD_NODE, 8947 un->un_node_type, NULL); 8948 } 8949 #endif 8950 /* 8951 * Read and initialize the devid for the unit. 8952 */ 8953 ASSERT(un->un_errstats != NULL); 8954 if (un->un_f_devid_supported) { 8955 sd_register_devid(un, devi, reservation_flag); 8956 } 8957 mutex_exit(SD_MUTEX(un)); 8958 8959 #if (defined(__fibre)) 8960 /* 8961 * Register callbacks for fibre only. You can't do this soley 8962 * on the basis of the devid_type because this is hba specific. 8963 * We need to query our hba capabilities to find out whether to 8964 * register or not. 8965 */ 8966 if (un->un_f_is_fibre) { 8967 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) { 8968 sd_init_event_callbacks(un); 8969 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 8970 "sd_unit_attach: un:0x%p event callbacks inserted", un); 8971 } 8972 } 8973 #endif 8974 8975 if (un->un_f_opt_disable_cache == TRUE) { 8976 /* 8977 * Disable both read cache and write cache. This is 8978 * the historic behavior of the keywords in the config file. 8979 */ 8980 if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) != 8981 0) { 8982 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 8983 "sd_unit_attach: un:0x%p Could not disable " 8984 "caching", un); 8985 goto devid_failed; 8986 } 8987 } 8988 8989 /* 8990 * Check the value of the WCE bit now and 8991 * set un_f_write_cache_enabled accordingly. 8992 */ 8993 (void) sd_get_write_cache_enabled(un, &wc_enabled); 8994 mutex_enter(SD_MUTEX(un)); 8995 un->un_f_write_cache_enabled = (wc_enabled != 0); 8996 mutex_exit(SD_MUTEX(un)); 8997 8998 /* 8999 * Set the pstat and error stat values here, so data obtained during the 9000 * previous attach-time routines is available. 9001 * 9002 * Note: This is a critical sequence that needs to be maintained: 9003 * 1) Instantiate the kstats before any routines using the iopath 9004 * (i.e. sd_send_scsi_cmd). 9005 * 2) Instantiate and initialize the partition stats 9006 * (sd_set_pstats) in sd_use_efi() and sd_validate_geometry(), 9007 * see detailed comments there. 9008 * 3) Initialize the error stats (sd_set_errstats), following 9009 * sd_validate_geometry(),sd_register_devid(), 9010 * and sd_cache_control(). 9011 */ 9012 sd_set_errstats(un); 9013 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9014 "sd_unit_attach: un:0x%p errstats set\n", un); 9015 9016 /* 9017 * Find out what type of reservation this disk supports. 9018 */ 9019 switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) { 9020 case 0: 9021 /* 9022 * SCSI-3 reservations are supported. 9023 */ 9024 un->un_reservation_type = SD_SCSI3_RESERVATION; 9025 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9026 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un); 9027 break; 9028 case ENOTSUP: 9029 /* 9030 * The PERSISTENT RESERVE IN command would not be recognized by 9031 * a SCSI-2 device, so assume the reservation type is SCSI-2. 9032 */ 9033 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9034 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un); 9035 un->un_reservation_type = SD_SCSI2_RESERVATION; 9036 break; 9037 default: 9038 /* 9039 * default to SCSI-3 reservations 9040 */ 9041 SD_INFO(SD_LOG_ATTACH_DETACH, un, 9042 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un); 9043 un->un_reservation_type = SD_SCSI3_RESERVATION; 9044 break; 9045 } 9046 9047 /* 9048 * After successfully attaching an instance, we record the information 9049 * of how many luns have been attached on the relative target and 9050 * controller for parallel SCSI. This information is used when sd tries 9051 * to set the tagged queuing capability in HBA. 9052 */ 9053 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9054 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH); 9055 } 9056 9057 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 9058 "sd_unit_attach: un:0x%p exit success\n", un); 9059 9060 return (DDI_SUCCESS); 9061 9062 /* 9063 * An error occurred during the attach; clean up & return failure. 9064 */ 9065 9066 devid_failed: 9067 9068 setup_pm_failed: 9069 ddi_remove_minor_node(devi, NULL); 9070 9071 create_minor_nodes_failed: 9072 /* 9073 * Cleanup from the scsi_ifsetcap() calls (437868) 9074 */ 9075 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9076 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9077 9078 /* 9079 * Refer to the comments of setting tagged-qing in the beginning of 9080 * sd_unit_attach. We can only disable tagged queuing when there is 9081 * no lun attached on the target. 9082 */ 9083 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) { 9084 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9085 } 9086 9087 if (un->un_f_is_fibre == FALSE) { 9088 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9089 } 9090 9091 spinup_failed: 9092 9093 mutex_enter(SD_MUTEX(un)); 9094 9095 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */ 9096 if (un->un_direct_priority_timeid != NULL) { 9097 timeout_id_t temp_id = un->un_direct_priority_timeid; 9098 un->un_direct_priority_timeid = NULL; 9099 mutex_exit(SD_MUTEX(un)); 9100 (void) untimeout(temp_id); 9101 mutex_enter(SD_MUTEX(un)); 9102 } 9103 9104 /* Cancel any pending start/stop timeouts */ 9105 if (un->un_startstop_timeid != NULL) { 9106 timeout_id_t temp_id = un->un_startstop_timeid; 9107 un->un_startstop_timeid = NULL; 9108 mutex_exit(SD_MUTEX(un)); 9109 (void) untimeout(temp_id); 9110 mutex_enter(SD_MUTEX(un)); 9111 } 9112 9113 /* Cancel any pending reset-throttle timeouts */ 9114 if (un->un_reset_throttle_timeid != NULL) { 9115 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9116 un->un_reset_throttle_timeid = NULL; 9117 mutex_exit(SD_MUTEX(un)); 9118 (void) untimeout(temp_id); 9119 mutex_enter(SD_MUTEX(un)); 9120 } 9121 9122 /* Cancel any pending retry timeouts */ 9123 if (un->un_retry_timeid != NULL) { 9124 timeout_id_t temp_id = un->un_retry_timeid; 9125 un->un_retry_timeid = NULL; 9126 mutex_exit(SD_MUTEX(un)); 9127 (void) untimeout(temp_id); 9128 mutex_enter(SD_MUTEX(un)); 9129 } 9130 9131 /* Cancel any pending delayed cv broadcast timeouts */ 9132 if (un->un_dcvb_timeid != NULL) { 9133 timeout_id_t temp_id = un->un_dcvb_timeid; 9134 un->un_dcvb_timeid = NULL; 9135 mutex_exit(SD_MUTEX(un)); 9136 (void) untimeout(temp_id); 9137 mutex_enter(SD_MUTEX(un)); 9138 } 9139 9140 mutex_exit(SD_MUTEX(un)); 9141 9142 /* There should not be any in-progress I/O so ASSERT this check */ 9143 ASSERT(un->un_ncmds_in_transport == 0); 9144 ASSERT(un->un_ncmds_in_driver == 0); 9145 9146 /* Do not free the softstate if the callback routine is active */ 9147 sd_sync_with_callback(un); 9148 9149 /* 9150 * Partition stats apparently are not used with removables. These would 9151 * not have been created during attach, so no need to clean them up... 9152 */ 9153 if (un->un_stats != NULL) { 9154 kstat_delete(un->un_stats); 9155 un->un_stats = NULL; 9156 } 9157 if (un->un_errstats != NULL) { 9158 kstat_delete(un->un_errstats); 9159 un->un_errstats = NULL; 9160 } 9161 9162 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9163 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9164 9165 ddi_prop_remove_all(devi); 9166 sema_destroy(&un->un_semoclose); 9167 cv_destroy(&un->un_state_cv); 9168 9169 getrbuf_failed: 9170 9171 sd_free_rqs(un); 9172 9173 alloc_rqs_failed: 9174 9175 devp->sd_private = NULL; 9176 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */ 9177 9178 get_softstate_failed: 9179 /* 9180 * Note: the man pages are unclear as to whether or not doing a 9181 * ddi_soft_state_free(sd_state, instance) is the right way to 9182 * clean up after the ddi_soft_state_zalloc() if the subsequent 9183 * ddi_get_soft_state() fails. The implication seems to be 9184 * that the get_soft_state cannot fail if the zalloc succeeds. 9185 */ 9186 ddi_soft_state_free(sd_state, instance); 9187 9188 probe_failed: 9189 scsi_unprobe(devp); 9190 #ifdef SDDEBUG 9191 if ((sd_component_mask & SD_LOG_ATTACH_DETACH) && 9192 (sd_level_mask & SD_LOGMASK_TRACE)) { 9193 cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n", 9194 (void *)un); 9195 } 9196 #endif 9197 return (DDI_FAILURE); 9198 } 9199 9200 9201 /* 9202 * Function: sd_unit_detach 9203 * 9204 * Description: Performs DDI_DETACH processing for sddetach(). 9205 * 9206 * Return Code: DDI_SUCCESS 9207 * DDI_FAILURE 9208 * 9209 * Context: Kernel thread context 9210 */ 9211 9212 static int 9213 sd_unit_detach(dev_info_t *devi) 9214 { 9215 struct scsi_device *devp; 9216 struct sd_lun *un; 9217 int i; 9218 int tgt; 9219 dev_t dev; 9220 dev_info_t *pdip = ddi_get_parent(devi); 9221 int instance = ddi_get_instance(devi); 9222 9223 mutex_enter(&sd_detach_mutex); 9224 9225 /* 9226 * Fail the detach for any of the following: 9227 * - Unable to get the sd_lun struct for the instance 9228 * - A layered driver has an outstanding open on the instance 9229 * - Another thread is already detaching this instance 9230 * - Another thread is currently performing an open 9231 */ 9232 devp = ddi_get_driver_private(devi); 9233 if ((devp == NULL) || 9234 ((un = (struct sd_lun *)devp->sd_private) == NULL) || 9235 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) || 9236 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) { 9237 mutex_exit(&sd_detach_mutex); 9238 return (DDI_FAILURE); 9239 } 9240 9241 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un); 9242 9243 /* 9244 * Mark this instance as currently in a detach, to inhibit any 9245 * opens from a layered driver. 9246 */ 9247 un->un_detach_count++; 9248 mutex_exit(&sd_detach_mutex); 9249 9250 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 9251 SCSI_ADDR_PROP_TARGET, -1); 9252 9253 dev = sd_make_device(SD_DEVINFO(un)); 9254 9255 #ifndef lint 9256 _NOTE(COMPETING_THREADS_NOW); 9257 #endif 9258 9259 mutex_enter(SD_MUTEX(un)); 9260 9261 /* 9262 * Fail the detach if there are any outstanding layered 9263 * opens on this device. 9264 */ 9265 for (i = 0; i < NDKMAP; i++) { 9266 if (un->un_ocmap.lyropen[i] != 0) { 9267 goto err_notclosed; 9268 } 9269 } 9270 9271 /* 9272 * Verify there are NO outstanding commands issued to this device. 9273 * ie, un_ncmds_in_transport == 0. 9274 * It's possible to have outstanding commands through the physio 9275 * code path, even though everything's closed. 9276 */ 9277 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) || 9278 (un->un_direct_priority_timeid != NULL) || 9279 (un->un_state == SD_STATE_RWAIT)) { 9280 mutex_exit(SD_MUTEX(un)); 9281 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9282 "sd_dr_detach: Detach failure due to outstanding cmds\n"); 9283 goto err_stillbusy; 9284 } 9285 9286 /* 9287 * If we have the device reserved, release the reservation. 9288 */ 9289 if ((un->un_resvd_status & SD_RESERVE) && 9290 !(un->un_resvd_status & SD_LOST_RESERVE)) { 9291 mutex_exit(SD_MUTEX(un)); 9292 /* 9293 * Note: sd_reserve_release sends a command to the device 9294 * via the sd_ioctlcmd() path, and can sleep. 9295 */ 9296 if (sd_reserve_release(dev, SD_RELEASE) != 0) { 9297 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9298 "sd_dr_detach: Cannot release reservation \n"); 9299 } 9300 } else { 9301 mutex_exit(SD_MUTEX(un)); 9302 } 9303 9304 /* 9305 * Untimeout any reserve recover, throttle reset, restart unit 9306 * and delayed broadcast timeout threads. Protect the timeout pointer 9307 * from getting nulled by their callback functions. 9308 */ 9309 mutex_enter(SD_MUTEX(un)); 9310 if (un->un_resvd_timeid != NULL) { 9311 timeout_id_t temp_id = un->un_resvd_timeid; 9312 un->un_resvd_timeid = NULL; 9313 mutex_exit(SD_MUTEX(un)); 9314 (void) untimeout(temp_id); 9315 mutex_enter(SD_MUTEX(un)); 9316 } 9317 9318 if (un->un_reset_throttle_timeid != NULL) { 9319 timeout_id_t temp_id = un->un_reset_throttle_timeid; 9320 un->un_reset_throttle_timeid = NULL; 9321 mutex_exit(SD_MUTEX(un)); 9322 (void) untimeout(temp_id); 9323 mutex_enter(SD_MUTEX(un)); 9324 } 9325 9326 if (un->un_startstop_timeid != NULL) { 9327 timeout_id_t temp_id = un->un_startstop_timeid; 9328 un->un_startstop_timeid = NULL; 9329 mutex_exit(SD_MUTEX(un)); 9330 (void) untimeout(temp_id); 9331 mutex_enter(SD_MUTEX(un)); 9332 } 9333 9334 if (un->un_dcvb_timeid != NULL) { 9335 timeout_id_t temp_id = un->un_dcvb_timeid; 9336 un->un_dcvb_timeid = NULL; 9337 mutex_exit(SD_MUTEX(un)); 9338 (void) untimeout(temp_id); 9339 } else { 9340 mutex_exit(SD_MUTEX(un)); 9341 } 9342 9343 /* Remove any pending reservation reclaim requests for this device */ 9344 sd_rmv_resv_reclaim_req(dev); 9345 9346 mutex_enter(SD_MUTEX(un)); 9347 9348 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */ 9349 if (un->un_direct_priority_timeid != NULL) { 9350 timeout_id_t temp_id = un->un_direct_priority_timeid; 9351 un->un_direct_priority_timeid = NULL; 9352 mutex_exit(SD_MUTEX(un)); 9353 (void) untimeout(temp_id); 9354 mutex_enter(SD_MUTEX(un)); 9355 } 9356 9357 /* Cancel any active multi-host disk watch thread requests */ 9358 if (un->un_mhd_token != NULL) { 9359 mutex_exit(SD_MUTEX(un)); 9360 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token)); 9361 if (scsi_watch_request_terminate(un->un_mhd_token, 9362 SCSI_WATCH_TERMINATE_NOWAIT)) { 9363 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9364 "sd_dr_detach: Cannot cancel mhd watch request\n"); 9365 /* 9366 * Note: We are returning here after having removed 9367 * some driver timeouts above. This is consistent with 9368 * the legacy implementation but perhaps the watch 9369 * terminate call should be made with the wait flag set. 9370 */ 9371 goto err_stillbusy; 9372 } 9373 mutex_enter(SD_MUTEX(un)); 9374 un->un_mhd_token = NULL; 9375 } 9376 9377 if (un->un_swr_token != NULL) { 9378 mutex_exit(SD_MUTEX(un)); 9379 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token)); 9380 if (scsi_watch_request_terminate(un->un_swr_token, 9381 SCSI_WATCH_TERMINATE_NOWAIT)) { 9382 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9383 "sd_dr_detach: Cannot cancel swr watch request\n"); 9384 /* 9385 * Note: We are returning here after having removed 9386 * some driver timeouts above. This is consistent with 9387 * the legacy implementation but perhaps the watch 9388 * terminate call should be made with the wait flag set. 9389 */ 9390 goto err_stillbusy; 9391 } 9392 mutex_enter(SD_MUTEX(un)); 9393 un->un_swr_token = NULL; 9394 } 9395 9396 mutex_exit(SD_MUTEX(un)); 9397 9398 /* 9399 * Clear any scsi_reset_notifies. We clear the reset notifies 9400 * if we have not registered one. 9401 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX! 9402 */ 9403 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 9404 sd_mhd_reset_notify_cb, (caddr_t)un); 9405 9406 /* 9407 * protect the timeout pointers from getting nulled by 9408 * their callback functions during the cancellation process. 9409 * In such a scenario untimeout can be invoked with a null value. 9410 */ 9411 _NOTE(NO_COMPETING_THREADS_NOW); 9412 9413 mutex_enter(&un->un_pm_mutex); 9414 if (un->un_pm_idle_timeid != NULL) { 9415 timeout_id_t temp_id = un->un_pm_idle_timeid; 9416 un->un_pm_idle_timeid = NULL; 9417 mutex_exit(&un->un_pm_mutex); 9418 9419 /* 9420 * Timeout is active; cancel it. 9421 * Note that it'll never be active on a device 9422 * that does not support PM therefore we don't 9423 * have to check before calling pm_idle_component. 9424 */ 9425 (void) untimeout(temp_id); 9426 (void) pm_idle_component(SD_DEVINFO(un), 0); 9427 mutex_enter(&un->un_pm_mutex); 9428 } 9429 9430 /* 9431 * Check whether there is already a timeout scheduled for power 9432 * management. If yes then don't lower the power here, that's. 9433 * the timeout handler's job. 9434 */ 9435 if (un->un_pm_timeid != NULL) { 9436 timeout_id_t temp_id = un->un_pm_timeid; 9437 un->un_pm_timeid = NULL; 9438 mutex_exit(&un->un_pm_mutex); 9439 /* 9440 * Timeout is active; cancel it. 9441 * Note that it'll never be active on a device 9442 * that does not support PM therefore we don't 9443 * have to check before calling pm_idle_component. 9444 */ 9445 (void) untimeout(temp_id); 9446 (void) pm_idle_component(SD_DEVINFO(un), 0); 9447 9448 } else { 9449 mutex_exit(&un->un_pm_mutex); 9450 if ((un->un_f_pm_is_enabled == TRUE) && 9451 (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) != 9452 DDI_SUCCESS)) { 9453 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9454 "sd_dr_detach: Lower power request failed, ignoring.\n"); 9455 /* 9456 * Fix for bug: 4297749, item # 13 9457 * The above test now includes a check to see if PM is 9458 * supported by this device before call 9459 * pm_lower_power(). 9460 * Note, the following is not dead code. The call to 9461 * pm_lower_power above will generate a call back into 9462 * our sdpower routine which might result in a timeout 9463 * handler getting activated. Therefore the following 9464 * code is valid and necessary. 9465 */ 9466 mutex_enter(&un->un_pm_mutex); 9467 if (un->un_pm_timeid != NULL) { 9468 timeout_id_t temp_id = un->un_pm_timeid; 9469 un->un_pm_timeid = NULL; 9470 mutex_exit(&un->un_pm_mutex); 9471 (void) untimeout(temp_id); 9472 (void) pm_idle_component(SD_DEVINFO(un), 0); 9473 } else { 9474 mutex_exit(&un->un_pm_mutex); 9475 } 9476 } 9477 } 9478 9479 /* 9480 * Cleanup from the scsi_ifsetcap() calls (437868) 9481 * Relocated here from above to be after the call to 9482 * pm_lower_power, which was getting errors. 9483 */ 9484 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1); 9485 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1); 9486 9487 /* 9488 * Currently, tagged queuing is supported per target based by HBA. 9489 * Setting this per lun instance actually sets the capability of this 9490 * target in HBA, which affects those luns already attached on the 9491 * same target. So during detach, we can only disable this capability 9492 * only when this is the only lun left on this target. By doing 9493 * this, we assume a target has the same tagged queuing capability 9494 * for every lun. The condition can be removed when HBA is changed to 9495 * support per lun based tagged queuing capability. 9496 */ 9497 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) { 9498 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 9499 } 9500 9501 if (un->un_f_is_fibre == FALSE) { 9502 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1); 9503 } 9504 9505 /* 9506 * Remove any event callbacks, fibre only 9507 */ 9508 if (un->un_f_is_fibre == TRUE) { 9509 if ((un->un_insert_event != NULL) && 9510 (ddi_remove_event_handler(un->un_insert_cb_id) != 9511 DDI_SUCCESS)) { 9512 /* 9513 * Note: We are returning here after having done 9514 * substantial cleanup above. This is consistent 9515 * with the legacy implementation but this may not 9516 * be the right thing to do. 9517 */ 9518 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9519 "sd_dr_detach: Cannot cancel insert event\n"); 9520 goto err_remove_event; 9521 } 9522 un->un_insert_event = NULL; 9523 9524 if ((un->un_remove_event != NULL) && 9525 (ddi_remove_event_handler(un->un_remove_cb_id) != 9526 DDI_SUCCESS)) { 9527 /* 9528 * Note: We are returning here after having done 9529 * substantial cleanup above. This is consistent 9530 * with the legacy implementation but this may not 9531 * be the right thing to do. 9532 */ 9533 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9534 "sd_dr_detach: Cannot cancel remove event\n"); 9535 goto err_remove_event; 9536 } 9537 un->un_remove_event = NULL; 9538 } 9539 9540 /* Do not free the softstate if the callback routine is active */ 9541 sd_sync_with_callback(un); 9542 9543 /* 9544 * Hold the detach mutex here, to make sure that no other threads ever 9545 * can access a (partially) freed soft state structure. 9546 */ 9547 mutex_enter(&sd_detach_mutex); 9548 9549 /* 9550 * Clean up the soft state struct. 9551 * Cleanup is done in reverse order of allocs/inits. 9552 * At this point there should be no competing threads anymore. 9553 */ 9554 9555 /* Unregister and free device id. */ 9556 ddi_devid_unregister(devi); 9557 if (un->un_devid) { 9558 ddi_devid_free(un->un_devid); 9559 un->un_devid = NULL; 9560 } 9561 9562 /* 9563 * Destroy wmap cache if it exists. 9564 */ 9565 if (un->un_wm_cache != NULL) { 9566 kmem_cache_destroy(un->un_wm_cache); 9567 un->un_wm_cache = NULL; 9568 } 9569 9570 /* Remove minor nodes */ 9571 ddi_remove_minor_node(devi, NULL); 9572 9573 /* 9574 * kstat cleanup is done in detach for all device types (4363169). 9575 * We do not want to fail detach if the device kstats are not deleted 9576 * since there is a confusion about the devo_refcnt for the device. 9577 * We just delete the kstats and let detach complete successfully. 9578 */ 9579 if (un->un_stats != NULL) { 9580 kstat_delete(un->un_stats); 9581 un->un_stats = NULL; 9582 } 9583 if (un->un_errstats != NULL) { 9584 kstat_delete(un->un_errstats); 9585 un->un_errstats = NULL; 9586 } 9587 9588 /* Remove partition stats */ 9589 if (un->un_f_pkstats_enabled) { 9590 for (i = 0; i < NSDMAP; i++) { 9591 if (un->un_pstats[i] != NULL) { 9592 kstat_delete(un->un_pstats[i]); 9593 un->un_pstats[i] = NULL; 9594 } 9595 } 9596 } 9597 9598 /* Remove xbuf registration */ 9599 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi); 9600 ddi_xbuf_attr_destroy(un->un_xbuf_attr); 9601 9602 /* Remove driver properties */ 9603 ddi_prop_remove_all(devi); 9604 9605 mutex_destroy(&un->un_pm_mutex); 9606 cv_destroy(&un->un_pm_busy_cv); 9607 9608 cv_destroy(&un->un_wcc_cv); 9609 9610 /* Open/close semaphore */ 9611 sema_destroy(&un->un_semoclose); 9612 9613 /* Removable media condvar. */ 9614 cv_destroy(&un->un_state_cv); 9615 9616 /* Suspend/resume condvar. */ 9617 cv_destroy(&un->un_suspend_cv); 9618 cv_destroy(&un->un_disk_busy_cv); 9619 9620 sd_free_rqs(un); 9621 9622 /* Free up soft state */ 9623 devp->sd_private = NULL; 9624 bzero(un, sizeof (struct sd_lun)); 9625 ddi_soft_state_free(sd_state, instance); 9626 9627 mutex_exit(&sd_detach_mutex); 9628 9629 /* This frees up the INQUIRY data associated with the device. */ 9630 scsi_unprobe(devp); 9631 9632 /* 9633 * After successfully detaching an instance, we update the information 9634 * of how many luns have been attached in the relative target and 9635 * controller for parallel SCSI. This information is used when sd tries 9636 * to set the tagged queuing capability in HBA. 9637 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to 9638 * check if the device is parallel SCSI. However, we don't need to 9639 * check here because we've already checked during attach. No device 9640 * that is not parallel SCSI is in the chain. 9641 */ 9642 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) { 9643 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH); 9644 } 9645 9646 return (DDI_SUCCESS); 9647 9648 err_notclosed: 9649 mutex_exit(SD_MUTEX(un)); 9650 9651 err_stillbusy: 9652 _NOTE(NO_COMPETING_THREADS_NOW); 9653 9654 err_remove_event: 9655 mutex_enter(&sd_detach_mutex); 9656 un->un_detach_count--; 9657 mutex_exit(&sd_detach_mutex); 9658 9659 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n"); 9660 return (DDI_FAILURE); 9661 } 9662 9663 9664 /* 9665 * Driver minor node structure and data table 9666 */ 9667 struct driver_minor_data { 9668 char *name; 9669 minor_t minor; 9670 int type; 9671 }; 9672 9673 static struct driver_minor_data sd_minor_data[] = { 9674 {"a", 0, S_IFBLK}, 9675 {"b", 1, S_IFBLK}, 9676 {"c", 2, S_IFBLK}, 9677 {"d", 3, S_IFBLK}, 9678 {"e", 4, S_IFBLK}, 9679 {"f", 5, S_IFBLK}, 9680 {"g", 6, S_IFBLK}, 9681 {"h", 7, S_IFBLK}, 9682 #if defined(_SUNOS_VTOC_16) 9683 {"i", 8, S_IFBLK}, 9684 {"j", 9, S_IFBLK}, 9685 {"k", 10, S_IFBLK}, 9686 {"l", 11, S_IFBLK}, 9687 {"m", 12, S_IFBLK}, 9688 {"n", 13, S_IFBLK}, 9689 {"o", 14, S_IFBLK}, 9690 {"p", 15, S_IFBLK}, 9691 #endif /* defined(_SUNOS_VTOC_16) */ 9692 #if defined(_FIRMWARE_NEEDS_FDISK) 9693 {"q", 16, S_IFBLK}, 9694 {"r", 17, S_IFBLK}, 9695 {"s", 18, S_IFBLK}, 9696 {"t", 19, S_IFBLK}, 9697 {"u", 20, S_IFBLK}, 9698 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9699 {"a,raw", 0, S_IFCHR}, 9700 {"b,raw", 1, S_IFCHR}, 9701 {"c,raw", 2, S_IFCHR}, 9702 {"d,raw", 3, S_IFCHR}, 9703 {"e,raw", 4, S_IFCHR}, 9704 {"f,raw", 5, S_IFCHR}, 9705 {"g,raw", 6, S_IFCHR}, 9706 {"h,raw", 7, S_IFCHR}, 9707 #if defined(_SUNOS_VTOC_16) 9708 {"i,raw", 8, S_IFCHR}, 9709 {"j,raw", 9, S_IFCHR}, 9710 {"k,raw", 10, S_IFCHR}, 9711 {"l,raw", 11, S_IFCHR}, 9712 {"m,raw", 12, S_IFCHR}, 9713 {"n,raw", 13, S_IFCHR}, 9714 {"o,raw", 14, S_IFCHR}, 9715 {"p,raw", 15, S_IFCHR}, 9716 #endif /* defined(_SUNOS_VTOC_16) */ 9717 #if defined(_FIRMWARE_NEEDS_FDISK) 9718 {"q,raw", 16, S_IFCHR}, 9719 {"r,raw", 17, S_IFCHR}, 9720 {"s,raw", 18, S_IFCHR}, 9721 {"t,raw", 19, S_IFCHR}, 9722 {"u,raw", 20, S_IFCHR}, 9723 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9724 {0} 9725 }; 9726 9727 static struct driver_minor_data sd_minor_data_efi[] = { 9728 {"a", 0, S_IFBLK}, 9729 {"b", 1, S_IFBLK}, 9730 {"c", 2, S_IFBLK}, 9731 {"d", 3, S_IFBLK}, 9732 {"e", 4, S_IFBLK}, 9733 {"f", 5, S_IFBLK}, 9734 {"g", 6, S_IFBLK}, 9735 {"wd", 7, S_IFBLK}, 9736 #if defined(_FIRMWARE_NEEDS_FDISK) 9737 {"q", 16, S_IFBLK}, 9738 {"r", 17, S_IFBLK}, 9739 {"s", 18, S_IFBLK}, 9740 {"t", 19, S_IFBLK}, 9741 {"u", 20, S_IFBLK}, 9742 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9743 {"a,raw", 0, S_IFCHR}, 9744 {"b,raw", 1, S_IFCHR}, 9745 {"c,raw", 2, S_IFCHR}, 9746 {"d,raw", 3, S_IFCHR}, 9747 {"e,raw", 4, S_IFCHR}, 9748 {"f,raw", 5, S_IFCHR}, 9749 {"g,raw", 6, S_IFCHR}, 9750 {"wd,raw", 7, S_IFCHR}, 9751 #if defined(_FIRMWARE_NEEDS_FDISK) 9752 {"q,raw", 16, S_IFCHR}, 9753 {"r,raw", 17, S_IFCHR}, 9754 {"s,raw", 18, S_IFCHR}, 9755 {"t,raw", 19, S_IFCHR}, 9756 {"u,raw", 20, S_IFCHR}, 9757 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 9758 {0} 9759 }; 9760 9761 9762 /* 9763 * Function: sd_create_minor_nodes 9764 * 9765 * Description: Create the minor device nodes for the instance. 9766 * 9767 * Arguments: un - driver soft state (unit) structure 9768 * devi - pointer to device info structure 9769 * 9770 * Return Code: DDI_SUCCESS 9771 * DDI_FAILURE 9772 * 9773 * Context: Kernel thread context 9774 */ 9775 9776 static int 9777 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi) 9778 { 9779 struct driver_minor_data *dmdp; 9780 struct scsi_device *devp; 9781 int instance; 9782 char name[48]; 9783 9784 ASSERT(un != NULL); 9785 devp = ddi_get_driver_private(devi); 9786 instance = ddi_get_instance(devp->sd_dev); 9787 9788 /* 9789 * Create all the minor nodes for this target. 9790 */ 9791 if (un->un_blockcount > DK_MAX_BLOCKS) 9792 dmdp = sd_minor_data_efi; 9793 else 9794 dmdp = sd_minor_data; 9795 while (dmdp->name != NULL) { 9796 9797 (void) sprintf(name, "%s", dmdp->name); 9798 9799 if (ddi_create_minor_node(devi, name, dmdp->type, 9800 (instance << SDUNIT_SHIFT) | dmdp->minor, 9801 un->un_node_type, NULL) == DDI_FAILURE) { 9802 /* 9803 * Clean up any nodes that may have been created, in 9804 * case this fails in the middle of the loop. 9805 */ 9806 ddi_remove_minor_node(devi, NULL); 9807 return (DDI_FAILURE); 9808 } 9809 dmdp++; 9810 } 9811 9812 return (DDI_SUCCESS); 9813 } 9814 9815 9816 /* 9817 * Function: sd_create_errstats 9818 * 9819 * Description: This routine instantiates the device error stats. 9820 * 9821 * Note: During attach the stats are instantiated first so they are 9822 * available for attach-time routines that utilize the driver 9823 * iopath to send commands to the device. The stats are initialized 9824 * separately so data obtained during some attach-time routines is 9825 * available. (4362483) 9826 * 9827 * Arguments: un - driver soft state (unit) structure 9828 * instance - driver instance 9829 * 9830 * Context: Kernel thread context 9831 */ 9832 9833 static void 9834 sd_create_errstats(struct sd_lun *un, int instance) 9835 { 9836 struct sd_errstats *stp; 9837 char kstatmodule_err[KSTAT_STRLEN]; 9838 char kstatname[KSTAT_STRLEN]; 9839 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t)); 9840 9841 ASSERT(un != NULL); 9842 9843 if (un->un_errstats != NULL) { 9844 return; 9845 } 9846 9847 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err), 9848 "%serr", sd_label); 9849 (void) snprintf(kstatname, sizeof (kstatname), 9850 "%s%d,err", sd_label, instance); 9851 9852 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname, 9853 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT); 9854 9855 if (un->un_errstats == NULL) { 9856 SD_ERROR(SD_LOG_ATTACH_DETACH, un, 9857 "sd_create_errstats: Failed kstat_create\n"); 9858 return; 9859 } 9860 9861 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9862 kstat_named_init(&stp->sd_softerrs, "Soft Errors", 9863 KSTAT_DATA_UINT32); 9864 kstat_named_init(&stp->sd_harderrs, "Hard Errors", 9865 KSTAT_DATA_UINT32); 9866 kstat_named_init(&stp->sd_transerrs, "Transport Errors", 9867 KSTAT_DATA_UINT32); 9868 kstat_named_init(&stp->sd_vid, "Vendor", 9869 KSTAT_DATA_CHAR); 9870 kstat_named_init(&stp->sd_pid, "Product", 9871 KSTAT_DATA_CHAR); 9872 kstat_named_init(&stp->sd_revision, "Revision", 9873 KSTAT_DATA_CHAR); 9874 kstat_named_init(&stp->sd_serial, "Serial No", 9875 KSTAT_DATA_CHAR); 9876 kstat_named_init(&stp->sd_capacity, "Size", 9877 KSTAT_DATA_ULONGLONG); 9878 kstat_named_init(&stp->sd_rq_media_err, "Media Error", 9879 KSTAT_DATA_UINT32); 9880 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready", 9881 KSTAT_DATA_UINT32); 9882 kstat_named_init(&stp->sd_rq_nodev_err, "No Device", 9883 KSTAT_DATA_UINT32); 9884 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable", 9885 KSTAT_DATA_UINT32); 9886 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request", 9887 KSTAT_DATA_UINT32); 9888 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis", 9889 KSTAT_DATA_UINT32); 9890 9891 un->un_errstats->ks_private = un; 9892 un->un_errstats->ks_update = nulldev; 9893 9894 kstat_install(un->un_errstats); 9895 } 9896 9897 9898 /* 9899 * Function: sd_set_errstats 9900 * 9901 * Description: This routine sets the value of the vendor id, product id, 9902 * revision, serial number, and capacity device error stats. 9903 * 9904 * Note: During attach the stats are instantiated first so they are 9905 * available for attach-time routines that utilize the driver 9906 * iopath to send commands to the device. The stats are initialized 9907 * separately so data obtained during some attach-time routines is 9908 * available. (4362483) 9909 * 9910 * Arguments: un - driver soft state (unit) structure 9911 * 9912 * Context: Kernel thread context 9913 */ 9914 9915 static void 9916 sd_set_errstats(struct sd_lun *un) 9917 { 9918 struct sd_errstats *stp; 9919 9920 ASSERT(un != NULL); 9921 ASSERT(un->un_errstats != NULL); 9922 stp = (struct sd_errstats *)un->un_errstats->ks_data; 9923 ASSERT(stp != NULL); 9924 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8); 9925 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16); 9926 (void) strncpy(stp->sd_revision.value.c, 9927 un->un_sd->sd_inq->inq_revision, 4); 9928 9929 /* 9930 * All the errstats are persistent across detach/attach, 9931 * so reset all the errstats here in case of the hot 9932 * replacement of disk drives, except for not changed 9933 * Sun qualified drives. 9934 */ 9935 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) || 9936 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9937 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) { 9938 stp->sd_softerrs.value.ui32 = 0; 9939 stp->sd_harderrs.value.ui32 = 0; 9940 stp->sd_transerrs.value.ui32 = 0; 9941 stp->sd_rq_media_err.value.ui32 = 0; 9942 stp->sd_rq_ntrdy_err.value.ui32 = 0; 9943 stp->sd_rq_nodev_err.value.ui32 = 0; 9944 stp->sd_rq_recov_err.value.ui32 = 0; 9945 stp->sd_rq_illrq_err.value.ui32 = 0; 9946 stp->sd_rq_pfa_err.value.ui32 = 0; 9947 } 9948 9949 /* 9950 * Set the "Serial No" kstat for Sun qualified drives (indicated by 9951 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid) 9952 * (4376302)) 9953 */ 9954 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) { 9955 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c, 9956 sizeof (SD_INQUIRY(un)->inq_serial)); 9957 } 9958 9959 if (un->un_f_blockcount_is_valid != TRUE) { 9960 /* 9961 * Set capacity error stat to 0 for no media. This ensures 9962 * a valid capacity is displayed in response to 'iostat -E' 9963 * when no media is present in the device. 9964 */ 9965 stp->sd_capacity.value.ui64 = 0; 9966 } else { 9967 /* 9968 * Multiply un_blockcount by un->un_sys_blocksize to get 9969 * capacity. 9970 * 9971 * Note: for non-512 blocksize devices "un_blockcount" has been 9972 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by 9973 * (un_tgt_blocksize / un->un_sys_blocksize). 9974 */ 9975 stp->sd_capacity.value.ui64 = (uint64_t) 9976 ((uint64_t)un->un_blockcount * un->un_sys_blocksize); 9977 } 9978 } 9979 9980 9981 /* 9982 * Function: sd_set_pstats 9983 * 9984 * Description: This routine instantiates and initializes the partition 9985 * stats for each partition with more than zero blocks. 9986 * (4363169) 9987 * 9988 * Arguments: un - driver soft state (unit) structure 9989 * 9990 * Context: Kernel thread context 9991 */ 9992 9993 static void 9994 sd_set_pstats(struct sd_lun *un) 9995 { 9996 char kstatname[KSTAT_STRLEN]; 9997 int instance; 9998 int i; 9999 10000 ASSERT(un != NULL); 10001 10002 instance = ddi_get_instance(SD_DEVINFO(un)); 10003 10004 /* Note:x86: is this a VTOC8/VTOC16 difference? */ 10005 for (i = 0; i < NSDMAP; i++) { 10006 if ((un->un_pstats[i] == NULL) && 10007 (un->un_map[i].dkl_nblk != 0)) { 10008 (void) snprintf(kstatname, sizeof (kstatname), 10009 "%s%d,%s", sd_label, instance, 10010 sd_minor_data[i].name); 10011 un->un_pstats[i] = kstat_create(sd_label, 10012 instance, kstatname, "partition", KSTAT_TYPE_IO, 10013 1, KSTAT_FLAG_PERSISTENT); 10014 if (un->un_pstats[i] != NULL) { 10015 un->un_pstats[i]->ks_lock = SD_MUTEX(un); 10016 kstat_install(un->un_pstats[i]); 10017 } 10018 } 10019 } 10020 } 10021 10022 10023 #if (defined(__fibre)) 10024 /* 10025 * Function: sd_init_event_callbacks 10026 * 10027 * Description: This routine initializes the insertion and removal event 10028 * callbacks. (fibre only) 10029 * 10030 * Arguments: un - driver soft state (unit) structure 10031 * 10032 * Context: Kernel thread context 10033 */ 10034 10035 static void 10036 sd_init_event_callbacks(struct sd_lun *un) 10037 { 10038 ASSERT(un != NULL); 10039 10040 if ((un->un_insert_event == NULL) && 10041 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT, 10042 &un->un_insert_event) == DDI_SUCCESS)) { 10043 /* 10044 * Add the callback for an insertion event 10045 */ 10046 (void) ddi_add_event_handler(SD_DEVINFO(un), 10047 un->un_insert_event, sd_event_callback, (void *)un, 10048 &(un->un_insert_cb_id)); 10049 } 10050 10051 if ((un->un_remove_event == NULL) && 10052 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT, 10053 &un->un_remove_event) == DDI_SUCCESS)) { 10054 /* 10055 * Add the callback for a removal event 10056 */ 10057 (void) ddi_add_event_handler(SD_DEVINFO(un), 10058 un->un_remove_event, sd_event_callback, (void *)un, 10059 &(un->un_remove_cb_id)); 10060 } 10061 } 10062 10063 10064 /* 10065 * Function: sd_event_callback 10066 * 10067 * Description: This routine handles insert/remove events (photon). The 10068 * state is changed to OFFLINE which can be used to supress 10069 * error msgs. (fibre only) 10070 * 10071 * Arguments: un - driver soft state (unit) structure 10072 * 10073 * Context: Callout thread context 10074 */ 10075 /* ARGSUSED */ 10076 static void 10077 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg, 10078 void *bus_impldata) 10079 { 10080 struct sd_lun *un = (struct sd_lun *)arg; 10081 10082 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event)); 10083 if (event == un->un_insert_event) { 10084 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event"); 10085 mutex_enter(SD_MUTEX(un)); 10086 if (un->un_state == SD_STATE_OFFLINE) { 10087 if (un->un_last_state != SD_STATE_SUSPENDED) { 10088 un->un_state = un->un_last_state; 10089 } else { 10090 /* 10091 * We have gone through SUSPEND/RESUME while 10092 * we were offline. Restore the last state 10093 */ 10094 un->un_state = un->un_save_state; 10095 } 10096 } 10097 mutex_exit(SD_MUTEX(un)); 10098 10099 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event)); 10100 } else if (event == un->un_remove_event) { 10101 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event"); 10102 mutex_enter(SD_MUTEX(un)); 10103 /* 10104 * We need to handle an event callback that occurs during 10105 * the suspend operation, since we don't prevent it. 10106 */ 10107 if (un->un_state != SD_STATE_OFFLINE) { 10108 if (un->un_state != SD_STATE_SUSPENDED) { 10109 New_state(un, SD_STATE_OFFLINE); 10110 } else { 10111 un->un_last_state = SD_STATE_OFFLINE; 10112 } 10113 } 10114 mutex_exit(SD_MUTEX(un)); 10115 } else { 10116 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 10117 "!Unknown event\n"); 10118 } 10119 10120 } 10121 #endif 10122 10123 /* 10124 * Function: sd_cache_control() 10125 * 10126 * Description: This routine is the driver entry point for setting 10127 * read and write caching by modifying the WCE (write cache 10128 * enable) and RCD (read cache disable) bits of mode 10129 * page 8 (MODEPAGE_CACHING). 10130 * 10131 * Arguments: un - driver soft state (unit) structure 10132 * rcd_flag - flag for controlling the read cache 10133 * wce_flag - flag for controlling the write cache 10134 * 10135 * Return Code: EIO 10136 * code returned by sd_send_scsi_MODE_SENSE and 10137 * sd_send_scsi_MODE_SELECT 10138 * 10139 * Context: Kernel Thread 10140 */ 10141 10142 static int 10143 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag) 10144 { 10145 struct mode_caching *mode_caching_page; 10146 uchar_t *header; 10147 size_t buflen; 10148 int hdrlen; 10149 int bd_len; 10150 int rval = 0; 10151 struct mode_header_grp2 *mhp; 10152 10153 ASSERT(un != NULL); 10154 10155 /* 10156 * Do a test unit ready, otherwise a mode sense may not work if this 10157 * is the first command sent to the device after boot. 10158 */ 10159 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10160 10161 if (un->un_f_cfg_is_atapi == TRUE) { 10162 hdrlen = MODE_HEADER_LENGTH_GRP2; 10163 } else { 10164 hdrlen = MODE_HEADER_LENGTH; 10165 } 10166 10167 /* 10168 * Allocate memory for the retrieved mode page and its headers. Set 10169 * a pointer to the page itself. Use mode_cache_scsi3 to insure 10170 * we get all of the mode sense data otherwise, the mode select 10171 * will fail. mode_cache_scsi3 is a superset of mode_caching. 10172 */ 10173 buflen = hdrlen + MODE_BLK_DESC_LENGTH + 10174 sizeof (struct mode_cache_scsi3); 10175 10176 header = kmem_zalloc(buflen, KM_SLEEP); 10177 10178 /* Get the information from the device. */ 10179 if (un->un_f_cfg_is_atapi == TRUE) { 10180 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10181 MODEPAGE_CACHING, SD_PATH_DIRECT); 10182 } else { 10183 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10184 MODEPAGE_CACHING, SD_PATH_DIRECT); 10185 } 10186 if (rval != 0) { 10187 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10188 "sd_cache_control: Mode Sense Failed\n"); 10189 kmem_free(header, buflen); 10190 return (rval); 10191 } 10192 10193 /* 10194 * Determine size of Block Descriptors in order to locate 10195 * the mode page data. ATAPI devices return 0, SCSI devices 10196 * should return MODE_BLK_DESC_LENGTH. 10197 */ 10198 if (un->un_f_cfg_is_atapi == TRUE) { 10199 mhp = (struct mode_header_grp2 *)header; 10200 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10201 } else { 10202 bd_len = ((struct mode_header *)header)->bdesc_length; 10203 } 10204 10205 if (bd_len > MODE_BLK_DESC_LENGTH) { 10206 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10207 "sd_cache_control: Mode Sense returned invalid " 10208 "block descriptor length\n"); 10209 kmem_free(header, buflen); 10210 return (EIO); 10211 } 10212 10213 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10214 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10215 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10216 " caching page code mismatch %d\n", 10217 mode_caching_page->mode_page.code); 10218 kmem_free(header, buflen); 10219 return (EIO); 10220 } 10221 10222 /* Check the relevant bits on successful mode sense. */ 10223 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) || 10224 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) || 10225 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) || 10226 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) { 10227 10228 size_t sbuflen; 10229 uchar_t save_pg; 10230 10231 /* 10232 * Construct select buffer length based on the 10233 * length of the sense data returned. 10234 */ 10235 sbuflen = hdrlen + MODE_BLK_DESC_LENGTH + 10236 sizeof (struct mode_page) + 10237 (int)mode_caching_page->mode_page.length; 10238 10239 /* 10240 * Set the caching bits as requested. 10241 */ 10242 if (rcd_flag == SD_CACHE_ENABLE) 10243 mode_caching_page->rcd = 0; 10244 else if (rcd_flag == SD_CACHE_DISABLE) 10245 mode_caching_page->rcd = 1; 10246 10247 if (wce_flag == SD_CACHE_ENABLE) 10248 mode_caching_page->wce = 1; 10249 else if (wce_flag == SD_CACHE_DISABLE) 10250 mode_caching_page->wce = 0; 10251 10252 /* 10253 * Save the page if the mode sense says the 10254 * drive supports it. 10255 */ 10256 save_pg = mode_caching_page->mode_page.ps ? 10257 SD_SAVE_PAGE : SD_DONTSAVE_PAGE; 10258 10259 /* Clear reserved bits before mode select. */ 10260 mode_caching_page->mode_page.ps = 0; 10261 10262 /* 10263 * Clear out mode header for mode select. 10264 * The rest of the retrieved page will be reused. 10265 */ 10266 bzero(header, hdrlen); 10267 10268 if (un->un_f_cfg_is_atapi == TRUE) { 10269 mhp = (struct mode_header_grp2 *)header; 10270 mhp->bdesc_length_hi = bd_len >> 8; 10271 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff; 10272 } else { 10273 ((struct mode_header *)header)->bdesc_length = bd_len; 10274 } 10275 10276 /* Issue mode select to change the cache settings */ 10277 if (un->un_f_cfg_is_atapi == TRUE) { 10278 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header, 10279 sbuflen, save_pg, SD_PATH_DIRECT); 10280 } else { 10281 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header, 10282 sbuflen, save_pg, SD_PATH_DIRECT); 10283 } 10284 } 10285 10286 kmem_free(header, buflen); 10287 return (rval); 10288 } 10289 10290 10291 /* 10292 * Function: sd_get_write_cache_enabled() 10293 * 10294 * Description: This routine is the driver entry point for determining if 10295 * write caching is enabled. It examines the WCE (write cache 10296 * enable) bits of mode page 8 (MODEPAGE_CACHING). 10297 * 10298 * Arguments: un - driver soft state (unit) structure 10299 * is_enabled - pointer to int where write cache enabled state 10300 * is returned (non-zero -> write cache enabled) 10301 * 10302 * 10303 * Return Code: EIO 10304 * code returned by sd_send_scsi_MODE_SENSE 10305 * 10306 * Context: Kernel Thread 10307 * 10308 * NOTE: If ioctl is added to disable write cache, this sequence should 10309 * be followed so that no locking is required for accesses to 10310 * un->un_f_write_cache_enabled: 10311 * do mode select to clear wce 10312 * do synchronize cache to flush cache 10313 * set un->un_f_write_cache_enabled = FALSE 10314 * 10315 * Conversely, an ioctl to enable the write cache should be done 10316 * in this order: 10317 * set un->un_f_write_cache_enabled = TRUE 10318 * do mode select to set wce 10319 */ 10320 10321 static int 10322 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled) 10323 { 10324 struct mode_caching *mode_caching_page; 10325 uchar_t *header; 10326 size_t buflen; 10327 int hdrlen; 10328 int bd_len; 10329 int rval = 0; 10330 10331 ASSERT(un != NULL); 10332 ASSERT(is_enabled != NULL); 10333 10334 /* in case of error, flag as enabled */ 10335 *is_enabled = TRUE; 10336 10337 /* 10338 * Do a test unit ready, otherwise a mode sense may not work if this 10339 * is the first command sent to the device after boot. 10340 */ 10341 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 10342 10343 if (un->un_f_cfg_is_atapi == TRUE) { 10344 hdrlen = MODE_HEADER_LENGTH_GRP2; 10345 } else { 10346 hdrlen = MODE_HEADER_LENGTH; 10347 } 10348 10349 /* 10350 * Allocate memory for the retrieved mode page and its headers. Set 10351 * a pointer to the page itself. 10352 */ 10353 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching); 10354 header = kmem_zalloc(buflen, KM_SLEEP); 10355 10356 /* Get the information from the device. */ 10357 if (un->un_f_cfg_is_atapi == TRUE) { 10358 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen, 10359 MODEPAGE_CACHING, SD_PATH_DIRECT); 10360 } else { 10361 rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen, 10362 MODEPAGE_CACHING, SD_PATH_DIRECT); 10363 } 10364 if (rval != 0) { 10365 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 10366 "sd_get_write_cache_enabled: Mode Sense Failed\n"); 10367 kmem_free(header, buflen); 10368 return (rval); 10369 } 10370 10371 /* 10372 * Determine size of Block Descriptors in order to locate 10373 * the mode page data. ATAPI devices return 0, SCSI devices 10374 * should return MODE_BLK_DESC_LENGTH. 10375 */ 10376 if (un->un_f_cfg_is_atapi == TRUE) { 10377 struct mode_header_grp2 *mhp; 10378 mhp = (struct mode_header_grp2 *)header; 10379 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo; 10380 } else { 10381 bd_len = ((struct mode_header *)header)->bdesc_length; 10382 } 10383 10384 if (bd_len > MODE_BLK_DESC_LENGTH) { 10385 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 10386 "sd_get_write_cache_enabled: Mode Sense returned invalid " 10387 "block descriptor length\n"); 10388 kmem_free(header, buflen); 10389 return (EIO); 10390 } 10391 10392 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len); 10393 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) { 10394 SD_ERROR(SD_LOG_COMMON, un, "sd_cache_control: Mode Sense" 10395 " caching page code mismatch %d\n", 10396 mode_caching_page->mode_page.code); 10397 kmem_free(header, buflen); 10398 return (EIO); 10399 } 10400 *is_enabled = mode_caching_page->wce; 10401 10402 kmem_free(header, buflen); 10403 return (0); 10404 } 10405 10406 10407 /* 10408 * Function: sd_make_device 10409 * 10410 * Description: Utility routine to return the Solaris device number from 10411 * the data in the device's dev_info structure. 10412 * 10413 * Return Code: The Solaris device number 10414 * 10415 * Context: Any 10416 */ 10417 10418 static dev_t 10419 sd_make_device(dev_info_t *devi) 10420 { 10421 return (makedevice(ddi_name_to_major(ddi_get_name(devi)), 10422 ddi_get_instance(devi) << SDUNIT_SHIFT)); 10423 } 10424 10425 10426 /* 10427 * Function: sd_pm_entry 10428 * 10429 * Description: Called at the start of a new command to manage power 10430 * and busy status of a device. This includes determining whether 10431 * the current power state of the device is sufficient for 10432 * performing the command or whether it must be changed. 10433 * The PM framework is notified appropriately. 10434 * Only with a return status of DDI_SUCCESS will the 10435 * component be busy to the framework. 10436 * 10437 * All callers of sd_pm_entry must check the return status 10438 * and only call sd_pm_exit it it was DDI_SUCCESS. A status 10439 * of DDI_FAILURE indicates the device failed to power up. 10440 * In this case un_pm_count has been adjusted so the result 10441 * on exit is still powered down, ie. count is less than 0. 10442 * Calling sd_pm_exit with this count value hits an ASSERT. 10443 * 10444 * Return Code: DDI_SUCCESS or DDI_FAILURE 10445 * 10446 * Context: Kernel thread context. 10447 */ 10448 10449 static int 10450 sd_pm_entry(struct sd_lun *un) 10451 { 10452 int return_status = DDI_SUCCESS; 10453 10454 ASSERT(!mutex_owned(SD_MUTEX(un))); 10455 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10456 10457 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n"); 10458 10459 if (un->un_f_pm_is_enabled == FALSE) { 10460 SD_TRACE(SD_LOG_IO_PM, un, 10461 "sd_pm_entry: exiting, PM not enabled\n"); 10462 return (return_status); 10463 } 10464 10465 /* 10466 * Just increment a counter if PM is enabled. On the transition from 10467 * 0 ==> 1, mark the device as busy. The iodone side will decrement 10468 * the count with each IO and mark the device as idle when the count 10469 * hits 0. 10470 * 10471 * If the count is less than 0 the device is powered down. If a powered 10472 * down device is successfully powered up then the count must be 10473 * incremented to reflect the power up. Note that it'll get incremented 10474 * a second time to become busy. 10475 * 10476 * Because the following has the potential to change the device state 10477 * and must release the un_pm_mutex to do so, only one thread can be 10478 * allowed through at a time. 10479 */ 10480 10481 mutex_enter(&un->un_pm_mutex); 10482 while (un->un_pm_busy == TRUE) { 10483 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex); 10484 } 10485 un->un_pm_busy = TRUE; 10486 10487 if (un->un_pm_count < 1) { 10488 10489 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n"); 10490 10491 /* 10492 * Indicate we are now busy so the framework won't attempt to 10493 * power down the device. This call will only fail if either 10494 * we passed a bad component number or the device has no 10495 * components. Neither of these should ever happen. 10496 */ 10497 mutex_exit(&un->un_pm_mutex); 10498 return_status = pm_busy_component(SD_DEVINFO(un), 0); 10499 ASSERT(return_status == DDI_SUCCESS); 10500 10501 mutex_enter(&un->un_pm_mutex); 10502 10503 if (un->un_pm_count < 0) { 10504 mutex_exit(&un->un_pm_mutex); 10505 10506 SD_TRACE(SD_LOG_IO_PM, un, 10507 "sd_pm_entry: power up component\n"); 10508 10509 /* 10510 * pm_raise_power will cause sdpower to be called 10511 * which brings the device power level to the 10512 * desired state, ON in this case. If successful, 10513 * un_pm_count and un_power_level will be updated 10514 * appropriately. 10515 */ 10516 return_status = pm_raise_power(SD_DEVINFO(un), 0, 10517 SD_SPINDLE_ON); 10518 10519 mutex_enter(&un->un_pm_mutex); 10520 10521 if (return_status != DDI_SUCCESS) { 10522 /* 10523 * Power up failed. 10524 * Idle the device and adjust the count 10525 * so the result on exit is that we're 10526 * still powered down, ie. count is less than 0. 10527 */ 10528 SD_TRACE(SD_LOG_IO_PM, un, 10529 "sd_pm_entry: power up failed," 10530 " idle the component\n"); 10531 10532 (void) pm_idle_component(SD_DEVINFO(un), 0); 10533 un->un_pm_count--; 10534 } else { 10535 /* 10536 * Device is powered up, verify the 10537 * count is non-negative. 10538 * This is debug only. 10539 */ 10540 ASSERT(un->un_pm_count == 0); 10541 } 10542 } 10543 10544 if (return_status == DDI_SUCCESS) { 10545 /* 10546 * For performance, now that the device has been tagged 10547 * as busy, and it's known to be powered up, update the 10548 * chain types to use jump tables that do not include 10549 * pm. This significantly lowers the overhead and 10550 * therefore improves performance. 10551 */ 10552 10553 mutex_exit(&un->un_pm_mutex); 10554 mutex_enter(SD_MUTEX(un)); 10555 SD_TRACE(SD_LOG_IO_PM, un, 10556 "sd_pm_entry: changing uscsi_chain_type from %d\n", 10557 un->un_uscsi_chain_type); 10558 10559 if (un->un_f_non_devbsize_supported) { 10560 un->un_buf_chain_type = 10561 SD_CHAIN_INFO_RMMEDIA_NO_PM; 10562 } else { 10563 un->un_buf_chain_type = 10564 SD_CHAIN_INFO_DISK_NO_PM; 10565 } 10566 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM; 10567 10568 SD_TRACE(SD_LOG_IO_PM, un, 10569 " changed uscsi_chain_type to %d\n", 10570 un->un_uscsi_chain_type); 10571 mutex_exit(SD_MUTEX(un)); 10572 mutex_enter(&un->un_pm_mutex); 10573 10574 if (un->un_pm_idle_timeid == NULL) { 10575 /* 300 ms. */ 10576 un->un_pm_idle_timeid = 10577 timeout(sd_pm_idletimeout_handler, un, 10578 (drv_usectohz((clock_t)300000))); 10579 /* 10580 * Include an extra call to busy which keeps the 10581 * device busy with-respect-to the PM layer 10582 * until the timer fires, at which time it'll 10583 * get the extra idle call. 10584 */ 10585 (void) pm_busy_component(SD_DEVINFO(un), 0); 10586 } 10587 } 10588 } 10589 un->un_pm_busy = FALSE; 10590 /* Next... */ 10591 cv_signal(&un->un_pm_busy_cv); 10592 10593 un->un_pm_count++; 10594 10595 SD_TRACE(SD_LOG_IO_PM, un, 10596 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count); 10597 10598 mutex_exit(&un->un_pm_mutex); 10599 10600 return (return_status); 10601 } 10602 10603 10604 /* 10605 * Function: sd_pm_exit 10606 * 10607 * Description: Called at the completion of a command to manage busy 10608 * status for the device. If the device becomes idle the 10609 * PM framework is notified. 10610 * 10611 * Context: Kernel thread context 10612 */ 10613 10614 static void 10615 sd_pm_exit(struct sd_lun *un) 10616 { 10617 ASSERT(!mutex_owned(SD_MUTEX(un))); 10618 ASSERT(!mutex_owned(&un->un_pm_mutex)); 10619 10620 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n"); 10621 10622 /* 10623 * After attach the following flag is only read, so don't 10624 * take the penalty of acquiring a mutex for it. 10625 */ 10626 if (un->un_f_pm_is_enabled == TRUE) { 10627 10628 mutex_enter(&un->un_pm_mutex); 10629 un->un_pm_count--; 10630 10631 SD_TRACE(SD_LOG_IO_PM, un, 10632 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count); 10633 10634 ASSERT(un->un_pm_count >= 0); 10635 if (un->un_pm_count == 0) { 10636 mutex_exit(&un->un_pm_mutex); 10637 10638 SD_TRACE(SD_LOG_IO_PM, un, 10639 "sd_pm_exit: idle component\n"); 10640 10641 (void) pm_idle_component(SD_DEVINFO(un), 0); 10642 10643 } else { 10644 mutex_exit(&un->un_pm_mutex); 10645 } 10646 } 10647 10648 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n"); 10649 } 10650 10651 10652 /* 10653 * Function: sdopen 10654 * 10655 * Description: Driver's open(9e) entry point function. 10656 * 10657 * Arguments: dev_i - pointer to device number 10658 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE) 10659 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10660 * cred_p - user credential pointer 10661 * 10662 * Return Code: EINVAL 10663 * ENXIO 10664 * EIO 10665 * EROFS 10666 * EBUSY 10667 * 10668 * Context: Kernel thread context 10669 */ 10670 /* ARGSUSED */ 10671 static int 10672 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 10673 { 10674 struct sd_lun *un; 10675 int nodelay; 10676 int part; 10677 uint64_t partmask; 10678 int instance; 10679 dev_t dev; 10680 int rval = EIO; 10681 10682 /* Validate the open type */ 10683 if (otyp >= OTYPCNT) { 10684 return (EINVAL); 10685 } 10686 10687 dev = *dev_p; 10688 instance = SDUNIT(dev); 10689 mutex_enter(&sd_detach_mutex); 10690 10691 /* 10692 * Fail the open if there is no softstate for the instance, or 10693 * if another thread somewhere is trying to detach the instance. 10694 */ 10695 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 10696 (un->un_detach_count != 0)) { 10697 mutex_exit(&sd_detach_mutex); 10698 /* 10699 * The probe cache only needs to be cleared when open (9e) fails 10700 * with ENXIO (4238046). 10701 */ 10702 /* 10703 * un-conditionally clearing probe cache is ok with 10704 * separate sd/ssd binaries 10705 * x86 platform can be an issue with both parallel 10706 * and fibre in 1 binary 10707 */ 10708 sd_scsi_clear_probe_cache(); 10709 return (ENXIO); 10710 } 10711 10712 /* 10713 * The un_layer_count is to prevent another thread in specfs from 10714 * trying to detach the instance, which can happen when we are 10715 * called from a higher-layer driver instead of thru specfs. 10716 * This will not be needed when DDI provides a layered driver 10717 * interface that allows specfs to know that an instance is in 10718 * use by a layered driver & should not be detached. 10719 * 10720 * Note: the semantics for layered driver opens are exactly one 10721 * close for every open. 10722 */ 10723 if (otyp == OTYP_LYR) { 10724 un->un_layer_count++; 10725 } 10726 10727 /* 10728 * Keep a count of the current # of opens in progress. This is because 10729 * some layered drivers try to call us as a regular open. This can 10730 * cause problems that we cannot prevent, however by keeping this count 10731 * we can at least keep our open and detach routines from racing against 10732 * each other under such conditions. 10733 */ 10734 un->un_opens_in_progress++; 10735 mutex_exit(&sd_detach_mutex); 10736 10737 nodelay = (flag & (FNDELAY | FNONBLOCK)); 10738 part = SDPART(dev); 10739 partmask = 1 << part; 10740 10741 /* 10742 * We use a semaphore here in order to serialize 10743 * open and close requests on the device. 10744 */ 10745 sema_p(&un->un_semoclose); 10746 10747 mutex_enter(SD_MUTEX(un)); 10748 10749 /* 10750 * All device accesses go thru sdstrategy() where we check 10751 * on suspend status but there could be a scsi_poll command, 10752 * which bypasses sdstrategy(), so we need to check pm 10753 * status. 10754 */ 10755 10756 if (!nodelay) { 10757 while ((un->un_state == SD_STATE_SUSPENDED) || 10758 (un->un_state == SD_STATE_PM_CHANGING)) { 10759 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10760 } 10761 10762 mutex_exit(SD_MUTEX(un)); 10763 if (sd_pm_entry(un) != DDI_SUCCESS) { 10764 rval = EIO; 10765 SD_ERROR(SD_LOG_OPEN_CLOSE, un, 10766 "sdopen: sd_pm_entry failed\n"); 10767 goto open_failed_with_pm; 10768 } 10769 mutex_enter(SD_MUTEX(un)); 10770 } 10771 10772 /* check for previous exclusive open */ 10773 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un); 10774 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10775 "sdopen: exclopen=%x, flag=%x, regopen=%x\n", 10776 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]); 10777 10778 if (un->un_exclopen & (partmask)) { 10779 goto excl_open_fail; 10780 } 10781 10782 if (flag & FEXCL) { 10783 int i; 10784 if (un->un_ocmap.lyropen[part]) { 10785 goto excl_open_fail; 10786 } 10787 for (i = 0; i < (OTYPCNT - 1); i++) { 10788 if (un->un_ocmap.regopen[i] & (partmask)) { 10789 goto excl_open_fail; 10790 } 10791 } 10792 } 10793 10794 /* 10795 * Check the write permission if this is a removable media device, 10796 * NDELAY has not been set, and writable permission is requested. 10797 * 10798 * Note: If NDELAY was set and this is write-protected media the WRITE 10799 * attempt will fail with EIO as part of the I/O processing. This is a 10800 * more permissive implementation that allows the open to succeed and 10801 * WRITE attempts to fail when appropriate. 10802 */ 10803 if (un->un_f_chk_wp_open) { 10804 if ((flag & FWRITE) && (!nodelay)) { 10805 mutex_exit(SD_MUTEX(un)); 10806 /* 10807 * Defer the check for write permission on writable 10808 * DVD drive till sdstrategy and will not fail open even 10809 * if FWRITE is set as the device can be writable 10810 * depending upon the media and the media can change 10811 * after the call to open(). 10812 */ 10813 if (un->un_f_dvdram_writable_device == FALSE) { 10814 if (ISCD(un) || sr_check_wp(dev)) { 10815 rval = EROFS; 10816 mutex_enter(SD_MUTEX(un)); 10817 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10818 "write to cd or write protected media\n"); 10819 goto open_fail; 10820 } 10821 } 10822 mutex_enter(SD_MUTEX(un)); 10823 } 10824 } 10825 10826 /* 10827 * If opening in NDELAY/NONBLOCK mode, just return. 10828 * Check if disk is ready and has a valid geometry later. 10829 */ 10830 if (!nodelay) { 10831 mutex_exit(SD_MUTEX(un)); 10832 rval = sd_ready_and_valid(un); 10833 mutex_enter(SD_MUTEX(un)); 10834 /* 10835 * Fail if device is not ready or if the number of disk 10836 * blocks is zero or negative for non CD devices. 10837 */ 10838 if ((rval != SD_READY_VALID) || 10839 (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) { 10840 rval = un->un_f_has_removable_media ? ENXIO : EIO; 10841 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10842 "device not ready or invalid disk block value\n"); 10843 goto open_fail; 10844 } 10845 #if defined(__i386) || defined(__amd64) 10846 } else { 10847 uchar_t *cp; 10848 /* 10849 * x86 requires special nodelay handling, so that p0 is 10850 * always defined and accessible. 10851 * Invalidate geometry only if device is not already open. 10852 */ 10853 cp = &un->un_ocmap.chkd[0]; 10854 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10855 if (*cp != (uchar_t)0) { 10856 break; 10857 } 10858 cp++; 10859 } 10860 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10861 un->un_f_geometry_is_valid = FALSE; 10862 } 10863 10864 #endif 10865 } 10866 10867 if (otyp == OTYP_LYR) { 10868 un->un_ocmap.lyropen[part]++; 10869 } else { 10870 un->un_ocmap.regopen[otyp] |= partmask; 10871 } 10872 10873 /* Set up open and exclusive open flags */ 10874 if (flag & FEXCL) { 10875 un->un_exclopen |= (partmask); 10876 } 10877 10878 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: " 10879 "open of part %d type %d\n", part, otyp); 10880 10881 mutex_exit(SD_MUTEX(un)); 10882 if (!nodelay) { 10883 sd_pm_exit(un); 10884 } 10885 10886 sema_v(&un->un_semoclose); 10887 10888 mutex_enter(&sd_detach_mutex); 10889 un->un_opens_in_progress--; 10890 mutex_exit(&sd_detach_mutex); 10891 10892 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n"); 10893 return (DDI_SUCCESS); 10894 10895 excl_open_fail: 10896 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n"); 10897 rval = EBUSY; 10898 10899 open_fail: 10900 mutex_exit(SD_MUTEX(un)); 10901 10902 /* 10903 * On a failed open we must exit the pm management. 10904 */ 10905 if (!nodelay) { 10906 sd_pm_exit(un); 10907 } 10908 open_failed_with_pm: 10909 sema_v(&un->un_semoclose); 10910 10911 mutex_enter(&sd_detach_mutex); 10912 un->un_opens_in_progress--; 10913 if (otyp == OTYP_LYR) { 10914 un->un_layer_count--; 10915 } 10916 mutex_exit(&sd_detach_mutex); 10917 10918 return (rval); 10919 } 10920 10921 10922 /* 10923 * Function: sdclose 10924 * 10925 * Description: Driver's close(9e) entry point function. 10926 * 10927 * Arguments: dev - device number 10928 * flag - file status flag, informational only 10929 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR) 10930 * cred_p - user credential pointer 10931 * 10932 * Return Code: ENXIO 10933 * 10934 * Context: Kernel thread context 10935 */ 10936 /* ARGSUSED */ 10937 static int 10938 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p) 10939 { 10940 struct sd_lun *un; 10941 uchar_t *cp; 10942 int part; 10943 int nodelay; 10944 int rval = 0; 10945 10946 /* Validate the open type */ 10947 if (otyp >= OTYPCNT) { 10948 return (ENXIO); 10949 } 10950 10951 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 10952 return (ENXIO); 10953 } 10954 10955 part = SDPART(dev); 10956 nodelay = flag & (FNDELAY | FNONBLOCK); 10957 10958 SD_TRACE(SD_LOG_OPEN_CLOSE, un, 10959 "sdclose: close of part %d type %d\n", part, otyp); 10960 10961 /* 10962 * We use a semaphore here in order to serialize 10963 * open and close requests on the device. 10964 */ 10965 sema_p(&un->un_semoclose); 10966 10967 mutex_enter(SD_MUTEX(un)); 10968 10969 /* Don't proceed if power is being changed. */ 10970 while (un->un_state == SD_STATE_PM_CHANGING) { 10971 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 10972 } 10973 10974 if (un->un_exclopen & (1 << part)) { 10975 un->un_exclopen &= ~(1 << part); 10976 } 10977 10978 /* Update the open partition map */ 10979 if (otyp == OTYP_LYR) { 10980 un->un_ocmap.lyropen[part] -= 1; 10981 } else { 10982 un->un_ocmap.regopen[otyp] &= ~(1 << part); 10983 } 10984 10985 cp = &un->un_ocmap.chkd[0]; 10986 while (cp < &un->un_ocmap.chkd[OCSIZE]) { 10987 if (*cp != NULL) { 10988 break; 10989 } 10990 cp++; 10991 } 10992 10993 if (cp == &un->un_ocmap.chkd[OCSIZE]) { 10994 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n"); 10995 10996 /* 10997 * We avoid persistance upon the last close, and set 10998 * the throttle back to the maximum. 10999 */ 11000 un->un_throttle = un->un_saved_throttle; 11001 11002 if (un->un_state == SD_STATE_OFFLINE) { 11003 if (un->un_f_is_fibre == FALSE) { 11004 scsi_log(SD_DEVINFO(un), sd_label, 11005 CE_WARN, "offline\n"); 11006 } 11007 un->un_f_geometry_is_valid = FALSE; 11008 11009 } else { 11010 /* 11011 * Flush any outstanding writes in NVRAM cache. 11012 * Note: SYNCHRONIZE CACHE is an optional SCSI-2 11013 * cmd, it may not work for non-Pluto devices. 11014 * SYNCHRONIZE CACHE is not required for removables, 11015 * except DVD-RAM drives. 11016 * 11017 * Also note: because SYNCHRONIZE CACHE is currently 11018 * the only command issued here that requires the 11019 * drive be powered up, only do the power up before 11020 * sending the Sync Cache command. If additional 11021 * commands are added which require a powered up 11022 * drive, the following sequence may have to change. 11023 * 11024 * And finally, note that parallel SCSI on SPARC 11025 * only issues a Sync Cache to DVD-RAM, a newly 11026 * supported device. 11027 */ 11028 #if defined(__i386) || defined(__amd64) 11029 if (un->un_f_sync_cache_supported || 11030 un->un_f_dvdram_writable_device == TRUE) { 11031 #else 11032 if (un->un_f_dvdram_writable_device == TRUE) { 11033 #endif 11034 mutex_exit(SD_MUTEX(un)); 11035 if (sd_pm_entry(un) == DDI_SUCCESS) { 11036 rval = 11037 sd_send_scsi_SYNCHRONIZE_CACHE(un, 11038 NULL); 11039 /* ignore error if not supported */ 11040 if (rval == ENOTSUP) { 11041 rval = 0; 11042 } else if (rval != 0) { 11043 rval = EIO; 11044 } 11045 sd_pm_exit(un); 11046 } else { 11047 rval = EIO; 11048 } 11049 mutex_enter(SD_MUTEX(un)); 11050 } 11051 11052 /* 11053 * For devices which supports DOOR_LOCK, send an ALLOW 11054 * MEDIA REMOVAL command, but don't get upset if it 11055 * fails. We need to raise the power of the drive before 11056 * we can call sd_send_scsi_DOORLOCK() 11057 */ 11058 if (un->un_f_doorlock_supported) { 11059 mutex_exit(SD_MUTEX(un)); 11060 if (sd_pm_entry(un) == DDI_SUCCESS) { 11061 rval = sd_send_scsi_DOORLOCK(un, 11062 SD_REMOVAL_ALLOW, SD_PATH_DIRECT); 11063 11064 sd_pm_exit(un); 11065 if (ISCD(un) && (rval != 0) && 11066 (nodelay != 0)) { 11067 rval = ENXIO; 11068 } 11069 } else { 11070 rval = EIO; 11071 } 11072 mutex_enter(SD_MUTEX(un)); 11073 } 11074 11075 /* 11076 * If a device has removable media, invalidate all 11077 * parameters related to media, such as geometry, 11078 * blocksize, and blockcount. 11079 */ 11080 if (un->un_f_has_removable_media) { 11081 sr_ejected(un); 11082 } 11083 11084 /* 11085 * Destroy the cache (if it exists) which was 11086 * allocated for the write maps since this is 11087 * the last close for this media. 11088 */ 11089 if (un->un_wm_cache) { 11090 /* 11091 * Check if there are pending commands. 11092 * and if there are give a warning and 11093 * do not destroy the cache. 11094 */ 11095 if (un->un_ncmds_in_driver > 0) { 11096 scsi_log(SD_DEVINFO(un), 11097 sd_label, CE_WARN, 11098 "Unable to clean up memory " 11099 "because of pending I/O\n"); 11100 } else { 11101 kmem_cache_destroy( 11102 un->un_wm_cache); 11103 un->un_wm_cache = NULL; 11104 } 11105 } 11106 } 11107 } 11108 11109 mutex_exit(SD_MUTEX(un)); 11110 sema_v(&un->un_semoclose); 11111 11112 if (otyp == OTYP_LYR) { 11113 mutex_enter(&sd_detach_mutex); 11114 /* 11115 * The detach routine may run when the layer count 11116 * drops to zero. 11117 */ 11118 un->un_layer_count--; 11119 mutex_exit(&sd_detach_mutex); 11120 } 11121 11122 return (rval); 11123 } 11124 11125 11126 /* 11127 * Function: sd_ready_and_valid 11128 * 11129 * Description: Test if device is ready and has a valid geometry. 11130 * 11131 * Arguments: dev - device number 11132 * un - driver soft state (unit) structure 11133 * 11134 * Return Code: SD_READY_VALID ready and valid label 11135 * SD_READY_NOT_VALID ready, geom ops never applicable 11136 * SD_NOT_READY_VALID not ready, no label 11137 * SD_RESERVED_BY_OTHERS reservation conflict 11138 * 11139 * Context: Never called at interrupt context. 11140 */ 11141 11142 static int 11143 sd_ready_and_valid(struct sd_lun *un) 11144 { 11145 struct sd_errstats *stp; 11146 uint64_t capacity; 11147 uint_t lbasize; 11148 int rval = SD_READY_VALID; 11149 char name_str[48]; 11150 11151 ASSERT(un != NULL); 11152 ASSERT(!mutex_owned(SD_MUTEX(un))); 11153 11154 mutex_enter(SD_MUTEX(un)); 11155 /* 11156 * If a device has removable media, we must check if media is 11157 * ready when checking if this device is ready and valid. 11158 */ 11159 if (un->un_f_has_removable_media) { 11160 mutex_exit(SD_MUTEX(un)); 11161 if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) { 11162 rval = SD_NOT_READY_VALID; 11163 mutex_enter(SD_MUTEX(un)); 11164 goto done; 11165 } 11166 11167 mutex_enter(SD_MUTEX(un)); 11168 if ((un->un_f_geometry_is_valid == FALSE) || 11169 (un->un_f_blockcount_is_valid == FALSE) || 11170 (un->un_f_tgt_blocksize_is_valid == FALSE)) { 11171 11172 /* capacity has to be read every open. */ 11173 mutex_exit(SD_MUTEX(un)); 11174 if (sd_send_scsi_READ_CAPACITY(un, &capacity, 11175 &lbasize, SD_PATH_DIRECT) != 0) { 11176 mutex_enter(SD_MUTEX(un)); 11177 un->un_f_geometry_is_valid = FALSE; 11178 rval = SD_NOT_READY_VALID; 11179 goto done; 11180 } else { 11181 mutex_enter(SD_MUTEX(un)); 11182 sd_update_block_info(un, lbasize, capacity); 11183 } 11184 } 11185 11186 /* 11187 * Check if the media in the device is writable or not. 11188 */ 11189 if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) { 11190 sd_check_for_writable_cd(un); 11191 } 11192 11193 } else { 11194 /* 11195 * Do a test unit ready to clear any unit attention from non-cd 11196 * devices. 11197 */ 11198 mutex_exit(SD_MUTEX(un)); 11199 (void) sd_send_scsi_TEST_UNIT_READY(un, 0); 11200 mutex_enter(SD_MUTEX(un)); 11201 } 11202 11203 11204 /* 11205 * If this is a non 512 block device, allocate space for 11206 * the wmap cache. This is being done here since every time 11207 * a media is changed this routine will be called and the 11208 * block size is a function of media rather than device. 11209 */ 11210 if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) { 11211 if (!(un->un_wm_cache)) { 11212 (void) snprintf(name_str, sizeof (name_str), 11213 "%s%d_cache", 11214 ddi_driver_name(SD_DEVINFO(un)), 11215 ddi_get_instance(SD_DEVINFO(un))); 11216 un->un_wm_cache = kmem_cache_create( 11217 name_str, sizeof (struct sd_w_map), 11218 8, sd_wm_cache_constructor, 11219 sd_wm_cache_destructor, NULL, 11220 (void *)un, NULL, 0); 11221 if (!(un->un_wm_cache)) { 11222 rval = ENOMEM; 11223 goto done; 11224 } 11225 } 11226 } 11227 11228 if (un->un_state == SD_STATE_NORMAL) { 11229 /* 11230 * If the target is not yet ready here (defined by a TUR 11231 * failure), invalidate the geometry and print an 'offline' 11232 * message. This is a legacy message, as the state of the 11233 * target is not actually changed to SD_STATE_OFFLINE. 11234 * 11235 * If the TUR fails for EACCES (Reservation Conflict), 11236 * SD_RESERVED_BY_OTHERS will be returned to indicate 11237 * reservation conflict. If the TUR fails for other 11238 * reasons, SD_NOT_READY_VALID will be returned. 11239 */ 11240 int err; 11241 11242 mutex_exit(SD_MUTEX(un)); 11243 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 11244 mutex_enter(SD_MUTEX(un)); 11245 11246 if (err != 0) { 11247 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 11248 "offline or reservation conflict\n"); 11249 un->un_f_geometry_is_valid = FALSE; 11250 if (err == EACCES) { 11251 rval = SD_RESERVED_BY_OTHERS; 11252 } else { 11253 rval = SD_NOT_READY_VALID; 11254 } 11255 goto done; 11256 } 11257 } 11258 11259 if (un->un_f_format_in_progress == FALSE) { 11260 /* 11261 * Note: sd_validate_geometry may return TRUE, but that does 11262 * not necessarily mean un_f_geometry_is_valid == TRUE! 11263 */ 11264 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 11265 if (rval == ENOTSUP) { 11266 if (un->un_f_geometry_is_valid == TRUE) 11267 rval = 0; 11268 else { 11269 rval = SD_READY_NOT_VALID; 11270 goto done; 11271 } 11272 } 11273 if (rval != 0) { 11274 /* 11275 * We don't check the validity of geometry for 11276 * CDROMs. Also we assume we have a good label 11277 * even if sd_validate_geometry returned ENOMEM. 11278 */ 11279 if (!ISCD(un) && rval != ENOMEM) { 11280 rval = SD_NOT_READY_VALID; 11281 goto done; 11282 } 11283 } 11284 } 11285 11286 /* 11287 * If this device supports DOOR_LOCK command, try and send 11288 * this command to PREVENT MEDIA REMOVAL, but don't get upset 11289 * if it fails. For a CD, however, it is an error 11290 */ 11291 if (un->un_f_doorlock_supported) { 11292 mutex_exit(SD_MUTEX(un)); 11293 if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 11294 SD_PATH_DIRECT) != 0) && ISCD(un)) { 11295 rval = SD_NOT_READY_VALID; 11296 mutex_enter(SD_MUTEX(un)); 11297 goto done; 11298 } 11299 mutex_enter(SD_MUTEX(un)); 11300 } 11301 11302 /* The state has changed, inform the media watch routines */ 11303 un->un_mediastate = DKIO_INSERTED; 11304 cv_broadcast(&un->un_state_cv); 11305 rval = SD_READY_VALID; 11306 11307 done: 11308 11309 /* 11310 * Initialize the capacity kstat value, if no media previously 11311 * (capacity kstat is 0) and a media has been inserted 11312 * (un_blockcount > 0). 11313 */ 11314 if (un->un_errstats != NULL) { 11315 stp = (struct sd_errstats *)un->un_errstats->ks_data; 11316 if ((stp->sd_capacity.value.ui64 == 0) && 11317 (un->un_f_blockcount_is_valid == TRUE)) { 11318 stp->sd_capacity.value.ui64 = 11319 (uint64_t)((uint64_t)un->un_blockcount * 11320 un->un_sys_blocksize); 11321 } 11322 } 11323 11324 mutex_exit(SD_MUTEX(un)); 11325 return (rval); 11326 } 11327 11328 11329 /* 11330 * Function: sdmin 11331 * 11332 * Description: Routine to limit the size of a data transfer. Used in 11333 * conjunction with physio(9F). 11334 * 11335 * Arguments: bp - pointer to the indicated buf(9S) struct. 11336 * 11337 * Context: Kernel thread context. 11338 */ 11339 11340 static void 11341 sdmin(struct buf *bp) 11342 { 11343 struct sd_lun *un; 11344 int instance; 11345 11346 instance = SDUNIT(bp->b_edev); 11347 11348 un = ddi_get_soft_state(sd_state, instance); 11349 ASSERT(un != NULL); 11350 11351 if (bp->b_bcount > un->un_max_xfer_size) { 11352 bp->b_bcount = un->un_max_xfer_size; 11353 } 11354 } 11355 11356 11357 /* 11358 * Function: sdread 11359 * 11360 * Description: Driver's read(9e) entry point function. 11361 * 11362 * Arguments: dev - device number 11363 * uio - structure pointer describing where data is to be stored 11364 * in user's space 11365 * cred_p - user credential pointer 11366 * 11367 * Return Code: ENXIO 11368 * EIO 11369 * EINVAL 11370 * value returned by physio 11371 * 11372 * Context: Kernel thread context. 11373 */ 11374 /* ARGSUSED */ 11375 static int 11376 sdread(dev_t dev, struct uio *uio, cred_t *cred_p) 11377 { 11378 struct sd_lun *un = NULL; 11379 int secmask; 11380 int err; 11381 11382 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11383 return (ENXIO); 11384 } 11385 11386 ASSERT(!mutex_owned(SD_MUTEX(un))); 11387 11388 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11389 mutex_enter(SD_MUTEX(un)); 11390 /* 11391 * Because the call to sd_ready_and_valid will issue I/O we 11392 * must wait here if either the device is suspended or 11393 * if it's power level is changing. 11394 */ 11395 while ((un->un_state == SD_STATE_SUSPENDED) || 11396 (un->un_state == SD_STATE_PM_CHANGING)) { 11397 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11398 } 11399 un->un_ncmds_in_driver++; 11400 mutex_exit(SD_MUTEX(un)); 11401 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11402 mutex_enter(SD_MUTEX(un)); 11403 un->un_ncmds_in_driver--; 11404 ASSERT(un->un_ncmds_in_driver >= 0); 11405 mutex_exit(SD_MUTEX(un)); 11406 return (EIO); 11407 } 11408 mutex_enter(SD_MUTEX(un)); 11409 un->un_ncmds_in_driver--; 11410 ASSERT(un->un_ncmds_in_driver >= 0); 11411 mutex_exit(SD_MUTEX(un)); 11412 } 11413 11414 /* 11415 * Read requests are restricted to multiples of the system block size. 11416 */ 11417 secmask = un->un_sys_blocksize - 1; 11418 11419 if (uio->uio_loffset & ((offset_t)(secmask))) { 11420 SD_ERROR(SD_LOG_READ_WRITE, un, 11421 "sdread: file offset not modulo %d\n", 11422 un->un_sys_blocksize); 11423 err = EINVAL; 11424 } else if (uio->uio_iov->iov_len & (secmask)) { 11425 SD_ERROR(SD_LOG_READ_WRITE, un, 11426 "sdread: transfer length not modulo %d\n", 11427 un->un_sys_blocksize); 11428 err = EINVAL; 11429 } else { 11430 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio); 11431 } 11432 return (err); 11433 } 11434 11435 11436 /* 11437 * Function: sdwrite 11438 * 11439 * Description: Driver's write(9e) entry point function. 11440 * 11441 * Arguments: dev - device number 11442 * uio - structure pointer describing where data is stored in 11443 * user's space 11444 * cred_p - user credential pointer 11445 * 11446 * Return Code: ENXIO 11447 * EIO 11448 * EINVAL 11449 * value returned by physio 11450 * 11451 * Context: Kernel thread context. 11452 */ 11453 /* ARGSUSED */ 11454 static int 11455 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p) 11456 { 11457 struct sd_lun *un = NULL; 11458 int secmask; 11459 int err; 11460 11461 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11462 return (ENXIO); 11463 } 11464 11465 ASSERT(!mutex_owned(SD_MUTEX(un))); 11466 11467 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11468 mutex_enter(SD_MUTEX(un)); 11469 /* 11470 * Because the call to sd_ready_and_valid will issue I/O we 11471 * must wait here if either the device is suspended or 11472 * if it's power level is changing. 11473 */ 11474 while ((un->un_state == SD_STATE_SUSPENDED) || 11475 (un->un_state == SD_STATE_PM_CHANGING)) { 11476 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11477 } 11478 un->un_ncmds_in_driver++; 11479 mutex_exit(SD_MUTEX(un)); 11480 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11481 mutex_enter(SD_MUTEX(un)); 11482 un->un_ncmds_in_driver--; 11483 ASSERT(un->un_ncmds_in_driver >= 0); 11484 mutex_exit(SD_MUTEX(un)); 11485 return (EIO); 11486 } 11487 mutex_enter(SD_MUTEX(un)); 11488 un->un_ncmds_in_driver--; 11489 ASSERT(un->un_ncmds_in_driver >= 0); 11490 mutex_exit(SD_MUTEX(un)); 11491 } 11492 11493 /* 11494 * Write requests are restricted to multiples of the system block size. 11495 */ 11496 secmask = un->un_sys_blocksize - 1; 11497 11498 if (uio->uio_loffset & ((offset_t)(secmask))) { 11499 SD_ERROR(SD_LOG_READ_WRITE, un, 11500 "sdwrite: file offset not modulo %d\n", 11501 un->un_sys_blocksize); 11502 err = EINVAL; 11503 } else if (uio->uio_iov->iov_len & (secmask)) { 11504 SD_ERROR(SD_LOG_READ_WRITE, un, 11505 "sdwrite: transfer length not modulo %d\n", 11506 un->un_sys_blocksize); 11507 err = EINVAL; 11508 } else { 11509 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio); 11510 } 11511 return (err); 11512 } 11513 11514 11515 /* 11516 * Function: sdaread 11517 * 11518 * Description: Driver's aread(9e) entry point function. 11519 * 11520 * Arguments: dev - device number 11521 * aio - structure pointer describing where data is to be stored 11522 * cred_p - user credential pointer 11523 * 11524 * Return Code: ENXIO 11525 * EIO 11526 * EINVAL 11527 * value returned by aphysio 11528 * 11529 * Context: Kernel thread context. 11530 */ 11531 /* ARGSUSED */ 11532 static int 11533 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11534 { 11535 struct sd_lun *un = NULL; 11536 struct uio *uio = aio->aio_uio; 11537 int secmask; 11538 int err; 11539 11540 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11541 return (ENXIO); 11542 } 11543 11544 ASSERT(!mutex_owned(SD_MUTEX(un))); 11545 11546 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11547 mutex_enter(SD_MUTEX(un)); 11548 /* 11549 * Because the call to sd_ready_and_valid will issue I/O we 11550 * must wait here if either the device is suspended or 11551 * if it's power level is changing. 11552 */ 11553 while ((un->un_state == SD_STATE_SUSPENDED) || 11554 (un->un_state == SD_STATE_PM_CHANGING)) { 11555 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11556 } 11557 un->un_ncmds_in_driver++; 11558 mutex_exit(SD_MUTEX(un)); 11559 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11560 mutex_enter(SD_MUTEX(un)); 11561 un->un_ncmds_in_driver--; 11562 ASSERT(un->un_ncmds_in_driver >= 0); 11563 mutex_exit(SD_MUTEX(un)); 11564 return (EIO); 11565 } 11566 mutex_enter(SD_MUTEX(un)); 11567 un->un_ncmds_in_driver--; 11568 ASSERT(un->un_ncmds_in_driver >= 0); 11569 mutex_exit(SD_MUTEX(un)); 11570 } 11571 11572 /* 11573 * Read requests are restricted to multiples of the system block size. 11574 */ 11575 secmask = un->un_sys_blocksize - 1; 11576 11577 if (uio->uio_loffset & ((offset_t)(secmask))) { 11578 SD_ERROR(SD_LOG_READ_WRITE, un, 11579 "sdaread: file offset not modulo %d\n", 11580 un->un_sys_blocksize); 11581 err = EINVAL; 11582 } else if (uio->uio_iov->iov_len & (secmask)) { 11583 SD_ERROR(SD_LOG_READ_WRITE, un, 11584 "sdaread: transfer length not modulo %d\n", 11585 un->un_sys_blocksize); 11586 err = EINVAL; 11587 } else { 11588 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio); 11589 } 11590 return (err); 11591 } 11592 11593 11594 /* 11595 * Function: sdawrite 11596 * 11597 * Description: Driver's awrite(9e) entry point function. 11598 * 11599 * Arguments: dev - device number 11600 * aio - structure pointer describing where data is stored 11601 * cred_p - user credential pointer 11602 * 11603 * Return Code: ENXIO 11604 * EIO 11605 * EINVAL 11606 * value returned by aphysio 11607 * 11608 * Context: Kernel thread context. 11609 */ 11610 /* ARGSUSED */ 11611 static int 11612 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 11613 { 11614 struct sd_lun *un = NULL; 11615 struct uio *uio = aio->aio_uio; 11616 int secmask; 11617 int err; 11618 11619 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 11620 return (ENXIO); 11621 } 11622 11623 ASSERT(!mutex_owned(SD_MUTEX(un))); 11624 11625 if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) { 11626 mutex_enter(SD_MUTEX(un)); 11627 /* 11628 * Because the call to sd_ready_and_valid will issue I/O we 11629 * must wait here if either the device is suspended or 11630 * if it's power level is changing. 11631 */ 11632 while ((un->un_state == SD_STATE_SUSPENDED) || 11633 (un->un_state == SD_STATE_PM_CHANGING)) { 11634 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11635 } 11636 un->un_ncmds_in_driver++; 11637 mutex_exit(SD_MUTEX(un)); 11638 if ((sd_ready_and_valid(un)) != SD_READY_VALID) { 11639 mutex_enter(SD_MUTEX(un)); 11640 un->un_ncmds_in_driver--; 11641 ASSERT(un->un_ncmds_in_driver >= 0); 11642 mutex_exit(SD_MUTEX(un)); 11643 return (EIO); 11644 } 11645 mutex_enter(SD_MUTEX(un)); 11646 un->un_ncmds_in_driver--; 11647 ASSERT(un->un_ncmds_in_driver >= 0); 11648 mutex_exit(SD_MUTEX(un)); 11649 } 11650 11651 /* 11652 * Write requests are restricted to multiples of the system block size. 11653 */ 11654 secmask = un->un_sys_blocksize - 1; 11655 11656 if (uio->uio_loffset & ((offset_t)(secmask))) { 11657 SD_ERROR(SD_LOG_READ_WRITE, un, 11658 "sdawrite: file offset not modulo %d\n", 11659 un->un_sys_blocksize); 11660 err = EINVAL; 11661 } else if (uio->uio_iov->iov_len & (secmask)) { 11662 SD_ERROR(SD_LOG_READ_WRITE, un, 11663 "sdawrite: transfer length not modulo %d\n", 11664 un->un_sys_blocksize); 11665 err = EINVAL; 11666 } else { 11667 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio); 11668 } 11669 return (err); 11670 } 11671 11672 11673 11674 11675 11676 /* 11677 * Driver IO processing follows the following sequence: 11678 * 11679 * sdioctl(9E) sdstrategy(9E) biodone(9F) 11680 * | | ^ 11681 * v v | 11682 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+ 11683 * | | | | 11684 * v | | | 11685 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone() 11686 * | | ^ ^ 11687 * v v | | 11688 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | | 11689 * | | | | 11690 * +---+ | +------------+ +-------+ 11691 * | | | | 11692 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11693 * | v | | 11694 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() | 11695 * | | ^ | 11696 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11697 * | v | | 11698 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() | 11699 * | | ^ | 11700 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| | 11701 * | v | | 11702 * | sd_checksum_iostart() sd_checksum_iodone() | 11703 * | | ^ | 11704 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+ 11705 * | v | | 11706 * | sd_pm_iostart() sd_pm_iodone() | 11707 * | | ^ | 11708 * | | | | 11709 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+ 11710 * | ^ 11711 * v | 11712 * sd_core_iostart() | 11713 * | | 11714 * | +------>(*destroypkt)() 11715 * +-> sd_start_cmds() <-+ | | 11716 * | | | v 11717 * | | | scsi_destroy_pkt(9F) 11718 * | | | 11719 * +->(*initpkt)() +- sdintr() 11720 * | | | | 11721 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx() 11722 * | +-> scsi_setup_cdb(9F) | 11723 * | | 11724 * +--> scsi_transport(9F) | 11725 * | | 11726 * +----> SCSA ---->+ 11727 * 11728 * 11729 * This code is based upon the following presumtions: 11730 * 11731 * - iostart and iodone functions operate on buf(9S) structures. These 11732 * functions perform the necessary operations on the buf(9S) and pass 11733 * them along to the next function in the chain by using the macros 11734 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE() 11735 * (for iodone side functions). 11736 * 11737 * - The iostart side functions may sleep. The iodone side functions 11738 * are called under interrupt context and may NOT sleep. Therefore 11739 * iodone side functions also may not call iostart side functions. 11740 * (NOTE: iostart side functions should NOT sleep for memory, as 11741 * this could result in deadlock.) 11742 * 11743 * - An iostart side function may call its corresponding iodone side 11744 * function directly (if necessary). 11745 * 11746 * - In the event of an error, an iostart side function can return a buf(9S) 11747 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and 11748 * b_error in the usual way of course). 11749 * 11750 * - The taskq mechanism may be used by the iodone side functions to dispatch 11751 * requests to the iostart side functions. The iostart side functions in 11752 * this case would be called under the context of a taskq thread, so it's 11753 * OK for them to block/sleep/spin in this case. 11754 * 11755 * - iostart side functions may allocate "shadow" buf(9S) structs and 11756 * pass them along to the next function in the chain. The corresponding 11757 * iodone side functions must coalesce the "shadow" bufs and return 11758 * the "original" buf to the next higher layer. 11759 * 11760 * - The b_private field of the buf(9S) struct holds a pointer to 11761 * an sd_xbuf struct, which contains information needed to 11762 * construct the scsi_pkt for the command. 11763 * 11764 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each 11765 * layer must acquire & release the SD_MUTEX(un) as needed. 11766 */ 11767 11768 11769 /* 11770 * Create taskq for all targets in the system. This is created at 11771 * _init(9E) and destroyed at _fini(9E). 11772 * 11773 * Note: here we set the minalloc to a reasonably high number to ensure that 11774 * we will have an adequate supply of task entries available at interrupt time. 11775 * This is used in conjunction with the TASKQ_PREPOPULATE flag in 11776 * sd_create_taskq(). Since we do not want to sleep for allocations at 11777 * interrupt time, set maxalloc equal to minalloc. That way we will just fail 11778 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq 11779 * requests any one instant in time. 11780 */ 11781 #define SD_TASKQ_NUMTHREADS 8 11782 #define SD_TASKQ_MINALLOC 256 11783 #define SD_TASKQ_MAXALLOC 256 11784 11785 static taskq_t *sd_tq = NULL; 11786 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq)) 11787 11788 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC; 11789 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC; 11790 11791 /* 11792 * The following task queue is being created for the write part of 11793 * read-modify-write of non-512 block size devices. 11794 * Limit the number of threads to 1 for now. This number has been choosen 11795 * considering the fact that it applies only to dvd ram drives/MO drives 11796 * currently. Performance for which is not main criteria at this stage. 11797 * Note: It needs to be explored if we can use a single taskq in future 11798 */ 11799 #define SD_WMR_TASKQ_NUMTHREADS 1 11800 static taskq_t *sd_wmr_tq = NULL; 11801 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq)) 11802 11803 /* 11804 * Function: sd_taskq_create 11805 * 11806 * Description: Create taskq thread(s) and preallocate task entries 11807 * 11808 * Return Code: Returns a pointer to the allocated taskq_t. 11809 * 11810 * Context: Can sleep. Requires blockable context. 11811 * 11812 * Notes: - The taskq() facility currently is NOT part of the DDI. 11813 * (definitely NOT recommeded for 3rd-party drivers!) :-) 11814 * - taskq_create() will block for memory, also it will panic 11815 * if it cannot create the requested number of threads. 11816 * - Currently taskq_create() creates threads that cannot be 11817 * swapped. 11818 * - We use TASKQ_PREPOPULATE to ensure we have an adequate 11819 * supply of taskq entries at interrupt time (ie, so that we 11820 * do not have to sleep for memory) 11821 */ 11822 11823 static void 11824 sd_taskq_create(void) 11825 { 11826 char taskq_name[TASKQ_NAMELEN]; 11827 11828 ASSERT(sd_tq == NULL); 11829 ASSERT(sd_wmr_tq == NULL); 11830 11831 (void) snprintf(taskq_name, sizeof (taskq_name), 11832 "%s_drv_taskq", sd_label); 11833 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS, 11834 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11835 TASKQ_PREPOPULATE)); 11836 11837 (void) snprintf(taskq_name, sizeof (taskq_name), 11838 "%s_rmw_taskq", sd_label); 11839 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS, 11840 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc, 11841 TASKQ_PREPOPULATE)); 11842 } 11843 11844 11845 /* 11846 * Function: sd_taskq_delete 11847 * 11848 * Description: Complementary cleanup routine for sd_taskq_create(). 11849 * 11850 * Context: Kernel thread context. 11851 */ 11852 11853 static void 11854 sd_taskq_delete(void) 11855 { 11856 ASSERT(sd_tq != NULL); 11857 ASSERT(sd_wmr_tq != NULL); 11858 taskq_destroy(sd_tq); 11859 taskq_destroy(sd_wmr_tq); 11860 sd_tq = NULL; 11861 sd_wmr_tq = NULL; 11862 } 11863 11864 11865 /* 11866 * Function: sdstrategy 11867 * 11868 * Description: Driver's strategy (9E) entry point function. 11869 * 11870 * Arguments: bp - pointer to buf(9S) 11871 * 11872 * Return Code: Always returns zero 11873 * 11874 * Context: Kernel thread context. 11875 */ 11876 11877 static int 11878 sdstrategy(struct buf *bp) 11879 { 11880 struct sd_lun *un; 11881 11882 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 11883 if (un == NULL) { 11884 bioerror(bp, EIO); 11885 bp->b_resid = bp->b_bcount; 11886 biodone(bp); 11887 return (0); 11888 } 11889 /* As was done in the past, fail new cmds. if state is dumping. */ 11890 if (un->un_state == SD_STATE_DUMPING) { 11891 bioerror(bp, ENXIO); 11892 bp->b_resid = bp->b_bcount; 11893 biodone(bp); 11894 return (0); 11895 } 11896 11897 ASSERT(!mutex_owned(SD_MUTEX(un))); 11898 11899 /* 11900 * Commands may sneak in while we released the mutex in 11901 * DDI_SUSPEND, we should block new commands. However, old 11902 * commands that are still in the driver at this point should 11903 * still be allowed to drain. 11904 */ 11905 mutex_enter(SD_MUTEX(un)); 11906 /* 11907 * Must wait here if either the device is suspended or 11908 * if it's power level is changing. 11909 */ 11910 while ((un->un_state == SD_STATE_SUSPENDED) || 11911 (un->un_state == SD_STATE_PM_CHANGING)) { 11912 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 11913 } 11914 11915 un->un_ncmds_in_driver++; 11916 11917 /* 11918 * atapi: Since we are running the CD for now in PIO mode we need to 11919 * call bp_mapin here to avoid bp_mapin called interrupt context under 11920 * the HBA's init_pkt routine. 11921 */ 11922 if (un->un_f_cfg_is_atapi == TRUE) { 11923 mutex_exit(SD_MUTEX(un)); 11924 bp_mapin(bp); 11925 mutex_enter(SD_MUTEX(un)); 11926 } 11927 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n", 11928 un->un_ncmds_in_driver); 11929 11930 mutex_exit(SD_MUTEX(un)); 11931 11932 /* 11933 * This will (eventually) allocate the sd_xbuf area and 11934 * call sd_xbuf_strategy(). We just want to return the 11935 * result of ddi_xbuf_qstrategy so that we have an opt- 11936 * imized tail call which saves us a stack frame. 11937 */ 11938 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr)); 11939 } 11940 11941 11942 /* 11943 * Function: sd_xbuf_strategy 11944 * 11945 * Description: Function for initiating IO operations via the 11946 * ddi_xbuf_qstrategy() mechanism. 11947 * 11948 * Context: Kernel thread context. 11949 */ 11950 11951 static void 11952 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg) 11953 { 11954 struct sd_lun *un = arg; 11955 11956 ASSERT(bp != NULL); 11957 ASSERT(xp != NULL); 11958 ASSERT(un != NULL); 11959 ASSERT(!mutex_owned(SD_MUTEX(un))); 11960 11961 /* 11962 * Initialize the fields in the xbuf and save a pointer to the 11963 * xbuf in bp->b_private. 11964 */ 11965 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL); 11966 11967 /* Send the buf down the iostart chain */ 11968 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp); 11969 } 11970 11971 11972 /* 11973 * Function: sd_xbuf_init 11974 * 11975 * Description: Prepare the given sd_xbuf struct for use. 11976 * 11977 * Arguments: un - ptr to softstate 11978 * bp - ptr to associated buf(9S) 11979 * xp - ptr to associated sd_xbuf 11980 * chain_type - IO chain type to use: 11981 * SD_CHAIN_NULL 11982 * SD_CHAIN_BUFIO 11983 * SD_CHAIN_USCSI 11984 * SD_CHAIN_DIRECT 11985 * SD_CHAIN_DIRECT_PRIORITY 11986 * pktinfop - ptr to private data struct for scsi_pkt(9S) 11987 * initialization; may be NULL if none. 11988 * 11989 * Context: Kernel thread context 11990 */ 11991 11992 static void 11993 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 11994 uchar_t chain_type, void *pktinfop) 11995 { 11996 int index; 11997 11998 ASSERT(un != NULL); 11999 ASSERT(bp != NULL); 12000 ASSERT(xp != NULL); 12001 12002 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n", 12003 bp, chain_type); 12004 12005 xp->xb_un = un; 12006 xp->xb_pktp = NULL; 12007 xp->xb_pktinfo = pktinfop; 12008 xp->xb_private = bp->b_private; 12009 xp->xb_blkno = (daddr_t)bp->b_blkno; 12010 12011 /* 12012 * Set up the iostart and iodone chain indexes in the xbuf, based 12013 * upon the specified chain type to use. 12014 */ 12015 switch (chain_type) { 12016 case SD_CHAIN_NULL: 12017 /* 12018 * Fall thru to just use the values for the buf type, even 12019 * tho for the NULL chain these values will never be used. 12020 */ 12021 /* FALLTHRU */ 12022 case SD_CHAIN_BUFIO: 12023 index = un->un_buf_chain_type; 12024 break; 12025 case SD_CHAIN_USCSI: 12026 index = un->un_uscsi_chain_type; 12027 break; 12028 case SD_CHAIN_DIRECT: 12029 index = un->un_direct_chain_type; 12030 break; 12031 case SD_CHAIN_DIRECT_PRIORITY: 12032 index = un->un_priority_chain_type; 12033 break; 12034 default: 12035 /* We're really broken if we ever get here... */ 12036 panic("sd_xbuf_init: illegal chain type!"); 12037 /*NOTREACHED*/ 12038 } 12039 12040 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index; 12041 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index; 12042 12043 /* 12044 * It might be a bit easier to simply bzero the entire xbuf above, 12045 * but it turns out that since we init a fair number of members anyway, 12046 * we save a fair number cycles by doing explicit assignment of zero. 12047 */ 12048 xp->xb_pkt_flags = 0; 12049 xp->xb_dma_resid = 0; 12050 xp->xb_retry_count = 0; 12051 xp->xb_victim_retry_count = 0; 12052 xp->xb_ua_retry_count = 0; 12053 xp->xb_sense_bp = NULL; 12054 xp->xb_sense_status = 0; 12055 xp->xb_sense_state = 0; 12056 xp->xb_sense_resid = 0; 12057 12058 bp->b_private = xp; 12059 bp->b_flags &= ~(B_DONE | B_ERROR); 12060 bp->b_resid = 0; 12061 bp->av_forw = NULL; 12062 bp->av_back = NULL; 12063 bioerror(bp, 0); 12064 12065 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n"); 12066 } 12067 12068 12069 /* 12070 * Function: sd_uscsi_strategy 12071 * 12072 * Description: Wrapper for calling into the USCSI chain via physio(9F) 12073 * 12074 * Arguments: bp - buf struct ptr 12075 * 12076 * Return Code: Always returns 0 12077 * 12078 * Context: Kernel thread context 12079 */ 12080 12081 static int 12082 sd_uscsi_strategy(struct buf *bp) 12083 { 12084 struct sd_lun *un; 12085 struct sd_uscsi_info *uip; 12086 struct sd_xbuf *xp; 12087 uchar_t chain_type; 12088 12089 ASSERT(bp != NULL); 12090 12091 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 12092 if (un == NULL) { 12093 bioerror(bp, EIO); 12094 bp->b_resid = bp->b_bcount; 12095 biodone(bp); 12096 return (0); 12097 } 12098 12099 ASSERT(!mutex_owned(SD_MUTEX(un))); 12100 12101 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp); 12102 12103 mutex_enter(SD_MUTEX(un)); 12104 /* 12105 * atapi: Since we are running the CD for now in PIO mode we need to 12106 * call bp_mapin here to avoid bp_mapin called interrupt context under 12107 * the HBA's init_pkt routine. 12108 */ 12109 if (un->un_f_cfg_is_atapi == TRUE) { 12110 mutex_exit(SD_MUTEX(un)); 12111 bp_mapin(bp); 12112 mutex_enter(SD_MUTEX(un)); 12113 } 12114 un->un_ncmds_in_driver++; 12115 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n", 12116 un->un_ncmds_in_driver); 12117 mutex_exit(SD_MUTEX(un)); 12118 12119 /* 12120 * A pointer to a struct sd_uscsi_info is expected in bp->b_private 12121 */ 12122 ASSERT(bp->b_private != NULL); 12123 uip = (struct sd_uscsi_info *)bp->b_private; 12124 12125 switch (uip->ui_flags) { 12126 case SD_PATH_DIRECT: 12127 chain_type = SD_CHAIN_DIRECT; 12128 break; 12129 case SD_PATH_DIRECT_PRIORITY: 12130 chain_type = SD_CHAIN_DIRECT_PRIORITY; 12131 break; 12132 default: 12133 chain_type = SD_CHAIN_USCSI; 12134 break; 12135 } 12136 12137 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 12138 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp); 12139 12140 /* Use the index obtained within xbuf_init */ 12141 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp); 12142 12143 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp); 12144 12145 return (0); 12146 } 12147 12148 12149 /* 12150 * These routines perform raw i/o operations. 12151 */ 12152 /*ARGSUSED*/ 12153 static void 12154 sduscsimin(struct buf *bp) 12155 { 12156 /* 12157 * do not break up because the CDB count would then 12158 * be incorrect and data underruns would result (incomplete 12159 * read/writes which would be retried and then failed, see 12160 * sdintr(). 12161 */ 12162 } 12163 12164 12165 12166 /* 12167 * Function: sd_send_scsi_cmd 12168 * 12169 * Description: Runs a USCSI command for user (when called thru sdioctl), 12170 * or for the driver 12171 * 12172 * Arguments: dev - the dev_t for the device 12173 * incmd - ptr to a valid uscsi_cmd struct 12174 * cdbspace - UIO_USERSPACE or UIO_SYSSPACE 12175 * dataspace - UIO_USERSPACE or UIO_SYSSPACE 12176 * rqbufspace - UIO_USERSPACE or UIO_SYSSPACE 12177 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 12178 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 12179 * to use the USCSI "direct" chain and bypass the normal 12180 * command waitq. 12181 * 12182 * Return Code: 0 - successful completion of the given command 12183 * EIO - scsi_reset() failed, or see biowait()/physio() codes. 12184 * ENXIO - soft state not found for specified dev 12185 * EINVAL 12186 * EFAULT - copyin/copyout error 12187 * return code of biowait(9F) or physio(9F): 12188 * EIO - IO error, caller may check incmd->uscsi_status 12189 * ENXIO 12190 * EACCES - reservation conflict 12191 * 12192 * Context: Waits for command to complete. Can sleep. 12193 */ 12194 12195 static int 12196 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, 12197 enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace, 12198 int path_flag) 12199 { 12200 struct sd_uscsi_info *uip; 12201 struct uscsi_cmd *uscmd; 12202 struct sd_lun *un; 12203 struct buf *bp; 12204 int rval; 12205 int flags; 12206 12207 un = ddi_get_soft_state(sd_state, SDUNIT(dev)); 12208 if (un == NULL) { 12209 return (ENXIO); 12210 } 12211 12212 ASSERT(!mutex_owned(SD_MUTEX(un))); 12213 12214 #ifdef SDDEBUG 12215 switch (dataspace) { 12216 case UIO_USERSPACE: 12217 SD_TRACE(SD_LOG_IO, un, 12218 "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un); 12219 break; 12220 case UIO_SYSSPACE: 12221 SD_TRACE(SD_LOG_IO, un, 12222 "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un); 12223 break; 12224 default: 12225 SD_TRACE(SD_LOG_IO, un, 12226 "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un); 12227 break; 12228 } 12229 #endif 12230 12231 /* 12232 * Perform resets directly; no need to generate a command to do it. 12233 */ 12234 if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) { 12235 flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ? 12236 RESET_ALL : RESET_TARGET; 12237 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n"); 12238 if (scsi_reset(SD_ADDRESS(un), flags) == 0) { 12239 /* Reset attempt was unsuccessful */ 12240 SD_TRACE(SD_LOG_IO, un, 12241 "sd_send_scsi_cmd: reset: failure\n"); 12242 return (EIO); 12243 } 12244 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n"); 12245 return (0); 12246 } 12247 12248 /* Perfunctory sanity check... */ 12249 if (incmd->uscsi_cdblen <= 0) { 12250 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12251 "invalid uscsi_cdblen, returning EINVAL\n"); 12252 return (EINVAL); 12253 } else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) { 12254 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12255 "unsupported uscsi_cdblen, returning EINVAL\n"); 12256 return (EINVAL); 12257 } 12258 12259 /* 12260 * In order to not worry about where the uscsi structure came from 12261 * (or where the cdb it points to came from) we're going to make 12262 * kmem_alloc'd copies of them here. This will also allow reference 12263 * to the data they contain long after this process has gone to 12264 * sleep and its kernel stack has been unmapped, etc. 12265 * 12266 * First get some memory for the uscsi_cmd struct and copy the 12267 * contents of the given uscsi_cmd struct into it. 12268 */ 12269 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12270 bcopy(incmd, uscmd, sizeof (struct uscsi_cmd)); 12271 12272 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd", 12273 (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX); 12274 12275 /* 12276 * Now get some space for the CDB, and copy the given CDB into 12277 * it. Use ddi_copyin() in case the data is in user space. 12278 */ 12279 uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP); 12280 flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0; 12281 if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb, 12282 (uint_t)incmd->uscsi_cdblen, flags) != 0) { 12283 kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen); 12284 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12285 return (EFAULT); 12286 } 12287 12288 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB", 12289 (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX); 12290 12291 bp = getrbuf(KM_SLEEP); 12292 12293 /* 12294 * Allocate an sd_uscsi_info struct and fill it with the info 12295 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 12296 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 12297 * since we allocate the buf here in this function, we do not 12298 * need to preserve the prior contents of b_private. 12299 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 12300 */ 12301 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 12302 uip->ui_flags = path_flag; 12303 uip->ui_cmdp = uscmd; 12304 bp->b_private = uip; 12305 12306 /* 12307 * Initialize Request Sense buffering, if requested. 12308 */ 12309 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12310 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12311 /* 12312 * Here uscmd->uscsi_rqbuf currently points to the caller's 12313 * buffer, but we replace this with a kernel buffer that 12314 * we allocate to use with the sense data. The sense data 12315 * (if present) gets copied into this new buffer before the 12316 * command is completed. Then we copy the sense data from 12317 * our allocated buf into the caller's buffer below. Note 12318 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used 12319 * below to perform the copy back to the caller's buf. 12320 */ 12321 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 12322 if (rqbufspace == UIO_USERSPACE) { 12323 uscmd->uscsi_rqlen = SENSE_LENGTH; 12324 uscmd->uscsi_rqresid = SENSE_LENGTH; 12325 } else { 12326 uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen); 12327 uscmd->uscsi_rqlen = rlen; 12328 uscmd->uscsi_rqresid = rlen; 12329 } 12330 } else { 12331 uscmd->uscsi_rqbuf = NULL; 12332 uscmd->uscsi_rqlen = 0; 12333 uscmd->uscsi_rqresid = 0; 12334 } 12335 12336 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p rqlen:%d\n", 12337 uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen); 12338 12339 if (un->un_f_is_fibre == FALSE) { 12340 /* 12341 * Force asynchronous mode, if necessary. Doing this here 12342 * has the unfortunate effect of running other queued 12343 * commands async also, but since the main purpose of this 12344 * capability is downloading new drive firmware, we can 12345 * probably live with it. 12346 */ 12347 if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) { 12348 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12349 == 1) { 12350 if (scsi_ifsetcap(SD_ADDRESS(un), 12351 "synchronous", 0, 1) == 1) { 12352 SD_TRACE(SD_LOG_IO, un, 12353 "sd_send_scsi_cmd: forced async ok\n"); 12354 } else { 12355 SD_TRACE(SD_LOG_IO, un, 12356 "sd_send_scsi_cmd:\ 12357 forced async failed\n"); 12358 rval = EINVAL; 12359 goto done; 12360 } 12361 } 12362 } 12363 12364 /* 12365 * Re-enable synchronous mode, if requested 12366 */ 12367 if (uscmd->uscsi_flags & USCSI_SYNC) { 12368 if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1) 12369 == 0) { 12370 int i = scsi_ifsetcap(SD_ADDRESS(un), 12371 "synchronous", 1, 1); 12372 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12373 "re-enabled sync %s\n", 12374 (i == 1) ? "ok" : "failed"); 12375 } 12376 } 12377 } 12378 12379 /* 12380 * Commands sent with priority are intended for error recovery 12381 * situations, and do not have retries performed. 12382 */ 12383 if (path_flag == SD_PATH_DIRECT_PRIORITY) { 12384 uscmd->uscsi_flags |= USCSI_DIAGNOSE; 12385 } 12386 12387 /* 12388 * If we're going to do actual I/O, let physio do all the right things 12389 */ 12390 if (uscmd->uscsi_buflen != 0) { 12391 struct iovec aiov; 12392 struct uio auio; 12393 struct uio *uio = &auio; 12394 12395 bzero(&auio, sizeof (struct uio)); 12396 bzero(&aiov, sizeof (struct iovec)); 12397 aiov.iov_base = uscmd->uscsi_bufaddr; 12398 aiov.iov_len = uscmd->uscsi_buflen; 12399 uio->uio_iov = &aiov; 12400 12401 uio->uio_iovcnt = 1; 12402 uio->uio_resid = uscmd->uscsi_buflen; 12403 uio->uio_segflg = dataspace; 12404 12405 /* 12406 * physio() will block here until the command completes.... 12407 */ 12408 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n"); 12409 12410 rval = physio(sd_uscsi_strategy, bp, dev, 12411 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE), 12412 sduscsimin, uio); 12413 12414 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12415 "returned from physio with 0x%x\n", rval); 12416 12417 } else { 12418 /* 12419 * We have to mimic what physio would do here! Argh! 12420 */ 12421 bp->b_flags = B_BUSY | 12422 ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE); 12423 bp->b_edev = dev; 12424 bp->b_dev = cmpdev(dev); /* maybe unnecessary? */ 12425 bp->b_bcount = 0; 12426 bp->b_blkno = 0; 12427 12428 SD_TRACE(SD_LOG_IO, un, 12429 "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n"); 12430 12431 (void) sd_uscsi_strategy(bp); 12432 12433 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n"); 12434 12435 rval = biowait(bp); 12436 12437 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12438 "returned from biowait with 0x%x\n", rval); 12439 } 12440 12441 done: 12442 12443 #ifdef SDDEBUG 12444 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12445 "uscsi_status: 0x%02x uscsi_resid:0x%x\n", 12446 uscmd->uscsi_status, uscmd->uscsi_resid); 12447 if (uscmd->uscsi_bufaddr != NULL) { 12448 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12449 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n", 12450 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen); 12451 if (dataspace == UIO_SYSSPACE) { 12452 SD_DUMP_MEMORY(un, SD_LOG_IO, 12453 "data", (uchar_t *)uscmd->uscsi_bufaddr, 12454 uscmd->uscsi_buflen, SD_LOG_HEX); 12455 } 12456 } 12457 #endif 12458 12459 /* 12460 * Get the status and residual to return to the caller. 12461 */ 12462 incmd->uscsi_status = uscmd->uscsi_status; 12463 incmd->uscsi_resid = uscmd->uscsi_resid; 12464 12465 /* 12466 * If the caller wants sense data, copy back whatever sense data 12467 * we may have gotten, and update the relevant rqsense info. 12468 */ 12469 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 12470 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 12471 12472 int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid; 12473 rqlen = min(((int)incmd->uscsi_rqlen), rqlen); 12474 12475 /* Update the Request Sense status and resid */ 12476 incmd->uscsi_rqresid = incmd->uscsi_rqlen - rqlen; 12477 incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus; 12478 12479 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12480 "uscsi_rqstatus: 0x%02x uscsi_rqresid:0x%x\n", 12481 incmd->uscsi_rqstatus, incmd->uscsi_rqresid); 12482 12483 /* Copy out the sense data for user processes */ 12484 if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) { 12485 int flags = 12486 (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL; 12487 if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf, 12488 rqlen, flags) != 0) { 12489 rval = EFAULT; 12490 } 12491 /* 12492 * Note: Can't touch incmd->uscsi_rqbuf so use 12493 * uscmd->uscsi_rqbuf instead. They're the same. 12494 */ 12495 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: " 12496 "incmd->uscsi_rqbuf: 0x%p rqlen:%d\n", 12497 incmd->uscsi_rqbuf, rqlen); 12498 SD_DUMP_MEMORY(un, SD_LOG_IO, "rq", 12499 (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX); 12500 } 12501 } 12502 12503 /* 12504 * Free allocated resources and return; mapout the buf in case it was 12505 * mapped in by a lower layer. 12506 */ 12507 bp_mapout(bp); 12508 freerbuf(bp); 12509 kmem_free(uip, sizeof (struct sd_uscsi_info)); 12510 if (uscmd->uscsi_rqbuf != NULL) { 12511 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 12512 } 12513 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 12514 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 12515 12516 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n"); 12517 12518 return (rval); 12519 } 12520 12521 12522 /* 12523 * Function: sd_buf_iodone 12524 * 12525 * Description: Frees the sd_xbuf & returns the buf to its originator. 12526 * 12527 * Context: May be called from interrupt context. 12528 */ 12529 /* ARGSUSED */ 12530 static void 12531 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp) 12532 { 12533 struct sd_xbuf *xp; 12534 12535 ASSERT(un != NULL); 12536 ASSERT(bp != NULL); 12537 ASSERT(!mutex_owned(SD_MUTEX(un))); 12538 12539 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n"); 12540 12541 xp = SD_GET_XBUF(bp); 12542 ASSERT(xp != NULL); 12543 12544 mutex_enter(SD_MUTEX(un)); 12545 12546 /* 12547 * Grab time when the cmd completed. 12548 * This is used for determining if the system has been 12549 * idle long enough to make it idle to the PM framework. 12550 * This is for lowering the overhead, and therefore improving 12551 * performance per I/O operation. 12552 */ 12553 un->un_pm_idle_time = ddi_get_time(); 12554 12555 un->un_ncmds_in_driver--; 12556 ASSERT(un->un_ncmds_in_driver >= 0); 12557 SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n", 12558 un->un_ncmds_in_driver); 12559 12560 mutex_exit(SD_MUTEX(un)); 12561 12562 ddi_xbuf_done(bp, un->un_xbuf_attr); /* xbuf is gone after this */ 12563 biodone(bp); /* bp is gone after this */ 12564 12565 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n"); 12566 } 12567 12568 12569 /* 12570 * Function: sd_uscsi_iodone 12571 * 12572 * Description: Frees the sd_xbuf & returns the buf to its originator. 12573 * 12574 * Context: May be called from interrupt context. 12575 */ 12576 /* ARGSUSED */ 12577 static void 12578 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 12579 { 12580 struct sd_xbuf *xp; 12581 12582 ASSERT(un != NULL); 12583 ASSERT(bp != NULL); 12584 12585 xp = SD_GET_XBUF(bp); 12586 ASSERT(xp != NULL); 12587 ASSERT(!mutex_owned(SD_MUTEX(un))); 12588 12589 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n"); 12590 12591 bp->b_private = xp->xb_private; 12592 12593 mutex_enter(SD_MUTEX(un)); 12594 12595 /* 12596 * Grab time when the cmd completed. 12597 * This is used for determining if the system has been 12598 * idle long enough to make it idle to the PM framework. 12599 * This is for lowering the overhead, and therefore improving 12600 * performance per I/O operation. 12601 */ 12602 un->un_pm_idle_time = ddi_get_time(); 12603 12604 un->un_ncmds_in_driver--; 12605 ASSERT(un->un_ncmds_in_driver >= 0); 12606 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n", 12607 un->un_ncmds_in_driver); 12608 12609 mutex_exit(SD_MUTEX(un)); 12610 12611 kmem_free(xp, sizeof (struct sd_xbuf)); 12612 biodone(bp); 12613 12614 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n"); 12615 } 12616 12617 12618 /* 12619 * Function: sd_mapblockaddr_iostart 12620 * 12621 * Description: Verify request lies withing the partition limits for 12622 * the indicated minor device. Issue "overrun" buf if 12623 * request would exceed partition range. Converts 12624 * partition-relative block address to absolute. 12625 * 12626 * Context: Can sleep 12627 * 12628 * Issues: This follows what the old code did, in terms of accessing 12629 * some of the partition info in the unit struct without holding 12630 * the mutext. This is a general issue, if the partition info 12631 * can be altered while IO is in progress... as soon as we send 12632 * a buf, its partitioning can be invalid before it gets to the 12633 * device. Probably the right fix is to move partitioning out 12634 * of the driver entirely. 12635 */ 12636 12637 static void 12638 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp) 12639 { 12640 daddr_t nblocks; /* #blocks in the given partition */ 12641 daddr_t blocknum; /* Block number specified by the buf */ 12642 size_t requested_nblocks; 12643 size_t available_nblocks; 12644 int partition; 12645 diskaddr_t partition_offset; 12646 struct sd_xbuf *xp; 12647 12648 12649 ASSERT(un != NULL); 12650 ASSERT(bp != NULL); 12651 ASSERT(!mutex_owned(SD_MUTEX(un))); 12652 12653 SD_TRACE(SD_LOG_IO_PARTITION, un, 12654 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp); 12655 12656 xp = SD_GET_XBUF(bp); 12657 ASSERT(xp != NULL); 12658 12659 /* 12660 * If the geometry is not indicated as valid, attempt to access 12661 * the unit & verify the geometry/label. This can be the case for 12662 * removable-media devices, of if the device was opened in 12663 * NDELAY/NONBLOCK mode. 12664 */ 12665 if ((un->un_f_geometry_is_valid != TRUE) && 12666 (sd_ready_and_valid(un) != SD_READY_VALID)) { 12667 /* 12668 * For removable devices it is possible to start an I/O 12669 * without a media by opening the device in nodelay mode. 12670 * Also for writable CDs there can be many scenarios where 12671 * there is no geometry yet but volume manager is trying to 12672 * issue a read() just because it can see TOC on the CD. So 12673 * do not print a message for removables. 12674 */ 12675 if (!un->un_f_has_removable_media) { 12676 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 12677 "i/o to invalid geometry\n"); 12678 } 12679 bioerror(bp, EIO); 12680 bp->b_resid = bp->b_bcount; 12681 SD_BEGIN_IODONE(index, un, bp); 12682 return; 12683 } 12684 12685 partition = SDPART(bp->b_edev); 12686 12687 /* #blocks in partition */ 12688 nblocks = un->un_map[partition].dkl_nblk; /* #blocks in partition */ 12689 12690 /* Use of a local variable potentially improves performance slightly */ 12691 partition_offset = un->un_offset[partition]; 12692 12693 /* 12694 * blocknum is the starting block number of the request. At this 12695 * point it is still relative to the start of the minor device. 12696 */ 12697 blocknum = xp->xb_blkno; 12698 12699 /* 12700 * Legacy: If the starting block number is one past the last block 12701 * in the partition, do not set B_ERROR in the buf. 12702 */ 12703 if (blocknum == nblocks) { 12704 goto error_exit; 12705 } 12706 12707 /* 12708 * Confirm that the first block of the request lies within the 12709 * partition limits. Also the requested number of bytes must be 12710 * a multiple of the system block size. 12711 */ 12712 if ((blocknum < 0) || (blocknum >= nblocks) || 12713 ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) { 12714 bp->b_flags |= B_ERROR; 12715 goto error_exit; 12716 } 12717 12718 /* 12719 * If the requsted # blocks exceeds the available # blocks, that 12720 * is an overrun of the partition. 12721 */ 12722 requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount); 12723 available_nblocks = (size_t)(nblocks - blocknum); 12724 ASSERT(nblocks >= blocknum); 12725 12726 if (requested_nblocks > available_nblocks) { 12727 /* 12728 * Allocate an "overrun" buf to allow the request to proceed 12729 * for the amount of space available in the partition. The 12730 * amount not transferred will be added into the b_resid 12731 * when the operation is complete. The overrun buf 12732 * replaces the original buf here, and the original buf 12733 * is saved inside the overrun buf, for later use. 12734 */ 12735 size_t resid = SD_SYSBLOCKS2BYTES(un, 12736 (offset_t)(requested_nblocks - available_nblocks)); 12737 size_t count = bp->b_bcount - resid; 12738 /* 12739 * Note: count is an unsigned entity thus it'll NEVER 12740 * be less than 0 so ASSERT the original values are 12741 * correct. 12742 */ 12743 ASSERT(bp->b_bcount >= resid); 12744 12745 bp = sd_bioclone_alloc(bp, count, blocknum, 12746 (int (*)(struct buf *)) sd_mapblockaddr_iodone); 12747 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */ 12748 ASSERT(xp != NULL); 12749 } 12750 12751 /* At this point there should be no residual for this buf. */ 12752 ASSERT(bp->b_resid == 0); 12753 12754 /* Convert the block number to an absolute address. */ 12755 xp->xb_blkno += partition_offset; 12756 12757 SD_NEXT_IOSTART(index, un, bp); 12758 12759 SD_TRACE(SD_LOG_IO_PARTITION, un, 12760 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp); 12761 12762 return; 12763 12764 error_exit: 12765 bp->b_resid = bp->b_bcount; 12766 SD_BEGIN_IODONE(index, un, bp); 12767 SD_TRACE(SD_LOG_IO_PARTITION, un, 12768 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp); 12769 } 12770 12771 12772 /* 12773 * Function: sd_mapblockaddr_iodone 12774 * 12775 * Description: Completion-side processing for partition management. 12776 * 12777 * Context: May be called under interrupt context 12778 */ 12779 12780 static void 12781 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp) 12782 { 12783 /* int partition; */ /* Not used, see below. */ 12784 ASSERT(un != NULL); 12785 ASSERT(bp != NULL); 12786 ASSERT(!mutex_owned(SD_MUTEX(un))); 12787 12788 SD_TRACE(SD_LOG_IO_PARTITION, un, 12789 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp); 12790 12791 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) { 12792 /* 12793 * We have an "overrun" buf to deal with... 12794 */ 12795 struct sd_xbuf *xp; 12796 struct buf *obp; /* ptr to the original buf */ 12797 12798 xp = SD_GET_XBUF(bp); 12799 ASSERT(xp != NULL); 12800 12801 /* Retrieve the pointer to the original buf */ 12802 obp = (struct buf *)xp->xb_private; 12803 ASSERT(obp != NULL); 12804 12805 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid); 12806 bioerror(obp, bp->b_error); 12807 12808 sd_bioclone_free(bp); 12809 12810 /* 12811 * Get back the original buf. 12812 * Note that since the restoration of xb_blkno below 12813 * was removed, the sd_xbuf is not needed. 12814 */ 12815 bp = obp; 12816 /* 12817 * xp = SD_GET_XBUF(bp); 12818 * ASSERT(xp != NULL); 12819 */ 12820 } 12821 12822 /* 12823 * Convert sd->xb_blkno back to a minor-device relative value. 12824 * Note: this has been commented out, as it is not needed in the 12825 * current implementation of the driver (ie, since this function 12826 * is at the top of the layering chains, so the info will be 12827 * discarded) and it is in the "hot" IO path. 12828 * 12829 * partition = getminor(bp->b_edev) & SDPART_MASK; 12830 * xp->xb_blkno -= un->un_offset[partition]; 12831 */ 12832 12833 SD_NEXT_IODONE(index, un, bp); 12834 12835 SD_TRACE(SD_LOG_IO_PARTITION, un, 12836 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp); 12837 } 12838 12839 12840 /* 12841 * Function: sd_mapblocksize_iostart 12842 * 12843 * Description: Convert between system block size (un->un_sys_blocksize) 12844 * and target block size (un->un_tgt_blocksize). 12845 * 12846 * Context: Can sleep to allocate resources. 12847 * 12848 * Assumptions: A higher layer has already performed any partition validation, 12849 * and converted the xp->xb_blkno to an absolute value relative 12850 * to the start of the device. 12851 * 12852 * It is also assumed that the higher layer has implemented 12853 * an "overrun" mechanism for the case where the request would 12854 * read/write beyond the end of a partition. In this case we 12855 * assume (and ASSERT) that bp->b_resid == 0. 12856 * 12857 * Note: The implementation for this routine assumes the target 12858 * block size remains constant between allocation and transport. 12859 */ 12860 12861 static void 12862 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp) 12863 { 12864 struct sd_mapblocksize_info *bsp; 12865 struct sd_xbuf *xp; 12866 offset_t first_byte; 12867 daddr_t start_block, end_block; 12868 daddr_t request_bytes; 12869 ushort_t is_aligned = FALSE; 12870 12871 ASSERT(un != NULL); 12872 ASSERT(bp != NULL); 12873 ASSERT(!mutex_owned(SD_MUTEX(un))); 12874 ASSERT(bp->b_resid == 0); 12875 12876 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 12877 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp); 12878 12879 /* 12880 * For a non-writable CD, a write request is an error 12881 */ 12882 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) && 12883 (un->un_f_mmc_writable_media == FALSE)) { 12884 bioerror(bp, EIO); 12885 bp->b_resid = bp->b_bcount; 12886 SD_BEGIN_IODONE(index, un, bp); 12887 return; 12888 } 12889 12890 /* 12891 * We do not need a shadow buf if the device is using 12892 * un->un_sys_blocksize as its block size or if bcount == 0. 12893 * In this case there is no layer-private data block allocated. 12894 */ 12895 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 12896 (bp->b_bcount == 0)) { 12897 goto done; 12898 } 12899 12900 #if defined(__i386) || defined(__amd64) 12901 /* We do not support non-block-aligned transfers for ROD devices */ 12902 ASSERT(!ISROD(un)); 12903 #endif 12904 12905 xp = SD_GET_XBUF(bp); 12906 ASSERT(xp != NULL); 12907 12908 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12909 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n", 12910 un->un_tgt_blocksize, un->un_sys_blocksize); 12911 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12912 "request start block:0x%x\n", xp->xb_blkno); 12913 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: " 12914 "request len:0x%x\n", bp->b_bcount); 12915 12916 /* 12917 * Allocate the layer-private data area for the mapblocksize layer. 12918 * Layers are allowed to use the xp_private member of the sd_xbuf 12919 * struct to store the pointer to their layer-private data block, but 12920 * each layer also has the responsibility of restoring the prior 12921 * contents of xb_private before returning the buf/xbuf to the 12922 * higher layer that sent it. 12923 * 12924 * Here we save the prior contents of xp->xb_private into the 12925 * bsp->mbs_oprivate field of our layer-private data area. This value 12926 * is restored by sd_mapblocksize_iodone() just prior to freeing up 12927 * the layer-private area and returning the buf/xbuf to the layer 12928 * that sent it. 12929 * 12930 * Note that here we use kmem_zalloc for the allocation as there are 12931 * parts of the mapblocksize code that expect certain fields to be 12932 * zero unless explicitly set to a required value. 12933 */ 12934 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 12935 bsp->mbs_oprivate = xp->xb_private; 12936 xp->xb_private = bsp; 12937 12938 /* 12939 * This treats the data on the disk (target) as an array of bytes. 12940 * first_byte is the byte offset, from the beginning of the device, 12941 * to the location of the request. This is converted from a 12942 * un->un_sys_blocksize block address to a byte offset, and then back 12943 * to a block address based upon a un->un_tgt_blocksize block size. 12944 * 12945 * xp->xb_blkno should be absolute upon entry into this function, 12946 * but, but it is based upon partitions that use the "system" 12947 * block size. It must be adjusted to reflect the block size of 12948 * the target. 12949 * 12950 * Note that end_block is actually the block that follows the last 12951 * block of the request, but that's what is needed for the computation. 12952 */ 12953 first_byte = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 12954 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize; 12955 end_block = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) / 12956 un->un_tgt_blocksize; 12957 12958 /* request_bytes is rounded up to a multiple of the target block size */ 12959 request_bytes = (end_block - start_block) * un->un_tgt_blocksize; 12960 12961 /* 12962 * See if the starting address of the request and the request 12963 * length are aligned on a un->un_tgt_blocksize boundary. If aligned 12964 * then we do not need to allocate a shadow buf to handle the request. 12965 */ 12966 if (((first_byte % un->un_tgt_blocksize) == 0) && 12967 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) { 12968 is_aligned = TRUE; 12969 } 12970 12971 if ((bp->b_flags & B_READ) == 0) { 12972 /* 12973 * Lock the range for a write operation. An aligned request is 12974 * considered a simple write; otherwise the request must be a 12975 * read-modify-write. 12976 */ 12977 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1, 12978 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW); 12979 } 12980 12981 /* 12982 * Alloc a shadow buf if the request is not aligned. Also, this is 12983 * where the READ command is generated for a read-modify-write. (The 12984 * write phase is deferred until after the read completes.) 12985 */ 12986 if (is_aligned == FALSE) { 12987 12988 struct sd_mapblocksize_info *shadow_bsp; 12989 struct sd_xbuf *shadow_xp; 12990 struct buf *shadow_bp; 12991 12992 /* 12993 * Allocate the shadow buf and it associated xbuf. Note that 12994 * after this call the xb_blkno value in both the original 12995 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the 12996 * same: absolute relative to the start of the device, and 12997 * adjusted for the target block size. The b_blkno in the 12998 * shadow buf will also be set to this value. We should never 12999 * change b_blkno in the original bp however. 13000 * 13001 * Note also that the shadow buf will always need to be a 13002 * READ command, regardless of whether the incoming command 13003 * is a READ or a WRITE. 13004 */ 13005 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ, 13006 xp->xb_blkno, 13007 (int (*)(struct buf *)) sd_mapblocksize_iodone); 13008 13009 shadow_xp = SD_GET_XBUF(shadow_bp); 13010 13011 /* 13012 * Allocate the layer-private data for the shadow buf. 13013 * (No need to preserve xb_private in the shadow xbuf.) 13014 */ 13015 shadow_xp->xb_private = shadow_bsp = 13016 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP); 13017 13018 /* 13019 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone 13020 * to figure out where the start of the user data is (based upon 13021 * the system block size) in the data returned by the READ 13022 * command (which will be based upon the target blocksize). Note 13023 * that this is only really used if the request is unaligned. 13024 */ 13025 bsp->mbs_copy_offset = (ssize_t)(first_byte - 13026 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize)); 13027 ASSERT((bsp->mbs_copy_offset >= 0) && 13028 (bsp->mbs_copy_offset < un->un_tgt_blocksize)); 13029 13030 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset; 13031 13032 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index; 13033 13034 /* Transfer the wmap (if any) to the shadow buf */ 13035 shadow_bsp->mbs_wmp = bsp->mbs_wmp; 13036 bsp->mbs_wmp = NULL; 13037 13038 /* 13039 * The shadow buf goes on from here in place of the 13040 * original buf. 13041 */ 13042 shadow_bsp->mbs_orig_bp = bp; 13043 bp = shadow_bp; 13044 } 13045 13046 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13047 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno); 13048 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13049 "sd_mapblocksize_iostart: tgt request len:0x%x\n", 13050 request_bytes); 13051 SD_INFO(SD_LOG_IO_RMMEDIA, un, 13052 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp); 13053 13054 done: 13055 SD_NEXT_IOSTART(index, un, bp); 13056 13057 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13058 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp); 13059 } 13060 13061 13062 /* 13063 * Function: sd_mapblocksize_iodone 13064 * 13065 * Description: Completion side processing for block-size mapping. 13066 * 13067 * Context: May be called under interrupt context 13068 */ 13069 13070 static void 13071 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp) 13072 { 13073 struct sd_mapblocksize_info *bsp; 13074 struct sd_xbuf *xp; 13075 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */ 13076 struct buf *orig_bp; /* ptr to the original buf */ 13077 offset_t shadow_end; 13078 offset_t request_end; 13079 offset_t shadow_start; 13080 ssize_t copy_offset; 13081 size_t copy_length; 13082 size_t shortfall; 13083 uint_t is_write; /* TRUE if this bp is a WRITE */ 13084 uint_t has_wmap; /* TRUE is this bp has a wmap */ 13085 13086 ASSERT(un != NULL); 13087 ASSERT(bp != NULL); 13088 13089 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 13090 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp); 13091 13092 /* 13093 * There is no shadow buf or layer-private data if the target is 13094 * using un->un_sys_blocksize as its block size or if bcount == 0. 13095 */ 13096 if ((un->un_tgt_blocksize == un->un_sys_blocksize) || 13097 (bp->b_bcount == 0)) { 13098 goto exit; 13099 } 13100 13101 xp = SD_GET_XBUF(bp); 13102 ASSERT(xp != NULL); 13103 13104 /* Retrieve the pointer to the layer-private data area from the xbuf. */ 13105 bsp = xp->xb_private; 13106 13107 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE; 13108 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE; 13109 13110 if (is_write) { 13111 /* 13112 * For a WRITE request we must free up the block range that 13113 * we have locked up. This holds regardless of whether this is 13114 * an aligned write request or a read-modify-write request. 13115 */ 13116 sd_range_unlock(un, bsp->mbs_wmp); 13117 bsp->mbs_wmp = NULL; 13118 } 13119 13120 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) { 13121 /* 13122 * An aligned read or write command will have no shadow buf; 13123 * there is not much else to do with it. 13124 */ 13125 goto done; 13126 } 13127 13128 orig_bp = bsp->mbs_orig_bp; 13129 ASSERT(orig_bp != NULL); 13130 orig_xp = SD_GET_XBUF(orig_bp); 13131 ASSERT(orig_xp != NULL); 13132 ASSERT(!mutex_owned(SD_MUTEX(un))); 13133 13134 if (!is_write && has_wmap) { 13135 /* 13136 * A READ with a wmap means this is the READ phase of a 13137 * read-modify-write. If an error occurred on the READ then 13138 * we do not proceed with the WRITE phase or copy any data. 13139 * Just release the write maps and return with an error. 13140 */ 13141 if ((bp->b_resid != 0) || (bp->b_error != 0)) { 13142 orig_bp->b_resid = orig_bp->b_bcount; 13143 bioerror(orig_bp, bp->b_error); 13144 sd_range_unlock(un, bsp->mbs_wmp); 13145 goto freebuf_done; 13146 } 13147 } 13148 13149 /* 13150 * Here is where we set up to copy the data from the shadow buf 13151 * into the space associated with the original buf. 13152 * 13153 * To deal with the conversion between block sizes, these 13154 * computations treat the data as an array of bytes, with the 13155 * first byte (byte 0) corresponding to the first byte in the 13156 * first block on the disk. 13157 */ 13158 13159 /* 13160 * shadow_start and shadow_len indicate the location and size of 13161 * the data returned with the shadow IO request. 13162 */ 13163 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno); 13164 shadow_end = shadow_start + bp->b_bcount - bp->b_resid; 13165 13166 /* 13167 * copy_offset gives the offset (in bytes) from the start of the first 13168 * block of the READ request to the beginning of the data. We retrieve 13169 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved 13170 * there by sd_mapblockize_iostart(). copy_length gives the amount of 13171 * data to be copied (in bytes). 13172 */ 13173 copy_offset = bsp->mbs_copy_offset; 13174 ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize)); 13175 copy_length = orig_bp->b_bcount; 13176 request_end = shadow_start + copy_offset + orig_bp->b_bcount; 13177 13178 /* 13179 * Set up the resid and error fields of orig_bp as appropriate. 13180 */ 13181 if (shadow_end >= request_end) { 13182 /* We got all the requested data; set resid to zero */ 13183 orig_bp->b_resid = 0; 13184 } else { 13185 /* 13186 * We failed to get enough data to fully satisfy the original 13187 * request. Just copy back whatever data we got and set 13188 * up the residual and error code as required. 13189 * 13190 * 'shortfall' is the amount by which the data received with the 13191 * shadow buf has "fallen short" of the requested amount. 13192 */ 13193 shortfall = (size_t)(request_end - shadow_end); 13194 13195 if (shortfall > orig_bp->b_bcount) { 13196 /* 13197 * We did not get enough data to even partially 13198 * fulfill the original request. The residual is 13199 * equal to the amount requested. 13200 */ 13201 orig_bp->b_resid = orig_bp->b_bcount; 13202 } else { 13203 /* 13204 * We did not get all the data that we requested 13205 * from the device, but we will try to return what 13206 * portion we did get. 13207 */ 13208 orig_bp->b_resid = shortfall; 13209 } 13210 ASSERT(copy_length >= orig_bp->b_resid); 13211 copy_length -= orig_bp->b_resid; 13212 } 13213 13214 /* Propagate the error code from the shadow buf to the original buf */ 13215 bioerror(orig_bp, bp->b_error); 13216 13217 if (is_write) { 13218 goto freebuf_done; /* No data copying for a WRITE */ 13219 } 13220 13221 if (has_wmap) { 13222 /* 13223 * This is a READ command from the READ phase of a 13224 * read-modify-write request. We have to copy the data given 13225 * by the user OVER the data returned by the READ command, 13226 * then convert the command from a READ to a WRITE and send 13227 * it back to the target. 13228 */ 13229 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset, 13230 copy_length); 13231 13232 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */ 13233 13234 /* 13235 * Dispatch the WRITE command to the taskq thread, which 13236 * will in turn send the command to the target. When the 13237 * WRITE command completes, we (sd_mapblocksize_iodone()) 13238 * will get called again as part of the iodone chain 13239 * processing for it. Note that we will still be dealing 13240 * with the shadow buf at that point. 13241 */ 13242 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp, 13243 KM_NOSLEEP) != 0) { 13244 /* 13245 * Dispatch was successful so we are done. Return 13246 * without going any higher up the iodone chain. Do 13247 * not free up any layer-private data until after the 13248 * WRITE completes. 13249 */ 13250 return; 13251 } 13252 13253 /* 13254 * Dispatch of the WRITE command failed; set up the error 13255 * condition and send this IO back up the iodone chain. 13256 */ 13257 bioerror(orig_bp, EIO); 13258 orig_bp->b_resid = orig_bp->b_bcount; 13259 13260 } else { 13261 /* 13262 * This is a regular READ request (ie, not a RMW). Copy the 13263 * data from the shadow buf into the original buf. The 13264 * copy_offset compensates for any "misalignment" between the 13265 * shadow buf (with its un->un_tgt_blocksize blocks) and the 13266 * original buf (with its un->un_sys_blocksize blocks). 13267 */ 13268 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr, 13269 copy_length); 13270 } 13271 13272 freebuf_done: 13273 13274 /* 13275 * At this point we still have both the shadow buf AND the original 13276 * buf to deal with, as well as the layer-private data area in each. 13277 * Local variables are as follows: 13278 * 13279 * bp -- points to shadow buf 13280 * xp -- points to xbuf of shadow buf 13281 * bsp -- points to layer-private data area of shadow buf 13282 * orig_bp -- points to original buf 13283 * 13284 * First free the shadow buf and its associated xbuf, then free the 13285 * layer-private data area from the shadow buf. There is no need to 13286 * restore xb_private in the shadow xbuf. 13287 */ 13288 sd_shadow_buf_free(bp); 13289 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13290 13291 /* 13292 * Now update the local variables to point to the original buf, xbuf, 13293 * and layer-private area. 13294 */ 13295 bp = orig_bp; 13296 xp = SD_GET_XBUF(bp); 13297 ASSERT(xp != NULL); 13298 ASSERT(xp == orig_xp); 13299 bsp = xp->xb_private; 13300 ASSERT(bsp != NULL); 13301 13302 done: 13303 /* 13304 * Restore xb_private to whatever it was set to by the next higher 13305 * layer in the chain, then free the layer-private data area. 13306 */ 13307 xp->xb_private = bsp->mbs_oprivate; 13308 kmem_free(bsp, sizeof (struct sd_mapblocksize_info)); 13309 13310 exit: 13311 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp), 13312 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp); 13313 13314 SD_NEXT_IODONE(index, un, bp); 13315 } 13316 13317 13318 /* 13319 * Function: sd_checksum_iostart 13320 * 13321 * Description: A stub function for a layer that's currently not used. 13322 * For now just a placeholder. 13323 * 13324 * Context: Kernel thread context 13325 */ 13326 13327 static void 13328 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp) 13329 { 13330 ASSERT(un != NULL); 13331 ASSERT(bp != NULL); 13332 ASSERT(!mutex_owned(SD_MUTEX(un))); 13333 SD_NEXT_IOSTART(index, un, bp); 13334 } 13335 13336 13337 /* 13338 * Function: sd_checksum_iodone 13339 * 13340 * Description: A stub function for a layer that's currently not used. 13341 * For now just a placeholder. 13342 * 13343 * Context: May be called under interrupt context 13344 */ 13345 13346 static void 13347 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp) 13348 { 13349 ASSERT(un != NULL); 13350 ASSERT(bp != NULL); 13351 ASSERT(!mutex_owned(SD_MUTEX(un))); 13352 SD_NEXT_IODONE(index, un, bp); 13353 } 13354 13355 13356 /* 13357 * Function: sd_checksum_uscsi_iostart 13358 * 13359 * Description: A stub function for a layer that's currently not used. 13360 * For now just a placeholder. 13361 * 13362 * Context: Kernel thread context 13363 */ 13364 13365 static void 13366 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp) 13367 { 13368 ASSERT(un != NULL); 13369 ASSERT(bp != NULL); 13370 ASSERT(!mutex_owned(SD_MUTEX(un))); 13371 SD_NEXT_IOSTART(index, un, bp); 13372 } 13373 13374 13375 /* 13376 * Function: sd_checksum_uscsi_iodone 13377 * 13378 * Description: A stub function for a layer that's currently not used. 13379 * For now just a placeholder. 13380 * 13381 * Context: May be called under interrupt context 13382 */ 13383 13384 static void 13385 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp) 13386 { 13387 ASSERT(un != NULL); 13388 ASSERT(bp != NULL); 13389 ASSERT(!mutex_owned(SD_MUTEX(un))); 13390 SD_NEXT_IODONE(index, un, bp); 13391 } 13392 13393 13394 /* 13395 * Function: sd_pm_iostart 13396 * 13397 * Description: iostart-side routine for Power mangement. 13398 * 13399 * Context: Kernel thread context 13400 */ 13401 13402 static void 13403 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp) 13404 { 13405 ASSERT(un != NULL); 13406 ASSERT(bp != NULL); 13407 ASSERT(!mutex_owned(SD_MUTEX(un))); 13408 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13409 13410 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n"); 13411 13412 if (sd_pm_entry(un) != DDI_SUCCESS) { 13413 /* 13414 * Set up to return the failed buf back up the 'iodone' 13415 * side of the calling chain. 13416 */ 13417 bioerror(bp, EIO); 13418 bp->b_resid = bp->b_bcount; 13419 13420 SD_BEGIN_IODONE(index, un, bp); 13421 13422 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13423 return; 13424 } 13425 13426 SD_NEXT_IOSTART(index, un, bp); 13427 13428 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n"); 13429 } 13430 13431 13432 /* 13433 * Function: sd_pm_iodone 13434 * 13435 * Description: iodone-side routine for power mangement. 13436 * 13437 * Context: may be called from interrupt context 13438 */ 13439 13440 static void 13441 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp) 13442 { 13443 ASSERT(un != NULL); 13444 ASSERT(bp != NULL); 13445 ASSERT(!mutex_owned(&un->un_pm_mutex)); 13446 13447 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n"); 13448 13449 /* 13450 * After attach the following flag is only read, so don't 13451 * take the penalty of acquiring a mutex for it. 13452 */ 13453 if (un->un_f_pm_is_enabled == TRUE) { 13454 sd_pm_exit(un); 13455 } 13456 13457 SD_NEXT_IODONE(index, un, bp); 13458 13459 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n"); 13460 } 13461 13462 13463 /* 13464 * Function: sd_core_iostart 13465 * 13466 * Description: Primary driver function for enqueuing buf(9S) structs from 13467 * the system and initiating IO to the target device 13468 * 13469 * Context: Kernel thread context. Can sleep. 13470 * 13471 * Assumptions: - The given xp->xb_blkno is absolute 13472 * (ie, relative to the start of the device). 13473 * - The IO is to be done using the native blocksize of 13474 * the device, as specified in un->un_tgt_blocksize. 13475 */ 13476 /* ARGSUSED */ 13477 static void 13478 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp) 13479 { 13480 struct sd_xbuf *xp; 13481 13482 ASSERT(un != NULL); 13483 ASSERT(bp != NULL); 13484 ASSERT(!mutex_owned(SD_MUTEX(un))); 13485 ASSERT(bp->b_resid == 0); 13486 13487 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp); 13488 13489 xp = SD_GET_XBUF(bp); 13490 ASSERT(xp != NULL); 13491 13492 mutex_enter(SD_MUTEX(un)); 13493 13494 /* 13495 * If we are currently in the failfast state, fail any new IO 13496 * that has B_FAILFAST set, then return. 13497 */ 13498 if ((bp->b_flags & B_FAILFAST) && 13499 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) { 13500 mutex_exit(SD_MUTEX(un)); 13501 bioerror(bp, EIO); 13502 bp->b_resid = bp->b_bcount; 13503 SD_BEGIN_IODONE(index, un, bp); 13504 return; 13505 } 13506 13507 if (SD_IS_DIRECT_PRIORITY(xp)) { 13508 /* 13509 * Priority command -- transport it immediately. 13510 * 13511 * Note: We may want to assert that USCSI_DIAGNOSE is set, 13512 * because all direct priority commands should be associated 13513 * with error recovery actions which we don't want to retry. 13514 */ 13515 sd_start_cmds(un, bp); 13516 } else { 13517 /* 13518 * Normal command -- add it to the wait queue, then start 13519 * transporting commands from the wait queue. 13520 */ 13521 sd_add_buf_to_waitq(un, bp); 13522 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 13523 sd_start_cmds(un, NULL); 13524 } 13525 13526 mutex_exit(SD_MUTEX(un)); 13527 13528 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp); 13529 } 13530 13531 13532 /* 13533 * Function: sd_init_cdb_limits 13534 * 13535 * Description: This is to handle scsi_pkt initialization differences 13536 * between the driver platforms. 13537 * 13538 * Legacy behaviors: 13539 * 13540 * If the block number or the sector count exceeds the 13541 * capabilities of a Group 0 command, shift over to a 13542 * Group 1 command. We don't blindly use Group 1 13543 * commands because a) some drives (CDC Wren IVs) get a 13544 * bit confused, and b) there is probably a fair amount 13545 * of speed difference for a target to receive and decode 13546 * a 10 byte command instead of a 6 byte command. 13547 * 13548 * The xfer time difference of 6 vs 10 byte CDBs is 13549 * still significant so this code is still worthwhile. 13550 * 10 byte CDBs are very inefficient with the fas HBA driver 13551 * and older disks. Each CDB byte took 1 usec with some 13552 * popular disks. 13553 * 13554 * Context: Must be called at attach time 13555 */ 13556 13557 static void 13558 sd_init_cdb_limits(struct sd_lun *un) 13559 { 13560 int hba_cdb_limit; 13561 13562 /* 13563 * Use CDB_GROUP1 commands for most devices except for 13564 * parallel SCSI fixed drives in which case we get better 13565 * performance using CDB_GROUP0 commands (where applicable). 13566 */ 13567 un->un_mincdb = SD_CDB_GROUP1; 13568 #if !defined(__fibre) 13569 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) && 13570 !un->un_f_has_removable_media) { 13571 un->un_mincdb = SD_CDB_GROUP0; 13572 } 13573 #endif 13574 13575 /* 13576 * Try to read the max-cdb-length supported by HBA. 13577 */ 13578 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1); 13579 if (0 >= un->un_max_hba_cdb) { 13580 un->un_max_hba_cdb = CDB_GROUP4; 13581 hba_cdb_limit = SD_CDB_GROUP4; 13582 } else if (0 < un->un_max_hba_cdb && 13583 un->un_max_hba_cdb < CDB_GROUP1) { 13584 hba_cdb_limit = SD_CDB_GROUP0; 13585 } else if (CDB_GROUP1 <= un->un_max_hba_cdb && 13586 un->un_max_hba_cdb < CDB_GROUP5) { 13587 hba_cdb_limit = SD_CDB_GROUP1; 13588 } else if (CDB_GROUP5 <= un->un_max_hba_cdb && 13589 un->un_max_hba_cdb < CDB_GROUP4) { 13590 hba_cdb_limit = SD_CDB_GROUP5; 13591 } else { 13592 hba_cdb_limit = SD_CDB_GROUP4; 13593 } 13594 13595 /* 13596 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4 13597 * commands for fixed disks unless we are building for a 32 bit 13598 * kernel. 13599 */ 13600 #ifdef _LP64 13601 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13602 min(hba_cdb_limit, SD_CDB_GROUP4); 13603 #else 13604 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 : 13605 min(hba_cdb_limit, SD_CDB_GROUP1); 13606 #endif 13607 13608 /* 13609 * x86 systems require the PKT_DMA_PARTIAL flag 13610 */ 13611 #if defined(__x86) 13612 un->un_pkt_flags = PKT_DMA_PARTIAL; 13613 #else 13614 un->un_pkt_flags = 0; 13615 #endif 13616 13617 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE) 13618 ? sizeof (struct scsi_arq_status) : 1); 13619 un->un_cmd_timeout = (ushort_t)sd_io_time; 13620 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout; 13621 } 13622 13623 13624 /* 13625 * Function: sd_initpkt_for_buf 13626 * 13627 * Description: Allocate and initialize for transport a scsi_pkt struct, 13628 * based upon the info specified in the given buf struct. 13629 * 13630 * Assumes the xb_blkno in the request is absolute (ie, 13631 * relative to the start of the device (NOT partition!). 13632 * Also assumes that the request is using the native block 13633 * size of the device (as returned by the READ CAPACITY 13634 * command). 13635 * 13636 * Return Code: SD_PKT_ALLOC_SUCCESS 13637 * SD_PKT_ALLOC_FAILURE 13638 * SD_PKT_ALLOC_FAILURE_NO_DMA 13639 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13640 * 13641 * Context: Kernel thread and may be called from software interrupt context 13642 * as part of a sdrunout callback. This function may not block or 13643 * call routines that block 13644 */ 13645 13646 static int 13647 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp) 13648 { 13649 struct sd_xbuf *xp; 13650 struct scsi_pkt *pktp = NULL; 13651 struct sd_lun *un; 13652 size_t blockcount; 13653 daddr_t startblock; 13654 int rval; 13655 int cmd_flags; 13656 13657 ASSERT(bp != NULL); 13658 ASSERT(pktpp != NULL); 13659 xp = SD_GET_XBUF(bp); 13660 ASSERT(xp != NULL); 13661 un = SD_GET_UN(bp); 13662 ASSERT(un != NULL); 13663 ASSERT(mutex_owned(SD_MUTEX(un))); 13664 ASSERT(bp->b_resid == 0); 13665 13666 SD_TRACE(SD_LOG_IO_CORE, un, 13667 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp); 13668 13669 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13670 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) { 13671 /* 13672 * Already have a scsi_pkt -- just need DMA resources. 13673 * We must recompute the CDB in case the mapping returns 13674 * a nonzero pkt_resid. 13675 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer 13676 * that is being retried, the unmap/remap of the DMA resouces 13677 * will result in the entire transfer starting over again 13678 * from the very first block. 13679 */ 13680 ASSERT(xp->xb_pktp != NULL); 13681 pktp = xp->xb_pktp; 13682 } else { 13683 pktp = NULL; 13684 } 13685 #endif /* __i386 || __amd64 */ 13686 13687 startblock = xp->xb_blkno; /* Absolute block num. */ 13688 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount); 13689 13690 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13691 13692 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK); 13693 13694 #else 13695 13696 cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags; 13697 13698 #endif 13699 13700 /* 13701 * sd_setup_rw_pkt will determine the appropriate CDB group to use, 13702 * call scsi_init_pkt, and build the CDB. 13703 */ 13704 rval = sd_setup_rw_pkt(un, &pktp, bp, 13705 cmd_flags, sdrunout, (caddr_t)un, 13706 startblock, blockcount); 13707 13708 if (rval == 0) { 13709 /* 13710 * Success. 13711 * 13712 * If partial DMA is being used and required for this transfer. 13713 * set it up here. 13714 */ 13715 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 && 13716 (pktp->pkt_resid != 0)) { 13717 13718 /* 13719 * Save the CDB length and pkt_resid for the 13720 * next xfer 13721 */ 13722 xp->xb_dma_resid = pktp->pkt_resid; 13723 13724 /* rezero resid */ 13725 pktp->pkt_resid = 0; 13726 13727 } else { 13728 xp->xb_dma_resid = 0; 13729 } 13730 13731 pktp->pkt_flags = un->un_tagflags; 13732 pktp->pkt_time = un->un_cmd_timeout; 13733 pktp->pkt_comp = sdintr; 13734 13735 pktp->pkt_private = bp; 13736 *pktpp = pktp; 13737 13738 SD_TRACE(SD_LOG_IO_CORE, un, 13739 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp); 13740 13741 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 13742 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED; 13743 #endif 13744 13745 return (SD_PKT_ALLOC_SUCCESS); 13746 13747 } 13748 13749 /* 13750 * SD_PKT_ALLOC_FAILURE is the only expected failure code 13751 * from sd_setup_rw_pkt. 13752 */ 13753 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 13754 13755 if (rval == SD_PKT_ALLOC_FAILURE) { 13756 *pktpp = NULL; 13757 /* 13758 * Set the driver state to RWAIT to indicate the driver 13759 * is waiting on resource allocations. The driver will not 13760 * suspend, pm_suspend, or detatch while the state is RWAIT. 13761 */ 13762 New_state(un, SD_STATE_RWAIT); 13763 13764 SD_ERROR(SD_LOG_IO_CORE, un, 13765 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp); 13766 13767 if ((bp->b_flags & B_ERROR) != 0) { 13768 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 13769 } 13770 return (SD_PKT_ALLOC_FAILURE); 13771 } else { 13772 /* 13773 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL 13774 * 13775 * This should never happen. Maybe someone messed with the 13776 * kernel's minphys? 13777 */ 13778 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 13779 "Request rejected: too large for CDB: " 13780 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount); 13781 SD_ERROR(SD_LOG_IO_CORE, un, 13782 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp); 13783 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13784 13785 } 13786 } 13787 13788 13789 /* 13790 * Function: sd_destroypkt_for_buf 13791 * 13792 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing). 13793 * 13794 * Context: Kernel thread or interrupt context 13795 */ 13796 13797 static void 13798 sd_destroypkt_for_buf(struct buf *bp) 13799 { 13800 ASSERT(bp != NULL); 13801 ASSERT(SD_GET_UN(bp) != NULL); 13802 13803 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13804 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp); 13805 13806 ASSERT(SD_GET_PKTP(bp) != NULL); 13807 scsi_destroy_pkt(SD_GET_PKTP(bp)); 13808 13809 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp), 13810 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp); 13811 } 13812 13813 /* 13814 * Function: sd_setup_rw_pkt 13815 * 13816 * Description: Determines appropriate CDB group for the requested LBA 13817 * and transfer length, calls scsi_init_pkt, and builds 13818 * the CDB. Do not use for partial DMA transfers except 13819 * for the initial transfer since the CDB size must 13820 * remain constant. 13821 * 13822 * Context: Kernel thread and may be called from software interrupt 13823 * context as part of a sdrunout callback. This function may not 13824 * block or call routines that block 13825 */ 13826 13827 13828 int 13829 sd_setup_rw_pkt(struct sd_lun *un, 13830 struct scsi_pkt **pktpp, struct buf *bp, int flags, 13831 int (*callback)(caddr_t), caddr_t callback_arg, 13832 diskaddr_t lba, uint32_t blockcount) 13833 { 13834 struct scsi_pkt *return_pktp; 13835 union scsi_cdb *cdbp; 13836 struct sd_cdbinfo *cp = NULL; 13837 int i; 13838 13839 /* 13840 * See which size CDB to use, based upon the request. 13841 */ 13842 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) { 13843 13844 /* 13845 * Check lba and block count against sd_cdbtab limits. 13846 * In the partial DMA case, we have to use the same size 13847 * CDB for all the transfers. Check lba + blockcount 13848 * against the max LBA so we know that segment of the 13849 * transfer can use the CDB we select. 13850 */ 13851 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) && 13852 (blockcount <= sd_cdbtab[i].sc_maxlen)) { 13853 13854 /* 13855 * The command will fit into the CDB type 13856 * specified by sd_cdbtab[i]. 13857 */ 13858 cp = sd_cdbtab + i; 13859 13860 /* 13861 * Call scsi_init_pkt so we can fill in the 13862 * CDB. 13863 */ 13864 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp, 13865 bp, cp->sc_grpcode, un->un_status_len, 0, 13866 flags, callback, callback_arg); 13867 13868 if (return_pktp != NULL) { 13869 13870 /* 13871 * Return new value of pkt 13872 */ 13873 *pktpp = return_pktp; 13874 13875 /* 13876 * To be safe, zero the CDB insuring there is 13877 * no leftover data from a previous command. 13878 */ 13879 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode); 13880 13881 /* 13882 * Handle partial DMA mapping 13883 */ 13884 if (return_pktp->pkt_resid != 0) { 13885 13886 /* 13887 * Not going to xfer as many blocks as 13888 * originally expected 13889 */ 13890 blockcount -= 13891 SD_BYTES2TGTBLOCKS(un, 13892 return_pktp->pkt_resid); 13893 } 13894 13895 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp; 13896 13897 /* 13898 * Set command byte based on the CDB 13899 * type we matched. 13900 */ 13901 cdbp->scc_cmd = cp->sc_grpmask | 13902 ((bp->b_flags & B_READ) ? 13903 SCMD_READ : SCMD_WRITE); 13904 13905 SD_FILL_SCSI1_LUN(un, return_pktp); 13906 13907 /* 13908 * Fill in LBA and length 13909 */ 13910 ASSERT((cp->sc_grpcode == CDB_GROUP1) || 13911 (cp->sc_grpcode == CDB_GROUP4) || 13912 (cp->sc_grpcode == CDB_GROUP0) || 13913 (cp->sc_grpcode == CDB_GROUP5)); 13914 13915 if (cp->sc_grpcode == CDB_GROUP1) { 13916 FORMG1ADDR(cdbp, lba); 13917 FORMG1COUNT(cdbp, blockcount); 13918 return (0); 13919 } else if (cp->sc_grpcode == CDB_GROUP4) { 13920 FORMG4LONGADDR(cdbp, lba); 13921 FORMG4COUNT(cdbp, blockcount); 13922 return (0); 13923 } else if (cp->sc_grpcode == CDB_GROUP0) { 13924 FORMG0ADDR(cdbp, lba); 13925 FORMG0COUNT(cdbp, blockcount); 13926 return (0); 13927 } else if (cp->sc_grpcode == CDB_GROUP5) { 13928 FORMG5ADDR(cdbp, lba); 13929 FORMG5COUNT(cdbp, blockcount); 13930 return (0); 13931 } 13932 13933 /* 13934 * It should be impossible to not match one 13935 * of the CDB types above, so we should never 13936 * reach this point. Set the CDB command byte 13937 * to test-unit-ready to avoid writing 13938 * to somewhere we don't intend. 13939 */ 13940 cdbp->scc_cmd = SCMD_TEST_UNIT_READY; 13941 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13942 } else { 13943 /* 13944 * Couldn't get scsi_pkt 13945 */ 13946 return (SD_PKT_ALLOC_FAILURE); 13947 } 13948 } 13949 } 13950 13951 /* 13952 * None of the available CDB types were suitable. This really 13953 * should never happen: on a 64 bit system we support 13954 * READ16/WRITE16 which will hold an entire 64 bit disk address 13955 * and on a 32 bit system we will refuse to bind to a device 13956 * larger than 2TB so addresses will never be larger than 32 bits. 13957 */ 13958 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 13959 } 13960 13961 #if defined(__i386) || defined(__amd64) 13962 /* 13963 * Function: sd_setup_next_rw_pkt 13964 * 13965 * Description: Setup packet for partial DMA transfers, except for the 13966 * initial transfer. sd_setup_rw_pkt should be used for 13967 * the initial transfer. 13968 * 13969 * Context: Kernel thread and may be called from interrupt context. 13970 */ 13971 13972 int 13973 sd_setup_next_rw_pkt(struct sd_lun *un, 13974 struct scsi_pkt *pktp, struct buf *bp, 13975 diskaddr_t lba, uint32_t blockcount) 13976 { 13977 uchar_t com; 13978 union scsi_cdb *cdbp; 13979 uchar_t cdb_group_id; 13980 13981 ASSERT(pktp != NULL); 13982 ASSERT(pktp->pkt_cdbp != NULL); 13983 13984 cdbp = (union scsi_cdb *)pktp->pkt_cdbp; 13985 com = cdbp->scc_cmd; 13986 cdb_group_id = CDB_GROUPID(com); 13987 13988 ASSERT((cdb_group_id == CDB_GROUPID_0) || 13989 (cdb_group_id == CDB_GROUPID_1) || 13990 (cdb_group_id == CDB_GROUPID_4) || 13991 (cdb_group_id == CDB_GROUPID_5)); 13992 13993 /* 13994 * Move pkt to the next portion of the xfer. 13995 * func is NULL_FUNC so we do not have to release 13996 * the disk mutex here. 13997 */ 13998 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0, 13999 NULL_FUNC, NULL) == pktp) { 14000 /* Success. Handle partial DMA */ 14001 if (pktp->pkt_resid != 0) { 14002 blockcount -= 14003 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid); 14004 } 14005 14006 cdbp->scc_cmd = com; 14007 SD_FILL_SCSI1_LUN(un, pktp); 14008 if (cdb_group_id == CDB_GROUPID_1) { 14009 FORMG1ADDR(cdbp, lba); 14010 FORMG1COUNT(cdbp, blockcount); 14011 return (0); 14012 } else if (cdb_group_id == CDB_GROUPID_4) { 14013 FORMG4LONGADDR(cdbp, lba); 14014 FORMG4COUNT(cdbp, blockcount); 14015 return (0); 14016 } else if (cdb_group_id == CDB_GROUPID_0) { 14017 FORMG0ADDR(cdbp, lba); 14018 FORMG0COUNT(cdbp, blockcount); 14019 return (0); 14020 } else if (cdb_group_id == CDB_GROUPID_5) { 14021 FORMG5ADDR(cdbp, lba); 14022 FORMG5COUNT(cdbp, blockcount); 14023 return (0); 14024 } 14025 14026 /* Unreachable */ 14027 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL); 14028 } 14029 14030 /* 14031 * Error setting up next portion of cmd transfer. 14032 * Something is definitely very wrong and this 14033 * should not happen. 14034 */ 14035 return (SD_PKT_ALLOC_FAILURE); 14036 } 14037 #endif /* defined(__i386) || defined(__amd64) */ 14038 14039 /* 14040 * Function: sd_initpkt_for_uscsi 14041 * 14042 * Description: Allocate and initialize for transport a scsi_pkt struct, 14043 * based upon the info specified in the given uscsi_cmd struct. 14044 * 14045 * Return Code: SD_PKT_ALLOC_SUCCESS 14046 * SD_PKT_ALLOC_FAILURE 14047 * SD_PKT_ALLOC_FAILURE_NO_DMA 14048 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL 14049 * 14050 * Context: Kernel thread and may be called from software interrupt context 14051 * as part of a sdrunout callback. This function may not block or 14052 * call routines that block 14053 */ 14054 14055 static int 14056 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp) 14057 { 14058 struct uscsi_cmd *uscmd; 14059 struct sd_xbuf *xp; 14060 struct scsi_pkt *pktp; 14061 struct sd_lun *un; 14062 uint32_t flags = 0; 14063 14064 ASSERT(bp != NULL); 14065 ASSERT(pktpp != NULL); 14066 xp = SD_GET_XBUF(bp); 14067 ASSERT(xp != NULL); 14068 un = SD_GET_UN(bp); 14069 ASSERT(un != NULL); 14070 ASSERT(mutex_owned(SD_MUTEX(un))); 14071 14072 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14073 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14074 ASSERT(uscmd != NULL); 14075 14076 SD_TRACE(SD_LOG_IO_CORE, un, 14077 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp); 14078 14079 /* 14080 * Allocate the scsi_pkt for the command. 14081 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path 14082 * during scsi_init_pkt time and will continue to use the 14083 * same path as long as the same scsi_pkt is used without 14084 * intervening scsi_dma_free(). Since uscsi command does 14085 * not call scsi_dmafree() before retry failed command, it 14086 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT 14087 * set such that scsi_vhci can use other available path for 14088 * retry. Besides, ucsci command does not allow DMA breakup, 14089 * so there is no need to set PKT_DMA_PARTIAL flag. 14090 */ 14091 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, 14092 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen, 14093 sizeof (struct scsi_arq_status), 0, 14094 (un->un_pkt_flags & ~PKT_DMA_PARTIAL), 14095 sdrunout, (caddr_t)un); 14096 14097 if (pktp == NULL) { 14098 *pktpp = NULL; 14099 /* 14100 * Set the driver state to RWAIT to indicate the driver 14101 * is waiting on resource allocations. The driver will not 14102 * suspend, pm_suspend, or detatch while the state is RWAIT. 14103 */ 14104 New_state(un, SD_STATE_RWAIT); 14105 14106 SD_ERROR(SD_LOG_IO_CORE, un, 14107 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp); 14108 14109 if ((bp->b_flags & B_ERROR) != 0) { 14110 return (SD_PKT_ALLOC_FAILURE_NO_DMA); 14111 } 14112 return (SD_PKT_ALLOC_FAILURE); 14113 } 14114 14115 /* 14116 * We do not do DMA breakup for USCSI commands, so return failure 14117 * here if all the needed DMA resources were not allocated. 14118 */ 14119 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) && 14120 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) { 14121 scsi_destroy_pkt(pktp); 14122 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: " 14123 "No partial DMA for USCSI. exit: buf:0x%p\n", bp); 14124 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL); 14125 } 14126 14127 /* Init the cdb from the given uscsi struct */ 14128 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp, 14129 uscmd->uscsi_cdb[0], 0, 0, 0); 14130 14131 SD_FILL_SCSI1_LUN(un, pktp); 14132 14133 /* 14134 * Set up the optional USCSI flags. See the uscsi (7I) man page 14135 * for listing of the supported flags. 14136 */ 14137 14138 if (uscmd->uscsi_flags & USCSI_SILENT) { 14139 flags |= FLAG_SILENT; 14140 } 14141 14142 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) { 14143 flags |= FLAG_DIAGNOSE; 14144 } 14145 14146 if (uscmd->uscsi_flags & USCSI_ISOLATE) { 14147 flags |= FLAG_ISOLATE; 14148 } 14149 14150 if (un->un_f_is_fibre == FALSE) { 14151 if (uscmd->uscsi_flags & USCSI_RENEGOT) { 14152 flags |= FLAG_RENEGOTIATE_WIDE_SYNC; 14153 } 14154 } 14155 14156 /* 14157 * Set the pkt flags here so we save time later. 14158 * Note: These flags are NOT in the uscsi man page!!! 14159 */ 14160 if (uscmd->uscsi_flags & USCSI_HEAD) { 14161 flags |= FLAG_HEAD; 14162 } 14163 14164 if (uscmd->uscsi_flags & USCSI_NOINTR) { 14165 flags |= FLAG_NOINTR; 14166 } 14167 14168 /* 14169 * For tagged queueing, things get a bit complicated. 14170 * Check first for head of queue and last for ordered queue. 14171 * If neither head nor order, use the default driver tag flags. 14172 */ 14173 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) { 14174 if (uscmd->uscsi_flags & USCSI_HTAG) { 14175 flags |= FLAG_HTAG; 14176 } else if (uscmd->uscsi_flags & USCSI_OTAG) { 14177 flags |= FLAG_OTAG; 14178 } else { 14179 flags |= un->un_tagflags & FLAG_TAGMASK; 14180 } 14181 } 14182 14183 if (uscmd->uscsi_flags & USCSI_NODISCON) { 14184 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON; 14185 } 14186 14187 pktp->pkt_flags = flags; 14188 14189 /* Copy the caller's CDB into the pkt... */ 14190 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen); 14191 14192 if (uscmd->uscsi_timeout == 0) { 14193 pktp->pkt_time = un->un_uscsi_timeout; 14194 } else { 14195 pktp->pkt_time = uscmd->uscsi_timeout; 14196 } 14197 14198 /* need it later to identify USCSI request in sdintr */ 14199 xp->xb_pkt_flags |= SD_XB_USCSICMD; 14200 14201 xp->xb_sense_resid = uscmd->uscsi_rqresid; 14202 14203 pktp->pkt_private = bp; 14204 pktp->pkt_comp = sdintr; 14205 *pktpp = pktp; 14206 14207 SD_TRACE(SD_LOG_IO_CORE, un, 14208 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp); 14209 14210 return (SD_PKT_ALLOC_SUCCESS); 14211 } 14212 14213 14214 /* 14215 * Function: sd_destroypkt_for_uscsi 14216 * 14217 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi 14218 * IOs.. Also saves relevant info into the associated uscsi_cmd 14219 * struct. 14220 * 14221 * Context: May be called under interrupt context 14222 */ 14223 14224 static void 14225 sd_destroypkt_for_uscsi(struct buf *bp) 14226 { 14227 struct uscsi_cmd *uscmd; 14228 struct sd_xbuf *xp; 14229 struct scsi_pkt *pktp; 14230 struct sd_lun *un; 14231 14232 ASSERT(bp != NULL); 14233 xp = SD_GET_XBUF(bp); 14234 ASSERT(xp != NULL); 14235 un = SD_GET_UN(bp); 14236 ASSERT(un != NULL); 14237 ASSERT(!mutex_owned(SD_MUTEX(un))); 14238 pktp = SD_GET_PKTP(bp); 14239 ASSERT(pktp != NULL); 14240 14241 SD_TRACE(SD_LOG_IO_CORE, un, 14242 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp); 14243 14244 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */ 14245 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo; 14246 ASSERT(uscmd != NULL); 14247 14248 /* Save the status and the residual into the uscsi_cmd struct */ 14249 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK); 14250 uscmd->uscsi_resid = bp->b_resid; 14251 14252 /* 14253 * If enabled, copy any saved sense data into the area specified 14254 * by the uscsi command. 14255 */ 14256 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) && 14257 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) { 14258 /* 14259 * Note: uscmd->uscsi_rqbuf should always point to a buffer 14260 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd()) 14261 */ 14262 uscmd->uscsi_rqstatus = xp->xb_sense_status; 14263 uscmd->uscsi_rqresid = xp->xb_sense_resid; 14264 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH); 14265 } 14266 14267 /* We are done with the scsi_pkt; free it now */ 14268 ASSERT(SD_GET_PKTP(bp) != NULL); 14269 scsi_destroy_pkt(SD_GET_PKTP(bp)); 14270 14271 SD_TRACE(SD_LOG_IO_CORE, un, 14272 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp); 14273 } 14274 14275 14276 /* 14277 * Function: sd_bioclone_alloc 14278 * 14279 * Description: Allocate a buf(9S) and init it as per the given buf 14280 * and the various arguments. The associated sd_xbuf 14281 * struct is (nearly) duplicated. The struct buf *bp 14282 * argument is saved in new_xp->xb_private. 14283 * 14284 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14285 * datalen - size of data area for the shadow bp 14286 * blkno - starting LBA 14287 * func - function pointer for b_iodone in the shadow buf. (May 14288 * be NULL if none.) 14289 * 14290 * Return Code: Pointer to allocates buf(9S) struct 14291 * 14292 * Context: Can sleep. 14293 */ 14294 14295 static struct buf * 14296 sd_bioclone_alloc(struct buf *bp, size_t datalen, 14297 daddr_t blkno, int (*func)(struct buf *)) 14298 { 14299 struct sd_lun *un; 14300 struct sd_xbuf *xp; 14301 struct sd_xbuf *new_xp; 14302 struct buf *new_bp; 14303 14304 ASSERT(bp != NULL); 14305 xp = SD_GET_XBUF(bp); 14306 ASSERT(xp != NULL); 14307 un = SD_GET_UN(bp); 14308 ASSERT(un != NULL); 14309 ASSERT(!mutex_owned(SD_MUTEX(un))); 14310 14311 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func, 14312 NULL, KM_SLEEP); 14313 14314 new_bp->b_lblkno = blkno; 14315 14316 /* 14317 * Allocate an xbuf for the shadow bp and copy the contents of the 14318 * original xbuf into it. 14319 */ 14320 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14321 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14322 14323 /* 14324 * The given bp is automatically saved in the xb_private member 14325 * of the new xbuf. Callers are allowed to depend on this. 14326 */ 14327 new_xp->xb_private = bp; 14328 14329 new_bp->b_private = new_xp; 14330 14331 return (new_bp); 14332 } 14333 14334 /* 14335 * Function: sd_shadow_buf_alloc 14336 * 14337 * Description: Allocate a buf(9S) and init it as per the given buf 14338 * and the various arguments. The associated sd_xbuf 14339 * struct is (nearly) duplicated. The struct buf *bp 14340 * argument is saved in new_xp->xb_private. 14341 * 14342 * Arguments: bp - ptr the the buf(9S) to be "shadowed" 14343 * datalen - size of data area for the shadow bp 14344 * bflags - B_READ or B_WRITE (pseudo flag) 14345 * blkno - starting LBA 14346 * func - function pointer for b_iodone in the shadow buf. (May 14347 * be NULL if none.) 14348 * 14349 * Return Code: Pointer to allocates buf(9S) struct 14350 * 14351 * Context: Can sleep. 14352 */ 14353 14354 static struct buf * 14355 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags, 14356 daddr_t blkno, int (*func)(struct buf *)) 14357 { 14358 struct sd_lun *un; 14359 struct sd_xbuf *xp; 14360 struct sd_xbuf *new_xp; 14361 struct buf *new_bp; 14362 14363 ASSERT(bp != NULL); 14364 xp = SD_GET_XBUF(bp); 14365 ASSERT(xp != NULL); 14366 un = SD_GET_UN(bp); 14367 ASSERT(un != NULL); 14368 ASSERT(!mutex_owned(SD_MUTEX(un))); 14369 14370 if (bp->b_flags & (B_PAGEIO | B_PHYS)) { 14371 bp_mapin(bp); 14372 } 14373 14374 bflags &= (B_READ | B_WRITE); 14375 #if defined(__i386) || defined(__amd64) 14376 new_bp = getrbuf(KM_SLEEP); 14377 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP); 14378 new_bp->b_bcount = datalen; 14379 new_bp->b_flags = bflags | 14380 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW)); 14381 #else 14382 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL, 14383 datalen, bflags, SLEEP_FUNC, NULL); 14384 #endif 14385 new_bp->av_forw = NULL; 14386 new_bp->av_back = NULL; 14387 new_bp->b_dev = bp->b_dev; 14388 new_bp->b_blkno = blkno; 14389 new_bp->b_iodone = func; 14390 new_bp->b_edev = bp->b_edev; 14391 new_bp->b_resid = 0; 14392 14393 /* We need to preserve the B_FAILFAST flag */ 14394 if (bp->b_flags & B_FAILFAST) { 14395 new_bp->b_flags |= B_FAILFAST; 14396 } 14397 14398 /* 14399 * Allocate an xbuf for the shadow bp and copy the contents of the 14400 * original xbuf into it. 14401 */ 14402 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 14403 bcopy(xp, new_xp, sizeof (struct sd_xbuf)); 14404 14405 /* Need later to copy data between the shadow buf & original buf! */ 14406 new_xp->xb_pkt_flags |= PKT_CONSISTENT; 14407 14408 /* 14409 * The given bp is automatically saved in the xb_private member 14410 * of the new xbuf. Callers are allowed to depend on this. 14411 */ 14412 new_xp->xb_private = bp; 14413 14414 new_bp->b_private = new_xp; 14415 14416 return (new_bp); 14417 } 14418 14419 /* 14420 * Function: sd_bioclone_free 14421 * 14422 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations 14423 * in the larger than partition operation. 14424 * 14425 * Context: May be called under interrupt context 14426 */ 14427 14428 static void 14429 sd_bioclone_free(struct buf *bp) 14430 { 14431 struct sd_xbuf *xp; 14432 14433 ASSERT(bp != NULL); 14434 xp = SD_GET_XBUF(bp); 14435 ASSERT(xp != NULL); 14436 14437 /* 14438 * Call bp_mapout() before freeing the buf, in case a lower 14439 * layer or HBA had done a bp_mapin(). we must do this here 14440 * as we are the "originator" of the shadow buf. 14441 */ 14442 bp_mapout(bp); 14443 14444 /* 14445 * Null out b_iodone before freeing the bp, to ensure that the driver 14446 * never gets confused by a stale value in this field. (Just a little 14447 * extra defensiveness here.) 14448 */ 14449 bp->b_iodone = NULL; 14450 14451 freerbuf(bp); 14452 14453 kmem_free(xp, sizeof (struct sd_xbuf)); 14454 } 14455 14456 /* 14457 * Function: sd_shadow_buf_free 14458 * 14459 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations. 14460 * 14461 * Context: May be called under interrupt context 14462 */ 14463 14464 static void 14465 sd_shadow_buf_free(struct buf *bp) 14466 { 14467 struct sd_xbuf *xp; 14468 14469 ASSERT(bp != NULL); 14470 xp = SD_GET_XBUF(bp); 14471 ASSERT(xp != NULL); 14472 14473 #if defined(__sparc) 14474 /* 14475 * Call bp_mapout() before freeing the buf, in case a lower 14476 * layer or HBA had done a bp_mapin(). we must do this here 14477 * as we are the "originator" of the shadow buf. 14478 */ 14479 bp_mapout(bp); 14480 #endif 14481 14482 /* 14483 * Null out b_iodone before freeing the bp, to ensure that the driver 14484 * never gets confused by a stale value in this field. (Just a little 14485 * extra defensiveness here.) 14486 */ 14487 bp->b_iodone = NULL; 14488 14489 #if defined(__i386) || defined(__amd64) 14490 kmem_free(bp->b_un.b_addr, bp->b_bcount); 14491 freerbuf(bp); 14492 #else 14493 scsi_free_consistent_buf(bp); 14494 #endif 14495 14496 kmem_free(xp, sizeof (struct sd_xbuf)); 14497 } 14498 14499 14500 /* 14501 * Function: sd_print_transport_rejected_message 14502 * 14503 * Description: This implements the ludicrously complex rules for printing 14504 * a "transport rejected" message. This is to address the 14505 * specific problem of having a flood of this error message 14506 * produced when a failover occurs. 14507 * 14508 * Context: Any. 14509 */ 14510 14511 static void 14512 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp, 14513 int code) 14514 { 14515 ASSERT(un != NULL); 14516 ASSERT(mutex_owned(SD_MUTEX(un))); 14517 ASSERT(xp != NULL); 14518 14519 /* 14520 * Print the "transport rejected" message under the following 14521 * conditions: 14522 * 14523 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set 14524 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR. 14525 * - If the error code IS a TRAN_FATAL_ERROR, then the message is 14526 * printed the FIRST time a TRAN_FATAL_ERROR is returned from 14527 * scsi_transport(9F) (which indicates that the target might have 14528 * gone off-line). This uses the un->un_tran_fatal_count 14529 * count, which is incremented whenever a TRAN_FATAL_ERROR is 14530 * received, and reset to zero whenver a TRAN_ACCEPT is returned 14531 * from scsi_transport(). 14532 * 14533 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of 14534 * the preceeding cases in order for the message to be printed. 14535 */ 14536 if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) { 14537 if ((sd_level_mask & SD_LOGMASK_DIAG) || 14538 (code != TRAN_FATAL_ERROR) || 14539 (un->un_tran_fatal_count == 1)) { 14540 switch (code) { 14541 case TRAN_BADPKT: 14542 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14543 "transport rejected bad packet\n"); 14544 break; 14545 case TRAN_FATAL_ERROR: 14546 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14547 "transport rejected fatal error\n"); 14548 break; 14549 default: 14550 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 14551 "transport rejected (%d)\n", code); 14552 break; 14553 } 14554 } 14555 } 14556 } 14557 14558 14559 /* 14560 * Function: sd_add_buf_to_waitq 14561 * 14562 * Description: Add the given buf(9S) struct to the wait queue for the 14563 * instance. If sorting is enabled, then the buf is added 14564 * to the queue via an elevator sort algorithm (a la 14565 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key. 14566 * If sorting is not enabled, then the buf is just added 14567 * to the end of the wait queue. 14568 * 14569 * Return Code: void 14570 * 14571 * Context: Does not sleep/block, therefore technically can be called 14572 * from any context. However if sorting is enabled then the 14573 * execution time is indeterminate, and may take long if 14574 * the wait queue grows large. 14575 */ 14576 14577 static void 14578 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp) 14579 { 14580 struct buf *ap; 14581 14582 ASSERT(bp != NULL); 14583 ASSERT(un != NULL); 14584 ASSERT(mutex_owned(SD_MUTEX(un))); 14585 14586 /* If the queue is empty, add the buf as the only entry & return. */ 14587 if (un->un_waitq_headp == NULL) { 14588 ASSERT(un->un_waitq_tailp == NULL); 14589 un->un_waitq_headp = un->un_waitq_tailp = bp; 14590 bp->av_forw = NULL; 14591 return; 14592 } 14593 14594 ASSERT(un->un_waitq_tailp != NULL); 14595 14596 /* 14597 * If sorting is disabled, just add the buf to the tail end of 14598 * the wait queue and return. 14599 */ 14600 if (un->un_f_disksort_disabled) { 14601 un->un_waitq_tailp->av_forw = bp; 14602 un->un_waitq_tailp = bp; 14603 bp->av_forw = NULL; 14604 return; 14605 } 14606 14607 /* 14608 * Sort thru the list of requests currently on the wait queue 14609 * and add the new buf request at the appropriate position. 14610 * 14611 * The un->un_waitq_headp is an activity chain pointer on which 14612 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The 14613 * first queue holds those requests which are positioned after 14614 * the current SD_GET_BLKNO() (in the first request); the second holds 14615 * requests which came in after their SD_GET_BLKNO() number was passed. 14616 * Thus we implement a one way scan, retracting after reaching 14617 * the end of the drive to the first request on the second 14618 * queue, at which time it becomes the first queue. 14619 * A one-way scan is natural because of the way UNIX read-ahead 14620 * blocks are allocated. 14621 * 14622 * If we lie after the first request, then we must locate the 14623 * second request list and add ourselves to it. 14624 */ 14625 ap = un->un_waitq_headp; 14626 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) { 14627 while (ap->av_forw != NULL) { 14628 /* 14629 * Look for an "inversion" in the (normally 14630 * ascending) block numbers. This indicates 14631 * the start of the second request list. 14632 */ 14633 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) { 14634 /* 14635 * Search the second request list for the 14636 * first request at a larger block number. 14637 * We go before that; however if there is 14638 * no such request, we go at the end. 14639 */ 14640 do { 14641 if (SD_GET_BLKNO(bp) < 14642 SD_GET_BLKNO(ap->av_forw)) { 14643 goto insert; 14644 } 14645 ap = ap->av_forw; 14646 } while (ap->av_forw != NULL); 14647 goto insert; /* after last */ 14648 } 14649 ap = ap->av_forw; 14650 } 14651 14652 /* 14653 * No inversions... we will go after the last, and 14654 * be the first request in the second request list. 14655 */ 14656 goto insert; 14657 } 14658 14659 /* 14660 * Request is at/after the current request... 14661 * sort in the first request list. 14662 */ 14663 while (ap->av_forw != NULL) { 14664 /* 14665 * We want to go after the current request (1) if 14666 * there is an inversion after it (i.e. it is the end 14667 * of the first request list), or (2) if the next 14668 * request is a larger block no. than our request. 14669 */ 14670 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) || 14671 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) { 14672 goto insert; 14673 } 14674 ap = ap->av_forw; 14675 } 14676 14677 /* 14678 * Neither a second list nor a larger request, therefore 14679 * we go at the end of the first list (which is the same 14680 * as the end of the whole schebang). 14681 */ 14682 insert: 14683 bp->av_forw = ap->av_forw; 14684 ap->av_forw = bp; 14685 14686 /* 14687 * If we inserted onto the tail end of the waitq, make sure the 14688 * tail pointer is updated. 14689 */ 14690 if (ap == un->un_waitq_tailp) { 14691 un->un_waitq_tailp = bp; 14692 } 14693 } 14694 14695 14696 /* 14697 * Function: sd_start_cmds 14698 * 14699 * Description: Remove and transport cmds from the driver queues. 14700 * 14701 * Arguments: un - pointer to the unit (soft state) struct for the target. 14702 * 14703 * immed_bp - ptr to a buf to be transported immediately. Only 14704 * the immed_bp is transported; bufs on the waitq are not 14705 * processed and the un_retry_bp is not checked. If immed_bp is 14706 * NULL, then normal queue processing is performed. 14707 * 14708 * Context: May be called from kernel thread context, interrupt context, 14709 * or runout callback context. This function may not block or 14710 * call routines that block. 14711 */ 14712 14713 static void 14714 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp) 14715 { 14716 struct sd_xbuf *xp; 14717 struct buf *bp; 14718 void (*statp)(kstat_io_t *); 14719 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14720 void (*saved_statp)(kstat_io_t *); 14721 #endif 14722 int rval; 14723 14724 ASSERT(un != NULL); 14725 ASSERT(mutex_owned(SD_MUTEX(un))); 14726 ASSERT(un->un_ncmds_in_transport >= 0); 14727 ASSERT(un->un_throttle >= 0); 14728 14729 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n"); 14730 14731 do { 14732 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14733 saved_statp = NULL; 14734 #endif 14735 14736 /* 14737 * If we are syncing or dumping, fail the command to 14738 * avoid recursively calling back into scsi_transport(). 14739 * The dump I/O itself uses a separate code path so this 14740 * only prevents non-dump I/O from being sent while dumping. 14741 * File system sync takes place before dumping begins. 14742 * During panic, filesystem I/O is allowed provided 14743 * un_in_callback is <= 1. This is to prevent recursion 14744 * such as sd_start_cmds -> scsi_transport -> sdintr -> 14745 * sd_start_cmds and so on. See panic.c for more information 14746 * about the states the system can be in during panic. 14747 */ 14748 if ((un->un_state == SD_STATE_DUMPING) || 14749 (ddi_in_panic() && (un->un_in_callback > 1))) { 14750 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14751 "sd_start_cmds: panicking\n"); 14752 goto exit; 14753 } 14754 14755 if ((bp = immed_bp) != NULL) { 14756 /* 14757 * We have a bp that must be transported immediately. 14758 * It's OK to transport the immed_bp here without doing 14759 * the throttle limit check because the immed_bp is 14760 * always used in a retry/recovery case. This means 14761 * that we know we are not at the throttle limit by 14762 * virtue of the fact that to get here we must have 14763 * already gotten a command back via sdintr(). This also 14764 * relies on (1) the command on un_retry_bp preventing 14765 * further commands from the waitq from being issued; 14766 * and (2) the code in sd_retry_command checking the 14767 * throttle limit before issuing a delayed or immediate 14768 * retry. This holds even if the throttle limit is 14769 * currently ratcheted down from its maximum value. 14770 */ 14771 statp = kstat_runq_enter; 14772 if (bp == un->un_retry_bp) { 14773 ASSERT((un->un_retry_statp == NULL) || 14774 (un->un_retry_statp == kstat_waitq_enter) || 14775 (un->un_retry_statp == 14776 kstat_runq_back_to_waitq)); 14777 /* 14778 * If the waitq kstat was incremented when 14779 * sd_set_retry_bp() queued this bp for a retry, 14780 * then we must set up statp so that the waitq 14781 * count will get decremented correctly below. 14782 * Also we must clear un->un_retry_statp to 14783 * ensure that we do not act on a stale value 14784 * in this field. 14785 */ 14786 if ((un->un_retry_statp == kstat_waitq_enter) || 14787 (un->un_retry_statp == 14788 kstat_runq_back_to_waitq)) { 14789 statp = kstat_waitq_to_runq; 14790 } 14791 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14792 saved_statp = un->un_retry_statp; 14793 #endif 14794 un->un_retry_statp = NULL; 14795 14796 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 14797 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p " 14798 "un_throttle:%d un_ncmds_in_transport:%d\n", 14799 un, un->un_retry_bp, un->un_throttle, 14800 un->un_ncmds_in_transport); 14801 } else { 14802 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: " 14803 "processing priority bp:0x%p\n", bp); 14804 } 14805 14806 } else if ((bp = un->un_waitq_headp) != NULL) { 14807 /* 14808 * A command on the waitq is ready to go, but do not 14809 * send it if: 14810 * 14811 * (1) the throttle limit has been reached, or 14812 * (2) a retry is pending, or 14813 * (3) a START_STOP_UNIT callback pending, or 14814 * (4) a callback for a SD_PATH_DIRECT_PRIORITY 14815 * command is pending. 14816 * 14817 * For all of these conditions, IO processing will 14818 * restart after the condition is cleared. 14819 */ 14820 if (un->un_ncmds_in_transport >= un->un_throttle) { 14821 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14822 "sd_start_cmds: exiting, " 14823 "throttle limit reached!\n"); 14824 goto exit; 14825 } 14826 if (un->un_retry_bp != NULL) { 14827 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14828 "sd_start_cmds: exiting, retry pending!\n"); 14829 goto exit; 14830 } 14831 if (un->un_startstop_timeid != NULL) { 14832 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14833 "sd_start_cmds: exiting, " 14834 "START_STOP pending!\n"); 14835 goto exit; 14836 } 14837 if (un->un_direct_priority_timeid != NULL) { 14838 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14839 "sd_start_cmds: exiting, " 14840 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n"); 14841 goto exit; 14842 } 14843 14844 /* Dequeue the command */ 14845 un->un_waitq_headp = bp->av_forw; 14846 if (un->un_waitq_headp == NULL) { 14847 un->un_waitq_tailp = NULL; 14848 } 14849 bp->av_forw = NULL; 14850 statp = kstat_waitq_to_runq; 14851 SD_TRACE(SD_LOG_IO_CORE, un, 14852 "sd_start_cmds: processing waitq bp:0x%p\n", bp); 14853 14854 } else { 14855 /* No work to do so bail out now */ 14856 SD_TRACE(SD_LOG_IO_CORE, un, 14857 "sd_start_cmds: no more work, exiting!\n"); 14858 goto exit; 14859 } 14860 14861 /* 14862 * Reset the state to normal. This is the mechanism by which 14863 * the state transitions from either SD_STATE_RWAIT or 14864 * SD_STATE_OFFLINE to SD_STATE_NORMAL. 14865 * If state is SD_STATE_PM_CHANGING then this command is 14866 * part of the device power control and the state must 14867 * not be put back to normal. Doing so would would 14868 * allow new commands to proceed when they shouldn't, 14869 * the device may be going off. 14870 */ 14871 if ((un->un_state != SD_STATE_SUSPENDED) && 14872 (un->un_state != SD_STATE_PM_CHANGING)) { 14873 New_state(un, SD_STATE_NORMAL); 14874 } 14875 14876 xp = SD_GET_XBUF(bp); 14877 ASSERT(xp != NULL); 14878 14879 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14880 /* 14881 * Allocate the scsi_pkt if we need one, or attach DMA 14882 * resources if we have a scsi_pkt that needs them. The 14883 * latter should only occur for commands that are being 14884 * retried. 14885 */ 14886 if ((xp->xb_pktp == NULL) || 14887 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) { 14888 #else 14889 if (xp->xb_pktp == NULL) { 14890 #endif 14891 /* 14892 * There is no scsi_pkt allocated for this buf. Call 14893 * the initpkt function to allocate & init one. 14894 * 14895 * The scsi_init_pkt runout callback functionality is 14896 * implemented as follows: 14897 * 14898 * 1) The initpkt function always calls 14899 * scsi_init_pkt(9F) with sdrunout specified as the 14900 * callback routine. 14901 * 2) A successful packet allocation is initialized and 14902 * the I/O is transported. 14903 * 3) The I/O associated with an allocation resource 14904 * failure is left on its queue to be retried via 14905 * runout or the next I/O. 14906 * 4) The I/O associated with a DMA error is removed 14907 * from the queue and failed with EIO. Processing of 14908 * the transport queues is also halted to be 14909 * restarted via runout or the next I/O. 14910 * 5) The I/O associated with a CDB size or packet 14911 * size error is removed from the queue and failed 14912 * with EIO. Processing of the transport queues is 14913 * continued. 14914 * 14915 * Note: there is no interface for canceling a runout 14916 * callback. To prevent the driver from detaching or 14917 * suspending while a runout is pending the driver 14918 * state is set to SD_STATE_RWAIT 14919 * 14920 * Note: using the scsi_init_pkt callback facility can 14921 * result in an I/O request persisting at the head of 14922 * the list which cannot be satisfied even after 14923 * multiple retries. In the future the driver may 14924 * implement some kind of maximum runout count before 14925 * failing an I/O. 14926 * 14927 * Note: the use of funcp below may seem superfluous, 14928 * but it helps warlock figure out the correct 14929 * initpkt function calls (see [s]sd.wlcmd). 14930 */ 14931 struct scsi_pkt *pktp; 14932 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp); 14933 14934 ASSERT(bp != un->un_rqs_bp); 14935 14936 funcp = sd_initpkt_map[xp->xb_chain_iostart]; 14937 switch ((*funcp)(bp, &pktp)) { 14938 case SD_PKT_ALLOC_SUCCESS: 14939 xp->xb_pktp = pktp; 14940 SD_TRACE(SD_LOG_IO_CORE, un, 14941 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n", 14942 pktp); 14943 goto got_pkt; 14944 14945 case SD_PKT_ALLOC_FAILURE: 14946 /* 14947 * Temporary (hopefully) resource depletion. 14948 * Since retries and RQS commands always have a 14949 * scsi_pkt allocated, these cases should never 14950 * get here. So the only cases this needs to 14951 * handle is a bp from the waitq (which we put 14952 * back onto the waitq for sdrunout), or a bp 14953 * sent as an immed_bp (which we just fail). 14954 */ 14955 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 14956 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n"); 14957 14958 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 14959 14960 if (bp == immed_bp) { 14961 /* 14962 * If SD_XB_DMA_FREED is clear, then 14963 * this is a failure to allocate a 14964 * scsi_pkt, and we must fail the 14965 * command. 14966 */ 14967 if ((xp->xb_pkt_flags & 14968 SD_XB_DMA_FREED) == 0) { 14969 break; 14970 } 14971 14972 /* 14973 * If this immediate command is NOT our 14974 * un_retry_bp, then we must fail it. 14975 */ 14976 if (bp != un->un_retry_bp) { 14977 break; 14978 } 14979 14980 /* 14981 * We get here if this cmd is our 14982 * un_retry_bp that was DMAFREED, but 14983 * scsi_init_pkt() failed to reallocate 14984 * DMA resources when we attempted to 14985 * retry it. This can happen when an 14986 * mpxio failover is in progress, but 14987 * we don't want to just fail the 14988 * command in this case. 14989 * 14990 * Use timeout(9F) to restart it after 14991 * a 100ms delay. We don't want to 14992 * let sdrunout() restart it, because 14993 * sdrunout() is just supposed to start 14994 * commands that are sitting on the 14995 * wait queue. The un_retry_bp stays 14996 * set until the command completes, but 14997 * sdrunout can be called many times 14998 * before that happens. Since sdrunout 14999 * cannot tell if the un_retry_bp is 15000 * already in the transport, it could 15001 * end up calling scsi_transport() for 15002 * the un_retry_bp multiple times. 15003 * 15004 * Also: don't schedule the callback 15005 * if some other callback is already 15006 * pending. 15007 */ 15008 if (un->un_retry_statp == NULL) { 15009 /* 15010 * restore the kstat pointer to 15011 * keep kstat counts coherent 15012 * when we do retry the command. 15013 */ 15014 un->un_retry_statp = 15015 saved_statp; 15016 } 15017 15018 if ((un->un_startstop_timeid == NULL) && 15019 (un->un_retry_timeid == NULL) && 15020 (un->un_direct_priority_timeid == 15021 NULL)) { 15022 15023 un->un_retry_timeid = 15024 timeout( 15025 sd_start_retry_command, 15026 un, SD_RESTART_TIMEOUT); 15027 } 15028 goto exit; 15029 } 15030 15031 #else 15032 if (bp == immed_bp) { 15033 break; /* Just fail the command */ 15034 } 15035 #endif 15036 15037 /* Add the buf back to the head of the waitq */ 15038 bp->av_forw = un->un_waitq_headp; 15039 un->un_waitq_headp = bp; 15040 if (un->un_waitq_tailp == NULL) { 15041 un->un_waitq_tailp = bp; 15042 } 15043 goto exit; 15044 15045 case SD_PKT_ALLOC_FAILURE_NO_DMA: 15046 /* 15047 * HBA DMA resource failure. Fail the command 15048 * and continue processing of the queues. 15049 */ 15050 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15051 "sd_start_cmds: " 15052 "SD_PKT_ALLOC_FAILURE_NO_DMA\n"); 15053 break; 15054 15055 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL: 15056 /* 15057 * Note:x86: Partial DMA mapping not supported 15058 * for USCSI commands, and all the needed DMA 15059 * resources were not allocated. 15060 */ 15061 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15062 "sd_start_cmds: " 15063 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n"); 15064 break; 15065 15066 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL: 15067 /* 15068 * Note:x86: Request cannot fit into CDB based 15069 * on lba and len. 15070 */ 15071 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15072 "sd_start_cmds: " 15073 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n"); 15074 break; 15075 15076 default: 15077 /* Should NEVER get here! */ 15078 panic("scsi_initpkt error"); 15079 /*NOTREACHED*/ 15080 } 15081 15082 /* 15083 * Fatal error in allocating a scsi_pkt for this buf. 15084 * Update kstats & return the buf with an error code. 15085 * We must use sd_return_failed_command_no_restart() to 15086 * avoid a recursive call back into sd_start_cmds(). 15087 * However this also means that we must keep processing 15088 * the waitq here in order to avoid stalling. 15089 */ 15090 if (statp == kstat_waitq_to_runq) { 15091 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 15092 } 15093 sd_return_failed_command_no_restart(un, bp, EIO); 15094 if (bp == immed_bp) { 15095 /* immed_bp is gone by now, so clear this */ 15096 immed_bp = NULL; 15097 } 15098 continue; 15099 } 15100 got_pkt: 15101 if (bp == immed_bp) { 15102 /* goto the head of the class.... */ 15103 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15104 } 15105 15106 un->un_ncmds_in_transport++; 15107 SD_UPDATE_KSTATS(un, statp, bp); 15108 15109 /* 15110 * Call scsi_transport() to send the command to the target. 15111 * According to SCSA architecture, we must drop the mutex here 15112 * before calling scsi_transport() in order to avoid deadlock. 15113 * Note that the scsi_pkt's completion routine can be executed 15114 * (from interrupt context) even before the call to 15115 * scsi_transport() returns. 15116 */ 15117 SD_TRACE(SD_LOG_IO_CORE, un, 15118 "sd_start_cmds: calling scsi_transport()\n"); 15119 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp); 15120 15121 mutex_exit(SD_MUTEX(un)); 15122 rval = scsi_transport(xp->xb_pktp); 15123 mutex_enter(SD_MUTEX(un)); 15124 15125 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15126 "sd_start_cmds: scsi_transport() returned %d\n", rval); 15127 15128 switch (rval) { 15129 case TRAN_ACCEPT: 15130 /* Clear this with every pkt accepted by the HBA */ 15131 un->un_tran_fatal_count = 0; 15132 break; /* Success; try the next cmd (if any) */ 15133 15134 case TRAN_BUSY: 15135 un->un_ncmds_in_transport--; 15136 ASSERT(un->un_ncmds_in_transport >= 0); 15137 15138 /* 15139 * Don't retry request sense, the sense data 15140 * is lost when another request is sent. 15141 * Free up the rqs buf and retry 15142 * the original failed cmd. Update kstat. 15143 */ 15144 if (bp == un->un_rqs_bp) { 15145 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15146 bp = sd_mark_rqs_idle(un, xp); 15147 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 15148 NULL, NULL, EIO, SD_BSY_TIMEOUT / 500, 15149 kstat_waitq_enter); 15150 goto exit; 15151 } 15152 15153 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */ 15154 /* 15155 * Free the DMA resources for the scsi_pkt. This will 15156 * allow mpxio to select another path the next time 15157 * we call scsi_transport() with this scsi_pkt. 15158 * See sdintr() for the rationalization behind this. 15159 */ 15160 if ((un->un_f_is_fibre == TRUE) && 15161 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 15162 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) { 15163 scsi_dmafree(xp->xb_pktp); 15164 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 15165 } 15166 #endif 15167 15168 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) { 15169 /* 15170 * Commands that are SD_PATH_DIRECT_PRIORITY 15171 * are for error recovery situations. These do 15172 * not use the normal command waitq, so if they 15173 * get a TRAN_BUSY we cannot put them back onto 15174 * the waitq for later retry. One possible 15175 * problem is that there could already be some 15176 * other command on un_retry_bp that is waiting 15177 * for this one to complete, so we would be 15178 * deadlocked if we put this command back onto 15179 * the waitq for later retry (since un_retry_bp 15180 * must complete before the driver gets back to 15181 * commands on the waitq). 15182 * 15183 * To avoid deadlock we must schedule a callback 15184 * that will restart this command after a set 15185 * interval. This should keep retrying for as 15186 * long as the underlying transport keeps 15187 * returning TRAN_BUSY (just like for other 15188 * commands). Use the same timeout interval as 15189 * for the ordinary TRAN_BUSY retry. 15190 */ 15191 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15192 "sd_start_cmds: scsi_transport() returned " 15193 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n"); 15194 15195 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15196 un->un_direct_priority_timeid = 15197 timeout(sd_start_direct_priority_command, 15198 bp, SD_BSY_TIMEOUT / 500); 15199 15200 goto exit; 15201 } 15202 15203 /* 15204 * For TRAN_BUSY, we want to reduce the throttle value, 15205 * unless we are retrying a command. 15206 */ 15207 if (bp != un->un_retry_bp) { 15208 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY); 15209 } 15210 15211 /* 15212 * Set up the bp to be tried again 10 ms later. 15213 * Note:x86: Is there a timeout value in the sd_lun 15214 * for this condition? 15215 */ 15216 sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500, 15217 kstat_runq_back_to_waitq); 15218 goto exit; 15219 15220 case TRAN_FATAL_ERROR: 15221 un->un_tran_fatal_count++; 15222 /* FALLTHRU */ 15223 15224 case TRAN_BADPKT: 15225 default: 15226 un->un_ncmds_in_transport--; 15227 ASSERT(un->un_ncmds_in_transport >= 0); 15228 15229 /* 15230 * If this is our REQUEST SENSE command with a 15231 * transport error, we must get back the pointers 15232 * to the original buf, and mark the REQUEST 15233 * SENSE command as "available". 15234 */ 15235 if (bp == un->un_rqs_bp) { 15236 bp = sd_mark_rqs_idle(un, xp); 15237 xp = SD_GET_XBUF(bp); 15238 } else { 15239 /* 15240 * Legacy behavior: do not update transport 15241 * error count for request sense commands. 15242 */ 15243 SD_UPDATE_ERRSTATS(un, sd_transerrs); 15244 } 15245 15246 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 15247 sd_print_transport_rejected_message(un, xp, rval); 15248 15249 /* 15250 * We must use sd_return_failed_command_no_restart() to 15251 * avoid a recursive call back into sd_start_cmds(). 15252 * However this also means that we must keep processing 15253 * the waitq here in order to avoid stalling. 15254 */ 15255 sd_return_failed_command_no_restart(un, bp, EIO); 15256 15257 /* 15258 * Notify any threads waiting in sd_ddi_suspend() that 15259 * a command completion has occurred. 15260 */ 15261 if (un->un_state == SD_STATE_SUSPENDED) { 15262 cv_broadcast(&un->un_disk_busy_cv); 15263 } 15264 15265 if (bp == immed_bp) { 15266 /* immed_bp is gone by now, so clear this */ 15267 immed_bp = NULL; 15268 } 15269 break; 15270 } 15271 15272 } while (immed_bp == NULL); 15273 15274 exit: 15275 ASSERT(mutex_owned(SD_MUTEX(un))); 15276 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n"); 15277 } 15278 15279 15280 /* 15281 * Function: sd_return_command 15282 * 15283 * Description: Returns a command to its originator (with or without an 15284 * error). Also starts commands waiting to be transported 15285 * to the target. 15286 * 15287 * Context: May be called from interrupt, kernel, or timeout context 15288 */ 15289 15290 static void 15291 sd_return_command(struct sd_lun *un, struct buf *bp) 15292 { 15293 struct sd_xbuf *xp; 15294 #if defined(__i386) || defined(__amd64) 15295 struct scsi_pkt *pktp; 15296 #endif 15297 15298 ASSERT(bp != NULL); 15299 ASSERT(un != NULL); 15300 ASSERT(mutex_owned(SD_MUTEX(un))); 15301 ASSERT(bp != un->un_rqs_bp); 15302 xp = SD_GET_XBUF(bp); 15303 ASSERT(xp != NULL); 15304 15305 #if defined(__i386) || defined(__amd64) 15306 pktp = SD_GET_PKTP(bp); 15307 #endif 15308 15309 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n"); 15310 15311 #if defined(__i386) || defined(__amd64) 15312 /* 15313 * Note:x86: check for the "sdrestart failed" case. 15314 */ 15315 if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) && 15316 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) && 15317 (xp->xb_pktp->pkt_resid == 0)) { 15318 15319 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) { 15320 /* 15321 * Successfully set up next portion of cmd 15322 * transfer, try sending it 15323 */ 15324 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 15325 NULL, NULL, 0, (clock_t)0, NULL); 15326 sd_start_cmds(un, NULL); 15327 return; /* Note:x86: need a return here? */ 15328 } 15329 } 15330 #endif 15331 15332 /* 15333 * If this is the failfast bp, clear it from un_failfast_bp. This 15334 * can happen if upon being re-tried the failfast bp either 15335 * succeeded or encountered another error (possibly even a different 15336 * error than the one that precipitated the failfast state, but in 15337 * that case it would have had to exhaust retries as well). Regardless, 15338 * this should not occur whenever the instance is in the active 15339 * failfast state. 15340 */ 15341 if (bp == un->un_failfast_bp) { 15342 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15343 un->un_failfast_bp = NULL; 15344 } 15345 15346 /* 15347 * Clear the failfast state upon successful completion of ANY cmd. 15348 */ 15349 if (bp->b_error == 0) { 15350 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15351 } 15352 15353 /* 15354 * This is used if the command was retried one or more times. Show that 15355 * we are done with it, and allow processing of the waitq to resume. 15356 */ 15357 if (bp == un->un_retry_bp) { 15358 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15359 "sd_return_command: un:0x%p: " 15360 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15361 un->un_retry_bp = NULL; 15362 un->un_retry_statp = NULL; 15363 } 15364 15365 SD_UPDATE_RDWR_STATS(un, bp); 15366 SD_UPDATE_PARTITION_STATS(un, bp); 15367 15368 switch (un->un_state) { 15369 case SD_STATE_SUSPENDED: 15370 /* 15371 * Notify any threads waiting in sd_ddi_suspend() that 15372 * a command completion has occurred. 15373 */ 15374 cv_broadcast(&un->un_disk_busy_cv); 15375 break; 15376 default: 15377 sd_start_cmds(un, NULL); 15378 break; 15379 } 15380 15381 /* Return this command up the iodone chain to its originator. */ 15382 mutex_exit(SD_MUTEX(un)); 15383 15384 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15385 xp->xb_pktp = NULL; 15386 15387 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15388 15389 ASSERT(!mutex_owned(SD_MUTEX(un))); 15390 mutex_enter(SD_MUTEX(un)); 15391 15392 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n"); 15393 } 15394 15395 15396 /* 15397 * Function: sd_return_failed_command 15398 * 15399 * Description: Command completion when an error occurred. 15400 * 15401 * Context: May be called from interrupt context 15402 */ 15403 15404 static void 15405 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode) 15406 { 15407 ASSERT(bp != NULL); 15408 ASSERT(un != NULL); 15409 ASSERT(mutex_owned(SD_MUTEX(un))); 15410 15411 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15412 "sd_return_failed_command: entry\n"); 15413 15414 /* 15415 * b_resid could already be nonzero due to a partial data 15416 * transfer, so do not change it here. 15417 */ 15418 SD_BIOERROR(bp, errcode); 15419 15420 sd_return_command(un, bp); 15421 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15422 "sd_return_failed_command: exit\n"); 15423 } 15424 15425 15426 /* 15427 * Function: sd_return_failed_command_no_restart 15428 * 15429 * Description: Same as sd_return_failed_command, but ensures that no 15430 * call back into sd_start_cmds will be issued. 15431 * 15432 * Context: May be called from interrupt context 15433 */ 15434 15435 static void 15436 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp, 15437 int errcode) 15438 { 15439 struct sd_xbuf *xp; 15440 15441 ASSERT(bp != NULL); 15442 ASSERT(un != NULL); 15443 ASSERT(mutex_owned(SD_MUTEX(un))); 15444 xp = SD_GET_XBUF(bp); 15445 ASSERT(xp != NULL); 15446 ASSERT(errcode != 0); 15447 15448 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15449 "sd_return_failed_command_no_restart: entry\n"); 15450 15451 /* 15452 * b_resid could already be nonzero due to a partial data 15453 * transfer, so do not change it here. 15454 */ 15455 SD_BIOERROR(bp, errcode); 15456 15457 /* 15458 * If this is the failfast bp, clear it. This can happen if the 15459 * failfast bp encounterd a fatal error when we attempted to 15460 * re-try it (such as a scsi_transport(9F) failure). However 15461 * we should NOT be in an active failfast state if the failfast 15462 * bp is not NULL. 15463 */ 15464 if (bp == un->un_failfast_bp) { 15465 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE); 15466 un->un_failfast_bp = NULL; 15467 } 15468 15469 if (bp == un->un_retry_bp) { 15470 /* 15471 * This command was retried one or more times. Show that we are 15472 * done with it, and allow processing of the waitq to resume. 15473 */ 15474 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15475 "sd_return_failed_command_no_restart: " 15476 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp); 15477 un->un_retry_bp = NULL; 15478 un->un_retry_statp = NULL; 15479 } 15480 15481 SD_UPDATE_RDWR_STATS(un, bp); 15482 SD_UPDATE_PARTITION_STATS(un, bp); 15483 15484 mutex_exit(SD_MUTEX(un)); 15485 15486 if (xp->xb_pktp != NULL) { 15487 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp); 15488 xp->xb_pktp = NULL; 15489 } 15490 15491 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp); 15492 15493 mutex_enter(SD_MUTEX(un)); 15494 15495 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15496 "sd_return_failed_command_no_restart: exit\n"); 15497 } 15498 15499 15500 /* 15501 * Function: sd_retry_command 15502 * 15503 * Description: queue up a command for retry, or (optionally) fail it 15504 * if retry counts are exhausted. 15505 * 15506 * Arguments: un - Pointer to the sd_lun struct for the target. 15507 * 15508 * bp - Pointer to the buf for the command to be retried. 15509 * 15510 * retry_check_flag - Flag to see which (if any) of the retry 15511 * counts should be decremented/checked. If the indicated 15512 * retry count is exhausted, then the command will not be 15513 * retried; it will be failed instead. This should use a 15514 * value equal to one of the following: 15515 * 15516 * SD_RETRIES_NOCHECK 15517 * SD_RESD_RETRIES_STANDARD 15518 * SD_RETRIES_VICTIM 15519 * 15520 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE 15521 * if the check should be made to see of FLAG_ISOLATE is set 15522 * in the pkt. If FLAG_ISOLATE is set, then the command is 15523 * not retried, it is simply failed. 15524 * 15525 * user_funcp - Ptr to function to call before dispatching the 15526 * command. May be NULL if no action needs to be performed. 15527 * (Primarily intended for printing messages.) 15528 * 15529 * user_arg - Optional argument to be passed along to 15530 * the user_funcp call. 15531 * 15532 * failure_code - errno return code to set in the bp if the 15533 * command is going to be failed. 15534 * 15535 * retry_delay - Retry delay interval in (clock_t) units. May 15536 * be zero which indicates that the retry should be retried 15537 * immediately (ie, without an intervening delay). 15538 * 15539 * statp - Ptr to kstat function to be updated if the command 15540 * is queued for a delayed retry. May be NULL if no kstat 15541 * update is desired. 15542 * 15543 * Context: May be called from interupt context. 15544 */ 15545 15546 static void 15547 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag, 15548 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int 15549 code), void *user_arg, int failure_code, clock_t retry_delay, 15550 void (*statp)(kstat_io_t *)) 15551 { 15552 struct sd_xbuf *xp; 15553 struct scsi_pkt *pktp; 15554 15555 ASSERT(un != NULL); 15556 ASSERT(mutex_owned(SD_MUTEX(un))); 15557 ASSERT(bp != NULL); 15558 xp = SD_GET_XBUF(bp); 15559 ASSERT(xp != NULL); 15560 pktp = SD_GET_PKTP(bp); 15561 ASSERT(pktp != NULL); 15562 15563 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15564 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp); 15565 15566 /* 15567 * If we are syncing or dumping, fail the command to avoid 15568 * recursively calling back into scsi_transport(). 15569 */ 15570 if (ddi_in_panic()) { 15571 goto fail_command_no_log; 15572 } 15573 15574 /* 15575 * We should never be be retrying a command with FLAG_DIAGNOSE set, so 15576 * log an error and fail the command. 15577 */ 15578 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 15579 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 15580 "ERROR, retrying FLAG_DIAGNOSE command.\n"); 15581 sd_dump_memory(un, SD_LOG_IO, "CDB", 15582 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 15583 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 15584 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 15585 goto fail_command; 15586 } 15587 15588 /* 15589 * If we are suspended, then put the command onto head of the 15590 * wait queue since we don't want to start more commands. 15591 */ 15592 switch (un->un_state) { 15593 case SD_STATE_SUSPENDED: 15594 case SD_STATE_DUMPING: 15595 bp->av_forw = un->un_waitq_headp; 15596 un->un_waitq_headp = bp; 15597 if (un->un_waitq_tailp == NULL) { 15598 un->un_waitq_tailp = bp; 15599 } 15600 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp); 15601 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: " 15602 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp); 15603 return; 15604 default: 15605 break; 15606 } 15607 15608 /* 15609 * If the caller wants us to check FLAG_ISOLATE, then see if that 15610 * is set; if it is then we do not want to retry the command. 15611 * Normally, FLAG_ISOLATE is only used with USCSI cmds. 15612 */ 15613 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) { 15614 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) { 15615 goto fail_command; 15616 } 15617 } 15618 15619 15620 /* 15621 * If SD_RETRIES_FAILFAST is set, it indicates that either a 15622 * command timeout or a selection timeout has occurred. This means 15623 * that we were unable to establish an kind of communication with 15624 * the target, and subsequent retries and/or commands are likely 15625 * to encounter similar results and take a long time to complete. 15626 * 15627 * If this is a failfast error condition, we need to update the 15628 * failfast state, even if this bp does not have B_FAILFAST set. 15629 */ 15630 if (retry_check_flag & SD_RETRIES_FAILFAST) { 15631 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) { 15632 ASSERT(un->un_failfast_bp == NULL); 15633 /* 15634 * If we are already in the active failfast state, and 15635 * another failfast error condition has been detected, 15636 * then fail this command if it has B_FAILFAST set. 15637 * If B_FAILFAST is clear, then maintain the legacy 15638 * behavior of retrying heroically, even tho this will 15639 * take a lot more time to fail the command. 15640 */ 15641 if (bp->b_flags & B_FAILFAST) { 15642 goto fail_command; 15643 } 15644 } else { 15645 /* 15646 * We're not in the active failfast state, but we 15647 * have a failfast error condition, so we must begin 15648 * transition to the next state. We do this regardless 15649 * of whether or not this bp has B_FAILFAST set. 15650 */ 15651 if (un->un_failfast_bp == NULL) { 15652 /* 15653 * This is the first bp to meet a failfast 15654 * condition so save it on un_failfast_bp & 15655 * do normal retry processing. Do not enter 15656 * active failfast state yet. This marks 15657 * entry into the "failfast pending" state. 15658 */ 15659 un->un_failfast_bp = bp; 15660 15661 } else if (un->un_failfast_bp == bp) { 15662 /* 15663 * This is the second time *this* bp has 15664 * encountered a failfast error condition, 15665 * so enter active failfast state & flush 15666 * queues as appropriate. 15667 */ 15668 un->un_failfast_state = SD_FAILFAST_ACTIVE; 15669 un->un_failfast_bp = NULL; 15670 sd_failfast_flushq(un); 15671 15672 /* 15673 * Fail this bp now if B_FAILFAST set; 15674 * otherwise continue with retries. (It would 15675 * be pretty ironic if this bp succeeded on a 15676 * subsequent retry after we just flushed all 15677 * the queues). 15678 */ 15679 if (bp->b_flags & B_FAILFAST) { 15680 goto fail_command; 15681 } 15682 15683 #if !defined(lint) && !defined(__lint) 15684 } else { 15685 /* 15686 * If neither of the preceeding conditionals 15687 * was true, it means that there is some 15688 * *other* bp that has met an inital failfast 15689 * condition and is currently either being 15690 * retried or is waiting to be retried. In 15691 * that case we should perform normal retry 15692 * processing on *this* bp, since there is a 15693 * chance that the current failfast condition 15694 * is transient and recoverable. If that does 15695 * not turn out to be the case, then retries 15696 * will be cleared when the wait queue is 15697 * flushed anyway. 15698 */ 15699 #endif 15700 } 15701 } 15702 } else { 15703 /* 15704 * SD_RETRIES_FAILFAST is clear, which indicates that we 15705 * likely were able to at least establish some level of 15706 * communication with the target and subsequent commands 15707 * and/or retries are likely to get through to the target, 15708 * In this case we want to be aggressive about clearing 15709 * the failfast state. Note that this does not affect 15710 * the "failfast pending" condition. 15711 */ 15712 un->un_failfast_state = SD_FAILFAST_INACTIVE; 15713 } 15714 15715 15716 /* 15717 * Check the specified retry count to see if we can still do 15718 * any retries with this pkt before we should fail it. 15719 */ 15720 switch (retry_check_flag & SD_RETRIES_MASK) { 15721 case SD_RETRIES_VICTIM: 15722 /* 15723 * Check the victim retry count. If exhausted, then fall 15724 * thru & check against the standard retry count. 15725 */ 15726 if (xp->xb_victim_retry_count < un->un_victim_retry_count) { 15727 /* Increment count & proceed with the retry */ 15728 xp->xb_victim_retry_count++; 15729 break; 15730 } 15731 /* Victim retries exhausted, fall back to std. retries... */ 15732 /* FALLTHRU */ 15733 15734 case SD_RETRIES_STANDARD: 15735 if (xp->xb_retry_count >= un->un_retry_count) { 15736 /* Retries exhausted, fail the command */ 15737 SD_TRACE(SD_LOG_IO_CORE, un, 15738 "sd_retry_command: retries exhausted!\n"); 15739 /* 15740 * update b_resid for failed SCMD_READ & SCMD_WRITE 15741 * commands with nonzero pkt_resid. 15742 */ 15743 if ((pktp->pkt_reason == CMD_CMPLT) && 15744 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) && 15745 (pktp->pkt_resid != 0)) { 15746 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F; 15747 if ((op == SCMD_READ) || (op == SCMD_WRITE)) { 15748 SD_UPDATE_B_RESID(bp, pktp); 15749 } 15750 } 15751 goto fail_command; 15752 } 15753 xp->xb_retry_count++; 15754 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15755 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15756 break; 15757 15758 case SD_RETRIES_UA: 15759 if (xp->xb_ua_retry_count >= sd_ua_retry_count) { 15760 /* Retries exhausted, fail the command */ 15761 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 15762 "Unit Attention retries exhausted. " 15763 "Check the target.\n"); 15764 goto fail_command; 15765 } 15766 xp->xb_ua_retry_count++; 15767 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15768 "sd_retry_command: retry count:%d\n", 15769 xp->xb_ua_retry_count); 15770 break; 15771 15772 case SD_RETRIES_BUSY: 15773 if (xp->xb_retry_count >= un->un_busy_retry_count) { 15774 /* Retries exhausted, fail the command */ 15775 SD_TRACE(SD_LOG_IO_CORE, un, 15776 "sd_retry_command: retries exhausted!\n"); 15777 goto fail_command; 15778 } 15779 xp->xb_retry_count++; 15780 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15781 "sd_retry_command: retry count:%d\n", xp->xb_retry_count); 15782 break; 15783 15784 case SD_RETRIES_NOCHECK: 15785 default: 15786 /* No retry count to check. Just proceed with the retry */ 15787 break; 15788 } 15789 15790 xp->xb_pktp->pkt_flags |= FLAG_HEAD; 15791 15792 /* 15793 * If we were given a zero timeout, we must attempt to retry the 15794 * command immediately (ie, without a delay). 15795 */ 15796 if (retry_delay == 0) { 15797 /* 15798 * Check some limiting conditions to see if we can actually 15799 * do the immediate retry. If we cannot, then we must 15800 * fall back to queueing up a delayed retry. 15801 */ 15802 if (un->un_ncmds_in_transport >= un->un_throttle) { 15803 /* 15804 * We are at the throttle limit for the target, 15805 * fall back to delayed retry. 15806 */ 15807 retry_delay = SD_BSY_TIMEOUT; 15808 statp = kstat_waitq_enter; 15809 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15810 "sd_retry_command: immed. retry hit " 15811 "throttle!\n"); 15812 } else { 15813 /* 15814 * We're clear to proceed with the immediate retry. 15815 * First call the user-provided function (if any) 15816 */ 15817 if (user_funcp != NULL) { 15818 (*user_funcp)(un, bp, user_arg, 15819 SD_IMMEDIATE_RETRY_ISSUED); 15820 #ifdef __lock_lint 15821 sd_print_incomplete_msg(un, bp, user_arg, 15822 SD_IMMEDIATE_RETRY_ISSUED); 15823 sd_print_cmd_incomplete_msg(un, bp, user_arg, 15824 SD_IMMEDIATE_RETRY_ISSUED); 15825 sd_print_sense_failed_msg(un, bp, user_arg, 15826 SD_IMMEDIATE_RETRY_ISSUED); 15827 #endif 15828 } 15829 15830 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15831 "sd_retry_command: issuing immediate retry\n"); 15832 15833 /* 15834 * Call sd_start_cmds() to transport the command to 15835 * the target. 15836 */ 15837 sd_start_cmds(un, bp); 15838 15839 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15840 "sd_retry_command exit\n"); 15841 return; 15842 } 15843 } 15844 15845 /* 15846 * Set up to retry the command after a delay. 15847 * First call the user-provided function (if any) 15848 */ 15849 if (user_funcp != NULL) { 15850 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED); 15851 } 15852 15853 sd_set_retry_bp(un, bp, retry_delay, statp); 15854 15855 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15856 return; 15857 15858 fail_command: 15859 15860 if (user_funcp != NULL) { 15861 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED); 15862 } 15863 15864 fail_command_no_log: 15865 15866 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15867 "sd_retry_command: returning failed command\n"); 15868 15869 sd_return_failed_command(un, bp, failure_code); 15870 15871 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n"); 15872 } 15873 15874 15875 /* 15876 * Function: sd_set_retry_bp 15877 * 15878 * Description: Set up the given bp for retry. 15879 * 15880 * Arguments: un - ptr to associated softstate 15881 * bp - ptr to buf(9S) for the command 15882 * retry_delay - time interval before issuing retry (may be 0) 15883 * statp - optional pointer to kstat function 15884 * 15885 * Context: May be called under interrupt context 15886 */ 15887 15888 static void 15889 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay, 15890 void (*statp)(kstat_io_t *)) 15891 { 15892 ASSERT(un != NULL); 15893 ASSERT(mutex_owned(SD_MUTEX(un))); 15894 ASSERT(bp != NULL); 15895 15896 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 15897 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp); 15898 15899 /* 15900 * Indicate that the command is being retried. This will not allow any 15901 * other commands on the wait queue to be transported to the target 15902 * until this command has been completed (success or failure). The 15903 * "retry command" is not transported to the target until the given 15904 * time delay expires, unless the user specified a 0 retry_delay. 15905 * 15906 * Note: the timeout(9F) callback routine is what actually calls 15907 * sd_start_cmds() to transport the command, with the exception of a 15908 * zero retry_delay. The only current implementor of a zero retry delay 15909 * is the case where a START_STOP_UNIT is sent to spin-up a device. 15910 */ 15911 if (un->un_retry_bp == NULL) { 15912 ASSERT(un->un_retry_statp == NULL); 15913 un->un_retry_bp = bp; 15914 15915 /* 15916 * If the user has not specified a delay the command should 15917 * be queued and no timeout should be scheduled. 15918 */ 15919 if (retry_delay == 0) { 15920 /* 15921 * Save the kstat pointer that will be used in the 15922 * call to SD_UPDATE_KSTATS() below, so that 15923 * sd_start_cmds() can correctly decrement the waitq 15924 * count when it is time to transport this command. 15925 */ 15926 un->un_retry_statp = statp; 15927 goto done; 15928 } 15929 } 15930 15931 if (un->un_retry_bp == bp) { 15932 /* 15933 * Save the kstat pointer that will be used in the call to 15934 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can 15935 * correctly decrement the waitq count when it is time to 15936 * transport this command. 15937 */ 15938 un->un_retry_statp = statp; 15939 15940 /* 15941 * Schedule a timeout if: 15942 * 1) The user has specified a delay. 15943 * 2) There is not a START_STOP_UNIT callback pending. 15944 * 15945 * If no delay has been specified, then it is up to the caller 15946 * to ensure that IO processing continues without stalling. 15947 * Effectively, this means that the caller will issue the 15948 * required call to sd_start_cmds(). The START_STOP_UNIT 15949 * callback does this after the START STOP UNIT command has 15950 * completed. In either of these cases we should not schedule 15951 * a timeout callback here. Also don't schedule the timeout if 15952 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart. 15953 */ 15954 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) && 15955 (un->un_direct_priority_timeid == NULL)) { 15956 un->un_retry_timeid = 15957 timeout(sd_start_retry_command, un, retry_delay); 15958 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15959 "sd_set_retry_bp: setting timeout: un: 0x%p" 15960 " bp:0x%p un_retry_timeid:0x%p\n", 15961 un, bp, un->un_retry_timeid); 15962 } 15963 } else { 15964 /* 15965 * We only get in here if there is already another command 15966 * waiting to be retried. In this case, we just put the 15967 * given command onto the wait queue, so it can be transported 15968 * after the current retry command has completed. 15969 * 15970 * Also we have to make sure that if the command at the head 15971 * of the wait queue is the un_failfast_bp, that we do not 15972 * put ahead of it any other commands that are to be retried. 15973 */ 15974 if ((un->un_failfast_bp != NULL) && 15975 (un->un_failfast_bp == un->un_waitq_headp)) { 15976 /* 15977 * Enqueue this command AFTER the first command on 15978 * the wait queue (which is also un_failfast_bp). 15979 */ 15980 bp->av_forw = un->un_waitq_headp->av_forw; 15981 un->un_waitq_headp->av_forw = bp; 15982 if (un->un_waitq_headp == un->un_waitq_tailp) { 15983 un->un_waitq_tailp = bp; 15984 } 15985 } else { 15986 /* Enqueue this command at the head of the waitq. */ 15987 bp->av_forw = un->un_waitq_headp; 15988 un->un_waitq_headp = bp; 15989 if (un->un_waitq_tailp == NULL) { 15990 un->un_waitq_tailp = bp; 15991 } 15992 } 15993 15994 if (statp == NULL) { 15995 statp = kstat_waitq_enter; 15996 } 15997 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 15998 "sd_set_retry_bp: un:0x%p already delayed retry\n", un); 15999 } 16000 16001 done: 16002 if (statp != NULL) { 16003 SD_UPDATE_KSTATS(un, statp, bp); 16004 } 16005 16006 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16007 "sd_set_retry_bp: exit un:0x%p\n", un); 16008 } 16009 16010 16011 /* 16012 * Function: sd_start_retry_command 16013 * 16014 * Description: Start the command that has been waiting on the target's 16015 * retry queue. Called from timeout(9F) context after the 16016 * retry delay interval has expired. 16017 * 16018 * Arguments: arg - pointer to associated softstate for the device. 16019 * 16020 * Context: timeout(9F) thread context. May not sleep. 16021 */ 16022 16023 static void 16024 sd_start_retry_command(void *arg) 16025 { 16026 struct sd_lun *un = arg; 16027 16028 ASSERT(un != NULL); 16029 ASSERT(!mutex_owned(SD_MUTEX(un))); 16030 16031 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16032 "sd_start_retry_command: entry\n"); 16033 16034 mutex_enter(SD_MUTEX(un)); 16035 16036 un->un_retry_timeid = NULL; 16037 16038 if (un->un_retry_bp != NULL) { 16039 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16040 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n", 16041 un, un->un_retry_bp); 16042 sd_start_cmds(un, un->un_retry_bp); 16043 } 16044 16045 mutex_exit(SD_MUTEX(un)); 16046 16047 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16048 "sd_start_retry_command: exit\n"); 16049 } 16050 16051 16052 /* 16053 * Function: sd_start_direct_priority_command 16054 * 16055 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had 16056 * received TRAN_BUSY when we called scsi_transport() to send it 16057 * to the underlying HBA. This function is called from timeout(9F) 16058 * context after the delay interval has expired. 16059 * 16060 * Arguments: arg - pointer to associated buf(9S) to be restarted. 16061 * 16062 * Context: timeout(9F) thread context. May not sleep. 16063 */ 16064 16065 static void 16066 sd_start_direct_priority_command(void *arg) 16067 { 16068 struct buf *priority_bp = arg; 16069 struct sd_lun *un; 16070 16071 ASSERT(priority_bp != NULL); 16072 un = SD_GET_UN(priority_bp); 16073 ASSERT(un != NULL); 16074 ASSERT(!mutex_owned(SD_MUTEX(un))); 16075 16076 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16077 "sd_start_direct_priority_command: entry\n"); 16078 16079 mutex_enter(SD_MUTEX(un)); 16080 un->un_direct_priority_timeid = NULL; 16081 sd_start_cmds(un, priority_bp); 16082 mutex_exit(SD_MUTEX(un)); 16083 16084 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16085 "sd_start_direct_priority_command: exit\n"); 16086 } 16087 16088 16089 /* 16090 * Function: sd_send_request_sense_command 16091 * 16092 * Description: Sends a REQUEST SENSE command to the target 16093 * 16094 * Context: May be called from interrupt context. 16095 */ 16096 16097 static void 16098 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp, 16099 struct scsi_pkt *pktp) 16100 { 16101 ASSERT(bp != NULL); 16102 ASSERT(un != NULL); 16103 ASSERT(mutex_owned(SD_MUTEX(un))); 16104 16105 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: " 16106 "entry: buf:0x%p\n", bp); 16107 16108 /* 16109 * If we are syncing or dumping, then fail the command to avoid a 16110 * recursive callback into scsi_transport(). Also fail the command 16111 * if we are suspended (legacy behavior). 16112 */ 16113 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) || 16114 (un->un_state == SD_STATE_DUMPING)) { 16115 sd_return_failed_command(un, bp, EIO); 16116 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16117 "sd_send_request_sense_command: syncing/dumping, exit\n"); 16118 return; 16119 } 16120 16121 /* 16122 * Retry the failed command and don't issue the request sense if: 16123 * 1) the sense buf is busy 16124 * 2) we have 1 or more outstanding commands on the target 16125 * (the sense data will be cleared or invalidated any way) 16126 * 16127 * Note: There could be an issue with not checking a retry limit here, 16128 * the problem is determining which retry limit to check. 16129 */ 16130 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) { 16131 /* Don't retry if the command is flagged as non-retryable */ 16132 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 16133 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, 16134 NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter); 16135 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16136 "sd_send_request_sense_command: " 16137 "at full throttle, retrying exit\n"); 16138 } else { 16139 sd_return_failed_command(un, bp, EIO); 16140 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16141 "sd_send_request_sense_command: " 16142 "at full throttle, non-retryable exit\n"); 16143 } 16144 return; 16145 } 16146 16147 sd_mark_rqs_busy(un, bp); 16148 sd_start_cmds(un, un->un_rqs_bp); 16149 16150 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16151 "sd_send_request_sense_command: exit\n"); 16152 } 16153 16154 16155 /* 16156 * Function: sd_mark_rqs_busy 16157 * 16158 * Description: Indicate that the request sense bp for this instance is 16159 * in use. 16160 * 16161 * Context: May be called under interrupt context 16162 */ 16163 16164 static void 16165 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp) 16166 { 16167 struct sd_xbuf *sense_xp; 16168 16169 ASSERT(un != NULL); 16170 ASSERT(bp != NULL); 16171 ASSERT(mutex_owned(SD_MUTEX(un))); 16172 ASSERT(un->un_sense_isbusy == 0); 16173 16174 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: " 16175 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un); 16176 16177 sense_xp = SD_GET_XBUF(un->un_rqs_bp); 16178 ASSERT(sense_xp != NULL); 16179 16180 SD_INFO(SD_LOG_IO, un, 16181 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp); 16182 16183 ASSERT(sense_xp->xb_pktp != NULL); 16184 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) 16185 == (FLAG_SENSING | FLAG_HEAD)); 16186 16187 un->un_sense_isbusy = 1; 16188 un->un_rqs_bp->b_resid = 0; 16189 sense_xp->xb_pktp->pkt_resid = 0; 16190 sense_xp->xb_pktp->pkt_reason = 0; 16191 16192 /* So we can get back the bp at interrupt time! */ 16193 sense_xp->xb_sense_bp = bp; 16194 16195 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH); 16196 16197 /* 16198 * Mark this buf as awaiting sense data. (This is already set in 16199 * the pkt_flags for the RQS packet.) 16200 */ 16201 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING; 16202 16203 sense_xp->xb_retry_count = 0; 16204 sense_xp->xb_victim_retry_count = 0; 16205 sense_xp->xb_ua_retry_count = 0; 16206 sense_xp->xb_dma_resid = 0; 16207 16208 /* Clean up the fields for auto-request sense */ 16209 sense_xp->xb_sense_status = 0; 16210 sense_xp->xb_sense_state = 0; 16211 sense_xp->xb_sense_resid = 0; 16212 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data)); 16213 16214 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n"); 16215 } 16216 16217 16218 /* 16219 * Function: sd_mark_rqs_idle 16220 * 16221 * Description: SD_MUTEX must be held continuously through this routine 16222 * to prevent reuse of the rqs struct before the caller can 16223 * complete it's processing. 16224 * 16225 * Return Code: Pointer to the RQS buf 16226 * 16227 * Context: May be called under interrupt context 16228 */ 16229 16230 static struct buf * 16231 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp) 16232 { 16233 struct buf *bp; 16234 ASSERT(un != NULL); 16235 ASSERT(sense_xp != NULL); 16236 ASSERT(mutex_owned(SD_MUTEX(un))); 16237 ASSERT(un->un_sense_isbusy != 0); 16238 16239 un->un_sense_isbusy = 0; 16240 bp = sense_xp->xb_sense_bp; 16241 sense_xp->xb_sense_bp = NULL; 16242 16243 /* This pkt is no longer interested in getting sense data */ 16244 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING; 16245 16246 return (bp); 16247 } 16248 16249 16250 16251 /* 16252 * Function: sd_alloc_rqs 16253 * 16254 * Description: Set up the unit to receive auto request sense data 16255 * 16256 * Return Code: DDI_SUCCESS or DDI_FAILURE 16257 * 16258 * Context: Called under attach(9E) context 16259 */ 16260 16261 static int 16262 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un) 16263 { 16264 struct sd_xbuf *xp; 16265 16266 ASSERT(un != NULL); 16267 ASSERT(!mutex_owned(SD_MUTEX(un))); 16268 ASSERT(un->un_rqs_bp == NULL); 16269 ASSERT(un->un_rqs_pktp == NULL); 16270 16271 /* 16272 * First allocate the required buf and scsi_pkt structs, then set up 16273 * the CDB in the scsi_pkt for a REQUEST SENSE command. 16274 */ 16275 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 16276 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 16277 if (un->un_rqs_bp == NULL) { 16278 return (DDI_FAILURE); 16279 } 16280 16281 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 16282 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL); 16283 16284 if (un->un_rqs_pktp == NULL) { 16285 sd_free_rqs(un); 16286 return (DDI_FAILURE); 16287 } 16288 16289 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */ 16290 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp, 16291 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 16292 16293 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp); 16294 16295 /* Set up the other needed members in the ARQ scsi_pkt. */ 16296 un->un_rqs_pktp->pkt_comp = sdintr; 16297 un->un_rqs_pktp->pkt_time = sd_io_time; 16298 un->un_rqs_pktp->pkt_flags |= 16299 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */ 16300 16301 /* 16302 * Allocate & init the sd_xbuf struct for the RQS command. Do not 16303 * provide any intpkt, destroypkt routines as we take care of 16304 * scsi_pkt allocation/freeing here and in sd_free_rqs(). 16305 */ 16306 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP); 16307 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL); 16308 xp->xb_pktp = un->un_rqs_pktp; 16309 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16310 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n", 16311 un, xp, un->un_rqs_pktp, un->un_rqs_bp); 16312 16313 /* 16314 * Save the pointer to the request sense private bp so it can 16315 * be retrieved in sdintr. 16316 */ 16317 un->un_rqs_pktp->pkt_private = un->un_rqs_bp; 16318 ASSERT(un->un_rqs_bp->b_private == xp); 16319 16320 /* 16321 * See if the HBA supports auto-request sense for the specified 16322 * target/lun. If it does, then try to enable it (if not already 16323 * enabled). 16324 * 16325 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return 16326 * failure, while for other HBAs (pln) scsi_ifsetcap will always 16327 * return success. However, in both of these cases ARQ is always 16328 * enabled and scsi_ifgetcap will always return true. The best approach 16329 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap(). 16330 * 16331 * The 3rd case is the HBA (adp) always return enabled on 16332 * scsi_ifgetgetcap even when it's not enable, the best approach 16333 * is issue a scsi_ifsetcap then a scsi_ifgetcap 16334 * Note: this case is to circumvent the Adaptec bug. (x86 only) 16335 */ 16336 16337 if (un->un_f_is_fibre == TRUE) { 16338 un->un_f_arq_enabled = TRUE; 16339 } else { 16340 #if defined(__i386) || defined(__amd64) 16341 /* 16342 * Circumvent the Adaptec bug, remove this code when 16343 * the bug is fixed 16344 */ 16345 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1); 16346 #endif 16347 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) { 16348 case 0: 16349 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16350 "sd_alloc_rqs: HBA supports ARQ\n"); 16351 /* 16352 * ARQ is supported by this HBA but currently is not 16353 * enabled. Attempt to enable it and if successful then 16354 * mark this instance as ARQ enabled. 16355 */ 16356 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1) 16357 == 1) { 16358 /* Successfully enabled ARQ in the HBA */ 16359 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16360 "sd_alloc_rqs: ARQ enabled\n"); 16361 un->un_f_arq_enabled = TRUE; 16362 } else { 16363 /* Could not enable ARQ in the HBA */ 16364 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16365 "sd_alloc_rqs: failed ARQ enable\n"); 16366 un->un_f_arq_enabled = FALSE; 16367 } 16368 break; 16369 case 1: 16370 /* 16371 * ARQ is supported by this HBA and is already enabled. 16372 * Just mark ARQ as enabled for this instance. 16373 */ 16374 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16375 "sd_alloc_rqs: ARQ already enabled\n"); 16376 un->un_f_arq_enabled = TRUE; 16377 break; 16378 default: 16379 /* 16380 * ARQ is not supported by this HBA; disable it for this 16381 * instance. 16382 */ 16383 SD_INFO(SD_LOG_ATTACH_DETACH, un, 16384 "sd_alloc_rqs: HBA does not support ARQ\n"); 16385 un->un_f_arq_enabled = FALSE; 16386 break; 16387 } 16388 } 16389 16390 return (DDI_SUCCESS); 16391 } 16392 16393 16394 /* 16395 * Function: sd_free_rqs 16396 * 16397 * Description: Cleanup for the pre-instance RQS command. 16398 * 16399 * Context: Kernel thread context 16400 */ 16401 16402 static void 16403 sd_free_rqs(struct sd_lun *un) 16404 { 16405 ASSERT(un != NULL); 16406 16407 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n"); 16408 16409 /* 16410 * If consistent memory is bound to a scsi_pkt, the pkt 16411 * has to be destroyed *before* freeing the consistent memory. 16412 * Don't change the sequence of this operations. 16413 * scsi_destroy_pkt() might access memory, which isn't allowed, 16414 * after it was freed in scsi_free_consistent_buf(). 16415 */ 16416 if (un->un_rqs_pktp != NULL) { 16417 scsi_destroy_pkt(un->un_rqs_pktp); 16418 un->un_rqs_pktp = NULL; 16419 } 16420 16421 if (un->un_rqs_bp != NULL) { 16422 kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf)); 16423 scsi_free_consistent_buf(un->un_rqs_bp); 16424 un->un_rqs_bp = NULL; 16425 } 16426 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n"); 16427 } 16428 16429 16430 16431 /* 16432 * Function: sd_reduce_throttle 16433 * 16434 * Description: Reduces the maximun # of outstanding commands on a 16435 * target to the current number of outstanding commands. 16436 * Queues a tiemout(9F) callback to restore the limit 16437 * after a specified interval has elapsed. 16438 * Typically used when we get a TRAN_BUSY return code 16439 * back from scsi_transport(). 16440 * 16441 * Arguments: un - ptr to the sd_lun softstate struct 16442 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL 16443 * 16444 * Context: May be called from interrupt context 16445 */ 16446 16447 static void 16448 sd_reduce_throttle(struct sd_lun *un, int throttle_type) 16449 { 16450 ASSERT(un != NULL); 16451 ASSERT(mutex_owned(SD_MUTEX(un))); 16452 ASSERT(un->un_ncmds_in_transport >= 0); 16453 16454 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16455 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n", 16456 un, un->un_throttle, un->un_ncmds_in_transport); 16457 16458 if (un->un_throttle > 1) { 16459 if (un->un_f_use_adaptive_throttle == TRUE) { 16460 switch (throttle_type) { 16461 case SD_THROTTLE_TRAN_BUSY: 16462 if (un->un_busy_throttle == 0) { 16463 un->un_busy_throttle = un->un_throttle; 16464 } 16465 break; 16466 case SD_THROTTLE_QFULL: 16467 un->un_busy_throttle = 0; 16468 break; 16469 default: 16470 ASSERT(FALSE); 16471 } 16472 16473 if (un->un_ncmds_in_transport > 0) { 16474 un->un_throttle = un->un_ncmds_in_transport; 16475 } 16476 16477 } else { 16478 if (un->un_ncmds_in_transport == 0) { 16479 un->un_throttle = 1; 16480 } else { 16481 un->un_throttle = un->un_ncmds_in_transport; 16482 } 16483 } 16484 } 16485 16486 /* Reschedule the timeout if none is currently active */ 16487 if (un->un_reset_throttle_timeid == NULL) { 16488 un->un_reset_throttle_timeid = timeout(sd_restore_throttle, 16489 un, SD_THROTTLE_RESET_INTERVAL); 16490 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16491 "sd_reduce_throttle: timeout scheduled!\n"); 16492 } 16493 16494 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: " 16495 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16496 } 16497 16498 16499 16500 /* 16501 * Function: sd_restore_throttle 16502 * 16503 * Description: Callback function for timeout(9F). Resets the current 16504 * value of un->un_throttle to its default. 16505 * 16506 * Arguments: arg - pointer to associated softstate for the device. 16507 * 16508 * Context: May be called from interrupt context 16509 */ 16510 16511 static void 16512 sd_restore_throttle(void *arg) 16513 { 16514 struct sd_lun *un = arg; 16515 16516 ASSERT(un != NULL); 16517 ASSERT(!mutex_owned(SD_MUTEX(un))); 16518 16519 mutex_enter(SD_MUTEX(un)); 16520 16521 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16522 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle); 16523 16524 un->un_reset_throttle_timeid = NULL; 16525 16526 if (un->un_f_use_adaptive_throttle == TRUE) { 16527 /* 16528 * If un_busy_throttle is nonzero, then it contains the 16529 * value that un_throttle was when we got a TRAN_BUSY back 16530 * from scsi_transport(). We want to revert back to this 16531 * value. 16532 * 16533 * In the QFULL case, the throttle limit will incrementally 16534 * increase until it reaches max throttle. 16535 */ 16536 if (un->un_busy_throttle > 0) { 16537 un->un_throttle = un->un_busy_throttle; 16538 un->un_busy_throttle = 0; 16539 } else { 16540 /* 16541 * increase throttle by 10% open gate slowly, schedule 16542 * another restore if saved throttle has not been 16543 * reached 16544 */ 16545 short throttle; 16546 if (sd_qfull_throttle_enable) { 16547 throttle = un->un_throttle + 16548 max((un->un_throttle / 10), 1); 16549 un->un_throttle = 16550 (throttle < un->un_saved_throttle) ? 16551 throttle : un->un_saved_throttle; 16552 if (un->un_throttle < un->un_saved_throttle) { 16553 un->un_reset_throttle_timeid = 16554 timeout(sd_restore_throttle, 16555 un, SD_QFULL_THROTTLE_RESET_INTERVAL); 16556 } 16557 } 16558 } 16559 16560 /* 16561 * If un_throttle has fallen below the low-water mark, we 16562 * restore the maximum value here (and allow it to ratchet 16563 * down again if necessary). 16564 */ 16565 if (un->un_throttle < un->un_min_throttle) { 16566 un->un_throttle = un->un_saved_throttle; 16567 } 16568 } else { 16569 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: " 16570 "restoring limit from 0x%x to 0x%x\n", 16571 un->un_throttle, un->un_saved_throttle); 16572 un->un_throttle = un->un_saved_throttle; 16573 } 16574 16575 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16576 "sd_restore_throttle: calling sd_start_cmds!\n"); 16577 16578 sd_start_cmds(un, NULL); 16579 16580 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, 16581 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n", 16582 un, un->un_throttle); 16583 16584 mutex_exit(SD_MUTEX(un)); 16585 16586 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n"); 16587 } 16588 16589 /* 16590 * Function: sdrunout 16591 * 16592 * Description: Callback routine for scsi_init_pkt when a resource allocation 16593 * fails. 16594 * 16595 * Arguments: arg - a pointer to the sd_lun unit struct for the particular 16596 * soft state instance. 16597 * 16598 * Return Code: The scsi_init_pkt routine allows for the callback function to 16599 * return a 0 indicating the callback should be rescheduled or a 1 16600 * indicating not to reschedule. This routine always returns 1 16601 * because the driver always provides a callback function to 16602 * scsi_init_pkt. This results in a callback always being scheduled 16603 * (via the scsi_init_pkt callback implementation) if a resource 16604 * failure occurs. 16605 * 16606 * Context: This callback function may not block or call routines that block 16607 * 16608 * Note: Using the scsi_init_pkt callback facility can result in an I/O 16609 * request persisting at the head of the list which cannot be 16610 * satisfied even after multiple retries. In the future the driver 16611 * may implement some time of maximum runout count before failing 16612 * an I/O. 16613 */ 16614 16615 static int 16616 sdrunout(caddr_t arg) 16617 { 16618 struct sd_lun *un = (struct sd_lun *)arg; 16619 16620 ASSERT(un != NULL); 16621 ASSERT(!mutex_owned(SD_MUTEX(un))); 16622 16623 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n"); 16624 16625 mutex_enter(SD_MUTEX(un)); 16626 sd_start_cmds(un, NULL); 16627 mutex_exit(SD_MUTEX(un)); 16628 /* 16629 * This callback routine always returns 1 (i.e. do not reschedule) 16630 * because we always specify sdrunout as the callback handler for 16631 * scsi_init_pkt inside the call to sd_start_cmds. 16632 */ 16633 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n"); 16634 return (1); 16635 } 16636 16637 16638 /* 16639 * Function: sdintr 16640 * 16641 * Description: Completion callback routine for scsi_pkt(9S) structs 16642 * sent to the HBA driver via scsi_transport(9F). 16643 * 16644 * Context: Interrupt context 16645 */ 16646 16647 static void 16648 sdintr(struct scsi_pkt *pktp) 16649 { 16650 struct buf *bp; 16651 struct sd_xbuf *xp; 16652 struct sd_lun *un; 16653 16654 ASSERT(pktp != NULL); 16655 bp = (struct buf *)pktp->pkt_private; 16656 ASSERT(bp != NULL); 16657 xp = SD_GET_XBUF(bp); 16658 ASSERT(xp != NULL); 16659 ASSERT(xp->xb_pktp != NULL); 16660 un = SD_GET_UN(bp); 16661 ASSERT(un != NULL); 16662 ASSERT(!mutex_owned(SD_MUTEX(un))); 16663 16664 #ifdef SD_FAULT_INJECTION 16665 16666 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n"); 16667 /* SD FaultInjection */ 16668 sd_faultinjection(pktp); 16669 16670 #endif /* SD_FAULT_INJECTION */ 16671 16672 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p," 16673 " xp:0x%p, un:0x%p\n", bp, xp, un); 16674 16675 mutex_enter(SD_MUTEX(un)); 16676 16677 /* Reduce the count of the #commands currently in transport */ 16678 un->un_ncmds_in_transport--; 16679 ASSERT(un->un_ncmds_in_transport >= 0); 16680 16681 /* Increment counter to indicate that the callback routine is active */ 16682 un->un_in_callback++; 16683 16684 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp); 16685 16686 #ifdef SDDEBUG 16687 if (bp == un->un_retry_bp) { 16688 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: " 16689 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n", 16690 un, un->un_retry_bp, un->un_ncmds_in_transport); 16691 } 16692 #endif 16693 16694 /* 16695 * If pkt_reason is CMD_DEV_GONE, just fail the command 16696 */ 16697 if (pktp->pkt_reason == CMD_DEV_GONE) { 16698 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16699 "Device is gone\n"); 16700 sd_return_failed_command(un, bp, EIO); 16701 goto exit; 16702 } 16703 16704 /* 16705 * First see if the pkt has auto-request sense data with it.... 16706 * Look at the packet state first so we don't take a performance 16707 * hit looking at the arq enabled flag unless absolutely necessary. 16708 */ 16709 if ((pktp->pkt_state & STATE_ARQ_DONE) && 16710 (un->un_f_arq_enabled == TRUE)) { 16711 /* 16712 * The HBA did an auto request sense for this command so check 16713 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16714 * driver command that should not be retried. 16715 */ 16716 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16717 /* 16718 * Save the relevant sense info into the xp for the 16719 * original cmd. 16720 */ 16721 struct scsi_arq_status *asp; 16722 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 16723 xp->xb_sense_status = 16724 *((uchar_t *)(&(asp->sts_rqpkt_status))); 16725 xp->xb_sense_state = asp->sts_rqpkt_state; 16726 xp->xb_sense_resid = asp->sts_rqpkt_resid; 16727 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 16728 min(sizeof (struct scsi_extended_sense), 16729 SENSE_LENGTH)); 16730 16731 /* fail the command */ 16732 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16733 "sdintr: arq done and FLAG_DIAGNOSE set\n"); 16734 sd_return_failed_command(un, bp, EIO); 16735 goto exit; 16736 } 16737 16738 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16739 /* 16740 * We want to either retry or fail this command, so free 16741 * the DMA resources here. If we retry the command then 16742 * the DMA resources will be reallocated in sd_start_cmds(). 16743 * Note that when PKT_DMA_PARTIAL is used, this reallocation 16744 * causes the *entire* transfer to start over again from the 16745 * beginning of the request, even for PARTIAL chunks that 16746 * have already transferred successfully. 16747 */ 16748 if ((un->un_f_is_fibre == TRUE) && 16749 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16750 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16751 scsi_dmafree(pktp); 16752 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16753 } 16754 #endif 16755 16756 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16757 "sdintr: arq done, sd_handle_auto_request_sense\n"); 16758 16759 sd_handle_auto_request_sense(un, bp, xp, pktp); 16760 goto exit; 16761 } 16762 16763 /* Next see if this is the REQUEST SENSE pkt for the instance */ 16764 if (pktp->pkt_flags & FLAG_SENSING) { 16765 /* This pktp is from the unit's REQUEST_SENSE command */ 16766 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16767 "sdintr: sd_handle_request_sense\n"); 16768 sd_handle_request_sense(un, bp, xp, pktp); 16769 goto exit; 16770 } 16771 16772 /* 16773 * Check to see if the command successfully completed as requested; 16774 * this is the most common case (and also the hot performance path). 16775 * 16776 * Requirements for successful completion are: 16777 * pkt_reason is CMD_CMPLT and packet status is status good. 16778 * In addition: 16779 * - A residual of zero indicates successful completion no matter what 16780 * the command is. 16781 * - If the residual is not zero and the command is not a read or 16782 * write, then it's still defined as successful completion. In other 16783 * words, if the command is a read or write the residual must be 16784 * zero for successful completion. 16785 * - If the residual is not zero and the command is a read or 16786 * write, and it's a USCSICMD, then it's still defined as 16787 * successful completion. 16788 */ 16789 if ((pktp->pkt_reason == CMD_CMPLT) && 16790 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) { 16791 16792 /* 16793 * Since this command is returned with a good status, we 16794 * can reset the count for Sonoma failover. 16795 */ 16796 un->un_sonoma_failure_count = 0; 16797 16798 /* 16799 * Return all USCSI commands on good status 16800 */ 16801 if (pktp->pkt_resid == 0) { 16802 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16803 "sdintr: returning command for resid == 0\n"); 16804 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) && 16805 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) { 16806 SD_UPDATE_B_RESID(bp, pktp); 16807 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16808 "sdintr: returning command for resid != 0\n"); 16809 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) { 16810 SD_UPDATE_B_RESID(bp, pktp); 16811 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16812 "sdintr: returning uscsi command\n"); 16813 } else { 16814 goto not_successful; 16815 } 16816 sd_return_command(un, bp); 16817 16818 /* 16819 * Decrement counter to indicate that the callback routine 16820 * is done. 16821 */ 16822 un->un_in_callback--; 16823 ASSERT(un->un_in_callback >= 0); 16824 mutex_exit(SD_MUTEX(un)); 16825 16826 return; 16827 } 16828 16829 not_successful: 16830 16831 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */ 16832 /* 16833 * The following is based upon knowledge of the underlying transport 16834 * and its use of DMA resources. This code should be removed when 16835 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor 16836 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf() 16837 * and sd_start_cmds(). 16838 * 16839 * Free any DMA resources associated with this command if there 16840 * is a chance it could be retried or enqueued for later retry. 16841 * If we keep the DMA binding then mpxio cannot reissue the 16842 * command on another path whenever a path failure occurs. 16843 * 16844 * Note that when PKT_DMA_PARTIAL is used, free/reallocation 16845 * causes the *entire* transfer to start over again from the 16846 * beginning of the request, even for PARTIAL chunks that 16847 * have already transferred successfully. 16848 * 16849 * This is only done for non-uscsi commands (and also skipped for the 16850 * driver's internal RQS command). Also just do this for Fibre Channel 16851 * devices as these are the only ones that support mpxio. 16852 */ 16853 if ((un->un_f_is_fibre == TRUE) && 16854 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) && 16855 ((pktp->pkt_flags & FLAG_SENSING) == 0)) { 16856 scsi_dmafree(pktp); 16857 xp->xb_pkt_flags |= SD_XB_DMA_FREED; 16858 } 16859 #endif 16860 16861 /* 16862 * The command did not successfully complete as requested so check 16863 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal 16864 * driver command that should not be retried so just return. If 16865 * FLAG_DIAGNOSE is not set the error will be processed below. 16866 */ 16867 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) { 16868 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16869 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n"); 16870 /* 16871 * Issue a request sense if a check condition caused the error 16872 * (we handle the auto request sense case above), otherwise 16873 * just fail the command. 16874 */ 16875 if ((pktp->pkt_reason == CMD_CMPLT) && 16876 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) { 16877 sd_send_request_sense_command(un, bp, pktp); 16878 } else { 16879 sd_return_failed_command(un, bp, EIO); 16880 } 16881 goto exit; 16882 } 16883 16884 /* 16885 * The command did not successfully complete as requested so process 16886 * the error, retry, and/or attempt recovery. 16887 */ 16888 switch (pktp->pkt_reason) { 16889 case CMD_CMPLT: 16890 switch (SD_GET_PKT_STATUS(pktp)) { 16891 case STATUS_GOOD: 16892 /* 16893 * The command completed successfully with a non-zero 16894 * residual 16895 */ 16896 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16897 "sdintr: STATUS_GOOD \n"); 16898 sd_pkt_status_good(un, bp, xp, pktp); 16899 break; 16900 16901 case STATUS_CHECK: 16902 case STATUS_TERMINATED: 16903 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16904 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n"); 16905 sd_pkt_status_check_condition(un, bp, xp, pktp); 16906 break; 16907 16908 case STATUS_BUSY: 16909 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16910 "sdintr: STATUS_BUSY\n"); 16911 sd_pkt_status_busy(un, bp, xp, pktp); 16912 break; 16913 16914 case STATUS_RESERVATION_CONFLICT: 16915 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16916 "sdintr: STATUS_RESERVATION_CONFLICT\n"); 16917 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 16918 break; 16919 16920 case STATUS_QFULL: 16921 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16922 "sdintr: STATUS_QFULL\n"); 16923 sd_pkt_status_qfull(un, bp, xp, pktp); 16924 break; 16925 16926 case STATUS_MET: 16927 case STATUS_INTERMEDIATE: 16928 case STATUS_SCSI2: 16929 case STATUS_INTERMEDIATE_MET: 16930 case STATUS_ACA_ACTIVE: 16931 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16932 "Unexpected SCSI status received: 0x%x\n", 16933 SD_GET_PKT_STATUS(pktp)); 16934 sd_return_failed_command(un, bp, EIO); 16935 break; 16936 16937 default: 16938 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 16939 "Invalid SCSI status received: 0x%x\n", 16940 SD_GET_PKT_STATUS(pktp)); 16941 sd_return_failed_command(un, bp, EIO); 16942 break; 16943 16944 } 16945 break; 16946 16947 case CMD_INCOMPLETE: 16948 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16949 "sdintr: CMD_INCOMPLETE\n"); 16950 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp); 16951 break; 16952 case CMD_TRAN_ERR: 16953 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16954 "sdintr: CMD_TRAN_ERR\n"); 16955 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp); 16956 break; 16957 case CMD_RESET: 16958 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16959 "sdintr: CMD_RESET \n"); 16960 sd_pkt_reason_cmd_reset(un, bp, xp, pktp); 16961 break; 16962 case CMD_ABORTED: 16963 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16964 "sdintr: CMD_ABORTED \n"); 16965 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp); 16966 break; 16967 case CMD_TIMEOUT: 16968 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16969 "sdintr: CMD_TIMEOUT\n"); 16970 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp); 16971 break; 16972 case CMD_UNX_BUS_FREE: 16973 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16974 "sdintr: CMD_UNX_BUS_FREE \n"); 16975 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp); 16976 break; 16977 case CMD_TAG_REJECT: 16978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16979 "sdintr: CMD_TAG_REJECT\n"); 16980 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp); 16981 break; 16982 default: 16983 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 16984 "sdintr: default\n"); 16985 sd_pkt_reason_default(un, bp, xp, pktp); 16986 break; 16987 } 16988 16989 exit: 16990 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n"); 16991 16992 /* Decrement counter to indicate that the callback routine is done. */ 16993 un->un_in_callback--; 16994 ASSERT(un->un_in_callback >= 0); 16995 16996 /* 16997 * At this point, the pkt has been dispatched, ie, it is either 16998 * being re-tried or has been returned to its caller and should 16999 * not be referenced. 17000 */ 17001 17002 mutex_exit(SD_MUTEX(un)); 17003 } 17004 17005 17006 /* 17007 * Function: sd_print_incomplete_msg 17008 * 17009 * Description: Prints the error message for a CMD_INCOMPLETE error. 17010 * 17011 * Arguments: un - ptr to associated softstate for the device. 17012 * bp - ptr to the buf(9S) for the command. 17013 * arg - message string ptr 17014 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED, 17015 * or SD_NO_RETRY_ISSUED. 17016 * 17017 * Context: May be called under interrupt context 17018 */ 17019 17020 static void 17021 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17022 { 17023 struct scsi_pkt *pktp; 17024 char *msgp; 17025 char *cmdp = arg; 17026 17027 ASSERT(un != NULL); 17028 ASSERT(mutex_owned(SD_MUTEX(un))); 17029 ASSERT(bp != NULL); 17030 ASSERT(arg != NULL); 17031 pktp = SD_GET_PKTP(bp); 17032 ASSERT(pktp != NULL); 17033 17034 switch (code) { 17035 case SD_DELAYED_RETRY_ISSUED: 17036 case SD_IMMEDIATE_RETRY_ISSUED: 17037 msgp = "retrying"; 17038 break; 17039 case SD_NO_RETRY_ISSUED: 17040 default: 17041 msgp = "giving up"; 17042 break; 17043 } 17044 17045 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17046 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17047 "incomplete %s- %s\n", cmdp, msgp); 17048 } 17049 } 17050 17051 17052 17053 /* 17054 * Function: sd_pkt_status_good 17055 * 17056 * Description: Processing for a STATUS_GOOD code in pkt_status. 17057 * 17058 * Context: May be called under interrupt context 17059 */ 17060 17061 static void 17062 sd_pkt_status_good(struct sd_lun *un, struct buf *bp, 17063 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17064 { 17065 char *cmdp; 17066 17067 ASSERT(un != NULL); 17068 ASSERT(mutex_owned(SD_MUTEX(un))); 17069 ASSERT(bp != NULL); 17070 ASSERT(xp != NULL); 17071 ASSERT(pktp != NULL); 17072 ASSERT(pktp->pkt_reason == CMD_CMPLT); 17073 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD); 17074 ASSERT(pktp->pkt_resid != 0); 17075 17076 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n"); 17077 17078 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17079 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) { 17080 case SCMD_READ: 17081 cmdp = "read"; 17082 break; 17083 case SCMD_WRITE: 17084 cmdp = "write"; 17085 break; 17086 default: 17087 SD_UPDATE_B_RESID(bp, pktp); 17088 sd_return_command(un, bp); 17089 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17090 return; 17091 } 17092 17093 /* 17094 * See if we can retry the read/write, preferrably immediately. 17095 * If retries are exhaused, then sd_retry_command() will update 17096 * the b_resid count. 17097 */ 17098 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg, 17099 cmdp, EIO, (clock_t)0, NULL); 17100 17101 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n"); 17102 } 17103 17104 17105 17106 17107 17108 /* 17109 * Function: sd_handle_request_sense 17110 * 17111 * Description: Processing for non-auto Request Sense command. 17112 * 17113 * Arguments: un - ptr to associated softstate 17114 * sense_bp - ptr to buf(9S) for the RQS command 17115 * sense_xp - ptr to the sd_xbuf for the RQS command 17116 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command 17117 * 17118 * Context: May be called under interrupt context 17119 */ 17120 17121 static void 17122 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp, 17123 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp) 17124 { 17125 struct buf *cmd_bp; /* buf for the original command */ 17126 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */ 17127 struct scsi_pkt *cmd_pktp; /* pkt for the original command */ 17128 17129 ASSERT(un != NULL); 17130 ASSERT(mutex_owned(SD_MUTEX(un))); 17131 ASSERT(sense_bp != NULL); 17132 ASSERT(sense_xp != NULL); 17133 ASSERT(sense_pktp != NULL); 17134 17135 /* 17136 * Note the sense_bp, sense_xp, and sense_pktp here are for the 17137 * RQS command and not the original command. 17138 */ 17139 ASSERT(sense_pktp == un->un_rqs_pktp); 17140 ASSERT(sense_bp == un->un_rqs_bp); 17141 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) == 17142 (FLAG_SENSING | FLAG_HEAD)); 17143 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) & 17144 FLAG_SENSING) == FLAG_SENSING); 17145 17146 /* These are the bp, xp, and pktp for the original command */ 17147 cmd_bp = sense_xp->xb_sense_bp; 17148 cmd_xp = SD_GET_XBUF(cmd_bp); 17149 cmd_pktp = SD_GET_PKTP(cmd_bp); 17150 17151 if (sense_pktp->pkt_reason != CMD_CMPLT) { 17152 /* 17153 * The REQUEST SENSE command failed. Release the REQUEST 17154 * SENSE command for re-use, get back the bp for the original 17155 * command, and attempt to re-try the original command if 17156 * FLAG_DIAGNOSE is not set in the original packet. 17157 */ 17158 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17159 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17160 cmd_bp = sd_mark_rqs_idle(un, sense_xp); 17161 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD, 17162 NULL, NULL, EIO, (clock_t)0, NULL); 17163 return; 17164 } 17165 } 17166 17167 /* 17168 * Save the relevant sense info into the xp for the original cmd. 17169 * 17170 * Note: if the request sense failed the state info will be zero 17171 * as set in sd_mark_rqs_busy() 17172 */ 17173 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp); 17174 cmd_xp->xb_sense_state = sense_pktp->pkt_state; 17175 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid; 17176 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH); 17177 17178 /* 17179 * Free up the RQS command.... 17180 * NOTE: 17181 * Must do this BEFORE calling sd_validate_sense_data! 17182 * sd_validate_sense_data may return the original command in 17183 * which case the pkt will be freed and the flags can no 17184 * longer be touched. 17185 * SD_MUTEX is held through this process until the command 17186 * is dispatched based upon the sense data, so there are 17187 * no race conditions. 17188 */ 17189 (void) sd_mark_rqs_idle(un, sense_xp); 17190 17191 /* 17192 * For a retryable command see if we have valid sense data, if so then 17193 * turn it over to sd_decode_sense() to figure out the right course of 17194 * action. Just fail a non-retryable command. 17195 */ 17196 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) { 17197 if (sd_validate_sense_data(un, cmd_bp, cmd_xp) == 17198 SD_SENSE_DATA_IS_VALID) { 17199 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp); 17200 } 17201 } else { 17202 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB", 17203 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17204 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data", 17205 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX); 17206 sd_return_failed_command(un, cmd_bp, EIO); 17207 } 17208 } 17209 17210 17211 17212 17213 /* 17214 * Function: sd_handle_auto_request_sense 17215 * 17216 * Description: Processing for auto-request sense information. 17217 * 17218 * Arguments: un - ptr to associated softstate 17219 * bp - ptr to buf(9S) for the command 17220 * xp - ptr to the sd_xbuf for the command 17221 * pktp - ptr to the scsi_pkt(9S) for the command 17222 * 17223 * Context: May be called under interrupt context 17224 */ 17225 17226 static void 17227 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp, 17228 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17229 { 17230 struct scsi_arq_status *asp; 17231 17232 ASSERT(un != NULL); 17233 ASSERT(mutex_owned(SD_MUTEX(un))); 17234 ASSERT(bp != NULL); 17235 ASSERT(xp != NULL); 17236 ASSERT(pktp != NULL); 17237 ASSERT(pktp != un->un_rqs_pktp); 17238 ASSERT(bp != un->un_rqs_bp); 17239 17240 /* 17241 * For auto-request sense, we get a scsi_arq_status back from 17242 * the HBA, with the sense data in the sts_sensedata member. 17243 * The pkt_scbp of the packet points to this scsi_arq_status. 17244 */ 17245 asp = (struct scsi_arq_status *)(pktp->pkt_scbp); 17246 17247 if (asp->sts_rqpkt_reason != CMD_CMPLT) { 17248 /* 17249 * The auto REQUEST SENSE failed; see if we can re-try 17250 * the original command. 17251 */ 17252 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17253 "auto request sense failed (reason=%s)\n", 17254 scsi_rname(asp->sts_rqpkt_reason)); 17255 17256 sd_reset_target(un, pktp); 17257 17258 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17259 NULL, NULL, EIO, (clock_t)0, NULL); 17260 return; 17261 } 17262 17263 /* Save the relevant sense info into the xp for the original cmd. */ 17264 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status))); 17265 xp->xb_sense_state = asp->sts_rqpkt_state; 17266 xp->xb_sense_resid = asp->sts_rqpkt_resid; 17267 bcopy(&asp->sts_sensedata, xp->xb_sense_data, 17268 min(sizeof (struct scsi_extended_sense), SENSE_LENGTH)); 17269 17270 /* 17271 * See if we have valid sense data, if so then turn it over to 17272 * sd_decode_sense() to figure out the right course of action. 17273 */ 17274 if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) { 17275 sd_decode_sense(un, bp, xp, pktp); 17276 } 17277 } 17278 17279 17280 /* 17281 * Function: sd_print_sense_failed_msg 17282 * 17283 * Description: Print log message when RQS has failed. 17284 * 17285 * Arguments: un - ptr to associated softstate 17286 * bp - ptr to buf(9S) for the command 17287 * arg - generic message string ptr 17288 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17289 * or SD_NO_RETRY_ISSUED 17290 * 17291 * Context: May be called from interrupt context 17292 */ 17293 17294 static void 17295 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg, 17296 int code) 17297 { 17298 char *msgp = arg; 17299 17300 ASSERT(un != NULL); 17301 ASSERT(mutex_owned(SD_MUTEX(un))); 17302 ASSERT(bp != NULL); 17303 17304 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) { 17305 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp); 17306 } 17307 } 17308 17309 17310 /* 17311 * Function: sd_validate_sense_data 17312 * 17313 * Description: Check the given sense data for validity. 17314 * If the sense data is not valid, the command will 17315 * be either failed or retried! 17316 * 17317 * Return Code: SD_SENSE_DATA_IS_INVALID 17318 * SD_SENSE_DATA_IS_VALID 17319 * 17320 * Context: May be called from interrupt context 17321 */ 17322 17323 static int 17324 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp) 17325 { 17326 struct scsi_extended_sense *esp; 17327 struct scsi_pkt *pktp; 17328 size_t actual_len; 17329 char *msgp = NULL; 17330 17331 ASSERT(un != NULL); 17332 ASSERT(mutex_owned(SD_MUTEX(un))); 17333 ASSERT(bp != NULL); 17334 ASSERT(bp != un->un_rqs_bp); 17335 ASSERT(xp != NULL); 17336 17337 pktp = SD_GET_PKTP(bp); 17338 ASSERT(pktp != NULL); 17339 17340 /* 17341 * Check the status of the RQS command (auto or manual). 17342 */ 17343 switch (xp->xb_sense_status & STATUS_MASK) { 17344 case STATUS_GOOD: 17345 break; 17346 17347 case STATUS_RESERVATION_CONFLICT: 17348 sd_pkt_status_reservation_conflict(un, bp, xp, pktp); 17349 return (SD_SENSE_DATA_IS_INVALID); 17350 17351 case STATUS_BUSY: 17352 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17353 "Busy Status on REQUEST SENSE\n"); 17354 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL, 17355 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17356 return (SD_SENSE_DATA_IS_INVALID); 17357 17358 case STATUS_QFULL: 17359 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17360 "QFULL Status on REQUEST SENSE\n"); 17361 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, 17362 NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter); 17363 return (SD_SENSE_DATA_IS_INVALID); 17364 17365 case STATUS_CHECK: 17366 case STATUS_TERMINATED: 17367 msgp = "Check Condition on REQUEST SENSE\n"; 17368 goto sense_failed; 17369 17370 default: 17371 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n"; 17372 goto sense_failed; 17373 } 17374 17375 /* 17376 * See if we got the minimum required amount of sense data. 17377 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes 17378 * or less. 17379 */ 17380 actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid); 17381 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) || 17382 (actual_len == 0)) { 17383 msgp = "Request Sense couldn't get sense data\n"; 17384 goto sense_failed; 17385 } 17386 17387 if (actual_len < SUN_MIN_SENSE_LENGTH) { 17388 msgp = "Not enough sense information\n"; 17389 goto sense_failed; 17390 } 17391 17392 /* 17393 * We require the extended sense data 17394 */ 17395 esp = (struct scsi_extended_sense *)xp->xb_sense_data; 17396 if (esp->es_class != CLASS_EXTENDED_SENSE) { 17397 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 17398 static char tmp[8]; 17399 static char buf[148]; 17400 char *p = (char *)(xp->xb_sense_data); 17401 int i; 17402 17403 mutex_enter(&sd_sense_mutex); 17404 (void) strcpy(buf, "undecodable sense information:"); 17405 for (i = 0; i < actual_len; i++) { 17406 (void) sprintf(tmp, " 0x%x", *(p++)&0xff); 17407 (void) strcpy(&buf[strlen(buf)], tmp); 17408 } 17409 i = strlen(buf); 17410 (void) strcpy(&buf[i], "-(assumed fatal)\n"); 17411 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf); 17412 mutex_exit(&sd_sense_mutex); 17413 } 17414 /* Note: Legacy behavior, fail the command with no retry */ 17415 sd_return_failed_command(un, bp, EIO); 17416 return (SD_SENSE_DATA_IS_INVALID); 17417 } 17418 17419 /* 17420 * Check that es_code is valid (es_class concatenated with es_code 17421 * make up the "response code" field. es_class will always be 7, so 17422 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the 17423 * format. 17424 */ 17425 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) && 17426 (esp->es_code != CODE_FMT_FIXED_DEFERRED) && 17427 (esp->es_code != CODE_FMT_DESCR_CURRENT) && 17428 (esp->es_code != CODE_FMT_DESCR_DEFERRED) && 17429 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) { 17430 goto sense_failed; 17431 } 17432 17433 return (SD_SENSE_DATA_IS_VALID); 17434 17435 sense_failed: 17436 /* 17437 * If the request sense failed (for whatever reason), attempt 17438 * to retry the original command. 17439 */ 17440 #if defined(__i386) || defined(__amd64) 17441 /* 17442 * SD_RETRY_DELAY is conditionally compile (#if fibre) in 17443 * sddef.h for Sparc platform, and x86 uses 1 binary 17444 * for both SCSI/FC. 17445 * The SD_RETRY_DELAY value need to be adjusted here 17446 * when SD_RETRY_DELAY change in sddef.h 17447 */ 17448 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17449 sd_print_sense_failed_msg, msgp, EIO, 17450 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL); 17451 #else 17452 sd_retry_command(un, bp, SD_RETRIES_STANDARD, 17453 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL); 17454 #endif 17455 17456 return (SD_SENSE_DATA_IS_INVALID); 17457 } 17458 17459 17460 17461 /* 17462 * Function: sd_decode_sense 17463 * 17464 * Description: Take recovery action(s) when SCSI Sense Data is received. 17465 * 17466 * Context: Interrupt context. 17467 */ 17468 17469 static void 17470 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 17471 struct scsi_pkt *pktp) 17472 { 17473 uint8_t sense_key; 17474 17475 ASSERT(un != NULL); 17476 ASSERT(mutex_owned(SD_MUTEX(un))); 17477 ASSERT(bp != NULL); 17478 ASSERT(bp != un->un_rqs_bp); 17479 ASSERT(xp != NULL); 17480 ASSERT(pktp != NULL); 17481 17482 sense_key = scsi_sense_key(xp->xb_sense_data); 17483 17484 switch (sense_key) { 17485 case KEY_NO_SENSE: 17486 sd_sense_key_no_sense(un, bp, xp, pktp); 17487 break; 17488 case KEY_RECOVERABLE_ERROR: 17489 sd_sense_key_recoverable_error(un, xp->xb_sense_data, 17490 bp, xp, pktp); 17491 break; 17492 case KEY_NOT_READY: 17493 sd_sense_key_not_ready(un, xp->xb_sense_data, 17494 bp, xp, pktp); 17495 break; 17496 case KEY_MEDIUM_ERROR: 17497 case KEY_HARDWARE_ERROR: 17498 sd_sense_key_medium_or_hardware_error(un, 17499 xp->xb_sense_data, bp, xp, pktp); 17500 break; 17501 case KEY_ILLEGAL_REQUEST: 17502 sd_sense_key_illegal_request(un, bp, xp, pktp); 17503 break; 17504 case KEY_UNIT_ATTENTION: 17505 sd_sense_key_unit_attention(un, xp->xb_sense_data, 17506 bp, xp, pktp); 17507 break; 17508 case KEY_WRITE_PROTECT: 17509 case KEY_VOLUME_OVERFLOW: 17510 case KEY_MISCOMPARE: 17511 sd_sense_key_fail_command(un, bp, xp, pktp); 17512 break; 17513 case KEY_BLANK_CHECK: 17514 sd_sense_key_blank_check(un, bp, xp, pktp); 17515 break; 17516 case KEY_ABORTED_COMMAND: 17517 sd_sense_key_aborted_command(un, bp, xp, pktp); 17518 break; 17519 case KEY_VENDOR_UNIQUE: 17520 case KEY_COPY_ABORTED: 17521 case KEY_EQUAL: 17522 case KEY_RESERVED: 17523 default: 17524 sd_sense_key_default(un, xp->xb_sense_data, 17525 bp, xp, pktp); 17526 break; 17527 } 17528 } 17529 17530 17531 /* 17532 * Function: sd_dump_memory 17533 * 17534 * Description: Debug logging routine to print the contents of a user provided 17535 * buffer. The output of the buffer is broken up into 256 byte 17536 * segments due to a size constraint of the scsi_log. 17537 * implementation. 17538 * 17539 * Arguments: un - ptr to softstate 17540 * comp - component mask 17541 * title - "title" string to preceed data when printed 17542 * data - ptr to data block to be printed 17543 * len - size of data block to be printed 17544 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c) 17545 * 17546 * Context: May be called from interrupt context 17547 */ 17548 17549 #define SD_DUMP_MEMORY_BUF_SIZE 256 17550 17551 static char *sd_dump_format_string[] = { 17552 " 0x%02x", 17553 " %c" 17554 }; 17555 17556 static void 17557 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data, 17558 int len, int fmt) 17559 { 17560 int i, j; 17561 int avail_count; 17562 int start_offset; 17563 int end_offset; 17564 size_t entry_len; 17565 char *bufp; 17566 char *local_buf; 17567 char *format_string; 17568 17569 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR)); 17570 17571 /* 17572 * In the debug version of the driver, this function is called from a 17573 * number of places which are NOPs in the release driver. 17574 * The debug driver therefore has additional methods of filtering 17575 * debug output. 17576 */ 17577 #ifdef SDDEBUG 17578 /* 17579 * In the debug version of the driver we can reduce the amount of debug 17580 * messages by setting sd_error_level to something other than 17581 * SCSI_ERR_ALL and clearing bits in sd_level_mask and 17582 * sd_component_mask. 17583 */ 17584 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) || 17585 (sd_error_level != SCSI_ERR_ALL)) { 17586 return; 17587 } 17588 if (((sd_component_mask & comp) == 0) || 17589 (sd_error_level != SCSI_ERR_ALL)) { 17590 return; 17591 } 17592 #else 17593 if (sd_error_level != SCSI_ERR_ALL) { 17594 return; 17595 } 17596 #endif 17597 17598 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP); 17599 bufp = local_buf; 17600 /* 17601 * Available length is the length of local_buf[], minus the 17602 * length of the title string, minus one for the ":", minus 17603 * one for the newline, minus one for the NULL terminator. 17604 * This gives the #bytes available for holding the printed 17605 * values from the given data buffer. 17606 */ 17607 if (fmt == SD_LOG_HEX) { 17608 format_string = sd_dump_format_string[0]; 17609 } else /* SD_LOG_CHAR */ { 17610 format_string = sd_dump_format_string[1]; 17611 } 17612 /* 17613 * Available count is the number of elements from the given 17614 * data buffer that we can fit into the available length. 17615 * This is based upon the size of the format string used. 17616 * Make one entry and find it's size. 17617 */ 17618 (void) sprintf(bufp, format_string, data[0]); 17619 entry_len = strlen(bufp); 17620 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len; 17621 17622 j = 0; 17623 while (j < len) { 17624 bufp = local_buf; 17625 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE); 17626 start_offset = j; 17627 17628 end_offset = start_offset + avail_count; 17629 17630 (void) sprintf(bufp, "%s:", title); 17631 bufp += strlen(bufp); 17632 for (i = start_offset; ((i < end_offset) && (j < len)); 17633 i++, j++) { 17634 (void) sprintf(bufp, format_string, data[i]); 17635 bufp += entry_len; 17636 } 17637 (void) sprintf(bufp, "\n"); 17638 17639 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf); 17640 } 17641 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE); 17642 } 17643 17644 /* 17645 * Function: sd_print_sense_msg 17646 * 17647 * Description: Log a message based upon the given sense data. 17648 * 17649 * Arguments: un - ptr to associated softstate 17650 * bp - ptr to buf(9S) for the command 17651 * arg - ptr to associate sd_sense_info struct 17652 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 17653 * or SD_NO_RETRY_ISSUED 17654 * 17655 * Context: May be called from interrupt context 17656 */ 17657 17658 static void 17659 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code) 17660 { 17661 struct sd_xbuf *xp; 17662 struct scsi_pkt *pktp; 17663 uint8_t *sensep; 17664 daddr_t request_blkno; 17665 diskaddr_t err_blkno; 17666 int severity; 17667 int pfa_flag; 17668 extern struct scsi_key_strings scsi_cmds[]; 17669 17670 ASSERT(un != NULL); 17671 ASSERT(mutex_owned(SD_MUTEX(un))); 17672 ASSERT(bp != NULL); 17673 xp = SD_GET_XBUF(bp); 17674 ASSERT(xp != NULL); 17675 pktp = SD_GET_PKTP(bp); 17676 ASSERT(pktp != NULL); 17677 ASSERT(arg != NULL); 17678 17679 severity = ((struct sd_sense_info *)(arg))->ssi_severity; 17680 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag; 17681 17682 if ((code == SD_DELAYED_RETRY_ISSUED) || 17683 (code == SD_IMMEDIATE_RETRY_ISSUED)) { 17684 severity = SCSI_ERR_RETRYABLE; 17685 } 17686 17687 /* Use absolute block number for the request block number */ 17688 request_blkno = xp->xb_blkno; 17689 17690 /* 17691 * Now try to get the error block number from the sense data 17692 */ 17693 sensep = xp->xb_sense_data; 17694 17695 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH, 17696 (uint64_t *)&err_blkno)) { 17697 /* 17698 * We retrieved the error block number from the information 17699 * portion of the sense data. 17700 * 17701 * For USCSI commands we are better off using the error 17702 * block no. as the requested block no. (This is the best 17703 * we can estimate.) 17704 */ 17705 if ((SD_IS_BUFIO(xp) == FALSE) && 17706 ((pktp->pkt_flags & FLAG_SILENT) == 0)) { 17707 request_blkno = err_blkno; 17708 } 17709 } else { 17710 /* 17711 * Without the es_valid bit set (for fixed format) or an 17712 * information descriptor (for descriptor format) we cannot 17713 * be certain of the error blkno, so just use the 17714 * request_blkno. 17715 */ 17716 err_blkno = (diskaddr_t)request_blkno; 17717 } 17718 17719 /* 17720 * The following will log the buffer contents for the release driver 17721 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error 17722 * level is set to verbose. 17723 */ 17724 sd_dump_memory(un, SD_LOG_IO, "Failed CDB", 17725 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX); 17726 sd_dump_memory(un, SD_LOG_IO, "Sense Data", 17727 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX); 17728 17729 if (pfa_flag == FALSE) { 17730 /* This is normally only set for USCSI */ 17731 if ((pktp->pkt_flags & FLAG_SILENT) != 0) { 17732 return; 17733 } 17734 17735 if ((SD_IS_BUFIO(xp) == TRUE) && 17736 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) && 17737 (severity < sd_error_level))) { 17738 return; 17739 } 17740 } 17741 17742 /* 17743 * Check for Sonoma Failover and keep a count of how many failed I/O's 17744 */ 17745 if ((SD_IS_LSI(un)) && 17746 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) && 17747 (scsi_sense_asc(sensep) == 0x94) && 17748 (scsi_sense_ascq(sensep) == 0x01)) { 17749 un->un_sonoma_failure_count++; 17750 if (un->un_sonoma_failure_count > 1) { 17751 return; 17752 } 17753 } 17754 17755 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity, 17756 request_blkno, err_blkno, scsi_cmds, 17757 (struct scsi_extended_sense *)sensep, 17758 un->un_additional_codes, NULL); 17759 } 17760 17761 /* 17762 * Function: sd_sense_key_no_sense 17763 * 17764 * Description: Recovery action when sense data was not received. 17765 * 17766 * Context: May be called from interrupt context 17767 */ 17768 17769 static void 17770 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, 17771 struct sd_xbuf *xp, struct scsi_pkt *pktp) 17772 { 17773 struct sd_sense_info si; 17774 17775 ASSERT(un != NULL); 17776 ASSERT(mutex_owned(SD_MUTEX(un))); 17777 ASSERT(bp != NULL); 17778 ASSERT(xp != NULL); 17779 ASSERT(pktp != NULL); 17780 17781 si.ssi_severity = SCSI_ERR_FATAL; 17782 si.ssi_pfa_flag = FALSE; 17783 17784 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17785 17786 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17787 &si, EIO, (clock_t)0, NULL); 17788 } 17789 17790 17791 /* 17792 * Function: sd_sense_key_recoverable_error 17793 * 17794 * Description: Recovery actions for a SCSI "Recovered Error" sense key. 17795 * 17796 * Context: May be called from interrupt context 17797 */ 17798 17799 static void 17800 sd_sense_key_recoverable_error(struct sd_lun *un, 17801 uint8_t *sense_datap, 17802 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17803 { 17804 struct sd_sense_info si; 17805 uint8_t asc = scsi_sense_asc(sense_datap); 17806 17807 ASSERT(un != NULL); 17808 ASSERT(mutex_owned(SD_MUTEX(un))); 17809 ASSERT(bp != NULL); 17810 ASSERT(xp != NULL); 17811 ASSERT(pktp != NULL); 17812 17813 /* 17814 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED 17815 */ 17816 if ((asc == 0x5D) && (sd_report_pfa != 0)) { 17817 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 17818 si.ssi_severity = SCSI_ERR_INFO; 17819 si.ssi_pfa_flag = TRUE; 17820 } else { 17821 SD_UPDATE_ERRSTATS(un, sd_softerrs); 17822 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err); 17823 si.ssi_severity = SCSI_ERR_RECOVERED; 17824 si.ssi_pfa_flag = FALSE; 17825 } 17826 17827 if (pktp->pkt_resid == 0) { 17828 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 17829 sd_return_command(un, bp); 17830 return; 17831 } 17832 17833 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 17834 &si, EIO, (clock_t)0, NULL); 17835 } 17836 17837 17838 17839 17840 /* 17841 * Function: sd_sense_key_not_ready 17842 * 17843 * Description: Recovery actions for a SCSI "Not Ready" sense key. 17844 * 17845 * Context: May be called from interrupt context 17846 */ 17847 17848 static void 17849 sd_sense_key_not_ready(struct sd_lun *un, 17850 uint8_t *sense_datap, 17851 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 17852 { 17853 struct sd_sense_info si; 17854 uint8_t asc = scsi_sense_asc(sense_datap); 17855 uint8_t ascq = scsi_sense_ascq(sense_datap); 17856 17857 ASSERT(un != NULL); 17858 ASSERT(mutex_owned(SD_MUTEX(un))); 17859 ASSERT(bp != NULL); 17860 ASSERT(xp != NULL); 17861 ASSERT(pktp != NULL); 17862 17863 si.ssi_severity = SCSI_ERR_FATAL; 17864 si.ssi_pfa_flag = FALSE; 17865 17866 /* 17867 * Update error stats after first NOT READY error. Disks may have 17868 * been powered down and may need to be restarted. For CDROMs, 17869 * report NOT READY errors only if media is present. 17870 */ 17871 if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) || 17872 (xp->xb_retry_count > 0)) { 17873 SD_UPDATE_ERRSTATS(un, sd_harderrs); 17874 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err); 17875 } 17876 17877 /* 17878 * Just fail if the "not ready" retry limit has been reached. 17879 */ 17880 if (xp->xb_retry_count >= un->un_notready_retry_count) { 17881 /* Special check for error message printing for removables. */ 17882 if (un->un_f_has_removable_media && (asc == 0x04) && 17883 (ascq >= 0x04)) { 17884 si.ssi_severity = SCSI_ERR_ALL; 17885 } 17886 goto fail_command; 17887 } 17888 17889 /* 17890 * Check the ASC and ASCQ in the sense data as needed, to determine 17891 * what to do. 17892 */ 17893 switch (asc) { 17894 case 0x04: /* LOGICAL UNIT NOT READY */ 17895 /* 17896 * disk drives that don't spin up result in a very long delay 17897 * in format without warning messages. We will log a message 17898 * if the error level is set to verbose. 17899 */ 17900 if (sd_error_level < SCSI_ERR_RETRYABLE) { 17901 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 17902 "logical unit not ready, resetting disk\n"); 17903 } 17904 17905 /* 17906 * There are different requirements for CDROMs and disks for 17907 * the number of retries. If a CD-ROM is giving this, it is 17908 * probably reading TOC and is in the process of getting 17909 * ready, so we should keep on trying for a long time to make 17910 * sure that all types of media are taken in account (for 17911 * some media the drive takes a long time to read TOC). For 17912 * disks we do not want to retry this too many times as this 17913 * can cause a long hang in format when the drive refuses to 17914 * spin up (a very common failure). 17915 */ 17916 switch (ascq) { 17917 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */ 17918 /* 17919 * Disk drives frequently refuse to spin up which 17920 * results in a very long hang in format without 17921 * warning messages. 17922 * 17923 * Note: This code preserves the legacy behavior of 17924 * comparing xb_retry_count against zero for fibre 17925 * channel targets instead of comparing against the 17926 * un_reset_retry_count value. The reason for this 17927 * discrepancy has been so utterly lost beneath the 17928 * Sands of Time that even Indiana Jones could not 17929 * find it. 17930 */ 17931 if (un->un_f_is_fibre == TRUE) { 17932 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17933 (xp->xb_retry_count > 0)) && 17934 (un->un_startstop_timeid == NULL)) { 17935 scsi_log(SD_DEVINFO(un), sd_label, 17936 CE_WARN, "logical unit not ready, " 17937 "resetting disk\n"); 17938 sd_reset_target(un, pktp); 17939 } 17940 } else { 17941 if (((sd_level_mask & SD_LOGMASK_DIAG) || 17942 (xp->xb_retry_count > 17943 un->un_reset_retry_count)) && 17944 (un->un_startstop_timeid == NULL)) { 17945 scsi_log(SD_DEVINFO(un), sd_label, 17946 CE_WARN, "logical unit not ready, " 17947 "resetting disk\n"); 17948 sd_reset_target(un, pktp); 17949 } 17950 } 17951 break; 17952 17953 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */ 17954 /* 17955 * If the target is in the process of becoming 17956 * ready, just proceed with the retry. This can 17957 * happen with CD-ROMs that take a long time to 17958 * read TOC after a power cycle or reset. 17959 */ 17960 goto do_retry; 17961 17962 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */ 17963 break; 17964 17965 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */ 17966 /* 17967 * Retries cannot help here so just fail right away. 17968 */ 17969 goto fail_command; 17970 17971 case 0x88: 17972 /* 17973 * Vendor-unique code for T3/T4: it indicates a 17974 * path problem in a mutipathed config, but as far as 17975 * the target driver is concerned it equates to a fatal 17976 * error, so we should just fail the command right away 17977 * (without printing anything to the console). If this 17978 * is not a T3/T4, fall thru to the default recovery 17979 * action. 17980 * T3/T4 is FC only, don't need to check is_fibre 17981 */ 17982 if (SD_IS_T3(un) || SD_IS_T4(un)) { 17983 sd_return_failed_command(un, bp, EIO); 17984 return; 17985 } 17986 /* FALLTHRU */ 17987 17988 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */ 17989 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */ 17990 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */ 17991 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */ 17992 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */ 17993 default: /* Possible future codes in SCSI spec? */ 17994 /* 17995 * For removable-media devices, do not retry if 17996 * ASCQ > 2 as these result mostly from USCSI commands 17997 * on MMC devices issued to check status of an 17998 * operation initiated in immediate mode. Also for 17999 * ASCQ >= 4 do not print console messages as these 18000 * mainly represent a user-initiated operation 18001 * instead of a system failure. 18002 */ 18003 if (un->un_f_has_removable_media) { 18004 si.ssi_severity = SCSI_ERR_ALL; 18005 goto fail_command; 18006 } 18007 break; 18008 } 18009 18010 /* 18011 * As part of our recovery attempt for the NOT READY 18012 * condition, we issue a START STOP UNIT command. However 18013 * we want to wait for a short delay before attempting this 18014 * as there may still be more commands coming back from the 18015 * target with the check condition. To do this we use 18016 * timeout(9F) to call sd_start_stop_unit_callback() after 18017 * the delay interval expires. (sd_start_stop_unit_callback() 18018 * dispatches sd_start_stop_unit_task(), which will issue 18019 * the actual START STOP UNIT command. The delay interval 18020 * is one-half of the delay that we will use to retry the 18021 * command that generated the NOT READY condition. 18022 * 18023 * Note that we could just dispatch sd_start_stop_unit_task() 18024 * from here and allow it to sleep for the delay interval, 18025 * but then we would be tying up the taskq thread 18026 * uncesessarily for the duration of the delay. 18027 * 18028 * Do not issue the START STOP UNIT if the current command 18029 * is already a START STOP UNIT. 18030 */ 18031 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) { 18032 break; 18033 } 18034 18035 /* 18036 * Do not schedule the timeout if one is already pending. 18037 */ 18038 if (un->un_startstop_timeid != NULL) { 18039 SD_INFO(SD_LOG_ERROR, un, 18040 "sd_sense_key_not_ready: restart already issued to" 18041 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)), 18042 ddi_get_instance(SD_DEVINFO(un))); 18043 break; 18044 } 18045 18046 /* 18047 * Schedule the START STOP UNIT command, then queue the command 18048 * for a retry. 18049 * 18050 * Note: A timeout is not scheduled for this retry because we 18051 * want the retry to be serial with the START_STOP_UNIT. The 18052 * retry will be started when the START_STOP_UNIT is completed 18053 * in sd_start_stop_unit_task. 18054 */ 18055 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback, 18056 un, SD_BSY_TIMEOUT / 2); 18057 xp->xb_retry_count++; 18058 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter); 18059 return; 18060 18061 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */ 18062 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18063 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18064 "unit does not respond to selection\n"); 18065 } 18066 break; 18067 18068 case 0x3A: /* MEDIUM NOT PRESENT */ 18069 if (sd_error_level >= SCSI_ERR_FATAL) { 18070 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18071 "Caddy not inserted in drive\n"); 18072 } 18073 18074 sr_ejected(un); 18075 un->un_mediastate = DKIO_EJECTED; 18076 /* The state has changed, inform the media watch routines */ 18077 cv_broadcast(&un->un_state_cv); 18078 /* Just fail if no media is present in the drive. */ 18079 goto fail_command; 18080 18081 default: 18082 if (sd_error_level < SCSI_ERR_RETRYABLE) { 18083 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, 18084 "Unit not Ready. Additional sense code 0x%x\n", 18085 asc); 18086 } 18087 break; 18088 } 18089 18090 do_retry: 18091 18092 /* 18093 * Retry the command, as some targets may report NOT READY for 18094 * several seconds after being reset. 18095 */ 18096 xp->xb_retry_count++; 18097 si.ssi_severity = SCSI_ERR_RETRYABLE; 18098 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 18099 &si, EIO, SD_BSY_TIMEOUT, NULL); 18100 18101 return; 18102 18103 fail_command: 18104 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18105 sd_return_failed_command(un, bp, EIO); 18106 } 18107 18108 18109 18110 /* 18111 * Function: sd_sense_key_medium_or_hardware_error 18112 * 18113 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error" 18114 * sense key. 18115 * 18116 * Context: May be called from interrupt context 18117 */ 18118 18119 static void 18120 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, 18121 uint8_t *sense_datap, 18122 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18123 { 18124 struct sd_sense_info si; 18125 uint8_t sense_key = scsi_sense_key(sense_datap); 18126 uint8_t asc = scsi_sense_asc(sense_datap); 18127 18128 ASSERT(un != NULL); 18129 ASSERT(mutex_owned(SD_MUTEX(un))); 18130 ASSERT(bp != NULL); 18131 ASSERT(xp != NULL); 18132 ASSERT(pktp != NULL); 18133 18134 si.ssi_severity = SCSI_ERR_FATAL; 18135 si.ssi_pfa_flag = FALSE; 18136 18137 if (sense_key == KEY_MEDIUM_ERROR) { 18138 SD_UPDATE_ERRSTATS(un, sd_rq_media_err); 18139 } 18140 18141 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18142 18143 if ((un->un_reset_retry_count != 0) && 18144 (xp->xb_retry_count == un->un_reset_retry_count)) { 18145 mutex_exit(SD_MUTEX(un)); 18146 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */ 18147 if (un->un_f_allow_bus_device_reset == TRUE) { 18148 18149 boolean_t try_resetting_target = B_TRUE; 18150 18151 /* 18152 * We need to be able to handle specific ASC when we are 18153 * handling a KEY_HARDWARE_ERROR. In particular 18154 * taking the default action of resetting the target may 18155 * not be the appropriate way to attempt recovery. 18156 * Resetting a target because of a single LUN failure 18157 * victimizes all LUNs on that target. 18158 * 18159 * This is true for the LSI arrays, if an LSI 18160 * array controller returns an ASC of 0x84 (LUN Dead) we 18161 * should trust it. 18162 */ 18163 18164 if (sense_key == KEY_HARDWARE_ERROR) { 18165 switch (asc) { 18166 case 0x84: 18167 if (SD_IS_LSI(un)) { 18168 try_resetting_target = B_FALSE; 18169 } 18170 break; 18171 default: 18172 break; 18173 } 18174 } 18175 18176 if (try_resetting_target == B_TRUE) { 18177 int reset_retval = 0; 18178 if (un->un_f_lun_reset_enabled == TRUE) { 18179 SD_TRACE(SD_LOG_IO_CORE, un, 18180 "sd_sense_key_medium_or_hardware_" 18181 "error: issuing RESET_LUN\n"); 18182 reset_retval = 18183 scsi_reset(SD_ADDRESS(un), 18184 RESET_LUN); 18185 } 18186 if (reset_retval == 0) { 18187 SD_TRACE(SD_LOG_IO_CORE, un, 18188 "sd_sense_key_medium_or_hardware_" 18189 "error: issuing RESET_TARGET\n"); 18190 (void) scsi_reset(SD_ADDRESS(un), 18191 RESET_TARGET); 18192 } 18193 } 18194 } 18195 mutex_enter(SD_MUTEX(un)); 18196 } 18197 18198 /* 18199 * This really ought to be a fatal error, but we will retry anyway 18200 * as some drives report this as a spurious error. 18201 */ 18202 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18203 &si, EIO, (clock_t)0, NULL); 18204 } 18205 18206 18207 18208 /* 18209 * Function: sd_sense_key_illegal_request 18210 * 18211 * Description: Recovery actions for a SCSI "Illegal Request" sense key. 18212 * 18213 * Context: May be called from interrupt context 18214 */ 18215 18216 static void 18217 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp, 18218 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18219 { 18220 struct sd_sense_info si; 18221 18222 ASSERT(un != NULL); 18223 ASSERT(mutex_owned(SD_MUTEX(un))); 18224 ASSERT(bp != NULL); 18225 ASSERT(xp != NULL); 18226 ASSERT(pktp != NULL); 18227 18228 SD_UPDATE_ERRSTATS(un, sd_softerrs); 18229 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err); 18230 18231 si.ssi_severity = SCSI_ERR_INFO; 18232 si.ssi_pfa_flag = FALSE; 18233 18234 /* Pointless to retry if the target thinks it's an illegal request */ 18235 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18236 sd_return_failed_command(un, bp, EIO); 18237 } 18238 18239 18240 18241 18242 /* 18243 * Function: sd_sense_key_unit_attention 18244 * 18245 * Description: Recovery actions for a SCSI "Unit Attention" sense key. 18246 * 18247 * Context: May be called from interrupt context 18248 */ 18249 18250 static void 18251 sd_sense_key_unit_attention(struct sd_lun *un, 18252 uint8_t *sense_datap, 18253 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18254 { 18255 /* 18256 * For UNIT ATTENTION we allow retries for one minute. Devices 18257 * like Sonoma can return UNIT ATTENTION close to a minute 18258 * under certain conditions. 18259 */ 18260 int retry_check_flag = SD_RETRIES_UA; 18261 boolean_t kstat_updated = B_FALSE; 18262 struct sd_sense_info si; 18263 uint8_t asc = scsi_sense_asc(sense_datap); 18264 18265 ASSERT(un != NULL); 18266 ASSERT(mutex_owned(SD_MUTEX(un))); 18267 ASSERT(bp != NULL); 18268 ASSERT(xp != NULL); 18269 ASSERT(pktp != NULL); 18270 18271 si.ssi_severity = SCSI_ERR_INFO; 18272 si.ssi_pfa_flag = FALSE; 18273 18274 18275 switch (asc) { 18276 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */ 18277 if (sd_report_pfa != 0) { 18278 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err); 18279 si.ssi_pfa_flag = TRUE; 18280 retry_check_flag = SD_RETRIES_STANDARD; 18281 goto do_retry; 18282 } 18283 18284 break; 18285 18286 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */ 18287 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 18288 un->un_resvd_status |= 18289 (SD_LOST_RESERVE | SD_WANT_RESERVE); 18290 } 18291 #ifdef _LP64 18292 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) { 18293 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task, 18294 un, KM_NOSLEEP) == 0) { 18295 /* 18296 * If we can't dispatch the task we'll just 18297 * live without descriptor sense. We can 18298 * try again on the next "unit attention" 18299 */ 18300 SD_ERROR(SD_LOG_ERROR, un, 18301 "sd_sense_key_unit_attention: " 18302 "Could not dispatch " 18303 "sd_reenable_dsense_task\n"); 18304 } 18305 } 18306 #endif /* _LP64 */ 18307 /* FALLTHRU */ 18308 18309 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */ 18310 if (!un->un_f_has_removable_media) { 18311 break; 18312 } 18313 18314 /* 18315 * When we get a unit attention from a removable-media device, 18316 * it may be in a state that will take a long time to recover 18317 * (e.g., from a reset). Since we are executing in interrupt 18318 * context here, we cannot wait around for the device to come 18319 * back. So hand this command off to sd_media_change_task() 18320 * for deferred processing under taskq thread context. (Note 18321 * that the command still may be failed if a problem is 18322 * encountered at a later time.) 18323 */ 18324 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp, 18325 KM_NOSLEEP) == 0) { 18326 /* 18327 * Cannot dispatch the request so fail the command. 18328 */ 18329 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18330 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18331 si.ssi_severity = SCSI_ERR_FATAL; 18332 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18333 sd_return_failed_command(un, bp, EIO); 18334 } 18335 18336 /* 18337 * If failed to dispatch sd_media_change_task(), we already 18338 * updated kstat. If succeed to dispatch sd_media_change_task(), 18339 * we should update kstat later if it encounters an error. So, 18340 * we update kstat_updated flag here. 18341 */ 18342 kstat_updated = B_TRUE; 18343 18344 /* 18345 * Either the command has been successfully dispatched to a 18346 * task Q for retrying, or the dispatch failed. In either case 18347 * do NOT retry again by calling sd_retry_command. This sets up 18348 * two retries of the same command and when one completes and 18349 * frees the resources the other will access freed memory, 18350 * a bad thing. 18351 */ 18352 return; 18353 18354 default: 18355 break; 18356 } 18357 18358 /* 18359 * Update kstat if we haven't done that. 18360 */ 18361 if (!kstat_updated) { 18362 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18363 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 18364 } 18365 18366 do_retry: 18367 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si, 18368 EIO, SD_UA_RETRY_DELAY, NULL); 18369 } 18370 18371 18372 18373 /* 18374 * Function: sd_sense_key_fail_command 18375 * 18376 * Description: Use to fail a command when we don't like the sense key that 18377 * was returned. 18378 * 18379 * Context: May be called from interrupt context 18380 */ 18381 18382 static void 18383 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, 18384 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18385 { 18386 struct sd_sense_info si; 18387 18388 ASSERT(un != NULL); 18389 ASSERT(mutex_owned(SD_MUTEX(un))); 18390 ASSERT(bp != NULL); 18391 ASSERT(xp != NULL); 18392 ASSERT(pktp != NULL); 18393 18394 si.ssi_severity = SCSI_ERR_FATAL; 18395 si.ssi_pfa_flag = FALSE; 18396 18397 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18398 sd_return_failed_command(un, bp, EIO); 18399 } 18400 18401 18402 18403 /* 18404 * Function: sd_sense_key_blank_check 18405 * 18406 * Description: Recovery actions for a SCSI "Blank Check" sense key. 18407 * Has no monetary connotation. 18408 * 18409 * Context: May be called from interrupt context 18410 */ 18411 18412 static void 18413 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, 18414 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18415 { 18416 struct sd_sense_info si; 18417 18418 ASSERT(un != NULL); 18419 ASSERT(mutex_owned(SD_MUTEX(un))); 18420 ASSERT(bp != NULL); 18421 ASSERT(xp != NULL); 18422 ASSERT(pktp != NULL); 18423 18424 /* 18425 * Blank check is not fatal for removable devices, therefore 18426 * it does not require a console message. 18427 */ 18428 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL : 18429 SCSI_ERR_FATAL; 18430 si.ssi_pfa_flag = FALSE; 18431 18432 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 18433 sd_return_failed_command(un, bp, EIO); 18434 } 18435 18436 18437 18438 18439 /* 18440 * Function: sd_sense_key_aborted_command 18441 * 18442 * Description: Recovery actions for a SCSI "Aborted Command" sense key. 18443 * 18444 * Context: May be called from interrupt context 18445 */ 18446 18447 static void 18448 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp, 18449 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18450 { 18451 struct sd_sense_info si; 18452 18453 ASSERT(un != NULL); 18454 ASSERT(mutex_owned(SD_MUTEX(un))); 18455 ASSERT(bp != NULL); 18456 ASSERT(xp != NULL); 18457 ASSERT(pktp != NULL); 18458 18459 si.ssi_severity = SCSI_ERR_FATAL; 18460 si.ssi_pfa_flag = FALSE; 18461 18462 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18463 18464 /* 18465 * This really ought to be a fatal error, but we will retry anyway 18466 * as some drives report this as a spurious error. 18467 */ 18468 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18469 &si, EIO, (clock_t)0, NULL); 18470 } 18471 18472 18473 18474 /* 18475 * Function: sd_sense_key_default 18476 * 18477 * Description: Default recovery action for several SCSI sense keys (basically 18478 * attempts a retry). 18479 * 18480 * Context: May be called from interrupt context 18481 */ 18482 18483 static void 18484 sd_sense_key_default(struct sd_lun *un, 18485 uint8_t *sense_datap, 18486 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp) 18487 { 18488 struct sd_sense_info si; 18489 uint8_t sense_key = scsi_sense_key(sense_datap); 18490 18491 ASSERT(un != NULL); 18492 ASSERT(mutex_owned(SD_MUTEX(un))); 18493 ASSERT(bp != NULL); 18494 ASSERT(xp != NULL); 18495 ASSERT(pktp != NULL); 18496 18497 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18498 18499 /* 18500 * Undecoded sense key. Attempt retries and hope that will fix 18501 * the problem. Otherwise, we're dead. 18502 */ 18503 if ((pktp->pkt_flags & FLAG_SILENT) == 0) { 18504 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18505 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]); 18506 } 18507 18508 si.ssi_severity = SCSI_ERR_FATAL; 18509 si.ssi_pfa_flag = FALSE; 18510 18511 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg, 18512 &si, EIO, (clock_t)0, NULL); 18513 } 18514 18515 18516 18517 /* 18518 * Function: sd_print_retry_msg 18519 * 18520 * Description: Print a message indicating the retry action being taken. 18521 * 18522 * Arguments: un - ptr to associated softstate 18523 * bp - ptr to buf(9S) for the command 18524 * arg - not used. 18525 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18526 * or SD_NO_RETRY_ISSUED 18527 * 18528 * Context: May be called from interrupt context 18529 */ 18530 /* ARGSUSED */ 18531 static void 18532 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag) 18533 { 18534 struct sd_xbuf *xp; 18535 struct scsi_pkt *pktp; 18536 char *reasonp; 18537 char *msgp; 18538 18539 ASSERT(un != NULL); 18540 ASSERT(mutex_owned(SD_MUTEX(un))); 18541 ASSERT(bp != NULL); 18542 pktp = SD_GET_PKTP(bp); 18543 ASSERT(pktp != NULL); 18544 xp = SD_GET_XBUF(bp); 18545 ASSERT(xp != NULL); 18546 18547 ASSERT(!mutex_owned(&un->un_pm_mutex)); 18548 mutex_enter(&un->un_pm_mutex); 18549 if ((un->un_state == SD_STATE_SUSPENDED) || 18550 (SD_DEVICE_IS_IN_LOW_POWER(un)) || 18551 (pktp->pkt_flags & FLAG_SILENT)) { 18552 mutex_exit(&un->un_pm_mutex); 18553 goto update_pkt_reason; 18554 } 18555 mutex_exit(&un->un_pm_mutex); 18556 18557 /* 18558 * Suppress messages if they are all the same pkt_reason; with 18559 * TQ, many (up to 256) are returned with the same pkt_reason. 18560 * If we are in panic, then suppress the retry messages. 18561 */ 18562 switch (flag) { 18563 case SD_NO_RETRY_ISSUED: 18564 msgp = "giving up"; 18565 break; 18566 case SD_IMMEDIATE_RETRY_ISSUED: 18567 case SD_DELAYED_RETRY_ISSUED: 18568 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) || 18569 ((pktp->pkt_reason == un->un_last_pkt_reason) && 18570 (sd_error_level != SCSI_ERR_ALL))) { 18571 return; 18572 } 18573 msgp = "retrying command"; 18574 break; 18575 default: 18576 goto update_pkt_reason; 18577 } 18578 18579 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" : 18580 scsi_rname(pktp->pkt_reason)); 18581 18582 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18583 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp); 18584 18585 update_pkt_reason: 18586 /* 18587 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason. 18588 * This is to prevent multiple console messages for the same failure 18589 * condition. Note that un->un_last_pkt_reason is NOT restored if & 18590 * when the command is retried successfully because there still may be 18591 * more commands coming back with the same value of pktp->pkt_reason. 18592 */ 18593 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) { 18594 un->un_last_pkt_reason = pktp->pkt_reason; 18595 } 18596 } 18597 18598 18599 /* 18600 * Function: sd_print_cmd_incomplete_msg 18601 * 18602 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason. 18603 * 18604 * Arguments: un - ptr to associated softstate 18605 * bp - ptr to buf(9S) for the command 18606 * arg - passed to sd_print_retry_msg() 18607 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED, 18608 * or SD_NO_RETRY_ISSUED 18609 * 18610 * Context: May be called from interrupt context 18611 */ 18612 18613 static void 18614 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, 18615 int code) 18616 { 18617 dev_info_t *dip; 18618 18619 ASSERT(un != NULL); 18620 ASSERT(mutex_owned(SD_MUTEX(un))); 18621 ASSERT(bp != NULL); 18622 18623 switch (code) { 18624 case SD_NO_RETRY_ISSUED: 18625 /* Command was failed. Someone turned off this target? */ 18626 if (un->un_state != SD_STATE_OFFLINE) { 18627 /* 18628 * Suppress message if we are detaching and 18629 * device has been disconnected 18630 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation 18631 * private interface and not part of the DDI 18632 */ 18633 dip = un->un_sd->sd_dev; 18634 if (!(DEVI_IS_DETACHING(dip) && 18635 DEVI_IS_DEVICE_REMOVED(dip))) { 18636 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 18637 "disk not responding to selection\n"); 18638 } 18639 New_state(un, SD_STATE_OFFLINE); 18640 } 18641 break; 18642 18643 case SD_DELAYED_RETRY_ISSUED: 18644 case SD_IMMEDIATE_RETRY_ISSUED: 18645 default: 18646 /* Command was successfully queued for retry */ 18647 sd_print_retry_msg(un, bp, arg, code); 18648 break; 18649 } 18650 } 18651 18652 18653 /* 18654 * Function: sd_pkt_reason_cmd_incomplete 18655 * 18656 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason. 18657 * 18658 * Context: May be called from interrupt context 18659 */ 18660 18661 static void 18662 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp, 18663 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18664 { 18665 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE; 18666 18667 ASSERT(un != NULL); 18668 ASSERT(mutex_owned(SD_MUTEX(un))); 18669 ASSERT(bp != NULL); 18670 ASSERT(xp != NULL); 18671 ASSERT(pktp != NULL); 18672 18673 /* Do not do a reset if selection did not complete */ 18674 /* Note: Should this not just check the bit? */ 18675 if (pktp->pkt_state != STATE_GOT_BUS) { 18676 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18677 sd_reset_target(un, pktp); 18678 } 18679 18680 /* 18681 * If the target was not successfully selected, then set 18682 * SD_RETRIES_FAILFAST to indicate that we lost communication 18683 * with the target, and further retries and/or commands are 18684 * likely to take a long time. 18685 */ 18686 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) { 18687 flag |= SD_RETRIES_FAILFAST; 18688 } 18689 18690 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18691 18692 sd_retry_command(un, bp, flag, 18693 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18694 } 18695 18696 18697 18698 /* 18699 * Function: sd_pkt_reason_cmd_tran_err 18700 * 18701 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason. 18702 * 18703 * Context: May be called from interrupt context 18704 */ 18705 18706 static void 18707 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp, 18708 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18709 { 18710 ASSERT(un != NULL); 18711 ASSERT(mutex_owned(SD_MUTEX(un))); 18712 ASSERT(bp != NULL); 18713 ASSERT(xp != NULL); 18714 ASSERT(pktp != NULL); 18715 18716 /* 18717 * Do not reset if we got a parity error, or if 18718 * selection did not complete. 18719 */ 18720 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18721 /* Note: Should this not just check the bit for pkt_state? */ 18722 if (((pktp->pkt_statistics & STAT_PERR) == 0) && 18723 (pktp->pkt_state != STATE_GOT_BUS)) { 18724 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18725 sd_reset_target(un, pktp); 18726 } 18727 18728 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18729 18730 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18731 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18732 } 18733 18734 18735 18736 /* 18737 * Function: sd_pkt_reason_cmd_reset 18738 * 18739 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason. 18740 * 18741 * Context: May be called from interrupt context 18742 */ 18743 18744 static void 18745 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, 18746 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18747 { 18748 ASSERT(un != NULL); 18749 ASSERT(mutex_owned(SD_MUTEX(un))); 18750 ASSERT(bp != NULL); 18751 ASSERT(xp != NULL); 18752 ASSERT(pktp != NULL); 18753 18754 /* The target may still be running the command, so try to reset. */ 18755 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18756 sd_reset_target(un, pktp); 18757 18758 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18759 18760 /* 18761 * If pkt_reason is CMD_RESET chances are that this pkt got 18762 * reset because another target on this bus caused it. The target 18763 * that caused it should get CMD_TIMEOUT with pkt_statistics 18764 * of STAT_TIMEOUT/STAT_DEV_RESET. 18765 */ 18766 18767 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18768 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18769 } 18770 18771 18772 18773 18774 /* 18775 * Function: sd_pkt_reason_cmd_aborted 18776 * 18777 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason. 18778 * 18779 * Context: May be called from interrupt context 18780 */ 18781 18782 static void 18783 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, 18784 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18785 { 18786 ASSERT(un != NULL); 18787 ASSERT(mutex_owned(SD_MUTEX(un))); 18788 ASSERT(bp != NULL); 18789 ASSERT(xp != NULL); 18790 ASSERT(pktp != NULL); 18791 18792 /* The target may still be running the command, so try to reset. */ 18793 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18794 sd_reset_target(un, pktp); 18795 18796 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18797 18798 /* 18799 * If pkt_reason is CMD_ABORTED chances are that this pkt got 18800 * aborted because another target on this bus caused it. The target 18801 * that caused it should get CMD_TIMEOUT with pkt_statistics 18802 * of STAT_TIMEOUT/STAT_DEV_RESET. 18803 */ 18804 18805 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE), 18806 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18807 } 18808 18809 18810 18811 /* 18812 * Function: sd_pkt_reason_cmd_timeout 18813 * 18814 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason. 18815 * 18816 * Context: May be called from interrupt context 18817 */ 18818 18819 static void 18820 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, 18821 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18822 { 18823 ASSERT(un != NULL); 18824 ASSERT(mutex_owned(SD_MUTEX(un))); 18825 ASSERT(bp != NULL); 18826 ASSERT(xp != NULL); 18827 ASSERT(pktp != NULL); 18828 18829 18830 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18831 sd_reset_target(un, pktp); 18832 18833 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18834 18835 /* 18836 * A command timeout indicates that we could not establish 18837 * communication with the target, so set SD_RETRIES_FAILFAST 18838 * as further retries/commands are likely to take a long time. 18839 */ 18840 sd_retry_command(un, bp, 18841 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST), 18842 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18843 } 18844 18845 18846 18847 /* 18848 * Function: sd_pkt_reason_cmd_unx_bus_free 18849 * 18850 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason. 18851 * 18852 * Context: May be called from interrupt context 18853 */ 18854 18855 static void 18856 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp, 18857 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18858 { 18859 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code); 18860 18861 ASSERT(un != NULL); 18862 ASSERT(mutex_owned(SD_MUTEX(un))); 18863 ASSERT(bp != NULL); 18864 ASSERT(xp != NULL); 18865 ASSERT(pktp != NULL); 18866 18867 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18868 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18869 18870 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ? 18871 sd_print_retry_msg : NULL; 18872 18873 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18874 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18875 } 18876 18877 18878 /* 18879 * Function: sd_pkt_reason_cmd_tag_reject 18880 * 18881 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason. 18882 * 18883 * Context: May be called from interrupt context 18884 */ 18885 18886 static void 18887 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp, 18888 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18889 { 18890 ASSERT(un != NULL); 18891 ASSERT(mutex_owned(SD_MUTEX(un))); 18892 ASSERT(bp != NULL); 18893 ASSERT(xp != NULL); 18894 ASSERT(pktp != NULL); 18895 18896 SD_UPDATE_ERRSTATS(un, sd_harderrs); 18897 pktp->pkt_flags = 0; 18898 un->un_tagflags = 0; 18899 if (un->un_f_opt_queueing == TRUE) { 18900 un->un_throttle = min(un->un_throttle, 3); 18901 } else { 18902 un->un_throttle = 1; 18903 } 18904 mutex_exit(SD_MUTEX(un)); 18905 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 18906 mutex_enter(SD_MUTEX(un)); 18907 18908 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18909 18910 /* Legacy behavior not to check retry counts here. */ 18911 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE), 18912 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18913 } 18914 18915 18916 /* 18917 * Function: sd_pkt_reason_default 18918 * 18919 * Description: Default recovery actions for SCSA pkt_reason values that 18920 * do not have more explicit recovery actions. 18921 * 18922 * Context: May be called from interrupt context 18923 */ 18924 18925 static void 18926 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, 18927 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18928 { 18929 ASSERT(un != NULL); 18930 ASSERT(mutex_owned(SD_MUTEX(un))); 18931 ASSERT(bp != NULL); 18932 ASSERT(xp != NULL); 18933 ASSERT(pktp != NULL); 18934 18935 SD_UPDATE_ERRSTATS(un, sd_transerrs); 18936 sd_reset_target(un, pktp); 18937 18938 SD_UPDATE_RESERVATION_STATUS(un, pktp); 18939 18940 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE), 18941 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL); 18942 } 18943 18944 18945 18946 /* 18947 * Function: sd_pkt_status_check_condition 18948 * 18949 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status. 18950 * 18951 * Context: May be called from interrupt context 18952 */ 18953 18954 static void 18955 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp, 18956 struct sd_xbuf *xp, struct scsi_pkt *pktp) 18957 { 18958 ASSERT(un != NULL); 18959 ASSERT(mutex_owned(SD_MUTEX(un))); 18960 ASSERT(bp != NULL); 18961 ASSERT(xp != NULL); 18962 ASSERT(pktp != NULL); 18963 18964 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: " 18965 "entry: buf:0x%p xp:0x%p\n", bp, xp); 18966 18967 /* 18968 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the 18969 * command will be retried after the request sense). Otherwise, retry 18970 * the command. Note: we are issuing the request sense even though the 18971 * retry limit may have been reached for the failed command. 18972 */ 18973 if (un->un_f_arq_enabled == FALSE) { 18974 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18975 "no ARQ, sending request sense command\n"); 18976 sd_send_request_sense_command(un, bp, pktp); 18977 } else { 18978 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: " 18979 "ARQ,retrying request sense command\n"); 18980 #if defined(__i386) || defined(__amd64) 18981 /* 18982 * The SD_RETRY_DELAY value need to be adjusted here 18983 * when SD_RETRY_DELAY change in sddef.h 18984 */ 18985 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 18986 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, 18987 NULL); 18988 #else 18989 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 18990 EIO, SD_RETRY_DELAY, NULL); 18991 #endif 18992 } 18993 18994 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n"); 18995 } 18996 18997 18998 /* 18999 * Function: sd_pkt_status_busy 19000 * 19001 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status. 19002 * 19003 * Context: May be called from interrupt context 19004 */ 19005 19006 static void 19007 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp, 19008 struct scsi_pkt *pktp) 19009 { 19010 ASSERT(un != NULL); 19011 ASSERT(mutex_owned(SD_MUTEX(un))); 19012 ASSERT(bp != NULL); 19013 ASSERT(xp != NULL); 19014 ASSERT(pktp != NULL); 19015 19016 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19017 "sd_pkt_status_busy: entry\n"); 19018 19019 /* If retries are exhausted, just fail the command. */ 19020 if (xp->xb_retry_count >= un->un_busy_retry_count) { 19021 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 19022 "device busy too long\n"); 19023 sd_return_failed_command(un, bp, EIO); 19024 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19025 "sd_pkt_status_busy: exit\n"); 19026 return; 19027 } 19028 xp->xb_retry_count++; 19029 19030 /* 19031 * Try to reset the target. However, we do not want to perform 19032 * more than one reset if the device continues to fail. The reset 19033 * will be performed when the retry count reaches the reset 19034 * threshold. This threshold should be set such that at least 19035 * one retry is issued before the reset is performed. 19036 */ 19037 if (xp->xb_retry_count == 19038 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) { 19039 int rval = 0; 19040 mutex_exit(SD_MUTEX(un)); 19041 if (un->un_f_allow_bus_device_reset == TRUE) { 19042 /* 19043 * First try to reset the LUN; if we cannot then 19044 * try to reset the target. 19045 */ 19046 if (un->un_f_lun_reset_enabled == TRUE) { 19047 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19048 "sd_pkt_status_busy: RESET_LUN\n"); 19049 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19050 } 19051 if (rval == 0) { 19052 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19053 "sd_pkt_status_busy: RESET_TARGET\n"); 19054 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19055 } 19056 } 19057 if (rval == 0) { 19058 /* 19059 * If the RESET_LUN and/or RESET_TARGET failed, 19060 * try RESET_ALL 19061 */ 19062 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19063 "sd_pkt_status_busy: RESET_ALL\n"); 19064 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL); 19065 } 19066 mutex_enter(SD_MUTEX(un)); 19067 if (rval == 0) { 19068 /* 19069 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed. 19070 * At this point we give up & fail the command. 19071 */ 19072 sd_return_failed_command(un, bp, EIO); 19073 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19074 "sd_pkt_status_busy: exit (failed cmd)\n"); 19075 return; 19076 } 19077 } 19078 19079 /* 19080 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as 19081 * we have already checked the retry counts above. 19082 */ 19083 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 19084 EIO, SD_BSY_TIMEOUT, NULL); 19085 19086 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19087 "sd_pkt_status_busy: exit\n"); 19088 } 19089 19090 19091 /* 19092 * Function: sd_pkt_status_reservation_conflict 19093 * 19094 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI 19095 * command status. 19096 * 19097 * Context: May be called from interrupt context 19098 */ 19099 19100 static void 19101 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp, 19102 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19103 { 19104 ASSERT(un != NULL); 19105 ASSERT(mutex_owned(SD_MUTEX(un))); 19106 ASSERT(bp != NULL); 19107 ASSERT(xp != NULL); 19108 ASSERT(pktp != NULL); 19109 19110 /* 19111 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation 19112 * conflict could be due to various reasons like incorrect keys, not 19113 * registered or not reserved etc. So, we return EACCES to the caller. 19114 */ 19115 if (un->un_reservation_type == SD_SCSI3_RESERVATION) { 19116 int cmd = SD_GET_PKT_OPCODE(pktp); 19117 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) || 19118 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) { 19119 sd_return_failed_command(un, bp, EACCES); 19120 return; 19121 } 19122 } 19123 19124 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 19125 19126 if ((un->un_resvd_status & SD_FAILFAST) != 0) { 19127 if (sd_failfast_enable != 0) { 19128 /* By definition, we must panic here.... */ 19129 sd_panic_for_res_conflict(un); 19130 /*NOTREACHED*/ 19131 } 19132 SD_ERROR(SD_LOG_IO, un, 19133 "sd_handle_resv_conflict: Disk Reserved\n"); 19134 sd_return_failed_command(un, bp, EACCES); 19135 return; 19136 } 19137 19138 /* 19139 * 1147670: retry only if sd_retry_on_reservation_conflict 19140 * property is set (default is 1). Retries will not succeed 19141 * on a disk reserved by another initiator. HA systems 19142 * may reset this via sd.conf to avoid these retries. 19143 * 19144 * Note: The legacy return code for this failure is EIO, however EACCES 19145 * seems more appropriate for a reservation conflict. 19146 */ 19147 if (sd_retry_on_reservation_conflict == 0) { 19148 SD_ERROR(SD_LOG_IO, un, 19149 "sd_handle_resv_conflict: Device Reserved\n"); 19150 sd_return_failed_command(un, bp, EIO); 19151 return; 19152 } 19153 19154 /* 19155 * Retry the command if we can. 19156 * 19157 * Note: The legacy return code for this failure is EIO, however EACCES 19158 * seems more appropriate for a reservation conflict. 19159 */ 19160 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO, 19161 (clock_t)2, NULL); 19162 } 19163 19164 19165 19166 /* 19167 * Function: sd_pkt_status_qfull 19168 * 19169 * Description: Handle a QUEUE FULL condition from the target. This can 19170 * occur if the HBA does not handle the queue full condition. 19171 * (Basically this means third-party HBAs as Sun HBAs will 19172 * handle the queue full condition.) Note that if there are 19173 * some commands already in the transport, then the queue full 19174 * has occurred because the queue for this nexus is actually 19175 * full. If there are no commands in the transport, then the 19176 * queue full is resulting from some other initiator or lun 19177 * consuming all the resources at the target. 19178 * 19179 * Context: May be called from interrupt context 19180 */ 19181 19182 static void 19183 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, 19184 struct sd_xbuf *xp, struct scsi_pkt *pktp) 19185 { 19186 ASSERT(un != NULL); 19187 ASSERT(mutex_owned(SD_MUTEX(un))); 19188 ASSERT(bp != NULL); 19189 ASSERT(xp != NULL); 19190 ASSERT(pktp != NULL); 19191 19192 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19193 "sd_pkt_status_qfull: entry\n"); 19194 19195 /* 19196 * Just lower the QFULL throttle and retry the command. Note that 19197 * we do not limit the number of retries here. 19198 */ 19199 sd_reduce_throttle(un, SD_THROTTLE_QFULL); 19200 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0, 19201 SD_RESTART_TIMEOUT, NULL); 19202 19203 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19204 "sd_pkt_status_qfull: exit\n"); 19205 } 19206 19207 19208 /* 19209 * Function: sd_reset_target 19210 * 19211 * Description: Issue a scsi_reset(9F), with either RESET_LUN, 19212 * RESET_TARGET, or RESET_ALL. 19213 * 19214 * Context: May be called under interrupt context. 19215 */ 19216 19217 static void 19218 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp) 19219 { 19220 int rval = 0; 19221 19222 ASSERT(un != NULL); 19223 ASSERT(mutex_owned(SD_MUTEX(un))); 19224 ASSERT(pktp != NULL); 19225 19226 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n"); 19227 19228 /* 19229 * No need to reset if the transport layer has already done so. 19230 */ 19231 if ((pktp->pkt_statistics & 19232 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) { 19233 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19234 "sd_reset_target: no reset\n"); 19235 return; 19236 } 19237 19238 mutex_exit(SD_MUTEX(un)); 19239 19240 if (un->un_f_allow_bus_device_reset == TRUE) { 19241 if (un->un_f_lun_reset_enabled == TRUE) { 19242 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19243 "sd_reset_target: RESET_LUN\n"); 19244 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 19245 } 19246 if (rval == 0) { 19247 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19248 "sd_reset_target: RESET_TARGET\n"); 19249 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 19250 } 19251 } 19252 19253 if (rval == 0) { 19254 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 19255 "sd_reset_target: RESET_ALL\n"); 19256 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 19257 } 19258 19259 mutex_enter(SD_MUTEX(un)); 19260 19261 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n"); 19262 } 19263 19264 19265 /* 19266 * Function: sd_media_change_task 19267 * 19268 * Description: Recovery action for CDROM to become available. 19269 * 19270 * Context: Executes in a taskq() thread context 19271 */ 19272 19273 static void 19274 sd_media_change_task(void *arg) 19275 { 19276 struct scsi_pkt *pktp = arg; 19277 struct sd_lun *un; 19278 struct buf *bp; 19279 struct sd_xbuf *xp; 19280 int err = 0; 19281 int retry_count = 0; 19282 int retry_limit = SD_UNIT_ATTENTION_RETRY/10; 19283 struct sd_sense_info si; 19284 19285 ASSERT(pktp != NULL); 19286 bp = (struct buf *)pktp->pkt_private; 19287 ASSERT(bp != NULL); 19288 xp = SD_GET_XBUF(bp); 19289 ASSERT(xp != NULL); 19290 un = SD_GET_UN(bp); 19291 ASSERT(un != NULL); 19292 ASSERT(!mutex_owned(SD_MUTEX(un))); 19293 ASSERT(un->un_f_monitor_media_state); 19294 19295 si.ssi_severity = SCSI_ERR_INFO; 19296 si.ssi_pfa_flag = FALSE; 19297 19298 /* 19299 * When a reset is issued on a CDROM, it takes a long time to 19300 * recover. First few attempts to read capacity and other things 19301 * related to handling unit attention fail (with a ASC 0x4 and 19302 * ASCQ 0x1). In that case we want to do enough retries and we want 19303 * to limit the retries in other cases of genuine failures like 19304 * no media in drive. 19305 */ 19306 while (retry_count++ < retry_limit) { 19307 if ((err = sd_handle_mchange(un)) == 0) { 19308 break; 19309 } 19310 if (err == EAGAIN) { 19311 retry_limit = SD_UNIT_ATTENTION_RETRY; 19312 } 19313 /* Sleep for 0.5 sec. & try again */ 19314 delay(drv_usectohz(500000)); 19315 } 19316 19317 /* 19318 * Dispatch (retry or fail) the original command here, 19319 * along with appropriate console messages.... 19320 * 19321 * Must grab the mutex before calling sd_retry_command, 19322 * sd_print_sense_msg and sd_return_failed_command. 19323 */ 19324 mutex_enter(SD_MUTEX(un)); 19325 if (err != SD_CMD_SUCCESS) { 19326 SD_UPDATE_ERRSTATS(un, sd_harderrs); 19327 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err); 19328 si.ssi_severity = SCSI_ERR_FATAL; 19329 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED); 19330 sd_return_failed_command(un, bp, EIO); 19331 } else { 19332 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg, 19333 &si, EIO, (clock_t)0, NULL); 19334 } 19335 mutex_exit(SD_MUTEX(un)); 19336 } 19337 19338 19339 19340 /* 19341 * Function: sd_handle_mchange 19342 * 19343 * Description: Perform geometry validation & other recovery when CDROM 19344 * has been removed from drive. 19345 * 19346 * Return Code: 0 for success 19347 * errno-type return code of either sd_send_scsi_DOORLOCK() or 19348 * sd_send_scsi_READ_CAPACITY() 19349 * 19350 * Context: Executes in a taskq() thread context 19351 */ 19352 19353 static int 19354 sd_handle_mchange(struct sd_lun *un) 19355 { 19356 uint64_t capacity; 19357 uint32_t lbasize; 19358 int rval; 19359 19360 ASSERT(!mutex_owned(SD_MUTEX(un))); 19361 ASSERT(un->un_f_monitor_media_state); 19362 19363 if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 19364 SD_PATH_DIRECT_PRIORITY)) != 0) { 19365 return (rval); 19366 } 19367 19368 mutex_enter(SD_MUTEX(un)); 19369 sd_update_block_info(un, lbasize, capacity); 19370 19371 if (un->un_errstats != NULL) { 19372 struct sd_errstats *stp = 19373 (struct sd_errstats *)un->un_errstats->ks_data; 19374 stp->sd_capacity.value.ui64 = (uint64_t) 19375 ((uint64_t)un->un_blockcount * 19376 (uint64_t)un->un_tgt_blocksize); 19377 } 19378 19379 /* 19380 * Note: Maybe let the strategy/partitioning chain worry about getting 19381 * valid geometry. 19382 */ 19383 un->un_f_geometry_is_valid = FALSE; 19384 (void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY); 19385 if (un->un_f_geometry_is_valid == FALSE) { 19386 mutex_exit(SD_MUTEX(un)); 19387 return (EIO); 19388 } 19389 19390 mutex_exit(SD_MUTEX(un)); 19391 19392 /* 19393 * Try to lock the door 19394 */ 19395 return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 19396 SD_PATH_DIRECT_PRIORITY)); 19397 } 19398 19399 19400 /* 19401 * Function: sd_send_scsi_DOORLOCK 19402 * 19403 * Description: Issue the scsi DOOR LOCK command 19404 * 19405 * Arguments: un - pointer to driver soft state (unit) structure for 19406 * this target. 19407 * flag - SD_REMOVAL_ALLOW 19408 * SD_REMOVAL_PREVENT 19409 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19410 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19411 * to use the USCSI "direct" chain and bypass the normal 19412 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19413 * command is issued as part of an error recovery action. 19414 * 19415 * Return Code: 0 - Success 19416 * errno return code from sd_send_scsi_cmd() 19417 * 19418 * Context: Can sleep. 19419 */ 19420 19421 static int 19422 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag) 19423 { 19424 union scsi_cdb cdb; 19425 struct uscsi_cmd ucmd_buf; 19426 struct scsi_extended_sense sense_buf; 19427 int status; 19428 19429 ASSERT(un != NULL); 19430 ASSERT(!mutex_owned(SD_MUTEX(un))); 19431 19432 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un); 19433 19434 /* already determined doorlock is not supported, fake success */ 19435 if (un->un_f_doorlock_supported == FALSE) { 19436 return (0); 19437 } 19438 19439 bzero(&cdb, sizeof (cdb)); 19440 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19441 19442 cdb.scc_cmd = SCMD_DOORLOCK; 19443 cdb.cdb_opaque[4] = (uchar_t)flag; 19444 19445 ucmd_buf.uscsi_cdb = (char *)&cdb; 19446 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19447 ucmd_buf.uscsi_bufaddr = NULL; 19448 ucmd_buf.uscsi_buflen = 0; 19449 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19450 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19451 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19452 ucmd_buf.uscsi_timeout = 15; 19453 19454 SD_TRACE(SD_LOG_IO, un, 19455 "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n"); 19456 19457 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19458 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19459 19460 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) && 19461 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19462 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) { 19463 /* fake success and skip subsequent doorlock commands */ 19464 un->un_f_doorlock_supported = FALSE; 19465 return (0); 19466 } 19467 19468 return (status); 19469 } 19470 19471 /* 19472 * Function: sd_send_scsi_READ_CAPACITY 19473 * 19474 * Description: This routine uses the scsi READ CAPACITY command to determine 19475 * the device capacity in number of blocks and the device native 19476 * block size. If this function returns a failure, then the 19477 * values in *capp and *lbap are undefined. If the capacity 19478 * returned is 0xffffffff then the lun is too large for a 19479 * normal READ CAPACITY command and the results of a 19480 * READ CAPACITY 16 will be used instead. 19481 * 19482 * Arguments: un - ptr to soft state struct for the target 19483 * capp - ptr to unsigned 64-bit variable to receive the 19484 * capacity value from the command. 19485 * lbap - ptr to unsigned 32-bit varaible to receive the 19486 * block size value from the command 19487 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19488 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19489 * to use the USCSI "direct" chain and bypass the normal 19490 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19491 * command is issued as part of an error recovery action. 19492 * 19493 * Return Code: 0 - Success 19494 * EIO - IO error 19495 * EACCES - Reservation conflict detected 19496 * EAGAIN - Device is becoming ready 19497 * errno return code from sd_send_scsi_cmd() 19498 * 19499 * Context: Can sleep. Blocks until command completes. 19500 */ 19501 19502 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity) 19503 19504 static int 19505 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap, 19506 int path_flag) 19507 { 19508 struct scsi_extended_sense sense_buf; 19509 struct uscsi_cmd ucmd_buf; 19510 union scsi_cdb cdb; 19511 uint32_t *capacity_buf; 19512 uint64_t capacity; 19513 uint32_t lbasize; 19514 int status; 19515 19516 ASSERT(un != NULL); 19517 ASSERT(!mutex_owned(SD_MUTEX(un))); 19518 ASSERT(capp != NULL); 19519 ASSERT(lbap != NULL); 19520 19521 SD_TRACE(SD_LOG_IO, un, 19522 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19523 19524 /* 19525 * First send a READ_CAPACITY command to the target. 19526 * (This command is mandatory under SCSI-2.) 19527 * 19528 * Set up the CDB for the READ_CAPACITY command. The Partial 19529 * Medium Indicator bit is cleared. The address field must be 19530 * zero if the PMI bit is zero. 19531 */ 19532 bzero(&cdb, sizeof (cdb)); 19533 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19534 19535 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP); 19536 19537 cdb.scc_cmd = SCMD_READ_CAPACITY; 19538 19539 ucmd_buf.uscsi_cdb = (char *)&cdb; 19540 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 19541 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf; 19542 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE; 19543 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19544 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19545 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19546 ucmd_buf.uscsi_timeout = 60; 19547 19548 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19549 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19550 19551 switch (status) { 19552 case 0: 19553 /* Return failure if we did not get valid capacity data. */ 19554 if (ucmd_buf.uscsi_resid != 0) { 19555 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19556 return (EIO); 19557 } 19558 19559 /* 19560 * Read capacity and block size from the READ CAPACITY 10 data. 19561 * This data may be adjusted later due to device specific 19562 * issues. 19563 * 19564 * According to the SCSI spec, the READ CAPACITY 10 19565 * command returns the following: 19566 * 19567 * bytes 0-3: Maximum logical block address available. 19568 * (MSB in byte:0 & LSB in byte:3) 19569 * 19570 * bytes 4-7: Block length in bytes 19571 * (MSB in byte:4 & LSB in byte:7) 19572 * 19573 */ 19574 capacity = BE_32(capacity_buf[0]); 19575 lbasize = BE_32(capacity_buf[1]); 19576 19577 /* 19578 * Done with capacity_buf 19579 */ 19580 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19581 19582 /* 19583 * if the reported capacity is set to all 0xf's, then 19584 * this disk is too large and requires SBC-2 commands. 19585 * Reissue the request using READ CAPACITY 16. 19586 */ 19587 if (capacity == 0xffffffff) { 19588 status = sd_send_scsi_READ_CAPACITY_16(un, &capacity, 19589 &lbasize, path_flag); 19590 if (status != 0) { 19591 return (status); 19592 } 19593 } 19594 break; /* Success! */ 19595 case EIO: 19596 switch (ucmd_buf.uscsi_status) { 19597 case STATUS_RESERVATION_CONFLICT: 19598 status = EACCES; 19599 break; 19600 case STATUS_CHECK: 19601 /* 19602 * Check condition; look for ASC/ASCQ of 0x04/0x01 19603 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19604 */ 19605 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19606 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19607 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19608 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19609 return (EAGAIN); 19610 } 19611 break; 19612 default: 19613 break; 19614 } 19615 /* FALLTHRU */ 19616 default: 19617 kmem_free(capacity_buf, SD_CAPACITY_SIZE); 19618 return (status); 19619 } 19620 19621 /* 19622 * Some ATAPI CD-ROM drives report inaccurate LBA size values 19623 * (2352 and 0 are common) so for these devices always force the value 19624 * to 2048 as required by the ATAPI specs. 19625 */ 19626 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) { 19627 lbasize = 2048; 19628 } 19629 19630 /* 19631 * Get the maximum LBA value from the READ CAPACITY data. 19632 * Here we assume that the Partial Medium Indicator (PMI) bit 19633 * was cleared when issuing the command. This means that the LBA 19634 * returned from the device is the LBA of the last logical block 19635 * on the logical unit. The actual logical block count will be 19636 * this value plus one. 19637 * 19638 * Currently the capacity is saved in terms of un->un_sys_blocksize, 19639 * so scale the capacity value to reflect this. 19640 */ 19641 capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize); 19642 19643 #if defined(__i386) || defined(__amd64) 19644 /* 19645 * Refer to comments related to off-by-1 at the 19646 * header of this file. 19647 * Treat 1TB disk as (1T - 512)B. 19648 */ 19649 if (un->un_f_capacity_adjusted == 1) 19650 capacity = DK_MAX_BLOCKS; 19651 #endif 19652 19653 /* 19654 * Copy the values from the READ CAPACITY command into the space 19655 * provided by the caller. 19656 */ 19657 *capp = capacity; 19658 *lbap = lbasize; 19659 19660 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: " 19661 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19662 19663 /* 19664 * Both the lbasize and capacity from the device must be nonzero, 19665 * otherwise we assume that the values are not valid and return 19666 * failure to the caller. (4203735) 19667 */ 19668 if ((capacity == 0) || (lbasize == 0)) { 19669 return (EIO); 19670 } 19671 19672 return (0); 19673 } 19674 19675 /* 19676 * Function: sd_send_scsi_READ_CAPACITY_16 19677 * 19678 * Description: This routine uses the scsi READ CAPACITY 16 command to 19679 * determine the device capacity in number of blocks and the 19680 * device native block size. If this function returns a failure, 19681 * then the values in *capp and *lbap are undefined. 19682 * This routine should always be called by 19683 * sd_send_scsi_READ_CAPACITY which will appy any device 19684 * specific adjustments to capacity and lbasize. 19685 * 19686 * Arguments: un - ptr to soft state struct for the target 19687 * capp - ptr to unsigned 64-bit variable to receive the 19688 * capacity value from the command. 19689 * lbap - ptr to unsigned 32-bit varaible to receive the 19690 * block size value from the command 19691 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19692 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19693 * to use the USCSI "direct" chain and bypass the normal 19694 * command waitq. SD_PATH_DIRECT_PRIORITY is used when 19695 * this command is issued as part of an error recovery 19696 * action. 19697 * 19698 * Return Code: 0 - Success 19699 * EIO - IO error 19700 * EACCES - Reservation conflict detected 19701 * EAGAIN - Device is becoming ready 19702 * errno return code from sd_send_scsi_cmd() 19703 * 19704 * Context: Can sleep. Blocks until command completes. 19705 */ 19706 19707 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16) 19708 19709 static int 19710 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp, 19711 uint32_t *lbap, int path_flag) 19712 { 19713 struct scsi_extended_sense sense_buf; 19714 struct uscsi_cmd ucmd_buf; 19715 union scsi_cdb cdb; 19716 uint64_t *capacity16_buf; 19717 uint64_t capacity; 19718 uint32_t lbasize; 19719 int status; 19720 19721 ASSERT(un != NULL); 19722 ASSERT(!mutex_owned(SD_MUTEX(un))); 19723 ASSERT(capp != NULL); 19724 ASSERT(lbap != NULL); 19725 19726 SD_TRACE(SD_LOG_IO, un, 19727 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un); 19728 19729 /* 19730 * First send a READ_CAPACITY_16 command to the target. 19731 * 19732 * Set up the CDB for the READ_CAPACITY_16 command. The Partial 19733 * Medium Indicator bit is cleared. The address field must be 19734 * zero if the PMI bit is zero. 19735 */ 19736 bzero(&cdb, sizeof (cdb)); 19737 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19738 19739 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP); 19740 19741 ucmd_buf.uscsi_cdb = (char *)&cdb; 19742 ucmd_buf.uscsi_cdblen = CDB_GROUP4; 19743 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf; 19744 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE; 19745 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19746 ucmd_buf.uscsi_rqlen = sizeof (sense_buf); 19747 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 19748 ucmd_buf.uscsi_timeout = 60; 19749 19750 /* 19751 * Read Capacity (16) is a Service Action In command. One 19752 * command byte (0x9E) is overloaded for multiple operations, 19753 * with the second CDB byte specifying the desired operation 19754 */ 19755 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4; 19756 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4; 19757 19758 /* 19759 * Fill in allocation length field 19760 */ 19761 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen); 19762 19763 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19764 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19765 19766 switch (status) { 19767 case 0: 19768 /* Return failure if we did not get valid capacity data. */ 19769 if (ucmd_buf.uscsi_resid > 20) { 19770 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19771 return (EIO); 19772 } 19773 19774 /* 19775 * Read capacity and block size from the READ CAPACITY 10 data. 19776 * This data may be adjusted later due to device specific 19777 * issues. 19778 * 19779 * According to the SCSI spec, the READ CAPACITY 10 19780 * command returns the following: 19781 * 19782 * bytes 0-7: Maximum logical block address available. 19783 * (MSB in byte:0 & LSB in byte:7) 19784 * 19785 * bytes 8-11: Block length in bytes 19786 * (MSB in byte:8 & LSB in byte:11) 19787 * 19788 */ 19789 capacity = BE_64(capacity16_buf[0]); 19790 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]); 19791 19792 /* 19793 * Done with capacity16_buf 19794 */ 19795 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19796 19797 /* 19798 * if the reported capacity is set to all 0xf's, then 19799 * this disk is too large. This could only happen with 19800 * a device that supports LBAs larger than 64 bits which 19801 * are not defined by any current T10 standards. 19802 */ 19803 if (capacity == 0xffffffffffffffff) { 19804 return (EIO); 19805 } 19806 break; /* Success! */ 19807 case EIO: 19808 switch (ucmd_buf.uscsi_status) { 19809 case STATUS_RESERVATION_CONFLICT: 19810 status = EACCES; 19811 break; 19812 case STATUS_CHECK: 19813 /* 19814 * Check condition; look for ASC/ASCQ of 0x04/0x01 19815 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY) 19816 */ 19817 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 19818 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) && 19819 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) { 19820 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19821 return (EAGAIN); 19822 } 19823 break; 19824 default: 19825 break; 19826 } 19827 /* FALLTHRU */ 19828 default: 19829 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE); 19830 return (status); 19831 } 19832 19833 *capp = capacity; 19834 *lbap = lbasize; 19835 19836 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: " 19837 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize); 19838 19839 return (0); 19840 } 19841 19842 19843 /* 19844 * Function: sd_send_scsi_START_STOP_UNIT 19845 * 19846 * Description: Issue a scsi START STOP UNIT command to the target. 19847 * 19848 * Arguments: un - pointer to driver soft state (unit) structure for 19849 * this target. 19850 * flag - SD_TARGET_START 19851 * SD_TARGET_STOP 19852 * SD_TARGET_EJECT 19853 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 19854 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 19855 * to use the USCSI "direct" chain and bypass the normal 19856 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this 19857 * command is issued as part of an error recovery action. 19858 * 19859 * Return Code: 0 - Success 19860 * EIO - IO error 19861 * EACCES - Reservation conflict detected 19862 * ENXIO - Not Ready, medium not present 19863 * errno return code from sd_send_scsi_cmd() 19864 * 19865 * Context: Can sleep. 19866 */ 19867 19868 static int 19869 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag) 19870 { 19871 struct scsi_extended_sense sense_buf; 19872 union scsi_cdb cdb; 19873 struct uscsi_cmd ucmd_buf; 19874 int status; 19875 19876 ASSERT(un != NULL); 19877 ASSERT(!mutex_owned(SD_MUTEX(un))); 19878 19879 SD_TRACE(SD_LOG_IO, un, 19880 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un); 19881 19882 if (un->un_f_check_start_stop && 19883 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) && 19884 (un->un_f_start_stop_supported != TRUE)) { 19885 return (0); 19886 } 19887 19888 bzero(&cdb, sizeof (cdb)); 19889 bzero(&ucmd_buf, sizeof (ucmd_buf)); 19890 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 19891 19892 cdb.scc_cmd = SCMD_START_STOP; 19893 cdb.cdb_opaque[4] = (uchar_t)flag; 19894 19895 ucmd_buf.uscsi_cdb = (char *)&cdb; 19896 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 19897 ucmd_buf.uscsi_bufaddr = NULL; 19898 ucmd_buf.uscsi_buflen = 0; 19899 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 19900 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 19901 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 19902 ucmd_buf.uscsi_timeout = 200; 19903 19904 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 19905 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 19906 19907 switch (status) { 19908 case 0: 19909 break; /* Success! */ 19910 case EIO: 19911 switch (ucmd_buf.uscsi_status) { 19912 case STATUS_RESERVATION_CONFLICT: 19913 status = EACCES; 19914 break; 19915 case STATUS_CHECK: 19916 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) { 19917 switch (scsi_sense_key( 19918 (uint8_t *)&sense_buf)) { 19919 case KEY_ILLEGAL_REQUEST: 19920 status = ENOTSUP; 19921 break; 19922 case KEY_NOT_READY: 19923 if (scsi_sense_asc( 19924 (uint8_t *)&sense_buf) 19925 == 0x3A) { 19926 status = ENXIO; 19927 } 19928 break; 19929 default: 19930 break; 19931 } 19932 } 19933 break; 19934 default: 19935 break; 19936 } 19937 break; 19938 default: 19939 break; 19940 } 19941 19942 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n"); 19943 19944 return (status); 19945 } 19946 19947 19948 /* 19949 * Function: sd_start_stop_unit_callback 19950 * 19951 * Description: timeout(9F) callback to begin recovery process for a 19952 * device that has spun down. 19953 * 19954 * Arguments: arg - pointer to associated softstate struct. 19955 * 19956 * Context: Executes in a timeout(9F) thread context 19957 */ 19958 19959 static void 19960 sd_start_stop_unit_callback(void *arg) 19961 { 19962 struct sd_lun *un = arg; 19963 ASSERT(un != NULL); 19964 ASSERT(!mutex_owned(SD_MUTEX(un))); 19965 19966 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n"); 19967 19968 (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP); 19969 } 19970 19971 19972 /* 19973 * Function: sd_start_stop_unit_task 19974 * 19975 * Description: Recovery procedure when a drive is spun down. 19976 * 19977 * Arguments: arg - pointer to associated softstate struct. 19978 * 19979 * Context: Executes in a taskq() thread context 19980 */ 19981 19982 static void 19983 sd_start_stop_unit_task(void *arg) 19984 { 19985 struct sd_lun *un = arg; 19986 19987 ASSERT(un != NULL); 19988 ASSERT(!mutex_owned(SD_MUTEX(un))); 19989 19990 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n"); 19991 19992 /* 19993 * Some unformatted drives report not ready error, no need to 19994 * restart if format has been initiated. 19995 */ 19996 mutex_enter(SD_MUTEX(un)); 19997 if (un->un_f_format_in_progress == TRUE) { 19998 mutex_exit(SD_MUTEX(un)); 19999 return; 20000 } 20001 mutex_exit(SD_MUTEX(un)); 20002 20003 /* 20004 * When a START STOP command is issued from here, it is part of a 20005 * failure recovery operation and must be issued before any other 20006 * commands, including any pending retries. Thus it must be sent 20007 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up 20008 * succeeds or not, we will start I/O after the attempt. 20009 */ 20010 (void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 20011 SD_PATH_DIRECT_PRIORITY); 20012 20013 /* 20014 * The above call blocks until the START_STOP_UNIT command completes. 20015 * Now that it has completed, we must re-try the original IO that 20016 * received the NOT READY condition in the first place. There are 20017 * three possible conditions here: 20018 * 20019 * (1) The original IO is on un_retry_bp. 20020 * (2) The original IO is on the regular wait queue, and un_retry_bp 20021 * is NULL. 20022 * (3) The original IO is on the regular wait queue, and un_retry_bp 20023 * points to some other, unrelated bp. 20024 * 20025 * For each case, we must call sd_start_cmds() with un_retry_bp 20026 * as the argument. If un_retry_bp is NULL, this will initiate 20027 * processing of the regular wait queue. If un_retry_bp is not NULL, 20028 * then this will process the bp on un_retry_bp. That may or may not 20029 * be the original IO, but that does not matter: the important thing 20030 * is to keep the IO processing going at this point. 20031 * 20032 * Note: This is a very specific error recovery sequence associated 20033 * with a drive that is not spun up. We attempt a START_STOP_UNIT and 20034 * serialize the I/O with completion of the spin-up. 20035 */ 20036 mutex_enter(SD_MUTEX(un)); 20037 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, 20038 "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n", 20039 un, un->un_retry_bp); 20040 un->un_startstop_timeid = NULL; /* Timeout is no longer pending */ 20041 sd_start_cmds(un, un->un_retry_bp); 20042 mutex_exit(SD_MUTEX(un)); 20043 20044 SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n"); 20045 } 20046 20047 20048 /* 20049 * Function: sd_send_scsi_INQUIRY 20050 * 20051 * Description: Issue the scsi INQUIRY command. 20052 * 20053 * Arguments: un 20054 * bufaddr 20055 * buflen 20056 * evpd 20057 * page_code 20058 * page_length 20059 * 20060 * Return Code: 0 - Success 20061 * errno return code from sd_send_scsi_cmd() 20062 * 20063 * Context: Can sleep. Does not return until command is completed. 20064 */ 20065 20066 static int 20067 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen, 20068 uchar_t evpd, uchar_t page_code, size_t *residp) 20069 { 20070 union scsi_cdb cdb; 20071 struct uscsi_cmd ucmd_buf; 20072 int status; 20073 20074 ASSERT(un != NULL); 20075 ASSERT(!mutex_owned(SD_MUTEX(un))); 20076 ASSERT(bufaddr != NULL); 20077 20078 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un); 20079 20080 bzero(&cdb, sizeof (cdb)); 20081 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20082 bzero(bufaddr, buflen); 20083 20084 cdb.scc_cmd = SCMD_INQUIRY; 20085 cdb.cdb_opaque[1] = evpd; 20086 cdb.cdb_opaque[2] = page_code; 20087 FORMG0COUNT(&cdb, buflen); 20088 20089 ucmd_buf.uscsi_cdb = (char *)&cdb; 20090 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20091 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20092 ucmd_buf.uscsi_buflen = buflen; 20093 ucmd_buf.uscsi_rqbuf = NULL; 20094 ucmd_buf.uscsi_rqlen = 0; 20095 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT; 20096 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */ 20097 20098 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20099 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT); 20100 20101 if ((status == 0) && (residp != NULL)) { 20102 *residp = ucmd_buf.uscsi_resid; 20103 } 20104 20105 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n"); 20106 20107 return (status); 20108 } 20109 20110 20111 /* 20112 * Function: sd_send_scsi_TEST_UNIT_READY 20113 * 20114 * Description: Issue the scsi TEST UNIT READY command. 20115 * This routine can be told to set the flag USCSI_DIAGNOSE to 20116 * prevent retrying failed commands. Use this when the intent 20117 * is either to check for device readiness, to clear a Unit 20118 * Attention, or to clear any outstanding sense data. 20119 * However under specific conditions the expected behavior 20120 * is for retries to bring a device ready, so use the flag 20121 * with caution. 20122 * 20123 * Arguments: un 20124 * flag: SD_CHECK_FOR_MEDIA: return ENXIO if no media present 20125 * SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE. 20126 * 0: dont check for media present, do retries on cmd. 20127 * 20128 * Return Code: 0 - Success 20129 * EIO - IO error 20130 * EACCES - Reservation conflict detected 20131 * ENXIO - Not Ready, medium not present 20132 * errno return code from sd_send_scsi_cmd() 20133 * 20134 * Context: Can sleep. Does not return until command is completed. 20135 */ 20136 20137 static int 20138 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag) 20139 { 20140 struct scsi_extended_sense sense_buf; 20141 union scsi_cdb cdb; 20142 struct uscsi_cmd ucmd_buf; 20143 int status; 20144 20145 ASSERT(un != NULL); 20146 ASSERT(!mutex_owned(SD_MUTEX(un))); 20147 20148 SD_TRACE(SD_LOG_IO, un, 20149 "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un); 20150 20151 /* 20152 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect 20153 * timeouts when they receive a TUR and the queue is not empty. Check 20154 * the configuration flag set during attach (indicating the drive has 20155 * this firmware bug) and un_ncmds_in_transport before issuing the 20156 * TUR. If there are 20157 * pending commands return success, this is a bit arbitrary but is ok 20158 * for non-removables (i.e. the eliteI disks) and non-clustering 20159 * configurations. 20160 */ 20161 if (un->un_f_cfg_tur_check == TRUE) { 20162 mutex_enter(SD_MUTEX(un)); 20163 if (un->un_ncmds_in_transport != 0) { 20164 mutex_exit(SD_MUTEX(un)); 20165 return (0); 20166 } 20167 mutex_exit(SD_MUTEX(un)); 20168 } 20169 20170 bzero(&cdb, sizeof (cdb)); 20171 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20172 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20173 20174 cdb.scc_cmd = SCMD_TEST_UNIT_READY; 20175 20176 ucmd_buf.uscsi_cdb = (char *)&cdb; 20177 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 20178 ucmd_buf.uscsi_bufaddr = NULL; 20179 ucmd_buf.uscsi_buflen = 0; 20180 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20181 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20182 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20183 20184 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */ 20185 if ((flag & SD_DONT_RETRY_TUR) != 0) { 20186 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE; 20187 } 20188 ucmd_buf.uscsi_timeout = 60; 20189 20190 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20191 UIO_SYSSPACE, UIO_SYSSPACE, 20192 ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD)); 20193 20194 switch (status) { 20195 case 0: 20196 break; /* Success! */ 20197 case EIO: 20198 switch (ucmd_buf.uscsi_status) { 20199 case STATUS_RESERVATION_CONFLICT: 20200 status = EACCES; 20201 break; 20202 case STATUS_CHECK: 20203 if ((flag & SD_CHECK_FOR_MEDIA) == 0) { 20204 break; 20205 } 20206 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20207 (scsi_sense_key((uint8_t *)&sense_buf) == 20208 KEY_NOT_READY) && 20209 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) { 20210 status = ENXIO; 20211 } 20212 break; 20213 default: 20214 break; 20215 } 20216 break; 20217 default: 20218 break; 20219 } 20220 20221 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n"); 20222 20223 return (status); 20224 } 20225 20226 20227 /* 20228 * Function: sd_send_scsi_PERSISTENT_RESERVE_IN 20229 * 20230 * Description: Issue the scsi PERSISTENT RESERVE IN command. 20231 * 20232 * Arguments: un 20233 * 20234 * Return Code: 0 - Success 20235 * EACCES 20236 * ENOTSUP 20237 * errno return code from sd_send_scsi_cmd() 20238 * 20239 * Context: Can sleep. Does not return until command is completed. 20240 */ 20241 20242 static int 20243 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t usr_cmd, 20244 uint16_t data_len, uchar_t *data_bufp) 20245 { 20246 struct scsi_extended_sense sense_buf; 20247 union scsi_cdb cdb; 20248 struct uscsi_cmd ucmd_buf; 20249 int status; 20250 int no_caller_buf = FALSE; 20251 20252 ASSERT(un != NULL); 20253 ASSERT(!mutex_owned(SD_MUTEX(un))); 20254 ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV)); 20255 20256 SD_TRACE(SD_LOG_IO, un, 20257 "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un); 20258 20259 bzero(&cdb, sizeof (cdb)); 20260 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20261 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20262 if (data_bufp == NULL) { 20263 /* Allocate a default buf if the caller did not give one */ 20264 ASSERT(data_len == 0); 20265 data_len = MHIOC_RESV_KEY_SIZE; 20266 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP); 20267 no_caller_buf = TRUE; 20268 } 20269 20270 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN; 20271 cdb.cdb_opaque[1] = usr_cmd; 20272 FORMG1COUNT(&cdb, data_len); 20273 20274 ucmd_buf.uscsi_cdb = (char *)&cdb; 20275 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20276 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp; 20277 ucmd_buf.uscsi_buflen = data_len; 20278 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20279 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20280 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20281 ucmd_buf.uscsi_timeout = 60; 20282 20283 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20284 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20285 20286 switch (status) { 20287 case 0: 20288 break; /* Success! */ 20289 case EIO: 20290 switch (ucmd_buf.uscsi_status) { 20291 case STATUS_RESERVATION_CONFLICT: 20292 status = EACCES; 20293 break; 20294 case STATUS_CHECK: 20295 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20296 (scsi_sense_key((uint8_t *)&sense_buf) == 20297 KEY_ILLEGAL_REQUEST)) { 20298 status = ENOTSUP; 20299 } 20300 break; 20301 default: 20302 break; 20303 } 20304 break; 20305 default: 20306 break; 20307 } 20308 20309 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n"); 20310 20311 if (no_caller_buf == TRUE) { 20312 kmem_free(data_bufp, data_len); 20313 } 20314 20315 return (status); 20316 } 20317 20318 20319 /* 20320 * Function: sd_send_scsi_PERSISTENT_RESERVE_OUT 20321 * 20322 * Description: This routine is the driver entry point for handling CD-ROM 20323 * multi-host persistent reservation requests (MHIOCGRP_INKEYS, 20324 * MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the 20325 * device. 20326 * 20327 * Arguments: un - Pointer to soft state struct for the target. 20328 * usr_cmd SCSI-3 reservation facility command (one of 20329 * SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE, 20330 * SD_SCSI3_PREEMPTANDABORT) 20331 * usr_bufp - user provided pointer register, reserve descriptor or 20332 * preempt and abort structure (mhioc_register_t, 20333 * mhioc_resv_desc_t, mhioc_preemptandabort_t) 20334 * 20335 * Return Code: 0 - Success 20336 * EACCES 20337 * ENOTSUP 20338 * errno return code from sd_send_scsi_cmd() 20339 * 20340 * Context: Can sleep. Does not return until command is completed. 20341 */ 20342 20343 static int 20344 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd, 20345 uchar_t *usr_bufp) 20346 { 20347 struct scsi_extended_sense sense_buf; 20348 union scsi_cdb cdb; 20349 struct uscsi_cmd ucmd_buf; 20350 int status; 20351 uchar_t data_len = sizeof (sd_prout_t); 20352 sd_prout_t *prp; 20353 20354 ASSERT(un != NULL); 20355 ASSERT(!mutex_owned(SD_MUTEX(un))); 20356 ASSERT(data_len == 24); /* required by scsi spec */ 20357 20358 SD_TRACE(SD_LOG_IO, un, 20359 "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un); 20360 20361 if (usr_bufp == NULL) { 20362 return (EINVAL); 20363 } 20364 20365 bzero(&cdb, sizeof (cdb)); 20366 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20367 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20368 prp = kmem_zalloc(data_len, KM_SLEEP); 20369 20370 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT; 20371 cdb.cdb_opaque[1] = usr_cmd; 20372 FORMG1COUNT(&cdb, data_len); 20373 20374 ucmd_buf.uscsi_cdb = (char *)&cdb; 20375 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 20376 ucmd_buf.uscsi_bufaddr = (caddr_t)prp; 20377 ucmd_buf.uscsi_buflen = data_len; 20378 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20379 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20380 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20381 ucmd_buf.uscsi_timeout = 60; 20382 20383 switch (usr_cmd) { 20384 case SD_SCSI3_REGISTER: { 20385 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp; 20386 20387 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20388 bcopy(ptr->newkey.key, prp->service_key, 20389 MHIOC_RESV_KEY_SIZE); 20390 prp->aptpl = ptr->aptpl; 20391 break; 20392 } 20393 case SD_SCSI3_RESERVE: 20394 case SD_SCSI3_RELEASE: { 20395 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp; 20396 20397 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20398 prp->scope_address = BE_32(ptr->scope_specific_addr); 20399 cdb.cdb_opaque[2] = ptr->type; 20400 break; 20401 } 20402 case SD_SCSI3_PREEMPTANDABORT: { 20403 mhioc_preemptandabort_t *ptr = 20404 (mhioc_preemptandabort_t *)usr_bufp; 20405 20406 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE); 20407 bcopy(ptr->victim_key.key, prp->service_key, 20408 MHIOC_RESV_KEY_SIZE); 20409 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr); 20410 cdb.cdb_opaque[2] = ptr->resvdesc.type; 20411 ucmd_buf.uscsi_flags |= USCSI_HEAD; 20412 break; 20413 } 20414 case SD_SCSI3_REGISTERANDIGNOREKEY: 20415 { 20416 mhioc_registerandignorekey_t *ptr; 20417 ptr = (mhioc_registerandignorekey_t *)usr_bufp; 20418 bcopy(ptr->newkey.key, 20419 prp->service_key, MHIOC_RESV_KEY_SIZE); 20420 prp->aptpl = ptr->aptpl; 20421 break; 20422 } 20423 default: 20424 ASSERT(FALSE); 20425 break; 20426 } 20427 20428 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20429 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20430 20431 switch (status) { 20432 case 0: 20433 break; /* Success! */ 20434 case EIO: 20435 switch (ucmd_buf.uscsi_status) { 20436 case STATUS_RESERVATION_CONFLICT: 20437 status = EACCES; 20438 break; 20439 case STATUS_CHECK: 20440 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 20441 (scsi_sense_key((uint8_t *)&sense_buf) == 20442 KEY_ILLEGAL_REQUEST)) { 20443 status = ENOTSUP; 20444 } 20445 break; 20446 default: 20447 break; 20448 } 20449 break; 20450 default: 20451 break; 20452 } 20453 20454 kmem_free(prp, data_len); 20455 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n"); 20456 return (status); 20457 } 20458 20459 20460 /* 20461 * Function: sd_send_scsi_SYNCHRONIZE_CACHE 20462 * 20463 * Description: Issues a scsi SYNCHRONIZE CACHE command to the target 20464 * 20465 * Arguments: un - pointer to the target's soft state struct 20466 * 20467 * Return Code: 0 - success 20468 * errno-type error code 20469 * 20470 * Context: kernel thread context only. 20471 */ 20472 20473 static int 20474 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc) 20475 { 20476 struct sd_uscsi_info *uip; 20477 struct uscsi_cmd *uscmd; 20478 union scsi_cdb *cdb; 20479 struct buf *bp; 20480 int rval = 0; 20481 20482 SD_TRACE(SD_LOG_IO, un, 20483 "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un); 20484 20485 ASSERT(un != NULL); 20486 ASSERT(!mutex_owned(SD_MUTEX(un))); 20487 20488 cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP); 20489 cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE; 20490 20491 /* 20492 * First get some memory for the uscsi_cmd struct and cdb 20493 * and initialize for SYNCHRONIZE_CACHE cmd. 20494 */ 20495 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 20496 uscmd->uscsi_cdblen = CDB_GROUP1; 20497 uscmd->uscsi_cdb = (caddr_t)cdb; 20498 uscmd->uscsi_bufaddr = NULL; 20499 uscmd->uscsi_buflen = 0; 20500 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 20501 uscmd->uscsi_rqlen = SENSE_LENGTH; 20502 uscmd->uscsi_rqresid = SENSE_LENGTH; 20503 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT; 20504 uscmd->uscsi_timeout = sd_io_time; 20505 20506 /* 20507 * Allocate an sd_uscsi_info struct and fill it with the info 20508 * needed by sd_initpkt_for_uscsi(). Then put the pointer into 20509 * b_private in the buf for sd_initpkt_for_uscsi(). Note that 20510 * since we allocate the buf here in this function, we do not 20511 * need to preserve the prior contents of b_private. 20512 * The sd_uscsi_info struct is also used by sd_uscsi_strategy() 20513 */ 20514 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP); 20515 uip->ui_flags = SD_PATH_DIRECT; 20516 uip->ui_cmdp = uscmd; 20517 20518 bp = getrbuf(KM_SLEEP); 20519 bp->b_private = uip; 20520 20521 /* 20522 * Setup buffer to carry uscsi request. 20523 */ 20524 bp->b_flags = B_BUSY; 20525 bp->b_bcount = 0; 20526 bp->b_blkno = 0; 20527 20528 if (dkc != NULL) { 20529 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone; 20530 uip->ui_dkc = *dkc; 20531 } 20532 20533 bp->b_edev = SD_GET_DEV(un); 20534 bp->b_dev = cmpdev(bp->b_edev); /* maybe unnecessary? */ 20535 20536 (void) sd_uscsi_strategy(bp); 20537 20538 /* 20539 * If synchronous request, wait for completion 20540 * If async just return and let b_iodone callback 20541 * cleanup. 20542 * NOTE: On return, u_ncmds_in_driver will be decremented, 20543 * but it was also incremented in sd_uscsi_strategy(), so 20544 * we should be ok. 20545 */ 20546 if (dkc == NULL) { 20547 (void) biowait(bp); 20548 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp); 20549 } 20550 20551 return (rval); 20552 } 20553 20554 20555 static int 20556 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp) 20557 { 20558 struct sd_uscsi_info *uip; 20559 struct uscsi_cmd *uscmd; 20560 uint8_t *sense_buf; 20561 struct sd_lun *un; 20562 int status; 20563 20564 uip = (struct sd_uscsi_info *)(bp->b_private); 20565 ASSERT(uip != NULL); 20566 20567 uscmd = uip->ui_cmdp; 20568 ASSERT(uscmd != NULL); 20569 20570 sense_buf = (uint8_t *)uscmd->uscsi_rqbuf; 20571 ASSERT(sense_buf != NULL); 20572 20573 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp)); 20574 ASSERT(un != NULL); 20575 20576 status = geterror(bp); 20577 switch (status) { 20578 case 0: 20579 break; /* Success! */ 20580 case EIO: 20581 switch (uscmd->uscsi_status) { 20582 case STATUS_RESERVATION_CONFLICT: 20583 /* Ignore reservation conflict */ 20584 status = 0; 20585 goto done; 20586 20587 case STATUS_CHECK: 20588 if ((uscmd->uscsi_rqstatus == STATUS_GOOD) && 20589 (scsi_sense_key(sense_buf) == 20590 KEY_ILLEGAL_REQUEST)) { 20591 /* Ignore Illegal Request error */ 20592 mutex_enter(SD_MUTEX(un)); 20593 un->un_f_sync_cache_supported = FALSE; 20594 mutex_exit(SD_MUTEX(un)); 20595 status = ENOTSUP; 20596 goto done; 20597 } 20598 break; 20599 default: 20600 break; 20601 } 20602 /* FALLTHRU */ 20603 default: 20604 /* 20605 * Don't log an error message if this device 20606 * has removable media. 20607 */ 20608 if (!un->un_f_has_removable_media) { 20609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 20610 "SYNCHRONIZE CACHE command failed (%d)\n", status); 20611 } 20612 break; 20613 } 20614 20615 done: 20616 if (uip->ui_dkc.dkc_callback != NULL) { 20617 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status); 20618 } 20619 20620 ASSERT((bp->b_flags & B_REMAPPED) == 0); 20621 freerbuf(bp); 20622 kmem_free(uip, sizeof (struct sd_uscsi_info)); 20623 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH); 20624 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen); 20625 kmem_free(uscmd, sizeof (struct uscsi_cmd)); 20626 20627 return (status); 20628 } 20629 20630 20631 /* 20632 * Function: sd_send_scsi_GET_CONFIGURATION 20633 * 20634 * Description: Issues the get configuration command to the device. 20635 * Called from sd_check_for_writable_cd & sd_get_media_info 20636 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN 20637 * Arguments: un 20638 * ucmdbuf 20639 * rqbuf 20640 * rqbuflen 20641 * bufaddr 20642 * buflen 20643 * 20644 * Return Code: 0 - Success 20645 * errno return code from sd_send_scsi_cmd() 20646 * 20647 * Context: Can sleep. Does not return until command is completed. 20648 * 20649 */ 20650 20651 static int 20652 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf, 20653 uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen) 20654 { 20655 char cdb[CDB_GROUP1]; 20656 int status; 20657 20658 ASSERT(un != NULL); 20659 ASSERT(!mutex_owned(SD_MUTEX(un))); 20660 ASSERT(bufaddr != NULL); 20661 ASSERT(ucmdbuf != NULL); 20662 ASSERT(rqbuf != NULL); 20663 20664 SD_TRACE(SD_LOG_IO, un, 20665 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un); 20666 20667 bzero(cdb, sizeof (cdb)); 20668 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20669 bzero(rqbuf, rqbuflen); 20670 bzero(bufaddr, buflen); 20671 20672 /* 20673 * Set up cdb field for the get configuration command. 20674 */ 20675 cdb[0] = SCMD_GET_CONFIGURATION; 20676 cdb[1] = 0x02; /* Requested Type */ 20677 cdb[8] = SD_PROFILE_HEADER_LEN; 20678 ucmdbuf->uscsi_cdb = cdb; 20679 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20680 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20681 ucmdbuf->uscsi_buflen = buflen; 20682 ucmdbuf->uscsi_timeout = sd_io_time; 20683 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20684 ucmdbuf->uscsi_rqlen = rqbuflen; 20685 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20686 20687 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20688 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20689 20690 switch (status) { 20691 case 0: 20692 break; /* Success! */ 20693 case EIO: 20694 switch (ucmdbuf->uscsi_status) { 20695 case STATUS_RESERVATION_CONFLICT: 20696 status = EACCES; 20697 break; 20698 default: 20699 break; 20700 } 20701 break; 20702 default: 20703 break; 20704 } 20705 20706 if (status == 0) { 20707 SD_DUMP_MEMORY(un, SD_LOG_IO, 20708 "sd_send_scsi_GET_CONFIGURATION: data", 20709 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20710 } 20711 20712 SD_TRACE(SD_LOG_IO, un, 20713 "sd_send_scsi_GET_CONFIGURATION: exit\n"); 20714 20715 return (status); 20716 } 20717 20718 /* 20719 * Function: sd_send_scsi_feature_GET_CONFIGURATION 20720 * 20721 * Description: Issues the get configuration command to the device to 20722 * retrieve a specfic feature. Called from 20723 * sd_check_for_writable_cd & sd_set_mmc_caps. 20724 * Arguments: un 20725 * ucmdbuf 20726 * rqbuf 20727 * rqbuflen 20728 * bufaddr 20729 * buflen 20730 * feature 20731 * 20732 * Return Code: 0 - Success 20733 * errno return code from sd_send_scsi_cmd() 20734 * 20735 * Context: Can sleep. Does not return until command is completed. 20736 * 20737 */ 20738 static int 20739 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un, 20740 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen, 20741 uchar_t *bufaddr, uint_t buflen, char feature) 20742 { 20743 char cdb[CDB_GROUP1]; 20744 int status; 20745 20746 ASSERT(un != NULL); 20747 ASSERT(!mutex_owned(SD_MUTEX(un))); 20748 ASSERT(bufaddr != NULL); 20749 ASSERT(ucmdbuf != NULL); 20750 ASSERT(rqbuf != NULL); 20751 20752 SD_TRACE(SD_LOG_IO, un, 20753 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un); 20754 20755 bzero(cdb, sizeof (cdb)); 20756 bzero(ucmdbuf, sizeof (struct uscsi_cmd)); 20757 bzero(rqbuf, rqbuflen); 20758 bzero(bufaddr, buflen); 20759 20760 /* 20761 * Set up cdb field for the get configuration command. 20762 */ 20763 cdb[0] = SCMD_GET_CONFIGURATION; 20764 cdb[1] = 0x02; /* Requested Type */ 20765 cdb[3] = feature; 20766 cdb[8] = buflen; 20767 ucmdbuf->uscsi_cdb = cdb; 20768 ucmdbuf->uscsi_cdblen = CDB_GROUP1; 20769 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr; 20770 ucmdbuf->uscsi_buflen = buflen; 20771 ucmdbuf->uscsi_timeout = sd_io_time; 20772 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf; 20773 ucmdbuf->uscsi_rqlen = rqbuflen; 20774 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ; 20775 20776 status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE, 20777 UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD); 20778 20779 switch (status) { 20780 case 0: 20781 break; /* Success! */ 20782 case EIO: 20783 switch (ucmdbuf->uscsi_status) { 20784 case STATUS_RESERVATION_CONFLICT: 20785 status = EACCES; 20786 break; 20787 default: 20788 break; 20789 } 20790 break; 20791 default: 20792 break; 20793 } 20794 20795 if (status == 0) { 20796 SD_DUMP_MEMORY(un, SD_LOG_IO, 20797 "sd_send_scsi_feature_GET_CONFIGURATION: data", 20798 (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX); 20799 } 20800 20801 SD_TRACE(SD_LOG_IO, un, 20802 "sd_send_scsi_feature_GET_CONFIGURATION: exit\n"); 20803 20804 return (status); 20805 } 20806 20807 20808 /* 20809 * Function: sd_send_scsi_MODE_SENSE 20810 * 20811 * Description: Utility function for issuing a scsi MODE SENSE command. 20812 * Note: This routine uses a consistent implementation for Group0, 20813 * Group1, and Group2 commands across all platforms. ATAPI devices 20814 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20815 * 20816 * Arguments: un - pointer to the softstate struct for the target. 20817 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20818 * CDB_GROUP[1|2] (10 byte). 20819 * bufaddr - buffer for page data retrieved from the target. 20820 * buflen - size of page to be retrieved. 20821 * page_code - page code of data to be retrieved from the target. 20822 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20823 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20824 * to use the USCSI "direct" chain and bypass the normal 20825 * command waitq. 20826 * 20827 * Return Code: 0 - Success 20828 * errno return code from sd_send_scsi_cmd() 20829 * 20830 * Context: Can sleep. Does not return until command is completed. 20831 */ 20832 20833 static int 20834 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20835 size_t buflen, uchar_t page_code, int path_flag) 20836 { 20837 struct scsi_extended_sense sense_buf; 20838 union scsi_cdb cdb; 20839 struct uscsi_cmd ucmd_buf; 20840 int status; 20841 int headlen; 20842 20843 ASSERT(un != NULL); 20844 ASSERT(!mutex_owned(SD_MUTEX(un))); 20845 ASSERT(bufaddr != NULL); 20846 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20847 (cdbsize == CDB_GROUP2)); 20848 20849 SD_TRACE(SD_LOG_IO, un, 20850 "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un); 20851 20852 bzero(&cdb, sizeof (cdb)); 20853 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20854 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20855 bzero(bufaddr, buflen); 20856 20857 if (cdbsize == CDB_GROUP0) { 20858 cdb.scc_cmd = SCMD_MODE_SENSE; 20859 cdb.cdb_opaque[2] = page_code; 20860 FORMG0COUNT(&cdb, buflen); 20861 headlen = MODE_HEADER_LENGTH; 20862 } else { 20863 cdb.scc_cmd = SCMD_MODE_SENSE_G1; 20864 cdb.cdb_opaque[2] = page_code; 20865 FORMG1COUNT(&cdb, buflen); 20866 headlen = MODE_HEADER_LENGTH_GRP2; 20867 } 20868 20869 ASSERT(headlen <= buflen); 20870 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20871 20872 ucmd_buf.uscsi_cdb = (char *)&cdb; 20873 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20874 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20875 ucmd_buf.uscsi_buflen = buflen; 20876 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20877 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20878 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 20879 ucmd_buf.uscsi_timeout = 60; 20880 20881 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20882 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20883 20884 switch (status) { 20885 case 0: 20886 /* 20887 * sr_check_wp() uses 0x3f page code and check the header of 20888 * mode page to determine if target device is write-protected. 20889 * But some USB devices return 0 bytes for 0x3f page code. For 20890 * this case, make sure that mode page header is returned at 20891 * least. 20892 */ 20893 if (buflen - ucmd_buf.uscsi_resid < headlen) 20894 status = EIO; 20895 break; /* Success! */ 20896 case EIO: 20897 switch (ucmd_buf.uscsi_status) { 20898 case STATUS_RESERVATION_CONFLICT: 20899 status = EACCES; 20900 break; 20901 default: 20902 break; 20903 } 20904 break; 20905 default: 20906 break; 20907 } 20908 20909 if (status == 0) { 20910 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data", 20911 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 20912 } 20913 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n"); 20914 20915 return (status); 20916 } 20917 20918 20919 /* 20920 * Function: sd_send_scsi_MODE_SELECT 20921 * 20922 * Description: Utility function for issuing a scsi MODE SELECT command. 20923 * Note: This routine uses a consistent implementation for Group0, 20924 * Group1, and Group2 commands across all platforms. ATAPI devices 20925 * use Group 1 Read/Write commands and Group 2 Mode Sense/Select 20926 * 20927 * Arguments: un - pointer to the softstate struct for the target. 20928 * cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or 20929 * CDB_GROUP[1|2] (10 byte). 20930 * bufaddr - buffer for page data retrieved from the target. 20931 * buflen - size of page to be retrieved. 20932 * save_page - boolean to determin if SP bit should be set. 20933 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 20934 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 20935 * to use the USCSI "direct" chain and bypass the normal 20936 * command waitq. 20937 * 20938 * Return Code: 0 - Success 20939 * errno return code from sd_send_scsi_cmd() 20940 * 20941 * Context: Can sleep. Does not return until command is completed. 20942 */ 20943 20944 static int 20945 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr, 20946 size_t buflen, uchar_t save_page, int path_flag) 20947 { 20948 struct scsi_extended_sense sense_buf; 20949 union scsi_cdb cdb; 20950 struct uscsi_cmd ucmd_buf; 20951 int status; 20952 20953 ASSERT(un != NULL); 20954 ASSERT(!mutex_owned(SD_MUTEX(un))); 20955 ASSERT(bufaddr != NULL); 20956 ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) || 20957 (cdbsize == CDB_GROUP2)); 20958 20959 SD_TRACE(SD_LOG_IO, un, 20960 "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un); 20961 20962 bzero(&cdb, sizeof (cdb)); 20963 bzero(&ucmd_buf, sizeof (ucmd_buf)); 20964 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 20965 20966 /* Set the PF bit for many third party drives */ 20967 cdb.cdb_opaque[1] = 0x10; 20968 20969 /* Set the savepage(SP) bit if given */ 20970 if (save_page == SD_SAVE_PAGE) { 20971 cdb.cdb_opaque[1] |= 0x01; 20972 } 20973 20974 if (cdbsize == CDB_GROUP0) { 20975 cdb.scc_cmd = SCMD_MODE_SELECT; 20976 FORMG0COUNT(&cdb, buflen); 20977 } else { 20978 cdb.scc_cmd = SCMD_MODE_SELECT_G1; 20979 FORMG1COUNT(&cdb, buflen); 20980 } 20981 20982 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 20983 20984 ucmd_buf.uscsi_cdb = (char *)&cdb; 20985 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 20986 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 20987 ucmd_buf.uscsi_buflen = buflen; 20988 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 20989 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 20990 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT; 20991 ucmd_buf.uscsi_timeout = 60; 20992 20993 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 20994 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 20995 20996 switch (status) { 20997 case 0: 20998 break; /* Success! */ 20999 case EIO: 21000 switch (ucmd_buf.uscsi_status) { 21001 case STATUS_RESERVATION_CONFLICT: 21002 status = EACCES; 21003 break; 21004 default: 21005 break; 21006 } 21007 break; 21008 default: 21009 break; 21010 } 21011 21012 if (status == 0) { 21013 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data", 21014 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21015 } 21016 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n"); 21017 21018 return (status); 21019 } 21020 21021 21022 /* 21023 * Function: sd_send_scsi_RDWR 21024 * 21025 * Description: Issue a scsi READ or WRITE command with the given parameters. 21026 * 21027 * Arguments: un: Pointer to the sd_lun struct for the target. 21028 * cmd: SCMD_READ or SCMD_WRITE 21029 * bufaddr: Address of caller's buffer to receive the RDWR data 21030 * buflen: Length of caller's buffer receive the RDWR data. 21031 * start_block: Block number for the start of the RDWR operation. 21032 * (Assumes target-native block size.) 21033 * residp: Pointer to variable to receive the redisual of the 21034 * RDWR operation (may be NULL of no residual requested). 21035 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and 21036 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY 21037 * to use the USCSI "direct" chain and bypass the normal 21038 * command waitq. 21039 * 21040 * Return Code: 0 - Success 21041 * errno return code from sd_send_scsi_cmd() 21042 * 21043 * Context: Can sleep. Does not return until command is completed. 21044 */ 21045 21046 static int 21047 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr, 21048 size_t buflen, daddr_t start_block, int path_flag) 21049 { 21050 struct scsi_extended_sense sense_buf; 21051 union scsi_cdb cdb; 21052 struct uscsi_cmd ucmd_buf; 21053 uint32_t block_count; 21054 int status; 21055 int cdbsize; 21056 uchar_t flag; 21057 21058 ASSERT(un != NULL); 21059 ASSERT(!mutex_owned(SD_MUTEX(un))); 21060 ASSERT(bufaddr != NULL); 21061 ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE)); 21062 21063 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un); 21064 21065 if (un->un_f_tgt_blocksize_is_valid != TRUE) { 21066 return (EINVAL); 21067 } 21068 21069 mutex_enter(SD_MUTEX(un)); 21070 block_count = SD_BYTES2TGTBLOCKS(un, buflen); 21071 mutex_exit(SD_MUTEX(un)); 21072 21073 flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE; 21074 21075 SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: " 21076 "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n", 21077 bufaddr, buflen, start_block, block_count); 21078 21079 bzero(&cdb, sizeof (cdb)); 21080 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21081 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21082 21083 /* Compute CDB size to use */ 21084 if (start_block > 0xffffffff) 21085 cdbsize = CDB_GROUP4; 21086 else if ((start_block & 0xFFE00000) || 21087 (un->un_f_cfg_is_atapi == TRUE)) 21088 cdbsize = CDB_GROUP1; 21089 else 21090 cdbsize = CDB_GROUP0; 21091 21092 switch (cdbsize) { 21093 case CDB_GROUP0: /* 6-byte CDBs */ 21094 cdb.scc_cmd = cmd; 21095 FORMG0ADDR(&cdb, start_block); 21096 FORMG0COUNT(&cdb, block_count); 21097 break; 21098 case CDB_GROUP1: /* 10-byte CDBs */ 21099 cdb.scc_cmd = cmd | SCMD_GROUP1; 21100 FORMG1ADDR(&cdb, start_block); 21101 FORMG1COUNT(&cdb, block_count); 21102 break; 21103 case CDB_GROUP4: /* 16-byte CDBs */ 21104 cdb.scc_cmd = cmd | SCMD_GROUP4; 21105 FORMG4LONGADDR(&cdb, (uint64_t)start_block); 21106 FORMG4COUNT(&cdb, block_count); 21107 break; 21108 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */ 21109 default: 21110 /* All others reserved */ 21111 return (EINVAL); 21112 } 21113 21114 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */ 21115 SD_FILL_SCSI1_LUN_CDB(un, &cdb); 21116 21117 ucmd_buf.uscsi_cdb = (char *)&cdb; 21118 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize; 21119 ucmd_buf.uscsi_bufaddr = bufaddr; 21120 ucmd_buf.uscsi_buflen = buflen; 21121 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21122 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21123 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT; 21124 ucmd_buf.uscsi_timeout = 60; 21125 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21126 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21127 switch (status) { 21128 case 0: 21129 break; /* Success! */ 21130 case EIO: 21131 switch (ucmd_buf.uscsi_status) { 21132 case STATUS_RESERVATION_CONFLICT: 21133 status = EACCES; 21134 break; 21135 default: 21136 break; 21137 } 21138 break; 21139 default: 21140 break; 21141 } 21142 21143 if (status == 0) { 21144 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data", 21145 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21146 } 21147 21148 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n"); 21149 21150 return (status); 21151 } 21152 21153 21154 /* 21155 * Function: sd_send_scsi_LOG_SENSE 21156 * 21157 * Description: Issue a scsi LOG_SENSE command with the given parameters. 21158 * 21159 * Arguments: un: Pointer to the sd_lun struct for the target. 21160 * 21161 * Return Code: 0 - Success 21162 * errno return code from sd_send_scsi_cmd() 21163 * 21164 * Context: Can sleep. Does not return until command is completed. 21165 */ 21166 21167 static int 21168 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen, 21169 uchar_t page_code, uchar_t page_control, uint16_t param_ptr, 21170 int path_flag) 21171 21172 { 21173 struct scsi_extended_sense sense_buf; 21174 union scsi_cdb cdb; 21175 struct uscsi_cmd ucmd_buf; 21176 int status; 21177 21178 ASSERT(un != NULL); 21179 ASSERT(!mutex_owned(SD_MUTEX(un))); 21180 21181 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un); 21182 21183 bzero(&cdb, sizeof (cdb)); 21184 bzero(&ucmd_buf, sizeof (ucmd_buf)); 21185 bzero(&sense_buf, sizeof (struct scsi_extended_sense)); 21186 21187 cdb.scc_cmd = SCMD_LOG_SENSE_G1; 21188 cdb.cdb_opaque[2] = (page_control << 6) | page_code; 21189 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8); 21190 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF); 21191 FORMG1COUNT(&cdb, buflen); 21192 21193 ucmd_buf.uscsi_cdb = (char *)&cdb; 21194 ucmd_buf.uscsi_cdblen = CDB_GROUP1; 21195 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr; 21196 ucmd_buf.uscsi_buflen = buflen; 21197 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf; 21198 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense); 21199 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT; 21200 ucmd_buf.uscsi_timeout = 60; 21201 21202 status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE, 21203 UIO_SYSSPACE, UIO_SYSSPACE, path_flag); 21204 21205 switch (status) { 21206 case 0: 21207 break; 21208 case EIO: 21209 switch (ucmd_buf.uscsi_status) { 21210 case STATUS_RESERVATION_CONFLICT: 21211 status = EACCES; 21212 break; 21213 case STATUS_CHECK: 21214 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) && 21215 (scsi_sense_key((uint8_t *)&sense_buf) == 21216 KEY_ILLEGAL_REQUEST) && 21217 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) { 21218 /* 21219 * ASC 0x24: INVALID FIELD IN CDB 21220 */ 21221 switch (page_code) { 21222 case START_STOP_CYCLE_PAGE: 21223 /* 21224 * The start stop cycle counter is 21225 * implemented as page 0x31 in earlier 21226 * generation disks. In new generation 21227 * disks the start stop cycle counter is 21228 * implemented as page 0xE. To properly 21229 * handle this case if an attempt for 21230 * log page 0xE is made and fails we 21231 * will try again using page 0x31. 21232 * 21233 * Network storage BU committed to 21234 * maintain the page 0x31 for this 21235 * purpose and will not have any other 21236 * page implemented with page code 0x31 21237 * until all disks transition to the 21238 * standard page. 21239 */ 21240 mutex_enter(SD_MUTEX(un)); 21241 un->un_start_stop_cycle_page = 21242 START_STOP_CYCLE_VU_PAGE; 21243 cdb.cdb_opaque[2] = 21244 (char)(page_control << 6) | 21245 un->un_start_stop_cycle_page; 21246 mutex_exit(SD_MUTEX(un)); 21247 status = sd_send_scsi_cmd( 21248 SD_GET_DEV(un), &ucmd_buf, 21249 UIO_SYSSPACE, UIO_SYSSPACE, 21250 UIO_SYSSPACE, path_flag); 21251 21252 break; 21253 case TEMPERATURE_PAGE: 21254 status = ENOTTY; 21255 break; 21256 default: 21257 break; 21258 } 21259 } 21260 break; 21261 default: 21262 break; 21263 } 21264 break; 21265 default: 21266 break; 21267 } 21268 21269 if (status == 0) { 21270 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data", 21271 (uchar_t *)bufaddr, buflen, SD_LOG_HEX); 21272 } 21273 21274 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n"); 21275 21276 return (status); 21277 } 21278 21279 21280 /* 21281 * Function: sdioctl 21282 * 21283 * Description: Driver's ioctl(9e) entry point function. 21284 * 21285 * Arguments: dev - device number 21286 * cmd - ioctl operation to be performed 21287 * arg - user argument, contains data to be set or reference 21288 * parameter for get 21289 * flag - bit flag, indicating open settings, 32/64 bit type 21290 * cred_p - user credential pointer 21291 * rval_p - calling process return value (OPT) 21292 * 21293 * Return Code: EINVAL 21294 * ENOTTY 21295 * ENXIO 21296 * EIO 21297 * EFAULT 21298 * ENOTSUP 21299 * EPERM 21300 * 21301 * Context: Called from the device switch at normal priority. 21302 */ 21303 21304 static int 21305 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p) 21306 { 21307 struct sd_lun *un = NULL; 21308 int geom_validated = FALSE; 21309 int err = 0; 21310 int i = 0; 21311 cred_t *cr; 21312 21313 /* 21314 * All device accesses go thru sdstrategy where we check on suspend 21315 * status 21316 */ 21317 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 21318 return (ENXIO); 21319 } 21320 21321 ASSERT(!mutex_owned(SD_MUTEX(un))); 21322 21323 /* 21324 * Moved this wait from sd_uscsi_strategy to here for 21325 * reasons of deadlock prevention. Internal driver commands, 21326 * specifically those to change a devices power level, result 21327 * in a call to sd_uscsi_strategy. 21328 */ 21329 mutex_enter(SD_MUTEX(un)); 21330 while ((un->un_state == SD_STATE_SUSPENDED) || 21331 (un->un_state == SD_STATE_PM_CHANGING)) { 21332 cv_wait(&un->un_suspend_cv, SD_MUTEX(un)); 21333 } 21334 /* 21335 * Twiddling the counter here protects commands from now 21336 * through to the top of sd_uscsi_strategy. Without the 21337 * counter inc. a power down, for example, could get in 21338 * after the above check for state is made and before 21339 * execution gets to the top of sd_uscsi_strategy. 21340 * That would cause problems. 21341 */ 21342 un->un_ncmds_in_driver++; 21343 21344 if ((un->un_f_geometry_is_valid == FALSE) && 21345 (flag & (FNDELAY | FNONBLOCK))) { 21346 switch (cmd) { 21347 case CDROMPAUSE: 21348 case CDROMRESUME: 21349 case CDROMPLAYMSF: 21350 case CDROMPLAYTRKIND: 21351 case CDROMREADTOCHDR: 21352 case CDROMREADTOCENTRY: 21353 case CDROMSTOP: 21354 case CDROMSTART: 21355 case CDROMVOLCTRL: 21356 case CDROMSUBCHNL: 21357 case CDROMREADMODE2: 21358 case CDROMREADMODE1: 21359 case CDROMREADOFFSET: 21360 case CDROMSBLKMODE: 21361 case CDROMGBLKMODE: 21362 case CDROMGDRVSPEED: 21363 case CDROMSDRVSPEED: 21364 case CDROMCDDA: 21365 case CDROMCDXA: 21366 case CDROMSUBCODE: 21367 if (!ISCD(un)) { 21368 un->un_ncmds_in_driver--; 21369 ASSERT(un->un_ncmds_in_driver >= 0); 21370 mutex_exit(SD_MUTEX(un)); 21371 return (ENOTTY); 21372 } 21373 break; 21374 case FDEJECT: 21375 case DKIOCEJECT: 21376 case CDROMEJECT: 21377 if (!un->un_f_eject_media_supported) { 21378 un->un_ncmds_in_driver--; 21379 ASSERT(un->un_ncmds_in_driver >= 0); 21380 mutex_exit(SD_MUTEX(un)); 21381 return (ENOTTY); 21382 } 21383 break; 21384 case DKIOCSVTOC: 21385 case DKIOCSETEFI: 21386 case DKIOCSMBOOT: 21387 case DKIOCFLUSHWRITECACHE: 21388 mutex_exit(SD_MUTEX(un)); 21389 err = sd_send_scsi_TEST_UNIT_READY(un, 0); 21390 if (err != 0) { 21391 mutex_enter(SD_MUTEX(un)); 21392 un->un_ncmds_in_driver--; 21393 ASSERT(un->un_ncmds_in_driver >= 0); 21394 mutex_exit(SD_MUTEX(un)); 21395 return (EIO); 21396 } 21397 mutex_enter(SD_MUTEX(un)); 21398 /* FALLTHROUGH */ 21399 case DKIOCREMOVABLE: 21400 case DKIOCHOTPLUGGABLE: 21401 case DKIOCINFO: 21402 case DKIOCGMEDIAINFO: 21403 case MHIOCENFAILFAST: 21404 case MHIOCSTATUS: 21405 case MHIOCTKOWN: 21406 case MHIOCRELEASE: 21407 case MHIOCGRP_INKEYS: 21408 case MHIOCGRP_INRESV: 21409 case MHIOCGRP_REGISTER: 21410 case MHIOCGRP_RESERVE: 21411 case MHIOCGRP_PREEMPTANDABORT: 21412 case MHIOCGRP_REGISTERANDIGNOREKEY: 21413 case CDROMCLOSETRAY: 21414 case USCSICMD: 21415 goto skip_ready_valid; 21416 default: 21417 break; 21418 } 21419 21420 mutex_exit(SD_MUTEX(un)); 21421 err = sd_ready_and_valid(un); 21422 mutex_enter(SD_MUTEX(un)); 21423 if (err == SD_READY_NOT_VALID) { 21424 switch (cmd) { 21425 case DKIOCGAPART: 21426 case DKIOCGGEOM: 21427 case DKIOCSGEOM: 21428 case DKIOCGVTOC: 21429 case DKIOCSVTOC: 21430 case DKIOCSAPART: 21431 case DKIOCG_PHYGEOM: 21432 case DKIOCG_VIRTGEOM: 21433 err = ENOTSUP; 21434 un->un_ncmds_in_driver--; 21435 ASSERT(un->un_ncmds_in_driver >= 0); 21436 mutex_exit(SD_MUTEX(un)); 21437 return (err); 21438 } 21439 } 21440 if (err != SD_READY_VALID) { 21441 switch (cmd) { 21442 case DKIOCSTATE: 21443 case CDROMGDRVSPEED: 21444 case CDROMSDRVSPEED: 21445 case FDEJECT: /* for eject command */ 21446 case DKIOCEJECT: 21447 case CDROMEJECT: 21448 case DKIOCGETEFI: 21449 case DKIOCSGEOM: 21450 case DKIOCREMOVABLE: 21451 case DKIOCHOTPLUGGABLE: 21452 case DKIOCSAPART: 21453 case DKIOCSETEFI: 21454 break; 21455 default: 21456 if (un->un_f_has_removable_media) { 21457 err = ENXIO; 21458 } else { 21459 /* Do not map SD_RESERVED_BY_OTHERS to EIO */ 21460 if (err == SD_RESERVED_BY_OTHERS) { 21461 err = EACCES; 21462 } else { 21463 err = EIO; 21464 } 21465 } 21466 un->un_ncmds_in_driver--; 21467 ASSERT(un->un_ncmds_in_driver >= 0); 21468 mutex_exit(SD_MUTEX(un)); 21469 return (err); 21470 } 21471 } 21472 geom_validated = TRUE; 21473 } 21474 if ((un->un_f_geometry_is_valid == TRUE) && 21475 (un->un_solaris_size > 0)) { 21476 /* 21477 * the "geometry_is_valid" flag could be true if we 21478 * have an fdisk table but no Solaris partition 21479 */ 21480 if (un->un_vtoc.v_sanity != VTOC_SANE) { 21481 /* it is EFI, so return ENOTSUP for these */ 21482 switch (cmd) { 21483 case DKIOCGAPART: 21484 case DKIOCGGEOM: 21485 case DKIOCGVTOC: 21486 case DKIOCSVTOC: 21487 case DKIOCSAPART: 21488 err = ENOTSUP; 21489 un->un_ncmds_in_driver--; 21490 ASSERT(un->un_ncmds_in_driver >= 0); 21491 mutex_exit(SD_MUTEX(un)); 21492 return (err); 21493 } 21494 } 21495 } 21496 21497 skip_ready_valid: 21498 mutex_exit(SD_MUTEX(un)); 21499 21500 switch (cmd) { 21501 case DKIOCINFO: 21502 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n"); 21503 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag); 21504 break; 21505 21506 case DKIOCGMEDIAINFO: 21507 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n"); 21508 err = sd_get_media_info(dev, (caddr_t)arg, flag); 21509 break; 21510 21511 case DKIOCGGEOM: 21512 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n"); 21513 err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag, 21514 geom_validated); 21515 break; 21516 21517 case DKIOCSGEOM: 21518 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n"); 21519 err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag); 21520 break; 21521 21522 case DKIOCGAPART: 21523 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n"); 21524 err = sd_dkio_get_partition(dev, (caddr_t)arg, flag, 21525 geom_validated); 21526 break; 21527 21528 case DKIOCSAPART: 21529 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n"); 21530 err = sd_dkio_set_partition(dev, (caddr_t)arg, flag); 21531 break; 21532 21533 case DKIOCGVTOC: 21534 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n"); 21535 err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag, 21536 geom_validated); 21537 break; 21538 21539 case DKIOCGETEFI: 21540 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n"); 21541 err = sd_dkio_get_efi(dev, (caddr_t)arg, flag); 21542 break; 21543 21544 case DKIOCPARTITION: 21545 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n"); 21546 err = sd_dkio_partition(dev, (caddr_t)arg, flag); 21547 break; 21548 21549 case DKIOCSVTOC: 21550 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n"); 21551 err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag); 21552 break; 21553 21554 case DKIOCSETEFI: 21555 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n"); 21556 err = sd_dkio_set_efi(dev, (caddr_t)arg, flag); 21557 break; 21558 21559 case DKIOCGMBOOT: 21560 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n"); 21561 err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag); 21562 break; 21563 21564 case DKIOCSMBOOT: 21565 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n"); 21566 err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag); 21567 break; 21568 21569 case DKIOCLOCK: 21570 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n"); 21571 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 21572 SD_PATH_STANDARD); 21573 break; 21574 21575 case DKIOCUNLOCK: 21576 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n"); 21577 err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 21578 SD_PATH_STANDARD); 21579 break; 21580 21581 case DKIOCSTATE: { 21582 enum dkio_state state; 21583 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n"); 21584 21585 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) { 21586 err = EFAULT; 21587 } else { 21588 err = sd_check_media(dev, state); 21589 if (err == 0) { 21590 if (ddi_copyout(&un->un_mediastate, (void *)arg, 21591 sizeof (int), flag) != 0) 21592 err = EFAULT; 21593 } 21594 } 21595 break; 21596 } 21597 21598 case DKIOCREMOVABLE: 21599 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n"); 21600 i = un->un_f_has_removable_media ? 1 : 0; 21601 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21602 err = EFAULT; 21603 } else { 21604 err = 0; 21605 } 21606 break; 21607 21608 case DKIOCHOTPLUGGABLE: 21609 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n"); 21610 i = un->un_f_is_hotpluggable ? 1 : 0; 21611 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) { 21612 err = EFAULT; 21613 } else { 21614 err = 0; 21615 } 21616 break; 21617 21618 case DKIOCGTEMPERATURE: 21619 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n"); 21620 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag); 21621 break; 21622 21623 case MHIOCENFAILFAST: 21624 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n"); 21625 if ((err = drv_priv(cred_p)) == 0) { 21626 err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag); 21627 } 21628 break; 21629 21630 case MHIOCTKOWN: 21631 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n"); 21632 if ((err = drv_priv(cred_p)) == 0) { 21633 err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag); 21634 } 21635 break; 21636 21637 case MHIOCRELEASE: 21638 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n"); 21639 if ((err = drv_priv(cred_p)) == 0) { 21640 err = sd_mhdioc_release(dev); 21641 } 21642 break; 21643 21644 case MHIOCSTATUS: 21645 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n"); 21646 if ((err = drv_priv(cred_p)) == 0) { 21647 switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) { 21648 case 0: 21649 err = 0; 21650 break; 21651 case EACCES: 21652 *rval_p = 1; 21653 err = 0; 21654 break; 21655 default: 21656 err = EIO; 21657 break; 21658 } 21659 } 21660 break; 21661 21662 case MHIOCQRESERVE: 21663 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n"); 21664 if ((err = drv_priv(cred_p)) == 0) { 21665 err = sd_reserve_release(dev, SD_RESERVE); 21666 } 21667 break; 21668 21669 case MHIOCREREGISTERDEVID: 21670 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n"); 21671 if (drv_priv(cred_p) == EPERM) { 21672 err = EPERM; 21673 } else if (!un->un_f_devid_supported) { 21674 err = ENOTTY; 21675 } else { 21676 err = sd_mhdioc_register_devid(dev); 21677 } 21678 break; 21679 21680 case MHIOCGRP_INKEYS: 21681 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n"); 21682 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21683 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21684 err = ENOTSUP; 21685 } else { 21686 err = sd_mhdioc_inkeys(dev, (caddr_t)arg, 21687 flag); 21688 } 21689 } 21690 break; 21691 21692 case MHIOCGRP_INRESV: 21693 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n"); 21694 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) { 21695 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21696 err = ENOTSUP; 21697 } else { 21698 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag); 21699 } 21700 } 21701 break; 21702 21703 case MHIOCGRP_REGISTER: 21704 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n"); 21705 if ((err = drv_priv(cred_p)) != EPERM) { 21706 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21707 err = ENOTSUP; 21708 } else if (arg != NULL) { 21709 mhioc_register_t reg; 21710 if (ddi_copyin((void *)arg, ®, 21711 sizeof (mhioc_register_t), flag) != 0) { 21712 err = EFAULT; 21713 } else { 21714 err = 21715 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21716 un, SD_SCSI3_REGISTER, 21717 (uchar_t *)®); 21718 } 21719 } 21720 } 21721 break; 21722 21723 case MHIOCGRP_RESERVE: 21724 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n"); 21725 if ((err = drv_priv(cred_p)) != EPERM) { 21726 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21727 err = ENOTSUP; 21728 } else if (arg != NULL) { 21729 mhioc_resv_desc_t resv_desc; 21730 if (ddi_copyin((void *)arg, &resv_desc, 21731 sizeof (mhioc_resv_desc_t), flag) != 0) { 21732 err = EFAULT; 21733 } else { 21734 err = 21735 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21736 un, SD_SCSI3_RESERVE, 21737 (uchar_t *)&resv_desc); 21738 } 21739 } 21740 } 21741 break; 21742 21743 case MHIOCGRP_PREEMPTANDABORT: 21744 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21745 if ((err = drv_priv(cred_p)) != EPERM) { 21746 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21747 err = ENOTSUP; 21748 } else if (arg != NULL) { 21749 mhioc_preemptandabort_t preempt_abort; 21750 if (ddi_copyin((void *)arg, &preempt_abort, 21751 sizeof (mhioc_preemptandabort_t), 21752 flag) != 0) { 21753 err = EFAULT; 21754 } else { 21755 err = 21756 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21757 un, SD_SCSI3_PREEMPTANDABORT, 21758 (uchar_t *)&preempt_abort); 21759 } 21760 } 21761 } 21762 break; 21763 21764 case MHIOCGRP_REGISTERANDIGNOREKEY: 21765 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n"); 21766 if ((err = drv_priv(cred_p)) != EPERM) { 21767 if (un->un_reservation_type == SD_SCSI2_RESERVATION) { 21768 err = ENOTSUP; 21769 } else if (arg != NULL) { 21770 mhioc_registerandignorekey_t r_and_i; 21771 if (ddi_copyin((void *)arg, (void *)&r_and_i, 21772 sizeof (mhioc_registerandignorekey_t), 21773 flag) != 0) { 21774 err = EFAULT; 21775 } else { 21776 err = 21777 sd_send_scsi_PERSISTENT_RESERVE_OUT( 21778 un, SD_SCSI3_REGISTERANDIGNOREKEY, 21779 (uchar_t *)&r_and_i); 21780 } 21781 } 21782 } 21783 break; 21784 21785 case USCSICMD: 21786 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n"); 21787 cr = ddi_get_cred(); 21788 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 21789 err = EPERM; 21790 } else { 21791 err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag); 21792 } 21793 break; 21794 21795 case CDROMPAUSE: 21796 case CDROMRESUME: 21797 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n"); 21798 if (!ISCD(un)) { 21799 err = ENOTTY; 21800 } else { 21801 err = sr_pause_resume(dev, cmd); 21802 } 21803 break; 21804 21805 case CDROMPLAYMSF: 21806 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n"); 21807 if (!ISCD(un)) { 21808 err = ENOTTY; 21809 } else { 21810 err = sr_play_msf(dev, (caddr_t)arg, flag); 21811 } 21812 break; 21813 21814 case CDROMPLAYTRKIND: 21815 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n"); 21816 #if defined(__i386) || defined(__amd64) 21817 /* 21818 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead 21819 */ 21820 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21821 #else 21822 if (!ISCD(un)) { 21823 #endif 21824 err = ENOTTY; 21825 } else { 21826 err = sr_play_trkind(dev, (caddr_t)arg, flag); 21827 } 21828 break; 21829 21830 case CDROMREADTOCHDR: 21831 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n"); 21832 if (!ISCD(un)) { 21833 err = ENOTTY; 21834 } else { 21835 err = sr_read_tochdr(dev, (caddr_t)arg, flag); 21836 } 21837 break; 21838 21839 case CDROMREADTOCENTRY: 21840 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n"); 21841 if (!ISCD(un)) { 21842 err = ENOTTY; 21843 } else { 21844 err = sr_read_tocentry(dev, (caddr_t)arg, flag); 21845 } 21846 break; 21847 21848 case CDROMSTOP: 21849 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n"); 21850 if (!ISCD(un)) { 21851 err = ENOTTY; 21852 } else { 21853 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP, 21854 SD_PATH_STANDARD); 21855 } 21856 break; 21857 21858 case CDROMSTART: 21859 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n"); 21860 if (!ISCD(un)) { 21861 err = ENOTTY; 21862 } else { 21863 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, 21864 SD_PATH_STANDARD); 21865 } 21866 break; 21867 21868 case CDROMCLOSETRAY: 21869 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n"); 21870 if (!ISCD(un)) { 21871 err = ENOTTY; 21872 } else { 21873 err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE, 21874 SD_PATH_STANDARD); 21875 } 21876 break; 21877 21878 case FDEJECT: /* for eject command */ 21879 case DKIOCEJECT: 21880 case CDROMEJECT: 21881 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n"); 21882 if (!un->un_f_eject_media_supported) { 21883 err = ENOTTY; 21884 } else { 21885 err = sr_eject(dev); 21886 } 21887 break; 21888 21889 case CDROMVOLCTRL: 21890 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n"); 21891 if (!ISCD(un)) { 21892 err = ENOTTY; 21893 } else { 21894 err = sr_volume_ctrl(dev, (caddr_t)arg, flag); 21895 } 21896 break; 21897 21898 case CDROMSUBCHNL: 21899 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n"); 21900 if (!ISCD(un)) { 21901 err = ENOTTY; 21902 } else { 21903 err = sr_read_subchannel(dev, (caddr_t)arg, flag); 21904 } 21905 break; 21906 21907 case CDROMREADMODE2: 21908 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n"); 21909 if (!ISCD(un)) { 21910 err = ENOTTY; 21911 } else if (un->un_f_cfg_is_atapi == TRUE) { 21912 /* 21913 * If the drive supports READ CD, use that instead of 21914 * switching the LBA size via a MODE SELECT 21915 * Block Descriptor 21916 */ 21917 err = sr_read_cd_mode2(dev, (caddr_t)arg, flag); 21918 } else { 21919 err = sr_read_mode2(dev, (caddr_t)arg, flag); 21920 } 21921 break; 21922 21923 case CDROMREADMODE1: 21924 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n"); 21925 if (!ISCD(un)) { 21926 err = ENOTTY; 21927 } else { 21928 err = sr_read_mode1(dev, (caddr_t)arg, flag); 21929 } 21930 break; 21931 21932 case CDROMREADOFFSET: 21933 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n"); 21934 if (!ISCD(un)) { 21935 err = ENOTTY; 21936 } else { 21937 err = sr_read_sony_session_offset(dev, (caddr_t)arg, 21938 flag); 21939 } 21940 break; 21941 21942 case CDROMSBLKMODE: 21943 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n"); 21944 /* 21945 * There is no means of changing block size in case of atapi 21946 * drives, thus return ENOTTY if drive type is atapi 21947 */ 21948 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) { 21949 err = ENOTTY; 21950 } else if (un->un_f_mmc_cap == TRUE) { 21951 21952 /* 21953 * MMC Devices do not support changing the 21954 * logical block size 21955 * 21956 * Note: EINVAL is being returned instead of ENOTTY to 21957 * maintain consistancy with the original mmc 21958 * driver update. 21959 */ 21960 err = EINVAL; 21961 } else { 21962 mutex_enter(SD_MUTEX(un)); 21963 if ((!(un->un_exclopen & (1<<SDPART(dev)))) || 21964 (un->un_ncmds_in_transport > 0)) { 21965 mutex_exit(SD_MUTEX(un)); 21966 err = EINVAL; 21967 } else { 21968 mutex_exit(SD_MUTEX(un)); 21969 err = sr_change_blkmode(dev, cmd, arg, flag); 21970 } 21971 } 21972 break; 21973 21974 case CDROMGBLKMODE: 21975 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n"); 21976 if (!ISCD(un)) { 21977 err = ENOTTY; 21978 } else if ((un->un_f_cfg_is_atapi != FALSE) && 21979 (un->un_f_blockcount_is_valid != FALSE)) { 21980 /* 21981 * Drive is an ATAPI drive so return target block 21982 * size for ATAPI drives since we cannot change the 21983 * blocksize on ATAPI drives. Used primarily to detect 21984 * if an ATAPI cdrom is present. 21985 */ 21986 if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg, 21987 sizeof (int), flag) != 0) { 21988 err = EFAULT; 21989 } else { 21990 err = 0; 21991 } 21992 21993 } else { 21994 /* 21995 * Drive supports changing block sizes via a Mode 21996 * Select. 21997 */ 21998 err = sr_change_blkmode(dev, cmd, arg, flag); 21999 } 22000 break; 22001 22002 case CDROMGDRVSPEED: 22003 case CDROMSDRVSPEED: 22004 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n"); 22005 if (!ISCD(un)) { 22006 err = ENOTTY; 22007 } else if (un->un_f_mmc_cap == TRUE) { 22008 /* 22009 * Note: In the future the driver implementation 22010 * for getting and 22011 * setting cd speed should entail: 22012 * 1) If non-mmc try the Toshiba mode page 22013 * (sr_change_speed) 22014 * 2) If mmc but no support for Real Time Streaming try 22015 * the SET CD SPEED (0xBB) command 22016 * (sr_atapi_change_speed) 22017 * 3) If mmc and support for Real Time Streaming 22018 * try the GET PERFORMANCE and SET STREAMING 22019 * commands (not yet implemented, 4380808) 22020 */ 22021 /* 22022 * As per recent MMC spec, CD-ROM speed is variable 22023 * and changes with LBA. Since there is no such 22024 * things as drive speed now, fail this ioctl. 22025 * 22026 * Note: EINVAL is returned for consistancy of original 22027 * implementation which included support for getting 22028 * the drive speed of mmc devices but not setting 22029 * the drive speed. Thus EINVAL would be returned 22030 * if a set request was made for an mmc device. 22031 * We no longer support get or set speed for 22032 * mmc but need to remain consistant with regard 22033 * to the error code returned. 22034 */ 22035 err = EINVAL; 22036 } else if (un->un_f_cfg_is_atapi == TRUE) { 22037 err = sr_atapi_change_speed(dev, cmd, arg, flag); 22038 } else { 22039 err = sr_change_speed(dev, cmd, arg, flag); 22040 } 22041 break; 22042 22043 case CDROMCDDA: 22044 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n"); 22045 if (!ISCD(un)) { 22046 err = ENOTTY; 22047 } else { 22048 err = sr_read_cdda(dev, (void *)arg, flag); 22049 } 22050 break; 22051 22052 case CDROMCDXA: 22053 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n"); 22054 if (!ISCD(un)) { 22055 err = ENOTTY; 22056 } else { 22057 err = sr_read_cdxa(dev, (caddr_t)arg, flag); 22058 } 22059 break; 22060 22061 case CDROMSUBCODE: 22062 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n"); 22063 if (!ISCD(un)) { 22064 err = ENOTTY; 22065 } else { 22066 err = sr_read_all_subcodes(dev, (caddr_t)arg, flag); 22067 } 22068 break; 22069 22070 case DKIOCPARTINFO: { 22071 /* 22072 * Return parameters describing the selected disk slice. 22073 * Note: this ioctl is for the intel platform only 22074 */ 22075 #if defined(__i386) || defined(__amd64) 22076 int part; 22077 22078 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22079 part = SDPART(dev); 22080 22081 /* don't check un_solaris_size for pN */ 22082 if (part < P0_RAW_DISK && un->un_solaris_size == 0) { 22083 err = EIO; 22084 } else { 22085 struct part_info p; 22086 22087 p.p_start = (daddr_t)un->un_offset[part]; 22088 p.p_length = (int)un->un_map[part].dkl_nblk; 22089 #ifdef _MULTI_DATAMODEL 22090 switch (ddi_model_convert_from(flag & FMODELS)) { 22091 case DDI_MODEL_ILP32: 22092 { 22093 struct part_info32 p32; 22094 22095 p32.p_start = (daddr32_t)p.p_start; 22096 p32.p_length = p.p_length; 22097 if (ddi_copyout(&p32, (void *)arg, 22098 sizeof (p32), flag)) 22099 err = EFAULT; 22100 break; 22101 } 22102 22103 case DDI_MODEL_NONE: 22104 { 22105 if (ddi_copyout(&p, (void *)arg, sizeof (p), 22106 flag)) 22107 err = EFAULT; 22108 break; 22109 } 22110 } 22111 #else /* ! _MULTI_DATAMODEL */ 22112 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag)) 22113 err = EFAULT; 22114 #endif /* _MULTI_DATAMODEL */ 22115 } 22116 #else 22117 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n"); 22118 err = ENOTTY; 22119 #endif 22120 break; 22121 } 22122 22123 case DKIOCG_PHYGEOM: { 22124 /* Return the driver's notion of the media physical geometry */ 22125 #if defined(__i386) || defined(__amd64) 22126 uint64_t capacity; 22127 struct dk_geom disk_geom; 22128 struct dk_geom *dkgp = &disk_geom; 22129 22130 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22131 mutex_enter(SD_MUTEX(un)); 22132 22133 if (un->un_g.dkg_nhead != 0 && 22134 un->un_g.dkg_nsect != 0) { 22135 /* 22136 * We succeeded in getting a geometry, but 22137 * right now it is being reported as just the 22138 * Solaris fdisk partition, just like for 22139 * DKIOCGGEOM. We need to change that to be 22140 * correct for the entire disk now. 22141 */ 22142 bcopy(&un->un_g, dkgp, sizeof (*dkgp)); 22143 dkgp->dkg_acyl = 0; 22144 dkgp->dkg_ncyl = un->un_blockcount / 22145 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22146 } else { 22147 bzero(dkgp, sizeof (struct dk_geom)); 22148 /* 22149 * This disk does not have a Solaris VTOC 22150 * so we must present a physical geometry 22151 * that will remain consistent regardless 22152 * of how the disk is used. This will ensure 22153 * that the geometry does not change regardless 22154 * of the fdisk partition type (ie. EFI, FAT32, 22155 * Solaris, etc). 22156 */ 22157 if (ISCD(un)) { 22158 dkgp->dkg_nhead = un->un_pgeom.g_nhead; 22159 dkgp->dkg_nsect = un->un_pgeom.g_nsect; 22160 dkgp->dkg_ncyl = un->un_pgeom.g_ncyl; 22161 dkgp->dkg_acyl = un->un_pgeom.g_acyl; 22162 } else { 22163 /* 22164 * Invalid un_blockcount can generate invalid 22165 * dk_geom and may result in division by zero 22166 * system failure. Should make sure blockcount 22167 * is valid before using it here. 22168 */ 22169 if (un->un_f_blockcount_is_valid == FALSE) { 22170 mutex_exit(SD_MUTEX(un)); 22171 err = EIO; 22172 22173 break; 22174 } 22175 22176 /* 22177 * Refer to comments related to off-by-1 at the 22178 * header of this file 22179 */ 22180 if (!un->un_f_capacity_adjusted && 22181 !un->un_f_has_removable_media && 22182 !un->un_f_is_hotpluggable && 22183 (un->un_tgt_blocksize == 22184 un->un_sys_blocksize)) 22185 capacity = un->un_blockcount - 1; 22186 else 22187 capacity = un->un_blockcount; 22188 22189 sd_convert_geometry(capacity, dkgp); 22190 dkgp->dkg_acyl = 0; 22191 dkgp->dkg_ncyl = capacity / 22192 (dkgp->dkg_nhead * dkgp->dkg_nsect); 22193 } 22194 } 22195 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22196 22197 if (ddi_copyout(dkgp, (void *)arg, 22198 sizeof (struct dk_geom), flag)) { 22199 mutex_exit(SD_MUTEX(un)); 22200 err = EFAULT; 22201 } else { 22202 mutex_exit(SD_MUTEX(un)); 22203 err = 0; 22204 } 22205 #else 22206 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n"); 22207 err = ENOTTY; 22208 #endif 22209 break; 22210 } 22211 22212 case DKIOCG_VIRTGEOM: { 22213 /* Return the driver's notion of the media's logical geometry */ 22214 #if defined(__i386) || defined(__amd64) 22215 struct dk_geom disk_geom; 22216 struct dk_geom *dkgp = &disk_geom; 22217 22218 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22219 mutex_enter(SD_MUTEX(un)); 22220 /* 22221 * If there is no HBA geometry available, or 22222 * if the HBA returned us something that doesn't 22223 * really fit into an Int 13/function 8 geometry 22224 * result, just fail the ioctl. See PSARC 1998/313. 22225 */ 22226 if (un->un_lgeom.g_nhead == 0 || 22227 un->un_lgeom.g_nsect == 0 || 22228 un->un_lgeom.g_ncyl > 1024) { 22229 mutex_exit(SD_MUTEX(un)); 22230 err = EINVAL; 22231 } else { 22232 dkgp->dkg_ncyl = un->un_lgeom.g_ncyl; 22233 dkgp->dkg_acyl = un->un_lgeom.g_acyl; 22234 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl; 22235 dkgp->dkg_nhead = un->un_lgeom.g_nhead; 22236 dkgp->dkg_nsect = un->un_lgeom.g_nsect; 22237 22238 if (ddi_copyout(dkgp, (void *)arg, 22239 sizeof (struct dk_geom), flag)) { 22240 mutex_exit(SD_MUTEX(un)); 22241 err = EFAULT; 22242 } else { 22243 mutex_exit(SD_MUTEX(un)); 22244 err = 0; 22245 } 22246 } 22247 #else 22248 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n"); 22249 err = ENOTTY; 22250 #endif 22251 break; 22252 } 22253 #ifdef SDDEBUG 22254 /* RESET/ABORTS testing ioctls */ 22255 case DKIOCRESET: { 22256 int reset_level; 22257 22258 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) { 22259 err = EFAULT; 22260 } else { 22261 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: " 22262 "reset_level = 0x%lx\n", reset_level); 22263 if (scsi_reset(SD_ADDRESS(un), reset_level)) { 22264 err = 0; 22265 } else { 22266 err = EIO; 22267 } 22268 } 22269 break; 22270 } 22271 22272 case DKIOCABORT: 22273 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n"); 22274 if (scsi_abort(SD_ADDRESS(un), NULL)) { 22275 err = 0; 22276 } else { 22277 err = EIO; 22278 } 22279 break; 22280 #endif 22281 22282 #ifdef SD_FAULT_INJECTION 22283 /* SDIOC FaultInjection testing ioctls */ 22284 case SDIOCSTART: 22285 case SDIOCSTOP: 22286 case SDIOCINSERTPKT: 22287 case SDIOCINSERTXB: 22288 case SDIOCINSERTUN: 22289 case SDIOCINSERTARQ: 22290 case SDIOCPUSH: 22291 case SDIOCRETRIEVE: 22292 case SDIOCRUN: 22293 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:" 22294 "SDIOC detected cmd:0x%X:\n", cmd); 22295 /* call error generator */ 22296 sd_faultinjection_ioctl(cmd, arg, un); 22297 err = 0; 22298 break; 22299 22300 #endif /* SD_FAULT_INJECTION */ 22301 22302 case DKIOCFLUSHWRITECACHE: 22303 { 22304 struct dk_callback *dkc = (struct dk_callback *)arg; 22305 22306 mutex_enter(SD_MUTEX(un)); 22307 if (!un->un_f_sync_cache_supported || 22308 !un->un_f_write_cache_enabled) { 22309 err = un->un_f_sync_cache_supported ? 22310 0 : ENOTSUP; 22311 mutex_exit(SD_MUTEX(un)); 22312 if ((flag & FKIOCTL) && dkc != NULL && 22313 dkc->dkc_callback != NULL) { 22314 (*dkc->dkc_callback)(dkc->dkc_cookie, 22315 err); 22316 /* 22317 * Did callback and reported error. 22318 * Since we did a callback, ioctl 22319 * should return 0. 22320 */ 22321 err = 0; 22322 } 22323 break; 22324 } 22325 mutex_exit(SD_MUTEX(un)); 22326 22327 if ((flag & FKIOCTL) && dkc != NULL && 22328 dkc->dkc_callback != NULL) { 22329 /* async SYNC CACHE request */ 22330 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc); 22331 } else { 22332 /* synchronous SYNC CACHE request */ 22333 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22334 } 22335 } 22336 break; 22337 22338 case DKIOCGETWCE: { 22339 22340 int wce; 22341 22342 if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) { 22343 break; 22344 } 22345 22346 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) { 22347 err = EFAULT; 22348 } 22349 break; 22350 } 22351 22352 case DKIOCSETWCE: { 22353 22354 int wce, sync_supported; 22355 22356 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) { 22357 err = EFAULT; 22358 break; 22359 } 22360 22361 /* 22362 * Synchronize multiple threads trying to enable 22363 * or disable the cache via the un_f_wcc_cv 22364 * condition variable. 22365 */ 22366 mutex_enter(SD_MUTEX(un)); 22367 22368 /* 22369 * Don't allow the cache to be enabled if the 22370 * config file has it disabled. 22371 */ 22372 if (un->un_f_opt_disable_cache && wce) { 22373 mutex_exit(SD_MUTEX(un)); 22374 err = EINVAL; 22375 break; 22376 } 22377 22378 /* 22379 * Wait for write cache change in progress 22380 * bit to be clear before proceeding. 22381 */ 22382 while (un->un_f_wcc_inprog) 22383 cv_wait(&un->un_wcc_cv, SD_MUTEX(un)); 22384 22385 un->un_f_wcc_inprog = 1; 22386 22387 if (un->un_f_write_cache_enabled && wce == 0) { 22388 /* 22389 * Disable the write cache. Don't clear 22390 * un_f_write_cache_enabled until after 22391 * the mode select and flush are complete. 22392 */ 22393 sync_supported = un->un_f_sync_cache_supported; 22394 mutex_exit(SD_MUTEX(un)); 22395 if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22396 SD_CACHE_DISABLE)) == 0 && sync_supported) { 22397 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL); 22398 } 22399 22400 mutex_enter(SD_MUTEX(un)); 22401 if (err == 0) { 22402 un->un_f_write_cache_enabled = 0; 22403 } 22404 22405 } else if (!un->un_f_write_cache_enabled && wce != 0) { 22406 /* 22407 * Set un_f_write_cache_enabled first, so there is 22408 * no window where the cache is enabled, but the 22409 * bit says it isn't. 22410 */ 22411 un->un_f_write_cache_enabled = 1; 22412 mutex_exit(SD_MUTEX(un)); 22413 22414 err = sd_cache_control(un, SD_CACHE_NOCHANGE, 22415 SD_CACHE_ENABLE); 22416 22417 mutex_enter(SD_MUTEX(un)); 22418 22419 if (err) { 22420 un->un_f_write_cache_enabled = 0; 22421 } 22422 } 22423 22424 un->un_f_wcc_inprog = 0; 22425 cv_broadcast(&un->un_wcc_cv); 22426 mutex_exit(SD_MUTEX(un)); 22427 break; 22428 } 22429 22430 default: 22431 err = ENOTTY; 22432 break; 22433 } 22434 mutex_enter(SD_MUTEX(un)); 22435 un->un_ncmds_in_driver--; 22436 ASSERT(un->un_ncmds_in_driver >= 0); 22437 mutex_exit(SD_MUTEX(un)); 22438 22439 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err); 22440 return (err); 22441 } 22442 22443 22444 /* 22445 * Function: sd_uscsi_ioctl 22446 * 22447 * Description: This routine is the driver entry point for handling USCSI ioctl 22448 * requests (USCSICMD). 22449 * 22450 * Arguments: dev - the device number 22451 * arg - user provided scsi command 22452 * flag - this argument is a pass through to ddi_copyxxx() 22453 * directly from the mode argument of ioctl(). 22454 * 22455 * Return Code: code returned by sd_send_scsi_cmd 22456 * ENXIO 22457 * EFAULT 22458 * EAGAIN 22459 */ 22460 22461 static int 22462 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag) 22463 { 22464 #ifdef _MULTI_DATAMODEL 22465 /* 22466 * For use when a 32 bit app makes a call into a 22467 * 64 bit ioctl 22468 */ 22469 struct uscsi_cmd32 uscsi_cmd_32_for_64; 22470 struct uscsi_cmd32 *ucmd32 = &uscsi_cmd_32_for_64; 22471 model_t model; 22472 #endif /* _MULTI_DATAMODEL */ 22473 struct uscsi_cmd *scmd = NULL; 22474 struct sd_lun *un = NULL; 22475 enum uio_seg uioseg; 22476 char cdb[CDB_GROUP0]; 22477 int rval = 0; 22478 22479 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22480 return (ENXIO); 22481 } 22482 22483 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un); 22484 22485 scmd = (struct uscsi_cmd *) 22486 kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 22487 22488 #ifdef _MULTI_DATAMODEL 22489 switch (model = ddi_model_convert_from(flag & FMODELS)) { 22490 case DDI_MODEL_ILP32: 22491 { 22492 if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) { 22493 rval = EFAULT; 22494 goto done; 22495 } 22496 /* 22497 * Convert the ILP32 uscsi data from the 22498 * application to LP64 for internal use. 22499 */ 22500 uscsi_cmd32touscsi_cmd(ucmd32, scmd); 22501 break; 22502 } 22503 case DDI_MODEL_NONE: 22504 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22505 rval = EFAULT; 22506 goto done; 22507 } 22508 break; 22509 } 22510 #else /* ! _MULTI_DATAMODEL */ 22511 if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) { 22512 rval = EFAULT; 22513 goto done; 22514 } 22515 #endif /* _MULTI_DATAMODEL */ 22516 22517 scmd->uscsi_flags &= ~USCSI_NOINTR; 22518 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 22519 if (un->un_f_format_in_progress == TRUE) { 22520 rval = EAGAIN; 22521 goto done; 22522 } 22523 22524 /* 22525 * Gotta do the ddi_copyin() here on the uscsi_cdb so that 22526 * we will have a valid cdb[0] to test. 22527 */ 22528 if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) && 22529 (cdb[0] == SCMD_FORMAT)) { 22530 SD_TRACE(SD_LOG_IOCTL, un, 22531 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22532 mutex_enter(SD_MUTEX(un)); 22533 un->un_f_format_in_progress = TRUE; 22534 mutex_exit(SD_MUTEX(un)); 22535 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22536 SD_PATH_STANDARD); 22537 mutex_enter(SD_MUTEX(un)); 22538 un->un_f_format_in_progress = FALSE; 22539 mutex_exit(SD_MUTEX(un)); 22540 } else { 22541 SD_TRACE(SD_LOG_IOCTL, un, 22542 "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]); 22543 /* 22544 * It's OK to fall into here even if the ddi_copyin() 22545 * on the uscsi_cdb above fails, because sd_send_scsi_cmd() 22546 * does this same copyin and will return the EFAULT 22547 * if it fails. 22548 */ 22549 rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg, 22550 SD_PATH_STANDARD); 22551 } 22552 #ifdef _MULTI_DATAMODEL 22553 switch (model) { 22554 case DDI_MODEL_ILP32: 22555 /* 22556 * Convert back to ILP32 before copyout to the 22557 * application 22558 */ 22559 uscsi_cmdtouscsi_cmd32(scmd, ucmd32); 22560 if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) { 22561 if (rval != 0) { 22562 rval = EFAULT; 22563 } 22564 } 22565 break; 22566 case DDI_MODEL_NONE: 22567 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22568 if (rval != 0) { 22569 rval = EFAULT; 22570 } 22571 } 22572 break; 22573 } 22574 #else /* ! _MULTI_DATAMODE */ 22575 if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) { 22576 if (rval != 0) { 22577 rval = EFAULT; 22578 } 22579 } 22580 #endif /* _MULTI_DATAMODE */ 22581 done: 22582 kmem_free(scmd, sizeof (struct uscsi_cmd)); 22583 22584 SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un); 22585 22586 return (rval); 22587 } 22588 22589 22590 /* 22591 * Function: sd_dkio_ctrl_info 22592 * 22593 * Description: This routine is the driver entry point for handling controller 22594 * information ioctl requests (DKIOCINFO). 22595 * 22596 * Arguments: dev - the device number 22597 * arg - pointer to user provided dk_cinfo structure 22598 * specifying the controller type and attributes. 22599 * flag - this argument is a pass through to ddi_copyxxx() 22600 * directly from the mode argument of ioctl(). 22601 * 22602 * Return Code: 0 22603 * EFAULT 22604 * ENXIO 22605 */ 22606 22607 static int 22608 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag) 22609 { 22610 struct sd_lun *un = NULL; 22611 struct dk_cinfo *info; 22612 dev_info_t *pdip; 22613 int lun, tgt; 22614 22615 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22616 return (ENXIO); 22617 } 22618 22619 info = (struct dk_cinfo *) 22620 kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP); 22621 22622 switch (un->un_ctype) { 22623 case CTYPE_CDROM: 22624 info->dki_ctype = DKC_CDROM; 22625 break; 22626 default: 22627 info->dki_ctype = DKC_SCSI_CCS; 22628 break; 22629 } 22630 pdip = ddi_get_parent(SD_DEVINFO(un)); 22631 info->dki_cnum = ddi_get_instance(pdip); 22632 if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) { 22633 (void) strcpy(info->dki_cname, ddi_get_name(pdip)); 22634 } else { 22635 (void) strncpy(info->dki_cname, ddi_node_name(pdip), 22636 DK_DEVLEN - 1); 22637 } 22638 22639 lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22640 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); 22641 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un), 22642 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0); 22643 22644 /* Unit Information */ 22645 info->dki_unit = ddi_get_instance(SD_DEVINFO(un)); 22646 info->dki_slave = ((tgt << 3) | lun); 22647 (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)), 22648 DK_DEVLEN - 1); 22649 info->dki_flags = DKI_FMTVOL; 22650 info->dki_partition = SDPART(dev); 22651 22652 /* Max Transfer size of this device in blocks */ 22653 info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize; 22654 info->dki_addr = 0; 22655 info->dki_space = 0; 22656 info->dki_prio = 0; 22657 info->dki_vec = 0; 22658 22659 if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) { 22660 kmem_free(info, sizeof (struct dk_cinfo)); 22661 return (EFAULT); 22662 } else { 22663 kmem_free(info, sizeof (struct dk_cinfo)); 22664 return (0); 22665 } 22666 } 22667 22668 22669 /* 22670 * Function: sd_get_media_info 22671 * 22672 * Description: This routine is the driver entry point for handling ioctl 22673 * requests for the media type or command set profile used by the 22674 * drive to operate on the media (DKIOCGMEDIAINFO). 22675 * 22676 * Arguments: dev - the device number 22677 * arg - pointer to user provided dk_minfo structure 22678 * specifying the media type, logical block size and 22679 * drive capacity. 22680 * flag - this argument is a pass through to ddi_copyxxx() 22681 * directly from the mode argument of ioctl(). 22682 * 22683 * Return Code: 0 22684 * EACCESS 22685 * EFAULT 22686 * ENXIO 22687 * EIO 22688 */ 22689 22690 static int 22691 sd_get_media_info(dev_t dev, caddr_t arg, int flag) 22692 { 22693 struct sd_lun *un = NULL; 22694 struct uscsi_cmd com; 22695 struct scsi_inquiry *sinq; 22696 struct dk_minfo media_info; 22697 u_longlong_t media_capacity; 22698 uint64_t capacity; 22699 uint_t lbasize; 22700 uchar_t *out_data; 22701 uchar_t *rqbuf; 22702 int rval = 0; 22703 int rtn; 22704 22705 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 22706 (un->un_state == SD_STATE_OFFLINE)) { 22707 return (ENXIO); 22708 } 22709 22710 SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n"); 22711 22712 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP); 22713 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP); 22714 22715 /* Issue a TUR to determine if the drive is ready with media present */ 22716 rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA); 22717 if (rval == ENXIO) { 22718 goto done; 22719 } 22720 22721 /* Now get configuration data */ 22722 if (ISCD(un)) { 22723 media_info.dki_media_type = DK_CDROM; 22724 22725 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */ 22726 if (un->un_f_mmc_cap == TRUE) { 22727 rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, 22728 SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN); 22729 22730 if (rtn) { 22731 /* 22732 * Failed for other than an illegal request 22733 * or command not supported 22734 */ 22735 if ((com.uscsi_status == STATUS_CHECK) && 22736 (com.uscsi_rqstatus == STATUS_GOOD)) { 22737 if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) || 22738 (rqbuf[12] != 0x20)) { 22739 rval = EIO; 22740 goto done; 22741 } 22742 } 22743 } else { 22744 /* 22745 * The GET CONFIGURATION command succeeded 22746 * so set the media type according to the 22747 * returned data 22748 */ 22749 media_info.dki_media_type = out_data[6]; 22750 media_info.dki_media_type <<= 8; 22751 media_info.dki_media_type |= out_data[7]; 22752 } 22753 } 22754 } else { 22755 /* 22756 * The profile list is not available, so we attempt to identify 22757 * the media type based on the inquiry data 22758 */ 22759 sinq = un->un_sd->sd_inq; 22760 if (sinq->inq_qual == 0) { 22761 /* This is a direct access device */ 22762 media_info.dki_media_type = DK_FIXED_DISK; 22763 22764 if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) || 22765 (bcmp(sinq->inq_vid, "iomega", 6) == 0)) { 22766 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) { 22767 media_info.dki_media_type = DK_ZIP; 22768 } else if ( 22769 (bcmp(sinq->inq_pid, "jaz", 3) == 0)) { 22770 media_info.dki_media_type = DK_JAZ; 22771 } 22772 } 22773 } else { 22774 /* Not a CD or direct access so return unknown media */ 22775 media_info.dki_media_type = DK_UNKNOWN; 22776 } 22777 } 22778 22779 /* Now read the capacity so we can provide the lbasize and capacity */ 22780 switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize, 22781 SD_PATH_DIRECT)) { 22782 case 0: 22783 break; 22784 case EACCES: 22785 rval = EACCES; 22786 goto done; 22787 default: 22788 rval = EIO; 22789 goto done; 22790 } 22791 22792 media_info.dki_lbsize = lbasize; 22793 media_capacity = capacity; 22794 22795 /* 22796 * sd_send_scsi_READ_CAPACITY() reports capacity in 22797 * un->un_sys_blocksize chunks. So we need to convert it into 22798 * cap.lbasize chunks. 22799 */ 22800 media_capacity *= un->un_sys_blocksize; 22801 media_capacity /= lbasize; 22802 media_info.dki_capacity = media_capacity; 22803 22804 if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) { 22805 rval = EFAULT; 22806 /* Put goto. Anybody might add some code below in future */ 22807 goto done; 22808 } 22809 done: 22810 kmem_free(out_data, SD_PROFILE_HEADER_LEN); 22811 kmem_free(rqbuf, SENSE_LENGTH); 22812 return (rval); 22813 } 22814 22815 22816 /* 22817 * Function: sd_dkio_get_geometry 22818 * 22819 * Description: This routine is the driver entry point for handling user 22820 * requests to get the device geometry (DKIOCGGEOM). 22821 * 22822 * Arguments: dev - the device number 22823 * arg - pointer to user provided dk_geom structure specifying 22824 * the controller's notion of the current geometry. 22825 * flag - this argument is a pass through to ddi_copyxxx() 22826 * directly from the mode argument of ioctl(). 22827 * geom_validated - flag indicating if the device geometry has been 22828 * previously validated in the sdioctl routine. 22829 * 22830 * Return Code: 0 22831 * EFAULT 22832 * ENXIO 22833 * EIO 22834 */ 22835 22836 static int 22837 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated) 22838 { 22839 struct sd_lun *un = NULL; 22840 struct dk_geom *tmp_geom = NULL; 22841 int rval = 0; 22842 22843 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22844 return (ENXIO); 22845 } 22846 22847 if (geom_validated == FALSE) { 22848 /* 22849 * sd_validate_geometry does not spin a disk up 22850 * if it was spun down. We need to make sure it 22851 * is ready. 22852 */ 22853 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 22854 return (rval); 22855 } 22856 mutex_enter(SD_MUTEX(un)); 22857 rval = sd_validate_geometry(un, SD_PATH_DIRECT); 22858 mutex_exit(SD_MUTEX(un)); 22859 } 22860 if (rval) 22861 return (rval); 22862 22863 /* 22864 * It is possible that un_solaris_size is 0(uninitialized) 22865 * after sd_unit_attach. Reservation conflict may cause the 22866 * above situation. Thus, the zero check of un_solaris_size 22867 * should occur after the sd_validate_geometry() call. 22868 */ 22869 #if defined(__i386) || defined(__amd64) 22870 if (un->un_solaris_size == 0) { 22871 return (EIO); 22872 } 22873 #endif 22874 22875 /* 22876 * Make a local copy of the soft state geometry to avoid some potential 22877 * race conditions associated with holding the mutex and updating the 22878 * write_reinstruct value 22879 */ 22880 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22881 mutex_enter(SD_MUTEX(un)); 22882 bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom)); 22883 mutex_exit(SD_MUTEX(un)); 22884 22885 if (tmp_geom->dkg_write_reinstruct == 0) { 22886 tmp_geom->dkg_write_reinstruct = 22887 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm * 22888 sd_rot_delay) / (int)60000); 22889 } 22890 22891 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom), 22892 flag); 22893 if (rval != 0) { 22894 rval = EFAULT; 22895 } 22896 22897 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22898 return (rval); 22899 22900 } 22901 22902 22903 /* 22904 * Function: sd_dkio_set_geometry 22905 * 22906 * Description: This routine is the driver entry point for handling user 22907 * requests to set the device geometry (DKIOCSGEOM). The actual 22908 * device geometry is not updated, just the driver "notion" of it. 22909 * 22910 * Arguments: dev - the device number 22911 * arg - pointer to user provided dk_geom structure used to set 22912 * the controller's notion of the current geometry. 22913 * flag - this argument is a pass through to ddi_copyxxx() 22914 * directly from the mode argument of ioctl(). 22915 * 22916 * Return Code: 0 22917 * EFAULT 22918 * ENXIO 22919 * EIO 22920 */ 22921 22922 static int 22923 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag) 22924 { 22925 struct sd_lun *un = NULL; 22926 struct dk_geom *tmp_geom; 22927 struct dk_map *lp; 22928 int rval = 0; 22929 int i; 22930 22931 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 22932 return (ENXIO); 22933 } 22934 22935 /* 22936 * Make sure there is no reservation conflict on the lun. 22937 */ 22938 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 22939 return (EACCES); 22940 } 22941 22942 #if defined(__i386) || defined(__amd64) 22943 if (un->un_solaris_size == 0) { 22944 return (EIO); 22945 } 22946 #endif 22947 22948 /* 22949 * We need to copy the user specified geometry into local 22950 * storage and then update the softstate. We don't want to hold 22951 * the mutex and copyin directly from the user to the soft state 22952 */ 22953 tmp_geom = (struct dk_geom *) 22954 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP); 22955 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag); 22956 if (rval != 0) { 22957 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22958 return (EFAULT); 22959 } 22960 22961 mutex_enter(SD_MUTEX(un)); 22962 bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom)); 22963 for (i = 0; i < NDKMAP; i++) { 22964 lp = &un->un_map[i]; 22965 un->un_offset[i] = 22966 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 22967 #if defined(__i386) || defined(__amd64) 22968 un->un_offset[i] += un->un_solaris_offset; 22969 #endif 22970 } 22971 un->un_f_geometry_is_valid = FALSE; 22972 mutex_exit(SD_MUTEX(un)); 22973 kmem_free(tmp_geom, sizeof (struct dk_geom)); 22974 22975 return (rval); 22976 } 22977 22978 22979 /* 22980 * Function: sd_dkio_get_partition 22981 * 22982 * Description: This routine is the driver entry point for handling user 22983 * requests to get the partition table (DKIOCGAPART). 22984 * 22985 * Arguments: dev - the device number 22986 * arg - pointer to user provided dk_allmap structure specifying 22987 * the controller's notion of the current partition table. 22988 * flag - this argument is a pass through to ddi_copyxxx() 22989 * directly from the mode argument of ioctl(). 22990 * geom_validated - flag indicating if the device geometry has been 22991 * previously validated in the sdioctl routine. 22992 * 22993 * Return Code: 0 22994 * EFAULT 22995 * ENXIO 22996 * EIO 22997 */ 22998 22999 static int 23000 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated) 23001 { 23002 struct sd_lun *un = NULL; 23003 int rval = 0; 23004 int size; 23005 23006 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23007 return (ENXIO); 23008 } 23009 23010 /* 23011 * Make sure the geometry is valid before getting the partition 23012 * information. 23013 */ 23014 mutex_enter(SD_MUTEX(un)); 23015 if (geom_validated == FALSE) { 23016 /* 23017 * sd_validate_geometry does not spin a disk up 23018 * if it was spun down. We need to make sure it 23019 * is ready before validating the geometry. 23020 */ 23021 mutex_exit(SD_MUTEX(un)); 23022 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23023 return (rval); 23024 } 23025 mutex_enter(SD_MUTEX(un)); 23026 23027 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23028 mutex_exit(SD_MUTEX(un)); 23029 return (rval); 23030 } 23031 } 23032 mutex_exit(SD_MUTEX(un)); 23033 23034 /* 23035 * It is possible that un_solaris_size is 0(uninitialized) 23036 * after sd_unit_attach. Reservation conflict may cause the 23037 * above situation. Thus, the zero check of un_solaris_size 23038 * should occur after the sd_validate_geometry() call. 23039 */ 23040 #if defined(__i386) || defined(__amd64) 23041 if (un->un_solaris_size == 0) { 23042 return (EIO); 23043 } 23044 #endif 23045 23046 #ifdef _MULTI_DATAMODEL 23047 switch (ddi_model_convert_from(flag & FMODELS)) { 23048 case DDI_MODEL_ILP32: { 23049 struct dk_map32 dk_map32[NDKMAP]; 23050 int i; 23051 23052 for (i = 0; i < NDKMAP; i++) { 23053 dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno; 23054 dk_map32[i].dkl_nblk = un->un_map[i].dkl_nblk; 23055 } 23056 size = NDKMAP * sizeof (struct dk_map32); 23057 rval = ddi_copyout(dk_map32, (void *)arg, size, flag); 23058 if (rval != 0) { 23059 rval = EFAULT; 23060 } 23061 break; 23062 } 23063 case DDI_MODEL_NONE: 23064 size = NDKMAP * sizeof (struct dk_map); 23065 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23066 if (rval != 0) { 23067 rval = EFAULT; 23068 } 23069 break; 23070 } 23071 #else /* ! _MULTI_DATAMODEL */ 23072 size = NDKMAP * sizeof (struct dk_map); 23073 rval = ddi_copyout(un->un_map, (void *)arg, size, flag); 23074 if (rval != 0) { 23075 rval = EFAULT; 23076 } 23077 #endif /* _MULTI_DATAMODEL */ 23078 return (rval); 23079 } 23080 23081 23082 /* 23083 * Function: sd_dkio_set_partition 23084 * 23085 * Description: This routine is the driver entry point for handling user 23086 * requests to set the partition table (DKIOCSAPART). The actual 23087 * device partition is not updated. 23088 * 23089 * Arguments: dev - the device number 23090 * arg - pointer to user provided dk_allmap structure used to set 23091 * the controller's notion of the partition table. 23092 * flag - this argument is a pass through to ddi_copyxxx() 23093 * directly from the mode argument of ioctl(). 23094 * 23095 * Return Code: 0 23096 * EINVAL 23097 * EFAULT 23098 * ENXIO 23099 * EIO 23100 */ 23101 23102 static int 23103 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag) 23104 { 23105 struct sd_lun *un = NULL; 23106 struct dk_map dk_map[NDKMAP]; 23107 struct dk_map *lp; 23108 int rval = 0; 23109 int size; 23110 int i; 23111 #if defined(_SUNOS_VTOC_16) 23112 struct dkl_partition *vp; 23113 #endif 23114 23115 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23116 return (ENXIO); 23117 } 23118 23119 /* 23120 * Set the map for all logical partitions. We lock 23121 * the priority just to make sure an interrupt doesn't 23122 * come in while the map is half updated. 23123 */ 23124 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size)) 23125 mutex_enter(SD_MUTEX(un)); 23126 if (un->un_blockcount > DK_MAX_BLOCKS) { 23127 mutex_exit(SD_MUTEX(un)); 23128 return (ENOTSUP); 23129 } 23130 mutex_exit(SD_MUTEX(un)); 23131 23132 /* 23133 * Make sure there is no reservation conflict on the lun. 23134 */ 23135 if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) { 23136 return (EACCES); 23137 } 23138 23139 #if defined(__i386) || defined(__amd64) 23140 if (un->un_solaris_size == 0) { 23141 return (EIO); 23142 } 23143 #endif 23144 23145 #ifdef _MULTI_DATAMODEL 23146 switch (ddi_model_convert_from(flag & FMODELS)) { 23147 case DDI_MODEL_ILP32: { 23148 struct dk_map32 dk_map32[NDKMAP]; 23149 23150 size = NDKMAP * sizeof (struct dk_map32); 23151 rval = ddi_copyin((void *)arg, dk_map32, size, flag); 23152 if (rval != 0) { 23153 return (EFAULT); 23154 } 23155 for (i = 0; i < NDKMAP; i++) { 23156 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno; 23157 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk; 23158 } 23159 break; 23160 } 23161 case DDI_MODEL_NONE: 23162 size = NDKMAP * sizeof (struct dk_map); 23163 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23164 if (rval != 0) { 23165 return (EFAULT); 23166 } 23167 break; 23168 } 23169 #else /* ! _MULTI_DATAMODEL */ 23170 size = NDKMAP * sizeof (struct dk_map); 23171 rval = ddi_copyin((void *)arg, dk_map, size, flag); 23172 if (rval != 0) { 23173 return (EFAULT); 23174 } 23175 #endif /* _MULTI_DATAMODEL */ 23176 23177 mutex_enter(SD_MUTEX(un)); 23178 /* Note: The size used in this bcopy is set based upon the data model */ 23179 bcopy(dk_map, un->un_map, size); 23180 #if defined(_SUNOS_VTOC_16) 23181 vp = (struct dkl_partition *)&(un->un_vtoc); 23182 #endif /* defined(_SUNOS_VTOC_16) */ 23183 for (i = 0; i < NDKMAP; i++) { 23184 lp = &un->un_map[i]; 23185 un->un_offset[i] = 23186 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 23187 #if defined(_SUNOS_VTOC_16) 23188 vp->p_start = un->un_offset[i]; 23189 vp->p_size = lp->dkl_nblk; 23190 vp++; 23191 #endif /* defined(_SUNOS_VTOC_16) */ 23192 #if defined(__i386) || defined(__amd64) 23193 un->un_offset[i] += un->un_solaris_offset; 23194 #endif 23195 } 23196 mutex_exit(SD_MUTEX(un)); 23197 return (rval); 23198 } 23199 23200 23201 /* 23202 * Function: sd_dkio_get_vtoc 23203 * 23204 * Description: This routine is the driver entry point for handling user 23205 * requests to get the current volume table of contents 23206 * (DKIOCGVTOC). 23207 * 23208 * Arguments: dev - the device number 23209 * arg - pointer to user provided vtoc structure specifying 23210 * the current vtoc. 23211 * flag - this argument is a pass through to ddi_copyxxx() 23212 * directly from the mode argument of ioctl(). 23213 * geom_validated - flag indicating if the device geometry has been 23214 * previously validated in the sdioctl routine. 23215 * 23216 * Return Code: 0 23217 * EFAULT 23218 * ENXIO 23219 * EIO 23220 */ 23221 23222 static int 23223 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated) 23224 { 23225 struct sd_lun *un = NULL; 23226 #if defined(_SUNOS_VTOC_8) 23227 struct vtoc user_vtoc; 23228 #endif /* defined(_SUNOS_VTOC_8) */ 23229 int rval = 0; 23230 23231 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23232 return (ENXIO); 23233 } 23234 23235 mutex_enter(SD_MUTEX(un)); 23236 if (geom_validated == FALSE) { 23237 /* 23238 * sd_validate_geometry does not spin a disk up 23239 * if it was spun down. We need to make sure it 23240 * is ready. 23241 */ 23242 mutex_exit(SD_MUTEX(un)); 23243 if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) { 23244 return (rval); 23245 } 23246 mutex_enter(SD_MUTEX(un)); 23247 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) { 23248 mutex_exit(SD_MUTEX(un)); 23249 return (rval); 23250 } 23251 } 23252 23253 #if defined(_SUNOS_VTOC_8) 23254 sd_build_user_vtoc(un, &user_vtoc); 23255 mutex_exit(SD_MUTEX(un)); 23256 23257 #ifdef _MULTI_DATAMODEL 23258 switch (ddi_model_convert_from(flag & FMODELS)) { 23259 case DDI_MODEL_ILP32: { 23260 struct vtoc32 user_vtoc32; 23261 23262 vtoctovtoc32(user_vtoc, user_vtoc32); 23263 if (ddi_copyout(&user_vtoc32, (void *)arg, 23264 sizeof (struct vtoc32), flag)) { 23265 return (EFAULT); 23266 } 23267 break; 23268 } 23269 23270 case DDI_MODEL_NONE: 23271 if (ddi_copyout(&user_vtoc, (void *)arg, 23272 sizeof (struct vtoc), flag)) { 23273 return (EFAULT); 23274 } 23275 break; 23276 } 23277 #else /* ! _MULTI_DATAMODEL */ 23278 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) { 23279 return (EFAULT); 23280 } 23281 #endif /* _MULTI_DATAMODEL */ 23282 23283 #elif defined(_SUNOS_VTOC_16) 23284 mutex_exit(SD_MUTEX(un)); 23285 23286 #ifdef _MULTI_DATAMODEL 23287 /* 23288 * The un_vtoc structure is a "struct dk_vtoc" which is always 23289 * 32-bit to maintain compatibility with existing on-disk 23290 * structures. Thus, we need to convert the structure when copying 23291 * it out to a datamodel-dependent "struct vtoc" in a 64-bit 23292 * program. If the target is a 32-bit program, then no conversion 23293 * is necessary. 23294 */ 23295 /* LINTED: logical expression always true: op "||" */ 23296 ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32)); 23297 switch (ddi_model_convert_from(flag & FMODELS)) { 23298 case DDI_MODEL_ILP32: 23299 if (ddi_copyout(&(un->un_vtoc), (void *)arg, 23300 sizeof (un->un_vtoc), flag)) { 23301 return (EFAULT); 23302 } 23303 break; 23304 23305 case DDI_MODEL_NONE: { 23306 struct vtoc user_vtoc; 23307 23308 vtoc32tovtoc(un->un_vtoc, user_vtoc); 23309 if (ddi_copyout(&user_vtoc, (void *)arg, 23310 sizeof (struct vtoc), flag)) { 23311 return (EFAULT); 23312 } 23313 break; 23314 } 23315 } 23316 #else /* ! _MULTI_DATAMODEL */ 23317 if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc), 23318 flag)) { 23319 return (EFAULT); 23320 } 23321 #endif /* _MULTI_DATAMODEL */ 23322 #else 23323 #error "No VTOC format defined." 23324 #endif 23325 23326 return (rval); 23327 } 23328 23329 static int 23330 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag) 23331 { 23332 struct sd_lun *un = NULL; 23333 dk_efi_t user_efi; 23334 int rval = 0; 23335 void *buffer; 23336 23337 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 23338 return (ENXIO); 23339 23340 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 23341 return (EFAULT); 23342 23343 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 23344 23345 if ((user_efi.dki_length % un->un_tgt_blocksize) || 23346 (user_efi.dki_length > un->un_max_xfer_size)) 23347 return (EINVAL); 23348 23349 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 23350 rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length, 23351 user_efi.dki_lba, SD_PATH_DIRECT); 23352 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data, 23353 user_efi.dki_length, flag) != 0) 23354 rval = EFAULT; 23355 23356 kmem_free(buffer, user_efi.dki_length); 23357 return (rval); 23358 } 23359 23360 #if defined(_SUNOS_VTOC_8) 23361 /* 23362 * Function: sd_build_user_vtoc 23363 * 23364 * Description: This routine populates a pass by reference variable with the 23365 * current volume table of contents. 23366 * 23367 * Arguments: un - driver soft state (unit) structure 23368 * user_vtoc - pointer to vtoc structure to be populated 23369 */ 23370 23371 static void 23372 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23373 { 23374 struct dk_map2 *lpart; 23375 struct dk_map *lmap; 23376 struct partition *vpart; 23377 int nblks; 23378 int i; 23379 23380 ASSERT(mutex_owned(SD_MUTEX(un))); 23381 23382 /* 23383 * Return vtoc structure fields in the provided VTOC area, addressed 23384 * by *vtoc. 23385 */ 23386 bzero(user_vtoc, sizeof (struct vtoc)); 23387 user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0]; 23388 user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1]; 23389 user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2]; 23390 user_vtoc->v_sanity = VTOC_SANE; 23391 user_vtoc->v_version = un->un_vtoc.v_version; 23392 bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL); 23393 user_vtoc->v_sectorsz = un->un_sys_blocksize; 23394 user_vtoc->v_nparts = un->un_vtoc.v_nparts; 23395 bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved, 23396 sizeof (un->un_vtoc.v_reserved)); 23397 /* 23398 * Convert partitioning information. 23399 * 23400 * Note the conversion from starting cylinder number 23401 * to starting sector number. 23402 */ 23403 lmap = un->un_map; 23404 lpart = (struct dk_map2 *)un->un_vtoc.v_part; 23405 vpart = user_vtoc->v_part; 23406 23407 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23408 23409 for (i = 0; i < V_NUMPAR; i++) { 23410 vpart->p_tag = lpart->p_tag; 23411 vpart->p_flag = lpart->p_flag; 23412 vpart->p_start = lmap->dkl_cylno * nblks; 23413 vpart->p_size = lmap->dkl_nblk; 23414 lmap++; 23415 lpart++; 23416 vpart++; 23417 23418 /* (4364927) */ 23419 user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i]; 23420 } 23421 23422 bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII); 23423 } 23424 #endif 23425 23426 static int 23427 sd_dkio_partition(dev_t dev, caddr_t arg, int flag) 23428 { 23429 struct sd_lun *un = NULL; 23430 struct partition64 p64; 23431 int rval = 0; 23432 uint_t nparts; 23433 efi_gpe_t *partitions; 23434 efi_gpt_t *buffer; 23435 diskaddr_t gpe_lba; 23436 23437 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23438 return (ENXIO); 23439 } 23440 23441 if (ddi_copyin((const void *)arg, &p64, 23442 sizeof (struct partition64), flag)) { 23443 return (EFAULT); 23444 } 23445 23446 buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP); 23447 rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE, 23448 1, SD_PATH_DIRECT); 23449 if (rval != 0) 23450 goto done_error; 23451 23452 sd_swap_efi_gpt(buffer); 23453 23454 if ((rval = sd_validate_efi(buffer)) != 0) 23455 goto done_error; 23456 23457 nparts = buffer->efi_gpt_NumberOfPartitionEntries; 23458 gpe_lba = buffer->efi_gpt_PartitionEntryLBA; 23459 if (p64.p_partno > nparts) { 23460 /* couldn't find it */ 23461 rval = ESRCH; 23462 goto done_error; 23463 } 23464 /* 23465 * if we're dealing with a partition that's out of the normal 23466 * 16K block, adjust accordingly 23467 */ 23468 gpe_lba += p64.p_partno / sizeof (efi_gpe_t); 23469 rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE, 23470 gpe_lba, SD_PATH_DIRECT); 23471 if (rval) { 23472 goto done_error; 23473 } 23474 partitions = (efi_gpe_t *)buffer; 23475 23476 sd_swap_efi_gpe(nparts, partitions); 23477 23478 partitions += p64.p_partno; 23479 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type, 23480 sizeof (struct uuid)); 23481 p64.p_start = partitions->efi_gpe_StartingLBA; 23482 p64.p_size = partitions->efi_gpe_EndingLBA - 23483 p64.p_start + 1; 23484 23485 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag)) 23486 rval = EFAULT; 23487 23488 done_error: 23489 kmem_free(buffer, EFI_MIN_ARRAY_SIZE); 23490 return (rval); 23491 } 23492 23493 23494 /* 23495 * Function: sd_dkio_set_vtoc 23496 * 23497 * Description: This routine is the driver entry point for handling user 23498 * requests to set the current volume table of contents 23499 * (DKIOCSVTOC). 23500 * 23501 * Arguments: dev - the device number 23502 * arg - pointer to user provided vtoc structure used to set the 23503 * current vtoc. 23504 * flag - this argument is a pass through to ddi_copyxxx() 23505 * directly from the mode argument of ioctl(). 23506 * 23507 * Return Code: 0 23508 * EFAULT 23509 * ENXIO 23510 * EINVAL 23511 * ENOTSUP 23512 */ 23513 23514 static int 23515 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag) 23516 { 23517 struct sd_lun *un = NULL; 23518 struct vtoc user_vtoc; 23519 int rval = 0; 23520 23521 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 23522 return (ENXIO); 23523 } 23524 23525 #if defined(__i386) || defined(__amd64) 23526 if (un->un_tgt_blocksize != un->un_sys_blocksize) { 23527 return (EINVAL); 23528 } 23529 #endif 23530 23531 #ifdef _MULTI_DATAMODEL 23532 switch (ddi_model_convert_from(flag & FMODELS)) { 23533 case DDI_MODEL_ILP32: { 23534 struct vtoc32 user_vtoc32; 23535 23536 if (ddi_copyin((const void *)arg, &user_vtoc32, 23537 sizeof (struct vtoc32), flag)) { 23538 return (EFAULT); 23539 } 23540 vtoc32tovtoc(user_vtoc32, user_vtoc); 23541 break; 23542 } 23543 23544 case DDI_MODEL_NONE: 23545 if (ddi_copyin((const void *)arg, &user_vtoc, 23546 sizeof (struct vtoc), flag)) { 23547 return (EFAULT); 23548 } 23549 break; 23550 } 23551 #else /* ! _MULTI_DATAMODEL */ 23552 if (ddi_copyin((const void *)arg, &user_vtoc, 23553 sizeof (struct vtoc), flag)) { 23554 return (EFAULT); 23555 } 23556 #endif /* _MULTI_DATAMODEL */ 23557 23558 mutex_enter(SD_MUTEX(un)); 23559 if (un->un_blockcount > DK_MAX_BLOCKS) { 23560 mutex_exit(SD_MUTEX(un)); 23561 return (ENOTSUP); 23562 } 23563 if (un->un_g.dkg_ncyl == 0) { 23564 mutex_exit(SD_MUTEX(un)); 23565 return (EINVAL); 23566 } 23567 23568 mutex_exit(SD_MUTEX(un)); 23569 sd_clear_efi(un); 23570 ddi_remove_minor_node(SD_DEVINFO(un), "wd"); 23571 ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw"); 23572 (void) ddi_create_minor_node(SD_DEVINFO(un), "h", 23573 S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23574 un->un_node_type, NULL); 23575 (void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw", 23576 S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 23577 un->un_node_type, NULL); 23578 mutex_enter(SD_MUTEX(un)); 23579 23580 if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) { 23581 if ((rval = sd_write_label(dev)) == 0) { 23582 if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) 23583 != 0) { 23584 SD_ERROR(SD_LOG_IOCTL_DKIO, un, 23585 "sd_dkio_set_vtoc: " 23586 "Failed validate geometry\n"); 23587 } 23588 } 23589 } 23590 23591 /* 23592 * If sd_build_label_vtoc, or sd_write_label failed above write the 23593 * devid anyway, what can it hurt? Also preserve the device id by 23594 * writing to the disk acyl for the case where a devid has been 23595 * fabricated. 23596 */ 23597 if (un->un_f_devid_supported && 23598 (un->un_f_opt_fab_devid == TRUE)) { 23599 if (un->un_devid == NULL) { 23600 sd_register_devid(un, SD_DEVINFO(un), 23601 SD_TARGET_IS_UNRESERVED); 23602 } else { 23603 /* 23604 * The device id for this disk has been 23605 * fabricated. Fabricated device id's are 23606 * managed by storing them in the last 2 23607 * available sectors on the drive. The device 23608 * id must be preserved by writing it back out 23609 * to this location. 23610 */ 23611 if (sd_write_deviceid(un) != 0) { 23612 ddi_devid_free(un->un_devid); 23613 un->un_devid = NULL; 23614 } 23615 } 23616 } 23617 mutex_exit(SD_MUTEX(un)); 23618 return (rval); 23619 } 23620 23621 23622 /* 23623 * Function: sd_build_label_vtoc 23624 * 23625 * Description: This routine updates the driver soft state current volume table 23626 * of contents based on a user specified vtoc. 23627 * 23628 * Arguments: un - driver soft state (unit) structure 23629 * user_vtoc - pointer to vtoc structure specifying vtoc to be used 23630 * to update the driver soft state. 23631 * 23632 * Return Code: 0 23633 * EINVAL 23634 */ 23635 23636 static int 23637 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc) 23638 { 23639 struct dk_map *lmap; 23640 struct partition *vpart; 23641 int nblks; 23642 #if defined(_SUNOS_VTOC_8) 23643 int ncyl; 23644 struct dk_map2 *lpart; 23645 #endif /* defined(_SUNOS_VTOC_8) */ 23646 int i; 23647 23648 ASSERT(mutex_owned(SD_MUTEX(un))); 23649 23650 /* Sanity-check the vtoc */ 23651 if (user_vtoc->v_sanity != VTOC_SANE || 23652 user_vtoc->v_sectorsz != un->un_sys_blocksize || 23653 user_vtoc->v_nparts != V_NUMPAR) { 23654 return (EINVAL); 23655 } 23656 23657 nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead; 23658 if (nblks == 0) { 23659 return (EINVAL); 23660 } 23661 23662 #if defined(_SUNOS_VTOC_8) 23663 vpart = user_vtoc->v_part; 23664 for (i = 0; i < V_NUMPAR; i++) { 23665 if ((vpart->p_start % nblks) != 0) { 23666 return (EINVAL); 23667 } 23668 ncyl = vpart->p_start / nblks; 23669 ncyl += vpart->p_size / nblks; 23670 if ((vpart->p_size % nblks) != 0) { 23671 ncyl++; 23672 } 23673 if (ncyl > (int)un->un_g.dkg_ncyl) { 23674 return (EINVAL); 23675 } 23676 vpart++; 23677 } 23678 #endif /* defined(_SUNOS_VTOC_8) */ 23679 23680 /* Put appropriate vtoc structure fields into the disk label */ 23681 #if defined(_SUNOS_VTOC_16) 23682 /* 23683 * The vtoc is always a 32bit data structure to maintain the 23684 * on-disk format. Convert "in place" instead of bcopying it. 23685 */ 23686 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc)))); 23687 23688 /* 23689 * in the 16-slice vtoc, starting sectors are expressed in 23690 * numbers *relative* to the start of the Solaris fdisk partition. 23691 */ 23692 lmap = un->un_map; 23693 vpart = user_vtoc->v_part; 23694 23695 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) { 23696 lmap->dkl_cylno = vpart->p_start / nblks; 23697 lmap->dkl_nblk = vpart->p_size; 23698 } 23699 23700 #elif defined(_SUNOS_VTOC_8) 23701 23702 un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0]; 23703 un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1]; 23704 un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2]; 23705 23706 un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity; 23707 un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version; 23708 23709 bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL); 23710 23711 un->un_vtoc.v_nparts = user_vtoc->v_nparts; 23712 23713 bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved, 23714 sizeof (un->un_vtoc.v_reserved)); 23715 23716 /* 23717 * Note the conversion from starting sector number 23718 * to starting cylinder number. 23719 * Return error if division results in a remainder. 23720 */ 23721 lmap = un->un_map; 23722 lpart = un->un_vtoc.v_part; 23723 vpart = user_vtoc->v_part; 23724 23725 for (i = 0; i < (int)user_vtoc->v_nparts; i++) { 23726 lpart->p_tag = vpart->p_tag; 23727 lpart->p_flag = vpart->p_flag; 23728 lmap->dkl_cylno = vpart->p_start / nblks; 23729 lmap->dkl_nblk = vpart->p_size; 23730 23731 lmap++; 23732 lpart++; 23733 vpart++; 23734 23735 /* (4387723) */ 23736 #ifdef _LP64 23737 if (user_vtoc->timestamp[i] > TIME32_MAX) { 23738 un->un_vtoc.v_timestamp[i] = TIME32_MAX; 23739 } else { 23740 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23741 } 23742 #else 23743 un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i]; 23744 #endif 23745 } 23746 23747 bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII); 23748 #else 23749 #error "No VTOC format defined." 23750 #endif 23751 return (0); 23752 } 23753 23754 /* 23755 * Function: sd_clear_efi 23756 * 23757 * Description: This routine clears all EFI labels. 23758 * 23759 * Arguments: un - driver soft state (unit) structure 23760 * 23761 * Return Code: void 23762 */ 23763 23764 static void 23765 sd_clear_efi(struct sd_lun *un) 23766 { 23767 efi_gpt_t *gpt; 23768 uint_t lbasize; 23769 uint64_t cap; 23770 int rval; 23771 23772 ASSERT(!mutex_owned(SD_MUTEX(un))); 23773 23774 mutex_enter(SD_MUTEX(un)); 23775 un->un_reserved = -1; 23776 mutex_exit(SD_MUTEX(un)); 23777 gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP); 23778 23779 if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) { 23780 goto done; 23781 } 23782 23783 sd_swap_efi_gpt(gpt); 23784 rval = sd_validate_efi(gpt); 23785 if (rval == 0) { 23786 /* clear primary */ 23787 bzero(gpt, sizeof (efi_gpt_t)); 23788 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1, 23789 SD_PATH_DIRECT))) { 23790 SD_INFO(SD_LOG_IO_PARTITION, un, 23791 "sd_clear_efi: clear primary label failed\n"); 23792 } 23793 } 23794 /* the backup */ 23795 rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize, 23796 SD_PATH_DIRECT); 23797 if (rval) { 23798 goto done; 23799 } 23800 /* 23801 * The MMC standard allows READ CAPACITY to be 23802 * inaccurate by a bounded amount (in the interest of 23803 * response latency). As a result, failed READs are 23804 * commonplace (due to the reading of metadata and not 23805 * data). Depending on the per-Vendor/drive Sense data, 23806 * the failed READ can cause many (unnecessary) retries. 23807 */ 23808 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23809 cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23810 SD_PATH_DIRECT)) != 0) { 23811 goto done; 23812 } 23813 sd_swap_efi_gpt(gpt); 23814 rval = sd_validate_efi(gpt); 23815 if (rval == 0) { 23816 /* clear backup */ 23817 SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n", 23818 cap-1); 23819 bzero(gpt, sizeof (efi_gpt_t)); 23820 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23821 cap-1, SD_PATH_DIRECT))) { 23822 SD_INFO(SD_LOG_IO_PARTITION, un, 23823 "sd_clear_efi: clear backup label failed\n"); 23824 } 23825 } else { 23826 /* 23827 * Refer to comments related to off-by-1 at the 23828 * header of this file 23829 */ 23830 if ((rval = sd_send_scsi_READ(un, gpt, lbasize, 23831 cap - 2, ISCD(un) ? SD_PATH_DIRECT_PRIORITY : 23832 SD_PATH_DIRECT)) != 0) { 23833 goto done; 23834 } 23835 sd_swap_efi_gpt(gpt); 23836 rval = sd_validate_efi(gpt); 23837 if (rval == 0) { 23838 /* clear legacy backup EFI label */ 23839 SD_TRACE(SD_LOG_IOCTL, un, 23840 "sd_clear_efi clear backup@%lu\n", cap-2); 23841 bzero(gpt, sizeof (efi_gpt_t)); 23842 if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 23843 cap-2, SD_PATH_DIRECT))) { 23844 SD_INFO(SD_LOG_IO_PARTITION, 23845 un, "sd_clear_efi: " 23846 " clear legacy backup label failed\n"); 23847 } 23848 } 23849 } 23850 23851 done: 23852 kmem_free(gpt, sizeof (efi_gpt_t)); 23853 } 23854 23855 /* 23856 * Function: sd_set_vtoc 23857 * 23858 * Description: This routine writes data to the appropriate positions 23859 * 23860 * Arguments: un - driver soft state (unit) structure 23861 * dkl - the data to be written 23862 * 23863 * Return: void 23864 */ 23865 23866 static int 23867 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl) 23868 { 23869 void *shadow_buf; 23870 uint_t label_addr; 23871 int sec; 23872 int blk; 23873 int head; 23874 int cyl; 23875 int rval; 23876 23877 #if defined(__i386) || defined(__amd64) 23878 label_addr = un->un_solaris_offset + DK_LABEL_LOC; 23879 #else 23880 /* Write the primary label at block 0 of the solaris partition. */ 23881 label_addr = 0; 23882 #endif 23883 23884 if (NOT_DEVBSIZE(un)) { 23885 shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP); 23886 /* 23887 * Read the target's first block. 23888 */ 23889 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23890 un->un_tgt_blocksize, label_addr, 23891 SD_PATH_STANDARD)) != 0) { 23892 goto exit; 23893 } 23894 /* 23895 * Copy the contents of the label into the shadow buffer 23896 * which is of the size of target block size. 23897 */ 23898 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23899 } 23900 23901 /* Write the primary label */ 23902 if (NOT_DEVBSIZE(un)) { 23903 rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize, 23904 label_addr, SD_PATH_STANDARD); 23905 } else { 23906 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23907 label_addr, SD_PATH_STANDARD); 23908 } 23909 if (rval != 0) { 23910 return (rval); 23911 } 23912 23913 /* 23914 * Calculate where the backup labels go. They are always on 23915 * the last alternate cylinder, but some older drives put them 23916 * on head 2 instead of the last head. They are always on the 23917 * first 5 odd sectors of the appropriate track. 23918 * 23919 * We have no choice at this point, but to believe that the 23920 * disk label is valid. Use the geometry of the disk 23921 * as described in the label. 23922 */ 23923 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1; 23924 head = dkl->dkl_nhead - 1; 23925 23926 /* 23927 * Write and verify the backup labels. Make sure we don't try to 23928 * write past the last cylinder. 23929 */ 23930 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) { 23931 blk = (daddr_t)( 23932 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) + 23933 (head * dkl->dkl_nsect) + sec); 23934 #if defined(__i386) || defined(__amd64) 23935 blk += un->un_solaris_offset; 23936 #endif 23937 if (NOT_DEVBSIZE(un)) { 23938 uint64_t tblk; 23939 /* 23940 * Need to read the block first for read modify write. 23941 */ 23942 tblk = (uint64_t)blk; 23943 blk = (int)((tblk * un->un_sys_blocksize) / 23944 un->un_tgt_blocksize); 23945 if ((rval = sd_send_scsi_READ(un, shadow_buf, 23946 un->un_tgt_blocksize, blk, 23947 SD_PATH_STANDARD)) != 0) { 23948 goto exit; 23949 } 23950 /* 23951 * Modify the shadow buffer with the label. 23952 */ 23953 bcopy(dkl, shadow_buf, sizeof (struct dk_label)); 23954 rval = sd_send_scsi_WRITE(un, shadow_buf, 23955 un->un_tgt_blocksize, blk, SD_PATH_STANDARD); 23956 } else { 23957 rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize, 23958 blk, SD_PATH_STANDARD); 23959 SD_INFO(SD_LOG_IO_PARTITION, un, 23960 "sd_set_vtoc: wrote backup label %d\n", blk); 23961 } 23962 if (rval != 0) { 23963 goto exit; 23964 } 23965 } 23966 exit: 23967 if (NOT_DEVBSIZE(un)) { 23968 kmem_free(shadow_buf, un->un_tgt_blocksize); 23969 } 23970 return (rval); 23971 } 23972 23973 /* 23974 * Function: sd_clear_vtoc 23975 * 23976 * Description: This routine clears out the VTOC labels. 23977 * 23978 * Arguments: un - driver soft state (unit) structure 23979 * 23980 * Return: void 23981 */ 23982 23983 static void 23984 sd_clear_vtoc(struct sd_lun *un) 23985 { 23986 struct dk_label *dkl; 23987 23988 mutex_exit(SD_MUTEX(un)); 23989 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 23990 mutex_enter(SD_MUTEX(un)); 23991 /* 23992 * sd_set_vtoc uses these fields in order to figure out 23993 * where to overwrite the backup labels 23994 */ 23995 dkl->dkl_apc = un->un_g.dkg_apc; 23996 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 23997 dkl->dkl_acyl = un->un_g.dkg_acyl; 23998 dkl->dkl_nhead = un->un_g.dkg_nhead; 23999 dkl->dkl_nsect = un->un_g.dkg_nsect; 24000 mutex_exit(SD_MUTEX(un)); 24001 (void) sd_set_vtoc(un, dkl); 24002 kmem_free(dkl, sizeof (struct dk_label)); 24003 24004 mutex_enter(SD_MUTEX(un)); 24005 } 24006 24007 /* 24008 * Function: sd_write_label 24009 * 24010 * Description: This routine will validate and write the driver soft state vtoc 24011 * contents to the device. 24012 * 24013 * Arguments: dev - the device number 24014 * 24015 * Return Code: the code returned by sd_send_scsi_cmd() 24016 * 0 24017 * EINVAL 24018 * ENXIO 24019 * ENOMEM 24020 */ 24021 24022 static int 24023 sd_write_label(dev_t dev) 24024 { 24025 struct sd_lun *un; 24026 struct dk_label *dkl; 24027 short sum; 24028 short *sp; 24029 int i; 24030 int rval; 24031 24032 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24033 (un->un_state == SD_STATE_OFFLINE)) { 24034 return (ENXIO); 24035 } 24036 ASSERT(mutex_owned(SD_MUTEX(un))); 24037 mutex_exit(SD_MUTEX(un)); 24038 dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP); 24039 mutex_enter(SD_MUTEX(un)); 24040 24041 bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc)); 24042 dkl->dkl_rpm = un->un_g.dkg_rpm; 24043 dkl->dkl_pcyl = un->un_g.dkg_pcyl; 24044 dkl->dkl_apc = un->un_g.dkg_apc; 24045 dkl->dkl_intrlv = un->un_g.dkg_intrlv; 24046 dkl->dkl_ncyl = un->un_g.dkg_ncyl; 24047 dkl->dkl_acyl = un->un_g.dkg_acyl; 24048 dkl->dkl_nhead = un->un_g.dkg_nhead; 24049 dkl->dkl_nsect = un->un_g.dkg_nsect; 24050 24051 #if defined(_SUNOS_VTOC_8) 24052 dkl->dkl_obs1 = un->un_g.dkg_obs1; 24053 dkl->dkl_obs2 = un->un_g.dkg_obs2; 24054 dkl->dkl_obs3 = un->un_g.dkg_obs3; 24055 for (i = 0; i < NDKMAP; i++) { 24056 dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno; 24057 dkl->dkl_map[i].dkl_nblk = un->un_map[i].dkl_nblk; 24058 } 24059 bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII); 24060 #elif defined(_SUNOS_VTOC_16) 24061 dkl->dkl_skew = un->un_dkg_skew; 24062 #else 24063 #error "No VTOC format defined." 24064 #endif 24065 24066 dkl->dkl_magic = DKL_MAGIC; 24067 dkl->dkl_write_reinstruct = un->un_g.dkg_write_reinstruct; 24068 dkl->dkl_read_reinstruct = un->un_g.dkg_read_reinstruct; 24069 24070 /* Construct checksum for the new disk label */ 24071 sum = 0; 24072 sp = (short *)dkl; 24073 i = sizeof (struct dk_label) / sizeof (short); 24074 while (i--) { 24075 sum ^= *sp++; 24076 } 24077 dkl->dkl_cksum = sum; 24078 24079 mutex_exit(SD_MUTEX(un)); 24080 24081 rval = sd_set_vtoc(un, dkl); 24082 exit: 24083 kmem_free(dkl, sizeof (struct dk_label)); 24084 mutex_enter(SD_MUTEX(un)); 24085 return (rval); 24086 } 24087 24088 static int 24089 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag) 24090 { 24091 struct sd_lun *un = NULL; 24092 dk_efi_t user_efi; 24093 int rval = 0; 24094 void *buffer; 24095 int valid_efi; 24096 24097 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) 24098 return (ENXIO); 24099 24100 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag)) 24101 return (EFAULT); 24102 24103 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64; 24104 24105 if ((user_efi.dki_length % un->un_tgt_blocksize) || 24106 (user_efi.dki_length > un->un_max_xfer_size)) 24107 return (EINVAL); 24108 24109 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP); 24110 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) { 24111 rval = EFAULT; 24112 } else { 24113 /* 24114 * let's clear the vtoc labels and clear the softstate 24115 * vtoc. 24116 */ 24117 mutex_enter(SD_MUTEX(un)); 24118 if (un->un_vtoc.v_sanity == VTOC_SANE) { 24119 SD_TRACE(SD_LOG_IO_PARTITION, un, 24120 "sd_dkio_set_efi: CLEAR VTOC\n"); 24121 sd_clear_vtoc(un); 24122 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24123 mutex_exit(SD_MUTEX(un)); 24124 ddi_remove_minor_node(SD_DEVINFO(un), "h"); 24125 ddi_remove_minor_node(SD_DEVINFO(un), "h,raw"); 24126 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd", 24127 S_IFBLK, 24128 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24129 un->un_node_type, NULL); 24130 (void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw", 24131 S_IFCHR, 24132 (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE, 24133 un->un_node_type, NULL); 24134 } else 24135 mutex_exit(SD_MUTEX(un)); 24136 rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length, 24137 user_efi.dki_lba, SD_PATH_DIRECT); 24138 if (rval == 0) { 24139 mutex_enter(SD_MUTEX(un)); 24140 24141 /* 24142 * Set the un_reserved for valid efi label. 24143 * Function clear_efi in fdisk and efi_write in 24144 * libefi both change efi label on disk in 3 steps 24145 * 1. Change primary gpt and gpe 24146 * 2. Change backup gpe 24147 * 3. Change backup gpt, which is one block 24148 * We only reread the efi label after the 3rd step, 24149 * or there will be warning "primary label corrupt". 24150 */ 24151 if (user_efi.dki_length == un->un_tgt_blocksize) { 24152 un->un_f_geometry_is_valid = FALSE; 24153 valid_efi = sd_use_efi(un, SD_PATH_DIRECT); 24154 if ((valid_efi == 0) && 24155 un->un_f_devid_supported && 24156 (un->un_f_opt_fab_devid == TRUE)) { 24157 if (un->un_devid == NULL) { 24158 sd_register_devid(un, 24159 SD_DEVINFO(un), 24160 SD_TARGET_IS_UNRESERVED); 24161 } else { 24162 /* 24163 * The device id for this disk 24164 * has been fabricated. The 24165 * device id must be preserved 24166 * by writing it back out to 24167 * disk. 24168 */ 24169 if (sd_write_deviceid(un) 24170 != 0) { 24171 ddi_devid_free( 24172 un->un_devid); 24173 un->un_devid = NULL; 24174 } 24175 } 24176 } 24177 } 24178 24179 mutex_exit(SD_MUTEX(un)); 24180 } 24181 } 24182 kmem_free(buffer, user_efi.dki_length); 24183 return (rval); 24184 } 24185 24186 /* 24187 * Function: sd_dkio_get_mboot 24188 * 24189 * Description: This routine is the driver entry point for handling user 24190 * requests to get the current device mboot (DKIOCGMBOOT) 24191 * 24192 * Arguments: dev - the device number 24193 * arg - pointer to user provided mboot structure specifying 24194 * the current mboot. 24195 * flag - this argument is a pass through to ddi_copyxxx() 24196 * directly from the mode argument of ioctl(). 24197 * 24198 * Return Code: 0 24199 * EINVAL 24200 * EFAULT 24201 * ENXIO 24202 */ 24203 24204 static int 24205 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag) 24206 { 24207 struct sd_lun *un; 24208 struct mboot *mboot; 24209 int rval; 24210 size_t buffer_size; 24211 24212 if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) || 24213 (un->un_state == SD_STATE_OFFLINE)) { 24214 return (ENXIO); 24215 } 24216 24217 if (!un->un_f_mboot_supported || arg == NULL) { 24218 return (EINVAL); 24219 } 24220 24221 /* 24222 * Read the mboot block, located at absolute block 0 on the target. 24223 */ 24224 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot)); 24225 24226 SD_TRACE(SD_LOG_IO_PARTITION, un, 24227 "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size); 24228 24229 mboot = kmem_zalloc(buffer_size, KM_SLEEP); 24230 if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0, 24231 SD_PATH_STANDARD)) == 0) { 24232 if (ddi_copyout(mboot, (void *)arg, 24233 sizeof (struct mboot), flag) != 0) { 24234 rval = EFAULT; 24235 } 24236 } 24237 kmem_free(mboot, buffer_size); 24238 return (rval); 24239 } 24240 24241 24242 /* 24243 * Function: sd_dkio_set_mboot 24244 * 24245 * Description: This routine is the driver entry point for handling user 24246 * requests to validate and set the device master boot 24247 * (DKIOCSMBOOT). 24248 * 24249 * Arguments: dev - the device number 24250 * arg - pointer to user provided mboot structure used to set the 24251 * master boot. 24252 * flag - this argument is a pass through to ddi_copyxxx() 24253 * directly from the mode argument of ioctl(). 24254 * 24255 * Return Code: 0 24256 * EINVAL 24257 * EFAULT 24258 * ENXIO 24259 */ 24260 24261 static int 24262 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag) 24263 { 24264 struct sd_lun *un = NULL; 24265 struct mboot *mboot = NULL; 24266 int rval; 24267 ushort_t magic; 24268 24269 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24270 return (ENXIO); 24271 } 24272 24273 ASSERT(!mutex_owned(SD_MUTEX(un))); 24274 24275 if (!un->un_f_mboot_supported) { 24276 return (EINVAL); 24277 } 24278 24279 if (arg == NULL) { 24280 return (EINVAL); 24281 } 24282 24283 mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP); 24284 24285 if (ddi_copyin((const void *)arg, mboot, 24286 sizeof (struct mboot), flag) != 0) { 24287 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24288 return (EFAULT); 24289 } 24290 24291 /* Is this really a master boot record? */ 24292 magic = LE_16(mboot->signature); 24293 if (magic != MBB_MAGIC) { 24294 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24295 return (EINVAL); 24296 } 24297 24298 rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0, 24299 SD_PATH_STANDARD); 24300 24301 mutex_enter(SD_MUTEX(un)); 24302 #if defined(__i386) || defined(__amd64) 24303 if (rval == 0) { 24304 /* 24305 * mboot has been written successfully. 24306 * update the fdisk and vtoc tables in memory 24307 */ 24308 rval = sd_update_fdisk_and_vtoc(un); 24309 if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) { 24310 mutex_exit(SD_MUTEX(un)); 24311 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24312 return (rval); 24313 } 24314 } 24315 24316 #ifdef __lock_lint 24317 sd_setup_default_geometry(un); 24318 #endif 24319 24320 #else 24321 if (rval == 0) { 24322 /* 24323 * mboot has been written successfully. 24324 * set up the default geometry and VTOC 24325 */ 24326 if (un->un_blockcount <= DK_MAX_BLOCKS) 24327 sd_setup_default_geometry(un); 24328 } 24329 #endif 24330 mutex_exit(SD_MUTEX(un)); 24331 kmem_free(mboot, (size_t)(sizeof (struct mboot))); 24332 return (rval); 24333 } 24334 24335 24336 /* 24337 * Function: sd_setup_default_geometry 24338 * 24339 * Description: This local utility routine sets the default geometry as part of 24340 * setting the device mboot. 24341 * 24342 * Arguments: un - driver soft state (unit) structure 24343 * 24344 * Note: This may be redundant with sd_build_default_label. 24345 */ 24346 24347 static void 24348 sd_setup_default_geometry(struct sd_lun *un) 24349 { 24350 /* zero out the soft state geometry and partition table. */ 24351 bzero(&un->un_g, sizeof (struct dk_geom)); 24352 bzero(&un->un_vtoc, sizeof (struct dk_vtoc)); 24353 bzero(un->un_map, NDKMAP * (sizeof (struct dk_map))); 24354 un->un_asciilabel[0] = '\0'; 24355 24356 /* 24357 * For the rpm, we use the minimum for the disk. 24358 * For the head, cyl and number of sector per track, 24359 * if the capacity <= 1GB, head = 64, sect = 32. 24360 * else head = 255, sect 63 24361 * Note: the capacity should be equal to C*H*S values. 24362 * This will cause some truncation of size due to 24363 * round off errors. For CD-ROMs, this truncation can 24364 * have adverse side effects, so returning ncyl and 24365 * nhead as 1. The nsect will overflow for most of 24366 * CD-ROMs as nsect is of type ushort. 24367 */ 24368 if (ISCD(un)) { 24369 un->un_g.dkg_ncyl = 1; 24370 un->un_g.dkg_nhead = 1; 24371 un->un_g.dkg_nsect = un->un_blockcount; 24372 } else { 24373 if (un->un_blockcount <= 0x1000) { 24374 /* Needed for unlabeled SCSI floppies. */ 24375 un->un_g.dkg_nhead = 2; 24376 un->un_g.dkg_ncyl = 80; 24377 un->un_g.dkg_pcyl = 80; 24378 un->un_g.dkg_nsect = un->un_blockcount / (2 * 80); 24379 } else if (un->un_blockcount <= 0x200000) { 24380 un->un_g.dkg_nhead = 64; 24381 un->un_g.dkg_nsect = 32; 24382 un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32); 24383 } else { 24384 un->un_g.dkg_nhead = 255; 24385 un->un_g.dkg_nsect = 63; 24386 un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63); 24387 } 24388 un->un_blockcount = un->un_g.dkg_ncyl * 24389 un->un_g.dkg_nhead * un->un_g.dkg_nsect; 24390 } 24391 un->un_g.dkg_acyl = 0; 24392 un->un_g.dkg_bcyl = 0; 24393 un->un_g.dkg_intrlv = 1; 24394 un->un_g.dkg_rpm = 200; 24395 un->un_g.dkg_read_reinstruct = 0; 24396 un->un_g.dkg_write_reinstruct = 0; 24397 if (un->un_g.dkg_pcyl == 0) { 24398 un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl; 24399 } 24400 24401 un->un_map['a'-'a'].dkl_cylno = 0; 24402 un->un_map['a'-'a'].dkl_nblk = un->un_blockcount; 24403 un->un_map['c'-'a'].dkl_cylno = 0; 24404 un->un_map['c'-'a'].dkl_nblk = un->un_blockcount; 24405 un->un_f_geometry_is_valid = FALSE; 24406 } 24407 24408 24409 #if defined(__i386) || defined(__amd64) 24410 /* 24411 * Function: sd_update_fdisk_and_vtoc 24412 * 24413 * Description: This local utility routine updates the device fdisk and vtoc 24414 * as part of setting the device mboot. 24415 * 24416 * Arguments: un - driver soft state (unit) structure 24417 * 24418 * Return Code: 0 for success or errno-type return code. 24419 * 24420 * Note:x86: This looks like a duplicate of sd_validate_geometry(), but 24421 * these did exist seperately in x86 sd.c!!! 24422 */ 24423 24424 static int 24425 sd_update_fdisk_and_vtoc(struct sd_lun *un) 24426 { 24427 static char labelstring[128]; 24428 static char buf[256]; 24429 char *label = 0; 24430 int count; 24431 int label_rc = 0; 24432 int gvalid = un->un_f_geometry_is_valid; 24433 int fdisk_rval; 24434 int lbasize; 24435 int capacity; 24436 24437 ASSERT(mutex_owned(SD_MUTEX(un))); 24438 24439 if (un->un_f_tgt_blocksize_is_valid == FALSE) { 24440 return (EINVAL); 24441 } 24442 24443 if (un->un_f_blockcount_is_valid == FALSE) { 24444 return (EINVAL); 24445 } 24446 24447 #if defined(_SUNOS_VTOC_16) 24448 /* 24449 * Set up the "whole disk" fdisk partition; this should always 24450 * exist, regardless of whether the disk contains an fdisk table 24451 * or vtoc. 24452 */ 24453 un->un_map[P0_RAW_DISK].dkl_cylno = 0; 24454 un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount; 24455 #endif /* defined(_SUNOS_VTOC_16) */ 24456 24457 /* 24458 * copy the lbasize and capacity so that if they're 24459 * reset while we're not holding the SD_MUTEX(un), we will 24460 * continue to use valid values after the SD_MUTEX(un) is 24461 * reacquired. 24462 */ 24463 lbasize = un->un_tgt_blocksize; 24464 capacity = un->un_blockcount; 24465 24466 /* 24467 * refresh the logical and physical geometry caches. 24468 * (data from mode sense format/rigid disk geometry pages, 24469 * and scsi_ifgetcap("geometry"). 24470 */ 24471 sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT); 24472 24473 /* 24474 * Only DIRECT ACCESS devices will have Sun labels. 24475 * CD's supposedly have a Sun label, too 24476 */ 24477 if (un->un_f_vtoc_label_supported) { 24478 fdisk_rval = sd_read_fdisk(un, capacity, lbasize, 24479 SD_PATH_DIRECT); 24480 if (fdisk_rval == SD_CMD_FAILURE) { 24481 ASSERT(mutex_owned(SD_MUTEX(un))); 24482 return (EIO); 24483 } 24484 24485 if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) { 24486 ASSERT(mutex_owned(SD_MUTEX(un))); 24487 return (EACCES); 24488 } 24489 24490 if (un->un_solaris_size <= DK_LABEL_LOC) { 24491 /* 24492 * Found fdisk table but no Solaris partition entry, 24493 * so don't call sd_uselabel() and don't create 24494 * a default label. 24495 */ 24496 label_rc = 0; 24497 un->un_f_geometry_is_valid = TRUE; 24498 goto no_solaris_partition; 24499 } 24500 24501 #if defined(_SUNOS_VTOC_8) 24502 label = (char *)un->un_asciilabel; 24503 #elif defined(_SUNOS_VTOC_16) 24504 label = (char *)un->un_vtoc.v_asciilabel; 24505 #else 24506 #error "No VTOC format defined." 24507 #endif 24508 } else if (capacity < 0) { 24509 ASSERT(mutex_owned(SD_MUTEX(un))); 24510 return (EINVAL); 24511 } 24512 24513 /* 24514 * For Removable media We reach here if we have found a 24515 * SOLARIS PARTITION. 24516 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS 24517 * PARTITION has changed from the previous one, hence we will setup a 24518 * default VTOC in this case. 24519 */ 24520 if (un->un_f_geometry_is_valid == FALSE) { 24521 sd_build_default_label(un); 24522 label_rc = 0; 24523 } 24524 24525 no_solaris_partition: 24526 if ((!un->un_f_has_removable_media || 24527 (un->un_f_has_removable_media && 24528 un->un_mediastate == DKIO_EJECTED)) && 24529 (un->un_state == SD_STATE_NORMAL && !gvalid)) { 24530 /* 24531 * Print out a message indicating who and what we are. 24532 * We do this only when we happen to really validate the 24533 * geometry. We may call sd_validate_geometry() at other 24534 * times, ioctl()'s like Get VTOC in which case we 24535 * don't want to print the label. 24536 * If the geometry is valid, print the label string, 24537 * else print vendor and product info, if available 24538 */ 24539 if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) { 24540 SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label); 24541 } else { 24542 mutex_enter(&sd_label_mutex); 24543 sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX, 24544 labelstring); 24545 sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX, 24546 &labelstring[64]); 24547 (void) sprintf(buf, "?Vendor '%s', product '%s'", 24548 labelstring, &labelstring[64]); 24549 if (un->un_f_blockcount_is_valid == TRUE) { 24550 (void) sprintf(&buf[strlen(buf)], 24551 ", %" PRIu64 " %u byte blocks\n", 24552 un->un_blockcount, 24553 un->un_tgt_blocksize); 24554 } else { 24555 (void) sprintf(&buf[strlen(buf)], 24556 ", (unknown capacity)\n"); 24557 } 24558 SD_INFO(SD_LOG_IOCTL_DKIO, un, buf); 24559 mutex_exit(&sd_label_mutex); 24560 } 24561 } 24562 24563 #if defined(_SUNOS_VTOC_16) 24564 /* 24565 * If we have valid geometry, set up the remaining fdisk partitions. 24566 * Note that dkl_cylno is not used for the fdisk map entries, so 24567 * we set it to an entirely bogus value. 24568 */ 24569 for (count = 0; count < FD_NUMPART; count++) { 24570 un->un_map[FDISK_P1 + count].dkl_cylno = -1; 24571 un->un_map[FDISK_P1 + count].dkl_nblk = 24572 un->un_fmap[count].fmap_nblk; 24573 un->un_offset[FDISK_P1 + count] = 24574 un->un_fmap[count].fmap_start; 24575 } 24576 #endif 24577 24578 for (count = 0; count < NDKMAP; count++) { 24579 #if defined(_SUNOS_VTOC_8) 24580 struct dk_map *lp = &un->un_map[count]; 24581 un->un_offset[count] = 24582 un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno; 24583 #elif defined(_SUNOS_VTOC_16) 24584 struct dkl_partition *vp = &un->un_vtoc.v_part[count]; 24585 un->un_offset[count] = vp->p_start + un->un_solaris_offset; 24586 #else 24587 #error "No VTOC format defined." 24588 #endif 24589 } 24590 24591 ASSERT(mutex_owned(SD_MUTEX(un))); 24592 return (label_rc); 24593 } 24594 #endif 24595 24596 24597 /* 24598 * Function: sd_check_media 24599 * 24600 * Description: This utility routine implements the functionality for the 24601 * DKIOCSTATE ioctl. This ioctl blocks the user thread until the 24602 * driver state changes from that specified by the user 24603 * (inserted or ejected). For example, if the user specifies 24604 * DKIO_EJECTED and the current media state is inserted this 24605 * routine will immediately return DKIO_INSERTED. However, if the 24606 * current media state is not inserted the user thread will be 24607 * blocked until the drive state changes. If DKIO_NONE is specified 24608 * the user thread will block until a drive state change occurs. 24609 * 24610 * Arguments: dev - the device number 24611 * state - user pointer to a dkio_state, updated with the current 24612 * drive state at return. 24613 * 24614 * Return Code: ENXIO 24615 * EIO 24616 * EAGAIN 24617 * EINTR 24618 */ 24619 24620 static int 24621 sd_check_media(dev_t dev, enum dkio_state state) 24622 { 24623 struct sd_lun *un = NULL; 24624 enum dkio_state prev_state; 24625 opaque_t token = NULL; 24626 int rval = 0; 24627 24628 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24629 return (ENXIO); 24630 } 24631 24632 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n"); 24633 24634 mutex_enter(SD_MUTEX(un)); 24635 24636 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: " 24637 "state=%x, mediastate=%x\n", state, un->un_mediastate); 24638 24639 prev_state = un->un_mediastate; 24640 24641 /* is there anything to do? */ 24642 if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) { 24643 /* 24644 * submit the request to the scsi_watch service; 24645 * scsi_media_watch_cb() does the real work 24646 */ 24647 mutex_exit(SD_MUTEX(un)); 24648 24649 /* 24650 * This change handles the case where a scsi watch request is 24651 * added to a device that is powered down. To accomplish this 24652 * we power up the device before adding the scsi watch request, 24653 * since the scsi watch sends a TUR directly to the device 24654 * which the device cannot handle if it is powered down. 24655 */ 24656 if (sd_pm_entry(un) != DDI_SUCCESS) { 24657 mutex_enter(SD_MUTEX(un)); 24658 goto done; 24659 } 24660 24661 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), 24662 sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb, 24663 (caddr_t)dev); 24664 24665 sd_pm_exit(un); 24666 24667 mutex_enter(SD_MUTEX(un)); 24668 if (token == NULL) { 24669 rval = EAGAIN; 24670 goto done; 24671 } 24672 24673 /* 24674 * This is a special case IOCTL that doesn't return 24675 * until the media state changes. Routine sdpower 24676 * knows about and handles this so don't count it 24677 * as an active cmd in the driver, which would 24678 * keep the device busy to the pm framework. 24679 * If the count isn't decremented the device can't 24680 * be powered down. 24681 */ 24682 un->un_ncmds_in_driver--; 24683 ASSERT(un->un_ncmds_in_driver >= 0); 24684 24685 /* 24686 * if a prior request had been made, this will be the same 24687 * token, as scsi_watch was designed that way. 24688 */ 24689 un->un_swr_token = token; 24690 un->un_specified_mediastate = state; 24691 24692 /* 24693 * now wait for media change 24694 * we will not be signalled unless mediastate == state but it is 24695 * still better to test for this condition, since there is a 24696 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED 24697 */ 24698 SD_TRACE(SD_LOG_COMMON, un, 24699 "sd_check_media: waiting for media state change\n"); 24700 while (un->un_mediastate == state) { 24701 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) { 24702 SD_TRACE(SD_LOG_COMMON, un, 24703 "sd_check_media: waiting for media state " 24704 "was interrupted\n"); 24705 un->un_ncmds_in_driver++; 24706 rval = EINTR; 24707 goto done; 24708 } 24709 SD_TRACE(SD_LOG_COMMON, un, 24710 "sd_check_media: received signal, state=%x\n", 24711 un->un_mediastate); 24712 } 24713 /* 24714 * Inc the counter to indicate the device once again 24715 * has an active outstanding cmd. 24716 */ 24717 un->un_ncmds_in_driver++; 24718 } 24719 24720 /* invalidate geometry */ 24721 if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) { 24722 sr_ejected(un); 24723 } 24724 24725 if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) { 24726 uint64_t capacity; 24727 uint_t lbasize; 24728 24729 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n"); 24730 mutex_exit(SD_MUTEX(un)); 24731 /* 24732 * Since the following routines use SD_PATH_DIRECT, we must 24733 * call PM directly before the upcoming disk accesses. This 24734 * may cause the disk to be power/spin up. 24735 */ 24736 24737 if (sd_pm_entry(un) == DDI_SUCCESS) { 24738 rval = sd_send_scsi_READ_CAPACITY(un, 24739 &capacity, 24740 &lbasize, SD_PATH_DIRECT); 24741 if (rval != 0) { 24742 sd_pm_exit(un); 24743 mutex_enter(SD_MUTEX(un)); 24744 goto done; 24745 } 24746 } else { 24747 rval = EIO; 24748 mutex_enter(SD_MUTEX(un)); 24749 goto done; 24750 } 24751 mutex_enter(SD_MUTEX(un)); 24752 24753 sd_update_block_info(un, lbasize, capacity); 24754 24755 un->un_f_geometry_is_valid = FALSE; 24756 (void) sd_validate_geometry(un, SD_PATH_DIRECT); 24757 24758 mutex_exit(SD_MUTEX(un)); 24759 rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT, 24760 SD_PATH_DIRECT); 24761 sd_pm_exit(un); 24762 24763 mutex_enter(SD_MUTEX(un)); 24764 } 24765 done: 24766 un->un_f_watcht_stopped = FALSE; 24767 if (un->un_swr_token) { 24768 /* 24769 * Use of this local token and the mutex ensures that we avoid 24770 * some race conditions associated with terminating the 24771 * scsi watch. 24772 */ 24773 token = un->un_swr_token; 24774 un->un_swr_token = (opaque_t)NULL; 24775 mutex_exit(SD_MUTEX(un)); 24776 (void) scsi_watch_request_terminate(token, 24777 SCSI_WATCH_TERMINATE_WAIT); 24778 mutex_enter(SD_MUTEX(un)); 24779 } 24780 24781 /* 24782 * Update the capacity kstat value, if no media previously 24783 * (capacity kstat is 0) and a media has been inserted 24784 * (un_f_blockcount_is_valid == TRUE) 24785 */ 24786 if (un->un_errstats) { 24787 struct sd_errstats *stp = NULL; 24788 24789 stp = (struct sd_errstats *)un->un_errstats->ks_data; 24790 if ((stp->sd_capacity.value.ui64 == 0) && 24791 (un->un_f_blockcount_is_valid == TRUE)) { 24792 stp->sd_capacity.value.ui64 = 24793 (uint64_t)((uint64_t)un->un_blockcount * 24794 un->un_sys_blocksize); 24795 } 24796 } 24797 mutex_exit(SD_MUTEX(un)); 24798 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n"); 24799 return (rval); 24800 } 24801 24802 24803 /* 24804 * Function: sd_delayed_cv_broadcast 24805 * 24806 * Description: Delayed cv_broadcast to allow for target to recover from media 24807 * insertion. 24808 * 24809 * Arguments: arg - driver soft state (unit) structure 24810 */ 24811 24812 static void 24813 sd_delayed_cv_broadcast(void *arg) 24814 { 24815 struct sd_lun *un = arg; 24816 24817 SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n"); 24818 24819 mutex_enter(SD_MUTEX(un)); 24820 un->un_dcvb_timeid = NULL; 24821 cv_broadcast(&un->un_state_cv); 24822 mutex_exit(SD_MUTEX(un)); 24823 } 24824 24825 24826 /* 24827 * Function: sd_media_watch_cb 24828 * 24829 * Description: Callback routine used for support of the DKIOCSTATE ioctl. This 24830 * routine processes the TUR sense data and updates the driver 24831 * state if a transition has occurred. The user thread 24832 * (sd_check_media) is then signalled. 24833 * 24834 * Arguments: arg - the device 'dev_t' is used for context to discriminate 24835 * among multiple watches that share this callback function 24836 * resultp - scsi watch facility result packet containing scsi 24837 * packet, status byte and sense data 24838 * 24839 * Return Code: 0 for success, -1 for failure 24840 */ 24841 24842 static int 24843 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 24844 { 24845 struct sd_lun *un; 24846 struct scsi_status *statusp = resultp->statusp; 24847 uint8_t *sensep = (uint8_t *)resultp->sensep; 24848 enum dkio_state state = DKIO_NONE; 24849 dev_t dev = (dev_t)arg; 24850 uchar_t actual_sense_length; 24851 uint8_t skey, asc, ascq; 24852 24853 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24854 return (-1); 24855 } 24856 actual_sense_length = resultp->actual_sense_length; 24857 24858 mutex_enter(SD_MUTEX(un)); 24859 SD_TRACE(SD_LOG_COMMON, un, 24860 "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n", 24861 *((char *)statusp), (void *)sensep, actual_sense_length); 24862 24863 if (resultp->pkt->pkt_reason == CMD_DEV_GONE) { 24864 un->un_mediastate = DKIO_DEV_GONE; 24865 cv_broadcast(&un->un_state_cv); 24866 mutex_exit(SD_MUTEX(un)); 24867 24868 return (0); 24869 } 24870 24871 /* 24872 * If there was a check condition then sensep points to valid sense data 24873 * If status was not a check condition but a reservation or busy status 24874 * then the new state is DKIO_NONE 24875 */ 24876 if (sensep != NULL) { 24877 skey = scsi_sense_key(sensep); 24878 asc = scsi_sense_asc(sensep); 24879 ascq = scsi_sense_ascq(sensep); 24880 24881 SD_INFO(SD_LOG_COMMON, un, 24882 "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n", 24883 skey, asc, ascq); 24884 /* This routine only uses up to 13 bytes of sense data. */ 24885 if (actual_sense_length >= 13) { 24886 if (skey == KEY_UNIT_ATTENTION) { 24887 if (asc == 0x28) { 24888 state = DKIO_INSERTED; 24889 } 24890 } else { 24891 /* 24892 * if 02/04/02 means that the host 24893 * should send start command. Explicitly 24894 * leave the media state as is 24895 * (inserted) as the media is inserted 24896 * and host has stopped device for PM 24897 * reasons. Upon next true read/write 24898 * to this media will bring the 24899 * device to the right state good for 24900 * media access. 24901 */ 24902 if ((skey == KEY_NOT_READY) && 24903 (asc == 0x3a)) { 24904 state = DKIO_EJECTED; 24905 } 24906 24907 /* 24908 * If the drivge is busy with an operation 24909 * or long write, keep the media in an 24910 * inserted state. 24911 */ 24912 24913 if ((skey == KEY_NOT_READY) && 24914 (asc == 0x04) && 24915 ((ascq == 0x02) || 24916 (ascq == 0x07) || 24917 (ascq == 0x08))) { 24918 state = DKIO_INSERTED; 24919 } 24920 } 24921 } 24922 } else if ((*((char *)statusp) == STATUS_GOOD) && 24923 (resultp->pkt->pkt_reason == CMD_CMPLT)) { 24924 state = DKIO_INSERTED; 24925 } 24926 24927 SD_TRACE(SD_LOG_COMMON, un, 24928 "sd_media_watch_cb: state=%x, specified=%x\n", 24929 state, un->un_specified_mediastate); 24930 24931 /* 24932 * now signal the waiting thread if this is *not* the specified state; 24933 * delay the signal if the state is DKIO_INSERTED to allow the target 24934 * to recover 24935 */ 24936 if (state != un->un_specified_mediastate) { 24937 un->un_mediastate = state; 24938 if (state == DKIO_INSERTED) { 24939 /* 24940 * delay the signal to give the drive a chance 24941 * to do what it apparently needs to do 24942 */ 24943 SD_TRACE(SD_LOG_COMMON, un, 24944 "sd_media_watch_cb: delayed cv_broadcast\n"); 24945 if (un->un_dcvb_timeid == NULL) { 24946 un->un_dcvb_timeid = 24947 timeout(sd_delayed_cv_broadcast, un, 24948 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 24949 } 24950 } else { 24951 SD_TRACE(SD_LOG_COMMON, un, 24952 "sd_media_watch_cb: immediate cv_broadcast\n"); 24953 cv_broadcast(&un->un_state_cv); 24954 } 24955 } 24956 mutex_exit(SD_MUTEX(un)); 24957 return (0); 24958 } 24959 24960 24961 /* 24962 * Function: sd_dkio_get_temp 24963 * 24964 * Description: This routine is the driver entry point for handling ioctl 24965 * requests to get the disk temperature. 24966 * 24967 * Arguments: dev - the device number 24968 * arg - pointer to user provided dk_temperature structure. 24969 * flag - this argument is a pass through to ddi_copyxxx() 24970 * directly from the mode argument of ioctl(). 24971 * 24972 * Return Code: 0 24973 * EFAULT 24974 * ENXIO 24975 * EAGAIN 24976 */ 24977 24978 static int 24979 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag) 24980 { 24981 struct sd_lun *un = NULL; 24982 struct dk_temperature *dktemp = NULL; 24983 uchar_t *temperature_page; 24984 int rval = 0; 24985 int path_flag = SD_PATH_STANDARD; 24986 24987 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 24988 return (ENXIO); 24989 } 24990 24991 dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP); 24992 24993 /* copyin the disk temp argument to get the user flags */ 24994 if (ddi_copyin((void *)arg, dktemp, 24995 sizeof (struct dk_temperature), flag) != 0) { 24996 rval = EFAULT; 24997 goto done; 24998 } 24999 25000 /* Initialize the temperature to invalid. */ 25001 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 25002 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 25003 25004 /* 25005 * Note: Investigate removing the "bypass pm" semantic. 25006 * Can we just bypass PM always? 25007 */ 25008 if (dktemp->dkt_flags & DKT_BYPASS_PM) { 25009 path_flag = SD_PATH_DIRECT; 25010 ASSERT(!mutex_owned(&un->un_pm_mutex)); 25011 mutex_enter(&un->un_pm_mutex); 25012 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 25013 /* 25014 * If DKT_BYPASS_PM is set, and the drive happens to be 25015 * in low power mode, we can not wake it up, Need to 25016 * return EAGAIN. 25017 */ 25018 mutex_exit(&un->un_pm_mutex); 25019 rval = EAGAIN; 25020 goto done; 25021 } else { 25022 /* 25023 * Indicate to PM the device is busy. This is required 25024 * to avoid a race - i.e. the ioctl is issuing a 25025 * command and the pm framework brings down the device 25026 * to low power mode (possible power cut-off on some 25027 * platforms). 25028 */ 25029 mutex_exit(&un->un_pm_mutex); 25030 if (sd_pm_entry(un) != DDI_SUCCESS) { 25031 rval = EAGAIN; 25032 goto done; 25033 } 25034 } 25035 } 25036 25037 temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP); 25038 25039 if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page, 25040 TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) { 25041 goto done2; 25042 } 25043 25044 /* 25045 * For the current temperature verify that the parameter length is 0x02 25046 * and the parameter code is 0x00 25047 */ 25048 if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) && 25049 (temperature_page[5] == 0x00)) { 25050 if (temperature_page[9] == 0xFF) { 25051 dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP; 25052 } else { 25053 dktemp->dkt_cur_temp = (short)(temperature_page[9]); 25054 } 25055 } 25056 25057 /* 25058 * For the reference temperature verify that the parameter 25059 * length is 0x02 and the parameter code is 0x01 25060 */ 25061 if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) && 25062 (temperature_page[11] == 0x01)) { 25063 if (temperature_page[15] == 0xFF) { 25064 dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP; 25065 } else { 25066 dktemp->dkt_ref_temp = (short)(temperature_page[15]); 25067 } 25068 } 25069 25070 /* Do the copyout regardless of the temperature commands status. */ 25071 if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature), 25072 flag) != 0) { 25073 rval = EFAULT; 25074 } 25075 25076 done2: 25077 if (path_flag == SD_PATH_DIRECT) { 25078 sd_pm_exit(un); 25079 } 25080 25081 kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE); 25082 done: 25083 if (dktemp != NULL) { 25084 kmem_free(dktemp, sizeof (struct dk_temperature)); 25085 } 25086 25087 return (rval); 25088 } 25089 25090 25091 /* 25092 * Function: sd_log_page_supported 25093 * 25094 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of 25095 * supported log pages. 25096 * 25097 * Arguments: un - 25098 * log_page - 25099 * 25100 * Return Code: -1 - on error (log sense is optional and may not be supported). 25101 * 0 - log page not found. 25102 * 1 - log page found. 25103 */ 25104 25105 static int 25106 sd_log_page_supported(struct sd_lun *un, int log_page) 25107 { 25108 uchar_t *log_page_data; 25109 int i; 25110 int match = 0; 25111 int log_size; 25112 25113 log_page_data = kmem_zalloc(0xFF, KM_SLEEP); 25114 25115 if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0, 25116 SD_PATH_DIRECT) != 0) { 25117 SD_ERROR(SD_LOG_COMMON, un, 25118 "sd_log_page_supported: failed log page retrieval\n"); 25119 kmem_free(log_page_data, 0xFF); 25120 return (-1); 25121 } 25122 log_size = log_page_data[3]; 25123 25124 /* 25125 * The list of supported log pages start from the fourth byte. Check 25126 * until we run out of log pages or a match is found. 25127 */ 25128 for (i = 4; (i < (log_size + 4)) && !match; i++) { 25129 if (log_page_data[i] == log_page) { 25130 match++; 25131 } 25132 } 25133 kmem_free(log_page_data, 0xFF); 25134 return (match); 25135 } 25136 25137 25138 /* 25139 * Function: sd_mhdioc_failfast 25140 * 25141 * Description: This routine is the driver entry point for handling ioctl 25142 * requests to enable/disable the multihost failfast option. 25143 * (MHIOCENFAILFAST) 25144 * 25145 * Arguments: dev - the device number 25146 * arg - user specified probing interval. 25147 * flag - this argument is a pass through to ddi_copyxxx() 25148 * directly from the mode argument of ioctl(). 25149 * 25150 * Return Code: 0 25151 * EFAULT 25152 * ENXIO 25153 */ 25154 25155 static int 25156 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag) 25157 { 25158 struct sd_lun *un = NULL; 25159 int mh_time; 25160 int rval = 0; 25161 25162 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25163 return (ENXIO); 25164 } 25165 25166 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag)) 25167 return (EFAULT); 25168 25169 if (mh_time) { 25170 mutex_enter(SD_MUTEX(un)); 25171 un->un_resvd_status |= SD_FAILFAST; 25172 mutex_exit(SD_MUTEX(un)); 25173 /* 25174 * If mh_time is INT_MAX, then this ioctl is being used for 25175 * SCSI-3 PGR purposes, and we don't need to spawn watch thread. 25176 */ 25177 if (mh_time != INT_MAX) { 25178 rval = sd_check_mhd(dev, mh_time); 25179 } 25180 } else { 25181 (void) sd_check_mhd(dev, 0); 25182 mutex_enter(SD_MUTEX(un)); 25183 un->un_resvd_status &= ~SD_FAILFAST; 25184 mutex_exit(SD_MUTEX(un)); 25185 } 25186 return (rval); 25187 } 25188 25189 25190 /* 25191 * Function: sd_mhdioc_takeown 25192 * 25193 * Description: This routine is the driver entry point for handling ioctl 25194 * requests to forcefully acquire exclusive access rights to the 25195 * multihost disk (MHIOCTKOWN). 25196 * 25197 * Arguments: dev - the device number 25198 * arg - user provided structure specifying the delay 25199 * parameters in milliseconds 25200 * flag - this argument is a pass through to ddi_copyxxx() 25201 * directly from the mode argument of ioctl(). 25202 * 25203 * Return Code: 0 25204 * EFAULT 25205 * ENXIO 25206 */ 25207 25208 static int 25209 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag) 25210 { 25211 struct sd_lun *un = NULL; 25212 struct mhioctkown *tkown = NULL; 25213 int rval = 0; 25214 25215 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25216 return (ENXIO); 25217 } 25218 25219 if (arg != NULL) { 25220 tkown = (struct mhioctkown *) 25221 kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP); 25222 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag); 25223 if (rval != 0) { 25224 rval = EFAULT; 25225 goto error; 25226 } 25227 } 25228 25229 rval = sd_take_ownership(dev, tkown); 25230 mutex_enter(SD_MUTEX(un)); 25231 if (rval == 0) { 25232 un->un_resvd_status |= SD_RESERVE; 25233 if (tkown != NULL && tkown->reinstate_resv_delay != 0) { 25234 sd_reinstate_resv_delay = 25235 tkown->reinstate_resv_delay * 1000; 25236 } else { 25237 sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY; 25238 } 25239 /* 25240 * Give the scsi_watch routine interval set by 25241 * the MHIOCENFAILFAST ioctl precedence here. 25242 */ 25243 if ((un->un_resvd_status & SD_FAILFAST) == 0) { 25244 mutex_exit(SD_MUTEX(un)); 25245 (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000); 25246 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25247 "sd_mhdioc_takeown : %d\n", 25248 sd_reinstate_resv_delay); 25249 } else { 25250 mutex_exit(SD_MUTEX(un)); 25251 } 25252 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY, 25253 sd_mhd_reset_notify_cb, (caddr_t)un); 25254 } else { 25255 un->un_resvd_status &= ~SD_RESERVE; 25256 mutex_exit(SD_MUTEX(un)); 25257 } 25258 25259 error: 25260 if (tkown != NULL) { 25261 kmem_free(tkown, sizeof (struct mhioctkown)); 25262 } 25263 return (rval); 25264 } 25265 25266 25267 /* 25268 * Function: sd_mhdioc_release 25269 * 25270 * Description: This routine is the driver entry point for handling ioctl 25271 * requests to release exclusive access rights to the multihost 25272 * disk (MHIOCRELEASE). 25273 * 25274 * Arguments: dev - the device number 25275 * 25276 * Return Code: 0 25277 * ENXIO 25278 */ 25279 25280 static int 25281 sd_mhdioc_release(dev_t dev) 25282 { 25283 struct sd_lun *un = NULL; 25284 timeout_id_t resvd_timeid_save; 25285 int resvd_status_save; 25286 int rval = 0; 25287 25288 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25289 return (ENXIO); 25290 } 25291 25292 mutex_enter(SD_MUTEX(un)); 25293 resvd_status_save = un->un_resvd_status; 25294 un->un_resvd_status &= 25295 ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE); 25296 if (un->un_resvd_timeid) { 25297 resvd_timeid_save = un->un_resvd_timeid; 25298 un->un_resvd_timeid = NULL; 25299 mutex_exit(SD_MUTEX(un)); 25300 (void) untimeout(resvd_timeid_save); 25301 } else { 25302 mutex_exit(SD_MUTEX(un)); 25303 } 25304 25305 /* 25306 * destroy any pending timeout thread that may be attempting to 25307 * reinstate reservation on this device. 25308 */ 25309 sd_rmv_resv_reclaim_req(dev); 25310 25311 if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) { 25312 mutex_enter(SD_MUTEX(un)); 25313 if ((un->un_mhd_token) && 25314 ((un->un_resvd_status & SD_FAILFAST) == 0)) { 25315 mutex_exit(SD_MUTEX(un)); 25316 (void) sd_check_mhd(dev, 0); 25317 } else { 25318 mutex_exit(SD_MUTEX(un)); 25319 } 25320 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL, 25321 sd_mhd_reset_notify_cb, (caddr_t)un); 25322 } else { 25323 /* 25324 * sd_mhd_watch_cb will restart the resvd recover timeout thread 25325 */ 25326 mutex_enter(SD_MUTEX(un)); 25327 un->un_resvd_status = resvd_status_save; 25328 mutex_exit(SD_MUTEX(un)); 25329 } 25330 return (rval); 25331 } 25332 25333 25334 /* 25335 * Function: sd_mhdioc_register_devid 25336 * 25337 * Description: This routine is the driver entry point for handling ioctl 25338 * requests to register the device id (MHIOCREREGISTERDEVID). 25339 * 25340 * Note: The implementation for this ioctl has been updated to 25341 * be consistent with the original PSARC case (1999/357) 25342 * (4375899, 4241671, 4220005) 25343 * 25344 * Arguments: dev - the device number 25345 * 25346 * Return Code: 0 25347 * ENXIO 25348 */ 25349 25350 static int 25351 sd_mhdioc_register_devid(dev_t dev) 25352 { 25353 struct sd_lun *un = NULL; 25354 int rval = 0; 25355 25356 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25357 return (ENXIO); 25358 } 25359 25360 ASSERT(!mutex_owned(SD_MUTEX(un))); 25361 25362 mutex_enter(SD_MUTEX(un)); 25363 25364 /* If a devid already exists, de-register it */ 25365 if (un->un_devid != NULL) { 25366 ddi_devid_unregister(SD_DEVINFO(un)); 25367 /* 25368 * After unregister devid, needs to free devid memory 25369 */ 25370 ddi_devid_free(un->un_devid); 25371 un->un_devid = NULL; 25372 } 25373 25374 /* Check for reservation conflict */ 25375 mutex_exit(SD_MUTEX(un)); 25376 rval = sd_send_scsi_TEST_UNIT_READY(un, 0); 25377 mutex_enter(SD_MUTEX(un)); 25378 25379 switch (rval) { 25380 case 0: 25381 sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED); 25382 break; 25383 case EACCES: 25384 break; 25385 default: 25386 rval = EIO; 25387 } 25388 25389 mutex_exit(SD_MUTEX(un)); 25390 return (rval); 25391 } 25392 25393 25394 /* 25395 * Function: sd_mhdioc_inkeys 25396 * 25397 * Description: This routine is the driver entry point for handling ioctl 25398 * requests to issue the SCSI-3 Persistent In Read Keys command 25399 * to the device (MHIOCGRP_INKEYS). 25400 * 25401 * Arguments: dev - the device number 25402 * arg - user provided in_keys structure 25403 * flag - this argument is a pass through to ddi_copyxxx() 25404 * directly from the mode argument of ioctl(). 25405 * 25406 * Return Code: code returned by sd_persistent_reservation_in_read_keys() 25407 * ENXIO 25408 * EFAULT 25409 */ 25410 25411 static int 25412 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag) 25413 { 25414 struct sd_lun *un; 25415 mhioc_inkeys_t inkeys; 25416 int rval = 0; 25417 25418 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25419 return (ENXIO); 25420 } 25421 25422 #ifdef _MULTI_DATAMODEL 25423 switch (ddi_model_convert_from(flag & FMODELS)) { 25424 case DDI_MODEL_ILP32: { 25425 struct mhioc_inkeys32 inkeys32; 25426 25427 if (ddi_copyin(arg, &inkeys32, 25428 sizeof (struct mhioc_inkeys32), flag) != 0) { 25429 return (EFAULT); 25430 } 25431 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li; 25432 if ((rval = sd_persistent_reservation_in_read_keys(un, 25433 &inkeys, flag)) != 0) { 25434 return (rval); 25435 } 25436 inkeys32.generation = inkeys.generation; 25437 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32), 25438 flag) != 0) { 25439 return (EFAULT); 25440 } 25441 break; 25442 } 25443 case DDI_MODEL_NONE: 25444 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), 25445 flag) != 0) { 25446 return (EFAULT); 25447 } 25448 if ((rval = sd_persistent_reservation_in_read_keys(un, 25449 &inkeys, flag)) != 0) { 25450 return (rval); 25451 } 25452 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), 25453 flag) != 0) { 25454 return (EFAULT); 25455 } 25456 break; 25457 } 25458 25459 #else /* ! _MULTI_DATAMODEL */ 25460 25461 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) { 25462 return (EFAULT); 25463 } 25464 rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag); 25465 if (rval != 0) { 25466 return (rval); 25467 } 25468 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) { 25469 return (EFAULT); 25470 } 25471 25472 #endif /* _MULTI_DATAMODEL */ 25473 25474 return (rval); 25475 } 25476 25477 25478 /* 25479 * Function: sd_mhdioc_inresv 25480 * 25481 * Description: This routine is the driver entry point for handling ioctl 25482 * requests to issue the SCSI-3 Persistent In Read Reservations 25483 * command to the device (MHIOCGRP_INKEYS). 25484 * 25485 * Arguments: dev - the device number 25486 * arg - user provided in_resv structure 25487 * flag - this argument is a pass through to ddi_copyxxx() 25488 * directly from the mode argument of ioctl(). 25489 * 25490 * Return Code: code returned by sd_persistent_reservation_in_read_resv() 25491 * ENXIO 25492 * EFAULT 25493 */ 25494 25495 static int 25496 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag) 25497 { 25498 struct sd_lun *un; 25499 mhioc_inresvs_t inresvs; 25500 int rval = 0; 25501 25502 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25503 return (ENXIO); 25504 } 25505 25506 #ifdef _MULTI_DATAMODEL 25507 25508 switch (ddi_model_convert_from(flag & FMODELS)) { 25509 case DDI_MODEL_ILP32: { 25510 struct mhioc_inresvs32 inresvs32; 25511 25512 if (ddi_copyin(arg, &inresvs32, 25513 sizeof (struct mhioc_inresvs32), flag) != 0) { 25514 return (EFAULT); 25515 } 25516 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li; 25517 if ((rval = sd_persistent_reservation_in_read_resv(un, 25518 &inresvs, flag)) != 0) { 25519 return (rval); 25520 } 25521 inresvs32.generation = inresvs.generation; 25522 if (ddi_copyout(&inresvs32, arg, 25523 sizeof (struct mhioc_inresvs32), flag) != 0) { 25524 return (EFAULT); 25525 } 25526 break; 25527 } 25528 case DDI_MODEL_NONE: 25529 if (ddi_copyin(arg, &inresvs, 25530 sizeof (mhioc_inresvs_t), flag) != 0) { 25531 return (EFAULT); 25532 } 25533 if ((rval = sd_persistent_reservation_in_read_resv(un, 25534 &inresvs, flag)) != 0) { 25535 return (rval); 25536 } 25537 if (ddi_copyout(&inresvs, arg, 25538 sizeof (mhioc_inresvs_t), flag) != 0) { 25539 return (EFAULT); 25540 } 25541 break; 25542 } 25543 25544 #else /* ! _MULTI_DATAMODEL */ 25545 25546 if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) { 25547 return (EFAULT); 25548 } 25549 rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag); 25550 if (rval != 0) { 25551 return (rval); 25552 } 25553 if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) { 25554 return (EFAULT); 25555 } 25556 25557 #endif /* ! _MULTI_DATAMODEL */ 25558 25559 return (rval); 25560 } 25561 25562 25563 /* 25564 * The following routines support the clustering functionality described below 25565 * and implement lost reservation reclaim functionality. 25566 * 25567 * Clustering 25568 * ---------- 25569 * The clustering code uses two different, independent forms of SCSI 25570 * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3 25571 * Persistent Group Reservations. For any particular disk, it will use either 25572 * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk. 25573 * 25574 * SCSI-2 25575 * The cluster software takes ownership of a multi-hosted disk by issuing the 25576 * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the 25577 * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster, 25578 * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues 25579 * the MHIOCENFAILFAST ioctl. This ioctl "enables failfast" in the driver. The 25580 * meaning of failfast is that if the driver (on this host) ever encounters the 25581 * scsi error return code RESERVATION_CONFLICT from the device, it should 25582 * immediately panic the host. The motivation for this ioctl is that if this 25583 * host does encounter reservation conflict, the underlying cause is that some 25584 * other host of the cluster has decided that this host is no longer in the 25585 * cluster and has seized control of the disks for itself. Since this host is no 25586 * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl 25587 * does two things: 25588 * (a) it sets a flag that will cause any returned RESERVATION_CONFLICT 25589 * error to panic the host 25590 * (b) it sets up a periodic timer to test whether this host still has 25591 * "access" (in that no other host has reserved the device): if the 25592 * periodic timer gets RESERVATION_CONFLICT, the host is panicked. The 25593 * purpose of that periodic timer is to handle scenarios where the host is 25594 * otherwise temporarily quiescent, temporarily doing no real i/o. 25595 * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host, 25596 * by issuing a SCSI Bus Device Reset. It will then issue a SCSI Reserve for 25597 * the device itself. 25598 * 25599 * SCSI-3 PGR 25600 * A direct semantic implementation of the SCSI-3 Persistent Reservation 25601 * facility is supported through the shared multihost disk ioctls 25602 * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE, 25603 * MHIOCGRP_PREEMPTANDABORT) 25604 * 25605 * Reservation Reclaim: 25606 * -------------------- 25607 * To support the lost reservation reclaim operations this driver creates a 25608 * single thread to handle reinstating reservations on all devices that have 25609 * lost reservations sd_resv_reclaim_requests are logged for all devices that 25610 * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb 25611 * and the reservation reclaim thread loops through the requests to regain the 25612 * lost reservations. 25613 */ 25614 25615 /* 25616 * Function: sd_check_mhd() 25617 * 25618 * Description: This function sets up and submits a scsi watch request or 25619 * terminates an existing watch request. This routine is used in 25620 * support of reservation reclaim. 25621 * 25622 * Arguments: dev - the device 'dev_t' is used for context to discriminate 25623 * among multiple watches that share the callback function 25624 * interval - the number of microseconds specifying the watch 25625 * interval for issuing TEST UNIT READY commands. If 25626 * set to 0 the watch should be terminated. If the 25627 * interval is set to 0 and if the device is required 25628 * to hold reservation while disabling failfast, the 25629 * watch is restarted with an interval of 25630 * reinstate_resv_delay. 25631 * 25632 * Return Code: 0 - Successful submit/terminate of scsi watch request 25633 * ENXIO - Indicates an invalid device was specified 25634 * EAGAIN - Unable to submit the scsi watch request 25635 */ 25636 25637 static int 25638 sd_check_mhd(dev_t dev, int interval) 25639 { 25640 struct sd_lun *un; 25641 opaque_t token; 25642 25643 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25644 return (ENXIO); 25645 } 25646 25647 /* is this a watch termination request? */ 25648 if (interval == 0) { 25649 mutex_enter(SD_MUTEX(un)); 25650 /* if there is an existing watch task then terminate it */ 25651 if (un->un_mhd_token) { 25652 token = un->un_mhd_token; 25653 un->un_mhd_token = NULL; 25654 mutex_exit(SD_MUTEX(un)); 25655 (void) scsi_watch_request_terminate(token, 25656 SCSI_WATCH_TERMINATE_WAIT); 25657 mutex_enter(SD_MUTEX(un)); 25658 } else { 25659 mutex_exit(SD_MUTEX(un)); 25660 /* 25661 * Note: If we return here we don't check for the 25662 * failfast case. This is the original legacy 25663 * implementation but perhaps we should be checking 25664 * the failfast case. 25665 */ 25666 return (0); 25667 } 25668 /* 25669 * If the device is required to hold reservation while 25670 * disabling failfast, we need to restart the scsi_watch 25671 * routine with an interval of reinstate_resv_delay. 25672 */ 25673 if (un->un_resvd_status & SD_RESERVE) { 25674 interval = sd_reinstate_resv_delay/1000; 25675 } else { 25676 /* no failfast so bail */ 25677 mutex_exit(SD_MUTEX(un)); 25678 return (0); 25679 } 25680 mutex_exit(SD_MUTEX(un)); 25681 } 25682 25683 /* 25684 * adjust minimum time interval to 1 second, 25685 * and convert from msecs to usecs 25686 */ 25687 if (interval > 0 && interval < 1000) { 25688 interval = 1000; 25689 } 25690 interval *= 1000; 25691 25692 /* 25693 * submit the request to the scsi_watch service 25694 */ 25695 token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval, 25696 SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev); 25697 if (token == NULL) { 25698 return (EAGAIN); 25699 } 25700 25701 /* 25702 * save token for termination later on 25703 */ 25704 mutex_enter(SD_MUTEX(un)); 25705 un->un_mhd_token = token; 25706 mutex_exit(SD_MUTEX(un)); 25707 return (0); 25708 } 25709 25710 25711 /* 25712 * Function: sd_mhd_watch_cb() 25713 * 25714 * Description: This function is the call back function used by the scsi watch 25715 * facility. The scsi watch facility sends the "Test Unit Ready" 25716 * and processes the status. If applicable (i.e. a "Unit Attention" 25717 * status and automatic "Request Sense" not used) the scsi watch 25718 * facility will send a "Request Sense" and retrieve the sense data 25719 * to be passed to this callback function. In either case the 25720 * automatic "Request Sense" or the facility submitting one, this 25721 * callback is passed the status and sense data. 25722 * 25723 * Arguments: arg - the device 'dev_t' is used for context to discriminate 25724 * among multiple watches that share this callback function 25725 * resultp - scsi watch facility result packet containing scsi 25726 * packet, status byte and sense data 25727 * 25728 * Return Code: 0 - continue the watch task 25729 * non-zero - terminate the watch task 25730 */ 25731 25732 static int 25733 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 25734 { 25735 struct sd_lun *un; 25736 struct scsi_status *statusp; 25737 uint8_t *sensep; 25738 struct scsi_pkt *pkt; 25739 uchar_t actual_sense_length; 25740 dev_t dev = (dev_t)arg; 25741 25742 ASSERT(resultp != NULL); 25743 statusp = resultp->statusp; 25744 sensep = (uint8_t *)resultp->sensep; 25745 pkt = resultp->pkt; 25746 actual_sense_length = resultp->actual_sense_length; 25747 25748 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 25749 return (ENXIO); 25750 } 25751 25752 SD_TRACE(SD_LOG_IOCTL_MHD, un, 25753 "sd_mhd_watch_cb: reason '%s', status '%s'\n", 25754 scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp))); 25755 25756 /* Begin processing of the status and/or sense data */ 25757 if (pkt->pkt_reason != CMD_CMPLT) { 25758 /* Handle the incomplete packet */ 25759 sd_mhd_watch_incomplete(un, pkt); 25760 return (0); 25761 } else if (*((unsigned char *)statusp) != STATUS_GOOD) { 25762 if (*((unsigned char *)statusp) 25763 == STATUS_RESERVATION_CONFLICT) { 25764 /* 25765 * Handle a reservation conflict by panicking if 25766 * configured for failfast or by logging the conflict 25767 * and updating the reservation status 25768 */ 25769 mutex_enter(SD_MUTEX(un)); 25770 if ((un->un_resvd_status & SD_FAILFAST) && 25771 (sd_failfast_enable)) { 25772 sd_panic_for_res_conflict(un); 25773 /*NOTREACHED*/ 25774 } 25775 SD_INFO(SD_LOG_IOCTL_MHD, un, 25776 "sd_mhd_watch_cb: Reservation Conflict\n"); 25777 un->un_resvd_status |= SD_RESERVATION_CONFLICT; 25778 mutex_exit(SD_MUTEX(un)); 25779 } 25780 } 25781 25782 if (sensep != NULL) { 25783 if (actual_sense_length >= (SENSE_LENGTH - 2)) { 25784 mutex_enter(SD_MUTEX(un)); 25785 if ((scsi_sense_asc(sensep) == 25786 SD_SCSI_RESET_SENSE_CODE) && 25787 (un->un_resvd_status & SD_RESERVE)) { 25788 /* 25789 * The additional sense code indicates a power 25790 * on or bus device reset has occurred; update 25791 * the reservation status. 25792 */ 25793 un->un_resvd_status |= 25794 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25795 SD_INFO(SD_LOG_IOCTL_MHD, un, 25796 "sd_mhd_watch_cb: Lost Reservation\n"); 25797 } 25798 } else { 25799 return (0); 25800 } 25801 } else { 25802 mutex_enter(SD_MUTEX(un)); 25803 } 25804 25805 if ((un->un_resvd_status & SD_RESERVE) && 25806 (un->un_resvd_status & SD_LOST_RESERVE)) { 25807 if (un->un_resvd_status & SD_WANT_RESERVE) { 25808 /* 25809 * A reset occurred in between the last probe and this 25810 * one so if a timeout is pending cancel it. 25811 */ 25812 if (un->un_resvd_timeid) { 25813 timeout_id_t temp_id = un->un_resvd_timeid; 25814 un->un_resvd_timeid = NULL; 25815 mutex_exit(SD_MUTEX(un)); 25816 (void) untimeout(temp_id); 25817 mutex_enter(SD_MUTEX(un)); 25818 } 25819 un->un_resvd_status &= ~SD_WANT_RESERVE; 25820 } 25821 if (un->un_resvd_timeid == 0) { 25822 /* Schedule a timeout to handle the lost reservation */ 25823 un->un_resvd_timeid = timeout(sd_mhd_resvd_recover, 25824 (void *)dev, 25825 drv_usectohz(sd_reinstate_resv_delay)); 25826 } 25827 } 25828 mutex_exit(SD_MUTEX(un)); 25829 return (0); 25830 } 25831 25832 25833 /* 25834 * Function: sd_mhd_watch_incomplete() 25835 * 25836 * Description: This function is used to find out why a scsi pkt sent by the 25837 * scsi watch facility was not completed. Under some scenarios this 25838 * routine will return. Otherwise it will send a bus reset to see 25839 * if the drive is still online. 25840 * 25841 * Arguments: un - driver soft state (unit) structure 25842 * pkt - incomplete scsi pkt 25843 */ 25844 25845 static void 25846 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt) 25847 { 25848 int be_chatty; 25849 int perr; 25850 25851 ASSERT(pkt != NULL); 25852 ASSERT(un != NULL); 25853 be_chatty = (!(pkt->pkt_flags & FLAG_SILENT)); 25854 perr = (pkt->pkt_statistics & STAT_PERR); 25855 25856 mutex_enter(SD_MUTEX(un)); 25857 if (un->un_state == SD_STATE_DUMPING) { 25858 mutex_exit(SD_MUTEX(un)); 25859 return; 25860 } 25861 25862 switch (pkt->pkt_reason) { 25863 case CMD_UNX_BUS_FREE: 25864 /* 25865 * If we had a parity error that caused the target to drop BSY*, 25866 * don't be chatty about it. 25867 */ 25868 if (perr && be_chatty) { 25869 be_chatty = 0; 25870 } 25871 break; 25872 case CMD_TAG_REJECT: 25873 /* 25874 * The SCSI-2 spec states that a tag reject will be sent by the 25875 * target if tagged queuing is not supported. A tag reject may 25876 * also be sent during certain initialization periods or to 25877 * control internal resources. For the latter case the target 25878 * may also return Queue Full. 25879 * 25880 * If this driver receives a tag reject from a target that is 25881 * going through an init period or controlling internal 25882 * resources tagged queuing will be disabled. This is a less 25883 * than optimal behavior but the driver is unable to determine 25884 * the target state and assumes tagged queueing is not supported 25885 */ 25886 pkt->pkt_flags = 0; 25887 un->un_tagflags = 0; 25888 25889 if (un->un_f_opt_queueing == TRUE) { 25890 un->un_throttle = min(un->un_throttle, 3); 25891 } else { 25892 un->un_throttle = 1; 25893 } 25894 mutex_exit(SD_MUTEX(un)); 25895 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1); 25896 mutex_enter(SD_MUTEX(un)); 25897 break; 25898 case CMD_INCOMPLETE: 25899 /* 25900 * The transport stopped with an abnormal state, fallthrough and 25901 * reset the target and/or bus unless selection did not complete 25902 * (indicated by STATE_GOT_BUS) in which case we don't want to 25903 * go through a target/bus reset 25904 */ 25905 if (pkt->pkt_state == STATE_GOT_BUS) { 25906 break; 25907 } 25908 /*FALLTHROUGH*/ 25909 25910 case CMD_TIMEOUT: 25911 default: 25912 /* 25913 * The lun may still be running the command, so a lun reset 25914 * should be attempted. If the lun reset fails or cannot be 25915 * issued, than try a target reset. Lastly try a bus reset. 25916 */ 25917 if ((pkt->pkt_statistics & 25918 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) { 25919 int reset_retval = 0; 25920 mutex_exit(SD_MUTEX(un)); 25921 if (un->un_f_allow_bus_device_reset == TRUE) { 25922 if (un->un_f_lun_reset_enabled == TRUE) { 25923 reset_retval = 25924 scsi_reset(SD_ADDRESS(un), 25925 RESET_LUN); 25926 } 25927 if (reset_retval == 0) { 25928 reset_retval = 25929 scsi_reset(SD_ADDRESS(un), 25930 RESET_TARGET); 25931 } 25932 } 25933 if (reset_retval == 0) { 25934 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 25935 } 25936 mutex_enter(SD_MUTEX(un)); 25937 } 25938 break; 25939 } 25940 25941 /* A device/bus reset has occurred; update the reservation status. */ 25942 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 25943 (STAT_BUS_RESET | STAT_DEV_RESET))) { 25944 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 25945 un->un_resvd_status |= 25946 (SD_LOST_RESERVE | SD_WANT_RESERVE); 25947 SD_INFO(SD_LOG_IOCTL_MHD, un, 25948 "sd_mhd_watch_incomplete: Lost Reservation\n"); 25949 } 25950 } 25951 25952 /* 25953 * The disk has been turned off; Update the device state. 25954 * 25955 * Note: Should we be offlining the disk here? 25956 */ 25957 if (pkt->pkt_state == STATE_GOT_BUS) { 25958 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: " 25959 "Disk not responding to selection\n"); 25960 if (un->un_state != SD_STATE_OFFLINE) { 25961 New_state(un, SD_STATE_OFFLINE); 25962 } 25963 } else if (be_chatty) { 25964 /* 25965 * suppress messages if they are all the same pkt reason; 25966 * with TQ, many (up to 256) are returned with the same 25967 * pkt_reason 25968 */ 25969 if (pkt->pkt_reason != un->un_last_pkt_reason) { 25970 SD_ERROR(SD_LOG_IOCTL_MHD, un, 25971 "sd_mhd_watch_incomplete: " 25972 "SCSI transport failed: reason '%s'\n", 25973 scsi_rname(pkt->pkt_reason)); 25974 } 25975 } 25976 un->un_last_pkt_reason = pkt->pkt_reason; 25977 mutex_exit(SD_MUTEX(un)); 25978 } 25979 25980 25981 /* 25982 * Function: sd_sname() 25983 * 25984 * Description: This is a simple little routine to return a string containing 25985 * a printable description of command status byte for use in 25986 * logging. 25987 * 25988 * Arguments: status - pointer to a status byte 25989 * 25990 * Return Code: char * - string containing status description. 25991 */ 25992 25993 static char * 25994 sd_sname(uchar_t status) 25995 { 25996 switch (status & STATUS_MASK) { 25997 case STATUS_GOOD: 25998 return ("good status"); 25999 case STATUS_CHECK: 26000 return ("check condition"); 26001 case STATUS_MET: 26002 return ("condition met"); 26003 case STATUS_BUSY: 26004 return ("busy"); 26005 case STATUS_INTERMEDIATE: 26006 return ("intermediate"); 26007 case STATUS_INTERMEDIATE_MET: 26008 return ("intermediate - condition met"); 26009 case STATUS_RESERVATION_CONFLICT: 26010 return ("reservation_conflict"); 26011 case STATUS_TERMINATED: 26012 return ("command terminated"); 26013 case STATUS_QFULL: 26014 return ("queue full"); 26015 default: 26016 return ("<unknown status>"); 26017 } 26018 } 26019 26020 26021 /* 26022 * Function: sd_mhd_resvd_recover() 26023 * 26024 * Description: This function adds a reservation entry to the 26025 * sd_resv_reclaim_request list and signals the reservation 26026 * reclaim thread that there is work pending. If the reservation 26027 * reclaim thread has not been previously created this function 26028 * will kick it off. 26029 * 26030 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26031 * among multiple watches that share this callback function 26032 * 26033 * Context: This routine is called by timeout() and is run in interrupt 26034 * context. It must not sleep or call other functions which may 26035 * sleep. 26036 */ 26037 26038 static void 26039 sd_mhd_resvd_recover(void *arg) 26040 { 26041 dev_t dev = (dev_t)arg; 26042 struct sd_lun *un; 26043 struct sd_thr_request *sd_treq = NULL; 26044 struct sd_thr_request *sd_cur = NULL; 26045 struct sd_thr_request *sd_prev = NULL; 26046 int already_there = 0; 26047 26048 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26049 return; 26050 } 26051 26052 mutex_enter(SD_MUTEX(un)); 26053 un->un_resvd_timeid = NULL; 26054 if (un->un_resvd_status & SD_WANT_RESERVE) { 26055 /* 26056 * There was a reset so don't issue the reserve, allow the 26057 * sd_mhd_watch_cb callback function to notice this and 26058 * reschedule the timeout for reservation. 26059 */ 26060 mutex_exit(SD_MUTEX(un)); 26061 return; 26062 } 26063 mutex_exit(SD_MUTEX(un)); 26064 26065 /* 26066 * Add this device to the sd_resv_reclaim_request list and the 26067 * sd_resv_reclaim_thread should take care of the rest. 26068 * 26069 * Note: We can't sleep in this context so if the memory allocation 26070 * fails allow the sd_mhd_watch_cb callback function to notice this and 26071 * reschedule the timeout for reservation. (4378460) 26072 */ 26073 sd_treq = (struct sd_thr_request *) 26074 kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP); 26075 if (sd_treq == NULL) { 26076 return; 26077 } 26078 26079 sd_treq->sd_thr_req_next = NULL; 26080 sd_treq->dev = dev; 26081 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26082 if (sd_tr.srq_thr_req_head == NULL) { 26083 sd_tr.srq_thr_req_head = sd_treq; 26084 } else { 26085 sd_cur = sd_prev = sd_tr.srq_thr_req_head; 26086 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) { 26087 if (sd_cur->dev == dev) { 26088 /* 26089 * already in Queue so don't log 26090 * another request for the device 26091 */ 26092 already_there = 1; 26093 break; 26094 } 26095 sd_prev = sd_cur; 26096 } 26097 if (!already_there) { 26098 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: " 26099 "logging request for %lx\n", dev); 26100 sd_prev->sd_thr_req_next = sd_treq; 26101 } else { 26102 kmem_free(sd_treq, sizeof (struct sd_thr_request)); 26103 } 26104 } 26105 26106 /* 26107 * Create a kernel thread to do the reservation reclaim and free up this 26108 * thread. We cannot block this thread while we go away to do the 26109 * reservation reclaim 26110 */ 26111 if (sd_tr.srq_resv_reclaim_thread == NULL) 26112 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0, 26113 sd_resv_reclaim_thread, NULL, 26114 0, &p0, TS_RUN, v.v_maxsyspri - 2); 26115 26116 /* Tell the reservation reclaim thread that it has work to do */ 26117 cv_signal(&sd_tr.srq_resv_reclaim_cv); 26118 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26119 } 26120 26121 /* 26122 * Function: sd_resv_reclaim_thread() 26123 * 26124 * Description: This function implements the reservation reclaim operations 26125 * 26126 * Arguments: arg - the device 'dev_t' is used for context to discriminate 26127 * among multiple watches that share this callback function 26128 */ 26129 26130 static void 26131 sd_resv_reclaim_thread() 26132 { 26133 struct sd_lun *un; 26134 struct sd_thr_request *sd_mhreq; 26135 26136 /* Wait for work */ 26137 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26138 if (sd_tr.srq_thr_req_head == NULL) { 26139 cv_wait(&sd_tr.srq_resv_reclaim_cv, 26140 &sd_tr.srq_resv_reclaim_mutex); 26141 } 26142 26143 /* Loop while we have work */ 26144 while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) { 26145 un = ddi_get_soft_state(sd_state, 26146 SDUNIT(sd_tr.srq_thr_cur_req->dev)); 26147 if (un == NULL) { 26148 /* 26149 * softstate structure is NULL so just 26150 * dequeue the request and continue 26151 */ 26152 sd_tr.srq_thr_req_head = 26153 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26154 kmem_free(sd_tr.srq_thr_cur_req, 26155 sizeof (struct sd_thr_request)); 26156 continue; 26157 } 26158 26159 /* dequeue the request */ 26160 sd_mhreq = sd_tr.srq_thr_cur_req; 26161 sd_tr.srq_thr_req_head = 26162 sd_tr.srq_thr_cur_req->sd_thr_req_next; 26163 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26164 26165 /* 26166 * Reclaim reservation only if SD_RESERVE is still set. There 26167 * may have been a call to MHIOCRELEASE before we got here. 26168 */ 26169 mutex_enter(SD_MUTEX(un)); 26170 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26171 /* 26172 * Note: The SD_LOST_RESERVE flag is cleared before 26173 * reclaiming the reservation. If this is done after the 26174 * call to sd_reserve_release a reservation loss in the 26175 * window between pkt completion of reserve cmd and 26176 * mutex_enter below may not be recognized 26177 */ 26178 un->un_resvd_status &= ~SD_LOST_RESERVE; 26179 mutex_exit(SD_MUTEX(un)); 26180 26181 if (sd_reserve_release(sd_mhreq->dev, 26182 SD_RESERVE) == 0) { 26183 mutex_enter(SD_MUTEX(un)); 26184 un->un_resvd_status |= SD_RESERVE; 26185 mutex_exit(SD_MUTEX(un)); 26186 SD_INFO(SD_LOG_IOCTL_MHD, un, 26187 "sd_resv_reclaim_thread: " 26188 "Reservation Recovered\n"); 26189 } else { 26190 mutex_enter(SD_MUTEX(un)); 26191 un->un_resvd_status |= SD_LOST_RESERVE; 26192 mutex_exit(SD_MUTEX(un)); 26193 SD_INFO(SD_LOG_IOCTL_MHD, un, 26194 "sd_resv_reclaim_thread: Failed " 26195 "Reservation Recovery\n"); 26196 } 26197 } else { 26198 mutex_exit(SD_MUTEX(un)); 26199 } 26200 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26201 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req); 26202 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26203 sd_mhreq = sd_tr.srq_thr_cur_req = NULL; 26204 /* 26205 * wakeup the destroy thread if anyone is waiting on 26206 * us to complete. 26207 */ 26208 cv_signal(&sd_tr.srq_inprocess_cv); 26209 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26210 "sd_resv_reclaim_thread: cv_signalling current request \n"); 26211 } 26212 26213 /* 26214 * cleanup the sd_tr structure now that this thread will not exist 26215 */ 26216 ASSERT(sd_tr.srq_thr_req_head == NULL); 26217 ASSERT(sd_tr.srq_thr_cur_req == NULL); 26218 sd_tr.srq_resv_reclaim_thread = NULL; 26219 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26220 thread_exit(); 26221 } 26222 26223 26224 /* 26225 * Function: sd_rmv_resv_reclaim_req() 26226 * 26227 * Description: This function removes any pending reservation reclaim requests 26228 * for the specified device. 26229 * 26230 * Arguments: dev - the device 'dev_t' 26231 */ 26232 26233 static void 26234 sd_rmv_resv_reclaim_req(dev_t dev) 26235 { 26236 struct sd_thr_request *sd_mhreq; 26237 struct sd_thr_request *sd_prev; 26238 26239 /* Remove a reservation reclaim request from the list */ 26240 mutex_enter(&sd_tr.srq_resv_reclaim_mutex); 26241 if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) { 26242 /* 26243 * We are attempting to reinstate reservation for 26244 * this device. We wait for sd_reserve_release() 26245 * to return before we return. 26246 */ 26247 cv_wait(&sd_tr.srq_inprocess_cv, 26248 &sd_tr.srq_resv_reclaim_mutex); 26249 } else { 26250 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head; 26251 if (sd_mhreq && sd_mhreq->dev == dev) { 26252 sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next; 26253 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26254 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26255 return; 26256 } 26257 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) { 26258 if (sd_mhreq && sd_mhreq->dev == dev) { 26259 break; 26260 } 26261 sd_prev = sd_mhreq; 26262 } 26263 if (sd_mhreq != NULL) { 26264 sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next; 26265 kmem_free(sd_mhreq, sizeof (struct sd_thr_request)); 26266 } 26267 } 26268 mutex_exit(&sd_tr.srq_resv_reclaim_mutex); 26269 } 26270 26271 26272 /* 26273 * Function: sd_mhd_reset_notify_cb() 26274 * 26275 * Description: This is a call back function for scsi_reset_notify. This 26276 * function updates the softstate reserved status and logs the 26277 * reset. The driver scsi watch facility callback function 26278 * (sd_mhd_watch_cb) and reservation reclaim thread functionality 26279 * will reclaim the reservation. 26280 * 26281 * Arguments: arg - driver soft state (unit) structure 26282 */ 26283 26284 static void 26285 sd_mhd_reset_notify_cb(caddr_t arg) 26286 { 26287 struct sd_lun *un = (struct sd_lun *)arg; 26288 26289 mutex_enter(SD_MUTEX(un)); 26290 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) { 26291 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE); 26292 SD_INFO(SD_LOG_IOCTL_MHD, un, 26293 "sd_mhd_reset_notify_cb: Lost Reservation\n"); 26294 } 26295 mutex_exit(SD_MUTEX(un)); 26296 } 26297 26298 26299 /* 26300 * Function: sd_take_ownership() 26301 * 26302 * Description: This routine implements an algorithm to achieve a stable 26303 * reservation on disks which don't implement priority reserve, 26304 * and makes sure that other host lose re-reservation attempts. 26305 * This algorithm contains of a loop that keeps issuing the RESERVE 26306 * for some period of time (min_ownership_delay, default 6 seconds) 26307 * During that loop, it looks to see if there has been a bus device 26308 * reset or bus reset (both of which cause an existing reservation 26309 * to be lost). If the reservation is lost issue RESERVE until a 26310 * period of min_ownership_delay with no resets has gone by, or 26311 * until max_ownership_delay has expired. This loop ensures that 26312 * the host really did manage to reserve the device, in spite of 26313 * resets. The looping for min_ownership_delay (default six 26314 * seconds) is important to early generation clustering products, 26315 * Solstice HA 1.x and Sun Cluster 2.x. Those products use an 26316 * MHIOCENFAILFAST periodic timer of two seconds. By having 26317 * MHIOCTKOWN issue Reserves in a loop for six seconds, and having 26318 * MHIOCENFAILFAST poll every two seconds, the idea is that by the 26319 * time the MHIOCTKOWN ioctl returns, the other host (if any) will 26320 * have already noticed, via the MHIOCENFAILFAST polling, that it 26321 * no longer "owns" the disk and will have panicked itself. Thus, 26322 * the host issuing the MHIOCTKOWN is assured (with timing 26323 * dependencies) that by the time it actually starts to use the 26324 * disk for real work, the old owner is no longer accessing it. 26325 * 26326 * min_ownership_delay is the minimum amount of time for which the 26327 * disk must be reserved continuously devoid of resets before the 26328 * MHIOCTKOWN ioctl will return success. 26329 * 26330 * max_ownership_delay indicates the amount of time by which the 26331 * take ownership should succeed or timeout with an error. 26332 * 26333 * Arguments: dev - the device 'dev_t' 26334 * *p - struct containing timing info. 26335 * 26336 * Return Code: 0 for success or error code 26337 */ 26338 26339 static int 26340 sd_take_ownership(dev_t dev, struct mhioctkown *p) 26341 { 26342 struct sd_lun *un; 26343 int rval; 26344 int err; 26345 int reservation_count = 0; 26346 int min_ownership_delay = 6000000; /* in usec */ 26347 int max_ownership_delay = 30000000; /* in usec */ 26348 clock_t start_time; /* starting time of this algorithm */ 26349 clock_t end_time; /* time limit for giving up */ 26350 clock_t ownership_time; /* time limit for stable ownership */ 26351 clock_t current_time; 26352 clock_t previous_current_time; 26353 26354 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26355 return (ENXIO); 26356 } 26357 26358 /* 26359 * Attempt a device reservation. A priority reservation is requested. 26360 */ 26361 if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE)) 26362 != SD_SUCCESS) { 26363 SD_ERROR(SD_LOG_IOCTL_MHD, un, 26364 "sd_take_ownership: return(1)=%d\n", rval); 26365 return (rval); 26366 } 26367 26368 /* Update the softstate reserved status to indicate the reservation */ 26369 mutex_enter(SD_MUTEX(un)); 26370 un->un_resvd_status |= SD_RESERVE; 26371 un->un_resvd_status &= 26372 ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT); 26373 mutex_exit(SD_MUTEX(un)); 26374 26375 if (p != NULL) { 26376 if (p->min_ownership_delay != 0) { 26377 min_ownership_delay = p->min_ownership_delay * 1000; 26378 } 26379 if (p->max_ownership_delay != 0) { 26380 max_ownership_delay = p->max_ownership_delay * 1000; 26381 } 26382 } 26383 SD_INFO(SD_LOG_IOCTL_MHD, un, 26384 "sd_take_ownership: min, max delays: %d, %d\n", 26385 min_ownership_delay, max_ownership_delay); 26386 26387 start_time = ddi_get_lbolt(); 26388 current_time = start_time; 26389 ownership_time = current_time + drv_usectohz(min_ownership_delay); 26390 end_time = start_time + drv_usectohz(max_ownership_delay); 26391 26392 while (current_time - end_time < 0) { 26393 delay(drv_usectohz(500000)); 26394 26395 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) { 26396 if ((sd_reserve_release(dev, SD_RESERVE)) != 0) { 26397 mutex_enter(SD_MUTEX(un)); 26398 rval = (un->un_resvd_status & 26399 SD_RESERVATION_CONFLICT) ? EACCES : EIO; 26400 mutex_exit(SD_MUTEX(un)); 26401 break; 26402 } 26403 } 26404 previous_current_time = current_time; 26405 current_time = ddi_get_lbolt(); 26406 mutex_enter(SD_MUTEX(un)); 26407 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) { 26408 ownership_time = ddi_get_lbolt() + 26409 drv_usectohz(min_ownership_delay); 26410 reservation_count = 0; 26411 } else { 26412 reservation_count++; 26413 } 26414 un->un_resvd_status |= SD_RESERVE; 26415 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE); 26416 mutex_exit(SD_MUTEX(un)); 26417 26418 SD_INFO(SD_LOG_IOCTL_MHD, un, 26419 "sd_take_ownership: ticks for loop iteration=%ld, " 26420 "reservation=%s\n", (current_time - previous_current_time), 26421 reservation_count ? "ok" : "reclaimed"); 26422 26423 if (current_time - ownership_time >= 0 && 26424 reservation_count >= 4) { 26425 rval = 0; /* Achieved a stable ownership */ 26426 break; 26427 } 26428 if (current_time - end_time >= 0) { 26429 rval = EACCES; /* No ownership in max possible time */ 26430 break; 26431 } 26432 } 26433 SD_TRACE(SD_LOG_IOCTL_MHD, un, 26434 "sd_take_ownership: return(2)=%d\n", rval); 26435 return (rval); 26436 } 26437 26438 26439 /* 26440 * Function: sd_reserve_release() 26441 * 26442 * Description: This function builds and sends scsi RESERVE, RELEASE, and 26443 * PRIORITY RESERVE commands based on a user specified command type 26444 * 26445 * Arguments: dev - the device 'dev_t' 26446 * cmd - user specified command type; one of SD_PRIORITY_RESERVE, 26447 * SD_RESERVE, SD_RELEASE 26448 * 26449 * Return Code: 0 or Error Code 26450 */ 26451 26452 static int 26453 sd_reserve_release(dev_t dev, int cmd) 26454 { 26455 struct uscsi_cmd *com = NULL; 26456 struct sd_lun *un = NULL; 26457 char cdb[CDB_GROUP0]; 26458 int rval; 26459 26460 ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) || 26461 (cmd == SD_PRIORITY_RESERVE)); 26462 26463 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 26464 return (ENXIO); 26465 } 26466 26467 /* instantiate and initialize the command and cdb */ 26468 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 26469 bzero(cdb, CDB_GROUP0); 26470 com->uscsi_flags = USCSI_SILENT; 26471 com->uscsi_timeout = un->un_reserve_release_time; 26472 com->uscsi_cdblen = CDB_GROUP0; 26473 com->uscsi_cdb = cdb; 26474 if (cmd == SD_RELEASE) { 26475 cdb[0] = SCMD_RELEASE; 26476 } else { 26477 cdb[0] = SCMD_RESERVE; 26478 } 26479 26480 /* Send the command. */ 26481 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26482 UIO_SYSSPACE, SD_PATH_STANDARD); 26483 26484 /* 26485 * "break" a reservation that is held by another host, by issuing a 26486 * reset if priority reserve is desired, and we could not get the 26487 * device. 26488 */ 26489 if ((cmd == SD_PRIORITY_RESERVE) && 26490 (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26491 /* 26492 * First try to reset the LUN. If we cannot, then try a target 26493 * reset, followed by a bus reset if the target reset fails. 26494 */ 26495 int reset_retval = 0; 26496 if (un->un_f_lun_reset_enabled == TRUE) { 26497 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN); 26498 } 26499 if (reset_retval == 0) { 26500 /* The LUN reset either failed or was not issued */ 26501 reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26502 } 26503 if ((reset_retval == 0) && 26504 (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) { 26505 rval = EIO; 26506 kmem_free(com, sizeof (*com)); 26507 return (rval); 26508 } 26509 26510 bzero(com, sizeof (struct uscsi_cmd)); 26511 com->uscsi_flags = USCSI_SILENT; 26512 com->uscsi_cdb = cdb; 26513 com->uscsi_cdblen = CDB_GROUP0; 26514 com->uscsi_timeout = 5; 26515 26516 /* 26517 * Reissue the last reserve command, this time without request 26518 * sense. Assume that it is just a regular reserve command. 26519 */ 26520 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 26521 UIO_SYSSPACE, SD_PATH_STANDARD); 26522 } 26523 26524 /* Return an error if still getting a reservation conflict. */ 26525 if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) { 26526 rval = EACCES; 26527 } 26528 26529 kmem_free(com, sizeof (*com)); 26530 return (rval); 26531 } 26532 26533 26534 #define SD_NDUMP_RETRIES 12 26535 /* 26536 * System Crash Dump routine 26537 */ 26538 26539 static int 26540 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 26541 { 26542 int instance; 26543 int partition; 26544 int i; 26545 int err; 26546 struct sd_lun *un; 26547 struct dk_map *lp; 26548 struct scsi_pkt *wr_pktp; 26549 struct buf *wr_bp; 26550 struct buf wr_buf; 26551 daddr_t tgt_byte_offset; /* rmw - byte offset for target */ 26552 daddr_t tgt_blkno; /* rmw - blkno for target */ 26553 size_t tgt_byte_count; /* rmw - # of bytes to xfer */ 26554 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */ 26555 size_t io_start_offset; 26556 int doing_rmw = FALSE; 26557 int rval; 26558 #if defined(__i386) || defined(__amd64) 26559 ssize_t dma_resid; 26560 daddr_t oblkno; 26561 #endif 26562 26563 instance = SDUNIT(dev); 26564 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) || 26565 (!un->un_f_geometry_is_valid) || ISCD(un)) { 26566 return (ENXIO); 26567 } 26568 26569 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un)) 26570 26571 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n"); 26572 26573 partition = SDPART(dev); 26574 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition); 26575 26576 /* Validate blocks to dump at against partition size. */ 26577 lp = &un->un_map[partition]; 26578 if ((blkno + nblk) > lp->dkl_nblk) { 26579 SD_TRACE(SD_LOG_DUMP, un, 26580 "sddump: dump range larger than partition: " 26581 "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n", 26582 blkno, nblk, lp->dkl_nblk); 26583 return (EINVAL); 26584 } 26585 26586 mutex_enter(&un->un_pm_mutex); 26587 if (SD_DEVICE_IS_IN_LOW_POWER(un)) { 26588 struct scsi_pkt *start_pktp; 26589 26590 mutex_exit(&un->un_pm_mutex); 26591 26592 /* 26593 * use pm framework to power on HBA 1st 26594 */ 26595 (void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON); 26596 26597 /* 26598 * Dump no long uses sdpower to power on a device, it's 26599 * in-line here so it can be done in polled mode. 26600 */ 26601 26602 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n"); 26603 26604 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL, 26605 CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL); 26606 26607 if (start_pktp == NULL) { 26608 /* We were not given a SCSI packet, fail. */ 26609 return (EIO); 26610 } 26611 bzero(start_pktp->pkt_cdbp, CDB_GROUP0); 26612 start_pktp->pkt_cdbp[0] = SCMD_START_STOP; 26613 start_pktp->pkt_cdbp[4] = SD_TARGET_START; 26614 start_pktp->pkt_flags = FLAG_NOINTR; 26615 26616 mutex_enter(SD_MUTEX(un)); 26617 SD_FILL_SCSI1_LUN(un, start_pktp); 26618 mutex_exit(SD_MUTEX(un)); 26619 /* 26620 * Scsi_poll returns 0 (success) if the command completes and 26621 * the status block is STATUS_GOOD. 26622 */ 26623 if (sd_scsi_poll(un, start_pktp) != 0) { 26624 scsi_destroy_pkt(start_pktp); 26625 return (EIO); 26626 } 26627 scsi_destroy_pkt(start_pktp); 26628 (void) sd_ddi_pm_resume(un); 26629 } else { 26630 mutex_exit(&un->un_pm_mutex); 26631 } 26632 26633 mutex_enter(SD_MUTEX(un)); 26634 un->un_throttle = 0; 26635 26636 /* 26637 * The first time through, reset the specific target device. 26638 * However, when cpr calls sddump we know that sd is in a 26639 * a good state so no bus reset is required. 26640 * Clear sense data via Request Sense cmd. 26641 * In sddump we don't care about allow_bus_device_reset anymore 26642 */ 26643 26644 if ((un->un_state != SD_STATE_SUSPENDED) && 26645 (un->un_state != SD_STATE_DUMPING)) { 26646 26647 New_state(un, SD_STATE_DUMPING); 26648 26649 if (un->un_f_is_fibre == FALSE) { 26650 mutex_exit(SD_MUTEX(un)); 26651 /* 26652 * Attempt a bus reset for parallel scsi. 26653 * 26654 * Note: A bus reset is required because on some host 26655 * systems (i.e. E420R) a bus device reset is 26656 * insufficient to reset the state of the target. 26657 * 26658 * Note: Don't issue the reset for fibre-channel, 26659 * because this tends to hang the bus (loop) for 26660 * too long while everyone is logging out and in 26661 * and the deadman timer for dumping will fire 26662 * before the dump is complete. 26663 */ 26664 if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) { 26665 mutex_enter(SD_MUTEX(un)); 26666 Restore_state(un); 26667 mutex_exit(SD_MUTEX(un)); 26668 return (EIO); 26669 } 26670 26671 /* Delay to give the device some recovery time. */ 26672 drv_usecwait(10000); 26673 26674 if (sd_send_polled_RQS(un) == SD_FAILURE) { 26675 SD_INFO(SD_LOG_DUMP, un, 26676 "sddump: sd_send_polled_RQS failed\n"); 26677 } 26678 mutex_enter(SD_MUTEX(un)); 26679 } 26680 } 26681 26682 /* 26683 * Convert the partition-relative block number to a 26684 * disk physical block number. 26685 */ 26686 blkno += un->un_offset[partition]; 26687 SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno); 26688 26689 26690 /* 26691 * Check if the device has a non-512 block size. 26692 */ 26693 wr_bp = NULL; 26694 if (NOT_DEVBSIZE(un)) { 26695 tgt_byte_offset = blkno * un->un_sys_blocksize; 26696 tgt_byte_count = nblk * un->un_sys_blocksize; 26697 if ((tgt_byte_offset % un->un_tgt_blocksize) || 26698 (tgt_byte_count % un->un_tgt_blocksize)) { 26699 doing_rmw = TRUE; 26700 /* 26701 * Calculate the block number and number of block 26702 * in terms of the media block size. 26703 */ 26704 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26705 tgt_nblk = 26706 ((tgt_byte_offset + tgt_byte_count + 26707 (un->un_tgt_blocksize - 1)) / 26708 un->un_tgt_blocksize) - tgt_blkno; 26709 26710 /* 26711 * Invoke the routine which is going to do read part 26712 * of read-modify-write. 26713 * Note that this routine returns a pointer to 26714 * a valid bp in wr_bp. 26715 */ 26716 err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk, 26717 &wr_bp); 26718 if (err) { 26719 mutex_exit(SD_MUTEX(un)); 26720 return (err); 26721 } 26722 /* 26723 * Offset is being calculated as - 26724 * (original block # * system block size) - 26725 * (new block # * target block size) 26726 */ 26727 io_start_offset = 26728 ((uint64_t)(blkno * un->un_sys_blocksize)) - 26729 ((uint64_t)(tgt_blkno * un->un_tgt_blocksize)); 26730 26731 ASSERT((io_start_offset >= 0) && 26732 (io_start_offset < un->un_tgt_blocksize)); 26733 /* 26734 * Do the modify portion of read modify write. 26735 */ 26736 bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset], 26737 (size_t)nblk * un->un_sys_blocksize); 26738 } else { 26739 doing_rmw = FALSE; 26740 tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize; 26741 tgt_nblk = tgt_byte_count / un->un_tgt_blocksize; 26742 } 26743 26744 /* Convert blkno and nblk to target blocks */ 26745 blkno = tgt_blkno; 26746 nblk = tgt_nblk; 26747 } else { 26748 wr_bp = &wr_buf; 26749 bzero(wr_bp, sizeof (struct buf)); 26750 wr_bp->b_flags = B_BUSY; 26751 wr_bp->b_un.b_addr = addr; 26752 wr_bp->b_bcount = nblk << DEV_BSHIFT; 26753 wr_bp->b_resid = 0; 26754 } 26755 26756 mutex_exit(SD_MUTEX(un)); 26757 26758 /* 26759 * Obtain a SCSI packet for the write command. 26760 * It should be safe to call the allocator here without 26761 * worrying about being locked for DVMA mapping because 26762 * the address we're passed is already a DVMA mapping 26763 * 26764 * We are also not going to worry about semaphore ownership 26765 * in the dump buffer. Dumping is single threaded at present. 26766 */ 26767 26768 wr_pktp = NULL; 26769 26770 #if defined(__i386) || defined(__amd64) 26771 dma_resid = wr_bp->b_bcount; 26772 oblkno = blkno; 26773 while (dma_resid != 0) { 26774 #endif 26775 26776 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26777 wr_bp->b_flags &= ~B_ERROR; 26778 26779 #if defined(__i386) || defined(__amd64) 26780 blkno = oblkno + 26781 ((wr_bp->b_bcount - dma_resid) / 26782 un->un_tgt_blocksize); 26783 nblk = dma_resid / un->un_tgt_blocksize; 26784 26785 if (wr_pktp) { 26786 /* Partial DMA transfers after initial transfer */ 26787 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp, 26788 blkno, nblk); 26789 } else { 26790 /* Initial transfer */ 26791 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26792 un->un_pkt_flags, NULL_FUNC, NULL, 26793 blkno, nblk); 26794 } 26795 #else 26796 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp, 26797 0, NULL_FUNC, NULL, blkno, nblk); 26798 #endif 26799 26800 if (rval == 0) { 26801 /* We were given a SCSI packet, continue. */ 26802 break; 26803 } 26804 26805 if (i == 0) { 26806 if (wr_bp->b_flags & B_ERROR) { 26807 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26808 "no resources for dumping; " 26809 "error code: 0x%x, retrying", 26810 geterror(wr_bp)); 26811 } else { 26812 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 26813 "no resources for dumping; retrying"); 26814 } 26815 } else if (i != (SD_NDUMP_RETRIES - 1)) { 26816 if (wr_bp->b_flags & B_ERROR) { 26817 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26818 "no resources for dumping; error code: " 26819 "0x%x, retrying\n", geterror(wr_bp)); 26820 } 26821 } else { 26822 if (wr_bp->b_flags & B_ERROR) { 26823 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26824 "no resources for dumping; " 26825 "error code: 0x%x, retries failed, " 26826 "giving up.\n", geterror(wr_bp)); 26827 } else { 26828 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26829 "no resources for dumping; " 26830 "retries failed, giving up.\n"); 26831 } 26832 mutex_enter(SD_MUTEX(un)); 26833 Restore_state(un); 26834 if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) { 26835 mutex_exit(SD_MUTEX(un)); 26836 scsi_free_consistent_buf(wr_bp); 26837 } else { 26838 mutex_exit(SD_MUTEX(un)); 26839 } 26840 return (EIO); 26841 } 26842 drv_usecwait(10000); 26843 } 26844 26845 #if defined(__i386) || defined(__amd64) 26846 /* 26847 * save the resid from PARTIAL_DMA 26848 */ 26849 dma_resid = wr_pktp->pkt_resid; 26850 if (dma_resid != 0) 26851 nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid); 26852 wr_pktp->pkt_resid = 0; 26853 #endif 26854 26855 /* SunBug 1222170 */ 26856 wr_pktp->pkt_flags = FLAG_NOINTR; 26857 26858 err = EIO; 26859 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 26860 26861 /* 26862 * Scsi_poll returns 0 (success) if the command completes and 26863 * the status block is STATUS_GOOD. We should only check 26864 * errors if this condition is not true. Even then we should 26865 * send our own request sense packet only if we have a check 26866 * condition and auto request sense has not been performed by 26867 * the hba. 26868 */ 26869 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n"); 26870 26871 if ((sd_scsi_poll(un, wr_pktp) == 0) && 26872 (wr_pktp->pkt_resid == 0)) { 26873 err = SD_SUCCESS; 26874 break; 26875 } 26876 26877 /* 26878 * Check CMD_DEV_GONE 1st, give up if device is gone. 26879 */ 26880 if (wr_pktp->pkt_reason == CMD_DEV_GONE) { 26881 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 26882 "Device is gone\n"); 26883 break; 26884 } 26885 26886 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) { 26887 SD_INFO(SD_LOG_DUMP, un, 26888 "sddump: write failed with CHECK, try # %d\n", i); 26889 if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) { 26890 (void) sd_send_polled_RQS(un); 26891 } 26892 26893 continue; 26894 } 26895 26896 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) { 26897 int reset_retval = 0; 26898 26899 SD_INFO(SD_LOG_DUMP, un, 26900 "sddump: write failed with BUSY, try # %d\n", i); 26901 26902 if (un->un_f_lun_reset_enabled == TRUE) { 26903 reset_retval = scsi_reset(SD_ADDRESS(un), 26904 RESET_LUN); 26905 } 26906 if (reset_retval == 0) { 26907 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 26908 } 26909 (void) sd_send_polled_RQS(un); 26910 26911 } else { 26912 SD_INFO(SD_LOG_DUMP, un, 26913 "sddump: write failed with 0x%x, try # %d\n", 26914 SD_GET_PKT_STATUS(wr_pktp), i); 26915 mutex_enter(SD_MUTEX(un)); 26916 sd_reset_target(un, wr_pktp); 26917 mutex_exit(SD_MUTEX(un)); 26918 } 26919 26920 /* 26921 * If we are not getting anywhere with lun/target resets, 26922 * let's reset the bus. 26923 */ 26924 if (i == SD_NDUMP_RETRIES/2) { 26925 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 26926 (void) sd_send_polled_RQS(un); 26927 } 26928 26929 } 26930 #if defined(__i386) || defined(__amd64) 26931 } /* dma_resid */ 26932 #endif 26933 26934 scsi_destroy_pkt(wr_pktp); 26935 mutex_enter(SD_MUTEX(un)); 26936 if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) { 26937 mutex_exit(SD_MUTEX(un)); 26938 scsi_free_consistent_buf(wr_bp); 26939 } else { 26940 mutex_exit(SD_MUTEX(un)); 26941 } 26942 SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err); 26943 return (err); 26944 } 26945 26946 /* 26947 * Function: sd_scsi_poll() 26948 * 26949 * Description: This is a wrapper for the scsi_poll call. 26950 * 26951 * Arguments: sd_lun - The unit structure 26952 * scsi_pkt - The scsi packet being sent to the device. 26953 * 26954 * Return Code: 0 - Command completed successfully with good status 26955 * -1 - Command failed. This could indicate a check condition 26956 * or other status value requiring recovery action. 26957 * 26958 */ 26959 26960 static int 26961 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp) 26962 { 26963 int status; 26964 26965 ASSERT(un != NULL); 26966 ASSERT(!mutex_owned(SD_MUTEX(un))); 26967 ASSERT(pktp != NULL); 26968 26969 status = SD_SUCCESS; 26970 26971 if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) { 26972 pktp->pkt_flags |= un->un_tagflags; 26973 pktp->pkt_flags &= ~FLAG_NODISCON; 26974 } 26975 26976 status = sd_ddi_scsi_poll(pktp); 26977 /* 26978 * Scsi_poll returns 0 (success) if the command completes and the 26979 * status block is STATUS_GOOD. We should only check errors if this 26980 * condition is not true. Even then we should send our own request 26981 * sense packet only if we have a check condition and auto 26982 * request sense has not been performed by the hba. 26983 * Don't get RQS data if pkt_reason is CMD_DEV_GONE. 26984 */ 26985 if ((status != SD_SUCCESS) && 26986 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) && 26987 (pktp->pkt_state & STATE_ARQ_DONE) == 0 && 26988 (pktp->pkt_reason != CMD_DEV_GONE)) 26989 (void) sd_send_polled_RQS(un); 26990 26991 return (status); 26992 } 26993 26994 /* 26995 * Function: sd_send_polled_RQS() 26996 * 26997 * Description: This sends the request sense command to a device. 26998 * 26999 * Arguments: sd_lun - The unit structure 27000 * 27001 * Return Code: 0 - Command completed successfully with good status 27002 * -1 - Command failed. 27003 * 27004 */ 27005 27006 static int 27007 sd_send_polled_RQS(struct sd_lun *un) 27008 { 27009 int ret_val; 27010 struct scsi_pkt *rqs_pktp; 27011 struct buf *rqs_bp; 27012 27013 ASSERT(un != NULL); 27014 ASSERT(!mutex_owned(SD_MUTEX(un))); 27015 27016 ret_val = SD_SUCCESS; 27017 27018 rqs_pktp = un->un_rqs_pktp; 27019 rqs_bp = un->un_rqs_bp; 27020 27021 mutex_enter(SD_MUTEX(un)); 27022 27023 if (un->un_sense_isbusy) { 27024 ret_val = SD_FAILURE; 27025 mutex_exit(SD_MUTEX(un)); 27026 return (ret_val); 27027 } 27028 27029 /* 27030 * If the request sense buffer (and packet) is not in use, 27031 * let's set the un_sense_isbusy and send our packet 27032 */ 27033 un->un_sense_isbusy = 1; 27034 rqs_pktp->pkt_resid = 0; 27035 rqs_pktp->pkt_reason = 0; 27036 rqs_pktp->pkt_flags |= FLAG_NOINTR; 27037 bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH); 27038 27039 mutex_exit(SD_MUTEX(un)); 27040 27041 SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at" 27042 " 0x%p\n", rqs_bp->b_un.b_addr); 27043 27044 /* 27045 * Can't send this to sd_scsi_poll, we wrap ourselves around the 27046 * axle - it has a call into us! 27047 */ 27048 if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) { 27049 SD_INFO(SD_LOG_COMMON, un, 27050 "sd_send_polled_RQS: RQS failed\n"); 27051 } 27052 27053 SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:", 27054 (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX); 27055 27056 mutex_enter(SD_MUTEX(un)); 27057 un->un_sense_isbusy = 0; 27058 mutex_exit(SD_MUTEX(un)); 27059 27060 return (ret_val); 27061 } 27062 27063 /* 27064 * Defines needed for localized version of the scsi_poll routine. 27065 */ 27066 #define SD_CSEC 10000 /* usecs */ 27067 #define SD_SEC_TO_CSEC (1000000/SD_CSEC) 27068 27069 27070 /* 27071 * Function: sd_ddi_scsi_poll() 27072 * 27073 * Description: Localized version of the scsi_poll routine. The purpose is to 27074 * send a scsi_pkt to a device as a polled command. This version 27075 * is to ensure more robust handling of transport errors. 27076 * Specifically this routine cures not ready, coming ready 27077 * transition for power up and reset of sonoma's. This can take 27078 * up to 45 seconds for power-on and 20 seconds for reset of a 27079 * sonoma lun. 27080 * 27081 * Arguments: scsi_pkt - The scsi_pkt being sent to a device 27082 * 27083 * Return Code: 0 - Command completed successfully with good status 27084 * -1 - Command failed. 27085 * 27086 */ 27087 27088 static int 27089 sd_ddi_scsi_poll(struct scsi_pkt *pkt) 27090 { 27091 int busy_count; 27092 int timeout; 27093 int rval = SD_FAILURE; 27094 int savef; 27095 uint8_t *sensep; 27096 long savet; 27097 void (*savec)(); 27098 /* 27099 * The following is defined in machdep.c and is used in determining if 27100 * the scsi transport system will do polled I/O instead of interrupt 27101 * I/O when called from xx_dump(). 27102 */ 27103 extern int do_polled_io; 27104 27105 /* 27106 * save old flags in pkt, to restore at end 27107 */ 27108 savef = pkt->pkt_flags; 27109 savec = pkt->pkt_comp; 27110 savet = pkt->pkt_time; 27111 27112 pkt->pkt_flags |= FLAG_NOINTR; 27113 27114 /* 27115 * XXX there is nothing in the SCSA spec that states that we should not 27116 * do a callback for polled cmds; however, removing this will break sd 27117 * and probably other target drivers 27118 */ 27119 pkt->pkt_comp = NULL; 27120 27121 /* 27122 * we don't like a polled command without timeout. 27123 * 60 seconds seems long enough. 27124 */ 27125 if (pkt->pkt_time == 0) { 27126 pkt->pkt_time = SCSI_POLL_TIMEOUT; 27127 } 27128 27129 /* 27130 * Send polled cmd. 27131 * 27132 * We do some error recovery for various errors. Tran_busy, 27133 * queue full, and non-dispatched commands are retried every 10 msec. 27134 * as they are typically transient failures. Busy status and Not 27135 * Ready are retried every second as this status takes a while to 27136 * change. Unit attention is retried for pkt_time (60) times 27137 * with no delay. 27138 */ 27139 timeout = pkt->pkt_time * SD_SEC_TO_CSEC; 27140 27141 for (busy_count = 0; busy_count < timeout; busy_count++) { 27142 int rc; 27143 int poll_delay; 27144 27145 /* 27146 * Initialize pkt status variables. 27147 */ 27148 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0; 27149 27150 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) { 27151 if (rc != TRAN_BUSY) { 27152 /* Transport failed - give up. */ 27153 break; 27154 } else { 27155 /* Transport busy - try again. */ 27156 poll_delay = 1 * SD_CSEC; /* 10 msec */ 27157 } 27158 } else { 27159 /* 27160 * Transport accepted - check pkt status. 27161 */ 27162 rc = (*pkt->pkt_scbp) & STATUS_MASK; 27163 if (pkt->pkt_reason == CMD_CMPLT && 27164 rc == STATUS_CHECK && 27165 pkt->pkt_state & STATE_ARQ_DONE) { 27166 struct scsi_arq_status *arqstat = 27167 (struct scsi_arq_status *)(pkt->pkt_scbp); 27168 27169 sensep = (uint8_t *)&arqstat->sts_sensedata; 27170 } else { 27171 sensep = NULL; 27172 } 27173 27174 if ((pkt->pkt_reason == CMD_CMPLT) && 27175 (rc == STATUS_GOOD)) { 27176 /* No error - we're done */ 27177 rval = SD_SUCCESS; 27178 break; 27179 27180 } else if (pkt->pkt_reason == CMD_DEV_GONE) { 27181 /* Lost connection - give up */ 27182 break; 27183 27184 } else if ((pkt->pkt_reason == CMD_INCOMPLETE) && 27185 (pkt->pkt_state == 0)) { 27186 /* Pkt not dispatched - try again. */ 27187 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27188 27189 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27190 (rc == STATUS_QFULL)) { 27191 /* Queue full - try again. */ 27192 poll_delay = 1 * SD_CSEC; /* 10 msec. */ 27193 27194 } else if ((pkt->pkt_reason == CMD_CMPLT) && 27195 (rc == STATUS_BUSY)) { 27196 /* Busy - try again. */ 27197 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27198 busy_count += (SD_SEC_TO_CSEC - 1); 27199 27200 } else if ((sensep != NULL) && 27201 (scsi_sense_key(sensep) == 27202 KEY_UNIT_ATTENTION)) { 27203 /* Unit Attention - try again */ 27204 busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */ 27205 continue; 27206 27207 } else if ((sensep != NULL) && 27208 (scsi_sense_key(sensep) == KEY_NOT_READY) && 27209 (scsi_sense_asc(sensep) == 0x04) && 27210 (scsi_sense_ascq(sensep) == 0x01)) { 27211 /* Not ready -> ready - try again. */ 27212 poll_delay = 100 * SD_CSEC; /* 1 sec. */ 27213 busy_count += (SD_SEC_TO_CSEC - 1); 27214 27215 } else { 27216 /* BAD status - give up. */ 27217 break; 27218 } 27219 } 27220 27221 if ((curthread->t_flag & T_INTR_THREAD) == 0 && 27222 !do_polled_io) { 27223 delay(drv_usectohz(poll_delay)); 27224 } else { 27225 /* we busy wait during cpr_dump or interrupt threads */ 27226 drv_usecwait(poll_delay); 27227 } 27228 } 27229 27230 pkt->pkt_flags = savef; 27231 pkt->pkt_comp = savec; 27232 pkt->pkt_time = savet; 27233 return (rval); 27234 } 27235 27236 27237 /* 27238 * Function: sd_persistent_reservation_in_read_keys 27239 * 27240 * Description: This routine is the driver entry point for handling CD-ROM 27241 * multi-host persistent reservation requests (MHIOCGRP_INKEYS) 27242 * by sending the SCSI-3 PRIN commands to the device. 27243 * Processes the read keys command response by copying the 27244 * reservation key information into the user provided buffer. 27245 * Support for the 32/64 bit _MULTI_DATAMODEL is implemented. 27246 * 27247 * Arguments: un - Pointer to soft state struct for the target. 27248 * usrp - user provided pointer to multihost Persistent In Read 27249 * Keys structure (mhioc_inkeys_t) 27250 * flag - this argument is a pass through to ddi_copyxxx() 27251 * directly from the mode argument of ioctl(). 27252 * 27253 * Return Code: 0 - Success 27254 * EACCES 27255 * ENOTSUP 27256 * errno return code from sd_send_scsi_cmd() 27257 * 27258 * Context: Can sleep. Does not return until command is completed. 27259 */ 27260 27261 static int 27262 sd_persistent_reservation_in_read_keys(struct sd_lun *un, 27263 mhioc_inkeys_t *usrp, int flag) 27264 { 27265 #ifdef _MULTI_DATAMODEL 27266 struct mhioc_key_list32 li32; 27267 #endif 27268 sd_prin_readkeys_t *in; 27269 mhioc_inkeys_t *ptr; 27270 mhioc_key_list_t li; 27271 uchar_t *data_bufp; 27272 int data_len; 27273 int rval; 27274 size_t copysz; 27275 27276 if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) { 27277 return (EINVAL); 27278 } 27279 bzero(&li, sizeof (mhioc_key_list_t)); 27280 27281 /* 27282 * Get the listsize from user 27283 */ 27284 #ifdef _MULTI_DATAMODEL 27285 27286 switch (ddi_model_convert_from(flag & FMODELS)) { 27287 case DDI_MODEL_ILP32: 27288 copysz = sizeof (struct mhioc_key_list32); 27289 if (ddi_copyin(ptr->li, &li32, copysz, flag)) { 27290 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27291 "sd_persistent_reservation_in_read_keys: " 27292 "failed ddi_copyin: mhioc_key_list32_t\n"); 27293 rval = EFAULT; 27294 goto done; 27295 } 27296 li.listsize = li32.listsize; 27297 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list; 27298 break; 27299 27300 case DDI_MODEL_NONE: 27301 copysz = sizeof (mhioc_key_list_t); 27302 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27303 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27304 "sd_persistent_reservation_in_read_keys: " 27305 "failed ddi_copyin: mhioc_key_list_t\n"); 27306 rval = EFAULT; 27307 goto done; 27308 } 27309 break; 27310 } 27311 27312 #else /* ! _MULTI_DATAMODEL */ 27313 copysz = sizeof (mhioc_key_list_t); 27314 if (ddi_copyin(ptr->li, &li, copysz, flag)) { 27315 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27316 "sd_persistent_reservation_in_read_keys: " 27317 "failed ddi_copyin: mhioc_key_list_t\n"); 27318 rval = EFAULT; 27319 goto done; 27320 } 27321 #endif 27322 27323 data_len = li.listsize * MHIOC_RESV_KEY_SIZE; 27324 data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t)); 27325 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27326 27327 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 27328 data_len, data_bufp)) != 0) { 27329 goto done; 27330 } 27331 in = (sd_prin_readkeys_t *)data_bufp; 27332 ptr->generation = BE_32(in->generation); 27333 li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE; 27334 27335 /* 27336 * Return the min(listsize, listlen) keys 27337 */ 27338 #ifdef _MULTI_DATAMODEL 27339 27340 switch (ddi_model_convert_from(flag & FMODELS)) { 27341 case DDI_MODEL_ILP32: 27342 li32.listlen = li.listlen; 27343 if (ddi_copyout(&li32, ptr->li, copysz, flag)) { 27344 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27345 "sd_persistent_reservation_in_read_keys: " 27346 "failed ddi_copyout: mhioc_key_list32_t\n"); 27347 rval = EFAULT; 27348 goto done; 27349 } 27350 break; 27351 27352 case DDI_MODEL_NONE: 27353 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27354 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27355 "sd_persistent_reservation_in_read_keys: " 27356 "failed ddi_copyout: mhioc_key_list_t\n"); 27357 rval = EFAULT; 27358 goto done; 27359 } 27360 break; 27361 } 27362 27363 #else /* ! _MULTI_DATAMODEL */ 27364 27365 if (ddi_copyout(&li, ptr->li, copysz, flag)) { 27366 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27367 "sd_persistent_reservation_in_read_keys: " 27368 "failed ddi_copyout: mhioc_key_list_t\n"); 27369 rval = EFAULT; 27370 goto done; 27371 } 27372 27373 #endif /* _MULTI_DATAMODEL */ 27374 27375 copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE, 27376 li.listsize * MHIOC_RESV_KEY_SIZE); 27377 if (ddi_copyout(&in->keylist, li.list, copysz, flag)) { 27378 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27379 "sd_persistent_reservation_in_read_keys: " 27380 "failed ddi_copyout: keylist\n"); 27381 rval = EFAULT; 27382 } 27383 done: 27384 kmem_free(data_bufp, data_len); 27385 return (rval); 27386 } 27387 27388 27389 /* 27390 * Function: sd_persistent_reservation_in_read_resv 27391 * 27392 * Description: This routine is the driver entry point for handling CD-ROM 27393 * multi-host persistent reservation requests (MHIOCGRP_INRESV) 27394 * by sending the SCSI-3 PRIN commands to the device. 27395 * Process the read persistent reservations command response by 27396 * copying the reservation information into the user provided 27397 * buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented. 27398 * 27399 * Arguments: un - Pointer to soft state struct for the target. 27400 * usrp - user provided pointer to multihost Persistent In Read 27401 * Keys structure (mhioc_inkeys_t) 27402 * flag - this argument is a pass through to ddi_copyxxx() 27403 * directly from the mode argument of ioctl(). 27404 * 27405 * Return Code: 0 - Success 27406 * EACCES 27407 * ENOTSUP 27408 * errno return code from sd_send_scsi_cmd() 27409 * 27410 * Context: Can sleep. Does not return until command is completed. 27411 */ 27412 27413 static int 27414 sd_persistent_reservation_in_read_resv(struct sd_lun *un, 27415 mhioc_inresvs_t *usrp, int flag) 27416 { 27417 #ifdef _MULTI_DATAMODEL 27418 struct mhioc_resv_desc_list32 resvlist32; 27419 #endif 27420 sd_prin_readresv_t *in; 27421 mhioc_inresvs_t *ptr; 27422 sd_readresv_desc_t *readresv_ptr; 27423 mhioc_resv_desc_list_t resvlist; 27424 mhioc_resv_desc_t resvdesc; 27425 uchar_t *data_bufp; 27426 int data_len; 27427 int rval; 27428 int i; 27429 size_t copysz; 27430 mhioc_resv_desc_t *bufp; 27431 27432 if ((ptr = usrp) == NULL) { 27433 return (EINVAL); 27434 } 27435 27436 /* 27437 * Get the listsize from user 27438 */ 27439 #ifdef _MULTI_DATAMODEL 27440 switch (ddi_model_convert_from(flag & FMODELS)) { 27441 case DDI_MODEL_ILP32: 27442 copysz = sizeof (struct mhioc_resv_desc_list32); 27443 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) { 27444 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27445 "sd_persistent_reservation_in_read_resv: " 27446 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27447 rval = EFAULT; 27448 goto done; 27449 } 27450 resvlist.listsize = resvlist32.listsize; 27451 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list; 27452 break; 27453 27454 case DDI_MODEL_NONE: 27455 copysz = sizeof (mhioc_resv_desc_list_t); 27456 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27457 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27458 "sd_persistent_reservation_in_read_resv: " 27459 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27460 rval = EFAULT; 27461 goto done; 27462 } 27463 break; 27464 } 27465 #else /* ! _MULTI_DATAMODEL */ 27466 copysz = sizeof (mhioc_resv_desc_list_t); 27467 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) { 27468 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27469 "sd_persistent_reservation_in_read_resv: " 27470 "failed ddi_copyin: mhioc_resv_desc_list_t\n"); 27471 rval = EFAULT; 27472 goto done; 27473 } 27474 #endif /* ! _MULTI_DATAMODEL */ 27475 27476 data_len = resvlist.listsize * SCSI3_RESV_DESC_LEN; 27477 data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t)); 27478 data_bufp = kmem_zalloc(data_len, KM_SLEEP); 27479 27480 if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV, 27481 data_len, data_bufp)) != 0) { 27482 goto done; 27483 } 27484 in = (sd_prin_readresv_t *)data_bufp; 27485 ptr->generation = BE_32(in->generation); 27486 resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN; 27487 27488 /* 27489 * Return the min(listsize, listlen( keys 27490 */ 27491 #ifdef _MULTI_DATAMODEL 27492 27493 switch (ddi_model_convert_from(flag & FMODELS)) { 27494 case DDI_MODEL_ILP32: 27495 resvlist32.listlen = resvlist.listlen; 27496 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) { 27497 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27498 "sd_persistent_reservation_in_read_resv: " 27499 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27500 rval = EFAULT; 27501 goto done; 27502 } 27503 break; 27504 27505 case DDI_MODEL_NONE: 27506 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27507 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27508 "sd_persistent_reservation_in_read_resv: " 27509 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27510 rval = EFAULT; 27511 goto done; 27512 } 27513 break; 27514 } 27515 27516 #else /* ! _MULTI_DATAMODEL */ 27517 27518 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) { 27519 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27520 "sd_persistent_reservation_in_read_resv: " 27521 "failed ddi_copyout: mhioc_resv_desc_list_t\n"); 27522 rval = EFAULT; 27523 goto done; 27524 } 27525 27526 #endif /* ! _MULTI_DATAMODEL */ 27527 27528 readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc; 27529 bufp = resvlist.list; 27530 copysz = sizeof (mhioc_resv_desc_t); 27531 for (i = 0; i < min(resvlist.listlen, resvlist.listsize); 27532 i++, readresv_ptr++, bufp++) { 27533 27534 bcopy(&readresv_ptr->resvkey, &resvdesc.key, 27535 MHIOC_RESV_KEY_SIZE); 27536 resvdesc.type = readresv_ptr->type; 27537 resvdesc.scope = readresv_ptr->scope; 27538 resvdesc.scope_specific_addr = 27539 BE_32(readresv_ptr->scope_specific_addr); 27540 27541 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) { 27542 SD_ERROR(SD_LOG_IOCTL_MHD, un, 27543 "sd_persistent_reservation_in_read_resv: " 27544 "failed ddi_copyout: resvlist\n"); 27545 rval = EFAULT; 27546 goto done; 27547 } 27548 } 27549 done: 27550 kmem_free(data_bufp, data_len); 27551 return (rval); 27552 } 27553 27554 27555 /* 27556 * Function: sr_change_blkmode() 27557 * 27558 * Description: This routine is the driver entry point for handling CD-ROM 27559 * block mode ioctl requests. Support for returning and changing 27560 * the current block size in use by the device is implemented. The 27561 * LBA size is changed via a MODE SELECT Block Descriptor. 27562 * 27563 * This routine issues a mode sense with an allocation length of 27564 * 12 bytes for the mode page header and a single block descriptor. 27565 * 27566 * Arguments: dev - the device 'dev_t' 27567 * cmd - the request type; one of CDROMGBLKMODE (get) or 27568 * CDROMSBLKMODE (set) 27569 * data - current block size or requested block size 27570 * flag - this argument is a pass through to ddi_copyxxx() directly 27571 * from the mode argument of ioctl(). 27572 * 27573 * Return Code: the code returned by sd_send_scsi_cmd() 27574 * EINVAL if invalid arguments are provided 27575 * EFAULT if ddi_copyxxx() fails 27576 * ENXIO if fail ddi_get_soft_state 27577 * EIO if invalid mode sense block descriptor length 27578 * 27579 */ 27580 27581 static int 27582 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag) 27583 { 27584 struct sd_lun *un = NULL; 27585 struct mode_header *sense_mhp, *select_mhp; 27586 struct block_descriptor *sense_desc, *select_desc; 27587 int current_bsize; 27588 int rval = EINVAL; 27589 uchar_t *sense = NULL; 27590 uchar_t *select = NULL; 27591 27592 ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE)); 27593 27594 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27595 return (ENXIO); 27596 } 27597 27598 /* 27599 * The block length is changed via the Mode Select block descriptor, the 27600 * "Read/Write Error Recovery" mode page (0x1) contents are not actually 27601 * required as part of this routine. Therefore the mode sense allocation 27602 * length is specified to be the length of a mode page header and a 27603 * block descriptor. 27604 */ 27605 sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27606 27607 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27608 BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) { 27609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27610 "sr_change_blkmode: Mode Sense Failed\n"); 27611 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27612 return (rval); 27613 } 27614 27615 /* Check the block descriptor len to handle only 1 block descriptor */ 27616 sense_mhp = (struct mode_header *)sense; 27617 if ((sense_mhp->bdesc_length == 0) || 27618 (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) { 27619 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27620 "sr_change_blkmode: Mode Sense returned invalid block" 27621 " descriptor length\n"); 27622 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27623 return (EIO); 27624 } 27625 sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH); 27626 current_bsize = ((sense_desc->blksize_hi << 16) | 27627 (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo); 27628 27629 /* Process command */ 27630 switch (cmd) { 27631 case CDROMGBLKMODE: 27632 /* Return the block size obtained during the mode sense */ 27633 if (ddi_copyout(¤t_bsize, (void *)data, 27634 sizeof (int), flag) != 0) 27635 rval = EFAULT; 27636 break; 27637 case CDROMSBLKMODE: 27638 /* Validate the requested block size */ 27639 switch (data) { 27640 case CDROM_BLK_512: 27641 case CDROM_BLK_1024: 27642 case CDROM_BLK_2048: 27643 case CDROM_BLK_2056: 27644 case CDROM_BLK_2336: 27645 case CDROM_BLK_2340: 27646 case CDROM_BLK_2352: 27647 case CDROM_BLK_2368: 27648 case CDROM_BLK_2448: 27649 case CDROM_BLK_2646: 27650 case CDROM_BLK_2647: 27651 break; 27652 default: 27653 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27654 "sr_change_blkmode: " 27655 "Block Size '%ld' Not Supported\n", data); 27656 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27657 return (EINVAL); 27658 } 27659 27660 /* 27661 * The current block size matches the requested block size so 27662 * there is no need to send the mode select to change the size 27663 */ 27664 if (current_bsize == data) { 27665 break; 27666 } 27667 27668 /* Build the select data for the requested block size */ 27669 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP); 27670 select_mhp = (struct mode_header *)select; 27671 select_desc = 27672 (struct block_descriptor *)(select + MODE_HEADER_LENGTH); 27673 /* 27674 * The LBA size is changed via the block descriptor, so the 27675 * descriptor is built according to the user data 27676 */ 27677 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH; 27678 select_desc->blksize_hi = (char)(((data) & 0x00ff0000) >> 16); 27679 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8); 27680 select_desc->blksize_lo = (char)((data) & 0x000000ff); 27681 27682 /* Send the mode select for the requested block size */ 27683 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27684 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27685 SD_PATH_STANDARD)) != 0) { 27686 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27687 "sr_change_blkmode: Mode Select Failed\n"); 27688 /* 27689 * The mode select failed for the requested block size, 27690 * so reset the data for the original block size and 27691 * send it to the target. The error is indicated by the 27692 * return value for the failed mode select. 27693 */ 27694 select_desc->blksize_hi = sense_desc->blksize_hi; 27695 select_desc->blksize_mid = sense_desc->blksize_mid; 27696 select_desc->blksize_lo = sense_desc->blksize_lo; 27697 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, 27698 select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE, 27699 SD_PATH_STANDARD); 27700 } else { 27701 ASSERT(!mutex_owned(SD_MUTEX(un))); 27702 mutex_enter(SD_MUTEX(un)); 27703 sd_update_block_info(un, (uint32_t)data, 0); 27704 27705 mutex_exit(SD_MUTEX(un)); 27706 } 27707 break; 27708 default: 27709 /* should not reach here, but check anyway */ 27710 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27711 "sr_change_blkmode: Command '%x' Not Supported\n", cmd); 27712 rval = EINVAL; 27713 break; 27714 } 27715 27716 if (select) { 27717 kmem_free(select, BUFLEN_CHG_BLK_MODE); 27718 } 27719 if (sense) { 27720 kmem_free(sense, BUFLEN_CHG_BLK_MODE); 27721 } 27722 return (rval); 27723 } 27724 27725 27726 /* 27727 * Note: The following sr_change_speed() and sr_atapi_change_speed() routines 27728 * implement driver support for getting and setting the CD speed. The command 27729 * set used will be based on the device type. If the device has not been 27730 * identified as MMC the Toshiba vendor specific mode page will be used. If 27731 * the device is MMC but does not support the Real Time Streaming feature 27732 * the SET CD SPEED command will be used to set speed and mode page 0x2A will 27733 * be used to read the speed. 27734 */ 27735 27736 /* 27737 * Function: sr_change_speed() 27738 * 27739 * Description: This routine is the driver entry point for handling CD-ROM 27740 * drive speed ioctl requests for devices supporting the Toshiba 27741 * vendor specific drive speed mode page. Support for returning 27742 * and changing the current drive speed in use by the device is 27743 * implemented. 27744 * 27745 * Arguments: dev - the device 'dev_t' 27746 * cmd - the request type; one of CDROMGDRVSPEED (get) or 27747 * CDROMSDRVSPEED (set) 27748 * data - current drive speed or requested drive speed 27749 * flag - this argument is a pass through to ddi_copyxxx() directly 27750 * from the mode argument of ioctl(). 27751 * 27752 * Return Code: the code returned by sd_send_scsi_cmd() 27753 * EINVAL if invalid arguments are provided 27754 * EFAULT if ddi_copyxxx() fails 27755 * ENXIO if fail ddi_get_soft_state 27756 * EIO if invalid mode sense block descriptor length 27757 */ 27758 27759 static int 27760 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27761 { 27762 struct sd_lun *un = NULL; 27763 struct mode_header *sense_mhp, *select_mhp; 27764 struct mode_speed *sense_page, *select_page; 27765 int current_speed; 27766 int rval = EINVAL; 27767 int bd_len; 27768 uchar_t *sense = NULL; 27769 uchar_t *select = NULL; 27770 27771 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27772 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27773 return (ENXIO); 27774 } 27775 27776 /* 27777 * Note: The drive speed is being modified here according to a Toshiba 27778 * vendor specific mode page (0x31). 27779 */ 27780 sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27781 27782 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 27783 BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED, 27784 SD_PATH_STANDARD)) != 0) { 27785 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27786 "sr_change_speed: Mode Sense Failed\n"); 27787 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27788 return (rval); 27789 } 27790 sense_mhp = (struct mode_header *)sense; 27791 27792 /* Check the block descriptor len to handle only 1 block descriptor */ 27793 bd_len = sense_mhp->bdesc_length; 27794 if (bd_len > MODE_BLK_DESC_LENGTH) { 27795 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27796 "sr_change_speed: Mode Sense returned invalid block " 27797 "descriptor length\n"); 27798 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27799 return (EIO); 27800 } 27801 27802 sense_page = (struct mode_speed *) 27803 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 27804 current_speed = sense_page->speed; 27805 27806 /* Process command */ 27807 switch (cmd) { 27808 case CDROMGDRVSPEED: 27809 /* Return the drive speed obtained during the mode sense */ 27810 if (current_speed == 0x2) { 27811 current_speed = CDROM_TWELVE_SPEED; 27812 } 27813 if (ddi_copyout(¤t_speed, (void *)data, 27814 sizeof (int), flag) != 0) { 27815 rval = EFAULT; 27816 } 27817 break; 27818 case CDROMSDRVSPEED: 27819 /* Validate the requested drive speed */ 27820 switch ((uchar_t)data) { 27821 case CDROM_TWELVE_SPEED: 27822 data = 0x2; 27823 /*FALLTHROUGH*/ 27824 case CDROM_NORMAL_SPEED: 27825 case CDROM_DOUBLE_SPEED: 27826 case CDROM_QUAD_SPEED: 27827 case CDROM_MAXIMUM_SPEED: 27828 break; 27829 default: 27830 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27831 "sr_change_speed: " 27832 "Drive Speed '%d' Not Supported\n", (uchar_t)data); 27833 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27834 return (EINVAL); 27835 } 27836 27837 /* 27838 * The current drive speed matches the requested drive speed so 27839 * there is no need to send the mode select to change the speed 27840 */ 27841 if (current_speed == data) { 27842 break; 27843 } 27844 27845 /* Build the select data for the requested drive speed */ 27846 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP); 27847 select_mhp = (struct mode_header *)select; 27848 select_mhp->bdesc_length = 0; 27849 select_page = 27850 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27851 select_page = 27852 (struct mode_speed *)(select + MODE_HEADER_LENGTH); 27853 select_page->mode_page.code = CDROM_MODE_SPEED; 27854 select_page->mode_page.length = 2; 27855 select_page->speed = (uchar_t)data; 27856 27857 /* Send the mode select for the requested block size */ 27858 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27859 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27860 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 27861 /* 27862 * The mode select failed for the requested drive speed, 27863 * so reset the data for the original drive speed and 27864 * send it to the target. The error is indicated by the 27865 * return value for the failed mode select. 27866 */ 27867 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27868 "sr_drive_speed: Mode Select Failed\n"); 27869 select_page->speed = sense_page->speed; 27870 (void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 27871 MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH, 27872 SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 27873 } 27874 break; 27875 default: 27876 /* should not reach here, but check anyway */ 27877 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27878 "sr_change_speed: Command '%x' Not Supported\n", cmd); 27879 rval = EINVAL; 27880 break; 27881 } 27882 27883 if (select) { 27884 kmem_free(select, BUFLEN_MODE_CDROM_SPEED); 27885 } 27886 if (sense) { 27887 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED); 27888 } 27889 27890 return (rval); 27891 } 27892 27893 27894 /* 27895 * Function: sr_atapi_change_speed() 27896 * 27897 * Description: This routine is the driver entry point for handling CD-ROM 27898 * drive speed ioctl requests for MMC devices that do not support 27899 * the Real Time Streaming feature (0x107). 27900 * 27901 * Note: This routine will use the SET SPEED command which may not 27902 * be supported by all devices. 27903 * 27904 * Arguments: dev- the device 'dev_t' 27905 * cmd- the request type; one of CDROMGDRVSPEED (get) or 27906 * CDROMSDRVSPEED (set) 27907 * data- current drive speed or requested drive speed 27908 * flag- this argument is a pass through to ddi_copyxxx() directly 27909 * from the mode argument of ioctl(). 27910 * 27911 * Return Code: the code returned by sd_send_scsi_cmd() 27912 * EINVAL if invalid arguments are provided 27913 * EFAULT if ddi_copyxxx() fails 27914 * ENXIO if fail ddi_get_soft_state 27915 * EIO if invalid mode sense block descriptor length 27916 */ 27917 27918 static int 27919 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag) 27920 { 27921 struct sd_lun *un; 27922 struct uscsi_cmd *com = NULL; 27923 struct mode_header_grp2 *sense_mhp; 27924 uchar_t *sense_page; 27925 uchar_t *sense = NULL; 27926 char cdb[CDB_GROUP5]; 27927 int bd_len; 27928 int current_speed = 0; 27929 int max_speed = 0; 27930 int rval; 27931 27932 ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED)); 27933 27934 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 27935 return (ENXIO); 27936 } 27937 27938 sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP); 27939 27940 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 27941 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, 27942 SD_PATH_STANDARD)) != 0) { 27943 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27944 "sr_atapi_change_speed: Mode Sense Failed\n"); 27945 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27946 return (rval); 27947 } 27948 27949 /* Check the block descriptor len to handle only 1 block descriptor */ 27950 sense_mhp = (struct mode_header_grp2 *)sense; 27951 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo; 27952 if (bd_len > MODE_BLK_DESC_LENGTH) { 27953 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27954 "sr_atapi_change_speed: Mode Sense returned invalid " 27955 "block descriptor length\n"); 27956 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27957 return (EIO); 27958 } 27959 27960 /* Calculate the current and maximum drive speeds */ 27961 sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 27962 current_speed = (sense_page[14] << 8) | sense_page[15]; 27963 max_speed = (sense_page[8] << 8) | sense_page[9]; 27964 27965 /* Process the command */ 27966 switch (cmd) { 27967 case CDROMGDRVSPEED: 27968 current_speed /= SD_SPEED_1X; 27969 if (ddi_copyout(¤t_speed, (void *)data, 27970 sizeof (int), flag) != 0) 27971 rval = EFAULT; 27972 break; 27973 case CDROMSDRVSPEED: 27974 /* Convert the speed code to KB/sec */ 27975 switch ((uchar_t)data) { 27976 case CDROM_NORMAL_SPEED: 27977 current_speed = SD_SPEED_1X; 27978 break; 27979 case CDROM_DOUBLE_SPEED: 27980 current_speed = 2 * SD_SPEED_1X; 27981 break; 27982 case CDROM_QUAD_SPEED: 27983 current_speed = 4 * SD_SPEED_1X; 27984 break; 27985 case CDROM_TWELVE_SPEED: 27986 current_speed = 12 * SD_SPEED_1X; 27987 break; 27988 case CDROM_MAXIMUM_SPEED: 27989 current_speed = 0xffff; 27990 break; 27991 default: 27992 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 27993 "sr_atapi_change_speed: invalid drive speed %d\n", 27994 (uchar_t)data); 27995 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 27996 return (EINVAL); 27997 } 27998 27999 /* Check the request against the drive's max speed. */ 28000 if (current_speed != 0xffff) { 28001 if (current_speed > max_speed) { 28002 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 28003 return (EINVAL); 28004 } 28005 } 28006 28007 /* 28008 * Build and send the SET SPEED command 28009 * 28010 * Note: The SET SPEED (0xBB) command used in this routine is 28011 * obsolete per the SCSI MMC spec but still supported in the 28012 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28013 * therefore the command is still implemented in this routine. 28014 */ 28015 bzero(cdb, sizeof (cdb)); 28016 cdb[0] = (char)SCMD_SET_CDROM_SPEED; 28017 cdb[2] = (uchar_t)(current_speed >> 8); 28018 cdb[3] = (uchar_t)current_speed; 28019 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28020 com->uscsi_cdb = (caddr_t)cdb; 28021 com->uscsi_cdblen = CDB_GROUP5; 28022 com->uscsi_bufaddr = NULL; 28023 com->uscsi_buflen = 0; 28024 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28025 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0, 28026 UIO_SYSSPACE, SD_PATH_STANDARD); 28027 break; 28028 default: 28029 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28030 "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd); 28031 rval = EINVAL; 28032 } 28033 28034 if (sense) { 28035 kmem_free(sense, BUFLEN_MODE_CDROM_CAP); 28036 } 28037 if (com) { 28038 kmem_free(com, sizeof (*com)); 28039 } 28040 return (rval); 28041 } 28042 28043 28044 /* 28045 * Function: sr_pause_resume() 28046 * 28047 * Description: This routine is the driver entry point for handling CD-ROM 28048 * pause/resume ioctl requests. This only affects the audio play 28049 * operation. 28050 * 28051 * Arguments: dev - the device 'dev_t' 28052 * cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used 28053 * for setting the resume bit of the cdb. 28054 * 28055 * Return Code: the code returned by sd_send_scsi_cmd() 28056 * EINVAL if invalid mode specified 28057 * 28058 */ 28059 28060 static int 28061 sr_pause_resume(dev_t dev, int cmd) 28062 { 28063 struct sd_lun *un; 28064 struct uscsi_cmd *com; 28065 char cdb[CDB_GROUP1]; 28066 int rval; 28067 28068 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28069 return (ENXIO); 28070 } 28071 28072 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28073 bzero(cdb, CDB_GROUP1); 28074 cdb[0] = SCMD_PAUSE_RESUME; 28075 switch (cmd) { 28076 case CDROMRESUME: 28077 cdb[8] = 1; 28078 break; 28079 case CDROMPAUSE: 28080 cdb[8] = 0; 28081 break; 28082 default: 28083 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:" 28084 " Command '%x' Not Supported\n", cmd); 28085 rval = EINVAL; 28086 goto done; 28087 } 28088 28089 com->uscsi_cdb = cdb; 28090 com->uscsi_cdblen = CDB_GROUP1; 28091 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28092 28093 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28094 UIO_SYSSPACE, SD_PATH_STANDARD); 28095 28096 done: 28097 kmem_free(com, sizeof (*com)); 28098 return (rval); 28099 } 28100 28101 28102 /* 28103 * Function: sr_play_msf() 28104 * 28105 * Description: This routine is the driver entry point for handling CD-ROM 28106 * ioctl requests to output the audio signals at the specified 28107 * starting address and continue the audio play until the specified 28108 * ending address (CDROMPLAYMSF) The address is in Minute Second 28109 * Frame (MSF) format. 28110 * 28111 * Arguments: dev - the device 'dev_t' 28112 * data - pointer to user provided audio msf structure, 28113 * specifying start/end addresses. 28114 * flag - this argument is a pass through to ddi_copyxxx() 28115 * directly from the mode argument of ioctl(). 28116 * 28117 * Return Code: the code returned by sd_send_scsi_cmd() 28118 * EFAULT if ddi_copyxxx() fails 28119 * ENXIO if fail ddi_get_soft_state 28120 * EINVAL if data pointer is NULL 28121 */ 28122 28123 static int 28124 sr_play_msf(dev_t dev, caddr_t data, int flag) 28125 { 28126 struct sd_lun *un; 28127 struct uscsi_cmd *com; 28128 struct cdrom_msf msf_struct; 28129 struct cdrom_msf *msf = &msf_struct; 28130 char cdb[CDB_GROUP1]; 28131 int rval; 28132 28133 if (data == NULL) { 28134 return (EINVAL); 28135 } 28136 28137 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28138 return (ENXIO); 28139 } 28140 28141 if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) { 28142 return (EFAULT); 28143 } 28144 28145 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28146 bzero(cdb, CDB_GROUP1); 28147 cdb[0] = SCMD_PLAYAUDIO_MSF; 28148 if (un->un_f_cfg_playmsf_bcd == TRUE) { 28149 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0); 28150 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0); 28151 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0); 28152 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1); 28153 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1); 28154 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1); 28155 } else { 28156 cdb[3] = msf->cdmsf_min0; 28157 cdb[4] = msf->cdmsf_sec0; 28158 cdb[5] = msf->cdmsf_frame0; 28159 cdb[6] = msf->cdmsf_min1; 28160 cdb[7] = msf->cdmsf_sec1; 28161 cdb[8] = msf->cdmsf_frame1; 28162 } 28163 com->uscsi_cdb = cdb; 28164 com->uscsi_cdblen = CDB_GROUP1; 28165 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28166 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28167 UIO_SYSSPACE, SD_PATH_STANDARD); 28168 kmem_free(com, sizeof (*com)); 28169 return (rval); 28170 } 28171 28172 28173 /* 28174 * Function: sr_play_trkind() 28175 * 28176 * Description: This routine is the driver entry point for handling CD-ROM 28177 * ioctl requests to output the audio signals at the specified 28178 * starting address and continue the audio play until the specified 28179 * ending address (CDROMPLAYTRKIND). The address is in Track Index 28180 * format. 28181 * 28182 * Arguments: dev - the device 'dev_t' 28183 * data - pointer to user provided audio track/index structure, 28184 * specifying start/end addresses. 28185 * flag - this argument is a pass through to ddi_copyxxx() 28186 * directly from the mode argument of ioctl(). 28187 * 28188 * Return Code: the code returned by sd_send_scsi_cmd() 28189 * EFAULT if ddi_copyxxx() fails 28190 * ENXIO if fail ddi_get_soft_state 28191 * EINVAL if data pointer is NULL 28192 */ 28193 28194 static int 28195 sr_play_trkind(dev_t dev, caddr_t data, int flag) 28196 { 28197 struct cdrom_ti ti_struct; 28198 struct cdrom_ti *ti = &ti_struct; 28199 struct uscsi_cmd *com = NULL; 28200 char cdb[CDB_GROUP1]; 28201 int rval; 28202 28203 if (data == NULL) { 28204 return (EINVAL); 28205 } 28206 28207 if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) { 28208 return (EFAULT); 28209 } 28210 28211 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28212 bzero(cdb, CDB_GROUP1); 28213 cdb[0] = SCMD_PLAYAUDIO_TI; 28214 cdb[4] = ti->cdti_trk0; 28215 cdb[5] = ti->cdti_ind0; 28216 cdb[7] = ti->cdti_trk1; 28217 cdb[8] = ti->cdti_ind1; 28218 com->uscsi_cdb = cdb; 28219 com->uscsi_cdblen = CDB_GROUP1; 28220 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT; 28221 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28222 UIO_SYSSPACE, SD_PATH_STANDARD); 28223 kmem_free(com, sizeof (*com)); 28224 return (rval); 28225 } 28226 28227 28228 /* 28229 * Function: sr_read_all_subcodes() 28230 * 28231 * Description: This routine is the driver entry point for handling CD-ROM 28232 * ioctl requests to return raw subcode data while the target is 28233 * playing audio (CDROMSUBCODE). 28234 * 28235 * Arguments: dev - the device 'dev_t' 28236 * data - pointer to user provided cdrom subcode structure, 28237 * specifying the transfer length and address. 28238 * flag - this argument is a pass through to ddi_copyxxx() 28239 * directly from the mode argument of ioctl(). 28240 * 28241 * Return Code: the code returned by sd_send_scsi_cmd() 28242 * EFAULT if ddi_copyxxx() fails 28243 * ENXIO if fail ddi_get_soft_state 28244 * EINVAL if data pointer is NULL 28245 */ 28246 28247 static int 28248 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag) 28249 { 28250 struct sd_lun *un = NULL; 28251 struct uscsi_cmd *com = NULL; 28252 struct cdrom_subcode *subcode = NULL; 28253 int rval; 28254 size_t buflen; 28255 char cdb[CDB_GROUP5]; 28256 28257 #ifdef _MULTI_DATAMODEL 28258 /* To support ILP32 applications in an LP64 world */ 28259 struct cdrom_subcode32 cdrom_subcode32; 28260 struct cdrom_subcode32 *cdsc32 = &cdrom_subcode32; 28261 #endif 28262 if (data == NULL) { 28263 return (EINVAL); 28264 } 28265 28266 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 28267 return (ENXIO); 28268 } 28269 28270 subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP); 28271 28272 #ifdef _MULTI_DATAMODEL 28273 switch (ddi_model_convert_from(flag & FMODELS)) { 28274 case DDI_MODEL_ILP32: 28275 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) { 28276 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28277 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28278 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28279 return (EFAULT); 28280 } 28281 /* Convert the ILP32 uscsi data from the application to LP64 */ 28282 cdrom_subcode32tocdrom_subcode(cdsc32, subcode); 28283 break; 28284 case DDI_MODEL_NONE: 28285 if (ddi_copyin(data, subcode, 28286 sizeof (struct cdrom_subcode), flag)) { 28287 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28288 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28289 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28290 return (EFAULT); 28291 } 28292 break; 28293 } 28294 #else /* ! _MULTI_DATAMODEL */ 28295 if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) { 28296 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28297 "sr_read_all_subcodes: ddi_copyin Failed\n"); 28298 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28299 return (EFAULT); 28300 } 28301 #endif /* _MULTI_DATAMODEL */ 28302 28303 /* 28304 * Since MMC-2 expects max 3 bytes for length, check if the 28305 * length input is greater than 3 bytes 28306 */ 28307 if ((subcode->cdsc_length & 0xFF000000) != 0) { 28308 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 28309 "sr_read_all_subcodes: " 28310 "cdrom transfer length too large: %d (limit %d)\n", 28311 subcode->cdsc_length, 0xFFFFFF); 28312 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28313 return (EINVAL); 28314 } 28315 28316 buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length; 28317 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28318 bzero(cdb, CDB_GROUP5); 28319 28320 if (un->un_f_mmc_cap == TRUE) { 28321 cdb[0] = (char)SCMD_READ_CD; 28322 cdb[2] = (char)0xff; 28323 cdb[3] = (char)0xff; 28324 cdb[4] = (char)0xff; 28325 cdb[5] = (char)0xff; 28326 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28327 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28328 cdb[8] = ((subcode->cdsc_length) & 0x000000ff); 28329 cdb[10] = 1; 28330 } else { 28331 /* 28332 * Note: A vendor specific command (0xDF) is being used her to 28333 * request a read of all subcodes. 28334 */ 28335 cdb[0] = (char)SCMD_READ_ALL_SUBCODES; 28336 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24); 28337 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16); 28338 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8); 28339 cdb[9] = ((subcode->cdsc_length) & 0x000000ff); 28340 } 28341 com->uscsi_cdb = cdb; 28342 com->uscsi_cdblen = CDB_GROUP5; 28343 com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr; 28344 com->uscsi_buflen = buflen; 28345 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28346 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28347 UIO_SYSSPACE, SD_PATH_STANDARD); 28348 kmem_free(subcode, sizeof (struct cdrom_subcode)); 28349 kmem_free(com, sizeof (*com)); 28350 return (rval); 28351 } 28352 28353 28354 /* 28355 * Function: sr_read_subchannel() 28356 * 28357 * Description: This routine is the driver entry point for handling CD-ROM 28358 * ioctl requests to return the Q sub-channel data of the CD 28359 * current position block. (CDROMSUBCHNL) The data includes the 28360 * track number, index number, absolute CD-ROM address (LBA or MSF 28361 * format per the user) , track relative CD-ROM address (LBA or MSF 28362 * format per the user), control data and audio status. 28363 * 28364 * Arguments: dev - the device 'dev_t' 28365 * data - pointer to user provided cdrom sub-channel structure 28366 * flag - this argument is a pass through to ddi_copyxxx() 28367 * directly from the mode argument of ioctl(). 28368 * 28369 * Return Code: the code returned by sd_send_scsi_cmd() 28370 * EFAULT if ddi_copyxxx() fails 28371 * ENXIO if fail ddi_get_soft_state 28372 * EINVAL if data pointer is NULL 28373 */ 28374 28375 static int 28376 sr_read_subchannel(dev_t dev, caddr_t data, int flag) 28377 { 28378 struct sd_lun *un; 28379 struct uscsi_cmd *com; 28380 struct cdrom_subchnl subchanel; 28381 struct cdrom_subchnl *subchnl = &subchanel; 28382 char cdb[CDB_GROUP1]; 28383 caddr_t buffer; 28384 int rval; 28385 28386 if (data == NULL) { 28387 return (EINVAL); 28388 } 28389 28390 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28391 (un->un_state == SD_STATE_OFFLINE)) { 28392 return (ENXIO); 28393 } 28394 28395 if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) { 28396 return (EFAULT); 28397 } 28398 28399 buffer = kmem_zalloc((size_t)16, KM_SLEEP); 28400 bzero(cdb, CDB_GROUP1); 28401 cdb[0] = SCMD_READ_SUBCHANNEL; 28402 /* Set the MSF bit based on the user requested address format */ 28403 cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02; 28404 /* 28405 * Set the Q bit in byte 2 to indicate that Q sub-channel data be 28406 * returned 28407 */ 28408 cdb[2] = 0x40; 28409 /* 28410 * Set byte 3 to specify the return data format. A value of 0x01 28411 * indicates that the CD-ROM current position should be returned. 28412 */ 28413 cdb[3] = 0x01; 28414 cdb[8] = 0x10; 28415 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28416 com->uscsi_cdb = cdb; 28417 com->uscsi_cdblen = CDB_GROUP1; 28418 com->uscsi_bufaddr = buffer; 28419 com->uscsi_buflen = 16; 28420 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28421 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28422 UIO_SYSSPACE, SD_PATH_STANDARD); 28423 if (rval != 0) { 28424 kmem_free(buffer, 16); 28425 kmem_free(com, sizeof (*com)); 28426 return (rval); 28427 } 28428 28429 /* Process the returned Q sub-channel data */ 28430 subchnl->cdsc_audiostatus = buffer[1]; 28431 subchnl->cdsc_adr = (buffer[5] & 0xF0); 28432 subchnl->cdsc_ctrl = (buffer[5] & 0x0F); 28433 subchnl->cdsc_trk = buffer[6]; 28434 subchnl->cdsc_ind = buffer[7]; 28435 if (subchnl->cdsc_format & CDROM_LBA) { 28436 subchnl->cdsc_absaddr.lba = 28437 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28438 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28439 subchnl->cdsc_reladdr.lba = 28440 ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) + 28441 ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]); 28442 } else if (un->un_f_cfg_readsub_bcd == TRUE) { 28443 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]); 28444 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]); 28445 subchnl->cdsc_absaddr.msf.frame = BCD_TO_BYTE(buffer[11]); 28446 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]); 28447 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]); 28448 subchnl->cdsc_reladdr.msf.frame = BCD_TO_BYTE(buffer[15]); 28449 } else { 28450 subchnl->cdsc_absaddr.msf.minute = buffer[9]; 28451 subchnl->cdsc_absaddr.msf.second = buffer[10]; 28452 subchnl->cdsc_absaddr.msf.frame = buffer[11]; 28453 subchnl->cdsc_reladdr.msf.minute = buffer[13]; 28454 subchnl->cdsc_reladdr.msf.second = buffer[14]; 28455 subchnl->cdsc_reladdr.msf.frame = buffer[15]; 28456 } 28457 kmem_free(buffer, 16); 28458 kmem_free(com, sizeof (*com)); 28459 if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag) 28460 != 0) { 28461 return (EFAULT); 28462 } 28463 return (rval); 28464 } 28465 28466 28467 /* 28468 * Function: sr_read_tocentry() 28469 * 28470 * Description: This routine is the driver entry point for handling CD-ROM 28471 * ioctl requests to read from the Table of Contents (TOC) 28472 * (CDROMREADTOCENTRY). This routine provides the ADR and CTRL 28473 * fields, the starting address (LBA or MSF format per the user) 28474 * and the data mode if the user specified track is a data track. 28475 * 28476 * Note: The READ HEADER (0x44) command used in this routine is 28477 * obsolete per the SCSI MMC spec but still supported in the 28478 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI 28479 * therefore the command is still implemented in this routine. 28480 * 28481 * Arguments: dev - the device 'dev_t' 28482 * data - pointer to user provided toc entry structure, 28483 * specifying the track # and the address format 28484 * (LBA or MSF). 28485 * flag - this argument is a pass through to ddi_copyxxx() 28486 * directly from the mode argument of ioctl(). 28487 * 28488 * Return Code: the code returned by sd_send_scsi_cmd() 28489 * EFAULT if ddi_copyxxx() fails 28490 * ENXIO if fail ddi_get_soft_state 28491 * EINVAL if data pointer is NULL 28492 */ 28493 28494 static int 28495 sr_read_tocentry(dev_t dev, caddr_t data, int flag) 28496 { 28497 struct sd_lun *un = NULL; 28498 struct uscsi_cmd *com; 28499 struct cdrom_tocentry toc_entry; 28500 struct cdrom_tocentry *entry = &toc_entry; 28501 caddr_t buffer; 28502 int rval; 28503 char cdb[CDB_GROUP1]; 28504 28505 if (data == NULL) { 28506 return (EINVAL); 28507 } 28508 28509 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28510 (un->un_state == SD_STATE_OFFLINE)) { 28511 return (ENXIO); 28512 } 28513 28514 if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) { 28515 return (EFAULT); 28516 } 28517 28518 /* Validate the requested track and address format */ 28519 if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) { 28520 return (EINVAL); 28521 } 28522 28523 if (entry->cdte_track == 0) { 28524 return (EINVAL); 28525 } 28526 28527 buffer = kmem_zalloc((size_t)12, KM_SLEEP); 28528 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28529 bzero(cdb, CDB_GROUP1); 28530 28531 cdb[0] = SCMD_READ_TOC; 28532 /* Set the MSF bit based on the user requested address format */ 28533 cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2); 28534 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28535 cdb[6] = BYTE_TO_BCD(entry->cdte_track); 28536 } else { 28537 cdb[6] = entry->cdte_track; 28538 } 28539 28540 /* 28541 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 28542 * (4 byte TOC response header + 8 byte track descriptor) 28543 */ 28544 cdb[8] = 12; 28545 com->uscsi_cdb = cdb; 28546 com->uscsi_cdblen = CDB_GROUP1; 28547 com->uscsi_bufaddr = buffer; 28548 com->uscsi_buflen = 0x0C; 28549 com->uscsi_flags = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ); 28550 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28551 UIO_SYSSPACE, SD_PATH_STANDARD); 28552 if (rval != 0) { 28553 kmem_free(buffer, 12); 28554 kmem_free(com, sizeof (*com)); 28555 return (rval); 28556 } 28557 28558 /* Process the toc entry */ 28559 entry->cdte_adr = (buffer[5] & 0xF0) >> 4; 28560 entry->cdte_ctrl = (buffer[5] & 0x0F); 28561 if (entry->cdte_format & CDROM_LBA) { 28562 entry->cdte_addr.lba = 28563 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 28564 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 28565 } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) { 28566 entry->cdte_addr.msf.minute = BCD_TO_BYTE(buffer[9]); 28567 entry->cdte_addr.msf.second = BCD_TO_BYTE(buffer[10]); 28568 entry->cdte_addr.msf.frame = BCD_TO_BYTE(buffer[11]); 28569 /* 28570 * Send a READ TOC command using the LBA address format to get 28571 * the LBA for the track requested so it can be used in the 28572 * READ HEADER request 28573 * 28574 * Note: The MSF bit of the READ HEADER command specifies the 28575 * output format. The block address specified in that command 28576 * must be in LBA format. 28577 */ 28578 cdb[1] = 0; 28579 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28580 UIO_SYSSPACE, SD_PATH_STANDARD); 28581 if (rval != 0) { 28582 kmem_free(buffer, 12); 28583 kmem_free(com, sizeof (*com)); 28584 return (rval); 28585 } 28586 } else { 28587 entry->cdte_addr.msf.minute = buffer[9]; 28588 entry->cdte_addr.msf.second = buffer[10]; 28589 entry->cdte_addr.msf.frame = buffer[11]; 28590 /* 28591 * Send a READ TOC command using the LBA address format to get 28592 * the LBA for the track requested so it can be used in the 28593 * READ HEADER request 28594 * 28595 * Note: The MSF bit of the READ HEADER command specifies the 28596 * output format. The block address specified in that command 28597 * must be in LBA format. 28598 */ 28599 cdb[1] = 0; 28600 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28601 UIO_SYSSPACE, SD_PATH_STANDARD); 28602 if (rval != 0) { 28603 kmem_free(buffer, 12); 28604 kmem_free(com, sizeof (*com)); 28605 return (rval); 28606 } 28607 } 28608 28609 /* 28610 * Build and send the READ HEADER command to determine the data mode of 28611 * the user specified track. 28612 */ 28613 if ((entry->cdte_ctrl & CDROM_DATA_TRACK) && 28614 (entry->cdte_track != CDROM_LEADOUT)) { 28615 bzero(cdb, CDB_GROUP1); 28616 cdb[0] = SCMD_READ_HEADER; 28617 cdb[2] = buffer[8]; 28618 cdb[3] = buffer[9]; 28619 cdb[4] = buffer[10]; 28620 cdb[5] = buffer[11]; 28621 cdb[8] = 0x08; 28622 com->uscsi_buflen = 0x08; 28623 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28624 UIO_SYSSPACE, SD_PATH_STANDARD); 28625 if (rval == 0) { 28626 entry->cdte_datamode = buffer[0]; 28627 } else { 28628 /* 28629 * READ HEADER command failed, since this is 28630 * obsoleted in one spec, its better to return 28631 * -1 for an invlid track so that we can still 28632 * recieve the rest of the TOC data. 28633 */ 28634 entry->cdte_datamode = (uchar_t)-1; 28635 } 28636 } else { 28637 entry->cdte_datamode = (uchar_t)-1; 28638 } 28639 28640 kmem_free(buffer, 12); 28641 kmem_free(com, sizeof (*com)); 28642 if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0) 28643 return (EFAULT); 28644 28645 return (rval); 28646 } 28647 28648 28649 /* 28650 * Function: sr_read_tochdr() 28651 * 28652 * Description: This routine is the driver entry point for handling CD-ROM 28653 * ioctl requests to read the Table of Contents (TOC) header 28654 * (CDROMREADTOHDR). The TOC header consists of the disk starting 28655 * and ending track numbers 28656 * 28657 * Arguments: dev - the device 'dev_t' 28658 * data - pointer to user provided toc header structure, 28659 * specifying the starting and ending track numbers. 28660 * flag - this argument is a pass through to ddi_copyxxx() 28661 * directly from the mode argument of ioctl(). 28662 * 28663 * Return Code: the code returned by sd_send_scsi_cmd() 28664 * EFAULT if ddi_copyxxx() fails 28665 * ENXIO if fail ddi_get_soft_state 28666 * EINVAL if data pointer is NULL 28667 */ 28668 28669 static int 28670 sr_read_tochdr(dev_t dev, caddr_t data, int flag) 28671 { 28672 struct sd_lun *un; 28673 struct uscsi_cmd *com; 28674 struct cdrom_tochdr toc_header; 28675 struct cdrom_tochdr *hdr = &toc_header; 28676 char cdb[CDB_GROUP1]; 28677 int rval; 28678 caddr_t buffer; 28679 28680 if (data == NULL) { 28681 return (EINVAL); 28682 } 28683 28684 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28685 (un->un_state == SD_STATE_OFFLINE)) { 28686 return (ENXIO); 28687 } 28688 28689 buffer = kmem_zalloc(4, KM_SLEEP); 28690 bzero(cdb, CDB_GROUP1); 28691 cdb[0] = SCMD_READ_TOC; 28692 /* 28693 * Specifying a track number of 0x00 in the READ TOC command indicates 28694 * that the TOC header should be returned 28695 */ 28696 cdb[6] = 0x00; 28697 /* 28698 * Bytes 7 & 8 are the 4 byte allocation length for TOC header. 28699 * (2 byte data len + 1 byte starting track # + 1 byte ending track #) 28700 */ 28701 cdb[8] = 0x04; 28702 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28703 com->uscsi_cdb = cdb; 28704 com->uscsi_cdblen = CDB_GROUP1; 28705 com->uscsi_bufaddr = buffer; 28706 com->uscsi_buflen = 0x04; 28707 com->uscsi_timeout = 300; 28708 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28709 28710 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 28711 UIO_SYSSPACE, SD_PATH_STANDARD); 28712 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) { 28713 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]); 28714 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]); 28715 } else { 28716 hdr->cdth_trk0 = buffer[2]; 28717 hdr->cdth_trk1 = buffer[3]; 28718 } 28719 kmem_free(buffer, 4); 28720 kmem_free(com, sizeof (*com)); 28721 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) { 28722 return (EFAULT); 28723 } 28724 return (rval); 28725 } 28726 28727 28728 /* 28729 * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(), 28730 * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for 28731 * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data, 28732 * digital audio and extended architecture digital audio. These modes are 28733 * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3 28734 * MMC specs. 28735 * 28736 * In addition to support for the various data formats these routines also 28737 * include support for devices that implement only the direct access READ 28738 * commands (0x08, 0x28), devices that implement the READ_CD commands 28739 * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and 28740 * READ CDXA commands (0xD8, 0xDB) 28741 */ 28742 28743 /* 28744 * Function: sr_read_mode1() 28745 * 28746 * Description: This routine is the driver entry point for handling CD-ROM 28747 * ioctl read mode1 requests (CDROMREADMODE1). 28748 * 28749 * Arguments: dev - the device 'dev_t' 28750 * data - pointer to user provided cd read structure specifying 28751 * the lba buffer address and length. 28752 * flag - this argument is a pass through to ddi_copyxxx() 28753 * directly from the mode argument of ioctl(). 28754 * 28755 * Return Code: the code returned by sd_send_scsi_cmd() 28756 * EFAULT if ddi_copyxxx() fails 28757 * ENXIO if fail ddi_get_soft_state 28758 * EINVAL if data pointer is NULL 28759 */ 28760 28761 static int 28762 sr_read_mode1(dev_t dev, caddr_t data, int flag) 28763 { 28764 struct sd_lun *un; 28765 struct cdrom_read mode1_struct; 28766 struct cdrom_read *mode1 = &mode1_struct; 28767 int rval; 28768 #ifdef _MULTI_DATAMODEL 28769 /* To support ILP32 applications in an LP64 world */ 28770 struct cdrom_read32 cdrom_read32; 28771 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28772 #endif /* _MULTI_DATAMODEL */ 28773 28774 if (data == NULL) { 28775 return (EINVAL); 28776 } 28777 28778 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28779 (un->un_state == SD_STATE_OFFLINE)) { 28780 return (ENXIO); 28781 } 28782 28783 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28784 "sd_read_mode1: entry: un:0x%p\n", un); 28785 28786 #ifdef _MULTI_DATAMODEL 28787 switch (ddi_model_convert_from(flag & FMODELS)) { 28788 case DDI_MODEL_ILP32: 28789 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28790 return (EFAULT); 28791 } 28792 /* Convert the ILP32 uscsi data from the application to LP64 */ 28793 cdrom_read32tocdrom_read(cdrd32, mode1); 28794 break; 28795 case DDI_MODEL_NONE: 28796 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28797 return (EFAULT); 28798 } 28799 } 28800 #else /* ! _MULTI_DATAMODEL */ 28801 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) { 28802 return (EFAULT); 28803 } 28804 #endif /* _MULTI_DATAMODEL */ 28805 28806 rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr, 28807 mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD); 28808 28809 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28810 "sd_read_mode1: exit: un:0x%p\n", un); 28811 28812 return (rval); 28813 } 28814 28815 28816 /* 28817 * Function: sr_read_cd_mode2() 28818 * 28819 * Description: This routine is the driver entry point for handling CD-ROM 28820 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28821 * support the READ CD (0xBE) command or the 1st generation 28822 * READ CD (0xD4) command. 28823 * 28824 * Arguments: dev - the device 'dev_t' 28825 * data - pointer to user provided cd read structure specifying 28826 * the lba buffer address and length. 28827 * flag - this argument is a pass through to ddi_copyxxx() 28828 * directly from the mode argument of ioctl(). 28829 * 28830 * Return Code: the code returned by sd_send_scsi_cmd() 28831 * EFAULT if ddi_copyxxx() fails 28832 * ENXIO if fail ddi_get_soft_state 28833 * EINVAL if data pointer is NULL 28834 */ 28835 28836 static int 28837 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag) 28838 { 28839 struct sd_lun *un; 28840 struct uscsi_cmd *com; 28841 struct cdrom_read mode2_struct; 28842 struct cdrom_read *mode2 = &mode2_struct; 28843 uchar_t cdb[CDB_GROUP5]; 28844 int nblocks; 28845 int rval; 28846 #ifdef _MULTI_DATAMODEL 28847 /* To support ILP32 applications in an LP64 world */ 28848 struct cdrom_read32 cdrom_read32; 28849 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28850 #endif /* _MULTI_DATAMODEL */ 28851 28852 if (data == NULL) { 28853 return (EINVAL); 28854 } 28855 28856 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28857 (un->un_state == SD_STATE_OFFLINE)) { 28858 return (ENXIO); 28859 } 28860 28861 #ifdef _MULTI_DATAMODEL 28862 switch (ddi_model_convert_from(flag & FMODELS)) { 28863 case DDI_MODEL_ILP32: 28864 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28865 return (EFAULT); 28866 } 28867 /* Convert the ILP32 uscsi data from the application to LP64 */ 28868 cdrom_read32tocdrom_read(cdrd32, mode2); 28869 break; 28870 case DDI_MODEL_NONE: 28871 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28872 return (EFAULT); 28873 } 28874 break; 28875 } 28876 28877 #else /* ! _MULTI_DATAMODEL */ 28878 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 28879 return (EFAULT); 28880 } 28881 #endif /* _MULTI_DATAMODEL */ 28882 28883 bzero(cdb, sizeof (cdb)); 28884 if (un->un_f_cfg_read_cd_xd4 == TRUE) { 28885 /* Read command supported by 1st generation atapi drives */ 28886 cdb[0] = SCMD_READ_CDD4; 28887 } else { 28888 /* Universal CD Access Command */ 28889 cdb[0] = SCMD_READ_CD; 28890 } 28891 28892 /* 28893 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book 28894 */ 28895 cdb[1] = CDROM_SECTOR_TYPE_MODE2; 28896 28897 /* set the start address */ 28898 cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF); 28899 cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF); 28900 cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 28901 cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF); 28902 28903 /* set the transfer length */ 28904 nblocks = mode2->cdread_buflen / 2336; 28905 cdb[6] = (uchar_t)(nblocks >> 16); 28906 cdb[7] = (uchar_t)(nblocks >> 8); 28907 cdb[8] = (uchar_t)nblocks; 28908 28909 /* set the filter bits */ 28910 cdb[9] = CDROM_READ_CD_USERDATA; 28911 28912 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 28913 com->uscsi_cdb = (caddr_t)cdb; 28914 com->uscsi_cdblen = sizeof (cdb); 28915 com->uscsi_bufaddr = mode2->cdread_bufaddr; 28916 com->uscsi_buflen = mode2->cdread_buflen; 28917 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 28918 28919 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 28920 UIO_SYSSPACE, SD_PATH_STANDARD); 28921 kmem_free(com, sizeof (*com)); 28922 return (rval); 28923 } 28924 28925 28926 /* 28927 * Function: sr_read_mode2() 28928 * 28929 * Description: This routine is the driver entry point for handling CD-ROM 28930 * ioctl read mode2 requests (CDROMREADMODE2) for devices that 28931 * do not support the READ CD (0xBE) command. 28932 * 28933 * Arguments: dev - the device 'dev_t' 28934 * data - pointer to user provided cd read structure specifying 28935 * the lba buffer address and length. 28936 * flag - this argument is a pass through to ddi_copyxxx() 28937 * directly from the mode argument of ioctl(). 28938 * 28939 * Return Code: the code returned by sd_send_scsi_cmd() 28940 * EFAULT if ddi_copyxxx() fails 28941 * ENXIO if fail ddi_get_soft_state 28942 * EINVAL if data pointer is NULL 28943 * EIO if fail to reset block size 28944 * EAGAIN if commands are in progress in the driver 28945 */ 28946 28947 static int 28948 sr_read_mode2(dev_t dev, caddr_t data, int flag) 28949 { 28950 struct sd_lun *un; 28951 struct cdrom_read mode2_struct; 28952 struct cdrom_read *mode2 = &mode2_struct; 28953 int rval; 28954 uint32_t restore_blksize; 28955 struct uscsi_cmd *com; 28956 uchar_t cdb[CDB_GROUP0]; 28957 int nblocks; 28958 28959 #ifdef _MULTI_DATAMODEL 28960 /* To support ILP32 applications in an LP64 world */ 28961 struct cdrom_read32 cdrom_read32; 28962 struct cdrom_read32 *cdrd32 = &cdrom_read32; 28963 #endif /* _MULTI_DATAMODEL */ 28964 28965 if (data == NULL) { 28966 return (EINVAL); 28967 } 28968 28969 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 28970 (un->un_state == SD_STATE_OFFLINE)) { 28971 return (ENXIO); 28972 } 28973 28974 /* 28975 * Because this routine will update the device and driver block size 28976 * being used we want to make sure there are no commands in progress. 28977 * If commands are in progress the user will have to try again. 28978 * 28979 * We check for 1 instead of 0 because we increment un_ncmds_in_driver 28980 * in sdioctl to protect commands from sdioctl through to the top of 28981 * sd_uscsi_strategy. See sdioctl for details. 28982 */ 28983 mutex_enter(SD_MUTEX(un)); 28984 if (un->un_ncmds_in_driver != 1) { 28985 mutex_exit(SD_MUTEX(un)); 28986 return (EAGAIN); 28987 } 28988 mutex_exit(SD_MUTEX(un)); 28989 28990 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 28991 "sd_read_mode2: entry: un:0x%p\n", un); 28992 28993 #ifdef _MULTI_DATAMODEL 28994 switch (ddi_model_convert_from(flag & FMODELS)) { 28995 case DDI_MODEL_ILP32: 28996 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) { 28997 return (EFAULT); 28998 } 28999 /* Convert the ILP32 uscsi data from the application to LP64 */ 29000 cdrom_read32tocdrom_read(cdrd32, mode2); 29001 break; 29002 case DDI_MODEL_NONE: 29003 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) { 29004 return (EFAULT); 29005 } 29006 break; 29007 } 29008 #else /* ! _MULTI_DATAMODEL */ 29009 if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) { 29010 return (EFAULT); 29011 } 29012 #endif /* _MULTI_DATAMODEL */ 29013 29014 /* Store the current target block size for restoration later */ 29015 restore_blksize = un->un_tgt_blocksize; 29016 29017 /* Change the device and soft state target block size to 2336 */ 29018 if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) { 29019 rval = EIO; 29020 goto done; 29021 } 29022 29023 29024 bzero(cdb, sizeof (cdb)); 29025 29026 /* set READ operation */ 29027 cdb[0] = SCMD_READ; 29028 29029 /* adjust lba for 2kbyte blocks from 512 byte blocks */ 29030 mode2->cdread_lba >>= 2; 29031 29032 /* set the start address */ 29033 cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F); 29034 cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF); 29035 cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF); 29036 29037 /* set the transfer length */ 29038 nblocks = mode2->cdread_buflen / 2336; 29039 cdb[4] = (uchar_t)nblocks & 0xFF; 29040 29041 /* build command */ 29042 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29043 com->uscsi_cdb = (caddr_t)cdb; 29044 com->uscsi_cdblen = sizeof (cdb); 29045 com->uscsi_bufaddr = mode2->cdread_bufaddr; 29046 com->uscsi_buflen = mode2->cdread_buflen; 29047 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29048 29049 /* 29050 * Issue SCSI command with user space address for read buffer. 29051 * 29052 * This sends the command through main channel in the driver. 29053 * 29054 * Since this is accessed via an IOCTL call, we go through the 29055 * standard path, so that if the device was powered down, then 29056 * it would be 'awakened' to handle the command. 29057 */ 29058 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29059 UIO_SYSSPACE, SD_PATH_STANDARD); 29060 29061 kmem_free(com, sizeof (*com)); 29062 29063 /* Restore the device and soft state target block size */ 29064 if (sr_sector_mode(dev, restore_blksize) != 0) { 29065 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29066 "can't do switch back to mode 1\n"); 29067 /* 29068 * If sd_send_scsi_READ succeeded we still need to report 29069 * an error because we failed to reset the block size 29070 */ 29071 if (rval == 0) { 29072 rval = EIO; 29073 } 29074 } 29075 29076 done: 29077 SD_TRACE(SD_LOG_ATTACH_DETACH, un, 29078 "sd_read_mode2: exit: un:0x%p\n", un); 29079 29080 return (rval); 29081 } 29082 29083 29084 /* 29085 * Function: sr_sector_mode() 29086 * 29087 * Description: This utility function is used by sr_read_mode2 to set the target 29088 * block size based on the user specified size. This is a legacy 29089 * implementation based upon a vendor specific mode page 29090 * 29091 * Arguments: dev - the device 'dev_t' 29092 * data - flag indicating if block size is being set to 2336 or 29093 * 512. 29094 * 29095 * Return Code: the code returned by sd_send_scsi_cmd() 29096 * EFAULT if ddi_copyxxx() fails 29097 * ENXIO if fail ddi_get_soft_state 29098 * EINVAL if data pointer is NULL 29099 */ 29100 29101 static int 29102 sr_sector_mode(dev_t dev, uint32_t blksize) 29103 { 29104 struct sd_lun *un; 29105 uchar_t *sense; 29106 uchar_t *select; 29107 int rval; 29108 29109 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29110 (un->un_state == SD_STATE_OFFLINE)) { 29111 return (ENXIO); 29112 } 29113 29114 sense = kmem_zalloc(20, KM_SLEEP); 29115 29116 /* Note: This is a vendor specific mode page (0x81) */ 29117 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81, 29118 SD_PATH_STANDARD)) != 0) { 29119 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29120 "sr_sector_mode: Mode Sense failed\n"); 29121 kmem_free(sense, 20); 29122 return (rval); 29123 } 29124 select = kmem_zalloc(20, KM_SLEEP); 29125 select[3] = 0x08; 29126 select[10] = ((blksize >> 8) & 0xff); 29127 select[11] = (blksize & 0xff); 29128 select[12] = 0x01; 29129 select[13] = 0x06; 29130 select[14] = sense[14]; 29131 select[15] = sense[15]; 29132 if (blksize == SD_MODE2_BLKSIZE) { 29133 select[14] |= 0x01; 29134 } 29135 29136 if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20, 29137 SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) { 29138 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29139 "sr_sector_mode: Mode Select failed\n"); 29140 } else { 29141 /* 29142 * Only update the softstate block size if we successfully 29143 * changed the device block mode. 29144 */ 29145 mutex_enter(SD_MUTEX(un)); 29146 sd_update_block_info(un, blksize, 0); 29147 mutex_exit(SD_MUTEX(un)); 29148 } 29149 kmem_free(sense, 20); 29150 kmem_free(select, 20); 29151 return (rval); 29152 } 29153 29154 29155 /* 29156 * Function: sr_read_cdda() 29157 * 29158 * Description: This routine is the driver entry point for handling CD-ROM 29159 * ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If 29160 * the target supports CDDA these requests are handled via a vendor 29161 * specific command (0xD8) If the target does not support CDDA 29162 * these requests are handled via the READ CD command (0xBE). 29163 * 29164 * Arguments: dev - the device 'dev_t' 29165 * data - pointer to user provided CD-DA structure specifying 29166 * the track starting address, transfer length, and 29167 * subcode options. 29168 * flag - this argument is a pass through to ddi_copyxxx() 29169 * directly from the mode argument of ioctl(). 29170 * 29171 * Return Code: the code returned by sd_send_scsi_cmd() 29172 * EFAULT if ddi_copyxxx() fails 29173 * ENXIO if fail ddi_get_soft_state 29174 * EINVAL if invalid arguments are provided 29175 * ENOTTY 29176 */ 29177 29178 static int 29179 sr_read_cdda(dev_t dev, caddr_t data, int flag) 29180 { 29181 struct sd_lun *un; 29182 struct uscsi_cmd *com; 29183 struct cdrom_cdda *cdda; 29184 int rval; 29185 size_t buflen; 29186 char cdb[CDB_GROUP5]; 29187 29188 #ifdef _MULTI_DATAMODEL 29189 /* To support ILP32 applications in an LP64 world */ 29190 struct cdrom_cdda32 cdrom_cdda32; 29191 struct cdrom_cdda32 *cdda32 = &cdrom_cdda32; 29192 #endif /* _MULTI_DATAMODEL */ 29193 29194 if (data == NULL) { 29195 return (EINVAL); 29196 } 29197 29198 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29199 return (ENXIO); 29200 } 29201 29202 cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP); 29203 29204 #ifdef _MULTI_DATAMODEL 29205 switch (ddi_model_convert_from(flag & FMODELS)) { 29206 case DDI_MODEL_ILP32: 29207 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) { 29208 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29209 "sr_read_cdda: ddi_copyin Failed\n"); 29210 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29211 return (EFAULT); 29212 } 29213 /* Convert the ILP32 uscsi data from the application to LP64 */ 29214 cdrom_cdda32tocdrom_cdda(cdda32, cdda); 29215 break; 29216 case DDI_MODEL_NONE: 29217 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29218 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29219 "sr_read_cdda: ddi_copyin Failed\n"); 29220 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29221 return (EFAULT); 29222 } 29223 break; 29224 } 29225 #else /* ! _MULTI_DATAMODEL */ 29226 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) { 29227 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29228 "sr_read_cdda: ddi_copyin Failed\n"); 29229 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29230 return (EFAULT); 29231 } 29232 #endif /* _MULTI_DATAMODEL */ 29233 29234 /* 29235 * Since MMC-2 expects max 3 bytes for length, check if the 29236 * length input is greater than 3 bytes 29237 */ 29238 if ((cdda->cdda_length & 0xFF000000) != 0) { 29239 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: " 29240 "cdrom transfer length too large: %d (limit %d)\n", 29241 cdda->cdda_length, 0xFFFFFF); 29242 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29243 return (EINVAL); 29244 } 29245 29246 switch (cdda->cdda_subcode) { 29247 case CDROM_DA_NO_SUBCODE: 29248 buflen = CDROM_BLK_2352 * cdda->cdda_length; 29249 break; 29250 case CDROM_DA_SUBQ: 29251 buflen = CDROM_BLK_2368 * cdda->cdda_length; 29252 break; 29253 case CDROM_DA_ALL_SUBCODE: 29254 buflen = CDROM_BLK_2448 * cdda->cdda_length; 29255 break; 29256 case CDROM_DA_SUBCODE_ONLY: 29257 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length; 29258 break; 29259 default: 29260 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29261 "sr_read_cdda: Subcode '0x%x' Not Supported\n", 29262 cdda->cdda_subcode); 29263 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29264 return (EINVAL); 29265 } 29266 29267 /* Build and send the command */ 29268 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29269 bzero(cdb, CDB_GROUP5); 29270 29271 if (un->un_f_cfg_cdda == TRUE) { 29272 cdb[0] = (char)SCMD_READ_CD; 29273 cdb[1] = 0x04; 29274 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29275 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29276 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29277 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29278 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29279 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29280 cdb[8] = ((cdda->cdda_length) & 0x000000ff); 29281 cdb[9] = 0x10; 29282 switch (cdda->cdda_subcode) { 29283 case CDROM_DA_NO_SUBCODE : 29284 cdb[10] = 0x0; 29285 break; 29286 case CDROM_DA_SUBQ : 29287 cdb[10] = 0x2; 29288 break; 29289 case CDROM_DA_ALL_SUBCODE : 29290 cdb[10] = 0x1; 29291 break; 29292 case CDROM_DA_SUBCODE_ONLY : 29293 /* FALLTHROUGH */ 29294 default : 29295 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29296 kmem_free(com, sizeof (*com)); 29297 return (ENOTTY); 29298 } 29299 } else { 29300 cdb[0] = (char)SCMD_READ_CDDA; 29301 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24); 29302 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16); 29303 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8); 29304 cdb[5] = ((cdda->cdda_addr) & 0x000000ff); 29305 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24); 29306 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16); 29307 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8); 29308 cdb[9] = ((cdda->cdda_length) & 0x000000ff); 29309 cdb[10] = cdda->cdda_subcode; 29310 } 29311 29312 com->uscsi_cdb = cdb; 29313 com->uscsi_cdblen = CDB_GROUP5; 29314 com->uscsi_bufaddr = (caddr_t)cdda->cdda_data; 29315 com->uscsi_buflen = buflen; 29316 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29317 29318 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29319 UIO_SYSSPACE, SD_PATH_STANDARD); 29320 29321 kmem_free(cdda, sizeof (struct cdrom_cdda)); 29322 kmem_free(com, sizeof (*com)); 29323 return (rval); 29324 } 29325 29326 29327 /* 29328 * Function: sr_read_cdxa() 29329 * 29330 * Description: This routine is the driver entry point for handling CD-ROM 29331 * ioctl requests to return CD-XA (Extended Architecture) data. 29332 * (CDROMCDXA). 29333 * 29334 * Arguments: dev - the device 'dev_t' 29335 * data - pointer to user provided CD-XA structure specifying 29336 * the data starting address, transfer length, and format 29337 * flag - this argument is a pass through to ddi_copyxxx() 29338 * directly from the mode argument of ioctl(). 29339 * 29340 * Return Code: the code returned by sd_send_scsi_cmd() 29341 * EFAULT if ddi_copyxxx() fails 29342 * ENXIO if fail ddi_get_soft_state 29343 * EINVAL if data pointer is NULL 29344 */ 29345 29346 static int 29347 sr_read_cdxa(dev_t dev, caddr_t data, int flag) 29348 { 29349 struct sd_lun *un; 29350 struct uscsi_cmd *com; 29351 struct cdrom_cdxa *cdxa; 29352 int rval; 29353 size_t buflen; 29354 char cdb[CDB_GROUP5]; 29355 uchar_t read_flags; 29356 29357 #ifdef _MULTI_DATAMODEL 29358 /* To support ILP32 applications in an LP64 world */ 29359 struct cdrom_cdxa32 cdrom_cdxa32; 29360 struct cdrom_cdxa32 *cdxa32 = &cdrom_cdxa32; 29361 #endif /* _MULTI_DATAMODEL */ 29362 29363 if (data == NULL) { 29364 return (EINVAL); 29365 } 29366 29367 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29368 return (ENXIO); 29369 } 29370 29371 cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP); 29372 29373 #ifdef _MULTI_DATAMODEL 29374 switch (ddi_model_convert_from(flag & FMODELS)) { 29375 case DDI_MODEL_ILP32: 29376 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) { 29377 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29378 return (EFAULT); 29379 } 29380 /* 29381 * Convert the ILP32 uscsi data from the 29382 * application to LP64 for internal use. 29383 */ 29384 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa); 29385 break; 29386 case DDI_MODEL_NONE: 29387 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29388 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29389 return (EFAULT); 29390 } 29391 break; 29392 } 29393 #else /* ! _MULTI_DATAMODEL */ 29394 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) { 29395 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29396 return (EFAULT); 29397 } 29398 #endif /* _MULTI_DATAMODEL */ 29399 29400 /* 29401 * Since MMC-2 expects max 3 bytes for length, check if the 29402 * length input is greater than 3 bytes 29403 */ 29404 if ((cdxa->cdxa_length & 0xFF000000) != 0) { 29405 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: " 29406 "cdrom transfer length too large: %d (limit %d)\n", 29407 cdxa->cdxa_length, 0xFFFFFF); 29408 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29409 return (EINVAL); 29410 } 29411 29412 switch (cdxa->cdxa_format) { 29413 case CDROM_XA_DATA: 29414 buflen = CDROM_BLK_2048 * cdxa->cdxa_length; 29415 read_flags = 0x10; 29416 break; 29417 case CDROM_XA_SECTOR_DATA: 29418 buflen = CDROM_BLK_2352 * cdxa->cdxa_length; 29419 read_flags = 0xf8; 29420 break; 29421 case CDROM_XA_DATA_W_ERROR: 29422 buflen = CDROM_BLK_2646 * cdxa->cdxa_length; 29423 read_flags = 0xfc; 29424 break; 29425 default: 29426 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29427 "sr_read_cdxa: Format '0x%x' Not Supported\n", 29428 cdxa->cdxa_format); 29429 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29430 return (EINVAL); 29431 } 29432 29433 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29434 bzero(cdb, CDB_GROUP5); 29435 if (un->un_f_mmc_cap == TRUE) { 29436 cdb[0] = (char)SCMD_READ_CD; 29437 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29438 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29439 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29440 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29441 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29442 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29443 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff); 29444 cdb[9] = (char)read_flags; 29445 } else { 29446 /* 29447 * Note: A vendor specific command (0xDB) is being used her to 29448 * request a read of all subcodes. 29449 */ 29450 cdb[0] = (char)SCMD_READ_CDXA; 29451 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24); 29452 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16); 29453 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8); 29454 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff); 29455 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24); 29456 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16); 29457 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8); 29458 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff); 29459 cdb[10] = cdxa->cdxa_format; 29460 } 29461 com->uscsi_cdb = cdb; 29462 com->uscsi_cdblen = CDB_GROUP5; 29463 com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data; 29464 com->uscsi_buflen = buflen; 29465 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29466 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE, 29467 UIO_SYSSPACE, SD_PATH_STANDARD); 29468 kmem_free(cdxa, sizeof (struct cdrom_cdxa)); 29469 kmem_free(com, sizeof (*com)); 29470 return (rval); 29471 } 29472 29473 29474 /* 29475 * Function: sr_eject() 29476 * 29477 * Description: This routine is the driver entry point for handling CD-ROM 29478 * eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT) 29479 * 29480 * Arguments: dev - the device 'dev_t' 29481 * 29482 * Return Code: the code returned by sd_send_scsi_cmd() 29483 */ 29484 29485 static int 29486 sr_eject(dev_t dev) 29487 { 29488 struct sd_lun *un; 29489 int rval; 29490 29491 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29492 (un->un_state == SD_STATE_OFFLINE)) { 29493 return (ENXIO); 29494 } 29495 if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW, 29496 SD_PATH_STANDARD)) != 0) { 29497 return (rval); 29498 } 29499 29500 rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT, 29501 SD_PATH_STANDARD); 29502 29503 if (rval == 0) { 29504 mutex_enter(SD_MUTEX(un)); 29505 sr_ejected(un); 29506 un->un_mediastate = DKIO_EJECTED; 29507 cv_broadcast(&un->un_state_cv); 29508 mutex_exit(SD_MUTEX(un)); 29509 } 29510 return (rval); 29511 } 29512 29513 29514 /* 29515 * Function: sr_ejected() 29516 * 29517 * Description: This routine updates the soft state structure to invalidate the 29518 * geometry information after the media has been ejected or a 29519 * media eject has been detected. 29520 * 29521 * Arguments: un - driver soft state (unit) structure 29522 */ 29523 29524 static void 29525 sr_ejected(struct sd_lun *un) 29526 { 29527 struct sd_errstats *stp; 29528 29529 ASSERT(un != NULL); 29530 ASSERT(mutex_owned(SD_MUTEX(un))); 29531 29532 un->un_f_blockcount_is_valid = FALSE; 29533 un->un_f_tgt_blocksize_is_valid = FALSE; 29534 un->un_f_geometry_is_valid = FALSE; 29535 29536 if (un->un_errstats != NULL) { 29537 stp = (struct sd_errstats *)un->un_errstats->ks_data; 29538 stp->sd_capacity.value.ui64 = 0; 29539 } 29540 } 29541 29542 29543 /* 29544 * Function: sr_check_wp() 29545 * 29546 * Description: This routine checks the write protection of a removable 29547 * media disk and hotpluggable devices via the write protect bit of 29548 * the Mode Page Header device specific field. Some devices choke 29549 * on unsupported mode page. In order to workaround this issue, 29550 * this routine has been implemented to use 0x3f mode page(request 29551 * for all pages) for all device types. 29552 * 29553 * Arguments: dev - the device 'dev_t' 29554 * 29555 * Return Code: int indicating if the device is write protected (1) or not (0) 29556 * 29557 * Context: Kernel thread. 29558 * 29559 */ 29560 29561 static int 29562 sr_check_wp(dev_t dev) 29563 { 29564 struct sd_lun *un; 29565 uchar_t device_specific; 29566 uchar_t *sense; 29567 int hdrlen; 29568 int rval = FALSE; 29569 29570 /* 29571 * Note: The return codes for this routine should be reworked to 29572 * properly handle the case of a NULL softstate. 29573 */ 29574 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) { 29575 return (FALSE); 29576 } 29577 29578 if (un->un_f_cfg_is_atapi == TRUE) { 29579 /* 29580 * The mode page contents are not required; set the allocation 29581 * length for the mode page header only 29582 */ 29583 hdrlen = MODE_HEADER_LENGTH_GRP2; 29584 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29585 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen, 29586 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29587 goto err_exit; 29588 device_specific = 29589 ((struct mode_header_grp2 *)sense)->device_specific; 29590 } else { 29591 hdrlen = MODE_HEADER_LENGTH; 29592 sense = kmem_zalloc(hdrlen, KM_SLEEP); 29593 if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen, 29594 MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0) 29595 goto err_exit; 29596 device_specific = 29597 ((struct mode_header *)sense)->device_specific; 29598 } 29599 29600 /* 29601 * Write protect mode sense failed; not all disks 29602 * understand this query. Return FALSE assuming that 29603 * these devices are not writable. 29604 */ 29605 if (device_specific & WRITE_PROTECT) { 29606 rval = TRUE; 29607 } 29608 29609 err_exit: 29610 kmem_free(sense, hdrlen); 29611 return (rval); 29612 } 29613 29614 /* 29615 * Function: sr_volume_ctrl() 29616 * 29617 * Description: This routine is the driver entry point for handling CD-ROM 29618 * audio output volume ioctl requests. (CDROMVOLCTRL) 29619 * 29620 * Arguments: dev - the device 'dev_t' 29621 * data - pointer to user audio volume control structure 29622 * flag - this argument is a pass through to ddi_copyxxx() 29623 * directly from the mode argument of ioctl(). 29624 * 29625 * Return Code: the code returned by sd_send_scsi_cmd() 29626 * EFAULT if ddi_copyxxx() fails 29627 * ENXIO if fail ddi_get_soft_state 29628 * EINVAL if data pointer is NULL 29629 * 29630 */ 29631 29632 static int 29633 sr_volume_ctrl(dev_t dev, caddr_t data, int flag) 29634 { 29635 struct sd_lun *un; 29636 struct cdrom_volctrl volume; 29637 struct cdrom_volctrl *vol = &volume; 29638 uchar_t *sense_page; 29639 uchar_t *select_page; 29640 uchar_t *sense; 29641 uchar_t *select; 29642 int sense_buflen; 29643 int select_buflen; 29644 int rval; 29645 29646 if (data == NULL) { 29647 return (EINVAL); 29648 } 29649 29650 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29651 (un->un_state == SD_STATE_OFFLINE)) { 29652 return (ENXIO); 29653 } 29654 29655 if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) { 29656 return (EFAULT); 29657 } 29658 29659 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29660 struct mode_header_grp2 *sense_mhp; 29661 struct mode_header_grp2 *select_mhp; 29662 int bd_len; 29663 29664 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN; 29665 select_buflen = MODE_HEADER_LENGTH_GRP2 + 29666 MODEPAGE_AUDIO_CTRL_LEN; 29667 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29668 select = kmem_zalloc(select_buflen, KM_SLEEP); 29669 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, 29670 sense_buflen, MODEPAGE_AUDIO_CTRL, 29671 SD_PATH_STANDARD)) != 0) { 29672 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, 29673 "sr_volume_ctrl: Mode Sense Failed\n"); 29674 kmem_free(sense, sense_buflen); 29675 kmem_free(select, select_buflen); 29676 return (rval); 29677 } 29678 sense_mhp = (struct mode_header_grp2 *)sense; 29679 select_mhp = (struct mode_header_grp2 *)select; 29680 bd_len = (sense_mhp->bdesc_length_hi << 8) | 29681 sense_mhp->bdesc_length_lo; 29682 if (bd_len > MODE_BLK_DESC_LENGTH) { 29683 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29684 "sr_volume_ctrl: Mode Sense returned invalid " 29685 "block descriptor length\n"); 29686 kmem_free(sense, sense_buflen); 29687 kmem_free(select, select_buflen); 29688 return (EIO); 29689 } 29690 sense_page = (uchar_t *) 29691 (sense + MODE_HEADER_LENGTH_GRP2 + bd_len); 29692 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2); 29693 select_mhp->length_msb = 0; 29694 select_mhp->length_lsb = 0; 29695 select_mhp->bdesc_length_hi = 0; 29696 select_mhp->bdesc_length_lo = 0; 29697 } else { 29698 struct mode_header *sense_mhp, *select_mhp; 29699 29700 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29701 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN; 29702 sense = kmem_zalloc(sense_buflen, KM_SLEEP); 29703 select = kmem_zalloc(select_buflen, KM_SLEEP); 29704 if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 29705 sense_buflen, MODEPAGE_AUDIO_CTRL, 29706 SD_PATH_STANDARD)) != 0) { 29707 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29708 "sr_volume_ctrl: Mode Sense Failed\n"); 29709 kmem_free(sense, sense_buflen); 29710 kmem_free(select, select_buflen); 29711 return (rval); 29712 } 29713 sense_mhp = (struct mode_header *)sense; 29714 select_mhp = (struct mode_header *)select; 29715 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) { 29716 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 29717 "sr_volume_ctrl: Mode Sense returned invalid " 29718 "block descriptor length\n"); 29719 kmem_free(sense, sense_buflen); 29720 kmem_free(select, select_buflen); 29721 return (EIO); 29722 } 29723 sense_page = (uchar_t *) 29724 (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length); 29725 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH); 29726 select_mhp->length = 0; 29727 select_mhp->bdesc_length = 0; 29728 } 29729 /* 29730 * Note: An audio control data structure could be created and overlayed 29731 * on the following in place of the array indexing method implemented. 29732 */ 29733 29734 /* Build the select data for the user volume data */ 29735 select_page[0] = MODEPAGE_AUDIO_CTRL; 29736 select_page[1] = 0xE; 29737 /* Set the immediate bit */ 29738 select_page[2] = 0x04; 29739 /* Zero out reserved fields */ 29740 select_page[3] = 0x00; 29741 select_page[4] = 0x00; 29742 /* Return sense data for fields not to be modified */ 29743 select_page[5] = sense_page[5]; 29744 select_page[6] = sense_page[6]; 29745 select_page[7] = sense_page[7]; 29746 /* Set the user specified volume levels for channel 0 and 1 */ 29747 select_page[8] = 0x01; 29748 select_page[9] = vol->channel0; 29749 select_page[10] = 0x02; 29750 select_page[11] = vol->channel1; 29751 /* Channel 2 and 3 are currently unsupported so return the sense data */ 29752 select_page[12] = sense_page[12]; 29753 select_page[13] = sense_page[13]; 29754 select_page[14] = sense_page[14]; 29755 select_page[15] = sense_page[15]; 29756 29757 if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) { 29758 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select, 29759 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29760 } else { 29761 rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 29762 select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD); 29763 } 29764 29765 kmem_free(sense, sense_buflen); 29766 kmem_free(select, select_buflen); 29767 return (rval); 29768 } 29769 29770 29771 /* 29772 * Function: sr_read_sony_session_offset() 29773 * 29774 * Description: This routine is the driver entry point for handling CD-ROM 29775 * ioctl requests for session offset information. (CDROMREADOFFSET) 29776 * The address of the first track in the last session of a 29777 * multi-session CD-ROM is returned 29778 * 29779 * Note: This routine uses a vendor specific key value in the 29780 * command control field without implementing any vendor check here 29781 * or in the ioctl routine. 29782 * 29783 * Arguments: dev - the device 'dev_t' 29784 * data - pointer to an int to hold the requested address 29785 * flag - this argument is a pass through to ddi_copyxxx() 29786 * directly from the mode argument of ioctl(). 29787 * 29788 * Return Code: the code returned by sd_send_scsi_cmd() 29789 * EFAULT if ddi_copyxxx() fails 29790 * ENXIO if fail ddi_get_soft_state 29791 * EINVAL if data pointer is NULL 29792 */ 29793 29794 static int 29795 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag) 29796 { 29797 struct sd_lun *un; 29798 struct uscsi_cmd *com; 29799 caddr_t buffer; 29800 char cdb[CDB_GROUP1]; 29801 int session_offset = 0; 29802 int rval; 29803 29804 if (data == NULL) { 29805 return (EINVAL); 29806 } 29807 29808 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL || 29809 (un->un_state == SD_STATE_OFFLINE)) { 29810 return (ENXIO); 29811 } 29812 29813 buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP); 29814 bzero(cdb, CDB_GROUP1); 29815 cdb[0] = SCMD_READ_TOC; 29816 /* 29817 * Bytes 7 & 8 are the 12 byte allocation length for a single entry. 29818 * (4 byte TOC response header + 8 byte response data) 29819 */ 29820 cdb[8] = SONY_SESSION_OFFSET_LEN; 29821 /* Byte 9 is the control byte. A vendor specific value is used */ 29822 cdb[9] = SONY_SESSION_OFFSET_KEY; 29823 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 29824 com->uscsi_cdb = cdb; 29825 com->uscsi_cdblen = CDB_GROUP1; 29826 com->uscsi_bufaddr = buffer; 29827 com->uscsi_buflen = SONY_SESSION_OFFSET_LEN; 29828 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ; 29829 29830 rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 29831 UIO_SYSSPACE, SD_PATH_STANDARD); 29832 if (rval != 0) { 29833 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29834 kmem_free(com, sizeof (*com)); 29835 return (rval); 29836 } 29837 if (buffer[1] == SONY_SESSION_OFFSET_VALID) { 29838 session_offset = 29839 ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) + 29840 ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]); 29841 /* 29842 * Offset returned offset in current lbasize block's. Convert to 29843 * 2k block's to return to the user 29844 */ 29845 if (un->un_tgt_blocksize == CDROM_BLK_512) { 29846 session_offset >>= 2; 29847 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) { 29848 session_offset >>= 1; 29849 } 29850 } 29851 29852 if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) { 29853 rval = EFAULT; 29854 } 29855 29856 kmem_free(buffer, SONY_SESSION_OFFSET_LEN); 29857 kmem_free(com, sizeof (*com)); 29858 return (rval); 29859 } 29860 29861 29862 /* 29863 * Function: sd_wm_cache_constructor() 29864 * 29865 * Description: Cache Constructor for the wmap cache for the read/modify/write 29866 * devices. 29867 * 29868 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29869 * un - sd_lun structure for the device. 29870 * flag - the km flags passed to constructor 29871 * 29872 * Return Code: 0 on success. 29873 * -1 on failure. 29874 */ 29875 29876 /*ARGSUSED*/ 29877 static int 29878 sd_wm_cache_constructor(void *wm, void *un, int flags) 29879 { 29880 bzero(wm, sizeof (struct sd_w_map)); 29881 cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL); 29882 return (0); 29883 } 29884 29885 29886 /* 29887 * Function: sd_wm_cache_destructor() 29888 * 29889 * Description: Cache destructor for the wmap cache for the read/modify/write 29890 * devices. 29891 * 29892 * Arguments: wm - A pointer to the sd_w_map to be initialized. 29893 * un - sd_lun structure for the device. 29894 */ 29895 /*ARGSUSED*/ 29896 static void 29897 sd_wm_cache_destructor(void *wm, void *un) 29898 { 29899 cv_destroy(&((struct sd_w_map *)wm)->wm_avail); 29900 } 29901 29902 29903 /* 29904 * Function: sd_range_lock() 29905 * 29906 * Description: Lock the range of blocks specified as parameter to ensure 29907 * that read, modify write is atomic and no other i/o writes 29908 * to the same location. The range is specified in terms 29909 * of start and end blocks. Block numbers are the actual 29910 * media block numbers and not system. 29911 * 29912 * Arguments: un - sd_lun structure for the device. 29913 * startb - The starting block number 29914 * endb - The end block number 29915 * typ - type of i/o - simple/read_modify_write 29916 * 29917 * Return Code: wm - pointer to the wmap structure. 29918 * 29919 * Context: This routine can sleep. 29920 */ 29921 29922 static struct sd_w_map * 29923 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ) 29924 { 29925 struct sd_w_map *wmp = NULL; 29926 struct sd_w_map *sl_wmp = NULL; 29927 struct sd_w_map *tmp_wmp; 29928 wm_state state = SD_WM_CHK_LIST; 29929 29930 29931 ASSERT(un != NULL); 29932 ASSERT(!mutex_owned(SD_MUTEX(un))); 29933 29934 mutex_enter(SD_MUTEX(un)); 29935 29936 while (state != SD_WM_DONE) { 29937 29938 switch (state) { 29939 case SD_WM_CHK_LIST: 29940 /* 29941 * This is the starting state. Check the wmap list 29942 * to see if the range is currently available. 29943 */ 29944 if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) { 29945 /* 29946 * If this is a simple write and no rmw 29947 * i/o is pending then try to lock the 29948 * range as the range should be available. 29949 */ 29950 state = SD_WM_LOCK_RANGE; 29951 } else { 29952 tmp_wmp = sd_get_range(un, startb, endb); 29953 if (tmp_wmp != NULL) { 29954 if ((wmp != NULL) && ONLIST(un, wmp)) { 29955 /* 29956 * Should not keep onlist wmps 29957 * while waiting this macro 29958 * will also do wmp = NULL; 29959 */ 29960 FREE_ONLIST_WMAP(un, wmp); 29961 } 29962 /* 29963 * sl_wmp is the wmap on which wait 29964 * is done, since the tmp_wmp points 29965 * to the inuse wmap, set sl_wmp to 29966 * tmp_wmp and change the state to sleep 29967 */ 29968 sl_wmp = tmp_wmp; 29969 state = SD_WM_WAIT_MAP; 29970 } else { 29971 state = SD_WM_LOCK_RANGE; 29972 } 29973 29974 } 29975 break; 29976 29977 case SD_WM_LOCK_RANGE: 29978 ASSERT(un->un_wm_cache); 29979 /* 29980 * The range need to be locked, try to get a wmap. 29981 * First attempt it with NO_SLEEP, want to avoid a sleep 29982 * if possible as we will have to release the sd mutex 29983 * if we have to sleep. 29984 */ 29985 if (wmp == NULL) 29986 wmp = kmem_cache_alloc(un->un_wm_cache, 29987 KM_NOSLEEP); 29988 if (wmp == NULL) { 29989 mutex_exit(SD_MUTEX(un)); 29990 _NOTE(DATA_READABLE_WITHOUT_LOCK 29991 (sd_lun::un_wm_cache)) 29992 wmp = kmem_cache_alloc(un->un_wm_cache, 29993 KM_SLEEP); 29994 mutex_enter(SD_MUTEX(un)); 29995 /* 29996 * we released the mutex so recheck and go to 29997 * check list state. 29998 */ 29999 state = SD_WM_CHK_LIST; 30000 } else { 30001 /* 30002 * We exit out of state machine since we 30003 * have the wmap. Do the housekeeping first. 30004 * place the wmap on the wmap list if it is not 30005 * on it already and then set the state to done. 30006 */ 30007 wmp->wm_start = startb; 30008 wmp->wm_end = endb; 30009 wmp->wm_flags = typ | SD_WM_BUSY; 30010 if (typ & SD_WTYPE_RMW) { 30011 un->un_rmw_count++; 30012 } 30013 /* 30014 * If not already on the list then link 30015 */ 30016 if (!ONLIST(un, wmp)) { 30017 wmp->wm_next = un->un_wm; 30018 wmp->wm_prev = NULL; 30019 if (wmp->wm_next) 30020 wmp->wm_next->wm_prev = wmp; 30021 un->un_wm = wmp; 30022 } 30023 state = SD_WM_DONE; 30024 } 30025 break; 30026 30027 case SD_WM_WAIT_MAP: 30028 ASSERT(sl_wmp->wm_flags & SD_WM_BUSY); 30029 /* 30030 * Wait is done on sl_wmp, which is set in the 30031 * check_list state. 30032 */ 30033 sl_wmp->wm_wanted_count++; 30034 cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un)); 30035 sl_wmp->wm_wanted_count--; 30036 /* 30037 * We can reuse the memory from the completed sl_wmp 30038 * lock range for our new lock, but only if noone is 30039 * waiting for it. 30040 */ 30041 ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY)); 30042 if (sl_wmp->wm_wanted_count == 0) { 30043 if (wmp != NULL) 30044 CHK_N_FREEWMP(un, wmp); 30045 wmp = sl_wmp; 30046 } 30047 sl_wmp = NULL; 30048 /* 30049 * After waking up, need to recheck for availability of 30050 * range. 30051 */ 30052 state = SD_WM_CHK_LIST; 30053 break; 30054 30055 default: 30056 panic("sd_range_lock: " 30057 "Unknown state %d in sd_range_lock", state); 30058 /*NOTREACHED*/ 30059 } /* switch(state) */ 30060 30061 } /* while(state != SD_WM_DONE) */ 30062 30063 mutex_exit(SD_MUTEX(un)); 30064 30065 ASSERT(wmp != NULL); 30066 30067 return (wmp); 30068 } 30069 30070 30071 /* 30072 * Function: sd_get_range() 30073 * 30074 * Description: Find if there any overlapping I/O to this one 30075 * Returns the write-map of 1st such I/O, NULL otherwise. 30076 * 30077 * Arguments: un - sd_lun structure for the device. 30078 * startb - The starting block number 30079 * endb - The end block number 30080 * 30081 * Return Code: wm - pointer to the wmap structure. 30082 */ 30083 30084 static struct sd_w_map * 30085 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb) 30086 { 30087 struct sd_w_map *wmp; 30088 30089 ASSERT(un != NULL); 30090 30091 for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) { 30092 if (!(wmp->wm_flags & SD_WM_BUSY)) { 30093 continue; 30094 } 30095 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) { 30096 break; 30097 } 30098 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) { 30099 break; 30100 } 30101 } 30102 30103 return (wmp); 30104 } 30105 30106 30107 /* 30108 * Function: sd_free_inlist_wmap() 30109 * 30110 * Description: Unlink and free a write map struct. 30111 * 30112 * Arguments: un - sd_lun structure for the device. 30113 * wmp - sd_w_map which needs to be unlinked. 30114 */ 30115 30116 static void 30117 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp) 30118 { 30119 ASSERT(un != NULL); 30120 30121 if (un->un_wm == wmp) { 30122 un->un_wm = wmp->wm_next; 30123 } else { 30124 wmp->wm_prev->wm_next = wmp->wm_next; 30125 } 30126 30127 if (wmp->wm_next) { 30128 wmp->wm_next->wm_prev = wmp->wm_prev; 30129 } 30130 30131 wmp->wm_next = wmp->wm_prev = NULL; 30132 30133 kmem_cache_free(un->un_wm_cache, wmp); 30134 } 30135 30136 30137 /* 30138 * Function: sd_range_unlock() 30139 * 30140 * Description: Unlock the range locked by wm. 30141 * Free write map if nobody else is waiting on it. 30142 * 30143 * Arguments: un - sd_lun structure for the device. 30144 * wmp - sd_w_map which needs to be unlinked. 30145 */ 30146 30147 static void 30148 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm) 30149 { 30150 ASSERT(un != NULL); 30151 ASSERT(wm != NULL); 30152 ASSERT(!mutex_owned(SD_MUTEX(un))); 30153 30154 mutex_enter(SD_MUTEX(un)); 30155 30156 if (wm->wm_flags & SD_WTYPE_RMW) { 30157 un->un_rmw_count--; 30158 } 30159 30160 if (wm->wm_wanted_count) { 30161 wm->wm_flags = 0; 30162 /* 30163 * Broadcast that the wmap is available now. 30164 */ 30165 cv_broadcast(&wm->wm_avail); 30166 } else { 30167 /* 30168 * If no one is waiting on the map, it should be free'ed. 30169 */ 30170 sd_free_inlist_wmap(un, wm); 30171 } 30172 30173 mutex_exit(SD_MUTEX(un)); 30174 } 30175 30176 30177 /* 30178 * Function: sd_read_modify_write_task 30179 * 30180 * Description: Called from a taskq thread to initiate the write phase of 30181 * a read-modify-write request. This is used for targets where 30182 * un->un_sys_blocksize != un->un_tgt_blocksize. 30183 * 30184 * Arguments: arg - a pointer to the buf(9S) struct for the write command. 30185 * 30186 * Context: Called under taskq thread context. 30187 */ 30188 30189 static void 30190 sd_read_modify_write_task(void *arg) 30191 { 30192 struct sd_mapblocksize_info *bsp; 30193 struct buf *bp; 30194 struct sd_xbuf *xp; 30195 struct sd_lun *un; 30196 30197 bp = arg; /* The bp is given in arg */ 30198 ASSERT(bp != NULL); 30199 30200 /* Get the pointer to the layer-private data struct */ 30201 xp = SD_GET_XBUF(bp); 30202 ASSERT(xp != NULL); 30203 bsp = xp->xb_private; 30204 ASSERT(bsp != NULL); 30205 30206 un = SD_GET_UN(bp); 30207 ASSERT(un != NULL); 30208 ASSERT(!mutex_owned(SD_MUTEX(un))); 30209 30210 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30211 "sd_read_modify_write_task: entry: buf:0x%p\n", bp); 30212 30213 /* 30214 * This is the write phase of a read-modify-write request, called 30215 * under the context of a taskq thread in response to the completion 30216 * of the read portion of the rmw request completing under interrupt 30217 * context. The write request must be sent from here down the iostart 30218 * chain as if it were being sent from sd_mapblocksize_iostart(), so 30219 * we use the layer index saved in the layer-private data area. 30220 */ 30221 SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp); 30222 30223 SD_TRACE(SD_LOG_IO_RMMEDIA, un, 30224 "sd_read_modify_write_task: exit: buf:0x%p\n", bp); 30225 } 30226 30227 30228 /* 30229 * Function: sddump_do_read_of_rmw() 30230 * 30231 * Description: This routine will be called from sddump, If sddump is called 30232 * with an I/O which not aligned on device blocksize boundary 30233 * then the write has to be converted to read-modify-write. 30234 * Do the read part here in order to keep sddump simple. 30235 * Note - That the sd_mutex is held across the call to this 30236 * routine. 30237 * 30238 * Arguments: un - sd_lun 30239 * blkno - block number in terms of media block size. 30240 * nblk - number of blocks. 30241 * bpp - pointer to pointer to the buf structure. On return 30242 * from this function, *bpp points to the valid buffer 30243 * to which the write has to be done. 30244 * 30245 * Return Code: 0 for success or errno-type return code 30246 */ 30247 30248 static int 30249 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk, 30250 struct buf **bpp) 30251 { 30252 int err; 30253 int i; 30254 int rval; 30255 struct buf *bp; 30256 struct scsi_pkt *pkt = NULL; 30257 uint32_t target_blocksize; 30258 30259 ASSERT(un != NULL); 30260 ASSERT(mutex_owned(SD_MUTEX(un))); 30261 30262 target_blocksize = un->un_tgt_blocksize; 30263 30264 mutex_exit(SD_MUTEX(un)); 30265 30266 bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL, 30267 (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL); 30268 if (bp == NULL) { 30269 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30270 "no resources for dumping; giving up"); 30271 err = ENOMEM; 30272 goto done; 30273 } 30274 30275 rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL, 30276 blkno, nblk); 30277 if (rval != 0) { 30278 scsi_free_consistent_buf(bp); 30279 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30280 "no resources for dumping; giving up"); 30281 err = ENOMEM; 30282 goto done; 30283 } 30284 30285 pkt->pkt_flags |= FLAG_NOINTR; 30286 30287 err = EIO; 30288 for (i = 0; i < SD_NDUMP_RETRIES; i++) { 30289 30290 /* 30291 * Scsi_poll returns 0 (success) if the command completes and 30292 * the status block is STATUS_GOOD. We should only check 30293 * errors if this condition is not true. Even then we should 30294 * send our own request sense packet only if we have a check 30295 * condition and auto request sense has not been performed by 30296 * the hba. 30297 */ 30298 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n"); 30299 30300 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) { 30301 err = 0; 30302 break; 30303 } 30304 30305 /* 30306 * Check CMD_DEV_GONE 1st, give up if device is gone, 30307 * no need to read RQS data. 30308 */ 30309 if (pkt->pkt_reason == CMD_DEV_GONE) { 30310 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, 30311 "Device is gone\n"); 30312 break; 30313 } 30314 30315 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) { 30316 SD_INFO(SD_LOG_DUMP, un, 30317 "sddump: read failed with CHECK, try # %d\n", i); 30318 if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) { 30319 (void) sd_send_polled_RQS(un); 30320 } 30321 30322 continue; 30323 } 30324 30325 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) { 30326 int reset_retval = 0; 30327 30328 SD_INFO(SD_LOG_DUMP, un, 30329 "sddump: read failed with BUSY, try # %d\n", i); 30330 30331 if (un->un_f_lun_reset_enabled == TRUE) { 30332 reset_retval = scsi_reset(SD_ADDRESS(un), 30333 RESET_LUN); 30334 } 30335 if (reset_retval == 0) { 30336 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET); 30337 } 30338 (void) sd_send_polled_RQS(un); 30339 30340 } else { 30341 SD_INFO(SD_LOG_DUMP, un, 30342 "sddump: read failed with 0x%x, try # %d\n", 30343 SD_GET_PKT_STATUS(pkt), i); 30344 mutex_enter(SD_MUTEX(un)); 30345 sd_reset_target(un, pkt); 30346 mutex_exit(SD_MUTEX(un)); 30347 } 30348 30349 /* 30350 * If we are not getting anywhere with lun/target resets, 30351 * let's reset the bus. 30352 */ 30353 if (i > SD_NDUMP_RETRIES/2) { 30354 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL); 30355 (void) sd_send_polled_RQS(un); 30356 } 30357 30358 } 30359 scsi_destroy_pkt(pkt); 30360 30361 if (err != 0) { 30362 scsi_free_consistent_buf(bp); 30363 *bpp = NULL; 30364 } else { 30365 *bpp = bp; 30366 } 30367 30368 done: 30369 mutex_enter(SD_MUTEX(un)); 30370 return (err); 30371 } 30372 30373 30374 /* 30375 * Function: sd_failfast_flushq 30376 * 30377 * Description: Take all bp's on the wait queue that have B_FAILFAST set 30378 * in b_flags and move them onto the failfast queue, then kick 30379 * off a thread to return all bp's on the failfast queue to 30380 * their owners with an error set. 30381 * 30382 * Arguments: un - pointer to the soft state struct for the instance. 30383 * 30384 * Context: may execute in interrupt context. 30385 */ 30386 30387 static void 30388 sd_failfast_flushq(struct sd_lun *un) 30389 { 30390 struct buf *bp; 30391 struct buf *next_waitq_bp; 30392 struct buf *prev_waitq_bp = NULL; 30393 30394 ASSERT(un != NULL); 30395 ASSERT(mutex_owned(SD_MUTEX(un))); 30396 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE); 30397 ASSERT(un->un_failfast_bp == NULL); 30398 30399 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30400 "sd_failfast_flushq: entry: un:0x%p\n", un); 30401 30402 /* 30403 * Check if we should flush all bufs when entering failfast state, or 30404 * just those with B_FAILFAST set. 30405 */ 30406 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) { 30407 /* 30408 * Move *all* bp's on the wait queue to the failfast flush 30409 * queue, including those that do NOT have B_FAILFAST set. 30410 */ 30411 if (un->un_failfast_headp == NULL) { 30412 ASSERT(un->un_failfast_tailp == NULL); 30413 un->un_failfast_headp = un->un_waitq_headp; 30414 } else { 30415 ASSERT(un->un_failfast_tailp != NULL); 30416 un->un_failfast_tailp->av_forw = un->un_waitq_headp; 30417 } 30418 30419 un->un_failfast_tailp = un->un_waitq_tailp; 30420 30421 /* update kstat for each bp moved out of the waitq */ 30422 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) { 30423 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30424 } 30425 30426 /* empty the waitq */ 30427 un->un_waitq_headp = un->un_waitq_tailp = NULL; 30428 30429 } else { 30430 /* 30431 * Go thru the wait queue, pick off all entries with 30432 * B_FAILFAST set, and move these onto the failfast queue. 30433 */ 30434 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) { 30435 /* 30436 * Save the pointer to the next bp on the wait queue, 30437 * so we get to it on the next iteration of this loop. 30438 */ 30439 next_waitq_bp = bp->av_forw; 30440 30441 /* 30442 * If this bp from the wait queue does NOT have 30443 * B_FAILFAST set, just move on to the next element 30444 * in the wait queue. Note, this is the only place 30445 * where it is correct to set prev_waitq_bp. 30446 */ 30447 if ((bp->b_flags & B_FAILFAST) == 0) { 30448 prev_waitq_bp = bp; 30449 continue; 30450 } 30451 30452 /* 30453 * Remove the bp from the wait queue. 30454 */ 30455 if (bp == un->un_waitq_headp) { 30456 /* The bp is the first element of the waitq. */ 30457 un->un_waitq_headp = next_waitq_bp; 30458 if (un->un_waitq_headp == NULL) { 30459 /* The wait queue is now empty */ 30460 un->un_waitq_tailp = NULL; 30461 } 30462 } else { 30463 /* 30464 * The bp is either somewhere in the middle 30465 * or at the end of the wait queue. 30466 */ 30467 ASSERT(un->un_waitq_headp != NULL); 30468 ASSERT(prev_waitq_bp != NULL); 30469 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST) 30470 == 0); 30471 if (bp == un->un_waitq_tailp) { 30472 /* bp is the last entry on the waitq. */ 30473 ASSERT(next_waitq_bp == NULL); 30474 un->un_waitq_tailp = prev_waitq_bp; 30475 } 30476 prev_waitq_bp->av_forw = next_waitq_bp; 30477 } 30478 bp->av_forw = NULL; 30479 30480 /* 30481 * update kstat since the bp is moved out of 30482 * the waitq 30483 */ 30484 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp); 30485 30486 /* 30487 * Now put the bp onto the failfast queue. 30488 */ 30489 if (un->un_failfast_headp == NULL) { 30490 /* failfast queue is currently empty */ 30491 ASSERT(un->un_failfast_tailp == NULL); 30492 un->un_failfast_headp = 30493 un->un_failfast_tailp = bp; 30494 } else { 30495 /* Add the bp to the end of the failfast q */ 30496 ASSERT(un->un_failfast_tailp != NULL); 30497 ASSERT(un->un_failfast_tailp->b_flags & 30498 B_FAILFAST); 30499 un->un_failfast_tailp->av_forw = bp; 30500 un->un_failfast_tailp = bp; 30501 } 30502 } 30503 } 30504 30505 /* 30506 * Now return all bp's on the failfast queue to their owners. 30507 */ 30508 while ((bp = un->un_failfast_headp) != NULL) { 30509 30510 un->un_failfast_headp = bp->av_forw; 30511 if (un->un_failfast_headp == NULL) { 30512 un->un_failfast_tailp = NULL; 30513 } 30514 30515 /* 30516 * We want to return the bp with a failure error code, but 30517 * we do not want a call to sd_start_cmds() to occur here, 30518 * so use sd_return_failed_command_no_restart() instead of 30519 * sd_return_failed_command(). 30520 */ 30521 sd_return_failed_command_no_restart(un, bp, EIO); 30522 } 30523 30524 /* Flush the xbuf queues if required. */ 30525 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) { 30526 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback); 30527 } 30528 30529 SD_TRACE(SD_LOG_IO_FAILFAST, un, 30530 "sd_failfast_flushq: exit: un:0x%p\n", un); 30531 } 30532 30533 30534 /* 30535 * Function: sd_failfast_flushq_callback 30536 * 30537 * Description: Return TRUE if the given bp meets the criteria for failfast 30538 * flushing. Used with ddi_xbuf_flushq(9F). 30539 * 30540 * Arguments: bp - ptr to buf struct to be examined. 30541 * 30542 * Context: Any 30543 */ 30544 30545 static int 30546 sd_failfast_flushq_callback(struct buf *bp) 30547 { 30548 /* 30549 * Return TRUE if (1) we want to flush ALL bufs when the failfast 30550 * state is entered; OR (2) the given bp has B_FAILFAST set. 30551 */ 30552 return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) || 30553 (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE); 30554 } 30555 30556 30557 30558 #if defined(__i386) || defined(__amd64) 30559 /* 30560 * Function: sd_setup_next_xfer 30561 * 30562 * Description: Prepare next I/O operation using DMA_PARTIAL 30563 * 30564 */ 30565 30566 static int 30567 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp, 30568 struct scsi_pkt *pkt, struct sd_xbuf *xp) 30569 { 30570 ssize_t num_blks_not_xfered; 30571 daddr_t strt_blk_num; 30572 ssize_t bytes_not_xfered; 30573 int rval; 30574 30575 ASSERT(pkt->pkt_resid == 0); 30576 30577 /* 30578 * Calculate next block number and amount to be transferred. 30579 * 30580 * How much data NOT transfered to the HBA yet. 30581 */ 30582 bytes_not_xfered = xp->xb_dma_resid; 30583 30584 /* 30585 * figure how many blocks NOT transfered to the HBA yet. 30586 */ 30587 num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered); 30588 30589 /* 30590 * set starting block number to the end of what WAS transfered. 30591 */ 30592 strt_blk_num = xp->xb_blkno + 30593 SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered); 30594 30595 /* 30596 * Move pkt to the next portion of the xfer. sd_setup_next_rw_pkt 30597 * will call scsi_initpkt with NULL_FUNC so we do not have to release 30598 * the disk mutex here. 30599 */ 30600 rval = sd_setup_next_rw_pkt(un, pkt, bp, 30601 strt_blk_num, num_blks_not_xfered); 30602 30603 if (rval == 0) { 30604 30605 /* 30606 * Success. 30607 * 30608 * Adjust things if there are still more blocks to be 30609 * transfered. 30610 */ 30611 xp->xb_dma_resid = pkt->pkt_resid; 30612 pkt->pkt_resid = 0; 30613 30614 return (1); 30615 } 30616 30617 /* 30618 * There's really only one possible return value from 30619 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt 30620 * returns NULL. 30621 */ 30622 ASSERT(rval == SD_PKT_ALLOC_FAILURE); 30623 30624 bp->b_resid = bp->b_bcount; 30625 bp->b_flags |= B_ERROR; 30626 30627 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, 30628 "Error setting up next portion of DMA transfer\n"); 30629 30630 return (0); 30631 } 30632 #endif 30633 30634 /* 30635 * Function: sd_panic_for_res_conflict 30636 * 30637 * Description: Call panic with a string formated with "Reservation Conflict" 30638 * and a human readable identifier indicating the SD instance 30639 * that experienced the reservation conflict. 30640 * 30641 * Arguments: un - pointer to the soft state struct for the instance. 30642 * 30643 * Context: may execute in interrupt context. 30644 */ 30645 30646 #define SD_RESV_CONFLICT_FMT_LEN 40 30647 void 30648 sd_panic_for_res_conflict(struct sd_lun *un) 30649 { 30650 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN]; 30651 char path_str[MAXPATHLEN]; 30652 30653 (void) snprintf(panic_str, sizeof (panic_str), 30654 "Reservation Conflict\nDisk: %s", 30655 ddi_pathname(SD_DEVINFO(un), path_str)); 30656 30657 panic(panic_str); 30658 } 30659 30660 /* 30661 * Note: The following sd_faultinjection_ioctl( ) routines implement 30662 * driver support for handling fault injection for error analysis 30663 * causing faults in multiple layers of the driver. 30664 * 30665 */ 30666 30667 #ifdef SD_FAULT_INJECTION 30668 static uint_t sd_fault_injection_on = 0; 30669 30670 /* 30671 * Function: sd_faultinjection_ioctl() 30672 * 30673 * Description: This routine is the driver entry point for handling 30674 * faultinjection ioctls to inject errors into the 30675 * layer model 30676 * 30677 * Arguments: cmd - the ioctl cmd recieved 30678 * arg - the arguments from user and returns 30679 */ 30680 30681 static void 30682 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un) { 30683 30684 uint_t i; 30685 uint_t rval; 30686 30687 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n"); 30688 30689 mutex_enter(SD_MUTEX(un)); 30690 30691 switch (cmd) { 30692 case SDIOCRUN: 30693 /* Allow pushed faults to be injected */ 30694 SD_INFO(SD_LOG_SDTEST, un, 30695 "sd_faultinjection_ioctl: Injecting Fault Run\n"); 30696 30697 sd_fault_injection_on = 1; 30698 30699 SD_INFO(SD_LOG_IOERR, un, 30700 "sd_faultinjection_ioctl: run finished\n"); 30701 break; 30702 30703 case SDIOCSTART: 30704 /* Start Injection Session */ 30705 SD_INFO(SD_LOG_SDTEST, un, 30706 "sd_faultinjection_ioctl: Injecting Fault Start\n"); 30707 30708 sd_fault_injection_on = 0; 30709 un->sd_injection_mask = 0xFFFFFFFF; 30710 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30711 un->sd_fi_fifo_pkt[i] = NULL; 30712 un->sd_fi_fifo_xb[i] = NULL; 30713 un->sd_fi_fifo_un[i] = NULL; 30714 un->sd_fi_fifo_arq[i] = NULL; 30715 } 30716 un->sd_fi_fifo_start = 0; 30717 un->sd_fi_fifo_end = 0; 30718 30719 mutex_enter(&(un->un_fi_mutex)); 30720 un->sd_fi_log[0] = '\0'; 30721 un->sd_fi_buf_len = 0; 30722 mutex_exit(&(un->un_fi_mutex)); 30723 30724 SD_INFO(SD_LOG_IOERR, un, 30725 "sd_faultinjection_ioctl: start finished\n"); 30726 break; 30727 30728 case SDIOCSTOP: 30729 /* Stop Injection Session */ 30730 SD_INFO(SD_LOG_SDTEST, un, 30731 "sd_faultinjection_ioctl: Injecting Fault Stop\n"); 30732 sd_fault_injection_on = 0; 30733 un->sd_injection_mask = 0x0; 30734 30735 /* Empty stray or unuseds structs from fifo */ 30736 for (i = 0; i < SD_FI_MAX_ERROR; i++) { 30737 if (un->sd_fi_fifo_pkt[i] != NULL) { 30738 kmem_free(un->sd_fi_fifo_pkt[i], 30739 sizeof (struct sd_fi_pkt)); 30740 } 30741 if (un->sd_fi_fifo_xb[i] != NULL) { 30742 kmem_free(un->sd_fi_fifo_xb[i], 30743 sizeof (struct sd_fi_xb)); 30744 } 30745 if (un->sd_fi_fifo_un[i] != NULL) { 30746 kmem_free(un->sd_fi_fifo_un[i], 30747 sizeof (struct sd_fi_un)); 30748 } 30749 if (un->sd_fi_fifo_arq[i] != NULL) { 30750 kmem_free(un->sd_fi_fifo_arq[i], 30751 sizeof (struct sd_fi_arq)); 30752 } 30753 un->sd_fi_fifo_pkt[i] = NULL; 30754 un->sd_fi_fifo_un[i] = NULL; 30755 un->sd_fi_fifo_xb[i] = NULL; 30756 un->sd_fi_fifo_arq[i] = NULL; 30757 } 30758 un->sd_fi_fifo_start = 0; 30759 un->sd_fi_fifo_end = 0; 30760 30761 SD_INFO(SD_LOG_IOERR, un, 30762 "sd_faultinjection_ioctl: stop finished\n"); 30763 break; 30764 30765 case SDIOCINSERTPKT: 30766 /* Store a packet struct to be pushed onto fifo */ 30767 SD_INFO(SD_LOG_SDTEST, un, 30768 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n"); 30769 30770 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30771 30772 sd_fault_injection_on = 0; 30773 30774 /* No more that SD_FI_MAX_ERROR allowed in Queue */ 30775 if (un->sd_fi_fifo_pkt[i] != NULL) { 30776 kmem_free(un->sd_fi_fifo_pkt[i], 30777 sizeof (struct sd_fi_pkt)); 30778 } 30779 if (arg != NULL) { 30780 un->sd_fi_fifo_pkt[i] = 30781 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP); 30782 if (un->sd_fi_fifo_pkt[i] == NULL) { 30783 /* Alloc failed don't store anything */ 30784 break; 30785 } 30786 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i], 30787 sizeof (struct sd_fi_pkt), 0); 30788 if (rval == -1) { 30789 kmem_free(un->sd_fi_fifo_pkt[i], 30790 sizeof (struct sd_fi_pkt)); 30791 un->sd_fi_fifo_pkt[i] = NULL; 30792 } 30793 } else { 30794 SD_INFO(SD_LOG_IOERR, un, 30795 "sd_faultinjection_ioctl: pkt null\n"); 30796 } 30797 break; 30798 30799 case SDIOCINSERTXB: 30800 /* Store a xb struct to be pushed onto fifo */ 30801 SD_INFO(SD_LOG_SDTEST, un, 30802 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n"); 30803 30804 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30805 30806 sd_fault_injection_on = 0; 30807 30808 if (un->sd_fi_fifo_xb[i] != NULL) { 30809 kmem_free(un->sd_fi_fifo_xb[i], 30810 sizeof (struct sd_fi_xb)); 30811 un->sd_fi_fifo_xb[i] = NULL; 30812 } 30813 if (arg != NULL) { 30814 un->sd_fi_fifo_xb[i] = 30815 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP); 30816 if (un->sd_fi_fifo_xb[i] == NULL) { 30817 /* Alloc failed don't store anything */ 30818 break; 30819 } 30820 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i], 30821 sizeof (struct sd_fi_xb), 0); 30822 30823 if (rval == -1) { 30824 kmem_free(un->sd_fi_fifo_xb[i], 30825 sizeof (struct sd_fi_xb)); 30826 un->sd_fi_fifo_xb[i] = NULL; 30827 } 30828 } else { 30829 SD_INFO(SD_LOG_IOERR, un, 30830 "sd_faultinjection_ioctl: xb null\n"); 30831 } 30832 break; 30833 30834 case SDIOCINSERTUN: 30835 /* Store a un struct to be pushed onto fifo */ 30836 SD_INFO(SD_LOG_SDTEST, un, 30837 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n"); 30838 30839 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30840 30841 sd_fault_injection_on = 0; 30842 30843 if (un->sd_fi_fifo_un[i] != NULL) { 30844 kmem_free(un->sd_fi_fifo_un[i], 30845 sizeof (struct sd_fi_un)); 30846 un->sd_fi_fifo_un[i] = NULL; 30847 } 30848 if (arg != NULL) { 30849 un->sd_fi_fifo_un[i] = 30850 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP); 30851 if (un->sd_fi_fifo_un[i] == NULL) { 30852 /* Alloc failed don't store anything */ 30853 break; 30854 } 30855 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i], 30856 sizeof (struct sd_fi_un), 0); 30857 if (rval == -1) { 30858 kmem_free(un->sd_fi_fifo_un[i], 30859 sizeof (struct sd_fi_un)); 30860 un->sd_fi_fifo_un[i] = NULL; 30861 } 30862 30863 } else { 30864 SD_INFO(SD_LOG_IOERR, un, 30865 "sd_faultinjection_ioctl: un null\n"); 30866 } 30867 30868 break; 30869 30870 case SDIOCINSERTARQ: 30871 /* Store a arq struct to be pushed onto fifo */ 30872 SD_INFO(SD_LOG_SDTEST, un, 30873 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n"); 30874 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR; 30875 30876 sd_fault_injection_on = 0; 30877 30878 if (un->sd_fi_fifo_arq[i] != NULL) { 30879 kmem_free(un->sd_fi_fifo_arq[i], 30880 sizeof (struct sd_fi_arq)); 30881 un->sd_fi_fifo_arq[i] = NULL; 30882 } 30883 if (arg != NULL) { 30884 un->sd_fi_fifo_arq[i] = 30885 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP); 30886 if (un->sd_fi_fifo_arq[i] == NULL) { 30887 /* Alloc failed don't store anything */ 30888 break; 30889 } 30890 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i], 30891 sizeof (struct sd_fi_arq), 0); 30892 if (rval == -1) { 30893 kmem_free(un->sd_fi_fifo_arq[i], 30894 sizeof (struct sd_fi_arq)); 30895 un->sd_fi_fifo_arq[i] = NULL; 30896 } 30897 30898 } else { 30899 SD_INFO(SD_LOG_IOERR, un, 30900 "sd_faultinjection_ioctl: arq null\n"); 30901 } 30902 30903 break; 30904 30905 case SDIOCPUSH: 30906 /* Push stored xb, pkt, un, and arq onto fifo */ 30907 sd_fault_injection_on = 0; 30908 30909 if (arg != NULL) { 30910 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0); 30911 if (rval != -1 && 30912 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30913 un->sd_fi_fifo_end += i; 30914 } 30915 } else { 30916 SD_INFO(SD_LOG_IOERR, un, 30917 "sd_faultinjection_ioctl: push arg null\n"); 30918 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) { 30919 un->sd_fi_fifo_end++; 30920 } 30921 } 30922 SD_INFO(SD_LOG_IOERR, un, 30923 "sd_faultinjection_ioctl: push to end=%d\n", 30924 un->sd_fi_fifo_end); 30925 break; 30926 30927 case SDIOCRETRIEVE: 30928 /* Return buffer of log from Injection session */ 30929 SD_INFO(SD_LOG_SDTEST, un, 30930 "sd_faultinjection_ioctl: Injecting Fault Retreive"); 30931 30932 sd_fault_injection_on = 0; 30933 30934 mutex_enter(&(un->un_fi_mutex)); 30935 rval = ddi_copyout(un->sd_fi_log, (void *)arg, 30936 un->sd_fi_buf_len+1, 0); 30937 mutex_exit(&(un->un_fi_mutex)); 30938 30939 if (rval == -1) { 30940 /* 30941 * arg is possibly invalid setting 30942 * it to NULL for return 30943 */ 30944 arg = NULL; 30945 } 30946 break; 30947 } 30948 30949 mutex_exit(SD_MUTEX(un)); 30950 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:" 30951 " exit\n"); 30952 } 30953 30954 30955 /* 30956 * Function: sd_injection_log() 30957 * 30958 * Description: This routine adds buff to the already existing injection log 30959 * for retrieval via faultinjection_ioctl for use in fault 30960 * detection and recovery 30961 * 30962 * Arguments: buf - the string to add to the log 30963 */ 30964 30965 static void 30966 sd_injection_log(char *buf, struct sd_lun *un) 30967 { 30968 uint_t len; 30969 30970 ASSERT(un != NULL); 30971 ASSERT(buf != NULL); 30972 30973 mutex_enter(&(un->un_fi_mutex)); 30974 30975 len = min(strlen(buf), 255); 30976 /* Add logged value to Injection log to be returned later */ 30977 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) { 30978 uint_t offset = strlen((char *)un->sd_fi_log); 30979 char *destp = (char *)un->sd_fi_log + offset; 30980 int i; 30981 for (i = 0; i < len; i++) { 30982 *destp++ = *buf++; 30983 } 30984 un->sd_fi_buf_len += len; 30985 un->sd_fi_log[un->sd_fi_buf_len] = '\0'; 30986 } 30987 30988 mutex_exit(&(un->un_fi_mutex)); 30989 } 30990 30991 30992 /* 30993 * Function: sd_faultinjection() 30994 * 30995 * Description: This routine takes the pkt and changes its 30996 * content based on error injection scenerio. 30997 * 30998 * Arguments: pktp - packet to be changed 30999 */ 31000 31001 static void 31002 sd_faultinjection(struct scsi_pkt *pktp) 31003 { 31004 uint_t i; 31005 struct sd_fi_pkt *fi_pkt; 31006 struct sd_fi_xb *fi_xb; 31007 struct sd_fi_un *fi_un; 31008 struct sd_fi_arq *fi_arq; 31009 struct buf *bp; 31010 struct sd_xbuf *xb; 31011 struct sd_lun *un; 31012 31013 ASSERT(pktp != NULL); 31014 31015 /* pull bp xb and un from pktp */ 31016 bp = (struct buf *)pktp->pkt_private; 31017 xb = SD_GET_XBUF(bp); 31018 un = SD_GET_UN(bp); 31019 31020 ASSERT(un != NULL); 31021 31022 mutex_enter(SD_MUTEX(un)); 31023 31024 SD_TRACE(SD_LOG_SDTEST, un, 31025 "sd_faultinjection: entry Injection from sdintr\n"); 31026 31027 /* if injection is off return */ 31028 if (sd_fault_injection_on == 0 || 31029 un->sd_fi_fifo_start == un->sd_fi_fifo_end) { 31030 mutex_exit(SD_MUTEX(un)); 31031 return; 31032 } 31033 31034 31035 /* take next set off fifo */ 31036 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR; 31037 31038 fi_pkt = un->sd_fi_fifo_pkt[i]; 31039 fi_xb = un->sd_fi_fifo_xb[i]; 31040 fi_un = un->sd_fi_fifo_un[i]; 31041 fi_arq = un->sd_fi_fifo_arq[i]; 31042 31043 31044 /* set variables accordingly */ 31045 /* set pkt if it was on fifo */ 31046 if (fi_pkt != NULL) { 31047 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags"); 31048 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp"); 31049 SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp"); 31050 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state"); 31051 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics"); 31052 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason"); 31053 31054 } 31055 31056 /* set xb if it was on fifo */ 31057 if (fi_xb != NULL) { 31058 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno"); 31059 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid"); 31060 SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count"); 31061 SD_CONDSET(xb, xb, xb_victim_retry_count, 31062 "xb_victim_retry_count"); 31063 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status"); 31064 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state"); 31065 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid"); 31066 31067 /* copy in block data from sense */ 31068 if (fi_xb->xb_sense_data[0] != -1) { 31069 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, 31070 SENSE_LENGTH); 31071 } 31072 31073 /* copy in extended sense codes */ 31074 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code, 31075 "es_code"); 31076 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key, 31077 "es_key"); 31078 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code, 31079 "es_add_code"); 31080 SD_CONDSET(((struct scsi_extended_sense *)xb), xb, 31081 es_qual_code, "es_qual_code"); 31082 } 31083 31084 /* set un if it was on fifo */ 31085 if (fi_un != NULL) { 31086 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb"); 31087 SD_CONDSET(un, un, un_ctype, "un_ctype"); 31088 SD_CONDSET(un, un, un_reset_retry_count, 31089 "un_reset_retry_count"); 31090 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type"); 31091 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status"); 31092 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled"); 31093 SD_CONDSET(un, un, un_f_geometry_is_valid, 31094 "un_f_geometry_is_valid"); 31095 SD_CONDSET(un, un, un_f_allow_bus_device_reset, 31096 "un_f_allow_bus_device_reset"); 31097 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing"); 31098 31099 } 31100 31101 /* copy in auto request sense if it was on fifo */ 31102 if (fi_arq != NULL) { 31103 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq)); 31104 } 31105 31106 /* free structs */ 31107 if (un->sd_fi_fifo_pkt[i] != NULL) { 31108 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt)); 31109 } 31110 if (un->sd_fi_fifo_xb[i] != NULL) { 31111 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb)); 31112 } 31113 if (un->sd_fi_fifo_un[i] != NULL) { 31114 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un)); 31115 } 31116 if (un->sd_fi_fifo_arq[i] != NULL) { 31117 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq)); 31118 } 31119 31120 /* 31121 * kmem_free does not gurantee to set to NULL 31122 * since we uses these to determine if we set 31123 * values or not lets confirm they are always 31124 * NULL after free 31125 */ 31126 un->sd_fi_fifo_pkt[i] = NULL; 31127 un->sd_fi_fifo_un[i] = NULL; 31128 un->sd_fi_fifo_xb[i] = NULL; 31129 un->sd_fi_fifo_arq[i] = NULL; 31130 31131 un->sd_fi_fifo_start++; 31132 31133 mutex_exit(SD_MUTEX(un)); 31134 31135 SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n"); 31136 } 31137 31138 #endif /* SD_FAULT_INJECTION */ 31139 31140 /* 31141 * This routine is invoked in sd_unit_attach(). Before calling it, the 31142 * properties in conf file should be processed already, and "hotpluggable" 31143 * property was processed also. 31144 * 31145 * The sd driver distinguishes 3 different type of devices: removable media, 31146 * non-removable media, and hotpluggable. Below the differences are defined: 31147 * 31148 * 1. Device ID 31149 * 31150 * The device ID of a device is used to identify this device. Refer to 31151 * ddi_devid_register(9F). 31152 * 31153 * For a non-removable media disk device which can provide 0x80 or 0x83 31154 * VPD page (refer to INQUIRY command of SCSI SPC specification), a unique 31155 * device ID is created to identify this device. For other non-removable 31156 * media devices, a default device ID is created only if this device has 31157 * at least 2 alter cylinders. Otherwise, this device has no devid. 31158 * 31159 * ------------------------------------------------------- 31160 * removable media hotpluggable | Can Have Device ID 31161 * ------------------------------------------------------- 31162 * false false | Yes 31163 * false true | Yes 31164 * true x | No 31165 * ------------------------------------------------------ 31166 * 31167 * 31168 * 2. SCSI group 4 commands 31169 * 31170 * In SCSI specs, only some commands in group 4 command set can use 31171 * 8-byte addresses that can be used to access >2TB storage spaces. 31172 * Other commands have no such capability. Without supporting group4, 31173 * it is impossible to make full use of storage spaces of a disk with 31174 * capacity larger than 2TB. 31175 * 31176 * ----------------------------------------------- 31177 * removable media hotpluggable LP64 | Group 31178 * ----------------------------------------------- 31179 * false false false | 1 31180 * false false true | 4 31181 * false true false | 1 31182 * false true true | 4 31183 * true x x | 5 31184 * ----------------------------------------------- 31185 * 31186 * 31187 * 3. Check for VTOC Label 31188 * 31189 * If a direct-access disk has no EFI label, sd will check if it has a 31190 * valid VTOC label. Now, sd also does that check for removable media 31191 * and hotpluggable devices. 31192 * 31193 * -------------------------------------------------------------- 31194 * Direct-Access removable media hotpluggable | Check Label 31195 * ------------------------------------------------------------- 31196 * false false false | No 31197 * false false true | No 31198 * false true false | Yes 31199 * false true true | Yes 31200 * true x x | Yes 31201 * -------------------------------------------------------------- 31202 * 31203 * 31204 * 4. Building default VTOC label 31205 * 31206 * As section 3 says, sd checks if some kinds of devices have VTOC label. 31207 * If those devices have no valid VTOC label, sd(7d) will attempt to 31208 * create default VTOC for them. Currently sd creates default VTOC label 31209 * for all devices on x86 platform (VTOC_16), but only for removable 31210 * media devices on SPARC (VTOC_8). 31211 * 31212 * ----------------------------------------------------------- 31213 * removable media hotpluggable platform | Default Label 31214 * ----------------------------------------------------------- 31215 * false false sparc | No 31216 * false true x86 | Yes 31217 * false true sparc | Yes 31218 * true x x | Yes 31219 * ---------------------------------------------------------- 31220 * 31221 * 31222 * 5. Supported blocksizes of target devices 31223 * 31224 * Sd supports non-512-byte blocksize for removable media devices only. 31225 * For other devices, only 512-byte blocksize is supported. This may be 31226 * changed in near future because some RAID devices require non-512-byte 31227 * blocksize 31228 * 31229 * ----------------------------------------------------------- 31230 * removable media hotpluggable | non-512-byte blocksize 31231 * ----------------------------------------------------------- 31232 * false false | No 31233 * false true | No 31234 * true x | Yes 31235 * ----------------------------------------------------------- 31236 * 31237 * 31238 * 6. Automatic mount & unmount 31239 * 31240 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query 31241 * if a device is removable media device. It return 1 for removable media 31242 * devices, and 0 for others. 31243 * 31244 * The automatic mounting subsystem should distinguish between the types 31245 * of devices and apply automounting policies to each. 31246 * 31247 * 31248 * 7. fdisk partition management 31249 * 31250 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver 31251 * just supports fdisk partitions on x86 platform. On sparc platform, sd 31252 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize 31253 * fdisk partitions on both x86 and SPARC platform. 31254 * 31255 * ----------------------------------------------------------- 31256 * platform removable media USB/1394 | fdisk supported 31257 * ----------------------------------------------------------- 31258 * x86 X X | true 31259 * ------------------------------------------------------------ 31260 * sparc X X | false 31261 * ------------------------------------------------------------ 31262 * 31263 * 31264 * 8. MBOOT/MBR 31265 * 31266 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support 31267 * read/write mboot for removable media devices on sparc platform. 31268 * 31269 * ----------------------------------------------------------- 31270 * platform removable media USB/1394 | mboot supported 31271 * ----------------------------------------------------------- 31272 * x86 X X | true 31273 * ------------------------------------------------------------ 31274 * sparc false false | false 31275 * sparc false true | true 31276 * sparc true false | true 31277 * sparc true true | true 31278 * ------------------------------------------------------------ 31279 * 31280 * 31281 * 9. error handling during opening device 31282 * 31283 * If failed to open a disk device, an errno is returned. For some kinds 31284 * of errors, different errno is returned depending on if this device is 31285 * a removable media device. This brings USB/1394 hard disks in line with 31286 * expected hard disk behavior. It is not expected that this breaks any 31287 * application. 31288 * 31289 * ------------------------------------------------------ 31290 * removable media hotpluggable | errno 31291 * ------------------------------------------------------ 31292 * false false | EIO 31293 * false true | EIO 31294 * true x | ENXIO 31295 * ------------------------------------------------------ 31296 * 31297 * 31298 * 11. ioctls: DKIOCEJECT, CDROMEJECT 31299 * 31300 * These IOCTLs are applicable only to removable media devices. 31301 * 31302 * ----------------------------------------------------------- 31303 * removable media hotpluggable |DKIOCEJECT, CDROMEJECT 31304 * ----------------------------------------------------------- 31305 * false false | No 31306 * false true | No 31307 * true x | Yes 31308 * ----------------------------------------------------------- 31309 * 31310 * 31311 * 12. Kstats for partitions 31312 * 31313 * sd creates partition kstat for non-removable media devices. USB and 31314 * Firewire hard disks now have partition kstats 31315 * 31316 * ------------------------------------------------------ 31317 * removable media hotplugable | kstat 31318 * ------------------------------------------------------ 31319 * false false | Yes 31320 * false true | Yes 31321 * true x | No 31322 * ------------------------------------------------------ 31323 * 31324 * 31325 * 13. Removable media & hotpluggable properties 31326 * 31327 * Sd driver creates a "removable-media" property for removable media 31328 * devices. Parent nexus drivers create a "hotpluggable" property if 31329 * it supports hotplugging. 31330 * 31331 * --------------------------------------------------------------------- 31332 * removable media hotpluggable | "removable-media" " hotpluggable" 31333 * --------------------------------------------------------------------- 31334 * false false | No No 31335 * false true | No Yes 31336 * true false | Yes No 31337 * true true | Yes Yes 31338 * --------------------------------------------------------------------- 31339 * 31340 * 31341 * 14. Power Management 31342 * 31343 * sd only power manages removable media devices or devices that support 31344 * LOG_SENSE or have a "pm-capable" property (PSARC/2002/250) 31345 * 31346 * A parent nexus that supports hotplugging can also set "pm-capable" 31347 * if the disk can be power managed. 31348 * 31349 * ------------------------------------------------------------ 31350 * removable media hotpluggable pm-capable | power manage 31351 * ------------------------------------------------------------ 31352 * false false false | No 31353 * false false true | Yes 31354 * false true false | No 31355 * false true true | Yes 31356 * true x x | Yes 31357 * ------------------------------------------------------------ 31358 * 31359 * USB and firewire hard disks can now be power managed independently 31360 * of the framebuffer 31361 * 31362 * 31363 * 15. Support for USB disks with capacity larger than 1TB 31364 * 31365 * Currently, sd doesn't permit a fixed disk device with capacity 31366 * larger than 1TB to be used in a 32-bit operating system environment. 31367 * However, sd doesn't do that for removable media devices. Instead, it 31368 * assumes that removable media devices cannot have a capacity larger 31369 * than 1TB. Therefore, using those devices on 32-bit system is partially 31370 * supported, which can cause some unexpected results. 31371 * 31372 * --------------------------------------------------------------------- 31373 * removable media USB/1394 | Capacity > 1TB | Used in 32-bit env 31374 * --------------------------------------------------------------------- 31375 * false false | true | no 31376 * false true | true | no 31377 * true false | true | Yes 31378 * true true | true | Yes 31379 * --------------------------------------------------------------------- 31380 * 31381 * 31382 * 16. Check write-protection at open time 31383 * 31384 * When a removable media device is being opened for writing without NDELAY 31385 * flag, sd will check if this device is writable. If attempting to open 31386 * without NDELAY flag a write-protected device, this operation will abort. 31387 * 31388 * ------------------------------------------------------------ 31389 * removable media USB/1394 | WP Check 31390 * ------------------------------------------------------------ 31391 * false false | No 31392 * false true | No 31393 * true false | Yes 31394 * true true | Yes 31395 * ------------------------------------------------------------ 31396 * 31397 * 31398 * 17. syslog when corrupted VTOC is encountered 31399 * 31400 * Currently, if an invalid VTOC is encountered, sd only print syslog 31401 * for fixed SCSI disks. 31402 * ------------------------------------------------------------ 31403 * removable media USB/1394 | print syslog 31404 * ------------------------------------------------------------ 31405 * false false | Yes 31406 * false true | No 31407 * true false | No 31408 * true true | No 31409 * ------------------------------------------------------------ 31410 */ 31411 static void 31412 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi) 31413 { 31414 int pm_capable_prop; 31415 31416 ASSERT(un->un_sd); 31417 ASSERT(un->un_sd->sd_inq); 31418 31419 #if defined(_SUNOS_VTOC_16) 31420 /* 31421 * For VTOC_16 devices, the default label will be created for all 31422 * devices. (see sd_build_default_label) 31423 */ 31424 un->un_f_default_vtoc_supported = TRUE; 31425 #endif 31426 31427 /* 31428 * Enable SYNC CACHE support for all devices. 31429 */ 31430 un->un_f_sync_cache_supported = TRUE; 31431 31432 if (un->un_sd->sd_inq->inq_rmb) { 31433 /* 31434 * The media of this device is removable. And for this kind 31435 * of devices, it is possible to change medium after opening 31436 * devices. Thus we should support this operation. 31437 */ 31438 un->un_f_has_removable_media = TRUE; 31439 31440 #if defined(_SUNOS_VTOC_8) 31441 /* 31442 * Note: currently, for VTOC_8 devices, default label is 31443 * created for removable and hotpluggable devices only. 31444 */ 31445 un->un_f_default_vtoc_supported = TRUE; 31446 #endif 31447 /* 31448 * support non-512-byte blocksize of removable media devices 31449 */ 31450 un->un_f_non_devbsize_supported = TRUE; 31451 31452 /* 31453 * Assume that all removable media devices support DOOR_LOCK 31454 */ 31455 un->un_f_doorlock_supported = TRUE; 31456 31457 /* 31458 * For a removable media device, it is possible to be opened 31459 * with NDELAY flag when there is no media in drive, in this 31460 * case we don't care if device is writable. But if without 31461 * NDELAY flag, we need to check if media is write-protected. 31462 */ 31463 un->un_f_chk_wp_open = TRUE; 31464 31465 /* 31466 * need to start a SCSI watch thread to monitor media state, 31467 * when media is being inserted or ejected, notify syseventd. 31468 */ 31469 un->un_f_monitor_media_state = TRUE; 31470 31471 /* 31472 * Some devices don't support START_STOP_UNIT command. 31473 * Therefore, we'd better check if a device supports it 31474 * before sending it. 31475 */ 31476 un->un_f_check_start_stop = TRUE; 31477 31478 /* 31479 * support eject media ioctl: 31480 * FDEJECT, DKIOCEJECT, CDROMEJECT 31481 */ 31482 un->un_f_eject_media_supported = TRUE; 31483 31484 /* 31485 * Because many removable-media devices don't support 31486 * LOG_SENSE, we couldn't use this command to check if 31487 * a removable media device support power-management. 31488 * We assume that they support power-management via 31489 * START_STOP_UNIT command and can be spun up and down 31490 * without limitations. 31491 */ 31492 un->un_f_pm_supported = TRUE; 31493 31494 /* 31495 * Need to create a zero length (Boolean) property 31496 * removable-media for the removable media devices. 31497 * Note that the return value of the property is not being 31498 * checked, since if unable to create the property 31499 * then do not want the attach to fail altogether. Consistent 31500 * with other property creation in attach. 31501 */ 31502 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, 31503 DDI_PROP_CANSLEEP, "removable-media", NULL, 0); 31504 31505 } else { 31506 /* 31507 * create device ID for device 31508 */ 31509 un->un_f_devid_supported = TRUE; 31510 31511 /* 31512 * Spin up non-removable-media devices once it is attached 31513 */ 31514 un->un_f_attach_spinup = TRUE; 31515 31516 /* 31517 * According to SCSI specification, Sense data has two kinds of 31518 * format: fixed format, and descriptor format. At present, we 31519 * don't support descriptor format sense data for removable 31520 * media. 31521 */ 31522 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31523 un->un_f_descr_format_supported = TRUE; 31524 } 31525 31526 /* 31527 * kstats are created only for non-removable media devices. 31528 * 31529 * Set this in sd.conf to 0 in order to disable kstats. The 31530 * default is 1, so they are enabled by default. 31531 */ 31532 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY, 31533 SD_DEVINFO(un), DDI_PROP_DONTPASS, 31534 "enable-partition-kstats", 1)); 31535 31536 /* 31537 * Check if HBA has set the "pm-capable" property. 31538 * If "pm-capable" exists and is non-zero then we can 31539 * power manage the device without checking the start/stop 31540 * cycle count log sense page. 31541 * 31542 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0) 31543 * then we should not power manage the device. 31544 * 31545 * If "pm-capable" doesn't exist then pm_capable_prop will 31546 * be set to SD_PM_CAPABLE_UNDEFINED (-1). In this case, 31547 * sd will check the start/stop cycle count log sense page 31548 * and power manage the device if the cycle count limit has 31549 * not been exceeded. 31550 */ 31551 pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi, 31552 DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED); 31553 if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) { 31554 un->un_f_log_sense_supported = TRUE; 31555 } else { 31556 /* 31557 * pm-capable property exists. 31558 * 31559 * Convert "TRUE" values for pm_capable_prop to 31560 * SD_PM_CAPABLE_TRUE (1) to make it easier to check 31561 * later. "TRUE" values are any values except 31562 * SD_PM_CAPABLE_FALSE (0) and 31563 * SD_PM_CAPABLE_UNDEFINED (-1) 31564 */ 31565 if (pm_capable_prop == SD_PM_CAPABLE_FALSE) { 31566 un->un_f_log_sense_supported = FALSE; 31567 } else { 31568 un->un_f_pm_supported = TRUE; 31569 } 31570 31571 SD_INFO(SD_LOG_ATTACH_DETACH, un, 31572 "sd_unit_attach: un:0x%p pm-capable " 31573 "property set to %d.\n", un, un->un_f_pm_supported); 31574 } 31575 } 31576 31577 if (un->un_f_is_hotpluggable) { 31578 #if defined(_SUNOS_VTOC_8) 31579 /* 31580 * Note: currently, for VTOC_8 devices, default label is 31581 * created for removable and hotpluggable devices only. 31582 */ 31583 un->un_f_default_vtoc_supported = TRUE; 31584 #endif 31585 31586 /* 31587 * Have to watch hotpluggable devices as well, since 31588 * that's the only way for userland applications to 31589 * detect hot removal while device is busy/mounted. 31590 */ 31591 un->un_f_monitor_media_state = TRUE; 31592 31593 un->un_f_check_start_stop = TRUE; 31594 31595 } 31596 31597 /* 31598 * By default, only DIRECT ACCESS devices and CDs will have Sun 31599 * labels. 31600 */ 31601 if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) || 31602 (un->un_sd->sd_inq->inq_rmb)) { 31603 /* 31604 * Direct access devices have disk label 31605 */ 31606 un->un_f_vtoc_label_supported = TRUE; 31607 } 31608 31609 /* 31610 * Fdisk partitions are supported for all direct access devices on 31611 * x86 platform, and just for removable media and hotpluggable 31612 * devices on SPARC platform. Later, we will set the following flag 31613 * to FALSE if current device is not removable media or hotpluggable 31614 * device and if sd works on SAPRC platform. 31615 */ 31616 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) { 31617 un->un_f_mboot_supported = TRUE; 31618 } 31619 31620 if (!un->un_f_is_hotpluggable && 31621 !un->un_sd->sd_inq->inq_rmb) { 31622 31623 #if defined(_SUNOS_VTOC_8) 31624 /* 31625 * Don't support fdisk on fixed disk 31626 */ 31627 un->un_f_mboot_supported = FALSE; 31628 #endif 31629 31630 /* 31631 * For fixed disk, if its VTOC is not valid, we will write 31632 * errlog into system log 31633 */ 31634 if (un->un_f_vtoc_label_supported) 31635 un->un_f_vtoc_errlog_supported = TRUE; 31636 } 31637 } 31638